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 = jiffies;
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 transport->dead = 1;
136
137
138 if (del_timer(&transport->hb_timer))
139 sctp_transport_put(transport);
140
141
142
143
144
145
146 if (del_timer(&transport->T3_rtx_timer))
147 sctp_transport_put(transport);
148
149
150 if (del_timer(&transport->proto_unreach_timer))
151 sctp_association_put(transport->asoc);
152
153 sctp_transport_put(transport);
154}
155
156static void sctp_transport_destroy_rcu(struct rcu_head *head)
157{
158 struct sctp_transport *transport;
159
160 transport = container_of(head, struct sctp_transport, rcu);
161
162 dst_release(transport->dst);
163 kfree(transport);
164 SCTP_DBG_OBJCNT_DEC(transport);
165}
166
167
168
169
170static void sctp_transport_destroy(struct sctp_transport *transport)
171{
172 if (unlikely(!transport->dead)) {
173 WARN(1, "Attempt to destroy undead transport %p!\n", transport);
174 return;
175 }
176
177 sctp_packet_free(&transport->packet);
178
179 if (transport->asoc)
180 sctp_association_put(transport->asoc);
181
182 call_rcu(&transport->rcu, sctp_transport_destroy_rcu);
183}
184
185
186
187
188void sctp_transport_reset_timers(struct sctp_transport *transport)
189{
190
191
192
193
194
195
196
197
198 if (!timer_pending(&transport->T3_rtx_timer))
199 if (!mod_timer(&transport->T3_rtx_timer,
200 jiffies + transport->rto))
201 sctp_transport_hold(transport);
202
203
204 if (!mod_timer(&transport->hb_timer,
205 sctp_transport_timeout(transport)))
206 sctp_transport_hold(transport);
207}
208
209
210
211
212
213void sctp_transport_set_owner(struct sctp_transport *transport,
214 struct sctp_association *asoc)
215{
216 transport->asoc = asoc;
217 sctp_association_hold(asoc);
218}
219
220
221void sctp_transport_pmtu(struct sctp_transport *transport, struct sock *sk)
222{
223
224 if (!transport->dst || transport->dst->obsolete) {
225 dst_release(transport->dst);
226 transport->af_specific->get_dst(transport, &transport->saddr,
227 &transport->fl, sk);
228 }
229
230 if (transport->dst) {
231 transport->pathmtu = dst_mtu(transport->dst);
232 } else
233 transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
234}
235
236void sctp_transport_update_pmtu(struct sock *sk, struct sctp_transport *t, u32 pmtu)
237{
238 struct dst_entry *dst;
239
240 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
241 pr_warn("%s: Reported pmtu %d too low, using default minimum of %d\n",
242 __func__, pmtu,
243 SCTP_DEFAULT_MINSEGMENT);
244
245
246
247 t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
248 } else {
249 t->pathmtu = pmtu;
250 }
251
252 dst = sctp_transport_dst_check(t);
253 if (!dst)
254 t->af_specific->get_dst(t, &t->saddr, &t->fl, sk);
255
256 if (dst) {
257 dst->ops->update_pmtu(dst, sk, NULL, pmtu);
258
259 dst = sctp_transport_dst_check(t);
260 if (!dst)
261 t->af_specific->get_dst(t, &t->saddr, &t->fl, sk);
262 }
263}
264
265
266
267
268void sctp_transport_route(struct sctp_transport *transport,
269 union sctp_addr *saddr, struct sctp_sock *opt)
270{
271 struct sctp_association *asoc = transport->asoc;
272 struct sctp_af *af = transport->af_specific;
273
274 af->get_dst(transport, saddr, &transport->fl, sctp_opt2sk(opt));
275
276 if (saddr)
277 memcpy(&transport->saddr, saddr, sizeof(union sctp_addr));
278 else
279 af->get_saddr(opt, transport, &transport->fl);
280
281 if ((transport->param_flags & SPP_PMTUD_DISABLE) && transport->pathmtu) {
282 return;
283 }
284 if (transport->dst) {
285 transport->pathmtu = dst_mtu(transport->dst);
286
287
288
289
290 if (asoc && (!asoc->peer.primary_path ||
291 (transport == asoc->peer.active_path)))
292 opt->pf->af->to_sk_saddr(&transport->saddr,
293 asoc->base.sk);
294 } else
295 transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
296}
297
298
299void sctp_transport_hold(struct sctp_transport *transport)
300{
301 atomic_inc(&transport->refcnt);
302}
303
304
305
306
307void sctp_transport_put(struct sctp_transport *transport)
308{
309 if (atomic_dec_and_test(&transport->refcnt))
310 sctp_transport_destroy(transport);
311}
312
313
314void sctp_transport_update_rto(struct sctp_transport *tp, __u32 rtt)
315{
316 if (unlikely(!tp->rto_pending))
317
318 pr_debug("%s: rto_pending not set on transport %p!\n", __func__, tp);
319
320 if (tp->rttvar || tp->srtt) {
321 struct net *net = sock_net(tp->asoc->base.sk);
322
323
324
325
326
327
328
329
330
331
332
333 tp->rttvar = tp->rttvar - (tp->rttvar >> net->sctp.rto_beta)
334 + (((__u32)abs64((__s64)tp->srtt - (__s64)rtt)) >> net->sctp.rto_beta);
335 tp->srtt = tp->srtt - (tp->srtt >> net->sctp.rto_alpha)
336 + (rtt >> net->sctp.rto_alpha);
337 } else {
338
339
340
341 tp->srtt = rtt;
342 tp->rttvar = rtt >> 1;
343 }
344
345
346
347
348 if (tp->rttvar == 0)
349 tp->rttvar = SCTP_CLOCK_GRANULARITY;
350
351
352 tp->rto = tp->srtt + (tp->rttvar << 2);
353
354
355
356
357 if (tp->rto < tp->asoc->rto_min)
358 tp->rto = tp->asoc->rto_min;
359
360
361
362
363 if (tp->rto > tp->asoc->rto_max)
364 tp->rto = tp->asoc->rto_max;
365
366 sctp_max_rto(tp->asoc, tp);
367 tp->rtt = rtt;
368
369
370
371
372 tp->rto_pending = 0;
373
374 pr_debug("%s: transport:%p, rtt:%d, srtt:%d rttvar:%d, rto:%ld\n",
375 __func__, tp, rtt, tp->srtt, tp->rttvar, tp->rto);
376}
377
378
379
380
381void sctp_transport_raise_cwnd(struct sctp_transport *transport,
382 __u32 sack_ctsn, __u32 bytes_acked)
383{
384 struct sctp_association *asoc = transport->asoc;
385 __u32 cwnd, ssthresh, flight_size, pba, pmtu;
386
387 cwnd = transport->cwnd;
388 flight_size = transport->flight_size;
389
390
391 if (asoc->fast_recovery &&
392 TSN_lte(asoc->fast_recovery_exit, sack_ctsn))
393 asoc->fast_recovery = 0;
394
395
396
397
398
399 if (TSN_lte(sack_ctsn, transport->asoc->ctsn_ack_point) ||
400 (flight_size < cwnd))
401 return;
402
403 ssthresh = transport->ssthresh;
404 pba = transport->partial_bytes_acked;
405 pmtu = transport->asoc->pathmtu;
406
407 if (cwnd <= ssthresh) {
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422 if (asoc->fast_recovery)
423 return;
424
425 if (bytes_acked > pmtu)
426 cwnd += pmtu;
427 else
428 cwnd += bytes_acked;
429
430 pr_debug("%s: slow start: transport:%p, bytes_acked:%d, "
431 "cwnd:%d, ssthresh:%d, flight_size:%d, pba:%d\n",
432 __func__, transport, bytes_acked, cwnd, ssthresh,
433 flight_size, pba);
434 } else {
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449 pba += bytes_acked;
450 if (pba >= cwnd) {
451 cwnd += pmtu;
452 pba = ((cwnd < pba) ? (pba - cwnd) : 0);
453 }
454
455 pr_debug("%s: congestion avoidance: transport:%p, "
456 "bytes_acked:%d, cwnd:%d, ssthresh:%d, "
457 "flight_size:%d, pba:%d\n", __func__,
458 transport, bytes_acked, cwnd, ssthresh,
459 flight_size, pba);
460 }
461
462 transport->cwnd = cwnd;
463 transport->partial_bytes_acked = pba;
464}
465
466
467
468
469void sctp_transport_lower_cwnd(struct sctp_transport *transport,
470 sctp_lower_cwnd_t reason)
471{
472 struct sctp_association *asoc = transport->asoc;
473
474 switch (reason) {
475 case SCTP_LOWER_CWND_T3_RTX:
476
477
478
479
480
481
482
483 transport->ssthresh = max(transport->cwnd/2,
484 4*asoc->pathmtu);
485 transport->cwnd = asoc->pathmtu;
486
487
488 asoc->fast_recovery = 0;
489 break;
490
491 case SCTP_LOWER_CWND_FAST_RTX:
492
493
494
495
496
497
498
499
500
501
502
503
504 if (asoc->fast_recovery)
505 return;
506
507
508 asoc->fast_recovery = 1;
509 asoc->fast_recovery_exit = asoc->next_tsn - 1;
510
511 transport->ssthresh = max(transport->cwnd/2,
512 4*asoc->pathmtu);
513 transport->cwnd = transport->ssthresh;
514 break;
515
516 case SCTP_LOWER_CWND_ECNE:
517
518
519
520
521
522
523
524
525
526
527
528
529 if (time_after(jiffies, transport->last_time_ecne_reduced +
530 transport->rtt)) {
531 transport->ssthresh = max(transport->cwnd/2,
532 4*asoc->pathmtu);
533 transport->cwnd = transport->ssthresh;
534 transport->last_time_ecne_reduced = jiffies;
535 }
536 break;
537
538 case SCTP_LOWER_CWND_INACTIVE:
539
540
541
542
543
544
545
546
547 transport->cwnd = max(transport->cwnd/2,
548 4*asoc->pathmtu);
549 break;
550 }
551
552 transport->partial_bytes_acked = 0;
553
554 pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d\n",
555 __func__, transport, reason, transport->cwnd,
556 transport->ssthresh);
557}
558
559
560
561
562
563
564
565
566
567
568
569void sctp_transport_burst_limited(struct sctp_transport *t)
570{
571 struct sctp_association *asoc = t->asoc;
572 u32 old_cwnd = t->cwnd;
573 u32 max_burst_bytes;
574
575 if (t->burst_limited || asoc->max_burst == 0)
576 return;
577
578 max_burst_bytes = t->flight_size + (asoc->max_burst * asoc->pathmtu);
579 if (max_burst_bytes < old_cwnd) {
580 t->cwnd = max_burst_bytes;
581 t->burst_limited = old_cwnd;
582 }
583}
584
585
586
587
588void sctp_transport_burst_reset(struct sctp_transport *t)
589{
590 if (t->burst_limited) {
591 t->cwnd = t->burst_limited;
592 t->burst_limited = 0;
593 }
594}
595
596
597unsigned long sctp_transport_timeout(struct sctp_transport *t)
598{
599 unsigned long timeout;
600 timeout = t->rto + sctp_jitter(t->rto);
601 if ((t->state != SCTP_UNCONFIRMED) &&
602 (t->state != SCTP_PF))
603 timeout += t->hbinterval;
604 timeout += jiffies;
605 return timeout;
606}
607
608
609void sctp_transport_reset(struct sctp_transport *t)
610{
611 struct sctp_association *asoc = t->asoc;
612
613
614
615
616
617
618 t->cwnd = min(4*asoc->pathmtu, max_t(__u32, 2*asoc->pathmtu, 4380));
619 t->burst_limited = 0;
620 t->ssthresh = asoc->peer.i.a_rwnd;
621 t->rto = asoc->rto_initial;
622 sctp_max_rto(asoc, t);
623 t->rtt = 0;
624 t->srtt = 0;
625 t->rttvar = 0;
626
627
628
629
630 t->partial_bytes_acked = 0;
631 t->flight_size = 0;
632 t->error_count = 0;
633 t->rto_pending = 0;
634 t->hb_sent = 0;
635
636
637 t->cacc.changeover_active = 0;
638 t->cacc.cycling_changeover = 0;
639 t->cacc.next_tsn_at_change = 0;
640 t->cacc.cacc_saw_newack = 0;
641}
642
643
644void sctp_transport_immediate_rtx(struct sctp_transport *t)
645{
646
647 if (del_timer(&t->T3_rtx_timer))
648 sctp_transport_put(t);
649
650 sctp_retransmit(&t->asoc->outqueue, t, SCTP_RTXR_T3_RTX);
651 if (!timer_pending(&t->T3_rtx_timer)) {
652 if (!mod_timer(&t->T3_rtx_timer, jiffies + t->rto))
653 sctp_transport_hold(t);
654 }
655 return;
656}
657