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