blob: ca3343c2c80e1bbfd1d55b979a542d7514446231 [file] [log] [blame]
Thomas Gleixner47505b82019-05-23 11:14:41 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vlad Yasevich60c778b2008-01-11 09:57:09 -05002/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05009 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
Christophe JAILLET5112cf52020-11-22 19:07:04 +010011 * This module provides the abstraction for an SCTP transport representing
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * a remote transport address. For local transport addresses, we just use
13 * union sctp_addr.
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Please send any bug reports or fixes you make to the
16 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020017 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
Joe Perches145ce502010-08-24 13:21:08 +000029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/types.h>
Sridhar Samudralaad8fec12006-07-21 14:48:50 -070033#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <net/sctp/sctp.h>
35#include <net/sctp/sm.h>
36
37/* 1st Level Abstractions. */
38
39/* Initialize a new transport from provided memory. */
Eric W. Biederman89bf3452012-08-07 07:26:14 +000040static struct sctp_transport *sctp_transport_init(struct net *net,
41 struct sctp_transport *peer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 const union sctp_addr *addr,
Al Virodd0fc662005-10-07 07:46:04 +010043 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 /* Copy in the address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 peer->af_specific = sctp_get_af_specific(addr->sa.sa_family);
Xin Long4c31bc62019-07-30 20:38:19 +080047 memcpy(&peer->ipaddr, addr, peer->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 memset(&peer->saddr, 0, sizeof(union sctp_addr));
49
Neil Horman42448542012-06-30 03:04:26 +000050 peer->sack_generation = 0;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 /* From 6.3.1 RTO Calculation:
53 *
54 * C1) Until an RTT measurement has been made for a packet sent to the
55 * given destination transport address, set RTO to the protocol
56 * parameter 'RTO.Initial'.
57 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +000058 peer->rto = msecs_to_jiffies(net->sctp.rto_initial);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Thomas Gleixner8b0e1952016-12-25 12:30:41 +010060 peer->last_time_heard = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 peer->last_time_ecne_reduced = jiffies;
62
Frank Filz52ccb8e2005-12-22 11:36:46 -080063 peer->param_flags = SPP_HB_DISABLE |
64 SPP_PMTUD_ENABLE |
65 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 /* Initialize the default path max_retrans. */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +000068 peer->pathmaxrxt = net->sctp.max_retrans_path;
69 peer->pf_retrans = net->sctp.pf_retrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 INIT_LIST_HEAD(&peer->transmitted);
72 INIT_LIST_HEAD(&peer->send_ready);
73 INIT_LIST_HEAD(&peer->transports);
74
Kees Cook9c3b5752017-10-24 01:45:31 -070075 timer_setup(&peer->T3_rtx_timer, sctp_generate_t3_rtx_event, 0);
76 timer_setup(&peer->hb_timer, sctp_generate_heartbeat_event, 0);
77 timer_setup(&peer->reconf_timer, sctp_generate_reconf_event, 0);
Xin Long92548ec2021-06-22 14:04:51 -040078 timer_setup(&peer->probe_timer, sctp_generate_probe_event, 0);
Kees Cook9c3b5752017-10-24 01:45:31 -070079 timer_setup(&peer->proto_unreach_timer,
80 sctp_generate_proto_unreach_event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Sridhar Samudralaad8fec12006-07-21 14:48:50 -070082 /* Initialize the 64-bit random nonce sent with heartbeat. */
83 get_random_bytes(&peer->hb_nonce, sizeof(peer->hb_nonce));
84
Reshetova, Elenaa4b2b582017-07-04 15:53:27 +030085 refcount_set(&peer->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 return peer;
88}
89
90/* Allocate and initialize a new transport. */
Eric W. Biederman89bf3452012-08-07 07:26:14 +000091struct sctp_transport *sctp_transport_new(struct net *net,
92 const union sctp_addr *addr,
Al Virodd0fc662005-10-07 07:46:04 +010093 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +090095 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Daniel Borkmann939cfa72013-06-17 11:40:04 +020097 transport = kzalloc(sizeof(*transport), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (!transport)
99 goto fail;
100
Eric W. Biederman89bf3452012-08-07 07:26:14 +0000101 if (!sctp_transport_init(net, transport, addr, gfp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 goto fail_init;
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 SCTP_DBG_OBJCNT_INC(transport);
105
106 return transport;
107
108fail_init:
109 kfree(transport);
110
111fail:
112 return NULL;
113}
114
115/* This transport is no longer needed. Free up if possible, or
116 * delay until it last reference count.
117 */
118void sctp_transport_free(struct sctp_transport *transport)
119{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /* Try to delete the heartbeat timer. */
121 if (del_timer(&transport->hb_timer))
122 sctp_transport_put(transport);
123
124 /* Delete the T3_rtx timer if it's active.
125 * There is no point in not doing this now and letting
126 * structure hang around in memory since we know
Christophe JAILLET5112cf52020-11-22 19:07:04 +0100127 * the transport is going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
Ying Xue25cc4ae2013-02-03 20:32:57 +0000129 if (del_timer(&transport->T3_rtx_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 sctp_transport_put(transport);
131
Xin Long7b9438d2017-01-18 00:44:43 +0800132 if (del_timer(&transport->reconf_timer))
133 sctp_transport_put(transport);
134
Xin Long92548ec2021-06-22 14:04:51 -0400135 if (del_timer(&transport->probe_timer))
136 sctp_transport_put(transport);
137
Wei Yongjun55fa0cf2010-05-09 16:56:07 +0000138 /* Delete the ICMP proto unreachable timer if it's active. */
Ying Xue25cc4ae2013-02-03 20:32:57 +0000139 if (del_timer(&transport->proto_unreach_timer))
Xin Long057a10f2020-11-14 13:22:53 +0800140 sctp_transport_put(transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 sctp_transport_put(transport);
143}
144
Thomas Graf45122ca262012-12-06 09:25:05 +0000145static void sctp_transport_destroy_rcu(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Thomas Graf45122ca262012-12-06 09:25:05 +0000147 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Thomas Graf45122ca262012-12-06 09:25:05 +0000149 transport = container_of(head, struct sctp_transport, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 dst_release(transport->dst);
152 kfree(transport);
153 SCTP_DBG_OBJCNT_DEC(transport);
154}
155
Thomas Graf45122ca262012-12-06 09:25:05 +0000156/* Destroy the transport data structure.
157 * Assumes there are no more users of this structure.
158 */
159static void sctp_transport_destroy(struct sctp_transport *transport)
160{
Reshetova, Elenaa4b2b582017-07-04 15:53:27 +0300161 if (unlikely(refcount_read(&transport->refcnt))) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200162 WARN(1, "Attempt to destroy undead transport %p!\n", transport);
163 return;
164 }
Thomas Graf45122ca262012-12-06 09:25:05 +0000165
Daniel Borkmann8c986532013-02-01 04:37:43 +0000166 sctp_packet_free(&transport->packet);
167
168 if (transport->asoc)
169 sctp_association_put(transport->asoc);
Daniel Borkmann771085d2013-08-09 16:25:21 +0200170
171 call_rcu(&transport->rcu, sctp_transport_destroy_rcu);
Thomas Graf45122ca262012-12-06 09:25:05 +0000172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/* Start T3_rtx timer if it is not already running and update the heartbeat
175 * timer. This routine is called every time a DATA chunk is sent.
176 */
Marcelo Ricardo Leitnerba6f5e32016-04-06 15:15:19 -0300177void sctp_transport_reset_t3_rtx(struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 /* RFC 2960 6.3.2 Retransmission Timer Rules
180 *
181 * R1) Every time a DATA chunk is sent to any address(including a
182 * retransmission), if the T3-rtx timer of that address is not running
183 * start it running so that it will expire after the RTO of that
184 * address.
185 */
186
Vlad Yasevichd9efc222010-04-30 22:41:09 -0400187 if (!timer_pending(&transport->T3_rtx_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (!mod_timer(&transport->T3_rtx_timer,
189 jiffies + transport->rto))
190 sctp_transport_hold(transport);
Marcelo Ricardo Leitnerba6f5e32016-04-06 15:15:19 -0300191}
192
193void sctp_transport_reset_hb_timer(struct sctp_transport *transport)
194{
195 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /* When a data chunk is sent, reset the heartbeat interval. */
Marcelo Ricardo Leitnerba6f5e32016-04-06 15:15:19 -0300198 expires = jiffies + sctp_transport_timeout(transport);
Maciej Kwieciend1f20c02019-02-22 09:45:26 +0100199 if ((time_before(transport->hb_timer.expires, expires) ||
200 !timer_pending(&transport->hb_timer)) &&
Marcelo Ricardo Leitnerba6f5e32016-04-06 15:15:19 -0300201 !mod_timer(&transport->hb_timer,
202 expires + prandom_u32_max(transport->rto)))
203 sctp_transport_hold(transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Xin Long7b9438d2017-01-18 00:44:43 +0800206void sctp_transport_reset_reconf_timer(struct sctp_transport *transport)
207{
208 if (!timer_pending(&transport->reconf_timer))
209 if (!mod_timer(&transport->reconf_timer,
210 jiffies + transport->rto))
211 sctp_transport_hold(transport);
212}
213
Xin Long92548ec2021-06-22 14:04:51 -0400214void sctp_transport_reset_probe_timer(struct sctp_transport *transport)
215{
216 int scale = 1;
217
218 if (timer_pending(&transport->probe_timer))
219 return;
220 if (transport->pl.state == SCTP_PL_COMPLETE &&
221 transport->pl.probe_count == 1)
222 scale = 30; /* works as PMTU_RAISE_TIMER */
223 if (!mod_timer(&transport->probe_timer,
224 jiffies + transport->probe_interval * scale))
225 sctp_transport_hold(transport);
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/* This transport has been assigned to an association.
229 * Initialize fields from the association or from the sock itself.
230 * Register the reference count in the association.
231 */
232void sctp_transport_set_owner(struct sctp_transport *transport,
233 struct sctp_association *asoc)
234{
235 transport->asoc = asoc;
236 sctp_association_hold(asoc);
237}
238
239/* Initialize the pmtu of a transport. */
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000240void sctp_transport_pmtu(struct sctp_transport *transport, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Vlad Yasevichda0420b2011-04-26 21:54:17 +0000242 /* If we don't have a fresh route, look one up */
David S. Millerf5b0a872012-07-19 12:31:33 -0700243 if (!transport->dst || transport->dst->obsolete) {
Julian Anastasovc86a7732017-02-06 23:14:13 +0200244 sctp_transport_dst_release(transport);
Vlad Yasevichda0420b2011-04-26 21:54:17 +0000245 transport->af_specific->get_dst(transport, &transport->saddr,
David S. Miller8663c932011-05-06 16:32:47 -0700246 &transport->fl, sk);
Vlad Yasevichda0420b2011-04-26 21:54:17 +0000247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300249 if (transport->param_flags & SPP_PMTUD_DISABLE) {
250 struct sctp_association *asoc = transport->asoc;
251
252 if (!transport->pathmtu && asoc && asoc->pathmtu)
253 transport->pathmtu = asoc->pathmtu;
254 if (transport->pathmtu)
255 return;
256 }
257
Marcelo Ricardo Leitner6ff0f872018-04-26 16:58:57 -0300258 if (transport->dst)
259 transport->pathmtu = sctp_dst_mtu(transport->dst);
260 else
Frank Filz52ccb8e2005-12-22 11:36:46 -0800261 transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200264bool sctp_transport_update_pmtu(struct sctp_transport *t, u32 pmtu)
Vlad Yasevichc910b472007-06-07 13:47:03 -0400265{
Xin Long3ebfdf02017-04-04 13:39:55 +0800266 struct dst_entry *dst = sctp_transport_dst_check(t);
Xin Longd7ab5cd2018-09-20 17:27:28 +0800267 struct sock *sk = t->asoc->base.sk;
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200268 bool change = true;
Vlad Yasevichc910b472007-06-07 13:47:03 -0400269
270 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200271 pr_warn_ratelimited("%s: Reported pmtu %d too low, using default minimum of %d\n",
272 __func__, pmtu, SCTP_DEFAULT_MINSEGMENT);
273 /* Use default minimum segment instead */
274 pmtu = SCTP_DEFAULT_MINSEGMENT;
Vlad Yasevichc910b472007-06-07 13:47:03 -0400275 }
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200276 pmtu = SCTP_TRUNC4(pmtu);
Vlad Yasevichc910b472007-06-07 13:47:03 -0400277
David S. Miller02f3d4c2012-07-16 03:57:14 -0700278 if (dst) {
Xin Longd7ab5cd2018-09-20 17:27:28 +0800279 struct sctp_pf *pf = sctp_get_pf_specific(dst->ops->family);
280 union sctp_addr addr;
281
282 pf->af->from_sk(&addr, sk);
283 pf->to_sk_daddr(&t->ipaddr, sk);
Hangbin Liubd085ef2019-12-22 10:51:09 +0800284 dst->ops->update_pmtu(dst, sk, NULL, pmtu, true);
Xin Longd7ab5cd2018-09-20 17:27:28 +0800285 pf->to_sk_daddr(&addr, sk);
286
David S. Miller02f3d4c2012-07-16 03:57:14 -0700287 dst = sctp_transport_dst_check(t);
David S. Miller02f3d4c2012-07-16 03:57:14 -0700288 }
Xin Long3ebfdf02017-04-04 13:39:55 +0800289
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200290 if (!dst) {
Xin Longd7ab5cd2018-09-20 17:27:28 +0800291 t->af_specific->get_dst(t, &t->saddr, &t->fl, sk);
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200292 dst = t->dst;
293 }
294
295 if (dst) {
296 /* Re-fetch, as under layers may have a higher minimum size */
Xin Longa6592542018-07-03 16:30:47 +0800297 pmtu = sctp_dst_mtu(dst);
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200298 change = t->pathmtu != pmtu;
299 }
300 t->pathmtu = pmtu;
301
302 return change;
Vlad Yasevichc910b472007-06-07 13:47:03 -0400303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/* Caches the dst entry and source address for a transport's destination
306 * address.
307 */
308void sctp_transport_route(struct sctp_transport *transport,
309 union sctp_addr *saddr, struct sctp_sock *opt)
310{
311 struct sctp_association *asoc = transport->asoc;
312 struct sctp_af *af = transport->af_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300314 sctp_transport_dst_release(transport);
David S. Miller8663c932011-05-06 16:32:47 -0700315 af->get_dst(transport, saddr, &transport->fl, sctp_opt2sk(opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 if (saddr)
318 memcpy(&transport->saddr, saddr, sizeof(union sctp_addr));
319 else
David S. Miller8663c932011-05-06 16:32:47 -0700320 af->get_saddr(opt, transport, &transport->fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300322 sctp_transport_pmtu(transport, sctp_opt2sk(opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300324 /* Initialize sk->sk_rcv_saddr, if the transport is the
325 * association's active path for getsockname().
326 */
327 if (transport->dst && asoc &&
328 (!asoc->peer.primary_path || transport == asoc->peer.active_path))
329 opt->pf->to_sk_saddr(&transport->saddr, asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332/* Hold a reference to a transport. */
Xin Long1eed6772016-01-22 01:49:07 +0800333int sctp_transport_hold(struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Reshetova, Elenaa4b2b582017-07-04 15:53:27 +0300335 return refcount_inc_not_zero(&transport->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338/* Release a reference to a transport and clean up
339 * if there are no more references.
340 */
341void sctp_transport_put(struct sctp_transport *transport)
342{
Reshetova, Elenaa4b2b582017-07-04 15:53:27 +0300343 if (refcount_dec_and_test(&transport->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 sctp_transport_destroy(transport);
345}
346
347/* Update transport's RTO based on the newly calculated RTT. */
348void sctp_transport_update_rto(struct sctp_transport *tp, __u32 rtt)
349{
Daniel Borkmannbb333812013-06-28 19:49:40 +0200350 if (unlikely(!tp->rto_pending))
351 /* We should not be doing any RTO updates unless rto_pending is set. */
352 pr_debug("%s: rto_pending not set on transport %p!\n", __func__, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (tp->rttvar || tp->srtt) {
Xin Long4e7696d2019-12-09 13:45:18 +0800355 struct net *net = tp->asoc->base.net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /* 6.3.1 C3) When a new RTT measurement R' is made, set
357 * RTTVAR <- (1 - RTO.Beta) * RTTVAR + RTO.Beta * |SRTT - R'|
358 * SRTT <- (1 - RTO.Alpha) * SRTT + RTO.Alpha * R'
359 */
360
361 /* Note: The above algorithm has been rewritten to
362 * express rto_beta and rto_alpha as inverse powers
363 * of two.
364 * For example, assuming the default value of RTO.Alpha of
365 * 1/8, rto_alpha would be expressed as 3.
366 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000367 tp->rttvar = tp->rttvar - (tp->rttvar >> net->sctp.rto_beta)
Andrew Morton79211c82015-11-09 14:58:13 -0800368 + (((__u32)abs((__s64)tp->srtt - (__s64)rtt)) >> net->sctp.rto_beta);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000369 tp->srtt = tp->srtt - (tp->srtt >> net->sctp.rto_alpha)
370 + (rtt >> net->sctp.rto_alpha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 } else {
372 /* 6.3.1 C2) When the first RTT measurement R is made, set
373 * SRTT <- R, RTTVAR <- R/2.
374 */
375 tp->srtt = rtt;
376 tp->rttvar = rtt >> 1;
377 }
378
379 /* 6.3.1 G1) Whenever RTTVAR is computed, if RTTVAR = 0, then
380 * adjust RTTVAR <- G, where G is the CLOCK GRANULARITY.
381 */
382 if (tp->rttvar == 0)
383 tp->rttvar = SCTP_CLOCK_GRANULARITY;
384
385 /* 6.3.1 C3) After the computation, update RTO <- SRTT + 4 * RTTVAR. */
386 tp->rto = tp->srtt + (tp->rttvar << 2);
387
388 /* 6.3.1 C6) Whenever RTO is computed, if it is less than RTO.Min
389 * seconds then it is rounded up to RTO.Min seconds.
390 */
391 if (tp->rto < tp->asoc->rto_min)
392 tp->rto = tp->asoc->rto_min;
393
394 /* 6.3.1 C7) A maximum value may be placed on RTO provided it is
395 * at least RTO.max seconds.
396 */
397 if (tp->rto > tp->asoc->rto_max)
398 tp->rto = tp->asoc->rto_max;
399
Michele Baldessari196d6752012-12-01 04:49:42 +0000400 sctp_max_rto(tp->asoc, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 tp->rtt = rtt;
402
403 /* Reset rto_pending so that a new RTT measurement is started when a
404 * new data chunk is sent.
405 */
406 tp->rto_pending = 0;
407
Daniel Borkmannbb333812013-06-28 19:49:40 +0200408 pr_debug("%s: transport:%p, rtt:%d, srtt:%d rttvar:%d, rto:%ld\n",
409 __func__, tp, rtt, tp->srtt, tp->rttvar, tp->rto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412/* This routine updates the transport's cwnd and partial_bytes_acked
413 * parameters based on the bytes acked in the received SACK.
414 */
415void sctp_transport_raise_cwnd(struct sctp_transport *transport,
416 __u32 sack_ctsn, __u32 bytes_acked)
417{
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400418 struct sctp_association *asoc = transport->asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 __u32 cwnd, ssthresh, flight_size, pba, pmtu;
420
421 cwnd = transport->cwnd;
422 flight_size = transport->flight_size;
423
Vlad Yasevicha6465232008-06-04 12:38:43 -0700424 /* See if we need to exit Fast Recovery first */
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400425 if (asoc->fast_recovery &&
426 TSN_lte(asoc->fast_recovery_exit, sack_ctsn))
427 asoc->fast_recovery = 0;
Vlad Yasevicha6465232008-06-04 12:38:43 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 ssthresh = transport->ssthresh;
430 pba = transport->partial_bytes_acked;
Frank Filz52ccb8e2005-12-22 11:36:46 -0800431 pmtu = transport->asoc->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (cwnd <= ssthresh) {
Vlad Yasevicha6465232008-06-04 12:38:43 -0700434 /* RFC 4960 7.2.1
435 * o When cwnd is less than or equal to ssthresh, an SCTP
436 * endpoint MUST use the slow-start algorithm to increase
437 * cwnd only if the current congestion window is being fully
438 * utilized, an incoming SACK advances the Cumulative TSN
439 * Ack Point, and the data sender is not in Fast Recovery.
440 * Only when these three conditions are met can the cwnd be
441 * increased; otherwise, the cwnd MUST not be increased.
442 * If these conditions are met, then cwnd MUST be increased
443 * by, at most, the lesser of 1) the total size of the
444 * previously outstanding DATA chunk(s) acknowledged, and
445 * 2) the destination's path MTU. This upper bound protects
446 * against the ACK-Splitting attack outlined in [SAVAGE99].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400448 if (asoc->fast_recovery)
Vlad Yasevicha6465232008-06-04 12:38:43 -0700449 return;
450
Marcelo Ricardo Leitner4ccbd0b2017-06-23 19:59:35 -0300451 /* The appropriate cwnd increase algorithm is performed
452 * if, and only if the congestion window is being fully
453 * utilized. Note that RFC4960 Errata 3.22 removed the
454 * other condition on ctsn moving.
455 */
456 if (flight_size < cwnd)
457 return;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (bytes_acked > pmtu)
460 cwnd += pmtu;
461 else
462 cwnd += bytes_acked;
Daniel Borkmannbb333812013-06-28 19:49:40 +0200463
464 pr_debug("%s: slow start: transport:%p, bytes_acked:%d, "
465 "cwnd:%d, ssthresh:%d, flight_size:%d, pba:%d\n",
466 __func__, transport, bytes_acked, cwnd, ssthresh,
467 flight_size, pba);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 } else {
469 /* RFC 2960 7.2.2 Whenever cwnd is greater than ssthresh,
Marcelo Ricardo Leitnere56f777a2017-06-23 19:59:34 -0300470 * upon each SACK arrival, increase partial_bytes_acked
471 * by the total number of bytes of all new chunks
472 * acknowledged in that SACK including chunks
473 * acknowledged by the new Cumulative TSN Ack and by Gap
474 * Ack Blocks. (updated by RFC4960 Errata 3.22)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *
Marcelo Ricardo Leitner4ccbd0b2017-06-23 19:59:35 -0300476 * When partial_bytes_acked is greater than cwnd and
477 * before the arrival of the SACK the sender had less
478 * bytes of data outstanding than cwnd (i.e., before
479 * arrival of the SACK, flightsize was less than cwnd),
480 * reset partial_bytes_acked to cwnd. (RFC 4960 Errata
481 * 3.26)
482 *
Marcelo Ricardo Leitnerd0b53f42017-06-23 19:59:33 -0300483 * When partial_bytes_acked is equal to or greater than
484 * cwnd and before the arrival of the SACK the sender
485 * had cwnd or more bytes of data outstanding (i.e.,
486 * before arrival of the SACK, flightsize was greater
487 * than or equal to cwnd), partial_bytes_acked is reset
488 * to (partial_bytes_acked - cwnd). Next, cwnd is
489 * increased by MTU. (RFC 4960 Errata 3.12)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 */
491 pba += bytes_acked;
Marcelo Ricardo Leitner4ccbd0b2017-06-23 19:59:35 -0300492 if (pba > cwnd && flight_size < cwnd)
493 pba = cwnd;
494 if (pba >= cwnd && flight_size >= cwnd) {
Marcelo Ricardo Leitnerd0b53f42017-06-23 19:59:33 -0300495 pba = pba - cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 cwnd += pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200498
499 pr_debug("%s: congestion avoidance: transport:%p, "
500 "bytes_acked:%d, cwnd:%d, ssthresh:%d, "
501 "flight_size:%d, pba:%d\n", __func__,
502 transport, bytes_acked, cwnd, ssthresh,
503 flight_size, pba);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
506 transport->cwnd = cwnd;
507 transport->partial_bytes_acked = pba;
508}
509
510/* This routine is used to lower the transport's cwnd when congestion is
511 * detected.
512 */
513void sctp_transport_lower_cwnd(struct sctp_transport *transport,
Xin Long233e7932017-08-05 19:59:51 +0800514 enum sctp_lower_cwnd reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400516 struct sctp_association *asoc = transport->asoc;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 switch (reason) {
519 case SCTP_LOWER_CWND_T3_RTX:
520 /* RFC 2960 Section 7.2.3, sctpimpguide
521 * When the T3-rtx timer expires on an address, SCTP should
522 * perform slow start by:
523 * ssthresh = max(cwnd/2, 4*MTU)
524 * cwnd = 1*MTU
525 * partial_bytes_acked = 0
526 */
527 transport->ssthresh = max(transport->cwnd/2,
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400528 4*asoc->pathmtu);
529 transport->cwnd = asoc->pathmtu;
Vlad Yasevich33ce8282009-09-04 18:20:58 -0400530
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400531 /* T3-rtx also clears fast recovery */
532 asoc->fast_recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 break;
534
535 case SCTP_LOWER_CWND_FAST_RTX:
536 /* RFC 2960 7.2.4 Adjust the ssthresh and cwnd of the
537 * destination address(es) to which the missing DATA chunks
538 * were last sent, according to the formula described in
539 * Section 7.2.3.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900540 *
541 * RFC 2960 7.2.3, sctpimpguide Upon detection of packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * losses from SACK (see Section 7.2.4), An endpoint
543 * should do the following:
544 * ssthresh = max(cwnd/2, 4*MTU)
545 * cwnd = ssthresh
546 * partial_bytes_acked = 0
547 */
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400548 if (asoc->fast_recovery)
Vlad Yasevicha6465232008-06-04 12:38:43 -0700549 return;
550
551 /* Mark Fast recovery */
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400552 asoc->fast_recovery = 1;
553 asoc->fast_recovery_exit = asoc->next_tsn - 1;
Vlad Yasevicha6465232008-06-04 12:38:43 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 transport->ssthresh = max(transport->cwnd/2,
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400556 4*asoc->pathmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 transport->cwnd = transport->ssthresh;
558 break;
559
560 case SCTP_LOWER_CWND_ECNE:
561 /* RFC 2481 Section 6.1.2.
562 * If the sender receives an ECN-Echo ACK packet
563 * then the sender knows that congestion was encountered in the
564 * network on the path from the sender to the receiver. The
565 * indication of congestion should be treated just as a
566 * congestion loss in non-ECN Capable TCP. That is, the TCP
567 * source halves the congestion window "cwnd" and reduces the
568 * slow start threshold "ssthresh".
569 * A critical condition is that TCP does not react to
570 * congestion indications more than once every window of
571 * data (or more loosely more than once every round-trip time).
572 */
Wei Yongjunf61f6f82009-03-02 09:46:13 +0000573 if (time_after(jiffies, transport->last_time_ecne_reduced +
574 transport->rtt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 transport->ssthresh = max(transport->cwnd/2,
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400576 4*asoc->pathmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 transport->cwnd = transport->ssthresh;
578 transport->last_time_ecne_reduced = jiffies;
579 }
580 break;
581
582 case SCTP_LOWER_CWND_INACTIVE:
583 /* RFC 2960 Section 7.2.1, sctpimpguide
584 * When the endpoint does not transmit data on a given
585 * transport address, the cwnd of the transport address
586 * should be adjusted to max(cwnd/2, 4*MTU) per RTO.
587 * NOTE: Although the draft recommends that this check needs
588 * to be done every RTO interval, we do it every hearbeat
589 * interval.
590 */
Vlad Yasevich245cba72009-11-23 15:53:58 -0500591 transport->cwnd = max(transport->cwnd/2,
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400592 4*asoc->pathmtu);
Marcelo Ricardo Leitnera02d0362017-06-23 19:59:36 -0300593 /* RFC 4960 Errata 3.27.2: also adjust sshthresh */
594 transport->ssthresh = transport->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 transport->partial_bytes_acked = 0;
Daniel Borkmannbb333812013-06-28 19:49:40 +0200599
600 pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d\n",
601 __func__, transport, reason, transport->cwnd,
602 transport->ssthresh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Vlad Yasevich46d5a802009-11-23 15:54:00 -0500605/* Apply Max.Burst limit to the congestion window:
606 * sctpimpguide-05 2.14.2
607 * D) When the time comes for the sender to
608 * transmit new DATA chunks, the protocol parameter Max.Burst MUST
609 * first be applied to limit how many new DATA chunks may be sent.
610 * The limit is applied by adjusting cwnd as follows:
611 * if ((flightsize+ Max.Burst * MTU) < cwnd)
612 * cwnd = flightsize + Max.Burst * MTU
613 */
614
615void sctp_transport_burst_limited(struct sctp_transport *t)
616{
617 struct sctp_association *asoc = t->asoc;
618 u32 old_cwnd = t->cwnd;
619 u32 max_burst_bytes;
620
wangweidong78ac8142013-12-04 17:32:39 +0800621 if (t->burst_limited || asoc->max_burst == 0)
Vlad Yasevich46d5a802009-11-23 15:54:00 -0500622 return;
623
624 max_burst_bytes = t->flight_size + (asoc->max_burst * asoc->pathmtu);
625 if (max_burst_bytes < old_cwnd) {
626 t->cwnd = max_burst_bytes;
627 t->burst_limited = old_cwnd;
628 }
629}
630
631/* Restore the old cwnd congestion window, after the burst had it's
632 * desired effect.
633 */
634void sctp_transport_burst_reset(struct sctp_transport *t)
635{
636 if (t->burst_limited) {
637 t->cwnd = t->burst_limited;
638 t->burst_limited = 0;
639 }
640}
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/* What is the next timeout value for this transport? */
Daniel Borkmann8f610592014-06-30 13:52:08 +0200643unsigned long sctp_transport_timeout(struct sctp_transport *trans)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Daniel Borkmann8f610592014-06-30 13:52:08 +0200645 /* RTO + timer slack +/- 50% of RTO */
Marcelo Ricardo Leitnerba6f5e32016-04-06 15:15:19 -0300646 unsigned long timeout = trans->rto >> 1;
Daniel Borkmann8f610592014-06-30 13:52:08 +0200647
648 if (trans->state != SCTP_UNCONFIRMED &&
649 trans->state != SCTP_PF)
650 timeout += trans->hbinterval;
651
Xin Long1d88ba12018-06-05 12:16:58 +0800652 return max_t(unsigned long, timeout, HZ / 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
Vlad Yasevich749bf922007-03-19 17:02:30 -0700654
655/* Reset transport variables to their initial values */
656void sctp_transport_reset(struct sctp_transport *t)
657{
658 struct sctp_association *asoc = t->asoc;
659
660 /* RFC 2960 (bis), Section 5.2.4
661 * All the congestion control parameters (e.g., cwnd, ssthresh)
662 * related to this peer MUST be reset to their initial values
663 * (see Section 6.2.1)
664 */
665 t->cwnd = min(4*asoc->pathmtu, max_t(__u32, 2*asoc->pathmtu, 4380));
Vlad Yasevich46d5a802009-11-23 15:54:00 -0500666 t->burst_limited = 0;
Vlad Yasevich289f4242007-03-22 12:26:25 -0700667 t->ssthresh = asoc->peer.i.a_rwnd;
Andrei Pelinescu-Onciul5fdd4ba2009-11-29 00:14:02 -0800668 t->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000669 sctp_max_rto(asoc, t);
Vlad Yasevich749bf922007-03-19 17:02:30 -0700670 t->rtt = 0;
671 t->srtt = 0;
672 t->rttvar = 0;
673
Masahiro Yamadab564d622017-02-27 14:29:06 -0800674 /* Reset these additional variables so that we have a clean slate. */
Vlad Yasevich749bf922007-03-19 17:02:30 -0700675 t->partial_bytes_acked = 0;
676 t->flight_size = 0;
677 t->error_count = 0;
678 t->rto_pending = 0;
Vlad Yasevichfaee47c2009-02-13 08:33:43 +0000679 t->hb_sent = 0;
Vlad Yasevich749bf922007-03-19 17:02:30 -0700680
681 /* Initialize the state information for SFR-CACC */
682 t->cacc.changeover_active = 0;
683 t->cacc.cycling_changeover = 0;
684 t->cacc.next_tsn_at_change = 0;
685 t->cacc.cacc_saw_newack = 0;
686}
Michio Hondaddc4bbe2011-06-17 11:03:23 +0900687
688/* Schedule retransmission on the given transport */
689void sctp_transport_immediate_rtx(struct sctp_transport *t)
690{
691 /* Stop pending T3_rtx_timer */
Ying Xue25cc4ae2013-02-03 20:32:57 +0000692 if (del_timer(&t->T3_rtx_timer))
Michio Hondaddc4bbe2011-06-17 11:03:23 +0900693 sctp_transport_put(t);
Ying Xue25cc4ae2013-02-03 20:32:57 +0000694
Michio Hondaddc4bbe2011-06-17 11:03:23 +0900695 sctp_retransmit(&t->asoc->outqueue, t, SCTP_RTXR_T3_RTX);
696 if (!timer_pending(&t->T3_rtx_timer)) {
697 if (!mod_timer(&t->T3_rtx_timer, jiffies + t->rto))
698 sctp_transport_hold(t);
699 }
Michio Hondaddc4bbe2011-06-17 11:03:23 +0900700}
Julian Anastasovc86a7732017-02-06 23:14:13 +0200701
702/* Drop dst */
703void sctp_transport_dst_release(struct sctp_transport *t)
704{
705 dst_release(t->dst);
706 t->dst = NULL;
707 t->dst_pending_confirm = 0;
708}
709
710/* Schedule neighbour confirm */
711void sctp_transport_dst_confirm(struct sctp_transport *t)
712{
713 t->dst_pending_confirm = 1;
714}