getifs 0.6.0

Cross-platform enumeration of network interfaces and their MTU, gateway, multicast, and local/private/public IP addresses.
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
use std::{
  io,
  net::{IpAddr, Ipv4Addr, Ipv6Addr},
};

use hardware_address::xtoi2;
use ipnet::{Ipv4Net, Ipv6Net};
use rustix::net::AddressFamily;
use smallvec_wrapper::{SmallVec, TinyVec};
use smol_str::SmolStr;

use super::{
  IfAddr, IfNet, Ifv4Addr, Ifv4Net, Ifv6Addr, Ifv6Net, Interface, IpRoute, Ipv4Route, Ipv6Route,
  MacAddr, Net, MAC_ADDRESS_SIZE,
};

pub(super) use local_addr::*;

#[path = "linux/netlink.rs"]
mod netlink;

#[path = "linux/local_addr.rs"]
mod local_addr;

use netlink::{netlink_addr, netlink_interface, netlink_walk_routes};

macro_rules! rt_generic_mod {
  ($($name:ident($rta:expr, $rtn:expr)), +$(,)?) => {
    $(
      paste::paste! {
        pub(super) use [< rt_ $name >]::*;

        mod [< rt_ $name >] {
          use rustix::net::AddressFamily;
          use smallvec_wrapper::SmallVec;
          use std::{
            io,
            net::{IpAddr, Ipv4Addr, Ipv6Addr},
          };

          use crate::{ipv4_filter_to_ip_filter, ipv6_filter_to_ip_filter};

          use super::{
            super::{IfAddr, Ifv4Addr, Ifv6Addr},
            netlink::rt_generic_addrs,
          };

          pub(crate) fn [< $name _addrs >]() -> io::Result<SmallVec<IfAddr>> {
            rt_generic_addrs(AddressFamily::UNSPEC, $rta, $rtn, |_| true)
          }

          pub(crate) fn [< $name _ipv4_addrs >]() -> io::Result<SmallVec<Ifv4Addr>> {
            rt_generic_addrs(AddressFamily::INET, $rta, $rtn, |_| true)
          }

          pub(crate) fn [< $name _ipv6_addrs >]() -> io::Result<SmallVec<Ifv6Addr>> {
            rt_generic_addrs(AddressFamily::INET6, $rta, $rtn, |_| true)
          }

          pub(crate) fn [< $name _addrs_by_filter >]<F>(f: F) -> io::Result<SmallVec<IfAddr>>
          where
            F: FnMut(&IpAddr) -> bool,
          {
            rt_generic_addrs(AddressFamily::UNSPEC, $rta, $rtn, f)
          }

          pub(crate) fn [< $name _ipv4_addrs_by_filter >]<F>(f: F) -> io::Result<SmallVec<Ifv4Addr>>
          where
            F: FnMut(&Ipv4Addr) -> bool,
          {
            rt_generic_addrs(AddressFamily::INET, $rta, $rtn, ipv4_filter_to_ip_filter(f))
          }

          pub(crate) fn [< $name _ipv6_addrs_by_filter >]<F>(f: F) -> io::Result<SmallVec<Ifv6Addr>>
          where
            F: FnMut(&Ipv6Addr) -> bool,
          {
            rt_generic_addrs(AddressFamily::INET6, $rta, $rtn, ipv6_filter_to_ip_filter(f))
          }
        }
      }
    )*
  };
}

rt_generic_mod!(gateway(
  linux_raw_sys::netlink::rtattr_type_t::RTA_GATEWAY as u16,
  None
),);

#[inline]
fn route_v4_from_raw(
  oif: u32,
  dst_len: u8,
  dst: Option<IpAddr>,
  gw: Option<IpAddr>,
) -> Option<Ipv4Route> {
  if dst_len > 32 {
    return None;
  }
  // Treat absent `dst` as the default route only when `dst_len == 0`.
  // The walker rejects "dst absent + dst_len != 0" as malformed; this
  // is a defence-in-depth check for any future caller that doesn't
  // pre-validate.
  let dst_ip = match dst {
    Some(IpAddr::V4(ip)) => ip,
    Some(_) => return None,
    None if dst_len == 0 => Ipv4Addr::UNSPECIFIED,
    None => return None,
  };
  let net = Ipv4Net::new(dst_ip, dst_len).ok()?;
  let gw = match gw {
    Some(IpAddr::V4(ip)) => Some(ip),
    Some(_) => return None,
    None => None,
  };
  Some(Ipv4Route::new(oif, net, gw))
}

