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
// 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
//! # Sending and hearing whole frames on one segment
//!
//! What a local sweep holds: somewhere to put Ethernet frames, and the frames
//! that arrived on the same link.
//!
//! ## The two halves come from different places, and should
//!
//! The send half is a `pnet` link-layer channel, because emitting a frame this
//! crate built byte for byte is what it is for. The receive half is a
//! [`capture`], because everything that makes a
//! receive path trustworthy lives there: a BPF filter the *kernel* applies, the
//! counters saying what the kernel discarded anyway, and a stop flag every
//! reader thread checks so that dropping the handle ends them.
//!
//! It was one channel doing both until this was split. That channel copied every
//! frame on the segment into this process to throw nearly all of them away, could
//! not say what it had lost, and its reader thread had no way to be told to stop.
//! All three are properties of the receive side alone, and all three were already
//! solved one module over.
use crateLink;
use crateCaptureCounts;
use crate;
/// How many frames may wait for the consumer at once.
///
/// A sweep reads its queue inside the same loop that paces its probes, so a tick
/// spent sending is a tick not spent receiving, and the queue is what covers
/// that gap. Sized for a burst, since every host on a `/24` answering an ARP
/// sweep at once is a few hundred frames, rather than for a sustained rate, which
/// the caller's filter is what keeps small.
const QUEUE_DEPTH: usize = 1024;
/// A live Ethernet channel: somewhere to put frames, and a stream of the frames
/// that arrived.
///
/// This is the link-layer counterpart to
/// [`ProbeTransport`](crate::transport::probe::ProbeTransport), and deliberately
/// not the same type. A probe transport carries Layer-4 segments with the link
/// and IP headers already stripped, which is all the SYN and UDP scanners need.
/// Local discovery needs the whole frame: it identifies a neighbour by the
/// Ethernet source MAC, and reads ARP, which has no Layer-4 segment to strip to.
/// Why a link-layer channel could not be opened.
///
/// The variants name the interface, because a scan opens one channel per segment
/// it means to sweep and "opening a channel failed" is not actionable without
/// knowing which.
/// Two halves open here and either can refuse, so which one did is the whole of
/// what this says. Both usually fail for the same underlying reason, that sending
/// and receiving raw frames needs root everywhere this engine runs, and a person
/// reading the message needs to know whether their probes would have
/// left, not only that something went wrong.
///
/// There were three variants and one of them was ever constructed. `NoCapture`
/// carried both failures, including the send half's, while its own
/// documentation said the send half had already opened; `Open` and
/// `NotEthernet` were named, documented and unreachable.
/// Opens both halves on one interface: a link-layer sender, and a capture of the
/// frames that link carries which `filter` admits.
///
/// The capture is promiscuous and `filter` is what narrows it, two halves of one
/// decision, argued at
/// [`CaptureOptions::for_link_traffic`](crate::transport::capture::CaptureOptions::for_link_traffic).
///
/// The filter belongs to the caller, since what a frame is worth is decided by
/// whoever reads it. This module knows how to open a capture and
/// nothing about ARP, neighbour discovery or DHCP; a filter written here would
/// be a scanner's knowledge kept one layer below the scanner, and would go stale
/// the first time a reader was added without anybody thinking to look down here.