1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3  * Copyright (c) 1999-2000 Cisco, Inc.
4  * Copyright (c) 1999-2001 Motorola, Inc.
5  * Copyright (c) 2001-2003 International Business Machines Corp.
6  * Copyright (c) 2001 Intel Corp.
7  * Copyright (c) 2001 La Monte H.P. Yarroll
8  *
9  * This file is part of the SCTP kernel implementation
10  *
11  * This module provides the abstraction for an SCTP tranport representing
12  * a remote transport address.  For local transport addresses, we just use
13  * union sctp_addr.
14  *
15  * Please send any bug reports or fixes you make to the
16  * email address(es):
17  *    lksctp developers <linux-sctp@vger.kernel.org>
18  *
19  * Written or modified by:
20  *    La Monte H.P. Yarroll <piggy@acm.org>
21  *    Karl Knutson          <karl@athena.chicago.il.us>
22  *    Jon Grimm             <jgrimm@us.ibm.com>
23  *    Xingang Guo           <xingang.guo@intel.com>
24  *    Hui Huang             <hui.huang@nokia.com>
25  *    Sridhar Samudrala	    <sri@us.ibm.com>
26  *    Ardelle Fan	    <ardelle.fan@intel.com>
27  */
28 
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 
31 #include <linux/slab.h>
32 #include <linux/types.h>
33 #include <linux/random.h>
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.  */
sctp_transport_init(struct net * net,struct sctp_transport * peer,const union sctp_addr * addr,gfp_t gfp)40 static struct sctp_transport *sctp_transport_init(struct net *net,
41 						  struct sctp_transport *peer,
42 						  const union sctp_addr *addr,
43 						  gfp_t gfp)
44 {
45 	/* Copy in the address.  */
46 	peer->af_specific = sctp_get_af_specific(addr->sa.sa_family);
47 	memcpy(&peer->ipaddr, addr, peer->af_specific->sockaddr_len);
48 	memset(&peer->saddr, 0, sizeof(union sctp_addr));
49 
50 	peer->sack_generation = 0;
51 
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 	 */
58 	peer->rto = msecs_to_jiffies(net->sctp.rto_initial);
59 
60 	peer->last_time_heard = 0;
61 	peer->last_time_ecne_reduced = jiffies;
62 
63 	peer->param_flags = SPP_HB_DISABLE |
64 			    SPP_PMTUD_ENABLE |
65 			    SPP_SACKDELAY_ENABLE;
66 
67 	/* Initialize the default path max_retrans.  */
68 	peer->pathmaxrxt  = net->sctp.max_retrans_path;
69 	peer->pf_retrans  = net->sctp.pf_retrans;
70 
71 	INIT_LIST_HEAD(&peer->transmitted);
72 	INIT_LIST_HEAD(&peer->send_ready);
73 	INIT_LIST_HEAD(&peer->transports);
74 
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);
80 
81 	/* Initialize the 64-bit random nonce sent with heartbeat. */
82 	get_random_bytes(&peer->hb_nonce, sizeof(peer->hb_nonce));
83 
84 	refcount_set(&peer->refcnt, 1);
85 
86 	return peer;
87 }
88 
89 /* Allocate and initialize a new transport.  */
sctp_transport_new(struct net * net,const union sctp_addr * addr,gfp_t gfp)90 struct sctp_transport *sctp_transport_new(struct net *net,
91 					  const union sctp_addr *addr,
92 					  gfp_t gfp)
93 {
94 	struct sctp_transport *transport;
95 
96 	transport = kzalloc(sizeof(*transport), gfp);
97 	if (!transport)
98 		goto fail;
99 
100 	if (!sctp_transport_init(net, transport, addr, gfp))
101 		goto fail_init;
102 
103 	SCTP_DBG_OBJCNT_INC(transport);
104 
105 	return transport;
106 
107 fail_init:
108 	kfree(transport);
109 
110 fail:
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  */
sctp_transport_free(struct sctp_transport * transport)117 void sctp_transport_free(struct sctp_transport *transport)
118 {
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 	 */
128 	if (del_timer(&transport->T3_rtx_timer))
129 		sctp_transport_put(transport);
130 
131 	if (del_timer(&transport->reconf_timer))
132 		sctp_transport_put(transport);
133 
134 	/* Delete the ICMP proto unreachable timer if it's active. */
135 	if (del_timer(&transport->proto_unreach_timer))
136 		sctp_transport_put(transport);
137 
138 	sctp_transport_put(transport);
139 }
140 
sctp_transport_destroy_rcu(struct rcu_head * head)141 static void sctp_transport_destroy_rcu(struct rcu_head *head)
142 {
143 	struct sctp_transport *transport;
144 
145 	transport = container_of(head, struct sctp_transport, rcu);
146 
147 	dst_release(transport->dst);
148 	kfree(transport);
149 	SCTP_DBG_OBJCNT_DEC(transport);
150 }
151 
152 /* Destroy the transport data structure.
153  * Assumes there are no more users of this structure.
154  */
sctp_transport_destroy(struct sctp_transport * transport)155 static void sctp_transport_destroy(struct sctp_transport *transport)
156 {
157 	if (unlikely(refcount_read(&transport->refcnt))) {
158 		WARN(1, "Attempt to destroy undead transport %p!\n", transport);
159 		return;
160 	}
161 
162 	sctp_packet_free(&transport->packet);
163 
164 	if (transport->asoc)
165 		sctp_association_put(transport->asoc);
166 
167 	call_rcu(&transport->rcu, sctp_transport_destroy_rcu);
168 }
169 
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  */
sctp_transport_reset_t3_rtx(struct sctp_transport * transport)173 void sctp_transport_reset_t3_rtx(struct sctp_transport *transport)
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 
183 	if (!timer_pending(&transport->T3_rtx_timer))
184 		if (!mod_timer(&transport->T3_rtx_timer,
185 			       jiffies + transport->rto))
186 			sctp_transport_hold(transport);
187 }
188 
sctp_transport_reset_hb_timer(struct sctp_transport * transport)189 void sctp_transport_reset_hb_timer(struct sctp_transport *transport)
190 {
191 	unsigned long expires;
192 
193 	/* When a data chunk is sent, reset the heartbeat interval.  */
194 	expires = jiffies + sctp_transport_timeout(transport);
195 	if ((time_before(transport->hb_timer.expires, expires) ||
196 	     !timer_pending(&transport->hb_timer)) &&
197 	    !mod_timer(&transport->hb_timer,
198 		       expires + prandom_u32_max(transport->rto)))
199 		sctp_transport_hold(transport);
200 }
201 
sctp_transport_reset_reconf_timer(struct sctp_transport * transport)202 void 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 
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  */
sctp_transport_set_owner(struct sctp_transport * transport,struct sctp_association * asoc)214 void 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. */
sctp_transport_pmtu(struct sctp_transport * transport,struct sock * sk)222 void sctp_transport_pmtu(struct sctp_transport *transport, struct sock *sk)
223 {
224 	/* If we don't have a fresh route, look one up */
225 	if (!transport->dst || transport->dst->obsolete) {
226 		sctp_transport_dst_release(transport);
227 		transport->af_specific->get_dst(transport, &transport->saddr,
228 						&transport->fl, sk);
229 	}
230 
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 
240 	if (transport->dst)
241 		transport->pathmtu = sctp_dst_mtu(transport->dst);
242 	else
243 		transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
244 }
245 
sctp_transport_update_pmtu(struct sctp_transport * t,u32 pmtu)246 bool sctp_transport_update_pmtu(struct sctp_transport *t, u32 pmtu)
247 {
248 	struct dst_entry *dst = sctp_transport_dst_check(t);
249 	struct sock *sk = t->asoc->base.sk;
250 	bool change = true;
251 
252 	if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
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;
257 	}
258 	pmtu = SCTP_TRUNC4(pmtu);
259 
260 	if (dst) {
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);
266 		dst->ops->update_pmtu(dst, sk, NULL, pmtu, true);
267 		pf->to_sk_daddr(&addr, sk);
268 
269 		dst = sctp_transport_dst_check(t);
270 	}
271 
272 	if (!dst) {
273 		t->af_specific->get_dst(t, &t->saddr, &t->fl, sk);
274 		dst = t->dst;
275 	}
276 
277 	if (dst) {
278 		/* Re-fetch, as under layers may have a higher minimum size */
279 		pmtu = sctp_dst_mtu(dst);
280 		change = t->pathmtu != pmtu;
281 	}
282 	t->pathmtu = pmtu;
283 
284 	return change;
285 }
286 
287 /* Caches the dst entry and source address for a transport's destination
288  * address.
289  */
sctp_transport_route(struct sctp_transport * transport,union sctp_addr * saddr,struct sctp_sock * opt)290 void 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;
295 
296 	sctp_transport_dst_release(transport);
297 	af->get_dst(transport, saddr, &transport->fl, sctp_opt2sk(opt));
298 
299 	if (saddr)
300 		memcpy(&transport->saddr, saddr, sizeof(union sctp_addr));
301 	else
302 		af->get_saddr(opt, transport, &transport->fl);
303 
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);
312 }
313 
314 /* Hold a reference to a transport.  */
sctp_transport_hold(struct sctp_transport * transport)315 int sctp_transport_hold(struct sctp_transport *transport)
316 {
317 	return refcount_inc_not_zero(&transport->refcnt);
318 }
319 
320 /* Release a reference to a transport and clean up
321  * if there are no more references.
322  */
sctp_transport_put(struct sctp_transport * transport)323 void sctp_transport_put(struct sctp_transport *transport)
324 {
325 	if (refcount_dec_and_test(&transport->refcnt))
326 		sctp_transport_destroy(transport);
327 }
328 
329 /* Update transport's RTO based on the newly calculated RTT. */
sctp_transport_update_rto(struct sctp_transport * tp,__u32 rtt)330 void sctp_transport_update_rto(struct sctp_transport *tp, __u32 rtt)
331 {
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);
335 
336 	if (tp->rttvar || tp->srtt) {
337 		struct net *net = tp->asoc->base.net;
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 		 */
349 		tp->rttvar = tp->rttvar - (tp->rttvar >> net->sctp.rto_beta)
350 			+ (((__u32)abs((__s64)tp->srtt - (__s64)rtt)) >> net->sctp.rto_beta);
351 		tp->srtt = tp->srtt - (tp->srtt >> net->sctp.rto_alpha)
352 			+ (rtt >> net->sctp.rto_alpha);
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 
382 	sctp_max_rto(tp->asoc, tp);
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 
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);
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  */
sctp_transport_raise_cwnd(struct sctp_transport * transport,__u32 sack_ctsn,__u32 bytes_acked)397 void sctp_transport_raise_cwnd(struct sctp_transport *transport,
398 			       __u32 sack_ctsn, __u32 bytes_acked)
399 {
400 	struct sctp_association *asoc = transport->asoc;
401 	__u32 cwnd, ssthresh, flight_size, pba, pmtu;
402 
403 	cwnd = transport->cwnd;
404 	flight_size = transport->flight_size;
405 
406 	/* See if we need to exit Fast Recovery first */
407 	if (asoc->fast_recovery &&
408 	    TSN_lte(asoc->fast_recovery_exit, sack_ctsn))
409 		asoc->fast_recovery = 0;
410 
411 	ssthresh = transport->ssthresh;
412 	pba = transport->partial_bytes_acked;
413 	pmtu = transport->asoc->pathmtu;
414 
415 	if (cwnd <= ssthresh) {
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].
429 		 */
430 		if (asoc->fast_recovery)
431 			return;
432 
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 
441 		if (bytes_acked > pmtu)
442 			cwnd += pmtu;
443 		else
444 			cwnd += bytes_acked;
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);
450 	} else {
451 		/* RFC 2960 7.2.2 Whenever cwnd is greater than ssthresh,
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)
457 		 *
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 		 *
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)
472 		 */
473 		pba += bytes_acked;
474 		if (pba > cwnd && flight_size < cwnd)
475 			pba = cwnd;
476 		if (pba >= cwnd && flight_size >= cwnd) {
477 			pba = pba - cwnd;
478 			cwnd += pmtu;
479 		}
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);
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  */
sctp_transport_lower_cwnd(struct sctp_transport * transport,enum sctp_lower_cwnd reason)495 void sctp_transport_lower_cwnd(struct sctp_transport *transport,
496 			       enum sctp_lower_cwnd reason)
497 {
498 	struct sctp_association *asoc = transport->asoc;
499 
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,
510 					  4*asoc->pathmtu);
511 		transport->cwnd = asoc->pathmtu;
512 
513 		/* T3-rtx also clears fast recovery */
514 		asoc->fast_recovery = 0;
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.
522 		 *
523 		 * RFC 2960 7.2.3, sctpimpguide Upon detection of packet
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 		 */
530 		if (asoc->fast_recovery)
531 			return;
532 
533 		/* Mark Fast recovery */
534 		asoc->fast_recovery = 1;
535 		asoc->fast_recovery_exit = asoc->next_tsn - 1;
536 
537 		transport->ssthresh = max(transport->cwnd/2,
538 					  4*asoc->pathmtu);
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 		 */
555 		if (time_after(jiffies, transport->last_time_ecne_reduced +
556 					transport->rtt)) {
557 			transport->ssthresh = max(transport->cwnd/2,
558 						  4*asoc->pathmtu);
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 		 */
573 		transport->cwnd = max(transport->cwnd/2,
574 					 4*asoc->pathmtu);
575 		/* RFC 4960 Errata 3.27.2: also adjust sshthresh */
576 		transport->ssthresh = transport->cwnd;
577 		break;
578 	}
579 
580 	transport->partial_bytes_acked = 0;
581 
582 	pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d\n",
583 		 __func__, transport, reason, transport->cwnd,
584 		 transport->ssthresh);
585 }
586 
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 
sctp_transport_burst_limited(struct sctp_transport * t)597 void 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 
603 	if (t->burst_limited || asoc->max_burst == 0)
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  */
sctp_transport_burst_reset(struct sctp_transport * t)616 void 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 
624 /* What is the next timeout value for this transport? */
sctp_transport_timeout(struct sctp_transport * trans)625 unsigned long sctp_transport_timeout(struct sctp_transport *trans)
626 {
627 	/* RTO + timer slack +/- 50% of RTO */
628 	unsigned long timeout = trans->rto >> 1;
629 
630 	if (trans->state != SCTP_UNCONFIRMED &&
631 	    trans->state != SCTP_PF)
632 		timeout += trans->hbinterval;
633 
634 	return max_t(unsigned long, timeout, HZ / 5);
635 }
636 
637 /* Reset transport variables to their initial values */
sctp_transport_reset(struct sctp_transport * t)638 void 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));
648 	t->burst_limited = 0;
649 	t->ssthresh = asoc->peer.i.a_rwnd;
650 	t->rto = asoc->rto_initial;
651 	sctp_max_rto(asoc, t);
652 	t->rtt = 0;
653 	t->srtt = 0;
654 	t->rttvar = 0;
655 
656 	/* Reset these additional variables so that we have a clean slate. */
657 	t->partial_bytes_acked = 0;
658 	t->flight_size = 0;
659 	t->error_count = 0;
660 	t->rto_pending = 0;
661 	t->hb_sent = 0;
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 }
669 
670 /* Schedule retransmission on the given transport */
sctp_transport_immediate_rtx(struct sctp_transport * t)671 void sctp_transport_immediate_rtx(struct sctp_transport *t)
672 {
673 	/* Stop pending T3_rtx_timer */
674 	if (del_timer(&t->T3_rtx_timer))
675 		sctp_transport_put(t);
676 
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 	}
682 }
683 
684 /* Drop dst */
sctp_transport_dst_release(struct sctp_transport * t)685 void 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 */
sctp_transport_dst_confirm(struct sctp_transport * t)693 void sctp_transport_dst_confirm(struct sctp_transport *t)
694 {
695 	t->dst_pending_confirm = 1;
696 }
697