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