#[inline]
fn route_v6_from_raw(
  oif: u32,
  dst_len: u8,
  dst: Option<IpAddr>,
  gw: Option<IpAddr>,
) -> Option<Ipv6Route> {
  if dst_len > 128 {
    return None;
  }
  let dst_ip = match dst {
    Some(IpAddr::V6(ip)) => ip,
    Some(_) => return None,
    None if dst_len == 0 => Ipv6Addr::UNSPECIFIED,
    None => return None,
  };
  let net = Ipv6Net::new(dst_ip, dst_len).ok()?;
  let gw = match gw {
    Some(IpAddr::V6(ip)) => Some(ip),
    Some(_) => return None,
    None => None,
  };
  Some(Ipv6Route::new(oif, net, gw))
}

pub(super) fn route_table_by_filter<F>(mut f: F) -> io::Result<SmallVec<IpRoute>>
where
  F: FnMut(&IpRoute) -> bool,
{
  // Walk `AF_INET` and `AF_INET6` separately rather than relying on
  // `AF_UNSPEC` to deliver both. Linux's `RTM_GETROUTE` with
  // `rtm_family = AF_UNSPEC` is documented as a "give me all
  // families" request, but in practice some kernel versions /
  // configurations can return only IPv4 — pyroute2 and similar
  // bindings document the same workaround
  // (https://pyroute2.org/docs/iproute_linux.html). On a dual-stack
  // host the union API would silently drop every IPv6 route while
  // `route_ipv6_table()` still surfaced them; the BSD path already
  // walks per-family for the same reason. Two dumps is the right
  // tradeoff for a consistent answer.
  let mut out: SmallVec<IpRoute> = SmallVec::new();
  netlink_walk_routes(AddressFamily::INET, |fam, oif, dst_len, dst, gw| {
    if fam as u16 == AddressFamily::INET.as_raw() {
      if let Some(r) = route_v4_from_raw(oif, dst_len, dst, gw).map(IpRoute::V4) {
        if f(&r) {
          out.push(r);
        }
      }
    }
  })?;
  netlink_walk_routes(AddressFamily::INET6, |fam, oif, dst_len, dst, gw| {
    if fam as u16 == AddressFamily::INET6.as_raw() {
      if let Some(r) = route_v6_from_raw(oif, dst_len, dst, gw).map(IpRoute::V6) {
        if f(&r) {
          out.push(r);
        }
      }
    }
  })?;
  Ok(out)
}

pub(super) fn route_ipv4_table_by_filter<F>(mut f: F) -> io::Result<SmallVec<Ipv4Route>>
where
  F: FnMut(&Ipv4Route) -> bool,
{
  let mut out: SmallVec<Ipv4Route> = SmallVec::new();
  netlink_walk_routes(AddressFamily::INET, |fam, oif, dst_len, dst, gw| {
    if fam as u16 != AddressFamily::INET.as_raw() {
      return;
    }
    if let Some(r) = route_v4_from_raw(oif, dst_len, dst, gw) {
      if f(&r) {
        out.push(r);
      }
    }
  })?;
  Ok(out)
}

pub(super) fn route_ipv6_table_by_filter<F>(mut f: F) -> io::Result<SmallVec<Ipv6Route>>
where
  F: FnMut(&Ipv6Route) -> bool,
{
  let mut out: SmallVec<Ipv6Route> = SmallVec::new();
  netlink_walk_routes(AddressFamily::INET6, |fam, oif, dst_len, dst, gw| {
    if fam as u16 != AddressFamily::INET6.as_raw() {
      return;
    }
    if let Some(r) = route_v6_from_raw(oif, dst_len, dst, gw) {
      if f(&r) {
        out.push(r);
      }
    }
  })?;
  Ok(out)
}

impl Interface {
  #[inline]
  fn new(index: u32, flags: Flags) -> Self {
    Self {
      index,
      mtu: 0,
      name: SmolStr::default(),
      mac_addr: None,
      flags,
    }
  }
}

bitflags::bitflags! {
  /// Flags represents the interface flags.
  #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
  pub struct Flags: u32 {
    /// Interface is administratively up
    const UP = 0x1;
    /// Interface supports broadcast access capability
    const BROADCAST = 0x2;
    /// Turn on debugging
    const DEBUG = 0x4;
    /// Interface is a loopback net
    const LOOPBACK = 0x8;
    /// Interface is point-to-point link
    const POINTOPOINT = 0x10;
    /// Obsolete: avoid use of trailers
    const NOTRAILERS = 0x20;
    /// Resources allocated
    const RUNNING = 0x40;
    /// No address resolution protocol
    const NOARP = 0x80;
    /// Receive all packets
    const PROMISC = 0x100;
    /// Receive all multicast packets
    const ALLMULTI = 0x200;
    /// Transmission is in progress
    const MASTER = 0x400;
    /// Can't hear own transmissions
    const SLAVE = 0x800;
    /// Per link layer defined bit
    const MULTICAST = 0x1000;
    /// Per link layer defined bit
    const PORTSEL = 0x2000;
    /// Per link layer defined bit
    const AUTOMEDIA = 0x4000;
    /// Supports multicast access capability
    const DYNAMIC = 0x8000;
  }
}

