sctp_timer.c revision 169382
1/*-
2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * a) Redistributions of source code must retain the above copyright notice,
8 *   this list of conditions and the following disclaimer.
9 *
10 * b) Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *   the documentation and/or other materials provided with the distribution.
13 *
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 *    contributors may be used to endorse or promote products derived
16 *    from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/* $KAME: sctp_timer.c,v 1.29 2005/03/06 16:04:18 itojun Exp $	 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/netinet/sctp_timer.c 169382 2007-05-08 17:01:12Z rrs $");
35
36#define _IP_VHL
37#include <netinet/sctp_os.h>
38#include <netinet/sctp_pcb.h>
39#ifdef INET6
40#include <netinet6/sctp6_var.h>
41#endif
42#include <netinet/sctp_var.h>
43#include <netinet/sctp_sysctl.h>
44#include <netinet/sctp_timer.h>
45#include <netinet/sctputil.h>
46#include <netinet/sctp_output.h>
47#include <netinet/sctp_header.h>
48#include <netinet/sctp_indata.h>
49#include <netinet/sctp_asconf.h>
50#include <netinet/sctp_input.h>
51#include <netinet/sctp.h>
52#include <netinet/sctp_uio.h>
53
54
55
56void
57sctp_early_fr_timer(struct sctp_inpcb *inp,
58    struct sctp_tcb *stcb,
59    struct sctp_nets *net)
60{
61	struct sctp_tmit_chunk *chk, *tp2;
62	struct timeval now, min_wait, tv;
63	unsigned int cur_rtt, cnt = 0, cnt_resend = 0;
64
65	/* an early FR is occuring. */
66	(void)SCTP_GETTIME_TIMEVAL(&now);
67	/* get cur rto in micro-seconds */
68	if (net->lastsa == 0) {
69		/* Hmm no rtt estimate yet? */
70		cur_rtt = stcb->asoc.initial_rto >> 2;
71	} else {
72
73		cur_rtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
74	}
75	if (cur_rtt < sctp_early_fr_msec) {
76		cur_rtt = sctp_early_fr_msec;
77	}
78	cur_rtt *= 1000;
79	tv.tv_sec = cur_rtt / 1000000;
80	tv.tv_usec = cur_rtt % 1000000;
81	min_wait = now;
82	timevalsub(&min_wait, &tv);
83	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
84		/*
85		 * if we hit here, we don't have enough seconds on the clock
86		 * to account for the RTO. We just let the lower seconds be
87		 * the bounds and don't worry about it. This may mean we
88		 * will mark a lot more than we should.
89		 */
90		min_wait.tv_sec = min_wait.tv_usec = 0;
91	}
92	chk = TAILQ_LAST(&stcb->asoc.sent_queue, sctpchunk_listhead);
93	for (; chk != NULL; chk = tp2) {
94		tp2 = TAILQ_PREV(chk, sctpchunk_listhead, sctp_next);
95		if (chk->whoTo != net) {
96			continue;
97		}
98		if (chk->sent == SCTP_DATAGRAM_RESEND)
99			cnt_resend++;
100		else if ((chk->sent > SCTP_DATAGRAM_UNSENT) &&
101		    (chk->sent < SCTP_DATAGRAM_RESEND)) {
102			/* pending, may need retran */
103			if (chk->sent_rcv_time.tv_sec > min_wait.tv_sec) {
104				/*
105				 * we have reached a chunk that was sent
106				 * some seconds past our min.. forget it we
107				 * will find no more to send.
108				 */
109				continue;
110			} else if (chk->sent_rcv_time.tv_sec == min_wait.tv_sec) {
111				/*
112				 * we must look at the micro seconds to
113				 * know.
114				 */
115				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
116					/*
117					 * ok it was sent after our boundary
118					 * time.
119					 */
120					continue;
121				}
122			}
123#ifdef SCTP_EARLYFR_LOGGING
124			sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
125			    4, SCTP_FR_MARKED_EARLY);
126#endif
127			SCTP_STAT_INCR(sctps_earlyfrmrkretrans);
128			chk->sent = SCTP_DATAGRAM_RESEND;
129			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
130			/* double book size since we are doing an early FR */
131			chk->book_size_scale++;
132			cnt += chk->send_size;
133			if ((cnt + net->flight_size) > net->cwnd) {
134				/* Mark all we could possibly resend */
135				break;
136			}
137		}
138	}
139	if (cnt) {
140#ifdef SCTP_CWND_MONITOR
141		int old_cwnd;
142
143		old_cwnd = net->cwnd;
144#endif
145		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_EARLY_FR_TMR);
146		/*
147		 * make a small adjustment to cwnd and force to CA.
148		 */
149
150		if (net->cwnd > net->mtu)
151			/* drop down one MTU after sending */
152			net->cwnd -= net->mtu;
153		if (net->cwnd < net->ssthresh)
154			/* still in SS move to CA */
155			net->ssthresh = net->cwnd - 1;
156#ifdef SCTP_CWND_MONITOR
157		sctp_log_cwnd(stcb, net, (old_cwnd - net->cwnd), SCTP_CWND_LOG_FROM_FR);
158#endif
159	} else if (cnt_resend) {
160		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_EARLY_FR_TMR);
161	}
162	/* Restart it? */
163	if (net->flight_size < net->cwnd) {
164		SCTP_STAT_INCR(sctps_earlyfrstrtmr);
165		sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net);
166	}
167}
168
169void
170sctp_audit_retranmission_queue(struct sctp_association *asoc)
171{
172	struct sctp_tmit_chunk *chk;
173
174#ifdef SCTP_DEBUG
175	if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
176		printf("Audit invoked on send queue cnt:%d onqueue:%d\n",
177		    asoc->sent_queue_retran_cnt,
178		    asoc->sent_queue_cnt);
179	}
180#endif				/* SCTP_DEBUG */
181	asoc->sent_queue_retran_cnt = 0;
182	asoc->sent_queue_cnt = 0;
183	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
184		if (chk->sent == SCTP_DATAGRAM_RESEND) {
185			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
186		}
187		asoc->sent_queue_cnt++;
188	}
189	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
190		if (chk->sent == SCTP_DATAGRAM_RESEND) {
191			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
192		}
193	}
194#ifdef SCTP_DEBUG
195	if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
196		printf("Audit completes retran:%d onqueue:%d\n",
197		    asoc->sent_queue_retran_cnt,
198		    asoc->sent_queue_cnt);
199	}
200#endif				/* SCTP_DEBUG */
201}
202
203int
204sctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
205    struct sctp_nets *net, uint16_t threshold)
206{
207	if (net) {
208		net->error_count++;
209#ifdef SCTP_DEBUG
210		if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
211			printf("Error count for %p now %d thresh:%d\n",
212			    net, net->error_count,
213			    net->failure_threshold);
214		}
215#endif				/* SCTP_DEBUG */
216		if (net->error_count > net->failure_threshold) {
217			/* We had a threshold failure */
218			if (net->dest_state & SCTP_ADDR_REACHABLE) {
219				net->dest_state &= ~SCTP_ADDR_REACHABLE;
220				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
221				net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
222				if (net == stcb->asoc.primary_destination) {
223					net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
224				}
225				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
226				    stcb,
227				    SCTP_FAILED_THRESHOLD,
228				    (void *)net);
229			}
230		}
231		/*********HOLD THIS COMMENT FOR PATCH OF ALTERNATE
232		 *********ROUTING CODE
233		 */
234		/*********HOLD THIS COMMENT FOR END OF PATCH OF ALTERNATE
235		 *********ROUTING CODE
236		 */
237	}
238	if (stcb == NULL)
239		return (0);
240
241	if (net) {
242		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
243			stcb->asoc.overall_error_count++;
244		}
245	} else {
246		stcb->asoc.overall_error_count++;
247	}
248#ifdef SCTP_DEBUG
249	if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
250		printf("Overall error count for %p now %d thresh:%u state:%x\n",
251		    &stcb->asoc,
252		    stcb->asoc.overall_error_count,
253		    (uint32_t) threshold,
254		    ((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
255	}
256#endif				/* SCTP_DEBUG */
257	/*
258	 * We specifically do not do >= to give the assoc one more change
259	 * before we fail it.
260	 */
261	if (stcb->asoc.overall_error_count > threshold) {
262		/* Abort notification sends a ULP notify */
263		struct mbuf *oper;
264
265		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
266		    0, M_DONTWAIT, 1, MT_DATA);
267		if (oper) {
268			struct sctp_paramhdr *ph;
269			uint32_t *ippp;
270
271			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
272			    sizeof(uint32_t);
273			ph = mtod(oper, struct sctp_paramhdr *);
274			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
275			ph->param_length = htons(SCTP_BUF_LEN(oper));
276			ippp = (uint32_t *) (ph + 1);
277			*ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_1);
278		}
279		inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_1;
280		sctp_abort_an_association(inp, stcb, SCTP_FAILED_THRESHOLD, oper);
281		return (1);
282	}
283	return (0);
284}
285
286struct sctp_nets *
287sctp_find_alternate_net(struct sctp_tcb *stcb,
288    struct sctp_nets *net,
289    int highest_ssthresh)
290{
291	/* Find and return an alternate network if possible */
292	struct sctp_nets *alt, *mnet, *hthresh = NULL;
293	int once;
294	uint32_t val = 0;
295
296	if (stcb->asoc.numnets == 1) {
297		/* No others but net */
298		return (TAILQ_FIRST(&stcb->asoc.nets));
299	}
300	if (highest_ssthresh) {
301		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
302			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
303			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)
304			    ) {
305				/*
306				 * will skip ones that are not-reachable or
307				 * unconfirmed
308				 */
309				continue;
310			}
311			if (val > mnet->ssthresh) {
312				hthresh = mnet;
313				val = mnet->ssthresh;
314			} else if (val == mnet->ssthresh) {
315				uint32_t rndval;
316				uint8_t this_random;
317
318				if (stcb->asoc.hb_random_idx > 3) {
319					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
320					memcpy(stcb->asoc.hb_random_values, &rndval,
321					    sizeof(stcb->asoc.hb_random_values));
322					this_random = stcb->asoc.hb_random_values[0];
323					stcb->asoc.hb_random_idx = 0;
324					stcb->asoc.hb_ect_randombit = 0;
325				} else {
326					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
327					stcb->asoc.hb_random_idx++;
328					stcb->asoc.hb_ect_randombit = 0;
329				}
330				if (this_random % 2) {
331					hthresh = mnet;
332					val = mnet->ssthresh;
333				}
334			}
335		}
336		if (hthresh) {
337			return (hthresh);
338		}
339	}
340	mnet = net;
341	once = 0;
342
343	if (mnet == NULL) {
344		mnet = TAILQ_FIRST(&stcb->asoc.nets);
345	}
346	do {
347		alt = TAILQ_NEXT(mnet, sctp_next);
348		if (alt == NULL) {
349			once++;
350			if (once > 1) {
351				break;
352			}
353			alt = TAILQ_FIRST(&stcb->asoc.nets);
354		}
355		if (alt->ro.ro_rt == NULL) {
356			if (alt->ro._s_addr) {
357				sctp_free_ifa(alt->ro._s_addr);
358				alt->ro._s_addr = NULL;
359
360			}
361			alt->src_addr_selected = 0;
362		}
363		if (
364		    ((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
365		    (alt->ro.ro_rt != NULL) &&
366		    (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))
367		    ) {
368			/* Found a reachable address */
369			break;
370		}
371		mnet = alt;
372	} while (alt != NULL);
373
374	if (alt == NULL) {
375		/* Case where NO insv network exists (dormant state) */
376		/* we rotate destinations */
377		once = 0;
378		mnet = net;
379		do {
380			alt = TAILQ_NEXT(mnet, sctp_next);
381			if (alt == NULL) {
382				once++;
383				if (once > 1) {
384					break;
385				}
386				alt = TAILQ_FIRST(&stcb->asoc.nets);
387			}
388			if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
389			    (alt != net)) {
390				/* Found an alternate address */
391				break;
392			}
393			mnet = alt;
394		} while (alt != NULL);
395	}
396	if (alt == NULL) {
397		return (net);
398	}
399	return (alt);
400}
401
402static void
403sctp_backoff_on_timeout(struct sctp_tcb *stcb,
404    struct sctp_nets *net,
405    int win_probe,
406    int num_marked)
407{
408	net->RTO <<= 1;
409	if (net->RTO > stcb->asoc.maxrto) {
410		net->RTO = stcb->asoc.maxrto;
411	}
412	if ((win_probe == 0) && num_marked) {
413		/* We don't apply penalty to window probe scenarios */
414#ifdef SCTP_CWND_MONITOR
415		int old_cwnd = net->cwnd;
416
417#endif
418		net->ssthresh = net->cwnd >> 1;
419		if (net->ssthresh < (net->mtu << 1)) {
420			net->ssthresh = (net->mtu << 1);
421		}
422		net->cwnd = net->mtu;
423		/* floor of 1 mtu */
424		if (net->cwnd < net->mtu)
425			net->cwnd = net->mtu;
426#ifdef SCTP_CWND_MONITOR
427		sctp_log_cwnd(stcb, net, net->cwnd - old_cwnd, SCTP_CWND_LOG_FROM_RTX);
428#endif
429
430		net->partial_bytes_acked = 0;
431	}
432}
433
434static int
435sctp_mark_all_for_resend(struct sctp_tcb *stcb,
436    struct sctp_nets *net,
437    struct sctp_nets *alt,
438    int window_probe,
439    int *num_marked)
440{
441
442	/*
443	 * Mark all chunks (well not all) that were sent to *net for
444	 * retransmission. Move them to alt for there destination as well...
445	 * We only mark chunks that have been outstanding long enough to
446	 * have received feed-back.
447	 */
448	struct sctp_tmit_chunk *chk, *tp2, *could_be_sent = NULL;
449	struct sctp_nets *lnets;
450	struct timeval now, min_wait, tv;
451	int cur_rtt;
452	int audit_tf, num_mk, fir;
453	unsigned int cnt_mk;
454	uint32_t orig_flight, orig_tf;
455	uint32_t tsnlast, tsnfirst;
456
457	/*
458	 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is being used,
459	 * then pick dest with largest ssthresh for any retransmission.
460	 * (iyengar@cis.udel.edu, 2005/08/12)
461	 */
462	if (sctp_cmt_on_off) {
463		alt = sctp_find_alternate_net(stcb, net, 1);
464		/*
465		 * CUCv2: If a different dest is picked for the
466		 * retransmission, then new (rtx-)pseudo_cumack needs to be
467		 * tracked for orig dest. Let CUCv2 track new (rtx-)
468		 * pseudo-cumack always.
469		 */
470		net->find_pseudo_cumack = 1;
471		net->find_rtx_pseudo_cumack = 1;
472	}
473	/* none in flight now */
474	audit_tf = 0;
475	fir = 0;
476	/*
477	 * figure out how long a data chunk must be pending before we can
478	 * mark it ..
479	 */
480	(void)SCTP_GETTIME_TIMEVAL(&now);
481	/* get cur rto in micro-seconds */
482	cur_rtt = (((net->lastsa >> 2) + net->lastsv) >> 1);
483	cur_rtt *= 1000;
484#if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
485	sctp_log_fr(cur_rtt,
486	    stcb->asoc.peers_rwnd,
487	    window_probe,
488	    SCTP_FR_T3_MARK_TIME);
489	sctp_log_fr(net->flight_size,
490	    SCTP_OS_TIMER_PENDING(&net->fr_timer.timer),
491	    SCTP_OS_TIMER_ACTIVE(&net->fr_timer.timer),
492	    SCTP_FR_CWND_REPORT);
493	sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
494#endif
495	tv.tv_sec = cur_rtt / 1000000;
496	tv.tv_usec = cur_rtt % 1000000;
497	min_wait = now;
498	timevalsub(&min_wait, &tv);
499	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
500		/*
501		 * if we hit here, we don't have enough seconds on the clock
502		 * to account for the RTO. We just let the lower seconds be
503		 * the bounds and don't worry about it. This may mean we
504		 * will mark a lot more than we should.
505		 */
506		min_wait.tv_sec = min_wait.tv_usec = 0;
507	}
508#if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
509	sctp_log_fr(cur_rtt, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
510	sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
511#endif
512	/*
513	 * Our rwnd will be incorrect here since we are not adding back the
514	 * cnt * mbuf but we will fix that down below.
515	 */
516	orig_flight = net->flight_size;
517	orig_tf = stcb->asoc.total_flight;
518
519	net->fast_retran_ip = 0;
520	/* Now on to each chunk */
521	num_mk = cnt_mk = 0;
522	tsnfirst = tsnlast = 0;
523	chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
524	for (; chk != NULL; chk = tp2) {
525		tp2 = TAILQ_NEXT(chk, sctp_next);
526		if ((compare_with_wrap(stcb->asoc.last_acked_seq,
527		    chk->rec.data.TSN_seq,
528		    MAX_TSN)) ||
529		    (stcb->asoc.last_acked_seq == chk->rec.data.TSN_seq)) {
530			/* Strange case our list got out of order? */
531			printf("Our list is out of order?\n");
532			panic("Out of order list");
533		}
534		if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
535			/*
536			 * found one to mark: If it is less than
537			 * DATAGRAM_ACKED it MUST not be a skipped or marked
538			 * TSN but instead one that is either already set
539			 * for retransmission OR one that needs
540			 * retransmission.
541			 */
542
543			/* validate its been outstanding long enough */
544#if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
545			sctp_log_fr(chk->rec.data.TSN_seq,
546			    chk->sent_rcv_time.tv_sec,
547			    chk->sent_rcv_time.tv_usec,
548			    SCTP_FR_T3_MARK_TIME);
549#endif
550			if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
551				/*
552				 * we have reached a chunk that was sent
553				 * some seconds past our min.. forget it we
554				 * will find no more to send.
555				 */
556#if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
557				sctp_log_fr(0,
558				    chk->sent_rcv_time.tv_sec,
559				    chk->sent_rcv_time.tv_usec,
560				    SCTP_FR_T3_STOPPED);
561#endif
562				continue;
563			} else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
564			    (window_probe == 0)) {
565				/*
566				 * we must look at the micro seconds to
567				 * know.
568				 */
569				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
570					/*
571					 * ok it was sent after our boundary
572					 * time.
573					 */
574#if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
575					sctp_log_fr(0,
576					    chk->sent_rcv_time.tv_sec,
577					    chk->sent_rcv_time.tv_usec,
578					    SCTP_FR_T3_STOPPED);
579#endif
580					continue;
581				}
582			}
583			if (PR_SCTP_TTL_ENABLED(chk->flags)) {
584				/* Is it expired? */
585				if ((now.tv_sec > chk->rec.data.timetodrop.tv_sec) ||
586				    ((chk->rec.data.timetodrop.tv_sec == now.tv_sec) &&
587				    (now.tv_usec > chk->rec.data.timetodrop.tv_usec))) {
588					/* Yes so drop it */
589					if (chk->data) {
590						sctp_release_pr_sctp_chunk(stcb,
591						    chk,
592						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
593						    &stcb->asoc.sent_queue);
594					}
595				}
596				continue;
597			}
598			if (PR_SCTP_RTX_ENABLED(chk->flags)) {
599				/* Has it been retransmitted tv_sec times? */
600				if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
601					if (chk->data) {
602						sctp_release_pr_sctp_chunk(stcb,
603						    chk,
604						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
605						    &stcb->asoc.sent_queue);
606					}
607				}
608				continue;
609			}
610			if (chk->sent < SCTP_DATAGRAM_RESEND) {
611				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
612				num_mk++;
613				if (fir == 0) {
614					fir = 1;
615					tsnfirst = chk->rec.data.TSN_seq;
616				}
617				tsnlast = chk->rec.data.TSN_seq;
618#if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
619				sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
620				    0, SCTP_FR_T3_MARKED);
621
622#endif
623				if (chk->rec.data.chunk_was_revoked) {
624					/* deflate the cwnd */
625					chk->whoTo->cwnd -= chk->book_size;
626					chk->rec.data.chunk_was_revoked = 0;
627				}
628				net->marked_retrans++;
629				stcb->asoc.marked_retrans++;
630#ifdef SCTP_FLIGHT_LOGGING
631				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
632				    chk->whoTo->flight_size,
633				    chk->book_size,
634				    (uintptr_t) chk->whoTo,
635				    chk->rec.data.TSN_seq);
636#endif
637				sctp_flight_size_decrease(chk);
638				sctp_total_flight_decrease(stcb, chk);
639				stcb->asoc.peers_rwnd += chk->send_size;
640				stcb->asoc.peers_rwnd += sctp_peer_chunk_oh;
641			}
642			chk->sent = SCTP_DATAGRAM_RESEND;
643			SCTP_STAT_INCR(sctps_markedretrans);
644
645			/* reset the TSN for striking and other FR stuff */
646			chk->rec.data.doing_fast_retransmit = 0;
647			/* Clear any time so NO RTT is being done */
648			chk->do_rtt = 0;
649			if (alt != net) {
650				sctp_free_remote_addr(chk->whoTo);
651				chk->no_fr_allowed = 1;
652				chk->whoTo = alt;
653				atomic_add_int(&alt->ref_count, 1);
654			} else {
655				chk->no_fr_allowed = 0;
656				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
657					chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
658				} else {
659					chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
660				}
661			}
662			if (sctp_cmt_on_off == 1) {
663				chk->no_fr_allowed = 1;
664			}
665		} else if (chk->sent == SCTP_DATAGRAM_ACKED) {
666			/* remember highest acked one */
667			could_be_sent = chk;
668		}
669		if (chk->sent == SCTP_DATAGRAM_RESEND) {
670			cnt_mk++;
671		}
672	}
673	if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
674		/* we did not subtract the same things? */
675		audit_tf = 1;
676	}
677#if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
678	sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
679#endif
680#ifdef SCTP_DEBUG
681	if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
682		if (num_mk) {
683			printf("LAST TSN marked was %x\n", tsnlast);
684			printf("Num marked for retransmission was %d peer-rwd:%ld\n",
685			    num_mk, (u_long)stcb->asoc.peers_rwnd);
686			printf("LAST TSN marked was %x\n", tsnlast);
687			printf("Num marked for retransmission was %d peer-rwd:%d\n",
688			    num_mk,
689			    (int)stcb->asoc.peers_rwnd
690			    );
691		}
692	}
693#endif
694	*num_marked = num_mk;
695	if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
696		/* fix it so we retransmit the highest acked anyway */
697		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
698		cnt_mk++;
699		could_be_sent->sent = SCTP_DATAGRAM_RESEND;
700	}
701	if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
702#ifdef INVARIANTS
703		printf("Local Audit says there are %d for retran asoc cnt:%d\n",
704		    cnt_mk, stcb->asoc.sent_queue_retran_cnt);
705#endif
706#ifndef SCTP_AUDITING_ENABLED
707		stcb->asoc.sent_queue_retran_cnt = cnt_mk;
708#endif
709	}
710	/* Now check for a ECN Echo that may be stranded */
711	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
712		if ((chk->whoTo == net) &&
713		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
714			sctp_free_remote_addr(chk->whoTo);
715			chk->whoTo = alt;
716			if (chk->sent != SCTP_DATAGRAM_RESEND) {
717				chk->sent = SCTP_DATAGRAM_RESEND;
718				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
719			}
720			atomic_add_int(&alt->ref_count, 1);
721		}
722	}
723	if (audit_tf) {
724#ifdef SCTP_DEBUG
725		if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
726			printf("Audit total flight due to negative value net:%p\n",
727			    net);
728		}
729#endif				/* SCTP_DEBUG */
730		stcb->asoc.total_flight = 0;
731		stcb->asoc.total_flight_count = 0;
732		/* Clear all networks flight size */
733		TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
734			lnets->flight_size = 0;
735#ifdef SCTP_DEBUG
736			if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
737				printf("Net:%p c-f cwnd:%d ssthresh:%d\n",
738				    lnets, lnets->cwnd, lnets->ssthresh);
739			}
740#endif				/* SCTP_DEBUG */
741		}
742		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
743			if (chk->sent < SCTP_DATAGRAM_RESEND) {
744#ifdef SCTP_FLIGHT_LOGGING
745				sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
746				    chk->whoTo->flight_size,
747				    chk->book_size,
748				    (uintptr_t) chk->whoTo,
749				    chk->rec.data.TSN_seq);
750#endif
751
752				sctp_flight_size_increase(chk);
753				sctp_total_flight_increase(stcb, chk);
754			}
755		}
756	}
757	/*
758	 * Setup the ecn nonce re-sync point. We do this since
759	 * retranmissions are NOT setup for ECN. This means that do to
760	 * Karn's rule, we don't know the total of the peers ecn bits.
761	 */
762	chk = TAILQ_FIRST(&stcb->asoc.send_queue);
763	if (chk == NULL) {
764		stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
765	} else {
766		stcb->asoc.nonce_resync_tsn = chk->rec.data.TSN_seq;
767	}
768	stcb->asoc.nonce_wait_for_ecne = 0;
769	stcb->asoc.nonce_sum_check = 0;
770	/* We return 1 if we only have a window probe outstanding */
771	return (0);
772}
773
774static void
775sctp_move_all_chunks_to_alt(struct sctp_tcb *stcb,
776    struct sctp_nets *net,
777    struct sctp_nets *alt)
778{
779	struct sctp_association *asoc;
780	struct sctp_stream_out *outs;
781	struct sctp_tmit_chunk *chk;
782	struct sctp_stream_queue_pending *sp;
783
784	if (net == alt)
785		/* nothing to do */
786		return;
787
788	asoc = &stcb->asoc;
789
790	/*
791	 * now through all the streams checking for chunks sent to our bad
792	 * network.
793	 */
794	TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
795		/* now clean up any chunks here */
796		TAILQ_FOREACH(sp, &outs->outqueue, next) {
797			if (sp->net == net) {
798				sctp_free_remote_addr(sp->net);
799				sp->net = alt;
800				atomic_add_int(&alt->ref_count, 1);
801			}
802		}
803	}
804	/* Now check the pending queue */
805	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
806		if (chk->whoTo == net) {
807			sctp_free_remote_addr(chk->whoTo);
808			chk->whoTo = alt;
809			atomic_add_int(&alt->ref_count, 1);
810		}
811	}
812
813}
814
815int
816sctp_t3rxt_timer(struct sctp_inpcb *inp,
817    struct sctp_tcb *stcb,
818    struct sctp_nets *net)
819{
820	struct sctp_nets *alt;
821	int win_probe, num_mk;
822
823#ifdef SCTP_FR_LOGGING
824	sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
825#ifdef SCTP_CWND_LOGGING
826	{
827		struct sctp_nets *lnet;
828
829		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
830			if (net == lnet) {
831				sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
832			} else {
833				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
834			}
835		}
836	}
837#endif
838#endif
839	/* Find an alternate and mark those for retransmission */
840	if ((stcb->asoc.peers_rwnd == 0) &&
841	    (stcb->asoc.total_flight < net->mtu)) {
842		SCTP_STAT_INCR(sctps_timowindowprobe);
843		win_probe = 1;
844	} else {
845		win_probe = 0;
846	}
847
848	if (sctp_cmt_on_off) {
849		/*
850		 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is being
851		 * used, then pick dest with largest ssthresh for any
852		 * retransmission.
853		 */
854		alt = net;
855		alt = sctp_find_alternate_net(stcb, alt, 1);
856		/*
857		 * CUCv2: If a different dest is picked for the
858		 * retransmission, then new (rtx-)pseudo_cumack needs to be
859		 * tracked for orig dest. Let CUCv2 track new (rtx-)
860		 * pseudo-cumack always.
861		 */
862		net->find_pseudo_cumack = 1;
863		net->find_rtx_pseudo_cumack = 1;
864
865	} else {		/* CMT is OFF */
866
867		alt = sctp_find_alternate_net(stcb, net, 0);
868	}
869
870	sctp_mark_all_for_resend(stcb, net, alt, win_probe, &num_mk);
871	/* FR Loss recovery just ended with the T3. */
872	stcb->asoc.fast_retran_loss_recovery = 0;
873
874	/* CMT FR loss recovery ended with the T3 */
875	net->fast_retran_loss_recovery = 0;
876
877	/*
878	 * setup the sat loss recovery that prevents satellite cwnd advance.
879	 */
880	stcb->asoc.sat_t3_loss_recovery = 1;
881	stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
882
883	/* Backoff the timer and cwnd */
884	sctp_backoff_on_timeout(stcb, net, win_probe, num_mk);
885	if (win_probe == 0) {
886		/* We don't do normal threshold management on window probes */
887		if (sctp_threshold_management(inp, stcb, net,
888		    stcb->asoc.max_send_times)) {
889			/* Association was destroyed */
890			return (1);
891		} else {
892			if (net != stcb->asoc.primary_destination) {
893				/* send a immediate HB if our RTO is stale */
894				struct timeval now;
895				unsigned int ms_goneby;
896
897				(void)SCTP_GETTIME_TIMEVAL(&now);
898				if (net->last_sent_time.tv_sec) {
899					ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
900				} else {
901					ms_goneby = 0;
902				}
903				if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
904					/*
905					 * no recent feed back in an RTO or
906					 * more, request a RTT update
907					 */
908					(void)sctp_send_hb(stcb, 1, net);
909				}
910			}
911		}
912	} else {
913		/*
914		 * For a window probe we don't penalize the net's but only
915		 * the association. This may fail it if SACKs are not coming
916		 * back. If sack's are coming with rwnd locked at 0, we will
917		 * continue to hold things waiting for rwnd to raise
918		 */
919		if (sctp_threshold_management(inp, stcb, NULL,
920		    stcb->asoc.max_send_times)) {
921			/* Association was destroyed */
922			return (1);
923		}
924	}
925	if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
926		/* Move all pending over too */
927		sctp_move_all_chunks_to_alt(stcb, net, alt);
928
929		/*
930		 * Get the address that failed, to force a new src address
931		 * selecton and a route allocation.
932		 */
933		if (net->ro._s_addr) {
934			sctp_free_ifa(net->ro._s_addr);
935			net->ro._s_addr = NULL;
936		}
937		net->src_addr_selected = 0;
938
939		/* Force a route allocation too */
940		if (net->ro.ro_rt) {
941			RTFREE(net->ro.ro_rt);
942			net->ro.ro_rt = NULL;
943		}
944		/* Was it our primary? */
945		if ((stcb->asoc.primary_destination == net) && (alt != net)) {
946			/*
947			 * Yes, note it as such and find an alternate note:
948			 * this means HB code must use this to resent the
949			 * primary if it goes active AND if someone does a
950			 * change-primary then this flag must be cleared
951			 * from any net structures.
952			 */
953			if (sctp_set_primary_addr(stcb,
954			    (struct sockaddr *)NULL,
955			    alt) == 0) {
956				net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
957			}
958		}
959	}
960	/*
961	 * Special case for cookie-echo'ed case, we don't do output but must
962	 * await the COOKIE-ACK before retransmission
963	 */
964	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
965		/*
966		 * Here we just reset the timer and start again since we
967		 * have not established the asoc
968		 */
969		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
970		return (0);
971	}
972	if (stcb->asoc.peer_supports_prsctp) {
973		struct sctp_tmit_chunk *lchk;
974
975		lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
976		/* C3. See if we need to send a Fwd-TSN */
977		if (compare_with_wrap(stcb->asoc.advanced_peer_ack_point,
978		    stcb->asoc.last_acked_seq, MAX_TSN)) {
979			/*
980			 * ISSUE with ECN, see FWD-TSN processing for notes
981			 * on issues that will occur when the ECN NONCE
982			 * stuff is put into SCTP for cross checking.
983			 */
984			send_forward_tsn(stcb, &stcb->asoc);
985			if (lchk) {
986				/* Assure a timer is up */
987				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
988			}
989		}
990	}
991#ifdef SCTP_CWND_MONITOR
992	sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
993#endif
994	return (0);
995}
996
997int
998sctp_t1init_timer(struct sctp_inpcb *inp,
999    struct sctp_tcb *stcb,
1000    struct sctp_nets *net)
1001{
1002	/* bump the thresholds */
1003	if (stcb->asoc.delayed_connection) {
1004		/*
1005		 * special hook for delayed connection. The library did NOT
1006		 * complete the rest of its sends.
1007		 */
1008		stcb->asoc.delayed_connection = 0;
1009		sctp_send_initiate(inp, stcb);
1010		return (0);
1011	}
1012	if (SCTP_GET_STATE((&stcb->asoc)) != SCTP_STATE_COOKIE_WAIT) {
1013		return (0);
1014	}
1015	if (sctp_threshold_management(inp, stcb, net,
1016	    stcb->asoc.max_init_times)) {
1017		/* Association was destroyed */
1018		return (1);
1019	}
1020	stcb->asoc.dropped_special_cnt = 0;
1021	sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0);
1022	if (stcb->asoc.initial_init_rto_max < net->RTO) {
1023		net->RTO = stcb->asoc.initial_init_rto_max;
1024	}
1025	if (stcb->asoc.numnets > 1) {
1026		/* If we have more than one addr use it */
1027		struct sctp_nets *alt;
1028
1029		alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1030		if ((alt != NULL) && (alt != stcb->asoc.primary_destination)) {
1031			sctp_move_all_chunks_to_alt(stcb, stcb->asoc.primary_destination, alt);
1032			stcb->asoc.primary_destination = alt;
1033		}
1034	}
1035	/* Send out a new init */
1036	sctp_send_initiate(inp, stcb);
1037	return (0);
1038}
1039
1040/*
1041 * For cookie and asconf we actually need to find and mark for resend, then
1042 * increment the resend counter (after all the threshold management stuff of
1043 * course).
1044 */
1045int
1046sctp_cookie_timer(struct sctp_inpcb *inp,
1047    struct sctp_tcb *stcb,
1048    struct sctp_nets *net)
1049{
1050	struct sctp_nets *alt;
1051	struct sctp_tmit_chunk *cookie;
1052
1053	/* first before all else we must find the cookie */
1054	TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1055		if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1056			break;
1057		}
1058	}
1059	if (cookie == NULL) {
1060		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1061			/* FOOBAR! */
1062			struct mbuf *oper;
1063
1064			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1065			    0, M_DONTWAIT, 1, MT_DATA);
1066			if (oper) {
1067				struct sctp_paramhdr *ph;
1068				uint32_t *ippp;
1069
1070				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1071				    sizeof(uint32_t);
1072				ph = mtod(oper, struct sctp_paramhdr *);
1073				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1074				ph->param_length = htons(SCTP_BUF_LEN(oper));
1075				ippp = (uint32_t *) (ph + 1);
1076				*ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_2);
1077			}
1078			inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_3;
1079			sctp_abort_an_association(inp, stcb, SCTP_INTERNAL_ERROR,
1080			    oper);
1081		} else {
1082#ifdef INVARIANTS
1083			panic("Cookie timer expires in wrong state?");
1084#else
1085			printf("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(&stcb->asoc));
1086			return (0);
1087#endif
1088		}
1089		return (0);
1090	}
1091	/* Ok we found the cookie, threshold management next */
1092	if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1093	    stcb->asoc.max_init_times)) {
1094		/* Assoc is over */
1095		return (1);
1096	}
1097	/*
1098	 * cleared theshold management now lets backoff the address & select
1099	 * an alternate
1100	 */
1101	stcb->asoc.dropped_special_cnt = 0;
1102	sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0);
1103	alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1104	if (alt != cookie->whoTo) {
1105		sctp_free_remote_addr(cookie->whoTo);
1106		cookie->whoTo = alt;
1107		atomic_add_int(&alt->ref_count, 1);
1108	}
1109	/* Now mark the retran info */
1110	if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1111		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1112	}
1113	cookie->sent = SCTP_DATAGRAM_RESEND;
1114	/*
1115	 * Now call the output routine to kick out the cookie again, Note we
1116	 * don't mark any chunks for retran so that FR will need to kick in
1117	 * to move these (or a send timer).
1118	 */
1119	return (0);
1120}
1121
1122int
1123sctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1124    struct sctp_nets *net)
1125{
1126	struct sctp_nets *alt;
1127	struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1128
1129	if (stcb->asoc.stream_reset_outstanding == 0) {
1130		return (0);
1131	}
1132	/* find the existing STRRESET, we use the seq number we sent out on */
1133	sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1134	if (strrst == NULL) {
1135		return (0);
1136	}
1137	/* do threshold management */
1138	if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1139	    stcb->asoc.max_send_times)) {
1140		/* Assoc is over */
1141		return (1);
1142	}
1143	/*
1144	 * cleared theshold management now lets backoff the address & select
1145	 * an alternate
1146	 */
1147	sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0);
1148	alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
1149	sctp_free_remote_addr(strrst->whoTo);
1150	strrst->whoTo = alt;
1151	atomic_add_int(&alt->ref_count, 1);
1152
1153	/* See if a ECN Echo is also stranded */
1154	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1155		if ((chk->whoTo == net) &&
1156		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1157			sctp_free_remote_addr(chk->whoTo);
1158			if (chk->sent != SCTP_DATAGRAM_RESEND) {
1159				chk->sent = SCTP_DATAGRAM_RESEND;
1160				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1161			}
1162			chk->whoTo = alt;
1163			atomic_add_int(&alt->ref_count, 1);
1164		}
1165	}
1166	if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1167		/*
1168		 * If the address went un-reachable, we need to move to
1169		 * alternates for ALL chk's in queue
1170		 */
1171		sctp_move_all_chunks_to_alt(stcb, net, alt);
1172	}
1173	/* mark the retran info */
1174	if (strrst->sent != SCTP_DATAGRAM_RESEND)
1175		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1176	strrst->sent = SCTP_DATAGRAM_RESEND;
1177
1178	/* restart the timer */
1179	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1180	return (0);
1181}
1182
1183int
1184sctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1185    struct sctp_nets *net)
1186{
1187	struct sctp_nets *alt;
1188	struct sctp_tmit_chunk *asconf, *chk;
1189
1190	/* is this the first send, or a retransmission? */
1191	if (stcb->asoc.asconf_sent == 0) {
1192		/* compose a new ASCONF chunk and send it */
1193		sctp_send_asconf(stcb, net);
1194	} else {
1195		/* Retransmission of the existing ASCONF needed... */
1196
1197		/* find the existing ASCONF */
1198		TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
1199		    sctp_next) {
1200			if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
1201				break;
1202			}
1203		}
1204		if (asconf == NULL) {
1205			return (0);
1206		}
1207		/* do threshold management */
1208		if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1209		    stcb->asoc.max_send_times)) {
1210			/* Assoc is over */
1211			return (1);
1212		}
1213		/*
1214		 * PETER? FIX? How will the following code ever run? If the
1215		 * max_send_times is hit, threshold managment will blow away
1216		 * the association?
1217		 */
1218		if (asconf->snd_count > stcb->asoc.max_send_times) {
1219			/*
1220			 * Something is rotten, peer is not responding to
1221			 * ASCONFs but maybe is to data etc.  e.g. it is not
1222			 * properly handling the chunk type upper bits Mark
1223			 * this peer as ASCONF incapable and cleanup
1224			 */
1225#ifdef SCTP_DEBUG
1226			if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1227				printf("asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1228			}
1229#endif				/* SCTP_DEBUG */
1230			sctp_asconf_cleanup(stcb, net);
1231			return (0);
1232		}
1233		/*
1234		 * cleared theshold management now lets backoff the address
1235		 * & select an alternate
1236		 */
1237		sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0);
1238		alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
1239		sctp_free_remote_addr(asconf->whoTo);
1240		asconf->whoTo = alt;
1241		atomic_add_int(&alt->ref_count, 1);
1242
1243		/* See if a ECN Echo is also stranded */
1244		TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1245			if ((chk->whoTo == net) &&
1246			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1247				sctp_free_remote_addr(chk->whoTo);
1248				chk->whoTo = alt;
1249				if (chk->sent != SCTP_DATAGRAM_RESEND) {
1250					chk->sent = SCTP_DATAGRAM_RESEND;
1251					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1252				}
1253				atomic_add_int(&alt->ref_count, 1);
1254			}
1255		}
1256		if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1257			/*
1258			 * If the address went un-reachable, we need to move
1259			 * to alternates for ALL chk's in queue
1260			 */
1261			sctp_move_all_chunks_to_alt(stcb, net, alt);
1262		}
1263		/* mark the retran info */
1264		if (asconf->sent != SCTP_DATAGRAM_RESEND)
1265			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1266		asconf->sent = SCTP_DATAGRAM_RESEND;
1267	}
1268	return (0);
1269}
1270
1271/*
1272 * For the shutdown and shutdown-ack, we do not keep one around on the
1273 * control queue. This means we must generate a new one and call the general
1274 * chunk output routine, AFTER having done threshold management.
1275 */
1276int
1277sctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1278    struct sctp_nets *net)
1279{
1280	struct sctp_nets *alt;
1281
1282	/* first threshold managment */
1283	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1284		/* Assoc is over */
1285		return (1);
1286	}
1287	/* second select an alternative */
1288	alt = sctp_find_alternate_net(stcb, net, 0);
1289
1290	/* third generate a shutdown into the queue for out net */
1291	if (alt) {
1292		sctp_send_shutdown(stcb, alt);
1293	} else {
1294		/*
1295		 * if alt is NULL, there is no dest to send to??
1296		 */
1297		return (0);
1298	}
1299	/* fourth restart timer */
1300	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1301	return (0);
1302}
1303
1304int
1305sctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1306    struct sctp_nets *net)
1307{
1308	struct sctp_nets *alt;
1309
1310	/* first threshold managment */
1311	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1312		/* Assoc is over */
1313		return (1);
1314	}
1315	/* second select an alternative */
1316	alt = sctp_find_alternate_net(stcb, net, 0);
1317
1318	/* third generate a shutdown into the queue for out net */
1319	sctp_send_shutdown_ack(stcb, alt);
1320
1321	/* fourth restart timer */
1322	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1323	return (0);
1324}
1325
1326static void
1327sctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1328    struct sctp_tcb *stcb)
1329{
1330	struct sctp_stream_out *outs;
1331	struct sctp_stream_queue_pending *sp;
1332	unsigned int chks_in_queue = 0;
1333	int being_filled = 0;
1334
1335	/*
1336	 * This function is ONLY called when the send/sent queues are empty.
1337	 */
1338	if ((stcb == NULL) || (inp == NULL))
1339		return;
1340
1341	if (stcb->asoc.sent_queue_retran_cnt) {
1342		printf("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1343		    stcb->asoc.sent_queue_retran_cnt);
1344		stcb->asoc.sent_queue_retran_cnt = 0;
1345	}
1346	SCTP_TCB_SEND_LOCK(stcb);
1347	if (TAILQ_EMPTY(&stcb->asoc.out_wheel)) {
1348		int i, cnt = 0;
1349
1350		/* Check to see if a spoke fell off the wheel */
1351		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1352			if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1353				sctp_insert_on_wheel(stcb, &stcb->asoc, &stcb->asoc.strmout[i], 1);
1354				cnt++;
1355			}
1356		}
1357		if (cnt) {
1358			/* yep, we lost a spoke or two */
1359			printf("Found an additional %d streams NOT on outwheel, corrected\n", cnt);
1360		} else {
1361			/* no spokes lost, */
1362			stcb->asoc.total_output_queue_size = 0;
1363		}
1364		SCTP_TCB_SEND_UNLOCK(stcb);
1365		return;
1366	}
1367	SCTP_TCB_SEND_UNLOCK(stcb);
1368	/* Check to see if some data queued, if so report it */
1369	TAILQ_FOREACH(outs, &stcb->asoc.out_wheel, next_spoke) {
1370		if (!TAILQ_EMPTY(&outs->outqueue)) {
1371			TAILQ_FOREACH(sp, &outs->outqueue, next) {
1372				if (sp->msg_is_complete)
1373					being_filled++;
1374				chks_in_queue++;
1375			}
1376		}
1377	}
1378	if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1379		printf("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1380		    stcb->asoc.stream_queue_cnt, chks_in_queue);
1381	}
1382	if (chks_in_queue) {
1383		/* call the output queue function */
1384		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3);
1385		if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1386		    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1387			/*
1388			 * Probably should go in and make it go back through
1389			 * and add fragments allowed
1390			 */
1391			if (being_filled == 0) {
1392				printf("Still nothing moved %d chunks are stuck\n",
1393				    chks_in_queue);
1394			}
1395		}
1396	} else {
1397		printf("Found no chunks on any queue tot:%lu\n",
1398		    (u_long)stcb->asoc.total_output_queue_size);
1399		stcb->asoc.total_output_queue_size = 0;
1400	}
1401}
1402
1403int
1404sctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1405    struct sctp_nets *net, int cnt_of_unconf)
1406{
1407	if (net) {
1408		if (net->hb_responded == 0) {
1409			if (net->ro._s_addr) {
1410				/*
1411				 * Invalidate the src address if we did not
1412				 * get a response last time.
1413				 */
1414				sctp_free_ifa(net->ro._s_addr);
1415				net->ro._s_addr = NULL;
1416				net->src_addr_selected = 0;
1417			}
1418			sctp_backoff_on_timeout(stcb, net, 1, 0);
1419		}
1420		/* Zero PBA, if it needs it */
1421		if (net->partial_bytes_acked) {
1422			net->partial_bytes_acked = 0;
1423		}
1424	}
1425	if ((stcb->asoc.total_output_queue_size > 0) &&
1426	    (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1427	    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1428		sctp_audit_stream_queues_for_size(inp, stcb);
1429	}
1430	/* Send a new HB, this will do threshold managment, pick a new dest */
1431	if (cnt_of_unconf == 0) {
1432		if (sctp_send_hb(stcb, 0, NULL) < 0) {
1433			return (1);
1434		}
1435	} else {
1436		/*
1437		 * this will send out extra hb's up to maxburst if there are
1438		 * any unconfirmed addresses.
1439		 */
1440		int cnt_sent = 0;
1441
1442		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1443			if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1444			    (net->dest_state & SCTP_ADDR_REACHABLE)) {
1445				cnt_sent++;
1446				if (net->hb_responded == 0) {
1447					/* Did we respond last time? */
1448					if (net->ro._s_addr) {
1449						sctp_free_ifa(net->ro._s_addr);
1450						net->ro._s_addr = NULL;
1451						net->src_addr_selected = 0;
1452					}
1453				}
1454				if (sctp_send_hb(stcb, 1, net) == 0) {
1455					break;
1456				}
1457				if (cnt_sent >= sctp_hb_maxburst)
1458					break;
1459			}
1460		}
1461	}
1462	return (0);
1463}
1464
1465int
1466sctp_is_hb_timer_running(struct sctp_tcb *stcb)
1467{
1468	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.hb_timer.timer)) {
1469		/* its running */
1470		return (1);
1471	} else {
1472		/* nope */
1473		return (0);
1474	}
1475}
1476
1477int
1478sctp_is_sack_timer_running(struct sctp_tcb *stcb)
1479{
1480	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
1481		/* its running */
1482		return (1);
1483	} else {
1484		/* nope */
1485		return (0);
1486	}
1487}
1488
1489#define SCTP_NUMBER_OF_MTU_SIZES 18
1490static uint32_t mtu_sizes[] = {
1491	68,
1492	296,
1493	508,
1494	512,
1495	544,
1496	576,
1497	1006,
1498	1492,
1499	1500,
1500	1536,
1501	2002,
1502	2048,
1503	4352,
1504	4464,
1505	8166,
1506	17914,
1507	32000,
1508	65535
1509};
1510
1511
1512static uint32_t
1513sctp_getnext_mtu(struct sctp_inpcb *inp, uint32_t cur_mtu)
1514{
1515	/* select another MTU that is just bigger than this one */
1516	int i;
1517
1518	for (i = 0; i < SCTP_NUMBER_OF_MTU_SIZES; i++) {
1519		if (cur_mtu < mtu_sizes[i]) {
1520			/* no max_mtu is bigger than this one */
1521			return (mtu_sizes[i]);
1522		}
1523	}
1524	/* here return the highest allowable */
1525	return (cur_mtu);
1526}
1527
1528
1529void
1530sctp_pathmtu_timer(struct sctp_inpcb *inp,
1531    struct sctp_tcb *stcb,
1532    struct sctp_nets *net)
1533{
1534	uint32_t next_mtu;
1535
1536	/* restart the timer in any case */
1537	next_mtu = sctp_getnext_mtu(inp, net->mtu);
1538	if (next_mtu <= net->mtu) {
1539		/* nothing to do */
1540		return;
1541	} {
1542		uint32_t mtu;
1543
1544		if ((net->src_addr_selected == 0) ||
1545		    (net->ro._s_addr == NULL) ||
1546		    (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1547			if ((net->ro._s_addr == NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1548				sctp_free_ifa(net->ro._s_addr);
1549				net->ro._s_addr = NULL;
1550				net->src_addr_selected = 0;
1551			}
1552			net->ro._s_addr = sctp_source_address_selection(inp,
1553			    stcb,
1554			    (sctp_route_t *) & net->ro,
1555			    net, 0, stcb->asoc.vrf_id);
1556			if (net->ro._s_addr)
1557				net->src_addr_selected = 1;
1558		}
1559		if (net->ro._s_addr) {
1560			mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
1561			if (mtu > next_mtu) {
1562				net->mtu = next_mtu;
1563			}
1564		}
1565	}
1566	/* restart the timer */
1567	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1568}
1569
1570void
1571sctp_autoclose_timer(struct sctp_inpcb *inp,
1572    struct sctp_tcb *stcb,
1573    struct sctp_nets *net)
1574{
1575	struct timeval tn, *tim_touse;
1576	struct sctp_association *asoc;
1577	int ticks_gone_by;
1578
1579	(void)SCTP_GETTIME_TIMEVAL(&tn);
1580	if (stcb->asoc.sctp_autoclose_ticks &&
1581	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1582		/* Auto close is on */
1583		asoc = &stcb->asoc;
1584		/* pick the time to use */
1585		if (asoc->time_last_rcvd.tv_sec >
1586		    asoc->time_last_sent.tv_sec) {
1587			tim_touse = &asoc->time_last_rcvd;
1588		} else {
1589			tim_touse = &asoc->time_last_sent;
1590		}
1591		/* Now has long enough transpired to autoclose? */
1592		ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
1593		if ((ticks_gone_by > 0) &&
1594		    (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1595			/*
1596			 * autoclose time has hit, call the output routine,
1597			 * which should do nothing just to be SURE we don't
1598			 * have hanging data. We can then safely check the
1599			 * queues and know that we are clear to send
1600			 * shutdown
1601			 */
1602			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR);
1603			/* Are we clean? */
1604			if (TAILQ_EMPTY(&asoc->send_queue) &&
1605			    TAILQ_EMPTY(&asoc->sent_queue)) {
1606				/*
1607				 * there is nothing queued to send, so I'm
1608				 * done...
1609				 */
1610				if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1611					/* only send SHUTDOWN 1st time thru */
1612					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
1613					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1614					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1615						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1616					}
1617					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1618					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1619					    stcb->sctp_ep, stcb,
1620					    asoc->primary_destination);
1621					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1622					    stcb->sctp_ep, stcb,
1623					    asoc->primary_destination);
1624				}
1625			}
1626		} else {
1627			/*
1628			 * No auto close at this time, reset t-o to check
1629			 * later
1630			 */
1631			int tmp;
1632
1633			/* fool the timer startup to use the time left */
1634			tmp = asoc->sctp_autoclose_ticks;
1635			asoc->sctp_autoclose_ticks -= ticks_gone_by;
1636			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1637			    net);
1638			/* restore the real tick value */
1639			asoc->sctp_autoclose_ticks = tmp;
1640		}
1641	}
1642}
1643
1644void
1645sctp_iterator_timer(struct sctp_iterator *it)
1646{
1647	int iteration_count = 0;
1648	int inp_skip = 0;
1649
1650	/*
1651	 * only one iterator can run at a time. This is the only way we can
1652	 * cleanly pull ep's from underneath all the running interators when
1653	 * a ep is freed.
1654	 */
1655	SCTP_ITERATOR_LOCK();
1656	if (it->inp == NULL) {
1657		/* iterator is complete */
1658done_with_iterator:
1659		SCTP_ITERATOR_UNLOCK();
1660		SCTP_INP_INFO_WLOCK();
1661		TAILQ_REMOVE(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
1662		/* stopping the callout is not needed, in theory */
1663		SCTP_INP_INFO_WUNLOCK();
1664		(void)SCTP_OS_TIMER_STOP(&it->tmr.timer);
1665		if (it->function_atend != NULL) {
1666			(*it->function_atend) (it->pointer, it->val);
1667		}
1668		SCTP_FREE(it);
1669		return;
1670	}
1671select_a_new_ep:
1672	SCTP_INP_WLOCK(it->inp);
1673	while (((it->pcb_flags) &&
1674	    ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1675	    ((it->pcb_features) &&
1676	    ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1677		/* endpoint flags or features don't match, so keep looking */
1678		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1679			SCTP_INP_WUNLOCK(it->inp);
1680			goto done_with_iterator;
1681		}
1682		SCTP_INP_WUNLOCK(it->inp);
1683		it->inp = LIST_NEXT(it->inp, sctp_list);
1684		if (it->inp == NULL) {
1685			goto done_with_iterator;
1686		}
1687		SCTP_INP_WLOCK(it->inp);
1688	}
1689	if ((it->inp->inp_starting_point_for_iterator != NULL) &&
1690	    (it->inp->inp_starting_point_for_iterator != it)) {
1691		printf("Iterator collision, waiting for one at %p\n",
1692		    it->inp);
1693		SCTP_INP_WUNLOCK(it->inp);
1694		goto start_timer_return;
1695	}
1696	/* mark the current iterator on the endpoint */
1697	it->inp->inp_starting_point_for_iterator = it;
1698	SCTP_INP_WUNLOCK(it->inp);
1699	SCTP_INP_RLOCK(it->inp);
1700	/* now go through each assoc which is in the desired state */
1701	if (it->done_current_ep == 0) {
1702		if (it->function_inp != NULL)
1703			inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1704		it->done_current_ep = 1;
1705	}
1706	if (it->stcb == NULL) {
1707		/* run the per instance function */
1708		it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1709	}
1710	SCTP_INP_RUNLOCK(it->inp);
1711	if ((inp_skip) || it->stcb == NULL) {
1712		if (it->function_inp_end != NULL) {
1713			inp_skip = (*it->function_inp_end) (it->inp,
1714			    it->pointer,
1715			    it->val);
1716		}
1717		goto no_stcb;
1718	}
1719	if ((it->stcb) &&
1720	    (it->stcb->asoc.stcb_starting_point_for_iterator == it)) {
1721		it->stcb->asoc.stcb_starting_point_for_iterator = NULL;
1722	}
1723	while (it->stcb) {
1724		SCTP_TCB_LOCK(it->stcb);
1725		if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1726			/* not in the right state... keep looking */
1727			SCTP_TCB_UNLOCK(it->stcb);
1728			goto next_assoc;
1729		}
1730		/* mark the current iterator on the assoc */
1731		it->stcb->asoc.stcb_starting_point_for_iterator = it;
1732		/* see if we have limited out the iterator loop */
1733		iteration_count++;
1734		if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1735	start_timer_return:
1736			/* set a timer to continue this later */
1737			SCTP_TCB_UNLOCK(it->stcb);
1738			sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR,
1739			    (struct sctp_inpcb *)it, NULL, NULL);
1740			SCTP_ITERATOR_UNLOCK();
1741			return;
1742		}
1743		/* run function on this one */
1744		(*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1745
1746		/*
1747		 * we lie here, it really needs to have its own type but
1748		 * first I must verify that this won't effect things :-0
1749		 */
1750		if (it->no_chunk_output == 0)
1751			sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3);
1752
1753		SCTP_TCB_UNLOCK(it->stcb);
1754next_assoc:
1755		it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1756		if (it->stcb == NULL) {
1757			if (it->function_inp_end != NULL) {
1758				inp_skip = (*it->function_inp_end) (it->inp,
1759				    it->pointer,
1760				    it->val);
1761			}
1762		}
1763	}
1764no_stcb:
1765	/* done with all assocs on this endpoint, move on to next endpoint */
1766	it->done_current_ep = 0;
1767	SCTP_INP_WLOCK(it->inp);
1768	it->inp->inp_starting_point_for_iterator = NULL;
1769	SCTP_INP_WUNLOCK(it->inp);
1770	if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1771		it->inp = NULL;
1772	} else {
1773		SCTP_INP_INFO_RLOCK();
1774		it->inp = LIST_NEXT(it->inp, sctp_list);
1775		SCTP_INP_INFO_RUNLOCK();
1776	}
1777	if (it->inp == NULL) {
1778		goto done_with_iterator;
1779	}
1780	goto select_a_new_ep;
1781}
1782