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