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