Skip to main content

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