linux_socketcall.c revision 1.48 1 1.48 christos /* $NetBSD: linux_socketcall.c,v 1.48 2017/02/09 22:01:48 christos Exp $ */
2 1.16 erh
3 1.16 erh /*-
4 1.18 fvdl * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5 1.16 erh * All rights reserved.
6 1.16 erh *
7 1.16 erh * This code is derived from software contributed to The NetBSD Foundation
8 1.18 fvdl * by Frank van der Linden and Eric Haszlakiewicz.
9 1.16 erh *
10 1.16 erh * Redistribution and use in source and binary forms, with or without
11 1.16 erh * modification, are permitted provided that the following conditions
12 1.16 erh * are met:
13 1.16 erh * 1. Redistributions of source code must retain the above copyright
14 1.16 erh * notice, this list of conditions and the following disclaimer.
15 1.16 erh * 2. Redistributions in binary form must reproduce the above copyright
16 1.16 erh * notice, this list of conditions and the following disclaimer in the
17 1.16 erh * documentation and/or other materials provided with the distribution.
18 1.16 erh *
19 1.16 erh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.16 erh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.16 erh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.16 erh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.16 erh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.16 erh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.16 erh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.16 erh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.16 erh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.16 erh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.16 erh * POSSIBILITY OF SUCH DAMAGE.
30 1.1 fvdl */
31 1.23 lukem
32 1.23 lukem #include <sys/cdefs.h>
33 1.48 christos __KERNEL_RCSID(0, "$NetBSD: linux_socketcall.c,v 1.48 2017/02/09 22:01:48 christos Exp $");
34 1.34 yamt
35 1.1 fvdl #include <sys/param.h>
36 1.1 fvdl #include <sys/kernel.h>
37 1.1 fvdl #include <sys/systm.h>
38 1.1 fvdl #include <sys/buf.h>
39 1.1 fvdl #include <sys/ioctl.h>
40 1.1 fvdl #include <sys/tty.h>
41 1.1 fvdl #include <sys/file.h>
42 1.1 fvdl #include <sys/filedesc.h>
43 1.1 fvdl #include <sys/select.h>
44 1.1 fvdl #include <sys/socket.h>
45 1.1 fvdl #include <sys/socketvar.h>
46 1.1 fvdl #include <net/if.h>
47 1.1 fvdl #include <netinet/in.h>
48 1.7 mycroft #include <netinet/tcp.h>
49 1.1 fvdl #include <sys/mount.h>
50 1.1 fvdl #include <sys/proc.h>
51 1.1 fvdl #include <sys/vnode.h>
52 1.1 fvdl #include <sys/device.h>
53 1.33 dsl #include <sys/ktrace.h>
54 1.1 fvdl
55 1.1 fvdl #include <sys/syscallargs.h>
56 1.1 fvdl
57 1.39 njoly #include <compat/sys/socket.h>
58 1.39 njoly
59 1.17 christos #include <compat/linux/common/linux_types.h>
60 1.17 christos #include <compat/linux/common/linux_util.h>
61 1.17 christos #include <compat/linux/common/linux_signal.h>
62 1.17 christos #include <compat/linux/common/linux_ioctl.h>
63 1.17 christos #include <compat/linux/common/linux_socket.h>
64 1.17 christos #include <compat/linux/common/linux_socketcall.h>
65 1.17 christos #include <compat/linux/common/linux_sockio.h>
66 1.17 christos
67 1.1 fvdl #include <compat/linux/linux_syscallargs.h>
68 1.1 fvdl
69 1.24 christos #undef DPRINTF
70 1.24 christos #ifdef DEBUG_LINUX
71 1.24 christos #define DPRINTF(a) uprintf a
72 1.24 christos #else
73 1.24 christos #define DPRINTF(a)
74 1.24 christos #endif
75 1.24 christos
76 1.16 erh /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
77 1.28 manu /* Not used on: alpha, amd64 */
78 1.1 fvdl
79 1.1 fvdl /*
80 1.16 erh * This file contains the linux_socketcall() multiplexer. Arguments
81 1.16 erh * for the various socket calls are on the user stack. A pointer
82 1.16 erh * to them is the only thing that is passed. We copyin the arguments
83 1.16 erh * here so the individual functions can assume they are normal syscalls.
84 1.16 erh */
85 1.16 erh
86 1.16 erh /* The sizes of the arguments. Used for copyin. */
87 1.24 christos static const struct {
88 1.29 christos const char *name;
89 1.24 christos int argsize;
90 1.24 christos } linux_socketcall[LINUX_MAX_SOCKETCALL+1] = {
91 1.41 christos #define L(a) "linux/" a
92 1.40 christos {L("invalid"), -1}, /* 0 */
93 1.40 christos {L("socket"), sizeof(struct linux_sys_socket_args)}, /* 1 */
94 1.40 christos {L("bind"), sizeof(struct linux_sys_bind_args)}, /* 2 */
95 1.40 christos {L("connect"), sizeof(struct linux_sys_connect_args)}, /* 3 */
96 1.48 christos {L("listen"), sizeof(struct sys_listen_args)}, /* 4 */
97 1.40 christos {L("accept"), sizeof(struct linux_sys_accept_args)}, /* 5 */
98 1.42 martin {L("getsockname"),sizeof(struct linux_sys_getsockname_args)}, /* 6 */
99 1.42 martin {L("getpeername"),sizeof(struct linux_sys_getpeername_args)}, /* 7 */
100 1.40 christos {L("socketpair"),sizeof(struct linux_sys_socketpair_args)}, /* 8 */
101 1.40 christos {L("send"), sizeof(struct linux_sys_send_args)}, /* 9 */
102 1.40 christos {L("recv"), sizeof(struct linux_sys_recv_args)}, /* 10 */
103 1.40 christos {L("sendto"), sizeof(struct linux_sys_sendto_args)}, /* 11 */
104 1.40 christos {L("recvfrom"), sizeof(struct linux_sys_recvfrom_args)}, /* 12 */
105 1.48 christos {L("shutdown"), sizeof(struct sys_shutdown_args)}, /* 13 */
106 1.40 christos {L("setsockopt"),sizeof(struct linux_sys_setsockopt_args)}, /* 14 */
107 1.40 christos {L("getsockopt"),sizeof(struct linux_sys_getsockopt_args)}, /* 15 */
108 1.40 christos {L("sendmsg"), sizeof(struct linux_sys_sendmsg_args)}, /* 16 */
109 1.40 christos {L("recvmsg"), sizeof(struct linux_sys_recvmsg_args)}, /* 17 */
110 1.46 christos {L("accept4"), sizeof(struct linux_sys_accept4_args)}, /* 18 */
111 1.40 christos #undef L
112 1.16 erh };
113 1.1 fvdl
114 1.1 fvdl /*
115 1.1 fvdl * Entry point to all Linux socket calls. Just check which call to
116 1.1 fvdl * make and take appropriate action.
117 1.1 fvdl */
118 1.1 fvdl int
119 1.37 dsl linux_sys_socketcall(struct lwp *l, const struct linux_sys_socketcall_args *uap, register_t *retval)
120 1.11 thorpej {
121 1.37 dsl /* {
122 1.1 fvdl syscallarg(int) what;
123 1.1 fvdl syscallarg(void *) args;
124 1.37 dsl } */
125 1.16 erh struct linux_socketcall_dummy_args lda;
126 1.16 erh int error;
127 1.16 erh
128 1.44 maxv if (SCARG(uap, what) <= 0 || SCARG(uap, what) > LINUX_MAX_SOCKETCALL)
129 1.16 erh return ENOSYS;
130 1.16 erh
131 1.33 dsl if ((error = copyin(SCARG(uap, args), &lda,
132 1.24 christos linux_socketcall[SCARG(uap, what)].argsize))) {
133 1.24 christos DPRINTF(("copyin for %s failed %d\n",
134 1.24 christos linux_socketcall[SCARG(uap, what)].name, error));
135 1.16 erh return error;
136 1.24 christos }
137 1.11 thorpej
138 1.35 ad ktrkuser(linux_socketcall[SCARG(uap, what)].name, &lda,
139 1.35 ad linux_socketcall[SCARG(uap, what)].argsize);
140 1.33 dsl
141 1.26 jdolecek #ifdef DEBUG_LINUX
142 1.27 jdolecek /* dump the passed argument data */
143 1.26 jdolecek {
144 1.27 jdolecek DPRINTF(("linux_socketcall('%s'): ",
145 1.27 jdolecek linux_socketcall[SCARG(uap, what)].name));
146 1.26 jdolecek
147 1.43 njoly if (SCARG(uap, what) == LINUX_SYS_SOCKET) {
148 1.27 jdolecek DPRINTF(("[dom %d type %d proto %d]\n",
149 1.27 jdolecek lda.dummy_ints[0],
150 1.27 jdolecek lda.dummy_ints[1],
151 1.27 jdolecek lda.dummy_ints[2]));
152 1.27 jdolecek } else {
153 1.27 jdolecek int i, sz;
154 1.27 jdolecek u_int8_t *data = (u_int8_t *)&lda.dummy_ints[1];
155 1.27 jdolecek
156 1.27 jdolecek sz = linux_socketcall[SCARG(uap, what)].argsize
157 1.27 jdolecek - sizeof(lda.dummy_ints[0]);
158 1.27 jdolecek
159 1.27 jdolecek DPRINTF(("socket %d [", lda.dummy_ints[0]));
160 1.27 jdolecek for(i=0; i < sz; i++)
161 1.27 jdolecek DPRINTF(("%02x ", data[i]));
162 1.27 jdolecek DPRINTF(("]\n"));
163 1.27 jdolecek }
164 1.26 jdolecek }
165 1.26 jdolecek #endif
166 1.26 jdolecek
167 1.1 fvdl switch (SCARG(uap, what)) {
168 1.43 njoly case LINUX_SYS_SOCKET:
169 1.25 thorpej error = linux_sys_socket(l, (void *)&lda, retval);
170 1.24 christos break;
171 1.43 njoly case LINUX_SYS_BIND:
172 1.25 thorpej error = linux_sys_bind(l, (void *)&lda, retval);
173 1.24 christos break;
174 1.43 njoly case LINUX_SYS_CONNECT:
175 1.25 thorpej error = linux_sys_connect(l, (void *)&lda, retval);
176 1.24 christos break;
177 1.43 njoly case LINUX_SYS_LISTEN:
178 1.25 thorpej error = sys_listen(l, (void *)&lda, retval);
179 1.24 christos break;
180 1.43 njoly case LINUX_SYS_ACCEPT:
181 1.25 thorpej error = linux_sys_accept(l, (void *)&lda, retval);
182 1.24 christos break;
183 1.43 njoly case LINUX_SYS_GETSOCKNAME:
184 1.25 thorpej error = linux_sys_getsockname(l, (void *)&lda, retval);
185 1.24 christos break;
186 1.43 njoly case LINUX_SYS_GETPEERNAME:
187 1.25 thorpej error = linux_sys_getpeername(l, (void *)&lda, retval);
188 1.24 christos break;
189 1.43 njoly case LINUX_SYS_SOCKETPAIR:
190 1.25 thorpej error = linux_sys_socketpair(l, (void *)&lda, retval);
191 1.24 christos break;
192 1.43 njoly case LINUX_SYS_SEND:
193 1.25 thorpej error = linux_sys_send(l, (void *)&lda, retval);
194 1.24 christos break;
195 1.43 njoly case LINUX_SYS_RECV:
196 1.25 thorpej error = linux_sys_recv(l, (void *)&lda, retval);
197 1.24 christos break;
198 1.43 njoly case LINUX_SYS_SENDTO:
199 1.25 thorpej error = linux_sys_sendto(l, (void *)&lda, retval);
200 1.24 christos break;
201 1.43 njoly case LINUX_SYS_RECVFROM:
202 1.25 thorpej error = linux_sys_recvfrom(l, (void *)&lda, retval);
203 1.24 christos break;
204 1.43 njoly case LINUX_SYS_SHUTDOWN:
205 1.25 thorpej error = sys_shutdown(l, (void *)&lda, retval);
206 1.24 christos break;
207 1.43 njoly case LINUX_SYS_SETSOCKOPT:
208 1.25 thorpej error = linux_sys_setsockopt(l, (void *)&lda, retval);
209 1.24 christos break;
210 1.43 njoly case LINUX_SYS_GETSOCKOPT:
211 1.25 thorpej error = linux_sys_getsockopt(l, (void *)&lda, retval);
212 1.24 christos break;
213 1.43 njoly case LINUX_SYS_SENDMSG:
214 1.25 thorpej error = linux_sys_sendmsg(l, (void *)&lda, retval);
215 1.24 christos break;
216 1.43 njoly case LINUX_SYS_RECVMSG:
217 1.25 thorpej error = linux_sys_recvmsg(l, (void *)&lda, retval);
218 1.24 christos break;
219 1.46 christos case LINUX_SYS_ACCEPT4:
220 1.46 christos error = linux_sys_accept4(l, (void *)&lda, retval);
221 1.46 christos break;
222 1.47 christos case LINUX_SYS_RECVMMSG:
223 1.47 christos error = linux_sys_recvmmsg(l, (void *)&lda, retval);
224 1.47 christos break;
225 1.47 christos case LINUX_SYS_SENDMMSG:
226 1.47 christos error = linux_sys_sendmmsg(l, (void *)&lda, retval);
227 1.47 christos break;
228 1.1 fvdl default:
229 1.24 christos error = ENOSYS;
230 1.24 christos break;
231 1.1 fvdl }
232 1.24 christos
233 1.24 christos DPRINTF(("sys_%s() = %d\n", linux_socketcall[SCARG(uap, what)].name,
234 1.24 christos error));
235 1.24 christos return error;
236 1.1 fvdl }
237