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