Skip to content

Bump netlink-packet-utils and netlink-packet-core crates #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ description = "netlink packet types"
rich_nlas = []

[dependencies]
anyhow = "1.0.31"
bitflags = "2"
byteorder = "1.3.2"
libc = "0.2.66"
log = { version = "0.4.20", features = ["std"] }
netlink-packet-core = { version = "0.7.0" }
netlink-packet-utils = { version = "0.5.2" }
netlink-packet-core = { git = "https://github.com/rust-netlink/netlink-packet-core.git", rev = "01e8dd1" }
Copy link
Author

Choose a reason for hiding this comment

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

This is temporary, obviously.

netlink-packet-utils = { version = "0.6.0" }

[[example]]
name = "dump_packet_links"
Expand Down
31 changes: 12 additions & 19 deletions src/address/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::mem::size_of;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use anyhow::Context;
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::{
nla::{DefaultNla, Nla, NlaBuffer},
Expand Down Expand Up @@ -107,10 +106,10 @@ impl Nla for AddressAttribute {
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
for AddressAttribute
{
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
impl<T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&T>> for AddressAttribute {
type Error = DecodeError;

fn parse(buf: &NlaBuffer<&T>) -> Result<Self, Self::Error> {
let payload = buf.value();
Ok(match buf.kind() {
IFA_ADDRESS => {
Expand Down Expand Up @@ -147,9 +146,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
)));
}
}
IFA_LABEL => Self::Label(
parse_string(payload).context("invalid IFA_LABEL value")?,
),
IFA_LABEL => Self::Label(parse_string(payload)?),
IFA_BROADCAST => {
if payload.len() == IPV4_ADDR_LEN {
let mut data = [0u8; IPV4_ADDR_LEN];
Expand All @@ -176,10 +173,9 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
)));
}
}
IFA_CACHEINFO => Self::CacheInfo(
CacheInfo::parse(&CacheInfoBuffer::new(payload))
.context(format!("Invalid IFA_CACHEINFO {:?}", payload))?,
),
IFA_CACHEINFO => Self::CacheInfo(CacheInfo::parse(
&CacheInfoBuffer::new(payload),
)?),
IFA_MULTICAST => {
if payload.len() == IPV6_ADDR_LEN {
let mut data = [0u8; IPV6_ADDR_LEN];
Expand All @@ -193,13 +189,10 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
)));
}
}
IFA_FLAGS => Self::Flags(AddressFlags::from_bits_retain(
parse_u32(payload).context("invalid IFA_FLAGS value")?,
)),
kind => Self::Other(
DefaultNla::parse(buf)
.context(format!("unknown NLA type {kind}"))?,
),
IFA_FLAGS => {
Self::Flags(AddressFlags::from_bits_retain(parse_u32(payload)?))
}
_ => Self::Other(DefaultNla::parse(buf)?),
})
}
}
4 changes: 3 additions & 1 deletion src/address/cache_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ buffer!(CacheInfoBuffer(ADDRESSS_CACHE_INFO_LEN) {
});

