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