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