mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-06 23:36:34 +09:00
feature/conn25: enforce the connector's grants once per flow, in the datapath
The connector enforced a peer's app grant on every packet in connector.ingressAllow runs from the packet filter's IngressAllowHooks. the hook took connector.mu, looked the mapping up and matched the grant's ranges, for every packet of every session. This introduced a performance concern. Move the check to where the flow is installed in the datapath. realIPForTransitIPConnection, which the datapath already calls on a flow miss and which already holds the mapping, now returns a verdict: unmapped, denied, or allowed with the real IP. The datapath drops what the grant denies. connector.ingressAllow keeps only the two questions the datapath cannot answer later: a) is the destination in a transit pool, and b) can the datapath map this protocol. Admitting a packet in connector.ingressAllow is no longer a decision that the packet reaches the app. Updates https://github.com/tailscale/corp/issues/49815 Change-Id: If2ed3092b44a0243b9e0b4a5ba1b97fa2df66eb7 Signed-off-by: Mazdak Nasab <mazdak.nasab@gmail.com>
This commit is contained in:
1 parent
ba4feb91db
commit
2caf855ca0
8 files changed
+527
-238
No files matched your search
@@ -257,8 +257,9 @@ func (e *extension) installHooks(dph *datapathHandler) error {
|
||||
|
||||
// Allow the connector to receive packets with Transit IP destinations
|
||||
// that are not "local" to it, and that it does not advertise, as long as
|
||||
// the datapath can do something with them and the peer's grant for the
|
||||
// app permits their protocol and port.
|
||||
// the datapath can do something with them. Admitting a packet here is not
|
||||
// a decision that it reaches the app: the peer's grant for the app is
|
||||
// enforced once per flow, in [connector.realIPForTransitIPConnection].
|
||||
e.host.Hooks().Filter.IngressAllowHooks.Add(func(p packet.Parsed) (bool, string) {
|
||||
if !e.conn25.isConfigured() {
|
||||
return false, ""
|
||||
@@ -338,10 +339,21 @@ func (c *Conn25) ClientPrepareNewFlowForMagicIP(m netip.Addr) (ClientFlowRef, bo
|
||||
}
|
||||
|
||||
// ConnectorRealIPForTransitIPConnection implements [Conn25Datapath].
|
||||
func (c *Conn25) ConnectorRealIPForTransitIPConnection(src, transit netip.Addr) (netip.Addr, error) {
|
||||
if addr, ok := c.connector.realIPForTransitIPConnection(src, transit); ok {
|
||||
func (c *Conn25) ConnectorRealIPForTransitIPConnection(p *packet.Parsed) (netip.Addr, error) {
|
||||
addr, verdict := c.connector.realIPForTransitIPConnection(p)
|
||||
switch verdict {
|
||||
case transitIPAllowed:
|
||||
return addr, nil
|
||||
case transitIPDenied:
|
||||
return netip.Addr{}, ErrGrantDenied
|
||||
case transitIPUnmapped:
|
||||
// Fall through to the pool check below, which decides whether to ask
|
||||
// the peer to re-register.
|
||||
default:
|
||||
// Fail closed: a verdict we don't know about is not an admission.
|
||||
return netip.Addr{}, ErrGrantDenied
|
||||
}
|
||||
transit := p.Dst.Addr()
|
||||
cfg, ok := c.getConfig()
|
||||
if !ok {
|
||||
return netip.Addr{}, nil
|
||||
@@ -645,7 +657,7 @@ func (c *Conn25) handleConnectorTransitIPRequest(n tailcfg.NodeView, peerCaps ta
|
||||
|
||||
// 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].
|
||||
// and enforced once per flow by [connector.realIPForTransitIPConnection].
|
||||
ports, hasCap, err := appProtoPortRanges(peerCaps, each.App)
|
||||
if !hasCap {
|
||||
resp.TransitIPs = append(resp.TransitIPs, TransitIPResponse{
|
||||
|
||||
@@ -31,6 +31,7 @@ import (
|
||||
"tailscale.com/net/dns"
|
||||
"tailscale.com/net/dns/resolver"
|
||||
"tailscale.com/net/netmon"
|
||||
"tailscale.com/net/packet"
|
||||
"tailscale.com/net/routecheck"
|
||||
"tailscale.com/net/tsdial"
|
||||
"tailscale.com/net/tstun"
|
||||
@@ -41,6 +42,7 @@ import (
|
||||
"tailscale.com/tstest"
|
||||
"tailscale.com/types/appctype"
|
||||
"tailscale.com/types/dnstype"
|
||||
"tailscale.com/types/ipproto"
|
||||
"tailscale.com/types/key"
|
||||
"tailscale.com/types/logger"
|
||||
"tailscale.com/types/opt"
|
||||
@@ -3458,10 +3460,14 @@ func TestConnectorRealIPForTransitIPConnection(t *testing.T) {
|
||||
unmappedTip := netip.MustParseAddr("100.64.0.42")
|
||||
nonTip := netip.MustParseAddr("100.0.0.3")
|
||||
mappedMip := netip.MustParseAddr("100.64.0.1")
|
||||
// The mapping is registered under a grant of tcp:443, so probes use that
|
||||
// port unless they are checking what happens outside the grant.
|
||||
const grantedPort = 443
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
src netip.Addr
|
||||
tip netip.Addr
|
||||
port uint16 // defaults to grantedPort
|
||||
wantMip netip.Addr
|
||||
wantErr error
|
||||
}{
|
||||
@@ -3500,14 +3506,35 @@ func TestConnectorRealIPForTransitIPConnection(t *testing.T) {
|
||||
wantMip: mappedMip,
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "mapped-src-and-transit-ip-ungranted-port",
|
||||
src: mappedSrc,
|
||||
tip: mappedTip,
|
||||
port: 80,
|
||||
wantMip: netip.Addr{},
|
||||
wantErr: ErrGrantDenied,
|
||||
},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := newConn25(t.Logf)
|
||||
c.reconfig(cfg)
|
||||
c.connector.transitIPs = map[netip.Addr]map[netip.Addr]appAddr{}
|
||||
c.connector.transitIPs[mappedSrc] = map[netip.Addr]appAddr{}
|
||||
c.connector.transitIPs[mappedSrc][mappedTip] = appAddr{addr: mappedMip}
|
||||
mip, err := c.ConnectorRealIPForTransitIPConnection(tt.src, tt.tip)
|
||||
c.connector.transitIPs[mappedSrc][mappedTip] = appAddr{
|
||||
addr: mappedMip,
|
||||
ports: mustProtoPortRanges(t, "tcp:443"),
|
||||
}
|
||||
port := tt.port
|
||||
if port == 0 {
|
||||
port = grantedPort
|
||||
}
|
||||
mip, err := c.ConnectorRealIPForTransitIPConnection(&packet.Parsed{
|
||||
IPVersion: 4,
|
||||
IPProto: ipproto.TCP,
|
||||
TCPFlags: packet.TCPSyn,
|
||||
Src: netip.AddrPortFrom(tt.src, 12345),
|
||||
Dst: netip.AddrPortFrom(tt.tip, port),
|
||||
})
|
||||
if mip != tt.wantMip {
|
||||
t.Fatalf("checking magic ip: want %v, got %v", tt.wantMip, mip)
|
||||
}
|
||||
@@ -3911,7 +3938,7 @@ func TestHandleHookReplyToDNSQueries(t *testing.T) {
|
||||
}
|
||||
// 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]
|
||||
// permits no traffic at all still resolves. See [connector.realIPForTransitIPConnection]
|
||||
// for where the values are enforced.
|
||||
exampleAppPeerCapRestrictive := tailcfg.PeerCapMap{
|
||||
peercap.Conn25Prefix.ToAttribute("example"): []tailcfg.RawMessage{},
|
||||
@@ -5053,9 +5080,15 @@ func TestHandleConnectorTransitIPRequestVerifiesToken(t *testing.T) {
|
||||
t.Fatalf("code = %d, want %d", got, InvalidDestinationIPToken)
|
||||
}
|
||||
|
||||
gotDst, ok := c.connector.realIPForTransitIPConnection(peerV4, transitIP)
|
||||
if ok {
|
||||
t.Errorf("rejected request left a mapping: %v", gotDst)
|
||||
gotDst, verdict := c.connector.realIPForTransitIPConnection(&packet.Parsed{
|
||||
IPVersion: 4,
|
||||
IPProto: ipproto.TCP,
|
||||
TCPFlags: packet.TCPSyn,
|
||||
Src: netip.AddrPortFrom(peerV4, 12345),
|
||||
Dst: netip.AddrPortFrom(transitIP, 443),
|
||||
})
|
||||
if verdict != transitIPUnmapped {
|
||||
t.Errorf("rejected request left a mapping: verdict %v, dst %v", verdict, gotDst)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
+47
-21
@@ -7,6 +7,7 @@ import (
|
||||
"container/list"
|
||||
"context"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -121,13 +122,50 @@ type transitIPExpiryEntry struct {
|
||||
createdAt time.Time
|
||||
}
|
||||
|
||||
// transitIPVerdict is what [connector.realIPForTransitIPConnection] made of a
|
||||
// packet addressed to a transit IP.
|
||||
type transitIPVerdict int
|
||||
|
||||
const (
|
||||
// transitIPUnmapped means the peer has registered no mapping for the
|
||||
// transit IP, so the connector has nothing to forward the packet to.
|
||||
transitIPUnmapped transitIPVerdict = iota
|
||||
// transitIPDenied means the peer has a mapping, but the grant it holds for
|
||||
// the app does not permit the packet's protocol and destination port.
|
||||
transitIPDenied
|
||||
// transitIPAllowed means the packet may be forwarded to the real IP
|
||||
// returned alongside this verdict.
|
||||
transitIPAllowed
|
||||
)
|
||||
|
||||
func (v transitIPVerdict) String() string {
|
||||
switch v {
|
||||
case transitIPUnmapped:
|
||||
return "unmapped"
|
||||
case transitIPDenied:
|
||||
return "denied"
|
||||
case transitIPAllowed:
|
||||
return "allowed"
|
||||
}
|
||||
return "transitIPVerdict(" + strconv.Itoa(int(v)) + ")"
|
||||
}
|
||||
|
||||
// realIPForTransitIPConnection is part of the implementation of the [Conn25Datapath] interface for dataflow lookups.
|
||||
// See also [Conn25Datapath.ConnectorRealIPForTransitIPConnection].
|
||||
func (c *connector) realIPForTransitIPConnection(srcIP netip.Addr, transitIP netip.Addr) (netip.Addr, bool) {
|
||||
//
|
||||
// It is reached only for a packet the datapath has no flow installed for, and
|
||||
// so is where the peer's grant for the app is enforced.
|
||||
func (c *connector) realIPForTransitIPConnection(p *packet.Parsed) (netip.Addr, transitIPVerdict) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
aa, ok := c.lookupAppAddrBySrcIPAndTransitIPLocked(srcIP, transitIP)
|
||||
return aa.addr, ok
|
||||
aa, ok := c.lookupAppAddrBySrcIPAndTransitIPLocked(p.Src.Addr(), p.Dst.Addr())
|
||||
if !ok {
|
||||
return netip.Addr{}, transitIPUnmapped
|
||||
}
|
||||
if !protoPortRangesAllow(aa.ports, *p) {
|
||||
return netip.Addr{}, transitIPDenied
|
||||
}
|
||||
return aa.addr, transitIPAllowed
|
||||
}
|
||||
|
||||
// transitIPInPool reports whether tip is within the transit IP pool of its
|
||||
@@ -176,28 +214,16 @@ func (c *connector) linkLocalAllow(p packet.Parsed) (bool, string) {
|
||||
|
||||
// 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, carries a protocol the datapath can map, 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.
|
||||
// configured transit IP pool and carries a protocol the datapath can map.
|
||||
//
|
||||
// Accepting here is not a decision that the packet reaches the app.
|
||||
// The datapath drops what the grant does not permit, and reports the drop as
|
||||
// the ACL drop it is through [tailscale.com/net/tstun.Wrapper.RejectInboundDueToACL].
|
||||
func (c *connector) ingressAllow(p packet.Parsed) (bool, string) {
|
||||
// Drop if:
|
||||
// - Datapath does not support the protocol. Admitting a protocol [isSupportedProtocol]
|
||||
// excludes would have datapath pass it through untouched, which
|
||||
// hands the host stack a packet addressed to something it has no route for.
|
||||
// - Packet destination is not in transit IP pool
|
||||
if !c.transitIPInPool(p.Dst.Addr()) || !isSupportedProtocol(p.IPProto) {
|
||||
return false, ""
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
aa, ok := c.lookupAppAddrBySrcIPAndTransitIPLocked(p.Src.Addr(), p.Dst.Addr())
|
||||
if !ok {
|
||||
// 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]).
|
||||
return true, connectorUnmappedReason
|
||||
}
|
||||
return protoPortRangesAllow(aa.ports, p)
|
||||
return true, connectorPendingReason
|
||||
}
|
||||
|
||||
// lookupAppAddrBySrcIPAndTransitIPLocked returns the mapping the peer at srcIP
|
||||
|
||||
@@ -17,9 +17,17 @@ import (
|
||||
"tailscale.com/types/appctype"
|
||||
"tailscale.com/types/ipproto"
|
||||
"tailscale.com/types/logger"
|
||||
"tailscale.com/types/views"
|
||||
"tailscale.com/util/must"
|
||||
)
|
||||
|
||||
// uint32ToIPv4 returns n as an IPv4 address, most significant byte first.
|
||||
func uint32ToIPv4(n uint32) netip.Addr {
|
||||
var buf [4]byte
|
||||
binary.BigEndian.PutUint32(buf[:], n)
|
||||
return netip.AddrFrom4(buf)
|
||||
}
|
||||
|
||||
func TestConnectorExpireTransitIPs(t *testing.T) {
|
||||
const appName = "app"
|
||||
|
||||
@@ -100,11 +108,6 @@ func TestConnectorExpireTransitIPs(t *testing.T) {
|
||||
}
|
||||
c.connector.mu.Unlock()
|
||||
|
||||
uint32ToIPv4 := func(n uint32) netip.Addr {
|
||||
var buf [4]byte
|
||||
binary.BigEndian.PutUint32(buf[:], n)
|
||||
return netip.AddrFrom4(buf)
|
||||
}
|
||||
// Transit IPs come from the pool configured above; destinations have to
|
||||
// stay out of it, or they'd be refused for being inside a configured pool.
|
||||
// The pool is 0.0.0.0/15, so the destinations start past 0.1.255.255.
|
||||
@@ -316,55 +319,84 @@ func TestConnectorLinkLocalAllow(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestConnectorIngressAllowProtocols checks that the connector's ingress
|
||||
// filter hook admits a packet to a transit IP only when the datapath can map
|
||||
// its protocol, and refuses addresses outside the transit pools outright.
|
||||
// TestConnectorIngressAllow checks that [connector.ingressAllow], the
|
||||
// connector's ingress filter hook, admits exactly the packets the datapath can
|
||||
// act on: those addressed into one of the configured transit IP pools, carrying
|
||||
// a protocol the datapath can map.
|
||||
//
|
||||
// No mapping is registered for the transit IP here, which isolates the
|
||||
// protocol check: an unmapped transit IP has no grant to be judged by and is
|
||||
// admitted for the datapath to answer, so whatever is refused below is refused
|
||||
// on protocol alone. The grant check is covered by [TestConnectorIngressAllow].
|
||||
func TestConnectorIngressAllowProtocols(t *testing.T) {
|
||||
src := netip.MustParseAddr("100.64.0.1")
|
||||
knownTip := netip.MustParseAddr("192.0.2.1")
|
||||
unknownTip := netip.MustParseAddr("100.64.0.42")
|
||||
// It deliberately does not vary the grant. Admitting a packet here is not a
|
||||
// decision that it reaches the app, only that the datapath gets to see it, and
|
||||
// the grant is enforced there, once per flow; see
|
||||
// [TestConnectorGrantEnforcement]. A mapping registered under a narrow grant
|
||||
// is what makes that visible below: ports and protocols the grant does not
|
||||
// name are admitted alike, so whatever is refused is refused on pool
|
||||
// membership or protocol alone.
|
||||
func TestConnectorIngressAllow(t *testing.T) {
|
||||
const appName = "app"
|
||||
|
||||
v4TransitIPsBuilder := netipx.IPSetBuilder{}
|
||||
v4TransitIPsBuilder.AddPrefix(netip.MustParsePrefix("192.0.2.0/24"))
|
||||
v4TransitIPs := must.Get(v4TransitIPsBuilder.IPSet())
|
||||
peerIP := netip.MustParseAddr("100.64.0.1")
|
||||
peerNode := (&tailcfg.Node{ID: 1, Addresses: []netip.Prefix{netip.PrefixFrom(peerIP, 32)}}).View()
|
||||
|
||||
mappedTip := netip.MustParseAddr("192.0.2.1")
|
||||
unmappedTip := netip.MustParseAddr("192.0.2.2")
|
||||
outsidePoolIP := netip.MustParseAddr("100.64.0.42")
|
||||
|
||||
c := newConn25(t.Logf)
|
||||
c.reconfig(&config{
|
||||
isConfigured: true,
|
||||
ipSets: ipSets{
|
||||
v4Transit: v4TransitIPs,
|
||||
},
|
||||
appsByName: map[string]appctype.AppConnectorAttr{appName: {}},
|
||||
ipSets: ipSets{v4Transit: mustIPSetFromPrefix("192.0.2.0/24")},
|
||||
})
|
||||
// A grant of tcp:443 alone, so that the cases below can reach past it.
|
||||
peerCap := tailcfg.PeerCapMap{
|
||||
peercap.Conn25Prefix.ToAttribute(appName): {`"tcp:443"`},
|
||||
}
|
||||
resp := c.handleConnectorTransitIPRequest(peerNode, peerCap, ConnectorTransitIPRequest{
|
||||
TransitIPs: []TransitIPRequest{withValidDstToken(t, c.connector, TransitIPRequest{
|
||||
TransitIP: mappedTip,
|
||||
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)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
proto ipproto.Proto
|
||||
port uint16
|
||||
dst netip.Addr
|
||||
want bool
|
||||
}{
|
||||
// The protocols the datapath maps between a transit IP and a
|
||||
// destination, which is the whole point of admitting these.
|
||||
{"tcp", ipproto.TCP, knownTip, true},
|
||||
{"udp", ipproto.UDP, knownTip, true},
|
||||
{"icmpv4", ipproto.ICMPv4, knownTip, true},
|
||||
{"icmpv6", ipproto.ICMPv6, knownTip, true},
|
||||
{"tcp", ipproto.TCP, 443, mappedTip, true},
|
||||
{"udp", ipproto.UDP, 53, mappedTip, true},
|
||||
{"icmpv4", ipproto.ICMPv4, 0, mappedTip, true},
|
||||
{"icmpv6", ipproto.ICMPv6, 0, mappedTip, true},
|
||||
|
||||
// The grant names tcp:443 only, but the grant is not this hook's to
|
||||
// enforce: a port and a protocol it does not permit are admitted for
|
||||
// the datapath to deny.
|
||||
{"tcp-to-ungranted-port", ipproto.TCP, 22, mappedTip, true},
|
||||
{"udp-to-ungranted-port", ipproto.UDP, 443, mappedTip, true},
|
||||
// As is a pool address no peer has registered, so that the datapath
|
||||
// can answer with the re-registration TSMP.
|
||||
{"tcp-to-unmapped-transit-ip", ipproto.TCP, 443, unmappedTip, true},
|
||||
{"tcp-to-unmapped-transit-ip-ungranted-port", ipproto.TCP, 22, unmappedTip, true},
|
||||
|
||||
// The rest the datapath passes through untouched, so admitting them
|
||||
// would only hand the host stack a packet addressed to a transit IP,
|
||||
// which is not an address this node holds.
|
||||
{"sctp", ipproto.SCTP, knownTip, false},
|
||||
{"gre", ipproto.Proto(47), knownTip, false},
|
||||
{"esp", ipproto.Proto(50), knownTip, false},
|
||||
{"tsmp", ipproto.TSMP, knownTip, false},
|
||||
{"sctp", ipproto.SCTP, 80, mappedTip, false},
|
||||
{"gre", ipproto.Proto(47), 0, mappedTip, false},
|
||||
{"esp", ipproto.Proto(50), 0, mappedTip, false},
|
||||
{"tsmp", ipproto.TSMP, 0, mappedTip, false},
|
||||
|
||||
// Outside the pool nothing is admitted whatever the protocol.
|
||||
{"tcp-outside-pool", ipproto.TCP, unknownTip, false},
|
||||
{"gre-outside-pool", ipproto.Proto(47), unknownTip, false},
|
||||
// Outside the pools nothing is admitted whatever the protocol.
|
||||
{"tcp-outside-pool", ipproto.TCP, 443, outsidePoolIP, false},
|
||||
{"gre-outside-pool", ipproto.Proto(47), 0, outsidePoolIP, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -372,12 +404,123 @@ func TestConnectorIngressAllowProtocols(t *testing.T) {
|
||||
got, why := c.connector.ingressAllow(packet.Parsed{
|
||||
IPVersion: 4,
|
||||
IPProto: tt.proto,
|
||||
Src: netip.AddrPortFrom(src, 1234),
|
||||
Dst: netip.AddrPortFrom(tt.dst, 1234),
|
||||
Src: netip.AddrPortFrom(peerIP, 12345),
|
||||
Dst: netip.AddrPortFrom(tt.dst, tt.port),
|
||||
})
|
||||
if got != tt.want {
|
||||
t.Errorf("ingressAllow(%v -> %v) = (%v, %q), want %v", tt.proto, tt.dst, got, why, tt.want)
|
||||
t.Errorf("ingressAllow(%v %v -> %v:%d) = (%v, %q), want %v",
|
||||
tt.proto, peerIP, tt.dst, tt.port, got, why, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// benchConnectorMappings is the number of transit IP mappings the ingress
|
||||
// benchmarks register before measuring.
|
||||
const benchConnectorMappings = 1024
|
||||
|
||||
// benchTransitIPAt returns the i'th address of the transit pool the ingress
|
||||
// benchmarks configure. The pool is a /15, so i has to be below 1<<17; past
|
||||
// that the addresses leave the pool and registering them fails.
|
||||
func benchTransitIPAt(i int) netip.Addr {
|
||||
return uint32ToIPv4(0xc0000000 | uint32(i))
|
||||
}
|
||||
|
||||
// newBenchConnector returns a connector holding [benchConnectorMappings]
|
||||
// mappings for the peer at peerIP, each registered under a grant of tcp:443.
|
||||
func newBenchConnector(b *testing.B, peerIP netip.Addr) *Conn25 {
|
||||
const appName = "app"
|
||||
|
||||
c := newConn25(logger.Discard)
|
||||
c.reconfig(&config{
|
||||
isConfigured: true,
|
||||
appsByName: map[string]appctype.AppConnectorAttr{appName: {}},
|
||||
ipSets: ipSets{v4Transit: mustIPSetFromPrefix("192.0.0.0/15")},
|
||||
})
|
||||
|
||||
node := (&tailcfg.Node{ID: 1, Addresses: []netip.Prefix{netip.PrefixFrom(peerIP, 32)}}).View()
|
||||
ports := views.SliceOf(must.Get(tailcfg.ParseProtoPortRanges([]string{"tcp:443"})))
|
||||
for i := range benchConnectorMappings {
|
||||
resp := c.connector.handleTransitIPRequest(node, peerIP, netip.Addr{}, TransitIPRequest{
|
||||
TransitIP: benchTransitIPAt(i),
|
||||
DestinationIP: netip.MustParseAddr("203.0.113.1"),
|
||||
App: appName,
|
||||
}, ports)
|
||||
// A refused request leaves no mapping behind, and an absent mapping is
|
||||
// not a denial, so a caller that did not check would quietly measure
|
||||
// the unmapped path instead of the grant.
|
||||
if resp.Code != OK {
|
||||
b.Fatalf("registering transit IP %v: code %d, %q", benchTransitIPAt(i), resp.Code, resp.Message)
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func benchPacketTo(peerIP, tip netip.Addr) packet.Parsed {
|
||||
return packet.Parsed{
|
||||
IPVersion: 4,
|
||||
IPProto: ipproto.TCP,
|
||||
TCPFlags: packet.TCPSyn,
|
||||
Src: netip.AddrPortFrom(peerIP, 12345),
|
||||
Dst: netip.AddrPortFrom(tip, 443),
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkConnectorIngressAllow measures the per-packet cost of the filter
|
||||
// hook, which every packet a peer sends to a transit IP pays, including the
|
||||
// ones the datapath will answer from its flow table. It is the reason the
|
||||
// grant is not checked here; compare
|
||||
// [BenchmarkConnectorRealIPForTransitIPConnection], which runs once per flow.
|
||||
func BenchmarkConnectorIngressAllow(b *testing.B) {
|
||||
peerIP := netip.MustParseAddr("100.64.0.1")
|
||||
c := newBenchConnector(b, peerIP)
|
||||
|
||||
// Check the verdict's reason once, outside the measured loop, so that the
|
||||
// loop only has to test a bool: an allow is not enough on its own to know
|
||||
// the hook took the path this benchmark is named for.
|
||||
allow, reason := c.connector.ingressAllow(benchPacketTo(peerIP, benchTransitIPAt(0)))
|
||||
if !allow || reason != connectorPendingReason {
|
||||
b.Fatalf("ingressAllow = %v, %q, want true, %q", allow, reason, connectorPendingReason)
|
||||
}
|
||||
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
var i int
|
||||
for pb.Next() {
|
||||
i++
|
||||
allow, _ := c.connector.ingressAllow(benchPacketTo(peerIP, benchTransitIPAt(i%benchConnectorMappings)))
|
||||
if !allow {
|
||||
b.Error("not allowed")
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// BenchmarkConnectorRealIPForTransitIPConnection measures the cost of the
|
||||
// grant check, which a peer pays once per flow rather than once per packet.
|
||||
func BenchmarkConnectorRealIPForTransitIPConnection(b *testing.B) {
|
||||
peerIP := netip.MustParseAddr("100.64.0.1")
|
||||
c := newBenchConnector(b, peerIP)
|
||||
|
||||
// As above: establish the path once, so the loop only tests a verdict.
|
||||
p := benchPacketTo(peerIP, benchTransitIPAt(0))
|
||||
if _, verdict := c.connector.realIPForTransitIPConnection(&p); verdict != transitIPAllowed {
|
||||
b.Fatalf("realIPForTransitIPConnection = %v, want %v", verdict, transitIPAllowed)
|
||||
}
|
||||
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
var i int
|
||||
for pb.Next() {
|
||||
i++
|
||||
p := benchPacketTo(peerIP, benchTransitIPAt(i%benchConnectorMappings))
|
||||
if _, verdict := c.connector.realIPForTransitIPConnection(&p); verdict != transitIPAllowed {
|
||||
b.Errorf("verdict = %v, want %v", verdict, transitIPAllowed)
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -27,6 +27,11 @@ var (
|
||||
ErrTransitIPNotRoutable = errors.New("transit IP not yet routable")
|
||||
|
||||
ErrUnmappedSrcAndTransitIP = errors.New("unmapped src and transit IP")
|
||||
|
||||
// ErrGrantDenied is returned when a peer has a mapping for the Transit IP
|
||||
// it addressed, but the grant it holds for that app does not permit the
|
||||
// packet's protocol and destination port.
|
||||
ErrGrantDenied = errors.New("app grant does not permit this traffic")
|
||||
)
|
||||
|
||||
// Conn25Datapath is the interface for the surface of [*Conn25] that the datapath
|
||||
@@ -37,14 +42,20 @@ var (
|
||||
// [*Conn25] is the only production implementation; the interface exists to let
|
||||
// datapath tests substitute a lightweight fake.
|
||||
type Conn25Datapath interface {
|
||||
// ConnectorRealIPForTransitIPConnection returns a real destination IP for the given
|
||||
// srcIP and transitIP on a connector. If the transitIP is within a configured Transit IP
|
||||
// range for an app on the connector, but not mapped to the client at srcIP, implementations
|
||||
// should return [ErrUnmappedSrcAndTransitIP]. If the transitIP is not within a configured
|
||||
// ConnectorRealIPForTransitIPConnection returns a real destination IP for p, a
|
||||
// packet from a peer to a Transit IP on a connector. If the Transit IP is within
|
||||
// a configured Transit IP range for an app on the connector, but not mapped to
|
||||
// the client that sent p, implementations should return
|
||||
// [ErrUnmappedSrcAndTransitIP]. If it is mapped, but the grant the client holds
|
||||
// for the app does not permit p, implementations should return [ErrGrantDenied].
|
||||
// If the Transit IP is not within a configured
|
||||
// Transit IP range, i.e. it is not actually a Transit IP, implementations should return
|
||||
// a nil error, and a zero-value [netip.Addr] to indicate this is potentially valid,
|
||||
// non-app-connector traffic.
|
||||
ConnectorRealIPForTransitIPConnection(srcIP netip.Addr, transitIP netip.Addr) (netip.Addr, error)
|
||||
//
|
||||
// It is called only for packets the datapath has no flow installed for, so implementations
|
||||
// enforcing a grant enforce it once per flow rather than once per packet.
|
||||
ConnectorRealIPForTransitIPConnection(p *packet.Parsed) (netip.Addr, error)
|
||||
|
||||
// ClientPrepareNewFlowForMagicIP is called before a client-side flow to
|
||||
// magicIP is installed in the client flow table. It reports whether it
|
||||
@@ -171,8 +182,14 @@ func (dh *datapathHandler) HandlePacketFromWireGuard(p *packet.Parsed, tun *tstu
|
||||
// other (non-app-connector) traffic, or broken app-connector traffic
|
||||
// that needs to be re-established by a new outbound packet.
|
||||
transitIP := p.Dst.Addr()
|
||||
realIP, err := dh.conn25.ConnectorRealIPForTransitIPConnection(p.Src.Addr(), transitIP)
|
||||
realIP, err := dh.conn25.ConnectorRealIPForTransitIPConnection(p)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrGrantDenied) {
|
||||
// The peer's grant for the app does not permit this protocol and port. That is an ACL drop,
|
||||
// so it is counted and reported to the sender as one.
|
||||
tun.RejectInboundDueToACL(p, packet.RejectedDueToACLs)
|
||||
return filter.Drop
|
||||
}
|
||||
if errors.Is(err, ErrUnmappedSrcAndTransitIP) {
|
||||
rj := packet.TailscaleRejectedHeader{
|
||||
IPSrc: p.Dst.Addr(),
|
||||
|
||||
@@ -26,7 +26,7 @@ import (
|
||||
|
||||
type testConn25 struct {
|
||||
clientPrepareNewFlowForMagicIPFn func(netip.Addr) (netip.Addr, error)
|
||||
connectorRealIPForTransitIPConnectionFn func(netip.Addr, netip.Addr) (netip.Addr, error)
|
||||
connectorRealIPForTransitIPConnectionFn func(*packet.Parsed) (netip.Addr, error)
|
||||
|
||||
// countsInvalidTransitIP makes ClientPrepareNewFlowForMagicIP model an
|
||||
// implementation that violates the [Conn25Datapath] contract by reporting
|
||||
@@ -48,8 +48,8 @@ func (tc *testConn25) ClientPrepareNewFlowForMagicIP(magicIP netip.Addr) (Client
|
||||
return ClientFlowRef{transit: transitIP}, true, nil
|
||||
}
|
||||
|
||||
func (tc *testConn25) ConnectorRealIPForTransitIPConnection(srcIP netip.Addr, transitIP netip.Addr) (netip.Addr, error) {
|
||||
return tc.connectorRealIPForTransitIPConnectionFn(srcIP, transitIP)
|
||||
func (tc *testConn25) ConnectorRealIPForTransitIPConnection(p *packet.Parsed) (netip.Addr, error) {
|
||||
return tc.connectorRealIPForTransitIPConnectionFn(p)
|
||||
}
|
||||
|
||||
// testNet is the cast of addresses the datapath tests run against: a client,
|
||||
@@ -199,23 +199,36 @@ func (tn testNet) newClientDatapath(t *testing.T, throwMappingErr bool) *datapat
|
||||
return newDatapathHandler(mock, t.Logf)
|
||||
}
|
||||
|
||||
// connectorGrantedPort is the only destination port
|
||||
// [testNet.newConnectorDatapath]'s connector has a grant for; anything else it
|
||||
// reports as denied, standing in for a peer reaching past its grant.
|
||||
const connectorGrantedPort = 80
|
||||
|
||||
func (tn testNet) newConnectorDatapath(t *testing.T, throwMappingErr bool) *datapathHandler {
|
||||
t.Helper()
|
||||
return newDatapathHandler(tn.newConnectorConn25(t, throwMappingErr), t.Logf)
|
||||
}
|
||||
|
||||
func (tn testNet) newConnectorConn25(t *testing.T, throwMappingErr bool) *testConn25 {
|
||||
t.Helper()
|
||||
|
||||
mock := &testConn25{}
|
||||
mock.connectorRealIPForTransitIPConnectionFn = func(src, tip netip.Addr) (netip.Addr, error) {
|
||||
mock.connectorRealIPForTransitIPConnectionFn = func(p *packet.Parsed) (netip.Addr, error) {
|
||||
if throwMappingErr {
|
||||
return netip.Addr{}, errors.New("synthetic mapping error")
|
||||
}
|
||||
if tip == tn.transitIP {
|
||||
if src == tn.clientSrcIP {
|
||||
return tn.realIP, nil
|
||||
if p.Dst.Addr() == tn.transitIP {
|
||||
if p.Src.Addr() != tn.clientSrcIP {
|
||||
return netip.Addr{}, ErrUnmappedSrcAndTransitIP
|
||||
}
|
||||
return netip.Addr{}, ErrUnmappedSrcAndTransitIP
|
||||
if p.Dst.Port() != connectorGrantedPort {
|
||||
return netip.Addr{}, ErrGrantDenied
|
||||
}
|
||||
return tn.realIP, nil
|
||||
}
|
||||
return netip.Addr{}, nil
|
||||
}
|
||||
return newDatapathHandler(mock, t.Logf)
|
||||
return mock
|
||||
}
|
||||
|
||||
func TestHandlePacketFromTunDevice(t *testing.T) {
|
||||
@@ -390,6 +403,14 @@ func TestHandlePacketFromWireGuard(t *testing.T) {
|
||||
Reason: packet.RejectedDueToUnknownAppConnectorTransitIP,
|
||||
}, nil),
|
||||
},
|
||||
{
|
||||
description: "drop-ungranted-port",
|
||||
src: netip.AddrPortFrom(tn.clientSrcIP, clientPort),
|
||||
dst: netip.AddrPortFrom(tn.transitIP, serverPort+1),
|
||||
expectedSrc: netip.AddrPortFrom(tn.clientSrcIP, clientPort),
|
||||
expectedDst: netip.AddrPortFrom(tn.transitIP, serverPort+1),
|
||||
expectedFilterResponse: filter.Drop,
|
||||
},
|
||||
{
|
||||
description: "accept-dont-nat-other-mapping-error",
|
||||
src: netip.AddrPortFrom(tn.clientSrcIP, clientPort),
|
||||
@@ -564,7 +585,7 @@ func TestConnectorFlowCache(t *testing.T) {
|
||||
getRealIPCalled := false
|
||||
|
||||
mock := &testConn25{}
|
||||
mock.connectorRealIPForTransitIPConnectionFn = func(src, tip netip.Addr) (netip.Addr, error) {
|
||||
mock.connectorRealIPForTransitIPConnectionFn = func(*packet.Parsed) (netip.Addr, error) {
|
||||
if getRealIPCalled {
|
||||
t.Errorf("ConnectorRealIPForTransitIPConnection unexpectedly called more than once")
|
||||
}
|
||||
@@ -614,6 +635,41 @@ func TestConnectorFlowCache(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestConnectorGrantDenialInstallsNoFlow checks that a packet the grant does
|
||||
// not permit leaves nothing behind in the flow table. A flow installed for a
|
||||
// denied packet would be a flow that answers the next one without asking
|
||||
// anybody, which is the whole mechanism this check sits in front of.
|
||||
func TestConnectorGrantDenialInstallsNoFlow(t *testing.T) {
|
||||
const clientPort = 1234
|
||||
|
||||
for _, tn := range testNets {
|
||||
t.Run(tn.name, func(t *testing.T) {
|
||||
mock := tn.newConnectorConn25(t, false)
|
||||
resolved := 0
|
||||
inner := mock.connectorRealIPForTransitIPConnectionFn
|
||||
mock.connectorRealIPForTransitIPConnectionFn = func(p *packet.Parsed) (netip.Addr, error) {
|
||||
resolved++
|
||||
return inner(p)
|
||||
}
|
||||
dph := newDatapathHandler(mock, t.Logf)
|
||||
tun := newFakeTUN(t)
|
||||
|
||||
ungranted := netip.AddrPortFrom(tn.transitIP, connectorGrantedPort+1)
|
||||
for i := range 2 {
|
||||
p := tn.udpPacket(netip.AddrPortFrom(tn.clientSrcIP, clientPort), ungranted)
|
||||
if got := dph.HandlePacketFromWireGuard(p, tun); got != filter.Drop {
|
||||
t.Fatalf("packet %d: HandlePacketFromWireGuard = %v, want %v", i, got, filter.Drop)
|
||||
}
|
||||
}
|
||||
// Both packets had to be resolved. One resolution would mean the
|
||||
// first denial installed a flow that carried the second.
|
||||
if resolved != 2 {
|
||||
t.Errorf("resolved %d times, want 2; a denied packet installed a flow", resolved)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newFakeTUN(t *testing.T) *tstun.Wrapper {
|
||||
t.Helper()
|
||||
return newWrappedTUN(t, tstun.NewFake())
|
||||
|
||||
+31
-35
@@ -22,27 +22,21 @@ const (
|
||||
// blanket drop of link-local destinations.
|
||||
connectorTransitIPReason = "app connector transit IP"
|
||||
|
||||
// connectorUnmappedReason is returned for a packet addressed to a transit
|
||||
// IP the peer has no mapping registered for. See [connector.ingressAllow]
|
||||
// for why those are admitted.
|
||||
connectorUnmappedReason = "app connector unregistered transit IP"
|
||||
|
||||
// The remaining reasons are returned by [protoPortRangesAllow], and are
|
||||
// named after the [tailscale.com/wgengine/filter.Filter.RunIn] verdicts
|
||||
// they correspond to.
|
||||
connectorICMPReason = "app connector icmp ok"
|
||||
connectorTCPReason = "app connector tcp ok"
|
||||
connectorPortReason = "app connector ok"
|
||||
// connectorPendingReason is returned by [connector.ingressAllow] for a
|
||||
// packet it admits for the datapath to judge. The datapath may still drop the
|
||||
// packet, for an unmapped transit IP or for a protocol and port the
|
||||
// peer's grant does not permit.
|
||||
connectorPendingReason = "app connector transit IP, pending datapath verdict"
|
||||
)
|
||||
|
||||
// protoPortRangesAllow reports whether ranges, the protocol and port combinations a peer was granted
|
||||
// for an app, permit p, and if so the reason to log alongside the allow verdict.
|
||||
// protoPortRangesAllow reports whether ranges, the protocol and port
|
||||
// combinations a peer was granted for an app, permit p.
|
||||
// It is modelled on [tailscale.com/wgengine/filter.Filter.RunIn].
|
||||
//
|
||||
// Only TCP, UDP and ICMP are answered for, because they are the only protocols
|
||||
// the datapath can map to a transit IP: the caller, [connector.ingressAllow],
|
||||
// gates on [isSupportedProtocol] and refuses the rest before any grant is consulted.
|
||||
func protoPortRangesAllow(ranges views.Slice[tailcfg.ProtoPortRange], p packet.Parsed) (bool, string) {
|
||||
// the datapath can map to a transit IP: the caller, [connector.realIPForTransitIPConnection],
|
||||
// is reached only for a packet [isSupportedProtocol] admitted.
|
||||
func protoPortRangesAllow(ranges views.Slice[tailcfg.ProtoPortRange], p packet.Parsed) bool {
|
||||
switch p.IPProto {
|
||||
case ipproto.ICMPv4, ipproto.ICMPv6:
|
||||
var want ipproto.Proto
|
||||
@@ -57,47 +51,49 @@ func protoPortRangesAllow(ranges views.Slice[tailcfg.ProtoPortRange], p packet.P
|
||||
// version and so lets an ICMP type that does not match the
|
||||
// packet's IP version fall through to its default case. Refusing
|
||||
// it outright seems unlikely to matter in practice.
|
||||
return false, ""
|
||||
return false
|
||||
}
|
||||
if ranges.Len() > 0 {
|
||||
// ICMP carries no port, so any grant at all permits it, however it
|
||||
// is written: if any port is open to an app, ICMP to it is too.
|
||||
return true, connectorICMPReason
|
||||
return true
|
||||
}
|
||||
// A further diversion from RunIn, which admits an echo response or an
|
||||
// ICMP error whatever the rules say, so that return traffic for pings
|
||||
// this node sent still arrives. There is no such return traffic on this
|
||||
// path: a connector sends nothing towards its own transit pool, and
|
||||
// what the app sends back reaches the peer through the datapath's SNAT
|
||||
// for the flow rather than through this hook. Admitting responses here
|
||||
// would only let a grant carrying no values, which is documented to
|
||||
// permit no traffic, still deliver arbitrary ICMP errors to the app.
|
||||
// this node sent still arrives. There is no such return traffic to
|
||||
// make room for: a connector sends nothing towards its own transit
|
||||
// pool, and what the app sends back reaches the peer through the
|
||||
// datapath's SNAT for the flow rather than through a grant check.
|
||||
// Admitting responses here would only let a grant carrying no values,
|
||||
// which is documented to permit no traffic, still deliver arbitrary
|
||||
// ICMP errors to the app.
|
||||
case ipproto.TCP:
|
||||
// A diversion from RunIn, which admits any packet that is not a lone
|
||||
// SYN so that return traffic for connections this node opened still
|
||||
// arrives. A connector never opens a connection towards its own
|
||||
// transit pool, so there is no return traffic to make room for here:
|
||||
// every TCP packet this hook sees travels peer -> transit IP, and the
|
||||
// datapath rewrites only addresses, never ports, so its destination
|
||||
// port is the app's port for the whole session.
|
||||
// Exempting non-SYNs, on the other hand, would let a peer reach a
|
||||
// port the grant does not name, since what is admitted here is then
|
||||
// DNATed on to the app: an ACK scan of it, with a flow installed.
|
||||
// transit pool, so there is no return traffic to make room for: every
|
||||
// TCP packet checked here travels peer -> transit IP, and the datapath
|
||||
// rewrites only addresses, never ports, so its destination port is the
|
||||
// app's port for the whole session.
|
||||
//
|
||||
// Exempting non-SYNs would let a peer reach a port the grant does not
|
||||
// name, since what is admitted here is then DNATed on to the app: an
|
||||
// ACK scan of it, with a flow installed.
|
||||
for _, ppr := range ranges.All() {
|
||||
if (ppr.Proto == 0 || ppr.Proto == int(ipproto.TCP)) &&
|
||||
ppr.Ports.Contains(p.Dst.Port()) {
|
||||
return true, connectorTCPReason
|
||||
return true
|
||||
}
|
||||
}
|
||||
case ipproto.UDP:
|
||||
for _, ppr := range ranges.All() {
|
||||
if (ppr.Proto == 0 || ppr.Proto == int(ipproto.UDP)) &&
|
||||
ppr.Ports.Contains(p.Dst.Port()) {
|
||||
return true, connectorPortReason
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false, ""
|
||||
return false
|
||||
}
|
||||
|
||||
// appProtoPortRanges returns the protocol and port combinations peerCaps
|
||||
@@ -108,7 +104,7 @@ func protoPortRangesAllow(ranges views.Slice[tailcfg.ProtoPortRange], p packet.P
|
||||
// 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.
|
||||
// [connector.realIPForTransitIPConnection] forwards nothing.
|
||||
func appProtoPortRanges(peerCaps tailcfg.PeerCapMap, app string) (
|
||||
ranges views.Slice[tailcfg.ProtoPortRange],
|
||||
hasCap bool,
|
||||
|
||||
+125
-119
@@ -16,18 +16,23 @@ import (
|
||||
"tailscale.com/types/views"
|
||||
)
|
||||
|
||||
func TestProtoPortRangesAllow(t *testing.T) {
|
||||
// pprs builds the grant values a peer holds for an app.
|
||||
pprs := func(ss ...string) views.Slice[tailcfg.ProtoPortRange] {
|
||||
var out []tailcfg.ProtoPortRange
|
||||
for _, s := range ss {
|
||||
var p tailcfg.ProtoPortRange
|
||||
if err := p.UnmarshalText([]byte(s)); err != nil {
|
||||
t.Fatalf("parsing %q: %v", s, err)
|
||||
}
|
||||
out = append(out, p)
|
||||
// mustProtoPortRanges builds the grant values a peer holds for an app.
|
||||
func mustProtoPortRanges(t *testing.T, ss ...string) views.Slice[tailcfg.ProtoPortRange] {
|
||||
t.Helper()
|
||||
var out []tailcfg.ProtoPortRange
|
||||
for _, s := range ss {
|
||||
var p tailcfg.ProtoPortRange
|
||||
if err := p.UnmarshalText([]byte(s)); err != nil {
|
||||
t.Fatalf("parsing %q: %v", s, err)
|
||||
}
|
||||
return views.SliceOf(out)
|
||||
out = append(out, p)
|
||||
}
|
||||
return views.SliceOf(out)
|
||||
}
|
||||
|
||||
func TestProtoPortRangesAllow(t *testing.T) {
|
||||
pprs := func(ss ...string) views.Slice[tailcfg.ProtoPortRange] {
|
||||
return mustProtoPortRanges(t, ss...)
|
||||
}
|
||||
|
||||
src := netip.MustParseAddr("100.64.0.1")
|
||||
@@ -67,92 +72,90 @@ func TestProtoPortRangesAllow(t *testing.T) {
|
||||
const gre = ipproto.Proto(47)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
ranges views.Slice[tailcfg.ProtoPortRange]
|
||||
p packet.Parsed
|
||||
want bool
|
||||
wantReason string
|
||||
name string
|
||||
ranges views.Slice[tailcfg.ProtoPortRange]
|
||||
p packet.Parsed
|
||||
want bool
|
||||
}{
|
||||
{"tcp-syn-match", pprs("tcp:443"), tcpSyn(443), true, connectorTCPReason},
|
||||
{"tcp-syn-wrong-port", pprs("tcp:443"), tcpSyn(80), false, ""},
|
||||
{"tcp-syn-wrong-proto", pprs("udp:443"), tcpSyn(443), false, ""},
|
||||
{"tcp-syn-protoless", pprs("80"), tcpSyn(80), true, connectorTCPReason},
|
||||
{"tcp-syn-wildcard", pprs("*"), tcpSyn(1), true, connectorTCPReason},
|
||||
{"tcp-syn-any-port", pprs("tcp:*"), tcpSyn(9999), true, connectorTCPReason},
|
||||
{"tcp-syn-range-first", pprs("tcp:80-90"), tcpSyn(80), true, connectorTCPReason},
|
||||
{"tcp-syn-range-last", pprs("tcp:80-90"), tcpSyn(90), true, connectorTCPReason},
|
||||
{"tcp-syn-range-low", pprs("tcp:80-90"), tcpSyn(79), false, ""},
|
||||
{"tcp-syn-range-high", pprs("tcp:80-90"), tcpSyn(91), false, ""},
|
||||
{"tcp-syn-empty-grant", pprs(), tcpSyn(443), false, ""},
|
||||
{"tcp-syn-union-first", pprs("tcp:443", "udp:53"), tcpSyn(443), true, connectorTCPReason},
|
||||
{"tcp-syn-union-neither", pprs("tcp:443", "udp:53"), tcpSyn(53), false, ""},
|
||||
{"tcp-syn-match", pprs("tcp:443"), tcpSyn(443), true},
|
||||
{"tcp-syn-wrong-port", pprs("tcp:443"), tcpSyn(80), false},
|
||||
{"tcp-syn-wrong-proto", pprs("udp:443"), tcpSyn(443), false},
|
||||
{"tcp-syn-protoless", pprs("80"), tcpSyn(80), true},
|
||||
{"tcp-syn-wildcard", pprs("*"), tcpSyn(1), true},
|
||||
{"tcp-syn-any-port", pprs("tcp:*"), tcpSyn(9999), true},
|
||||
{"tcp-syn-range-first", pprs("tcp:80-90"), tcpSyn(80), true},
|
||||
{"tcp-syn-range-last", pprs("tcp:80-90"), tcpSyn(90), true},
|
||||
{"tcp-syn-range-low", pprs("tcp:80-90"), tcpSyn(79), false},
|
||||
{"tcp-syn-range-high", pprs("tcp:80-90"), tcpSyn(91), false},
|
||||
{"tcp-syn-empty-grant", pprs(), tcpSyn(443), false},
|
||||
{"tcp-syn-union-first", pprs("tcp:443", "udp:53"), tcpSyn(443), true},
|
||||
{"tcp-syn-union-neither", pprs("tcp:443", "udp:53"), tcpSyn(53), false},
|
||||
|
||||
// Unlike RunIn, a packet that is not a lone SYN is held to the grant
|
||||
// too. Every packet of a session carries the app's port, so the ones
|
||||
// continuing a permitted session still pass...
|
||||
{"tcp-ack-granted-port", pprs("tcp:443"), tcp(443, packet.TCPAck), true, connectorTCPReason},
|
||||
{"tcp-fin-granted-port", pprs("tcp:443"), tcp(443, packet.TCPFin), true, connectorTCPReason},
|
||||
{"tcp-rst-granted-port", pprs("tcp:443"), tcp(443, packet.TCPRst), true, connectorTCPReason},
|
||||
{"tcp-ack-granted-port", pprs("tcp:443"), tcp(443, packet.TCPAck), true},
|
||||
{"tcp-fin-granted-port", pprs("tcp:443"), tcp(443, packet.TCPFin), true},
|
||||
{"tcp-rst-granted-port", pprs("tcp:443"), tcp(443, packet.TCPRst), true},
|
||||
// ...while one aimed at a port the grant does not name does not, so a
|
||||
// peer cannot ACK-scan the app around the grant.
|
||||
{"tcp-ack-ungranted-port", pprs("tcp:443"), tcp(22, packet.TCPAck), false, ""},
|
||||
{"tcp-ack-empty-grant", pprs(), tcp(22, packet.TCPAck), false, ""},
|
||||
{"tcp-syn-ack-ungranted-port", pprs("tcp:443"), tcp(22, packet.TCPSynAck), false, ""},
|
||||
{"tcp-rst-ungranted-port", pprs("tcp:443"), tcp(22, packet.TCPRst), false, ""},
|
||||
{"tcp-ack-ungranted-port", pprs("tcp:443"), tcp(22, packet.TCPAck), false},
|
||||
{"tcp-ack-empty-grant", pprs(), tcp(22, packet.TCPAck), false},
|
||||
{"tcp-syn-ack-ungranted-port", pprs("tcp:443"), tcp(22, packet.TCPSynAck), false},
|
||||
{"tcp-rst-ungranted-port", pprs("tcp:443"), tcp(22, packet.TCPRst), false},
|
||||
|
||||
{"udp-match", pprs("udp:53"), udp(53), true, connectorPortReason},
|
||||
{"udp-wrong-port", pprs("udp:53"), udp(54), false, ""},
|
||||
{"udp-protoless", pprs("80"), udp(80), true, connectorPortReason},
|
||||
{"udp-empty-grant", pprs(), udp(53), false, ""},
|
||||
{"udp-protoless-allowed", pprs("9"), udp(9), true, connectorPortReason},
|
||||
{"udp-match", pprs("udp:53"), udp(53), true},
|
||||
{"udp-wrong-port", pprs("udp:53"), udp(54), false},
|
||||
{"udp-protoless", pprs("80"), udp(80), true},
|
||||
{"udp-empty-grant", pprs(), udp(53), false},
|
||||
{"udp-protoless-allowed", pprs("9"), udp(9), true},
|
||||
|
||||
// Any grant at all permits ICMP, since ICMP carries no port for a
|
||||
// grant to name, and an empty grant permits none of it. The ICMP type
|
||||
// does not enter into it either way: unlike RunIn, responses and
|
||||
// errors are not admitted around the grant, there being no return
|
||||
// traffic on this path for that to serve.
|
||||
{"icmp4-request-with-grant", pprs("tcp:443"), icmp4(packet.ICMP4EchoRequest), true, connectorICMPReason},
|
||||
{"icmp4-echo-reply-with-grant", pprs("tcp:443"), icmp4(packet.ICMP4EchoReply), true, connectorICMPReason},
|
||||
{"icmp4-unreachable-with-grant", pprs("tcp:443"), icmp4(packet.ICMP4Unreachable), true, connectorICMPReason},
|
||||
{"icmp4-request-empty-grant", pprs(), icmp4(packet.ICMP4EchoRequest), false, ""},
|
||||
{"icmp4-echo-reply-empty-grant", pprs(), icmp4(packet.ICMP4EchoReply), false, ""},
|
||||
{"icmp4-unreachable-empty-grant", pprs(), icmp4(packet.ICMP4Unreachable), false, ""},
|
||||
{"icmp4-request-with-grant", pprs("tcp:443"), icmp4(packet.ICMP4EchoRequest), true},
|
||||
{"icmp4-echo-reply-with-grant", pprs("tcp:443"), icmp4(packet.ICMP4EchoReply), true},
|
||||
{"icmp4-unreachable-with-grant", pprs("tcp:443"), icmp4(packet.ICMP4Unreachable), true},
|
||||
{"icmp4-request-empty-grant", pprs(), icmp4(packet.ICMP4EchoRequest), false},
|
||||
{"icmp4-echo-reply-empty-grant", pprs(), icmp4(packet.ICMP4EchoReply), false},
|
||||
{"icmp4-unreachable-empty-grant", pprs(), icmp4(packet.ICMP4Unreachable), false},
|
||||
// An ICMP protocol that disagrees with the packet's IP version is
|
||||
// refused rather than falling through to the default case.
|
||||
{"icmp6-on-ipv4", pprs("*"), pkt(4, ipproto.ICMPv6, 0, 0), false, ""},
|
||||
{"icmp4-on-ipv6", pprs("*"), pkt(6, ipproto.ICMPv4, 0, 0), false, ""},
|
||||
{"icmp6-on-ipv4", pprs("*"), pkt(4, ipproto.ICMPv6, 0, 0), false},
|
||||
{"icmp4-on-ipv6", pprs("*"), pkt(6, ipproto.ICMPv4, 0, 0), false},
|
||||
|
||||
// Only the protocols the datapath can map to a transit IP are answered
|
||||
// for, so no grant permits anything else however it is written.
|
||||
{"sctp-granted-by-number", pprs("132:9"), sctp(9), false, ""},
|
||||
{"sctp-protoless-grant", pprs("9"), sctp(9), false, ""},
|
||||
{"gre-granted-by-number", pprs("47:*"), pkt(4, gre, 0, 0), false, ""},
|
||||
{"gre-wildcard-grant", pprs("*"), pkt(4, gre, 0, 0), false, ""},
|
||||
{"tsmp-granted-by-number", pprs("99:*"), pkt(4, ipproto.TSMP, 0, 0), false, ""},
|
||||
{"tsmp-wildcard-grant", pprs("*"), pkt(4, ipproto.TSMP, 0, 0), false, ""},
|
||||
{"tsmp-ipv6", pprs("*"), pkt(6, ipproto.TSMP, 0, 0), false, ""},
|
||||
{"tsmp-empty-grant", pprs(), pkt(4, ipproto.TSMP, 0, 0), false, ""},
|
||||
{"sctp-granted-by-number", pprs("132:9"), sctp(9), false},
|
||||
{"sctp-protoless-grant", pprs("9"), sctp(9), false},
|
||||
{"gre-granted-by-number", pprs("47:*"), pkt(4, gre, 0, 0), false},
|
||||
{"gre-wildcard-grant", pprs("*"), pkt(4, gre, 0, 0), false},
|
||||
{"tsmp-granted-by-number", pprs("99:*"), pkt(4, ipproto.TSMP, 0, 0), false},
|
||||
{"tsmp-wildcard-grant", pprs("*"), pkt(4, ipproto.TSMP, 0, 0), false},
|
||||
{"tsmp-ipv6", pprs("*"), pkt(6, ipproto.TSMP, 0, 0), false},
|
||||
{"tsmp-empty-grant", pprs(), pkt(4, ipproto.TSMP, 0, 0), false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, gotReason := protoPortRangesAllow(tt.ranges, tt.p)
|
||||
if got != tt.want || gotReason != tt.wantReason {
|
||||
t.Errorf("protoPortRangesAllow(%v, %v) = (%v, %q), want (%v, %q)",
|
||||
tt.ranges, &tt.p, got, gotReason, tt.want, tt.wantReason)
|
||||
if got := protoPortRangesAllow(tt.ranges, tt.p); got != tt.want {
|
||||
t.Errorf("protoPortRangesAllow(%v, %v) = %v, want %v",
|
||||
tt.ranges, &tt.p, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
// TestConnectorGrantEnforcement checks [connector.realIPForTransitIPConnection],
|
||||
// which the datapath consults for the first packet of a flow, holds peers to
|
||||
// the protocols and ports their grant for the app permits, and reports a transit IP
|
||||
// with no registered mapping as unmapped rather than as a denial, so the datapath can answer with
|
||||
// the re-registration TSMP.
|
||||
//
|
||||
// The per-protocol matching itself is covered in more detail by [TestProtoPortRangesAllow].
|
||||
func TestConnectorIngressAllow(t *testing.T) {
|
||||
func TestConnectorGrantEnforcement(t *testing.T) {
|
||||
const appName = "app"
|
||||
|
||||
peerIP := netip.MustParseAddr("100.64.0.1")
|
||||
@@ -161,7 +164,7 @@ func TestConnectorIngressAllow(t *testing.T) {
|
||||
|
||||
transitIP := netip.MustParseAddr("192.0.2.1")
|
||||
unmappedTransitIP := netip.MustParseAddr("192.0.2.2")
|
||||
outsidePoolIP := netip.MustParseAddr("100.64.0.42")
|
||||
destIP := netip.MustParseAddr("203.0.113.1")
|
||||
|
||||
type probe struct {
|
||||
proto ipproto.Proto
|
||||
@@ -171,7 +174,7 @@ func TestConnectorIngressAllow(t *testing.T) {
|
||||
flags packet.TCPFlag
|
||||
dst netip.Addr // defaults to transitIP
|
||||
src netip.Addr // defaults to peerIP
|
||||
want bool
|
||||
want transitIPVerdict
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
@@ -186,98 +189,90 @@ func TestConnectorIngressAllow(t *testing.T) {
|
||||
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},
|
||||
{proto: ipproto.TCP, port: 443, want: transitIPAllowed},
|
||||
{proto: ipproto.TCP, port: 80, want: transitIPAllowed},
|
||||
{proto: ipproto.UDP, port: 53, want: transitIPAllowed},
|
||||
{proto: ipproto.ICMPv4, want: transitIPAllowed},
|
||||
},
|
||||
},
|
||||
{
|
||||
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},
|
||||
{proto: ipproto.TCP, port: 443, want: transitIPAllowed},
|
||||
{proto: ipproto.TCP, port: 80, want: transitIPDenied},
|
||||
{proto: ipproto.TCP, port: 442, want: transitIPDenied},
|
||||
{proto: ipproto.TCP, port: 444, want: transitIPDenied},
|
||||
{proto: ipproto.UDP, port: 443, want: transitIPDenied},
|
||||
// ICMP has no port, so any grant at all permits it.
|
||||
{proto: ipproto.ICMPv4, want: true},
|
||||
{proto: ipproto.ICMPv4, want: transitIPAllowed},
|
||||
},
|
||||
},
|
||||
{
|
||||
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},
|
||||
{proto: ipproto.TCP, port: 80, want: transitIPAllowed},
|
||||
{proto: ipproto.UDP, port: 80, want: transitIPAllowed},
|
||||
{proto: ipproto.TCP, port: 443, want: transitIPDenied},
|
||||
// A protocol-less range covers TCP and UDP, not SCTP.
|
||||
{proto: ipproto.SCTP, port: 80, want: false},
|
||||
{proto: ipproto.SCTP, port: 80, want: transitIPDenied},
|
||||
},
|
||||
},
|
||||
{
|
||||
// Diverging from the packet filter, every TCP packet is held to
|
||||
// the grant's ports, not just a lone SYN; a connector has no
|
||||
// return traffic arriving on this path to make room for. See
|
||||
// [protoPortRangesAllow].
|
||||
// A packet that is not a lone SYN is held to the grant's ports
|
||||
// too. Reaching here at all means the datapath found no flow for
|
||||
// it, so there is no established session for it to belong to.
|
||||
name: "tcp-non-syn-held-to-ports",
|
||||
grant: []tailcfg.RawMessage{`"tcp:443"`},
|
||||
probes: []probe{
|
||||
{proto: ipproto.TCP, port: 443, flags: packet.TCPAck, want: true},
|
||||
{proto: ipproto.TCP, port: 22, flags: packet.TCPAck, want: false},
|
||||
{proto: ipproto.TCP, port: 22, flags: packet.TCPSynAck, want: false},
|
||||
{proto: ipproto.TCP, port: 22, flags: packet.TCPSyn, want: false},
|
||||
{proto: ipproto.TCP, port: 443, flags: packet.TCPAck, want: transitIPAllowed},
|
||||
{proto: ipproto.TCP, port: 22, flags: packet.TCPAck, want: transitIPDenied},
|
||||
{proto: ipproto.TCP, port: 22, flags: packet.TCPSynAck, want: transitIPDenied},
|
||||
{proto: ipproto.TCP, port: 22, flags: packet.TCPSyn, want: transitIPDenied},
|
||||
},
|
||||
},
|
||||
{
|
||||
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},
|
||||
{proto: ipproto.TCP, port: 7999, want: transitIPDenied},
|
||||
{proto: ipproto.TCP, port: 8000, want: transitIPAllowed},
|
||||
{proto: ipproto.TCP, port: 8050, want: transitIPAllowed},
|
||||
{proto: ipproto.TCP, port: 8100, want: transitIPAllowed},
|
||||
{proto: ipproto.TCP, port: 8101, want: transitIPDenied},
|
||||
},
|
||||
},
|
||||
{
|
||||
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},
|
||||
{proto: ipproto.TCP, port: 443, want: transitIPAllowed},
|
||||
{proto: ipproto.UDP, port: 53, want: transitIPAllowed},
|
||||
{proto: ipproto.UDP, port: 443, want: transitIPDenied},
|
||||
{proto: ipproto.TCP, port: 53, want: transitIPDenied},
|
||||
},
|
||||
},
|
||||
{
|
||||
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},
|
||||
{proto: ipproto.TSMP, want: false},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "outside-transit-pool",
|
||||
grant: []tailcfg.RawMessage{`"*"`},
|
||||
probes: []probe{
|
||||
{proto: ipproto.TCP, port: 443, dst: outsidePoolIP, want: false},
|
||||
{proto: ipproto.TCP, port: 443, want: transitIPDenied},
|
||||
{proto: ipproto.UDP, port: 53, want: transitIPDenied},
|
||||
{proto: ipproto.ICMPv4, want: transitIPDenied},
|
||||
{proto: ipproto.TSMP, want: transitIPDenied},
|
||||
},
|
||||
},
|
||||
{
|
||||
// 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.
|
||||
// packet by, and reports that rather than a denial, so the
|
||||
// datapath 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},
|
||||
{proto: ipproto.TCP, port: 443, dst: unmappedTransitIP, want: transitIPUnmapped},
|
||||
{proto: ipproto.TCP, port: 80, dst: unmappedTransitIP, want: transitIPUnmapped},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -286,7 +281,8 @@ func TestConnectorIngressAllow(t *testing.T) {
|
||||
name: "different-peer",
|
||||
grant: []tailcfg.RawMessage{`"tcp:443"`},
|
||||
probes: []probe{
|
||||
{proto: ipproto.TCP, port: 80, src: otherPeerIP, want: true},
|
||||
{proto: ipproto.TCP, port: 80, src: otherPeerIP, want: transitIPUnmapped},
|
||||
{proto: ipproto.TCP, port: 443, src: otherPeerIP, want: transitIPUnmapped},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -307,7 +303,7 @@ func TestConnectorIngressAllow(t *testing.T) {
|
||||
resp := c.handleConnectorTransitIPRequest(peerNode, peerCap, ConnectorTransitIPRequest{
|
||||
TransitIPs: []TransitIPRequest{withValidDstToken(t, c.connector, TransitIPRequest{
|
||||
TransitIP: transitIP,
|
||||
DestinationIP: netip.MustParseAddr("203.0.113.1"),
|
||||
DestinationIP: destIP,
|
||||
App: appName,
|
||||
})},
|
||||
})
|
||||
@@ -328,7 +324,7 @@ func TestConnectorIngressAllow(t *testing.T) {
|
||||
if p.proto == ipproto.TCP && flags == 0 {
|
||||
flags = packet.TCPSyn
|
||||
}
|
||||
got, _ := c.connector.ingressAllow(packet.Parsed{
|
||||
gotAddr, got := c.connector.realIPForTransitIPConnection(&packet.Parsed{
|
||||
IPVersion: 4,
|
||||
IPProto: p.proto,
|
||||
TCPFlags: flags,
|
||||
@@ -336,8 +332,18 @@ func TestConnectorIngressAllow(t *testing.T) {
|
||||
Dst: netip.AddrPortFrom(dst, p.port),
|
||||
})
|
||||
if got != p.want {
|
||||
t.Errorf("ingressAllow(%v %v -> %v:%d) = %v, want %v",
|
||||
t.Errorf("realIPForTransitIPConnection(%v %v -> %v:%d) verdict = %v, want %v",
|
||||
p.proto, src, dst, p.port, got, p.want)
|
||||
continue
|
||||
}
|
||||
// Only an allowed packet gets somewhere to go.
|
||||
wantAddr := netip.Addr{}
|
||||
if got == transitIPAllowed {
|
||||
wantAddr = destIP
|
||||
}
|
||||
if gotAddr != wantAddr {
|
||||
t.Errorf("realIPForTransitIPConnection(%v %v -> %v:%d) addr = %v, want %v",
|
||||
p.proto, src, dst, p.port, gotAddr, wantAddr)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user