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