zond-engine 0.18.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
// 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

//! # How this host reaches a target
//!
//! The classifier, and the one decision every strategy a scan runs follows
//! from. A target is on a segment this machine is attached to, or behind a
//! gateway, or reachable by neither, and which of the three it is decides
//! whether it gets a link-layer sweep, a raw probe with a source address
//! attached, or the unprivileged fallback.
//!
//! A segment is a link a frame can be put on. A tunnel's address carries a
//! prefix too, and a WireGuard peer or an OpenVPN server in subnet topology
//! sits inside it, but nothing on the far side of a tunnel answers ARP or
//! neighbour discovery: the prefix is a route through the tunnel, and a target
//! inside it is probed through the tunnel like any routed target.
//!
//! ## What it refuses, and why refusing is the work
//!
//! Two of the five buckets [`RoutedTargets`] hands back are refusals, and they
//! carry more of this module's reasoning than the three that succeed.
//!
//! A bare IPv6 link-local matches every interface and identifies none, so it is
//! reported as the unanswerable question it is rather than assigned to whichever
//! interface the host listed first. An off-link IPv6 range past
//! [`MAX_ENUMERABLE_ADDRESSES`] is kept whole and refused, because the only
//! strategy the engine has for an off-link range is to walk it and IPv6 defeats
//! walking outright.
//!
//! Both are carried out rather than dropped, on the rule the rest of the crate
//! is built on: a scan may report that it found nothing, and may never be quiet
//! about ground it did not look at.
//!
//! ## What a frame reaches
//!
//! A second question is asked of the same classification, by a process whose
//! raw strategies put self-built frames on the wire with nothing behind them:
//! an unprivileged run on macOS holding the BPF devices, and a privileged one on
//! Windows. A frame reaches what has Ethernet in front of it, and
//! [`beyond_frames`] names the rest and why, so that a scan can reach those
//! targets by connect instead of sending them nothing.
//!
//! ## What it costs
//!
//! One `connect` per off-link target on an unbound UDP socket, which performs a
//! route lookup and sends nothing, parallelised across the target list. On-link
//! targets cost a prefix comparison and no syscall at all.

use crate::model::ip::range::IpRange::{self, V4, V6};
use crate::model::ip::range::{Ipv4Range, Ipv6Range};
use crate::model::ip::scoped::ScopedIp;
use crate::model::ip::set::IpSet;
use crate::system::interface::source::{
    RouteAnswer, ask_route, plausible_source, refuses_neighbour, viable_interfaces,
};
use crate::system::interface::{Link, LinkAddress};
use rayon::prelude::*;
use std::collections::{HashMap, HashSet};
use std::net::IpAddr;

/// An off-link target paired with the local source address a probe to it must
/// be sent from.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RoutedTarget {
    /// The destination being probed.
    pub target: IpAddr,
    /// The source address to send its probe from.
    pub source: IpAddr,
}

impl RoutedTarget {
    /// A probe of `target`, sent from `source`.
    pub const fn new(target: IpAddr, source: IpAddr) -> Self {
        Self { target, source }
    }
}

/// The largest IPv6 range any strategy will turn into addresses one at a time.
///
/// Sixty-five thousand addresses, the size of an IPv4 `/16` and of an IPv6
/// `/112`. Enumeration is the only discovery strategy the engine has for an
/// off-link range, and it is a strategy IPv6 defeats outright rather than
/// merely slows: a `/64` holds 2^64 addresses, which at the four thousand
/// probes a second a routed sweep paces itself to is about 146 million years of
/// scanning, and long before that the expansion into a `Vec<IpAddr>` exhausts
/// memory. There is no ceiling at which walking a `/64` becomes reasonable, so
/// the question is only where to stop pretending.
///
/// It is the same number as the largest IPv4 range anyone sweeps
/// in practice, because the limit is about how many probes a scan can spend
/// rather than about the address family. Larger IPv6 ranges are not scanned
/// less thoroughly here; they are refused, loudly, so the caller knows the
/// engine did not look rather than believing it looked and found nothing.
///
/// IPv4 ranges are not bounded by this. Every IPv4 range is finite in a way a
/// user can reason about, the whole space is 2^32, and a `/8` is an
/// unreasonable request rather than an impossible one.
///
/// Public as the number, where [`is_enumerable`] is the test: three places
/// ask the question and every one of them asks it through the function, which is
/// what keeps the number in one place. The classifier applies it to a routed
/// range it would have to walk; [`crate::scanner`] applies it on the
/// unprivileged path, which takes its addresses as given and has no classifier
/// to consult; and `DiscoveryPlan::build` applies it to an on-link range, which
/// the classifier hands over whole because a segment is swept by multicast
/// rather than walked.
///
/// Each of the three arrived after a defect. Two spellings of the number meant a
/// `/64` refused with root and scanned forever without it; no check at all on
/// the third path meant an on-link `/64` was walked two thousand addresses deep
/// and reported covered.
pub const MAX_ENUMERABLE_ADDRESSES: u128 = 1 << 16;

/// The result of classifying a set of targets against this host's interfaces
/// and routing table.
#[non_exhaustive]
#[derive(Debug, Default)]
pub struct RoutedTargets {
    /// Targets that share an interface's Layer-2 segment, grouped by that
    /// interface. Reachable directly, so they get an ARP/NDP discovery
    /// strategy bound to the interface.
    ///
    /// Only a link that carries frames has a segment. The one exception is a
    /// link-local target whose zone names a link that does not: the zone is
    /// the user's own statement of where the target is, and it is kept.
    pub local: HashMap<Link, IpSet>,
    /// Targets reached through a gateway or a tunnel, each already paired with
    /// the source address to probe it from. Handled by a single raw TCP SYN
    /// scanner.
    pub routed: Vec<RoutedTarget>,
    /// Targets that are neither on-link nor have a resolvable route, left to
    /// the unprivileged connect fallback. Loopback is always here, in both
    /// families, whatever the routing table or a forced source says about it.
    pub unmapped: IpSet,
    /// Targets that are this host's own addresses.
    ///
    /// Separated because no strategy can establish one. An address the host
    /// holds is reached through loopback whatever its subnet says, so an ARP
    /// request for it goes onto a link where nothing will answer, and the
    /// address is reported down while `ping` to it succeeds. It is up by
    /// construction, and saying so is both the correct answer and the cheap one.
    pub ours: IpSet,
    /// Link-local IPv6 targets with no interface named on them.
    ///
    /// Every interface holds an `fe80::/64`, so such a target matches all of
    /// them and identifies none. Assigning it to whichever the host happened to
    /// list first probes an arbitrary segment and reports the address absent
    /// when it is present on another: a wrong answer arrived at silently, and
    /// on a laptop with two dozen interfaces an unlikely guess. Written
    /// `fe80::1%en0`, it is unambiguous; written bare, it is a question with no
    /// answer and is reported as one.
    pub ambiguous: Vec<Ipv6Range>,
    /// Off-link IPv6 ranges too large to enumerate, kept whole.
    ///
    /// These are not failures of the network and not addresses that went
    /// unanswered; they were never probed. They are carried out of here rather
    /// than dropped because the one thing a scanner may never do is stay quiet
    /// about a target it declined to look at: a caller reading "no hosts
    /// found" would otherwise take it as evidence about the range.
    pub unenumerable: Vec<Ipv6Range>,
}

