Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use netlink_packet_route::RouteNetlinkMessage;
use netlink_proto::{sys::SocketAddr, ConnectionHandle};

use crate::{
AddressHandle, Error, LinkHandle, NeighbourHandle, RouteHandle, RuleHandle,
AddressHandle, Error, LinkHandle, NeighbourHandle, NexthopHandle,
RouteHandle, RuleHandle,
};
#[cfg(not(target_os = "freebsd"))]
use crate::{
Expand Down Expand Up @@ -71,6 +72,12 @@ impl Handle {
NeighbourHandle::new(self.clone())
}

/// Create a new handle, specifically for nexthop requests
/// (equivalent to `ip nexthop` commands)
pub fn nexthop(&self) -> NexthopHandle {
NexthopHandle::new(self.clone())
}

/// Create a new handle, specifically for traffic control qdisc requests
/// (equivalent to `tc qdisc show` commands)
#[cfg(not(target_os = "freebsd"))]
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod link;
mod macros;
mod multicast;
mod neighbour;
mod nexthop;
#[cfg(not(target_os = "freebsd"))]
mod ns;
mod route;
Expand Down Expand Up @@ -60,6 +61,10 @@ pub use crate::{
NeighbourAddRequest, NeighbourDelRequest, NeighbourGetRequest,
NeighbourHandle,
},
nexthop::{
NexthopAddRequest, NexthopDelRequest, NexthopGetRequest, NexthopHandle,
NexthopMessageBuilder,
},
route::{
IpVersion, RouteAddRequest, RouteDelRequest, RouteGetRequest,
RouteHandle, RouteMessageBuilder, RouteNextHopBuilder,
Expand Down
4 changes: 3 additions & 1 deletion src/link/bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ impl LinkMessageBuilder<LinkBond> {
/// This is equivalent to `ip link add name NAME type bond ad_select
/// AD_SELECT`.
pub fn ad_select(self, ad_select: u8) -> Self {
self.append_info_data(InfoBond::AdSelect(ad_select))
self.append_info_data(InfoBond::AdSelect(
netlink_packet_route::link::BondAdSelect::Other(ad_select),
))
}

/// Adds the `ad_actor_sys_prio` attribute to the bond
Expand Down
2 changes: 1 addition & 1 deletion src/link/bridge_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl LinkMessageBuilder<LinkBridgePort> {
/// `ip link set name NAME type bridge_slave \
/// neigh_vlan_suppress { on | off }`.
pub fn neigh_vlan_suppress(self, v: bool) -> Self {
self.append_info_data(InfoBridgePort::NeighVlanSupress(v))
self.append_info_data(InfoBridgePort::NeighVlanSuppress(v))
}

/// This is equivalent to
Expand Down
59 changes: 59 additions & 0 deletions src/nexthop/add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: MIT

use crate::{try_nl, Error, Handle};
use futures_util::stream::StreamExt;
use netlink_packet_core::{
NetlinkMessage, NLM_F_ACK, NLM_F_CREATE, NLM_F_EXCL, NLM_F_REPLACE,
NLM_F_REQUEST,
};
use netlink_packet_route::{nexthop::NexthopMessage, RouteNetlinkMessage};

/// A request to create a new nexthop. This is equivalent to the `ip nexthop add`
/// commands.
#[derive(Debug)]
pub struct NexthopAddRequest {
handle: Handle,
message: NexthopMessage,
replace: bool,
}

impl NexthopAddRequest {
pub(crate) fn new(handle: Handle, message: NexthopMessage) -> Self {
NexthopAddRequest {
handle,
message,
replace: false,
}
}

/// Replace existing matching nexthop.
pub fn replace(self) -> Self {
Self {
replace: true,
..self
}
}

/// Execute the request.
pub async fn execute(self) -> Result<(), Error> {
let NexthopAddRequest {
mut handle, // Need mut for handle.request
message,
replace,
} = self;
let mut req =
NetlinkMessage::from(RouteNetlinkMessage::NewNexthop(message));
let replace = if replace { NLM_F_REPLACE } else { NLM_F_EXCL };
req.header.flags = NLM_F_REQUEST | NLM_F_ACK | replace | NLM_F_CREATE;

let mut response = handle.request(req)?;
while let Some(message) = response.next().await {
try_nl!(message);
}
Ok(())
}

pub fn message_mut(&mut self) -> &mut NexthopMessage {
&mut self.message
}
}
164 changes: 164 additions & 0 deletions src/nexthop/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// SPDX-License-Identifier: MIT

use netlink_packet_route::{
nexthop::{
NexthopAttribute, NexthopFlags, NexthopGroupEntry, NexthopMessage,
},
route::{RouteProtocol, RouteScope},
AddressFamily,
};
use std::{
marker::PhantomData,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
};

/// A builder for [`NexthopMessage`]
#[derive(Debug)]
pub struct NexthopMessageBuilder<T = IpAddr> {
message: NexthopMessage,
_phantom: PhantomData<T>,
}

impl<T> NexthopMessageBuilder<T> {
/// Create a new builder without specifying address family
fn new_no_address_family() -> Self {
let mut message = NexthopMessage::default();
message.header.protocol = u8::from(RouteProtocol::Static);
message.header.scope = u8::from(RouteScope::Universe);
Self {
message,
_phantom: PhantomData,
}
}

/// Set the nexthop ID
pub fn id(mut self, id: u32) -> Self {
self.message.nlas.push(NexthopAttribute::Id(id));
self
}

/// Set the interface index
pub fn oif(mut self, index: u32) -> Self {
self.message.nlas.push(NexthopAttribute::Oif(index));
self
}

/// Set the nexthop as blackhole
pub fn blackhole(mut self) -> Self {
self.message.nlas.push(NexthopAttribute::Blackhole);
self
}

/// Set the nexthop group
pub fn group(mut self, entries: Vec<(u32, u8)>) -> Self {
let group_entries = entries
.into_iter()
.map(|(id, weight)| NexthopGroupEntry {
id,
weight,
resvd1: 0,
resvd2: 0,
})
.collect();
self.message
.nlas
.push(NexthopAttribute::Group(group_entries));
self
}

/// Set flags
pub fn flags(mut self, flags: NexthopFlags) -> Self {
self.message.header.flags = flags;
self
}

/// Set the nexthop protocol
pub fn protocol(mut self, protocol: RouteProtocol) -> Self {
self.message.header.protocol = u8::from(protocol);
self
}

/// Set the nexthop scope
pub fn scope(mut self, scope: RouteScope) -> Self {
self.message.header.scope = u8::from(scope);
self
}

/// Build the message
pub fn build(self) -> NexthopMessage {
self.message
}
}

impl Default for NexthopMessageBuilder<Ipv4Addr> {
fn default() -> Self {
Self::new()
}
}

impl NexthopMessageBuilder<Ipv4Addr> {
/// Create a new builder for IPv4
pub fn new() -> Self {
let mut builder = Self::new_no_address_family();
builder.message.header.family = AddressFamily::Inet;
builder
}

/// Set the gateway IP address
pub fn gateway(mut self, addr: Ipv4Addr) -> Self {
self.message
.nlas
.push(NexthopAttribute::Gateway(addr.octets().to_vec()));
self
}
}

impl Default for NexthopMessageBuilder<Ipv6Addr> {
fn default() -> Self {
Self::new()
}
}

impl NexthopMessageBuilder<Ipv6Addr> {
/// Create a new builder for IPv6
pub fn new() -> Self {
let mut builder = Self::new_no_address_family();
builder.message.header.family = AddressFamily::Inet6;
builder
}

/// Set the gateway IP address
pub fn gateway(mut self, addr: Ipv6Addr) -> Self {
self.message
.nlas
.push(NexthopAttribute::Gateway(addr.octets().to_vec()));
self
}
}

impl Default for NexthopMessageBuilder<IpAddr> {
fn default() -> Self {
Self::new()
}
}

impl NexthopMessageBuilder<IpAddr> {
/// Create a new builder for any IP address family
pub fn new() -> Self {
Self::new_no_address_family()
}

/// Set the gateway IP address
pub fn gateway(mut self, addr: IpAddr) -> Self {
let (family, bytes) = match addr {
IpAddr::V4(addr) => (AddressFamily::Inet, addr.octets().to_vec()),
IpAddr::V6(addr) => (AddressFamily::Inet6, addr.octets().to_vec()),
};
// Only set family if not already set or if explicitly different (though usually we trust the caller)
if self.message.header.family == AddressFamily::Unspec {
self.message.header.family = family;
}
self.message.nlas.push(NexthopAttribute::Gateway(bytes));
self
}
Comment on lines +152 to +163

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation of gateway() can lead to an inconsistent NexthopMessage if it's called multiple times with IP addresses of different families. The address family is set based on the first call, but this is not validated on subsequent calls. This could result in a message with family = Inet but a gateway with an IPv6 address, which the kernel would likely reject.

To ensure correctness, the method should validate that any subsequent calls use an IP address of the same family. Panicking on misuse is an acceptable and common approach for builders to enforce correct usage.

The comment on line 157 is also a bit confusing and can be removed as part of this change.

    pub fn gateway(mut self, addr: IpAddr) -> Self {
        let (family, bytes) = match addr {
            IpAddr::V4(addr) => (AddressFamily::Inet, addr.octets().to_vec()),
            IpAddr::V6(addr) => (AddressFamily::Inet6, addr.octets().to_vec()),
        };
        if self.message.header.family == AddressFamily::Unspec {
            self.message.header.family = family;
        } else if self.message.header.family != family {
            panic!("Attempted to set gateway with an inconsistent IP address family");
        }
        self.message.nlas.push(NexthopAttribute::Gateway(bytes));
        self
    }

}
46 changes: 46 additions & 0 deletions src/nexthop/del.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: MIT

use crate::{try_nl, Error, Handle};
use futures_util::stream::StreamExt;
use netlink_packet_core::{NetlinkMessage, NLM_F_ACK, NLM_F_REQUEST};
use netlink_packet_route::{
nexthop::{NexthopAttribute, NexthopMessage},
RouteNetlinkMessage,
};

/// A request to delete a nexthop. This is equivalent to the `ip nexthop del`
/// commands.
#[derive(Debug)]
pub struct NexthopDelRequest {
handle: Handle,
message: NexthopMessage,
}

impl NexthopDelRequest {
pub(crate) fn new(handle: Handle, id: u32) -> Self {
let mut message = NexthopMessage::default();
message.nlas.push(NexthopAttribute::Id(id));
NexthopDelRequest { handle, message }
}

/// Execute the request.
pub async fn execute(self) -> Result<(), Error> {
let NexthopDelRequest {
mut handle,
message,
} = self;
let mut req =
NetlinkMessage::from(RouteNetlinkMessage::DelNexthop(message));
req.header.flags = NLM_F_REQUEST | NLM_F_ACK;

let mut response = handle.request(req)?;
while let Some(message) = response.next().await {
try_nl!(message);
}
Ok(())
}

pub fn message_mut(&mut self) -> &mut NexthopMessage {
&mut self.message
}
}
51 changes: 51 additions & 0 deletions src/nexthop/get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: MIT

use futures_util::{
future::{self, Either},
stream::{Stream, StreamExt},
FutureExt,
};
use netlink_packet_core::{NetlinkMessage, NLM_F_DUMP, NLM_F_REQUEST};
use netlink_packet_route::{nexthop::NexthopMessage, RouteNetlinkMessage};

use crate::{try_rtnl, Error, Handle};

/// A request to get nexthops. This is equivalent to the `ip nexthop show` commands.
#[derive(Debug)]
pub struct NexthopGetRequest {
handle: Handle,
message: NexthopMessage,
}

impl NexthopGetRequest {
pub(crate) fn new(handle: Handle) -> Self {
NexthopGetRequest {
handle,
message: NexthopMessage::default(),
}
}

/// Execute the request.
pub fn execute(self) -> impl Stream<Item = Result<NexthopMessage, Error>> {
let NexthopGetRequest {
mut handle,
message,
} = self;
let mut req =
NetlinkMessage::from(RouteNetlinkMessage::GetNexthop(message));
req.header.flags = NLM_F_REQUEST | NLM_F_DUMP;

match handle.request(req) {
Ok(response) => Either::Left(response.map(move |msg| {
Ok(try_rtnl!(msg, RouteNetlinkMessage::NewNexthop))
})),
Err(e) => Either::Right(
future::err::<NexthopMessage, Error>(e).into_stream(),
),
}
}

pub fn message_mut(&mut self) -> &mut NexthopMessage {
&mut self.message
}
}
Loading