Skip to content

Commit 58662d1

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 476635a commit 58662d1

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
@@ -2067,6 +2067,8 @@ fn test_freebsd(target: &str) {
20672067
"netinet/sctp.h",
20682068
"netinet/tcp.h",
20692069
"netinet/udp.h",
2070+
[freebsd13]:"netlink/netlink.h",
2071+
[freebsd13]:"netlink/netlink_generic.h",
20702072
"poll.h",
20712073
"pthread.h",
20722074
"pthread_np.h",
@@ -2301,6 +2303,91 @@ fn test_freebsd(target: &str) {
23012303
// Added in in FreeBSD 13.0 (r367776 and r367287)
23022304
"SCM_CREDS2" | "LOCAL_CREDS_PERSISTENT" if Some(13) > freebsd_ver => true,
23032305

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

@@ -2496,6 +2583,9 @@ fn test_freebsd(target: &str) {
24962583
// `shm_largepage_conf` was introduced in FreeBSD 13.
24972584
"shm_largepage_conf" if Some(13) > freebsd_ver => true,
24982585

2586+
// `sockaddr_nl` introduced in FreeBSD 13.2
2587+
"sockaddr_nl" if Some(13) > freebsd_ver => true,
2588+
24992589
// Those are private types
25002590
"memory_type" => true,
25012591
"memory_type_list" => true,

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

+134
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,14 @@ s_no_extra_traits! {
13801380
pub sdl_data: [::c_char; 46],
13811381
}
13821382

1383+
pub struct sockaddr_nl {
1384+
pub nl_len: ::c_uchar,
1385+
pub nl_family: ::sa_family_t,
1386+
nl_pad: ::c_ushort,
1387+
pub nl_pid: u32,
1388+
pub nl_groups: u32
1389+
}
1390+
13831391
pub struct mq_attr {
13841392
pub mq_flags: ::c_long,
13851393
pub mq_maxmsg: ::c_long,
@@ -1763,6 +1771,34 @@ cfg_if! {
17631771
}
17641772
}
17651773

1774+
impl PartialEq for sockaddr_nl {
1775+
fn eq(&self, other: &sockaddr_nl) -> bool {
1776+
self.nl_len == other.nl_len &&
1777+
self.nl_family == other.nl_family &&
1778+
self.nl_pid == other.nl_pid &&
1779+
self.nl_groups == other.nl_groups
1780+
}
1781+
}
1782+
impl Eq for sockaddr_nl {}
1783+
impl ::fmt::Debug for sockaddr_nl {
1784+
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
1785+
f.debug_struct("sockaddr_nl")
1786+
.field("nl_len", &self.nl_len)
1787+
.field("nl_family", &self.nl_family)
1788+
.field("nl_pid", &self.nl_pid)
1789+
.field("nl_groups", &self.nl_groups)
1790+
.finish()
1791+
}
1792+
}
1793+
impl ::hash::Hash for sockaddr_nl {
1794+
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
1795+
self.nl_len.hash(state);
1796+
self.nl_family.hash(state);
1797+
self.nl_pid.hash(state);
1798+
self.nl_groups.hash(state);
1799+
}
1800+
}
1801+
17661802
impl PartialEq for mq_attr {
17671803
fn eq(&self, other: &mq_attr) -> bool {
17681804
self.mq_flags == other.mq_flags &&
@@ -2989,6 +3025,104 @@ pub const SO_TS_MONOTONIC: ::c_int = 3;
29893025
pub const SO_TS_DEFAULT: ::c_int = SO_TS_REALTIME_MICRO;
29903026
pub const SO_TS_CLOCK_MAX: ::c_int = SO_TS_MONOTONIC;
29913027

3028+
/// netlink constants
3029+
3030+
// sys/socket.h
3031+
pub const AF_NETLINK: ::c_int = 38;
3032+
pub const PF_NETLINK: ::c_int = AF_NETLINK;
3033+
3034+
// netlink/netlink.h
3035+
pub const SOL_NETLINK: ::c_int = 270;
3036+
pub const NETLINK_ADD_MEMBERSHIP: ::c_int = 1;
3037+
pub const NETLINK_DROP_MEMBERSHIP: ::c_int = 2;
3038+
pub const NETLINK_PKTINFO: ::c_int = 3;
3039+
pub const NETLINK_BROADCAST_ERROR: ::c_int = 4;
3040+
pub const NETLINK_NO_ENOBUFS: ::c_int = 5;
3041+
pub const NETLINK_RX_RING: ::c_int = 6;
3042+
pub const NETLINK_TX_RING: ::c_int = 7;
3043+
pub const NETLINK_LISTEN_ALL_NSID: ::c_int = 8;
3044+
pub const NETLINK_LIST_MEMBERSHIPS: ::c_int = 9;
3045+
pub const NETLINK_CAP_ACK: ::c_int = 10;
3046+
pub const NETLINK_EXT_ACK: ::c_int = 11;
3047+
pub const NETLINK_GET_STRICT_CHK: ::c_int = 12;
3048+
//
3049+
pub const NLM_F_REQUEST: ::c_int = 0x01;
3050+
pub const NLM_F_MULTI: ::c_int = 0x02;
3051+
pub const NLM_F_ACK: ::c_int = 0x04;
3052+
pub const NLM_F_ECHO: ::c_int = 0x08;
3053+
pub const NLM_F_DUMP_INTR: ::c_int = 0x10;
3054+
pub const NLM_F_DUMP_FILTERED: ::c_int = 0x20;
3055+
//
3056+
pub const NLM_F_ROOT: ::c_int = 0x100;
3057+
pub const NLM_F_MATCH: ::c_int = 0x200;
3058+
pub const NLM_F_ATOMIC: ::c_int = 0x400;
3059+
pub const NLM_F_DUMP: ::c_int = NLM_F_ROOT | NLM_F_MATCH;
3060+
//
3061+
pub const NLM_F_REPLACE: ::c_int = 0x100;
3062+
pub const NLM_F_EXCL: ::c_int = 0x200;
3063+
pub const NLM_F_CREATE: ::c_int = 0x400;
3064+
pub const NLM_F_APPEND: ::c_int = 0x800;
3065+
//
3066+
pub const NLM_F_NONREC: ::c_int = 0x100;
3067+
//
3068+
pub const NLM_F_CAPPED: ::c_int = 0x100;
3069+
pub const NLM_F_ACK_TLVS: ::c_int = 0x200;
3070+
//
3071+
pub const NLMSG_NOOP: ::c_int = 0x1;
3072+
pub const NLMSG_ERROR: ::c_int = 0x2;
3073+
pub const NLMSG_DONE: ::c_int = 0x3;
3074+
pub const NLMSG_OVERRUN: ::c_int = 0x4;
3075+
//
3076+
pub const NETLINK_ROUTE: ::c_int = 0;
3077+
pub const NETLINK_UNUSED: ::c_int = 1;
3078+
pub const NETLINK_USERSOCK: ::c_int = 2;
3079+
pub const NETLINK_FIREWALL: ::c_int = 3;
3080+
pub const NETLINK_SOCK_DIAG: ::c_int = 4;
3081+
pub const NETLINK_NFLOG: ::c_int = 5;
3082+
pub const NETLINK_XFRM: ::c_int = 6;
3083+
pub const NETLINK_SELINUX: ::c_int = 7;
3084+
pub const NETLINK_ISCSI: ::c_int = 8;
3085+
pub const NETLINK_AUDIT: ::c_int = 9;
3086+
pub const NETLINK_FIB_LOOKUP: ::c_int = 10;
3087+
pub const NETLINK_CONNECTOR: ::c_int = 11;
3088+
pub const NETLINK_NETFILTER: ::c_int = 12;
3089+
pub const NETLINK_IP6_FW: ::c_int = 13;
3090+
pub const NETLINK_DNRTMSG: ::c_int = 14;
3091+
pub const NETLINK_KOBJECT_UEVENT: ::c_int = 15;
3092+
pub const NETLINK_GENERIC: ::c_int = 16;
3093+
//
3094+
const NL_ITEM_ALIGN_SIZE: ::c_int = 4; // mem::size_of::<u32>(); FIXME accept new dep?
3095+
pub const NLMSG_ALIGNTO: ::c_int = NL_ITEM_ALIGN_SIZE;
3096+
3097+
// netlink/netlink_generic.h
3098+
pub const CTRL_CMD_UNSPEC: ::c_int = 0;
3099+
pub const CTRL_CMD_NEWFAMILY: ::c_int = 1;
3100+
pub const CTRL_CMD_DELFAMILY: ::c_int = 2;
3101+
pub const CTRL_CMD_GETFAMILY: ::c_int = 3;
3102+
pub const CTRL_CMD_NEWOPS: ::c_int = 4;
3103+
pub const CTRL_CMD_DELOPS: ::c_int = 5;
3104+
pub const CTRL_CMD_GETOPS: ::c_int = 6;
3105+
pub const CTRL_CMD_NEWMCAST_GRP: ::c_int = 7;
3106+
pub const CTRL_CMD_DELMCAST_GRP: ::c_int = 8;
3107+
pub const CTRL_CMD_GETMCAST_GRP: ::c_int = 9;
3108+
pub const CTRL_CMD_GETPOLICY: ::c_int = 10;
3109+
//
3110+
pub const CTRL_ATTR_UNSPEC: ::c_int = 0;
3111+
pub const CTRL_ATTR_FAMILY_ID: ::c_int = 1;
3112+
pub const CTRL_ATTR_FAMILY_NAME: ::c_int = 2;
3113+
pub const CTRL_ATTR_VERSION: ::c_int = 3;
3114+
pub const CTRL_ATTR_HDRSIZE: ::c_int = 4;
3115+
pub const CTRL_ATTR_MAXATTR: ::c_int = 5;
3116+
pub const CTRL_ATTR_OPS: ::c_int = 6;
3117+
pub const CTRL_ATTR_MCAST_GROUPS: ::c_int = 7;
3118+
pub const CTRL_ATTR_POLICY: ::c_int = 8;
3119+
pub const CTRL_ATTR_OP_POLICY: ::c_int = 9;
3120+
pub const CTRL_ATTR_OP: ::c_int = 10;
3121+
//
3122+
pub const CTRL_ATTR_MCAST_GRP_UNSPEC: ::c_int = 0;
3123+
pub const CTRL_ATTR_MCAST_GRP_NAME: ::c_int = 1;
3124+
pub const CTRL_ATTR_MCAST_GRP_ID: ::c_int = 2;
3125+
29923126
pub const LOCAL_CREDS: ::c_int = 2;
29933127
pub const LOCAL_CREDS_PERSISTENT: ::c_int = 3;
29943128
pub const LOCAL_CONNWAIT: ::c_int = 4;

0 commit comments

Comments
 (0)