/// Classifies target IPs by how this host reaches them: on-link (per
/// interface), routed through a gateway or a tunnel (paired with a source
/// address), or unreachable.
///
/// Reads the host's interface table through
/// [`interfaces`](super::interfaces), narrowed to the links that could carry a
/// probe. Where that table comes from is [`Link::from_netdev`](super::Link)'s
/// business and nobody else's, so nothing here names the crate that reads it.
pub fn map_ips_to_interfaces(ip_set: IpSet) -> RoutedTargets {
    map_ips_to_interfaces_with(ip_set, viable_interfaces(), &[])
}

/// [`map_ips_to_interfaces`], with source addresses forced ahead of the routing
/// table. A scan pinned to an interface routes every off-link target from that
/// interface's address, the override for a host whose default route a VPN owns.
pub(crate) fn map_ips_to_interfaces_forced(ip_set: IpSet, forced: &[IpAddr]) -> RoutedTargets {
    map_ips_to_interfaces_with(ip_set, viable_interfaces(), forced)
}

/// Per-single classification carried out of the parallel pass, before the
/// results are folded back into interface-indexed buckets.
enum Classification {
    /// On-link on the interface at this index.
    Local(usize),
    /// Routed off-link, to be sent from this source address.
    Routed(IpAddr),
    /// An address this host holds.
    Ours,
    /// No route found.
    Unmapped,
}

/// [`map_ips_to_interfaces`] against an interface table the caller supplies.
///
/// The seam every classification 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 which bucket a target lands in can be
/// exercised without depending on what the machine running the tests happens to
/// have plugged in.
pub(crate) fn map_ips_to_interfaces_with(
    ip_set: IpSet,
    interfaces: Vec<Link>,
    forced: &[IpAddr],
) -> RoutedTargets {
    map_ips_to_interfaces_asking(ip_set, interfaces, forced, ask_route)
}

/// [`map_ips_to_interfaces_with`], asking the routing table about a routed
/// target through `route`, which a test hands in to have the table answer as
/// no host a test runs on does.
fn map_ips_to_interfaces_asking(
    ip_set: IpSet,
    interfaces: Vec<Link>,
    forced: &[IpAddr],
    route: fn(IpAddr) -> RouteAnswer,
) -> RoutedTargets {
    let owned_ips: HashSet<IpAddr> = interfaces
        .iter()
        .flat_map(|link| link.addresses().iter().map(|held| held.address()))
        .collect();

    let mut local: HashMap<usize, IpSet> = HashMap::new();
    let mut routed: Vec<RoutedTarget> = Vec::new();
    let mut unmapped = IpSet::new();
    let mut ours = IpSet::new();
    let mut unenumerable: Vec<Ipv6Range> = Vec::new();
    let mut ambiguous: Vec<Ipv6Range> = Vec::new();
    let mut singles_to_route: Vec<IpAddr> = Vec::new();

    // A range wholly inside one segment's subnet is kept intact; anything
    // else is expanded to singles for per-target route resolution.
    for range in ip_set.v4() {
        let start = IpAddr::V4(range.start_addr());
        let end = IpAddr::V4(range.end_addr());
        match owning_interface(&interfaces, start, end) {
            Some(idx) => local.entry(idx).or_default().insert_range(V4(*range)),
            None => singles_to_route.extend(range.iter()),
        }
    }
    for range in ip_set.v6() {
        let start = IpAddr::V6(range.start_addr());
        let end = IpAddr::V6(range.end_addr());

        // Checked before any interface is consulted, because consulting them is
        // exactly the mistake: they all match.
        if range.is_ambiguous() {
            ambiguous.push(*range);
            continue;
        }
        // A named interface answers the question outright. The scope id is the
        // user's own statement about which segment they meant, and it outranks
        // any prefix match.
        if let Some(zone) = range.zone() {
            match interfaces.iter().position(|link| link.index() == zone) {
                Some(idx) => local.entry(idx).or_default().insert_range(V6(*range)),
                None => ambiguous.push(*range),
            }
            continue;
        }

        match owning_interface(&interfaces, start, end) {
            // On-link, so it is kept whole and never expanded here: a segment is
            // reached by multicast, and that is one packet whatever the prefix
            // length. Whether the range is *also* small enough to walk address
            // by address is a question for whoever builds the sweep, since only
            // a targeted run walks one; `DiscoveryPlan::build` asks it.
            Some(idx) => local.entry(idx).or_default().insert_range(V6(*range)),
            // Off-link, where the only strategy is to probe each address in
            // turn. The check comes before `to_iter` because the expansion is
            // what does the damage, not the probing.
            None if !is_enumerable(range) => unenumerable.push(*range),
            None => singles_to_route.extend(range.iter()),
        }
    }

    let processed: Vec<(IpAddr, Classification)> = singles_to_route
        .par_iter()
        .map(|&target| {
            // Loopback is this host, and nothing below may say otherwise. The
            // kernel answers `::1` with `::1`, which no interface here holds, and
            // the fallback after it would then pair the target with a global
            // source as though it were a routed address behind a VPN; a forced
            // source would do the same. `127.0.0.1` would fall through to
            // `Unmapped` only because that fallback declines IPv4, so without
            // this the two loopbacks would be planned differently for no reason
            // either of them has.
            if target.is_loopback() {
                return (target, Classification::Unmapped);
            }
            // An IPv4-mapped address is an IPv4 host written inside IPv6, and
            // no wire carries one. The fallbacks below would pair it with a
            // global IPv6 source as a routed target and frame it toward the
            // router, which is the one place it certainly is not. Unmapped, it
            // is left to whatever asks the kernel, whose dual-stack socket
            // reaches the IPv4 host it spells.
            if is_ipv4_mapped(target) {
                return (target, Classification::Unmapped);
            }
            // Before any prefix is consulted, because this host's address is
            // inside its own link's prefix and the kernel answers it from
            // loopback whatever that prefix says.
            if owned_ips.contains(&target) {
                return (target, Classification::Ours);
            }

            // Inside a prefix this host holds, the link holding it is the one
            // route to the target. A segment is swept; inside a tunnel's own
            // prefix the target is reached through the tunnel from the
            // tunnel's address. Read off the interface table rather than asked
            // of the kernel: the table already says it, and a forced source
            // must not move the target off the only link that reaches it.
            if let Some((idx, held)) = holding_prefix(&interfaces, target) {
                return if interfaces[idx].carries_frames() {
                    (target, Classification::Local(idx))
                } else {
                    (target, Classification::Routed(held.address()))
                };
            }

            // A route that refuses by policy is the host's decision about the
            // target, and neither a forced source nor the fallback below
            // steps around it: each sends from an address named outright,
            // which the kernel sends from wherever the table refuses. Left to
            // the connect fallback, whose connect the kernel refuses too, and
            // which files the address as one nothing reaches. See
            // `RouteAnswer::Forbidden`.
            let answer = route(target);
            if matches!(answer, RouteAnswer::Forbidden) {
                return (target, Classification::Unmapped);
            }

            // A forced source outranks the routing table otherwise. On-link
            // and tunnel targets are already settled above and answer through
            // their own link; a routed target the kernel would send from the
            // wrong interface is what the override exists for.
            if let Some(source) = forced
                .iter()
                .copied()
                .find(|s| s.is_ipv4() == target.is_ipv4())
            {
                return (target, Classification::Routed(source));
            }

            if let RouteAnswer::From(source) = answer
                && owned_ips.contains(&source)
            {
                return (target, Classification::Routed(source));
            }

            // The kernel has no route, but this host may still hold an address
            // of the right scope - see `plausible_source`. Without this a
            // laptop whose VPN swallowed the IPv6 default route sends no probe
            // at all and reports the targets as unreachable.
            if let Some(source) = plausible_source(&interfaces, target) {
                return (target, Classification::Routed(source));
            }

            (target, Classification::Unmapped)
        })
        .collect();

    for (target, class) in processed {
        match class {
            Classification::Local(idx) => local.entry(idx).or_default().insert(target),
            Classification::Routed(source) => routed.push(RoutedTarget { target, source }),
            Classification::Unmapped => unmapped.insert(target),
            Classification::Ours => ours.insert(target),
        }
    }

    // Withheld from every strategy. The per-address pass settles an address
    // this host holds before anything else, but a range wholly inside a
    // segment's subnet is kept intact and assigned to that link without ever
    // reaching that pass, so an address it holds arrives here inside a set:
    // a sweep of the subnet containing it ends up in `local` whole.
    //
    // Nothing can establish one. The kernel routes traffic for an address this
    // host holds through loopback, so an ARP request goes onto a link where
    // nothing will answer and the address is reported down while `ping` to it
    // succeeds.
    for address in &owned_ips {
        let mut one = IpSet::new();
        one.insert(*address);

        for targets in local.values_mut() {
            if targets.contains(address) {
                targets.subtract(&one);
                ours.insert(*address);
            }
        }
    }
    // A link whose only target was ours has nothing left to sweep.
    local.retain(|_, targets| !targets.is_empty());

    let local = local
        .into_iter()
        .map(|(idx, ips)| (interfaces[idx].clone(), ips))
        .collect();

    RoutedTargets {
        local,
        routed,
        unmapped,
        ours,
        ambiguous,
        unenumerable,
    }
}

