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