netdev 0.46.1

Cross-platform library for enumerating network interfaces with metadata.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use crate::interface::types::InterfaceType;
use crate::stats::counters::InterfaceStats;
use netlink_packet_core::NetlinkPayload;
use netlink_packet_route::{
    RouteNetlinkMessage,
    address::{AddressAttribute, AddressFlags, AddressMessage},
    link::{LinkAttribute, LinkMessage},
};
use netlink_sys::{Socket, protocols::NETLINK_ROUTE};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::time::SystemTime;
use std::{collections::HashMap, io};

#[cfg(feature = "gateway")]
use netlink_packet_route::AddressFamily;
#[cfg(feature = "gateway")]
use netlink_packet_route::neighbour::{NeighbourAddress, NeighbourAttribute, NeighbourMessage};
#[cfg(feature = "gateway")]
use netlink_packet_route::route::{RouteAddress, RouteAttribute, RouteMessage};

const SEQ_BASE: u32 = 0x6E_64_65_76; // "ndev"

fn open_route_socket() -> io::Result<Socket> {
    let sock = Socket::new(NETLINK_ROUTE)
        .map_err(|e| io::Error::new(io::ErrorKind::Other, format!("netlink open: {e}")))?;
    // On Android 11+, bind is denied by SELinux
    //sock.bind_auto().map_err(|e| io::Error::new(io::ErrorKind::Other, format!("bind_auto: {e}")))?;
    crate::os::linux::netlink_io::set_non_blocking(&sock)?;
    Ok(sock)
}

pub fn dump_links() -> io::Result<Vec<LinkMessage>> {
    let mut sock = open_route_socket()?;
    let seq = SEQ_BASE ^ 0x01;
    crate::os::linux::netlink_io::send_dump(
        &mut sock,
        RouteNetlinkMessage::GetLink(LinkMessage::default()),
        seq,
    )?;
    let msgs = crate::os::linux::netlink_io::recv_multi(&mut sock, seq)?;
    let mut out = Vec::new();
    for m in msgs {
        if let NetlinkPayload::InnerMessage(RouteNetlinkMessage::NewLink(link)) = m.payload {
            out.push(link);
        }
    }
    Ok(out)
}

pub fn dump_addrs() -> io::Result<Vec<AddressMessage>> {
    let mut sock = open_route_socket()?;
    let seq = SEQ_BASE ^ 0x02;
    crate::os::linux::netlink_io::send_dump(
        &mut sock,
        RouteNetlinkMessage::GetAddress(AddressMessage::default()),
        seq,
    )?;
    let msgs = crate::os::linux::netlink_io::recv_multi(&mut sock, seq)?;
    let mut out = Vec::new();
    for m in msgs {
        if let NetlinkPayload::InnerMessage(RouteNetlinkMessage::NewAddress(addr)) = m.payload {
            out.push(addr);
        }
    }
    Ok(out)
}

#[cfg(feature = "gateway")]
pub fn dump_routes() -> io::Result<Vec<RouteMessage>> {
    let mut sock = open_route_socket()?;
    let seq = SEQ_BASE ^ 0x03;
    crate::os::linux::netlink_io::send_dump(
        &mut sock,
        RouteNetlinkMessage::GetRoute(RouteMessage::default()),
        seq,
    )?;
    let msgs = crate::os::linux::netlink_io::recv_multi(&mut sock, seq)?;
    let mut out = Vec::new();
    for m in msgs {
        if let NetlinkPayload::InnerMessage(RouteNetlinkMessage::NewRoute(rt)) = m.payload {
            out.push(rt);
        }
    }
    Ok(out)
}

#[cfg(feature = "gateway")]
pub fn dump_neigh() -> io::Result<Vec<NeighbourMessage>> {
    let mut sock = open_route_socket()?;
    let seq = SEQ_BASE ^ 0x04;
    crate::os::linux::netlink_io::send_dump(
        &mut sock,
        RouteNetlinkMessage::GetNeighbour(NeighbourMessage::default()),
        seq,
    )?;
    let msgs = crate::os::linux::netlink_io::recv_multi(&mut sock, seq)?;
    let mut out = Vec::new();
    for m in msgs {
        if let NetlinkPayload::InnerMessage(RouteNetlinkMessage::NewNeighbour(n)) = m.payload {
            out.push(n);
        }
    }
    Ok(out)
}

fn mac_from_link(link: &LinkMessage) -> Option<[u8; 6]> {
    for nla in &link.attributes {
        if let LinkAttribute::Address(bytes) = nla {
            if bytes.len() == 6 {
                return Some([bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5]]);
            }
        }
    }
    None
}

