Skip to content

Commit d3b0e4e

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 58f3ce4 commit d3b0e4e

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
@@ -1956,6 +1956,11 @@ fn test_freebsd(target: &str) {
19561956
// Required for making freebsd11_stat available in the headers
19571957
cfg.define("_WANT_FREEBSD11_STAT", None);
19581958

1959+
let freebsd12 = match freebsd_ver {
1960+
Some(n) if n >= 12 => true,
1961+
_ => false,
1962+
};
1963+
19591964
let freebsd13 = match freebsd_ver {
19601965
Some(n) if n >= 13 => true,
19611966
_ => false,
@@ -2001,6 +2006,9 @@ fn test_freebsd(target: &str) {
20012006
"netinet/sctp.h",
20022007
"netinet/tcp.h",
20032008
"netinet/udp.h",
2009+
[freebsd13]:"netlink/netlink.h",
2010+
[freebsd13]:"netlink/netlink_generic.h",
2011+
[freebsd12]:"pcap/nflog.h",
20042012
"poll.h",
20052013
"pthread.h",
20062014
"pthread_np.h",
@@ -2225,9 +2233,116 @@ fn test_freebsd(target: &str) {
22252233
true
22262234
}
22272235

2228-
// Added in in FreeBSD 13.0 (r367776 and r367287)
2236+
// Added in FreeBSD 13.0 (r367776 and r367287)
22292237
"SCM_CREDS2" | "LOCAL_CREDS_PERSISTENT" if Some(13) > freebsd_ver => true,
22302238

2239+
// Added in FreeBSD 13.2
2240+
"AF_NETLINK"
2241+
| "PF_NETLINK"
2242+
| "SOL_NETLINK"
2243+
| "NETLINK_ADD_MEMBERSHIP"
2244+
| "NETLINK_DROP_MEMBERSHIP"
2245+
| "NETLINK_PKTINFO"
2246+
| "NETLINK_BROADCAST_ERROR"
2247+
| "NETLINK_NO_ENOBUFS"
2248+
| "NETLINK_RX_RING"
2249+
| "NETLINK_TX_RING"
2250+
| "NETLINK_LISTEN_ALL_NSID"
2251+
| "NETLINK_LIST_MEMBERSHIPS"
2252+
| "NETLINK_CAP_ACK"
2253+
| "NETLINK_EXT_ACK"
2254+
| "NETLINK_GET_STRICT_CHK"
2255+
| "NLM_F_REQUEST"
2256+
| "NLM_F_MULTI"
2257+
| "NLM_F_ACK"
2258+
| "NLM_F_ECHO"
2259+
| "NLM_F_DUMP_INTR"
2260+
| "NLM_F_DUMP_FILTERED"
2261+
| "NLM_F_ROOT"
2262+
| "NLM_F_MATCH"
2263+
| "NLM_F_ATOMIC"
2264+
| "NLM_F_DUMP"
2265+
| "NLM_F_REPLACE"
2266+
| "NLM_F_EXCL"
2267+
| "NLM_F_CREATE"
2268+
| "NLM_F_APPEND"
2269+
| "NLM_F_NONREC"
2270+
| "NLM_F_CAPPED"
2271+
| "NLM_F_ACK_TLVS"
2272+
| "NLMSG_NOOP"
2273+
| "NLMSG_ERROR"
2274+
| "NLMSG_DONE"
2275+
| "NLMSG_OVERRUN"
2276+
| "NETLINK_ROUTE"
2277+
| "NETLINK_UNUSED"
2278+
| "NETLINK_USERSOCK"
2279+
| "NETLINK_FIREWALL"
2280+
| "NETLINK_SOCK_DIAG"
2281+
| "NETLINK_NFLOG"
2282+
| "NETLINK_XFRM"
2283+
| "NETLINK_SELINUX"
2284+
| "NETLINK_ISCSI"
2285+
| "NETLINK_AUDIT"
2286+
| "NETLINK_FIB_LOOKUP"
2287+
| "NETLINK_CONNECTOR"
2288+
| "NETLINK_NETFILTER"
2289+
| "NETLINK_IP6_FW"
2290+
| "NETLINK_DNRTMSG"
2291+
| "NETLINK_KOBJECT_UEVENT"
2292+
| "NETLINK_GENERIC"
2293+
| "NLMSG_ALIGNTO"
2294+
| "CTRL_CMD_UNSPEC"
2295+
| "CTRL_CMD_NEWFAMILY"
2296+
| "CTRL_CMD_DELFAMILY"
2297+
| "CTRL_CMD_GETFAMILY"
2298+
| "CTRL_CMD_NEWOPS"
2299+
| "CTRL_CMD_DELOPS"
2300+
| "CTRL_CMD_GETOPS"
2301+
| "CTRL_CMD_NEWMCAST_GRP"
2302+
| "CTRL_CMD_DELMCAST_GRP"
2303+
| "CTRL_CMD_GETMCAST_GRP"
2304+
| "CTRL_CMD_GETPOLICY"
2305+
| "CTRL_ATTR_UNSPEC"
2306+
| "CTRL_ATTR_FAMILY_ID"
2307+
| "CTRL_ATTR_FAMILY_NAME"
2308+
| "CTRL_ATTR_VERSION"
2309+
| "CTRL_ATTR_HDRSIZE"
2310+
| "CTRL_ATTR_MAXATTR"
2311+
| "CTRL_ATTR_OPS"
2312+
| "CTRL_ATTR_MCAST_GROUPS"
2313+
| "CTRL_ATTR_POLICY"
2314+
| "CTRL_ATTR_OP_POLICY"
2315+
| "CTRL_ATTR_OP"
2316+
| "CTRL_ATTR_MCAST_GRP_UNSPEC"
2317+
| "CTRL_ATTR_MCAST_GRP_NAME"
2318+
| "CTRL_ATTR_MCAST_GRP_ID"
2319+
if Some(13) > freebsd_ver =>
2320+
{
2321+
true
2322+
}
2323+
2324+
"NFULA_PACKET_HDR"
2325+
| "NFULA_MARK"
2326+
| "NFULA_TIMESTAMP"
2327+
| "NFULA_IFINDEX_INDEV"
2328+
| "NFULA_IFINDEX_OUTDEV"
2329+
| "NFULA_IFINDEX_PHYSINDEV"
2330+
| "NFULA_IFINDEX_PHYSOUTDEV"
2331+
| "NFULA_HWADDR"
2332+
| "NFULA_PAYLOAD"
2333+
| "NFULA_PREFIX"
2334+
| "NFULA_UID"
2335+
| "NFULA_SEQ"
2336+
| "NFULA_SEQ_GLOBAL"
2337+
| "NFULA_GID"
2338+
| "NFULA_HWTYPE"
2339+
| "NFULA_HWHEADER"
2340+
| "NFULA_HWLEN"
2341+
if Some(12) > freebsd_ver =>
2342+
{
2343+
true
2344+
}
2345+
22312346
// Added in FreeBSD 14
22322347
"SPACECTL_DEALLOC" if Some(14) > freebsd_ver => true,
22332348

@@ -2411,6 +2526,9 @@ fn test_freebsd(target: &str) {
24112526
// `shm_largepage_conf` was introduced in FreeBSD 13.
24122527
"shm_largepage_conf" if Some(13) > freebsd_ver => true,
24132528

2529+
// `sockaddr_nl` introduced in FreeBSD 13.2
2530+
"sockaddr_nl" if Some(13) > freebsd_ver => true,
2531+
24142532
// Those are private types
24152533
"memory_type" => true,
24162534
"memory_type_list" => true,

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

+153
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 &&
@@ -3029,6 +3065,123 @@ pub const SO_TS_MONOTONIC: ::c_int = 3;
30293065
pub const SO_TS_DEFAULT: ::c_int = SO_TS_REALTIME_MICRO;
30303066
pub const SO_TS_CLOCK_MAX: ::c_int = SO_TS_MONOTONIC;
30313067

3068+
/// netlink constants
3069+
3070+
// sys/socket.h
3071+
pub const AF_NETLINK: ::c_int = 38;
3072+
pub const PF_NETLINK: ::c_int = AF_NETLINK;
3073+
3074+
// netlink/netlink.h
3075+
pub const SOL_NETLINK: ::c_int = 270;
3076+
pub const NETLINK_ADD_MEMBERSHIP: ::c_int = 1;
3077+
pub const NETLINK_DROP_MEMBERSHIP: ::c_int = 2;
3078+
pub const NETLINK_PKTINFO: ::c_int = 3;
3079+
pub const NETLINK_BROADCAST_ERROR: ::c_int = 4;
3080+
pub const NETLINK_NO_ENOBUFS: ::c_int = 5;
3081+
pub const NETLINK_RX_RING: ::c_int = 6;
3082+
pub const NETLINK_TX_RING: ::c_int = 7;
3083+
pub const NETLINK_LISTEN_ALL_NSID: ::c_int = 8;
3084+
pub const NETLINK_LIST_MEMBERSHIPS: ::c_int = 9;
3085+
pub const NETLINK_CAP_ACK: ::c_int = 10;
3086+
pub const NETLINK_EXT_ACK: ::c_int = 11;
3087+
pub const NETLINK_GET_STRICT_CHK: ::c_int = 12;
3088+
//
3089+
pub const NLM_F_REQUEST: ::c_int = 0x01;
3090+
pub const NLM_F_MULTI: ::c_int = 0x02;
3091+
pub const NLM_F_ACK: ::c_int = 0x04;
3092+
pub const NLM_F_ECHO: ::c_int = 0x08;
3093+
pub const NLM_F_DUMP_INTR: ::c_int = 0x10;
3094+
pub const NLM_F_DUMP_FILTERED: ::c_int = 0x20;
3095+
//
3096+
pub const NLM_F_ROOT: ::c_int = 0x100;
3097+
pub const NLM_F_MATCH: ::c_int = 0x200;
3098+
pub const NLM_F_ATOMIC: ::c_int = 0x400;
3099+
pub const NLM_F_DUMP: ::c_int = NLM_F_ROOT | NLM_F_MATCH;
3100+
//
3101+
pub const NLM_F_REPLACE: ::c_int = 0x100;
3102+
pub const NLM_F_EXCL: ::c_int = 0x200;
3103+
pub const NLM_F_CREATE: ::c_int = 0x400;
3104+
pub const NLM_F_APPEND: ::c_int = 0x800;
3105+
//
3106+
pub const NLM_F_NONREC: ::c_int = 0x100;
3107+
//
3108+
pub const NLM_F_CAPPED: ::c_int = 0x100;
3109+
pub const NLM_F_ACK_TLVS: ::c_int = 0x200;
3110+
//
3111+
pub const NLMSG_NOOP: ::c_int = 0x1;
3112+
pub const NLMSG_ERROR: ::c_int = 0x2;
3113+
pub const NLMSG_DONE: ::c_int = 0x3;
3114+
pub const NLMSG_OVERRUN: ::c_int = 0x4;
3115+
//
3116+
pub const NETLINK_ROUTE: ::c_int = 0;
3117+
pub const NETLINK_UNUSED: ::c_int = 1;
3118+
pub const NETLINK_USERSOCK: ::c_int = 2;
3119+
pub const NETLINK_FIREWALL: ::c_int = 3;
3120+
pub const NETLINK_SOCK_DIAG: ::c_int = 4;
3121+
pub const NETLINK_NFLOG: ::c_int = 5;
3122+
pub const NETLINK_XFRM: ::c_int = 6;
3123+
pub const NETLINK_SELINUX: ::c_int = 7;
3124+
pub const NETLINK_ISCSI: ::c_int = 8;
3125+
pub const NETLINK_AUDIT: ::c_int = 9;
3126+
pub const NETLINK_FIB_LOOKUP: ::c_int = 10;
3127+
pub const NETLINK_CONNECTOR: ::c_int = 11;
3128+
pub const NETLINK_NETFILTER: ::c_int = 12;
3129+
pub const NETLINK_IP6_FW: ::c_int = 13;
3130+
pub const NETLINK_DNRTMSG: ::c_int = 14;
3131+
pub const NETLINK_KOBJECT_UEVENT: ::c_int = 15;
3132+
pub const NETLINK_GENERIC: ::c_int = 16;
3133+
//
3134+
const NL_ITEM_ALIGN_SIZE: ::c_int = 4; // mem::size_of::<u32>(); FIXME accept new dep?
3135+
pub const NLMSG_ALIGNTO: ::c_int = NL_ITEM_ALIGN_SIZE;
3136+
3137+
// netlink/netlink_generic.h
3138+
pub const CTRL_CMD_UNSPEC: ::c_int = 0;
3139+
pub const CTRL_CMD_NEWFAMILY: ::c_int = 1;
3140+
pub const CTRL_CMD_DELFAMILY: ::c_int = 2;
3141+
pub const CTRL_CMD_GETFAMILY: ::c_int = 3;
3142+
pub const CTRL_CMD_NEWOPS: ::c_int = 4;
3143+
pub const CTRL_CMD_DELOPS: ::c_int = 5;
3144+
pub const CTRL_CMD_GETOPS: ::c_int = 6;
3145+
pub const CTRL_CMD_NEWMCAST_GRP: ::c_int = 7;
3146+
pub const CTRL_CMD_DELMCAST_GRP: ::c_int = 8;
3147+
pub const CTRL_CMD_GETMCAST_GRP: ::c_int = 9;
3148+
pub const CTRL_CMD_GETPOLICY: ::c_int = 10;
3149+
//
3150+
pub const CTRL_ATTR_UNSPEC: ::c_int = 0;
3151+
pub const CTRL_ATTR_FAMILY_ID: ::c_int = 1;
3152+
pub const CTRL_ATTR_FAMILY_NAME: ::c_int = 2;
3153+
pub const CTRL_ATTR_VERSION: ::c_int = 3;
3154+
pub const CTRL_ATTR_HDRSIZE: ::c_int = 4;
3155+
pub const CTRL_ATTR_MAXATTR: ::c_int = 5;
3156+
pub const CTRL_ATTR_OPS: ::c_int = 6;
3157+
pub const CTRL_ATTR_MCAST_GROUPS: ::c_int = 7;
3158+
pub const CTRL_ATTR_POLICY: ::c_int = 8;
3159+
pub const CTRL_ATTR_OP_POLICY: ::c_int = 9;
3160+
pub const CTRL_ATTR_OP: ::c_int = 10;
3161+
//
3162+
pub const CTRL_ATTR_MCAST_GRP_UNSPEC: ::c_int = 0;
3163+
pub const CTRL_ATTR_MCAST_GRP_NAME: ::c_int = 1;
3164+
pub const CTRL_ATTR_MCAST_GRP_ID: ::c_int = 2;
3165+
3166+
// pcap/nflog.h
3167+
pub const NFULA_PACKET_HDR: ::c_int = 1;
3168+
pub const NFULA_MARK: ::c_int = 2;
3169+
pub const NFULA_TIMESTAMP: ::c_int = 3;
3170+
pub const NFULA_IFINDEX_INDEV: ::c_int = 4;
3171+
pub const NFULA_IFINDEX_OUTDEV: ::c_int = 5;
3172+
pub const NFULA_IFINDEX_PHYSINDEV: ::c_int = 6;
3173+
pub const NFULA_IFINDEX_PHYSOUTDEV: ::c_int = 7;
3174+
pub const NFULA_HWADDR: ::c_int = 8;
3175+
pub const NFULA_PAYLOAD: ::c_int = 9;
3176+
pub const NFULA_PREFIX: ::c_int = 10;
3177+
pub const NFULA_UID: ::c_int = 11;
3178+
pub const NFULA_SEQ: ::c_int = 12;
3179+
pub const NFULA_SEQ_GLOBAL: ::c_int = 13;
3180+
pub const NFULA_GID: ::c_int = 14;
3181+
pub const NFULA_HWTYPE: ::c_int = 15;
3182+
pub const NFULA_HWHEADER: ::c_int = 16;
3183+
pub const NFULA_HWLEN: ::c_int = 17;
3184+
30323185
pub const LOCAL_CREDS: ::c_int = 2;
30333186
pub const LOCAL_CREDS_PERSISTENT: ::c_int = 3;
30343187
pub const LOCAL_CONNWAIT: ::c_int = 4;

0 commit comments

Comments
 (0)