Skip to content

Commit 8ea95ae

Browse files
committed
Attribute vxlan listening ports
1 parent b60536b commit 8ea95ae

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

src/main.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ fn main() -> Result<()> {
6767
}
6868

6969
// output wireguards
70-
let mut wireguard_sockets = HashMap::<_, Vec<_>>::new();
70+
let mut interface_sockets = HashMap::<_, Vec<_>>::new();
7171
socks.retain(|_sockid, sockinfo| {
72-
if let Some(if_id) = iface_info.wireguard_ports.get(&sockinfo.port) {
73-
wireguard_sockets
72+
if let Some(if_id) = iface_info.interface_ports.get(&sockinfo.port) {
73+
interface_sockets
7474
.entry(if_id)
7575
.or_default()
7676
.push(sockinfo.to_owned());
@@ -79,11 +79,11 @@ fn main() -> Result<()> {
7979
true
8080
}
8181
});
82-
for (if_id, socks) in &wireguard_sockets {
82+
for (if_id, socks) in &interface_sockets {
8383
if filters.accept_wg() {
8484
let name = match iface_info.id2name.get(if_id) {
85-
Some(ifname) => format!("[wireguard {ifname}]"),
86-
None => format!("wireguard, index {if_id}"),
85+
Some(ifname) => format!("[network interface {ifname}]"),
86+
None => format!("[network interface #{if_id}]"),
8787
};
8888
output.node(name, sockets_tree(socks, &filters));
8989
}
@@ -126,7 +126,7 @@ fn main() -> Result<()> {
126126
#[derive(Default)]
127127
struct IfaceInfo {
128128
id2name: HashMap<u32, String>,
129-
wireguard_ports: HashMap<u16, u32>,
129+
interface_ports: HashMap<u16, u32>,
130130
local_routes: netlink::route::Rtbl,
131131
}
132132

@@ -137,12 +137,17 @@ fn interfaces_routes() -> IfaceInfo {
137137
let netlink::route::Interfaces {
138138
id2name,
139139
wireguard_ids,
140+
vxlan_ports,
140141
} = netlink::route::interface_names(route_socket).unwrap_or_default();
141142
let local_routes = netlink::route::local_routes(route_socket).unwrap_or_default();
142143
let wireguard_ports = wireguards(&wireguard_ids).unwrap_or_default();
143144
IfaceInfo {
144145
id2name,
145-
wireguard_ports,
146+
// TODO: be angry on collisions
147+
interface_ports: wireguard_ports
148+
.into_iter()
149+
.chain(vxlan_ports.into_iter())
150+
.collect(),
146151
local_routes,
147152
}
148153
}

src/netlink/route.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use netlink_packet_core::{
44
NetlinkHeader, NetlinkMessage, NetlinkPayload, NLM_F_DUMP, NLM_F_REQUEST,
55
};
66
use netlink_packet_route::{
7-
link::{InfoKind, LinkAttribute, LinkExtentMask, LinkInfo, LinkMessage},
7+
link::{InfoData, InfoKind, InfoVxlan, LinkAttribute, LinkExtentMask, LinkInfo, LinkMessage},
88
route::{RouteAddress, RouteAttribute, RouteMessage, RouteType},
99
RouteNetlinkMessage,
1010
};
@@ -15,6 +15,7 @@ use std::{cmp::Reverse, collections::HashMap, net::IpAddr};
1515
pub struct Interfaces {
1616
pub id2name: HashMap<u32, String>,
1717
pub wireguard_ids: Vec<u32>,
18+
pub vxlan_ports: HashMap<u16, u32>,
1819
}
1920

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

3334
let mut map = HashMap::new();
3435
let mut wg_ids = Vec::new();
36+
let mut vxlan_ports = HashMap::new();
3537
drive_req(packet, socket, |inner| {
3638
if let RouteNetlinkMessage::NewLink(nl) = inner {
3739
for nla in nl.attributes {
@@ -41,8 +43,18 @@ pub fn interface_names(socket: &Socket) -> Result<Interfaces> {
4143
}
4244
LinkAttribute::LinkInfo(infos) => {
4345
for info in infos {
44-
if info == LinkInfo::Kind(InfoKind::Wireguard) {
45-
wg_ids.push(nl.header.index);
46+
match info {
47+
LinkInfo::Kind(InfoKind::Wireguard) => {
48+
wg_ids.push(nl.header.index);
49+
}
50+
LinkInfo::Data(InfoData::Vxlan(data)) => {
51+
for datum in data {
52+
if let InfoVxlan::Port(port) = datum {
53+
vxlan_ports.insert(port, nl.header.index);
54+
}
55+
}
56+
}
57+
_ => (),
4658
}
4759
}
4860
}
@@ -56,6 +68,7 @@ pub fn interface_names(socket: &Socket) -> Result<Interfaces> {
5668
Ok(Interfaces {
5769
id2name: map,
5870
wireguard_ids: wg_ids,
71+
vxlan_ports,
5972
})
6073
}
6174

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

0 commit comments

Comments
 (0)