sctp_timer.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
  5. * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
  6. * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * a) Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * b) Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the distribution.
  17. *
  18. * c) Neither the name of Cisco Systems, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #if defined(__FreeBSD__) && !defined(__Userspace__)
  35. #include <sys/cdefs.h>
  36. __FBSDID("$FreeBSD$");
  37. #endif
  38. #define _IP_VHL
  39. #include <netinet/sctp_os.h>
  40. #include <netinet/sctp_pcb.h>
  41. #ifdef INET6
  42. #if defined(__FreeBSD__) && defined(__Userspace__)
  43. #include <netinet6/sctp6_var.h>
  44. #endif
  45. #endif
  46. #include <netinet/sctp_var.h>
  47. #include <netinet/sctp_sysctl.h>
  48. #include <netinet/sctp_timer.h>
  49. #include <netinet/sctputil.h>
  50. #include <netinet/sctp_output.h>
  51. #include <netinet/sctp_header.h>
  52. #include <netinet/sctp_indata.h>
  53. #include <netinet/sctp_asconf.h>
  54. #include <netinet/sctp_input.h>
  55. #include <netinet/sctp.h>
  56. #include <netinet/sctp_uio.h>
  57. #if defined(INET) || defined(INET6)
  58. #if !(defined(_WIN32) && defined(__Userspace__))
  59. #include <netinet/udp.h>
  60. #endif
  61. #endif
  62. void
  63. sctp_audit_retranmission_queue(struct sctp_association *asoc)
  64. {
  65. struct sctp_tmit_chunk *chk;
  66. SCTPDBG(SCTP_DEBUG_TIMER4, "Audit invoked on send queue cnt:%d onqueue:%d\n",
  67. asoc->sent_queue_retran_cnt,
  68. asoc->sent_queue_cnt);
  69. asoc->sent_queue_retran_cnt = 0;
  70. asoc->sent_queue_cnt = 0;
  71. TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
  72. if (chk->sent == SCTP_DATAGRAM_RESEND) {
  73. sctp_ucount_incr(asoc->sent_queue_retran_cnt);
  74. }
  75. asoc->sent_queue_cnt++;
  76. }
  77. TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
  78. if (chk->sent == SCTP_DATAGRAM_RESEND) {
  79. sctp_ucount_incr(asoc->sent_queue_retran_cnt);
  80. }
  81. }
  82. TAILQ_FOREACH(chk, &asoc->asconf_send_queue, sctp_next) {
  83. if (chk->sent == SCTP_DATAGRAM_RESEND) {
  84. sctp_ucount_incr(asoc->sent_queue_retran_cnt);
  85. }
  86. }
  87. SCTPDBG(SCTP_DEBUG_TIMER4, "Audit completes retran:%d onqueue:%d\n",
  88. asoc->sent_queue_retran_cnt,
  89. asoc->sent_queue_cnt);
  90. }
  91. static int
  92. sctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  93. struct sctp_nets *net, uint16_t threshold)
  94. {
  95. if (net) {
  96. net->error_count++;
  97. SCTPDBG(SCTP_DEBUG_TIMER4, "Error count for %p now %d thresh:%d\n",
  98. (void *)net, net->error_count,
  99. net->failure_threshold);
  100. if (net->error_count > net->failure_threshold) {
  101. /* We had a threshold failure */
  102. if (net->dest_state & SCTP_ADDR_REACHABLE) {
  103. net->dest_state &= ~SCTP_ADDR_REACHABLE;
  104. net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
  105. net->dest_state &= ~SCTP_ADDR_PF;
  106. sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
  107. stcb, 0,
  108. (void *)net, SCTP_SO_NOT_LOCKED);
  109. }
  110. } else if ((net->pf_threshold < net->failure_threshold) &&
  111. (net->error_count > net->pf_threshold)) {
  112. if ((net->dest_state & SCTP_ADDR_PF) == 0) {
  113. net->dest_state |= SCTP_ADDR_PF;
  114. net->last_active = sctp_get_tick_count();
  115. sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
  116. sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
  117. inp, stcb, net,
  118. SCTP_FROM_SCTP_TIMER + SCTP_LOC_1);
  119. sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
  120. }
  121. }
  122. }
  123. if (stcb == NULL)
  124. return (0);
  125. if (net) {
  126. if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
  127. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
  128. sctp_misc_ints(SCTP_THRESHOLD_INCR,
  129. stcb->asoc.overall_error_count,
  130. (stcb->asoc.overall_error_count+1),
  131. SCTP_FROM_SCTP_TIMER,
  132. __LINE__);
  133. }
  134. stcb->asoc.overall_error_count++;
  135. }
  136. } else {
  137. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
  138. sctp_misc_ints(SCTP_THRESHOLD_INCR,
  139. stcb->asoc.overall_error_count,
  140. (stcb->asoc.overall_error_count+1),
  141. SCTP_FROM_SCTP_TIMER,
  142. __LINE__);
  143. }
  144. stcb->asoc.overall_error_count++;
  145. }
  146. SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n",
  147. (void *)&stcb->asoc, stcb->asoc.overall_error_count,
  148. (uint32_t)threshold,
  149. ((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
  150. /*
  151. * We specifically do not do >= to give the assoc one more change
  152. * before we fail it.
  153. */
  154. if (stcb->asoc.overall_error_count > threshold) {
  155. /* Abort notification sends a ULP notify */
  156. struct mbuf *op_err;
  157. op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
  158. "Association error counter exceeded");
  159. inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_2;
  160. sctp_abort_an_association(inp, stcb, op_err, true, SCTP_SO_NOT_LOCKED);
  161. return (1);
  162. }
  163. return (0);
  164. }
  165. /*
  166. * sctp_find_alternate_net() returns a non-NULL pointer as long as there
  167. * exists nets, which are not being deleted.
  168. */
  169. struct sctp_nets *
  170. sctp_find_alternate_net(struct sctp_tcb *stcb,
  171. struct sctp_nets *net,
  172. int mode)
  173. {
  174. /* Find and return an alternate network if possible */
  175. struct sctp_nets *alt, *mnet, *min_errors_net = NULL , *max_cwnd_net = NULL;
  176. bool looped;
  177. /* JRS 5/14/07 - Initialize min_errors to an impossible value. */
  178. int min_errors = -1;
  179. uint32_t max_cwnd = 0;
  180. if (stcb->asoc.numnets == 1) {
  181. /* No selection can be made. */
  182. return (TAILQ_FIRST(&stcb->asoc.nets));
  183. }
  184. /*
  185. * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate net algorithm.
  186. * This algorithm chooses the active destination (not in PF state) with the largest
  187. * cwnd value. If all destinations are in PF state, unreachable, or unconfirmed, choose
  188. * the destination that is in PF state with the lowest error count. In case of a tie,
  189. * choose the destination that was most recently active.
  190. */
  191. if (mode == 2) {
  192. TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
  193. /* JRS 5/14/07 - If the destination is unreachable or unconfirmed, skip it. */
  194. if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
  195. (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
  196. continue;
  197. }
  198. /*
  199. * JRS 5/14/07 - If the destination is reachable but in PF state, compare
  200. * the error count of the destination to the minimum error count seen thus far.
  201. * Store the destination with the lower error count. If the error counts are
  202. * equal, store the destination that was most recently active.
  203. */
  204. if (mnet->dest_state & SCTP_ADDR_PF) {
  205. /*
  206. * JRS 5/14/07 - If the destination under consideration is the current
  207. * destination, work as if the error count is one higher. The
  208. * actual error count will not be incremented until later in the
  209. * t3 handler.
  210. */
  211. if (mnet == net) {
  212. if (min_errors == -1) {
  213. min_errors = mnet->error_count + 1;
  214. min_errors_net = mnet;
  215. } else if (mnet->error_count + 1 < min_errors) {
  216. min_errors = mnet->error_count + 1;
  217. min_errors_net = mnet;
  218. } else if (mnet->error_count + 1 == min_errors
  219. && mnet->last_active > min_errors_net->last_active) {
  220. min_errors_net = mnet;
  221. min_errors = mnet->error_count + 1;
  222. }
  223. continue;
  224. } else {
  225. if (min_errors == -1) {
  226. min_errors = mnet->error_count;
  227. min_errors_net = mnet;
  228. } else if (mnet->error_count < min_errors) {
  229. min_errors = mnet->error_count;
  230. min_errors_net = mnet;
  231. } else if (mnet->error_count == min_errors
  232. && mnet->last_active > min_errors_net->last_active) {
  233. min_errors_net = mnet;
  234. min_errors = mnet->error_count;
  235. }
  236. continue;
  237. }
  238. }
  239. /*
  240. * JRS 5/14/07 - If the destination is reachable and not in PF state, compare the
  241. * cwnd of the destination to the highest cwnd seen thus far. Store the
  242. * destination with the higher cwnd value. If the cwnd values are equal,
  243. * randomly choose one of the two destinations.
  244. */
  245. if (max_cwnd < mnet->cwnd) {
  246. max_cwnd_net = mnet;
  247. max_cwnd = mnet->cwnd;
  248. } else if (max_cwnd == mnet->cwnd) {
  249. uint32_t rndval;
  250. uint8_t this_random;
  251. if (stcb->asoc.hb_random_idx > 3) {
  252. rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
  253. memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values));
  254. this_random = stcb->asoc.hb_random_values[0];
  255. stcb->asoc.hb_random_idx++;
  256. stcb->asoc.hb_ect_randombit = 0;
  257. } else {
  258. this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
  259. stcb->asoc.hb_random_idx++;
  260. stcb->asoc.hb_ect_randombit = 0;
  261. }
  262. if (this_random % 2 == 1) {
  263. max_cwnd_net = mnet;
  264. max_cwnd = mnet->cwnd; /* Useless? */
  265. }
  266. }
  267. }
  268. if (max_cwnd_net == NULL) {
  269. if (min_errors_net == NULL) {
  270. return (net);
  271. }
  272. return (min_errors_net);
  273. } else {
  274. return (max_cwnd_net);
  275. }
  276. } /* JRS 5/14/07 - If mode is set to 1, use the CMT policy for choosing an alternate net. */
  277. else if (mode == 1) {
  278. TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
  279. if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
  280. (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
  281. /*
  282. * will skip ones that are not-reachable or
  283. * unconfirmed
  284. */
  285. continue;
  286. }
  287. if (max_cwnd < mnet->cwnd) {
  288. max_cwnd_net = mnet;
  289. max_cwnd = mnet->cwnd;
  290. } else if (max_cwnd == mnet->cwnd) {
  291. uint32_t rndval;
  292. uint8_t this_random;
  293. if (stcb->asoc.hb_random_idx > 3) {
  294. rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
  295. memcpy(stcb->asoc.hb_random_values, &rndval,
  296. sizeof(stcb->asoc.hb_random_values));
  297. this_random = stcb->asoc.hb_random_values[0];
  298. stcb->asoc.hb_random_idx = 0;
  299. stcb->asoc.hb_ect_randombit = 0;
  300. } else {
  301. this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
  302. stcb->asoc.hb_random_idx++;
  303. stcb->asoc.hb_ect_randombit = 0;
  304. }
  305. if (this_random % 2) {
  306. max_cwnd_net = mnet;
  307. max_cwnd = mnet->cwnd;
  308. }
  309. }
  310. }
  311. if (max_cwnd_net) {
  312. return (max_cwnd_net);
  313. }
  314. }
  315. /* Look for an alternate net, which is active. */
  316. if ((net != NULL) && ((net->dest_state & SCTP_ADDR_BEING_DELETED) == 0)) {
  317. alt = TAILQ_NEXT(net, sctp_next);
  318. } else {
  319. alt = TAILQ_FIRST(&stcb->asoc.nets);
  320. }
  321. looped = false;
  322. for (;;) {
  323. if (alt == NULL) {
  324. if (!looped) {
  325. alt = TAILQ_FIRST(&stcb->asoc.nets);
  326. looped = true;
  327. }
  328. /* Definitely out of candidates. */
  329. if (alt == NULL) {
  330. break;
  331. }
  332. }
  333. #if defined(__FreeBSD__) && !defined(__Userspace__)
  334. if (alt->ro.ro_nh == NULL) {
  335. #else
  336. if (alt->ro.ro_rt == NULL) {
  337. #endif
  338. if (alt->ro._s_addr) {
  339. sctp_free_ifa(alt->ro._s_addr);
  340. alt->ro._s_addr = NULL;
  341. }
  342. alt->src_addr_selected = 0;
  343. }
  344. if (((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
  345. #if defined(__FreeBSD__) && !defined(__Userspace__)
  346. (alt->ro.ro_nh != NULL) &&
  347. #else
  348. (alt->ro.ro_rt != NULL) &&
  349. #endif
  350. ((alt->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) &&
  351. (alt != net)) {
  352. /* Found an alternate net, which is reachable. */
  353. break;
  354. }
  355. alt = TAILQ_NEXT(alt, sctp_next);
  356. }
  357. if (alt == NULL) {
  358. /*
  359. * In case no active alternate net has been found, look for
  360. * an alternate net, which is confirmed.
  361. */
  362. if ((net != NULL) && ((net->dest_state & SCTP_ADDR_BEING_DELETED) == 0)) {
  363. alt = TAILQ_NEXT(net, sctp_next);
  364. } else {
  365. alt = TAILQ_FIRST(&stcb->asoc.nets);
  366. }
  367. looped = false;
  368. for (;;) {
  369. if (alt == NULL) {
  370. if (!looped) {
  371. alt = TAILQ_FIRST(&stcb->asoc.nets);
  372. looped = true;
  373. }
  374. /* Definitely out of candidates. */
  375. if (alt == NULL) {
  376. break;
  377. }
  378. }
  379. if (((alt->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) &&
  380. (alt != net)) {
  381. /* Found an alternate net, which is confirmed. */
  382. break;
  383. }
  384. alt = TAILQ_NEXT(alt, sctp_next);
  385. }
  386. }
  387. if (alt == NULL) {
  388. /*
  389. * In case no confirmed alternate net has been found, just
  390. * return net, if it is not being deleted. In the other case
  391. * just return the first net.
  392. */
  393. if ((net != NULL) && ((net->dest_state & SCTP_ADDR_BEING_DELETED) == 0)) {
  394. alt = net;
  395. }
  396. if (alt == NULL) {
  397. alt = TAILQ_FIRST(&stcb->asoc.nets);
  398. }
  399. }
  400. return (alt);
  401. }
  402. static void
  403. sctp_backoff_on_timeout(struct sctp_tcb *stcb,
  404. struct sctp_nets *net,
  405. int win_probe,
  406. int num_marked, int num_abandoned)
  407. {
  408. if (net->RTO == 0) {
  409. if (net->RTO_measured) {
  410. net->RTO = stcb->asoc.minrto;
  411. } else {
  412. net->RTO = stcb->asoc.initial_rto;
  413. }
  414. }
  415. net->RTO <<= 1;
  416. if (net->RTO > stcb->asoc.maxrto) {
  417. net->RTO = stcb->asoc.maxrto;
  418. }
  419. if ((win_probe == 0) && (num_marked || num_abandoned)) {
  420. /* We don't apply penalty to window probe scenarios */
  421. /* JRS - Use the congestion control given in the CC module */
  422. stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
  423. }
  424. }
  425. #ifndef INVARIANTS
  426. static void
  427. sctp_recover_sent_list(struct sctp_tcb *stcb)
  428. {
  429. struct sctp_tmit_chunk *chk, *nchk;
  430. struct sctp_association *asoc;
  431. asoc = &stcb->asoc;
  432. TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
  433. if (SCTP_TSN_GE(asoc->last_acked_seq, chk->rec.data.tsn)) {
  434. SCTP_PRINTF("Found chk:%p tsn:%x <= last_acked_seq:%x\n",
  435. (void *)chk, chk->rec.data.tsn, asoc->last_acked_seq);
  436. if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
  437. if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
  438. asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
  439. }
  440. }
  441. if ((asoc->strmout[chk->rec.data.sid].chunks_on_queues == 0) &&
  442. (asoc->strmout[chk->rec.data.sid].state == SCTP_STREAM_RESET_PENDING) &&
  443. TAILQ_EMPTY(&asoc->strmout[chk->rec.data.sid].outqueue)) {
  444. asoc->trigger_reset = 1;
  445. }
  446. TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
  447. if (PR_SCTP_ENABLED(chk->flags)) {
  448. if (asoc->pr_sctp_cnt != 0)
  449. asoc->pr_sctp_cnt--;
  450. }
  451. if (chk->data) {
  452. /*sa_ignore NO_NULL_CHK*/
  453. sctp_free_bufspace(stcb, asoc, chk, 1);
  454. sctp_m_freem(chk->data);
  455. chk->data = NULL;
  456. if (asoc->prsctp_supported && PR_SCTP_BUF_ENABLED(chk->flags)) {
  457. asoc->sent_queue_cnt_removeable--;
  458. }
  459. }
  460. asoc->sent_queue_cnt--;
  461. sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
  462. }
  463. }
  464. SCTP_PRINTF("after recover order is as follows\n");
  465. TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
  466. SCTP_PRINTF("chk:%p TSN:%x\n", (void *)chk, chk->rec.data.tsn);
  467. }
  468. }
  469. #endif
  470. static int
  471. sctp_mark_all_for_resend(struct sctp_tcb *stcb,
  472. struct sctp_nets *net,
  473. struct sctp_nets *alt,
  474. int window_probe,
  475. int *num_marked,
  476. int *num_abandoned)
  477. {
  478. /*
  479. * Mark all chunks (well not all) that were sent to *net for
  480. * retransmission. Move them to alt for there destination as well...
  481. * We only mark chunks that have been outstanding long enough to
  482. * have received feed-back.
  483. */
  484. struct sctp_tmit_chunk *chk, *nchk;
  485. struct sctp_nets *lnets;
  486. struct timeval now, min_wait, tv;
  487. int cur_rto;
  488. int cnt_abandoned;
  489. int audit_tf, num_mk, fir;
  490. unsigned int cnt_mk;
  491. uint32_t orig_flight, orig_tf;
  492. uint32_t tsnlast, tsnfirst;
  493. #ifndef INVARIANTS
  494. int recovery_cnt = 0;
  495. #endif
  496. /* none in flight now */
  497. audit_tf = 0;
  498. fir = 0;
  499. /*
  500. * figure out how long a data chunk must be pending before we can
  501. * mark it ..
  502. */
  503. (void)SCTP_GETTIME_TIMEVAL(&now);
  504. /* get cur rto in micro-seconds */
  505. cur_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
  506. cur_rto *= 1000;
  507. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
  508. sctp_log_fr(cur_rto,
  509. stcb->asoc.peers_rwnd,
  510. window_probe,
  511. SCTP_FR_T3_MARK_TIME);
  512. sctp_log_fr(net->flight_size, 0, 0, SCTP_FR_CWND_REPORT);
  513. sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
  514. }
  515. tv.tv_sec = cur_rto / 1000000;
  516. tv.tv_usec = cur_rto % 1000000;
  517. #if !(defined(__FreeBSD__) && !defined(__Userspace__))
  518. timersub(&now, &tv, &min_wait);
  519. #else
  520. min_wait = now;
  521. timevalsub(&min_wait, &tv);
  522. #endif
  523. if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
  524. /*
  525. * if we hit here, we don't have enough seconds on the clock
  526. * to account for the RTO. We just let the lower seconds be
  527. * the bounds and don't worry about it. This may mean we
  528. * will mark a lot more than we should.
  529. */
  530. min_wait.tv_sec = min_wait.tv_usec = 0;
  531. }
  532. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
  533. sctp_log_fr(cur_rto, (uint32_t)now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
  534. sctp_log_fr(0, (uint32_t)min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
  535. }
  536. /*
  537. * Our rwnd will be incorrect here since we are not adding back the
  538. * cnt * mbuf but we will fix that down below.
  539. */
  540. orig_flight = net->flight_size;
  541. orig_tf = stcb->asoc.total_flight;
  542. net->fast_retran_ip = 0;
  543. /* Now on to each chunk */
  544. cnt_abandoned = 0;
  545. num_mk = cnt_mk = 0;
  546. tsnfirst = tsnlast = 0;
  547. #ifndef INVARIANTS
  548. start_again:
  549. #endif
  550. TAILQ_FOREACH_SAFE(chk, &stcb->asoc.sent_queue, sctp_next, nchk) {
  551. if (SCTP_TSN_GE(stcb->asoc.last_acked_seq, chk->rec.data.tsn)) {
  552. /* Strange case our list got out of order? */
  553. SCTP_PRINTF("Our list is out of order? last_acked:%x chk:%x\n",
  554. (unsigned int)stcb->asoc.last_acked_seq, (unsigned int)chk->rec.data.tsn);
  555. #ifdef INVARIANTS
  556. panic("last acked >= chk on sent-Q");
  557. #else
  558. recovery_cnt++;
  559. SCTP_PRINTF("Recover attempts a restart cnt:%d\n", recovery_cnt);
  560. sctp_recover_sent_list(stcb);
  561. if (recovery_cnt < 10) {
  562. goto start_again;
  563. } else {
  564. SCTP_PRINTF("Recovery fails %d times??\n", recovery_cnt);
  565. }
  566. #endif
  567. }
  568. if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
  569. /*
  570. * found one to mark: If it is less than
  571. * DATAGRAM_ACKED it MUST not be a skipped or marked
  572. * TSN but instead one that is either already set
  573. * for retransmission OR one that needs
  574. * retransmission.
  575. */
  576. /* validate its been outstanding long enough */
  577. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
  578. sctp_log_fr(chk->rec.data.tsn,
  579. (uint32_t)chk->sent_rcv_time.tv_sec,
  580. chk->sent_rcv_time.tv_usec,
  581. SCTP_FR_T3_MARK_TIME);
  582. }
  583. if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
  584. /*
  585. * we have reached a chunk that was sent
  586. * some seconds past our min.. forget it we
  587. * will find no more to send.
  588. */
  589. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
  590. sctp_log_fr(0,
  591. (uint32_t)chk->sent_rcv_time.tv_sec,
  592. chk->sent_rcv_time.tv_usec,
  593. SCTP_FR_T3_STOPPED);
  594. }
  595. continue;
  596. } else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
  597. (window_probe == 0)) {
  598. /*
  599. * we must look at the micro seconds to
  600. * know.
  601. */
  602. if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
  603. /*
  604. * ok it was sent after our boundary
  605. * time.
  606. */
  607. continue;
  608. }
  609. }
  610. if (stcb->asoc.prsctp_supported && PR_SCTP_TTL_ENABLED(chk->flags)) {
  611. /* Is it expired? */
  612. #if !(defined(__FreeBSD__) && !defined(__Userspace__))
  613. if (timercmp(&now, &chk->rec.data.timetodrop, >)) {
  614. #else
  615. if (timevalcmp(&now, &chk->rec.data.timetodrop, >)) {
  616. #endif
  617. /* Yes so drop it */
  618. if (chk->data) {
  619. (void)sctp_release_pr_sctp_chunk(stcb,
  620. chk,
  621. 1,
  622. SCTP_SO_NOT_LOCKED);
  623. cnt_abandoned++;
  624. }
  625. continue;
  626. }
  627. }
  628. if (stcb->asoc.prsctp_supported && PR_SCTP_RTX_ENABLED(chk->flags)) {
  629. /* Has it been retransmitted tv_sec times? */
  630. if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
  631. if (chk->data) {
  632. (void)sctp_release_pr_sctp_chunk(stcb,
  633. chk,
  634. 1,
  635. SCTP_SO_NOT_LOCKED);
  636. cnt_abandoned++;
  637. }
  638. continue;
  639. }
  640. }
  641. if (chk->sent < SCTP_DATAGRAM_RESEND) {
  642. sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
  643. num_mk++;
  644. if (fir == 0) {
  645. fir = 1;
  646. tsnfirst = chk->rec.data.tsn;
  647. }
  648. tsnlast = chk->rec.data.tsn;
  649. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
  650. sctp_log_fr(chk->rec.data.tsn, chk->snd_count,
  651. 0, SCTP_FR_T3_MARKED);
  652. }
  653. if (chk->rec.data.chunk_was_revoked) {
  654. /* deflate the cwnd */
  655. chk->whoTo->cwnd -= chk->book_size;
  656. chk->rec.data.chunk_was_revoked = 0;
  657. }
  658. net->marked_retrans++;
  659. stcb->asoc.marked_retrans++;
  660. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
  661. sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
  662. chk->whoTo->flight_size,
  663. chk->book_size,
  664. (uint32_t)(uintptr_t)chk->whoTo,
  665. chk->rec.data.tsn);
  666. }
  667. sctp_flight_size_decrease(chk);
  668. sctp_total_flight_decrease(stcb, chk);
  669. stcb->asoc.peers_rwnd += chk->send_size;
  670. stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
  671. }
  672. chk->sent = SCTP_DATAGRAM_RESEND;
  673. chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
  674. SCTP_STAT_INCR(sctps_markedretrans);
  675. /* reset the TSN for striking and other FR stuff */
  676. chk->rec.data.doing_fast_retransmit = 0;
  677. /* Clear any time so NO RTT is being done */
  678. if (chk->do_rtt) {
  679. if (chk->whoTo->rto_needed == 0) {
  680. chk->whoTo->rto_needed = 1;
  681. }
  682. }
  683. chk->do_rtt = 0;
  684. if (alt != net) {
  685. sctp_free_remote_addr(chk->whoTo);
  686. chk->no_fr_allowed = 1;
  687. chk->whoTo = alt;
  688. atomic_add_int(&alt->ref_count, 1);
  689. } else {
  690. chk->no_fr_allowed = 0;
  691. if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
  692. chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
  693. } else {
  694. chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
  695. }
  696. }
  697. /* CMT: Do not allow FRs on retransmitted TSNs.
  698. */
  699. if (stcb->asoc.sctp_cmt_on_off > 0) {
  700. chk->no_fr_allowed = 1;
  701. }
  702. #ifdef THIS_SHOULD_NOT_BE_DONE
  703. } else if (chk->sent == SCTP_DATAGRAM_ACKED) {
  704. /* remember highest acked one */
  705. could_be_sent = chk;
  706. #endif
  707. }
  708. if (chk->sent == SCTP_DATAGRAM_RESEND) {
  709. cnt_mk++;
  710. }
  711. }
  712. if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
  713. /* we did not subtract the same things? */
  714. audit_tf = 1;
  715. }
  716. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
  717. sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
  718. }
  719. #ifdef SCTP_DEBUG
  720. if (num_mk) {
  721. SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
  722. tsnlast);
  723. SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%u\n",
  724. num_mk,
  725. stcb->asoc.peers_rwnd);
  726. }
  727. #endif
  728. *num_marked = num_mk;
  729. *num_abandoned = cnt_abandoned;
  730. /* Now check for a ECN Echo that may be stranded And
  731. * include the cnt_mk'd to have all resends in the
  732. * control queue.
  733. */
  734. TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
  735. if (chk->sent == SCTP_DATAGRAM_RESEND) {
  736. cnt_mk++;
  737. }
  738. if ((chk->whoTo == net) &&
  739. (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
  740. sctp_free_remote_addr(chk->whoTo);
  741. chk->whoTo = alt;
  742. if (chk->sent != SCTP_DATAGRAM_RESEND) {
  743. chk->sent = SCTP_DATAGRAM_RESEND;
  744. chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
  745. sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
  746. cnt_mk++;
  747. }
  748. atomic_add_int(&alt->ref_count, 1);
  749. }
  750. }
  751. #ifdef THIS_SHOULD_NOT_BE_DONE
  752. if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
  753. /* fix it so we retransmit the highest acked anyway */
  754. sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
  755. cnt_mk++;
  756. could_be_sent->sent = SCTP_DATAGRAM_RESEND;
  757. }
  758. #endif
  759. if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
  760. #ifdef INVARIANTS
  761. SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
  762. cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
  763. #endif
  764. #ifndef SCTP_AUDITING_ENABLED
  765. stcb->asoc.sent_queue_retran_cnt = cnt_mk;
  766. #endif
  767. }
  768. if (audit_tf) {
  769. SCTPDBG(SCTP_DEBUG_TIMER4,
  770. "Audit total flight due to negative value net:%p\n",
  771. (void *)net);
  772. stcb->asoc.total_flight = 0;
  773. stcb->asoc.total_flight_count = 0;
  774. /* Clear all networks flight size */
  775. TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
  776. lnets->flight_size = 0;
  777. SCTPDBG(SCTP_DEBUG_TIMER4,
  778. "Net:%p c-f cwnd:%d ssthresh:%d\n",
  779. (void *)lnets, lnets->cwnd, lnets->ssthresh);
  780. }
  781. TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
  782. if (chk->sent < SCTP_DATAGRAM_RESEND) {
  783. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
  784. sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
  785. chk->whoTo->flight_size,
  786. chk->book_size,
  787. (uint32_t)(uintptr_t)chk->whoTo,
  788. chk->rec.data.tsn);
  789. }
  790. sctp_flight_size_increase(chk);
  791. sctp_total_flight_increase(stcb, chk);
  792. }
  793. }
  794. }
  795. /* We return 1 if we only have a window probe outstanding */
  796. return (0);
  797. }
  798. int
  799. sctp_t3rxt_timer(struct sctp_inpcb *inp,
  800. struct sctp_tcb *stcb,
  801. struct sctp_nets *net)
  802. {
  803. struct sctp_nets *alt;
  804. int win_probe, num_mk, num_abandoned;
  805. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
  806. sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
  807. }
  808. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
  809. struct sctp_nets *lnet;
  810. TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
  811. if (net == lnet) {
  812. sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
  813. } else {
  814. sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
  815. }
  816. }
  817. }
  818. /* Find an alternate and mark those for retransmission */
  819. if ((stcb->asoc.peers_rwnd == 0) &&
  820. (stcb->asoc.total_flight < net->mtu)) {
  821. SCTP_STAT_INCR(sctps_timowindowprobe);
  822. win_probe = 1;
  823. } else {
  824. win_probe = 0;
  825. }
  826. if (win_probe == 0) {
  827. /* We don't do normal threshold management on window probes */
  828. if (sctp_threshold_management(inp, stcb, net,
  829. stcb->asoc.max_send_times)) {
  830. /* Association was destroyed */
  831. return (1);
  832. } else {
  833. if (net != stcb->asoc.primary_destination) {
  834. /* send a immediate HB if our RTO is stale */
  835. struct timeval now;
  836. uint32_t ms_goneby;
  837. (void)SCTP_GETTIME_TIMEVAL(&now);
  838. if (net->last_sent_time.tv_sec) {
  839. ms_goneby = (uint32_t)(now.tv_sec - net->last_sent_time.tv_sec) * 1000;
  840. } else {
  841. ms_goneby = 0;
  842. }
  843. if ((net->dest_state & SCTP_ADDR_PF) == 0) {
  844. if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
  845. /*
  846. * no recent feed back in an RTO or
  847. * more, request a RTT update
  848. */
  849. sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
  850. }
  851. }
  852. }
  853. }
  854. } else {
  855. /*
  856. * For a window probe we don't penalize the net's but only
  857. * the association. This may fail it if SACKs are not coming
  858. * back. If sack's are coming with rwnd locked at 0, we will
  859. * continue to hold things waiting for rwnd to raise
  860. */
  861. if (sctp_threshold_management(inp, stcb, NULL,
  862. stcb->asoc.max_send_times)) {
  863. /* Association was destroyed */
  864. return (1);
  865. }
  866. }
  867. if (stcb->asoc.sctp_cmt_on_off > 0) {
  868. if (net->pf_threshold < net->failure_threshold) {
  869. alt = sctp_find_alternate_net(stcb, net, 2);
  870. } else {
  871. /*
  872. * CMT: Using RTX_SSTHRESH policy for CMT.
  873. * If CMT is being used, then pick dest with
  874. * largest ssthresh for any retransmission.
  875. */
  876. alt = sctp_find_alternate_net(stcb, net, 1);
  877. /*
  878. * CUCv2: If a different dest is picked for
  879. * the retransmission, then new
  880. * (rtx-)pseudo_cumack needs to be tracked
  881. * for orig dest. Let CUCv2 track new (rtx-)
  882. * pseudo-cumack always.
  883. */
  884. net->find_pseudo_cumack = 1;
  885. net->find_rtx_pseudo_cumack = 1;
  886. }
  887. } else {
  888. alt = sctp_find_alternate_net(stcb, net, 0);
  889. }
  890. num_mk = 0;
  891. num_abandoned = 0;
  892. (void)sctp_mark_all_for_resend(stcb, net, alt, win_probe,
  893. &num_mk, &num_abandoned);
  894. /* FR Loss recovery just ended with the T3. */
  895. stcb->asoc.fast_retran_loss_recovery = 0;
  896. /* CMT FR loss recovery ended with the T3 */
  897. net->fast_retran_loss_recovery = 0;
  898. if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
  899. (net->flight_size == 0)) {
  900. (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins)(stcb, net);
  901. }
  902. /*
  903. * setup the sat loss recovery that prevents satellite cwnd advance.
  904. */
  905. stcb->asoc.sat_t3_loss_recovery = 1;
  906. stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
  907. /* Backoff the timer and cwnd */
  908. sctp_backoff_on_timeout(stcb, net, win_probe, num_mk, num_abandoned);
  909. if (((net->dest_state & SCTP_ADDR_REACHABLE) == 0) ||
  910. (net->dest_state & SCTP_ADDR_PF)) {
  911. /* Move all pending over too */
  912. sctp_move_chunks_from_net(stcb, net);
  913. /* Get the address that failed, to
  914. * force a new src address selection and
  915. * a route allocation.
  916. */
  917. if (net->ro._s_addr != NULL) {
  918. sctp_free_ifa(net->ro._s_addr);
  919. net->ro._s_addr = NULL;
  920. }
  921. net->src_addr_selected = 0;
  922. /* Force a route allocation too */
  923. #if defined(__FreeBSD__) && !defined(__Userspace__)
  924. RO_NHFREE(&net->ro);
  925. #else
  926. if (net->ro.ro_rt != NULL) {
  927. RTFREE(net->ro.ro_rt);
  928. net->ro.ro_rt = NULL;
  929. }
  930. #endif
  931. /* Was it our primary? */
  932. if ((stcb->asoc.primary_destination == net) && (alt != net)) {
  933. /*
  934. * Yes, note it as such and find an alternate note:
  935. * this means HB code must use this to resent the
  936. * primary if it goes active AND if someone does a
  937. * change-primary then this flag must be cleared
  938. * from any net structures.
  939. */
  940. if (stcb->asoc.alternate != NULL) {
  941. sctp_free_remote_addr(stcb->asoc.alternate);
  942. }
  943. stcb->asoc.alternate = alt;
  944. atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
  945. }
  946. }
  947. /*
  948. * Special case for cookie-echo'ed case, we don't do output but must
  949. * await the COOKIE-ACK before retransmission
  950. */
  951. if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
  952. /*
  953. * Here we just reset the timer and start again since we
  954. * have not established the asoc
  955. */
  956. sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
  957. return (0);
  958. }
  959. if (stcb->asoc.prsctp_supported) {
  960. struct sctp_tmit_chunk *lchk;
  961. lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
  962. /* C3. See if we need to send a Fwd-TSN */
  963. if (SCTP_TSN_GT(stcb->asoc.advanced_peer_ack_point, stcb->asoc.last_acked_seq)) {
  964. send_forward_tsn(stcb, &stcb->asoc);
  965. for (; lchk != NULL; lchk = TAILQ_NEXT(lchk, sctp_next)) {
  966. if (lchk->whoTo != NULL) {
  967. break;
  968. }
  969. }
  970. if (lchk != NULL) {
  971. /* Assure a timer is up */
  972. sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
  973. }
  974. }
  975. }
  976. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
  977. sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
  978. }
  979. return (0);
  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, SCTP_SO_NOT_LOCKED);
  994. return (0);
  995. }
  996. if (SCTP_GET_STATE(stcb) != 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, 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. alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
  1013. if (alt != stcb->asoc.primary_destination) {
  1014. sctp_move_chunks_from_net(stcb, stcb->asoc.primary_destination);
  1015. stcb->asoc.primary_destination = alt;
  1016. }
  1017. }
  1018. /* Send out a new init */
  1019. sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
  1020. return (0);
  1021. }
  1022. /*
  1023. * For cookie and asconf we actually need to find and mark for resend, then
  1024. * increment the resend counter (after all the threshold management stuff of
  1025. * course).
  1026. */
  1027. int
  1028. sctp_cookie_timer(struct sctp_inpcb *inp,
  1029. struct sctp_tcb *stcb,
  1030. struct sctp_nets *net SCTP_UNUSED)
  1031. {
  1032. struct sctp_nets *alt;
  1033. struct sctp_tmit_chunk *cookie;
  1034. /* first before all else we must find the cookie */
  1035. TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
  1036. if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
  1037. break;
  1038. }
  1039. }
  1040. if (cookie == NULL) {
  1041. if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
  1042. /* FOOBAR! */
  1043. struct mbuf *op_err;
  1044. op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
  1045. "Cookie timer expired, but no cookie");
  1046. inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_3;
  1047. sctp_abort_an_association(inp, stcb, op_err, false, SCTP_SO_NOT_LOCKED);
  1048. } else {
  1049. #ifdef INVARIANTS
  1050. panic("Cookie timer expires in wrong state?");
  1051. #else
  1052. SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(stcb));
  1053. return (0);
  1054. #endif
  1055. }
  1056. return (0);
  1057. }
  1058. /* Ok we found the cookie, threshold management next */
  1059. if (sctp_threshold_management(inp, stcb, cookie->whoTo,
  1060. stcb->asoc.max_init_times)) {
  1061. /* Assoc is over */
  1062. return (1);
  1063. }
  1064. /*
  1065. * Cleared threshold management, now lets backoff the address
  1066. * and select an alternate
  1067. */
  1068. stcb->asoc.dropped_special_cnt = 0;
  1069. sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0, 0);
  1070. alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
  1071. if (alt != cookie->whoTo) {
  1072. sctp_free_remote_addr(cookie->whoTo);
  1073. cookie->whoTo = alt;
  1074. atomic_add_int(&alt->ref_count, 1);
  1075. }
  1076. /* Now mark the retran info */
  1077. if (cookie->sent != SCTP_DATAGRAM_RESEND) {
  1078. sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
  1079. }
  1080. cookie->sent = SCTP_DATAGRAM_RESEND;
  1081. cookie->flags |= CHUNK_FLAGS_FRAGMENT_OK;
  1082. /*
  1083. * Now call the output routine to kick out the cookie again, Note we
  1084. * don't mark any chunks for retran so that FR will need to kick in
  1085. * to move these (or a send timer).
  1086. */
  1087. return (0);
  1088. }
  1089. int
  1090. sctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
  1091. {
  1092. struct sctp_nets *alt, *net;
  1093. struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
  1094. if (stcb->asoc.stream_reset_outstanding == 0) {
  1095. return (0);
  1096. }
  1097. /* find the existing STRRESET, we use the seq number we sent out on */
  1098. (void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
  1099. if (strrst == NULL) {
  1100. return (0);
  1101. }
  1102. net = strrst->whoTo;
  1103. /* do threshold management */
  1104. if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
  1105. /* Assoc is over */
  1106. return (1);
  1107. }
  1108. /*
  1109. * Cleared threshold management, now lets backoff the address
  1110. * and select an alternate
  1111. */
  1112. sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
  1113. alt = sctp_find_alternate_net(stcb, net, 0);
  1114. strrst->whoTo = alt;
  1115. atomic_add_int(&alt->ref_count, 1);
  1116. /* See if a ECN Echo is also stranded */
  1117. TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
  1118. if ((chk->whoTo == net) &&
  1119. (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
  1120. sctp_free_remote_addr(chk->whoTo);
  1121. if (chk->sent != SCTP_DATAGRAM_RESEND) {
  1122. chk->sent = SCTP_DATAGRAM_RESEND;
  1123. chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
  1124. sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
  1125. }
  1126. chk->whoTo = alt;
  1127. atomic_add_int(&alt->ref_count, 1);
  1128. }
  1129. }
  1130. if ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) {
  1131. /*
  1132. * If the address went un-reachable, we need to move to
  1133. * alternates for ALL chk's in queue
  1134. */
  1135. sctp_move_chunks_from_net(stcb, net);
  1136. }
  1137. sctp_free_remote_addr(net);
  1138. /* mark the retran info */
  1139. if (strrst->sent != SCTP_DATAGRAM_RESEND)
  1140. sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
  1141. strrst->sent = SCTP_DATAGRAM_RESEND;
  1142. strrst->flags |= CHUNK_FLAGS_FRAGMENT_OK;
  1143. /* restart the timer */
  1144. sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, alt);
  1145. return (0);
  1146. }
  1147. int
  1148. sctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  1149. struct sctp_nets *net)
  1150. {
  1151. struct sctp_nets *alt;
  1152. struct sctp_tmit_chunk *asconf, *chk;
  1153. /* is this a first send, or a retransmission? */
  1154. if (TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) {
  1155. /* compose a new ASCONF chunk and send it */
  1156. sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
  1157. } else {
  1158. /*
  1159. * Retransmission of the existing ASCONF is needed
  1160. */
  1161. /* find the existing ASCONF */
  1162. asconf = TAILQ_FIRST(&stcb->asoc.asconf_send_queue);
  1163. if (asconf == NULL) {
  1164. return (0);
  1165. }
  1166. net = asconf->whoTo;
  1167. /* do threshold management */
  1168. if (sctp_threshold_management(inp, stcb, net,
  1169. stcb->asoc.max_send_times)) {
  1170. /* Assoc is over */
  1171. return (1);
  1172. }
  1173. if (asconf->snd_count > stcb->asoc.max_send_times) {
  1174. /*
  1175. * Something is rotten: our peer is not responding to
  1176. * ASCONFs but apparently is to other chunks. i.e. it
  1177. * is not properly handling the chunk type upper bits.
  1178. * Mark this peer as ASCONF incapable and cleanup.
  1179. */
  1180. SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
  1181. sctp_asconf_cleanup(stcb);
  1182. return (0);
  1183. }
  1184. /*
  1185. * cleared threshold management, so now backoff the net and
  1186. * select an alternate
  1187. */
  1188. sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
  1189. alt = sctp_find_alternate_net(stcb, net, 0);
  1190. if (asconf->whoTo != alt) {
  1191. asconf->whoTo = alt;
  1192. atomic_add_int(&alt->ref_count, 1);
  1193. }
  1194. /* See if an ECN Echo is also stranded */
  1195. TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
  1196. if ((chk->whoTo == net) &&
  1197. (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
  1198. sctp_free_remote_addr(chk->whoTo);
  1199. chk->whoTo = alt;
  1200. if (chk->sent != SCTP_DATAGRAM_RESEND) {
  1201. chk->sent = SCTP_DATAGRAM_RESEND;
  1202. chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
  1203. sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
  1204. }
  1205. atomic_add_int(&alt->ref_count, 1);
  1206. }
  1207. }
  1208. TAILQ_FOREACH(chk, &stcb->asoc.asconf_send_queue, sctp_next) {
  1209. if (chk->whoTo != alt) {
  1210. sctp_free_remote_addr(chk->whoTo);
  1211. chk->whoTo = alt;
  1212. atomic_add_int(&alt->ref_count, 1);
  1213. }
  1214. if (asconf->sent != SCTP_DATAGRAM_RESEND && chk->sent != SCTP_DATAGRAM_UNSENT)
  1215. sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
  1216. chk->sent = SCTP_DATAGRAM_RESEND;
  1217. chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
  1218. }
  1219. if ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) {
  1220. /*
  1221. * If the address went un-reachable, we need to move
  1222. * to the alternate for ALL chunks in queue
  1223. */
  1224. sctp_move_chunks_from_net(stcb, net);
  1225. }
  1226. sctp_free_remote_addr(net);
  1227. /* mark the retran info */
  1228. if (asconf->sent != SCTP_DATAGRAM_RESEND)
  1229. sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
  1230. asconf->sent = SCTP_DATAGRAM_RESEND;
  1231. asconf->flags |= CHUNK_FLAGS_FRAGMENT_OK;
  1232. /* send another ASCONF if any and we can do */
  1233. sctp_send_asconf(stcb, alt, SCTP_ADDR_NOT_LOCKED);
  1234. }
  1235. return (0);
  1236. }
  1237. /* Mobility adaptation */
  1238. void
  1239. sctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
  1240. {
  1241. if (stcb->asoc.deleted_primary == NULL) {
  1242. SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n");
  1243. sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
  1244. return;
  1245. }
  1246. SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary ");
  1247. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
  1248. sctp_free_remote_addr(stcb->asoc.deleted_primary);
  1249. stcb->asoc.deleted_primary = NULL;
  1250. sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
  1251. return;
  1252. }
  1253. /*
  1254. * For the shutdown and shutdown-ack, we do not keep one around on the
  1255. * control queue. This means we must generate a new one and call the general
  1256. * chunk output routine, AFTER having done threshold management.
  1257. * It is assumed that net is non-NULL.
  1258. */
  1259. int
  1260. sctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  1261. struct sctp_nets *net)
  1262. {
  1263. struct sctp_nets *alt;
  1264. /* first threshold management */
  1265. if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
  1266. /* Assoc is over */
  1267. return (1);
  1268. }
  1269. sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
  1270. /* second select an alternative */
  1271. alt = sctp_find_alternate_net(stcb, net, 0);
  1272. /* third generate a shutdown into the queue for out net */
  1273. sctp_send_shutdown(stcb, alt);
  1274. /* fourth restart timer */
  1275. sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
  1276. return (0);
  1277. }
  1278. int
  1279. sctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  1280. struct sctp_nets *net)
  1281. {
  1282. struct sctp_nets *alt;
  1283. /* first threshold management */
  1284. if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
  1285. /* Assoc is over */
  1286. return (1);
  1287. }
  1288. sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
  1289. /* second select an alternative */
  1290. alt = sctp_find_alternate_net(stcb, net, 0);
  1291. /* third generate a shutdown into the queue for out net */
  1292. sctp_send_shutdown_ack(stcb, alt);
  1293. /* fourth restart timer */
  1294. sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
  1295. return (0);
  1296. }
  1297. static void
  1298. sctp_audit_stream_queues_for_size(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
  1299. {
  1300. struct sctp_stream_queue_pending *sp;
  1301. unsigned int i, chks_in_queue = 0;
  1302. int being_filled = 0;
  1303. KASSERT(inp != NULL, ("inp is NULL"));
  1304. KASSERT(stcb != NULL, ("stcb is NULL"));
  1305. SCTP_TCB_LOCK_ASSERT(stcb);
  1306. KASSERT(TAILQ_EMPTY(&stcb->asoc.send_queue), ("send_queue not empty"));
  1307. KASSERT(TAILQ_EMPTY(&stcb->asoc.sent_queue), ("sent_queue not empty"));
  1308. if (stcb->asoc.sent_queue_retran_cnt) {
  1309. SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
  1310. stcb->asoc.sent_queue_retran_cnt);
  1311. stcb->asoc.sent_queue_retran_cnt = 0;
  1312. }
  1313. if (stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
  1314. /* No stream scheduler information, initialize scheduler */
  1315. stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc);
  1316. if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
  1317. /* yep, we lost a stream or two */
  1318. SCTP_PRINTF("Found additional streams NOT managed by scheduler, corrected\n");
  1319. } else {
  1320. /* no streams lost */
  1321. stcb->asoc.total_output_queue_size = 0;
  1322. }
  1323. }
  1324. /* Check to see if some data queued, if so report it */
  1325. for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
  1326. if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
  1327. TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
  1328. if (sp->msg_is_complete)
  1329. being_filled++;
  1330. chks_in_queue++;
  1331. }
  1332. }
  1333. }
  1334. if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
  1335. SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
  1336. stcb->asoc.stream_queue_cnt, chks_in_queue);
  1337. }
  1338. if (chks_in_queue) {
  1339. /* call the output queue function */
  1340. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
  1341. if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
  1342. (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
  1343. /*
  1344. * Probably should go in and make it go back through
  1345. * and add fragments allowed
  1346. */
  1347. if (being_filled == 0) {
  1348. SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
  1349. chks_in_queue);
  1350. }
  1351. }
  1352. } else {
  1353. SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
  1354. (u_long)stcb->asoc.total_output_queue_size);
  1355. stcb->asoc.total_output_queue_size = 0;
  1356. }
  1357. }
  1358. int
  1359. sctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  1360. struct sctp_nets *net)
  1361. {
  1362. bool net_was_pf;
  1363. net_was_pf = (net->dest_state & SCTP_ADDR_PF) != 0;
  1364. if (net->hb_responded == 0) {
  1365. if (net->ro._s_addr != NULL) {
  1366. /* Invalidate the src address if we did not get
  1367. * a response last time.
  1368. */
  1369. sctp_free_ifa(net->ro._s_addr);
  1370. net->ro._s_addr = NULL;
  1371. net->src_addr_selected = 0;
  1372. }
  1373. sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
  1374. if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
  1375. /* Assoc is over */
  1376. return (1);
  1377. }
  1378. }
  1379. /* Zero PBA, if it needs it */
  1380. if (net->partial_bytes_acked > 0) {
  1381. net->partial_bytes_acked = 0;
  1382. }
  1383. if ((stcb->asoc.total_output_queue_size > 0) &&
  1384. (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
  1385. (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
  1386. sctp_audit_stream_queues_for_size(inp, stcb);
  1387. }
  1388. if ((((net->dest_state & SCTP_ADDR_NOHB) == 0) ||
  1389. (net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
  1390. (net_was_pf || ((net->dest_state & SCTP_ADDR_PF) == 0))) {
  1391. /* When moving to PF during threshold management, a HB has been
  1392. queued in that routine. */
  1393. uint32_t ms_gone_by;
  1394. if ((net->last_sent_time.tv_sec > 0) ||
  1395. (net->last_sent_time.tv_usec > 0)) {
  1396. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1397. struct timeval diff;
  1398. SCTP_GETTIME_TIMEVAL(&diff);
  1399. timevalsub(&diff, &net->last_sent_time);
  1400. #else
  1401. struct timeval diff, now;
  1402. SCTP_GETTIME_TIMEVAL(&now);
  1403. timersub(&now, &net->last_sent_time, &diff);
  1404. #endif
  1405. ms_gone_by = (uint32_t)(diff.tv_sec * 1000) +
  1406. (uint32_t)(diff.tv_usec / 1000);
  1407. } else {
  1408. ms_gone_by = 0xffffffff;
  1409. }
  1410. if ((ms_gone_by >= net->heart_beat_delay) ||
  1411. (net->dest_state & SCTP_ADDR_UNCONFIRMED) ||
  1412. (net->dest_state & SCTP_ADDR_PF)) {
  1413. sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
  1414. }
  1415. }
  1416. return (0);
  1417. }
  1418. void
  1419. sctp_pathmtu_timer(struct sctp_inpcb *inp,
  1420. struct sctp_tcb *stcb,
  1421. struct sctp_nets *net)
  1422. {
  1423. uint32_t next_mtu, mtu;
  1424. next_mtu = sctp_get_next_mtu(net->mtu);
  1425. if ((next_mtu > net->mtu) && (net->port == 0)) {
  1426. if ((net->src_addr_selected == 0) ||
  1427. (net->ro._s_addr == NULL) ||
  1428. (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
  1429. if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
  1430. sctp_free_ifa(net->ro._s_addr);
  1431. net->ro._s_addr = NULL;
  1432. net->src_addr_selected = 0;
  1433. } else if (net->ro._s_addr == NULL) {
  1434. #if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
  1435. if (net->ro._l_addr.sa.sa_family == AF_INET6) {
  1436. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
  1437. /* KAME hack: embed scopeid */
  1438. #if defined(__APPLE__) && !defined(__Userspace__)
  1439. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
  1440. (void)in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL);
  1441. #else
  1442. (void)in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL);
  1443. #endif
  1444. #elif defined(SCTP_KAME)
  1445. (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
  1446. #else
  1447. (void)in6_embedscope(&sin6->sin6_addr, sin6);
  1448. #endif
  1449. }
  1450. #endif
  1451. net->ro._s_addr = sctp_source_address_selection(inp,
  1452. stcb,
  1453. (sctp_route_t *)&net->ro,
  1454. net, 0, stcb->asoc.vrf_id);
  1455. #if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
  1456. if (net->ro._l_addr.sa.sa_family == AF_INET6) {
  1457. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
  1458. #ifdef SCTP_KAME
  1459. (void)sa6_recoverscope(sin6);
  1460. #else
  1461. (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
  1462. #endif /* SCTP_KAME */
  1463. }
  1464. #endif /* INET6 */
  1465. }
  1466. if (net->ro._s_addr)
  1467. net->src_addr_selected = 1;
  1468. }
  1469. if (net->ro._s_addr) {
  1470. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1471. mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_nh);
  1472. #else
  1473. mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
  1474. #endif
  1475. #if defined(INET) || defined(INET6)
  1476. if (net->port) {
  1477. mtu -= sizeof(struct udphdr);
  1478. }
  1479. #endif
  1480. if (mtu > next_mtu) {
  1481. net->mtu = next_mtu;
  1482. } else {
  1483. net->mtu = mtu;
  1484. }
  1485. }
  1486. }
  1487. /* restart the timer */
  1488. sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
  1489. }
  1490. void
  1491. sctp_autoclose_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
  1492. {
  1493. struct timeval tn, *tim_touse;
  1494. struct sctp_association *asoc;
  1495. uint32_t ticks_gone_by;
  1496. (void)SCTP_GETTIME_TIMEVAL(&tn);
  1497. if (stcb->asoc.sctp_autoclose_ticks > 0 &&
  1498. sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
  1499. /* Auto close is on */
  1500. asoc = &stcb->asoc;
  1501. /* pick the time to use */
  1502. if (asoc->time_last_rcvd.tv_sec >
  1503. asoc->time_last_sent.tv_sec) {
  1504. tim_touse = &asoc->time_last_rcvd;
  1505. } else {
  1506. tim_touse = &asoc->time_last_sent;
  1507. }
  1508. /* Now has long enough transpired to autoclose? */
  1509. ticks_gone_by = sctp_secs_to_ticks((uint32_t)(tn.tv_sec - tim_touse->tv_sec));
  1510. if (ticks_gone_by >= asoc->sctp_autoclose_ticks) {
  1511. /*
  1512. * autoclose time has hit, call the output routine,
  1513. * which should do nothing just to be SURE we don't
  1514. * have hanging data. We can then safely check the
  1515. * queues and know that we are clear to send
  1516. * shutdown
  1517. */
  1518. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
  1519. /* Are we clean? */
  1520. if (TAILQ_EMPTY(&asoc->send_queue) &&
  1521. TAILQ_EMPTY(&asoc->sent_queue)) {
  1522. /*
  1523. * there is nothing queued to send, so I'm
  1524. * done...
  1525. */
  1526. if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
  1527. /* only send SHUTDOWN 1st time thru */
  1528. struct sctp_nets *net;
  1529. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
  1530. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  1531. SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  1532. }
  1533. SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
  1534. sctp_stop_timers_for_shutdown(stcb);
  1535. if (stcb->asoc.alternate) {
  1536. net = stcb->asoc.alternate;
  1537. } else {
  1538. net = stcb->asoc.primary_destination;
  1539. }
  1540. sctp_send_shutdown(stcb, net);
  1541. sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
  1542. stcb->sctp_ep, stcb, net);
  1543. sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
  1544. stcb->sctp_ep, stcb, NULL);
  1545. }
  1546. }
  1547. } else {
  1548. /*
  1549. * No auto close at this time, reset t-o to check
  1550. * later
  1551. */
  1552. uint32_t tmp;
  1553. /* fool the timer startup to use the time left */
  1554. tmp = asoc->sctp_autoclose_ticks;
  1555. asoc->sctp_autoclose_ticks -= ticks_gone_by;
  1556. sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
  1557. /* restore the real tick value */
  1558. asoc->sctp_autoclose_ticks = tmp;
  1559. }
  1560. }
  1561. }