xref: /netbsd-src/sys/compat/linux/common/linux_socketcall.c (revision 3feacbccbd4c8b6fd5f19c8e34474c4c3f8923d7)
1 /*	$NetBSD: linux_socketcall.c,v 1.45 2014/11/09 17:48:08 maxv 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.45 2014/11/09 17:48:08 maxv 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/ioctl.h>
40 #include <sys/tty.h>
41 #include <sys/file.h>
42 #include <sys/filedesc.h>
43 #include <sys/select.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <net/if.h>
47 #include <netinet/in.h>
48 #include <netinet/tcp.h>
49 #include <sys/mount.h>
50 #include <sys/proc.h>
51 #include <sys/vnode.h>
52 #include <sys/device.h>
53 #include <sys/ktrace.h>
54 
55 #include <sys/syscallargs.h>
56 
57 #include <compat/sys/socket.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 #define L(a) "linux/" a
92 	{L("invalid"),	-1},						/* 0 */
93 	{L("socket"),	sizeof(struct linux_sys_socket_args)},		/* 1 */
94 	{L("bind"),	sizeof(struct linux_sys_bind_args)},		/* 2 */
95 	{L("connect"),	sizeof(struct linux_sys_connect_args)},		/* 3 */
96 	{L("listen"),	sizeof(struct linux_sys_listen_args)},		/* 4 */
97 	{L("accept"),	sizeof(struct linux_sys_accept_args)},		/* 5 */
98 	{L("getsockname"),sizeof(struct linux_sys_getsockname_args)},	/* 6 */
99 	{L("getpeername"),sizeof(struct linux_sys_getpeername_args)},	/* 7 */
100 	{L("socketpair"),sizeof(struct linux_sys_socketpair_args)},	/* 8 */
101 	{L("send"),	sizeof(struct linux_sys_send_args)},		/* 9 */
102 	{L("recv"),	sizeof(struct linux_sys_recv_args)},		/* 10 */
103 	{L("sendto"),	sizeof(struct linux_sys_sendto_args)},		/* 11 */
104 	{L("recvfrom"),	sizeof(struct linux_sys_recvfrom_args)},	/* 12 */
105 	{L("shutdown"),	sizeof(struct linux_sys_shutdown_args)},	/* 13 */
106 	{L("setsockopt"),sizeof(struct linux_sys_setsockopt_args)},	/* 14 */
107 	{L("getsockopt"),sizeof(struct linux_sys_getsockopt_args)},	/* 15 */
108 	{L("sendmsg"),	sizeof(struct linux_sys_sendmsg_args)},		/* 16 */
109 	{L("recvmsg"),	sizeof(struct linux_sys_recvmsg_args)},		/* 17 */
110 #undef L
111 };
112 
113 /*
114  * Entry point to all Linux socket calls. Just check which call to
115  * make and take appropriate action.
116  */
117 int
118 linux_sys_socketcall(struct lwp *l, const struct linux_sys_socketcall_args *uap, register_t *retval)
119 {
120 	/* {
121 		syscallarg(int) what;
122 		syscallarg(void *) args;
123 	} */
124 	struct linux_socketcall_dummy_args lda;
125 	int error;
126 
127 	if (SCARG(uap, what) <= 0 || SCARG(uap, what) > LINUX_MAX_SOCKETCALL)
128 		return ENOSYS;
129 
130 	if ((error = copyin(SCARG(uap, args), &lda,
131 	    linux_socketcall[SCARG(uap, what)].argsize))) {
132 		DPRINTF(("copyin for %s failed %d\n",
133 		linux_socketcall[SCARG(uap, what)].name, error));
134 		return error;
135 	}
136 
137 	ktrkuser(linux_socketcall[SCARG(uap, what)].name, &lda,
138 	    linux_socketcall[SCARG(uap, what)].argsize);
139 
140 #ifdef DEBUG_LINUX
141 	/* dump the passed argument data */
142 	{
143         	DPRINTF(("linux_socketcall('%s'): ",
144 		    linux_socketcall[SCARG(uap, what)].name));
145 
146 		if (SCARG(uap, what) == LINUX_SYS_SOCKET) {
147 			DPRINTF(("[dom %d type %d proto %d]\n",
148 				lda.dummy_ints[0],
149 				lda.dummy_ints[1],
150 				lda.dummy_ints[2]));
151 		} else {
152 			int i, sz;
153 			u_int8_t *data = (u_int8_t *)&lda.dummy_ints[1];
154 
155 			sz = linux_socketcall[SCARG(uap, what)].argsize
156 			    - sizeof(lda.dummy_ints[0]);
157 
158 			DPRINTF(("socket %d [", lda.dummy_ints[0]));
159 			for(i=0; i < sz; i++)
160 				DPRINTF(("%02x ", data[i]));
161 			DPRINTF(("]\n"));
162 		}
163 	}
164 #endif
165 
166 	switch (SCARG(uap, what)) {
167 	case LINUX_SYS_SOCKET:
168 		error = linux_sys_socket(l, (void *)&lda, retval);
169 		break;
170 	case LINUX_SYS_BIND:
171 		error = linux_sys_bind(l, (void *)&lda, retval);
172 		break;
173 	case LINUX_SYS_CONNECT:
174 		error = linux_sys_connect(l, (void *)&lda, retval);
175 		break;
176 	case LINUX_SYS_LISTEN:
177 		error = sys_listen(l, (void *)&lda, retval);
178 		break;
179 	case LINUX_SYS_ACCEPT:
180 		error = linux_sys_accept(l, (void *)&lda, retval);
181 		break;
182 	case LINUX_SYS_GETSOCKNAME:
183 		error = linux_sys_getsockname(l, (void *)&lda, retval);
184 		break;
185 	case LINUX_SYS_GETPEERNAME:
186 		error = linux_sys_getpeername(l, (void *)&lda, retval);
187 		break;
188 	case LINUX_SYS_SOCKETPAIR:
189 		error = linux_sys_socketpair(l, (void *)&lda, retval);
190 		break;
191 	case LINUX_SYS_SEND:
192 		error = linux_sys_send(l, (void *)&lda, retval);
193 		break;
194 	case LINUX_SYS_RECV:
195 		error = linux_sys_recv(l, (void *)&lda, retval);
196 		break;
197 	case LINUX_SYS_SENDTO:
198 		error = linux_sys_sendto(l, (void *)&lda, retval);
199 		break;
200 	case LINUX_SYS_RECVFROM:
201 		error = linux_sys_recvfrom(l, (void *)&lda, retval);
202 		break;
203 	case LINUX_SYS_SHUTDOWN:
204 		error = sys_shutdown(l, (void *)&lda, retval);
205 		break;
206 	case LINUX_SYS_SETSOCKOPT:
207 		error = linux_sys_setsockopt(l, (void *)&lda, retval);
208 		break;
209 	case LINUX_SYS_GETSOCKOPT:
210 		error = linux_sys_getsockopt(l, (void *)&lda, retval);
211 		break;
212 	case LINUX_SYS_SENDMSG:
213 		error = linux_sys_sendmsg(l, (void *)&lda, retval);
214 		break;
215 	case LINUX_SYS_RECVMSG:
216 		error = linux_sys_recvmsg(l, (void *)&lda, retval);
217 		break;
218 	default:
219 		error = ENOSYS;
220 		break;
221 	}
222 
223 	DPRINTF(("sys_%s() = %d\n", linux_socketcall[SCARG(uap, what)].name,
224 	    error));
225 	return error;
226 }
227