Update netlink-packet-route to 0.21 (#324)

Fixes: https://github.com/tonarino/innernet/issues/303
main
Florian Badie 2024-10-24 09:42:39 +02:00 committed by GitHub
parent 9578a15cae
commit b2e414984e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 78 additions and 77 deletions

7
Cargo.lock generated
View File

@ -862,14 +862,15 @@ dependencies = [
[[package]] [[package]]
name = "netlink-packet-route" name = "netlink-packet-route"
version = "0.17.1" version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "053998cea5a306971f88580d0829e90f270f940befd7cf928da179d4187a5a66" checksum = "483325d4bfef65699214858f097d504eb812c38ce7077d165f301ec406c3066e"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bitflags 1.3.2", "bitflags 2.5.0",
"byteorder", "byteorder",
"libc", "libc",
"log",
"netlink-packet-core", "netlink-packet-core",
"netlink-packet-utils", "netlink-packet-utils",
] ]

View File

@ -14,7 +14,7 @@ version = "1.6.1"
netlink-sys = "0.8.5" netlink-sys = "0.8.5"
netlink-packet-core = "0.7" netlink-packet-core = "0.7"
netlink-packet-generic = "0.3.3" netlink-packet-generic = "0.3.3"
netlink-packet-route = "0.17" netlink-packet-route = "0.21"
netlink-packet-utils = "0.5.2" netlink-packet-utils = "0.5.2"
nix = { version = "0.25", features = ["feature"] } nix = { version = "0.25", features = ["feature"] }
once_cell = "1" once_cell = "1"

View File

@ -9,7 +9,7 @@ mod linux {
ctrl::{nlas::GenlCtrlAttrs, GenlCtrl, GenlCtrlCmd}, ctrl::{nlas::GenlCtrlAttrs, GenlCtrl, GenlCtrlCmd},
GenlFamily, GenlHeader, GenlMessage, GenlFamily, GenlHeader, GenlMessage,
}; };
use netlink_packet_route::RtnlMessage; use netlink_packet_route::RouteNetlinkMessage;
use netlink_packet_utils::{Emitable, ParseableParametrized}; use netlink_packet_utils::{Emitable, ParseableParametrized};
use netlink_sys::{constants::NETLINK_GENERIC, protocols::NETLINK_ROUTE, Socket}; use netlink_sys::{constants::NETLINK_GENERIC, protocols::NETLINK_ROUTE, Socket};
use nix::unistd::{sysconf, SysconfVar}; use nix::unistd::{sysconf, SysconfVar};
@ -86,9 +86,9 @@ mod linux {
} }
pub fn netlink_request_rtnl( pub fn netlink_request_rtnl(
message: RtnlMessage, message: RouteNetlinkMessage,
flags: Option<u16>, flags: Option<u16>,
) -> Result<Vec<NetlinkMessage<RtnlMessage>>, io::Error> { ) -> Result<Vec<NetlinkMessage<RouteNetlinkMessage>>, io::Error> {
netlink_request(message, flags, NETLINK_ROUTE) netlink_request(message, flags, NETLINK_ROUTE)
} }

View File

@ -31,7 +31,7 @@ wireguard-control = { path = "../wireguard-control" }
[target.'cfg(target_os = "linux")'.dependencies] [target.'cfg(target_os = "linux")'.dependencies]
netlink-packet-core = "0.7" netlink-packet-core = "0.7"
netlink-packet-route = "0.17" netlink-packet-route = "0.21"
netlink-request = { path = "../netlink-request" } netlink-request = { path = "../netlink-request" }
netlink-sys = "0.8.5" netlink-sys = "0.8.5"

View File

