zond-engine 0.17.0

The core networking library powering Zond. Raw packet injection, protocol parsing, service and OS fingerprinting, and asynchronous network scanning.
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// Copyright (c) 2026 Erik Lening (hollowpointer) and Contributors
//
// This file is part of Zond Engine, licensed under the GNU Affero General
// Public License, version 3 or later. See the LICENSE file for details, or
// <https://www.gnu.org/licenses/agpl-3.0.html>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! # Which link `lan` means
//!
//! One question: of the interfaces this machine has, which one is the network
//! a person means when they type `lan`?
//!
//! The answer is the link the default route leaves by, and the reason it is
//! that rather than anything about the hardware is written on
//! [`select_best_lan_interface`]. Every hardware guess this module tried before
//! picked the wrong interface on a real laptop, and the routing table is the
//! one fact that is answerable the same way on every platform.
//!
//! [`ViabilityError`] is the other half: which links could carry a sweep at all,
//! named the way each check reads rather than the way it passes.

use crate::info;
use crate::system::interface::source::viable_interfaces;
use crate::system::interface::{Link, LinkAddress};
use std::net::Ipv6Addr;

/// Why a link cannot carry a LAN sweep.
///
/// One variant per condition [`lan_link`] requires, named the way the check
/// reads rather than the way it passes.
#[non_exhaustive]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ViabilityError {
    /// The interface is operationally down.
    IsDown,
    /// There is no physical port behind the interface: a tunnel, a hypervisor's
    /// virtual switch, a VPN. Each carries IP and none has a neighbour to ARP
    /// for.
    NotPhysical,
    /// The interface does not have a MAC address.
    NoMacAddress,
    /// The interface does not support broadcast (required for ARP).
    NotBroadcast,
    /// The interface is a point-to-point link (e.g., a VPN).
    IsPointToPoint,
    /// The interface has no IPv4 address (for ARP) AND no IPv6 Link-Local (for NDP).
    NoValidLanIp,
}

/// The link a LAN scan runs on: the interface itself and how it is addressed in
/// both families.
///
/// The selection picks a *link*, and returns one rather than an
/// `Ipv4Network`, which would throw away everything the link knew about itself
/// at the moment it was chosen. The interface identity is what
/// [`Zone`](crate::model::ip::scoped::Zone) needs to make a link-local
/// address usable, and the IPv6 prefixes are what say which addresses are on
/// this segment at all.
///
/// `ipv4` is optional because a viable LAN link need not have one. An interface
/// carrying only a link-local IPv6 address is perfectly scannable, the
/// all-nodes echo and neighbour discovery both work, and a return value that
/// required an IPv4 network would force treating that as "no network found".
#[derive(Debug, Clone)]
pub struct LanLink {
    /// The interface the sweep runs on, carrying the name and index a zone is
    /// built from and the hardware address probes leave from.
    pub link: Link,
    /// The private IPv4 network to sweep, when the link has one.
    pub ipv4: Option<LinkAddress>,
    /// Every IPv6 network the link is addressed in, link-local included.
    pub ipv6: Vec<LinkAddress>,
}

impl LanLink {
    /// The link-local address probes leave from, if it has one.
    pub fn link_local(&self) -> Option<Ipv6Addr> {
        self.ipv6
            .iter()
            .filter_map(|held| match held.address() {
                std::net::IpAddr::V6(v6) => Some(v6),
                std::net::IpAddr::V4(_) => None,
            })
            .find(Ipv6Addr::is_unicast_link_local)
    }
}

/// The links most likely to be worth scanning from, best first.
pub fn prioritized_interfaces(limit: usize) -> Vec<Link> {
    prioritized_interfaces_with(limit, viable_interfaces())
}

/// The ordering, decoupled from the host so it can be tested.
///
/// Wired before wireless, and a name is not consulted. A sort on
/// `name.starts_with("e")` catches `eth0` and `en0` on Linux and macOS, and
/// nothing at all on Windows, where an adapter is named by its GUID. It also
/// ranks `en1` above `wlan0` on a machine where `en1` *is* the Wi-Fi, which is
/// an ordinary Mac layout. A link says what it is, so the sort asks it.
pub(crate) fn prioritized_interfaces_with(limit: usize, mut links: Vec<Link>) -> Vec<Link> {
    links.sort_by_key(|link| if link.is_wireless() { 1 } else { 0 });
    links.into_iter().take(limit).collect()
}

