feature/conn25: enforce the grant's proto/port ranges in the filter

A conn25 app grant reaches the connector as a peer capability keyed
tailscale.com/cap/conn25/<app>, and peercap.Conn25Prefix already documents
that each value under such a key is a tailcfg.ProtoPortRange whose union is
what the peer may reach. Nothing implemented that: both consumers only called
PeerCapMap.HasCapability, so once a client had registered a transit IP, the
connector accepted any protocol on any port to any address in the transit
pool.

Decode the values when the client registers a transit IP, store them with the
mapping, and check every inbound packet against them in the connector's
ingress filter hook. A packet from WireGuard addressed to a transit IP always
falls through to IngressAllowHooks, since a transit IP is never in localNets,
so the check sees every packet rather than just TCP SYNs, and a denial gets
the usual RejectedDueToACLs TSMP reply.

Split packetFilterAllow in two along the way. linkLocalAllow keeps the old
pool-membership behaviour, which exists only to lift the filter's blanket drop
of link-local unicast destinations; ingressAllow makes the access decision. A
transit IP with no registered mapping still passes ingressAllow, so that the
datapath can drop it and answer with the TSMP that prompts the client to
re-register. Nothing is forwarded on its behalf.

ICMP carries no port, so any grant at all permits it, mirroring how the packet
filter treats ICMP for ordinary ACL rules. A capability present with no values
permits no traffic, while still resolving and registering so the
misconfiguration is diagnosable.

Updates tailscale/corp#49815

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
Mazdak NasabandClaude Opus 5 committed 2026-11-20 21:49:55 +00:00
1 parent 2e8f381c2e
commit b469f073e8
9 files changed
+517 -38

No files matched your search

