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