pub(super) fn interface_table(index: u32) -> io::Result<TinyVec<Interface>> {
  netlink_interface(AddressFamily::UNSPEC, index)
}

pub(super) fn interface_ipv4_addresses<F>(index: u32, f: F) -> io::Result<SmallVec<Ifv4Net>>
where
  F: FnMut(&IpAddr) -> bool,
{
  netlink_addr(AddressFamily::INET, index, f)
}

pub(super) fn interface_ipv6_addresses<F>(index: u32, f: F) -> io::Result<SmallVec<Ifv6Net>>
where
  F: FnMut(&IpAddr) -> bool,
{
  netlink_addr(AddressFamily::INET6, index, f)
}

pub(super) fn interface_addresses<F>(index: u32, f: F) -> io::Result<SmallVec<IfNet>>
where
  F: FnMut(&IpAddr) -> bool,
{
  netlink_addr(AddressFamily::UNSPEC, index, f)
}

const IGMP_PATH: &str = "/proc/net/igmp";
const IGMP6_PATH: &str = "/proc/net/igmp6";

pub(super) fn interface_multicast_ipv4_addresses<F>(
  ifi: u32,
  f: F,
) -> io::Result<SmallVec<Ifv4Addr>>
where
  F: FnMut(&Ipv4Addr) -> bool,
{
  parse_proc_net_igmp(IGMP_PATH, ifi, f)
}

pub(super) fn interface_multicast_ipv6_addresses<F>(
  ifi: u32,
  f: F,
) -> io::Result<SmallVec<Ifv6Addr>>
where
  F: FnMut(&Ipv6Addr) -> bool,
{
  parse_proc_net_igmp6(IGMP6_PATH, ifi, f)
}

pub(super) fn interface_multicast_addresses<F>(ifi: u32, mut f: F) -> io::Result<SmallVec<IfAddr>>
where
  F: FnMut(&IpAddr) -> bool,
{
  // Parse IPv4 multicast addrs
  let ifmat4 = parse_proc_net_igmp("/proc/net/igmp", ifi, |addr| f(&(*addr).into()))?;

  // Parse IPv6 multicast addrs
  let ifmat6 = parse_proc_net_igmp6("/proc/net/igmp6", ifi, |addr| f(&(*addr).into()))?;

  Ok(
    ifmat4
      .into_iter()
      .map(From::from)
      .chain(ifmat6.into_iter().map(From::from))
      .collect(),
  )
}

fn parse_proc_net_igmp<F>(path: &str, ifi: u32, mut f: F) -> std::io::Result<SmallVec<Ifv4Addr>>
where
  F: FnMut(&Ipv4Addr) -> bool,
{
  use std::io::BufRead;

  let file = std::fs::File::open(path)?;
  let reader = std::io::BufReader::new(file);
  let mut ifmat = SmallVec::new();
  let mut idx = 0;
  let mut lines = reader.lines();

  // Skip first line
  lines.next();

  for line in lines {
    let line = line?;

    // Only `fields[0]` is consulted below and we need ≥4 fields total.
    // Walking the whitespace-delimited iterator directly avoids the
    // per-line allocation of the old `split([' ',':','\r','\t','\n'])
    // .filter(...).collect::<MediumVec<_>>()`. Colons are never in
    // `fields[0]` (neither in the leading index nor in an 8-char
    // group-address column), so dropping them from the delimiter set
    // does not affect parsing.
    let mut it = line.split_ascii_whitespace();
    let field0 = match it.next() {
      Some(s) => s,
      None => continue,
    };
    if it.nth(2).is_none() {
      // Fewer than 4 tokens on this line.
      continue;
    }

    if !line.starts_with(' ') && !line.starts_with('\t') {
      // New interface line
      match field0.parse() {
        Ok(res) => idx = res,
        Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, e)),
      }
    } else if field0.len() == 8 {
      if ifi == 0 || ifi == idx {
        // The Linux kernel puts the IP address in /proc/net/igmp in
        // native endianness.
        let src = field0.as_bytes();
        let mut b = [0u8; 4];
        for i in (0..src.len()).step_by(2) {
          b[i / 2] = xtoi2(&src[i..i + 2], 0).unwrap_or(0);
        }

        b.reverse();
        let ip = b.into();
        if f(&ip) {
          ifmat.push(Ifv4Addr::new(idx, ip));
        }
      }
    }
  }

  Ok(ifmat)
}

