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#include <linux/types.h>
45#include <linux/kernel.h>
46#include <linux/wait.h>
47#include <linux/time.h>
48#include <linux/ip.h>
49#include <linux/ipv6.h>
50#include <linux/init.h>
51#include <net/inet_ecn.h>
52#include <net/ip.h>
53#include <net/icmp.h>
54#include <net/net_namespace.h>
55
56#include <linux/socket.h>
57#include <net/sock.h>
58
59#include <net/sctp/sctp.h>
60#include <net/sctp/sm.h>
61#include <net/sctp/checksum.h>
62
63
64static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
65 struct sctp_chunk *chunk);
66
67
68
69
70struct sctp_packet *sctp_packet_config(struct sctp_packet *packet,
71 __u32 vtag, int ecn_capable)
72{
73 struct sctp_chunk *chunk = NULL;
74
75 SCTP_DEBUG_PRINTK("%s: packet:%p vtag:0x%x\n", __func__,
76 packet, vtag);
77
78 packet->vtag = vtag;
79 packet->has_cookie_echo = 0;
80 packet->has_sack = 0;
81 packet->has_auth = 0;
82 packet->has_data = 0;
83 packet->ipfragok = 0;
84 packet->auth = NULL;
85
86 if (ecn_capable && sctp_packet_empty(packet)) {
87 chunk = sctp_get_ecne_prepend(packet->transport->asoc);
88
89
90
91
92 if (chunk)
93 sctp_packet_append_chunk(packet, chunk);
94 }
95
96 return packet;
97}
98
99
100struct sctp_packet *sctp_packet_init(struct sctp_packet *packet,
101 struct sctp_transport *transport,
102 __u16 sport, __u16 dport)
103{
104 struct sctp_association *asoc = transport->asoc;
105 size_t overhead;
106
107 SCTP_DEBUG_PRINTK("%s: packet:%p transport:%p\n", __func__,
108 packet, transport);
109
110 packet->transport = transport;
111 packet->source_port = sport;
112 packet->destination_port = dport;
113 INIT_LIST_HEAD(&packet->chunk_list);
114 if (asoc) {
115 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
116 overhead = sp->pf->af->net_header_len;
117 } else {
118 overhead = sizeof(struct ipv6hdr);
119 }
120 overhead += sizeof(struct sctphdr);
121 packet->overhead = overhead;
122 packet->size = overhead;
123 packet->vtag = 0;
124 packet->has_cookie_echo = 0;
125 packet->has_sack = 0;
126 packet->has_auth = 0;
127 packet->has_data = 0;
128 packet->ipfragok = 0;
129 packet->malloced = 0;
130 packet->auth = NULL;
131 return packet;
132}
133
134
135void sctp_packet_free(struct sctp_packet *packet)
136{
137 struct sctp_chunk *chunk, *tmp;
138
139 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __func__, packet);
140
141 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
142 list_del_init(&chunk->list);
143 sctp_chunk_free(chunk);
144 }
145
146 if (packet->malloced)
147 kfree(packet);
148}
149
150
151
152
153
154
155
156
157sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet,
158 struct sctp_chunk *chunk,
159 int one_packet)
160{
161 sctp_xmit_t retval;
162 int error = 0;
163
164 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __func__,
165 packet, chunk);
166
167 switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
168 case SCTP_XMIT_PMTU_FULL:
169 if (!packet->has_cookie_echo) {
170 error = sctp_packet_transmit(packet);
171 if (error < 0)
172 chunk->skb->sk->sk_err = -error;
173
174
175
176
177 if (!one_packet)
178 retval = sctp_packet_append_chunk(packet,
179 chunk);
180 }
181 break;
182
183 case SCTP_XMIT_RWND_FULL:
184 case SCTP_XMIT_OK:
185 case SCTP_XMIT_NAGLE_DELAY:
186 break;
187 }
188
189 return retval;
190}
191
192
193static sctp_xmit_t sctp_packet_bundle_auth(struct sctp_packet *pkt,
194 struct sctp_chunk *chunk)
195{
196 struct sctp_association *asoc = pkt->transport->asoc;
197 struct sctp_chunk *auth;
198 sctp_xmit_t retval = SCTP_XMIT_OK;
199
200
201 if (!asoc)
202 return retval;
203
204
205
206
207 if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->auth)
208 return retval;
209
210
211
212
213 if (!chunk->auth)
214 return retval;
215
216 auth = sctp_make_auth(asoc);
217 if (!auth)
218 return retval;
219
220 retval = sctp_packet_append_chunk(pkt, auth);
221
222 return retval;
223}
224
225
226static sctp_xmit_t sctp_packet_bundle_sack(struct sctp_packet *pkt,
227 struct sctp_chunk *chunk)
228{
229 sctp_xmit_t retval = SCTP_XMIT_OK;
230
231
232
233
234 if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&
235 !pkt->has_cookie_echo) {
236 struct sctp_association *asoc;
237 asoc = pkt->transport->asoc;
238
239 if (asoc->a_rwnd > asoc->rwnd) {
240 struct sctp_chunk *sack;
241 asoc->a_rwnd = asoc->rwnd;
242 sack = sctp_make_sack(asoc);
243 if (sack) {
244 struct timer_list *timer;
245 retval = sctp_packet_append_chunk(pkt, sack);
246 asoc->peer.sack_needed = 0;
247 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
248 if (timer_pending(timer) && del_timer(timer))
249 sctp_association_put(asoc);
250 }
251 }
252 }
253 return retval;
254}
255
256
257
258
259sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet,
260 struct sctp_chunk *chunk)
261{
262 sctp_xmit_t retval = SCTP_XMIT_OK;
263 __u16 chunk_len = WORD_ROUND(ntohs(chunk->chunk_hdr->length));
264 size_t psize;
265 size_t pmtu;
266 int too_big;
267
268 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __func__, packet,
269 chunk);
270
271
272 retval = sctp_packet_bundle_auth(packet, chunk);
273 if (retval != SCTP_XMIT_OK)
274 goto finish;
275
276
277 retval = sctp_packet_bundle_sack(packet, chunk);
278 if (retval != SCTP_XMIT_OK)
279 goto finish;
280
281 psize = packet->size;
282 pmtu = ((packet->transport->asoc) ?
283 (packet->transport->asoc->pathmtu) :
284 (packet->transport->pathmtu));
285
286 too_big = (psize + chunk_len > pmtu);
287
288
289 if (too_big) {
290
291
292
293
294
295
296
297
298 if (sctp_packet_empty(packet) || !sctp_chunk_is_data(chunk) ||
299 (!packet->has_data && chunk->auth)) {
300
301
302
303
304 packet->ipfragok = 1;
305 goto append;
306
307 } else {
308 retval = SCTP_XMIT_PMTU_FULL;
309 goto finish;
310 }
311 }
312
313append:
314
315
316
317
318
319
320
321 switch (chunk->chunk_hdr->type) {
322 case SCTP_CID_DATA:
323 retval = sctp_packet_append_data(packet, chunk);
324 if (SCTP_XMIT_OK != retval)
325 goto finish;
326
327 packet->has_sack = 1;
328
329 packet->has_auth = 1;
330
331 packet->has_data = 1;
332
333 chunk->sent_at = jiffies;
334 break;
335 case SCTP_CID_COOKIE_ECHO:
336 packet->has_cookie_echo = 1;
337 break;
338
339 case SCTP_CID_SACK:
340 packet->has_sack = 1;
341 break;
342
343 case SCTP_CID_AUTH:
344 packet->has_auth = 1;
345 packet->auth = chunk;
346 break;
347 }
348
349
350 list_add_tail(&chunk->list, &packet->chunk_list);
351 packet->size += chunk_len;
352 chunk->transport = packet->transport;
353finish:
354 return retval;
355}
356
357
358
359
360
361
362int sctp_packet_transmit(struct sctp_packet *packet)
363{
364 struct sctp_transport *tp = packet->transport;
365 struct sctp_association *asoc = tp->asoc;
366 struct sctphdr *sh;
367 struct sk_buff *nskb;
368 struct sctp_chunk *chunk, *tmp;
369 struct sock *sk;
370 int err = 0;
371 int padding;
372 __u8 has_data = 0;
373 struct dst_entry *dst = tp->dst;
374 unsigned char *auth = NULL;
375 __u32 cksum_buf_len = sizeof(struct sctphdr);
376
377 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __func__, packet);
378
379
380 if (list_empty(&packet->chunk_list))
381 return err;
382
383
384 chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
385 sk = chunk->skb->sk;
386
387
388 nskb = alloc_skb(packet->size + LL_MAX_HEADER, GFP_ATOMIC);
389 if (!nskb)
390 goto nomem;
391
392
393 skb_reserve(nskb, packet->overhead + LL_MAX_HEADER);
394
395
396
397
398 skb_set_owner_w(nskb, sk);
399
400
401 if (!dst || (dst->obsolete > 1)) {
402 dst_release(dst);
403 sctp_transport_route(tp, NULL, sctp_sk(sk));
404 if (asoc && (asoc->param_flags & SPP_PMTUD_ENABLE)) {
405 sctp_assoc_sync_pmtu(asoc);
406 }
407 }
408 nskb->dst = dst_clone(tp->dst);
409 if (!nskb->dst)
410 goto no_route;
411 dst = nskb->dst;
412
413
414 sh = (struct sctphdr *)skb_push(nskb, sizeof(struct sctphdr));
415 sh->source = htons(packet->source_port);
416 sh->dest = htons(packet->destination_port);
417
418
419
420
421
422
423
424
425
426 sh->vtag = htonl(packet->vtag);
427 sh->checksum = 0;
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448 SCTP_DEBUG_PRINTK("***sctp_transmit_packet***\n");
449 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
450 list_del_init(&chunk->list);
451 if (sctp_chunk_is_data(chunk)) {
452
453 if (!chunk->has_tsn) {
454 sctp_chunk_assign_ssn(chunk);
455 sctp_chunk_assign_tsn(chunk);
456
457
458
459
460
461
462
463
464 if (!tp->rto_pending) {
465 chunk->rtt_in_progress = 1;
466 tp->rto_pending = 1;
467 }
468 } else
469 chunk->resent = 1;
470
471 has_data = 1;
472 }
473
474 padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len;
475 if (padding)
476 memset(skb_put(chunk->skb, padding), 0, padding);
477
478
479
480
481
482 if (chunk == packet->auth)
483 auth = skb_tail_pointer(nskb);
484
485 cksum_buf_len += chunk->skb->len;
486 memcpy(skb_put(nskb, chunk->skb->len),
487 chunk->skb->data, chunk->skb->len);
488
489 SCTP_DEBUG_PRINTK("%s %p[%s] %s 0x%x, %s %d, %s %d, %s %d\n",
490 "*** Chunk", chunk,
491 sctp_cname(SCTP_ST_CHUNK(
492 chunk->chunk_hdr->type)),
493 chunk->has_tsn ? "TSN" : "No TSN",
494 chunk->has_tsn ?
495 ntohl(chunk->subh.data_hdr->tsn) : 0,
496 "length", ntohs(chunk->chunk_hdr->length),
497 "chunk->skb->len", chunk->skb->len,
498 "rtt_in_progress", chunk->rtt_in_progress);
499
500
501
502
503
504
505 if (!sctp_chunk_is_data(chunk))
506 sctp_chunk_free(chunk);
507 }
508
509
510
511
512
513
514
515
516
517
518 if (auth)
519 sctp_auth_calculate_hmac(asoc, nskb,
520 (struct sctp_auth_chunk *)auth,
521 GFP_ATOMIC);
522
523
524
525
526
527
528
529
530 if (!sctp_checksum_disable && !(dst->dev->features & NETIF_F_NO_CSUM)) {
531 __u32 crc32 = sctp_start_cksum((__u8 *)sh, cksum_buf_len);
532
533
534
535
536 sh->checksum = sctp_end_cksum(crc32);
537 } else
538 nskb->ip_summed = CHECKSUM_UNNECESSARY;
539
540
541
542
543
544
545
546
547
548
549
550
551
552 (*tp->af_specific->ecn_capable)(nskb->sk);
553
554
555
556
557
558
559
560 if (asoc && asoc->peer.last_sent_to != tp) {
561
562
563
564 asoc->peer.last_sent_to = tp;
565 }
566
567 if (has_data) {
568 struct timer_list *timer;
569 unsigned long timeout;
570
571 tp->last_time_used = jiffies;
572
573
574 if (sctp_state(asoc, ESTABLISHED) && asoc->autoclose) {
575 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
576 timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
577
578 if (!mod_timer(timer, jiffies + timeout))
579 sctp_association_hold(asoc);
580 }
581 }
582
583 SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb len %d\n",
584 nskb->len);
585
586 nskb->local_df = packet->ipfragok;
587 (*tp->af_specific->sctp_xmit)(nskb, tp);
588
589out:
590 packet->size = packet->overhead;
591 return err;
592no_route:
593 kfree_skb(nskb);
594 IP_INC_STATS_BH(&init_net, IPSTATS_MIB_OUTNOROUTES);
595
596
597
598
599
600
601
602
603
604err:
605
606
607
608
609 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
610 list_del_init(&chunk->list);
611 if (!sctp_chunk_is_data(chunk))
612 sctp_chunk_free(chunk);
613 }
614 goto out;
615nomem:
616 err = -ENOMEM;
617 goto err;
618}
619
620
621
622
623
624
625static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
626 struct sctp_chunk *chunk)
627{
628 sctp_xmit_t retval = SCTP_XMIT_OK;
629 size_t datasize, rwnd, inflight;
630 struct sctp_transport *transport = packet->transport;
631 __u32 max_burst_bytes;
632 struct sctp_association *asoc = transport->asoc;
633 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
634 struct sctp_outq *q = &asoc->outqueue;
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649 rwnd = asoc->peer.rwnd;
650 inflight = asoc->outqueue.outstanding_bytes;
651
652 datasize = sctp_data_size(chunk);
653
654 if (datasize > rwnd) {
655 if (inflight > 0) {
656
657
658
659 retval = SCTP_XMIT_RWND_FULL;
660 goto finish;
661 }
662 }
663
664
665
666
667
668
669
670
671
672 max_burst_bytes = asoc->max_burst * asoc->pathmtu;
673 if ((transport->flight_size + max_burst_bytes) < transport->cwnd) {
674 transport->cwnd = transport->flight_size + max_burst_bytes;
675 SCTP_DEBUG_PRINTK("%s: cwnd limited by max_burst: "
676 "transport: %p, cwnd: %d, "
677 "ssthresh: %d, flight_size: %d, "
678 "pba: %d\n",
679 __func__, transport,
680 transport->cwnd,
681 transport->ssthresh,
682 transport->flight_size,
683 transport->partial_bytes_acked);
684 }
685
686
687
688
689
690
691
692
693
694
695
696
697
698 if (chunk->fast_retransmit != SCTP_NEED_FRTX)
699 if (transport->flight_size >= transport->cwnd) {
700 retval = SCTP_XMIT_RWND_FULL;
701 goto finish;
702 }
703
704
705
706
707
708
709 if (!sp->nodelay && sctp_packet_empty(packet) &&
710 q->outstanding_bytes && sctp_state(asoc, ESTABLISHED)) {
711 unsigned len = datasize + q->out_qlen;
712
713
714
715
716
717 if (len < asoc->frag_point) {
718 retval = SCTP_XMIT_NAGLE_DELAY;
719 goto finish;
720 }
721 }
722
723
724 transport->flight_size += datasize;
725
726
727 asoc->outqueue.outstanding_bytes += datasize;
728
729
730
731
732
733
734
735 datasize += sizeof(struct sk_buff);
736 if (datasize < rwnd)
737 rwnd -= datasize;
738 else
739 rwnd = 0;
740
741 asoc->peer.rwnd = rwnd;
742
743 if (!asoc->peer.prsctp_capable)
744 chunk->msg->can_abandon = 0;
745
746finish:
747 return retval;
748}
749