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->truesize)))
706 goto fail;
707 }
708
709
710 skb = skb_clone(chunk->skb, gfp);
711 if (!skb)
712 goto fail;
713
714
715
716
717 if (sctp_tsnmap_mark(&asoc->peer.tsn_map,
718 ntohl(chunk->subh.data_hdr->tsn)))
719 goto fail_mark;
720
721
722
723
724
725
726
727
728
729
730
731
732
733 len = ntohs(chunk->chunk_hdr->length);
734 padding = WORD_ROUND(len) - len;
735
736
737 skb_trim(skb, chunk->chunk_end - padding - skb->data);
738
739
740 event = sctp_skb2event(skb);
741
742
743
744
745
746 sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
747
748 sctp_ulpevent_receive_data(event, asoc);
749
750 event->stream = ntohs(chunk->subh.data_hdr->stream);
751 event->ssn = ntohs(chunk->subh.data_hdr->ssn);
752 event->ppid = chunk->subh.data_hdr->ppid;
753 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
754 event->flags |= SCTP_UNORDERED;
755 event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
756 }
757 event->tsn = ntohl(chunk->subh.data_hdr->tsn);
758 event->msg_flags |= chunk->chunk_hdr->flags;
759 event->iif = sctp_chunk_iif(chunk);
760
761 return event;
762
763fail_mark:
764 kfree_skb(skb);
765fail:
766 return NULL;
767}
768
769
770
771
772
773
774
775
776
777struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
778 const struct sctp_association *asoc, __u32 indication,
779 gfp_t gfp)
780{
781 struct sctp_ulpevent *event;
782 struct sctp_pdapi_event *pd;
783 struct sk_buff *skb;
784
785 event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
786 MSG_NOTIFICATION, gfp);
787 if (!event)
788 goto fail;
789
790 skb = sctp_event2skb(event);
791 pd = (struct sctp_pdapi_event *)
792 skb_put(skb, sizeof(struct sctp_pdapi_event));
793
794
795
796
797
798
799
800 pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
801 pd->pdapi_flags = 0;
802
803
804
805
806
807
808
809 pd->pdapi_length = sizeof(struct sctp_pdapi_event);
810
811
812
813
814
815 pd->pdapi_indication = indication;
816
817
818
819
820
821 sctp_ulpevent_set_owner(event, asoc);
822 pd->pdapi_assoc_id = sctp_assoc2id(asoc);
823
824 return event;
825fail:
826 return NULL;
827}
828
829struct sctp_ulpevent *sctp_ulpevent_make_authkey(
830 const struct sctp_association *asoc, __u16 key_id,
831 __u32 indication, gfp_t gfp)
832{
833 struct sctp_ulpevent *event;
834 struct sctp_authkey_event *ak;
835 struct sk_buff *skb;
836
837 event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event),
838 MSG_NOTIFICATION, gfp);
839 if (!event)
840 goto fail;
841
842 skb = sctp_event2skb(event);
843 ak = (struct sctp_authkey_event *)
844 skb_put(skb, sizeof(struct sctp_authkey_event));
845
846 ak->auth_type = SCTP_AUTHENTICATION_EVENT;
847 ak->auth_flags = 0;
848 ak->auth_length = sizeof(struct sctp_authkey_event);
849
850 ak->auth_keynumber = key_id;
851 ak->auth_altkeynumber = 0;
852 ak->auth_indication = indication;
853
854
855
856
857 sctp_ulpevent_set_owner(event, asoc);
858 ak->auth_assoc_id = sctp_assoc2id(asoc);
859
860 return event;
861fail:
862 return NULL;
863}
864
865
866
867
868
869struct sctp_ulpevent *sctp_ulpevent_make_sender_dry_event(
870 const struct sctp_association *asoc, gfp_t gfp)
871{
872 struct sctp_ulpevent *event;
873 struct sctp_sender_dry_event *sdry;
874 struct sk_buff *skb;
875
876 event = sctp_ulpevent_new(sizeof(struct sctp_sender_dry_event),
877 MSG_NOTIFICATION, gfp);
878 if (!event)
879 return NULL;
880
881 skb = sctp_event2skb(event);
882 sdry = (struct sctp_sender_dry_event *)
883 skb_put(skb, sizeof(struct sctp_sender_dry_event));
884
885 sdry->sender_dry_type = SCTP_SENDER_DRY_EVENT;
886 sdry->sender_dry_flags = 0;
887 sdry->sender_dry_length = sizeof(struct sctp_sender_dry_event);
888 sctp_ulpevent_set_owner(event, asoc);
889 sdry->sender_dry_assoc_id = sctp_assoc2id(asoc);
890
891 return event;
892}
893
894
895
896
897__u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
898{
899 union sctp_notification *notification;
900 struct sk_buff *skb;
901
902 skb = sctp_event2skb(event);
903 notification = (union sctp_notification *) skb->data;
904 return notification->sn_header.sn_type;
905}
906
907
908void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
909 struct msghdr *msghdr)
910{
911 struct sctp_sndrcvinfo sinfo;
912
913 if (sctp_ulpevent_is_notification(event))
914 return;
915
916
917
918
919
920
921
922
923
924 sinfo.sinfo_stream = event->stream;
925
926
927
928
929
930
931
932 sinfo.sinfo_ssn = event->ssn;
933
934
935
936
937
938
939
940
941 sinfo.sinfo_ppid = event->ppid;
942
943
944
945
946
947
948
949
950
951
952 sinfo.sinfo_flags = event->flags;
953
954
955
956
957
958 sinfo.sinfo_tsn = event->tsn;
959
960
961
962
963
964
965
966 sinfo.sinfo_cumtsn = event->cumtsn;
967
968
969
970
971
972
973
974 sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
975
976
977 sinfo.sinfo_context = event->asoc->default_rcv_context;
978
979
980 sinfo.sinfo_timetolive = 0;
981
982 put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
983 sizeof(struct sctp_sndrcvinfo), (void *)&sinfo);
984}
985
986
987
988
989static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
990 struct sctp_association *asoc)
991{
992 struct sk_buff *skb, *frag;
993
994 skb = sctp_event2skb(event);
995
996 sctp_ulpevent_set_owner(event, asoc);
997 sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
998
999 if (!skb->data_len)
1000 return;
1001
1002
1003
1004
1005
1006
1007
1008 skb_walk_frags(skb, frag)
1009 sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
1010}
1011
1012
1013
1014
1015static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
1016{
1017 struct sk_buff *skb, *frag;
1018 unsigned int len;
1019
1020
1021
1022
1023
1024
1025
1026
1027 skb = sctp_event2skb(event);
1028 len = skb->len;
1029
1030 if (!skb->data_len)
1031 goto done;
1032
1033
1034 skb_walk_frags(skb, frag) {
1035
1036
1037
1038
1039 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
1040 }
1041
1042done:
1043 sctp_assoc_rwnd_increase(event->asoc, len);
1044 sctp_ulpevent_release_owner(event);
1045}
1046
1047static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
1048{
1049 struct sk_buff *skb, *frag;
1050
1051 skb = sctp_event2skb(event);
1052
1053 if (!skb->data_len)
1054 goto done;
1055
1056
1057 skb_walk_frags(skb, frag) {
1058
1059
1060
1061
1062 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
1063 }
1064
1065done:
1066 sctp_ulpevent_release_owner(event);
1067}
1068
1069
1070
1071
1072
1073void sctp_ulpevent_free(struct sctp_ulpevent *event)
1074{
1075 if (sctp_ulpevent_is_notification(event))
1076 sctp_ulpevent_release_owner(event);
1077 else
1078 sctp_ulpevent_release_data(event);
1079
1080 kfree_skb(sctp_event2skb(event));
1081}
1082
1083
1084unsigned int sctp_queue_purge_ulpevents(struct sk_buff_head *list)
1085{
1086 struct sk_buff *skb;
1087 unsigned int data_unread = 0;
1088
1089 while ((skb = skb_dequeue(list)) != NULL) {
1090 struct sctp_ulpevent *event = sctp_skb2event(skb);
1091
1092 if (!sctp_ulpevent_is_notification(event))
1093 data_unread += skb->len;
1094
1095 sctp_ulpevent_free(event);
1096 }
1097
1098 return data_unread;
1099}
1100