]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/sctp/transport.c
mac80211: sta_info: Add lockdep condition for RCU list usage
[mirror_ubuntu-hirsute-kernel.git] / net / sctp / transport.c
CommitLineData
47505b8b 1// SPDX-License-Identifier: GPL-2.0-or-later
60c778b2 2/* SCTP kernel implementation
1da177e4
LT
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 *
60c778b2 9 * This file is part of the SCTP kernel implementation
1da177e4
LT
10 *
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 *
1da177e4
LT
15 * Please send any bug reports or fixes you make to the
16 * email address(es):
91705c61 17 * lksctp developers <linux-sctp@vger.kernel.org>
1da177e4 18 *
1da177e4
LT
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>
1da177e4
LT
27 */
28
145ce502
JP
29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
5a0e3ad6 31#include <linux/slab.h>
1da177e4 32#include <linux/types.h>
ad8fec17 33#include <linux/random.h>
1da177e4
LT
34#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. */
89bf3450
EB
40static struct sctp_transport *sctp_transport_init(struct net *net,
41 struct sctp_transport *peer,
1da177e4 42 const union sctp_addr *addr,
dd0fc66f 43 gfp_t gfp)
1da177e4
LT
44{
45 /* Copy in the address. */
1da177e4 46 peer->af_specific = sctp_get_af_specific(addr->sa.sa_family);
4c31bc6b 47 memcpy(&peer->ipaddr, addr, peer->af_specific->sockaddr_len);
1da177e4
LT
48 memset(&peer->saddr, 0, sizeof(union sctp_addr));
49
4244854d
NH
50 peer->sack_generation = 0;
51
1da177e4
LT
52 /* 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 */
e1fc3b14 58 peer->rto = msecs_to_jiffies(net->sctp.rto_initial);
1da177e4 59
8b0e1953 60 peer->last_time_heard = 0;
1da177e4
LT
61 peer->last_time_ecne_reduced = jiffies;
62
52ccb8e9
FF
63 peer->param_flags = SPP_HB_DISABLE |
64 SPP_PMTUD_ENABLE |
65 SPP_SACKDELAY_ENABLE;
1da177e4
LT
66
67 /* Initialize the default path max_retrans. */
e1fc3b14
EB
68 peer->pathmaxrxt = net->sctp.max_retrans_path;
69 peer->pf_retrans = net->sctp.pf_retrans;
1da177e4
LT
70
71 INIT_LIST_HEAD(&peer->transmitted);
72 INIT_LIST_HEAD(&peer->send_ready);
73 INIT_LIST_HEAD(&peer->transports);
74
9c3b5751
KC
75 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);
1da177e4 80
ad8fec17
SS
81 /* Initialize the 64-bit random nonce sent with heartbeat. */
82 get_random_bytes(&peer->hb_nonce, sizeof(peer->hb_nonce));
83
a4b2b58e 84 refcount_set(&peer->refcnt, 1);
1da177e4
LT
85
86 return peer;
87}
88
89/* Allocate and initialize a new transport. */
89bf3450
EB
90struct sctp_transport *sctp_transport_new(struct net *net,
91 const union sctp_addr *addr,
dd0fc66f 92 gfp_t gfp)
1da177e4 93{
d808ad9a 94 struct sctp_transport *transport;
1da177e4 95
939cfa75 96 transport = kzalloc(sizeof(*transport), gfp);
1da177e4
LT
97 if (!transport)
98 goto fail;
99
89bf3450 100 if (!sctp_transport_init(net, transport, addr, gfp))
1da177e4
LT
101 goto fail_init;
102
1da177e4
LT
103 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{
1da177e4
LT
119 /* 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 */
25cc4ae9 128 if (del_timer(&transport->T3_rtx_timer))
1da177e4
LT
129 sctp_transport_put(transport);
130
7b9438de
XL
131 if (del_timer(&transport->reconf_timer))
132 sctp_transport_put(transport);
133
55fa0cfd 134 /* Delete the ICMP proto unreachable timer if it's active. */
25cc4ae9 135 if (del_timer(&transport->proto_unreach_timer))
55fa0cfd 136 sctp_association_put(transport->asoc);
1da177e4
LT
137
138 sctp_transport_put(transport);
139}
140
45122ca2 141static void sctp_transport_destroy_rcu(struct rcu_head *head)
1da177e4 142{
45122ca2 143 struct sctp_transport *transport;
1da177e4 144
45122ca2 145 transport = container_of(head, struct sctp_transport, rcu);
1da177e4
LT
146
147 dst_release(transport->dst);
148 kfree(transport);
149 SCTP_DBG_OBJCNT_DEC(transport);
150}
151
45122ca2
TG
152/* 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{
a4b2b58e 157 if (unlikely(refcount_read(&transport->refcnt))) {
bb33381d
DB
158 WARN(1, "Attempt to destroy undead transport %p!\n", transport);
159 return;
160 }
45122ca2 161
8c98653f
DB
162 sctp_packet_free(&transport->packet);
163
164 if (transport->asoc)
165 sctp_association_put(transport->asoc);
771085d6
DB
166
167 call_rcu(&transport->rcu, sctp_transport_destroy_rcu);
45122ca2
TG
168}
169
1da177e4
LT
170/* 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 */
ba6f5e33 173void sctp_transport_reset_t3_rtx(struct sctp_transport *transport)
1da177e4
LT
174{
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
d9efc223 183 if (!timer_pending(&transport->T3_rtx_timer))
1da177e4
LT
184 if (!mod_timer(&transport->T3_rtx_timer,
185 jiffies + transport->rto))
186 sctp_transport_hold(transport);
ba6f5e33
MRL
187}
188
189void sctp_transport_reset_hb_timer(struct sctp_transport *transport)
190{
191 unsigned long expires;
1da177e4
LT
192
193 /* When a data chunk is sent, reset the heartbeat interval. */
ba6f5e33 194 expires = jiffies + sctp_transport_timeout(transport);
d1f20c03
MK
195 if ((time_before(transport->hb_timer.expires, expires) ||
196 !timer_pending(&transport->hb_timer)) &&
ba6f5e33
MRL
197 !mod_timer(&transport->hb_timer,
198 expires + prandom_u32_max(transport->rto)))
199 sctp_transport_hold(transport);
1da177e4
LT
200}
201
7b9438de
XL
202void 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
1da177e4
LT
210/* 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. */
9914ae3c 222void sctp_transport_pmtu(struct sctp_transport *transport, struct sock *sk)
1da177e4 223{
da0420be 224 /* If we don't have a fresh route, look one up */
f5b0a874 225 if (!transport->dst || transport->dst->obsolete) {
c86a773c 226 sctp_transport_dst_release(transport);
da0420be 227 transport->af_specific->get_dst(transport, &transport->saddr,
8663c938 228 &transport->fl, sk);
da0420be 229 }
1da177e4 230
6e91b578
MRL
231 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
6ff0f871
MRL
240 if (transport->dst)
241 transport->pathmtu = sctp_dst_mtu(transport->dst);
242 else
52ccb8e9 243 transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
1da177e4
LT
244}
245
b6c5734d 246bool sctp_transport_update_pmtu(struct sctp_transport *t, u32 pmtu)
c910b47e 247{
3ebfdf08 248 struct dst_entry *dst = sctp_transport_dst_check(t);
d7ab5cdc 249 struct sock *sk = t->asoc->base.sk;
b6c5734d 250 bool change = true;
c910b47e
VY
251
252 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
b6c5734d
MRL
253 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;
c910b47e 257 }
b6c5734d 258 pmtu = SCTP_TRUNC4(pmtu);
c910b47e 259
02f3d4ce 260 if (dst) {
d7ab5cdc
XL
261 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);
bd085ef6 266 dst->ops->update_pmtu(dst, sk, NULL, pmtu, true);
d7ab5cdc
XL
267 pf->to_sk_daddr(&addr, sk);
268
02f3d4ce 269 dst = sctp_transport_dst_check(t);
02f3d4ce 270 }
3ebfdf08 271
b6c5734d 272 if (!dst) {
d7ab5cdc 273 t->af_specific->get_dst(t, &t->saddr, &t->fl, sk);
b6c5734d
MRL
274 dst = t->dst;
275 }
276
277 if (dst) {
278 /* Re-fetch, as under layers may have a higher minimum size */
a6592547 279 pmtu = sctp_dst_mtu(dst);
b6c5734d
MRL
280 change = t->pathmtu != pmtu;
281 }
282 t->pathmtu = pmtu;
283
284 return change;
c910b47e
VY
285}
286
1da177e4
LT
287/* 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;
1da177e4 295
6e91b578 296 sctp_transport_dst_release(transport);
8663c938 297 af->get_dst(transport, saddr, &transport->fl, sctp_opt2sk(opt));
1da177e4
LT
298
299 if (saddr)
300 memcpy(&transport->saddr, saddr, sizeof(union sctp_addr));
301 else
8663c938 302 af->get_saddr(opt, transport, &transport->fl);
1da177e4 303
6e91b578
MRL
304 sctp_transport_pmtu(transport, sctp_opt2sk(opt));
305
306 /* 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);
1da177e4
LT
312}
313
314/* Hold a reference to a transport. */
1eed6779 315int sctp_transport_hold(struct sctp_transport *transport)
1da177e4 316{
a4b2b58e 317 return refcount_inc_not_zero(&transport->refcnt);
1da177e4
LT
318}
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{
a4b2b58e 325 if (refcount_dec_and_test(&transport->refcnt))
1da177e4
LT
326 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{
bb33381d
DB
332 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);
1da177e4
LT
335
336 if (tp->rttvar || tp->srtt) {
4e7696d9 337 struct net *net = tp->asoc->base.net;
1da177e4
LT
338 /* 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 */
e1fc3b14 349 tp->rttvar = tp->rttvar - (tp->rttvar >> net->sctp.rto_beta)
79211c8e 350 + (((__u32)abs((__s64)tp->srtt - (__s64)rtt)) >> net->sctp.rto_beta);
e1fc3b14
EB
351 tp->srtt = tp->srtt - (tp->srtt >> net->sctp.rto_alpha)
352 + (rtt >> net->sctp.rto_alpha);
1da177e4
LT
353 } 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
196d6759 382 sctp_max_rto(tp->asoc, tp);
1da177e4
LT
383 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
bb33381d
DB
390 pr_debug("%s: transport:%p, rtt:%d, srtt:%d rttvar:%d, rto:%ld\n",
391 __func__, tp, rtt, tp->srtt, tp->rttvar, tp->rto);
1da177e4
LT
392}
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{
cf9b4812 400 struct sctp_association *asoc = transport->asoc;
1da177e4
LT
401 __u32 cwnd, ssthresh, flight_size, pba, pmtu;
402
403 cwnd = transport->cwnd;
404 flight_size = transport->flight_size;
405
a6465234 406 /* See if we need to exit Fast Recovery first */
cf9b4812
VY
407 if (asoc->fast_recovery &&
408 TSN_lte(asoc->fast_recovery_exit, sack_ctsn))
409 asoc->fast_recovery = 0;
a6465234 410
1da177e4
LT
411 ssthresh = transport->ssthresh;
412 pba = transport->partial_bytes_acked;
52ccb8e9 413 pmtu = transport->asoc->pathmtu;
1da177e4
LT
414
415 if (cwnd <= ssthresh) {
a6465234
VY
416 /* 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].
1da177e4 429 */
cf9b4812 430 if (asoc->fast_recovery)
a6465234
VY
431 return;
432
4ccbd0b0
MRL
433 /* 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
1da177e4
LT
441 if (bytes_acked > pmtu)
442 cwnd += pmtu;
443 else
444 cwnd += bytes_acked;
bb33381d
DB
445
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);
1da177e4
LT
450 } else {
451 /* RFC 2960 7.2.2 Whenever cwnd is greater than ssthresh,
e56f777a
MRL
452 * 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)
1da177e4 457 *
4ccbd0b0
MRL
458 * 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 *
d0b53f40
MRL
465 * 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)
1da177e4
LT
472 */
473 pba += bytes_acked;
4ccbd0b0
MRL
474 if (pba > cwnd && flight_size < cwnd)
475 pba = cwnd;
476 if (pba >= cwnd && flight_size >= cwnd) {
d0b53f40 477 pba = pba - cwnd;
1da177e4 478 cwnd += pmtu;
1da177e4 479 }
bb33381d
DB
480
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);
1da177e4
LT
486 }
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,
233e7936 496 enum sctp_lower_cwnd reason)
1da177e4 497{
cf9b4812
VY
498 struct sctp_association *asoc = transport->asoc;
499
1da177e4
LT
500 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,
cf9b4812
VY
510 4*asoc->pathmtu);
511 transport->cwnd = asoc->pathmtu;
33ce8281 512
cf9b4812
VY
513 /* T3-rtx also clears fast recovery */
514 asoc->fast_recovery = 0;
1da177e4
LT
515 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.
d808ad9a
YH
522 *
523 * RFC 2960 7.2.3, sctpimpguide Upon detection of packet
1da177e4
LT
524 * 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 */
cf9b4812 530 if (asoc->fast_recovery)
a6465234
VY
531 return;
532
533 /* Mark Fast recovery */
cf9b4812
VY
534 asoc->fast_recovery = 1;
535 asoc->fast_recovery_exit = asoc->next_tsn - 1;
a6465234 536
1da177e4 537 transport->ssthresh = max(transport->cwnd/2,
cf9b4812 538 4*asoc->pathmtu);
1da177e4
LT
539 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 */
f61f6f82
WY
555 if (time_after(jiffies, transport->last_time_ecne_reduced +
556 transport->rtt)) {
1da177e4 557 transport->ssthresh = max(transport->cwnd/2,
cf9b4812 558 4*asoc->pathmtu);
1da177e4
LT
559 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 */
245cba7e 573 transport->cwnd = max(transport->cwnd/2,
cf9b4812 574 4*asoc->pathmtu);
a02d036c
MRL
575 /* RFC 4960 Errata 3.27.2: also adjust sshthresh */
576 transport->ssthresh = transport->cwnd;
1da177e4 577 break;
3ff50b79 578 }
1da177e4
LT
579
580 transport->partial_bytes_acked = 0;
bb33381d
DB
581
582 pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d\n",
583 __func__, transport, reason, transport->cwnd,
584 transport->ssthresh);
1da177e4
LT
585}
586
46d5a808
VY
587/* 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
78ac814f 603 if (t->burst_limited || asoc->max_burst == 0)
46d5a808
VY
604 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
1da177e4 624/* What is the next timeout value for this transport? */
8f61059a 625unsigned long sctp_transport_timeout(struct sctp_transport *trans)
1da177e4 626{
8f61059a 627 /* RTO + timer slack +/- 50% of RTO */
ba6f5e33 628 unsigned long timeout = trans->rto >> 1;
8f61059a
DB
629
630 if (trans->state != SCTP_UNCONFIRMED &&
631 trans->state != SCTP_PF)
632 timeout += trans->hbinterval;
633
1d88ba1e 634 return max_t(unsigned long, timeout, HZ / 5);
1da177e4 635}
749bf921
VY
636
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));
46d5a808 648 t->burst_limited = 0;
289f4249 649 t->ssthresh = asoc->peer.i.a_rwnd;
5fdd4bae 650 t->rto = asoc->rto_initial;
196d6759 651 sctp_max_rto(asoc, t);
749bf921
VY
652 t->rtt = 0;
653 t->srtt = 0;
654 t->rttvar = 0;
655
b564d62e 656 /* Reset these additional variables so that we have a clean slate. */
749bf921
VY
657 t->partial_bytes_acked = 0;
658 t->flight_size = 0;
659 t->error_count = 0;
660 t->rto_pending = 0;
faee47cd 661 t->hb_sent = 0;
749bf921
VY
662
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}
ddc4bbee
MH
669
670/* Schedule retransmission on the given transport */
671void sctp_transport_immediate_rtx(struct sctp_transport *t)
672{
673 /* Stop pending T3_rtx_timer */
25cc4ae9 674 if (del_timer(&t->T3_rtx_timer))
ddc4bbee 675 sctp_transport_put(t);
25cc4ae9 676
ddc4bbee
MH
677 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 }
ddc4bbee 682}
c86a773c
JA
683
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}