sctp_timer.c revision 234793
1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3218319Srrs * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4218319Srrs * Copyright (c) 2008-2011, 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,
10231038Stuexen *    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
14231038Stuexen *    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/* $KAME: sctp_timer.c,v 1.29 2005/03/06 16:04:18 itojun Exp $	 */
34163953Srrs
35163953Srrs#include <sys/cdefs.h>
36163953Srrs__FBSDID("$FreeBSD: stable/9/sys/netinet/sctp_timer.c 234793 2012-04-29 16:16:31Z tuexen $");
37163953Srrs
38163953Srrs#define _IP_VHL
39166086Srrs#include <netinet/sctp_os.h>
40163953Srrs#include <netinet/sctp_pcb.h>
41163953Srrs#ifdef INET6
42163953Srrs#endif
43163953Srrs#include <netinet/sctp_var.h>
44167598Srrs#include <netinet/sctp_sysctl.h>
45163953Srrs#include <netinet/sctp_timer.h>
46163953Srrs#include <netinet/sctputil.h>
47163953Srrs#include <netinet/sctp_output.h>
48163953Srrs#include <netinet/sctp_header.h>
49163953Srrs#include <netinet/sctp_indata.h>
50163953Srrs#include <netinet/sctp_asconf.h>
51163953Srrs#include <netinet/sctp_input.h>
52163953Srrs#include <netinet/sctp.h>
53163953Srrs#include <netinet/sctp_uio.h>
54185694Srrs#include <netinet/udp.h>
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
88163953Srrsint
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",
95169420Srrs		    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,
104163953Srrs				    stcb,
105163953Srrs				    SCTP_FAILED_THRESHOLD,
106172090Srrs				    (void *)net, SCTP_SO_NOT_LOCKED);
107163953Srrs			}
108224641Stuexen		} else if ((net->pf_threshold < net->failure_threshold) &&
109224641Stuexen		    (net->error_count > net->pf_threshold)) {
110224641Stuexen			if (!(net->dest_state & SCTP_ADDR_PF)) {
111224641Stuexen				net->dest_state |= SCTP_ADDR_PF;
112224641Stuexen				net->last_active = sctp_get_tick_count();
113224641Stuexen				sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
114224641Stuexen				sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
115224641Stuexen				sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
116224641Stuexen			}
117163953Srrs		}
118163953Srrs	}
119163953Srrs	if (stcb == NULL)
120163953Srrs		return (0);
121163953Srrs
122163953Srrs	if (net) {
123163953Srrs		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
124179783Srrs			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
125171943Srrs				sctp_misc_ints(SCTP_THRESHOLD_INCR,
126171943Srrs				    stcb->asoc.overall_error_count,
127171943Srrs				    (stcb->asoc.overall_error_count + 1),
128171943Srrs				    SCTP_FROM_SCTP_TIMER,
129171943Srrs				    __LINE__);
130171943Srrs			}
131163953Srrs			stcb->asoc.overall_error_count++;
132163953Srrs		}
133163953Srrs	} else {
134179783Srrs		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
135171943Srrs			sctp_misc_ints(SCTP_THRESHOLD_INCR,
136171943Srrs			    stcb->asoc.overall_error_count,
137171943Srrs			    (stcb->asoc.overall_error_count + 1),
138171943Srrs			    SCTP_FROM_SCTP_TIMER,
139171943Srrs			    __LINE__);
140171943Srrs		}
141163953Srrs		stcb->asoc.overall_error_count++;
142163953Srrs	}
143169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n",
144169420Srrs	    &stcb->asoc, stcb->asoc.overall_error_count,
145169420Srrs	    (uint32_t) threshold,
146169420Srrs	    ((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
147163953Srrs	/*
148163953Srrs	 * We specifically do not do >= to give the assoc one more change
149163953Srrs	 * before we fail it.
150163953Srrs	 */
151163953Srrs	if (stcb->asoc.overall_error_count > threshold) {
152163953Srrs		/* Abort notification sends a ULP notify */
153163953Srrs		struct mbuf *oper;
154163953Srrs
155163953Srrs		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
156163953Srrs		    0, M_DONTWAIT, 1, MT_DATA);
157163953Srrs		if (oper) {
158163953Srrs			struct sctp_paramhdr *ph;
159163953Srrs			uint32_t *ippp;
160163953Srrs
161165647Srrs			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
162163953Srrs			    sizeof(uint32_t);
163163953Srrs			ph = mtod(oper, struct sctp_paramhdr *);
164163953Srrs			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
165165647Srrs			ph->param_length = htons(SCTP_BUF_LEN(oper));
166163953Srrs			ippp = (uint32_t *) (ph + 1);
167165220Srrs			*ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_1);
168163953Srrs		}
169165220Srrs		inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_1;
170172090Srrs		sctp_abort_an_association(inp, stcb, SCTP_FAILED_THRESHOLD, oper, SCTP_SO_NOT_LOCKED);
171163953Srrs		return (1);
172163953Srrs	}
173163953Srrs	return (0);
174163953Srrs}
175163953Srrs
176214928Stuexen/*
177214928Stuexen * sctp_find_alternate_net() returns a non-NULL pointer as long
178214928Stuexen * the argument net is non-NULL.
179214928Stuexen */
180163953Srrsstruct sctp_nets *
181163953Srrssctp_find_alternate_net(struct sctp_tcb *stcb,
182163953Srrs    struct sctp_nets *net,
183171440Srrs    int mode)
184163953Srrs{
185163953Srrs	/* Find and return an alternate network if possible */
186171440Srrs	struct sctp_nets *alt, *mnet, *min_errors_net = NULL, *max_cwnd_net = NULL;
187163953Srrs	int once;
188163953Srrs
189171440Srrs	/* JRS 5/14/07 - Initialize min_errors to an impossible value. */
190171440Srrs	int min_errors = -1;
191171440Srrs	uint32_t max_cwnd = 0;
192171440Srrs
193163953Srrs	if (stcb->asoc.numnets == 1) {
194163953Srrs		/* No others but net */
195163953Srrs		return (TAILQ_FIRST(&stcb->asoc.nets));
196163953Srrs	}
197171440Srrs	/*
198171440Srrs	 * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate
199171440Srrs	 * net algorithm. This algorithm chooses the active destination (not
200171440Srrs	 * in PF state) with the largest cwnd value. If all destinations are
201171440Srrs	 * in PF state, unreachable, or unconfirmed, choose the desination
202171440Srrs	 * that is in PF state with the lowest error count. In case of a
203171440Srrs	 * tie, choose the destination that was most recently active.
204171440Srrs	 */
205171440Srrs	if (mode == 2) {
206163953Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
207171440Srrs			/*
208171440Srrs			 * JRS 5/14/07 - If the destination is unreachable
209171440Srrs			 * or unconfirmed, skip it.
210171440Srrs			 */
211163953Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
212171440Srrs			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
213171440Srrs				continue;
214171440Srrs			}
215171440Srrs			/*
216171440Srrs			 * JRS 5/14/07 -  If the destination is reachable
217171440Srrs			 * but in PF state, compare the error count of the
218171440Srrs			 * destination to the minimum error count seen thus
219171440Srrs			 * far. Store the destination with the lower error
220171440Srrs			 * count.  If the error counts are equal, store the
221171440Srrs			 * destination that was most recently active.
222171440Srrs			 */
223171440Srrs			if (mnet->dest_state & SCTP_ADDR_PF) {
224171440Srrs				/*
225171440Srrs				 * JRS 5/14/07 - If the destination under
226171440Srrs				 * consideration is the current destination,
227171440Srrs				 * work as if the error count is one higher.
228171440Srrs				 * The actual error count will not be
229171440Srrs				 * incremented until later in the t3
230171440Srrs				 * handler.
231171440Srrs				 */
232171440Srrs				if (mnet == net) {
233171440Srrs					if (min_errors == -1) {
234171440Srrs						min_errors = mnet->error_count + 1;
235171440Srrs						min_errors_net = mnet;
236171440Srrs					} else if (mnet->error_count + 1 < min_errors) {
237171440Srrs						min_errors = mnet->error_count + 1;
238171440Srrs						min_errors_net = mnet;
239171440Srrs					} else if (mnet->error_count + 1 == min_errors
240171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
241171440Srrs						min_errors_net = mnet;
242171440Srrs						min_errors = mnet->error_count + 1;
243171440Srrs					}
244171440Srrs					continue;
245171440Srrs				} else {
246171440Srrs					if (min_errors == -1) {
247171440Srrs						min_errors = mnet->error_count;
248171440Srrs						min_errors_net = mnet;
249171440Srrs					} else if (mnet->error_count < min_errors) {
250171440Srrs						min_errors = mnet->error_count;
251171440Srrs						min_errors_net = mnet;
252171440Srrs					} else if (mnet->error_count == min_errors
253171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
254171440Srrs						min_errors_net = mnet;
255171440Srrs						min_errors = mnet->error_count;
256171440Srrs					}
257171440Srrs					continue;
258171440Srrs				}
259171440Srrs			}
260171440Srrs			/*
261171440Srrs			 * JRS 5/14/07 - If the destination is reachable and
262171440Srrs			 * not in PF state, compare the cwnd of the
263171440Srrs			 * destination to the highest cwnd seen thus far.
264171440Srrs			 * Store the destination with the higher cwnd value.
265171440Srrs			 * If the cwnd values are equal, randomly choose one
266171440Srrs			 * of the two destinations.
267171440Srrs			 */
268171440Srrs			if (max_cwnd < mnet->cwnd) {
269171440Srrs				max_cwnd_net = mnet;
270171440Srrs				max_cwnd = mnet->cwnd;
271171440Srrs			} else if (max_cwnd == mnet->cwnd) {
272171440Srrs				uint32_t rndval;
273171440Srrs				uint8_t this_random;
274171440Srrs
275171440Srrs				if (stcb->asoc.hb_random_idx > 3) {
276171440Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
277171440Srrs					memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values));
278171440Srrs					this_random = stcb->asoc.hb_random_values[0];
279171440Srrs					stcb->asoc.hb_random_idx++;
280171440Srrs					stcb->asoc.hb_ect_randombit = 0;
281171440Srrs				} else {
282171440Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
283171440Srrs					stcb->asoc.hb_random_idx++;
284171440Srrs					stcb->asoc.hb_ect_randombit = 0;
285171440Srrs				}
286171440Srrs				if (this_random % 2 == 1) {
287171440Srrs					max_cwnd_net = mnet;
288180387Srrs					max_cwnd = mnet->cwnd;	/* Useless? */
289171440Srrs				}
290171440Srrs			}
291171440Srrs		}
292171440Srrs		if (max_cwnd_net == NULL) {
293171440Srrs			if (min_errors_net == NULL) {
294171440Srrs				return (net);
295171440Srrs			}
296171440Srrs			return (min_errors_net);
297171440Srrs		} else {
298171440Srrs			return (max_cwnd_net);
299171440Srrs		}
300171440Srrs	}
301171440Srrs	/*
302171440Srrs	 * JRS 5/14/07 - If mode is set to 1, use the CMT policy for
303171440Srrs	 * choosing an alternate net.
304171440Srrs	 */
305171440Srrs	else if (mode == 1) {
306171440Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
307171440Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
308214928Stuexen			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
309163953Srrs				/*
310163953Srrs				 * will skip ones that are not-reachable or
311163953Srrs				 * unconfirmed
312163953Srrs				 */
313163953Srrs				continue;
314163953Srrs			}
315171440Srrs			if (max_cwnd < mnet->cwnd) {
316171440Srrs				max_cwnd_net = mnet;
317171440Srrs				max_cwnd = mnet->cwnd;
318171440Srrs			} else if (max_cwnd == mnet->cwnd) {
319163953Srrs				uint32_t rndval;
320163953Srrs				uint8_t this_random;
321163953Srrs
322163953Srrs				if (stcb->asoc.hb_random_idx > 3) {
323163953Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
324163953Srrs					memcpy(stcb->asoc.hb_random_values, &rndval,
325163953Srrs					    sizeof(stcb->asoc.hb_random_values));
326163953Srrs					this_random = stcb->asoc.hb_random_values[0];
327163953Srrs					stcb->asoc.hb_random_idx = 0;
328163953Srrs					stcb->asoc.hb_ect_randombit = 0;
329163953Srrs				} else {
330163953Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
331163953Srrs					stcb->asoc.hb_random_idx++;
332163953Srrs					stcb->asoc.hb_ect_randombit = 0;
333163953Srrs				}
334163953Srrs				if (this_random % 2) {
335171440Srrs					max_cwnd_net = mnet;
336171440Srrs					max_cwnd = mnet->cwnd;
337163953Srrs				}
338163953Srrs			}
339163953Srrs		}
340171440Srrs		if (max_cwnd_net) {
341171440Srrs			return (max_cwnd_net);
342163953Srrs		}
343163953Srrs	}
344163953Srrs	mnet = net;
345163953Srrs	once = 0;
346163953Srrs
347163953Srrs	if (mnet == NULL) {
348163953Srrs		mnet = TAILQ_FIRST(&stcb->asoc.nets);
349212225Srrs		if (mnet == NULL) {
350212225Srrs			return (NULL);
351212225Srrs		}
352163953Srrs	}
353163953Srrs	do {
354163953Srrs		alt = TAILQ_NEXT(mnet, sctp_next);
355163953Srrs		if (alt == NULL) {
356163953Srrs			once++;
357163953Srrs			if (once > 1) {
358163953Srrs				break;
359163953Srrs			}
360163953Srrs			alt = TAILQ_FIRST(&stcb->asoc.nets);
361212225Srrs			if (alt == NULL) {
362212225Srrs				return (NULL);
363212225Srrs			}
364163953Srrs		}
365163953Srrs		if (alt->ro.ro_rt == NULL) {
366167598Srrs			if (alt->ro._s_addr) {
367167598Srrs				sctp_free_ifa(alt->ro._s_addr);
368167598Srrs				alt->ro._s_addr = NULL;
369167598Srrs			}
370163953Srrs			alt->src_addr_selected = 0;
371163953Srrs		}
372214928Stuexen		/* sa_ignore NO_NULL_CHK */
373214928Stuexen		if (((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
374163953Srrs		    (alt->ro.ro_rt != NULL) &&
375214928Stuexen		    (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))) {
376163953Srrs			/* Found a reachable address */
377163953Srrs			break;
378163953Srrs		}
379163953Srrs		mnet = alt;
380163953Srrs	} while (alt != NULL);
381163953Srrs
382163953Srrs	if (alt == NULL) {
383163953Srrs		/* Case where NO insv network exists (dormant state) */
384163953Srrs		/* we rotate destinations */
385163953Srrs		once = 0;
386163953Srrs		mnet = net;
387163953Srrs		do {
388212225Srrs			if (mnet == NULL) {
389212225Srrs				return (TAILQ_FIRST(&stcb->asoc.nets));
390212225Srrs			}
391163953Srrs			alt = TAILQ_NEXT(mnet, sctp_next);
392163953Srrs			if (alt == NULL) {
393163953Srrs				once++;
394163953Srrs				if (once > 1) {
395163953Srrs					break;
396163953Srrs				}
397163953Srrs				alt = TAILQ_FIRST(&stcb->asoc.nets);
398163953Srrs			}
399169655Srrs			/* sa_ignore NO_NULL_CHK */
400163953Srrs			if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
401163953Srrs			    (alt != net)) {
402163953Srrs				/* Found an alternate address */
403163953Srrs				break;
404163953Srrs			}
405163953Srrs			mnet = alt;
406163953Srrs		} while (alt != NULL);
407163953Srrs	}
408163953Srrs	if (alt == NULL) {
409163953Srrs		return (net);
410163953Srrs	}
411163953Srrs	return (alt);
412163953Srrs}
413163953Srrs
414163953Srrsstatic void
415163953Srrssctp_backoff_on_timeout(struct sctp_tcb *stcb,
416163953Srrs    struct sctp_nets *net,
417163953Srrs    int win_probe,
418210599Srrs    int num_marked, int num_abandoned)
419163953Srrs{
420170642Srrs	if (net->RTO == 0) {
421170642Srrs		net->RTO = stcb->asoc.minrto;
422170642Srrs	}
423163953Srrs	net->RTO <<= 1;
424163953Srrs	if (net->RTO > stcb->asoc.maxrto) {
425163953Srrs		net->RTO = stcb->asoc.maxrto;
426163953Srrs	}
427210599Srrs	if ((win_probe == 0) && (num_marked || num_abandoned)) {
428163953Srrs		/* We don't apply penalty to window probe scenarios */
429171440Srrs		/* JRS - Use the congestion control given in the CC module */
430171440Srrs		stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
431163953Srrs	}
432163953Srrs}
433163953Srrs
434184333Srrs#ifndef INVARIANTS
435184333Srrsstatic void
436182367Srrssctp_recover_sent_list(struct sctp_tcb *stcb)
437182367Srrs{
438216822Stuexen	struct sctp_tmit_chunk *chk, *nchk;
439182367Srrs	struct sctp_association *asoc;
440182367Srrs
441182367Srrs	asoc = &stcb->asoc;
442216822Stuexen	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
443216825Stuexen		if (SCTP_TSN_GE(asoc->last_acked_seq, chk->rec.data.TSN_seq)) {
444182367Srrs			SCTP_PRINTF("Found chk:%p tsn:%x <= last_acked_seq:%x\n",
445216822Stuexen			    chk, chk->rec.data.TSN_seq, asoc->last_acked_seq);
446182367Srrs			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
447182367Srrs			if (chk->pr_sctp_on) {
448182367Srrs				if (asoc->pr_sctp_cnt != 0)
449182367Srrs					asoc->pr_sctp_cnt--;
450182367Srrs			}
451182367Srrs			if (chk->data) {
452182367Srrs				/* sa_ignore NO_NULL_CHK */
453182367Srrs				sctp_free_bufspace(stcb, asoc, chk, 1);
454182367Srrs				sctp_m_freem(chk->data);
455216822Stuexen				chk->data = NULL;
456196260Stuexen				if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(chk->flags)) {
457182367Srrs					asoc->sent_queue_cnt_removeable--;
458182367Srrs				}
459182367Srrs			}
460182367Srrs			asoc->sent_queue_cnt--;
461221627Stuexen			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
462182367Srrs		}
463182367Srrs	}
464182367Srrs	SCTP_PRINTF("after recover order is as follows\n");
465216822Stuexen	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
466182367Srrs		SCTP_PRINTF("chk:%p TSN:%x\n", chk, chk->rec.data.TSN_seq);
467182367Srrs	}
468182367Srrs}
469182367Srrs
470184333Srrs#endif
471184333Srrs
472163953Srrsstatic int
473163953Srrssctp_mark_all_for_resend(struct sctp_tcb *stcb,
474163953Srrs    struct sctp_nets *net,
475163953Srrs    struct sctp_nets *alt,
476163953Srrs    int window_probe,
477210599Srrs    int *num_marked,
478210599Srrs    int *num_abandoned)
479163953Srrs{
480163953Srrs
481163953Srrs	/*
482163953Srrs	 * Mark all chunks (well not all) that were sent to *net for
483163953Srrs	 * retransmission. Move them to alt for there destination as well...
484163953Srrs	 * We only mark chunks that have been outstanding long enough to
485163953Srrs	 * have received feed-back.
486163953Srrs	 */
487216822Stuexen	struct sctp_tmit_chunk *chk, *nchk;
488163953Srrs	struct sctp_nets *lnets;
489163953Srrs	struct timeval now, min_wait, tv;
490219014Stuexen	int cur_rto;
491210599Srrs	int cnt_abandoned;
492168709Srrs	int audit_tf, num_mk, fir;
493163953Srrs	unsigned int cnt_mk;
494168709Srrs	uint32_t orig_flight, orig_tf;
495163953Srrs	uint32_t tsnlast, tsnfirst;
496182367Srrs	int recovery_cnt = 0;
497163953Srrs
498171440Srrs
499163953Srrs	/* none in flight now */
500163953Srrs	audit_tf = 0;
501163953Srrs	fir = 0;
502163953Srrs	/*
503163953Srrs	 * figure out how long a data chunk must be pending before we can
504163953Srrs	 * mark it ..
505163953Srrs	 */
506169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&now);
507163953Srrs	/* get cur rto in micro-seconds */
508219014Stuexen	cur_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
509219014Stuexen	cur_rto *= 1000;
510224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
511219014Stuexen		sctp_log_fr(cur_rto,
512170744Srrs		    stcb->asoc.peers_rwnd,
513170744Srrs		    window_probe,
514170744Srrs		    SCTP_FR_T3_MARK_TIME);
515224641Stuexen		sctp_log_fr(net->flight_size, 0, 0, SCTP_FR_CWND_REPORT);
516170744Srrs		sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
517170744Srrs	}
518219014Stuexen	tv.tv_sec = cur_rto / 1000000;
519219014Stuexen	tv.tv_usec = cur_rto % 1000000;
520163953Srrs	min_wait = now;
521163953Srrs	timevalsub(&min_wait, &tv);
522163953Srrs	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
523163953Srrs		/*
524163953Srrs		 * if we hit here, we don't have enough seconds on the clock
525163953Srrs		 * to account for the RTO. We just let the lower seconds be
526163953Srrs		 * the bounds and don't worry about it. This may mean we
527163953Srrs		 * will mark a lot more than we should.
528163953Srrs		 */
529163953Srrs		min_wait.tv_sec = min_wait.tv_usec = 0;
530163953Srrs	}
531224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
532219014Stuexen		sctp_log_fr(cur_rto, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
533170744Srrs		sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
534170744Srrs	}
535163953Srrs	/*
536163953Srrs	 * Our rwnd will be incorrect here since we are not adding back the
537163953Srrs	 * cnt * mbuf but we will fix that down below.
538163953Srrs	 */
539163953Srrs	orig_flight = net->flight_size;
540168709Srrs	orig_tf = stcb->asoc.total_flight;
541168709Srrs
542163953Srrs	net->fast_retran_ip = 0;
543163953Srrs	/* Now on to each chunk */
544210599Srrs	cnt_abandoned = 0;
545163953Srrs	num_mk = cnt_mk = 0;
546163953Srrs	tsnfirst = tsnlast = 0;
547184333Srrs#ifndef INVARIANTS
548182367Srrsstart_again:
549184333Srrs#endif
550216822Stuexen	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.sent_queue, sctp_next, nchk) {
551216825Stuexen		if (SCTP_TSN_GE(stcb->asoc.last_acked_seq, chk->rec.data.TSN_seq)) {
552163953Srrs			/* Strange case our list got out of order? */
553182367Srrs			SCTP_PRINTF("Our list is out of order? last_acked:%x chk:%x",
554182367Srrs			    (unsigned int)stcb->asoc.last_acked_seq, (unsigned int)chk->rec.data.TSN_seq);
555182367Srrs			recovery_cnt++;
556182367Srrs#ifdef INVARIANTS
557182367Srrs			panic("last acked >= chk on sent-Q");
558182367Srrs#else
559182367Srrs			SCTP_PRINTF("Recover attempts a restart cnt:%d\n", recovery_cnt);
560182367Srrs			sctp_recover_sent_list(stcb);
561182367Srrs			if (recovery_cnt < 10) {
562182367Srrs				goto start_again;
563182367Srrs			} else {
564182367Srrs				SCTP_PRINTF("Recovery fails %d times??\n", recovery_cnt);
565182367Srrs			}
566182367Srrs#endif
567163953Srrs		}
568163953Srrs		if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
569163953Srrs			/*
570163953Srrs			 * found one to mark: If it is less than
571163953Srrs			 * DATAGRAM_ACKED it MUST not be a skipped or marked
572163953Srrs			 * TSN but instead one that is either already set
573163953Srrs			 * for retransmission OR one that needs
574163953Srrs			 * retransmission.
575163953Srrs			 */
576163953Srrs
577163953Srrs			/* validate its been outstanding long enough */
578224641Stuexen			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
579170744Srrs				sctp_log_fr(chk->rec.data.TSN_seq,
580170744Srrs				    chk->sent_rcv_time.tv_sec,
581170744Srrs				    chk->sent_rcv_time.tv_usec,
582170744Srrs				    SCTP_FR_T3_MARK_TIME);
583170744Srrs			}
584163953Srrs			if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
585163953Srrs				/*
586163953Srrs				 * we have reached a chunk that was sent
587163953Srrs				 * some seconds past our min.. forget it we
588163953Srrs				 * will find no more to send.
589163953Srrs				 */
590224641Stuexen				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
591170744Srrs					sctp_log_fr(0,
592170744Srrs					    chk->sent_rcv_time.tv_sec,
593170744Srrs					    chk->sent_rcv_time.tv_usec,
594170744Srrs					    SCTP_FR_T3_STOPPED);
595170744Srrs				}
596163953Srrs				continue;
597163953Srrs			} else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
598163953Srrs			    (window_probe == 0)) {
599163953Srrs				/*
600163953Srrs				 * we must look at the micro seconds to
601163953Srrs				 * know.
602163953Srrs				 */
603163953Srrs				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
604163953Srrs					/*
605163953Srrs					 * ok it was sent after our boundary
606163953Srrs					 * time.
607163953Srrs					 */
608163953Srrs					continue;
609163953Srrs				}
610163953Srrs			}
611196260Stuexen			if (stcb->asoc.peer_supports_prsctp && PR_SCTP_TTL_ENABLED(chk->flags)) {
612163953Srrs				/* Is it expired? */
613212801Stuexen				if (timevalcmp(&now, &chk->rec.data.timetodrop, >)) {
614163953Srrs					/* Yes so drop it */
615163953Srrs					if (chk->data) {
616169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
617163953Srrs						    chk,
618163953Srrs						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
619189790Srrs						    SCTP_SO_NOT_LOCKED);
620210599Srrs						cnt_abandoned++;
621163953Srrs					}
622185694Srrs					continue;
623163953Srrs				}
624163953Srrs			}
625196260Stuexen			if (stcb->asoc.peer_supports_prsctp && PR_SCTP_RTX_ENABLED(chk->flags)) {
626163953Srrs				/* Has it been retransmitted tv_sec times? */
627163953Srrs				if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
628163953Srrs					if (chk->data) {
629169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
630163953Srrs						    chk,
631163953Srrs						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
632189790Srrs						    SCTP_SO_NOT_LOCKED);
633210599Srrs						cnt_abandoned++;
634163953Srrs					}
635185694Srrs					continue;
636163953Srrs				}
637163953Srrs			}
638168709Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
639163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
640163953Srrs				num_mk++;
641163953Srrs				if (fir == 0) {
642163953Srrs					fir = 1;
643163953Srrs					tsnfirst = chk->rec.data.TSN_seq;
644163953Srrs				}
645163953Srrs				tsnlast = chk->rec.data.TSN_seq;
646224641Stuexen				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
647170744Srrs					sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
648170744Srrs					    0, SCTP_FR_T3_MARKED);
649170744Srrs				}
650168709Srrs				if (chk->rec.data.chunk_was_revoked) {
651168709Srrs					/* deflate the cwnd */
652168709Srrs					chk->whoTo->cwnd -= chk->book_size;
653168709Srrs					chk->rec.data.chunk_was_revoked = 0;
654168709Srrs				}
655168709Srrs				net->marked_retrans++;
656168709Srrs				stcb->asoc.marked_retrans++;
657179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
658170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
659170744Srrs					    chk->whoTo->flight_size,
660170744Srrs					    chk->book_size,
661170744Srrs					    (uintptr_t) chk->whoTo,
662170744Srrs					    chk->rec.data.TSN_seq);
663170744Srrs				}
664168709Srrs				sctp_flight_size_decrease(chk);
665168709Srrs				sctp_total_flight_decrease(stcb, chk);
666168709Srrs				stcb->asoc.peers_rwnd += chk->send_size;
667179783Srrs				stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
668163953Srrs			}
669163953Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
670163953Srrs			SCTP_STAT_INCR(sctps_markedretrans);
671165220Srrs
672163953Srrs			/* reset the TSN for striking and other FR stuff */
673163953Srrs			chk->rec.data.doing_fast_retransmit = 0;
674163953Srrs			/* Clear any time so NO RTT is being done */
675219397Srrs
676219397Srrs			if (chk->do_rtt) {
677219397Srrs				if (chk->whoTo->rto_needed == 0) {
678219397Srrs					chk->whoTo->rto_needed = 1;
679219397Srrs				}
680219397Srrs			}
681163953Srrs			chk->do_rtt = 0;
682163953Srrs			if (alt != net) {
683163953Srrs				sctp_free_remote_addr(chk->whoTo);
684163953Srrs				chk->no_fr_allowed = 1;
685163953Srrs				chk->whoTo = alt;
686163953Srrs				atomic_add_int(&alt->ref_count, 1);
687163953Srrs			} else {
688163953Srrs				chk->no_fr_allowed = 0;
689163953Srrs				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
690163953Srrs					chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
691163953Srrs				} else {
692163953Srrs					chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
693163953Srrs				}
694163953Srrs			}
695170181Srrs			/*
696170181Srrs			 * CMT: Do not allow FRs on retransmitted TSNs.
697170181Srrs			 */
698216669Stuexen			if (stcb->asoc.sctp_cmt_on_off > 0) {
699163953Srrs				chk->no_fr_allowed = 1;
700163953Srrs			}
701210599Srrs#ifdef THIS_SHOULD_NOT_BE_DONE
702163953Srrs		} else if (chk->sent == SCTP_DATAGRAM_ACKED) {
703163953Srrs			/* remember highest acked one */
704163953Srrs			could_be_sent = chk;
705210599Srrs#endif
706163953Srrs		}
707163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
708163953Srrs			cnt_mk++;
709163953Srrs		}
710163953Srrs	}
711168709Srrs	if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
712168709Srrs		/* we did not subtract the same things? */
713168709Srrs		audit_tf = 1;
714168709Srrs	}
715224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
716170744Srrs		sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
717170744Srrs	}
718163953Srrs#ifdef SCTP_DEBUG
719169420Srrs	if (num_mk) {
720169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
721169420Srrs		    tsnlast);
722169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%ld\n",
723169420Srrs		    num_mk, (u_long)stcb->asoc.peers_rwnd);
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:%d\n",
727169420Srrs		    num_mk,
728169420Srrs		    (int)stcb->asoc.peers_rwnd);
729163953Srrs	}
730163953Srrs#endif
731163953Srrs	*num_marked = num_mk;
732210599Srrs	*num_abandoned = cnt_abandoned;
733210493Srrs	/*
734210493Srrs	 * Now check for a ECN Echo that may be stranded And include the
735210493Srrs	 * cnt_mk'd to have all resends in the control queue.
736210493Srrs	 */
737210493Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
738210493Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
739210493Srrs			cnt_mk++;
740210493Srrs		}
741210493Srrs		if ((chk->whoTo == net) &&
742210493Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
743210493Srrs			sctp_free_remote_addr(chk->whoTo);
744210493Srrs			chk->whoTo = alt;
745210493Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
746210493Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
747210493Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
748210493Srrs				cnt_mk++;
749210493Srrs			}
750210493Srrs			atomic_add_int(&alt->ref_count, 1);
751210493Srrs		}
752210493Srrs	}
753210599Srrs#ifdef THIS_SHOULD_NOT_BE_DONE
754163953Srrs	if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
755163953Srrs		/* fix it so we retransmit the highest acked anyway */
756163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
757163953Srrs		cnt_mk++;
758163953Srrs		could_be_sent->sent = SCTP_DATAGRAM_RESEND;
759163953Srrs	}
760210599Srrs#endif
761163953Srrs	if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
762165220Srrs#ifdef INVARIANTS
763171477Srrs		SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
764171477Srrs		    cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
765163953Srrs#endif
766163953Srrs#ifndef SCTP_AUDITING_ENABLED
767163953Srrs		stcb->asoc.sent_queue_retran_cnt = cnt_mk;
768163953Srrs#endif
769163953Srrs	}
770163953Srrs	if (audit_tf) {
771169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER4,
772169420Srrs		    "Audit total flight due to negative value net:%p\n",
773169420Srrs		    net);
774163953Srrs		stcb->asoc.total_flight = 0;
775163953Srrs		stcb->asoc.total_flight_count = 0;
776163953Srrs		/* Clear all networks flight size */
777163953Srrs		TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
778163953Srrs			lnets->flight_size = 0;
779169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER4,
780169420Srrs			    "Net:%p c-f cwnd:%d ssthresh:%d\n",
781169420Srrs			    lnets, lnets->cwnd, lnets->ssthresh);
782163953Srrs		}
783163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
784163953Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
785179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
786170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
787170744Srrs					    chk->whoTo->flight_size,
788170744Srrs					    chk->book_size,
789170744Srrs					    (uintptr_t) chk->whoTo,
790170744Srrs					    chk->rec.data.TSN_seq);
791170744Srrs				}
792168709Srrs				sctp_flight_size_increase(chk);
793168709Srrs				sctp_total_flight_increase(stcb, chk);
794163953Srrs			}
795163953Srrs		}
796163953Srrs	}
797163953Srrs	/* We return 1 if we only have a window probe outstanding */
798163953Srrs	return (0);
799163953Srrs}
800163953Srrs
801163953Srrs
802163953Srrsint
803163953Srrssctp_t3rxt_timer(struct sctp_inpcb *inp,
804163953Srrs    struct sctp_tcb *stcb,
805163953Srrs    struct sctp_nets *net)
806163953Srrs{
807163953Srrs	struct sctp_nets *alt;
808210599Srrs	int win_probe, num_mk, num_abandoned;
809163953Srrs
810179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
811170744Srrs		sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
812170744Srrs	}
813179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
814163953Srrs		struct sctp_nets *lnet;
815163953Srrs
816163953Srrs		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
817163953Srrs			if (net == lnet) {
818163953Srrs				sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
819163953Srrs			} else {
820163953Srrs				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
821163953Srrs			}
822163953Srrs		}
823163953Srrs	}
824163953Srrs	/* Find an alternate and mark those for retransmission */
825163953Srrs	if ((stcb->asoc.peers_rwnd == 0) &&
826163953Srrs	    (stcb->asoc.total_flight < net->mtu)) {
827163953Srrs		SCTP_STAT_INCR(sctps_timowindowprobe);
828163953Srrs		win_probe = 1;
829163953Srrs	} else {
830163953Srrs		win_probe = 0;
831163953Srrs	}
832168709Srrs
833163953Srrs	if (win_probe == 0) {
834163953Srrs		/* We don't do normal threshold management on window probes */
835163953Srrs		if (sctp_threshold_management(inp, stcb, net,
836163953Srrs		    stcb->asoc.max_send_times)) {
837163953Srrs			/* Association was destroyed */
838163953Srrs			return (1);
839163953Srrs		} else {
840163953Srrs			if (net != stcb->asoc.primary_destination) {
841163953Srrs				/* send a immediate HB if our RTO is stale */
842163953Srrs				struct timeval now;
843163953Srrs				unsigned int ms_goneby;
844163953Srrs
845169378Srrs				(void)SCTP_GETTIME_TIMEVAL(&now);
846163953Srrs				if (net->last_sent_time.tv_sec) {
847163953Srrs					ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
848163953Srrs				} else {
849163953Srrs					ms_goneby = 0;
850163953Srrs				}
851224641Stuexen				if ((net->dest_state & SCTP_ADDR_PF) == 0) {
852224641Stuexen					if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
853185694Srrs						/*
854224641Stuexen						 * no recent feed back in an
855224641Stuexen						 * RTO or more, request a
856224641Stuexen						 * RTT update
857185694Srrs						 */
858224641Stuexen						sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
859224641Stuexen					}
860163953Srrs				}
861163953Srrs			}
862163953Srrs		}
863163953Srrs	} else {
864163953Srrs		/*
865163953Srrs		 * For a window probe we don't penalize the net's but only
866163953Srrs		 * the association. This may fail it if SACKs are not coming
867163953Srrs		 * back. If sack's are coming with rwnd locked at 0, we will
868163953Srrs		 * continue to hold things waiting for rwnd to raise
869163953Srrs		 */
870163953Srrs		if (sctp_threshold_management(inp, stcb, NULL,
871163953Srrs		    stcb->asoc.max_send_times)) {
872163953Srrs			/* Association was destroyed */
873163953Srrs			return (1);
874163953Srrs		}
875163953Srrs	}
876224641Stuexen	if (stcb->asoc.sctp_cmt_on_off > 0) {
877224641Stuexen		if (net->pf_threshold < net->failure_threshold) {
878224641Stuexen			alt = sctp_find_alternate_net(stcb, net, 2);
879224641Stuexen		} else {
880224641Stuexen			/*
881224641Stuexen			 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is
882224641Stuexen			 * being used, then pick dest with largest ssthresh
883224641Stuexen			 * for any retransmission.
884224641Stuexen			 */
885224641Stuexen			alt = sctp_find_alternate_net(stcb, net, 1);
886224641Stuexen			/*
887224641Stuexen			 * CUCv2: If a different dest is picked for the
888224641Stuexen			 * retransmission, then new (rtx-)pseudo_cumack
889224641Stuexen			 * needs to be tracked for orig dest. Let CUCv2
890224641Stuexen			 * track new (rtx-) pseudo-cumack always.
891224641Stuexen			 */
892224641Stuexen			net->find_pseudo_cumack = 1;
893224641Stuexen			net->find_rtx_pseudo_cumack = 1;
894224641Stuexen		}
895224641Stuexen	} else {
896224641Stuexen		alt = sctp_find_alternate_net(stcb, net, 0);
897224641Stuexen	}
898224641Stuexen
899224641Stuexen	num_mk = 0;
900224641Stuexen	num_abandoned = 0;
901224641Stuexen	(void)sctp_mark_all_for_resend(stcb, net, alt, win_probe,
902224641Stuexen	    &num_mk, &num_abandoned);
903224641Stuexen	/* FR Loss recovery just ended with the T3. */
904224641Stuexen	stcb->asoc.fast_retran_loss_recovery = 0;
905224641Stuexen
906224641Stuexen	/* CMT FR loss recovery ended with the T3 */
907224641Stuexen	net->fast_retran_loss_recovery = 0;
908224641Stuexen	if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
909224641Stuexen	    (net->flight_size == 0)) {
910224641Stuexen		(*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
911224641Stuexen	}
912224641Stuexen	/*
913224641Stuexen	 * setup the sat loss recovery that prevents satellite cwnd advance.
914224641Stuexen	 */
915224641Stuexen	stcb->asoc.sat_t3_loss_recovery = 1;
916224641Stuexen	stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
917224641Stuexen
918224641Stuexen	/* Backoff the timer and cwnd */
919224641Stuexen	sctp_backoff_on_timeout(stcb, net, win_probe, num_mk, num_abandoned);
920224641Stuexen	if ((!(net->dest_state & SCTP_ADDR_REACHABLE)) ||
921224641Stuexen	    (net->dest_state & SCTP_ADDR_PF)) {
922163953Srrs		/* Move all pending over too */
923212712Stuexen		sctp_move_chunks_from_net(stcb, net);
924169352Srrs
925169352Srrs		/*
926169352Srrs		 * Get the address that failed, to force a new src address
927169352Srrs		 * selecton and a route allocation.
928169352Srrs		 */
929169352Srrs		if (net->ro._s_addr) {
930169352Srrs			sctp_free_ifa(net->ro._s_addr);
931169352Srrs			net->ro._s_addr = NULL;
932169352Srrs		}
933169352Srrs		net->src_addr_selected = 0;
934169352Srrs
935169352Srrs		/* Force a route allocation too */
936169352Srrs		if (net->ro.ro_rt) {
937169352Srrs			RTFREE(net->ro.ro_rt);
938169352Srrs			net->ro.ro_rt = NULL;
939169352Srrs		}
940163953Srrs		/* Was it our primary? */
941163953Srrs		if ((stcb->asoc.primary_destination == net) && (alt != net)) {
942163953Srrs			/*
943163953Srrs			 * Yes, note it as such and find an alternate note:
944163953Srrs			 * this means HB code must use this to resent the
945163953Srrs			 * primary if it goes active AND if someone does a
946163953Srrs			 * change-primary then this flag must be cleared
947163953Srrs			 * from any net structures.
948163953Srrs			 */
949224641Stuexen			if (stcb->asoc.alternate) {
950224641Stuexen				sctp_free_remote_addr(stcb->asoc.alternate);
951163953Srrs			}
952224641Stuexen			stcb->asoc.alternate = alt;
953224641Stuexen			atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
954163953Srrs		}
955163953Srrs	}
956163953Srrs	/*
957163953Srrs	 * Special case for cookie-echo'ed case, we don't do output but must
958163953Srrs	 * await the COOKIE-ACK before retransmission
959163953Srrs	 */
960163953Srrs	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
961163953Srrs		/*
962163953Srrs		 * Here we just reset the timer and start again since we
963163953Srrs		 * have not established the asoc
964163953Srrs		 */
965163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
966163953Srrs		return (0);
967163953Srrs	}
968163953Srrs	if (stcb->asoc.peer_supports_prsctp) {
969163953Srrs		struct sctp_tmit_chunk *lchk;
970163953Srrs
971163953Srrs		lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
972163953Srrs		/* C3. See if we need to send a Fwd-TSN */
973216825Stuexen		if (SCTP_TSN_GT(stcb->asoc.advanced_peer_ack_point, stcb->asoc.last_acked_seq)) {
974163953Srrs			send_forward_tsn(stcb, &stcb->asoc);
975163953Srrs			if (lchk) {
976163953Srrs				/* Assure a timer is up */
977163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
978163953Srrs			}
979163953Srrs		}
980163953Srrs	}
981179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
982170744Srrs		sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
983170744Srrs	}
984163953Srrs	return (0);
985163953Srrs}
986163953Srrs
987163953Srrsint
988163953Srrssctp_t1init_timer(struct sctp_inpcb *inp,
989163953Srrs    struct sctp_tcb *stcb,
990163953Srrs    struct sctp_nets *net)
991163953Srrs{
992163953Srrs	/* bump the thresholds */
993163953Srrs	if (stcb->asoc.delayed_connection) {
994163953Srrs		/*
995163953Srrs		 * special hook for delayed connection. The library did NOT
996163953Srrs		 * complete the rest of its sends.
997163953Srrs		 */
998163953Srrs		stcb->asoc.delayed_connection = 0;
999172090Srrs		sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1000163953Srrs		return (0);
1001163953Srrs	}
1002163953Srrs	if (SCTP_GET_STATE((&stcb->asoc)) != SCTP_STATE_COOKIE_WAIT) {
1003163953Srrs		return (0);
1004163953Srrs	}
1005163953Srrs	if (sctp_threshold_management(inp, stcb, net,
1006163953Srrs	    stcb->asoc.max_init_times)) {
1007163953Srrs		/* Association was destroyed */
1008163953Srrs		return (1);
1009163953Srrs	}
1010163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1011210599Srrs	sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0, 0);
1012163953Srrs	if (stcb->asoc.initial_init_rto_max < net->RTO) {
1013163953Srrs		net->RTO = stcb->asoc.initial_init_rto_max;
1014163953Srrs	}
1015163953Srrs	if (stcb->asoc.numnets > 1) {
1016163953Srrs		/* If we have more than one addr use it */
1017163953Srrs		struct sctp_nets *alt;
1018163953Srrs
1019163953Srrs		alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1020214928Stuexen		if (alt != stcb->asoc.primary_destination) {
1021212712Stuexen			sctp_move_chunks_from_net(stcb, stcb->asoc.primary_destination);
1022163953Srrs			stcb->asoc.primary_destination = alt;
1023163953Srrs		}
1024163953Srrs	}
1025163953Srrs	/* Send out a new init */
1026172090Srrs	sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1027163953Srrs	return (0);
1028163953Srrs}
1029163953Srrs
1030163953Srrs/*
1031163953Srrs * For cookie and asconf we actually need to find and mark for resend, then
1032163953Srrs * increment the resend counter (after all the threshold management stuff of
1033163953Srrs * course).
1034163953Srrs */
1035163953Srrsint
1036163953Srrssctp_cookie_timer(struct sctp_inpcb *inp,
1037163953Srrs    struct sctp_tcb *stcb,
1038231038Stuexen    struct sctp_nets *net SCTP_UNUSED)
1039163953Srrs{
1040163953Srrs	struct sctp_nets *alt;
1041163953Srrs	struct sctp_tmit_chunk *cookie;
1042163953Srrs
1043163953Srrs	/* first before all else we must find the cookie */
1044163953Srrs	TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1045163953Srrs		if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1046163953Srrs			break;
1047163953Srrs		}
1048163953Srrs	}
1049163953Srrs	if (cookie == NULL) {
1050163953Srrs		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1051163953Srrs			/* FOOBAR! */
1052163953Srrs			struct mbuf *oper;
1053163953Srrs
1054163953Srrs			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1055163953Srrs			    0, M_DONTWAIT, 1, MT_DATA);
1056163953Srrs			if (oper) {
1057163953Srrs				struct sctp_paramhdr *ph;
1058163953Srrs				uint32_t *ippp;
1059163953Srrs
1060165647Srrs				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1061163953Srrs				    sizeof(uint32_t);
1062163953Srrs				ph = mtod(oper, struct sctp_paramhdr *);
1063163953Srrs				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1064165647Srrs				ph->param_length = htons(SCTP_BUF_LEN(oper));
1065163953Srrs				ippp = (uint32_t *) (ph + 1);
1066171440Srrs				*ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
1067163953Srrs			}
1068171440Srrs			inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_4;
1069163953Srrs			sctp_abort_an_association(inp, stcb, SCTP_INTERNAL_ERROR,
1070172090Srrs			    oper, SCTP_SO_NOT_LOCKED);
1071163953Srrs		} else {
1072165220Srrs#ifdef INVARIANTS
1073163953Srrs			panic("Cookie timer expires in wrong state?");
1074163953Srrs#else
1075169420Srrs			SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(&stcb->asoc));
1076163953Srrs			return (0);
1077163953Srrs#endif
1078163953Srrs		}
1079163953Srrs		return (0);
1080163953Srrs	}
1081163953Srrs	/* Ok we found the cookie, threshold management next */
1082163953Srrs	if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1083163953Srrs	    stcb->asoc.max_init_times)) {
1084163953Srrs		/* Assoc is over */
1085163953Srrs		return (1);
1086163953Srrs	}
1087163953Srrs	/*
1088163953Srrs	 * cleared theshold management now lets backoff the address & select
1089163953Srrs	 * an alternate
1090163953Srrs	 */
1091163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1092210599Srrs	sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0, 0);
1093163953Srrs	alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1094163953Srrs	if (alt != cookie->whoTo) {
1095163953Srrs		sctp_free_remote_addr(cookie->whoTo);
1096163953Srrs		cookie->whoTo = alt;
1097163953Srrs		atomic_add_int(&alt->ref_count, 1);
1098163953Srrs	}
1099163953Srrs	/* Now mark the retran info */
1100163953Srrs	if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1101163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1102163953Srrs	}
1103163953Srrs	cookie->sent = SCTP_DATAGRAM_RESEND;
1104163953Srrs	/*
1105163953Srrs	 * Now call the output routine to kick out the cookie again, Note we
1106163953Srrs	 * don't mark any chunks for retran so that FR will need to kick in
1107163953Srrs	 * to move these (or a send timer).
1108163953Srrs	 */
1109163953Srrs	return (0);
1110163953Srrs}
1111163953Srrs
1112163953Srrsint
1113163953Srrssctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1114163953Srrs    struct sctp_nets *net)
1115163953Srrs{
1116163953Srrs	struct sctp_nets *alt;
1117163953Srrs	struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1118163953Srrs
1119163953Srrs	if (stcb->asoc.stream_reset_outstanding == 0) {
1120163953Srrs		return (0);
1121163953Srrs	}
1122163953Srrs	/* find the existing STRRESET, we use the seq number we sent out on */
1123169420Srrs	(void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1124163953Srrs	if (strrst == NULL) {
1125163953Srrs		return (0);
1126163953Srrs	}
1127163953Srrs	/* do threshold management */
1128163953Srrs	if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1129163953Srrs	    stcb->asoc.max_send_times)) {
1130163953Srrs		/* Assoc is over */
1131163953Srrs		return (1);
1132163953Srrs	}
1133163953Srrs	/*
1134163953Srrs	 * cleared theshold management now lets backoff the address & select
1135163953Srrs	 * an alternate
1136163953Srrs	 */
1137210599Srrs	sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0, 0);
1138163953Srrs	alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
1139163953Srrs	sctp_free_remote_addr(strrst->whoTo);
1140163953Srrs	strrst->whoTo = alt;
1141163953Srrs	atomic_add_int(&alt->ref_count, 1);
1142163953Srrs
1143163953Srrs	/* See if a ECN Echo is also stranded */
1144163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1145163953Srrs		if ((chk->whoTo == net) &&
1146163953Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1147163953Srrs			sctp_free_remote_addr(chk->whoTo);
1148163953Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
1149163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
1150163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1151163953Srrs			}
1152163953Srrs			chk->whoTo = alt;
1153163953Srrs			atomic_add_int(&alt->ref_count, 1);
1154163953Srrs		}
1155163953Srrs	}
1156224641Stuexen	if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
1157163953Srrs		/*
1158163953Srrs		 * If the address went un-reachable, we need to move to
1159163953Srrs		 * alternates for ALL chk's in queue
1160163953Srrs		 */
1161212712Stuexen		sctp_move_chunks_from_net(stcb, net);
1162163953Srrs	}
1163163953Srrs	/* mark the retran info */
1164163953Srrs	if (strrst->sent != SCTP_DATAGRAM_RESEND)
1165163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1166163953Srrs	strrst->sent = SCTP_DATAGRAM_RESEND;
1167163953Srrs
1168163953Srrs	/* restart the timer */
1169163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1170163953Srrs	return (0);
1171163953Srrs}
1172163953Srrs
1173163953Srrsint
1174163953Srrssctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1175163953Srrs    struct sctp_nets *net)
1176163953Srrs{
1177163953Srrs	struct sctp_nets *alt;
1178216822Stuexen	struct sctp_tmit_chunk *asconf, *chk;
1179163953Srrs
1180171572Srrs	/* is this a first send, or a retransmission? */
1181179157Srrs	if (TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) {
1182163953Srrs		/* compose a new ASCONF chunk and send it */
1183172190Srrs		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1184163953Srrs	} else {
1185171572Srrs		/*
1186171572Srrs		 * Retransmission of the existing ASCONF is needed
1187171572Srrs		 */
1188163953Srrs
1189163953Srrs		/* find the existing ASCONF */
1190179157Srrs		asconf = TAILQ_FIRST(&stcb->asoc.asconf_send_queue);
1191163953Srrs		if (asconf == NULL) {
1192163953Srrs			return (0);
1193163953Srrs		}
1194163953Srrs		/* do threshold management */
1195163953Srrs		if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1196163953Srrs		    stcb->asoc.max_send_times)) {
1197163953Srrs			/* Assoc is over */
1198163953Srrs			return (1);
1199163953Srrs		}
1200163953Srrs		if (asconf->snd_count > stcb->asoc.max_send_times) {
1201163953Srrs			/*
1202171572Srrs			 * Something is rotten: our peer is not responding
1203171572Srrs			 * to ASCONFs but apparently is to other chunks.
1204171572Srrs			 * i.e. it is not properly handling the chunk type
1205171572Srrs			 * upper bits. Mark this peer as ASCONF incapable
1206171572Srrs			 * and cleanup.
1207163953Srrs			 */
1208169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1209163953Srrs			sctp_asconf_cleanup(stcb, net);
1210163953Srrs			return (0);
1211163953Srrs		}
1212163953Srrs		/*
1213171572Srrs		 * cleared threshold management, so now backoff the net and
1214171572Srrs		 * select an alternate
1215163953Srrs		 */
1216210599Srrs		sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0, 0);
1217163953Srrs		alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
1218179157Srrs		if (asconf->whoTo != alt) {
1219179157Srrs			sctp_free_remote_addr(asconf->whoTo);
1220179157Srrs			asconf->whoTo = alt;
1221179157Srrs			atomic_add_int(&alt->ref_count, 1);
1222179157Srrs		}
1223171572Srrs		/* See if an ECN Echo is also stranded */
1224163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1225163953Srrs			if ((chk->whoTo == net) &&
1226163953Srrs			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1227163953Srrs				sctp_free_remote_addr(chk->whoTo);
1228163953Srrs				chk->whoTo = alt;
1229163953Srrs				if (chk->sent != SCTP_DATAGRAM_RESEND) {
1230163953Srrs					chk->sent = SCTP_DATAGRAM_RESEND;
1231163953Srrs					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1232163953Srrs				}
1233163953Srrs				atomic_add_int(&alt->ref_count, 1);
1234163953Srrs			}
1235163953Srrs		}
1236216822Stuexen		TAILQ_FOREACH(chk, &stcb->asoc.asconf_send_queue, sctp_next) {
1237179157Srrs			if (chk->whoTo != alt) {
1238179157Srrs				sctp_free_remote_addr(chk->whoTo);
1239179157Srrs				chk->whoTo = alt;
1240179157Srrs				atomic_add_int(&alt->ref_count, 1);
1241179157Srrs			}
1242179157Srrs			if (asconf->sent != SCTP_DATAGRAM_RESEND && chk->sent != SCTP_DATAGRAM_UNSENT)
1243179157Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1244179157Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
1245179157Srrs		}
1246224641Stuexen		if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
1247163953Srrs			/*
1248163953Srrs			 * If the address went un-reachable, we need to move
1249171572Srrs			 * to the alternate for ALL chunks in queue
1250163953Srrs			 */
1251212712Stuexen			sctp_move_chunks_from_net(stcb, net);
1252163953Srrs		}
1253163953Srrs		/* mark the retran info */
1254163953Srrs		if (asconf->sent != SCTP_DATAGRAM_RESEND)
1255163953Srrs			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1256163953Srrs		asconf->sent = SCTP_DATAGRAM_RESEND;
1257179157Srrs
1258179157Srrs		/* send another ASCONF if any and we can do */
1259179157Srrs		sctp_send_asconf(stcb, alt, SCTP_ADDR_NOT_LOCKED);
1260163953Srrs	}
1261163953Srrs	return (0);
1262163953Srrs}
1263163953Srrs
1264172091Srrs/* Mobility adaptation */
1265172156Srrsvoid
1266172091Srrssctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1267231038Stuexen    struct sctp_nets *net SCTP_UNUSED)
1268172091Srrs{
1269172091Srrs	if (stcb->asoc.deleted_primary == NULL) {
1270172091Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n");
1271172091Srrs		sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1272172156Srrs		return;
1273172091Srrs	}
1274172091Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary ");
1275172091Srrs	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1276172091Srrs	sctp_free_remote_addr(stcb->asoc.deleted_primary);
1277172091Srrs	stcb->asoc.deleted_primary = NULL;
1278172091Srrs	sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1279172156Srrs	return;
1280172091Srrs}
1281172091Srrs
1282163953Srrs/*
1283163953Srrs * For the shutdown and shutdown-ack, we do not keep one around on the
1284163953Srrs * control queue. This means we must generate a new one and call the general
1285163953Srrs * chunk output routine, AFTER having done threshold management.
1286214928Stuexen * It is assumed that net is non-NULL.
1287163953Srrs */
1288163953Srrsint
1289163953Srrssctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1290163953Srrs    struct sctp_nets *net)
1291163953Srrs{
1292163953Srrs	struct sctp_nets *alt;
1293163953Srrs
1294163953Srrs	/* first threshold managment */
1295163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1296163953Srrs		/* Assoc is over */
1297163953Srrs		return (1);
1298163953Srrs	}
1299214928Stuexen	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1300163953Srrs	/* second select an alternative */
1301163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1302163953Srrs
1303163953Srrs	/* third generate a shutdown into the queue for out net */
1304214928Stuexen	sctp_send_shutdown(stcb, alt);
1305214928Stuexen
1306163953Srrs	/* fourth restart timer */
1307163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1308163953Srrs	return (0);
1309163953Srrs}
1310163953Srrs
1311163953Srrsint
1312163953Srrssctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1313163953Srrs    struct sctp_nets *net)
1314163953Srrs{
1315163953Srrs	struct sctp_nets *alt;
1316163953Srrs
1317163953Srrs	/* first threshold managment */
1318163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1319163953Srrs		/* Assoc is over */
1320163953Srrs		return (1);
1321163953Srrs	}
1322214928Stuexen	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1323163953Srrs	/* second select an alternative */
1324163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1325163953Srrs
1326163953Srrs	/* third generate a shutdown into the queue for out net */
1327163953Srrs	sctp_send_shutdown_ack(stcb, alt);
1328163953Srrs
1329163953Srrs	/* fourth restart timer */
1330163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1331163953Srrs	return (0);
1332163953Srrs}
1333163953Srrs
1334163953Srrsstatic void
1335163953Srrssctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1336163953Srrs    struct sctp_tcb *stcb)
1337163953Srrs{
1338163953Srrs	struct sctp_stream_queue_pending *sp;
1339217760Stuexen	unsigned int i, chks_in_queue = 0;
1340163953Srrs	int being_filled = 0;
1341163953Srrs
1342163953Srrs	/*
1343163953Srrs	 * This function is ONLY called when the send/sent queues are empty.
1344163953Srrs	 */
1345163953Srrs	if ((stcb == NULL) || (inp == NULL))
1346163953Srrs		return;
1347163953Srrs
1348163953Srrs	if (stcb->asoc.sent_queue_retran_cnt) {
1349169420Srrs		SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1350163953Srrs		    stcb->asoc.sent_queue_retran_cnt);
1351163953Srrs		stcb->asoc.sent_queue_retran_cnt = 0;
1352163953Srrs	}
1353217760Stuexen	if (stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1354218241Stuexen		/* No stream scheduler information, initialize scheduler */
1355218241Stuexen		stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 0);
1356218241Stuexen		if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1357218241Stuexen			/* yep, we lost a stream or two */
1358218241Stuexen			SCTP_PRINTF("Found additional streams NOT managed by scheduler, corrected\n");
1359163953Srrs		} else {
1360218241Stuexen			/* no streams lost */
1361163953Srrs			stcb->asoc.total_output_queue_size = 0;
1362163953Srrs		}
1363163953Srrs	}
1364163953Srrs	/* Check to see if some data queued, if so report it */
1365217760Stuexen	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1366217760Stuexen		if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1367217760Stuexen			TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
1368163953Srrs				if (sp->msg_is_complete)
1369163953Srrs					being_filled++;
1370163953Srrs				chks_in_queue++;
1371163953Srrs			}
1372163953Srrs		}
1373163953Srrs	}
1374163953Srrs	if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1375169420Srrs		SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1376163953Srrs		    stcb->asoc.stream_queue_cnt, chks_in_queue);
1377163953Srrs	}
1378163953Srrs	if (chks_in_queue) {
1379163953Srrs		/* call the output queue function */
1380172090Srrs		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1381163953Srrs		if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1382163953Srrs		    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1383163953Srrs			/*
1384163953Srrs			 * Probably should go in and make it go back through
1385163953Srrs			 * and add fragments allowed
1386163953Srrs			 */
1387163953Srrs			if (being_filled == 0) {
1388169420Srrs				SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
1389163953Srrs				    chks_in_queue);
1390163953Srrs			}
1391163953Srrs		}
1392163953Srrs	} else {
1393169420Srrs		SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
1394163953Srrs		    (u_long)stcb->asoc.total_output_queue_size);
1395163953Srrs		stcb->asoc.total_output_queue_size = 0;
1396163953Srrs	}
1397163953Srrs}
1398163953Srrs
1399163953Srrsint
1400163953Srrssctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1401224641Stuexen    struct sctp_nets *net)
1402163953Srrs{
1403224641Stuexen	uint8_t net_was_pf;
1404171440Srrs
1405231039Stuexen	if (net->dest_state & SCTP_ADDR_PF) {
1406231039Stuexen		net_was_pf = 1;
1407231039Stuexen	} else {
1408231039Stuexen		net_was_pf = 0;
1409231039Stuexen	}
1410231039Stuexen	if (net->hb_responded == 0) {
1411231039Stuexen		if (net->ro._s_addr) {
1412231039Stuexen			/*
1413231039Stuexen			 * Invalidate the src address if we did not get a
1414231039Stuexen			 * response last time.
1415231039Stuexen			 */
1416231039Stuexen			sctp_free_ifa(net->ro._s_addr);
1417231039Stuexen			net->ro._s_addr = NULL;
1418231039Stuexen			net->src_addr_selected = 0;
1419224641Stuexen		}
1420231039Stuexen		sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1421231039Stuexen		if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1422231039Stuexen			/* Assoc is over */
1423231039Stuexen			return (1);
1424163953Srrs		}
1425163953Srrs	}
1426231039Stuexen	/* Zero PBA, if it needs it */
1427231039Stuexen	if (net->partial_bytes_acked) {
1428231039Stuexen		net->partial_bytes_acked = 0;
1429231039Stuexen	}
1430163953Srrs	if ((stcb->asoc.total_output_queue_size > 0) &&
1431163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1432163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1433163953Srrs		sctp_audit_stream_queues_for_size(inp, stcb);
1434163953Srrs	}
1435224641Stuexen	if (!(net->dest_state & SCTP_ADDR_NOHB) &&
1436224641Stuexen	    !((net_was_pf == 0) && (net->dest_state & SCTP_ADDR_PF))) {
1437163953Srrs		/*
1438224641Stuexen		 * when move to PF during threshold mangement, a HB has been
1439224641Stuexen		 * queued in that routine
1440163953Srrs		 */
1441234793Stuexen		uint32_t ms_gone_by;
1442234793Stuexen
1443234793Stuexen		if ((net->last_sent_time.tv_sec > 0) ||
1444234793Stuexen		    (net->last_sent_time.tv_usec > 0)) {
1445234793Stuexen			struct timeval diff;
1446234793Stuexen
1447234793Stuexen			SCTP_GETTIME_TIMEVAL(&diff);
1448234793Stuexen			timevalsub(&diff, &net->last_sent_time);
1449234793Stuexen			ms_gone_by = (uint32_t) (diff.tv_sec * 1000) +
1450234793Stuexen			    (uint32_t) (diff.tv_usec / 1000);
1451234793Stuexen		} else {
1452234793Stuexen			ms_gone_by = 0xffffffff;
1453234793Stuexen		}
1454234793Stuexen		if ((ms_gone_by >= net->heart_beat_delay) ||
1455234793Stuexen		    (net->dest_state & SCTP_ADDR_PF)) {
1456234793Stuexen			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
1457234793Stuexen		}
1458163953Srrs	}
1459163953Srrs	return (0);
1460163953Srrs}
1461163953Srrs
1462163953Srrsvoid
1463163953Srrssctp_pathmtu_timer(struct sctp_inpcb *inp,
1464163953Srrs    struct sctp_tcb *stcb,
1465163953Srrs    struct sctp_nets *net)
1466163953Srrs{
1467179157Srrs	uint32_t next_mtu, mtu;
1468163953Srrs
1469231038Stuexen	next_mtu = sctp_get_next_mtu(net->mtu);
1470169352Srrs
1471179157Srrs	if ((next_mtu > net->mtu) && (net->port == 0)) {
1472169352Srrs		if ((net->src_addr_selected == 0) ||
1473169352Srrs		    (net->ro._s_addr == NULL) ||
1474169352Srrs		    (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1475169420Srrs			if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1476169352Srrs				sctp_free_ifa(net->ro._s_addr);
1477169352Srrs				net->ro._s_addr = NULL;
1478169352Srrs				net->src_addr_selected = 0;
1479169420Srrs			} else if (net->ro._s_addr == NULL) {
1480179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1481179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1482179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1483179157Srrs
1484179157Srrs					/* KAME hack: embed scopeid */
1485197288Srrs					(void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
1486179157Srrs				}
1487179157Srrs#endif
1488179157Srrs
1489169420Srrs				net->ro._s_addr = sctp_source_address_selection(inp,
1490169420Srrs				    stcb,
1491169420Srrs				    (sctp_route_t *) & net->ro,
1492169420Srrs				    net, 0, stcb->asoc.vrf_id);
1493179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1494179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1495179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1496179157Srrs
1497179157Srrs					(void)sa6_recoverscope(sin6);
1498179157Srrs				}
1499179157Srrs#endif				/* INET6 */
1500169352Srrs			}
1501169352Srrs			if (net->ro._s_addr)
1502169352Srrs				net->src_addr_selected = 1;
1503169352Srrs		}
1504169352Srrs		if (net->ro._s_addr) {
1505169352Srrs			mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
1506185694Srrs			if (net->port) {
1507185694Srrs				mtu -= sizeof(struct udphdr);
1508185694Srrs			}
1509169352Srrs			if (mtu > next_mtu) {
1510163953Srrs				net->mtu = next_mtu;
1511163953Srrs			}
1512163953Srrs		}
1513163953Srrs	}
1514163953Srrs	/* restart the timer */
1515163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1516163953Srrs}
1517163953Srrs
1518163953Srrsvoid
1519163953Srrssctp_autoclose_timer(struct sctp_inpcb *inp,
1520163953Srrs    struct sctp_tcb *stcb,
1521163953Srrs    struct sctp_nets *net)
1522163953Srrs{
1523163953Srrs	struct timeval tn, *tim_touse;
1524163953Srrs	struct sctp_association *asoc;
1525163953Srrs	int ticks_gone_by;
1526163953Srrs
1527169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&tn);
1528163953Srrs	if (stcb->asoc.sctp_autoclose_ticks &&
1529163953Srrs	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1530163953Srrs		/* Auto close is on */
1531163953Srrs		asoc = &stcb->asoc;
1532163953Srrs		/* pick the time to use */
1533163953Srrs		if (asoc->time_last_rcvd.tv_sec >
1534163953Srrs		    asoc->time_last_sent.tv_sec) {
1535163953Srrs			tim_touse = &asoc->time_last_rcvd;
1536163953Srrs		} else {
1537163953Srrs			tim_touse = &asoc->time_last_sent;
1538163953Srrs		}
1539163953Srrs		/* Now has long enough transpired to autoclose? */
1540163953Srrs		ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
1541163953Srrs		if ((ticks_gone_by > 0) &&
1542163953Srrs		    (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1543163953Srrs			/*
1544163953Srrs			 * autoclose time has hit, call the output routine,
1545163953Srrs			 * which should do nothing just to be SURE we don't
1546163953Srrs			 * have hanging data. We can then safely check the
1547163953Srrs			 * queues and know that we are clear to send
1548163953Srrs			 * shutdown
1549163953Srrs			 */
1550172090Srrs			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1551163953Srrs			/* Are we clean? */
1552163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
1553163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue)) {
1554163953Srrs				/*
1555163953Srrs				 * there is nothing queued to send, so I'm
1556163953Srrs				 * done...
1557163953Srrs				 */
1558166675Srrs				if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1559163953Srrs					/* only send SHUTDOWN 1st time thru */
1560224641Stuexen					struct sctp_nets *netp;
1561224641Stuexen
1562224641Stuexen					if (stcb->asoc.alternate) {
1563224641Stuexen						netp = stcb->asoc.alternate;
1564224641Stuexen					} else {
1565224641Stuexen						netp = stcb->asoc.primary_destination;
1566224641Stuexen					}
1567224641Stuexen					sctp_send_shutdown(stcb, netp);
1568166675Srrs					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1569166675Srrs					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1570166675Srrs						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1571166675Srrs					}
1572171943Srrs					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1573172703Srrs					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1574163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1575163953Srrs					    stcb->sctp_ep, stcb,
1576224641Stuexen					    netp);
1577163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1578163953Srrs					    stcb->sctp_ep, stcb,
1579224641Stuexen					    netp);
1580163953Srrs				}
1581163953Srrs			}
1582163953Srrs		} else {
1583163953Srrs			/*
1584163953Srrs			 * No auto close at this time, reset t-o to check
1585163953Srrs			 * later
1586163953Srrs			 */
1587163953Srrs			int tmp;
1588163953Srrs
1589163953Srrs			/* fool the timer startup to use the time left */
1590163953Srrs			tmp = asoc->sctp_autoclose_ticks;
1591163953Srrs			asoc->sctp_autoclose_ticks -= ticks_gone_by;
1592163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1593163953Srrs			    net);
1594163953Srrs			/* restore the real tick value */
1595163953Srrs			asoc->sctp_autoclose_ticks = tmp;
1596163953Srrs		}
1597163953Srrs	}
1598163953Srrs}
1599