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