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