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