netbsd32_ioctl.c revision 1.29 1 /* $NetBSD: netbsd32_ioctl.c,v 1.29 2007/03/04 06:01:26 christos 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.29 2007/03/04 06:01:26 christos 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/syscallargs.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/netbsd32/netbsd32.h>
70 #include <compat/netbsd32/netbsd32_ioctl.h>
71 #include <compat/netbsd32/netbsd32_syscallargs.h>
72
73 /* prototypes for the converters */
74 static inline void netbsd32_to_partinfo(struct netbsd32_partinfo *,
75 struct partinfo *, u_long);
76 #if 0
77 static inline void netbsd32_to_format_op(struct netbsd32_format_op *,
78 struct format_op *, u_long);
79 #endif
80 static inline void netbsd32_to_ifreq(struct netbsd32_ifreq *, struct ifreq *,
81 u_long cmd);
82 static inline void netbsd32_to_ifconf(struct netbsd32_ifconf *,
83 struct ifconf *, u_long);
84 static inline void netbsd32_to_ifmediareq(struct netbsd32_ifmediareq *,
85 struct ifmediareq *, u_long);
86 static inline void netbsd32_to_ifdrv(struct netbsd32_ifdrv *, struct ifdrv *,
87 u_long);
88 static inline void netbsd32_to_sioc_vif_req(struct netbsd32_sioc_vif_req *,
89 struct sioc_vif_req *, u_long);
90 static inline void netbsd32_to_sioc_sg_req(struct netbsd32_sioc_sg_req *,
91 struct sioc_sg_req *, u_long);
92 static inline void netbsd32_from_partinfo(struct partinfo *,
93 struct netbsd32_partinfo *, u_long);
94 #if 0
95 static inline void netbsd32_from_format_op(struct format_op *,
96 struct netbsd32_format_op *,
97 u_long);
98 #endif
99 static inline void netbsd32_from_ifreq(struct ifreq *,
100 struct netbsd32_ifreq *, u_long);
101 static inline void netbsd32_from_ifconf(struct ifconf *,
102 struct netbsd32_ifconf *, u_long);
103 static inline void netbsd32_from_ifmediareq(struct ifmediareq *,
104 struct netbsd32_ifmediareq *,
105 u_long);
106 static inline void netbsd32_from_ifdrv(struct ifdrv *,
107 struct netbsd32_ifdrv *, u_long);
108 static inline void netbsd32_from_sioc_vif_req(struct sioc_vif_req *,
109 struct netbsd32_sioc_vif_req *,
110 u_long);
111 static inline void netbsd32_from_sioc_sg_req(struct sioc_sg_req *,
112 struct netbsd32_sioc_sg_req *,
113 u_long);
114
115 /* convert to/from different structures */
116
117 static inline void
118 netbsd32_to_partinfo(s32p, p, cmd)
119 struct netbsd32_partinfo *s32p;
120 struct partinfo *p;
121 u_long cmd;
122 {
123
124 p->disklab = (struct disklabel *)NETBSD32PTR64(s32p->disklab);
125 p->part = (struct partition *)NETBSD32PTR64(s32p->part);
126 }
127
128 #if 0
129 static inline void
130 netbsd32_to_format_op(s32p, p, cmd)
131 struct netbsd32_format_op *s32p;
132 struct format_op *p;
133 u_long cmd;
134 {
135
136 p->df_buf = (char *)NETBSD32PTR64(s32p->df_buf);
137 p->df_count = s32p->df_count;
138 p->df_startblk = s32p->df_startblk;
139 memcpy(p->df_reg, s32p->df_reg, sizeof(s32p->df_reg));
140 }
141 #endif
142
143 static inline void
144 netbsd32_to_ifreq(s32p, p, cmd)
145 struct netbsd32_ifreq *s32p;
146 struct ifreq *p;
147 u_long cmd;
148 {
149
150 memcpy(p, s32p, sizeof *s32p);
151 /*
152 * XXX
153 * struct ifreq says the same, but sometimes the ifr_data
154 * union member needs to be converted to 64 bits... this
155 * is very driver specific and so we ignore it for now..
156 */
157 if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
158 p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
159 }
160
161 static inline void
162 netbsd32_to_ifconf(s32p, p, cmd)
163 struct netbsd32_ifconf *s32p;
164 struct ifconf *p;
165 u_long cmd;
166 {
167
168 p->ifc_len = s32p->ifc_len;
169 /* ifc_buf & ifc_req are the same size so this works */
170 p->ifc_buf = (void *)NETBSD32PTR64(s32p->ifc_buf);
171 }
172
173 static inline void
174 netbsd32_to_ifmediareq(s32p, p, cmd)
175 struct netbsd32_ifmediareq *s32p;
176 struct ifmediareq *p;
177 u_long cmd;
178 {
179
180 memcpy(p, s32p, sizeof *s32p);
181 p->ifm_ulist = (int *)NETBSD32PTR64(s32p->ifm_ulist);
182 }
183
184 static inline void
185 netbsd32_to_ifdrv(s32p, p, cmd)
186 struct netbsd32_ifdrv *s32p;
187 struct ifdrv *p;
188 u_long cmd;
189 {
190
191 memcpy(p, s32p, sizeof *s32p);
192 p->ifd_data = (void *)NETBSD32PTR64(s32p->ifd_data);
193 }
194
195 static inline void
196 netbsd32_to_sioc_vif_req(s32p, p, cmd)
197 struct netbsd32_sioc_vif_req *s32p;
198 struct sioc_vif_req *p;
199 u_long cmd;
200 {
201
202 p->vifi = s32p->vifi;
203 p->icount = (u_long)s32p->icount;
204 p->ocount = (u_long)s32p->ocount;
205 p->ibytes = (u_long)s32p->ibytes;
206 p->obytes = (u_long)s32p->obytes;
207 }
208
209 static inline void
210 netbsd32_to_sioc_sg_req(s32p, p, cmd)
211 struct netbsd32_sioc_sg_req *s32p;
212 struct sioc_sg_req *p;
213 u_long cmd;
214 {
215
216 p->src = s32p->src;
217 p->grp = s32p->grp;
218 p->pktcnt = (u_long)s32p->pktcnt;
219 p->bytecnt = (u_long)s32p->bytecnt;
220 p->wrong_if = (u_long)s32p->wrong_if;
221 }
222
223 /*
224 * handle ioctl conversions from 64-bit kernel -> netbsd32
225 */
226
227 static inline void
228 netbsd32_from_partinfo(p, s32p, cmd)
229 struct partinfo *p;
230 struct netbsd32_partinfo *s32p;
231 u_long cmd;
232 {
233
234 s32p->disklab = (netbsd32_disklabel_tp_t)(u_long)p->disklab;
235 s32p->part = s32p->part;
236 }
237
238 #if 0
239 static inline void
240 netbsd32_from_format_op(p, s32p, cmd)
241 struct format_op *p;
242 struct netbsd32_format_op *s32p;
243 u_long cmd;
244 {
245
246 /* filled in */
247 #if 0
248 s32p->df_buf = (netbsd32_charp)p->df_buf;
249 #endif
250 s32p->df_count = p->df_count;
251 s32p->df_startblk = p->df_startblk;
252 memcpy(s32p->df_reg, p->df_reg, sizeof(p->df_reg));
253 }
254 #endif
255
256 static inline void
257 netbsd32_from_ifreq(p, s32p, cmd)
258 struct ifreq *p;
259 struct netbsd32_ifreq *s32p;
260 u_long cmd;
261 {
262
263 /*
264 * XXX
265 * struct ifreq says the same, but sometimes the ifr_data
266 * union member needs to be converted to 64 bits... this
267 * is very driver specific and so we ignore it for now..
268 */
269 *s32p->ifr_name = *p->ifr_name;
270 if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
271 s32p->ifr_data = (netbsd32_void *)(u_long)s32p->ifr_data;
272 }
273
274 static inline void
275 netbsd32_from_ifconf(p, s32p, cmd)
276 struct ifconf *p;
277 struct netbsd32_ifconf *s32p;
278 u_long cmd;
279 {
280
281 s32p->ifc_len = p->ifc_len;
282 /* ifc_buf & ifc_req are the same size so this works */
283 s32p->ifc_buf = (netbsd32_void *)(u_long)p->ifc_buf;
284 }
285
286 static inline void
287 netbsd32_from_ifmediareq(p, s32p, cmd)
288 struct ifmediareq *p;
289 struct netbsd32_ifmediareq *s32p;
290 u_long cmd;
291 {
292
293 memcpy(s32p, p, sizeof *p);
294 /* filled in? */
295 #if 0
296 s32p->ifm_ulist = (netbsd32_intp_t)p->ifm_ulist;
297 #endif
298 }
299
300 static inline void
301 netbsd32_from_ifdrv(p, s32p, cmd)
302 struct ifdrv *p;
303 struct netbsd32_ifdrv *s32p;
304 u_long cmd;
305 {
306
307 memcpy(s32p, p, sizeof *p);
308 /* filled in? */
309 #if 0
310 s32p->ifm_data = (netbsd32_u_longp_t)p->ifm_data;
311 #endif
312 }
313
314 static inline void
315 netbsd32_from_sioc_vif_req(p, s32p, cmd)
316 struct sioc_vif_req *p;
317 struct netbsd32_sioc_vif_req *s32p;
318 u_long cmd;
319 {
320
321 s32p->vifi = p->vifi;
322 s32p->icount = (netbsd32_u_long)p->icount;
323 s32p->ocount = (netbsd32_u_long)p->ocount;
324 s32p->ibytes = (netbsd32_u_long)p->ibytes;
325 s32p->obytes = (netbsd32_u_long)p->obytes;
326 }
327
328 static inline void
329 netbsd32_from_sioc_sg_req(p, s32p, cmd)
330 struct sioc_sg_req *p;
331 struct netbsd32_sioc_sg_req *s32p;
332 u_long cmd;
333 {
334
335 s32p->src = p->src;
336 s32p->grp = p->grp;
337 s32p->pktcnt = (netbsd32_u_long)p->pktcnt;
338 s32p->bytecnt = (netbsd32_u_long)p->bytecnt;
339 s32p->wrong_if = (netbsd32_u_long)p->wrong_if;
340 }
341
342
343 /*
344 * main ioctl syscall.
345 *
346 * ok, here we are in the biggy. we have to do fix ups depending
347 * on the ioctl command before and afterwards.
348 */
349 int
350 netbsd32_ioctl(l, v, retval)
351 struct lwp *l;
352 void *v;
353 register_t *retval;
354 {
355 struct netbsd32_ioctl_args /* {
356 syscallarg(int) fd;
357 syscallarg(netbsd32_u_long) com;
358 syscallarg(netbsd32_voidp) data;
359 } */ *uap = v;
360 struct proc *p = l->l_proc;
361 struct file *fp;
362 struct filedesc *fdp;
363 u_long com;
364 int error = 0;
365 u_int size, size32;
366 void *data, memp = NULL;
367 void *data32, memp32 = NULL;
368 int tmp;
369 #define STK_PARAMS 128
370 u_long stkbuf[STK_PARAMS/sizeof(u_long)];
371 u_long stkbuf32[STK_PARAMS/sizeof(u_long)];
372
373 /*
374 * we need to translate some commands (_IOW) before calling sys_ioctl,
375 * some after (_IOR), and some both (_IOWR).
376 */
377 #if 0
378 {
379 char *dirs[8] = { "NONE!", "VOID", "OUT", "VOID|OUT!", "IN", "VOID|IN!",
380 "INOUT", "VOID|IN|OUT!" };
381
382 printf("netbsd32_ioctl(%d, %x, %x): %s group %c base %d len %d\n",
383 SCARG(uap, fd), SCARG(uap, com), SCARG(uap, data),
384 dirs[((SCARG(uap, com) & IOC_DIRMASK)>>29)],
385 IOCGROUP(SCARG(uap, com)), IOCBASECMD(SCARG(uap, com)),
386 IOCPARM_LEN(SCARG(uap, com)));
387 }
388 #endif
389
390 fdp = p->p_fd;
391 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
392 return (EBADF);
393
394 FILE_USE(fp);
395
396 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
397 error = EBADF;
398 goto out;
399 }
400
401 switch (com = SCARG(uap, com)) {
402 case FIONCLEX:
403 fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
404 goto out;
405
406 case FIOCLEX:
407 fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
408 goto out;
409 }
410
411 /*
412 * Interpret high order word to find amount of data to be
413 * copied to/from the user's address space.
414 */
415 size32 = IOCPARM_LEN(com);
416 if (size32 > IOCPARM_MAX) {
417 error = ENOTTY;
418 goto out;
419 }
420 memp = NULL;
421 if (size32 > sizeof(stkbuf)) {
422 memp32 = (void *)malloc((u_long)size32, M_IOCTLOPS, M_WAITOK);
423 data32 = memp32;
424 } else
425 data32 = (void *)stkbuf32;
426 if (com&IOC_IN) {
427 if (size32) {
428 error = copyin((void *)NETBSD32PTR64(SCARG(uap, data)),
429 data32, size32);
430 if (error) {
431 if (memp32)
432 free(memp32, M_IOCTLOPS);
433 goto out;
434 }
435 } else
436 *(void **)data32 =
437 (void *)NETBSD32PTR64(SCARG(uap, data));
438 } else if ((com&IOC_OUT) && size32)
439 /*
440 * Zero the buffer so the user always
441 * gets back something deterministic.
442 */
443 memset(data32, 0, size32);
444 else if (com&IOC_VOID)
445 *(void **)data32 = (void *)NETBSD32PTR64(SCARG(uap, data));
446
447 /*
448 * convert various structures, pointers, and other objects that
449 * change size from 32 bit -> 64 bit, for all ioctl commands.
450 */
451 switch (SCARG(uap, com)) {
452 case FIONBIO:
453 if ((tmp = *(int *)data32) != 0)
454 fp->f_flag |= FNONBLOCK;
455 else
456 fp->f_flag &= ~FNONBLOCK;
457 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (void *)&tmp, l);
458 break;
459
460 case FIOASYNC:
461 if ((tmp = *(int *)data32) != 0)
462 fp->f_flag |= FASYNC;
463 else
464 fp->f_flag &= ~FASYNC;
465 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (void *)&tmp, l);
466 break;
467
468 case DIOCGPART32:
469 IOCTL_STRUCT_CONV_TO(DIOCGPART, partinfo);
470 #if 0 /* not implemented by anything */
471 case DIOCRFORMAT32:
472 IOCTL_STRUCT_CONV_TO(DIOCRFORMAT, format_op);
473 case DIOCWFORMAT32:
474 IOCTL_STRUCT_CONV_TO(DIOCWFORMAT, format_op);
475 #endif
476
477 /*
478 * only a few ifreq syscalls need conversion and those are
479 * all driver specific... XXX
480 */
481 #if 0
482 case SIOCGADDRROM3232:
483 IOCTL_STRUCT_CONV_TO(SIOCGADDRROM32, ifreq);
484 case SIOCGCHIPID32:
485 IOCTL_STRUCT_CONV_TO(SIOCGCHIPID, ifreq);
486 case SIOCSIFADDR32:
487 IOCTL_STRUCT_CONV_TO(SIOCSIFADDR, ifreq);
488 case OSIOCGIFADDR32:
489 IOCTL_STRUCT_CONV_TO(OSIOCGIFADDR, ifreq);
490 case SIOCGIFADDR32:
491 IOCTL_STRUCT_CONV_TO(SIOCGIFADDR, ifreq);
492 case SIOCSIFDSTADDR32:
493 IOCTL_STRUCT_CONV_TO(SIOCSIFDSTADDR, ifreq);
494 case OSIOCGIFDSTADDR32:
495 IOCTL_STRUCT_CONV_TO(OSIOCGIFDSTADDR, ifreq);
496 case SIOCGIFDSTADDR32:
497 IOCTL_STRUCT_CONV_TO(SIOCGIFDSTADDR, ifreq);
498 case OSIOCGIFBRDADDR32:
499 IOCTL_STRUCT_CONV_TO(OSIOCGIFBRDADDR, ifreq);
500 case SIOCGIFBRDADDR32:
501 IOCTL_STRUCT_CONV_TO(SIOCGIFBRDADDR, ifreq);
502 case SIOCSIFBRDADDR32:
503 IOCTL_STRUCT_CONV_TO(SIOCSIFBRDADDR, ifreq);
504 case OSIOCGIFNETMASK32:
505 IOCTL_STRUCT_CONV_TO(OSIOCGIFNETMASK, ifreq);
506 case SIOCGIFNETMASK32:
507 IOCTL_STRUCT_CONV_TO(SIOCGIFNETMASK, ifreq);
508 case SIOCSIFNETMASK32:
509 IOCTL_STRUCT_CONV_TO(SIOCSIFNETMASK, ifreq);
510 case SIOCGIFMETRIC32:
511 IOCTL_STRUCT_CONV_TO(SIOCGIFMETRIC, ifreq);
512 case SIOCSIFMETRIC32:
513 IOCTL_STRUCT_CONV_TO(SIOCSIFMETRIC, ifreq);
514 case SIOCDIFADDR32:
515 IOCTL_STRUCT_CONV_TO(SIOCDIFADDR, ifreq);
516 case SIOCADDMULTI32:
517 IOCTL_STRUCT_CONV_TO(SIOCADDMULTI, ifreq);
518 case SIOCDELMULTI32:
519 IOCTL_STRUCT_CONV_TO(SIOCDELMULTI, ifreq);
520 case SIOCSIFMEDIA32:
521 IOCTL_STRUCT_CONV_TO(SIOCSIFMEDIA, ifreq);
522 case SIOCSIFMTU32:
523 IOCTL_STRUCT_CONV_TO(SIOCSIFMTU, ifreq);
524 case SIOCGIFMTU32:
525 IOCTL_STRUCT_CONV_TO(SIOCGIFMTU, ifreq);
526 case BIOCGETIF32:
527 IOCTL_STRUCT_CONV_TO(BIOCGETIF, ifreq);
528 case BIOCSETIF32:
529 IOCTL_STRUCT_CONV_TO(BIOCSETIF, ifreq);
530 case SIOCPHASE132:
531 IOCTL_STRUCT_CONV_TO(SIOCPHASE1, ifreq);
532 case SIOCPHASE232:
533 IOCTL_STRUCT_CONV_TO(SIOCPHASE2, ifreq);
534 #endif
535
536 case OSIOCGIFCONF32:
537 IOCTL_STRUCT_CONV_TO(OSIOCGIFCONF, ifconf);
538 case SIOCGIFCONF32:
539 IOCTL_STRUCT_CONV_TO(SIOCGIFCONF, ifconf);
540
541 case SIOCGIFFLAGS32:
542 IOCTL_STRUCT_CONV_TO(SIOCGIFFLAGS, ifreq);
543 case SIOCSIFFLAGS32:
544 IOCTL_STRUCT_CONV_TO(SIOCSIFFLAGS, ifreq);
545
546 case SIOCGIFMEDIA32:
547 IOCTL_STRUCT_CONV_TO(SIOCGIFMEDIA, ifmediareq);
548
549 case SIOCSDRVSPEC32:
550 IOCTL_STRUCT_CONV_TO(SIOCSDRVSPEC, ifdrv);
551
552 case SIOCGETVIFCNT32:
553 IOCTL_STRUCT_CONV_TO(SIOCGETVIFCNT, sioc_vif_req);
554
555 case SIOCGETSGCNT32:
556 IOCTL_STRUCT_CONV_TO(SIOCGETSGCNT, sioc_sg_req);
557
558 default:
559 #ifdef NETBSD32_MD_IOCTL
560 error = netbsd32_md_ioctl(fp, com, data32, l);
561 #else
562 error = (*fp->f_ops->fo_ioctl)(fp, com, data32, l);
563 #endif
564 break;
565 }
566
567 if (error == EPASSTHROUGH)
568 error = ENOTTY;
569
570 /*
571 * Copy any data to user, size was
572 * already set and checked above.
573 */
574 if (error == 0 && (com&IOC_OUT) && size32)
575 error = copyout(data32,
576 (void *)NETBSD32PTR64(SCARG(uap, data)), size32);
577
578 /* if we malloced data, free it here */
579 if (memp32)
580 free(memp32, M_IOCTLOPS);
581 if (memp)
582 free(memp, M_IOCTLOPS);
583
584 out:
585 FILE_UNUSE(fp, l);
586 return (error);
587 }
588