Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_ioctl.c revision 1.64.2.1
      1 /*	$NetBSD: netbsd32_ioctl.c,v 1.64.2.1 2012/05/23 10:07:54 yamt 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.64.2.1 2012/05/23 10:07:54 yamt 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 #include <sys/wdog.h>
     55 #include <sys/clockctl.h>
     56 
     57 #ifdef __sparc__
     58 #include <dev/sun/fbio.h>
     59 #include <machine/openpromio.h>
     60 #endif
     61 
     62 #include <net/if.h>
     63 #include <net/route.h>
     64 
     65 #include <net/bpf.h>
     66 #include <netinet/in.h>
     67 #include <netinet/in_var.h>
     68 #include <netinet/igmp.h>
     69 #include <netinet/igmp_var.h>
     70 #include <netinet/ip_mroute.h>
     71 
     72 #include <compat/sys/sockio.h>
     73 
     74 #include <compat/netbsd32/netbsd32.h>
     75 #include <compat/netbsd32/netbsd32_ioctl.h>
     76 #include <compat/netbsd32/netbsd32_syscallargs.h>
     77 
     78 #include <dev/vndvar.h>
     79 
     80 /* convert to/from different structures */
     81 
     82 static inline void
     83 netbsd32_to_partinfo(struct netbsd32_partinfo *s32p, struct partinfo *p, u_long cmd)
     84 {
     85 
     86 	p->disklab = (struct disklabel *)NETBSD32PTR64(s32p->disklab);
     87 	p->part = (struct partition *)NETBSD32PTR64(s32p->part);
     88 }
     89 
     90 #if 0
     91 static inline void
     92 netbsd32_to_format_op(struct netbsd32_format_op *s32p, struct format_op *p, u_long cmd)
     93 {
     94 
     95 	p->df_buf = (char *)NETBSD32PTR64(s32p->df_buf);
     96 	p->df_count = s32p->df_count;
     97 	p->df_startblk = s32p->df_startblk;
     98 	memcpy(p->df_reg, s32p->df_reg, sizeof(s32p->df_reg));
     99 }
    100 #endif
    101 
    102 static inline void
    103 netbsd32_to_ifreq(struct netbsd32_ifreq *s32p, struct ifreq *p, u_long cmd)
    104 {
    105 
    106 	memcpy(p, s32p, sizeof *s32p);
    107 	/*
    108 	 * XXX
    109 	 * struct ifreq says the same, but sometimes the ifr_data
    110 	 * union member needs to be converted to 64 bits... this
    111 	 * is very driver specific and so we ignore it for now..
    112 	 */
    113 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    114 		p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
    115 }
    116 
    117 static inline void
    118 netbsd32_to_oifreq(struct netbsd32_oifreq *s32p, struct oifreq *p, u_long cmd)
    119 {
    120 
    121 	memcpy(p, s32p, sizeof *s32p);
    122 	/*
    123 	 * XXX
    124 	 * struct ifreq says the same, but sometimes the ifr_data
    125 	 * union member needs to be converted to 64 bits... this
    126 	 * is very driver specific and so we ignore it for now..
    127 	 */
    128 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    129 		p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
    130 }
    131 
    132 static inline void
    133 netbsd32_to_if_addrprefreq(const struct netbsd32_if_addrprefreq *ifap32,
    134 	struct if_addrprefreq *ifap, u_long cmd)
    135 {
    136 	strlcpy(ifap->ifap_name, ifap32->ifap_name, sizeof(ifap->ifap_name));
    137 	ifap->ifap_preference = ifap32->ifap_preference;
    138 	memcpy(&ifap->ifap_addr, &ifap32->ifap_addr,
    139 	    max(ifap32->ifap_addr.ss_len, _SS_MAXSIZE));
    140 }
    141 
    142 static inline void
    143 netbsd32_to_ifconf(struct netbsd32_ifconf *s32p, struct ifconf *p, u_long cmd)
    144 {
    145 
    146 	p->ifc_len = s32p->ifc_len;
    147 	/* ifc_buf & ifc_req are the same size so this works */
    148 	p->ifc_buf = (void *)NETBSD32PTR64(s32p->ifc_buf);
    149 }
    150 
    151 static inline void
    152 netbsd32_to_ifmediareq(struct netbsd32_ifmediareq *s32p, struct ifmediareq *p, u_long cmd)
    153 {
    154 
    155 	memcpy(p, s32p, sizeof *s32p);
    156 	p->ifm_ulist = (int *)NETBSD32PTR64(s32p->ifm_ulist);
    157 }
    158 
    159 static inline void
    160 netbsd32_to_ifdrv(struct netbsd32_ifdrv *s32p, struct ifdrv *p, u_long cmd)
    161 {
    162 
    163 	memcpy(p, s32p, sizeof *s32p);
    164 	p->ifd_data = (void *)NETBSD32PTR64(s32p->ifd_data);
    165 }
    166 
    167 static inline void
    168 netbsd32_to_sioc_vif_req(struct netbsd32_sioc_vif_req *s32p, struct sioc_vif_req *p, u_long cmd)
    169 {
    170 
    171 	p->vifi = s32p->vifi;
    172 	p->icount = (u_long)s32p->icount;
    173 	p->ocount = (u_long)s32p->ocount;
    174 	p->ibytes = (u_long)s32p->ibytes;
    175 	p->obytes = (u_long)s32p->obytes;
    176 }
    177 
    178 static inline void
    179 netbsd32_to_sioc_sg_req(struct netbsd32_sioc_sg_req *s32p, struct sioc_sg_req *p, u_long cmd)
    180 {
    181 
    182 	p->src = s32p->src;
    183 	p->grp = s32p->grp;
    184 	p->pktcnt = (u_long)s32p->pktcnt;
    185 	p->bytecnt = (u_long)s32p->bytecnt;
    186 	p->wrong_if = (u_long)s32p->wrong_if;
    187 }
    188 
    189 static inline void
    190 netbsd32_to_vnd_ioctl(struct netbsd32_vnd_ioctl *s32p, struct vnd_ioctl *p, u_long cmd)
    191 {
    192 
    193 	p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
    194 	p->vnd_flags = s32p->vnd_flags;
    195 	p->vnd_geom = s32p->vnd_geom;
    196 	p->vnd_osize = s32p->vnd_osize;
    197 	p->vnd_size = s32p->vnd_size;
    198 }
    199 
    200 static inline void
    201 netbsd32_to_vnd_user(struct netbsd32_vnd_user *s32p, struct vnd_user *p, u_long cmd)
    202 {
    203 
    204 	p->vnu_unit = s32p->vnu_unit;
    205 	p->vnu_dev = s32p->vnu_dev;
    206 	p->vnu_ino = s32p->vnu_ino;
    207 }
    208 
    209 static inline void
    210 netbsd32_to_vnd_ioctl50(struct netbsd32_vnd_ioctl50 *s32p, struct vnd_ioctl50 *p, u_long cmd)
    211 {
    212 
    213 	p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
    214 	p->vnd_flags = s32p->vnd_flags;
    215 	p->vnd_geom = s32p->vnd_geom;
    216 	p->vnd_size = s32p->vnd_size;
    217 }
    218 
    219 static inline void
    220 netbsd32_to_plistref(struct netbsd32_plistref *s32p, struct plistref *p, u_long cmd)
    221 {
    222 
    223 	p->pref_plist = NETBSD32PTR64(s32p->pref_plist);
    224 	p->pref_len = s32p->pref_len;
    225 }
    226 
    227 static inline void
    228 netbsd32_to_u_long(netbsd32_u_long *s32p, u_long *p, u_long cmd)
    229 {
    230 
    231 	*p = (u_long)*s32p;
    232 }
    233 
    234 static inline void
    235 netbsd32_to_wdog_conf(struct netbsd32_wdog_conf *s32p, struct wdog_conf *p, u_long cmd)
    236 {
    237 
    238 	p->wc_names = (char *)NETBSD32PTR64(s32p->wc_names);
    239 	p->wc_count = s32p->wc_count;
    240 }
    241 
    242 static inline void
    243 netbsd32_to_bpf_program(struct netbsd32_bpf_program *s32p, struct bpf_program *p, u_long cmd)
    244 {
    245 
    246 	p->bf_insns = (void *)NETBSD32PTR64(s32p->bf_insns);
    247 	p->bf_len = s32p->bf_len;
    248 }
    249 
    250 static inline void
    251 netbsd32_to_bpf_dltlist(struct netbsd32_bpf_dltlist *s32p, struct bpf_dltlist *p, u_long cmd)
    252 {
    253 
    254 	p->bfl_list = (void *)NETBSD32PTR64(s32p->bfl_list);
    255 	p->bfl_len = s32p->bfl_len;
    256 }
    257 
    258 /* wsdisplay stuff */
    259 static inline void
    260 netbsd32_to_wsdisplay_addscreendata(struct netbsd32_wsdisplay_addscreendata *asd32,
    261 					       struct wsdisplay_addscreendata *asd,
    262 					       u_long cmd)
    263 {
    264 	asd->screentype = (char *)NETBSD32PTR64(asd32->screentype);
    265 	asd->emul = (char *)NETBSD32PTR64(asd32->emul);
    266 	asd->idx = asd32->idx;
    267 }
    268 
    269 static inline void
    270 netbsd32_to_ieee80211_nwkey(struct netbsd32_ieee80211_nwkey *nwk32,
    271 					       struct ieee80211_nwkey *nwk,
    272 					       u_long cmd)
    273 {
    274 	int i;
    275 
    276 	strncpy(nwk->i_name, nwk32->i_name, IFNAMSIZ);
    277 	nwk->i_wepon = nwk32->i_wepon;
    278 	nwk->i_defkid = nwk32->i_defkid;
    279 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    280 		nwk->i_key[i].i_keylen = nwk32->i_key[i].i_keylen;
    281 		nwk->i_key[i].i_keydat =
    282 		    NETBSD32PTR64(nwk32->i_key[i].i_keydat);
    283 	}
    284 }
    285 
    286 static inline void
    287 netbsd32_to_wsdisplay_cursor(struct netbsd32_wsdisplay_cursor *c32,
    288 					       struct wsdisplay_cursor *c,
    289 					       u_long cmd)
    290 {
    291 	c->which = c32->which;
    292 	c->enable = c32->enable;
    293 	c->pos.x = c32->pos.x;
    294 	c->pos.y = c32->pos.y;
    295 	c->hot.x = c32->hot.x;
    296 	c->hot.y = c32->hot.y;
    297 	c->size.x = c32->size.x;
    298 	c->size.y = c32->size.y;
    299 	c->cmap.index = c32->cmap.index;
    300 	c->cmap.count = c32->cmap.count;
    301 	c->cmap.red = NETBSD32PTR64(c32->cmap.red);
    302 	c->cmap.green = NETBSD32PTR64(c32->cmap.green);
    303 	c->cmap.blue = NETBSD32PTR64(c32->cmap.blue);
    304 	c->image = NETBSD32PTR64(c32->image);
    305 	c->mask = NETBSD32PTR64(c32->mask);
    306 }
    307 
    308 static inline void
    309 netbsd32_to_clockctl_settimeofday(
    310     const struct netbsd32_clockctl_settimeofday *s32p,
    311     struct clockctl_settimeofday *p,
    312     u_long cmd)
    313 {
    314 
    315 	p->tv = NETBSD32PTR64(s32p->tv);
    316 	p->tzp = NETBSD32PTR64(s32p->tzp);
    317 }
    318 
    319 static inline void
    320 netbsd32_to_clockctl_adjtime(
    321     const struct netbsd32_clockctl_adjtime *s32p,
    322     struct clockctl_adjtime *p,
    323     u_long cmd)
    324 {
    325 
    326 	p->delta = NETBSD32PTR64(s32p->delta);
    327 	p->olddelta = NETBSD32PTR64(s32p->olddelta);
    328 }
    329 
    330 static inline void
    331 netbsd32_to_clockctl_clock_settime(
    332     const struct netbsd32_clockctl_clock_settime *s32p,
    333     struct clockctl_clock_settime *p,
    334     u_long cmd)
    335 {
    336 
    337 	p->clock_id = s32p->clock_id;
    338 	p->tp = NETBSD32PTR64(s32p->tp);
    339 }
    340 
    341 static inline void
    342 netbsd32_to_clockctl_ntp_adjtime(
    343     const struct netbsd32_clockctl_ntp_adjtime *s32p,
    344     struct clockctl_ntp_adjtime *p,
    345     u_long cmd)
    346 {
    347 
    348 	p->tp = NETBSD32PTR64(s32p->tp);
    349 	p->retval = s32p->retval;
    350 }
    351 
    352 /*
    353  * handle ioctl conversions from 64-bit kernel -> netbsd32
    354  */
    355 
    356 static inline void
    357 netbsd32_from_partinfo(struct partinfo *p, struct netbsd32_partinfo *s32p, u_long cmd)
    358 {
    359 
    360 	NETBSD32PTR32(s32p->disklab, p->disklab);
    361 	NETBSD32PTR32(s32p->part, p->part);
    362 }
    363 
    364 #if 0
    365 static inline void
    366 netbsd32_from_format_op(struct format_op *p, struct netbsd32_format_op *s32p, u_long cmd)
    367 {
    368 
    369 /* filled in */
    370 #if 0
    371 	s32p->df_buf = (netbsd32_charp)p->df_buf;
    372 #endif
    373 	s32p->df_count = p->df_count;
    374 	s32p->df_startblk = p->df_startblk;
    375 	memcpy(s32p->df_reg, p->df_reg, sizeof(p->df_reg));
    376 }
    377 #endif
    378 
    379 static inline void
    380 netbsd32_from_ifreq(struct ifreq *p, struct netbsd32_ifreq *s32p, u_long cmd)
    381 {
    382 
    383 	/*
    384 	 * XXX
    385 	 * struct ifreq says the same, but sometimes the ifr_data
    386 	 * union member needs to be converted to 64 bits... this
    387 	 * is very driver specific and so we ignore it for now..
    388 	 */
    389 	memcpy(s32p, p, sizeof *s32p);
    390 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    391 		NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
    392 }
    393 
    394 static inline void
    395 netbsd32_from_oifreq(struct oifreq *p, struct netbsd32_oifreq *s32p, u_long cmd)
    396 {
    397 
    398 	/*
    399 	 * XXX
    400 	 * struct ifreq says the same, but sometimes the ifr_data
    401 	 * union member needs to be converted to 64 bits... this
    402 	 * is very driver specific and so we ignore it for now..
    403 	 */
    404 	memcpy(s32p, p, sizeof *s32p);
    405 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    406 		NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
    407 }
    408 
    409 static inline void
    410 netbsd32_from_if_addrprefreq(const struct if_addrprefreq *ifap,
    411 	struct netbsd32_if_addrprefreq *ifap32, u_long cmd)
    412 {
    413 	strlcpy(ifap32->ifap_name, ifap->ifap_name, sizeof(ifap32->ifap_name));
    414 	ifap32->ifap_preference = ifap->ifap_preference;
    415 	memcpy(&ifap32->ifap_addr, &ifap->ifap_addr,
    416 	    max(ifap->ifap_addr.ss_len, _SS_MAXSIZE));
    417 }
    418 
    419 static inline void
    420 netbsd32_from_ifconf(struct ifconf *p, struct netbsd32_ifconf *s32p, u_long cmd)
    421 {
    422 
    423 	s32p->ifc_len = p->ifc_len;
    424 	/* ifc_buf & ifc_req are the same size so this works */
    425 	NETBSD32PTR32(s32p->ifc_buf, p->ifc_buf);
    426 }
    427 
    428 static inline void
    429 netbsd32_from_ifmediareq(struct ifmediareq *p, struct netbsd32_ifmediareq *s32p, u_long cmd)
    430 {
    431 
    432 	memcpy(s32p, p, sizeof *p);
    433 /* filled in? */
    434 #if 0
    435 	s32p->ifm_ulist = (netbsd32_intp_t)p->ifm_ulist;
    436 #endif
    437 }
    438 
    439 static inline void
    440 netbsd32_from_ifdrv(struct ifdrv *p, struct netbsd32_ifdrv *s32p, u_long cmd)
    441 {
    442 
    443 	memcpy(s32p, p, sizeof *p);
    444 /* filled in? */
    445 #if 0
    446 	s32p->ifm_data = (netbsd32_u_longp_t)p->ifm_data;
    447 #endif
    448 }
    449 
    450 static inline void
    451 netbsd32_from_sioc_vif_req(struct sioc_vif_req *p, struct netbsd32_sioc_vif_req *s32p, u_long cmd)
    452 {
    453 
    454 	s32p->vifi = p->vifi;
    455 	s32p->icount = (netbsd32_u_long)p->icount;
    456 	s32p->ocount = (netbsd32_u_long)p->ocount;
    457 	s32p->ibytes = (netbsd32_u_long)p->ibytes;
    458 	s32p->obytes = (netbsd32_u_long)p->obytes;
    459 }
    460 
    461 static inline void
    462 netbsd32_from_sioc_sg_req(struct sioc_sg_req *p, struct netbsd32_sioc_sg_req *s32p, u_long cmd)
    463 {
    464 
    465 	s32p->src = p->src;
    466 	s32p->grp = p->grp;
    467 	s32p->pktcnt = (netbsd32_u_long)p->pktcnt;
    468 	s32p->bytecnt = (netbsd32_u_long)p->bytecnt;
    469 	s32p->wrong_if = (netbsd32_u_long)p->wrong_if;
    470 }
    471 
    472 static inline void
    473 netbsd32_from_vnd_ioctl(struct vnd_ioctl *p, struct netbsd32_vnd_ioctl *s32p, u_long cmd)
    474 {
    475 
    476 	s32p->vnd_flags = p->vnd_flags;
    477 	s32p->vnd_geom = p->vnd_geom;
    478 	s32p->vnd_osize = p->vnd_osize;
    479 	s32p->vnd_size = p->vnd_size;
    480 }
    481 
    482 static inline void
    483 netbsd32_from_vnd_user(struct vnd_user *p, struct netbsd32_vnd_user *s32p, u_long cmd)
    484 {
    485 
    486 	s32p->vnu_unit = p->vnu_unit;
    487 	s32p->vnu_dev = p->vnu_dev;
    488 	s32p->vnu_ino = p->vnu_ino;
    489 }
    490 
    491 static inline void
    492 netbsd32_from_vnd_ioctl50(struct vnd_ioctl50 *p, struct netbsd32_vnd_ioctl50 *s32p, u_long cmd)
    493 {
    494 
    495 	s32p->vnd_flags = p->vnd_flags;
    496 	s32p->vnd_geom = p->vnd_geom;
    497 	s32p->vnd_size = p->vnd_size;
    498 }
    499 
    500 static inline void
    501 netbsd32_from_plistref(struct plistref *p, struct netbsd32_plistref *s32p, u_long cmd)
    502 {
    503 
    504 	NETBSD32PTR32(s32p->pref_plist, p->pref_plist);
    505 	s32p->pref_len = p->pref_len;
    506 }
    507 
    508 static inline void
    509 netbsd32_from_wdog_conf(struct wdog_conf *p, struct netbsd32_wdog_conf *s32p, u_long cmd)
    510 {
    511 
    512 	NETBSD32PTR32(s32p->wc_names, p->wc_names);
    513 	s32p->wc_count = p->wc_count;
    514 }
    515 
    516 /* wsdisplay stuff */
    517 static inline void
    518 netbsd32_from_wsdisplay_addscreendata(struct wsdisplay_addscreendata *asd,
    519 					struct netbsd32_wsdisplay_addscreendata *asd32,
    520 					u_long cmd)
    521 {
    522 	NETBSD32PTR32(asd32->screentype, asd->screentype);
    523 	NETBSD32PTR32(asd32->emul, asd->emul);
    524 	asd32->idx = asd->idx;
    525 }
    526 
    527 static inline void
    528 netbsd32_from_wsdisplay_cursor(struct wsdisplay_cursor *c,
    529 					       struct netbsd32_wsdisplay_cursor *c32,
    530 					       u_long cmd)
    531 {
    532 	c32->which = c->which;
    533 	c32->enable = c->enable;
    534 	c32->pos.x = c->pos.x;
    535 	c32->pos.y = c->pos.y;
    536 	c32->hot.x = c->hot.x;
    537 	c32->hot.y = c->hot.y;
    538 	c32->size.x = c->size.x;
    539 	c32->size.y = c->size.y;
    540 	c32->cmap.index = c->cmap.index;
    541 	c32->cmap.count = c->cmap.count;
    542 	NETBSD32PTR32(c32->cmap.red, c->cmap.red);
    543 	NETBSD32PTR32(c32->cmap.green, c->cmap.green);
    544 	NETBSD32PTR32(c32->cmap.blue, c->cmap.blue);
    545 	NETBSD32PTR32(c32->image, c->image);
    546 	NETBSD32PTR32(c32->mask, c->mask);
    547 }
    548 
    549 static inline void
    550 netbsd32_from_ieee80211_nwkey(struct ieee80211_nwkey *nwk,
    551 				struct netbsd32_ieee80211_nwkey *nwk32,
    552 				u_long cmd)
    553 {
    554 	int i;
    555 
    556 	strncpy(nwk32->i_name, nwk->i_name, IFNAMSIZ);
    557 	nwk32->i_wepon = nwk->i_wepon;
    558 	nwk32->i_defkid = nwk->i_defkid;
    559 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    560 		nwk32->i_key[i].i_keylen = nwk->i_key[i].i_keylen;
    561 		NETBSD32PTR32(nwk32->i_key[i].i_keydat,
    562 				nwk->i_key[i].i_keydat);
    563 	}
    564 }
    565 
    566 static inline void
    567 netbsd32_from_bpf_program(struct bpf_program *p, struct netbsd32_bpf_program *s32p, u_long cmd)
    568 {
    569 
    570 	NETBSD32PTR32(s32p->bf_insns, p->bf_insns);
    571 	s32p->bf_len = p->bf_len;
    572 }
    573 
    574 static inline void
    575 netbsd32_from_bpf_dltlist(struct bpf_dltlist *p, struct netbsd32_bpf_dltlist *s32p, u_long cmd)
    576 {
    577 
    578 	NETBSD32PTR32(s32p->bfl_list, p->bfl_list);
    579 	s32p->bfl_len = p->bfl_len;
    580 }
    581 
    582 static inline void
    583 netbsd32_from_u_long(u_long *p, netbsd32_u_long *s32p, u_long cmd)
    584 {
    585 
    586 	*s32p = (netbsd32_u_long)*p;
    587 }
    588 
    589 static inline void
    590 netbsd32_from_clockctl_settimeofday(
    591     const struct clockctl_settimeofday *p,
    592     struct netbsd32_clockctl_settimeofday *s32p,
    593     u_long cmd)
    594 {
    595 
    596 	NETBSD32PTR32(s32p->tv, p->tv);
    597 	NETBSD32PTR32(s32p->tzp, p->tzp);
    598 }
    599 
    600 static inline void
    601 netbsd32_from_clockctl_adjtime(
    602     const struct clockctl_adjtime *p,
    603     struct netbsd32_clockctl_adjtime *s32p,
    604     u_long cmd)
    605 {
    606 
    607 	NETBSD32PTR32(s32p->delta, p->delta);
    608 	NETBSD32PTR32(s32p->olddelta, p->olddelta);
    609 }
    610 
    611 static inline void
    612 netbsd32_from_clockctl_clock_settime(
    613     const struct clockctl_clock_settime *p,
    614     struct netbsd32_clockctl_clock_settime *s32p,
    615     u_long cmd)
    616 {
    617 
    618 	s32p->clock_id = p->clock_id;
    619 	NETBSD32PTR32(s32p->tp, p->tp);
    620 }
    621 
    622 static inline void
    623 netbsd32_from_clockctl_ntp_adjtime(
    624     const struct clockctl_ntp_adjtime *p,
    625     struct netbsd32_clockctl_ntp_adjtime *s32p,
    626     u_long cmd)
    627 {
    628 
    629 	NETBSD32PTR32(s32p->tp, p->tp);
    630 	s32p->retval = p->retval;
    631 }
    632 
    633 /*
    634  * main ioctl syscall.
    635  *
    636  * ok, here we are in the biggy.  we have to do fix ups depending
    637  * on the ioctl command before and afterwards.
    638  */
    639 int
    640 netbsd32_ioctl(struct lwp *l, const struct netbsd32_ioctl_args *uap, register_t *retval)
    641 {
    642 	/* {
    643 		syscallarg(int) fd;
    644 		syscallarg(netbsd32_u_long) com;
    645 		syscallarg(netbsd32_voidp) data;
    646 	} */
    647 	struct proc *p = l->l_proc;
    648 	struct file *fp;
    649 	struct filedesc *fdp;
    650 	u_long com;
    651 	int error = 0;
    652 	size_t size;
    653 	size_t alloc_size32, size32;
    654 	void *data, *memp = NULL;
    655 	void *data32, *memp32 = NULL;
    656 	unsigned int fd;
    657 	fdfile_t *ff;
    658 	int tmp;
    659 #define STK_PARAMS	128
    660 	u_long stkbuf[STK_PARAMS/sizeof(u_long)];
    661 	u_long stkbuf32[STK_PARAMS/sizeof(u_long)];
    662 
    663 	/*
    664 	 * we need to translate some commands (_IOW) before calling sys_ioctl,
    665 	 * some after (_IOR), and some both (_IOWR).
    666 	 */
    667 #if 0
    668 	{
    669 		const char * const dirs[8] = {
    670 		    "NONE!", "VOID", "OUT", "VOID|OUT!", "IN", "VOID|IN!",
    671 		    "INOUT", "VOID|IN|OUT!"
    672 		};
    673 
    674 		printf("netbsd32_ioctl(%d, %x, %x): "
    675 		    "%s group %c base %d len %d\n",
    676 		    SCARG(uap, fd), SCARG(uap, com), SCARG(uap, data).i32,
    677 		    dirs[((SCARG(uap, com) & IOC_DIRMASK)>>29)],
    678 		    IOCGROUP(SCARG(uap, com)), IOCBASECMD(SCARG(uap, com)),
    679 		    IOCPARM_LEN(SCARG(uap, com)));
    680 	}
    681 #endif
    682 
    683 	memp = NULL;
    684 	memp32 = NULL;
    685 	alloc_size32 = 0;
    686 	size32 = 0;
    687 	size = 0;
    688 
    689 	fdp = p->p_fd;
    690 	fd = SCARG(uap, fd);
    691 	if ((fp = fd_getfile(fd)) == NULL)
    692 		return (EBADF);
    693 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    694 		error = EBADF;
    695 		goto out;
    696 	}
    697 
    698 	ff = fdp->fd_dt->dt_ff[SCARG(uap, fd)];
    699 	switch (com = SCARG(uap, com)) {
    700 	case FIOCLEX:
    701 		ff->ff_exclose = true;
    702 		fdp->fd_exclose = true;
    703 		goto out;
    704 
    705 	case FIONCLEX:
    706 		ff->ff_exclose = false;
    707 		goto out;
    708 	}
    709 
    710 	/*
    711 	 * Interpret high order word to find amount of data to be
    712 	 * copied to/from the user's address space.
    713 	 */
    714 	size32 = IOCPARM_LEN(com);
    715 	alloc_size32 = size32;
    716 
    717 	/*
    718 	 * The disklabel is now padded to a multiple of 8 bytes however the old
    719 	 * disklabel on 32bit platforms wasn't.  This leaves a difference in
    720 	 * size of 4 bytes between the two but are otherwise identical.
    721 	 * To deal with this, we allocate enough space for the new disklabel
    722 	 * but only copyin/out the smaller amount.
    723 	 */
    724 	if (IOCGROUP(com) == 'd') {
    725 		u_long ncom = com ^ (DIOCGDINFO ^ DIOCGDINFO32);
    726 		switch (ncom) {
    727 		case DIOCGDINFO:
    728 		case DIOCWDINFO:
    729 		case DIOCSDINFO:
    730 		case DIOCGDEFLABEL:
    731 			com = ncom;
    732 			if (IOCPARM_LEN(DIOCGDINFO32) < IOCPARM_LEN(DIOCGDINFO))
    733 				alloc_size32 = IOCPARM_LEN(DIOCGDINFO);
    734 			break;
    735 		}
    736 	}
    737 	if (alloc_size32 > IOCPARM_MAX) {
    738 		error = ENOTTY;
    739 		goto out;
    740 	}
    741 	if (alloc_size32 > sizeof(stkbuf)) {
    742 		memp32 = kmem_alloc(alloc_size32, KM_SLEEP);
    743 		data32 = memp32;
    744 	} else
    745 		data32 = (void *)stkbuf32;
    746 	if ((com >> IOCPARM_SHIFT) == 0)  {
    747 		/* UNIX-style ioctl. */
    748 		data32 = SCARG_P32(uap, data);
    749 	} else {
    750 		if (com&IOC_IN) {
    751 			if (size32) {
    752 				error = copyin(SCARG_P32(uap, data), data32,
    753 				    size32);
    754 				if (error) {
    755 					goto out;
    756 				}
    757 				/*
    758 				 * The data between size and alloc_size has
    759 				 * not been overwritten.  It shouldn't matter
    760 				 * but let's clear that anyway.
    761 				 */
    762 				if (__predict_false(size32 < alloc_size32)) {
    763 					memset((char *)data32+size32, 0,
    764 					    alloc_size32 - size32);
    765 				}
    766 				ktrgenio(fd, UIO_WRITE, SCARG_P32(uap, data),
    767 				    size32, 0);
    768 			} else
    769 				*(void **)data32 = SCARG_P32(uap, data);
    770 		} else if ((com&IOC_OUT) && size32) {
    771 			/*
    772 			 * Zero the buffer so the user always
    773 			 * gets back something deterministic.
    774 			 */
    775 			memset(data32, 0, alloc_size32);
    776 		} else if (com&IOC_VOID) {
    777 			*(void **)data32 = SCARG_P32(uap, data);
    778 		}
    779 	}
    780 
    781 	/*
    782 	 * convert various structures, pointers, and other objects that
    783 	 * change size from 32 bit -> 64 bit, for all ioctl commands.
    784 	 */
    785 	switch (SCARG(uap, com)) {
    786 	case FIONBIO:
    787 		mutex_enter(&fp->f_lock);
    788 		if ((tmp = *(int *)data32) != 0)
    789 			fp->f_flag |= FNONBLOCK;
    790 		else
    791 			fp->f_flag &= ~FNONBLOCK;
    792 		mutex_exit(&fp->f_lock);
    793 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (void *)&tmp);
    794 		break;
    795 
    796 	case FIOASYNC:
    797 		mutex_enter(&fp->f_lock);
    798 		if ((tmp = *(int *)data32) != 0)
    799 			fp->f_flag |= FASYNC;
    800 		else
    801 			fp->f_flag &= ~FASYNC;
    802 		mutex_exit(&fp->f_lock);
    803 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (void *)&tmp);
    804 		break;
    805 
    806 	case AUDIO_WSEEK32:
    807 		IOCTL_CONV_TO(AUDIO_WSEEK, u_long);
    808 
    809 	case DIOCGPART32:
    810 		IOCTL_STRUCT_CONV_TO(DIOCGPART, partinfo);
    811 #if 0	/* not implemented by anything */
    812 	case DIOCRFORMAT32:
    813 		IOCTL_STRUCT_CONV_TO(DIOCRFORMAT, format_op);
    814 	case DIOCWFORMAT32:
    815 		IOCTL_STRUCT_CONV_TO(DIOCWFORMAT, format_op);
    816 #endif
    817 
    818 /*
    819  * only a few ifreq syscalls need conversion and those are
    820  * all driver specific... XXX
    821  */
    822 #if 0
    823 	case SIOCGADDRROM3232:
    824 		IOCTL_STRUCT_CONV_TO(SIOCGADDRROM32, ifreq);
    825 	case SIOCGCHIPID32:
    826 		IOCTL_STRUCT_CONV_TO(SIOCGCHIPID, ifreq);
    827 	case SIOCSIFADDR32:
    828 		IOCTL_STRUCT_CONV_TO(SIOCSIFADDR, ifreq);
    829 	case OSIOCGIFADDR32:
    830 		IOCTL_STRUCT_CONV_TO(OSIOCGIFADDR, ifreq);
    831 	case SIOCGIFADDR32:
    832 		IOCTL_STRUCT_CONV_TO(SIOCGIFADDR, ifreq);
    833 	case SIOCSIFDSTADDR32:
    834 		IOCTL_STRUCT_CONV_TO(SIOCSIFDSTADDR, ifreq);
    835 	case OSIOCGIFDSTADDR32:
    836 		IOCTL_STRUCT_CONV_TO(OSIOCGIFDSTADDR, ifreq);
    837 	case SIOCGIFDSTADDR32:
    838 		IOCTL_STRUCT_CONV_TO(SIOCGIFDSTADDR, ifreq);
    839 	case OSIOCGIFBRDADDR32:
    840 		IOCTL_STRUCT_CONV_TO(OSIOCGIFBRDADDR, ifreq);
    841 	case SIOCGIFBRDADDR32:
    842 		IOCTL_STRUCT_CONV_TO(SIOCGIFBRDADDR, ifreq);
    843 	case SIOCSIFBRDADDR32:
    844 		IOCTL_STRUCT_CONV_TO(SIOCSIFBRDADDR, ifreq);
    845 	case OSIOCGIFNETMASK32:
    846 		IOCTL_STRUCT_CONV_TO(OSIOCGIFNETMASK, ifreq);
    847 	case SIOCGIFNETMASK32:
    848 		IOCTL_STRUCT_CONV_TO(SIOCGIFNETMASK, ifreq);
    849 	case SIOCSIFNETMASK32:
    850 		IOCTL_STRUCT_CONV_TO(SIOCSIFNETMASK, ifreq);
    851 	case SIOCGIFMETRIC32:
    852 		IOCTL_STRUCT_CONV_TO(SIOCGIFMETRIC, ifreq);
    853 	case SIOCSIFMETRIC32:
    854 		IOCTL_STRUCT_CONV_TO(SIOCSIFMETRIC, ifreq);
    855 	case SIOCDIFADDR32:
    856 		IOCTL_STRUCT_CONV_TO(SIOCDIFADDR, ifreq);
    857 	case SIOCADDMULTI32:
    858 		IOCTL_STRUCT_CONV_TO(SIOCADDMULTI, ifreq);
    859 	case SIOCDELMULTI32:
    860 		IOCTL_STRUCT_CONV_TO(SIOCDELMULTI, ifreq);
    861 	case SIOCSIFMEDIA32:
    862 		IOCTL_STRUCT_CONV_TO(SIOCSIFMEDIA, ifreq);
    863 	case SIOCSIFMTU32:
    864 		IOCTL_STRUCT_CONV_TO(SIOCSIFMTU, ifreq);
    865 	case SIOCGIFMTU32:
    866 		IOCTL_STRUCT_CONV_TO(SIOCGIFMTU, ifreq);
    867 	case BIOCGETIF32:
    868 		IOCTL_STRUCT_CONV_TO(BIOCGETIF, ifreq);
    869 	case BIOCSETIF32:
    870 		IOCTL_STRUCT_CONV_TO(BIOCSETIF, ifreq);
    871 	case SIOCPHASE132:
    872 		IOCTL_STRUCT_CONV_TO(SIOCPHASE1, ifreq);
    873 	case SIOCPHASE232:
    874 		IOCTL_STRUCT_CONV_TO(SIOCPHASE2, ifreq);
    875 #endif
    876 
    877 	case OOSIOCGIFCONF32:
    878 		IOCTL_STRUCT_CONV_TO(OOSIOCGIFCONF, ifconf);
    879 	case OSIOCGIFCONF32:
    880 		IOCTL_STRUCT_CONV_TO(OSIOCGIFCONF, ifconf);
    881 	case SIOCGIFCONF32:
    882 		IOCTL_STRUCT_CONV_TO(SIOCGIFCONF, ifconf);
    883 
    884 	case SIOCGIFFLAGS32:
    885 		IOCTL_STRUCT_CONV_TO(SIOCGIFFLAGS, ifreq);
    886 	case SIOCSIFFLAGS32:
    887 		IOCTL_STRUCT_CONV_TO(SIOCSIFFLAGS, ifreq);
    888 
    889 	case SIOCGIFADDRPREF32:
    890 		IOCTL_STRUCT_CONV_TO(SIOCGIFADDRPREF, if_addrprefreq);
    891 	case SIOCSIFADDRPREF32:
    892 		IOCTL_STRUCT_CONV_TO(SIOCSIFADDRPREF, if_addrprefreq);
    893 
    894 
    895 	case OSIOCGIFFLAGS32:
    896 		IOCTL_STRUCT_CONV_TO(OSIOCGIFFLAGS, oifreq);
    897 	case OSIOCSIFFLAGS32:
    898 		IOCTL_STRUCT_CONV_TO(OSIOCSIFFLAGS, oifreq);
    899 
    900 	case SIOCGIFMEDIA32:
    901 		IOCTL_STRUCT_CONV_TO(SIOCGIFMEDIA, ifmediareq);
    902 
    903 	case SIOCSDRVSPEC32:
    904 		IOCTL_STRUCT_CONV_TO(SIOCSDRVSPEC, ifdrv);
    905 
    906 	case SIOCGETVIFCNT32:
    907 		IOCTL_STRUCT_CONV_TO(SIOCGETVIFCNT, sioc_vif_req);
    908 
    909 	case SIOCGETSGCNT32:
    910 		IOCTL_STRUCT_CONV_TO(SIOCGETSGCNT, sioc_sg_req);
    911 
    912 	case VNDIOCSET32:
    913 		IOCTL_STRUCT_CONV_TO(VNDIOCSET, vnd_ioctl);
    914 
    915 	case VNDIOCCLR32:
    916 		IOCTL_STRUCT_CONV_TO(VNDIOCCLR, vnd_ioctl);
    917 
    918 	case VNDIOCGET32:
    919 		IOCTL_STRUCT_CONV_TO(VNDIOCGET, vnd_user);
    920 
    921 	case VNDIOCSET5032:
    922 		IOCTL_STRUCT_CONV_TO(VNDIOCSET50, vnd_ioctl50);
    923 
    924 	case VNDIOCCLR5032:
    925 		IOCTL_STRUCT_CONV_TO(VNDIOCCLR50, vnd_ioctl50);
    926 
    927 	case ENVSYS_GETDICTIONARY32:
    928 		IOCTL_STRUCT_CONV_TO(ENVSYS_GETDICTIONARY, plistref);
    929 	case ENVSYS_SETDICTIONARY32:
    930 		IOCTL_STRUCT_CONV_TO(ENVSYS_SETDICTIONARY, plistref);
    931 	case ENVSYS_REMOVEPROPS32:
    932 		IOCTL_STRUCT_CONV_TO(ENVSYS_REMOVEPROPS, plistref);
    933 
    934 	case WDOGIOC_GWDOGS32:
    935 		IOCTL_STRUCT_CONV_TO(WDOGIOC_GWDOGS, wdog_conf);
    936 
    937 	case BIOCSETF32:
    938 		IOCTL_STRUCT_CONV_TO(BIOCSETF, bpf_program);
    939 	case BIOCSTCPF32:
    940 		IOCTL_STRUCT_CONV_TO(BIOCSTCPF, bpf_program);
    941 	case BIOCSUDPF32:
    942 		IOCTL_STRUCT_CONV_TO(BIOCSUDPF, bpf_program);
    943 	case BIOCGDLTLIST32:
    944 		IOCTL_STRUCT_CONV_TO(BIOCGDLTLIST, bpf_dltlist);
    945 
    946 	case WSDISPLAYIO_ADDSCREEN32:
    947 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_ADDSCREEN, wsdisplay_addscreendata);
    948 
    949 	case WSDISPLAYIO_GCURSOR32:
    950 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_GCURSOR, wsdisplay_cursor);
    951 	case WSDISPLAYIO_SCURSOR32:
    952 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_SCURSOR, wsdisplay_cursor);
    953 
    954 	case SIOCS80211NWKEY32:
    955 		IOCTL_STRUCT_CONV_TO(SIOCS80211NWKEY, ieee80211_nwkey);
    956 	case SIOCG80211NWKEY32:
    957 		IOCTL_STRUCT_CONV_TO(SIOCG80211NWKEY, ieee80211_nwkey);
    958 
    959 	case POWER_EVENT_RECVDICT32:
    960 		IOCTL_STRUCT_CONV_TO(POWER_EVENT_RECVDICT, plistref);
    961 
    962 	case CLOCKCTL_SETTIMEOFDAY32:
    963 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_SETTIMEOFDAY,
    964 		    clockctl_settimeofday);
    965 	case CLOCKCTL_ADJTIME32:
    966 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_ADJTIME, clockctl_adjtime);
    967 	case CLOCKCTL_CLOCK_SETTIME32:
    968 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_CLOCK_SETTIME,
    969 		    clockctl_clock_settime);
    970 	case CLOCKCTL_NTP_ADJTIME32:
    971 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_NTP_ADJTIME,
    972 		    clockctl_ntp_adjtime);
    973 
    974 	default:
    975 #ifdef NETBSD32_MD_IOCTL
    976 		error = netbsd32_md_ioctl(fp, com, data32, l);
    977 #else
    978 		error = (*fp->f_ops->fo_ioctl)(fp, com, data32);
    979 #endif
    980 		break;
    981 	}
    982 
    983 	if (error == EPASSTHROUGH)
    984 		error = ENOTTY;
    985 
    986 	/*
    987 	 * Copy any data to user, size was
    988 	 * already set and checked above.
    989 	 */
    990 	if (error == 0 && (com&IOC_OUT) && size32) {
    991 		error = copyout(data32, SCARG_P32(uap, data), size32);
    992 		ktrgenio(fd, UIO_READ, SCARG_P32(uap, data),
    993 		    size32, error);
    994 	}
    995 
    996  out:
    997 	/* If we allocated data, free it here. */
    998 	if (memp32)
    999 		kmem_free(memp32, alloc_size32);
   1000 	if (memp)
   1001 		kmem_free(memp, size);
   1002 	fd_putfile(fd);
   1003 	return (error);
   1004 }
   1005