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