2 * Copyright (c) 2001-2007, by 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 (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;
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 if (sctp_logging_level & SCTP_EARLYFR_LOGGING_ENABLE) {
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 */
141 * JRS - Use the congestion control given in the congestion
144 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer(inp, stcb, net);
145 } else if (cnt_resend) {
146 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_EARLY_FR_TMR, SCTP_SO_NOT_LOCKED);
149 if (net->flight_size < net->cwnd) {
150 SCTP_STAT_INCR(sctps_earlyfrstrtmr);
151 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net);
156 sctp_audit_retranmission_queue(struct sctp_association *asoc)
158 struct sctp_tmit_chunk *chk;
160 SCTPDBG(SCTP_DEBUG_TIMER4, "Audit invoked on send queue cnt:%d onqueue:%d\n",
161 asoc->sent_queue_retran_cnt,
162 asoc->sent_queue_cnt);
163 asoc->sent_queue_retran_cnt = 0;
164 asoc->sent_queue_cnt = 0;
165 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
166 if (chk->sent == SCTP_DATAGRAM_RESEND) {
167 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
169 asoc->sent_queue_cnt++;
171 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
172 if (chk->sent == SCTP_DATAGRAM_RESEND) {
173 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
176 SCTPDBG(SCTP_DEBUG_TIMER4, "Audit completes retran:%d onqueue:%d\n",
177 asoc->sent_queue_retran_cnt,
178 asoc->sent_queue_cnt);
182 sctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
183 struct sctp_nets *net, uint16_t threshold)
187 SCTPDBG(SCTP_DEBUG_TIMER4, "Error count for %p now %d thresh:%d\n",
188 net, net->error_count,
189 net->failure_threshold);
190 if (net->error_count > net->failure_threshold) {
191 /* We had a threshold failure */
192 if (net->dest_state & SCTP_ADDR_REACHABLE) {
193 net->dest_state &= ~SCTP_ADDR_REACHABLE;
194 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
195 net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
196 if (net == stcb->asoc.primary_destination) {
197 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
200 * JRS 5/14/07 - If a destination is
201 * unreachable, the PF bit is turned off.
202 * This allows an unambiguous use of the PF
203 * bit for destinations that are reachable
204 * but potentially failed. If the
205 * destination is set to the unreachable
206 * state, also set the destination to the PF
210 * Add debug message here if destination is
213 /* Stop any running T3 timers here? */
214 if (sctp_cmt_on_off && sctp_cmt_pf) {
215 net->dest_state &= ~SCTP_ADDR_PF;
216 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
219 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
221 SCTP_FAILED_THRESHOLD,
222 (void *)net, SCTP_SO_NOT_LOCKED);
225 /*********HOLD THIS COMMENT FOR PATCH OF ALTERNATE
226 *********ROUTING CODE
228 /*********HOLD THIS COMMENT FOR END OF PATCH OF ALTERNATE
229 *********ROUTING CODE
236 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
237 if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) {
238 sctp_misc_ints(SCTP_THRESHOLD_INCR,
239 stcb->asoc.overall_error_count,
240 (stcb->asoc.overall_error_count + 1),
241 SCTP_FROM_SCTP_TIMER,
244 stcb->asoc.overall_error_count++;
247 if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) {
248 sctp_misc_ints(SCTP_THRESHOLD_INCR,
249 stcb->asoc.overall_error_count,
250 (stcb->asoc.overall_error_count + 1),
251 SCTP_FROM_SCTP_TIMER,
254 stcb->asoc.overall_error_count++;
256 SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n",
257 &stcb->asoc, stcb->asoc.overall_error_count,
258 (uint32_t) threshold,
259 ((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
261 * We specifically do not do >= to give the assoc one more change
264 if (stcb->asoc.overall_error_count > threshold) {
265 /* Abort notification sends a ULP notify */
268 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
269 0, M_DONTWAIT, 1, MT_DATA);
271 struct sctp_paramhdr *ph;
274 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
276 ph = mtod(oper, struct sctp_paramhdr *);
277 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
278 ph->param_length = htons(SCTP_BUF_LEN(oper));
279 ippp = (uint32_t *) (ph + 1);
280 *ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_1);
282 inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_1;
283 printf("Aborting association threshold:%d overall error count:%d\n",
285 stcb->asoc.overall_error_count);
286 sctp_abort_an_association(inp, stcb, SCTP_FAILED_THRESHOLD, oper, SCTP_SO_NOT_LOCKED);
293 sctp_find_alternate_net(struct sctp_tcb *stcb,
294 struct sctp_nets *net,
297 /* Find and return an alternate network if possible */
298 struct sctp_nets *alt, *mnet, *min_errors_net = NULL, *max_cwnd_net = NULL;
301 /* JRS 5/14/07 - Initialize min_errors to an impossible value. */
303 uint32_t max_cwnd = 0;
305 if (stcb->asoc.numnets == 1) {
306 /* No others but net */
307 return (TAILQ_FIRST(&stcb->asoc.nets));
310 * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate
311 * net algorithm. This algorithm chooses the active destination (not
312 * in PF state) with the largest cwnd value. If all destinations are
313 * in PF state, unreachable, or unconfirmed, choose the desination
314 * that is in PF state with the lowest error count. In case of a
315 * tie, choose the destination that was most recently active.
318 TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
320 * JRS 5/14/07 - If the destination is unreachable
321 * or unconfirmed, skip it.
323 if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
324 (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
328 * JRS 5/14/07 - If the destination is reachable
329 * but in PF state, compare the error count of the
330 * destination to the minimum error count seen thus
331 * far. Store the destination with the lower error
332 * count. If the error counts are equal, store the
333 * destination that was most recently active.
335 if (mnet->dest_state & SCTP_ADDR_PF) {
337 * JRS 5/14/07 - If the destination under
338 * consideration is the current destination,
339 * work as if the error count is one higher.
340 * The actual error count will not be
341 * incremented until later in the t3
345 if (min_errors == -1) {
346 min_errors = mnet->error_count + 1;
347 min_errors_net = mnet;
348 } else if (mnet->error_count + 1 < min_errors) {
349 min_errors = mnet->error_count + 1;
350 min_errors_net = mnet;
351 } else if (mnet->error_count + 1 == min_errors
352 && mnet->last_active > min_errors_net->last_active) {
353 min_errors_net = mnet;
354 min_errors = mnet->error_count + 1;
358 if (min_errors == -1) {
359 min_errors = mnet->error_count;
360 min_errors_net = mnet;
361 } else if (mnet->error_count < min_errors) {
362 min_errors = mnet->error_count;
363 min_errors_net = mnet;
364 } else if (mnet->error_count == min_errors
365 && mnet->last_active > min_errors_net->last_active) {
366 min_errors_net = mnet;
367 min_errors = mnet->error_count;
373 * JRS 5/14/07 - If the destination is reachable and
374 * not in PF state, compare the cwnd of the
375 * destination to the highest cwnd seen thus far.
376 * Store the destination with the higher cwnd value.
377 * If the cwnd values are equal, randomly choose one
378 * of the two destinations.
380 if (max_cwnd < mnet->cwnd) {
382 max_cwnd = mnet->cwnd;
383 } else if (max_cwnd == mnet->cwnd) {
387 if (stcb->asoc.hb_random_idx > 3) {
388 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
389 memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values));
390 this_random = stcb->asoc.hb_random_values[0];
391 stcb->asoc.hb_random_idx++;
392 stcb->asoc.hb_ect_randombit = 0;
394 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
395 stcb->asoc.hb_random_idx++;
396 stcb->asoc.hb_ect_randombit = 0;
398 if (this_random % 2 == 1) {
400 max_cwnd = mnet->cwnd;
406 * JRS 5/14/07 - After all destination have been considered
407 * as alternates, check to see if there was some active
408 * destination (not in PF state). If not, check to see if
409 * there was some PF destination with the minimum number of
410 * errors. If not, return the original destination. If
411 * there is a min_errors_net, remove the PF flag from that
412 * destination, set the cwnd to one or two MTUs, and return
413 * the destination as an alt. If there was some active
414 * destination with a highest cwnd, return the destination
417 if (max_cwnd_net == NULL) {
418 if (min_errors_net == NULL) {
421 min_errors_net->dest_state &= ~SCTP_ADDR_PF;
422 min_errors_net->cwnd = min_errors_net->mtu * sctp_cmt_pf;
423 if (SCTP_OS_TIMER_PENDING(&min_errors_net->rxt_timer.timer)) {
424 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
425 stcb, min_errors_net,
426 SCTP_FROM_SCTP_TIMER + SCTP_LOC_2);
428 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to active with %d errors.\n",
429 min_errors_net, min_errors_net->error_count);
430 return (min_errors_net);
432 return (max_cwnd_net);
436 * JRS 5/14/07 - If mode is set to 1, use the CMT policy for
437 * choosing an alternate net.
439 else if (mode == 1) {
440 TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
441 if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
442 (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)
445 * will skip ones that are not-reachable or
450 if (max_cwnd < mnet->cwnd) {
452 max_cwnd = mnet->cwnd;
453 } else if (max_cwnd == mnet->cwnd) {
457 if (stcb->asoc.hb_random_idx > 3) {
458 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
459 memcpy(stcb->asoc.hb_random_values, &rndval,
460 sizeof(stcb->asoc.hb_random_values));
461 this_random = stcb->asoc.hb_random_values[0];
462 stcb->asoc.hb_random_idx = 0;
463 stcb->asoc.hb_ect_randombit = 0;
465 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
466 stcb->asoc.hb_random_idx++;
467 stcb->asoc.hb_ect_randombit = 0;
469 if (this_random % 2) {
471 max_cwnd = mnet->cwnd;
476 return (max_cwnd_net);
483 mnet = TAILQ_FIRST(&stcb->asoc.nets);
486 alt = TAILQ_NEXT(mnet, sctp_next);
492 alt = TAILQ_FIRST(&stcb->asoc.nets);
494 if (alt->ro.ro_rt == NULL) {
495 if (alt->ro._s_addr) {
496 sctp_free_ifa(alt->ro._s_addr);
497 alt->ro._s_addr = NULL;
499 alt->src_addr_selected = 0;
502 ((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
503 (alt->ro.ro_rt != NULL) &&
504 /* sa_ignore NO_NULL_CHK */
505 (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))
507 /* Found a reachable address */
511 } while (alt != NULL);
514 /* Case where NO insv network exists (dormant state) */
515 /* we rotate destinations */
519 alt = TAILQ_NEXT(mnet, sctp_next);
525 alt = TAILQ_FIRST(&stcb->asoc.nets);
527 /* sa_ignore NO_NULL_CHK */
528 if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
530 /* Found an alternate address */
534 } while (alt != NULL);
545 sctp_backoff_on_timeout(struct sctp_tcb *stcb,
546 struct sctp_nets *net,
551 net->RTO = stcb->asoc.minrto;
554 if (net->RTO > stcb->asoc.maxrto) {
555 net->RTO = stcb->asoc.maxrto;
557 if ((win_probe == 0) && num_marked) {
558 /* We don't apply penalty to window probe scenarios */
559 /* JRS - Use the congestion control given in the CC module */
560 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
565 sctp_mark_all_for_resend(struct sctp_tcb *stcb,
566 struct sctp_nets *net,
567 struct sctp_nets *alt,
573 * Mark all chunks (well not all) that were sent to *net for
574 * retransmission. Move them to alt for there destination as well...
575 * We only mark chunks that have been outstanding long enough to
576 * have received feed-back.
578 struct sctp_tmit_chunk *chk, *tp2, *could_be_sent = NULL;
579 struct sctp_nets *lnets;
580 struct timeval now, min_wait, tv;
582 int audit_tf, num_mk, fir;
584 uint32_t orig_flight, orig_tf;
585 uint32_t tsnlast, tsnfirst;
588 /* none in flight now */
592 * figure out how long a data chunk must be pending before we can
595 (void)SCTP_GETTIME_TIMEVAL(&now);
596 /* get cur rto in micro-seconds */
597 cur_rtt = (((net->lastsa >> 2) + net->lastsv) >> 1);
599 if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
601 stcb->asoc.peers_rwnd,
603 SCTP_FR_T3_MARK_TIME);
604 sctp_log_fr(net->flight_size,
605 SCTP_OS_TIMER_PENDING(&net->fr_timer.timer),
606 SCTP_OS_TIMER_ACTIVE(&net->fr_timer.timer),
607 SCTP_FR_CWND_REPORT);
608 sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
610 tv.tv_sec = cur_rtt / 1000000;
611 tv.tv_usec = cur_rtt % 1000000;
613 timevalsub(&min_wait, &tv);
614 if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
616 * if we hit here, we don't have enough seconds on the clock
617 * to account for the RTO. We just let the lower seconds be
618 * the bounds and don't worry about it. This may mean we
619 * will mark a lot more than we should.
621 min_wait.tv_sec = min_wait.tv_usec = 0;
623 if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
624 sctp_log_fr(cur_rtt, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
625 sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
628 * Our rwnd will be incorrect here since we are not adding back the
629 * cnt * mbuf but we will fix that down below.
631 orig_flight = net->flight_size;
632 orig_tf = stcb->asoc.total_flight;
634 net->fast_retran_ip = 0;
635 /* Now on to each chunk */
637 tsnfirst = tsnlast = 0;
638 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
639 for (; chk != NULL; chk = tp2) {
640 tp2 = TAILQ_NEXT(chk, sctp_next);
641 if ((compare_with_wrap(stcb->asoc.last_acked_seq,
642 chk->rec.data.TSN_seq,
644 (stcb->asoc.last_acked_seq == chk->rec.data.TSN_seq)) {
645 /* Strange case our list got out of order? */
646 SCTP_PRINTF("Our list is out of order?\n");
647 panic("Out of order list");
649 if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
651 * found one to mark: If it is less than
652 * DATAGRAM_ACKED it MUST not be a skipped or marked
653 * TSN but instead one that is either already set
654 * for retransmission OR one that needs
658 /* validate its been outstanding long enough */
659 if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
660 sctp_log_fr(chk->rec.data.TSN_seq,
661 chk->sent_rcv_time.tv_sec,
662 chk->sent_rcv_time.tv_usec,
663 SCTP_FR_T3_MARK_TIME);
665 if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
667 * we have reached a chunk that was sent
668 * some seconds past our min.. forget it we
669 * will find no more to send.
671 if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
673 chk->sent_rcv_time.tv_sec,
674 chk->sent_rcv_time.tv_usec,
678 } else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
679 (window_probe == 0)) {
681 * we must look at the micro seconds to
684 if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
686 * ok it was sent after our boundary
689 if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
691 chk->sent_rcv_time.tv_sec,
692 chk->sent_rcv_time.tv_usec,
698 if (PR_SCTP_TTL_ENABLED(chk->flags)) {
700 if ((now.tv_sec > chk->rec.data.timetodrop.tv_sec) ||
701 ((chk->rec.data.timetodrop.tv_sec == now.tv_sec) &&
702 (now.tv_usec > chk->rec.data.timetodrop.tv_usec))) {
705 (void)sctp_release_pr_sctp_chunk(stcb,
707 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
708 &stcb->asoc.sent_queue, SCTP_SO_NOT_LOCKED);
713 if (PR_SCTP_RTX_ENABLED(chk->flags)) {
714 /* Has it been retransmitted tv_sec times? */
715 if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
717 (void)sctp_release_pr_sctp_chunk(stcb,
719 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
720 &stcb->asoc.sent_queue, SCTP_SO_NOT_LOCKED);
725 if (chk->sent < SCTP_DATAGRAM_RESEND) {
726 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
730 tsnfirst = chk->rec.data.TSN_seq;
732 tsnlast = chk->rec.data.TSN_seq;
733 if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
734 sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
735 0, SCTP_FR_T3_MARKED);
737 if (chk->rec.data.chunk_was_revoked) {
738 /* deflate the cwnd */
739 chk->whoTo->cwnd -= chk->book_size;
740 chk->rec.data.chunk_was_revoked = 0;
742 net->marked_retrans++;
743 stcb->asoc.marked_retrans++;
744 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
745 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
746 chk->whoTo->flight_size,
748 (uintptr_t) chk->whoTo,
749 chk->rec.data.TSN_seq);
751 sctp_flight_size_decrease(chk);
752 sctp_total_flight_decrease(stcb, chk);
753 stcb->asoc.peers_rwnd += chk->send_size;
754 stcb->asoc.peers_rwnd += sctp_peer_chunk_oh;
756 chk->sent = SCTP_DATAGRAM_RESEND;
757 SCTP_STAT_INCR(sctps_markedretrans);
759 /* reset the TSN for striking and other FR stuff */
760 chk->rec.data.doing_fast_retransmit = 0;
761 /* Clear any time so NO RTT is being done */
764 sctp_free_remote_addr(chk->whoTo);
765 chk->no_fr_allowed = 1;
767 atomic_add_int(&alt->ref_count, 1);
769 chk->no_fr_allowed = 0;
770 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
771 chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
773 chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
777 * CMT: Do not allow FRs on retransmitted TSNs.
779 if (sctp_cmt_on_off == 1) {
780 chk->no_fr_allowed = 1;
782 } else if (chk->sent == SCTP_DATAGRAM_ACKED) {
783 /* remember highest acked one */
786 if (chk->sent == SCTP_DATAGRAM_RESEND) {
790 if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
791 /* we did not subtract the same things? */
794 if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
795 sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
799 SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
801 SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%ld\n",
802 num_mk, (u_long)stcb->asoc.peers_rwnd);
803 SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
805 SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%d\n",
807 (int)stcb->asoc.peers_rwnd);
810 *num_marked = num_mk;
811 if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
812 /* fix it so we retransmit the highest acked anyway */
813 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
815 could_be_sent->sent = SCTP_DATAGRAM_RESEND;
817 if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
819 SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
820 cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
822 #ifndef SCTP_AUDITING_ENABLED
823 stcb->asoc.sent_queue_retran_cnt = cnt_mk;
826 /* Now check for a ECN Echo that may be stranded */
827 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
828 if ((chk->whoTo == net) &&
829 (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
830 sctp_free_remote_addr(chk->whoTo);
832 if (chk->sent != SCTP_DATAGRAM_RESEND) {
833 chk->sent = SCTP_DATAGRAM_RESEND;
834 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
836 atomic_add_int(&alt->ref_count, 1);
840 SCTPDBG(SCTP_DEBUG_TIMER4,
841 "Audit total flight due to negative value net:%p\n",
843 stcb->asoc.total_flight = 0;
844 stcb->asoc.total_flight_count = 0;
845 /* Clear all networks flight size */
846 TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
847 lnets->flight_size = 0;
848 SCTPDBG(SCTP_DEBUG_TIMER4,
849 "Net:%p c-f cwnd:%d ssthresh:%d\n",
850 lnets, lnets->cwnd, lnets->ssthresh);
852 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
853 if (chk->sent < SCTP_DATAGRAM_RESEND) {
854 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
855 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
856 chk->whoTo->flight_size,
858 (uintptr_t) chk->whoTo,
859 chk->rec.data.TSN_seq);
861 sctp_flight_size_increase(chk);
862 sctp_total_flight_increase(stcb, chk);
867 * Setup the ecn nonce re-sync point. We do this since
868 * retranmissions are NOT setup for ECN. This means that do to
869 * Karn's rule, we don't know the total of the peers ecn bits.
871 chk = TAILQ_FIRST(&stcb->asoc.send_queue);
873 stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
875 stcb->asoc.nonce_resync_tsn = chk->rec.data.TSN_seq;
877 stcb->asoc.nonce_wait_for_ecne = 0;
878 stcb->asoc.nonce_sum_check = 0;
879 /* We return 1 if we only have a window probe outstanding */
884 sctp_move_all_chunks_to_alt(struct sctp_tcb *stcb,
885 struct sctp_nets *net,
886 struct sctp_nets *alt)
888 struct sctp_association *asoc;
889 struct sctp_stream_out *outs;
890 struct sctp_tmit_chunk *chk;
891 struct sctp_stream_queue_pending *sp;
900 * now through all the streams checking for chunks sent to our bad
903 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
904 /* now clean up any chunks here */
905 TAILQ_FOREACH(sp, &outs->outqueue, next) {
906 if (sp->net == net) {
907 sctp_free_remote_addr(sp->net);
909 atomic_add_int(&alt->ref_count, 1);
913 /* Now check the pending queue */
914 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
915 if (chk->whoTo == net) {
916 sctp_free_remote_addr(chk->whoTo);
918 atomic_add_int(&alt->ref_count, 1);
925 sctp_t3rxt_timer(struct sctp_inpcb *inp,
926 struct sctp_tcb *stcb,
927 struct sctp_nets *net)
929 struct sctp_nets *alt;
930 int win_probe, num_mk;
932 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
933 sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
935 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
936 struct sctp_nets *lnet;
938 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
940 sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
942 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
946 /* Find an alternate and mark those for retransmission */
947 if ((stcb->asoc.peers_rwnd == 0) &&
948 (stcb->asoc.total_flight < net->mtu)) {
949 SCTP_STAT_INCR(sctps_timowindowprobe);
956 * JRS 5/14/07 - If CMT PF is on and the destination if not already
957 * in PF state, set the destination to PF state and store the
958 * current time as the time that the destination was last active. In
959 * addition, find an alternate destination with PF-based
962 if (sctp_cmt_on_off && sctp_cmt_pf) {
963 if ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF) {
964 net->dest_state |= SCTP_ADDR_PF;
965 net->last_active = sctp_get_tick_count();
966 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from active to PF.\n",
969 alt = sctp_find_alternate_net(stcb, net, 2);
970 } else if (sctp_cmt_on_off) {
972 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is being
973 * used, then pick dest with largest ssthresh for any
977 alt = sctp_find_alternate_net(stcb, alt, 1);
979 * CUCv2: If a different dest is picked for the
980 * retransmission, then new (rtx-)pseudo_cumack needs to be
981 * tracked for orig dest. Let CUCv2 track new (rtx-)
982 * pseudo-cumack always.
984 net->find_pseudo_cumack = 1;
985 net->find_rtx_pseudo_cumack = 1;
986 } else { /* CMT is OFF */
987 alt = sctp_find_alternate_net(stcb, net, 0);
990 (void)sctp_mark_all_for_resend(stcb, net, alt, win_probe, &num_mk);
991 /* FR Loss recovery just ended with the T3. */
992 stcb->asoc.fast_retran_loss_recovery = 0;
994 /* CMT FR loss recovery ended with the T3 */
995 net->fast_retran_loss_recovery = 0;
998 * setup the sat loss recovery that prevents satellite cwnd advance.
1000 stcb->asoc.sat_t3_loss_recovery = 1;
1001 stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
1003 /* Backoff the timer and cwnd */
1004 sctp_backoff_on_timeout(stcb, net, win_probe, num_mk);
1005 if (win_probe == 0) {
1006 /* We don't do normal threshold management on window probes */
1007 if (sctp_threshold_management(inp, stcb, net,
1008 stcb->asoc.max_send_times)) {
1009 /* Association was destroyed */
1012 if (net != stcb->asoc.primary_destination) {
1013 /* send a immediate HB if our RTO is stale */
1015 unsigned int ms_goneby;
1017 (void)SCTP_GETTIME_TIMEVAL(&now);
1018 if (net->last_sent_time.tv_sec) {
1019 ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
1023 if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
1025 * no recent feed back in an RTO or
1026 * more, request a RTT update
1028 if (sctp_send_hb(stcb, 1, net) < 0)
1035 * For a window probe we don't penalize the net's but only
1036 * the association. This may fail it if SACKs are not coming
1037 * back. If sack's are coming with rwnd locked at 0, we will
1038 * continue to hold things waiting for rwnd to raise
1040 if (sctp_threshold_management(inp, stcb, NULL,
1041 stcb->asoc.max_send_times)) {
1042 /* Association was destroyed */
1046 if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1047 /* Move all pending over too */
1048 sctp_move_all_chunks_to_alt(stcb, net, alt);
1051 * Get the address that failed, to force a new src address
1052 * selecton and a route allocation.
1054 if (net->ro._s_addr) {
1055 sctp_free_ifa(net->ro._s_addr);
1056 net->ro._s_addr = NULL;
1058 net->src_addr_selected = 0;
1060 /* Force a route allocation too */
1061 if (net->ro.ro_rt) {
1062 RTFREE(net->ro.ro_rt);
1063 net->ro.ro_rt = NULL;
1065 /* Was it our primary? */
1066 if ((stcb->asoc.primary_destination == net) && (alt != net)) {
1068 * Yes, note it as such and find an alternate note:
1069 * this means HB code must use this to resent the
1070 * primary if it goes active AND if someone does a
1071 * change-primary then this flag must be cleared
1072 * from any net structures.
1074 if (sctp_set_primary_addr(stcb,
1075 (struct sockaddr *)NULL,
1077 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
1080 } else if (sctp_cmt_on_off && sctp_cmt_pf && (net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) {
1082 * JRS 5/14/07 - If the destination hasn't failed completely
1083 * but is in PF state, a PF-heartbeat needs to be sent
1086 if (sctp_send_hb(stcb, 1, net) < 0)
1090 * Special case for cookie-echo'ed case, we don't do output but must
1091 * await the COOKIE-ACK before retransmission
1093 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1095 * Here we just reset the timer and start again since we
1096 * have not established the asoc
1098 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
1101 if (stcb->asoc.peer_supports_prsctp) {
1102 struct sctp_tmit_chunk *lchk;
1104 lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
1105 /* C3. See if we need to send a Fwd-TSN */
1106 if (compare_with_wrap(stcb->asoc.advanced_peer_ack_point,
1107 stcb->asoc.last_acked_seq, MAX_TSN)) {
1109 * ISSUE with ECN, see FWD-TSN processing for notes
1110 * on issues that will occur when the ECN NONCE
1111 * stuff is put into SCTP for cross checking.
1113 send_forward_tsn(stcb, &stcb->asoc);
1115 /* Assure a timer is up */
1116 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
1120 if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
1121 sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
1127 sctp_t1init_timer(struct sctp_inpcb *inp,
1128 struct sctp_tcb *stcb,
1129 struct sctp_nets *net)
1131 /* bump the thresholds */
1132 if (stcb->asoc.delayed_connection) {
1134 * special hook for delayed connection. The library did NOT
1135 * complete the rest of its sends.
1137 stcb->asoc.delayed_connection = 0;
1138 sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1141 if (SCTP_GET_STATE((&stcb->asoc)) != SCTP_STATE_COOKIE_WAIT) {
1144 if (sctp_threshold_management(inp, stcb, net,
1145 stcb->asoc.max_init_times)) {
1146 /* Association was destroyed */
1149 stcb->asoc.dropped_special_cnt = 0;
1150 sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0);
1151 if (stcb->asoc.initial_init_rto_max < net->RTO) {
1152 net->RTO = stcb->asoc.initial_init_rto_max;
1154 if (stcb->asoc.numnets > 1) {
1155 /* If we have more than one addr use it */
1156 struct sctp_nets *alt;
1158 alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1159 if ((alt != NULL) && (alt != stcb->asoc.primary_destination)) {
1160 sctp_move_all_chunks_to_alt(stcb, stcb->asoc.primary_destination, alt);
1161 stcb->asoc.primary_destination = alt;
1164 /* Send out a new init */
1165 sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1170 * For cookie and asconf we actually need to find and mark for resend, then
1171 * increment the resend counter (after all the threshold management stuff of
1175 sctp_cookie_timer(struct sctp_inpcb *inp,
1176 struct sctp_tcb *stcb,
1177 struct sctp_nets *net)
1179 struct sctp_nets *alt;
1180 struct sctp_tmit_chunk *cookie;
1182 /* first before all else we must find the cookie */
1183 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1184 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1188 if (cookie == NULL) {
1189 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1193 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1194 0, M_DONTWAIT, 1, MT_DATA);
1196 struct sctp_paramhdr *ph;
1199 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1201 ph = mtod(oper, struct sctp_paramhdr *);
1202 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1203 ph->param_length = htons(SCTP_BUF_LEN(oper));
1204 ippp = (uint32_t *) (ph + 1);
1205 *ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
1207 inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_4;
1208 sctp_abort_an_association(inp, stcb, SCTP_INTERNAL_ERROR,
1209 oper, SCTP_SO_NOT_LOCKED);
1212 panic("Cookie timer expires in wrong state?");
1214 SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(&stcb->asoc));
1220 /* Ok we found the cookie, threshold management next */
1221 if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1222 stcb->asoc.max_init_times)) {
1227 * cleared theshold management now lets backoff the address & select
1230 stcb->asoc.dropped_special_cnt = 0;
1231 sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0);
1232 alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1233 if (alt != cookie->whoTo) {
1234 sctp_free_remote_addr(cookie->whoTo);
1235 cookie->whoTo = alt;
1236 atomic_add_int(&alt->ref_count, 1);
1238 /* Now mark the retran info */
1239 if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1240 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1242 cookie->sent = SCTP_DATAGRAM_RESEND;
1244 * Now call the output routine to kick out the cookie again, Note we
1245 * don't mark any chunks for retran so that FR will need to kick in
1246 * to move these (or a send timer).
1252 sctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1253 struct sctp_nets *net)
1255 struct sctp_nets *alt;
1256 struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1258 if (stcb->asoc.stream_reset_outstanding == 0) {
1261 /* find the existing STRRESET, we use the seq number we sent out on */
1262 (void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1263 if (strrst == NULL) {
1266 /* do threshold management */
1267 if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1268 stcb->asoc.max_send_times)) {
1273 * cleared theshold management now lets backoff the address & select
1276 sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0);
1277 alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
1278 sctp_free_remote_addr(strrst->whoTo);
1279 strrst->whoTo = alt;
1280 atomic_add_int(&alt->ref_count, 1);
1282 /* See if a ECN Echo is also stranded */
1283 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1284 if ((chk->whoTo == net) &&
1285 (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1286 sctp_free_remote_addr(chk->whoTo);
1287 if (chk->sent != SCTP_DATAGRAM_RESEND) {
1288 chk->sent = SCTP_DATAGRAM_RESEND;
1289 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1292 atomic_add_int(&alt->ref_count, 1);
1295 if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1297 * If the address went un-reachable, we need to move to
1298 * alternates for ALL chk's in queue
1300 sctp_move_all_chunks_to_alt(stcb, net, alt);
1302 /* mark the retran info */
1303 if (strrst->sent != SCTP_DATAGRAM_RESEND)
1304 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1305 strrst->sent = SCTP_DATAGRAM_RESEND;
1307 /* restart the timer */
1308 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1313 sctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1314 struct sctp_nets *net)
1316 struct sctp_nets *alt;
1317 struct sctp_tmit_chunk *asconf, *chk;
1319 /* is this a first send, or a retransmission? */
1320 if (stcb->asoc.asconf_sent == 0) {
1321 /* compose a new ASCONF chunk and send it */
1322 sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1325 * Retransmission of the existing ASCONF is needed
1328 /* find the existing ASCONF */
1329 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
1331 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
1335 if (asconf == NULL) {
1338 /* do threshold management */
1339 if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1340 stcb->asoc.max_send_times)) {
1344 if (asconf->snd_count > stcb->asoc.max_send_times) {
1346 * Something is rotten: our peer is not responding
1347 * to ASCONFs but apparently is to other chunks.
1348 * i.e. it is not properly handling the chunk type
1349 * upper bits. Mark this peer as ASCONF incapable
1352 SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1353 sctp_asconf_cleanup(stcb, net);
1357 * cleared threshold management, so now backoff the net and
1358 * select an alternate
1360 sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0);
1361 alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
1362 sctp_free_remote_addr(asconf->whoTo);
1363 asconf->whoTo = alt;
1364 atomic_add_int(&alt->ref_count, 1);
1366 /* See if an ECN Echo is also stranded */
1367 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1368 if ((chk->whoTo == net) &&
1369 (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1370 sctp_free_remote_addr(chk->whoTo);
1372 if (chk->sent != SCTP_DATAGRAM_RESEND) {
1373 chk->sent = SCTP_DATAGRAM_RESEND;
1374 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1376 atomic_add_int(&alt->ref_count, 1);
1379 if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1381 * If the address went un-reachable, we need to move
1382 * to the alternate for ALL chunks in queue
1384 sctp_move_all_chunks_to_alt(stcb, net, alt);
1386 /* mark the retran info */
1387 if (asconf->sent != SCTP_DATAGRAM_RESEND)
1388 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1389 asconf->sent = SCTP_DATAGRAM_RESEND;
1394 /* Mobility adaptation */
1396 sctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1397 struct sctp_nets *net)
1399 if (stcb->asoc.deleted_primary == NULL) {
1400 SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n");
1401 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1404 SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary ");
1405 SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1406 sctp_free_remote_addr(stcb->asoc.deleted_primary);
1407 stcb->asoc.deleted_primary = NULL;
1408 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1413 * For the shutdown and shutdown-ack, we do not keep one around on the
1414 * control queue. This means we must generate a new one and call the general
1415 * chunk output routine, AFTER having done threshold management.
1418 sctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1419 struct sctp_nets *net)
1421 struct sctp_nets *alt;
1423 /* first threshold managment */
1424 if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1428 /* second select an alternative */
1429 alt = sctp_find_alternate_net(stcb, net, 0);
1431 /* third generate a shutdown into the queue for out net */
1433 sctp_send_shutdown(stcb, alt);
1436 * if alt is NULL, there is no dest to send to??
1440 /* fourth restart timer */
1441 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1446 sctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1447 struct sctp_nets *net)
1449 struct sctp_nets *alt;
1451 /* first threshold managment */
1452 if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1456 /* second select an alternative */
1457 alt = sctp_find_alternate_net(stcb, net, 0);
1459 /* third generate a shutdown into the queue for out net */
1460 sctp_send_shutdown_ack(stcb, alt);
1462 /* fourth restart timer */
1463 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1468 sctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1469 struct sctp_tcb *stcb)
1471 struct sctp_stream_out *outs;
1472 struct sctp_stream_queue_pending *sp;
1473 unsigned int chks_in_queue = 0;
1474 int being_filled = 0;
1477 * This function is ONLY called when the send/sent queues are empty.
1479 if ((stcb == NULL) || (inp == NULL))
1482 if (stcb->asoc.sent_queue_retran_cnt) {
1483 SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1484 stcb->asoc.sent_queue_retran_cnt);
1485 stcb->asoc.sent_queue_retran_cnt = 0;
1487 SCTP_TCB_SEND_LOCK(stcb);
1488 if (TAILQ_EMPTY(&stcb->asoc.out_wheel)) {
1491 /* Check to see if a spoke fell off the wheel */
1492 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1493 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1494 sctp_insert_on_wheel(stcb, &stcb->asoc, &stcb->asoc.strmout[i], 1);
1499 /* yep, we lost a spoke or two */
1500 SCTP_PRINTF("Found an additional %d streams NOT on outwheel, corrected\n", cnt);
1502 /* no spokes lost, */
1503 stcb->asoc.total_output_queue_size = 0;
1505 SCTP_TCB_SEND_UNLOCK(stcb);
1508 SCTP_TCB_SEND_UNLOCK(stcb);
1509 /* Check to see if some data queued, if so report it */
1510 TAILQ_FOREACH(outs, &stcb->asoc.out_wheel, next_spoke) {
1511 if (!TAILQ_EMPTY(&outs->outqueue)) {
1512 TAILQ_FOREACH(sp, &outs->outqueue, next) {
1513 if (sp->msg_is_complete)
1519 if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1520 SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1521 stcb->asoc.stream_queue_cnt, chks_in_queue);
1523 if (chks_in_queue) {
1524 /* call the output queue function */
1525 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1526 if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1527 (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1529 * Probably should go in and make it go back through
1530 * and add fragments allowed
1532 if (being_filled == 0) {
1533 SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
1538 SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
1539 (u_long)stcb->asoc.total_output_queue_size);
1540 stcb->asoc.total_output_queue_size = 0;
1545 sctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1546 struct sctp_nets *net, int cnt_of_unconf)
1551 if (net->hb_responded == 0) {
1552 if (net->ro._s_addr) {
1554 * Invalidate the src address if we did not
1555 * get a response last time.
1557 sctp_free_ifa(net->ro._s_addr);
1558 net->ro._s_addr = NULL;
1559 net->src_addr_selected = 0;
1561 sctp_backoff_on_timeout(stcb, net, 1, 0);
1563 /* Zero PBA, if it needs it */
1564 if (net->partial_bytes_acked) {
1565 net->partial_bytes_acked = 0;
1568 if ((stcb->asoc.total_output_queue_size > 0) &&
1569 (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1570 (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1571 sctp_audit_stream_queues_for_size(inp, stcb);
1573 /* Send a new HB, this will do threshold managment, pick a new dest */
1574 if (cnt_of_unconf == 0) {
1575 if (sctp_send_hb(stcb, 0, NULL) < 0) {
1580 * this will send out extra hb's up to maxburst if there are
1581 * any unconfirmed addresses.
1583 uint32_t cnt_sent = 0;
1585 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1586 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1587 (net->dest_state & SCTP_ADDR_REACHABLE)) {
1589 if (net->hb_responded == 0) {
1590 /* Did we respond last time? */
1591 if (net->ro._s_addr) {
1592 sctp_free_ifa(net->ro._s_addr);
1593 net->ro._s_addr = NULL;
1594 net->src_addr_selected = 0;
1597 ret = sctp_send_hb(stcb, 1, net);
1600 else if (ret == 0) {
1603 if (cnt_sent >= sctp_hb_maxburst)
1612 sctp_is_hb_timer_running(struct sctp_tcb *stcb)
1614 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.hb_timer.timer)) {
1624 sctp_is_sack_timer_running(struct sctp_tcb *stcb)
1626 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
1635 #define SCTP_NUMBER_OF_MTU_SIZES 18
1636 static uint32_t mtu_sizes[] = {
1659 sctp_getnext_mtu(struct sctp_inpcb *inp, uint32_t cur_mtu)
1661 /* select another MTU that is just bigger than this one */
1664 for (i = 0; i < SCTP_NUMBER_OF_MTU_SIZES; i++) {
1665 if (cur_mtu < mtu_sizes[i]) {
1666 /* no max_mtu is bigger than this one */
1667 return (mtu_sizes[i]);
1670 /* here return the highest allowable */
1676 sctp_pathmtu_timer(struct sctp_inpcb *inp,
1677 struct sctp_tcb *stcb,
1678 struct sctp_nets *net)
1682 /* restart the timer in any case */
1683 next_mtu = sctp_getnext_mtu(inp, net->mtu);
1684 if (next_mtu <= net->mtu) {
1690 if ((net->src_addr_selected == 0) ||
1691 (net->ro._s_addr == NULL) ||
1692 (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1693 if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1694 sctp_free_ifa(net->ro._s_addr);
1695 net->ro._s_addr = NULL;
1696 net->src_addr_selected = 0;
1697 } else if (net->ro._s_addr == NULL) {
1698 net->ro._s_addr = sctp_source_address_selection(inp,
1700 (sctp_route_t *) & net->ro,
1701 net, 0, stcb->asoc.vrf_id);
1703 if (net->ro._s_addr)
1704 net->src_addr_selected = 1;
1706 if (net->ro._s_addr) {
1707 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
1708 if (mtu > next_mtu) {
1709 net->mtu = next_mtu;
1713 /* restart the timer */
1714 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1718 sctp_autoclose_timer(struct sctp_inpcb *inp,
1719 struct sctp_tcb *stcb,
1720 struct sctp_nets *net)
1722 struct timeval tn, *tim_touse;
1723 struct sctp_association *asoc;
1726 (void)SCTP_GETTIME_TIMEVAL(&tn);
1727 if (stcb->asoc.sctp_autoclose_ticks &&
1728 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1729 /* Auto close is on */
1731 /* pick the time to use */
1732 if (asoc->time_last_rcvd.tv_sec >
1733 asoc->time_last_sent.tv_sec) {
1734 tim_touse = &asoc->time_last_rcvd;
1736 tim_touse = &asoc->time_last_sent;
1738 /* Now has long enough transpired to autoclose? */
1739 ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
1740 if ((ticks_gone_by > 0) &&
1741 (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1743 * autoclose time has hit, call the output routine,
1744 * which should do nothing just to be SURE we don't
1745 * have hanging data. We can then safely check the
1746 * queues and know that we are clear to send
1749 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1751 if (TAILQ_EMPTY(&asoc->send_queue) &&
1752 TAILQ_EMPTY(&asoc->sent_queue)) {
1754 * there is nothing queued to send, so I'm
1757 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1758 /* only send SHUTDOWN 1st time thru */
1759 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
1760 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1761 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1762 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1764 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1765 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1766 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1767 stcb->sctp_ep, stcb,
1768 asoc->primary_destination);
1769 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1770 stcb->sctp_ep, stcb,
1771 asoc->primary_destination);
1776 * No auto close at this time, reset t-o to check
1781 /* fool the timer startup to use the time left */
1782 tmp = asoc->sctp_autoclose_ticks;
1783 asoc->sctp_autoclose_ticks -= ticks_gone_by;
1784 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1786 /* restore the real tick value */
1787 asoc->sctp_autoclose_ticks = tmp;
1793 sctp_iterator_timer(struct sctp_iterator *it)
1795 int iteration_count = 0;
1799 * only one iterator can run at a time. This is the only way we can
1800 * cleanly pull ep's from underneath all the running interators when
1803 SCTP_ITERATOR_LOCK();
1804 if (it->inp == NULL) {
1805 /* iterator is complete */
1807 SCTP_ITERATOR_UNLOCK();
1808 SCTP_INP_INFO_WLOCK();
1809 TAILQ_REMOVE(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
1810 /* stopping the callout is not needed, in theory */
1811 SCTP_INP_INFO_WUNLOCK();
1812 (void)SCTP_OS_TIMER_STOP(&it->tmr.timer);
1813 if (it->function_atend != NULL) {
1814 (*it->function_atend) (it->pointer, it->val);
1816 SCTP_FREE(it, SCTP_M_ITER);
1820 SCTP_INP_WLOCK(it->inp);
1821 while (((it->pcb_flags) &&
1822 ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1823 ((it->pcb_features) &&
1824 ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1825 /* endpoint flags or features don't match, so keep looking */
1826 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1827 SCTP_INP_WUNLOCK(it->inp);
1828 goto done_with_iterator;
1830 SCTP_INP_WUNLOCK(it->inp);
1831 it->inp = LIST_NEXT(it->inp, sctp_list);
1832 if (it->inp == NULL) {
1833 goto done_with_iterator;
1835 SCTP_INP_WLOCK(it->inp);
1837 if ((it->inp->inp_starting_point_for_iterator != NULL) &&
1838 (it->inp->inp_starting_point_for_iterator != it)) {
1839 SCTP_PRINTF("Iterator collision, waiting for one at %p\n",
1841 SCTP_INP_WUNLOCK(it->inp);
1842 goto start_timer_return;
1844 /* mark the current iterator on the endpoint */
1845 it->inp->inp_starting_point_for_iterator = it;
1846 SCTP_INP_WUNLOCK(it->inp);
1847 SCTP_INP_RLOCK(it->inp);
1848 /* now go through each assoc which is in the desired state */
1849 if (it->done_current_ep == 0) {
1850 if (it->function_inp != NULL)
1851 inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1852 it->done_current_ep = 1;
1854 if (it->stcb == NULL) {
1855 /* run the per instance function */
1856 it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1858 SCTP_INP_RUNLOCK(it->inp);
1859 if ((inp_skip) || it->stcb == NULL) {
1860 if (it->function_inp_end != NULL) {
1861 inp_skip = (*it->function_inp_end) (it->inp,
1868 (it->stcb->asoc.stcb_starting_point_for_iterator == it)) {
1869 it->stcb->asoc.stcb_starting_point_for_iterator = NULL;
1872 SCTP_TCB_LOCK(it->stcb);
1873 if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1874 /* not in the right state... keep looking */
1875 SCTP_TCB_UNLOCK(it->stcb);
1878 /* mark the current iterator on the assoc */
1879 it->stcb->asoc.stcb_starting_point_for_iterator = it;
1880 /* see if we have limited out the iterator loop */
1882 if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1884 /* set a timer to continue this later */
1886 SCTP_TCB_UNLOCK(it->stcb);
1887 sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR,
1888 (struct sctp_inpcb *)it, NULL, NULL);
1889 SCTP_ITERATOR_UNLOCK();
1892 /* run function on this one */
1893 (*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1896 * we lie here, it really needs to have its own type but
1897 * first I must verify that this won't effect things :-0
1899 if (it->no_chunk_output == 0)
1900 sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1902 SCTP_TCB_UNLOCK(it->stcb);
1904 it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1905 if (it->stcb == NULL) {
1906 if (it->function_inp_end != NULL) {
1907 inp_skip = (*it->function_inp_end) (it->inp,
1914 /* done with all assocs on this endpoint, move on to next endpoint */
1915 it->done_current_ep = 0;
1916 SCTP_INP_WLOCK(it->inp);
1917 it->inp->inp_starting_point_for_iterator = NULL;
1918 SCTP_INP_WUNLOCK(it->inp);
1919 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1922 SCTP_INP_INFO_RLOCK();
1923 it->inp = LIST_NEXT(it->inp, sctp_list);
1924 SCTP_INP_INFO_RUNLOCK();
1926 if (it->inp == NULL) {
1927 goto done_with_iterator;
1929 goto select_a_new_ep;