diff --git a/hscontrol/policy/policyutil/reduce.go b/hscontrol/policy/policyutil/reduce.go index a42f262bb..107dee8fd 100644 --- a/hscontrol/policy/policyutil/reduce.go +++ b/hscontrol/policy/policyutil/reduce.go @@ -11,11 +11,9 @@ import ( ) // ReduceFilterRules takes a node and a set of global filter rules and removes all rules -// and destinations that are not relevant to that particular node. -// -// nodeAddrs holds every node's addresses. A destination prefix made only of -// them merges several nodes' addresses, and the node keeps just its own, as -// Tailscale lists them; any other prefix stays whole. nil keeps all whole. +// and destinations that are not relevant to that particular node. Rules list +// node addresses one by one, so a node keeps its own and those its routes +// cover, as Tailscale sends them. // // IMPORTANT: This function is designed for global filters only. Per-node filters // (from autogroup:self policies) are already node-specific and should not be passed @@ -24,7 +22,6 @@ import ( func ReduceFilterRules( node types.NodeView, rules []tailcfg.FilterRule, - nodeAddrs *netipx.IPSet, ) []tailcfg.FilterRule { ret := []tailcfg.FilterRule{} nodeIPs := node.IPs() @@ -48,7 +45,7 @@ func ReduceFilterRules( for _, dest := range rule.DstPorts { if destReachesNode(dest.IP, nodeIPs, subnetRoutes, exitRoutes) { - dests = appendOwnDests(dests, dest, nodeIPs, subnetRoutes, exitRoutes, nodeAddrs) + dests = append(dests, dest) } } @@ -91,42 +88,6 @@ func destReachesNode(dst string, nodeIPs []netip.Addr, subnetRoutes, exitRoutes slices.ContainsFunc(exitRoutes, overlaps) } -// appendOwnDests appends dest, which reaches the node, to dests. A prefix -// made only of node addresses, which no route of the node overlaps, becomes -// one entry per node address inside it; anything else stays whole. -func appendOwnDests( - dests []tailcfg.NetPortRange, - dest tailcfg.NetPortRange, - nodeIPs []netip.Addr, - subnetRoutes, exitRoutes []netip.Prefix, - nodeAddrs *netipx.IPSet, -) []tailcfg.NetPortRange { - pfx, err := netip.ParsePrefix(dest.IP) - if err != nil || !destinationNeedsOwnAddressExpansion(pfx, subnetRoutes, exitRoutes, nodeAddrs) { - return append(dests, dest) - } - - for _, ip := range nodeIPs { - if pfx.Contains(ip) { - own := dest - own.IP = ip.String() - dests = append(dests, own) - } - } - - return dests -} - -func destinationNeedsOwnAddressExpansion( - pfx netip.Prefix, - subnetRoutes, exitRoutes []netip.Prefix, - nodeAddrs *netipx.IPSet, -) bool { - return !pfx.IsSingleIP() && nodeAddrs != nil && nodeAddrs.ContainsPrefix(pfx) && - !slices.ContainsFunc(subnetRoutes, pfx.Overlaps) && - !slices.ContainsFunc(exitRoutes, pfx.Overlaps) -} - // reduceCapGrantRule filters a [tailcfg.CapGrant] rule to only include // [tailcfg.CapGrant] entries whose Dsts match the given node's IPs. When a // broad prefix (e.g. 100.64.0.0/10 from dst:*) contains a node's IP, it is diff --git a/hscontrol/policy/policyutil/reduce_test.go b/hscontrol/policy/policyutil/reduce_test.go index 9fde0088b..8e7a3facb 100644 --- a/hscontrol/policy/policyutil/reduce_test.go +++ b/hscontrol/policy/policyutil/reduce_test.go @@ -13,7 +13,6 @@ import ( "github.com/juanfont/headscale/hscontrol/util" "github.com/rs/zerolog/log" "github.com/stretchr/testify/require" - "go4.org/netipx" "tailscale.com/net/tsaddr" "tailscale.com/tailcfg" "tailscale.com/util/must" @@ -211,8 +210,10 @@ func TestReduceFilterRules(t *testing.T) { // Merged: Both ACL rules combined (same SrcIPs) { SrcIPs: []string{ - "100.64.0.1-100.64.0.2", - "fd7a:115c:a1e0::1-fd7a:115c:a1e0::2", + "100.64.0.1", + "100.64.0.2", + "fd7a:115c:a1e0::1", + "fd7a:115c:a1e0::2", }, DstPorts: []tailcfg.NetPortRange{ { @@ -356,8 +357,10 @@ func TestReduceFilterRules(t *testing.T) { // by exit node routing via AllowedIPs, not by packet filtering. { SrcIPs: []string{ - "100.64.0.1-100.64.0.2", - "fd7a:115c:a1e0::1-fd7a:115c:a1e0::2", + "100.64.0.1", + "100.64.0.2", + "fd7a:115c:a1e0::1", + "fd7a:115c:a1e0::2", }, DstPorts: []tailcfg.NetPortRange{ { @@ -462,8 +465,10 @@ func TestReduceFilterRules(t *testing.T) { // Here, 64.0.0.0/2 contains 100.64.0.100 (CGNAT range), so it matches. { SrcIPs: []string{ - "100.64.0.1-100.64.0.2", - "fd7a:115c:a1e0::1-fd7a:115c:a1e0::2", + "100.64.0.1", + "100.64.0.2", + "fd7a:115c:a1e0::1", + "fd7a:115c:a1e0::2", }, DstPorts: []tailcfg.NetPortRange{ { @@ -539,8 +544,10 @@ func TestReduceFilterRules(t *testing.T) { // Merged: Both ACL rules combined (same SrcIPs) { SrcIPs: []string{ - "100.64.0.1-100.64.0.2", - "fd7a:115c:a1e0::1-fd7a:115c:a1e0::2", + "100.64.0.1", + "100.64.0.2", + "fd7a:115c:a1e0::1", + "fd7a:115c:a1e0::2", }, DstPorts: []tailcfg.NetPortRange{ { @@ -623,8 +630,10 @@ func TestReduceFilterRules(t *testing.T) { // Merged: Both ACL rules combined (same SrcIPs) { SrcIPs: []string{ - "100.64.0.1-100.64.0.2", - "fd7a:115c:a1e0::1-fd7a:115c:a1e0::2", + "100.64.0.1", + "100.64.0.2", + "fd7a:115c:a1e0::1", + "fd7a:115c:a1e0::2", }, DstPorts: []tailcfg.NetPortRange{ { @@ -779,7 +788,7 @@ func TestReduceFilterRules(t *testing.T) { got, _ := pm.Filter() t.Logf("full filter:\n%s", must.Get(json.MarshalIndent(got, "", " "))) - got = policyutil.ReduceFilterRules(tt.node.View(), got, nil) + got = policyutil.ReduceFilterRules(tt.node.View(), got) if diff := cmp.Diff(tt.want, got); diff != "" { log.Trace().Interface("got", got).Msg("result") @@ -952,7 +961,7 @@ func TestReduceFilterRulesPartialApproval(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := policyutil.ReduceFilterRules( - tt.node.View(), tt.rules, nil, + tt.node.View(), tt.rules, ) require.Len(t, got, tt.wantCount, "rule count mismatch") @@ -1299,7 +1308,7 @@ func TestReduceFilterRulesCapGrant(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := policyutil.ReduceFilterRules(tt.node.View(), tt.rules, nil) + got := policyutil.ReduceFilterRules(tt.node.View(), tt.rules) require.Len(t, got, len(tt.want), "rule count mismatch") @@ -1361,7 +1370,7 @@ func TestReduceFilterRulesDestForms(t *testing.T) { rule.DstPorts = append(rule.DstPorts, tailcfg.NetPortRange{IP: dst, Ports: tailcfg.PortRangeAny}) } - got := policyutil.ReduceFilterRules(node.View(), []tailcfg.FilterRule{rule}, nil) + got := policyutil.ReduceFilterRules(node.View(), []tailcfg.FilterRule{rule}) require.Len(t, got, 1) kept := make(map[string]bool) @@ -1381,13 +1390,6 @@ func TestReduceFilterRulesDestForms(t *testing.T) { func TestReduceFilterRulesAllocsIndependentOfDests(t *testing.T) { node := (&types.Node{IPv4: ap("100.64.0.1"), IPv6: ap("fd7a:115c:a1e0::1")}).View() - var b netipx.IPSetBuilder - - node.AppendToIPSet(&b) - - nodeAddrs, err := b.IPSet() - require.NoError(t, err) - allocs := func(n int) int { rule := tailcfg.FilterRule{SrcIPs: []string{"*"}} for i := range n { @@ -1399,48 +1401,40 @@ func TestReduceFilterRulesAllocsIndependentOfDests(t *testing.T) { rules := []tailcfg.FilterRule{rule} - return int(testing.AllocsPerRun(10, func() { policyutil.ReduceFilterRules(node, rules, nodeAddrs) })) + return int(testing.AllocsPerRun(10, func() { policyutil.ReduceFilterRules(node, rules) })) } require.Equal(t, allocs(10), allocs(1000), "reducing a filter must not allocate per DstPorts entry") } -// TestReduceFilterRulesMergedNodeDests checks that a destination merged from -// node addresses shrinks to the node's own addresses, while a written prefix, -// and any destination a router serves, stays whole. -func TestReduceFilterRulesMergedNodeDests(t *testing.T) { +// TestReduceFilterRulesKeepsDestsAsWritten checks that a node keeps the +// destinations that reach it exactly as written: its own addresses, not a +// neighbour's, and a written prefix whole; an exit node keeps them all. +func TestReduceFilterRulesKeepsDestsAsWritten(t *testing.T) { node := &types.Node{IPv4: ap("100.64.0.6"), IPv6: ap("fd7a:115c:a1e0::6")} - neighbour := &types.Node{IPv4: ap("100.64.0.7"), IPv6: ap("fd7a:115c:a1e0::7")} - - var b netipx.IPSetBuilder - - node.AppendToIPSet(&b) - neighbour.AppendToIPSet(&b) - - nodeAddrs, err := b.IPSet() - require.NoError(t, err) rules := []tailcfg.FilterRule{{ SrcIPs: []string{"100.64.0.1"}, DstPorts: []tailcfg.NetPortRange{ - {IP: "100.64.0.6/31", Ports: tailcfg.PortRange{First: 22, Last: 22}}, - {IP: "fd7a:115c:a1e0::6/127", Ports: tailcfg.PortRange{First: 22, Last: 22}}, + {IP: "100.64.0.6", Ports: tailcfg.PortRange{First: 22, Last: 22}}, + {IP: "100.64.0.7", Ports: tailcfg.PortRange{First: 22, Last: 22}}, + {IP: "fd7a:115c:a1e0::6", Ports: tailcfg.PortRange{First: 22, Last: 22}}, {IP: "100.64.0.0/12", Ports: tailcfg.PortRangeAny}, }, }} - got := policyutil.ReduceFilterRules(node.View(), rules, nodeAddrs) + got := policyutil.ReduceFilterRules(node.View(), rules) require.Equal(t, []tailcfg.NetPortRange{ {IP: "100.64.0.6", Ports: tailcfg.PortRange{First: 22, Last: 22}}, {IP: "fd7a:115c:a1e0::6", Ports: tailcfg.PortRange{First: 22, Last: 22}}, {IP: "100.64.0.0/12", Ports: tailcfg.PortRangeAny}, }, got[0].DstPorts) - // An exit node forwards to every destination, so it keeps them whole. + // An exit node forwards to every destination, so it keeps them all. node.Hostinfo = &tailcfg.Hostinfo{RoutableIPs: tsaddr.ExitRoutes()} node.ApprovedRoutes = tsaddr.ExitRoutes() - got = policyutil.ReduceFilterRules(node.View(), rules, nodeAddrs) + got = policyutil.ReduceFilterRules(node.View(), rules) require.Equal(t, rules[0].DstPorts, got[0].DstPorts) } diff --git a/hscontrol/policy/v2/compiled.go b/hscontrol/policy/v2/compiled.go index b0ef67f92..c23932885 100644 --- a/hscontrol/policy/v2/compiled.go +++ b/hscontrol/policy/v2/compiled.go @@ -7,14 +7,12 @@ import ( "slices" "github.com/juanfont/headscale/hscontrol/types" - "github.com/juanfont/headscale/hscontrol/util" "github.com/rs/zerolog/log" "go4.org/netipx" "tailscale.com/tailcfg" "tailscale.com/tailcfg/nodecap" "tailscale.com/tailcfg/peercap" "tailscale.com/types/views" - "tailscale.com/util/set" ) // grantCategory classifies a grant by what per-node work it needs. @@ -144,20 +142,6 @@ func nodeHasUserIdentity(node types.NodeView) bool { return !node.IsTagged() && node.User().Valid() } -// nodeAddrSet is the addresses of every node. -func nodeAddrSet(nodes views.Slice[types.NodeView]) *netipx.IPSet { - var b netipx.IPSetBuilder - - for _, n := range nodes.All() { - n.AppendToIPSet(&b) - } - - // The builder only fails on invalid addresses, which nodes never hold. - set, _ := b.IPSet() - - return set -} - // compileNodeAttrs returns the per-node CapMap derived from policy // nodeAttrs plus the tailnet-wide [Policy.RandomizeClientPort] flag. // @@ -368,26 +352,6 @@ func (pol *Policy) compileOneGrant( return cg, nil } -// mergeResolvedSrcs merges every prefix from the resolved sources into a -// single [resolved] address set. -func mergeResolvedSrcs(resolvedSrcs []ResolvedAddresses) (resolved, error) { - var b netipx.IPSetBuilder - - for _, ips := range resolvedSrcs { - if r, ok := ips.(resolved); ok { - b.AddSet(&r.ips) - - continue - } - - for _, pref := range ips.Prefixes() { - b.AddPrefix(pref) - } - } - - return newResolved(&b) -} - // sourceCache holds each source list's resolution for one compile: an ACL // compiles to one grant per destination, all sharing its sources, and // resolving and merging them is most of a wide ACL's compile. @@ -400,11 +364,11 @@ type sourcesKey struct { n int } -// grantSources is a source list resolved per alias, all and without -// wildcards, and its SrcIPs once built. +// grantSources is a source list resolved per alias, and its SrcIPs once +// built. type grantSources struct { - all, nonWild []ResolvedAddresses - err error + all []ResolvedAddresses + err error built bool strs []string @@ -427,7 +391,7 @@ func (c sourceCache) get( } s := &grantSources{} - s.all, s.nonWild, s.err = resolveSources(pol, sources, users, nodes) + s.all, s.err = resolveSources(pol, sources, users, nodes) if c != nil { c[key] = s @@ -441,7 +405,7 @@ func (c sourceCache) get( // and clipped, so a caller appending to it copies. func (s *grantSources) strings(hasWildcard, hasDangerAll bool, nodes views.Slice[types.NodeView]) []string { if !s.built { - s.strs = slices.Clip(buildSrcIPStrings(s.all, s.nonWild, hasWildcard, hasDangerAll, nodes)) + s.strs = slices.Clip(buildSrcIPStrings(s.all, hasWildcard, hasDangerAll, nodes)) s.built = true } @@ -456,30 +420,21 @@ func (pol *Policy) compileOneViaGrant( users types.Users, nodes views.Slice[types.NodeView], ) (*compiledGrant, error) { - resolvedSrcs, _, err := resolveSources( + resolvedSrcs, err := resolveSources( pol, grant.Sources, users, nodes, ) if err != nil { return nil, err } - if len(resolvedSrcs) == 0 { - return nil, nil //nolint:nilnil - } - - // Build merged SrcIPs. - srcResolved, err := mergeResolvedSrcs(resolvedSrcs) - if err != nil { - return nil, err - } - - if srcResolved.Empty() { - return nil, nil //nolint:nilnil - } - hasWildcard := sourcesHaveWildcard(grant.Sources) hasDangerAll := sourcesHaveDangerAll(grant.Sources) + srcIPStrings := buildSrcIPStrings(resolvedSrcs, hasWildcard, hasDangerAll, nodes) + if len(srcIPStrings) == 0 { + return nil, nil //nolint:nilnil + } + resolvedDsts, hasAutoGroupInternet := resolveViaDestinations( pol, users, nodes, grant.Destinations, ) @@ -491,29 +446,32 @@ func (pol *Policy) compileOneViaGrant( resolvedDsts: resolvedDsts, hasAutoGroupInternet: hasAutoGroupInternet, internetProtocols: grant.InternetProtocols, - srcIPStrings: srcIPsWithRoutes( - srcResolved, hasWildcard, hasDangerAll, nodes, - ), + srcIPStrings: srcIPStrings, }, }, nil } -// resolveSources resolves grant sources per-alias, returning the -// resolved addresses and a separate slice of non-wildcard sources. -// This is the canonical source-resolution path. Its output lands in -// [compiledGrant.srcIPStrings] (among other places) and callers on the -// hot path should prefer reading that over calling [Alias.Resolve] again. +// viaRuleKey returns the merge key of a rule of a via grant: Tailscale +// merges a via rule only with via rules through the same tags. +func viaRuleKey(cg *compiledGrant, rule tailcfg.FilterRule) string { + return fmt.Sprintf("via:%v|%s", cg.via.viaTags, filterRuleKey(rule)) +} + +// resolveSources resolves grant sources per alias. This is the canonical +// source-resolution path. Its output lands in [compiledGrant.srcIPStrings] +// (among other places) and callers on the hot path should prefer reading +// that over calling [Alias.Resolve] again. func resolveSources( pol *Policy, sources Aliases, users types.Users, nodes views.Slice[types.NodeView], -) ([]ResolvedAddresses, []ResolvedAddresses, error) { - var all, nonWild []ResolvedAddresses +) ([]ResolvedAddresses, error) { + var all []ResolvedAddresses - for i, src := range sources { + for _, src := range sources { if ag, ok := src.(*AutoGroup); ok && ag.Is(AutoGroupSelf) { - return nil, nil, errSelfInSources + return nil, errSelfInSources } ips, err := src.Resolve(pol, users, nodes) @@ -524,51 +482,41 @@ func resolveSources( if ips != nil { all = append(all, ips) - - if _, isWildcard := sources[i].(Asterix); !isWildcard { - nonWild = append(nonWild, ips) - } } } - return all, nonWild, nil + return all, nil } -// buildSrcIPStrings builds the final SrcIPs string slice from -// resolved sources, preserving non-wildcard IPs alongside wildcard -// CGNAT ranges to match Tailscale SaaS behavior. +// buildSrcIPStrings writes sources as Tailscale does: each alias on its +// own, unioned and sorted as text, so a node address stays next to a +// wildcard range that contains it. A wildcard adds every approved subnet +// route; danger-all is "*". func buildSrcIPStrings( - resolvedSrcs, nonWildcardSrcs []ResolvedAddresses, + resolvedSrcs []ResolvedAddresses, hasWildcard, hasDangerAll bool, nodes views.Slice[types.NodeView], ) []string { - srcResolved, err := mergeResolvedSrcs(resolvedSrcs) - if err != nil || srcResolved.Empty() { + var strs []string + for _, src := range resolvedSrcs { + strs = append(strs, src.Strings()...) + } + + if len(strs) == 0 { return nil } - srcIPStrs := srcIPsWithRoutes( - srcResolved, hasWildcard, hasDangerAll, nodes, - ) - - // When sources include a wildcard (*) alongside explicit - // sources (tags, groups, etc.), Tailscale preserves the - // individual IPs from non-wildcard sources alongside the - // merged CGNAT ranges rather than absorbing them. - if hasWildcard && len(nonWildcardSrcs) > 0 { - seen := set.SetOf(srcIPStrs) - - for _, ips := range nonWildcardSrcs { - for _, s := range ips.Strings() { - if !seen.Contains(s) { - seen.Add(s) - srcIPStrs = append(srcIPStrs, s) - } - } - } + if hasDangerAll { + return []string{"*"} } - return srcIPStrs + if hasWildcard { + strs = append(strs, approvedSubnetRoutes(nodes)...) + } + + slices.Sort(strs) + + return slices.Compact(strs) } type resolvedDestination struct { @@ -792,7 +740,7 @@ func filterRulesForNode( node types.NodeView, userIdx userNodeIndex, ) []tailcfg.FilterRule { - return rulesForNode(grants, node, userIdx, compileViaForNode) + return rulesForNode(grants, node, userIdx, filterSelfRules, compileViaForNode) } // peerRulesForNode returns the rules that decide a node's peers: its @@ -802,7 +750,7 @@ func peerRulesForNode( node types.NodeView, userIdx userNodeIndex, ) []tailcfg.FilterRule { - return rulesForNode(grants, node, userIdx, viaPeerRule) + return rulesForNode(grants, node, userIdx, compileAutogroupSelf, viaPeerRule) } // nodeOnlyRules returns the self and via rules a node adds to the @@ -834,9 +782,13 @@ func rulesForNode( grants []compiledGrant, node types.NodeView, userIdx userNodeIndex, + selfRules func(*compiledGrant, types.NodeView, userNodeIndex) []tailcfg.FilterRule, viaRules func(*compiledGrant, types.NodeView) []tailcfg.FilterRule, ) []tailcfg.FilterRule { - var rules []tailcfg.FilterRule + var ( + rules []tailcfg.FilterRule + keys []string + ) for i := range grants { cg := &grants[i] @@ -844,26 +796,26 @@ func rulesForNode( // Pre-compiled rules apply to all grant categories // (empty for via-only grants). rules = append(rules, cg.rules...) + keys = append(keys, ruleKeys(cg.rules)...) switch cg.category { case grantCategoryRegular: // Nothing more to do. case grantCategorySelf: - rules = append( - rules, - compileAutogroupSelf(cg, node, userIdx)..., - ) + self := selfRules(cg, node, userIdx) + rules = append(rules, self...) + keys = append(keys, ruleKeys(self)...) case grantCategoryVia: - rules = append( - rules, - viaRules(cg, node)..., - ) + for _, rule := range viaRules(cg, node) { + rules = append(rules, rule) + keys = append(keys, viaRuleKey(cg, rule)) + } } } - return mergeFilterRules(rules) + return mergeKeyedRules(rules, keys) } // compileAutogroupSelf produces filter rules for autogroup:self @@ -881,36 +833,27 @@ func compileAutogroupSelf( return compileSelfForUser(cg, userIdx[node.User().ID()]) } -// exitNodeSelfRules returns the autogroup:self rules of every user -// other than the node's own for an exit node, whose exit routes contain -// every self destination. Only the packet filter needs them: they never -// make the exit node a peer, and expanding every user for peer matching -// would cost O(users) per exit node on each peer map build. -func exitNodeSelfRules( - grants []compiledGrant, +// filterSelfRules returns the autogroup:self rules a node's packet filter +// holds: its own user's, or, on an exit node, whose exit routes contain +// every self destination, every user's in user order, as Tailscale sends +// them. Only the packet filter needs other users' rules: they never make +// the exit node a peer, so the peer paths use [compileAutogroupSelf]. +func filterSelfRules( + cg *compiledGrant, node types.NodeView, userIdx userNodeIndex, ) []tailcfg.FilterRule { if !node.IsExitNode() { + return compileAutogroupSelf(cg, node, userIdx) + } + + if cg.self == nil { return nil } var rules []tailcfg.FilterRule - - for i := range grants { - cg := &grants[i] - if cg.self == nil { - continue - } - - for _, uid := range slices.Sorted(maps.Keys(userIdx)) { - // compileAutogroupSelf already covers an untagged node's own user. - if nodeHasUserIdentity(node) && node.User().ID() == uid { - continue - } - - rules = append(rules, compileSelfForUser(cg, userIdx[uid])...) - } + for _, uid := range slices.Sorted(maps.Keys(userIdx)) { + rules = append(rules, compileSelfForUser(cg, userIdx[uid])...) } return rules @@ -928,11 +871,10 @@ func compileSelfForUser( var rules []tailcfg.FilterRule - // Filter sources to only same-user untagged devices. - srcResolved := filterSourcesToSameUser( - cg.self.resolvedSrcs, sameUserNodes, - ) - if srcResolved == nil || srcResolved.Empty() { + // Tailscale writes autogroup:self once per source alias, each with the + // same-user devices that alias names. + perSrc, srcResolved := sameUserSources(cg.self.resolvedSrcs, sameUserNodes) + if len(perSrc) == 0 { return nil } @@ -955,11 +897,13 @@ func compileSelfForUser( } if len(destPorts) > 0 { - rules = append(rules, tailcfg.FilterRule{ - SrcIPs: srcResolved.Strings(), - DstPorts: destPorts, - IPProto: ipp.Protocol.toIANAProtocolNumbers(), - }) + for _, src := range perSrc { + rules = append(rules, tailcfg.FilterRule{ + SrcIPs: src.Strings(), + DstPorts: destPorts, + IPProto: ipp.Protocol.toIANAProtocolNumbers(), + }) + } } } @@ -1008,29 +952,44 @@ func compileSelfForUser( return rules } -// filterSourcesToSameUser intersects resolved source addresses with -// same-user untagged device IPs, returning only the addresses that -// belong to those devices. -func filterSourcesToSameUser( +// sameUserSources returns, per source alias, the same-user untagged +// devices it names, and all of them together. +func sameUserSources( resolvedSrcs []ResolvedAddresses, sameUserNodes []types.NodeView, -) ResolvedAddresses { - var srcIPs netipx.IPSetBuilder +) ([]ResolvedAddresses, ResolvedAddresses) { + var ( + perSrc []ResolvedAddresses + all netipx.IPSetBuilder + ) for _, ips := range resolvedSrcs { + var named netipx.IPSetBuilder + for _, n := range sameUserNodes { if slices.ContainsFunc(n.IPs(), ips.Contains) { - n.AppendToIPSet(&srcIPs) + n.AppendToIPSet(&named) + n.AppendToIPSet(&all) } } + + set, err := named.IPSet() + if err != nil || len(set.Ranges()) == 0 { + continue + } + + src, _ := newNodeAddresses(set, nil) + perSrc = append(perSrc, src) } - srcResolved, err := newResolved(&srcIPs) + set, err := all.IPSet() if err != nil { - return nil + return nil, nil } - return srcResolved + union, _ := newNodeAddresses(set, nil) + + return perSrc, union } // viaDstsForNode returns the destinations node serves for a via grant: none @@ -1039,9 +998,9 @@ func filterSourcesToSameUser( func viaDstsForNode( cg *compiledGrant, node types.NodeView, -) []netip.Prefix { +) ([]netip.Prefix, bool) { if cg.via == nil { - return nil + return nil, false } // Check if node matches any via tag. @@ -1056,7 +1015,7 @@ func viaDstsForNode( } if !matchesVia { - return nil + return nil, false } // [types.NodeView.SubnetRoutes] excludes exit routes, so the overlap @@ -1087,14 +1046,7 @@ func viaDstsForNode( // to the grant source via [matcher.Match.DestsIsTheInternet]. // [policyutil.ReduceFilterRules] strips the rule from the wire format // on non-exit advertisers, preserving SaaS PacketFilter encoding. - if cg.via.hasAutoGroupInternet && node.IsExitNode() { - viaDstPrefixes = append( - viaDstPrefixes, - util.TheInternet().Prefixes()..., - ) - } - - return viaDstPrefixes + return viaDstPrefixes, cg.via.hasAutoGroupInternet && node.IsExitNode() } // compileViaForNode produces via-grant filter rules for a specific @@ -1104,23 +1056,32 @@ func compileViaForNode( cg *compiledGrant, node types.NodeView, ) []tailcfg.FilterRule { - viaDstPrefixes := viaDstsForNode(cg, node) - if len(viaDstPrefixes) == 0 { + viaDstPrefixes, internet := viaDstsForNode(cg, node) + if len(viaDstPrefixes) == 0 && !internet { return nil } + dsts := make([]string, 0, len(viaDstPrefixes)) + for _, prefix := range viaDstPrefixes { + dsts = append(dsts, filterIP(prefix)) + } + + if internet { + dsts = append(dsts, internetStrings()...) + } + // Build rules using pre-resolved srcIPStrings. var rules []tailcfg.FilterRule for _, ipp := range cg.via.internetProtocols { var destPorts []tailcfg.NetPortRange - for _, prefix := range viaDstPrefixes { + for _, ip := range dsts { for _, port := range ipp.Ports { destPorts = append( destPorts, tailcfg.NetPortRange{ - IP: filterIP(prefix), + IP: ip, Ports: port, }, ) @@ -1150,7 +1111,7 @@ func viaPeerRule( cg *compiledGrant, node types.NodeView, ) []tailcfg.FilterRule { - if len(viaDstsForNode(cg, node)) == 0 { + if prefixes, internet := viaDstsForNode(cg, node); len(prefixes) == 0 && !internet { return nil } diff --git a/hscontrol/policy/v2/compiled_test.go b/hscontrol/policy/v2/compiled_test.go index f26fc1c72..f9f914eed 100644 --- a/hscontrol/policy/v2/compiled_test.go +++ b/hscontrol/policy/v2/compiled_test.go @@ -205,10 +205,10 @@ func assertCompileOtherDestsReference( ) { t.Helper() - srcs, nonWild, err := resolveSources(pol, grant.Sources, users, nodes.ViewSlice()) + srcs, err := resolveSources(pol, grant.Sources, users, nodes.ViewSlice()) require.NoError(t, err) - srcStrings := buildSrcIPStrings(srcs, nonWild, + srcStrings := buildSrcIPStrings(srcs, sourcesHaveWildcard(grant.Sources), sourcesHaveDangerAll(grant.Sources), nodes.ViewSlice()) if grant.Sources != nil && len(grant.Sources) == 0 { @@ -446,10 +446,10 @@ func (dst *countedDestination) Resolve( func TestCompileOtherDestsResolvesOnce(t *testing.T) { pol, users, nodes := compileOtherDestsFixture(t, 4, 24) grant := pol.Grants[0] - srcs, nonWild, err := resolveSources(pol, grant.Sources, users, nodes.ViewSlice()) + srcs, err := resolveSources(pol, grant.Sources, users, nodes.ViewSlice()) require.NoError(t, err) - srcStrings := buildSrcIPStrings(srcs, nonWild, true, false, nodes.ViewSlice()) + srcStrings := buildSrcIPStrings(srcs, true, false, nodes.ViewSlice()) dst := &countedDestination{Alias: gp("group:all")} grant.Destinations = Aliases{dst} @@ -501,7 +501,7 @@ func BenchmarkCompileOtherDests(b *testing.B) { grant.InternetProtocols, grant.App = grant.InternetProtocols[:1], nil } - srcs, nonWild, err := resolveSources( + srcs, err := resolveSources( pol, grant.Sources, users, @@ -511,7 +511,6 @@ func BenchmarkCompileOtherDests(b *testing.B) { srcStrings := buildSrcIPStrings( srcs, - nonWild, true, false, nodes.ViewSlice(), diff --git a/hscontrol/policy/v2/filter.go b/hscontrol/policy/v2/filter.go index 9393781bd..3ec9c7169 100644 --- a/hscontrol/policy/v2/filter.go +++ b/hscontrol/policy/v2/filter.go @@ -105,27 +105,6 @@ func sourcesHaveDangerAll(srcs Aliases) bool { return false } -// srcIPsWithRoutes returns the [tailcfg.FilterRule.SrcIPs] string slice, -// appending approved subnet routes when the sources include a wildcard. -// When hasDangerAll is true, returns ["*"] to represent all IPs. -func srcIPsWithRoutes( - resolved ResolvedAddresses, - hasWildcard bool, - hasDangerAll bool, - nodes views.Slice[types.NodeView], -) []string { - if hasDangerAll { - return []string{"*"} - } - - ips := resolved.Strings() - if hasWildcard { - ips = append(ips, approvedSubnetRoutes(nodes)...) - } - - return ips -} - // compileFilterRules takes a set of nodes and a [Policy] and generates a // set of Tailscale compatible [tailcfg.FilterRule]s used to allow traffic // on clients. @@ -188,12 +167,10 @@ func (pol *Policy) destinationsToNetPortRange( continue } - prefixes := ips.Prefixes() - - for _, pref := range prefixes { + for _, ip := range ips.Strings() { for _, port := range ports { ret = append(ret, tailcfg.NetPortRange{ - IP: filterIP(pref), + IP: ip, Ports: port, }) } @@ -226,10 +203,7 @@ func (pol *Policy) compileFilterRulesForNode( grants := pol.compileGrants(users, nodes) userIdx := buildUserNodeIndex(nodes) - return append( - filterRulesForNode(grants, node, userIdx), - exitNodeSelfRules(grants, node, userIdx)..., - ) + return filterRulesForNode(grants, node, userIdx) } var sshAccept = tailcfg.SSHAction{ @@ -746,6 +720,45 @@ func filterRuleKey(rule tailcfg.FilterRule) string { return fmt.Sprintf("%s|%v", strings.Join(rule.SrcIPs, ","), rule.IPProto) } +// ruleKeys returns each rule's merge key, see [filterRuleKey]. Rules +// sharing a SrcIPs slice, as an ACL's rules do, build its key once. +// [tailcfg.CapGrant] rules get no key: they are never merged. +func ruleKeys(rules []tailcfg.FilterRule) []string { + // sharedSrcIPs identifies a SrcIPs slice by its backing array. + type sharedSrcIPs struct { + first *string + n int + ipProto string + } + + built := make(map[sharedSrcIPs]string) + keys := make([]string, len(rules)) + + for i, rule := range rules { + if len(rule.CapGrant) > 0 { + continue + } + + shared := sharedSrcIPs{ + n: len(rule.SrcIPs), + ipProto: fmt.Sprint(rule.IPProto), + } + if len(rule.SrcIPs) > 0 { + shared.first = &rule.SrcIPs[0] + } + + key, ok := built[shared] + if !ok { + key = filterRuleKey(rule) + built[shared] = key + } + + keys[i] = key + } + + return keys +} + // mergeFilterRules merges rules with identical [tailcfg.FilterRule.SrcIPs] and // [tailcfg.FilterRule.IPProto] by combining their [tailcfg.FilterRule.DstPorts]. // DstPorts are NOT deduplicated to match Tailscale behavior. @@ -758,61 +771,39 @@ func mergeFilterRules(rules []tailcfg.FilterRule) []tailcfg.FilterRule { return rules } - // sharedSrcIPs identifies a SrcIPs slice by its backing array. - type sharedSrcIPs struct { - first *string - n int - ipProto string + return mergeKeyedRules(rules, ruleKeys(rules)) +} + +// mergeKeyedRules merges rules whose keys are equal into the first of them, +// keeping first-appearance order, as Tailscale does. keys[i] is rules[i]'s +// key; [tailcfg.CapGrant] rules pass through. +func mergeKeyedRules(rules []tailcfg.FilterRule, keys []string) []tailcfg.FilterRule { + if len(rules) <= 1 { + return rules } keyToIdx := make(map[string]int) - sharedToIdx := make(map[sharedSrcIPs]int) result := make([]tailcfg.FilterRule, 0, len(rules)) - for _, rule := range rules { - // CapGrant rules are not merged — they are structurally - // different from DstPorts rules and passed through as-is. + for i, rule := range rules { if len(rule.CapGrant) > 0 { result = append(result, rule) continue } - // An ACL compiles to one rule per destination, all with the same - // SrcIPs slice. Build the key of a slice once, or merging them is - // len(src) × len(dst) work. - shared := sharedSrcIPs{ - n: len(rule.SrcIPs), - ipProto: fmt.Sprint(rule.IPProto), - } - if len(rule.SrcIPs) > 0 { - shared.first = &rule.SrcIPs[0] - } - - idx, exists := sharedToIdx[shared] - if !exists { - key := filterRuleKey(rule) - - idx, exists = keyToIdx[key] - if !exists { - idx = len(result) - keyToIdx[key] = idx - } - - sharedToIdx[shared] = idx - } - - if exists { - // Merge: append DstPorts to existing rule + if idx, ok := keyToIdx[keys[i]]; ok { result[idx].DstPorts = append(result[idx].DstPorts, rule.DstPorts...) - } else { - // New unique combination - result = append(result, tailcfg.FilterRule{ - SrcIPs: rule.SrcIPs, - DstPorts: slices.Clone(rule.DstPorts), - IPProto: rule.IPProto, - }) + + continue } + + keyToIdx[keys[i]] = len(result) + result = append(result, tailcfg.FilterRule{ + SrcIPs: rule.SrcIPs, + DstPorts: slices.Clone(rule.DstPorts), + IPProto: rule.IPProto, + }) } return result diff --git a/hscontrol/policy/v2/filter_test.go b/hscontrol/policy/v2/filter_test.go index 2880dff7d..0fcf5cb67 100644 --- a/hscontrol/policy/v2/filter_test.go +++ b/hscontrol/policy/v2/filter_test.go @@ -153,21 +153,21 @@ func TestParsing(t *testing.T) { }`, want: []tailcfg.FilterRule{ { - SrcIPs: []string{"100.64.0.0-100.115.91.255", "100.115.94.0-100.127.255.255", "fd7a:115c:a1e0::/48"}, + SrcIPs: []string{"100.115.94.0-100.127.255.255", "100.64.0.0-100.115.91.255", "fd7a:115c:a1e0::/48"}, DstPorts: []tailcfg.NetPortRange{ {IP: "100.100.100.100", Ports: tailcfg.PortRangeAny}, }, IPProto: []int{ProtocolTCP}, }, { - SrcIPs: []string{"100.64.0.0-100.115.91.255", "100.115.94.0-100.127.255.255", "fd7a:115c:a1e0::/48"}, + SrcIPs: []string{"100.115.94.0-100.127.255.255", "100.64.0.0-100.115.91.255", "fd7a:115c:a1e0::/48"}, DstPorts: []tailcfg.NetPortRange{ {IP: "100.100.100.100", Ports: tailcfg.PortRange{First: 53, Last: 53}}, }, IPProto: []int{ProtocolUDP}, }, { - SrcIPs: []string{"100.64.0.0-100.115.91.255", "100.115.94.0-100.127.255.255", "fd7a:115c:a1e0::/48"}, + SrcIPs: []string{"100.115.94.0-100.127.255.255", "100.64.0.0-100.115.91.255", "fd7a:115c:a1e0::/48"}, DstPorts: []tailcfg.NetPortRange{ {IP: "100.100.100.100", Ports: tailcfg.PortRangeAny}, }, @@ -202,7 +202,7 @@ func TestParsing(t *testing.T) { `, want: []tailcfg.FilterRule{ { - SrcIPs: []string{"100.64.0.0-100.115.91.255", "100.115.94.0-100.127.255.255", "fd7a:115c:a1e0::/48"}, + SrcIPs: []string{"100.115.94.0-100.127.255.255", "100.64.0.0-100.115.91.255", "fd7a:115c:a1e0::/48"}, DstPorts: []tailcfg.NetPortRange{ {IP: "100.100.100.100", Ports: tailcfg.PortRangeAny}, }, @@ -343,7 +343,7 @@ func TestParsing(t *testing.T) { `, want: []tailcfg.FilterRule{ { - SrcIPs: []string{"100.64.0.0-100.115.91.255", "100.115.94.0-100.127.255.255", "fd7a:115c:a1e0::/48"}, + SrcIPs: []string{"100.115.94.0-100.127.255.255", "100.64.0.0-100.115.91.255", "fd7a:115c:a1e0::/48"}, DstPorts: []tailcfg.NetPortRange{ {IP: "100.100.100.100", Ports: tailcfg.PortRangeAny}, }, @@ -1497,7 +1497,7 @@ func TestTagUserMutualExclusivity(t *testing.T) { compiled := pol.compileFilterRulesForNode(users, userNode, nodes.ViewSlice()) - userRules := policyutil.ReduceFilterRules(userNode, compiled, nodeAddrSet(nodes.ViewSlice())) + userRules := policyutil.ReduceFilterRules(userNode, compiled) for _, rule := range userRules { for _, dst := range rule.DstPorts { @@ -1519,7 +1519,7 @@ func TestTagUserMutualExclusivity(t *testing.T) { compiled = pol.compileFilterRulesForNode(users, dbNode, nodes.ViewSlice()) - dbRules := policyutil.ReduceFilterRules(dbNode, compiled, nodeAddrSet(nodes.ViewSlice())) + dbRules := policyutil.ReduceFilterRules(dbNode, compiled) foundServerSrc := false @@ -1590,7 +1590,7 @@ func TestUserToTagCrossIdentityGrant(t *testing.T) { compiled := pol.compileFilterRulesForNode(users, taggedNode, nodes.ViewSlice()) - rules := policyutil.ReduceFilterRules(taggedNode, compiled, nodeAddrSet(nodes.ViewSlice())) + rules := policyutil.ReduceFilterRules(taggedNode, compiled) // user1's IP should appear as a source that can reach tag:server. foundUser1Src := false @@ -3513,7 +3513,7 @@ func TestSourcesHaveDangerAll(t *testing.T) { } } -func TestSrcIPsWithRoutes(t *testing.T) { +func TestBuildSrcIPStrings(t *testing.T) { t.Parallel() // Build a resolved address set for a single IP. @@ -3586,7 +3586,7 @@ func TestSrcIPsWithRoutes(t *testing.T) { nodes = nodeWithRoutes } - got := srcIPsWithRoutes(tt.resolved, tt.hasWildcard, tt.hasDangerAll, nodes) + got := buildSrcIPStrings([]ResolvedAddresses{tt.resolved}, tt.hasWildcard, tt.hasDangerAll, nodes) if tt.hasDangerAll { assert.Equal(t, []string{"*"}, got) @@ -3598,6 +3598,26 @@ func TestSrcIPsWithRoutes(t *testing.T) { } }) } + + // Each alias is written on its own: node addresses stay listed one by + // one next to the wildcard range that contains them, sorted as text. + var pair netipx.IPSetBuilder + pair.AddPrefix(netip.MustParsePrefix("100.64.0.2/31")) + + pairSet, err := pair.IPSet() + require.NoError(t, err) + + nodeAddrs, err := newNodeAddresses(pairSet, nil) + require.NoError(t, err) + + wildcard, err := Asterix(0).Resolve(nil, nil, emptyNodes) + require.NoError(t, err) + + want := append(wildcard.Strings(), "100.64.0.2", "100.64.0.3") + slices.Sort(want) + + got := buildSrcIPStrings([]ResolvedAddresses{wildcard, nodeAddrs}, true, false, emptyNodes) + assert.Equal(t, want, got, "node addresses must stay listed beside the wildcard range") } func TestFilterAllowAllFix(t *testing.T) { @@ -3771,15 +3791,13 @@ func TestCompileViaGrant(t *testing.T) { } // Expected rule for autogroup:internet on a via-tagged exit - // advertiser: SrcIPs scoped to the grant source, DstPorts - // enumerating util.TheInternet() prefixes. - internetDstPorts := make( - []tailcfg.NetPortRange, 0, len(util.TheInternet().Prefixes()), - ) + // advertiser: SrcIPs scoped to the grant source, DstPorts the + // internet's address ranges, as Tailscale writes them. + internetDstPorts := make([]tailcfg.NetPortRange, 0, len(internetStrings())) - for _, p := range util.TheInternet().Prefixes() { + for _, r := range internetStrings() { internetDstPorts = append(internetDstPorts, tailcfg.NetPortRange{ - IP: p.String(), + IP: r, Ports: tailcfg.PortRangeAny, }) } @@ -4262,14 +4280,12 @@ func TestCompileGrantWithAutogroupSelf_GrantPaths(t *testing.T) { switch tt.name { case "autogroup:self destination for untagged node produces same-user devices": // Should produce rules; sources and destinations should only - // include user1's untagged devices (node1 and node2). - // IPs are merged into ranges by IPSet (e.g. "100.64.0.1-100.64.0.2"). + // include user1's untagged devices (node1 and node2), each + // address on its own, as Tailscale writes them. require.NotEmpty(t, got, "expected rules for autogroup:self") rule := got[0] - // SrcIPs from IPSet may be a merged range. - require.Len(t, rule.SrcIPs, 1) - assert.Equal(t, "100.64.0.1-100.64.0.2", rule.SrcIPs[0], - "SrcIPs should contain merged range for user1 untagged devices") + assert.Equal(t, []string{"100.64.0.1", "100.64.0.2"}, rule.SrcIPs, + "SrcIPs should list user1's untagged devices") var destIPs []string for _, dp := range rule.DstPorts { @@ -4326,10 +4342,9 @@ func TestDestinationsToNetPortRange_AutogroupInternet(t *testing.T) { // autogroup:internet must surface as DstPorts (not be skipped at // compile time). The matcher derived from these FilterRules is // what makes Node.CanAccess return true for exit-node peers via - // DestsIsTheInternet (#3212). The wire format is currently the - // canonical CIDR breakdown of util.TheInternet(); aligning it to - // the SaaS range form is tracked separately. - internetPrefixCount := len(util.TheInternet().Prefixes()) + // DestsIsTheInternet (#3212). They are written as the internet's + // address ranges, as Tailscale writes them. + internetPrefixCount := len(internetStrings()) tests := []struct { name string diff --git a/hscontrol/policy/v2/policy.go b/hscontrol/policy/v2/policy.go index b282ad58b..9af5f6613 100644 --- a/hscontrol/policy/v2/policy.go +++ b/hscontrol/policy/v2/policy.go @@ -44,9 +44,6 @@ type PolicyManager struct { nodes views.Slice[types.NodeView] // nodesByID indexes nodes; see [PolicyManager.cacheableLocked]. nodesByID map[types.NodeID]types.NodeView - // nodeAddrs is every node's addresses, for - // [policyutil.ReduceFilterRules]. - nodeAddrs *netipx.IPSet // resolvedPol is pol with its tags indexed and its groups resolved // against users and nodes, see [Policy.withNodesByTag] and @@ -335,7 +332,6 @@ func (pm *PolicyManager) updateLocked() (bool, error) { pm.resolvedPol = pol pm.compiledGrants = grants pm.userNodeIdx = buildUserNodeIndex(pm.nodes) - pm.nodeAddrs = nodeAddrSet(pm.nodes) pm.needsPerNodeFilter = hasPerNodeGrants(pm.compiledGrants) pm.viaTargetTags = collectViaTargetTags(pm.compiledGrants) pm.relayTargetIPs = relayTargetIPs @@ -1056,13 +1052,10 @@ func (pm *PolicyManager) filterForNodeLocked( if !pm.needsPerNodeFilter { unreduced = pm.filter } else { - unreduced = append( - pm.filterRulesForNodeLocked(node), - exitNodeSelfRules(pm.compiledGrants, node, pm.userNodeIdx)..., - ) + unreduced = pm.filterRulesForNodeLocked(node) } - reduced := policyutil.ReduceFilterRules(node, unreduced, pm.nodeAddrs) + reduced := policyutil.ReduceFilterRules(node, unreduced) if pm.cacheableLocked(node) { pm.filterRulesMap.Store(node.ID(), reduced) } diff --git a/hscontrol/policy/v2/tailscale_acl_data_compat_test.go b/hscontrol/policy/v2/tailscale_acl_data_compat_test.go index 3c628ef2a..1dee2baad 100644 --- a/hscontrol/policy/v2/tailscale_acl_data_compat_test.go +++ b/hscontrol/policy/v2/tailscale_acl_data_compat_test.go @@ -19,7 +19,6 @@ import ( "fmt" "net/netip" "path/filepath" - "slices" "strings" "testing" @@ -28,10 +27,7 @@ import ( "github.com/juanfont/headscale/hscontrol/policy/policyutil" "github.com/juanfont/headscale/hscontrol/types" "github.com/juanfont/headscale/hscontrol/types/testcapture" - "github.com/juanfont/headscale/hscontrol/util" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go4.org/netipx" "tailscale.com/tailcfg" ) @@ -208,90 +204,11 @@ func loadACLTestFile(t *testing.T, path string) *testcapture.Capture { // aclSkipReasons lists checks where headscale differs from SaaS, keyed // [/]/. var aclSkipReasons = map[string]string{ - "acl-au06/beedrill/filter": srcAddressForm, - "acl-k03/beedrill/filter": srcAddressForm, - "acl-k03/bulbasaur/filter": srcAddressForm, - "acl-k03/charmander/filter": srcAddressForm, - "acl-k03/ivysaur/filter": srcAddressForm, - "acl-k03/kakuna/filter": srcAddressForm, - "acl-k03/squirtle/filter": srcAddressForm, - "acl-k03/venusaur/filter": srcAddressForm, - "acl-k03/weedle/filter": srcAddressForm, - "acl-mr09/beedrill/filter": srcAddressForm, - "acl-mr20/beedrill/filter": srcAddressForm, - "acl-mu07/beedrill/filter": srcAddressForm, - "acl-mu07/bulbasaur/filter": srcAddressForm, - "acl-mu07/charmander/filter": srcAddressForm, - "acl-mu07/ivysaur/filter": srcAddressForm, - "acl-mu07/kakuna/filter": srcAddressForm, - "acl-mu07/squirtle/filter": srcAddressForm, - "acl-mu07/venusaur/filter": srcAddressForm, - "acl-mu07/weedle/filter": srcAddressForm, - - "acl-self-plus-dsts/kakuna/filter": srcAddressForm, - "acl-self-plus-dsts/beedrill/filter": srcAddressForm, - "acl-self-plus-dsts/bulbasaur/filter": srcAndSelfDstForm, - "routes-exit-family-ipv4/refused": aclDefaultRouteAccepted, "routes-exit-family-ipv6/refused": aclDefaultRouteAccepted, } -const ( - srcAddressForm = "FILTER_ADDRESS_FORM: SaaS lists contiguous source addresses one by one, headscale as a range" - srcAndSelfDstForm = "FILTER_ADDRESS_FORM+SELF_DST_REPEAT: sources listed one by one, the autogroup:self destination written once per source alias" - aclDefaultRouteAccepted = "ACL_DEFAULT_ROUTE_ACCEPTED: headscale accepts literal 0.0.0.0/0 and ::/0 ACL destinations; SaaS rejects them and requires * or autogroup:internet" -) - -// sourcesAsRanges rewrites each rule's sources as the ranges they cover, so -// that one list of addresses and one range of them compare equal. -func sourcesAsRanges(t *testing.T, rules []tailcfg.FilterRule) []tailcfg.FilterRule { - t.Helper() - - out := make([]tailcfg.FilterRule, len(rules)) - - for i, rule := range rules { - var builder netipx.IPSetBuilder - - for _, src := range rule.SrcIPs { - set, err := util.ParseIPSet(src, nil) - require.NoError(t, err) - - builder.AddSet(set) - } - - set, err := builder.IPSet() - require.NoError(t, err) - - rule.SrcIPs = nil - for _, r := range set.Ranges() { - rule.SrcIPs = append(rule.SrcIPs, r.String()) - } - - out[i] = rule - } - - return out -} - -// withoutRepeatedDsts drops each rule's repeated destinations, keeping the -// first of each. -func withoutRepeatedDsts(rules []tailcfg.FilterRule) []tailcfg.FilterRule { - out := make([]tailcfg.FilterRule, len(rules)) - - for i, rule := range rules { - seen := map[string]bool{} - rule.DstPorts = slices.DeleteFunc(slices.Clone(rule.DstPorts), func(dst tailcfg.NetPortRange) bool { - key := dst.IP + ":" + dst.Ports.String() - repeat := seen[key] - seen[key] = true - - return repeat - }) - out[i] = rule - } - - return out -} +const aclDefaultRouteAccepted = "ACL_DEFAULT_ROUTE_ACCEPTED: headscale accepts literal 0.0.0.0/0 and ::/0 ACL destinations; SaaS rejects them and requires * or autogroup:internet" // TestACLCompat is a data-driven test that loads all ACL-*.json test files // and compares headscale's ACL engine output against the expected behavior. @@ -441,29 +358,9 @@ func testACLSuccess( return policyutil.ReduceFilterRules( node.View(), pol.compileFilterRulesForNode(users, node.View(), nodes.ViewSlice()), - nodeAddrSet(nodes.ViewSlice()), ) } - // How sources and their repeats are written is all this skip may hide. - if reason := aclSkipReasons[key]; reason == srcAddressForm || reason == srcAndSelfDstForm { - pmRules, err := pm.FilterForNode(node.View()) - require.NoError(t, err) - - want := sourcesAsRanges(t, wantRules) - if reason == srcAndSelfDstForm { - want = withoutRepeatedDsts(want) - } - - assert.Empty(t, cmp.Diff(want, sourcesAsRanges(t, pmRules), opts...), - "manager filter differs in more than how it is written") - - if compiles { - assert.Empty(t, cmp.Diff(want, sourcesAsRanges(t, compiled()), opts...), - "compiled filter differs in more than how it is written") - } - } - compatCheck(t, aclSkipReasons, key, func() string { var diffs []string diff --git a/hscontrol/policy/v2/tailscale_grants_compat_test.go b/hscontrol/policy/v2/tailscale_grants_compat_test.go index bd32464e7..33bbf98cc 100644 --- a/hscontrol/policy/v2/tailscale_grants_compat_test.go +++ b/hscontrol/policy/v2/tailscale_grants_compat_test.go @@ -139,47 +139,10 @@ func loadGrantTestFile(t *testing.T, path string) *testcapture.Capture { return c } -// Skip categories document WHY tests are expected to differ from Tailscale SaaS. -// Tests are grouped by root cause. -var grantSkipReasons = map[string]string{ - // SaaS lists contiguous source addresses one by one and writes - // autogroup:internet as address ranges; headscale merges sources into - // ranges and writes the internet as prefixes. The address sets match; - // TestViaGrantMapCompat compares these captures' filters by address. - "via-grant-v52": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v53": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v54": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v55": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v56": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v57": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v58": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v59": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v60": "FILTER_ADDRESS_FORM: same addresses, different wire form and rule order", - "via-grant-v61": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v62": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v93": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v94": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v95": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v96": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v97": "FILTER_ADDRESS_FORM: same addresses, different wire form", - "via-grant-v98": "FILTER_ADDRESS_FORM: same addresses, different wire form", - - // Checks keyed //filter: a listed check that matches - // fails, so a fix must delete its entry. Contiguous pinned addresses; - // TestViaGrantHACompat compares these filters by address. - "via-grant-v64/client-a/filter": srcAddressForm, - "via-grant-v64/client-b/filter": srcAddressForm, - "via-grant-v64/plain/filter": srcAddressForm, - "via-grant-v64/srv/filter": srcAddressForm, - "via-grant-v64/via-empty/filter": srcAddressForm, - "via-grant-v64/via-r/filter": srcAddressForm, - "via-grant-v65/client-a/filter": srcAddressForm, - "via-grant-v65/client-b/filter": srcAddressForm, - "via-grant-v65/plain/filter": srcAddressForm, - "via-grant-v65/srv/filter": srcAddressForm, - "via-grant-v65/via-empty/filter": srcAddressForm, - "via-grant-v65/via-r/filter": srcAddressForm, -} +// grantSkipReasons lists checks where headscale differs from SaaS, keyed +// [//]. A listed check that matches fails, so a fix +// must delete its entry. +var grantSkipReasons = map[string]string{} // TestGrantsCompat is a data-driven test that loads all GRANT-*.json // test files captured from Tailscale SaaS and compares headscale's grants @@ -275,7 +238,6 @@ func testGrantSuccess( gotRules := policyutil.ReduceFilterRules( node.View(), pol.compileFilterRulesForNode(users, node.View(), nodes.ViewSlice()), - nodeAddrSet(nodes.ViewSlice()), ) pmRules, err := pm.FilterForNode(node.View()) diff --git a/hscontrol/policy/v2/tailscale_routes_data_compat_test.go b/hscontrol/policy/v2/tailscale_routes_data_compat_test.go index 6e25a692e..fc41d986b 100644 --- a/hscontrol/policy/v2/tailscale_routes_data_compat_test.go +++ b/hscontrol/policy/v2/tailscale_routes_data_compat_test.go @@ -184,11 +184,6 @@ func buildRoutesUsersAndNodes( var routesSkipReasons = map[string]string{ // SaaS lists contiguous source addresses one by one; headscale merges // them into ranges. - "routes-m2-member-partial-subnet/r-exit/filter": filterAddressForm, - "routes-m2-member-partial-subnet/r-multi/filter": filterAddressForm, - "routes-m4-member-self/m-odin1/filter": filterAddressForm, - "routes-m4-member-self/m-odin2/filter": filterAddressForm, - "routes-m4-member-self/r-exit/filter": "FILTER_ADDRESS_FORM: same addresses, different wire form and rule order", } // routesNotCompared lists, by key prefix, checks left out: SaaS's answer @@ -265,8 +260,6 @@ func notCompared(prefixes []string, key string) bool { }) } -const filterAddressForm = "FILTER_ADDRESS_FORM: same addresses, different wire form" - // routesSetByAPI reports whether the capture's routes were approved through // the routes API rather than by the policy. func routesSetByAPI(c *testcapture.Capture) bool { @@ -357,7 +350,6 @@ func TestRoutesCompat(t *testing.T) { gotRules := policyutil.ReduceFilterRules( node.View(), compiledRules, - nodeAddrSet(nodes.ViewSlice()), ) wantRules := capture.PacketFilterRules diff --git a/hscontrol/policy/v2/tailscale_sequence_compat_test.go b/hscontrol/policy/v2/tailscale_sequence_compat_test.go index 6d6ac6227..4b9559fc8 100644 --- a/hscontrol/policy/v2/tailscale_sequence_compat_test.go +++ b/hscontrol/policy/v2/tailscale_sequence_compat_test.go @@ -33,17 +33,7 @@ import ( // policySequenceSkipReasons lists checks where headscale differs from SaaS, // keyed ///. A listed check that matches fails, // so a fix must delete its entry. -var policySequenceSkipReasons = map[string]string{ - "policy-generation/G0/rt/filter": viaRuleMerged, - "policy-generation/G1/rt/filter": viaRuleMerged, - "policy-generation/G2/rt/filter": viaRuleMerged, - "policy-generation/G3/rt/filter": viaRuleMerged, - "policy-generation/G3/rt-b/filter": viaRuleMerged, - "policy-generation/G4/rt-b/filter": viaRuleMerged, - "policy-generation/G5/rt-b/filter": viaRuleMerged, -} - -const viaRuleMerged = "VIA_RULE_MERGED: on a via router headscale merges the via grant's filter rule with an ACL rule of the same sources; SaaS sends the two apart" +var policySequenceSkipReasons = map[string]string{} // policySequenceNotCompared lists, by key prefix, checks left out: SaaS's // answer there is in doubt, so it is no reference. diff --git a/hscontrol/policy/v2/types.go b/hscontrol/policy/v2/types.go index 4255e5031..caf0aa301 100644 --- a/hscontrol/policy/v2/types.go +++ b/hscontrol/policy/v2/types.go @@ -211,6 +211,10 @@ var ( type resolved struct { ips netipx.IPSet + + // wire is the alias as Tailscale writes it in a filter rule; nil + // writes ips as ranges and prefixes. + wire []string } func newResolved(ipb *netipx.IPSetBuilder) (resolved, error) { @@ -230,6 +234,35 @@ func newResolvedAddresses(ips *netipx.IPSet, err error) (ResolvedAddresses, erro return resolved{ips: *ips}, err } +// newNodeAddresses wraps the addresses of the nodes an alias names. +// Tailscale writes each node address on its own, never merged into a +// range with its neighbours. +func newNodeAddresses(ips *netipx.IPSet, err error) (ResolvedAddresses, error) { + if ips == nil { + return nil, err + } + + res := resolved{ips: *ips} + for addr := range util.IPSetAddrIter(ips) { + res.wire = append(res.wire, addr.String()) + } + + return res, err +} + +// internetStrings writes [util.TheInternet] as Tailscale does: one address +// range per span, also where a span is a prefix. +var internetStrings = sync.OnceValue(func() []string { + ranges := util.TheInternet().Ranges() + out := make([]string, 0, len(ranges)) + + for _, r := range ranges { + out = append(out, r.String()) + } + + return out +}) + func ipSetToStrings(ips *netipx.IPSet) []string { var result []string @@ -251,6 +284,10 @@ func ipSetToStrings(ips *netipx.IPSet) []string { } func (res resolved) Strings() []string { + if res.wire != nil { + return slices.Clip(res.wire) + } + return ipSetToStrings(&res.ips) } @@ -492,7 +529,7 @@ func (u *Username) resolveUser(users types.Users) (types.User, error) { } func (u *Username) Resolve(_ *Policy, users types.Users, nodes views.Slice[types.NodeView]) (ResolvedAddresses, error) { - return newResolvedAddresses(u.resolve(nil, users, nodes)) + return newNodeAddresses(u.resolve(nil, users, nodes)) } func (u *Username) resolve(_ *Policy, users types.Users, nodes views.Slice[types.NodeView]) (*netipx.IPSet, error) { @@ -591,7 +628,7 @@ func (g *Group) MarshalJSON() ([]byte, error) { } func (g *Group) Resolve(p *Policy, users types.Users, nodes views.Slice[types.NodeView]) (ResolvedAddresses, error) { - return newResolvedAddresses(g.resolve(p, users, nodes)) + return newNodeAddresses(g.resolve(p, users, nodes)) } func (g *Group) resolve(p *Policy, users types.Users, nodes views.Slice[types.NodeView]) (*netipx.IPSet, error) { @@ -657,7 +694,7 @@ func (t *Tag) UnmarshalJSON(b []byte) error { } func (t *Tag) Resolve(p *Policy, users types.Users, nodes views.Slice[types.NodeView]) (ResolvedAddresses, error) { - return newResolvedAddresses(t.resolve(p, users, nodes)) + return newNodeAddresses(t.resolve(p, users, nodes)) } func (t *Tag) resolve(p *Policy, users types.Users, nodes views.Slice[types.NodeView]) (*netipx.IPSet, error) { @@ -875,7 +912,17 @@ func (ag *AutoGroup) MarshalJSON() ([]byte, error) { } func (ag *AutoGroup) Resolve(p *Policy, users types.Users, nodes views.Slice[types.NodeView]) (ResolvedAddresses, error) { - return newResolvedAddresses(ag.resolve(p, users, nodes)) + ips, err := ag.resolve(p, users, nodes) + + switch *ag { + case AutoGroupMember, AutoGroupTagged: + return newNodeAddresses(ips, err) + case AutoGroupInternet: + return resolved{ips: *ips, wire: internetStrings()}, err + case AutoGroupSelf, AutoGroupDangerAll, AutoGroupNonRoot: + } + + return newResolvedAddresses(ips, err) } func (ag *AutoGroup) resolve(p *Policy, users types.Users, nodes views.Slice[types.NodeView]) (*netipx.IPSet, error) { @@ -2502,7 +2549,7 @@ func validateGrantSrcDstCombination(sources Aliases, destinations Aliases) error // not known before the policy is compiled. func minDstPrefixes(dst Alias) int64 { if ag, ok := dst.(*AutoGroup); ok && ag.Is(AutoGroupInternet) { - return int64(len(util.TheInternet().Prefixes())) + return int64(len(internetStrings())) } return 1 diff --git a/hscontrol/policy/v2/types_test.go b/hscontrol/policy/v2/types_test.go index 8c5ecd7a3..96aff7aee 100644 --- a/hscontrol/policy/v2/types_test.go +++ b/hscontrol/policy/v2/types_test.go @@ -6550,9 +6550,9 @@ func TestUnmarshalPolicyMaxDstPorts(t *testing.T) { wantErr: true, }, { - name: "autogroup:internet counts as its prefixes", + name: "autogroup:internet counts as its address ranges", policy: `{"grants": [` + - grant("", "autogroup:internet", 100, ports(100)) + `]}`, + grant("", "autogroup:internet", 150, ports(100)) + `]}`, wantErr: true, }, {