diff --git a/feature/conn25/client.go b/feature/conn25/client.go index 6a5493964c..b04b65ec4f 100644 --- a/feature/conn25/client.go +++ b/feature/conn25/client.go @@ -69,7 +69,7 @@ func (c *client) transitIPRoutableLocked(as *addrs) bool { func (c *client) linkLocalAllow(p packet.Parsed) (bool, string) { ok := c.isKnownTransitIP(p.Dst.Addr()) if ok { - return true, packetFilterAllowReason + return true, transitIPAllowReason } return false, "" } diff --git a/feature/conn25/conn25.go b/feature/conn25/conn25.go index 7046cb5a49..75c7722385 100644 --- a/feature/conn25/conn25.go +++ b/feature/conn25/conn25.go @@ -252,16 +252,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 datapath can do something with them. 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 @@ -1437,7 +1438,7 @@ func rewriteHTTPSResponse(hdr dnsmessage.Header, question dnsmessage.Question, p return b.Finish() } -const packetFilterAllowReason = "app connector transit IP" +const transitIPAllowReason = "app connector transit IP" func (e *extension) getRouteCheckReport() ipnlocal.RouteCheckReport { b, ok := e.backend.(*ipnlocal.LocalBackend) diff --git a/feature/conn25/connector.go b/feature/conn25/connector.go index dcee25d6b8..7c6f7f690f 100644 --- a/feature/conn25/connector.go +++ b/feature/conn25/connector.go @@ -149,15 +149,36 @@ 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 Dst that is in +// linkLocalAllow returns true if the provided packet has a Dst that is in // the configured transit IP range for this connector, false otherwise. -func (c *connector) packetFilterAllow(p packet.Parsed) (bool, string) { +// +// It only exempts the packet from the filter's blanket drop of link-local +// destinations; it does not accept it. Acceptance is +// [connector.ingressAllow]'s decision, which a packet to a transit IP has to +// get through either way. +func (c *connector) linkLocalAllow(p packet.Parsed) (bool, string) { if c.transitIPInPool(p.Dst.Addr()) { - return true, packetFilterAllowReason + return true, transitIPAllowReason } return false, "" } +// ingressAllow reports whether p, a packet arriving from a peer, may be +// accepted by this connector. It may if it is addressed to an address in a +// configured transit IP pool and carries a protocol the datapath can map. +// +// The protocol check matters because a transit IP is not an address this node +// holds. Admitting a protocol [isSupportedProtocol] excludes would have +// [datapathHandler.HandlePacketFromWireGuard] pass it through untouched, which +// hands the host stack a packet addressed to something it has no route for. +// Only the protocols the datapath can map have anywhere to go. +func (c *connector) ingressAllow(p packet.Parsed) (bool, string) { + if !c.transitIPInPool(p.Dst.Addr()) || !isSupportedProtocol(p.IPProto) { + return false, "" + } + return true, transitIPAllowReason +} + // lookupAddrBySrcIPAndTransitIPLocked returns the real destination IP that the // peer at srcIP registered for transitIP, or (zero, false) if it registered // none. c.mu must be held. diff --git a/feature/conn25/connector_test.go b/feature/conn25/connector_test.go index 320fd7991f..8d2dc424f3 100644 --- a/feature/conn25/connector_test.go +++ b/feature/conn25/connector_test.go @@ -15,6 +15,7 @@ import ( "tailscale.com/tailcfg/peercap" "tailscale.com/tstest" "tailscale.com/types/appctype" + "tailscale.com/types/ipproto" "tailscale.com/types/logger" "tailscale.com/util/must" ) @@ -283,7 +284,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") @@ -300,17 +301,78 @@ 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 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. +func TestConnectorIngressAllow(t *testing.T) { + src := netip.MustParseAddr("100.64.0.1") + knownTip := netip.MustParseAddr("192.0.2.1") + unknownTip := netip.MustParseAddr("100.64.0.42") + + v4TransitIPsBuilder := netipx.IPSetBuilder{} + v4TransitIPsBuilder.AddPrefix(netip.MustParsePrefix("192.0.2.0/24")) + v4TransitIPs := must.Get(v4TransitIPsBuilder.IPSet()) + + c := newConn25(t.Logf) + c.reconfig(&config{ + isConfigured: true, + ipSets: ipSets{ + v4Transit: v4TransitIPs, + }, + }) + + tests := []struct { + name string + proto ipproto.Proto + 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}, + + // 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}, + + // Outside the pool nothing is admitted whatever the protocol. + {"tcp-outside-pool", ipproto.TCP, unknownTip, false}, + {"gre-outside-pool", ipproto.Proto(47), unknownTip, false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(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), + }) + if got != tt.want { + t.Errorf("ingressAllow(%v -> %v) = (%v, %q), want %v", tt.proto, tt.dst, got, why, tt.want) + } + }) + } +} diff --git a/feature/conn25/datapath.go b/feature/conn25/datapath.go index 526ee5803d..9b2fa8a286 100644 --- a/feature/conn25/datapath.go +++ b/feature/conn25/datapath.go @@ -132,6 +132,10 @@ func (dh *datapathHandler) StartFlowExpirySweepers(ctx context.Context) { go dh.connectorFlowTable.StartExpiredSweeper(ctx) } +// isSupportedProtocol reports whether the datapath can map proto between a +// transit IP and a destination. Anything else it passes through untouched, so +// [connector.ingressAllow] refuses it rather than admitting a packet nothing +// downstream will act on. func isSupportedProtocol(proto ipproto.Proto) bool { return proto == ipproto.TCP || proto == ipproto.UDP || proto == ipproto.ICMPv4 || proto == ipproto.ICMPv6 }