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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
39#include <linux/types.h>
40#include <linux/kernel.h>
41#include <linux/wait.h>
42#include <linux/time.h>
43#include <linux/ip.h>
44#include <linux/ipv6.h>
45#include <linux/init.h>
46#include <linux/slab.h>
47#include <net/inet_ecn.h>
48#include <net/ip.h>
49#include <net/icmp.h>
50#include <net/net_namespace.h>
51
52#include <linux/socket.h>
53#include <net/sock.h>
54
55#include <net/sctp/sctp.h>
56#include <net/sctp/sm.h>
57#include <net/sctp/checksum.h>
58
59
60static enum sctp_xmit __sctp_packet_append_chunk(struct sctp_packet *packet,
61 struct sctp_chunk *chunk);
62static enum sctp_xmit sctp_packet_can_append_data(struct sctp_packet *packet,
63 struct sctp_chunk *chunk);
64static void sctp_packet_append_data(struct sctp_packet *packet,
65 struct sctp_chunk *chunk);
66static enum sctp_xmit sctp_packet_will_fit(struct sctp_packet *packet,
67 struct sctp_chunk *chunk,
68 u16 chunk_len);
69
70static void sctp_packet_reset(struct sctp_packet *packet)
71{
72
73
74
75 packet->size = packet->overhead;
76
77 packet->has_cookie_echo = 0;
78 packet->has_sack = 0;
79 packet->has_data = 0;
80 packet->has_auth = 0;
81 packet->ipfragok = 0;
82 packet->auth = NULL;
83}
84
85
86
87
88void sctp_packet_config(struct sctp_packet *packet, __u32 vtag,
89 int ecn_capable)
90{
91 struct sctp_transport *tp = packet->transport;
92 struct sctp_association *asoc = tp->asoc;
93 struct sctp_sock *sp = NULL;
94 struct sock *sk;
95
96 pr_debug("%s: packet:%p vtag:0x%x\n", __func__, packet, vtag);
97 packet->vtag = vtag;
98
99
100 if (!sctp_packet_empty(packet))
101 return;
102
103
104 packet->max_size = tp->pathmtu;
105
106 if (asoc) {
107 sk = asoc->base.sk;
108 sp = sctp_sk(sk);
109 }
110 packet->overhead = sctp_mtu_payload(sp, 0, 0);
111 packet->size = packet->overhead;
112
113 if (!asoc)
114 return;
115
116
117 if (!sctp_transport_dst_check(tp)) {
118 sctp_transport_route(tp, NULL, sp);
119 if (asoc->param_flags & SPP_PMTUD_ENABLE)
120 sctp_assoc_sync_pmtu(asoc);
121 } else if (!sctp_transport_pmtu_check(tp)) {
122 if (asoc->param_flags & SPP_PMTUD_ENABLE)
123 sctp_assoc_sync_pmtu(asoc);
124 }
125
126 if (asoc->pmtu_pending) {
127 if (asoc->param_flags & SPP_PMTUD_ENABLE)
128 sctp_assoc_sync_pmtu(asoc);
129 asoc->pmtu_pending = 0;
130 }
131
132
133
134
135 if (ecn_capable) {
136 struct sctp_chunk *chunk = sctp_get_ecne_prepend(asoc);
137
138 if (chunk)
139 sctp_packet_append_chunk(packet, chunk);
140 }
141
142 if (!tp->dst)
143 return;
144
145
146 rcu_read_lock();
147 if (__sk_dst_get(sk) != tp->dst) {
148 dst_hold(tp->dst);
149 sk_setup_caps(sk, tp->dst);
150 }
151 packet->max_size = sk_can_gso(sk) ? tp->dst->dev->gso_max_size
152 : asoc->pathmtu;
153 rcu_read_unlock();
154}
155
156
157void sctp_packet_init(struct sctp_packet *packet,
158 struct sctp_transport *transport,
159 __u16 sport, __u16 dport)
160{
161 pr_debug("%s: packet:%p transport:%p\n", __func__, packet, transport);
162
163 packet->transport = transport;
164 packet->source_port = sport;
165 packet->destination_port = dport;
166 INIT_LIST_HEAD(&packet->chunk_list);
167
168 packet->overhead = 0;
169 sctp_packet_reset(packet);
170 packet->vtag = 0;
171}
172
173
174void sctp_packet_free(struct sctp_packet *packet)
175{
176 struct sctp_chunk *chunk, *tmp;
177
178 pr_debug("%s: packet:%p\n", __func__, packet);
179
180 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
181 list_del_init(&chunk->list);
182 sctp_chunk_free(chunk);
183 }
184}
185
186
187
188
189
190
191
192
193enum sctp_xmit sctp_packet_transmit_chunk(struct sctp_packet *packet,
194 struct sctp_chunk *chunk,
195 int one_packet, gfp_t gfp)
196{
197 enum sctp_xmit retval;
198
199 pr_debug("%s: packet:%p size:%zu chunk:%p size:%d\n", __func__,
200 packet, packet->size, chunk, chunk->skb ? chunk->skb->len : -1);
201
202 switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
203 case SCTP_XMIT_PMTU_FULL:
204 if (!packet->has_cookie_echo) {
205 int error = 0;
206
207 error = sctp_packet_transmit(packet, gfp);
208 if (error < 0)
209 chunk->skb->sk->sk_err = -error;
210
211
212
213
214 if (!one_packet)
215 retval = sctp_packet_append_chunk(packet,
216 chunk);
217 }
218 break;
219
220 case SCTP_XMIT_RWND_FULL:
221 case SCTP_XMIT_OK:
222 case SCTP_XMIT_DELAY:
223 break;
224 }
225
226 return retval;
227}
228
229
230static enum sctp_xmit sctp_packet_bundle_auth(struct sctp_packet *pkt,
231 struct sctp_chunk *chunk)
232{
233 struct sctp_association *asoc = pkt->transport->asoc;
234 enum sctp_xmit retval = SCTP_XMIT_OK;
235 struct sctp_chunk *auth;
236
237
238 if (!asoc)
239 return retval;
240
241
242
243
244 if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->has_auth)
245 return retval;
246
247
248
249
250 if (!chunk->auth)
251 return retval;
252
253 auth = sctp_make_auth(asoc, chunk->shkey->key_id);
254 if (!auth)
255 return retval;
256
257 auth->shkey = chunk->shkey;
258 sctp_auth_shkey_hold(auth->shkey);
259
260 retval = __sctp_packet_append_chunk(pkt, auth);
261
262 if (retval != SCTP_XMIT_OK)
263 sctp_chunk_free(auth);
264
265 return retval;
266}
267
268
269static enum sctp_xmit sctp_packet_bundle_sack(struct sctp_packet *pkt,
270 struct sctp_chunk *chunk)
271{
272 enum sctp_xmit retval = SCTP_XMIT_OK;
273
274
275
276
277 if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&
278 !pkt->has_cookie_echo) {
279 struct sctp_association *asoc;
280 struct timer_list *timer;
281 asoc = pkt->transport->asoc;
282 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
283
284
285 if (timer_pending(timer)) {
286 struct sctp_chunk *sack;
287
288 if (pkt->transport->sack_generation !=
289 pkt->transport->asoc->peer.sack_generation)
290 return retval;
291
292 asoc->a_rwnd = asoc->rwnd;
293 sack = sctp_make_sack(asoc);
294 if (sack) {
295 retval = __sctp_packet_append_chunk(pkt, sack);
296 if (retval != SCTP_XMIT_OK) {
297 sctp_chunk_free(sack);
298 goto out;
299 }
300 asoc->peer.sack_needed = 0;
301 if (del_timer(timer))
302 sctp_association_put(asoc);
303 }
304 }
305 }
306out:
307 return retval;
308}
309
310
311
312
313
314static enum sctp_xmit __sctp_packet_append_chunk(struct sctp_packet *packet,
315 struct sctp_chunk *chunk)
316{
317 __u16 chunk_len = SCTP_PAD4(ntohs(chunk->chunk_hdr->length));
318 enum sctp_xmit retval = SCTP_XMIT_OK;
319
320
321 retval = sctp_packet_will_fit(packet, chunk, chunk_len);
322 if (retval != SCTP_XMIT_OK)
323 goto finish;
324
325
326 switch (chunk->chunk_hdr->type) {
327 case SCTP_CID_DATA:
328 case SCTP_CID_I_DATA:
329
330 sctp_packet_append_data(packet, chunk);
331
332 packet->has_sack = 1;
333
334 packet->has_auth = 1;
335
336 packet->has_data = 1;
337
338 chunk->sent_at = jiffies;
339
340 chunk->sent_count++;
341 break;
342 case SCTP_CID_COOKIE_ECHO:
343 packet->has_cookie_echo = 1;
344 break;
345
346 case SCTP_CID_SACK:
347 packet->has_sack = 1;
348 if (chunk->asoc)
349 chunk->asoc->stats.osacks++;
350 break;
351
352 case SCTP_CID_AUTH:
353 packet->has_auth = 1;
354 packet->auth = chunk;
355 break;
356 }
357
358
359 list_add_tail(&chunk->list, &packet->chunk_list);
360 packet->size += chunk_len;
361 chunk->transport = packet->transport;
362finish:
363 return retval;
364}
365
366
367
368
369enum sctp_xmit sctp_packet_append_chunk(struct sctp_packet *packet,
370 struct sctp_chunk *chunk)
371{
372 enum sctp_xmit retval = SCTP_XMIT_OK;
373
374 pr_debug("%s: packet:%p chunk:%p\n", __func__, packet, chunk);
375
376
377
378
379
380 if (sctp_chunk_is_data(chunk)) {
381 retval = sctp_packet_can_append_data(packet, chunk);
382 if (retval != SCTP_XMIT_OK)
383 goto finish;
384 }
385
386
387 retval = sctp_packet_bundle_auth(packet, chunk);
388 if (retval != SCTP_XMIT_OK)
389 goto finish;
390
391
392 retval = sctp_packet_bundle_sack(packet, chunk);
393 if (retval != SCTP_XMIT_OK)
394 goto finish;
395
396 retval = __sctp_packet_append_chunk(packet, chunk);
397
398finish:
399 return retval;
400}
401
402static void sctp_packet_gso_append(struct sk_buff *head, struct sk_buff *skb)
403{
404 if (SCTP_OUTPUT_CB(head)->last == head)
405 skb_shinfo(head)->frag_list = skb;
406 else
407 SCTP_OUTPUT_CB(head)->last->next = skb;
408 SCTP_OUTPUT_CB(head)->last = skb;
409
410 head->truesize += skb->truesize;
411 head->data_len += skb->len;
412 head->len += skb->len;
413 refcount_add(skb->truesize, &head->sk->sk_wmem_alloc);
414
415 __skb_header_release(skb);
416}
417
418static int sctp_packet_pack(struct sctp_packet *packet,
419 struct sk_buff *head, int gso, gfp_t gfp)
420{
421 struct sctp_transport *tp = packet->transport;
422 struct sctp_auth_chunk *auth = NULL;
423 struct sctp_chunk *chunk, *tmp;
424 int pkt_count = 0, pkt_size;
425 struct sock *sk = head->sk;
426 struct sk_buff *nskb;
427 int auth_len = 0;
428
429 if (gso) {
430 skb_shinfo(head)->gso_type = sk->sk_gso_type;
431 SCTP_OUTPUT_CB(head)->last = head;
432 } else {
433 nskb = head;
434 pkt_size = packet->size;
435 goto merge;
436 }
437
438 do {
439
440 pkt_size = packet->overhead;
441 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list,
442 list) {
443 int padded = SCTP_PAD4(chunk->skb->len);
444
445 if (chunk == packet->auth)
446 auth_len = padded;
447 else if (auth_len + padded + packet->overhead >
448 tp->pathmtu)
449 return 0;
450 else if (pkt_size + padded > tp->pathmtu)
451 break;
452 pkt_size += padded;
453 }
454 nskb = alloc_skb(pkt_size + MAX_HEADER, gfp);
455 if (!nskb)
456 return 0;
457 skb_reserve(nskb, packet->overhead + MAX_HEADER);
458
459merge:
460
461 pkt_size -= packet->overhead;
462 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
463 int padding;
464
465 list_del_init(&chunk->list);
466 if (sctp_chunk_is_data(chunk)) {
467 if (!sctp_chunk_retransmitted(chunk) &&
468 !tp->rto_pending) {
469 chunk->rtt_in_progress = 1;
470 tp->rto_pending = 1;
471 }
472 }
473
474 padding = SCTP_PAD4(chunk->skb->len) - chunk->skb->len;
475 if (padding)
476 skb_put_zero(chunk->skb, padding);
477
478 if (chunk == packet->auth)
479 auth = (struct sctp_auth_chunk *)
480 skb_tail_pointer(nskb);
481
482 skb_put_data(nskb, chunk->skb->data, chunk->skb->len);
483
484 pr_debug("*** Chunk:%p[%s] %s 0x%x, length:%d, chunk->skb->len:%d, rtt_in_progress:%d\n",
485 chunk,
486 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)),
487 chunk->has_tsn ? "TSN" : "No TSN",
488 chunk->has_tsn ? ntohl(chunk->subh.data_hdr->tsn) : 0,
489 ntohs(chunk->chunk_hdr->length), chunk->skb->len,
490 chunk->rtt_in_progress);
491
492 pkt_size -= SCTP_PAD4(chunk->skb->len);
493
494 if (!sctp_chunk_is_data(chunk) && chunk != packet->auth)
495 sctp_chunk_free(chunk);
496
497 if (!pkt_size)
498 break;
499 }
500
501 if (auth) {
502 sctp_auth_calculate_hmac(tp->asoc, nskb, auth,
503 packet->auth->shkey, gfp);
504
505 if (list_empty(&packet->chunk_list))
506 sctp_chunk_free(packet->auth);
507 else
508 list_add(&packet->auth->list,
509 &packet->chunk_list);
510 }
511
512 if (gso)
513 sctp_packet_gso_append(head, nskb);
514
515 pkt_count++;
516 } while (!list_empty(&packet->chunk_list));
517
518 if (gso) {
519 memset(head->cb, 0, max(sizeof(struct inet_skb_parm),
520 sizeof(struct inet6_skb_parm)));
521 skb_shinfo(head)->gso_segs = pkt_count;
522 skb_shinfo(head)->gso_size = GSO_BY_FRAGS;
523 rcu_read_lock();
524 if (skb_dst(head) != tp->dst) {
525 dst_hold(tp->dst);
526 sk_setup_caps(sk, tp->dst);
527 }
528 rcu_read_unlock();
529 goto chksum;
530 }
531
532 if (sctp_checksum_disable)
533 return 1;
534
535 if (!(skb_dst(head)->dev->features & NETIF_F_SCTP_CRC) ||
536 dst_xfrm(skb_dst(head)) || packet->ipfragok) {
537 struct sctphdr *sh =
538 (struct sctphdr *)skb_transport_header(head);
539
540 sh->checksum = sctp_compute_cksum(head, 0);
541 } else {
542chksum:
543 head->ip_summed = CHECKSUM_PARTIAL;
544 head->csum_not_inet = 1;
545 head->csum_start = skb_transport_header(head) - head->head;
546 head->csum_offset = offsetof(struct sctphdr, checksum);
547 }
548
549 return pkt_count;
550}
551
552
553
554
555
556
557int sctp_packet_transmit(struct sctp_packet *packet, gfp_t gfp)
558{
559 struct sctp_transport *tp = packet->transport;
560 struct sctp_association *asoc = tp->asoc;
561 struct sctp_chunk *chunk, *tmp;
562 int pkt_count, gso = 0;
563 struct dst_entry *dst;
564 struct sk_buff *head;
565 struct sctphdr *sh;
566 struct sock *sk;
567
568 pr_debug("%s: packet:%p\n", __func__, packet);
569 if (list_empty(&packet->chunk_list))
570 return 0;
571 chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
572 sk = chunk->skb->sk;
573
574
575 if (packet->size > tp->pathmtu && !packet->ipfragok) {
576 if (!sk_can_gso(sk)) {
577 pr_err_once("Trying to GSO but underlying device doesn't support it.");
578 goto out;
579 }
580 gso = 1;
581 }
582
583
584 head = alloc_skb((gso ? packet->overhead : packet->size) +
585 MAX_HEADER, gfp);
586 if (!head)
587 goto out;
588 skb_reserve(head, packet->overhead + MAX_HEADER);
589 skb_set_owner_w(head, sk);
590
591
592 sh = skb_push(head, sizeof(struct sctphdr));
593 skb_reset_transport_header(head);
594 sh->source = htons(packet->source_port);
595 sh->dest = htons(packet->destination_port);
596 sh->vtag = htonl(packet->vtag);
597 sh->checksum = 0;
598
599
600 dst = dst_clone(tp->dst);
601 if (!dst) {
602 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
603 kfree_skb(head);
604 goto out;
605 }
606 skb_dst_set(head, dst);
607
608
609 pkt_count = sctp_packet_pack(packet, head, gso, gfp);
610 if (!pkt_count) {
611 kfree_skb(head);
612 goto out;
613 }
614 pr_debug("***sctp_transmit_packet*** skb->len:%d\n", head->len);
615
616
617 if (packet->has_data && sctp_state(asoc, ESTABLISHED) &&
618 asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
619 struct timer_list *timer =
620 &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
621 unsigned long timeout =
622 asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
623
624 if (!mod_timer(timer, jiffies + timeout))
625 sctp_association_hold(asoc);
626 }
627
628
629 tp->af_specific->ecn_capable(sk);
630 if (asoc) {
631 asoc->stats.opackets += pkt_count;
632 if (asoc->peer.last_sent_to != tp)
633 asoc->peer.last_sent_to = tp;
634 }
635 head->ignore_df = packet->ipfragok;
636 if (tp->dst_pending_confirm)
637 skb_set_dst_pending_confirm(head, 1);
638
639
640
641 if (tp->af_specific->sctp_xmit(head, tp) >= 0 &&
642 tp->dst_pending_confirm)
643 tp->dst_pending_confirm = 0;
644
645out:
646 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
647 list_del_init(&chunk->list);
648 if (!sctp_chunk_is_data(chunk))
649 sctp_chunk_free(chunk);
650 }
651 sctp_packet_reset(packet);
652 return 0;
653}
654
655
656
657
658
659
660static enum sctp_xmit sctp_packet_can_append_data(struct sctp_packet *packet,
661 struct sctp_chunk *chunk)
662{
663 size_t datasize, rwnd, inflight, flight_size;
664 struct sctp_transport *transport = packet->transport;
665 struct sctp_association *asoc = transport->asoc;
666 struct sctp_outq *q = &asoc->outqueue;
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681 rwnd = asoc->peer.rwnd;
682 inflight = q->outstanding_bytes;
683 flight_size = transport->flight_size;
684
685 datasize = sctp_data_size(chunk);
686
687 if (datasize > rwnd && inflight > 0)
688
689
690
691 return SCTP_XMIT_RWND_FULL;
692
693
694
695
696
697
698
699
700
701
702
703
704
705 if (chunk->fast_retransmit != SCTP_NEED_FRTX &&
706 flight_size >= transport->cwnd)
707 return SCTP_XMIT_RWND_FULL;
708
709
710
711
712
713
714
715 if ((sctp_sk(asoc->base.sk)->nodelay || inflight == 0) &&
716 !asoc->force_delay)
717
718 return SCTP_XMIT_OK;
719
720 if (!sctp_packet_empty(packet))
721
722 return SCTP_XMIT_OK;
723
724 if (!sctp_state(asoc, ESTABLISHED))
725 return SCTP_XMIT_OK;
726
727
728
729
730 if (chunk->skb->len + q->out_qlen > transport->pathmtu -
731 packet->overhead - sctp_datachk_len(&chunk->asoc->stream) - 4)
732
733 return SCTP_XMIT_OK;
734
735
736 if (!chunk->msg->can_delay)
737 return SCTP_XMIT_OK;
738
739
740 return SCTP_XMIT_DELAY;
741}
742
743
744static void sctp_packet_append_data(struct sctp_packet *packet,
745 struct sctp_chunk *chunk)
746{
747 struct sctp_transport *transport = packet->transport;
748 size_t datasize = sctp_data_size(chunk);
749 struct sctp_association *asoc = transport->asoc;
750 u32 rwnd = asoc->peer.rwnd;
751
752
753 transport->flight_size += datasize;
754
755
756 asoc->outqueue.outstanding_bytes += datasize;
757
758
759 if (datasize < rwnd)
760 rwnd -= datasize;
761 else
762 rwnd = 0;
763
764 asoc->peer.rwnd = rwnd;
765 sctp_chunk_assign_tsn(chunk);
766 asoc->stream.si->assign_number(chunk);
767}
768
769static enum sctp_xmit sctp_packet_will_fit(struct sctp_packet *packet,
770 struct sctp_chunk *chunk,
771 u16 chunk_len)
772{
773 enum sctp_xmit retval = SCTP_XMIT_OK;
774 size_t psize, pmtu, maxsize;
775
776
777
778
779
780
781 if ((packet->auth && chunk->shkey != packet->auth->shkey) ||
782 (!packet->auth && chunk->shkey &&
783 chunk->chunk_hdr->type != SCTP_CID_AUTH))
784 return SCTP_XMIT_PMTU_FULL;
785
786 psize = packet->size;
787 if (packet->transport->asoc)
788 pmtu = packet->transport->asoc->pathmtu;
789 else
790 pmtu = packet->transport->pathmtu;
791
792
793 if (psize + chunk_len > pmtu) {
794
795
796
797
798
799
800
801 if (sctp_packet_empty(packet) ||
802 (!packet->has_data && chunk->auth)) {
803
804
805
806
807 packet->ipfragok = 1;
808 goto out;
809 }
810
811
812
813
814
815
816 maxsize = pmtu - packet->overhead;
817 if (packet->auth)
818 maxsize -= SCTP_PAD4(packet->auth->skb->len);
819 if (chunk_len > maxsize)
820 retval = SCTP_XMIT_PMTU_FULL;
821
822
823
824
825
826
827
828 if (!sctp_chunk_is_data(chunk) && packet->has_data)
829 retval = SCTP_XMIT_PMTU_FULL;
830
831 if (psize + chunk_len > packet->max_size)
832
833 retval = SCTP_XMIT_PMTU_FULL;
834
835 if (!packet->transport->burst_limited &&
836 psize + chunk_len > (packet->transport->cwnd >> 1))
837
838
839
840 retval = SCTP_XMIT_PMTU_FULL;
841
842 if (packet->transport->burst_limited &&
843 psize + chunk_len > (packet->transport->burst_limited >> 1))
844
845
846
847 retval = SCTP_XMIT_PMTU_FULL;
848
849 }
850
851out:
852 return retval;
853}
854