From f64ef1840d62b1bf73e178d456607fd01d6afad4 Mon Sep 17 00:00:00 2001 From: Yann Dirson Date: Fri, 14 Apr 2023 17:43:42 +0200 Subject: [PATCH] WIP Netlink support for FreeBSD 13.2 NOTE: This is an early subset of the Netlink interface. FIXME: Netlink support starts with 13.2, but for now we activate it for all minor release of v13. Signed-off-by: Yann Dirson --- libc-test/build.rs | 1 + .../bsd/freebsdlike/freebsd/freebsd13/mod.rs | 72 +++++++++++++++++++ .../bsd/freebsdlike/freebsd/freebsd14/mod.rs | 72 +++++++++++++++++++ 3 files changed, 145 insertions(+) diff --git a/libc-test/build.rs b/libc-test/build.rs index ac0f996fc4e92..9e531f665a3bd 100644 --- a/libc-test/build.rs +++ b/libc-test/build.rs @@ -1945,6 +1945,7 @@ fn test_freebsd(target: &str) { "netinet/sctp.h", "netinet/tcp.h", "netinet/udp.h", + [freebsd13]:"netlink/netlink.h", "poll.h", "pthread.h", "pthread_np.h", diff --git a/src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs b/src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs index 56564eeb454f0..ce49673632f12 100644 --- a/src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs @@ -277,6 +277,14 @@ s_no_extra_traits! { pub vn_mode: u16, pub vn_devname: [::c_char; ::SPECNAMELEN as usize + 1], } + + pub struct sockaddr_nl { + pub nl_len: ::c_uchar, + pub nl_family: ::sa_family_t, + nl_pad: ::c_ushort, + pub nl_pid: u32, + pub nl_groups: u32 + } } cfg_if! { @@ -447,6 +455,34 @@ cfg_if! { self_vn_devname.hash(state); } } + + impl PartialEq for sockaddr_nl { + fn eq(&self, other: &sockaddr_nl) -> bool { + self.nl_len == other.nl_len && + self.nl_family == other.nl_family && + self.nl_pid == other.nl_pid && + self.nl_groups == other.nl_groups + } + } + impl Eq for sockaddr_nl {} + impl ::fmt::Debug for sockaddr_nl { + fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result { + f.debug_struct("sockaddr_nl") + .field("nl_len", &self.nl_len) + .field("nl_family", &self.nl_family) + .field("nl_pid", &self.nl_pid) + .field("nl_groups", &self.nl_groups) + .finish() + } + } + impl ::hash::Hash for sockaddr_nl { + fn hash(&self, state: &mut H) { + self.nl_len.hash(state); + self.nl_family.hash(state); + self.nl_pid.hash(state); + self.nl_groups.hash(state); + } + } } } @@ -468,6 +504,42 @@ pub const DOMAINSET_POLICY_INTERLEAVE: ::c_int = 4; pub const MINCORE_SUPER: ::c_int = 0x20; +/// netlink constants +pub const SOL_NETLINK: ::c_int = 270; +pub const NETLINK_ADD_MEMBERSHIP: ::c_int = 1; +pub const NETLINK_DROP_MEMBERSHIP: ::c_int = 2; +pub const NETLINK_PKTINFO: ::c_int = 3; +pub const NETLINK_BROADCAST_ERROR: ::c_int = 4; +pub const NETLINK_NO_ENOBUFS: ::c_int = 5; +pub const NETLINK_RX_RING: ::c_int = 6; +pub const NETLINK_TX_RING: ::c_int = 7; +pub const NETLINK_LISTEN_ALL_NSID: ::c_int = 8; +pub const NETLINK_LIST_MEMBERSHIPS: ::c_int = 9; +pub const NETLINK_CAP_ACK: ::c_int = 10; +pub const NETLINK_EXT_ACK: ::c_int = 11; +pub const NETLINK_GET_STRICT_CHK: ::c_int = 12; + +pub const AF_NETLINK: ::c_int = 38; +pub const PF_NETLINK: ::c_int = AF_NETLINK; + +pub const NETLINK_ROUTE: ::c_int = 0; +pub const NETLINK_UNUSED: ::c_int = 1; +pub const NETLINK_USERSOCK: ::c_int = 2; +pub const NETLINK_FIREWALL: ::c_int = 3; +pub const NETLINK_SOCK_DIAG: ::c_int = 4; +pub const NETLINK_NFLOG: ::c_int = 5; +pub const NETLINK_XFRM: ::c_int = 6; +pub const NETLINK_SELINUX: ::c_int = 7; +pub const NETLINK_ISCSI: ::c_int = 8; +pub const NETLINK_AUDIT: ::c_int = 9; +pub const NETLINK_FIB_LOOKUP: ::c_int = 10; +pub const NETLINK_CONNECTOR: ::c_int = 11; +pub const NETLINK_NETFILTER: ::c_int = 12; +pub const NETLINK_IP6_FW: ::c_int = 13; +pub const NETLINK_DNRTMSG: ::c_int = 14; +pub const NETLINK_KOBJECT_UEVENT: ::c_int = 15; +pub const NETLINK_GENERIC: ::c_int = 16; + safe_f! { pub {const} fn makedev(major: ::c_uint, minor: ::c_uint) -> ::dev_t { let major = major as ::dev_t; diff --git a/src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs b/src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs index d60f1a174f1d5..669ceb1523d68 100644 --- a/src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs @@ -277,6 +277,14 @@ s_no_extra_traits! { pub vn_mode: u16, pub vn_devname: [::c_char; ::SPECNAMELEN as usize + 1], } + + pub struct sockaddr_nl { + pub nl_len: ::c_uchar, + pub nl_family: ::sa_family_t, + nl_pad: ::c_ushort, + pub nl_pid: u32, + pub nl_groups: u32 + } } cfg_if! { @@ -447,6 +455,34 @@ cfg_if! { self_vn_devname.hash(state); } } + + impl PartialEq for sockaddr_nl { + fn eq(&self, other: &sockaddr_nl) -> bool { + self.nl_len == other.nl_len && + self.nl_family == other.nl_family && + self.nl_pid == other.nl_pid && + self.nl_groups == other.nl_groups + } + } + impl Eq for sockaddr_nl {} + impl ::fmt::Debug for sockaddr_nl { + fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result { + f.debug_struct("sockaddr_nl") + .field("nl_len", &self.nl_len) + .field("nl_family", &self.nl_family) + .field("nl_pid", &self.nl_pid) + .field("nl_groups", &self.nl_groups) + .finish() + } + } + impl ::hash::Hash for sockaddr_nl { + fn hash(&self, state: &mut H) { + self.nl_len.hash(state); + self.nl_family.hash(state); + self.nl_pid.hash(state); + self.nl_groups.hash(state); + } + } } } @@ -468,6 +504,42 @@ pub const DOMAINSET_POLICY_INTERLEAVE: ::c_int = 4; pub const MINCORE_SUPER: ::c_int = 0x60; +/// netlink constants +pub const SOL_NETLINK: ::c_int = 270; +pub const NETLINK_ADD_MEMBERSHIP: ::c_int = 1; +pub const NETLINK_DROP_MEMBERSHIP: ::c_int = 2; +pub const NETLINK_PKTINFO: ::c_int = 3; +pub const NETLINK_BROADCAST_ERROR: ::c_int = 4; +pub const NETLINK_NO_ENOBUFS: ::c_int = 5; +pub const NETLINK_RX_RING: ::c_int = 6; +pub const NETLINK_TX_RING: ::c_int = 7; +pub const NETLINK_LISTEN_ALL_NSID: ::c_int = 8; +pub const NETLINK_LIST_MEMBERSHIPS: ::c_int = 9; +pub const NETLINK_CAP_ACK: ::c_int = 10; +pub const NETLINK_EXT_ACK: ::c_int = 11; +pub const NETLINK_GET_STRICT_CHK: ::c_int = 12; + +pub const AF_NETLINK: ::c_int = 38; +pub const PF_NETLINK: ::c_int = AF_NETLINK; + +pub const NETLINK_ROUTE: ::c_int = 0; +pub const NETLINK_UNUSED: ::c_int = 1; +pub const NETLINK_USERSOCK: ::c_int = 2; +pub const NETLINK_FIREWALL: ::c_int = 3; +pub const NETLINK_SOCK_DIAG: ::c_int = 4; +pub const NETLINK_NFLOG: ::c_int = 5; +pub const NETLINK_XFRM: ::c_int = 6; +pub const NETLINK_SELINUX: ::c_int = 7; +pub const NETLINK_ISCSI: ::c_int = 8; +pub const NETLINK_AUDIT: ::c_int = 9; +pub const NETLINK_FIB_LOOKUP: ::c_int = 10; +pub const NETLINK_CONNECTOR: ::c_int = 11; +pub const NETLINK_NETFILTER: ::c_int = 12; +pub const NETLINK_IP6_FW: ::c_int = 13; +pub const NETLINK_DNRTMSG: ::c_int = 14; +pub const NETLINK_KOBJECT_UEVENT: ::c_int = 15; +pub const NETLINK_GENERIC: ::c_int = 16; + safe_f! { pub {const} fn makedev(major: ::c_uint, minor: ::c_uint) -> ::dev_t { let major = major as ::dev_t;