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