23 Commits
Author SHA1 Message Date
Mazdak Nasab 2e8f381c2e feature/conn25: guard against an unusable Transit IP (#22513)
ClientPrepareNewFlowForMagicIP folded resolving a Magic IP and counting the
flow into one call, and the datapath dropped its own IsValid check on the
resolved Transit IP in favor of trusting the new ok return. That left the
validity requirement stated only in the Conn25Datapath doc comment, and an
implementation that violates it panics the packet path two different ways.
A zero Transit IP is neither Is4 nor Is6, so it slips past both of
checksum.UpdateDstAddr's family-mismatch panics and reaches As4, which
panics in turn; a valid Transit IP in the other family hits one of those
family-mismatch panics outright. Check for both, releasing the counted
reference since no flow is installed, so the packet passes through
unmodified as before.

Updates https://github.com/tailscale/corp/issues/44273

Change-Id: Ib0c0eaa92bc4f0470299da08d48c865e1d101849

Signed-off-by: Mazdak Nasab <[email protected]>
2026-11-19 15:11:35 -07:00
Mazdak Nasab 4f19a4aff8 feature/conn25: reject Magic IPs whose Transit IP isn't in WireGuard yet (#22374)
A Magic IP gets its address assignment when the DNS response is rewritten,
but the Transit IP it maps to only becomes usable later: handleAddressAssignment
registers it with the connector over PeerAPI, and only then does
addTransitIPForConnector put it in byConnKey, where extraWireGuardAllowedIPs
picks it up and hands it to WireGuard as an allowed IP for that peer.

Until then, transitIPForMagicIP would happily return the Transit IP, the
datapath would DNAT to it, and WireGuard would drop the packet because no
peer claims the address. The client application saw a black hole and hung.
That window stays open indefinitely when the registration never lands: a
failed PeerAPI request, a full assignment queue, or a reset that clears
byConnKey.

transitIPForMagicIP now checks that the Transit IP is registered under its
connector's node key, and returns the new ErrTransitIPNotRoutable if it
isn't. The datapath treats that like an unknown Magic IP and injects an
ICMP host unreachable, so the application fails fast instead of hanging.

Fixes https://github.com/tailscale/corp/issues/50405

Change-Id: Id232e27063f27278e65279d9b1adad5d67d3b6ef

Signed-off-by: Mazdak Nasab <[email protected]>
2026-11-17 15:10:44 -07:00
Mazdak Nasab 04ed834b36 feature/conn25: resolve and count client flows atomically (#22288)
The datapath resolved a Magic IP to its Transit IP and then, in a second
lock acquisition, counted the new flow against the assignment holding
that Transit IP. A concurrent reserveAddresses could expire the
assignment in between and hand the Transit IP to an unrelated
destination, so the flow was installed DNATing to an address another
destination now owned, counted against that stranger's assignment.

Merge the two steps into ClientFlowCreatedForMagicIP, which resolves and
counts under one c.mu acquisition.

Fixes https://github.com/tailscale/corp/issues/44273

Change-Id: Iefae902267fd06a5d2c965a3570b6977921be76f

Signed-off-by: Mazdak Nasab <[email protected]>
2026-11-16 12:19:54 -07:00
Mazdak Nasab ad8bad6114 feature/conn25: make client flow cleanups idempotent (#22280)
The cleanup returned by flowCreated decremented activeFlowCount on every
call, and its documented contract pushed the burden of calling it exactly
once onto the datapath. A caller that lost track and ran a cleanup twice
would undercount the flows still forwarding over the assignment, letting
popExpired reclaim both of its addresses back to the pools mid-connection,
or drive the count to -1, which can never again satisfy popExpired's
zero-flow test and so pins the assignment in the expiry heap and leaks
both addresses for the life of the process.

The cleanup now releases its hold at most once, guarded by a bool read and
written under c.mu alongside the decrement it protects. Repeat calls are
no-ops. The Conn25Datapath docs no longer ask callers to guarantee this.

No such double-call is reachable through FlowTable today: removeFlowLocked
deletes both cache entries, so NewFlow's second lookup misses, and the
capacity eviction takes lru.Back(), never the flow just pushed to the
front. This removes the class rather than a live bug.

Updates https://github.com/tailscale/corp/issues/50760

Change-Id: I1888f36eb8fdec9b219ccfd2d290dc4eb19c55a0

Signed-off-by: Mazdak Nasab <[email protected]>
2026-11-10 09:19:22 -07:00
Mazdak Nasab 7ed0138541 feature/conn25: match client flow removals to the assignment they counted (#22142)
flowRemoved identified an address assignment by transit IP alone, but a
transit IP does not identify an assignment for the lifetime of a flow.
An assignment can be dropped while a flow created against it is still in
the client flow table, and the pools then hand the same transit IP back
out to an unrelated assignment. The stale removal was charged to that
stranger, dropping its activeFlowCount while its own flows were still
forwarding, so popExpired reclaimed both of its addresses back to the
pools mid-connection. If the stranger had no flows of its own the count
went to -1, which can never again satisfy popExpired's zero-flow test,
pinning it in the expiry heap and leaking both addresses for the life of
the process.

flowCreated now returns a function, which the datapath calls on removal
to clean up the flow.

Fixes https://github.com/tailscale/corp/issues/50760

Change-Id: I3693c487b1031737763b5e49f8f8400c72848e8e

Signed-off-by: Mazdak Nasab <[email protected]>
2026-11-09 15:23:33 -07:00
Mazdak Nasab 6372b9a43a feature/conn25: shut the address assignment queue down from the sender side (#22269)
extension.Shutdown closed client.addrsCh, but the hooks that feed that
channel are still live at that point. LocalBackend.shutdown shuts the
extension host down before closing the engine, and nothing unregisters
what installHooks registered on the DNS manager and the tun device, so a
DNS response arriving during the shutdown window still reaches
mapDNSResponse -> rewriteDNSResponse -> reserveAddresses ->
enqueueAddressAssignment, and sends on the closed channel. That's a
panic, not a dropped assignment.

Every send already happens under c.mu, so the client can shut its own
queue down: client.shutdown takes the lock, sets a closed flag and
drains the queue, and enqueueAddressAssignment refuses with
errClientShutdown from then on.

A late DNS response now fails the reservation instead of queueing it for
a sendLoop that is gone. reserveAddresses rolls back, returning the
magic and transit IPs to their pools, and the query servfails rather
than being answered with a magic IP that no connector will ever hear
about.

Fixes https://github.com/tailscale/corp/issues/51235

Change-Id: I712ba8493e77b8176e133decc6097efb52b1b4ab

Signed-off-by: Mazdak Nasab <[email protected]>
2026-11-09 13:37:58 -07:00
Mazdak Nasab 3dbb215e57 feature/conn25: drop address assignments queued before a client.reset (#22210)
Give the client a generation counter, bump it in reset, and stamp each
assignment with it as it is queued. insertTransitConnMapping rejects
assignments from an older generation; because that check happens under
c.mu it is exclusive with reset, which is what actually closes the
window. handleAddressAssignment also checks before sending, so an
assignment invalidated while it was still queued doesn't reach a
connector at all.

Fixes https://github.com/tailscale/corp/issues/51125

Change-Id: Ib524324b6e8a196fdcb5b4cd307854f61c7a0f04

Signed-off-by: Mazdak Nasab <[email protected]>
2026-11-06 12:04:36 -07:00
Mazdak Nasab 8aa18b518f feature/conn25: drain the address assignment queue in client.reset (#22186)
client.reset clears the IP pools, assignments and byConnKey, but leaves
addrsCh alone. The channel is buffered, so assignments queued before the
reset are still delivered by sendLoop afterwards: each one POSTs to a
connector from the profile we just left, registering a transit IP this
client no longer knows about, and then fails in addTransitIPForConnector
because the assignment is gone from c.assignments.

Drain addrsCh at the end of reset. The drain is non-blocking, so holding
c.mu can't deadlock against reserveAddresses, which enqueues under the
same lock, or against sendLoop, which receives without it.

Fixes https://github.com/tailscale/tailscale/issues/22185

Change-Id: I579131bc54e82d2c797b4741ccaf6cbe7abfdc4a

Signed-off-by: Mazdak Nasab <[email protected]>
2026-11-05 12:16:06 -07:00
Mazdak Nasab 274a3f2023 feature/conn25: roll back the address assignment when the send queue is full (#22165)
reserveAddresses inserts the assignment into c.assignments before
enqueueing it, and leaves it there when the enqueue fails. The connector
never learns about such a mapping, and nothing tell it later.
Add addrAssignments.remove and use it to undo the insert and return both
addresses to their pools when enqueueAddressAssignment fails. The caller
still gets a SERVFAIL, but the next DNS query starts from a clean slate.

Fixes https://github.com/tailscale/corp/issues/50917

Change-Id: I5a387bdcf5a5e87e2dca44867e88ed6149678729

Signed-off-by: Mazdak Nasab <[email protected]>
2026-11-04 14:14:10 -07:00
Mazdak Nasab 35d8212746 feature/conn25: block destinations inside the magic and transit IP pools (#22147)
Similar to b6fc02d for private destinations, a destination inside one of
the configured magic or transit IP pools is never somewhere an app can be
reached: the magic range is installed as a route towards Tailscale on
every configured node, and the transit range belongs to conn25's own
peer-to-connector mappings. Forwarding to one routes back into the
mapping layer instead of out to an app.

Updates https://github.com/tailscale/corp/issues/50118
Updates https://github.com/tailscale/corp/issues/46169

Change-Id: I8116543d4b7a02e1f4c3c6b882ee3ddec1ac4f97

Signed-off-by: Mazdak Nasab <[email protected]>
2026-11-02 14:30:42 -07:00
Mazdak Nasab b6fc02d501 feature/conn25: block private destinations (#22073)
Similar to 7304dd4 for host-scoped destinations, conn25 needs to
block private destinations: a connector forwards to app destinations,
and the packet filter only ever sees the transit IP, never the
destination, so a private destination would let a client reach into the
connector's own network.

Updates https://github.com/tailscale/corp/issues/50118
Updates https://github.com/tailscale/corp/issues/46169

Change-Id: I700b0b5a270723f5a77907e35c52d729f88c44b6

Signed-off-by: Mazdak Nasab <[email protected]>
2026-10-30 08:55:05 -07:00
Mazdak Nasab 3ad81d1908 feature/conn25: return pool addresses when reserveAddresses fails
Addresses handed out by ippool.next() are marked in use, and the only path
back to the pool is the popExpired loop in reserveAddresses, which can
only see an addrs once it's in the client's assignments. So a reservation
that fails after taking an address leaks it from the pool
for the lifetime of the process.

The leak is the result of interaction between the magic and transit pools.
First an address from magic pool is picked. Then another address from
the transit pool is picked. If an error happens during picking an address
from transit pool, the address from the magic pool needs to be returned,
otherwise that address is leaked, as the address in not in the client
assignments and won't be released.

Fixes https://github.com/tailscale/tailscale/pull/22019

Change-Id: I3d98282762576cc57c135761859902a1d0a63c31
Signed-off-by: Mazdak Nasab <[email protected]>
2026-10-29 09:10:47 -07:00
Mazdak Nasab ce20d24a2b feature/conn25: add test coverage for DNS optional sections (#21782)
Additionals and authorities are two optional sections
in a DNS response. Conn25 treats DNS responses differently,
based on the response type. Adding test coverage for these
optional sections to assert the expected behavior.

Updates #cleanup

Change-Id: I778af5b368f53f6209fe2934aa98d9009c7fa395
Signed-off-by: Mazdak Nasab <[email protected]>
2026-10-26 16:44:37 -07:00
Mazdak Nasab aaaaa97189 feature/conn25: lock mu in lookupTransitIPsByConnKey
Lock mutex in lookupTransitIPsByConnKey right
before critical section, rather than in the caller.

Updates #cleanup

Change-Id: I8ee8950dce056e1945d4356d82f0bffbc6688624
Signed-off-by: Mazdak Nasab <[email protected]>
2026-10-13 13:26:43 -07:00
Mazdak Nasab 5b226c7d1d feature/conn25: write through email and voip related DNS responses
Write through email related DNS responses (MX and TXT),
and voip ones (SRV and NAPTR) as we try to not break them.

Fixes https://github.com/tailscale/corp/issues/39982

Change-Id: I920c4e4401f5a20bcdcad092e94fce8ffa0f12b5
Signed-off-by: Mazdak Nasab <[email protected]>
2026-10-09 14:00:58 -07:00
Mazdak Nasab 417d3c00b0 feature/conn25: lock mutex right before shared resources in client
Lock shared resources right before access
for clarity and preventing bugs.

Updates #cleanup

Change-Id: I1f905a3e4ec8b9ede4e8846e84e7bc50d78c67f0
Signed-off-by: Mazdak Nasab <[email protected]>
2026-10-08 09:26:56 -07:00
Mazdak Nasab 3420967a5f feature/conn25: lock mutext in client resendTransitIPMapping
Lock mutext in resendTransitIPMapping before accessing
the shared resource to prevent data race.

Fixes https://github.com/tailscale/corp/issues/49568

Change-Id: I35b9e99428361732c9a785020d7609d29a99877e
Signed-off-by: Mazdak Nasab <[email protected]>
2026-10-08 09:26:56 -07:00
Mazdak Nasab ecedffff5b feature/conn25: unmap addresses in TransitIPRequest
Unmap both transit and destination IPs in
TransitIPRequest to prevent unpredictable mappings.

Fixes https://github.com/tailscale/corp/issues/48781

Change-Id: I5aea2bf4e5ff5240c66f20c71cfa1ceba1722622
Signed-off-by: Mazdak Nasab <[email protected]>
2026-09-28 11:11:52 -07:00
Mazdak Nasab 8a119e1ec0 feature/conn25: skip AAAA records with IPv4-in-IPv6 addresses
Skip destination IPs from AAAA records that are
in ipv4-in-ipv6 format.

Fixes https://github.com/tailscale/corp/issues/48781

Change-Id: I5eeaa9c998c79250d2e6a943fa9f26ed7feb5ec6
Signed-off-by: Mazdak Nasab <[email protected]>
2026-09-28 11:11:52 -07:00
Mazdak Nasab 72fa821ed4 feature/conn25: clean up formatting in conn25*.go files
Improving formatting in conn25.go and conn25_test.go.

Updates #cleanup

Change-Id: Ic597e8e3e9284d0cc244de538abd6a563f783f32
Signed-off-by: Mazdak Nasab <[email protected]>
2026-09-25 09:46:53 -07:00
Mazdak Nasab 5af313f0aa feature/conn25: validate ConnectorTransitIPRequest contains TransitIPs in the pool
Validate ConnectorTransitIPRequest messages contain
TransitIPs in the configured pool.

Fixes https://github.com/tailscale/corp/issues/46600

Change-Id: I35dfcaa95a08b4347b1164df905cc59de47e4d96
Signed-off-by: Mazdak Nasab <[email protected]>
2026-09-25 09:46:53 -07:00
Mazdak Nasab b6c6c59dde feature/conn25: extend datapath test to verify full packets (#21422)
Extend datapath tests to not only assert headers but
also the full expected packets.

Fixes https://github.com/tailscale/corp/issues/48609

Change-Id: I0e9a1c2f61f2d63eafa430d8e6436908fe927cf4

Signed-off-by: Mazdak Nasab <[email protected]>
2026-09-23 13:52:21 -07:00
Mazdak Nasab 1edf391c9c feature/conn25: add ipv6 test coverage for datapath (#21362)
Add ipv6 test coverage for conn25 datapath by
extending existing tests.

Fixes tailscale/corp#42521

Change-Id: I4c6df35572d96cc80cae3685353522942e2a8546

Signed-off-by: Mazdak Nasab <[email protected]>
2026-09-22 09:36:50 -07:00