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/icmp.h>
53
54#ifndef TEST_FRAME
55#include <net/tcp.h>
56#endif
57
58#include <linux/socket.h>
59#include <net/sock.h>
60
61#include <net/sctp/sctp.h>
62#include <net/sctp/sm.h>
63
64
65static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
66 struct sctp_chunk *chunk);
67
68
69
70
71struct sctp_packet *sctp_packet_config(struct sctp_packet *packet,
72 __u32 vtag, int ecn_capable)
73{
74 struct sctp_chunk *chunk = NULL;
75
76 SCTP_DEBUG_PRINTK("%s: packet:%p vtag:0x%x\n", __FUNCTION__,
77 packet, vtag);
78
79 packet->vtag = vtag;
80 packet->has_cookie_echo = 0;
81 packet->has_sack = 0;
82 packet->has_auth = 0;
83 packet->has_data = 0;
84 packet->ipfragok = 0;
85 packet->auth = NULL;
86
87 if (ecn_capable && sctp_packet_empty(packet)) {
88 chunk = sctp_get_ecne_prepend(packet->transport->asoc);
89
90
91
92
93 if (chunk)
94 sctp_packet_append_chunk(packet, chunk);
95 }
96
97 return packet;
98}
99
100
101struct sctp_packet *sctp_packet_init(struct sctp_packet *packet,
102 struct sctp_transport *transport,
103 __u16 sport, __u16 dport)
104{
105 struct sctp_association *asoc = transport->asoc;
106 size_t overhead;
107
108 SCTP_DEBUG_PRINTK("%s: packet:%p transport:%p\n", __FUNCTION__,
109 packet, transport);
110
111 packet->transport = transport;
112 packet->source_port = sport;
113 packet->destination_port = dport;
114 INIT_LIST_HEAD(&packet->chunk_list);
115 if (asoc) {
116 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
117 overhead = sp->pf->af->net_header_len;
118 } else {
119 overhead = sizeof(struct ipv6hdr);
120 }
121 overhead += sizeof(struct sctphdr);
122 packet->overhead = overhead;
123 packet->size = overhead;
124 packet->vtag = 0;
125 packet->has_cookie_echo = 0;
126 packet->has_sack = 0;
127 packet->has_auth = 0;
128 packet->has_data = 0;
129 packet->ipfragok = 0;
130 packet->malloced = 0;
131 packet->auth = NULL;
132 return packet;
133}
134
135
136void sctp_packet_free(struct sctp_packet *packet)
137{
138 struct sctp_chunk *chunk, *tmp;
139
140 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__, packet);
141
142 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
143 list_del_init(&chunk->list);
144 sctp_chunk_free(chunk);
145 }
146
147 if (packet->malloced)
148 kfree(packet);
149}
150
151
152
153
154
155
156
157
158sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet,
159 struct sctp_chunk *chunk)
160{
161 sctp_xmit_t retval;
162 int error = 0;
163
164 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__,
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 retval = sctp_packet_append_chunk(packet, chunk);
178 }
179 break;
180
181 case SCTP_XMIT_RWND_FULL:
182 case SCTP_XMIT_OK:
183 case SCTP_XMIT_NAGLE_DELAY:
184 break;
185 }
186
187 return retval;
188}
189
190
191static sctp_xmit_t sctp_packet_bundle_auth(struct sctp_packet *pkt,
192 struct sctp_chunk *chunk)
193{
194 struct sctp_association *asoc = pkt->transport->asoc;
195 struct sctp_chunk *auth;
196 sctp_xmit_t retval = SCTP_XMIT_OK;
197
198
199 if (!asoc)
200 return retval;
201
202
203
204
205 if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->auth)
206 return retval;
207
208
209
210
211 if (!chunk->auth)
212 return retval;
213
214 auth = sctp_make_auth(asoc);
215 if (!auth)
216 return retval;
217
218 retval = sctp_packet_append_chunk(pkt, auth);
219
220 return retval;
221}
222
223
224static sctp_xmit_t sctp_packet_bundle_sack(struct sctp_packet *pkt,
225 struct sctp_chunk *chunk)
226{
227 sctp_xmit_t retval = SCTP_XMIT_OK;
228
229
230
231
232 if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&
233 !pkt->has_cookie_echo) {
234 struct sctp_association *asoc;
235 asoc = pkt->transport->asoc;
236
237 if (asoc->a_rwnd > asoc->rwnd) {
238 struct sctp_chunk *sack;
239 asoc->a_rwnd = asoc->rwnd;
240 sack = sctp_make_sack(asoc);
241 if (sack) {
242 struct timer_list *timer;
243 retval = sctp_packet_append_chunk(pkt, sack);
244 asoc->peer.sack_needed = 0;
245 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
246 if (timer_pending(timer) && del_timer(timer))
247 sctp_association_put(asoc);
248 }
249 }
250 }
251 return retval;
252}
253
254
255
256
257sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet,
258 struct sctp_chunk *chunk)
259{
260 sctp_xmit_t retval = SCTP_XMIT_OK;
261 __u16 chunk_len = WORD_ROUND(ntohs(chunk->chunk_hdr->length));
262 size_t psize;
263 size_t pmtu;
264 int too_big;
265
266 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__, packet,
267 chunk);
268
269
270 retval = sctp_packet_bundle_auth(packet, chunk);
271 if (retval != SCTP_XMIT_OK)
272 goto finish;
273
274
275 retval = sctp_packet_bundle_sack(packet, chunk);
276 if (retval != SCTP_XMIT_OK)
277 goto finish;
278
279 psize = packet->size;
280 pmtu = ((packet->transport->asoc) ?
281 (packet->transport->asoc->pathmtu) :
282 (packet->transport->pathmtu));
283
284 too_big = (psize + chunk_len > pmtu);
285
286
287 if (too_big) {
288
289
290
291
292
293
294
295
296 if (sctp_packet_empty(packet) || !sctp_chunk_is_data(chunk) ||
297 (!packet->has_data && chunk->auth)) {
298
299
300
301
302 packet->ipfragok = 1;
303 goto append;
304
305 } else {
306 retval = SCTP_XMIT_PMTU_FULL;
307 goto finish;
308 }
309 }
310
311append:
312
313
314
315
316
317
318
319 switch (chunk->chunk_hdr->type) {
320 case SCTP_CID_DATA:
321 retval = sctp_packet_append_data(packet, chunk);
322
323 packet->has_sack = 1;
324
325 packet->has_auth = 1;
326
327 packet->has_data = 1;
328 if (SCTP_XMIT_OK != retval)
329 goto finish;
330 break;
331 case SCTP_CID_COOKIE_ECHO:
332 packet->has_cookie_echo = 1;
333 break;
334
335 case SCTP_CID_SACK:
336 packet->has_sack = 1;
337 break;
338
339 case SCTP_CID_AUTH:
340 packet->has_auth = 1;
341 packet->auth = chunk;
342 break;
343 }
344
345
346 list_add_tail(&chunk->list, &packet->chunk_list);
347 packet->size += chunk_len;
348 chunk->transport = packet->transport;
349finish:
350 return retval;
351}
352
353
354
355
356
357
358int sctp_packet_transmit(struct sctp_packet *packet)
359{
360 struct sctp_transport *tp = packet->transport;
361 struct sctp_association *asoc = tp->asoc;
362 struct sctphdr *sh;
363 __u32 crc32 = 0;
364 struct sk_buff *nskb;
365 struct sctp_chunk *chunk, *tmp;
366 struct sock *sk;
367 int err = 0;
368 int padding;
369 __u8 has_data = 0;
370 struct dst_entry *dst = tp->dst;
371 unsigned char *auth = NULL;
372 __u32 cksum_buf_len = sizeof(struct sctphdr);
373
374 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__, packet);
375
376
377 if (list_empty(&packet->chunk_list))
378 return err;
379
380
381 chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
382 sk = chunk->skb->sk;
383
384
385 nskb = alloc_skb(packet->size + LL_MAX_HEADER, GFP_ATOMIC);
386 if (!nskb)
387 goto nomem;
388
389
390 skb_reserve(nskb, packet->overhead + LL_MAX_HEADER);
391
392
393
394
395 skb_set_owner_w(nskb, sk);
396
397
398 if (!dst || (dst->obsolete > 1)) {
399 dst_release(dst);
400 sctp_transport_route(tp, NULL, sctp_sk(sk));
401 if (asoc && (asoc->param_flags & SPP_PMTUD_ENABLE)) {
402 sctp_assoc_sync_pmtu(asoc);
403 }
404 }
405 nskb->dst = dst_clone(tp->dst);
406 if (!nskb->dst)
407 goto no_route;
408 dst = nskb->dst;
409
410
411 sh = (struct sctphdr *)skb_push(nskb, sizeof(struct sctphdr));
412 sh->source = htons(packet->source_port);
413 sh->dest = htons(packet->destination_port);
414
415
416
417
418
419
420
421
422
423 sh->vtag = htonl(packet->vtag);
424 sh->checksum = 0;
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445 SCTP_DEBUG_PRINTK("***sctp_transmit_packet***\n");
446 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
447 list_del_init(&chunk->list);
448 if (sctp_chunk_is_data(chunk)) {
449
450 if (!chunk->has_tsn) {
451 sctp_chunk_assign_ssn(chunk);
452 sctp_chunk_assign_tsn(chunk);
453
454
455
456
457
458
459
460
461 if (!tp->rto_pending) {
462 chunk->rtt_in_progress = 1;
463 tp->rto_pending = 1;
464 }
465 } else
466 chunk->resent = 1;
467
468 chunk->sent_at = jiffies;
469 has_data = 1;
470 }
471
472 padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len;
473 if (padding)
474 memset(skb_put(chunk->skb, padding), 0, padding);
475
476
477
478
479
480 if (chunk == packet->auth)
481 auth = skb_tail_pointer(nskb);
482
483 cksum_buf_len += chunk->skb->len;
484 memcpy(skb_put(nskb, chunk->skb->len),
485 chunk->skb->data, chunk->skb->len);
486
487 SCTP_DEBUG_PRINTK("%s %p[%s] %s 0x%x, %s %d, %s %d, %s %d\n",
488 "*** Chunk", chunk,
489 sctp_cname(SCTP_ST_CHUNK(
490 chunk->chunk_hdr->type)),
491 chunk->has_tsn ? "TSN" : "No TSN",
492 chunk->has_tsn ?
493 ntohl(chunk->subh.data_hdr->tsn) : 0,
494 "length", ntohs(chunk->chunk_hdr->length),
495 "chunk->skb->len", chunk->skb->len,
496 "rtt_in_progress", chunk->rtt_in_progress);
497
498
499
500
501
502
503 if (!sctp_chunk_is_data(chunk))
504 sctp_chunk_free(chunk);
505 }
506
507
508
509
510
511
512
513
514
515
516 if (auth)
517 sctp_auth_calculate_hmac(asoc, nskb,
518 (struct sctp_auth_chunk *)auth,
519 GFP_ATOMIC);
520
521
522
523
524
525
526
527
528 if (!(dst->dev->features & NETIF_F_NO_CSUM)) {
529 crc32 = sctp_start_cksum((__u8 *)sh, cksum_buf_len);
530 crc32 = sctp_end_cksum(crc32);
531 }
532
533
534
535
536 sh->checksum = htonl(crc32);
537
538
539
540
541
542
543
544
545
546
547
548
549
550 INET_ECN_xmit(nskb->sk);
551
552
553
554
555
556
557
558 if (asoc && asoc->peer.last_sent_to != tp) {
559
560
561
562 asoc->peer.last_sent_to = tp;
563 }
564
565 if (has_data) {
566 struct timer_list *timer;
567 unsigned long timeout;
568
569 tp->last_time_used = jiffies;
570
571
572 if (sctp_state(asoc, ESTABLISHED) && asoc->autoclose) {
573 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
574 timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
575
576 if (!mod_timer(timer, jiffies + timeout))
577 sctp_association_hold(asoc);
578 }
579 }
580
581 SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb len %d\n",
582 nskb->len);
583
584 if (tp->param_flags & SPP_PMTUD_ENABLE)
585 (*tp->af_specific->sctp_xmit)(nskb, tp, packet->ipfragok);
586 else
587 (*tp->af_specific->sctp_xmit)(nskb, tp, 1);
588
589out:
590 packet->size = packet->overhead;
591 return err;
592no_route:
593 kfree_skb(nskb);
594 IP_INC_STATS_BH(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 __FUNCTION__, 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 <= 0)
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