From f210e1184d84288503f9164e8d69817f50fc85a7 Mon Sep 17 00:00:00 2001 From: Yann Dirson Date: Mon, 22 Jan 2024 18:34:01 +0100 Subject: [PATCH] freebsd/netlink: hide feature between "future-freebsd-netlink" flag To allow the "Netlink on FreeBSD" feature to be usable before 0.3 gets released, this builds on top of both the netlink feature patch from #3201 if explicitely requested through a feature flag, but by default provides the ifmib constants where they are located in previous 0.2 releases. Since this relies on creating the netlink constants in a separate module, and it seems we cannot check those automatically, avoids testing them. Signed-off-by: Yann Dirson --- Cargo.toml | 3 + libc-test/build.rs | 4 +- src/unix/bsd/freebsdlike/freebsd/mod.rs | 107 ++------------------ src/unix/bsd/freebsdlike/freebsd/netlink.rs | 95 +++++++++++++++++ 4 files changed, 110 insertions(+), 99 deletions(-) create mode 100644 src/unix/bsd/freebsdlike/freebsd/netlink.rs diff --git a/Cargo.toml b/Cargo.toml index 062f76472fa9c..956e464f68a87 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -141,6 +141,9 @@ extra_traits = [] const-extern-fn = [] # use_std is deprecated, use `std` instead use_std = ['std'] +# get Netlink support on FreeBSD, but with breaking API change, +# see https://github.com/rust-lang/libc/pull/3367 +future-freebsd-netlink = [] [workspace] members = ["libc-test"] diff --git a/libc-test/build.rs b/libc-test/build.rs index d2519bb3a19d9..3bed74be77b3f 100644 --- a/libc-test/build.rs +++ b/libc-test/build.rs @@ -2428,7 +2428,9 @@ fn test_freebsd(target: &str) { | "CTRL_ATTR_MCAST_GRP_UNSPEC" | "CTRL_ATTR_MCAST_GRP_NAME" | "CTRL_ATTR_MCAST_GRP_ID" - if Some(13) > freebsd_ver => + if env!("CARGO_PKG_VERSION_MAJOR") == "0" + && env!("CARGO_PKG_VERSION_MINOR") == "2" + || Some(13) > freebsd_ver => { true } diff --git a/src/unix/bsd/freebsdlike/freebsd/mod.rs b/src/unix/bsd/freebsdlike/freebsd/mod.rs index f08fa928bfcc9..8a34354366dce 100644 --- a/src/unix/bsd/freebsdlike/freebsd/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/mod.rs @@ -3097,104 +3097,6 @@ pub const SO_TS_MONOTONIC: ::c_int = 3; pub const SO_TS_DEFAULT: ::c_int = SO_TS_REALTIME_MICRO; pub const SO_TS_CLOCK_MAX: ::c_int = SO_TS_MONOTONIC; -/// netlink constants - -// sys/socket.h -pub const AF_NETLINK: ::c_int = 38; -pub const PF_NETLINK: ::c_int = AF_NETLINK; - -// netlink/netlink.h -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 NLM_F_REQUEST: ::c_int = 0x01; -pub const NLM_F_MULTI: ::c_int = 0x02; -pub const NLM_F_ACK: ::c_int = 0x04; -pub const NLM_F_ECHO: ::c_int = 0x08; -pub const NLM_F_DUMP_INTR: ::c_int = 0x10; -pub const NLM_F_DUMP_FILTERED: ::c_int = 0x20; -// -pub const NLM_F_ROOT: ::c_int = 0x100; -pub const NLM_F_MATCH: ::c_int = 0x200; -pub const NLM_F_ATOMIC: ::c_int = 0x400; -pub const NLM_F_DUMP: ::c_int = NLM_F_ROOT | NLM_F_MATCH; -// -pub const NLM_F_REPLACE: ::c_int = 0x100; -pub const NLM_F_EXCL: ::c_int = 0x200; -pub const NLM_F_CREATE: ::c_int = 0x400; -pub const NLM_F_APPEND: ::c_int = 0x800; -// -pub const NLM_F_NONREC: ::c_int = 0x100; -// -pub const NLM_F_CAPPED: ::c_int = 0x100; -pub const NLM_F_ACK_TLVS: ::c_int = 0x200; -// -pub const NLMSG_NOOP: ::c_int = 0x1; -pub const NLMSG_ERROR: ::c_int = 0x2; -pub const NLMSG_DONE: ::c_int = 0x3; -pub const NLMSG_OVERRUN: ::c_int = 0x4; -// -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; -// -const NL_ITEM_ALIGN_SIZE: ::c_int = 4; // mem::size_of::(); FIXME accept new dep? -pub const NLMSG_ALIGNTO: ::c_int = NL_ITEM_ALIGN_SIZE; - -// netlink/netlink_generic.h -pub const CTRL_CMD_UNSPEC: ::c_int = 0; -pub const CTRL_CMD_NEWFAMILY: ::c_int = 1; -pub const CTRL_CMD_DELFAMILY: ::c_int = 2; -pub const CTRL_CMD_GETFAMILY: ::c_int = 3; -pub const CTRL_CMD_NEWOPS: ::c_int = 4; -pub const CTRL_CMD_DELOPS: ::c_int = 5; -pub const CTRL_CMD_GETOPS: ::c_int = 6; -pub const CTRL_CMD_NEWMCAST_GRP: ::c_int = 7; -pub const CTRL_CMD_DELMCAST_GRP: ::c_int = 8; -pub const CTRL_CMD_GETMCAST_GRP: ::c_int = 9; -pub const CTRL_CMD_GETPOLICY: ::c_int = 10; -// -pub const CTRL_ATTR_UNSPEC: ::c_int = 0; -pub const CTRL_ATTR_FAMILY_ID: ::c_int = 1; -pub const CTRL_ATTR_FAMILY_NAME: ::c_int = 2; -pub const CTRL_ATTR_VERSION: ::c_int = 3; -pub const CTRL_ATTR_HDRSIZE: ::c_int = 4; -pub const CTRL_ATTR_MAXATTR: ::c_int = 5; -pub const CTRL_ATTR_OPS: ::c_int = 6; -pub const CTRL_ATTR_MCAST_GROUPS: ::c_int = 7; -pub const CTRL_ATTR_POLICY: ::c_int = 8; -pub const CTRL_ATTR_OP_POLICY: ::c_int = 9; -pub const CTRL_ATTR_OP: ::c_int = 10; -// -pub const CTRL_ATTR_MCAST_GRP_UNSPEC: ::c_int = 0; -pub const CTRL_ATTR_MCAST_GRP_NAME: ::c_int = 1; -pub const CTRL_ATTR_MCAST_GRP_ID: ::c_int = 2; - pub const LOCAL_CREDS: ::c_int = 2; pub const LOCAL_CREDS_PERSISTENT: ::c_int = 3; pub const LOCAL_CONNWAIT: ::c_int = 4; @@ -5841,3 +5743,12 @@ cfg_if! { // sys/net/if_mib.h pub mod ifmib; + +cfg_if! { + if #[cfg(feature = "future-freebsd-netlink")] { + mod netlink; + pub use self::netlink::*; + } else { + pub use ifmib::*; + } +} diff --git a/src/unix/bsd/freebsdlike/freebsd/netlink.rs b/src/unix/bsd/freebsdlike/freebsd/netlink.rs new file mode 100644 index 0000000000000..65eb701e169fe --- /dev/null +++ b/src/unix/bsd/freebsdlike/freebsd/netlink.rs @@ -0,0 +1,95 @@ +// sys/socket.h +pub const AF_NETLINK: ::c_int = 38; +pub const PF_NETLINK: ::c_int = AF_NETLINK; + +// netlink/netlink.h +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 NLM_F_REQUEST: ::c_int = 0x01; +pub const NLM_F_MULTI: ::c_int = 0x02; +pub const NLM_F_ACK: ::c_int = 0x04; +pub const NLM_F_ECHO: ::c_int = 0x08; +pub const NLM_F_DUMP_INTR: ::c_int = 0x10; +pub const NLM_F_DUMP_FILTERED: ::c_int = 0x20; +// +pub const NLM_F_ROOT: ::c_int = 0x100; +pub const NLM_F_MATCH: ::c_int = 0x200; +pub const NLM_F_ATOMIC: ::c_int = 0x400; +pub const NLM_F_DUMP: ::c_int = NLM_F_ROOT | NLM_F_MATCH; +// +pub const NLM_F_REPLACE: ::c_int = 0x100; +pub const NLM_F_EXCL: ::c_int = 0x200; +pub const NLM_F_CREATE: ::c_int = 0x400; +pub const NLM_F_APPEND: ::c_int = 0x800; +// +pub const NLM_F_NONREC: ::c_int = 0x100; +// +pub const NLM_F_CAPPED: ::c_int = 0x100; +pub const NLM_F_ACK_TLVS: ::c_int = 0x200; +// +pub const NLMSG_NOOP: ::c_int = 0x1; +pub const NLMSG_ERROR: ::c_int = 0x2; +pub const NLMSG_DONE: ::c_int = 0x3; +pub const NLMSG_OVERRUN: ::c_int = 0x4; +// +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; +// +const NL_ITEM_ALIGN_SIZE: ::c_int = 4; // mem::size_of::(); FIXME accept new dep? +pub const NLMSG_ALIGNTO: ::c_int = NL_ITEM_ALIGN_SIZE; + +// netlink/netlink_generic.h +pub const CTRL_CMD_UNSPEC: ::c_int = 0; +pub const CTRL_CMD_NEWFAMILY: ::c_int = 1; +pub const CTRL_CMD_DELFAMILY: ::c_int = 2; +pub const CTRL_CMD_GETFAMILY: ::c_int = 3; +pub const CTRL_CMD_NEWOPS: ::c_int = 4; +pub const CTRL_CMD_DELOPS: ::c_int = 5; +pub const CTRL_CMD_GETOPS: ::c_int = 6; +pub const CTRL_CMD_NEWMCAST_GRP: ::c_int = 7; +pub const CTRL_CMD_DELMCAST_GRP: ::c_int = 8; +pub const CTRL_CMD_GETMCAST_GRP: ::c_int = 9; +pub const CTRL_CMD_GETPOLICY: ::c_int = 10; +// +pub const CTRL_ATTR_UNSPEC: ::c_int = 0; +pub const CTRL_ATTR_FAMILY_ID: ::c_int = 1; +pub const CTRL_ATTR_FAMILY_NAME: ::c_int = 2; +pub const CTRL_ATTR_VERSION: ::c_int = 3; +pub const CTRL_ATTR_HDRSIZE: ::c_int = 4; +pub const CTRL_ATTR_MAXATTR: ::c_int = 5; +pub const CTRL_ATTR_OPS: ::c_int = 6; +pub const CTRL_ATTR_MCAST_GROUPS: ::c_int = 7; +pub const CTRL_ATTR_POLICY: ::c_int = 8; +pub const CTRL_ATTR_OP_POLICY: ::c_int = 9; +pub const CTRL_ATTR_OP: ::c_int = 10; +// +pub const CTRL_ATTR_MCAST_GRP_UNSPEC: ::c_int = 0; +pub const CTRL_ATTR_MCAST_GRP_NAME: ::c_int = 1; +pub const CTRL_ATTR_MCAST_GRP_ID: ::c_int = 2;