fn name_from_link(link: &LinkMessage) -> Option<String> {
    for nla in &link.attributes {
        if let LinkAttribute::IfName(n) = nla {
            return Some(n.clone());
        }
    }
    None
}

fn ip_from_addr(addr: &AddressMessage) -> Option<(IpAddr, u8, u32)> {
    let pfx = addr.header.prefix_len;
    let mut ip_out = None;
    let mut flags: Option<AddressFlags> = None;
    for nla in &addr.attributes {
        match nla {
            AddressAttribute::Local(ip) | AddressAttribute::Address(ip) => {
                ip_out = Some(*ip);
            }
            AddressAttribute::Flags(f) => {
                flags = Some(*f);
            }
            _ => {}
        }
    }
    let addr_flags = flags
        .map(|f| f.bits())
        .unwrap_or(addr.header.flags.bits() as u32);
    ip_out.map(|ip| (ip, pfx, addr_flags))
}

#[cfg(feature = "gateway")]
fn route_addr_to_ip(a: &RouteAddress) -> Option<IpAddr> {
    match a {
        RouteAddress::Inet(v4) => Some(IpAddr::V4(*v4)),
        RouteAddress::Inet6(v6) => Some(IpAddr::V6(*v6)),
        _ => None,
    }
}

#[cfg(feature = "gateway")]
fn route_extract(rt: &RouteMessage) -> (Option<IpAddr>, Option<u8>, Option<IpAddr>, Option<u32>) {
    // (dst, prefix, gateway, oif)
    let mut dst: Option<IpAddr> = None;
    let pfx: Option<u8> = Some(rt.header.destination_prefix_length);
    let mut gw: Option<IpAddr> = None;
    let mut oif: Option<u32> = None;

    for nla in &rt.attributes {
        match nla {
            RouteAttribute::Destination(a) => dst = route_addr_to_ip(a),
            RouteAttribute::Gateway(a) => gw = route_addr_to_ip(a),
            RouteAttribute::Oif(i) => oif = Some(*i),
            _ => {}
        }
    }

    // if dst is None and pfx is 0, it means default route
    if dst.is_none() && pfx == Some(0) {
        dst = match rt.header.address_family {
            AddressFamily::Inet => Some(IpAddr::V4(Ipv4Addr::UNSPECIFIED)),
            AddressFamily::Inet6 => Some(IpAddr::V6(Ipv6Addr::UNSPECIFIED)),
            _ => None,
        };
    }

    (dst, pfx, gw, oif)
}

#[cfg(feature = "gateway")]
fn neigh_addr_to_ip(a: &NeighbourAddress) -> Option<IpAddr> {
    match a {
        NeighbourAddress::Inet(v4) => Some(IpAddr::V4(*v4)),
        NeighbourAddress::Inet6(v6) => Some(IpAddr::V6(*v6)),
        #[allow(unreachable_patterns)]
        _ => None,
    }
}

#[cfg(feature = "gateway")]
fn neigh_extract(n: &NeighbourMessage) -> (Option<IpAddr>, Option<[u8; 6]>, Option<u32>) {
    let mut ip = None;
    let mut mac = None;
    let mut oif = Some(n.header.ifindex as u32);

    for nla in &n.attributes {
        match nla {
            NeighbourAttribute::Destination(a) => {
                ip = neigh_addr_to_ip(a);
            }
            // Link-layer address (MAC)
            NeighbourAttribute::LinkLayerAddress(v) => {
                if v.len() == 6 {
                    mac = Some([v[0], v[1], v[2], v[3], v[4], v[5]]);
                }
            }
            NeighbourAttribute::IfIndex(i) => oif = Some(*i as u32),
            _ => {}
        }
    }
    (ip, mac, oif)
}

fn mtu_from_link(link: &LinkMessage) -> Option<u32> {
    for nla in &link.attributes {
        if let LinkAttribute::Mtu(m) = nla {
            return Some(*m);
        }
    }
    None
}

fn if_type_from_link(link: &LinkMessage, _name: &str) -> InterfaceType {
    let arphrd = link.header.link_layer_type as u32;
    InterfaceType::try_from(arphrd).unwrap_or(InterfaceType::UnknownWithValue(arphrd))
}

fn stats_from_link(link: &LinkMessage) -> Option<InterfaceStats> {
    for nla in &link.attributes {
        match nla {
            LinkAttribute::Stats64(s) => {
                return Some(InterfaceStats {
                    rx_bytes: s.rx_bytes,
                    tx_bytes: s.tx_bytes,
                    timestamp: Some(SystemTime::now()),
                });
            }
            LinkAttribute::Stats(s) => {
                return Some(InterfaceStats {
                    rx_bytes: s.rx_bytes as u64,
                    tx_bytes: s.tx_bytes as u64,
                    timestamp: Some(SystemTime::now()),
                });
            }
            _ => {}
        }
    }
    None
}

