Skip to content

Commit

Permalink
Merge branch 'main' into matchall_method
Browse files Browse the repository at this point in the history
  • Loading branch information
cathay4t authored Jan 10, 2024
2 parents 49646df + 24982ec commit 5275c05
Show file tree
Hide file tree
Showing 43 changed files with 645 additions and 442 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
# Changelog
## [0.14.0] - 2023-12-05
### Breaking changes
- Many `VxlanAddRequest` functions changed from u8 to bool. (ba4825a)
- Deprecated `LinkSetRequest::master()` in the favor of
`LinkSetRequest::controller()`. (ba4825a)
- Deprecated `LinkSetRequest::nomaster()` in the favor of
`LinkSetRequest::nocontroller()`. (ba4825a)
- Many `NeighbourAddRequest` functions changed from u8/u16 to enum. (ba4825a)
- Many `TrafficFilterNewRequest` functions changed from u8/u16 to enum.
(ba4825a)

### New features
- Rule: function to set fw_mark when adding rule. (dabef43)

### Bug fixes
- N/A

## [0.13.1] - 2023-07-18
### Breaking changes
- Deprecated `BondAddRequest::active_slave()` in the favor of
Expand Down
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rtnetlink"
version = "0.13.1"
version = "0.14.0"
authors = ["Corentin Henry <[email protected]>"]
edition = "2018"
homepage = "https://github.com/rust-netlink/rtnetlink"
Expand All @@ -9,6 +9,7 @@ license = "MIT"
readme = "README.md"
repository = "https://github.com/rust-netlink/rtnetlink"
description = "manipulate linux networking resources via netlink"
rust-version = "1.66.1"

[features]
test_as_root = []
Expand All @@ -22,10 +23,10 @@ log = "0.4.8"
thiserror = "1"
netlink-sys = { version = "0.8" }
netlink-packet-utils = { version = "0.5" }
netlink-packet-route = { version = "0.17" }
netlink-packet-route = { version = "0.18" }
netlink-packet-core = { version = "0.7" }
netlink-proto = { default-features = false, version = "0.11" }
nix = { version = "0.26.1", default-features = false, features = ["fs", "mount", "sched", "signal"] }
nix = { version = "0.27.1", default-features = false, features = ["fs", "mount", "sched", "signal"] }
tokio = { version = "1.0.1", features = ["rt"], optional = true}
async-global-executor = { version = "2.0.2", optional = true }

Expand Down
14 changes: 7 additions & 7 deletions examples/create_vlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ use rtnetlink::{new_connection, QosMapping};
fn parse_mapping(parameter: &str) -> Result<QosMapping, Box<dyn StdError>> {
let (from, to) = parameter
.split_once(':')
.ok_or_else(|| "Failed to parse mapping..")?;
.ok_or("Failed to parse mapping..")?;

Ok(QosMapping {
from: u32::from_str(from)?,
to: u32::from_str(to)?,
})
}

const ARG_BASE: &'static str = "--base";
const ARG_NAME: &'static str = "--name";
const ARG_ID: &'static str = "--id";
const ARG_INGRESS_QOS: &'static str = "--ingress-qos-mapping";
const ARG_EGRESS_QOS: &'static str = "--egress-qos-mapping";
const ARG_BASE: &str = "--base";
const ARG_NAME: &str = "--name";
const ARG_ID: &str = "--id";
const ARG_INGRESS_QOS: &str = "--ingress-qos-mapping";
const ARG_EGRESS_QOS: &str = "--egress-qos-mapping";

