blob: 806af58f43758546af7fc9ebdfee5250d70a1caf [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 *
11 * This module provides the abstraction for an SCTP tranport representing
12 * 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);
78 timer_setup(&peer->proto_unreach_timer,
79 sctp_generate_proto_unreach_event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Sridhar Samudralaad8fec12006-07-21 14:48:50 -070081 /* Initialize the 64-bit random nonce sent with heartbeat. */
82 get_random_bytes(&peer->hb_nonce, sizeof(peer->hb_nonce));
83
Reshetova, Elenaa4b2b582017-07-04 15:53:27 +030084 refcount_set(&peer->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 return peer;
87}
88
89/* Allocate and initialize a new transport. */
Eric W. Biederman89bf3452012-08-07 07:26:14 +000090struct sctp_transport *sctp_transport_new(struct net *net,
91 const union sctp_addr *addr,
Al Virodd0fc662005-10-07 07:46:04 +010092 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +090094 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Daniel Borkmann939cfa72013-06-17 11:40:04 +020096 transport = kzalloc(sizeof(*transport), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (!transport)
98 goto fail;
99
Eric W. Biederman89bf3452012-08-07 07:26:14 +0000100 if (!sctp_transport_init(net, transport, addr, gfp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 goto fail_init;
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 SCTP_DBG_OBJCNT_INC(transport);
104
105 return transport;
106
107fail_init:
108 kfree(transport);
109
110fail:
111 return NULL;
112}
113
114/* This transport is no longer needed. Free up if possible, or
115 * delay until it last reference count.
116 */
117void sctp_transport_free(struct sctp_transport *transport)
118{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 /* Try to delete the heartbeat timer. */
120 if (del_timer(&transport->hb_timer))
121 sctp_transport_put(transport);
122
123 /* Delete the T3_rtx timer if it's active.
124 * There is no point in not doing this now and letting
125 * structure hang around in memory since we know
126 * the tranport is going away.
127 */
Ying Xue25cc4ae2013-02-03 20:32:57 +0000128 if (del_timer(&transport->T3_rtx_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 sctp_transport_put(transport);
130
Xin Long7b9438d2017-01-18 00:44:43 +0800131 if (del_timer(&transport->reconf_timer))
132 sctp_transport_put(transport);
133
Wei Yongjun55fa0cf2010-05-09 16:56:07 +0000134 /* Delete the ICMP proto unreachable timer if it's active. */
Ying Xue25cc4ae2013-02-03 20:32:57 +0000135 if (del_timer(&transport->proto_unreach_timer))
Wei Yongjun55fa0cf2010-05-09 16:56:07 +0000136 sctp_association_put(transport->asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 sctp_transport_put(transport);
139}
140
Thomas Graf45122ca262012-12-06 09:25:05 +0000141static void sctp_transport_destroy_rcu(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Thomas Graf45122ca262012-12-06 09:25:05 +0000143 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Thomas Graf45122ca262012-12-06 09:25:05 +0000145 transport = container_of(head, struct sctp_transport, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 dst_release(transport->dst);
148 kfree(transport);
149 SCTP_DBG_OBJCNT_DEC(transport);
150}
151
Thomas Graf45122ca262012-12-06 09:25:05 +0000152/* Destroy the transport data structure.
153 * Assumes there are no more users of this structure.
154 */
155static void sctp_transport_destroy(struct sctp_transport *transport)
156{
Reshetova, Elenaa4b2b582017-07-04 15:53:27 +0300157 if (unlikely(refcount_read(&transport->refcnt))) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200158 WARN(1, "Attempt to destroy undead transport %p!\n", transport);
159 return;
160 }
Thomas Graf45122ca262012-12-06 09:25:05 +0000161
Daniel Borkmann8c986532013-02-01 04:37:43 +0000162 sctp_packet_free(&transport->packet);
163
164 if (transport->asoc)
165 sctp_association_put(transport->asoc);
Daniel Borkmann771085d2013-08-09 16:25:21 +0200166
167 call_rcu(&transport->rcu, sctp_transport_destroy_rcu);
Thomas Graf45122ca262012-12-06 09:25:05 +0000168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/* Start T3_rtx timer if it is not already running and update the heartbeat
171 * timer. This routine is called every time a DATA chunk is sent.
172 */
Marcelo Ricardo Leitnerba6f5e32016-04-06 15:15:19 -0300173void sctp_transport_reset_t3_rtx(struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 /* RFC 2960 6.3.2 Retransmission Timer Rules
176 *
177 * R1) Every time a DATA chunk is sent to any address(including a
178 * retransmission), if the T3-rtx timer of that address is not running
179 * start it running so that it will expire after the RTO of that
180 * address.
181 */
182
Vlad Yasevichd9efc222010-04-30 22:41:09 -0400183 if (!timer_pending(&transport->T3_rtx_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (!mod_timer(&transport->T3_rtx_timer,
185 jiffies + transport->rto))
186 sctp_transport_hold(transport);
Marcelo Ricardo Leitnerba6f5e32016-04-06 15:15:19 -0300187}
188
189void sctp_transport_reset_hb_timer(struct sctp_transport *transport)
190{
191 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /* When a data chunk is sent, reset the heartbeat interval. */
Marcelo Ricardo Leitnerba6f5e32016-04-06 15:15:19 -0300194 expires = jiffies + sctp_transport_timeout(transport);
Maciej Kwieciend1f20c02019-02-22 09:45:26 +0100195 if ((time_before(transport->hb_timer.expires, expires) ||
196 !timer_pending(&transport->hb_timer)) &&
Marcelo Ricardo Leitnerba6f5e32016-04-06 15:15:19 -0300197 !mod_timer(&transport->hb_timer,
198 expires + prandom_u32_max(transport->rto)))
199 sctp_transport_hold(transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Xin Long7b9438d2017-01-18 00:44:43 +0800202void sctp_transport_reset_reconf_timer(struct sctp_transport *transport)
203{
204 if (!timer_pending(&transport->reconf_timer))
205 if (!mod_timer(&transport->reconf_timer,
206 jiffies + transport->rto))
207 sctp_transport_hold(transport);
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/* This transport has been assigned to an association.
211 * Initialize fields from the association or from the sock itself.
212 * Register the reference count in the association.
213 */
214void sctp_transport_set_owner(struct sctp_transport *transport,
215 struct sctp_association *asoc)
216{
217 transport->asoc = asoc;
218 sctp_association_hold(asoc);
219}
220
221/* Initialize the pmtu of a transport. */
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000222void sctp_transport_pmtu(struct sctp_transport *transport, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Vlad Yasevichda0420b2011-04-26 21:54:17 +0000224 /* If we don't have a fresh route, look one up */
David S. Millerf5b0a872012-07-19 12:31:33 -0700225 if (!transport->dst || transport->dst->obsolete) {
Julian Anastasovc86a7732017-02-06 23:14:13 +0200226 sctp_transport_dst_release(transport);
Vlad Yasevichda0420b2011-04-26 21:54:17 +0000227 transport->af_specific->get_dst(transport, &transport->saddr,
David S. Miller8663c932011-05-06 16:32:47 -0700228 &transport->fl, sk);
Vlad Yasevichda0420b2011-04-26 21:54:17 +0000229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300231 if (transport->param_flags & SPP_PMTUD_DISABLE) {
232 struct sctp_association *asoc = transport->asoc;
233
234 if (!transport->pathmtu && asoc && asoc->pathmtu)
235 transport->pathmtu = asoc->pathmtu;
236 if (transport->pathmtu)
237 return;
238 }
239
Marcelo Ricardo Leitner6ff0f872018-04-26 16:58:57 -0300240 if (transport->dst)
241 transport->pathmtu = sctp_dst_mtu(transport->dst);
242 else
Frank Filz52ccb8e2005-12-22 11:36:46 -0800243 transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200246bool sctp_transport_update_pmtu(struct sctp_transport *t, u32 pmtu)
Vlad Yasevichc910b472007-06-07 13:47:03 -0400247{
Xin Long3ebfdf02017-04-04 13:39:55 +0800248 struct dst_entry *dst = sctp_transport_dst_check(t);
Xin Longd7ab5cd2018-09-20 17:27:28 +0800249 struct sock *sk = t->asoc->base.sk;
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200250 bool change = true;
Vlad Yasevichc910b472007-06-07 13:47:03 -0400251
252 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200253 pr_warn_ratelimited("%s: Reported pmtu %d too low, using default minimum of %d\n",
254 __func__, pmtu, SCTP_DEFAULT_MINSEGMENT);
255 /* Use default minimum segment instead */
256 pmtu = SCTP_DEFAULT_MINSEGMENT;
Vlad Yasevichc910b472007-06-07 13:47:03 -0400257 }
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200258 pmtu = SCTP_TRUNC4(pmtu);
Vlad Yasevichc910b472007-06-07 13:47:03 -0400259
David S. Miller02f3d4c2012-07-16 03:57:14 -0700260 if (dst) {
Xin Longd7ab5cd2018-09-20 17:27:28 +0800261 struct sctp_pf *pf = sctp_get_pf_specific(dst->ops->family);
262 union sctp_addr addr;
263
264 pf->af->from_sk(&addr, sk);
265 pf->to_sk_daddr(&t->ipaddr, sk);
Hangbin Liubd085ef2019-12-22 10:51:09 +0800266 dst->ops->update_pmtu(dst, sk, NULL, pmtu, true);
Xin Longd7ab5cd2018-09-20 17:27:28 +0800267 pf->to_sk_daddr(&addr, sk);
268
David S. Miller02f3d4c2012-07-16 03:57:14 -0700269 dst = sctp_transport_dst_check(t);
David S. Miller02f3d4c2012-07-16 03:57:14 -0700270 }
Xin Long3ebfdf02017-04-04 13:39:55 +0800271
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200272 if (!dst) {
Xin Longd7ab5cd2018-09-20 17:27:28 +0800273 t->af_specific->get_dst(t, &t->saddr, &t->fl, sk);
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200274 dst = t->dst;
275 }
276
277 if (dst) {
278 /* Re-fetch, as under layers may have a higher minimum size */
Xin Longa6592542018-07-03 16:30:47 +0800279 pmtu = sctp_dst_mtu(dst);
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200280 change = t->pathmtu != pmtu;
281 }
282 t->pathmtu = pmtu;
283
284 return change;
Vlad Yasevichc910b472007-06-07 13:47:03 -0400285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/* Caches the dst entry and source address for a transport's destination
288 * address.
289 */
290void sctp_transport_route(struct sctp_transport *transport,
291 union sctp_addr *saddr, struct sctp_sock *opt)
292{
293 struct sctp_association *asoc = transport->asoc;
294 struct sctp_af *af = transport->af_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300296 sctp_transport_dst_release(transport);
David S. Miller8663c932011-05-06 16:32:47 -0700297 af->get_dst(transport, saddr, &transport->fl, sctp_opt2sk(opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 if (saddr)
300 memcpy(&transport->saddr, saddr, sizeof(union sctp_addr));
301 else
David S. Miller8663c932011-05-06 16:32:47 -0700302 af->get_saddr(opt, transport, &transport->fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300304 sctp_transport_pmtu(transport, sctp_opt2sk(opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300306 /* Initialize sk->sk_rcv_saddr, if the transport is the
307 * association's active path for getsockname().
308 */
309 if (transport->dst && asoc &&
310 (!asoc->peer.primary_path || transport == asoc->peer.active_path))
311 opt->pf->to_sk_saddr(&transport->saddr, asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314/* Hold a reference to a transport. */
Xin Long1eed6772016-01-22 01:49:07 +0800315int sctp_transport_hold(struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Reshetova, Elenaa4b2b582017-07-04 15:53:27 +0300317 return refcount_inc_not_zero(&transport->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320/* Release a reference to a transport and clean up
321 * if there are no more references.
322 */
323void sctp_transport_put(struct sctp_transport *transport)
324{
Reshetova, Elenaa4b2b582017-07-04 15:53:27 +0300325 if (refcount_dec_and_test(&transport->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 sctp_transport_destroy(transport);
327}
328
329/* Update transport's RTO based on the newly calculated RTT. */
330void sctp_transport_update_rto(struct sctp_transport *tp, __u32 rtt)
331{
Daniel Borkmannbb333812013-06-28 19:49:40 +0200332 if (unlikely(!tp->rto_pending))
333 /* We should not be doing any RTO updates unless rto_pending is set. */
334 pr_debug("%s: rto_pending not set on transport %p!\n", __func__, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 if (tp->rttvar || tp->srtt) {
Xin Long4e7696d2019-12-09 13:45:18 +0800337 struct net *net = tp->asoc->base.net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /* 6.3.1 C3) When a new RTT measurement R' is made, set
339 * RTTVAR <- (1 - RTO.Beta) * RTTVAR + RTO.Beta * |SRTT - R'|
340 * SRTT <- (1 - RTO.Alpha) * SRTT + RTO.Alpha * R'
341 */
342
343 /* Note: The above algorithm has been rewritten to
344 * express rto_beta and rto_alpha as inverse powers
345 * of two.
346 * For example, assuming the default value of RTO.Alpha of
347 * 1/8, rto_alpha would be expressed as 3.
348 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000349 tp->rttvar = tp->rttvar - (tp->rttvar >> net->sctp.rto_beta)
Andrew Morton79211c82015-11-09 14:58:13 -0800350 + (((__u32)abs((__s64)tp->srtt - (__s64)rtt)) >> net->sctp.rto_beta);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000351 tp->srtt = tp->srtt - (tp->srtt >> net->sctp.rto_alpha)
352 + (rtt >> net->sctp.rto_alpha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 } else {
354 /* 6.3.1 C2) When the first RTT measurement R is made, set
355 * SRTT <- R, RTTVAR <- R/2.
356 */
357 tp->srtt = rtt;
358 tp->rttvar = rtt >> 1;
359 }
360
361 /* 6.3.1 G1) Whenever RTTVAR is computed, if RTTVAR = 0, then
362 * adjust RTTVAR <- G, where G is the CLOCK GRANULARITY.
363 */
364 if (tp->rttvar == 0)
365 tp->rttvar = SCTP_CLOCK_GRANULARITY;
366
367 /* 6.3.1 C3) After the computation, update RTO <- SRTT + 4 * RTTVAR. */
368 tp->rto = tp->srtt + (tp->rttvar << 2);
369
370 /* 6.3.1 C6) Whenever RTO is computed, if it is less than RTO.Min
371 * seconds then it is rounded up to RTO.Min seconds.
372 */
373 if (tp->rto < tp->asoc->rto_min)
374 tp->rto = tp->asoc->rto_min;
375
376 /* 6.3.1 C7) A maximum value may be placed on RTO provided it is
377 * at least RTO.max seconds.
378 */
379 if (tp->rto > tp->asoc->rto_max)
380 tp->rto = tp->asoc->rto_max;
381
Michele Baldessari196d6752012-12-01 04:49:42 +0000382 sctp_max_rto(tp->asoc, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 tp->rtt = rtt;
384
385 /* Reset rto_pending so that a new RTT measurement is started when a
386 * new data chunk is sent.
387 */
388 tp->rto_pending = 0;
389
Daniel Borkmannbb333812013-06-28 19:49:40 +0200390 pr_debug("%s: transport:%p, rtt:%d, srtt:%d rttvar:%d, rto:%ld\n",
391 __func__, tp, rtt, tp->srtt, tp->rttvar, tp->rto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394/* This routine updates the transport's cwnd and partial_bytes_acked
395 * parameters based on the bytes acked in the received SACK.
396 */
397void sctp_transport_raise_cwnd(struct sctp_transport *transport,
398 __u32 sack_ctsn, __u32 bytes_acked)
399{
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400400 struct sctp_association *asoc = transport->asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 __u32 cwnd, ssthresh, flight_size, pba, pmtu;
402
403 cwnd = transport->cwnd;
404 flight_size = transport->flight_size;
405
Vlad Yasevicha6465232008-06-04 12:38:43 -0700406 /* See if we need to exit Fast Recovery first */
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400407 if (asoc->fast_recovery &&
408 TSN_lte(asoc->fast_recovery_exit, sack_ctsn))
409 asoc->fast_recovery = 0;
Vlad Yasevicha6465232008-06-04 12:38:43 -0700410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 ssthresh = transport->ssthresh;
412 pba = transport->partial_bytes_acked;
Frank Filz52ccb8e2005-12-22 11:36:46 -0800413 pmtu = transport->asoc->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 if (cwnd <= ssthresh) {
Vlad Yasevicha6465232008-06-04 12:38:43 -0700416 /* RFC 4960 7.2.1
417 * o When cwnd is less than or equal to ssthresh, an SCTP
418 * endpoint MUST use the slow-start algorithm to increase
419 * cwnd only if the current congestion window is being fully
420 * utilized, an incoming SACK advances the Cumulative TSN
421 * Ack Point, and the data sender is not in Fast Recovery.
422 * Only when these three conditions are met can the cwnd be
423 * increased; otherwise, the cwnd MUST not be increased.
424 * If these conditions are met, then cwnd MUST be increased
425 * by, at most, the lesser of 1) the total size of the
426 * previously outstanding DATA chunk(s) acknowledged, and
427 * 2) the destination's path MTU. This upper bound protects
428 * against the ACK-Splitting attack outlined in [SAVAGE99].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400430 if (asoc->fast_recovery)
Vlad Yasevicha6465232008-06-04 12:38:43 -0700431 return;
432
Marcelo Ricardo Leitner4ccbd0b2017-06-23 19:59:35 -0300433 /* The appropriate cwnd increase algorithm is performed
434 * if, and only if the congestion window is being fully
435 * utilized. Note that RFC4960 Errata 3.22 removed the
436 * other condition on ctsn moving.
437 */
438 if (flight_size < cwnd)
439 return;
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (bytes_acked > pmtu)
442 cwnd += pmtu;
443 else
444 cwnd += bytes_acked;
Daniel Borkmannbb333812013-06-28 19:49:40 +0200445
446 pr_debug("%s: slow start: transport:%p, bytes_acked:%d, "
447 "cwnd:%d, ssthresh:%d, flight_size:%d, pba:%d\n",
448 __func__, transport, bytes_acked, cwnd, ssthresh,
449 flight_size, pba);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 } else {
451 /* RFC 2960 7.2.2 Whenever cwnd is greater than ssthresh,
Marcelo Ricardo Leitnere56f777a2017-06-23 19:59:34 -0300452 * upon each SACK arrival, increase partial_bytes_acked
453 * by the total number of bytes of all new chunks
454 * acknowledged in that SACK including chunks
455 * acknowledged by the new Cumulative TSN Ack and by Gap
456 * Ack Blocks. (updated by RFC4960 Errata 3.22)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 *
Marcelo Ricardo Leitner4ccbd0b2017-06-23 19:59:35 -0300458 * When partial_bytes_acked is greater than cwnd and
459 * before the arrival of the SACK the sender had less
460 * bytes of data outstanding than cwnd (i.e., before
461 * arrival of the SACK, flightsize was less than cwnd),
462 * reset partial_bytes_acked to cwnd. (RFC 4960 Errata
463 * 3.26)
464 *
Marcelo Ricardo Leitnerd0b53f42017-06-23 19:59:33 -0300465 * When partial_bytes_acked is equal to or greater than
466 * cwnd and before the arrival of the SACK the sender
467 * had cwnd or more bytes of data outstanding (i.e.,
468 * before arrival of the SACK, flightsize was greater
469 * than or equal to cwnd), partial_bytes_acked is reset
470 * to (partial_bytes_acked - cwnd). Next, cwnd is
471 * increased by MTU. (RFC 4960 Errata 3.12)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
473 pba += bytes_acked;
Marcelo Ricardo Leitner4ccbd0b2017-06-23 19:59:35 -0300474 if (pba > cwnd && flight_size < cwnd)
475 pba = cwnd;
476 if (pba >= cwnd && flight_size >= cwnd) {
Marcelo Ricardo Leitnerd0b53f42017-06-23 19:59:33 -0300477 pba = pba - cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 cwnd += pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200480
481 pr_debug("%s: congestion avoidance: transport:%p, "
482 "bytes_acked:%d, cwnd:%d, ssthresh:%d, "
483 "flight_size:%d, pba:%d\n", __func__,
484 transport, bytes_acked, cwnd, ssthresh,
485 flight_size, pba);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
488 transport->cwnd = cwnd;
489 transport->partial_bytes_acked = pba;
490}
491
492/* This routine is used to lower the transport's cwnd when congestion is
493 * detected.
494 */
495void sctp_transport_lower_cwnd(struct sctp_transport *transport,
Xin Long233e7932017-08-05 19:59:51 +0800496 enum sctp_lower_cwnd reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400498 struct sctp_association *asoc = transport->asoc;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 switch (reason) {
501 case SCTP_LOWER_CWND_T3_RTX:
502 /* RFC 2960 Section 7.2.3, sctpimpguide
503 * When the T3-rtx timer expires on an address, SCTP should
504 * perform slow start by:
505 * ssthresh = max(cwnd/2, 4*MTU)
506 * cwnd = 1*MTU
507 * partial_bytes_acked = 0
508 */
509 transport->ssthresh = max(transport->cwnd/2,
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400510 4*asoc->pathmtu);
511 transport->cwnd = asoc->pathmtu;
Vlad Yasevich33ce8282009-09-04 18:20:58 -0400512
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400513 /* T3-rtx also clears fast recovery */
514 asoc->fast_recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
516
517 case SCTP_LOWER_CWND_FAST_RTX:
518 /* RFC 2960 7.2.4 Adjust the ssthresh and cwnd of the
519 * destination address(es) to which the missing DATA chunks
520 * were last sent, according to the formula described in
521 * Section 7.2.3.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900522 *
523 * RFC 2960 7.2.3, sctpimpguide Upon detection of packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * losses from SACK (see Section 7.2.4), An endpoint
525 * should do the following:
526 * ssthresh = max(cwnd/2, 4*MTU)
527 * cwnd = ssthresh
528 * partial_bytes_acked = 0
529 */
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400530 if (asoc->fast_recovery)
Vlad Yasevicha6465232008-06-04 12:38:43 -0700531 return;
532
533 /* Mark Fast recovery */
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400534 asoc->fast_recovery = 1;
535 asoc->fast_recovery_exit = asoc->next_tsn - 1;
Vlad Yasevicha6465232008-06-04 12:38:43 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 transport->ssthresh = max(transport->cwnd/2,
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400538 4*asoc->pathmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 transport->cwnd = transport->ssthresh;
540 break;
541
542 case SCTP_LOWER_CWND_ECNE:
543 /* RFC 2481 Section 6.1.2.
544 * If the sender receives an ECN-Echo ACK packet
545 * then the sender knows that congestion was encountered in the
546 * network on the path from the sender to the receiver. The
547 * indication of congestion should be treated just as a
548 * congestion loss in non-ECN Capable TCP. That is, the TCP
549 * source halves the congestion window "cwnd" and reduces the
550 * slow start threshold "ssthresh".
551 * A critical condition is that TCP does not react to
552 * congestion indications more than once every window of
553 * data (or more loosely more than once every round-trip time).
554 */
Wei Yongjunf61f6f82009-03-02 09:46:13 +0000555 if (time_after(jiffies, transport->last_time_ecne_reduced +
556 transport->rtt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 transport->ssthresh = max(transport->cwnd/2,
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400558 4*asoc->pathmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 transport->cwnd = transport->ssthresh;
560 transport->last_time_ecne_reduced = jiffies;
561 }
562 break;
563
564 case SCTP_LOWER_CWND_INACTIVE:
565 /* RFC 2960 Section 7.2.1, sctpimpguide
566 * When the endpoint does not transmit data on a given
567 * transport address, the cwnd of the transport address
568 * should be adjusted to max(cwnd/2, 4*MTU) per RTO.
569 * NOTE: Although the draft recommends that this check needs
570 * to be done every RTO interval, we do it every hearbeat
571 * interval.
572 */
Vlad Yasevich245cba72009-11-23 15:53:58 -0500573 transport->cwnd = max(transport->cwnd/2,
Vlad Yasevichcf9b4812010-04-30 22:41:10 -0400574 4*asoc->pathmtu);
Marcelo Ricardo Leitnera02d0362017-06-23 19:59:36 -0300575 /* RFC 4960 Errata 3.27.2: also adjust sshthresh */
576 transport->ssthresh = transport->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 transport->partial_bytes_acked = 0;
Daniel Borkmannbb333812013-06-28 19:49:40 +0200581
582 pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d\n",
583 __func__, transport, reason, transport->cwnd,
584 transport->ssthresh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Vlad Yasevich46d5a802009-11-23 15:54:00 -0500587/* Apply Max.Burst limit to the congestion window:
588 * sctpimpguide-05 2.14.2
589 * D) When the time comes for the sender to
590 * transmit new DATA chunks, the protocol parameter Max.Burst MUST
591 * first be applied to limit how many new DATA chunks may be sent.
592 * The limit is applied by adjusting cwnd as follows:
593 * if ((flightsize+ Max.Burst * MTU) < cwnd)
594 * cwnd = flightsize + Max.Burst * MTU
595 */
596
597void sctp_transport_burst_limited(struct sctp_transport *t)
598{
599 struct sctp_association *asoc = t->asoc;
600 u32 old_cwnd = t->cwnd;
601 u32 max_burst_bytes;
602
wangweidong78ac8142013-12-04 17:32:39 +0800603 if (t->burst_limited || asoc->max_burst == 0)
Vlad Yasevich46d5a802009-11-23 15:54:00 -0500604 return;
605
606 max_burst_bytes = t->flight_size + (asoc->max_burst * asoc->pathmtu);
607 if (max_burst_bytes < old_cwnd) {
608 t->cwnd = max_burst_bytes;
609 t->burst_limited = old_cwnd;
610 }
611}
612
613/* Restore the old cwnd congestion window, after the burst had it's
614 * desired effect.
615 */
616void sctp_transport_burst_reset(struct sctp_transport *t)
617{
618 if (t->burst_limited) {
619 t->cwnd = t->burst_limited;
620 t->burst_limited = 0;
621 }
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624/* What is the next timeout value for this transport? */
Daniel Borkmann8f610592014-06-30 13:52:08 +0200625unsigned long sctp_transport_timeout(struct sctp_transport *trans)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Daniel Borkmann8f610592014-06-30 13:52:08 +0200627 /* RTO + timer slack +/- 50% of RTO */
Marcelo Ricardo Leitnerba6f5e32016-04-06 15:15:19 -0300628 unsigned long timeout = trans->rto >> 1;
Daniel Borkmann8f610592014-06-30 13:52:08 +0200629
630 if (trans->state != SCTP_UNCONFIRMED &&
631 trans->state != SCTP_PF)
632 timeout += trans->hbinterval;
633
Xin Long1d88ba12018-06-05 12:16:58 +0800634 return max_t(unsigned long, timeout, HZ / 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
Vlad Yasevich749bf922007-03-19 17:02:30 -0700636
637/* Reset transport variables to their initial values */
638void sctp_transport_reset(struct sctp_transport *t)
639{
640 struct sctp_association *asoc = t->asoc;
641
642 /* RFC 2960 (bis), Section 5.2.4
643 * All the congestion control parameters (e.g., cwnd, ssthresh)
644 * related to this peer MUST be reset to their initial values
645 * (see Section 6.2.1)
646 */
647 t->cwnd = min(4*asoc->pathmtu, max_t(__u32, 2*asoc->pathmtu, 4380));
Vlad Yasevich46d5a802009-11-23 15:54:00 -0500648 t->burst_limited = 0;
Vlad Yasevich289f4242007-03-22 12:26:25 -0700649 t->ssthresh = asoc->peer.i.a_rwnd;
Andrei Pelinescu-Onciul5fdd4ba2009-11-29 00:14:02 -0800650 t->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000651 sctp_max_rto(asoc, t);
Vlad Yasevich749bf922007-03-19 17:02:30 -0700652 t->rtt = 0;
653 t->srtt = 0;
654 t->rttvar = 0;
655
Masahiro Yamadab564d622017-02-27 14:29:06 -0800656 /* Reset these additional variables so that we have a clean slate. */
Vlad Yasevich749bf922007-03-19 17:02:30 -0700657 t->partial_bytes_acked = 0;
658 t->flight_size = 0;
659 t->error_count = 0;
660 t->rto_pending = 0;
Vlad Yasevichfaee47c2009-02-13 08:33:43 +0000661 t->hb_sent = 0;
Vlad Yasevich749bf922007-03-19 17:02:30 -0700662
663 /* Initialize the state information for SFR-CACC */
664 t->cacc.changeover_active = 0;
665 t->cacc.cycling_changeover = 0;
666 t->cacc.next_tsn_at_change = 0;
667 t->cacc.cacc_saw_newack = 0;
668}
Michio Hondaddc4bbe2011-06-17 11:03:23 +0900669
670/* Schedule retransmission on the given transport */
671void sctp_transport_immediate_rtx(struct sctp_transport *t)
672{
673 /* Stop pending T3_rtx_timer */
Ying Xue25cc4ae2013-02-03 20:32:57 +0000674 if (del_timer(&t->T3_rtx_timer))
Michio Hondaddc4bbe2011-06-17 11:03:23 +0900675 sctp_transport_put(t);
Ying Xue25cc4ae2013-02-03 20:32:57 +0000676
Michio Hondaddc4bbe2011-06-17 11:03:23 +0900677 sctp_retransmit(&t->asoc->outqueue, t, SCTP_RTXR_T3_RTX);
678 if (!timer_pending(&t->T3_rtx_timer)) {
679 if (!mod_timer(&t->T3_rtx_timer, jiffies + t->rto))
680 sctp_transport_hold(t);
681 }
Michio Hondaddc4bbe2011-06-17 11:03:23 +0900682}
Julian Anastasovc86a7732017-02-06 23:14:13 +0200683
684/* Drop dst */
685void sctp_transport_dst_release(struct sctp_transport *t)
686{
687 dst_release(t->dst);
688 t->dst = NULL;
689 t->dst_pending_confirm = 0;
690}
691
692/* Schedule neighbour confirm */
693void sctp_transport_dst_confirm(struct sctp_transport *t)
694{
695 t->dst_pending_confirm = 1;
696}