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