xref: /netbsd-src/sys/compat/linux/common/linux_socketcall.c (revision de2138164cd16145ee5fd4d0aef1e8f952c1a9fb)
1 /*	$NetBSD: linux_socketcall.c,v 1.31 2007/02/09 21:55:19 ad 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.31 2007/02/09 21:55:19 ad Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/malloc.h>
47 #include <sys/ioctl.h>
48 #include <sys/tty.h>
49 #include <sys/file.h>
50 #include <sys/filedesc.h>
51 #include <sys/select.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <net/if.h>
55 #include <netinet/in.h>
56 #include <netinet/tcp.h>
57 #include <sys/mount.h>
58 #include <sys/proc.h>
59 #include <sys/vnode.h>
60 #include <sys/device.h>
61 
62 #include <sys/syscallargs.h>
63 
64 #include <compat/linux/common/linux_types.h>
65 #include <compat/linux/common/linux_util.h>
66 #include <compat/linux/common/linux_signal.h>
67 #include <compat/linux/common/linux_ioctl.h>
68 #include <compat/linux/common/linux_socket.h>
69 #include <compat/linux/common/linux_socketcall.h>
70 #include <compat/linux/common/linux_sockio.h>
71 
72 #include <compat/linux/linux_syscallargs.h>
73 
74 #undef DPRINTF
75 #ifdef DEBUG_LINUX
76 #define DPRINTF(a)	uprintf a
77 #else
78 #define DPRINTF(a)
79 #endif
80 
81 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
82 /* Not used on: alpha, amd64 */
83 
84 /*
85  * This file contains the linux_socketcall() multiplexer.  Arguments
86  * for the various socket calls are on the user stack. A pointer
87  * to them is the only thing that is passed.  We copyin the arguments
88  * here so the individual functions can assume they are normal syscalls.
89  */
90 
91 /* The sizes of the arguments.  Used for copyin. */
92 static const struct {
93 	const char *name;
94 	int argsize;
95 } linux_socketcall[LINUX_MAX_SOCKETCALL+1] = {
96 	{"invalid",	-1},						/* 0 */
97 	{"socket",	sizeof(struct linux_sys_socket_args)},		/* 1 */
98 	{"bind",	sizeof(struct linux_sys_bind_args)},		/* 2 */
99 	{"connect",	sizeof(struct linux_sys_connect_args)},		/* 3 */
100 	{"listen",	sizeof(struct linux_sys_listen_args)},		/* 4 */
101 	{"accept",	sizeof(struct linux_sys_accept_args)},		/* 5 */
102 	{"getsockname",	sizeof(struct linux_sys_getsockname_args)},	/* 6 */
103 	{"getpeername",	sizeof(struct linux_sys_getpeername_args)},	/* 7 */
104 	{"socketpair",	sizeof(struct linux_sys_socketpair_args)},	/* 8 */
105 	{"send",	sizeof(struct linux_sys_send_args)},		/* 9 */
106 	{"recv",	sizeof(struct linux_sys_recv_args)},		/* 10 */
107 	{"sendto",	sizeof(struct linux_sys_sendto_args)},		/* 11 */
108 	{"recvfrom",	sizeof(struct linux_sys_recvfrom_args)},	/* 12 */
109 	{"shutdown",	sizeof(struct linux_sys_shutdown_args)},	/* 13 */
110 	{"setsockopt",	sizeof(struct linux_sys_setsockopt_args)},	/* 14 */
111 	{"getsockopt",	sizeof(struct linux_sys_getsockopt_args)},	/* 15 */
112 	{"sendmsg",	sizeof(struct linux_sys_sendmsg_args)},		/* 16 */
113 	{"recvmsg",	sizeof(struct linux_sys_recvmsg_args)},		/* 17 */
114 };
115 
116 /*
117  * Entry point to all Linux socket calls. Just check which call to
118  * make and take appropriate action.
119  */
120 int
121 linux_sys_socketcall(l, v, retval)
122 	struct lwp *l;
123 	void *v;
124 	register_t *retval;
125 {
126 	struct linux_sys_socketcall_args /* {
127 		syscallarg(int) what;
128 		syscallarg(void *) args;
129 	} */ *uap = v;
130 	struct linux_socketcall_dummy_args lda;
131 	int error;
132 
133 	if (SCARG(uap, what) < 0 || SCARG(uap, what) > LINUX_MAX_SOCKETCALL)
134 		return ENOSYS;
135 
136 	if ((error = copyin((caddr_t) SCARG(uap, args), (caddr_t) &lda,
137 	    linux_socketcall[SCARG(uap, what)].argsize))) {
138 		DPRINTF(("copyin for %s failed %d\n",
139 		linux_socketcall[SCARG(uap, what)].name, error));
140 		return error;
141 	}
142 
143 #ifdef DEBUG_LINUX
144 	/* dump the passed argument data */
145 	{
146         	DPRINTF(("linux_socketcall('%s'): ",
147 		    linux_socketcall[SCARG(uap, what)].name));
148 
149 		if (SCARG(uap, what) == LINUX_SYS_socket) {
150 			DPRINTF(("[dom %d type %d proto %d]\n",
151 				lda.dummy_ints[0],
152 				lda.dummy_ints[1],
153 				lda.dummy_ints[2]));
154 		} else {
155 			int i, sz;
156 			u_int8_t *data = (u_int8_t *)&lda.dummy_ints[1];
157 
158 			sz = linux_socketcall[SCARG(uap, what)].argsize
159 			    - sizeof(lda.dummy_ints[0]);
160 
161 			DPRINTF(("socket %d [", lda.dummy_ints[0]));
162 			for(i=0; i < sz; i++)
163 				DPRINTF(("%02x ", data[i]));
164 			DPRINTF(("]\n"));
165 		}
166 	}
167 #endif
168 
169 	switch (SCARG(uap, what)) {
170 	case LINUX_SYS_socket:
171 		error = linux_sys_socket(l, (void *)&lda, retval);
172 		break;
173 	case LINUX_SYS_bind:
174 		error = linux_sys_bind(l, (void *)&lda, retval);
175 		break;
176 	case LINUX_SYS_connect:
177 		error = linux_sys_connect(l, (void *)&lda, retval);
178 		break;
179 	case LINUX_SYS_listen:
180 		error = sys_listen(l, (void *)&lda, retval);
181 		break;
182 	case LINUX_SYS_accept:
183 		error = linux_sys_accept(l, (void *)&lda, retval);
184 		break;
185 	case LINUX_SYS_getsockname:
186 		error = linux_sys_getsockname(l, (void *)&lda, retval);
187 		break;
188 	case LINUX_SYS_getpeername:
189 		error = linux_sys_getpeername(l, (void *)&lda, retval);
190 		break;
191 	case LINUX_SYS_socketpair:
192 		error = linux_sys_socketpair(l, (void *)&lda, retval);
193 		break;
194 	case LINUX_SYS_send:
195 		error = linux_sys_send(l, (void *)&lda, retval);
196 		break;
197 	case LINUX_SYS_recv:
198 		error = linux_sys_recv(l, (void *)&lda, retval);
199 		break;
200 	case LINUX_SYS_sendto:
201 		error = linux_sys_sendto(l, (void *)&lda, retval);
202 		break;
203 	case LINUX_SYS_recvfrom:
204 		error = linux_sys_recvfrom(l, (void *)&lda, retval);
205 		break;
206 	case LINUX_SYS_shutdown:
207 		error = sys_shutdown(l, (void *)&lda, retval);
208 		break;
209 	case LINUX_SYS_setsockopt:
210 		error = linux_sys_setsockopt(l, (void *)&lda, retval);
211 		break;
212 	case LINUX_SYS_getsockopt:
213 		error = linux_sys_getsockopt(l, (void *)&lda, retval);
214 		break;
215 	case LINUX_SYS_sendmsg:
216 		error = linux_sys_sendmsg(l, (void *)&lda, retval);
217 		break;
218 	case LINUX_SYS_recvmsg:
219 		error = linux_sys_recvmsg(l, (void *)&lda, retval);
220 		break;
221 	default:
222 		error = ENOSYS;
223 		break;
224 	}
225 
226 	DPRINTF(("sys_%s() = %d\n", linux_socketcall[SCARG(uap, what)].name,
227 	    error));
228 	return error;
229 }
230