transport.c 26 KB

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