Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_ioctl.c revision 1.89.8.3
      1 /*	$NetBSD: netbsd32_ioctl.c,v 1.89.8.3 2020/02/13 19:34:29 martin 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.89.8.3 2020/02/13 19:34:29 martin Exp $");
     35 
     36 #if defined(_KERNEL_OPT)
     37 #include "opt_ntp.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/filedesc.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/file.h>
     45 #include <sys/proc.h>
     46 #include <sys/socketvar.h>
     47 #include <sys/audioio.h>
     48 #include <sys/disklabel.h>
     49 #include <sys/dkio.h>
     50 #include <sys/ataio.h>
     51 #include <sys/sockio.h>
     52 #include <sys/socket.h>
     53 #include <sys/ttycom.h>
     54 #include <sys/mount.h>
     55 #include <sys/syscallargs.h>
     56 #include <sys/ktrace.h>
     57 #include <sys/kmem.h>
     58 #include <sys/envsys.h>
     59 #include <sys/wdog.h>
     60 #include <sys/clockctl.h>
     61 #include <sys/exec_elf.h>
     62 #include <sys/ksyms.h>
     63 #include <sys/drvctlio.h>
     64 
     65 #ifdef __sparc__
     66 #include <dev/sun/fbio.h>
     67 #include <machine/openpromio.h>
     68 #endif
     69 
     70 #include <net/if.h>
     71 #include <net/route.h>
     72 
     73 #include <net/if_pppoe.h>
     74 #include <net/if_sppp.h>
     75 
     76 #include <net/npf/npf.h>
     77 
     78 #include <net/bpf.h>
     79 #include <netinet/in.h>
     80 #include <netinet/in_var.h>
     81 #include <netinet/igmp.h>
     82 #include <netinet/igmp_var.h>
     83 #include <netinet/ip_mroute.h>
     84 
     85 #include <compat/sys/sockio.h>
     86 
     87 #include <compat/netbsd32/netbsd32.h>
     88 #include <compat/netbsd32/netbsd32_ioctl.h>
     89 #include <compat/netbsd32/netbsd32_syscallargs.h>
     90 #include <compat/netbsd32/netbsd32_conv.h>
     91 
     92 #include <dev/vndvar.h>
     93 
     94 /* convert to/from different structures */
     95 
     96 #if 0
     97 static inline void
     98 netbsd32_to_format_op(struct netbsd32_format_op *s32p, struct format_op *p, u_long cmd)
     99 {
    100 
    101 	p->df_buf = (char *)NETBSD32PTR64(s32p->df_buf);
    102 	p->df_count = s32p->df_count;
    103 	p->df_startblk = s32p->df_startblk;
    104 	memcpy(p->df_reg, s32p->df_reg, sizeof(s32p->df_reg));
    105 }
    106 #endif
    107 
    108 static inline void
    109 netbsd32_to_ifreq(struct netbsd32_ifreq *s32p, struct ifreq *p, u_long cmd)
    110 {
    111 
    112 	memcpy(p, s32p, sizeof *s32p);
    113 	/*
    114 	 * XXX
    115 	 * struct ifreq says the same, but sometimes the ifr_data
    116 	 * union member needs to be converted to 64 bits... this
    117 	 * is very driver specific and so we ignore it for now..
    118 	 */
    119 	switch (cmd) {
    120 	case SIOCGIFDATA:
    121 	case SIOCZIFDATA:
    122 	case SIOCGIFGENERIC:
    123 	case SIOCSIFGENERIC:
    124 		p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
    125 		break;
    126 	}
    127 }
    128 
    129 static inline void
    130 netbsd32_to_oifreq(struct netbsd32_oifreq *s32p, struct oifreq *p, u_long cmd)
    131 {
    132 
    133 	memcpy(p, s32p, sizeof *s32p);
    134 	/*
    135 	 * XXX
    136 	 * struct ifreq says the same, but sometimes the ifr_data
    137 	 * union member needs to be converted to 64 bits... this
    138 	 * is very driver specific and so we ignore it for now..
    139 	 */
    140 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    141 		p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
    142 }
    143 
    144 static inline void
    145 netbsd32_to_if_addrprefreq(const struct netbsd32_if_addrprefreq *ifap32,
    146 	struct if_addrprefreq *ifap, u_long cmd)
    147 {
    148 	memcpy(ifap->ifap_name, ifap32->ifap_name, IFNAMSIZ);
    149 	ifap->ifap_preference = ifap32->ifap_preference;
    150 	memcpy(&ifap->ifap_addr, &ifap32->ifap_addr,
    151 	    min(ifap32->ifap_addr.ss_len, _SS_MAXSIZE));
    152 }
    153 
    154 static inline void
    155 netbsd32_to_ifconf(struct netbsd32_ifconf *s32p, struct ifconf *p, u_long cmd)
    156 {
    157 
    158 	p->ifc_len = s32p->ifc_len;
    159 	/* ifc_buf & ifc_req are the same size so this works */
    160 	p->ifc_buf = (void *)NETBSD32PTR64(s32p->ifc_buf);
    161 }
    162 
    163 static inline void
    164 netbsd32_to_ifmediareq(struct netbsd32_ifmediareq *s32p, struct ifmediareq *p, u_long cmd)
    165 {
    166 
    167 	memcpy(p, s32p, sizeof *s32p);
    168 	p->ifm_ulist = (int *)NETBSD32PTR64(s32p->ifm_ulist);
    169 }
    170 
    171 static inline void
    172 netbsd32_to_pppoediscparms(struct netbsd32_pppoediscparms *s32p,
    173     struct pppoediscparms *p, u_long cmd)
    174 {
    175 
    176 	memcpy(p->ifname, s32p->ifname, sizeof p->ifname);
    177 	memcpy(p->eth_ifname, s32p->eth_ifname, sizeof p->eth_ifname);
    178 	p->ac_name = (char *)NETBSD32PTR64(s32p->ac_name);
    179 	p->ac_name_len = s32p->ac_name_len;
    180 	p->service_name = (char *)NETBSD32PTR64(s32p->service_name);
    181 	p->service_name_len = s32p->service_name_len;
    182 }
    183 
    184 static inline void
    185 netbsd32_to_spppauthcfg(struct netbsd32_spppauthcfg *s32p,
    186     struct spppauthcfg *p, u_long cmd)
    187 {
    188 
    189 	memcpy(p->ifname, s32p->ifname, sizeof p->ifname);
    190 	p->hisauth = s32p->hisauth;
    191 	p->myauth = s32p->myauth;
    192 	p->myname_length = s32p->myname_length;
    193 	p->mysecret_length = s32p->mysecret_length;
    194 	p->hisname_length = s32p->hisname_length;
    195 	p->hissecret_length = s32p->hissecret_length;
    196 	p->myauthflags = s32p->myauthflags;
    197 	p->hisauthflags = s32p->hisauthflags;
    198 	p->myname = (char *)NETBSD32PTR64(s32p->myname);
    199 	p->mysecret = (char *)NETBSD32PTR64(s32p->mysecret);
    200 	p->hisname = (char *)NETBSD32PTR64(s32p->hisname);
    201 	p->hissecret = (char *)NETBSD32PTR64(s32p->hissecret);
    202 }
    203 
    204 static inline void
    205 netbsd32_to_ifdrv(struct netbsd32_ifdrv *s32p, struct ifdrv *p, u_long cmd)
    206 {
    207 
    208 	memcpy(p->ifd_name, s32p->ifd_name, sizeof p->ifd_name);
    209 	p->ifd_cmd = s32p->ifd_cmd;
    210 	p->ifd_len = s32p->ifd_len;
    211 	p->ifd_data = (void *)NETBSD32PTR64(s32p->ifd_data);
    212 }
    213 
    214 static inline void
    215 netbsd32_to_sioc_vif_req(struct netbsd32_sioc_vif_req *s32p, struct sioc_vif_req *p, u_long cmd)
    216 {
    217 
    218 	p->vifi = s32p->vifi;
    219 	p->icount = (u_long)s32p->icount;
    220 	p->ocount = (u_long)s32p->ocount;
    221 	p->ibytes = (u_long)s32p->ibytes;
    222 	p->obytes = (u_long)s32p->obytes;
    223 }
    224 
    225 static inline void
    226 netbsd32_to_sioc_sg_req(struct netbsd32_sioc_sg_req *s32p, struct sioc_sg_req *p, u_long cmd)
    227 {
    228 
    229 	p->src = s32p->src;
    230 	p->grp = s32p->grp;
    231 	p->pktcnt = (u_long)s32p->pktcnt;
    232 	p->bytecnt = (u_long)s32p->bytecnt;
    233 	p->wrong_if = (u_long)s32p->wrong_if;
    234 }
    235 
    236 static inline void
    237 netbsd32_to_atareq(struct netbsd32_atareq *s32p, struct atareq *p, u_long cmd)
    238 {
    239 	p->flags = (u_long)s32p->flags;
    240 	p->command = s32p->command;
    241 	p->features = s32p->features;
    242 	p->sec_count = s32p->sec_count;
    243 	p->sec_num = s32p->sec_num;
    244 	p->head = s32p->head;
    245 	p->cylinder = s32p->cylinder;
    246 	p->databuf =  (char *)NETBSD32PTR64(s32p->databuf);
    247 	p->datalen = (u_long)s32p->datalen;
    248 	p->timeout = s32p->timeout;
    249 	p->retsts = s32p->retsts;
    250 	p->error = s32p->error;
    251 }
    252 
    253 static inline void
    254 netbsd32_to_vnd_ioctl(struct netbsd32_vnd_ioctl *s32p, struct vnd_ioctl *p, u_long cmd)
    255 {
    256 
    257 	p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
    258 	p->vnd_flags = s32p->vnd_flags;
    259 	p->vnd_geom = s32p->vnd_geom;
    260 	p->vnd_osize = s32p->vnd_osize;
    261 	p->vnd_size = s32p->vnd_size;
    262 }
    263 
    264 static inline void
    265 netbsd32_to_vnd_user(struct netbsd32_vnd_user *s32p, struct vnd_user *p, u_long cmd)
    266 {
    267 
    268 	p->vnu_unit = s32p->vnu_unit;
    269 	p->vnu_dev = s32p->vnu_dev;
    270 	p->vnu_ino = s32p->vnu_ino;
    271 }
    272 
    273 static inline void
    274 netbsd32_to_vnd_ioctl50(struct netbsd32_vnd_ioctl50 *s32p, struct vnd_ioctl50 *p, u_long cmd)
    275 {
    276 
    277 	p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
    278 	p->vnd_flags = s32p->vnd_flags;
    279 	p->vnd_geom = s32p->vnd_geom;
    280 	p->vnd_size = s32p->vnd_size;
    281 }
    282 
    283 static inline void
    284 netbsd32_to_plistref(struct netbsd32_plistref *s32p, struct plistref *p, u_long cmd)
    285 {
    286 
    287 	p->pref_plist = NETBSD32PTR64(s32p->pref_plist);
    288 	p->pref_len = s32p->pref_len;
    289 }
    290 
    291 static inline void
    292 netbsd32_to_u_long(netbsd32_u_long *s32p, u_long *p, u_long cmd)
    293 {
    294 
    295 	*p = (u_long)*s32p;
    296 }
    297 
    298 static inline void
    299 netbsd32_to_voidp(netbsd32_voidp *s32p, voidp *p, u_long cmd)
    300 {
    301 
    302 	*p = (void *)NETBSD32PTR64(*s32p);
    303 }
    304 
    305 static inline void
    306 netbsd32_to_wdog_conf(struct netbsd32_wdog_conf *s32p, struct wdog_conf *p, u_long cmd)
    307 {
    308 
    309 	p->wc_names = (char *)NETBSD32PTR64(s32p->wc_names);
    310 	p->wc_count = s32p->wc_count;
    311 }
    312 
    313 static inline void
    314 netbsd32_to_bpf_program(struct netbsd32_bpf_program *s32p, struct bpf_program *p, u_long cmd)
    315 {
    316 
    317 	p->bf_insns = (void *)NETBSD32PTR64(s32p->bf_insns);
    318 	p->bf_len = s32p->bf_len;
    319 }
    320 
    321 static inline void
    322 netbsd32_to_bpf_dltlist(struct netbsd32_bpf_dltlist *s32p, struct bpf_dltlist *p, u_long cmd)
    323 {
    324 
    325 	p->bfl_list = (void *)NETBSD32PTR64(s32p->bfl_list);
    326 	p->bfl_len = s32p->bfl_len;
    327 }
    328 
    329 /* wsdisplay stuff */
    330 static inline void
    331 netbsd32_to_wsdisplay_addscreendata(struct netbsd32_wsdisplay_addscreendata *asd32,
    332 					       struct wsdisplay_addscreendata *asd,
    333 					       u_long cmd)
    334 {
    335 	asd->screentype = (char *)NETBSD32PTR64(asd32->screentype);
    336 	asd->emul = (char *)NETBSD32PTR64(asd32->emul);
    337 	asd->idx = asd32->idx;
    338 }
    339 
    340 static inline void
    341 netbsd32_to_ieee80211req(struct netbsd32_ieee80211req *ireq32,
    342 			 struct ieee80211req *ireq, u_long cmd)
    343 {
    344 	memcpy(ireq->i_name, ireq32->i_name, IFNAMSIZ);
    345 	ireq->i_type = ireq32->i_type;
    346 	ireq->i_val = ireq32->i_val;
    347 	ireq->i_len = ireq32->i_len;
    348 	ireq->i_data = NETBSD32PTR64(ireq32->i_data);
    349 }
    350 
    351 static inline void
    352 netbsd32_to_ieee80211_nwkey(struct netbsd32_ieee80211_nwkey *nwk32,
    353 					       struct ieee80211_nwkey *nwk,
    354 					       u_long cmd)
    355 {
    356 	int i;
    357 
    358 	memcpy(nwk->i_name, nwk32->i_name, IFNAMSIZ);
    359 	nwk->i_wepon = nwk32->i_wepon;
    360 	nwk->i_defkid = nwk32->i_defkid;
    361 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    362 		nwk->i_key[i].i_keylen = nwk32->i_key[i].i_keylen;
    363 		nwk->i_key[i].i_keydat =
    364 		    NETBSD32PTR64(nwk32->i_key[i].i_keydat);
    365 	}
    366 }
    367 
    368 static inline void
    369 netbsd32_to_wsdisplay_cursor(struct netbsd32_wsdisplay_cursor *c32,
    370 					       struct wsdisplay_cursor *c,
    371 					       u_long cmd)
    372 {
    373 	c->which = c32->which;
    374 	c->enable = c32->enable;
    375 	c->pos.x = c32->pos.x;
    376 	c->pos.y = c32->pos.y;
    377 	c->hot.x = c32->hot.x;
    378 	c->hot.y = c32->hot.y;
    379 	c->size.x = c32->size.x;
    380 	c->size.y = c32->size.y;
    381 	c->cmap.index = c32->cmap.index;
    382 	c->cmap.count = c32->cmap.count;
    383 	c->cmap.red = NETBSD32PTR64(c32->cmap.red);
    384 	c->cmap.green = NETBSD32PTR64(c32->cmap.green);
    385 	c->cmap.blue = NETBSD32PTR64(c32->cmap.blue);
    386 	c->image = NETBSD32PTR64(c32->image);
    387 	c->mask = NETBSD32PTR64(c32->mask);
    388 }
    389 
    390 static inline void
    391 netbsd32_to_wsdisplay_cmap(struct netbsd32_wsdisplay_cmap *c32,
    392 					       struct wsdisplay_cmap *c,
    393 					       u_long cmd)
    394 {
    395 	c->index = c32->index;
    396 	c->count = c32->count;
    397 	c->red   = NETBSD32PTR64(c32->red);
    398 	c->green = NETBSD32PTR64(c32->green);
    399 	c->blue  = NETBSD32PTR64(c32->blue);
    400 }
    401 
    402 static inline void
    403 netbsd32_to_clockctl_settimeofday(
    404     const struct netbsd32_clockctl_settimeofday *s32p,
    405     struct clockctl_settimeofday *p,
    406     u_long cmd)
    407 {
    408 
    409 	p->tv = NETBSD32PTR64(s32p->tv);
    410 	p->tzp = NETBSD32PTR64(s32p->tzp);
    411 }
    412 
    413 static inline void
    414 netbsd32_to_clockctl_adjtime(
    415     const struct netbsd32_clockctl_adjtime *s32p,
    416     struct clockctl_adjtime *p,
    417     u_long cmd)
    418 {
    419 
    420 	p->delta = NETBSD32PTR64(s32p->delta);
    421 	p->olddelta = NETBSD32PTR64(s32p->olddelta);
    422 }
    423 
    424 static inline void
    425 netbsd32_to_clockctl_clock_settime(
    426     const struct netbsd32_clockctl_clock_settime *s32p,
    427     struct clockctl_clock_settime *p,
    428     u_long cmd)
    429 {
    430 
    431 	p->clock_id = s32p->clock_id;
    432 	p->tp = NETBSD32PTR64(s32p->tp);
    433 }
    434 
    435 #ifdef NTP
    436 static inline void
    437 netbsd32_to_clockctl_ntp_adjtime(
    438     const struct netbsd32_clockctl_ntp_adjtime *s32p,
    439     struct clockctl_ntp_adjtime *p,
    440     u_long cmd)
    441 {
    442 
    443 	p->tp = NETBSD32PTR64(s32p->tp);
    444 	p->retval = s32p->retval;
    445 }
    446 #endif
    447 
    448 static inline void
    449 netbsd32_to_ksyms_gsymbol(
    450     const struct netbsd32_ksyms_gsymbol *s32p,
    451     struct ksyms_gsymbol *p,
    452     u_long cmd)
    453 {
    454 
    455 	p->kg_name = NETBSD32PTR64(s32p->kg_name);
    456 }
    457 
    458 static inline void
    459 netbsd32_to_ksyms_gvalue(
    460     const struct netbsd32_ksyms_gvalue *s32p,
    461     struct ksyms_gvalue *p,
    462     u_long cmd)
    463 {
    464 
    465 	p->kv_name = NETBSD32PTR64(s32p->kv_name);
    466 }
    467 
    468 static inline void
    469 netbsd32_to_npf_ioctl_table(
    470     const struct netbsd32_npf_ioctl_table *s32p,
    471     struct npf_ioctl_table *p,
    472     u_long cmd)
    473 {
    474 
    475 	p->nct_cmd = s32p->nct_cmd;
    476 	p->nct_name = NETBSD32PTR64(s32p->nct_name);
    477 	switch (s32p->nct_cmd) {
    478 	case NPF_CMD_TABLE_LOOKUP:
    479 	case NPF_CMD_TABLE_ADD:
    480 	case NPF_CMD_TABLE_REMOVE:
    481 		p->nct_data.ent = s32p->nct_data.ent;
    482 		break;
    483 	case NPF_CMD_TABLE_LIST:
    484 		p->nct_data.buf.buf = NETBSD32PTR64(s32p->nct_data.buf.buf);
    485 		p->nct_data.buf.len = s32p->nct_data.buf.len;
    486 		break;
    487 	}
    488 }
    489 
    490 static inline void
    491 netbsd32_to_devlistargs(
    492     const struct netbsd32_devlistargs *s32p,
    493     struct devlistargs *p,
    494     u_long cmd)
    495 {
    496 	memcpy(p->l_devname, s32p->l_devname, sizeof(p->l_devname));
    497 	p->l_children = s32p->l_children;
    498 	p->l_childname = NETBSD32PTR64(s32p->l_childname);
    499 }
    500 
    501 static inline void
    502 netbsd32_to_devrescanargs(
    503     const struct netbsd32_devrescanargs *s32p,
    504     struct devrescanargs *p,
    505     u_long cmd)
    506 {
    507 	memcpy(p->busname, s32p->busname, sizeof(p->busname));
    508 	memcpy(p->ifattr, s32p->ifattr, sizeof(p->ifattr));
    509 	p->numlocators = s32p->numlocators;
    510 	p->locators = NETBSD32PTR64(s32p->locators);
    511 }
    512 
    513 /*
    514  * handle ioctl conversions from 64-bit kernel -> netbsd32
    515  */
    516 
    517 #if 0
    518 static inline void
    519 netbsd32_from_format_op(struct format_op *p, struct netbsd32_format_op *s32p, u_long cmd)
    520 {
    521 
    522 /* filled in */
    523 #if 0
    524 	s32p->df_buf = (netbsd32_charp)p->df_buf;
    525 #endif
    526 	s32p->df_count = p->df_count;
    527 	s32p->df_startblk = p->df_startblk;
    528 	memcpy(s32p->df_reg, p->df_reg, sizeof(p->df_reg));
    529 }
    530 #endif
    531 
    532 static inline void
    533 netbsd32_from_ifreq(struct ifreq *p, struct netbsd32_ifreq *s32p, u_long cmd)
    534 {
    535 
    536 	/*
    537 	 * XXX
    538 	 * struct ifreq says the same, but sometimes the ifr_data
    539 	 * union member needs to be converted to 64 bits... this
    540 	 * is very driver specific and so we ignore it for now..
    541 	 */
    542 	memcpy(s32p, p, sizeof *s32p);
    543 	switch (cmd) {
    544 	case SIOCGIFDATA:
    545 	case SIOCZIFDATA:
    546 	case SIOCGIFGENERIC:
    547 	case SIOCSIFGENERIC:
    548 		NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
    549 		break;
    550 	}
    551 }
    552 
    553 static inline void
    554 netbsd32_from_oifreq(struct oifreq *p, struct netbsd32_oifreq *s32p, u_long cmd)
    555 {
    556 
    557 	/*
    558 	 * XXX
    559 	 * struct ifreq says the same, but sometimes the ifr_data
    560 	 * union member needs to be converted to 64 bits... this
    561 	 * is very driver specific and so we ignore it for now..
    562 	 */
    563 	memcpy(s32p, p, sizeof *s32p);
    564 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    565 		NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
    566 }
    567 
    568 static inline void
    569 netbsd32_from_if_addrprefreq(const struct if_addrprefreq *ifap,
    570 	struct netbsd32_if_addrprefreq *ifap32, u_long cmd)
    571 {
    572 	memcpy(ifap32->ifap_name, ifap->ifap_name, IFNAMSIZ);
    573 	ifap32->ifap_preference = ifap->ifap_preference;
    574 	memcpy(&ifap32->ifap_addr, &ifap->ifap_addr,
    575 	    min(ifap->ifap_addr.ss_len, _SS_MAXSIZE));
    576 }
    577 
    578 static inline void
    579 netbsd32_from_ifconf(struct ifconf *p, struct netbsd32_ifconf *s32p, u_long cmd)
    580 {
    581 
    582 	s32p->ifc_len = p->ifc_len;
    583 	/* ifc_buf & ifc_req are the same size so this works */
    584 	NETBSD32PTR32(s32p->ifc_buf, p->ifc_buf);
    585 }
    586 
    587 static inline void
    588 netbsd32_from_ifmediareq(struct ifmediareq *p, struct netbsd32_ifmediareq *s32p, u_long cmd)
    589 {
    590 
    591 	memcpy(s32p, p, sizeof *p);
    592 /* filled in? */
    593 #if 0
    594 	s32p->ifm_ulist = (netbsd32_intp_t)p->ifm_ulist;
    595 #endif
    596 }
    597 
    598 static inline void
    599 netbsd32_from_pppoediscparms(struct pppoediscparms *p,
    600     struct netbsd32_pppoediscparms *s32p, u_long cmd)
    601 {
    602 
    603 	memcpy(s32p->ifname, p->ifname, sizeof s32p->ifname);
    604 	memcpy(s32p->eth_ifname, p->eth_ifname, sizeof s32p->eth_ifname);
    605 	NETBSD32PTR32(s32p->ac_name, p->ac_name);
    606 	s32p->ac_name_len = p->ac_name_len;
    607 	NETBSD32PTR32(s32p->service_name, p->service_name);
    608 	s32p->service_name_len = p->service_name_len;
    609 }
    610 
    611 static inline void
    612 netbsd32_from_spppauthcfg(struct spppauthcfg *p,
    613     struct netbsd32_spppauthcfg *s32p, u_long cmd)
    614 {
    615 
    616 	memcpy(s32p->ifname, p->ifname, sizeof s32p->ifname);
    617 	s32p->hisauth = p->hisauth;
    618 	s32p->myauth = p->myauth;
    619 	s32p->myname_length = p->myname_length;
    620 	s32p->mysecret_length = p->mysecret_length;
    621 	s32p->hisname_length = p->hisname_length;
    622 	s32p->hissecret_length = p->hissecret_length;
    623 	s32p->myauthflags = p->myauthflags;
    624 	s32p->hisauthflags = p->hisauthflags;
    625 	NETBSD32PTR32(s32p->myname, p->myname);
    626 	NETBSD32PTR32(s32p->mysecret, p->mysecret);
    627 	NETBSD32PTR32(s32p->hisname, p->hisname);
    628 	NETBSD32PTR32(s32p->hissecret, p->hissecret);
    629 }
    630 
    631 static inline void
    632 netbsd32_from_ifdrv(struct ifdrv *p, struct netbsd32_ifdrv *s32p, u_long cmd)
    633 {
    634 
    635 	memcpy(s32p->ifd_name, p->ifd_name, sizeof s32p->ifd_name);
    636 	s32p->ifd_cmd = p->ifd_cmd;
    637 	s32p->ifd_len = p->ifd_len;
    638 	NETBSD32PTR32(s32p->ifd_data, p->ifd_data);
    639 }
    640 
    641 static inline void
    642 netbsd32_from_sioc_vif_req(struct sioc_vif_req *p, struct netbsd32_sioc_vif_req *s32p, u_long cmd)
    643 {
    644 
    645 	s32p->vifi = p->vifi;
    646 	s32p->icount = (netbsd32_u_long)p->icount;
    647 	s32p->ocount = (netbsd32_u_long)p->ocount;
    648 	s32p->ibytes = (netbsd32_u_long)p->ibytes;
    649 	s32p->obytes = (netbsd32_u_long)p->obytes;
    650 }
    651 
    652 static inline void
    653 netbsd32_from_sioc_sg_req(struct sioc_sg_req *p, struct netbsd32_sioc_sg_req *s32p, u_long cmd)
    654 {
    655 
    656 	s32p->src = p->src;
    657 	s32p->grp = p->grp;
    658 	s32p->pktcnt = (netbsd32_u_long)p->pktcnt;
    659 	s32p->bytecnt = (netbsd32_u_long)p->bytecnt;
    660 	s32p->wrong_if = (netbsd32_u_long)p->wrong_if;
    661 }
    662 
    663 static inline void
    664 netbsd32_from_atareq(struct atareq *p, struct netbsd32_atareq *s32p, u_long cmd)
    665 {
    666 	s32p->flags = (netbsd32_u_long)p->flags;
    667 	s32p->command = p->command;
    668 	s32p->features = p->features;
    669 	s32p->sec_count = p->sec_count;
    670 	s32p->sec_num = p->sec_num;
    671 	s32p->head = p->head;
    672 	s32p->cylinder = p->cylinder;
    673 	NETBSD32PTR32(s32p->databuf, p->databuf);
    674 	s32p->datalen = (netbsd32_u_long)p->datalen;
    675 	s32p->timeout = p->timeout;
    676 	s32p->retsts = p->retsts;
    677 	s32p->error = p->error;
    678 }
    679 
    680 static inline void
    681 netbsd32_from_vnd_ioctl(struct vnd_ioctl *p, struct netbsd32_vnd_ioctl *s32p, u_long cmd)
    682 {
    683 
    684 	s32p->vnd_flags = p->vnd_flags;
    685 	s32p->vnd_geom = p->vnd_geom;
    686 	s32p->vnd_osize = p->vnd_osize;
    687 	s32p->vnd_size = p->vnd_size;
    688 }
    689 
    690 static inline void
    691 netbsd32_from_vnd_user(struct vnd_user *p, struct netbsd32_vnd_user *s32p, u_long cmd)
    692 {
    693 
    694 	s32p->vnu_unit = p->vnu_unit;
    695 	s32p->vnu_dev = p->vnu_dev;
    696 	s32p->vnu_ino = p->vnu_ino;
    697 }
    698 
    699 static inline void
    700 netbsd32_from_vnd_ioctl50(struct vnd_ioctl50 *p, struct netbsd32_vnd_ioctl50 *s32p, u_long cmd)
    701 {
    702 
    703 	s32p->vnd_flags = p->vnd_flags;
    704 	s32p->vnd_geom = p->vnd_geom;
    705 	s32p->vnd_size = p->vnd_size;
    706 }
    707 
    708 static inline void
    709 netbsd32_from_plistref(struct plistref *p, struct netbsd32_plistref *s32p, u_long cmd)
    710 {
    711 
    712 	NETBSD32PTR32(s32p->pref_plist, p->pref_plist);
    713 	s32p->pref_len = p->pref_len;
    714 }
    715 
    716 static inline void
    717 netbsd32_from_wdog_conf(struct wdog_conf *p, struct netbsd32_wdog_conf *s32p, u_long cmd)
    718 {
    719 
    720 	NETBSD32PTR32(s32p->wc_names, p->wc_names);
    721 	s32p->wc_count = p->wc_count;
    722 }
    723 
    724 /* wsdisplay stuff */
    725 static inline void
    726 netbsd32_from_wsdisplay_addscreendata(struct wsdisplay_addscreendata *asd,
    727 					struct netbsd32_wsdisplay_addscreendata *asd32,
    728 					u_long cmd)
    729 {
    730 	NETBSD32PTR32(asd32->screentype, asd->screentype);
    731 	NETBSD32PTR32(asd32->emul, asd->emul);
    732 	asd32->idx = asd->idx;
    733 }
    734 
    735 static inline void
    736 netbsd32_from_wsdisplay_cursor(struct wsdisplay_cursor *c,
    737 					       struct netbsd32_wsdisplay_cursor *c32,
    738 					       u_long cmd)
    739 {
    740 	c32->which = c->which;
    741 	c32->enable = c->enable;
    742 	c32->pos.x = c->pos.x;
    743 	c32->pos.y = c->pos.y;
    744 	c32->hot.x = c->hot.x;
    745 	c32->hot.y = c->hot.y;
    746 	c32->size.x = c->size.x;
    747 	c32->size.y = c->size.y;
    748 	c32->cmap.index = c->cmap.index;
    749 	c32->cmap.count = c->cmap.count;
    750 	NETBSD32PTR32(c32->cmap.red, c->cmap.red);
    751 	NETBSD32PTR32(c32->cmap.green, c->cmap.green);
    752 	NETBSD32PTR32(c32->cmap.blue, c->cmap.blue);
    753 	NETBSD32PTR32(c32->image, c->image);
    754 	NETBSD32PTR32(c32->mask, c->mask);
    755 }
    756 
    757 static inline void
    758 netbsd32_from_wsdisplay_cmap(struct wsdisplay_cmap *c,
    759 					   struct netbsd32_wsdisplay_cmap *c32,
    760 					   u_long cmd)
    761 {
    762 	c32->index = c->index;
    763 	c32->count = c->count;
    764 	NETBSD32PTR32(c32->red, c->red);
    765 	NETBSD32PTR32(c32->green, c->green);
    766 	NETBSD32PTR32(c32->blue, c->blue);
    767 }
    768 
    769 static inline void
    770 netbsd32_from_ieee80211req(struct ieee80211req *ireq,
    771 			   struct netbsd32_ieee80211req *ireq32, u_long cmd)
    772 {
    773 	memcpy(ireq32->i_name, ireq->i_name, IFNAMSIZ);
    774 	ireq32->i_type = ireq->i_type;
    775 	ireq32->i_val = ireq->i_val;
    776 	ireq32->i_len = ireq->i_len;
    777 	NETBSD32PTR32(ireq32->i_data, ireq->i_data);
    778 }
    779 
    780 static inline void
    781 netbsd32_from_ieee80211_nwkey(struct ieee80211_nwkey *nwk,
    782 				struct netbsd32_ieee80211_nwkey *nwk32,
    783 				u_long cmd)
    784 {
    785 	int i;
    786 
    787 	memcpy(nwk32->i_name, nwk->i_name, IFNAMSIZ);
    788 	nwk32->i_wepon = nwk->i_wepon;
    789 	nwk32->i_defkid = nwk->i_defkid;
    790 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    791 		nwk32->i_key[i].i_keylen = nwk->i_key[i].i_keylen;
    792 		NETBSD32PTR32(nwk32->i_key[i].i_keydat,
    793 				nwk->i_key[i].i_keydat);
    794 	}
    795 }
    796 
    797 static inline void
    798 netbsd32_from_bpf_program(struct bpf_program *p, struct netbsd32_bpf_program *s32p, u_long cmd)
    799 {
    800 
    801 	NETBSD32PTR32(s32p->bf_insns, p->bf_insns);
    802 	s32p->bf_len = p->bf_len;
    803 }
    804 
    805 static inline void
    806 netbsd32_from_bpf_dltlist(struct bpf_dltlist *p, struct netbsd32_bpf_dltlist *s32p, u_long cmd)
    807 {
    808 
    809 	NETBSD32PTR32(s32p->bfl_list, p->bfl_list);
    810 	s32p->bfl_len = p->bfl_len;
    811 }
    812 
    813 static inline void
    814 netbsd32_from_u_long(u_long *p, netbsd32_u_long *s32p, u_long cmd)
    815 {
    816 
    817 	*s32p = (netbsd32_u_long)*p;
    818 }
    819 
    820 static inline void
    821 netbsd32_from_voidp(voidp *p, netbsd32_voidp *s32p, u_long cmd)
    822 {
    823 
    824 	NETBSD32PTR32(*s32p, *p);
    825 }
    826 
    827 
    828 static inline void
    829 netbsd32_from_clockctl_settimeofday(
    830     const struct clockctl_settimeofday *p,
    831     struct netbsd32_clockctl_settimeofday *s32p,
    832     u_long cmd)
    833 {
    834 
    835 	NETBSD32PTR32(s32p->tv, p->tv);
    836 	NETBSD32PTR32(s32p->tzp, p->tzp);
    837 }
    838 
    839 static inline void
    840 netbsd32_from_clockctl_adjtime(
    841     const struct clockctl_adjtime *p,
    842     struct netbsd32_clockctl_adjtime *s32p,
    843     u_long cmd)
    844 {
    845 
    846 	NETBSD32PTR32(s32p->delta, p->delta);
    847 	NETBSD32PTR32(s32p->olddelta, p->olddelta);
    848 }
    849 
    850 static inline void
    851 netbsd32_from_clockctl_clock_settime(
    852     const struct clockctl_clock_settime *p,
    853     struct netbsd32_clockctl_clock_settime *s32p,
    854     u_long cmd)
    855 {
    856 
    857 	s32p->clock_id = p->clock_id;
    858 	NETBSD32PTR32(s32p->tp, p->tp);
    859 }
    860 
    861 #ifdef NTP
    862 static inline void
    863 netbsd32_from_clockctl_ntp_adjtime(
    864     const struct clockctl_ntp_adjtime *p,
    865     struct netbsd32_clockctl_ntp_adjtime *s32p,
    866     u_long cmd)
    867 {
    868 
    869 	NETBSD32PTR32(s32p->tp, p->tp);
    870 	s32p->retval = p->retval;
    871 }
    872 #endif
    873 
    874 static inline void
    875 netbsd32_from_ksyms_gsymbol(
    876     const struct ksyms_gsymbol *p,
    877     struct netbsd32_ksyms_gsymbol *s32p,
    878     u_long cmd)
    879 {
    880 
    881 	NETBSD32PTR32(s32p->kg_name, p->kg_name);
    882 	s32p->kg_sym = p->kg_sym;
    883 }
    884 
    885 static inline void
    886 netbsd32_from_ksyms_gvalue(
    887     const struct ksyms_gvalue *p,
    888     struct netbsd32_ksyms_gvalue *s32p,
    889     u_long cmd)
    890 {
    891 
    892 	NETBSD32PTR32(s32p->kv_name, p->kv_name);
    893 	s32p->kv_value = p->kv_value;
    894 }
    895 
    896 static inline void
    897 netbsd32_from_npf_ioctl_table(
    898     const struct npf_ioctl_table *p,
    899     struct netbsd32_npf_ioctl_table *s32p,
    900     u_long cmd)
    901 {
    902 
    903 	s32p->nct_cmd = p->nct_cmd;
    904 	NETBSD32PTR32(s32p->nct_name, p->nct_name);
    905 	switch (p->nct_cmd) {
    906 	case NPF_CMD_TABLE_LOOKUP:
    907 	case NPF_CMD_TABLE_ADD:
    908 	case NPF_CMD_TABLE_REMOVE:
    909 		s32p->nct_data.ent = p->nct_data.ent;
    910 		break;
    911 	case NPF_CMD_TABLE_LIST:
    912 		NETBSD32PTR32(s32p->nct_data.buf.buf, p->nct_data.buf.buf);
    913 		s32p->nct_data.buf.len = p->nct_data.buf.len;
    914 		break;
    915 	}
    916 }
    917 
    918 static inline void
    919 netbsd32_from_devlistargs(
    920     const struct devlistargs *p,
    921     struct netbsd32_devlistargs *s32p,
    922     u_long cmd)
    923 {
    924 	memcpy(s32p->l_devname, p->l_devname, sizeof(s32p->l_devname));
    925 	s32p->l_children = p->l_children;
    926 	NETBSD32PTR32(s32p->l_childname, p->l_childname);
    927 }
    928 
    929 static inline void
    930 netbsd32_from_devrescanargs(
    931     const struct devrescanargs *p,
    932     struct netbsd32_devrescanargs *s32p,
    933     u_long cmd)
    934 {
    935 	memcpy(s32p->busname, p->busname, sizeof(s32p->busname));
    936 	memcpy(s32p->ifattr, p->ifattr, sizeof(s32p->ifattr));
    937 	s32p->numlocators = p->numlocators;
    938 	NETBSD32PTR32(s32p->locators, p->locators);
    939 }
    940 
    941 #ifdef NTP
    942 static int
    943 netbsd32_do_clockctl_ntp_adjtime(struct clockctl_ntp_adjtime *args)
    944 {
    945 
    946 	struct netbsd32_timex ntv32;
    947 	struct timex ntv;
    948 	int error;
    949 
    950 	error = copyin(args->tp, &ntv32, sizeof(ntv32));
    951 	if (error)
    952 		return (error);
    953 
    954 	netbsd32_to_timex(&ntv32, &ntv);
    955 	ntp_adjtime1(&ntv);
    956 	netbsd32_from_timex(&ntv, &ntv32);
    957 
    958 	error = copyout(&ntv32, args->tp, sizeof(ntv));
    959 	if (error == 0)
    960 		args->retval = ntp_timestatus();
    961 
    962 	return error;
    963 }
    964 #endif
    965 
    966 /*
    967  * main ioctl syscall.
    968  *
    969  * ok, here we are in the biggy.  we have to do fix ups depending
    970  * on the ioctl command before and afterwards.
    971  */
    972 int
    973 netbsd32_ioctl(struct lwp *l, const struct netbsd32_ioctl_args *uap, register_t *retval)
    974 {
    975 	/* {
    976 		syscallarg(int) fd;
    977 		syscallarg(netbsd32_u_long) com;
    978 		syscallarg(netbsd32_voidp) data;
    979 	} */
    980 	struct proc *p = l->l_proc;
    981 	struct file *fp;
    982 	struct filedesc *fdp;
    983 	u_long com;
    984 	int error = 0;
    985 	size_t size;
    986 	size_t alloc_size32, size32;
    987 	void *data, *memp = NULL;
    988 	void *data32, *memp32 = NULL;
    989 	unsigned int fd;
    990 	fdfile_t *ff;
    991 	int tmp;
    992 #define STK_PARAMS	128
    993 	uint64_t stkbuf[STK_PARAMS/sizeof(uint64_t)];
    994 	uint64_t stkbuf32[STK_PARAMS/sizeof(uint64_t)];
    995 
    996 	/*
    997 	 * we need to translate some commands (_IOW) before calling sys_ioctl,
    998 	 * some after (_IOR), and some both (_IOWR).
    999 	 */
   1000 #if 0
   1001 	{
   1002 		const char * const dirs[8] = {
   1003 		    "NONE!", "VOID", "OUT", "VOID|OUT!", "IN", "VOID|IN!",
   1004 		    "INOUT", "VOID|IN|OUT!"
   1005 		};
   1006 
   1007 		printf("netbsd32_ioctl(%d, %x, %x): "
   1008 		    "%s group %c base %d len %d\n",
   1009 		    SCARG(uap, fd), SCARG(uap, com), SCARG(uap, data).i32,
   1010 		    dirs[((SCARG(uap, com) & IOC_DIRMASK)>>29)],
   1011 		    IOCGROUP(SCARG(uap, com)), IOCBASECMD(SCARG(uap, com)),
   1012 		    IOCPARM_LEN(SCARG(uap, com)));
   1013 	}
   1014 #endif
   1015 
   1016 	memp = NULL;
   1017 	memp32 = NULL;
   1018 	alloc_size32 = 0;
   1019 	size32 = 0;
   1020 	size = 0;
   1021 
   1022 	fdp = p->p_fd;
   1023 	fd = SCARG(uap, fd);
   1024 	if ((fp = fd_getfile(fd)) == NULL)
   1025 		return (EBADF);
   1026 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
   1027 		error = EBADF;
   1028 		goto out;
   1029 	}
   1030 
   1031 	ff = fdp->fd_dt->dt_ff[SCARG(uap, fd)];
   1032 	switch (com = SCARG(uap, com)) {
   1033 	case FIOCLEX:
   1034 		ff->ff_exclose = true;
   1035 		fdp->fd_exclose = true;
   1036 		goto out;
   1037 
   1038 	case FIONCLEX:
   1039 		ff->ff_exclose = false;
   1040 		goto out;
   1041 	}
   1042 
   1043 	/*
   1044 	 * Interpret high order word to find amount of data to be
   1045 	 * copied to/from the user's address space.
   1046 	 */
   1047 	size32 = IOCPARM_LEN(com);
   1048 	alloc_size32 = size32;
   1049 
   1050 	/*
   1051 	 * The disklabel is now padded to a multiple of 8 bytes however the old
   1052 	 * disklabel on 32bit platforms wasn't.  This leaves a difference in
   1053 	 * size of 4 bytes between the two but are otherwise identical.
   1054 	 * To deal with this, we allocate enough space for the new disklabel
   1055 	 * but only copyin/out the smaller amount.
   1056 	 */
   1057 	if (IOCGROUP(com) == 'd') {
   1058 		u_long ncom = com ^ (DIOCGDINFO ^ DIOCGDINFO32);
   1059 		switch (ncom) {
   1060 		case DIOCGDINFO:
   1061 		case DIOCWDINFO:
   1062 		case DIOCSDINFO:
   1063 		case DIOCGDEFLABEL:
   1064 			com = ncom;
   1065 			if (IOCPARM_LEN(DIOCGDINFO32) < IOCPARM_LEN(DIOCGDINFO))
   1066 				alloc_size32 = IOCPARM_LEN(DIOCGDINFO);
   1067 			break;
   1068 		}
   1069 	}
   1070 	if (alloc_size32 > IOCPARM_MAX) {
   1071 		error = ENOTTY;
   1072 		goto out;
   1073 	}
   1074 	if (alloc_size32 > sizeof(stkbuf)) {
   1075 		memp32 = kmem_alloc(alloc_size32, KM_SLEEP);
   1076 		data32 = memp32;
   1077 	} else
   1078 		data32 = (void *)stkbuf32;
   1079 	if ((com >> IOCPARM_SHIFT) == 0)  {
   1080 		/* UNIX-style ioctl. */
   1081 		data32 = SCARG_P32(uap, data);
   1082 	} else {
   1083 		if (com&IOC_IN) {
   1084 			if (size32) {
   1085 				error = copyin(SCARG_P32(uap, data), data32,
   1086 				    size32);
   1087 				if (error) {
   1088 					goto out;
   1089 				}
   1090 				/*
   1091 				 * The data between size and alloc_size has
   1092 				 * not been overwritten.  It shouldn't matter
   1093 				 * but let's clear that anyway.
   1094 				 */
   1095 				if (__predict_false(size32 < alloc_size32)) {
   1096 					memset((char *)data32+size32, 0,
   1097 					    alloc_size32 - size32);
   1098 				}
   1099 				ktrgenio(fd, UIO_WRITE, SCARG_P32(uap, data),
   1100 				    size32, 0);
   1101 			} else
   1102 				*(void **)data32 = SCARG_P32(uap, data);
   1103 		} else if ((com&IOC_OUT) && size32) {
   1104 			/*
   1105 			 * Zero the buffer so the user always
   1106 			 * gets back something deterministic.
   1107 			 */
   1108 			memset(data32, 0, alloc_size32);
   1109 		} else if (com&IOC_VOID) {
   1110 			*(void **)data32 = SCARG_P32(uap, data);
   1111 		}
   1112 	}
   1113 
   1114 	/*
   1115 	 * convert various structures, pointers, and other objects that
   1116 	 * change size from 32 bit -> 64 bit, for all ioctl commands.
   1117 	 */
   1118 	switch (SCARG(uap, com)) {
   1119 	case FIONBIO:
   1120 		mutex_enter(&fp->f_lock);
   1121 		if ((tmp = *(int *)data32) != 0)
   1122 			fp->f_flag |= FNONBLOCK;
   1123 		else
   1124 			fp->f_flag &= ~FNONBLOCK;
   1125 		mutex_exit(&fp->f_lock);
   1126 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (void *)&tmp);
   1127 		break;
   1128 
   1129 	case FIOASYNC:
   1130 		mutex_enter(&fp->f_lock);
   1131 		if ((tmp = *(int *)data32) != 0)
   1132 			fp->f_flag |= FASYNC;
   1133 		else
   1134 			fp->f_flag &= ~FASYNC;
   1135 		mutex_exit(&fp->f_lock);
   1136 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (void *)&tmp);
   1137 		break;
   1138 
   1139 	case AUDIO_WSEEK32:
   1140 		IOCTL_CONV_TO(AUDIO_WSEEK, u_long);
   1141 
   1142 #if 0	/* not implemented by anything */
   1143 	case DIOCRFORMAT32:
   1144 		IOCTL_STRUCT_CONV_TO(DIOCRFORMAT, format_op);
   1145 	case DIOCWFORMAT32:
   1146 		IOCTL_STRUCT_CONV_TO(DIOCWFORMAT, format_op);
   1147 #endif
   1148 
   1149 	case ATAIOCCOMMAND32:
   1150 		IOCTL_STRUCT_CONV_TO(ATAIOCCOMMAND, atareq);
   1151 
   1152 	case SIOCIFGCLONERS32:
   1153 		{
   1154 			struct netbsd32_if_clonereq *req =
   1155 			    (struct netbsd32_if_clonereq *)data32;
   1156 			char *buf = NETBSD32PTR64(req->ifcr_buffer);
   1157 
   1158 			error = if_clone_list(req->ifcr_count,
   1159 			    buf, &req->ifcr_total);
   1160 			break;
   1161 		}
   1162 
   1163 /*
   1164  * only a few ifreq syscalls need conversion and those are
   1165  * all driver specific... XXX
   1166  */
   1167 #if 0
   1168 	case SIOCGADDRROM3232:
   1169 		IOCTL_STRUCT_CONV_TO(SIOCGADDRROM32, ifreq);
   1170 	case SIOCGCHIPID32:
   1171 		IOCTL_STRUCT_CONV_TO(SIOCGCHIPID, ifreq);
   1172 	case SIOCSIFADDR32:
   1173 		IOCTL_STRUCT_CONV_TO(SIOCSIFADDR, ifreq);
   1174 	case OSIOCGIFADDR32:
   1175 		IOCTL_STRUCT_CONV_TO(OSIOCGIFADDR, ifreq);
   1176 	case SIOCGIFADDR32:
   1177 		IOCTL_STRUCT_CONV_TO(SIOCGIFADDR, ifreq);
   1178 	case SIOCSIFDSTADDR32:
   1179 		IOCTL_STRUCT_CONV_TO(SIOCSIFDSTADDR, ifreq);
   1180 	case OSIOCGIFDSTADDR32:
   1181 		IOCTL_STRUCT_CONV_TO(OSIOCGIFDSTADDR, ifreq);
   1182 	case SIOCGIFDSTADDR32:
   1183 		IOCTL_STRUCT_CONV_TO(SIOCGIFDSTADDR, ifreq);
   1184 	case OSIOCGIFBRDADDR32:
   1185 		IOCTL_STRUCT_CONV_TO(OSIOCGIFBRDADDR, ifreq);
   1186 	case SIOCGIFBRDADDR32:
   1187 		IOCTL_STRUCT_CONV_TO(SIOCGIFBRDADDR, ifreq);
   1188 	case SIOCSIFBRDADDR32:
   1189 		IOCTL_STRUCT_CONV_TO(SIOCSIFBRDADDR, ifreq);
   1190 	case OSIOCGIFNETMASK32:
   1191 		IOCTL_STRUCT_CONV_TO(OSIOCGIFNETMASK, ifreq);
   1192 	case SIOCGIFNETMASK32:
   1193 		IOCTL_STRUCT_CONV_TO(SIOCGIFNETMASK, ifreq);
   1194 	case SIOCSIFNETMASK32:
   1195 		IOCTL_STRUCT_CONV_TO(SIOCSIFNETMASK, ifreq);
   1196 	case SIOCGIFMETRIC32:
   1197 		IOCTL_STRUCT_CONV_TO(SIOCGIFMETRIC, ifreq);
   1198 	case SIOCSIFMETRIC32:
   1199 		IOCTL_STRUCT_CONV_TO(SIOCSIFMETRIC, ifreq);
   1200 	case SIOCDIFADDR32:
   1201 		IOCTL_STRUCT_CONV_TO(SIOCDIFADDR, ifreq);
   1202 	case SIOCADDMULTI32:
   1203 		IOCTL_STRUCT_CONV_TO(SIOCADDMULTI, ifreq);
   1204 	case SIOCDELMULTI32:
   1205 		IOCTL_STRUCT_CONV_TO(SIOCDELMULTI, ifreq);
   1206 	case SIOCSIFMEDIA32:
   1207 		IOCTL_STRUCT_CONV_TO(SIOCSIFMEDIA, ifreq);
   1208 	case SIOCSIFMTU32:
   1209 		IOCTL_STRUCT_CONV_TO(SIOCSIFMTU, ifreq);
   1210 	case SIOCGIFMTU32:
   1211 		IOCTL_STRUCT_CONV_TO(SIOCGIFMTU, ifreq);
   1212 	case BIOCGETIF32:
   1213 		IOCTL_STRUCT_CONV_TO(BIOCGETIF, ifreq);
   1214 	case BIOCSETIF32:
   1215 		IOCTL_STRUCT_CONV_TO(BIOCSETIF, ifreq);
   1216 	case SIOCPHASE132:
   1217 		IOCTL_STRUCT_CONV_TO(SIOCPHASE1, ifreq);
   1218 	case SIOCPHASE232:
   1219 		IOCTL_STRUCT_CONV_TO(SIOCPHASE2, ifreq);
   1220 #endif
   1221 
   1222 	case OOSIOCGIFCONF32:
   1223 		IOCTL_STRUCT_CONV_TO(OOSIOCGIFCONF, ifconf);
   1224 	case OSIOCGIFCONF32:
   1225 		IOCTL_STRUCT_CONV_TO(OSIOCGIFCONF, ifconf);
   1226 	case SIOCGIFCONF32:
   1227 		IOCTL_STRUCT_CONV_TO(SIOCGIFCONF, ifconf);
   1228 
   1229 	case SIOCGIFFLAGS32:
   1230 		IOCTL_STRUCT_CONV_TO(SIOCGIFFLAGS, ifreq);
   1231 	case SIOCSIFFLAGS32:
   1232 		IOCTL_STRUCT_CONV_TO(SIOCSIFFLAGS, ifreq);
   1233 
   1234 	case SIOCGIFADDRPREF32:
   1235 		IOCTL_STRUCT_CONV_TO(SIOCGIFADDRPREF, if_addrprefreq);
   1236 	case SIOCSIFADDRPREF32:
   1237 		IOCTL_STRUCT_CONV_TO(SIOCSIFADDRPREF, if_addrprefreq);
   1238 
   1239 
   1240 	case OSIOCGIFFLAGS32:
   1241 		IOCTL_STRUCT_CONV_TO(OSIOCGIFFLAGS, oifreq);
   1242 	case OSIOCSIFFLAGS32:
   1243 		IOCTL_STRUCT_CONV_TO(OSIOCSIFFLAGS, oifreq);
   1244 
   1245 	case SIOCGIFMEDIA32:
   1246 		IOCTL_STRUCT_CONV_TO(SIOCGIFMEDIA, ifmediareq);
   1247 
   1248 	case SIOCGIFGENERIC32:
   1249 		IOCTL_STRUCT_CONV_TO(SIOCGIFGENERIC, ifreq);
   1250 	case SIOCSIFGENERIC32:
   1251 		IOCTL_STRUCT_CONV_TO(SIOCSIFGENERIC, ifreq);
   1252 
   1253 	case PPPOESETPARMS32:
   1254 		IOCTL_STRUCT_CONV_TO(PPPOESETPARMS, pppoediscparms);
   1255 	case PPPOEGETPARMS32:
   1256 		IOCTL_STRUCT_CONV_TO(PPPOEGETPARMS, pppoediscparms);
   1257 	case SPPPGETAUTHCFG32:
   1258 		IOCTL_STRUCT_CONV_TO(SPPPGETAUTHCFG, spppauthcfg);
   1259 	case SPPPSETAUTHCFG32:
   1260 		IOCTL_STRUCT_CONV_TO(SPPPSETAUTHCFG, spppauthcfg);
   1261 
   1262 	case SIOCSDRVSPEC32:
   1263 		IOCTL_STRUCT_CONV_TO(SIOCSDRVSPEC, ifdrv);
   1264 	case SIOCGDRVSPEC32:
   1265 		IOCTL_STRUCT_CONV_TO(SIOCGDRVSPEC, ifdrv);
   1266 
   1267 	case SIOCGETVIFCNT32:
   1268 		IOCTL_STRUCT_CONV_TO(SIOCGETVIFCNT, sioc_vif_req);
   1269 
   1270 	case SIOCGETSGCNT32:
   1271 		IOCTL_STRUCT_CONV_TO(SIOCGETSGCNT, sioc_sg_req);
   1272 
   1273 	case VNDIOCSET32:
   1274 		IOCTL_STRUCT_CONV_TO(VNDIOCSET, vnd_ioctl);
   1275 
   1276 	case VNDIOCCLR32:
   1277 		IOCTL_STRUCT_CONV_TO(VNDIOCCLR, vnd_ioctl);
   1278 
   1279 	case VNDIOCGET32:
   1280 		IOCTL_STRUCT_CONV_TO(VNDIOCGET, vnd_user);
   1281 
   1282 	case VNDIOCSET5032:
   1283 		IOCTL_STRUCT_CONV_TO(VNDIOCSET50, vnd_ioctl50);
   1284 
   1285 	case VNDIOCCLR5032:
   1286 		IOCTL_STRUCT_CONV_TO(VNDIOCCLR50, vnd_ioctl50);
   1287 
   1288 	case ENVSYS_GETDICTIONARY32:
   1289 		IOCTL_STRUCT_CONV_TO(ENVSYS_GETDICTIONARY, plistref);
   1290 	case ENVSYS_SETDICTIONARY32:
   1291 		IOCTL_STRUCT_CONV_TO(ENVSYS_SETDICTIONARY, plistref);
   1292 	case ENVSYS_REMOVEPROPS32:
   1293 		IOCTL_STRUCT_CONV_TO(ENVSYS_REMOVEPROPS, plistref);
   1294 
   1295 	case WDOGIOC_GWDOGS32:
   1296 		IOCTL_STRUCT_CONV_TO(WDOGIOC_GWDOGS, wdog_conf);
   1297 
   1298 	case BIOCSETF32:
   1299 		IOCTL_STRUCT_CONV_TO(BIOCSETF, bpf_program);
   1300 	case BIOCSTCPF32:
   1301 		IOCTL_STRUCT_CONV_TO(BIOCSTCPF, bpf_program);
   1302 	case BIOCSUDPF32:
   1303 		IOCTL_STRUCT_CONV_TO(BIOCSUDPF, bpf_program);
   1304 	case BIOCGDLTLIST32:
   1305 		IOCTL_STRUCT_CONV_TO(BIOCGDLTLIST, bpf_dltlist);
   1306 	case BIOCSRTIMEOUT32:
   1307 #define netbsd32_to_timeval(s32p, p, cmd) netbsd32_to_timeval(s32p, p)
   1308 #define netbsd32_from_timeval(p, s32p, cmd) netbsd32_from_timeval(p, s32p)
   1309 		IOCTL_STRUCT_CONV_TO(BIOCSRTIMEOUT, timeval);
   1310 #undef netbsd32_to_timeval
   1311 #undef netbsd32_from_timeval
   1312 
   1313 	case WSDISPLAYIO_ADDSCREEN32:
   1314 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_ADDSCREEN, wsdisplay_addscreendata);
   1315 
   1316 	case WSDISPLAYIO_GCURSOR32:
   1317 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_GCURSOR, wsdisplay_cursor);
   1318 	case WSDISPLAYIO_SCURSOR32:
   1319 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_SCURSOR, wsdisplay_cursor);
   1320 
   1321 	case WSDISPLAYIO_GETCMAP32:
   1322 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_GETCMAP, wsdisplay_cmap);
   1323 	case WSDISPLAYIO_PUTCMAP32:
   1324 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_PUTCMAP, wsdisplay_cmap);
   1325 
   1326 	case SIOCS8021132:
   1327 		IOCTL_STRUCT_CONV_TO(SIOCS80211, ieee80211req);
   1328 	case SIOCG8021132:
   1329 		IOCTL_STRUCT_CONV_TO(SIOCG80211, ieee80211req);
   1330 	case SIOCS80211NWKEY32:
   1331 		IOCTL_STRUCT_CONV_TO(SIOCS80211NWKEY, ieee80211_nwkey);
   1332 	case SIOCG80211NWKEY32:
   1333 		IOCTL_STRUCT_CONV_TO(SIOCG80211NWKEY, ieee80211_nwkey);
   1334 
   1335 	case POWER_EVENT_RECVDICT32:
   1336 		IOCTL_STRUCT_CONV_TO(POWER_EVENT_RECVDICT, plistref);
   1337 
   1338 	case CLOCKCTL_SETTIMEOFDAY32:
   1339 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_SETTIMEOFDAY,
   1340 		    clockctl_settimeofday);
   1341 	case CLOCKCTL_ADJTIME32:
   1342 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_ADJTIME, clockctl_adjtime);
   1343 	case CLOCKCTL_CLOCK_SETTIME32:
   1344 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_CLOCK_SETTIME,
   1345 		    clockctl_clock_settime);
   1346 	case CLOCKCTL_NTP_ADJTIME32:
   1347 #ifdef NTP
   1348 		{
   1349 			size = IOCPARM_LEN(CLOCKCTL_NTP_ADJTIME);
   1350 			if (size > sizeof(stkbuf))
   1351 				data = memp = kmem_alloc(size, KM_SLEEP);
   1352 			else
   1353 				data = (void *)stkbuf;
   1354 
   1355 			netbsd32_to_clockctl_ntp_adjtime(
   1356 				(const struct netbsd32_clockctl_ntp_adjtime *)data32,
   1357 				(struct clockctl_ntp_adjtime *)data,
   1358 				CLOCKCTL_NTP_ADJTIME);
   1359 			error = netbsd32_do_clockctl_ntp_adjtime(
   1360 				(struct clockctl_ntp_adjtime *)data);
   1361 			netbsd32_from_clockctl_ntp_adjtime(
   1362 				(const struct clockctl_ntp_adjtime *)data,
   1363 				(struct netbsd32_clockctl_ntp_adjtime *)data32,
   1364 				CLOCKCTL_NTP_ADJTIME);
   1365 
   1366 			break;
   1367 		}
   1368 #else
   1369 		error = ENOTTY;
   1370 		break;
   1371 #endif /* NTP */
   1372 
   1373 	case KIOCGSYMBOL32:
   1374 		IOCTL_STRUCT_CONV_TO(KIOCGSYMBOL, ksyms_gsymbol);
   1375 	case KIOCGVALUE32:
   1376 		IOCTL_STRUCT_CONV_TO(KIOCGVALUE, ksyms_gvalue);
   1377 
   1378 	case IOC_NPF_LOAD32:
   1379 		IOCTL_STRUCT_CONV_TO(IOC_NPF_LOAD, plistref);
   1380 	case IOC_NPF_TABLE32:
   1381 		IOCTL_STRUCT_CONV_TO(IOC_NPF_TABLE, npf_ioctl_table);
   1382 	case IOC_NPF_STATS32:
   1383 		IOCTL_CONV_TO(IOC_NPF_STATS, voidp);
   1384 	case IOC_NPF_SAVE32:
   1385 		IOCTL_STRUCT_CONV_TO(IOC_NPF_SAVE, plistref);
   1386 	case IOC_NPF_RULE32:
   1387 		IOCTL_STRUCT_CONV_TO(IOC_NPF_RULE, plistref);
   1388 
   1389 	case DRVRESCANBUS32:
   1390 		IOCTL_STRUCT_CONV_TO(DRVRESCANBUS, devrescanargs);
   1391 	case DRVLISTDEV32:
   1392 		IOCTL_STRUCT_CONV_TO(DRVLISTDEV, devlistargs);
   1393 	case DRVCTLCOMMAND32:
   1394 		IOCTL_STRUCT_CONV_TO(DRVCTLCOMMAND, plistref);
   1395 	case DRVGETEVENT32:
   1396 		IOCTL_STRUCT_CONV_TO(DRVGETEVENT, plistref);
   1397 
   1398 	default:
   1399 #ifdef NETBSD32_MD_IOCTL
   1400 		error = netbsd32_md_ioctl(fp, com, data32, l);
   1401 #else
   1402 		error = (*fp->f_ops->fo_ioctl)(fp, com, data32);
   1403 #endif
   1404 		break;
   1405 	}
   1406 
   1407 	if (error == EPASSTHROUGH)
   1408 		error = ENOTTY;
   1409 
   1410 	/*
   1411 	 * Copy any data to user, size was
   1412 	 * already set and checked above.
   1413 	 */
   1414 	if (error == 0 && (com&IOC_OUT) && size32) {
   1415 		error = copyout(data32, SCARG_P32(uap, data), size32);
   1416 		ktrgenio(fd, UIO_READ, SCARG_P32(uap, data),
   1417 		    size32, error);
   1418 	}
   1419 
   1420  out:
   1421 	/* If we allocated data, free it here. */
   1422 	if (memp32)
   1423 		kmem_free(memp32, alloc_size32);
   1424 	if (memp)
   1425 		kmem_free(memp, size);
   1426 	fd_putfile(fd);
   1427 	return (error);
   1428 }
   1429