forked from F-Stack/f-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathff_syscall_wrapper.c
1331 lines (1133 loc) · 30.4 KB
/
ff_syscall_wrapper.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) 2010 Kip Macy. All rights reserved.
* Copyright (C) 2017-2021 THL A29 Limited, a Tencent company.
* 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 COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
*
* Derived in part from libplebnet's pn_syscall_wrapper.c.
*/
#include <sys/param.h>
#include <sys/limits.h>
#include <sys/uio.h>
#include <sys/proc.h>
#include <sys/syscallsubr.h>
#include <sys/module.h>
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/socketvar.h>
#include <sys/event.h>
#include <sys/kernel.h>
#include <sys/refcount.h>
#include <sys/sysctl.h>
#include <sys/pcpu.h>
#include <sys/select.h>
#include <sys/poll.h>
#include <sys/event.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/ttycom.h>
#include <sys/filio.h>
#include <sys/sysproto.h>
#include <sys/fcntl.h>
#include <net/route.h>
#include <net/route/route_ctl.h>
#include <net/if.h>
#include <sys/sockio.h>
#include <machine/stdarg.h>
#include "ff_api.h"
#include "ff_host_interface.h"
/* setsockopt/getsockopt define start */
#define LINUX_SOL_SOCKET 1
#define LINUX_SO_DEBUG 1
#define LINUX_SO_REUSEADDR 2
#define LINUX_SO_ERROR 4
#define LINUX_SO_DONTROUTE 5
#define LINUX_SO_BROADCAST 6
#define LINUX_SO_SNDBUF 7
#define LINUX_SO_RCVBUF 8
#define LINUX_SO_KEEPALIVE 9
#define LINUX_SO_OOBINLINE 10
#define LINUX_SO_LINGER 13
#define LINUX_SO_REUSEPORT 15
#define LINUX_SO_RCVLOWAT 18
#define LINUX_SO_SNDLOWAT 19
#define LINUX_SO_RCVTIMEO 20
#define LINUX_SO_SNDTIMEO 21
#define LINUX_SO_ACCEPTCONN 30
#define LINUX_SO_PROTOCOL 38
#define LINUX_IP_TOS 1
#define LINUX_IP_TTL 2
#define LINUX_IP_HDRINCL 3
#define LINUX_IP_OPTIONS 4
#define LINUX_IP_MULTICAST_IF 32
#define LINUX_IP_MULTICAST_TTL 33
#define LINUX_IP_MULTICAST_LOOP 34
#define LINUX_IP_ADD_MEMBERSHIP 35
#define LINUX_IP_DROP_MEMBERSHIP 36
#define LINUX_IPV6_V6ONLY 26
#define LINUX_IPV6_RECVPKTINFO 49
#define LINUX_TCP_NODELAY 1
#define LINUX_TCP_MAXSEG 2
#define LINUX_TCP_KEEPIDLE 4
#define LINUX_TCP_KEEPINTVL 5
#define LINUX_TCP_KEEPCNT 6
#define LINUX_TCP_INFO 11
#define LINUX_TCP_MD5SIG 14
/* setsockopt/getsockopt define end */
/* ioctl define start */
#define LINUX_TIOCEXCL 0x540C
#define LINUX_TIOCNXCL 0x540D
#define LINUX_TIOCSCTTY 0x540E
#define LINUX_TIOCGPGRP 0x540F
#define LINUX_TIOCSPGRP 0x5410
#define LINUX_TIOCOUTQ 0x5411
#define LINUX_TIOCSTI 0x5412
#define LINUX_TIOCGWINSZ 0x5413
#define LINUX_TIOCSWINSZ 0x5414
#define LINUX_TIOCMGET 0x5415
#define LINUX_TIOCMBIS 0x5416
#define LINUX_TIOCMBIC 0x5417
#define LINUX_TIOCMSET 0x5418
#define LINUX_FIONREAD 0x541B
#define LINUX_TIOCCONS 0x541D
#define LINUX_TIOCPKT 0x5420
#define LINUX_FIONBIO 0x5421
#define LINUX_TIOCNOTTY 0x5422
#define LINUX_TIOCSETD 0x5423
#define LINUX_TIOCGETD 0x5424
#define LINUX_TIOCSBRK 0x5427
#define LINUX_TIOCCBRK 0x5428
#define LINUX_TIOCGSID 0x5429
#define LINUX_FIONCLEX 0x5450
#define LINUX_FIOCLEX 0x5451
#define LINUX_FIOASYNC 0x5452
#define LINUX_TIOCPKT_DATA 0
#define LINUX_TIOCPKT_FLUSHREAD 1
#define LINUX_TIOCPKT_FLUSHWRITE 2
#define LINUX_TIOCPKT_STOP 4
#define LINUX_TIOCPKT_START 8
#define LINUX_TIOCPKT_NOSTOP 16
#define LINUX_TIOCPKT_DOSTOP 32
#define LINUX_TIOCPKT_IOCTL 64
#define LINUX_SIOCGIFCONF 0x8912
#define LINUX_SIOCGIFFLAGS 0x8913
#define LINUX_SIOCSIFFLAGS 0x8914
#define LINUX_SIOCGIFADDR 0x8915
#define LINUX_SIOCSIFADDR 0x8916
#define LINUX_SIOCGIFDSTADDR 0x8917
#define LINUX_SIOCSIFDSTADDR 0x8918
#define LINUX_SIOCGIFBRDADDR 0x8919
#define LINUX_SIOCSIFBRDADDR 0x891a
#define LINUX_SIOCGIFNETMASK 0x891b
#define LINUX_SIOCSIFNETMASK 0x891c
#define LINUX_SIOCGIFMETRIC 0x891d
#define LINUX_SIOCSIFMETRIC 0x891e
#define LINUX_SIOCGIFMTU 0x8921
#define LINUX_SIOCSIFMTU 0x8922
#define LINUX_SIOCSIFNAME 0x8923
#define LINUX_SIOCADDMULTI 0x8931
#define LINUX_SIOCDELMULTI 0x8932
#define LINUX_SIOCGIFINDEX 0x8933
#define LINUX_SIOCDIFADDR 0x8936
/* ioctl define end */
/* af define start */
#define LINUX_AF_INET6 10
/* af define end */
/* msghdr define start */
struct linux_msghdr {
void *msg_name; /* Address to send to/receive from. */
socklen_t msg_namelen; /* Length of address data. */
struct iovec *msg_iov; /* Vector of data to send/receive into. */
size_t msg_iovlen; /* Number of elements in the vector. */
void *msg_control; /* Ancillary data (eg BSD filedesc passing). */
size_t msg_controllen; /* Ancillary data buffer length.
!! The type should be socklen_t but the
definition of the kernel is incompatible
with this. */
int msg_flags; /* Flags on received message. */
};
/* msghdr define end */
extern int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
static long
linux2freebsd_ioctl(unsigned long request)
{
switch(request) {
case LINUX_TIOCEXCL:
return TIOCEXCL;
case LINUX_TIOCNXCL:
return TIOCNXCL;
case LINUX_TIOCSCTTY:
return TIOCSCTTY;
case LINUX_TIOCGPGRP:
return TIOCGPGRP;
case LINUX_TIOCSPGRP:
return TIOCSPGRP;
case LINUX_TIOCOUTQ:
return TIOCOUTQ;
case LINUX_TIOCSTI:
return TIOCSTI;
case LINUX_TIOCGWINSZ:
return TIOCGWINSZ;
case LINUX_TIOCSWINSZ:
return TIOCSWINSZ;
case LINUX_TIOCMGET:
return TIOCMGET;
case LINUX_TIOCMBIS:
return TIOCMBIS;
case LINUX_TIOCMBIC:
return TIOCMBIC;
case LINUX_TIOCMSET:
return TIOCMSET;
case LINUX_FIONREAD:
return FIONREAD;
case LINUX_TIOCCONS:
return TIOCCONS;
case LINUX_TIOCPKT:
return TIOCPKT;
case LINUX_FIONBIO:
return FIONBIO;
case LINUX_TIOCNOTTY:
return TIOCNOTTY;
case LINUX_TIOCSETD:
return TIOCSETD;
case LINUX_TIOCGETD:
return TIOCGETD;
case LINUX_TIOCSBRK:
return TIOCSBRK;
case LINUX_TIOCCBRK:
return TIOCCBRK;
case LINUX_TIOCGSID:
return TIOCGSID;
case LINUX_FIONCLEX:
return FIONCLEX;
case LINUX_FIOCLEX:
return FIOCLEX;
case LINUX_FIOASYNC:
return FIOASYNC;
case LINUX_TIOCPKT_DATA:
return TIOCPKT_DATA;
case LINUX_TIOCPKT_FLUSHREAD:
return TIOCPKT_FLUSHREAD;
case LINUX_TIOCPKT_FLUSHWRITE:
return TIOCPKT_FLUSHWRITE;
case LINUX_TIOCPKT_STOP:
return TIOCPKT_STOP;
case LINUX_TIOCPKT_START:
return TIOCPKT_START;
case LINUX_TIOCPKT_NOSTOP:
return TIOCPKT_NOSTOP;
case LINUX_TIOCPKT_DOSTOP:
return TIOCPKT_DOSTOP;
case LINUX_TIOCPKT_IOCTL:
return TIOCPKT_IOCTL;
case LINUX_SIOCGIFCONF:
return SIOCGIFCONF;
case LINUX_SIOCGIFFLAGS:
return SIOCGIFFLAGS;
case LINUX_SIOCSIFFLAGS:
return SIOCSIFFLAGS;
case LINUX_SIOCGIFADDR:
return SIOCGIFADDR;
case LINUX_SIOCSIFADDR:
return SIOCSIFADDR;
case LINUX_SIOCGIFDSTADDR:
return SIOCGIFDSTADDR;
case LINUX_SIOCSIFDSTADDR:
return SIOCSIFDSTADDR;
case LINUX_SIOCGIFBRDADDR:
return SIOCGIFBRDADDR;
case LINUX_SIOCSIFBRDADDR:
return SIOCSIFBRDADDR;
case LINUX_SIOCGIFNETMASK:
return SIOCGIFNETMASK;
case LINUX_SIOCSIFNETMASK:
return SIOCSIFNETMASK;
case LINUX_SIOCGIFMETRIC:
return SIOCGIFMETRIC;
case LINUX_SIOCSIFMETRIC:
return SIOCSIFMETRIC;
case LINUX_SIOCGIFMTU:
return SIOCGIFMTU;
case LINUX_SIOCSIFMTU:
return SIOCSIFMTU;
case LINUX_SIOCSIFNAME:
return SIOCSIFNAME;
case LINUX_SIOCADDMULTI:
return SIOCADDMULTI;
case LINUX_SIOCDELMULTI:
return SIOCDELMULTI;
case LINUX_SIOCGIFINDEX:
return SIOCGIFINDEX;
case LINUX_SIOCDIFADDR:
return SIOCDIFADDR;
default:
return -1;
}
}
static int
so_opt_convert(int optname)
{
switch(optname) {
case LINUX_SO_DEBUG:
return SO_DEBUG;
case LINUX_SO_REUSEADDR:
return SO_REUSEADDR;
case LINUX_SO_ERROR:
return SO_ERROR;
case LINUX_SO_DONTROUTE:
return SO_DONTROUTE;
case LINUX_SO_BROADCAST:
return SO_BROADCAST;
case LINUX_SO_SNDBUF:
return SO_SNDBUF;
case LINUX_SO_RCVBUF:
return SO_RCVBUF;
case LINUX_SO_KEEPALIVE:
return SO_KEEPALIVE;
case LINUX_SO_OOBINLINE:
return SO_OOBINLINE;
case LINUX_SO_LINGER:
return SO_LINGER;
case LINUX_SO_REUSEPORT:
return SO_REUSEPORT;
case LINUX_SO_RCVLOWAT:
return SO_RCVLOWAT;
case LINUX_SO_SNDLOWAT:
return SO_SNDLOWAT;
case LINUX_SO_RCVTIMEO:
return SO_RCVTIMEO;
case LINUX_SO_SNDTIMEO:
return SO_SNDTIMEO;
case LINUX_SO_ACCEPTCONN:
return SO_ACCEPTCONN;
case LINUX_SO_PROTOCOL:
return SO_PROTOCOL;
default:
return -1;
}
}
static int
ip_opt_convert(int optname)
{
switch(optname) {
case LINUX_IP_TOS:
return IP_TOS;
case LINUX_IP_TTL:
return IP_TTL;
case LINUX_IP_HDRINCL:
return IP_HDRINCL;
case LINUX_IP_OPTIONS:
return IP_OPTIONS;
case LINUX_IP_MULTICAST_IF:
return IP_MULTICAST_IF;
case LINUX_IP_MULTICAST_TTL:
return IP_MULTICAST_TTL;
case LINUX_IP_MULTICAST_LOOP:
return IP_MULTICAST_LOOP;
case LINUX_IP_ADD_MEMBERSHIP:
return IP_ADD_MEMBERSHIP;
case LINUX_IP_DROP_MEMBERSHIP:
return IP_DROP_MEMBERSHIP;
default:
return optname;
}
}
static int
ip6_opt_convert(int optname)
{
switch(optname) {
case LINUX_IPV6_V6ONLY:
return IPV6_V6ONLY;
case LINUX_IPV6_RECVPKTINFO:
return IPV6_RECVPKTINFO;
default:
return optname;
}
}
static int
tcp_opt_convert(int optname)
{
switch(optname) {
case LINUX_TCP_NODELAY:
return TCP_NODELAY;
case LINUX_TCP_MAXSEG:
return TCP_MAXSEG;
case LINUX_TCP_KEEPIDLE:
return TCP_KEEPIDLE;
case LINUX_TCP_KEEPINTVL:
return TCP_KEEPINTVL;
case LINUX_TCP_KEEPCNT:
return TCP_KEEPCNT;
case LINUX_TCP_INFO:
return TCP_INFO;
case LINUX_TCP_MD5SIG:
return TCP_MD5SIG;
default:
return -1;
}
}
static int
linux2freebsd_opt(int level, int optname)
{
switch(level) {
case SOL_SOCKET:
return so_opt_convert(optname);
case IPPROTO_IP:
return ip_opt_convert(optname);
case IPPROTO_IPV6:
return ip6_opt_convert(optname);
case IPPROTO_TCP:
return tcp_opt_convert(optname);
default:
return -1;
}
}
static void
linux2freebsd_sockaddr(const struct linux_sockaddr *linux,
socklen_t addrlen, struct sockaddr *freebsd)
{
if (linux == NULL) {
return;
}
/* #linux and #freebsd may point to the same address */
freebsd->sa_family = linux->sa_family == LINUX_AF_INET6 ? AF_INET6 : linux->sa_family;
freebsd->sa_len = addrlen;
bcopy(linux->sa_data, freebsd->sa_data, addrlen - sizeof(linux->sa_family));
}
static void
freebsd2linux_sockaddr(struct linux_sockaddr *linux,
struct sockaddr *freebsd)
{
if (linux == NULL) {
return;
}
linux->sa_family = freebsd->sa_family == AF_INET6 ? LINUX_AF_INET6 : freebsd->sa_family;
bcopy(freebsd->sa_data, linux->sa_data, freebsd->sa_len - sizeof(linux->sa_family));
}
int
ff_socket(int domain, int type, int protocol)
{
int rc;
struct socket_args sa;
sa.domain = domain == LINUX_AF_INET6 ? AF_INET6 : domain;
sa.type = type;
sa.protocol = protocol;
if ((rc = sys_socket(curthread, &sa)))
goto kern_fail;
return curthread->td_retval[0];
kern_fail:
ff_os_errno(rc);
return (-1);
}
int
ff_getsockopt(int s, int level, int optname, void *optval,
socklen_t *optlen)
{
int rc;
if (level == LINUX_SOL_SOCKET)
level = SOL_SOCKET;
optname = linux2freebsd_opt(level, optname);
if (optname < 0) {
rc = EINVAL;
goto kern_fail;
}
if ((rc = kern_getsockopt(curthread, s, level, optname,
optval, UIO_USERSPACE, optlen)))
goto kern_fail;
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
int
ff_getsockopt_freebsd(int s, int level, int optname,
void *optval, socklen_t *optlen)
{
int rc;
if ((rc = kern_getsockopt(curthread, s, level, optname,
optval, UIO_USERSPACE, optlen)))
goto kern_fail;
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
int
ff_setsockopt(int s, int level, int optname, const void *optval,
socklen_t optlen)
{
int rc;
if (level == LINUX_SOL_SOCKET)
level = SOL_SOCKET;
optname = linux2freebsd_opt(level, optname);
if (optname < 0) {
rc = EINVAL;
goto kern_fail;
}
if ((rc = kern_setsockopt(curthread, s, level, optname,
__DECONST(void *, optval), UIO_USERSPACE, optlen)))
goto kern_fail;
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
int
ff_setsockopt_freebsd(int s, int level, int optname,
const void *optval, socklen_t optlen)
{
int rc;
if ((rc = kern_setsockopt(curthread, s, level, optname,
__DECONST(void *, optval), UIO_USERSPACE, optlen)))
goto kern_fail;
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
int
ff_ioctl(int fd, unsigned long request, ...)
{
int rc;
va_list ap;
caddr_t argp;
long req = linux2freebsd_ioctl(request);
if (req < 0) {
rc = EINVAL;
goto kern_fail;
}
va_start(ap, request);
argp = va_arg(ap, caddr_t);
va_end(ap);
if ((rc = kern_ioctl(curthread, fd, req, argp)))
goto kern_fail;
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
int
ff_ioctl_freebsd(int fd, unsigned long request, ...)
{
int rc;
va_list ap;
caddr_t argp;
va_start(ap, request);
argp = va_arg(ap, caddr_t);
va_end(ap);
if ((rc = kern_ioctl(curthread, fd, request, argp)))
goto kern_fail;
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
int
ff_close(int fd)
{
int rc;
if ((rc = kern_close(curthread, fd)))
goto kern_fail;
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
ssize_t
ff_read(int fd, void *buf, size_t nbytes)
{
struct uio auio;
struct iovec aiov;
int rc;
if (nbytes > INT_MAX) {
rc = EINVAL;
goto kern_fail;
}
aiov.iov_base = buf;
aiov.iov_len = nbytes;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_resid = nbytes;
auio.uio_segflg = UIO_SYSSPACE;
if ((rc = kern_readv(curthread, fd, &auio)))
goto kern_fail;
rc = curthread->td_retval[0];
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
ssize_t
ff_readv(int fd, const struct iovec *iov, int iovcnt)
{
struct uio auio;
int rc, len, i;
len = 0;
for (i = 0; i < iovcnt; i++)
len += iov[i].iov_len;
auio.uio_iov = __DECONST(struct iovec *, iov);
auio.uio_iovcnt = iovcnt;
auio.uio_resid = len;
auio.uio_segflg = UIO_SYSSPACE;
if ((rc = kern_readv(curthread, fd, &auio)))
goto kern_fail;
rc = curthread->td_retval[0];
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
ssize_t
ff_write(int fd, const void *buf, size_t nbytes)
{
struct uio auio;
struct iovec aiov;
int rc;
if (nbytes > INT_MAX) {
rc = EINVAL;
goto kern_fail;
}
aiov.iov_base = (void *)(uintptr_t)buf;
aiov.iov_len = nbytes;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_resid = nbytes;
auio.uio_segflg = UIO_SYSSPACE;
if ((rc = kern_writev(curthread, fd, &auio)))
goto kern_fail;
rc = curthread->td_retval[0];
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
ssize_t
ff_writev(int fd, const struct iovec *iov, int iovcnt)
{
struct uio auio;
int i, rc, len;
len = 0;
for (i = 0; i < iovcnt; i++)
len += iov[i].iov_len;
auio.uio_iov = __DECONST(struct iovec *, iov);
auio.uio_iovcnt = iovcnt;
auio.uio_resid = len;
auio.uio_segflg = UIO_SYSSPACE;
if ((rc = kern_writev(curthread, fd, &auio)))
goto kern_fail;
rc = curthread->td_retval[0];
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
ssize_t
ff_send(int s, const void *buf, size_t len, int flags)
{
return (ff_sendto(s, buf, len, flags, NULL, 0));
}
ssize_t
ff_sendto(int s, const void *buf, size_t len, int flags,
const struct linux_sockaddr *to, socklen_t tolen)
{
struct msghdr msg;
struct iovec aiov;
int rc;
struct sockaddr_storage bsdaddr;
struct sockaddr *pf = (struct sockaddr *)&bsdaddr;
if (to) {
linux2freebsd_sockaddr(to, tolen, pf);
} else {
pf = NULL;
}
msg.msg_name = pf;
msg.msg_namelen = tolen;
msg.msg_iov = &aiov;
msg.msg_iovlen = 1;
msg.msg_control = 0;
aiov.iov_base = __DECONST(void *, buf);
aiov.iov_len = len;
if ((rc = sendit(curthread, s, &msg, flags)))
goto kern_fail;
rc = curthread->td_retval[0];
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
ssize_t
ff_sendmsg(int s, const struct msghdr *msg, int flags)
{
int rc;
struct sockaddr_storage freebsd_sa;
void *linux_sa = msg->msg_name;
if (linux_sa != NULL) {
linux2freebsd_sockaddr(linux_sa,
sizeof(struct linux_sockaddr), (struct sockaddr *)&freebsd_sa);
__DECONST(struct msghdr *, msg)->msg_name = &freebsd_sa;
}
rc = sendit(curthread, s, __DECONST(struct msghdr *, msg), flags);
__DECONST(struct msghdr *, msg)->msg_name = linux_sa;
if (rc)
goto kern_fail;
rc = curthread->td_retval[0];
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
ssize_t
ff_recv(int s, void *buf, size_t len, int flags)
{
return (ff_recvfrom(s, buf, len, flags, NULL, 0));
}
ssize_t
ff_recvfrom(int s, void *buf, size_t len, int flags,
struct linux_sockaddr *from, socklen_t *fromlen)
{
struct msghdr msg;
struct iovec aiov;
int rc;
struct sockaddr_storage bsdaddr;
if (fromlen != NULL)
msg.msg_namelen = *fromlen;
else
msg.msg_namelen = 0;
msg.msg_name = &bsdaddr;
msg.msg_iov = &aiov;
msg.msg_iovlen = 1;
aiov.iov_base = buf;
aiov.iov_len = len;
msg.msg_control = 0;
msg.msg_flags = flags;
if ((rc = kern_recvit(curthread, s, &msg, UIO_SYSSPACE, NULL)))
goto kern_fail;
rc = curthread->td_retval[0];
if (fromlen != NULL)
*fromlen = msg.msg_namelen;
if (from)
freebsd2linux_sockaddr(from, (struct sockaddr *)&bsdaddr);
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
/*
* It is considered here that the upper 4 bytes of
* msg->iovlen and msg->msg_controllen in linux_msghdr are 0.
*/
ssize_t
ff_recvmsg(int s, struct msghdr *msg, int flags)
{
int rc;
struct linux_msghdr *linux_msg = (struct linux_msghdr *)msg;
msg->msg_flags = flags;
if ((rc = kern_recvit(curthread, s, msg, UIO_SYSSPACE, NULL))) {
msg->msg_flags = 0;
goto kern_fail;
}
rc = curthread->td_retval[0];
freebsd2linux_sockaddr(linux_msg->msg_name, msg->msg_name);
linux_msg->msg_flags = msg->msg_flags;
msg->msg_flags = 0;
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
int
ff_fcntl(int fd, int cmd, ...)
{
int rc;
va_list ap;
uintptr_t argp;
va_start(ap, cmd);
argp = va_arg(ap, uintptr_t);
va_end(ap);
if ((rc = kern_fcntl(curthread, fd, cmd, argp)))
goto kern_fail;
rc = curthread->td_retval[0];
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
int
ff_accept(int s, struct linux_sockaddr * addr,
socklen_t * addrlen)
{
int rc;
struct file *fp;
struct sockaddr *pf = NULL;
socklen_t socklen = sizeof(struct sockaddr_storage);
if ((rc = kern_accept(curthread, s, &pf, &socklen, &fp)))
goto kern_fail;
rc = curthread->td_retval[0];
fdrop(fp, curthread);
if (addr && pf)
freebsd2linux_sockaddr(addr, pf);
if (addrlen)
*addrlen = pf->sa_len;
if(pf != NULL)
free(pf, M_SONAME);
return (rc);
kern_fail:
if(pf != NULL)
free(pf, M_SONAME);
ff_os_errno(rc);
return (-1);
}
int
ff_listen(int s, int backlog)
{
int rc;
struct listen_args la = {
.s = s,
.backlog = backlog,
};
if ((rc = sys_listen(curthread, &la)))
goto kern_fail;
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
int
ff_bind(int s, const struct linux_sockaddr *addr, socklen_t addrlen)
{
int rc;
struct sockaddr_storage bsdaddr;
linux2freebsd_sockaddr(addr, addrlen, (struct sockaddr *)&bsdaddr);
if ((rc = kern_bindat(curthread, AT_FDCWD, s, (struct sockaddr *)&bsdaddr)))
goto kern_fail;
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
int
ff_connect(int s, const struct linux_sockaddr *name, socklen_t namelen)
{
int rc;
struct sockaddr_storage bsdaddr;
linux2freebsd_sockaddr(name, namelen, (struct sockaddr *)&bsdaddr);
if ((rc = kern_connectat(curthread, AT_FDCWD, s, (struct sockaddr *)&bsdaddr)))
goto kern_fail;
return (rc);
kern_fail:
ff_os_errno(rc);
return (-1);
}
int
ff_getpeername(int s, struct linux_sockaddr * name,
socklen_t *namelen)
{
int rc;
struct sockaddr *pf = NULL;
if ((rc = kern_getpeername(curthread, s, &pf, namelen)))
goto kern_fail;
if (name && pf)
freebsd2linux_sockaddr(name, pf);
if(pf != NULL)
free(pf, M_SONAME);
return (rc);
kern_fail:
if(pf != NULL)
free(pf, M_SONAME);
ff_os_errno(rc);