From: Warner Losh <imp@bsdimp.com>
To: qemu-devel@nongnu.org
Cc: Warner Losh <imp@bsdimp.com>, Kyle Evans <kevans@freebsd.org>,
Richard Henderson <richard.henderson@linaro.org>
Subject: [PULL 18/41] bsd-user: Implement both getsockopt and setsockopt
Date: Sat, 19 Sep 2026 10:47:46 -0600 [thread overview]
Message-ID: <20260919164810.70151-19-imp@bsdimp.com> (raw)
In-Reply-To: <20260919164810.70151-1-imp@bsdimp.com>
Implement get/setsockopt with the generated file describing the socket
options. This uses a table driven conversion. At the moment, we only
implement the simple types. For 64 on 64 with the same endian, we also
try to just copy in/out since the representation should be the same.
Acked-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
bsd-user/freebsd/os-socket.h | 340 +++++++++++++++++++++++++++++++++++
1 file changed, 340 insertions(+)
create mode 100644 bsd-user/freebsd/os-socket.h
diff --git a/bsd-user/freebsd/os-socket.h b/bsd-user/freebsd/os-socket.h
new file mode 100644
index 0000000000..126e49b282
--- /dev/null
+++ b/bsd-user/freebsd/os-socket.h
@@ -0,0 +1,340 @@
+/*
+ * FreeBSD socket related system call shims
+ *
+ * Copyright (c) 2013-2014 Stacey D. Son
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef BSD_USER_FREEBSD_OS_SOCKET_H
+#define BSD_USER_FREEBSD_OS_SOCKET_H
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+
+#include "qemu-os.h"
+#include "os-sockopt.h"
+
+ssize_t safe_recvmsg(int s, struct msghdr *msg, int flags);
+ssize_t safe_sendmsg(int s, const struct msghdr *msg, int flags);
+
+/* do_sendrecvmsg_locked() Must return target values and target errnos. */
+static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
+ int flags, int send)
+{
+ abi_long ret, len;
+ struct msghdr msg;
+ abi_ulong count;
+ struct iovec *vec;
+ abi_ulong target_vec;
+
+ if (msgp->msg_name) {
+ msg.msg_namelen = tswap32(msgp->msg_namelen);
+ msg.msg_name = alloca(msg.msg_namelen + 1);
+ ret = target_to_host_sockaddr(msg.msg_name,
+ tswapal(msgp->msg_name),
+ msg.msg_namelen);
+ if (ret == -TARGET_EFAULT) {
+ /*
+ * For connected sockets msg_name and msg_namelen must be ignored,
+ * so returning EFAULT immediately is wrong. Instead, pass a bad
+ * msg_name to the host kernel, and let it decide whether to return
+ * EFAULT or not.
+ */
+ msg.msg_name = (void *)-1;
+ } else if (ret) {
+ goto out2;
+ }
+ } else {
+ msg.msg_name = NULL;
+ msg.msg_namelen = 0;
+ }
+ msg.msg_controllen = 2 * tswap32(msgp->msg_controllen);
+ if (msgp->msg_control) {
+ msg.msg_control = alloca(msg.msg_controllen);
+ memset(msg.msg_control, 0, msg.msg_controllen);
+ } else {
+ msg.msg_control = NULL;
+ }
+
+ msg.msg_flags = tswap32(msgp->msg_flags);
+
+ count = tswap32(msgp->msg_iovlen);
+ target_vec = tswapal(msgp->msg_iov);
+
+ if (count > IOV_MAX) {
+ /*
+ * sendrcvmsg returns a different errno for this condition than
+ * readv/writev, so we must catch it here before lock_iovec() does.
+ */
+ ret = -TARGET_EMSGSIZE;
+ goto out2;
+ }
+
+ vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
+ target_vec, count, send);
+ if (vec == NULL) {
+ ret = -host_to_target_errno(errno);
+ goto out2;
+ }
+ msg.msg_iovlen = count;
+ msg.msg_iov = vec;
+
+ if (send) {
+ if (msg.msg_control != NULL) {
+ ret = t2h_freebsd_cmsg(&msg, msgp);
+ } else {
+ ret = 0;
+ }
+ if (ret == 0) {
+ ret = get_errno(safe_sendmsg(fd, &msg, flags));
+ }
+ } else {
+ ret = get_errno(safe_recvmsg(fd, &msg, flags));
+ if (!is_error(ret)) {
+ len = ret;
+ if (msg.msg_control != NULL) {
+ ret = h2t_freebsd_cmsg(msgp, &msg);
+ } else {
+ ret = 0;
+ }
+ if (!is_error(ret)) {
+ msgp->msg_namelen = tswap32(msg.msg_namelen);
+ msgp->msg_flags = tswap32(msg.msg_flags);
+ if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
+ ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
+ msg.msg_name, msg.msg_namelen);
+ if (ret) {
+ goto out;
+ }
+ }
+
+ ret = len;
+ }
+ }
+ }
+
+out:
+ unlock_iovec(vec, target_vec, count, !send);
+out2:
+ return ret;
+}
+
+
+static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
+ int flags, int send)
+{
+ abi_long ret;
+ struct target_msghdr *msgp;
+
+ if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
+ msgp,
+ target_msg,
+ send ? 1 : 0)) {
+ return -TARGET_EFAULT;
+ }
+ ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
+ unlock_user_struct(msgp, target_msg, send ? 0 : 1);
+ return ret;
+}
+
+static inline struct sockopt_entry *get_sockopt_entry(int level)
+{
+ for (int i = 0; i < ARRAY_SIZE(so_cache); i++) {
+ if (so_cache[i].level == level) {
+ return so_cache[i].entry;
+ }
+ }
+ return NULL;
+}
+
+/* setsockopt(2) */
+static inline abi_long do_bsd_setsockopt(int sockfd, int level, int optname,
+ abi_ulong optval_addr, socklen_t optlen)
+{
+ abi_long ret;
+ int val;
+ u_char ch;
+ uint64_t val64;
+ struct sockopt_entry *e = get_sockopt_entry(level);
+ void *p;
+
+ if (e == NULL) {
+ gemu_log("Unsupported setsockopt level=%d optname=%d\n",
+ level, optname);
+ return -TARGET_ENOPROTOOPT;
+ }
+
+ while (e->level != SOCK_LEVEL_NONE && e->optname != optname) {
+ e++;
+ }
+
+ if (e->level == SOCK_LEVEL_NONE) {
+ gemu_log("Unsupported setsockopt level=%d optname=%d\n",
+ level, optname);
+ return -TARGET_ENOPROTOOPT;
+ }
+
+ for (int i = 0; i < ARRAY_SIZE(e->type) && e->type[i] != 0; i++) {
+ switch (e->type[i]) {
+ case SOCKOPT_TYPE_BOOL:
+ case SOCKOPT_TYPE_INT:
+ case SOCKOPT_TYPE_U_INT:
+ case SOCKOPT_TYPE_UINT32_T:
+ if (optlen != sizeof(int)) {
+ continue;
+ }
+ if (get_user_u32(val, optval_addr)) {
+ return -TARGET_EFAULT;
+ }
+ return get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
+ case SOCKOPT_TYPE_U_CHAR:
+ if (optlen != sizeof(u_char)) {
+ continue;
+ }
+ if (get_user_u8(ch, optval_addr)) {
+ return -TARGET_EFAULT;
+ }
+ return get_errno(setsockopt(sockfd, level, optname, &ch, sizeof(ch)));
+ case SOCKOPT_TYPE_UINT64_T:
+ if (optlen != sizeof(uint64_t)) {
+ continue;
+ }
+ if (get_user_u64(val64, optval_addr)) {
+ return -TARGET_EFAULT;
+ }
+ return get_errno(setsockopt(sockfd, level, optname, &val64, sizeof(val64)));
+ default:
+#if HOST_LONG_BITS != TARGET_ABI_BITS || HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
+ gemu_log("Unsupported setsockopt level=%d optname=%d\n",
+ level, optname);
+ return -TARGET_ENOPROTOOPT;
+#endif
+ break;
+ }
+ }
+
+ /*
+ * When bit size and endian are the same, the structures are the same (we hope).
+ */
+ p = lock_user(VERIFY_READ, optval_addr, optlen, 1);
+ if (p == NULL) {
+ return -TARGET_EFAULT;
+ }
+ ret = get_errno(setsockopt(sockfd, level, optname, p, optlen));
+ unlock_user(p, optval_addr, 0);
+ return ret;
+}
+
+/* getsockopt(2) */
+static inline abi_long do_bsd_getsockopt(int sockfd, int level, int optname,
+ abi_ulong optval_addr, abi_ulong optlen)
+{
+ abi_long ret;
+ int val;
+ u_char ch;
+ socklen_t len;
+ uint64_t val64;
+ struct sockopt_entry *e = get_sockopt_entry(level);
+ void *p;
+
+ if (e == NULL) {
+ gemu_log("Unsupported getsockopt level=%d optname=%d\n",
+ level, optname);
+ return -TARGET_ENOPROTOOPT;
+ }
+
+ while (e->level != SOCK_LEVEL_NONE && e->optname != optname) {
+ e++;
+ }
+
+ if (e->level == SOCK_LEVEL_NONE) {
+ gemu_log("Unsupported getsockopt level=%d optname=%d\n",
+ level, optname);
+ return -TARGET_ENOPROTOOPT;
+ }
+
+ if (get_user_u32(len, optlen)) {
+ return -TARGET_EFAULT;
+ }
+
+ for (int i = 0; i < ARRAY_SIZE(e->type) && e->type[i] != 0; i++) {
+ switch (e->type[i]) {
+ case SOCKOPT_TYPE_BOOL:
+ case SOCKOPT_TYPE_INT:
+ case SOCKOPT_TYPE_U_INT:
+ case SOCKOPT_TYPE_UINT32_T:
+ if (len != sizeof(int)) {
+ continue;
+ }
+ ret = get_errno(getsockopt(sockfd, level, optname, &val, &len));
+ if (ret < 0) {
+ return ret;
+ }
+ if (len == sizeof(int)) {
+ if (put_user_u32(val, optval_addr)) {
+ return -TARGET_EFAULT;
+ }
+ goto done;
+ }
+ continue;
+ case SOCKOPT_TYPE_U_CHAR:
+ if (len != sizeof(u_char)) {
+ continue;
+ }
+ ret = get_errno(getsockopt(sockfd, level, optname, &ch, &len));
+ if (ret < 0) {
+ return ret;
+ }
+ if (len == sizeof(u_char)) {
+ if (put_user_u8(ch, optval_addr)) {
+ return -TARGET_EFAULT;
+ }
+ goto done;
+ }
+ continue;
+ case SOCKOPT_TYPE_UINT64_T:
+ if (len != sizeof(uint64_t)) {
+ continue;
+ }
+ ret = get_errno(getsockopt(sockfd, level, optname, &val64, &len));
+ if (ret < 0) {
+ return ret;
+ }
+ if (len == sizeof(uint64_t)) {
+ if (put_user_u64(val64, optval_addr)) {
+ return -TARGET_EFAULT;
+ }
+ goto done;
+ }
+ continue;
+ default:
+#if HOST_LONG_BITS != TARGET_ABI_BITS || HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
+ gemu_log("Unsupported getsockopt level=%d optname=%d\n",
+ level, optname);
+ return -TARGET_ENOPROTOOPT;
+#endif
+ break;
+ }
+ }
+
+ /*
+ * When bit size and endian are the same, the structures are the same, and
+ * if not that is handled above.
+ */
+ p = lock_user(VERIFY_WRITE, optval_addr, len, 0);
+ if (p == NULL) {
+ return -TARGET_EFAULT;
+ }
+ ret = get_errno(getsockopt(sockfd, level, optname, p, &len));
+ unlock_user(p, optval_addr, ret < 0 ? 0 : len);
+done:
+ if (put_user_u32(len, optlen)) {
+ return -TARGET_EFAULT;
+ }
+ return ret;
+}
+
+
+#endif /* BSD_USER_FREEBSD_OS_SOCKET_H */
--
2.55.0
next prev parent reply other threads:[~2026-09-19 16:49 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 ` [PULL 14/41] bsd-user: Add FreeBSD socket helpers and sockaddr conversion Warner Losh
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 ` Warner Losh [this message]
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 18/41] bsd-user: Implement both getsockopt and setsockopt 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-19-imp@bsdimp.com \
--to=imp@bsdimp.com \
--cc=kevans@freebsd.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.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.