enum ParsingMode {
None,
Expand Down Expand Up @@ -51,7 +51,7 @@ async fn main() -> Result<(), String> {
ARG_EGRESS_QOS => Ok(ParsingMode::Egress),
other => {
usage();
return Err(format!("Unexpected argument: {other}"));
Err(format!("Unexpected argument: {other}"))
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions examples/get_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use futures::stream::TryStreamExt;
use netlink_packet_route::{
constants::{AF_BRIDGE, RTEXT_FILTER_BRVLAN},
link::nlas::Nla,
link::{LinkAttribute, LinkExtentMask},
AddressFamily,
};
use rtnetlink::{new_connection, Error, Handle};

Expand Down Expand Up @@ -52,8 +52,8 @@ async fn get_link_by_index(handle: Handle, index: u32) -> Result<(), Error> {
// We should have received only one message
assert!(links.try_next().await?.is_none());

for nla in msg.nlas.into_iter() {
if let Nla::IfName(name) = nla {
for nla in msg.attributes.into_iter() {
if let LinkAttribute::IfName(name) = nla {
println!("found link with index {index} (name = {name})");
return Ok(());
}
Expand All @@ -79,8 +79,8 @@ async fn get_link_by_name(handle: Handle, name: String) -> Result<(), Error> {
async fn dump_links(handle: Handle) -> Result<(), Error> {
let mut links = handle.link().get().execute();
'outer: while let Some(msg) = links.try_next().await? {
for nla in msg.nlas.into_iter() {
if let Nla::IfName(name) = nla {
for nla in msg.attributes.into_iter() {
if let LinkAttribute::IfName(name) = nla {
println!("found link {} ({})", msg.header.index, name);
continue 'outer;
}
Expand All @@ -94,11 +94,11 @@ async fn dump_bridge_filter_info(handle: Handle) -> Result<(), Error> {
let mut links = handle
.link()
.get()
.set_filter_mask(AF_BRIDGE as u8, RTEXT_FILTER_BRVLAN)
.set_filter_mask(AddressFamily::Bridge, vec![LinkExtentMask::Brvlan])
.execute();
'outer: while let Some(msg) = links.try_next().await? {
for nla in msg.nlas.into_iter() {
if let Nla::AfSpecBridge(data) = nla {
for nla in msg.attributes.into_iter() {
if let LinkAttribute::AfSpecBridge(data) = nla {
println!(
"found interface {} with AfSpecBridge data {:?})",
msg.header.index, data
Expand Down
18 changes: 9 additions & 9 deletions examples/get_links_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use futures::stream::TryStreamExt;
use netlink_packet_route::{
constants::{AF_BRIDGE, RTEXT_FILTER_BRVLAN},
link::nlas::Nla,
link::{LinkAttribute, LinkExtentMask},
AddressFamily,
};
use rtnetlink::{new_connection, Error, Handle};

Expand Down Expand Up @@ -52,8 +52,8 @@ async fn get_link_by_index(handle: Handle, index: u32) -> Result<(), Error> {
// We should have received only one message
assert!(links.try_next().await?.is_none());

for nla in msg.nlas.into_iter() {
if let Nla::IfName(name) = nla {
for nla in msg.attributes.into_iter() {
if let LinkAttribute::IfName(name) = nla {
println!("found link with index {index} (name = {name})");
return Ok(());
}
Expand All @@ -79,8 +79,8 @@ async fn get_link_by_name(handle: Handle, name: String) -> Result<(), Error> {
async fn dump_links(handle: Handle) -> Result<(), Error> {
let mut links = handle.link().get().execute();
'outer: while let Some(msg) = links.try_next().await? {
for nla in msg.nlas.into_iter() {
if let Nla::IfName(name) = nla {
for nla in msg.attributes.into_iter() {
if let LinkAttribute::IfName(name) = nla {
println!("found link {} ({})", msg.header.index, name);
continue 'outer;
}
Expand All @@ -94,11 +94,11 @@ async fn dump_bridge_filter_info(handle: Handle) -> Result<(), Error> {
let mut links = handle
.link()
.get()
.set_filter_mask(AF_BRIDGE as u8, RTEXT_FILTER_BRVLAN)
.set_filter_mask(AddressFamily::Bridge, vec![LinkExtentMask::Brvlan])
.execute();
'outer: while let Some(msg) = links.try_next().await? {
for nla in msg.nlas.into_iter() {
if let Nla::AfSpecBridge(data) = nla {
for nla in msg.attributes.into_iter() {
if let LinkAttribute::AfSpecBridge(data) = nla {
println!(
"found interface {} with AfSpecBridge data {:?})",
msg.header.index, data
Expand Down
19 changes: 11 additions & 8 deletions examples/get_links_thread_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// SPDX-License-Identifier: MIT

use futures::stream::TryStreamExt;
use netlink_packet_route::{link::nlas::Nla, AF_BRIDGE, RTEXT_FILTER_BRVLAN};
use netlink_packet_route::{
link::{LinkAttribute, LinkExtentMask},
AddressFamily,
};
use rtnetlink::{new_connection, Error, Handle};

async fn do_it(rt: &tokio::runtime::Runtime) -> Result<(), ()> {
Expand Down Expand Up @@ -48,8 +51,8 @@ async fn get_link_by_index(handle: Handle, index: u32) -> Result<(), Error> {
// We should have received only one message
assert!(links.try_next().await?.is_none());

for nla in msg.nlas.into_iter() {
if let Nla::IfName(name) = nla {
for nla in msg.attributes.into_iter() {
if let LinkAttribute::IfName(name) = nla {
println!("found link with index {index} (name = {name})");
return Ok(());
}
Expand All @@ -75,8 +78,8 @@ async fn get_link_by_name(handle: Handle, name: String) -> Result<(), Error> {
async fn dump_links(handle: Handle) -> Result<(), Error> {
let mut links = handle.link().get().execute();
'outer: while let Some(msg) = links.try_next().await? {
for nla in msg.nlas.into_iter() {
if let Nla::IfName(name) = nla {
for nla in msg.attributes.into_iter() {
if let LinkAttribute::IfName(name) = nla {
println!("found link {} ({})", msg.header.index, name);
continue 'outer;
}
Expand All @@ -90,11 +93,11 @@ async fn dump_bridge_filter_info(handle: Handle) -> Result<(), Error> {
let mut links = handle
.link()
.get()
.set_filter_mask(AF_BRIDGE as u8, RTEXT_FILTER_BRVLAN)
.set_filter_mask(AddressFamily::Bridge, vec![LinkExtentMask::Brvlan])
.execute();
'outer: while let Some(msg) = links.try_next().await? {
for nla in msg.nlas.into_iter() {
if let Nla::AfSpecBridge(data) = nla {
for nla in msg.attributes.into_iter() {
if let LinkAttribute::AfSpecBridge(data) = nla {
println!(
"found interface {} with AfSpecBridge data {:?})",
msg.header.index, data
Expand Down
17 changes: 16 additions & 1 deletion examples/ip_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@

use futures::stream::StreamExt;

use netlink_packet_route::constants::*;
use netlink_sys::{AsyncSocket, SocketAddr};
use rtnetlink::new_connection;

const RTNLGRP_LINK: u32 = 1;
const RTNLGRP_NEIGH: u32 = 3;
const RTNLGRP_IPV4_IFADDR: u32 = 5;
const RTNLGRP_IPV4_MROUTE: u32 = 6;
const RTNLGRP_IPV4_ROUTE: u32 = 7;
const RTNLGRP_IPV4_RULE: u32 = 8;
const RTNLGRP_IPV6_IFADDR: u32 = 9;
const RTNLGRP_IPV6_MROUTE: u32 = 10;
const RTNLGRP_IPV6_ROUTE: u32 = 11;
const RTNLGRP_IPV6_RULE: u32 = 19;
const RTNLGRP_IPV4_NETCONF: u32 = 24;
const RTNLGRP_IPV6_NETCONF: u32 = 25;
const RTNLGRP_MPLS_ROUTE: u32 = 27;
const RTNLGRP_NSID: u32 = 28;
const RTNLGRP_MPLS_NETCONF: u32 = 29;

const fn nl_mgrp(group: u32) -> u32 {
if group > 31 {
panic!("use netlink_sys::Socket::add_membership() for this group");
Expand Down
9 changes: 3 additions & 6 deletions examples/property_altname.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// SPDX-License-Identifier: MIT

use futures::stream::TryStreamExt;
use netlink_packet_route::{
link::nlas::{Nla, Prop},
LinkMessage,
};
use netlink_packet_route::link::{LinkAttribute, LinkMessage, Prop};
use rtnetlink::{new_connection, Error, Handle};
use std::env;

Expand Down Expand Up @@ -66,8 +63,8 @@ async fn show_property_alt_ifnames(
link_name: &str,
handle: Handle,
) -> Result<(), Error> {
for nla in get_link(link_name, handle).await?.nlas.into_iter() {
if let Nla::PropList(ref prop_list) = nla {
for nla in get_link(link_name, handle).await?.attributes.into_iter() {
if let LinkAttribute::PropList(ref prop_list) = nla {
for prop in prop_list {
if let Prop::AltIfName(altname) = prop {
println!("altname: {altname}");
Expand Down
55 changes: 23 additions & 32 deletions src/addr/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use netlink_packet_core::{
};

use netlink_packet_route::{
nlas::address::Nla, AddressMessage, RtnlMessage, AF_INET, AF_INET6,
address::{AddressAttribute, AddressMessage},
AddressFamily, RouteNetlinkMessage,
};

use crate::{try_nl, Error, Handle};
Expand All @@ -34,46 +35,35 @@ impl AddressAddRequest {
message.header.prefix_len = prefix_len;
message.header.index = index;

let address_vec = match address {
IpAddr::V4(ipv4) => {
message.header.family = AF_INET as u8;
ipv4.octets().to_vec()
}
IpAddr::V6(ipv6) => {
message.header.family = AF_INET6 as u8;
ipv6.octets().to_vec()
}
message.header.family = match address {
IpAddr::V4(_) => AddressFamily::Inet,
IpAddr::V6(_) => AddressFamily::Inet6,
};

if address.is_multicast() {
message.nlas.push(Nla::Multicast(address_vec));
} else if address.is_unspecified() {
message.nlas.push(Nla::Unspec(address_vec));
} else if address.is_ipv6() {
message.nlas.push(Nla::Address(address_vec));
if let IpAddr::V6(a) = address {
message.attributes.push(AddressAttribute::Multicast(a));
}
} else {
message.nlas.push(Nla::Address(address_vec.clone()));
message.attributes.push(AddressAttribute::Address(address));

// for IPv4 the IFA_LOCAL address can be set to the same value as
// IFA_ADDRESS
message.nlas.push(Nla::Local(address_vec.clone()));
message.attributes.push(AddressAttribute::Local(address));

// set the IFA_BROADCAST address as well (IPv6 does not support
// broadcast)
if prefix_len == 32 {
message.nlas.push(Nla::Broadcast(address_vec));
} else {
let ip_addr: u32 = u32::from(Ipv4Addr::new(
address_vec[0],
address_vec[1],
address_vec[2],
address_vec[3],
));
let brd = Ipv4Addr::from(
(0xffff_ffff_u32) >> u32::from(prefix_len) | ip_addr,
);
message.nlas.push(Nla::Broadcast(brd.octets().to_vec()));
};
if let IpAddr::V4(a) = address {
if prefix_len == 32 {
message.attributes.push(AddressAttribute::Broadcast(a));
} else {
let ip_addr = u32::from(a);
let brd = Ipv4Addr::from(
(0xffff_ffff_u32) >> u32::from(prefix_len) | ip_addr,
);
message.attributes.push(AddressAttribute::Broadcast(brd));
};
}
}
AddressAddRequest {
handle,
Expand All @@ -97,7 +87,8 @@ impl AddressAddRequest {
message,
replace,
} = self;
let mut req = NetlinkMessage::from(RtnlMessage::NewAddress(message));
let mut req =
NetlinkMessage::from(RouteNetlinkMessage::NewAddress(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;

Expand Down
5 changes: 3 additions & 2 deletions src/addr/del.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use futures::stream::StreamExt;
use netlink_packet_core::{NetlinkMessage, NLM_F_ACK, NLM_F_REQUEST};
use netlink_packet_route::{AddressMessage, RtnlMessage};
use netlink_packet_route::{address::AddressMessage, RouteNetlinkMessage};

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

Expand All @@ -23,7 +23,8 @@ impl AddressDelRequest {
message,
} = self;

let mut req = NetlinkMessage::from(RtnlMessage::DelAddress(message));
let mut req =
NetlinkMessage::from(RouteNetlinkMessage::DelAddress(message));
req.header.flags = NLM_F_REQUEST | NLM_F_ACK;
let mut response = handle.request(req)?;
while let Some(msg) = response.next().await {
Expand Down
Loading

0 comments on commit 5275c05

Please sign in to comment.