/// The addresses among `targets`, on `link`'s segment, that this host's
/// routing table refuses.
///
/// A segment is reached by frames built for the neighbour, which never ask
/// the table, so a route an administrator added over one address of a
/// connected prefix, or a VPN's kill switch keeping the local network out,
/// is heard only by asking. Every other program on the machine honours it,
/// its ping and its connect alike, and a sweep that framed its questions to
/// such a neighbour anyway would report it up and then have every port scan
/// refuse it: the one account of the host that ignored the host's own
/// policy, and one at odds with itself.
///
/// Asked address by address, which is a route lookup each and no packet.
/// Three kinds of target are not asked. A link-local address is on its
/// zone's segment by definition and the table has no route to consult for it
/// without one. A range too large to walk is not walked, whether it is
/// refused a sweep or answered by the solicitation, which is one packet to
/// the whole segment. And the network and broadcast addresses of one of the
/// link's own prefixes: a kernel refuses a connect to the broadcast address
/// the way it refuses a policy's, and neither is a neighbour.
pub(crate) fn refused_neighbours(link: &Link, targets: &IpSet) -> IpSet {
    refused_neighbours_asking(link, targets, refuses_neighbour)
}

/// [`refused_neighbours`], asking the table through `refuses`, which a test
/// hands in to have the table refuse as no host a test runs on does.
fn refused_neighbours_asking(link: &Link, targets: &IpSet, refuses: fn(IpAddr) -> bool) -> IpSet {
    let edges: HashSet<IpAddr> = link
        .addresses()
        .iter()
        .filter_map(|held| match held.network() {
            IpRange::V4(range) => Some([range.start_addr(), range.end_addr()]),
            IpRange::V6(_) => None,
        })
        .flatten()
        .map(IpAddr::V4)
        .collect();
    let v4 = targets.v4().iter().flat_map(Ipv4Range::iter);
    let v6 = targets
        .v6()
        .iter()
        .filter(|range| range.zone().is_none() && is_enumerable(range))
        .flat_map(Ipv6Range::iter);
    let walked = v4.chain(v6);

    let refused: Vec<IpAddr> = walked
        .filter(|target| !edges.contains(target) && !ScopedIp::needs_zone(target))
        .par_bridge()
        .map(|target| refuses(target).then_some(target))
        .flatten()
        .collect();

    let mut set = IpSet::new();
    for address in refused {
        set.insert(address);
    }
    set.canonicalize();
    set
}

/// Which of the engine's two frame builders a question about reach is asked for.
///
/// They differ in one respect, and it decides what they reach. The segment sweep
/// resolves an IPv6 neighbour itself, by neighbour discovery over the link it
/// holds open. The probe transport's frame sender has ARP and nothing else, so an
/// IPv6 neighbour on the same segment is one it has no hardware address to send
/// to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FrameSender {
    /// ARP and ICMPv6 across a segment, which is discovery's local step.
    Sweep,
    /// The probe transport's frames: port probes, routed discovery, and every
    /// later pass that sends its own segments to a host.
    Probe,
}

/// Why a self-built frame cannot reach a target.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Unframed {
    /// The target is loopback.
    Loopback,
    /// The target is an address this host holds. Its kernel answers it through
    /// loopback, so a frame put on a link for it reaches nobody who replies.
    Ours,
    /// The route to the target leaves by the named link, which carries no
    /// frames: a tunnel, a VPN, or anything else without a hardware address
    /// and a segment.
    Tunnel(String),
    /// Nothing routes to the target at all.
    NoRoute,
    /// The target is an IPv6 neighbour on the named link, and the probe
    /// transport's sender has no neighbour discovery to resolve it with.
    Neighbour(String),
    /// The target is an IPv4 address written in the IPv4-mapped IPv6 block,
    /// which only a socket can reach: no frame carries such an address.
    Mapped,
}

/// A few words, since a message puts one in brackets after the addresses it
/// covers.
impl std::fmt::Display for Unframed {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Loopback => f.write_str("loopback"),
            Self::Ours => f.write_str("own address"),
            Self::Tunnel(link) => write!(f, "via {link}"),
            Self::NoRoute => f.write_str("no route"),
            Self::Neighbour(link) => write!(f, "IPv6 neighbour on {link}, no NDP"),
            Self::Mapped => f.write_str("IPv4-mapped"),
        }
    }
}

/// The targets a self-built frame cannot reach from this host, and why.
///
/// Built by [`beyond_frames`]. What it serves is a process that may inject
/// frames and holds nothing else, which is an unprivileged run on macOS with
/// the BPF devices handed to a group, and every privileged run on Windows. The
/// frames reach whatever has Ethernet in front of it; everything here needs the
/// kernel to carry it, and so needs a strategy that asks the kernel.
#[derive(Debug, Default)]
pub(crate) struct BeyondFrames {
    /// Every target no frame reaches.
    pub(crate) targets: IpSet,
    /// Each reason that applied and the targets it applied to, in a fixed
    /// order, none of them empty.
    reasons: Vec<(Unframed, IpSet)>,
}

impl BeyondFrames {
    /// Whether every target is within a frame's reach.
    pub(crate) fn is_empty(&self) -> bool {
        self.targets.is_empty()
    }

    /// Each reason with the lowest address it applied to and how many it
    /// covered, which is what a message about the set quotes.
    pub(crate) fn summary(&self) -> Vec<(Unframed, IpAddr, u128)> {
        self.reasons
            .iter()
            .filter_map(|(reason, targets)| {
                let first = targets.iter().next()?;
                Some((reason.clone(), first, targets.len()))
            })
            .collect()
    }

    /// Each reason that applied, in order, joined for the brackets after a
    /// line naming what the set was asked by.
    pub(crate) fn reasons(&self) -> String {
        let named: Vec<String> = self
            .reasons
            .iter()
            .map(|(reason, _)| reason.to_string())
            .collect();
        named.join(", ")
    }

