Skip to content

Commit

Permalink
Attribute vxlan listening ports
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaesar committed Nov 1, 2024
1 parent b60536b commit 8ea95ae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
21 changes: 13 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ fn main() -> Result<()> {
}

// output wireguards
let mut wireguard_sockets = HashMap::<_, Vec<_>>::new();
let mut interface_sockets = HashMap::<_, Vec<_>>::new();
socks.retain(|_sockid, sockinfo| {
if let Some(if_id) = iface_info.wireguard_ports.get(&sockinfo.port) {
wireguard_sockets
if let Some(if_id) = iface_info.interface_ports.get(&sockinfo.port) {
interface_sockets
.entry(if_id)
.or_default()
.push(sockinfo.to_owned());
Expand All @@ -79,11 +79,11 @@ fn main() -> Result<()> {
true
}
});
for (if_id, socks) in &wireguard_sockets {
for (if_id, socks) in &interface_sockets {
if filters.accept_wg() {
let name = match iface_info.id2name.get(if_id) {
Some(ifname) => format!("[wireguard {ifname}]"),
None => format!("wireguard, index {if_id}"),
Some(ifname) => format!("[network interface {ifname}]"),
None => format!("[network interface #{if_id}]"),
};
output.node(name, sockets_tree(socks, &filters));
}
Expand Down Expand Up @@ -126,7 +126,7 @@ fn main() -> Result<()> {
#[derive(Default)]
struct IfaceInfo {
id2name: HashMap<u32, String>,
wireguard_ports: HashMap<u16, u32>,
interface_ports: HashMap<u16, u32>,
local_routes: netlink::route::Rtbl,
}

Expand All @@ -137,12 +137,17 @@ fn interfaces_routes() -> IfaceInfo {
let netlink::route::Interfaces {
id2name,
wireguard_ids,
vxlan_ports,
} = netlink::route::interface_names(route_socket).unwrap_or_default();
let local_routes = netlink::route::local_routes(route_socket).unwrap_or_default();
let wireguard_ports = wireguards(&wireguard_ids).unwrap_or_default();
IfaceInfo {
id2name,
wireguard_ports,
// TODO: be angry on collisions
interface_ports: wireguard_ports
.into_iter()
.chain(vxlan_ports.into_iter())
.collect(),
local_routes,
}
}
Expand Down
20 changes: 16 additions & 4 deletions src/netlink/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use netlink_packet_core::{
NetlinkHeader, NetlinkMessage, NetlinkPayload, NLM_F_DUMP, NLM_F_REQUEST,
};
use netlink_packet_route::{
link::{InfoKind, LinkAttribute, LinkExtentMask, LinkInfo, LinkMessage},
link::{InfoData, InfoKind, InfoVxlan, LinkAttribute, LinkExtentMask, LinkInfo, LinkMessage},
route::{RouteAddress, RouteAttribute, RouteMessage, RouteType},
RouteNetlinkMessage,
};
Expand All @@ -15,6 +15,7 @@ use std::{cmp::Reverse, collections::HashMap, net::IpAddr};
pub struct Interfaces {
pub id2name: HashMap<u32, String>,
pub wireguard_ids: Vec<u32>,
pub vxlan_ports: HashMap<u16, u32>,
}

pub fn interface_names(socket: &Socket) -> Result<Interfaces> {
Expand All @@ -32,6 +33,7 @@ pub fn interface_names(socket: &Socket) -> Result<Interfaces> {

let mut map = HashMap::new();
let mut wg_ids = Vec::new();
let mut vxlan_ports = HashMap::new();
drive_req(packet, socket, |inner| {
if let RouteNetlinkMessage::NewLink(nl) = inner {
for nla in nl.attributes {
Expand All @@ -41,8 +43,18 @@ pub fn interface_names(socket: &Socket) -> Result<Interfaces> {
}
LinkAttribute::LinkInfo(infos) => {
for info in infos {
if info == LinkInfo::Kind(InfoKind::Wireguard) {
wg_ids.push(nl.header.index);
match info {
LinkInfo::Kind(InfoKind::Wireguard) => {
wg_ids.push(nl.header.index);
}
LinkInfo::Data(InfoData::Vxlan(data)) => {
for datum in data {
if let InfoVxlan::Port(port) = datum {
vxlan_ports.insert(port, nl.header.index);
}
}
}
_ => (),
}
}
}
Expand All @@ -56,6 +68,7 @@ pub fn interface_names(socket: &Socket) -> Result<Interfaces> {
Ok(Interfaces {
id2name: map,
wireguard_ids: wg_ids,
vxlan_ports,
})
}

Expand Down Expand Up @@ -170,7 +183,6 @@ pub fn local_routes(socket: &Socket) -> Result<Rtbl> {
_ => None,
});
if let (Some(&iface), Some(dst)) = (iface, dst) {
// TODO: more anyhow, less expect/unreachable
let dst = match *dst {
RouteAddress::Inet(a) => IpAddr::from(a),
RouteAddress::Inet6(a) => IpAddr::from(a),
Expand Down

0 comments on commit 8ea95ae

Please sign in to comment.