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