Skip to content

Commit

Permalink
Update to Neli 0.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestask authored and phi-gamma committed Sep 24, 2022
1 parent d846040 commit 684639c
Show file tree
Hide file tree
Showing 14 changed files with 421 additions and 297 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ hex = { version = "0.4.3", optional = true }
take-until = { version = " 0.1.0", optional = true }

[target.'cfg(target_os = "linux")'.dependencies]
neli = "=0.4.3"
neli = "=0.5.3"
libc = "0.2.66"

[dev-dependencies]
Expand Down
27 changes: 17 additions & 10 deletions src/linux/attr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use neli::consts::NlAttrType;
use neli::{impl_var, impl_var_base, impl_var_trait};
use neli::consts::genl::NlAttrType;
use neli::impl_var;
use std::fmt;

// As of neli 0.4.3, the NLA_F_NESTED flag needs to be added to newly created
Expand Down Expand Up @@ -29,19 +29,21 @@ macro_rules! impl_bit_ops_for_nla {
};
}

impl_var_trait!(
NlaNested, u16, NlAttrType,
impl_var!(
pub NlaNested, u16,
Unspec => 0,
// neli requires 1 non-zero argument even though WireGuard
// does not use it.
Unused => 1
);

impl NlAttrType for NlaNested {}

impl_bit_ops_for_nla!(NlaNested);

// https://github.com/WireGuard/WireGuard/blob/62b335b56cc99312ccedfa571500fbef3756a623/src/uapi/wireguard.h#L147
impl_var_trait!(
WgDeviceAttribute, u16, NlAttrType,
impl_var!(
pub WgDeviceAttribute, u16,
Unspec => 0,
Ifindex => 1,
Ifname => 2,
Expand All @@ -53,6 +55,8 @@ impl_var_trait!(
Peers => 8
);

impl NlAttrType for WgDeviceAttribute {}

impl fmt::Display for WgDeviceAttribute {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
Expand All @@ -62,8 +66,8 @@ impl fmt::Display for WgDeviceAttribute {
impl_bit_ops_for_nla!(WgDeviceAttribute);

// https://github.com/WireGuard/WireGuard/blob/62b335b56cc99312ccedfa571500fbef3756a623/src/uapi/wireguard.h#L165
impl_var_trait!(
WgPeerAttribute, u16, NlAttrType,
impl_var!(
pub WgPeerAttribute, u16,
Unspec => 0,
PublicKey => 1,
PresharedKey => 2,
Expand All @@ -77,6 +81,8 @@ impl_var_trait!(
ProtocolVersion => 10
);

impl NlAttrType for WgPeerAttribute {}

impl fmt::Display for WgPeerAttribute {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
Expand All @@ -86,10 +92,11 @@ impl fmt::Display for WgPeerAttribute {
impl_bit_ops_for_nla!(WgPeerAttribute);

// https://github.com/WireGuard/WireGuard/blob/62b335b56cc99312ccedfa571500fbef3756a623/src/uapi/wireguard.h#L181
impl_var_trait!(
WgAllowedIpAttribute, u16, NlAttrType,
impl_var!(
pub WgAllowedIpAttribute, u16,
Unspec => 0,
Family => 1,
IpAddr => 2,
CidrMask => 3
);
impl NlAttrType for WgAllowedIpAttribute {}
9 changes: 5 additions & 4 deletions src/linux/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use neli::consts::Cmd;
use neli::{impl_var, impl_var_base, impl_var_trait};
use neli::{consts::genl::Cmd, impl_var};

// https://github.com/WireGuard/WireGuard/blob/62b335b56cc99312ccedfa571500fbef3756a623/src/uapi/wireguard.h#L137
impl_var_trait!(
WgCmd, u8, Cmd,
impl_var!(
pub WgCmd, u8,
GetDevice => 0,
SetDevice => 1
);

impl Cmd for WgCmd {}
10 changes: 10 additions & 0 deletions src/linux/err/get_device_error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::ParseDeviceError;
use neli::err::{NlError, SerError};
use std::io;
use thiserror::Error;

#[derive(Error, Debug)]
Expand All @@ -20,6 +21,9 @@ pub enum GetDeviceError {

#[error(transparent)]
ParseDeviceError(ParseDeviceError),

#[error(transparent)]
IoError(io::Error),
}

impl From<NlError> for GetDeviceError {
Expand All @@ -39,3 +43,9 @@ impl From<ParseDeviceError> for GetDeviceError {
GetDeviceError::ParseDeviceError(error)
}
}

impl From<io::Error> for GetDeviceError {
fn from(error: io::Error) -> Self {
Self::IoError(error)
}
}
10 changes: 10 additions & 0 deletions src/linux/err/list_devices_error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::ParseAttributeError;
use neli::err::{DeError, NlError, SerError};
use std::io;
use thiserror::Error;

#[derive(Error, Debug)]
Expand All @@ -16,6 +17,9 @@ pub enum ListDevicesError {
#[error(transparent)]
ParseAttributeError(ParseAttributeError),

#[error(transparent)]
IoError(io::Error),

// TODO: Print netlink error message when neli exposes it.
#[error("Unknown netlink error while reading devices.")]
Unknown,
Expand Down Expand Up @@ -44,3 +48,9 @@ impl From<ParseAttributeError> for ListDevicesError {
Self::ParseAttributeError(error)
}
}

impl From<io::Error> for ListDevicesError {
fn from(error: io::Error) -> Self {
Self::IoError(error)
}
}
10 changes: 10 additions & 0 deletions src/linux/err/set_device_error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use neli::err::{NlError, SerError};
use std::io;
use thiserror::Error;

#[derive(Error, Debug)]
Expand All @@ -8,6 +9,9 @@ pub enum SetDeviceError {

#[error(transparent)]
NlSerError(SerError),

#[error(transparent)]
IoError(io::Error),
}

impl From<NlError> for SetDeviceError {
Expand All @@ -21,3 +25,9 @@ impl From<SerError> for SetDeviceError {
SetDeviceError::NlSerError(error)
}
}

impl From<io::Error> for SetDeviceError {
fn from(error: io::Error) -> Self {
Self::IoError(error)
}
}
19 changes: 11 additions & 8 deletions src/linux/interface.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::linux::attr::WgDeviceAttribute;
use neli::err::SerError;
use neli::nlattr::Nlattr;
use neli::{err::NlError, genl::Nlattr, types::Buffer};
use std::borrow::Cow;
use std::convert::TryFrom;

Expand All @@ -20,17 +19,21 @@ impl<'a> DeviceInterface<'a> {
}
}

impl<'a> TryFrom<&DeviceInterface<'a>> for Nlattr<WgDeviceAttribute, Vec<u8>> {
type Error = SerError;
impl<'a> TryFrom<&DeviceInterface<'a>> for Nlattr<WgDeviceAttribute, Buffer> {
type Error = NlError;

fn try_from(interface: &DeviceInterface) -> Result<Self, Self::Error> {
let attr = match interface {
&DeviceInterface::Index(ifindex) => {
Nlattr::new(None, WgDeviceAttribute::Ifindex, ifindex)?
}
DeviceInterface::Name(ifname) => {
Nlattr::new(None, WgDeviceAttribute::Ifname, ifname.as_ref())?
Nlattr::new(None, false, false, WgDeviceAttribute::Ifindex, ifindex)?
}
DeviceInterface::Name(ifname) => Nlattr::new(
None,
false,
false,
WgDeviceAttribute::Ifname,
ifname.as_ref(),
)?,
};
Ok(attr)
}
Expand Down
24 changes: 18 additions & 6 deletions src/linux/set/allowed_ip.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::linux::attr::NLA_F_NESTED;
use crate::linux::attr::{NlaNested, WgAllowedIpAttribute};
use neli::err::SerError;
use neli::nlattr::Nlattr;
use neli::err::NlError;
use neli::genl::Nlattr;
use neli::types::Buffer;
use std::convert::TryFrom;
use std::net::IpAddr;

Expand All @@ -20,18 +21,21 @@ impl<'a> AllowedIp<'a> {
}
}

impl<'a> TryFrom<&AllowedIp<'a>> for Nlattr<NlaNested, Vec<u8>> {
type Error = SerError;
impl<'a> TryFrom<&AllowedIp<'a>> for Nlattr<NlaNested, Buffer> {
type Error = NlError;

fn try_from(allowed_ip: &AllowedIp) -> Result<Self, Self::Error> {
let mut nested = Nlattr::new::<Vec<u8>>(None, NlaNested::Unspec | NLA_F_NESTED, vec![])?;
let mut nested =
Nlattr::new::<Vec<u8>>(None, false, false, NlaNested::Unspec | NLA_F_NESTED, vec![])?;

let family = match allowed_ip.ipaddr {
IpAddr::V4(_) => libc::AF_INET as u16,
IpAddr::V6(_) => libc::AF_INET6 as u16,
};
nested.add_nested_attribute(&Nlattr::new(
None,
false,
false,
WgAllowedIpAttribute::Family,
&family.to_ne_bytes()[..],
)?)?;
Expand All @@ -40,14 +44,22 @@ impl<'a> TryFrom<&AllowedIp<'a>> for Nlattr<NlaNested, Vec<u8>> {
IpAddr::V4(addr) => addr.octets().to_vec(),
IpAddr::V6(addr) => addr.octets().to_vec(),
};
nested.add_nested_attribute(&Nlattr::new(None, WgAllowedIpAttribute::IpAddr, ipaddr)?)?;
nested.add_nested_attribute(&Nlattr::new(
None,
false,
false,
WgAllowedIpAttribute::IpAddr,
ipaddr,
)?)?;

let cidr_mask = allowed_ip.cidr_mask.unwrap_or(match allowed_ip.ipaddr {
IpAddr::V4(_) => 32,
IpAddr::V6(_) => 128,
});
nested.add_nested_attribute(&Nlattr::new(
None,
false,
false,
WgAllowedIpAttribute::CidrMask,
&cidr_mask.to_ne_bytes()[..],
)?)?;
Expand Down
Loading

0 comments on commit 684639c

Please sign in to comment.