netbsd32_ioctl.c revision 1.75 1 /* $NetBSD: netbsd32_ioctl.c,v 1.75 2015/05/31 15:08:14 roy 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.75 2015/05/31 15:08:14 roy 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/ataio.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 #include <sys/envsys.h>
55 #include <sys/wdog.h>
56 #include <sys/clockctl.h>
57 #include <sys/exec_elf.h>
58 #include <sys/ksyms.h>
59
60 #ifdef __sparc__
61 #include <dev/sun/fbio.h>
62 #include <machine/openpromio.h>
63 #endif
64
65 #include <net/if.h>
66 #include <net/route.h>
67
68 #include <net/bpf.h>
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/igmp.h>
72 #include <netinet/igmp_var.h>
73 #include <netinet/ip_mroute.h>
74
75 #include <compat/sys/sockio.h>
76
77 #include <compat/netbsd32/netbsd32.h>
78 #include <compat/netbsd32/netbsd32_ioctl.h>
79 #include <compat/netbsd32/netbsd32_syscallargs.h>
80
81 #include <dev/vndvar.h>
82
83 /* convert to/from different structures */
84
85 static inline void
86 netbsd32_to_partinfo(struct netbsd32_partinfo *s32p, struct partinfo *p, u_long cmd)
87 {
88
89 p->disklab = (struct disklabel *)NETBSD32PTR64(s32p->disklab);
90 p->part = (struct partition *)NETBSD32PTR64(s32p->part);
91 }
92
93 #if 0
94 static inline void
95 netbsd32_to_format_op(struct netbsd32_format_op *s32p, struct format_op *p, u_long cmd)
96 {
97
98 p->df_buf = (char *)NETBSD32PTR64(s32p->df_buf);
99 p->df_count = s32p->df_count;
100 p->df_startblk = s32p->df_startblk;
101 memcpy(p->df_reg, s32p->df_reg, sizeof(s32p->df_reg));
102 }
103 #endif
104
105 static inline void
106 netbsd32_to_ifreq(struct netbsd32_ifreq *s32p, struct ifreq *p, u_long cmd)
107 {
108
109 memcpy(p, s32p, sizeof *s32p);
110 /*
111 * XXX
112 * struct ifreq says the same, but sometimes the ifr_data
113 * union member needs to be converted to 64 bits... this
114 * is very driver specific and so we ignore it for now..
115 */
116 if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
117 p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
118 }
119
120 static inline void
121 netbsd32_to_oifreq(struct netbsd32_oifreq *s32p, struct oifreq *p, u_long cmd)
122 {
123
124 memcpy(p, s32p, sizeof *s32p);
125 /*
126 * XXX
127 * struct ifreq says the same, but sometimes the ifr_data
128 * union member needs to be converted to 64 bits... this
129 * is very driver specific and so we ignore it for now..
130 */
131 if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
132 p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
133 }
134
135 static inline void
136 netbsd32_to_if_addrprefreq(const struct netbsd32_if_addrprefreq *ifap32,
137 struct if_addrprefreq *ifap, u_long cmd)
138 {
139 strlcpy(ifap->ifap_name, ifap32->ifap_name, sizeof(ifap->ifap_name));
140 ifap->ifap_preference = ifap32->ifap_preference;
141 memcpy(&ifap->ifap_addr, &ifap32->ifap_addr,
142 max(ifap32->ifap_addr.ss_len, _SS_MAXSIZE));
143 }
144
145 static inline void
146 netbsd32_to_ifconf(struct netbsd32_ifconf *s32p, struct ifconf *p, u_long cmd)
147 {
148
149 p->ifc_len = s32p->ifc_len;
150 /* ifc_buf & ifc_req are the same size so this works */
151 p->ifc_buf = (void *)NETBSD32PTR64(s32p->ifc_buf);
152 }
153
154 static inline void
155 netbsd32_to_ifmediareq(struct netbsd32_ifmediareq *s32p, struct ifmediareq *p, u_long cmd)
156 {
157
158 memcpy(p, s32p, sizeof *s32p);
159 p->ifm_ulist = (int *)NETBSD32PTR64(s32p->ifm_ulist);
160 }
161
162 static inline void
163 netbsd32_to_ifdrv(struct netbsd32_ifdrv *s32p, struct ifdrv *p, u_long cmd)
164 {
165
166 memcpy(p->ifd_name, s32p->ifd_name, sizeof p->ifd_name);
167 p->ifd_cmd = s32p->ifd_cmd;
168 p->ifd_len = s32p->ifd_len;
169 p->ifd_data = (void *)NETBSD32PTR64(s32p->ifd_data);
170 }
171
172 static inline void
173 netbsd32_to_sioc_vif_req(struct netbsd32_sioc_vif_req *s32p, struct sioc_vif_req *p, u_long cmd)
174 {
175
176 p->vifi = s32p->vifi;
177 p->icount = (u_long)s32p->icount;
178 p->ocount = (u_long)s32p->ocount;
179 p->ibytes = (u_long)s32p->ibytes;
180 p->obytes = (u_long)s32p->obytes;
181 }
182
183 static inline void
184 netbsd32_to_sioc_sg_req(struct netbsd32_sioc_sg_req *s32p, struct sioc_sg_req *p, u_long cmd)
185 {
186
187 p->src = s32p->src;
188 p->grp = s32p->grp;
189 p->pktcnt = (u_long)s32p->pktcnt;
190 p->bytecnt = (u_long)s32p->bytecnt;
191 p->wrong_if = (u_long)s32p->wrong_if;
192 }
193
194 static inline void
195 netbsd32_to_atareq(struct netbsd32_atareq *s32p, struct atareq *p, u_long cmd)
196 {
197 p->flags = (u_long)s32p->flags;
198 p->command = s32p->command;
199 p->features = s32p->features;
200 p->sec_count = s32p->sec_count;
201 p->sec_num = s32p->sec_num;
202 p->head = s32p->head;
203 p->cylinder = s32p->cylinder;
204 p->databuf = (char *)NETBSD32PTR64(s32p->databuf);
205 p->datalen = (u_long)s32p->datalen;
206 p->timeout = s32p->timeout;
207 p->retsts = s32p->retsts;
208 p->error = s32p->error;
209 }
210
211 static inline void
212 netbsd32_to_vnd_ioctl(struct netbsd32_vnd_ioctl *s32p, struct vnd_ioctl *p, u_long cmd)
213 {
214
215 p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
216 p->vnd_flags = s32p->vnd_flags;
217 p->vnd_geom = s32p->vnd_geom;
218 p->vnd_osize = s32p->vnd_osize;
219 p->vnd_size = s32p->vnd_size;
220 }
221
222 static inline void
223 netbsd32_to_vnd_user(struct netbsd32_vnd_user *s32p, struct vnd_user *p, u_long cmd)
224 {
225
226 p->vnu_unit = s32p->vnu_unit;
227 p->vnu_dev = s32p->vnu_dev;
228 p->vnu_ino = s32p->vnu_ino;
229 }
230
231 static inline void
232 netbsd32_to_vnd_ioctl50(struct netbsd32_vnd_ioctl50 *s32p, struct vnd_ioctl50 *p, u_long cmd)
233 {
234
235 p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
236 p->vnd_flags = s32p->vnd_flags;
237 p->vnd_geom = s32p->vnd_geom;
238 p->vnd_size = s32p->vnd_size;
239 }
240
241 static inline void
242 netbsd32_to_plistref(struct netbsd32_plistref *s32p, struct plistref *p, u_long cmd)
243 {
244
245 p->pref_plist = NETBSD32PTR64(s32p->pref_plist);
246 p->pref_len = s32p->pref_len;
247 }
248
249 static inline void
250 netbsd32_to_u_long(netbsd32_u_long *s32p, u_long *p, u_long cmd)
251 {
252
253 *p = (u_long)*s32p;
254 }
255
256 static inline void
257 netbsd32_to_wdog_conf(struct netbsd32_wdog_conf *s32p, struct wdog_conf *p, u_long cmd)
258 {
259
260 p->wc_names = (char *)NETBSD32PTR64(s32p->wc_names);
261 p->wc_count = s32p->wc_count;
262 }
263
264 static inline void
265 netbsd32_to_bpf_program(struct netbsd32_bpf_program *s32p, struct bpf_program *p, u_long cmd)
266 {
267
268 p->bf_insns = (void *)NETBSD32PTR64(s32p->bf_insns);
269 p->bf_len = s32p->bf_len;
270 }
271
272 static inline void
273 netbsd32_to_bpf_dltlist(struct netbsd32_bpf_dltlist *s32p, struct bpf_dltlist *p, u_long cmd)
274 {
275
276 p->bfl_list = (void *)NETBSD32PTR64(s32p->bfl_list);
277 p->bfl_len = s32p->bfl_len;
278 }
279
280 /* wsdisplay stuff */
281 static inline void
282 netbsd32_to_wsdisplay_addscreendata(struct netbsd32_wsdisplay_addscreendata *asd32,
283 struct wsdisplay_addscreendata *asd,
284 u_long cmd)
285 {
286 asd->screentype = (char *)NETBSD32PTR64(asd32->screentype);
287 asd->emul = (char *)NETBSD32PTR64(asd32->emul);
288 asd->idx = asd32->idx;
289 }
290
291 static inline void
292 netbsd32_to_ieee80211req(struct netbsd32_ieee80211req *ireq32,
293 struct ieee80211req *ireq, u_long cmd)
294 {
295 strncpy(ireq->i_name, ireq32->i_name, IFNAMSIZ);
296 ireq->i_type = ireq32->i_type;
297 ireq->i_val = ireq32->i_val;
298 ireq->i_len = ireq32->i_len;
299 ireq->i_data = NETBSD32PTR64(ireq32->i_data);
300 }
301
302 static inline void
303 netbsd32_to_ieee80211_nwkey(struct netbsd32_ieee80211_nwkey *nwk32,
304 struct ieee80211_nwkey *nwk,
305 u_long cmd)
306 {
307 int i;
308
309 strncpy(nwk->i_name, nwk32->i_name, IFNAMSIZ);
310 nwk->i_wepon = nwk32->i_wepon;
311 nwk->i_defkid = nwk32->i_defkid;
312 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
313 nwk->i_key[i].i_keylen = nwk32->i_key[i].i_keylen;
314 nwk->i_key[i].i_keydat =
315 NETBSD32PTR64(nwk32->i_key[i].i_keydat);
316 }
317 }
318
319 static inline void
320 netbsd32_to_wsdisplay_cursor(struct netbsd32_wsdisplay_cursor *c32,
321 struct wsdisplay_cursor *c,
322 u_long cmd)
323 {
324 c->which = c32->which;
325 c->enable = c32->enable;
326 c->pos.x = c32->pos.x;
327 c->pos.y = c32->pos.y;
328 c->hot.x = c32->hot.x;
329 c->hot.y = c32->hot.y;
330 c->size.x = c32->size.x;
331 c->size.y = c32->size.y;
332 c->cmap.index = c32->cmap.index;
333 c->cmap.count = c32->cmap.count;
334 c->cmap.red = NETBSD32PTR64(c32->cmap.red);
335 c->cmap.green = NETBSD32PTR64(c32->cmap.green);
336 c->cmap.blue = NETBSD32PTR64(c32->cmap.blue);
337 c->image = NETBSD32PTR64(c32->image);
338 c->mask = NETBSD32PTR64(c32->mask);
339 }
340
341 static inline void
342 netbsd32_to_wsdisplay_cmap(struct netbsd32_wsdisplay_cmap *c32,
343 struct wsdisplay_cmap *c,
344 u_long cmd)
345 {
346 c->index = c32->index;
347 c->count = c32->count;
348 c->red = NETBSD32PTR64(c32->red);
349 c->green = NETBSD32PTR64(c32->green);
350 c->blue = NETBSD32PTR64(c32->blue);
351 }
352
353 static inline void
354 netbsd32_to_clockctl_settimeofday(
355 const struct netbsd32_clockctl_settimeofday *s32p,
356 struct clockctl_settimeofday *p,
357 u_long cmd)
358 {
359
360 p->tv = NETBSD32PTR64(s32p->tv);
361 p->tzp = NETBSD32PTR64(s32p->tzp);
362 }
363
364 static inline void
365 netbsd32_to_clockctl_adjtime(
366 const struct netbsd32_clockctl_adjtime *s32p,
367 struct clockctl_adjtime *p,
368 u_long cmd)
369 {
370
371 p->delta = NETBSD32PTR64(s32p->delta);
372 p->olddelta = NETBSD32PTR64(s32p->olddelta);
373 }
374
375 static inline void
376 netbsd32_to_clockctl_clock_settime(
377 const struct netbsd32_clockctl_clock_settime *s32p,
378 struct clockctl_clock_settime *p,
379 u_long cmd)
380 {
381
382 p->clock_id = s32p->clock_id;
383 p->tp = NETBSD32PTR64(s32p->tp);
384 }
385
386 static inline void
387 netbsd32_to_clockctl_ntp_adjtime(
388 const struct netbsd32_clockctl_ntp_adjtime *s32p,
389 struct clockctl_ntp_adjtime *p,
390 u_long cmd)
391 {
392
393 p->tp = NETBSD32PTR64(s32p->tp);
394 p->retval = s32p->retval;
395 }
396
397 static inline void
398 netbsd32_to_ksyms_gsymbol(
399 const struct netbsd32_ksyms_gsymbol *s32p,
400 struct ksyms_gsymbol *p,
401 u_long cmd)
402 {
403
404 p->kg_name = NETBSD32PTR64(s32p->kg_name);
405 }
406
407 static inline void
408 netbsd32_to_ksyms_gvalue(
409 const struct netbsd32_ksyms_gvalue *s32p,
410 struct ksyms_gvalue *p,
411 u_long cmd)
412 {
413
414 p->kv_name = NETBSD32PTR64(s32p->kv_name);
415 }
416
417 /*
418 * handle ioctl conversions from 64-bit kernel -> netbsd32
419 */
420
421 static inline void
422 netbsd32_from_partinfo(struct partinfo *p, struct netbsd32_partinfo *s32p, u_long cmd)
423 {
424
425 NETBSD32PTR32(s32p->disklab, p->disklab);
426 NETBSD32PTR32(s32p->part, p->part);
427 }
428
429 #if 0
430 static inline void
431 netbsd32_from_format_op(struct format_op *p, struct netbsd32_format_op *s32p, u_long cmd)
432 {
433
434 /* filled in */
435 #if 0
436 s32p->df_buf = (netbsd32_charp)p->df_buf;
437 #endif
438 s32p->df_count = p->df_count;
439 s32p->df_startblk = p->df_startblk;
440 memcpy(s32p->df_reg, p->df_reg, sizeof(p->df_reg));
441 }
442 #endif
443
444 static inline void
445 netbsd32_from_ifreq(struct ifreq *p, struct netbsd32_ifreq *s32p, u_long cmd)
446 {
447
448 /*
449 * XXX
450 * struct ifreq says the same, but sometimes the ifr_data
451 * union member needs to be converted to 64 bits... this
452 * is very driver specific and so we ignore it for now..
453 */
454 memcpy(s32p, p, sizeof *s32p);
455 if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
456 NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
457 }
458
459 static inline void
460 netbsd32_from_oifreq(struct oifreq *p, struct netbsd32_oifreq *s32p, u_long cmd)
461 {
462
463 /*
464 * XXX
465 * struct ifreq says the same, but sometimes the ifr_data
466 * union member needs to be converted to 64 bits... this
467 * is very driver specific and so we ignore it for now..
468 */
469 memcpy(s32p, p, sizeof *s32p);
470 if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
471 NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
472 }
473
474 static inline void
475 netbsd32_from_if_addrprefreq(const struct if_addrprefreq *ifap,
476 struct netbsd32_if_addrprefreq *ifap32, u_long cmd)
477 {
478 strlcpy(ifap32->ifap_name, ifap->ifap_name, sizeof(ifap32->ifap_name));
479 ifap32->ifap_preference = ifap->ifap_preference;
480 memcpy(&ifap32->ifap_addr, &ifap->ifap_addr,
481 max(ifap->ifap_addr.ss_len, _SS_MAXSIZE));
482 }
483
484 static inline void
485 netbsd32_from_ifconf(struct ifconf *p, struct netbsd32_ifconf *s32p, u_long cmd)
486 {
487
488 s32p->ifc_len = p->ifc_len;
489 /* ifc_buf & ifc_req are the same size so this works */
490 NETBSD32PTR32(s32p->ifc_buf, p->ifc_buf);
491 }
492
493 static inline void
494 netbsd32_from_ifmediareq(struct ifmediareq *p, struct netbsd32_ifmediareq *s32p, u_long cmd)
495 {
496
497 memcpy(s32p, p, sizeof *p);
498 /* filled in? */
499 #if 0
500 s32p->ifm_ulist = (netbsd32_intp_t)p->ifm_ulist;
501 #endif
502 }
503
504 static inline void
505 netbsd32_from_ifdrv(struct ifdrv *p, struct netbsd32_ifdrv *s32p, u_long cmd)
506 {
507
508 memcpy(s32p->ifd_name, p->ifd_name, sizeof s32p->ifd_name);
509 s32p->ifd_cmd = p->ifd_cmd;
510 s32p->ifd_len = p->ifd_len;
511 NETBSD32PTR32(s32p->ifd_data, p->ifd_data);
512 }
513
514 static inline void
515 netbsd32_from_sioc_vif_req(struct sioc_vif_req *p, struct netbsd32_sioc_vif_req *s32p, u_long cmd)
516 {
517
518 s32p->vifi = p->vifi;
519 s32p->icount = (netbsd32_u_long)p->icount;
520 s32p->ocount = (netbsd32_u_long)p->ocount;
521 s32p->ibytes = (netbsd32_u_long)p->ibytes;
522 s32p->obytes = (netbsd32_u_long)p->obytes;
523 }
524
525 static inline void
526 netbsd32_from_sioc_sg_req(struct sioc_sg_req *p, struct netbsd32_sioc_sg_req *s32p, u_long cmd)
527 {
528
529 s32p->src = p->src;
530 s32p->grp = p->grp;
531 s32p->pktcnt = (netbsd32_u_long)p->pktcnt;
532 s32p->bytecnt = (netbsd32_u_long)p->bytecnt;
533 s32p->wrong_if = (netbsd32_u_long)p->wrong_if;
534 }
535
536 static inline void
537 netbsd32_from_atareq(struct atareq *p, struct netbsd32_atareq *s32p, u_long cmd)
538 {
539 s32p->flags = (netbsd32_u_long)p->flags;
540 s32p->command = p->command;
541 s32p->features = p->features;
542 s32p->sec_count = p->sec_count;
543 s32p->sec_num = p->sec_num;
544 s32p->head = p->head;
545 s32p->cylinder = p->cylinder;
546 NETBSD32PTR32(s32p->databuf, p->databuf);
547 s32p->datalen = (netbsd32_u_long)p->datalen;
548 s32p->timeout = p->timeout;
549 s32p->retsts = p->retsts;
550 s32p->error = p->error;
551 }
552
553 static inline void
554 netbsd32_from_vnd_ioctl(struct vnd_ioctl *p, struct netbsd32_vnd_ioctl *s32p, u_long cmd)
555 {
556
557 s32p->vnd_flags = p->vnd_flags;
558 s32p->vnd_geom = p->vnd_geom;
559 s32p->vnd_osize = p->vnd_osize;
560 s32p->vnd_size = p->vnd_size;
561 }
562
563 static inline void
564 netbsd32_from_vnd_user(struct vnd_user *p, struct netbsd32_vnd_user *s32p, u_long cmd)
565 {
566
567 s32p->vnu_unit = p->vnu_unit;
568 s32p->vnu_dev = p->vnu_dev;
569 s32p->vnu_ino = p->vnu_ino;
570 }
571
572 static inline void
573 netbsd32_from_vnd_ioctl50(struct vnd_ioctl50 *p, struct netbsd32_vnd_ioctl50 *s32p, u_long cmd)
574 {
575
576 s32p->vnd_flags = p->vnd_flags;
577 s32p->vnd_geom = p->vnd_geom;
578 s32p->vnd_size = p->vnd_size;
579 }
580
581 static inline void
582 netbsd32_from_plistref(struct plistref *p, struct netbsd32_plistref *s32p, u_long cmd)
583 {
584
585 NETBSD32PTR32(s32p->pref_plist, p->pref_plist);
586 s32p->pref_len = p->pref_len;
587 }
588
589 static inline void
590 netbsd32_from_wdog_conf(struct wdog_conf *p, struct netbsd32_wdog_conf *s32p, u_long cmd)
591 {
592
593 NETBSD32PTR32(s32p->wc_names, p->wc_names);
594 s32p->wc_count = p->wc_count;
595 }
596
597 /* wsdisplay stuff */
598 static inline void
599 netbsd32_from_wsdisplay_addscreendata(struct wsdisplay_addscreendata *asd,
600 struct netbsd32_wsdisplay_addscreendata *asd32,
601 u_long cmd)
602 {
603 NETBSD32PTR32(asd32->screentype, asd->screentype);
604 NETBSD32PTR32(asd32->emul, asd->emul);
605 asd32->idx = asd->idx;
606 }
607
608 static inline void
609 netbsd32_from_wsdisplay_cursor(struct wsdisplay_cursor *c,
610 struct netbsd32_wsdisplay_cursor *c32,
611 u_long cmd)
612 {
613 c32->which = c->which;
614 c32->enable = c->enable;
615 c32->pos.x = c->pos.x;
616 c32->pos.y = c->pos.y;
617 c32->hot.x = c->hot.x;
618 c32->hot.y = c->hot.y;
619 c32->size.x = c->size.x;
620 c32->size.y = c->size.y;
621 c32->cmap.index = c->cmap.index;
622 c32->cmap.count = c->cmap.count;
623 NETBSD32PTR32(c32->cmap.red, c->cmap.red);
624 NETBSD32PTR32(c32->cmap.green, c->cmap.green);
625 NETBSD32PTR32(c32->cmap.blue, c->cmap.blue);
626 NETBSD32PTR32(c32->image, c->image);
627 NETBSD32PTR32(c32->mask, c->mask);
628 }
629
630 static inline void
631 netbsd32_from_wsdisplay_cmap(struct wsdisplay_cmap *c,
632 struct netbsd32_wsdisplay_cmap *c32,
633 u_long cmd)
634 {
635 c32->index = c->index;
636 c32->count = c->count;
637 NETBSD32PTR32(c32->red, c->red);
638 NETBSD32PTR32(c32->green, c->green);
639 NETBSD32PTR32(c32->blue, c->blue);
640 }
641
642 static inline void
643 netbsd32_from_ieee80211req(struct ieee80211req *ireq,
644 struct netbsd32_ieee80211req *ireq32, u_long cmd)
645 {
646 strncpy(ireq32->i_name, ireq->i_name, IFNAMSIZ);
647 ireq32->i_type = ireq->i_type;
648 ireq32->i_val = ireq->i_val;
649 ireq32->i_len = ireq->i_len;
650 NETBSD32PTR32(ireq32->i_data, ireq->i_data);
651 }
652
653 static inline void
654 netbsd32_from_ieee80211_nwkey(struct ieee80211_nwkey *nwk,
655 struct netbsd32_ieee80211_nwkey *nwk32,
656 u_long cmd)
657 {
658 int i;
659
660 strncpy(nwk32->i_name, nwk->i_name, IFNAMSIZ);
661 nwk32->i_wepon = nwk->i_wepon;
662 nwk32->i_defkid = nwk->i_defkid;
663 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
664 nwk32->i_key[i].i_keylen = nwk->i_key[i].i_keylen;
665 NETBSD32PTR32(nwk32->i_key[i].i_keydat,
666 nwk->i_key[i].i_keydat);
667 }
668 }
669
670 static inline void
671 netbsd32_from_bpf_program(struct bpf_program *p, struct netbsd32_bpf_program *s32p, u_long cmd)
672 {
673
674 NETBSD32PTR32(s32p->bf_insns, p->bf_insns);
675 s32p->bf_len = p->bf_len;
676 }
677
678 static inline void
679 netbsd32_from_bpf_dltlist(struct bpf_dltlist *p, struct netbsd32_bpf_dltlist *s32p, u_long cmd)
680 {
681
682 NETBSD32PTR32(s32p->bfl_list, p->bfl_list);
683 s32p->bfl_len = p->bfl_len;
684 }
685
686 static inline void
687 netbsd32_from_u_long(u_long *p, netbsd32_u_long *s32p, u_long cmd)
688 {
689
690 *s32p = (netbsd32_u_long)*p;
691 }
692
693 static inline void
694 netbsd32_from_clockctl_settimeofday(
695 const struct clockctl_settimeofday *p,
696 struct netbsd32_clockctl_settimeofday *s32p,
697 u_long cmd)
698 {
699
700 NETBSD32PTR32(s32p->tv, p->tv);
701 NETBSD32PTR32(s32p->tzp, p->tzp);
702 }
703
704 static inline void
705 netbsd32_from_clockctl_adjtime(
706 const struct clockctl_adjtime *p,
707 struct netbsd32_clockctl_adjtime *s32p,
708 u_long cmd)
709 {
710
711 NETBSD32PTR32(s32p->delta, p->delta);
712 NETBSD32PTR32(s32p->olddelta, p->olddelta);
713 }
714
715 static inline void
716 netbsd32_from_clockctl_clock_settime(
717 const struct clockctl_clock_settime *p,
718 struct netbsd32_clockctl_clock_settime *s32p,
719 u_long cmd)
720 {
721
722 s32p->clock_id = p->clock_id;
723 NETBSD32PTR32(s32p->tp, p->tp);
724 }
725
726 static inline void
727 netbsd32_from_clockctl_ntp_adjtime(
728 const struct clockctl_ntp_adjtime *p,
729 struct netbsd32_clockctl_ntp_adjtime *s32p,
730 u_long cmd)
731 {
732
733 NETBSD32PTR32(s32p->tp, p->tp);
734 s32p->retval = p->retval;
735 }
736
737 static inline void
738 netbsd32_from_ksyms_gsymbol(
739 const struct ksyms_gsymbol *p,
740 struct netbsd32_ksyms_gsymbol *s32p,
741 u_long cmd)
742 {
743
744 NETBSD32PTR32(s32p->kg_name, p->kg_name);
745 s32p->kg_sym = p->kg_sym;
746 }
747
748 static inline void
749 netbsd32_from_ksyms_gvalue(
750 const struct ksyms_gvalue *p,
751 struct netbsd32_ksyms_gvalue *s32p,
752 u_long cmd)
753 {
754
755 NETBSD32PTR32(s32p->kv_name, p->kv_name);
756 s32p->kv_value = p->kv_value;
757 }
758
759 /*
760 * main ioctl syscall.
761 *
762 * ok, here we are in the biggy. we have to do fix ups depending
763 * on the ioctl command before and afterwards.
764 */
765 int
766 netbsd32_ioctl(struct lwp *l, const struct netbsd32_ioctl_args *uap, register_t *retval)
767 {
768 /* {
769 syscallarg(int) fd;
770 syscallarg(netbsd32_u_long) com;
771 syscallarg(netbsd32_voidp) data;
772 } */
773 struct proc *p = l->l_proc;
774 struct file *fp;
775 struct filedesc *fdp;
776 u_long com;
777 int error = 0;
778 size_t size;
779 size_t alloc_size32, size32;
780 void *data, *memp = NULL;
781 void *data32, *memp32 = NULL;
782 unsigned int fd;
783 fdfile_t *ff;
784 int tmp;
785 #define STK_PARAMS 128
786 uint64_t stkbuf[STK_PARAMS/sizeof(uint64_t)];
787 uint64_t stkbuf32[STK_PARAMS/sizeof(uint64_t)];
788
789 /*
790 * we need to translate some commands (_IOW) before calling sys_ioctl,
791 * some after (_IOR), and some both (_IOWR).
792 */
793 #if 0
794 {
795 const char * const dirs[8] = {
796 "NONE!", "VOID", "OUT", "VOID|OUT!", "IN", "VOID|IN!",
797 "INOUT", "VOID|IN|OUT!"
798 };
799
800 printf("netbsd32_ioctl(%d, %x, %x): "
801 "%s group %c base %d len %d\n",
802 SCARG(uap, fd), SCARG(uap, com), SCARG(uap, data).i32,
803 dirs[((SCARG(uap, com) & IOC_DIRMASK)>>29)],
804 IOCGROUP(SCARG(uap, com)), IOCBASECMD(SCARG(uap, com)),
805 IOCPARM_LEN(SCARG(uap, com)));
806 }
807 #endif
808
809 memp = NULL;
810 memp32 = NULL;
811 alloc_size32 = 0;
812 size32 = 0;
813 size = 0;
814
815 fdp = p->p_fd;
816 fd = SCARG(uap, fd);
817 if ((fp = fd_getfile(fd)) == NULL)
818 return (EBADF);
819 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
820 error = EBADF;
821 goto out;
822 }
823
824 ff = fdp->fd_dt->dt_ff[SCARG(uap, fd)];
825 switch (com = SCARG(uap, com)) {
826 case FIOCLEX:
827 ff->ff_exclose = true;
828 fdp->fd_exclose = true;
829 goto out;
830
831 case FIONCLEX:
832 ff->ff_exclose = false;
833 goto out;
834 }
835
836 /*
837 * Interpret high order word to find amount of data to be
838 * copied to/from the user's address space.
839 */
840 size32 = IOCPARM_LEN(com);
841 alloc_size32 = size32;
842
843 /*
844 * The disklabel is now padded to a multiple of 8 bytes however the old
845 * disklabel on 32bit platforms wasn't. This leaves a difference in
846 * size of 4 bytes between the two but are otherwise identical.
847 * To deal with this, we allocate enough space for the new disklabel
848 * but only copyin/out the smaller amount.
849 */
850 if (IOCGROUP(com) == 'd') {
851 u_long ncom = com ^ (DIOCGDINFO ^ DIOCGDINFO32);
852 switch (ncom) {
853 case DIOCGDINFO:
854 case DIOCWDINFO:
855 case DIOCSDINFO:
856 case DIOCGDEFLABEL:
857 com = ncom;
858 if (IOCPARM_LEN(DIOCGDINFO32) < IOCPARM_LEN(DIOCGDINFO))
859 alloc_size32 = IOCPARM_LEN(DIOCGDINFO);
860 break;
861 }
862 }
863 if (alloc_size32 > IOCPARM_MAX) {
864 error = ENOTTY;
865 goto out;
866 }
867 if (alloc_size32 > sizeof(stkbuf)) {
868 memp32 = kmem_alloc(alloc_size32, KM_SLEEP);
869 data32 = memp32;
870 } else
871 data32 = (void *)stkbuf32;
872 if ((com >> IOCPARM_SHIFT) == 0) {
873 /* UNIX-style ioctl. */
874 data32 = SCARG_P32(uap, data);
875 } else {
876 if (com&IOC_IN) {
877 if (size32) {
878 error = copyin(SCARG_P32(uap, data), data32,
879 size32);
880 if (error) {
881 goto out;
882 }
883 /*
884 * The data between size and alloc_size has
885 * not been overwritten. It shouldn't matter
886 * but let's clear that anyway.
887 */
888 if (__predict_false(size32 < alloc_size32)) {
889 memset((char *)data32+size32, 0,
890 alloc_size32 - size32);
891 }
892 ktrgenio(fd, UIO_WRITE, SCARG_P32(uap, data),
893 size32, 0);
894 } else
895 *(void **)data32 = SCARG_P32(uap, data);
896 } else if ((com&IOC_OUT) && size32) {
897 /*
898 * Zero the buffer so the user always
899 * gets back something deterministic.
900 */
901 memset(data32, 0, alloc_size32);
902 } else if (com&IOC_VOID) {
903 *(void **)data32 = SCARG_P32(uap, data);
904 }
905 }
906
907 /*
908 * convert various structures, pointers, and other objects that
909 * change size from 32 bit -> 64 bit, for all ioctl commands.
910 */
911 switch (SCARG(uap, com)) {
912 case FIONBIO:
913 mutex_enter(&fp->f_lock);
914 if ((tmp = *(int *)data32) != 0)
915 fp->f_flag |= FNONBLOCK;
916 else
917 fp->f_flag &= ~FNONBLOCK;
918 mutex_exit(&fp->f_lock);
919 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (void *)&tmp);
920 break;
921
922 case FIOASYNC:
923 mutex_enter(&fp->f_lock);
924 if ((tmp = *(int *)data32) != 0)
925 fp->f_flag |= FASYNC;
926 else
927 fp->f_flag &= ~FASYNC;
928 mutex_exit(&fp->f_lock);
929 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (void *)&tmp);
930 break;
931
932 case AUDIO_WSEEK32:
933 IOCTL_CONV_TO(AUDIO_WSEEK, u_long);
934
935 case DIOCGPART32:
936 IOCTL_STRUCT_CONV_TO(DIOCGPART, partinfo);
937 #if 0 /* not implemented by anything */
938 case DIOCRFORMAT32:
939 IOCTL_STRUCT_CONV_TO(DIOCRFORMAT, format_op);
940 case DIOCWFORMAT32:
941 IOCTL_STRUCT_CONV_TO(DIOCWFORMAT, format_op);
942 #endif
943
944 case ATAIOCCOMMAND32:
945 IOCTL_STRUCT_CONV_TO(ATAIOCCOMMAND, atareq);
946
947 case SIOCIFGCLONERS32:
948 {
949 struct netbsd32_if_clonereq *req =
950 (struct netbsd32_if_clonereq *)data32;
951 char *buf = NETBSD32PTR64(req->ifcr_buffer);
952
953 error = if_clone_list(req->ifcr_count,
954 buf, &req->ifcr_total);
955 break;
956 }
957
958 /*
959 * only a few ifreq syscalls need conversion and those are
960 * all driver specific... XXX
961 */
962 #if 0
963 case SIOCGADDRROM3232:
964 IOCTL_STRUCT_CONV_TO(SIOCGADDRROM32, ifreq);
965 case SIOCGCHIPID32:
966 IOCTL_STRUCT_CONV_TO(SIOCGCHIPID, ifreq);
967 case SIOCSIFADDR32:
968 IOCTL_STRUCT_CONV_TO(SIOCSIFADDR, ifreq);
969 case OSIOCGIFADDR32:
970 IOCTL_STRUCT_CONV_TO(OSIOCGIFADDR, ifreq);
971 case SIOCGIFADDR32:
972 IOCTL_STRUCT_CONV_TO(SIOCGIFADDR, ifreq);
973 case SIOCSIFDSTADDR32:
974 IOCTL_STRUCT_CONV_TO(SIOCSIFDSTADDR, ifreq);
975 case OSIOCGIFDSTADDR32:
976 IOCTL_STRUCT_CONV_TO(OSIOCGIFDSTADDR, ifreq);
977 case SIOCGIFDSTADDR32:
978 IOCTL_STRUCT_CONV_TO(SIOCGIFDSTADDR, ifreq);
979 case OSIOCGIFBRDADDR32:
980 IOCTL_STRUCT_CONV_TO(OSIOCGIFBRDADDR, ifreq);
981 case SIOCGIFBRDADDR32:
982 IOCTL_STRUCT_CONV_TO(SIOCGIFBRDADDR, ifreq);
983 case SIOCSIFBRDADDR32:
984 IOCTL_STRUCT_CONV_TO(SIOCSIFBRDADDR, ifreq);
985 case OSIOCGIFNETMASK32:
986 IOCTL_STRUCT_CONV_TO(OSIOCGIFNETMASK, ifreq);
987 case SIOCGIFNETMASK32:
988 IOCTL_STRUCT_CONV_TO(SIOCGIFNETMASK, ifreq);
989 case SIOCSIFNETMASK32:
990 IOCTL_STRUCT_CONV_TO(SIOCSIFNETMASK, ifreq);
991 case SIOCGIFMETRIC32:
992 IOCTL_STRUCT_CONV_TO(SIOCGIFMETRIC, ifreq);
993 case SIOCSIFMETRIC32:
994 IOCTL_STRUCT_CONV_TO(SIOCSIFMETRIC, ifreq);
995 case SIOCDIFADDR32:
996 IOCTL_STRUCT_CONV_TO(SIOCDIFADDR, ifreq);
997 case SIOCADDMULTI32:
998 IOCTL_STRUCT_CONV_TO(SIOCADDMULTI, ifreq);
999 case SIOCDELMULTI32:
1000 IOCTL_STRUCT_CONV_TO(SIOCDELMULTI, ifreq);
1001 case SIOCSIFMEDIA32:
1002 IOCTL_STRUCT_CONV_TO(SIOCSIFMEDIA, ifreq);
1003 case SIOCSIFMTU32:
1004 IOCTL_STRUCT_CONV_TO(SIOCSIFMTU, ifreq);
1005 case SIOCGIFMTU32:
1006 IOCTL_STRUCT_CONV_TO(SIOCGIFMTU, ifreq);
1007 case BIOCGETIF32:
1008 IOCTL_STRUCT_CONV_TO(BIOCGETIF, ifreq);
1009 case BIOCSETIF32:
1010 IOCTL_STRUCT_CONV_TO(BIOCSETIF, ifreq);
1011 case SIOCPHASE132:
1012 IOCTL_STRUCT_CONV_TO(SIOCPHASE1, ifreq);
1013 case SIOCPHASE232:
1014 IOCTL_STRUCT_CONV_TO(SIOCPHASE2, ifreq);
1015 #endif
1016
1017 case OOSIOCGIFCONF32:
1018 IOCTL_STRUCT_CONV_TO(OOSIOCGIFCONF, ifconf);
1019 case OSIOCGIFCONF32:
1020 IOCTL_STRUCT_CONV_TO(OSIOCGIFCONF, ifconf);
1021 case SIOCGIFCONF32:
1022 IOCTL_STRUCT_CONV_TO(SIOCGIFCONF, ifconf);
1023
1024 case SIOCGIFFLAGS32:
1025 IOCTL_STRUCT_CONV_TO(SIOCGIFFLAGS, ifreq);
1026 case SIOCSIFFLAGS32:
1027 IOCTL_STRUCT_CONV_TO(SIOCSIFFLAGS, ifreq);
1028
1029 case SIOCGIFADDRPREF32:
1030 IOCTL_STRUCT_CONV_TO(SIOCGIFADDRPREF, if_addrprefreq);
1031 case SIOCSIFADDRPREF32:
1032 IOCTL_STRUCT_CONV_TO(SIOCSIFADDRPREF, if_addrprefreq);
1033
1034
1035 case OSIOCGIFFLAGS32:
1036 IOCTL_STRUCT_CONV_TO(OSIOCGIFFLAGS, oifreq);
1037 case OSIOCSIFFLAGS32:
1038 IOCTL_STRUCT_CONV_TO(OSIOCSIFFLAGS, oifreq);
1039
1040 case SIOCGIFMEDIA32:
1041 IOCTL_STRUCT_CONV_TO(SIOCGIFMEDIA, ifmediareq);
1042
1043 case SIOCSDRVSPEC32:
1044 IOCTL_STRUCT_CONV_TO(SIOCSDRVSPEC, ifdrv);
1045 case SIOCGDRVSPEC32:
1046 IOCTL_STRUCT_CONV_TO(SIOCGDRVSPEC, ifdrv);
1047
1048 case SIOCGETVIFCNT32:
1049 IOCTL_STRUCT_CONV_TO(SIOCGETVIFCNT, sioc_vif_req);
1050
1051 case SIOCGETSGCNT32:
1052 IOCTL_STRUCT_CONV_TO(SIOCGETSGCNT, sioc_sg_req);
1053
1054 case VNDIOCSET32:
1055 IOCTL_STRUCT_CONV_TO(VNDIOCSET, vnd_ioctl);
1056
1057 case VNDIOCCLR32:
1058 IOCTL_STRUCT_CONV_TO(VNDIOCCLR, vnd_ioctl);
1059
1060 case VNDIOCGET32:
1061 IOCTL_STRUCT_CONV_TO(VNDIOCGET, vnd_user);
1062
1063 case VNDIOCSET5032:
1064 IOCTL_STRUCT_CONV_TO(VNDIOCSET50, vnd_ioctl50);
1065
1066 case VNDIOCCLR5032:
1067 IOCTL_STRUCT_CONV_TO(VNDIOCCLR50, vnd_ioctl50);
1068
1069 case ENVSYS_GETDICTIONARY32:
1070 IOCTL_STRUCT_CONV_TO(ENVSYS_GETDICTIONARY, plistref);
1071 case ENVSYS_SETDICTIONARY32:
1072 IOCTL_STRUCT_CONV_TO(ENVSYS_SETDICTIONARY, plistref);
1073 case ENVSYS_REMOVEPROPS32:
1074 IOCTL_STRUCT_CONV_TO(ENVSYS_REMOVEPROPS, plistref);
1075
1076 case WDOGIOC_GWDOGS32:
1077 IOCTL_STRUCT_CONV_TO(WDOGIOC_GWDOGS, wdog_conf);
1078
1079 case BIOCSETF32:
1080 IOCTL_STRUCT_CONV_TO(BIOCSETF, bpf_program);
1081 case BIOCSTCPF32:
1082 IOCTL_STRUCT_CONV_TO(BIOCSTCPF, bpf_program);
1083 case BIOCSUDPF32:
1084 IOCTL_STRUCT_CONV_TO(BIOCSUDPF, bpf_program);
1085 case BIOCGDLTLIST32:
1086 IOCTL_STRUCT_CONV_TO(BIOCGDLTLIST, bpf_dltlist);
1087
1088 case WSDISPLAYIO_ADDSCREEN32:
1089 IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_ADDSCREEN, wsdisplay_addscreendata);
1090
1091 case WSDISPLAYIO_GCURSOR32:
1092 IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_GCURSOR, wsdisplay_cursor);
1093 case WSDISPLAYIO_SCURSOR32:
1094 IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_SCURSOR, wsdisplay_cursor);
1095
1096 case WSDISPLAYIO_GETCMAP32:
1097 IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_GETCMAP, wsdisplay_cmap);
1098 case WSDISPLAYIO_PUTCMAP32:
1099 IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_PUTCMAP, wsdisplay_cmap);
1100
1101 case SIOCS8021132:
1102 IOCTL_STRUCT_CONV_TO(SIOCS80211, ieee80211req);
1103 case SIOCG8021132:
1104 IOCTL_STRUCT_CONV_TO(SIOCG80211, ieee80211req);
1105 case SIOCS80211NWKEY32:
1106 IOCTL_STRUCT_CONV_TO(SIOCS80211NWKEY, ieee80211_nwkey);
1107 case SIOCG80211NWKEY32:
1108 IOCTL_STRUCT_CONV_TO(SIOCG80211NWKEY, ieee80211_nwkey);
1109
1110 case POWER_EVENT_RECVDICT32:
1111 IOCTL_STRUCT_CONV_TO(POWER_EVENT_RECVDICT, plistref);
1112
1113 case CLOCKCTL_SETTIMEOFDAY32:
1114 IOCTL_STRUCT_CONV_TO(CLOCKCTL_SETTIMEOFDAY,
1115 clockctl_settimeofday);
1116 case CLOCKCTL_ADJTIME32:
1117 IOCTL_STRUCT_CONV_TO(CLOCKCTL_ADJTIME, clockctl_adjtime);
1118 case CLOCKCTL_CLOCK_SETTIME32:
1119 IOCTL_STRUCT_CONV_TO(CLOCKCTL_CLOCK_SETTIME,
1120 clockctl_clock_settime);
1121 case CLOCKCTL_NTP_ADJTIME32:
1122 IOCTL_STRUCT_CONV_TO(CLOCKCTL_NTP_ADJTIME,
1123 clockctl_ntp_adjtime);
1124
1125 case KIOCGSYMBOL32:
1126 IOCTL_STRUCT_CONV_TO(KIOCGSYMBOL, ksyms_gsymbol);
1127 case KIOCGVALUE32:
1128 IOCTL_STRUCT_CONV_TO(KIOCGVALUE, ksyms_gvalue);
1129
1130 default:
1131 #ifdef NETBSD32_MD_IOCTL
1132 error = netbsd32_md_ioctl(fp, com, data32, l);
1133 #else
1134 error = (*fp->f_ops->fo_ioctl)(fp, com, data32);
1135 #endif
1136 break;
1137 }
1138
1139 if (error == EPASSTHROUGH)
1140 error = ENOTTY;
1141
1142 /*
1143 * Copy any data to user, size was
1144 * already set and checked above.
1145 */
1146 if (error == 0 && (com&IOC_OUT) && size32) {
1147 error = copyout(data32, SCARG_P32(uap, data), size32);
1148 ktrgenio(fd, UIO_READ, SCARG_P32(uap, data),
1149 size32, error);
1150 }
1151
1152 out:
1153 /* If we allocated data, free it here. */
1154 if (memp32)
1155 kmem_free(memp32, alloc_size32);
1156 if (memp)
1157 kmem_free(memp, size);
1158 fd_putfile(fd);
1159 return (error);
1160 }
1161