fn parse_proc_net_igmp6<F>(path: &str, ifi: u32, mut f: F) -> io::Result<SmallVec<Ifv6Addr>>
where
  F: FnMut(&Ipv6Addr) -> bool,
{
  use std::io::BufRead;

  let file = std::fs::File::open(path)?;
  let reader = std::io::BufReader::new(file);
  let mut ifmat = SmallVec::new();

  for line in reader.lines() {
    let line = line?;

    // `split_ascii_whitespace` already handles spaces/tabs/CR/LF without
    // a collect+filter, and we only use `fields[0]` and `fields[2]`.
    let mut it = line.split_ascii_whitespace();
    let field0 = match it.next() {
      Some(s) => s,
      None => continue,
    };
    // skip field1
    if it.next().is_none() {
      continue;
    }
    let field2 = match it.next() {
      Some(s) => s,
      None => continue,
    };
    // need 3 more tokens (fields[3..=5]) for a total of 6+.
    if it.nth(2).is_none() {
      continue;
    }

    let idx = match field0.parse() {
      Ok(res) => res,
      Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, e)),
    };

    if ifi == 0 || ifi == idx {
      let mut i = 0;
      let src = field2.as_bytes();
      let mut data = [0u8; 16];
      while i + 1 < src.len() {
        data[i / 2] = xtoi2(&src[i..i + 2], 0).unwrap_or(0);
        i += 2;
      }

      let ip = data.into();
      if f(&ip) {
        ifmat.push(Ifv6Addr::new(idx, ip));
      }
    }
  }

  Ok(ifmat)
}

#[cfg(test)]
mod tests {
  use super::*;

  // `route_v4_from_raw` / `route_v6_from_raw` cover every branch of
  // the family / length / gateway validation matrix. They live on
  // the hot path between the netlink walker and `IpRoute`, so any
  // regression silently affects every `route_*_table*()` caller.
  // Live tarpaulin runs only exercise the success arm; these unit
  // tests fill in the wrong-family / out-of-range / absent-dst
  // branches.

  #[test]
  fn route_v4_from_raw_rejects_oversize_prefix() {
    assert!(route_v4_from_raw(1, 33, Some(IpAddr::V4(Ipv4Addr::UNSPECIFIED)), None).is_none());
  }

  #[test]
  fn route_v4_from_raw_rejects_wrong_family_dst() {
    assert!(route_v4_from_raw(1, 0, Some(IpAddr::V6(Ipv6Addr::UNSPECIFIED)), None).is_none());
  }

  #[test]
  fn route_v4_from_raw_treats_absent_dst_as_default() {
    let r = route_v4_from_raw(1, 0, None, None).unwrap();
    assert_eq!(r.destination().addr(), Ipv4Addr::UNSPECIFIED);
  }

  #[test]
  fn route_v4_from_raw_rejects_absent_dst_with_nonzero_prefix() {
    assert!(route_v4_from_raw(1, 8, None, None).is_none());
  }

  #[test]
  fn route_v4_from_raw_rejects_wrong_family_gateway() {
    let dst = Some(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
    let gw_v6 = Some(IpAddr::V6(Ipv6Addr::UNSPECIFIED));
    assert!(route_v4_from_raw(1, 0, dst, gw_v6).is_none());
  }

  #[test]
  fn route_v4_from_raw_accepts_absent_gateway() {
    let dst = Some(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 0)));
    let r = route_v4_from_raw(1, 8, dst, None).unwrap();
    assert!(r.gateway().is_none());
  }

  #[test]
  fn route_v6_from_raw_rejects_oversize_prefix() {
    assert!(route_v6_from_raw(1, 129, Some(IpAddr::V6(Ipv6Addr::UNSPECIFIED)), None).is_none());
  }

  #[test]
  fn route_v6_from_raw_rejects_wrong_family_dst() {
    assert!(route_v6_from_raw(1, 0, Some(IpAddr::V4(Ipv4Addr::UNSPECIFIED)), None).is_none());
  }

  #[test]
  fn route_v6_from_raw_treats_absent_dst_as_default() {
    let r = route_v6_from_raw(1, 0, None, None).unwrap();
    assert_eq!(r.destination().addr(), Ipv6Addr::UNSPECIFIED);
  }

  #[test]
  fn route_v6_from_raw_rejects_absent_dst_with_nonzero_prefix() {
    assert!(route_v6_from_raw(1, 64, None, None).is_none());
  }

  #[test]
  fn route_v6_from_raw_rejects_wrong_family_gateway() {
    let dst = Some(IpAddr::V6(Ipv6Addr::UNSPECIFIED));
    let gw_v4 = Some(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
    assert!(route_v6_from_raw(1, 0, dst, gw_v4).is_none());
  }

  #[test]
  fn route_v6_from_raw_accepts_absent_gateway() {
    let dst = Some(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)));
    let r = route_v6_from_raw(1, 32, dst, None).unwrap();
    assert!(r.gateway().is_none());
  }
}