    /// These, together with every address of `unmapped` they do not already
    /// hold, each under the reason nothing routes a frame to it.
    ///
    /// For a phase whose connect step holds both what a frame cannot reach
    /// and what the routing table left without a route, loopback among it,
    /// whatever the privilege: the second is classified from the address
    /// alone, since the routing table has already been asked about it.
    pub(crate) fn and_unmapped(mut self, unmapped: &IpSet) -> Self {
        let mut rest = unmapped.clone();
        rest.subtract(&self.targets);
        for (reason, targets) in unmapped_reasons(rest) {
            self.note(reason, targets);
        }
        self.targets.canonicalize();
        self
    }

    /// One reason and the addresses it covers, joined to those it already
    /// covers where it has been noted before.
    fn note(&mut self, reason: Unframed, mut targets: IpSet) {
        targets.canonicalize();
        if targets.is_empty() {
            return;
        }
        extend(&mut self.targets, &targets);
        match self.reasons.iter_mut().find(|(held, _)| *held == reason) {
            Some((_, held)) => {
                extend(held, &targets);
                held.canonicalize();
            }
            None => self.reasons.push((reason, targets)),
        }
    }
}

/// Why no frame reaches each of `unmapped`, addresses the routing table found
/// no link for, split by reason: loopback, IPv4-mapped, and no route.
///
/// Split by block rather than address by address, since an unrouted range can
/// be as wide as the target list, and none of the three needs the routing
/// table asked again. Loopback is `127.0.0.0/8` and `::1`, as
/// [`IpAddr::is_loopback`] has it; an IPv4 loopback address written in the
/// mapped block is mapped.
fn unmapped_reasons(unmapped: IpSet) -> Vec<(Unframed, IpSet)> {
    let block = |ranges: &[IpRange]| {
        let mut set = IpSet::new();
        for range in ranges {
            set.insert_range(*range);
        }
        set
    };
    let v4 = |start: [u8; 4], end: [u8; 4]| {
        V4(Ipv4Range::new(start.into(), end.into()).expect("an ordered range"))
    };
    let v6 = |start: u128, end: u128| {
        V6(Ipv6Range::new(start.into(), end.into()).expect("an ordered range"))
    };
    let blocks = [
        (
            Unframed::Loopback,
            block(&[v4([127, 0, 0, 0], [127, 255, 255, 255]), v6(1, 1)]),
        ),
        (
            Unframed::Mapped,
            block(&[v6(0xffff_0000_0000, 0xffff_ffff_ffff)]),
        ),
    ];

    let mut reasons = Vec::new();
    let mut rest = unmapped;
    for (reason, block) in blocks {
        let mut outside = rest.clone();
        outside.subtract(&block);
        let mut inside = rest;
        inside.subtract(&outside);
        reasons.push((reason, inside));
        rest = outside;
    }
    reasons.push((Unframed::NoRoute, rest));
    reasons.retain(|(_, targets)| !targets.is_empty());
    reasons
}

/// Whether `target` is an IPv4 address written in the IPv4-mapped IPv6 block,
/// `::ffff:0:0/96`.
fn is_ipv4_mapped(target: IpAddr) -> bool {
    matches!(target, IpAddr::V6(v6) if v6.to_ipv4_mapped().is_some())
}

/// Adds every range of `from` to `into`, leaving the merge to whoever reads it.
fn extend(into: &mut IpSet, from: &IpSet) {
    for range in from.v4() {
        into.push_v4_range(*range);
    }
    for range in from.v6() {
        into.push_v6_range(*range);
    }
}

/// Which of `ip_set` a frame built by `sender` cannot reach from this host.
///
/// Classified the way [`map_ips_to_interfaces_forced`] classifies, and so by the
/// routing table: a routed target is out of reach when the source the kernel
/// would send it from belongs to a link that carries no frames. That is the
/// kernel's own statement of where the packet leaves, and a VPN that routes a
/// target through its tunnel has made it here. The frame sender's own choice of
/// egress is not consulted, because it is the thing whose reach is in question.
pub(crate) fn beyond_frames(ip_set: IpSet, forced: &[IpAddr], sender: FrameSender) -> BeyondFrames {
    beyond_frames_with(ip_set, viable_interfaces(), forced, sender)
}

/// [`beyond_frames`] against an interface table the caller supplies, the seam
/// its decisions are tested through.
pub(crate) fn beyond_frames_with(
    ip_set: IpSet,
    interfaces: Vec<Link>,
    forced: &[IpAddr],
    sender: FrameSender,
) -> BeyondFrames {
    let links = interfaces.clone();
    let RoutedTargets {
        local,
        routed,
        unmapped,
        ours,
        ..
    } = map_ips_to_interfaces_with(ip_set, interfaces, forced);

    // Grouped by reason before anything is counted, since one tunnel can hold
    // targets on its own subnet and targets routed through it alike.
    let mut groups: Vec<(Unframed, IpSet)> = Vec::new();
    let mut add = |reason: Unframed, range: IpRange| match groups
        .iter_mut()
        .find(|(held, _)| *held == reason)
    {
        Some((_, targets)) => targets.insert_range(range),
        None => {
            let mut targets = IpSet::new();
            targets.insert_range(range);
            groups.push((reason, targets));
        }
    };

    for (reason, targets) in unmapped_reasons(unmapped) {
        for range in targets.v4() {
            add(reason.clone(), V4(*range));
        }
        for range in targets.v6() {
            add(reason.clone(), V6(*range));
        }
    }
    for range in ours.v4() {
        add(Unframed::Ours, V4(*range));
    }
    for range in ours.v6() {
        add(Unframed::Ours, V6(*range));
    }

    // Sorted by name, so the order a message lists them in does not depend on a
    // hash map's.
    let mut local: Vec<(Link, IpSet)> = local.into_iter().collect();
    local.sort_by(|(a, _), (b, _)| a.name().cmp(b.name()));
    for (link, targets) in &local {
        // Only a link-local target whose zone named a tunnel is in `local` on
        // a link without frames; a tunnel's own subnet is routed through it.
        if !link.carries_frames() {
            for range in targets.v4() {
                add(Unframed::Tunnel(link.name().to_string()), V4(*range));
            }
            for range in targets.v6() {
                add(Unframed::Tunnel(link.name().to_string()), V6(*range));
            }
        } else if sender == FrameSender::Probe {
            for range in targets.v6() {
                add(Unframed::Neighbour(link.name().to_string()), V6(*range));
            }
        }
    }

    for RoutedTarget { target, source } in routed {
        // A source no link here holds is a forced one naming an address this
        // host does not have. The frame sender builds that frame as asked, and
        // a connect could not honour it, so it is left where it is.
        let Some(owner) = links
            .iter()
            .find(|link| link.addresses().iter().any(|held| held.address() == source))
        else {
            continue;
        };
        if !owner.carries_frames() {
            add(Unframed::Tunnel(owner.name().to_string()), single(target));
        }
    }

    let mut beyond = BeyondFrames::default();
    for (reason, targets) in groups {
        beyond.note(reason, targets);
    }
    beyond.targets.canonicalize();
    beyond
}

/// One address as a range of itself.
fn single(address: IpAddr) -> IpRange {
    match address {
        IpAddr::V4(v4) => V4(Ipv4Range::single(v4)),
        IpAddr::V6(v6) => V6(Ipv6Range::single(v6)),
    }
}

