netbsd32_ioctl.c revision 1.38.6.1 1 /* $NetBSD: netbsd32_ioctl.c,v 1.38.6.1 2008/05/10 23:48:57 wrstuden 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 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 /*
32 * handle ioctl conversions from netbsd32 -> 64-bit kernel
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: netbsd32_ioctl.c,v 1.38.6.1 2008/05/10 23:48:57 wrstuden Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/filedesc.h>
41 #include <sys/ioctl.h>
42 #include <sys/file.h>
43 #include <sys/proc.h>
44 #include <sys/socketvar.h>
45 #include <sys/audioio.h>
46 #include <sys/disklabel.h>
47 #include <sys/dkio.h>
48 #include <sys/malloc.h>
49 #include <sys/sockio.h>
50 #include <sys/socket.h>
51 #include <sys/ttycom.h>
52 #include <sys/mount.h>
53 #include <sys/sa.h>
54 #include <sys/syscallargs.h>
55 #include <sys/ktrace.h>
56 #include <sys/kmem.h>
57
58 #ifdef __sparc__
59 #include <dev/sun/fbio.h>
60 #include <machine/openpromio.h>
61 #endif
62
63 #include <net/if.h>
64 #include <net/route.h>
65
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/igmp.h>
69 #include <netinet/igmp_var.h>
70 #include <netinet/ip_mroute.h>
71
72 #include <compat/sys/sockio.h>
73
74 #include <compat/netbsd32/netbsd32.h>
75 #include <compat/netbsd32/netbsd32_ioctl.h>
76 #include <compat/netbsd32/netbsd32_syscallargs.h>
77
78 /* prototypes for the converters */
79 static inline void netbsd32_to_partinfo(struct netbsd32_partinfo *,
80 struct partinfo *, u_long);
81 #if 0
82 static inline void netbsd32_to_format_op(struct netbsd32_format_op *,
83 struct format_op *, u_long);
84 #endif
85 static inline void netbsd32_to_ifreq(struct netbsd32_ifreq *, struct ifreq *,
86 u_long cmd);
87 static inline void netbsd32_to_ifconf(struct netbsd32_ifconf *,
88 struct ifconf *, u_long);
89 static inline void netbsd32_to_ifmediareq(struct netbsd32_ifmediareq *,
90 struct ifmediareq *, u_long);
91 static inline void netbsd32_to_ifdrv(struct netbsd32_ifdrv *, struct ifdrv *,
92 u_long);
93 static inline void netbsd32_to_sioc_vif_req(struct netbsd32_sioc_vif_req *,
94 struct sioc_vif_req *, u_long);
95 static inline void netbsd32_to_sioc_sg_req(struct netbsd32_sioc_sg_req *,
96 struct sioc_sg_req *, u_long);
97 static inline void netbsd32_from_partinfo(struct partinfo *,
98 struct netbsd32_partinfo *, u_long);
99 #if 0
100 static inline void netbsd32_from_format_op(struct format_op *,
101 struct netbsd32_format_op *,
102 u_long);
103 #endif
104 static inline void netbsd32_from_ifreq(struct ifreq *,
105 struct netbsd32_ifreq *, u_long);
106 static inline void netbsd32_from_ifconf(struct ifconf *,
107 struct netbsd32_ifconf *, u_long);
108 static inline void netbsd32_from_ifmediareq(struct ifmediareq *,
109 struct netbsd32_ifmediareq *,
110 u_long);
111 static inline void netbsd32_from_ifdrv(struct ifdrv *,
112 struct netbsd32_ifdrv *, u_long);
113 static inline void netbsd32_from_sioc_vif_req(struct sioc_vif_req *,
114 struct netbsd32_sioc_vif_req *,
115 u_long);
116 static inline void netbsd32_from_sioc_sg_req(struct sioc_sg_req *,
117 struct netbsd32_sioc_sg_req *,
118 u_long);
119
120 /* convert to/from different structures */
121
122 static inline void
123 netbsd32_to_partinfo(struct netbsd32_partinfo *s32p, struct partinfo *p, u_long cmd)
124 {
125
126 p->disklab = (struct disklabel *)NETBSD32PTR64(s32p->disklab);
127 p->part = (struct partition *)NETBSD32PTR64(s32p->part);
128 }
129
130 #if 0
131 static inline void
132 netbsd32_to_format_op(struct netbsd32_format_op *s32p, struct format_op *p, u_long cmd)
133 {
134
135 p->df_buf = (char *)NETBSD32PTR64(s32p->df_buf);
136 p->df_count = s32p->df_count;
137 p->df_startblk = s32p->df_startblk;
138 memcpy(p->df_reg, s32p->df_reg, sizeof(s32p->df_reg));
139 }
140 #endif
141
142 static inline void
143 netbsd32_to_ifreq(struct netbsd32_ifreq *s32p, struct ifreq *p, u_long cmd)
144 {
145
146 memcpy(p, s32p, sizeof *s32p);
147 /*
148 * XXX
149 * struct ifreq says the same, but sometimes the ifr_data
150 * union member needs to be converted to 64 bits... this
151 * is very driver specific and so we ignore it for now..
152 */
153 if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
154 p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
155 }
156
157 static inline void
158 netbsd32_to_ifconf(struct netbsd32_ifconf *s32p, struct ifconf *p, u_long cmd)
159 {
160
161 p->ifc_len = s32p->ifc_len;
162 /* ifc_buf & ifc_req are the same size so this works */
163 p->ifc_buf = (void *)NETBSD32PTR64(s32p->ifc_buf);
164 }
165
166 static inline void
167 netbsd32_to_ifmediareq(struct netbsd32_ifmediareq *s32p, 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_ifdrv(struct netbsd32_ifdrv *s32p, struct ifdrv *p, u_long cmd)
176 {
177
178 memcpy(p, s32p, sizeof *s32p);
179 p->ifd_data = (void *)NETBSD32PTR64(s32p->ifd_data);
180 }
181
182 static inline void
183 netbsd32_to_sioc_vif_req(struct netbsd32_sioc_vif_req *s32p, struct sioc_vif_req *p, u_long cmd)
184 {
185
186 p->vifi = s32p->vifi;
187 p->icount = (u_long)s32p->icount;
188 p->ocount = (u_long)s32p->ocount;
189 p->ibytes = (u_long)s32p->ibytes;
190 p->obytes = (u_long)s32p->obytes;
191 }
192
193 static inline void
194 netbsd32_to_sioc_sg_req(struct netbsd32_sioc_sg_req *s32p, struct sioc_sg_req *p, u_long cmd)
195 {
196
197 p->src = s32p->src;
198 p->grp = s32p->grp;
199 p->pktcnt = (u_long)s32p->pktcnt;
200 p->bytecnt = (u_long)s32p->bytecnt;
201 p->wrong_if = (u_long)s32p->wrong_if;
202 }
203
204 /*
205 * handle ioctl conversions from 64-bit kernel -> netbsd32
206 */
207
208 static inline void
209 netbsd32_from_partinfo(struct partinfo *p, struct netbsd32_partinfo *s32p, u_long cmd)
210 {
211
212 NETBSD32PTR32(s32p->disklab, p->disklab);
213 NETBSD32PTR32(s32p->part, p->part);
214 }
215
216 #if 0
217 static inline void
218 netbsd32_from_format_op(struct format_op *p, struct netbsd32_format_op *s32p, u_long cmd)
219 {
220
221 /* filled in */
222 #if 0
223 s32p->df_buf = (netbsd32_charp)p->df_buf;
224 #endif
225 s32p->df_count = p->df_count;
226 s32p->df_startblk = p->df_startblk;
227 memcpy(s32p->df_reg, p->df_reg, sizeof(p->df_reg));
228 }
229 #endif
230
231 static inline void
232 netbsd32_from_ifreq(struct ifreq *p, struct netbsd32_ifreq *s32p, u_long cmd)
233 {
234
235 /*
236 * XXX
237 * struct ifreq says the same, but sometimes the ifr_data
238 * union member needs to be converted to 64 bits... this
239 * is very driver specific and so we ignore it for now..
240 */
241 *s32p->ifr_name = *p->ifr_name;
242 if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
243 NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
244 }
245
246 static inline void
247 netbsd32_from_ifconf(struct ifconf *p, struct netbsd32_ifconf *s32p, u_long cmd)
248 {
249
250 s32p->ifc_len = p->ifc_len;
251 /* ifc_buf & ifc_req are the same size so this works */
252 NETBSD32PTR32(s32p->ifc_buf, p->ifc_buf);
253 }
254
255 static inline void
256 netbsd32_from_ifmediareq(struct ifmediareq *p, struct netbsd32_ifmediareq *s32p, u_long cmd)
257 {
258
259 memcpy(s32p, p, sizeof *p);
260 /* filled in? */
261 #if 0
262 s32p->ifm_ulist = (netbsd32_intp_t)p->ifm_ulist;
263 #endif
264 }
265
266 static inline void
267 netbsd32_from_ifdrv(struct ifdrv *p, struct netbsd32_ifdrv *s32p, u_long cmd)
268 {
269
270 memcpy(s32p, p, sizeof *p);
271 /* filled in? */
272 #if 0
273 s32p->ifm_data = (netbsd32_u_longp_t)p->ifm_data;
274 #endif
275 }
276
277 static inline void
278 netbsd32_from_sioc_vif_req(struct sioc_vif_req *p, struct netbsd32_sioc_vif_req *s32p, u_long cmd)
279 {
280
281 s32p->vifi = p->vifi;
282 s32p->icount = (netbsd32_u_long)p->icount;
283 s32p->ocount = (netbsd32_u_long)p->ocount;
284 s32p->ibytes = (netbsd32_u_long)p->ibytes;
285 s32p->obytes = (netbsd32_u_long)p->obytes;
286 }
287
288 static inline void
289 netbsd32_from_sioc_sg_req(struct sioc_sg_req *p, struct netbsd32_sioc_sg_req *s32p, u_long cmd)
290 {
291
292 s32p->src = p->src;
293 s32p->grp = p->grp;
294 s32p->pktcnt = (netbsd32_u_long)p->pktcnt;
295 s32p->bytecnt = (netbsd32_u_long)p->bytecnt;
296 s32p->wrong_if = (netbsd32_u_long)p->wrong_if;
297 }
298
299
300 /*
301 * main ioctl syscall.
302 *
303 * ok, here we are in the biggy. we have to do fix ups depending
304 * on the ioctl command before and afterwards.
305 */
306 int
307 netbsd32_ioctl(struct lwp *l, const struct netbsd32_ioctl_args *uap, register_t *retval)
308 {
309 /* {
310 syscallarg(int) fd;
311 syscallarg(netbsd32_u_long) com;
312 syscallarg(netbsd32_voidp) data;
313 } */
314 struct proc *p = l->l_proc;
315 struct file *fp;
316 struct filedesc *fdp;
317 u_long com;
318 int error = 0;
319 u_int size, size32;
320 void *data, *memp = NULL;
321 void *data32, *memp32 = NULL;
322 unsigned fd;
323 fdfile_t *ff;
324 int tmp;
325 #define STK_PARAMS 128
326 u_long stkbuf[STK_PARAMS/sizeof(u_long)];
327 u_long stkbuf32[STK_PARAMS/sizeof(u_long)];
328
329 /*
330 * we need to translate some commands (_IOW) before calling sys_ioctl,
331 * some after (_IOR), and some both (_IOWR).
332 */
333 #if 0
334 {
335 char *dirs[8] = { "NONE!", "VOID", "OUT", "VOID|OUT!", "IN", "VOID|IN!",
336 "INOUT", "VOID|IN|OUT!" };
337
338 printf("netbsd32_ioctl(%d, %x, %x): %s group %c base %d len %d\n",
339 SCARG(uap, fd), SCARG(uap, com), SCARG(uap, data),
340 dirs[((SCARG(uap, com) & IOC_DIRMASK)>>29)],
341 IOCGROUP(SCARG(uap, com)), IOCBASECMD(SCARG(uap, com)),
342 IOCPARM_LEN(SCARG(uap, com)));
343 }
344 #endif
345
346 fdp = p->p_fd;
347 fd = SCARG(uap, fd);
348 if ((fp = fd_getfile(fd)) == NULL)
349 return (EBADF);
350 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
351 error = EBADF;
352 goto out;
353 }
354
355 ff = fdp->fd_ofiles[SCARG(uap, fd)];
356 switch (com = SCARG(uap, com)) {
357 case FIONCLEX:
358 ff->ff_exclose = 0;
359 goto out;
360
361 case FIOCLEX:
362 ff->ff_exclose = 1;
363 fdp->fd_exclose = 1;
364 goto out;
365 }
366
367 /*
368 * Interpret high order word to find amount of data to be
369 * copied to/from the user's address space.
370 */
371 size = 0;
372 size32 = IOCPARM_LEN(com);
373 if (size32 > IOCPARM_MAX) {
374 error = ENOTTY;
375 goto out;
376 }
377 if (size32 > sizeof(stkbuf)) {
378 memp32 = kmem_alloc((size_t)size32, KM_SLEEP);
379 data32 = memp32;
380 } else
381 data32 = (void *)stkbuf32;
382 if (com&IOC_IN) {
383 if (size32) {
384 error = copyin(SCARG_P32(uap, data), data32, size32);
385 if (error) {
386 if (memp32)
387 kmem_free(memp32, (size_t)size32);
388 goto out;
389 }
390 ktrgenio(fd, UIO_WRITE, SCARG_P32(uap, data),
391 size32, 0);
392 } else
393 *(void **)data32 = SCARG_P32(uap, data);
394 } else if ((com&IOC_OUT) && size32)
395 /*
396 * Zero the buffer so the user always
397 * gets back something deterministic.
398 */
399 memset(data32, 0, size32);
400 else if (com&IOC_VOID)
401 *(void **)data32 = SCARG_P32(uap, data);
402
403 /*
404 * convert various structures, pointers, and other objects that
405 * change size from 32 bit -> 64 bit, for all ioctl commands.
406 */
407 switch (SCARG(uap, com)) {
408 case FIONBIO:
409 mutex_enter(&fp->f_lock);
410 if ((tmp = *(int *)data32) != 0)
411 fp->f_flag |= FNONBLOCK;
412 else
413 fp->f_flag &= ~FNONBLOCK;
414 mutex_exit(&fp->f_lock);
415 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (void *)&tmp);
416 break;
417
418 case FIOASYNC:
419 mutex_enter(&fp->f_lock);
420 if ((tmp = *(int *)data32) != 0)
421 fp->f_flag |= FASYNC;
422 else
423 fp->f_flag &= ~FASYNC;
424 mutex_exit(&fp->f_lock);
425 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (void *)&tmp);
426 break;
427
428 case DIOCGPART32:
429 IOCTL_STRUCT_CONV_TO(DIOCGPART, partinfo);
430 #if 0 /* not implemented by anything */
431 case DIOCRFORMAT32:
432 IOCTL_STRUCT_CONV_TO(DIOCRFORMAT, format_op);
433 case DIOCWFORMAT32:
434 IOCTL_STRUCT_CONV_TO(DIOCWFORMAT, format_op);
435 #endif
436
437 /*
438 * only a few ifreq syscalls need conversion and those are
439 * all driver specific... XXX
440 */
441 #if 0
442 case SIOCGADDRROM3232:
443 IOCTL_STRUCT_CONV_TO(SIOCGADDRROM32, ifreq);
444 case SIOCGCHIPID32:
445 IOCTL_STRUCT_CONV_TO(SIOCGCHIPID, ifreq);
446 case SIOCSIFADDR32:
447 IOCTL_STRUCT_CONV_TO(SIOCSIFADDR, ifreq);
448 case OSIOCGIFADDR32:
449 IOCTL_STRUCT_CONV_TO(OSIOCGIFADDR, ifreq);
450 case SIOCGIFADDR32:
451 IOCTL_STRUCT_CONV_TO(SIOCGIFADDR, ifreq);
452 case SIOCSIFDSTADDR32:
453 IOCTL_STRUCT_CONV_TO(SIOCSIFDSTADDR, ifreq);
454 case OSIOCGIFDSTADDR32:
455 IOCTL_STRUCT_CONV_TO(OSIOCGIFDSTADDR, ifreq);
456 case SIOCGIFDSTADDR32:
457 IOCTL_STRUCT_CONV_TO(SIOCGIFDSTADDR, ifreq);
458 case OSIOCGIFBRDADDR32:
459 IOCTL_STRUCT_CONV_TO(OSIOCGIFBRDADDR, ifreq);
460 case SIOCGIFBRDADDR32:
461 IOCTL_STRUCT_CONV_TO(SIOCGIFBRDADDR, ifreq);
462 case SIOCSIFBRDADDR32:
463 IOCTL_STRUCT_CONV_TO(SIOCSIFBRDADDR, ifreq);
464 case OSIOCGIFNETMASK32:
465 IOCTL_STRUCT_CONV_TO(OSIOCGIFNETMASK, ifreq);
466 case SIOCGIFNETMASK32:
467 IOCTL_STRUCT_CONV_TO(SIOCGIFNETMASK, ifreq);
468 case SIOCSIFNETMASK32:
469 IOCTL_STRUCT_CONV_TO(SIOCSIFNETMASK, ifreq);
470 case SIOCGIFMETRIC32:
471 IOCTL_STRUCT_CONV_TO(SIOCGIFMETRIC, ifreq);
472 case SIOCSIFMETRIC32:
473 IOCTL_STRUCT_CONV_TO(SIOCSIFMETRIC, ifreq);
474 case SIOCDIFADDR32:
475 IOCTL_STRUCT_CONV_TO(SIOCDIFADDR, ifreq);
476 case SIOCADDMULTI32:
477 IOCTL_STRUCT_CONV_TO(SIOCADDMULTI, ifreq);
478 case SIOCDELMULTI32:
479 IOCTL_STRUCT_CONV_TO(SIOCDELMULTI, ifreq);
480 case SIOCSIFMEDIA32:
481 IOCTL_STRUCT_CONV_TO(SIOCSIFMEDIA, ifreq);
482 case SIOCSIFMTU32:
483 IOCTL_STRUCT_CONV_TO(SIOCSIFMTU, ifreq);
484 case SIOCGIFMTU32:
485 IOCTL_STRUCT_CONV_TO(SIOCGIFMTU, ifreq);
486 case BIOCGETIF32:
487 IOCTL_STRUCT_CONV_TO(BIOCGETIF, ifreq);
488 case BIOCSETIF32:
489 IOCTL_STRUCT_CONV_TO(BIOCSETIF, ifreq);
490 case SIOCPHASE132:
491 IOCTL_STRUCT_CONV_TO(SIOCPHASE1, ifreq);
492 case SIOCPHASE232:
493 IOCTL_STRUCT_CONV_TO(SIOCPHASE2, ifreq);
494 #endif
495
496 case OSIOCGIFCONF32:
497 IOCTL_STRUCT_CONV_TO(OSIOCGIFCONF, ifconf);
498 case SIOCGIFCONF32:
499 IOCTL_STRUCT_CONV_TO(SIOCGIFCONF, ifconf);
500
501 case SIOCGIFFLAGS32:
502 IOCTL_STRUCT_CONV_TO(SIOCGIFFLAGS, ifreq);
503 case SIOCSIFFLAGS32:
504 IOCTL_STRUCT_CONV_TO(SIOCSIFFLAGS, ifreq);
505
506 case SIOCGIFMEDIA32:
507 IOCTL_STRUCT_CONV_TO(SIOCGIFMEDIA, ifmediareq);
508
509 case SIOCSDRVSPEC32:
510 IOCTL_STRUCT_CONV_TO(SIOCSDRVSPEC, ifdrv);
511
512 case SIOCGETVIFCNT32:
513 IOCTL_STRUCT_CONV_TO(SIOCGETVIFCNT, sioc_vif_req);
514
515 case SIOCGETSGCNT32:
516 IOCTL_STRUCT_CONV_TO(SIOCGETSGCNT, sioc_sg_req);
517
518 default:
519 #ifdef NETBSD32_MD_IOCTL
520 error = netbsd32_md_ioctl(fp, com, data32, l);
521 #else
522 error = (*fp->f_ops->fo_ioctl)(fp, com, data32);
523 #endif
524 break;
525 }
526
527 if (error == EPASSTHROUGH)
528 error = ENOTTY;
529
530 /*
531 * Copy any data to user, size was
532 * already set and checked above.
533 */
534 if (error == 0 && (com&IOC_OUT) && size32) {
535 error = copyout(data32, SCARG_P32(uap, data), size32);
536 ktrgenio(fd, UIO_READ, SCARG_P32(uap, data),
537 size32, error);
538 }
539
540 /* if we malloced data, free it here */
541 if (memp32)
542 kmem_free(memp32, (size_t)size32);
543 if (memp)
544 kmem_free(memp, (size_t)size);
545 out:
546 fd_putfile(fd);
547 return (error);
548 }
549