1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45
46#include <linux/slab.h>
47#include <linux/types.h>
48#include <linux/random.h>
49#include <net/sctp/sctp.h>
50#include <net/sctp/sm.h>
51
52
53
54
55static struct sctp_transport *sctp_transport_init(struct net *net,
56 struct sctp_transport *peer,
57 const union sctp_addr *addr,
58 gfp_t gfp)
59{
60
61 peer->ipaddr = *addr;
62 peer->af_specific = sctp_get_af_specific(addr->sa.sa_family);
63 memset(&peer->saddr, 0, sizeof(union sctp_addr));
64
65 peer->sack_generation = 0;
66
67
68
69
70
71
72
73 peer->rto = msecs_to_jiffies(net->sctp.rto_initial);
74
75 peer->last_time_heard = ktime_set(0, 0);
76 peer->last_time_ecne_reduced = jiffies;
77
78 peer->param_flags = SPP_HB_DISABLE |
79 SPP_PMTUD_ENABLE |
80 SPP_SACKDELAY_ENABLE;
81
82
83 peer->pathmaxrxt = net->sctp.max_retrans_path;
84 peer->pf_retrans = net->sctp.pf_retrans;
85
86 INIT_LIST_HEAD(&peer->transmitted);
87 INIT_LIST_HEAD(&peer->send_ready);
88 INIT_LIST_HEAD(&peer->transports);
89
90 setup_timer(&peer->T3_rtx_timer, sctp_generate_t3_rtx_event,
91 (unsigned long)peer);
92 setup_timer(&peer->hb_timer, sctp_generate_heartbeat_event,
93 (unsigned long)peer);
94 setup_timer(&peer->proto_unreach_timer,
95 sctp_generate_proto_unreach_event, (unsigned long)peer);
96
97
98 get_random_bytes(&peer->hb_nonce, sizeof(peer->hb_nonce));
99
100 atomic_set(&peer->refcnt, 1);
101
102 return peer;
103}
104
105
106struct sctp_transport *sctp_transport_new(struct net *net,
107 const union sctp_addr *addr,
108 gfp_t gfp)
109{
110 struct sctp_transport *transport;
111
112 transport = kzalloc(sizeof(*transport), gfp);
113 if (!transport)
114 goto fail;
115
116 if (!sctp_transport_init(net, transport, addr, gfp))
117 goto fail_init;
118
119 SCTP_DBG_OBJCNT_INC(transport);
120
121 return transport;
122
123fail_init:
124 kfree(transport);
125
126fail:
127 return NULL;
128}
129
130
131
132
133void sctp_transport_free(struct sctp_transport *transport)
134{
135
136 if (del_timer(&transport->hb_timer))
137 sctp_transport_put(transport);
138
139
140
141
142
143
144 if (del_timer(&transport->T3_rtx_timer))
145 sctp_transport_put(transport);
146
147
148 if (del_timer(&transport->proto_unreach_timer))
149 sctp_association_put(transport->asoc);
150
151 sctp_transport_put(transport);
152}
153
154static void sctp_transport_destroy_rcu(struct rcu_head *head)
155{
156 struct sctp_transport *transport;
157
158 transport = container_of(head, struct sctp_transport, rcu);
159
160 dst_release(transport->dst);
161 kfree(transport);
162 SCTP_DBG_OBJCNT_DEC(transport);
163}
164
165
166
167
168static void sctp_transport_destroy(struct sctp_transport *transport)
169{
170 if (unlikely(atomic_read(&transport->refcnt))) {
171 WARN(1, "Attempt to destroy undead transport %p!\n", transport);
172 return;
173 }
174
175 sctp_packet_free(&transport->packet);
176
177 if (transport->asoc)
178 sctp_association_put(transport->asoc);
179
180 call_rcu(&transport->rcu, sctp_transport_destroy_rcu);
181}
182
183
184
185
186void sctp_transport_reset_t3_rtx(struct sctp_transport *transport)
187{
188
189
190
191
192
193
194
195
196 if (!timer_pending(&transport->T3_rtx_timer))
197 if (!mod_timer(&transport->T3_rtx_timer,
198 jiffies + transport->rto))
199 sctp_transport_hold(transport);
200}
201
202void sctp_transport_reset_hb_timer(struct sctp_transport *transport)
203{
204 unsigned long expires;
205
206
207 expires = jiffies + sctp_transport_timeout(transport);
208 if (time_before(transport->hb_timer.expires, expires) &&
209 !mod_timer(&transport->hb_timer,
210 expires + prandom_u32_max(transport->rto)))
211 sctp_transport_hold(transport);
212}
213
214
215
216
217
218void sctp_transport_set_owner(struct sctp_transport *transport,
219 struct sctp_association *asoc)
220{
221 transport->asoc = asoc;
222 sctp_association_hold(asoc);
223}
224
225
226void sctp_transport_pmtu(struct sctp_transport *transport, struct sock *sk)
227{
228
229 if (!transport->dst || transport->dst->obsolete) {
230 dst_release(transport->dst);
231 transport->af_specific->get_dst(transport, &transport->saddr,
232 &transport->fl, sk);
233 }
234
235 if (transport->dst) {
236 transport->pathmtu = SCTP_TRUNC4(dst_mtu(transport->dst));
237 } else
238 transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
239}
240
241void sctp_transport_update_pmtu(struct sock *sk, struct sctp_transport *t, u32 pmtu)
242{
243 struct dst_entry *dst;
244
245 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
246 pr_warn("%s: Reported pmtu %d too low, using default minimum of %d\n",
247 __func__, pmtu,
248 SCTP_DEFAULT_MINSEGMENT);
249
250
251
252 t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
253 } else {
254 t->pathmtu = pmtu;
255 }
256
257 dst = sctp_transport_dst_check(t);
258 if (!dst)
259 t->af_specific->get_dst(t, &t->saddr, &t->fl, sk);
260
261 if (dst) {
262 dst->ops->update_pmtu(dst, sk, NULL, pmtu);
263
264 dst = sctp_transport_dst_check(t);
265 if (!dst)
266 t->af_specific->get_dst(t, &t->saddr, &t->fl, sk);
267 }
268}
269
270
271
272
273void sctp_transport_route(struct sctp_transport *transport,
274 union sctp_addr *saddr, struct sctp_sock *opt)
275{
276 struct sctp_association *asoc = transport->asoc;
277 struct sctp_af *af = transport->af_specific;
278
279 af->get_dst(transport, saddr, &transport->fl, sctp_opt2sk(opt));
280
281 if (saddr)
282 memcpy(&transport->saddr, saddr, sizeof(union sctp_addr));
283 else
284 af->get_saddr(opt, transport, &transport->fl);
285
286 if ((transport->param_flags & SPP_PMTUD_DISABLE) && transport->pathmtu) {
287 return;
288 }
289 if (transport->dst) {
290 transport->pathmtu = SCTP_TRUNC4(dst_mtu(transport->dst));
291
292
293
294
295 if (asoc && (!asoc->peer.primary_path ||
296 (transport == asoc->peer.active_path)))
297 opt->pf->to_sk_saddr(&transport->saddr,
298 asoc->base.sk);
299 } else
300 transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
301}
302
303
304int sctp_transport_hold(struct sctp_transport *transport)
305{
306 return atomic_add_unless(&transport->refcnt, 1, 0);
307}
308
309
310
311
312void sctp_transport_put(struct sctp_transport *transport)
313{
314 if (atomic_dec_and_test(&transport->refcnt))
315 sctp_transport_destroy(transport);
316}
317
318
319void sctp_transport_update_rto(struct sctp_transport *tp, __u32 rtt)
320{
321 if (unlikely(!tp->rto_pending))
322
323 pr_debug("%s: rto_pending not set on transport %p!\n", __func__, tp);
324
325 if (tp->rttvar || tp->srtt) {
326 struct net *net = sock_net(tp->asoc->base.sk);
327
328
329
330
331
332
333
334
335
336
337
338 tp->rttvar = tp->rttvar - (tp->rttvar >> net->sctp.rto_beta)
339 + (((__u32)abs((__s64)tp->srtt - (__s64)rtt)) >> net->sctp.rto_beta);
340 tp->srtt = tp->srtt - (tp->srtt >> net->sctp.rto_alpha)
341 + (rtt >> net->sctp.rto_alpha);
342 } else {
343
344
345
346 tp->srtt = rtt;
347 tp->rttvar = rtt >> 1;
348 }
349
350
351
352
353 if (tp->rttvar == 0)
354 tp->rttvar = SCTP_CLOCK_GRANULARITY;
355
356
357 tp->rto = tp->srtt + (tp->rttvar << 2);
358
359
360
361
362 if (tp->rto < tp->asoc->rto_min)
363 tp->rto = tp->asoc->rto_min;
364
365
366
367
368 if (tp->rto > tp->asoc->rto_max)
369 tp->rto = tp->asoc->rto_max;
370
371 sctp_max_rto(tp->asoc, tp);
372 tp->rtt = rtt;
373
374
375
376
377 tp->rto_pending = 0;
378
379 pr_debug("%s: transport:%p, rtt:%d, srtt:%d rttvar:%d, rto:%ld\n",
380 __func__, tp, rtt, tp->srtt, tp->rttvar, tp->rto);
381}
382
383
384
385
386void sctp_transport_raise_cwnd(struct sctp_transport *transport,
387 __u32 sack_ctsn, __u32 bytes_acked)
388{
389 struct sctp_association *asoc = transport->asoc;
390 __u32 cwnd, ssthresh, flight_size, pba, pmtu;
391
392 cwnd = transport->cwnd;
393 flight_size = transport->flight_size;
394
395
396 if (asoc->fast_recovery &&
397 TSN_lte(asoc->fast_recovery_exit, sack_ctsn))
398 asoc->fast_recovery = 0;
399
400
401
402
403
404 if (TSN_lte(sack_ctsn, transport->asoc->ctsn_ack_point) ||
405 (flight_size < cwnd))
406 return;
407
408 ssthresh = transport->ssthresh;
409 pba = transport->partial_bytes_acked;
410 pmtu = transport->asoc->pathmtu;
411
412 if (cwnd <= ssthresh) {
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427 if (asoc->fast_recovery)
428 return;
429
430 if (bytes_acked > pmtu)
431 cwnd += pmtu;
432 else
433 cwnd += bytes_acked;
434
435 pr_debug("%s: slow start: transport:%p, bytes_acked:%d, "
436 "cwnd:%d, ssthresh:%d, flight_size:%d, pba:%d\n",
437 __func__, transport, bytes_acked, cwnd, ssthresh,
438 flight_size, pba);
439 } else {
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454 pba += bytes_acked;
455 if (pba >= cwnd) {
456 cwnd += pmtu;
457 pba = ((cwnd < pba) ? (pba - cwnd) : 0);
458 }
459
460 pr_debug("%s: congestion avoidance: transport:%p, "
461 "bytes_acked:%d, cwnd:%d, ssthresh:%d, "
462 "flight_size:%d, pba:%d\n", __func__,
463 transport, bytes_acked, cwnd, ssthresh,
464 flight_size, pba);
465 }
466
467 transport->cwnd = cwnd;
468 transport->partial_bytes_acked = pba;
469}
470
471
472
473
474void sctp_transport_lower_cwnd(struct sctp_transport *transport,
475 sctp_lower_cwnd_t reason)
476{
477 struct sctp_association *asoc = transport->asoc;
478
479 switch (reason) {
480 case SCTP_LOWER_CWND_T3_RTX:
481
482
483
484
485
486
487
488 transport->ssthresh = max(transport->cwnd/2,
489 4*asoc->pathmtu);
490 transport->cwnd = asoc->pathmtu;
491
492
493 asoc->fast_recovery = 0;
494 break;
495
496 case SCTP_LOWER_CWND_FAST_RTX:
497
498
499
500
501
502
503
504
505
506
507
508
509 if (asoc->fast_recovery)
510 return;
511
512
513 asoc->fast_recovery = 1;
514 asoc->fast_recovery_exit = asoc->next_tsn - 1;
515
516 transport->ssthresh = max(transport->cwnd/2,
517 4*asoc->pathmtu);
518 transport->cwnd = transport->ssthresh;
519 break;
520
521 case SCTP_LOWER_CWND_ECNE:
522
523
524
525
526
527
528
529
530
531
532
533
534 if (time_after(jiffies, transport->last_time_ecne_reduced +
535 transport->rtt)) {
536 transport->ssthresh = max(transport->cwnd/2,
537 4*asoc->pathmtu);
538 transport->cwnd = transport->ssthresh;
539 transport->last_time_ecne_reduced = jiffies;
540 }
541 break;
542
543 case SCTP_LOWER_CWND_INACTIVE:
544
545
546
547
548
549
550
551
552 transport->cwnd = max(transport->cwnd/2,
553 4*asoc->pathmtu);
554 break;
555 }
556
557 transport->partial_bytes_acked = 0;
558
559 pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d\n",
560 __func__, transport, reason, transport->cwnd,
561 transport->ssthresh);
562}
563
564
565
566
567
568
569
570
571
572
573
574void sctp_transport_burst_limited(struct sctp_transport *t)
575{
576 struct sctp_association *asoc = t->asoc;
577 u32 old_cwnd = t->cwnd;
578 u32 max_burst_bytes;
579
580 if (t->burst_limited || asoc->max_burst == 0)
581 return;
582
583 max_burst_bytes = t->flight_size + (asoc->max_burst * asoc->pathmtu);
584 if (max_burst_bytes < old_cwnd) {
585 t->cwnd = max_burst_bytes;
586 t->burst_limited = old_cwnd;
587 }
588}
589
590
591
592
593void sctp_transport_burst_reset(struct sctp_transport *t)
594{
595 if (t->burst_limited) {
596 t->cwnd = t->burst_limited;
597 t->burst_limited = 0;
598 }
599}
600
601
602unsigned long sctp_transport_timeout(struct sctp_transport *trans)
603{
604
605 unsigned long timeout = trans->rto >> 1;
606
607 if (trans->state != SCTP_UNCONFIRMED &&
608 trans->state != SCTP_PF)
609 timeout += trans->hbinterval;
610
611 return timeout;
612}
613
614
615void sctp_transport_reset(struct sctp_transport *t)
616{
617 struct sctp_association *asoc = t->asoc;
618
619
620
621
622
623
624 t->cwnd = min(4*asoc->pathmtu, max_t(__u32, 2*asoc->pathmtu, 4380));
625 t->burst_limited = 0;
626 t->ssthresh = asoc->peer.i.a_rwnd;
627 t->rto = asoc->rto_initial;
628 sctp_max_rto(asoc, t);
629 t->rtt = 0;
630 t->srtt = 0;
631 t->rttvar = 0;
632
633
634
635
636 t->partial_bytes_acked = 0;
637 t->flight_size = 0;
638 t->error_count = 0;
639 t->rto_pending = 0;
640 t->hb_sent = 0;
641
642
643 t->cacc.changeover_active = 0;
644 t->cacc.cycling_changeover = 0;
645 t->cacc.next_tsn_at_change = 0;
646 t->cacc.cacc_saw_newack = 0;
647}
648
649
650void sctp_transport_immediate_rtx(struct sctp_transport *t)
651{
652
653 if (del_timer(&t->T3_rtx_timer))
654 sctp_transport_put(t);
655
656 sctp_retransmit(&t->asoc->outqueue, t, SCTP_RTXR_T3_RTX);
657 if (!timer_pending(&t->T3_rtx_timer)) {
658 if (!mod_timer(&t->T3_rtx_timer, jiffies + t->rto))
659 sctp_transport_hold(t);
660 }
661}
662