/// The best local network this host is attached to, or `None` if it is attached
/// to none.
///
/// Reads the host's own interface table and picks among the links that could
/// carry a sweep: up, physical, not loopback, holding a hardware address,
/// broadcast-capable and not point-to-point. [`ViabilityError`] names each of
/// those the other way round.
///
/// `None` rather than an error, because there is no error to report. A
/// machine with nothing but loopback and a VPN tunnel is a machine with no LAN,
/// which is an answer about the host and not a failure to find one out, and a
/// caller has nothing to do differently between the two.
pub fn lan_link() -> Option<LanLink> {
    lan_link_with(crate::system::interface::interfaces())
}

/// The IPv4 half of [`lan_link`], for callers that only sweep IPv4.
///
/// Part of the engine's published surface, which a front end builds against;
/// work inside the engine wants the link, since half of what a LAN scan does
/// is IPv6.
pub fn lan_network() -> Option<LinkAddress> {
    lan_link()?.ipv4
}

/// [`lan_link`] against an interface table the caller supplies.
///
/// The seam every decision in this module is tested through: on a real host the
/// table comes from the platform, and a test hands in interfaces that do not
/// exist, so the selection can be exercised without depending on whatever the
/// machine running the tests happens to have plugged in.
///
/// Whether a link is physical is read off the `Link` itself, not injected
/// alongside as a predicate.
pub(crate) fn lan_link_with(interfaces: Vec<Link>) -> Option<LanLink> {
    let interfaces_str: &str = match interfaces.len() {
        1 => "interface",
        _ => "interfaces",
    };

    info!(
        verbosity = 3,
        "identified {} network {}, picking the best one",
        interfaces.len(),
        interfaces_str
    );

    let viable: Vec<Link> = interfaces
        .into_iter()
        .filter(|link| lan_viability(link).is_ok())
        .collect();

    let link = select_best_lan_interface(viable)?;
    info!(
        verbosity = 3,
        "performing LAN scan on interface {}",
        link.name()
    );

    let ipv4 = link
        .addresses()
        .iter()
        .copied()
        .find(|held| matches!(held.address(), std::net::IpAddr::V4(v4) if v4.is_private()));
    let ipv6 = link
        .addresses()
        .iter()
        .copied()
        .filter(|held| held.address().is_ipv6())
        .collect();

    Some(LanLink { link, ipv4, ipv6 })
}

/// Whether `link` could carry a LAN sweep, and what stops it if not.
///
/// Public because [`ViabilityError`] would otherwise be a vocabulary for a
/// decision nobody could see: [`lan_link`] answers `Option` and drops the
/// reason, which is right for the question it asks and leaves a caller whose
/// sweep found no network with nothing to look at. This is that reason, per
/// link.
///
/// The conditions are the ones ARP and neighbour discovery need between them: a
/// segment with somebody else on it, and a hardware address to send from.
pub fn lan_viability(link: &Link) -> Result<(), ViabilityError> {
    if !link.is_up() {
        return Err(ViabilityError::IsDown);
    }
    if !link.is_physical() || link.is_loopback() {
        return Err(ViabilityError::NotPhysical);
    }
    if link.mac().is_none() {
        return Err(ViabilityError::NoMacAddress);
    }
    if !link.is_broadcast() {
        return Err(ViabilityError::NotBroadcast);
    }
    if link.is_point_to_point() {
        return Err(ViabilityError::IsPointToPoint);
    }

    let has_valid_ip = link.addresses().iter().any(|held| match held.address() {
        std::net::IpAddr::V4(v4) => v4.is_private(),
        std::net::IpAddr::V6(v6) => v6.is_unicast_link_local(),
    });
    if !has_valid_ip {
        return Err(ViabilityError::NoValidLanIp);
    }

    Ok(())
}