/// Whether an IPv6 range is small enough to probe one address at a time.
///
/// The question every strategy that walks addresses has to ask before it starts,
/// and the reason it is asked of a range rather than of a set: a set holding a
/// `/64` and three literals is partly walkable, and refusing all four of them
/// would throw away three addresses somebody named. See
/// [`MAX_ENUMERABLE_ADDRESSES`].
pub fn is_enumerable(range: &Ipv6Range) -> bool {
    range.len() <= MAX_ENUMERABLE_ADDRESSES
}

/// The segment the whole inclusive range `[start, end]` is on, if it is on one.
///
/// A range is kept whole only where one link answers for every address in
/// it: the segment's, with no narrower prefix on another link inside the range
/// to take some of it elsewhere, a VPN's `/24` within the LAN's `/8` for one.
/// Anything less is expanded and each address asks [`holding_prefix`] for
/// itself.
///
/// A host prefix does not count. A `/32` or `/128` routes only the address it
/// was assigned with, which is this host's own and is withheld as that, and
/// Linux lists every DHCPv6 address as one: splitting an on-link `/64` around
/// it would refuse the segment as too large to walk, where it is swept whole.
fn owning_interface(links: &[Link], start: IpAddr, end: IpAddr) -> Option<usize> {
    let (idx, owner) = holding_prefix(links, start)?;
    let narrower_inside = links
        .iter()
        .enumerate()
        .filter(|(other, _)| *other != idx)
        .flat_map(|(_, link)| link.addresses())
        .filter(|held| held.prefix() > owner.prefix())
        .map(|held| held.network())
        .filter(|network| network.len() > 1)
        .any(|network| {
            let (low, high) = (network.start_addr(), network.end_addr());
            low.is_ipv4() == start.is_ipv4() && low <= end && start <= high
        });

    (links[idx].carries_frames() && owner.contains(&end) && !narrower_inside).then_some(idx)
}

