Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

netlink-packet-route: re-introduce rich NLAs #199

Merged
merged 1 commit into from
Nov 28, 2021
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
run: |
cd netlink-packet-route
cargo test
cargo test --features rich_nlas

- name: test (netlink-packet-sock-diag)
run: |
Expand Down
3 changes: 3 additions & 0 deletions netlink-packet-route/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ readme = "../README.md"
repository = "https://github.com/little-dude/netlink"
description = "netlink packet types"

[features]
rich_nlas = []

[dependencies]
anyhow = "1.0.31"
byteorder = "1.3.2"
Expand Down
67 changes: 63 additions & 4 deletions netlink-packet-route/src/rtnl/route/nlas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,33 @@ use crate::{
DecodeError,
};

#[cfg(feature = "rich_nlas")]
use crate::traits::Emitable;

/// Netlink attributes for `RTM_NEWROUTE`, `RTM_DELROUTE`,
/// `RTM_GETROUTE` messages.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Nla {
#[cfg(not(feature = "rich_nlas"))]
Metrics(Vec<u8>),
#[cfg(feature = "rich_nlas")]
Metrics(Metrics),
#[cfg(not(feature = "rich_nlas"))]
MfcStats(Vec<u8>),
#[cfg(feature = "rich_nlas")]
MfcStats(MfcStats),
#[cfg(not(feature = "rich_nlas"))]
CacheInfo(Vec<u8>),
#[cfg(feature = "rich_nlas")]
CacheInfo(CacheInfo),
Unspec(Vec<u8>),
Destination(Vec<u8>),
Source(Vec<u8>),
Gateway(Vec<u8>),
PrefSource(Vec<u8>),
Metrics(Vec<u8>),
MultiPath(Vec<u8>),
CacheInfo(Vec<u8>),
Session(Vec<u8>),
MpAlgo(Vec<u8>),
MfcStats(Vec<u8>),
Via(Vec<u8>),
NewDestination(Vec<u8>),
Pref(Vec<u8>),
Expand Down Expand Up @@ -71,10 +85,19 @@ impl nlas::Nla for Nla {
| Pad(ref bytes)
| Uid(ref bytes)
| TtlPropagate(ref bytes)
| CacheInfo(ref bytes)
=> bytes.len(),

#[cfg(not(feature = "rich_nlas"))]
CacheInfo(ref bytes)
| MfcStats(ref bytes)
| Metrics(ref bytes)
=> bytes.len(),
#[cfg(feature = "rich_nlas")]
CacheInfo(ref cache_info) => cache_info.buffer_len(),
#[cfg(feature = "rich_nlas")]
MfcStats(ref stats) => stats.buffer_len(),
#[cfg(feature = "rich_nlas")]
Metrics(ref metrics) => metrics.buffer_len(),

EncapType(_) => 2,
Iif(_)
Expand Down Expand Up @@ -110,10 +133,20 @@ impl nlas::Nla for Nla {
| Pad(ref bytes)
| Uid(ref bytes)
| TtlPropagate(ref bytes)
=> buffer.copy_from_slice(bytes.as_slice()),

#[cfg(not(feature = "rich_nlas"))]
| CacheInfo(ref bytes)
| MfcStats(ref bytes)
| Metrics(ref bytes)
=> buffer.copy_from_slice(bytes.as_slice()),

#[cfg(feature = "rich_nlas")]
CacheInfo(ref cache_info) => cache_info.emit(buffer),
#[cfg(feature = "rich_nlas")]
MfcStats(ref stats) => stats.emit(buffer),
#[cfg(feature = "rich_nlas")]
Metrics(ref metrics) => metrics.emit(buffer),
EncapType(value) => NativeEndian::write_u16(buffer, value),
Iif(value)
| Oif(value)
Expand Down Expand Up @@ -196,9 +229,35 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for Nla {
RTA_FLOW => Flow(parse_u32(payload).context("invalid RTA_FLOW value")?),
RTA_TABLE => Table(parse_u32(payload).context("invalid RTA_TABLE value")?),
RTA_MARK => Mark(parse_u32(payload).context("invalid RTA_MARK value")?),

#[cfg(not(feature = "rich_nlas"))]
RTA_CACHEINFO => CacheInfo(payload.to_vec()),
#[cfg(feature = "rich_nlas")]
RTA_CACHEINFO => CacheInfo(
cache_info::CacheInfo::parse(
&CacheInfoBuffer::new_checked(payload)
.context("invalid RTA_CACHEINFO value")?,
)
.context("invalid RTA_CACHEINFO value")?,
),
#[cfg(not(feature = "rich_nlas"))]
RTA_MFC_STATS => MfcStats(payload.to_vec()),
#[cfg(feature = "rich_nlas")]
RTA_MFC_STATS => MfcStats(
mfc_stats::MfcStats::parse(
&MfcStatsBuffer::new_checked(payload).context("invalid RTA_MFC_STATS value")?,
)
.context("invalid RTA_MFC_STATS value")?,
),
#[cfg(not(feature = "rich_nlas"))]
RTA_METRICS => Metrics(payload.to_vec()),
#[cfg(feature = "rich_nlas")]
RTA_METRICS => Metrics(
metrics::Metrics::parse(
&NlaBuffer::new_checked(payload).context("invalid RTA_METRICS value")?,
)
.context("invalid RTA_METRICS value")?,
),
_ => Other(DefaultNla::parse(buf).context("invalid NLA (unknown kind)")?),
})
}
Expand Down