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