xref: /netbsd-src/sys/compat/linux/common/linux_socketcall.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: linux_socketcall.c,v 1.23 2001/11/13 02:09:00 lukem 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.23 2001/11/13 02:09:00 lukem 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 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
75 /* Not used on: alpha */
76 
77 /*
78  * This file contains the linux_socketcall() multiplexer.  Arguments
79  * for the various socket calls are on the user stack. A pointer
80  * to them is the only thing that is passed.  We copyin the arguments
81  * here so the individual functions can assume they are normal syscalls.
82  */
83 
84 /* The sizes of the arguments.  Used for copyin. */
85 static const int linux_socketcall_argsize[LINUX_MAX_SOCKETCALL+1] = {
86 	-1,
87 	sizeof(struct linux_sys_socket_args),		/* 1 */
88 	sizeof(struct linux_sys_bind_args),
89 	sizeof(struct linux_sys_connect_args),
90 	sizeof(struct linux_sys_listen_args),
91 	sizeof(struct linux_sys_accept_args),		/* 5 */
92 	sizeof(struct linux_sys_getsockname_args),
93 	sizeof(struct linux_sys_getpeername_args),
94 	sizeof(struct linux_sys_socketpair_args),
95 	sizeof(struct linux_sys_send_args),
96 	sizeof(struct linux_sys_recv_args),		/* 10 */
97 	sizeof(struct linux_sys_sendto_args),
98 	sizeof(struct linux_sys_recvfrom_args),
99 	sizeof(struct linux_sys_shutdown_args),
100 	sizeof(struct linux_sys_setsockopt_args),
101 	sizeof(struct linux_sys_getsockopt_args),	/* 15 */
102 	sizeof(struct linux_sys_sendmsg_args),
103 	sizeof(struct linux_sys_recvmsg_args),		/* 17 */
104 };
105 
106 /*
107  * Entry point to all Linux socket calls. Just check which call to
108  * make and take appropriate action.
109  */
110 int
111 linux_sys_socketcall(p, v, retval)
112 	struct proc *p;
113 	void *v;
114 	register_t *retval;
115 {
116 	struct linux_sys_socketcall_args /* {
117 		syscallarg(int) what;
118 		syscallarg(void *) args;
119 	} */ *uap = v;
120 	struct linux_socketcall_dummy_args lda;
121 	int error;
122 
123 	if (SCARG(uap, what) < 0 || SCARG(uap, what) > LINUX_MAX_SOCKETCALL)
124 		return ENOSYS;
125 
126 	if ((error = copyin((caddr_t) SCARG(uap, args), (caddr_t) &lda,
127 	    linux_socketcall_argsize[SCARG(uap, what)])))
128 		return error;
129 
130 	switch (SCARG(uap, what)) {
131 	case LINUX_SYS_socket:
132 		return linux_sys_socket(p, (void *)&lda, retval);
133 	case LINUX_SYS_bind:
134 		return linux_sys_bind(p, (void *)&lda, retval);
135 	case LINUX_SYS_connect:
136 		return linux_sys_connect(p, (void *)&lda, retval);
137 	case LINUX_SYS_listen:
138 		return sys_listen(p, (void *)&lda, retval);
139 	case LINUX_SYS_accept:
140 		return linux_sys_accept(p, (void *)&lda, retval);
141 	case LINUX_SYS_getsockname:
142 		return linux_sys_getsockname(p, (void *)&lda, retval);
143 	case LINUX_SYS_getpeername:
144 		return linux_sys_getpeername(p, (void *)&lda, retval);
145 	case LINUX_SYS_socketpair:
146 		return linux_sys_socketpair(p, (void *)&lda, retval);
147 	case LINUX_SYS_send:
148 		return linux_sys_send(p, (void *)&lda, retval);
149 	case LINUX_SYS_recv:
150 		return linux_sys_recv(p, (void *)&lda, retval);
151 	case LINUX_SYS_sendto:
152 		return linux_sys_sendto(p, (void *)&lda, retval);
153 	case LINUX_SYS_recvfrom:
154 		return linux_sys_recvfrom(p, (void *)&lda, retval);
155 	case LINUX_SYS_shutdown:
156 		return sys_shutdown(p, (void *)&lda, retval);
157 	case LINUX_SYS_setsockopt:
158 		return linux_sys_setsockopt(p, (void *)&lda, retval);
159 	case LINUX_SYS_getsockopt:
160 		return linux_sys_getsockopt(p, (void *)&lda, retval);
161 	case LINUX_SYS_sendmsg:
162 		return linux_sys_sendmsg(p, (void *)&lda, retval);
163 	case LINUX_SYS_recvmsg:
164 		return linux_sys_recvmsg(p, (void *)&lda, retval);
165 	default:
166 		return ENOSYS;
167 	}
168 }
169