scsi: esp: check buffer length before reading scsi command
[qemu.git] / linux-user / syscall.c
1 /*
2 * Linux syscalls
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #define _ATFILE_SOURCE
20 #include "qemu/osdep.h"
21 #include "qemu/cutils.h"
22 #include "qemu/path.h"
23 #include <elf.h>
24 #include <endian.h>
25 #include <grp.h>
26 #include <sys/ipc.h>
27 #include <sys/msg.h>
28 #include <sys/wait.h>
29 #include <sys/mount.h>
30 #include <sys/file.h>
31 #include <sys/fsuid.h>
32 #include <sys/personality.h>
33 #include <sys/prctl.h>
34 #include <sys/resource.h>
35 #include <sys/swap.h>
36 #include <linux/capability.h>
37 #include <sched.h>
38 #ifdef __ia64__
39 int __clone2(int (*fn)(void *), void *child_stack_base,
40 size_t stack_size, int flags, void *arg, ...);
41 #endif
42 #include <sys/socket.h>
43 #include <sys/un.h>
44 #include <sys/uio.h>
45 #include <sys/poll.h>
46 #include <sys/times.h>
47 #include <sys/shm.h>
48 #include <sys/sem.h>
49 #include <sys/statfs.h>
50 #include <utime.h>
51 #include <sys/sysinfo.h>
52 #include <sys/signalfd.h>
53 //#include <sys/user.h>
54 #include <netinet/ip.h>
55 #include <netinet/tcp.h>
56 #include <linux/wireless.h>
57 #include <linux/icmp.h>
58 #include "qemu-common.h"
59 #ifdef CONFIG_TIMERFD
60 #include <sys/timerfd.h>
61 #endif
62 #ifdef TARGET_GPROF
63 #include <sys/gmon.h>
64 #endif
65 #ifdef CONFIG_EVENTFD
66 #include <sys/eventfd.h>
67 #endif
68 #ifdef CONFIG_EPOLL
69 #include <sys/epoll.h>
70 #endif
71 #ifdef CONFIG_ATTR
72 #include "qemu/xattr.h"
73 #endif
74 #ifdef CONFIG_SENDFILE
75 #include <sys/sendfile.h>
76 #endif
77
78 #define termios host_termios
79 #define winsize host_winsize
80 #define termio host_termio
81 #define sgttyb host_sgttyb /* same as target */
82 #define tchars host_tchars /* same as target */
83 #define ltchars host_ltchars /* same as target */
84
85 #include <linux/termios.h>
86 #include <linux/unistd.h>
87 #include <linux/cdrom.h>
88 #include <linux/hdreg.h>
89 #include <linux/soundcard.h>
90 #include <linux/kd.h>
91 #include <linux/mtio.h>
92 #include <linux/fs.h>
93 #if defined(CONFIG_FIEMAP)
94 #include <linux/fiemap.h>
95 #endif
96 #include <linux/fb.h>
97 #include <linux/vt.h>
98 #include <linux/dm-ioctl.h>
99 #include <linux/reboot.h>
100 #include <linux/route.h>
101 #include <linux/filter.h>
102 #include <linux/blkpg.h>
103 #include <linux/netlink.h>
104 #ifdef CONFIG_RTNETLINK
105 #include <linux/rtnetlink.h>
106 #endif
107 #include <linux/audit.h>
108 #include "linux_loop.h"
109 #include "uname.h"
110
111 #include "qemu.h"
112
113 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
114 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
115
116 //#define DEBUG
117 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
118 * once. This exercises the codepaths for restart.
119 */
120 //#define DEBUG_ERESTARTSYS
121
122 //#include <linux/msdos_fs.h>
123 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
124 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
125
126 /* This is the size of the host kernel's sigset_t, needed where we make
127 * direct system calls that take a sigset_t pointer and a size.
128 */
129 #define SIGSET_T_SIZE (_NSIG / 8)
130
131 #undef _syscall0
132 #undef _syscall1
133 #undef _syscall2
134 #undef _syscall3
135 #undef _syscall4
136 #undef _syscall5
137 #undef _syscall6
138
139 #define _syscall0(type,name) \
140 static type name (void) \
141 { \
142 return syscall(__NR_##name); \
143 }
144
145 #define _syscall1(type,name,type1,arg1) \
146 static type name (type1 arg1) \
147 { \
148 return syscall(__NR_##name, arg1); \
149 }
150
151 #define _syscall2(type,name,type1,arg1,type2,arg2) \
152 static type name (type1 arg1,type2 arg2) \
153 { \
154 return syscall(__NR_##name, arg1, arg2); \
155 }
156
157 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
158 static type name (type1 arg1,type2 arg2,type3 arg3) \
159 { \
160 return syscall(__NR_##name, arg1, arg2, arg3); \
161 }
162
163 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
164 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
165 { \
166 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
167 }
168
169 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
170 type5,arg5) \
171 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
172 { \
173 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
174 }
175
176
177 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
178 type5,arg5,type6,arg6) \
179 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
180 type6 arg6) \
181 { \
182 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
183 }
184
185
186 #define __NR_sys_uname __NR_uname
187 #define __NR_sys_getcwd1 __NR_getcwd
188 #define __NR_sys_getdents __NR_getdents
189 #define __NR_sys_getdents64 __NR_getdents64
190 #define __NR_sys_getpriority __NR_getpriority
191 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
192 #define __NR_sys_syslog __NR_syslog
193 #define __NR_sys_futex __NR_futex
194 #define __NR_sys_inotify_init __NR_inotify_init
195 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
196 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
197
198 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
199 defined(__s390x__)
200 #define __NR__llseek __NR_lseek
201 #endif
202
203 /* Newer kernel ports have llseek() instead of _llseek() */
204 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
205 #define TARGET_NR__llseek TARGET_NR_llseek
206 #endif
207
208 #ifdef __NR_gettid
209 _syscall0(int, gettid)
210 #else
211 /* This is a replacement for the host gettid() and must return a host
212 errno. */
213 static int gettid(void) {
214 return -ENOSYS;
215 }
216 #endif
217 #if defined(TARGET_NR_getdents) && defined(__NR_getdents)
218 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
219 #endif
220 #if !defined(__NR_getdents) || \
221 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
222 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
223 #endif
224 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
225 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
226 loff_t *, res, uint, wh);
227 #endif
228 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
229 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
230 #ifdef __NR_exit_group
231 _syscall1(int,exit_group,int,error_code)
232 #endif
233 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
234 _syscall1(int,set_tid_address,int *,tidptr)
235 #endif
236 #if defined(TARGET_NR_futex) && defined(__NR_futex)
237 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
238 const struct timespec *,timeout,int *,uaddr2,int,val3)
239 #endif
240 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
241 _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
242 unsigned long *, user_mask_ptr);
243 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
244 _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
245 unsigned long *, user_mask_ptr);
246 _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
247 void *, arg);
248 _syscall2(int, capget, struct __user_cap_header_struct *, header,
249 struct __user_cap_data_struct *, data);
250 _syscall2(int, capset, struct __user_cap_header_struct *, header,
251 struct __user_cap_data_struct *, data);
252 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
253 _syscall2(int, ioprio_get, int, which, int, who)
254 #endif
255 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
256 _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
257 #endif
258 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
259 _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
260 #endif
261
262 static bitmask_transtbl fcntl_flags_tbl[] = {
263 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
264 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
265 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
266 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
267 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
268 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
269 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
270 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
271 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
272 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
273 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
274 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
275 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
276 #if defined(O_DIRECT)
277 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
278 #endif
279 #if defined(O_NOATIME)
280 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
281 #endif
282 #if defined(O_CLOEXEC)
283 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
284 #endif
285 #if defined(O_PATH)
286 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
287 #endif
288 /* Don't terminate the list prematurely on 64-bit host+guest. */
289 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
290 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
291 #endif
292 { 0, 0, 0, 0 }
293 };
294
295 typedef abi_long (*TargetFdDataFunc)(void *, size_t);
296 typedef abi_long (*TargetFdAddrFunc)(void *, abi_ulong, socklen_t);
297 typedef struct TargetFdTrans {
298 TargetFdDataFunc host_to_target_data;
299 TargetFdDataFunc target_to_host_data;
300 TargetFdAddrFunc target_to_host_addr;
301 } TargetFdTrans;
302
303 static TargetFdTrans **target_fd_trans;
304
305 static unsigned int target_fd_max;
306
307 static TargetFdDataFunc fd_trans_target_to_host_data(int fd)
308 {
309 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
310 return target_fd_trans[fd]->target_to_host_data;
311 }
312 return NULL;
313 }
314
315 static TargetFdDataFunc fd_trans_host_to_target_data(int fd)
316 {
317 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
318 return target_fd_trans[fd]->host_to_target_data;
319 }
320 return NULL;
321 }
322
323 static TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
324 {
325 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
326 return target_fd_trans[fd]->target_to_host_addr;
327 }
328 return NULL;
329 }
330
331 static void fd_trans_register(int fd, TargetFdTrans *trans)
332 {
333 unsigned int oldmax;
334
335 if (fd >= target_fd_max) {
336 oldmax = target_fd_max;
337 target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */
338 target_fd_trans = g_renew(TargetFdTrans *,
339 target_fd_trans, target_fd_max);
340 memset((void *)(target_fd_trans + oldmax), 0,
341 (target_fd_max - oldmax) * sizeof(TargetFdTrans *));
342 }
343 target_fd_trans[fd] = trans;
344 }
345
346 static void fd_trans_unregister(int fd)
347 {
348 if (fd >= 0 && fd < target_fd_max) {
349 target_fd_trans[fd] = NULL;
350 }
351 }
352
353 static void fd_trans_dup(int oldfd, int newfd)
354 {
355 fd_trans_unregister(newfd);
356 if (oldfd < target_fd_max && target_fd_trans[oldfd]) {
357 fd_trans_register(newfd, target_fd_trans[oldfd]);
358 }
359 }
360
361 static int sys_getcwd1(char *buf, size_t size)
362 {
363 if (getcwd(buf, size) == NULL) {
364 /* getcwd() sets errno */
365 return (-1);
366 }
367 return strlen(buf)+1;
368 }
369
370 #ifdef TARGET_NR_utimensat
371 #ifdef CONFIG_UTIMENSAT
372 static int sys_utimensat(int dirfd, const char *pathname,
373 const struct timespec times[2], int flags)
374 {
375 if (pathname == NULL)
376 return futimens(dirfd, times);
377 else
378 return utimensat(dirfd, pathname, times, flags);
379 }
380 #elif defined(__NR_utimensat)
381 #define __NR_sys_utimensat __NR_utimensat
382 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
383 const struct timespec *,tsp,int,flags)
384 #else
385 static int sys_utimensat(int dirfd, const char *pathname,
386 const struct timespec times[2], int flags)
387 {
388 errno = ENOSYS;
389 return -1;
390 }
391 #endif
392 #endif /* TARGET_NR_utimensat */
393
394 #ifdef CONFIG_INOTIFY
395 #include <sys/inotify.h>
396
397 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
398 static int sys_inotify_init(void)
399 {
400 return (inotify_init());
401 }
402 #endif
403 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
404 static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
405 {
406 return (inotify_add_watch(fd, pathname, mask));
407 }
408 #endif
409 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
410 static int sys_inotify_rm_watch(int fd, int32_t wd)
411 {
412 return (inotify_rm_watch(fd, wd));
413 }
414 #endif
415 #ifdef CONFIG_INOTIFY1
416 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
417 static int sys_inotify_init1(int flags)
418 {
419 return (inotify_init1(flags));
420 }
421 #endif
422 #endif
423 #else
424 /* Userspace can usually survive runtime without inotify */
425 #undef TARGET_NR_inotify_init
426 #undef TARGET_NR_inotify_init1
427 #undef TARGET_NR_inotify_add_watch
428 #undef TARGET_NR_inotify_rm_watch
429 #endif /* CONFIG_INOTIFY */
430
431 #if defined(TARGET_NR_prlimit64)
432 #ifndef __NR_prlimit64
433 # define __NR_prlimit64 -1
434 #endif
435 #define __NR_sys_prlimit64 __NR_prlimit64
436 /* The glibc rlimit structure may not be that used by the underlying syscall */
437 struct host_rlimit64 {
438 uint64_t rlim_cur;
439 uint64_t rlim_max;
440 };
441 _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
442 const struct host_rlimit64 *, new_limit,
443 struct host_rlimit64 *, old_limit)
444 #endif
445
446
447 #if defined(TARGET_NR_timer_create)
448 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
449 static timer_t g_posix_timers[32] = { 0, } ;
450
451 static inline int next_free_host_timer(void)
452 {
453 int k ;
454 /* FIXME: Does finding the next free slot require a lock? */
455 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
456 if (g_posix_timers[k] == 0) {
457 g_posix_timers[k] = (timer_t) 1;
458 return k;
459 }
460 }
461 return -1;
462 }
463 #endif
464
465 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
466 #ifdef TARGET_ARM
467 static inline int regpairs_aligned(void *cpu_env) {
468 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
469 }
470 #elif defined(TARGET_MIPS)
471 static inline int regpairs_aligned(void *cpu_env) { return 1; }
472 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
473 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
474 * of registers which translates to the same as ARM/MIPS, because we start with
475 * r3 as arg1 */
476 static inline int regpairs_aligned(void *cpu_env) { return 1; }
477 #else
478 static inline int regpairs_aligned(void *cpu_env) { return 0; }
479 #endif
480
481 #define ERRNO_TABLE_SIZE 1200
482
483 /* target_to_host_errno_table[] is initialized from
484 * host_to_target_errno_table[] in syscall_init(). */
485 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
486 };
487
488 /*
489 * This list is the union of errno values overridden in asm-<arch>/errno.h
490 * minus the errnos that are not actually generic to all archs.
491 */
492 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
493 [EAGAIN] = TARGET_EAGAIN,
494 [EIDRM] = TARGET_EIDRM,
495 [ECHRNG] = TARGET_ECHRNG,
496 [EL2NSYNC] = TARGET_EL2NSYNC,
497 [EL3HLT] = TARGET_EL3HLT,
498 [EL3RST] = TARGET_EL3RST,
499 [ELNRNG] = TARGET_ELNRNG,
500 [EUNATCH] = TARGET_EUNATCH,
501 [ENOCSI] = TARGET_ENOCSI,
502 [EL2HLT] = TARGET_EL2HLT,
503 [EDEADLK] = TARGET_EDEADLK,
504 [ENOLCK] = TARGET_ENOLCK,
505 [EBADE] = TARGET_EBADE,
506 [EBADR] = TARGET_EBADR,
507 [EXFULL] = TARGET_EXFULL,
508 [ENOANO] = TARGET_ENOANO,
509 [EBADRQC] = TARGET_EBADRQC,
510 [EBADSLT] = TARGET_EBADSLT,
511 [EBFONT] = TARGET_EBFONT,
512 [ENOSTR] = TARGET_ENOSTR,
513 [ENODATA] = TARGET_ENODATA,
514 [ETIME] = TARGET_ETIME,
515 [ENOSR] = TARGET_ENOSR,
516 [ENONET] = TARGET_ENONET,
517 [ENOPKG] = TARGET_ENOPKG,
518 [EREMOTE] = TARGET_EREMOTE,
519 [ENOLINK] = TARGET_ENOLINK,
520 [EADV] = TARGET_EADV,
521 [ESRMNT] = TARGET_ESRMNT,
522 [ECOMM] = TARGET_ECOMM,
523 [EPROTO] = TARGET_EPROTO,
524 [EDOTDOT] = TARGET_EDOTDOT,
525 [EMULTIHOP] = TARGET_EMULTIHOP,
526 [EBADMSG] = TARGET_EBADMSG,
527 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
528 [EOVERFLOW] = TARGET_EOVERFLOW,
529 [ENOTUNIQ] = TARGET_ENOTUNIQ,
530 [EBADFD] = TARGET_EBADFD,
531 [EREMCHG] = TARGET_EREMCHG,
532 [ELIBACC] = TARGET_ELIBACC,
533 [ELIBBAD] = TARGET_ELIBBAD,
534 [ELIBSCN] = TARGET_ELIBSCN,
535 [ELIBMAX] = TARGET_ELIBMAX,
536 [ELIBEXEC] = TARGET_ELIBEXEC,
537 [EILSEQ] = TARGET_EILSEQ,
538 [ENOSYS] = TARGET_ENOSYS,
539 [ELOOP] = TARGET_ELOOP,
540 [ERESTART] = TARGET_ERESTART,
541 [ESTRPIPE] = TARGET_ESTRPIPE,
542 [ENOTEMPTY] = TARGET_ENOTEMPTY,
543 [EUSERS] = TARGET_EUSERS,
544 [ENOTSOCK] = TARGET_ENOTSOCK,
545 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
546 [EMSGSIZE] = TARGET_EMSGSIZE,
547 [EPROTOTYPE] = TARGET_EPROTOTYPE,
548 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
549 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
550 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
551 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
552 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
553 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
554 [EADDRINUSE] = TARGET_EADDRINUSE,
555 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
556 [ENETDOWN] = TARGET_ENETDOWN,
557 [ENETUNREACH] = TARGET_ENETUNREACH,
558 [ENETRESET] = TARGET_ENETRESET,
559 [ECONNABORTED] = TARGET_ECONNABORTED,
560 [ECONNRESET] = TARGET_ECONNRESET,
561 [ENOBUFS] = TARGET_ENOBUFS,
562 [EISCONN] = TARGET_EISCONN,
563 [ENOTCONN] = TARGET_ENOTCONN,
564 [EUCLEAN] = TARGET_EUCLEAN,
565 [ENOTNAM] = TARGET_ENOTNAM,
566 [ENAVAIL] = TARGET_ENAVAIL,
567 [EISNAM] = TARGET_EISNAM,
568 [EREMOTEIO] = TARGET_EREMOTEIO,
569 [ESHUTDOWN] = TARGET_ESHUTDOWN,
570 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
571 [ETIMEDOUT] = TARGET_ETIMEDOUT,
572 [ECONNREFUSED] = TARGET_ECONNREFUSED,
573 [EHOSTDOWN] = TARGET_EHOSTDOWN,
574 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
575 [EALREADY] = TARGET_EALREADY,
576 [EINPROGRESS] = TARGET_EINPROGRESS,
577 [ESTALE] = TARGET_ESTALE,
578 [ECANCELED] = TARGET_ECANCELED,
579 [ENOMEDIUM] = TARGET_ENOMEDIUM,
580 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
581 #ifdef ENOKEY
582 [ENOKEY] = TARGET_ENOKEY,
583 #endif
584 #ifdef EKEYEXPIRED
585 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
586 #endif
587 #ifdef EKEYREVOKED
588 [EKEYREVOKED] = TARGET_EKEYREVOKED,
589 #endif
590 #ifdef EKEYREJECTED
591 [EKEYREJECTED] = TARGET_EKEYREJECTED,
592 #endif
593 #ifdef EOWNERDEAD
594 [EOWNERDEAD] = TARGET_EOWNERDEAD,
595 #endif
596 #ifdef ENOTRECOVERABLE
597 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
598 #endif
599 };
600
601 static inline int host_to_target_errno(int err)
602 {
603 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
604 host_to_target_errno_table[err]) {
605 return host_to_target_errno_table[err];
606 }
607 return err;
608 }
609
610 static inline int target_to_host_errno(int err)
611 {
612 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
613 target_to_host_errno_table[err]) {
614 return target_to_host_errno_table[err];
615 }
616 return err;
617 }
618
619 static inline abi_long get_errno(abi_long ret)
620 {
621 if (ret == -1)
622 return -host_to_target_errno(errno);
623 else
624 return ret;
625 }
626
627 static inline int is_error(abi_long ret)
628 {
629 return (abi_ulong)ret >= (abi_ulong)(-4096);
630 }
631
632 const char *target_strerror(int err)
633 {
634 if (err == TARGET_ERESTARTSYS) {
635 return "To be restarted";
636 }
637 if (err == TARGET_QEMU_ESIGRETURN) {
638 return "Successful exit from sigreturn";
639 }
640
641 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
642 return NULL;
643 }
644 return strerror(target_to_host_errno(err));
645 }
646
647 #define safe_syscall0(type, name) \
648 static type safe_##name(void) \
649 { \
650 return safe_syscall(__NR_##name); \
651 }
652
653 #define safe_syscall1(type, name, type1, arg1) \
654 static type safe_##name(type1 arg1) \
655 { \
656 return safe_syscall(__NR_##name, arg1); \
657 }
658
659 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
660 static type safe_##name(type1 arg1, type2 arg2) \
661 { \
662 return safe_syscall(__NR_##name, arg1, arg2); \
663 }
664
665 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
666 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
667 { \
668 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
669 }
670
671 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
672 type4, arg4) \
673 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
674 { \
675 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
676 }
677
678 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
679 type4, arg4, type5, arg5) \
680 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
681 type5 arg5) \
682 { \
683 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
684 }
685
686 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
687 type4, arg4, type5, arg5, type6, arg6) \
688 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
689 type5 arg5, type6 arg6) \
690 { \
691 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
692 }
693
694 safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
695 safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
696 safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
697 int, flags, mode_t, mode)
698 safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
699 struct rusage *, rusage)
700 safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
701 int, options, struct rusage *, rusage)
702 safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
703 safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
704 fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
705 safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
706 struct timespec *, tsp, const sigset_t *, sigmask,
707 size_t, sigsetsize)
708 safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
709 int, maxevents, int, timeout, const sigset_t *, sigmask,
710 size_t, sigsetsize)
711 safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
712 const struct timespec *,timeout,int *,uaddr2,int,val3)
713 safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
714 safe_syscall2(int, kill, pid_t, pid, int, sig)
715 safe_syscall2(int, tkill, int, tid, int, sig)
716 safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
717 safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
718 safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
719 safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
720 socklen_t, addrlen)
721 safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
722 int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
723 safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
724 int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
725 safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
726 safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
727 safe_syscall2(int, flock, int, fd, int, operation)
728 safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
729 const struct timespec *, uts, size_t, sigsetsize)
730 safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
731 int, flags)
732 safe_syscall2(int, nanosleep, const struct timespec *, req,
733 struct timespec *, rem)
734 #ifdef TARGET_NR_clock_nanosleep
735 safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
736 const struct timespec *, req, struct timespec *, rem)
737 #endif
738 #ifdef __NR_msgsnd
739 safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
740 int, flags)
741 safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
742 long, msgtype, int, flags)
743 safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
744 unsigned, nsops, const struct timespec *, timeout)
745 #else
746 /* This host kernel architecture uses a single ipc syscall; fake up
747 * wrappers for the sub-operations to hide this implementation detail.
748 * Annoyingly we can't include linux/ipc.h to get the constant definitions
749 * for the call parameter because some structs in there conflict with the
750 * sys/ipc.h ones. So we just define them here, and rely on them being
751 * the same for all host architectures.
752 */
753 #define Q_SEMTIMEDOP 4
754 #define Q_MSGSND 11
755 #define Q_MSGRCV 12
756 #define Q_IPCCALL(VERSION, OP) ((VERSION) << 16 | (OP))
757
758 safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
759 void *, ptr, long, fifth)
760 static int safe_msgsnd(int msgid, const void *msgp, size_t sz, int flags)
761 {
762 return safe_ipc(Q_IPCCALL(0, Q_MSGSND), msgid, sz, flags, (void *)msgp, 0);
763 }
764 static int safe_msgrcv(int msgid, void *msgp, size_t sz, long type, int flags)
765 {
766 return safe_ipc(Q_IPCCALL(1, Q_MSGRCV), msgid, sz, flags, msgp, type);
767 }
768 static int safe_semtimedop(int semid, struct sembuf *tsops, unsigned nsops,
769 const struct timespec *timeout)
770 {
771 return safe_ipc(Q_IPCCALL(0, Q_SEMTIMEDOP), semid, nsops, 0, tsops,
772 (long)timeout);
773 }
774 #endif
775 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
776 safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
777 size_t, len, unsigned, prio, const struct timespec *, timeout)
778 safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
779 size_t, len, unsigned *, prio, const struct timespec *, timeout)
780 #endif
781 /* We do ioctl like this rather than via safe_syscall3 to preserve the
782 * "third argument might be integer or pointer or not present" behaviour of
783 * the libc function.
784 */
785 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
786
787 static inline int host_to_target_sock_type(int host_type)
788 {
789 int target_type;
790
791 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
792 case SOCK_DGRAM:
793 target_type = TARGET_SOCK_DGRAM;
794 break;
795 case SOCK_STREAM:
796 target_type = TARGET_SOCK_STREAM;
797 break;
798 default:
799 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
800 break;
801 }
802
803 #if defined(SOCK_CLOEXEC)
804 if (host_type & SOCK_CLOEXEC) {
805 target_type |= TARGET_SOCK_CLOEXEC;
806 }
807 #endif
808
809 #if defined(SOCK_NONBLOCK)
810 if (host_type & SOCK_NONBLOCK) {
811 target_type |= TARGET_SOCK_NONBLOCK;
812 }
813 #endif
814
815 return target_type;
816 }
817
818 static abi_ulong target_brk;
819 static abi_ulong target_original_brk;
820 static abi_ulong brk_page;
821
822 void target_set_brk(abi_ulong new_brk)
823 {
824 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
825 brk_page = HOST_PAGE_ALIGN(target_brk);
826 }
827
828 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
829 #define DEBUGF_BRK(message, args...)
830
831 /* do_brk() must return target values and target errnos. */
832 abi_long do_brk(abi_ulong new_brk)
833 {
834 abi_long mapped_addr;
835 int new_alloc_size;
836
837 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
838
839 if (!new_brk) {
840 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
841 return target_brk;
842 }
843 if (new_brk < target_original_brk) {
844 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
845 target_brk);
846 return target_brk;
847 }
848
849 /* If the new brk is less than the highest page reserved to the
850 * target heap allocation, set it and we're almost done... */
851 if (new_brk <= brk_page) {
852 /* Heap contents are initialized to zero, as for anonymous
853 * mapped pages. */
854 if (new_brk > target_brk) {
855 memset(g2h(target_brk), 0, new_brk - target_brk);
856 }
857 target_brk = new_brk;
858 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
859 return target_brk;
860 }
861
862 /* We need to allocate more memory after the brk... Note that
863 * we don't use MAP_FIXED because that will map over the top of
864 * any existing mapping (like the one with the host libc or qemu
865 * itself); instead we treat "mapped but at wrong address" as
866 * a failure and unmap again.
867 */
868 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
869 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
870 PROT_READ|PROT_WRITE,
871 MAP_ANON|MAP_PRIVATE, 0, 0));
872
873 if (mapped_addr == brk_page) {
874 /* Heap contents are initialized to zero, as for anonymous
875 * mapped pages. Technically the new pages are already
876 * initialized to zero since they *are* anonymous mapped
877 * pages, however we have to take care with the contents that
878 * come from the remaining part of the previous page: it may
879 * contains garbage data due to a previous heap usage (grown
880 * then shrunken). */
881 memset(g2h(target_brk), 0, brk_page - target_brk);
882
883 target_brk = new_brk;
884 brk_page = HOST_PAGE_ALIGN(target_brk);
885 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
886 target_brk);
887 return target_brk;
888 } else if (mapped_addr != -1) {
889 /* Mapped but at wrong address, meaning there wasn't actually
890 * enough space for this brk.
891 */
892 target_munmap(mapped_addr, new_alloc_size);
893 mapped_addr = -1;
894 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
895 }
896 else {
897 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
898 }
899
900 #if defined(TARGET_ALPHA)
901 /* We (partially) emulate OSF/1 on Alpha, which requires we
902 return a proper errno, not an unchanged brk value. */
903 return -TARGET_ENOMEM;
904 #endif
905 /* For everything else, return the previous break. */
906 return target_brk;
907 }
908
909 static inline abi_long copy_from_user_fdset(fd_set *fds,
910 abi_ulong target_fds_addr,
911 int n)
912 {
913 int i, nw, j, k;
914 abi_ulong b, *target_fds;
915
916 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
917 if (!(target_fds = lock_user(VERIFY_READ,
918 target_fds_addr,
919 sizeof(abi_ulong) * nw,
920 1)))
921 return -TARGET_EFAULT;
922
923 FD_ZERO(fds);
924 k = 0;
925 for (i = 0; i < nw; i++) {
926 /* grab the abi_ulong */
927 __get_user(b, &target_fds[i]);
928 for (j = 0; j < TARGET_ABI_BITS; j++) {
929 /* check the bit inside the abi_ulong */
930 if ((b >> j) & 1)
931 FD_SET(k, fds);
932 k++;
933 }
934 }
935
936 unlock_user(target_fds, target_fds_addr, 0);
937
938 return 0;
939 }
940
941 static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
942 abi_ulong target_fds_addr,
943 int n)
944 {
945 if (target_fds_addr) {
946 if (copy_from_user_fdset(fds, target_fds_addr, n))
947 return -TARGET_EFAULT;
948 *fds_ptr = fds;
949 } else {
950 *fds_ptr = NULL;
951 }
952 return 0;
953 }
954
955 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
956 const fd_set *fds,
957 int n)
958 {
959 int i, nw, j, k;
960 abi_long v;
961 abi_ulong *target_fds;
962
963 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
964 if (!(target_fds = lock_user(VERIFY_WRITE,
965 target_fds_addr,
966 sizeof(abi_ulong) * nw,
967 0)))
968 return -TARGET_EFAULT;
969
970 k = 0;
971 for (i = 0; i < nw; i++) {
972 v = 0;
973 for (j = 0; j < TARGET_ABI_BITS; j++) {
974 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
975 k++;
976 }
977 __put_user(v, &target_fds[i]);
978 }
979
980 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
981
982 return 0;
983 }
984
985 #if defined(__alpha__)
986 #define HOST_HZ 1024
987 #else
988 #define HOST_HZ 100
989 #endif
990
991 static inline abi_long host_to_target_clock_t(long ticks)
992 {
993 #if HOST_HZ == TARGET_HZ
994 return ticks;
995 #else
996 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
997 #endif
998 }
999
1000 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
1001 const struct rusage *rusage)
1002 {
1003 struct target_rusage *target_rusage;
1004
1005 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
1006 return -TARGET_EFAULT;
1007 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
1008 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
1009 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
1010 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
1011 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
1012 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
1013 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
1014 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
1015 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
1016 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
1017 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
1018 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
1019 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
1020 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
1021 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
1022 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
1023 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
1024 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
1025 unlock_user_struct(target_rusage, target_addr, 1);
1026
1027 return 0;
1028 }
1029
1030 static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
1031 {
1032 abi_ulong target_rlim_swap;
1033 rlim_t result;
1034
1035 target_rlim_swap = tswapal(target_rlim);
1036 if (target_rlim_swap == TARGET_RLIM_INFINITY)
1037 return RLIM_INFINITY;
1038
1039 result = target_rlim_swap;
1040 if (target_rlim_swap != (rlim_t)result)
1041 return RLIM_INFINITY;
1042
1043 return result;
1044 }
1045
1046 static inline abi_ulong host_to_target_rlim(rlim_t rlim)
1047 {
1048 abi_ulong target_rlim_swap;
1049 abi_ulong result;
1050
1051 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
1052 target_rlim_swap = TARGET_RLIM_INFINITY;
1053 else
1054 target_rlim_swap = rlim;
1055 result = tswapal(target_rlim_swap);
1056
1057 return result;
1058 }
1059
1060 static inline int target_to_host_resource(int code)
1061 {
1062 switch (code) {
1063 case TARGET_RLIMIT_AS:
1064 return RLIMIT_AS;
1065 case TARGET_RLIMIT_CORE:
1066 return RLIMIT_CORE;
1067 case TARGET_RLIMIT_CPU:
1068 return RLIMIT_CPU;
1069 case TARGET_RLIMIT_DATA:
1070 return RLIMIT_DATA;
1071 case TARGET_RLIMIT_FSIZE:
1072 return RLIMIT_FSIZE;
1073 case TARGET_RLIMIT_LOCKS:
1074 return RLIMIT_LOCKS;
1075 case TARGET_RLIMIT_MEMLOCK:
1076 return RLIMIT_MEMLOCK;
1077 case TARGET_RLIMIT_MSGQUEUE:
1078 return RLIMIT_MSGQUEUE;
1079 case TARGET_RLIMIT_NICE:
1080 return RLIMIT_NICE;
1081 case TARGET_RLIMIT_NOFILE:
1082 return RLIMIT_NOFILE;
1083 case TARGET_RLIMIT_NPROC:
1084 return RLIMIT_NPROC;
1085 case TARGET_RLIMIT_RSS:
1086 return RLIMIT_RSS;
1087 case TARGET_RLIMIT_RTPRIO:
1088 return RLIMIT_RTPRIO;
1089 case TARGET_RLIMIT_SIGPENDING:
1090 return RLIMIT_SIGPENDING;
1091 case TARGET_RLIMIT_STACK:
1092 return RLIMIT_STACK;
1093 default:
1094 return code;
1095 }
1096 }
1097
1098 static inline abi_long copy_from_user_timeval(struct timeval *tv,
1099 abi_ulong target_tv_addr)
1100 {
1101 struct target_timeval *target_tv;
1102
1103 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
1104 return -TARGET_EFAULT;
1105
1106 __get_user(tv->tv_sec, &target_tv->tv_sec);
1107 __get_user(tv->tv_usec, &target_tv->tv_usec);
1108
1109 unlock_user_struct(target_tv, target_tv_addr, 0);
1110
1111 return 0;
1112 }
1113
1114 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1115 const struct timeval *tv)
1116 {
1117 struct target_timeval *target_tv;
1118
1119 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
1120 return -TARGET_EFAULT;
1121
1122 __put_user(tv->tv_sec, &target_tv->tv_sec);
1123 __put_user(tv->tv_usec, &target_tv->tv_usec);
1124
1125 unlock_user_struct(target_tv, target_tv_addr, 1);
1126
1127 return 0;
1128 }
1129
1130 static inline abi_long copy_from_user_timezone(struct timezone *tz,
1131 abi_ulong target_tz_addr)
1132 {
1133 struct target_timezone *target_tz;
1134
1135 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1136 return -TARGET_EFAULT;
1137 }
1138
1139 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1140 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1141
1142 unlock_user_struct(target_tz, target_tz_addr, 0);
1143
1144 return 0;
1145 }
1146
1147 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1148 #include <mqueue.h>
1149
1150 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1151 abi_ulong target_mq_attr_addr)
1152 {
1153 struct target_mq_attr *target_mq_attr;
1154
1155 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1156 target_mq_attr_addr, 1))
1157 return -TARGET_EFAULT;
1158
1159 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1160 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1161 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1162 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1163
1164 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1165
1166 return 0;
1167 }
1168
1169 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1170 const struct mq_attr *attr)
1171 {
1172 struct target_mq_attr *target_mq_attr;
1173
1174 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1175 target_mq_attr_addr, 0))
1176 return -TARGET_EFAULT;
1177
1178 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1179 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1180 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1181 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1182
1183 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1184
1185 return 0;
1186 }
1187 #endif
1188
1189 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1190 /* do_select() must return target values and target errnos. */
1191 static abi_long do_select(int n,
1192 abi_ulong rfd_addr, abi_ulong wfd_addr,
1193 abi_ulong efd_addr, abi_ulong target_tv_addr)
1194 {
1195 fd_set rfds, wfds, efds;
1196 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1197 struct timeval tv;
1198 struct timespec ts, *ts_ptr;
1199 abi_long ret;
1200
1201 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1202 if (ret) {
1203 return ret;
1204 }
1205 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1206 if (ret) {
1207 return ret;
1208 }
1209 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1210 if (ret) {
1211 return ret;
1212 }
1213
1214 if (target_tv_addr) {
1215 if (copy_from_user_timeval(&tv, target_tv_addr))
1216 return -TARGET_EFAULT;
1217 ts.tv_sec = tv.tv_sec;
1218 ts.tv_nsec = tv.tv_usec * 1000;
1219 ts_ptr = &ts;
1220 } else {
1221 ts_ptr = NULL;
1222 }
1223
1224 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1225 ts_ptr, NULL));
1226
1227 if (!is_error(ret)) {
1228 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1229 return -TARGET_EFAULT;
1230 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1231 return -TARGET_EFAULT;
1232 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1233 return -TARGET_EFAULT;
1234
1235 if (target_tv_addr) {
1236 tv.tv_sec = ts.tv_sec;
1237 tv.tv_usec = ts.tv_nsec / 1000;
1238 if (copy_to_user_timeval(target_tv_addr, &tv)) {
1239 return -TARGET_EFAULT;
1240 }
1241 }
1242 }
1243
1244 return ret;
1245 }
1246 #endif
1247
1248 static abi_long do_pipe2(int host_pipe[], int flags)
1249 {
1250 #ifdef CONFIG_PIPE2
1251 return pipe2(host_pipe, flags);
1252 #else
1253 return -ENOSYS;
1254 #endif
1255 }
1256
1257 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1258 int flags, int is_pipe2)
1259 {
1260 int host_pipe[2];
1261 abi_long ret;
1262 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1263
1264 if (is_error(ret))
1265 return get_errno(ret);
1266
1267 /* Several targets have special calling conventions for the original
1268 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1269 if (!is_pipe2) {
1270 #if defined(TARGET_ALPHA)
1271 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1272 return host_pipe[0];
1273 #elif defined(TARGET_MIPS)
1274 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1275 return host_pipe[0];
1276 #elif defined(TARGET_SH4)
1277 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
1278 return host_pipe[0];
1279 #elif defined(TARGET_SPARC)
1280 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1281 return host_pipe[0];
1282 #endif
1283 }
1284
1285 if (put_user_s32(host_pipe[0], pipedes)
1286 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1287 return -TARGET_EFAULT;
1288 return get_errno(ret);
1289 }
1290
1291 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1292 abi_ulong target_addr,
1293 socklen_t len)
1294 {
1295 struct target_ip_mreqn *target_smreqn;
1296
1297 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1298 if (!target_smreqn)
1299 return -TARGET_EFAULT;
1300 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1301 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1302 if (len == sizeof(struct target_ip_mreqn))
1303 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
1304 unlock_user(target_smreqn, target_addr, 0);
1305
1306 return 0;
1307 }
1308
1309 static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
1310 abi_ulong target_addr,
1311 socklen_t len)
1312 {
1313 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1314 sa_family_t sa_family;
1315 struct target_sockaddr *target_saddr;
1316
1317 if (fd_trans_target_to_host_addr(fd)) {
1318 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1319 }
1320
1321 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1322 if (!target_saddr)
1323 return -TARGET_EFAULT;
1324
1325 sa_family = tswap16(target_saddr->sa_family);
1326
1327 /* Oops. The caller might send a incomplete sun_path; sun_path
1328 * must be terminated by \0 (see the manual page), but
1329 * unfortunately it is quite common to specify sockaddr_un
1330 * length as "strlen(x->sun_path)" while it should be
1331 * "strlen(...) + 1". We'll fix that here if needed.
1332 * Linux kernel has a similar feature.
1333 */
1334
1335 if (sa_family == AF_UNIX) {
1336 if (len < unix_maxlen && len > 0) {
1337 char *cp = (char*)target_saddr;
1338
1339 if ( cp[len-1] && !cp[len] )
1340 len++;
1341 }
1342 if (len > unix_maxlen)
1343 len = unix_maxlen;
1344 }
1345
1346 memcpy(addr, target_saddr, len);
1347 addr->sa_family = sa_family;
1348 if (sa_family == AF_NETLINK) {
1349 struct sockaddr_nl *nladdr;
1350
1351 nladdr = (struct sockaddr_nl *)addr;
1352 nladdr->nl_pid = tswap32(nladdr->nl_pid);
1353 nladdr->nl_groups = tswap32(nladdr->nl_groups);
1354 } else if (sa_family == AF_PACKET) {
1355 struct target_sockaddr_ll *lladdr;
1356
1357 lladdr = (struct target_sockaddr_ll *)addr;
1358 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1359 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
1360 }
1361 unlock_user(target_saddr, target_addr, 0);
1362
1363 return 0;
1364 }
1365
1366 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1367 struct sockaddr *addr,
1368 socklen_t len)
1369 {
1370 struct target_sockaddr *target_saddr;
1371
1372 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1373 if (!target_saddr)
1374 return -TARGET_EFAULT;
1375 memcpy(target_saddr, addr, len);
1376 target_saddr->sa_family = tswap16(addr->sa_family);
1377 if (addr->sa_family == AF_NETLINK) {
1378 struct sockaddr_nl *target_nl = (struct sockaddr_nl *)target_saddr;
1379 target_nl->nl_pid = tswap32(target_nl->nl_pid);
1380 target_nl->nl_groups = tswap32(target_nl->nl_groups);
1381 }
1382 unlock_user(target_saddr, target_addr, len);
1383
1384 return 0;
1385 }
1386
1387 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1388 struct target_msghdr *target_msgh)
1389 {
1390 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1391 abi_long msg_controllen;
1392 abi_ulong target_cmsg_addr;
1393 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
1394 socklen_t space = 0;
1395
1396 msg_controllen = tswapal(target_msgh->msg_controllen);
1397 if (msg_controllen < sizeof (struct target_cmsghdr))
1398 goto the_end;
1399 target_cmsg_addr = tswapal(target_msgh->msg_control);
1400 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1401 target_cmsg_start = target_cmsg;
1402 if (!target_cmsg)
1403 return -TARGET_EFAULT;
1404
1405 while (cmsg && target_cmsg) {
1406 void *data = CMSG_DATA(cmsg);
1407 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1408
1409 int len = tswapal(target_cmsg->cmsg_len)
1410 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1411
1412 space += CMSG_SPACE(len);
1413 if (space > msgh->msg_controllen) {
1414 space -= CMSG_SPACE(len);
1415 /* This is a QEMU bug, since we allocated the payload
1416 * area ourselves (unlike overflow in host-to-target
1417 * conversion, which is just the guest giving us a buffer
1418 * that's too small). It can't happen for the payload types
1419 * we currently support; if it becomes an issue in future
1420 * we would need to improve our allocation strategy to
1421 * something more intelligent than "twice the size of the
1422 * target buffer we're reading from".
1423 */
1424 gemu_log("Host cmsg overflow\n");
1425 break;
1426 }
1427
1428 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1429 cmsg->cmsg_level = SOL_SOCKET;
1430 } else {
1431 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1432 }
1433 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1434 cmsg->cmsg_len = CMSG_LEN(len);
1435
1436 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
1437 int *fd = (int *)data;
1438 int *target_fd = (int *)target_data;
1439 int i, numfds = len / sizeof(int);
1440
1441 for (i = 0; i < numfds; i++) {
1442 __get_user(fd[i], target_fd + i);
1443 }
1444 } else if (cmsg->cmsg_level == SOL_SOCKET
1445 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1446 struct ucred *cred = (struct ucred *)data;
1447 struct target_ucred *target_cred =
1448 (struct target_ucred *)target_data;
1449
1450 __get_user(cred->pid, &target_cred->pid);
1451 __get_user(cred->uid, &target_cred->uid);
1452 __get_user(cred->gid, &target_cred->gid);
1453 } else {
1454 gemu_log("Unsupported ancillary data: %d/%d\n",
1455 cmsg->cmsg_level, cmsg->cmsg_type);
1456 memcpy(data, target_data, len);
1457 }
1458
1459 cmsg = CMSG_NXTHDR(msgh, cmsg);
1460 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1461 target_cmsg_start);
1462 }
1463 unlock_user(target_cmsg, target_cmsg_addr, 0);
1464 the_end:
1465 msgh->msg_controllen = space;
1466 return 0;
1467 }
1468
1469 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1470 struct msghdr *msgh)
1471 {
1472 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1473 abi_long msg_controllen;
1474 abi_ulong target_cmsg_addr;
1475 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
1476 socklen_t space = 0;
1477
1478 msg_controllen = tswapal(target_msgh->msg_controllen);
1479 if (msg_controllen < sizeof (struct target_cmsghdr))
1480 goto the_end;
1481 target_cmsg_addr = tswapal(target_msgh->msg_control);
1482 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1483 target_cmsg_start = target_cmsg;
1484 if (!target_cmsg)
1485 return -TARGET_EFAULT;
1486
1487 while (cmsg && target_cmsg) {
1488 void *data = CMSG_DATA(cmsg);
1489 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1490
1491 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1492 int tgt_len, tgt_space;
1493
1494 /* We never copy a half-header but may copy half-data;
1495 * this is Linux's behaviour in put_cmsg(). Note that
1496 * truncation here is a guest problem (which we report
1497 * to the guest via the CTRUNC bit), unlike truncation
1498 * in target_to_host_cmsg, which is a QEMU bug.
1499 */
1500 if (msg_controllen < sizeof(struct cmsghdr)) {
1501 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1502 break;
1503 }
1504
1505 if (cmsg->cmsg_level == SOL_SOCKET) {
1506 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1507 } else {
1508 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1509 }
1510 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1511
1512 tgt_len = TARGET_CMSG_LEN(len);
1513
1514 /* Payload types which need a different size of payload on
1515 * the target must adjust tgt_len here.
1516 */
1517 switch (cmsg->cmsg_level) {
1518 case SOL_SOCKET:
1519 switch (cmsg->cmsg_type) {
1520 case SO_TIMESTAMP:
1521 tgt_len = sizeof(struct target_timeval);
1522 break;
1523 default:
1524 break;
1525 }
1526 default:
1527 break;
1528 }
1529
1530 if (msg_controllen < tgt_len) {
1531 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1532 tgt_len = msg_controllen;
1533 }
1534
1535 /* We must now copy-and-convert len bytes of payload
1536 * into tgt_len bytes of destination space. Bear in mind
1537 * that in both source and destination we may be dealing
1538 * with a truncated value!
1539 */
1540 switch (cmsg->cmsg_level) {
1541 case SOL_SOCKET:
1542 switch (cmsg->cmsg_type) {
1543 case SCM_RIGHTS:
1544 {
1545 int *fd = (int *)data;
1546 int *target_fd = (int *)target_data;
1547 int i, numfds = tgt_len / sizeof(int);
1548
1549 for (i = 0; i < numfds; i++) {
1550 __put_user(fd[i], target_fd + i);
1551 }
1552 break;
1553 }
1554 case SO_TIMESTAMP:
1555 {
1556 struct timeval *tv = (struct timeval *)data;
1557 struct target_timeval *target_tv =
1558 (struct target_timeval *)target_data;
1559
1560 if (len != sizeof(struct timeval) ||
1561 tgt_len != sizeof(struct target_timeval)) {
1562 goto unimplemented;
1563 }
1564
1565 /* copy struct timeval to target */
1566 __put_user(tv->tv_sec, &target_tv->tv_sec);
1567 __put_user(tv->tv_usec, &target_tv->tv_usec);
1568 break;
1569 }
1570 case SCM_CREDENTIALS:
1571 {
1572 struct ucred *cred = (struct ucred *)data;
1573 struct target_ucred *target_cred =
1574 (struct target_ucred *)target_data;
1575
1576 __put_user(cred->pid, &target_cred->pid);
1577 __put_user(cred->uid, &target_cred->uid);
1578 __put_user(cred->gid, &target_cred->gid);
1579 break;
1580 }
1581 default:
1582 goto unimplemented;
1583 }
1584 break;
1585
1586 default:
1587 unimplemented:
1588 gemu_log("Unsupported ancillary data: %d/%d\n",
1589 cmsg->cmsg_level, cmsg->cmsg_type);
1590 memcpy(target_data, data, MIN(len, tgt_len));
1591 if (tgt_len > len) {
1592 memset(target_data + len, 0, tgt_len - len);
1593 }
1594 }
1595
1596 target_cmsg->cmsg_len = tswapal(tgt_len);
1597 tgt_space = TARGET_CMSG_SPACE(len);
1598 if (msg_controllen < tgt_space) {
1599 tgt_space = msg_controllen;
1600 }
1601 msg_controllen -= tgt_space;
1602 space += tgt_space;
1603 cmsg = CMSG_NXTHDR(msgh, cmsg);
1604 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1605 target_cmsg_start);
1606 }
1607 unlock_user(target_cmsg, target_cmsg_addr, space);
1608 the_end:
1609 target_msgh->msg_controllen = tswapal(space);
1610 return 0;
1611 }
1612
1613 static void tswap_nlmsghdr(struct nlmsghdr *nlh)
1614 {
1615 nlh->nlmsg_len = tswap32(nlh->nlmsg_len);
1616 nlh->nlmsg_type = tswap16(nlh->nlmsg_type);
1617 nlh->nlmsg_flags = tswap16(nlh->nlmsg_flags);
1618 nlh->nlmsg_seq = tswap32(nlh->nlmsg_seq);
1619 nlh->nlmsg_pid = tswap32(nlh->nlmsg_pid);
1620 }
1621
1622 static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh,
1623 size_t len,
1624 abi_long (*host_to_target_nlmsg)
1625 (struct nlmsghdr *))
1626 {
1627 uint32_t nlmsg_len;
1628 abi_long ret;
1629
1630 while (len > sizeof(struct nlmsghdr)) {
1631
1632 nlmsg_len = nlh->nlmsg_len;
1633 if (nlmsg_len < sizeof(struct nlmsghdr) ||
1634 nlmsg_len > len) {
1635 break;
1636 }
1637
1638 switch (nlh->nlmsg_type) {
1639 case NLMSG_DONE:
1640 tswap_nlmsghdr(nlh);
1641 return 0;
1642 case NLMSG_NOOP:
1643 break;
1644 case NLMSG_ERROR:
1645 {
1646 struct nlmsgerr *e = NLMSG_DATA(nlh);
1647 e->error = tswap32(e->error);
1648 tswap_nlmsghdr(&e->msg);
1649 tswap_nlmsghdr(nlh);
1650 return 0;
1651 }
1652 default:
1653 ret = host_to_target_nlmsg(nlh);
1654 if (ret < 0) {
1655 tswap_nlmsghdr(nlh);
1656 return ret;
1657 }
1658 break;
1659 }
1660 tswap_nlmsghdr(nlh);
1661 len -= NLMSG_ALIGN(nlmsg_len);
1662 nlh = (struct nlmsghdr *)(((char*)nlh) + NLMSG_ALIGN(nlmsg_len));
1663 }
1664 return 0;
1665 }
1666
1667 static abi_long target_to_host_for_each_nlmsg(struct nlmsghdr *nlh,
1668 size_t len,
1669 abi_long (*target_to_host_nlmsg)
1670 (struct nlmsghdr *))
1671 {
1672 int ret;
1673
1674 while (len > sizeof(struct nlmsghdr)) {
1675 if (tswap32(nlh->nlmsg_len) < sizeof(struct nlmsghdr) ||
1676 tswap32(nlh->nlmsg_len) > len) {
1677 break;
1678 }
1679 tswap_nlmsghdr(nlh);
1680 switch (nlh->nlmsg_type) {
1681 case NLMSG_DONE:
1682 return 0;
1683 case NLMSG_NOOP:
1684 break;
1685 case NLMSG_ERROR:
1686 {
1687 struct nlmsgerr *e = NLMSG_DATA(nlh);
1688 e->error = tswap32(e->error);
1689 tswap_nlmsghdr(&e->msg);
1690 }
1691 default:
1692 ret = target_to_host_nlmsg(nlh);
1693 if (ret < 0) {
1694 return ret;
1695 }
1696 }
1697 len -= NLMSG_ALIGN(nlh->nlmsg_len);
1698 nlh = (struct nlmsghdr *)(((char *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
1699 }
1700 return 0;
1701 }
1702
1703 #ifdef CONFIG_RTNETLINK
1704 static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr,
1705 size_t len,
1706 abi_long (*host_to_target_rtattr)
1707 (struct rtattr *))
1708 {
1709 unsigned short rta_len;
1710 abi_long ret;
1711
1712 while (len > sizeof(struct rtattr)) {
1713 rta_len = rtattr->rta_len;
1714 if (rta_len < sizeof(struct rtattr) ||
1715 rta_len > len) {
1716 break;
1717 }
1718 ret = host_to_target_rtattr(rtattr);
1719 rtattr->rta_len = tswap16(rtattr->rta_len);
1720 rtattr->rta_type = tswap16(rtattr->rta_type);
1721 if (ret < 0) {
1722 return ret;
1723 }
1724 len -= RTA_ALIGN(rta_len);
1725 rtattr = (struct rtattr *)(((char *)rtattr) + RTA_ALIGN(rta_len));
1726 }
1727 return 0;
1728 }
1729
1730 static abi_long host_to_target_data_link_rtattr(struct rtattr *rtattr)
1731 {
1732 uint32_t *u32;
1733 struct rtnl_link_stats *st;
1734 struct rtnl_link_stats64 *st64;
1735 struct rtnl_link_ifmap *map;
1736
1737 switch (rtattr->rta_type) {
1738 /* binary stream */
1739 case IFLA_ADDRESS:
1740 case IFLA_BROADCAST:
1741 /* string */
1742 case IFLA_IFNAME:
1743 case IFLA_QDISC:
1744 break;
1745 /* uin8_t */
1746 case IFLA_OPERSTATE:
1747 case IFLA_LINKMODE:
1748 case IFLA_CARRIER:
1749 case IFLA_PROTO_DOWN:
1750 break;
1751 /* uint32_t */
1752 case IFLA_MTU:
1753 case IFLA_LINK:
1754 case IFLA_WEIGHT:
1755 case IFLA_TXQLEN:
1756 case IFLA_CARRIER_CHANGES:
1757 case IFLA_NUM_RX_QUEUES:
1758 case IFLA_NUM_TX_QUEUES:
1759 case IFLA_PROMISCUITY:
1760 case IFLA_EXT_MASK:
1761 case IFLA_LINK_NETNSID:
1762 case IFLA_GROUP:
1763 case IFLA_MASTER:
1764 case IFLA_NUM_VF:
1765 u32 = RTA_DATA(rtattr);
1766 *u32 = tswap32(*u32);
1767 break;
1768 /* struct rtnl_link_stats */
1769 case IFLA_STATS:
1770 st = RTA_DATA(rtattr);
1771 st->rx_packets = tswap32(st->rx_packets);
1772 st->tx_packets = tswap32(st->tx_packets);
1773 st->rx_bytes = tswap32(st->rx_bytes);
1774 st->tx_bytes = tswap32(st->tx_bytes);
1775 st->rx_errors = tswap32(st->rx_errors);
1776 st->tx_errors = tswap32(st->tx_errors);
1777 st->rx_dropped = tswap32(st->rx_dropped);
1778 st->tx_dropped = tswap32(st->tx_dropped);
1779 st->multicast = tswap32(st->multicast);
1780 st->collisions = tswap32(st->collisions);
1781
1782 /* detailed rx_errors: */
1783 st->rx_length_errors = tswap32(st->rx_length_errors);
1784 st->rx_over_errors = tswap32(st->rx_over_errors);
1785 st->rx_crc_errors = tswap32(st->rx_crc_errors);
1786 st->rx_frame_errors = tswap32(st->rx_frame_errors);
1787 st->rx_fifo_errors = tswap32(st->rx_fifo_errors);
1788 st->rx_missed_errors = tswap32(st->rx_missed_errors);
1789
1790 /* detailed tx_errors */
1791 st->tx_aborted_errors = tswap32(st->tx_aborted_errors);
1792 st->tx_carrier_errors = tswap32(st->tx_carrier_errors);
1793 st->tx_fifo_errors = tswap32(st->tx_fifo_errors);
1794 st->tx_heartbeat_errors = tswap32(st->tx_heartbeat_errors);
1795 st->tx_window_errors = tswap32(st->tx_window_errors);
1796
1797 /* for cslip etc */
1798 st->rx_compressed = tswap32(st->rx_compressed);
1799 st->tx_compressed = tswap32(st->tx_compressed);
1800 break;
1801 /* struct rtnl_link_stats64 */
1802 case IFLA_STATS64:
1803 st64 = RTA_DATA(rtattr);
1804 st64->rx_packets = tswap64(st64->rx_packets);
1805 st64->tx_packets = tswap64(st64->tx_packets);
1806 st64->rx_bytes = tswap64(st64->rx_bytes);
1807 st64->tx_bytes = tswap64(st64->tx_bytes);
1808 st64->rx_errors = tswap64(st64->rx_errors);
1809 st64->tx_errors = tswap64(st64->tx_errors);
1810 st64->rx_dropped = tswap64(st64->rx_dropped);
1811 st64->tx_dropped = tswap64(st64->tx_dropped);
1812 st64->multicast = tswap64(st64->multicast);
1813 st64->collisions = tswap64(st64->collisions);
1814
1815 /* detailed rx_errors: */
1816 st64->rx_length_errors = tswap64(st64->rx_length_errors);
1817 st64->rx_over_errors = tswap64(st64->rx_over_errors);
1818 st64->rx_crc_errors = tswap64(st64->rx_crc_errors);
1819 st64->rx_frame_errors = tswap64(st64->rx_frame_errors);
1820 st64->rx_fifo_errors = tswap64(st64->rx_fifo_errors);
1821 st64->rx_missed_errors = tswap64(st64->rx_missed_errors);
1822
1823 /* detailed tx_errors */
1824 st64->tx_aborted_errors = tswap64(st64->tx_aborted_errors);
1825 st64->tx_carrier_errors = tswap64(st64->tx_carrier_errors);
1826 st64->tx_fifo_errors = tswap64(st64->tx_fifo_errors);
1827 st64->tx_heartbeat_errors = tswap64(st64->tx_heartbeat_errors);
1828 st64->tx_window_errors = tswap64(st64->tx_window_errors);
1829
1830 /* for cslip etc */
1831 st64->rx_compressed = tswap64(st64->rx_compressed);
1832 st64->tx_compressed = tswap64(st64->tx_compressed);
1833 break;
1834 /* struct rtnl_link_ifmap */
1835 case IFLA_MAP:
1836 map = RTA_DATA(rtattr);
1837 map->mem_start = tswap64(map->mem_start);
1838 map->mem_end = tswap64(map->mem_end);
1839 map->base_addr = tswap64(map->base_addr);
1840 map->irq = tswap16(map->irq);
1841 break;
1842 /* nested */
1843 case IFLA_AF_SPEC:
1844 case IFLA_LINKINFO:
1845 /* FIXME: implement nested type */
1846 gemu_log("Unimplemented nested type %d\n", rtattr->rta_type);
1847 break;
1848 default:
1849 gemu_log("Unknown host IFLA type: %d\n", rtattr->rta_type);
1850 break;
1851 }
1852 return 0;
1853 }
1854
1855 static abi_long host_to_target_data_addr_rtattr(struct rtattr *rtattr)
1856 {
1857 uint32_t *u32;
1858 struct ifa_cacheinfo *ci;
1859
1860 switch (rtattr->rta_type) {
1861 /* binary: depends on family type */
1862 case IFA_ADDRESS:
1863 case IFA_LOCAL:
1864 break;
1865 /* string */
1866 case IFA_LABEL:
1867 break;
1868 /* u32 */
1869 case IFA_FLAGS:
1870 case IFA_BROADCAST:
1871 u32 = RTA_DATA(rtattr);
1872 *u32 = tswap32(*u32);
1873 break;
1874 /* struct ifa_cacheinfo */
1875 case IFA_CACHEINFO:
1876 ci = RTA_DATA(rtattr);
1877 ci->ifa_prefered = tswap32(ci->ifa_prefered);
1878 ci->ifa_valid = tswap32(ci->ifa_valid);
1879 ci->cstamp = tswap32(ci->cstamp);
1880 ci->tstamp = tswap32(ci->tstamp);
1881 break;
1882 default:
1883 gemu_log("Unknown host IFA type: %d\n", rtattr->rta_type);
1884 break;
1885 }
1886 return 0;
1887 }
1888
1889 static abi_long host_to_target_data_route_rtattr(struct rtattr *rtattr)
1890 {
1891 uint32_t *u32;
1892 switch (rtattr->rta_type) {
1893 /* binary: depends on family type */
1894 case RTA_GATEWAY:
1895 case RTA_DST:
1896 case RTA_PREFSRC:
1897 break;
1898 /* u32 */
1899 case RTA_PRIORITY:
1900 case RTA_TABLE:
1901 case RTA_OIF:
1902 u32 = RTA_DATA(rtattr);
1903 *u32 = tswap32(*u32);
1904 break;
1905 default:
1906 gemu_log("Unknown host RTA type: %d\n", rtattr->rta_type);
1907 break;
1908 }
1909 return 0;
1910 }
1911
1912 static abi_long host_to_target_link_rtattr(struct rtattr *rtattr,
1913 uint32_t rtattr_len)
1914 {
1915 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1916 host_to_target_data_link_rtattr);
1917 }
1918
1919 static abi_long host_to_target_addr_rtattr(struct rtattr *rtattr,
1920 uint32_t rtattr_len)
1921 {
1922 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1923 host_to_target_data_addr_rtattr);
1924 }
1925
1926 static abi_long host_to_target_route_rtattr(struct rtattr *rtattr,
1927 uint32_t rtattr_len)
1928 {
1929 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1930 host_to_target_data_route_rtattr);
1931 }
1932
1933 static abi_long host_to_target_data_route(struct nlmsghdr *nlh)
1934 {
1935 uint32_t nlmsg_len;
1936 struct ifinfomsg *ifi;
1937 struct ifaddrmsg *ifa;
1938 struct rtmsg *rtm;
1939
1940 nlmsg_len = nlh->nlmsg_len;
1941 switch (nlh->nlmsg_type) {
1942 case RTM_NEWLINK:
1943 case RTM_DELLINK:
1944 case RTM_GETLINK:
1945 ifi = NLMSG_DATA(nlh);
1946 ifi->ifi_type = tswap16(ifi->ifi_type);
1947 ifi->ifi_index = tswap32(ifi->ifi_index);
1948 ifi->ifi_flags = tswap32(ifi->ifi_flags);
1949 ifi->ifi_change = tswap32(ifi->ifi_change);
1950 host_to_target_link_rtattr(IFLA_RTA(ifi),
1951 nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
1952 break;
1953 case RTM_NEWADDR:
1954 case RTM_DELADDR:
1955 case RTM_GETADDR:
1956 ifa = NLMSG_DATA(nlh);
1957 ifa->ifa_index = tswap32(ifa->ifa_index);
1958 host_to_target_addr_rtattr(IFA_RTA(ifa),
1959 nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
1960 break;
1961 case RTM_NEWROUTE:
1962 case RTM_DELROUTE:
1963 case RTM_GETROUTE:
1964 rtm = NLMSG_DATA(nlh);
1965 rtm->rtm_flags = tswap32(rtm->rtm_flags);
1966 host_to_target_route_rtattr(RTM_RTA(rtm),
1967 nlmsg_len - NLMSG_LENGTH(sizeof(*rtm)));
1968 break;
1969 default:
1970 return -TARGET_EINVAL;
1971 }
1972 return 0;
1973 }
1974
1975 static inline abi_long host_to_target_nlmsg_route(struct nlmsghdr *nlh,
1976 size_t len)
1977 {
1978 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_route);
1979 }
1980
1981 static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,
1982 size_t len,
1983 abi_long (*target_to_host_rtattr)
1984 (struct rtattr *))
1985 {
1986 abi_long ret;
1987
1988 while (len >= sizeof(struct rtattr)) {
1989 if (tswap16(rtattr->rta_len) < sizeof(struct rtattr) ||
1990 tswap16(rtattr->rta_len) > len) {
1991 break;
1992 }
1993 rtattr->rta_len = tswap16(rtattr->rta_len);
1994 rtattr->rta_type = tswap16(rtattr->rta_type);
1995 ret = target_to_host_rtattr(rtattr);
1996 if (ret < 0) {
1997 return ret;
1998 }
1999 len -= RTA_ALIGN(rtattr->rta_len);
2000 rtattr = (struct rtattr *)(((char *)rtattr) +
2001 RTA_ALIGN(rtattr->rta_len));
2002 }
2003 return 0;
2004 }
2005
2006 static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr)
2007 {
2008 switch (rtattr->rta_type) {
2009 default:
2010 gemu_log("Unknown target IFLA type: %d\n", rtattr->rta_type);
2011 break;
2012 }
2013 return 0;
2014 }
2015
2016 static abi_long target_to_host_data_addr_rtattr(struct rtattr *rtattr)
2017 {
2018 switch (rtattr->rta_type) {
2019 /* binary: depends on family type */
2020 case IFA_LOCAL:
2021 case IFA_ADDRESS:
2022 break;
2023 default:
2024 gemu_log("Unknown target IFA type: %d\n", rtattr->rta_type);
2025 break;
2026 }
2027 return 0;
2028 }
2029
2030 static abi_long target_to_host_data_route_rtattr(struct rtattr *rtattr)
2031 {
2032 uint32_t *u32;
2033 switch (rtattr->rta_type) {
2034 /* binary: depends on family type */
2035 case RTA_DST:
2036 case RTA_SRC:
2037 case RTA_GATEWAY:
2038 break;
2039 /* u32 */
2040 case RTA_OIF:
2041 u32 = RTA_DATA(rtattr);
2042 *u32 = tswap32(*u32);
2043 break;
2044 default:
2045 gemu_log("Unknown target RTA type: %d\n", rtattr->rta_type);
2046 break;
2047 }
2048 return 0;
2049 }
2050
2051 static void target_to_host_link_rtattr(struct rtattr *rtattr,
2052 uint32_t rtattr_len)
2053 {
2054 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2055 target_to_host_data_link_rtattr);
2056 }
2057
2058 static void target_to_host_addr_rtattr(struct rtattr *rtattr,
2059 uint32_t rtattr_len)
2060 {
2061 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2062 target_to_host_data_addr_rtattr);
2063 }
2064
2065 static void target_to_host_route_rtattr(struct rtattr *rtattr,
2066 uint32_t rtattr_len)
2067 {
2068 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2069 target_to_host_data_route_rtattr);
2070 }
2071
2072 static abi_long target_to_host_data_route(struct nlmsghdr *nlh)
2073 {
2074 struct ifinfomsg *ifi;
2075 struct ifaddrmsg *ifa;
2076 struct rtmsg *rtm;
2077
2078 switch (nlh->nlmsg_type) {
2079 case RTM_GETLINK:
2080 break;
2081 case RTM_NEWLINK:
2082 case RTM_DELLINK:
2083 ifi = NLMSG_DATA(nlh);
2084 ifi->ifi_type = tswap16(ifi->ifi_type);
2085 ifi->ifi_index = tswap32(ifi->ifi_index);
2086 ifi->ifi_flags = tswap32(ifi->ifi_flags);
2087 ifi->ifi_change = tswap32(ifi->ifi_change);
2088 target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len -
2089 NLMSG_LENGTH(sizeof(*ifi)));
2090 break;
2091 case RTM_GETADDR:
2092 case RTM_NEWADDR:
2093 case RTM_DELADDR:
2094 ifa = NLMSG_DATA(nlh);
2095 ifa->ifa_index = tswap32(ifa->ifa_index);
2096 target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len -
2097 NLMSG_LENGTH(sizeof(*ifa)));
2098 break;
2099 case RTM_GETROUTE:
2100 break;
2101 case RTM_NEWROUTE:
2102 case RTM_DELROUTE:
2103 rtm = NLMSG_DATA(nlh);
2104 rtm->rtm_flags = tswap32(rtm->rtm_flags);
2105 target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len -
2106 NLMSG_LENGTH(sizeof(*rtm)));
2107 break;
2108 default:
2109 return -TARGET_EOPNOTSUPP;
2110 }
2111 return 0;
2112 }
2113
2114 static abi_long target_to_host_nlmsg_route(struct nlmsghdr *nlh, size_t len)
2115 {
2116 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_route);
2117 }
2118 #endif /* CONFIG_RTNETLINK */
2119
2120 static abi_long host_to_target_data_audit(struct nlmsghdr *nlh)
2121 {
2122 switch (nlh->nlmsg_type) {
2123 default:
2124 gemu_log("Unknown host audit message type %d\n",
2125 nlh->nlmsg_type);
2126 return -TARGET_EINVAL;
2127 }
2128 return 0;
2129 }
2130
2131 static inline abi_long host_to_target_nlmsg_audit(struct nlmsghdr *nlh,
2132 size_t len)
2133 {
2134 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_audit);
2135 }
2136
2137 static abi_long target_to_host_data_audit(struct nlmsghdr *nlh)
2138 {
2139 switch (nlh->nlmsg_type) {
2140 case AUDIT_USER:
2141 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
2142 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
2143 break;
2144 default:
2145 gemu_log("Unknown target audit message type %d\n",
2146 nlh->nlmsg_type);
2147 return -TARGET_EINVAL;
2148 }
2149
2150 return 0;
2151 }
2152
2153 static abi_long target_to_host_nlmsg_audit(struct nlmsghdr *nlh, size_t len)
2154 {
2155 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_audit);
2156 }
2157
2158 /* do_setsockopt() Must return target values and target errnos. */
2159 static abi_long do_setsockopt(int sockfd, int level, int optname,
2160 abi_ulong optval_addr, socklen_t optlen)
2161 {
2162 abi_long ret;
2163 int val;
2164 struct ip_mreqn *ip_mreq;
2165 struct ip_mreq_source *ip_mreq_source;
2166
2167 switch(level) {
2168 case SOL_TCP:
2169 /* TCP options all take an 'int' value. */
2170 if (optlen < sizeof(uint32_t))
2171 return -TARGET_EINVAL;
2172
2173 if (get_user_u32(val, optval_addr))
2174 return -TARGET_EFAULT;
2175 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2176 break;
2177 case SOL_IP:
2178 switch(optname) {
2179 case IP_TOS:
2180 case IP_TTL:
2181 case IP_HDRINCL:
2182 case IP_ROUTER_ALERT:
2183 case IP_RECVOPTS:
2184 case IP_RETOPTS:
2185 case IP_PKTINFO:
2186 case IP_MTU_DISCOVER:
2187 case IP_RECVERR:
2188 case IP_RECVTOS:
2189 #ifdef IP_FREEBIND
2190 case IP_FREEBIND:
2191 #endif
2192 case IP_MULTICAST_TTL:
2193 case IP_MULTICAST_LOOP:
2194 val = 0;
2195 if (optlen >= sizeof(uint32_t)) {
2196 if (get_user_u32(val, optval_addr))
2197 return -TARGET_EFAULT;
2198 } else if (optlen >= 1) {
2199 if (get_user_u8(val, optval_addr))
2200 return -TARGET_EFAULT;
2201 }
2202 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2203 break;
2204 case IP_ADD_MEMBERSHIP:
2205 case IP_DROP_MEMBERSHIP:
2206 if (optlen < sizeof (struct target_ip_mreq) ||
2207 optlen > sizeof (struct target_ip_mreqn))
2208 return -TARGET_EINVAL;
2209
2210 ip_mreq = (struct ip_mreqn *) alloca(optlen);
2211 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
2212 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
2213 break;
2214
2215 case IP_BLOCK_SOURCE:
2216 case IP_UNBLOCK_SOURCE:
2217 case IP_ADD_SOURCE_MEMBERSHIP:
2218 case IP_DROP_SOURCE_MEMBERSHIP:
2219 if (optlen != sizeof (struct target_ip_mreq_source))
2220 return -TARGET_EINVAL;
2221
2222 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2223 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
2224 unlock_user (ip_mreq_source, optval_addr, 0);
2225 break;
2226
2227 default:
2228 goto unimplemented;
2229 }
2230 break;
2231 case SOL_IPV6:
2232 switch (optname) {
2233 case IPV6_MTU_DISCOVER:
2234 case IPV6_MTU:
2235 case IPV6_V6ONLY:
2236 case IPV6_RECVPKTINFO:
2237 val = 0;
2238 if (optlen < sizeof(uint32_t)) {
2239 return -TARGET_EINVAL;
2240 }
2241 if (get_user_u32(val, optval_addr)) {
2242 return -TARGET_EFAULT;
2243 }
2244 ret = get_errno(setsockopt(sockfd, level, optname,
2245 &val, sizeof(val)));
2246 break;
2247 default:
2248 goto unimplemented;
2249 }
2250 break;
2251 case SOL_RAW:
2252 switch (optname) {
2253 case ICMP_FILTER:
2254 /* struct icmp_filter takes an u32 value */
2255 if (optlen < sizeof(uint32_t)) {
2256 return -TARGET_EINVAL;
2257 }
2258
2259 if (get_user_u32(val, optval_addr)) {
2260 return -TARGET_EFAULT;
2261 }
2262 ret = get_errno(setsockopt(sockfd, level, optname,
2263 &val, sizeof(val)));
2264 break;
2265
2266 default:
2267 goto unimplemented;
2268 }
2269 break;
2270 case TARGET_SOL_SOCKET:
2271 switch (optname) {
2272 case TARGET_SO_RCVTIMEO:
2273 {
2274 struct timeval tv;
2275
2276 optname = SO_RCVTIMEO;
2277
2278 set_timeout:
2279 if (optlen != sizeof(struct target_timeval)) {
2280 return -TARGET_EINVAL;
2281 }
2282
2283 if (copy_from_user_timeval(&tv, optval_addr)) {
2284 return -TARGET_EFAULT;
2285 }
2286
2287 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2288 &tv, sizeof(tv)));
2289 return ret;
2290 }
2291 case TARGET_SO_SNDTIMEO:
2292 optname = SO_SNDTIMEO;
2293 goto set_timeout;
2294 case TARGET_SO_ATTACH_FILTER:
2295 {
2296 struct target_sock_fprog *tfprog;
2297 struct target_sock_filter *tfilter;
2298 struct sock_fprog fprog;
2299 struct sock_filter *filter;
2300 int i;
2301
2302 if (optlen != sizeof(*tfprog)) {
2303 return -TARGET_EINVAL;
2304 }
2305 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
2306 return -TARGET_EFAULT;
2307 }
2308 if (!lock_user_struct(VERIFY_READ, tfilter,
2309 tswapal(tfprog->filter), 0)) {
2310 unlock_user_struct(tfprog, optval_addr, 1);
2311 return -TARGET_EFAULT;
2312 }
2313
2314 fprog.len = tswap16(tfprog->len);
2315 filter = g_try_new(struct sock_filter, fprog.len);
2316 if (filter == NULL) {
2317 unlock_user_struct(tfilter, tfprog->filter, 1);
2318 unlock_user_struct(tfprog, optval_addr, 1);
2319 return -TARGET_ENOMEM;
2320 }
2321 for (i = 0; i < fprog.len; i++) {
2322 filter[i].code = tswap16(tfilter[i].code);
2323 filter[i].jt = tfilter[i].jt;
2324 filter[i].jf = tfilter[i].jf;
2325 filter[i].k = tswap32(tfilter[i].k);
2326 }
2327 fprog.filter = filter;
2328
2329 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
2330 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
2331 g_free(filter);
2332
2333 unlock_user_struct(tfilter, tfprog->filter, 1);
2334 unlock_user_struct(tfprog, optval_addr, 1);
2335 return ret;
2336 }
2337 case TARGET_SO_BINDTODEVICE:
2338 {
2339 char *dev_ifname, *addr_ifname;
2340
2341 if (optlen > IFNAMSIZ - 1) {
2342 optlen = IFNAMSIZ - 1;
2343 }
2344 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2345 if (!dev_ifname) {
2346 return -TARGET_EFAULT;
2347 }
2348 optname = SO_BINDTODEVICE;
2349 addr_ifname = alloca(IFNAMSIZ);
2350 memcpy(addr_ifname, dev_ifname, optlen);
2351 addr_ifname[optlen] = 0;
2352 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2353 addr_ifname, optlen));
2354 unlock_user (dev_ifname, optval_addr, 0);
2355 return ret;
2356 }
2357 /* Options with 'int' argument. */
2358 case TARGET_SO_DEBUG:
2359 optname = SO_DEBUG;
2360 break;
2361 case TARGET_SO_REUSEADDR:
2362 optname = SO_REUSEADDR;
2363 break;
2364 case TARGET_SO_TYPE:
2365 optname = SO_TYPE;
2366 break;
2367 case TARGET_SO_ERROR:
2368 optname = SO_ERROR;
2369 break;
2370 case TARGET_SO_DONTROUTE:
2371 optname = SO_DONTROUTE;
2372 break;
2373 case TARGET_SO_BROADCAST:
2374 optname = SO_BROADCAST;
2375 break;
2376 case TARGET_SO_SNDBUF:
2377 optname = SO_SNDBUF;
2378 break;
2379 case TARGET_SO_SNDBUFFORCE:
2380 optname = SO_SNDBUFFORCE;
2381 break;
2382 case TARGET_SO_RCVBUF:
2383 optname = SO_RCVBUF;
2384 break;
2385 case TARGET_SO_RCVBUFFORCE:
2386 optname = SO_RCVBUFFORCE;
2387 break;
2388 case TARGET_SO_KEEPALIVE:
2389 optname = SO_KEEPALIVE;
2390 break;
2391 case TARGET_SO_OOBINLINE:
2392 optname = SO_OOBINLINE;
2393 break;
2394 case TARGET_SO_NO_CHECK:
2395 optname = SO_NO_CHECK;
2396 break;
2397 case TARGET_SO_PRIORITY:
2398 optname = SO_PRIORITY;
2399 break;
2400 #ifdef SO_BSDCOMPAT
2401 case TARGET_SO_BSDCOMPAT:
2402 optname = SO_BSDCOMPAT;
2403 break;
2404 #endif
2405 case TARGET_SO_PASSCRED:
2406 optname = SO_PASSCRED;
2407 break;
2408 case TARGET_SO_PASSSEC:
2409 optname = SO_PASSSEC;
2410 break;
2411 case TARGET_SO_TIMESTAMP:
2412 optname = SO_TIMESTAMP;
2413 break;
2414 case TARGET_SO_RCVLOWAT:
2415 optname = SO_RCVLOWAT;
2416 break;
2417 break;
2418 default:
2419 goto unimplemented;
2420 }
2421 if (optlen < sizeof(uint32_t))
2422 return -TARGET_EINVAL;
2423
2424 if (get_user_u32(val, optval_addr))
2425 return -TARGET_EFAULT;
2426 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
2427 break;
2428 default:
2429 unimplemented:
2430 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
2431 ret = -TARGET_ENOPROTOOPT;
2432 }
2433 return ret;
2434 }
2435
2436 /* do_getsockopt() Must return target values and target errnos. */
2437 static abi_long do_getsockopt(int sockfd, int level, int optname,
2438 abi_ulong optval_addr, abi_ulong optlen)
2439 {
2440 abi_long ret;
2441 int len, val;
2442 socklen_t lv;
2443
2444 switch(level) {
2445 case TARGET_SOL_SOCKET:
2446 level = SOL_SOCKET;
2447 switch (optname) {
2448 /* These don't just return a single integer */
2449 case TARGET_SO_LINGER:
2450 case TARGET_SO_RCVTIMEO:
2451 case TARGET_SO_SNDTIMEO:
2452 case TARGET_SO_PEERNAME:
2453 goto unimplemented;
2454 case TARGET_SO_PEERCRED: {
2455 struct ucred cr;
2456 socklen_t crlen;
2457 struct target_ucred *tcr;
2458
2459 if (get_user_u32(len, optlen)) {
2460 return -TARGET_EFAULT;
2461 }
2462 if (len < 0) {
2463 return -TARGET_EINVAL;
2464 }
2465
2466 crlen = sizeof(cr);
2467 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
2468 &cr, &crlen));
2469 if (ret < 0) {
2470 return ret;
2471 }
2472 if (len > crlen) {
2473 len = crlen;
2474 }
2475 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
2476 return -TARGET_EFAULT;
2477 }
2478 __put_user(cr.pid, &tcr->pid);
2479 __put_user(cr.uid, &tcr->uid);
2480 __put_user(cr.gid, &tcr->gid);
2481 unlock_user_struct(tcr, optval_addr, 1);
2482 if (put_user_u32(len, optlen)) {
2483 return -TARGET_EFAULT;
2484 }
2485 break;
2486 }
2487 /* Options with 'int' argument. */
2488 case TARGET_SO_DEBUG:
2489 optname = SO_DEBUG;
2490 goto int_case;
2491 case TARGET_SO_REUSEADDR:
2492 optname = SO_REUSEADDR;
2493 goto int_case;
2494 case TARGET_SO_TYPE:
2495 optname = SO_TYPE;
2496 goto int_case;
2497 case TARGET_SO_ERROR:
2498 optname = SO_ERROR;
2499 goto int_case;
2500 case TARGET_SO_DONTROUTE:
2501 optname = SO_DONTROUTE;
2502 goto int_case;
2503 case TARGET_SO_BROADCAST:
2504 optname = SO_BROADCAST;
2505 goto int_case;
2506 case TARGET_SO_SNDBUF:
2507 optname = SO_SNDBUF;
2508 goto int_case;
2509 case TARGET_SO_RCVBUF:
2510 optname = SO_RCVBUF;
2511 goto int_case;
2512 case TARGET_SO_KEEPALIVE:
2513 optname = SO_KEEPALIVE;
2514 goto int_case;
2515 case TARGET_SO_OOBINLINE:
2516 optname = SO_OOBINLINE;
2517 goto int_case;
2518 case TARGET_SO_NO_CHECK:
2519 optname = SO_NO_CHECK;
2520 goto int_case;
2521 case TARGET_SO_PRIORITY:
2522 optname = SO_PRIORITY;
2523 goto int_case;
2524 #ifdef SO_BSDCOMPAT
2525 case TARGET_SO_BSDCOMPAT:
2526 optname = SO_BSDCOMPAT;
2527 goto int_case;
2528 #endif
2529 case TARGET_SO_PASSCRED:
2530 optname = SO_PASSCRED;
2531 goto int_case;
2532 case TARGET_SO_TIMESTAMP:
2533 optname = SO_TIMESTAMP;
2534 goto int_case;
2535 case TARGET_SO_RCVLOWAT:
2536 optname = SO_RCVLOWAT;
2537 goto int_case;
2538 case TARGET_SO_ACCEPTCONN:
2539 optname = SO_ACCEPTCONN;
2540 goto int_case;
2541 default:
2542 goto int_case;
2543 }
2544 break;
2545 case SOL_TCP:
2546 /* TCP options all take an 'int' value. */
2547 int_case:
2548 if (get_user_u32(len, optlen))
2549 return -TARGET_EFAULT;
2550 if (len < 0)
2551 return -TARGET_EINVAL;
2552 lv = sizeof(lv);
2553 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2554 if (ret < 0)
2555 return ret;
2556 if (optname == SO_TYPE) {
2557 val = host_to_target_sock_type(val);
2558 }
2559 if (len > lv)
2560 len = lv;
2561 if (len == 4) {
2562 if (put_user_u32(val, optval_addr))
2563 return -TARGET_EFAULT;
2564 } else {
2565 if (put_user_u8(val, optval_addr))
2566 return -TARGET_EFAULT;
2567 }
2568 if (put_user_u32(len, optlen))
2569 return -TARGET_EFAULT;
2570 break;
2571 case SOL_IP:
2572 switch(optname) {
2573 case IP_TOS:
2574 case IP_TTL:
2575 case IP_HDRINCL:
2576 case IP_ROUTER_ALERT:
2577 case IP_RECVOPTS:
2578 case IP_RETOPTS:
2579 case IP_PKTINFO:
2580 case IP_MTU_DISCOVER:
2581 case IP_RECVERR:
2582 case IP_RECVTOS:
2583 #ifdef IP_FREEBIND
2584 case IP_FREEBIND:
2585 #endif
2586 case IP_MULTICAST_TTL:
2587 case IP_MULTICAST_LOOP:
2588 if (get_user_u32(len, optlen))
2589 return -TARGET_EFAULT;
2590 if (len < 0)
2591 return -TARGET_EINVAL;
2592 lv = sizeof(lv);
2593 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2594 if (ret < 0)
2595 return ret;
2596 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2597 len = 1;
2598 if (put_user_u32(len, optlen)
2599 || put_user_u8(val, optval_addr))
2600 return -TARGET_EFAULT;
2601 } else {
2602 if (len > sizeof(int))
2603 len = sizeof(int);
2604 if (put_user_u32(len, optlen)
2605 || put_user_u32(val, optval_addr))
2606 return -TARGET_EFAULT;
2607 }
2608 break;
2609 default:
2610 ret = -TARGET_ENOPROTOOPT;
2611 break;
2612 }
2613 break;
2614 default:
2615 unimplemented:
2616 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
2617 level, optname);
2618 ret = -TARGET_EOPNOTSUPP;
2619 break;
2620 }
2621 return ret;
2622 }
2623
2624 static struct iovec *lock_iovec(int type, abi_ulong target_addr,
2625 int count, int copy)
2626 {
2627 struct target_iovec *target_vec;
2628 struct iovec *vec;
2629 abi_ulong total_len, max_len;
2630 int i;
2631 int err = 0;
2632 bool bad_address = false;
2633
2634 if (count == 0) {
2635 errno = 0;
2636 return NULL;
2637 }
2638 if (count < 0 || count > IOV_MAX) {
2639 errno = EINVAL;
2640 return NULL;
2641 }
2642
2643 vec = g_try_new0(struct iovec, count);
2644 if (vec == NULL) {
2645 errno = ENOMEM;
2646 return NULL;
2647 }
2648
2649 target_vec = lock_user(VERIFY_READ, target_addr,
2650 count * sizeof(struct target_iovec), 1);
2651 if (target_vec == NULL) {
2652 err = EFAULT;
2653 goto fail2;
2654 }
2655
2656 /* ??? If host page size > target page size, this will result in a
2657 value larger than what we can actually support. */
2658 max_len = 0x7fffffff & TARGET_PAGE_MASK;
2659 total_len = 0;
2660
2661 for (i = 0; i < count; i++) {
2662 abi_ulong base = tswapal(target_vec[i].iov_base);
2663 abi_long len = tswapal(target_vec[i].iov_len);
2664
2665 if (len < 0) {
2666 err = EINVAL;
2667 goto fail;
2668 } else if (len == 0) {
2669 /* Zero length pointer is ignored. */
2670 vec[i].iov_base = 0;
2671 } else {
2672 vec[i].iov_base = lock_user(type, base, len, copy);
2673 /* If the first buffer pointer is bad, this is a fault. But
2674 * subsequent bad buffers will result in a partial write; this
2675 * is realized by filling the vector with null pointers and
2676 * zero lengths. */
2677 if (!vec[i].iov_base) {
2678 if (i == 0) {
2679 err = EFAULT;
2680 goto fail;
2681 } else {
2682 bad_address = true;
2683 }
2684 }
2685 if (bad_address) {
2686 len = 0;
2687 }
2688 if (len > max_len - total_len) {
2689 len = max_len - total_len;
2690 }
2691 }
2692 vec[i].iov_len = len;
2693 total_len += len;
2694 }
2695
2696 unlock_user(target_vec, target_addr, 0);
2697 return vec;
2698
2699 fail:
2700 while (--i >= 0) {
2701 if (tswapal(target_vec[i].iov_len) > 0) {
2702 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
2703 }
2704 }
2705 unlock_user(target_vec, target_addr, 0);
2706 fail2:
2707 g_free(vec);
2708 errno = err;
2709 return NULL;
2710 }
2711
2712 static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
2713 int count, int copy)
2714 {
2715 struct target_iovec *target_vec;
2716 int i;
2717
2718 target_vec = lock_user(VERIFY_READ, target_addr,
2719 count * sizeof(struct target_iovec), 1);
2720 if (target_vec) {
2721 for (i = 0; i < count; i++) {
2722 abi_ulong base = tswapal(target_vec[i].iov_base);
2723 abi_long len = tswapal(target_vec[i].iov_len);
2724 if (len < 0) {
2725 break;
2726 }
2727 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
2728 }
2729 unlock_user(target_vec, target_addr, 0);
2730 }
2731
2732 g_free(vec);
2733 }
2734
2735 static inline int target_to_host_sock_type(int *type)
2736 {
2737 int host_type = 0;
2738 int target_type = *type;
2739
2740 switch (target_type & TARGET_SOCK_TYPE_MASK) {
2741 case TARGET_SOCK_DGRAM:
2742 host_type = SOCK_DGRAM;
2743 break;
2744 case TARGET_SOCK_STREAM:
2745 host_type = SOCK_STREAM;
2746 break;
2747 default:
2748 host_type = target_type & TARGET_SOCK_TYPE_MASK;
2749 break;
2750 }
2751 if (target_type & TARGET_SOCK_CLOEXEC) {
2752 #if defined(SOCK_CLOEXEC)
2753 host_type |= SOCK_CLOEXEC;
2754 #else
2755 return -TARGET_EINVAL;
2756 #endif
2757 }
2758 if (target_type & TARGET_SOCK_NONBLOCK) {
2759 #if defined(SOCK_NONBLOCK)
2760 host_type |= SOCK_NONBLOCK;
2761 #elif !defined(O_NONBLOCK)
2762 return -TARGET_EINVAL;
2763 #endif
2764 }
2765 *type = host_type;
2766 return 0;
2767 }
2768
2769 /* Try to emulate socket type flags after socket creation. */
2770 static int sock_flags_fixup(int fd, int target_type)
2771 {
2772 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
2773 if (target_type & TARGET_SOCK_NONBLOCK) {
2774 int flags = fcntl(fd, F_GETFL);
2775 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
2776 close(fd);
2777 return -TARGET_EINVAL;
2778 }
2779 }
2780 #endif
2781 return fd;
2782 }
2783
2784 static abi_long packet_target_to_host_sockaddr(void *host_addr,
2785 abi_ulong target_addr,
2786 socklen_t len)
2787 {
2788 struct sockaddr *addr = host_addr;
2789 struct target_sockaddr *target_saddr;
2790
2791 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
2792 if (!target_saddr) {
2793 return -TARGET_EFAULT;
2794 }
2795
2796 memcpy(addr, target_saddr, len);
2797 addr->sa_family = tswap16(target_saddr->sa_family);
2798 /* spkt_protocol is big-endian */
2799
2800 unlock_user(target_saddr, target_addr, 0);
2801 return 0;
2802 }
2803
2804 static TargetFdTrans target_packet_trans = {
2805 .target_to_host_addr = packet_target_to_host_sockaddr,
2806 };
2807
2808 #ifdef CONFIG_RTNETLINK
2809 static abi_long netlink_route_target_to_host(void *buf, size_t len)
2810 {
2811 return target_to_host_nlmsg_route(buf, len);
2812 }
2813
2814 static abi_long netlink_route_host_to_target(void *buf, size_t len)
2815 {
2816 return host_to_target_nlmsg_route(buf, len);
2817 }
2818
2819 static TargetFdTrans target_netlink_route_trans = {
2820 .target_to_host_data = netlink_route_target_to_host,
2821 .host_to_target_data = netlink_route_host_to_target,
2822 };
2823 #endif /* CONFIG_RTNETLINK */
2824
2825 static abi_long netlink_audit_target_to_host(void *buf, size_t len)
2826 {
2827 return target_to_host_nlmsg_audit(buf, len);
2828 }
2829
2830 static abi_long netlink_audit_host_to_target(void *buf, size_t len)
2831 {
2832 return host_to_target_nlmsg_audit(buf, len);
2833 }
2834
2835 static TargetFdTrans target_netlink_audit_trans = {
2836 .target_to_host_data = netlink_audit_target_to_host,
2837 .host_to_target_data = netlink_audit_host_to_target,
2838 };
2839
2840 /* do_socket() Must return target values and target errnos. */
2841 static abi_long do_socket(int domain, int type, int protocol)
2842 {
2843 int target_type = type;
2844 int ret;
2845
2846 ret = target_to_host_sock_type(&type);
2847 if (ret) {
2848 return ret;
2849 }
2850
2851 if (domain == PF_NETLINK && !(
2852 #ifdef CONFIG_RTNETLINK
2853 protocol == NETLINK_ROUTE ||
2854 #endif
2855 protocol == NETLINK_KOBJECT_UEVENT ||
2856 protocol == NETLINK_AUDIT)) {
2857 return -EPFNOSUPPORT;
2858 }
2859
2860 if (domain == AF_PACKET ||
2861 (domain == AF_INET && type == SOCK_PACKET)) {
2862 protocol = tswap16(protocol);
2863 }
2864
2865 ret = get_errno(socket(domain, type, protocol));
2866 if (ret >= 0) {
2867 ret = sock_flags_fixup(ret, target_type);
2868 if (type == SOCK_PACKET) {
2869 /* Manage an obsolete case :
2870 * if socket type is SOCK_PACKET, bind by name
2871 */
2872 fd_trans_register(ret, &target_packet_trans);
2873 } else if (domain == PF_NETLINK) {
2874 switch (protocol) {
2875 #ifdef CONFIG_RTNETLINK
2876 case NETLINK_ROUTE:
2877 fd_trans_register(ret, &target_netlink_route_trans);
2878 break;
2879 #endif
2880 case NETLINK_KOBJECT_UEVENT:
2881 /* nothing to do: messages are strings */
2882 break;
2883 case NETLINK_AUDIT:
2884 fd_trans_register(ret, &target_netlink_audit_trans);
2885 break;
2886 default:
2887 g_assert_not_reached();
2888 }
2889 }
2890 }
2891 return ret;
2892 }
2893
2894 /* do_bind() Must return target values and target errnos. */
2895 static abi_long do_bind(int sockfd, abi_ulong target_addr,
2896 socklen_t addrlen)
2897 {
2898 void *addr;
2899 abi_long ret;
2900
2901 if ((int)addrlen < 0) {
2902 return -TARGET_EINVAL;
2903 }
2904
2905 addr = alloca(addrlen+1);
2906
2907 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
2908 if (ret)
2909 return ret;
2910
2911 return get_errno(bind(sockfd, addr, addrlen));
2912 }
2913
2914 /* do_connect() Must return target values and target errnos. */
2915 static abi_long do_connect(int sockfd, abi_ulong target_addr,
2916 socklen_t addrlen)
2917 {
2918 void *addr;
2919 abi_long ret;
2920
2921 if ((int)addrlen < 0) {
2922 return -TARGET_EINVAL;
2923 }
2924
2925 addr = alloca(addrlen+1);
2926
2927 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
2928 if (ret)
2929 return ret;
2930
2931 return get_errno(safe_connect(sockfd, addr, addrlen));
2932 }
2933
2934 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
2935 static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
2936 int flags, int send)
2937 {
2938 abi_long ret, len;
2939 struct msghdr msg;
2940 int count;
2941 struct iovec *vec;
2942 abi_ulong target_vec;
2943
2944 if (msgp->msg_name) {
2945 msg.msg_namelen = tswap32(msgp->msg_namelen);
2946 msg.msg_name = alloca(msg.msg_namelen+1);
2947 ret = target_to_host_sockaddr(fd, msg.msg_name,
2948 tswapal(msgp->msg_name),
2949 msg.msg_namelen);
2950 if (ret) {
2951 goto out2;
2952 }
2953 } else {
2954 msg.msg_name = NULL;
2955 msg.msg_namelen = 0;
2956 }
2957 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
2958 msg.msg_control = alloca(msg.msg_controllen);
2959 msg.msg_flags = tswap32(msgp->msg_flags);
2960
2961 count = tswapal(msgp->msg_iovlen);
2962 target_vec = tswapal(msgp->msg_iov);
2963 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
2964 target_vec, count, send);
2965 if (vec == NULL) {
2966 ret = -host_to_target_errno(errno);
2967 goto out2;
2968 }
2969 msg.msg_iovlen = count;
2970 msg.msg_iov = vec;
2971
2972 if (send) {
2973 if (fd_trans_target_to_host_data(fd)) {
2974 ret = fd_trans_target_to_host_data(fd)(msg.msg_iov->iov_base,
2975 msg.msg_iov->iov_len);
2976 } else {
2977 ret = target_to_host_cmsg(&msg, msgp);
2978 }
2979 if (ret == 0) {
2980 ret = get_errno(safe_sendmsg(fd, &msg, flags));
2981 }
2982 } else {
2983 ret = get_errno(safe_recvmsg(fd, &msg, flags));
2984 if (!is_error(ret)) {
2985 len = ret;
2986 if (fd_trans_host_to_target_data(fd)) {
2987 ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
2988 msg.msg_iov->iov_len);
2989 } else {
2990 ret = host_to_target_cmsg(msgp, &msg);
2991 }
2992 if (!is_error(ret)) {
2993 msgp->msg_namelen = tswap32(msg.msg_namelen);
2994 if (msg.msg_name != NULL) {
2995 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
2996 msg.msg_name, msg.msg_namelen);
2997 if (ret) {
2998 goto out;
2999 }
3000 }
3001
3002 ret = len;
3003 }
3004 }
3005 }
3006
3007 out:
3008 unlock_iovec(vec, target_vec, count, !send);
3009 out2:
3010 return ret;
3011 }
3012
3013 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
3014 int flags, int send)
3015 {
3016 abi_long ret;
3017 struct target_msghdr *msgp;
3018
3019 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
3020 msgp,
3021 target_msg,
3022 send ? 1 : 0)) {
3023 return -TARGET_EFAULT;
3024 }
3025 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
3026 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
3027 return ret;
3028 }
3029
3030 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
3031 * so it might not have this *mmsg-specific flag either.
3032 */
3033 #ifndef MSG_WAITFORONE
3034 #define MSG_WAITFORONE 0x10000
3035 #endif
3036
3037 static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
3038 unsigned int vlen, unsigned int flags,
3039 int send)
3040 {
3041 struct target_mmsghdr *mmsgp;
3042 abi_long ret = 0;
3043 int i;
3044
3045 if (vlen > UIO_MAXIOV) {
3046 vlen = UIO_MAXIOV;
3047 }
3048
3049 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
3050 if (!mmsgp) {
3051 return -TARGET_EFAULT;
3052 }
3053
3054 for (i = 0; i < vlen; i++) {
3055 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
3056 if (is_error(ret)) {
3057 break;
3058 }
3059 mmsgp[i].msg_len = tswap32(ret);
3060 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3061 if (flags & MSG_WAITFORONE) {
3062 flags |= MSG_DONTWAIT;
3063 }
3064 }
3065
3066 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
3067
3068 /* Return number of datagrams sent if we sent any at all;
3069 * otherwise return the error.
3070 */
3071 if (i) {
3072 return i;
3073 }
3074 return ret;
3075 }
3076
3077 /* do_accept4() Must return target values and target errnos. */
3078 static abi_long do_accept4(int fd, abi_ulong target_addr,
3079 abi_ulong target_addrlen_addr, int flags)
3080 {
3081 socklen_t addrlen;
3082 void *addr;
3083 abi_long ret;
3084 int host_flags;
3085
3086 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
3087
3088 if (target_addr == 0) {
3089 return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
3090 }
3091
3092 /* linux returns EINVAL if addrlen pointer is invalid */
3093 if (get_user_u32(addrlen, target_addrlen_addr))
3094 return -TARGET_EINVAL;
3095
3096 if ((int)addrlen < 0) {
3097 return -TARGET_EINVAL;
3098 }
3099
3100 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3101 return -TARGET_EINVAL;
3102
3103 addr = alloca(addrlen);
3104
3105 ret = get_errno(safe_accept4(fd, addr, &addrlen, host_flags));
3106 if (!is_error(ret)) {
3107 host_to_target_sockaddr(target_addr, addr, addrlen);
3108 if (put_user_u32(addrlen, target_addrlen_addr))
3109 ret = -TARGET_EFAULT;
3110 }
3111 return ret;
3112 }
3113
3114 /* do_getpeername() Must return target values and target errnos. */
3115 static abi_long do_getpeername(int fd, abi_ulong target_addr,
3116 abi_ulong target_addrlen_addr)
3117 {
3118 socklen_t addrlen;
3119 void *addr;
3120 abi_long ret;
3121
3122 if (get_user_u32(addrlen, target_addrlen_addr))
3123 return -TARGET_EFAULT;
3124
3125 if ((int)addrlen < 0) {
3126 return -TARGET_EINVAL;
3127 }
3128
3129 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3130 return -TARGET_EFAULT;
3131
3132 addr = alloca(addrlen);
3133
3134 ret = get_errno(getpeername(fd, addr, &addrlen));
3135 if (!is_error(ret)) {
3136 host_to_target_sockaddr(target_addr, addr, addrlen);
3137 if (put_user_u32(addrlen, target_addrlen_addr))
3138 ret = -TARGET_EFAULT;
3139 }
3140 return ret;
3141 }
3142
3143 /* do_getsockname() Must return target values and target errnos. */
3144 static abi_long do_getsockname(int fd, abi_ulong target_addr,
3145 abi_ulong target_addrlen_addr)
3146 {
3147 socklen_t addrlen;
3148 void *addr;
3149 abi_long ret;
3150
3151 if (get_user_u32(addrlen, target_addrlen_addr))
3152 return -TARGET_EFAULT;
3153
3154 if ((int)addrlen < 0) {
3155 return -TARGET_EINVAL;
3156 }
3157
3158 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3159 return -TARGET_EFAULT;
3160
3161 addr = alloca(addrlen);
3162
3163 ret = get_errno(getsockname(fd, addr, &addrlen));
3164 if (!is_error(ret)) {
3165 host_to_target_sockaddr(target_addr, addr, addrlen);
3166 if (put_user_u32(addrlen, target_addrlen_addr))
3167 ret = -TARGET_EFAULT;
3168 }
3169 return ret;
3170 }
3171
3172 /* do_socketpair() Must return target values and target errnos. */
3173 static abi_long do_socketpair(int domain, int type, int protocol,
3174 abi_ulong target_tab_addr)
3175 {
3176 int tab[2];
3177 abi_long ret;
3178
3179 target_to_host_sock_type(&type);
3180
3181 ret = get_errno(socketpair(domain, type, protocol, tab));
3182 if (!is_error(ret)) {
3183 if (put_user_s32(tab[0], target_tab_addr)
3184 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
3185 ret = -TARGET_EFAULT;
3186 }
3187 return ret;
3188 }
3189
3190 /* do_sendto() Must return target values and target errnos. */
3191 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
3192 abi_ulong target_addr, socklen_t addrlen)
3193 {
3194 void *addr;
3195 void *host_msg;
3196 abi_long ret;
3197
3198 if ((int)addrlen < 0) {
3199 return -TARGET_EINVAL;
3200 }
3201
3202 host_msg = lock_user(VERIFY_READ, msg, len, 1);
3203 if (!host_msg)
3204 return -TARGET_EFAULT;
3205 if (fd_trans_target_to_host_data(fd)) {
3206 ret = fd_trans_target_to_host_data(fd)(host_msg, len);
3207 if (ret < 0) {
3208 unlock_user(host_msg, msg, 0);
3209 return ret;
3210 }
3211 }
3212 if (target_addr) {
3213 addr = alloca(addrlen+1);
3214 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
3215 if (ret) {
3216 unlock_user(host_msg, msg, 0);
3217 return ret;
3218 }
3219 ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
3220 } else {
3221 ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
3222 }
3223 unlock_user(host_msg, msg, 0);
3224 return ret;
3225 }
3226
3227 /* do_recvfrom() Must return target values and target errnos. */
3228 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
3229 abi_ulong target_addr,
3230 abi_ulong target_addrlen)
3231 {
3232 socklen_t addrlen;
3233 void *addr;
3234 void *host_msg;
3235 abi_long ret;
3236
3237 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
3238 if (!host_msg)
3239 return -TARGET_EFAULT;
3240 if (target_addr) {
3241 if (get_user_u32(addrlen, target_addrlen)) {
3242 ret = -TARGET_EFAULT;
3243 goto fail;
3244 }
3245 if ((int)addrlen < 0) {
3246 ret = -TARGET_EINVAL;
3247 goto fail;
3248 }
3249 addr = alloca(addrlen);
3250 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
3251 addr, &addrlen));
3252 } else {
3253 addr = NULL; /* To keep compiler quiet. */
3254 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
3255 }
3256 if (!is_error(ret)) {
3257 if (target_addr) {
3258 host_to_target_sockaddr(target_addr, addr, addrlen);
3259 if (put_user_u32(addrlen, target_addrlen)) {
3260 ret = -TARGET_EFAULT;
3261 goto fail;
3262 }
3263 }
3264 unlock_user(host_msg, msg, len);
3265 } else {
3266 fail:
3267 unlock_user(host_msg, msg, 0);
3268 }
3269 return ret;
3270 }
3271
3272 #ifdef TARGET_NR_socketcall
3273 /* do_socketcall() Must return target values and target errnos. */
3274 static abi_long do_socketcall(int num, abi_ulong vptr)
3275 {
3276 static const unsigned ac[] = { /* number of arguments per call */
3277 [SOCKOP_socket] = 3, /* domain, type, protocol */
3278 [SOCKOP_bind] = 3, /* sockfd, addr, addrlen */
3279 [SOCKOP_connect] = 3, /* sockfd, addr, addrlen */
3280 [SOCKOP_listen] = 2, /* sockfd, backlog */
3281 [SOCKOP_accept] = 3, /* sockfd, addr, addrlen */
3282 [SOCKOP_accept4] = 4, /* sockfd, addr, addrlen, flags */
3283 [SOCKOP_getsockname] = 3, /* sockfd, addr, addrlen */
3284 [SOCKOP_getpeername] = 3, /* sockfd, addr, addrlen */
3285 [SOCKOP_socketpair] = 4, /* domain, type, protocol, tab */
3286 [SOCKOP_send] = 4, /* sockfd, msg, len, flags */
3287 [SOCKOP_recv] = 4, /* sockfd, msg, len, flags */
3288 [SOCKOP_sendto] = 6, /* sockfd, msg, len, flags, addr, addrlen */
3289 [SOCKOP_recvfrom] = 6, /* sockfd, msg, len, flags, addr, addrlen */
3290 [SOCKOP_shutdown] = 2, /* sockfd, how */
3291 [SOCKOP_sendmsg] = 3, /* sockfd, msg, flags */
3292 [SOCKOP_recvmsg] = 3, /* sockfd, msg, flags */
3293 [SOCKOP_sendmmsg] = 4, /* sockfd, msgvec, vlen, flags */