This source file includes following definitions.
- sctp_transport_init
- sctp_transport_new
- sctp_transport_free
- sctp_transport_destroy_rcu
- sctp_transport_destroy
- sctp_transport_reset_t3_rtx
- sctp_transport_reset_hb_timer
- sctp_transport_reset_reconf_timer
- sctp_transport_set_owner
- sctp_transport_pmtu
- sctp_transport_update_pmtu
- sctp_transport_route
- sctp_transport_hold
- sctp_transport_put
- sctp_transport_update_rto
- sctp_transport_raise_cwnd
- sctp_transport_lower_cwnd
- sctp_transport_burst_limited
- sctp_transport_burst_reset
- sctp_transport_timeout
- sctp_transport_reset
- sctp_transport_immediate_rtx
- sctp_transport_dst_release
- sctp_transport_dst_confirm
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 #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
38
39
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
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
53
54
55
56
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
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
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
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
115
116
117 void sctp_transport_free(struct sctp_transport *transport)
118 {
119
120 if (del_timer(&transport->hb_timer))
121 sctp_transport_put(transport);
122
123
124
125
126
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
135 if (del_timer(&transport->proto_unreach_timer))
136 sctp_association_put(transport->asoc);
137
138 sctp_transport_put(transport);
139 }
140
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
153
154
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
171
172
173 void sctp_transport_reset_t3_rtx(struct sctp_transport *transport)
174 {
175
176
177
178
179
180
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
189 void sctp_transport_reset_hb_timer(struct sctp_transport *transport)
190 {
191 unsigned long expires;
192
193
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
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
211
212
213
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
222 void sctp_transport_pmtu(struct sctp_transport *transport, struct sock *sk)
223 {
224
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
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
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
279 pmtu = sctp_dst_mtu(dst);
280 change = t->pathmtu != pmtu;
281 }
282 t->pathmtu = pmtu;
283
284 return change;
285 }
286
287
288
289
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
307
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
315 int sctp_transport_hold(struct sctp_transport *transport)
316 {
317 return refcount_inc_not_zero(&transport->refcnt);
318 }
319
320
321
322
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
330 void sctp_transport_update_rto(struct sctp_transport *tp, __u32 rtt)
331 {
332 if (unlikely(!tp->rto_pending))
333
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 = sock_net(tp->asoc->base.sk);
338
339
340
341
342
343
344
345
346
347
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
355
356
357 tp->srtt = rtt;
358 tp->rttvar = rtt >> 1;
359 }
360
361
362
363
364 if (tp->rttvar == 0)
365 tp->rttvar = SCTP_CLOCK_GRANULARITY;
366
367
368 tp->rto = tp->srtt + (tp->rttvar << 2);
369
370
371
372
373 if (tp->rto < tp->asoc->rto_min)
374 tp->rto = tp->asoc->rto_min;
375
376
377
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
386
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
395
396
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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430 if (asoc->fast_recovery)
431 return;
432
433
434
435
436
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
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
493
494
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
503
504
505
506
507
508
509 transport->ssthresh = max(transport->cwnd/2,
510 4*asoc->pathmtu);
511 transport->cwnd = asoc->pathmtu;
512
513
514 asoc->fast_recovery = 0;
515 break;
516
517 case SCTP_LOWER_CWND_FAST_RTX:
518
519
520
521
522
523
524
525
526
527
528
529
530 if (asoc->fast_recovery)
531 return;
532
533
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
544
545
546
547
548
549
550
551
552
553
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
566
567
568
569
570
571
572
573 transport->cwnd = max(transport->cwnd/2,
574 4*asoc->pathmtu);
575
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
588
589
590
591
592
593
594
595
596
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
614
615
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
625 unsigned long sctp_transport_timeout(struct sctp_transport *trans)
626 {
627
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
638 void sctp_transport_reset(struct sctp_transport *t)
639 {
640 struct sctp_association *asoc = t->asoc;
641
642
643
644
645
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
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
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
671 void sctp_transport_immediate_rtx(struct sctp_transport *t)
672 {
673
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
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
693 void sctp_transport_dst_confirm(struct sctp_transport *t)
694 {
695 t->dst_pending_confirm = 1;
696 }