pub fn get_flags_by_name(name: &str) -> io::Result<Option<u32>> {
    let links = dump_links()?;
    for l in links {
        if let Some(ifname) = name_from_link(&l) {
            if ifname == name {
                return Ok(Some(l.header.flags.bits()));
            }
        }
    }
    Ok(None)
}

#[derive(Debug, Clone)]
pub struct IfRow {
    pub index: u32,
    pub name: String,
    pub mac: Option<[u8; 6]>,
    pub ipv4: Vec<(Ipv4Addr, u8)>,
    pub ipv6: Vec<(Ipv6Addr, u8)>,
    pub ipv6_addr_flags: Vec<u32>,
    pub flags: u32,
    pub mtu: Option<u32>,
    pub if_type: InterfaceType,
    pub stats: Option<InterfaceStats>,
}

pub fn collect_interfaces() -> io::Result<Vec<IfRow>> {
    let links = dump_links()?;
    let addrs = dump_addrs().unwrap_or_default();

    let mut base: HashMap<u32, IfRow> = HashMap::new();
    for l in links {
        let idx = l.header.index as u32;
        let name = name_from_link(&l).unwrap_or_else(|| idx.to_string());
        let mac = mac_from_link(&l);
        let flags = l.header.flags.bits();
        let mtu_nl = mtu_from_link(&l);
        let if_type = if_type_from_link(&l, &name);
        let stats = stats_from_link(&l);

        base.insert(
            idx,
            IfRow {
                index: idx,
                name,
                mac,
                ipv4: vec![],
                ipv6: vec![],
                ipv6_addr_flags: vec![],
                flags,
                mtu: mtu_nl,
                if_type,
                stats,
            },
        );
    }

    for a in addrs {
        let idx = a.header.index as u32;
        if let Some((ip, pfx, addr_flags)) = ip_from_addr(&a) {
            if let Some(row) = base.get_mut(&idx) {
                match ip {
                    IpAddr::V4(v4) => row.ipv4.push((v4, pfx)),
                    IpAddr::V6(v6) => {
                        row.ipv6.push((v6, pfx));
                        row.ipv6_addr_flags.push(addr_flags);
                    }
                }
            }
        }
    }

    Ok(base.into_values().collect())
}

#[cfg(feature = "gateway")]
#[derive(Debug, Clone)]
pub struct GwRow {
    #[allow(dead_code)]
    pub ifindex: u32,
    pub gw_v4: Vec<Ipv4Addr>,
    pub gw_v6: Vec<Ipv6Addr>,
    pub mac: Option<[u8; 6]>,
}

#[cfg(feature = "gateway")]
pub fn collect_routes() -> io::Result<HashMap<u32, GwRow>> {
    let routes = dump_routes()?;
    let neighs = dump_neigh().unwrap_or_default();

    let mut m: HashMap<u32, GwRow> = HashMap::new();
    for rt in routes {
        let (_dst, pfx, gw, oif) = route_extract(&rt);
        // default route only
        if pfx != Some(0) {
            continue;
        }
        let oif = match oif {
            Some(i) => i,
            None => continue,
        };
        if let Some(gwip) = gw {
            let e = m.entry(oif).or_insert(GwRow {
                ifindex: oif,
                gw_v4: vec![],
                gw_v6: vec![],
                mac: None,
            });
            match gwip {
                IpAddr::V4(v4) => {
                    if !e.gw_v4.contains(&v4) {
                        e.gw_v4.push(v4);
                    }
                }
                IpAddr::V6(v6) => {
                    if !e.gw_v6.contains(&v6) {
                        e.gw_v6.push(v6);
                    }
                }
            }
        }
    }

    let mut mac_candidates = crate::os::linux::gateway::GatewayMacCandidates::default();
    for n in neighs {
        let (ip, mac, ifi) = neigh_extract(&n);
        let ifi = match ifi {
            Some(i) => i,
            None => continue,
        };
        let (Some(row), Some(m6), Some(ip)) = (m.get_mut(&ifi), mac, ip) else {
            continue;
        };
        match ip {
            IpAddr::V4(v4) if row.gw_v4.contains(&v4) => {
                mac_candidates.record_ipv4(ifi, m6);
            }
            IpAddr::V6(v6) if row.gw_v6.contains(&v6) => {
                mac_candidates.record_ipv6(ifi, m6);
            }
            _ => {}
        }
    }

    for (ifindex, row) in &mut m {
        // Preserve the established IPv4 result when both families use different routers.
        row.mac = mac_candidates.get(*ifindex);
    }

    Ok(m)
}