Thomas Weißschuh
2026-Jan-20 14:10 UTC
[klibc] [PATCH net-next v2 0/4] net: uapi: Provide an UAPI definition of 'struct sockaddr'
Various UAPI headers reference 'struct sockaddr'. Currently the
definition of this struct is pulled in from the libc header
sys/socket.h. This is problematic as it introduces a dependency
on a full userspace toolchain.
Add a definition of 'struct sockaddr' to the UAPI headers.
Before that, reorder some problematic header inclusions in the selftests.
Signed-off-by: Thomas Wei?schuh <thomas.weissschuh at linutronix.de>
---
Changes in v2:
- Fix compilation failures in BPF samples and selftests
- Link to v1: https://lore.kernel.org/r/20260105-uapi-sockaddr-v1-1-b7653aba12a5
at linutronix.de
---
Thomas Wei?schuh (4):
selftests: net: Move some UAPI header inclusions after libc ones
selftests/landlock: Move some UAPI header inclusions after libc ones
samples/bpf: Move some UAPI header inclusions after libc ones
net: uapi: Provide an UAPI definition of 'struct sockaddr'
include/linux/socket.h | 10 ----------
include/uapi/linux/if.h | 4 ----
include/uapi/linux/libc-compat.h | 12 ++++++++++++
include/uapi/linux/socket.h | 14 ++++++++++++++
samples/bpf/xdp_adjust_tail_user.c | 6 ++++--
samples/bpf/xdp_fwd_user.c | 7 ++++---
samples/bpf/xdp_router_ipv4_user.c | 6 +++---
samples/bpf/xdp_sample_user.c | 15 ++++++++-------
samples/bpf/xdp_tx_iptunnel_user.c | 4 ++--
tools/testing/selftests/landlock/audit.h | 7 ++++---
tools/testing/selftests/net/af_unix/diag_uid.c | 9 +++++----
tools/testing/selftests/net/busy_poller.c | 3 ++-
tools/testing/selftests/net/mptcp/mptcp_diag.c | 11 ++++++-----
tools/testing/selftests/net/nettest.c | 4 ++--
tools/testing/selftests/net/tcp_ao/icmps-discard.c | 6 +++---
tools/testing/selftests/net/tcp_ao/lib/netlink.c | 9 +++++----
tools/testing/selftests/net/tun.c | 5 +++--
17 files changed, 77 insertions(+), 55 deletions(-)
---
base-commit: 24d479d26b25bce5faea3ddd9fa8f3a6c3129ea7
change-id: 20251222-uapi-sockaddr-cf10e7624729
Best regards,
--
Thomas Wei?schuh <thomas.weissschuh at linutronix.de>
Thomas Weißschuh
2026-Jan-20 14:10 UTC
[klibc] [PATCH net-next v2 1/4] selftests: net: Move some UAPI header inclusions after libc ones
Interleaving inclusions of UAPI headers and libc headers is problematic. Both sets of headers define conflicting symbols. To enable their coexistence a compatibility-mechanism is in place. An upcoming change will define 'struct sockaddr' from linux/socket.h. However sys/socket.h from libc does not yet handle this case and a symbol conflict will arise. Furthermore libc-compat.h evaluates the state of the libc inclusions only once, at the point it is included first. If another problematic header from libc is included later, symbol conflicts arise. This will trigger other duplicate definitions when linux/libc-compat.h is added to linux/socket.h Move the inclusion of UAPI headers after the inclusion of the glibc ones, so the libc-compat.h continues to work correctly. Signed-off-by: Thomas Wei?schuh <thomas.weissschuh at linutronix.de> --- tools/testing/selftests/net/af_unix/diag_uid.c | 9 +++++---- tools/testing/selftests/net/busy_poller.c | 3 ++- tools/testing/selftests/net/mptcp/mptcp_diag.c | 11 ++++++----- tools/testing/selftests/net/nettest.c | 4 ++-- tools/testing/selftests/net/tcp_ao/icmps-discard.c | 6 +++--- tools/testing/selftests/net/tcp_ao/lib/netlink.c | 9 +++++---- tools/testing/selftests/net/tun.c | 5 +++-- 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/tools/testing/selftests/net/af_unix/diag_uid.c b/tools/testing/selftests/net/af_unix/diag_uid.c index da7d50cedee6..05456a205325 100644 --- a/tools/testing/selftests/net/af_unix/diag_uid.c +++ b/tools/testing/selftests/net/af_unix/diag_uid.c @@ -5,15 +5,16 @@ #include <sched.h> #include <unistd.h> -#include <linux/netlink.h> -#include <linux/rtnetlink.h> -#include <linux/sock_diag.h> -#include <linux/unix_diag.h> #include <sys/socket.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/un.h> +#include <linux/netlink.h> +#include <linux/rtnetlink.h> +#include <linux/sock_diag.h> +#include <linux/unix_diag.h> + #include "kselftest_harness.h" FIXTURE(diag_uid) diff --git a/tools/testing/selftests/net/busy_poller.c b/tools/testing/selftests/net/busy_poller.c index 3a81f9c94795..34bd8d28808a 100644 --- a/tools/testing/selftests/net/busy_poller.c +++ b/tools/testing/selftests/net/busy_poller.c @@ -9,7 +9,6 @@ #include <stdio.h> #include <string.h> #include <unistd.h> -#include <ynl.h> #include <arpa/inet.h> #include <netinet/in.h> @@ -19,6 +18,8 @@ #include <sys/socket.h> #include <sys/types.h> +#include <ynl.h> + #include <linux/genetlink.h> #include <linux/netlink.h> diff --git a/tools/testing/selftests/net/mptcp/mptcp_diag.c b/tools/testing/selftests/net/mptcp/mptcp_diag.c index 8e0b1b8d84b6..af25ebfd2915 100644 --- a/tools/testing/selftests/net/mptcp/mptcp_diag.c +++ b/tools/testing/selftests/net/mptcp/mptcp_diag.c @@ -1,11 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2025, Kylin Software */ -#include <linux/sock_diag.h> -#include <linux/rtnetlink.h> -#include <linux/inet_diag.h> -#include <linux/netlink.h> -#include <linux/compiler.h> #include <sys/socket.h> #include <netinet/in.h> #include <linux/tcp.h> @@ -17,6 +12,12 @@ #include <errno.h> #include <stdio.h> +#include <linux/sock_diag.h> +#include <linux/rtnetlink.h> +#include <linux/inet_diag.h> +#include <linux/netlink.h> +#include <linux/compiler.h> + #ifndef IPPROTO_MPTCP #define IPPROTO_MPTCP 262 #endif diff --git a/tools/testing/selftests/net/nettest.c b/tools/testing/selftests/net/nettest.c index 1f5227f3d64d..71430403b50b 100644 --- a/tools/testing/selftests/net/nettest.c +++ b/tools/testing/selftests/net/nettest.c @@ -10,8 +10,6 @@ #include <sys/ioctl.h> #include <sys/socket.h> #include <sys/wait.h> -#include <linux/tcp.h> -#include <linux/udp.h> #include <arpa/inet.h> #include <net/if.h> #include <netinet/in.h> @@ -30,6 +28,8 @@ #include <errno.h> #include <getopt.h> +#include <linux/tcp.h> +#include <linux/udp.h> #include <linux/xfrm.h> #include <linux/ipsec.h> #include <linux/pfkeyv2.h> diff --git a/tools/testing/selftests/net/tcp_ao/icmps-discard.c b/tools/testing/selftests/net/tcp_ao/icmps-discard.c index 85c1a1e958c6..451ba89914c9 100644 --- a/tools/testing/selftests/net/tcp_ao/icmps-discard.c +++ b/tools/testing/selftests/net/tcp_ao/icmps-discard.c @@ -16,12 +16,12 @@ * Author: Dmitry Safonov <dima at arista.com> */ #include <inttypes.h> -#include <linux/icmp.h> -#include <linux/icmpv6.h> -#include <linux/ipv6.h> #include <netinet/in.h> #include <netinet/ip.h> #include <sys/socket.h> +#include <linux/icmp.h> +#include <linux/icmpv6.h> +#include <linux/ipv6.h> #include "aolib.h" #include "../../../../include/linux/compiler.h" diff --git a/tools/testing/selftests/net/tcp_ao/lib/netlink.c b/tools/testing/selftests/net/tcp_ao/lib/netlink.c index 7f108493a29a..69a05820782a 100644 --- a/tools/testing/selftests/net/tcp_ao/lib/netlink.c +++ b/tools/testing/selftests/net/tcp_ao/lib/netlink.c @@ -1,14 +1,15 @@ // SPDX-License-Identifier: GPL-2.0 /* Original from tools/testing/selftests/net/ipsec.c */ -#include <linux/netlink.h> -#include <linux/random.h> -#include <linux/rtnetlink.h> -#include <linux/veth.h> #include <net/if.h> #include <stdint.h> #include <string.h> #include <sys/socket.h> +#include <linux/netlink.h> +#include <linux/random.h> +#include <linux/rtnetlink.h> +#include <linux/veth.h> + #include "aolib.h" #define MAX_PAYLOAD 2048 diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c index 0efc67b0357a..e6e4c52d538e 100644 --- a/tools/testing/selftests/net/tun.c +++ b/tools/testing/selftests/net/tun.c @@ -8,12 +8,13 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <sys/ioctl.h> +#include <sys/socket.h> + #include <linux/if.h> #include <linux/if_tun.h> #include <linux/netlink.h> #include <linux/rtnetlink.h> -#include <sys/ioctl.h> -#include <sys/socket.h> #include "kselftest_harness.h" -- 2.52.0
Thomas Weißschuh
2026-Jan-20 14:10 UTC
[klibc] [PATCH net-next v2 2/4] selftests/landlock: Move some UAPI header inclusions after libc ones
Interleaving inclusions of UAPI headers and libc headers is problematic. Both sets of headers define conflicting symbols. To enable their coexistence a compatibility-mechanism is in place. An upcoming change will define 'struct sockaddr' from linux/socket.h. However sys/socket.h from libc does not yet handle this case and a symbol conflict will arise. Move the inclusion of all UAPI headers after the inclusion of the glibc ones, so the compatibility mechanism from the UAPI headers is used. Signed-off-by: Thomas Wei?schuh <thomas.weissschuh at linutronix.de> --- tools/testing/selftests/landlock/audit.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/landlock/audit.h b/tools/testing/selftests/landlock/audit.h index 44eb433e9666..c12b16c690dc 100644 --- a/tools/testing/selftests/landlock/audit.h +++ b/tools/testing/selftests/landlock/audit.h @@ -7,9 +7,6 @@ #define _GNU_SOURCE #include <errno.h> -#include <linux/audit.h> -#include <linux/limits.h> -#include <linux/netlink.h> #include <regex.h> #include <stdbool.h> #include <stdint.h> @@ -20,6 +17,10 @@ #include <sys/time.h> #include <unistd.h> +#include <linux/audit.h> +#include <linux/limits.h> +#include <linux/netlink.h> + #include "kselftest.h" #ifndef ARRAY_SIZE -- 2.52.0
Thomas Weißschuh
2026-Jan-20 14:10 UTC
[klibc] [PATCH net-next v2 3/4] samples/bpf: Move some UAPI header inclusions after libc ones
Interleaving inclusions of UAPI headers and libc headers is problematic. Both sets of headers define conflicting symbols. To enable their coexistence a compatibility-mechanism is in place. An upcoming change will define 'struct sockaddr' from linux/socket.h. However sys/socket.h from libc does not yet handle this case and a symbol conflict will arise. Move the inclusion of all UAPI headers after the inclusion of the glibc ones, so the compatibility mechanism from the UAPI headers is used. Signed-off-by: Thomas Wei?schuh <thomas.weissschuh at linutronix.de> --- samples/bpf/xdp_adjust_tail_user.c | 6 ++++-- samples/bpf/xdp_fwd_user.c | 7 ++++--- samples/bpf/xdp_router_ipv4_user.c | 6 +++--- samples/bpf/xdp_sample_user.c | 15 ++++++++------- samples/bpf/xdp_tx_iptunnel_user.c | 4 ++-- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/samples/bpf/xdp_adjust_tail_user.c b/samples/bpf/xdp_adjust_tail_user.c index e9426bd65420..32d00405debc 100644 --- a/samples/bpf/xdp_adjust_tail_user.c +++ b/samples/bpf/xdp_adjust_tail_user.c @@ -5,8 +5,6 @@ * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. */ -#include <linux/bpf.h> -#include <linux/if_link.h> #include <assert.h> #include <errno.h> #include <signal.h> @@ -18,9 +16,13 @@ #include <netinet/ether.h> #include <unistd.h> #include <time.h> + #include <bpf/bpf.h> #include <bpf/libbpf.h> +#include <linux/bpf.h> +#include <linux/if_link.h> + #define STATS_INTERVAL_S 2U #define MAX_PCKT_SIZE 600 diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c index 193b3b79b31f..ca55f3eac12a 100644 --- a/samples/bpf/xdp_fwd_user.c +++ b/samples/bpf/xdp_fwd_user.c @@ -11,9 +11,6 @@ * General Public License for more details. */ -#include <linux/bpf.h> -#include <linux/if_link.h> -#include <linux/limits.h> #include <net/if.h> #include <errno.h> #include <stdio.h> @@ -27,6 +24,10 @@ #include <bpf/libbpf.h> #include <bpf/bpf.h> +#include <linux/bpf.h> +#include <linux/if_link.h> +#include <linux/limits.h> + static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; static int do_attach(int idx, int prog_fd, int map_fd, const char *name) diff --git a/samples/bpf/xdp_router_ipv4_user.c b/samples/bpf/xdp_router_ipv4_user.c index 266fdd0b025d..2abc7d294251 100644 --- a/samples/bpf/xdp_router_ipv4_user.c +++ b/samples/bpf/xdp_router_ipv4_user.c @@ -1,9 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-only /* Copyright (C) 2017 Cavium, Inc. */ -#include <linux/bpf.h> -#include <linux/netlink.h> -#include <linux/rtnetlink.h> #include <assert.h> #include <errno.h> #include <signal.h> @@ -25,6 +22,9 @@ #include <libgen.h> #include <getopt.h> #include <pthread.h> +#include <linux/bpf.h> +#include <linux/netlink.h> +#include <linux/rtnetlink.h> #include "xdp_sample_user.h" #include "xdp_router_ipv4.skel.h" diff --git a/samples/bpf/xdp_sample_user.c b/samples/bpf/xdp_sample_user.c index 158682852162..d9aec2bd372c 100644 --- a/samples/bpf/xdp_sample_user.c +++ b/samples/bpf/xdp_sample_user.c @@ -7,13 +7,6 @@ #include <errno.h> #include <fcntl.h> #include <getopt.h> -#include <linux/ethtool.h> -#include <linux/hashtable.h> -#include <linux/if_link.h> -#include <linux/jhash.h> -#include <linux/limits.h> -#include <linux/list.h> -#include <linux/sockios.h> #include <locale.h> #include <math.h> #include <net/if.h> @@ -32,6 +25,14 @@ #include <time.h> #include <unistd.h> +#include <linux/ethtool.h> +#include <linux/hashtable.h> +#include <linux/if_link.h> +#include <linux/jhash.h> +#include <linux/limits.h> +#include <linux/list.h> +#include <linux/sockios.h> + #include "bpf_util.h" #include "xdp_sample_user.h" diff --git a/samples/bpf/xdp_tx_iptunnel_user.c b/samples/bpf/xdp_tx_iptunnel_user.c index 7e4b2f7108a6..e9503036d0a0 100644 --- a/samples/bpf/xdp_tx_iptunnel_user.c +++ b/samples/bpf/xdp_tx_iptunnel_user.c @@ -1,8 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-only /* Copyright (c) 2016 Facebook */ -#include <linux/bpf.h> -#include <linux/if_link.h> #include <assert.h> #include <errno.h> #include <signal.h> @@ -16,6 +14,8 @@ #include <time.h> #include <bpf/libbpf.h> #include <bpf/bpf.h> +#include <linux/bpf.h> +#include <linux/if_link.h> #include "bpf_util.h" #include "xdp_tx_iptunnel_common.h" -- 2.52.0
Thomas Weißschuh
2026-Jan-20 14:10 UTC
[klibc] [PATCH net-next v2 4/4] net: uapi: Provide an UAPI definition of 'struct sockaddr'
Various UAPI headers reference 'struct sockaddr'. Currently the
definition of this struct is pulled in from the libc header
sys/socket.h. This is problematic as it introduces a dependency
on a full userspace toolchain.
Instead expose a custom but compatible definition of 'struct sockaddr'
in the UAPI headers. It is guarded by the libc compatibility
infrastructure to avoid potential conflicts.
The compatibility symbol won't be supported by glibc right away,
but right now __UAPI_DEF_IF_IFNAMSIZ is not supported either,
so including the libc headers before the UAPI headers is broken anyways.
Signed-off-by: Thomas Wei?schuh <thomas.weissschuh at linutronix.de>
---
include/linux/socket.h | 10 ----------
include/uapi/linux/if.h | 4 ----
include/uapi/linux/libc-compat.h | 12 ++++++++++++
include/uapi/linux/socket.h | 14 ++++++++++++++
4 files changed, 26 insertions(+), 14 deletions(-)
diff --git a/include/linux/socket.h b/include/linux/socket.h
index ec715ad4bf25..8363d4e0a044 100644
--- a/include/linux/socket.h
+++ b/include/linux/socket.h
@@ -28,16 +28,6 @@ extern void socket_seq_show(struct seq_file *seq);
typedef __kernel_sa_family_t sa_family_t;
-/*
- * 1003.1g requires sa_family_t and that sa_data is char.
- */
-
-/* Deprecated for in-kernel use. Use struct sockaddr_unsized instead. */
-struct sockaddr {
- sa_family_t sa_family; /* address family, AF_xxx */
- char sa_data[14]; /* 14 bytes of protocol address */
-};
-
/**
* struct sockaddr_unsized - Unspecified size sockaddr for callbacks
* @sa_family: Address family (AF_UNIX, AF_INET, AF_INET6, etc.)
diff --git a/include/uapi/linux/if.h b/include/uapi/linux/if.h
index 797ba2c1562a..a4bc54196a07 100644
--- a/include/uapi/linux/if.h
+++ b/include/uapi/linux/if.h
@@ -25,10 +25,6 @@
#include <linux/socket.h> /* for "struct sockaddr" et al */
#include <linux/compiler.h> /* for "__user" et al */
-#ifndef __KERNEL__
-#include <sys/socket.h> /* for struct sockaddr. */
-#endif
-
#if __UAPI_DEF_IF_IFNAMSIZ
#define IFNAMSIZ 16
#endif /* __UAPI_DEF_IF_IFNAMSIZ */
diff --git a/include/uapi/linux/libc-compat.h b/include/uapi/linux/libc-compat.h
index 0eca95ccb41e..13a06ce4e825 100644
--- a/include/uapi/linux/libc-compat.h
+++ b/include/uapi/linux/libc-compat.h
@@ -140,6 +140,13 @@
#endif /* _NETINET_IN_H */
+/* Definitions for socket.h */
+#if defined(_SYS_SOCKET_H)
+#define __UAPI_DEF_SOCKADDR 0
+#else
+#define __UAPI_DEF_SOCKADDR 1
+#endif
+
/* Definitions for xattr.h */
#if defined(_SYS_XATTR_H)
#define __UAPI_DEF_XATTR 0
@@ -221,6 +228,11 @@
#define __UAPI_DEF_IP6_MTUINFO 1
#endif
+/* Definitions for socket.h */
+#ifndef __UAPI_DEF_SOCKADDR
+#define __UAPI_DEF_SOCKADDR 1
+#endif
+
/* Definitions for xattr.h */
#ifndef __UAPI_DEF_XATTR
#define __UAPI_DEF_XATTR 1
diff --git a/include/uapi/linux/socket.h b/include/uapi/linux/socket.h
index d3fcd3b5ec53..35d7d5f4b1a8 100644
--- a/include/uapi/linux/socket.h
+++ b/include/uapi/linux/socket.h
@@ -2,6 +2,8 @@
#ifndef _UAPI_LINUX_SOCKET_H
#define _UAPI_LINUX_SOCKET_H
+#include <linux/libc-compat.h> /* for compatibility with glibc
*/
+
/*
* Desired design of maximum size and alignment (see RFC2553)
*/
@@ -26,6 +28,18 @@ struct __kernel_sockaddr_storage {
};
};
+/*
+ * 1003.1g requires sa_family_t and that sa_data is char.
+ */
+
+/* Deprecated for in-kernel use. Use struct sockaddr_unsized instead. */
+#if __UAPI_DEF_SOCKADDR
+struct sockaddr {
+ __kernel_sa_family_t sa_family; /* address family, AF_xxx */
+ char sa_data[14]; /* 14 bytes of protocol address */
+};
+#endif /* __UAPI_DEF_SOCKADDR */
+
#define SOCK_SNDBUF_LOCK 1
#define SOCK_RCVBUF_LOCK 2
--
2.52.0
Jakub Kicinski
2026-Jan-22 03:27 UTC
[klibc] [PATCH net-next v2 0/4] net: uapi: Provide an UAPI definition of 'struct sockaddr'
On Tue, 20 Jan 2026 15:10:30 +0100 Thomas Wei?schuh wrote:> Various UAPI headers reference 'struct sockaddr'. Currently the > definition of this struct is pulled in from the libc header > sys/socket.h. This is problematic as it introduces a dependency > on a full userspace toolchain. > > Add a definition of 'struct sockaddr' to the UAPI headers. > Before that, reorder some problematic header inclusions in the selftests.> include/linux/socket.h | 10 ---------- > include/uapi/linux/if.h | 4 ---- > include/uapi/linux/libc-compat.h | 12 ++++++++++++ > include/uapi/linux/socket.h | 14 ++++++++++++++ > samples/bpf/xdp_adjust_tail_user.c | 6 ++++-- > samples/bpf/xdp_fwd_user.c | 7 ++++--- > samples/bpf/xdp_router_ipv4_user.c | 6 +++--- > samples/bpf/xdp_sample_user.c | 15 ++++++++------- > samples/bpf/xdp_tx_iptunnel_user.c | 4 ++-- > tools/testing/selftests/landlock/audit.h | 7 ++++--- > tools/testing/selftests/net/af_unix/diag_uid.c | 9 +++++---- > tools/testing/selftests/net/busy_poller.c | 3 ++- > tools/testing/selftests/net/mptcp/mptcp_diag.c | 11 ++++++----- > tools/testing/selftests/net/nettest.c | 4 ++-- > tools/testing/selftests/net/tcp_ao/icmps-discard.c | 6 +++--- > tools/testing/selftests/net/tcp_ao/lib/netlink.c | 9 +++++---- > tools/testing/selftests/net/tun.c | 5 +++-- > 17 files changed, 77 insertions(+), 55 deletions(-)Are all those selftests / samples getting broken by this patch set? I understand that we should avoid libc dependencies in uAPI but at least speaking for networking - building selftests without libc is.. not a practical proposition?