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                },
3758            );
3759            dp.ur_out.table.insert(peer, underlay);
3760        }
3761
3762        // Only A knows how to advertise: its own disco key, its own address, and B's address.
3763        a.disco_advertisement = Some(Arc::new(advertisement_state(
3764            peer,
3765            AdvertisementTarget {
3766                node_addr: b_addr,
3767                wireguard_only: false,
3768            },
3769        )));
3770
3771        // B attributes A's tailnet address to the WireGuard peer that carries it, as the runtime's
3772        // source filter does — without that, B drops the advertisement before parsing it.
3773        let mut src_filter = ts_bart::Table::default();
3774        src_filter.insert(ipnet::IpNet::from(a_addr), peer);
3775        b.src_filter_in = Arc::new(src_filter);
3776
3777        // Drive the handshake. Only the initiation is kicked off directly (the dataplane starts one
3778        // from routed outbound traffic, which is not what this test is about); everything after it
3779        // goes through `process_inbound`, the path under test.
3780        let take = |out: HashMap<(UnderlayTransportId, PeerId), Vec<PacketMut>>| {
3781            out.into_values().flatten().collect::<Vec<_>>()
3782        };
3783        let init = a
3784            .wireguard
3785            .send([(wg_peer, vec![PacketMut::from(&b"hello"[..])])])
3786            .to_peers
3787            .remove(&wg_peer)
3788            .expect("handshake initiation");
3789
3790        let resp = take(b.process_inbound(init).to_peers);
3791        assert!(!resp.is_empty(), "B must answer the handshake initiation");
3792
3793        // A completes the handshake. Its session is now current, so alongside the queued data it
3794        // emits the advertisement.
3795        let from_a = take(a.process_inbound(resp).to_peers);
3796        assert_eq!(
3797            from_a.len(),
3798            2,
3799            "A must emit the queued data AND its disco-key advertisement"
3800        );
3801
3802        // B learns A's disco key from it, and the advertisement itself is consumed rather than
3803        // delivered to B's local stack.
3804        let inbound = b.process_inbound(from_a);
3805        assert_eq!(
3806            inbound
3807                .learned_disco_keys
3808                .iter()
3809                .map(|(peer, advert)| (*peer, advert.key))
3810                .collect::<Vec<_>>(),
3811            vec![(peer, SELF_DISCO_KEY)],
3812            "B must learn exactly the disco key A holds, attributed to A's wireguard peer"
3813        );
3814        assert!(
3815            inbound.to_peers.is_empty(),
3816            "B has no advertisement state, so it advertises nothing back"
3817        );
3818    }
3819
3820    /// Order regression: the advertisement must LEAD the traffic the same establishment released,
3821    /// not trail it.
3822    ///
3823    /// wireguard-go hands a priority message straight to the peer's *outbound* queue
3824    /// (`SendPriorityMessage` → `queueOutboundIfRunning`) and runs it before the flush that
3825    /// follows at both call sites — `peer.SendPriorityMessage()` ahead of `peer.SendKeepalive()`
3826    /// on the initiator and ahead of `peer.SendStagedPackets()` on the responder
3827    /// (`device/receive.go`) — so the advertisement is the first thing on the wire once a keypair
3828    /// becomes current. In this tree the flush has already happened inside `Endpoint::recv` by the
3829    /// time the advertisement exists, so `process_inbound` has to splice it in front; appending it
3830    /// would put it behind up to `MAX_QUEUED_PER_PEER` packets of queued traffic.
3831    ///
3832    /// The order is read off B's *decrypted* stream — its capture tee, which sees every inbound
3833    /// packet before any filtering — so what is pinned is the order the peer actually observes,
3834    /// not the order of a local vector.
3835    #[test]
3836    fn the_advertisement_leads_the_traffic_released_by_the_same_establishment() {
3837        let underlay: UnderlayTransportId = 0.into();
3838        let wg_peer = ts_tunnel::PeerId(1);
3839        let peer = PeerId(1);
3840        let a_addr = std::net::IpAddr::from([100, 64, 0, 1]);
3841        let b_addr = std::net::IpAddr::from([100, 64, 0, 2]);
3842
3843        let (a_static, b_static) = (NodeKeyPair::new(), NodeKeyPair::new());
3844        let (mut a, mut b) = (
3845            DataPlane::new(a_static.clone()),
3846            DataPlane::new(b_static.clone()),
3847        );
3848
3849        for (dp, key) in [(&mut a, b_static.public), (&mut b, a_static.public)] {
3850            dp.wireguard.upsert_peer(
3851                wg_peer,
3852                ts_tunnel::PeerConfig {
3853                    key,
3854                    psk: [0u8; 32].into(),
3855                    persistent_keepalive_interval: None,
3856                },
3857            );
3858            dp.ur_out.table.insert(peer, underlay);
3859        }
3860
3861        a.disco_advertisement = Some(Arc::new(advertisement_state(
3862            peer,
3863            AdvertisementTarget {
3864                node_addr: b_addr,
3865                wireguard_only: false,
3866            },
3867        )));
3868
3869        let mut src_filter = ts_bart::Table::default();
3870        src_filter.insert(ipnet::IpNet::from(a_addr), peer);
3871        b.src_filter_in = Arc::new(src_filter);
3872
3873        // Everything B decrypts, in arrival order, before any filtering runs.
3874        let recorded: CaptureLog = Arc::new(Mutex::new(Vec::new()));
3875        let sink = recorded.clone();
3876        b.capture = Some(Arc::new(move |path: CapturePath, bytes: &[u8]| {
3877            sink.lock().unwrap().push((path, bytes.to_vec()));
3878        }));
3879
3880        let take = |out: HashMap<(UnderlayTransportId, PeerId), Vec<PacketMut>>| {
3881            out.into_values().flatten().collect::<Vec<_>>()
3882        };
3883
3884        // Traffic for a peer with no session yet: it stages, and a handshake starts.
3885        const QUEUED: &[u8] = b"staged while the session was still coming up";
3886        let init = a
3887            .wireguard
3888            .send([(wg_peer, vec![PacketMut::from(QUEUED)])])
3889            .to_peers
3890            .remove(&wg_peer)
3891            .expect("handshake initiation");
3892        let resp = take(b.process_inbound(init).to_peers);
3893
3894        // A's keypair becomes current here, which both flushes the staged packet and produces the
3895        // advertisement — the batch whose order is under test.
3896        let from_a = take(a.process_inbound(resp).to_peers);
3897        assert_eq!(
3898            from_a.len(),
3899            2,
3900            "A must emit the queued data AND its disco-key advertisement"
3901        );
3902
3903        // Hand them to B in exactly the order A produced them.
3904        let learned = b.process_inbound(from_a).learned_disco_keys;
3905        assert_eq!(
3906            learned
3907                .iter()
3908                .map(|(peer, advert)| (*peer, advert.key))
3909                .collect::<Vec<_>>(),
3910            vec![(peer, SELF_DISCO_KEY)],
3911            "B must still learn A's disco key"
3912        );
3913
3914        let advertisement = ts_packet::tsmp::DiscoKeyAdvertisement {
3915            src: a_addr,
3916            dst: b_addr,
3917            key: SELF_DISCO_KEY,
3918        }
3919        .marshal()
3920        .expect("a v4 advertisement between two v4 addresses marshals");
3921
3922        let captured = recorded.lock().unwrap();
3923        let from_peer = captured
3924            .iter()
3925            .filter(|(path, _)| *path == CapturePath::FromPeer)
3926            .map(|(_, bytes)| bytes.as_slice())
3927            .collect::<Vec<_>>();
3928        assert_eq!(from_peer.len(), 2, "B must decrypt both of A's packets");
3929        // The send path zero-pads each payload up to a 16-byte boundary and the receiver delivers
3930        // it with that padding intact (see `session::PADDING_MULTIPLE`), so compare on the leading
3931        // bytes rather than for equality.
3932        assert!(
3933            from_peer[0].starts_with(&advertisement),
3934            "the advertisement must reach the peer FIRST, ahead of the traffic the same \
3935             establishment released"
3936        );
3937        assert!(
3938            from_peer[1].starts_with(QUEUED),
3939            "the queued traffic follows the advertisement"
3940        );
3941    }
3942
3943    /// Behavioral guard: an installed capture hook MUST be invoked with `CapturePath::FromLocal`
3944    /// and the exact packet bytes for every outbound packet. The tee sits at the top of
3945    /// `process_outbound`, before `or_out.route` consumes the packets, so it fires regardless of
3946    /// whether a wireguard peer exists (an empty router just drops the routed packets afterward).
3947    /// This is the only end-to-end guard that the dataplane capture tee actually fires; a refactor
3948    /// that drops the tee would leave every byte-layout test green.
3949    #[test]
3950    fn capture_hook_fires_on_outbound() {
3951        let mut dp = DataPlane::new(NodeKeyPair::new());
3952
3953        let recorded: CaptureLog = Arc::new(Mutex::new(Vec::new()));
3954        let sink = recorded.clone();
3955        dp.capture = Some(Arc::new(move |path: CapturePath, bytes: &[u8]| {
3956            sink.lock().unwrap().push((path, bytes.to_vec()));
3957        }));
3958
3959        // The outbound tee passes `p.as_ref()` as-given; the bytes need not be a valid IP packet.
3960        let payload: Vec<u8> = vec![0xde, 0xad, 0xbe, 0xef];
3961        let packet = PacketMut::from(payload.clone());
3962
3963        drop(dp.process_outbound(vec![packet]));
3964
3965        let captured = recorded.lock().unwrap();
3966        assert_eq!(captured.len(), 1, "hook must fire exactly once per packet");
3967        assert_eq!(captured[0].0, CapturePath::FromLocal);
3968        assert_eq!(captured[0].1, payload);
3969    }
3970
3971    /// A minimal IPv4/UDP datagram from `src` to `dst`. The control for the outbound TSMP refusal:
3972    /// same source, same destination, same batch as the forged advertisement — only the protocol
3973    /// byte differs.
3974    fn v4_udp_packet(
3975        src: std::net::SocketAddr,
3976        dst: std::net::SocketAddr,
3977        payload: &[u8],
3978    ) -> Vec<u8> {
3979        let (std::net::IpAddr::V4(src_ip), std::net::IpAddr::V4(dst_ip)) = (src.ip(), dst.ip())
3980        else {
3981            panic!("v4_udp_packet needs two IPv4 addresses");
3982        };
3983        let total_len = u16::try_from(IP4_HEADER_LEN + 8 + payload.len()).unwrap();
3984        let mut buf = vec![0u8; usize::from(total_len)];
3985        buf[0] = 0x45; // version 4, IHL 5 (no options)
3986        buf[2..4].copy_from_slice(&total_len.to_be_bytes());
3987        buf[8] = 64; // TTL
3988        buf[9] = 17; // UDP
3989        buf[12..16].copy_from_slice(&src_ip.octets());
3990        buf[16..20].copy_from_slice(&dst_ip.octets());
3991        buf[20..22].copy_from_slice(&src.port().to_be_bytes());
3992        buf[22..24].copy_from_slice(&dst.port().to_be_bytes());
3993        let udp_len = u16::try_from(8 + payload.len()).unwrap();
3994        buf[24..26].copy_from_slice(&udp_len.to_be_bytes());
3995        // UDP checksum left 0 ("not computed"), which is legal for IPv4.
3996        buf[IP4_HEADER_LEN + 8..].copy_from_slice(payload);
3997        buf
3998    }
3999
4000    /// What `process_outbound` refuses, mirroring the `p.IPProto == ipproto.TSMP` arm of Go
4001    /// `tstun.filterPacketOutboundToWireGuard` — plus the three shapes this tree drops that Go's
4002    /// TSMP arm alone does not, because there is no outbound ACL behind it here to refuse them as
4003    /// `ipproto.Unknown`. See [`outbound_packet_carries_tsmp`].
4004    #[test]
4005    fn outbound_tsmp_classification_matches_go_decode() {
4006        let v4_src = std::net::IpAddr::from([100, 64, 0, 1]);
4007        let v4_dst = std::net::IpAddr::from([100, 64, 0, 2]);
4008        let v4 = ts_packet::tsmp::DiscoKeyAdvertisement {
4009            src: v4_src,
4010            dst: v4_dst,
4011            key: SELF_DISCO_KEY,
4012        }
4013        .marshal()
4014        .expect("a v4 advertisement between two v4 addresses marshals");
4015        let v6 = ts_packet::tsmp::DiscoKeyAdvertisement {
4016            src: std::net::IpAddr::V6(IPV6_FIXTURE_SRC),
4017            dst: std::net::IpAddr::V6(IPV6_FIXTURE_DST),
4018            key: SELF_DISCO_KEY,
4019        }
4020        .marshal()
4021        .expect("a v6 advertisement between two v6 addresses marshals");
4022
4023        // The forgery this exists to stop, in both families: bytes byte-identical to what this node
4024        // would itself emit, handed to us by the host instead.
4025        assert!(
4026            outbound_packet_carries_tsmp(&v4),
4027            "an IPv4 TSMP packet from the host is refused"
4028        );
4029        assert!(
4030            outbound_packet_carries_tsmp(&v6),
4031            "an IPv6 TSMP packet from the host is refused"
4032        );
4033
4034        // Ordinary traffic is untouched — the refusal is protocol-specific, not a blanket drop.
4035        assert!(
4036            !outbound_packet_carries_tsmp(&v4_udp_packet(
4037                std::net::SocketAddr::new(v4_src, 4242),
4038                std::net::SocketAddr::new(v4_dst, 4343),
4039                b"hello"
4040            )),
4041            "IPv4 UDP passes"
4042        );
4043        assert!(
4044            !outbound_packet_carries_tsmp(&ipv6_udp_packet(&udp_header(53))),
4045            "IPv6 UDP passes"
4046        );
4047
4048        // Go demotes a *fragmented* IPv4 TSMP packet to `ipproto.Unknown`, which its outbound ACL
4049        // then drops for "unknown proto". With no outbound ACL here the protocol byte is the whole
4050        // verdict, so the refusal happens one step earlier and the packet still never ships.
4051        let mut fragmented = v4.clone();
4052        fragmented[6] = 0x20; // More Fragments
4053        assert!(
4054            outbound_packet_carries_tsmp(&fragmented),
4055            "a fragmented IPv4 TSMP packet is refused too"
4056        );
4057
4058        // An IPv6 Fragment extension header naming TSMP: Go classifies the head fragment TSMP and
4059        // its followers `ipproto.Fragment`. Both are refused here — every fragment of one datagram
4060        // repeats the same Next Header, and with the head refused no peer could reassemble anyway.
4061        assert!(
4062            outbound_packet_carries_tsmp(&ipv6_fragment_packet(
4063                ts_packet::tsmp::IP_PROTO_TSMP,
4064                0,
4065                true,
4066                &[b'a'; 33],
4067            )),
4068            "the head fragment of an IPv6 TSMP datagram is refused"
4069        );
4070        assert!(
4071            outbound_packet_carries_tsmp(&ipv6_fragment_packet(
4072                ts_packet::tsmp::IP_PROTO_TSMP,
4073                MIN_FRAG_BLKS,
4074                false,
4075                &[0u8; 8],
4076            )),
4077            "so are its later fragments"
4078        );
4079        assert!(
4080            !outbound_packet_carries_tsmp(&ipv6_fragment_packet(17, 0, true, &udp_header(53))),
4081            "a fragmented IPv6 UDP datagram is not TSMP and still passes"
4082        );
4083
4084        // Nothing to classify: not IP at all, or truncated before the protocol byte can be trusted.
4085        assert!(
4086            !outbound_packet_carries_tsmp(&[]),
4087            "the empty buffer passes"
4088        );
4089        assert!(
4090            !outbound_packet_carries_tsmp(&[0xde, 0xad, 0xbe, 0xef]),
4091            "a non-IP buffer passes (the router drops it for want of a destination)"
4092        );
4093        assert!(
4094            !outbound_packet_carries_tsmp(&v4[..IP4_HEADER_LEN - 1]),
4095            "an IPv4 packet cut off inside its header passes"
4096        );
4097        assert!(
4098            !outbound_packet_carries_tsmp(&v6[..IP6_HEADER_LEN - 1]),
4099            "an IPv6 packet cut off inside its header passes"
4100        );
4101    }
4102
4103    /// The whole point of the outbound TSMP refusal, end to end, together with the negative case
4104    /// that keeps it from silently disabling capability version 144.
4105    ///
4106    /// A local process writes a well-formed disco-key advertisement — naming a disco key of its own
4107    /// choosing, addressed to a peer whose route really does resolve to a live WireGuard session —
4108    /// into the tun. The peer must never see it: it arrives inside this node's session from this
4109    /// node's tailnet address, so it is indistinguishable from one this node meant to send, and the
4110    /// peer would bind the forger's key for us. Ordinary traffic in the same batch to the same
4111    /// destination must be untouched.
4112    ///
4113    /// And the advertisement this node itself sends must still go out. It is built by
4114    /// `DiscoAdvertisementState::advertisement_for` and injected by `process_inbound` on session
4115    /// establishment, *below* the refusal — Go has the same relationship, where `injectedRead`
4116    /// bypasses the outbound filter. Without this half of the test a drop placed one layer too low
4117    /// would look green.
4118    #[test]
4119    fn host_written_tsmp_is_dropped_while_our_own_advertisement_still_goes_out() {
4120        let underlay: UnderlayTransportId = 0.into();
4121        let wg_peer = ts_tunnel::PeerId(1);
4122        let peer = PeerId(1);
4123        let a_addr = std::net::IpAddr::from([100, 64, 0, 1]);
4124        let b_addr = std::net::IpAddr::from([100, 64, 0, 2]);
4125
4126        let (a_static, b_static) = (NodeKeyPair::new(), NodeKeyPair::new());
4127        let (mut a, mut b) = (
4128            DataPlane::new(a_static.clone()),
4129            DataPlane::new(b_static.clone()),
4130        );
4131
4132        for (dp, key) in [(&mut a, b_static.public), (&mut b, a_static.public)] {
4133            dp.wireguard.upsert_peer(
4134                wg_peer,
4135                ts_tunnel::PeerConfig {
4136                    key,
4137                    psk: [0u8; 32].into(),
4138                    persistent_keepalive_interval: None,
4139                },
4140            );
4141            dp.ur_out.table.insert(peer, underlay);
4142        }
4143
4144        a.disco_advertisement = Some(Arc::new(advertisement_state(
4145            peer,
4146            AdvertisementTarget {
4147                node_addr: b_addr,
4148                wireguard_only: false,
4149            },
4150        )));
4151
4152        // A routes B's tailnet address to the wireguard peer, so a host-written packet addressed to
4153        // B really would be encrypted and shipped were it not refused. Without this the test would
4154        // pass on an empty routing table and prove nothing.
4155        let mut routes = ts_bart::Table::default();
4156        routes.insert(
4157            ipnet::IpNet::from(b_addr),
4158            or::outbound::RouteAction::Wireguard(peer),
4159        );
4160        a.or_out.swap(routes);
4161
4162        // B attributes A's tailnet address to the wireguard peer that carries it, as the runtime's
4163        // source filter does.
4164        let mut src_filter = ts_bart::Table::default();
4165        src_filter.insert(ipnet::IpNet::from(a_addr), peer);
4166        b.src_filter_in = Arc::new(src_filter);
4167
4168        // Everything B decrypts, in arrival order, before any filtering runs.
4169        let recorded: CaptureLog = Arc::new(Mutex::new(Vec::new()));
4170        let sink = recorded.clone();
4171        b.capture = Some(Arc::new(move |path: CapturePath, bytes: &[u8]| {
4172            sink.lock().unwrap().push((path, bytes.to_vec()));
4173        }));
4174
4175        let take = |out: HashMap<(UnderlayTransportId, PeerId), Vec<PacketMut>>| {
4176            out.into_values().flatten().collect::<Vec<_>>()
4177        };
4178
4179        // Establish the session. A's own advertisement rides the establishment.
4180        let init = a
4181            .wireguard
4182            .send([(wg_peer, vec![PacketMut::from(&b"hello"[..])])])
4183            .to_peers
4184            .remove(&wg_peer)
4185            .expect("handshake initiation");
4186        let resp = take(b.process_inbound(init).to_peers);
4187        let from_a = take(a.process_inbound(resp).to_peers);
4188        let learned = b.process_inbound(from_a).learned_disco_keys;
4189        assert_eq!(
4190            learned
4191                .iter()
4192                .map(|(peer, advert)| (*peer, advert.key))
4193                .collect::<Vec<_>>(),
4194            vec![(peer, SELF_DISCO_KEY)],
4195            "our own advertisement must still reach the peer: it is injected below process_outbound"
4196        );
4197
4198        // Now the forgery, alongside ordinary traffic to the same destination in the same batch.
4199        const FORGED_KEY: [u8; 32] = [0xff; 32];
4200        let forged = ts_packet::tsmp::DiscoKeyAdvertisement {
4201            src: a_addr,
4202            dst: b_addr,
4203            key: FORGED_KEY,
4204        }
4205        .marshal()
4206        .expect("a v4 advertisement between two v4 addresses marshals");
4207        const CARRIED: &[u8] = b"ordinary traffic in the same batch";
4208        let control = v4_udp_packet(
4209            std::net::SocketAddr::new(a_addr, 4242),
4210            std::net::SocketAddr::new(b_addr, 4343),
4211            CARRIED,
4212        );
4213
4214        // This is the only test that increments this counter, so the delta is exact.
4215        let counted_before = metric_out_to_wg_drop_tsmp().value();
4216        let out = a.process_outbound(vec![
4217            PacketMut::from(&forged[..]),
4218            PacketMut::from(&control[..]),
4219        ]);
4220
4221        let mark = recorded.lock().unwrap().len();
4222        let inbound = b.process_inbound(take(out.to_peers));
4223        assert!(
4224            inbound.learned_disco_keys.is_empty(),
4225            "the forged advertisement must never reach the peer, or it binds the forger's key for us"
4226        );
4227
4228        let captured = recorded.lock().unwrap();
4229        let delivered = captured[mark..]
4230            .iter()
4231            .filter(|(path, _)| *path == CapturePath::FromPeer)
4232            .map(|(_, bytes)| bytes.as_slice())
4233            .collect::<Vec<_>>();
4234        assert_eq!(
4235            delivered.len(),
4236            1,
4237            "exactly the one non-TSMP packet of the batch crosses the tunnel"
4238        );
4239        // The send path zero-pads each payload up to a 16-byte boundary and the receiver delivers it
4240        // with that padding intact (see `session::PADDING_MULTIPLE`), so compare on the leading bytes.
4241        assert!(
4242            delivered[0].starts_with(&control),
4243            "and it is the ordinary traffic, unaltered"
4244        );
4245
4246        assert_eq!(
4247            metric_out_to_wg_drop_tsmp().value(),
4248            counted_before + 1,
4249            "the drop is counted in tstun_out_to_wg_drop_tsmp (Go metricPacketOutDropTSMP)"
4250        );
4251    }
4252
4253    /// Run one inbound packet through the real inbound filter with `flows` as the connection-
4254    /// tracking state, and report whether it survived.
4255    fn admitted(
4256        filter: &(dyn ts_packetfilter::Filter + Send + Sync),
4257        flows: &mut flowtrack::FlowCache,
4258        packet: Vec<u8>,
4259    ) -> bool {
4260        let mut packets = vec![PacketMut::from(packet)];
4261        let mut harvest = InboundHarvest::default();
4262        filter_inbound_from_peer(
4263            filter,
4264            flows,
4265            PeerId(1),
4266            &mut packets,
4267            RejectConfig::default(),
4268            &mut harvest,
4269        );
4270        assert!(
4271            harvest.learned_disco_keys.is_empty(),
4272            "no TSMP advertisement in these fixtures"
4273        );
4274        !packets.is_empty()
4275    }
4276
4277    /// A dataplane wired up well enough that `process_outbound` really routes a packet to a peer,
4278    /// so the flow-tracking tests exercise the live outbound path rather than a stub.
4279    fn dataplane_routing_to(peer: PeerId, dsts: &[std::net::IpAddr]) -> DataPlane {
4280        let mut dp = DataPlane::new(NodeKeyPair::new());
4281        dp.wireguard.upsert_peer(
4282            ts_tunnel::PeerId(peer.0),
4283            ts_tunnel::PeerConfig {
4284                key: NodeKeyPair::new().public,
4285                psk: [0u8; 32].into(),
4286                persistent_keepalive_interval: None,
4287            },
4288        );
4289        dp.ur_out.table.insert(peer, 0.into());
4290        let mut routes = ts_bart::Table::default();
4291        for dst in dsts {
4292            routes.insert(
4293                ipnet::IpNet::from(*dst),
4294                or::outbound::RouteAction::Wireguard(peer),
4295            );
4296        }
4297        dp.or_out.swap(routes);
4298        dp
4299    }
4300
4301    /// Go's reverse-flow connection tracking (`wgengine/filter`'s `Filter.state`), across the two
4302    /// paths it spans: `process_outbound` records the reversed tuple of every outbound UDP
4303    /// datagram (Go `UpdateOutboundFlowState`, which upstream `e0677ccc7` had to start calling on
4304    /// the netstack-injected path — the only path this engine has), and the inbound filter admits
4305    /// the reply on it ahead of any rule (Go `runIn4`'s `return Accept, "cached"`).
4306    ///
4307    /// Every verdict here is taken under a **deny-all** ACL, so an admission can only have come
4308    /// from the flow cache. The refusals are the point of the test as much as the admission is: a
4309    /// conntrack that admits too much is an open door, so each of the four fields of Go's
4310    /// `flowtrack.Tuple` is varied in turn and must miss.
4311    #[test]
4312    fn an_outbound_udp_flow_admits_its_own_reply_and_nothing_else() {
4313        let peer = PeerId(1);
4314        let me = std::net::IpAddr::from([100, 64, 0, 1]);
4315        let them = std::net::IpAddr::from([100, 64, 0, 2]);
4316        let elsewhere = std::net::IpAddr::from([100, 64, 0, 3]);
4317        let sa = |ip, port| std::net::SocketAddr::new(ip, port);
4318
4319        let mut dp = dataplane_routing_to(peer, &[them, elsewhere]);
4320
4321        // Our datagram out, and the reply it should get: the same tuple, reversed. Our source port
4322        // is ephemeral, which is exactly why no ACL from control can name it.
4323        let query = v4_udp_packet(sa(me, 41234), sa(them, 53), b"query");
4324        let reply = v4_udp_packet(sa(them, 53), sa(me, 41234), b"answer");
4325
4326        // Before anything is sent the reply is an unsolicited datagram to an ephemeral port, and a
4327        // deny-all ACL drops it. That is what every UDP reply used to get in this fork.
4328        assert!(
4329            !admitted(&DenyAll, &mut dp.flows, reply.clone()),
4330            "an inbound datagram matching no outbound flow and no rule is dropped"
4331        );
4332
4333        let out = dp.process_outbound(vec![PacketMut::from(query)]);
4334        assert!(
4335            !out.to_peers.is_empty(),
4336            "the datagram really did route to the peer, so this is the live outbound path"
4337        );
4338
4339        assert!(
4340            admitted(&DenyAll, &mut dp.flows, reply.clone()),
4341            "the reply to our own datagram is admitted with no rule matching it"
4342        );
4343
4344        // One field of Go's tuple differs in each of these, and each must miss. Without them the
4345        // cache would be a blanket "any inbound UDP is fine once we have sent one".
4346        for (why, packet) in [
4347            (
4348                "a different source PORT",
4349                v4_udp_packet(sa(them, 5353), sa(me, 41234), b"x"),
4350            ),
4351            (
4352                "a different source ADDRESS",
4353                v4_udp_packet(sa(elsewhere, 53), sa(me, 41234), b"x"),
4354            ),
4355            (
4356                "a different destination PORT",
4357                v4_udp_packet(sa(them, 53), sa(me, 41235), b"x"),
4358            ),
4359            (
4360                "a different destination ADDRESS",
4361                v4_udp_packet(sa(them, 53), sa(elsewhere, 41234), b"x"),
4362            ),
4363        ] {
4364            assert!(
4365                !admitted(&DenyAll, &mut dp.flows, packet),
4366                "{why} does not ride the recorded entry"
4367            );
4368        }
4369
4370        // And the entry admits, it never denies: the ACL still decides everything the cache misses.
4371        assert!(
4372            admitted(
4373                &AllowAll,
4374                &mut dp.flows,
4375                v4_udp_packet(sa(elsewhere, 53), sa(me, 41234), b"x")
4376            ),
4377            "a cache miss falls through to the rule match, exactly as Go does"
4378        );
4379    }
4380
4381    /// SCTP is the second arm of Go's `case ipproto.UDP, ipproto.SCTP` — on both the recording and
4382    /// the admitting side — and the protocol is part of the tuple, so a UDP entry cannot carry an
4383    /// SCTP datagram or the other way round.
4384    #[test]
4385    fn outbound_sctp_flows_are_tracked_and_do_not_cross_protocols() {
4386        let peer = PeerId(1);
4387        let dst = std::net::IpAddr::V4(IPV4_FIXTURE_DST);
4388        let mut dp = dataplane_routing_to(peer, &[dst]);
4389
4390        // `v4_packet` addresses this fixture src -> dst, so the reply is dst -> src. `sctp_header`
4391        // writes source port 54276.
4392        let out = v4_packet(132, 0, false, &sctp_header(443, SCTP_HEADER_LEN));
4393        let mut reply = v4_packet(132, 0, false, &sctp_header(54276, SCTP_HEADER_LEN));
4394        reply[12..16].copy_from_slice(&IPV4_FIXTURE_DST.octets());
4395        reply[16..20].copy_from_slice(&IPV4_FIXTURE_SRC.octets());
4396        reply[20..22].copy_from_slice(&443u16.to_be_bytes());
4397
4398        assert!(
4399            !admitted(&DenyAll, &mut dp.flows, reply.clone()),
4400            "an unsolicited SCTP packet with no rule is dropped"
4401        );
4402        drop(dp.process_outbound(vec![PacketMut::from(out)]));
4403        assert!(
4404            admitted(&DenyAll, &mut dp.flows, reply.clone()),
4405            "the SCTP reply to our own packet is admitted (Go's second switch arm)"
4406        );
4407
4408        // Same addresses, same ports, UDP instead of SCTP: the protocol is part of Go's tuple.
4409        //
4410        // Reinterpreting the SCTP header as a UDP one puts the verification tag where the UDP
4411        // length field belongs, and `UdpSlice::from_slice` refuses a length field the slice cannot
4412        // satisfy — so the datagram has to be given a real length here, or the drop below is the
4413        // parse failing and says nothing at all about the protocol in the tuple.
4414        let mut as_udp = reply.clone();
4415        as_udp[9] = 17;
4416        as_udp[24..26].copy_from_slice(&u16::try_from(SCTP_HEADER_LEN).unwrap().to_be_bytes());
4417        assert!(
4418            admitted(&AllowAll, &mut dp.flows, as_udp.clone()),
4419            "the fixture is a datagram the filter can parse, so the drop below is the tuple's"
4420        );
4421        assert!(
4422            !admitted(&DenyAll, &mut dp.flows, as_udp),
4423            "a UDP datagram does not ride an SCTP entry"
4424        );
4425    }
4426
4427    /// What [`outbound_udp_or_sctp_flow`] records and — the half that keeps the cache honest —
4428    /// what it refuses to record, carrying Go's `decode4`/`decode6` refusals into
4429    /// `UpdateOutboundFlowState`. Everything this function declines leaves the reply where it was
4430    /// before: needing an ACL rule.
4431    #[test]
4432    fn outbound_flow_decoding_matches_go_decode() {
4433        let me = std::net::IpAddr::from([100, 64, 0, 1]);
4434        let them = std::net::IpAddr::from([100, 64, 0, 2]);
4435        let sa = |ip, port| std::net::SocketAddr::new(ip, port);
4436
4437        // UDP: both ports off the wire, addresses in packet order — reversing them is the cache's
4438        // job (Go `MakeTuple(q.IPProto, q.Dst, q.Src)`), not the decoder's.
4439        assert_eq!(
4440            outbound_udp_or_sctp_flow(&v4_udp_packet(sa(me, 41234), sa(them, 53), b"q")),
4441            Some((IpProto::UDP, sa(me, 41234), sa(them, 53))),
4442            "an outbound UDP datagram yields its own tuple"
4443        );
4444
4445        // SCTP: etherparse has no arm for it, so the ports come from Go's `sub[0:2]`/`sub[2:4]`.
4446        let v4_fixture_src = std::net::IpAddr::V4(IPV4_FIXTURE_SRC);
4447        let v4_fixture_dst = std::net::IpAddr::V4(IPV4_FIXTURE_DST);
4448        assert_eq!(
4449            outbound_udp_or_sctp_flow(&v4_packet(
4450                132,
4451                0,
4452                false,
4453                &sctp_header(443, SCTP_HEADER_LEN)
4454            )),
4455            Some((
4456                IpProto::SCTP,
4457                sa(v4_fixture_src, 54276),
4458                sa(v4_fixture_dst, 443)
4459            )),
4460            "an outbound SCTP packet yields its tuple, ports read the way Go reads them"
4461        );
4462        assert_eq!(
4463            outbound_udp_or_sctp_flow(&v4_packet(
4464                132,
4465                0,
4466                false,
4467                &sctp_header(443, SCTP_HEADER_LEN - 1)
4468            )),
4469            None,
4470            "an SCTP common header too short to hold its ports records nothing, not a port-0 flow"
4471        );
4472
4473        // Go's switch has exactly two arms; TCP, ICMP and TSMP fall through it.
4474        for (proto, name) in [(6u8, "TCP"), (1, "ICMP"), (99, "TSMP")] {
4475            assert_eq!(
4476                outbound_udp_or_sctp_flow(&v4_packet(proto, 0, false, &[0u8; 20])),
4477                None,
4478                "{name} is not tracked (Go tracks UDP and SCTP only)"
4479            );
4480        }
4481
4482        // A *first* IPv4 fragment carries its whole UDP header, so `decode4` reads its ports and
4483        // the flow is recorded like an unfragmented datagram's...
4484        let mut head = v4_udp_packet(sa(me, 41234), sa(them, 53), b"payload!");
4485        head[6] = 0x20; // More Fragments, offset 0
4486        assert_eq!(
4487            outbound_udp_or_sctp_flow(&head),
4488            Some((IpProto::UDP, sa(me, 41234), sa(them, 53))),
4489            "the head fragment of an outbound datagram records the same tuple"
4490        );
4491        // ...while a non-first fragment is `ipproto.Fragment` to Go, matching neither arm, and has
4492        // no transport header whose ports could be invented.
4493        let mut later = v4_udp_packet(sa(me, 41234), sa(them, 53), b"payload!");
4494        later[6..8].copy_from_slice(&MIN_FRAG_BLKS.to_be_bytes());
4495        assert_eq!(
4496            outbound_udp_or_sctp_flow(&later),
4497            None,
4498            "a non-first IPv4 fragment records nothing"
4499        );
4500
4501        // IPv6: the protocol comes from the base header only (Go `decode6`'s `b[6]`), so a UDP
4502        // header buried behind a chained extension header is not a flow upstream tracks either —
4503        // even though etherparse could walk to it.
4504        assert_eq!(
4505            outbound_udp_or_sctp_flow(&ipv6_udp_packet(&udp_header(53))),
4506            Some((
4507                IpProto::UDP,
4508                sa(std::net::IpAddr::V6(IPV6_FIXTURE_SRC), 54276),
4509                sa(std::net::IpAddr::V6(IPV6_FIXTURE_DST), 53)
4510            )),
4511            "an outbound IPv6 UDP datagram yields its tuple"
4512        );
4513        assert_eq!(
4514            outbound_udp_or_sctp_flow(&ipv6_with_prepended_ext_header(
4515                60,
4516                &ipv6_udp_packet(&udp_header(53))
4517            )),
4518            None,
4519            "a UDP header behind a chained extension header is not a tracked flow"
4520        );
4521
4522        // A leading IPv6 Fragment header IS stepped over, exactly as `decode6` does.
4523        assert_eq!(
4524            outbound_udp_or_sctp_flow(&ipv6_fragment_packet(17, 0, true, &udp_header(53))),
4525            Some((
4526                IpProto::UDP,
4527                sa(std::net::IpAddr::V6(IPV6_FIXTURE_SRC), 54276),
4528                sa(std::net::IpAddr::V6(IPV6_FIXTURE_DST), 53)
4529            )),
4530            "a first IPv6 fragment records the tuple behind its Fragment header"
4531        );
4532        assert_eq!(
4533            outbound_udp_or_sctp_flow(&ipv6_fragment_packet(17, MIN_FRAG_BLKS, false, &[0u8; 8])),
4534            None,
4535            "a later IPv6 fragment records nothing"
4536        );
4537
4538        // Nothing to decode at all.
4539        assert_eq!(outbound_udp_or_sctp_flow(&[]), None, "the empty buffer");
4540        assert_eq!(
4541            outbound_udp_or_sctp_flow(&[0xde, 0xad, 0xbe, 0xef]),
4542            None,
4543            "a non-IP buffer"
4544        );
4545    }
4546
4547    /// Go `packet.TCPFin`, the flag bit `tcp_header` writes into the byte Go reads as `q.TCPFlags`.
4548    const TCP_FIN: u8 = 0x01;
4549    /// Go `packet.TCPSyn`.
4550    const TCP_SYN: u8 = 0x02;
4551    /// Go `packet.TCPRst`.
4552    const TCP_RST: u8 = 0x04;
4553    /// Go `packet.TCPAck`.
4554    const TCP_ACK: u8 = 0x10;
4555
4556    /// A minimal 20-byte TCP header: the two ports, a data offset of 5 (no options, which is what
4557    /// makes it 20 bytes and what etherparse bounds-checks), and `flags` in `sub[13]` — the byte Go
4558    /// `decode4`/`decode6` copy into `q.TCPFlags`. The checksum is left zero; nothing on this path
4559    /// verifies it, and neither does Go's decoder.
4560    fn tcp_header(src_port: u16, dst_port: u16, flags: u8) -> Vec<u8> {
4561        let mut hdr = vec![0u8; 20];
4562        hdr[0..2].copy_from_slice(&src_port.to_be_bytes());
4563        hdr[2..4].copy_from_slice(&dst_port.to_be_bytes());
4564        hdr[12] = 0x50; // data offset 5 (a 20-byte header), no options
4565        hdr[13] = flags;
4566        hdr
4567    }
4568
4569    /// A minimal 8-byte ICMP/ICMPv6 message: type, code, a zero checksum, and four bytes of
4570    /// rest-of-header. Eight is the length Go guards every one of its type/code reads with
4571    /// (`len(q.b) >= q.subofs+8`), and etherparse's ICMP slices refuse anything shorter too.
4572    fn icmp_header(icmp_type: u8, icmp_code: u8) -> Vec<u8> {
4573        let mut hdr = vec![0u8; 8];
4574        hdr[0] = icmp_type;
4575        hdr[1] = icmp_code;
4576        hdr
4577    }
4578
4579    /// Go's TCP reply carve-out, on real bytes through the whole inbound filter: `runIn4`/`runIn6`
4580    /// accept an inbound TCP segment that is not a SYN *before* any rule is consulted.
4581    ///
4582    /// ```text
4583    /// if !q.IsTCPSyn() {
4584    ///     return Accept, "tcp non-syn"
4585    /// }
4586    /// ```
4587    ///
4588    /// Every accept below is taken under a **deny-all** filter, so it can only have come from the
4589    /// carve-out. The motivating case is the first one: this node dials out, the peer answers with
4590    /// a SYN-ACK aimed at our ephemeral source port, and no ACL control can write will ever name
4591    /// that port. Without the carve-out the handshake never completes and the failure reads as a
4592    /// network fault rather than as a filter decision.
4593    ///
4594    /// The refusals are what keep it from being an open door, and they are the point of the test as
4595    /// much as the accepts are. A SYN — the one segment that can *start* an inbound session — still
4596    /// faces the rules, and so does a non-SYN segment buried under an IPv6 extension header, which
4597    /// upstream filters as protocol 43 rather than as TCP. The third refusal, a segment whose flags
4598    /// this fork never decoded, is pinned by every case that goes through [`stateless_verdict`]:
4599    /// they all carry [`ts_packetfilter::L4Header::Unknown`].
4600    ///
4601    /// Ported from github.com/tailscale/tailscale `wgengine/filter/filter.go` (`runIn4`, `runIn6`)
4602    /// and `net/packet/packet.go` (`IsTCPSyn`, `decode4`, `decode6`) at
4603    /// `3945b82f8a9550b54c33e61d4ed2227862d53e8a`.
4604    #[test]
4605    fn inbound_tcp_non_syn_is_admitted_without_a_rule_but_a_syn_is_not() {
4606        let flows = &mut flowtrack::FlowCache::default();
4607        // Source port 443, destination port 41234: the shape of a reply to a connection this node
4608        // opened. No rule in `DenyAll` names it, and none in a real tailnet ACL could.
4609        let v4 = |flags| v4_packet(6, 0, false, &tcp_header(443, 41234, flags));
4610        let v6 = |flags| ipv6_packet(6, &tcp_header(443, 41234, flags));
4611
4612        for (why, flags) in [
4613            (
4614                "a SYN-ACK — the answer to our own outbound connection",
4615                TCP_SYN | TCP_ACK,
4616            ),
4617            (
4618                "a bare ACK — the continuation of an established session",
4619                TCP_ACK,
4620            ),
4621            ("a FIN-ACK closing that session", TCP_FIN | TCP_ACK),
4622            ("an RST tearing it down", TCP_RST),
4623            // Go's test is `(flags & (SYN|ACK)) == SYN`, so a segment with neither bit set is a
4624            // non-SYN as well. Ported as it stands rather than tightened.
4625            ("a segment with no flags at all", 0x00),
4626        ] {
4627            assert!(
4628                admitted(&DenyAll, flows, v4(flags)),
4629                "{why} is admitted with no rule matching it"
4630            );
4631            assert!(
4632                admitted(&DenyAll, flows, v6(flags)),
4633                "{why} is admitted on IPv6 too (Go `runIn6` carries the same arm)"
4634            );
4635        }
4636
4637        // The negative that gives the carve-out its value: SYN set and ACK clear is Go's
4638        // `IsTCPSyn`, and it is the only way to open an inbound session.
4639        assert!(
4640            !admitted(&DenyAll, flows, v4(TCP_SYN)),
4641            "an inbound SYN with no matching rule is still dropped"
4642        );
4643        assert!(
4644            !admitted(&DenyAll, flows, v6(TCP_SYN)),
4645            "an inbound SYN with no matching rule is still dropped on IPv6"
4646        );
4647        // ...and it is admitted when a rule really does open the port, so the drop above is the
4648        // rules speaking and not the carve-out swallowing the packet.
4649        assert!(
4650            admitted(
4651                &acl(IPV4_FIXTURE_NET, &[6], 41234..=41234),
4652                flows,
4653                v4(TCP_SYN)
4654            ),
4655            "a rule opening the destination port admits the SYN"
4656        );
4657
4658        // The protocol number the carve-out is scoped to is the one the **base** IP header
4659        // declares, which is what Go's `runIn4`/`runIn6` switch dispatches on. etherparse walks an
4660        // IPv6 extension-header chain through to the real transport, so a non-SYN segment behind a
4661        // Routing header hands this code a TCP transport slice for a packet upstream filters as
4662        // protocol 43. Admitting it would make "prepend an extension header" a way past the rules.
4663        for ext in [43u8, 60] {
4664            assert!(
4665                !admitted(
4666                    &DenyAll,
4667                    flows,
4668                    ipv6_with_prepended_ext_header(ext, &v6(TCP_ACK))
4669                ),
4670                "a non-SYN segment behind extension header {ext} is not TCP to the filter"
4671            );
4672        }
4673    }
4674
4675    /// Go's ICMP reply carve-out, on real bytes through the whole inbound filter: `runIn4`/`runIn6`
4676    /// accept an ICMP echo *response* or an ICMP *error* before any rule is consulted.
4677    ///
4678    /// ```text
4679    /// if q.IsEchoResponse() || q.IsError() {
4680    ///     // ICMP responses are allowed.
4681    ///     return Accept, "icmp response ok"
4682    /// } else if f.matches4.matchIPsOnly(q, f.srcIPHasCap) {
4683    ///     // If any port is open to an IP, allow ICMP to it.
4684    ///     return Accept, "icmp ok"
4685    /// }
4686    /// ```
4687    ///
4688    /// The reply to a ping this node sent is unsolicited as far as any ACL is concerned, and so is
4689    /// the Packet Too Big that path-MTU discovery needs; both were silently dropped under a policy
4690    /// that does not grant the peer inbound access back.
4691    ///
4692    /// The negative half is the `else if`, which this fork already had: ICMP that is *not* a
4693    /// response must still go to the rules and be matched IPs-only, never admitted as a "response".
4694    /// An echo request is the case that matters — it is how an inbound ICMP session starts.
4695    ///
4696    /// Ported from github.com/tailscale/tailscale `wgengine/filter/filter.go` (`runIn4`, `runIn6`)
4697    /// and `net/packet/packet.go` (`IsEchoResponse`, `IsError`) at
4698    /// `3945b82f8a9550b54c33e61d4ed2227862d53e8a`.
4699    #[test]
4700    fn inbound_icmp_responses_are_admitted_without_a_rule_but_requests_are_not() {
4701        let flows = &mut flowtrack::FlowCache::default();
4702        let v4 = |icmp_type, icmp_code| v4_packet(1, 0, false, &icmp_header(icmp_type, icmp_code));
4703        let v6 = |icmp_type, icmp_code| ipv6_packet(58, &icmp_header(icmp_type, icmp_code));
4704
4705        // Go `IsEchoResponse` and `IsError` on ICMPv4.
4706        for (why, icmp_type, icmp_code) in [
4707            ("an echo reply — the answer to our own ping", 0x00u8, 0u8),
4708            ("destination unreachable", 0x03, 1),
4709            ("time exceeded", 0x0b, 0),
4710            // Upstream's `ICMP4ParamProblem` is 0x12. IANA's Parameter Problem is 12 (0x0c) and
4711            // 0x12 is Address Mask Reply, so the constant names one message and holds another —
4712            // ported as it stands, because "fixing" it would admit type 12 traffic upstream drops.
4713            (
4714                "upstream's ICMP4ParamProblem constant, whatever IANA calls it",
4715                0x12,
4716                0,
4717            ),
4718        ] {
4719            assert!(
4720                admitted(&DenyAll, flows, v4(icmp_type, icmp_code)),
4721                "{why} is admitted with no rule matching it"
4722            );
4723        }
4724
4725        // ...and on ICMPv6, where the type numbers are entirely different and Packet Too Big is an
4726        // error class of its own.
4727        for (why, icmp_type, icmp_code) in [
4728            ("an ICMPv6 echo reply", 129u8, 0u8),
4729            ("ICMPv6 destination unreachable", 1, 3),
4730            (
4731                "ICMPv6 packet too big — what path-MTU discovery rides on",
4732                2,
4733                0,
4734            ),
4735            ("ICMPv6 time exceeded", 3, 0),
4736            ("ICMPv6 parameter problem", 4, 0),
4737        ] {
4738            assert!(
4739                admitted(&DenyAll, flows, v6(icmp_type, icmp_code)),
4740                "{why} is admitted with no rule matching it"
4741            );
4742        }
4743
4744        // The negatives. None of these is a "response", so each falls through to the rules — and a
4745        // deny-all filter says no.
4746        for (why, packet) in [
4747            (
4748                "an echo REQUEST is how an inbound session starts",
4749                v4(0x08, 0),
4750            ),
4751            (
4752                "an echo reply with a non-zero code is not one (Go tests the code)",
4753                v4(0x00, 1),
4754            ),
4755            (
4756                "IANA's Parameter Problem (12) is not upstream's constant (0x12)",
4757                v4(0x0c, 0),
4758            ),
4759            ("an ICMPv6 echo REQUEST", v6(128, 0)),
4760            ("an ICMPv6 echo reply with a non-zero code", v6(129, 1)),
4761            (
4762                "an ICMPv6 router advertisement is neither response nor error",
4763                v6(134, 0),
4764            ),
4765        ] {
4766            assert!(!admitted(&DenyAll, flows, packet), "{why}: still dropped");
4767        }
4768
4769        // And what "still faces the rules" means: Go's `matchIPsOnly`, which this fork already
4770        // ported. A rule that opens *any* port to this destination admits the echo request, ports
4771        // ignored — so the drop above is the deny-all ruleset speaking, not the decode failing.
4772        assert!(
4773            admitted(&acl(IPV4_FIXTURE_NET, &[1], 443..=443), flows, v4(0x08, 0)),
4774            "an echo request is matched IPs-only, so a port-scoped ICMP rule admits it"
4775        );
4776    }
4777
4778    /// The reply carve-outs sit inside Go's proto switch, and `pre()` runs *before* that switch. So
4779    /// the unconditional multicast and link-local drops outrank both of them: a SYN-ACK or an echo
4780    /// reply addressed somewhere `pre()` refuses is still dropped, however much of a reply it is.
4781    ///
4782    /// This pins the branch ordering, which is the only thing keeping the carve-outs from
4783    /// re-opening destinations upstream closes unconditionally.
4784    #[test]
4785    fn pre_rule_drops_outrank_the_reply_carve_outs() {
4786        let ip = |s: &str| s.parse::<std::net::IpAddr>().unwrap();
4787        let src = ip("100.64.0.9");
4788        let verdict = |proto, dst, l4| {
4789            inbound_filter_verdict(
4790                &AllowAll,
4791                &mut flowtrack::FlowCache::default(),
4792                proto,
4793                std::net::SocketAddr::new(src, 443),
4794                std::net::SocketAddr::new(dst, 41234),
4795                l4,
4796                None,
4797            )
4798        };
4799        let syn_ack = ts_packetfilter::L4Header::Tcp {
4800            flags: TCP_SYN | TCP_ACK,
4801        };
4802        let echo_reply = ts_packetfilter::L4Header::Icmp {
4803            icmp_type: 0x00,
4804            icmp_code: 0,
4805        };
4806
4807        // The controls: to an ordinary tailnet address both are admitted.
4808        assert!(verdict(IpProto::TCP, ip("100.64.0.1"), syn_ack));
4809        assert!(verdict(IpProto::ICMP, ip("100.64.0.1"), echo_reply));
4810
4811        for dst in ["224.0.0.1", "169.254.1.1"] {
4812            assert!(
4813                !verdict(IpProto::TCP, ip(dst), syn_ack),
4814                "a non-SYN segment to {dst} is still dropped by pre()"
4815            );
4816            assert!(
4817                !verdict(IpProto::ICMP, ip(dst), echo_reply),
4818                "an echo reply to {dst} is still dropped by pre()"
4819            );
4820        }
4821        // The one link-local address `pre()` allows through is still allowed through.
4822        assert!(
4823            verdict(IpProto::ICMP, ip("169.254.169.254"), echo_reply),
4824            "the cloud-metadata exception is unaffected"
4825        );
4826    }
4827
4828    /// A *first* IPv6 fragment is decoded past its Fragment extension header, TCP flags included —
4829    /// Go `decode6` reaches `q.TCPFlags = TCPFlag(sub[13])` through exactly the same switch a
4830    /// unfragmented segment takes — so the reply carve-out applies to it on the same terms.
4831    ///
4832    /// Without this a peer that source-fragments could not complete a handshake this node started
4833    /// under a restrictive ACL; with it wrong, a fragmented SYN would slip past the rules. Both
4834    /// directions are asserted, on real bytes and on the classification itself.
4835    #[test]
4836    fn ipv6_first_fragment_carries_its_tcp_flags_to_the_reply_carve_out() {
4837        let flows = &mut flowtrack::FlowCache::default();
4838        let frag = |flags| ipv6_fragment_packet(6, 0, true, &tcp_header(443, 41234, flags));
4839
4840        assert!(
4841            admitted(&DenyAll, flows, frag(TCP_SYN | TCP_ACK)),
4842            "a fragmented SYN-ACK takes the non-SYN carve-out like an unfragmented one"
4843        );
4844        assert!(
4845            !admitted(&DenyAll, flows, frag(TCP_SYN)),
4846            "a fragmented SYN does not"
4847        );
4848
4849        // The classification carries the byte, not merely the verdict — this is the value the
4850        // verdict reads, and the ports are still Go's `sub[0:2]`/`sub[2:4]` beside it.
4851        assert_eq!(
4852            decode6_first_fragment(IpProto::TCP, &tcp_header(443, 41234, TCP_SYN | TCP_ACK)),
4853            Ipv6Fragment::First {
4854                proto: IpProto::TCP,
4855                src_port: 443,
4856                dst_port: 41234,
4857                l4: ts_packetfilter::L4Header::Tcp {
4858                    flags: TCP_SYN | TCP_ACK
4859                },
4860            },
4861            "`decode6` reads the flags byte past the Fragment header"
4862        );
4863    }
4864
4865    /// A dropped inbound packet's four-tuple, as `filter_inbound_from_peer` hands it to
4866    /// [`tsmp_reject_for_drop`]: the fixture peer dialling this node's SSH port.
4867    fn dropped_syn_tuple() -> (std::net::SocketAddr, std::net::SocketAddr) {
4868        (
4869            std::net::SocketAddr::new(IPV4_FIXTURE_SRC.into(), 41234),
4870            std::net::SocketAddr::new(IPV4_FIXTURE_DST.into(), 22),
4871        )
4872    }
4873
4874    /// The happy path of the send half, at the function Go's guard lives in: a dropped IPv4 TCP SYN
4875    /// produces a rejected-connection message addressed back at the dialer, carrying the four-tuple
4876    /// with the *connection's* own direction and `RejectedDueToACLs`.
4877    ///
4878    /// The assertion runs the emitted bytes back through the production parser, which is the far
4879    /// side's view of them — the peer has to read out the flow it opened, not the one we saw.
4880    #[test]
4881    fn an_acl_dropped_syn_produces_a_reject_addressed_back_to_the_dialer() {
4882        let (src, dst) = dropped_syn_tuple();
4883
4884        let bytes = tsmp_reject_for_drop(
4885            IpProto::TCP,
4886            src,
4887            dst,
4888            ts_packetfilter::L4Header::Tcp { flags: TCP_SYN },
4889            false,
4890            RejectConfig::default(),
4891        )
4892        .expect("a dropped IPv4 TCP SYN must be answered");
4893
4894        let reject = ts_packet::tsmp::TailscaleRejectedHeader::parse(&bytes)
4895            .expect("what we emit must parse as a rejected-connection message");
4896
4897        // Go `IPSrc: p.Dst.Addr(), IPDst: p.Src.Addr()`: the reply travels back the way the SYN
4898        // came, so the IP pair is reversed...
4899        assert_eq!(reject.ip_src, dst.ip(), "sourced from the address dialled");
4900        assert_eq!(reject.ip_dst, src.ip(), "addressed to the dialer");
4901        // ...while the four-tuple keeps the rejected connection's own direction, which is what lets
4902        // the dialer match it against the flow it opened.
4903        assert_eq!(reject.src, src);
4904        assert_eq!(reject.dst, dst);
4905        assert_eq!(reject.proto, IPPROTO_TCP_BYTE);
4906        assert_eq!(reject.reason, ts_packet::tsmp::RejectReason::ACLS);
4907        assert!(!reject.maybe_broken, "an ACL deny is a terminal verdict");
4908    }
4909
4910    /// Go: `if t.filter.ShieldsUp() { rj.Reason = packet.RejectedDueToShieldsUp }`. The two reasons
4911    /// render differently in a peer's connection history, so a shields-up deny must not be reported
4912    /// as an ACL deny.
4913    #[test]
4914    fn shields_up_is_a_distinct_reject_reason() {
4915        let (src, dst) = dropped_syn_tuple();
4916        let syn = ts_packetfilter::L4Header::Tcp { flags: TCP_SYN };
4917
4918        let reason = |shields_up| {
4919            ts_packet::tsmp::TailscaleRejectedHeader::parse(
4920                &tsmp_reject_for_drop(
4921                    IpProto::TCP,
4922                    src,
4923                    dst,
4924                    syn,
4925                    shields_up,
4926                    RejectConfig::default(),
4927                )
4928                .expect("emitted"),
4929            )
4930            .expect("parses")
4931            .reason
4932        };
4933
4934        assert_eq!(reason(true), ts_packet::tsmp::RejectReason::SHIELDS_UP);
4935        assert_eq!(reason(false), ts_packet::tsmp::RejectReason::ACLS);
4936    }
4937
4938    /// Everything Go stays **silent** about. Each of these is a packet the ACL drops just the same;
4939    /// the drop is unchanged and only the reply is withheld, which is what makes them the part of
4940    /// the port worth testing.
4941    #[test]
4942    fn most_drops_produce_no_reject_at_all() {
4943        let (src, dst) = dropped_syn_tuple();
4944        let syn = ts_packetfilter::L4Header::Tcp { flags: TCP_SYN };
4945        let v6_src = std::net::SocketAddr::new(std::net::IpAddr::V6(IPV6_FIXTURE_SRC), 41234);
4946        let v6_dst = std::net::SocketAddr::new(std::net::IpAddr::V6(IPV6_FIXTURE_DST), 22);
4947
4948        for (name, proto, src, dst, l4, cfg) in [
4949            // Go `p.TCPFlags&packet.TCPSyn != 0`: a segment with no SYN bit is not a connection
4950            // being opened, so there is nothing to refuse.
4951            (
4952                "a non-SYN segment",
4953                IpProto::TCP,
4954                src,
4955                dst,
4956                ts_packetfilter::L4Header::Tcp {
4957                    flags: TCP_ACK | TCP_FIN,
4958                },
4959                RejectConfig::default(),
4960            ),
4961            (
4962                "a RST",
4963                IpProto::TCP,
4964                src,
4965                dst,
4966                ts_packetfilter::L4Header::Tcp { flags: TCP_RST },
4967                RejectConfig::default(),
4968            ),
4969            // Go `p.IPProto == ipproto.TCP`: a datagram protocol has no SYN and no connection.
4970            (
4971                "a UDP datagram",
4972                IpProto::UDP,
4973                src,
4974                dst,
4975                ts_packetfilter::L4Header::Unknown,
4976                RejectConfig::default(),
4977            ),
4978            // A UDP datagram whose 14th payload byte happens to have the SYN bit pattern must not
4979            // be read as a TCP header: the protocol number decides, as it does in Go's guard.
4980            (
4981                "a UDP datagram carrying SYN-shaped bytes",
4982                IpProto::UDP,
4983                src,
4984                dst,
4985                syn,
4986                RejectConfig::default(),
4987            ),
4988            (
4989                "an ICMP echo request",
4990                IpProto::ICMP,
4991                src,
4992                dst,
4993                ts_packetfilter::L4Header::Icmp {
4994                    icmp_type: 8,
4995                    icmp_code: 0,
4996                },
4997                RejectConfig::default(),
4998            ),
4999            // Go `p.IPVersion == 4`: an IPv6 drop is silent, even though the message itself is
5000            // family-agnostic and `TailscaleRejectedHeader::marshal` would happily emit one.
5001            (
5002                "an IPv6 SYN",
5003                IpProto::TCP,
5004                v6_src,
5005                v6_dst,
5006                syn,
5007                RejectConfig::default(),
5008            ),
5009            // A packet whose L4 header was never decoded — a first IPv4 fragment, say — is not
5010            // known to be a SYN. Go reaches the same place: a header it could not decode leaves
5011            // `q.TCPFlags` zero.
5012            (
5013                "a SYN whose TCP header was never decoded",
5014                IpProto::TCP,
5015                src,
5016                dst,
5017                ts_packetfilter::L4Header::Unknown,
5018                RejectConfig::default(),
5019            ),
5020            // Go's peerAPI carve-out, which flips an ACL-refused peerAPI SYN back to `Accept`
5021            // before the reject block runs. Same outcome reached here by withholding the reply.
5022            (
5023                "a SYN to the peerAPI port",
5024                IpProto::TCP,
5025                src,
5026                dst,
5027                syn,
5028                RejectConfig {
5029                    disabled: false,
5030                    peerapi_port: Some(dst.port()),
5031                },
5032            ),
5033            // Go `!t.disableTSMPRejected`, the node-wide off switch.
5034            (
5035                "any SYN at all, with rejects disabled",
5036                IpProto::TCP,
5037                src,
5038                dst,
5039                syn,
5040                RejectConfig {
5041                    disabled: true,
5042                    peerapi_port: None,
5043                },
5044            ),
5045        ] {
5046            assert!(
5047                tsmp_reject_for_drop(proto, src, dst, l4, false, cfg).is_none(),
5048                "{name} must be dropped silently"
5049            );
5050        }
5051
5052        // Control: with the peerAPI on some *other* port, the same SYN is answered — the carve-out
5053        // is scoped to the port, not a blanket mute.
5054        assert!(
5055            tsmp_reject_for_drop(
5056                IpProto::TCP,
5057                src,
5058                dst,
5059                syn,
5060                false,
5061                RejectConfig {
5062                    disabled: false,
5063                    peerapi_port: Some(dst.port() + 1),
5064                },
5065            )
5066            .is_some(),
5067            "a SYN to a port that is not the peerAPI's is still answered"
5068        );
5069    }
5070
5071    /// The send half through the real filter step, on real bytes: an ACL that denies everything
5072    /// drops the SYN *and* queues a reject for the peer that sent it, while the packets Go stays
5073    /// silent about queue nothing. This is the assertion that the guard is wired into the drop
5074    /// path at all, not merely correct in isolation.
5075    #[test]
5076    fn the_filter_step_queues_a_reject_only_for_a_dropped_ipv4_syn() {
5077        let peer = PeerId(7);
5078
5079        // Returns `(survived, harvest)`: whether the ACL admitted the packet, and what the filter
5080        // step produced alongside it.
5081        let run = |packet: Vec<u8>| {
5082            let mut packets = vec![PacketMut::from(packet)];
5083            let mut harvest = InboundHarvest::default();
5084            filter_inbound_from_peer(
5085                &DenyAll,
5086                &mut flowtrack::FlowCache::default(),
5087                peer,
5088                &mut packets,
5089                RejectConfig::default(),
5090                &mut harvest,
5091            );
5092            (!packets.is_empty(), harvest)
5093        };
5094
5095        let (survived, harvest) = run(v4_packet(6, 0, false, &tcp_header(41234, 22, TCP_SYN)));
5096        assert!(!survived, "the deny-all ACL drops the SYN");
5097        assert_eq!(
5098            harvest.rejects_to_send.len(),
5099            1,
5100            "a dropped SYN is answered"
5101        );
5102        assert_eq!(harvest.rejects_to_send[0].0, peer, "back to the sender");
5103        let reject =
5104            ts_packet::tsmp::TailscaleRejectedHeader::parse(harvest.rejects_to_send[0].1.as_ref())
5105                .expect("the queued packet is a rejected-connection message");
5106        assert_eq!(reject.src.port(), 41234);
5107        assert_eq!(reject.dst.port(), 22);
5108        assert_eq!(reject.reason, ts_packet::tsmp::RejectReason::ACLS);
5109
5110        // A segment that is not a SYN never produces one. Under this deny-all ACL it is in fact
5111        // *admitted*, by the "tcp non-syn" reply carve-out that runs ahead of the rules — which is
5112        // the more useful assertion anyway: the reject path must not fire on a packet that was
5113        // never dropped in the first place.
5114        let (survived, harvest) = run(v4_packet(6, 0, false, &tcp_header(41234, 22, TCP_RST)));
5115        assert!(survived, "Go's 'tcp non-syn' carve-out admits it");
5116        assert!(harvest.rejects_to_send.is_empty(), "and answers nothing");
5117
5118        for (name, packet) in [
5119            ("a UDP datagram", v4_packet(17, 0, false, &udp_header(22))),
5120            (
5121                "an IPv6 SYN",
5122                ipv6_packet(6, &tcp_header(41234, 22, TCP_SYN)),
5123            ),
5124        ] {
5125            let (survived, harvest) = run(packet);
5126            assert!(!survived, "{name} is dropped by the deny-all ACL");
5127            assert!(
5128                harvest.rejects_to_send.is_empty(),
5129                "{name} must be dropped in silence"
5130            );
5131        }
5132    }
5133
5134    /// The peerAPI ordering, through the real filter step: a SYN to the peerAPI port is dropped by
5135    /// the ACL exactly as before — this fork has no L7 carve-out that admits it — but the peer is
5136    /// told nothing, because upstream's carve-out has already turned that packet into an `Accept`
5137    /// by the time its reject block runs.
5138    #[test]
5139    fn a_peerapi_syn_is_dropped_without_a_reject() {
5140        let mut packets = vec![PacketMut::from(v4_packet(
5141            6,
5142            0,
5143            false,
5144            &tcp_header(41234, 8089, TCP_SYN),
5145        ))];
5146        let mut harvest = InboundHarvest::default();
5147        filter_inbound_from_peer(
5148            &DenyAll,
5149            &mut flowtrack::FlowCache::default(),
5150            PeerId(7),
5151            &mut packets,
5152            RejectConfig {
5153                disabled: false,
5154                peerapi_port: Some(8089),
5155            },
5156            &mut harvest,
5157        );
5158
5159        assert!(packets.is_empty(), "the verdict on the packet is unchanged");
5160        assert!(
5161            harvest.rejects_to_send.is_empty(),
5162            "a peerAPI SYN must never produce a reject"
5163        );
5164    }
5165
5166    /// The receive half through the real filter step: a peer's rejected-connection message is
5167    /// consumed — harvested for the embedder and **dropped**, never handed to the local stack,
5168    /// which has no handler for IP protocol 99 (Go `filter.DropSilently`).
5169    ///
5170    /// The ACL denies everything, so the packet surviving could only have come from the TSMP
5171    /// bypass; asserting it does *not* survive is the whole point.
5172    #[test]
5173    fn a_received_reject_is_consumed_and_not_delivered() {
5174        let peer = PeerId(7);
5175
5176        // The bytes a Go peer puts on the wire when its ACL refuses our dial: it marshals from its
5177        // own side, we read it from ours.
5178        let wire = ts_packet::tsmp::TailscaleRejectedHeader {
5179            ip_src: IPV4_FIXTURE_SRC.into(),
5180            ip_dst: IPV4_FIXTURE_DST.into(),
5181            src: std::net::SocketAddr::new(IPV4_FIXTURE_DST.into(), 41234),
5182            dst: std::net::SocketAddr::new(IPV4_FIXTURE_SRC.into(), 22),
5183            proto: IPPROTO_TCP_BYTE,
5184            reason: ts_packet::tsmp::RejectReason::ACLS,
5185            maybe_broken: false,
5186        }
5187        .marshal()
5188        .expect("marshals");
5189
5190        let mut packets = vec![PacketMut::from(wire)];
5191        let mut harvest = InboundHarvest::default();
5192        filter_inbound_from_peer(
5193            &DenyAll,
5194            &mut flowtrack::FlowCache::default(),
5195            peer,
5196            &mut packets,
5197            RejectConfig::default(),
5198            &mut harvest,
5199        );
5200
5201        assert!(
5202            packets.is_empty(),
5203            "a reject must be consumed, not delivered to the local stack"
5204        );
5205        assert_eq!(harvest.rejected_flows.len(), 1);
5206        assert_eq!(harvest.rejected_flows[0].0, peer);
5207        let reject = harvest.rejected_flows[0].1;
5208        assert_eq!(
5209            reject.src,
5210            std::net::SocketAddr::new(IPV4_FIXTURE_DST.into(), 41234),
5211            "src is OUR address and ephemeral port — the flow we opened"
5212        );
5213        assert_eq!(
5214            reject.dst,
5215            std::net::SocketAddr::new(IPV4_FIXTURE_SRC.into(), 22)
5216        );
5217        assert_eq!(reject.reason, ts_packet::tsmp::RejectReason::ACLS);
5218        assert!(!reject.maybe_broken);
5219        assert!(
5220            harvest.rejects_to_send.is_empty(),
5221            "consuming a reject must not answer it with another one"
5222        );
5223
5224        // The `MaybeBroken` bit reaches the embedder intact: Go treats such a rejection as
5225        // non-terminal (it marks the flow problematic instead of removing it), so flattening the
5226        // bit would turn a transient problem into a hard failure.
5227        let broken = ts_packet::tsmp::TailscaleRejectedHeader {
5228            ip_src: IPV4_FIXTURE_SRC.into(),
5229            ip_dst: IPV4_FIXTURE_DST.into(),
5230            src: std::net::SocketAddr::new(IPV4_FIXTURE_DST.into(), 41234),
5231            dst: std::net::SocketAddr::new(IPV4_FIXTURE_SRC.into(), 22),
5232            proto: IPPROTO_TCP_BYTE,
5233            reason: ts_packet::tsmp::RejectReason::IP_FORWARDING,
5234            maybe_broken: true,
5235        }
5236        .marshal()
5237        .expect("marshals");
5238        let mut packets = vec![PacketMut::from(broken)];
5239        let mut harvest = InboundHarvest::default();
5240        filter_inbound_from_peer(
5241            &DenyAll,
5242            &mut flowtrack::FlowCache::default(),
5243            peer,
5244            &mut packets,
5245            RejectConfig::default(),
5246            &mut harvest,
5247        );
5248        assert!(packets.is_empty());
5249        assert!(harvest.rejected_flows[0].1.maybe_broken);
5250        assert_eq!(
5251            harvest.rejected_flows[0].1.reason,
5252            ts_packet::tsmp::RejectReason::IP_FORWARDING
5253        );
5254    }
5255
5256    /// The one reason that is **not** consumed: `RejectedDueToUnknownAppConnectorTransitIP`.
5257    ///
5258    /// Go's `trackOpenPreFilterIn` switches on the reason before settling its verdict and leaves
5259    /// `res = filter.Accept` for `'T'` — "don't drop this packet because it will be used later for
5260    /// further communication between app connector and client". Everything else takes
5261    /// `filter.DropSilently`. This fork is always the dialing client, so a real Go app connector is
5262    /// the thing that sends one of these, and swallowing it breaks the exchange that re-establishes
5263    /// the transit-IP binding.
5264    ///
5265    /// Both halves are asserted against the same peer, the same ACL and two headers that differ in
5266    /// one byte, so the survival can only be the reason byte and not some property of the fixture.
5267    #[test]
5268    fn an_app_connector_transit_ip_reject_is_passed_through_not_consumed() {
5269        let peer = PeerId(7);
5270
5271        let wire = |reason| {
5272            ts_packet::tsmp::TailscaleRejectedHeader {
5273                ip_src: IPV4_FIXTURE_SRC.into(),
5274                ip_dst: IPV4_FIXTURE_DST.into(),
5275                src: std::net::SocketAddr::new(IPV4_FIXTURE_DST.into(), 41234),
5276                dst: std::net::SocketAddr::new(IPV4_FIXTURE_SRC.into(), 443),
5277                proto: IPPROTO_TCP_BYTE,
5278                reason,
5279                maybe_broken: false,
5280            }
5281            .marshal()
5282            .expect("marshals")
5283        };
5284
5285        let run = |reason| {
5286            let mut packets = vec![PacketMut::from(wire(reason))];
5287            let mut harvest = InboundHarvest::default();
5288            filter_inbound_from_peer(
5289                &DenyAll,
5290                &mut flowtrack::FlowCache::default(),
5291                peer,
5292                &mut packets,
5293                RejectConfig::default(),
5294                &mut harvest,
5295            );
5296            (packets, harvest)
5297        };
5298
5299        let (packets, harvest) =
5300            run(ts_packet::tsmp::RejectReason::UNKNOWN_APP_CONNECTOR_TRANSIT_IP);
5301        assert_eq!(
5302            packets.len(),
5303            1,
5304            "a 'T' reject is Go's one `filter.Accept` — it must reach the local stack, \
5305             not be eaten by the filter step"
5306        );
5307        assert_eq!(
5308            ts_packet::tsmp::TailscaleRejectedHeader::parse(packets[0].as_ref())
5309                .expect("what survives is still the reject the peer sent")
5310                .reason,
5311            ts_packet::tsmp::RejectReason::UNKNOWN_APP_CONNECTOR_TRANSIT_IP,
5312            "and it is passed through unmodified"
5313        );
5314        // Upstream logs, notifies and *then* switches, so accepting does not cost the embedder the
5315        // record — it gets both.
5316        assert_eq!(harvest.rejected_flows.len(), 1);
5317        assert_eq!(harvest.rejected_flows[0].0, peer);
5318        assert_eq!(
5319            harvest.rejected_flows[0].1.reason,
5320            ts_packet::tsmp::RejectReason::UNKNOWN_APP_CONNECTOR_TRANSIT_IP
5321        );
5322        assert!(
5323            harvest.rejects_to_send.is_empty(),
5324            "passing a reject through must not answer it with another one"
5325        );
5326
5327        // The contrast, one byte away: every other reason keeps Go's `filter.DropSilently`.
5328        for reason in [
5329            ts_packet::tsmp::RejectReason::ACLS,
5330            ts_packet::tsmp::RejectReason::SHIELDS_UP,
5331            ts_packet::tsmp::RejectReason::IP_FORWARDING,
5332            ts_packet::tsmp::RejectReason::HOST_FIREWALL,
5333            // Go's `default` arm covers a reason this build has never heard of, too.
5334            ts_packet::tsmp::RejectReason(b'Z'),
5335        ] {
5336            let (packets, harvest) = run(reason);
5337            assert!(
5338                packets.is_empty(),
5339                "a {reason} reject is consumed, never delivered"
5340            );
5341            assert_eq!(harvest.rejected_flows.len(), 1, "{reason}");
5342        }
5343    }
5344
5345    /// A peer cannot drive the default-level log with rejected-connection messages.
5346    ///
5347    /// Go logs `open-conn-track` only for a flow sitting in its pending-open table, so its volume
5348    /// is bounded by the dials *this* node made. This fork keeps no such table and logs every
5349    /// well-formed reject it can attribute to a peer, which bounds the volume by what the peer
5350    /// chooses to send instead — nothing here matches a pending flow and nothing rate-limits it.
5351    /// At `info!` that is an authenticated peer holding the operator's default-level log open;
5352    /// `debug!` keeps the diagnostic without putting it there.
5353    ///
5354    /// The record itself must keep flowing regardless: `rejected_flows` is the path the embedder
5355    /// uses to do the pending-open match this layer cannot, so quieting the log must not quiet it.
5356    #[test]
5357    #[tracing_test::traced_test]
5358    fn a_flood_of_rejects_stays_off_the_default_log_level() {
5359        let peer = PeerId(7);
5360        // More than a real flow-refusal burst, few enough to keep the test cheap: what matters is
5361        // that the count of default-level lines does not track it.
5362        const FLOOD: usize = 32;
5363
5364        let wire = ts_packet::tsmp::TailscaleRejectedHeader {
5365            ip_src: IPV4_FIXTURE_SRC.into(),
5366            ip_dst: IPV4_FIXTURE_DST.into(),
5367            src: std::net::SocketAddr::new(IPV4_FIXTURE_DST.into(), 41234),
5368            dst: std::net::SocketAddr::new(IPV4_FIXTURE_SRC.into(), 22),
5369            proto: IPPROTO_TCP_BYTE,
5370            reason: ts_packet::tsmp::RejectReason::ACLS,
5371            maybe_broken: false,
5372        }
5373        .marshal()
5374        .expect("marshals");
5375
5376        let mut packets: Vec<PacketMut> =
5377            (0..FLOOD).map(|_| PacketMut::from(wire.clone())).collect();
5378        let mut harvest = InboundHarvest::default();
5379        filter_inbound_from_peer(
5380            &DenyAll,
5381            &mut flowtrack::FlowCache::default(),
5382            peer,
5383            &mut packets,
5384            RejectConfig::default(),
5385            &mut harvest,
5386        );
5387
5388        assert!(packets.is_empty(), "every reject is consumed");
5389        assert_eq!(
5390            harvest.rejected_flows.len(),
5391            FLOOD,
5392            "the embedder must still receive every record — quieting the log is not dropping it"
5393        );
5394
5395        logs_assert(|lines: &[&str]| {
5396            let tracked: Vec<&&str> = lines
5397                .iter()
5398                .filter(|l| l.contains("open-conn-track"))
5399                .collect();
5400            if tracked.len() != FLOOD {
5401                return Err(format!(
5402                    "expected {FLOOD} open-conn-track lines at debug, got {}",
5403                    tracked.len()
5404                ));
5405            }
5406            // `INFO` and above is what an operator sees by default; a peer must not reach it.
5407            let loud: Vec<&&&str> = tracked.iter().filter(|l| !l.contains(" DEBUG ")).collect();
5408            if let Some(first) = loud.first() {
5409                return Err(format!(
5410                    "{} of {FLOOD} peer-driven reject line(s) above debug, e.g. {first}",
5411                    loud.len()
5412                ));
5413            }
5414            Ok(())
5415        });
5416    }
5417
5418    /// A *fragmented* rejected-connection message is refused before the ACL ever sees it, and is
5419    /// not consumed either: Go's `decode4` demotes a first TSMP fragment with More-Fragments set to
5420    /// `ipproto.Unknown`, which `pre()` drops, and classifies a later fragment as
5421    /// `ipproto.Fragment`. Without the whole message in hand it cannot be a valid control packet,
5422    /// and half a reject must never be read as a flow being refused.
5423    #[test]
5424    fn a_fragmented_reject_is_refused_before_the_acl() {
5425        let body = {
5426            let mut b = vec![
5427                ts_packet::tsmp::TSMP_TYPE_REJECTED_CONN,
5428                IPPROTO_TCP_BYTE,
5429                b'A',
5430            ];
5431            b.extend_from_slice(&41234u16.to_be_bytes());
5432            b.extend_from_slice(&22u16.to_be_bytes());
5433            b.push(0);
5434            b
5435        };
5436
5437        // Control: unfragmented, the same body is consumed as a reject.
5438        let mut packets = vec![PacketMut::from(v4_packet(99, 0, false, &body))];
5439        let mut harvest = InboundHarvest::default();
5440        filter_inbound_from_peer(
5441            &DenyAll,
5442            &mut flowtrack::FlowCache::default(),
5443            PeerId(7),
5444            &mut packets,
5445            RejectConfig::default(),
5446            &mut harvest,
5447        );
5448        assert!(packets.is_empty() && harvest.rejected_flows.len() == 1);
5449
5450        // A first fragment with More-Fragments set, and a later fragment. Neither is parsed as a
5451        // reject. What happens to the bytes afterwards is the fragment rule this tree already
5452        // had, unchanged: the first fragment is demoted to `unknown` and dropped, the later
5453        // fragment takes Go's stateless pass-through (`pre()`'s `case ipproto.Fragment: Accept`).
5454        for (name, offset, more, delivered) in [
5455            ("first fragment, MF set", 0, true, false),
5456            ("later fragment", MIN_FRAG_BLKS, false, true),
5457        ] {
5458            let mut packets = vec![PacketMut::from(v4_packet(99, offset, more, &body))];
5459            let mut harvest = InboundHarvest::default();
5460            filter_inbound_from_peer(
5461                &DenyAll,
5462                &mut flowtrack::FlowCache::default(),
5463                PeerId(7),
5464                &mut packets,
5465                RejectConfig::default(),
5466                &mut harvest,
5467            );
5468            assert!(
5469                harvest.rejected_flows.is_empty(),
5470                "{name}: half a reject is not a rejected flow"
5471            );
5472            assert_eq!(
5473                packets.len(),
5474                usize::from(delivered),
5475                "{name}: the existing fragment rule decides what happens to the bytes"
5476            );
5477        }
5478    }
5479
5480    /// Both halves meeting over a real WireGuard session, which is the only test that proves the
5481    /// reject is actually *sent*: A dials B, B's ACL refuses the SYN, and A learns why.
5482    ///
5483    /// This engine is always the dialing client, so A's side is the one that matters day to day —
5484    /// without it a dial into a peer's ACL drop waits out a TCP timeout with no reason anywhere.
5485    /// B's side is the peer-observable one: a real Go node dialling into our ACL drop expects to be
5486    /// told, and was not.
5487    #[test]
5488    fn a_refused_dial_is_answered_over_the_wire_and_understood() {
5489        let underlay: UnderlayTransportId = 0.into();
5490        let wg_peer = ts_tunnel::PeerId(1);
5491        let peer = PeerId(1);
5492        let a_addr = std::net::IpAddr::from(IPV4_FIXTURE_SRC);
5493        let b_addr = std::net::IpAddr::from(IPV4_FIXTURE_DST);
5494
5495        let (a_static, b_static) = (NodeKeyPair::new(), NodeKeyPair::new());
5496        let (mut a, mut b) = (
5497            DataPlane::new(a_static.clone()),
5498            DataPlane::new(b_static.clone()),
5499        );
5500
5501        for (dp, key, remote) in [
5502            (&mut a, b_static.public, b_addr),
5503            (&mut b, a_static.public, a_addr),
5504        ] {
5505            dp.wireguard.upsert_peer(
5506                wg_peer,
5507                ts_tunnel::PeerConfig {
5508                    key,
5509                    psk: [0u8; 32].into(),
5510                    persistent_keepalive_interval: None,
5511                },
5512            );
5513            dp.ur_out.table.insert(peer, underlay);
5514            // Each side attributes the other's tailnet address to the WireGuard peer carrying it,
5515            // as the runtime's source filter does.
5516            let mut src_filter = ts_bart::Table::default();
5517            src_filter.insert(ipnet::IpNet::from(remote), peer);
5518            dp.src_filter_in = Arc::new(src_filter);
5519        }
5520
5521        // B refuses everything: this is the ACL drop the message exists to explain.
5522        b.packet_filter = Arc::new(DenyAll);
5523
5524        let take = |out: HashMap<(UnderlayTransportId, PeerId), Vec<PacketMut>>| {
5525            out.into_values().flatten().collect::<Vec<_>>()
5526        };
5527
5528        // A dials B. The SYN rides the handshake this send starts.
5529        let syn = PacketMut::from(v4_packet(6, 0, false, &tcp_header(41234, 22, TCP_SYN)));
5530        let init = a
5531            .wireguard
5532            .send([(wg_peer, vec![syn])])
5533            .to_peers
5534            .remove(&wg_peer)
5535            .expect("handshake initiation");
5536        let resp = take(b.process_inbound(init).to_peers);
5537        let from_a = take(a.process_inbound(resp).to_peers);
5538
5539        // B drops the SYN on its ACL and answers with a rejected-connection message.
5540        let at_b = b.process_inbound(from_a);
5541        assert!(
5542            at_b.to_local.values().all(|v| v.is_empty()),
5543            "the SYN itself must not reach B's local stack"
5544        );
5545        let to_a = take(at_b.to_peers);
5546        assert!(
5547            !to_a.is_empty(),
5548            "B must tell A why it dropped the connection"
5549        );
5550
5551        // A consumes it: it learns the flow was refused and the message never reaches its stack.
5552        let at_a = a.process_inbound(to_a);
5553        assert!(
5554            at_a.to_local.values().all(|v| v.is_empty()),
5555            "the reject must be consumed, not delivered to A's local stack"
5556        );
5557        assert_eq!(at_a.rejected_flows.len(), 1, "A must learn of the refusal");
5558        let (from_peer, reject) = at_a.rejected_flows[0];
5559        assert_eq!(from_peer, peer);
5560        assert_eq!(
5561            reject.src,
5562            std::net::SocketAddr::new(a_addr, 41234),
5563            "the flow A opened, from A's own address and ephemeral port"
5564        );
5565        assert_eq!(reject.dst, std::net::SocketAddr::new(b_addr, 22));
5566        assert_eq!(reject.proto, IPPROTO_TCP_BYTE);
5567        assert_eq!(reject.reason, ts_packet::tsmp::RejectReason::ACLS);
5568        assert!(!reject.maybe_broken);
5569
5570        // And A does not answer the reject with a reject of its own — that would be a loop between
5571        // two nodes that both deny.
5572        assert!(
5573            take(at_a.to_peers).is_empty(),
5574            "consuming a reject must put nothing back on the wire"
5575        );
5576    }
5577}