Skip to content

Commit 7eda3b0

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 987dc97 commit 7eda3b0

File tree

2 files changed

+272
-1
lines changed

2 files changed

+272
-1
lines changed

libc-test/build.rs

+119-1
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,11 @@ fn test_freebsd(target: &str) {
20222022
// Required for making freebsd11_stat available in the headers
20232023
cfg.define("_WANT_FREEBSD11_STAT", None);
20242024

2025+
let freebsd12 = match freebsd_ver {
2026+
Some(n) if n >= 12 => true,
2027+
_ => false,
2028+
};
2029+
20252030
let freebsd13 = match freebsd_ver {
20262031
Some(n) if n >= 13 => true,
20272032
_ => false,
@@ -2071,6 +2076,9 @@ fn test_freebsd(target: &str) {
20712076
"netinet/sctp.h",
20722077
"netinet/tcp.h",
20732078
"netinet/udp.h",
2079+
[freebsd13]:"netlink/netlink.h",
2080+
[freebsd13]:"netlink/netlink_generic.h",
2081+
[freebsd12]:"pcap/nflog.h",
20742082
"poll.h",
20752083
"pthread.h",
20762084
"pthread_np.h",
@@ -2302,9 +2310,116 @@ fn test_freebsd(target: &str) {
23022310
true
23032311
}
23042312

2305-
// Added in in FreeBSD 13.0 (r367776 and r367287)
2313+
// Added in FreeBSD 13.0 (r367776 and r367287)
23062314
"SCM_CREDS2" | "LOCAL_CREDS_PERSISTENT" if Some(13) > freebsd_ver => true,
23072315

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

@@ -2490,6 +2605,9 @@ fn test_freebsd(target: &str) {
24902605
// `shm_largepage_conf` was introduced in FreeBSD 13.
24912606
"shm_largepage_conf" if Some(13) > freebsd_ver => true,
24922607

2608+
// `sockaddr_nl` introduced in FreeBSD 13.2
2609+
"sockaddr_nl" if Some(13) > freebsd_ver => true,
2610+
24932611
// Those are private types
24942612
"memory_type" => true,
24952613
"memory_type_list" => true,

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

+153
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,14 @@ s_no_extra_traits! {
13851385
pub sdl_data: [::c_char; 46],
13861386
}
13871387

1388+
pub struct sockaddr_nl {
1389+
pub nl_len: ::c_uchar,
1390+
pub nl_family: ::sa_family_t,
1391+
nl_pad: ::c_ushort,
1392+
pub nl_pid: u32,
1393+
pub nl_groups: u32
1394+
}
1395+
13881396
pub struct mq_attr {
13891397
pub mq_flags: ::c_long,
13901398
pub mq_maxmsg: ::c_long,
@@ -1798,6 +1806,34 @@ cfg_if! {
17981806
}
17991807
}
18001808

1809+
impl PartialEq for sockaddr_nl {
1810+
fn eq(&self, other: &sockaddr_nl) -> bool {
1811+
self.nl_len == other.nl_len &&
1812+
self.nl_family == other.nl_family &&
1813+
self.nl_pid == other.nl_pid &&
1814+
self.nl_groups == other.nl_groups
1815+
}
1816+
}
1817+
impl Eq for sockaddr_nl {}
1818+
impl ::fmt::Debug for sockaddr_nl {
1819+
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
1820+
f.debug_struct("sockaddr_nl")
1821+
.field("nl_len", &self.nl_len)
1822+
.field("nl_family", &self.nl_family)
1823+
.field("nl_pid", &self.nl_pid)
1824+
.field("nl_groups", &self.nl_groups)
1825+
.finish()
1826+
}
1827+
}
1828+
impl ::hash::Hash for sockaddr_nl {
1829+
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
1830+
self.nl_len.hash(state);
1831+
self.nl_family.hash(state);
1832+
self.nl_pid.hash(state);
1833+
self.nl_groups.hash(state);
1834+
}
1835+
}
1836+
18011837
impl PartialEq for mq_attr {
18021838
fn eq(&self, other: &mq_attr) -> bool {
18031839
self.mq_flags == other.mq_flags &&
@@ -3059,6 +3095,123 @@ pub const SO_TS_MONOTONIC: ::c_int = 3;
30593095
pub const SO_TS_DEFAULT: ::c_int = SO_TS_REALTIME_MICRO;
30603096
pub const SO_TS_CLOCK_MAX: ::c_int = SO_TS_MONOTONIC;
30613097

3098+
/// netlink constants
3099+
3100+
// sys/socket.h
3101+
pub const AF_NETLINK: ::c_int = 38;
3102+
pub const PF_NETLINK: ::c_int = AF_NETLINK;
3103+
3104+
// netlink/netlink.h
3105+
pub const SOL_NETLINK: ::c_int = 270;
3106+
pub const NETLINK_ADD_MEMBERSHIP: ::c_int = 1;
3107+
pub const NETLINK_DROP_MEMBERSHIP: ::c_int = 2;
3108+
pub const NETLINK_PKTINFO: ::c_int = 3;
3109+
pub const NETLINK_BROADCAST_ERROR: ::c_int = 4;
3110+
pub const NETLINK_NO_ENOBUFS: ::c_int = 5;
3111+
pub const NETLINK_RX_RING: ::c_int = 6;
3112+
pub const NETLINK_TX_RING: ::c_int = 7;
3113+
pub const NETLINK_LISTEN_ALL_NSID: ::c_int = 8;
3114+
pub const NETLINK_LIST_MEMBERSHIPS: ::c_int = 9;
3115+
pub const NETLINK_CAP_ACK: ::c_int = 10;
3116+
pub const NETLINK_EXT_ACK: ::c_int = 11;
3117+
pub const NETLINK_GET_STRICT_CHK: ::c_int = 12;
3118+
//
3119+
pub const NLM_F_REQUEST: ::c_int = 0x01;
3120+
pub const NLM_F_MULTI: ::c_int = 0x02;
3121+
pub const NLM_F_ACK: ::c_int = 0x04;
3122+
pub const NLM_F_ECHO: ::c_int = 0x08;
3123+
pub const NLM_F_DUMP_INTR: ::c_int = 0x10;
3124+
pub const NLM_F_DUMP_FILTERED: ::c_int = 0x20;
3125+
//
3126+
pub const NLM_F_ROOT: ::c_int = 0x100;
3127+
pub const NLM_F_MATCH: ::c_int = 0x200;
3128+
pub const NLM_F_ATOMIC: ::c_int = 0x400;
3129+
pub const NLM_F_DUMP: ::c_int = NLM_F_ROOT | NLM_F_MATCH;
3130+
//
3131+
pub const NLM_F_REPLACE: ::c_int = 0x100;
3132+
pub const NLM_F_EXCL: ::c_int = 0x200;
3133+
pub const NLM_F_CREATE: ::c_int = 0x400;
3134+
pub const NLM_F_APPEND: ::c_int = 0x800;
3135+
//
3136+
pub const NLM_F_NONREC: ::c_int = 0x100;
3137+
//
3138+
pub const NLM_F_CAPPED: ::c_int = 0x100;
3139+
pub const NLM_F_ACK_TLVS: ::c_int = 0x200;
3140+
//
3141+
pub const NLMSG_NOOP: ::c_int = 0x1;
3142+
pub const NLMSG_ERROR: ::c_int = 0x2;
3143+
pub const NLMSG_DONE: ::c_int = 0x3;
3144+
pub const NLMSG_OVERRUN: ::c_int = 0x4;
3145+
//
3146+
pub const NETLINK_ROUTE: ::c_int = 0;
3147+
pub const NETLINK_UNUSED: ::c_int = 1;
3148+
pub const NETLINK_USERSOCK: ::c_int = 2;
3149+
pub const NETLINK_FIREWALL: ::c_int = 3;
3150+
pub const NETLINK_SOCK_DIAG: ::c_int = 4;
3151+
pub const NETLINK_NFLOG: ::c_int = 5;
3152+
pub const NETLINK_XFRM: ::c_int = 6;
3153+
pub const NETLINK_SELINUX: ::c_int = 7;
3154+
pub const NETLINK_ISCSI: ::c_int = 8;
3155+
pub const NETLINK_AUDIT: ::c_int = 9;
3156+
pub const NETLINK_FIB_LOOKUP: ::c_int = 10;
3157+
pub const NETLINK_CONNECTOR: ::c_int = 11;
3158+
pub const NETLINK_NETFILTER: ::c_int = 12;
3159+
pub const NETLINK_IP6_FW: ::c_int = 13;
3160+
pub const NETLINK_DNRTMSG: ::c_int = 14;
3161+
pub const NETLINK_KOBJECT_UEVENT: ::c_int = 15;
3162+
pub const NETLINK_GENERIC: ::c_int = 16;
3163+
//
3164+
const NL_ITEM_ALIGN_SIZE: ::c_int = 4; // mem::size_of::<u32>(); FIXME accept new dep?
3165+
pub const NLMSG_ALIGNTO: ::c_int = NL_ITEM_ALIGN_SIZE;
3166+
3167+
// netlink/netlink_generic.h
3168+
pub const CTRL_CMD_UNSPEC: ::c_int = 0;
3169+
pub const CTRL_CMD_NEWFAMILY: ::c_int = 1;
3170+
pub const CTRL_CMD_DELFAMILY: ::c_int = 2;
3171+
pub const CTRL_CMD_GETFAMILY: ::c_int = 3;
3172+
pub const CTRL_CMD_NEWOPS: ::c_int = 4;
3173+
pub const CTRL_CMD_DELOPS: ::c_int = 5;
3174+
pub const CTRL_CMD_GETOPS: ::c_int = 6;
3175+
pub const CTRL_CMD_NEWMCAST_GRP: ::c_int = 7;
3176+
pub const CTRL_CMD_DELMCAST_GRP: ::c_int = 8;
3177+
pub const CTRL_CMD_GETMCAST_GRP: ::c_int = 9;
3178+
pub const CTRL_CMD_GETPOLICY: ::c_int = 10;
3179+
//
3180+
pub const CTRL_ATTR_UNSPEC: ::c_int = 0;
3181+
pub const CTRL_ATTR_FAMILY_ID: ::c_int = 1;
3182+
pub const CTRL_ATTR_FAMILY_NAME: ::c_int = 2;
3183+
pub const CTRL_ATTR_VERSION: ::c_int = 3;
3184+
pub const CTRL_ATTR_HDRSIZE: ::c_int = 4;
3185+
pub const CTRL_ATTR_MAXATTR: ::c_int = 5;
3186+
pub const CTRL_ATTR_OPS: ::c_int = 6;
3187+
pub const CTRL_ATTR_MCAST_GROUPS: ::c_int = 7;
3188+
pub const CTRL_ATTR_POLICY: ::c_int = 8;
3189+
pub const CTRL_ATTR_OP_POLICY: ::c_int = 9;
3190+
pub const CTRL_ATTR_OP: ::c_int = 10;
3191+
//
3192+
pub const CTRL_ATTR_MCAST_GRP_UNSPEC: ::c_int = 0;
3193+
pub const CTRL_ATTR_MCAST_GRP_NAME: ::c_int = 1;
3194+
pub const CTRL_ATTR_MCAST_GRP_ID: ::c_int = 2;
3195+
3196+
// pcap/nflog.h
3197+
pub const NFULA_PACKET_HDR: ::c_int = 1;
3198+
pub const NFULA_MARK: ::c_int = 2;
3199+
pub const NFULA_TIMESTAMP: ::c_int = 3;
3200+
pub const NFULA_IFINDEX_INDEV: ::c_int = 4;
3201+
pub const NFULA_IFINDEX_OUTDEV: ::c_int = 5;
3202+
pub const NFULA_IFINDEX_PHYSINDEV: ::c_int = 6;
3203+
pub const NFULA_IFINDEX_PHYSOUTDEV: ::c_int = 7;
3204+
pub const NFULA_HWADDR: ::c_int = 8;
3205+
pub const NFULA_PAYLOAD: ::c_int = 9;
3206+
pub const NFULA_PREFIX: ::c_int = 10;
3207+
pub const NFULA_UID: ::c_int = 11;
3208+
pub const NFULA_SEQ: ::c_int = 12;
3209+
pub const NFULA_SEQ_GLOBAL: ::c_int = 13;
3210+
pub const NFULA_GID: ::c_int = 14;
3211+
pub const NFULA_HWTYPE: ::c_int = 15;
3212+
pub const NFULA_HWHEADER: ::c_int = 16;
3213+
pub const NFULA_HWLEN: ::c_int = 17;
3214+
30623215
pub const LOCAL_CREDS: ::c_int = 2;
30633216
pub const LOCAL_CREDS_PERSISTENT: ::c_int = 3;
30643217
pub const LOCAL_CONNWAIT: ::c_int = 4;

0 commit comments

Comments
 (0)