Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_ioctl.c revision 1.52.2.1
      1 /*	$NetBSD: netbsd32_ioctl.c,v 1.52.2.1 2011/02/08 16:19:47 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * handle ioctl conversions from netbsd32 -> 64-bit kernel
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: netbsd32_ioctl.c,v 1.52.2.1 2011/02/08 16:19:47 bouyer Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/filedesc.h>
     39 #include <sys/ioctl.h>
     40 #include <sys/file.h>
     41 #include <sys/proc.h>
     42 #include <sys/socketvar.h>
     43 #include <sys/audioio.h>
     44 #include <sys/disklabel.h>
     45 #include <sys/dkio.h>
     46 #include <sys/sockio.h>
     47 #include <sys/socket.h>
     48 #include <sys/ttycom.h>
     49 #include <sys/mount.h>
     50 #include <sys/syscallargs.h>
     51 #include <sys/ktrace.h>
     52 #include <sys/kmem.h>
     53 #include <sys/envsys.h>
     54 
     55 #ifdef __sparc__
     56 #include <dev/sun/fbio.h>
     57 #include <machine/openpromio.h>
     58 #endif
     59 
     60 #include <net/if.h>
     61 #include <net/route.h>
     62 
     63 #include <netinet/in.h>
     64 #include <netinet/in_var.h>
     65 #include <netinet/igmp.h>
     66 #include <netinet/igmp_var.h>
     67 #include <netinet/ip_mroute.h>
     68 
     69 #include <compat/sys/sockio.h>
     70 
     71 #include <compat/netbsd32/netbsd32.h>
     72 #include <compat/netbsd32/netbsd32_ioctl.h>
     73 #include <compat/netbsd32/netbsd32_syscallargs.h>
     74 
     75 #include <dev/vndvar.h>
     76 
     77 /* prototypes for the converters */
     78 static inline void netbsd32_to_partinfo(struct netbsd32_partinfo *,
     79 					  struct partinfo *, u_long);
     80 #if 0
     81 static inline void netbsd32_to_format_op(struct netbsd32_format_op *,
     82 					   struct format_op *, u_long);
     83 #endif
     84 static inline void netbsd32_to_oifreq(struct netbsd32_oifreq *, struct oifreq *,
     85 				       u_long cmd);
     86 static inline void netbsd32_to_ifreq(struct netbsd32_ifreq *, struct ifreq *,
     87 				       u_long cmd);
     88 static inline void netbsd32_to_if_addrprefreq(
     89     const struct netbsd32_if_addrprefreq *, struct if_addrprefreq *, u_long);
     90 static inline void netbsd32_to_ifconf(struct netbsd32_ifconf *,
     91 					struct ifconf *, u_long);
     92 static inline void netbsd32_to_ifmediareq(struct netbsd32_ifmediareq *,
     93 					    struct ifmediareq *, u_long);
     94 static inline void netbsd32_to_ifdrv(struct netbsd32_ifdrv *, struct ifdrv *,
     95 				       u_long);
     96 static inline void netbsd32_to_sioc_vif_req(struct netbsd32_sioc_vif_req *,
     97 					      struct sioc_vif_req *, u_long);
     98 static inline void netbsd32_to_sioc_sg_req(struct netbsd32_sioc_sg_req *,
     99 					     struct sioc_sg_req *, u_long);
    100 static inline void netbsd32_from_partinfo(struct partinfo *,
    101 					    struct netbsd32_partinfo *, u_long);
    102 #if 0
    103 static inline void netbsd32_from_format_op(struct format_op *,
    104 					     struct netbsd32_format_op *,
    105 					     u_long);
    106 #endif
    107 static inline void netbsd32_from_if_addrprefreq(const struct if_addrprefreq *,
    108 					struct netbsd32_if_addrprefreq *,
    109 					u_long);
    110 static inline void netbsd32_from_ifreq(struct ifreq *,
    111                                          struct netbsd32_ifreq *, u_long);
    112 static inline void netbsd32_from_oifreq(struct oifreq *,
    113                                          struct netbsd32_oifreq *, u_long);
    114 static inline void netbsd32_from_ifconf(struct ifconf *,
    115 					  struct netbsd32_ifconf *, u_long);
    116 static inline void netbsd32_from_ifmediareq(struct ifmediareq *,
    117 					      struct netbsd32_ifmediareq *,
    118 					      u_long);
    119 static inline void netbsd32_from_ifdrv(struct ifdrv *,
    120 					 struct netbsd32_ifdrv *, u_long);
    121 static inline void netbsd32_from_sioc_vif_req(struct sioc_vif_req *,
    122 						struct netbsd32_sioc_vif_req *,
    123 						u_long);
    124 static inline void netbsd32_from_sioc_sg_req(struct sioc_sg_req *,
    125 					       struct netbsd32_sioc_sg_req *,
    126 					       u_long);
    127 
    128 /* convert to/from different structures */
    129 
    130 static inline void
    131 netbsd32_to_partinfo(struct netbsd32_partinfo *s32p, struct partinfo *p, u_long cmd)
    132 {
    133 
    134 	p->disklab = (struct disklabel *)NETBSD32PTR64(s32p->disklab);
    135 	p->part = (struct partition *)NETBSD32PTR64(s32p->part);
    136 }
    137 
    138 #if 0
    139 static inline void
    140 netbsd32_to_format_op(struct netbsd32_format_op *s32p, struct format_op *p, u_long cmd)
    141 {
    142 
    143 	p->df_buf = (char *)NETBSD32PTR64(s32p->df_buf);
    144 	p->df_count = s32p->df_count;
    145 	p->df_startblk = s32p->df_startblk;
    146 	memcpy(p->df_reg, s32p->df_reg, sizeof(s32p->df_reg));
    147 }
    148 #endif
    149 
    150 static inline void
    151 netbsd32_to_ifreq(struct netbsd32_ifreq *s32p, struct ifreq *p, u_long cmd)
    152 {
    153 
    154 	memcpy(p, s32p, sizeof *s32p);
    155 	/*
    156 	 * XXX
    157 	 * struct ifreq says the same, but sometimes the ifr_data
    158 	 * union member needs to be converted to 64 bits... this
    159 	 * is very driver specific and so we ignore it for now..
    160 	 */
    161 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    162 		p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
    163 }
    164 
    165 static inline void
    166 netbsd32_to_oifreq(struct netbsd32_oifreq *s32p, struct oifreq *p, u_long cmd)
    167 {
    168 
    169 	memcpy(p, s32p, sizeof *s32p);
    170 	/*
    171 	 * XXX
    172 	 * struct ifreq says the same, but sometimes the ifr_data
    173 	 * union member needs to be converted to 64 bits... this
    174 	 * is very driver specific and so we ignore it for now..
    175 	 */
    176 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    177 		p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
    178 }
    179 
    180 static inline void
    181 netbsd32_to_if_addrprefreq(const struct netbsd32_if_addrprefreq *ifap32,
    182 	struct if_addrprefreq *ifap, u_long cmd)
    183 {
    184 	strlcpy(ifap->ifap_name, ifap32->ifap_name, sizeof(ifap->ifap_name));
    185 	ifap->ifap_preference = ifap32->ifap_preference;
    186 	memcpy(&ifap->ifap_addr, &ifap32->ifap_addr,
    187 	    max(ifap32->ifap_addr.ss_len, _SS_MAXSIZE));
    188 }
    189 
    190 static inline void
    191 netbsd32_to_ifconf(struct netbsd32_ifconf *s32p, struct ifconf *p, u_long cmd)
    192 {
    193 
    194 	p->ifc_len = s32p->ifc_len;
    195 	/* ifc_buf & ifc_req are the same size so this works */
    196 	p->ifc_buf = (void *)NETBSD32PTR64(s32p->ifc_buf);
    197 }
    198 
    199 static inline void
    200 netbsd32_to_ifmediareq(struct netbsd32_ifmediareq *s32p, struct ifmediareq *p, u_long cmd)
    201 {
    202 
    203 	memcpy(p, s32p, sizeof *s32p);
    204 	p->ifm_ulist = (int *)NETBSD32PTR64(s32p->ifm_ulist);
    205 }
    206 
    207 static inline void
    208 netbsd32_to_ifdrv(struct netbsd32_ifdrv *s32p, struct ifdrv *p, u_long cmd)
    209 {
    210 
    211 	memcpy(p, s32p, sizeof *s32p);
    212 	p->ifd_data = (void *)NETBSD32PTR64(s32p->ifd_data);
    213 }
    214 
    215 static inline void
    216 netbsd32_to_sioc_vif_req(struct netbsd32_sioc_vif_req *s32p, struct sioc_vif_req *p, u_long cmd)
    217 {
    218 
    219 	p->vifi = s32p->vifi;
    220 	p->icount = (u_long)s32p->icount;
    221 	p->ocount = (u_long)s32p->ocount;
    222 	p->ibytes = (u_long)s32p->ibytes;
    223 	p->obytes = (u_long)s32p->obytes;
    224 }
    225 
    226 static inline void
    227 netbsd32_to_sioc_sg_req(struct netbsd32_sioc_sg_req *s32p, struct sioc_sg_req *p, u_long cmd)
    228 {
    229 
    230 	p->src = s32p->src;
    231 	p->grp = s32p->grp;
    232 	p->pktcnt = (u_long)s32p->pktcnt;
    233 	p->bytecnt = (u_long)s32p->bytecnt;
    234 	p->wrong_if = (u_long)s32p->wrong_if;
    235 }
    236 
    237 static inline void
    238 netbsd32_to_vnd_ioctl(struct netbsd32_vnd_ioctl *s32p, struct vnd_ioctl *p, u_long cmd)
    239 {
    240 
    241 	p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
    242 	p->vnd_flags = s32p->vnd_flags;
    243 	p->vnd_geom = s32p->vnd_geom;
    244 	p->vnd_osize = s32p->vnd_osize;
    245 	p->vnd_size = s32p->vnd_size;
    246 }
    247 
    248 static inline void
    249 netbsd32_to_vnd_user(struct netbsd32_vnd_user *s32p, struct vnd_user *p, u_long cmd)
    250 {
    251 
    252 	p->vnu_unit = s32p->vnu_unit;
    253 	p->vnu_dev = s32p->vnu_dev;
    254 	p->vnu_ino = s32p->vnu_ino;
    255 }
    256 
    257 static inline void
    258 netbsd32_to_vnd_ioctl50(struct netbsd32_vnd_ioctl50 *s32p, struct vnd_ioctl50 *p, u_long cmd)
    259 {
    260 
    261 	p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
    262 	p->vnd_flags = s32p->vnd_flags;
    263 	p->vnd_geom = s32p->vnd_geom;
    264 	p->vnd_size = s32p->vnd_size;
    265 }
    266 
    267 static inline void
    268 netbsd32_to_plistref(struct netbsd32_plistref *s32p, struct plistref *p, u_long cmd)
    269 {
    270 
    271 	p->pref_plist = NETBSD32PTR64(s32p->pref_plist);
    272 	p->pref_len = s32p->pref_len;
    273 }
    274 
    275 static inline void
    276 netbsd32_to_u_long(netbsd32_u_long *s32p, u_long *p, u_long cmd)
    277 {
    278 
    279 	*p = (u_long)*s32p;
    280 }
    281 
    282 /*
    283  * handle ioctl conversions from 64-bit kernel -> netbsd32
    284  */
    285 
    286 static inline void
    287 netbsd32_from_partinfo(struct partinfo *p, struct netbsd32_partinfo *s32p, u_long cmd)
    288 {
    289 
    290 	NETBSD32PTR32(s32p->disklab, p->disklab);
    291 	NETBSD32PTR32(s32p->part, p->part);
    292 }
    293 
    294 #if 0
    295 static inline void
    296 netbsd32_from_format_op(struct format_op *p, struct netbsd32_format_op *s32p, u_long cmd)
    297 {
    298 
    299 /* filled in */
    300 #if 0
    301 	s32p->df_buf = (netbsd32_charp)p->df_buf;
    302 #endif
    303 	s32p->df_count = p->df_count;
    304 	s32p->df_startblk = p->df_startblk;
    305 	memcpy(s32p->df_reg, p->df_reg, sizeof(p->df_reg));
    306 }
    307 #endif
    308 
    309 static inline void
    310 netbsd32_from_ifreq(struct ifreq *p, struct netbsd32_ifreq *s32p, u_long cmd)
    311 {
    312 
    313 	/*
    314 	 * XXX
    315 	 * struct ifreq says the same, but sometimes the ifr_data
    316 	 * union member needs to be converted to 64 bits... this
    317 	 * is very driver specific and so we ignore it for now..
    318 	 */
    319 	memcpy(s32p, p, sizeof *s32p);
    320 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    321 		NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
    322 }
    323 
    324 static inline void
    325 netbsd32_from_oifreq(struct oifreq *p, struct netbsd32_oifreq *s32p, u_long cmd)
    326 {
    327 
    328 	/*
    329 	 * XXX
    330 	 * struct ifreq says the same, but sometimes the ifr_data
    331 	 * union member needs to be converted to 64 bits... this
    332 	 * is very driver specific and so we ignore it for now..
    333 	 */
    334 	memcpy(s32p, p, sizeof *s32p);
    335 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    336 		NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
    337 }
    338 
    339 static inline void
    340 netbsd32_from_if_addrprefreq(const struct if_addrprefreq *ifap,
    341 	struct netbsd32_if_addrprefreq *ifap32, u_long cmd)
    342 {
    343 	strlcpy(ifap32->ifap_name, ifap->ifap_name, sizeof(ifap32->ifap_name));
    344 	ifap32->ifap_preference = ifap->ifap_preference;
    345 	memcpy(&ifap32->ifap_addr, &ifap->ifap_addr,
    346 	    max(ifap->ifap_addr.ss_len, _SS_MAXSIZE));
    347 }
    348 
    349 static inline void
    350 netbsd32_from_ifconf(struct ifconf *p, struct netbsd32_ifconf *s32p, u_long cmd)
    351 {
    352 
    353 	s32p->ifc_len = p->ifc_len;
    354 	/* ifc_buf & ifc_req are the same size so this works */
    355 	NETBSD32PTR32(s32p->ifc_buf, p->ifc_buf);
    356 }
    357 
    358 static inline void
    359 netbsd32_from_ifmediareq(struct ifmediareq *p, struct netbsd32_ifmediareq *s32p, u_long cmd)
    360 {
    361 
    362 	memcpy(s32p, p, sizeof *p);
    363 /* filled in? */
    364 #if 0
    365 	s32p->ifm_ulist = (netbsd32_intp_t)p->ifm_ulist;
    366 #endif
    367 }
    368 
    369 static inline void
    370 netbsd32_from_ifdrv(struct ifdrv *p, struct netbsd32_ifdrv *s32p, u_long cmd)
    371 {
    372 
    373 	memcpy(s32p, p, sizeof *p);
    374 /* filled in? */
    375 #if 0
    376 	s32p->ifm_data = (netbsd32_u_longp_t)p->ifm_data;
    377 #endif
    378 }
    379 
    380 static inline void
    381 netbsd32_from_sioc_vif_req(struct sioc_vif_req *p, struct netbsd32_sioc_vif_req *s32p, u_long cmd)
    382 {
    383 
    384 	s32p->vifi = p->vifi;
    385 	s32p->icount = (netbsd32_u_long)p->icount;
    386 	s32p->ocount = (netbsd32_u_long)p->ocount;
    387 	s32p->ibytes = (netbsd32_u_long)p->ibytes;
    388 	s32p->obytes = (netbsd32_u_long)p->obytes;
    389 }
    390 
    391 static inline void
    392 netbsd32_from_sioc_sg_req(struct sioc_sg_req *p, struct netbsd32_sioc_sg_req *s32p, u_long cmd)
    393 {
    394 
    395 	s32p->src = p->src;
    396 	s32p->grp = p->grp;
    397 	s32p->pktcnt = (netbsd32_u_long)p->pktcnt;
    398 	s32p->bytecnt = (netbsd32_u_long)p->bytecnt;
    399 	s32p->wrong_if = (netbsd32_u_long)p->wrong_if;
    400 }
    401 
    402 static inline void
    403 netbsd32_from_vnd_ioctl(struct vnd_ioctl *p, struct netbsd32_vnd_ioctl *s32p, u_long cmd)
    404 {
    405 
    406 	s32p->vnd_flags = p->vnd_flags;
    407 	s32p->vnd_geom = p->vnd_geom;
    408 	s32p->vnd_osize = p->vnd_osize;
    409 	s32p->vnd_size = p->vnd_size;
    410 }
    411 
    412 static inline void
    413 netbsd32_from_vnd_user(struct vnd_user *p, struct netbsd32_vnd_user *s32p, u_long cmd)
    414 {
    415 
    416 	s32p->vnu_unit = p->vnu_unit;
    417 	s32p->vnu_dev = p->vnu_dev;
    418 	s32p->vnu_ino = p->vnu_ino;
    419 }
    420 
    421 static inline void
    422 netbsd32_from_vnd_ioctl50(struct vnd_ioctl50 *p, struct netbsd32_vnd_ioctl50 *s32p, u_long cmd)
    423 {
    424 
    425 	s32p->vnd_flags = p->vnd_flags;
    426 	s32p->vnd_geom = p->vnd_geom;
    427 	s32p->vnd_size = p->vnd_size;
    428 }
    429 
    430 static inline void
    431 netbsd32_from_plistref(struct plistref *p, struct netbsd32_plistref *s32p, u_long cmd)
    432 {
    433 
    434 	NETBSD32PTR32(s32p->pref_plist, p->pref_plist);
    435 	s32p->pref_len = p->pref_len;
    436 }
    437 
    438 static inline void
    439 netbsd32_from_u_long(u_long *p, netbsd32_u_long *s32p, u_long cmd)
    440 {
    441 
    442 	*s32p = (netbsd32_u_long)*p;
    443 }
    444 
    445 
    446 /*
    447  * main ioctl syscall.
    448  *
    449  * ok, here we are in the biggy.  we have to do fix ups depending
    450  * on the ioctl command before and afterwards.
    451  */
    452 int
    453 netbsd32_ioctl(struct lwp *l, const struct netbsd32_ioctl_args *uap, register_t *retval)
    454 {
    455 	/* {
    456 		syscallarg(int) fd;
    457 		syscallarg(netbsd32_u_long) com;
    458 		syscallarg(netbsd32_voidp) data;
    459 	} */
    460 	struct proc *p = l->l_proc;
    461 	struct file *fp;
    462 	struct filedesc *fdp;
    463 	u_long com;
    464 	int error = 0;
    465 	size_t size;
    466 	size_t alloc_size32, size32;
    467 	void *data, *memp = NULL;
    468 	void *data32, *memp32 = NULL;
    469 	unsigned int fd;
    470 	fdfile_t *ff;
    471 	int tmp;
    472 #define STK_PARAMS	128
    473 	u_long stkbuf[STK_PARAMS/sizeof(u_long)];
    474 	u_long stkbuf32[STK_PARAMS/sizeof(u_long)];
    475 
    476 	/*
    477 	 * we need to translate some commands (_IOW) before calling sys_ioctl,
    478 	 * some after (_IOR), and some both (_IOWR).
    479 	 */
    480 #if 0
    481 	{
    482 		const char * const dirs[8] = {
    483 		    "NONE!", "VOID", "OUT", "VOID|OUT!", "IN", "VOID|IN!",
    484 		    "INOUT", "VOID|IN|OUT!"
    485 		};
    486 
    487 		printf("netbsd32_ioctl(%d, %x, %x): "
    488 		    "%s group %c base %d len %d\n",
    489 		    SCARG(uap, fd), SCARG(uap, com), SCARG(uap, data).i32,
    490 		    dirs[((SCARG(uap, com) & IOC_DIRMASK)>>29)],
    491 		    IOCGROUP(SCARG(uap, com)), IOCBASECMD(SCARG(uap, com)),
    492 		    IOCPARM_LEN(SCARG(uap, com)));
    493 	}
    494 #endif
    495 
    496 	memp = NULL;
    497 	memp32 = NULL;
    498 	alloc_size32 = 0;
    499 	size32 = 0;
    500 	size = 0;
    501 
    502 	fdp = p->p_fd;
    503 	fd = SCARG(uap, fd);
    504 	if ((fp = fd_getfile(fd)) == NULL)
    505 		return (EBADF);
    506 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    507 		error = EBADF;
    508 		goto out;
    509 	}
    510 
    511 	ff = fdp->fd_dt->dt_ff[SCARG(uap, fd)];
    512 	switch (com = SCARG(uap, com)) {
    513 	case FIOCLEX:
    514 		ff->ff_exclose = true;
    515 		fdp->fd_exclose = true;
    516 		goto out;
    517 
    518 	case FIONCLEX:
    519 		ff->ff_exclose = false;
    520 		goto out;
    521 	}
    522 
    523 	/*
    524 	 * Interpret high order word to find amount of data to be
    525 	 * copied to/from the user's address space.
    526 	 */
    527 	size32 = IOCPARM_LEN(com);
    528 	alloc_size32 = size32;
    529 
    530 	/*
    531 	 * The disklabel is now padded to a multiple of 8 bytes however the old
    532 	 * disklabel on 32bit platforms wasn't.  This leaves a difference in
    533 	 * size of 4 bytes between the two but are otherwise identical.
    534 	 * To deal with this, we allocate enough space for the new disklabel
    535 	 * but only copyin/out the smaller amount.
    536 	 */
    537 	if (IOCGROUP(com) == 'd') {
    538 		u_long ncom = com ^ (DIOCGDINFO ^ DIOCGDINFO32);
    539 		switch (ncom) {
    540 		case DIOCGDINFO:
    541 		case DIOCWDINFO:
    542 		case DIOCSDINFO:
    543 		case DIOCGDEFLABEL:
    544 			com = ncom;
    545 			if (IOCPARM_LEN(DIOCGDINFO32) < IOCPARM_LEN(DIOCGDINFO))
    546 				alloc_size32 = IOCPARM_LEN(DIOCGDINFO);
    547 			break;
    548 		}
    549 	}
    550 	if (alloc_size32 > IOCPARM_MAX) {
    551 		error = ENOTTY;
    552 		goto out;
    553 	}
    554 	if (alloc_size32 > sizeof(stkbuf)) {
    555 		memp32 = kmem_alloc(alloc_size32, KM_SLEEP);
    556 		data32 = memp32;
    557 	} else
    558 		data32 = (void *)stkbuf32;
    559 	if ((com >> IOCPARM_SHIFT) == 0)  {
    560 		/* UNIX-style ioctl. */
    561 		data32 = SCARG_P32(uap, data);
    562 	} else {
    563 		if (com&IOC_IN) {
    564 			if (size32) {
    565 				error = copyin(SCARG_P32(uap, data), data32,
    566 				    size32);
    567 				if (error) {
    568 					goto out;
    569 				}
    570 				/*
    571 				 * The data between size and alloc_size has
    572 				 * not been overwritten.  It shouldn't matter
    573 				 * but let's clear that anyway.
    574 				 */
    575 				if (__predict_false(size32 < alloc_size32)) {
    576 					memset((char *)data32+size32, 0,
    577 					    alloc_size32 - size32);
    578 				}
    579 				ktrgenio(fd, UIO_WRITE, SCARG_P32(uap, data),
    580 				    size32, 0);
    581 			} else
    582 				*(void **)data32 = SCARG_P32(uap, data);
    583 		} else if ((com&IOC_OUT) && size32) {
    584 			/*
    585 			 * Zero the buffer so the user always
    586 			 * gets back something deterministic.
    587 			 */
    588 			memset(data32, 0, alloc_size32);
    589 		} else if (com&IOC_VOID) {
    590 			*(void **)data32 = SCARG_P32(uap, data);
    591 		}
    592 	}
    593 
    594 	/*
    595 	 * convert various structures, pointers, and other objects that
    596 	 * change size from 32 bit -> 64 bit, for all ioctl commands.
    597 	 */
    598 	switch (SCARG(uap, com)) {
    599 	case FIONBIO:
    600 		mutex_enter(&fp->f_lock);
    601 		if ((tmp = *(int *)data32) != 0)
    602 			fp->f_flag |= FNONBLOCK;
    603 		else
    604 			fp->f_flag &= ~FNONBLOCK;
    605 		mutex_exit(&fp->f_lock);
    606 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (void *)&tmp);
    607 		break;
    608 
    609 	case FIOASYNC:
    610 		mutex_enter(&fp->f_lock);
    611 		if ((tmp = *(int *)data32) != 0)
    612 			fp->f_flag |= FASYNC;
    613 		else
    614 			fp->f_flag &= ~FASYNC;
    615 		mutex_exit(&fp->f_lock);
    616 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (void *)&tmp);
    617 		break;
    618 
    619 	case AUDIO_WSEEK32:
    620 		IOCTL_CONV_TO(AUDIO_WSEEK, u_long);
    621 
    622 	case DIOCGPART32:
    623 		IOCTL_STRUCT_CONV_TO(DIOCGPART, partinfo);
    624 #if 0	/* not implemented by anything */
    625 	case DIOCRFORMAT32:
    626 		IOCTL_STRUCT_CONV_TO(DIOCRFORMAT, format_op);
    627 	case DIOCWFORMAT32:
    628 		IOCTL_STRUCT_CONV_TO(DIOCWFORMAT, format_op);
    629 #endif
    630 
    631 /*
    632  * only a few ifreq syscalls need conversion and those are
    633  * all driver specific... XXX
    634  */
    635 #if 0
    636 	case SIOCGADDRROM3232:
    637 		IOCTL_STRUCT_CONV_TO(SIOCGADDRROM32, ifreq);
    638 	case SIOCGCHIPID32:
    639 		IOCTL_STRUCT_CONV_TO(SIOCGCHIPID, ifreq);
    640 	case SIOCSIFADDR32:
    641 		IOCTL_STRUCT_CONV_TO(SIOCSIFADDR, ifreq);
    642 	case OSIOCGIFADDR32:
    643 		IOCTL_STRUCT_CONV_TO(OSIOCGIFADDR, ifreq);
    644 	case SIOCGIFADDR32:
    645 		IOCTL_STRUCT_CONV_TO(SIOCGIFADDR, ifreq);
    646 	case SIOCSIFDSTADDR32:
    647 		IOCTL_STRUCT_CONV_TO(SIOCSIFDSTADDR, ifreq);
    648 	case OSIOCGIFDSTADDR32:
    649 		IOCTL_STRUCT_CONV_TO(OSIOCGIFDSTADDR, ifreq);
    650 	case SIOCGIFDSTADDR32:
    651 		IOCTL_STRUCT_CONV_TO(SIOCGIFDSTADDR, ifreq);
    652 	case OSIOCGIFBRDADDR32:
    653 		IOCTL_STRUCT_CONV_TO(OSIOCGIFBRDADDR, ifreq);
    654 	case SIOCGIFBRDADDR32:
    655 		IOCTL_STRUCT_CONV_TO(SIOCGIFBRDADDR, ifreq);
    656 	case SIOCSIFBRDADDR32:
    657 		IOCTL_STRUCT_CONV_TO(SIOCSIFBRDADDR, ifreq);
    658 	case OSIOCGIFNETMASK32:
    659 		IOCTL_STRUCT_CONV_TO(OSIOCGIFNETMASK, ifreq);
    660 	case SIOCGIFNETMASK32:
    661 		IOCTL_STRUCT_CONV_TO(SIOCGIFNETMASK, ifreq);
    662 	case SIOCSIFNETMASK32:
    663 		IOCTL_STRUCT_CONV_TO(SIOCSIFNETMASK, ifreq);
    664 	case SIOCGIFMETRIC32:
    665 		IOCTL_STRUCT_CONV_TO(SIOCGIFMETRIC, ifreq);
    666 	case SIOCSIFMETRIC32:
    667 		IOCTL_STRUCT_CONV_TO(SIOCSIFMETRIC, ifreq);
    668 	case SIOCDIFADDR32:
    669 		IOCTL_STRUCT_CONV_TO(SIOCDIFADDR, ifreq);
    670 	case SIOCADDMULTI32:
    671 		IOCTL_STRUCT_CONV_TO(SIOCADDMULTI, ifreq);
    672 	case SIOCDELMULTI32:
    673 		IOCTL_STRUCT_CONV_TO(SIOCDELMULTI, ifreq);
    674 	case SIOCSIFMEDIA32:
    675 		IOCTL_STRUCT_CONV_TO(SIOCSIFMEDIA, ifreq);
    676 	case SIOCSIFMTU32:
    677 		IOCTL_STRUCT_CONV_TO(SIOCSIFMTU, ifreq);
    678 	case SIOCGIFMTU32:
    679 		IOCTL_STRUCT_CONV_TO(SIOCGIFMTU, ifreq);
    680 	case BIOCGETIF32:
    681 		IOCTL_STRUCT_CONV_TO(BIOCGETIF, ifreq);
    682 	case BIOCSETIF32:
    683 		IOCTL_STRUCT_CONV_TO(BIOCSETIF, ifreq);
    684 	case SIOCPHASE132:
    685 		IOCTL_STRUCT_CONV_TO(SIOCPHASE1, ifreq);
    686 	case SIOCPHASE232:
    687 		IOCTL_STRUCT_CONV_TO(SIOCPHASE2, ifreq);
    688 #endif
    689 
    690 	case OOSIOCGIFCONF32:
    691 		IOCTL_STRUCT_CONV_TO(OOSIOCGIFCONF, ifconf);
    692 	case OSIOCGIFCONF32:
    693 		IOCTL_STRUCT_CONV_TO(OSIOCGIFCONF, ifconf);
    694 	case SIOCGIFCONF32:
    695 		IOCTL_STRUCT_CONV_TO(SIOCGIFCONF, ifconf);
    696 
    697 	case SIOCGIFFLAGS32:
    698 		IOCTL_STRUCT_CONV_TO(SIOCGIFFLAGS, ifreq);
    699 	case SIOCSIFFLAGS32:
    700 		IOCTL_STRUCT_CONV_TO(SIOCSIFFLAGS, ifreq);
    701 
    702 	case SIOCGIFADDRPREF32:
    703 		IOCTL_STRUCT_CONV_TO(SIOCGIFADDRPREF, if_addrprefreq);
    704 	case SIOCSIFADDRPREF32:
    705 		IOCTL_STRUCT_CONV_TO(SIOCSIFADDRPREF, if_addrprefreq);
    706 
    707 
    708 	case OSIOCGIFFLAGS32:
    709 		IOCTL_STRUCT_CONV_TO(OSIOCGIFFLAGS, oifreq);
    710 	case OSIOCSIFFLAGS32:
    711 		IOCTL_STRUCT_CONV_TO(OSIOCSIFFLAGS, oifreq);
    712 
    713 	case SIOCGIFMEDIA32:
    714 		IOCTL_STRUCT_CONV_TO(SIOCGIFMEDIA, ifmediareq);
    715 
    716 	case SIOCSDRVSPEC32:
    717 		IOCTL_STRUCT_CONV_TO(SIOCSDRVSPEC, ifdrv);
    718 
    719 	case SIOCGETVIFCNT32:
    720 		IOCTL_STRUCT_CONV_TO(SIOCGETVIFCNT, sioc_vif_req);
    721 
    722 	case SIOCGETSGCNT32:
    723 		IOCTL_STRUCT_CONV_TO(SIOCGETSGCNT, sioc_sg_req);
    724 
    725 	case VNDIOCSET32:
    726 		IOCTL_STRUCT_CONV_TO(VNDIOCSET, vnd_ioctl);
    727 
    728 	case VNDIOCCLR32:
    729 		IOCTL_STRUCT_CONV_TO(VNDIOCCLR, vnd_ioctl);
    730 
    731 	case VNDIOCGET32:
    732 		IOCTL_STRUCT_CONV_TO(VNDIOCGET, vnd_user);
    733 
    734 	case VNDIOCSET5032:
    735 		IOCTL_STRUCT_CONV_TO(VNDIOCSET50, vnd_ioctl50);
    736 
    737 	case VNDIOCCLR5032:
    738 		IOCTL_STRUCT_CONV_TO(VNDIOCCLR50, vnd_ioctl50);
    739 
    740 	case ENVSYS_GETDICTIONARY32:
    741 		IOCTL_STRUCT_CONV_TO(ENVSYS_GETDICTIONARY, plistref);
    742 	case ENVSYS_SETDICTIONARY32:
    743 		IOCTL_STRUCT_CONV_TO(ENVSYS_SETDICTIONARY, plistref);
    744 	case ENVSYS_REMOVEPROPS32:
    745 		IOCTL_STRUCT_CONV_TO(ENVSYS_REMOVEPROPS, plistref);
    746 
    747 	default:
    748 #ifdef NETBSD32_MD_IOCTL
    749 		error = netbsd32_md_ioctl(fp, com, data32, l);
    750 #else
    751 		error = (*fp->f_ops->fo_ioctl)(fp, com, data32);
    752 #endif
    753 		break;
    754 	}
    755 
    756 	if (error == EPASSTHROUGH)
    757 		error = ENOTTY;
    758 
    759 	/*
    760 	 * Copy any data to user, size was
    761 	 * already set and checked above.
    762 	 */
    763 	if (error == 0 && (com&IOC_OUT) && size32) {
    764 		error = copyout(data32, SCARG_P32(uap, data), size32);
    765 		ktrgenio(fd, UIO_READ, SCARG_P32(uap, data),
    766 		    size32, error);
    767 	}
    768 
    769  out:
    770 	/* If we allocated data, free it here. */
    771 	if (memp32)
    772 		kmem_free(memp32, alloc_size32);
    773 	if (memp)
    774 		kmem_free(memp, size);
    775 	fd_putfile(fd);
    776 	return (error);
    777 }
    778