From: Warner Losh <imp@bsdimp.com>
To: qemu-devel@nongnu.org
Cc: Warner Losh <imp@bsdimp.com>, Kyle Evans <kevans@freebsd.org>,
Stacey Son <sson@FreeBSD.org>,
Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>,
Richard Henderson <richard.henderson@linaro.org>
Subject: [PULL 14/41] bsd-user: Add FreeBSD socket helpers and sockaddr conversion
Date: Sat, 19 Sep 2026 10:47:42 -0600 [thread overview]
Message-ID: <20260919164810.70151-15-imp@bsdimp.com> (raw)
In-Reply-To: <20260919164810.70151-1-imp@bsdimp.com>
Add bsd-socket.c with target_to_host_sockaddr, host_to_target_sockaddr,
and target_to_host_ip_mreq helper functions, and add it to the build.
Signed-off-by: Stacey Son <sson@FreeBSD.org>
Assisted-by: Claude Opus 4.6 (1M context)
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
Acked-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
bsd-user/bsd-socket.c | 98 +++++++++++++++++++++++++++++++++++++++++
bsd-user/meson.build | 1 +
bsd-user/qemu-bsd.h | 8 ++++
bsd-user/syscall_defs.h | 49 +++++++++++++++------
4 files changed, 143 insertions(+), 13 deletions(-)
create mode 100644 bsd-user/bsd-socket.c
diff --git a/bsd-user/bsd-socket.c b/bsd-user/bsd-socket.c
new file mode 100644
index 0000000000..cc45400e4c
--- /dev/null
+++ b/bsd-user/bsd-socket.c
@@ -0,0 +1,98 @@
+/*
+ * BSD socket system call related helpers
+ *
+ * Copyright (c) 2013 Stacey D. Son
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+
+#include "qemu.h"
+#include "qemu-bsd.h"
+
+/*
+ * socket conversion
+ */
+abi_long target_to_host_sockaddr(struct sockaddr *addr, abi_ulong target_addr,
+ socklen_t len)
+{
+ const socklen_t unix_maxlen = sizeof(struct sockaddr_un);
+ sa_family_t sa_family;
+ struct target_sockaddr *target_saddr;
+
+ target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
+ if (target_saddr == 0) {
+ return -TARGET_EFAULT;
+ }
+
+ sa_family = target_saddr->sa_family;
+
+ /*
+ * Oops. The caller might send a incomplete sun_path; sun_path
+ * must be terminated by \0 (see the manual page), but unfortunately
+ * it is quite common to specify sockaddr_un length as
+ * "strlen(x->sun_path)" while it should be "strlen(...) + 1". We will
+ * fix that here if needed.
+ */
+ if (target_saddr->sa_family == AF_UNIX) {
+ if (len < unix_maxlen && len > 0) {
+ char *cp = (char *)target_saddr;
+
+ if (cp[len - 1] && !cp[len]) {
+ len++;
+ }
+ }
+ if (len > unix_maxlen) {
+ len = unix_maxlen;
+ }
+ }
+
+ memcpy(addr, target_saddr, len);
+ addr->sa_family = sa_family; /* type uint8_t */
+ addr->sa_len = target_saddr->sa_len; /* type uint8_t */
+ unlock_user(target_saddr, target_addr, 0);
+
+ return 0;
+}
+
+abi_long host_to_target_sockaddr(abi_ulong target_addr, struct sockaddr *addr,
+ socklen_t len)
+{
+ struct target_sockaddr *target_saddr;
+
+ target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
+ if (target_saddr == 0) {
+ return -TARGET_EFAULT;
+ }
+ memcpy(target_saddr, addr, len);
+ target_saddr->sa_family = addr->sa_family; /* type uint8_t */
+ target_saddr->sa_len = addr->sa_len; /* type uint8_t */
+ unlock_user(target_saddr, target_addr, len);
+
+ return 0;
+}
+
+abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn, abi_ulong target_addr,
+ socklen_t len)
+{
+ struct target_ip_mreqn *target_smreqn;
+
+ target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
+ if (target_smreqn == 0) {
+ return -TARGET_EFAULT;
+ }
+ mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
+ mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
+ if (len == sizeof(struct target_ip_mreqn)) {
+ mreqn->imr_ifindex = tswap32(target_smreqn->imr_ifindex);
+ }
+ unlock_user(target_smreqn, target_addr, 0);
+
+ return 0;
+}
+
diff --git a/bsd-user/meson.build b/bsd-user/meson.build
index 2abcae5122..ab6ddf2e9f 100644
--- a/bsd-user/meson.build
+++ b/bsd-user/meson.build
@@ -11,6 +11,7 @@ bsd_user_ss.add(files(
'bsd-mem.c',
'bsd-misc.c',
'bsd-proc.c',
+ 'bsd-socket.c',
'bsdload.c',
'elfload.c',
'main.c',
diff --git a/bsd-user/qemu-bsd.h b/bsd-user/qemu-bsd.h
index 756425d18e..e32a09b37c 100644
--- a/bsd-user/qemu-bsd.h
+++ b/bsd-user/qemu-bsd.h
@@ -32,6 +32,14 @@ int host_to_target_waitstatus(int status);
void h2g_rusage(const struct rusage *rusage,
struct target_freebsd_rusage *target_rusage);
+/* bsd-socket.c */
+abi_long target_to_host_sockaddr(struct sockaddr *addr, abi_ulong target_addr,
+ socklen_t len);
+abi_long host_to_target_sockaddr(abi_ulong target_addr, struct sockaddr *addr,
+ socklen_t len);
+abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn, abi_ulong target_addr,
+ socklen_t len);
+
/* bsd-mem.c */
void target_to_host_ipc_perm__locked(struct ipc_perm *host_ip,
struct target_ipc_perm *target_ip);
diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index 75122ad521..1a85ceb464 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -203,6 +203,42 @@ struct target_freebsd__wrusage {
struct target_freebsd_rusage wru_children;
};
+/*
+ * sys/socket.h
+ */
+struct target_msghdr {
+ abi_long msg_name; /* Socket name */
+ int32_t msg_namelen; /* Length of name */
+ abi_long msg_iov; /* Data blocks */
+ int32_t msg_iovlen; /* Number of blocks */
+ abi_long msg_control; /* Per protocol magic (eg BSD fd passing) */
+ int32_t msg_controllen; /* Length of cmsg list */
+ int32_t msg_flags; /* flags on received message */
+};
+
+struct target_sockaddr {
+ uint8_t sa_len;
+ uint8_t sa_family;
+ uint8_t sa_data[14];
+} QEMU_PACKED;
+
+struct target_in_addr {
+ uint32_t s_addr; /* big endian */
+};
+/*
+ * netinet/in.h
+ */
+struct target_ip_mreq {
+ struct target_in_addr imr_multiaddr;
+ struct target_in_addr imr_interface;
+};
+
+struct target_ip_mreqn {
+ struct target_in_addr imr_multiaddr;
+ struct target_in_addr imr_address;
+ int32_t imr_ifindex;
+};
+
/*
* sys/stat.h
*/
@@ -531,19 +567,6 @@ type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
return safe_syscall(SYS_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
}
-/*
- * sys/socket.h
- */
-struct target_sockaddr {
- uint8_t sa_len;
- uint8_t sa_family;
- uint8_t sa_data[14];
-} QEMU_PACKED;
-
-struct target_in_addr {
- uint32_t s_addr; /* big endian */
-};
-
#define safe_ioctl(...) safe_syscall(SYS_ioctl, __VA_ARGS__)
#define safe_fcntl(...) safe_syscall(SYS_fcntl, __VA_ARGS__)
--
2.55.0
next prev parent reply other threads:[~2026-09-19 16:50 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-19 16:47 [PULL 00/41] Misc 2026q2 v4 patches Warner Losh
2026-09-19 16:47 ` [PULL 01/41] bsd-user: Upstream most of the remaining system calls Warner Losh
2026-09-19 16:47 ` [PULL 02/41] bsd-user: Rename cpu_env to env throughout bsd-user Warner Losh
2026-09-19 16:47 ` [PULL 03/41] bsd-user: Fix copy direciton in preadv Warner Losh
2026-09-19 16:47 ` [PULL 04/41] bsd-user: Add bsd-signal.h with sigaction through sigreturn Warner Losh
2026-09-19 16:47 ` [PULL 05/41] bsd-user: Add signal shims sigwait through kill and os-signal.h Warner Losh
2026-09-19 16:47 ` [PULL 06/41] bsd-user: Add signal system call dispatch Warner Losh
2026-09-19 16:47 ` [PULL 07/41] bsd-user: move sys/shm.h lines Warner Losh
2026-09-19 16:47 ` [PULL 08/41] bsd-user: Remove mmap defines Warner Losh
2026-09-19 16:47 ` [PULL 09/41] bsd-user: Use target_suseconds_t Warner Losh
2026-09-19 16:47 ` [PULL 10/41] bsd-user: Move sys/uuid.h defines Warner Losh
2026-09-19 16:47 ` [PULL 11/41] bsd-user: Fix realpathat for FreeBSD 15.0 and newer Warner Losh
2026-09-19 16:47 ` [PULL 12/41] bsd-user: Add os-file.h with chflagsat, close_range, and more Warner Losh
2026-09-19 16:47 ` [PULL 13/41] bsd-user: Add poll, poll2, lseek, pipe, and swap system call shims Warner Losh
2026-09-19 16:47 ` Warner Losh [this message]
2026-09-19 16:47 ` [PULL 15/41] bsd-user: Add bsd-socket.h with bind through getsockname Warner Losh
2026-09-19 16:47 ` [PULL 16/41] bsd-user: Add socket shims socketpair through shutdown Warner Losh
2026-09-19 16:47 ` [PULL 17/41] bsd-user: Commit the generated os-sockopt.h files Warner Losh
2026-09-19 16:47 ` [PULL 18/41] bsd-user: Implement both getsockopt and setsockopt Warner Losh
2026-09-19 16:47 ` [PULL 19/41] bsd-user: Add FreeBSD socket system calls Warner Losh
2026-09-19 16:47 ` [PULL 20/41] bsd-user: Add os-socket.[ch] sendmsg, recvmsg and cmsg Warner Losh
2026-09-19 16:47 ` [PULL 21/41] bsd-user: Add socket system call dispatch Warner Losh
2026-09-19 16:47 ` [PULL 22/41] bsd-user: Add os-time.h with clock and time-of-day functions Warner Losh
2026-09-19 16:47 ` [PULL 23/41] bsd-user: Add utimes, futimes, and ktimer functions to os-time.h Warner Losh
2026-09-19 16:47 ` [PULL 24/41] bsd-user: Add select, pselect, and ppoll " Warner Losh
2026-09-19 16:47 ` [PULL 25/41] bsd-user: Add kqueue and kevent functions " Warner Losh
2026-09-19 16:47 ` [PULL 26/41] bsd-user: Add sigtimedwait, itimer, and futimens " Warner Losh
2026-09-19 16:47 ` [PULL 27/41] bsd-user: Add os-time.c and time-related type definitions Warner Losh
2026-09-19 16:47 ` [PULL 28/41] bsd-user: Add time system call dispatch Warner Losh
2026-09-19 16:47 ` [PULL 29/41] bsd-user: Add cpu_copy and make init_task_state non-static Warner Losh
2026-09-19 16:47 ` [PULL 30/41] bsd-user: Add os-thread.h with thr and context functions Warner Losh
2026-09-19 16:47 ` [PULL 31/41] bsd-user: Add do_freebsd__umtx_op to os-thread.h Warner Losh
2026-09-19 16:48 ` [PULL 32/41] bsd-user: Add thread system call dispatch Warner Losh
2026-09-19 16:48 ` [PULL 33/41] bsd-user: Add os-extattr.h with file and fd extattr functions Warner Losh
2026-09-19 16:48 ` [PULL 34/41] bsd-user: Add link and list extattr functions to os-extattr.h Warner Losh
2026-09-19 16:48 ` [PULL 35/41] bsd-user: Add ACL system call shims " Warner Losh
2026-09-19 16:48 ` [PULL 36/41] bsd-user: Add os-extattr.c with ACL conversion functions Warner Losh
2026-09-19 16:48 ` [PULL 37/41] bsd-user: Add extended attribute and ACL system call dispatch Warner Losh
2026-09-19 16:48 ` [PULL 38/41] bsd-user: Add scheduler and cpuset functions to os-misc.h Warner Losh
2026-09-19 16:48 ` [PULL 39/41] bsd-user: Add scheduler, cpuset, kmod, and misc syscall dispatch Warner Losh
2026-09-19 16:48 ` [PULL 40/41] bsd-user: Move qemu_guest_random_seed_main to same as linux-user Warner Losh
2026-09-19 16:48 ` [PULL 41/41] bsd-user Update random_seed on thread creation Warner Losh
-- strict thread matches above, loose matches on Subject: below --
2026-09-18 4:23 [PULL 00/41] Misc 2026q2 v4 patches Warner Losh
2026-09-18 4:23 ` [PULL 14/41] bsd-user: Add FreeBSD socket helpers and sockaddr conversion Warner Losh
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260919164810.70151-15-imp@bsdimp.com \
--to=imp@bsdimp.com \
--cc=kevans@freebsd.org \
--cc=pierrick.bouvier@oss.qualcomm.com \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=sson@FreeBSD.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.