Skip to content

Commit f2adf2a

Browse files
committed
WIP Netlink support for FreeBSD 13.2 and later
NOTE: This is an early subset of the Netlink interface. Signed-off-by: Yann Dirson <[email protected]>
1 parent 92a5d3e commit f2adf2a

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

libc-test/build.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -1998,6 +1998,7 @@ fn test_freebsd(target: &str) {
19981998
"netinet/sctp.h",
19991999
"netinet/tcp.h",
20002000
"netinet/udp.h",
2001+
[freebsd13]:"netlink/netlink.h",
20012002
"poll.h",
20022003
"pthread.h",
20032004
"pthread_np.h",
@@ -2222,9 +2223,47 @@ fn test_freebsd(target: &str) {
22222223
true
22232224
}
22242225

2225-
// Added in in FreeBSD 13.0 (r367776 and r367287)
2226+
// Added in FreeBSD 13.0 (r367776 and r367287)
22262227
"SCM_CREDS2" | "LOCAL_CREDS_PERSISTENT" if Some(13) > freebsd_ver => true,
22272228

2229+
// Added in FreeBSD 13.2
2230+
"SOL_NETLINK"
2231+
| "NETLINK_ADD_MEMBERSHIP"
2232+
| "NETLINK_DROP_MEMBERSHIP"
2233+
| "NETLINK_PKTINFO"
2234+
| "NETLINK_BROADCAST_ERROR"
2235+
| "NETLINK_NO_ENOBUFS"
2236+
| "NETLINK_RX_RING"
2237+
| "NETLINK_TX_RING"
2238+
| "NETLINK_LISTEN_ALL_NSID"
2239+
| "NETLINK_LIST_MEMBERSHIPS"
2240+
| "NETLINK_CAP_ACK"
2241+
| "NETLINK_EXT_ACK"
2242+
| "NETLINK_GET_STRICT_CHK"
2243+
| "AF_NETLINK"
2244+
| "PF_NETLINK"
2245+
| "NETLINK_ROUTE"
2246+
| "NETLINK_UNUSED"
2247+
| "NETLINK_USERSOCK"
2248+
| "NETLINK_FIREWALL"
2249+
| "NETLINK_SOCK_DIAG"
2250+
| "NETLINK_NFLOG"
2251+
| "NETLINK_XFRM"
2252+
| "NETLINK_SELINUX"
2253+
| "NETLINK_ISCSI"
2254+
| "NETLINK_AUDIT"
2255+
| "NETLINK_FIB_LOOKUP"
2256+
| "NETLINK_CONNECTOR"
2257+
| "NETLINK_NETFILTER"
2258+
| "NETLINK_IP6_FW"
2259+
| "NETLINK_DNRTMSG"
2260+
| "NETLINK_KOBJECT_UEVENT"
2261+
| "NETLINK_GENERIC"
2262+
if Some(13) > freebsd_ver =>
2263+
{
2264+
true
2265+
}
2266+
22282267
// Added in FreeBSD 14
22292268
"SPACECTL_DEALLOC" if Some(14) > freebsd_ver => true,
22302269

@@ -2378,6 +2417,9 @@ fn test_freebsd(target: &str) {
23782417
// `shm_largepage_conf` was introduced in FreeBSD 13.
23792418
"shm_largepage_conf" if Some(13) > freebsd_ver => true,
23802419

2420+
// `sockaddr_nl` introduced in FreeBSD 13.2
2421+
"sockaddr_nl" if Some(13) > freebsd_ver => true,
2422+
23812423
// Those are private types
23822424
"memory_type" => true,
23832425
"memory_type_list" => true,

src/unix/bsd/freebsdlike/freebsd/mod.rs

+72
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,14 @@ s_no_extra_traits! {
13611361
pub sdl_data: [::c_char; 46],
13621362
}
13631363

1364+
pub struct sockaddr_nl {
1365+
pub nl_len: ::c_uchar,
1366+
pub nl_family: ::sa_family_t,
1367+
nl_pad: ::c_ushort,
1368+
pub nl_pid: u32,
1369+
pub nl_groups: u32
1370+
}
1371+
13641372
pub struct mq_attr {
13651373
pub mq_flags: ::c_long,
13661374
pub mq_maxmsg: ::c_long,
@@ -1774,6 +1782,34 @@ cfg_if! {
17741782
}
17751783
}
17761784

1785+
impl PartialEq for sockaddr_nl {
1786+
fn eq(&self, other: &sockaddr_nl) -> bool {
1787+
self.nl_len == other.nl_len &&
1788+
self.nl_family == other.nl_family &&
1789+
self.nl_pid == other.nl_pid &&
1790+
self.nl_groups == other.nl_groups
1791+
}
1792+
}
1793+
impl Eq for sockaddr_nl {}
1794+
impl ::fmt::Debug for sockaddr_nl {
1795+
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
1796+
f.debug_struct("sockaddr_nl")
1797+
.field("nl_len", &self.nl_len)
1798+
.field("nl_family", &self.nl_family)
1799+
.field("nl_pid", &self.nl_pid)
1800+
.field("nl_groups", &self.nl_groups)
1801+
.finish()
1802+
}
1803+
}
1804+
impl ::hash::Hash for sockaddr_nl {
1805+
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
1806+
self.nl_len.hash(state);
1807+
self.nl_family.hash(state);
1808+
self.nl_pid.hash(state);
1809+
self.nl_groups.hash(state);
1810+
}
1811+
}
1812+
17771813
impl PartialEq for mq_attr {
17781814
fn eq(&self, other: &mq_attr) -> bool {
17791815
self.mq_flags == other.mq_flags &&
@@ -3027,6 +3063,42 @@ pub const SO_TS_MONOTONIC: ::c_int = 3;
30273063
pub const SO_TS_DEFAULT: ::c_int = SO_TS_REALTIME_MICRO;
30283064
pub const SO_TS_CLOCK_MAX: ::c_int = SO_TS_MONOTONIC;
30293065

3066+
/// netlink constants
3067+
pub const SOL_NETLINK: ::c_int = 270;
3068+
pub const NETLINK_ADD_MEMBERSHIP: ::c_int = 1;
3069+
pub const NETLINK_DROP_MEMBERSHIP: ::c_int = 2;
3070+
pub const NETLINK_PKTINFO: ::c_int = 3;
3071+
pub const NETLINK_BROADCAST_ERROR: ::c_int = 4;
3072+
pub const NETLINK_NO_ENOBUFS: ::c_int = 5;
3073+
pub const NETLINK_RX_RING: ::c_int = 6;
3074+
pub const NETLINK_TX_RING: ::c_int = 7;
3075+
pub const NETLINK_LISTEN_ALL_NSID: ::c_int = 8;
3076+
pub const NETLINK_LIST_MEMBERSHIPS: ::c_int = 9;
3077+
pub const NETLINK_CAP_ACK: ::c_int = 10;
3078+
pub const NETLINK_EXT_ACK: ::c_int = 11;
3079+
pub const NETLINK_GET_STRICT_CHK: ::c_int = 12;
3080+
3081+
pub const AF_NETLINK: ::c_int = 38;
3082+
pub const PF_NETLINK: ::c_int = AF_NETLINK;
3083+
3084+
pub const NETLINK_ROUTE: ::c_int = 0;
3085+
pub const NETLINK_UNUSED: ::c_int = 1;
3086+
pub const NETLINK_USERSOCK: ::c_int = 2;
3087+
pub const NETLINK_FIREWALL: ::c_int = 3;
3088+
pub const NETLINK_SOCK_DIAG: ::c_int = 4;
3089+
pub const NETLINK_NFLOG: ::c_int = 5;
3090+
pub const NETLINK_XFRM: ::c_int = 6;
3091+
pub const NETLINK_SELINUX: ::c_int = 7;
3092+
pub const NETLINK_ISCSI: ::c_int = 8;
3093+
pub const NETLINK_AUDIT: ::c_int = 9;
3094+
pub const NETLINK_FIB_LOOKUP: ::c_int = 10;
3095+
pub const NETLINK_CONNECTOR: ::c_int = 11;
3096+
pub const NETLINK_NETFILTER: ::c_int = 12;
3097+
pub const NETLINK_IP6_FW: ::c_int = 13;
3098+
pub const NETLINK_DNRTMSG: ::c_int = 14;
3099+
pub const NETLINK_KOBJECT_UEVENT: ::c_int = 15;
3100+
pub const NETLINK_GENERIC: ::c_int = 16;
3101+
30303102
pub const LOCAL_CREDS: ::c_int = 2;
30313103
pub const LOCAL_CREDS_PERSISTENT: ::c_int = 3;
30323104
pub const LOCAL_CONNWAIT: ::c_int = 4;

0 commit comments

Comments
 (0)