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#include <linux/slab.h>
47#include <linux/types.h>
48#include <linux/skbuff.h>
49#include <net/sctp/structs.h>
50#include <net/sctp/sctp.h>
51#include <net/sctp/sm.h>
52
53static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
54 struct sctp_association *asoc);
55static void sctp_ulpevent_release_data(struct sctp_ulpevent *event);
56static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event);
57
58
59
60SCTP_STATIC void sctp_ulpevent_init(struct sctp_ulpevent *event,
61 int msg_flags,
62 unsigned int len)
63{
64 memset(event, 0, sizeof(struct sctp_ulpevent));
65 event->msg_flags = msg_flags;
66 event->rmem_len = len;
67}
68
69
70SCTP_STATIC struct sctp_ulpevent *sctp_ulpevent_new(int size, int msg_flags,
71 gfp_t gfp)
72{
73 struct sctp_ulpevent *event;
74 struct sk_buff *skb;
75
76 skb = alloc_skb(size, gfp);
77 if (!skb)
78 goto fail;
79
80 event = sctp_skb2event(skb);
81 sctp_ulpevent_init(event, msg_flags, skb->truesize);
82
83 return event;
84
85fail:
86 return NULL;
87}
88
89
90int sctp_ulpevent_is_notification(const struct sctp_ulpevent *event)
91{
92 return MSG_NOTIFICATION == (event->msg_flags & MSG_NOTIFICATION);
93}
94
95
96
97
98static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
99 const struct sctp_association *asoc)
100{
101 struct sk_buff *skb;
102
103
104
105
106 sctp_association_hold((struct sctp_association *)asoc);
107 skb = sctp_event2skb(event);
108 event->asoc = (struct sctp_association *)asoc;
109 atomic_add(event->rmem_len, &event->asoc->rmem_alloc);
110 sctp_skb_set_owner_r(skb, asoc->base.sk);
111}
112
113
114static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
115{
116 struct sctp_association *asoc = event->asoc;
117
118 atomic_sub(event->rmem_len, &asoc->rmem_alloc);
119 sctp_association_put(asoc);
120}
121
122
123
124
125
126
127
128
129
130
131
132
133struct sctp_ulpevent *sctp_ulpevent_make_assoc_change(
134 const struct sctp_association *asoc,
135 __u16 flags, __u16 state, __u16 error, __u16 outbound,
136 __u16 inbound, struct sctp_chunk *chunk, gfp_t gfp)
137{
138 struct sctp_ulpevent *event;
139 struct sctp_assoc_change *sac;
140 struct sk_buff *skb;
141
142
143
144
145 if (chunk) {
146
147
148
149 skb = skb_copy_expand(chunk->skb,
150 sizeof(struct sctp_assoc_change), 0, gfp);
151
152 if (!skb)
153 goto fail;
154
155
156 event = sctp_skb2event(skb);
157 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
158
159
160 sac = (struct sctp_assoc_change *)
161 skb_push(skb, sizeof(struct sctp_assoc_change));
162
163
164 skb_trim(skb, sizeof(struct sctp_assoc_change) +
165 ntohs(chunk->chunk_hdr->length) -
166 sizeof(sctp_chunkhdr_t));
167 } else {
168 event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
169 MSG_NOTIFICATION, gfp);
170 if (!event)
171 goto fail;
172
173 skb = sctp_event2skb(event);
174 sac = (struct sctp_assoc_change *) skb_put(skb,
175 sizeof(struct sctp_assoc_change));
176 }
177
178
179
180
181
182
183
184 sac->sac_type = SCTP_ASSOC_CHANGE;
185
186
187
188
189
190
191
192
193 sac->sac_state = state;
194
195
196
197
198
199
200
201 sac->sac_flags = 0;
202
203
204
205
206
207
208
209
210 sac->sac_length = skb->len;
211
212
213
214
215
216
217
218
219
220
221
222 sac->sac_error = error;
223
224
225
226
227
228
229
230
231
232
233 sac->sac_outbound_streams = outbound;
234 sac->sac_inbound_streams = inbound;
235
236
237
238
239
240
241
242
243
244
245 sctp_ulpevent_set_owner(event, asoc);
246 sac->sac_assoc_id = sctp_assoc2id(asoc);
247
248 return event;
249
250fail:
251 return NULL;
252}
253
254
255
256
257
258
259
260
261
262struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
263 const struct sctp_association *asoc,
264 const struct sockaddr_storage *aaddr,
265 int flags, int state, int error, gfp_t gfp)
266{
267 struct sctp_ulpevent *event;
268 struct sctp_paddr_change *spc;
269 struct sk_buff *skb;
270
271 event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
272 MSG_NOTIFICATION, gfp);
273 if (!event)
274 goto fail;
275
276 skb = sctp_event2skb(event);
277 spc = (struct sctp_paddr_change *)
278 skb_put(skb, sizeof(struct sctp_paddr_change));
279
280
281
282
283
284
285
286
287 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
288
289
290
291
292
293
294
295
296
297 spc->spc_length = sizeof(struct sctp_paddr_change);
298
299
300
301
302
303
304
305 spc->spc_flags = 0;
306
307
308
309
310
311
312
313
314
315 spc->spc_state = state;
316
317
318
319
320
321
322
323
324
325
326 spc->spc_error = error;
327
328
329
330
331
332
333
334
335
336
337 sctp_ulpevent_set_owner(event, asoc);
338 spc->spc_assoc_id = sctp_assoc2id(asoc);
339
340
341
342
343
344
345
346
347
348 memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));
349
350
351 sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_v4map(
352 sctp_sk(asoc->base.sk),
353 (union sctp_addr *)&spc->spc_aaddr);
354
355 return event;
356
357fail:
358 return NULL;
359}
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376struct sctp_ulpevent *sctp_ulpevent_make_remote_error(
377 const struct sctp_association *asoc, struct sctp_chunk *chunk,
378 __u16 flags, gfp_t gfp)
379{
380 struct sctp_ulpevent *event;
381 struct sctp_remote_error *sre;
382 struct sk_buff *skb;
383 sctp_errhdr_t *ch;
384 __be16 cause;
385 int elen;
386
387 ch = (sctp_errhdr_t *)(chunk->skb->data);
388 cause = ch->cause;
389 elen = WORD_ROUND(ntohs(ch->length)) - sizeof(sctp_errhdr_t);
390
391
392 skb_pull(chunk->skb, sizeof(sctp_errhdr_t));
393
394
395
396
397 skb = skb_copy_expand(chunk->skb, sizeof(struct sctp_remote_error),
398 0, gfp);
399
400
401 skb_pull(chunk->skb, elen);
402 if (!skb)
403 goto fail;
404
405
406 event = sctp_skb2event(skb);
407 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
408
409 sre = (struct sctp_remote_error *)
410 skb_push(skb, sizeof(struct sctp_remote_error));
411
412
413 skb_trim(skb, sizeof(struct sctp_remote_error) + elen);
414
415
416
417
418
419
420
421 sre->sre_type = SCTP_REMOTE_ERROR;
422
423
424
425
426
427
428
429
430 sre->sre_flags = 0;
431
432
433
434
435
436
437
438
439
440 sre->sre_length = skb->len;
441
442
443
444
445
446
447
448
449 sre->sre_error = cause;
450
451
452
453
454
455
456
457
458
459
460 sctp_ulpevent_set_owner(event, asoc);
461 sre->sre_assoc_id = sctp_assoc2id(asoc);
462
463 return event;
464
465fail:
466 return NULL;
467}
468
469
470
471
472
473
474struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
475 const struct sctp_association *asoc, struct sctp_chunk *chunk,
476 __u16 flags, __u32 error, gfp_t gfp)
477{
478 struct sctp_ulpevent *event;
479 struct sctp_send_failed *ssf;
480 struct sk_buff *skb;
481
482
483 int len = ntohs(chunk->chunk_hdr->length);
484
485
486 skb = skb_copy_expand(chunk->skb,
487 sizeof(struct sctp_send_failed),
488 0,
489 gfp);
490 if (!skb)
491 goto fail;
492
493
494 skb_pull(skb, sizeof(struct sctp_data_chunk));
495 len -= sizeof(struct sctp_data_chunk);
496
497
498 event = sctp_skb2event(skb);
499 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
500
501 ssf = (struct sctp_send_failed *)
502 skb_push(skb, sizeof(struct sctp_send_failed));
503
504
505
506
507
508
509
510 ssf->ssf_type = SCTP_SEND_FAILED;
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525 ssf->ssf_flags = flags;
526
527
528
529
530
531
532
533
534 ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
535 skb_trim(skb, ssf->ssf_length);
536
537
538
539
540
541
542
543
544
545 ssf->ssf_error = error;
546
547
548
549
550
551
552
553
554 memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
555
556
557
558
559 ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
560
561
562
563
564
565
566
567
568
569
570 sctp_ulpevent_set_owner(event, asoc);
571 ssf->ssf_assoc_id = sctp_assoc2id(asoc);
572 return event;
573
574fail:
575 return NULL;
576}
577
578
579
580
581
582
583struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
584 const struct sctp_association *asoc,
585 __u16 flags, gfp_t gfp)
586{
587 struct sctp_ulpevent *event;
588 struct sctp_shutdown_event *sse;
589 struct sk_buff *skb;
590
591 event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),
592 MSG_NOTIFICATION, gfp);
593 if (!event)
594 goto fail;
595
596 skb = sctp_event2skb(event);
597 sse = (struct sctp_shutdown_event *)
598 skb_put(skb, sizeof(struct sctp_shutdown_event));
599
600
601
602
603
604
605
606 sse->sse_type = SCTP_SHUTDOWN_EVENT;
607
608
609
610
611
612
613
614 sse->sse_flags = 0;
615
616
617
618
619
620
621
622
623 sse->sse_length = sizeof(struct sctp_shutdown_event);
624
625
626
627
628
629
630
631
632
633 sctp_ulpevent_set_owner(event, asoc);
634 sse->sse_assoc_id = sctp_assoc2id(asoc);
635
636 return event;
637
638fail:
639 return NULL;
640}
641
642
643
644
645
646
647struct sctp_ulpevent *sctp_ulpevent_make_adaptation_indication(
648 const struct sctp_association *asoc, gfp_t gfp)
649{
650 struct sctp_ulpevent *event;
651 struct sctp_adaptation_event *sai;
652 struct sk_buff *skb;
653
654 event = sctp_ulpevent_new(sizeof(struct sctp_adaptation_event),
655 MSG_NOTIFICATION, gfp);
656 if (!event)
657 goto fail;
658
659 skb = sctp_event2skb(event);
660 sai = (struct sctp_adaptation_event *)
661 skb_put(skb, sizeof(struct sctp_adaptation_event));
662
663 sai->sai_type = SCTP_ADAPTATION_INDICATION;
664 sai->sai_flags = 0;
665 sai->sai_length = sizeof(struct sctp_adaptation_event);
666 sai->sai_adaptation_ind = asoc->peer.adaptation_ind;
667 sctp_ulpevent_set_owner(event, asoc);
668 sai->sai_assoc_id = sctp_assoc2id(asoc);
669
670 return event;
671
672fail:
673 return NULL;
674}
675
676
677
678
679
680
681
682
683struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
684 struct sctp_chunk *chunk,
685 gfp_t gfp)
686{
687 struct sctp_ulpevent *event = NULL;
688 struct sk_buff *skb;
689 size_t padding, len;
690 int rx_count;
691
692
693
694
695
696
697 if (asoc->ep->rcvbuf_policy)
698 rx_count = atomic_read(&asoc->rmem_alloc);
699 else
700 rx_count = atomic_read(&asoc->base.sk->sk_rmem_alloc);
701
702 if (rx_count >= asoc->base.sk->sk_rcvbuf) {
703
704 if ((asoc->base.sk->sk_userlocks & SOCK_RCVBUF_LOCK) ||
705 (!sk_rmem_schedule(asoc->base.sk, chunk->skb,
706 chunk->skb->truesize)))
707 goto fail;
708 }
709
710
711 skb = skb_clone(chunk->skb, gfp);
712 if (!skb)
713 goto fail;
714
715
716
717
718 if (sctp_tsnmap_mark(&asoc->peer.tsn_map,
719 ntohl(chunk->subh.data_hdr->tsn),
720 chunk->transport))
721 goto fail_mark;
722
723
724
725
726
727
728
729
730
731
732
733
734
735 len = ntohs(chunk->chunk_hdr->length);
736 padding = WORD_ROUND(len) - len;
737
738
739 skb_trim(skb, chunk->chunk_end - padding - skb->data);
740
741
742 event = sctp_skb2event(skb);
743
744
745
746
747
748 sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
749
750 sctp_ulpevent_receive_data(event, asoc);
751
752 event->stream = ntohs(chunk->subh.data_hdr->stream);
753 event->ssn = ntohs(chunk->subh.data_hdr->ssn);
754 event->ppid = chunk->subh.data_hdr->ppid;
755 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
756 event->flags |= SCTP_UNORDERED;
757 event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
758 }
759 event->tsn = ntohl(chunk->subh.data_hdr->tsn);
760 event->msg_flags |= chunk->chunk_hdr->flags;
761 event->iif = sctp_chunk_iif(chunk);
762
763 return event;
764
765fail_mark:
766 kfree_skb(skb);
767fail:
768 return NULL;
769}
770
771
772
773
774
775
776
777
778
779struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
780 const struct sctp_association *asoc, __u32 indication,
781 gfp_t gfp)
782{
783 struct sctp_ulpevent *event;
784 struct sctp_pdapi_event *pd;
785 struct sk_buff *skb;
786
787 event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
788 MSG_NOTIFICATION, gfp);
789 if (!event)
790 goto fail;
791
792 skb = sctp_event2skb(event);
793 pd = (struct sctp_pdapi_event *)
794 skb_put(skb, sizeof(struct sctp_pdapi_event));
795
796
797
798
799
800
801
802 pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
803 pd->pdapi_flags = 0;
804
805
806
807
808
809
810
811 pd->pdapi_length = sizeof(struct sctp_pdapi_event);
812
813
814
815
816
817 pd->pdapi_indication = indication;
818
819
820
821
822
823 sctp_ulpevent_set_owner(event, asoc);
824 pd->pdapi_assoc_id = sctp_assoc2id(asoc);
825
826 return event;
827fail:
828 return NULL;
829}
830
831struct sctp_ulpevent *sctp_ulpevent_make_authkey(
832 const struct sctp_association *asoc, __u16 key_id,
833 __u32 indication, gfp_t gfp)
834{
835 struct sctp_ulpevent *event;
836 struct sctp_authkey_event *ak;
837 struct sk_buff *skb;
838
839 event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event),
840 MSG_NOTIFICATION, gfp);
841 if (!event)
842 goto fail;
843
844 skb = sctp_event2skb(event);
845 ak = (struct sctp_authkey_event *)
846 skb_put(skb, sizeof(struct sctp_authkey_event));
847
848 ak->auth_type = SCTP_AUTHENTICATION_EVENT;
849 ak->auth_flags = 0;
850 ak->auth_length = sizeof(struct sctp_authkey_event);
851
852 ak->auth_keynumber = key_id;
853 ak->auth_altkeynumber = 0;
854 ak->auth_indication = indication;
855
856
857
858
859 sctp_ulpevent_set_owner(event, asoc);
860 ak->auth_assoc_id = sctp_assoc2id(asoc);
861
862 return event;
863fail:
864 return NULL;
865}
866
867
868
869
870
871struct sctp_ulpevent *sctp_ulpevent_make_sender_dry_event(
872 const struct sctp_association *asoc, gfp_t gfp)
873{
874 struct sctp_ulpevent *event;
875 struct sctp_sender_dry_event *sdry;
876 struct sk_buff *skb;
877
878 event = sctp_ulpevent_new(sizeof(struct sctp_sender_dry_event),
879 MSG_NOTIFICATION, gfp);
880 if (!event)
881 return NULL;
882
883 skb = sctp_event2skb(event);
884 sdry = (struct sctp_sender_dry_event *)
885 skb_put(skb, sizeof(struct sctp_sender_dry_event));
886
887 sdry->sender_dry_type = SCTP_SENDER_DRY_EVENT;
888 sdry->sender_dry_flags = 0;
889 sdry->sender_dry_length = sizeof(struct sctp_sender_dry_event);
890 sctp_ulpevent_set_owner(event, asoc);
891 sdry->sender_dry_assoc_id = sctp_assoc2id(asoc);
892
893 return event;
894}
895
896
897
898
899__u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
900{
901 union sctp_notification *notification;
902 struct sk_buff *skb;
903
904 skb = sctp_event2skb(event);
905 notification = (union sctp_notification *) skb->data;
906 return notification->sn_header.sn_type;
907}
908
909
910void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
911 struct msghdr *msghdr)
912{
913 struct sctp_sndrcvinfo sinfo;
914
915 if (sctp_ulpevent_is_notification(event))
916 return;
917
918
919
920
921
922
923
924
925
926 sinfo.sinfo_stream = event->stream;
927
928
929
930
931
932
933
934 sinfo.sinfo_ssn = event->ssn;
935
936
937
938
939
940
941
942
943 sinfo.sinfo_ppid = event->ppid;
944
945
946
947
948
949
950
951
952
953
954 sinfo.sinfo_flags = event->flags;
955
956
957
958
959
960 sinfo.sinfo_tsn = event->tsn;
961
962
963
964
965
966
967
968 sinfo.sinfo_cumtsn = event->cumtsn;
969
970
971
972
973
974
975
976 sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
977
978
979 sinfo.sinfo_context = event->asoc->default_rcv_context;
980
981
982 sinfo.sinfo_timetolive = 0;
983
984 put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
985 sizeof(struct sctp_sndrcvinfo), (void *)&sinfo);
986}
987
988
989
990
991static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
992 struct sctp_association *asoc)
993{
994 struct sk_buff *skb, *frag;
995
996 skb = sctp_event2skb(event);
997
998 sctp_ulpevent_set_owner(event, asoc);
999 sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
1000
1001 if (!skb->data_len)
1002 return;
1003
1004
1005
1006
1007
1008
1009
1010 skb_walk_frags(skb, frag)
1011 sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
1012}
1013
1014
1015
1016
1017static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
1018{
1019 struct sk_buff *skb, *frag;
1020 unsigned int len;
1021
1022
1023
1024
1025
1026
1027
1028
1029 skb = sctp_event2skb(event);
1030 len = skb->len;
1031
1032 if (!skb->data_len)
1033 goto done;
1034
1035
1036 skb_walk_frags(skb, frag) {
1037
1038
1039
1040
1041 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
1042 }
1043
1044done:
1045 sctp_assoc_rwnd_increase(event->asoc, len);
1046 sctp_ulpevent_release_owner(event);
1047}
1048
1049static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
1050{
1051 struct sk_buff *skb, *frag;
1052
1053 skb = sctp_event2skb(event);
1054
1055 if (!skb->data_len)
1056 goto done;
1057
1058
1059 skb_walk_frags(skb, frag) {
1060
1061
1062
1063
1064 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
1065 }
1066
1067done:
1068 sctp_ulpevent_release_owner(event);
1069}
1070
1071
1072
1073
1074
1075void sctp_ulpevent_free(struct sctp_ulpevent *event)
1076{
1077 if (sctp_ulpevent_is_notification(event))
1078 sctp_ulpevent_release_owner(event);
1079 else
1080 sctp_ulpevent_release_data(event);
1081
1082 kfree_skb(sctp_event2skb(event));
1083}
1084
1085
1086unsigned int sctp_queue_purge_ulpevents(struct sk_buff_head *list)
1087{
1088 struct sk_buff *skb;
1089 unsigned int data_unread = 0;
1090
1091 while ((skb = skb_dequeue(list)) != NULL) {
1092 struct sctp_ulpevent *event = sctp_skb2event(skb);
1093
1094 if (!sctp_ulpevent_is_notification(event))
1095 data_unread += skb->len;
1096
1097 sctp_ulpevent_free(event);
1098 }
1099
1100 return data_unread;
1101}
1102