Skip to content

Change AddrGenMode into enum #149

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
56 changes: 56 additions & 0 deletions src/link/af_spec/in6_addr_gen_mode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: MIT

use std::fmt::Display;

const IN6_ADDR_GEN_MODE_EUI64: u8 = 0;
const IN6_ADDR_GEN_MODE_NONE: u8 = 1;
const IN6_ADDR_GEN_MODE_STABLE_PRIVACY: u8 = 2;
const IN6_ADDR_GEN_MODE_RANDOM: u8 = 3;

#[derive(Clone, Copy, Eq, PartialEq, Debug, Default)]
#[non_exhaustive]
#[repr(u8)]
pub enum In6AddrGenMode {
#[default]
Eui64,
None,
StablePrivacy,
Random,
Other(u8),
}

impl From<u8> for In6AddrGenMode {
fn from(d: u8) -> Self {
match d {
IN6_ADDR_GEN_MODE_EUI64 => Self::Eui64,
IN6_ADDR_GEN_MODE_NONE => Self::None,
IN6_ADDR_GEN_MODE_STABLE_PRIVACY => Self::StablePrivacy,
IN6_ADDR_GEN_MODE_RANDOM => Self::Random,
_ => Self::Other(d),
}
}
}
impl From<&In6AddrGenMode> for u8 {
fn from(v: &In6AddrGenMode) -> Self {
match v {
In6AddrGenMode::Eui64 => IN6_ADDR_GEN_MODE_EUI64,
In6AddrGenMode::None => IN6_ADDR_GEN_MODE_NONE,
In6AddrGenMode::StablePrivacy => IN6_ADDR_GEN_MODE_STABLE_PRIVACY,
In6AddrGenMode::Random => IN6_ADDR_GEN_MODE_RANDOM,
In6AddrGenMode::Other(d) => *d,
}
}
}

impl Display for In6AddrGenMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Eui64 => write!(f, "eui64"),
Self::None => write!(f, "none"),
// https://github.com/iproute2/iproute2/blob/afbfd2f2b0a633d068990775f8e1b73b8ee83733/ip/ipaddress.c#L325-L329
Self::StablePrivacy => write!(f, "stable_secret"),
Copy link
Member

Choose a reason for hiding this comment

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

stable_privacy?

Copy link
Author

Choose a reason for hiding this comment

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

I was weirdly confused too but this is how it is implemented in iproute2 [1], [2]. I'm happy to change this though if you feel the need. Alternatively I could attach one of the links as a code comment to make thinks more clear.

Copy link
Contributor

Choose a reason for hiding this comment

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

Even if "stable_secret" is used in iproute, I am more in favor of "stable_privacy". But if @cathay4t agrees on "stable_secret" that is 2 vs 1, so ignore the comment.

Copy link
Author

Choose a reason for hiding this comment

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

Your call @cathay4t. I just outlined why I choose stable_secret. I'm fine either way.

Self::Random => write!(f, "random"),
Self::Other(d) => write!(f, "other({d})"),
}
}
}
15 changes: 8 additions & 7 deletions src/link/af_spec/inet6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::{
inet6_devconf::LINK_INET6_DEV_CONF_LEN, inet6_icmp::ICMP6_STATS_LEN,
inet6_stats::INET6_STATS_LEN,
};
use crate::ip::parse_ipv6_addr;
use crate::{ip::parse_ipv6_addr, link::af_spec::In6AddrGenMode};

const IFLA_INET6_FLAGS: u16 = 1;
const IFLA_INET6_CONF: u16 = 2;
Expand All @@ -43,7 +43,7 @@ pub enum AfSpecInet6 {
Stats(Inet6Stats),
Icmp6Stats(Icmp6Stats),
Token(Ipv6Addr),
AddrGenMode(u8),
AddrGenMode(In6AddrGenMode),
RaMtu(u32),
Other(DefaultNla),
}
Expand Down Expand Up @@ -89,7 +89,7 @@ impl Nla for AfSpecInet6 {
Stats(ref v) => v.emit(buffer),
Icmp6Stats(ref v) => v.emit(buffer),
Token(v) => buffer.copy_from_slice(&v.octets()),
AddrGenMode(value) => buffer[0] = value,
AddrGenMode(ref v) => buffer[0] = v.into(),
Other(ref nla) => nla.emit_value(buffer),
}
}
Expand Down Expand Up @@ -172,10 +172,11 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for AfSpecInet6 {
parse_ipv6_addr(payload)
.context("invalid IFLA_INET6_TOKEN value")?,
),
IFLA_INET6_ADDR_GEN_MODE => AddrGenMode(
parse_u8(payload)
.context("invalid IFLA_INET6_ADDR_GEN_MODE value")?,
),
IFLA_INET6_ADDR_GEN_MODE => {
let mode = parse_u8(payload)
.context("invalid IFLA_INET6_ADDR_GEN_MODE")?;
AddrGenMode(In6AddrGenMode::from(mode))
}
IFLA_INET6_RA_MTU => RaMtu(
parse_u32(payload)
.context("invalid IFLA_INET6_RA_MTU value")?,
Expand Down
2 changes: 2 additions & 0 deletions src/link/af_spec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: MIT

mod bridge;
mod in6_addr_gen_mode;
mod inet;
mod inet6;
mod inet6_cache;
Expand All @@ -14,6 +15,7 @@ pub use self::bridge::{
AfSpecBridge, BridgeFlag, BridgeMode, BridgeVlanInfo, BridgeVlanInfoFlags,
BridgeVlanTunnelInfo,
};
pub use self::in6_addr_gen_mode::In6AddrGenMode;
pub use self::inet::{AfSpecInet, InetDevConf};
pub use self::inet6::AfSpecInet6;
pub use self::inet6_cache::{Inet6CacheInfo, Inet6CacheInfoBuffer};
Expand Down
3 changes: 2 additions & 1 deletion src/link/tests/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use netlink_packet_utils::{nla::DefaultNla, Emitable, Parseable};

use crate::link::af_spec::In6AddrGenMode;
use crate::link::link_flag::LinkFlags;
use crate::link::{
AfSpecInet, AfSpecInet6, AfSpecUnspec, Icmp6Stats, Inet6CacheInfo,
Expand Down Expand Up @@ -382,7 +383,7 @@ fn test_parsing_link_statistics_on_kernel_4_18() {
csum_errors: 0,
}),
AfSpecInet6::Token(std::net::Ipv6Addr::UNSPECIFIED),
AfSpecInet6::AddrGenMode(1),
AfSpecInet6::AddrGenMode(In6AddrGenMode::None),
]),
]),
LinkAttribute::PropList(vec![Prop::AltIfName("enp0s3".into())]),
Expand Down
3 changes: 2 additions & 1 deletion src/link/tests/vrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::net::Ipv6Addr;
use netlink_packet_utils::nla::DefaultNla;
use netlink_packet_utils::{Emitable, Parseable};

use crate::link::af_spec::In6AddrGenMode;
use crate::link::link_flag::LinkFlags;
use crate::link::link_info::InfoVrfPort;
use crate::link::{
Expand Down Expand Up @@ -345,7 +346,7 @@ fn test_link_info_with_ifla_vrf_port_table() {
..Default::default()
}),
AfSpecInet6::Token(Ipv6Addr::UNSPECIFIED),
AfSpecInet6::AddrGenMode(0),
AfSpecInet6::AddrGenMode(In6AddrGenMode::Eui64),
]),
]),
],
Expand Down