From 7d1160ddaab7bd34a57957c5afc7c99c3c09afcc Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Wed, 25 Sep 2024 11:10:46 +0200 Subject: [PATCH] {ipn,net,tsnet}: use tsaddr helpers Signed-off-by: Kristoffer Dalby --- ipn/ipnlocal/local.go | 22 +++++----------------- net/netutil/routes.go | 17 ++++++----------- 2 files changed, 11 insertions(+), 28 deletions(-) diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 1988eb1ef..d9f36797d 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -4593,11 +4593,6 @@ func magicDNSRootDomains(nm *netmap.NetworkMap) []dnsname.FQDN { return nil } -var ( - ipv4Default = netip.MustParsePrefix("0.0.0.0/0") - ipv6Default = netip.MustParsePrefix("::/0") -) - // peerRoutes returns the routerConfig.Routes to access peers. // If there are over cgnatThreshold CGNAT routes, one big CGNAT route // is used instead. @@ -4698,9 +4693,9 @@ func (b *LocalBackend) routerConfig(cfg *wgcfg.Config, prefs ipn.PrefsView, oneC var default4, default6 bool for _, route := range rs.Routes { switch route { - case ipv4Default: + case tsaddr.AllIPv4(): default4 = true - case ipv6Default: + case tsaddr.AllIPv6(): default6 = true } if default4 && default6 { @@ -4708,10 +4703,10 @@ func (b *LocalBackend) routerConfig(cfg *wgcfg.Config, prefs ipn.PrefsView, oneC } } if !default4 { - rs.Routes = append(rs.Routes, ipv4Default) + rs.Routes = append(rs.Routes, tsaddr.AllIPv4()) } if !default6 { - rs.Routes = append(rs.Routes, ipv6Default) + rs.Routes = append(rs.Routes, tsaddr.AllIPv6()) } internalIPs, externalIPs, err := internalAndExternalInterfaces() if err != nil { @@ -4768,14 +4763,7 @@ func (b *LocalBackend) applyPrefsToHostinfoLocked(hi *tailcfg.Hostinfo, prefs ip hi.ShieldsUp = prefs.ShieldsUp() hi.AllowsUpdate = envknob.AllowsRemoteUpdate() || prefs.AutoUpdate().Apply.EqualBool(true) - // count routes without exit node routes - var routes int64 - for _, route := range hi.RoutableIPs { - if route.Bits() != 0 { - routes++ - } - } - b.metrics.advertisedRoutes.Set(float64(routes)) + b.metrics.advertisedRoutes.Set(float64(tsaddr.WithoutExitRoute(prefs.AdvertiseRoutes()).Len())) var sshHostKeys []string if prefs.RunSSH() && envknob.CanSSHD() { diff --git a/net/netutil/routes.go b/net/netutil/routes.go index e86bb861c..7d67d3695 100644 --- a/net/netutil/routes.go +++ b/net/netutil/routes.go @@ -13,11 +13,6 @@ import ( "tailscale.com/net/tsaddr" ) -var ( - ipv4default = netip.MustParsePrefix("0.0.0.0/0") - ipv6default = netip.MustParsePrefix("::/0") -) - func validateViaPrefix(ipp netip.Prefix) error { if !tsaddr.IsViaPrefix(ipp) { return fmt.Errorf("%v is not a 4-in-6 prefix", ipp) @@ -60,22 +55,22 @@ func CalcAdvertiseRoutes(advertiseRoutes string, advertiseDefaultRoute bool) ([] return nil, err } } - if ipp == ipv4default { + if ipp == tsaddr.AllIPv4() { default4 = true - } else if ipp == ipv6default { + } else if ipp == tsaddr.AllIPv6() { default6 = true } routeMap[ipp] = true } if default4 && !default6 { - return nil, fmt.Errorf("%s advertised without its IPv6 counterpart, please also advertise %s", ipv4default, ipv6default) + return nil, fmt.Errorf("%s advertised without its IPv6 counterpart, please also advertise %s", tsaddr.AllIPv4(), tsaddr.AllIPv6()) } else if default6 && !default4 { - return nil, fmt.Errorf("%s advertised without its IPv4 counterpart, please also advertise %s", ipv6default, ipv4default) + return nil, fmt.Errorf("%s advertised without its IPv4 counterpart, please also advertise %s", tsaddr.AllIPv6(), tsaddr.AllIPv4()) } } if advertiseDefaultRoute { - routeMap[netip.MustParsePrefix("0.0.0.0/0")] = true - routeMap[netip.MustParsePrefix("::/0")] = true + routeMap[tsaddr.AllIPv4()] = true + routeMap[tsaddr.AllIPv6()] = true } if len(routeMap) == 0 { return nil, nil