ts_dataplane/lib.rs
1#![doc = include_str!("../README.md")]
2
3use std::{collections::HashMap, sync::Arc, time::Instant};
4
5use ts_bart::RoutingTable;
6use ts_overlay_router as or;
7use ts_packet::PacketMut;
8use ts_packetfilter::{FilterExt, IpProto};
9use ts_time::{Handle, Scheduler};
10use ts_transport::{OverlayTransportId, PeerId, UnderlayTransportId};
11use ts_tunnel::{Endpoint, NodeKeyPair};
12use ts_underlay_router as ur;
13
14pub mod async_tokio;
15
16mod flowtrack;
17
18/// The single link-local destination Go's filter `pre()` exempts from the link-local drop: the
19/// cloud-metadata address `169.254.169.254` (Go `isAllowedLinkLocal`).
20const ALLOWED_LINK_LOCAL_V4: std::net::Ipv4Addr = std::net::Ipv4Addr::new(169, 254, 169, 254);
21
22/// Whether an inbound packet to destination `dst` must be dropped BEFORE consulting the ACL rules,
23/// mirroring Go's filter `pre()`: drop multicast destinations (`ReasonMulticast`) and link-local
24/// unicast destinations that are not the allowlisted cloud-metadata address (`ReasonLinkLocalUnicast`).
25/// Returning `true` means drop. This runs ahead of `can_access` so a permissive ACL cannot admit the
26/// multicast / link-local traffic Go rejects unconditionally.
27///
28/// Go's `isAllowedLinkLocal` is `dst == gcpDNSAddr || any(LinkLocalAllowHooks)`; only the static
29/// `gcpDNSAddr` arm is modeled here. The dynamic `LinkLocalAllowHooks` slice is empty in a plain
30/// engine/tsnet embedding (its only upstream producer is the GCP metadata path), so the omission is
31/// behaviorally equivalent for this fork; a feature that needs a dynamic link-local allowlist would
32/// have to extend this. Like Go's `netip.Addr` predicates, an IPv4-mapped-IPv6 destination (e.g.
33/// `::ffff:224.0.0.1`) matches NEITHER arm and falls through to the ACL — we deliberately do not
34/// canonicalize/unmap, to stay byte-faithful to Go (see the mapped-v6 test cases).
35fn drop_before_rules(dst: std::net::IpAddr) -> bool {
36 if dst.is_multicast() {
37 return true;
38 }
39 match dst {
40 // IPv4 link-local is 169.254.0.0/16; allow only the cloud-metadata address (Go parity).
41 std::net::IpAddr::V4(v4) => v4.is_link_local() && v4 != ALLOWED_LINK_LOCAL_V4,
42 // IPv6 unicast link-local is fe80::/10. (`Ipv6Addr::is_unicast_link_local` is unstable, so
43 // test the prefix directly.) This fork is IPv4-only by default, but match Go for any v6.
44 std::net::IpAddr::V6(v6) => (v6.segments()[0] & 0xffc0) == 0xfe80,
45 }
46}
47
48/// IPv4 fragment state read from the base header (Go `net/packet.decode4` reads `b[6:8]`): the
49/// fragment offset in 8-byte blocks and the more-fragments flag. A non-first fragment carries no L4
50/// header, so it needs its own verdict path rather than the (always-port-0) ACL match.
51#[derive(Debug, Clone, Copy)]
52struct Ipv4Fragment {
53 /// Fragment offset in 8-byte blocks (the 13-bit IPv4 field), 0 for the first/only fragment.
54 offset_blocks: u16,
55 /// The "more fragments" (MF) flag.
56 more_fragments: bool,
57}
58
59/// Minimum fragment offset (in 8-byte blocks) Go permits for a non-first fragment — Go
60/// `net/packet.minFragBlks = (60 + 20) / 8 = 10` (max IPv4 header + a basic TCP header). A later
61/// fragment starting before this could overlap a transport header (the RFC 1858 overlapping-fragment
62/// evasion), so Go demotes it to `unknown` and drops it; only fragments at or beyond this offset are
63/// allowed to "slide through".
64///
65/// Upstream reuses this one bound for IPv6 too (Go `net/packet` `26b2ed0a6` documents the reuse):
66/// it is sized for IPv4 and is therefore *conservative* for IPv6, whose fragments carry no
67/// per-fragment IP header — so on the v6 side it only ever rejects more later fragments as
68/// `unknown`, never fewer. Keep the single constant for both, exactly as Go does.
69const MIN_FRAG_BLKS: u16 = (60 + 20) / 8;
70
71/// Minimum IPv4 base header length (Go `net/packet.ip4HeaderLength`). A buffer shorter than this
72/// is not a decodable IPv4 packet at all (Go `decode4` returns `unknown`).
73const IP4_HEADER_LEN: usize = 20;
74
75/// Fixed IPv6 base header length (Go `net/packet.ip6HeaderLength`).
76const IP6_HEADER_LEN: usize = 40;
77
78/// IANA protocol number of the IPv6 Fragment extension header, "IPv6-Frag" (Go
79/// `net/packet.ip6FragHeader`). It appears as the **base** header's Next Header on a
80/// source-fragmented IPv6 packet, and is distinct from Go's internal `ipproto.Fragment` sentinel
81/// (0xff), which marks a non-first fragment whose sub-protocol header is not present.
82const IP6_FRAG_HEADER: u8 = 44;
83
84/// Go's `ipproto.Unknown` (0). Go's decoders assign it to every packet they refuse to classify, and
85/// filter `pre()` drops it — `if q.IPProto == ipproto.Unknown { return Drop }` — before the ACL can
86/// see the packet. It is also the real IANA number of the IPv6 Hop-by-Hop Options extension header,
87/// which is why an IPv6 packet that leads with Hop-by-Hop is dropped by upstream: `decode6` reads
88/// the base header's Next Header byte straight into `q.IPProto`, and 0 *is* "unknown".
89const IPPROTO_UNKNOWN: IpProto = IpProto::new(0);
90
91/// TCP's IP protocol number as a single byte, for the one place it is written onto the wire rather
92/// than matched against: the `Proto` field of a TSMP rejected-connection message, which is a byte
93/// (Go `ipproto.Proto`) where this fork's [`IpProto`] is `i64`-wide.
94const IPPROTO_TCP_BYTE: u8 = 6;
95
96/// Go's internal `ipproto.Fragment` sentinel (0xff), which `decode6Fragment` assigns to a later
97/// fragment. Seeing it as a real Next Header on the wire is suspicious, so Go's `decode6` switch
98/// maps it back to [`IPPROTO_UNKNOWN`] (`case ipproto.Fragment: q.IPProto = unknown`) — whether it
99/// arrived as the base header's Next Header or as a Fragment header's.
100const IPPROTO_FRAGMENT_SENTINEL: IpProto = IpProto::new(0xff);
101
102/// Length of the IPv6 Fragment extension header (Go `net/packet.ip6FragHeaderLength`): Next Header,
103/// Reserved, a 13-bit Fragment Offset in 8-byte blocks plus two reserved bits and the
104/// More-Fragments flag, then a 32-bit Identification.
105const IP6_FRAG_HEADER_LEN: usize = 8;
106
107/// Length of the SCTP common header (Go `net/packet.sctpHeaderLength`): source port, destination
108/// port, verification tag, checksum. Go's `decode4`/`decode6` refuse an SCTP packet shorter than
109/// this rather than guess at its ports.
110const SCTP_HEADER_LEN: usize = 12;
111
112/// How an IPv6 packet whose base header's Next Header is the Fragment extension header classifies —
113/// the port of Go `net/packet.Parsed.decode6Fragment` plus the sub-protocol switch `decode6` runs
114/// when it reports `continueDecode` (upstream `4c4ec3d46`, clarified by `26b2ed0a6`).
115///
116/// This is the IPv6 half of the RFC 1858 fragment rules [`Ipv4Fragment`] already carries. It only
117/// matters on the opt-in `Config::enable_ipv6` path — the tailnet is IPv4-only by default — but
118/// without it a source-fragmented IPv6 datagram reaches the ACL with no sub-protocol and port 0,
119/// so an allow-all rule admits the very low-offset fragments upstream drops, and a port-scoped rule
120/// blackholes the later fragments upstream passes through.
121#[derive(Debug, Clone, Copy, PartialEq, Eq)]
122enum Ipv6Fragment {
123 /// Go's `unknown`, which filter `pre()` drops outright: a Fragment header truncated by the
124 /// packet, a *first* fragment too short to hold its own transport header, a later fragment at
125 /// an offset small enough to overlap that transport header on reassembly (RFC 1858), the
126 /// on-the-wire use of Go's internal `ipproto.Fragment` sentinel, or a Fragment header reached
127 /// through a chained extension header rather than as the base header's immediate Next Header
128 /// ([`fragment_header_is_chained`]).
129 Unknown,
130 /// Go's `ipproto.Fragment`: a later fragment at a safe offset. It carries no sub-protocol
131 /// header, so there is nothing for a rule to match on and filter `pre()` passes it through
132 /// ahead of the ACL — statelessly, exactly as for IPv4. RFC 8200 §4.5 requires the receiver to
133 /// reassemble, and its kernel drops the pieces if the head fragment never arrives.
134 Later,
135 /// Go's `continueDecode == true`: the first fragment. `decode6` steps over the 8-byte Fragment
136 /// header and parses the real sub-protocol's header, so the ACL matches this datagram on the
137 /// same rule it would match unfragmented.
138 First {
139 /// The Fragment header's Next Header — the real sub-protocol (Go `q.IPProto = nextHdr`).
140 proto: IpProto,
141 /// The source port read from that sub-protocol's header, 0 for a protocol Go does not
142 /// port-match (Go `withPort(q.Src, ...)`). Only the reverse-flow cache
143 /// ([`flowtrack::FlowCache`]) reads it — the ACL matches on the destination port alone —
144 /// but Go's `flowtrack.Tuple` keys on both, so both have to be carried.
145 src_port: u16,
146 /// The destination port read from that sub-protocol's header, 0 for a protocol Go does not
147 /// port-match (Go `withPort(q.Dst, ...)`).
148 dst_port: u16,
149 /// The rest of what Go's `decode6` reads out of that same sub-protocol header: the TCP
150 /// flags byte (`q.TCPFlags = TCPFlag(sub[13])`) or the ICMPv6 type/code. The inbound
151 /// filter's reply carve-outs consult it, so a first fragment carrying the SYN-ACK for this
152 /// node's own connection is admitted on exactly the terms an unfragmented one is.
153 l4: ts_packetfilter::L4Header,
154 },
155}
156
157/// Classify a whole IPv6 packet `b` whose base header's Next Header is [`IP6_FRAG_HEADER`], as Go
158/// `net/packet.Parsed.decode6` does when it dispatches to `decode6Fragment`.
159///
160/// Callers must have already checked that immediate Next Header byte: Go parses the Fragment header
161/// **only** as the base header's immediate next header (upstream `26b2ed0a6` added a test locking
162/// that scoping in). No other extension header, and no IPSec AH/ESP header, is parsed here either —
163/// same as Go. A Fragment header reached through a chained extension header is *not* this
164/// function's business; it is [`fragment_header_is_chained`]'s, which classifies it
165/// [`Ipv6Fragment::Unknown`] so it is dropped.
166fn decode6_fragment(b: &[u8]) -> Ipv6Fragment {
167 // Go `q.length = BE16(b[4:6]) + ip6HeaderLength; if len(b) < q.length` — a packet cut off before
168 // its declared payload is `unknown`.
169 if b.len() < IP6_HEADER_LEN {
170 return Ipv6Fragment::Unknown;
171 }
172 let length = usize::from(u16::from_be_bytes([b[4], b[5]])) + IP6_HEADER_LEN;
173 if b.len() < length {
174 return Ipv6Fragment::Unknown;
175 }
176
177 // Go `if len(b) < q.subofs+ip6FragHeaderLength` with `q.subofs == 40`.
178 let Some(frag) = b.get(IP6_HEADER_LEN..) else {
179 return Ipv6Fragment::Unknown;
180 };
181 if frag.len() < IP6_FRAG_HEADER_LEN {
182 return Ipv6Fragment::Unknown;
183 }
184
185 let next_header = frag[0];
186 // Go `fragOfs := binary.BigEndian.Uint16(frag[2:4]) >> 3`: the top 13 bits are the offset in
187 // 8-byte blocks; the low 3 are two reserved bits and the More-Fragments flag. Go reads no MF
188 // flag here at all — unlike `decode4`, `decode6` has no more-fragments guard on the first
189 // fragment, so a first IPv6 fragment is decoded exactly like an unfragmented packet (TSMP
190 // included, where `decode4` instead demotes a fragmented first packet to `unknown`).
191 let frag_ofs = u16::from_be_bytes([frag[2], frag[3]]) >> 3;
192
193 // Go steps `q.subofs += ip6FragHeaderLength` before branching; `sub` is what follows.
194 let sub = &frag[IP6_FRAG_HEADER_LEN..];
195
196 if frag_ofs == 0 {
197 return decode6_first_fragment(IpProto::new(i64::from(next_header)), sub);
198 }
199 if frag_ofs < MIN_FRAG_BLKS {
200 // RFC 1858: this fragment's bytes could land on top of the transport header the ACL matched
201 // the head fragment on. Go `q.IPProto = unknown`, same guard as `decode4`.
202 return Ipv6Fragment::Unknown;
203 }
204 Ipv6Fragment::Later
205}
206
207/// The sub-protocol switch Go `decode6` runs on a first fragment once `decode6Fragment` has stepped
208/// over the Fragment header. `sub` is the buffer from the sub-protocol's header onwards (Go's
209/// `sub := b[q.subofs:]`, measured against the buffer, not the IPv6 length field).
210///
211/// Each arm's bounds check is Go's, and each failure is Go's `unknown`: a first fragment too short
212/// to hold the transport header must be **dropped**, never guessed at, or a follow-up fragment
213/// supplying the rest of that header would carry the flow past a rule the filter never really
214/// matched (RFC 1858, the same reason `decode4` rejects a short first fragment).
215fn decode6_first_fragment(proto: IpProto, sub: &[u8]) -> Ipv6Fragment {
216 /// Go `net/packet.icmp6HeaderLength`.
217 const ICMP6_HEADER_LEN: usize = 4;
218 /// Go `net/packet.tcpHeaderLength`.
219 const TCP_HEADER_LEN: usize = 20;
220 /// Go `net/packet.udpHeaderLength`.
221 const UDP_HEADER_LEN: usize = 8;
222 /// Go `net/packet.minTSMPSize` — the shortest TSMP body (a 7-byte rejected-connection message).
223 const MIN_TSMP_SIZE: usize = 7;
224
225 // Go's port-ful arms: bounds-check, then read the source port from `sub[0:2]` and the
226 // destination port from `sub[2:4]`. `l4` is whatever else Go reads out of that same header —
227 // only the TCP arm reads anything (`q.TCPFlags = TCPFlag(sub[13])`).
228 let ported = |min_len: usize, l4: ts_packetfilter::L4Header| {
229 if sub.len() < min_len {
230 return Ipv6Fragment::Unknown;
231 }
232 Ipv6Fragment::First {
233 proto,
234 src_port: u16::from_be_bytes([sub[0], sub[1]]),
235 dst_port: u16::from_be_bytes([sub[2], sub[3]]),
236 l4,
237 }
238 };
239 // Go's portless arms: bounds-check only, both ports left at 0.
240 let portless = |min_len: usize, l4: ts_packetfilter::L4Header| {
241 if sub.len() < min_len {
242 return Ipv6Fragment::Unknown;
243 }
244 Ipv6Fragment::First {
245 proto,
246 src_port: 0,
247 dst_port: 0,
248 l4,
249 }
250 };
251 // Go's `IsEchoResponse`/`IsError` read `q.b[q.subofs]` and `q.b[q.subofs+1]` behind
252 // `len(q.b) >= q.subofs+8` — eight bytes, not the four `decode6`'s ICMPv6 arm bounds-checks.
253 // A shorter first fragment is decoded (and matched IPs-only) but is no "response" to upstream
254 // either, so it carries no ICMP header here.
255 let icmp6 = || match (sub.first(), sub.get(1)) {
256 (Some(&icmp_type), Some(&icmp_code)) if sub.len() >= 8 => ts_packetfilter::L4Header::Icmp {
257 icmp_type,
258 icmp_code,
259 },
260 _ => ts_packetfilter::L4Header::Unknown,
261 };
262 // Go `q.TCPFlags = TCPFlag(sub[13])`, guarded by the same 20-byte bounds check `ported` runs.
263 let tcp_flags = || match sub.get(13) {
264 Some(&flags) => ts_packetfilter::L4Header::Tcp { flags },
265 None => ts_packetfilter::L4Header::Unknown,
266 };
267
268 match proto {
269 IpProto::ICMPV6 => portless(ICMP6_HEADER_LEN, icmp6()),
270 IpProto::TCP => ported(TCP_HEADER_LEN, tcp_flags()),
271 IpProto::UDP => ported(UDP_HEADER_LEN, ts_packetfilter::L4Header::Unknown),
272 IpProto::SCTP => ported(SCTP_HEADER_LEN, ts_packetfilter::L4Header::Unknown),
273 IpProto::TSMP => portless(MIN_TSMP_SIZE, ts_packetfilter::L4Header::Unknown),
274 IPPROTO_FRAGMENT_SENTINEL => Ipv6Fragment::Unknown,
275 // Go's switch has no default arm: any other protocol keeps its number and port 0, and the
276 // ACL matches it IPs-only (`IpProto::is_port_ful`).
277 //
278 // Protocol 0 is carried here like any other, which is Go's `q.IPProto = nextHdr` followed
279 // by a switch with no case for it. It is not an admission: 0 is `ipproto.Unknown`, so the
280 // packet dies on `inbound_filter_verdict`'s [`IPPROTO_UNKNOWN`] arm before a rule sees it,
281 // exactly where Go's `pre()` kills it. Pinned by
282 // `first_ipv6_fragment_with_unknown_next_header_is_dropped_before_the_acl`.
283 _ => Ipv6Fragment::First {
284 proto,
285 src_port: 0,
286 dst_port: 0,
287 l4: ts_packetfilter::L4Header::Unknown,
288 },
289 }
290}
291
292/// The transport-header fields Go's `decode4` reads out of a **first** IPv4 fragment — the
293/// `(source port, destination port, L4 header)` of the sub-protocol switch it runs under
294/// `if fragOfs == 0`, from the same bytes Go calls `sub`.
295///
296/// `None` is Go's `q.IPProto = unknown` for a fragment too short to hold the whole transport header
297/// it claims to carry, which filter `pre()` drops before any rule is consulted. Upstream spells out
298/// why every arm bounds-checks:
299///
300/// ```text
301/// // This is the first fragment
302/// // Every protocol below MUST check that it has at least one entire
303/// // transport header in order to protect against fragment confusion.
304/// ```
305///
306/// Falling back to port 0 instead would be the fragment-confusion bug itself: a first fragment cut
307/// off before its ports would be matched (and possibly admitted) on a port it never carried, with
308/// the follow-up fragments supplying the real header after the verdict was taken.
309///
310/// This exists for the same reason [`sctp_ports`] does — etherparse cannot supply the header. It
311/// deliberately refuses to descend into a *fragmenting* payload, so `SlicedPacket::transport` is
312/// empty for a first fragment even though the fragment does carry the full header, where Go's
313/// `decode4` parses it exactly as it parses an unfragmented packet's.
314///
315/// Only the protocols whose header `decode4` reads are listed. Everything else keeps its protocol
316/// number and both ports 0 — Go's switch has no `default` arm — which is what
317/// [`IpProto::is_port_ful`] already gives such a packet on the unfragmented path. Go's `ipproto.IGMP`
318/// arm is the one omission: it bounds-checks the IGMP header and reads nothing out of it, and IGMP is
319/// addressed to a multicast group, so [`drop_before_rules`] has already refused any packet that arm
320/// could speak about. `ipproto.TSMP`'s `if moreFrags` refusal lives in [`inbound_filter_verdict`],
321/// which is where this fork already carried it.
322fn decode4_first_fragment(
323 proto: IpProto,
324 sub: &[u8],
325) -> Option<(u16, u16, ts_packetfilter::L4Header)> {
326 /// Go `net/packet.icmp4HeaderLength`.
327 const ICMP4_HEADER_LEN: usize = 4;
328 /// Go `net/packet.tcpHeaderLength`.
329 const TCP_HEADER_LEN: usize = 20;
330 /// Go `net/packet.udpHeaderLength`.
331 const UDP_HEADER_LEN: usize = 8;
332
333 // Go's port-ful arms: bounds-check the whole header, then `sub[0:2]` and `sub[2:4]`.
334 let ported = |min_len: usize, l4: ts_packetfilter::L4Header| {
335 if sub.len() < min_len {
336 return None;
337 }
338 Some((
339 u16::from_be_bytes([sub[0], sub[1]]),
340 u16::from_be_bytes([sub[2], sub[3]]),
341 l4,
342 ))
343 };
344
345 match proto {
346 // Go `q.TCPFlags = TCPFlag(sub[13])`, behind the same 20-byte bounds check.
347 IpProto::TCP => ported(
348 TCP_HEADER_LEN,
349 match sub.get(13) {
350 Some(&flags) => ts_packetfilter::L4Header::Tcp { flags },
351 None => ts_packetfilter::L4Header::Unknown,
352 },
353 ),
354 IpProto::UDP => ported(UDP_HEADER_LEN, ts_packetfilter::L4Header::Unknown),
355 IpProto::SCTP => sctp_ports(sub).map(|(s, d)| (s, d, ts_packetfilter::L4Header::Unknown)),
356 // Go `case ipproto.ICMPv4`: bounds-check four bytes, then `withPort(…, 0)` on both. The
357 // type/code pair its `IsEchoResponse`/`IsError` read is guarded by a *wider* bound —
358 // `len(q.b) >= q.subofs+8` — so a first fragment between 4 and 7 bytes long is decoded and
359 // matched IPs-only, but is no "response" to upstream and carries no header here either.
360 IpProto::ICMP => {
361 if sub.len() < ICMP4_HEADER_LEN {
362 return None;
363 }
364 let l4 = match (sub.first(), sub.get(1)) {
365 (Some(&icmp_type), Some(&icmp_code)) if sub.len() >= 8 => {
366 ts_packetfilter::L4Header::Icmp {
367 icmp_type,
368 icmp_code,
369 }
370 }
371 _ => ts_packetfilter::L4Header::Unknown,
372 };
373 Some((0, 0, l4))
374 }
375 // Go's internal later-fragment sentinel, seen as a real protocol number on the wire: Go
376 // `case ipproto.Fragment: q.IPProto = unknown`, so this is a drop, not a port-0 pass.
377 IPPROTO_FRAGMENT_SENTINEL => None,
378 _ => Some((0, 0, ts_packetfilter::L4Header::Unknown)),
379 }
380}
381
382/// The `(source, destination)` ports of the SCTP packet whose common header starts at `sub` — Go's
383/// `case ipproto.SCTP` arm, which both `decode4` and `decode6` carry verbatim: bounds-check the
384/// 12-byte common header, then read `sub[0:2]` and `sub[2:4]`.
385///
386/// `None` is Go's refusal in that same arm (`q.IPProto = unknown`), which filter `pre()` turns into
387/// a drop. It must never be read as "port 0": a truncated SCTP header carries no port for a rule to
388/// match, and admitting it as port 0 would let an all-ports rule pass the packet Go throws away.
389///
390/// This exists because etherparse's `TransportSlice` has arms for ICMPv4/ICMPv6/TCP/UDP and nothing
391/// else, so an SCTP packet leaves `SlicedPacket::transport` empty and its ports have to be read the
392/// way Go reads them.
393fn sctp_ports(sub: &[u8]) -> Option<(u16, u16)> {
394 if sub.len() < SCTP_HEADER_LEN {
395 return None;
396 }
397 Some((
398 u16::from_be_bytes([sub[0], sub[1]]),
399 u16::from_be_bytes([sub[2], sub[3]]),
400 ))
401}
402
403/// Whether `ipv6` carries a Fragment extension header somewhere in its extension-header chain
404/// *other than* as the base header's immediate Next Header — the case [`decode6_fragment`] is
405/// deliberately not scoped to, and which must therefore fail closed here.
406///
407/// Callers must only ask this when the base header's Next Header is **not** [`IP6_FRAG_HEADER`];
408/// otherwise the leading Fragment header itself answers `true` and would shadow its own
409/// classification.
410///
411/// Why a drop and not a pass. Go's `decode6` steps over *only* a leading Fragment header, so a
412/// chained one is never classified at all: the packet is filtered as whatever extension header the
413/// base Next Header names, and its fragment offset is never read. Anything this tree said about
414/// such a packet would therefore be its own invention, so it says the one thing that cannot be an
415/// invention in the permissive direction — [`Ipv6Fragment::Unknown`], a drop.
416///
417/// This never admits what upstream refuses. Where the chain leads with Hop-by-Hop Options, Go's
418/// `q.IPProto` is 0 == `ipproto.Unknown` and `pre()` drops it too. Where it leads with Routing (43)
419/// or Destination Options (60), Go carries that number to `runIn6`'s `default` arm, so it can be
420/// admitted only by an all-ports rule that names protocol 43 or 60 IPs-only
421/// (`matchProtoAndIPsOnlyIfAllPorts`) — an ACL nobody writes by accident, and the sole case where
422/// this drop is stricter than upstream. Refusing it cannot break a real Tailscale, `wireguard-go`
423/// or kernel-WireGuard peer: none of them source-fragments behind a chained extension header, and
424/// no peer can be relying on delivery of a packet whose fragment offset upstream never looked at.
425fn fragment_header_is_chained(ipv6: ðerparse::Ipv6Slice<'_>) -> bool {
426 ipv6.extensions()
427 .clone()
428 .into_iter()
429 .any(|ext| matches!(ext, etherparse::Ipv6ExtensionSlice::Fragment(_)))
430}
431
432/// Which address family's fragment rules apply to a packet, so [`inbound_filter_verdict`] can run
433/// Go's `decode4` and `decode6` fragment classifications on the packets each actually governs.
434#[derive(Debug, Clone, Copy)]
435enum Fragment {
436 /// IPv4: the offset and MF flag straight out of the base header (Go `decode4`).
437 V4(Ipv4Fragment),
438 /// IPv6: the already-resolved classification of a Fragment extension header (Go `decode6`).
439 V6(Ipv6Fragment),
440}
441
442/// The inbound packet-filter verdict for an already-parsed packet (`true` = admit). This is the
443/// proto-switch of Go's filter `runIn4`/`runIn6`, applied after `pre()` and after this fork's
444/// source-attribution and local-destination routing (the analogues of Go's `local4`/`local6`
445/// precondition) have run:
446///
447/// 1. `drop_before_rules` — Go `pre()`'s unconditional multicast / link-local-unicast drops.
448/// 2. **Fragment classification** (Go `net/packet.decode4`/`decode6` + filter `pre()`): a non-first
449/// fragment carries no L4 header, so it cannot be port-matched. Go classifies it by offset — a
450/// fragment at offset `>= MIN_FRAG_BLKS` is mapped to `ipproto.Fragment` and `pre()` **accepts**
451/// it (stateless pass-through; the receiver's kernel discards it if the head fragment was
452/// dropped), while a fragment at a smaller offset is dropped (RFC 1858). On IPv4 a *fragmented*
453/// TSMP is additionally disallowed (`moreFrags` on a first TSMP fragment → drop). Without this,
454/// etherparse leaves the transport `None` and the port reads as 0, so a normal ACL rule would
455/// silently drop every valid later fragment — breaking large/fragmented inbound traffic on the
456/// 1280-MTU overlay. The IPv6 half ([`Ipv6Fragment`], Go `decode6Fragment`) additionally folds in
457/// the sub-protocol decode of a *first* fragment, so `proto`/`dst_port` here are already the ones
458/// read past the Fragment extension header, and `Ipv6Fragment::Unknown` — a truncated or
459/// short-first fragment, or one whose Fragment header sits behind a chained extension header
460/// ([`fragment_header_is_chained`]) — is dropped where Go's `pre()` drops `ipproto.Unknown`.
461/// A *first* IPv4 fragment is decoded the same way ([`decode4_first_fragment`], Go `decode4`'s
462/// `fragOfs == 0` switch), so `dst_port` and `l4` here are the fragmented datagram's own and it
463/// faces the rules — and the reply carve-outs below — on the terms an unfragmented one does.
464/// 3. **Unknown protocol** ([`IPPROTO_UNKNOWN`]) — Go `pre()`'s `if q.IPProto == ipproto.Unknown`
465/// drop. `proto` is whatever the *base* header declared (Go `decode4`'s `b[9]`, `decode6`'s
466/// `b[6]`), so this is the arm that refuses an IPv6 packet leading with Hop-by-Hop Options,
467/// which is literally protocol 0.
468/// 4. TSMP (proto 99) is always admitted, bypassing the ACL — Go `case ipproto.TSMP: return Accept`.
469/// TSMP carries in-band control messages between nodes, so it must reach the local stack
470/// regardless of the ACL rules. The two TSMP messages this fork understands never get here:
471/// [`filter_inbound_from_peer`] consumes them ahead of this call, as Go does.
472/// 5. **A UDP or SCTP reply to a flow this node started** is admitted from `flows` — Go's
473/// `case ipproto.UDP, ipproto.SCTP` arm, which consults the `flowtrack` LRU and returns
474/// `Accept, "cached"` *before* the rule match. See [`flowtrack`].
475/// 6. **A TCP segment that is not a SYN**, and **an ICMP echo response or ICMP error**, are
476/// admitted with no rule consulted — Go's `return Accept, "tcp non-syn"` and
477/// `return Accept, "icmp response ok"`, the other two thirds of the reply admission step 5
478/// starts. All three exist for the same reason: the answer to a connection *this node* opened
479/// arrives at an ephemeral port, so no ACL control can write will ever name it. Without them a
480/// policy that grants this node outbound access to a peer without granting that peer inbound
481/// access back hangs every TCP connection on its SYN-ACK and silently eats every ping reply.
482/// The predicates are [`ts_packetfilter::PacketInfo`]'s, and each fails closed when the L4
483/// header was not decoded, so a **SYN** — or any packet whose flags this fork never read —
484/// still faces the rules.
485/// 7. Everything else consults the control-derived ACL via `can_access` — Go's `matches4.match`.
486/// A protocol Go's `runIn4`/`runIn6` switch has no arm for (an IPv6 Routing or
487/// Destination-Options header, say) lands in its `default`, which admits IPs-only and only
488/// under an all-ports rule naming that protocol (`matchProtoAndIPsOnlyIfAllPorts`); that
489/// per-protocol port semantics lives in [`ts_packetfilter::Rule`]. ICMP that is *not* a
490/// response reaches the same call and is matched IPs-only there, which is Go's
491/// `else if f.matches4.matchIPsOnly(q, …)`.
492///
493/// `src` and `dst` carry ports because Go's `packet.Parsed` does (`q.Src`/`q.Dst` are
494/// `netip.AddrPort`) and because the flow cache in step 5 keys on all four fields. The ACL itself
495/// still sees only the destination port, which is all Go's `matches4.match` reads. `l4` is the rest
496/// of what Go's `packet.Parsed` holds — `TCPFlags` and the ICMP type/code — and only step 6 reads
497/// it.
498fn inbound_filter_verdict(
499 filter: &(dyn ts_packetfilter::Filter + Send + Sync),
500 flows: &mut flowtrack::FlowCache,
501 proto: IpProto,
502 src: std::net::SocketAddr,
503 dst: std::net::SocketAddr,
504 l4: ts_packetfilter::L4Header,
505 frag: Option<Fragment>,
506) -> bool {
507 if drop_before_rules(dst.ip()) {
508 tracing::trace!(?dst, "dropping multicast/link-local dst (pre-rule)");
509 return false;
510 }
511
512 match frag {
513 Some(Fragment::V4(frag)) => {
514 if frag.offset_blocks > 0 {
515 // A non-first fragment (Go `decode4`'s `fragOfs != 0` branch). It has no transport
516 // header to match, so the verdict is decided purely by offset:
517 if frag.offset_blocks < MIN_FRAG_BLKS {
518 // Potentially overlaps a transport header (RFC 1858); Go demotes to `unknown` → drop.
519 tracing::trace!(?dst, "dropping low-offset IPv4 fragment (RFC 1858)");
520 return false;
521 }
522 // A valid later fragment — Go maps it to `ipproto.Fragment`, which `pre()` accepts
523 // ahead of the ACL. Stateless: if the head fragment was filtered the receiver's kernel
524 // drops this on reassembly timeout. Accepting here is what large fragmented inbound
525 // traffic relies on.
526 tracing::trace!(
527 ?dst,
528 "accepting later IPv4 fragment (Go pre() pass-through)"
529 );
530 return true;
531 }
532 // `frag.offset_blocks == 0`: the first fragment (or an unfragmented packet). Go disallows a
533 // *fragmented* TSMP (a first fragment with MF set) — without the whole message it can't be a
534 // valid inter-node control packet. Fall through to the normal proto-switch for everything
535 // else; the first fragment of TCP/UDP carries its L4 header, so `dst_port` was parsed above.
536 if proto == IpProto::TSMP && frag.more_fragments {
537 tracing::trace!(?dst, "dropping fragmented TSMP (Go parity)");
538 return false;
539 }
540 }
541 // The IPv6 Fragment extension header (Go `decode6Fragment`, upstream `4c4ec3d46`). Only
542 // reachable on the opt-in `Config::enable_ipv6` path; the classification itself already ran
543 // Go's offset and bounds checks, so all that is left is Go's `pre()` disposition of the
544 // three protocol values `decode6` can end up with.
545 Some(Fragment::V6(Ipv6Fragment::Unknown)) => {
546 // Go `pre()`: `if q.IPProto == ipproto.Unknown { return Drop }`. This is the
547 // security-relevant arm — a short first fragment, an RFC 1858 low-offset later
548 // fragment, or a Fragment header hidden behind a chained extension header must never
549 // reach the ACL, where an allow-all rule would admit it.
550 tracing::trace!(
551 ?dst,
552 "dropping IPv6 fragment classified unknown (Go pre() drop)"
553 );
554 return false;
555 }
556 Some(Fragment::V6(Ipv6Fragment::Later)) => {
557 // Go `pre()`: `case ipproto.Fragment: return Accept`, same stateless pass-through as
558 // IPv4 — and required by RFC 8200 §4.5, which puts reassembly on the receiver.
559 tracing::trace!(
560 ?dst,
561 "accepting later IPv6 fragment (Go pre() pass-through)"
562 );
563 return true;
564 }
565 // A first IPv6 fragment: `proto` and `dst_port` were read past the Fragment header, so it
566 // takes the ordinary proto switch below and matches the rule an unfragmented datagram would.
567 // Note the deliberate asymmetry with IPv4: `decode6` has no more-fragments guard at all, so
568 // — unlike `decode4` — upstream does not demote a fragmented first TSMP packet to `unknown`.
569 // Falling through is also what refuses a first fragment whose Fragment header names
570 // protocol 0: it arrives here as `proto == IPPROTO_UNKNOWN` and the shared arm below drops
571 // it pre-rules, which is the same fall-through Go gets from a switch with no case for 0.
572 Some(Fragment::V6(Ipv6Fragment::First { .. })) | None => {}
573 }
574
575 // Go filter `pre()`: `if q.IPProto == ipproto.Unknown { return Drop }`. A protocol number
576 // upstream's decoder refused to classify never reaches the ACL, so no rule — however
577 // permissive — can admit it. The check sits after the fragment arms above rather than at the
578 // top of the function only because those arms use `IPPROTO_UNKNOWN` as their own "no
579 // sub-protocol here" placeholder; in Go the two are distinct values (`ipproto.Fragment` is
580 // 0xff) and `pre()` tests them in either order to the same effect.
581 //
582 // The common way to land here is an IPv6 packet whose base Next Header is Hop-by-Hop Options,
583 // which *is* protocol 0: `decode6` copies it into `q.IPProto` and never looks past it.
584 if proto == IPPROTO_UNKNOWN {
585 tracing::trace!(?dst, "dropping unknown-proto packet (Go pre() drop)");
586 return false;
587 }
588
589 if proto == IpProto::TSMP {
590 tracing::trace!(?dst, "accepting TSMP inbound (bypasses ACL, Go parity)");
591 return true;
592 }
593
594 // Go `runIn4`/`runIn6`, at the top of the UDP/SCTP arm and ahead of the rule match:
595 //
596 // case ipproto.UDP, ipproto.SCTP:
597 // t := flowtrack.MakeTuple(q.IPProto, q.Src, q.Dst)
598 // f.state.mu.Lock()
599 // _, ok := f.state.lru.Get(t)
600 // f.state.mu.Unlock()
601 // if ok {
602 // return Accept, "cached"
603 // }
604 //
605 // This is the reply to a datagram `process_outbound` sent, so no ACL rule can be expected to
606 // name it: our source port was ephemeral. A miss falls straight through to the rule match
607 // below, which is exactly what Go does — the cache only ever admits, it never denies.
608 if flows.admits_inbound(proto, src, dst) {
609 tracing::trace!(
610 ?src,
611 ?dst,
612 "accepting reply to a tracked outbound flow (cached)"
613 );
614 return true;
615 }
616
617 let info = ts_packetfilter::PacketInfo {
618 ip_proto: proto,
619 port: dst.port(),
620 src: src.ip(),
621 dst: dst.ip(),
622 l4,
623 };
624
625 // Go `runIn4`/`runIn6`, `case ipproto.TCP`, ahead of the rule match:
626 //
627 // // For TCP, we want to allow *outgoing* connections, which means we want to allow
628 // // return packets on those connections. To make this restriction work, we need to
629 // // allow non-SYN packets (continuation of an existing session) to arrive. This
630 // // should be okay since a new incoming session can't be initiated without first
631 // // sending a SYN.
632 // if !q.IsTCPSyn() {
633 // return Accept, "tcp non-syn"
634 // }
635 //
636 // The SYN-ACK answering a connection `process_outbound` opened is a non-SYN, and it lands on
637 // our ephemeral source port, so no rule from control can name it. A SYN is the one segment this
638 // must never admit: it is the only way to *start* an inbound session, and admitting it would
639 // turn the carve-out into an open door. See `PacketInfo::is_tcp_non_syn`, which demands a
640 // decoded flags byte rather than treating "no flags" as "not a SYN".
641 if info.is_tcp_non_syn() {
642 tracing::trace!(
643 ?src,
644 ?dst,
645 "accepting inbound TCP non-SYN (Go 'tcp non-syn')"
646 );
647 return true;
648 }
649
650 // Go `runIn4`'s `case ipproto.ICMPv4` and `runIn6`'s `case ipproto.ICMPv6`, ahead of the same
651 // rule match:
652 //
653 // if q.IsEchoResponse() || q.IsError() {
654 // // ICMP responses are allowed.
655 // return Accept, "icmp response ok"
656 // } else if f.matches4.matchIPsOnly(q, f.srcIPHasCap) {
657 // // If any port is open to an IP, allow ICMP to it.
658 // return Accept, "icmp ok"
659 // }
660 //
661 // The `else if` is already in place: `ts_packetfilter::Rule` matches ICMP IPs-only, so an echo
662 // *request* — and anything else that is not a response — falls through to `can_access` below
663 // and is admitted only if a rule opens some port to this destination. Only the unconditional
664 // arm is added here.
665 if info.is_icmp_echo_response() || info.is_icmp_error() {
666 tracing::trace!(
667 ?src,
668 ?dst,
669 "accepting inbound ICMP response (Go 'icmp response ok')"
670 );
671 return true;
672 }
673
674 // TODO(npry): wire in nodecaps
675 let caps = [];
676 let verdict = filter.can_access(&info, caps);
677 tracing::trace!(?info, ?caps, verdict);
678 verdict
679}
680
681/// The knobs [`filter_inbound_from_peer`] reads that come from this node's configuration rather
682/// than from the packet in front of it — everything Go's `tstun.Wrapper` holds on itself for the
683/// TSMP rejected-connection reply.
684#[derive(Debug, Clone, Copy, Default)]
685struct RejectConfig {
686 /// Go `tstun.Wrapper.disableTSMPRejected`: when set, this node tells a peer nothing about an
687 /// ACL drop, exactly as before this existed.
688 disabled: bool,
689 /// Go `tstun.Wrapper.PeerAPIPort`: the TCP port this node's peerAPI listens on, if it runs one.
690 /// A SYN to it never produces a reject — see [`tsmp_reject_for_drop`].
691 peerapi_port: Option<u16>,
692}
693
694/// Everything a filtered inbound batch produces besides the packets it keeps.
695///
696/// Grouped into one out-parameter because all three are appended to across a whole
697/// [`DataPlane::process_inbound_from`] call, never per packet, and because every one of them is a
698/// TSMP message either consumed or synthesized by the filter step.
699#[derive(Debug, Default)]
700struct InboundHarvest {
701 /// Disco keys peers advertised (Go `packet.TSMPDiscoKeyAdvertisement`). See
702 /// [`InboundResult::learned_disco_keys`].
703 learned_disco_keys: Vec<(PeerId, ts_packet::tsmp::DiscoKeyAdvertisement)>,
704 /// Rejected-connection messages peers sent us. See [`InboundResult::rejected_flows`].
705 rejected_flows: Vec<(PeerId, ts_packet::tsmp::TailscaleRejectedHeader)>,
706 /// Rejected-connection messages *this* node is sending back, already marshalled into complete
707 /// IP packets, each addressed to the peer whose packet was dropped. The caller encrypts and
708 /// queues them (Go `tstun.Wrapper.InjectOutbound`).
709 rejects_to_send: Vec<(PeerId, PacketMut)>,
710}
711
712/// The TSMP rejected-connection message to send back for an inbound packet the filter just
713/// dropped, marshalled into a complete IP packet — or `None` when this drop must stay silent.
714///
715/// Go `tstun.Wrapper.filterPacketInboundFromWireGuard`, the block that runs on `outcome !=
716/// filter.Accept`:
717///
718/// ```text
719/// // Tell them, via TSMP, we're dropping them due to the ACL.
720/// // Their host networking stack can translate this into ICMP
721/// // or whatnot as required. But notably, their GUI or tailscale CLI
722/// // can show them a rejection history with reasons.
723/// if p.IPVersion == 4 && p.IPProto == ipproto.TCP && p.TCPFlags&packet.TCPSyn != 0 && !t.disableTSMPRejected {
724/// ```
725///
726/// Every clause of that guard is a refusal that has to come with the message, because each one is a
727/// packet upstream stays **silent** about:
728///
729/// 1. **`disableTSMPRejected`** ([`RejectConfig::disabled`]) — the node-wide off switch.
730/// 2. **IPv4 only.** An IPv6 packet dropped by the ACL produces nothing. Upstream's guard is
731/// `p.IPVersion == 4` and the message itself is family-agnostic, so this is a deliberate
732/// upstream scoping and not a limitation of the encoding.
733/// 3. **TCP only, and only a segment with the SYN bit set.** A dropped UDP datagram, a dropped
734/// non-SYN segment and a dropped ICMP packet are all silent: the message names a *connection*
735/// being refused, and only a SYN is a connection being opened. See
736/// [`ts_packetfilter::L4Header::tcp_syn_flag_set`] for why this is not `is_tcp_non_syn`'s
737/// negation.
738/// 4. **Never for the peerAPI port.** Go's carve-out immediately above — "Let peerapi through the
739/// filter; its ACLs are handled at L7, not at the packet level" — flips an ACL-refused peerAPI
740/// SYN back to `Accept` *before* this block runs, so upstream never emits a reject for one. This
741/// fork has no such carve-out (its peerAPI is reachable only through the ordinary rules), so the
742/// same outcome is reached by suppressing the message here rather than by admitting the packet:
743/// the verdict on the packet is untouched, and, as upstream, the peer is told nothing.
744///
745/// The reason is [`RejectReason::SHIELDS_UP`] when the live filter has shields up (Go
746/// `if t.filter.ShieldsUp() { rj.Reason = packet.RejectedDueToShieldsUp }`) and
747/// [`RejectReason::ACLS`] otherwise. `maybe_broken` stays false: both reasons are terminal
748/// verdicts about this node's own policy, and Go sets neither of the non-terminal reasons here.
749///
750/// [`RejectReason::SHIELDS_UP`]: ts_packet::tsmp::RejectReason::SHIELDS_UP
751/// [`RejectReason::ACLS`]: ts_packet::tsmp::RejectReason::ACLS
752fn tsmp_reject_for_drop(
753 proto: IpProto,
754 src: std::net::SocketAddr,
755 dst: std::net::SocketAddr,
756 l4: ts_packetfilter::L4Header,
757 shields_up: bool,
758 cfg: RejectConfig,
759) -> Option<Vec<u8>> {
760 if cfg.disabled {
761 return None;
762 }
763 // Go `p.IPVersion == 4`. `src` and `dst` come from one IP header, so testing either is testing
764 // the header; both are spelled out because the marshal below refuses a mixed pair anyway.
765 if !src.is_ipv4() || !dst.is_ipv4() {
766 return None;
767 }
768 if proto != IpProto::TCP || !l4.tcp_syn_flag_set() {
769 return None;
770 }
771 if cfg.peerapi_port == Some(dst.port()) {
772 tracing::trace!(?dst, "not rejecting a peerAPI SYN (Go's peerapi carve-out)");
773 return None;
774 }
775
776 let reason = if shields_up {
777 ts_packet::tsmp::RejectReason::SHIELDS_UP
778 } else {
779 ts_packet::tsmp::RejectReason::ACLS
780 };
781
782 // Go: `IPSrc: p.Dst.Addr(), IPDst: p.Src.Addr(), Src: p.Src, Dst: p.Dst`. The IP pair is
783 // reversed — the reject travels back the way the packet came — while the four-tuple keeps the
784 // *rejected connection's* own direction, so the dialer can match it against the flow it opened.
785 ts_packet::tsmp::TailscaleRejectedHeader {
786 ip_src: dst.ip(),
787 ip_dst: src.ip(),
788 src,
789 dst,
790 // Go `Proto: p.IPProto`, which the guard above has already pinned to TCP.
791 proto: IPPROTO_TCP_BYTE,
792 reason,
793 maybe_broken: false,
794 }
795 .marshal()
796 .inspect_err(|e| tracing::debug!(?dst, error = %e, "not sending a TSMP reject"))
797 .ok()
798}
799
800/// Apply the inbound packet filter to one peer's already-source-attributed batch of decrypted
801/// packets, in place, and harvest any TSMP disco-key advertisements it carried.
802///
803/// This is the body of Go's `tstun.Wrapper.filterPacketInboundFromWireGuard`, in Go's order:
804///
805/// 1. **TSMP consumption.** Go inspects TSMP *before* running the ACL filter and returns
806/// `filter.DropSilently` for the messages it consumes itself. The one consumed here is the
807/// disco-key advertisement (Go `packet.TSMPDiscoKeyAdvertisement`, upstream capability version
808/// 144): a peer announces its disco public key right after an eligible WireGuard session comes
809/// up, so the receiver learns it without waiting for a netmap update or restarting WireGuard.
810/// A real Go peer sends this unprompted. The other message consumed here is the
811/// **rejected-connection** message (Go `packet.TailscaleRejectedHeader`), which Go consumes in
812/// `wgengine.userspaceEngine.trackOpenPreFilterIn` — also ahead of the ACL, also with
813/// `filter.DropSilently`. Every *remaining* TSMP message (ping, pong, a type this fork does not
814/// know) is left in the batch and falls through to step 2, which admits it — exactly as Go's
815/// filter does for the TSMP types it does not consume.
816/// 2. **The ACL verdict**, [`inbound_filter_verdict`] (Go `runIn4`/`runIn6`).
817/// 3. **The rejected-connection reply**, on a drop: Go answers a dropped IPv4 TCP SYN by injecting
818/// a `packet.TailscaleRejectedHeader` back to the peer, so the far side learns *why* its dial
819/// failed instead of waiting out a TCP timeout. See [`tsmp_reject_for_drop`], which carries the
820/// whole of upstream's guard — including the drops that must stay silent.
821///
822/// `harvest` is appended to, never cleared, so one batch can carry messages from several peers. A
823/// learned key is attributed to `peer_id` — the WireGuard peer whose session
824/// decrypted the packet, and whose source addresses the caller's source filter has already bound.
825/// Go reaches the same peer the long way round, looking the advertisement's source IP up in the
826/// netmap (`wgengine.userspaceEngine.peerForIP`). Either way a peer can only advertise a key for
827/// *itself*: it cannot speak for another peer.
828fn filter_inbound_from_peer(
829 filter: &(dyn ts_packetfilter::Filter + Send + Sync),
830 flows: &mut flowtrack::FlowCache,
831 peer_id: PeerId,
832 packets: &mut Vec<PacketMut>,
833 reject_cfg: RejectConfig,
834 harvest: &mut InboundHarvest,
835) {
836 packets.retain(|packet| {
837 let bytes = packet.as_ref();
838 let Ok(pkt) = etherparse::SlicedPacket::from_ip(bytes) else {
839 tracing::trace!("does not look like ip packet");
840 return false;
841 };
842
843 // Go's `sub` in `decode4`/`decode6`: the packet from the sub-protocol's header onwards
844 // (`b[q.subofs:]`, the bytes after the IPv4 header or after the IPv6 base header and any
845 // extension headers). Taken here because the classification below consumes `pkt.net`; only
846 // the SCTP arm of `dst_port` reads it, for the ports etherparse does not parse itself.
847 let sub = match &pkt.net {
848 Some(etherparse::NetSlice::Ipv4(ipv4)) => ipv4.payload().payload,
849 Some(etherparse::NetSlice::Ipv6(ipv6)) => ipv6.payload().payload,
850 _ => &[][..],
851 };
852
853 let (proto, src, dst, frag) = match pkt.net {
854 Some(etherparse::NetSlice::Ipv4(ipv4)) => {
855 // IPv4 fragment state (Go `net/packet.decode4` reads `b[6:8]`): a
856 // non-first fragment carries no L4 header, so etherparse leaves
857 // `transport == None` and the port would read as 0 below — which a normal
858 // ACL rule never admits. Without classifying the fragment that silently
859 // drops valid later fragments Go *accepts* (breaking large/fragmented
860 // inbound traffic on the 1280-MTU overlay). Capture the offset (in 8-byte
861 // blocks) + the more-fragments bit so the verdict can mirror Go's
862 // `decode4`/`pre()` fragment handling.
863 let hdr = ipv4.header();
864 (
865 IpProto::new(ipv4.payload().ip_number.0 as _),
866 hdr.source_addr().into(),
867 hdr.destination_addr().into(),
868 Some(Fragment::V4(Ipv4Fragment {
869 offset_blocks: hdr.fragments_offset().value(),
870 more_fragments: hdr.more_fragments(),
871 })),
872 )
873 }
874 Some(etherparse::NetSlice::Ipv6(ipv6)) => {
875 let hdr = ipv6.header();
876 // Go `decode6` reads the protocol out of the base header and only the base
877 // header (`q.IPProto = ipproto.Proto(b[6])`). `next_header()` is that byte.
878 // Its one remapping is `decode6`'s switch arm `case ipproto.Fragment:
879 // q.IPProto = unknown` — Go's internal later-fragment sentinel has no business
880 // being on the wire, and `decode6_first_fragment` already refuses it in the
881 // other place it can appear.
882 let base_proto = match IpProto::new(i64::from(hdr.next_header().0)) {
883 IPPROTO_FRAGMENT_SENTINEL => IPPROTO_UNKNOWN,
884 other => other,
885 };
886 // IPv6 fragmentation is carried in a Fragment extension header, not the
887 // base header. Go `decode6` parses that header — and *only* when it is the
888 // base header's immediate Next Header. `next_header()` is exactly that
889 // immediate byte, so testing it here reproduces upstream's scoping. Only
890 // reachable under the opt-in `Config::enable_ipv6`; the tailnet is IPv4-only
891 // by default.
892 //
893 // A Fragment header reached through a *chained* hop-by-hop / routing /
894 // destination-options / AH header is outside that scope, and fails closed
895 // rather than falling through to the ACL as a fragment Go never classified.
896 // See `fragment_header_is_chained`.
897 let frag = if hdr.next_header().0 == IP6_FRAG_HEADER {
898 Some(decode6_fragment(bytes))
899 } else if fragment_header_is_chained(&ipv6) {
900 Some(Ipv6Fragment::Unknown)
901 } else {
902 None
903 };
904 let proto = match frag {
905 // Go `q.IPProto = nextHdr`: the first fragment's real sub-protocol, read
906 // past the 8-byte Fragment header.
907 Some(Ipv6Fragment::First { proto, .. }) => proto,
908 // A later or malformed fragment has no sub-protocol at all (Go's
909 // `ipproto.Fragment` / `unknown`); the verdict decides on the
910 // classification alone and never consults this.
911 Some(Ipv6Fragment::Later | Ipv6Fragment::Unknown) => IPPROTO_UNKNOWN,
912 // Go `decode6`: `q.IPProto = ipproto.Proto(b[6])` — the **base** header's
913 // Next Header byte, and nothing after that line resolves it any further.
914 // `decode6` steps over exactly one header, the leading Fragment header
915 // handled above; every other extension header is left unparsed, so the
916 // protocol Go matches on is the extension header's own number. Reading
917 // `ipv6.payload().ip_number` instead would take etherparse's walk *through*
918 // the whole chain to the real transport number, which is a different packet
919 // than the one upstream filters: a chain that leads with Hop-by-Hop (0) is
920 // `ipproto.Unknown` and `pre()` drops it, and one that leads with Routing
921 // (43) or Destination Options (60) reaches the ACL as protocol 43/60 —
922 // never matched against a TCP or UDP rule, and admitted only by an
923 // all-ports rule naming that protocol (Go `matchProtoAndIPsOnlyIfAllPorts`).
924 None => base_proto,
925 };
926 (
927 proto,
928 hdr.source_addr().into(),
929 hdr.destination_addr().into(),
930 frag.map(Fragment::V6),
931 )
932 }
933 _ => {
934 // A packet that parsed as IP but is neither IPv4 nor IPv6 (e.g. a
935 // future/odd `NetSlice` shape). These bytes are attacker-controlled
936 // post-decrypt, so fail closed — drop it — rather than `unreachable!`,
937 // which would panic the single-threaded dataplane on a crafted packet.
938 // Go's filter `pre()` likewise returns Drop/"not-ip" here, never panics.
939 tracing::trace!("parsed packet is neither IPv4 nor IPv6; dropping");
940 return false;
941 }
942 };
943
944 // Go `decode4`, `if fragOfs == 0`: a *first* IPv4 fragment carries the whole transport
945 // header, and Go parses it exactly as it parses an unfragmented packet's — ports, TCP flags
946 // and all. etherparse refuses to descend into a fragmenting payload and leaves
947 // `pkt.transport` empty, so those bytes have to be read out of `sub` the way Go reads them.
948 //
949 // Without this a fragmented inbound datagram reaches the ACL as port 0 and misses the
950 // reverse-flow cache, while its *later* fragments — accepted on their offset alone, as Go
951 // accepts them — arrive and can never be reassembled. `process_outbound` already reads a
952 // first fragment's ports this way (see `outbound_udp_or_sctp_flow`), so the flow this node
953 // opened was recorded and only the reply's head fragment was thrown away.
954 //
955 // Scoped to a fragmented first fragment (MF set, offset 0), which is exactly the shape
956 // etherparse withholds the transport header for; an unfragmented packet keeps the
957 // etherparse-parsed header it has always had.
958 let v4_first_frag = match frag {
959 Some(Fragment::V4(v4)) if v4.more_fragments && v4.offset_blocks == 0 => {
960 let Some(decoded) = decode4_first_fragment(proto, sub) else {
961 // Go's `q.IPProto = unknown`, dropped by `pre()` before any rule is consulted:
962 // a first fragment too short to hold the transport header it claims, or one
963 // carrying Go's internal later-fragment sentinel as its protocol number.
964 tracing::trace!(
965 ?dst,
966 "dropping first IPv4 fragment Go decodes as unknown (fragment confusion)"
967 );
968 return false;
969 };
970 Some(decoded)
971 }
972 _ => None,
973 };
974
975 // The rest of what Go's `packet.Parsed` holds about the L4 header — `q.TCPFlags`, and the
976 // ICMP type/code its `IsEchoResponse`/`IsError` read — for the reply carve-outs
977 // `inbound_filter_verdict` applies ahead of the rules. Taken before the port match below,
978 // which consumes `pkt.transport`.
979 //
980 // Every arm is guarded on `proto`, the number the *base* IP header declared. That is what
981 // Go's `runIn4`/`runIn6` switch on, and it is load-bearing here: etherparse walks an IPv6
982 // extension-header chain through to the real transport, so a packet leading with a Routing
983 // (43) or Destination Options (60) header can hand us a `TransportSlice::Tcp` for a
984 // protocol upstream never treats as TCP. Without the guard, burying a non-SYN segment under
985 // an extension header would be enough to skip the rules entirely.
986 let l4 = match frag {
987 // Go `decode6` parses a first fragment's real sub-protocol header past the Fragment
988 // extension header, TCP flags and ICMPv6 type included.
989 Some(Fragment::V6(Ipv6Fragment::First { l4, .. })) => l4,
990 // A later or malformed fragment has no L4 header to read at all, and the verdict
991 // decides on the classification alone.
992 Some(Fragment::V6(Ipv6Fragment::Later | Ipv6Fragment::Unknown)) => {
993 ts_packetfilter::L4Header::Unknown
994 }
995 // Any IPv4 packet, and any IPv6 packet with no Fragment header.
996 //
997 // A *first* IPv4 fragment has no transport slice — etherparse will not descend into a
998 // fragmenting payload — so its header comes from the Go-shaped decode above instead,
999 // and the reply carve-outs see the same TCP flags or ICMP type they would see on the
1000 // unfragmented datagram.
1001 Some(Fragment::V4(_)) | None => match v4_first_frag {
1002 Some((_, _, l4)) => l4,
1003 None => match &pkt.transport {
1004 // Go `decode4`/`decode6`: `q.TCPFlags = TCPFlag(sub[13])`. `TcpSlice::from_slice`
1005 // has already bounds-checked a 20-byte header, so the `None` is unreachable — but
1006 // it is spelled out rather than indexed, because these bytes are attacker
1007 // controlled post-decrypt and `L4Header::Unknown` fails closed where a panic would
1008 // take down the dataplane.
1009 Some(etherparse::TransportSlice::Tcp(tcp)) if proto == IpProto::TCP => {
1010 match tcp.header_slice().get(13) {
1011 Some(&flags) => ts_packetfilter::L4Header::Tcp { flags },
1012 None => ts_packetfilter::L4Header::Unknown,
1013 }
1014 }
1015 // Go's `IsEchoResponse`/`IsError` read `q.b[q.subofs]` and `q.b[q.subofs+1]` behind
1016 // `len(q.b) >= q.subofs+8`; etherparse's ICMP slices refuse anything shorter than
1017 // those same 8 bytes, so reaching this arm at all satisfies upstream's guard.
1018 Some(etherparse::TransportSlice::Icmpv4(icmp)) if proto == IpProto::ICMP => {
1019 ts_packetfilter::L4Header::Icmp {
1020 icmp_type: icmp.type_u8(),
1021 icmp_code: icmp.code_u8(),
1022 }
1023 }
1024 Some(etherparse::TransportSlice::Icmpv6(icmp)) if proto == IpProto::ICMPV6 => {
1025 ts_packetfilter::L4Header::Icmp {
1026 icmp_type: icmp.type_u8(),
1027 icmp_code: icmp.code_u8(),
1028 }
1029 }
1030 _ => ts_packetfilter::L4Header::Unknown,
1031 },
1032 },
1033 };
1034
1035 // Go `decode6` reads a *first* IPv6 fragment's transport ports past the Fragment
1036 // extension header, so a fragmented datagram matches the same rule as an
1037 // unfragmented one. etherparse deliberately refuses to descend into a fragmenting
1038 // payload and leaves `transport == None`, so that port comes from the
1039 // classification above instead.
1040 let (src_port, dst_port) = match frag {
1041 Some(Fragment::V6(Ipv6Fragment::First {
1042 src_port, dst_port, ..
1043 })) => (src_port, dst_port),
1044 // Go reads a destination port in exactly three arms of `decode4`/`decode6` — TCP,
1045 // UDP and SCTP — and which arm runs is decided by the protocol number the *base*
1046 // header declared, not by what a header walk can reach. So an IPv6 packet that
1047 // leads with an extension header takes the switch's `default` (in `decode6`, no
1048 // arm at all) and keeps port 0 even though a transport header does sit further
1049 // down its chain. Reading that buried port here is what let a chained packet be
1050 // matched against a port-scoped TCP/UDP rule it is not upstream's to match.
1051 _ if !proto.is_port_ful() => (0, 0),
1052 // A later IPv4 fragment carries no transport header at all: Go `decode4` leaves both
1053 // ports 0 and classifies it `ipproto.Fragment`, and the verdict below decides on the
1054 // offset alone. `sub` is continued payload here, not a header, so the SCTP arm must
1055 // not read it — that would invent a port, and would drop a short later fragment Go
1056 // passes through.
1057 Some(Fragment::V4(v4)) if v4.offset_blocks > 0 => (0, 0),
1058 // SCTP. etherparse's `TransportSlice` parses ICMPv4, ICMPv6, TCP and UDP and nothing
1059 // else, so `pkt.transport` is `None` for SCTP and the arm below would report port 0
1060 // for every SCTP packet on the wire — a match Go never makes. Go has an SCTP arm in
1061 // both `decode4` and `decode6` that reads `sub[2:4]`, so read it there too, from the
1062 // same bytes Go calls `sub`. (An IPv6 *first fragment* carrying SCTP is already
1063 // handled by the first arm, out of `decode6_first_fragment`'s own SCTP arm.)
1064 _ if proto == IpProto::SCTP => {
1065 let Some(ports) = sctp_ports(sub) else {
1066 // Go's `q.IPProto = unknown` for a header too short to hold the ports, which
1067 // `pre()` drops before any rule is consulted. Falling back to port 0 instead
1068 // would hand the packet to an all-ports SCTP rule.
1069 tracing::trace!(?dst, "dropping SCTP packet shorter than its own header");
1070 return false;
1071 };
1072 ports
1073 }
1074 // A *first* IPv4 fragment carrying TCP or UDP: Go's `fragOfs == 0` arms read its ports
1075 // like any other packet's, and etherparse cannot, because it hands over no transport
1076 // slice for a fragmenting payload. Without this the ACL sees port 0 and the
1077 // reverse-flow cache is asked about a flow nobody opened.
1078 _ => match (v4_first_frag, pkt.transport) {
1079 (Some((src_port, dst_port, _)), _) => (src_port, dst_port),
1080 (None, Some(etherparse::TransportSlice::Udp(udp))) => {
1081 (udp.source_port(), udp.destination_port())
1082 }
1083 (None, Some(etherparse::TransportSlice::Tcp(tcp))) => {
1084 (tcp.source_port(), tcp.destination_port())
1085 }
1086 _ => (0, 0),
1087 },
1088 };
1089
1090 // TSMP disco-key advertisement (Go `packet.TSMPDiscoKeyAdvertisement`,
1091 // upstream capability version 144). Go handles TSMP in
1092 // `tstun.filterPacketInboundFromWireGuard` *before* the ACL filter runs, and
1093 // returns `filter.DropSilently` for an advertisement: it is an inter-node
1094 // control message consumed here, never delivered to the local stack. Mirror
1095 // both the position (after source attribution, before the ACL) and the drop.
1096 //
1097 if proto == IpProto::TSMP
1098 && let Some(advert) = ts_packet::tsmp::DiscoKeyAdvertisement::parse(bytes)
1099 {
1100 if advert.key_is_zero() {
1101 // Go publishes only `if !discoKeyAdvert.Key.IsZero()`. Still a
1102 // well-formed advertisement, so it is still dropped.
1103 tracing::debug!(
1104 ?peer_id,
1105 "TSMP disco-key advertisement carried the zero key; ignoring"
1106 );
1107 } else {
1108 tracing::debug!(?peer_id, %src, "learned peer disco key over TSMP");
1109 harvest.learned_disco_keys.push((peer_id, advert));
1110 }
1111 return false;
1112 }
1113
1114 // TSMP rejected-connection message (Go `packet.TailscaleRejectedHeader`): a peer telling us
1115 // it refused a connection *we* opened, and why. Go consumes it in
1116 // `wgengine.userspaceEngine.trackOpenPreFilterIn`, a pre-filter hook that runs ahead of the
1117 // ACL and returns `filter.DropSilently` — the message is an inter-node control message
1118 // addressed to the engine, never traffic for the local stack. Consumed in the same position
1119 // and dropped the same way here; without this it reaches `smoltcp` as an IP-proto-99
1120 // datagram it has no handler for.
1121 //
1122 // Narrower than Go's hook, which drops *every* TSMP packet it sees: only a well-formed
1123 // rejected-connection message is consumed here, so the TSMP types this fork does not
1124 // implement keep the unconditional accept `inbound_filter_verdict` has always given them.
1125 if proto == IpProto::TSMP
1126 && let Some(reject) = ts_packet::tsmp::TailscaleRejectedHeader::parse(bytes)
1127 {
1128 // Go `wgengine/pendopen.go`:
1129 // e.logf("open-conn-track: flow %v %v > %v rejected due to %v", ...)
1130 // Go looks the flow up in its pending-open table first and logs only for a flow it was
1131 // actually waiting on (or, on `MaybeBroken`, marks that flow problematic instead of
1132 // removing it). This fork keeps no pending-open table, so the message is logged
1133 // unconditionally and handed to the caller with `MaybeBroken` intact, for the embedder
1134 // to match against whatever it has open.
1135 //
1136 // `debug!`, not the `info!` Go's `logf` amounts to, precisely *because* the
1137 // pending-open lookup is missing: Go's log volume is bounded by the flows this node
1138 // opened, and an unconditional log's is bounded only by what a peer chooses to send.
1139 // Every field below is peer-supplied, and reaching here needs nothing but a WireGuard
1140 // session and a well-formed TSMP body — no pending-flow match, no rate limit — so at
1141 // `info!` any authenticated peer can drive an operator's default-level log as fast as
1142 // the link allows and bury real events under it. The same reasoning already puts the
1143 // sibling TSMP branches (the disco-key advertisement above, the reject *send* below)
1144 // at `debug!`. Nothing is lost: `rejected_flows` still carries the whole record, and
1145 // the embedder is the layer that *can* do Go's match, so it is the layer that gets to
1146 // decide a rejection is worth an `info!`.
1147 tracing::debug!(
1148 ?peer_id,
1149 maybe_broken = reject.maybe_broken,
1150 "open-conn-track: flow {} {} > {} rejected due to {}",
1151 reject.proto,
1152 reject.src,
1153 reject.dst,
1154 reject.reason,
1155 );
1156 harvest.rejected_flows.push((peer_id, reject));
1157 return false;
1158 }
1159
1160 // The inbound proto-switch (Go `runIn4`/`runIn6`): Go `pre()` multicast/link-local
1161 // drops, then the fragment classification (Go `decode4` + `pre()`), then
1162 // unconditional TSMP accept, then the control-derived ACL. The caller's source
1163 // attribution and `or_in.route` bound this to attributable peers and local
1164 // destinations (Go's `local4`/`local6` precondition).
1165 let src = std::net::SocketAddr::new(src, src_port);
1166 let dst = std::net::SocketAddr::new(dst, dst_port);
1167
1168 let verdict = inbound_filter_verdict(filter, flows, proto, src, dst, l4, frag);
1169
1170 // Go, on any non-`Accept` outcome: "Tell them, via TSMP, we're dropping them due to the
1171 // ACL. Their host networking stack can translate this into ICMP or whatnot as required. But
1172 // notably, their GUI or tailscale CLI can show them a rejection history with reasons."
1173 // Almost every drop stays silent — see `tsmp_reject_for_drop` for the guard, and for the
1174 // one shape (a peerAPI SYN) upstream silences by admitting rather than by suppressing.
1175 if !verdict
1176 && let Some(reject) =
1177 tsmp_reject_for_drop(proto, src, dst, l4, filter.shields_up(), reject_cfg)
1178 {
1179 tracing::debug!(
1180 ?peer_id,
1181 ?src,
1182 ?dst,
1183 "sending a TSMP reject for a dropped SYN"
1184 );
1185 harvest.rejects_to_send.push((peer_id, reject.into()));
1186 }
1187
1188 verdict
1189 });
1190}
1191
1192/// Where this node sends a TSMP disco-key advertisement, and what it puts in one.
1193///
1194/// The send half of Go's capability version 144 (`packet.TSMPDiscoKeyAdvertisement`): when a
1195/// WireGuard session with a peer is established, this node announces its own disco public key to
1196/// that peer over TSMP, so the peer can learn (or re-learn) the key without waiting for a netmap
1197/// update from control. It is the mirror image of the receive half in
1198/// `filter_inbound_from_peer`, and both are unconditional — a real Go peer sends us one whether
1199/// or not we send one back.
1200///
1201/// This is the netmap state Go's [`magicsock.Conn.PriorityMessageForPeer`] reads, snapshotted into
1202/// the dataplane so building the message stays a cheap, synchronous, allocation-only step on the
1203/// datapath. wireguard-go requires the same of its callback: "must be cheap and must not call back
1204/// into the [`Device`]". The runtime refreshes the snapshot whenever the netmap changes.
1205///
1206/// [`magicsock.Conn.PriorityMessageForPeer`]: https://github.com/tailscale/tailscale/blob/main/wgengine/magicsock/magicsock.go
1207/// [`Device`]: https://github.com/tailscale/wireguard-go/blob/main/device/device.go
1208#[derive(Debug, Clone, Default)]
1209pub struct DiscoAdvertisementState {
1210 /// This node's own disco public key, raw (Go `Conn.DiscoPublicKey()`). The all-zero key means
1211 /// "no disco key", and nothing is ever advertised — Go's first refusal.
1212 pub disco_key: [u8; ts_packet::tsmp::DISCO_KEY_LEN],
1213 /// This node's own tailnet addresses, in the order control sent them (Go `self.Addresses()`,
1214 /// already narrowed to the single-IP prefixes `selfIPMatchingFamily` accepts). The
1215 /// advertisement's source is the first entry matching the destination's family.
1216 pub self_addrs: Vec<std::net::IpAddr>,
1217 /// Where to send an advertisement, per peer. A peer absent from this map is never advertised
1218 /// to — Go's `endpointForNodeKey` miss.
1219 pub peers: HashMap<PeerId, AdvertisementTarget>,
1220}
1221
1222/// One peer's advertisement destination, as [`DiscoAdvertisementState`] holds it.
1223#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1224pub struct AdvertisementTarget {
1225 /// The peer's first tailnet address (Go `endpoint.nodeAddr`), which is the advertisement's
1226 /// destination address.
1227 pub node_addr: std::net::IpAddr,
1228 /// Whether this is a plain WireGuard peer rather than a Tailscale node (Go
1229 /// `endpoint.isWireguardOnly`). Such a peer speaks no TSMP, so Go never sends it one — and a
1230 /// kernel-WireGuard or `wireguard-go` peer would hand the advertisement straight to its host
1231 /// network stack as an unknown-protocol packet.
1232 pub wireguard_only: bool,
1233}
1234
1235impl DiscoAdvertisementState {
1236 /// The marshalled TSMP disco-key advertisement to send `peer` on session establishment, or
1237 /// `None` if this node must not advertise to it.
1238 ///
1239 /// Go [`magicsock.Conn.PriorityMessageForPeer`], refusal for refusal — every one of these is a
1240 /// silent "send nothing", never a fallback to some other message:
1241 ///
1242 /// 1. **No disco key of our own** (`disco.IsZero()`): there is nothing to advertise.
1243 /// 2. **Unknown peer** (`endpointForNodeKey` miss, or `!self.Valid()`): the netmap snapshot has
1244 /// no destination address for this WireGuard peer, so any address we invented would be a
1245 /// guess.
1246 /// 3. **A WireGuard-only peer** (`ep.isWireguardOnly`): "Do not send TSMP messages to peers
1247 /// that only speaks wireguard."
1248 /// 4. **No source address in the destination's family** (`selfIPMatchingFamily` returning the
1249 /// zero `Addr`): an IPv4-only node has nothing to put in the source field of a packet to a
1250 /// peer's IPv6 address.
1251 /// 5. A marshal refusal, which by construction of (4) cannot happen — see
1252 /// [`ts_packet::tsmp::DiscoKeyAdvertisement::marshal`].
1253 ///
1254 /// [`magicsock.Conn.PriorityMessageForPeer`]: https://github.com/tailscale/tailscale/blob/main/wgengine/magicsock/magicsock.go
1255 pub fn advertisement_for(&self, peer: PeerId) -> Option<Vec<u8>> {
1256 if self.disco_key == [0u8; ts_packet::tsmp::DISCO_KEY_LEN] {
1257 tracing::debug!(?peer, "no disco key of our own; not advertising");
1258 return None;
1259 }
1260
1261 let target = self.peers.get(&peer)?;
1262
1263 if target.wireguard_only {
1264 return None;
1265 }
1266
1267 let src = self_ip_matching_family(&self.self_addrs, target.node_addr)?;
1268
1269 ts_packet::tsmp::DiscoKeyAdvertisement {
1270 src,
1271 dst: target.node_addr,
1272 key: self.disco_key,
1273 }
1274 .marshal()
1275 .inspect_err(|e| tracing::debug!(?peer, error = %e, "not advertising our disco key"))
1276 .ok()
1277 }
1278}
1279
1280/// This node's first tailnet address whose family matches `want`, or `None`.
1281///
1282/// Go `magicsock.selfIPMatchingFamily`, which walks `self.Addresses()` and returns the first
1283/// single-IP prefix with `Addr().BitLen() == want.BitLen()`. `addrs` is already narrowed to
1284/// single IPs by the caller that builds the snapshot, so only the family test remains.
1285fn self_ip_matching_family(
1286 addrs: &[std::net::IpAddr],
1287 want: std::net::IpAddr,
1288) -> Option<std::net::IpAddr> {
1289 addrs
1290 .iter()
1291 .copied()
1292 .find(|addr| addr.is_ipv4() == want.is_ipv4())
1293}
1294
1295/// The `tstun_out_to_wg_drop_tsmp` counter (Go `metricPacketOutDropTSMP`), registered into the
1296/// process-global registry on first use and exported by `ts_metrics::write_prometheus`. This is the
1297/// durable signal for [`outbound_packet_carries_tsmp`] firing: the datapath log below it is
1298/// `debug!`, because a local process can write these as fast as it likes and this tree has no
1299/// rate-limited logger to put behind Go's `limitedLogf`.
1300fn metric_out_to_wg_drop_tsmp() -> &'static ts_metrics::Metric {
1301 static M: std::sync::OnceLock<&'static ts_metrics::Metric> = std::sync::OnceLock::new();
1302 M.get_or_init(|| ts_metrics::Metric::new_counter("tstun_out_to_wg_drop_tsmp"))
1303}
1304
1305/// Whether the IP packet `b`, written into the TUN by a local host process, carries TSMP and must
1306/// therefore be dropped before it reaches WireGuard.
1307///
1308/// Go `tstun.filterPacketOutboundToWireGuard`: "TSMP traffic should only originate from tailscaled,
1309/// not from the host itself." TSMP is the inter-node control channel — capability version 144's
1310/// disco-key advertisement rides it — so a TSMP packet the host writes is either a confused
1311/// networking stack or a local process forging a control message in this node's name. A peer cannot
1312/// tell a forged advertisement from one this node meant to send: both arrive inside this node's
1313/// WireGuard session, from this node's tailnet address. It would bind whatever disco key the forger
1314/// chose.
1315///
1316/// The advertisements this node legitimately sends never pass through here. They are built in
1317/// [`DiscoAdvertisementState::advertisement_for`] and injected straight into the WireGuard session
1318/// by [`DataPlane::process_inbound`] (the priority-message path), which is *below* this check —
1319/// the same relationship Go has, where `injectedRead` bypasses the outbound filter entirely.
1320///
1321/// # Where this is a superset of Go's classification, and why
1322///
1323/// Go tests the decoded `p.IPProto`, so a *malformed* proto-99 packet decodes to `ipproto.Unknown`
1324/// rather than TSMP and slips past this particular check — only to be dropped one step later by the
1325/// outbound ACL, whose `pre()` refuses `ipproto.Unknown` outright. This tree has no outbound ACL at
1326/// all, so there is no second refusal to fall through to; testing the header's protocol byte
1327/// reaches Go's *net* verdict (nothing carrying proto 99 leaves the host) in one step instead of
1328/// two. Concretely, three shapes are dropped here that Go's TSMP arm alone would not:
1329///
1330/// - an IPv4 TSMP packet that is fragmented, truncated, or shorter than `minTSMPSize`;
1331/// - an IPv6 packet whose Fragment extension header names TSMP but whose first fragment is too
1332/// short to hold a TSMP body;
1333/// - a *later* IPv6 fragment of a TSMP datagram (Go classifies it `ipproto.Fragment` and does put
1334/// it on the wire). This is the one shape Go sends and we do not, and it is unreachable in
1335/// practice: its head fragment is dropped by Go and by us alike, so no peer could ever reassemble
1336/// the datagram, and nothing in a Tailscale node ever emits a fragmented TSMP message in the
1337/// first place. No real peer can be relying on one arriving.
1338///
1339/// A Fragment header reached through a *chained* extension header (hop-by-hop, routing, destination
1340/// options) is deliberately not chased: Go's `decode6` only steps over a Fragment header that is the
1341/// base header's immediate Next Header, so such a packet decodes to `ipproto.Unknown` at every
1342/// Tailscale receiver — including [`ts_packet::tsmp::DiscoKeyAdvertisement::parse`] here — and is
1343/// discarded rather than read as a control message. It is not a forgery vector.
1344fn outbound_packet_carries_tsmp(b: &[u8]) -> bool {
1345 match b.first().map(|first| first >> 4) {
1346 // Go `decode4`: `q.IPProto = ipproto.Proto(b[9])`.
1347 Some(4) => b.len() >= IP4_HEADER_LEN && b[9] == ts_packet::tsmp::IP_PROTO_TSMP,
1348 Some(6) => {
1349 if b.len() < IP6_HEADER_LEN {
1350 return false;
1351 }
1352 // Go `decode6`: `q.IPProto = ipproto.Proto(b[6])`, then step over a leading Fragment
1353 // extension header and take its Next Header instead. Every fragment of one datagram
1354 // repeats that Next Header, so this catches the head fragment (which is what Go's TSMP
1355 // arm catches) and its followers alike.
1356 match b[6] {
1357 ts_packet::tsmp::IP_PROTO_TSMP => true,
1358 IP6_FRAG_HEADER => b
1359 .get(IP6_HEADER_LEN)
1360 .is_some_and(|next| *next == ts_packet::tsmp::IP_PROTO_TSMP),
1361 _ => false,
1362 }
1363 }
1364 // Not an IP packet at all: `or_out.route` drops it a moment later for want of a
1365 // destination address. Nothing to classify.
1366 _ => false,
1367 }
1368}
1369
1370/// The UDP or SCTP flow an outbound packet belongs to — `(proto, src, dst)` with the packet's own
1371/// source and destination — or `None` for anything Go's `UpdateOutboundFlowState` switch would not
1372/// record. The caller hands it to [`flowtrack::FlowCache::record_outbound`], which stores the
1373/// reverse.
1374///
1375/// This is Go's `net/packet.Parsed.decode4`/`decode6` narrowed to the two protocols that switch has
1376/// arms for, and it keeps that decoder's refusals rather than guessing:
1377///
1378/// - **The protocol comes from the base header only** — `decode4`'s `b[9]`, `decode6`'s `b[6]` —
1379/// so an IPv6 packet behind a chained extension header is not recorded even though etherparse can
1380/// walk to its UDP header. Go never reaches that header either, so recording it would be this
1381/// tree inventing a flow upstream does not track.
1382/// - **A non-first IPv4 fragment is not a flow.** `decode4` classifies it `ipproto.Fragment`, which
1383/// matches neither arm of Go's switch; it also has no transport header, so its ports would be
1384/// invented.
1385/// - **A leading IPv6 Fragment header is stepped over**, exactly as `decode6` does, so the *first*
1386/// fragment of an outbound datagram records the same tuple an unfragmented one would.
1387/// - **A truncated SCTP common header records nothing** (Go's `q.IPProto = unknown`), rather than
1388/// recording a flow on ports read as 0 — an entry keyed on port 0 would admit inbound SCTP that
1389/// no outbound packet ever justified.
1390fn outbound_udp_or_sctp_flow(
1391 b: &[u8],
1392) -> Option<(IpProto, std::net::SocketAddr, std::net::SocketAddr)> {
1393 let pkt = etherparse::SlicedPacket::from_ip(b).ok()?;
1394
1395 // Go's `sub`: the bytes from the sub-protocol header onwards. Only the SCTP arm reads it,
1396 // because etherparse's `TransportSlice` has no SCTP variant.
1397 let sub = match &pkt.net {
1398 Some(etherparse::NetSlice::Ipv4(ipv4)) => ipv4.payload().payload,
1399 Some(etherparse::NetSlice::Ipv6(ipv6)) => ipv6.payload().payload,
1400 _ => &[][..],
1401 };
1402
1403 let (proto, src_ip, dst_ip, v6_first_fragment_ports) = match &pkt.net {
1404 Some(etherparse::NetSlice::Ipv4(ipv4)) => {
1405 let hdr = ipv4.header();
1406 if hdr.fragments_offset().value() > 0 {
1407 return None;
1408 }
1409 (
1410 IpProto::new(i64::from(ipv4.payload().ip_number.0)),
1411 std::net::IpAddr::from(hdr.source_addr()),
1412 std::net::IpAddr::from(hdr.destination_addr()),
1413 None,
1414 )
1415 }
1416 Some(etherparse::NetSlice::Ipv6(ipv6)) => {
1417 let hdr = ipv6.header();
1418 let src_ip = std::net::IpAddr::from(hdr.source_addr());
1419 let dst_ip = std::net::IpAddr::from(hdr.destination_addr());
1420 if hdr.next_header().0 == IP6_FRAG_HEADER {
1421 // Go `decode6Fragment`: a first fragment yields the real sub-protocol and its
1422 // ports; a later or malformed one yields no flow at all.
1423 let Ipv6Fragment::First {
1424 proto,
1425 src_port,
1426 dst_port,
1427 // The flow cache keys on Go's `flowtrack.Tuple` — proto and both address/port
1428 // pairs — and nothing else, so the L4 header is not part of an outbound flow.
1429 l4: _,
1430 } = decode6_fragment(b)
1431 else {
1432 return None;
1433 };
1434 (proto, src_ip, dst_ip, Some((src_port, dst_port)))
1435 } else {
1436 (
1437 IpProto::new(i64::from(hdr.next_header().0)),
1438 src_ip,
1439 dst_ip,
1440 None,
1441 )
1442 }
1443 }
1444 // Not an IP packet at all; `or_out.route` drops it a moment later for want of a
1445 // destination address.
1446 _ => return None,
1447 };
1448
1449 /// Go `net/packet.udpHeaderLength`.
1450 const UDP_HEADER_LEN: usize = 8;
1451
1452 // Go's `decode4`/`decode6` read both ports straight out of `sub` — `sub[0:2]` and `sub[2:4]` —
1453 // after a bounds check, for exactly the protocols below. Reading them here rather than from
1454 // etherparse's `TransportSlice` is not a shortcut: etherparse deliberately refuses to descend
1455 // into a fragmenting payload, so a *first* IPv4 fragment would otherwise surface no ports at
1456 // all, where Go reads them and records the flow like an unfragmented datagram's.
1457 let (src_port, dst_port) = match (proto, v6_first_fragment_ports) {
1458 (IpProto::UDP | IpProto::SCTP, Some(ports)) => ports,
1459 (IpProto::UDP, None) => {
1460 if sub.len() < UDP_HEADER_LEN {
1461 return None;
1462 }
1463 (
1464 u16::from_be_bytes([sub[0], sub[1]]),
1465 u16::from_be_bytes([sub[2], sub[3]]),
1466 )
1467 }
1468 (IpProto::SCTP, None) => sctp_ports(sub)?,
1469 // Every other protocol: Go's switch has no arm for it, so nothing is recorded.
1470 _ => return None,
1471 };
1472
1473 Some((
1474 proto,
1475 std::net::SocketAddr::new(src_ip, src_port),
1476 std::net::SocketAddr::new(dst_ip, dst_port),
1477 ))
1478}
1479
1480/// A data plane subsystem that can be the subject of timer events.
1481pub enum Subsystem {
1482 /// The wireguard component.
1483 Wireguard,
1484}
1485
1486/// The direction/path of a captured packet, mirroring Go Tailscale's `capture.Path`. The numeric
1487/// values are the on-wire path codes written into each pcap record's Tailscale preamble.
1488#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1489pub enum CapturePath {
1490 /// A packet from the local device, heading out to a peer (pre-encrypt).
1491 FromLocal = 0,
1492 /// A packet received from a peer, decrypted, heading to the local device.
1493 FromPeer = 1,
1494 /// A packet synthesized by us toward the local device. Retained for Go `capture.Path` on-wire
1495 /// code parity (so captured pcap path codes match Go's, and a future synthesized-packet tee
1496 /// point can emit it); not currently emitted — the tee only produces `FromLocal`/`FromPeer`.
1497 SynthesizedToLocal = 2,
1498 /// A packet synthesized by us toward a peer. Retained for Go `capture.Path` on-wire code parity
1499 /// (see [`Self::SynthesizedToLocal`]); not currently emitted.
1500 SynthesizedToPeer = 3,
1501}
1502
1503impl CapturePath {
1504 /// The on-wire path code (the `uint16` written into the pcap record preamble).
1505 pub fn code(self) -> u16 {
1506 self as u16
1507 }
1508}
1509
1510/// A debug packet-capture hook. When installed on a [`DataPlane`], it is invoked with the path and
1511/// the raw IP packet bytes for every plaintext packet crossing the datapath. It must be cheap and
1512/// non-blocking — it runs inline on the single-threaded dataplane step, so a slow hook backs up the
1513/// datapath. Wrapped in `Arc` so it is cheap to clone and `Send + Sync` for the actor that installs
1514/// it.
1515pub type CaptureHook = std::sync::Arc<dyn Fn(CapturePath, &[u8]) + Send + Sync>;
1516
1517/// Transforms packets to make tailscale happen.
1518pub struct DataPlane {
1519 /// Wireguard encryption/decryption.
1520 pub wireguard: Endpoint,
1521
1522 /// Outbound overlay router.
1523 pub or_out: or::outbound::Router,
1524 /// Outbound underlay router.
1525 pub ur_out: ur::outbound::Router,
1526
1527 /// Inbound source filter.
1528 pub src_filter_in: Arc<ts_bart::Table<PeerId>>,
1529 /// Inbound overlay router.
1530 pub or_in: or::inbound::Router,
1531
1532 /// The packet filter.
1533 pub packet_filter: Arc<dyn ts_packetfilter::Filter + Send + Sync>,
1534
1535 /// Events queued for future processing.
1536 pub events: Scheduler<Subsystem>,
1537
1538 /// Next event for the wireguard subsystem.
1539 pub wg_next: Option<Handle<Subsystem>>,
1540
1541 /// Optional debug packet-capture hook (Go `tstun.Wrapper` capture hook). `None` (the default)
1542 /// means no capture and zero datapath overhead. Installed/cleared at runtime by the dataplane
1543 /// actor; see [`DataPlane::process_outbound`]/[`DataPlane::process_inbound`] for the tee points.
1544 pub capture: Option<CaptureHook>,
1545
1546 /// Reverse-flow connection tracking for outbound UDP and SCTP, so a peer's reply to a
1547 /// datagram this node sent is admitted without an ACL rule naming our ephemeral source port
1548 /// (Go `filter.Filter.state`). Filled by [`DataPlane::process_outbound`] and consulted by the
1549 /// inbound filter; bounded at Go's `lruMax`. Private because it is derived datapath state, not
1550 /// configuration — nothing outside this crate has anything to set it to.
1551 flows: flowtrack::FlowCache,
1552
1553 /// Netmap snapshot for the TSMP disco-key advertisement this node sends on session
1554 /// establishment (Go capability version 144). `None` (the default) advertises nothing at all,
1555 /// which is what an embedder that never populates it gets — the same position this fork was in
1556 /// before the send side existed, and still fully interoperable, since a peer's own
1557 /// advertisement is unsolicited. Refreshed from the netmap by the runtime's dataplane actor.
1558 pub disco_advertisement: Option<Arc<DiscoAdvertisementState>>,
1559
1560 /// The TCP port this node's peerAPI listens on, if it runs one (Go `tstun.Wrapper.PeerAPIPort`).
1561 /// Read only when a dropped packet is about to be answered with a TSMP rejected-connection
1562 /// message: a SYN to the peerAPI port never produces one, because upstream's peerAPI carve-out
1563 /// has already flipped it back to `Accept` by that point. See `tsmp_reject_for_drop`.
1564 ///
1565 /// `None` (the default) means no peerAPI, and is also what an embedder that never sets it gets
1566 /// — the same position this fork was in before the message existed. Refreshed by the runtime's
1567 /// dataplane actor.
1568 pub peerapi_port: Option<u16>,
1569
1570 /// Go `tstun.Wrapper.disableTSMPRejected`: when `true`, an ACL drop tells the peer nothing.
1571 /// `false` (the default) is upstream's default and the interoperable one — a Go peer expects
1572 /// to be told.
1573 pub disable_tsmp_rejected: bool,
1574}
1575
1576impl DataPlane {
1577 /// Creates a new data plane for a wireguard node key.
1578 pub fn new(my_key: NodeKeyPair) -> Self {
1579 DataPlane {
1580 wireguard: Endpoint::new(my_key),
1581 or_out: Default::default(),
1582 ur_out: Default::default(),
1583 src_filter_in: Default::default(),
1584 or_in: Default::default(),
1585 events: Default::default(),
1586 packet_filter: Arc::new(ts_packetfilter::DropAllFilter),
1587 wg_next: None,
1588 capture: None,
1589 flows: flowtrack::FlowCache::default(),
1590 disco_advertisement: None,
1591 peerapi_port: None,
1592 disable_tsmp_rejected: false,
1593 }
1594 }
1595
1596 /// Processes packets originating from the local device.
1597 ///
1598 /// Packets carrying TSMP are refused here (Go `tstun.filterPacketOutboundToWireGuard`): the
1599 /// inter-node control channel must only ever carry messages this node built, never bytes a host
1600 /// process handed us. See `outbound_packet_carries_tsmp` for why, and for the one shape Go
1601 /// forwards that this refuses.
1602 #[tracing::instrument(skip_all, fields(n_packets = packets.len()))]
1603 pub fn process_outbound(&mut self, mut packets: Vec<PacketMut>) -> OutboundResult {
1604 // The capture tee runs first, and so still sees the packets dropped just below — Go tees to
1605 // its capture hook in `Wrapper.Read` before calling the outbound filter, so a pcap taken on
1606 // either implementation shows the refused packet.
1607 if let Some(hook) = &self.capture {
1608 for p in &packets {
1609 hook(CapturePath::FromLocal, p.as_ref());
1610 }
1611 }
1612
1613 packets.retain(|p| {
1614 if outbound_packet_carries_tsmp(p.as_ref()) {
1615 tracing::debug!("[unexpected] TSMP packet written into the tun; dropping");
1616 metric_out_to_wg_drop_tsmp().inc();
1617 return false;
1618 }
1619 true
1620 });
1621
1622 // Go `filter.Filter.UpdateOutboundFlowState`, called from `RunOut` for every packet read
1623 // off the TUN and — since upstream `e0677ccc7` — from `net/tstun`'s injected path too,
1624 // because packets produced by netstack never pass `RunOut` and "a netstack-side dial of UDP
1625 // would send fine but the reply would be dropped as `no matching rule`". Every outbound
1626 // packet in this engine comes from the netstack, so this is that call site. It runs after
1627 // the TSMP refusal above, matching Go's order in `filterPacketOutboundToWireGuard`, and
1628 // before `or_out.route` consumes the batch.
1629 for p in &packets {
1630 if let Some((proto, src, dst)) = outbound_udp_or_sctp_flow(p.as_ref()) {
1631 self.flows.record_outbound(proto, src, dst);
1632 }
1633 }
1634
1635 let or::outbound::Result {
1636 to_wireguard,
1637 loopback,
1638 } = self.or_out.route(packets);
1639
1640 let to_wireguard = to_wireguard
1641 .into_iter()
1642 .map(|(k, v)| (ts_tunnel::PeerId(k.0), v))
1643 .collect::<Vec<_>>();
1644
1645 let ts_tunnel::SendResult {
1646 to_peers: encrypted,
1647 } = self.wireguard.send(to_wireguard);
1648
1649 let to_peers = self
1650 .ur_out
1651 .route(encrypted.into_iter().map(|(k, v)| (PeerId(k.0), v)));
1652
1653 if let Some(next) = self.wireguard.next_event()
1654 && let Some(prev) = self
1655 .wg_next
1656 .replace(self.events.add(next, Subsystem::Wireguard))
1657 {
1658 prev.cancel();
1659 }
1660
1661 OutboundResult { to_peers, loopback }
1662 }
1663
1664 /// Processes packets received from elsewhere, with no information about which peer sent them.
1665 ///
1666 /// Equivalent to [`DataPlane::process_inbound_from`] with no attribution; see there for what
1667 /// the attribution buys.
1668 pub fn process_inbound(
1669 &mut self,
1670 packets: impl IntoIterator<Item = PacketMut>,
1671 ) -> InboundResult {
1672 self.process_inbound_from(None, packets)
1673 }
1674
1675 /// Processes packets an underlay transport received and attributed to peer `from`.
1676 ///
1677 /// The attribution is what lets the WireGuard layer answer a handshake initiation with a
1678 /// cookie while it is under load: the reply has to go back where the initiation came from, and
1679 /// in this stack that origin is a peer, not a source address. See
1680 /// [`ts_tunnel::Endpoint::recv_from`].
1681 pub fn process_inbound_from(
1682 &mut self,
1683 from: Option<PeerId>,
1684 packets: impl IntoIterator<Item = PacketMut>,
1685 ) -> InboundResult {
1686 let ts_tunnel::RecvResult {
1687 to_local,
1688 to_peers,
1689 sessions_established,
1690 } = self
1691 .wireguard
1692 .recv_from(from.map(|p| ts_tunnel::PeerId(p.0)), packets);
1693
1694 if let Some(hook) = &self.capture {
1695 for packets in to_local.values() {
1696 for p in packets {
1697 hook(CapturePath::FromPeer, p.as_ref());
1698 }
1699 }
1700 }
1701
1702 // The TSMP messages this batch consumes or synthesizes (Go `tstun.Wrapper`'s
1703 // `discoKeyAdvertisementPub` publisher, its `trackOpenPreFilterIn` hook, and its
1704 // `InjectOutbound` of a rejected-connection reply). Filled in by the packet-filter stage
1705 // below, which is the point at which a packet has both been attributed to a peer and been
1706 // decoded far enough to know it is TSMP.
1707 let mut harvest = InboundHarvest::default();
1708
1709 // Hoisted out of the filter closure below, which borrows `self.flows` mutably.
1710 let reject_cfg = RejectConfig {
1711 disabled: self.disable_tsmp_rejected,
1712 peerapi_port: self.peerapi_port,
1713 };
1714
1715 let to_local = to_local
1716 .into_iter()
1717 .map(|(peer_id, mut packets)| -> (PeerId, Vec<PacketMut>) {
1718 let _span = tracing::trace_span!(
1719 "src_filter_inbound",
1720 peer_id = ?peer_id,
1721 n_packet = packets.len(),
1722 )
1723 .entered();
1724
1725 packets.retain(|packet| {
1726 let Some(src) = packet.get_src_addr() else {
1727 tracing::trace!("does not look like ip packet");
1728 return false;
1729 };
1730 let verdict = if let Some(allowed_peer) = self.src_filter_in.lookup(src) {
1731 *allowed_peer == PeerId(peer_id.0)
1732 } else {
1733 tracing::trace!(remote_ip = %src, "unknown peer address");
1734 false
1735 };
1736 tracing::trace!(?src, verdict);
1737 verdict
1738 });
1739
1740 (PeerId(peer_id.0), packets)
1741 })
1742 .map(|(peer_id, mut v)| {
1743 let _span = tracing::trace_span!(
1744 "packet_filter_inbound",
1745 peer_id = ?peer_id,
1746 n_packet = v.len()
1747 )
1748 .entered();
1749
1750 filter_inbound_from_peer(
1751 self.packet_filter.as_ref(),
1752 &mut self.flows,
1753 peer_id,
1754 &mut v,
1755 reject_cfg,
1756 &mut harvest,
1757 );
1758
1759 v
1760 })
1761 // Forced here rather than left lazy: the TSMP rejected-connection replies the filter
1762 // stage synthesizes have to be encrypted and merged into `to_peers` below, and that
1763 // cannot happen while the iterator that produces them is still unconsumed.
1764 .collect::<Vec<_>>();
1765
1766 // TSMP disco-key advertisement, send side (Go capability version 144). wireguard-go calls
1767 // `peer.SendPriorityMessage()` the moment a keypair becomes current for forward
1768 // transmission — on the initiator when the handshake response lands, and on the responder
1769 // when the first transport packet authenticates on the new keypair (`device/receive.go`).
1770 // `sessions_established` is exactly those two moments; the message is Go's
1771 // `magicsock.Conn.PriorityMessageForPeer` return value. A peer we must not advertise to
1772 // (see [`DiscoAdvertisementState::advertisement_for`]) simply gets nothing, and the fresh
1773 // session is otherwise untouched.
1774 let mut to_peers = to_peers;
1775
1776 // TSMP rejected-connection replies, send side (Go `tstun.Wrapper.InjectOutbound`, called
1777 // from `filterPacketInboundFromWireGuard` on an ACL drop). Each goes to the peer whose own
1778 // packet was dropped — the peer whose session decrypted it, which the source filter has
1779 // already bound to that packet's source address, so this is the same node Go reaches by
1780 // routing the injected packet on its destination IP.
1781 //
1782 // These deliberately bypass `process_outbound`'s TSMP refusal: that refusal exists to stop
1783 // a *host process* writing TSMP into the tun, and upstream's injected packets skip the
1784 // outbound filter for the same reason.
1785 if !harvest.rejects_to_send.is_empty() {
1786 let mut by_peer: HashMap<ts_tunnel::PeerId, Vec<PacketMut>> = HashMap::new();
1787 for (peer, packet) in std::mem::take(&mut harvest.rejects_to_send) {
1788 by_peer
1789 .entry(ts_tunnel::PeerId(peer.0))
1790 .or_default()
1791 .push(packet);
1792 }
1793 let ts_tunnel::SendResult { to_peers: sealed } = self.wireguard.send(by_peer);
1794 for (peer, packets) in sealed {
1795 to_peers.entry(peer).or_default().extend(packets);
1796 }
1797 }
1798
1799 if let Some(advert) = self.disco_advertisement.clone() {
1800 // Held apart from what `recv` already queued for these peers so it can be spliced in
1801 // FRONT of it below, rather than appended behind it.
1802 let mut priority: HashMap<ts_tunnel::PeerId, Vec<PacketMut>> = HashMap::new();
1803 for peer in sessions_established {
1804 let Some(msg) = advert.advertisement_for(PeerId(peer.0)) else {
1805 continue;
1806 };
1807 tracing::debug!(peer_id = ?peer, "advertising our disco key over TSMP");
1808 for (peer, packets) in self.wireguard.send_priority_message(peer, &msg).to_peers {
1809 priority.entry(peer).or_default().extend(packets);
1810 }
1811 }
1812 // A priority message leads the traffic the same establishment released. wireguard-go
1813 // hands it straight to the peer's *outbound* queue (`SendPriorityMessage` →
1814 // `queueOutboundIfRunning`), never to the staged queue, and both call sites run it
1815 // before the flush that follows — `peer.SendPriorityMessage()` ahead of
1816 // `peer.SendKeepalive()` on the initiator and ahead of `peer.SendStagedPackets()` on
1817 // the responder (`device/receive.go`). Here the flush has already happened inside
1818 // [`Endpoint::recv`] (`activate` encrypts whatever was queued), so restoring Go's wire
1819 // order means splicing the advertisement in front of it.
1820 //
1821 // Only the wire order is restored, not Go's nonce order: those flushed packets were
1822 // sealed first and so hold the lower nonces, where Go would have numbered the priority
1823 // message first. That is invisible to the peer. A WireGuard receiver accepts an
1824 // earlier counter after a later one by construction, and the inversion is bounded by
1825 // the send queue a session flushes on activation (`MAX_QUEUED_PER_PEER`, 32 packets) —
1826 // two orders of magnitude inside the 8128-packet anti-replay window WireGuard
1827 // receivers carry (`ts_tunnel`'s `ReplayWindow::WINDOW_SIZE`, wireguard-go parity).
1828 for (peer, mut packets) in priority {
1829 let queued = to_peers.entry(peer).or_default();
1830 packets.append(queued);
1831 *queued = packets;
1832 }
1833 }
1834
1835 let to_peers = to_peers
1836 .into_iter()
1837 .map(|(k, v)| (ts_transport::PeerId(k.0), v));
1838
1839 let to_local = self.or_in.route(to_local.into_iter().flatten());
1840 let to_peers = self.ur_out.route(to_peers);
1841
1842 if let Some(next) = self.wireguard.next_event()
1843 && let Some(prev) = self
1844 .wg_next
1845 .replace(self.events.add(next, Subsystem::Wireguard))
1846 {
1847 prev.cancel();
1848 }
1849
1850 InboundResult {
1851 to_local,
1852 to_peers,
1853 learned_disco_keys: harvest.learned_disco_keys,
1854 rejected_flows: harvest.rejected_flows,
1855 }
1856 }
1857
1858 /// Return the next time at which [`DataPlane::process_events`] must be called.
1859 ///
1860 /// [`DataPlane::process_outbound`], [`DataPlane::process_inbound`] and
1861 /// [`DataPlane::process_events`] may all update the next event time. Callers should prefer
1862 /// calling `next_event` as needed to get a correct result, rather than store the returned
1863 /// value.
1864 pub fn next_event(&self) -> Option<Instant> {
1865 self.events.next_dispatch()
1866 }
1867
1868 /// Process all queued events that are due for processing.
1869 ///
1870 /// Must be called at least as often as dictated by [`DataPlane::next_event`] for the
1871 /// data plane to function correctly. It is harmless to call it more frequently.
1872 pub fn process_events(&mut self) -> EventResult {
1873 let mut to_peers = HashMap::new();
1874 let now = Instant::now();
1875 for event in self.events.dispatch(now) {
1876 match event {
1877 Subsystem::Wireguard => {
1878 let res = self.wireguard.dispatch_events(now);
1879 to_peers.extend(
1880 res.to_peers
1881 .into_iter()
1882 .map(|(id, pkts)| (ts_transport::PeerId(id.0), pkts)),
1883 );
1884 }
1885 }
1886 }
1887 let to_peers = self.ur_out.route(to_peers);
1888
1889 if let Some(next) = self.wireguard.next_event()
1890 && let Some(prev) = self
1891 .wg_next
1892 .replace(self.events.add(next, Subsystem::Wireguard))
1893 {
1894 prev.cancel();
1895 }
1896
1897 EventResult { to_peers }
1898 }
1899}
1900
1901/// The result of processing outbound packets.
1902pub struct OutboundResult {
1903 /// Packets to be sent into underlay transports for transmission.
1904 pub to_peers: HashMap<(UnderlayTransportId, PeerId), Vec<PacketMut>>,
1905 /// Packets to be looped back and delivered to overlay transports.
1906 pub loopback: HashMap<OverlayTransportId, Vec<PacketMut>>,
1907}
1908
1909/// The result of processing inbound packets.
1910pub struct InboundResult {
1911 /// Decrypted packets to be delivered to overlay transports.
1912 pub to_local: HashMap<OverlayTransportId, Vec<PacketMut>>,
1913 /// Encrypted packets to be sent to wireguard peers by the underlay.
1914 pub to_peers: HashMap<(UnderlayTransportId, PeerId), Vec<PacketMut>>,
1915 /// Disco keys peers advertised over TSMP in this batch, each paired with the WireGuard peer
1916 /// whose session carried it (Go `tstun.Wrapper` publishing `events.PeerDiscoKeyUpdate`, which
1917 /// `wgengine` turns into a `magicsock.Conn.HandleDiscoKeyAdvertisement` call).
1918 ///
1919 /// The advertisement packets themselves are dropped: they are inter-node control messages, not
1920 /// traffic for the local stack. Zero keys are already filtered out. Empty for a batch that
1921 /// carried none, which is the overwhelmingly common case.
1922 pub learned_disco_keys: Vec<(PeerId, ts_packet::tsmp::DiscoKeyAdvertisement)>,
1923 /// Connections peers refused, as they told us over TSMP (Go `packet.TailscaleRejectedHeader`,
1924 /// which `wgengine.userspaceEngine.trackOpenPreFilterIn` matches against its pending-open
1925 /// flows), each paired with the WireGuard peer whose session carried it.
1926 ///
1927 /// This engine is always the dialing client, so this is the answer to a connection *this node*
1928 /// opened: without it a dial into a peer's ACL drop waits out a TCP timeout with no reason
1929 /// available anywhere. `src` is this node's own address and ephemeral port and `dst` is the
1930 /// peer's, so an embedder can match the message against the flow it opened;
1931 /// [`maybe_broken`](ts_packet::tsmp::TailscaleRejectedHeader::maybe_broken) says whether the
1932 /// refusal is terminal.
1933 ///
1934 /// The messages themselves are dropped rather than delivered — they are inter-node control
1935 /// messages, and the local stack has no handler for IP protocol 99. Empty for a batch that
1936 /// carried none, which is the overwhelmingly common case.
1937 pub rejected_flows: Vec<(PeerId, ts_packet::tsmp::TailscaleRejectedHeader)>,
1938}
1939
1940/// The result of processing an event.
1941#[derive(Default)]
1942pub struct EventResult {
1943 /// Encrypted packets to be sent to wireguard peers by the underlay.
1944 pub to_peers: HashMap<(UnderlayTransportId, PeerId), Vec<PacketMut>>,
1945}
1946
1947#[cfg(test)]
1948mod tests {
1949 use std::sync::Mutex;
1950
1951 use super::*;
1952
1953 /// Records `(path, bytes)` for each capture-hook invocation in a test.
1954 type CaptureLog = Arc<Mutex<Vec<(CapturePath, Vec<u8>)>>>;
1955
1956 /// [`inbound_filter_verdict`] against an **empty** flow cache and with **no decoded L4 header**,
1957 /// which is the verdict for a packet that is not a reply to anything this node sent. Every case
1958 /// that predates the reverse-flow cache is exactly that case, so they all go through here and
1959 /// stay pinned to the stateless decision; the tests that do record an outbound flow call
1960 /// `inbound_filter_verdict` directly.
1961 ///
1962 /// [`ts_packetfilter::L4Header::Unknown`] is also the assertion that the reply carve-outs fail
1963 /// closed: a TCP packet whose flags were never read must still face the rules here, and it does.
1964 fn stateless_verdict(
1965 filter: &(dyn ts_packetfilter::Filter + Send + Sync),
1966 proto: IpProto,
1967 src: std::net::IpAddr,
1968 dst: std::net::IpAddr,
1969 dst_port: u16,
1970 frag: Option<Fragment>,
1971 ) -> bool {
1972 inbound_filter_verdict(
1973 filter,
1974 &mut flowtrack::FlowCache::default(),
1975 proto,
1976 std::net::SocketAddr::new(src, 0),
1977 std::net::SocketAddr::new(dst, dst_port),
1978 ts_packetfilter::L4Header::Unknown,
1979 frag,
1980 )
1981 }
1982
1983 #[test]
1984 fn capture_path_codes() {
1985 assert_eq!(CapturePath::FromLocal.code(), 0);
1986 assert_eq!(CapturePath::FromPeer.code(), 1);
1987 assert_eq!(CapturePath::SynthesizedToLocal.code(), 2);
1988 assert_eq!(CapturePath::SynthesizedToPeer.code(), 3);
1989 }
1990
1991 /// The pre-rule destination screen (Go filter `pre()`): multicast and non-allowlisted link-local
1992 /// destinations are dropped before the ACL; ordinary unicast and the cloud-metadata link-local
1993 /// exception pass through to the rules.
1994 #[test]
1995 fn pre_rule_drop_matches_go() {
1996 let ip = |s: &str| s.parse::<std::net::IpAddr>().unwrap();
1997 // Dropped pre-rules:
1998 assert!(drop_before_rules(ip("224.0.0.1")), "IPv4 multicast dropped");
1999 assert!(
2000 drop_before_rules(ip("239.255.255.250")),
2001 "IPv4 multicast (SSDP) dropped"
2002 );
2003 assert!(
2004 drop_before_rules(ip("169.254.1.1")),
2005 "IPv4 link-local dropped"
2006 );
2007 assert!(drop_before_rules(ip("ff02::1")), "IPv6 multicast dropped");
2008 assert!(drop_before_rules(ip("fe80::1")), "IPv6 link-local dropped");
2009 assert!(
2010 drop_before_rules(ip("febf:ffff::1")),
2011 "top of fe80::/10 dropped (locks the 0xffc0/0xfe80 mask)"
2012 );
2013 // Passed through to the rules:
2014 assert!(
2015 !drop_before_rules(ip("fec0::1")),
2016 "just past fe80::/10 passes (locks the 0xffc0/0xfe80 mask)"
2017 );
2018 // IPv4-mapped-IPv6 destinations match NEITHER arm and fall through to the ACL, exactly as
2019 // Go's `netip.Addr` predicates do (no unmap/canonicalize). Pinning this guards against a
2020 // future "canonicalize to be safe" refactor silently diverging from Go.
2021 assert!(
2022 !drop_before_rules(ip("::ffff:224.0.0.1")),
2023 "4in6-mapped multicast falls through to the ACL, matching Go"
2024 );
2025 assert!(
2026 !drop_before_rules(ip("::ffff:169.254.1.1")),
2027 "4in6-mapped link-local falls through to the ACL, matching Go"
2028 );
2029 assert!(
2030 !drop_before_rules(ip("100.64.0.5")),
2031 "ordinary tailnet unicast passes"
2032 );
2033 assert!(
2034 !drop_before_rules(ip("8.8.8.8")),
2035 "ordinary public unicast passes"
2036 );
2037 assert!(
2038 !drop_before_rules(ip("169.254.169.254")),
2039 "the cloud-metadata link-local address is the Go-allowlisted exception"
2040 );
2041 assert!(
2042 !drop_before_rules(ip("fd7a:115c:a1e0::1")),
2043 "IPv6 ULA (tailnet) passes"
2044 );
2045 }
2046
2047 /// A filter that drops everything (returns `None` for every packet). Lets a test prove that TSMP
2048 /// is admitted by bypassing the ACL — not by the ACL happening to allow it.
2049 struct DenyAll;
2050 impl ts_packetfilter::Filter for DenyAll {
2051 fn match_for(
2052 &self,
2053 _info: &ts_packetfilter::PacketInfo,
2054 _caps: ts_packetfilter::filter::CapIter,
2055 ) -> Option<&str> {
2056 None
2057 }
2058 }
2059
2060 /// The inbound proto-switch (Go `runIn4`/`runIn6`): TSMP is always admitted, bypassing the ACL;
2061 /// `pre()` drops still win over TSMP; non-TSMP defers to the ACL.
2062 #[test]
2063 fn tsmp_bypasses_acl_matches_go() {
2064 let ip = |s: &str| s.parse::<std::net::IpAddr>().unwrap();
2065 let src = ip("100.64.0.9");
2066 let dst = ip("100.64.0.1");
2067 let tsmp = IpProto::new(99);
2068
2069 // TSMP is accepted even though the ACL denies everything — Go `case TSMP: return Accept`.
2070 assert!(
2071 stateless_verdict(&DenyAll, tsmp, src, dst, 0, None),
2072 "TSMP admitted by bypassing the (deny-all) ACL"
2073 );
2074 // A non-TSMP proto under the same deny-all ACL is dropped — proves the bypass is TSMP-specific.
2075 assert!(
2076 !stateless_verdict(&DenyAll, IpProto::TCP, src, dst, 443, None),
2077 "TCP still consults the ACL (deny-all → dropped)"
2078 );
2079 // `pre()` drops outrank the TSMP accept: TSMP to a multicast/link-local dst is still dropped,
2080 // exactly as Go runs `pre()` before the proto switch.
2081 assert!(
2082 !stateless_verdict(&DenyAll, tsmp, src, ip("224.0.0.1"), 0, None),
2083 "TSMP to a multicast dst is still dropped (pre() before the switch)"
2084 );
2085 assert!(
2086 !stateless_verdict(&DenyAll, tsmp, src, ip("169.254.1.1"), 0, None),
2087 "TSMP to a link-local dst is still dropped (pre() before the switch)"
2088 );
2089 // IpProto::TSMP is the named constant for proto 99.
2090 assert_eq!(IpProto::TSMP, tsmp, "IpProto::TSMP == 99");
2091 }
2092
2093 /// IPv4 fragment handling, mirroring Go `net/packet.decode4` + filter `pre()`:
2094 /// - a valid later fragment (offset ≥ `MIN_FRAG_BLKS`) is ACCEPTED ahead of the ACL (Go maps it
2095 /// to `ipproto.Fragment`, which `pre()` admits) — even under a deny-all ACL and even though its
2096 /// parsed port is 0, which a normal rule would never match;
2097 /// - a low-offset later fragment (offset < `MIN_FRAG_BLKS`) is DROPPED (RFC 1858);
2098 /// - a first fragment (offset 0) defers to the normal proto-switch/ACL on its real port;
2099 /// - a *fragmented* TSMP first fragment (offset 0, MF set) is DROPPED (Go disallows it), unlike a
2100 /// non-fragmented TSMP which bypasses the ACL.
2101 #[test]
2102 fn ipv4_fragment_handling_matches_go_decode4() {
2103 let ip = |s: &str| s.parse::<std::net::IpAddr>().unwrap();
2104 let src = ip("100.64.0.9");
2105 let dst = ip("100.64.0.1");
2106 let frag = |offset_blocks: u16, more_fragments: bool| {
2107 Some(Fragment::V4(Ipv4Fragment {
2108 offset_blocks,
2109 more_fragments,
2110 }))
2111 };
2112
2113 // A valid later fragment is accepted under a DENY-ALL ACL with port 0 — proves the accept is
2114 // the Go `pre()` Fragment pass-through, not the ACL happening to allow it.
2115 assert!(
2116 stateless_verdict(
2117 &DenyAll,
2118 IpProto::TCP,
2119 src,
2120 dst,
2121 0,
2122 frag(MIN_FRAG_BLKS, false)
2123 ),
2124 "a valid later fragment (offset >= MIN_FRAG_BLKS) is accepted ahead of the ACL"
2125 );
2126 assert!(
2127 stateless_verdict(
2128 &DenyAll,
2129 IpProto::UDP,
2130 src,
2131 dst,
2132 0,
2133 frag(MIN_FRAG_BLKS + 50, true)
2134 ),
2135 "a later fragment well past the floor (MF set) is also accepted"
2136 );
2137
2138 // A low-offset later fragment (could overlap a transport header) is dropped — RFC 1858.
2139 assert!(
2140 !stateless_verdict(
2141 &DenyAll,
2142 IpProto::TCP,
2143 src,
2144 dst,
2145 0,
2146 frag(MIN_FRAG_BLKS - 1, false)
2147 ),
2148 "a low-offset later fragment is dropped (RFC 1858)"
2149 );
2150 assert!(
2151 !stateless_verdict(&DenyAll, IpProto::TCP, src, dst, 0, frag(1, false)),
2152 "the smallest non-zero offset is dropped"
2153 );
2154
2155 // A first fragment (offset 0) defers to the normal ACL on its real port: deny-all drops a
2156 // TCP first fragment, exactly as it drops a non-fragmented TCP packet.
2157 assert!(
2158 !stateless_verdict(&DenyAll, IpProto::TCP, src, dst, 443, frag(0, true)),
2159 "a first fragment defers to the ACL (deny-all -> dropped) on its parsed port"
2160 );
2161
2162 // A fragmented TSMP first fragment (offset 0, MF set) is dropped — Go disallows it — even
2163 // though a non-fragmented TSMP bypasses the ACL.
2164 assert!(
2165 !stateless_verdict(&DenyAll, IpProto::TSMP, src, dst, 0, frag(0, true)),
2166 "a fragmented TSMP first fragment is dropped (Go parity)"
2167 );
2168 assert!(
2169 stateless_verdict(&DenyAll, IpProto::TSMP, src, dst, 0, frag(0, false)),
2170 "a non-fragmented TSMP (offset 0, MF clear) still bypasses the ACL"
2171 );
2172
2173 // A *later* TSMP fragment (offset >= MIN_FRAG_BLKS) is accepted via the offset-based
2174 // fragment pass-through, NOT dropped by the fragmented-TSMP rule — that rule is offset-0
2175 // only (a first fragment with MF). This proves the later-fragment branch is proto-independent
2176 // and wins over the TSMP-specific logic (Go maps any offset>=minFragBlks to ipproto.Fragment
2177 // regardless of the L4 proto byte), locking the branch ordering against regression.
2178 assert!(
2179 stateless_verdict(
2180 &DenyAll,
2181 IpProto::TSMP,
2182 src,
2183 dst,
2184 0,
2185 frag(MIN_FRAG_BLKS, true)
2186 ),
2187 "a later TSMP fragment is accepted via the fragment path (proto-independent)"
2188 );
2189 }
2190
2191 /// The head fragment of a fragmented IPv4/UDP datagram from `src` to `dst`: the same 8-byte UDP
2192 /// header an unfragmented datagram carries, plus the More-Fragments bit, plus the UDP length
2193 /// field a real sender writes — the length of the **whole** reassembled datagram, not of the
2194 /// piece on the wire. That is the shape etherparse hands over with `transport == None`, and the
2195 /// one Go `decode4` parses under `if fragOfs == 0`.
2196 fn v4_udp_first_fragment(
2197 src: std::net::SocketAddr,
2198 dst: std::net::SocketAddr,
2199 payload: &[u8],
2200 ) -> Vec<u8> {
2201 let mut buf = v4_udp_packet(src, dst, payload);
2202 // Fragment offset 0, More Fragments set.
2203 buf[6..8].copy_from_slice(&0x2000u16.to_be_bytes());
2204 let whole_datagram = u16::try_from(8 + payload.len() + 512).unwrap();
2205 buf[24..26].copy_from_slice(&whole_datagram.to_be_bytes());
2206 buf
2207 }
2208
2209 /// A fragmented reply to a UDP flow *this node* opened is admitted whole — head fragment
2210 /// included — with no rule naming it.
2211 ///
2212 /// A DNS answer larger than the 1280-byte overlay MTU arrives as a head fragment plus later
2213 /// ones. The later fragments were already accepted on their offset alone (Go `pre()`'s
2214 /// `ipproto.Fragment` pass-through), but the head fragment carries the ports, and it reached
2215 /// the reverse-flow cache with both of them 0 — so it missed the entry `process_outbound` had
2216 /// just recorded, was dropped as "no matching rule", and the pieces that did arrive could never
2217 /// be reassembled. Go `decode4` reads that header (`if fragOfs == 0`, the `case ipproto.UDP`
2218 /// arm) exactly as it reads an unfragmented datagram's.
2219 ///
2220 /// Ported from github.com/tailscale/tailscale `net/packet/packet.go` (`decode4`) and
2221 /// `wgengine/filter/filter.go` (`pre`, `runIn4`) at
2222 /// `9ea7cba44591e0cd840c6c94d23274dd222059bf`.
2223 #[test]
2224 fn a_fragmented_reply_to_our_own_udp_flow_is_admitted_head_fragment_included() {
2225 let peer = PeerId(1);
2226 let me = std::net::IpAddr::from([100, 64, 0, 1]);
2227 let them = std::net::IpAddr::from([100, 64, 0, 2]);
2228 let sa = |ip, port| std::net::SocketAddr::new(ip, port);
2229 let mut dp = dataplane_routing_to(peer, &[them]);
2230
2231 let query = v4_udp_packet(sa(me, 41234), sa(them, 53), b"query");
2232 let head = v4_udp_first_fragment(sa(them, 53), sa(me, 41234), b"answer");
2233 // The rest of the same answer: a later fragment at a safe offset, carrying no header at all.
2234 let mut tail = head.clone();
2235 tail[6..8].copy_from_slice(&MIN_FRAG_BLKS.to_be_bytes());
2236
2237 assert!(
2238 !admitted(&DenyAll, &mut dp.flows, head.clone()),
2239 "before we send anything the head fragment is unsolicited, and a deny-all ACL drops it"
2240 );
2241
2242 let out = dp.process_outbound(vec![PacketMut::from(query)]);
2243 assert!(
2244 !out.to_peers.is_empty(),
2245 "the query really did route to the peer, so this is the live outbound path"
2246 );
2247
2248 assert!(
2249 admitted(&DenyAll, &mut dp.flows, head),
2250 "the head fragment of the reply rides the flow we opened (Go's `Accept, \"cached\"`)"
2251 );
2252 assert!(
2253 admitted(&DenyAll, &mut dp.flows, tail),
2254 "its later fragments still pass on their offset alone"
2255 );
2256 assert!(
2257 !admitted(
2258 &DenyAll,
2259 &mut dp.flows,
2260 v4_udp_first_fragment(sa(them, 53), sa(me, 41235), b"x")
2261 ),
2262 "a head fragment to a port we never sent from is not carried by the entry"
2263 );
2264 }
2265
2266 /// A first IPv4 fragment faces the rules — and the reply carve-outs — on the header it really
2267 /// carries, and is refused outright when it is too short to carry one.
2268 ///
2269 /// Go `decode4`, `if fragOfs == 0`: "Every protocol below MUST check that it has at least one
2270 /// entire transport header in order to protect against fragment confusion." A fragment cut off
2271 /// inside its own transport header is `q.IPProto = unknown`, which filter `pre()` drops before
2272 /// any rule is consulted — never a fallback to port 0, which an all-ports rule would admit
2273 /// while the follow-up fragments supplied the real header afterwards.
2274 ///
2275 /// Ported from github.com/tailscale/tailscale `net/packet/packet.go` (`decode4`) at
2276 /// `9ea7cba44591e0cd840c6c94d23274dd222059bf`.
2277 #[test]
2278 fn first_ipv4_fragment_is_decoded_like_go_decode4() {
2279 let flows = &mut flowtrack::FlowCache::default();
2280 let sa = |ip: std::net::Ipv4Addr, port| std::net::SocketAddr::new(ip.into(), port);
2281 let from = sa(IPV4_FIXTURE_SRC, 41234);
2282
2283 // The ACL matches on the port the fragment carries, both ways round.
2284 assert!(
2285 admitted(
2286 &AllowPort(443),
2287 flows,
2288 v4_udp_first_fragment(from, sa(IPV4_FIXTURE_DST, 443), b"x")
2289 ),
2290 "a head fragment to an allowed port is admitted on the port it carries"
2291 );
2292 assert!(
2293 !admitted(
2294 &AllowPort(443),
2295 flows,
2296 v4_udp_first_fragment(from, sa(IPV4_FIXTURE_DST, 8443), b"x")
2297 ),
2298 "a head fragment to any other port is not — the port is read, not assumed"
2299 );
2300
2301 // And the TCP flags byte with it, so the non-SYN carve-out applies to a fragmented segment
2302 // on exactly the terms it applies to an unfragmented one.
2303 let tcp_head = |flags| v4_packet(6, 0, true, &tcp_header(443, 41234, flags));
2304 assert!(
2305 admitted(&DenyAll, flows, tcp_head(TCP_SYN | TCP_ACK)),
2306 "a fragmented SYN-ACK takes the non-SYN carve-out"
2307 );
2308 assert!(
2309 !admitted(&DenyAll, flows, tcp_head(TCP_SYN)),
2310 "a fragmented SYN still faces the rules"
2311 );
2312
2313 // Go's fragment-confusion refusal, asserted under an ALLOW-ALL ACL: the drop can only be
2314 // the bounds check, because there is no rule left to refuse it.
2315 for (why, packet) in [
2316 (
2317 "a UDP head fragment one byte short of its own header",
2318 v4_packet(17, 0, true, &udp_header(443)[..7]),
2319 ),
2320 (
2321 "a TCP head fragment one byte short of its own header",
2322 v4_packet(6, 0, true, &tcp_header(443, 41234, TCP_ACK)[..19]),
2323 ),
2324 (
2325 "an ICMP head fragment shorter than its four-byte header",
2326 v4_packet(1, 0, true, &icmp_header(0, 0)[..3]),
2327 ),
2328 (
2329 "an SCTP head fragment shorter than its common header",
2330 v4_packet(132, 0, true, &sctp_header(443, SCTP_HEADER_LEN - 1)),
2331 ),
2332 ] {
2333 assert!(!admitted(&AllowAll, flows, packet), "{why} is dropped");
2334 }
2335 // The controls: at full length every one of those is admitted by the same ACL, so the
2336 // drops above are the bounds checks and not the fixtures.
2337 for (why, packet) in [
2338 (
2339 "UDP",
2340 v4_udp_first_fragment(from, sa(IPV4_FIXTURE_DST, 443), b"x"),
2341 ),
2342 ("TCP", tcp_head(TCP_SYN)),
2343 ("ICMP", v4_packet(1, 0, true, &icmp_header(8, 0))),
2344 (
2345 "SCTP",
2346 v4_packet(132, 0, true, &sctp_header(443, SCTP_HEADER_LEN)),
2347 ),
2348 ] {
2349 assert!(
2350 admitted(&AllowAll, flows, packet),
2351 "a full-length {why} head fragment is admitted"
2352 );
2353 }
2354
2355 // The decode itself, beside the verdicts it feeds: Go's `sub[0:2]`/`sub[2:4]` and
2356 // `sub[13]`, and its refusals.
2357 assert_eq!(
2358 decode4_first_fragment(IpProto::UDP, &udp_header(443)),
2359 Some((54276, 443, ts_packetfilter::L4Header::Unknown)),
2360 "`decode4`'s UDP arm reads both ports"
2361 );
2362 assert_eq!(
2363 decode4_first_fragment(IpProto::TCP, &tcp_header(443, 41234, TCP_SYN | TCP_ACK)),
2364 Some((
2365 443,
2366 41234,
2367 ts_packetfilter::L4Header::Tcp {
2368 flags: TCP_SYN | TCP_ACK
2369 }
2370 )),
2371 "`decode4`'s TCP arm reads the flags byte beside the ports"
2372 );
2373 assert_eq!(
2374 decode4_first_fragment(IpProto::ICMP, &icmp_header(0, 0)),
2375 Some((
2376 0,
2377 0,
2378 ts_packetfilter::L4Header::Icmp {
2379 icmp_type: 0,
2380 icmp_code: 0
2381 }
2382 )),
2383 "`decode4`'s ICMPv4 arm reads the type/code and leaves both ports 0"
2384 );
2385 assert_eq!(
2386 decode4_first_fragment(IpProto::UDP, &udp_header(443)[..7]),
2387 None,
2388 "a short UDP header is Go's `unknown`, not a port-0 pass"
2389 );
2390 assert_eq!(
2391 decode4_first_fragment(IPPROTO_FRAGMENT_SENTINEL, &udp_header(443)),
2392 None,
2393 "Go's internal later-fragment sentinel seen on the wire is `unknown` too"
2394 );
2395 assert_eq!(
2396 decode4_first_fragment(IpProto::new(89), &udp_header(443)),
2397 Some((0, 0, ts_packetfilter::L4Header::Unknown)),
2398 "a protocol Go's switch has no arm for keeps its number and both ports 0"
2399 );
2400 }
2401
2402 /// An ACL that admits everything, the shape a permissive "allow the whole tailnet" policy has.
2403 /// Under it, a DROP can only have come from a rule the filter applies *ahead* of the ACL — which
2404 /// is exactly what makes it the right control for the fragment classification's negative cases.
2405 struct AllowAll;
2406 impl ts_packetfilter::Filter for AllowAll {
2407 fn match_for(
2408 &self,
2409 _info: &ts_packetfilter::PacketInfo,
2410 _caps: ts_packetfilter::filter::CapIter,
2411 ) -> Option<&str> {
2412 Some("allow-all")
2413 }
2414 }
2415
2416 /// An ACL that admits exactly one destination port. An admitted packet therefore proves the
2417 /// filter read that port off the wire — the point of Go `decode6` reaching past the Fragment
2418 /// extension header to the first fragment's real transport header.
2419 struct AllowPort(u16);
2420 impl ts_packetfilter::Filter for AllowPort {
2421 fn match_for(
2422 &self,
2423 info: &ts_packetfilter::PacketInfo,
2424 _caps: ts_packetfilter::filter::CapIter,
2425 ) -> Option<&str> {
2426 (info.port == self.0).then_some("allow-port")
2427 }
2428 }
2429
2430 /// Source/destination for the IPv6 fixtures: RFC 3849 documentation addresses, standing in for
2431 /// the real ones upstream's `udp6*FragmentBuffer` fixtures use. Neither is multicast or
2432 /// link-local, so `drop_before_rules` never fires and every verdict below is the fragment
2433 /// classification's own.
2434 const IPV6_FIXTURE_SRC: std::net::Ipv6Addr =
2435 std::net::Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 5);
2436 const IPV6_FIXTURE_DST: std::net::Ipv6Addr =
2437 std::net::Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1);
2438
2439 /// The IPv6 packet a source-fragmenting host puts on the wire, in the shape of upstream's
2440 /// `udp6FirstFragmentBuffer` / `udp6NonFirstFragmentBuffer` fixtures (Go
2441 /// `net/packet/packet_test.go`): a 40-byte base header whose Next Header is the Fragment
2442 /// extension header (44), the 8-byte Fragment header itself, then `rest` — the real
2443 /// sub-protocol header on a first fragment, or continued payload on a later one.
2444 fn ipv6_fragment_packet(
2445 next_header: u8,
2446 offset_blocks: u16,
2447 more_fragments: bool,
2448 rest: &[u8],
2449 ) -> Vec<u8> {
2450 let mut buf = vec![0u8; IP6_HEADER_LEN + IP6_FRAG_HEADER_LEN + rest.len()];
2451 buf[0] = 0x60; // version 6, traffic class/flow label 0
2452 let payload_len = u16::try_from(IP6_FRAG_HEADER_LEN + rest.len()).unwrap();
2453 buf[4..6].copy_from_slice(&payload_len.to_be_bytes());
2454 buf[6] = IP6_FRAG_HEADER;
2455 buf[7] = 64; // hop limit
2456 buf[8..24].copy_from_slice(&IPV6_FIXTURE_SRC.octets());
2457 buf[24..40].copy_from_slice(&IPV6_FIXTURE_DST.octets());
2458 // Fragment extension header: Next Header, Reserved, offset<<3 | MF, Identification.
2459 buf[40] = next_header;
2460 let offset_field = (offset_blocks << 3) | u16::from(more_fragments);
2461 buf[42..44].copy_from_slice(&offset_field.to_be_bytes());
2462 buf[44..48].copy_from_slice(&[0xde, 0xad, 0xbe, 0xef]);
2463 buf[48..].copy_from_slice(rest);
2464 buf
2465 }
2466
2467 /// A plain, unfragmented IPv6 packet: the same 40-byte base header the fragment fixtures use,
2468 /// with `payload` sitting directly behind it as the protocol `next_header` names.
2469 fn ipv6_packet(next_header: u8, payload: &[u8]) -> Vec<u8> {
2470 let mut buf = vec![0u8; IP6_HEADER_LEN + payload.len()];
2471 buf[0] = 0x60; // version 6, traffic class/flow label 0
2472 buf[4..6].copy_from_slice(&u16::try_from(payload.len()).unwrap().to_be_bytes());
2473 buf[6] = next_header;
2474 buf[7] = 64; // hop limit
2475 buf[8..24].copy_from_slice(&IPV6_FIXTURE_SRC.octets());
2476 buf[24..40].copy_from_slice(&IPV6_FIXTURE_DST.octets());
2477 buf[IP6_HEADER_LEN..].copy_from_slice(payload);
2478 buf
2479 }
2480
2481 /// A plain, unfragmented IPv6/UDP packet: [`ipv6_packet`] with UDP as its immediate Next
2482 /// Header. The control for the chained-extension-header fixtures below.
2483 fn ipv6_udp_packet(udp: &[u8]) -> Vec<u8> {
2484 let mut buf = ipv6_packet(17, udp);
2485 // Unlike a fragment fixture, this datagram is actually parsed as UDP, so its Length field
2486 // has to agree with the bytes present or etherparse rejects the packet outright.
2487 let udp_len = u16::try_from(udp.len()).unwrap();
2488 buf[IP6_HEADER_LEN + 4..IP6_HEADER_LEN + 6].copy_from_slice(&udp_len.to_be_bytes());
2489 buf
2490 }
2491
2492 /// Push one 8-byte extension header of protocol `ext_proto` in front of `inner`'s payload, so
2493 /// whatever `inner`'s base header pointed at directly is now reached through a *chain*. The
2494 /// generic Next-Header / Hdr-Ext-Len-0 / six-bytes-of-body shape is the on-the-wire layout of
2495 /// Hop-by-Hop Options (0), Routing (43) and Destination Options (60) alike.
2496 ///
2497 /// Those six body bytes are chosen so the header is well formed under *every* one of those
2498 /// three readings, not merely one etherparse happens not to look at:
2499 ///
2500 /// - as Options (0 / 60) they are a TLV stream — `1, 0` is a zero-length PadN, and the four
2501 /// trailing zeros are four Pad1s, filling the 8-byte header exactly;
2502 /// - as Routing (43) they are Routing Type 1, **Segments Left 0**, and four bytes of
2503 /// type-specific data. Segments Left must stay 0: `Hdr Ext Len` is 0, so there is no room
2504 /// for a single 16-byte segment, and RFC 8200 §4.4 has a receiver that meets a non-zero
2505 /// Segments Left on an unrecognized Routing Type discard the packet and answer ICMP
2506 /// Parameter Problem. etherparse walks a Routing header as a raw ext header and never reads
2507 /// the field, so a non-zero value parses here today — but a fixture that only survives
2508 /// because the parser is lenient is one parser release away from turning the negative
2509 /// assertions below into vacuous passes.
2510 fn ipv6_with_prepended_ext_header(ext_proto: u8, inner: &[u8]) -> Vec<u8> {
2511 let mut buf = Vec::with_capacity(inner.len() + 8);
2512 buf.extend_from_slice(&inner[..IP6_HEADER_LEN]);
2513 // The header we are displacing becomes the extension header's Next Header.
2514 let displaced = buf[6];
2515 buf[6] = ext_proto;
2516 let payload_len = u16::try_from(inner.len() - IP6_HEADER_LEN + 8).unwrap();
2517 buf[4..6].copy_from_slice(&payload_len.to_be_bytes());
2518 buf.extend_from_slice(&[displaced, 0, 1, 0, 0, 0, 0, 0]);
2519 buf.extend_from_slice(&inner[IP6_HEADER_LEN..]);
2520 buf
2521 }
2522
2523 /// An 8-byte UDP header carrying `dst_port`, as a first fragment's `rest`.
2524 fn udp_header(dst_port: u16) -> Vec<u8> {
2525 let mut hdr = vec![0u8; 8];
2526 hdr[0..2].copy_from_slice(&54276u16.to_be_bytes());
2527 hdr[2..4].copy_from_slice(&dst_port.to_be_bytes());
2528 hdr[4..6].copy_from_slice(&16u16.to_be_bytes());
2529 hdr
2530 }
2531
2532 /// The IPv6 Fragment extension-header classification, mirroring Go
2533 /// `net/packet.Parsed.decode6Fragment` plus the sub-protocol switch `decode6` runs when it
2534 /// reports `continueDecode` (upstream `4c4ec3d46`, clarified by `26b2ed0a6`). Cases are
2535 /// upstream's own `TestDecode` fixtures: `ipv6_frag_first`, `ipv6_frag_nonfirst`,
2536 /// `ipv6_frag_short_first` and `ipv6_frag_small_offset`.
2537 #[test]
2538 fn ipv6_fragment_classification_matches_go_decode6() {
2539 // `ipv6_frag_first`: offset 0 with MF set, and a whole UDP header behind the fragment
2540 // header — Go steps over the 8 bytes and reads the ports, so the ACL matches this datagram
2541 // on the same rule it would match unfragmented.
2542 assert_eq!(
2543 decode6_fragment(&ipv6_fragment_packet(17, 0, true, &udp_header(443))),
2544 Ipv6Fragment::First {
2545 proto: IpProto::UDP,
2546 // `udp_header` writes 54276 as its source port; Go reads `sub[0:2]` for it, and the
2547 // reverse-flow cache is the only thing that consults it.
2548 src_port: 54276,
2549 dst_port: 443,
2550 // UDP is one of Go's port-ful arms and nothing else is read out of its header.
2551 l4: ts_packetfilter::L4Header::Unknown,
2552 },
2553 "a first fragment is decoded past the Fragment header, ports and all"
2554 );
2555
2556 // `ipv6_frag_nonfirst`: a later fragment at offset 185 blocks has no transport header at
2557 // all, so Go marks it `ipproto.Fragment` for `pre()` to pass through.
2558 assert_eq!(
2559 decode6_fragment(&ipv6_fragment_packet(17, 185, false, &[0x61; 8])),
2560 Ipv6Fragment::Later,
2561 "a later fragment at a safe offset classifies as a pass-through fragment"
2562 );
2563 // The floor itself is safe; one block below it is not. `MIN_FRAG_BLKS` is the IPv4-sized
2564 // bound upstream deliberately reuses for IPv6 (Go `26b2ed0a6`).
2565 assert_eq!(
2566 decode6_fragment(&ipv6_fragment_packet(17, MIN_FRAG_BLKS, false, &[0x61; 8])),
2567 Ipv6Fragment::Later,
2568 "offset == MIN_FRAG_BLKS is the first accepted later fragment"
2569 );
2570
2571 // `ipv6_frag_small_offset`: a later fragment whose bytes could land on top of the transport
2572 // header the head fragment was matched on — RFC 1858. Go rejects it as `unknown`.
2573 assert_eq!(
2574 decode6_fragment(&ipv6_fragment_packet(17, 1, false, &[0x61; 8])),
2575 Ipv6Fragment::Unknown,
2576 "a later fragment at offset 1 block is rejected (RFC 1858)"
2577 );
2578 assert_eq!(
2579 decode6_fragment(&ipv6_fragment_packet(
2580 17,
2581 MIN_FRAG_BLKS - 1,
2582 false,
2583 &[0x61; 8]
2584 )),
2585 Ipv6Fragment::Unknown,
2586 "one block below the floor is still rejected (RFC 1858)"
2587 );
2588
2589 // `ipv6_frag_short_first`: a first fragment truncated before its full transport header. Go
2590 // refuses to guess at the ports, because a follow-up fragment supplying the rest of that
2591 // header would otherwise carry the flow past a rule the filter never really matched.
2592 assert_eq!(
2593 decode6_fragment(&ipv6_fragment_packet(17, 0, true, &udp_header(443)[..4])),
2594 Ipv6Fragment::Unknown,
2595 "a first fragment with only half a UDP header is rejected"
2596 );
2597 assert_eq!(
2598 decode6_fragment(&ipv6_fragment_packet(6, 0, true, &[0u8; 19])),
2599 Ipv6Fragment::Unknown,
2600 "a first fragment one byte short of a TCP header is rejected"
2601 );
2602 // ...and the same header one byte longer is accepted, so the rejection is the bounds check
2603 // and not the protocol.
2604 let mut tcp = vec![0u8; 20];
2605 tcp[2..4].copy_from_slice(&443u16.to_be_bytes());
2606 assert_eq!(
2607 decode6_fragment(&ipv6_fragment_packet(6, 0, true, &tcp)),
2608 Ipv6Fragment::First {
2609 proto: IpProto::TCP,
2610 src_port: 0,
2611 dst_port: 443,
2612 // Go `q.TCPFlags = TCPFlag(sub[13])`, which is 0 in this all-zero header. A
2613 // fragment carrying real flags is exercised by
2614 // `ipv6_first_fragment_carries_its_tcp_flags_to_the_reply_carve_out`.
2615 l4: ts_packetfilter::L4Header::Tcp { flags: 0 },
2616 },
2617 "a complete TCP header in the first fragment is read normally"
2618 );
2619
2620 // A Fragment header truncated by the packet itself (Go's `len(b) < q.subofs+8` guard).
2621 let mut short = ipv6_fragment_packet(17, 0, true, &[]);
2622 short.truncate(IP6_HEADER_LEN + 4);
2623 short[4..6].copy_from_slice(&4u16.to_be_bytes());
2624 assert_eq!(
2625 decode6_fragment(&short),
2626 Ipv6Fragment::Unknown,
2627 "a truncated Fragment extension header is rejected"
2628 );
2629 // A packet cut off before its declared payload length (Go `len(b) < q.length`).
2630 let mut cut = ipv6_fragment_packet(17, 0, true, &udp_header(443));
2631 cut.truncate(cut.len() - 1);
2632 assert_eq!(
2633 decode6_fragment(&cut),
2634 Ipv6Fragment::Unknown,
2635 "a packet cut off before its declared IPv6 length is rejected"
2636 );
2637
2638 // Go's portless arms bounds-check but leave the port at 0, and the on-the-wire use of Go's
2639 // internal `ipproto.Fragment` sentinel (0xff) maps back to `unknown`.
2640 assert_eq!(
2641 decode6_fragment(&ipv6_fragment_packet(58, 0, true, &[0u8; 4])),
2642 Ipv6Fragment::First {
2643 proto: IpProto::ICMPV6,
2644 src_port: 0,
2645 dst_port: 0,
2646 // Go bounds-checks 4 bytes here but reads the type/code behind `len(q.b) >=
2647 // q.subofs+8`, so a 4-byte first fragment is no "response" upstream either.
2648 l4: ts_packetfilter::L4Header::Unknown,
2649 },
2650 "a first ICMPv6 fragment keeps port 0 and is matched IPs-only"
2651 );
2652 assert_eq!(
2653 decode6_fragment(&ipv6_fragment_packet(58, 0, true, &[0u8; 3])),
2654 Ipv6Fragment::Unknown,
2655 "a first ICMPv6 fragment shorter than the ICMPv6 header is rejected"
2656 );
2657 assert_eq!(
2658 decode6_fragment(&ipv6_fragment_packet(0xff, 0, true, &[0u8; 8])),
2659 Ipv6Fragment::Unknown,
2660 "Go's internal Fragment sentinel seen on the wire maps back to unknown"
2661 );
2662 }
2663
2664 /// The verdict Go's filter `pre()` reaches for each IPv6 fragment classification, asserted
2665 /// against an ACL that would otherwise decide the packet the other way — so each assertion can
2666 /// only be the fragment rule, never the ACL:
2667 ///
2668 /// - `Unknown` is DROPPED under an ALLOW-ALL ACL (Go `pre()`: `IPProto == Unknown → Drop`).
2669 /// This is the security-relevant direction: an allow-all tailnet policy must not admit a
2670 /// short-first or RFC-1858 low-offset fragment.
2671 /// - `Later` is ACCEPTED under a DENY-ALL ACL (Go `pre()`: `case ipproto.Fragment: Accept`).
2672 /// - `First` consults the ACL normally on the port read past the Fragment header.
2673 #[test]
2674 fn ipv6_fragment_verdict_matches_go_pre() {
2675 let src = std::net::IpAddr::V6(IPV6_FIXTURE_SRC);
2676 let dst = std::net::IpAddr::V6(IPV6_FIXTURE_DST);
2677 let v6 = |class| Some(Fragment::V6(class));
2678
2679 // The negative case, stated explicitly: allow-all cannot rescue an `unknown` fragment.
2680 assert!(
2681 !stateless_verdict(
2682 &AllowAll,
2683 IpProto::new(0),
2684 src,
2685 dst,
2686 0,
2687 v6(Ipv6Fragment::Unknown)
2688 ),
2689 "an unknown IPv6 fragment is dropped even under an allow-all ACL"
2690 );
2691 // The control: the same allow-all ACL admits an ordinary non-fragment packet, so the drop
2692 // above is the classification and not the harness.
2693 assert!(
2694 stateless_verdict(&AllowAll, IpProto::UDP, src, dst, 443, None),
2695 "the allow-all ACL does admit an ordinary packet"
2696 );
2697
2698 // A safe later fragment slides through ahead of the ACL, with nothing but port 0 to match.
2699 assert!(
2700 stateless_verdict(
2701 &DenyAll,
2702 IpProto::new(0),
2703 src,
2704 dst,
2705 0,
2706 v6(Ipv6Fragment::Later)
2707 ),
2708 "a later IPv6 fragment is accepted ahead of a deny-all ACL"
2709 );
2710
2711 // A first fragment is an ordinary packet again: admitted on the port the ACL allows,
2712 // dropped on one it does not.
2713 let first = |dst_port| {
2714 v6(Ipv6Fragment::First {
2715 proto: IpProto::UDP,
2716 src_port: 0,
2717 dst_port,
2718 l4: ts_packetfilter::L4Header::Unknown,
2719 })
2720 };
2721 assert!(
2722 stateless_verdict(&AllowPort(443), IpProto::UDP, src, dst, 443, first(443)),
2723 "a first IPv6 fragment is matched on the port behind the Fragment header"
2724 );
2725 assert!(
2726 !stateless_verdict(&AllowPort(443), IpProto::UDP, src, dst, 444, first(444)),
2727 "a first IPv6 fragment on a disallowed port is dropped by the ACL"
2728 );
2729 // Control: the same ACL decides an unfragmented packet the same way, so the two results
2730 // above are the ACL being consulted on a real port and not a fragment-specific shortcut.
2731 assert!(
2732 stateless_verdict(&AllowPort(443), IpProto::UDP, src, dst, 443, None),
2733 "control: the port-scoped ACL admits an unfragmented packet to 443"
2734 );
2735 assert!(
2736 !stateless_verdict(&AllowPort(443), IpProto::UDP, src, dst, 0, None),
2737 "control: port 0 - what a v6 fragment used to read as - is not admitted"
2738 );
2739
2740 // `pre()`'s multicast/link-local drops still outrank the fragment pass-through, exactly as
2741 // Go runs them before `case ipproto.Fragment`.
2742 assert!(
2743 !stateless_verdict(
2744 &AllowAll,
2745 IpProto::new(0),
2746 src,
2747 "ff02::1".parse().unwrap(),
2748 0,
2749 v6(Ipv6Fragment::Later)
2750 ),
2751 "a later fragment to a multicast dst is still dropped by pre()"
2752 );
2753 assert!(
2754 !stateless_verdict(
2755 &AllowAll,
2756 IpProto::new(0),
2757 src,
2758 "fe80::1".parse().unwrap(),
2759 0,
2760 v6(Ipv6Fragment::Later)
2761 ),
2762 "a later fragment to a link-local dst is still dropped by pre()"
2763 );
2764 }
2765
2766 /// The whole inbound path on real IPv6 bytes — parse, classify, verdict — which is the shape
2767 /// the bypass had: before the Fragment extension header was classified, every source-fragmented
2768 /// IPv6 datagram reached the ACL with no sub-protocol and port 0, so an allow-all rule admitted
2769 /// the RFC 1858 fragments upstream drops and a port-scoped rule blackholed the later fragments
2770 /// upstream passes through.
2771 #[test]
2772 fn ipv6_fragments_are_filtered_end_to_end() {
2773 let keep = |filter: &(dyn ts_packetfilter::Filter + Send + Sync), packet: Vec<u8>| {
2774 let mut packets = vec![PacketMut::from(packet)];
2775 let mut harvest = InboundHarvest::default();
2776 filter_inbound_from_peer(
2777 filter,
2778 &mut flowtrack::FlowCache::default(),
2779 PeerId(3),
2780 &mut packets,
2781 RejectConfig::default(),
2782 &mut harvest,
2783 );
2784 assert!(
2785 harvest.learned_disco_keys.is_empty(),
2786 "no TSMP advertisement in these fixtures"
2787 );
2788 !packets.is_empty()
2789 };
2790
2791 // Under an ALLOW-ALL ACL — the permissive policy the bypass needs — the RFC 1858 fragment
2792 // must still be dropped, while the legitimate later fragment must still be delivered.
2793 assert!(
2794 !keep(&AllowAll, ipv6_fragment_packet(17, 1, false, &[0x61; 8])),
2795 "a low-offset later IPv6 fragment is dropped even by an allow-all ACL (RFC 1858)"
2796 );
2797 assert!(
2798 !keep(
2799 &AllowAll,
2800 ipv6_fragment_packet(17, 0, true, &udp_header(443)[..4])
2801 ),
2802 "a first IPv6 fragment too short to hold its UDP header is dropped by an allow-all ACL"
2803 );
2804 assert!(
2805 keep(&AllowAll, ipv6_fragment_packet(17, 185, false, &[0x61; 8])),
2806 "a legitimate later IPv6 fragment is delivered"
2807 );
2808
2809 // ...and the later fragment is delivered even under a DENY-ALL ACL, which is the Go
2810 // `pre()` pass-through and not the ACL agreeing.
2811 assert!(
2812 keep(&DenyAll, ipv6_fragment_packet(17, 185, false, &[0x61; 8])),
2813 "a legitimate later IPv6 fragment slides through a deny-all ACL (Go pre())"
2814 );
2815 assert!(
2816 !keep(&DenyAll, ipv6_fragment_packet(17, 1, false, &[0x61; 8])),
2817 "a low-offset later IPv6 fragment is dropped under a deny-all ACL too"
2818 );
2819
2820 // A first fragment is matched on the port that lives behind the Fragment extension header,
2821 // which is the whole point of stepping over it: 443 is admitted, 444 is not, under the same
2822 // port-scoped ACL. Before the port was read past the header both read as port 0 and both
2823 // were dropped.
2824 assert!(
2825 keep(
2826 &AllowPort(443),
2827 ipv6_fragment_packet(17, 0, true, &udp_header(443))
2828 ),
2829 "a first IPv6 fragment to an allowed port is delivered"
2830 );
2831 assert!(
2832 !keep(
2833 &AllowPort(443),
2834 ipv6_fragment_packet(17, 0, true, &udp_header(444))
2835 ),
2836 "a first IPv6 fragment to a disallowed port is dropped"
2837 );
2838
2839 // Scoping (Go `26b2ed0a6`): the Fragment header is parsed here ONLY as the base header's
2840 // immediate Next Header. What happens to one reached through a chained extension header —
2841 // it must fail closed, not fall through to the ACL — is
2842 // `chained_extension_header_cannot_bypass_the_ipv6_fragment_rules`.
2843 }
2844
2845 /// An allow-all ACL that also records whether it was consulted at all.
2846 ///
2847 /// [`AllowAll`] alone can show that a packet was dropped; it cannot show *where*. Under an ACL
2848 /// that admits everything, "dropped AND never consulted" is the signature of a `pre()` drop and
2849 /// of nothing else — which is the guarantee the fragment classification exists to keep, so it
2850 /// is worth asserting directly rather than inferring from the verdict.
2851 #[derive(Default)]
2852 struct RecordingAllowAll(std::sync::atomic::AtomicBool);
2853
2854 impl RecordingAllowAll {
2855 /// Whether the ACL was asked about any packet since this filter was made.
2856 fn consulted(&self) -> bool {
2857 self.0.load(std::sync::atomic::Ordering::Relaxed)
2858 }
2859 }
2860
2861 impl ts_packetfilter::Filter for RecordingAllowAll {
2862 fn match_for(
2863 &self,
2864 _info: &ts_packetfilter::PacketInfo,
2865 _caps: ts_packetfilter::filter::CapIter,
2866 ) -> Option<&str> {
2867 self.0.store(true, std::sync::atomic::Ordering::Relaxed);
2868 Some("allow-all")
2869 }
2870 }
2871
2872 /// A *first* IPv6 fragment whose Fragment header's Next Header is 0 is dropped ahead of the
2873 /// rules, never matched by them.
2874 ///
2875 /// Go `net/packet.decode6Fragment` copies that byte into `q.IPProto` (`q.IPProto = nextHdr`)
2876 /// and reports `continueDecode`, so the packet goes back through `decode6`'s sub-protocol
2877 /// switch — which has no case for 0. `ipproto.Unknown` *is* 0, so `q.IPProto` is left at
2878 /// Unknown and filter `pre()`'s `if q.IPProto == ipproto.Unknown { return Drop }` fires before
2879 /// any rule is consulted. (Protocol 0 on the wire is Hop-by-Hop Options; an IPv6 packet whose
2880 /// *base* header declares it is refused by that same arm, and always has been.)
2881 ///
2882 /// This tree reaches the same drop by the same route: `decode6_first_fragment`'s catch-all arm
2883 /// keeps the number, exactly as Go's absent switch case does, and the drop comes from
2884 /// `inbound_filter_verdict`'s shared [`IPPROTO_UNKNOWN`] arm — which a first fragment falls
2885 /// through to for the same reason an unfragmented packet does. Nothing about that is specific
2886 /// to fragments, which is why there is no fragment-specific arm for it; this test is what pins
2887 /// the fall-through, at each of the three levels the packet passes through.
2888 #[test]
2889 fn first_ipv6_fragment_with_unknown_next_header_is_dropped_before_the_acl() {
2890 // 1. Classification. Go's `q.IPProto = nextHdr` on a first fragment, verbatim: the 0 is
2891 // carried, not translated. `dst_port` is 0 because Go reads a port in the TCP/UDP/SCTP
2892 // arms only, and protocol 0 is in none of them.
2893 assert_eq!(
2894 decode6_fragment(&ipv6_fragment_packet(0, 0, true, &udp_header(443))),
2895 Ipv6Fragment::First {
2896 proto: IPPROTO_UNKNOWN,
2897 src_port: 0,
2898 dst_port: 0,
2899 l4: ts_packetfilter::L4Header::Unknown,
2900 },
2901 "a first fragment carries its Fragment header's Next Header, 0 included"
2902 );
2903
2904 // 2. Verdict. The allow-all ACL is the control that makes this a pre-rule drop and not a
2905 // rule saying no.
2906 let src = std::net::IpAddr::V6(IPV6_FIXTURE_SRC);
2907 let dst = std::net::IpAddr::V6(IPV6_FIXTURE_DST);
2908 assert!(
2909 !stateless_verdict(
2910 &AllowAll,
2911 IPPROTO_UNKNOWN,
2912 src,
2913 dst,
2914 0,
2915 Some(Fragment::V6(Ipv6Fragment::First {
2916 proto: IPPROTO_UNKNOWN,
2917 src_port: 0,
2918 dst_port: 0,
2919 l4: ts_packetfilter::L4Header::Unknown,
2920 })),
2921 ),
2922 "a first IPv6 fragment declaring protocol 0 is dropped under an allow-all ACL"
2923 );
2924
2925 // 3. The whole inbound path on real bytes, and the part the ACL never sees. The two
2926 // fixtures differ in exactly one byte — the Fragment header's Next Header — so the
2927 // control proves the drop is the protocol number and not the packet shape: the same
2928 // fragment naming UDP is parsed, matched and delivered.
2929 let keep = |filter: &(dyn ts_packetfilter::Filter + Send + Sync), packet: Vec<u8>| {
2930 let mut packets = vec![PacketMut::from(packet)];
2931 let mut harvest = InboundHarvest::default();
2932 filter_inbound_from_peer(
2933 filter,
2934 &mut flowtrack::FlowCache::default(),
2935 PeerId(5),
2936 &mut packets,
2937 RejectConfig::default(),
2938 &mut harvest,
2939 );
2940 assert!(
2941 harvest.learned_disco_keys.is_empty(),
2942 "no TSMP advertisement in these fixtures"
2943 );
2944 !packets.is_empty()
2945 };
2946
2947 let acl = RecordingAllowAll::default();
2948 assert!(
2949 !keep(&acl, ipv6_fragment_packet(0, 0, true, &udp_header(443))),
2950 "a crafted first IPv6 fragment naming protocol 0 is dropped by an allow-all ACL"
2951 );
2952 assert!(
2953 !acl.consulted(),
2954 "and it is dropped ahead of the rules: the ACL is never asked about it"
2955 );
2956
2957 let control = RecordingAllowAll::default();
2958 assert!(
2959 keep(
2960 &control,
2961 ipv6_fragment_packet(17, 0, true, &udp_header(443))
2962 ),
2963 "control: the same fragment naming UDP is delivered"
2964 );
2965 assert!(
2966 control.consulted(),
2967 "control: and it got there by being matched against the rules"
2968 );
2969 }
2970
2971 /// Prepending an extension header must not defeat the fragment rules.
2972 ///
2973 /// [`decode6_fragment`] is scoped exactly as Go scopes it: the Fragment header is parsed only
2974 /// as the base header's immediate Next Header. Go can afford that narrow scope because
2975 /// everything it does not parse *keeps the base header's Next Header* as `q.IPProto`, so a
2976 /// chained fragment is filtered as the extension header it leads with and its fragment offset
2977 /// is never read at all. This tree does classify the chain
2978 /// ([`fragment_header_is_chained`]), and the only classification that cannot be an invention
2979 /// in the permissive direction is [`Ipv6Fragment::Unknown`] — a drop. Without it, eight bytes
2980 /// of Hop-by-Hop Options were enough to walk every RFC 1858 fragment straight past the rules
2981 /// the rest of this file exists to enforce.
2982 ///
2983 /// Every assertion is against an ALLOW-ALL ACL, so a drop can only be the fragment rule and
2984 /// never the ACL — and each extension type carries its own control that proves it: the same
2985 /// chain shape with no Fragment header in it is still walked to its UDP header by the parser.
2986 /// That control is per-type rather than once at the end because `keep` cannot tell a
2987 /// fragment-rule drop from a parser rejection, so a fixture malformed for only one of the
2988 /// three protocols would otherwise turn that protocol's four drops into vacuous passes with
2989 /// the suite still green.
2990 #[test]
2991 fn chained_extension_header_cannot_bypass_the_ipv6_fragment_rules() {
2992 let keep = |filter: &(dyn ts_packetfilter::Filter + Send + Sync), packet: Vec<u8>| {
2993 let mut packets = vec![PacketMut::from(packet)];
2994 let mut harvest = InboundHarvest::default();
2995 filter_inbound_from_peer(
2996 filter,
2997 &mut flowtrack::FlowCache::default(),
2998 PeerId(4),
2999 &mut packets,
3000 RejectConfig::default(),
3001 &mut harvest,
3002 );
3003 assert!(
3004 harvest.learned_disco_keys.is_empty(),
3005 "no TSMP advertisement in these fixtures"
3006 );
3007 !packets.is_empty()
3008 };
3009
3010 // Hop-by-Hop Options (0), Routing (43) and Destination Options (60): the fragment rules
3011 // must not depend on which header the sender chose to hide behind.
3012 for ext in [0u8, 43, 60] {
3013 // The RFC 1858 evasion itself: a later fragment whose bytes can land on top of the
3014 // transport header the head fragment was matched on.
3015 assert!(
3016 !keep(
3017 &AllowAll,
3018 ipv6_with_prepended_ext_header(
3019 ext,
3020 &ipv6_fragment_packet(17, 1, false, &[0x61; 8])
3021 )
3022 ),
3023 "a low-offset later fragment behind extension header {ext} is dropped (RFC 1858)"
3024 );
3025 // A first fragment truncated before its own transport header, which a follow-up
3026 // fragment can then complete.
3027 assert!(
3028 !keep(
3029 &AllowAll,
3030 ipv6_with_prepended_ext_header(
3031 ext,
3032 &ipv6_fragment_packet(17, 0, true, &udp_header(443)[..4])
3033 )
3034 ),
3035 "a short first fragment behind extension header {ext} is dropped"
3036 );
3037 // A *well-formed* chained fragment is dropped too — Go drops this whole class, so
3038 // failing closed here can never admit something upstream refuses.
3039 assert!(
3040 !keep(
3041 &AllowAll,
3042 ipv6_with_prepended_ext_header(
3043 ext,
3044 &ipv6_fragment_packet(17, 185, false, &[0x61; 8])
3045 )
3046 ),
3047 "a chained later fragment behind extension header {ext} gets no pass-through"
3048 );
3049 assert!(
3050 !keep(
3051 &AllowAll,
3052 ipv6_with_prepended_ext_header(
3053 ext,
3054 &ipv6_fragment_packet(17, 0, true, &udp_header(443))
3055 )
3056 ),
3057 "a chained first fragment behind extension header {ext} is dropped"
3058 );
3059
3060 // Control for THIS extension type. Every assertion above is a `!keep`, and `keep`
3061 // reports a packet the parser rejected exactly as it reports a packet the fragment
3062 // rule dropped — so on its own the block above would also pass if this builder simply
3063 // produced eight bytes etherparse refuses to walk. It does not: the same chain shape
3064 // with no Fragment header behind it is walked all the way to its UDP header. The drops
3065 // above are therefore this file refusing a packet it could perfectly well have read,
3066 // which is the whole claim.
3067 //
3068 // The control is a parser assertion and not a `keep`, because what the *filter* does
3069 // with a chained non-fragment is no longer "deliver it on the port behind the chain" —
3070 // it is Go's base-Next-Header disposition, which
3071 // `ipv6_extension_header_chain_is_matched_on_the_base_next_header` covers in full.
3072 let plain = ipv6_with_prepended_ext_header(ext, &ipv6_udp_packet(&udp_header(443)));
3073 let parsed = etherparse::SlicedPacket::from_ip(&plain)
3074 .unwrap_or_else(|e| panic!("extension header {ext} fixture must parse: {e:?}"));
3075 assert!(
3076 matches!(parsed.transport, Some(etherparse::TransportSlice::Udp(_))),
3077 "extension header {ext} fixture must chain to a UDP header the parser can reach"
3078 );
3079 }
3080
3081 // Contrast: the very same later fragment, reached as the base header's immediate Next
3082 // Header, is still delivered. Only the 8 prepended bytes separate this from the third
3083 // assertion above, so the drops really are the chain and not the fragment fixtures.
3084 assert!(
3085 keep(&AllowAll, ipv6_fragment_packet(17, 185, false, &[0x61; 8])),
3086 "an unchained later fragment is still delivered"
3087 );
3088 }
3089
3090 /// A filter that admits everything and records the [`ts_packetfilter::PacketInfo`] it was asked
3091 /// about, so a test can assert on the protocol and port the dataplane actually derived — and on
3092 /// a packet never reaching the ACL at all.
3093 #[derive(Default)]
3094 struct Recording(Mutex<Vec<ts_packetfilter::PacketInfo>>);
3095 impl ts_packetfilter::Filter for Recording {
3096 fn match_for(
3097 &self,
3098 info: &ts_packetfilter::PacketInfo,
3099 _caps: ts_packetfilter::filter::CapIter,
3100 ) -> Option<&str> {
3101 self.0.lock().unwrap().push(*info);
3102 Some("recording")
3103 }
3104 }
3105
3106 /// A real control-derived ACL — one rule built out of [`ts_packetfilter::Rule`] itself rather
3107 /// than a hand-written stub, so the assertions run through the same per-protocol port semantics
3108 /// as production: TCP/UDP/SCTP are port-matched, and any other protocol matches IPs-only and
3109 /// only under an all-ports rule (Go `matchProtoAndIPsOnlyIfAllPorts`).
3110 fn ipv6_acl(
3111 protos: &[i64],
3112 ports: std::ops::RangeInclusive<u16>,
3113 ) -> std::collections::BTreeMap<String, ts_packetfilter::Ruleset> {
3114 acl("2001:db8::/32", protos, ports)
3115 }
3116
3117 /// [`ipv6_acl`] for either family: one rule whose source and destination are both `net`.
3118 fn acl(
3119 net: &str,
3120 protos: &[i64],
3121 ports: std::ops::RangeInclusive<u16>,
3122 ) -> std::collections::BTreeMap<String, ts_packetfilter::Ruleset> {
3123 let net: ipnet::IpNet = net.parse().unwrap();
3124 std::collections::BTreeMap::from([(
3125 ts_packetfilter::DEFAULT_RULESET_NAME.to_string(),
3126 vec![ts_packetfilter::Rule {
3127 src: ts_packetfilter::SrcMatch {
3128 pfxs: vec![net],
3129 caps: Vec::new(),
3130 },
3131 protos: protos.iter().copied().map(IpProto::new).collect(),
3132 dst: vec![ts_packetfilter::DstMatch {
3133 ports,
3134 ips: vec![net],
3135 }],
3136 }],
3137 )])
3138 }
3139
3140 /// An IPv6 extension-header chain is filtered on the **base** header's Next Header, never on
3141 /// the transport the chain resolves to.
3142 ///
3143 /// Go `net/packet.decode6` assigns `q.IPProto = ipproto.Proto(b[6])` and — apart from a leading
3144 /// Fragment header — parses nothing further, so the number that reaches `wgengine/filter` is
3145 /// the extension header's own. Two consequences, both asserted here:
3146 ///
3147 /// * Hop-by-Hop Options **is** protocol 0, which is `ipproto.Unknown`, so filter `pre()` drops
3148 /// the packet outright before any rule is consulted.
3149 /// * Routing (43) and Destination Options (60) reach `runIn6`'s `default` arm, where the only
3150 /// way in is `matchProtoAndIPsOnlyIfAllPorts` — an all-ports rule naming protocol 43 or 60.
3151 /// Such a packet is never matched against a TCP or UDP rule and its transport port is never
3152 /// read.
3153 ///
3154 /// Reading the protocol out of etherparse's extension-header walk instead resolves straight
3155 /// through the chain to the real transport number and reads that transport's destination port,
3156 /// which is a strictly more permissive filter than upstream's: an ordinary `udp:443` ACL
3157 /// admitted a packet Go matches IPs-only, and would admit it for any protocol an attacker
3158 /// chose to bury the chain under.
3159 ///
3160 /// Ported from github.com/tailscale/tailscale `net/packet/packet.go` (`decode6`) and
3161 /// `wgengine/filter/filter.go` (`pre`, `runIn6`) at
3162 /// `9ea7cba44591e0cd840c6c94d23274dd222059bf`.
3163 #[test]
3164 fn ipv6_extension_header_chain_is_matched_on_the_base_next_header() {
3165 let keep = |filter: &(dyn ts_packetfilter::Filter + Send + Sync), packet: Vec<u8>| {
3166 let mut packets = vec![PacketMut::from(packet)];
3167 let mut harvest = InboundHarvest::default();
3168 filter_inbound_from_peer(
3169 filter,
3170 &mut flowtrack::FlowCache::default(),
3171 PeerId(5),
3172 &mut packets,
3173 RejectConfig::default(),
3174 &mut harvest,
3175 );
3176 assert!(
3177 harvest.learned_disco_keys.is_empty(),
3178 "no TSMP advertisement in these fixtures"
3179 );
3180 !packets.is_empty()
3181 };
3182 // What the ACL was asked about, or `None` if the packet never got that far.
3183 let seen = |packet: Vec<u8>| {
3184 let recording = Recording::default();
3185 keep(&recording, packet);
3186 let seen = recording.0.into_inner().unwrap();
3187 assert!(seen.len() <= 1, "one packet in, at most one ACL question");
3188 seen.into_iter().next()
3189 };
3190
3191 let unchained = ipv6_udp_packet(&udp_header(443));
3192
3193 // The baseline this is all measured against: with UDP as the base header's Next Header,
3194 // `decode6` takes its UDP arm, so the ACL sees protocol 17 on port 443.
3195 let info = seen(unchained.clone()).expect("an unchained UDP datagram reaches the ACL");
3196 assert_eq!(info.ip_proto, IpProto::UDP, "unchained: protocol is UDP");
3197 assert_eq!(
3198 info.port, 443,
3199 "unchained: the UDP destination port is read"
3200 );
3201
3202 // Routing (43) and Destination Options (60): the base header now says "extension header",
3203 // so that is the protocol the ACL is asked about — and no port is read, even though the
3204 // very same UDP header still sits 8 bytes further down the chain.
3205 for ext in [43u8, 60] {
3206 let chained = ipv6_with_prepended_ext_header(ext, &unchained);
3207 let info = seen(chained.clone()).unwrap_or_else(|| {
3208 panic!("a packet behind extension header {ext} reaches the ACL")
3209 });
3210 assert_eq!(
3211 info.ip_proto,
3212 IpProto::new(i64::from(ext)),
3213 "behind extension header {ext}: the ACL sees the base Next Header, not the transport"
3214 );
3215 assert_eq!(
3216 info.port, 0,
3217 "behind extension header {ext}: no port is read past the chain"
3218 );
3219
3220 // And what that means for a real ACL. An ordinary `udp:443` rule admits the unchained
3221 // datagram and refuses the chained one, because protocol 43/60 is not UDP...
3222 let udp443 = ipv6_acl(&[i64::from(IpProto::UDP)], 443..=443);
3223 assert!(
3224 keep(&udp443, unchained.clone()),
3225 "a udp:443 rule admits the unchained datagram"
3226 );
3227 assert!(
3228 !keep(&udp443, chained.clone()),
3229 "a udp:443 rule does not admit a packet behind extension header {ext}"
3230 );
3231
3232 // ...and the one rule that does admit it is Go's `matchProtoAndIPsOnlyIfAllPorts`:
3233 // the protocol named, IPs-only, all ports open. A narrower port range on the same
3234 // protocol opens nothing, because a portless protocol carries no port to match.
3235 assert!(
3236 keep(&ipv6_acl(&[i64::from(ext)], 0..=u16::MAX), chained.clone()),
3237 "an all-ports rule naming protocol {ext} admits it IPs-only"
3238 );
3239 assert!(
3240 !keep(&ipv6_acl(&[i64::from(ext)], 443..=443), chained),
3241 "a port-scoped rule naming protocol {ext} opens nothing (matchProtoAndIPsOnlyIfAllPorts)"
3242 );
3243 }
3244
3245 // Hop-by-Hop Options is protocol 0, and protocol 0 is `ipproto.Unknown`: Go's `pre()`
3246 // drops it before the ACL exists, so not even an allow-everything filter is consulted.
3247 let hop_by_hop = ipv6_with_prepended_ext_header(0, &unchained);
3248 assert!(
3249 seen(hop_by_hop.clone()).is_none(),
3250 "a hop-by-hop-led packet never reaches the ACL"
3251 );
3252 assert!(
3253 !keep(&AllowAll, hop_by_hop),
3254 "a hop-by-hop-led packet is dropped by an allow-all ACL (Go pre() unknown-proto drop)"
3255 );
3256
3257 // The same drop for Go's internal later-fragment sentinel used as a real Next Header:
3258 // `decode6`'s `case ipproto.Fragment: q.IPProto = unknown`.
3259 let mut sentinel = unchained.clone();
3260 sentinel[6] = 0xff;
3261 assert!(
3262 seen(sentinel.clone()).is_none(),
3263 "a packet whose base Next Header is the 0xff sentinel never reaches the ACL"
3264 );
3265 assert!(
3266 !keep(&AllowAll, sentinel),
3267 "...and is dropped by an allow-all ACL"
3268 );
3269 }
3270
3271 /// Source/destination for the IPv4 fixtures: ordinary tailnet unicast, so `drop_before_rules`
3272 /// never fires and every verdict below is the decode's own.
3273 const IPV4_FIXTURE_SRC: std::net::Ipv4Addr = std::net::Ipv4Addr::new(100, 64, 0, 9);
3274 const IPV4_FIXTURE_DST: std::net::Ipv4Addr = std::net::Ipv4Addr::new(100, 64, 0, 1);
3275 /// The tailnet range both IPv4 fixture addresses sit in, for [`acl`].
3276 const IPV4_FIXTURE_NET: &str = "100.64.0.0/10";
3277
3278 /// A minimal IPv4 packet: a 20-byte header carrying protocol `proto`, the fragment offset (in
3279 /// 8-byte blocks) and More-Fragments flag asked for, and `payload` behind it. The header
3280 /// checksum is left zero — nothing on this path verifies it, and neither does Go's decoder.
3281 fn v4_packet(proto: u8, offset_blocks: u16, more_fragments: bool, payload: &[u8]) -> Vec<u8> {
3282 let total_len = u16::try_from(IP4_HEADER_LEN + payload.len()).unwrap();
3283 let mut buf = vec![0u8; usize::from(total_len)];
3284 buf[0] = 0x45; // version 4, IHL 5 (no options)
3285 buf[2..4].copy_from_slice(&total_len.to_be_bytes());
3286 let frag_field = (offset_blocks & 0x1fff) | if more_fragments { 0x2000 } else { 0 };
3287 buf[6..8].copy_from_slice(&frag_field.to_be_bytes());
3288 buf[8] = 64; // TTL
3289 buf[9] = proto;
3290 buf[12..16].copy_from_slice(&IPV4_FIXTURE_SRC.octets());
3291 buf[16..20].copy_from_slice(&IPV4_FIXTURE_DST.octets());
3292 buf[IP4_HEADER_LEN..].copy_from_slice(payload);
3293 buf
3294 }
3295
3296 /// An SCTP common header carrying `dst_port`, truncated to `len` bytes so a test can hand the
3297 /// decoder the short header Go refuses.
3298 fn sctp_header(dst_port: u16, len: usize) -> Vec<u8> {
3299 let mut hdr = vec![0u8; SCTP_HEADER_LEN];
3300 hdr[0..2].copy_from_slice(&54276u16.to_be_bytes()); // source port
3301 hdr[2..4].copy_from_slice(&dst_port.to_be_bytes());
3302 hdr[4..8].copy_from_slice(&[0xde, 0xad, 0xbe, 0xef]); // verification tag
3303 hdr.truncate(len);
3304 hdr
3305 }
3306
3307 /// An SCTP packet is filtered on its real destination port, on both families — Go
3308 /// `net/packet.decode4` and `decode6` each carry a `case ipproto.SCTP` arm that bounds-checks
3309 /// the 12-byte common header and reads `sub[2:4]`, exactly as their TCP and UDP arms do.
3310 ///
3311 /// etherparse parses no SCTP header of its own (its `TransportSlice` has ICMPv4, ICMPv6, TCP
3312 /// and UDP arms and nothing else), so leaving the port to `SlicedPacket::transport` reported
3313 /// port 0 for every SCTP packet on the wire. That is wrong in both directions: an `sctp:443`
3314 /// rule blackholed the SCTP traffic it was written to admit, and any rule whose port range
3315 /// happens to contain 0 admitted SCTP to *every* port. Both are asserted below through a real
3316 /// control-derived rule, not just through the recorded `PacketInfo`.
3317 ///
3318 /// The refusals come with it. A header too short to hold the ports is Go's
3319 /// `q.IPProto = unknown`, which filter `pre()` drops before any rule is consulted — never a
3320 /// fallback to port 0, which an all-ports rule would admit. And a *later* fragment is not an
3321 /// SCTP header at all: Go leaves its ports 0 and passes it through on its offset alone.
3322 ///
3323 /// Ported from github.com/tailscale/tailscale `net/packet/packet.go` (`decode4`, `decode6`) and
3324 /// `wgengine/filter/filter.go` (`pre`, `runIn4`, `runIn6`) at
3325 /// `9ea7cba44591e0cd840c6c94d23274dd222059bf`.
3326 #[test]
3327 fn sctp_destination_port_is_read_before_the_acl() {
3328 let keep = |filter: &(dyn ts_packetfilter::Filter + Send + Sync), packet: Vec<u8>| {
3329 let mut packets = vec![PacketMut::from(packet)];
3330 let mut harvest = InboundHarvest::default();
3331 filter_inbound_from_peer(
3332 filter,
3333 &mut flowtrack::FlowCache::default(),
3334 PeerId(11),
3335 &mut packets,
3336 RejectConfig::default(),
3337 &mut harvest,
3338 );
3339 assert!(
3340 harvest.learned_disco_keys.is_empty(),
3341 "no TSMP advertisement in these fixtures"
3342 );
3343 !packets.is_empty()
3344 };
3345 // What the ACL was asked about, or `None` if the packet never got that far.
3346 let seen = |packet: Vec<u8>| {
3347 let recording = Recording::default();
3348 keep(&recording, packet);
3349 let seen = recording.0.into_inner().unwrap();
3350 assert!(seen.len() <= 1, "one packet in, at most one ACL question");
3351 seen.into_iter().next()
3352 };
3353
3354 let sctp = i64::from(IpProto::SCTP);
3355 let whole = sctp_header(443, SCTP_HEADER_LEN);
3356 let v4 = v4_packet(132, 0, false, &whole);
3357 let v6 = ipv6_packet(132, &whole);
3358
3359 for (family, packet) in [("IPv4", &v4), ("IPv6", &v6)] {
3360 let info = seen(packet.clone())
3361 .unwrap_or_else(|| panic!("{family}: an SCTP packet reaches the ACL"));
3362 assert_eq!(
3363 info.ip_proto,
3364 IpProto::SCTP,
3365 "{family}: the protocol is SCTP"
3366 );
3367 assert_eq!(
3368 info.port, 443,
3369 "{family}: the SCTP destination port is read off the wire"
3370 );
3371 }
3372
3373 // And what that means for a real control-derived rule. An `sctp:443` rule admits the
3374 // packet; a rule whose range covers port 0 but not 443 does not — the ACL bypass a
3375 // hard-coded port 0 would have opened.
3376 assert!(
3377 keep(&acl(IPV4_FIXTURE_NET, &[sctp], 443..=443), v4.clone()),
3378 "IPv4: an sctp:443 rule admits an SCTP packet to port 443"
3379 );
3380 assert!(
3381 !keep(&acl(IPV4_FIXTURE_NET, &[sctp], 0..=442), v4.clone()),
3382 "IPv4: an sctp:0-442 rule does not admit an SCTP packet to port 443"
3383 );
3384 assert!(
3385 keep(&ipv6_acl(&[sctp], 443..=443), v6.clone()),
3386 "IPv6: an sctp:443 rule admits an SCTP packet to port 443"
3387 );
3388 assert!(
3389 !keep(&ipv6_acl(&[sctp], 0..=442), v6),
3390 "IPv6: an sctp:0-442 rule does not admit an SCTP packet to port 443"
3391 );
3392
3393 // A *first* fragment carries the whole common header, so Go reads its ports like an
3394 // unfragmented packet's (`decode4` only skips the transport header when `fragOfs != 0`).
3395 let info = seen(v4_packet(132, 0, true, &whole))
3396 .expect("IPv4: a first SCTP fragment reaches the ACL");
3397 assert_eq!(
3398 info.port, 443,
3399 "IPv4: a first fragment's SCTP port is read, as decode4 does"
3400 );
3401
3402 // A later fragment is continued payload, not a header: Go leaves its ports 0 and `pre()`
3403 // passes it through on its offset alone. Reading `sub[2:4]` here would invent a port, and
3404 // the short-header refusal would drop a fragment upstream delivers — so a deny-all ACL is
3405 // the control, proving the accept came from the fragment path and not from a rule.
3406 assert!(
3407 keep(
3408 &DenyAll,
3409 v4_packet(132, MIN_FRAG_BLKS, false, &[0x01, 0x02, 0x03, 0x04])
3410 ),
3411 "IPv4: a valid later SCTP fragment is passed through ahead of the ACL"
3412 );
3413
3414 // Go's short-header refusal: `q.IPProto = unknown`, dropped by `pre()` before the ACL
3415 // exists, so not even an allow-everything filter is consulted.
3416 let short = sctp_header(443, SCTP_HEADER_LEN - 1);
3417 for (family, packet) in [
3418 ("IPv4", v4_packet(132, 0, false, &short)),
3419 ("IPv6", ipv6_packet(132, &short)),
3420 ] {
3421 assert!(
3422 seen(packet.clone()).is_none(),
3423 "{family}: an SCTP header too short to hold its ports never reaches the ACL"
3424 );
3425 assert!(
3426 !keep(&AllowAll, packet),
3427 "{family}: ...and an allow-all ACL does not admit it"
3428 );
3429 }
3430 }
3431
3432 /// Build the IPv4 packet a Go peer puts on the wire for a TSMP message: a 20-byte IPv4
3433 /// header with proto 99 and `body` appended (Go `packet.Generate(IP4Header{...}, body)`,
3434 /// which is what `TSMPDiscoKeyAdvertisement.Marshal` calls). The header checksum is left
3435 /// zero — nothing on this path verifies it, and neither does Go's decoder.
3436 fn tsmp_packet4(src: [u8; 4], dst: [u8; 4], body: &[u8]) -> PacketMut {
3437 let mut buf = vec![0u8; 20 + body.len()];
3438 buf[20..].copy_from_slice(body);
3439 buf[0] = 0x45;
3440 let total_len = buf.len() as u16;
3441 buf[2..4].copy_from_slice(&total_len.to_be_bytes());
3442 buf[8] = 64;
3443 buf[9] = 99;
3444 buf[12..16].copy_from_slice(&src);
3445 buf[16..20].copy_from_slice(&dst);
3446 PacketMut::from(buf)
3447 }
3448
3449 /// A body a real Go peer sends: `'a'` then its 32-byte disco key.
3450 fn advertisement_body(key: [u8; 32]) -> Vec<u8> {
3451 let mut body = vec![ts_packet::tsmp::TSMP_TYPE_DISCO_ADVERTISEMENT];
3452 body.extend_from_slice(&key);
3453 body
3454 }
3455
3456 /// The receive side of the TSMP disco-key advertisement, at the point Go handles it: a
3457 /// well-formed advertisement is CONSUMED — the peer's key is learned and the packet is
3458 /// dropped rather than delivered to the local stack (Go `filter.DropSilently`) — while every
3459 /// other TSMP body is left alone and still admitted by the TSMP ACL bypass.
3460 ///
3461 /// The ACL here denies everything, so an admitted packet can only have come through the
3462 /// TSMP bypass, and a learned key can only have come from the advertisement path.
3463 #[test]
3464 fn tsmp_disco_key_advertisement_is_learned_and_dropped() {
3465 let peer = PeerId(7);
3466 let src = [100, 64, 0, 2];
3467 let dst = [100, 64, 0, 1];
3468 let key = [0xa5u8; 32];
3469
3470 let mut packets = vec![tsmp_packet4(src, dst, &advertisement_body(key))];
3471 let mut harvest = InboundHarvest::default();
3472 filter_inbound_from_peer(
3473 &DenyAll,
3474 &mut flowtrack::FlowCache::default(),
3475 peer,
3476 &mut packets,
3477 RejectConfig::default(),
3478 &mut harvest,
3479 );
3480
3481 assert!(
3482 packets.is_empty(),
3483 "a consumed advertisement must not be delivered to the local stack"
3484 );
3485 let learned = &harvest.learned_disco_keys;
3486 assert_eq!(learned.len(), 1, "the advertisement must be harvested");
3487 assert_eq!(
3488 learned[0].0, peer,
3489 "attributed to the sending wireguard peer"
3490 );
3491 assert_eq!(learned[0].1.key, key, "the advertised disco key is learned");
3492 assert_eq!(learned[0].1.src, std::net::IpAddr::from(src));
3493
3494 // A TSMP message that is NOT an advertisement stays in the batch (Go leaves the types it
3495 // does not consume to the filter, which accepts TSMP) and teaches us nothing.
3496 let mut ping = vec![ts_packet::tsmp::TSMP_TYPE_PING];
3497 ping.extend_from_slice(&[1, 2, 3, 4, 5, 6, 7, 8]);
3498 let mut packets = vec![tsmp_packet4(src, dst, &ping)];
3499 let mut harvest = InboundHarvest::default();
3500 filter_inbound_from_peer(
3501 &DenyAll,
3502 &mut flowtrack::FlowCache::default(),
3503 peer,
3504 &mut packets,
3505 RejectConfig::default(),
3506 &mut harvest,
3507 );
3508 assert_eq!(packets.len(), 1, "a TSMP ping still bypasses the ACL");
3509 assert!(
3510 harvest.learned_disco_keys.is_empty(),
3511 "a ping advertises no disco key"
3512 );
3513 }
3514
3515 /// The negative case, at the dataplane boundary: a TSMP body that is *nearly* an
3516 /// advertisement must not be half-parsed into a learned key. None of these may put anything
3517 /// in the harvest — a truncated key that was zero-padded, or a zero key that was accepted,
3518 /// would be a wrong disco key bound to a real peer.
3519 #[test]
3520 fn malformed_tsmp_disco_key_advertisements_teach_nothing() {
3521 let peer = PeerId(7);
3522 let src = [100, 64, 0, 2];
3523 let dst = [100, 64, 0, 1];
3524
3525 // A truncated advertisement: the type byte and only 31 of 32 key bytes.
3526 let mut truncated = advertisement_body([0xa5u8; 32]);
3527 truncated.truncate(32);
3528
3529 for (name, body, still_delivered) in [
3530 ("truncated advertisement", truncated, true),
3531 (
3532 "unknown TSMP type byte",
3533 {
3534 let mut b = advertisement_body([0xa5u8; 32]);
3535 b[0] = b'Z';
3536 b
3537 },
3538 true,
3539 ),
3540 // A well-formed advertisement of the zero key: Go parses it but publishes only
3541 // `if !discoKeyAdvert.Key.IsZero()`, so it teaches nothing — and it is still a TSMP
3542 // message we consumed, so it is still dropped.
3543 (
3544 "zero-key advertisement",
3545 advertisement_body([0u8; 32]),
3546 false,
3547 ),
3548 ] {
3549 let mut packets = vec![tsmp_packet4(src, dst, &body)];
3550 let mut harvest = InboundHarvest::default();
3551 filter_inbound_from_peer(
3552 &DenyAll,
3553 &mut flowtrack::FlowCache::default(),
3554 peer,
3555 &mut packets,
3556 RejectConfig::default(),
3557 &mut harvest,
3558 );
3559
3560 assert!(
3561 harvest.learned_disco_keys.is_empty(),
3562 "a {name} must not be half-parsed into a learned disco key"
3563 );
3564 assert_eq!(
3565 packets.len(),
3566 usize::from(still_delivered),
3567 "a {name} must {} be delivered",
3568 if still_delivered { "still" } else { "not" }
3569 );
3570 }
3571 }
3572
3573 /// Our own disco key, the one this node advertises. Asymmetric so a reversed or offset slice
3574 /// would be visible in the marshalled bytes.
3575 const SELF_DISCO_KEY: [u8; 32] = [
3576 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
3577 0x00, 0x9c, 0x5f, 0x3a, 0x01, 0x7d, 0xe2, 0x44, 0xb8, 0x0f, 0x1e, 0x2d, 0x3c, 0x4b, 0x5a,
3578 0x69, 0x78,
3579 ];
3580
3581 /// An advertisement state with one peer, a v4 and a v6 address of our own, and a real disco key.
3582 fn advertisement_state(peer: PeerId, target: AdvertisementTarget) -> DiscoAdvertisementState {
3583 DiscoAdvertisementState {
3584 disco_key: SELF_DISCO_KEY,
3585 self_addrs: vec![
3586 std::net::IpAddr::from([100, 64, 0, 1]),
3587 std::net::IpAddr::from([
3588 0xfd, 0x7a, 0x11, 0x5c, 0xa1, 0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
3589 ]),
3590 ],
3591 peers: HashMap::from([(peer, target)]),
3592 }
3593 }
3594
3595 /// What this node advertises, and to whom (Go `magicsock.Conn.PriorityMessageForPeer`): the
3596 /// happy path emits the exact bytes `TSMPDiscoKeyAdvertisement.Marshal` emits, and each of Go's
3597 /// refusals emits nothing at all.
3598 #[test]
3599 fn disco_advertisement_matches_priority_message_for_peer() {
3600 let peer = PeerId(3);
3601 let peer_v4 = std::net::IpAddr::from([100, 64, 0, 2]);
3602 let target = AdvertisementTarget {
3603 node_addr: peer_v4,
3604 wireguard_only: false,
3605 };
3606 let state = advertisement_state(peer, target);
3607
3608 // Happy path: a v4 peer gets a v4 advertisement sourced from our v4 address — the first
3609 // self address in the destination's family (Go `selfIPMatchingFamily`).
3610 let msg = state
3611 .advertisement_for(peer)
3612 .expect("a Tailscale peer with a matching-family address must be advertised to");
3613 let parsed = ts_packet::tsmp::DiscoKeyAdvertisement::parse(&msg)
3614 .expect("what we emit must parse as an advertisement");
3615 assert_eq!(parsed.key, SELF_DISCO_KEY, "we advertise OUR disco key");
3616 assert_eq!(parsed.src, std::net::IpAddr::from([100, 64, 0, 1]));
3617 assert_eq!(parsed.dst, peer_v4);
3618 assert_eq!(
3619 msg,
3620 ts_packet::tsmp::DiscoKeyAdvertisement {
3621 src: std::net::IpAddr::from([100, 64, 0, 1]),
3622 dst: peer_v4,
3623 key: SELF_DISCO_KEY,
3624 }
3625 .marshal()
3626 .unwrap(),
3627 "the emitted bytes are exactly what Marshal produces"
3628 );
3629
3630 // A v6 peer is sourced from our v6 address, not our v4 one.
3631 let peer_v6 = std::net::IpAddr::from([
3632 0xfd, 0x7a, 0x11, 0x5c, 0xa1, 0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
3633 ]);
3634 let v6_state = advertisement_state(
3635 peer,
3636 AdvertisementTarget {
3637 node_addr: peer_v6,
3638 wireguard_only: false,
3639 },
3640 );
3641 let parsed = v6_state
3642 .advertisement_for(peer)
3643 .and_then(|m| ts_packet::tsmp::DiscoKeyAdvertisement::parse(&m))
3644 .expect("a v6 peer must be advertised to over v6");
3645 assert!(parsed.src.is_ipv6(), "source must match the peer's family");
3646 assert_eq!(parsed.dst, peer_v6);
3647
3648 // Refusal 1 (Go `disco.IsZero()`): no disco key of our own, nothing to advertise.
3649 let mut no_key = advertisement_state(peer, target);
3650 no_key.disco_key = [0u8; 32];
3651 assert!(
3652 no_key.advertisement_for(peer).is_none(),
3653 "the zero disco key must never be advertised"
3654 );
3655
3656 // Refusal 2 (Go `endpointForNodeKey` miss / `!self.Valid()`): a peer the netmap snapshot
3657 // does not cover, and a node with no addresses of its own.
3658 assert!(
3659 state.advertisement_for(PeerId(0xbad)).is_none(),
3660 "an unknown peer must not be advertised to"
3661 );
3662 let mut no_self = advertisement_state(peer, target);
3663 no_self.self_addrs.clear();
3664 assert!(
3665 no_self.advertisement_for(peer).is_none(),
3666 "a node with no tailnet address of its own has no source to advertise from"
3667 );
3668
3669 // Refusal 3 (Go `ep.isWireguardOnly`): "Do not send TSMP messages to peers that only speaks
3670 // wireguard" — such a peer would hand it to its host stack as an unknown protocol.
3671 let wg_only = advertisement_state(
3672 peer,
3673 AdvertisementTarget {
3674 node_addr: peer_v4,
3675 wireguard_only: true,
3676 },
3677 );
3678 assert!(
3679 wg_only.advertisement_for(peer).is_none(),
3680 "a WireGuard-only peer must never be sent TSMP"
3681 );
3682
3683 // Refusal 4 (Go `selfIPMatchingFamily` returning the zero Addr): an IPv4-only node has no
3684 // source address for a packet to a peer's IPv6 address.
3685 let mut v4_only = advertisement_state(
3686 peer,
3687 AdvertisementTarget {
3688 node_addr: peer_v6,
3689 wireguard_only: false,
3690 },
3691 );
3692 v4_only.self_addrs = vec![std::net::IpAddr::from([100, 64, 0, 1])];
3693 assert!(
3694 v4_only.advertisement_for(peer).is_none(),
3695 "no self address in the peer's family means no advertisement"
3696 );
3697 }
3698
3699 /// End to end, over a real WireGuard handshake: when a session with a peer comes up, this
3700 /// node's dataplane emits its own TSMP disco-key advertisement to that peer — and the peer's
3701 /// dataplane learns the key from it and drops the packet.
3702 ///
3703 /// This is the send side (Go capability version 144) meeting the receive side already in this
3704 /// tree, so the assertion is not "some bytes went out" but "the far side learned exactly the
3705 /// disco key we hold". B is deliberately left with no advertisement state, which also pins the
3706 /// unconfigured case: it establishes the same session and sends nothing back.
3707 #[test]
3708 fn session_establishment_advertises_our_disco_key_to_the_peer() {
3709 let underlay: UnderlayTransportId = 0.into();
3710 let wg_peer = ts_tunnel::PeerId(1);
3711 let peer = PeerId(1);
3712 let a_addr = std::net::IpAddr::from([100, 64, 0, 1]);
3713 let b_addr = std::net::IpAddr::from([100, 64, 0, 2]);
3714
3715 let (a_static, b_static) = (NodeKeyPair::new(), NodeKeyPair::new());
3716 let (mut a, mut b) = (
3717 DataPlane::new(a_static.clone()),
3718 DataPlane::new(b_static.clone()),
3719 );
3720
3721 for (dp, key) in [(&mut a, b_static.public), (&mut b, a_static.public)] {
3722 dp.wireguard.upsert_peer(
3723 wg_peer,
3724 ts_tunnel::PeerConfig {
3725 key,
3726 psk: [0u8; 32].into(),
3727 persistent_keepalive_interval: None,
3728 },
3729 );
3730 dp.ur_out.table.insert(peer, underlay);
3731 }
3732
3733 // Only A knows how to advertise: its own disco key, its own address, and B's address.
3734 a.disco_advertisement = Some(Arc::new(advertisement_state(
3735 peer,
3736 AdvertisementTarget {
3737 node_addr: b_addr,
3738 wireguard_only: false,
3739 },
3740 )));
3741
3742 // B attributes A's tailnet address to the WireGuard peer that carries it, as the runtime's
3743 // source filter does — without that, B drops the advertisement before parsing it.
3744 let mut src_filter = ts_bart::Table::default();
3745 src_filter.insert(ipnet::IpNet::from(a_addr), peer);
3746 b.src_filter_in = Arc::new(src_filter);
3747
3748 // Drive the handshake. Only the initiation is kicked off directly (the dataplane starts one
3749 // from routed outbound traffic, which is not what this test is about); everything after it
3750 // goes through `process_inbound`, the path under test.
3751 let take = |out: HashMap<(UnderlayTransportId, PeerId), Vec<PacketMut>>| {
3752 out.into_values().flatten().collect::<Vec<_>>()
3753 };
3754 let init = a
3755 .wireguard
3756 .send([(wg_peer, vec![PacketMut::from(&b"hello"[..])])])
3757 .to_peers
3758 .remove(&wg_peer)
3759 .expect("handshake initiation");
3760
3761 let resp = take(b.process_inbound(init).to_peers);
3762 assert!(!resp.is_empty(), "B must answer the handshake initiation");
3763
3764 // A completes the handshake. Its session is now current, so alongside the queued data it
3765 // emits the advertisement.
3766 let from_a = take(a.process_inbound(resp).to_peers);
3767 assert_eq!(
3768 from_a.len(),
3769 2,
3770 "A must emit the queued data AND its disco-key advertisement"
3771 );
3772
3773 // B learns A's disco key from it, and the advertisement itself is consumed rather than
3774 // delivered to B's local stack.
3775 let inbound = b.process_inbound(from_a);
3776 assert_eq!(
3777 inbound
3778 .learned_disco_keys
3779 .iter()
3780 .map(|(peer, advert)| (*peer, advert.key))
3781 .collect::<Vec<_>>(),
3782 vec![(peer, SELF_DISCO_KEY)],
3783 "B must learn exactly the disco key A holds, attributed to A's wireguard peer"
3784 );
3785 assert!(
3786 inbound.to_peers.is_empty(),
3787 "B has no advertisement state, so it advertises nothing back"
3788 );
3789 }
3790
3791 /// Order regression: the advertisement must LEAD the traffic the same establishment released,
3792 /// not trail it.
3793 ///
3794 /// wireguard-go hands a priority message straight to the peer's *outbound* queue
3795 /// (`SendPriorityMessage` → `queueOutboundIfRunning`) and runs it before the flush that
3796 /// follows at both call sites — `peer.SendPriorityMessage()` ahead of `peer.SendKeepalive()`
3797 /// on the initiator and ahead of `peer.SendStagedPackets()` on the responder
3798 /// (`device/receive.go`) — so the advertisement is the first thing on the wire once a keypair
3799 /// becomes current. In this tree the flush has already happened inside `Endpoint::recv` by the
3800 /// time the advertisement exists, so `process_inbound` has to splice it in front; appending it
3801 /// would put it behind up to `MAX_QUEUED_PER_PEER` packets of queued traffic.
3802 ///
3803 /// The order is read off B's *decrypted* stream — its capture tee, which sees every inbound
3804 /// packet before any filtering — so what is pinned is the order the peer actually observes,
3805 /// not the order of a local vector.
3806 #[test]
3807 fn the_advertisement_leads_the_traffic_released_by_the_same_establishment() {
3808 let underlay: UnderlayTransportId = 0.into();
3809 let wg_peer = ts_tunnel::PeerId(1);
3810 let peer = PeerId(1);
3811 let a_addr = std::net::IpAddr::from([100, 64, 0, 1]);
3812 let b_addr = std::net::IpAddr::from([100, 64, 0, 2]);
3813
3814 let (a_static, b_static) = (NodeKeyPair::new(), NodeKeyPair::new());
3815 let (mut a, mut b) = (
3816 DataPlane::new(a_static.clone()),
3817 DataPlane::new(b_static.clone()),
3818 );
3819
3820 for (dp, key) in [(&mut a, b_static.public), (&mut b, a_static.public)] {
3821 dp.wireguard.upsert_peer(
3822 wg_peer,
3823 ts_tunnel::PeerConfig {
3824 key,
3825 psk: [0u8; 32].into(),
3826 persistent_keepalive_interval: None,
3827 },
3828 );
3829 dp.ur_out.table.insert(peer, underlay);
3830 }
3831
3832 a.disco_advertisement = Some(Arc::new(advertisement_state(
3833 peer,
3834 AdvertisementTarget {
3835 node_addr: b_addr,
3836 wireguard_only: false,
3837 },
3838 )));
3839
3840 let mut src_filter = ts_bart::Table::default();
3841 src_filter.insert(ipnet::IpNet::from(a_addr), peer);
3842 b.src_filter_in = Arc::new(src_filter);
3843
3844 // Everything B decrypts, in arrival order, before any filtering runs.
3845 let recorded: CaptureLog = Arc::new(Mutex::new(Vec::new()));
3846 let sink = recorded.clone();
3847 b.capture = Some(Arc::new(move |path: CapturePath, bytes: &[u8]| {
3848 sink.lock().unwrap().push((path, bytes.to_vec()));
3849 }));
3850
3851 let take = |out: HashMap<(UnderlayTransportId, PeerId), Vec<PacketMut>>| {
3852 out.into_values().flatten().collect::<Vec<_>>()
3853 };
3854
3855 // Traffic for a peer with no session yet: it stages, and a handshake starts.
3856 const QUEUED: &[u8] = b"staged while the session was still coming up";
3857 let init = a
3858 .wireguard
3859 .send([(wg_peer, vec![PacketMut::from(QUEUED)])])
3860 .to_peers
3861 .remove(&wg_peer)
3862 .expect("handshake initiation");
3863 let resp = take(b.process_inbound(init).to_peers);
3864
3865 // A's keypair becomes current here, which both flushes the staged packet and produces the
3866 // advertisement — the batch whose order is under test.
3867 let from_a = take(a.process_inbound(resp).to_peers);
3868 assert_eq!(
3869 from_a.len(),
3870 2,
3871 "A must emit the queued data AND its disco-key advertisement"
3872 );
3873
3874 // Hand them to B in exactly the order A produced them.
3875 let learned = b.process_inbound(from_a).learned_disco_keys;
3876 assert_eq!(
3877 learned
3878 .iter()
3879 .map(|(peer, advert)| (*peer, advert.key))
3880 .collect::<Vec<_>>(),
3881 vec![(peer, SELF_DISCO_KEY)],
3882 "B must still learn A's disco key"
3883 );
3884
3885 let advertisement = ts_packet::tsmp::DiscoKeyAdvertisement {
3886 src: a_addr,
3887 dst: b_addr,
3888 key: SELF_DISCO_KEY,
3889 }
3890 .marshal()
3891 .expect("a v4 advertisement between two v4 addresses marshals");
3892
3893 let captured = recorded.lock().unwrap();
3894 let from_peer = captured
3895 .iter()
3896 .filter(|(path, _)| *path == CapturePath::FromPeer)
3897 .map(|(_, bytes)| bytes.as_slice())
3898 .collect::<Vec<_>>();
3899 assert_eq!(from_peer.len(), 2, "B must decrypt both of A's packets");
3900 // The send path zero-pads each payload up to a 16-byte boundary and the receiver delivers
3901 // it with that padding intact (see `session::PADDING_MULTIPLE`), so compare on the leading
3902 // bytes rather than for equality.
3903 assert!(
3904 from_peer[0].starts_with(&advertisement),
3905 "the advertisement must reach the peer FIRST, ahead of the traffic the same \
3906 establishment released"
3907 );
3908 assert!(
3909 from_peer[1].starts_with(QUEUED),
3910 "the queued traffic follows the advertisement"
3911 );
3912 }
3913
3914 /// Behavioral guard: an installed capture hook MUST be invoked with `CapturePath::FromLocal`
3915 /// and the exact packet bytes for every outbound packet. The tee sits at the top of
3916 /// `process_outbound`, before `or_out.route` consumes the packets, so it fires regardless of
3917 /// whether a wireguard peer exists (an empty router just drops the routed packets afterward).
3918 /// This is the only end-to-end guard that the dataplane capture tee actually fires; a refactor
3919 /// that drops the tee would leave every byte-layout test green.
3920 #[test]
3921 fn capture_hook_fires_on_outbound() {
3922 let mut dp = DataPlane::new(NodeKeyPair::new());
3923
3924 let recorded: CaptureLog = Arc::new(Mutex::new(Vec::new()));
3925 let sink = recorded.clone();
3926 dp.capture = Some(Arc::new(move |path: CapturePath, bytes: &[u8]| {
3927 sink.lock().unwrap().push((path, bytes.to_vec()));
3928 }));
3929
3930 // The outbound tee passes `p.as_ref()` as-given; the bytes need not be a valid IP packet.
3931 let payload: Vec<u8> = vec![0xde, 0xad, 0xbe, 0xef];
3932 let packet = PacketMut::from(payload.clone());
3933
3934 drop(dp.process_outbound(vec![packet]));
3935
3936 let captured = recorded.lock().unwrap();
3937 assert_eq!(captured.len(), 1, "hook must fire exactly once per packet");
3938 assert_eq!(captured[0].0, CapturePath::FromLocal);
3939 assert_eq!(captured[0].1, payload);
3940 }
3941
3942 /// A minimal IPv4/UDP datagram from `src` to `dst`. The control for the outbound TSMP refusal:
3943 /// same source, same destination, same batch as the forged advertisement — only the protocol
3944 /// byte differs.
3945 fn v4_udp_packet(
3946 src: std::net::SocketAddr,
3947 dst: std::net::SocketAddr,
3948 payload: &[u8],
3949 ) -> Vec<u8> {
3950 let (std::net::IpAddr::V4(src_ip), std::net::IpAddr::V4(dst_ip)) = (src.ip(), dst.ip())
3951 else {
3952 panic!("v4_udp_packet needs two IPv4 addresses");
3953 };
3954 let total_len = u16::try_from(IP4_HEADER_LEN + 8 + payload.len()).unwrap();
3955 let mut buf = vec![0u8; usize::from(total_len)];
3956 buf[0] = 0x45; // version 4, IHL 5 (no options)
3957 buf[2..4].copy_from_slice(&total_len.to_be_bytes());
3958 buf[8] = 64; // TTL
3959 buf[9] = 17; // UDP
3960 buf[12..16].copy_from_slice(&src_ip.octets());
3961 buf[16..20].copy_from_slice(&dst_ip.octets());
3962 buf[20..22].copy_from_slice(&src.port().to_be_bytes());
3963 buf[22..24].copy_from_slice(&dst.port().to_be_bytes());
3964 let udp_len = u16::try_from(8 + payload.len()).unwrap();
3965 buf[24..26].copy_from_slice(&udp_len.to_be_bytes());
3966 // UDP checksum left 0 ("not computed"), which is legal for IPv4.
3967 buf[IP4_HEADER_LEN + 8..].copy_from_slice(payload);
3968 buf
3969 }
3970
3971 /// What `process_outbound` refuses, mirroring the `p.IPProto == ipproto.TSMP` arm of Go
3972 /// `tstun.filterPacketOutboundToWireGuard` — plus the three shapes this tree drops that Go's
3973 /// TSMP arm alone does not, because there is no outbound ACL behind it here to refuse them as
3974 /// `ipproto.Unknown`. See [`outbound_packet_carries_tsmp`].
3975 #[test]
3976 fn outbound_tsmp_classification_matches_go_decode() {
3977 let v4_src = std::net::IpAddr::from([100, 64, 0, 1]);
3978 let v4_dst = std::net::IpAddr::from([100, 64, 0, 2]);
3979 let v4 = ts_packet::tsmp::DiscoKeyAdvertisement {
3980 src: v4_src,
3981 dst: v4_dst,
3982 key: SELF_DISCO_KEY,
3983 }
3984 .marshal()
3985 .expect("a v4 advertisement between two v4 addresses marshals");
3986 let v6 = ts_packet::tsmp::DiscoKeyAdvertisement {
3987 src: std::net::IpAddr::V6(IPV6_FIXTURE_SRC),
3988 dst: std::net::IpAddr::V6(IPV6_FIXTURE_DST),
3989 key: SELF_DISCO_KEY,
3990 }
3991 .marshal()
3992 .expect("a v6 advertisement between two v6 addresses marshals");
3993
3994 // The forgery this exists to stop, in both families: bytes byte-identical to what this node
3995 // would itself emit, handed to us by the host instead.
3996 assert!(
3997 outbound_packet_carries_tsmp(&v4),
3998 "an IPv4 TSMP packet from the host is refused"
3999 );
4000 assert!(
4001 outbound_packet_carries_tsmp(&v6),
4002 "an IPv6 TSMP packet from the host is refused"
4003 );
4004
4005 // Ordinary traffic is untouched — the refusal is protocol-specific, not a blanket drop.
4006 assert!(
4007 !outbound_packet_carries_tsmp(&v4_udp_packet(
4008 std::net::SocketAddr::new(v4_src, 4242),
4009 std::net::SocketAddr::new(v4_dst, 4343),
4010 b"hello"
4011 )),
4012 "IPv4 UDP passes"
4013 );
4014 assert!(
4015 !outbound_packet_carries_tsmp(&ipv6_udp_packet(&udp_header(53))),
4016 "IPv6 UDP passes"
4017 );
4018
4019 // Go demotes a *fragmented* IPv4 TSMP packet to `ipproto.Unknown`, which its outbound ACL
4020 // then drops for "unknown proto". With no outbound ACL here the protocol byte is the whole
4021 // verdict, so the refusal happens one step earlier and the packet still never ships.
4022 let mut fragmented = v4.clone();
4023 fragmented[6] = 0x20; // More Fragments
4024 assert!(
4025 outbound_packet_carries_tsmp(&fragmented),
4026 "a fragmented IPv4 TSMP packet is refused too"
4027 );
4028
4029 // An IPv6 Fragment extension header naming TSMP: Go classifies the head fragment TSMP and
4030 // its followers `ipproto.Fragment`. Both are refused here — every fragment of one datagram
4031 // repeats the same Next Header, and with the head refused no peer could reassemble anyway.
4032 assert!(
4033 outbound_packet_carries_tsmp(&ipv6_fragment_packet(
4034 ts_packet::tsmp::IP_PROTO_TSMP,
4035 0,
4036 true,
4037 &[b'a'; 33],
4038 )),
4039 "the head fragment of an IPv6 TSMP datagram is refused"
4040 );
4041 assert!(
4042 outbound_packet_carries_tsmp(&ipv6_fragment_packet(
4043 ts_packet::tsmp::IP_PROTO_TSMP,
4044 MIN_FRAG_BLKS,
4045 false,
4046 &[0u8; 8],
4047 )),
4048 "so are its later fragments"
4049 );
4050 assert!(
4051 !outbound_packet_carries_tsmp(&ipv6_fragment_packet(17, 0, true, &udp_header(53))),
4052 "a fragmented IPv6 UDP datagram is not TSMP and still passes"
4053 );
4054
4055 // Nothing to classify: not IP at all, or truncated before the protocol byte can be trusted.
4056 assert!(
4057 !outbound_packet_carries_tsmp(&[]),
4058 "the empty buffer passes"
4059 );
4060 assert!(
4061 !outbound_packet_carries_tsmp(&[0xde, 0xad, 0xbe, 0xef]),
4062 "a non-IP buffer passes (the router drops it for want of a destination)"
4063 );
4064 assert!(
4065 !outbound_packet_carries_tsmp(&v4[..IP4_HEADER_LEN - 1]),
4066 "an IPv4 packet cut off inside its header passes"
4067 );
4068 assert!(
4069 !outbound_packet_carries_tsmp(&v6[..IP6_HEADER_LEN - 1]),
4070 "an IPv6 packet cut off inside its header passes"
4071 );
4072 }
4073
4074 /// The whole point of the outbound TSMP refusal, end to end, together with the negative case
4075 /// that keeps it from silently disabling capability version 144.
4076 ///
4077 /// A local process writes a well-formed disco-key advertisement — naming a disco key of its own
4078 /// choosing, addressed to a peer whose route really does resolve to a live WireGuard session —
4079 /// into the tun. The peer must never see it: it arrives inside this node's session from this
4080 /// node's tailnet address, so it is indistinguishable from one this node meant to send, and the
4081 /// peer would bind the forger's key for us. Ordinary traffic in the same batch to the same
4082 /// destination must be untouched.
4083 ///
4084 /// And the advertisement this node itself sends must still go out. It is built by
4085 /// `DiscoAdvertisementState::advertisement_for` and injected by `process_inbound` on session
4086 /// establishment, *below* the refusal — Go has the same relationship, where `injectedRead`
4087 /// bypasses the outbound filter. Without this half of the test a drop placed one layer too low
4088 /// would look green.
4089 #[test]
4090 fn host_written_tsmp_is_dropped_while_our_own_advertisement_still_goes_out() {
4091 let underlay: UnderlayTransportId = 0.into();
4092 let wg_peer = ts_tunnel::PeerId(1);
4093 let peer = PeerId(1);
4094 let a_addr = std::net::IpAddr::from([100, 64, 0, 1]);
4095 let b_addr = std::net::IpAddr::from([100, 64, 0, 2]);
4096
4097 let (a_static, b_static) = (NodeKeyPair::new(), NodeKeyPair::new());
4098 let (mut a, mut b) = (
4099 DataPlane::new(a_static.clone()),
4100 DataPlane::new(b_static.clone()),
4101 );
4102
4103 for (dp, key) in [(&mut a, b_static.public), (&mut b, a_static.public)] {
4104 dp.wireguard.upsert_peer(
4105 wg_peer,
4106 ts_tunnel::PeerConfig {
4107 key,
4108 psk: [0u8; 32].into(),
4109 persistent_keepalive_interval: None,
4110 },
4111 );
4112 dp.ur_out.table.insert(peer, underlay);
4113 }
4114
4115 a.disco_advertisement = Some(Arc::new(advertisement_state(
4116 peer,
4117 AdvertisementTarget {
4118 node_addr: b_addr,
4119 wireguard_only: false,
4120 },
4121 )));
4122
4123 // A routes B's tailnet address to the wireguard peer, so a host-written packet addressed to
4124 // B really would be encrypted and shipped were it not refused. Without this the test would
4125 // pass on an empty routing table and prove nothing.
4126 let mut routes = ts_bart::Table::default();
4127 routes.insert(
4128 ipnet::IpNet::from(b_addr),
4129 or::outbound::RouteAction::Wireguard(peer),
4130 );
4131 a.or_out.swap(routes);
4132
4133 // B attributes A's tailnet address to the wireguard peer that carries it, as the runtime's
4134 // source filter does.
4135 let mut src_filter = ts_bart::Table::default();
4136 src_filter.insert(ipnet::IpNet::from(a_addr), peer);
4137 b.src_filter_in = Arc::new(src_filter);
4138
4139 // Everything B decrypts, in arrival order, before any filtering runs.
4140 let recorded: CaptureLog = Arc::new(Mutex::new(Vec::new()));
4141 let sink = recorded.clone();
4142 b.capture = Some(Arc::new(move |path: CapturePath, bytes: &[u8]| {
4143 sink.lock().unwrap().push((path, bytes.to_vec()));
4144 }));
4145
4146 let take = |out: HashMap<(UnderlayTransportId, PeerId), Vec<PacketMut>>| {
4147 out.into_values().flatten().collect::<Vec<_>>()
4148 };
4149
4150 // Establish the session. A's own advertisement rides the establishment.
4151 let init = a
4152 .wireguard
4153 .send([(wg_peer, vec![PacketMut::from(&b"hello"[..])])])
4154 .to_peers
4155 .remove(&wg_peer)
4156 .expect("handshake initiation");
4157 let resp = take(b.process_inbound(init).to_peers);
4158 let from_a = take(a.process_inbound(resp).to_peers);
4159 let learned = b.process_inbound(from_a).learned_disco_keys;
4160 assert_eq!(
4161 learned
4162 .iter()
4163 .map(|(peer, advert)| (*peer, advert.key))
4164 .collect::<Vec<_>>(),
4165 vec![(peer, SELF_DISCO_KEY)],
4166 "our own advertisement must still reach the peer: it is injected below process_outbound"
4167 );
4168
4169 // Now the forgery, alongside ordinary traffic to the same destination in the same batch.
4170 const FORGED_KEY: [u8; 32] = [0xff; 32];
4171 let forged = ts_packet::tsmp::DiscoKeyAdvertisement {
4172 src: a_addr,
4173 dst: b_addr,
4174 key: FORGED_KEY,
4175 }
4176 .marshal()
4177 .expect("a v4 advertisement between two v4 addresses marshals");
4178 const CARRIED: &[u8] = b"ordinary traffic in the same batch";
4179 let control = v4_udp_packet(
4180 std::net::SocketAddr::new(a_addr, 4242),
4181 std::net::SocketAddr::new(b_addr, 4343),
4182 CARRIED,
4183 );
4184
4185 // This is the only test that increments this counter, so the delta is exact.
4186 let counted_before = metric_out_to_wg_drop_tsmp().value();
4187 let out = a.process_outbound(vec![
4188 PacketMut::from(&forged[..]),
4189 PacketMut::from(&control[..]),
4190 ]);
4191
4192 let mark = recorded.lock().unwrap().len();
4193 let inbound = b.process_inbound(take(out.to_peers));
4194 assert!(
4195 inbound.learned_disco_keys.is_empty(),
4196 "the forged advertisement must never reach the peer, or it binds the forger's key for us"
4197 );
4198
4199 let captured = recorded.lock().unwrap();
4200 let delivered = captured[mark..]
4201 .iter()
4202 .filter(|(path, _)| *path == CapturePath::FromPeer)
4203 .map(|(_, bytes)| bytes.as_slice())
4204 .collect::<Vec<_>>();
4205 assert_eq!(
4206 delivered.len(),
4207 1,
4208 "exactly the one non-TSMP packet of the batch crosses the tunnel"
4209 );
4210 // The send path zero-pads each payload up to a 16-byte boundary and the receiver delivers it
4211 // with that padding intact (see `session::PADDING_MULTIPLE`), so compare on the leading bytes.
4212 assert!(
4213 delivered[0].starts_with(&control),
4214 "and it is the ordinary traffic, unaltered"
4215 );
4216
4217 assert_eq!(
4218 metric_out_to_wg_drop_tsmp().value(),
4219 counted_before + 1,
4220 "the drop is counted in tstun_out_to_wg_drop_tsmp (Go metricPacketOutDropTSMP)"
4221 );
4222 }
4223
4224 /// Run one inbound packet through the real inbound filter with `flows` as the connection-
4225 /// tracking state, and report whether it survived.
4226 fn admitted(
4227 filter: &(dyn ts_packetfilter::Filter + Send + Sync),
4228 flows: &mut flowtrack::FlowCache,
4229 packet: Vec<u8>,
4230 ) -> bool {
4231 let mut packets = vec![PacketMut::from(packet)];
4232 let mut harvest = InboundHarvest::default();
4233 filter_inbound_from_peer(
4234 filter,
4235 flows,
4236 PeerId(1),
4237 &mut packets,
4238 RejectConfig::default(),
4239 &mut harvest,
4240 );
4241 assert!(
4242 harvest.learned_disco_keys.is_empty(),
4243 "no TSMP advertisement in these fixtures"
4244 );
4245 !packets.is_empty()
4246 }
4247
4248 /// A dataplane wired up well enough that `process_outbound` really routes a packet to a peer,
4249 /// so the flow-tracking tests exercise the live outbound path rather than a stub.
4250 fn dataplane_routing_to(peer: PeerId, dsts: &[std::net::IpAddr]) -> DataPlane {
4251 let mut dp = DataPlane::new(NodeKeyPair::new());
4252 dp.wireguard.upsert_peer(
4253 ts_tunnel::PeerId(peer.0),
4254 ts_tunnel::PeerConfig {
4255 key: NodeKeyPair::new().public,
4256 psk: [0u8; 32].into(),
4257 persistent_keepalive_interval: None,
4258 },
4259 );
4260 dp.ur_out.table.insert(peer, 0.into());
4261 let mut routes = ts_bart::Table::default();
4262 for dst in dsts {
4263 routes.insert(
4264 ipnet::IpNet::from(*dst),
4265 or::outbound::RouteAction::Wireguard(peer),
4266 );
4267 }
4268 dp.or_out.swap(routes);
4269 dp
4270 }
4271
4272 /// Go's reverse-flow connection tracking (`wgengine/filter`'s `Filter.state`), across the two
4273 /// paths it spans: `process_outbound` records the reversed tuple of every outbound UDP
4274 /// datagram (Go `UpdateOutboundFlowState`, which upstream `e0677ccc7` had to start calling on
4275 /// the netstack-injected path — the only path this engine has), and the inbound filter admits
4276 /// the reply on it ahead of any rule (Go `runIn4`'s `return Accept, "cached"`).
4277 ///
4278 /// Every verdict here is taken under a **deny-all** ACL, so an admission can only have come
4279 /// from the flow cache. The refusals are the point of the test as much as the admission is: a
4280 /// conntrack that admits too much is an open door, so each of the four fields of Go's
4281 /// `flowtrack.Tuple` is varied in turn and must miss.
4282 #[test]
4283 fn an_outbound_udp_flow_admits_its_own_reply_and_nothing_else() {
4284 let peer = PeerId(1);
4285 let me = std::net::IpAddr::from([100, 64, 0, 1]);
4286 let them = std::net::IpAddr::from([100, 64, 0, 2]);
4287 let elsewhere = std::net::IpAddr::from([100, 64, 0, 3]);
4288 let sa = |ip, port| std::net::SocketAddr::new(ip, port);
4289
4290 let mut dp = dataplane_routing_to(peer, &[them, elsewhere]);
4291
4292 // Our datagram out, and the reply it should get: the same tuple, reversed. Our source port
4293 // is ephemeral, which is exactly why no ACL from control can name it.
4294 let query = v4_udp_packet(sa(me, 41234), sa(them, 53), b"query");
4295 let reply = v4_udp_packet(sa(them, 53), sa(me, 41234), b"answer");
4296
4297 // Before anything is sent the reply is an unsolicited datagram to an ephemeral port, and a
4298 // deny-all ACL drops it. That is what every UDP reply used to get in this fork.
4299 assert!(
4300 !admitted(&DenyAll, &mut dp.flows, reply.clone()),
4301 "an inbound datagram matching no outbound flow and no rule is dropped"
4302 );
4303
4304 let out = dp.process_outbound(vec![PacketMut::from(query)]);
4305 assert!(
4306 !out.to_peers.is_empty(),
4307 "the datagram really did route to the peer, so this is the live outbound path"
4308 );
4309
4310 assert!(
4311 admitted(&DenyAll, &mut dp.flows, reply.clone()),
4312 "the reply to our own datagram is admitted with no rule matching it"
4313 );
4314
4315 // One field of Go's tuple differs in each of these, and each must miss. Without them the
4316 // cache would be a blanket "any inbound UDP is fine once we have sent one".
4317 for (why, packet) in [
4318 (
4319 "a different source PORT",
4320 v4_udp_packet(sa(them, 5353), sa(me, 41234), b"x"),
4321 ),
4322 (
4323 "a different source ADDRESS",
4324 v4_udp_packet(sa(elsewhere, 53), sa(me, 41234), b"x"),
4325 ),
4326 (
4327 "a different destination PORT",
4328 v4_udp_packet(sa(them, 53), sa(me, 41235), b"x"),
4329 ),
4330 (
4331 "a different destination ADDRESS",
4332 v4_udp_packet(sa(them, 53), sa(elsewhere, 41234), b"x"),
4333 ),
4334 ] {
4335 assert!(
4336 !admitted(&DenyAll, &mut dp.flows, packet),
4337 "{why} does not ride the recorded entry"
4338 );
4339 }
4340
4341 // And the entry admits, it never denies: the ACL still decides everything the cache misses.
4342 assert!(
4343 admitted(
4344 &AllowAll,
4345 &mut dp.flows,
4346 v4_udp_packet(sa(elsewhere, 53), sa(me, 41234), b"x")
4347 ),
4348 "a cache miss falls through to the rule match, exactly as Go does"
4349 );
4350 }
4351
4352 /// SCTP is the second arm of Go's `case ipproto.UDP, ipproto.SCTP` — on both the recording and
4353 /// the admitting side — and the protocol is part of the tuple, so a UDP entry cannot carry an
4354 /// SCTP datagram or the other way round.
4355 #[test]
4356 fn outbound_sctp_flows_are_tracked_and_do_not_cross_protocols() {
4357 let peer = PeerId(1);
4358 let dst = std::net::IpAddr::V4(IPV4_FIXTURE_DST);
4359 let mut dp = dataplane_routing_to(peer, &[dst]);
4360
4361 // `v4_packet` addresses this fixture src -> dst, so the reply is dst -> src. `sctp_header`
4362 // writes source port 54276.
4363 let out = v4_packet(132, 0, false, &sctp_header(443, SCTP_HEADER_LEN));
4364 let mut reply = v4_packet(132, 0, false, &sctp_header(54276, SCTP_HEADER_LEN));
4365 reply[12..16].copy_from_slice(&IPV4_FIXTURE_DST.octets());
4366 reply[16..20].copy_from_slice(&IPV4_FIXTURE_SRC.octets());
4367 reply[20..22].copy_from_slice(&443u16.to_be_bytes());
4368
4369 assert!(
4370 !admitted(&DenyAll, &mut dp.flows, reply.clone()),
4371 "an unsolicited SCTP packet with no rule is dropped"
4372 );
4373 drop(dp.process_outbound(vec![PacketMut::from(out)]));
4374 assert!(
4375 admitted(&DenyAll, &mut dp.flows, reply.clone()),
4376 "the SCTP reply to our own packet is admitted (Go's second switch arm)"
4377 );
4378
4379 // Same addresses, same ports, UDP instead of SCTP: the protocol is part of Go's tuple.
4380 //
4381 // Reinterpreting the SCTP header as a UDP one puts the verification tag where the UDP
4382 // length field belongs, and `UdpSlice::from_slice` refuses a length field the slice cannot
4383 // satisfy — so the datagram has to be given a real length here, or the drop below is the
4384 // parse failing and says nothing at all about the protocol in the tuple.
4385 let mut as_udp = reply.clone();
4386 as_udp[9] = 17;
4387 as_udp[24..26].copy_from_slice(&u16::try_from(SCTP_HEADER_LEN).unwrap().to_be_bytes());
4388 assert!(
4389 admitted(&AllowAll, &mut dp.flows, as_udp.clone()),
4390 "the fixture is a datagram the filter can parse, so the drop below is the tuple's"
4391 );
4392 assert!(
4393 !admitted(&DenyAll, &mut dp.flows, as_udp),
4394 "a UDP datagram does not ride an SCTP entry"
4395 );
4396 }
4397
4398 /// What [`outbound_udp_or_sctp_flow`] records and — the half that keeps the cache honest —
4399 /// what it refuses to record, carrying Go's `decode4`/`decode6` refusals into
4400 /// `UpdateOutboundFlowState`. Everything this function declines leaves the reply where it was
4401 /// before: needing an ACL rule.
4402 #[test]
4403 fn outbound_flow_decoding_matches_go_decode() {
4404 let me = std::net::IpAddr::from([100, 64, 0, 1]);
4405 let them = std::net::IpAddr::from([100, 64, 0, 2]);
4406 let sa = |ip, port| std::net::SocketAddr::new(ip, port);
4407
4408 // UDP: both ports off the wire, addresses in packet order — reversing them is the cache's
4409 // job (Go `MakeTuple(q.IPProto, q.Dst, q.Src)`), not the decoder's.
4410 assert_eq!(
4411 outbound_udp_or_sctp_flow(&v4_udp_packet(sa(me, 41234), sa(them, 53), b"q")),
4412 Some((IpProto::UDP, sa(me, 41234), sa(them, 53))),
4413 "an outbound UDP datagram yields its own tuple"
4414 );
4415
4416 // SCTP: etherparse has no arm for it, so the ports come from Go's `sub[0:2]`/`sub[2:4]`.
4417 let v4_fixture_src = std::net::IpAddr::V4(IPV4_FIXTURE_SRC);
4418 let v4_fixture_dst = std::net::IpAddr::V4(IPV4_FIXTURE_DST);
4419 assert_eq!(
4420 outbound_udp_or_sctp_flow(&v4_packet(
4421 132,
4422 0,
4423 false,
4424 &sctp_header(443, SCTP_HEADER_LEN)
4425 )),
4426 Some((
4427 IpProto::SCTP,
4428 sa(v4_fixture_src, 54276),
4429 sa(v4_fixture_dst, 443)
4430 )),
4431 "an outbound SCTP packet yields its tuple, ports read the way Go reads them"
4432 );
4433 assert_eq!(
4434 outbound_udp_or_sctp_flow(&v4_packet(
4435 132,
4436 0,
4437 false,
4438 &sctp_header(443, SCTP_HEADER_LEN - 1)
4439 )),
4440 None,
4441 "an SCTP common header too short to hold its ports records nothing, not a port-0 flow"
4442 );
4443
4444 // Go's switch has exactly two arms; TCP, ICMP and TSMP fall through it.
4445 for (proto, name) in [(6u8, "TCP"), (1, "ICMP"), (99, "TSMP")] {
4446 assert_eq!(
4447 outbound_udp_or_sctp_flow(&v4_packet(proto, 0, false, &[0u8; 20])),
4448 None,
4449 "{name} is not tracked (Go tracks UDP and SCTP only)"
4450 );
4451 }
4452
4453 // A *first* IPv4 fragment carries its whole UDP header, so `decode4` reads its ports and
4454 // the flow is recorded like an unfragmented datagram's...
4455 let mut head = v4_udp_packet(sa(me, 41234), sa(them, 53), b"payload!");
4456 head[6] = 0x20; // More Fragments, offset 0
4457 assert_eq!(
4458 outbound_udp_or_sctp_flow(&head),
4459 Some((IpProto::UDP, sa(me, 41234), sa(them, 53))),
4460 "the head fragment of an outbound datagram records the same tuple"
4461 );
4462 // ...while a non-first fragment is `ipproto.Fragment` to Go, matching neither arm, and has
4463 // no transport header whose ports could be invented.
4464 let mut later = v4_udp_packet(sa(me, 41234), sa(them, 53), b"payload!");
4465 later[6..8].copy_from_slice(&MIN_FRAG_BLKS.to_be_bytes());
4466 assert_eq!(
4467 outbound_udp_or_sctp_flow(&later),
4468 None,
4469 "a non-first IPv4 fragment records nothing"
4470 );
4471
4472 // IPv6: the protocol comes from the base header only (Go `decode6`'s `b[6]`), so a UDP
4473 // header buried behind a chained extension header is not a flow upstream tracks either —
4474 // even though etherparse could walk to it.
4475 assert_eq!(
4476 outbound_udp_or_sctp_flow(&ipv6_udp_packet(&udp_header(53))),
4477 Some((
4478 IpProto::UDP,
4479 sa(std::net::IpAddr::V6(IPV6_FIXTURE_SRC), 54276),
4480 sa(std::net::IpAddr::V6(IPV6_FIXTURE_DST), 53)
4481 )),
4482 "an outbound IPv6 UDP datagram yields its tuple"
4483 );
4484 assert_eq!(
4485 outbound_udp_or_sctp_flow(&ipv6_with_prepended_ext_header(
4486 60,
4487 &ipv6_udp_packet(&udp_header(53))
4488 )),
4489 None,
4490 "a UDP header behind a chained extension header is not a tracked flow"
4491 );
4492
4493 // A leading IPv6 Fragment header IS stepped over, exactly as `decode6` does.
4494 assert_eq!(
4495 outbound_udp_or_sctp_flow(&ipv6_fragment_packet(17, 0, true, &udp_header(53))),
4496 Some((
4497 IpProto::UDP,
4498 sa(std::net::IpAddr::V6(IPV6_FIXTURE_SRC), 54276),
4499 sa(std::net::IpAddr::V6(IPV6_FIXTURE_DST), 53)
4500 )),
4501 "a first IPv6 fragment records the tuple behind its Fragment header"
4502 );
4503 assert_eq!(
4504 outbound_udp_or_sctp_flow(&ipv6_fragment_packet(17, MIN_FRAG_BLKS, false, &[0u8; 8])),
4505 None,
4506 "a later IPv6 fragment records nothing"
4507 );
4508
4509 // Nothing to decode at all.
4510 assert_eq!(outbound_udp_or_sctp_flow(&[]), None, "the empty buffer");
4511 assert_eq!(
4512 outbound_udp_or_sctp_flow(&[0xde, 0xad, 0xbe, 0xef]),
4513 None,
4514 "a non-IP buffer"
4515 );
4516 }
4517
4518 /// Go `packet.TCPFin`, the flag bit `tcp_header` writes into the byte Go reads as `q.TCPFlags`.
4519 const TCP_FIN: u8 = 0x01;
4520 /// Go `packet.TCPSyn`.
4521 const TCP_SYN: u8 = 0x02;
4522 /// Go `packet.TCPRst`.
4523 const TCP_RST: u8 = 0x04;
4524 /// Go `packet.TCPAck`.
4525 const TCP_ACK: u8 = 0x10;
4526
4527 /// A minimal 20-byte TCP header: the two ports, a data offset of 5 (no options, which is what
4528 /// makes it 20 bytes and what etherparse bounds-checks), and `flags` in `sub[13]` — the byte Go
4529 /// `decode4`/`decode6` copy into `q.TCPFlags`. The checksum is left zero; nothing on this path
4530 /// verifies it, and neither does Go's decoder.
4531 fn tcp_header(src_port: u16, dst_port: u16, flags: u8) -> Vec<u8> {
4532 let mut hdr = vec![0u8; 20];
4533 hdr[0..2].copy_from_slice(&src_port.to_be_bytes());
4534 hdr[2..4].copy_from_slice(&dst_port.to_be_bytes());
4535 hdr[12] = 0x50; // data offset 5 (a 20-byte header), no options
4536 hdr[13] = flags;
4537 hdr
4538 }
4539
4540 /// A minimal 8-byte ICMP/ICMPv6 message: type, code, a zero checksum, and four bytes of
4541 /// rest-of-header. Eight is the length Go guards every one of its type/code reads with
4542 /// (`len(q.b) >= q.subofs+8`), and etherparse's ICMP slices refuse anything shorter too.
4543 fn icmp_header(icmp_type: u8, icmp_code: u8) -> Vec<u8> {
4544 let mut hdr = vec![0u8; 8];
4545 hdr[0] = icmp_type;
4546 hdr[1] = icmp_code;
4547 hdr
4548 }
4549
4550 /// Go's TCP reply carve-out, on real bytes through the whole inbound filter: `runIn4`/`runIn6`
4551 /// accept an inbound TCP segment that is not a SYN *before* any rule is consulted.
4552 ///
4553 /// ```text
4554 /// if !q.IsTCPSyn() {
4555 /// return Accept, "tcp non-syn"
4556 /// }
4557 /// ```
4558 ///
4559 /// Every accept below is taken under a **deny-all** filter, so it can only have come from the
4560 /// carve-out. The motivating case is the first one: this node dials out, the peer answers with
4561 /// a SYN-ACK aimed at our ephemeral source port, and no ACL control can write will ever name
4562 /// that port. Without the carve-out the handshake never completes and the failure reads as a
4563 /// network fault rather than as a filter decision.
4564 ///
4565 /// The refusals are what keep it from being an open door, and they are the point of the test as
4566 /// much as the accepts are. A SYN — the one segment that can *start* an inbound session — still
4567 /// faces the rules, and so does a non-SYN segment buried under an IPv6 extension header, which
4568 /// upstream filters as protocol 43 rather than as TCP. The third refusal, a segment whose flags
4569 /// this fork never decoded, is pinned by every case that goes through [`stateless_verdict`]:
4570 /// they all carry [`ts_packetfilter::L4Header::Unknown`].
4571 ///
4572 /// Ported from github.com/tailscale/tailscale `wgengine/filter/filter.go` (`runIn4`, `runIn6`)
4573 /// and `net/packet/packet.go` (`IsTCPSyn`, `decode4`, `decode6`) at
4574 /// `3945b82f8a9550b54c33e61d4ed2227862d53e8a`.
4575 #[test]
4576 fn inbound_tcp_non_syn_is_admitted_without_a_rule_but_a_syn_is_not() {
4577 let flows = &mut flowtrack::FlowCache::default();
4578 // Source port 443, destination port 41234: the shape of a reply to a connection this node
4579 // opened. No rule in `DenyAll` names it, and none in a real tailnet ACL could.
4580 let v4 = |flags| v4_packet(6, 0, false, &tcp_header(443, 41234, flags));
4581 let v6 = |flags| ipv6_packet(6, &tcp_header(443, 41234, flags));
4582
4583 for (why, flags) in [
4584 (
4585 "a SYN-ACK — the answer to our own outbound connection",
4586 TCP_SYN | TCP_ACK,
4587 ),
4588 (
4589 "a bare ACK — the continuation of an established session",
4590 TCP_ACK,
4591 ),
4592 ("a FIN-ACK closing that session", TCP_FIN | TCP_ACK),
4593 ("an RST tearing it down", TCP_RST),
4594 // Go's test is `(flags & (SYN|ACK)) == SYN`, so a segment with neither bit set is a
4595 // non-SYN as well. Ported as it stands rather than tightened.
4596 ("a segment with no flags at all", 0x00),
4597 ] {
4598 assert!(
4599 admitted(&DenyAll, flows, v4(flags)),
4600 "{why} is admitted with no rule matching it"
4601 );
4602 assert!(
4603 admitted(&DenyAll, flows, v6(flags)),
4604 "{why} is admitted on IPv6 too (Go `runIn6` carries the same arm)"
4605 );
4606 }
4607
4608 // The negative that gives the carve-out its value: SYN set and ACK clear is Go's
4609 // `IsTCPSyn`, and it is the only way to open an inbound session.
4610 assert!(
4611 !admitted(&DenyAll, flows, v4(TCP_SYN)),
4612 "an inbound SYN with no matching rule is still dropped"
4613 );
4614 assert!(
4615 !admitted(&DenyAll, flows, v6(TCP_SYN)),
4616 "an inbound SYN with no matching rule is still dropped on IPv6"
4617 );
4618 // ...and it is admitted when a rule really does open the port, so the drop above is the
4619 // rules speaking and not the carve-out swallowing the packet.
4620 assert!(
4621 admitted(
4622 &acl(IPV4_FIXTURE_NET, &[6], 41234..=41234),
4623 flows,
4624 v4(TCP_SYN)
4625 ),
4626 "a rule opening the destination port admits the SYN"
4627 );
4628
4629 // The protocol number the carve-out is scoped to is the one the **base** IP header
4630 // declares, which is what Go's `runIn4`/`runIn6` switch dispatches on. etherparse walks an
4631 // IPv6 extension-header chain through to the real transport, so a non-SYN segment behind a
4632 // Routing header hands this code a TCP transport slice for a packet upstream filters as
4633 // protocol 43. Admitting it would make "prepend an extension header" a way past the rules.
4634 for ext in [43u8, 60] {
4635 assert!(
4636 !admitted(
4637 &DenyAll,
4638 flows,
4639 ipv6_with_prepended_ext_header(ext, &v6(TCP_ACK))
4640 ),
4641 "a non-SYN segment behind extension header {ext} is not TCP to the filter"
4642 );
4643 }
4644 }
4645
4646 /// Go's ICMP reply carve-out, on real bytes through the whole inbound filter: `runIn4`/`runIn6`
4647 /// accept an ICMP echo *response* or an ICMP *error* before any rule is consulted.
4648 ///
4649 /// ```text
4650 /// if q.IsEchoResponse() || q.IsError() {
4651 /// // ICMP responses are allowed.
4652 /// return Accept, "icmp response ok"
4653 /// } else if f.matches4.matchIPsOnly(q, f.srcIPHasCap) {
4654 /// // If any port is open to an IP, allow ICMP to it.
4655 /// return Accept, "icmp ok"
4656 /// }
4657 /// ```
4658 ///
4659 /// The reply to a ping this node sent is unsolicited as far as any ACL is concerned, and so is
4660 /// the Packet Too Big that path-MTU discovery needs; both were silently dropped under a policy
4661 /// that does not grant the peer inbound access back.
4662 ///
4663 /// The negative half is the `else if`, which this fork already had: ICMP that is *not* a
4664 /// response must still go to the rules and be matched IPs-only, never admitted as a "response".
4665 /// An echo request is the case that matters — it is how an inbound ICMP session starts.
4666 ///
4667 /// Ported from github.com/tailscale/tailscale `wgengine/filter/filter.go` (`runIn4`, `runIn6`)
4668 /// and `net/packet/packet.go` (`IsEchoResponse`, `IsError`) at
4669 /// `3945b82f8a9550b54c33e61d4ed2227862d53e8a`.
4670 #[test]
4671 fn inbound_icmp_responses_are_admitted_without_a_rule_but_requests_are_not() {
4672 let flows = &mut flowtrack::FlowCache::default();
4673 let v4 = |icmp_type, icmp_code| v4_packet(1, 0, false, &icmp_header(icmp_type, icmp_code));
4674 let v6 = |icmp_type, icmp_code| ipv6_packet(58, &icmp_header(icmp_type, icmp_code));
4675
4676 // Go `IsEchoResponse` and `IsError` on ICMPv4.
4677 for (why, icmp_type, icmp_code) in [
4678 ("an echo reply — the answer to our own ping", 0x00u8, 0u8),
4679 ("destination unreachable", 0x03, 1),
4680 ("time exceeded", 0x0b, 0),
4681 // Upstream's `ICMP4ParamProblem` is 0x12. IANA's Parameter Problem is 12 (0x0c) and
4682 // 0x12 is Address Mask Reply, so the constant names one message and holds another —
4683 // ported as it stands, because "fixing" it would admit type 12 traffic upstream drops.
4684 (
4685 "upstream's ICMP4ParamProblem constant, whatever IANA calls it",
4686 0x12,
4687 0,
4688 ),
4689 ] {
4690 assert!(
4691 admitted(&DenyAll, flows, v4(icmp_type, icmp_code)),
4692 "{why} is admitted with no rule matching it"
4693 );
4694 }
4695
4696 // ...and on ICMPv6, where the type numbers are entirely different and Packet Too Big is an
4697 // error class of its own.
4698 for (why, icmp_type, icmp_code) in [
4699 ("an ICMPv6 echo reply", 129u8, 0u8),
4700 ("ICMPv6 destination unreachable", 1, 3),
4701 (
4702 "ICMPv6 packet too big — what path-MTU discovery rides on",
4703 2,
4704 0,
4705 ),
4706 ("ICMPv6 time exceeded", 3, 0),
4707 ("ICMPv6 parameter problem", 4, 0),
4708 ] {
4709 assert!(
4710 admitted(&DenyAll, flows, v6(icmp_type, icmp_code)),
4711 "{why} is admitted with no rule matching it"
4712 );
4713 }
4714
4715 // The negatives. None of these is a "response", so each falls through to the rules — and a
4716 // deny-all filter says no.
4717 for (why, packet) in [
4718 (
4719 "an echo REQUEST is how an inbound session starts",
4720 v4(0x08, 0),
4721 ),
4722 (
4723 "an echo reply with a non-zero code is not one (Go tests the code)",
4724 v4(0x00, 1),
4725 ),
4726 (
4727 "IANA's Parameter Problem (12) is not upstream's constant (0x12)",
4728 v4(0x0c, 0),
4729 ),
4730 ("an ICMPv6 echo REQUEST", v6(128, 0)),
4731 ("an ICMPv6 echo reply with a non-zero code", v6(129, 1)),
4732 (
4733 "an ICMPv6 router advertisement is neither response nor error",
4734 v6(134, 0),
4735 ),
4736 ] {
4737 assert!(!admitted(&DenyAll, flows, packet), "{why}: still dropped");
4738 }
4739
4740 // And what "still faces the rules" means: Go's `matchIPsOnly`, which this fork already
4741 // ported. A rule that opens *any* port to this destination admits the echo request, ports
4742 // ignored — so the drop above is the deny-all ruleset speaking, not the decode failing.
4743 assert!(
4744 admitted(&acl(IPV4_FIXTURE_NET, &[1], 443..=443), flows, v4(0x08, 0)),
4745 "an echo request is matched IPs-only, so a port-scoped ICMP rule admits it"
4746 );
4747 }
4748
4749 /// The reply carve-outs sit inside Go's proto switch, and `pre()` runs *before* that switch. So
4750 /// the unconditional multicast and link-local drops outrank both of them: a SYN-ACK or an echo
4751 /// reply addressed somewhere `pre()` refuses is still dropped, however much of a reply it is.
4752 ///
4753 /// This pins the branch ordering, which is the only thing keeping the carve-outs from
4754 /// re-opening destinations upstream closes unconditionally.
4755 #[test]
4756 fn pre_rule_drops_outrank_the_reply_carve_outs() {
4757 let ip = |s: &str| s.parse::<std::net::IpAddr>().unwrap();
4758 let src = ip("100.64.0.9");
4759 let verdict = |proto, dst, l4| {
4760 inbound_filter_verdict(
4761 &AllowAll,
4762 &mut flowtrack::FlowCache::default(),
4763 proto,
4764 std::net::SocketAddr::new(src, 443),
4765 std::net::SocketAddr::new(dst, 41234),
4766 l4,
4767 None,
4768 )
4769 };
4770 let syn_ack = ts_packetfilter::L4Header::Tcp {
4771 flags: TCP_SYN | TCP_ACK,
4772 };
4773 let echo_reply = ts_packetfilter::L4Header::Icmp {
4774 icmp_type: 0x00,
4775 icmp_code: 0,
4776 };
4777
4778 // The controls: to an ordinary tailnet address both are admitted.
4779 assert!(verdict(IpProto::TCP, ip("100.64.0.1"), syn_ack));
4780 assert!(verdict(IpProto::ICMP, ip("100.64.0.1"), echo_reply));
4781
4782 for dst in ["224.0.0.1", "169.254.1.1"] {
4783 assert!(
4784 !verdict(IpProto::TCP, ip(dst), syn_ack),
4785 "a non-SYN segment to {dst} is still dropped by pre()"
4786 );
4787 assert!(
4788 !verdict(IpProto::ICMP, ip(dst), echo_reply),
4789 "an echo reply to {dst} is still dropped by pre()"
4790 );
4791 }
4792 // The one link-local address `pre()` allows through is still allowed through.
4793 assert!(
4794 verdict(IpProto::ICMP, ip("169.254.169.254"), echo_reply),
4795 "the cloud-metadata exception is unaffected"
4796 );
4797 }
4798
4799 /// A *first* IPv6 fragment is decoded past its Fragment extension header, TCP flags included —
4800 /// Go `decode6` reaches `q.TCPFlags = TCPFlag(sub[13])` through exactly the same switch a
4801 /// unfragmented segment takes — so the reply carve-out applies to it on the same terms.
4802 ///
4803 /// Without this a peer that source-fragments could not complete a handshake this node started
4804 /// under a restrictive ACL; with it wrong, a fragmented SYN would slip past the rules. Both
4805 /// directions are asserted, on real bytes and on the classification itself.
4806 #[test]
4807 fn ipv6_first_fragment_carries_its_tcp_flags_to_the_reply_carve_out() {
4808 let flows = &mut flowtrack::FlowCache::default();
4809 let frag = |flags| ipv6_fragment_packet(6, 0, true, &tcp_header(443, 41234, flags));
4810
4811 assert!(
4812 admitted(&DenyAll, flows, frag(TCP_SYN | TCP_ACK)),
4813 "a fragmented SYN-ACK takes the non-SYN carve-out like an unfragmented one"
4814 );
4815 assert!(
4816 !admitted(&DenyAll, flows, frag(TCP_SYN)),
4817 "a fragmented SYN does not"
4818 );
4819
4820 // The classification carries the byte, not merely the verdict — this is the value the
4821 // verdict reads, and the ports are still Go's `sub[0:2]`/`sub[2:4]` beside it.
4822 assert_eq!(
4823 decode6_first_fragment(IpProto::TCP, &tcp_header(443, 41234, TCP_SYN | TCP_ACK)),
4824 Ipv6Fragment::First {
4825 proto: IpProto::TCP,
4826 src_port: 443,
4827 dst_port: 41234,
4828 l4: ts_packetfilter::L4Header::Tcp {
4829 flags: TCP_SYN | TCP_ACK
4830 },
4831 },
4832 "`decode6` reads the flags byte past the Fragment header"
4833 );
4834 }
4835
4836 /// A dropped inbound packet's four-tuple, as `filter_inbound_from_peer` hands it to
4837 /// [`tsmp_reject_for_drop`]: the fixture peer dialling this node's SSH port.
4838 fn dropped_syn_tuple() -> (std::net::SocketAddr, std::net::SocketAddr) {
4839 (
4840 std::net::SocketAddr::new(IPV4_FIXTURE_SRC.into(), 41234),
4841 std::net::SocketAddr::new(IPV4_FIXTURE_DST.into(), 22),
4842 )
4843 }
4844
4845 /// The happy path of the send half, at the function Go's guard lives in: a dropped IPv4 TCP SYN
4846 /// produces a rejected-connection message addressed back at the dialer, carrying the four-tuple
4847 /// with the *connection's* own direction and `RejectedDueToACLs`.
4848 ///
4849 /// The assertion runs the emitted bytes back through the production parser, which is the far
4850 /// side's view of them — the peer has to read out the flow it opened, not the one we saw.
4851 #[test]
4852 fn an_acl_dropped_syn_produces_a_reject_addressed_back_to_the_dialer() {
4853 let (src, dst) = dropped_syn_tuple();
4854
4855 let bytes = tsmp_reject_for_drop(
4856 IpProto::TCP,
4857 src,
4858 dst,
4859 ts_packetfilter::L4Header::Tcp { flags: TCP_SYN },
4860 false,
4861 RejectConfig::default(),
4862 )
4863 .expect("a dropped IPv4 TCP SYN must be answered");
4864
4865 let reject = ts_packet::tsmp::TailscaleRejectedHeader::parse(&bytes)
4866 .expect("what we emit must parse as a rejected-connection message");
4867
4868 // Go `IPSrc: p.Dst.Addr(), IPDst: p.Src.Addr()`: the reply travels back the way the SYN
4869 // came, so the IP pair is reversed...
4870 assert_eq!(reject.ip_src, dst.ip(), "sourced from the address dialled");
4871 assert_eq!(reject.ip_dst, src.ip(), "addressed to the dialer");
4872 // ...while the four-tuple keeps the rejected connection's own direction, which is what lets
4873 // the dialer match it against the flow it opened.
4874 assert_eq!(reject.src, src);
4875 assert_eq!(reject.dst, dst);
4876 assert_eq!(reject.proto, IPPROTO_TCP_BYTE);
4877 assert_eq!(reject.reason, ts_packet::tsmp::RejectReason::ACLS);
4878 assert!(!reject.maybe_broken, "an ACL deny is a terminal verdict");
4879 }
4880
4881 /// Go: `if t.filter.ShieldsUp() { rj.Reason = packet.RejectedDueToShieldsUp }`. The two reasons
4882 /// render differently in a peer's connection history, so a shields-up deny must not be reported
4883 /// as an ACL deny.
4884 #[test]
4885 fn shields_up_is_a_distinct_reject_reason() {
4886 let (src, dst) = dropped_syn_tuple();
4887 let syn = ts_packetfilter::L4Header::Tcp { flags: TCP_SYN };
4888
4889 let reason = |shields_up| {
4890 ts_packet::tsmp::TailscaleRejectedHeader::parse(
4891 &tsmp_reject_for_drop(
4892 IpProto::TCP,
4893 src,
4894 dst,
4895 syn,
4896 shields_up,
4897 RejectConfig::default(),
4898 )
4899 .expect("emitted"),
4900 )
4901 .expect("parses")
4902 .reason
4903 };
4904
4905 assert_eq!(reason(true), ts_packet::tsmp::RejectReason::SHIELDS_UP);
4906 assert_eq!(reason(false), ts_packet::tsmp::RejectReason::ACLS);
4907 }
4908
4909 /// Everything Go stays **silent** about. Each of these is a packet the ACL drops just the same;
4910 /// the drop is unchanged and only the reply is withheld, which is what makes them the part of
4911 /// the port worth testing.
4912 #[test]
4913 fn most_drops_produce_no_reject_at_all() {
4914 let (src, dst) = dropped_syn_tuple();
4915 let syn = ts_packetfilter::L4Header::Tcp { flags: TCP_SYN };
4916 let v6_src = std::net::SocketAddr::new(std::net::IpAddr::V6(IPV6_FIXTURE_SRC), 41234);
4917 let v6_dst = std::net::SocketAddr::new(std::net::IpAddr::V6(IPV6_FIXTURE_DST), 22);
4918
4919 for (name, proto, src, dst, l4, cfg) in [
4920 // Go `p.TCPFlags&packet.TCPSyn != 0`: a segment with no SYN bit is not a connection
4921 // being opened, so there is nothing to refuse.
4922 (
4923 "a non-SYN segment",
4924 IpProto::TCP,
4925 src,
4926 dst,
4927 ts_packetfilter::L4Header::Tcp {
4928 flags: TCP_ACK | TCP_FIN,
4929 },
4930 RejectConfig::default(),
4931 ),
4932 (
4933 "a RST",
4934 IpProto::TCP,
4935 src,
4936 dst,
4937 ts_packetfilter::L4Header::Tcp { flags: TCP_RST },
4938 RejectConfig::default(),
4939 ),
4940 // Go `p.IPProto == ipproto.TCP`: a datagram protocol has no SYN and no connection.
4941 (
4942 "a UDP datagram",
4943 IpProto::UDP,
4944 src,
4945 dst,
4946 ts_packetfilter::L4Header::Unknown,
4947 RejectConfig::default(),
4948 ),
4949 // A UDP datagram whose 14th payload byte happens to have the SYN bit pattern must not
4950 // be read as a TCP header: the protocol number decides, as it does in Go's guard.
4951 (
4952 "a UDP datagram carrying SYN-shaped bytes",
4953 IpProto::UDP,
4954 src,
4955 dst,
4956 syn,
4957 RejectConfig::default(),
4958 ),
4959 (
4960 "an ICMP echo request",
4961 IpProto::ICMP,
4962 src,
4963 dst,
4964 ts_packetfilter::L4Header::Icmp {
4965 icmp_type: 8,
4966 icmp_code: 0,
4967 },
4968 RejectConfig::default(),
4969 ),
4970 // Go `p.IPVersion == 4`: an IPv6 drop is silent, even though the message itself is
4971 // family-agnostic and `TailscaleRejectedHeader::marshal` would happily emit one.
4972 (
4973 "an IPv6 SYN",
4974 IpProto::TCP,
4975 v6_src,
4976 v6_dst,
4977 syn,
4978 RejectConfig::default(),
4979 ),
4980 // A packet whose L4 header was never decoded — a first IPv4 fragment, say — is not
4981 // known to be a SYN. Go reaches the same place: a header it could not decode leaves
4982 // `q.TCPFlags` zero.
4983 (
4984 "a SYN whose TCP header was never decoded",
4985 IpProto::TCP,
4986 src,
4987 dst,
4988 ts_packetfilter::L4Header::Unknown,
4989 RejectConfig::default(),
4990 ),
4991 // Go's peerAPI carve-out, which flips an ACL-refused peerAPI SYN back to `Accept`
4992 // before the reject block runs. Same outcome reached here by withholding the reply.
4993 (
4994 "a SYN to the peerAPI port",
4995 IpProto::TCP,
4996 src,
4997 dst,
4998 syn,
4999 RejectConfig {
5000 disabled: false,
5001 peerapi_port: Some(dst.port()),
5002 },
5003 ),
5004 // Go `!t.disableTSMPRejected`, the node-wide off switch.
5005 (
5006 "any SYN at all, with rejects disabled",
5007 IpProto::TCP,
5008 src,
5009 dst,
5010 syn,
5011 RejectConfig {
5012 disabled: true,
5013 peerapi_port: None,
5014 },
5015 ),
5016 ] {
5017 assert!(
5018 tsmp_reject_for_drop(proto, src, dst, l4, false, cfg).is_none(),
5019 "{name} must be dropped silently"
5020 );
5021 }
5022
5023 // Control: with the peerAPI on some *other* port, the same SYN is answered — the carve-out
5024 // is scoped to the port, not a blanket mute.
5025 assert!(
5026 tsmp_reject_for_drop(
5027 IpProto::TCP,
5028 src,
5029 dst,
5030 syn,
5031 false,
5032 RejectConfig {
5033 disabled: false,
5034 peerapi_port: Some(dst.port() + 1),
5035 },
5036 )
5037 .is_some(),
5038 "a SYN to a port that is not the peerAPI's is still answered"
5039 );
5040 }
5041
5042 /// The send half through the real filter step, on real bytes: an ACL that denies everything
5043 /// drops the SYN *and* queues a reject for the peer that sent it, while the packets Go stays
5044 /// silent about queue nothing. This is the assertion that the guard is wired into the drop
5045 /// path at all, not merely correct in isolation.
5046 #[test]
5047 fn the_filter_step_queues_a_reject_only_for_a_dropped_ipv4_syn() {
5048 let peer = PeerId(7);
5049
5050 // Returns `(survived, harvest)`: whether the ACL admitted the packet, and what the filter
5051 // step produced alongside it.
5052 let run = |packet: Vec<u8>| {
5053 let mut packets = vec![PacketMut::from(packet)];
5054 let mut harvest = InboundHarvest::default();
5055 filter_inbound_from_peer(
5056 &DenyAll,
5057 &mut flowtrack::FlowCache::default(),
5058 peer,
5059 &mut packets,
5060 RejectConfig::default(),
5061 &mut harvest,
5062 );
5063 (!packets.is_empty(), harvest)
5064 };
5065
5066 let (survived, harvest) = run(v4_packet(6, 0, false, &tcp_header(41234, 22, TCP_SYN)));
5067 assert!(!survived, "the deny-all ACL drops the SYN");
5068 assert_eq!(
5069 harvest.rejects_to_send.len(),
5070 1,
5071 "a dropped SYN is answered"
5072 );
5073 assert_eq!(harvest.rejects_to_send[0].0, peer, "back to the sender");
5074 let reject =
5075 ts_packet::tsmp::TailscaleRejectedHeader::parse(harvest.rejects_to_send[0].1.as_ref())
5076 .expect("the queued packet is a rejected-connection message");
5077 assert_eq!(reject.src.port(), 41234);
5078 assert_eq!(reject.dst.port(), 22);
5079 assert_eq!(reject.reason, ts_packet::tsmp::RejectReason::ACLS);
5080
5081 // A segment that is not a SYN never produces one. Under this deny-all ACL it is in fact
5082 // *admitted*, by the "tcp non-syn" reply carve-out that runs ahead of the rules — which is
5083 // the more useful assertion anyway: the reject path must not fire on a packet that was
5084 // never dropped in the first place.
5085 let (survived, harvest) = run(v4_packet(6, 0, false, &tcp_header(41234, 22, TCP_RST)));
5086 assert!(survived, "Go's 'tcp non-syn' carve-out admits it");
5087 assert!(harvest.rejects_to_send.is_empty(), "and answers nothing");
5088
5089 for (name, packet) in [
5090 ("a UDP datagram", v4_packet(17, 0, false, &udp_header(22))),
5091 (
5092 "an IPv6 SYN",
5093 ipv6_packet(6, &tcp_header(41234, 22, TCP_SYN)),
5094 ),
5095 ] {
5096 let (survived, harvest) = run(packet);
5097 assert!(!survived, "{name} is dropped by the deny-all ACL");
5098 assert!(
5099 harvest.rejects_to_send.is_empty(),
5100 "{name} must be dropped in silence"
5101 );
5102 }
5103 }
5104
5105 /// The peerAPI ordering, through the real filter step: a SYN to the peerAPI port is dropped by
5106 /// the ACL exactly as before — this fork has no L7 carve-out that admits it — but the peer is
5107 /// told nothing, because upstream's carve-out has already turned that packet into an `Accept`
5108 /// by the time its reject block runs.
5109 #[test]
5110 fn a_peerapi_syn_is_dropped_without_a_reject() {
5111 let mut packets = vec![PacketMut::from(v4_packet(
5112 6,
5113 0,
5114 false,
5115 &tcp_header(41234, 8089, TCP_SYN),
5116 ))];
5117 let mut harvest = InboundHarvest::default();
5118 filter_inbound_from_peer(
5119 &DenyAll,
5120 &mut flowtrack::FlowCache::default(),
5121 PeerId(7),
5122 &mut packets,
5123 RejectConfig {
5124 disabled: false,
5125 peerapi_port: Some(8089),
5126 },
5127 &mut harvest,
5128 );
5129
5130 assert!(packets.is_empty(), "the verdict on the packet is unchanged");
5131 assert!(
5132 harvest.rejects_to_send.is_empty(),
5133 "a peerAPI SYN must never produce a reject"
5134 );
5135 }
5136
5137 /// The receive half through the real filter step: a peer's rejected-connection message is
5138 /// consumed — harvested for the embedder and **dropped**, never handed to the local stack,
5139 /// which has no handler for IP protocol 99 (Go `filter.DropSilently`).
5140 ///
5141 /// The ACL denies everything, so the packet surviving could only have come from the TSMP
5142 /// bypass; asserting it does *not* survive is the whole point.
5143 #[test]
5144 fn a_received_reject_is_consumed_and_not_delivered() {
5145 let peer = PeerId(7);
5146
5147 // The bytes a Go peer puts on the wire when its ACL refuses our dial: it marshals from its
5148 // own side, we read it from ours.
5149 let wire = ts_packet::tsmp::TailscaleRejectedHeader {
5150 ip_src: IPV4_FIXTURE_SRC.into(),
5151 ip_dst: IPV4_FIXTURE_DST.into(),
5152 src: std::net::SocketAddr::new(IPV4_FIXTURE_DST.into(), 41234),
5153 dst: std::net::SocketAddr::new(IPV4_FIXTURE_SRC.into(), 22),
5154 proto: IPPROTO_TCP_BYTE,
5155 reason: ts_packet::tsmp::RejectReason::ACLS,
5156 maybe_broken: false,
5157 }
5158 .marshal()
5159 .expect("marshals");
5160
5161 let mut packets = vec![PacketMut::from(wire)];
5162 let mut harvest = InboundHarvest::default();
5163 filter_inbound_from_peer(
5164 &DenyAll,
5165 &mut flowtrack::FlowCache::default(),
5166 peer,
5167 &mut packets,
5168 RejectConfig::default(),
5169 &mut harvest,
5170 );
5171
5172 assert!(
5173 packets.is_empty(),
5174 "a reject must be consumed, not delivered to the local stack"
5175 );
5176 assert_eq!(harvest.rejected_flows.len(), 1);
5177 assert_eq!(harvest.rejected_flows[0].0, peer);
5178 let reject = harvest.rejected_flows[0].1;
5179 assert_eq!(
5180 reject.src,
5181 std::net::SocketAddr::new(IPV4_FIXTURE_DST.into(), 41234),
5182 "src is OUR address and ephemeral port — the flow we opened"
5183 );
5184 assert_eq!(
5185 reject.dst,
5186 std::net::SocketAddr::new(IPV4_FIXTURE_SRC.into(), 22)
5187 );
5188 assert_eq!(reject.reason, ts_packet::tsmp::RejectReason::ACLS);
5189 assert!(!reject.maybe_broken);
5190 assert!(
5191 harvest.rejects_to_send.is_empty(),
5192 "consuming a reject must not answer it with another one"
5193 );
5194
5195 // The `MaybeBroken` bit reaches the embedder intact: Go treats such a rejection as
5196 // non-terminal (it marks the flow problematic instead of removing it), so flattening the
5197 // bit would turn a transient problem into a hard failure.
5198 let broken = ts_packet::tsmp::TailscaleRejectedHeader {
5199 ip_src: IPV4_FIXTURE_SRC.into(),
5200 ip_dst: IPV4_FIXTURE_DST.into(),
5201 src: std::net::SocketAddr::new(IPV4_FIXTURE_DST.into(), 41234),
5202 dst: std::net::SocketAddr::new(IPV4_FIXTURE_SRC.into(), 22),
5203 proto: IPPROTO_TCP_BYTE,
5204 reason: ts_packet::tsmp::RejectReason::IP_FORWARDING,
5205 maybe_broken: true,
5206 }
5207 .marshal()
5208 .expect("marshals");
5209 let mut packets = vec![PacketMut::from(broken)];
5210 let mut harvest = InboundHarvest::default();
5211 filter_inbound_from_peer(
5212 &DenyAll,
5213 &mut flowtrack::FlowCache::default(),
5214 peer,
5215 &mut packets,
5216 RejectConfig::default(),
5217 &mut harvest,
5218 );
5219 assert!(packets.is_empty());
5220 assert!(harvest.rejected_flows[0].1.maybe_broken);
5221 assert_eq!(
5222 harvest.rejected_flows[0].1.reason,
5223 ts_packet::tsmp::RejectReason::IP_FORWARDING
5224 );
5225 }
5226
5227 /// A peer cannot drive the default-level log with rejected-connection messages.
5228 ///
5229 /// Go logs `open-conn-track` only for a flow sitting in its pending-open table, so its volume
5230 /// is bounded by the dials *this* node made. This fork keeps no such table and logs every
5231 /// well-formed reject it can attribute to a peer, which bounds the volume by what the peer
5232 /// chooses to send instead — nothing here matches a pending flow and nothing rate-limits it.
5233 /// At `info!` that is an authenticated peer holding the operator's default-level log open;
5234 /// `debug!` keeps the diagnostic without putting it there.
5235 ///
5236 /// The record itself must keep flowing regardless: `rejected_flows` is the path the embedder
5237 /// uses to do the pending-open match this layer cannot, so quieting the log must not quiet it.
5238 #[test]
5239 #[tracing_test::traced_test]
5240 fn a_flood_of_rejects_stays_off_the_default_log_level() {
5241 let peer = PeerId(7);
5242 // More than a real flow-refusal burst, few enough to keep the test cheap: what matters is
5243 // that the count of default-level lines does not track it.
5244 const FLOOD: usize = 32;
5245
5246 let wire = ts_packet::tsmp::TailscaleRejectedHeader {
5247 ip_src: IPV4_FIXTURE_SRC.into(),
5248 ip_dst: IPV4_FIXTURE_DST.into(),
5249 src: std::net::SocketAddr::new(IPV4_FIXTURE_DST.into(), 41234),
5250 dst: std::net::SocketAddr::new(IPV4_FIXTURE_SRC.into(), 22),
5251 proto: IPPROTO_TCP_BYTE,
5252 reason: ts_packet::tsmp::RejectReason::ACLS,
5253 maybe_broken: false,
5254 }
5255 .marshal()
5256 .expect("marshals");
5257
5258 let mut packets: Vec<PacketMut> =
5259 (0..FLOOD).map(|_| PacketMut::from(wire.clone())).collect();
5260 let mut harvest = InboundHarvest::default();
5261 filter_inbound_from_peer(
5262 &DenyAll,
5263 &mut flowtrack::FlowCache::default(),
5264 peer,
5265 &mut packets,
5266 RejectConfig::default(),
5267 &mut harvest,
5268 );
5269
5270 assert!(packets.is_empty(), "every reject is consumed");
5271 assert_eq!(
5272 harvest.rejected_flows.len(),
5273 FLOOD,
5274 "the embedder must still receive every record — quieting the log is not dropping it"
5275 );
5276
5277 logs_assert(|lines: &[&str]| {
5278 let tracked: Vec<&&str> = lines
5279 .iter()
5280 .filter(|l| l.contains("open-conn-track"))
5281 .collect();
5282 if tracked.len() != FLOOD {
5283 return Err(format!(
5284 "expected {FLOOD} open-conn-track lines at debug, got {}",
5285 tracked.len()
5286 ));
5287 }
5288 // `INFO` and above is what an operator sees by default; a peer must not reach it.
5289 let loud: Vec<&&&str> = tracked.iter().filter(|l| !l.contains(" DEBUG ")).collect();
5290 if let Some(first) = loud.first() {
5291 return Err(format!(
5292 "{} of {FLOOD} peer-driven reject line(s) above debug, e.g. {first}",
5293 loud.len()
5294 ));
5295 }
5296 Ok(())
5297 });
5298 }
5299
5300 /// A *fragmented* rejected-connection message is refused before the ACL ever sees it, and is
5301 /// not consumed either: Go's `decode4` demotes a first TSMP fragment with More-Fragments set to
5302 /// `ipproto.Unknown`, which `pre()` drops, and classifies a later fragment as
5303 /// `ipproto.Fragment`. Without the whole message in hand it cannot be a valid control packet,
5304 /// and half a reject must never be read as a flow being refused.
5305 #[test]
5306 fn a_fragmented_reject_is_refused_before_the_acl() {
5307 let body = {
5308 let mut b = vec![
5309 ts_packet::tsmp::TSMP_TYPE_REJECTED_CONN,
5310 IPPROTO_TCP_BYTE,
5311 b'A',
5312 ];
5313 b.extend_from_slice(&41234u16.to_be_bytes());
5314 b.extend_from_slice(&22u16.to_be_bytes());
5315 b.push(0);
5316 b
5317 };
5318
5319 // Control: unfragmented, the same body is consumed as a reject.
5320 let mut packets = vec![PacketMut::from(v4_packet(99, 0, false, &body))];
5321 let mut harvest = InboundHarvest::default();
5322 filter_inbound_from_peer(
5323 &DenyAll,
5324 &mut flowtrack::FlowCache::default(),
5325 PeerId(7),
5326 &mut packets,
5327 RejectConfig::default(),
5328 &mut harvest,
5329 );
5330 assert!(packets.is_empty() && harvest.rejected_flows.len() == 1);
5331
5332 // A first fragment with More-Fragments set, and a later fragment. Neither is parsed as a
5333 // reject. What happens to the bytes afterwards is the fragment rule this tree already
5334 // had, unchanged: the first fragment is demoted to `unknown` and dropped, the later
5335 // fragment takes Go's stateless pass-through (`pre()`'s `case ipproto.Fragment: Accept`).
5336 for (name, offset, more, delivered) in [
5337 ("first fragment, MF set", 0, true, false),
5338 ("later fragment", MIN_FRAG_BLKS, false, true),
5339 ] {
5340 let mut packets = vec![PacketMut::from(v4_packet(99, offset, more, &body))];
5341 let mut harvest = InboundHarvest::default();
5342 filter_inbound_from_peer(
5343 &DenyAll,
5344 &mut flowtrack::FlowCache::default(),
5345 PeerId(7),
5346 &mut packets,
5347 RejectConfig::default(),
5348 &mut harvest,
5349 );
5350 assert!(
5351 harvest.rejected_flows.is_empty(),
5352 "{name}: half a reject is not a rejected flow"
5353 );
5354 assert_eq!(
5355 packets.len(),
5356 usize::from(delivered),
5357 "{name}: the existing fragment rule decides what happens to the bytes"
5358 );
5359 }
5360 }
5361
5362 /// Both halves meeting over a real WireGuard session, which is the only test that proves the
5363 /// reject is actually *sent*: A dials B, B's ACL refuses the SYN, and A learns why.
5364 ///
5365 /// This engine is always the dialing client, so A's side is the one that matters day to day —
5366 /// without it a dial into a peer's ACL drop waits out a TCP timeout with no reason anywhere.
5367 /// B's side is the peer-observable one: a real Go node dialling into our ACL drop expects to be
5368 /// told, and was not.
5369 #[test]
5370 fn a_refused_dial_is_answered_over_the_wire_and_understood() {
5371 let underlay: UnderlayTransportId = 0.into();
5372 let wg_peer = ts_tunnel::PeerId(1);
5373 let peer = PeerId(1);
5374 let a_addr = std::net::IpAddr::from(IPV4_FIXTURE_SRC);
5375 let b_addr = std::net::IpAddr::from(IPV4_FIXTURE_DST);
5376
5377 let (a_static, b_static) = (NodeKeyPair::new(), NodeKeyPair::new());
5378 let (mut a, mut b) = (
5379 DataPlane::new(a_static.clone()),
5380 DataPlane::new(b_static.clone()),
5381 );
5382
5383 for (dp, key, remote) in [
5384 (&mut a, b_static.public, b_addr),
5385 (&mut b, a_static.public, a_addr),
5386 ] {
5387 dp.wireguard.upsert_peer(
5388 wg_peer,
5389 ts_tunnel::PeerConfig {
5390 key,
5391 psk: [0u8; 32].into(),
5392 persistent_keepalive_interval: None,
5393 },
5394 );
5395 dp.ur_out.table.insert(peer, underlay);
5396 // Each side attributes the other's tailnet address to the WireGuard peer carrying it,
5397 // as the runtime's source filter does.
5398 let mut src_filter = ts_bart::Table::default();
5399 src_filter.insert(ipnet::IpNet::from(remote), peer);
5400 dp.src_filter_in = Arc::new(src_filter);
5401 }
5402
5403 // B refuses everything: this is the ACL drop the message exists to explain.
5404 b.packet_filter = Arc::new(DenyAll);
5405
5406 let take = |out: HashMap<(UnderlayTransportId, PeerId), Vec<PacketMut>>| {
5407 out.into_values().flatten().collect::<Vec<_>>()
5408 };
5409
5410 // A dials B. The SYN rides the handshake this send starts.
5411 let syn = PacketMut::from(v4_packet(6, 0, false, &tcp_header(41234, 22, TCP_SYN)));
5412 let init = a
5413 .wireguard
5414 .send([(wg_peer, vec![syn])])
5415 .to_peers
5416 .remove(&wg_peer)
5417 .expect("handshake initiation");
5418 let resp = take(b.process_inbound(init).to_peers);
5419 let from_a = take(a.process_inbound(resp).to_peers);
5420
5421 // B drops the SYN on its ACL and answers with a rejected-connection message.
5422 let at_b = b.process_inbound(from_a);
5423 assert!(
5424 at_b.to_local.values().all(|v| v.is_empty()),
5425 "the SYN itself must not reach B's local stack"
5426 );
5427 let to_a = take(at_b.to_peers);
5428 assert!(
5429 !to_a.is_empty(),
5430 "B must tell A why it dropped the connection"
5431 );
5432
5433 // A consumes it: it learns the flow was refused and the message never reaches its stack.
5434 let at_a = a.process_inbound(to_a);
5435 assert!(
5436 at_a.to_local.values().all(|v| v.is_empty()),
5437 "the reject must be consumed, not delivered to A's local stack"
5438 );
5439 assert_eq!(at_a.rejected_flows.len(), 1, "A must learn of the refusal");
5440 let (from_peer, reject) = at_a.rejected_flows[0];
5441 assert_eq!(from_peer, peer);
5442 assert_eq!(
5443 reject.src,
5444 std::net::SocketAddr::new(a_addr, 41234),
5445 "the flow A opened, from A's own address and ephemeral port"
5446 );
5447 assert_eq!(reject.dst, std::net::SocketAddr::new(b_addr, 22));
5448 assert_eq!(reject.proto, IPPROTO_TCP_BYTE);
5449 assert_eq!(reject.reason, ts_packet::tsmp::RejectReason::ACLS);
5450 assert!(!reject.maybe_broken);
5451
5452 // And A does not answer the reject with a reject of its own — that would be a loop between
5453 // two nodes that both deny.
5454 assert!(
5455 take(at_a.to_peers).is_empty(),
5456 "consuming a reject must put nothing back on the wire"
5457 );
5458 }
5459}