master
c 436 lines 13.1 KB
Raw
1 /*
2 * BSD ioctl(2) emulation
3 *
4 * Copyright (c) 2013-2015 Stacey D. Son
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8 #include "qemu/osdep.h"
9
10 #include <sys/types.h>
11 #include <sys/param.h>
12 #include <sys/disk.h>
13 #include <sys/ioccom.h>
14 #include <sys/ioctl.h>
15 #include <sys/sockio.h>
16 #include <sys/_termios.h>
17 #include <sys/ttycom.h>
18 #include <sys/filio.h>
19
20 #include <crypto/cryptodev.h>
21
22 #include <net/ethernet.h>
23 #include <net/if.h>
24 #include <net/if_var.h>
25 #include <net/if_types.h>
26 #include <net/route.h>
27 #include <net/if_dl.h>
28 #include <net/if_gif.h>
29 #include <net/if_gre.h>
30 #include <net/if_lagg.h>
31 #include <net/if_media.h>
32 #include <net/pfvar.h>
33 #include <net/if_pfsync.h>
34 #include <netinet/icmp6.h>
35 #include <netinet/in.h>
36 #include <netinet/ip_carp.h>
37 #include <netinet6/in6_var.h>
38 #include <netinet6/nd6.h>
39 #include <net80211/ieee80211_ioctl.h>
40
41 #include <stdio.h>
42
43 #include "qemu.h"
44
45 #include "syscall_defs.h"
46 #include "bsd-ioctl.h"
47 #include "os-ioctl-cryptodev.h"
48 #include "os-ioctl-filio.h"
49 #include "os-ioctl-in6_var.h"
50 #include "os-ioctl-sockio.h"
51 #include "os-ioctl-ttycom.h"
52 #include "os-ioctl-disk.h"
53
54 static void target_to_host_termios(void *dst, const void *src)
55 {
56 struct termios *host = dst;
57 const struct target_termios *target = src;
58
59 host->c_iflag = target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
60 host->c_oflag = target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
61 host->c_cflag = target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
62 host->c_lflag = target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
63
64 memset(host->c_cc, 0, sizeof(host->c_cc));
65 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
66 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
67 #ifdef VEOL2
68 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
69 #endif
70 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
71 #ifdef VWERASE
72 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
73 #endif
74 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
75 #ifdef VREPRINT
76 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
77 #endif
78 #ifdef VERASE2
79 host->c_cc[VERASE2] = target->c_cc[TARGET_VERASE2];
80 #endif
81 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
82 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
83 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
84 #ifdef VDSUSP
85 host->c_cc[VDSUSP] = target->c_cc[TARGET_VDSUSP];
86 #endif
87 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
88 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
89 #ifdef VLNEXT
90 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
91 #endif
92 #ifdef VDISCARD
93 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
94 #endif
95 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
96 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
97 #ifdef VSTATUS
98 host->c_cc[VSTATUS] = target->c_cc[TARGET_VSTATUS];
99 #endif
100
101 host->c_ispeed = tswap32(target->c_ispeed);
102 host->c_ospeed = tswap32(target->c_ospeed);
103 }
104
105 static void host_to_target_termios(void *dst, const void *src)
106 {
107 struct target_termios *target = dst;
108 const struct termios *host = src;
109
110 target->c_iflag = tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
111 target->c_oflag = tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
112 target->c_cflag = tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
113 target->c_lflag = tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
114
115 memset(target->c_cc, 0, sizeof(target->c_cc));
116 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
117 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
118 #ifdef VEOL2
119 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
120 #endif
121 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
122 #ifdef VWERASE
123 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
124 #endif
125 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
126 #ifdef VREPRINT
127 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
128 #endif
129 #ifdef VERASE2
130 target->c_cc[TARGET_VERASE2] = host->c_cc[VERASE2];
131 #endif
132 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
133 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
134 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
135 #ifdef VDSUSP
136 target->c_cc[TARGET_VDSUSP] = host->c_cc[VDSUSP];
137 #endif
138 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
139 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
140 #ifdef VLNEXT
141 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
142 #endif
143 #ifdef VDISCARD
144 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
145 #endif
146 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
147 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
148 #ifdef VSTATUS
149 target->c_cc[TARGET_VSTATUS] = host->c_cc[VSTATUS];
150 #endif
151
152 target->c_ispeed = tswap32(host->c_ispeed);
153 target->c_ospeed = tswap32(host->c_ospeed);
154 }
155
156 static const StructEntry struct_termios_def = {
157 .convert = { host_to_target_termios, target_to_host_termios },
158 .size = { sizeof(struct target_termios), sizeof(struct termios) },
159 .align = { __alignof__(struct target_termios),
160 __alignof__(struct termios) },
161 };
162
163 /* ioctl structure type definitions */
164 #define STRUCT(name, ...) STRUCT_ ## name,
165 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
166 enum {
167 #include "os-ioctl-types.h"
168 STRUCT_MAX
169 };
170 #undef STRUCT
171 #undef STRUCT_SPECIAL
172
173 #define STRUCT(name, ...) \
174 static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
175 #define STRUCT_SPECIAL(name)
176 #include "os-ioctl-types.h"
177 #undef STRUCT
178 #undef STRUCT_SPECIAL
179
180
181 struct IOCTLEntry;
182
183 typedef abi_long do_ioctl_fn(const struct IOCTLEntry *ie, uint8_t *buf_temp,
184 int fd, abi_long cmd, abi_long arg);
185
186 struct IOCTLEntry {
187 unsigned int target_cmd;
188 unsigned int host_cmd;
189 const char *name;
190 int access;
191 do_ioctl_fn *do_ioctl;
192 const argtype arg_type[5];
193 };
194 typedef struct IOCTLEntry IOCTLEntry;
195
196 #define MAX_STRUCT_SIZE 4096
197
198 static abi_long do_ioctl_unsupported(__unused const IOCTLEntry *ie,
199 __unused uint8_t *buf_temp,
200 __unused int fd, __unused abi_long cmd,
201 __unused abi_long arg);
202
203 static abi_long do_ioctl_in6_ifreq_sockaddr_int(const IOCTLEntry *ie,
204 uint8_t *buf_temp, int fd, abi_long cmd, abi_long arg);
205
206 static IOCTLEntry ioctl_entries[] = {
207 #define IOC_ 0x0000
208 #define IOC_R 0x0001
209 #define IOC_W 0x0002
210 #define IOC_RW (IOC_R | IOC_W)
211 #define IOCTL(cmd, access, ...) \
212 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
213 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
214 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
215 #define IOCTL_SPECIAL_UNIMPL(cmd, access, dofn, ...) \
216 { TARGET_ ## cmd, 0, #cmd, access, dofn, { __VA_ARGS__ } },
217 #include "os-ioctl-cmds.h"
218 { 0, 0 },
219 };
220
221 static void log_unsupported_ioctl(unsigned long cmd)
222 {
223 gemu_log("cmd=0x%08lx dir=", cmd);
224 switch (cmd & IOC_DIRMASK) {
225 case IOC_VOID:
226 gemu_log("VOID ");
227 break;
228 case IOC_OUT:
229 gemu_log("OUT ");
230 break;
231 case IOC_IN:
232 gemu_log("IN ");
233 break;
234 case IOC_INOUT:
235 gemu_log("INOUT");
236 break;
237 default:
238 gemu_log("%01lx ???", (cmd & IOC_DIRMASK) >> 29);
239 break;
240 }
241 gemu_log(" '%c' %3d %lu\n", (char)IOCGROUP(cmd), (int)(cmd & 0xff),
242 IOCPARM_LEN(cmd));
243 }
244
245 static abi_long do_ioctl_unsupported(__unused const IOCTLEntry *ie,
246 __unused uint8_t *buf_temp,
247 __unused int fd, __unused abi_long cmd,
248 __unused abi_long arg)
249 {
250 return -TARGET_ENXIO;
251 }
252
253 static void target_to_host_sockaddr_in6(struct sockaddr_in6 *hsa_in6,
254 struct target_sockaddr_in6 *tsa_in6)
255 {
256 __get_user(hsa_in6->sin6_len, &tsa_in6->sin6_len);
257 __get_user(hsa_in6->sin6_family, &tsa_in6->sin6_family);
258 __get_user(hsa_in6->sin6_port, &tsa_in6->sin6_port);
259 __get_user(hsa_in6->sin6_flowinfo, &tsa_in6->sin6_flowinfo);
260 memcpy(&hsa_in6->sin6_addr, &tsa_in6->sin6_addr, 16);
261 __get_user(hsa_in6->sin6_scope_id, &tsa_in6->sin6_scope_id);
262 }
263
264 /*
265 * For ioctl()'s such as SIOCGIFAFLAG_IN6 and SIOCGIFALIFETIME_IN6 that
266 * passes a struct sockaddr_in6 in and gets an int out using
267 * struct in6_ifreq.
268 */
269 static abi_long do_ioctl_in6_ifreq_sockaddr_int(const IOCTLEntry *ie,
270 uint8_t *buf_temp, int fd, abi_long cmd, abi_long arg)
271 {
272 abi_long ret;
273 struct target_in6_ifreq *tin6ifreq;
274 struct target_sockaddr_in6 *tsa_in6;
275 struct in6_ifreq hin6ifreq;
276 struct sockaddr_in6 *hsa_in6 = &hin6ifreq.ifr_ifru.ifru_addr;
277
278 tin6ifreq = lock_user(VERIFY_WRITE, arg, sizeof(*tin6ifreq), 1);
279 if (tin6ifreq == NULL) {
280 return -TARGET_EFAULT;
281 }
282 memcpy(hin6ifreq.ifr_name, tin6ifreq->ifr_name, IFNAMSIZ);
283 tsa_in6 = &tin6ifreq->ifr_ifru.ifru_addr;
284 target_to_host_sockaddr_in6(hsa_in6, tsa_in6);
285
286 ret = get_errno(safe_ioctl(fd, ie->host_cmd, &hin6ifreq));
287 if (!is_error(ret)) {
288 put_user_s32(hin6ifreq.ifr_ifru.ifru_flags6,
289 arg + offsetof(struct target_in6_ifreq, ifr_ifru.ifru_flags6));
290 }
291 unlock_user(tin6ifreq, arg, sizeof(*tin6ifreq));
292
293 return ret;
294 }
295
296 abi_long do_bsd_ioctl(int fd, abi_long cmd, abi_long arg)
297 {
298 const IOCTLEntry *ie;
299 const argtype *arg_type;
300 abi_long ret;
301 uint8_t buf_temp[MAX_STRUCT_SIZE];
302 int target_size;
303 void *argptr;
304
305 ie = ioctl_entries;
306 for (;;) {
307 if (ie->target_cmd == 0) {
308 gemu_log("QEMU unsupported ioctl: ");
309 log_unsupported_ioctl(cmd);
310 return -TARGET_ENOSYS;
311 }
312 if (ie->target_cmd == cmd) {
313 break;
314 }
315 ie++;
316 }
317 arg_type = ie->arg_type;
318 #if defined(DEBUG)
319 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
320 #endif
321 if (ie->do_ioctl) {
322 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
323 }
324
325 switch (arg_type[0]) {
326 case TYPE_NULL:
327 /* no argument */
328 ret = get_errno(safe_ioctl(fd, ie->host_cmd));
329 break;
330
331 case TYPE_PTRVOID:
332 case TYPE_INT:
333 /* int argument */
334 ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
335 break;
336
337 case TYPE_PTR:
338 arg_type++;
339 target_size = thunk_type_size(arg_type, 0);
340 switch (ie->access) {
341 case IOC_R:
342 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
343 if (!is_error(ret)) {
344 argptr = lock_user(VERIFY_WRITE, arg,
345 target_size, 0);
346 if (!argptr) {
347 return -TARGET_EFAULT;
348 }
349 thunk_convert(argptr, buf_temp, arg_type,
350 THUNK_TARGET);
351 unlock_user(argptr, arg, target_size);
352 }
353 break;
354
355 case IOC_W:
356 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
357 if (!argptr) {
358 return -TARGET_EFAULT;
359 }
360 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
361 unlock_user(argptr, arg, 0);
362 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
363 break;
364
365 case IOC_RW:
366 /* fallthrough */
367 default:
368 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
369 if (!argptr) {
370 return -TARGET_EFAULT;
371 }
372 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
373 unlock_user(argptr, arg, 0);
374 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
375 if (!is_error(ret)) {
376 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
377 if (!argptr) {
378 return -TARGET_EFAULT;
379 }
380 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
381 unlock_user(argptr, arg, target_size);
382 }
383 break;
384 }
385 break;
386
387 default:
388 gemu_log("QEMU unknown ioctl: type=%d ", arg_type[0]);
389 log_unsupported_ioctl(cmd);
390 ret = -TARGET_ENOSYS;
391 break;
392 }
393 return ret;
394 }
395
396 void init_bsd_ioctl(void)
397 {
398 IOCTLEntry *ie;
399 const argtype *arg_type;
400 int size;
401
402 thunk_init(STRUCT_MAX);
403
404 #define STRUCT(name, ...) \
405 thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
406 #define STRUCT_SPECIAL(name) \
407 thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
408 #include "os-ioctl-types.h"
409 #undef STRUCT
410 #undef STRUCT_SPECIAL
411
412 /*
413 * Patch the ioctl size if necessary using the fact that no
414 * ioctl has all the bits at '1' in the size field
415 * (IOCPARM_MAX - 1).
416 */
417 ie = ioctl_entries;
418 while (ie->target_cmd != 0) {
419 if (((ie->target_cmd >> TARGET_IOCPARM_SHIFT) &
420 TARGET_IOCPARM_MASK) == TARGET_IOCPARM_MASK) {
421 arg_type = ie->arg_type;
422 if (arg_type[0] != TYPE_PTR) {
423 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
424 ie->target_cmd);
425 exit(1);
426 }
427 arg_type++;
428 size = thunk_type_size(arg_type, 0);
429 ie->target_cmd = (ie->target_cmd &
430 ~(TARGET_IOCPARM_MASK << TARGET_IOCPARM_SHIFT)) |
431 (size << TARGET_IOCPARM_SHIFT);
432 }
433 ie++;
434 }
435
436 }