xref: /freebsd/sys/netinet/sctp_timer.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1f8829a4aSRandall Stewart /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4b1006367SRandall Stewart  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7f8829a4aSRandall Stewart  *
8f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
9f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
10f8829a4aSRandall Stewart  *
11f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
12f8829a4aSRandall Stewart  *    this list of conditions and the following disclaimer.
13f8829a4aSRandall Stewart  *
14f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
15f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
16f8829a4aSRandall Stewart  *    the documentation and/or other materials provided with the distribution.
17f8829a4aSRandall Stewart  *
18f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
20f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
21f8829a4aSRandall Stewart  *
22f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
33f8829a4aSRandall Stewart  */
34f8829a4aSRandall Stewart 
35f8829a4aSRandall Stewart #define _IP_VHL
3693164cf9SRandall Stewart #include <netinet/sctp_os.h>
37f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
38*fdafd315SWarner Losh 
39f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
4042551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
41f8829a4aSRandall Stewart #include <netinet/sctp_timer.h>
42f8829a4aSRandall Stewart #include <netinet/sctputil.h>
43f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>
46f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h>
47f8829a4aSRandall Stewart #include <netinet/sctp_input.h>
48f8829a4aSRandall Stewart #include <netinet/sctp.h>
49f8829a4aSRandall Stewart #include <netinet/sctp_uio.h>
504474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
51830d754dSRandall Stewart #include <netinet/udp.h>
524474d71aSMichael Tuexen #endif
53f8829a4aSRandall Stewart 
54f8829a4aSRandall Stewart void
sctp_audit_retranmission_queue(struct sctp_association * asoc)55f8829a4aSRandall Stewart sctp_audit_retranmission_queue(struct sctp_association *asoc)
56f8829a4aSRandall Stewart {
57f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
58f8829a4aSRandall Stewart 
59ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit invoked on send queue cnt:%d onqueue:%d\n",
60f8829a4aSRandall Stewart 	    asoc->sent_queue_retran_cnt,
61f8829a4aSRandall Stewart 	    asoc->sent_queue_cnt);
62f8829a4aSRandall Stewart 	asoc->sent_queue_retran_cnt = 0;
63f8829a4aSRandall Stewart 	asoc->sent_queue_cnt = 0;
64f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
65f8829a4aSRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
66f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
67f8829a4aSRandall Stewart 		}
68f8829a4aSRandall Stewart 		asoc->sent_queue_cnt++;
69f8829a4aSRandall Stewart 	}
70f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
71f8829a4aSRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
72f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
73f8829a4aSRandall Stewart 		}
74f8829a4aSRandall Stewart 	}
75c54a18d2SRandall Stewart 	TAILQ_FOREACH(chk, &asoc->asconf_send_queue, sctp_next) {
76c54a18d2SRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
77c54a18d2SRandall Stewart 			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
78c54a18d2SRandall Stewart 		}
79c54a18d2SRandall Stewart 	}
80ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit completes retran:%d onqueue:%d\n",
81f8829a4aSRandall Stewart 	    asoc->sent_queue_retran_cnt,
82f8829a4aSRandall Stewart 	    asoc->sent_queue_cnt);
83f8829a4aSRandall Stewart }
84f8829a4aSRandall Stewart 
85815f806bSMichael Tuexen static int
sctp_threshold_management(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net,uint16_t threshold)86f8829a4aSRandall Stewart sctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
87f8829a4aSRandall Stewart     struct sctp_nets *net, uint16_t threshold)
88f8829a4aSRandall Stewart {
89f0c8e811SMichael Tuexen 	KASSERT(stcb != NULL, ("stcb is NULL"));
90f0c8e811SMichael Tuexen 	SCTP_TCB_LOCK_ASSERT(stcb);
91f0c8e811SMichael Tuexen 
92f0c8e811SMichael Tuexen 	if (net != NULL) {
93f8829a4aSRandall Stewart 		net->error_count++;
94ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER4, "Error count for %p now %d thresh:%d\n",
95dd294dceSMichael Tuexen 		    (void *)net, net->error_count,
96f8829a4aSRandall Stewart 		    net->failure_threshold);
97f8829a4aSRandall Stewart 		if (net->error_count > net->failure_threshold) {
98f8829a4aSRandall Stewart 			/* We had a threshold failure */
99f8829a4aSRandall Stewart 			if (net->dest_state & SCTP_ADDR_REACHABLE) {
100f8829a4aSRandall Stewart 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
10142551e99SRandall Stewart 				net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
102b54d3a6cSRandall Stewart 				net->dest_state &= ~SCTP_ADDR_PF;
103f8829a4aSRandall Stewart 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
1044b1f78e1SMichael Tuexen 				    stcb, 0,
105ceaad40aSRandall Stewart 				    (void *)net, SCTP_SO_NOT_LOCKED);
106f8829a4aSRandall Stewart 			}
107ca85e948SMichael Tuexen 		} else if ((net->pf_threshold < net->failure_threshold) &&
108ca85e948SMichael Tuexen 		    (net->error_count > net->pf_threshold)) {
1099b2a35b3SMichael Tuexen 			if ((net->dest_state & SCTP_ADDR_PF) == 0) {
110ca85e948SMichael Tuexen 				net->dest_state |= SCTP_ADDR_PF;
111ca85e948SMichael Tuexen 				net->last_active = sctp_get_tick_count();
112ca85e948SMichael Tuexen 				sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
113b7d130beSMichael Tuexen 				sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
114815f806bSMichael Tuexen 				    inp, stcb, net,
115b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_TIMER + SCTP_LOC_1);
116815f806bSMichael Tuexen 				sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
117f8829a4aSRandall Stewart 			}
118ca85e948SMichael Tuexen 		}
119f8829a4aSRandall Stewart 		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
120b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
121c4739e2fSRandall Stewart 				sctp_misc_ints(SCTP_THRESHOLD_INCR,
122c4739e2fSRandall Stewart 				    stcb->asoc.overall_error_count,
123c4739e2fSRandall Stewart 				    (stcb->asoc.overall_error_count + 1),
124c4739e2fSRandall Stewart 				    SCTP_FROM_SCTP_TIMER,
125c4739e2fSRandall Stewart 				    __LINE__);
126c4739e2fSRandall Stewart 			}
127f8829a4aSRandall Stewart 			stcb->asoc.overall_error_count++;
128f8829a4aSRandall Stewart 		}
129f8829a4aSRandall Stewart 	} else {
130b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
131c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_INCR,
132c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
133c4739e2fSRandall Stewart 			    (stcb->asoc.overall_error_count + 1),
134c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_TIMER,
135c4739e2fSRandall Stewart 			    __LINE__);
136c4739e2fSRandall Stewart 		}
137f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count++;
138f8829a4aSRandall Stewart 	}
139ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n",
140dd294dceSMichael Tuexen 	    (void *)&stcb->asoc, stcb->asoc.overall_error_count,
141f8829a4aSRandall Stewart 	    (uint32_t)threshold,
142f8829a4aSRandall Stewart 	    ((net == NULL) ? (uint32_t)0 : (uint32_t)net->dest_state));
143f8829a4aSRandall Stewart 	/*
144f8829a4aSRandall Stewart 	 * We specifically do not do >= to give the assoc one more change
145f8829a4aSRandall Stewart 	 * before we fail it.
146f8829a4aSRandall Stewart 	 */
147f8829a4aSRandall Stewart 	if (stcb->asoc.overall_error_count > threshold) {
148f8829a4aSRandall Stewart 		/* Abort notification sends a ULP notify */
149ff1ffd74SMichael Tuexen 		struct mbuf *op_err;
150f8829a4aSRandall Stewart 
151267dbe63SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
1528f290ed5SMichael Tuexen 		    "Association error counter exceeded");
153b7d130beSMichael Tuexen 		inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_2;
154105b68b4SMichael Tuexen 		sctp_abort_an_association(inp, stcb, op_err, true, SCTP_SO_NOT_LOCKED);
155f8829a4aSRandall Stewart 		return (1);
156f8829a4aSRandall Stewart 	}
157f8829a4aSRandall Stewart 	return (0);
158f8829a4aSRandall Stewart }
159f8829a4aSRandall Stewart 
160b61c3588SMichael Tuexen /*
161b963ce45SMichael Tuexen  * sctp_find_alternate_net() returns a non-NULL pointer as long as there
162b963ce45SMichael Tuexen  * exists nets, which are not being deleted.
163b61c3588SMichael Tuexen  */
164f8829a4aSRandall Stewart struct sctp_nets *
sctp_find_alternate_net(struct sctp_tcb * stcb,struct sctp_nets * net,int mode)165f8829a4aSRandall Stewart sctp_find_alternate_net(struct sctp_tcb *stcb,
166f8829a4aSRandall Stewart     struct sctp_nets *net,
167b54d3a6cSRandall Stewart     int mode)
168f8829a4aSRandall Stewart {
169f8829a4aSRandall Stewart 	/* Find and return an alternate network if possible */
170b54d3a6cSRandall Stewart 	struct sctp_nets *alt, *mnet, *min_errors_net = NULL, *max_cwnd_net = NULL;
171b963ce45SMichael Tuexen 	bool looped;
172b54d3a6cSRandall Stewart 
173b54d3a6cSRandall Stewart 	/* JRS 5/14/07 - Initialize min_errors to an impossible value. */
174b54d3a6cSRandall Stewart 	int min_errors = -1;
175b54d3a6cSRandall Stewart 	uint32_t max_cwnd = 0;
176f8829a4aSRandall Stewart 
177f8829a4aSRandall Stewart 	if (stcb->asoc.numnets == 1) {
178b963ce45SMichael Tuexen 		/* No selection can be made. */
179f8829a4aSRandall Stewart 		return (TAILQ_FIRST(&stcb->asoc.nets));
180f8829a4aSRandall Stewart 	}
181b54d3a6cSRandall Stewart 	/*
182b54d3a6cSRandall Stewart 	 * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate
183b54d3a6cSRandall Stewart 	 * net algorithm. This algorithm chooses the active destination (not
184b54d3a6cSRandall Stewart 	 * in PF state) with the largest cwnd value. If all destinations are
185e7e65008SMichael Tuexen 	 * in PF state, unreachable, or unconfirmed, choose the destination
186b54d3a6cSRandall Stewart 	 * that is in PF state with the lowest error count. In case of a
187b54d3a6cSRandall Stewart 	 * tie, choose the destination that was most recently active.
188b54d3a6cSRandall Stewart 	 */
189b54d3a6cSRandall Stewart 	if (mode == 2) {
190b54d3a6cSRandall Stewart 		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
191b7b84c0eSMichael Tuexen 			/*
192b7b84c0eSMichael Tuexen 			 * JRS 5/14/07 - If the destination is unreachable
193b7b84c0eSMichael Tuexen 			 * or unconfirmed, skip it.
194b7b84c0eSMichael Tuexen 			 */
195b54d3a6cSRandall Stewart 			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
196b54d3a6cSRandall Stewart 			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
197b54d3a6cSRandall Stewart 				continue;
198b54d3a6cSRandall Stewart 			}
199b54d3a6cSRandall Stewart 			/*
200b54d3a6cSRandall Stewart 			 * JRS 5/14/07 -  If the destination is reachable
201b54d3a6cSRandall Stewart 			 * but in PF state, compare the error count of the
202b54d3a6cSRandall Stewart 			 * destination to the minimum error count seen thus
203b54d3a6cSRandall Stewart 			 * far. Store the destination with the lower error
204b54d3a6cSRandall Stewart 			 * count.  If the error counts are equal, store the
205b54d3a6cSRandall Stewart 			 * destination that was most recently active.
206b54d3a6cSRandall Stewart 			 */
207b54d3a6cSRandall Stewart 			if (mnet->dest_state & SCTP_ADDR_PF) {
208b54d3a6cSRandall Stewart 				/*
209b54d3a6cSRandall Stewart 				 * JRS 5/14/07 - If the destination under
210b54d3a6cSRandall Stewart 				 * consideration is the current destination,
211b54d3a6cSRandall Stewart 				 * work as if the error count is one higher.
212b54d3a6cSRandall Stewart 				 * The actual error count will not be
213b54d3a6cSRandall Stewart 				 * incremented until later in the t3
214b54d3a6cSRandall Stewart 				 * handler.
215b54d3a6cSRandall Stewart 				 */
216b54d3a6cSRandall Stewart 				if (mnet == net) {
217b54d3a6cSRandall Stewart 					if (min_errors == -1) {
218b54d3a6cSRandall Stewart 						min_errors = mnet->error_count + 1;
219b54d3a6cSRandall Stewart 						min_errors_net = mnet;
220b54d3a6cSRandall Stewart 					} else if (mnet->error_count + 1 < min_errors) {
221b54d3a6cSRandall Stewart 						min_errors = mnet->error_count + 1;
222b54d3a6cSRandall Stewart 						min_errors_net = mnet;
223b54d3a6cSRandall Stewart 					} else if (mnet->error_count + 1 == min_errors
224b54d3a6cSRandall Stewart 					    && mnet->last_active > min_errors_net->last_active) {
225b54d3a6cSRandall Stewart 						min_errors_net = mnet;
226b54d3a6cSRandall Stewart 						min_errors = mnet->error_count + 1;
227b54d3a6cSRandall Stewart 					}
228b54d3a6cSRandall Stewart 					continue;
229b54d3a6cSRandall Stewart 				} else {
230b54d3a6cSRandall Stewart 					if (min_errors == -1) {
231b54d3a6cSRandall Stewart 						min_errors = mnet->error_count;
232b54d3a6cSRandall Stewart 						min_errors_net = mnet;
233b54d3a6cSRandall Stewart 					} else if (mnet->error_count < min_errors) {
234b54d3a6cSRandall Stewart 						min_errors = mnet->error_count;
235b54d3a6cSRandall Stewart 						min_errors_net = mnet;
236b54d3a6cSRandall Stewart 					} else if (mnet->error_count == min_errors
237b54d3a6cSRandall Stewart 					    && mnet->last_active > min_errors_net->last_active) {
238b54d3a6cSRandall Stewart 						min_errors_net = mnet;
239b54d3a6cSRandall Stewart 						min_errors = mnet->error_count;
240b54d3a6cSRandall Stewart 					}
241b54d3a6cSRandall Stewart 					continue;
242b54d3a6cSRandall Stewart 				}
243b54d3a6cSRandall Stewart 			}
244b54d3a6cSRandall Stewart 			/*
245b54d3a6cSRandall Stewart 			 * JRS 5/14/07 - If the destination is reachable and
246b54d3a6cSRandall Stewart 			 * not in PF state, compare the cwnd of the
247b54d3a6cSRandall Stewart 			 * destination to the highest cwnd seen thus far.
248b54d3a6cSRandall Stewart 			 * Store the destination with the higher cwnd value.
249b54d3a6cSRandall Stewart 			 * If the cwnd values are equal, randomly choose one
250b54d3a6cSRandall Stewart 			 * of the two destinations.
251b54d3a6cSRandall Stewart 			 */
252b54d3a6cSRandall Stewart 			if (max_cwnd < mnet->cwnd) {
253b54d3a6cSRandall Stewart 				max_cwnd_net = mnet;
254b54d3a6cSRandall Stewart 				max_cwnd = mnet->cwnd;
255b54d3a6cSRandall Stewart 			} else if (max_cwnd == mnet->cwnd) {
256b54d3a6cSRandall Stewart 				uint32_t rndval;
257b54d3a6cSRandall Stewart 				uint8_t this_random;
258b54d3a6cSRandall Stewart 
259b54d3a6cSRandall Stewart 				if (stcb->asoc.hb_random_idx > 3) {
260b54d3a6cSRandall Stewart 					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
261b54d3a6cSRandall Stewart 					memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values));
262b54d3a6cSRandall Stewart 					this_random = stcb->asoc.hb_random_values[0];
263b54d3a6cSRandall Stewart 					stcb->asoc.hb_random_idx++;
264b54d3a6cSRandall Stewart 					stcb->asoc.hb_ect_randombit = 0;
265b54d3a6cSRandall Stewart 				} else {
266b54d3a6cSRandall Stewart 					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
267b54d3a6cSRandall Stewart 					stcb->asoc.hb_random_idx++;
268b54d3a6cSRandall Stewart 					stcb->asoc.hb_ect_randombit = 0;
269b54d3a6cSRandall Stewart 				}
270b54d3a6cSRandall Stewart 				if (this_random % 2 == 1) {
271b54d3a6cSRandall Stewart 					max_cwnd_net = mnet;
272fc14de76SRandall Stewart 					max_cwnd = mnet->cwnd;	/* Useless? */
273b54d3a6cSRandall Stewart 				}
274b54d3a6cSRandall Stewart 			}
275b54d3a6cSRandall Stewart 		}
276b54d3a6cSRandall Stewart 		if (max_cwnd_net == NULL) {
277b54d3a6cSRandall Stewart 			if (min_errors_net == NULL) {
278b54d3a6cSRandall Stewart 				return (net);
279b54d3a6cSRandall Stewart 			}
280b54d3a6cSRandall Stewart 			return (min_errors_net);
281b54d3a6cSRandall Stewart 		} else {
282b54d3a6cSRandall Stewart 			return (max_cwnd_net);
283b54d3a6cSRandall Stewart 		}
2845b495f17SMichael Tuexen 	}			/* JRS 5/14/07 - If mode is set to 1, use the
2855b495f17SMichael Tuexen 				 * CMT policy for choosing an alternate net. */
286b54d3a6cSRandall Stewart 	else if (mode == 1) {
287f8829a4aSRandall Stewart 		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
288f8829a4aSRandall Stewart 			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
289b61c3588SMichael Tuexen 			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
290f8829a4aSRandall Stewart 				/*
291f8829a4aSRandall Stewart 				 * will skip ones that are not-reachable or
292f8829a4aSRandall Stewart 				 * unconfirmed
293f8829a4aSRandall Stewart 				 */
294f8829a4aSRandall Stewart 				continue;
295f8829a4aSRandall Stewart 			}
296b54d3a6cSRandall Stewart 			if (max_cwnd < mnet->cwnd) {
297b54d3a6cSRandall Stewart 				max_cwnd_net = mnet;
298b54d3a6cSRandall Stewart 				max_cwnd = mnet->cwnd;
299b54d3a6cSRandall Stewart 			} else if (max_cwnd == mnet->cwnd) {
300f8829a4aSRandall Stewart 				uint32_t rndval;
301f8829a4aSRandall Stewart 				uint8_t this_random;
302f8829a4aSRandall Stewart 
303f8829a4aSRandall Stewart 				if (stcb->asoc.hb_random_idx > 3) {
304f8829a4aSRandall Stewart 					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
305f8829a4aSRandall Stewart 					memcpy(stcb->asoc.hb_random_values, &rndval,
306f8829a4aSRandall Stewart 					    sizeof(stcb->asoc.hb_random_values));
307f8829a4aSRandall Stewart 					this_random = stcb->asoc.hb_random_values[0];
308f8829a4aSRandall Stewart 					stcb->asoc.hb_random_idx = 0;
309f8829a4aSRandall Stewart 					stcb->asoc.hb_ect_randombit = 0;
310f8829a4aSRandall Stewart 				} else {
311f8829a4aSRandall Stewart 					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
312f8829a4aSRandall Stewart 					stcb->asoc.hb_random_idx++;
313f8829a4aSRandall Stewart 					stcb->asoc.hb_ect_randombit = 0;
314f8829a4aSRandall Stewart 				}
315f8829a4aSRandall Stewart 				if (this_random % 2) {
316b54d3a6cSRandall Stewart 					max_cwnd_net = mnet;
317b54d3a6cSRandall Stewart 					max_cwnd = mnet->cwnd;
318f8829a4aSRandall Stewart 				}
319f8829a4aSRandall Stewart 			}
320f8829a4aSRandall Stewart 		}
321b54d3a6cSRandall Stewart 		if (max_cwnd_net) {
322b54d3a6cSRandall Stewart 			return (max_cwnd_net);
323f8829a4aSRandall Stewart 		}
324f8829a4aSRandall Stewart 	}
325b963ce45SMichael Tuexen 	/* Look for an alternate net, which is active. */
326b963ce45SMichael Tuexen 	if ((net != NULL) && ((net->dest_state & SCTP_ADDR_BEING_DELETED) == 0)) {
32745421646SMichael Tuexen 		alt = TAILQ_NEXT(net, sctp_next);
328b963ce45SMichael Tuexen 	} else {
329f8829a4aSRandall Stewart 		alt = TAILQ_FIRST(&stcb->asoc.nets);
330b963ce45SMichael Tuexen 	}
331b963ce45SMichael Tuexen 	looped = false;
332b963ce45SMichael Tuexen 	for (;;) {
33352129fcdSRandall Stewart 		if (alt == NULL) {
334b963ce45SMichael Tuexen 			if (!looped) {
335b963ce45SMichael Tuexen 				alt = TAILQ_FIRST(&stcb->asoc.nets);
336b963ce45SMichael Tuexen 				looped = true;
337b963ce45SMichael Tuexen 			}
338b963ce45SMichael Tuexen 			/* Definitely out of candidates. */
339b963ce45SMichael Tuexen 			if (alt == NULL) {
340b963ce45SMichael Tuexen 				break;
34152129fcdSRandall Stewart 			}
342f8829a4aSRandall Stewart 		}
343983066f0SAlexander V. Chernikov 		if (alt->ro.ro_nh == NULL) {
34442551e99SRandall Stewart 			if (alt->ro._s_addr) {
34542551e99SRandall Stewart 				sctp_free_ifa(alt->ro._s_addr);
34642551e99SRandall Stewart 				alt->ro._s_addr = NULL;
34742551e99SRandall Stewart 			}
348f8829a4aSRandall Stewart 			alt->src_addr_selected = 0;
349f8829a4aSRandall Stewart 		}
350b61c3588SMichael Tuexen 		if (((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
351983066f0SAlexander V. Chernikov 		    (alt->ro.ro_nh != NULL) &&
3529b2a35b3SMichael Tuexen 		    ((alt->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) &&
353b963ce45SMichael Tuexen 		    (alt != net)) {
354b963ce45SMichael Tuexen 			/* Found an alternate net, which is reachable. */
355f8829a4aSRandall Stewart 			break;
356f8829a4aSRandall Stewart 		}
357b963ce45SMichael Tuexen 		alt = TAILQ_NEXT(alt, sctp_next);
3582010054dSMichael Tuexen 	}
359f8829a4aSRandall Stewart 
360f8829a4aSRandall Stewart 	if (alt == NULL) {
361b963ce45SMichael Tuexen 		/*
362b963ce45SMichael Tuexen 		 * In case no active alternate net has been found, look for
363b963ce45SMichael Tuexen 		 * an alternate net, which is confirmed.
364b963ce45SMichael Tuexen 		 */
365b963ce45SMichael Tuexen 		if ((net != NULL) && ((net->dest_state & SCTP_ADDR_BEING_DELETED) == 0)) {
36645421646SMichael Tuexen 			alt = TAILQ_NEXT(net, sctp_next);
367b963ce45SMichael Tuexen 		} else {
368f8829a4aSRandall Stewart 			alt = TAILQ_FIRST(&stcb->asoc.nets);
369b963ce45SMichael Tuexen 		}
370b963ce45SMichael Tuexen 		looped = false;
371b963ce45SMichael Tuexen 		for (;;) {
372b963ce45SMichael Tuexen 			if (alt == NULL) {
373b963ce45SMichael Tuexen 				if (!looped) {
374b963ce45SMichael Tuexen 					alt = TAILQ_FIRST(&stcb->asoc.nets);
375b963ce45SMichael Tuexen 					looped = true;
376b963ce45SMichael Tuexen 				}
377b963ce45SMichael Tuexen 				/* Definitely out of candidates. */
3782010054dSMichael Tuexen 				if (alt == NULL) {
3792010054dSMichael Tuexen 					break;
380f8829a4aSRandall Stewart 				}
3812010054dSMichael Tuexen 			}
3829b2a35b3SMichael Tuexen 			if (((alt->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) &&
383f8829a4aSRandall Stewart 			    (alt != net)) {
384b963ce45SMichael Tuexen 				/*
385b963ce45SMichael Tuexen 				 * Found an alternate net, which is
386b963ce45SMichael Tuexen 				 * confirmed.
387b963ce45SMichael Tuexen 				 */
388f8829a4aSRandall Stewart 				break;
389f8829a4aSRandall Stewart 			}
390b963ce45SMichael Tuexen 			alt = TAILQ_NEXT(alt, sctp_next);
3912010054dSMichael Tuexen 		}
392f8829a4aSRandall Stewart 	}
393f8829a4aSRandall Stewart 	if (alt == NULL) {
394b963ce45SMichael Tuexen 		/*
395b963ce45SMichael Tuexen 		 * In case no confirmed alternate net has been found, just
396b963ce45SMichael Tuexen 		 * return net, if it is not being deleted. In the other case
397b963ce45SMichael Tuexen 		 * just return the first net.
398b963ce45SMichael Tuexen 		 */
399b963ce45SMichael Tuexen 		if ((net != NULL) && ((net->dest_state & SCTP_ADDR_BEING_DELETED) == 0)) {
400b963ce45SMichael Tuexen 			alt = net;
401b963ce45SMichael Tuexen 		}
402b963ce45SMichael Tuexen 		if (alt == NULL) {
403b963ce45SMichael Tuexen 			alt = TAILQ_FIRST(&stcb->asoc.nets);
404b963ce45SMichael Tuexen 		}
405f8829a4aSRandall Stewart 	}
406f8829a4aSRandall Stewart 	return (alt);
407f8829a4aSRandall Stewart }
408f8829a4aSRandall Stewart 
409f8829a4aSRandall Stewart static void
sctp_backoff_on_timeout(struct sctp_tcb * stcb,struct sctp_nets * net,int win_probe,int num_marked,int num_abandoned)410f8829a4aSRandall Stewart sctp_backoff_on_timeout(struct sctp_tcb *stcb,
411f8829a4aSRandall Stewart     struct sctp_nets *net,
412f8829a4aSRandall Stewart     int win_probe,
41344fbe462SRandall Stewart     int num_marked, int num_abandoned)
414f8829a4aSRandall Stewart {
4159a972525SRandall Stewart 	if (net->RTO == 0) {
41693725308SMichael Tuexen 		if (net->RTO_measured) {
4179a972525SRandall Stewart 			net->RTO = stcb->asoc.minrto;
41893725308SMichael Tuexen 		} else {
41993725308SMichael Tuexen 			net->RTO = stcb->asoc.initial_rto;
42093725308SMichael Tuexen 		}
4219a972525SRandall Stewart 	}
422f8829a4aSRandall Stewart 	net->RTO <<= 1;
423f8829a4aSRandall Stewart 	if (net->RTO > stcb->asoc.maxrto) {
424f8829a4aSRandall Stewart 		net->RTO = stcb->asoc.maxrto;
425f8829a4aSRandall Stewart 	}
42644fbe462SRandall Stewart 	if ((win_probe == 0) && (num_marked || num_abandoned)) {
427f8829a4aSRandall Stewart 		/* We don't apply penalty to window probe scenarios */
428b54d3a6cSRandall Stewart 		/* JRS - Use the congestion control given in the CC module */
429b54d3a6cSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
430f8829a4aSRandall Stewart 	}
431f8829a4aSRandall Stewart }
432f8829a4aSRandall Stewart 
43383416c88SRandall Stewart #ifndef INVARIANTS
43483416c88SRandall Stewart static void
sctp_recover_sent_list(struct sctp_tcb * stcb)435df6e0cc3SRandall Stewart sctp_recover_sent_list(struct sctp_tcb *stcb)
436df6e0cc3SRandall Stewart {
4374a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
438df6e0cc3SRandall Stewart 	struct sctp_association *asoc;
439df6e0cc3SRandall Stewart 
440df6e0cc3SRandall Stewart 	asoc = &stcb->asoc;
4414a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
44249656eefSMichael Tuexen 		if (SCTP_TSN_GE(asoc->last_acked_seq, chk->rec.data.tsn)) {
443df6e0cc3SRandall Stewart 			SCTP_PRINTF("Found chk:%p tsn:%x <= last_acked_seq:%x\n",
44449656eefSMichael Tuexen 			    (void *)chk, chk->rec.data.tsn, asoc->last_acked_seq);
445325c8c46SMichael Tuexen 			if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
44649656eefSMichael Tuexen 				if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
44749656eefSMichael Tuexen 					asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
448a7ad6026SMichael Tuexen 				}
449a7ad6026SMichael Tuexen 			}
45049656eefSMichael Tuexen 			if ((asoc->strmout[chk->rec.data.sid].chunks_on_queues == 0) &&
45149656eefSMichael Tuexen 			    (asoc->strmout[chk->rec.data.sid].state == SCTP_STREAM_RESET_PENDING) &&
45249656eefSMichael Tuexen 			    TAILQ_EMPTY(&asoc->strmout[chk->rec.data.sid].outqueue)) {
453d96bef9cSMichael Tuexen 				asoc->trigger_reset = 1;
454d96bef9cSMichael Tuexen 			}
455df6e0cc3SRandall Stewart 			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4560ddb4299SMichael Tuexen 			if (PR_SCTP_ENABLED(chk->flags)) {
457df6e0cc3SRandall Stewart 				if (asoc->pr_sctp_cnt != 0)
458df6e0cc3SRandall Stewart 					asoc->pr_sctp_cnt--;
459df6e0cc3SRandall Stewart 			}
460df6e0cc3SRandall Stewart 			if (chk->data) {
461df6e0cc3SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
462df6e0cc3SRandall Stewart 				sctp_free_bufspace(stcb, asoc, chk, 1);
463df6e0cc3SRandall Stewart 				sctp_m_freem(chk->data);
4644a9ef3f8SMichael Tuexen 				chk->data = NULL;
465dd973b0eSMichael Tuexen 				if (asoc->prsctp_supported && PR_SCTP_BUF_ENABLED(chk->flags)) {
466df6e0cc3SRandall Stewart 					asoc->sent_queue_cnt_removeable--;
467df6e0cc3SRandall Stewart 				}
468df6e0cc3SRandall Stewart 			}
469df6e0cc3SRandall Stewart 			asoc->sent_queue_cnt--;
470689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
471df6e0cc3SRandall Stewart 		}
472df6e0cc3SRandall Stewart 	}
473df6e0cc3SRandall Stewart 	SCTP_PRINTF("after recover order is as follows\n");
4744a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
47549656eefSMichael Tuexen 		SCTP_PRINTF("chk:%p TSN:%x\n", (void *)chk, chk->rec.data.tsn);
476df6e0cc3SRandall Stewart 	}
477df6e0cc3SRandall Stewart }
47883416c88SRandall Stewart #endif
47983416c88SRandall Stewart 
480f8829a4aSRandall Stewart 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)481f8829a4aSRandall Stewart sctp_mark_all_for_resend(struct sctp_tcb *stcb,
482f8829a4aSRandall Stewart     struct sctp_nets *net,
483f8829a4aSRandall Stewart     struct sctp_nets *alt,
484f8829a4aSRandall Stewart     int window_probe,
48544fbe462SRandall Stewart     int *num_marked,
48644fbe462SRandall Stewart     int *num_abandoned)
487f8829a4aSRandall Stewart {
488f8829a4aSRandall Stewart 
489f8829a4aSRandall Stewart 	/*
490f8829a4aSRandall Stewart 	 * Mark all chunks (well not all) that were sent to *net for
491f8829a4aSRandall Stewart 	 * retransmission. Move them to alt for there destination as well...
492f8829a4aSRandall Stewart 	 * We only mark chunks that have been outstanding long enough to
493f8829a4aSRandall Stewart 	 * have received feed-back.
494f8829a4aSRandall Stewart 	 */
4954a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
496f8829a4aSRandall Stewart 	struct sctp_nets *lnets;
497f8829a4aSRandall Stewart 	struct timeval now, min_wait, tv;
4980191fb6dSMichael Tuexen 	int cur_rto;
49944fbe462SRandall Stewart 	int cnt_abandoned;
500c105859eSRandall Stewart 	int audit_tf, num_mk, fir;
501f8829a4aSRandall Stewart 	unsigned int cnt_mk;
502c105859eSRandall Stewart 	uint32_t orig_flight, orig_tf;
503f8829a4aSRandall Stewart 	uint32_t tsnlast, tsnfirst;
5045bfd8cf3SDimitry Andric #ifndef INVARIANTS
505df6e0cc3SRandall Stewart 	int recovery_cnt = 0;
5065bfd8cf3SDimitry Andric #endif
507f8829a4aSRandall Stewart 
508f8829a4aSRandall Stewart 	/* none in flight now */
509f8829a4aSRandall Stewart 	audit_tf = 0;
510f8829a4aSRandall Stewart 	fir = 0;
511f8829a4aSRandall Stewart 	/*
512f8829a4aSRandall Stewart 	 * figure out how long a data chunk must be pending before we can
513f8829a4aSRandall Stewart 	 * mark it ..
514f8829a4aSRandall Stewart 	 */
5156e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&now);
516f8829a4aSRandall Stewart 	/* get cur rto in micro-seconds */
5170191fb6dSMichael Tuexen 	cur_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
5180191fb6dSMichael Tuexen 	cur_rto *= 1000;
519ca85e948SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
5200191fb6dSMichael Tuexen 		sctp_log_fr(cur_rto,
521f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
522f8829a4aSRandall Stewart 		    window_probe,
523f8829a4aSRandall Stewart 		    SCTP_FR_T3_MARK_TIME);
524ca85e948SMichael Tuexen 		sctp_log_fr(net->flight_size, 0, 0, SCTP_FR_CWND_REPORT);
525f8829a4aSRandall Stewart 		sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
52680fefe0aSRandall Stewart 	}
5270191fb6dSMichael Tuexen 	tv.tv_sec = cur_rto / 1000000;
5280191fb6dSMichael Tuexen 	tv.tv_usec = cur_rto % 1000000;
529f8829a4aSRandall Stewart 	min_wait = now;
530f8829a4aSRandall Stewart 	timevalsub(&min_wait, &tv);
531f8829a4aSRandall Stewart 	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
532f8829a4aSRandall Stewart 		/*
533f8829a4aSRandall Stewart 		 * if we hit here, we don't have enough seconds on the clock
534f8829a4aSRandall Stewart 		 * to account for the RTO. We just let the lower seconds be
535f8829a4aSRandall Stewart 		 * the bounds and don't worry about it. This may mean we
536f8829a4aSRandall Stewart 		 * will mark a lot more than we should.
537f8829a4aSRandall Stewart 		 */
538f8829a4aSRandall Stewart 		min_wait.tv_sec = min_wait.tv_usec = 0;
539f8829a4aSRandall Stewart 	}
540ca85e948SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
541aab1d593SMichael Tuexen 		sctp_log_fr(cur_rto, (uint32_t)now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
542aab1d593SMichael Tuexen 		sctp_log_fr(0, (uint32_t)min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
54380fefe0aSRandall Stewart 	}
544f8829a4aSRandall Stewart 	/*
545f8829a4aSRandall Stewart 	 * Our rwnd will be incorrect here since we are not adding back the
546f8829a4aSRandall Stewart 	 * cnt * mbuf but we will fix that down below.
547f8829a4aSRandall Stewart 	 */
548f8829a4aSRandall Stewart 	orig_flight = net->flight_size;
549c105859eSRandall Stewart 	orig_tf = stcb->asoc.total_flight;
550c105859eSRandall Stewart 
551f8829a4aSRandall Stewart 	net->fast_retran_ip = 0;
552f8829a4aSRandall Stewart 	/* Now on to each chunk */
55344fbe462SRandall Stewart 	cnt_abandoned = 0;
554f8829a4aSRandall Stewart 	num_mk = cnt_mk = 0;
555f8829a4aSRandall Stewart 	tsnfirst = tsnlast = 0;
55683416c88SRandall Stewart #ifndef INVARIANTS
557df6e0cc3SRandall Stewart start_again:
55883416c88SRandall Stewart #endif
5594a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.sent_queue, sctp_next, nchk) {
56049656eefSMichael Tuexen 		if (SCTP_TSN_GE(stcb->asoc.last_acked_seq, chk->rec.data.tsn)) {
561f8829a4aSRandall Stewart 			/* Strange case our list got out of order? */
5626ed72810SMichael Tuexen 			SCTP_PRINTF("Our list is out of order? last_acked:%x chk:%x\n",
56349656eefSMichael Tuexen 			    (unsigned int)stcb->asoc.last_acked_seq, (unsigned int)chk->rec.data.tsn);
564df6e0cc3SRandall Stewart #ifdef INVARIANTS
565df6e0cc3SRandall Stewart 			panic("last acked >= chk on sent-Q");
566df6e0cc3SRandall Stewart #else
5675bfd8cf3SDimitry Andric 			recovery_cnt++;
568df6e0cc3SRandall Stewart 			SCTP_PRINTF("Recover attempts a restart cnt:%d\n", recovery_cnt);
569df6e0cc3SRandall Stewart 			sctp_recover_sent_list(stcb);
570df6e0cc3SRandall Stewart 			if (recovery_cnt < 10) {
571df6e0cc3SRandall Stewart 				goto start_again;
572df6e0cc3SRandall Stewart 			} else {
573df6e0cc3SRandall Stewart 				SCTP_PRINTF("Recovery fails %d times??\n", recovery_cnt);
574df6e0cc3SRandall Stewart 			}
575df6e0cc3SRandall Stewart #endif
576f8829a4aSRandall Stewart 		}
577f8829a4aSRandall Stewart 		if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
578f8829a4aSRandall Stewart 			/*
579f8829a4aSRandall Stewart 			 * found one to mark: If it is less than
580f8829a4aSRandall Stewart 			 * DATAGRAM_ACKED it MUST not be a skipped or marked
581f8829a4aSRandall Stewart 			 * TSN but instead one that is either already set
582f8829a4aSRandall Stewart 			 * for retransmission OR one that needs
583f8829a4aSRandall Stewart 			 * retransmission.
584f8829a4aSRandall Stewart 			 */
585f8829a4aSRandall Stewart 
586f8829a4aSRandall Stewart 			/* validate its been outstanding long enough */
587ca85e948SMichael Tuexen 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
58849656eefSMichael Tuexen 				sctp_log_fr(chk->rec.data.tsn,
589aab1d593SMichael Tuexen 				    (uint32_t)chk->sent_rcv_time.tv_sec,
590f8829a4aSRandall Stewart 				    chk->sent_rcv_time.tv_usec,
591f8829a4aSRandall Stewart 				    SCTP_FR_T3_MARK_TIME);
59280fefe0aSRandall Stewart 			}
593f8829a4aSRandall Stewart 			if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
594f8829a4aSRandall Stewart 				/*
595f8829a4aSRandall Stewart 				 * we have reached a chunk that was sent
596f8829a4aSRandall Stewart 				 * some seconds past our min.. forget it we
597f8829a4aSRandall Stewart 				 * will find no more to send.
598f8829a4aSRandall Stewart 				 */
599ca85e948SMichael Tuexen 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
600f8829a4aSRandall Stewart 					sctp_log_fr(0,
601aab1d593SMichael Tuexen 					    (uint32_t)chk->sent_rcv_time.tv_sec,
602f8829a4aSRandall Stewart 					    chk->sent_rcv_time.tv_usec,
603f8829a4aSRandall Stewart 					    SCTP_FR_T3_STOPPED);
60480fefe0aSRandall Stewart 				}
605f8829a4aSRandall Stewart 				continue;
606f8829a4aSRandall Stewart 			} else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
607f8829a4aSRandall Stewart 			    (window_probe == 0)) {
608f8829a4aSRandall Stewart 				/*
609f8829a4aSRandall Stewart 				 * we must look at the micro seconds to
610f8829a4aSRandall Stewart 				 * know.
611f8829a4aSRandall Stewart 				 */
612f8829a4aSRandall Stewart 				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
613f8829a4aSRandall Stewart 					/*
614f8829a4aSRandall Stewart 					 * ok it was sent after our boundary
615f8829a4aSRandall Stewart 					 * time.
616f8829a4aSRandall Stewart 					 */
617f8829a4aSRandall Stewart 					continue;
618f8829a4aSRandall Stewart 				}
619f8829a4aSRandall Stewart 			}
620dd973b0eSMichael Tuexen 			if (stcb->asoc.prsctp_supported && PR_SCTP_TTL_ENABLED(chk->flags)) {
621f8829a4aSRandall Stewart 				/* Is it expired? */
62299ddc825SMichael Tuexen 				if (timevalcmp(&now, &chk->rec.data.timetodrop, >)) {
623f8829a4aSRandall Stewart 					/* Yes so drop it */
624f8829a4aSRandall Stewart 					if (chk->data) {
625ad81507eSRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb,
626f8829a4aSRandall Stewart 						    chk,
6271edc9dbaSMichael Tuexen 						    1,
6280c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
62944fbe462SRandall Stewart 						cnt_abandoned++;
630f8829a4aSRandall Stewart 					}
631f8829a4aSRandall Stewart 					continue;
632f8829a4aSRandall Stewart 				}
633830d754dSRandall Stewart 			}
634dd973b0eSMichael Tuexen 			if (stcb->asoc.prsctp_supported && PR_SCTP_RTX_ENABLED(chk->flags)) {
635f8829a4aSRandall Stewart 				/* Has it been retransmitted tv_sec times? */
636f8829a4aSRandall Stewart 				if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
637f8829a4aSRandall Stewart 					if (chk->data) {
638ad81507eSRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb,
639f8829a4aSRandall Stewart 						    chk,
6401edc9dbaSMichael Tuexen 						    1,
6410c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
64244fbe462SRandall Stewart 						cnt_abandoned++;
643f8829a4aSRandall Stewart 					}
644f8829a4aSRandall Stewart 					continue;
645f8829a4aSRandall Stewart 				}
646830d754dSRandall Stewart 			}
647c105859eSRandall Stewart 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
648f8829a4aSRandall Stewart 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
649f8829a4aSRandall Stewart 				num_mk++;
650f8829a4aSRandall Stewart 				if (fir == 0) {
651f8829a4aSRandall Stewart 					fir = 1;
65249656eefSMichael Tuexen 					tsnfirst = chk->rec.data.tsn;
653f8829a4aSRandall Stewart 				}
65449656eefSMichael Tuexen 				tsnlast = chk->rec.data.tsn;
655ca85e948SMichael Tuexen 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
65649656eefSMichael Tuexen 					sctp_log_fr(chk->rec.data.tsn, chk->snd_count,
657f8829a4aSRandall Stewart 					    0, SCTP_FR_T3_MARKED);
65880fefe0aSRandall Stewart 				}
6590053ed28SMichael Tuexen 
66042551e99SRandall Stewart 				if (chk->rec.data.chunk_was_revoked) {
66142551e99SRandall Stewart 					/* deflate the cwnd */
66242551e99SRandall Stewart 					chk->whoTo->cwnd -= chk->book_size;
66342551e99SRandall Stewart 					chk->rec.data.chunk_was_revoked = 0;
66442551e99SRandall Stewart 				}
665f42a358aSRandall Stewart 				net->marked_retrans++;
666f42a358aSRandall Stewart 				stcb->asoc.marked_retrans++;
667b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
668c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
669a5d547adSRandall Stewart 					    chk->whoTo->flight_size,
670a5d547adSRandall Stewart 					    chk->book_size,
6719a8e3088SMichael Tuexen 					    (uint32_t)(uintptr_t)chk->whoTo,
67249656eefSMichael Tuexen 					    chk->rec.data.tsn);
67380fefe0aSRandall Stewart 				}
674c105859eSRandall Stewart 				sctp_flight_size_decrease(chk);
675c105859eSRandall Stewart 				sctp_total_flight_decrease(stcb, chk);
676f8829a4aSRandall Stewart 				stcb->asoc.peers_rwnd += chk->send_size;
677b3f1ea41SRandall Stewart 				stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
678c105859eSRandall Stewart 			}
679c105859eSRandall Stewart 			chk->sent = SCTP_DATAGRAM_RESEND;
6804f43a14aSMichael Tuexen 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
681c105859eSRandall Stewart 			SCTP_STAT_INCR(sctps_markedretrans);
682f8829a4aSRandall Stewart 
683f8829a4aSRandall Stewart 			/* reset the TSN for striking and other FR stuff */
684f8829a4aSRandall Stewart 			chk->rec.data.doing_fast_retransmit = 0;
685f8829a4aSRandall Stewart 			/* Clear any time so NO RTT is being done */
686f79aab18SRandall Stewart 
687f79aab18SRandall Stewart 			if (chk->do_rtt) {
688f79aab18SRandall Stewart 				if (chk->whoTo->rto_needed == 0) {
689f79aab18SRandall Stewart 					chk->whoTo->rto_needed = 1;
690f79aab18SRandall Stewart 				}
691f79aab18SRandall Stewart 			}
692f8829a4aSRandall Stewart 			chk->do_rtt = 0;
693f8829a4aSRandall Stewart 			if (alt != net) {
694f8829a4aSRandall Stewart 				sctp_free_remote_addr(chk->whoTo);
695f8829a4aSRandall Stewart 				chk->no_fr_allowed = 1;
696f8829a4aSRandall Stewart 				chk->whoTo = alt;
697f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
698f8829a4aSRandall Stewart 			} else {
699f8829a4aSRandall Stewart 				chk->no_fr_allowed = 0;
700f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
701f8829a4aSRandall Stewart 					chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
702f8829a4aSRandall Stewart 				} else {
70349656eefSMichael Tuexen 					chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
704f8829a4aSRandall Stewart 				}
705f8829a4aSRandall Stewart 			}
706ad21a364SRandall Stewart 			/*
707ad21a364SRandall Stewart 			 * CMT: Do not allow FRs on retransmitted TSNs.
708ad21a364SRandall Stewart 			 */
7097c99d56fSMichael Tuexen 			if (stcb->asoc.sctp_cmt_on_off > 0) {
710f8829a4aSRandall Stewart 				chk->no_fr_allowed = 1;
711f8829a4aSRandall Stewart 			}
71244fbe462SRandall Stewart #ifdef THIS_SHOULD_NOT_BE_DONE
713f8829a4aSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_ACKED) {
714f8829a4aSRandall Stewart 			/* remember highest acked one */
715f8829a4aSRandall Stewart 			could_be_sent = chk;
71644fbe462SRandall Stewart #endif
717f8829a4aSRandall Stewart 		}
718f8829a4aSRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
719f8829a4aSRandall Stewart 			cnt_mk++;
720f8829a4aSRandall Stewart 		}
721f8829a4aSRandall Stewart 	}
722c105859eSRandall Stewart 	if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
723c105859eSRandall Stewart 		/* we did not subtract the same things? */
724c105859eSRandall Stewart 		audit_tf = 1;
725c105859eSRandall Stewart 	}
7260053ed28SMichael Tuexen 
727ca85e948SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
728f8829a4aSRandall Stewart 		sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
72980fefe0aSRandall Stewart 	}
730f8829a4aSRandall Stewart #ifdef SCTP_DEBUG
731f8829a4aSRandall Stewart 	if (num_mk) {
732ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
733ad81507eSRandall Stewart 		    tsnlast);
7347858d7cbSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%u\n",
735f8829a4aSRandall Stewart 		    num_mk,
7367858d7cbSMichael Tuexen 		    stcb->asoc.peers_rwnd);
737f8829a4aSRandall Stewart 	}
738f8829a4aSRandall Stewart #endif
739f8829a4aSRandall Stewart 	*num_marked = num_mk;
74044fbe462SRandall Stewart 	*num_abandoned = cnt_abandoned;
7416c065bbeSRandall Stewart 	/*
7426c065bbeSRandall Stewart 	 * Now check for a ECN Echo that may be stranded And include the
7436c065bbeSRandall Stewart 	 * cnt_mk'd to have all resends in the control queue.
7446c065bbeSRandall Stewart 	 */
7456c065bbeSRandall Stewart 	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
7466c065bbeSRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
7476c065bbeSRandall Stewart 			cnt_mk++;
7486c065bbeSRandall Stewart 		}
7496c065bbeSRandall Stewart 		if ((chk->whoTo == net) &&
7506c065bbeSRandall Stewart 		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
7516c065bbeSRandall Stewart 			sctp_free_remote_addr(chk->whoTo);
7526c065bbeSRandall Stewart 			chk->whoTo = alt;
7536c065bbeSRandall Stewart 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
7546c065bbeSRandall Stewart 				chk->sent = SCTP_DATAGRAM_RESEND;
7554f43a14aSMichael Tuexen 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
7566c065bbeSRandall Stewart 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
7576c065bbeSRandall Stewart 				cnt_mk++;
7586c065bbeSRandall Stewart 			}
7596c065bbeSRandall Stewart 			atomic_add_int(&alt->ref_count, 1);
7606c065bbeSRandall Stewart 		}
7616c065bbeSRandall Stewart 	}
76244fbe462SRandall Stewart #ifdef THIS_SHOULD_NOT_BE_DONE
763f8829a4aSRandall Stewart 	if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
764f8829a4aSRandall Stewart 		/* fix it so we retransmit the highest acked anyway */
765f8829a4aSRandall Stewart 		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
766f8829a4aSRandall Stewart 		cnt_mk++;
767f8829a4aSRandall Stewart 		could_be_sent->sent = SCTP_DATAGRAM_RESEND;
768f8829a4aSRandall Stewart 	}
76944fbe462SRandall Stewart #endif
770f8829a4aSRandall Stewart 	if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
771a5d547adSRandall Stewart #ifdef INVARIANTS
77218e198d3SRandall Stewart 		SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
77318e198d3SRandall Stewart 		    cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
774f8829a4aSRandall Stewart #endif
775f8829a4aSRandall Stewart #ifndef SCTP_AUDITING_ENABLED
776f8829a4aSRandall Stewart 		stcb->asoc.sent_queue_retran_cnt = cnt_mk;
777f8829a4aSRandall Stewart #endif
778f8829a4aSRandall Stewart 	}
779f8829a4aSRandall Stewart 	if (audit_tf) {
780ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER4,
781ad81507eSRandall Stewart 		    "Audit total flight due to negative value net:%p\n",
782dd294dceSMichael Tuexen 		    (void *)net);
783f8829a4aSRandall Stewart 		stcb->asoc.total_flight = 0;
784f8829a4aSRandall Stewart 		stcb->asoc.total_flight_count = 0;
785f8829a4aSRandall Stewart 		/* Clear all networks flight size */
786f8829a4aSRandall Stewart 		TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
787f8829a4aSRandall Stewart 			lnets->flight_size = 0;
788ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_TIMER4,
789ad81507eSRandall Stewart 			    "Net:%p c-f cwnd:%d ssthresh:%d\n",
790dd294dceSMichael Tuexen 			    (void *)lnets, lnets->cwnd, lnets->ssthresh);
791f8829a4aSRandall Stewart 		}
792f8829a4aSRandall Stewart 		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
793f8829a4aSRandall Stewart 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
794b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
795a5d547adSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
796a5d547adSRandall Stewart 					    chk->whoTo->flight_size,
797a5d547adSRandall Stewart 					    chk->book_size,
7989a8e3088SMichael Tuexen 					    (uint32_t)(uintptr_t)chk->whoTo,
79949656eefSMichael Tuexen 					    chk->rec.data.tsn);
80080fefe0aSRandall Stewart 				}
8010053ed28SMichael Tuexen 
802c105859eSRandall Stewart 				sctp_flight_size_increase(chk);
803c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, chk);
804f8829a4aSRandall Stewart 			}
805f8829a4aSRandall Stewart 		}
806f8829a4aSRandall Stewart 	}
807f8829a4aSRandall Stewart 	/* We return 1 if we only have a window probe outstanding */
808f8829a4aSRandall Stewart 	return (0);
809f8829a4aSRandall Stewart }
810f8829a4aSRandall Stewart 
811f8829a4aSRandall Stewart int
sctp_t3rxt_timer(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net)812f8829a4aSRandall Stewart sctp_t3rxt_timer(struct sctp_inpcb *inp,
813f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
814f8829a4aSRandall Stewart     struct sctp_nets *net)
815f8829a4aSRandall Stewart {
816f8829a4aSRandall Stewart 	struct sctp_nets *alt;
81744fbe462SRandall Stewart 	int win_probe, num_mk, num_abandoned;
818f8829a4aSRandall Stewart 
819b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
820562a89b5SRandall Stewart 		sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
82180fefe0aSRandall Stewart 	}
822b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
823f8829a4aSRandall Stewart 		struct sctp_nets *lnet;
824f8829a4aSRandall Stewart 
825f8829a4aSRandall Stewart 		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
826f8829a4aSRandall Stewart 			if (net == lnet) {
827f8829a4aSRandall Stewart 				sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
828f8829a4aSRandall Stewart 			} else {
829f8829a4aSRandall Stewart 				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
830f8829a4aSRandall Stewart 			}
831f8829a4aSRandall Stewart 		}
832f8829a4aSRandall Stewart 	}
833f8829a4aSRandall Stewart 	/* Find an alternate and mark those for retransmission */
834f8829a4aSRandall Stewart 	if ((stcb->asoc.peers_rwnd == 0) &&
835f8829a4aSRandall Stewart 	    (stcb->asoc.total_flight < net->mtu)) {
836f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timowindowprobe);
837f8829a4aSRandall Stewart 		win_probe = 1;
838f8829a4aSRandall Stewart 	} else {
839f8829a4aSRandall Stewart 		win_probe = 0;
840f8829a4aSRandall Stewart 	}
841c105859eSRandall Stewart 
842ca85e948SMichael Tuexen 	if (win_probe == 0) {
843ca85e948SMichael Tuexen 		/* We don't do normal threshold management on window probes */
844ca85e948SMichael Tuexen 		if (sctp_threshold_management(inp, stcb, net,
845ca85e948SMichael Tuexen 		    stcb->asoc.max_send_times)) {
846ca85e948SMichael Tuexen 			/* Association was destroyed */
847ca85e948SMichael Tuexen 			return (1);
848ca85e948SMichael Tuexen 		} else {
849ca85e948SMichael Tuexen 			if (net != stcb->asoc.primary_destination) {
850ca85e948SMichael Tuexen 				/* send a immediate HB if our RTO is stale */
851ca85e948SMichael Tuexen 				struct timeval now;
852aab1d593SMichael Tuexen 				uint32_t ms_goneby;
853ca85e948SMichael Tuexen 
854ca85e948SMichael Tuexen 				(void)SCTP_GETTIME_TIMEVAL(&now);
855ca85e948SMichael Tuexen 				if (net->last_sent_time.tv_sec) {
856aab1d593SMichael Tuexen 					ms_goneby = (uint32_t)(now.tv_sec - net->last_sent_time.tv_sec) * 1000;
857ca85e948SMichael Tuexen 				} else {
858ca85e948SMichael Tuexen 					ms_goneby = 0;
859b54d3a6cSRandall Stewart 				}
860ca85e948SMichael Tuexen 				if ((net->dest_state & SCTP_ADDR_PF) == 0) {
861ca85e948SMichael Tuexen 					if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
862c105859eSRandall Stewart 						/*
863ca85e948SMichael Tuexen 						 * no recent feed back in an
864ca85e948SMichael Tuexen 						 * RTO or more, request a
865ca85e948SMichael Tuexen 						 * RTT update
866ca85e948SMichael Tuexen 						 */
867ca85e948SMichael Tuexen 						sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
868ca85e948SMichael Tuexen 					}
869ca85e948SMichael Tuexen 				}
870ca85e948SMichael Tuexen 			}
871ca85e948SMichael Tuexen 		}
872ca85e948SMichael Tuexen 	} else {
873ca85e948SMichael Tuexen 		/*
874ca85e948SMichael Tuexen 		 * For a window probe we don't penalize the net's but only
875ca85e948SMichael Tuexen 		 * the association. This may fail it if SACKs are not coming
876ca85e948SMichael Tuexen 		 * back. If sack's are coming with rwnd locked at 0, we will
877ca85e948SMichael Tuexen 		 * continue to hold things waiting for rwnd to raise
878ca85e948SMichael Tuexen 		 */
879ca85e948SMichael Tuexen 		if (sctp_threshold_management(inp, stcb, NULL,
880ca85e948SMichael Tuexen 		    stcb->asoc.max_send_times)) {
881ca85e948SMichael Tuexen 			/* Association was destroyed */
882ca85e948SMichael Tuexen 			return (1);
883ca85e948SMichael Tuexen 		}
884ca85e948SMichael Tuexen 	}
885ca85e948SMichael Tuexen 	if (stcb->asoc.sctp_cmt_on_off > 0) {
886ca85e948SMichael Tuexen 		if (net->pf_threshold < net->failure_threshold) {
887ca85e948SMichael Tuexen 			alt = sctp_find_alternate_net(stcb, net, 2);
888ca85e948SMichael Tuexen 		} else {
889ca85e948SMichael Tuexen 			/*
890ca85e948SMichael Tuexen 			 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is
891ca85e948SMichael Tuexen 			 * being used, then pick dest with largest ssthresh
892ca85e948SMichael Tuexen 			 * for any retransmission.
893c105859eSRandall Stewart 			 */
894b61c3588SMichael Tuexen 			alt = sctp_find_alternate_net(stcb, net, 1);
895c105859eSRandall Stewart 			/*
896c105859eSRandall Stewart 			 * CUCv2: If a different dest is picked for the
897ca85e948SMichael Tuexen 			 * retransmission, then new (rtx-)pseudo_cumack
898ca85e948SMichael Tuexen 			 * needs to be tracked for orig dest. Let CUCv2
899ca85e948SMichael Tuexen 			 * track new (rtx-) pseudo-cumack always.
900c105859eSRandall Stewart 			 */
901c105859eSRandall Stewart 			net->find_pseudo_cumack = 1;
902c105859eSRandall Stewart 			net->find_rtx_pseudo_cumack = 1;
903ca85e948SMichael Tuexen 		}
904ca85e948SMichael Tuexen 	} else {
905f8829a4aSRandall Stewart 		alt = sctp_find_alternate_net(stcb, net, 0);
906c105859eSRandall Stewart 	}
907ca85e948SMichael Tuexen 
90844fbe462SRandall Stewart 	num_mk = 0;
90944fbe462SRandall Stewart 	num_abandoned = 0;
91044fbe462SRandall Stewart 	(void)sctp_mark_all_for_resend(stcb, net, alt, win_probe,
91144fbe462SRandall Stewart 	    &num_mk, &num_abandoned);
912f8829a4aSRandall Stewart 	/* FR Loss recovery just ended with the T3. */
913f8829a4aSRandall Stewart 	stcb->asoc.fast_retran_loss_recovery = 0;
914f8829a4aSRandall Stewart 
915f8829a4aSRandall Stewart 	/* CMT FR loss recovery ended with the T3 */
916f8829a4aSRandall Stewart 	net->fast_retran_loss_recovery = 0;
917299108c5SRandall Stewart 	if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
918299108c5SRandall Stewart 	    (net->flight_size == 0)) {
919299108c5SRandall Stewart 		(*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
920299108c5SRandall Stewart 	}
9210053ed28SMichael Tuexen 
922f8829a4aSRandall Stewart 	/*
923f8829a4aSRandall Stewart 	 * setup the sat loss recovery that prevents satellite cwnd advance.
924f8829a4aSRandall Stewart 	 */
925f8829a4aSRandall Stewart 	stcb->asoc.sat_t3_loss_recovery = 1;
926f8829a4aSRandall Stewart 	stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
927f8829a4aSRandall Stewart 
928f8829a4aSRandall Stewart 	/* Backoff the timer and cwnd */
92944fbe462SRandall Stewart 	sctp_backoff_on_timeout(stcb, net, win_probe, num_mk, num_abandoned);
9309b2a35b3SMichael Tuexen 	if (((net->dest_state & SCTP_ADDR_REACHABLE) == 0) ||
931ca85e948SMichael Tuexen 	    (net->dest_state & SCTP_ADDR_PF)) {
932f8829a4aSRandall Stewart 		/* Move all pending over too */
9339eea4a2dSMichael Tuexen 		sctp_move_chunks_from_net(stcb, net);
93417205eccSRandall Stewart 
93517205eccSRandall Stewart 		/*
93617205eccSRandall Stewart 		 * Get the address that failed, to force a new src address
937e7e65008SMichael Tuexen 		 * selection and a route allocation.
93817205eccSRandall Stewart 		 */
9399b2a35b3SMichael Tuexen 		if (net->ro._s_addr != NULL) {
94017205eccSRandall Stewart 			sctp_free_ifa(net->ro._s_addr);
94117205eccSRandall Stewart 			net->ro._s_addr = NULL;
94217205eccSRandall Stewart 		}
94317205eccSRandall Stewart 		net->src_addr_selected = 0;
94417205eccSRandall Stewart 
94517205eccSRandall Stewart 		/* Force a route allocation too */
946983066f0SAlexander V. Chernikov 		RO_NHFREE(&net->ro);
9470053ed28SMichael Tuexen 
948f8829a4aSRandall Stewart 		/* Was it our primary? */
949f8829a4aSRandall Stewart 		if ((stcb->asoc.primary_destination == net) && (alt != net)) {
950f8829a4aSRandall Stewart 			/*
951f8829a4aSRandall Stewart 			 * Yes, note it as such and find an alternate note:
952f8829a4aSRandall Stewart 			 * this means HB code must use this to resent the
953f8829a4aSRandall Stewart 			 * primary if it goes active AND if someone does a
954f8829a4aSRandall Stewart 			 * change-primary then this flag must be cleared
955f8829a4aSRandall Stewart 			 * from any net structures.
956f8829a4aSRandall Stewart 			 */
9579b2a35b3SMichael Tuexen 			if (stcb->asoc.alternate != NULL) {
958ca85e948SMichael Tuexen 				sctp_free_remote_addr(stcb->asoc.alternate);
959f8829a4aSRandall Stewart 			}
960ca85e948SMichael Tuexen 			stcb->asoc.alternate = alt;
961ca85e948SMichael Tuexen 			atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
962f8829a4aSRandall Stewart 		}
963f8829a4aSRandall Stewart 	}
964f8829a4aSRandall Stewart 	/*
965f8829a4aSRandall Stewart 	 * Special case for cookie-echo'ed case, we don't do output but must
966f8829a4aSRandall Stewart 	 * await the COOKIE-ACK before retransmission
967f8829a4aSRandall Stewart 	 */
968839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
969f8829a4aSRandall Stewart 		/*
970f8829a4aSRandall Stewart 		 * Here we just reset the timer and start again since we
971f8829a4aSRandall Stewart 		 * have not established the asoc
972f8829a4aSRandall Stewart 		 */
973f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
974f8829a4aSRandall Stewart 		return (0);
975f8829a4aSRandall Stewart 	}
976dd973b0eSMichael Tuexen 	if (stcb->asoc.prsctp_supported) {
977f8829a4aSRandall Stewart 		struct sctp_tmit_chunk *lchk;
978f8829a4aSRandall Stewart 
979f8829a4aSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
980f8829a4aSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
98120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(stcb->asoc.advanced_peer_ack_point, stcb->asoc.last_acked_seq)) {
982f8829a4aSRandall Stewart 			send_forward_tsn(stcb, &stcb->asoc);
983efd5e692SMichael Tuexen 			for (; lchk != NULL; lchk = TAILQ_NEXT(lchk, sctp_next)) {
984efd5e692SMichael Tuexen 				if (lchk->whoTo != NULL) {
985efd5e692SMichael Tuexen 					break;
986efd5e692SMichael Tuexen 				}
987efd5e692SMichael Tuexen 			}
988efd5e692SMichael Tuexen 			if (lchk != NULL) {
989f8829a4aSRandall Stewart 				/* Assure a timer is up */
990f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
991f8829a4aSRandall Stewart 			}
992f8829a4aSRandall Stewart 		}
993f8829a4aSRandall Stewart 	}
994b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
995f8829a4aSRandall Stewart 		sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
99680fefe0aSRandall Stewart 	}
997f8829a4aSRandall Stewart 	return (0);
998f8829a4aSRandall Stewart }
999f8829a4aSRandall Stewart 
1000f8829a4aSRandall Stewart int
sctp_t1init_timer(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net)1001f8829a4aSRandall Stewart sctp_t1init_timer(struct sctp_inpcb *inp,
1002f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
1003f8829a4aSRandall Stewart     struct sctp_nets *net)
1004f8829a4aSRandall Stewart {
1005f8829a4aSRandall Stewart 	/* bump the thresholds */
1006f8829a4aSRandall Stewart 	if (stcb->asoc.delayed_connection) {
1007f8829a4aSRandall Stewart 		/*
1008f8829a4aSRandall Stewart 		 * special hook for delayed connection. The library did NOT
1009f8829a4aSRandall Stewart 		 * complete the rest of its sends.
1010f8829a4aSRandall Stewart 		 */
1011f8829a4aSRandall Stewart 		stcb->asoc.delayed_connection = 0;
1012ceaad40aSRandall Stewart 		sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1013f8829a4aSRandall Stewart 		return (0);
1014f8829a4aSRandall Stewart 	}
1015839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) {
1016f8829a4aSRandall Stewart 		return (0);
1017f8829a4aSRandall Stewart 	}
1018f8829a4aSRandall Stewart 	if (sctp_threshold_management(inp, stcb, net,
1019f8829a4aSRandall Stewart 	    stcb->asoc.max_init_times)) {
1020f8829a4aSRandall Stewart 		/* Association was destroyed */
1021f8829a4aSRandall Stewart 		return (1);
1022f8829a4aSRandall Stewart 	}
1023f8829a4aSRandall Stewart 	stcb->asoc.dropped_special_cnt = 0;
102444fbe462SRandall Stewart 	sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0, 0);
1025f8829a4aSRandall Stewart 	if (stcb->asoc.initial_init_rto_max < net->RTO) {
1026f8829a4aSRandall Stewart 		net->RTO = stcb->asoc.initial_init_rto_max;
1027f8829a4aSRandall Stewart 	}
1028f8829a4aSRandall Stewart 	if (stcb->asoc.numnets > 1) {
1029f8829a4aSRandall Stewart 		/* If we have more than one addr use it */
1030f8829a4aSRandall Stewart 		struct sctp_nets *alt;
1031f8829a4aSRandall Stewart 
1032f8829a4aSRandall Stewart 		alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1033b61c3588SMichael Tuexen 		if (alt != stcb->asoc.primary_destination) {
10349eea4a2dSMichael Tuexen 			sctp_move_chunks_from_net(stcb, stcb->asoc.primary_destination);
1035f8829a4aSRandall Stewart 			stcb->asoc.primary_destination = alt;
1036f8829a4aSRandall Stewart 		}
1037f8829a4aSRandall Stewart 	}
1038f8829a4aSRandall Stewart 	/* Send out a new init */
1039ceaad40aSRandall Stewart 	sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1040f8829a4aSRandall Stewart 	return (0);
1041f8829a4aSRandall Stewart }
1042f8829a4aSRandall Stewart 
1043f8829a4aSRandall Stewart /*
1044f8829a4aSRandall Stewart  * For cookie and asconf we actually need to find and mark for resend, then
1045f8829a4aSRandall Stewart  * increment the resend counter (after all the threshold management stuff of
1046f8829a4aSRandall Stewart  * course).
1047f8829a4aSRandall Stewart  */
1048f8829a4aSRandall Stewart int
sctp_cookie_timer(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net SCTP_UNUSED)1049f8829a4aSRandall Stewart sctp_cookie_timer(struct sctp_inpcb *inp,
1050f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
10517215cc1bSMichael Tuexen     struct sctp_nets *net SCTP_UNUSED)
1052f8829a4aSRandall Stewart {
1053f8829a4aSRandall Stewart 	struct sctp_nets *alt;
1054f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *cookie;
1055f8829a4aSRandall Stewart 
1056f8829a4aSRandall Stewart 	/* first before all else we must find the cookie */
1057f8829a4aSRandall Stewart 	TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1058f8829a4aSRandall Stewart 		if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1059f8829a4aSRandall Stewart 			break;
1060f8829a4aSRandall Stewart 		}
1061f8829a4aSRandall Stewart 	}
1062f8829a4aSRandall Stewart 	if (cookie == NULL) {
1063839d21d6SMichael Tuexen 		if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
1064f8829a4aSRandall Stewart 			/* FOOBAR! */
1065ff1ffd74SMichael Tuexen 			struct mbuf *op_err;
1066f8829a4aSRandall Stewart 
1067267dbe63SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
1068ff1ffd74SMichael Tuexen 			    "Cookie timer expired, but no cookie");
1069b7d130beSMichael Tuexen 			inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_3;
1070105b68b4SMichael Tuexen 			sctp_abort_an_association(inp, stcb, op_err, false, SCTP_SO_NOT_LOCKED);
1071f8829a4aSRandall Stewart 		} else {
1072a5d547adSRandall Stewart #ifdef INVARIANTS
1073f8829a4aSRandall Stewart 			panic("Cookie timer expires in wrong state?");
1074f8829a4aSRandall Stewart #else
1075839d21d6SMichael Tuexen 			SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(stcb));
1076f8829a4aSRandall Stewart 			return (0);
1077f8829a4aSRandall Stewart #endif
1078f8829a4aSRandall Stewart 		}
1079f8829a4aSRandall Stewart 		return (0);
1080f8829a4aSRandall Stewart 	}
1081f8829a4aSRandall Stewart 	/* Ok we found the cookie, threshold management next */
1082f8829a4aSRandall Stewart 	if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1083f8829a4aSRandall Stewart 	    stcb->asoc.max_init_times)) {
1084f8829a4aSRandall Stewart 		/* Assoc is over */
1085f8829a4aSRandall Stewart 		return (1);
1086f8829a4aSRandall Stewart 	}
1087f8829a4aSRandall Stewart 	/*
1088a807fe2dSMichael Tuexen 	 * Cleared threshold management, now lets backoff the address and
1089a807fe2dSMichael Tuexen 	 * select an alternate
1090f8829a4aSRandall Stewart 	 */
1091f8829a4aSRandall Stewart 	stcb->asoc.dropped_special_cnt = 0;
109244fbe462SRandall Stewart 	sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0, 0);
1093f8829a4aSRandall Stewart 	alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1094f8829a4aSRandall Stewart 	if (alt != cookie->whoTo) {
1095f8829a4aSRandall Stewart 		sctp_free_remote_addr(cookie->whoTo);
1096f8829a4aSRandall Stewart 		cookie->whoTo = alt;
1097f8829a4aSRandall Stewart 		atomic_add_int(&alt->ref_count, 1);
1098f8829a4aSRandall Stewart 	}
1099f8829a4aSRandall Stewart 	/* Now mark the retran info */
1100f8829a4aSRandall Stewart 	if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1101f8829a4aSRandall Stewart 		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1102f8829a4aSRandall Stewart 	}
1103f8829a4aSRandall Stewart 	cookie->sent = SCTP_DATAGRAM_RESEND;
11044f43a14aSMichael Tuexen 	cookie->flags |= CHUNK_FLAGS_FRAGMENT_OK;
1105f8829a4aSRandall Stewart 	/*
1106f8829a4aSRandall Stewart 	 * Now call the output routine to kick out the cookie again, Note we
1107f8829a4aSRandall Stewart 	 * don't mark any chunks for retran so that FR will need to kick in
1108f8829a4aSRandall Stewart 	 * to move these (or a send timer).
1109f8829a4aSRandall Stewart 	 */
1110f8829a4aSRandall Stewart 	return (0);
1111f8829a4aSRandall Stewart }
1112f8829a4aSRandall Stewart 
1113f8829a4aSRandall Stewart int
sctp_strreset_timer(struct sctp_inpcb * inp,struct sctp_tcb * stcb)1114e95b3d7fSMichael Tuexen sctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1115f8829a4aSRandall Stewart {
1116e95b3d7fSMichael Tuexen 	struct sctp_nets *alt, *net;
1117f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1118f8829a4aSRandall Stewart 
1119f8829a4aSRandall Stewart 	if (stcb->asoc.stream_reset_outstanding == 0) {
1120f8829a4aSRandall Stewart 		return (0);
1121f8829a4aSRandall Stewart 	}
1122f8829a4aSRandall Stewart 	/* find the existing STRRESET, we use the seq number we sent out on */
1123ad81507eSRandall Stewart 	(void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1124f8829a4aSRandall Stewart 	if (strrst == NULL) {
1125f8829a4aSRandall Stewart 		return (0);
1126f8829a4aSRandall Stewart 	}
1127e95b3d7fSMichael Tuexen 	net = strrst->whoTo;
1128f8829a4aSRandall Stewart 	/* do threshold management */
1129e95b3d7fSMichael Tuexen 	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1130f8829a4aSRandall Stewart 		/* Assoc is over */
1131f8829a4aSRandall Stewart 		return (1);
1132f8829a4aSRandall Stewart 	}
1133f8829a4aSRandall Stewart 	/*
1134a807fe2dSMichael Tuexen 	 * Cleared threshold management, now lets backoff the address and
1135a807fe2dSMichael Tuexen 	 * select an alternate
1136f8829a4aSRandall Stewart 	 */
1137e95b3d7fSMichael Tuexen 	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1138e95b3d7fSMichael Tuexen 	alt = sctp_find_alternate_net(stcb, net, 0);
1139f8829a4aSRandall Stewart 	strrst->whoTo = alt;
1140f8829a4aSRandall Stewart 	atomic_add_int(&alt->ref_count, 1);
1141f8829a4aSRandall Stewart 
1142f8829a4aSRandall Stewart 	/* See if a ECN Echo is also stranded */
1143f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1144f8829a4aSRandall Stewart 		if ((chk->whoTo == net) &&
1145f8829a4aSRandall Stewart 		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1146f8829a4aSRandall Stewart 			sctp_free_remote_addr(chk->whoTo);
1147f8829a4aSRandall Stewart 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
1148f8829a4aSRandall Stewart 				chk->sent = SCTP_DATAGRAM_RESEND;
11494f43a14aSMichael Tuexen 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
1150f8829a4aSRandall Stewart 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1151f8829a4aSRandall Stewart 			}
1152f8829a4aSRandall Stewart 			chk->whoTo = alt;
1153f8829a4aSRandall Stewart 			atomic_add_int(&alt->ref_count, 1);
1154f8829a4aSRandall Stewart 		}
1155f8829a4aSRandall Stewart 	}
11569b2a35b3SMichael Tuexen 	if ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) {
1157f8829a4aSRandall Stewart 		/*
1158f8829a4aSRandall Stewart 		 * If the address went un-reachable, we need to move to
1159f8829a4aSRandall Stewart 		 * alternates for ALL chk's in queue
1160f8829a4aSRandall Stewart 		 */
11619eea4a2dSMichael Tuexen 		sctp_move_chunks_from_net(stcb, net);
1162f8829a4aSRandall Stewart 	}
1163e95b3d7fSMichael Tuexen 	sctp_free_remote_addr(net);
1164e95b3d7fSMichael Tuexen 
1165f8829a4aSRandall Stewart 	/* mark the retran info */
1166f8829a4aSRandall Stewart 	if (strrst->sent != SCTP_DATAGRAM_RESEND)
1167f8829a4aSRandall Stewart 		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1168f8829a4aSRandall Stewart 	strrst->sent = SCTP_DATAGRAM_RESEND;
11694f43a14aSMichael Tuexen 	strrst->flags |= CHUNK_FLAGS_FRAGMENT_OK;
1170f8829a4aSRandall Stewart 
1171f8829a4aSRandall Stewart 	/* restart the timer */
1172e95b3d7fSMichael Tuexen 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, alt);
1173f8829a4aSRandall Stewart 	return (0);
1174f8829a4aSRandall Stewart }
1175f8829a4aSRandall Stewart 
1176f8829a4aSRandall Stewart int
sctp_asconf_timer(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net)1177f8829a4aSRandall Stewart sctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1178f8829a4aSRandall Stewart     struct sctp_nets *net)
1179f8829a4aSRandall Stewart {
1180f8829a4aSRandall Stewart 	struct sctp_nets *alt;
11814a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *asconf, *chk;
1182f8829a4aSRandall Stewart 
11831b649582SRandall Stewart 	/* is this a first send, or a retransmission? */
1184c54a18d2SRandall Stewart 	if (TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) {
1185f8829a4aSRandall Stewart 		/* compose a new ASCONF chunk and send it */
11863232788eSRandall Stewart 		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1187f8829a4aSRandall Stewart 	} else {
11881b649582SRandall Stewart 		/*
11891b649582SRandall Stewart 		 * Retransmission of the existing ASCONF is needed
11901b649582SRandall Stewart 		 */
1191f8829a4aSRandall Stewart 
1192f8829a4aSRandall Stewart 		/* find the existing ASCONF */
1193c54a18d2SRandall Stewart 		asconf = TAILQ_FIRST(&stcb->asoc.asconf_send_queue);
1194f8829a4aSRandall Stewart 		if (asconf == NULL) {
1195f8829a4aSRandall Stewart 			return (0);
1196f8829a4aSRandall Stewart 		}
1197e95b3d7fSMichael Tuexen 		net = asconf->whoTo;
1198f8829a4aSRandall Stewart 		/* do threshold management */
1199e95b3d7fSMichael Tuexen 		if (sctp_threshold_management(inp, stcb, net,
1200f8829a4aSRandall Stewart 		    stcb->asoc.max_send_times)) {
1201f8829a4aSRandall Stewart 			/* Assoc is over */
1202f8829a4aSRandall Stewart 			return (1);
1203f8829a4aSRandall Stewart 		}
1204f8829a4aSRandall Stewart 		if (asconf->snd_count > stcb->asoc.max_send_times) {
1205f8829a4aSRandall Stewart 			/*
12061b649582SRandall Stewart 			 * Something is rotten: our peer is not responding
12071b649582SRandall Stewart 			 * to ASCONFs but apparently is to other chunks.
12081b649582SRandall Stewart 			 * i.e. it is not properly handling the chunk type
12091b649582SRandall Stewart 			 * upper bits. Mark this peer as ASCONF incapable
12101b649582SRandall Stewart 			 * and cleanup.
1211f8829a4aSRandall Stewart 			 */
1212ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
12136fb7b4fbSMichael Tuexen 			sctp_asconf_cleanup(stcb);
1214f8829a4aSRandall Stewart 			return (0);
1215f8829a4aSRandall Stewart 		}
1216f8829a4aSRandall Stewart 		/*
12171b649582SRandall Stewart 		 * cleared threshold management, so now backoff the net and
12181b649582SRandall Stewart 		 * select an alternate
1219f8829a4aSRandall Stewart 		 */
1220e95b3d7fSMichael Tuexen 		sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1221e95b3d7fSMichael Tuexen 		alt = sctp_find_alternate_net(stcb, net, 0);
1222c54a18d2SRandall Stewart 		if (asconf->whoTo != alt) {
1223f8829a4aSRandall Stewart 			asconf->whoTo = alt;
1224f8829a4aSRandall Stewart 			atomic_add_int(&alt->ref_count, 1);
1225c54a18d2SRandall Stewart 		}
12260053ed28SMichael Tuexen 
12271b649582SRandall Stewart 		/* See if an ECN Echo is also stranded */
1228f8829a4aSRandall Stewart 		TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1229f8829a4aSRandall Stewart 			if ((chk->whoTo == net) &&
1230f8829a4aSRandall Stewart 			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1231f8829a4aSRandall Stewart 				sctp_free_remote_addr(chk->whoTo);
1232f8829a4aSRandall Stewart 				chk->whoTo = alt;
1233f8829a4aSRandall Stewart 				if (chk->sent != SCTP_DATAGRAM_RESEND) {
1234f8829a4aSRandall Stewart 					chk->sent = SCTP_DATAGRAM_RESEND;
12354f43a14aSMichael Tuexen 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
1236f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1237f8829a4aSRandall Stewart 				}
1238f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
1239f8829a4aSRandall Stewart 			}
1240f8829a4aSRandall Stewart 		}
12414a9ef3f8SMichael Tuexen 		TAILQ_FOREACH(chk, &stcb->asoc.asconf_send_queue, sctp_next) {
1242c54a18d2SRandall Stewart 			if (chk->whoTo != alt) {
1243c54a18d2SRandall Stewart 				sctp_free_remote_addr(chk->whoTo);
1244c54a18d2SRandall Stewart 				chk->whoTo = alt;
1245c54a18d2SRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
1246c54a18d2SRandall Stewart 			}
1247c54a18d2SRandall Stewart 			if (asconf->sent != SCTP_DATAGRAM_RESEND && chk->sent != SCTP_DATAGRAM_UNSENT)
1248c54a18d2SRandall Stewart 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1249c54a18d2SRandall Stewart 			chk->sent = SCTP_DATAGRAM_RESEND;
12504f43a14aSMichael Tuexen 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
1251c54a18d2SRandall Stewart 		}
12529b2a35b3SMichael Tuexen 		if ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) {
1253f8829a4aSRandall Stewart 			/*
1254f8829a4aSRandall Stewart 			 * If the address went un-reachable, we need to move
12551b649582SRandall Stewart 			 * to the alternate for ALL chunks in queue
1256f8829a4aSRandall Stewart 			 */
12579eea4a2dSMichael Tuexen 			sctp_move_chunks_from_net(stcb, net);
1258f8829a4aSRandall Stewart 		}
1259e95b3d7fSMichael Tuexen 		sctp_free_remote_addr(net);
1260e95b3d7fSMichael Tuexen 
1261f8829a4aSRandall Stewart 		/* mark the retran info */
1262f8829a4aSRandall Stewart 		if (asconf->sent != SCTP_DATAGRAM_RESEND)
1263f8829a4aSRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1264f8829a4aSRandall Stewart 		asconf->sent = SCTP_DATAGRAM_RESEND;
12654f43a14aSMichael Tuexen 		asconf->flags |= CHUNK_FLAGS_FRAGMENT_OK;
1266c54a18d2SRandall Stewart 
1267c54a18d2SRandall Stewart 		/* send another ASCONF if any and we can do */
1268c54a18d2SRandall Stewart 		sctp_send_asconf(stcb, alt, SCTP_ADDR_NOT_LOCKED);
1269f8829a4aSRandall Stewart 	}
1270f8829a4aSRandall Stewart 	return (0);
1271f8829a4aSRandall Stewart }
1272f8829a4aSRandall Stewart 
1273851b7298SRandall Stewart /* Mobility adaptation */
127404ee05e8SRandall Stewart void
sctp_delete_prim_timer(struct sctp_inpcb * inp,struct sctp_tcb * stcb)1275a57fb68bSMichael Tuexen sctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1276851b7298SRandall Stewart {
1277851b7298SRandall Stewart 	if (stcb->asoc.deleted_primary == NULL) {
1278851b7298SRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n");
1279851b7298SRandall Stewart 		sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
128004ee05e8SRandall Stewart 		return;
1281851b7298SRandall Stewart 	}
1282851b7298SRandall Stewart 	SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary ");
1283851b7298SRandall Stewart 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1284851b7298SRandall Stewart 	sctp_free_remote_addr(stcb->asoc.deleted_primary);
1285851b7298SRandall Stewart 	stcb->asoc.deleted_primary = NULL;
1286851b7298SRandall Stewart 	sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
128704ee05e8SRandall Stewart 	return;
1288851b7298SRandall Stewart }
1289851b7298SRandall Stewart 
1290f8829a4aSRandall Stewart /*
1291f8829a4aSRandall Stewart  * For the shutdown and shutdown-ack, we do not keep one around on the
1292f8829a4aSRandall Stewart  * control queue. This means we must generate a new one and call the general
1293f8829a4aSRandall Stewart  * chunk output routine, AFTER having done threshold management.
1294b61c3588SMichael Tuexen  * It is assumed that net is non-NULL.
1295f8829a4aSRandall Stewart  */
1296f8829a4aSRandall Stewart int
sctp_shutdown_timer(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net)1297f8829a4aSRandall Stewart sctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1298f8829a4aSRandall Stewart     struct sctp_nets *net)
1299f8829a4aSRandall Stewart {
1300f8829a4aSRandall Stewart 	struct sctp_nets *alt;
1301f8829a4aSRandall Stewart 
1302cd0a4ff6SPedro F. Giffuni 	/* first threshold management */
1303f8829a4aSRandall Stewart 	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1304f8829a4aSRandall Stewart 		/* Assoc is over */
1305f8829a4aSRandall Stewart 		return (1);
1306f8829a4aSRandall Stewart 	}
1307b61c3588SMichael Tuexen 	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1308f8829a4aSRandall Stewart 	/* second select an alternative */
1309f8829a4aSRandall Stewart 	alt = sctp_find_alternate_net(stcb, net, 0);
1310f8829a4aSRandall Stewart 
1311f8829a4aSRandall Stewart 	/* third generate a shutdown into the queue for out net */
1312f8829a4aSRandall Stewart 	sctp_send_shutdown(stcb, alt);
1313b61c3588SMichael Tuexen 
1314f8829a4aSRandall Stewart 	/* fourth restart timer */
1315f8829a4aSRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1316f8829a4aSRandall Stewart 	return (0);
1317f8829a4aSRandall Stewart }
1318f8829a4aSRandall Stewart 
1319f8829a4aSRandall Stewart int
sctp_shutdownack_timer(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net)1320f8829a4aSRandall Stewart sctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1321f8829a4aSRandall Stewart     struct sctp_nets *net)
1322f8829a4aSRandall Stewart {
1323f8829a4aSRandall Stewart 	struct sctp_nets *alt;
1324f8829a4aSRandall Stewart 
1325cd0a4ff6SPedro F. Giffuni 	/* first threshold management */
1326f8829a4aSRandall Stewart 	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1327f8829a4aSRandall Stewart 		/* Assoc is over */
1328f8829a4aSRandall Stewart 		return (1);
1329f8829a4aSRandall Stewart 	}
1330b61c3588SMichael Tuexen 	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1331f8829a4aSRandall Stewart 	/* second select an alternative */
1332f8829a4aSRandall Stewart 	alt = sctp_find_alternate_net(stcb, net, 0);
1333f8829a4aSRandall Stewart 
1334f8829a4aSRandall Stewart 	/* third generate a shutdown into the queue for out net */
1335f8829a4aSRandall Stewart 	sctp_send_shutdown_ack(stcb, alt);
1336f8829a4aSRandall Stewart 
1337f8829a4aSRandall Stewart 	/* fourth restart timer */
1338f8829a4aSRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1339f8829a4aSRandall Stewart 	return (0);
1340f8829a4aSRandall Stewart }
1341f8829a4aSRandall Stewart 
1342f8829a4aSRandall Stewart static void
sctp_audit_stream_queues_for_size(struct sctp_inpcb * inp,struct sctp_tcb * stcb)13430b79a76fSMichael Tuexen sctp_audit_stream_queues_for_size(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1344f8829a4aSRandall Stewart {
1345f8829a4aSRandall Stewart 	struct sctp_stream_queue_pending *sp;
1346f7a77f6fSMichael Tuexen 	unsigned int i, chks_in_queue = 0;
1347f8829a4aSRandall Stewart 	int being_filled = 0;
1348f8829a4aSRandall Stewart 
13490b79a76fSMichael Tuexen 	KASSERT(inp != NULL, ("inp is NULL"));
13500b79a76fSMichael Tuexen 	KASSERT(stcb != NULL, ("stcb is NULL"));
13515ac91821SMichael Tuexen 	SCTP_TCB_LOCK_ASSERT(stcb);
13520b79a76fSMichael Tuexen 	KASSERT(TAILQ_EMPTY(&stcb->asoc.send_queue), ("send_queue not empty"));
13530b79a76fSMichael Tuexen 	KASSERT(TAILQ_EMPTY(&stcb->asoc.sent_queue), ("sent_queue not empty"));
1354f8829a4aSRandall Stewart 
1355f8829a4aSRandall Stewart 	if (stcb->asoc.sent_queue_retran_cnt) {
1356ad81507eSRandall Stewart 		SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1357f8829a4aSRandall Stewart 		    stcb->asoc.sent_queue_retran_cnt);
1358f8829a4aSRandall Stewart 		stcb->asoc.sent_queue_retran_cnt = 0;
1359f8829a4aSRandall Stewart 	}
1360f7a77f6fSMichael Tuexen 	if (stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1361252f7f93SMichael Tuexen 		/* No stream scheduler information, initialize scheduler */
1362762ae0ecSMichael Tuexen 		stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc);
1363252f7f93SMichael Tuexen 		if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1364252f7f93SMichael Tuexen 			/* yep, we lost a stream or two */
1365252f7f93SMichael Tuexen 			SCTP_PRINTF("Found additional streams NOT managed by scheduler, corrected\n");
1366f8829a4aSRandall Stewart 		} else {
1367252f7f93SMichael Tuexen 			/* no streams lost */
1368f8829a4aSRandall Stewart 			stcb->asoc.total_output_queue_size = 0;
1369f8829a4aSRandall Stewart 		}
1370f8829a4aSRandall Stewart 	}
1371f8829a4aSRandall Stewart 	/* Check to see if some data queued, if so report it */
1372f7a77f6fSMichael Tuexen 	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1373f7a77f6fSMichael Tuexen 		if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1374f7a77f6fSMichael Tuexen 			TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
1375f8829a4aSRandall Stewart 				if (sp->msg_is_complete)
1376f8829a4aSRandall Stewart 					being_filled++;
1377f8829a4aSRandall Stewart 				chks_in_queue++;
1378f8829a4aSRandall Stewart 			}
1379f8829a4aSRandall Stewart 		}
1380f8829a4aSRandall Stewart 	}
1381f8829a4aSRandall Stewart 	if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1382ad81507eSRandall Stewart 		SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1383f8829a4aSRandall Stewart 		    stcb->asoc.stream_queue_cnt, chks_in_queue);
1384f8829a4aSRandall Stewart 	}
1385f8829a4aSRandall Stewart 	if (chks_in_queue) {
1386f8829a4aSRandall Stewart 		/* call the output queue function */
1387ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1388f8829a4aSRandall Stewart 		if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1389f8829a4aSRandall Stewart 		    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1390f8829a4aSRandall Stewart 			/*
1391f8829a4aSRandall Stewart 			 * Probably should go in and make it go back through
1392f8829a4aSRandall Stewart 			 * and add fragments allowed
1393f8829a4aSRandall Stewart 			 */
1394f8829a4aSRandall Stewart 			if (being_filled == 0) {
1395ad81507eSRandall Stewart 				SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
1396f8829a4aSRandall Stewart 				    chks_in_queue);
1397f8829a4aSRandall Stewart 			}
1398f8829a4aSRandall Stewart 		}
1399f8829a4aSRandall Stewart 	} else {
1400ad81507eSRandall Stewart 		SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
1401f8829a4aSRandall Stewart 		    (u_long)stcb->asoc.total_output_queue_size);
1402f8829a4aSRandall Stewart 		stcb->asoc.total_output_queue_size = 0;
1403f8829a4aSRandall Stewart 	}
1404f8829a4aSRandall Stewart }
1405f8829a4aSRandall Stewart 
1406f8829a4aSRandall Stewart int
sctp_heartbeat_timer(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net)1407f8829a4aSRandall Stewart sctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1408ca85e948SMichael Tuexen     struct sctp_nets *net)
1409f8829a4aSRandall Stewart {
14109b2a35b3SMichael Tuexen 	bool net_was_pf;
1411b54d3a6cSRandall Stewart 
14129b2a35b3SMichael Tuexen 	net_was_pf = (net->dest_state & SCTP_ADDR_PF) != 0;
1413f8829a4aSRandall Stewart 	if (net->hb_responded == 0) {
14149b2a35b3SMichael Tuexen 		if (net->ro._s_addr != NULL) {
141542551e99SRandall Stewart 			/*
141660990c0cSMichael Tuexen 			 * Invalidate the src address if we did not get a
141760990c0cSMichael Tuexen 			 * response last time.
141842551e99SRandall Stewart 			 */
141942551e99SRandall Stewart 			sctp_free_ifa(net->ro._s_addr);
142042551e99SRandall Stewart 			net->ro._s_addr = NULL;
142142551e99SRandall Stewart 			net->src_addr_selected = 0;
142242551e99SRandall Stewart 		}
142344fbe462SRandall Stewart 		sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1424ca85e948SMichael Tuexen 		if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1425ca85e948SMichael Tuexen 			/* Assoc is over */
1426ca85e948SMichael Tuexen 			return (1);
1427ca85e948SMichael Tuexen 		}
1428f8829a4aSRandall Stewart 	}
1429f8829a4aSRandall Stewart 	/* Zero PBA, if it needs it */
14309b2a35b3SMichael Tuexen 	if (net->partial_bytes_acked > 0) {
1431f8829a4aSRandall Stewart 		net->partial_bytes_acked = 0;
1432f8829a4aSRandall Stewart 	}
1433f8829a4aSRandall Stewart 	if ((stcb->asoc.total_output_queue_size > 0) &&
1434f8829a4aSRandall Stewart 	    (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1435f8829a4aSRandall Stewart 	    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1436f8829a4aSRandall Stewart 		sctp_audit_stream_queues_for_size(inp, stcb);
1437f8829a4aSRandall Stewart 	}
1438aab6e5bdSMichael Tuexen 	if ((((net->dest_state & SCTP_ADDR_NOHB) == 0) ||
1439aab6e5bdSMichael Tuexen 	    (net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
14409b2a35b3SMichael Tuexen 	    (net_was_pf || ((net->dest_state & SCTP_ADDR_PF) == 0))) {
1441f8829a4aSRandall Stewart 		/*
14429b2a35b3SMichael Tuexen 		 * When moving to PF during threshold management, a HB has
14439b2a35b3SMichael Tuexen 		 * been queued in that routine.
1444f8829a4aSRandall Stewart 		 */
1445ca7567c9SMichael Tuexen 		uint32_t ms_gone_by;
1446ca7567c9SMichael Tuexen 
1447ca7567c9SMichael Tuexen 		if ((net->last_sent_time.tv_sec > 0) ||
1448ca7567c9SMichael Tuexen 		    (net->last_sent_time.tv_usec > 0)) {
1449ca7567c9SMichael Tuexen 			struct timeval diff;
1450ca7567c9SMichael Tuexen 
1451ca7567c9SMichael Tuexen 			SCTP_GETTIME_TIMEVAL(&diff);
1452ca7567c9SMichael Tuexen 			timevalsub(&diff, &net->last_sent_time);
1453ca7567c9SMichael Tuexen 			ms_gone_by = (uint32_t)(diff.tv_sec * 1000) +
1454ca7567c9SMichael Tuexen 			    (uint32_t)(diff.tv_usec / 1000);
1455ca7567c9SMichael Tuexen 		} else {
1456ca7567c9SMichael Tuexen 			ms_gone_by = 0xffffffff;
1457ca7567c9SMichael Tuexen 		}
14584dca0ef4SMichael Tuexen 		if ((ms_gone_by >= net->heart_beat_delay) ||
1459aab6e5bdSMichael Tuexen 		    (net->dest_state & SCTP_ADDR_UNCONFIRMED) ||
14604dca0ef4SMichael Tuexen 		    (net->dest_state & SCTP_ADDR_PF)) {
1461ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
1462f8829a4aSRandall Stewart 		}
1463ca7567c9SMichael Tuexen 	}
1464f8829a4aSRandall Stewart 	return (0);
1465f8829a4aSRandall Stewart }
1466f8829a4aSRandall Stewart 
1467f8829a4aSRandall Stewart void
sctp_pathmtu_timer(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net)1468f8829a4aSRandall Stewart sctp_pathmtu_timer(struct sctp_inpcb *inp,
1469f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
1470f8829a4aSRandall Stewart     struct sctp_nets *net)
1471f8829a4aSRandall Stewart {
1472c54a18d2SRandall Stewart 	uint32_t next_mtu, mtu;
1473f8829a4aSRandall Stewart 
14747215cc1bSMichael Tuexen 	next_mtu = sctp_get_next_mtu(net->mtu);
147517205eccSRandall Stewart 
1476c54a18d2SRandall Stewart 	if ((next_mtu > net->mtu) && (net->port == 0)) {
147717205eccSRandall Stewart 		if ((net->src_addr_selected == 0) ||
147817205eccSRandall Stewart 		    (net->ro._s_addr == NULL) ||
147917205eccSRandall Stewart 		    (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1480ad81507eSRandall Stewart 			if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
148117205eccSRandall Stewart 				sctp_free_ifa(net->ro._s_addr);
148217205eccSRandall Stewart 				net->ro._s_addr = NULL;
148317205eccSRandall Stewart 				net->src_addr_selected = 0;
1484ad81507eSRandall Stewart 			} else if (net->ro._s_addr == NULL) {
1485c54a18d2SRandall Stewart #if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1486c54a18d2SRandall Stewart 				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1487c54a18d2SRandall Stewart 					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1488c54a18d2SRandall Stewart 
1489c54a18d2SRandall Stewart 					/* KAME hack: embed scopeid */
1490482444b4SRandall Stewart 					(void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
1491c54a18d2SRandall Stewart 				}
1492c54a18d2SRandall Stewart #endif
1493c54a18d2SRandall Stewart 
149417205eccSRandall Stewart 				net->ro._s_addr = sctp_source_address_selection(inp,
149517205eccSRandall Stewart 				    stcb,
149617205eccSRandall Stewart 				    (sctp_route_t *)&net->ro,
149717205eccSRandall Stewart 				    net, 0, stcb->asoc.vrf_id);
1498c54a18d2SRandall Stewart #if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1499c54a18d2SRandall Stewart 				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1500c54a18d2SRandall Stewart 					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1501c54a18d2SRandall Stewart 
1502c54a18d2SRandall Stewart 					(void)sa6_recoverscope(sin6);
1503c54a18d2SRandall Stewart 				}
1504c54a18d2SRandall Stewart #endif				/* INET6 */
1505ad81507eSRandall Stewart 			}
150617205eccSRandall Stewart 			if (net->ro._s_addr)
150717205eccSRandall Stewart 				net->src_addr_selected = 1;
150817205eccSRandall Stewart 		}
150917205eccSRandall Stewart 		if (net->ro._s_addr) {
1510983066f0SAlexander V. Chernikov 			mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_nh);
15114474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
1512830d754dSRandall Stewart 			if (net->port) {
1513830d754dSRandall Stewart 				mtu -= sizeof(struct udphdr);
1514830d754dSRandall Stewart 			}
15154474d71aSMichael Tuexen #endif
151617205eccSRandall Stewart 			if (mtu > next_mtu) {
1517f8829a4aSRandall Stewart 				net->mtu = next_mtu;
1518faadc1b4SMichael Tuexen 			} else {
1519faadc1b4SMichael Tuexen 				net->mtu = mtu;
1520f8829a4aSRandall Stewart 			}
1521f8829a4aSRandall Stewart 		}
1522f8829a4aSRandall Stewart 	}
1523f8829a4aSRandall Stewart 	/* restart the timer */
1524f8829a4aSRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1525f8829a4aSRandall Stewart }
1526f8829a4aSRandall Stewart 
1527f8829a4aSRandall Stewart void
sctp_autoclose_timer(struct sctp_inpcb * inp,struct sctp_tcb * stcb)1528a57fb68bSMichael Tuexen sctp_autoclose_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1529f8829a4aSRandall Stewart {
1530f8829a4aSRandall Stewart 	struct timeval tn, *tim_touse;
1531f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1532d5d190f2SMichael Tuexen 	uint32_t ticks_gone_by;
1533f8829a4aSRandall Stewart 
15346e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&tn);
1535d5d190f2SMichael Tuexen 	if (stcb->asoc.sctp_autoclose_ticks > 0 &&
1536f8829a4aSRandall Stewart 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1537f8829a4aSRandall Stewart 		/* Auto close is on */
1538f8829a4aSRandall Stewart 		asoc = &stcb->asoc;
1539f8829a4aSRandall Stewart 		/* pick the time to use */
1540f8829a4aSRandall Stewart 		if (asoc->time_last_rcvd.tv_sec >
1541f8829a4aSRandall Stewart 		    asoc->time_last_sent.tv_sec) {
1542f8829a4aSRandall Stewart 			tim_touse = &asoc->time_last_rcvd;
1543f8829a4aSRandall Stewart 		} else {
1544f8829a4aSRandall Stewart 			tim_touse = &asoc->time_last_sent;
1545f8829a4aSRandall Stewart 		}
1546f8829a4aSRandall Stewart 		/* Now has long enough transpired to autoclose? */
154725ec3553SMichael Tuexen 		ticks_gone_by = sctp_secs_to_ticks((uint32_t)(tn.tv_sec - tim_touse->tv_sec));
1548d5d190f2SMichael Tuexen 		if (ticks_gone_by >= asoc->sctp_autoclose_ticks) {
1549f8829a4aSRandall Stewart 			/*
1550f8829a4aSRandall Stewart 			 * autoclose time has hit, call the output routine,
1551f8829a4aSRandall Stewart 			 * which should do nothing just to be SURE we don't
1552f8829a4aSRandall Stewart 			 * have hanging data. We can then safely check the
1553f8829a4aSRandall Stewart 			 * queues and know that we are clear to send
1554f8829a4aSRandall Stewart 			 * shutdown
1555f8829a4aSRandall Stewart 			 */
1556ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1557f8829a4aSRandall Stewart 			/* Are we clean? */
1558f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue) &&
1559f8829a4aSRandall Stewart 			    TAILQ_EMPTY(&asoc->sent_queue)) {
1560f8829a4aSRandall Stewart 				/*
1561f8829a4aSRandall Stewart 				 * there is nothing queued to send, so I'm
1562f8829a4aSRandall Stewart 				 * done...
1563f8829a4aSRandall Stewart 				 */
1564839d21d6SMichael Tuexen 				if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1565f8829a4aSRandall Stewart 					/* only send SHUTDOWN 1st time thru */
1566a57fb68bSMichael Tuexen 					struct sctp_nets *net;
1567ca85e948SMichael Tuexen 
1568839d21d6SMichael Tuexen 					if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
1569839d21d6SMichael Tuexen 					    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1570f8829a4aSRandall Stewart 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1571f42a358aSRandall Stewart 					}
1572839d21d6SMichael Tuexen 					SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
1573c39cfa1fSMichael Tuexen 					sctp_stop_timers_for_shutdown(stcb);
1574c39cfa1fSMichael Tuexen 					if (stcb->asoc.alternate) {
1575a57fb68bSMichael Tuexen 						net = stcb->asoc.alternate;
1576c39cfa1fSMichael Tuexen 					} else {
1577a57fb68bSMichael Tuexen 						net = stcb->asoc.primary_destination;
1578c39cfa1fSMichael Tuexen 					}
1579a57fb68bSMichael Tuexen 					sctp_send_shutdown(stcb, net);
1580f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1581a57fb68bSMichael Tuexen 					    stcb->sctp_ep, stcb, net);
1582f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1583a57fb68bSMichael Tuexen 					    stcb->sctp_ep, stcb, NULL);
1584f8829a4aSRandall Stewart 				}
1585f8829a4aSRandall Stewart 			}
1586f8829a4aSRandall Stewart 		} else {
1587f8829a4aSRandall Stewart 			/*
1588f8829a4aSRandall Stewart 			 * No auto close at this time, reset t-o to check
1589f8829a4aSRandall Stewart 			 * later
1590f8829a4aSRandall Stewart 			 */
1591d5d190f2SMichael Tuexen 			uint32_t tmp;
1592f8829a4aSRandall Stewart 
1593f8829a4aSRandall Stewart 			/* fool the timer startup to use the time left */
1594f8829a4aSRandall Stewart 			tmp = asoc->sctp_autoclose_ticks;
1595f8829a4aSRandall Stewart 			asoc->sctp_autoclose_ticks -= ticks_gone_by;
1596a57fb68bSMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
1597f8829a4aSRandall Stewart 			/* restore the real tick value */
1598f8829a4aSRandall Stewart 			asoc->sctp_autoclose_ticks = tmp;
1599f8829a4aSRandall Stewart 		}
1600f8829a4aSRandall Stewart 	}
1601f8829a4aSRandall Stewart }
1602