impl<T: AsRef<[u8]>> Parseable<CacheInfoBuffer<T>> for CacheInfo {
fn parse(buf: &CacheInfoBuffer<T>) -> Result<Self, DecodeError> {
type Error = DecodeError;

fn parse(buf: &CacheInfoBuffer<T>) -> Result<Self, Self::Error> {
Ok(CacheInfo {
ifa_preferred: buf.ifa_preferred(),
ifa_valid: buf.ifa_valid(),
Expand Down
31 changes: 16 additions & 15 deletions src/address/message.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// SPDX-License-Identifier: MIT

use anyhow::Context;
use netlink_packet_utils::{
nla::{NlaBuffer, NlasIterator},
nla::{NlaBuffer, NlaError, NlasIterator},
traits::{Emitable, Parseable},
DecodeError,
};
Expand All @@ -23,10 +22,10 @@ buffer!(AddressMessageBuffer(ADDRESS_HEADER_LEN) {
payload: (slice, ADDRESS_HEADER_LEN..),
});

impl<'a, T: AsRef<[u8]> + ?Sized> AddressMessageBuffer<&'a T> {
impl<T: AsRef<[u8]> + ?Sized> AddressMessageBuffer<&T> {
pub fn attributes(
&self,
) -> impl Iterator<Item = Result<NlaBuffer<&'a [u8]>, DecodeError>> {
) -> impl Iterator<Item = Result<NlaBuffer<&[u8]>, NlaError>> {
NlasIterator::new(self.payload())
}
}
Expand Down Expand Up @@ -76,7 +75,9 @@ impl Emitable for AddressMessage {
}

impl<T: AsRef<[u8]>> Parseable<AddressMessageBuffer<T>> for AddressHeader {
fn parse(buf: &AddressMessageBuffer<T>) -> Result<Self, DecodeError> {
type Error = DecodeError;

fn parse(buf: &AddressMessageBuffer<T>) -> Result<Self, Self::Error> {
Ok(Self {
family: buf.family().into(),
prefix_len: buf.prefix_len(),
Expand All @@ -87,23 +88,23 @@ impl<T: AsRef<[u8]>> Parseable<AddressMessageBuffer<T>> for AddressHeader {
}
}

impl<'a, T: AsRef<[u8]> + 'a> Parseable<AddressMessageBuffer<&'a T>>
for AddressMessage
{
fn parse(buf: &AddressMessageBuffer<&'a T>) -> Result<Self, DecodeError> {
impl<T: AsRef<[u8]>> Parseable<AddressMessageBuffer<&T>> for AddressMessage {
type Error = DecodeError;

fn parse(buf: &AddressMessageBuffer<&T>) -> Result<Self, Self::Error> {
Ok(AddressMessage {
header: AddressHeader::parse(buf)
.context("failed to parse address message header")?,
attributes: Vec::<AddressAttribute>::parse(buf)
.context("failed to parse address message NLAs")?,
header: AddressHeader::parse(buf)?,
attributes: Vec::<AddressAttribute>::parse(buf)?,
})
}
}

impl<'a, T: AsRef<[u8]> + 'a> Parseable<AddressMessageBuffer<&'a T>>
impl<T: AsRef<[u8]>> Parseable<AddressMessageBuffer<&T>>
for Vec<AddressAttribute>
{
fn parse(buf: &AddressMessageBuffer<&'a T>) -> Result<Self, DecodeError> {
type Error = DecodeError;

fn parse(buf: &AddressMessageBuffer<&T>) -> Result<Self, Self::Error> {
let mut attributes = vec![];
for nla_buf in buf.attributes() {
attributes.push(AddressAttribute::parse(&nla_buf?)?);
Expand Down
92 changes: 30 additions & 62 deletions src/link/af_spec/bridge.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: MIT

use anyhow::Context;
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::{
nla::{DefaultNla, Nla, NlaBuffer, NlasIterator},
Expand Down Expand Up @@ -65,39 +64,26 @@ impl Nla for AfSpecBridge {
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for AfSpecBridge {
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
impl<T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&T>> for AfSpecBridge {
type Error = DecodeError;

fn parse(buf: &NlaBuffer<&T>) -> Result<Self, Self::Error> {
let payload = buf.value();
Ok(match buf.kind() {
IFLA_BRIDGE_FLAGS => Self::Flags(
parse_u16(payload)
.context("Invalid IFLA_BRIDGE_FLAGS value")?
.into(),
),
IFLA_BRIDGE_MODE => Self::Mode(
parse_u16(payload)
.context("Invalid IFLA_BRIDGE_MODE value")?
.into(),
),
IFLA_BRIDGE_VLAN_INFO => Self::VlanInfo(
BridgeVlanInfo::try_from(payload)
.context("Invalid IFLA_BRIDGE_VLAN_INFO value")?,
),
IFLA_BRIDGE_FLAGS => Self::Flags(parse_u16(payload)?.into()),
IFLA_BRIDGE_MODE => Self::Mode(parse_u16(payload)?.into()),
IFLA_BRIDGE_VLAN_INFO => {
Self::VlanInfo(BridgeVlanInfo::try_from(payload)?)
}
IFLA_BRIDGE_VLAN_TUNNEL_INFO => {
let mut nlas = Vec::new();
for nla in NlasIterator::new(payload) {
let nla = &nla.context(format!(
"Invalid IFLA_BRIDGE_VLAN_TUNNEL_INFO for {payload:?}"
))?;
let parsed = BridgeVlanTunnelInfo::parse(nla)?;
let parsed = BridgeVlanTunnelInfo::parse(&nla?)?;
nlas.push(parsed);
}
Self::VlanTunnelInfo(nlas)
}
kind => Self::Other(
DefaultNla::parse(buf)
.context(format!("Unknown NLA type {kind}"))?,
),
_ => Self::Other(DefaultNla::parse(buf)?),
})
}
}
Expand All @@ -106,15 +92,13 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for AfSpecBridge {
pub(crate) struct VecAfSpecBridge(pub(crate) Vec<AfSpecBridge>);

#[cfg(any(target_os = "linux", target_os = "fuchsia"))]
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
for VecAfSpecBridge
{
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
impl<T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&T>> for VecAfSpecBridge {
type Error = DecodeError;

fn parse(buf: &NlaBuffer<&T>) -> Result<Self, Self::Error> {
let mut nlas = vec![];
let err = "Invalid AF_INET NLA for IFLA_AF_SPEC(AF_BRIDGE)";
for nla in NlasIterator::new(buf.into_inner()) {
let nla = nla.context(err)?;
nlas.push(AfSpecBridge::parse(&nla).context(err)?);
nlas.push(AfSpecBridge::parse(&nla?)?);
}
Ok(Self(nlas))
}
Expand Down Expand Up @@ -178,14 +162,10 @@ impl TryFrom<&[u8]> for BridgeVlanInfo {
fn try_from(raw: &[u8]) -> Result<Self, DecodeError> {
if raw.len() == 4 {
Ok(Self {
flags: BridgeVlanInfoFlags::from_bits_retain(
parse_u16(&raw[0..2]).context(format!(
"Invalid IFLA_BRIDGE_VLAN_INFO value: {raw:?}"
))?,
),
vid: parse_u16(&raw[2..4]).context(format!(
"Invalid IFLA_BRIDGE_VLAN_INFO value: {raw:?}"
))?,
flags: BridgeVlanInfoFlags::from_bits_retain(parse_u16(
&raw[0..2],
)?),
vid: parse_u16(&raw[2..4])?,
})
} else {
Err(DecodeError::from(format!(
Expand Down Expand Up @@ -313,32 +293,20 @@ impl Nla for BridgeVlanTunnelInfo {
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
impl<T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&T>>
for BridgeVlanTunnelInfo
{
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
type Error = DecodeError;

fn parse(buf: &NlaBuffer<&T>) -> Result<Self, Self::Error> {
let payload = buf.value();
Ok(match buf.kind() {
IFLA_BRIDGE_VLAN_TUNNEL_ID => {
Self::Id(parse_u32(payload).context(format!(
"Invalid IFLA_BRIDGE_VLAN_TUNNEL_ID {payload:?}"
))?)
}
IFLA_BRIDGE_VLAN_TUNNEL_VID => {
Self::Vid(parse_u16(payload).context(format!(
"Invalid IFLA_BRIDGE_VLAN_TUNNEL_VID {payload:?}"
))?)
}
IFLA_BRIDGE_VLAN_TUNNEL_FLAGS => {
Self::Flags(BridgeVlanInfoFlags::from_bits_retain(
parse_u16(payload).context(format!(
"Invalid IFLA_BRIDGE_VLAN_TUNNEL_VID {payload:?}"
))?,
))
}
_ => {
Self::Other(DefaultNla::parse(buf).context("Unknown NLA type")?)
}
IFLA_BRIDGE_VLAN_TUNNEL_ID => Self::Id(parse_u32(payload)?),
IFLA_BRIDGE_VLAN_TUNNEL_VID => Self::Vid(parse_u16(payload)?),
IFLA_BRIDGE_VLAN_TUNNEL_FLAGS => Self::Flags(
BridgeVlanInfoFlags::from_bits_retain(parse_u16(payload)?),
),
_ => Self::Other(DefaultNla::parse(buf)?),
})
}
}
27 changes: 13 additions & 14 deletions src/link/af_spec/inet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: MIT

use anyhow::Context;
use netlink_packet_utils::{
nla::{self, DefaultNla, NlaBuffer, NlasIterator},
traits::{Emitable, Parseable},
Expand All @@ -24,15 +23,13 @@ pub enum AfSpecInet {

pub(crate) struct VecAfSpecInet(pub(crate) Vec<AfSpecInet>);

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
for VecAfSpecInet
{
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
impl<T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&T>> for VecAfSpecInet {
type Error = DecodeError;

fn parse(buf: &NlaBuffer<&T>) -> Result<Self, Self::Error> {
let mut nlas = vec![];
let err = "Invalid AF_INET NLA for IFLA_AF_SPEC(AF_UNSPEC)";
for nla in NlasIterator::new(buf.into_inner()) {
let nla = nla.context(err)?;
nlas.push(AfSpecInet::parse(&nla)?);
nlas.push(AfSpecInet::parse(&nla?)?);
}
Ok(Self(nlas))
}
Expand Down Expand Up @@ -64,8 +61,10 @@ impl nla::Nla for AfSpecInet {
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for AfSpecInet {
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
impl<T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&T>> for AfSpecInet {
type Error = DecodeError;

fn parse(buf: &NlaBuffer<&T>) -> Result<Self, Self::Error> {
use self::AfSpecInet::*;

let payload = buf.value();
Expand All @@ -80,9 +79,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for AfSpecInet {
.as_slice(),
))?)
}
kind => Other(DefaultNla::parse(buf).context(format!(
"Unknown NLA type {kind} for IFLA_AF_SPEC(inet)"
))?),
_ => Other(DefaultNla::parse(buf)?),
})
}
}
Expand Down Expand Up @@ -162,7 +159,9 @@ pub struct InetDevConf {
}

impl<T: AsRef<[u8]>> Parseable<InetDevConfBuffer<T>> for InetDevConf {
fn parse(buf: &InetDevConfBuffer<T>) -> Result<Self, DecodeError> {
type Error = DecodeError;

fn parse(buf: &InetDevConfBuffer<T>) -> Result<Self, Self::Error> {
Ok(Self {
forwarding: buf.forwarding(),
mc_forwarding: buf.mc_forwarding(),
Expand Down
Loading