/// The interface holding the most specific prefix that contains `target`,
/// and the address it holds there.
///
/// Each prefix an address carries is a connected route, and the kernel sends
/// by the most specific one, so this does too: a VPN's `/24` inside a LAN's
/// `/8` takes the targets in the `/24`. On a tie the first interface listed
/// wins. Matching is within one address family, the check
/// `LinkAddress::contains` already makes.
///
/// What the prefix means depends on the link. On one that carries frames it is
/// a segment, swept at the link layer. On a tunnel it is only a route: its
/// peers sit inside it, a WireGuard peer on the tunnel's `/24` or an OpenVPN
/// server in subnet topology, and a link-layer strategy handed one sends it
/// nothing, since no frame can be put on the tunnel and nothing behind it
/// answers ARP or neighbour discovery. Such a peer is probed through the
/// tunnel from the tunnel's address.
fn holding_prefix(links: &[Link], target: IpAddr) -> Option<(usize, LinkAddress)> {
    let mut best: Option<(usize, LinkAddress)> = None;
    for (idx, link) in links.iter().enumerate() {
        for held in link.addresses() {
            if held.contains(&target) && best.is_none_or(|(_, b)| held.prefix() > b.prefix()) {
                best = Some((idx, *held));
            }
        }
    }
    best
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::ip::range::{IpRange, Ipv4Range, Ipv6Range};
    use crate::model::mac::MacAddr;
    use std::net::{Ipv4Addr, Ipv6Addr};

    /// An Ethernet interface holding one address, which is the segment the
    /// on-link tests below are about.
    fn mock_named(name: &str, index: u32, ip: IpAddr, prefix: u8) -> Link {
        Link::new(name, index)
            .with_mac(MacAddr::new(0x02, 0, 0, 0, 0, index as u8))
            .with_addressing(crate::system::interface::Addressing::Broadcast)
            .with_addresses(vec![crate::system::interface::LinkAddress::new(ip, prefix)])
    }

    fn mock_interface(ip: IpAddr, prefix: u8) -> Link {
        mock_named("test0", 0, ip, prefix)
    }

    #[test]
    fn the_interface_holding_a_targets_prefix_is_found() {
        let interfaces = vec![
            mock_interface(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 100)), 24),
            mock_interface(IpAddr::V4(Ipv4Addr::new(198, 51, 100, 5)), 24),
        ];
        let holder = |target: [u8; 4]| {
            holding_prefix(&interfaces, IpAddr::V4(Ipv4Addr::from(target))).map(|(idx, _)| idx)
        };

        assert_eq!(holder([192, 0, 2, 50]), Some(0));
        assert_eq!(holder([198, 51, 100, 200]), Some(1));
        assert_eq!(holder([203, 0, 113, 1]), None, "held by neither");
    }

    /// A routed target whose route refuses by policy is given to no raw
    /// strategy, forced source or fallback source: each would send from an
    /// address named outright, which the kernel sends from wherever its
    /// table refuses. Left to the connect fallback, it meets the refusal the
    /// rest of the machine meets. A missing route still gets the fallback,
    /// which is the case the fallback is for.
    #[test]
    fn a_routed_target_a_route_forbids_is_left_to_the_connect_fallback() {
        let global = IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 5));
        let lan = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1));
        let interfaces = || vec![mock_interface(global, 64), mock_interface(lan, 24)];
        let v6 = IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 9, 0, 0, 0, 0, 1));
        let v4 = IpAddr::V4(Ipv4Addr::new(203, 0, 113, 1));
        let targets = || {
            let mut set = IpSet::new();
            set.insert(v6);
            set.insert(v4);
            set
        };

        let forbidden = map_ips_to_interfaces_asking(targets(), interfaces(), &[lan], |_| {
            RouteAnswer::Forbidden
        });
        assert!(forbidden.routed.is_empty(), "{:?}", forbidden.routed);
        assert!(forbidden.unmapped.contains(&v6) && forbidden.unmapped.contains(&v4));

        let missing =
            map_ips_to_interfaces_asking(targets(), interfaces(), &[], |_| RouteAnswer::NoRoute);
        assert!(
            missing
                .routed
                .iter()
                .any(|routed| routed.target == v6 && routed.source == global),
            "{:?}",
            missing.routed
        );
    }

    /// A neighbour the routing table refuses is found among a segment's
    /// targets, a walked range's as much as a named address's, and nothing
    /// else is: not its neighbours, not the segment's network and broadcast
    /// addresses, which a kernel refuses a connect to on other grounds, and
    /// not a link-local address, which no table routes without its zone.
    #[test]
    fn a_neighbour_the_routing_table_refuses_is_told_apart_from_the_segment() {
        let link = mock_interface(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1)), 24);
        let mut targets = IpSet::new();
        targets.insert_range(IpRange::V4(
            Ipv4Range::new(Ipv4Addr::new(192, 0, 2, 0), Ipv4Addr::new(192, 0, 2, 255)).unwrap(),
        ));
        targets.insert(IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 13)));
        targets.insert(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 13)));

        // Refuses every address whose last group is 13, and the segment's
        // edges, as a kernel refuses a broadcast.
        let refused = refused_neighbours_asking(&link, &targets, |target| match target {
            IpAddr::V4(v4) => matches!(v4.octets()[3], 0 | 13 | 255),
            IpAddr::V6(v6) => v6.segments()[7] == 13,
        });

        let mut expected = IpSet::new();
        expected.insert(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 13)));
        expected.insert(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 13)));
        expected.canonicalize();
        assert_eq!(refused, expected);
    }

    #[test]
    fn on_link_v4_range_stays_intact_and_local() {
        let interfaces = vec![mock_interface(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1)), 24)];
        let mut set = IpSet::new();
        set.insert_range(IpRange::V4(
            Ipv4Range::new(Ipv4Addr::new(192, 0, 2, 10), Ipv4Addr::new(192, 0, 2, 20)).unwrap(),
        ));

        let result = map_ips_to_interfaces_with(set, interfaces, &[]);

        assert!(result.routed.is_empty());
        assert!(result.unmapped.is_empty());
        assert_eq!(result.local.len(), 1);
        let (_, ips) = result.local.into_iter().next().unwrap();
        assert_eq!(ips.len(), 11);
    }

    /// The boundary of the enumeration ceiling, checked exactly rather than by
    /// expanding a range: a `/112` is probed, a `/111` is not.
    #[test]
    fn the_enumeration_ceiling_is_a_112() {
        let base = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0);
        let at_ceiling = Ipv6Range::new(base, Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0xffff));
        let over_ceiling = Ipv6Range::new(base, Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 1, 0));

        let at_ceiling = at_ceiling.unwrap();
        assert_eq!(at_ceiling.len(), MAX_ENUMERABLE_ADDRESSES);
        assert!(is_enumerable(&at_ceiling), "a /112 is 65536 addresses");
        assert!(
            !is_enumerable(&over_ceiling.unwrap()),
            "one address more is not"
        );
    }

    /// The failure this ceiling exists to prevent: a routed `/64` expanded into
    /// a `Vec<IpAddr>` is 2^64 allocations, which is not a slow scan but an
    /// out-of-memory condition reached from a perfectly ordinary target
    /// expression.
    ///
    /// It has to come out as its own category. Silently dropping it would report
    /// an empty scan of a range nobody probed, and a caller cannot tell that
    /// from a range with nothing on it.
    #[test]
    fn a_routed_v6_prefix_too_large_to_walk_is_refused_rather_than_expanded() {
        let interfaces = vec![mock_interface(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1)), 24)];
        let mut set = IpSet::new();
        set.insert_range(IpRange::V6(
            Ipv6Range::new(
                Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0),
                Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0xffff, 0xffff, 0xffff, 0xffff),
            )
            .unwrap(),
        ));

        let result = map_ips_to_interfaces_with(set, interfaces, &[]);

        assert_eq!(result.unenumerable.len(), 1, "the /64 is reported whole");
        assert!(result.routed.is_empty());
        assert!(result.unmapped.is_empty());
        assert!(result.local.is_empty());
    }

    /// The silent wrong answer this refusal exists to prevent.
    ///
    /// Every interface holds an `fe80::/64`, so a bare link-local target matches
    /// all of them and `owning_interface` returns whichever the host listed
    /// first. On a laptop with two dozen interfaces that is close to a random
    /// choice: the scan probes one segment, hears nothing, and reports a host
    /// that was present on another as absent.
    #[test]
    fn a_link_local_target_naming_no_interface_is_refused() {
        let link_local = Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 0xAA);
        let interfaces = vec![
            mock_interface(IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)), 64),
            mock_interface(IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 2)), 64),
        ];
        let mut set = IpSet::new();
        set.insert(IpAddr::V6(link_local));

        let result = map_ips_to_interfaces_with(set, interfaces, &[]);

        assert_eq!(result.ambiguous.len(), 1);
        assert!(
            result.local.is_empty(),
            "guessing an interface is what this prevents"
        );
    }

    /// Named, the same target is unambiguous, and the name outranks any prefix
    /// match: every interface matches the prefix, so a prefix match is no
    /// evidence at all.
    #[test]
    fn a_link_local_target_goes_to_the_interface_it_names() {
        let link_local = Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 0xAA);
        let first = mock_named(
            "en3",
            3,
            IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)),
            64,
        );
        let second = mock_named(
            "en9",
            9,
            IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 2)),
            64,
        );

        let mut set = IpSet::new();
        set.insert_range(IpRange::V6(
            Ipv6Range::scoped(link_local, link_local, Some(9)).unwrap(),
        ));

        let result = map_ips_to_interfaces_with(set, vec![first, second], &[]);

        assert!(result.ambiguous.is_empty());
        assert_eq!(result.local.len(), 1);
        let (intf, ips) = result.local.into_iter().next().unwrap();
        assert_eq!(intf.name(), "en9", "the second interface was the one named");
        assert_eq!(ips.len(), 1);
    }

    #[test]
    fn on_link_v6_range_is_classified_local() {
        let base = Ipv6Addr::new(0xfd00, 0, 0, 0, 0, 0, 0, 0);
        let interfaces = vec![mock_interface(IpAddr::V6(base), 64)];
        let mut set = IpSet::new();
        set.insert_range(IpRange::V6(
            Ipv6Range::new(
                Ipv6Addr::new(0xfd00, 0, 0, 0, 0, 0, 0, 1),
                Ipv6Addr::new(0xfd00, 0, 0, 0, 0, 0, 0, 5),
            )
            .unwrap(),
        ));

        let result = map_ips_to_interfaces_with(set, interfaces, &[]);

        assert!(result.routed.is_empty());
        assert!(result.unmapped.is_empty());
        assert_eq!(result.local.len(), 1);
        let (_, ips) = result.local.into_iter().next().unwrap();
        assert_eq!(ips.len(), 5);
    }

    /// The case that sends a scan of this host's own LAN address looking for an
    /// ARP reply nothing would send: the address sits inside its own
    /// interface's subnet, so the on-link test claims it, and no probe can
    /// establish it because the kernel routes it through loopback.
    #[test]
    fn an_address_this_host_holds_is_ours_rather_than_on_link() {
        let own: IpAddr = "203.0.113.160".parse().unwrap();
        let interfaces = vec![mock_interface(own, 24)];

        let mut targets = IpSet::new();
        targets.insert(own);

        let routed = map_ips_to_interfaces_with(targets, interfaces, &[]);

        assert!(routed.ours.contains(&own), "the address is this host's own");
        assert!(
            routed.local.is_empty(),
            "and must not be handed to a link-layer strategy"
        );
    }

    /// A neighbour on the same segment still gets the on-link strategy, which is
    /// the half of the distinction that has to keep working.
    #[test]
    fn a_neighbour_on_the_same_segment_is_still_on_link() {
        let own: IpAddr = "203.0.113.160".parse().unwrap();
        let neighbour: IpAddr = "203.0.113.101".parse().unwrap();
        let interfaces = vec![mock_interface(own, 24)];

        let mut targets = IpSet::new();
        targets.insert(neighbour);

        let routed = map_ips_to_interfaces_with(targets, interfaces, &[]);

        assert!(routed.ours.is_empty());
        assert_eq!(
            routed.local.values().next().map(IpSet::len),
            Some(1u128),
            "the neighbour is on-link"
        );
    }

    /// A link a frame can be put on: a hardware address and a segment.
    fn ethernet(addresses: &[(&str, u8)]) -> Link {
        Link::new("en0", 4)
            .with_mac(crate::model::mac::MacAddr::new(0x02, 0, 0, 0, 0, 0x10))
            .with_kind(crate::system::interface::LinkKind::Wired)
            .with_addressing(crate::system::interface::Addressing::Broadcast)
            .with_link_up(true)
            .with_addresses(held(addresses))
    }

    /// A VPN's tunnel, as macOS presents one: a peer rather than a segment, and
    /// no hardware address.
    fn tunnel(addresses: &[(&str, u8)]) -> Link {
        Link::new("utun9", 20)
            .with_addressing(crate::system::interface::Addressing::PointToPoint)
            .with_link_up(true)
            .with_addresses(held(addresses))
    }

    fn held(addresses: &[(&str, u8)]) -> Vec<crate::system::interface::LinkAddress> {
        addresses
            .iter()
            .map(|(address, prefix)| {
                crate::system::interface::LinkAddress::new(
                    address.parse().expect("a literal"),
                    *prefix,
                )
            })
            .collect()
    }

    fn set_of(addresses: &[&str]) -> IpSet {
        let mut set = IpSet::new();
        for address in addresses {
            set.insert(address.parse().expect("a literal"));
        }
        set
    }

    fn ip(literal: &str) -> IpAddr {
        literal.parse().expect("a literal")
    }

    /// Without its own check, `::1` would come out of the classifier as a
    /// routed target paired with a global source, because the kernel answers it
    /// from `::1`, no viable interface holds that, and the VPN fallback then
    /// offers the first global address it finds. `127.0.0.1` would be spared
    /// only because that fallback declines IPv4. A forced source would do the
    /// same to both.
    #[test]
    fn loopback_is_unmapped_in_both_families_whatever_is_forced() {
        let interfaces = vec![ethernet(&[("192.0.2.10", 24), ("2001:db8:1::10", 64)])];
        let forced = [ip("192.0.2.10"), ip("2001:db8:1::10")];

        for forced in [&forced[..], &[]] {
            let routed = map_ips_to_interfaces_with(
                set_of(&["127.0.0.1", "::1"]),
                interfaces.clone(),
                forced,
            );

            assert!(
                routed.routed.is_empty(),
                "loopback is not behind a gateway: {:?}",
                routed.routed
            );
            assert!(routed.unmapped.contains(&ip("127.0.0.1")));
            assert!(routed.unmapped.contains(&ip("::1")));
        }
    }

    /// An IPv4-mapped address is an IPv4 host no frame can carry, so it is never
    /// paired with an IPv6 source and framed toward the router, whatever source
    /// is forced and whatever global address the host holds.
    #[test]
    fn a_mapped_address_is_unmapped_whatever_is_forced() {
        let interfaces = vec![ethernet(&[("192.0.2.10", 24), ("2001:db8:1::10", 64)])];
        let forced = [ip("192.0.2.10"), ip("2001:db8:1::10")];

        for forced in [&forced[..], &[]] {
            let routed = map_ips_to_interfaces_with(
                set_of(&["::ffff:127.0.0.1", "::ffff:198.51.100.7"]),
                interfaces.clone(),
                forced,
            );

            assert!(
                routed.routed.is_empty(),
                "a mapped address is not behind a gateway: {:?}",
                routed.routed
            );
            assert!(routed.unmapped.contains(&ip("::ffff:127.0.0.1")));
            assert!(routed.unmapped.contains(&ip("::ffff:198.51.100.7")));
        }
    }

    /// And a frames-only run says why it reaches one by connect rather than
    /// calling it unroutable.
    #[test]
    fn a_mapped_address_is_beyond_frames_as_what_it_is() {
        let beyond = beyond_frames_with(
            set_of(&["::ffff:198.51.100.7"]),
            vec![ethernet(&[("192.0.2.10", 24), ("2001:db8:1::10", 64)])],
            &[],
            FrameSender::Probe,
        );

        assert_eq!(
            beyond.summary(),
            vec![(Unframed::Mapped, ip("::ffff:198.51.100.7"), 1)]
        );
    }

    /// A connect step holds what a frame cannot reach and what no route leads
    /// to, and a message about it names each address for what it is: loopback
    /// as loopback, a mapped address as mapped, whichever block it was
    /// written in, and the rest as having no route. An address already held
    /// keeps the reason it was held for.
    #[test]
    fn what_no_route_leads_to_is_named_for_what_it_is() {
        let held = beyond_frames_with(
            set_of(&["::ffff:198.51.100.7"]),
            vec![ethernet(&[("192.0.2.10", 24), ("2001:db8:1::10", 64)])],
            &[],
            FrameSender::Probe,
        );

        let all = held.and_unmapped(&set_of(&[
            "::ffff:198.51.100.7",
            "127.0.0.2",
            "::1",
            "::ffff:127.0.0.1",
            "203.0.113.9",
            "203.0.113.10",
        ]));

        assert_eq!(
            all.summary(),
            vec![
                (Unframed::Mapped, ip("::ffff:127.0.0.1"), 2),
                (Unframed::Loopback, ip("127.0.0.2"), 2),
                (Unframed::NoRoute, ip("203.0.113.9"), 2),
            ]
        );
        assert_eq!(all.targets.len(), 6);
    }

    /// What a frame reaches, which has to keep working for the split to be worth
    /// anything: a neighbour it can ARP for, and a routed target whose route
    /// leaves by a link with Ethernet in front of it.
    #[test]
    fn a_frame_reaches_an_ipv4_neighbour_and_a_target_routed_over_ethernet() {
        let beyond = beyond_frames_with(
            set_of(&["192.0.2.50", "203.0.113.9"]),
            vec![ethernet(&[("192.0.2.10", 24)])],
            &[ip("192.0.2.10")],
            FrameSender::Probe,
        );

        assert!(beyond.is_empty(), "nothing out of reach: {beyond:?}");
    }

    /// The case a VPN makes: the kernel sends the target from the tunnel's
    /// address, so its route leaves by a link no frame can be put on, and the
    /// frame sender's own guess at an egress is not what decides it.
    #[test]
    fn a_target_routed_through_a_tunnel_is_beyond_frames() {
        let beyond = beyond_frames_with(
            set_of(&["203.0.113.23"]),
            vec![
                ethernet(&[("192.0.2.10", 24)]),
                tunnel(&[("198.51.100.2", 32)]),
            ],
            &[ip("198.51.100.2")],
            FrameSender::Sweep,
        );

        assert!(beyond.targets.contains(&ip("203.0.113.23")));
        assert_eq!(
            beyond.summary(),
            vec![(Unframed::Tunnel("utun9".into()), ip("203.0.113.23"), 1)]
        );
    }

    /// A tunnel with a prefix of its own claims its subnet as on-link, and it is
    /// no more a segment for that. Its own subnet and a target routed through it
    /// are one reason, counted once.
    #[test]
    fn a_tunnels_own_subnet_is_beyond_frames_under_the_same_reason() {
        let beyond = beyond_frames_with(
            set_of(&["198.51.100.7", "203.0.113.23"]),
            vec![tunnel(&[("198.51.100.2", 24)])],
            &[ip("198.51.100.2")],
            FrameSender::Sweep,
        );

        assert_eq!(beyond.targets.len(), 2);
        assert_eq!(
            beyond.summary(),
            vec![(Unframed::Tunnel("utun9".into()), ip("198.51.100.7"), 2)]
        );
    }

    /// A WireGuard peer, or an OpenVPN server in subnet topology, sits inside
    /// the prefix the tunnel's own address carries. That prefix is a route
    /// through the tunnel and not a segment, so the peer is probed through the
    /// tunnel from the tunnel's address, the way the kernel would send to it.
    #[test]
    fn a_host_on_a_tunnels_own_subnet_is_routed_through_the_tunnel() {
        let interfaces = vec![
            ethernet(&[("192.0.2.10", 24)]),
            tunnel(&[("198.51.100.2", 24), ("2001:db8:66::2", 64)]),
        ];

        let routed = map_ips_to_interfaces_with(
            set_of(&["198.51.100.1", "2001:db8:66::1"]),
            interfaces,
            &[],
        );

        assert!(
            routed.local.is_empty(),
            "a tunnel has no segment to sweep: {:?}",
            routed.local
        );
        assert_eq!(
            routed.routed,
            vec![
                RoutedTarget {
                    target: ip("198.51.100.1"),
                    source: ip("198.51.100.2"),
                },
                RoutedTarget {
                    target: ip("2001:db8:66::1"),
                    source: ip("2001:db8:66::2"),
                },
            ]
        );
    }

    /// A forced source is for a target the routing table would send through
    /// the wrong interface. A peer on the tunnel's own subnet is reached through
    /// that tunnel and nowhere else, so it keeps the tunnel's address as a
    /// neighbour on a segment keeps the segment's.
    #[test]
    fn a_forced_source_does_not_take_a_host_off_its_tunnels_subnet() {
        let routed = map_ips_to_interfaces_with(
            set_of(&["198.51.100.1"]),
            vec![
                ethernet(&[("192.0.2.10", 24)]),
                tunnel(&[("198.51.100.2", 24)]),
            ],
            &[ip("192.0.2.10")],
        );

        assert_eq!(
            routed.routed,
            vec![RoutedTarget {
                target: ip("198.51.100.1"),
                source: ip("198.51.100.2"),
            }]
        );
    }

    /// A sweep of the tunnel's whole subnet is a list of peers to probe through
    /// it, less the tunnel's own address, which is this host's.
    #[test]
    fn a_tunnels_subnet_as_a_range_is_routed_address_by_address() {
        let mut targets = IpSet::new();
        targets.insert_range(V4(Ipv4Range::new(
            "198.51.100.0".parse().unwrap(),
            "198.51.100.3".parse().unwrap(),
        )
        .expect("an ordered range")));

        let routed =
            map_ips_to_interfaces_with(targets, vec![tunnel(&[("198.51.100.2", 24)])], &[]);

        assert!(routed.local.is_empty(), "{:?}", routed.local);
        let probed: Vec<IpAddr> = routed.routed.iter().map(|r| r.target).collect();
        assert_eq!(
            probed,
            ["198.51.100.0", "198.51.100.1", "198.51.100.3"].map(ip)
        );
        assert!(routed.routed.iter().all(|r| r.source == ip("198.51.100.2")));
        assert!(routed.ours.contains(&ip("198.51.100.2")));
        assert_eq!(routed.ours.len(), 1);
    }

    /// Prefixes nest, and the most specific one is the route, as it is in the
    /// kernel: a VPN's prefix inside the LAN's takes its own targets, and a
    /// LAN inside a tunnel's wider prefix keeps its neighbours.
    #[test]
    fn the_most_specific_prefix_decides_between_a_segment_and_a_tunnel() {
        let interfaces = vec![
            ethernet(&[("198.51.100.10", 24), ("2001:db8:0:1::10", 64)]),
            tunnel(&[("198.51.100.130", 25), ("2001:db8::2", 40)]),
        ];

        let routed = map_ips_to_interfaces_with(
            set_of(&[
                "198.51.100.7",
                "198.51.100.200",
                "2001:db8:0:1::20",
                "2001:db8:5::1",
            ]),
            interfaces,
            &[],
        );

        let swept: Vec<IpAddr> = routed.local.values().flat_map(IpSet::iter).collect();
        assert_eq!(swept, ["198.51.100.7", "2001:db8:0:1::20"].map(ip));
        assert_eq!(
            routed.routed,
            vec![
                RoutedTarget {
                    target: ip("198.51.100.200"),
                    source: ip("198.51.100.130"),
                },
                RoutedTarget {
                    target: ip("2001:db8:5::1"),
                    source: ip("2001:db8::2"),
                },
            ]
        );
    }

    /// A segment's range stays whole around a host prefix, on its own link or
    /// on a tunnel. Each covers only an address this host holds, and splitting
    /// an on-link `/64` around one would refuse it as too large to walk.
    #[test]
    fn a_host_prefix_inside_a_segments_range_leaves_the_range_whole() {
        let mut lan = ethernet(&[("2001:db8:1::10", 64)]);
        lan = lan.with_addresses(held(&[("2001:db8:1::10", 64), ("2001:db8:1::abcd", 128)]));
        let interfaces = vec![lan, tunnel(&[("2001:db8:1::99", 128)])];

        let mut targets = IpSet::new();
        targets.insert_range(V6(Ipv6Range::new(
            "2001:db8:1::".parse().unwrap(),
            "2001:db8:1::ffff:ffff:ffff:ffff".parse().unwrap(),
        )
        .expect("an ordered range")));

        let routed = map_ips_to_interfaces_with(targets, interfaces, &[]);

        assert!(routed.unenumerable.is_empty(), "{:?}", routed.unenumerable);
        assert_eq!(routed.local.len(), 1, "the /64 is swept on its segment");
    }

    /// The one place the two frame builders differ. The segment sweep resolves
    /// an IPv6 neighbour itself; the probe sender has ARP and nothing else.
    #[test]
    fn an_ipv6_neighbour_is_within_the_sweep_and_beyond_the_probe_sender() {
        let interfaces = vec![ethernet(&[("192.0.2.10", 24), ("2001:db8:1::10", 64)])];
        let targets = set_of(&["2001:db8:1::20", "192.0.2.50"]);

        let swept =
            beyond_frames_with(targets.clone(), interfaces.clone(), &[], FrameSender::Sweep);
        assert!(swept.is_empty(), "the sweep reaches both: {swept:?}");

        let probed = beyond_frames_with(targets, interfaces, &[], FrameSender::Probe);
        assert_eq!(
            probed.summary(),
            vec![(Unframed::Neighbour("en0".into()), ip("2001:db8:1::20"), 1)],
            "and the probe sender only the IPv4 one"
        );
    }

    /// The kernel answers these itself, so a frame for either reaches nobody who
    /// replies. Two reasons, in the order a message names them.
    #[test]
    fn loopback_and_this_hosts_own_addresses_are_beyond_frames() {
        let beyond = beyond_frames_with(
            set_of(&["127.0.0.1", "::1", "192.0.2.10"]),
            vec![ethernet(&[("192.0.2.10", 24)])],
            &[],
            FrameSender::Probe,
        );

        assert_eq!(
            beyond.summary(),
            vec![
                (Unframed::Loopback, ip("127.0.0.1"), 2),
                (Unframed::Ours, ip("192.0.2.10"), 1),
            ]
        );
    }

    /// The VPN case made deterministic: a routed target the kernel would send
    /// from the tunnel is sent from the forced LAN source instead, and the
    /// routing table is never asked - the source picks itself by family.
    #[test]
    fn a_forced_source_outranks_the_routing_table_for_a_routed_target() {
        let lan: IpAddr = "203.0.113.160".parse().unwrap();
        let public: IpAddr = "1.1.1.1".parse().unwrap();
        let interfaces = vec![mock_interface(lan, 24)];

        let mut targets = IpSet::new();
        targets.insert(public);

        let routed = map_ips_to_interfaces_with(targets, interfaces, &[lan]);

        assert_eq!(
            routed.routed,
            vec![RoutedTarget {
                target: public,
                source: lan
            }],
            "the off-link target is probed from the forced source"
        );
    }
}