Skip to content

Commit

Permalink
Merge pull request #301 from hermit-os/dependabot/cargo/netlink-28182…
Browse files Browse the repository at this point in the history
…59a68

Bump the netlink group with 2 updates
  • Loading branch information
mkroening authored Jan 2, 2024
2 parents 8b6e14e + 6e346fe commit dccb498
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
34 changes: 8 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ goblin = { version = "0.8", default-features = false, features = ["elf64", "elf3
libc = "0.2"
log = { version = "0.4", features = ["std"] }
netlink-packet-core = "0.7"
netlink-packet-route = "0.17"
netlink-packet-route = "0.18"
nix = { version = "0.27", features = [
"hostname",
"ioctl",
Expand All @@ -38,7 +38,7 @@ nix = { version = "0.27", features = [
oci-spec = "0.6"
path-clean = "1.0"
procfs = { version = "0.15", default-features = false }
rtnetlink = "0.13"
rtnetlink = "0.14"
serde_json = "1.0"
serde = "1.0"
time = { version = "0.3", features = ["formatting"] }
Expand Down
30 changes: 21 additions & 9 deletions src/network.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use futures::TryStreamExt;
use netlink_packet_core::ErrorMessage;
use netlink_packet_route::{address, link, route, MACVLAN_MODE_PASSTHRU};
use netlink_packet_route::address::AddressAttribute;
use netlink_packet_route::link::LinkAttribute;
use netlink_packet_route::route::{RouteAddress, RouteAttribute};
use nix::sys::stat::SFlag;
use rtnetlink::Error::NetlinkError;
use std::net::IpAddr;
use std::num::NonZeroI32;
use std::path::PathBuf;
use std::{error::Error, fmt, net::Ipv4Addr};

// FIXME: https://github.com/rust-netlink/netlink-packet-route/issues/88
const MACVLAN_MODE_PASSTHRU: u32 = 8;

#[derive(Debug)]
struct VirtioNetworkError {
details: String,
Expand Down Expand Up @@ -117,19 +123,25 @@ pub async fn create_tap() -> Result<VirtioNetworkConfig, Box<dyn std::error::Err
.expect("Could not read address info for interface eth0!");

// Extract IP address from address info
for nla in device_addr_msg.nlas.iter() {
if let address::nlas::Nla::Address(addr) = nla {
ip_address = Some(Ipv4Addr::from([addr[0], addr[1], addr[2], addr[3]]));
for address_attribute in device_addr_msg.attributes.into_iter() {
if let AddressAttribute::Address(addr) = address_attribute {
match addr {
IpAddr::V4(addr) => ip_address = Some(addr),
IpAddr::V6(_) => panic!(),
}
prefix_length = Some(device_addr_msg.header.prefix_len);
}
}

// Get route info and extract gateway address
let mut route_get_req = handle.route().get(rtnetlink::IpVersion::V4).execute();
while let Some(route_msg) = route_get_req.try_next().await? {
for nla in route_msg.nlas.into_iter() {
if let route::Nla::Gateway(addr) = nla {
gateway_address = Some(Ipv4Addr::from([addr[0], addr[1], addr[2], addr[3]]));
for route_attribute in route_msg.attributes.into_iter() {
if let RouteAttribute::Gateway(addr) = route_attribute {
match addr {
RouteAddress::Inet(addr) => gateway_address = Some(addr),
_ => panic!(),
}
break;
}
}
Expand Down Expand Up @@ -158,8 +170,8 @@ pub async fn create_tap() -> Result<VirtioNetworkConfig, Box<dyn std::error::Err
let macvtap_index = macvtap_link_info.header.index;

// Extract mac from macvtap
for nla in macvtap_link_info.nlas.into_iter() {
if let link::nlas::Nla::Address(addr) = nla {
for link_attribute in macvtap_link_info.attributes.into_iter() {
if let LinkAttribute::Address(addr) = link_attribute {
if addr.len() != 6 {
return Err(Box::new(VirtioNetworkError::from(format!(
"Received invalid MAC address {addr:?} for macvtap device!"
Expand Down

0 comments on commit dccb498

Please sign in to comment.