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