xref: /freebsd/sys/netinet/sctp_timer.c (revision 4474d71a7b9082a2f94739e34fb3abcd0601aa9c)
1f8829a4aSRandall Stewart /*-
2b1006367SRandall Stewart  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5f8829a4aSRandall Stewart  *
6f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
7f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
8f8829a4aSRandall Stewart  *
9f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
10f8829a4aSRandall Stewart  *    this list of conditions and the following disclaimer.
11f8829a4aSRandall Stewart  *
12f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
13f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
14f8829a4aSRandall Stewart  *    the documentation and/or other materials provided with the distribution.
15f8829a4aSRandall Stewart  *
16f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
18f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
19f8829a4aSRandall Stewart  *
20f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
31f8829a4aSRandall Stewart  */
32f8829a4aSRandall Stewart 
33f8829a4aSRandall Stewart #include <sys/cdefs.h>
34f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
35f8829a4aSRandall Stewart 
36f8829a4aSRandall Stewart #define _IP_VHL
3793164cf9SRandall Stewart #include <netinet/sctp_os.h>
38f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
39f8829a4aSRandall Stewart #ifdef INET6
40f8829a4aSRandall Stewart #endif
41f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
4242551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
43f8829a4aSRandall Stewart #include <netinet/sctp_timer.h>
44f8829a4aSRandall Stewart #include <netinet/sctputil.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
46f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
47f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>
48f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h>
49f8829a4aSRandall Stewart #include <netinet/sctp_input.h>
50f8829a4aSRandall Stewart #include <netinet/sctp.h>
51f8829a4aSRandall Stewart #include <netinet/sctp_uio.h>
52*4474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
53830d754dSRandall Stewart #include <netinet/udp.h>
54*4474d71aSMichael Tuexen #endif
55f8829a4aSRandall Stewart 
56f8829a4aSRandall Stewart 
57f8829a4aSRandall Stewart void
58f8829a4aSRandall Stewart sctp_audit_retranmission_queue(struct sctp_association *asoc)
59f8829a4aSRandall Stewart {
60f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
61f8829a4aSRandall Stewart 
62ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit invoked on send queue cnt:%d onqueue:%d\n",
63f8829a4aSRandall Stewart 	    asoc->sent_queue_retran_cnt,
64f8829a4aSRandall Stewart 	    asoc->sent_queue_cnt);
65f8829a4aSRandall Stewart 	asoc->sent_queue_retran_cnt = 0;
66f8829a4aSRandall Stewart 	asoc->sent_queue_cnt = 0;
67f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
68f8829a4aSRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
69f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
70f8829a4aSRandall Stewart 		}
71f8829a4aSRandall Stewart 		asoc->sent_queue_cnt++;
72f8829a4aSRandall Stewart 	}
73f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
74f8829a4aSRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
75f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
76f8829a4aSRandall Stewart 		}
77f8829a4aSRandall Stewart 	}
78c54a18d2SRandall Stewart 	TAILQ_FOREACH(chk, &asoc->asconf_send_queue, sctp_next) {
79c54a18d2SRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
80c54a18d2SRandall Stewart 			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
81c54a18d2SRandall Stewart 		}
82c54a18d2SRandall Stewart 	}
83ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit completes retran:%d onqueue:%d\n",
84f8829a4aSRandall Stewart 	    asoc->sent_queue_retran_cnt,
85f8829a4aSRandall Stewart 	    asoc->sent_queue_cnt);
86f8829a4aSRandall Stewart }
87f8829a4aSRandall Stewart 
88f8829a4aSRandall Stewart int
89f8829a4aSRandall Stewart sctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
90f8829a4aSRandall Stewart     struct sctp_nets *net, uint16_t threshold)
91f8829a4aSRandall Stewart {
92f8829a4aSRandall Stewart 	if (net) {
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)) {
109ca85e948SMichael Tuexen 			if (!(net->dest_state & SCTP_ADDR_PF)) {
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);
113ca85e948SMichael Tuexen 				sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
114ca85e948SMichael Tuexen 				sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
115f8829a4aSRandall Stewart 			}
116ca85e948SMichael Tuexen 		}
117f8829a4aSRandall Stewart 	}
118f8829a4aSRandall Stewart 	if (stcb == NULL)
119f8829a4aSRandall Stewart 		return (0);
120f8829a4aSRandall Stewart 
121f8829a4aSRandall Stewart 	if (net) {
122f8829a4aSRandall Stewart 		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
123b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
124c4739e2fSRandall Stewart 				sctp_misc_ints(SCTP_THRESHOLD_INCR,
125c4739e2fSRandall Stewart 				    stcb->asoc.overall_error_count,
126c4739e2fSRandall Stewart 				    (stcb->asoc.overall_error_count + 1),
127c4739e2fSRandall Stewart 				    SCTP_FROM_SCTP_TIMER,
128c4739e2fSRandall Stewart 				    __LINE__);
129c4739e2fSRandall Stewart 			}
130f8829a4aSRandall Stewart 			stcb->asoc.overall_error_count++;
131f8829a4aSRandall Stewart 		}
132f8829a4aSRandall Stewart 	} else {
133b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
134c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_INCR,
135c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
136c4739e2fSRandall Stewart 			    (stcb->asoc.overall_error_count + 1),
137c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_TIMER,
138c4739e2fSRandall Stewart 			    __LINE__);
139c4739e2fSRandall Stewart 		}
140f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count++;
141f8829a4aSRandall Stewart 	}
142ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n",
143dd294dceSMichael Tuexen 	    (void *)&stcb->asoc, stcb->asoc.overall_error_count,
144f8829a4aSRandall Stewart 	    (uint32_t) threshold,
145f8829a4aSRandall Stewart 	    ((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
146f8829a4aSRandall Stewart 	/*
147f8829a4aSRandall Stewart 	 * We specifically do not do >= to give the assoc one more change
148f8829a4aSRandall Stewart 	 * before we fail it.
149f8829a4aSRandall Stewart 	 */
150f8829a4aSRandall Stewart 	if (stcb->asoc.overall_error_count > threshold) {
151f8829a4aSRandall Stewart 		/* Abort notification sends a ULP notify */
152ff1ffd74SMichael Tuexen 		struct mbuf *op_err;
153f8829a4aSRandall Stewart 
154ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION,
155ff1ffd74SMichael Tuexen 		    "Association error couter exceeded");
156a5d547adSRandall Stewart 		inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_1;
157ff1ffd74SMichael Tuexen 		sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
158f8829a4aSRandall Stewart 		return (1);
159f8829a4aSRandall Stewart 	}
160f8829a4aSRandall Stewart 	return (0);
161f8829a4aSRandall Stewart }
162f8829a4aSRandall Stewart 
163b61c3588SMichael Tuexen /*
164b61c3588SMichael Tuexen  * sctp_find_alternate_net() returns a non-NULL pointer as long
165b61c3588SMichael Tuexen  * the argument net is non-NULL.
166b61c3588SMichael Tuexen  */
167f8829a4aSRandall Stewart struct sctp_nets *
168f8829a4aSRandall Stewart sctp_find_alternate_net(struct sctp_tcb *stcb,
169f8829a4aSRandall Stewart     struct sctp_nets *net,
170b54d3a6cSRandall Stewart     int mode)
171f8829a4aSRandall Stewart {
172f8829a4aSRandall Stewart 	/* Find and return an alternate network if possible */
173b54d3a6cSRandall Stewart 	struct sctp_nets *alt, *mnet, *min_errors_net = NULL, *max_cwnd_net = NULL;
174f8829a4aSRandall Stewart 	int once;
175b54d3a6cSRandall Stewart 
176b54d3a6cSRandall Stewart 	/* JRS 5/14/07 - Initialize min_errors to an impossible value. */
177b54d3a6cSRandall Stewart 	int min_errors = -1;
178b54d3a6cSRandall Stewart 	uint32_t max_cwnd = 0;
179f8829a4aSRandall Stewart 
180f8829a4aSRandall Stewart 	if (stcb->asoc.numnets == 1) {
181f8829a4aSRandall Stewart 		/* No others but net */
182f8829a4aSRandall Stewart 		return (TAILQ_FIRST(&stcb->asoc.nets));
183f8829a4aSRandall Stewart 	}
184b54d3a6cSRandall Stewart 	/*
185b54d3a6cSRandall Stewart 	 * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate
186b54d3a6cSRandall Stewart 	 * net algorithm. This algorithm chooses the active destination (not
187b54d3a6cSRandall Stewart 	 * in PF state) with the largest cwnd value. If all destinations are
188b54d3a6cSRandall Stewart 	 * in PF state, unreachable, or unconfirmed, choose the desination
189b54d3a6cSRandall Stewart 	 * that is in PF state with the lowest error count. In case of a
190b54d3a6cSRandall Stewart 	 * tie, choose the destination that was most recently active.
191b54d3a6cSRandall Stewart 	 */
192b54d3a6cSRandall Stewart 	if (mode == 2) {
193b54d3a6cSRandall Stewart 		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
194b54d3a6cSRandall Stewart 			/*
195b54d3a6cSRandall Stewart 			 * JRS 5/14/07 - If the destination is unreachable
196b54d3a6cSRandall Stewart 			 * or unconfirmed, skip it.
197b54d3a6cSRandall Stewart 			 */
198b54d3a6cSRandall Stewart 			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
199b54d3a6cSRandall Stewart 			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
200b54d3a6cSRandall Stewart 				continue;
201b54d3a6cSRandall Stewart 			}
202b54d3a6cSRandall Stewart 			/*
203b54d3a6cSRandall Stewart 			 * JRS 5/14/07 -  If the destination is reachable
204b54d3a6cSRandall Stewart 			 * but in PF state, compare the error count of the
205b54d3a6cSRandall Stewart 			 * destination to the minimum error count seen thus
206b54d3a6cSRandall Stewart 			 * far. Store the destination with the lower error
207b54d3a6cSRandall Stewart 			 * count.  If the error counts are equal, store the
208b54d3a6cSRandall Stewart 			 * destination that was most recently active.
209b54d3a6cSRandall Stewart 			 */
210b54d3a6cSRandall Stewart 			if (mnet->dest_state & SCTP_ADDR_PF) {
211b54d3a6cSRandall Stewart 				/*
212b54d3a6cSRandall Stewart 				 * JRS 5/14/07 - If the destination under
213b54d3a6cSRandall Stewart 				 * consideration is the current destination,
214b54d3a6cSRandall Stewart 				 * work as if the error count is one higher.
215b54d3a6cSRandall Stewart 				 * The actual error count will not be
216b54d3a6cSRandall Stewart 				 * incremented until later in the t3
217b54d3a6cSRandall Stewart 				 * handler.
218b54d3a6cSRandall Stewart 				 */
219b54d3a6cSRandall Stewart 				if (mnet == net) {
220b54d3a6cSRandall Stewart 					if (min_errors == -1) {
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 						min_errors = mnet->error_count + 1;
225b54d3a6cSRandall Stewart 						min_errors_net = mnet;
226b54d3a6cSRandall Stewart 					} else if (mnet->error_count + 1 == min_errors
227b54d3a6cSRandall Stewart 					    && mnet->last_active > min_errors_net->last_active) {
228b54d3a6cSRandall Stewart 						min_errors_net = mnet;
229b54d3a6cSRandall Stewart 						min_errors = mnet->error_count + 1;
230b54d3a6cSRandall Stewart 					}
231b54d3a6cSRandall Stewart 					continue;
232b54d3a6cSRandall Stewart 				} else {
233b54d3a6cSRandall Stewart 					if (min_errors == -1) {
234b54d3a6cSRandall Stewart 						min_errors = mnet->error_count;
235b54d3a6cSRandall Stewart 						min_errors_net = mnet;
236b54d3a6cSRandall Stewart 					} else if (mnet->error_count < min_errors) {
237b54d3a6cSRandall Stewart 						min_errors = mnet->error_count;
238b54d3a6cSRandall Stewart 						min_errors_net = mnet;
239b54d3a6cSRandall Stewart 					} else if (mnet->error_count == min_errors
240b54d3a6cSRandall Stewart 					    && mnet->last_active > min_errors_net->last_active) {
241b54d3a6cSRandall Stewart 						min_errors_net = mnet;
242b54d3a6cSRandall Stewart 						min_errors = mnet->error_count;
243b54d3a6cSRandall Stewart 					}
244b54d3a6cSRandall Stewart 					continue;
245b54d3a6cSRandall Stewart 				}
246b54d3a6cSRandall Stewart 			}
247b54d3a6cSRandall Stewart 			/*
248b54d3a6cSRandall Stewart 			 * JRS 5/14/07 - If the destination is reachable and
249b54d3a6cSRandall Stewart 			 * not in PF state, compare the cwnd of the
250b54d3a6cSRandall Stewart 			 * destination to the highest cwnd seen thus far.
251b54d3a6cSRandall Stewart 			 * Store the destination with the higher cwnd value.
252b54d3a6cSRandall Stewart 			 * If the cwnd values are equal, randomly choose one
253b54d3a6cSRandall Stewart 			 * of the two destinations.
254b54d3a6cSRandall Stewart 			 */
255b54d3a6cSRandall Stewart 			if (max_cwnd < mnet->cwnd) {
256b54d3a6cSRandall Stewart 				max_cwnd_net = mnet;
257b54d3a6cSRandall Stewart 				max_cwnd = mnet->cwnd;
258b54d3a6cSRandall Stewart 			} else if (max_cwnd == mnet->cwnd) {
259b54d3a6cSRandall Stewart 				uint32_t rndval;
260b54d3a6cSRandall Stewart 				uint8_t this_random;
261b54d3a6cSRandall Stewart 
262b54d3a6cSRandall Stewart 				if (stcb->asoc.hb_random_idx > 3) {
263b54d3a6cSRandall Stewart 					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
264b54d3a6cSRandall Stewart 					memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values));
265b54d3a6cSRandall Stewart 					this_random = stcb->asoc.hb_random_values[0];
266b54d3a6cSRandall Stewart 					stcb->asoc.hb_random_idx++;
267b54d3a6cSRandall Stewart 					stcb->asoc.hb_ect_randombit = 0;
268b54d3a6cSRandall Stewart 				} else {
269b54d3a6cSRandall Stewart 					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
270b54d3a6cSRandall Stewart 					stcb->asoc.hb_random_idx++;
271b54d3a6cSRandall Stewart 					stcb->asoc.hb_ect_randombit = 0;
272b54d3a6cSRandall Stewart 				}
273b54d3a6cSRandall Stewart 				if (this_random % 2 == 1) {
274b54d3a6cSRandall Stewart 					max_cwnd_net = mnet;
275fc14de76SRandall Stewart 					max_cwnd = mnet->cwnd;	/* Useless? */
276b54d3a6cSRandall Stewart 				}
277b54d3a6cSRandall Stewart 			}
278b54d3a6cSRandall Stewart 		}
279b54d3a6cSRandall Stewart 		if (max_cwnd_net == NULL) {
280b54d3a6cSRandall Stewart 			if (min_errors_net == NULL) {
281b54d3a6cSRandall Stewart 				return (net);
282b54d3a6cSRandall Stewart 			}
283b54d3a6cSRandall Stewart 			return (min_errors_net);
284b54d3a6cSRandall Stewart 		} else {
285b54d3a6cSRandall Stewart 			return (max_cwnd_net);
286b54d3a6cSRandall Stewart 		}
287b54d3a6cSRandall Stewart 	}
288b54d3a6cSRandall Stewart 	/*
289b54d3a6cSRandall Stewart 	 * JRS 5/14/07 - If mode is set to 1, use the CMT policy for
290b54d3a6cSRandall Stewart 	 * choosing an alternate net.
291b54d3a6cSRandall Stewart 	 */
292b54d3a6cSRandall Stewart 	else if (mode == 1) {
293f8829a4aSRandall Stewart 		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
294f8829a4aSRandall Stewart 			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
295b61c3588SMichael Tuexen 			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
296f8829a4aSRandall Stewart 				/*
297f8829a4aSRandall Stewart 				 * will skip ones that are not-reachable or
298f8829a4aSRandall Stewart 				 * unconfirmed
299f8829a4aSRandall Stewart 				 */
300f8829a4aSRandall Stewart 				continue;
301f8829a4aSRandall Stewart 			}
302b54d3a6cSRandall Stewart 			if (max_cwnd < mnet->cwnd) {
303b54d3a6cSRandall Stewart 				max_cwnd_net = mnet;
304b54d3a6cSRandall Stewart 				max_cwnd = mnet->cwnd;
305b54d3a6cSRandall Stewart 			} else if (max_cwnd == mnet->cwnd) {
306f8829a4aSRandall Stewart 				uint32_t rndval;
307f8829a4aSRandall Stewart 				uint8_t this_random;
308f8829a4aSRandall Stewart 
309f8829a4aSRandall Stewart 				if (stcb->asoc.hb_random_idx > 3) {
310f8829a4aSRandall Stewart 					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
311f8829a4aSRandall Stewart 					memcpy(stcb->asoc.hb_random_values, &rndval,
312f8829a4aSRandall Stewart 					    sizeof(stcb->asoc.hb_random_values));
313f8829a4aSRandall Stewart 					this_random = stcb->asoc.hb_random_values[0];
314f8829a4aSRandall Stewart 					stcb->asoc.hb_random_idx = 0;
315f8829a4aSRandall Stewart 					stcb->asoc.hb_ect_randombit = 0;
316f8829a4aSRandall Stewart 				} else {
317f8829a4aSRandall Stewart 					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
318f8829a4aSRandall Stewart 					stcb->asoc.hb_random_idx++;
319f8829a4aSRandall Stewart 					stcb->asoc.hb_ect_randombit = 0;
320f8829a4aSRandall Stewart 				}
321f8829a4aSRandall Stewart 				if (this_random % 2) {
322b54d3a6cSRandall Stewart 					max_cwnd_net = mnet;
323b54d3a6cSRandall Stewart 					max_cwnd = mnet->cwnd;
324f8829a4aSRandall Stewart 				}
325f8829a4aSRandall Stewart 			}
326f8829a4aSRandall Stewart 		}
327b54d3a6cSRandall Stewart 		if (max_cwnd_net) {
328b54d3a6cSRandall Stewart 			return (max_cwnd_net);
329f8829a4aSRandall Stewart 		}
330f8829a4aSRandall Stewart 	}
331f8829a4aSRandall Stewart 	mnet = net;
332f8829a4aSRandall Stewart 	once = 0;
333f8829a4aSRandall Stewart 
334f8829a4aSRandall Stewart 	if (mnet == NULL) {
335f8829a4aSRandall Stewart 		mnet = TAILQ_FIRST(&stcb->asoc.nets);
33652129fcdSRandall Stewart 		if (mnet == NULL) {
33752129fcdSRandall Stewart 			return (NULL);
33852129fcdSRandall Stewart 		}
339f8829a4aSRandall Stewart 	}
340f8829a4aSRandall Stewart 	do {
341f8829a4aSRandall Stewart 		alt = TAILQ_NEXT(mnet, sctp_next);
342f8829a4aSRandall Stewart 		if (alt == NULL) {
343f8829a4aSRandall Stewart 			once++;
344f8829a4aSRandall Stewart 			if (once > 1) {
345f8829a4aSRandall Stewart 				break;
346f8829a4aSRandall Stewart 			}
347f8829a4aSRandall Stewart 			alt = TAILQ_FIRST(&stcb->asoc.nets);
34852129fcdSRandall Stewart 			if (alt == NULL) {
34952129fcdSRandall Stewart 				return (NULL);
35052129fcdSRandall Stewart 			}
351f8829a4aSRandall Stewart 		}
352f8829a4aSRandall Stewart 		if (alt->ro.ro_rt == NULL) {
35342551e99SRandall Stewart 			if (alt->ro._s_addr) {
35442551e99SRandall Stewart 				sctp_free_ifa(alt->ro._s_addr);
35542551e99SRandall Stewart 				alt->ro._s_addr = NULL;
35642551e99SRandall Stewart 			}
357f8829a4aSRandall Stewart 			alt->src_addr_selected = 0;
358f8829a4aSRandall Stewart 		}
3593c503c28SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
360b61c3588SMichael Tuexen 		if (((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
361b61c3588SMichael Tuexen 		    (alt->ro.ro_rt != NULL) &&
362b61c3588SMichael Tuexen 		    (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))) {
363f8829a4aSRandall Stewart 			/* Found a reachable address */
364f8829a4aSRandall Stewart 			break;
365f8829a4aSRandall Stewart 		}
366f8829a4aSRandall Stewart 		mnet = alt;
367f8829a4aSRandall Stewart 	} while (alt != NULL);
368f8829a4aSRandall Stewart 
369f8829a4aSRandall Stewart 	if (alt == NULL) {
370f8829a4aSRandall Stewart 		/* Case where NO insv network exists (dormant state) */
371f8829a4aSRandall Stewart 		/* we rotate destinations */
372f8829a4aSRandall Stewart 		once = 0;
373f8829a4aSRandall Stewart 		mnet = net;
374f8829a4aSRandall Stewart 		do {
37552129fcdSRandall Stewart 			if (mnet == NULL) {
37652129fcdSRandall Stewart 				return (TAILQ_FIRST(&stcb->asoc.nets));
37752129fcdSRandall Stewart 			}
378f8829a4aSRandall Stewart 			alt = TAILQ_NEXT(mnet, sctp_next);
379f8829a4aSRandall Stewart 			if (alt == NULL) {
380f8829a4aSRandall Stewart 				once++;
381f8829a4aSRandall Stewart 				if (once > 1) {
382f8829a4aSRandall Stewart 					break;
383f8829a4aSRandall Stewart 				}
384f8829a4aSRandall Stewart 				alt = TAILQ_FIRST(&stcb->asoc.nets);
385f8829a4aSRandall Stewart 			}
3863c503c28SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
387f8829a4aSRandall Stewart 			if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
388f8829a4aSRandall Stewart 			    (alt != net)) {
389f8829a4aSRandall Stewart 				/* Found an alternate address */
390f8829a4aSRandall Stewart 				break;
391f8829a4aSRandall Stewart 			}
392f8829a4aSRandall Stewart 			mnet = alt;
393f8829a4aSRandall Stewart 		} while (alt != NULL);
394f8829a4aSRandall Stewart 	}
395f8829a4aSRandall Stewart 	if (alt == NULL) {
396f8829a4aSRandall Stewart 		return (net);
397f8829a4aSRandall Stewart 	}
398f8829a4aSRandall Stewart 	return (alt);
399f8829a4aSRandall Stewart }
400f8829a4aSRandall Stewart 
401f8829a4aSRandall Stewart static void
402f8829a4aSRandall Stewart sctp_backoff_on_timeout(struct sctp_tcb *stcb,
403f8829a4aSRandall Stewart     struct sctp_nets *net,
404f8829a4aSRandall Stewart     int win_probe,
40544fbe462SRandall Stewart     int num_marked, int num_abandoned)
406f8829a4aSRandall Stewart {
4079a972525SRandall Stewart 	if (net->RTO == 0) {
4089a972525SRandall Stewart 		net->RTO = stcb->asoc.minrto;
4099a972525SRandall Stewart 	}
410f8829a4aSRandall Stewart 	net->RTO <<= 1;
411f8829a4aSRandall Stewart 	if (net->RTO > stcb->asoc.maxrto) {
412f8829a4aSRandall Stewart 		net->RTO = stcb->asoc.maxrto;
413f8829a4aSRandall Stewart 	}
41444fbe462SRandall Stewart 	if ((win_probe == 0) && (num_marked || num_abandoned)) {
415f8829a4aSRandall Stewart 		/* We don't apply penalty to window probe scenarios */
416b54d3a6cSRandall Stewart 		/* JRS - Use the congestion control given in the CC module */
417b54d3a6cSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
418f8829a4aSRandall Stewart 	}
419f8829a4aSRandall Stewart }
420f8829a4aSRandall Stewart 
42183416c88SRandall Stewart #ifndef INVARIANTS
42283416c88SRandall Stewart static void
423df6e0cc3SRandall Stewart sctp_recover_sent_list(struct sctp_tcb *stcb)
424df6e0cc3SRandall Stewart {
4254a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
426df6e0cc3SRandall Stewart 	struct sctp_association *asoc;
427df6e0cc3SRandall Stewart 
428df6e0cc3SRandall Stewart 	asoc = &stcb->asoc;
4294a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
43020b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->last_acked_seq, chk->rec.data.TSN_seq)) {
431df6e0cc3SRandall Stewart 			SCTP_PRINTF("Found chk:%p tsn:%x <= last_acked_seq:%x\n",
432dd294dceSMichael Tuexen 			    (void *)chk, chk->rec.data.TSN_seq, asoc->last_acked_seq);
433325c8c46SMichael Tuexen 			if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
434a7ad6026SMichael Tuexen 				if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
435a7ad6026SMichael Tuexen 					asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
436a7ad6026SMichael Tuexen 				}
437a7ad6026SMichael Tuexen 			}
438df6e0cc3SRandall Stewart 			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4390ddb4299SMichael Tuexen 			if (PR_SCTP_ENABLED(chk->flags)) {
440df6e0cc3SRandall Stewart 				if (asoc->pr_sctp_cnt != 0)
441df6e0cc3SRandall Stewart 					asoc->pr_sctp_cnt--;
442df6e0cc3SRandall Stewart 			}
443df6e0cc3SRandall Stewart 			if (chk->data) {
444df6e0cc3SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
445df6e0cc3SRandall Stewart 				sctp_free_bufspace(stcb, asoc, chk, 1);
446df6e0cc3SRandall Stewart 				sctp_m_freem(chk->data);
4474a9ef3f8SMichael Tuexen 				chk->data = NULL;
448810ec536SMichael Tuexen 				if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(chk->flags)) {
449df6e0cc3SRandall Stewart 					asoc->sent_queue_cnt_removeable--;
450df6e0cc3SRandall Stewart 				}
451df6e0cc3SRandall Stewart 			}
452df6e0cc3SRandall Stewart 			asoc->sent_queue_cnt--;
453689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
454df6e0cc3SRandall Stewart 		}
455df6e0cc3SRandall Stewart 	}
456df6e0cc3SRandall Stewart 	SCTP_PRINTF("after recover order is as follows\n");
4574a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
458dd294dceSMichael Tuexen 		SCTP_PRINTF("chk:%p TSN:%x\n", (void *)chk, chk->rec.data.TSN_seq);
459df6e0cc3SRandall Stewart 	}
460df6e0cc3SRandall Stewart }
461df6e0cc3SRandall Stewart 
46283416c88SRandall Stewart #endif
46383416c88SRandall Stewart 
464f8829a4aSRandall Stewart static int
465f8829a4aSRandall Stewart sctp_mark_all_for_resend(struct sctp_tcb *stcb,
466f8829a4aSRandall Stewart     struct sctp_nets *net,
467f8829a4aSRandall Stewart     struct sctp_nets *alt,
468f8829a4aSRandall Stewart     int window_probe,
46944fbe462SRandall Stewart     int *num_marked,
47044fbe462SRandall Stewart     int *num_abandoned)
471f8829a4aSRandall Stewart {
472f8829a4aSRandall Stewart 
473f8829a4aSRandall Stewart 	/*
474f8829a4aSRandall Stewart 	 * Mark all chunks (well not all) that were sent to *net for
475f8829a4aSRandall Stewart 	 * retransmission. Move them to alt for there destination as well...
476f8829a4aSRandall Stewart 	 * We only mark chunks that have been outstanding long enough to
477f8829a4aSRandall Stewart 	 * have received feed-back.
478f8829a4aSRandall Stewart 	 */
4794a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
480f8829a4aSRandall Stewart 	struct sctp_nets *lnets;
481f8829a4aSRandall Stewart 	struct timeval now, min_wait, tv;
4820191fb6dSMichael Tuexen 	int cur_rto;
48344fbe462SRandall Stewart 	int cnt_abandoned;
484c105859eSRandall Stewart 	int audit_tf, num_mk, fir;
485f8829a4aSRandall Stewart 	unsigned int cnt_mk;
486c105859eSRandall Stewart 	uint32_t orig_flight, orig_tf;
487f8829a4aSRandall Stewart 	uint32_t tsnlast, tsnfirst;
488df6e0cc3SRandall Stewart 	int recovery_cnt = 0;
489f8829a4aSRandall Stewart 
490b54d3a6cSRandall Stewart 
491f8829a4aSRandall Stewart 	/* none in flight now */
492f8829a4aSRandall Stewart 	audit_tf = 0;
493f8829a4aSRandall Stewart 	fir = 0;
494f8829a4aSRandall Stewart 	/*
495f8829a4aSRandall Stewart 	 * figure out how long a data chunk must be pending before we can
496f8829a4aSRandall Stewart 	 * mark it ..
497f8829a4aSRandall Stewart 	 */
4986e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&now);
499f8829a4aSRandall Stewart 	/* get cur rto in micro-seconds */
5000191fb6dSMichael Tuexen 	cur_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
5010191fb6dSMichael Tuexen 	cur_rto *= 1000;
502ca85e948SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
5030191fb6dSMichael Tuexen 		sctp_log_fr(cur_rto,
504f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
505f8829a4aSRandall Stewart 		    window_probe,
506f8829a4aSRandall Stewart 		    SCTP_FR_T3_MARK_TIME);
507ca85e948SMichael Tuexen 		sctp_log_fr(net->flight_size, 0, 0, SCTP_FR_CWND_REPORT);
508f8829a4aSRandall Stewart 		sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
50980fefe0aSRandall Stewart 	}
5100191fb6dSMichael Tuexen 	tv.tv_sec = cur_rto / 1000000;
5110191fb6dSMichael Tuexen 	tv.tv_usec = cur_rto % 1000000;
512f8829a4aSRandall Stewart 	min_wait = now;
513f8829a4aSRandall Stewart 	timevalsub(&min_wait, &tv);
514f8829a4aSRandall Stewart 	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
515f8829a4aSRandall Stewart 		/*
516f8829a4aSRandall Stewart 		 * if we hit here, we don't have enough seconds on the clock
517f8829a4aSRandall Stewart 		 * to account for the RTO. We just let the lower seconds be
518f8829a4aSRandall Stewart 		 * the bounds and don't worry about it. This may mean we
519f8829a4aSRandall Stewart 		 * will mark a lot more than we should.
520f8829a4aSRandall Stewart 		 */
521f8829a4aSRandall Stewart 		min_wait.tv_sec = min_wait.tv_usec = 0;
522f8829a4aSRandall Stewart 	}
523ca85e948SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
5240191fb6dSMichael Tuexen 		sctp_log_fr(cur_rto, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
525f8829a4aSRandall Stewart 		sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
52680fefe0aSRandall Stewart 	}
527f8829a4aSRandall Stewart 	/*
528f8829a4aSRandall Stewart 	 * Our rwnd will be incorrect here since we are not adding back the
529f8829a4aSRandall Stewart 	 * cnt * mbuf but we will fix that down below.
530f8829a4aSRandall Stewart 	 */
531f8829a4aSRandall Stewart 	orig_flight = net->flight_size;
532c105859eSRandall Stewart 	orig_tf = stcb->asoc.total_flight;
533c105859eSRandall Stewart 
534f8829a4aSRandall Stewart 	net->fast_retran_ip = 0;
535f8829a4aSRandall Stewart 	/* Now on to each chunk */
53644fbe462SRandall Stewart 	cnt_abandoned = 0;
537f8829a4aSRandall Stewart 	num_mk = cnt_mk = 0;
538f8829a4aSRandall Stewart 	tsnfirst = tsnlast = 0;
53983416c88SRandall Stewart #ifndef INVARIANTS
540df6e0cc3SRandall Stewart start_again:
54183416c88SRandall Stewart #endif
5424a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.sent_queue, sctp_next, nchk) {
54320b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(stcb->asoc.last_acked_seq, chk->rec.data.TSN_seq)) {
544f8829a4aSRandall Stewart 			/* Strange case our list got out of order? */
5456ed72810SMichael Tuexen 			SCTP_PRINTF("Our list is out of order? last_acked:%x chk:%x\n",
546df6e0cc3SRandall Stewart 			    (unsigned int)stcb->asoc.last_acked_seq, (unsigned int)chk->rec.data.TSN_seq);
547df6e0cc3SRandall Stewart 			recovery_cnt++;
548df6e0cc3SRandall Stewart #ifdef INVARIANTS
549df6e0cc3SRandall Stewart 			panic("last acked >= chk on sent-Q");
550df6e0cc3SRandall Stewart #else
551df6e0cc3SRandall Stewart 			SCTP_PRINTF("Recover attempts a restart cnt:%d\n", recovery_cnt);
552df6e0cc3SRandall Stewart 			sctp_recover_sent_list(stcb);
553df6e0cc3SRandall Stewart 			if (recovery_cnt < 10) {
554df6e0cc3SRandall Stewart 				goto start_again;
555df6e0cc3SRandall Stewart 			} else {
556df6e0cc3SRandall Stewart 				SCTP_PRINTF("Recovery fails %d times??\n", recovery_cnt);
557df6e0cc3SRandall Stewart 			}
558df6e0cc3SRandall Stewart #endif
559f8829a4aSRandall Stewart 		}
560f8829a4aSRandall Stewart 		if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
561f8829a4aSRandall Stewart 			/*
562f8829a4aSRandall Stewart 			 * found one to mark: If it is less than
563f8829a4aSRandall Stewart 			 * DATAGRAM_ACKED it MUST not be a skipped or marked
564f8829a4aSRandall Stewart 			 * TSN but instead one that is either already set
565f8829a4aSRandall Stewart 			 * for retransmission OR one that needs
566f8829a4aSRandall Stewart 			 * retransmission.
567f8829a4aSRandall Stewart 			 */
568f8829a4aSRandall Stewart 
569f8829a4aSRandall Stewart 			/* validate its been outstanding long enough */
570ca85e948SMichael Tuexen 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
571f8829a4aSRandall Stewart 				sctp_log_fr(chk->rec.data.TSN_seq,
572f8829a4aSRandall Stewart 				    chk->sent_rcv_time.tv_sec,
573f8829a4aSRandall Stewart 				    chk->sent_rcv_time.tv_usec,
574f8829a4aSRandall Stewart 				    SCTP_FR_T3_MARK_TIME);
57580fefe0aSRandall Stewart 			}
576f8829a4aSRandall Stewart 			if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
577f8829a4aSRandall Stewart 				/*
578f8829a4aSRandall Stewart 				 * we have reached a chunk that was sent
579f8829a4aSRandall Stewart 				 * some seconds past our min.. forget it we
580f8829a4aSRandall Stewart 				 * will find no more to send.
581f8829a4aSRandall Stewart 				 */
582ca85e948SMichael Tuexen 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
583f8829a4aSRandall Stewart 					sctp_log_fr(0,
584f8829a4aSRandall Stewart 					    chk->sent_rcv_time.tv_sec,
585f8829a4aSRandall Stewart 					    chk->sent_rcv_time.tv_usec,
586f8829a4aSRandall Stewart 					    SCTP_FR_T3_STOPPED);
58780fefe0aSRandall Stewart 				}
588f8829a4aSRandall Stewart 				continue;
589f8829a4aSRandall Stewart 			} else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
590f8829a4aSRandall Stewart 			    (window_probe == 0)) {
591f8829a4aSRandall Stewart 				/*
592f8829a4aSRandall Stewart 				 * we must look at the micro seconds to
593f8829a4aSRandall Stewart 				 * know.
594f8829a4aSRandall Stewart 				 */
595f8829a4aSRandall Stewart 				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
596f8829a4aSRandall Stewart 					/*
597f8829a4aSRandall Stewart 					 * ok it was sent after our boundary
598f8829a4aSRandall Stewart 					 * time.
599f8829a4aSRandall Stewart 					 */
600f8829a4aSRandall Stewart 					continue;
601f8829a4aSRandall Stewart 				}
602f8829a4aSRandall Stewart 			}
603810ec536SMichael Tuexen 			if (stcb->asoc.peer_supports_prsctp && PR_SCTP_TTL_ENABLED(chk->flags)) {
604f8829a4aSRandall Stewart 				/* Is it expired? */
60599ddc825SMichael Tuexen 				if (timevalcmp(&now, &chk->rec.data.timetodrop, >)) {
606f8829a4aSRandall Stewart 					/* Yes so drop it */
607f8829a4aSRandall Stewart 					if (chk->data) {
608ad81507eSRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb,
609f8829a4aSRandall Stewart 						    chk,
6101edc9dbaSMichael Tuexen 						    1,
6110c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
61244fbe462SRandall Stewart 						cnt_abandoned++;
613f8829a4aSRandall Stewart 					}
614f8829a4aSRandall Stewart 					continue;
615f8829a4aSRandall Stewart 				}
616830d754dSRandall Stewart 			}
617810ec536SMichael Tuexen 			if (stcb->asoc.peer_supports_prsctp && PR_SCTP_RTX_ENABLED(chk->flags)) {
618f8829a4aSRandall Stewart 				/* Has it been retransmitted tv_sec times? */
619f8829a4aSRandall Stewart 				if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
620f8829a4aSRandall Stewart 					if (chk->data) {
621ad81507eSRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb,
622f8829a4aSRandall Stewart 						    chk,
6231edc9dbaSMichael Tuexen 						    1,
6240c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
62544fbe462SRandall Stewart 						cnt_abandoned++;
626f8829a4aSRandall Stewart 					}
627f8829a4aSRandall Stewart 					continue;
628f8829a4aSRandall Stewart 				}
629830d754dSRandall Stewart 			}
630c105859eSRandall Stewart 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
631f8829a4aSRandall Stewart 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
632f8829a4aSRandall Stewart 				num_mk++;
633f8829a4aSRandall Stewart 				if (fir == 0) {
634f8829a4aSRandall Stewart 					fir = 1;
635f8829a4aSRandall Stewart 					tsnfirst = chk->rec.data.TSN_seq;
636f8829a4aSRandall Stewart 				}
637f8829a4aSRandall Stewart 				tsnlast = chk->rec.data.TSN_seq;
638ca85e948SMichael Tuexen 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
639f8829a4aSRandall Stewart 					sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
640f8829a4aSRandall Stewart 					    0, SCTP_FR_T3_MARKED);
64180fefe0aSRandall Stewart 				}
64242551e99SRandall Stewart 				if (chk->rec.data.chunk_was_revoked) {
64342551e99SRandall Stewart 					/* deflate the cwnd */
64442551e99SRandall Stewart 					chk->whoTo->cwnd -= chk->book_size;
64542551e99SRandall Stewart 					chk->rec.data.chunk_was_revoked = 0;
64642551e99SRandall Stewart 				}
647f42a358aSRandall Stewart 				net->marked_retrans++;
648f42a358aSRandall Stewart 				stcb->asoc.marked_retrans++;
649b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
650c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
651a5d547adSRandall Stewart 					    chk->whoTo->flight_size,
652a5d547adSRandall Stewart 					    chk->book_size,
653c105859eSRandall Stewart 					    (uintptr_t) chk->whoTo,
654a5d547adSRandall Stewart 					    chk->rec.data.TSN_seq);
65580fefe0aSRandall Stewart 				}
656c105859eSRandall Stewart 				sctp_flight_size_decrease(chk);
657c105859eSRandall Stewart 				sctp_total_flight_decrease(stcb, chk);
658f8829a4aSRandall Stewart 				stcb->asoc.peers_rwnd += chk->send_size;
659b3f1ea41SRandall Stewart 				stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
660c105859eSRandall Stewart 			}
661c105859eSRandall Stewart 			chk->sent = SCTP_DATAGRAM_RESEND;
662c105859eSRandall Stewart 			SCTP_STAT_INCR(sctps_markedretrans);
663f8829a4aSRandall Stewart 
664f8829a4aSRandall Stewart 			/* reset the TSN for striking and other FR stuff */
665f8829a4aSRandall Stewart 			chk->rec.data.doing_fast_retransmit = 0;
666f8829a4aSRandall Stewart 			/* Clear any time so NO RTT is being done */
667f79aab18SRandall Stewart 
668f79aab18SRandall Stewart 			if (chk->do_rtt) {
669f79aab18SRandall Stewart 				if (chk->whoTo->rto_needed == 0) {
670f79aab18SRandall Stewart 					chk->whoTo->rto_needed = 1;
671f79aab18SRandall Stewart 				}
672f79aab18SRandall Stewart 			}
673f8829a4aSRandall Stewart 			chk->do_rtt = 0;
674f8829a4aSRandall Stewart 			if (alt != net) {
675f8829a4aSRandall Stewart 				sctp_free_remote_addr(chk->whoTo);
676f8829a4aSRandall Stewart 				chk->no_fr_allowed = 1;
677f8829a4aSRandall Stewart 				chk->whoTo = alt;
678f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
679f8829a4aSRandall Stewart 			} else {
680f8829a4aSRandall Stewart 				chk->no_fr_allowed = 0;
681f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
682f8829a4aSRandall Stewart 					chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
683f8829a4aSRandall Stewart 				} else {
684f8829a4aSRandall Stewart 					chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
685f8829a4aSRandall Stewart 				}
686f8829a4aSRandall Stewart 			}
687ad21a364SRandall Stewart 			/*
688ad21a364SRandall Stewart 			 * CMT: Do not allow FRs on retransmitted TSNs.
689ad21a364SRandall Stewart 			 */
6907c99d56fSMichael Tuexen 			if (stcb->asoc.sctp_cmt_on_off > 0) {
691f8829a4aSRandall Stewart 				chk->no_fr_allowed = 1;
692f8829a4aSRandall Stewart 			}
69344fbe462SRandall Stewart #ifdef THIS_SHOULD_NOT_BE_DONE
694f8829a4aSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_ACKED) {
695f8829a4aSRandall Stewart 			/* remember highest acked one */
696f8829a4aSRandall Stewart 			could_be_sent = chk;
69744fbe462SRandall Stewart #endif
698f8829a4aSRandall Stewart 		}
699f8829a4aSRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
700f8829a4aSRandall Stewart 			cnt_mk++;
701f8829a4aSRandall Stewart 		}
702f8829a4aSRandall Stewart 	}
703c105859eSRandall Stewart 	if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
704c105859eSRandall Stewart 		/* we did not subtract the same things? */
705c105859eSRandall Stewart 		audit_tf = 1;
706c105859eSRandall Stewart 	}
707ca85e948SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
708f8829a4aSRandall Stewart 		sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
70980fefe0aSRandall Stewart 	}
710f8829a4aSRandall Stewart #ifdef SCTP_DEBUG
711f8829a4aSRandall Stewart 	if (num_mk) {
712ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
713ad81507eSRandall Stewart 		    tsnlast);
714ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%ld\n",
715f8829a4aSRandall Stewart 		    num_mk, (u_long)stcb->asoc.peers_rwnd);
716ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
717ad81507eSRandall Stewart 		    tsnlast);
718ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%d\n",
719f8829a4aSRandall Stewart 		    num_mk,
720ad81507eSRandall Stewart 		    (int)stcb->asoc.peers_rwnd);
721f8829a4aSRandall Stewart 	}
722f8829a4aSRandall Stewart #endif
723f8829a4aSRandall Stewart 	*num_marked = num_mk;
72444fbe462SRandall Stewart 	*num_abandoned = cnt_abandoned;
7256c065bbeSRandall Stewart 	/*
7266c065bbeSRandall Stewart 	 * Now check for a ECN Echo that may be stranded And include the
7276c065bbeSRandall Stewart 	 * cnt_mk'd to have all resends in the control queue.
7286c065bbeSRandall Stewart 	 */
7296c065bbeSRandall Stewart 	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
7306c065bbeSRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
7316c065bbeSRandall Stewart 			cnt_mk++;
7326c065bbeSRandall Stewart 		}
7336c065bbeSRandall Stewart 		if ((chk->whoTo == net) &&
7346c065bbeSRandall Stewart 		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
7356c065bbeSRandall Stewart 			sctp_free_remote_addr(chk->whoTo);
7366c065bbeSRandall Stewart 			chk->whoTo = alt;
7376c065bbeSRandall Stewart 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
7386c065bbeSRandall Stewart 				chk->sent = SCTP_DATAGRAM_RESEND;
7396c065bbeSRandall Stewart 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
7406c065bbeSRandall Stewart 				cnt_mk++;
7416c065bbeSRandall Stewart 			}
7426c065bbeSRandall Stewart 			atomic_add_int(&alt->ref_count, 1);
7436c065bbeSRandall Stewart 		}
7446c065bbeSRandall Stewart 	}
74544fbe462SRandall Stewart #ifdef THIS_SHOULD_NOT_BE_DONE
746f8829a4aSRandall Stewart 	if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
747f8829a4aSRandall Stewart 		/* fix it so we retransmit the highest acked anyway */
748f8829a4aSRandall Stewart 		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
749f8829a4aSRandall Stewart 		cnt_mk++;
750f8829a4aSRandall Stewart 		could_be_sent->sent = SCTP_DATAGRAM_RESEND;
751f8829a4aSRandall Stewart 	}
75244fbe462SRandall Stewart #endif
753f8829a4aSRandall Stewart 	if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
754a5d547adSRandall Stewart #ifdef INVARIANTS
75518e198d3SRandall Stewart 		SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
75618e198d3SRandall Stewart 		    cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
757f8829a4aSRandall Stewart #endif
758f8829a4aSRandall Stewart #ifndef SCTP_AUDITING_ENABLED
759f8829a4aSRandall Stewart 		stcb->asoc.sent_queue_retran_cnt = cnt_mk;
760f8829a4aSRandall Stewart #endif
761f8829a4aSRandall Stewart 	}
762f8829a4aSRandall Stewart 	if (audit_tf) {
763ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER4,
764ad81507eSRandall Stewart 		    "Audit total flight due to negative value net:%p\n",
765dd294dceSMichael Tuexen 		    (void *)net);
766f8829a4aSRandall Stewart 		stcb->asoc.total_flight = 0;
767f8829a4aSRandall Stewart 		stcb->asoc.total_flight_count = 0;
768f8829a4aSRandall Stewart 		/* Clear all networks flight size */
769f8829a4aSRandall Stewart 		TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
770f8829a4aSRandall Stewart 			lnets->flight_size = 0;
771ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_TIMER4,
772ad81507eSRandall Stewart 			    "Net:%p c-f cwnd:%d ssthresh:%d\n",
773dd294dceSMichael Tuexen 			    (void *)lnets, lnets->cwnd, lnets->ssthresh);
774f8829a4aSRandall Stewart 		}
775f8829a4aSRandall Stewart 		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
776f8829a4aSRandall Stewart 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
777b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
778a5d547adSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
779a5d547adSRandall Stewart 					    chk->whoTo->flight_size,
780a5d547adSRandall Stewart 					    chk->book_size,
781c105859eSRandall Stewart 					    (uintptr_t) chk->whoTo,
782a5d547adSRandall Stewart 					    chk->rec.data.TSN_seq);
78380fefe0aSRandall Stewart 				}
784c105859eSRandall Stewart 				sctp_flight_size_increase(chk);
785c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, chk);
786f8829a4aSRandall Stewart 			}
787f8829a4aSRandall Stewart 		}
788f8829a4aSRandall Stewart 	}
789f8829a4aSRandall Stewart 	/* We return 1 if we only have a window probe outstanding */
790f8829a4aSRandall Stewart 	return (0);
791f8829a4aSRandall Stewart }
792f8829a4aSRandall Stewart 
793f8829a4aSRandall Stewart 
794f8829a4aSRandall Stewart int
795f8829a4aSRandall Stewart sctp_t3rxt_timer(struct sctp_inpcb *inp,
796f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
797f8829a4aSRandall Stewart     struct sctp_nets *net)
798f8829a4aSRandall Stewart {
799f8829a4aSRandall Stewart 	struct sctp_nets *alt;
80044fbe462SRandall Stewart 	int win_probe, num_mk, num_abandoned;
801f8829a4aSRandall Stewart 
802b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
803562a89b5SRandall Stewart 		sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
80480fefe0aSRandall Stewart 	}
805b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
806f8829a4aSRandall Stewart 		struct sctp_nets *lnet;
807f8829a4aSRandall Stewart 
808f8829a4aSRandall Stewart 		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
809f8829a4aSRandall Stewart 			if (net == lnet) {
810f8829a4aSRandall Stewart 				sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
811f8829a4aSRandall Stewart 			} else {
812f8829a4aSRandall Stewart 				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
813f8829a4aSRandall Stewart 			}
814f8829a4aSRandall Stewart 		}
815f8829a4aSRandall Stewart 	}
816f8829a4aSRandall Stewart 	/* Find an alternate and mark those for retransmission */
817f8829a4aSRandall Stewart 	if ((stcb->asoc.peers_rwnd == 0) &&
818f8829a4aSRandall Stewart 	    (stcb->asoc.total_flight < net->mtu)) {
819f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timowindowprobe);
820f8829a4aSRandall Stewart 		win_probe = 1;
821f8829a4aSRandall Stewart 	} else {
822f8829a4aSRandall Stewart 		win_probe = 0;
823f8829a4aSRandall Stewart 	}
824c105859eSRandall Stewart 
825ca85e948SMichael Tuexen 	if (win_probe == 0) {
826ca85e948SMichael Tuexen 		/* We don't do normal threshold management on window probes */
827ca85e948SMichael Tuexen 		if (sctp_threshold_management(inp, stcb, net,
828ca85e948SMichael Tuexen 		    stcb->asoc.max_send_times)) {
829ca85e948SMichael Tuexen 			/* Association was destroyed */
830ca85e948SMichael Tuexen 			return (1);
831ca85e948SMichael Tuexen 		} else {
832ca85e948SMichael Tuexen 			if (net != stcb->asoc.primary_destination) {
833ca85e948SMichael Tuexen 				/* send a immediate HB if our RTO is stale */
834ca85e948SMichael Tuexen 				struct timeval now;
835ca85e948SMichael Tuexen 				unsigned int ms_goneby;
836ca85e948SMichael Tuexen 
837ca85e948SMichael Tuexen 				(void)SCTP_GETTIME_TIMEVAL(&now);
838ca85e948SMichael Tuexen 				if (net->last_sent_time.tv_sec) {
839ca85e948SMichael Tuexen 					ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
840ca85e948SMichael Tuexen 				} else {
841ca85e948SMichael Tuexen 					ms_goneby = 0;
842b54d3a6cSRandall Stewart 				}
843ca85e948SMichael Tuexen 				if ((net->dest_state & SCTP_ADDR_PF) == 0) {
844ca85e948SMichael Tuexen 					if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
845c105859eSRandall Stewart 						/*
846ca85e948SMichael Tuexen 						 * no recent feed back in an
847ca85e948SMichael Tuexen 						 * RTO or more, request a
848ca85e948SMichael Tuexen 						 * RTT update
849ca85e948SMichael Tuexen 						 */
850ca85e948SMichael Tuexen 						sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
851ca85e948SMichael Tuexen 					}
852ca85e948SMichael Tuexen 				}
853ca85e948SMichael Tuexen 			}
854ca85e948SMichael Tuexen 		}
855ca85e948SMichael Tuexen 	} else {
856ca85e948SMichael Tuexen 		/*
857ca85e948SMichael Tuexen 		 * For a window probe we don't penalize the net's but only
858ca85e948SMichael Tuexen 		 * the association. This may fail it if SACKs are not coming
859ca85e948SMichael Tuexen 		 * back. If sack's are coming with rwnd locked at 0, we will
860ca85e948SMichael Tuexen 		 * continue to hold things waiting for rwnd to raise
861ca85e948SMichael Tuexen 		 */
862ca85e948SMichael Tuexen 		if (sctp_threshold_management(inp, stcb, NULL,
863ca85e948SMichael Tuexen 		    stcb->asoc.max_send_times)) {
864ca85e948SMichael Tuexen 			/* Association was destroyed */
865ca85e948SMichael Tuexen 			return (1);
866ca85e948SMichael Tuexen 		}
867ca85e948SMichael Tuexen 	}
868ca85e948SMichael Tuexen 	if (stcb->asoc.sctp_cmt_on_off > 0) {
869ca85e948SMichael Tuexen 		if (net->pf_threshold < net->failure_threshold) {
870ca85e948SMichael Tuexen 			alt = sctp_find_alternate_net(stcb, net, 2);
871ca85e948SMichael Tuexen 		} else {
872ca85e948SMichael Tuexen 			/*
873ca85e948SMichael Tuexen 			 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is
874ca85e948SMichael Tuexen 			 * being used, then pick dest with largest ssthresh
875ca85e948SMichael Tuexen 			 * for any retransmission.
876c105859eSRandall Stewart 			 */
877b61c3588SMichael Tuexen 			alt = sctp_find_alternate_net(stcb, net, 1);
878c105859eSRandall Stewart 			/*
879c105859eSRandall Stewart 			 * CUCv2: If a different dest is picked for the
880ca85e948SMichael Tuexen 			 * retransmission, then new (rtx-)pseudo_cumack
881ca85e948SMichael Tuexen 			 * needs to be tracked for orig dest. Let CUCv2
882ca85e948SMichael Tuexen 			 * track new (rtx-) pseudo-cumack always.
883c105859eSRandall Stewart 			 */
884c105859eSRandall Stewart 			net->find_pseudo_cumack = 1;
885c105859eSRandall Stewart 			net->find_rtx_pseudo_cumack = 1;
886ca85e948SMichael Tuexen 		}
887ca85e948SMichael Tuexen 	} else {
888f8829a4aSRandall Stewart 		alt = sctp_find_alternate_net(stcb, net, 0);
889c105859eSRandall Stewart 	}
890ca85e948SMichael Tuexen 
89144fbe462SRandall Stewart 	num_mk = 0;
89244fbe462SRandall Stewart 	num_abandoned = 0;
89344fbe462SRandall Stewart 	(void)sctp_mark_all_for_resend(stcb, net, alt, win_probe,
89444fbe462SRandall Stewart 	    &num_mk, &num_abandoned);
895f8829a4aSRandall Stewart 	/* FR Loss recovery just ended with the T3. */
896f8829a4aSRandall Stewart 	stcb->asoc.fast_retran_loss_recovery = 0;
897f8829a4aSRandall Stewart 
898f8829a4aSRandall Stewart 	/* CMT FR loss recovery ended with the T3 */
899f8829a4aSRandall Stewart 	net->fast_retran_loss_recovery = 0;
900299108c5SRandall Stewart 	if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
901299108c5SRandall Stewart 	    (net->flight_size == 0)) {
902299108c5SRandall Stewart 		(*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
903299108c5SRandall Stewart 	}
904f8829a4aSRandall Stewart 	/*
905f8829a4aSRandall Stewart 	 * setup the sat loss recovery that prevents satellite cwnd advance.
906f8829a4aSRandall Stewart 	 */
907f8829a4aSRandall Stewart 	stcb->asoc.sat_t3_loss_recovery = 1;
908f8829a4aSRandall Stewart 	stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
909f8829a4aSRandall Stewart 
910f8829a4aSRandall Stewart 	/* Backoff the timer and cwnd */
91144fbe462SRandall Stewart 	sctp_backoff_on_timeout(stcb, net, win_probe, num_mk, num_abandoned);
912ca85e948SMichael Tuexen 	if ((!(net->dest_state & SCTP_ADDR_REACHABLE)) ||
913ca85e948SMichael Tuexen 	    (net->dest_state & SCTP_ADDR_PF)) {
914f8829a4aSRandall Stewart 		/* Move all pending over too */
9159eea4a2dSMichael Tuexen 		sctp_move_chunks_from_net(stcb, net);
91617205eccSRandall Stewart 
91717205eccSRandall Stewart 		/*
91817205eccSRandall Stewart 		 * Get the address that failed, to force a new src address
91917205eccSRandall Stewart 		 * selecton and a route allocation.
92017205eccSRandall Stewart 		 */
92117205eccSRandall Stewart 		if (net->ro._s_addr) {
92217205eccSRandall Stewart 			sctp_free_ifa(net->ro._s_addr);
92317205eccSRandall Stewart 			net->ro._s_addr = NULL;
92417205eccSRandall Stewart 		}
92517205eccSRandall Stewart 		net->src_addr_selected = 0;
92617205eccSRandall Stewart 
92717205eccSRandall Stewart 		/* Force a route allocation too */
92817205eccSRandall Stewart 		if (net->ro.ro_rt) {
92917205eccSRandall Stewart 			RTFREE(net->ro.ro_rt);
93017205eccSRandall Stewart 			net->ro.ro_rt = NULL;
93117205eccSRandall Stewart 		}
932f8829a4aSRandall Stewart 		/* Was it our primary? */
933f8829a4aSRandall Stewart 		if ((stcb->asoc.primary_destination == net) && (alt != net)) {
934f8829a4aSRandall Stewart 			/*
935f8829a4aSRandall Stewart 			 * Yes, note it as such and find an alternate note:
936f8829a4aSRandall Stewart 			 * this means HB code must use this to resent the
937f8829a4aSRandall Stewart 			 * primary if it goes active AND if someone does a
938f8829a4aSRandall Stewart 			 * change-primary then this flag must be cleared
939f8829a4aSRandall Stewart 			 * from any net structures.
940f8829a4aSRandall Stewart 			 */
941ca85e948SMichael Tuexen 			if (stcb->asoc.alternate) {
942ca85e948SMichael Tuexen 				sctp_free_remote_addr(stcb->asoc.alternate);
943f8829a4aSRandall Stewart 			}
944ca85e948SMichael Tuexen 			stcb->asoc.alternate = alt;
945ca85e948SMichael Tuexen 			atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
946f8829a4aSRandall Stewart 		}
947f8829a4aSRandall Stewart 	}
948f8829a4aSRandall Stewart 	/*
949f8829a4aSRandall Stewart 	 * Special case for cookie-echo'ed case, we don't do output but must
950f8829a4aSRandall Stewart 	 * await the COOKIE-ACK before retransmission
951f8829a4aSRandall Stewart 	 */
952f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
953f8829a4aSRandall Stewart 		/*
954f8829a4aSRandall Stewart 		 * Here we just reset the timer and start again since we
955f8829a4aSRandall Stewart 		 * have not established the asoc
956f8829a4aSRandall Stewart 		 */
957f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
958f8829a4aSRandall Stewart 		return (0);
959f8829a4aSRandall Stewart 	}
960f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_prsctp) {
961f8829a4aSRandall Stewart 		struct sctp_tmit_chunk *lchk;
962f8829a4aSRandall Stewart 
963f8829a4aSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
964f8829a4aSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
96520b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(stcb->asoc.advanced_peer_ack_point, stcb->asoc.last_acked_seq)) {
966f8829a4aSRandall Stewart 			send_forward_tsn(stcb, &stcb->asoc);
967f8829a4aSRandall Stewart 			if (lchk) {
968f8829a4aSRandall Stewart 				/* Assure a timer is up */
969f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
970f8829a4aSRandall Stewart 			}
971f8829a4aSRandall Stewart 		}
972f8829a4aSRandall Stewart 	}
973b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
974f8829a4aSRandall Stewart 		sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
97580fefe0aSRandall Stewart 	}
976f8829a4aSRandall Stewart 	return (0);
977f8829a4aSRandall Stewart }
978f8829a4aSRandall Stewart 
979f8829a4aSRandall Stewart int
980f8829a4aSRandall Stewart sctp_t1init_timer(struct sctp_inpcb *inp,
981f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
982f8829a4aSRandall Stewart     struct sctp_nets *net)
983f8829a4aSRandall Stewart {
984f8829a4aSRandall Stewart 	/* bump the thresholds */
985f8829a4aSRandall Stewart 	if (stcb->asoc.delayed_connection) {
986f8829a4aSRandall Stewart 		/*
987f8829a4aSRandall Stewart 		 * special hook for delayed connection. The library did NOT
988f8829a4aSRandall Stewart 		 * complete the rest of its sends.
989f8829a4aSRandall Stewart 		 */
990f8829a4aSRandall Stewart 		stcb->asoc.delayed_connection = 0;
991ceaad40aSRandall Stewart 		sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
992f8829a4aSRandall Stewart 		return (0);
993f8829a4aSRandall Stewart 	}
994f8829a4aSRandall Stewart 	if (SCTP_GET_STATE((&stcb->asoc)) != SCTP_STATE_COOKIE_WAIT) {
995f8829a4aSRandall Stewart 		return (0);
996f8829a4aSRandall Stewart 	}
997f8829a4aSRandall Stewart 	if (sctp_threshold_management(inp, stcb, net,
998f8829a4aSRandall Stewart 	    stcb->asoc.max_init_times)) {
999f8829a4aSRandall Stewart 		/* Association was destroyed */
1000f8829a4aSRandall Stewart 		return (1);
1001f8829a4aSRandall Stewart 	}
1002f8829a4aSRandall Stewart 	stcb->asoc.dropped_special_cnt = 0;
100344fbe462SRandall Stewart 	sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0, 0);
1004f8829a4aSRandall Stewart 	if (stcb->asoc.initial_init_rto_max < net->RTO) {
1005f8829a4aSRandall Stewart 		net->RTO = stcb->asoc.initial_init_rto_max;
1006f8829a4aSRandall Stewart 	}
1007f8829a4aSRandall Stewart 	if (stcb->asoc.numnets > 1) {
1008f8829a4aSRandall Stewart 		/* If we have more than one addr use it */
1009f8829a4aSRandall Stewart 		struct sctp_nets *alt;
1010f8829a4aSRandall Stewart 
1011f8829a4aSRandall Stewart 		alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1012b61c3588SMichael Tuexen 		if (alt != stcb->asoc.primary_destination) {
10139eea4a2dSMichael Tuexen 			sctp_move_chunks_from_net(stcb, stcb->asoc.primary_destination);
1014f8829a4aSRandall Stewart 			stcb->asoc.primary_destination = alt;
1015f8829a4aSRandall Stewart 		}
1016f8829a4aSRandall Stewart 	}
1017f8829a4aSRandall Stewart 	/* Send out a new init */
1018ceaad40aSRandall Stewart 	sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1019f8829a4aSRandall Stewart 	return (0);
1020f8829a4aSRandall Stewart }
1021f8829a4aSRandall Stewart 
1022f8829a4aSRandall Stewart /*
1023f8829a4aSRandall Stewart  * For cookie and asconf we actually need to find and mark for resend, then
1024f8829a4aSRandall Stewart  * increment the resend counter (after all the threshold management stuff of
1025f8829a4aSRandall Stewart  * course).
1026f8829a4aSRandall Stewart  */
1027f8829a4aSRandall Stewart int
1028f8829a4aSRandall Stewart sctp_cookie_timer(struct sctp_inpcb *inp,
1029f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
10307215cc1bSMichael Tuexen     struct sctp_nets *net SCTP_UNUSED)
1031f8829a4aSRandall Stewart {
1032f8829a4aSRandall Stewart 	struct sctp_nets *alt;
1033f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *cookie;
1034f8829a4aSRandall Stewart 
1035f8829a4aSRandall Stewart 	/* first before all else we must find the cookie */
1036f8829a4aSRandall Stewart 	TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1037f8829a4aSRandall Stewart 		if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1038f8829a4aSRandall Stewart 			break;
1039f8829a4aSRandall Stewart 		}
1040f8829a4aSRandall Stewart 	}
1041f8829a4aSRandall Stewart 	if (cookie == NULL) {
1042f8829a4aSRandall Stewart 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1043f8829a4aSRandall Stewart 			/* FOOBAR! */
1044ff1ffd74SMichael Tuexen 			struct mbuf *op_err;
1045f8829a4aSRandall Stewart 
1046ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION,
1047ff1ffd74SMichael Tuexen 			    "Cookie timer expired, but no cookie");
1048b54d3a6cSRandall Stewart 			inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_4;
1049ff1ffd74SMichael Tuexen 			sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
1050f8829a4aSRandall Stewart 		} else {
1051a5d547adSRandall Stewart #ifdef INVARIANTS
1052f8829a4aSRandall Stewart 			panic("Cookie timer expires in wrong state?");
1053f8829a4aSRandall Stewart #else
1054ad81507eSRandall Stewart 			SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(&stcb->asoc));
1055f8829a4aSRandall Stewart 			return (0);
1056f8829a4aSRandall Stewart #endif
1057f8829a4aSRandall Stewart 		}
1058f8829a4aSRandall Stewart 		return (0);
1059f8829a4aSRandall Stewart 	}
1060f8829a4aSRandall Stewart 	/* Ok we found the cookie, threshold management next */
1061f8829a4aSRandall Stewart 	if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1062f8829a4aSRandall Stewart 	    stcb->asoc.max_init_times)) {
1063f8829a4aSRandall Stewart 		/* Assoc is over */
1064f8829a4aSRandall Stewart 		return (1);
1065f8829a4aSRandall Stewart 	}
1066f8829a4aSRandall Stewart 	/*
1067f8829a4aSRandall Stewart 	 * cleared theshold management now lets backoff the address & select
1068f8829a4aSRandall Stewart 	 * an alternate
1069f8829a4aSRandall Stewart 	 */
1070f8829a4aSRandall Stewart 	stcb->asoc.dropped_special_cnt = 0;
107144fbe462SRandall Stewart 	sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0, 0);
1072f8829a4aSRandall Stewart 	alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1073f8829a4aSRandall Stewart 	if (alt != cookie->whoTo) {
1074f8829a4aSRandall Stewart 		sctp_free_remote_addr(cookie->whoTo);
1075f8829a4aSRandall Stewart 		cookie->whoTo = alt;
1076f8829a4aSRandall Stewart 		atomic_add_int(&alt->ref_count, 1);
1077f8829a4aSRandall Stewart 	}
1078f8829a4aSRandall Stewart 	/* Now mark the retran info */
1079f8829a4aSRandall Stewart 	if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1080f8829a4aSRandall Stewart 		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1081f8829a4aSRandall Stewart 	}
1082f8829a4aSRandall Stewart 	cookie->sent = SCTP_DATAGRAM_RESEND;
1083f8829a4aSRandall Stewart 	/*
1084f8829a4aSRandall Stewart 	 * Now call the output routine to kick out the cookie again, Note we
1085f8829a4aSRandall Stewart 	 * don't mark any chunks for retran so that FR will need to kick in
1086f8829a4aSRandall Stewart 	 * to move these (or a send timer).
1087f8829a4aSRandall Stewart 	 */
1088f8829a4aSRandall Stewart 	return (0);
1089f8829a4aSRandall Stewart }
1090f8829a4aSRandall Stewart 
1091f8829a4aSRandall Stewart int
1092f8829a4aSRandall Stewart sctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1093f8829a4aSRandall Stewart     struct sctp_nets *net)
1094f8829a4aSRandall Stewart {
1095f8829a4aSRandall Stewart 	struct sctp_nets *alt;
1096f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1097f8829a4aSRandall Stewart 
1098f8829a4aSRandall Stewart 	if (stcb->asoc.stream_reset_outstanding == 0) {
1099f8829a4aSRandall Stewart 		return (0);
1100f8829a4aSRandall Stewart 	}
1101f8829a4aSRandall Stewart 	/* find the existing STRRESET, we use the seq number we sent out on */
1102ad81507eSRandall Stewart 	(void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1103f8829a4aSRandall Stewart 	if (strrst == NULL) {
1104f8829a4aSRandall Stewart 		return (0);
1105f8829a4aSRandall Stewart 	}
1106f8829a4aSRandall Stewart 	/* do threshold management */
1107f8829a4aSRandall Stewart 	if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1108f8829a4aSRandall Stewart 	    stcb->asoc.max_send_times)) {
1109f8829a4aSRandall Stewart 		/* Assoc is over */
1110f8829a4aSRandall Stewart 		return (1);
1111f8829a4aSRandall Stewart 	}
1112f8829a4aSRandall Stewart 	/*
1113f8829a4aSRandall Stewart 	 * cleared theshold management now lets backoff the address & select
1114f8829a4aSRandall Stewart 	 * an alternate
1115f8829a4aSRandall Stewart 	 */
111644fbe462SRandall Stewart 	sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0, 0);
1117f8829a4aSRandall Stewart 	alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
1118f8829a4aSRandall Stewart 	sctp_free_remote_addr(strrst->whoTo);
1119f8829a4aSRandall Stewart 	strrst->whoTo = alt;
1120f8829a4aSRandall Stewart 	atomic_add_int(&alt->ref_count, 1);
1121f8829a4aSRandall Stewart 
1122f8829a4aSRandall Stewart 	/* See if a ECN Echo is also stranded */
1123f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1124f8829a4aSRandall Stewart 		if ((chk->whoTo == net) &&
1125f8829a4aSRandall Stewart 		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1126f8829a4aSRandall Stewart 			sctp_free_remote_addr(chk->whoTo);
1127f8829a4aSRandall Stewart 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
1128f8829a4aSRandall Stewart 				chk->sent = SCTP_DATAGRAM_RESEND;
1129f8829a4aSRandall Stewart 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1130f8829a4aSRandall Stewart 			}
1131f8829a4aSRandall Stewart 			chk->whoTo = alt;
1132f8829a4aSRandall Stewart 			atomic_add_int(&alt->ref_count, 1);
1133f8829a4aSRandall Stewart 		}
1134f8829a4aSRandall Stewart 	}
1135ca85e948SMichael Tuexen 	if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
1136f8829a4aSRandall Stewart 		/*
1137f8829a4aSRandall Stewart 		 * If the address went un-reachable, we need to move to
1138f8829a4aSRandall Stewart 		 * alternates for ALL chk's in queue
1139f8829a4aSRandall Stewart 		 */
11409eea4a2dSMichael Tuexen 		sctp_move_chunks_from_net(stcb, net);
1141f8829a4aSRandall Stewart 	}
1142f8829a4aSRandall Stewart 	/* mark the retran info */
1143f8829a4aSRandall Stewart 	if (strrst->sent != SCTP_DATAGRAM_RESEND)
1144f8829a4aSRandall Stewart 		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1145f8829a4aSRandall Stewart 	strrst->sent = SCTP_DATAGRAM_RESEND;
1146f8829a4aSRandall Stewart 
1147f8829a4aSRandall Stewart 	/* restart the timer */
1148f8829a4aSRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1149f8829a4aSRandall Stewart 	return (0);
1150f8829a4aSRandall Stewart }
1151f8829a4aSRandall Stewart 
1152f8829a4aSRandall Stewart int
1153f8829a4aSRandall Stewart sctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1154f8829a4aSRandall Stewart     struct sctp_nets *net)
1155f8829a4aSRandall Stewart {
1156f8829a4aSRandall Stewart 	struct sctp_nets *alt;
11574a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *asconf, *chk;
1158f8829a4aSRandall Stewart 
11591b649582SRandall Stewart 	/* is this a first send, or a retransmission? */
1160c54a18d2SRandall Stewart 	if (TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) {
1161f8829a4aSRandall Stewart 		/* compose a new ASCONF chunk and send it */
11623232788eSRandall Stewart 		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1163f8829a4aSRandall Stewart 	} else {
11641b649582SRandall Stewart 		/*
11651b649582SRandall Stewart 		 * Retransmission of the existing ASCONF is needed
11661b649582SRandall Stewart 		 */
1167f8829a4aSRandall Stewart 
1168f8829a4aSRandall Stewart 		/* find the existing ASCONF */
1169c54a18d2SRandall Stewart 		asconf = TAILQ_FIRST(&stcb->asoc.asconf_send_queue);
1170f8829a4aSRandall Stewart 		if (asconf == NULL) {
1171f8829a4aSRandall Stewart 			return (0);
1172f8829a4aSRandall Stewart 		}
1173f8829a4aSRandall Stewart 		/* do threshold management */
1174f8829a4aSRandall Stewart 		if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1175f8829a4aSRandall Stewart 		    stcb->asoc.max_send_times)) {
1176f8829a4aSRandall Stewart 			/* Assoc is over */
1177f8829a4aSRandall Stewart 			return (1);
1178f8829a4aSRandall Stewart 		}
1179f8829a4aSRandall Stewart 		if (asconf->snd_count > stcb->asoc.max_send_times) {
1180f8829a4aSRandall Stewart 			/*
11811b649582SRandall Stewart 			 * Something is rotten: our peer is not responding
11821b649582SRandall Stewart 			 * to ASCONFs but apparently is to other chunks.
11831b649582SRandall Stewart 			 * i.e. it is not properly handling the chunk type
11841b649582SRandall Stewart 			 * upper bits. Mark this peer as ASCONF incapable
11851b649582SRandall Stewart 			 * and cleanup.
1186f8829a4aSRandall Stewart 			 */
1187ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1188f8829a4aSRandall Stewart 			sctp_asconf_cleanup(stcb, net);
1189f8829a4aSRandall Stewart 			return (0);
1190f8829a4aSRandall Stewart 		}
1191f8829a4aSRandall Stewart 		/*
11921b649582SRandall Stewart 		 * cleared threshold management, so now backoff the net and
11931b649582SRandall Stewart 		 * select an alternate
1194f8829a4aSRandall Stewart 		 */
119544fbe462SRandall Stewart 		sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0, 0);
1196f8829a4aSRandall Stewart 		alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
1197c54a18d2SRandall Stewart 		if (asconf->whoTo != alt) {
1198f8829a4aSRandall Stewart 			sctp_free_remote_addr(asconf->whoTo);
1199f8829a4aSRandall Stewart 			asconf->whoTo = alt;
1200f8829a4aSRandall Stewart 			atomic_add_int(&alt->ref_count, 1);
1201c54a18d2SRandall Stewart 		}
12021b649582SRandall Stewart 		/* See if an ECN Echo is also stranded */
1203f8829a4aSRandall Stewart 		TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1204f8829a4aSRandall Stewart 			if ((chk->whoTo == net) &&
1205f8829a4aSRandall Stewart 			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1206f8829a4aSRandall Stewart 				sctp_free_remote_addr(chk->whoTo);
1207f8829a4aSRandall Stewart 				chk->whoTo = alt;
1208f8829a4aSRandall Stewart 				if (chk->sent != SCTP_DATAGRAM_RESEND) {
1209f8829a4aSRandall Stewart 					chk->sent = SCTP_DATAGRAM_RESEND;
1210f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1211f8829a4aSRandall Stewart 				}
1212f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
1213f8829a4aSRandall Stewart 			}
1214f8829a4aSRandall Stewart 		}
12154a9ef3f8SMichael Tuexen 		TAILQ_FOREACH(chk, &stcb->asoc.asconf_send_queue, sctp_next) {
1216c54a18d2SRandall Stewart 			if (chk->whoTo != alt) {
1217c54a18d2SRandall Stewart 				sctp_free_remote_addr(chk->whoTo);
1218c54a18d2SRandall Stewart 				chk->whoTo = alt;
1219c54a18d2SRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
1220c54a18d2SRandall Stewart 			}
1221c54a18d2SRandall Stewart 			if (asconf->sent != SCTP_DATAGRAM_RESEND && chk->sent != SCTP_DATAGRAM_UNSENT)
1222c54a18d2SRandall Stewart 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1223c54a18d2SRandall Stewart 			chk->sent = SCTP_DATAGRAM_RESEND;
1224c54a18d2SRandall Stewart 		}
1225ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
1226f8829a4aSRandall Stewart 			/*
1227f8829a4aSRandall Stewart 			 * If the address went un-reachable, we need to move
12281b649582SRandall Stewart 			 * to the alternate for ALL chunks in queue
1229f8829a4aSRandall Stewart 			 */
12309eea4a2dSMichael Tuexen 			sctp_move_chunks_from_net(stcb, net);
1231f8829a4aSRandall Stewart 		}
1232f8829a4aSRandall Stewart 		/* mark the retran info */
1233f8829a4aSRandall Stewart 		if (asconf->sent != SCTP_DATAGRAM_RESEND)
1234f8829a4aSRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1235f8829a4aSRandall Stewart 		asconf->sent = SCTP_DATAGRAM_RESEND;
1236c54a18d2SRandall Stewart 
1237c54a18d2SRandall Stewart 		/* send another ASCONF if any and we can do */
1238c54a18d2SRandall Stewart 		sctp_send_asconf(stcb, alt, SCTP_ADDR_NOT_LOCKED);
1239f8829a4aSRandall Stewart 	}
1240f8829a4aSRandall Stewart 	return (0);
1241f8829a4aSRandall Stewart }
1242f8829a4aSRandall Stewart 
1243851b7298SRandall Stewart /* Mobility adaptation */
124404ee05e8SRandall Stewart void
1245851b7298SRandall Stewart sctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
12467215cc1bSMichael Tuexen     struct sctp_nets *net SCTP_UNUSED)
1247851b7298SRandall Stewart {
1248851b7298SRandall Stewart 	if (stcb->asoc.deleted_primary == NULL) {
1249851b7298SRandall Stewart 		SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n");
1250851b7298SRandall Stewart 		sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
125104ee05e8SRandall Stewart 		return;
1252851b7298SRandall Stewart 	}
1253851b7298SRandall Stewart 	SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary ");
1254851b7298SRandall Stewart 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1255851b7298SRandall Stewart 	sctp_free_remote_addr(stcb->asoc.deleted_primary);
1256851b7298SRandall Stewart 	stcb->asoc.deleted_primary = NULL;
1257851b7298SRandall Stewart 	sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
125804ee05e8SRandall Stewart 	return;
1259851b7298SRandall Stewart }
1260851b7298SRandall Stewart 
1261f8829a4aSRandall Stewart /*
1262f8829a4aSRandall Stewart  * For the shutdown and shutdown-ack, we do not keep one around on the
1263f8829a4aSRandall Stewart  * control queue. This means we must generate a new one and call the general
1264f8829a4aSRandall Stewart  * chunk output routine, AFTER having done threshold management.
1265b61c3588SMichael Tuexen  * It is assumed that net is non-NULL.
1266f8829a4aSRandall Stewart  */
1267f8829a4aSRandall Stewart int
1268f8829a4aSRandall Stewart sctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1269f8829a4aSRandall Stewart     struct sctp_nets *net)
1270f8829a4aSRandall Stewart {
1271f8829a4aSRandall Stewart 	struct sctp_nets *alt;
1272f8829a4aSRandall Stewart 
1273f8829a4aSRandall Stewart 	/* first threshold managment */
1274f8829a4aSRandall Stewart 	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1275f8829a4aSRandall Stewart 		/* Assoc is over */
1276f8829a4aSRandall Stewart 		return (1);
1277f8829a4aSRandall Stewart 	}
1278b61c3588SMichael Tuexen 	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1279f8829a4aSRandall Stewart 	/* second select an alternative */
1280f8829a4aSRandall Stewart 	alt = sctp_find_alternate_net(stcb, net, 0);
1281f8829a4aSRandall Stewart 
1282f8829a4aSRandall Stewart 	/* third generate a shutdown into the queue for out net */
1283f8829a4aSRandall Stewart 	sctp_send_shutdown(stcb, alt);
1284b61c3588SMichael Tuexen 
1285f8829a4aSRandall Stewart 	/* fourth restart timer */
1286f8829a4aSRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1287f8829a4aSRandall Stewart 	return (0);
1288f8829a4aSRandall Stewart }
1289f8829a4aSRandall Stewart 
1290f8829a4aSRandall Stewart int
1291f8829a4aSRandall Stewart sctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1292f8829a4aSRandall Stewart     struct sctp_nets *net)
1293f8829a4aSRandall Stewart {
1294f8829a4aSRandall Stewart 	struct sctp_nets *alt;
1295f8829a4aSRandall Stewart 
1296f8829a4aSRandall Stewart 	/* first threshold managment */
1297f8829a4aSRandall Stewart 	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1298f8829a4aSRandall Stewart 		/* Assoc is over */
1299f8829a4aSRandall Stewart 		return (1);
1300f8829a4aSRandall Stewart 	}
1301b61c3588SMichael Tuexen 	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1302f8829a4aSRandall Stewart 	/* second select an alternative */
1303f8829a4aSRandall Stewart 	alt = sctp_find_alternate_net(stcb, net, 0);
1304f8829a4aSRandall Stewart 
1305f8829a4aSRandall Stewart 	/* third generate a shutdown into the queue for out net */
1306f8829a4aSRandall Stewart 	sctp_send_shutdown_ack(stcb, alt);
1307f8829a4aSRandall Stewart 
1308f8829a4aSRandall Stewart 	/* fourth restart timer */
1309f8829a4aSRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1310f8829a4aSRandall Stewart 	return (0);
1311f8829a4aSRandall Stewart }
1312f8829a4aSRandall Stewart 
1313f8829a4aSRandall Stewart static void
1314f8829a4aSRandall Stewart sctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1315f8829a4aSRandall Stewart     struct sctp_tcb *stcb)
1316f8829a4aSRandall Stewart {
1317f8829a4aSRandall Stewart 	struct sctp_stream_queue_pending *sp;
1318f7a77f6fSMichael Tuexen 	unsigned int i, chks_in_queue = 0;
1319f8829a4aSRandall Stewart 	int being_filled = 0;
1320f8829a4aSRandall Stewart 
1321f8829a4aSRandall Stewart 	/*
1322f8829a4aSRandall Stewart 	 * This function is ONLY called when the send/sent queues are empty.
1323f8829a4aSRandall Stewart 	 */
1324f8829a4aSRandall Stewart 	if ((stcb == NULL) || (inp == NULL))
1325f8829a4aSRandall Stewart 		return;
1326f8829a4aSRandall Stewart 
1327f8829a4aSRandall Stewart 	if (stcb->asoc.sent_queue_retran_cnt) {
1328ad81507eSRandall Stewart 		SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1329f8829a4aSRandall Stewart 		    stcb->asoc.sent_queue_retran_cnt);
1330f8829a4aSRandall Stewart 		stcb->asoc.sent_queue_retran_cnt = 0;
1331f8829a4aSRandall Stewart 	}
1332f7a77f6fSMichael Tuexen 	if (stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1333252f7f93SMichael Tuexen 		/* No stream scheduler information, initialize scheduler */
1334252f7f93SMichael Tuexen 		stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 0);
1335252f7f93SMichael Tuexen 		if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1336252f7f93SMichael Tuexen 			/* yep, we lost a stream or two */
1337252f7f93SMichael Tuexen 			SCTP_PRINTF("Found additional streams NOT managed by scheduler, corrected\n");
1338f8829a4aSRandall Stewart 		} else {
1339252f7f93SMichael Tuexen 			/* no streams lost */
1340f8829a4aSRandall Stewart 			stcb->asoc.total_output_queue_size = 0;
1341f8829a4aSRandall Stewart 		}
1342f8829a4aSRandall Stewart 	}
1343f8829a4aSRandall Stewart 	/* Check to see if some data queued, if so report it */
1344f7a77f6fSMichael Tuexen 	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1345f7a77f6fSMichael Tuexen 		if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1346f7a77f6fSMichael Tuexen 			TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
1347f8829a4aSRandall Stewart 				if (sp->msg_is_complete)
1348f8829a4aSRandall Stewart 					being_filled++;
1349f8829a4aSRandall Stewart 				chks_in_queue++;
1350f8829a4aSRandall Stewart 			}
1351f8829a4aSRandall Stewart 		}
1352f8829a4aSRandall Stewart 	}
1353f8829a4aSRandall Stewart 	if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1354ad81507eSRandall Stewart 		SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1355f8829a4aSRandall Stewart 		    stcb->asoc.stream_queue_cnt, chks_in_queue);
1356f8829a4aSRandall Stewart 	}
1357f8829a4aSRandall Stewart 	if (chks_in_queue) {
1358f8829a4aSRandall Stewart 		/* call the output queue function */
1359ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1360f8829a4aSRandall Stewart 		if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1361f8829a4aSRandall Stewart 		    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1362f8829a4aSRandall Stewart 			/*
1363f8829a4aSRandall Stewart 			 * Probably should go in and make it go back through
1364f8829a4aSRandall Stewart 			 * and add fragments allowed
1365f8829a4aSRandall Stewart 			 */
1366f8829a4aSRandall Stewart 			if (being_filled == 0) {
1367ad81507eSRandall Stewart 				SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
1368f8829a4aSRandall Stewart 				    chks_in_queue);
1369f8829a4aSRandall Stewart 			}
1370f8829a4aSRandall Stewart 		}
1371f8829a4aSRandall Stewart 	} else {
1372ad81507eSRandall Stewart 		SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
1373f8829a4aSRandall Stewart 		    (u_long)stcb->asoc.total_output_queue_size);
1374f8829a4aSRandall Stewart 		stcb->asoc.total_output_queue_size = 0;
1375f8829a4aSRandall Stewart 	}
1376f8829a4aSRandall Stewart }
1377f8829a4aSRandall Stewart 
1378f8829a4aSRandall Stewart int
1379f8829a4aSRandall Stewart sctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1380ca85e948SMichael Tuexen     struct sctp_nets *net)
1381f8829a4aSRandall Stewart {
1382ca85e948SMichael Tuexen 	uint8_t net_was_pf;
1383b54d3a6cSRandall Stewart 
1384ca85e948SMichael Tuexen 	if (net->dest_state & SCTP_ADDR_PF) {
1385ca85e948SMichael Tuexen 		net_was_pf = 1;
138660990c0cSMichael Tuexen 	} else {
138760990c0cSMichael Tuexen 		net_was_pf = 0;
1388ca85e948SMichael Tuexen 	}
1389f8829a4aSRandall Stewart 	if (net->hb_responded == 0) {
139042551e99SRandall Stewart 		if (net->ro._s_addr) {
139142551e99SRandall Stewart 			/*
139260990c0cSMichael Tuexen 			 * Invalidate the src address if we did not get a
139360990c0cSMichael Tuexen 			 * response last time.
139442551e99SRandall Stewart 			 */
139542551e99SRandall Stewart 			sctp_free_ifa(net->ro._s_addr);
139642551e99SRandall Stewart 			net->ro._s_addr = NULL;
139742551e99SRandall Stewart 			net->src_addr_selected = 0;
139842551e99SRandall Stewart 		}
139944fbe462SRandall Stewart 		sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1400ca85e948SMichael Tuexen 		if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1401ca85e948SMichael Tuexen 			/* Assoc is over */
1402ca85e948SMichael Tuexen 			return (1);
1403ca85e948SMichael Tuexen 		}
1404f8829a4aSRandall Stewart 	}
1405f8829a4aSRandall Stewart 	/* Zero PBA, if it needs it */
1406f8829a4aSRandall Stewart 	if (net->partial_bytes_acked) {
1407f8829a4aSRandall Stewart 		net->partial_bytes_acked = 0;
1408f8829a4aSRandall Stewart 	}
1409f8829a4aSRandall Stewart 	if ((stcb->asoc.total_output_queue_size > 0) &&
1410f8829a4aSRandall Stewart 	    (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1411f8829a4aSRandall Stewart 	    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1412f8829a4aSRandall Stewart 		sctp_audit_stream_queues_for_size(inp, stcb);
1413f8829a4aSRandall Stewart 	}
1414ca85e948SMichael Tuexen 	if (!(net->dest_state & SCTP_ADDR_NOHB) &&
1415ca85e948SMichael Tuexen 	    !((net_was_pf == 0) && (net->dest_state & SCTP_ADDR_PF))) {
1416f8829a4aSRandall Stewart 		/*
1417ca85e948SMichael Tuexen 		 * when move to PF during threshold mangement, a HB has been
1418ca85e948SMichael Tuexen 		 * queued in that routine
1419f8829a4aSRandall Stewart 		 */
1420ca7567c9SMichael Tuexen 		uint32_t ms_gone_by;
1421ca7567c9SMichael Tuexen 
1422ca7567c9SMichael Tuexen 		if ((net->last_sent_time.tv_sec > 0) ||
1423ca7567c9SMichael Tuexen 		    (net->last_sent_time.tv_usec > 0)) {
1424ca7567c9SMichael Tuexen 			struct timeval diff;
1425ca7567c9SMichael Tuexen 
1426ca7567c9SMichael Tuexen 			SCTP_GETTIME_TIMEVAL(&diff);
1427ca7567c9SMichael Tuexen 			timevalsub(&diff, &net->last_sent_time);
1428ca7567c9SMichael Tuexen 			ms_gone_by = (uint32_t) (diff.tv_sec * 1000) +
1429ca7567c9SMichael Tuexen 			    (uint32_t) (diff.tv_usec / 1000);
1430ca7567c9SMichael Tuexen 		} else {
1431ca7567c9SMichael Tuexen 			ms_gone_by = 0xffffffff;
1432ca7567c9SMichael Tuexen 		}
14334dca0ef4SMichael Tuexen 		if ((ms_gone_by >= net->heart_beat_delay) ||
14344dca0ef4SMichael Tuexen 		    (net->dest_state & SCTP_ADDR_PF)) {
1435ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
1436f8829a4aSRandall Stewart 		}
1437ca7567c9SMichael Tuexen 	}
1438f8829a4aSRandall Stewart 	return (0);
1439f8829a4aSRandall Stewart }
1440f8829a4aSRandall Stewart 
1441f8829a4aSRandall Stewart void
1442f8829a4aSRandall Stewart sctp_pathmtu_timer(struct sctp_inpcb *inp,
1443f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
1444f8829a4aSRandall Stewart     struct sctp_nets *net)
1445f8829a4aSRandall Stewart {
1446c54a18d2SRandall Stewart 	uint32_t next_mtu, mtu;
1447f8829a4aSRandall Stewart 
14487215cc1bSMichael Tuexen 	next_mtu = sctp_get_next_mtu(net->mtu);
144917205eccSRandall Stewart 
1450c54a18d2SRandall Stewart 	if ((next_mtu > net->mtu) && (net->port == 0)) {
145117205eccSRandall Stewart 		if ((net->src_addr_selected == 0) ||
145217205eccSRandall Stewart 		    (net->ro._s_addr == NULL) ||
145317205eccSRandall Stewart 		    (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1454ad81507eSRandall Stewart 			if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
145517205eccSRandall Stewart 				sctp_free_ifa(net->ro._s_addr);
145617205eccSRandall Stewart 				net->ro._s_addr = NULL;
145717205eccSRandall Stewart 				net->src_addr_selected = 0;
1458ad81507eSRandall Stewart 			} else if (net->ro._s_addr == NULL) {
1459c54a18d2SRandall Stewart #if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1460c54a18d2SRandall Stewart 				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1461c54a18d2SRandall Stewart 					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1462c54a18d2SRandall Stewart 
1463c54a18d2SRandall Stewart 					/* KAME hack: embed scopeid */
1464482444b4SRandall Stewart 					(void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
1465c54a18d2SRandall Stewart 				}
1466c54a18d2SRandall Stewart #endif
1467c54a18d2SRandall Stewart 
146817205eccSRandall Stewart 				net->ro._s_addr = sctp_source_address_selection(inp,
146917205eccSRandall Stewart 				    stcb,
147017205eccSRandall Stewart 				    (sctp_route_t *) & net->ro,
147117205eccSRandall Stewart 				    net, 0, stcb->asoc.vrf_id);
1472c54a18d2SRandall Stewart #if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1473c54a18d2SRandall Stewart 				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1474c54a18d2SRandall Stewart 					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1475c54a18d2SRandall Stewart 
1476c54a18d2SRandall Stewart 					(void)sa6_recoverscope(sin6);
1477c54a18d2SRandall Stewart 				}
1478c54a18d2SRandall Stewart #endif				/* INET6 */
1479ad81507eSRandall Stewart 			}
148017205eccSRandall Stewart 			if (net->ro._s_addr)
148117205eccSRandall Stewart 				net->src_addr_selected = 1;
148217205eccSRandall Stewart 		}
148317205eccSRandall Stewart 		if (net->ro._s_addr) {
148417205eccSRandall Stewart 			mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
1485*4474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
1486830d754dSRandall Stewart 			if (net->port) {
1487830d754dSRandall Stewart 				mtu -= sizeof(struct udphdr);
1488830d754dSRandall Stewart 			}
1489*4474d71aSMichael Tuexen #endif
149017205eccSRandall Stewart 			if (mtu > next_mtu) {
1491f8829a4aSRandall Stewart 				net->mtu = next_mtu;
1492f8829a4aSRandall Stewart 			}
1493f8829a4aSRandall Stewart 		}
1494f8829a4aSRandall Stewart 	}
1495f8829a4aSRandall Stewart 	/* restart the timer */
1496f8829a4aSRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1497f8829a4aSRandall Stewart }
1498f8829a4aSRandall Stewart 
1499f8829a4aSRandall Stewart void
1500f8829a4aSRandall Stewart sctp_autoclose_timer(struct sctp_inpcb *inp,
1501f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
1502f8829a4aSRandall Stewart     struct sctp_nets *net)
1503f8829a4aSRandall Stewart {
1504f8829a4aSRandall Stewart 	struct timeval tn, *tim_touse;
1505f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1506f8829a4aSRandall Stewart 	int ticks_gone_by;
1507f8829a4aSRandall Stewart 
15086e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&tn);
1509f8829a4aSRandall Stewart 	if (stcb->asoc.sctp_autoclose_ticks &&
1510f8829a4aSRandall Stewart 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1511f8829a4aSRandall Stewart 		/* Auto close is on */
1512f8829a4aSRandall Stewart 		asoc = &stcb->asoc;
1513f8829a4aSRandall Stewart 		/* pick the time to use */
1514f8829a4aSRandall Stewart 		if (asoc->time_last_rcvd.tv_sec >
1515f8829a4aSRandall Stewart 		    asoc->time_last_sent.tv_sec) {
1516f8829a4aSRandall Stewart 			tim_touse = &asoc->time_last_rcvd;
1517f8829a4aSRandall Stewart 		} else {
1518f8829a4aSRandall Stewart 			tim_touse = &asoc->time_last_sent;
1519f8829a4aSRandall Stewart 		}
1520f8829a4aSRandall Stewart 		/* Now has long enough transpired to autoclose? */
1521f8829a4aSRandall Stewart 		ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
1522f8829a4aSRandall Stewart 		if ((ticks_gone_by > 0) &&
1523f8829a4aSRandall Stewart 		    (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1524f8829a4aSRandall Stewart 			/*
1525f8829a4aSRandall Stewart 			 * autoclose time has hit, call the output routine,
1526f8829a4aSRandall Stewart 			 * which should do nothing just to be SURE we don't
1527f8829a4aSRandall Stewart 			 * have hanging data. We can then safely check the
1528f8829a4aSRandall Stewart 			 * queues and know that we are clear to send
1529f8829a4aSRandall Stewart 			 * shutdown
1530f8829a4aSRandall Stewart 			 */
1531ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1532f8829a4aSRandall Stewart 			/* Are we clean? */
1533f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue) &&
1534f8829a4aSRandall Stewart 			    TAILQ_EMPTY(&asoc->sent_queue)) {
1535f8829a4aSRandall Stewart 				/*
1536f8829a4aSRandall Stewart 				 * there is nothing queued to send, so I'm
1537f8829a4aSRandall Stewart 				 * done...
1538f8829a4aSRandall Stewart 				 */
1539f42a358aSRandall Stewart 				if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1540f8829a4aSRandall Stewart 					/* only send SHUTDOWN 1st time thru */
1541ca85e948SMichael Tuexen 					struct sctp_nets *netp;
1542ca85e948SMichael Tuexen 
1543f42a358aSRandall Stewart 					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1544f42a358aSRandall Stewart 					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1545f8829a4aSRandall Stewart 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1546f42a358aSRandall Stewart 					}
1547c4739e2fSRandall Stewart 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1548b201f536SRandall Stewart 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1549c39cfa1fSMichael Tuexen 					sctp_stop_timers_for_shutdown(stcb);
1550c39cfa1fSMichael Tuexen 					if (stcb->asoc.alternate) {
1551c39cfa1fSMichael Tuexen 						netp = stcb->asoc.alternate;
1552c39cfa1fSMichael Tuexen 					} else {
1553c39cfa1fSMichael Tuexen 						netp = stcb->asoc.primary_destination;
1554c39cfa1fSMichael Tuexen 					}
1555c39cfa1fSMichael Tuexen 					sctp_send_shutdown(stcb, netp);
1556f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1557f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb,
1558ca85e948SMichael Tuexen 					    netp);
1559f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1560f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb,
1561ca85e948SMichael Tuexen 					    netp);
1562f8829a4aSRandall Stewart 				}
1563f8829a4aSRandall Stewart 			}
1564f8829a4aSRandall Stewart 		} else {
1565f8829a4aSRandall Stewart 			/*
1566f8829a4aSRandall Stewart 			 * No auto close at this time, reset t-o to check
1567f8829a4aSRandall Stewart 			 * later
1568f8829a4aSRandall Stewart 			 */
1569f8829a4aSRandall Stewart 			int tmp;
1570f8829a4aSRandall Stewart 
1571f8829a4aSRandall Stewart 			/* fool the timer startup to use the time left */
1572f8829a4aSRandall Stewart 			tmp = asoc->sctp_autoclose_ticks;
1573f8829a4aSRandall Stewart 			asoc->sctp_autoclose_ticks -= ticks_gone_by;
1574f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1575f8829a4aSRandall Stewart 			    net);
1576f8829a4aSRandall Stewart 			/* restore the real tick value */
1577f8829a4aSRandall Stewart 			asoc->sctp_autoclose_ticks = tmp;
1578f8829a4aSRandall Stewart 		}
1579f8829a4aSRandall Stewart 	}
1580f8829a4aSRandall Stewart }
1581