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