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 sctp_transport *peer,
63 const union sctp_addr *addr,
64 gfp_t gfp)
65{
66
67 peer->ipaddr = *addr;
68 peer->af_specific = sctp_get_af_specific(addr->sa.sa_family);
69 memset(&peer->saddr, 0, sizeof(union sctp_addr));
70
71
72
73
74
75
76
77 peer->rto = msecs_to_jiffies(sctp_rto_initial);
78
79 peer->last_time_heard = jiffies;
80 peer->last_time_ecne_reduced = jiffies;
81
82 peer->param_flags = SPP_HB_DISABLE |
83 SPP_PMTUD_ENABLE |
84 SPP_SACKDELAY_ENABLE;
85
86
87 peer->pathmaxrxt = sctp_max_retrans_path;
88
89 INIT_LIST_HEAD(&peer->transmitted);
90 INIT_LIST_HEAD(&peer->send_ready);
91 INIT_LIST_HEAD(&peer->transports);
92
93 setup_timer(&peer->T3_rtx_timer, sctp_generate_t3_rtx_event,
94 (unsigned long)peer);
95 setup_timer(&peer->hb_timer, sctp_generate_heartbeat_event,
96 (unsigned long)peer);
97 setup_timer(&peer->proto_unreach_timer,
98 sctp_generate_proto_unreach_event, (unsigned long)peer);
99
100
101 get_random_bytes(&peer->hb_nonce, sizeof(peer->hb_nonce));
102
103 atomic_set(&peer->refcnt, 1);
104
105 return peer;
106}
107
108
109struct sctp_transport *sctp_transport_new(const union sctp_addr *addr,
110 gfp_t gfp)
111{
112 struct sctp_transport *transport;
113
114 transport = t_new(struct sctp_transport, gfp);
115 if (!transport)
116 goto fail;
117
118 if (!sctp_transport_init(transport, addr, gfp))
119 goto fail_init;
120
121 transport->malloced = 1;
122 SCTP_DBG_OBJCNT_INC(transport);
123
124 return transport;
125
126fail_init:
127 kfree(transport);
128
129fail:
130 return NULL;
131}
132
133
134
135
136void sctp_transport_free(struct sctp_transport *transport)
137{
138 transport->dead = 1;
139
140
141 if (del_timer(&transport->hb_timer))
142 sctp_transport_put(transport);
143
144
145
146
147
148
149 if (timer_pending(&transport->T3_rtx_timer) &&
150 del_timer(&transport->T3_rtx_timer))
151 sctp_transport_put(transport);
152
153
154 if (timer_pending(&transport->proto_unreach_timer) &&
155 del_timer(&transport->proto_unreach_timer))
156 sctp_association_put(transport->asoc);
157
158 sctp_transport_put(transport);
159}
160
161
162
163
164static void sctp_transport_destroy(struct sctp_transport *transport)
165{
166 SCTP_ASSERT(transport->dead, "Transport is not dead", return);
167
168 if (transport->asoc)
169 sctp_association_put(transport->asoc);
170
171 sctp_packet_free(&transport->packet);
172
173 dst_release(transport->dst);
174 kfree(transport);
175 SCTP_DBG_OBJCNT_DEC(transport);
176}
177
178
179
180
181void sctp_transport_reset_timers(struct sctp_transport *transport)
182{
183
184
185
186
187
188
189
190
191 if (!timer_pending(&transport->T3_rtx_timer))
192 if (!mod_timer(&transport->T3_rtx_timer,
193 jiffies + transport->rto))
194 sctp_transport_hold(transport);
195
196
197 if (!mod_timer(&transport->hb_timer,
198 sctp_transport_timeout(transport)))
199 sctp_transport_hold(transport);
200}
201
202
203
204
205
206void sctp_transport_set_owner(struct sctp_transport *transport,
207 struct sctp_association *asoc)
208{
209 transport->asoc = asoc;
210 sctp_association_hold(asoc);
211}
212
213
214void sctp_transport_pmtu(struct sctp_transport *transport)
215{
216 struct dst_entry *dst;
217
218 dst = transport->af_specific->get_dst(NULL, &transport->ipaddr, NULL);
219
220 if (dst) {
221 transport->pathmtu = dst_mtu(dst);
222 dst_release(dst);
223 } else
224 transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
225}
226
227
228
229
230
231static struct dst_entry *sctp_transport_dst_check(struct sctp_transport *t)
232{
233 struct dst_entry *dst = t->dst;
234
235 if (dst && dst->obsolete && dst->ops->check(dst, 0) == NULL) {
236 dst_release(t->dst);
237 t->dst = NULL;
238 return NULL;
239 }
240
241 return dst;
242}
243
244void sctp_transport_update_pmtu(struct sctp_transport *t, u32 pmtu)
245{
246 struct dst_entry *dst;
247
248 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
249 pr_warn("%s: Reported pmtu %d too low, using default minimum of %d\n",
250 __func__, pmtu,
251 SCTP_DEFAULT_MINSEGMENT);
252
253
254
255 t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
256 } else {
257 t->pathmtu = pmtu;
258 }
259
260 dst = sctp_transport_dst_check(t);
261 if (dst)
262 dst->ops->update_pmtu(dst, pmtu);
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 union sctp_addr *daddr = &transport->ipaddr;
274 struct dst_entry *dst;
275
276 dst = af->get_dst(asoc, daddr, saddr);
277
278 if (saddr)
279 memcpy(&transport->saddr, saddr, sizeof(union sctp_addr));
280 else
281 af->get_saddr(opt, asoc, dst, daddr, &transport->saddr);
282
283 transport->dst = dst;
284 if ((transport->param_flags & SPP_PMTUD_DISABLE) && transport->pathmtu) {
285 return;
286 }
287 if (dst) {
288 transport->pathmtu = dst_mtu(dst);
289
290
291
292
293 if (asoc && (!asoc->peer.primary_path ||
294 (transport == asoc->peer.active_path)))
295 opt->pf->af->to_sk_saddr(&transport->saddr,
296 asoc->base.sk);
297 } else
298 transport->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
299}
300
301
302void sctp_transport_hold(struct sctp_transport *transport)
303{
304 atomic_inc(&transport->refcnt);
305}
306
307
308
309
310void sctp_transport_put(struct sctp_transport *transport)
311{
312 if (atomic_dec_and_test(&transport->refcnt))
313 sctp_transport_destroy(transport);
314}
315
316
317void sctp_transport_update_rto(struct sctp_transport *tp, __u32 rtt)
318{
319
320 SCTP_ASSERT(tp, "NULL transport", return);
321
322
323 SCTP_ASSERT(tp->rto_pending, "rto_pending not set", return);
324
325 if (tp->rttvar || tp->srtt) {
326
327
328
329
330
331
332
333
334
335
336
337 tp->rttvar = tp->rttvar - (tp->rttvar >> sctp_rto_beta)
338 + ((abs(tp->srtt - rtt)) >> sctp_rto_beta);
339 tp->srtt = tp->srtt - (tp->srtt >> sctp_rto_alpha)
340 + (rtt >> sctp_rto_alpha);
341 } else {
342
343
344
345 tp->srtt = rtt;
346 tp->rttvar = rtt >> 1;
347 }
348
349
350
351
352 if (tp->rttvar == 0)
353 tp->rttvar = SCTP_CLOCK_GRANULARITY;
354
355
356 tp->rto = tp->srtt + (tp->rttvar << 2);
357
358
359
360
361 if (tp->rto < tp->asoc->rto_min)
362 tp->rto = tp->asoc->rto_min;
363
364
365
366
367 if (tp->rto > tp->asoc->rto_max)
368 tp->rto = tp->asoc->rto_max;
369
370 tp->rtt = rtt;
371
372
373
374
375 tp->rto_pending = 0;
376
377 SCTP_DEBUG_PRINTK("%s: transport: %p, rtt: %d, srtt: %d "
378 "rttvar: %d, rto: %ld\n", __func__,
379 tp, rtt, tp->srtt, tp->rttvar, tp->rto);
380}
381
382
383
384
385void sctp_transport_raise_cwnd(struct sctp_transport *transport,
386 __u32 sack_ctsn, __u32 bytes_acked)
387{
388 struct sctp_association *asoc = transport->asoc;
389 __u32 cwnd, ssthresh, flight_size, pba, pmtu;
390
391 cwnd = transport->cwnd;
392 flight_size = transport->flight_size;
393
394
395 if (asoc->fast_recovery &&
396 TSN_lte(asoc->fast_recovery_exit, sack_ctsn))
397 asoc->fast_recovery = 0;
398
399
400
401
402
403 if (TSN_lte(sack_ctsn, transport->asoc->ctsn_ack_point) ||
404 (flight_size < cwnd))
405 return;
406
407 ssthresh = transport->ssthresh;
408 pba = transport->partial_bytes_acked;
409 pmtu = transport->asoc->pathmtu;
410
411 if (cwnd <= ssthresh) {
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426 if (asoc->fast_recovery)
427 return;
428
429 if (bytes_acked > pmtu)
430 cwnd += pmtu;
431 else
432 cwnd += bytes_acked;
433 SCTP_DEBUG_PRINTK("%s: SLOW START: transport: %p, "
434 "bytes_acked: %d, cwnd: %d, ssthresh: %d, "
435 "flight_size: %d, pba: %d\n",
436 __func__,
437 transport, bytes_acked, cwnd,
438 ssthresh, 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 SCTP_DEBUG_PRINTK("%s: CONGESTION AVOIDANCE: "
460 "transport: %p, bytes_acked: %d, cwnd: %d, "
461 "ssthresh: %d, flight_size: %d, pba: %d\n",
462 __func__,
463 transport, bytes_acked, cwnd,
464 ssthresh, 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 SCTP_DEBUG_PRINTK("%s: transport: %p reason: %d cwnd: "
559 "%d ssthresh: %d\n", __func__,
560 transport, reason,
561 transport->cwnd, 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)
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 *t)
603{
604 unsigned long timeout;
605 timeout = t->rto + sctp_jitter(t->rto);
606 if (t->state != SCTP_UNCONFIRMED)
607 timeout += t->hbinterval;
608 timeout += jiffies;
609 return timeout;
610}
611
612
613void sctp_transport_reset(struct sctp_transport *t)
614{
615 struct sctp_association *asoc = t->asoc;
616
617
618
619
620
621
622 t->cwnd = min(4*asoc->pathmtu, max_t(__u32, 2*asoc->pathmtu, 4380));
623 t->burst_limited = 0;
624 t->ssthresh = asoc->peer.i.a_rwnd;
625 t->rto = asoc->rto_initial;
626 t->rtt = 0;
627 t->srtt = 0;
628 t->rttvar = 0;
629
630
631
632
633 t->partial_bytes_acked = 0;
634 t->flight_size = 0;
635 t->error_count = 0;
636 t->rto_pending = 0;
637 t->hb_sent = 0;
638
639
640 t->cacc.changeover_active = 0;
641 t->cacc.cycling_changeover = 0;
642 t->cacc.next_tsn_at_change = 0;
643 t->cacc.cacc_saw_newack = 0;
644}
645