Skip to content

Commit 98d71b4

Browse files
committed
Netlink support for FreeBSD 13.2 and later
This is an early subset of the Netlink interface, but it proves sufficient for monitoring changes in IP addresses, the coverage can be extended later as needed. Signed-off-by: Yann Dirson <[email protected]>
1 parent ae16a69 commit 98d71b4

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-0
lines changed

libc-test/build.rs

+90
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,8 @@ fn test_freebsd(target: &str) {
20722072
"netinet/sctp.h",
20732073
"netinet/tcp.h",
20742074
"netinet/udp.h",
2075+
[freebsd13]:"netlink/netlink.h",
2076+
[freebsd13]:"netlink/netlink_generic.h",
20752077
"poll.h",
20762078
"pthread.h",
20772079
"pthread_np.h",
@@ -2312,6 +2314,91 @@ fn test_freebsd(target: &str) {
23122314
// Added in FreeBSD 13.0 (r367776 and r367287)
23132315
"SCM_CREDS2" | "LOCAL_CREDS_PERSISTENT" if Some(13) > freebsd_ver => true,
23142316

2317+
// Added in FreeBSD 13.2
2318+
"AF_NETLINK"
2319+
| "PF_NETLINK"
2320+
| "SOL_NETLINK"
2321+
| "NETLINK_ADD_MEMBERSHIP"
2322+
| "NETLINK_DROP_MEMBERSHIP"
2323+
| "NETLINK_PKTINFO"
2324+
| "NETLINK_BROADCAST_ERROR"
2325+
| "NETLINK_NO_ENOBUFS"
2326+
| "NETLINK_RX_RING"
2327+
| "NETLINK_TX_RING"
2328+
| "NETLINK_LISTEN_ALL_NSID"
2329+
| "NETLINK_LIST_MEMBERSHIPS"
2330+
| "NETLINK_CAP_ACK"
2331+
| "NETLINK_EXT_ACK"
2332+
| "NETLINK_GET_STRICT_CHK"
2333+
| "NLM_F_REQUEST"
2334+
| "NLM_F_MULTI"
2335+
| "NLM_F_ACK"
2336+
| "NLM_F_ECHO"
2337+
| "NLM_F_DUMP_INTR"
2338+
| "NLM_F_DUMP_FILTERED"
2339+
| "NLM_F_ROOT"
2340+
| "NLM_F_MATCH"
2341+
| "NLM_F_ATOMIC"
2342+
| "NLM_F_DUMP"
2343+
| "NLM_F_REPLACE"
2344+
| "NLM_F_EXCL"
2345+
| "NLM_F_CREATE"
2346+
| "NLM_F_APPEND"
2347+
| "NLM_F_NONREC"
2348+
| "NLM_F_CAPPED"
2349+
| "NLM_F_ACK_TLVS"
2350+
| "NLMSG_NOOP"
2351+
| "NLMSG_ERROR"
2352+
| "NLMSG_DONE"
2353+
| "NLMSG_OVERRUN"
2354+
| "NETLINK_ROUTE"
2355+
| "NETLINK_UNUSED"
2356+
| "NETLINK_USERSOCK"
2357+
| "NETLINK_FIREWALL"
2358+
| "NETLINK_SOCK_DIAG"
2359+
| "NETLINK_NFLOG"
2360+
| "NETLINK_XFRM"
2361+
| "NETLINK_SELINUX"
2362+
| "NETLINK_ISCSI"
2363+
| "NETLINK_AUDIT"
2364+
| "NETLINK_FIB_LOOKUP"
2365+
| "NETLINK_CONNECTOR"
2366+
| "NETLINK_NETFILTER"
2367+
| "NETLINK_IP6_FW"
2368+
| "NETLINK_DNRTMSG"
2369+
| "NETLINK_KOBJECT_UEVENT"
2370+
| "NETLINK_GENERIC"
2371+
| "NLMSG_ALIGNTO"
2372+
| "CTRL_CMD_UNSPEC"
2373+
| "CTRL_CMD_NEWFAMILY"
2374+
| "CTRL_CMD_DELFAMILY"
2375+
| "CTRL_CMD_GETFAMILY"
2376+
| "CTRL_CMD_NEWOPS"
2377+
| "CTRL_CMD_DELOPS"
2378+
| "CTRL_CMD_GETOPS"
2379+
| "CTRL_CMD_NEWMCAST_GRP"
2380+
| "CTRL_CMD_DELMCAST_GRP"
2381+
| "CTRL_CMD_GETMCAST_GRP"
2382+
| "CTRL_CMD_GETPOLICY"
2383+
| "CTRL_ATTR_UNSPEC"
2384+
| "CTRL_ATTR_FAMILY_ID"
2385+
| "CTRL_ATTR_FAMILY_NAME"
2386+
| "CTRL_ATTR_VERSION"
2387+
| "CTRL_ATTR_HDRSIZE"
2388+
| "CTRL_ATTR_MAXATTR"
2389+
| "CTRL_ATTR_OPS"
2390+
| "CTRL_ATTR_MCAST_GROUPS"
2391+
| "CTRL_ATTR_POLICY"
2392+
| "CTRL_ATTR_OP_POLICY"
2393+
| "CTRL_ATTR_OP"
2394+
| "CTRL_ATTR_MCAST_GRP_UNSPEC"
2395+
| "CTRL_ATTR_MCAST_GRP_NAME"
2396+
| "CTRL_ATTR_MCAST_GRP_ID"
2397+
if Some(13) > freebsd_ver =>
2398+
{
2399+
true
2400+
}
2401+
23152402
// Added in FreeBSD 14
23162403
"SPACECTL_DEALLOC" if Some(14) > freebsd_ver => true,
23172404

@@ -2517,6 +2604,9 @@ fn test_freebsd(target: &str) {
25172604
// `shm_largepage_conf` was introduced in FreeBSD 13.
25182605
"shm_largepage_conf" if Some(13) > freebsd_ver => true,
25192606

2607+
// `sockaddr_nl` introduced in FreeBSD 13.2
2608+
"sockaddr_nl" if Some(13) > freebsd_ver => true,
2609+
25202610
// Those are private types
25212611
"memory_type" => true,
25222612
"memory_type_list" => true,

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

+134
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,14 @@ s_no_extra_traits! {
13881388
pub sdl_data: [::c_char; 46],
13891389
}
13901390

1391+
pub struct sockaddr_nl {
1392+
pub nl_len: ::c_uchar,
1393+
pub nl_family: ::sa_family_t,
1394+
nl_pad: ::c_ushort,
1395+
pub nl_pid: u32,
1396+
pub nl_groups: u32
1397+
}
1398+
13911399
pub struct mq_attr {
13921400
pub mq_flags: ::c_long,
13931401
pub mq_maxmsg: ::c_long,
@@ -1788,6 +1796,34 @@ cfg_if! {
17881796
}
17891797
}
17901798

1799+
impl PartialEq for sockaddr_nl {
1800+
fn eq(&self, other: &sockaddr_nl) -> bool {
1801+
self.nl_len == other.nl_len &&
1802+
self.nl_family == other.nl_family &&
1803+
self.nl_pid == other.nl_pid &&
1804+
self.nl_groups == other.nl_groups
1805+
}
1806+
}
1807+
impl Eq for sockaddr_nl {}
1808+
impl ::fmt::Debug for sockaddr_nl {
1809+
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
1810+
f.debug_struct("sockaddr_nl")
1811+
.field("nl_len", &self.nl_len)
1812+
.field("nl_family", &self.nl_family)
1813+
.field("nl_pid", &self.nl_pid)
1814+
.field("nl_groups", &self.nl_groups)
1815+
.finish()
1816+
}
1817+
}
1818+
impl ::hash::Hash for sockaddr_nl {
1819+
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
1820+
self.nl_len.hash(state);
1821+
self.nl_family.hash(state);
1822+
self.nl_pid.hash(state);
1823+
self.nl_groups.hash(state);
1824+
}
1825+
}
1826+
17911827
impl PartialEq for mq_attr {
17921828
fn eq(&self, other: &mq_attr) -> bool {
17931829
self.mq_flags == other.mq_flags &&
@@ -3183,6 +3219,104 @@ pub const SO_TS_MONOTONIC: ::c_int = 3;
31833219
pub const SO_TS_DEFAULT: ::c_int = SO_TS_REALTIME_MICRO;
31843220
pub const SO_TS_CLOCK_MAX: ::c_int = SO_TS_MONOTONIC;
31853221

3222+
/// netlink constants
3223+
3224+
// sys/socket.h
3225+
pub const AF_NETLINK: ::c_int = 38;
3226+
pub const PF_NETLINK: ::c_int = AF_NETLINK;
3227+
3228+
// netlink/netlink.h
3229+
pub const SOL_NETLINK: ::c_int = 270;
3230+
pub const NETLINK_ADD_MEMBERSHIP: ::c_int = 1;
3231+
pub const NETLINK_DROP_MEMBERSHIP: ::c_int = 2;
3232+
pub const NETLINK_PKTINFO: ::c_int = 3;
3233+
pub const NETLINK_BROADCAST_ERROR: ::c_int = 4;
3234+
pub const NETLINK_NO_ENOBUFS: ::c_int = 5;
3235+
pub const NETLINK_RX_RING: ::c_int = 6;
3236+
pub const NETLINK_TX_RING: ::c_int = 7;
3237+
pub const NETLINK_LISTEN_ALL_NSID: ::c_int = 8;
3238+
pub const NETLINK_LIST_MEMBERSHIPS: ::c_int = 9;
3239+
pub const NETLINK_CAP_ACK: ::c_int = 10;
3240+
pub const NETLINK_EXT_ACK: ::c_int = 11;
3241+
pub const NETLINK_GET_STRICT_CHK: ::c_int = 12;
3242+
//
3243+
pub const NLM_F_REQUEST: ::c_int = 0x01;
3244+
pub const NLM_F_MULTI: ::c_int = 0x02;
3245+
pub const NLM_F_ACK: ::c_int = 0x04;
3246+
pub const NLM_F_ECHO: ::c_int = 0x08;
3247+
pub const NLM_F_DUMP_INTR: ::c_int = 0x10;
3248+
pub const NLM_F_DUMP_FILTERED: ::c_int = 0x20;
3249+
//
3250+
pub const NLM_F_ROOT: ::c_int = 0x100;
3251+
pub const NLM_F_MATCH: ::c_int = 0x200;
3252+
pub const NLM_F_ATOMIC: ::c_int = 0x400;
3253+
pub const NLM_F_DUMP: ::c_int = NLM_F_ROOT | NLM_F_MATCH;
3254+
//
3255+
pub const NLM_F_REPLACE: ::c_int = 0x100;
3256+
pub const NLM_F_EXCL: ::c_int = 0x200;
3257+
pub const NLM_F_CREATE: ::c_int = 0x400;
3258+
pub const NLM_F_APPEND: ::c_int = 0x800;
3259+
//
3260+
pub const NLM_F_NONREC: ::c_int = 0x100;
3261+
//
3262+
pub const NLM_F_CAPPED: ::c_int = 0x100;
3263+
pub const NLM_F_ACK_TLVS: ::c_int = 0x200;
3264+
//
3265+
pub const NLMSG_NOOP: ::c_int = 0x1;
3266+
pub const NLMSG_ERROR: ::c_int = 0x2;
3267+
pub const NLMSG_DONE: ::c_int = 0x3;
3268+
pub const NLMSG_OVERRUN: ::c_int = 0x4;
3269+
//
3270+
pub const NETLINK_ROUTE: ::c_int = 0;
3271+
pub const NETLINK_UNUSED: ::c_int = 1;
3272+
pub const NETLINK_USERSOCK: ::c_int = 2;
3273+
pub const NETLINK_FIREWALL: ::c_int = 3;
3274+
pub const NETLINK_SOCK_DIAG: ::c_int = 4;
3275+
pub const NETLINK_NFLOG: ::c_int = 5;
3276+
pub const NETLINK_XFRM: ::c_int = 6;
3277+
pub const NETLINK_SELINUX: ::c_int = 7;
3278+
pub const NETLINK_ISCSI: ::c_int = 8;
3279+
pub const NETLINK_AUDIT: ::c_int = 9;
3280+
pub const NETLINK_FIB_LOOKUP: ::c_int = 10;
3281+
pub const NETLINK_CONNECTOR: ::c_int = 11;
3282+
pub const NETLINK_NETFILTER: ::c_int = 12;
3283+
pub const NETLINK_IP6_FW: ::c_int = 13;
3284+
pub const NETLINK_DNRTMSG: ::c_int = 14;
3285+
pub const NETLINK_KOBJECT_UEVENT: ::c_int = 15;
3286+
pub const NETLINK_GENERIC: ::c_int = 16;
3287+
//
3288+
const NL_ITEM_ALIGN_SIZE: ::c_int = 4; // mem::size_of::<u32>(); FIXME accept new dep?
3289+
pub const NLMSG_ALIGNTO: ::c_int = NL_ITEM_ALIGN_SIZE;
3290+
3291+
// netlink/netlink_generic.h
3292+
pub const CTRL_CMD_UNSPEC: ::c_int = 0;
3293+
pub const CTRL_CMD_NEWFAMILY: ::c_int = 1;
3294+
pub const CTRL_CMD_DELFAMILY: ::c_int = 2;
3295+
pub const CTRL_CMD_GETFAMILY: ::c_int = 3;
3296+
pub const CTRL_CMD_NEWOPS: ::c_int = 4;
3297+
pub const CTRL_CMD_DELOPS: ::c_int = 5;
3298+
pub const CTRL_CMD_GETOPS: ::c_int = 6;
3299+
pub const CTRL_CMD_NEWMCAST_GRP: ::c_int = 7;
3300+
pub const CTRL_CMD_DELMCAST_GRP: ::c_int = 8;
3301+
pub const CTRL_CMD_GETMCAST_GRP: ::c_int = 9;
3302+
pub const CTRL_CMD_GETPOLICY: ::c_int = 10;
3303+
//
3304+
pub const CTRL_ATTR_UNSPEC: ::c_int = 0;
3305+
pub const CTRL_ATTR_FAMILY_ID: ::c_int = 1;
3306+
pub const CTRL_ATTR_FAMILY_NAME: ::c_int = 2;
3307+
pub const CTRL_ATTR_VERSION: ::c_int = 3;
3308+
pub const CTRL_ATTR_HDRSIZE: ::c_int = 4;
3309+
pub const CTRL_ATTR_MAXATTR: ::c_int = 5;
3310+
pub const CTRL_ATTR_OPS: ::c_int = 6;
3311+
pub const CTRL_ATTR_MCAST_GROUPS: ::c_int = 7;
3312+
pub const CTRL_ATTR_POLICY: ::c_int = 8;
3313+
pub const CTRL_ATTR_OP_POLICY: ::c_int = 9;
3314+
pub const CTRL_ATTR_OP: ::c_int = 10;
3315+
//
3316+
pub const CTRL_ATTR_MCAST_GRP_UNSPEC: ::c_int = 0;
3317+
pub const CTRL_ATTR_MCAST_GRP_NAME: ::c_int = 1;
3318+
pub const CTRL_ATTR_MCAST_GRP_ID: ::c_int = 2;
3319+
31863320
pub const LOCAL_CREDS: ::c_int = 2;
31873321
pub const LOCAL_CREDS_PERSISTENT: ::c_int = 3;
31883322
pub const LOCAL_CONNWAIT: ::c_int = 4;

0 commit comments

Comments
 (0)