/// The best of the viable links.
///
/// The one the default route leaves by, before anything else. That is what
/// `lan` means to somebody who types it, the network this machine is actually
/// on, and it is a fact about the routing table rather than a guess about the
/// hardware, which is why it is answerable the same way on every platform.
///
/// A guess about the hardware breaks on macOS. `awdl0` (AirDrop) and `llw0`
/// present as ordinary broadcast Ethernet with real hardware behind them:
/// physical, up, a MAC, indistinguishable from a wired port by every field an
/// interface table exposes. So "prefer a wired link" would pick `awdl0`, which
/// has no IPv4 at all, over the Wi-Fi carrying the whole `/24`, and a sweep of
/// `lan` would answer *"awdl0 has no private IPv4 network to sweep"* on a
/// machine plainly on a network.
///
/// Neither does having an address make a link the LAN. Falling back to "the
/// first one with a private IPv4" would pick `bridge100` on the same kind of
/// Mac, the virtualisation bridge on `192.168.64.1/24`: a real private network
/// with nothing on it but virtual machines.
///
/// The remaining order is for the case where no link claims the default route
/// at all, which is a machine with no route off itself: prefer one that could
/// be swept, then a wired one, then whatever there is.
fn select_best_lan_interface(links: Vec<Link>) -> Option<Link> {
    if let Some(routed) = links.iter().find(|link| link.carries_default_route()) {
        return Some(routed.clone());
    }

    // The default route is not always on a viable link: a VPN tunnel owns it and
    // is point-to-point, so it is filtered out before this runs. What is left of
    // "the network this machine is on" is then the link with a router of its
    // own. That is what tells a Wi-Fi `/24` with a gateway from the virtualisation
    // bridge on the same host, which has a private address and no gateway and
    // which the wired-first rule below would otherwise prefer.
    links
        .iter()
        .find(|link| link.has_gateway() && has_private_ipv4(link))
        .or_else(|| {
            links
                .iter()
                .find(|link| link.is_wired() && has_private_ipv4(link))
        })
        .or_else(|| links.iter().find(|link| has_private_ipv4(link)))
        .or_else(|| links.iter().find(|link| link.is_wired()))
        .or_else(|| links.first())
        .cloned()
}

/// Whether a link holds an address on a private network, which is the one a LAN
/// sweep walks.
fn has_private_ipv4(link: &Link) -> bool {
    link.ipv4().any(|(address, _)| address.is_private())
}

// ╔════════════════════════════════════════════╗
// ║ ████████╗███████╗███████╗████████╗███████╗ ║
// ║ ╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝ ║
// ║    ██║   █████╗  ███████╗   ██║   ███████╗ ║
// ║    ██║   ██╔══╝  ╚════██║   ██║   ╚════██║ ║
// ║    ██║   ███████╗███████║   ██║   ███████║ ║
// ║    ╚═╝   ╚══════╝╚══════╝   ╚═╝   ╚══════╝ ║
// ╚════════════════════════════════════════════╝

#[cfg(test)]
mod tests {
    use crate::model::mac::MacAddr;

    fn of_kind(name: &str, kind: LinkKind) -> Link {
        Link::new(name, 1)
            .with_kind(kind)
            .with_mac(MacAddr::new(1, 2, 3, 4, 5, 6))
    }

    /// A wired link outranks a wireless one, whatever either is called.
    ///
    /// An ordering by the first letter of the name cannot tell them apart:
    /// `en1` can be a Mac's Wi-Fi and `eth0` a server's wired port, and both
    /// start with `e`, so such a sort is right by accident where it is right at
    /// all, and has nothing to say on Windows, where an adapter is named by a
    /// GUID.
    #[test]
    fn a_wired_link_is_preferred_however_the_platform_names_it() {
        let ordered = prioritized_interfaces_with(
            10,
            vec![
                of_kind("en1", LinkKind::Wireless),
                of_kind("{3F2504E0-4F89}", LinkKind::Wired),
            ],
        );

        assert_eq!(ordered[0].name(), "{3F2504E0-4F89}");
        assert_eq!(ordered[1].name(), "en1");
    }

    /// The limit is a limit.
    #[test]
    fn no_more_links_than_were_asked_for() {
        let links = vec![
            of_kind("en0", LinkKind::Wired),
            of_kind("en1", LinkKind::Wired),
            of_kind("en2", LinkKind::Wired),
        ];

        assert_eq!(prioritized_interfaces_with(2, links.clone()).len(), 2);
        assert_eq!(prioritized_interfaces_with(0, links).len(), 0);
    }

    use super::*;
    use crate::system::interface::Addressing;
    use crate::system::interface::LinkKind;
    use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