+5
View File
@@ -170,9 +170,14 @@ func (c *connector) getActiveState() appctype.Conn25ConnectorState {
// Sort address mappings by transit IP.
for _, transitIP := range slices.SortedFunc(maps.Keys(transitToAddr), netip.Addr.Compare) {
addr := transitToAddr[transitIP]
var ports []string
for _, p := range addr.ports {
ports = append(ports, p.String())
}
apiAddr := appctype.Conn25ConnectorAddressState{
DestinationIP: addr.addr.String(),
TransitIP: transitIP.String(),
Ports: ports,
}
mak.Set(&appToAddrs, addr.app, append(appToAddrs[addr.app], apiAddr))
}
+11 -2
View File
@@ -10,7 +10,9 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"tailscale.com/tailcfg"
"tailscale.com/types/appctype"
"tailscale.com/types/ipproto"
"tailscale.com/types/logger"
"tailscale.com/util/dnsname"
"tailscale.com/util/must"
@@ -62,7 +64,12 @@ func TestGetActiveState(t *testing.T) {
netip.MustParseAddr("169.254.0.11"): {app: "app1", addr: netip.MustParseAddr("10.0.0.11")},
},
netip.MustParseAddr("11.0.0.1"): {
netip.MustParseAddr("169.254.0.5"): {app: "zapp", addr: netip.MustParseAddr("10.0.0.5")},
// A mapping whose grant restricts the protocols and ports it
// may carry; the others were granted nothing and carry none.
netip.MustParseAddr("169.254.0.5"): {app: "zapp", addr: netip.MustParseAddr("10.0.0.5"), ports: []tailcfg.ProtoPortRange{
{Proto: int(ipproto.TCP), Ports: tailcfg.PortRange{First: 443, Last: 443}},
{Ports: tailcfg.PortRangeAny},
}},
netip.MustParseAddr("169.254.0.6"): {app: "app1", addr: netip.MustParseAddr("10.0.0.6")},
},
}
@@ -134,7 +141,9 @@ func TestGetActiveState(t *testing.T) {
ClientIP: "11.0.0.1",
Apps: []appctype.Conn25ConnectorAppState{
{App: "app1", Addresses: []appctype.Conn25ConnectorAddressState{{DestinationIP: "10.0.0.6", TransitIP: "169.254.0.6"}}},
{App: "zapp", Addresses: []appctype.Conn25ConnectorAddressState{{DestinationIP: "10.0.0.5", TransitIP: "169.254.0.5"}}},
{App: "zapp", Addresses: []appctype.Conn25ConnectorAddressState{
{DestinationIP: "10.0.0.5", TransitIP: "169.254.0.5", Ports: []string{"tcp:443", "*"}},
}},
},
},
{
+29 -12
View File
@@ -251,16 +251,17 @@ func (e *extension) installHooks(dph *datapathHandler) error {
if !e.conn25.isConfigured() {
return false, ""
}
return e.conn25.connector.packetFilterAllow(p)
return e.conn25.connector.linkLocalAllow(p)
})
// Allow the connector to receive packets with Transit IP destinations
// that are not "local" to it, and that it does not advertise.
// that are not "local" to it, and that it does not advertise, as long as
// the peer's grant for the app permits their protocol and port.
e.host.Hooks().Filter.IngressAllowHooks.Add(func(p packet.Parsed) (bool, string) {
if !e.conn25.isConfigured() {
return false, ""
}
return e.conn25.connector.packetFilterAllow(p)
return e.conn25.connector.ingressAllow(p)
})
// Route DNS queries for app domains to the connector that serves
@@ -569,13 +570,14 @@ func (c *Conn25) reconfig(cfg *config) {
}
const (
dupeTransitIPMessage = "Duplicate transit address in ConnectorTransitIPRequest"
noMatchingPeerIPFamilyMessage = "No peer IP found with matching IP family"
addrFamilyMismatchMessage = "Transit and Destination addresses must have matching IP family"
unknownAppNameMessage = "The App name in the request does not match a configured App"
missingAppPermissionMessage = "You do not have permission to use this App"
transitIPNotInPoolMessage = "The transit address is not in a configured transit IP pool"
disallowedDestinationMessage = "Destination address is in a disallowed address range"
dupeTransitIPMessage = "Duplicate transit address in ConnectorTransitIPRequest"
noMatchingPeerIPFamilyMessage = "No peer IP found with matching IP family"
addrFamilyMismatchMessage = "Transit and Destination addresses must have matching IP family"
unknownAppNameMessage = "The App name in the request does not match a configured App"
missingAppPermissionMessage = "You do not have permission to use this App"
unparseableAppPermissionMessage = "The permission granting access to this App could not be parsed"
transitIPNotInPoolMessage = "The transit address is not in a configured transit IP pool"
disallowedDestinationMessage = "Destination address is in a disallowed address range"
)
// handleConnectorTransitIPRequest creates a ConnectorTransitIPResponse in response
@@ -629,13 +631,28 @@ func (c *Conn25) handleConnectorTransitIPRequest(n tailcfg.NodeView, peerCaps ta
continue
}
if !peerCaps.HasCapability(peercap.Conn25Prefix.ToAttribute(each.App)) {
// The grant that gives the peer access to the app also carries the
// protocols and ports it may use, which get stored with the mapping
// and enforced on every packet by [connector.ingressAllow].
ports, hasCap, err := appProtoPortRanges(peerCaps, each.App)
if !hasCap {
resp.TransitIPs = append(resp.TransitIPs, TransitIPResponse{
Code: MissingAppPermission,
Message: missingAppPermissionMessage,
})
continue
}
if err != nil {
// Fail closed on the whole grant rather than registering a
// mapping under the values we did manage to understand.
resp.TransitIPs = append(resp.TransitIPs, TransitIPResponse{
Code: MissingAppPermission,
Message: unparseableAppPermissionMessage,
})
c.logf("[Unexpected] peer holds an unparseable conn25 grant: node: %s, app: %q, err: %v",
n.StableID(), each.App, err)
continue
}
if _, ok := cfg.appsByName[each.App]; !ok {
resp.TransitIPs = append(resp.TransitIPs, TransitIPResponse{
@@ -646,7 +663,7 @@ func (c *Conn25) handleConnectorTransitIPRequest(n tailcfg.NodeView, peerCaps ta
n.StableID(), each.App)
continue
}
tipresp := c.connector.handleTransitIPRequest(n, peerIPv4, peerIPv6, each)
tipresp := c.connector.handleTransitIPRequest(n, peerIPv4, peerIPv6, each, ports)
seen[each.TransitIP] = true
resp.TransitIPs = append(resp.TransitIPs, tipresp)
}
+4 -2
View File
@@ -4043,8 +4043,10 @@ func TestHandleHookReplyToDNSQueries(t *testing.T) {
exampleAppPeerCap := tailcfg.PeerCapMap{
peercap.Conn25Prefix.ToAttribute("example"): []tailcfg.RawMessage{`"*"`},
}
// This is to verify that it's not picky about which ports are allowed for
// the app.
// The presence of the capability is what grants DNS resolution for the
// app; its values restrict traffic, not queries. So even a grant that
// permits no traffic at all still resolves. See [connector.ingressAllow]
// for where the values are enforced.
exampleAppPeerCapRestrictive := tailcfg.PeerCapMap{
peercap.Conn25Prefix.ToAttribute("example"): []tailcfg.RawMessage{},
}
+116 -17
View File
@@ -7,23 +7,31 @@ import (
"container/list"
"context"
"net/netip"
"slices"
"sync"
"time"
"tailscale.com/net/packet"
"tailscale.com/net/routelimits"
"tailscale.com/tailcfg"
"tailscale.com/tailcfg/peercap"
"tailscale.com/tstime"
"tailscale.com/types/ipproto"
"tailscale.com/types/logger"
)
type appAddr struct {
app string
addr netip.Addr
app string
addr netip.Addr
// ports are the protocol and port combinations the peer was permitted to
// reach through app at the moment it registered this mapping. See
// [appProtoPortRanges] for where they come from, and
// [connector.ingressAllow] for how they are enforced.
ports []tailcfg.ProtoPortRange
expiryEntry *list.Element
}
func (c *connector) handleTransitIPRequest(n tailcfg.NodeView, peerV4 netip.Addr, peerV6 netip.Addr, tipr TransitIPRequest) TransitIPResponse {
func (c *connector) handleTransitIPRequest(n tailcfg.NodeView, peerV4 netip.Addr, peerV6 netip.Addr, tipr TransitIPRequest, ports []tailcfg.ProtoPortRange) TransitIPResponse {
if tipr.TransitIP.Is4() != tipr.DestinationIP.Is4() {
c.logf("[Unexpected] peer attempt to map a transit IP to dest IP did not have matching families: node: %s, tIPv4: %v dIPv4: %v",
n.StableID(), tipr.TransitIP.Is4(), tipr.DestinationIP.Is4())
@@ -77,10 +85,65 @@ func (c *connector) handleTransitIPRequest(n tailcfg.NodeView, peerV4 netip.Addr
transitIP: tipr.TransitIP,
createdAt: c.clock.Now(),
})
peerMap[tipr.TransitIP] = appAddr{addr: tipr.DestinationIP, app: tipr.App, expiryEntry: elem}
peerMap[tipr.TransitIP] = appAddr{addr: tipr.DestinationIP, app: tipr.App, ports: ports, expiryEntry: elem}
return TransitIPResponse{}
}
// appProtoPortRanges returns the protocol and port combinations peerCaps
// permits the peer to reach through app, as granted by the "ip" field of the
// grant that produced the peer's [peercap.Conn25Prefix] capability for it.
//
// It reports hasCap false if the peer holds no capability for app at all. A
// capability that is present but carries no values permits no traffic, and
// comes back as an empty slice with hasCap true: the app stays resolvable and
// registrable, which keeps the misconfiguration diagnosable, but
// [connector.ingressAllow] lets nothing through.
func appProtoPortRanges(peerCaps tailcfg.PeerCapMap, app string) (ranges []tailcfg.ProtoPortRange, hasCap bool, err error) {
cap := peercap.Conn25Prefix.ToAttribute(app)
if !peerCaps.HasCapability(cap) {
return nil, false, nil
}
ranges, err = tailcfg.UnmarshalCapJSON[tailcfg.ProtoPortRange](peerCaps, cap)
if err != nil {
return nil, true, err
}
return ranges, true, nil
}
// protolessGrantProtos is the set of protocols a [tailcfg.ProtoPortRange] with
// no protocol (e.g. "*" or "80") covers. It is the same set the packet filter
// uses for a [tailcfg.FilterRule] that names no protocol, and the set
// [tailcfg.ProtoPortRange] documents for a zero Proto.
var protolessGrantProtos = []ipproto.Proto{ipproto.TCP, ipproto.UDP, ipproto.ICMPv4, ipproto.ICMPv6}
// protoPortRangesAllow reports whether ranges, the protocol and port
// combinations a peer was granted for an app, permit a packet of protocol
// proto addressed to port.
//
// ICMP carries no port, so it is permitted whenever the peer was granted
// anything at all for the app. That mirrors the packet filter, which allows
// ICMP to any destination the source may reach on any port by any protocol
// (see matchIPsOnly in [tailscale.com/wgengine/filter]), so a "tcp:443" grant
// does not break ping.
func protoPortRangesAllow(ranges []tailcfg.ProtoPortRange, proto ipproto.Proto, port uint16) bool {
if proto == ipproto.ICMPv4 || proto == ipproto.ICMPv6 {
return len(ranges) > 0
}
for _, r := range ranges {
if r.Proto == 0 {
if !slices.Contains(protolessGrantProtos, proto) {
continue
}
} else if ipproto.Proto(r.Proto) != proto {
continue
}
if port >= r.Ports.First && port <= r.Ports.Last {
return true
}
}
return false
}
// connectorTransitIPExpiry is the minimum length of time a peer+transitIP -> dstIP mapping will be held in the connector.
// The longer this time is the larger the map will be in memory.
// The shorter this time is the more often a client will try to use a mapping, find it doesn't exist anymore and have to re-register.
@@ -149,28 +212,64 @@ func (c *connector) destinationInPools(dst netip.Addr) bool {
return (magic != nil && magic.Contains(dst)) || (transit != nil && transit.Contains(dst))
}
// packetFilterAllow returns true if the provided packet has a Src that is in
// the configured transit IP range for this connector, false otherwise.
func (c *connector) packetFilterAllow(p packet.Parsed) (bool, string) {
c.mu.Lock()
defer c.mu.Unlock()
ipSets := c.getIPSets()
if ipSets.v4Transit != nil && ipSets.v4Transit.Contains(p.Dst.Addr()) {
return true, packetFilterAllowReason
}
if ipSets.v6Transit != nil && ipSets.v6Transit.Contains(p.Dst.Addr()) {
// linkLocalAllow reports whether p, whose destination is a link-local unicast
// address, should be exempt from the filter's blanket drop of such packets.
// conn25 allocates transit IPs out of link-local space, so a packet addressed
// to one has to get past that check; whether the peer may actually reach it is
// decided by [connector.ingressAllow].
func (c *connector) linkLocalAllow(p packet.Parsed) (bool, string) {
if c.transitIPInPool(p.Dst.Addr()) {
return true, packetFilterAllowReason
}
return false, ""
}
// ingressAllow reports whether p, a packet arriving from a peer, may be
// accepted by this connector. It may if p is addressed to an address in a
// configured transit IP pool and either the peer has no mapping registered for
// that address, or the grant it registered the mapping under permits p's
// protocol and destination port.
//
// Packets for an unregistered mapping are accepted here so that the datapath
// can drop them itself and answer with the TSMP message that prompts the
// client to re-register (see [datapathHandler.HandlePacketFromWireGuard]).
// Nothing is forwarded on their behalf.
//
// The permitted protocols and ports are the ones the peer held when it
// registered the mapping. A grant that is tightened afterwards takes effect
// when the mapping is re-registered, at the latest [connectorTransitIPExpiry]
// after it was last registered.
func (c *connector) ingressAllow(p packet.Parsed) (bool, string) {
if !c.transitIPInPool(p.Dst.Addr()) {
return false, ""
}
c.mu.Lock()
defer c.mu.Unlock()
aa, ok := c.lookupAppAddrBySrcIPAndTransitIP(p.Src.Addr(), p.Dst.Addr())
if !ok {
return true, packetFilterAllowReason
}
if !protoPortRangesAllow(aa.ports, p.IPProto, p.Dst.Port()) {
return false, ""
}
return true, packetFilterAllowReason
}
func (c *connector) lookupAddrBySrcIPAndTransitIP(srcIP, transitIP netip.Addr) (netip.Addr, bool) {
m, ok := c.transitIPs[srcIP]
if !ok || m == nil {
v, ok := c.lookupAppAddrBySrcIPAndTransitIP(srcIP, transitIP)
if !ok {
return netip.Addr{}, false
}
return v.addr, true
}
func (c *connector) lookupAppAddrBySrcIPAndTransitIP(srcIP, transitIP netip.Addr) (appAddr, bool) {
m, ok := c.transitIPs[srcIP]
if !ok || m == nil {
return appAddr{}, false
}
v, ok := m[transitIP]
return v.addr, ok
return v, ok
}
// expireTransitIPs expires entries in the connector's transitIPs map that are
+339 -3
View File
@@ -9,12 +9,14 @@ import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"go4.org/netipx"
"tailscale.com/net/packet"
"tailscale.com/tailcfg"
"tailscale.com/tailcfg/peercap"
"tailscale.com/tstest"
"tailscale.com/types/appctype"
"tailscale.com/types/ipproto"
"tailscale.com/types/logger"
"tailscale.com/util/must"
)
@@ -279,7 +281,7 @@ func TestConnectorDisallowedDestinations(t *testing.T) {
}
}
func TestConnectorPacketFilterAllow(t *testing.T) {
func TestConnectorLinkLocalAllow(t *testing.T) {
src := netip.MustParseAddr("100.64.0.1")
knownTip := netip.MustParseAddr("192.0.2.1")
unknownTip := netip.MustParseAddr("100.64.0.42")
@@ -296,17 +298,351 @@ func TestConnectorPacketFilterAllow(t *testing.T) {
},
})
if allow, _ := c.connector.packetFilterAllow(packet.Parsed{
if allow, _ := c.connector.linkLocalAllow(packet.Parsed{
Src: netip.AddrPortFrom(src, 1234),
Dst: netip.AddrPortFrom(knownTip, 1234),
}); !allow {
t.Fatal("knownTip: should have been allowed")
}
if allow, _ := c.connector.packetFilterAllow(packet.Parsed{
if allow, _ := c.connector.linkLocalAllow(packet.Parsed{
Src: netip.AddrPortFrom(src, 1234),
Dst: netip.AddrPortFrom(unknownTip, 1234),
}); allow {
t.Fatal("unknownTip: should not have been allowed")
}
}
// TestConnectorIngressAllow checks that [connector.ingressAllow] holds peers
// to the protocols and ports their grant for the app permits, that a transit
// IP with no registered mapping still gets through so the datapath can answer
// with the re-registration TSMP, and that addresses outside the transit pools
// are refused outright.
func TestConnectorIngressAllow(t *testing.T) {
const appName = "app"
peerIP := netip.MustParseAddr("100.64.0.1")
peerNode := (&tailcfg.Node{ID: 1, Addresses: []netip.Prefix{netip.PrefixFrom(peerIP, 32)}}).View()
otherPeerIP := netip.MustParseAddr("100.64.0.2")
transitIP := netip.MustParseAddr("192.0.2.1")
unmappedTransitIP := netip.MustParseAddr("192.0.2.2")
outsidePoolIP := netip.MustParseAddr("100.64.0.42")
type probe struct {
proto ipproto.Proto
port uint16
dst netip.Addr // defaults to transitIP
src netip.Addr // defaults to peerIP
want bool
}
tests := []struct {
name string
grant []tailcfg.RawMessage
// noRegister skips registering the transit IP mapping, leaving the
// connector with nothing to look up for it.
noRegister bool
probes []probe
}{
{
name: "wildcard",
grant: []tailcfg.RawMessage{`"*"`},
probes: []probe{
{proto: ipproto.TCP, port: 443, want: true},
{proto: ipproto.TCP, port: 80, want: true},
{proto: ipproto.UDP, port: 53, want: true},
{proto: ipproto.ICMPv4, want: true},
},
},
{
name: "tcp-443",
grant: []tailcfg.RawMessage{`"tcp:443"`},
probes: []probe{
{proto: ipproto.TCP, port: 443, want: true},
{proto: ipproto.TCP, port: 80, want: false},
{proto: ipproto.TCP, port: 442, want: false},
{proto: ipproto.TCP, port: 444, want: false},
{proto: ipproto.UDP, port: 443, want: false},
// ICMP has no port, so any grant at all permits it.
{proto: ipproto.ICMPv4, want: true},
},
},
{
name: "protoless-port",
grant: []tailcfg.RawMessage{`"80"`},
probes: []probe{
{proto: ipproto.TCP, port: 80, want: true},
{proto: ipproto.UDP, port: 80, want: true},
{proto: ipproto.TCP, port: 443, want: false},
// A protocol-less range covers TCP+UDP+ICMP, not SCTP.
{proto: ipproto.SCTP, port: 80, want: false},
},
},
{
name: "port-range",
grant: []tailcfg.RawMessage{`"tcp:8000-8100"`},
probes: []probe{
{proto: ipproto.TCP, port: 7999, want: false},
{proto: ipproto.TCP, port: 8000, want: true},
{proto: ipproto.TCP, port: 8050, want: true},
{proto: ipproto.TCP, port: 8100, want: true},
{proto: ipproto.TCP, port: 8101, want: false},
},
},
{
name: "union-of-values",
grant: []tailcfg.RawMessage{`"tcp:443"`, `"udp:53"`},
probes: []probe{
{proto: ipproto.TCP, port: 443, want: true},
{proto: ipproto.UDP, port: 53, want: true},
{proto: ipproto.UDP, port: 443, want: false},
{proto: ipproto.TCP, port: 53, want: false},
},
},
{
name: "empty-grant-permits-nothing",
grant: []tailcfg.RawMessage{},
probes: []probe{
{proto: ipproto.TCP, port: 443, want: false},
{proto: ipproto.UDP, port: 53, want: false},
{proto: ipproto.ICMPv4, want: false},
},
},
{
name: "outside-transit-pool",
grant: []tailcfg.RawMessage{`"*"`},
probes: []probe{
{proto: ipproto.TCP, port: 443, dst: outsidePoolIP, want: false},
},
},
{
// Without a mapping the connector has no grant to judge the
// packet by, so it passes the filter and the datapath drops it
// and tells the client to re-register.
name: "no-mapping-registered",
grant: []tailcfg.RawMessage{`"tcp:443"`},
noRegister: true,
probes: []probe{
{proto: ipproto.TCP, port: 443, dst: unmappedTransitIP, want: true},
{proto: ipproto.TCP, port: 80, dst: unmappedTransitIP, want: true},
},
},
{
// A mapping registered by one peer says nothing about another,
// which lands in the no-mapping case above.
name: "different-peer",
grant: []tailcfg.RawMessage{`"tcp:443"`},
probes: []probe{
{proto: ipproto.TCP, port: 80, src: otherPeerIP, want: true},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newConn25(t.Logf)
c.reconfig(&config{
isConfigured: true,
appsByName: map[string]appctype.AppConnectorAttr{appName: {}},
ipSets: ipSets{v4Transit: mustIPSetFromPrefix("192.0.2.0/24")},
})
if !tt.noRegister {
peerCap := tailcfg.PeerCapMap{
peercap.Conn25Prefix.ToAttribute(appName): tt.grant,
}
resp := c.handleConnectorTransitIPRequest(peerNode, peerCap, ConnectorTransitIPRequest{
TransitIPs: []TransitIPRequest{{
TransitIP: transitIP,
DestinationIP: netip.MustParseAddr("203.0.113.1"),
App: appName,
}},
})
if len(resp.TransitIPs) != 1 || resp.TransitIPs[0].Code != OK {
t.Fatalf("registration failed: %+v", resp.TransitIPs)
}
}
for _, p := range tt.probes {
src, dst := p.src, p.dst
if !src.IsValid() {
src = peerIP
}
if !dst.IsValid() {
dst = transitIP
}
got, _ := c.connector.ingressAllow(packet.Parsed{
IPProto: p.proto,
Src: netip.AddrPortFrom(src, 12345),
Dst: netip.AddrPortFrom(dst, p.port),
})
if got != p.want {
t.Errorf("ingressAllow(%v %v -> %v:%d) = %v, want %v",
p.proto, src, dst, p.port, got, p.want)
}
}
})
}
}
// TestConnectorGrantRegistration checks that the grant's values are decoded
// and stored with the mapping, and that a grant that cannot be decoded is
// refused outright rather than registered under a partial interpretation.
func TestConnectorGrantRegistration(t *testing.T) {
const appName = "app"
peerIP := netip.MustParseAddr("100.64.0.1")
peerNode := (&tailcfg.Node{ID: 1, Addresses: []netip.Prefix{netip.PrefixFrom(peerIP, 32)}}).View()
transitIP := netip.MustParseAddr("192.0.2.1")
tests := []struct {
name string
grant []tailcfg.RawMessage
omitCap bool
wantCode TransitIPResponseCode
wantMsg string
wantPorts []tailcfg.ProtoPortRange
}{
{
name: "proto-and-port",
grant: []tailcfg.RawMessage{`"tcp:443"`},
wantCode: OK,
wantPorts: []tailcfg.ProtoPortRange{{Proto: int(ipproto.TCP), Ports: tailcfg.PortRange{First: 443, Last: 443}}},
},
{
name: "wildcard",
grant: []tailcfg.RawMessage{`"*"`},
wantCode: OK,
wantPorts: []tailcfg.ProtoPortRange{{Ports: tailcfg.PortRangeAny}},
},
{
name: "empty",
grant: []tailcfg.RawMessage{},
wantCode: OK,
wantPorts: []tailcfg.ProtoPortRange{},
},
{
name: "unparseable",
grant: []tailcfg.RawMessage{`"not-a-port"`},
wantCode: MissingAppPermission,
wantMsg: unparseableAppPermissionMessage,
},
{
name: "no-capability",
omitCap: true,
wantCode: MissingAppPermission,
wantMsg: missingAppPermissionMessage,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newConn25(t.Logf)
c.reconfig(&config{
isConfigured: true,
appsByName: map[string]appctype.AppConnectorAttr{appName: {}},
ipSets: ipSets{v4Transit: mustIPSetFromPrefix("192.0.2.0/24")},
})
peerCap := tailcfg.PeerCapMap{}
if !tt.omitCap {
peerCap[peercap.Conn25Prefix.ToAttribute(appName)] = tt.grant
}
resp := c.handleConnectorTransitIPRequest(peerNode, peerCap, ConnectorTransitIPRequest{
TransitIPs: []TransitIPRequest{{
TransitIP: transitIP,
DestinationIP: netip.MustParseAddr("203.0.113.1"),
App: appName,
}},
})
if len(resp.TransitIPs) != 1 {
t.Fatalf("got %d responses, want 1", len(resp.TransitIPs))
}
if got := resp.TransitIPs[0].Code; got != tt.wantCode {
t.Errorf("code = %d, want %d", got, tt.wantCode)
}
if got := resp.TransitIPs[0].Message; got != tt.wantMsg {
t.Errorf("message = %q, want %q", got, tt.wantMsg)
}
c.connector.mu.Lock()
defer c.connector.mu.Unlock()
aa, ok := c.connector.lookupAppAddrBySrcIPAndTransitIP(peerIP, transitIP)
if tt.wantCode != OK {
// A refused request must not leave a mapping behind.
if ok {
t.Errorf("mapping was registered for a refused request: %+v", aa)
}
return
}
if !ok {
t.Fatal("no mapping registered")
}
if diff := cmp.Diff(tt.wantPorts, aa.ports); diff != "" {
t.Errorf("stored ports mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestProtoPortRangesAllow(t *testing.T) {
ppr := func(s string) tailcfg.ProtoPortRange {
var p tailcfg.ProtoPortRange
if err := p.UnmarshalText([]byte(s)); err != nil {
t.Fatalf("parsing %q: %v", s, err)
}
return p
}
tests := []struct {
name string
ranges []tailcfg.ProtoPortRange
proto ipproto.Proto
port uint16
want bool
}{
{name: "nil-denies-tcp", proto: ipproto.TCP, port: 80},
{name: "nil-denies-icmp", proto: ipproto.ICMPv4},
{name: "empty-denies-icmp", ranges: []tailcfg.ProtoPortRange{}, proto: ipproto.ICMPv4},
{name: "wildcard-tcp", ranges: []tailcfg.ProtoPortRange{ppr("*")}, proto: ipproto.TCP, port: 1, want: true},
{name: "wildcard-udp", ranges: []tailcfg.ProtoPortRange{ppr("*")}, proto: ipproto.UDP, port: 65535, want: true},
{name: "wildcard-icmp6", ranges: []tailcfg.ProtoPortRange{ppr("*")}, proto: ipproto.ICMPv6, want: true},
// "*" is protocol-less, which means TCP+UDP+ICMP, not every protocol.
{name: "wildcard-not-sctp", ranges: []tailcfg.ProtoPortRange{ppr("*")}, proto: ipproto.SCTP, port: 80},
{name: "tcp-match", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443")}, proto: ipproto.TCP, port: 443, want: true},
{name: "tcp-wrong-port", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443")}, proto: ipproto.TCP, port: 80},
{name: "tcp-wrong-proto", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443")}, proto: ipproto.UDP, port: 443},
// ICMP is permitted by any grant, even a TCP-only one.
{name: "tcp-grant-allows-icmp", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443")}, proto: ipproto.ICMPv4, want: true},
{name: "tcp-any-port", ranges: []tailcfg.ProtoPortRange{ppr("tcp:*")}, proto: ipproto.TCP, port: 9999, want: true},
{name: "tcp-any-port-not-udp", ranges: []tailcfg.ProtoPortRange{ppr("tcp:*")}, proto: ipproto.UDP, port: 9999},
{name: "range-low", ranges: []tailcfg.ProtoPortRange{ppr("tcp:80-90")}, proto: ipproto.TCP, port: 79},
{name: "range-first", ranges: []tailcfg.ProtoPortRange{ppr("tcp:80-90")}, proto: ipproto.TCP, port: 80, want: true},
{name: "range-last", ranges: []tailcfg.ProtoPortRange{ppr("tcp:80-90")}, proto: ipproto.TCP, port: 90, want: true},
{name: "range-high", ranges: []tailcfg.ProtoPortRange{ppr("tcp:80-90")}, proto: ipproto.TCP, port: 91},
{name: "protoless-tcp", ranges: []tailcfg.ProtoPortRange{ppr("80")}, proto: ipproto.TCP, port: 80, want: true},
{name: "protoless-udp", ranges: []tailcfg.ProtoPortRange{ppr("80")}, proto: ipproto.UDP, port: 80, want: true},
{name: "protoless-wrong-port", ranges: []tailcfg.ProtoPortRange{ppr("80")}, proto: ipproto.TCP, port: 81},
{name: "numeric-proto", ranges: []tailcfg.ProtoPortRange{ppr("132:9")}, proto: ipproto.SCTP, port: 9, want: true},
{name: "union-first", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443"), ppr("udp:53")}, proto: ipproto.TCP, port: 443, want: true},
{name: "union-second", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443"), ppr("udp:53")}, proto: ipproto.UDP, port: 53, want: true},
{name: "union-neither", ranges: []tailcfg.ProtoPortRange{ppr("tcp:443"), ppr("udp:53")}, proto: ipproto.UDP, port: 443},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := protoPortRangesAllow(tt.ranges, tt.proto, tt.port); got != tt.want {
t.Errorf("protoPortRangesAllow(%v, %v, %d) = %v, want %v",
tt.ranges, tt.proto, tt.port, got, tt.want)
}
})
}
}
+7 -1
View File
@@ -74,6 +74,12 @@ const (
// an app named "example".
// Each value under such a key is a [tailscale.com/tailcfg.ProtoPortRange].
// If multiple values are present, the union of all protocol/port
// combinations described shall be permitted.
// combinations described shall be permitted. The connector enforces them
// in its packet filter, on every packet addressed to a transit IP the
// peer registered for the app.
//
// A key present with no values identifies the app as granted — the peer
// may still resolve its domains and register transit IPs for it, so that
// the configuration is diagnosable — but permits no traffic.
Conn25Prefix Prefix = "tailscale.com/cap/conn25/"
)
+2 -1
View File
@@ -201,7 +201,8 @@ type CapabilityVersion int
// - 150: 2026-10-13: Client handles app connectors with "mode": "magicip" as conn25.
// - 151: 2026-11-13: Client understands [nodecap.LogUploadAuth] for authenticated log uploads
// - 152: 2026-11-17: Client supports hybrid post-quantum handshakes
const CurrentCapabilityVersion CapabilityVersion = 152
// - 153: 2026-11-20: conn25 connector enforces the proto/port ranges in [peercap.Conn25Prefix] grant values
const CurrentCapabilityVersion CapabilityVersion = 153
// ID is an integer ID for a user, node, or login allocated by the
// control plane.
+4
View File
@@ -199,4 +199,8 @@ type Conn25ConnectorAppState struct {
type Conn25ConnectorAddressState struct {
DestinationIP string `json:"destinationIP,omitempty"`
TransitIP string `json:"transitIP,omitempty"`
// Ports are the protocol and port combinations the peer's grant for the
// app permits through this mapping, in [tailcfg.ProtoPortRange] form
// (e.g. "tcp:443", "*"). An empty list means no traffic is permitted.
Ports []string `json:"ports,omitempty"`
}