Home | History | Annotate | Line # | Download | only in common
linux32_socket.c revision 1.5.10.1
      1  1.5.10.1  yamt /*	$NetBSD: linux32_socket.c,v 1.5.10.1 2009/05/04 08:12:23 yamt 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.10.1  yamt __KERNEL_RCSID(0, "$NetBSD: linux32_socket.c,v 1.5.10.1 2009/05/04 08:12:23 yamt 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.10.1  yamt #include <sys/file.h>
     50  1.5.10.1  yamt #include <sys/vnode.h>
     51  1.5.10.1  yamt #include <sys/filedesc.h>
     52       1.1  manu 
     53       1.1  manu #include <machine/types.h>
     54       1.1  manu 
     55  1.5.10.1  yamt #include <net/if.h>
     56  1.5.10.1  yamt #include <net/if_dl.h>
     57  1.5.10.1  yamt #include <net/if_types.h>
     58  1.5.10.1  yamt #include <net/route.h>
     59  1.5.10.1  yamt 
     60  1.5.10.1  yamt #include <netinet/in.h>
     61  1.5.10.1  yamt #include <netinet/ip_mroute.h>
     62  1.5.10.1  yamt 
     63       1.1  manu #include <sys/syscallargs.h>
     64       1.1  manu 
     65       1.1  manu #include <compat/netbsd32/netbsd32.h>
     66  1.5.10.1  yamt #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.10.1  yamt #include <compat/sys/socket.h>
     71  1.5.10.1  yamt #include <compat/sys/sockio.h>
     72  1.5.10.1  yamt 
     73  1.5.10.1  yamt #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.10.1  yamt #include <compat/linux/common/linux_ioctl.h>
     80  1.5.10.1  yamt #include <compat/linux/common/linux_sockio.h>
     81  1.5.10.1  yamt #include <compat/linux/common/linux_ipc.h>
     82  1.5.10.1  yamt #include <compat/linux/common/linux_sem.h>
     83       1.1  manu #include <compat/linux/linux_syscallargs.h>
     84       1.1  manu 
     85       1.1  manu #include <compat/linux32/common/linux32_types.h>
     86       1.1  manu #include <compat/linux32/common/linux32_signal.h>
     87       1.1  manu #include <compat/linux32/common/linux32_machdep.h>
     88       1.1  manu #include <compat/linux32/common/linux32_sysctl.h>
     89       1.1  manu #include <compat/linux32/common/linux32_socketcall.h>
     90  1.5.10.1  yamt #include <compat/linux32/common/linux32_sockio.h>
     91  1.5.10.1  yamt #include <compat/linux32/common/linux32_ioctl.h>
     92       1.1  manu #include <compat/linux32/linux32_syscallargs.h>
     93       1.1  manu 
     94  1.5.10.1  yamt int linux32_getifconf(struct lwp *, register_t *, void *);
     95  1.5.10.1  yamt int linux32_getifhwaddr(struct lwp *, register_t *, u_int, void *);
     96  1.5.10.1  yamt 
     97       1.1  manu int
     98       1.5   dsl linux32_sys_socketpair(struct lwp *l, const struct linux32_sys_socketpair_args *uap, register_t *retval)
     99       1.1  manu {
    100       1.5   dsl 	/* {
    101       1.1  manu 		syscallarg(int) domain;
    102       1.1  manu 		syscallarg(int) type;
    103       1.1  manu 		syscallarg(int) protocol;
    104       1.1  manu 		syscallarg(netbsd32_intp) rsv;
    105       1.5   dsl 	} */
    106       1.1  manu 	struct linux_sys_socketpair_args ua;
    107       1.1  manu 
    108       1.1  manu 	NETBSD32TO64_UAP(domain);
    109       1.1  manu 	NETBSD32TO64_UAP(type);
    110       1.1  manu 	NETBSD32TO64_UAP(protocol);
    111       1.1  manu 	NETBSD32TOP_UAP(rsv, int)
    112       1.1  manu 
    113       1.1  manu 	return linux_sys_socketpair(l, &ua, retval);
    114       1.1  manu }
    115       1.1  manu 
    116       1.1  manu int
    117       1.5   dsl linux32_sys_sendto(struct lwp *l, const struct linux32_sys_sendto_args *uap, register_t *retval)
    118       1.1  manu {
    119       1.5   dsl 	/* {
    120       1.1  manu 		syscallarg(int) s;
    121       1.5   dsl 		syscallarg(netbsd32_voidp) msg;
    122       1.1  manu 		syscallarg(int) len;
    123       1.1  manu 		syscallarg(int) flags;
    124       1.1  manu 		syscallarg(netbsd32_osockaddrp_t) to;
    125       1.1  manu 		syscallarg(int) tolen;
    126       1.5   dsl 	} */
    127       1.1  manu 	struct linux_sys_sendto_args ua;
    128       1.1  manu 
    129       1.1  manu 	NETBSD32TO64_UAP(s);
    130       1.1  manu 	NETBSD32TOP_UAP(msg, void);
    131       1.1  manu 	NETBSD32TO64_UAP(len);
    132       1.1  manu 	NETBSD32TO64_UAP(flags);
    133       1.1  manu 	NETBSD32TOP_UAP(to, struct osockaddr);
    134       1.1  manu 	NETBSD32TO64_UAP(tolen);
    135       1.1  manu 
    136       1.1  manu 	return linux_sys_sendto(l, &ua, retval);
    137       1.1  manu }
    138       1.1  manu 
    139       1.1  manu 
    140       1.1  manu int
    141       1.5   dsl linux32_sys_recvfrom(struct lwp *l, const struct linux32_sys_recvfrom_args *uap, register_t *retval)
    142       1.1  manu {
    143       1.5   dsl 	/* {
    144       1.1  manu 		syscallarg(int) s;
    145       1.5   dsl 		syscallarg(netbsd32_voidp) buf;
    146       1.1  manu 		syscallarg(netbsd32_size_t) len;
    147       1.1  manu 		syscallarg(int) flags;
    148       1.1  manu 		syscallarg(netbsd32_osockaddrp_t) from;
    149       1.1  manu 		syscallarg(netbsd32_intp) fromlenaddr;
    150       1.5   dsl 	} */
    151       1.1  manu 	struct linux_sys_recvfrom_args ua;
    152       1.1  manu 
    153       1.1  manu 	NETBSD32TO64_UAP(s);
    154       1.1  manu 	NETBSD32TOP_UAP(buf, void);
    155       1.1  manu 	NETBSD32TO64_UAP(len);
    156       1.1  manu 	NETBSD32TO64_UAP(flags);
    157       1.1  manu 	NETBSD32TOP_UAP(from, struct osockaddr);
    158       1.1  manu 	NETBSD32TOP_UAP(fromlenaddr, unsigned int);
    159       1.1  manu 
    160       1.1  manu 	return linux_sys_recvfrom(l, &ua, retval);
    161       1.1  manu }
    162       1.1  manu 
    163       1.1  manu int
    164       1.5   dsl linux32_sys_setsockopt(struct lwp *l, const struct linux32_sys_setsockopt_args *uap, register_t *retval)
    165       1.1  manu {
    166       1.5   dsl 	/* {
    167       1.1  manu 		syscallarg(int) s;
    168       1.1  manu 		syscallarg(int) level;
    169       1.1  manu 		syscallarg(int) optname;
    170       1.1  manu 		syscallarg(netbsd32_voidp) optval;
    171       1.1  manu 		syscallarg(int) optlen;
    172       1.5   dsl 	} */
    173       1.1  manu 	struct linux_sys_setsockopt_args ua;
    174       1.1  manu 
    175       1.1  manu 	NETBSD32TO64_UAP(s);
    176       1.1  manu 	NETBSD32TO64_UAP(level);
    177       1.1  manu 	NETBSD32TO64_UAP(optname);
    178       1.1  manu 	NETBSD32TOP_UAP(optval, void);
    179       1.1  manu 	NETBSD32TO64_UAP(optlen);
    180       1.1  manu 
    181       1.1  manu 	return linux_sys_setsockopt(l, &ua, retval);
    182       1.1  manu }
    183       1.1  manu 
    184       1.1  manu 
    185       1.1  manu int
    186       1.5   dsl linux32_sys_getsockopt(struct lwp *l, const struct linux32_sys_getsockopt_args *uap, register_t *retval)
    187       1.1  manu {
    188       1.5   dsl 	/* {
    189       1.1  manu 		syscallarg(int) s;
    190       1.1  manu 		syscallarg(int) level;
    191       1.1  manu 		syscallarg(int) optname;
    192       1.1  manu 		syscallarg(netbsd32_voidp) optval;
    193       1.1  manu 		syscallarg(netbsd32_intp) optlen;
    194       1.5   dsl 	} */
    195       1.1  manu 	struct linux_sys_getsockopt_args ua;
    196       1.1  manu 
    197       1.1  manu 	NETBSD32TO64_UAP(s);
    198       1.1  manu 	NETBSD32TO64_UAP(level);
    199       1.1  manu 	NETBSD32TO64_UAP(optname);
    200       1.1  manu 	NETBSD32TOP_UAP(optval, void);
    201       1.1  manu 	NETBSD32TOP_UAP(optlen, int);
    202       1.1  manu 
    203       1.1  manu 	return linux_sys_getsockopt(l, &ua, retval);
    204       1.1  manu }
    205       1.1  manu 
    206       1.1  manu int
    207       1.5   dsl linux32_sys_socket(struct lwp *l, const struct linux32_sys_socket_args *uap, register_t *retval)
    208       1.1  manu {
    209       1.5   dsl 	/* {
    210       1.1  manu 		syscallarg(int) domain;
    211       1.1  manu 		syscallarg(int) type;
    212       1.1  manu 		syscallarg(int) protocol;
    213       1.5   dsl 	} */
    214       1.1  manu 	struct linux_sys_socket_args ua;
    215       1.1  manu 
    216       1.1  manu 	NETBSD32TO64_UAP(domain);
    217       1.1  manu 	NETBSD32TO64_UAP(type);
    218       1.1  manu 	NETBSD32TO64_UAP(protocol);
    219       1.1  manu 
    220       1.1  manu 	return linux_sys_socket(l, &ua, retval);
    221       1.1  manu }
    222       1.1  manu 
    223       1.1  manu int
    224       1.5   dsl linux32_sys_bind(struct lwp *l, const struct linux32_sys_bind_args *uap, register_t *retval)
    225       1.1  manu {
    226       1.5   dsl 	/* {
    227       1.1  manu 		syscallarg(int) s;
    228       1.1  manu 		syscallarg(netbsd32_osockaddrp_t) name;
    229       1.1  manu 		syscallarg(int) namelen;
    230       1.5   dsl 	} */
    231       1.1  manu 	struct linux_sys_bind_args ua;
    232       1.1  manu 
    233       1.1  manu 	NETBSD32TO64_UAP(s);
    234       1.1  manu 	NETBSD32TOP_UAP(name, struct osockaddr)
    235       1.1  manu 	NETBSD32TO64_UAP(namelen);
    236       1.1  manu 
    237       1.1  manu 	return linux_sys_bind(l, &ua, retval);
    238       1.1  manu }
    239       1.1  manu 
    240       1.1  manu int
    241       1.5   dsl linux32_sys_connect(struct lwp *l, const struct linux32_sys_connect_args *uap, register_t *retval)
    242       1.1  manu {
    243       1.5   dsl 	/* {
    244       1.1  manu 		syscallarg(int) s;
    245       1.1  manu 		syscallarg(netbsd32_osockaddrp_t) name;
    246       1.1  manu 		syscallarg(int) namelen;
    247       1.5   dsl 	} */
    248       1.1  manu 	struct linux_sys_connect_args ua;
    249       1.1  manu 
    250       1.1  manu 	NETBSD32TO64_UAP(s);
    251       1.1  manu 	NETBSD32TOP_UAP(name, struct osockaddr)
    252       1.1  manu 	NETBSD32TO64_UAP(namelen);
    253       1.1  manu 
    254       1.2  manu #ifdef DEBUG_LINUX
    255       1.1  manu 	printf("linux32_sys_connect: s = %d, name = %p, namelen = %d\n",
    256       1.1  manu 		SCARG(&ua, s), SCARG(&ua, name), SCARG(&ua, namelen));
    257       1.2  manu #endif
    258       1.1  manu 
    259       1.1  manu 	return linux_sys_connect(l, &ua, retval);
    260       1.1  manu }
    261       1.1  manu 
    262       1.1  manu int
    263       1.5   dsl linux32_sys_accept(struct lwp *l, const struct linux32_sys_accept_args *uap, register_t *retval)
    264       1.1  manu {
    265       1.5   dsl 	/* {
    266       1.1  manu 		syscallarg(int) s;
    267       1.1  manu 		syscallarg(netbsd32_osockaddrp_t) name;
    268       1.1  manu 		syscallarg(netbsd32_intp) anamelen;
    269       1.5   dsl 	} */
    270       1.1  manu 	struct linux_sys_accept_args ua;
    271       1.1  manu 
    272       1.1  manu 	NETBSD32TO64_UAP(s);
    273       1.1  manu 	NETBSD32TOP_UAP(name, struct osockaddr)
    274       1.1  manu 	NETBSD32TOP_UAP(anamelen, int);
    275       1.1  manu 
    276       1.1  manu 	return linux_sys_accept(l, &ua, retval);
    277       1.1  manu }
    278       1.1  manu 
    279       1.1  manu int
    280       1.5   dsl linux32_sys_getpeername(struct lwp *l, const struct linux32_sys_getpeername_args *uap, register_t *retval)
    281       1.1  manu {
    282       1.5   dsl 	/* {
    283       1.1  manu 		syscallarg(int) fdes;
    284       1.1  manu 		syscallarg(netbsd32_sockaddrp_t) asa;
    285       1.1  manu 		syscallarg(netbsd32_intp) alen;
    286       1.5   dsl 	} */
    287       1.1  manu 	struct linux_sys_getpeername_args ua;
    288       1.1  manu 
    289       1.1  manu 	NETBSD32TO64_UAP(fdes);
    290       1.1  manu 	NETBSD32TOP_UAP(asa, struct sockaddr)
    291       1.1  manu 	NETBSD32TOP_UAP(alen, int);
    292       1.1  manu 
    293       1.1  manu 	return linux_sys_getpeername(l, &ua, retval);
    294       1.1  manu }
    295       1.1  manu 
    296       1.1  manu int
    297       1.5   dsl linux32_sys_getsockname(struct lwp *l, const struct linux32_sys_getsockname_args *uap, register_t *retval)
    298       1.1  manu {
    299       1.5   dsl 	/* {
    300       1.1  manu 		syscallarg(int) fdec;
    301       1.1  manu 		syscallarg(netbsd32_charp) asa;
    302       1.1  manu 		syscallarg(netbsd32_intp) alen;
    303       1.5   dsl 	} */
    304       1.1  manu 	struct linux_sys_getsockname_args ua;
    305       1.1  manu 
    306       1.1  manu 	NETBSD32TO64_UAP(fdec);
    307       1.1  manu 	NETBSD32TOP_UAP(asa, char)
    308       1.1  manu 	NETBSD32TOP_UAP(alen, int);
    309       1.1  manu 
    310       1.1  manu 	return linux_sys_getsockname(l, &ua, retval);
    311       1.1  manu }
    312       1.1  manu 
    313       1.1  manu int
    314       1.5   dsl linux32_sys_sendmsg(struct lwp *l, const struct linux32_sys_sendmsg_args *uap, register_t *retval)
    315       1.1  manu {
    316       1.5   dsl 	/* {
    317       1.1  manu 		syscallarg(int) s;
    318       1.1  manu 		syscallarg(netbsd32_msghdrp_t) msg;
    319       1.1  manu 		syscallarg(int) flags;
    320       1.5   dsl 	} */
    321       1.1  manu 	struct linux_sys_sendmsg_args ua;
    322       1.1  manu 
    323       1.1  manu 	NETBSD32TO64_UAP(s);
    324       1.1  manu 	NETBSD32TOP_UAP(msg, struct msghdr);
    325       1.1  manu 	NETBSD32TO64_UAP(flags);
    326       1.1  manu 
    327       1.1  manu 	return linux_sys_sendmsg(l, &ua, retval);
    328       1.1  manu }
    329       1.1  manu 
    330       1.1  manu int
    331       1.5   dsl linux32_sys_recvmsg(struct lwp *l, const struct linux32_sys_recvmsg_args *uap, register_t *retval)
    332       1.1  manu {
    333       1.5   dsl 	/* {
    334       1.1  manu 		syscallarg(int) s;
    335       1.1  manu 		syscallarg(netbsd32_msghdrp_t) msg;
    336       1.1  manu 		syscallarg(int) flags;
    337       1.5   dsl 	} */
    338       1.1  manu 	struct linux_sys_recvmsg_args ua;
    339       1.1  manu 
    340       1.1  manu 	NETBSD32TO64_UAP(s);
    341       1.1  manu 	NETBSD32TOP_UAP(msg, struct msghdr);
    342       1.1  manu 	NETBSD32TO64_UAP(flags);
    343       1.1  manu 
    344       1.1  manu 	return linux_sys_recvmsg(l, &ua, retval);
    345       1.1  manu }
    346       1.1  manu 
    347       1.1  manu int
    348       1.5   dsl linux32_sys_send(struct lwp *l, const struct linux32_sys_send_args *uap, register_t *retval)
    349       1.1  manu {
    350       1.5   dsl 	/* {
    351       1.1  manu 		syscallarg(int) s;
    352       1.1  manu 		syscallarg(netbsd32_voidp) buf;
    353       1.1  manu 		syscallarg(int) len;
    354       1.1  manu 		syscallarg(int) flags;
    355       1.5   dsl 	} */
    356       1.1  manu 	struct sys_sendto_args ua;
    357       1.1  manu 
    358       1.1  manu 	NETBSD32TO64_UAP(s);
    359       1.1  manu 	NETBSD32TOP_UAP(buf, void);
    360       1.1  manu 	NETBSD32TO64_UAP(len);
    361       1.1  manu 	NETBSD32TO64_UAP(flags);
    362       1.1  manu 	SCARG(&ua, to) = NULL;
    363       1.1  manu 	SCARG(&ua, tolen) = 0;
    364       1.1  manu 
    365       1.1  manu 	return sys_sendto(l, &ua, retval);
    366       1.1  manu }
    367       1.1  manu 
    368       1.1  manu int
    369       1.5   dsl linux32_sys_recv(struct lwp *l, const struct linux32_sys_recv_args *uap, register_t *retval)
    370       1.1  manu {
    371       1.5   dsl 	/* {
    372       1.1  manu 		syscallarg(int) s;
    373       1.1  manu 		syscallarg(netbsd32_voidp) buf;
    374       1.1  manu 		syscallarg(int) len;
    375       1.1  manu 		syscallarg(int) flags;
    376       1.5   dsl 	} */
    377       1.1  manu 	struct sys_recvfrom_args ua;
    378       1.1  manu 
    379       1.1  manu 	NETBSD32TO64_UAP(s);
    380       1.1  manu 	NETBSD32TOP_UAP(buf, void);
    381       1.1  manu 	NETBSD32TO64_UAP(len);
    382       1.1  manu 	NETBSD32TO64_UAP(flags);
    383       1.1  manu 	SCARG(&ua, from) = NULL;
    384       1.1  manu 	SCARG(&ua, fromlenaddr) = NULL;
    385       1.1  manu 
    386       1.1  manu 	return sys_recvfrom(l, &ua, retval);
    387       1.1  manu }
    388  1.5.10.1  yamt 
    389  1.5.10.1  yamt int
    390  1.5.10.1  yamt linux32_getifconf(struct lwp *l, register_t *retval, void *data)
    391  1.5.10.1  yamt {
    392  1.5.10.1  yamt 	struct linux32_ifreq ifr, *ifrp;
    393  1.5.10.1  yamt 	struct netbsd32_ifconf *ifc = data;
    394  1.5.10.1  yamt 	struct ifnet *ifp;
    395  1.5.10.1  yamt 	struct ifaddr *ifa;
    396  1.5.10.1  yamt 	struct sockaddr *sa;
    397  1.5.10.1  yamt 	struct osockaddr *osa;
    398  1.5.10.1  yamt 	int space, error = 0;
    399  1.5.10.1  yamt 	const int sz = (int)sizeof(ifr);
    400  1.5.10.1  yamt 
    401  1.5.10.1  yamt 	ifrp = (struct linux32_ifreq *)NETBSD32PTR64(ifc->ifc_req);
    402  1.5.10.1  yamt 	if (ifrp == NULL)
    403  1.5.10.1  yamt 		space = 0;
    404  1.5.10.1  yamt 	else
    405  1.5.10.1  yamt 		space = ifc->ifc_len;
    406  1.5.10.1  yamt 
    407  1.5.10.1  yamt 	IFNET_FOREACH(ifp) {
    408  1.5.10.1  yamt 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
    409  1.5.10.1  yamt 		    sizeof(ifr.ifr_name));
    410  1.5.10.1  yamt 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
    411  1.5.10.1  yamt 			return ENAMETOOLONG;
    412  1.5.10.1  yamt 		if (IFADDR_EMPTY(ifp))
    413  1.5.10.1  yamt 			continue;
    414  1.5.10.1  yamt 		IFADDR_FOREACH(ifa, ifp) {
    415  1.5.10.1  yamt 			sa = ifa->ifa_addr;
    416  1.5.10.1  yamt 			if (sa->sa_family != AF_INET ||
    417  1.5.10.1  yamt 			    sa->sa_len > sizeof(*osa))
    418  1.5.10.1  yamt 				continue;
    419  1.5.10.1  yamt 			memcpy(&ifr.ifr_addr, sa, sa->sa_len);
    420  1.5.10.1  yamt 			osa = (struct osockaddr *)&ifr.ifr_addr;
    421  1.5.10.1  yamt 			osa->sa_family = sa->sa_family;
    422  1.5.10.1  yamt 			if (space >= sz) {
    423  1.5.10.1  yamt 				error = copyout(&ifr, ifrp, sz);
    424  1.5.10.1  yamt 				if (error != 0)
    425  1.5.10.1  yamt 					return error;
    426  1.5.10.1  yamt 				ifrp++;
    427  1.5.10.1  yamt 			}
    428  1.5.10.1  yamt 			space -= sz;
    429  1.5.10.1  yamt 		}
    430  1.5.10.1  yamt 	}
    431  1.5.10.1  yamt 
    432  1.5.10.1  yamt 	if (ifrp != NULL)
    433  1.5.10.1  yamt 		ifc->ifc_len -= space;
    434  1.5.10.1  yamt 	else
    435  1.5.10.1  yamt 		ifc->ifc_len = -space;
    436  1.5.10.1  yamt 
    437  1.5.10.1  yamt 	return 0;
    438  1.5.10.1  yamt }
    439  1.5.10.1  yamt 
    440  1.5.10.1  yamt int
    441  1.5.10.1  yamt linux32_getifhwaddr(struct lwp *l, register_t *retval, u_int fd,
    442  1.5.10.1  yamt     void *data)
    443  1.5.10.1  yamt {
    444  1.5.10.1  yamt 	struct linux32_ifreq lreq;
    445  1.5.10.1  yamt 	file_t *fp;
    446  1.5.10.1  yamt 	struct ifaddr *ifa;
    447  1.5.10.1  yamt 	struct ifnet *ifp;
    448  1.5.10.1  yamt 	struct sockaddr_dl *sadl;
    449  1.5.10.1  yamt 	int error, found;
    450  1.5.10.1  yamt 	int index, ifnum;
    451  1.5.10.1  yamt 
    452  1.5.10.1  yamt 	/*
    453  1.5.10.1  yamt 	 * We can't emulate this ioctl by calling sys_ioctl() to run
    454  1.5.10.1  yamt 	 * SIOCGIFCONF, because the user buffer is not of the right
    455  1.5.10.1  yamt 	 * type to take those results.  We can't use kernel buffers to
    456  1.5.10.1  yamt 	 * receive the results, as the implementation of sys_ioctl()
    457  1.5.10.1  yamt 	 * and ifconf() [which implements SIOCGIFCONF] use
    458  1.5.10.1  yamt 	 * copyin()/copyout() which will fail on kernel addresses.
    459  1.5.10.1  yamt 	 *
    460  1.5.10.1  yamt 	 * So, we must duplicate code from sys_ioctl() and ifconf().  Ugh.
    461  1.5.10.1  yamt 	 */
    462  1.5.10.1  yamt 
    463  1.5.10.1  yamt 	if ((fp = fd_getfile(fd)) == NULL)
    464  1.5.10.1  yamt 		return (EBADF);
    465  1.5.10.1  yamt 
    466  1.5.10.1  yamt 	KERNEL_LOCK(1, NULL);
    467  1.5.10.1  yamt 
    468  1.5.10.1  yamt 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    469  1.5.10.1  yamt 		error = EBADF;
    470  1.5.10.1  yamt 		goto out;
    471  1.5.10.1  yamt 	}
    472  1.5.10.1  yamt 
    473  1.5.10.1  yamt 	error = copyin(data, &lreq, sizeof(lreq));
    474  1.5.10.1  yamt 	if (error)
    475  1.5.10.1  yamt 		goto out;
    476  1.5.10.1  yamt 	lreq.ifr_name[LINUX32_IFNAMSIZ-1] = '\0';		/* just in case */
    477  1.5.10.1  yamt 
    478  1.5.10.1  yamt 	/*
    479  1.5.10.1  yamt 	 * Try real interface name first, then fake "ethX"
    480  1.5.10.1  yamt 	 */
    481  1.5.10.1  yamt 	found = 0;
    482  1.5.10.1  yamt 	IFNET_FOREACH(ifp) {
    483  1.5.10.1  yamt 		if (found)
    484  1.5.10.1  yamt 			break;
    485  1.5.10.1  yamt 		if (strcmp(lreq.ifr_name, ifp->if_xname))
    486  1.5.10.1  yamt 			/* not this interface */
    487  1.5.10.1  yamt 			continue;
    488  1.5.10.1  yamt 		found=1;
    489  1.5.10.1  yamt 		if (IFADDR_EMPTY(ifp)) {
    490  1.5.10.1  yamt 			error = ENODEV;
    491  1.5.10.1  yamt 			goto out;
    492  1.5.10.1  yamt 		}
    493  1.5.10.1  yamt 		IFADDR_FOREACH(ifa, ifp) {
    494  1.5.10.1  yamt 			sadl = satosdl(ifa->ifa_addr);
    495  1.5.10.1  yamt 			/* only return ethernet addresses */
    496  1.5.10.1  yamt 			/* XXX what about FDDI, etc. ? */
    497  1.5.10.1  yamt 			if (sadl->sdl_family != AF_LINK ||
    498  1.5.10.1  yamt 			    sadl->sdl_type != IFT_ETHER)
    499  1.5.10.1  yamt 				continue;
    500  1.5.10.1  yamt 			memcpy(&lreq.ifr_hwaddr.sa_data, CLLADDR(sadl),
    501  1.5.10.1  yamt 			       MIN(sadl->sdl_alen,
    502  1.5.10.1  yamt 				   sizeof(lreq.ifr_hwaddr.sa_data)));
    503  1.5.10.1  yamt 			lreq.ifr_hwaddr.sa_family =
    504  1.5.10.1  yamt 				sadl->sdl_family;
    505  1.5.10.1  yamt 			error = copyout(&lreq, data, sizeof(lreq));
    506  1.5.10.1  yamt 			goto out;
    507  1.5.10.1  yamt 		}
    508  1.5.10.1  yamt 	}
    509  1.5.10.1  yamt 
    510  1.5.10.1  yamt 	if (strncmp(lreq.ifr_name, "eth", 3) == 0) {
    511  1.5.10.1  yamt 		for (ifnum = 0, index = 3;
    512  1.5.10.1  yamt 		     lreq.ifr_name[index] != '\0' && index < LINUX32_IFNAMSIZ;
    513  1.5.10.1  yamt 		     index++) {
    514  1.5.10.1  yamt 			ifnum *= 10;
    515  1.5.10.1  yamt 			ifnum += lreq.ifr_name[index] - '0';
    516  1.5.10.1  yamt 		}
    517  1.5.10.1  yamt 
    518  1.5.10.1  yamt 		error = EINVAL;			/* in case we don't find one */
    519  1.5.10.1  yamt 		found = 0;
    520  1.5.10.1  yamt 		IFNET_FOREACH(ifp) {
    521  1.5.10.1  yamt 			if (found)
    522  1.5.10.1  yamt 				break;
    523  1.5.10.1  yamt 			memcpy(lreq.ifr_name, ifp->if_xname,
    524  1.5.10.1  yamt 			       MIN(LINUX32_IFNAMSIZ, IFNAMSIZ));
    525  1.5.10.1  yamt 			IFADDR_FOREACH(ifa, ifp) {
    526  1.5.10.1  yamt 				sadl = satosdl(ifa->ifa_addr);
    527  1.5.10.1  yamt 				/* only return ethernet addresses */
    528  1.5.10.1  yamt 				/* XXX what about FDDI, etc. ? */
    529  1.5.10.1  yamt 				if (sadl->sdl_family != AF_LINK ||
    530  1.5.10.1  yamt 				    sadl->sdl_type != IFT_ETHER)
    531  1.5.10.1  yamt 					continue;
    532  1.5.10.1  yamt 				if (ifnum--)
    533  1.5.10.1  yamt 					/* not the reqested iface */
    534  1.5.10.1  yamt 					continue;
    535  1.5.10.1  yamt 				memcpy(&lreq.ifr_hwaddr.sa_data,
    536  1.5.10.1  yamt 				       CLLADDR(sadl),
    537  1.5.10.1  yamt 				       MIN(sadl->sdl_alen,
    538  1.5.10.1  yamt 					   sizeof(lreq.ifr_hwaddr.sa_data)));
    539  1.5.10.1  yamt 				lreq.ifr_hwaddr.sa_family =
    540  1.5.10.1  yamt 					sadl->sdl_family;
    541  1.5.10.1  yamt 				error = copyout(&lreq, data, sizeof(lreq));
    542  1.5.10.1  yamt 				found = 1;
    543  1.5.10.1  yamt 				break;
    544  1.5.10.1  yamt 			}
    545  1.5.10.1  yamt 		}
    546  1.5.10.1  yamt 	} else {
    547  1.5.10.1  yamt 		/* unknown interface, not even an "eth*" name */
    548  1.5.10.1  yamt 		error = ENODEV;
    549  1.5.10.1  yamt 	}
    550  1.5.10.1  yamt 
    551  1.5.10.1  yamt out:
    552  1.5.10.1  yamt 	KERNEL_UNLOCK_ONE(NULL);
    553  1.5.10.1  yamt 	fd_putfile(fd);
    554  1.5.10.1  yamt 	return error;
    555  1.5.10.1  yamt }
    556  1.5.10.1  yamt 
    557  1.5.10.1  yamt int
    558  1.5.10.1  yamt linux32_ioctl_socket(struct lwp *l, const struct linux32_sys_ioctl_args *uap, register_t *retval)
    559  1.5.10.1  yamt {
    560  1.5.10.1  yamt 	/* {
    561  1.5.10.1  yamt 		syscallarg(int) fd;
    562  1.5.10.1  yamt 		syscallarg(u_long) com;
    563  1.5.10.1  yamt 		syscallarg(void *) data;
    564  1.5.10.1  yamt 	} */
    565  1.5.10.1  yamt 	u_long com;
    566  1.5.10.1  yamt 	int error = 0, isdev = 0, dosys = 1;
    567  1.5.10.1  yamt 	struct netbsd32_ioctl_args ia;
    568  1.5.10.1  yamt 	file_t *fp;
    569  1.5.10.1  yamt 	struct vnode *vp;
    570  1.5.10.1  yamt 	int (*ioctlf)(file_t *, u_long, void *);
    571  1.5.10.1  yamt 	struct ioctl_pt pt;
    572  1.5.10.1  yamt 
    573  1.5.10.1  yamt 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
    574  1.5.10.1  yamt 		return (EBADF);
    575  1.5.10.1  yamt 
    576  1.5.10.1  yamt 	if (fp->f_type == DTYPE_VNODE) {
    577  1.5.10.1  yamt 		vp = (struct vnode *)fp->f_data;
    578  1.5.10.1  yamt 		isdev = vp->v_type == VCHR;
    579  1.5.10.1  yamt 	}
    580  1.5.10.1  yamt 
    581  1.5.10.1  yamt 	/*
    582  1.5.10.1  yamt 	 * Don't try to interpret socket ioctl calls that are done
    583  1.5.10.1  yamt 	 * on a device filedescriptor, just pass them through, to
    584  1.5.10.1  yamt 	 * emulate Linux behaviour. Use PTIOCLINUX so that the
    585  1.5.10.1  yamt 	 * device will only handle these if it's prepared to do
    586  1.5.10.1  yamt 	 * so, to avoid unexpected things from happening.
    587  1.5.10.1  yamt 	 */
    588  1.5.10.1  yamt 	if (isdev) {
    589  1.5.10.1  yamt 		dosys = 0;
    590  1.5.10.1  yamt 		ioctlf = fp->f_ops->fo_ioctl;
    591  1.5.10.1  yamt 		pt.com = SCARG(uap, com);
    592  1.5.10.1  yamt 		pt.data = (void *)NETBSD32PTR64(SCARG(uap, data));
    593  1.5.10.1  yamt 		error = ioctlf(fp, PTIOCLINUX, &pt);
    594  1.5.10.1  yamt 		/*
    595  1.5.10.1  yamt 		 * XXX hack: if the function returns EJUSTRETURN,
    596  1.5.10.1  yamt 		 * it has stuffed a sysctl return value in pt.data.
    597  1.5.10.1  yamt 		 */
    598  1.5.10.1  yamt 		if (error == EJUSTRETURN) {
    599  1.5.10.1  yamt 			retval[0] = (register_t)pt.data;
    600  1.5.10.1  yamt 			error = 0;
    601  1.5.10.1  yamt 		}
    602  1.5.10.1  yamt 		goto out;
    603  1.5.10.1  yamt 	}
    604  1.5.10.1  yamt 
    605  1.5.10.1  yamt 	com = SCARG(uap, com);
    606  1.5.10.1  yamt 	retval[0] = 0;
    607  1.5.10.1  yamt 
    608  1.5.10.1  yamt 	switch (com) {
    609  1.5.10.1  yamt 	case LINUX_SIOCGIFCONF:
    610  1.5.10.1  yamt 		error = linux32_getifconf(l, retval, SCARG_P32(uap, data));
    611  1.5.10.1  yamt 		dosys = 0;
    612  1.5.10.1  yamt 		break;
    613  1.5.10.1  yamt 	case LINUX_SIOCGIFFLAGS:
    614  1.5.10.1  yamt 		SCARG(&ia, com) = OSIOCGIFFLAGS;
    615  1.5.10.1  yamt 		break;
    616  1.5.10.1  yamt 	case LINUX_SIOCSIFFLAGS:
    617  1.5.10.1  yamt 		SCARG(&ia, com) = OSIOCSIFFLAGS;
    618  1.5.10.1  yamt 		break;
    619  1.5.10.1  yamt 	case LINUX_SIOCGIFADDR:
    620  1.5.10.1  yamt 		SCARG(&ia, com) = OOSIOCGIFADDR;
    621  1.5.10.1  yamt 		break;
    622  1.5.10.1  yamt 	case LINUX_SIOCGIFDSTADDR:
    623  1.5.10.1  yamt 		SCARG(&ia, com) = OOSIOCGIFDSTADDR;
    624  1.5.10.1  yamt 		break;
    625  1.5.10.1  yamt 	case LINUX_SIOCGIFBRDADDR:
    626  1.5.10.1  yamt 		SCARG(&ia, com) = OOSIOCGIFBRDADDR;
    627  1.5.10.1  yamt 		break;
    628  1.5.10.1  yamt 	case LINUX_SIOCGIFNETMASK:
    629  1.5.10.1  yamt 		SCARG(&ia, com) = OOSIOCGIFNETMASK;
    630  1.5.10.1  yamt 		break;
    631  1.5.10.1  yamt 	case LINUX_SIOCADDMULTI:
    632  1.5.10.1  yamt 		SCARG(&ia, com) = OSIOCADDMULTI;
    633  1.5.10.1  yamt 		break;
    634  1.5.10.1  yamt 	case LINUX_SIOCDELMULTI:
    635  1.5.10.1  yamt 		SCARG(&ia, com) = OSIOCDELMULTI;
    636  1.5.10.1  yamt 		break;
    637  1.5.10.1  yamt 	case LINUX_SIOCGIFHWADDR:
    638  1.5.10.1  yamt 		error = linux32_getifhwaddr(l, retval, SCARG(uap, fd),
    639  1.5.10.1  yamt 		    SCARG_P32(uap, data));
    640  1.5.10.1  yamt 		dosys = 0;
    641  1.5.10.1  yamt 		break;
    642  1.5.10.1  yamt 	default:
    643  1.5.10.1  yamt 		error = EINVAL;
    644  1.5.10.1  yamt 	}
    645  1.5.10.1  yamt 
    646  1.5.10.1  yamt  out:
    647  1.5.10.1  yamt  	fd_putfile(SCARG(uap, fd));
    648  1.5.10.1  yamt 
    649  1.5.10.1  yamt 	if (error == 0 && dosys) {
    650  1.5.10.1  yamt 		SCARG(&ia, fd) = SCARG(uap, fd);
    651  1.5.10.1  yamt 		SCARG(&ia, data) = SCARG(uap, data);
    652  1.5.10.1  yamt 		error = netbsd32_ioctl(curlwp, &ia, retval);
    653  1.5.10.1  yamt 	}
    654  1.5.10.1  yamt 
    655  1.5.10.1  yamt 	return error;
    656  1.5.10.1  yamt }
    657