    fn mock_interface(
        up: bool,
        mac: bool,
        broadcast: bool,
        p2p: bool,
        loopback: bool,
        ip: bool,
    ) -> Link {
        let mut link = Link::new("test0", 0)
            .with_link_up(up)
            .with_addressing(Addressing::of(broadcast, p2p))
            // Every case this builds is a real interface unless it says
            // otherwise; the physical/virtual axis is exercised by `loopback`.
            .with_physical(!loopback)
            .with_kind(if loopback {
                LinkKind::Loopback
            } else {
                LinkKind::Wired
            });

        if mac {
            link = link.with_mac(crate::model::mac::MacAddr::new(1, 2, 3, 4, 5, 6));
        }
        if ip {
            link = link.with_addresses(vec![LinkAddress::new(
                IpAddr::V4(Ipv4Addr::new(192, 168, 1, 100)),
                24,
            )]);
        }

        link
    }

    #[test]
    fn a_link_with_only_ipv6_is_still_a_lan_link() {
        let intf = mock_interface(true, true, true, false, false, false).with_addresses(vec![
            LinkAddress::new(IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)), 64),
        ]);

        assert_eq!(lan_viability(&intf), Ok(()));

        let link = lan_link_with(vec![intf]).expect("a viable link is selected");

        assert!(
            link.ipv4.is_none(),
            "there is no private IPv4 network here, and saying so is the point"
        );
        assert_eq!(
            link.link_local(),
            Some(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)),
            "the address probes would leave from has to survive selection"
        );
        assert_eq!(link.link.name(), "test0");
    }

    /// The link carries both families, so a dual-stack segment does not have to
    /// choose which half of itself to be described by.
    #[test]
    fn a_dual_stack_link_carries_both_families() {
        let mut held = mock_interface(true, true, true, false, false, true)
            .addresses()
            .to_vec();
        held.push(LinkAddress::new(
            IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)),
            64,
        ));
        let intf = mock_interface(true, true, true, false, false, true).with_addresses(held);

        let link = lan_link_with(vec![intf]).expect("a viable link is selected");

        assert_eq!(
            link.ipv4.map(|held| held.address()),
            Some(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 100)))
        );
        assert_eq!(link.ipv6.len(), 1);
    }

    /// The published IPv4-only entry point answers with a link's private IPv4
    /// network, address and prefix, since a front end outside this repo builds
    /// against it.
    #[test]
    fn the_ipv4_view_of_a_link_is_unchanged() {
        let intf = mock_interface(true, true, true, false, false, true);

        let link = lan_link_with(vec![intf]).expect("a viable link is selected");

        let held = link.ipv4.expect("the link has a private IPv4 network");
        assert_eq!(held.address(), IpAddr::V4(Ipv4Addr::new(192, 168, 1, 100)));
        assert_eq!(held.prefix(), 24);
    }

    /// The default route decides, and a link that merely looks like hardware
    /// does not.
    ///
    /// On a real Mac sitting on a `/24`, a preference for wired links answers a
    /// sweep of `lan` with *"awdl0 has no private IPv4 network to sweep"*.
    /// `awdl0` is AirDrop: macOS presents it as broadcast Ethernet, physical, up,
    /// with a MAC, every field a wired port has, so "prefer a wired link" chooses
    /// it over the Wi-Fi that has the actual network.
    #[test]
    fn the_link_carrying_the_default_route_is_the_lan() {
        let wifi = mock_interface(true, true, true, false, false, true)
            .with_kind(LinkKind::Wireless)
            .with_default_route(true);
        // No IPv4 at all, and indistinguishable from a wired port otherwise.
        let airdrop = Link::new("awdl0", 17)
            .with_link_up(true)
            .with_physical(true)
            .with_addressing(Addressing::Broadcast)
            .with_kind(LinkKind::Wired)
            .with_mac(crate::model::mac::MacAddr::new(1, 2, 3, 4, 5, 6))
            .with_addresses(vec![LinkAddress::new(
                IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)),
                64,
            )]);

        let chosen =
            select_best_lan_interface(vec![airdrop, wifi]).expect("one of them is the LAN");

        assert_eq!(
            chosen.name(),
            "test0",
            "the wired-looking link with no network won over the one carrying the route"
        );
    }

    /// Nor does a private network of its own make a link the LAN.
    ///
    /// The same laptop carries `bridge100` on `192.168.64.1/24`, which is the
    /// virtualisation bridge: a real private network with nothing on it but
    /// virtual machines. Falling back to "the first link with a private IPv4"
    /// would sweep that and report the host's own VMs as the network.
    #[test]
    fn a_virtualisation_bridge_does_not_outrank_the_default_route() {
        let wifi = mock_interface(true, true, true, false, false, true)
            .with_kind(LinkKind::Wireless)
            .with_default_route(true);
        let bridge = Link::new("bridge100", 20)
            .with_link_up(true)
            .with_physical(true)
            .with_addressing(Addressing::Broadcast)
            .with_kind(LinkKind::Wired)
            .with_mac(crate::model::mac::MacAddr::new(1, 2, 3, 4, 5, 7))
            .with_addresses(vec![LinkAddress::new(
                IpAddr::V4(Ipv4Addr::new(192, 168, 64, 1)),
                24,
            )]);

        let chosen = select_best_lan_interface(vec![bridge, wifi]).expect("one of them is the LAN");

        assert_eq!(chosen.name(), "test0");
    }

    /// A VPN owns the default route, so no viable link carries it, and the LAN
    /// is the link with a gateway. On a Mac with a VPN up, the tunnel is
    /// point-to-point and drops out of viability, leaving a Wi-Fi `/24` with a
    /// router and the virtualisation bridge with none; the wired-first rule
    /// would take the bridge, so the gateway is what has to decide.
    #[test]
    fn a_gateway_beats_a_wired_bridge_when_a_vpn_holds_the_default_route() {
        let wifi = mock_interface(true, true, true, false, false, true)
            .with_kind(LinkKind::Wireless)
            .with_gateway(true);
        let bridge = Link::new("bridge100", 20)
            .with_link_up(true)
            .with_physical(true)
            .with_addressing(Addressing::Broadcast)
            .with_kind(LinkKind::Wired)
            .with_mac(crate::model::mac::MacAddr::new(1, 2, 3, 4, 5, 7))
            .with_addresses(vec![LinkAddress::new(
                IpAddr::V4(Ipv4Addr::new(192, 168, 64, 1)),
                24,
            )]);

        let chosen = select_best_lan_interface(vec![bridge, wifi]).expect("one of them is the LAN");

        assert_eq!(chosen.name(), "test0", "the link with a router is the LAN");
    }

    /// With no default route anywhere, a link that could be swept beats one that
    /// could not, which is the ordering that would have made the reported bug
    /// harmless even without the rule above.
    #[test]
    fn with_no_route_a_sweepable_link_beats_one_with_nothing_to_sweep() {
        let addressed = mock_interface(true, true, true, false, false, true);
        let bare = Link::new("awdl0", 17)
            .with_link_up(true)
            .with_physical(true)
            .with_addressing(Addressing::Broadcast)
            .with_kind(LinkKind::Wired)
            .with_mac(crate::model::mac::MacAddr::new(1, 2, 3, 4, 5, 6));

        let chosen =
            select_best_lan_interface(vec![bare, addressed]).expect("one of them is picked");

        assert_eq!(chosen.name(), "test0");
    }

    #[test]
    fn is_viable_down() {
        let intf = mock_interface(false, true, true, false, false, true);
        assert_eq!(lan_viability(&intf), Err(ViabilityError::IsDown));
    }

    /// A virtual adapter is not a LAN, however well-addressed it is.
    ///
    /// It says so itself: the link carries whether it is physical, so the test
    /// states the fact rather than stubbing a function that finds it out.
    #[test]
    fn is_viable_not_physical() {
        let intf = mock_interface(true, true, true, false, false, true).with_physical(false);
        assert_eq!(lan_viability(&intf), Err(ViabilityError::NotPhysical));
    }

    #[test]
    fn is_viable_no_mac() {
        let intf = mock_interface(true, false, true, false, false, true);
        assert_eq!(lan_viability(&intf), Err(ViabilityError::NoMacAddress));
    }

    #[test]
    fn is_viable_success() {
        let intf = mock_interface(true, true, true, false, false, true);
        assert_eq!(lan_viability(&intf), Ok(()));
    }
}