2 * Copyright (c) 2001-2007, Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
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.
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.
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.
31 /* $KAME: sctp_timer.c,v 1.29 2005/03/06 16:04:18 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 #include <netinet/sctp_os.h>
38 #include <netinet/sctp_pcb.h>
40 #include <netinet6/sctp6_var.h>
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>
57 sctp_early_fr_timer(struct sctp_inpcb *inp,
58 struct sctp_tcb *stcb,
59 struct sctp_nets *net)
61 struct sctp_tmit_chunk *chk, *tp2;
62 struct timeval now, min_wait, tv;
63 unsigned int cur_rtt, cnt = 0, cnt_resend = 0;
65 /* an early FR is occuring. */
66 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;
73 cur_rtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
75 if (cur_rtt < sctp_early_fr_msec) {
76 cur_rtt = sctp_early_fr_msec;
79 tv.tv_sec = cur_rtt / 1000000;
80 tv.tv_usec = cur_rtt % 1000000;
82 timevalsub(&min_wait, &tv);
83 if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
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.
90 min_wait.tv_sec = min_wait.tv_usec = 0;
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) {
98 if (chk->sent == SCTP_DATAGRAM_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) {
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.
110 } else if (chk->sent_rcv_time.tv_sec == min_wait.tv_sec) {
112 * we must look at the micro seconds to
115 if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
117 * ok it was sent after our boundary
123 #ifdef SCTP_EARLYFR_LOGGING
124 sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
125 4, SCTP_FR_MARKED_EARLY);
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 */
140 #ifdef SCTP_CWND_MONITOR
143 old_cwnd = net->cwnd;
145 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_EARLY_FR_TMR);
147 * make a small adjustment to cwnd and force to CA.
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);
159 } else if (cnt_resend) {
160 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_EARLY_FR_TMR);
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);
170 sctp_audit_retranmission_queue(struct sctp_association *asoc)
172 struct sctp_tmit_chunk *chk;
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);
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);
187 asoc->sent_queue_cnt++;
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);
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);
200 #endif /* SCTP_DEBUG */
204 sctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
205 struct sctp_nets *net, uint16_t threshold)
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);
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;
225 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
227 SCTP_FAILED_THRESHOLD,
231 /*********HOLD THIS COMMENT FOR PATCH OF ALTERNATE
232 *********ROUTING CODE
234 /*********HOLD THIS COMMENT FOR END OF PATCH OF ALTERNATE
235 *********ROUTING CODE
242 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
243 stcb->asoc.overall_error_count++;
246 stcb->asoc.overall_error_count++;
249 if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
250 printf("Overall error count for %p now %d thresh:%u state:%x\n",
252 stcb->asoc.overall_error_count,
253 (uint32_t) threshold,
254 ((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
256 #endif /* SCTP_DEBUG */
258 * We specifically do not do >= to give the assoc one more change
261 if (stcb->asoc.overall_error_count > threshold) {
262 /* Abort notification sends a ULP notify */
265 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
266 0, M_DONTWAIT, 1, MT_DATA);
268 struct sctp_paramhdr *ph;
271 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
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);
279 inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_1;
280 sctp_abort_an_association(inp, stcb, SCTP_FAILED_THRESHOLD, oper);
287 sctp_find_alternate_net(struct sctp_tcb *stcb,
288 struct sctp_nets *net,
289 int highest_ssthresh)
291 /* Find and return an alternate network if possible */
292 struct sctp_nets *alt, *mnet, *hthresh = NULL;
296 if (stcb->asoc.numnets == 1) {
297 /* No others but net */
298 return (TAILQ_FIRST(&stcb->asoc.nets));
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)
306 * will skip ones that are not-reachable or
311 if (val > mnet->ssthresh) {
313 val = mnet->ssthresh;
314 } else if (val == mnet->ssthresh) {
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;
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;
330 if (this_random % 2) {
332 val = mnet->ssthresh;
344 mnet = TAILQ_FIRST(&stcb->asoc.nets);
347 alt = TAILQ_NEXT(mnet, sctp_next);
353 alt = TAILQ_FIRST(&stcb->asoc.nets);
355 if (alt->ro.ro_rt == NULL) {
356 struct sockaddr_in6 *sin6;
358 sin6 = (struct sockaddr_in6 *)&alt->ro._l_addr;
359 if (sin6->sin6_family == AF_INET6) {
360 (void)sa6_embedscope(sin6, ip6_use_defzone);
362 rtalloc_ign((struct route *)&alt->ro, 0UL);
363 if (sin6->sin6_family == AF_INET6) {
364 (void)sa6_recoverscope(sin6);
366 if (alt->ro._s_addr) {
367 sctp_free_ifa(alt->ro._s_addr);
368 alt->ro._s_addr = NULL;
370 alt->src_addr_selected = 0;
373 ((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
374 (alt->ro.ro_rt != NULL) &&
375 (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))
377 /* Found a reachable address */
381 } while (alt != NULL);
384 /* Case where NO insv network exists (dormant state) */
385 /* we rotate destinations */
389 alt = TAILQ_NEXT(mnet, sctp_next);
395 alt = TAILQ_FIRST(&stcb->asoc.nets);
397 if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
399 /* Found an alternate address */
403 } while (alt != NULL);
412 sctp_backoff_on_timeout(struct sctp_tcb *stcb,
413 struct sctp_nets *net,
418 if (net->RTO > stcb->asoc.maxrto) {
419 net->RTO = stcb->asoc.maxrto;
421 if ((win_probe == 0) && num_marked) {
422 /* We don't apply penalty to window probe scenarios */
423 #ifdef SCTP_CWND_MONITOR
424 int old_cwnd = net->cwnd;
427 net->ssthresh = net->cwnd >> 1;
428 if (net->ssthresh < (net->mtu << 1)) {
429 net->ssthresh = (net->mtu << 1);
431 net->cwnd = net->mtu;
433 if (net->cwnd < net->mtu)
434 net->cwnd = net->mtu;
435 #ifdef SCTP_CWND_MONITOR
436 sctp_log_cwnd(stcb, net, net->cwnd - old_cwnd, SCTP_CWND_LOG_FROM_RTX);
439 net->partial_bytes_acked = 0;
444 sctp_mark_all_for_resend(struct sctp_tcb *stcb,
445 struct sctp_nets *net,
446 struct sctp_nets *alt,
452 * Mark all chunks (well not all) that were sent to *net for
453 * retransmission. Move them to alt for there destination as well...
454 * We only mark chunks that have been outstanding long enough to
455 * have received feed-back.
457 struct sctp_tmit_chunk *chk, *tp2, *could_be_sent = NULL;
458 struct sctp_nets *lnets;
459 struct timeval now, min_wait, tv;
461 int orig_rwnd, audit_tf, num_mk, fir;
463 uint32_t orig_flight;
464 uint32_t tsnlast, tsnfirst;
467 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is being used,
468 * then pick dest with largest ssthresh for any retransmission.
469 * (iyengar@cis.udel.edu, 2005/08/12)
471 if (sctp_cmt_on_off) {
472 alt = sctp_find_alternate_net(stcb, net, 1);
474 * CUCv2: If a different dest is picked for the
475 * retransmission, then new (rtx-)pseudo_cumack needs to be
476 * tracked for orig dest. Let CUCv2 track new (rtx-)
477 * pseudo-cumack always.
479 net->find_pseudo_cumack = 1;
480 net->find_rtx_pseudo_cumack = 1;
482 /* none in flight now */
486 * figure out how long a data chunk must be pending before we can
489 SCTP_GETTIME_TIMEVAL(&now);
490 /* get cur rto in micro-seconds */
491 cur_rtt = (((net->lastsa >> 2) + net->lastsv) >> 1);
493 #if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
495 stcb->asoc.peers_rwnd,
497 SCTP_FR_T3_MARK_TIME);
498 sctp_log_fr(net->flight_size,
499 SCTP_OS_TIMER_PENDING(&net->fr_timer.timer),
500 SCTP_OS_TIMER_ACTIVE(&net->fr_timer.timer),
501 SCTP_FR_CWND_REPORT);
502 sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
504 tv.tv_sec = cur_rtt / 1000000;
505 tv.tv_usec = cur_rtt % 1000000;
507 timevalsub(&min_wait, &tv);
508 if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
510 * if we hit here, we don't have enough seconds on the clock
511 * to account for the RTO. We just let the lower seconds be
512 * the bounds and don't worry about it. This may mean we
513 * will mark a lot more than we should.
515 min_wait.tv_sec = min_wait.tv_usec = 0;
517 #if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
518 sctp_log_fr(cur_rtt, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
519 sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
522 * Our rwnd will be incorrect here since we are not adding back the
523 * cnt * mbuf but we will fix that down below.
525 orig_rwnd = stcb->asoc.peers_rwnd;
526 orig_flight = net->flight_size;
527 net->rto_pending = 0;
528 net->fast_retran_ip = 0;
529 /* Now on to each chunk */
531 tsnfirst = tsnlast = 0;
532 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
533 for (; chk != NULL; chk = tp2) {
534 tp2 = TAILQ_NEXT(chk, sctp_next);
535 if ((compare_with_wrap(stcb->asoc.last_acked_seq,
536 chk->rec.data.TSN_seq,
538 (stcb->asoc.last_acked_seq == chk->rec.data.TSN_seq)) {
539 /* Strange case our list got out of order? */
540 printf("Our list is out of order?\n");
541 panic("Out of order list");
543 if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
545 * found one to mark: If it is less than
546 * DATAGRAM_ACKED it MUST not be a skipped or marked
547 * TSN but instead one that is either already set
548 * for retransmission OR one that needs
552 /* validate its been outstanding long enough */
553 #if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
554 sctp_log_fr(chk->rec.data.TSN_seq,
555 chk->sent_rcv_time.tv_sec,
556 chk->sent_rcv_time.tv_usec,
557 SCTP_FR_T3_MARK_TIME);
559 if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
561 * we have reached a chunk that was sent
562 * some seconds past our min.. forget it we
563 * will find no more to send.
565 #if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
567 chk->sent_rcv_time.tv_sec,
568 chk->sent_rcv_time.tv_usec,
572 } else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
573 (window_probe == 0)) {
575 * we must look at the micro seconds to
578 if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
580 * ok it was sent after our boundary
583 #if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
585 chk->sent_rcv_time.tv_sec,
586 chk->sent_rcv_time.tv_usec,
592 if (PR_SCTP_TTL_ENABLED(chk->flags)) {
594 if ((now.tv_sec > chk->rec.data.timetodrop.tv_sec) ||
595 ((chk->rec.data.timetodrop.tv_sec == now.tv_sec) &&
596 (now.tv_usec > chk->rec.data.timetodrop.tv_usec))) {
599 sctp_release_pr_sctp_chunk(stcb,
601 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
602 &stcb->asoc.sent_queue);
607 if (PR_SCTP_RTX_ENABLED(chk->flags)) {
608 /* Has it been retransmitted tv_sec times? */
609 if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
611 sctp_release_pr_sctp_chunk(stcb,
613 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
614 &stcb->asoc.sent_queue);
619 if (chk->sent != SCTP_DATAGRAM_RESEND) {
620 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
624 tsnfirst = chk->rec.data.TSN_seq;
626 tsnlast = chk->rec.data.TSN_seq;
627 #if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
628 sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
629 0, SCTP_FR_T3_MARKED);
633 if (stcb->asoc.total_flight_count > 0)
634 stcb->asoc.total_flight_count--;
635 if (chk->rec.data.chunk_was_revoked) {
636 /* deflate the cwnd */
637 chk->whoTo->cwnd -= chk->book_size;
638 chk->rec.data.chunk_was_revoked = 0;
640 chk->sent = SCTP_DATAGRAM_RESEND;
641 SCTP_STAT_INCR(sctps_markedretrans);
642 net->marked_retrans++;
643 stcb->asoc.marked_retrans++;
644 #ifdef SCTP_FLIGHT_LOGGING
645 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN,
646 chk->whoTo->flight_size,
649 chk->rec.data.TSN_seq);
652 if (net->flight_size >= chk->book_size)
653 net->flight_size -= chk->book_size;
655 net->flight_size = 0;
657 stcb->asoc.peers_rwnd += chk->send_size;
658 stcb->asoc.peers_rwnd += sctp_peer_chunk_oh;
660 /* reset the TSN for striking and other FR stuff */
661 chk->rec.data.doing_fast_retransmit = 0;
662 /* Clear any time so NO RTT is being done */
665 sctp_free_remote_addr(chk->whoTo);
666 chk->no_fr_allowed = 1;
668 atomic_add_int(&alt->ref_count, 1);
670 chk->no_fr_allowed = 0;
671 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
672 chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
674 chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
677 if (sctp_cmt_on_off == 1) {
678 chk->no_fr_allowed = 1;
680 } else if (chk->sent == SCTP_DATAGRAM_ACKED) {
681 /* remember highest acked one */
684 if (chk->sent == SCTP_DATAGRAM_RESEND) {
688 #if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
689 sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
692 if (stcb->asoc.total_flight >= (orig_flight - net->flight_size)) {
693 stcb->asoc.total_flight -= (orig_flight - net->flight_size);
695 stcb->asoc.total_flight = 0;
696 stcb->asoc.total_flight_count = 0;
701 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
703 printf("LAST TSN marked was %x\n", tsnlast);
704 printf("Num marked for retransmission was %d peer-rwd:%ld\n",
705 num_mk, (u_long)stcb->asoc.peers_rwnd);
706 printf("LAST TSN marked was %x\n", tsnlast);
707 printf("Num marked for retransmission was %d peer-rwd:%d\n",
709 (int)stcb->asoc.peers_rwnd
714 *num_marked = num_mk;
715 if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
716 /* fix it so we retransmit the highest acked anyway */
717 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
719 could_be_sent->sent = SCTP_DATAGRAM_RESEND;
721 if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
723 printf("Local Audit says there are %d for retran asoc cnt:%d\n",
724 cnt_mk, stcb->asoc.sent_queue_retran_cnt);
726 #ifndef SCTP_AUDITING_ENABLED
727 stcb->asoc.sent_queue_retran_cnt = cnt_mk;
730 /* Now check for a ECN Echo that may be stranded */
731 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
732 if ((chk->whoTo == net) &&
733 (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
734 sctp_free_remote_addr(chk->whoTo);
736 if (chk->sent != SCTP_DATAGRAM_RESEND) {
737 chk->sent = SCTP_DATAGRAM_RESEND;
738 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
740 atomic_add_int(&alt->ref_count, 1);
745 if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
746 printf("Audit total flight due to negative value net:%p\n",
749 #endif /* SCTP_DEBUG */
750 stcb->asoc.total_flight = 0;
751 stcb->asoc.total_flight_count = 0;
752 /* Clear all networks flight size */
753 TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
754 lnets->flight_size = 0;
756 if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
757 printf("Net:%p c-f cwnd:%d ssthresh:%d\n",
758 lnets, lnets->cwnd, lnets->ssthresh);
760 #endif /* SCTP_DEBUG */
762 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
763 if (chk->sent < SCTP_DATAGRAM_RESEND) {
764 #ifdef SCTP_FLIGHT_LOGGING
765 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
766 chk->whoTo->flight_size,
769 chk->rec.data.TSN_seq);
771 stcb->asoc.total_flight += chk->book_size;
772 chk->whoTo->flight_size += chk->book_size;
773 stcb->asoc.total_flight_count++;
778 * Setup the ecn nonce re-sync point. We do this since
779 * retranmissions are NOT setup for ECN. This means that do to
780 * Karn's rule, we don't know the total of the peers ecn bits.
782 chk = TAILQ_FIRST(&stcb->asoc.send_queue);
784 stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
786 stcb->asoc.nonce_resync_tsn = chk->rec.data.TSN_seq;
788 stcb->asoc.nonce_wait_for_ecne = 0;
789 stcb->asoc.nonce_sum_check = 0;
790 /* We return 1 if we only have a window probe outstanding */
795 sctp_move_all_chunks_to_alt(struct sctp_tcb *stcb,
796 struct sctp_nets *net,
797 struct sctp_nets *alt)
799 struct sctp_association *asoc;
800 struct sctp_stream_out *outs;
801 struct sctp_tmit_chunk *chk;
802 struct sctp_stream_queue_pending *sp;
811 * now through all the streams checking for chunks sent to our bad
814 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
815 /* now clean up any chunks here */
816 TAILQ_FOREACH(sp, &outs->outqueue, next) {
817 if (sp->net == net) {
818 sctp_free_remote_addr(sp->net);
820 atomic_add_int(&alt->ref_count, 1);
824 /* Now check the pending queue */
825 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
826 if (chk->whoTo == net) {
827 sctp_free_remote_addr(chk->whoTo);
829 atomic_add_int(&alt->ref_count, 1);
836 sctp_t3rxt_timer(struct sctp_inpcb *inp,
837 struct sctp_tcb *stcb,
838 struct sctp_nets *net)
840 struct sctp_nets *alt;
841 int win_probe, num_mk;
843 #ifdef SCTP_FR_LOGGING
844 sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
845 #ifdef SCTP_CWND_LOGGING
847 struct sctp_nets *lnet;
849 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
851 sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
853 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
859 /* Find an alternate and mark those for retransmission */
860 if ((stcb->asoc.peers_rwnd == 0) &&
861 (stcb->asoc.total_flight < net->mtu)) {
862 SCTP_STAT_INCR(sctps_timowindowprobe);
867 alt = sctp_find_alternate_net(stcb, net, 0);
868 sctp_mark_all_for_resend(stcb, net, alt, win_probe, &num_mk);
869 /* FR Loss recovery just ended with the T3. */
870 stcb->asoc.fast_retran_loss_recovery = 0;
872 /* CMT FR loss recovery ended with the T3 */
873 net->fast_retran_loss_recovery = 0;
876 * setup the sat loss recovery that prevents satellite cwnd advance.
878 stcb->asoc.sat_t3_loss_recovery = 1;
879 stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
881 /* Backoff the timer and cwnd */
882 sctp_backoff_on_timeout(stcb, net, win_probe, num_mk);
883 if (win_probe == 0) {
884 /* We don't do normal threshold management on window probes */
885 if (sctp_threshold_management(inp, stcb, net,
886 stcb->asoc.max_send_times)) {
887 /* Association was destroyed */
890 if (net != stcb->asoc.primary_destination) {
891 /* send a immediate HB if our RTO is stale */
893 unsigned int ms_goneby;
895 SCTP_GETTIME_TIMEVAL(&now);
896 if (net->last_sent_time.tv_sec) {
897 ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
901 if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
903 * no recent feed back in an RTO or
904 * more, request a RTT update
906 sctp_send_hb(stcb, 1, net);
912 * For a window probe we don't penalize the net's but only
913 * the association. This may fail it if SACKs are not coming
914 * back. If sack's are coming with rwnd locked at 0, we will
915 * continue to hold things waiting for rwnd to raise
917 if (sctp_threshold_management(inp, stcb, NULL,
918 stcb->asoc.max_send_times)) {
919 /* Association was destroyed */
923 if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
924 /* Move all pending over too */
925 sctp_move_all_chunks_to_alt(stcb, net, alt);
926 /* Was it our primary? */
927 if ((stcb->asoc.primary_destination == net) && (alt != net)) {
929 * Yes, note it as such and find an alternate note:
930 * this means HB code must use this to resent the
931 * primary if it goes active AND if someone does a
932 * change-primary then this flag must be cleared
933 * from any net structures.
935 if (sctp_set_primary_addr(stcb,
936 (struct sockaddr *)NULL,
938 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
939 if (net->ro._s_addr) {
940 sctp_free_ifa(net->ro._s_addr);
941 net->ro._s_addr = NULL;
943 net->src_addr_selected = 0;
948 * Special case for cookie-echo'ed case, we don't do output but must
949 * await the COOKIE-ACK before retransmission
951 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
953 * Here we just reset the timer and start again since we
954 * have not established the asoc
956 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
959 if (stcb->asoc.peer_supports_prsctp) {
960 struct sctp_tmit_chunk *lchk;
962 lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
963 /* C3. See if we need to send a Fwd-TSN */
964 if (compare_with_wrap(stcb->asoc.advanced_peer_ack_point,
965 stcb->asoc.last_acked_seq, MAX_TSN)) {
967 * ISSUE with ECN, see FWD-TSN processing for notes
968 * on issues that will occur when the ECN NONCE
969 * stuff is put into SCTP for cross checking.
971 send_forward_tsn(stcb, &stcb->asoc);
973 /* Assure a timer is up */
974 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
978 #ifdef SCTP_CWND_MONITOR
979 sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
985 sctp_t1init_timer(struct sctp_inpcb *inp,
986 struct sctp_tcb *stcb,
987 struct sctp_nets *net)
989 /* bump the thresholds */
990 if (stcb->asoc.delayed_connection) {
992 * special hook for delayed connection. The library did NOT
993 * complete the rest of its sends.
995 stcb->asoc.delayed_connection = 0;
996 sctp_send_initiate(inp, stcb);
999 if (SCTP_GET_STATE((&stcb->asoc)) != SCTP_STATE_COOKIE_WAIT) {
1002 if (sctp_threshold_management(inp, stcb, net,
1003 stcb->asoc.max_init_times)) {
1004 /* Association was destroyed */
1007 stcb->asoc.dropped_special_cnt = 0;
1008 sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0);
1009 if (stcb->asoc.initial_init_rto_max < net->RTO) {
1010 net->RTO = stcb->asoc.initial_init_rto_max;
1012 if (stcb->asoc.numnets > 1) {
1013 /* If we have more than one addr use it */
1014 struct sctp_nets *alt;
1016 alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1017 if ((alt != NULL) && (alt != stcb->asoc.primary_destination)) {
1018 sctp_move_all_chunks_to_alt(stcb, stcb->asoc.primary_destination, alt);
1019 stcb->asoc.primary_destination = alt;
1022 /* Send out a new init */
1023 sctp_send_initiate(inp, stcb);
1028 * For cookie and asconf we actually need to find and mark for resend, then
1029 * increment the resend counter (after all the threshold management stuff of
1033 sctp_cookie_timer(struct sctp_inpcb *inp,
1034 struct sctp_tcb *stcb,
1035 struct sctp_nets *net)
1037 struct sctp_nets *alt;
1038 struct sctp_tmit_chunk *cookie;
1040 /* first before all else we must find the cookie */
1041 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1042 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1046 if (cookie == NULL) {
1047 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1051 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1052 0, M_DONTWAIT, 1, MT_DATA);
1054 struct sctp_paramhdr *ph;
1057 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1059 ph = mtod(oper, struct sctp_paramhdr *);
1060 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1061 ph->param_length = htons(SCTP_BUF_LEN(oper));
1062 ippp = (uint32_t *) (ph + 1);
1063 *ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_2);
1065 inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_3;
1066 sctp_abort_an_association(inp, stcb, SCTP_INTERNAL_ERROR,
1070 panic("Cookie timer expires in wrong state?");
1072 printf("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(&stcb->asoc));
1078 /* Ok we found the cookie, threshold management next */
1079 if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1080 stcb->asoc.max_init_times)) {
1085 * cleared theshold management now lets backoff the address & select
1088 stcb->asoc.dropped_special_cnt = 0;
1089 sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0);
1090 alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1091 if (alt != cookie->whoTo) {
1092 sctp_free_remote_addr(cookie->whoTo);
1093 cookie->whoTo = alt;
1094 atomic_add_int(&alt->ref_count, 1);
1096 /* Now mark the retran info */
1097 if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1098 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1100 cookie->sent = SCTP_DATAGRAM_RESEND;
1102 * Now call the output routine to kick out the cookie again, Note we
1103 * don't mark any chunks for retran so that FR will need to kick in
1104 * to move these (or a send timer).
1110 sctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1111 struct sctp_nets *net)
1113 struct sctp_nets *alt;
1114 struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1116 if (stcb->asoc.stream_reset_outstanding == 0) {
1119 /* find the existing STRRESET, we use the seq number we sent out on */
1120 sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1121 if (strrst == NULL) {
1124 /* do threshold management */
1125 if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1126 stcb->asoc.max_send_times)) {
1131 * cleared theshold management now lets backoff the address & select
1134 sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0);
1135 alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
1136 sctp_free_remote_addr(strrst->whoTo);
1137 strrst->whoTo = alt;
1138 atomic_add_int(&alt->ref_count, 1);
1140 /* See if a ECN Echo is also stranded */
1141 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1142 if ((chk->whoTo == net) &&
1143 (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1144 sctp_free_remote_addr(chk->whoTo);
1145 if (chk->sent != SCTP_DATAGRAM_RESEND) {
1146 chk->sent = SCTP_DATAGRAM_RESEND;
1147 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1150 atomic_add_int(&alt->ref_count, 1);
1153 if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1155 * If the address went un-reachable, we need to move to
1156 * alternates for ALL chk's in queue
1158 sctp_move_all_chunks_to_alt(stcb, net, alt);
1160 /* mark the retran info */
1161 if (strrst->sent != SCTP_DATAGRAM_RESEND)
1162 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1163 strrst->sent = SCTP_DATAGRAM_RESEND;
1165 /* restart the timer */
1166 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1171 sctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1172 struct sctp_nets *net)
1174 struct sctp_nets *alt;
1175 struct sctp_tmit_chunk *asconf, *chk;
1177 /* is this the first send, or a retransmission? */
1178 if (stcb->asoc.asconf_sent == 0) {
1179 /* compose a new ASCONF chunk and send it */
1180 sctp_send_asconf(stcb, net);
1182 /* Retransmission of the existing ASCONF needed... */
1184 /* find the existing ASCONF */
1185 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
1187 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
1191 if (asconf == NULL) {
1194 /* do threshold management */
1195 if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1196 stcb->asoc.max_send_times)) {
1201 * PETER? FIX? How will the following code ever run? If the
1202 * max_send_times is hit, threshold managment will blow away
1205 if (asconf->snd_count > stcb->asoc.max_send_times) {
1207 * Something is rotten, peer is not responding to
1208 * ASCONFs but maybe is to data etc. e.g. it is not
1209 * properly handling the chunk type upper bits Mark
1210 * this peer as ASCONF incapable and cleanup
1213 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1214 printf("asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1216 #endif /* SCTP_DEBUG */
1217 sctp_asconf_cleanup(stcb, net);
1221 * cleared theshold management now lets backoff the address
1222 * & select an alternate
1224 sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0);
1225 alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
1226 sctp_free_remote_addr(asconf->whoTo);
1227 asconf->whoTo = alt;
1228 atomic_add_int(&alt->ref_count, 1);
1230 /* See if a ECN Echo is also stranded */
1231 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1232 if ((chk->whoTo == net) &&
1233 (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1234 sctp_free_remote_addr(chk->whoTo);
1236 if (chk->sent != SCTP_DATAGRAM_RESEND) {
1237 chk->sent = SCTP_DATAGRAM_RESEND;
1238 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1240 atomic_add_int(&alt->ref_count, 1);
1243 if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1245 * If the address went un-reachable, we need to move
1246 * to alternates for ALL chk's in queue
1248 sctp_move_all_chunks_to_alt(stcb, net, alt);
1250 /* mark the retran info */
1251 if (asconf->sent != SCTP_DATAGRAM_RESEND)
1252 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1253 asconf->sent = SCTP_DATAGRAM_RESEND;
1259 * For the shutdown and shutdown-ack, we do not keep one around on the
1260 * control queue. This means we must generate a new one and call the general
1261 * chunk output routine, AFTER having done threshold management.
1264 sctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1265 struct sctp_nets *net)
1267 struct sctp_nets *alt;
1269 /* first threshold managment */
1270 if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1274 /* second select an alternative */
1275 alt = sctp_find_alternate_net(stcb, net, 0);
1277 /* third generate a shutdown into the queue for out net */
1279 sctp_send_shutdown(stcb, alt);
1282 * if alt is NULL, there is no dest to send to??
1286 /* fourth restart timer */
1287 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1292 sctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1293 struct sctp_nets *net)
1295 struct sctp_nets *alt;
1297 /* first threshold managment */
1298 if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1302 /* second select an alternative */
1303 alt = sctp_find_alternate_net(stcb, net, 0);
1305 /* third generate a shutdown into the queue for out net */
1306 sctp_send_shutdown_ack(stcb, alt);
1308 /* fourth restart timer */
1309 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1314 sctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1315 struct sctp_tcb *stcb)
1317 struct sctp_stream_out *outs;
1318 struct sctp_stream_queue_pending *sp;
1319 unsigned int chks_in_queue = 0;
1320 int being_filled = 0;
1323 * This function is ONLY called when the send/sent queues are empty.
1325 if ((stcb == NULL) || (inp == NULL))
1328 if (stcb->asoc.sent_queue_retran_cnt) {
1329 printf("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1330 stcb->asoc.sent_queue_retran_cnt);
1331 stcb->asoc.sent_queue_retran_cnt = 0;
1333 SCTP_TCB_SEND_LOCK(stcb);
1334 if (TAILQ_EMPTY(&stcb->asoc.out_wheel)) {
1337 /* Check to see if a spoke fell off the wheel */
1338 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1339 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1340 sctp_insert_on_wheel(stcb, &stcb->asoc, &stcb->asoc.strmout[i], 1);
1345 /* yep, we lost a spoke or two */
1346 printf("Found an additional %d streams NOT on outwheel, corrected\n", cnt);
1348 /* no spokes lost, */
1349 stcb->asoc.total_output_queue_size = 0;
1351 SCTP_TCB_SEND_UNLOCK(stcb);
1354 SCTP_TCB_SEND_UNLOCK(stcb);
1355 /* Check to see if some data queued, if so report it */
1356 TAILQ_FOREACH(outs, &stcb->asoc.out_wheel, next_spoke) {
1357 if (!TAILQ_EMPTY(&outs->outqueue)) {
1358 TAILQ_FOREACH(sp, &outs->outqueue, next) {
1359 if (sp->msg_is_complete)
1365 if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1366 printf("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1367 stcb->asoc.stream_queue_cnt, chks_in_queue);
1369 if (chks_in_queue) {
1370 /* call the output queue function */
1371 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3);
1372 if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1373 (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1375 * Probably should go in and make it go back through
1376 * and add fragments allowed
1378 if (being_filled == 0) {
1379 printf("Still nothing moved %d chunks are stuck\n",
1384 printf("Found no chunks on any queue tot:%lu\n",
1385 (u_long)stcb->asoc.total_output_queue_size);
1386 stcb->asoc.total_output_queue_size = 0;
1391 sctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1392 struct sctp_nets *net, int cnt_of_unconf)
1395 if (net->hb_responded == 0) {
1396 if (net->ro._s_addr) {
1398 * Invalidate the src address if we did not
1399 * get a response last time.
1401 sctp_free_ifa(net->ro._s_addr);
1402 net->ro._s_addr = NULL;
1403 net->src_addr_selected = 0;
1405 sctp_backoff_on_timeout(stcb, net, 1, 0);
1407 /* Zero PBA, if it needs it */
1408 if (net->partial_bytes_acked) {
1409 net->partial_bytes_acked = 0;
1412 if ((stcb->asoc.total_output_queue_size > 0) &&
1413 (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1414 (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1415 sctp_audit_stream_queues_for_size(inp, stcb);
1417 /* Send a new HB, this will do threshold managment, pick a new dest */
1418 if (cnt_of_unconf == 0) {
1419 if (sctp_send_hb(stcb, 0, NULL) < 0) {
1424 * this will send out extra hb's up to maxburst if there are
1425 * any unconfirmed addresses.
1429 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1430 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1431 (net->dest_state & SCTP_ADDR_REACHABLE)) {
1433 if (net->hb_responded == 0) {
1434 /* Did we respond last time? */
1435 if (net->ro._s_addr) {
1436 sctp_free_ifa(net->ro._s_addr);
1437 net->ro._s_addr = NULL;
1438 net->src_addr_selected = 0;
1441 if (sctp_send_hb(stcb, 1, net) == 0) {
1444 if (cnt_sent >= sctp_hb_maxburst)
1453 sctp_is_hb_timer_running(struct sctp_tcb *stcb)
1455 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.hb_timer.timer)) {
1465 sctp_is_sack_timer_running(struct sctp_tcb *stcb)
1467 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
1476 #define SCTP_NUMBER_OF_MTU_SIZES 18
1477 static uint32_t mtu_sizes[] = {
1500 sctp_getnext_mtu(struct sctp_inpcb *inp, uint32_t cur_mtu)
1502 /* select another MTU that is just bigger than this one */
1505 for (i = 0; i < SCTP_NUMBER_OF_MTU_SIZES; i++) {
1506 if (cur_mtu < mtu_sizes[i]) {
1507 /* no max_mtu is bigger than this one */
1508 return (mtu_sizes[i]);
1511 /* here return the highest allowable */
1517 sctp_pathmtu_timer(struct sctp_inpcb *inp,
1518 struct sctp_tcb *stcb,
1519 struct sctp_nets *net)
1523 /* restart the timer in any case */
1524 next_mtu = sctp_getnext_mtu(inp, net->mtu);
1525 if (next_mtu <= net->mtu) {
1529 if (net->ro.ro_rt != NULL) {
1531 * only if we have a route and interface do we set anything.
1532 * Note we always restart the timer though just in case it
1533 * is updated (i.e. the ifp) or route/ifp is populated.
1535 if (net->ro.ro_rt->rt_ifp != NULL) {
1536 if (net->ro.ro_rt->rt_ifp->if_mtu > next_mtu) {
1537 /* ok it will fit out the door */
1538 net->mtu = next_mtu;
1542 /* restart the timer */
1543 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1547 sctp_autoclose_timer(struct sctp_inpcb *inp,
1548 struct sctp_tcb *stcb,
1549 struct sctp_nets *net)
1551 struct timeval tn, *tim_touse;
1552 struct sctp_association *asoc;
1555 SCTP_GETTIME_TIMEVAL(&tn);
1556 if (stcb->asoc.sctp_autoclose_ticks &&
1557 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1558 /* Auto close is on */
1560 /* pick the time to use */
1561 if (asoc->time_last_rcvd.tv_sec >
1562 asoc->time_last_sent.tv_sec) {
1563 tim_touse = &asoc->time_last_rcvd;
1565 tim_touse = &asoc->time_last_sent;
1567 /* Now has long enough transpired to autoclose? */
1568 ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
1569 if ((ticks_gone_by > 0) &&
1570 (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1572 * autoclose time has hit, call the output routine,
1573 * which should do nothing just to be SURE we don't
1574 * have hanging data. We can then safely check the
1575 * queues and know that we are clear to send
1578 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR);
1580 if (TAILQ_EMPTY(&asoc->send_queue) &&
1581 TAILQ_EMPTY(&asoc->sent_queue)) {
1583 * there is nothing queued to send, so I'm
1586 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1587 /* only send SHUTDOWN 1st time thru */
1588 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
1589 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1590 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1591 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1593 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1594 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1595 stcb->sctp_ep, stcb,
1596 asoc->primary_destination);
1597 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1598 stcb->sctp_ep, stcb,
1599 asoc->primary_destination);
1604 * No auto close at this time, reset t-o to check
1609 /* fool the timer startup to use the time left */
1610 tmp = asoc->sctp_autoclose_ticks;
1611 asoc->sctp_autoclose_ticks -= ticks_gone_by;
1612 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1614 /* restore the real tick value */
1615 asoc->sctp_autoclose_ticks = tmp;
1621 sctp_iterator_timer(struct sctp_iterator *it)
1623 int iteration_count = 0;
1627 * only one iterator can run at a time. This is the only way we can
1628 * cleanly pull ep's from underneath all the running interators when
1631 SCTP_ITERATOR_LOCK();
1632 if (it->inp == NULL) {
1633 /* iterator is complete */
1635 SCTP_ITERATOR_UNLOCK();
1636 SCTP_INP_INFO_WLOCK();
1637 TAILQ_REMOVE(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
1638 /* stopping the callout is not needed, in theory */
1639 SCTP_INP_INFO_WUNLOCK();
1640 SCTP_OS_TIMER_STOP(&it->tmr.timer);
1641 if (it->function_atend != NULL) {
1642 (*it->function_atend) (it->pointer, it->val);
1648 SCTP_INP_WLOCK(it->inp);
1649 while (((it->pcb_flags) &&
1650 ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1651 ((it->pcb_features) &&
1652 ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1653 /* endpoint flags or features don't match, so keep looking */
1654 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1655 SCTP_INP_WUNLOCK(it->inp);
1656 goto done_with_iterator;
1658 SCTP_INP_WUNLOCK(it->inp);
1659 it->inp = LIST_NEXT(it->inp, sctp_list);
1660 if (it->inp == NULL) {
1661 goto done_with_iterator;
1663 SCTP_INP_WLOCK(it->inp);
1665 if ((it->inp->inp_starting_point_for_iterator != NULL) &&
1666 (it->inp->inp_starting_point_for_iterator != it)) {
1667 printf("Iterator collision, waiting for one at %p\n",
1669 SCTP_INP_WUNLOCK(it->inp);
1670 goto start_timer_return;
1672 /* mark the current iterator on the endpoint */
1673 it->inp->inp_starting_point_for_iterator = it;
1674 SCTP_INP_WUNLOCK(it->inp);
1675 SCTP_INP_RLOCK(it->inp);
1676 /* now go through each assoc which is in the desired state */
1677 if (it->done_current_ep == 0) {
1678 if (it->function_inp != NULL)
1679 inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1680 it->done_current_ep = 1;
1682 if (it->stcb == NULL) {
1683 /* run the per instance function */
1684 it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1686 SCTP_INP_RUNLOCK(it->inp);
1687 if ((inp_skip) || it->stcb == NULL) {
1688 if (it->function_inp_end != NULL) {
1689 inp_skip = (*it->function_inp_end) (it->inp,
1696 (it->stcb->asoc.stcb_starting_point_for_iterator == it)) {
1697 it->stcb->asoc.stcb_starting_point_for_iterator = NULL;
1700 SCTP_TCB_LOCK(it->stcb);
1701 if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1702 /* not in the right state... keep looking */
1703 SCTP_TCB_UNLOCK(it->stcb);
1706 /* mark the current iterator on the assoc */
1707 it->stcb->asoc.stcb_starting_point_for_iterator = it;
1708 /* see if we have limited out the iterator loop */
1710 if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1712 /* set a timer to continue this later */
1713 SCTP_TCB_UNLOCK(it->stcb);
1714 sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR,
1715 (struct sctp_inpcb *)it, NULL, NULL);
1716 SCTP_ITERATOR_UNLOCK();
1719 /* run function on this one */
1720 (*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1723 * we lie here, it really needs to have its own type but
1724 * first I must verify that this won't effect things :-0
1726 if (it->no_chunk_output == 0)
1727 sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3);
1729 SCTP_TCB_UNLOCK(it->stcb);
1731 it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1732 if (it->stcb == NULL) {
1733 if (it->function_inp_end != NULL) {
1734 inp_skip = (*it->function_inp_end) (it->inp,
1741 /* done with all assocs on this endpoint, move on to next endpoint */
1742 it->done_current_ep = 0;
1743 SCTP_INP_WLOCK(it->inp);
1744 it->inp->inp_starting_point_for_iterator = NULL;
1745 SCTP_INP_WUNLOCK(it->inp);
1746 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1749 SCTP_INP_INFO_RLOCK();
1750 it->inp = LIST_NEXT(it->inp, sctp_list);
1751 SCTP_INP_INFO_RUNLOCK();
1753 if (it->inp == NULL) {
1754 goto done_with_iterator;
1756 goto select_a_new_ep;