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