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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
|
/* $OpenBSD: linux_socket.c,v 1.17 1999/02/10 08:04:26 deraadt Exp $ */
/* $NetBSD: linux_socket.c,v 1.14 1996/04/05 00:01:50 christos Exp $ */
/*
* Copyright (c) 1995 Frank van der Linden
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project
* by Frank van der Linden
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/malloc.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/if_dl.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/device.h>
#include <sys/syscallargs.h>
#include <compat/linux/linux_types.h>
#include <compat/linux/linux_util.h>
#include <compat/linux/linux_signal.h>
#include <compat/linux/linux_syscallargs.h>
#include <compat/linux/linux_ioctl.h>
#include <compat/linux/linux_socket.h>
#include <compat/linux/linux_socketcall.h>
#include <compat/linux/linux_sockio.h>
/*
* All the calls in this file are entered via one common system
* call in Linux, represented here by linux_socketcall()
* Arguments for the various calls are on the user stack. A pointer
* to them is the only thing that is passed. It is up to the various
* calls to copy them in themselves. To make it look better, they
* are copied to structures.
*/
int linux_to_bsd_domain __P((int));
int linux_socket __P((struct proc *, struct linux_socket_args *, register_t *));
int linux_bind __P((struct proc *, struct linux_bind_args *, register_t *));
int linux_connect __P((struct proc *, struct linux_connect_args *,
register_t *));
int linux_listen __P((struct proc *, struct linux_listen_args *, register_t *));
int linux_accept __P((struct proc *, struct linux_accept_args *, register_t *));
int linux_getsockname __P((struct proc *, struct linux_getsockname_args *,
register_t *));
int linux_getpeername __P((struct proc *, struct linux_getpeername_args *,
register_t *));
int linux_socketpair __P((struct proc *, struct linux_socketpair_args *,
register_t *));
int linux_send __P((struct proc *, struct linux_send_args *, register_t *));
int linux_recv __P((struct proc *, struct linux_recv_args *, register_t *));
int linux_sendto __P((struct proc *, struct linux_sendto_args *, register_t *));
int linux_recvfrom __P((struct proc *, struct linux_recvfrom_args *,
register_t *));
int linux_shutdown __P((struct proc *, struct linux_shutdown_args *,
register_t *));
int linux_to_bsd_sopt_level __P((int));
int linux_to_bsd_so_sockopt __P((int));
int linux_to_bsd_ip_sockopt __P((int));
int linux_to_bsd_tcp_sockopt __P((int));
int linux_to_bsd_udp_sockopt __P((int));
int linux_setsockopt __P((struct proc *, struct linux_setsockopt_args *,
register_t *));
int linux_getsockopt __P((struct proc *, struct linux_getsockopt_args *,
register_t *));
int linux_recvmsg __P((struct proc *, struct linux_recvmsg_args *,
register_t *));
int linux_sendmsg __P((struct proc *, struct linux_sendmsg_args *,
register_t *));
int linux_check_hdrincl __P((struct proc *, int, register_t *));
int linux_sendto_hdrincl __P((struct proc *, struct sys_sendto_args *,
register_t *));
/*
* Convert between Linux and BSD socket domain values
*/
int
linux_to_bsd_domain(ldom)
int ldom;
{
switch (ldom) {
case LINUX_AF_UNSPEC:
return AF_UNSPEC;
case LINUX_AF_UNIX:
return AF_LOCAL;
case LINUX_AF_INET:
return AF_INET;
case LINUX_AF_AX25:
return AF_CCITT;
case LINUX_AF_IPX:
return AF_IPX;
case LINUX_AF_APPLETALK:
return AF_APPLETALK;
default:
return -1;
}
}
int
linux_socket(p, uap, retval)
struct proc *p;
struct linux_socket_args /* {
syscallarg(int) domain;
syscallarg(int) type;
syscallarg(int) protocol;
} */ *uap;
register_t *retval;
{
struct linux_socket_args lsa;
struct sys_socket_args bsa;
int error;
if ((error = copyin((caddr_t) uap, (caddr_t) &lsa, sizeof lsa)))
return error;
SCARG(&bsa, protocol) = lsa.protocol;
SCARG(&bsa, type) = lsa.type;
SCARG(&bsa, domain) = linux_to_bsd_domain(lsa.domain);
if (SCARG(&bsa, domain) == -1)
return EINVAL;
return sys_socket(p, &bsa, retval);
}
int
linux_bind(p, uap, retval)
struct proc *p;
struct linux_bind_args /* {
syscallarg(int) s;
syscallarg(struct sockaddr *) name;
syscallarg(int) namelen;
} */ *uap;
register_t *retval;
{
struct linux_bind_args lba;
struct sys_bind_args bba;
int error;
if ((error = copyin((caddr_t) uap, (caddr_t) &lba, sizeof lba)))
return error;
SCARG(&bba, s) = lba.s;
SCARG(&bba, name) = (void *) lba.name;
SCARG(&bba, namelen) = lba.namelen;
return sys_bind(p, &bba, retval);
}
int
linux_connect(p, uap, retval)
struct proc *p;
struct linux_connect_args /* {
syscallarg(int) s;
syscallarg(struct sockaddr *) name;
syscallarg(int) namelen;
} */ *uap;
register_t *retval;
{
struct linux_connect_args lca;
struct sys_connect_args bca;
int error;
if ((error = copyin((caddr_t) uap, (caddr_t) &lca, sizeof lca)))
return error;
SCARG(&bca, s) = lca.s;
SCARG(&bca, name) = (void *) lca.name;
SCARG(&bca, namelen) = lca.namelen;
error = sys_connect(p, &bca, retval);
if (error == EISCONN) {
struct sys_getsockopt_args bga;
#if 0
struct sys_fcntl_args fca;
#endif
void *status, *statusl;
int stat, statl = sizeof stat;
caddr_t sg;
#if 0
SCARG(&fca, fd) = lca.s;
SCARG(&fca, cmd) = F_GETFL;
SCARG(&fca, arg) = 0;
if (sys_fcntl(p, &fca, retval) == -1 ||
(*retval & O_NONBLOCK) == 0)
return error;
#endif
sg = stackgap_init(p->p_emul);
status = stackgap_alloc(&sg, sizeof stat);
statusl = stackgap_alloc(&sg, sizeof statusl);
if ((error = copyout(&statl, statusl, sizeof statl)))
return error;
SCARG(&bga, s) = lca.s;
SCARG(&bga, level) = SOL_SOCKET;
SCARG(&bga, name) = SO_ERROR;
SCARG(&bga, val) = status;
SCARG(&bga, avalsize) = statusl;
error = sys_getsockopt(p, &bga, retval);
if (error)
return error;
if ((error = copyin(status, &stat, sizeof stat)))
return error;
return stat;
}
return error;
}
int
linux_listen(p, uap, retval)
struct proc *p;
struct linux_listen_args /* {
syscallarg(int) s;
syscallarg(int) backlog;
} */ *uap;
register_t *retval;
{
struct linux_listen_args lla;
struct sys_listen_args bla;
int error;
if ((error = copyin((caddr_t) uap, (caddr_t) &lla, sizeof lla)))
return error;
SCARG(&bla, s) = lla.s;
SCARG(&bla, backlog) = lla.backlog;
return sys_listen(p, &bla, retval);
}
int
linux_accept(p, uap, retval)
struct proc *p;
struct linux_accept_args /* {
syscallarg(int) s;
syscallarg(struct sockaddr *) addr;
syscallarg(int *) namelen;
} */ *uap;
register_t *retval;
{
struct linux_accept_args laa;
struct compat_43_sys_accept_args baa;
int error;
if ((error = copyin((caddr_t) uap, (caddr_t) &laa, sizeof laa)))
return error;
SCARG(&baa, s) = laa.s;
SCARG(&baa, name) = (caddr_t) laa.addr;
SCARG(&baa, anamelen) = laa.namelen;
return compat_43_sys_accept(p, &baa, retval);
}
int
linux_getsockname(p, uap, retval)
struct proc *p;
struct linux_getsockname_args /* {
syscallarg(int) s;
syscallarg(struct sockaddr *) addr;
syscallarg(int *) namelen;
} */ *uap;
register_t *retval;
{
struct linux_getsockname_args lga;
struct compat_43_sys_getsockname_args bga;
int error;
if ((error = copyin((caddr_t) uap, (caddr_t) &lga, sizeof lga)))
return error;
SCARG(&bga, fdec) = lga.s;
SCARG(&bga, asa) = (caddr_t) lga.addr;
SCARG(&bga, alen) = lga.namelen;
return compat_43_sys_getsockname(p, &bga, retval);
}
int
linux_getpeername(p, uap, retval)
struct proc *p;
struct linux_getpeername_args /* {
syscallarg(int) s;
syscallarg(struct sockaddr *) addr;
syscallarg(int *) namelen;
} */ *uap;
register_t *retval;
{
struct linux_getpeername_args lga;
struct compat_43_sys_getpeername_args bga;
int error;
if ((error = copyin((caddr_t) uap, (caddr_t) &lga, sizeof lga)))
return error;
SCARG(&bga, fdes) = lga.s;
SCARG(&bga, asa) = (caddr_t) lga.addr;
SCARG(&bga, alen) = lga.namelen;
return compat_43_sys_getpeername(p, &bga, retval);
}
int
linux_socketpair(p, uap, retval)
struct proc *p;
struct linux_socketpair_args /* {
syscallarg(int) domain;
syscallarg(int) type;
syscallarg(int) protocol;
syscallarg(int *) rsv;
} */ *uap;
register_t *retval;
{
struct linux_socketpair_args lsa;
struct sys_socketpair_args bsa;
int error;
if ((error = copyin((caddr_t) uap, &lsa, sizeof lsa)))
return error;
SCARG(&bsa, domain) = linux_to_bsd_domain(lsa.domain);
if (SCARG(&bsa, domain) == -1)
return EINVAL;
SCARG(&bsa, type) = lsa.type;
SCARG(&bsa, protocol) = lsa.protocol;
SCARG(&bsa, rsv) = lsa.rsv;
return sys_socketpair(p, &bsa, retval);
}
int
linux_send(p, uap, retval)
struct proc *p;
struct linux_send_args /* {
syscallarg(int) s;
syscallarg(void *) msg;
syscallarg(int) len;
syscallarg(int) flags;
} */ *uap;
register_t *retval;
{
struct linux_send_args lsa;
struct compat_43_sys_send_args bsa;
int error;
if ((error = copyin((caddr_t) uap, (caddr_t) &lsa, sizeof lsa)))
return error;
SCARG(&bsa, s) = lsa.s;
SCARG(&bsa, buf) = lsa.msg;
SCARG(&bsa, len) = lsa.len;
SCARG(&bsa, flags) = lsa.flags;
return compat_43_sys_send(p, &bsa, retval);
}
int
linux_recv(p, uap, retval)
struct proc *p;
struct linux_recv_args /* {
syscallarg(int) s;
syscallarg(void *) msg;
syscallarg(int) len;
syscallarg(int) flags;
} */ *uap;
register_t *retval;
{
struct linux_recv_args lra;
struct compat_43_sys_recv_args bra;
int error;
if ((error = copyin((caddr_t) uap, (caddr_t) &lra, sizeof lra)))
return error;
SCARG(&bra, s) = lra.s;
SCARG(&bra, buf) = lra.msg;
SCARG(&bra, len) = lra.len;
SCARG(&bra, flags) = lra.flags;
return compat_43_sys_recv(p, &bra, retval);
}
int
linux_check_hdrincl(p, fd, retval)
struct proc *p;
int fd;
register_t *retval;
{
struct sys_getsockopt_args /* {
int s;
int level;
int name;
caddr_t val;
int *avalsize;
} */ gsa;
int error;
caddr_t sg, val;
int *valsize;
int size_val = sizeof val;
int optval;
sg = stackgap_init(p->p_emul);
val = stackgap_alloc(&sg, sizeof(optval));
valsize = stackgap_alloc(&sg, sizeof(size_val));
if ((error = copyout(&size_val, valsize, sizeof(size_val))))
return (error);
SCARG(&gsa, s) = fd;
SCARG(&gsa, level) = IPPROTO_IP;
SCARG(&gsa, name) = IP_HDRINCL;
SCARG(&gsa, val) = val;
SCARG(&gsa, avalsize) = valsize;
if ((error = sys_getsockopt(p, &gsa, retval)))
return (error);
if ((error = copyin(val, &optval, sizeof(optval))))
return (error);
return (optval == 0);
}
/*
* linux_ip_copysize defines how many bytes we should copy
* from the beginning of the IP packet before we customize it for BSD.
* It should include all the fields we modify (ip_len and ip_off)
* and be as small as possible to minimize copying overhead.
*/
#define linux_ip_copysize 8
int
linux_sendto_hdrincl(p, bsa, retval)
struct proc *p;
struct sys_sendto_args *bsa;
register_t *retval;
{
caddr_t sg;
struct sys_sendmsg_args ssa;
struct ip *packet, rpacket;
struct msghdr *msg, rmsg;
struct iovec *iov, riov[2];
int error;
/* Check the packet isn't too small before we mess with it */
if (SCARG(bsa, len) < linux_ip_copysize)
return EINVAL;
/*
* Tweaking the user buffer in place would be bad manners.
* We create a corrected IP header with just the needed length,
* then use an iovec to glue it to the rest of the user packet
* when calling sendmsg().
*/
sg = stackgap_init(p->p_emul);
packet = (struct ip *)stackgap_alloc(&sg, linux_ip_copysize);
msg = (struct msghdr *)stackgap_alloc(&sg, sizeof(*msg));
iov = (struct iovec *)stackgap_alloc(&sg, sizeof(*iov)*2);
/* Make a copy of the beginning of the packet to be sent */
if ((error = copyin(SCARG(bsa, buf), (caddr_t)&rpacket,
linux_ip_copysize)))
return error;
/* Convert fields from Linux to BSD raw IP socket format */
rpacket.ip_len = SCARG(bsa, len);
error = copyout(&rpacket, packet, sizeof(packet));
if (error)
return (error);
riov[0].iov_base = (char *)packet;
riov[0].iov_len = linux_ip_copysize;
riov[1].iov_base = (caddr_t)SCARG(bsa, buf) + linux_ip_copysize;
riov[1].iov_len = SCARG(bsa, len) - linux_ip_copysize;
error = copyout(&riov[0], iov, sizeof(riov));
if (error)
return (error);
/* Prepare the msghdr and iovec structures describing the new packet */
rmsg.msg_name = (void *)SCARG(bsa, to);
rmsg.msg_namelen = SCARG(bsa, tolen);
rmsg.msg_iov = iov;
rmsg.msg_iovlen = 2;
rmsg.msg_control = NULL;
rmsg.msg_controllen = 0;
rmsg.msg_flags = 0;
error = copyout(&riov[0], iov, sizeof(riov));
if (error)
return (error);
SCARG(&ssa, s) = SCARG(bsa, s);
SCARG(&ssa, msg) = msg;
SCARG(&ssa, flags) = SCARG(bsa, flags);
return sys_sendmsg(p, &ssa, retval);
}
int
linux_sendto(p, uap, retval)
struct proc *p;
struct linux_sendto_args /* {
syscallarg(int) s;
syscallarg(void *) msg;
syscallarg(int) len;
syscallarg(int) flags;
syscallarg(sockaddr *) to;
syscallarg(int) tolen;
} */ *uap;
register_t *retval;
{
struct linux_sendto_args lsa;
struct sys_sendto_args bsa;
int error;
if ((error = copyin((caddr_t) uap, (caddr_t) &lsa, sizeof lsa)))
return error;
SCARG(&bsa, s) = lsa.s;
SCARG(&bsa, buf) = lsa.msg;
SCARG(&bsa, len) = lsa.len;
SCARG(&bsa, flags) = lsa.flags;
SCARG(&bsa, to) = (void *) lsa.to;
SCARG(&bsa, tolen) = lsa.tolen;
if (linux_check_hdrincl(p, lsa.s, retval) == 0)
return linux_sendto_hdrincl(p, &bsa, retval);
return sys_sendto(p, &bsa, retval);
}
int
linux_recvfrom(p, uap, retval)
struct proc *p;
struct linux_recvfrom_args /* {
syscallarg(int) s;
syscallarg(void *) buf;
syscallarg(int) len;
syscallarg(int) flags;
syscallarg(struct sockaddr *) from;
syscallarg(int *) fromlen;
} */ *uap;
register_t *retval;
{
struct linux_recvfrom_args lra;
struct compat_43_sys_recvfrom_args bra;
int error;
if ((error = copyin((caddr_t) uap, (caddr_t) &lra, sizeof lra)))
return error;
SCARG(&bra, s) = lra.s;
SCARG(&bra, buf) = lra.buf;
SCARG(&bra, len) = lra.len;
SCARG(&bra, flags) = lra.flags;
SCARG(&bra, from) = (caddr_t) lra.from;
SCARG(&bra, fromlenaddr) = lra.fromlen;
return compat_43_sys_recvfrom(p, &bra, retval);
}
int
linux_shutdown(p, uap, retval)
struct proc *p;
struct linux_shutdown_args /* {
syscallarg(int) s;
syscallarg(int) how;
} */ *uap;
register_t *retval;
{
struct linux_shutdown_args lsa;
struct sys_shutdown_args bsa;
int error;
if ((error = copyin((caddr_t) uap, (caddr_t) &lsa, sizeof lsa)))
return error;
SCARG(&bsa, s) = lsa.s;
SCARG(&bsa, how) = lsa.how;
return sys_shutdown(p, &bsa, retval);
}
/*
* Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET
* is different, the rest matches IPPROTO_* on both systems.
*/
int
linux_to_bsd_sopt_level(llevel)
int llevel;
{
switch (llevel) {
case LINUX_SOL_SOCKET:
return SOL_SOCKET;
case LINUX_SOL_IP:
return IPPROTO_IP;
case LINUX_SOL_TCP:
return IPPROTO_TCP;
case LINUX_SOL_UDP:
return IPPROTO_UDP;
default:
return -1;
}
}
/*
* Convert Linux socket level socket option numbers to NetBSD values.
*/
int
linux_to_bsd_so_sockopt(lopt)
int lopt;
{
switch (lopt) {
case LINUX_SO_DEBUG:
return SO_DEBUG;
case LINUX_SO_REUSEADDR:
return SO_REUSEADDR;
case LINUX_SO_TYPE:
return SO_TYPE;
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_PRIORITY:
case LINUX_SO_NO_CHECK:
default:
return -1;
}
}
/*
* Convert Linux IP level socket option number to NetBSD values.
*/
int
linux_to_bsd_ip_sockopt(lopt)
int lopt;
{
switch (lopt) {
case LINUX_IP_TOS:
return IP_TOS;
case LINUX_IP_TTL:
return IP_TTL;
case LINUX_IP_MULTICAST_TTL:
return IP_MULTICAST_TTL;
case LINUX_IP_MULTICAST_LOOP:
return IP_MULTICAST_LOOP;
case LINUX_IP_MULTICAST_IF:
return IP_MULTICAST_IF;
case LINUX_IP_ADD_MEMBERSHIP:
return IP_ADD_MEMBERSHIP;
case LINUX_IP_DROP_MEMBERSHIP:
return IP_DROP_MEMBERSHIP;
case LINUX_IP_HDRINCL:
return IP_HDRINCL;
default:
return -1;
}
}
/*
* Convert Linux TCP level socket option number to NetBSD values.
*/
int
linux_to_bsd_tcp_sockopt(lopt)
int lopt;
{
switch (lopt) {
case LINUX_TCP_NODELAY:
return TCP_NODELAY;
case LINUX_TCP_MAXSEG:
return TCP_MAXSEG;
default:
return -1;
}
}
/*
* Convert Linux UDP level socket option number to NetBSD values.
*/
int
linux_to_bsd_udp_sockopt(lopt)
int lopt;
{
switch (lopt) {
default:
return -1;
}
}
/*
* Another reasonably straightforward function: setsockopt(2).
* The level and option numbers are converted; the values passed
* are not (yet) converted, the ones currently implemented don't
* need conversion, as they are the same on both systems.
*/
int
linux_setsockopt(p, uap, retval)
struct proc *p;
struct linux_setsockopt_args /* {
syscallarg(int) s;
syscallarg(int) level;
syscallarg(int) optname;
syscallarg(void *) optval;
syscallarg(int) optlen;
} */ *uap;
register_t *retval;
{
struct linux_setsockopt_args lsa;
struct sys_setsockopt_args bsa;
int error, name;
if ((error = copyin((caddr_t) uap, (caddr_t) &lsa, sizeof lsa)))
return error;
SCARG(&bsa, s) = lsa.s;
SCARG(&bsa, level) = linux_to_bsd_sopt_level(lsa.level);
SCARG(&bsa, val) = lsa.optval;
SCARG(&bsa, valsize) = lsa.optlen;
switch (SCARG(&bsa, level)) {
case SOL_SOCKET:
name = linux_to_bsd_so_sockopt(lsa.optname);
break;
case IPPROTO_IP:
name = linux_to_bsd_ip_sockopt(lsa.optname);
break;
case IPPROTO_TCP:
name = linux_to_bsd_tcp_sockopt(lsa.optname);
break;
case IPPROTO_UDP:
name = linux_to_bsd_udp_sockopt(lsa.optname);
break;
default:
return EINVAL;
}
if (name == -1)
return EINVAL;
SCARG(&bsa, name) = name;
return sys_setsockopt(p, &bsa, retval);
}
/*
* getsockopt(2) is very much the same as setsockopt(2) (see above)
*/
int
linux_getsockopt(p, uap, retval)
struct proc *p;
struct linux_getsockopt_args /* {
syscallarg(int) s;
syscallarg(int) level;
syscallarg(int) optname;
syscallarg(void *) optval;
syscallarg(int) *optlen;
} */ *uap;
register_t *retval;
{
struct linux_getsockopt_args lga;
struct sys_getsockopt_args bga;
int error, name;
if ((error = copyin((caddr_t) uap, (caddr_t) &lga, sizeof lga)))
return error;
SCARG(&bga, s) = lga.s;
SCARG(&bga, level) = linux_to_bsd_sopt_level(lga.level);
SCARG(&bga, val) = lga.optval;
SCARG(&bga, avalsize) = lga.optlen;
switch (SCARG(&bga, level)) {
case SOL_SOCKET:
name = linux_to_bsd_so_sockopt(lga.optname);
break;
case IPPROTO_IP:
name = linux_to_bsd_ip_sockopt(lga.optname);
break;
case IPPROTO_TCP:
name = linux_to_bsd_tcp_sockopt(lga.optname);
break;
case IPPROTO_UDP:
name = linux_to_bsd_udp_sockopt(lga.optname);
break;
default:
return EINVAL;
}
if (name == -1)
return EINVAL;
SCARG(&bga, name) = name;
return sys_getsockopt(p, &bga, retval);
}
int
linux_recvmsg(p, uap, retval)
struct proc *p;
struct linux_recvmsg_args /* {
syscallarg(int) s;
syscallarg(caddr_t) msg;
syscallarg(int) flags;
} */ *uap;
register_t *retval;
{
struct linux_recvmsg_args lla;
struct sys_recvmsg_args bla;
int error;
if ((error = copyin((caddr_t) uap, (caddr_t) &lla, sizeof lla)))
return error;
SCARG(&bla, s) = lla.s;
SCARG(&bla, msg) = (struct msghdr *)lla.msg;
SCARG(&bla, flags) = lla.flags;
return sys_recvmsg(p, &bla, retval);
}
int
linux_sendmsg(p, uap, retval)
struct proc *p;
struct linux_sendmsg_args /* {
syscallarg(int) s;
syscallarg(struct msghdr *) msg;
syscallarg(int) flags;
} */ *uap;
register_t *retval;
{
struct linux_sendmsg_args lla;
struct sys_sendmsg_args bla;
int error;
caddr_t control;
int level;
if ((error = copyin((caddr_t) uap, (caddr_t) &lla, sizeof lla)))
return error;
SCARG(&bla, s) = lla.s;
SCARG(&bla, msg) = lla.msg;
SCARG(&bla, flags) = lla.flags;
error = copyin(lla.msg->msg_control, &control, sizeof(caddr_t));
if (error)
return error;
if (control == NULL)
goto done;
error = copyin(&((struct cmsghdr *)control)->cmsg_level,
&level, sizeof(int));
if (error)
return error;
if (level == 1) {
/*
* Linux thinks that SOL_SOCKET is 1; we know that it's really
* 0xffff, of course.
*/
level = SOL_SOCKET;
/* XXX should use stack gap! */
error = copyout(&level, &((struct cmsghdr *)control)->
cmsg_level, sizeof(int));
if (error)
return error;
}
done:
return sys_sendmsg(p, &bla, retval);
}
/*
* Entry point to all Linux socket calls. Just check which call to
* make and take appropriate action.
*/
int
linux_sys_socketcall(p, v, retval)
struct proc *p;
void *v;
register_t *retval;
{
struct linux_sys_socketcall_args /* {
syscallarg(int) what;
syscallarg(void *) args;
} */ *uap = v;
switch (SCARG(uap, what)) {
case LINUX_SYS_socket:
return linux_socket(p, SCARG(uap, args), retval);
case LINUX_SYS_bind:
return linux_bind(p, SCARG(uap, args), retval);
case LINUX_SYS_connect:
return linux_connect(p, SCARG(uap, args), retval);
case LINUX_SYS_listen:
return linux_listen(p, SCARG(uap, args), retval);
case LINUX_SYS_accept:
return linux_accept(p, SCARG(uap, args), retval);
case LINUX_SYS_getsockname:
return linux_getsockname(p, SCARG(uap, args), retval);
case LINUX_SYS_getpeername:
return linux_getpeername(p, SCARG(uap, args), retval);
case LINUX_SYS_socketpair:
return linux_socketpair(p, SCARG(uap, args), retval);
case LINUX_SYS_send:
return linux_send(p, SCARG(uap, args), retval);
case LINUX_SYS_recv:
return linux_recv(p, SCARG(uap, args), retval);
case LINUX_SYS_sendto:
return linux_sendto(p, SCARG(uap, args), retval);
case LINUX_SYS_recvfrom:
return linux_recvfrom(p, SCARG(uap, args), retval);
case LINUX_SYS_shutdown:
return linux_shutdown(p, SCARG(uap, args), retval);
case LINUX_SYS_setsockopt:
return linux_setsockopt(p, SCARG(uap, args), retval);
case LINUX_SYS_getsockopt:
return linux_getsockopt(p, SCARG(uap, args), retval);
case LINUX_SYS_sendmsg:
return linux_sendmsg(p, SCARG(uap, args), retval);
case LINUX_SYS_recvmsg:
return linux_recvmsg(p, SCARG(uap, args), retval);
default:
return ENOSYS;
}
}
int
linux_ioctl_socket(p, uap, retval)
register struct proc *p;
register struct linux_sys_ioctl_args /* {
syscallarg(int) fd;
syscallarg(u_long) com;
syscallarg(caddr_t) data;
} */ *uap;
register_t *retval;
{
u_long com;
struct sys_ioctl_args ia;
com = SCARG(uap, com);
retval[0] = 0;
switch (com) {
case LINUX_FIOSETOWN:
SCARG(&ia, com) = FIOSETOWN;
break;
case LINUX_SIOCSPGRP:
SCARG(&ia, com) = SIOCSPGRP;
break;
case LINUX_FIOGETOWN:
SCARG(&ia, com) = FIOGETOWN;
break;
case LINUX_SIOCGPGRP:
SCARG(&ia, com) = SIOCGPGRP;
break;
case LINUX_SIOCATMARK:
SCARG(&ia, com) = SIOCATMARK;
break;
#if 0
case LINUX_SIOCGSTAMP:
SCARG(&ia, com) = SIOCGSTAMP;
break;
#endif
case LINUX_SIOCGIFCONF:
SCARG(&ia, com) = OSIOCGIFCONF;
break;
case LINUX_SIOCGIFFLAGS:
SCARG(&ia, com) = SIOCGIFFLAGS;
break;
case LINUX_SIOCGIFADDR:
SCARG(&ia, com) = OSIOCGIFADDR;
break;
case LINUX_SIOCGIFDSTADDR:
SCARG(&ia, com) = OSIOCGIFDSTADDR;
break;
case LINUX_SIOCGIFBRDADDR:
SCARG(&ia, com) = OSIOCGIFBRDADDR;
break;
case LINUX_SIOCGIFNETMASK:
SCARG(&ia, com) = OSIOCGIFNETMASK;
break;
case LINUX_SIOCGIFMETRIC:
SCARG(&ia, com) = SIOCGIFMETRIC;
break;
case LINUX_SIOCGIFMTU:
SCARG(&ia, com) = SIOCGIFMTU;
break;
case LINUX_SIOCADDMULTI:
SCARG(&ia, com) = SIOCADDMULTI;
break;
case LINUX_SIOCDELMULTI:
SCARG(&ia, com) = SIOCDELMULTI;
break;
case LINUX_SIOCGIFHWADDR: {
struct linux_ifreq *ifr = (struct linux_ifreq *)SCARG(&ia, data);
struct sockaddr_dl *sdl;
struct ifnet *ifp;
struct ifaddr *ifa;
/*
* Note that we don't actually respect the name in the ifreq
* structure, as Linux interface names are all different.
*/
for (ifp = ifnet.tqh_first; ifp != 0;
ifp = ifp->if_list.tqe_next) {
if (ifp->if_type != IFT_ETHER)
continue;
for (ifa = ifp->if_addrlist.tqh_first; ifa;
ifa = ifa->ifa_list.tqe_next) {
if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
(sdl->sdl_family == AF_LINK) &&
(sdl->sdl_type == IFT_ETHER)) {
return copyout(LLADDR(sdl),
(caddr_t)&ifr->ifr_hwaddr.sa_data,
LINUX_IFHWADDRLEN);
}
}
}
return ENOENT;
}
default:
return EINVAL;
}
SCARG(&ia, fd) = SCARG(uap, fd);
SCARG(&ia, data) = SCARG(uap, data);
return sys_ioctl(p, &ia, retval);
}
|