transport.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001-2003 International Business Machines Corp.
  6. * Copyright (c) 2001 Intel Corp.
  7. * Copyright (c) 2001 La Monte H.P. Yarroll
  8. *
  9. * This file is part of the SCTP kernel implementation
  10. *
  11. * This module provides the abstraction for an SCTP transport representing
  12. * a remote transport address. For local transport addresses, we just use
  13. * union sctp_addr.
  14. *
  15. * Please send any bug reports or fixes you make to the
  16. * email address(es):
  17. * lksctp developers <[email protected]>
  18. *
  19. * Written or modified by:
  20. * La Monte H.P. Yarroll <[email protected]>
  21. * Karl Knutson <[email protected]>
  22. * Jon Grimm <[email protected]>
  23. * Xingang Guo <[email protected]>
  24. * Hui Huang <[email protected]>
  25. * Sridhar Samudrala <[email protected]>
  26. * Ardelle Fan <[email protected]>
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/slab.h>
  30. #include <linux/types.h>
  31. #include <linux/random.h>
  32. #include <net/sctp/sctp.h>
  33. #include <net/sctp/sm.h>
  34. /* 1st Level Abstractions. */
  35. /* Initialize a new transport from provided memory. */
  36. static struct sctp_transport *sctp_transport_init(struct net *net,
  37. struct sctp_transport *peer,
  38. const union sctp_addr *addr,
  39. gfp_t gfp)
  40. {
  41. /* Copy in the address. */
  42. peer->af_specific = sctp_get_af_specific(addr->sa.sa_family);
  43. memcpy(&peer->ipaddr, addr, peer->af_specific->sockaddr_len);
  44. memset(&peer->saddr, 0, sizeof(union sctp_addr));
  45. peer->sack_generation = 0;
  46. /* From 6.3.1 RTO Calculation:
  47. *
  48. * C1) Until an RTT measurement has been made for a packet sent to the
  49. * given destination transport address, set RTO to the protocol
  50. * parameter 'RTO.Initial'.
  51. */
  52. peer->rto = msecs_to_jiffies(net->sctp.rto_initial);
  53. peer->last_time_heard = 0;
  54. peer->last_time_ecne_reduced = jiffies;
  55. peer->param_flags = SPP_HB_DISABLE |
  56. SPP_PMTUD_ENABLE |
  57. SPP_SACKDELAY_ENABLE;
  58. /* Initialize the default path max_retrans. */
  59. peer->pathmaxrxt = net->sctp.max_retrans_path;
  60. peer->pf_retrans = net->sctp.pf_retrans;
  61. INIT_LIST_HEAD(&peer->transmitted);
  62. INIT_LIST_HEAD(&peer->send_ready);
  63. INIT_LIST_HEAD(&peer->transports);
  64. timer_setup(&peer->T3_rtx_timer, sctp_generate_t3_rtx_event, 0);
  65. timer_setup(&peer->hb_timer, sctp_generate_heartbeat_event, 0);
  66. timer_setup(&peer->reconf_timer, sctp_generate_reconf_event, 0);
  67. timer_setup(&peer->probe_timer, sctp_generate_probe_event, 0);
  68. timer_setup(&peer->proto_unreach_timer,
  69. sctp_generate_proto_unreach_event, 0);
  70. /* Initialize the 64-bit random nonce sent with heartbeat. */
  71. get_random_bytes(&peer->hb_nonce, sizeof(peer->hb_nonce));
  72. refcount_set(&peer->refcnt, 1);
  73. return peer;
  74. }
  75. /* Allocate and initialize a new transport. */
  76. struct sctp_transport *sctp_transport_new(struct net *net,
  77. const union sctp_addr *addr,
  78. gfp_t gfp)
  79. {
  80. struct sctp_transport *transport;
  81. transport = kzalloc(sizeof(*transport), gfp);
  82. if (!transport)
  83. goto fail;
  84. if (!sctp_transport_init(net, transport, addr, gfp))
  85. goto fail_init;
  86. SCTP_DBG_OBJCNT_INC(transport);
  87. return transport;
  88. fail_init:
  89. kfree(transport);
  90. fail:
  91. return NULL;
  92. }
  93. /* This transport is no longer needed. Free up if possible, or
  94. * delay until it last reference count.
  95. */
  96. void sctp_transport_free(struct sctp_transport *transport)
  97. {
  98. /* Try to delete the heartbeat timer. */
  99. if (del_timer(&transport->hb_timer))
  100. sctp_transport_put(transport);
  101. /* Delete the T3_rtx timer if it's active.
  102. * There is no point in not doing this now and letting
  103. * structure hang around in memory since we know
  104. * the transport is going away.
  105. */
  106. if (del_timer(&transport->T3_rtx_timer))
  107. sctp_transport_put(transport);
  108. if (del_timer(&transport->reconf_timer))
  109. sctp_transport_put(transport);
  110. if (del_timer(&transport->probe_timer))
  111. sctp_transport_put(transport);
  112. /* Delete the ICMP proto unreachable timer if it's active. */
  113. if (del_timer(&transport->proto_unreach_timer))
  114. sctp_transport_put(transport);
  115. sctp_transport_put(transport);
  116. }
  117. static void sctp_transport_destroy_rcu(struct rcu_head *head)
  118. {
  119. struct sctp_transport *transport;
  120. transport = container_of(head, struct sctp_transport, rcu);
  121. dst_release(transport->dst);
  122. kfree(transport);
  123. SCTP_DBG_OBJCNT_DEC(transport);
  124. }
  125. /* Destroy the transport data structure.
  126. * Assumes there are no more users of this structure.
  127. */
  128. static void sctp_transport_destroy(struct sctp_transport *transport)
  129. {
  130. if (unlikely(refcount_read(&transport->refcnt))) {
  131. WARN(1, "Attempt to destroy undead transport %p!\n", transport);
  132. return;
  133. }
  134. sctp_packet_free(&transport->packet);
  135. if (transport->asoc)
  136. sctp_association_put(transport->asoc);
  137. call_rcu(&transport->rcu, sctp_transport_destroy_rcu);
  138. }
  139. /* Start T3_rtx timer if it is not already running and update the heartbeat
  140. * timer. This routine is called every time a DATA chunk is sent.
  141. */
  142. void sctp_transport_reset_t3_rtx(struct sctp_transport *transport)
  143. {
  144. /* RFC 2960 6.3.2 Retransmission Timer Rules
  145. *
  146. * R1) Every time a DATA chunk is sent to any address(including a
  147. * retransmission), if the T3-rtx timer of that address is not running
  148. * start it running so that it will expire after the RTO of that
  149. * address.
  150. */
  151. if (!timer_pending(&transport->T3_rtx_timer))
  152. if (!mod_timer(&transport->T3_rtx_timer,
  153. jiffies + transport->rto))
  154. sctp_transport_hold(transport);
  155. }
  156. void sctp_transport_reset_hb_timer(struct sctp_transport *transport)
  157. {
  158. unsigned long expires;
  159. /* When a data chunk is sent, reset the heartbeat interval. */
  160. expires = jiffies + sctp_transport_timeout(transport);
  161. if (!mod_timer(&transport->hb_timer,
  162. expires + prandom_u32_max(transport->rto)))
  163. sctp_transport_hold(transport);
  164. }
  165. void sctp_transport_reset_reconf_timer(struct sctp_transport *transport)
  166. {
  167. if (!timer_pending(&transport->reconf_timer))
  168. if (!mod_timer(&transport->reconf_timer,
  169. jiffies + transport->rto))
  170. sctp_transport_hold(transport);
  171. }
  172. void sctp_transport_reset_probe_timer(struct sctp_transport *transport)
  173. {
  174. if (!mod_timer(&transport->probe_timer,
  175. jiffies + transport->probe_interval))
  176. sctp_transport_hold(transport);
  177. }
  178. void sctp_transport_reset_raise_timer(struct sctp_transport *transport)
  179. {
  180. if (!mod_timer(&transport->probe_timer,
  181. jiffies + transport->probe_interval * 30))
  182. sctp_transport_hold(transport);
  183. }
  184. /* This transport has been assigned to an association.
  185. * Initialize fields from the association or from the sock itself.
  186. * Register the reference count in the association.
  187. */
  188. void sctp_transport_set_owner(struct sctp_transport *transport,
  189. struct sctp_association *asoc)
  190. {
  191. transport->asoc = asoc;
  192. sctp_association_hold(asoc);
  193. }
  194. /* Initialize the pmtu of a transport. */
  195. void sctp_transport_pmtu(struct sctp_transport *transport, struct sock *sk)
  196. {
  197. /* If we don't have a fresh route, look one up */
  198. if (!transport->dst || transport->dst->obsolete) {
  199. sctp_transport_dst_release(transport);
  200. transport->af_specific->get_dst(transport, &transport->saddr,
  201. &transport->fl, sk);
  202. }
  203. if (transport->param_flags & SPP_PMTUD_DISABLE) {
  204. struct sctp_association *asoc = transport->asoc;
  205. if (!transport->pathmtu && asoc && asoc->pathmtu)
  206. transport->pathmtu = asoc->pathmtu;
  207. if (transport->pathmtu)
  208. return;
  209. }
  210. if (transport->dst)
  211. transport->pathmtu = sctp_dst_mtu(transport->dst);
  212. else
  213. transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
  214. sctp_transport_pl_update(transport);
  215. }
  216. void sctp_transport_pl_send(struct sctp_transport *t)
  217. {
  218. if (t->pl.probe_count < SCTP_MAX_PROBES)
  219. goto out;
  220. t->pl.probe_count = 0;
  221. if (t->pl.state == SCTP_PL_BASE) {
  222. if (t->pl.probe_size == SCTP_BASE_PLPMTU) { /* BASE_PLPMTU Confirmation Failed */
  223. t->pl.state = SCTP_PL_ERROR; /* Base -> Error */
  224. t->pl.pmtu = SCTP_BASE_PLPMTU;
  225. t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
  226. sctp_assoc_sync_pmtu(t->asoc);
  227. }
  228. } else if (t->pl.state == SCTP_PL_SEARCH) {
  229. if (t->pl.pmtu == t->pl.probe_size) { /* Black Hole Detected */
  230. t->pl.state = SCTP_PL_BASE; /* Search -> Base */
  231. t->pl.probe_size = SCTP_BASE_PLPMTU;
  232. t->pl.probe_high = 0;
  233. t->pl.pmtu = SCTP_BASE_PLPMTU;
  234. t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
  235. sctp_assoc_sync_pmtu(t->asoc);
  236. } else { /* Normal probe failure. */
  237. t->pl.probe_high = t->pl.probe_size;
  238. t->pl.probe_size = t->pl.pmtu;
  239. }
  240. } else if (t->pl.state == SCTP_PL_COMPLETE) {
  241. if (t->pl.pmtu == t->pl.probe_size) { /* Black Hole Detected */
  242. t->pl.state = SCTP_PL_BASE; /* Search Complete -> Base */
  243. t->pl.probe_size = SCTP_BASE_PLPMTU;
  244. t->pl.pmtu = SCTP_BASE_PLPMTU;
  245. t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
  246. sctp_assoc_sync_pmtu(t->asoc);
  247. }
  248. }
  249. out:
  250. pr_debug("%s: PLPMTUD: transport: %p, state: %d, pmtu: %d, size: %d, high: %d\n",
  251. __func__, t, t->pl.state, t->pl.pmtu, t->pl.probe_size, t->pl.probe_high);
  252. t->pl.probe_count++;
  253. }
  254. bool sctp_transport_pl_recv(struct sctp_transport *t)
  255. {
  256. pr_debug("%s: PLPMTUD: transport: %p, state: %d, pmtu: %d, size: %d, high: %d\n",
  257. __func__, t, t->pl.state, t->pl.pmtu, t->pl.probe_size, t->pl.probe_high);
  258. t->pl.pmtu = t->pl.probe_size;
  259. t->pl.probe_count = 0;
  260. if (t->pl.state == SCTP_PL_BASE) {
  261. t->pl.state = SCTP_PL_SEARCH; /* Base -> Search */
  262. t->pl.probe_size += SCTP_PL_BIG_STEP;
  263. } else if (t->pl.state == SCTP_PL_ERROR) {
  264. t->pl.state = SCTP_PL_SEARCH; /* Error -> Search */
  265. t->pl.pmtu = t->pl.probe_size;
  266. t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
  267. sctp_assoc_sync_pmtu(t->asoc);
  268. t->pl.probe_size += SCTP_PL_BIG_STEP;
  269. } else if (t->pl.state == SCTP_PL_SEARCH) {
  270. if (!t->pl.probe_high) {
  271. if (t->pl.probe_size < SCTP_MAX_PLPMTU) {
  272. t->pl.probe_size = min(t->pl.probe_size + SCTP_PL_BIG_STEP,
  273. SCTP_MAX_PLPMTU);
  274. return false;
  275. }
  276. t->pl.probe_high = SCTP_MAX_PLPMTU;
  277. }
  278. t->pl.probe_size += SCTP_PL_MIN_STEP;
  279. if (t->pl.probe_size >= t->pl.probe_high) {
  280. t->pl.probe_high = 0;
  281. t->pl.state = SCTP_PL_COMPLETE; /* Search -> Search Complete */
  282. t->pl.probe_size = t->pl.pmtu;
  283. t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
  284. sctp_assoc_sync_pmtu(t->asoc);
  285. sctp_transport_reset_raise_timer(t);
  286. }
  287. } else if (t->pl.state == SCTP_PL_COMPLETE) {
  288. /* Raise probe_size again after 30 * interval in Search Complete */
  289. t->pl.state = SCTP_PL_SEARCH; /* Search Complete -> Search */
  290. t->pl.probe_size = min(t->pl.probe_size + SCTP_PL_MIN_STEP, SCTP_MAX_PLPMTU);
  291. }
  292. return t->pl.state == SCTP_PL_COMPLETE;
  293. }
  294. static bool sctp_transport_pl_toobig(struct sctp_transport *t, u32 pmtu)
  295. {
  296. pr_debug("%s: PLPMTUD: transport: %p, state: %d, pmtu: %d, size: %d, ptb: %d\n",
  297. __func__, t, t->pl.state, t->pl.pmtu, t->pl.probe_size, pmtu);
  298. if (pmtu < SCTP_MIN_PLPMTU || pmtu >= t->pl.probe_size)
  299. return false;
  300. if (t->pl.state == SCTP_PL_BASE) {
  301. if (pmtu >= SCTP_MIN_PLPMTU && pmtu < SCTP_BASE_PLPMTU) {
  302. t->pl.state = SCTP_PL_ERROR; /* Base -> Error */
  303. t->pl.pmtu = SCTP_BASE_PLPMTU;
  304. t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
  305. return true;
  306. }
  307. } else if (t->pl.state == SCTP_PL_SEARCH) {
  308. if (pmtu >= SCTP_BASE_PLPMTU && pmtu < t->pl.pmtu) {
  309. t->pl.state = SCTP_PL_BASE; /* Search -> Base */
  310. t->pl.probe_size = SCTP_BASE_PLPMTU;
  311. t->pl.probe_count = 0;
  312. t->pl.probe_high = 0;
  313. t->pl.pmtu = SCTP_BASE_PLPMTU;
  314. t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
  315. return true;
  316. } else if (pmtu > t->pl.pmtu && pmtu < t->pl.probe_size) {
  317. t->pl.probe_size = pmtu;
  318. t->pl.probe_count = 0;
  319. }
  320. } else if (t->pl.state == SCTP_PL_COMPLETE) {
  321. if (pmtu >= SCTP_BASE_PLPMTU && pmtu < t->pl.pmtu) {
  322. t->pl.state = SCTP_PL_BASE; /* Complete -> Base */
  323. t->pl.probe_size = SCTP_BASE_PLPMTU;
  324. t->pl.probe_count = 0;
  325. t->pl.probe_high = 0;
  326. t->pl.pmtu = SCTP_BASE_PLPMTU;
  327. t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
  328. sctp_transport_reset_probe_timer(t);
  329. return true;
  330. }
  331. }
  332. return false;
  333. }
  334. bool sctp_transport_update_pmtu(struct sctp_transport *t, u32 pmtu)
  335. {
  336. struct sock *sk = t->asoc->base.sk;
  337. struct dst_entry *dst;
  338. bool change = true;
  339. if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
  340. pr_warn_ratelimited("%s: Reported pmtu %d too low, using default minimum of %d\n",
  341. __func__, pmtu, SCTP_DEFAULT_MINSEGMENT);
  342. /* Use default minimum segment instead */
  343. pmtu = SCTP_DEFAULT_MINSEGMENT;
  344. }
  345. pmtu = SCTP_TRUNC4(pmtu);
  346. if (sctp_transport_pl_enabled(t))
  347. return sctp_transport_pl_toobig(t, pmtu - sctp_transport_pl_hlen(t));
  348. dst = sctp_transport_dst_check(t);
  349. if (dst) {
  350. struct sctp_pf *pf = sctp_get_pf_specific(dst->ops->family);
  351. union sctp_addr addr;
  352. pf->af->from_sk(&addr, sk);
  353. pf->to_sk_daddr(&t->ipaddr, sk);
  354. dst->ops->update_pmtu(dst, sk, NULL, pmtu, true);
  355. pf->to_sk_daddr(&addr, sk);
  356. dst = sctp_transport_dst_check(t);
  357. }
  358. if (!dst) {
  359. t->af_specific->get_dst(t, &t->saddr, &t->fl, sk);
  360. dst = t->dst;
  361. }
  362. if (dst) {
  363. /* Re-fetch, as under layers may have a higher minimum size */
  364. pmtu = sctp_dst_mtu(dst);
  365. change = t->pathmtu != pmtu;
  366. }
  367. t->pathmtu = pmtu;
  368. return change;
  369. }
  370. /* Caches the dst entry and source address for a transport's destination
  371. * address.
  372. */
  373. void sctp_transport_route(struct sctp_transport *transport,
  374. union sctp_addr *saddr, struct sctp_sock *opt)
  375. {
  376. struct sctp_association *asoc = transport->asoc;
  377. struct sctp_af *af = transport->af_specific;
  378. sctp_transport_dst_release(transport);
  379. af->get_dst(transport, saddr, &transport->fl, sctp_opt2sk(opt));
  380. if (saddr)
  381. memcpy(&transport->saddr, saddr, sizeof(union sctp_addr));
  382. else
  383. af->get_saddr(opt, transport, &transport->fl);
  384. sctp_transport_pmtu(transport, sctp_opt2sk(opt));
  385. /* Initialize sk->sk_rcv_saddr, if the transport is the
  386. * association's active path for getsockname().
  387. */
  388. if (transport->dst && asoc &&
  389. (!asoc->peer.primary_path || transport == asoc->peer.active_path))
  390. opt->pf->to_sk_saddr(&transport->saddr, asoc->base.sk);
  391. }
  392. /* Hold a reference to a transport. */
  393. int sctp_transport_hold(struct sctp_transport *transport)
  394. {
  395. return refcount_inc_not_zero(&transport->refcnt);
  396. }
  397. /* Release a reference to a transport and clean up
  398. * if there are no more references.
  399. */
  400. void sctp_transport_put(struct sctp_transport *transport)
  401. {
  402. if (refcount_dec_and_test(&transport->refcnt))
  403. sctp_transport_destroy(transport);
  404. }
  405. /* Update transport's RTO based on the newly calculated RTT. */
  406. void sctp_transport_update_rto(struct sctp_transport *tp, __u32 rtt)
  407. {
  408. if (unlikely(!tp->rto_pending))
  409. /* We should not be doing any RTO updates unless rto_pending is set. */
  410. pr_debug("%s: rto_pending not set on transport %p!\n", __func__, tp);
  411. if (tp->rttvar || tp->srtt) {
  412. struct net *net = tp->asoc->base.net;
  413. /* 6.3.1 C3) When a new RTT measurement R' is made, set
  414. * RTTVAR <- (1 - RTO.Beta) * RTTVAR + RTO.Beta * |SRTT - R'|
  415. * SRTT <- (1 - RTO.Alpha) * SRTT + RTO.Alpha * R'
  416. */
  417. /* Note: The above algorithm has been rewritten to
  418. * express rto_beta and rto_alpha as inverse powers
  419. * of two.
  420. * For example, assuming the default value of RTO.Alpha of
  421. * 1/8, rto_alpha would be expressed as 3.
  422. */
  423. tp->rttvar = tp->rttvar - (tp->rttvar >> net->sctp.rto_beta)
  424. + (((__u32)abs((__s64)tp->srtt - (__s64)rtt)) >> net->sctp.rto_beta);
  425. tp->srtt = tp->srtt - (tp->srtt >> net->sctp.rto_alpha)
  426. + (rtt >> net->sctp.rto_alpha);
  427. } else {
  428. /* 6.3.1 C2) When the first RTT measurement R is made, set
  429. * SRTT <- R, RTTVAR <- R/2.
  430. */
  431. tp->srtt = rtt;
  432. tp->rttvar = rtt >> 1;
  433. }
  434. /* 6.3.1 G1) Whenever RTTVAR is computed, if RTTVAR = 0, then
  435. * adjust RTTVAR <- G, where G is the CLOCK GRANULARITY.
  436. */
  437. if (tp->rttvar == 0)
  438. tp->rttvar = SCTP_CLOCK_GRANULARITY;
  439. /* 6.3.1 C3) After the computation, update RTO <- SRTT + 4 * RTTVAR. */
  440. tp->rto = tp->srtt + (tp->rttvar << 2);
  441. /* 6.3.1 C6) Whenever RTO is computed, if it is less than RTO.Min
  442. * seconds then it is rounded up to RTO.Min seconds.
  443. */
  444. if (tp->rto < tp->asoc->rto_min)
  445. tp->rto = tp->asoc->rto_min;
  446. /* 6.3.1 C7) A maximum value may be placed on RTO provided it is
  447. * at least RTO.max seconds.
  448. */
  449. if (tp->rto > tp->asoc->rto_max)
  450. tp->rto = tp->asoc->rto_max;
  451. sctp_max_rto(tp->asoc, tp);
  452. tp->rtt = rtt;
  453. /* Reset rto_pending so that a new RTT measurement is started when a
  454. * new data chunk is sent.
  455. */
  456. tp->rto_pending = 0;
  457. pr_debug("%s: transport:%p, rtt:%d, srtt:%d rttvar:%d, rto:%ld\n",
  458. __func__, tp, rtt, tp->srtt, tp->rttvar, tp->rto);
  459. }
  460. /* This routine updates the transport's cwnd and partial_bytes_acked
  461. * parameters based on the bytes acked in the received SACK.
  462. */
  463. void sctp_transport_raise_cwnd(struct sctp_transport *transport,
  464. __u32 sack_ctsn, __u32 bytes_acked)
  465. {
  466. struct sctp_association *asoc = transport->asoc;
  467. __u32 cwnd, ssthresh, flight_size, pba, pmtu;
  468. cwnd = transport->cwnd;
  469. flight_size = transport->flight_size;
  470. /* See if we need to exit Fast Recovery first */
  471. if (asoc->fast_recovery &&
  472. TSN_lte(asoc->fast_recovery_exit, sack_ctsn))
  473. asoc->fast_recovery = 0;
  474. ssthresh = transport->ssthresh;
  475. pba = transport->partial_bytes_acked;
  476. pmtu = transport->asoc->pathmtu;
  477. if (cwnd <= ssthresh) {
  478. /* RFC 4960 7.2.1
  479. * o When cwnd is less than or equal to ssthresh, an SCTP
  480. * endpoint MUST use the slow-start algorithm to increase
  481. * cwnd only if the current congestion window is being fully
  482. * utilized, an incoming SACK advances the Cumulative TSN
  483. * Ack Point, and the data sender is not in Fast Recovery.
  484. * Only when these three conditions are met can the cwnd be
  485. * increased; otherwise, the cwnd MUST not be increased.
  486. * If these conditions are met, then cwnd MUST be increased
  487. * by, at most, the lesser of 1) the total size of the
  488. * previously outstanding DATA chunk(s) acknowledged, and
  489. * 2) the destination's path MTU. This upper bound protects
  490. * against the ACK-Splitting attack outlined in [SAVAGE99].
  491. */
  492. if (asoc->fast_recovery)
  493. return;
  494. /* The appropriate cwnd increase algorithm is performed
  495. * if, and only if the congestion window is being fully
  496. * utilized. Note that RFC4960 Errata 3.22 removed the
  497. * other condition on ctsn moving.
  498. */
  499. if (flight_size < cwnd)
  500. return;
  501. if (bytes_acked > pmtu)
  502. cwnd += pmtu;
  503. else
  504. cwnd += bytes_acked;
  505. pr_debug("%s: slow start: transport:%p, bytes_acked:%d, "
  506. "cwnd:%d, ssthresh:%d, flight_size:%d, pba:%d\n",
  507. __func__, transport, bytes_acked, cwnd, ssthresh,
  508. flight_size, pba);
  509. } else {
  510. /* RFC 2960 7.2.2 Whenever cwnd is greater than ssthresh,
  511. * upon each SACK arrival, increase partial_bytes_acked
  512. * by the total number of bytes of all new chunks
  513. * acknowledged in that SACK including chunks
  514. * acknowledged by the new Cumulative TSN Ack and by Gap
  515. * Ack Blocks. (updated by RFC4960 Errata 3.22)
  516. *
  517. * When partial_bytes_acked is greater than cwnd and
  518. * before the arrival of the SACK the sender had less
  519. * bytes of data outstanding than cwnd (i.e., before
  520. * arrival of the SACK, flightsize was less than cwnd),
  521. * reset partial_bytes_acked to cwnd. (RFC 4960 Errata
  522. * 3.26)
  523. *
  524. * When partial_bytes_acked is equal to or greater than
  525. * cwnd and before the arrival of the SACK the sender
  526. * had cwnd or more bytes of data outstanding (i.e.,
  527. * before arrival of the SACK, flightsize was greater
  528. * than or equal to cwnd), partial_bytes_acked is reset
  529. * to (partial_bytes_acked - cwnd). Next, cwnd is
  530. * increased by MTU. (RFC 4960 Errata 3.12)
  531. */
  532. pba += bytes_acked;
  533. if (pba > cwnd && flight_size < cwnd)
  534. pba = cwnd;
  535. if (pba >= cwnd && flight_size >= cwnd) {
  536. pba = pba - cwnd;
  537. cwnd += pmtu;
  538. }
  539. pr_debug("%s: congestion avoidance: transport:%p, "
  540. "bytes_acked:%d, cwnd:%d, ssthresh:%d, "
  541. "flight_size:%d, pba:%d\n", __func__,
  542. transport, bytes_acked, cwnd, ssthresh,
  543. flight_size, pba);
  544. }
  545. transport->cwnd = cwnd;
  546. transport->partial_bytes_acked = pba;
  547. }
  548. /* This routine is used to lower the transport's cwnd when congestion is
  549. * detected.
  550. */
  551. void sctp_transport_lower_cwnd(struct sctp_transport *transport,
  552. enum sctp_lower_cwnd reason)
  553. {
  554. struct sctp_association *asoc = transport->asoc;
  555. switch (reason) {
  556. case SCTP_LOWER_CWND_T3_RTX:
  557. /* RFC 2960 Section 7.2.3, sctpimpguide
  558. * When the T3-rtx timer expires on an address, SCTP should
  559. * perform slow start by:
  560. * ssthresh = max(cwnd/2, 4*MTU)
  561. * cwnd = 1*MTU
  562. * partial_bytes_acked = 0
  563. */
  564. transport->ssthresh = max(transport->cwnd/2,
  565. 4*asoc->pathmtu);
  566. transport->cwnd = asoc->pathmtu;
  567. /* T3-rtx also clears fast recovery */
  568. asoc->fast_recovery = 0;
  569. break;
  570. case SCTP_LOWER_CWND_FAST_RTX:
  571. /* RFC 2960 7.2.4 Adjust the ssthresh and cwnd of the
  572. * destination address(es) to which the missing DATA chunks
  573. * were last sent, according to the formula described in
  574. * Section 7.2.3.
  575. *
  576. * RFC 2960 7.2.3, sctpimpguide Upon detection of packet
  577. * losses from SACK (see Section 7.2.4), An endpoint
  578. * should do the following:
  579. * ssthresh = max(cwnd/2, 4*MTU)
  580. * cwnd = ssthresh
  581. * partial_bytes_acked = 0
  582. */
  583. if (asoc->fast_recovery)
  584. return;
  585. /* Mark Fast recovery */
  586. asoc->fast_recovery = 1;
  587. asoc->fast_recovery_exit = asoc->next_tsn - 1;
  588. transport->ssthresh = max(transport->cwnd/2,
  589. 4*asoc->pathmtu);
  590. transport->cwnd = transport->ssthresh;
  591. break;
  592. case SCTP_LOWER_CWND_ECNE:
  593. /* RFC 2481 Section 6.1.2.
  594. * If the sender receives an ECN-Echo ACK packet
  595. * then the sender knows that congestion was encountered in the
  596. * network on the path from the sender to the receiver. The
  597. * indication of congestion should be treated just as a
  598. * congestion loss in non-ECN Capable TCP. That is, the TCP
  599. * source halves the congestion window "cwnd" and reduces the
  600. * slow start threshold "ssthresh".
  601. * A critical condition is that TCP does not react to
  602. * congestion indications more than once every window of
  603. * data (or more loosely more than once every round-trip time).
  604. */
  605. if (time_after(jiffies, transport->last_time_ecne_reduced +
  606. transport->rtt)) {
  607. transport->ssthresh = max(transport->cwnd/2,
  608. 4*asoc->pathmtu);
  609. transport->cwnd = transport->ssthresh;
  610. transport->last_time_ecne_reduced = jiffies;
  611. }
  612. break;
  613. case SCTP_LOWER_CWND_INACTIVE:
  614. /* RFC 2960 Section 7.2.1, sctpimpguide
  615. * When the endpoint does not transmit data on a given
  616. * transport address, the cwnd of the transport address
  617. * should be adjusted to max(cwnd/2, 4*MTU) per RTO.
  618. * NOTE: Although the draft recommends that this check needs
  619. * to be done every RTO interval, we do it every hearbeat
  620. * interval.
  621. */
  622. transport->cwnd = max(transport->cwnd/2,
  623. 4*asoc->pathmtu);
  624. /* RFC 4960 Errata 3.27.2: also adjust sshthresh */
  625. transport->ssthresh = transport->cwnd;
  626. break;
  627. }
  628. transport->partial_bytes_acked = 0;
  629. pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d\n",
  630. __func__, transport, reason, transport->cwnd,
  631. transport->ssthresh);
  632. }
  633. /* Apply Max.Burst limit to the congestion window:
  634. * sctpimpguide-05 2.14.2
  635. * D) When the time comes for the sender to
  636. * transmit new DATA chunks, the protocol parameter Max.Burst MUST
  637. * first be applied to limit how many new DATA chunks may be sent.
  638. * The limit is applied by adjusting cwnd as follows:
  639. * if ((flightsize+ Max.Burst * MTU) < cwnd)
  640. * cwnd = flightsize + Max.Burst * MTU
  641. */
  642. void sctp_transport_burst_limited(struct sctp_transport *t)
  643. {
  644. struct sctp_association *asoc = t->asoc;
  645. u32 old_cwnd = t->cwnd;
  646. u32 max_burst_bytes;
  647. if (t->burst_limited || asoc->max_burst == 0)
  648. return;
  649. max_burst_bytes = t->flight_size + (asoc->max_burst * asoc->pathmtu);
  650. if (max_burst_bytes < old_cwnd) {
  651. t->cwnd = max_burst_bytes;
  652. t->burst_limited = old_cwnd;
  653. }
  654. }
  655. /* Restore the old cwnd congestion window, after the burst had it's
  656. * desired effect.
  657. */
  658. void sctp_transport_burst_reset(struct sctp_transport *t)
  659. {
  660. if (t->burst_limited) {
  661. t->cwnd = t->burst_limited;
  662. t->burst_limited = 0;
  663. }
  664. }
  665. /* What is the next timeout value for this transport? */
  666. unsigned long sctp_transport_timeout(struct sctp_transport *trans)
  667. {
  668. /* RTO + timer slack +/- 50% of RTO */
  669. unsigned long timeout = trans->rto >> 1;
  670. if (trans->state != SCTP_UNCONFIRMED &&
  671. trans->state != SCTP_PF)
  672. timeout += trans->hbinterval;
  673. return max_t(unsigned long, timeout, HZ / 5);
  674. }
  675. /* Reset transport variables to their initial values */
  676. void sctp_transport_reset(struct sctp_transport *t)
  677. {
  678. struct sctp_association *asoc = t->asoc;
  679. /* RFC 2960 (bis), Section 5.2.4
  680. * All the congestion control parameters (e.g., cwnd, ssthresh)
  681. * related to this peer MUST be reset to their initial values
  682. * (see Section 6.2.1)
  683. */
  684. t->cwnd = min(4*asoc->pathmtu, max_t(__u32, 2*asoc->pathmtu, 4380));
  685. t->burst_limited = 0;
  686. t->ssthresh = asoc->peer.i.a_rwnd;
  687. t->rto = asoc->rto_initial;
  688. sctp_max_rto(asoc, t);
  689. t->rtt = 0;
  690. t->srtt = 0;
  691. t->rttvar = 0;
  692. /* Reset these additional variables so that we have a clean slate. */
  693. t->partial_bytes_acked = 0;
  694. t->flight_size = 0;
  695. t->error_count = 0;
  696. t->rto_pending = 0;
  697. t->hb_sent = 0;
  698. /* Initialize the state information for SFR-CACC */
  699. t->cacc.changeover_active = 0;
  700. t->cacc.cycling_changeover = 0;
  701. t->cacc.next_tsn_at_change = 0;
  702. t->cacc.cacc_saw_newack = 0;
  703. }
  704. /* Schedule retransmission on the given transport */
  705. void sctp_transport_immediate_rtx(struct sctp_transport *t)
  706. {
  707. /* Stop pending T3_rtx_timer */
  708. if (del_timer(&t->T3_rtx_timer))
  709. sctp_transport_put(t);
  710. sctp_retransmit(&t->asoc->outqueue, t, SCTP_RTXR_T3_RTX);
  711. if (!timer_pending(&t->T3_rtx_timer)) {
  712. if (!mod_timer(&t->T3_rtx_timer, jiffies + t->rto))
  713. sctp_transport_hold(t);
  714. }
  715. }
  716. /* Drop dst */
  717. void sctp_transport_dst_release(struct sctp_transport *t)
  718. {
  719. dst_release(t->dst);
  720. t->dst = NULL;
  721. t->dst_pending_confirm = 0;
  722. }
  723. /* Schedule neighbour confirm */
  724. void sctp_transport_dst_confirm(struct sctp_transport *t)
  725. {
  726. t->dst_pending_confirm = 1;
  727. }