From edbdfc1d4ba9cb7f83dd322db3d500bbbbb190b3 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Tue, 21 May 2024 13:24:48 +0200 Subject: [PATCH] add netbsd, improve freebsd --- iroh-net/src/net/interfaces/bsd.rs | 5 + iroh-net/src/net/interfaces/bsd/freebsd.rs | 63 ++++++----- iroh-net/src/net/interfaces/bsd/netbsd.rs | 115 +++++++++++++++++++++ 3 files changed, 159 insertions(+), 24 deletions(-) create mode 100644 iroh-net/src/net/interfaces/bsd/netbsd.rs diff --git a/iroh-net/src/net/interfaces/bsd.rs b/iroh-net/src/net/interfaces/bsd.rs index cdd62db056c..29bfb11df68 100644 --- a/iroh-net/src/net/interfaces/bsd.rs +++ b/iroh-net/src/net/interfaces/bsd.rs @@ -21,6 +21,10 @@ use super::DefaultRouteDetails; mod freebsd; #[cfg(target_os = "freebsd")] use self::freebsd::*; +#[cfg(target_os = "netbsd")] +mod netbsd; +#[cfg(target_os = "netbsd")] +use self::netbsd::*; #[cfg(any(target_os = "macos", target_os = "ios"))] mod macos; @@ -385,6 +389,7 @@ enum MessageType { Interface, InterfaceAddr, InterfaceMulticastAddr, + InterfaceAnnounce, } static ROUTING_STACK: Lazy = Lazy::new(probe_routing_stack); diff --git a/iroh-net/src/net/interfaces/bsd/freebsd.rs b/iroh-net/src/net/interfaces/bsd/freebsd.rs index d4705699fa0..ec6352ce6ec 100644 --- a/iroh-net/src/net/interfaces/bsd/freebsd.rs +++ b/iroh-net/src/net/interfaces/bsd/freebsd.rs @@ -260,33 +260,48 @@ mod arm64 { pub(super) const SIZEOF_SOCKADDR_INET6: usize = 0x1c; } +/// 386 emulation on amd64 +fn detect_compat_freebsd32() -> bool { + // TODO: implement detection when someone actually needs it + false +} + pub(super) fn probe_routing_stack() -> RoutingStack { let rtm_version = RTM_VERSION; - let rtm = WireFormat { - ext_off: SIZEOF_RT_MSGHDR_FREE_BSD10, - body_off: SIZEOF_RT_MSGHDR_FREE_BSD10, - typ: MessageType::Route, - }; - let ifm = WireFormat { - ext_off: SIZEOF_IF_MSGHDR_FREE_BSD11, - body_off: SIZEOF_IF_MSGHDR_FREE_BSD11, - typ: MessageType::Interface, - }; - let ifam = WireFormat { - ext_off: SIZEOF_IFA_MSGHDR_FREE_BSD10, - body_off: SIZEOF_IFA_MSGHDR_FREE_BSD10, - typ: MessageType::InterfaceAddr, - }; - let ifmam = WireFormat { - ext_off: SIZEOF_IFMA_MSGHDR_FREE_BSD10, - body_off: SIZEOF_IFMA_MSGHDR_FREE_BSD10, - typ: MessageType::InterfaceMulticastAddr, - }; - let ifannm = WireFormat { - ext_off: SIZEOF_IF_ANNOUNCEMSGHDR_FREE_BSD10, - body_off: SIZEOF_IF_ANNOUNCEMSGHDR_FREE_BSD10, - typ: MessageType::Interface, + // Currently only BSD11 support is implemented. + // At the time of this writing rust supports 10 and 11, if this is a problem + // please file an issue. + + let (rtm, ifm, ifam, ifmam, ifanm) = if detect_compat_freebsd32() { + unimplemented!() + } else { + let rtm = WireFormat { + ext_off: SIZEOF_RT_MSGHDR_FREE_BSD10 - SIZEOF_RT_METRICS_FREE_BSD10, + body_off: SIZEOF_RT_MSGHDR_FREE_BSD10, + typ: MessageType::Route, + }; + let ifm = WireFormat { + ext_off: 16, + body_off: SIZEOF_IF_MSGHDR_FREE_BSD11, + typ: MessageType::Interface, + }; + let ifam = WireFormat { + ext_off: SIZEOF_IFA_MSGHDR_FREE_BSD10, + body_off: SIZEOF_IFA_MSGHDR_FREE_BSD10, + typ: MessageType::InterfaceAddr, + }; + let ifmam = WireFormat { + ext_off: SIZEOF_IFMA_MSGHDR_FREE_BSD10, + body_off: SIZEOF_IFMA_MSGHDR_FREE_BSD10, + typ: MessageType::InterfaceMulticastAddr, + }; + let ifannm = WireFormat { + ext_off: SIZEOF_IF_ANNOUNCEMSGHDR_FREE_BSD10, + body_off: SIZEOF_IF_ANNOUNCEMSGHDR_FREE_BSD10, + typ: MessageType::InterfaceAnnounce, + }; + (rtm, ifm, ifam, ifmam, ifanm) }; let wire_formats = [ diff --git a/iroh-net/src/net/interfaces/bsd/netbsd.rs b/iroh-net/src/net/interfaces/bsd/netbsd.rs new file mode 100644 index 00000000000..c520564051c --- /dev/null +++ b/iroh-net/src/net/interfaces/bsd/netbsd.rs @@ -0,0 +1,115 @@ +use super::{MessageType, RoutingStack, WireFormat}; + +use libc::c_int; + +// Missing constants from libc. +// https://github.com/rust-lang/libc/issues/3711 + +const LOCAL_PEERCRED: c_int = 1; + +// net/route.h +const RTF_GATEWAY: c_int = 0x2; +const RTAX_DST: c_int = 0; +const RTAX_GATEWAY: c_int = 1; +const RTAX_NETMASK: c_int = 2; +const RTAX_IFP: c_int = 4; +const RTAX_BRD: c_int = 7; +const RTAX_MAX: c_int = 8; +const RTM_VERSION: c_int = 5; +const RTA_DST: c_int = 0x1; +const RTA_GATEWAY: c_int = 0x2; +const RTA_NETMASK: c_int = 0x4; +const RTA_GENMASK: c_int = 0x8; +const RTA_IFP: c_int = 0x10; +const RTA_IFA: c_int = 0x20; +const RTA_AUTHOR: c_int = 0x40; +const RTA_BRD: c_int = 0x80; + +// Message types +const RTM_ADD: c_int = 0x1; +const RTM_DELETE: c_int = 0x2; +const RTM_CHANGE: c_int = 0x3; +const RTM_GET: c_int = 0x4; +const RTM_LOSING: c_int = 0x5; +const RTM_REDIRECT: c_int = 0x6; +const RTM_MISS: c_int = 0x7; +const RTM_LOCK: c_int = 0x8; +const RTM_OLDADD: c_int = 0x9; +const RTM_OLDDEL: c_int = 0xa; +const RTM_RESOLVE: c_int = 0xb; +const RTM_NEWADDR: c_int = 0xc; +const RTM_DELADDR: c_int = 0xd; +const RTM_IFINFO: c_int = 0xe; +const RTM_NEWMADDR: c_int = 0xf; +const RTM_DELMADDR: c_int = 0x10; +const RTM_IFANNOUNCE: c_int = 0x11; +const RTM_IEEE80211: c_int = 0x12; + +const SHUT_RD: c_int = 0; +const SHUT_WR: c_int = 1; +const SHUT_RDWR: c_int = 2; + + +// Hardcoded based on the generated values here: https://cs.opensource.google/go/x/net/+/master:route/zsys_netbsd.go + +pub(super) const SIZEOF_IF_MSGHDR_NET_BSD7: usize = 0x98; +pub(super) const SIZEOF_IFA_MSGHDR_NET_BSD7: usize = 0x18; +pub(super) const SIZEOF_IF_ANNOUNCEMSGHDR_NET_BSD7: usize = 0x18; + +pub(super) const SIZEOF_RT_MSGHDR_NET_BSD7: usize = 0x78; +pub(super) const SIZEOF_RT_METRICS_NET_BSD7: usize = 0x50; + +pub(super) const SIZEOF_SOCKADDR_STORAGE: usize = 0x80; +pub(super) const SIZEOF_SOCKADDR_INET: usize = 0x10; +pub(super) const SIZEOF_SOCKADDR_INET6: usize = 0x1c; + + +pub(super) fn probe_routing_stack() -> RoutingStack { + let rtm_version = RTM_VERSION; + + let rtm = WireFormat { + ext_off: 40, + body_off: SIZEOF_RT_MSGHDR_NET_BSD7, + typ: MessageType::Route, + }; + let ifm = WireFormat { + ext_off: 16, + body_off: SIZEOF_IF_MSGHDR_NET_BSD7, + typ: MessageType::Interface, + }; + let ifam = WireFormat { + ext_off: SIZEOF_IFA_MSGHDR_NET_BSD7, + body_off: SIZEOF_IFA_MSGHDR_NET_BSD7, + typ: MessageType::InterfaceAddr, + }; + let ifannm = WireFormat { + ext_off: SIZEOF_IF_ANNOUNCEMSGHDR_NET_BSD7, + body_off: SIZEOF_IF_ANNOUNCEMSGHDR_NET_BSD7, + typ: MessageType::InterfaceAnnounce, + }; + + let wire_formats = [ + (RTM_ADD, rtm), + (RTM_DELETE, rtm), + (RTM_CHANGE, rtm), + (RTM_GET, rtm), + (RTM_LOSING, rtm), + (RTM_REDIRECT, rtm), + (RTM_MISS, rtm), + (RTM_LOCK, rtm), + (RTM_RESOLVE, rtm), + (RTM_NEWADDR, ifam), + (RTM_DELADDR, ifam), + (RTM_IFANNOUNCE, ifannm), + (RTM_IFINFO, ifm), + ] + .into_iter() + .collect(); + + // NetBSD 6 and above kernels require 64-bit aligned access to routing facilities. + RoutingStack { + rtm_version, + wire_formats, + kernel_align: 8, + } +}