Home | History | Annotate | Line # | Download | only in common
linux32_socket.c revision 1.5.6.1
      1  1.5.6.1   mjf /*	$NetBSD: linux32_socket.c,v 1.5.6.1 2008/06/29 09:33:03 mjf Exp $ */
      2      1.1  manu 
      3      1.1  manu /*-
      4      1.1  manu  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
      5      1.1  manu  *
      6      1.1  manu  * Redistribution and use in source and binary forms, with or without
      7      1.1  manu  * modification, are permitted provided that the following conditions
      8      1.1  manu  * are met:
      9      1.1  manu  * 1. Redistributions of source code must retain the above copyright
     10      1.1  manu  *    notice, this list of conditions and the following disclaimer.
     11      1.1  manu  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1  manu  *    notice, this list of conditions and the following disclaimer in the
     13      1.1  manu  *    documentation and/or other materials provided with the distribution.
     14      1.1  manu  * 3. All advertising materials mentioning features or use of this software
     15      1.1  manu  *    must display the following acknowledgement:
     16      1.1  manu  *	This product includes software developed by Emmanuel Dreyfus
     17      1.1  manu  * 4. The name of the author may not be used to endorse or promote
     18      1.1  manu  *    products derived from this software without specific prior written
     19      1.1  manu  *    permission.
     20      1.1  manu  *
     21      1.1  manu  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
     22      1.1  manu  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     23      1.1  manu  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24      1.1  manu  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     25      1.1  manu  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26      1.1  manu  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27      1.1  manu  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28      1.1  manu  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29      1.1  manu  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30      1.1  manu  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31      1.1  manu  * POSSIBILITY OF SUCH DAMAGE.
     32      1.1  manu  */
     33      1.1  manu 
     34      1.1  manu #include <sys/cdefs.h>
     35      1.1  manu 
     36  1.5.6.1   mjf __KERNEL_RCSID(0, "$NetBSD: linux32_socket.c,v 1.5.6.1 2008/06/29 09:33:03 mjf Exp $");
     37      1.1  manu 
     38      1.1  manu #include <sys/types.h>
     39      1.1  manu #include <sys/param.h>
     40      1.1  manu #include <sys/fstypes.h>
     41      1.1  manu #include <sys/signal.h>
     42      1.1  manu #include <sys/dirent.h>
     43      1.1  manu #include <sys/kernel.h>
     44      1.1  manu #include <sys/fcntl.h>
     45      1.1  manu #include <sys/select.h>
     46      1.1  manu #include <sys/proc.h>
     47      1.1  manu #include <sys/ucred.h>
     48      1.1  manu #include <sys/swap.h>
     49  1.5.6.1   mjf #include <sys/file.h>
     50  1.5.6.1   mjf #include <sys/vnode.h>
     51  1.5.6.1   mjf #include <sys/filedesc.h>
     52      1.1  manu 
     53      1.1  manu #include <machine/types.h>
     54      1.1  manu 
     55  1.5.6.1   mjf #include <net/if.h>
     56  1.5.6.1   mjf #include <net/if_dl.h>
     57  1.5.6.1   mjf #include <net/if_types.h>
     58  1.5.6.1   mjf #include <net/route.h>
     59  1.5.6.1   mjf 
     60  1.5.6.1   mjf #include <netinet/in.h>
     61  1.5.6.1   mjf #include <netinet/ip_mroute.h>
     62  1.5.6.1   mjf 
     63      1.1  manu #include <sys/syscallargs.h>
     64      1.1  manu 
     65      1.1  manu #include <compat/netbsd32/netbsd32.h>
     66  1.5.6.1   mjf #include <compat/netbsd32/netbsd32_ioctl.h>
     67      1.1  manu #include <compat/netbsd32/netbsd32_conv.h>
     68      1.1  manu #include <compat/netbsd32/netbsd32_syscallargs.h>
     69      1.1  manu 
     70  1.5.6.1   mjf #include <compat/sys/socket.h>
     71  1.5.6.1   mjf #include <compat/sys/sockio.h>
     72  1.5.6.1   mjf 
     73  1.5.6.1   mjf #include <compat/linux/common/linux_types.h>
     74      1.1  manu #include <compat/linux/common/linux_types.h>
     75      1.1  manu #include <compat/linux/common/linux_signal.h>
     76      1.1  manu #include <compat/linux/common/linux_machdep.h>
     77      1.1  manu #include <compat/linux/common/linux_misc.h>
     78      1.1  manu #include <compat/linux/common/linux_oldolduname.h>
     79  1.5.6.1   mjf #include <compat/linux/common/linux_ioctl.h>
     80  1.5.6.1   mjf #include <compat/linux/common/linux_sockio.h>
     81      1.1  manu #include <compat/linux/linux_syscallargs.h>
     82      1.1  manu 
     83      1.1  manu #include <compat/linux32/common/linux32_types.h>
     84      1.1  manu #include <compat/linux32/common/linux32_signal.h>
     85      1.1  manu #include <compat/linux32/common/linux32_machdep.h>
     86      1.1  manu #include <compat/linux32/common/linux32_sysctl.h>
     87      1.1  manu #include <compat/linux32/common/linux32_socketcall.h>
     88  1.5.6.1   mjf #include <compat/linux32/common/linux32_sockio.h>
     89  1.5.6.1   mjf #include <compat/linux32/common/linux32_ioctl.h>
     90      1.1  manu #include <compat/linux32/linux32_syscallargs.h>
     91      1.1  manu 
     92  1.5.6.1   mjf int linux32_getifhwaddr(struct lwp *, register_t *, u_int, void *);
     93  1.5.6.1   mjf 
     94      1.1  manu int
     95      1.5   dsl linux32_sys_socketpair(struct lwp *l, const struct linux32_sys_socketpair_args *uap, register_t *retval)
     96      1.1  manu {
     97      1.5   dsl 	/* {
     98      1.1  manu 		syscallarg(int) domain;
     99      1.1  manu 		syscallarg(int) type;
    100      1.1  manu 		syscallarg(int) protocol;
    101      1.1  manu 		syscallarg(netbsd32_intp) rsv;
    102      1.5   dsl 	} */
    103      1.1  manu 	struct linux_sys_socketpair_args ua;
    104      1.1  manu 
    105      1.1  manu 	NETBSD32TO64_UAP(domain);
    106      1.1  manu 	NETBSD32TO64_UAP(type);
    107      1.1  manu 	NETBSD32TO64_UAP(protocol);
    108      1.1  manu 	NETBSD32TOP_UAP(rsv, int)
    109      1.1  manu 
    110      1.1  manu 	return linux_sys_socketpair(l, &ua, retval);
    111      1.1  manu }
    112      1.1  manu 
    113      1.1  manu int
    114      1.5   dsl linux32_sys_sendto(struct lwp *l, const struct linux32_sys_sendto_args *uap, register_t *retval)
    115      1.1  manu {
    116      1.5   dsl 	/* {
    117      1.1  manu 		syscallarg(int) s;
    118      1.5   dsl 		syscallarg(netbsd32_voidp) msg;
    119      1.1  manu 		syscallarg(int) len;
    120      1.1  manu 		syscallarg(int) flags;
    121      1.1  manu 		syscallarg(netbsd32_osockaddrp_t) to;
    122      1.1  manu 		syscallarg(int) tolen;
    123      1.5   dsl 	} */
    124      1.1  manu 	struct linux_sys_sendto_args ua;
    125      1.1  manu 
    126      1.1  manu 	NETBSD32TO64_UAP(s);
    127      1.1  manu 	NETBSD32TOP_UAP(msg, void);
    128      1.1  manu 	NETBSD32TO64_UAP(len);
    129      1.1  manu 	NETBSD32TO64_UAP(flags);
    130      1.1  manu 	NETBSD32TOP_UAP(to, struct osockaddr);
    131      1.1  manu 	NETBSD32TO64_UAP(tolen);
    132      1.1  manu 
    133      1.1  manu 	return linux_sys_sendto(l, &ua, retval);
    134      1.1  manu }
    135      1.1  manu 
    136      1.1  manu 
    137      1.1  manu int
    138      1.5   dsl linux32_sys_recvfrom(struct lwp *l, const struct linux32_sys_recvfrom_args *uap, register_t *retval)
    139      1.1  manu {
    140      1.5   dsl 	/* {
    141      1.1  manu 		syscallarg(int) s;
    142      1.5   dsl 		syscallarg(netbsd32_voidp) buf;
    143      1.1  manu 		syscallarg(netbsd32_size_t) len;
    144      1.1  manu 		syscallarg(int) flags;
    145      1.1  manu 		syscallarg(netbsd32_osockaddrp_t) from;
    146      1.1  manu 		syscallarg(netbsd32_intp) fromlenaddr;
    147      1.5   dsl 	} */
    148      1.1  manu 	struct linux_sys_recvfrom_args ua;
    149      1.1  manu 
    150      1.1  manu 	NETBSD32TO64_UAP(s);
    151      1.1  manu 	NETBSD32TOP_UAP(buf, void);
    152      1.1  manu 	NETBSD32TO64_UAP(len);
    153      1.1  manu 	NETBSD32TO64_UAP(flags);
    154      1.1  manu 	NETBSD32TOP_UAP(from, struct osockaddr);
    155      1.1  manu 	NETBSD32TOP_UAP(fromlenaddr, unsigned int);
    156      1.1  manu 
    157      1.1  manu 	return linux_sys_recvfrom(l, &ua, retval);
    158      1.1  manu }
    159      1.1  manu 
    160      1.1  manu int
    161      1.5   dsl linux32_sys_setsockopt(struct lwp *l, const struct linux32_sys_setsockopt_args *uap, register_t *retval)
    162      1.1  manu {
    163      1.5   dsl 	/* {
    164      1.1  manu 		syscallarg(int) s;
    165      1.1  manu 		syscallarg(int) level;
    166      1.1  manu 		syscallarg(int) optname;
    167      1.1  manu 		syscallarg(netbsd32_voidp) optval;
    168      1.1  manu 		syscallarg(int) optlen;
    169      1.5   dsl 	} */
    170      1.1  manu 	struct linux_sys_setsockopt_args ua;
    171      1.1  manu 
    172      1.1  manu 	NETBSD32TO64_UAP(s);
    173      1.1  manu 	NETBSD32TO64_UAP(level);
    174      1.1  manu 	NETBSD32TO64_UAP(optname);
    175      1.1  manu 	NETBSD32TOP_UAP(optval, void);
    176      1.1  manu 	NETBSD32TO64_UAP(optlen);
    177      1.1  manu 
    178      1.1  manu 	return linux_sys_setsockopt(l, &ua, retval);
    179      1.1  manu }
    180      1.1  manu 
    181      1.1  manu 
    182      1.1  manu int
    183      1.5   dsl linux32_sys_getsockopt(struct lwp *l, const struct linux32_sys_getsockopt_args *uap, register_t *retval)
    184      1.1  manu {
    185      1.5   dsl 	/* {
    186      1.1  manu 		syscallarg(int) s;
    187      1.1  manu 		syscallarg(int) level;
    188      1.1  manu 		syscallarg(int) optname;
    189      1.1  manu 		syscallarg(netbsd32_voidp) optval;
    190      1.1  manu 		syscallarg(netbsd32_intp) optlen;
    191      1.5   dsl 	} */
    192      1.1  manu 	struct linux_sys_getsockopt_args ua;
    193      1.1  manu 
    194      1.1  manu 	NETBSD32TO64_UAP(s);
    195      1.1  manu 	NETBSD32TO64_UAP(level);
    196      1.1  manu 	NETBSD32TO64_UAP(optname);
    197      1.1  manu 	NETBSD32TOP_UAP(optval, void);
    198      1.1  manu 	NETBSD32TOP_UAP(optlen, int);
    199      1.1  manu 
    200      1.1  manu 	return linux_sys_getsockopt(l, &ua, retval);
    201      1.1  manu }
    202      1.1  manu 
    203      1.1  manu int
    204      1.5   dsl linux32_sys_socket(struct lwp *l, const struct linux32_sys_socket_args *uap, register_t *retval)
    205      1.1  manu {
    206      1.5   dsl 	/* {
    207      1.1  manu 		syscallarg(int) domain;
    208      1.1  manu 		syscallarg(int) type;
    209      1.1  manu 		syscallarg(int) protocol;
    210      1.5   dsl 	} */
    211      1.1  manu 	struct linux_sys_socket_args ua;
    212      1.1  manu 
    213      1.1  manu 	NETBSD32TO64_UAP(domain);
    214      1.1  manu 	NETBSD32TO64_UAP(type);
    215      1.1  manu 	NETBSD32TO64_UAP(protocol);
    216      1.1  manu 
    217      1.1  manu 	return linux_sys_socket(l, &ua, retval);
    218      1.1  manu }
    219      1.1  manu 
    220      1.1  manu int
    221      1.5   dsl linux32_sys_bind(struct lwp *l, const struct linux32_sys_bind_args *uap, register_t *retval)
    222      1.1  manu {
    223      1.5   dsl 	/* {
    224      1.1  manu 		syscallarg(int) s;
    225      1.1  manu 		syscallarg(netbsd32_osockaddrp_t) name;
    226      1.1  manu 		syscallarg(int) namelen;
    227      1.5   dsl 	} */
    228      1.1  manu 	struct linux_sys_bind_args ua;
    229      1.1  manu 
    230      1.1  manu 	NETBSD32TO64_UAP(s);
    231      1.1  manu 	NETBSD32TOP_UAP(name, struct osockaddr)
    232      1.1  manu 	NETBSD32TO64_UAP(namelen);
    233      1.1  manu 
    234      1.1  manu 	return linux_sys_bind(l, &ua, retval);
    235      1.1  manu }
    236      1.1  manu 
    237      1.1  manu int
    238      1.5   dsl linux32_sys_connect(struct lwp *l, const struct linux32_sys_connect_args *uap, register_t *retval)
    239      1.1  manu {
    240      1.5   dsl 	/* {
    241      1.1  manu 		syscallarg(int) s;
    242      1.1  manu 		syscallarg(netbsd32_osockaddrp_t) name;
    243      1.1  manu 		syscallarg(int) namelen;
    244      1.5   dsl 	} */
    245      1.1  manu 	struct linux_sys_connect_args ua;
    246      1.1  manu 
    247      1.1  manu 	NETBSD32TO64_UAP(s);
    248      1.1  manu 	NETBSD32TOP_UAP(name, struct osockaddr)
    249      1.1  manu 	NETBSD32TO64_UAP(namelen);
    250      1.1  manu 
    251      1.2  manu #ifdef DEBUG_LINUX
    252      1.1  manu 	printf("linux32_sys_connect: s = %d, name = %p, namelen = %d\n",
    253      1.1  manu 		SCARG(&ua, s), SCARG(&ua, name), SCARG(&ua, namelen));
    254      1.2  manu #endif
    255      1.1  manu 
    256      1.1  manu 	return linux_sys_connect(l, &ua, retval);
    257      1.1  manu }
    258      1.1  manu 
    259      1.1  manu int
    260      1.5   dsl linux32_sys_accept(struct lwp *l, const struct linux32_sys_accept_args *uap, register_t *retval)
    261      1.1  manu {
    262      1.5   dsl 	/* {
    263      1.1  manu 		syscallarg(int) s;
    264      1.1  manu 		syscallarg(netbsd32_osockaddrp_t) name;
    265      1.1  manu 		syscallarg(netbsd32_intp) anamelen;
    266      1.5   dsl 	} */
    267      1.1  manu 	struct linux_sys_accept_args ua;
    268      1.1  manu 
    269      1.1  manu 	NETBSD32TO64_UAP(s);
    270      1.1  manu 	NETBSD32TOP_UAP(name, struct osockaddr)
    271      1.1  manu 	NETBSD32TOP_UAP(anamelen, int);
    272      1.1  manu 
    273      1.1  manu 	return linux_sys_accept(l, &ua, retval);
    274      1.1  manu }
    275      1.1  manu 
    276      1.1  manu int
    277      1.5   dsl linux32_sys_getpeername(struct lwp *l, const struct linux32_sys_getpeername_args *uap, register_t *retval)
    278      1.1  manu {
    279      1.5   dsl 	/* {
    280      1.1  manu 		syscallarg(int) fdes;
    281      1.1  manu 		syscallarg(netbsd32_sockaddrp_t) asa;
    282      1.1  manu 		syscallarg(netbsd32_intp) alen;
    283      1.5   dsl 	} */
    284      1.1  manu 	struct linux_sys_getpeername_args ua;
    285      1.1  manu 
    286      1.1  manu 	NETBSD32TO64_UAP(fdes);
    287      1.1  manu 	NETBSD32TOP_UAP(asa, struct sockaddr)
    288      1.1  manu 	NETBSD32TOP_UAP(alen, int);
    289      1.1  manu 
    290      1.1  manu 	return linux_sys_getpeername(l, &ua, retval);
    291      1.1  manu }
    292      1.1  manu 
    293      1.1  manu int
    294      1.5   dsl linux32_sys_getsockname(struct lwp *l, const struct linux32_sys_getsockname_args *uap, register_t *retval)
    295      1.1  manu {
    296      1.5   dsl 	/* {
    297      1.1  manu 		syscallarg(int) fdec;
    298      1.1  manu 		syscallarg(netbsd32_charp) asa;
    299      1.1  manu 		syscallarg(netbsd32_intp) alen;
    300      1.5   dsl 	} */
    301      1.1  manu 	struct linux_sys_getsockname_args ua;
    302      1.1  manu 
    303      1.1  manu 	NETBSD32TO64_UAP(fdec);
    304      1.1  manu 	NETBSD32TOP_UAP(asa, char)
    305      1.1  manu 	NETBSD32TOP_UAP(alen, int);
    306      1.1  manu 
    307      1.1  manu 	return linux_sys_getsockname(l, &ua, retval);
    308      1.1  manu }
    309      1.1  manu 
    310      1.1  manu int
    311      1.5   dsl linux32_sys_sendmsg(struct lwp *l, const struct linux32_sys_sendmsg_args *uap, register_t *retval)
    312      1.1  manu {
    313      1.5   dsl 	/* {
    314      1.1  manu 		syscallarg(int) s;
    315      1.1  manu 		syscallarg(netbsd32_msghdrp_t) msg;
    316      1.1  manu 		syscallarg(int) flags;
    317      1.5   dsl 	} */
    318      1.1  manu 	struct linux_sys_sendmsg_args ua;
    319      1.1  manu 
    320      1.1  manu 	NETBSD32TO64_UAP(s);
    321      1.1  manu 	NETBSD32TOP_UAP(msg, struct msghdr);
    322      1.1  manu 	NETBSD32TO64_UAP(flags);
    323      1.1  manu 
    324      1.1  manu 	return linux_sys_sendmsg(l, &ua, retval);
    325      1.1  manu }
    326      1.1  manu 
    327      1.1  manu int
    328      1.5   dsl linux32_sys_recvmsg(struct lwp *l, const struct linux32_sys_recvmsg_args *uap, register_t *retval)
    329      1.1  manu {
    330      1.5   dsl 	/* {
    331      1.1  manu 		syscallarg(int) s;
    332      1.1  manu 		syscallarg(netbsd32_msghdrp_t) msg;
    333      1.1  manu 		syscallarg(int) flags;
    334      1.5   dsl 	} */
    335      1.1  manu 	struct linux_sys_recvmsg_args ua;
    336      1.1  manu 
    337      1.1  manu 	NETBSD32TO64_UAP(s);
    338      1.1  manu 	NETBSD32TOP_UAP(msg, struct msghdr);
    339      1.1  manu 	NETBSD32TO64_UAP(flags);
    340      1.1  manu 
    341      1.1  manu 	return linux_sys_recvmsg(l, &ua, retval);
    342      1.1  manu }
    343      1.1  manu 
    344      1.1  manu int
    345      1.5   dsl linux32_sys_send(struct lwp *l, const struct linux32_sys_send_args *uap, register_t *retval)
    346      1.1  manu {
    347      1.5   dsl 	/* {
    348      1.1  manu 		syscallarg(int) s;
    349      1.1  manu 		syscallarg(netbsd32_voidp) buf;
    350      1.1  manu 		syscallarg(int) len;
    351      1.1  manu 		syscallarg(int) flags;
    352      1.5   dsl 	} */
    353      1.1  manu 	struct sys_sendto_args ua;
    354      1.1  manu 
    355      1.1  manu 	NETBSD32TO64_UAP(s);
    356      1.1  manu 	NETBSD32TOP_UAP(buf, void);
    357      1.1  manu 	NETBSD32TO64_UAP(len);
    358      1.1  manu 	NETBSD32TO64_UAP(flags);
    359      1.1  manu 	SCARG(&ua, to) = NULL;
    360      1.1  manu 	SCARG(&ua, tolen) = 0;
    361      1.1  manu 
    362      1.1  manu 	return sys_sendto(l, &ua, retval);
    363      1.1  manu }
    364      1.1  manu 
    365      1.1  manu int
    366      1.5   dsl linux32_sys_recv(struct lwp *l, const struct linux32_sys_recv_args *uap, register_t *retval)
    367      1.1  manu {
    368      1.5   dsl 	/* {
    369      1.1  manu 		syscallarg(int) s;
    370      1.1  manu 		syscallarg(netbsd32_voidp) buf;
    371      1.1  manu 		syscallarg(int) len;
    372      1.1  manu 		syscallarg(int) flags;
    373      1.5   dsl 	} */
    374      1.1  manu 	struct sys_recvfrom_args ua;
    375      1.1  manu 
    376      1.1  manu 	NETBSD32TO64_UAP(s);
    377      1.1  manu 	NETBSD32TOP_UAP(buf, void);
    378      1.1  manu 	NETBSD32TO64_UAP(len);
    379      1.1  manu 	NETBSD32TO64_UAP(flags);
    380      1.1  manu 	SCARG(&ua, from) = NULL;
    381      1.1  manu 	SCARG(&ua, fromlenaddr) = NULL;
    382      1.1  manu 
    383      1.1  manu 	return sys_recvfrom(l, &ua, retval);
    384      1.1  manu }
    385  1.5.6.1   mjf 
    386  1.5.6.1   mjf int
    387  1.5.6.1   mjf linux32_getifhwaddr(struct lwp *l, register_t *retval, u_int fd,
    388  1.5.6.1   mjf     void *data)
    389  1.5.6.1   mjf {
    390  1.5.6.1   mjf 	struct linux32_ifreq lreq;
    391  1.5.6.1   mjf 	file_t *fp;
    392  1.5.6.1   mjf 	struct ifaddr *ifa;
    393  1.5.6.1   mjf 	struct ifnet *ifp;
    394  1.5.6.1   mjf 	struct sockaddr_dl *sadl;
    395  1.5.6.1   mjf 	int error, found;
    396  1.5.6.1   mjf 	int index, ifnum;
    397  1.5.6.1   mjf 
    398  1.5.6.1   mjf 	/*
    399  1.5.6.1   mjf 	 * We can't emulate this ioctl by calling sys_ioctl() to run
    400  1.5.6.1   mjf 	 * SIOCGIFCONF, because the user buffer is not of the right
    401  1.5.6.1   mjf 	 * type to take those results.  We can't use kernel buffers to
    402  1.5.6.1   mjf 	 * receive the results, as the implementation of sys_ioctl()
    403  1.5.6.1   mjf 	 * and ifconf() [which implements SIOCGIFCONF] use
    404  1.5.6.1   mjf 	 * copyin()/copyout() which will fail on kernel addresses.
    405  1.5.6.1   mjf 	 *
    406  1.5.6.1   mjf 	 * So, we must duplicate code from sys_ioctl() and ifconf().  Ugh.
    407  1.5.6.1   mjf 	 */
    408  1.5.6.1   mjf 
    409  1.5.6.1   mjf 	if ((fp = fd_getfile(fd)) == NULL)
    410  1.5.6.1   mjf 		return (EBADF);
    411  1.5.6.1   mjf 
    412  1.5.6.1   mjf 	KERNEL_LOCK(1, NULL);
    413  1.5.6.1   mjf 
    414  1.5.6.1   mjf 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    415  1.5.6.1   mjf 		error = EBADF;
    416  1.5.6.1   mjf 		goto out;
    417  1.5.6.1   mjf 	}
    418  1.5.6.1   mjf 
    419  1.5.6.1   mjf 	error = copyin(data, &lreq, sizeof(lreq));
    420  1.5.6.1   mjf 	if (error)
    421  1.5.6.1   mjf 		goto out;
    422  1.5.6.1   mjf 	lreq.ifr_name[LINUX32_IFNAMSIZ-1] = '\0';		/* just in case */
    423  1.5.6.1   mjf 
    424  1.5.6.1   mjf 	/*
    425  1.5.6.1   mjf 	 * Try real interface name first, then fake "ethX"
    426  1.5.6.1   mjf 	 */
    427  1.5.6.1   mjf 	found = 0;
    428  1.5.6.1   mjf 	IFNET_FOREACH(ifp) {
    429  1.5.6.1   mjf 		if (found)
    430  1.5.6.1   mjf 			break;
    431  1.5.6.1   mjf 		if (strcmp(lreq.ifr_name, ifp->if_xname))
    432  1.5.6.1   mjf 			/* not this interface */
    433  1.5.6.1   mjf 			continue;
    434  1.5.6.1   mjf 		found=1;
    435  1.5.6.1   mjf 		if (IFADDR_EMPTY(ifp)) {
    436  1.5.6.1   mjf 			error = ENODEV;
    437  1.5.6.1   mjf 			goto out;
    438  1.5.6.1   mjf 		}
    439  1.5.6.1   mjf 		IFADDR_FOREACH(ifa, ifp) {
    440  1.5.6.1   mjf 			sadl = satosdl(ifa->ifa_addr);
    441  1.5.6.1   mjf 			/* only return ethernet addresses */
    442  1.5.6.1   mjf 			/* XXX what about FDDI, etc. ? */
    443  1.5.6.1   mjf 			if (sadl->sdl_family != AF_LINK ||
    444  1.5.6.1   mjf 			    sadl->sdl_type != IFT_ETHER)
    445  1.5.6.1   mjf 				continue;
    446  1.5.6.1   mjf 			memcpy(&lreq.ifr_hwaddr.sa_data, CLLADDR(sadl),
    447  1.5.6.1   mjf 			       MIN(sadl->sdl_alen,
    448  1.5.6.1   mjf 				   sizeof(lreq.ifr_hwaddr.sa_data)));
    449  1.5.6.1   mjf 			lreq.ifr_hwaddr.sa_family =
    450  1.5.6.1   mjf 				sadl->sdl_family;
    451  1.5.6.1   mjf 			error = copyout(&lreq, data, sizeof(lreq));
    452  1.5.6.1   mjf 			goto out;
    453  1.5.6.1   mjf 		}
    454  1.5.6.1   mjf 	}
    455  1.5.6.1   mjf 
    456  1.5.6.1   mjf 	if (strncmp(lreq.ifr_name, "eth", 3) == 0) {
    457  1.5.6.1   mjf 		for (ifnum = 0, index = 3;
    458  1.5.6.1   mjf 		     lreq.ifr_name[index] != '\0' && index < LINUX32_IFNAMSIZ;
    459  1.5.6.1   mjf 		     index++) {
    460  1.5.6.1   mjf 			ifnum *= 10;
    461  1.5.6.1   mjf 			ifnum += lreq.ifr_name[index] - '0';
    462  1.5.6.1   mjf 		}
    463  1.5.6.1   mjf 
    464  1.5.6.1   mjf 		error = EINVAL;			/* in case we don't find one */
    465  1.5.6.1   mjf 		found = 0;
    466  1.5.6.1   mjf 		IFNET_FOREACH(ifp) {
    467  1.5.6.1   mjf 			if (found)
    468  1.5.6.1   mjf 				break;
    469  1.5.6.1   mjf 			memcpy(lreq.ifr_name, ifp->if_xname,
    470  1.5.6.1   mjf 			       MIN(LINUX32_IFNAMSIZ, IFNAMSIZ));
    471  1.5.6.1   mjf 			IFADDR_FOREACH(ifa, ifp) {
    472  1.5.6.1   mjf 				sadl = satosdl(ifa->ifa_addr);
    473  1.5.6.1   mjf 				/* only return ethernet addresses */
    474  1.5.6.1   mjf 				/* XXX what about FDDI, etc. ? */
    475  1.5.6.1   mjf 				if (sadl->sdl_family != AF_LINK ||
    476  1.5.6.1   mjf 				    sadl->sdl_type != IFT_ETHER)
    477  1.5.6.1   mjf 					continue;
    478  1.5.6.1   mjf 				if (ifnum--)
    479  1.5.6.1   mjf 					/* not the reqested iface */
    480  1.5.6.1   mjf 					continue;
    481  1.5.6.1   mjf 				memcpy(&lreq.ifr_hwaddr.sa_data,
    482  1.5.6.1   mjf 				       CLLADDR(sadl),
    483  1.5.6.1   mjf 				       MIN(sadl->sdl_alen,
    484  1.5.6.1   mjf 					   sizeof(lreq.ifr_hwaddr.sa_data)));
    485  1.5.6.1   mjf 				lreq.ifr_hwaddr.sa_family =
    486  1.5.6.1   mjf 					sadl->sdl_family;
    487  1.5.6.1   mjf 				error = copyout(&lreq, data, sizeof(lreq));
    488  1.5.6.1   mjf 				found = 1;
    489  1.5.6.1   mjf 				break;
    490  1.5.6.1   mjf 			}
    491  1.5.6.1   mjf 		}
    492  1.5.6.1   mjf 	} else {
    493  1.5.6.1   mjf 		/* unknown interface, not even an "eth*" name */
    494  1.5.6.1   mjf 		error = ENODEV;
    495  1.5.6.1   mjf 	}
    496  1.5.6.1   mjf 
    497  1.5.6.1   mjf out:
    498  1.5.6.1   mjf 	KERNEL_UNLOCK_ONE(NULL);
    499  1.5.6.1   mjf 	fd_putfile(fd);
    500  1.5.6.1   mjf 	return error;
    501  1.5.6.1   mjf }
    502  1.5.6.1   mjf 
    503  1.5.6.1   mjf int
    504  1.5.6.1   mjf linux32_ioctl_socket(struct lwp *l, const struct linux32_sys_ioctl_args *uap, register_t *retval)
    505  1.5.6.1   mjf {
    506  1.5.6.1   mjf 	/* {
    507  1.5.6.1   mjf 		syscallarg(int) fd;
    508  1.5.6.1   mjf 		syscallarg(u_long) com;
    509  1.5.6.1   mjf 		syscallarg(void *) data;
    510  1.5.6.1   mjf 	} */
    511  1.5.6.1   mjf 	u_long com;
    512  1.5.6.1   mjf 	int error = 0, isdev = 0, dosys = 1;
    513  1.5.6.1   mjf 	struct netbsd32_ioctl_args ia;
    514  1.5.6.1   mjf 	file_t *fp;
    515  1.5.6.1   mjf 	struct vnode *vp;
    516  1.5.6.1   mjf 	int (*ioctlf)(file_t *, u_long, void *);
    517  1.5.6.1   mjf 	struct ioctl_pt pt;
    518  1.5.6.1   mjf 
    519  1.5.6.1   mjf 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
    520  1.5.6.1   mjf 		return (EBADF);
    521  1.5.6.1   mjf 
    522  1.5.6.1   mjf 	if (fp->f_type == DTYPE_VNODE) {
    523  1.5.6.1   mjf 		vp = (struct vnode *)fp->f_data;
    524  1.5.6.1   mjf 		isdev = vp->v_type == VCHR;
    525  1.5.6.1   mjf 	}
    526  1.5.6.1   mjf 
    527  1.5.6.1   mjf 	/*
    528  1.5.6.1   mjf 	 * Don't try to interpret socket ioctl calls that are done
    529  1.5.6.1   mjf 	 * on a device filedescriptor, just pass them through, to
    530  1.5.6.1   mjf 	 * emulate Linux behaviour. Use PTIOCLINUX so that the
    531  1.5.6.1   mjf 	 * device will only handle these if it's prepared to do
    532  1.5.6.1   mjf 	 * so, to avoid unexpected things from happening.
    533  1.5.6.1   mjf 	 */
    534  1.5.6.1   mjf 	if (isdev) {
    535  1.5.6.1   mjf 		dosys = 0;
    536  1.5.6.1   mjf 		ioctlf = fp->f_ops->fo_ioctl;
    537  1.5.6.1   mjf 		pt.com = SCARG(uap, com);
    538  1.5.6.1   mjf 		pt.data = (void *)NETBSD32PTR64(SCARG(uap, data));
    539  1.5.6.1   mjf 		error = ioctlf(fp, PTIOCLINUX, &pt);
    540  1.5.6.1   mjf 		/*
    541  1.5.6.1   mjf 		 * XXX hack: if the function returns EJUSTRETURN,
    542  1.5.6.1   mjf 		 * it has stuffed a sysctl return value in pt.data.
    543  1.5.6.1   mjf 		 */
    544  1.5.6.1   mjf 		if (error == EJUSTRETURN) {
    545  1.5.6.1   mjf 			retval[0] = (register_t)pt.data;
    546  1.5.6.1   mjf 			error = 0;
    547  1.5.6.1   mjf 		}
    548  1.5.6.1   mjf 		goto out;
    549  1.5.6.1   mjf 	}
    550  1.5.6.1   mjf 
    551  1.5.6.1   mjf 	com = SCARG(uap, com);
    552  1.5.6.1   mjf 	retval[0] = 0;
    553  1.5.6.1   mjf 
    554  1.5.6.1   mjf 	switch (com) {
    555  1.5.6.1   mjf 	case LINUX_SIOCGIFCONF:
    556  1.5.6.1   mjf 		SCARG(&ia, com) = OOSIOCGIFCONF32;
    557  1.5.6.1   mjf 		break;
    558  1.5.6.1   mjf 	case LINUX_SIOCGIFFLAGS:
    559  1.5.6.1   mjf 		SCARG(&ia, com) = OSIOCGIFFLAGS;
    560  1.5.6.1   mjf 		break;
    561  1.5.6.1   mjf 	case LINUX_SIOCSIFFLAGS:
    562  1.5.6.1   mjf 		SCARG(&ia, com) = OSIOCSIFFLAGS;
    563  1.5.6.1   mjf 		break;
    564  1.5.6.1   mjf 	case LINUX_SIOCGIFADDR:
    565  1.5.6.1   mjf 		SCARG(&ia, com) = OOSIOCGIFADDR;
    566  1.5.6.1   mjf 		break;
    567  1.5.6.1   mjf 	case LINUX_SIOCGIFDSTADDR:
    568  1.5.6.1   mjf 		SCARG(&ia, com) = OOSIOCGIFDSTADDR;
    569  1.5.6.1   mjf 		break;
    570  1.5.6.1   mjf 	case LINUX_SIOCGIFBRDADDR:
    571  1.5.6.1   mjf 		SCARG(&ia, com) = OOSIOCGIFBRDADDR;
    572  1.5.6.1   mjf 		break;
    573  1.5.6.1   mjf 	case LINUX_SIOCGIFNETMASK:
    574  1.5.6.1   mjf 		SCARG(&ia, com) = OOSIOCGIFNETMASK;
    575  1.5.6.1   mjf 		break;
    576  1.5.6.1   mjf 	case LINUX_SIOCADDMULTI:
    577  1.5.6.1   mjf 		SCARG(&ia, com) = OSIOCADDMULTI;
    578  1.5.6.1   mjf 		break;
    579  1.5.6.1   mjf 	case LINUX_SIOCDELMULTI:
    580  1.5.6.1   mjf 		SCARG(&ia, com) = OSIOCDELMULTI;
    581  1.5.6.1   mjf 		break;
    582  1.5.6.1   mjf 	case LINUX_SIOCGIFHWADDR:
    583  1.5.6.1   mjf 		error = linux32_getifhwaddr(l, retval, SCARG(uap, fd),
    584  1.5.6.1   mjf 		    SCARG_P32(uap, data));
    585  1.5.6.1   mjf 		dosys = 0;
    586  1.5.6.1   mjf 		break;
    587  1.5.6.1   mjf 	default:
    588  1.5.6.1   mjf 		error = EINVAL;
    589  1.5.6.1   mjf 	}
    590  1.5.6.1   mjf 
    591  1.5.6.1   mjf  out:
    592  1.5.6.1   mjf  	fd_putfile(SCARG(uap, fd));
    593  1.5.6.1   mjf 
    594  1.5.6.1   mjf 	if (error == 0 && dosys) {
    595  1.5.6.1   mjf 		SCARG(&ia, fd) = SCARG(uap, fd);
    596  1.5.6.1   mjf 		SCARG(&ia, data) = SCARG(uap, data);
    597  1.5.6.1   mjf 		error = netbsd32_ioctl(curlwp, &ia, retval);
    598  1.5.6.1   mjf 	}
    599  1.5.6.1   mjf 
    600  1.5.6.1   mjf 	return error;
    601  1.5.6.1   mjf }
    602