1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3235828Stuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4235828Stuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5163953Srrs *
6163953Srrs * Redistribution and use in source and binary forms, with or without
7163953Srrs * modification, are permitted provided that the following conditions are met:
8163953Srrs *
9163953Srrs * a) Redistributions of source code must retain the above copyright notice,
10228653Stuexen *    this list of conditions and the following disclaimer.
11163953Srrs *
12163953Srrs * b) Redistributions in binary form must reproduce the above copyright
13163953Srrs *    notice, this list of conditions and the following disclaimer in
14228653Stuexen *    the documentation and/or other materials provided with the distribution.
15163953Srrs *
16163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
17163953Srrs *    contributors may be used to endorse or promote products derived
18163953Srrs *    from this software without specific prior written permission.
19163953Srrs *
20163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
31163953Srrs */
32163953Srrs
33163953Srrs#include <sys/cdefs.h>
34163953Srrs__FBSDID("$FreeBSD$");
35163953Srrs
36163953Srrs#define _IP_VHL
37166086Srrs#include <netinet/sctp_os.h>
38163953Srrs#include <netinet/sctp_pcb.h>
39163953Srrs#ifdef INET6
40163953Srrs#endif
41163953Srrs#include <netinet/sctp_var.h>
42167598Srrs#include <netinet/sctp_sysctl.h>
43163953Srrs#include <netinet/sctp_timer.h>
44163953Srrs#include <netinet/sctputil.h>
45163953Srrs#include <netinet/sctp_output.h>
46163953Srrs#include <netinet/sctp_header.h>
47163953Srrs#include <netinet/sctp_indata.h>
48163953Srrs#include <netinet/sctp_asconf.h>
49163953Srrs#include <netinet/sctp_input.h>
50163953Srrs#include <netinet/sctp.h>
51163953Srrs#include <netinet/sctp_uio.h>
52270350Stuexen#if defined(INET) || defined(INET6)
53185694Srrs#include <netinet/udp.h>
54270350Stuexen#endif
55163953Srrs
56163953Srrs
57163953Srrsvoid
58163953Srrssctp_audit_retranmission_queue(struct sctp_association *asoc)
59163953Srrs{
60163953Srrs	struct sctp_tmit_chunk *chk;
61163953Srrs
62169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit invoked on send queue cnt:%d onqueue:%d\n",
63169420Srrs	    asoc->sent_queue_retran_cnt,
64169420Srrs	    asoc->sent_queue_cnt);
65163953Srrs	asoc->sent_queue_retran_cnt = 0;
66163953Srrs	asoc->sent_queue_cnt = 0;
67163953Srrs	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
68163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
69163953Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
70163953Srrs		}
71163953Srrs		asoc->sent_queue_cnt++;
72163953Srrs	}
73163953Srrs	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
74163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
75163953Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
76163953Srrs		}
77163953Srrs	}
78179157Srrs	TAILQ_FOREACH(chk, &asoc->asconf_send_queue, sctp_next) {
79179157Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
80179157Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
81179157Srrs		}
82179157Srrs	}
83169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit completes retran:%d onqueue:%d\n",
84169420Srrs	    asoc->sent_queue_retran_cnt,
85169420Srrs	    asoc->sent_queue_cnt);
86163953Srrs}
87163953Srrs
88296052Stuexenstatic int
89163953Srrssctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
90163953Srrs    struct sctp_nets *net, uint16_t threshold)
91163953Srrs{
92163953Srrs	if (net) {
93163953Srrs		net->error_count++;
94169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER4, "Error count for %p now %d thresh:%d\n",
95240148Stuexen		    (void *)net, net->error_count,
96169420Srrs		    net->failure_threshold);
97163953Srrs		if (net->error_count > net->failure_threshold) {
98163953Srrs			/* We had a threshold failure */
99163953Srrs			if (net->dest_state & SCTP_ADDR_REACHABLE) {
100163953Srrs				net->dest_state &= ~SCTP_ADDR_REACHABLE;
101167598Srrs				net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
102224641Stuexen				net->dest_state &= ~SCTP_ADDR_PF;
103163953Srrs				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
104235414Stuexen				    stcb, 0,
105172090Srrs				    (void *)net, SCTP_SO_NOT_LOCKED);
106163953Srrs			}
107224641Stuexen		} else if ((net->pf_threshold < net->failure_threshold) &&
108224641Stuexen		    (net->error_count > net->pf_threshold)) {
109224641Stuexen			if (!(net->dest_state & SCTP_ADDR_PF)) {
110224641Stuexen				net->dest_state |= SCTP_ADDR_PF;
111224641Stuexen				net->last_active = sctp_get_tick_count();
112224641Stuexen				sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
113283822Stuexen				sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
114296052Stuexen				    inp, stcb, net,
115283822Stuexen				    SCTP_FROM_SCTP_TIMER + SCTP_LOC_1);
116296052Stuexen				sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
117224641Stuexen			}
118163953Srrs		}
119163953Srrs	}
120163953Srrs	if (stcb == NULL)
121163953Srrs		return (0);
122163953Srrs
123163953Srrs	if (net) {
124163953Srrs		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
125179783Srrs			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
126171943Srrs				sctp_misc_ints(SCTP_THRESHOLD_INCR,
127171943Srrs				    stcb->asoc.overall_error_count,
128171943Srrs				    (stcb->asoc.overall_error_count + 1),
129171943Srrs				    SCTP_FROM_SCTP_TIMER,
130171943Srrs				    __LINE__);
131171943Srrs			}
132163953Srrs			stcb->asoc.overall_error_count++;
133163953Srrs		}
134163953Srrs	} else {
135179783Srrs		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
136171943Srrs			sctp_misc_ints(SCTP_THRESHOLD_INCR,
137171943Srrs			    stcb->asoc.overall_error_count,
138171943Srrs			    (stcb->asoc.overall_error_count + 1),
139171943Srrs			    SCTP_FROM_SCTP_TIMER,
140171943Srrs			    __LINE__);
141171943Srrs		}
142163953Srrs		stcb->asoc.overall_error_count++;
143163953Srrs	}
144169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n",
145240148Stuexen	    (void *)&stcb->asoc, stcb->asoc.overall_error_count,
146169420Srrs	    (uint32_t) threshold,
147169420Srrs	    ((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
148163953Srrs	/*
149163953Srrs	 * We specifically do not do >= to give the assoc one more change
150163953Srrs	 * before we fail it.
151163953Srrs	 */
152163953Srrs	if (stcb->asoc.overall_error_count > threshold) {
153163953Srrs		/* Abort notification sends a ULP notify */
154267723Stuexen		struct mbuf *op_err;
155163953Srrs
156294145Stuexen		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
157283723Stuexen		    "Association error counter exceeded");
158283822Stuexen		inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_2;
159267723Stuexen		sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
160163953Srrs		return (1);
161163953Srrs	}
162163953Srrs	return (0);
163163953Srrs}
164163953Srrs
165214928Stuexen/*
166214928Stuexen * sctp_find_alternate_net() returns a non-NULL pointer as long
167214928Stuexen * the argument net is non-NULL.
168214928Stuexen */
169163953Srrsstruct sctp_nets *
170163953Srrssctp_find_alternate_net(struct sctp_tcb *stcb,
171163953Srrs    struct sctp_nets *net,
172171440Srrs    int mode)
173163953Srrs{
174163953Srrs	/* Find and return an alternate network if possible */
175171440Srrs	struct sctp_nets *alt, *mnet, *min_errors_net = NULL, *max_cwnd_net = NULL;
176163953Srrs	int once;
177163953Srrs
178171440Srrs	/* JRS 5/14/07 - Initialize min_errors to an impossible value. */
179171440Srrs	int min_errors = -1;
180171440Srrs	uint32_t max_cwnd = 0;
181171440Srrs
182163953Srrs	if (stcb->asoc.numnets == 1) {
183163953Srrs		/* No others but net */
184163953Srrs		return (TAILQ_FIRST(&stcb->asoc.nets));
185163953Srrs	}
186171440Srrs	/*
187171440Srrs	 * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate
188171440Srrs	 * net algorithm. This algorithm chooses the active destination (not
189171440Srrs	 * in PF state) with the largest cwnd value. If all destinations are
190171440Srrs	 * in PF state, unreachable, or unconfirmed, choose the desination
191171440Srrs	 * that is in PF state with the lowest error count. In case of a
192171440Srrs	 * tie, choose the destination that was most recently active.
193171440Srrs	 */
194171440Srrs	if (mode == 2) {
195163953Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
196171440Srrs			/*
197171440Srrs			 * JRS 5/14/07 - If the destination is unreachable
198171440Srrs			 * or unconfirmed, skip it.
199171440Srrs			 */
200163953Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
201171440Srrs			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
202171440Srrs				continue;
203171440Srrs			}
204171440Srrs			/*
205171440Srrs			 * JRS 5/14/07 -  If the destination is reachable
206171440Srrs			 * but in PF state, compare the error count of the
207171440Srrs			 * destination to the minimum error count seen thus
208171440Srrs			 * far. Store the destination with the lower error
209171440Srrs			 * count.  If the error counts are equal, store the
210171440Srrs			 * destination that was most recently active.
211171440Srrs			 */
212171440Srrs			if (mnet->dest_state & SCTP_ADDR_PF) {
213171440Srrs				/*
214171440Srrs				 * JRS 5/14/07 - If the destination under
215171440Srrs				 * consideration is the current destination,
216171440Srrs				 * work as if the error count is one higher.
217171440Srrs				 * The actual error count will not be
218171440Srrs				 * incremented until later in the t3
219171440Srrs				 * handler.
220171440Srrs				 */
221171440Srrs				if (mnet == net) {
222171440Srrs					if (min_errors == -1) {
223171440Srrs						min_errors = mnet->error_count + 1;
224171440Srrs						min_errors_net = mnet;
225171440Srrs					} else if (mnet->error_count + 1 < min_errors) {
226171440Srrs						min_errors = mnet->error_count + 1;
227171440Srrs						min_errors_net = mnet;
228171440Srrs					} else if (mnet->error_count + 1 == min_errors
229171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
230171440Srrs						min_errors_net = mnet;
231171440Srrs						min_errors = mnet->error_count + 1;
232171440Srrs					}
233171440Srrs					continue;
234171440Srrs				} else {
235171440Srrs					if (min_errors == -1) {
236171440Srrs						min_errors = mnet->error_count;
237171440Srrs						min_errors_net = mnet;
238171440Srrs					} else if (mnet->error_count < min_errors) {
239171440Srrs						min_errors = mnet->error_count;
240171440Srrs						min_errors_net = mnet;
241171440Srrs					} else if (mnet->error_count == min_errors
242171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
243171440Srrs						min_errors_net = mnet;
244171440Srrs						min_errors = mnet->error_count;
245171440Srrs					}
246171440Srrs					continue;
247171440Srrs				}
248171440Srrs			}
249171440Srrs			/*
250171440Srrs			 * JRS 5/14/07 - If the destination is reachable and
251171440Srrs			 * not in PF state, compare the cwnd of the
252171440Srrs			 * destination to the highest cwnd seen thus far.
253171440Srrs			 * Store the destination with the higher cwnd value.
254171440Srrs			 * If the cwnd values are equal, randomly choose one
255171440Srrs			 * of the two destinations.
256171440Srrs			 */
257171440Srrs			if (max_cwnd < mnet->cwnd) {
258171440Srrs				max_cwnd_net = mnet;
259171440Srrs				max_cwnd = mnet->cwnd;
260171440Srrs			} else if (max_cwnd == mnet->cwnd) {
261171440Srrs				uint32_t rndval;
262171440Srrs				uint8_t this_random;
263171440Srrs
264171440Srrs				if (stcb->asoc.hb_random_idx > 3) {
265171440Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
266171440Srrs					memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values));
267171440Srrs					this_random = stcb->asoc.hb_random_values[0];
268171440Srrs					stcb->asoc.hb_random_idx++;
269171440Srrs					stcb->asoc.hb_ect_randombit = 0;
270171440Srrs				} else {
271171440Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
272171440Srrs					stcb->asoc.hb_random_idx++;
273171440Srrs					stcb->asoc.hb_ect_randombit = 0;
274171440Srrs				}
275171440Srrs				if (this_random % 2 == 1) {
276171440Srrs					max_cwnd_net = mnet;
277180387Srrs					max_cwnd = mnet->cwnd;	/* Useless? */
278171440Srrs				}
279171440Srrs			}
280171440Srrs		}
281171440Srrs		if (max_cwnd_net == NULL) {
282171440Srrs			if (min_errors_net == NULL) {
283171440Srrs				return (net);
284171440Srrs			}
285171440Srrs			return (min_errors_net);
286171440Srrs		} else {
287171440Srrs			return (max_cwnd_net);
288171440Srrs		}
289171440Srrs	}
290171440Srrs	/*
291171440Srrs	 * JRS 5/14/07 - If mode is set to 1, use the CMT policy for
292171440Srrs	 * choosing an alternate net.
293171440Srrs	 */
294171440Srrs	else if (mode == 1) {
295171440Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
296171440Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
297214928Stuexen			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
298163953Srrs				/*
299163953Srrs				 * will skip ones that are not-reachable or
300163953Srrs				 * unconfirmed
301163953Srrs				 */
302163953Srrs				continue;
303163953Srrs			}
304171440Srrs			if (max_cwnd < mnet->cwnd) {
305171440Srrs				max_cwnd_net = mnet;
306171440Srrs				max_cwnd = mnet->cwnd;
307171440Srrs			} else if (max_cwnd == mnet->cwnd) {
308163953Srrs				uint32_t rndval;
309163953Srrs				uint8_t this_random;
310163953Srrs
311163953Srrs				if (stcb->asoc.hb_random_idx > 3) {
312163953Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
313163953Srrs					memcpy(stcb->asoc.hb_random_values, &rndval,
314163953Srrs					    sizeof(stcb->asoc.hb_random_values));
315163953Srrs					this_random = stcb->asoc.hb_random_values[0];
316163953Srrs					stcb->asoc.hb_random_idx = 0;
317163953Srrs					stcb->asoc.hb_ect_randombit = 0;
318163953Srrs				} else {
319163953Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
320163953Srrs					stcb->asoc.hb_random_idx++;
321163953Srrs					stcb->asoc.hb_ect_randombit = 0;
322163953Srrs				}
323163953Srrs				if (this_random % 2) {
324171440Srrs					max_cwnd_net = mnet;
325171440Srrs					max_cwnd = mnet->cwnd;
326163953Srrs				}
327163953Srrs			}
328163953Srrs		}
329171440Srrs		if (max_cwnd_net) {
330171440Srrs			return (max_cwnd_net);
331163953Srrs		}
332163953Srrs	}
333163953Srrs	mnet = net;
334163953Srrs	once = 0;
335163953Srrs
336163953Srrs	if (mnet == NULL) {
337163953Srrs		mnet = TAILQ_FIRST(&stcb->asoc.nets);
338212225Srrs		if (mnet == NULL) {
339212225Srrs			return (NULL);
340212225Srrs		}
341163953Srrs	}
342283720Stuexen	for (;;) {
343163953Srrs		alt = TAILQ_NEXT(mnet, sctp_next);
344163953Srrs		if (alt == NULL) {
345163953Srrs			once++;
346163953Srrs			if (once > 1) {
347163953Srrs				break;
348163953Srrs			}
349163953Srrs			alt = TAILQ_FIRST(&stcb->asoc.nets);
350212225Srrs			if (alt == NULL) {
351212225Srrs				return (NULL);
352212225Srrs			}
353163953Srrs		}
354163953Srrs		if (alt->ro.ro_rt == NULL) {
355167598Srrs			if (alt->ro._s_addr) {
356167598Srrs				sctp_free_ifa(alt->ro._s_addr);
357167598Srrs				alt->ro._s_addr = NULL;
358167598Srrs			}
359163953Srrs			alt->src_addr_selected = 0;
360163953Srrs		}
361214928Stuexen		if (((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
362163953Srrs		    (alt->ro.ro_rt != NULL) &&
363214928Stuexen		    (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))) {
364163953Srrs			/* Found a reachable address */
365163953Srrs			break;
366163953Srrs		}
367163953Srrs		mnet = alt;
368283720Stuexen	}
369163953Srrs
370163953Srrs	if (alt == NULL) {
371163953Srrs		/* Case where NO insv network exists (dormant state) */
372163953Srrs		/* we rotate destinations */
373163953Srrs		once = 0;
374163953Srrs		mnet = net;
375283720Stuexen		for (;;) {
376212225Srrs			if (mnet == NULL) {
377212225Srrs				return (TAILQ_FIRST(&stcb->asoc.nets));
378212225Srrs			}
379163953Srrs			alt = TAILQ_NEXT(mnet, sctp_next);
380163953Srrs			if (alt == NULL) {
381163953Srrs				once++;
382163953Srrs				if (once > 1) {
383163953Srrs					break;
384163953Srrs				}
385163953Srrs				alt = TAILQ_FIRST(&stcb->asoc.nets);
386283720Stuexen				if (alt == NULL) {
387283720Stuexen					break;
388283720Stuexen				}
389163953Srrs			}
390163953Srrs			if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
391163953Srrs			    (alt != net)) {
392163953Srrs				/* Found an alternate address */
393163953Srrs				break;
394163953Srrs			}
395163953Srrs			mnet = alt;
396283720Stuexen		}
397163953Srrs	}
398163953Srrs	if (alt == NULL) {
399163953Srrs		return (net);
400163953Srrs	}
401163953Srrs	return (alt);
402163953Srrs}
403163953Srrs
404163953Srrsstatic void
405163953Srrssctp_backoff_on_timeout(struct sctp_tcb *stcb,
406163953Srrs    struct sctp_nets *net,
407163953Srrs    int win_probe,
408210599Srrs    int num_marked, int num_abandoned)
409163953Srrs{
410170642Srrs	if (net->RTO == 0) {
411294173Stuexen		if (net->RTO_measured) {
412294173Stuexen			net->RTO = stcb->asoc.minrto;
413294173Stuexen		} else {
414294173Stuexen			net->RTO = stcb->asoc.initial_rto;
415294173Stuexen		}
416170642Srrs	}
417163953Srrs	net->RTO <<= 1;
418163953Srrs	if (net->RTO > stcb->asoc.maxrto) {
419163953Srrs		net->RTO = stcb->asoc.maxrto;
420163953Srrs	}
421210599Srrs	if ((win_probe == 0) && (num_marked || num_abandoned)) {
422163953Srrs		/* We don't apply penalty to window probe scenarios */
423171440Srrs		/* JRS - Use the congestion control given in the CC module */
424171440Srrs		stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
425163953Srrs	}
426163953Srrs}
427163953Srrs
428184333Srrs#ifndef INVARIANTS
429184333Srrsstatic void
430182367Srrssctp_recover_sent_list(struct sctp_tcb *stcb)
431182367Srrs{
432216822Stuexen	struct sctp_tmit_chunk *chk, *nchk;
433182367Srrs	struct sctp_association *asoc;
434182367Srrs
435182367Srrs	asoc = &stcb->asoc;
436216822Stuexen	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
437216825Stuexen		if (SCTP_TSN_GE(asoc->last_acked_seq, chk->rec.data.TSN_seq)) {
438182367Srrs			SCTP_PRINTF("Found chk:%p tsn:%x <= last_acked_seq:%x\n",
439240148Stuexen			    (void *)chk, chk->rec.data.TSN_seq, asoc->last_acked_seq);
440243157Stuexen			if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
441242714Stuexen				if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
442242714Stuexen					asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
443242714Stuexen				}
444242714Stuexen			}
445294210Stuexen			if ((asoc->strmout[chk->rec.data.stream_number].chunks_on_queues == 0) &&
446294210Stuexen			    (asoc->strmout[chk->rec.data.stream_number].state == SCTP_STREAM_RESET_PENDING) &&
447294210Stuexen			    TAILQ_EMPTY(&asoc->strmout[chk->rec.data.stream_number].outqueue)) {
448294210Stuexen				asoc->trigger_reset = 1;
449294210Stuexen			}
450182367Srrs			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
451255190Stuexen			if (PR_SCTP_ENABLED(chk->flags)) {
452182367Srrs				if (asoc->pr_sctp_cnt != 0)
453182367Srrs					asoc->pr_sctp_cnt--;
454182367Srrs			}
455182367Srrs			if (chk->data) {
456182367Srrs				/* sa_ignore NO_NULL_CHK */
457182367Srrs				sctp_free_bufspace(stcb, asoc, chk, 1);
458182367Srrs				sctp_m_freem(chk->data);
459216822Stuexen				chk->data = NULL;
460270357Stuexen				if (asoc->prsctp_supported && PR_SCTP_BUF_ENABLED(chk->flags)) {
461182367Srrs					asoc->sent_queue_cnt_removeable--;
462182367Srrs				}
463182367Srrs			}
464182367Srrs			asoc->sent_queue_cnt--;
465221627Stuexen			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
466182367Srrs		}
467182367Srrs	}
468182367Srrs	SCTP_PRINTF("after recover order is as follows\n");
469216822Stuexen	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
470240148Stuexen		SCTP_PRINTF("chk:%p TSN:%x\n", (void *)chk, chk->rec.data.TSN_seq);
471182367Srrs	}
472182367Srrs}
473182367Srrs
474184333Srrs#endif
475184333Srrs
476163953Srrsstatic int
477163953Srrssctp_mark_all_for_resend(struct sctp_tcb *stcb,
478163953Srrs    struct sctp_nets *net,
479163953Srrs    struct sctp_nets *alt,
480163953Srrs    int window_probe,
481210599Srrs    int *num_marked,
482210599Srrs    int *num_abandoned)
483163953Srrs{
484163953Srrs
485163953Srrs	/*
486163953Srrs	 * Mark all chunks (well not all) that were sent to *net for
487163953Srrs	 * retransmission. Move them to alt for there destination as well...
488163953Srrs	 * We only mark chunks that have been outstanding long enough to
489163953Srrs	 * have received feed-back.
490163953Srrs	 */
491216822Stuexen	struct sctp_tmit_chunk *chk, *nchk;
492163953Srrs	struct sctp_nets *lnets;
493163953Srrs	struct timeval now, min_wait, tv;
494219014Stuexen	int cur_rto;
495210599Srrs	int cnt_abandoned;
496168709Srrs	int audit_tf, num_mk, fir;
497163953Srrs	unsigned int cnt_mk;
498168709Srrs	uint32_t orig_flight, orig_tf;
499163953Srrs	uint32_t tsnlast, tsnfirst;
500182367Srrs	int recovery_cnt = 0;
501163953Srrs
502171440Srrs
503163953Srrs	/* none in flight now */
504163953Srrs	audit_tf = 0;
505163953Srrs	fir = 0;
506163953Srrs	/*
507163953Srrs	 * figure out how long a data chunk must be pending before we can
508163953Srrs	 * mark it ..
509163953Srrs	 */
510169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&now);
511163953Srrs	/* get cur rto in micro-seconds */
512219014Stuexen	cur_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
513219014Stuexen	cur_rto *= 1000;
514224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
515219014Stuexen		sctp_log_fr(cur_rto,
516170744Srrs		    stcb->asoc.peers_rwnd,
517170744Srrs		    window_probe,
518170744Srrs		    SCTP_FR_T3_MARK_TIME);
519224641Stuexen		sctp_log_fr(net->flight_size, 0, 0, SCTP_FR_CWND_REPORT);
520170744Srrs		sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
521170744Srrs	}
522219014Stuexen	tv.tv_sec = cur_rto / 1000000;
523219014Stuexen	tv.tv_usec = cur_rto % 1000000;
524163953Srrs	min_wait = now;
525163953Srrs	timevalsub(&min_wait, &tv);
526163953Srrs	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
527163953Srrs		/*
528163953Srrs		 * if we hit here, we don't have enough seconds on the clock
529163953Srrs		 * to account for the RTO. We just let the lower seconds be
530163953Srrs		 * the bounds and don't worry about it. This may mean we
531163953Srrs		 * will mark a lot more than we should.
532163953Srrs		 */
533163953Srrs		min_wait.tv_sec = min_wait.tv_usec = 0;
534163953Srrs	}
535224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
536219014Stuexen		sctp_log_fr(cur_rto, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
537170744Srrs		sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
538170744Srrs	}
539163953Srrs	/*
540163953Srrs	 * Our rwnd will be incorrect here since we are not adding back the
541163953Srrs	 * cnt * mbuf but we will fix that down below.
542163953Srrs	 */
543163953Srrs	orig_flight = net->flight_size;
544168709Srrs	orig_tf = stcb->asoc.total_flight;
545168709Srrs
546163953Srrs	net->fast_retran_ip = 0;
547163953Srrs	/* Now on to each chunk */
548210599Srrs	cnt_abandoned = 0;
549163953Srrs	num_mk = cnt_mk = 0;
550163953Srrs	tsnfirst = tsnlast = 0;
551184333Srrs#ifndef INVARIANTS
552182367Srrsstart_again:
553184333Srrs#endif
554216822Stuexen	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.sent_queue, sctp_next, nchk) {
555216825Stuexen		if (SCTP_TSN_GE(stcb->asoc.last_acked_seq, chk->rec.data.TSN_seq)) {
556163953Srrs			/* Strange case our list got out of order? */
557258454Stuexen			SCTP_PRINTF("Our list is out of order? last_acked:%x chk:%x\n",
558182367Srrs			    (unsigned int)stcb->asoc.last_acked_seq, (unsigned int)chk->rec.data.TSN_seq);
559182367Srrs			recovery_cnt++;
560182367Srrs#ifdef INVARIANTS
561182367Srrs			panic("last acked >= chk on sent-Q");
562182367Srrs#else
563182367Srrs			SCTP_PRINTF("Recover attempts a restart cnt:%d\n", recovery_cnt);
564182367Srrs			sctp_recover_sent_list(stcb);
565182367Srrs			if (recovery_cnt < 10) {
566182367Srrs				goto start_again;
567182367Srrs			} else {
568182367Srrs				SCTP_PRINTF("Recovery fails %d times??\n", recovery_cnt);
569182367Srrs			}
570182367Srrs#endif
571163953Srrs		}
572163953Srrs		if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
573163953Srrs			/*
574163953Srrs			 * found one to mark: If it is less than
575163953Srrs			 * DATAGRAM_ACKED it MUST not be a skipped or marked
576163953Srrs			 * TSN but instead one that is either already set
577163953Srrs			 * for retransmission OR one that needs
578163953Srrs			 * retransmission.
579163953Srrs			 */
580163953Srrs
581163953Srrs			/* validate its been outstanding long enough */
582224641Stuexen			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
583170744Srrs				sctp_log_fr(chk->rec.data.TSN_seq,
584170744Srrs				    chk->sent_rcv_time.tv_sec,
585170744Srrs				    chk->sent_rcv_time.tv_usec,
586170744Srrs				    SCTP_FR_T3_MARK_TIME);
587170744Srrs			}
588163953Srrs			if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
589163953Srrs				/*
590163953Srrs				 * we have reached a chunk that was sent
591163953Srrs				 * some seconds past our min.. forget it we
592163953Srrs				 * will find no more to send.
593163953Srrs				 */
594224641Stuexen				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
595170744Srrs					sctp_log_fr(0,
596170744Srrs					    chk->sent_rcv_time.tv_sec,
597170744Srrs					    chk->sent_rcv_time.tv_usec,
598170744Srrs					    SCTP_FR_T3_STOPPED);
599170744Srrs				}
600163953Srrs				continue;
601163953Srrs			} else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
602163953Srrs			    (window_probe == 0)) {
603163953Srrs				/*
604163953Srrs				 * we must look at the micro seconds to
605163953Srrs				 * know.
606163953Srrs				 */
607163953Srrs				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
608163953Srrs					/*
609163953Srrs					 * ok it was sent after our boundary
610163953Srrs					 * time.
611163953Srrs					 */
612163953Srrs					continue;
613163953Srrs				}
614163953Srrs			}
615270357Stuexen			if (stcb->asoc.prsctp_supported && PR_SCTP_TTL_ENABLED(chk->flags)) {
616163953Srrs				/* Is it expired? */
617212801Stuexen				if (timevalcmp(&now, &chk->rec.data.timetodrop, >)) {
618163953Srrs					/* Yes so drop it */
619163953Srrs					if (chk->data) {
620169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
621163953Srrs						    chk,
622235416Stuexen						    1,
623189790Srrs						    SCTP_SO_NOT_LOCKED);
624210599Srrs						cnt_abandoned++;
625163953Srrs					}
626185694Srrs					continue;
627163953Srrs				}
628163953Srrs			}
629270357Stuexen			if (stcb->asoc.prsctp_supported && PR_SCTP_RTX_ENABLED(chk->flags)) {
630163953Srrs				/* Has it been retransmitted tv_sec times? */
631163953Srrs				if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
632163953Srrs					if (chk->data) {
633169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
634163953Srrs						    chk,
635235416Stuexen						    1,
636189790Srrs						    SCTP_SO_NOT_LOCKED);
637210599Srrs						cnt_abandoned++;
638163953Srrs					}
639185694Srrs					continue;
640163953Srrs				}
641163953Srrs			}
642168709Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
643163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
644163953Srrs				num_mk++;
645163953Srrs				if (fir == 0) {
646163953Srrs					fir = 1;
647163953Srrs					tsnfirst = chk->rec.data.TSN_seq;
648163953Srrs				}
649163953Srrs				tsnlast = chk->rec.data.TSN_seq;
650224641Stuexen				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
651170744Srrs					sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
652170744Srrs					    0, SCTP_FR_T3_MARKED);
653170744Srrs				}
654168709Srrs				if (chk->rec.data.chunk_was_revoked) {
655168709Srrs					/* deflate the cwnd */
656168709Srrs					chk->whoTo->cwnd -= chk->book_size;
657168709Srrs					chk->rec.data.chunk_was_revoked = 0;
658168709Srrs				}
659168709Srrs				net->marked_retrans++;
660168709Srrs				stcb->asoc.marked_retrans++;
661179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
662170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
663170744Srrs					    chk->whoTo->flight_size,
664170744Srrs					    chk->book_size,
665170744Srrs					    (uintptr_t) chk->whoTo,
666170744Srrs					    chk->rec.data.TSN_seq);
667170744Srrs				}
668168709Srrs				sctp_flight_size_decrease(chk);
669168709Srrs				sctp_total_flight_decrease(stcb, chk);
670168709Srrs				stcb->asoc.peers_rwnd += chk->send_size;
671179783Srrs				stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
672163953Srrs			}
673163953Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
674163953Srrs			SCTP_STAT_INCR(sctps_markedretrans);
675165220Srrs
676163953Srrs			/* reset the TSN for striking and other FR stuff */
677163953Srrs			chk->rec.data.doing_fast_retransmit = 0;
678163953Srrs			/* Clear any time so NO RTT is being done */
679219397Srrs
680219397Srrs			if (chk->do_rtt) {
681219397Srrs				if (chk->whoTo->rto_needed == 0) {
682219397Srrs					chk->whoTo->rto_needed = 1;
683219397Srrs				}
684219397Srrs			}
685163953Srrs			chk->do_rtt = 0;
686163953Srrs			if (alt != net) {
687163953Srrs				sctp_free_remote_addr(chk->whoTo);
688163953Srrs				chk->no_fr_allowed = 1;
689163953Srrs				chk->whoTo = alt;
690163953Srrs				atomic_add_int(&alt->ref_count, 1);
691163953Srrs			} else {
692163953Srrs				chk->no_fr_allowed = 0;
693163953Srrs				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
694163953Srrs					chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
695163953Srrs				} else {
696163953Srrs					chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
697163953Srrs				}
698163953Srrs			}
699170181Srrs			/*
700170181Srrs			 * CMT: Do not allow FRs on retransmitted TSNs.
701170181Srrs			 */
702216669Stuexen			if (stcb->asoc.sctp_cmt_on_off > 0) {
703163953Srrs				chk->no_fr_allowed = 1;
704163953Srrs			}
705210599Srrs#ifdef THIS_SHOULD_NOT_BE_DONE
706163953Srrs		} else if (chk->sent == SCTP_DATAGRAM_ACKED) {
707163953Srrs			/* remember highest acked one */
708163953Srrs			could_be_sent = chk;
709210599Srrs#endif
710163953Srrs		}
711163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
712163953Srrs			cnt_mk++;
713163953Srrs		}
714163953Srrs	}
715168709Srrs	if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
716168709Srrs		/* we did not subtract the same things? */
717168709Srrs		audit_tf = 1;
718168709Srrs	}
719224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
720170744Srrs		sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
721170744Srrs	}
722163953Srrs#ifdef SCTP_DEBUG
723169420Srrs	if (num_mk) {
724169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
725169420Srrs		    tsnlast);
726169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%ld\n",
727169420Srrs		    num_mk, (u_long)stcb->asoc.peers_rwnd);
728169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
729169420Srrs		    tsnlast);
730169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%d\n",
731169420Srrs		    num_mk,
732169420Srrs		    (int)stcb->asoc.peers_rwnd);
733163953Srrs	}
734163953Srrs#endif
735163953Srrs	*num_marked = num_mk;
736210599Srrs	*num_abandoned = cnt_abandoned;
737210493Srrs	/*
738210493Srrs	 * Now check for a ECN Echo that may be stranded And include the
739210493Srrs	 * cnt_mk'd to have all resends in the control queue.
740210493Srrs	 */
741210493Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
742210493Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
743210493Srrs			cnt_mk++;
744210493Srrs		}
745210493Srrs		if ((chk->whoTo == net) &&
746210493Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
747210493Srrs			sctp_free_remote_addr(chk->whoTo);
748210493Srrs			chk->whoTo = alt;
749210493Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
750210493Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
751210493Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
752210493Srrs				cnt_mk++;
753210493Srrs			}
754210493Srrs			atomic_add_int(&alt->ref_count, 1);
755210493Srrs		}
756210493Srrs	}
757210599Srrs#ifdef THIS_SHOULD_NOT_BE_DONE
758163953Srrs	if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
759163953Srrs		/* fix it so we retransmit the highest acked anyway */
760163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
761163953Srrs		cnt_mk++;
762163953Srrs		could_be_sent->sent = SCTP_DATAGRAM_RESEND;
763163953Srrs	}
764210599Srrs#endif
765163953Srrs	if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
766165220Srrs#ifdef INVARIANTS
767171477Srrs		SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
768171477Srrs		    cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
769163953Srrs#endif
770163953Srrs#ifndef SCTP_AUDITING_ENABLED
771163953Srrs		stcb->asoc.sent_queue_retran_cnt = cnt_mk;
772163953Srrs#endif
773163953Srrs	}
774163953Srrs	if (audit_tf) {
775169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER4,
776169420Srrs		    "Audit total flight due to negative value net:%p\n",
777240148Stuexen		    (void *)net);
778163953Srrs		stcb->asoc.total_flight = 0;
779163953Srrs		stcb->asoc.total_flight_count = 0;
780163953Srrs		/* Clear all networks flight size */
781163953Srrs		TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
782163953Srrs			lnets->flight_size = 0;
783169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER4,
784169420Srrs			    "Net:%p c-f cwnd:%d ssthresh:%d\n",
785240148Stuexen			    (void *)lnets, lnets->cwnd, lnets->ssthresh);
786163953Srrs		}
787163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
788163953Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
789179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
790170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
791170744Srrs					    chk->whoTo->flight_size,
792170744Srrs					    chk->book_size,
793170744Srrs					    (uintptr_t) chk->whoTo,
794170744Srrs					    chk->rec.data.TSN_seq);
795170744Srrs				}
796168709Srrs				sctp_flight_size_increase(chk);
797168709Srrs				sctp_total_flight_increase(stcb, chk);
798163953Srrs			}
799163953Srrs		}
800163953Srrs	}
801163953Srrs	/* We return 1 if we only have a window probe outstanding */
802163953Srrs	return (0);
803163953Srrs}
804163953Srrs
805163953Srrs
806163953Srrsint
807163953Srrssctp_t3rxt_timer(struct sctp_inpcb *inp,
808163953Srrs    struct sctp_tcb *stcb,
809163953Srrs    struct sctp_nets *net)
810163953Srrs{
811163953Srrs	struct sctp_nets *alt;
812210599Srrs	int win_probe, num_mk, num_abandoned;
813163953Srrs
814179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
815170744Srrs		sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
816170744Srrs	}
817179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
818163953Srrs		struct sctp_nets *lnet;
819163953Srrs
820163953Srrs		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
821163953Srrs			if (net == lnet) {
822163953Srrs				sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
823163953Srrs			} else {
824163953Srrs				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
825163953Srrs			}
826163953Srrs		}
827163953Srrs	}
828163953Srrs	/* Find an alternate and mark those for retransmission */
829163953Srrs	if ((stcb->asoc.peers_rwnd == 0) &&
830163953Srrs	    (stcb->asoc.total_flight < net->mtu)) {
831163953Srrs		SCTP_STAT_INCR(sctps_timowindowprobe);
832163953Srrs		win_probe = 1;
833163953Srrs	} else {
834163953Srrs		win_probe = 0;
835163953Srrs	}
836168709Srrs
837163953Srrs	if (win_probe == 0) {
838163953Srrs		/* We don't do normal threshold management on window probes */
839163953Srrs		if (sctp_threshold_management(inp, stcb, net,
840163953Srrs		    stcb->asoc.max_send_times)) {
841163953Srrs			/* Association was destroyed */
842163953Srrs			return (1);
843163953Srrs		} else {
844163953Srrs			if (net != stcb->asoc.primary_destination) {
845163953Srrs				/* send a immediate HB if our RTO is stale */
846163953Srrs				struct timeval now;
847163953Srrs				unsigned int ms_goneby;
848163953Srrs
849169378Srrs				(void)SCTP_GETTIME_TIMEVAL(&now);
850163953Srrs				if (net->last_sent_time.tv_sec) {
851163953Srrs					ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
852163953Srrs				} else {
853163953Srrs					ms_goneby = 0;
854163953Srrs				}
855224641Stuexen				if ((net->dest_state & SCTP_ADDR_PF) == 0) {
856224641Stuexen					if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
857185694Srrs						/*
858224641Stuexen						 * no recent feed back in an
859224641Stuexen						 * RTO or more, request a
860224641Stuexen						 * RTT update
861185694Srrs						 */
862224641Stuexen						sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
863224641Stuexen					}
864163953Srrs				}
865163953Srrs			}
866163953Srrs		}
867163953Srrs	} else {
868163953Srrs		/*
869163953Srrs		 * For a window probe we don't penalize the net's but only
870163953Srrs		 * the association. This may fail it if SACKs are not coming
871163953Srrs		 * back. If sack's are coming with rwnd locked at 0, we will
872163953Srrs		 * continue to hold things waiting for rwnd to raise
873163953Srrs		 */
874163953Srrs		if (sctp_threshold_management(inp, stcb, NULL,
875163953Srrs		    stcb->asoc.max_send_times)) {
876163953Srrs			/* Association was destroyed */
877163953Srrs			return (1);
878163953Srrs		}
879163953Srrs	}
880224641Stuexen	if (stcb->asoc.sctp_cmt_on_off > 0) {
881224641Stuexen		if (net->pf_threshold < net->failure_threshold) {
882224641Stuexen			alt = sctp_find_alternate_net(stcb, net, 2);
883224641Stuexen		} else {
884224641Stuexen			/*
885224641Stuexen			 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is
886224641Stuexen			 * being used, then pick dest with largest ssthresh
887224641Stuexen			 * for any retransmission.
888224641Stuexen			 */
889224641Stuexen			alt = sctp_find_alternate_net(stcb, net, 1);
890224641Stuexen			/*
891224641Stuexen			 * CUCv2: If a different dest is picked for the
892224641Stuexen			 * retransmission, then new (rtx-)pseudo_cumack
893224641Stuexen			 * needs to be tracked for orig dest. Let CUCv2
894224641Stuexen			 * track new (rtx-) pseudo-cumack always.
895224641Stuexen			 */
896224641Stuexen			net->find_pseudo_cumack = 1;
897224641Stuexen			net->find_rtx_pseudo_cumack = 1;
898224641Stuexen		}
899224641Stuexen	} else {
900224641Stuexen		alt = sctp_find_alternate_net(stcb, net, 0);
901224641Stuexen	}
902224641Stuexen
903224641Stuexen	num_mk = 0;
904224641Stuexen	num_abandoned = 0;
905224641Stuexen	(void)sctp_mark_all_for_resend(stcb, net, alt, win_probe,
906224641Stuexen	    &num_mk, &num_abandoned);
907224641Stuexen	/* FR Loss recovery just ended with the T3. */
908224641Stuexen	stcb->asoc.fast_retran_loss_recovery = 0;
909224641Stuexen
910224641Stuexen	/* CMT FR loss recovery ended with the T3 */
911224641Stuexen	net->fast_retran_loss_recovery = 0;
912224641Stuexen	if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
913224641Stuexen	    (net->flight_size == 0)) {
914224641Stuexen		(*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
915224641Stuexen	}
916224641Stuexen	/*
917224641Stuexen	 * setup the sat loss recovery that prevents satellite cwnd advance.
918224641Stuexen	 */
919224641Stuexen	stcb->asoc.sat_t3_loss_recovery = 1;
920224641Stuexen	stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
921224641Stuexen
922224641Stuexen	/* Backoff the timer and cwnd */
923224641Stuexen	sctp_backoff_on_timeout(stcb, net, win_probe, num_mk, num_abandoned);
924224641Stuexen	if ((!(net->dest_state & SCTP_ADDR_REACHABLE)) ||
925224641Stuexen	    (net->dest_state & SCTP_ADDR_PF)) {
926163953Srrs		/* Move all pending over too */
927212712Stuexen		sctp_move_chunks_from_net(stcb, net);
928169352Srrs
929169352Srrs		/*
930169352Srrs		 * Get the address that failed, to force a new src address
931169352Srrs		 * selecton and a route allocation.
932169352Srrs		 */
933169352Srrs		if (net->ro._s_addr) {
934169352Srrs			sctp_free_ifa(net->ro._s_addr);
935169352Srrs			net->ro._s_addr = NULL;
936169352Srrs		}
937169352Srrs		net->src_addr_selected = 0;
938169352Srrs
939169352Srrs		/* Force a route allocation too */
940169352Srrs		if (net->ro.ro_rt) {
941169352Srrs			RTFREE(net->ro.ro_rt);
942169352Srrs			net->ro.ro_rt = NULL;
943169352Srrs		}
944163953Srrs		/* Was it our primary? */
945163953Srrs		if ((stcb->asoc.primary_destination == net) && (alt != net)) {
946163953Srrs			/*
947163953Srrs			 * Yes, note it as such and find an alternate note:
948163953Srrs			 * this means HB code must use this to resent the
949163953Srrs			 * primary if it goes active AND if someone does a
950163953Srrs			 * change-primary then this flag must be cleared
951163953Srrs			 * from any net structures.
952163953Srrs			 */
953224641Stuexen			if (stcb->asoc.alternate) {
954224641Stuexen				sctp_free_remote_addr(stcb->asoc.alternate);
955163953Srrs			}
956224641Stuexen			stcb->asoc.alternate = alt;
957224641Stuexen			atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
958163953Srrs		}
959163953Srrs	}
960163953Srrs	/*
961163953Srrs	 * Special case for cookie-echo'ed case, we don't do output but must
962163953Srrs	 * await the COOKIE-ACK before retransmission
963163953Srrs	 */
964163953Srrs	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
965163953Srrs		/*
966163953Srrs		 * Here we just reset the timer and start again since we
967163953Srrs		 * have not established the asoc
968163953Srrs		 */
969163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
970163953Srrs		return (0);
971163953Srrs	}
972270357Stuexen	if (stcb->asoc.prsctp_supported) {
973163953Srrs		struct sctp_tmit_chunk *lchk;
974163953Srrs
975163953Srrs		lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
976163953Srrs		/* C3. See if we need to send a Fwd-TSN */
977216825Stuexen		if (SCTP_TSN_GT(stcb->asoc.advanced_peer_ack_point, stcb->asoc.last_acked_seq)) {
978163953Srrs			send_forward_tsn(stcb, &stcb->asoc);
979163953Srrs			if (lchk) {
980163953Srrs				/* Assure a timer is up */
981163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
982163953Srrs			}
983163953Srrs		}
984163953Srrs	}
985179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
986170744Srrs		sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
987170744Srrs	}
988163953Srrs	return (0);
989163953Srrs}
990163953Srrs
991163953Srrsint
992163953Srrssctp_t1init_timer(struct sctp_inpcb *inp,
993163953Srrs    struct sctp_tcb *stcb,
994163953Srrs    struct sctp_nets *net)
995163953Srrs{
996163953Srrs	/* bump the thresholds */
997163953Srrs	if (stcb->asoc.delayed_connection) {
998163953Srrs		/*
999163953Srrs		 * special hook for delayed connection. The library did NOT
1000163953Srrs		 * complete the rest of its sends.
1001163953Srrs		 */
1002163953Srrs		stcb->asoc.delayed_connection = 0;
1003172090Srrs		sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1004163953Srrs		return (0);
1005163953Srrs	}
1006163953Srrs	if (SCTP_GET_STATE((&stcb->asoc)) != SCTP_STATE_COOKIE_WAIT) {
1007163953Srrs		return (0);
1008163953Srrs	}
1009163953Srrs	if (sctp_threshold_management(inp, stcb, net,
1010163953Srrs	    stcb->asoc.max_init_times)) {
1011163953Srrs		/* Association was destroyed */
1012163953Srrs		return (1);
1013163953Srrs	}
1014163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1015210599Srrs	sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0, 0);
1016163953Srrs	if (stcb->asoc.initial_init_rto_max < net->RTO) {
1017163953Srrs		net->RTO = stcb->asoc.initial_init_rto_max;
1018163953Srrs	}
1019163953Srrs	if (stcb->asoc.numnets > 1) {
1020163953Srrs		/* If we have more than one addr use it */
1021163953Srrs		struct sctp_nets *alt;
1022163953Srrs
1023163953Srrs		alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1024214928Stuexen		if (alt != stcb->asoc.primary_destination) {
1025212712Stuexen			sctp_move_chunks_from_net(stcb, stcb->asoc.primary_destination);
1026163953Srrs			stcb->asoc.primary_destination = alt;
1027163953Srrs		}
1028163953Srrs	}
1029163953Srrs	/* Send out a new init */
1030172090Srrs	sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1031163953Srrs	return (0);
1032163953Srrs}
1033163953Srrs
1034163953Srrs/*
1035163953Srrs * For cookie and asconf we actually need to find and mark for resend, then
1036163953Srrs * increment the resend counter (after all the threshold management stuff of
1037163953Srrs * course).
1038163953Srrs */
1039163953Srrsint
1040163953Srrssctp_cookie_timer(struct sctp_inpcb *inp,
1041163953Srrs    struct sctp_tcb *stcb,
1042228653Stuexen    struct sctp_nets *net SCTP_UNUSED)
1043163953Srrs{
1044163953Srrs	struct sctp_nets *alt;
1045163953Srrs	struct sctp_tmit_chunk *cookie;
1046163953Srrs
1047163953Srrs	/* first before all else we must find the cookie */
1048163953Srrs	TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1049163953Srrs		if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1050163953Srrs			break;
1051163953Srrs		}
1052163953Srrs	}
1053163953Srrs	if (cookie == NULL) {
1054163953Srrs		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1055163953Srrs			/* FOOBAR! */
1056267723Stuexen			struct mbuf *op_err;
1057163953Srrs
1058294145Stuexen			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
1059267723Stuexen			    "Cookie timer expired, but no cookie");
1060283822Stuexen			inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_3;
1061267723Stuexen			sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
1062163953Srrs		} else {
1063165220Srrs#ifdef INVARIANTS
1064163953Srrs			panic("Cookie timer expires in wrong state?");
1065163953Srrs#else
1066169420Srrs			SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(&stcb->asoc));
1067163953Srrs			return (0);
1068163953Srrs#endif
1069163953Srrs		}
1070163953Srrs		return (0);
1071163953Srrs	}
1072163953Srrs	/* Ok we found the cookie, threshold management next */
1073163953Srrs	if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1074163953Srrs	    stcb->asoc.max_init_times)) {
1075163953Srrs		/* Assoc is over */
1076163953Srrs		return (1);
1077163953Srrs	}
1078163953Srrs	/*
1079163953Srrs	 * cleared theshold management now lets backoff the address & select
1080163953Srrs	 * an alternate
1081163953Srrs	 */
1082163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1083210599Srrs	sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0, 0);
1084163953Srrs	alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1085163953Srrs	if (alt != cookie->whoTo) {
1086163953Srrs		sctp_free_remote_addr(cookie->whoTo);
1087163953Srrs		cookie->whoTo = alt;
1088163953Srrs		atomic_add_int(&alt->ref_count, 1);
1089163953Srrs	}
1090163953Srrs	/* Now mark the retran info */
1091163953Srrs	if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1092163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1093163953Srrs	}
1094163953Srrs	cookie->sent = SCTP_DATAGRAM_RESEND;
1095163953Srrs	/*
1096163953Srrs	 * Now call the output routine to kick out the cookie again, Note we
1097163953Srrs	 * don't mark any chunks for retran so that FR will need to kick in
1098163953Srrs	 * to move these (or a send timer).
1099163953Srrs	 */
1100163953Srrs	return (0);
1101163953Srrs}
1102163953Srrs
1103163953Srrsint
1104163953Srrssctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1105163953Srrs    struct sctp_nets *net)
1106163953Srrs{
1107163953Srrs	struct sctp_nets *alt;
1108163953Srrs	struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1109163953Srrs
1110163953Srrs	if (stcb->asoc.stream_reset_outstanding == 0) {
1111163953Srrs		return (0);
1112163953Srrs	}
1113163953Srrs	/* find the existing STRRESET, we use the seq number we sent out on */
1114169420Srrs	(void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1115163953Srrs	if (strrst == NULL) {
1116163953Srrs		return (0);
1117163953Srrs	}
1118163953Srrs	/* do threshold management */
1119163953Srrs	if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1120163953Srrs	    stcb->asoc.max_send_times)) {
1121163953Srrs		/* Assoc is over */
1122163953Srrs		return (1);
1123163953Srrs	}
1124163953Srrs	/*
1125163953Srrs	 * cleared theshold management now lets backoff the address & select
1126163953Srrs	 * an alternate
1127163953Srrs	 */
1128210599Srrs	sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0, 0);
1129163953Srrs	alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
1130163953Srrs	sctp_free_remote_addr(strrst->whoTo);
1131163953Srrs	strrst->whoTo = alt;
1132163953Srrs	atomic_add_int(&alt->ref_count, 1);
1133163953Srrs
1134163953Srrs	/* See if a ECN Echo is also stranded */
1135163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1136163953Srrs		if ((chk->whoTo == net) &&
1137163953Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1138163953Srrs			sctp_free_remote_addr(chk->whoTo);
1139163953Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
1140163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
1141163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1142163953Srrs			}
1143163953Srrs			chk->whoTo = alt;
1144163953Srrs			atomic_add_int(&alt->ref_count, 1);
1145163953Srrs		}
1146163953Srrs	}
1147224641Stuexen	if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
1148163953Srrs		/*
1149163953Srrs		 * If the address went un-reachable, we need to move to
1150163953Srrs		 * alternates for ALL chk's in queue
1151163953Srrs		 */
1152212712Stuexen		sctp_move_chunks_from_net(stcb, net);
1153163953Srrs	}
1154163953Srrs	/* mark the retran info */
1155163953Srrs	if (strrst->sent != SCTP_DATAGRAM_RESEND)
1156163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1157163953Srrs	strrst->sent = SCTP_DATAGRAM_RESEND;
1158163953Srrs
1159163953Srrs	/* restart the timer */
1160163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1161163953Srrs	return (0);
1162163953Srrs}
1163163953Srrs
1164163953Srrsint
1165163953Srrssctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1166163953Srrs    struct sctp_nets *net)
1167163953Srrs{
1168163953Srrs	struct sctp_nets *alt;
1169216822Stuexen	struct sctp_tmit_chunk *asconf, *chk;
1170163953Srrs
1171171572Srrs	/* is this a first send, or a retransmission? */
1172179157Srrs	if (TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) {
1173163953Srrs		/* compose a new ASCONF chunk and send it */
1174172190Srrs		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1175163953Srrs	} else {
1176171572Srrs		/*
1177171572Srrs		 * Retransmission of the existing ASCONF is needed
1178171572Srrs		 */
1179163953Srrs
1180163953Srrs		/* find the existing ASCONF */
1181179157Srrs		asconf = TAILQ_FIRST(&stcb->asoc.asconf_send_queue);
1182163953Srrs		if (asconf == NULL) {
1183163953Srrs			return (0);
1184163953Srrs		}
1185163953Srrs		/* do threshold management */
1186163953Srrs		if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1187163953Srrs		    stcb->asoc.max_send_times)) {
1188163953Srrs			/* Assoc is over */
1189163953Srrs			return (1);
1190163953Srrs		}
1191163953Srrs		if (asconf->snd_count > stcb->asoc.max_send_times) {
1192163953Srrs			/*
1193171572Srrs			 * Something is rotten: our peer is not responding
1194171572Srrs			 * to ASCONFs but apparently is to other chunks.
1195171572Srrs			 * i.e. it is not properly handling the chunk type
1196171572Srrs			 * upper bits. Mark this peer as ASCONF incapable
1197171572Srrs			 * and cleanup.
1198163953Srrs			 */
1199169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1200163953Srrs			sctp_asconf_cleanup(stcb, net);
1201163953Srrs			return (0);
1202163953Srrs		}
1203163953Srrs		/*
1204171572Srrs		 * cleared threshold management, so now backoff the net and
1205171572Srrs		 * select an alternate
1206163953Srrs		 */
1207210599Srrs		sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0, 0);
1208163953Srrs		alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
1209179157Srrs		if (asconf->whoTo != alt) {
1210179157Srrs			sctp_free_remote_addr(asconf->whoTo);
1211179157Srrs			asconf->whoTo = alt;
1212179157Srrs			atomic_add_int(&alt->ref_count, 1);
1213179157Srrs		}
1214171572Srrs		/* See if an ECN Echo is also stranded */
1215163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1216163953Srrs			if ((chk->whoTo == net) &&
1217163953Srrs			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1218163953Srrs				sctp_free_remote_addr(chk->whoTo);
1219163953Srrs				chk->whoTo = alt;
1220163953Srrs				if (chk->sent != SCTP_DATAGRAM_RESEND) {
1221163953Srrs					chk->sent = SCTP_DATAGRAM_RESEND;
1222163953Srrs					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1223163953Srrs				}
1224163953Srrs				atomic_add_int(&alt->ref_count, 1);
1225163953Srrs			}
1226163953Srrs		}
1227216822Stuexen		TAILQ_FOREACH(chk, &stcb->asoc.asconf_send_queue, sctp_next) {
1228179157Srrs			if (chk->whoTo != alt) {
1229179157Srrs				sctp_free_remote_addr(chk->whoTo);
1230179157Srrs				chk->whoTo = alt;
1231179157Srrs				atomic_add_int(&alt->ref_count, 1);
1232179157Srrs			}
1233179157Srrs			if (asconf->sent != SCTP_DATAGRAM_RESEND && chk->sent != SCTP_DATAGRAM_UNSENT)
1234179157Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1235179157Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
1236179157Srrs		}
1237224641Stuexen		if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
1238163953Srrs			/*
1239163953Srrs			 * If the address went un-reachable, we need to move
1240171572Srrs			 * to the alternate for ALL chunks in queue
1241163953Srrs			 */
1242212712Stuexen			sctp_move_chunks_from_net(stcb, net);
1243163953Srrs		}
1244163953Srrs		/* mark the retran info */
1245163953Srrs		if (asconf->sent != SCTP_DATAGRAM_RESEND)
1246163953Srrs			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1247163953Srrs		asconf->sent = SCTP_DATAGRAM_RESEND;
1248179157Srrs
1249179157Srrs		/* send another ASCONF if any and we can do */
1250179157Srrs		sctp_send_asconf(stcb, alt, SCTP_ADDR_NOT_LOCKED);
1251163953Srrs	}
1252163953Srrs	return (0);
1253163953Srrs}
1254163953Srrs
1255172091Srrs/* Mobility adaptation */
1256172156Srrsvoid
1257172091Srrssctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1258228653Stuexen    struct sctp_nets *net SCTP_UNUSED)
1259172091Srrs{
1260172091Srrs	if (stcb->asoc.deleted_primary == NULL) {
1261172091Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n");
1262172091Srrs		sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1263172156Srrs		return;
1264172091Srrs	}
1265172091Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary ");
1266172091Srrs	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1267172091Srrs	sctp_free_remote_addr(stcb->asoc.deleted_primary);
1268172091Srrs	stcb->asoc.deleted_primary = NULL;
1269172091Srrs	sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1270172156Srrs	return;
1271172091Srrs}
1272172091Srrs
1273163953Srrs/*
1274163953Srrs * For the shutdown and shutdown-ack, we do not keep one around on the
1275163953Srrs * control queue. This means we must generate a new one and call the general
1276163953Srrs * chunk output routine, AFTER having done threshold management.
1277214928Stuexen * It is assumed that net is non-NULL.
1278163953Srrs */
1279163953Srrsint
1280163953Srrssctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1281163953Srrs    struct sctp_nets *net)
1282163953Srrs{
1283163953Srrs	struct sctp_nets *alt;
1284163953Srrs
1285163953Srrs	/* first threshold managment */
1286163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1287163953Srrs		/* Assoc is over */
1288163953Srrs		return (1);
1289163953Srrs	}
1290214928Stuexen	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1291163953Srrs	/* second select an alternative */
1292163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1293163953Srrs
1294163953Srrs	/* third generate a shutdown into the queue for out net */
1295214928Stuexen	sctp_send_shutdown(stcb, alt);
1296214928Stuexen
1297163953Srrs	/* fourth restart timer */
1298163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1299163953Srrs	return (0);
1300163953Srrs}
1301163953Srrs
1302163953Srrsint
1303163953Srrssctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1304163953Srrs    struct sctp_nets *net)
1305163953Srrs{
1306163953Srrs	struct sctp_nets *alt;
1307163953Srrs
1308163953Srrs	/* first threshold managment */
1309163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1310163953Srrs		/* Assoc is over */
1311163953Srrs		return (1);
1312163953Srrs	}
1313214928Stuexen	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1314163953Srrs	/* second select an alternative */
1315163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1316163953Srrs
1317163953Srrs	/* third generate a shutdown into the queue for out net */
1318163953Srrs	sctp_send_shutdown_ack(stcb, alt);
1319163953Srrs
1320163953Srrs	/* fourth restart timer */
1321163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1322163953Srrs	return (0);
1323163953Srrs}
1324163953Srrs
1325163953Srrsstatic void
1326163953Srrssctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1327163953Srrs    struct sctp_tcb *stcb)
1328163953Srrs{
1329163953Srrs	struct sctp_stream_queue_pending *sp;
1330217760Stuexen	unsigned int i, chks_in_queue = 0;
1331163953Srrs	int being_filled = 0;
1332163953Srrs
1333163953Srrs	/*
1334163953Srrs	 * This function is ONLY called when the send/sent queues are empty.
1335163953Srrs	 */
1336163953Srrs	if ((stcb == NULL) || (inp == NULL))
1337163953Srrs		return;
1338163953Srrs
1339163953Srrs	if (stcb->asoc.sent_queue_retran_cnt) {
1340169420Srrs		SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1341163953Srrs		    stcb->asoc.sent_queue_retran_cnt);
1342163953Srrs		stcb->asoc.sent_queue_retran_cnt = 0;
1343163953Srrs	}
1344217760Stuexen	if (stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1345218241Stuexen		/* No stream scheduler information, initialize scheduler */
1346218241Stuexen		stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 0);
1347218241Stuexen		if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1348218241Stuexen			/* yep, we lost a stream or two */
1349218241Stuexen			SCTP_PRINTF("Found additional streams NOT managed by scheduler, corrected\n");
1350163953Srrs		} else {
1351218241Stuexen			/* no streams lost */
1352163953Srrs			stcb->asoc.total_output_queue_size = 0;
1353163953Srrs		}
1354163953Srrs	}
1355163953Srrs	/* Check to see if some data queued, if so report it */
1356217760Stuexen	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1357217760Stuexen		if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1358217760Stuexen			TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
1359163953Srrs				if (sp->msg_is_complete)
1360163953Srrs					being_filled++;
1361163953Srrs				chks_in_queue++;
1362163953Srrs			}
1363163953Srrs		}
1364163953Srrs	}
1365163953Srrs	if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1366169420Srrs		SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1367163953Srrs		    stcb->asoc.stream_queue_cnt, chks_in_queue);
1368163953Srrs	}
1369163953Srrs	if (chks_in_queue) {
1370163953Srrs		/* call the output queue function */
1371172090Srrs		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1372163953Srrs		if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1373163953Srrs		    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1374163953Srrs			/*
1375163953Srrs			 * Probably should go in and make it go back through
1376163953Srrs			 * and add fragments allowed
1377163953Srrs			 */
1378163953Srrs			if (being_filled == 0) {
1379169420Srrs				SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
1380163953Srrs				    chks_in_queue);
1381163953Srrs			}
1382163953Srrs		}
1383163953Srrs	} else {
1384169420Srrs		SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
1385163953Srrs		    (u_long)stcb->asoc.total_output_queue_size);
1386163953Srrs		stcb->asoc.total_output_queue_size = 0;
1387163953Srrs	}
1388163953Srrs}
1389163953Srrs
1390163953Srrsint
1391163953Srrssctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1392224641Stuexen    struct sctp_nets *net)
1393163953Srrs{
1394224641Stuexen	uint8_t net_was_pf;
1395171440Srrs
1396228907Stuexen	if (net->dest_state & SCTP_ADDR_PF) {
1397228907Stuexen		net_was_pf = 1;
1398228907Stuexen	} else {
1399228907Stuexen		net_was_pf = 0;
1400228907Stuexen	}
1401228907Stuexen	if (net->hb_responded == 0) {
1402228907Stuexen		if (net->ro._s_addr) {
1403228907Stuexen			/*
1404228907Stuexen			 * Invalidate the src address if we did not get a
1405228907Stuexen			 * response last time.
1406228907Stuexen			 */
1407228907Stuexen			sctp_free_ifa(net->ro._s_addr);
1408228907Stuexen			net->ro._s_addr = NULL;
1409228907Stuexen			net->src_addr_selected = 0;
1410224641Stuexen		}
1411228907Stuexen		sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1412228907Stuexen		if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1413228907Stuexen			/* Assoc is over */
1414228907Stuexen			return (1);
1415163953Srrs		}
1416163953Srrs	}
1417228907Stuexen	/* Zero PBA, if it needs it */
1418228907Stuexen	if (net->partial_bytes_acked) {
1419228907Stuexen		net->partial_bytes_acked = 0;
1420228907Stuexen	}
1421163953Srrs	if ((stcb->asoc.total_output_queue_size > 0) &&
1422163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1423163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1424163953Srrs		sctp_audit_stream_queues_for_size(inp, stcb);
1425163953Srrs	}
1426224641Stuexen	if (!(net->dest_state & SCTP_ADDR_NOHB) &&
1427224641Stuexen	    !((net_was_pf == 0) && (net->dest_state & SCTP_ADDR_PF))) {
1428163953Srrs		/*
1429224641Stuexen		 * when move to PF during threshold mangement, a HB has been
1430224641Stuexen		 * queued in that routine
1431163953Srrs		 */
1432234296Stuexen		uint32_t ms_gone_by;
1433234296Stuexen
1434234296Stuexen		if ((net->last_sent_time.tv_sec > 0) ||
1435234296Stuexen		    (net->last_sent_time.tv_usec > 0)) {
1436234296Stuexen			struct timeval diff;
1437234296Stuexen
1438234296Stuexen			SCTP_GETTIME_TIMEVAL(&diff);
1439234296Stuexen			timevalsub(&diff, &net->last_sent_time);
1440234296Stuexen			ms_gone_by = (uint32_t) (diff.tv_sec * 1000) +
1441234296Stuexen			    (uint32_t) (diff.tv_usec / 1000);
1442234296Stuexen		} else {
1443234296Stuexen			ms_gone_by = 0xffffffff;
1444234296Stuexen		}
1445234297Stuexen		if ((ms_gone_by >= net->heart_beat_delay) ||
1446234297Stuexen		    (net->dest_state & SCTP_ADDR_PF)) {
1447234296Stuexen			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
1448234296Stuexen		}
1449163953Srrs	}
1450163953Srrs	return (0);
1451163953Srrs}
1452163953Srrs
1453163953Srrsvoid
1454163953Srrssctp_pathmtu_timer(struct sctp_inpcb *inp,
1455163953Srrs    struct sctp_tcb *stcb,
1456163953Srrs    struct sctp_nets *net)
1457163953Srrs{
1458179157Srrs	uint32_t next_mtu, mtu;
1459163953Srrs
1460228653Stuexen	next_mtu = sctp_get_next_mtu(net->mtu);
1461169352Srrs
1462179157Srrs	if ((next_mtu > net->mtu) && (net->port == 0)) {
1463169352Srrs		if ((net->src_addr_selected == 0) ||
1464169352Srrs		    (net->ro._s_addr == NULL) ||
1465169352Srrs		    (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1466169420Srrs			if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1467169352Srrs				sctp_free_ifa(net->ro._s_addr);
1468169352Srrs				net->ro._s_addr = NULL;
1469169352Srrs				net->src_addr_selected = 0;
1470169420Srrs			} else if (net->ro._s_addr == NULL) {
1471179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1472179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1473179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1474179157Srrs
1475179157Srrs					/* KAME hack: embed scopeid */
1476197288Srrs					(void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
1477179157Srrs				}
1478179157Srrs#endif
1479179157Srrs
1480169420Srrs				net->ro._s_addr = sctp_source_address_selection(inp,
1481169420Srrs				    stcb,
1482169420Srrs				    (sctp_route_t *) & net->ro,
1483169420Srrs				    net, 0, stcb->asoc.vrf_id);
1484179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1485179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1486179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1487179157Srrs
1488179157Srrs					(void)sa6_recoverscope(sin6);
1489179157Srrs				}
1490179157Srrs#endif				/* INET6 */
1491169352Srrs			}
1492169352Srrs			if (net->ro._s_addr)
1493169352Srrs				net->src_addr_selected = 1;
1494169352Srrs		}
1495169352Srrs		if (net->ro._s_addr) {
1496169352Srrs			mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
1497270350Stuexen#if defined(INET) || defined(INET6)
1498185694Srrs			if (net->port) {
1499185694Srrs				mtu -= sizeof(struct udphdr);
1500185694Srrs			}
1501270350Stuexen#endif
1502169352Srrs			if (mtu > next_mtu) {
1503163953Srrs				net->mtu = next_mtu;
1504294148Stuexen			} else {
1505294148Stuexen				net->mtu = mtu;
1506163953Srrs			}
1507163953Srrs		}
1508163953Srrs	}
1509163953Srrs	/* restart the timer */
1510163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1511163953Srrs}
1512163953Srrs
1513163953Srrsvoid
1514163953Srrssctp_autoclose_timer(struct sctp_inpcb *inp,
1515163953Srrs    struct sctp_tcb *stcb,
1516163953Srrs    struct sctp_nets *net)
1517163953Srrs{
1518163953Srrs	struct timeval tn, *tim_touse;
1519163953Srrs	struct sctp_association *asoc;
1520163953Srrs	int ticks_gone_by;
1521163953Srrs
1522169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&tn);
1523163953Srrs	if (stcb->asoc.sctp_autoclose_ticks &&
1524163953Srrs	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1525163953Srrs		/* Auto close is on */
1526163953Srrs		asoc = &stcb->asoc;
1527163953Srrs		/* pick the time to use */
1528163953Srrs		if (asoc->time_last_rcvd.tv_sec >
1529163953Srrs		    asoc->time_last_sent.tv_sec) {
1530163953Srrs			tim_touse = &asoc->time_last_rcvd;
1531163953Srrs		} else {
1532163953Srrs			tim_touse = &asoc->time_last_sent;
1533163953Srrs		}
1534163953Srrs		/* Now has long enough transpired to autoclose? */
1535163953Srrs		ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
1536163953Srrs		if ((ticks_gone_by > 0) &&
1537163953Srrs		    (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1538163953Srrs			/*
1539163953Srrs			 * autoclose time has hit, call the output routine,
1540163953Srrs			 * which should do nothing just to be SURE we don't
1541163953Srrs			 * have hanging data. We can then safely check the
1542163953Srrs			 * queues and know that we are clear to send
1543163953Srrs			 * shutdown
1544163953Srrs			 */
1545172090Srrs			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1546163953Srrs			/* Are we clean? */
1547163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
1548163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue)) {
1549163953Srrs				/*
1550163953Srrs				 * there is nothing queued to send, so I'm
1551163953Srrs				 * done...
1552163953Srrs				 */
1553166675Srrs				if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1554163953Srrs					/* only send SHUTDOWN 1st time thru */
1555224641Stuexen					struct sctp_nets *netp;
1556224641Stuexen
1557246588Stuexen					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1558246588Stuexen					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1559246588Stuexen						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1560246588Stuexen					}
1561246588Stuexen					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1562246588Stuexen					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1563246588Stuexen					sctp_stop_timers_for_shutdown(stcb);
1564224641Stuexen					if (stcb->asoc.alternate) {
1565224641Stuexen						netp = stcb->asoc.alternate;
1566224641Stuexen					} else {
1567224641Stuexen						netp = stcb->asoc.primary_destination;
1568224641Stuexen					}
1569224641Stuexen					sctp_send_shutdown(stcb, netp);
1570163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1571163953Srrs					    stcb->sctp_ep, stcb,
1572224641Stuexen					    netp);
1573163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1574163953Srrs					    stcb->sctp_ep, stcb,
1575224641Stuexen					    netp);
1576163953Srrs				}
1577163953Srrs			}
1578163953Srrs		} else {
1579163953Srrs			/*
1580163953Srrs			 * No auto close at this time, reset t-o to check
1581163953Srrs			 * later
1582163953Srrs			 */
1583163953Srrs			int tmp;
1584163953Srrs
1585163953Srrs			/* fool the timer startup to use the time left */
1586163953Srrs			tmp = asoc->sctp_autoclose_ticks;
1587163953Srrs			asoc->sctp_autoclose_ticks -= ticks_gone_by;
1588163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1589163953Srrs			    net);
1590163953Srrs			/* restore the real tick value */
1591163953Srrs			asoc->sctp_autoclose_ticks = tmp;
1592163953Srrs		}
1593163953Srrs	}
1594163953Srrs}
1595