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