-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathuser_socket.c
executable file
·3590 lines (3286 loc) · 87.9 KB
/
user_socket.c
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 1982, 1986, 1988, 1990, 1993
* The Regents of the University of California.
* Copyright (c) 2004 The FreeBSD Foundation
* Copyright (c) 2004-2008 Robert N. M. Watson
* Copyright (c) 2009-2010 Brad Penoff
* Copyright (c) 2009-2010 Humaira Kamal
* Copyright (c) 2011-2012 Irene Ruengeler
* Copyright (c) 2011-2012 Michael Tuexen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <netinet/sctp_os.h>
#include <netinet/sctp_pcb.h>
#include <netinet/sctputil.h>
#include <netinet/sctp_var.h>
#include <netinet/sctp_sysctl.h>
#include <netinet/sctp_input.h>
#include <netinet/sctp_peeloff.h>
#include <netinet/sctp_callout.h>
#include <netinet/sctp_crc32.h>
#ifdef INET6
#include <netinet6/sctp6_var.h>
#endif
#if defined(__FreeBSD__)
#include <sys/param.h>
#endif
#if defined(__linux__)
#define __FAVOR_BSD /* (on Ubuntu at least) enables UDP header field names like BSD in RFC 768 */
#endif
#if !defined(_WIN32)
#if defined INET || defined INET6
#include <netinet/udp.h>
#endif
#include <arpa/inet.h>
#else
#include <user_socketvar.h>
#endif
userland_mutex_t accept_mtx;
userland_cond_t accept_cond;
#ifdef _WIN32
#include <time.h>
#include <sys/timeb.h>
#endif
MALLOC_DEFINE(M_PCB, "sctp_pcb", "sctp pcb");
MALLOC_DEFINE(M_SONAME, "sctp_soname", "sctp soname");
#define MAXLEN_MBUF_CHAIN 32
/* Prototypes */
extern int sctp_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
struct mbuf *top, struct mbuf *control, int flags,
/* proc is a dummy in __Userspace__ and will not be passed to sctp_lower_sosend */
struct proc *p);
extern int sctp_attach(struct socket *so, int proto, uint32_t vrf_id);
extern int sctpconn_attach(struct socket *so, int proto, uint32_t vrf_id);
static void init_sync(void) {
#if defined(_WIN32)
#if defined(INET) || defined(INET6)
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
SCTP_PRINTF("WSAStartup failed\n");
exit (-1);
}
#endif
InitializeConditionVariable(&accept_cond);
InitializeCriticalSection(&accept_mtx);
#else
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
#ifdef INVARIANTS
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
#endif
pthread_mutex_init(&accept_mtx, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
pthread_cond_init(&accept_cond, NULL);
#endif
}
void
usrsctp_init(uint16_t port,
int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df),
void (*debug_printf)(const char *format, ...))
{
init_sync();
sctp_init(port, conn_output, debug_printf, 1);
}
void
usrsctp_init_nothreads(uint16_t port,
int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df),
void (*debug_printf)(const char *format, ...))
{
init_sync();
sctp_init(port, conn_output, debug_printf, 0);
}
/* Taken from usr/src/sys/kern/uipc_sockbuf.c and modified for __Userspace__*/
/*
* Socantsendmore indicates that no more data will be sent on the socket; it
* would normally be applied to a socket when the user informs the system
* that no more data is to be sent, by the protocol code (in case
* PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be
* received, and will normally be applied to the socket by a protocol when it
* detects that the peer will send no more data. Data queued for reading in
* the socket may yet be read.
*/
void socantrcvmore_locked(struct socket *so)
{
SOCKBUF_LOCK_ASSERT(&so->so_rcv);
so->so_rcv.sb_state |= SBS_CANTRCVMORE;
sorwakeup_locked(so);
}
void socantrcvmore(struct socket *so)
{
SOCKBUF_LOCK(&so->so_rcv);
socantrcvmore_locked(so);
}
void
socantsendmore_locked(struct socket *so)
{
SOCKBUF_LOCK_ASSERT(&so->so_snd);
so->so_snd.sb_state |= SBS_CANTSENDMORE;
sowwakeup_locked(so);
}
void
socantsendmore(struct socket *so)
{
SOCKBUF_LOCK(&so->so_snd);
socantsendmore_locked(so);
}
/* Taken from usr/src/sys/kern/uipc_sockbuf.c and called within sctp_lower_sosend.
*/
int
sbwait(struct sockbuf *sb)
{
SOCKBUF_LOCK_ASSERT(sb);
sb->sb_flags |= SB_WAIT;
#if defined(_WIN32)
if (SleepConditionVariableCS(&(sb->sb_cond), &(sb->sb_mtx), INFINITE))
return (0);
else
return (-1);
#else
return (pthread_cond_wait(&(sb->sb_cond), &(sb->sb_mtx)));
#endif
}
/* Taken from /src/sys/kern/uipc_socket.c
* and modified for __Userspace__
*/
static struct socket *
soalloc(void)
{
struct socket *so;
/*
* soalloc() sets of socket layer state for a socket,
* called only by socreate() and sonewconn().
*
* sodealloc() tears down socket layer state for a socket,
* called only by sofree() and sonewconn().
* __Userspace__ TODO : Make sure so is properly deallocated
* when tearing down the connection.
*/
so = (struct socket *)malloc(sizeof(struct socket));
if (so == NULL) {
return (NULL);
}
memset(so, 0, sizeof(struct socket));
/* __Userspace__ Initializing the socket locks here */
SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
SOCKBUF_COND_INIT(&so->so_snd);
SOCKBUF_COND_INIT(&so->so_rcv);
SOCK_COND_INIT(so); /* timeo_cond */
/* __Userspace__ Any ref counting required here? Will we have any use for aiojobq?
What about gencnt and numopensockets?*/
TAILQ_INIT(&so->so_aiojobq);
return (so);
}
static void
sodealloc(struct socket *so)
{
KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
SOCKBUF_COND_DESTROY(&so->so_snd);
SOCKBUF_COND_DESTROY(&so->so_rcv);
SOCK_COND_DESTROY(so);
SOCKBUF_LOCK_DESTROY(&so->so_snd);
SOCKBUF_LOCK_DESTROY(&so->so_rcv);
free(so);
}
/* Taken from /src/sys/kern/uipc_socket.c
* and modified for __Userspace__
*/
void
sofree(struct socket *so)
{
struct socket *head;
ACCEPT_LOCK_ASSERT();
SOCK_LOCK_ASSERT(so);
/* SS_NOFDREF unset in accept call. this condition seems irrelevent
* for __Userspace__...
*/
if (so->so_count != 0 ||
(so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
SOCK_UNLOCK(so);
ACCEPT_UNLOCK();
return;
}
head = so->so_head;
if (head != NULL) {
KASSERT((so->so_qstate & SQ_COMP) != 0 ||
(so->so_qstate & SQ_INCOMP) != 0,
("sofree: so_head != NULL, but neither SQ_COMP nor "
"SQ_INCOMP"));
KASSERT((so->so_qstate & SQ_COMP) == 0 ||
(so->so_qstate & SQ_INCOMP) == 0,
("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
TAILQ_REMOVE(&head->so_incomp, so, so_list);
head->so_incqlen--;
so->so_qstate &= ~SQ_INCOMP;
so->so_head = NULL;
}
KASSERT((so->so_qstate & SQ_COMP) == 0 &&
(so->so_qstate & SQ_INCOMP) == 0,
("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
if (so->so_options & SCTP_SO_ACCEPTCONN) {
KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated"));
KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_comp populated"));
}
SOCK_UNLOCK(so);
ACCEPT_UNLOCK();
sctp_close(so); /* was... sctp_detach(so); */
/*
* From this point on, we assume that no other references to this
* socket exist anywhere else in the stack. Therefore, no locks need
* to be acquired or held.
*
* We used to do a lot of socket buffer and socket locking here, as
* well as invoke sorflush() and perform wakeups. The direct call to
* dom_dispose() and sbrelease_internal() are an inlining of what was
* necessary from sorflush().
*
* Notice that the socket buffer and kqueue state are torn down
* before calling pru_detach. This means that protocols shold not
* assume they can perform socket wakeups, etc, in their detach code.
*/
sodealloc(so);
}
/* Taken from /src/sys/kern/uipc_socket.c */
void
soabort(struct socket *so)
{
#if defined(INET6)
struct sctp_inpcb *inp;
#endif
#if defined(INET6)
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
sctp6_abort(so);
} else {
#if defined(INET)
sctp_abort(so);
#endif
}
#elif defined(INET)
sctp_abort(so);
#endif
ACCEPT_LOCK();
SOCK_LOCK(so);
sofree(so);
}
/* Taken from usr/src/sys/kern/uipc_socket.c and called within sctp_connect (sctp_usrreq.c).
* We use sctp_connect for send_one_init_real in ms1.
*/
void
soisconnecting(struct socket *so)
{
SOCK_LOCK(so);
so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
so->so_state |= SS_ISCONNECTING;
SOCK_UNLOCK(so);
}
/* Taken from usr/src/sys/kern/uipc_socket.c and called within sctp_disconnect (sctp_usrreq.c).
* TODO Do we use sctp_disconnect?
*/
void
soisdisconnecting(struct socket *so)
{
/*
* Note: This code assumes that SOCK_LOCK(so) and
* SOCKBUF_LOCK(&so->so_rcv) are the same.
*/
SOCKBUF_LOCK(&so->so_rcv);
so->so_state &= ~SS_ISCONNECTING;
so->so_state |= SS_ISDISCONNECTING;
so->so_rcv.sb_state |= SBS_CANTRCVMORE;
sorwakeup_locked(so);
SOCKBUF_LOCK(&so->so_snd);
so->so_snd.sb_state |= SBS_CANTSENDMORE;
sowwakeup_locked(so);
wakeup("dummy",so);
/* requires 2 args but this was in orig */
/* wakeup(&so->so_timeo); */
}
/* Taken from sys/kern/kern_synch.c and
modified for __Userspace__
*/
/*
* Make all threads sleeping on the specified identifier runnable.
* Associating wakeup with so_timeo identifier and timeo_cond
* condition variable. TODO. If we use iterator thread then we need to
* modify wakeup so it can distinguish between iterator identifier and
* timeo identifier.
*/
void
wakeup(void *ident, struct socket *so)
{
SOCK_LOCK(so);
#if defined(_WIN32)
WakeAllConditionVariable(&(so)->timeo_cond);
#else
pthread_cond_broadcast(&(so)->timeo_cond);
#endif
SOCK_UNLOCK(so);
}
/*
* Make a thread sleeping on the specified identifier runnable.
* May wake more than one thread if a target thread is currently
* swapped out.
*/
void
wakeup_one(void *ident)
{
/* __Userspace__ Check: We are using accept_cond for wakeup_one.
It seems that wakeup_one is only called within
soisconnected() and sonewconn() with ident &head->so_timeo
head is so->so_head, which is back pointer to listen socket
This seems to indicate that the use of accept_cond is correct
since socket where accepts occur is so_head in all
subsidiary sockets.
*/
ACCEPT_LOCK();
#if defined(_WIN32)
WakeAllConditionVariable(&accept_cond);
#else
pthread_cond_broadcast(&accept_cond);
#endif
ACCEPT_UNLOCK();
}
/* Called within sctp_process_cookie_[existing/new] */
void
soisconnected(struct socket *so)
{
struct socket *head;
ACCEPT_LOCK();
SOCK_LOCK(so);
so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
so->so_state |= SS_ISCONNECTED;
head = so->so_head;
if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
SOCK_UNLOCK(so);
TAILQ_REMOVE(&head->so_incomp, so, so_list);
head->so_incqlen--;
so->so_qstate &= ~SQ_INCOMP;
TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
head->so_qlen++;
so->so_qstate |= SQ_COMP;
ACCEPT_UNLOCK();
sorwakeup(head);
wakeup_one(&head->so_timeo);
return;
}
SOCK_UNLOCK(so);
ACCEPT_UNLOCK();
wakeup(&so->so_timeo, so);
sorwakeup(so);
sowwakeup(so);
}
/* called within sctp_handle_cookie_echo */
struct socket *
sonewconn(struct socket *head, int connstatus)
{
struct socket *so;
int over;
ACCEPT_LOCK();
over = (head->so_qlen > 3 * head->so_qlimit / 2);
ACCEPT_UNLOCK();
#ifdef REGRESSION
if (regression_sonewconn_earlytest && over)
#else
if (over)
#endif
return (NULL);
so = soalloc();
if (so == NULL)
return (NULL);
so->so_head = head;
so->so_type = head->so_type;
so->so_options = head->so_options &~ SCTP_SO_ACCEPTCONN;
so->so_linger = head->so_linger;
so->so_state = head->so_state | SS_NOFDREF;
so->so_dom = head->so_dom;
#ifdef MAC
SOCK_LOCK(head);
mac_create_socket_from_socket(head, so);
SOCK_UNLOCK(head);
#endif
if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) {
sodealloc(so);
return (NULL);
}
switch (head->so_dom) {
#ifdef INET
case AF_INET:
if (sctp_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) {
sodealloc(so);
return (NULL);
}
break;
#endif
#ifdef INET6
case AF_INET6:
if (sctp6_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) {
sodealloc(so);
return (NULL);
}
break;
#endif
case AF_CONN:
if (sctpconn_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) {
sodealloc(so);
return (NULL);
}
break;
default:
sodealloc(so);
return (NULL);
break;
}
so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
so->so_snd.sb_lowat = head->so_snd.sb_lowat;
so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
so->so_snd.sb_timeo = head->so_snd.sb_timeo;
so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
so->so_state |= connstatus;
ACCEPT_LOCK();
if (connstatus) {
TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
so->so_qstate |= SQ_COMP;
head->so_qlen++;
} else {
/*
* Keep removing sockets from the head until there's room for
* us to insert on the tail. In pre-locking revisions, this
* was a simple if (), but as we could be racing with other
* threads and soabort() requires dropping locks, we must
* loop waiting for the condition to be true.
*/
while (head->so_incqlen > head->so_qlimit) {
struct socket *sp;
sp = TAILQ_FIRST(&head->so_incomp);
TAILQ_REMOVE(&head->so_incomp, sp, so_list);
head->so_incqlen--;
sp->so_qstate &= ~SQ_INCOMP;
sp->so_head = NULL;
ACCEPT_UNLOCK();
soabort(sp);
ACCEPT_LOCK();
}
TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
so->so_qstate |= SQ_INCOMP;
head->so_incqlen++;
}
ACCEPT_UNLOCK();
if (connstatus) {
sorwakeup(head);
wakeup_one(&head->so_timeo);
}
return (so);
}
/*
Source: /src/sys/gnu/fs/xfs/FreeBSD/xfs_ioctl.c
*/
static __inline__ int
copy_to_user(void *dst, void *src, size_t len) {
memcpy(dst, src, len);
return 0;
}
static __inline__ int
copy_from_user(void *dst, void *src, size_t len) {
memcpy(dst, src, len);
return 0;
}
/*
References:
src/sys/dev/lmc/if_lmc.h:
src/sys/powerpc/powerpc/copyinout.c
src/sys/sys/systm.h
*/
# define copyin(u, k, len) copy_from_user(k, u, len)
/* References:
src/sys/powerpc/powerpc/copyinout.c
src/sys/sys/systm.h
*/
# define copyout(k, u, len) copy_to_user(u, k, len)
/* copyiniov definition copied/modified from src/sys/kern/kern_subr.c */
int
copyiniov(struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
{
u_int iovlen;
*iov = NULL;
if (iovcnt > UIO_MAXIOV)
return (error);
iovlen = iovcnt * sizeof (struct iovec);
*iov = malloc(iovlen); /*, M_IOV, M_WAITOK); */
error = copyin(iovp, *iov, iovlen);
if (error) {
free(*iov); /*, M_IOV); */
*iov = NULL;
}
return (error);
}
/* (__Userspace__) version of uiomove */
int
uiomove(void *cp, int n, struct uio *uio)
{
struct iovec *iov;
size_t cnt;
int error = 0;
if ((uio->uio_rw != UIO_READ) &&
(uio->uio_rw != UIO_WRITE)) {
return (EINVAL);
}
while (n > 0 && uio->uio_resid) {
iov = uio->uio_iov;
cnt = iov->iov_len;
if (cnt == 0) {
uio->uio_iov++;
uio->uio_iovcnt--;
continue;
}
if (cnt > (size_t)n)
cnt = n;
switch (uio->uio_segflg) {
case UIO_USERSPACE:
if (uio->uio_rw == UIO_READ)
error = copyout(cp, iov->iov_base, cnt);
else
error = copyin(iov->iov_base, cp, cnt);
if (error)
goto out;
break;
case UIO_SYSSPACE:
if (uio->uio_rw == UIO_READ)
memcpy(iov->iov_base, cp, cnt);
else
memcpy(cp, iov->iov_base, cnt);
break;
}
iov->iov_base = (char *)iov->iov_base + cnt;
iov->iov_len -= cnt;
uio->uio_resid -= cnt;
uio->uio_offset += (off_t)cnt;
cp = (char *)cp + cnt;
n -= (int)cnt;
}
out:
return (error);
}
/* Source: src/sys/kern/uipc_syscalls.c */
int
getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
{
struct sockaddr *sa;
int error;
if (len > SOCK_MAXADDRLEN)
return (ENAMETOOLONG);
if (len < offsetof(struct sockaddr, sa_data))
return (EINVAL);
MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
error = copyin(uaddr, sa, len);
if (error) {
FREE(sa, M_SONAME);
} else {
#ifdef HAVE_SA_LEN
sa->sa_len = len;
#endif
*namp = sa;
}
return (error);
}
int
usrsctp_getsockopt(struct socket *so, int level, int option_name,
void *option_value, socklen_t *option_len);
sctp_assoc_t
usrsctp_getassocid(struct socket *sock, struct sockaddr *sa)
{
struct sctp_paddrinfo sp;
socklen_t siz;
#ifndef HAVE_SA_LEN
size_t sa_len;
#endif
/* First get the assoc id */
siz = sizeof(sp);
memset(&sp, 0, sizeof(sp));
#ifdef HAVE_SA_LEN
memcpy((caddr_t)&sp.spinfo_address, sa, sa->sa_len);
#else
switch (sa->sa_family) {
#ifdef INET
case AF_INET:
sa_len = sizeof(struct sockaddr_in);
break;
#endif
#ifdef INET6
case AF_INET6:
sa_len = sizeof(struct sockaddr_in6);
break;
#endif
case AF_CONN:
sa_len = sizeof(struct sockaddr_conn);
break;
default:
sa_len = 0;
break;
}
memcpy((caddr_t)&sp.spinfo_address, sa, sa_len);
#endif
if (usrsctp_getsockopt(sock, IPPROTO_SCTP, SCTP_GET_PEER_ADDR_INFO, &sp, &siz) != 0) {
/* We depend on the fact that 0 can never be returned */
return ((sctp_assoc_t) 0);
}
return (sp.spinfo_assoc_id);
}
/* Taken from /src/lib/libc/net/sctp_sys_calls.c
* and modified for __Userspace__
* calling sctp_generic_sendmsg from this function
*/
ssize_t
userspace_sctp_sendmsg(struct socket *so,
const void *data,
size_t len,
struct sockaddr *to,
socklen_t tolen,
uint32_t ppid,
uint32_t flags,
uint16_t stream_no,
uint32_t timetolive,
uint32_t context)
{
struct sctp_sndrcvinfo sndrcvinfo, *sinfo = &sndrcvinfo;
struct uio auio;
struct iovec iov[1];
memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
sinfo->sinfo_ppid = ppid;
sinfo->sinfo_flags = flags;
sinfo->sinfo_stream = stream_no;
sinfo->sinfo_timetolive = timetolive;
sinfo->sinfo_context = context;
sinfo->sinfo_assoc_id = 0;
/* Perform error checks on destination (to) */
if (tolen > SOCK_MAXADDRLEN) {
errno = ENAMETOOLONG;
return (-1);
}
if ((tolen > 0) &&
((to == NULL) || (tolen < (socklen_t)sizeof(struct sockaddr)))) {
errno = EINVAL;
return (-1);
}
if (data == NULL) {
errno = EFAULT;
return (-1);
}
/* Adding the following as part of defensive programming, in case the application
does not do it when preparing the destination address.*/
#ifdef HAVE_SA_LEN
if (to != NULL) {
to->sa_len = tolen;
}
#endif
iov[0].iov_base = (caddr_t)data;
iov[0].iov_len = len;
auio.uio_iov = iov;
auio.uio_iovcnt = 1;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_rw = UIO_WRITE;
auio.uio_offset = 0; /* XXX */
auio.uio_resid = len;
errno = sctp_lower_sosend(so, to, &auio, NULL, NULL, 0, sinfo);
if (errno == 0) {
return (len - auio.uio_resid);
} else {
return (-1);
}
}
ssize_t
usrsctp_sendv(struct socket *so,
const void *data,
size_t len,
struct sockaddr *to,
int addrcnt,
void *info,
socklen_t infolen,
unsigned int infotype,
int flags)
{
struct sctp_sndrcvinfo sinfo;
struct uio auio;
struct iovec iov[1];
int use_sinfo;
sctp_assoc_t *assoc_id;
if (so == NULL) {
errno = EBADF;
return (-1);
}
if (data == NULL) {
errno = EFAULT;
return (-1);
}
memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
assoc_id = NULL;
use_sinfo = 0;
switch (infotype) {
case SCTP_SENDV_NOINFO:
if ((infolen != 0) || (info != NULL)) {
errno = EINVAL;
return (-1);
}
break;
case SCTP_SENDV_SNDINFO:
if ((info == NULL) || (infolen != sizeof(struct sctp_sndinfo))) {
errno = EINVAL;
return (-1);
}
sinfo.sinfo_stream = ((struct sctp_sndinfo *)info)->snd_sid;
sinfo.sinfo_flags = ((struct sctp_sndinfo *)info)->snd_flags;
sinfo.sinfo_ppid = ((struct sctp_sndinfo *)info)->snd_ppid;
sinfo.sinfo_context = ((struct sctp_sndinfo *)info)->snd_context;
sinfo.sinfo_assoc_id = ((struct sctp_sndinfo *)info)->snd_assoc_id;
assoc_id = &(((struct sctp_sndinfo *)info)->snd_assoc_id);
use_sinfo = 1;
break;
case SCTP_SENDV_PRINFO:
if ((info == NULL) || (infolen != sizeof(struct sctp_prinfo))) {
errno = EINVAL;
return (-1);
}
sinfo.sinfo_stream = 0;
sinfo.sinfo_flags = PR_SCTP_POLICY(((struct sctp_prinfo *)info)->pr_policy);
sinfo.sinfo_timetolive = ((struct sctp_prinfo *)info)->pr_value;
use_sinfo = 1;
break;
case SCTP_SENDV_AUTHINFO:
errno = EINVAL;
return (-1);
case SCTP_SENDV_SPA:
if ((info == NULL) || (infolen != sizeof(struct sctp_sendv_spa))) {
errno = EINVAL;
return (-1);
}
if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_SNDINFO_VALID) {
sinfo.sinfo_stream = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_sid;
sinfo.sinfo_flags = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_flags;
sinfo.sinfo_ppid = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_ppid;
sinfo.sinfo_context = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_context;
sinfo.sinfo_assoc_id = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_assoc_id;
assoc_id = &(((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_assoc_id);
} else {
sinfo.sinfo_flags = 0;
sinfo.sinfo_stream = 0;
}
if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_PRINFO_VALID) {
sinfo.sinfo_flags |= PR_SCTP_POLICY(((struct sctp_sendv_spa *)info)->sendv_prinfo.pr_policy);
sinfo.sinfo_timetolive = ((struct sctp_sendv_spa *)info)->sendv_prinfo.pr_value;
}
if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_AUTHINFO_VALID) {
errno = EINVAL;
return (-1);
}
use_sinfo = 1;
break;
default:
errno = EINVAL;
return (-1);
}
/* Perform error checks on destination (to) */
if (addrcnt > 1) {
errno = EINVAL;
return (-1);
}
iov[0].iov_base = (caddr_t)data;
iov[0].iov_len = len;
auio.uio_iov = iov;
auio.uio_iovcnt = 1;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_rw = UIO_WRITE;
auio.uio_offset = 0; /* XXX */
auio.uio_resid = len;
errno = sctp_lower_sosend(so, to, &auio, NULL, NULL, flags, use_sinfo ? &sinfo : NULL);
if (errno == 0) {
if ((to != NULL) && (assoc_id != NULL)) {
*assoc_id = usrsctp_getassocid(so, to);
}
return (len - auio.uio_resid);
} else {
return (-1);
}
}
ssize_t
userspace_sctp_sendmbuf(struct socket *so,
struct mbuf* mbufdata,
size_t len,
struct sockaddr *to,
socklen_t tolen,
uint32_t ppid,
uint32_t flags,
uint16_t stream_no,
uint32_t timetolive,
uint32_t context)
{
struct sctp_sndrcvinfo sndrcvinfo, *sinfo = &sndrcvinfo;
/* struct uio auio;
struct iovec iov[1]; */
int error = 0;
int uflags = 0;
ssize_t retval;
sinfo->sinfo_ppid = ppid;
sinfo->sinfo_flags = flags;
sinfo->sinfo_stream = stream_no;
sinfo->sinfo_timetolive = timetolive;
sinfo->sinfo_context = context;
sinfo->sinfo_assoc_id = 0;
/* Perform error checks on destination (to) */
if (tolen > SOCK_MAXADDRLEN){
error = (ENAMETOOLONG);
goto sendmsg_return;
}
if (tolen < (socklen_t)offsetof(struct sockaddr, sa_data)){
error = (EINVAL);
goto sendmsg_return;
}
/* Adding the following as part of defensive programming, in case the application
does not do it when preparing the destination address.*/
#ifdef HAVE_SA_LEN
to->sa_len = tolen;
#endif
error = sctp_lower_sosend(so, to, NULL/*uio*/,
(struct mbuf *)mbufdata, (struct mbuf *)NULL,
uflags, sinfo);
sendmsg_return:
/* TODO: Needs a condition for non-blocking when error is EWOULDBLOCK */
if (0 == error)
retval = len;
else if (error == EWOULDBLOCK) {
errno = EWOULDBLOCK;
retval = -1;
} else {
SCTP_PRINTF("%s: error = %d\n", __func__, error);
errno = error;
retval = -1;
}
return (retval);
}
/* taken from usr.lib/sctp_sys_calls.c and needed here */
#define SCTP_SMALL_IOVEC_SIZE 2
/* Taken from /src/lib/libc/net/sctp_sys_calls.c
* and modified for __Userspace__
* calling sctp_generic_recvmsg from this function
*/
ssize_t
userspace_sctp_recvmsg(struct socket *so,
void *dbuf,
size_t len,
struct sockaddr *from,
socklen_t *fromlenp,
struct sctp_sndrcvinfo *sinfo,
int *msg_flags)
{
struct uio auio;
struct iovec iov[SCTP_SMALL_IOVEC_SIZE];