@ -4,11 +4,10 @@ use netlink_packet_core::{
NLM_F_REQUEST, NLM_F_REQUEST,
}; };
use netlink_packet_route::{ use netlink_packet_route::{
address, address::{self, AddressHeader, AddressMessage},
constants::*, link::{self, LinkFlags, LinkHeader, LinkMessage, State},
link::{self, nlas::State}, route::{self, RouteHeader, RouteMessage},
route, AddressHeader, AddressMessage, LinkHeader, LinkMessage, RouteHeader, RouteMessage, AddressFamily, RouteNetlinkMessage,
RtnlMessage, RTN_UNICAST, RT_SCOPE_LINK, RT_TABLE_MAIN,
}; };
use netlink_request::netlink_request_rtnl; use netlink_request::netlink_request_rtnl;
use std::{io, net::IpAddr}; use std::{io, net::IpAddr};
@ -28,13 +27,13 @@ pub fn set_up(interface: &InterfaceName, mtu: u32) -> Result<(), io::Error> {
let index = if_nametoindex(interface)?; let index = if_nametoindex(interface)?;
let header = LinkHeader { let header = LinkHeader {
index, index,
flags: IFF_UP, flags: LinkFlags::Up,
..Default::default() ..Default::default()
}; };
let mut message = LinkMessage::default(); let mut message = LinkMessage::default();
message.header = header; message.header = header;
message.nlas = vec![link::nlas::Nla::Mtu(mtu)]; message.attributes = vec![link::LinkAttribute::Mtu(mtu)];
netlink_request_rtnl(RtnlMessage::SetLink(message), None)?; netlink_request_rtnl(RouteNetlinkMessage::SetLink(message), None)?;
log::debug!("set interface {} up with mtu {}", interface, mtu); log::debug!("set interface {} up with mtu {}", interface, mtu);
Ok(()) Ok(())
} }
@ -43,33 +42,35 @@ pub fn set_addr(interface: &InterfaceName, addr: IpNet) -> Result<(), io::Error>
let index = if_nametoindex(interface)?; let index = if_nametoindex(interface)?;
let (family, nlas) = match addr { let (family, nlas) = match addr {
IpNet::V4(network) => { IpNet::V4(network) => {
let addr_bytes = network.addr().octets().to_vec(); let addr = IpAddr::V4(network.addr());
( (
AF_INET as u8, AddressFamily::Inet,
vec![ vec![
address::Nla::Local(addr_bytes.clone()), address::AddressAttribute::Local(addr),
address::Nla::Address(addr_bytes), address::AddressAttribute::Address(addr),
], ],
) )
}, },
IpNet::V6(network) => ( IpNet::V6(network) => (
AF_INET6 as u8, AddressFamily::Inet6,
vec![address::Nla::Address(network.addr().octets().to_vec())], vec![address::AddressAttribute::Address(IpAddr::V6(
network.addr(),
))],
), ),
}; };
let header = AddressHeader { let header = AddressHeader {
index, index,
family, family,
prefix_len: addr.prefix_len(), prefix_len: addr.prefix_len(),
scope: RT_SCOPE_UNIVERSE, scope: address::AddressScope::Universe,
..Default::default() ..Default::default()
}; };
let mut message = AddressMessage::default(); let mut message = AddressMessage::default();
message.header = header; message.header = header;
message.nlas = nlas; message.attributes = nlas;
netlink_request_rtnl( netlink_request_rtnl(
RtnlMessage::NewAddress(message), RouteNetlinkMessage::NewAddress(message),
Some(NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE | NLM_F_CREATE), Some(NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE | NLM_F_CREATE),
)?; )?;
log::debug!("set address {} on interface {}", addr, interface); log::debug!("set address {} on interface {}", addr, interface);
@ -79,23 +80,29 @@ pub fn set_addr(interface: &InterfaceName, addr: IpNet) -> Result<(), io::Error>
pub fn add_route(interface: &InterfaceName, cidr: IpNet) -> Result<bool, io::Error> { pub fn add_route(interface: &InterfaceName, cidr: IpNet) -> Result<bool, io::Error> {
let if_index = if_nametoindex(interface)?; let if_index = if_nametoindex(interface)?;
let (address_family, dst) = match cidr { let (address_family, dst) = match cidr {
IpNet::V4(network) => (AF_INET as u8, network.network().octets().to_vec()), IpNet::V4(network) => (
IpNet::V6(network) => (AF_INET6 as u8, network.network().octets().to_vec()), AddressFamily::Inet,
route::RouteAttribute::Destination(route::RouteAddress::Inet(network.network())),
),
IpNet::V6(network) => (
AddressFamily::Inet6,
route::RouteAttribute::Destination(route::RouteAddress::Inet6(network.network())),
),
}; };
let header = RouteHeader { let header = RouteHeader {
table: RT_TABLE_MAIN, table: RouteHeader::RT_TABLE_MAIN,
protocol: RTPROT_BOOT, protocol: route::RouteProtocol::Boot,
scope: RT_SCOPE_LINK, scope: route::RouteScope::Link,
kind: RTN_UNICAST, kind: route::RouteType::Unicast,
destination_prefix_length: cidr.prefix_len(), destination_prefix_length: cidr.prefix_len(),
address_family, address_family,
..Default::default() ..Default::default()
}; };
let mut message = RouteMessage::default(); let mut message = RouteMessage::default();
message.header = header; message.header = header;
message.nlas = vec![route::Nla::Destination(dst), route::Nla::Oif(if_index)]; message.attributes = vec![dst, route::RouteAttribute::Oif(if_index)];
match netlink_request_rtnl(RtnlMessage::NewRoute(message), None) { match netlink_request_rtnl(RouteNetlinkMessage::NewRoute(message), None) {
Ok(_) => { Ok(_) => {
log::debug!("added route {} to interface {}", cidr, interface); log::debug!("added route {} to interface {}", cidr, interface);
Ok(true) Ok(true)
@ -110,7 +117,7 @@ pub fn add_route(interface: &InterfaceName, cidr: IpNet) -> Result<bool, io::Err
fn get_links() -> Result<Vec<String>, io::Error> { fn get_links() -> Result<Vec<String>, io::Error> {
let link_responses = netlink_request_rtnl( let link_responses = netlink_request_rtnl(
RtnlMessage::GetLink(LinkMessage::default()), RouteNetlinkMessage::GetLink(LinkMessage::default()),
Some(NLM_F_DUMP | NLM_F_REQUEST), Some(NLM_F_DUMP | NLM_F_REQUEST),
)?; )?;
let links = link_responses let links = link_responses
@ -118,21 +125,21 @@ fn get_links() -> Result<Vec<String>, io::Error> {
// Filter out non-link messages // Filter out non-link messages
.filter_map(|response| match response { .filter_map(|response| match response {
NetlinkMessage { NetlinkMessage {
payload: NetlinkPayload::InnerMessage(RtnlMessage::NewLink(link)), payload: NetlinkPayload::InnerMessage(RouteNetlinkMessage::NewLink(link)),
.. ..
} => Some(link), } => Some(link),
_ => None, _ => None,
}) })
// Filter out loopback links // Filter out loopback links
.filter_map(|link| if link.header.flags & IFF_LOOPBACK == 0 { .filter_map(|link| if link.header.flags.contains(LinkFlags::Loopback) {
Some(link.nlas) Some(link.attributes)
} else { } else {
None None
}) })
// Find and filter out addresses for interfaces // Find and filter out addresses for interfaces
.filter(|nlas| nlas.iter().any(|nla| nla == &link::nlas::Nla::OperState(State::Up))) .filter(|nlas| nlas.iter().any(|nla| nla == &link::LinkAttribute::OperState(State::Up)))
.filter_map(|nlas| nlas.iter().find_map(|nla| match nla { .filter_map(|nlas| nlas.iter().find_map(|nla| match nla {
link::nlas::Nla::IfName(name) => Some(name.clone()), link::LinkAttribute::IfName(name) => Some(name.clone()),
_ => None, _ => None,
})) }))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -143,7 +150,7 @@ fn get_links() -> Result<Vec<String>, io::Error> {
pub fn get_local_addrs() -> Result<impl Iterator<Item = IpAddr>, io::Error> { pub fn get_local_addrs() -> Result<impl Iterator<Item = IpAddr>, io::Error> {
let links = get_links()?; let links = get_links()?;
let addr_responses = netlink_request_rtnl( let addr_responses = netlink_request_rtnl(
RtnlMessage::GetAddress(AddressMessage::default()), RouteNetlinkMessage::GetAddress(AddressMessage::default()),
Some(NLM_F_DUMP | NLM_F_REQUEST), Some(NLM_F_DUMP | NLM_F_REQUEST),
)?; )?;
let addrs = addr_responses let addrs = addr_responses
@ -151,33 +158,25 @@ pub fn get_local_addrs() -> Result<impl Iterator<Item = IpAddr>, io::Error> {
// Filter out non-link messages // Filter out non-link messages
.filter_map(|response| match response { .filter_map(|response| match response {
NetlinkMessage { NetlinkMessage {
payload: NetlinkPayload::InnerMessage(RtnlMessage::NewAddress(addr)), payload: NetlinkPayload::InnerMessage(RouteNetlinkMessage::NewAddress(addr)),
.. ..
} => Some(addr), } => Some(addr),
_ => None, _ => None,
}) })
// Filter out non-global-scoped addresses // Filter out non-global-scoped addresses
.filter_map(|link| if link.header.scope == RT_SCOPE_UNIVERSE { .filter_map(|link| if link.header.scope == address::AddressScope::Universe {
Some(link.nlas) Some(link.attributes)
} else { } else {
None None
}) })
// Only select addresses for helpful links // Only select addresses for helpful links
.filter(move |nlas| nlas.iter().any(|nla| { .filter(move |nlas| nlas.iter().any(|nla| {
matches!(nla, address::nlas::Nla::Label(label) if links.contains(label)) matches!(nla, address::AddressAttribute::Label(label) if links.contains(label))
|| matches!(nla, address::nlas::Nla::Address(name) if name.len() == 16) || matches!(nla, address::AddressAttribute::Address(IpAddr::V6(_addr)))
})) }))
.filter_map(|nlas| nlas.iter().find_map(|nla| match nla { .filter_map(|nlas| nlas.iter().find_map(|nla| match nla {
address::nlas::Nla::Address(name) if name.len() == 4 => { address::AddressAttribute::Address(IpAddr::V4(addr)) => Some(IpAddr::V4(*addr)),
let mut addr = [0u8; 4]; address::AddressAttribute::Address(IpAddr::V6(addr)) => Some(IpAddr::V6(*addr)),
addr.copy_from_slice(name);
Some(IpAddr::V4(addr.into()))
},
address::nlas::Nla::Address(name) if name.len() == 16 => {
let mut addr = [0u8; 16];
addr.copy_from_slice(name);
Some(IpAddr::V6(addr.into()))
},
_ => None, _ => None,
})); }));
Ok(addrs) Ok(addrs)

View File

@ -21,7 +21,7 @@ x25519-dalek = { version = "2.0.1", features = ["static_secrets"] }
[target.'cfg(target_os = "linux")'.dependencies] [target.'cfg(target_os = "linux")'.dependencies]
netlink-packet-core = "0.7" netlink-packet-core = "0.7"
netlink-packet-generic = "0.3.3" netlink-packet-generic = "0.3.3"
netlink-packet-route = "0.17" netlink-packet-route = "0.21"
netlink-packet-utils = "0.5.2" netlink-packet-utils = "0.5.2"
netlink-packet-wireguard = "0.2.3" netlink-packet-wireguard = "0.2.3"
netlink-request = { path = "../netlink-request", version = "1.6.1" } netlink-request = { path = "../netlink-request", version = "1.6.1" }

View File

@ -7,17 +7,16 @@ use netlink_packet_core::{
}; };
use netlink_packet_generic::GenlMessage; use netlink_packet_generic::GenlMessage;
use netlink_packet_route::{ use netlink_packet_route::{
constants::*, link::{self, InfoKind, LinkInfo, LinkMessage},
link::{ RouteNetlinkMessage,
self,
nlas::{Info, InfoKind},
},
LinkMessage, RtnlMessage,
}; };
use netlink_packet_utils::traits::Emitable; use netlink_packet_utils::traits::Emitable;
use netlink_packet_wireguard::{ use netlink_packet_wireguard::{
self, self,
constants::{WGDEVICE_F_REPLACE_PEERS, WGPEER_F_REMOVE_ME, WGPEER_F_REPLACE_ALLOWEDIPS}, constants::{
AF_INET, AF_INET6, WGDEVICE_F_REPLACE_PEERS, WGPEER_F_REMOVE_ME,
WGPEER_F_REPLACE_ALLOWEDIPS,
},
nlas::{WgAllowedIp, WgAllowedIpAttrs, WgDeviceAttrs, WgPeer, WgPeerAttrs}, nlas::{WgAllowedIp, WgAllowedIpAttrs, WgDeviceAttrs, WgPeer, WgPeerAttrs},
Wireguard, WireguardCmd, Wireguard, WireguardCmd,
}; };
@ -166,7 +165,7 @@ impl<'a> TryFrom<&'a [WgDeviceAttrs]> for Device {
pub fn enumerate() -> Result<Vec<InterfaceName>, io::Error> { pub fn enumerate() -> Result<Vec<InterfaceName>, io::Error> {
let link_responses = netlink_request_rtnl( let link_responses = netlink_request_rtnl(
RtnlMessage::GetLink(LinkMessage::default()), RouteNetlinkMessage::GetLink(LinkMessage::default()),
Some(NLM_F_DUMP | NLM_F_REQUEST), Some(NLM_F_DUMP | NLM_F_REQUEST),
)?; )?;
let links = link_responses let links = link_responses
@ -174,21 +173,21 @@ pub fn enumerate() -> Result<Vec<InterfaceName>, io::Error> {
// Filter out non-link messages // Filter out non-link messages
.filter_map(|response| match response { .filter_map(|response| match response {
NetlinkMessage { NetlinkMessage {
payload: NetlinkPayload::InnerMessage(RtnlMessage::NewLink(link)), payload: NetlinkPayload::InnerMessage(RouteNetlinkMessage::NewLink(link)),
.. ..
} => Some(link), } => Some(link),
_ => None, _ => None,
}) })
.filter(|link| { .filter(|link| {
for nla in link.nlas.iter() { for nla in link.attributes.iter() {
if let link::nlas::Nla::Info(infos) = nla { if let link::LinkAttribute::LinkInfo(infos) = nla {
return infos.iter().any(|info| info == &Info::Kind(InfoKind::Wireguard)) return infos.iter().any(|info| info == &LinkInfo::Kind(InfoKind::Wireguard))
} }
} }
false false
}) })
.filter_map(|link| link.nlas.iter().find_map(|nla| match nla { .filter_map(|link| link.attributes.iter().find_map(|nla| match nla {
link::nlas::Nla::IfName(name) => Some(name.clone()), link::LinkAttribute::IfName(name) => Some(name.clone()),
_ => None, _ => None,
})) }))
.filter_map(|name| name.parse().ok()) .filter_map(|name| name.parse().ok())
@ -199,17 +198,19 @@ pub fn enumerate() -> Result<Vec<InterfaceName>, io::Error> {
fn add_del(iface: &InterfaceName, add: bool) -> io::Result<()> { fn add_del(iface: &InterfaceName, add: bool) -> io::Result<()> {
let mut message = LinkMessage::default(); let mut message = LinkMessage::default();
message.attributes.push(link::LinkAttribute::IfName(
iface.as_str_lossy().to_string(),
));
message message
.nlas .attributes
.push(link::nlas::Nla::IfName(iface.as_str_lossy().to_string())); .push(link::LinkAttribute::LinkInfo(vec![LinkInfo::Kind(
message.nlas.push(link::nlas::Nla::Info(vec![Info::Kind( link::InfoKind::Wireguard,
link::nlas::InfoKind::Wireguard, )]));
)]));
let extra_flags = if add { NLM_F_CREATE | NLM_F_EXCL } else { 0 }; let extra_flags = if add { NLM_F_CREATE | NLM_F_EXCL } else { 0 };
let rtnl_message = if add { let rtnl_message = if add {
RtnlMessage::NewLink(message) RouteNetlinkMessage::NewLink(message)
} else { } else {
RtnlMessage::DelLink(message) RouteNetlinkMessage::DelLink(message)
}; };
match netlink_request_rtnl(rtnl_message, Some(NLM_F_REQUEST | NLM_F_ACK | extra_flags)) { match netlink_request_rtnl(rtnl_message, Some(NLM_F_REQUEST | NLM_F_ACK | extra_flags)) {
Err(e) if e.kind() != io::ErrorKind::AlreadyExists => Err(e), Err(e) if e.kind() != io::ErrorKind::AlreadyExists => Err(e),