sctp: do state transition when receiving an icmp TOOBIG packet
[linux-block.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 10 *
5112cf59 11 * This module provides the abstraction for an SCTP transport representing
1da177e4
LT
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);
92548ec2 78 timer_setup(&peer->probe_timer, sctp_generate_probe_event, 0);
9c3b5751
KC
79 timer_setup(&peer->proto_unreach_timer,
80 sctp_generate_proto_unreach_event, 0);
1da177e4 81
ad8fec17
SS
82 /* Initialize the 64-bit random nonce sent with heartbeat. */
83 get_random_bytes(&peer->hb_nonce, sizeof(peer->hb_nonce));
84
a4b2b58e 85 refcount_set(&peer->refcnt, 1);
1da177e4
LT
86
87 return peer;
88}
89
90/* Allocate and initialize a new transport. */
89bf3450
EB
91struct sctp_transport *sctp_transport_new(struct net *net,
92 const union sctp_addr *addr,
dd0fc66f 93 gfp_t gfp)
1da177e4 94{
d808ad9a 95 struct sctp_transport *transport;
1da177e4 96
939cfa75 97 transport = kzalloc(sizeof(*transport), gfp);
1da177e4
LT
98 if (!transport)
99 goto fail;
100
89bf3450 101 if (!sctp_transport_init(net, transport, addr, gfp))
1da177e4
LT
102 goto fail_init;
103
1da177e4
LT
104 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{
1da177e4
LT
120 /* 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
5112cf59 127 * the transport is going away.
1da177e4 128 */
25cc4ae9 129 if (del_timer(&transport->T3_rtx_timer))
1da177e4
LT
130 sctp_transport_put(transport);
131
7b9438de
XL
132 if (del_timer(&transport->reconf_timer))
133 sctp_transport_put(transport);
134
92548ec2
XL
135 if (del_timer(&transport->probe_timer))
136 sctp_transport_put(transport);
137
55fa0cfd 138 /* Delete the ICMP proto unreachable timer if it's active. */
25cc4ae9 139 if (del_timer(&transport->proto_unreach_timer))
057a10fa 140 sctp_transport_put(transport);
1da177e4
LT
141
142 sctp_transport_put(transport);
143}
144
45122ca2 145static void sctp_transport_destroy_rcu(struct rcu_head *head)
1da177e4 146{
45122ca2 147 struct sctp_transport *transport;
1da177e4 148
45122ca2 149 transport = container_of(head, struct sctp_transport, rcu);
1da177e4
LT
150
151 dst_release(transport->dst);
152 kfree(transport);
153 SCTP_DBG_OBJCNT_DEC(transport);
154}
155
45122ca2
TG
156/* 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{
a4b2b58e 161 if (unlikely(refcount_read(&transport->refcnt))) {
bb33381d
DB
162 WARN(1, "Attempt to destroy undead transport %p!\n", transport);
163 return;
164 }
45122ca2 165
8c98653f
DB
166 sctp_packet_free(&transport->packet);
167
168 if (transport->asoc)
169 sctp_association_put(transport->asoc);
771085d6
DB
170
171 call_rcu(&transport->rcu, sctp_transport_destroy_rcu);
45122ca2
TG
172}
173
1da177e4
LT
174/* 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 */
ba6f5e33 177void sctp_transport_reset_t3_rtx(struct sctp_transport *transport)
1da177e4
LT
178{
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
d9efc223 187 if (!timer_pending(&transport->T3_rtx_timer))
1da177e4
LT
188 if (!mod_timer(&transport->T3_rtx_timer,
189 jiffies + transport->rto))
190 sctp_transport_hold(transport);
ba6f5e33
MRL
191}
192
193void sctp_transport_reset_hb_timer(struct sctp_transport *transport)
194{
195 unsigned long expires;
1da177e4
LT
196
197 /* When a data chunk is sent, reset the heartbeat interval. */
ba6f5e33 198 expires = jiffies + sctp_transport_timeout(transport);
d1f20c03
MK
199 if ((time_before(transport->hb_timer.expires, expires) ||
200 !timer_pending(&transport->hb_timer)) &&
ba6f5e33
MRL
201 !mod_timer(&transport->hb_timer,
202 expires + prandom_u32_max(transport->rto)))
203 sctp_transport_hold(transport);
1da177e4
LT
204}
205
7b9438de
XL
206void 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
92548ec2
XL
214void 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
1da177e4
LT
228/* 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. */
9914ae3c 240void sctp_transport_pmtu(struct sctp_transport *transport, struct sock *sk)
1da177e4 241{
da0420be 242 /* If we don't have a fresh route, look one up */
f5b0a874 243 if (!transport->dst || transport->dst->obsolete) {
c86a773c 244 sctp_transport_dst_release(transport);
da0420be 245 transport->af_specific->get_dst(transport, &transport->saddr,
8663c938 246 &transport->fl, sk);
da0420be 247 }
1da177e4 248
6e91b578
MRL
249 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
6ff0f871
MRL
258 if (transport->dst)
259 transport->pathmtu = sctp_dst_mtu(transport->dst);
260 else
52ccb8e9 261 transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
1da177e4
LT
262}
263
1dc68c19
XL
264void sctp_transport_pl_send(struct sctp_transport *t)
265{
266 pr_debug("%s: PLPMTUD: transport: %p, state: %d, pmtu: %d, size: %d, high: %d\n",
267 __func__, t, t->pl.state, t->pl.pmtu, t->pl.probe_size, t->pl.probe_high);
268
269 if (t->pl.probe_count < SCTP_MAX_PROBES) {
270 t->pl.probe_count++;
271 return;
272 }
273
274 if (t->pl.state == SCTP_PL_BASE) {
275 if (t->pl.probe_size == SCTP_BASE_PLPMTU) { /* BASE_PLPMTU Confirmation Failed */
276 t->pl.state = SCTP_PL_ERROR; /* Base -> Error */
277
278 t->pl.pmtu = SCTP_MIN_PLPMTU;
279 t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
280 sctp_assoc_sync_pmtu(t->asoc);
281 }
282 } else if (t->pl.state == SCTP_PL_SEARCH) {
283 if (t->pl.pmtu == t->pl.probe_size) { /* Black Hole Detected */
284 t->pl.state = SCTP_PL_BASE; /* Search -> Base */
285 t->pl.probe_size = SCTP_BASE_PLPMTU;
286 t->pl.probe_high = 0;
287
288 t->pl.pmtu = SCTP_BASE_PLPMTU;
289 t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
290 sctp_assoc_sync_pmtu(t->asoc);
291 } else { /* Normal probe failure. */
292 t->pl.probe_high = t->pl.probe_size;
293 t->pl.probe_size = t->pl.pmtu;
294 }
295 } else if (t->pl.state == SCTP_PL_COMPLETE) {
296 if (t->pl.pmtu == t->pl.probe_size) { /* Black Hole Detected */
297 t->pl.state = SCTP_PL_BASE; /* Search Complete -> Base */
298 t->pl.probe_size = SCTP_BASE_PLPMTU;
299
300 t->pl.pmtu = SCTP_BASE_PLPMTU;
301 t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
302 sctp_assoc_sync_pmtu(t->asoc);
303 }
304 }
305 t->pl.probe_count = 1;
306}
307
b87641af
XL
308void sctp_transport_pl_recv(struct sctp_transport *t)
309{
310 pr_debug("%s: PLPMTUD: transport: %p, state: %d, pmtu: %d, size: %d, high: %d\n",
311 __func__, t, t->pl.state, t->pl.pmtu, t->pl.probe_size, t->pl.probe_high);
312
313 t->pl.pmtu = t->pl.probe_size;
314 t->pl.probe_count = 0;
315 if (t->pl.state == SCTP_PL_BASE) {
316 t->pl.state = SCTP_PL_SEARCH; /* Base -> Search */
317 t->pl.probe_size += SCTP_PL_BIG_STEP;
318 } else if (t->pl.state == SCTP_PL_ERROR) {
319 t->pl.state = SCTP_PL_SEARCH; /* Error -> Search */
320
321 t->pl.pmtu = t->pl.probe_size;
322 t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
323 sctp_assoc_sync_pmtu(t->asoc);
324 t->pl.probe_size += SCTP_PL_BIG_STEP;
325 } else if (t->pl.state == SCTP_PL_SEARCH) {
326 if (!t->pl.probe_high) {
327 t->pl.probe_size = min(t->pl.probe_size + SCTP_PL_BIG_STEP,
328 SCTP_MAX_PLPMTU);
329 return;
330 }
331 t->pl.probe_size += SCTP_PL_MIN_STEP;
332 if (t->pl.probe_size >= t->pl.probe_high) {
333 t->pl.probe_high = 0;
334 t->pl.state = SCTP_PL_COMPLETE; /* Search -> Search Complete */
335
336 t->pl.probe_size = t->pl.pmtu;
337 t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
338 sctp_assoc_sync_pmtu(t->asoc);
339 }
340 } else if (t->pl.state == SCTP_PL_COMPLETE) {
341 t->pl.state = SCTP_PL_SEARCH; /* Search Complete -> Search */
342 t->pl.probe_size += SCTP_PL_MIN_STEP;
343 }
344}
345
83696408
XL
346static bool sctp_transport_pl_toobig(struct sctp_transport *t, u32 pmtu)
347{
348 pr_debug("%s: PLPMTUD: transport: %p, state: %d, pmtu: %d, size: %d, ptb: %d\n",
349 __func__, t, t->pl.state, t->pl.pmtu, t->pl.probe_size, pmtu);
350
351 if (pmtu < SCTP_MIN_PLPMTU || pmtu >= t->pl.probe_size)
352 return false;
353
354 if (t->pl.state == SCTP_PL_BASE) {
355 if (pmtu >= SCTP_MIN_PLPMTU && pmtu < SCTP_BASE_PLPMTU) {
356 t->pl.state = SCTP_PL_ERROR; /* Base -> Error */
357
358 t->pl.pmtu = SCTP_MIN_PLPMTU;
359 t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
360 }
361 } else if (t->pl.state == SCTP_PL_SEARCH) {
362 if (pmtu >= SCTP_BASE_PLPMTU && pmtu < t->pl.pmtu) {
363 t->pl.state = SCTP_PL_BASE; /* Search -> Base */
364 t->pl.probe_size = SCTP_BASE_PLPMTU;
365 t->pl.probe_count = 0;
366
367 t->pl.probe_high = 0;
368 t->pl.pmtu = SCTP_BASE_PLPMTU;
369 t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
370 } else if (pmtu > t->pl.pmtu && pmtu < t->pl.probe_size) {
371 t->pl.probe_size = pmtu;
372 t->pl.probe_count = 0;
373
374 return false;
375 }
376 } else if (t->pl.state == SCTP_PL_COMPLETE) {
377 if (pmtu >= SCTP_BASE_PLPMTU && pmtu < t->pl.pmtu) {
378 t->pl.state = SCTP_PL_BASE; /* Complete -> Base */
379 t->pl.probe_size = SCTP_BASE_PLPMTU;
380 t->pl.probe_count = 0;
381
382 t->pl.probe_high = 0;
383 t->pl.pmtu = SCTP_BASE_PLPMTU;
384 t->pathmtu = t->pl.pmtu + sctp_transport_pl_hlen(t);
385 }
386 }
387
388 return true;
389}
390
b6c5734d 391bool sctp_transport_update_pmtu(struct sctp_transport *t, u32 pmtu)
c910b47e 392{
d7ab5cdc 393 struct sock *sk = t->asoc->base.sk;
83696408 394 struct dst_entry *dst;
b6c5734d 395 bool change = true;
c910b47e
VY
396
397 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
b6c5734d
MRL
398 pr_warn_ratelimited("%s: Reported pmtu %d too low, using default minimum of %d\n",
399 __func__, pmtu, SCTP_DEFAULT_MINSEGMENT);
400 /* Use default minimum segment instead */
401 pmtu = SCTP_DEFAULT_MINSEGMENT;
c910b47e 402 }
b6c5734d 403 pmtu = SCTP_TRUNC4(pmtu);
c910b47e 404
83696408
XL
405 if (sctp_transport_pl_enabled(t))
406 return sctp_transport_pl_toobig(t, pmtu - sctp_transport_pl_hlen(t));
407
408 dst = sctp_transport_dst_check(t);
02f3d4ce 409 if (dst) {
d7ab5cdc
XL
410 struct sctp_pf *pf = sctp_get_pf_specific(dst->ops->family);
411 union sctp_addr addr;
412
413 pf->af->from_sk(&addr, sk);
414 pf->to_sk_daddr(&t->ipaddr, sk);
bd085ef6 415 dst->ops->update_pmtu(dst, sk, NULL, pmtu, true);
d7ab5cdc
XL
416 pf->to_sk_daddr(&addr, sk);
417
02f3d4ce 418 dst = sctp_transport_dst_check(t);
02f3d4ce 419 }
3ebfdf08 420
b6c5734d 421 if (!dst) {
d7ab5cdc 422 t->af_specific->get_dst(t, &t->saddr, &t->fl, sk);
b6c5734d
MRL
423 dst = t->dst;
424 }
425
426 if (dst) {
427 /* Re-fetch, as under layers may have a higher minimum size */
a6592547 428 pmtu = sctp_dst_mtu(dst);
b6c5734d
MRL
429 change = t->pathmtu != pmtu;
430 }
431 t->pathmtu = pmtu;
432
433 return change;
c910b47e
VY
434}
435
1da177e4
LT
436/* Caches the dst entry and source address for a transport's destination
437 * address.
438 */
439void sctp_transport_route(struct sctp_transport *transport,
440 union sctp_addr *saddr, struct sctp_sock *opt)
441{
442 struct sctp_association *asoc = transport->asoc;
443 struct sctp_af *af = transport->af_specific;
1da177e4 444
6e91b578 445 sctp_transport_dst_release(transport);
8663c938 446 af->get_dst(transport, saddr, &transport->fl, sctp_opt2sk(opt));
1da177e4
LT
447
448 if (saddr)
449 memcpy(&transport->saddr, saddr, sizeof(union sctp_addr));
450 else
8663c938 451 af->get_saddr(opt, transport, &transport->fl);
1da177e4 452
6e91b578
MRL
453 sctp_transport_pmtu(transport, sctp_opt2sk(opt));
454
455 /* Initialize sk->sk_rcv_saddr, if the transport is the
456 * association's active path for getsockname().
457 */
458 if (transport->dst && asoc &&
459 (!asoc->peer.primary_path || transport == asoc->peer.active_path))
460 opt->pf->to_sk_saddr(&transport->saddr, asoc->base.sk);
1da177e4
LT
461}
462
463/* Hold a reference to a transport. */
1eed6779 464int sctp_transport_hold(struct sctp_transport *transport)
1da177e4 465{
a4b2b58e 466 return refcount_inc_not_zero(&transport->refcnt);
1da177e4
LT
467}
468
469/* Release a reference to a transport and clean up
470 * if there are no more references.
471 */
472void sctp_transport_put(struct sctp_transport *transport)
473{
a4b2b58e 474 if (refcount_dec_and_test(&transport->refcnt))
1da177e4
LT
475 sctp_transport_destroy(transport);
476}
477
478/* Update transport's RTO based on the newly calculated RTT. */
479void sctp_transport_update_rto(struct sctp_transport *tp, __u32 rtt)
480{
bb33381d
DB
481 if (unlikely(!tp->rto_pending))
482 /* We should not be doing any RTO updates unless rto_pending is set. */
483 pr_debug("%s: rto_pending not set on transport %p!\n", __func__, tp);
1da177e4
LT
484
485 if (tp->rttvar || tp->srtt) {
4e7696d9 486 struct net *net = tp->asoc->base.net;
1da177e4
LT
487 /* 6.3.1 C3) When a new RTT measurement R' is made, set
488 * RTTVAR <- (1 - RTO.Beta) * RTTVAR + RTO.Beta * |SRTT - R'|
489 * SRTT <- (1 - RTO.Alpha) * SRTT + RTO.Alpha * R'
490 */
491
492 /* Note: The above algorithm has been rewritten to
493 * express rto_beta and rto_alpha as inverse powers
494 * of two.
495 * For example, assuming the default value of RTO.Alpha of
496 * 1/8, rto_alpha would be expressed as 3.
497 */
e1fc3b14 498 tp->rttvar = tp->rttvar - (tp->rttvar >> net->sctp.rto_beta)
79211c8e 499 + (((__u32)abs((__s64)tp->srtt - (__s64)rtt)) >> net->sctp.rto_beta);
e1fc3b14
EB
500 tp->srtt = tp->srtt - (tp->srtt >> net->sctp.rto_alpha)
501 + (rtt >> net->sctp.rto_alpha);
1da177e4
LT
502 } else {
503 /* 6.3.1 C2) When the first RTT measurement R is made, set
504 * SRTT <- R, RTTVAR <- R/2.
505 */
506 tp->srtt = rtt;
507 tp->rttvar = rtt >> 1;
508 }
509
510 /* 6.3.1 G1) Whenever RTTVAR is computed, if RTTVAR = 0, then
511 * adjust RTTVAR <- G, where G is the CLOCK GRANULARITY.
512 */
513 if (tp->rttvar == 0)
514 tp->rttvar = SCTP_CLOCK_GRANULARITY;
515
516 /* 6.3.1 C3) After the computation, update RTO <- SRTT + 4 * RTTVAR. */
517 tp->rto = tp->srtt + (tp->rttvar << 2);
518
519 /* 6.3.1 C6) Whenever RTO is computed, if it is less than RTO.Min
520 * seconds then it is rounded up to RTO.Min seconds.
521 */
522 if (tp->rto < tp->asoc->rto_min)
523 tp->rto = tp->asoc->rto_min;
524
525 /* 6.3.1 C7) A maximum value may be placed on RTO provided it is
526 * at least RTO.max seconds.
527 */
528 if (tp->rto > tp->asoc->rto_max)
529 tp->rto = tp->asoc->rto_max;
530
196d6759 531 sctp_max_rto(tp->asoc, tp);
1da177e4
LT
532 tp->rtt = rtt;
533
534 /* Reset rto_pending so that a new RTT measurement is started when a
535 * new data chunk is sent.
536 */
537 tp->rto_pending = 0;
538
bb33381d
DB
539 pr_debug("%s: transport:%p, rtt:%d, srtt:%d rttvar:%d, rto:%ld\n",
540 __func__, tp, rtt, tp->srtt, tp->rttvar, tp->rto);
1da177e4
LT
541}
542
543/* This routine updates the transport's cwnd and partial_bytes_acked
544 * parameters based on the bytes acked in the received SACK.
545 */
546void sctp_transport_raise_cwnd(struct sctp_transport *transport,
547 __u32 sack_ctsn, __u32 bytes_acked)
548{
cf9b4812 549 struct sctp_association *asoc = transport->asoc;
1da177e4
LT
550 __u32 cwnd, ssthresh, flight_size, pba, pmtu;
551
552 cwnd = transport->cwnd;
553 flight_size = transport->flight_size;
554
a6465234 555 /* See if we need to exit Fast Recovery first */
cf9b4812
VY
556 if (asoc->fast_recovery &&
557 TSN_lte(asoc->fast_recovery_exit, sack_ctsn))
558 asoc->fast_recovery = 0;
a6465234 559
1da177e4
LT
560 ssthresh = transport->ssthresh;
561 pba = transport->partial_bytes_acked;
52ccb8e9 562 pmtu = transport->asoc->pathmtu;
1da177e4
LT
563
564 if (cwnd <= ssthresh) {
a6465234
VY
565 /* RFC 4960 7.2.1
566 * o When cwnd is less than or equal to ssthresh, an SCTP
567 * endpoint MUST use the slow-start algorithm to increase
568 * cwnd only if the current congestion window is being fully
569 * utilized, an incoming SACK advances the Cumulative TSN
570 * Ack Point, and the data sender is not in Fast Recovery.
571 * Only when these three conditions are met can the cwnd be
572 * increased; otherwise, the cwnd MUST not be increased.
573 * If these conditions are met, then cwnd MUST be increased
574 * by, at most, the lesser of 1) the total size of the
575 * previously outstanding DATA chunk(s) acknowledged, and
576 * 2) the destination's path MTU. This upper bound protects
577 * against the ACK-Splitting attack outlined in [SAVAGE99].
1da177e4 578 */
cf9b4812 579 if (asoc->fast_recovery)
a6465234
VY
580 return;
581
4ccbd0b0
MRL
582 /* The appropriate cwnd increase algorithm is performed
583 * if, and only if the congestion window is being fully
584 * utilized. Note that RFC4960 Errata 3.22 removed the
585 * other condition on ctsn moving.
586 */
587 if (flight_size < cwnd)
588 return;
589
1da177e4
LT
590 if (bytes_acked > pmtu)
591 cwnd += pmtu;
592 else
593 cwnd += bytes_acked;
bb33381d
DB
594
595 pr_debug("%s: slow start: transport:%p, bytes_acked:%d, "
596 "cwnd:%d, ssthresh:%d, flight_size:%d, pba:%d\n",
597 __func__, transport, bytes_acked, cwnd, ssthresh,
598 flight_size, pba);
1da177e4
LT
599 } else {
600 /* RFC 2960 7.2.2 Whenever cwnd is greater than ssthresh,
e56f777a
MRL
601 * upon each SACK arrival, increase partial_bytes_acked
602 * by the total number of bytes of all new chunks
603 * acknowledged in that SACK including chunks
604 * acknowledged by the new Cumulative TSN Ack and by Gap
605 * Ack Blocks. (updated by RFC4960 Errata 3.22)
1da177e4 606 *
4ccbd0b0
MRL
607 * When partial_bytes_acked is greater than cwnd and
608 * before the arrival of the SACK the sender had less
609 * bytes of data outstanding than cwnd (i.e., before
610 * arrival of the SACK, flightsize was less than cwnd),
611 * reset partial_bytes_acked to cwnd. (RFC 4960 Errata
612 * 3.26)
613 *
d0b53f40
MRL
614 * When partial_bytes_acked is equal to or greater than
615 * cwnd and before the arrival of the SACK the sender
616 * had cwnd or more bytes of data outstanding (i.e.,
617 * before arrival of the SACK, flightsize was greater
618 * than or equal to cwnd), partial_bytes_acked is reset
619 * to (partial_bytes_acked - cwnd). Next, cwnd is
620 * increased by MTU. (RFC 4960 Errata 3.12)
1da177e4
LT
621 */
622 pba += bytes_acked;
4ccbd0b0
MRL
623 if (pba > cwnd && flight_size < cwnd)
624 pba = cwnd;
625 if (pba >= cwnd && flight_size >= cwnd) {
d0b53f40 626 pba = pba - cwnd;
1da177e4 627 cwnd += pmtu;
1da177e4 628 }
bb33381d
DB
629
630 pr_debug("%s: congestion avoidance: transport:%p, "
631 "bytes_acked:%d, cwnd:%d, ssthresh:%d, "
632 "flight_size:%d, pba:%d\n", __func__,
633 transport, bytes_acked, cwnd, ssthresh,
634 flight_size, pba);
1da177e4
LT
635 }
636
637 transport->cwnd = cwnd;
638 transport->partial_bytes_acked = pba;
639}
640
641/* This routine is used to lower the transport's cwnd when congestion is
642 * detected.
643 */
644void sctp_transport_lower_cwnd(struct sctp_transport *transport,
233e7936 645 enum sctp_lower_cwnd reason)
1da177e4 646{
cf9b4812
VY
647 struct sctp_association *asoc = transport->asoc;
648
1da177e4
LT
649 switch (reason) {
650 case SCTP_LOWER_CWND_T3_RTX:
651 /* RFC 2960 Section 7.2.3, sctpimpguide
652 * When the T3-rtx timer expires on an address, SCTP should
653 * perform slow start by:
654 * ssthresh = max(cwnd/2, 4*MTU)
655 * cwnd = 1*MTU
656 * partial_bytes_acked = 0
657 */
658 transport->ssthresh = max(transport->cwnd/2,
cf9b4812
VY
659 4*asoc->pathmtu);
660 transport->cwnd = asoc->pathmtu;
33ce8281 661
cf9b4812
VY
662 /* T3-rtx also clears fast recovery */
663 asoc->fast_recovery = 0;
1da177e4
LT
664 break;
665
666 case SCTP_LOWER_CWND_FAST_RTX:
667 /* RFC 2960 7.2.4 Adjust the ssthresh and cwnd of the
668 * destination address(es) to which the missing DATA chunks
669 * were last sent, according to the formula described in
670 * Section 7.2.3.
d808ad9a
YH
671 *
672 * RFC 2960 7.2.3, sctpimpguide Upon detection of packet
1da177e4
LT
673 * losses from SACK (see Section 7.2.4), An endpoint
674 * should do the following:
675 * ssthresh = max(cwnd/2, 4*MTU)
676 * cwnd = ssthresh
677 * partial_bytes_acked = 0
678 */
cf9b4812 679 if (asoc->fast_recovery)
a6465234
VY
680 return;
681
682 /* Mark Fast recovery */
cf9b4812
VY
683 asoc->fast_recovery = 1;
684 asoc->fast_recovery_exit = asoc->next_tsn - 1;
a6465234 685
1da177e4 686 transport->ssthresh = max(transport->cwnd/2,
cf9b4812 687 4*asoc->pathmtu);
1da177e4
LT
688 transport->cwnd = transport->ssthresh;
689 break;
690
691 case SCTP_LOWER_CWND_ECNE:
692 /* RFC 2481 Section 6.1.2.
693 * If the sender receives an ECN-Echo ACK packet
694 * then the sender knows that congestion was encountered in the
695 * network on the path from the sender to the receiver. The
696 * indication of congestion should be treated just as a
697 * congestion loss in non-ECN Capable TCP. That is, the TCP
698 * source halves the congestion window "cwnd" and reduces the
699 * slow start threshold "ssthresh".
700 * A critical condition is that TCP does not react to
701 * congestion indications more than once every window of
702 * data (or more loosely more than once every round-trip time).
703 */
f61f6f82
WY
704 if (time_after(jiffies, transport->last_time_ecne_reduced +
705 transport->rtt)) {
1da177e4 706 transport->ssthresh = max(transport->cwnd/2,
cf9b4812 707 4*asoc->pathmtu);
1da177e4
LT
708 transport->cwnd = transport->ssthresh;
709 transport->last_time_ecne_reduced = jiffies;
710 }
711 break;
712
713 case SCTP_LOWER_CWND_INACTIVE:
714 /* RFC 2960 Section 7.2.1, sctpimpguide
715 * When the endpoint does not transmit data on a given
716 * transport address, the cwnd of the transport address
717 * should be adjusted to max(cwnd/2, 4*MTU) per RTO.
718 * NOTE: Although the draft recommends that this check needs
719 * to be done every RTO interval, we do it every hearbeat
720 * interval.
721 */
245cba7e 722 transport->cwnd = max(transport->cwnd/2,
cf9b4812 723 4*asoc->pathmtu);
a02d036c
MRL
724 /* RFC 4960 Errata 3.27.2: also adjust sshthresh */
725 transport->ssthresh = transport->cwnd;
1da177e4 726 break;
3ff50b79 727 }
1da177e4
LT
728
729 transport->partial_bytes_acked = 0;
bb33381d
DB
730
731 pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d\n",
732 __func__, transport, reason, transport->cwnd,
733 transport->ssthresh);
1da177e4
LT
734}
735
46d5a808
VY
736/* Apply Max.Burst limit to the congestion window:
737 * sctpimpguide-05 2.14.2
738 * D) When the time comes for the sender to
739 * transmit new DATA chunks, the protocol parameter Max.Burst MUST
740 * first be applied to limit how many new DATA chunks may be sent.
741 * The limit is applied by adjusting cwnd as follows:
742 * if ((flightsize+ Max.Burst * MTU) < cwnd)
743 * cwnd = flightsize + Max.Burst * MTU
744 */
745
746void sctp_transport_burst_limited(struct sctp_transport *t)
747{
748 struct sctp_association *asoc = t->asoc;
749 u32 old_cwnd = t->cwnd;
750 u32 max_burst_bytes;
751
78ac814f 752 if (t->burst_limited || asoc->max_burst == 0)
46d5a808
VY
753 return;
754
755 max_burst_bytes = t->flight_size + (asoc->max_burst * asoc->pathmtu);
756 if (max_burst_bytes < old_cwnd) {
757 t->cwnd = max_burst_bytes;
758 t->burst_limited = old_cwnd;
759 }
760}
761
762/* Restore the old cwnd congestion window, after the burst had it's
763 * desired effect.
764 */
765void sctp_transport_burst_reset(struct sctp_transport *t)
766{
767 if (t->burst_limited) {
768 t->cwnd = t->burst_limited;
769 t->burst_limited = 0;
770 }
771}
772
1da177e4 773/* What is the next timeout value for this transport? */
8f61059a 774unsigned long sctp_transport_timeout(struct sctp_transport *trans)
1da177e4 775{
8f61059a 776 /* RTO + timer slack +/- 50% of RTO */
ba6f5e33 777 unsigned long timeout = trans->rto >> 1;
8f61059a
DB
778
779 if (trans->state != SCTP_UNCONFIRMED &&
780 trans->state != SCTP_PF)
781 timeout += trans->hbinterval;
782
1d88ba1e 783 return max_t(unsigned long, timeout, HZ / 5);
1da177e4 784}
749bf921
VY
785
786/* Reset transport variables to their initial values */
787void sctp_transport_reset(struct sctp_transport *t)
788{
789 struct sctp_association *asoc = t->asoc;
790
791 /* RFC 2960 (bis), Section 5.2.4
792 * All the congestion control parameters (e.g., cwnd, ssthresh)
793 * related to this peer MUST be reset to their initial values
794 * (see Section 6.2.1)
795 */
796 t->cwnd = min(4*asoc->pathmtu, max_t(__u32, 2*asoc->pathmtu, 4380));
46d5a808 797 t->burst_limited = 0;
289f4249 798 t->ssthresh = asoc->peer.i.a_rwnd;
5fdd4bae 799 t->rto = asoc->rto_initial;
196d6759 800 sctp_max_rto(asoc, t);
749bf921
VY
801 t->rtt = 0;
802 t->srtt = 0;
803 t->rttvar = 0;
804
b564d62e 805 /* Reset these additional variables so that we have a clean slate. */
749bf921
VY
806 t->partial_bytes_acked = 0;
807 t->flight_size = 0;
808 t->error_count = 0;
809 t->rto_pending = 0;
faee47cd 810 t->hb_sent = 0;
749bf921
VY
811
812 /* Initialize the state information for SFR-CACC */
813 t->cacc.changeover_active = 0;
814 t->cacc.cycling_changeover = 0;
815 t->cacc.next_tsn_at_change = 0;
816 t->cacc.cacc_saw_newack = 0;
817}
ddc4bbee
MH
818
819/* Schedule retransmission on the given transport */
820void sctp_transport_immediate_rtx(struct sctp_transport *t)
821{
822 /* Stop pending T3_rtx_timer */
25cc4ae9 823 if (del_timer(&t->T3_rtx_timer))
ddc4bbee 824 sctp_transport_put(t);
25cc4ae9 825
ddc4bbee
MH
826 sctp_retransmit(&t->asoc->outqueue, t, SCTP_RTXR_T3_RTX);
827 if (!timer_pending(&t->T3_rtx_timer)) {
828 if (!mod_timer(&t->T3_rtx_timer, jiffies + t->rto))
829 sctp_transport_hold(t);
830 }
ddc4bbee 831}
c86a773c
JA
832
833/* Drop dst */
834void sctp_transport_dst_release(struct sctp_transport *t)
835{
836 dst_release(t->dst);
837 t->dst = NULL;
838 t->dst_pending_confirm = 0;
839}
840
841/* Schedule neighbour confirm */
842void sctp_transport_dst_confirm(struct sctp_transport *t)
843{
844 t->dst_pending_confirm = 1;
845}