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