diff --git a/ipn/ipnlocal/local_test.go b/ipn/ipnlocal/local_test.go index 31e04e1f2..7ab0d523e 100644 --- a/ipn/ipnlocal/local_test.go +++ b/ipn/ipnlocal/local_test.go @@ -11225,9 +11225,9 @@ func (f forwardTestOutcome) String() string { } func TestShouldForwardToVia(t *testing.T) { - tests := []struct { name string + envAdditions string advertisements []netip.Prefix want map[netip.Addr]forwardTestOutcome }{ @@ -11245,6 +11245,103 @@ func TestShouldForwardToVia(t *testing.T) { mapViaAddr("169.254.169.254", 1): notAllowed, }, }, + { + name: "cloudmeta-exact", + advertisements: []netip.Prefix{ + mapViaPrefix("169.254.169.254/32", 1), + }, + want: map[netip.Addr]forwardTestOutcome{ + mapViaAddr("169.254.169.254", 1): shouldForward, + mapViaAddr("169.254.169.255", 1): notAllowed, + mapViaAddr("169.254.169.254", 2): notAllowed, + }, + }, + { + name: "cloudmeta-inexact", + advertisements: []netip.Prefix{ + mapViaPrefix("169.254.169.0/24", 2), + }, + want: map[netip.Addr]forwardTestOutcome{ + mapViaAddr("169.254.1.1", 2): notAllowed, + mapViaAddr("169.254.169.1", 2): shouldForward, + mapViaAddr("169.254.169.254", 2): shouldHandle, + mapViaAddr("169.254.169.255", 2): shouldForward, + mapViaAddr("169.254.169.254", 3): notAllowed, + mapViaAddr("169.254.169.255", 3): notAllowed, + mapViaAddr("169.254.255.1", 2): notAllowed, + }, + }, + { + name: "loopback", + advertisements: []netip.Prefix{ + mapViaPrefix("127.0.0.0/8", 3), + }, + want: map[netip.Addr]forwardTestOutcome{ + mapViaAddr("127.0.0.1", 3): shouldForward, + mapViaAddr("127.127.127.127", 3): shouldForward, + mapViaAddr("169.254.169.255", 3): notAllowed, + mapViaAddr("127.0.0.1", 1): notAllowed, + }, + }, + { + name: "link-local", + advertisements: []netip.Prefix{ + mapViaPrefix("169.254.0.0/16", 4), + }, + want: map[netip.Addr]forwardTestOutcome{ + mapViaAddr("169.254.1.1", 4): shouldForward, + mapViaAddr("169.254.169.1", 4): shouldForward, + mapViaAddr("169.254.169.254", 4): shouldHandle, + mapViaAddr("169.254.169.255", 4): shouldForward, + mapViaAddr("169.254.169.254", 3): notAllowed, + mapViaAddr("169.254.255.1", 4): shouldForward, + mapViaAddr("169.254.255.1", 3): notAllowed, + }, + }, + { + name: "cgnat", + advertisements: []netip.Prefix{ + mapViaPrefix("100.64.0.0/10", 5), + }, + want: []netipx.IPRange{ + mapViaRange("100.64.0.0-100.100.100.99", 5), + mapViaRange("100.100.100.101-100.100.100.199", 5), + mapViaRange("100.100.100.201-100.127.255.255", 5), + }, + }, + { + name: "non-via", + advertisements: []netip.Prefix{ + netip.MustParsePrefix("8.0.0.0/8"), + }, + }, + { + name: "many-site-id", + advertisements: []netip.Prefix{ + mapViaPrefix("169.0.0.0/8", 4), + mapViaPrefix("169.254.169.254/32", 4), + mapViaPrefix("100.100.0.0/16", 4), + mapViaPrefix("192.168.32.0/24", 4), + mapViaPrefix("169.254.0.0/16", 5), + mapViaPrefix("10.0.0.0/8", 5), + mapViaPrefix("169.254.169.254/32", 5), + mapViaPrefix("169.254.169.0/24", 6), + mapViaPrefix("100.0.0.0/8", 7), + mapViaPrefix("100.100.100.100/32", 7), + mapViaPrefix("192.0.2.0/24", 7), + }, + want: []netipx.IPRange{ + mapViaRange("100.100.0.0-100.100.100.99", 4), + mapViaRange("100.100.100.101-100.100.100.199", 4), + mapViaRange("100.100.100.201-100.100.255.255", 4), + mapViaRange("169.254.169.254-169.254.169.254", 4), + mapViaRange("169.254.0.0-169.254.42.41", 5), + mapViaRange("169.254.42.43-169.254.255.255", 5), + mapViaRange("169.254.169.0-169.254.169.253", 6), + mapViaRange("169.254.169.255-169.254.169.255", 6), + mapViaRange("100.100.100.100-100.100.100.100", 7), + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/ipn/ipnlocal/via_test.go b/ipn/ipnlocal/via_test.go index 6c26bfef6..3593c14c6 100644 --- a/ipn/ipnlocal/via_test.go +++ b/ipn/ipnlocal/via_test.go @@ -222,14 +222,21 @@ func TestGenerateViaTargetAdditions(t *testing.T) { } } -func mapViaPrefix(v4str string, siteID uint32) netip.Prefix { - v4 := netip.MustParsePrefix(v4str) +func mapViaPrefix(v4Str string, siteID uint32) netip.Prefix { + v4 := netip.MustParsePrefix(v4Str) return must.Get(tsaddr.MapVia(siteID, v4)) } -func mapViaAddr(v4str string, siteID uint32) netip.Addr { - v4 := netip.MustParseAddr(v4str) +func mapViaAddr(v4Str string, siteID uint32) netip.Addr { + v4 := netip.MustParseAddr(v4Str) return must.Get(tsaddr.MapVia(siteID, netip.PrefixFrom(v4, v4.BitLen()))).Addr() } +func mapViaRange(rangeStr string, siteID uint32) netipx.IPRange { + v4Range := netipx.MustParseIPRange(rangeStr) + return netipx.IPRangeFrom( + must.Get(tsaddr.MapVia(siteID, netip.PrefixFrom(v4Range.From(), v4Range.From().BitLen()))).Addr(), + must.Get(tsaddr.MapVia(siteID, netip.PrefixFrom(v4Range.To(), v4Range.To().BitLen()))).Addr(), + ) +} func printMaybeViaAddr(a netip.Addr) string { siteID, v4 := tsaddr.UnmapViaWithSiteID(a) @@ -261,7 +268,7 @@ func TestGenerateViaAdvertisedAllowlist(t *testing.T) { mapViaPrefix("169.254.169.254/32", 1), }, want: []netipx.IPRange{ - netipx.IPRangeFrom(mapViaAddr("169.254.169.254", 1), mapViaAddr("169.254.169.254", 1)), + mapViaRange("169.254.169.254-169.254.169.254", 1), }, }, { @@ -270,8 +277,8 @@ func TestGenerateViaAdvertisedAllowlist(t *testing.T) { mapViaPrefix("169.254.169.0/24", 2), }, want: []netipx.IPRange{ - netipx.IPRangeFrom(mapViaAddr("169.254.169.0", 2), mapViaAddr("169.254.169.253", 2)), - netipx.IPRangeFrom(mapViaAddr("169.254.169.255", 2), mapViaAddr("169.254.169.255", 2)), + mapViaRange("169.254.169.0-169.254.169.253", 2), + mapViaRange("169.254.169.255-169.254.169.255", 2), }, }, { @@ -280,7 +287,7 @@ func TestGenerateViaAdvertisedAllowlist(t *testing.T) { mapViaPrefix("127.0.0.0/8", 3), }, want: []netipx.IPRange{ - netipx.IPRangeFrom(mapViaAddr("127.0.0.0", 3), mapViaAddr("127.255.255.255", 3)), + mapViaRange("127.0.0.0-127.255.255.255", 3), }, }, { @@ -289,9 +296,9 @@ func TestGenerateViaAdvertisedAllowlist(t *testing.T) { mapViaPrefix("169.254.0.0/16", 4), }, want: []netipx.IPRange{ - netipx.IPRangeFrom(mapViaAddr("169.254.0.0", 4), mapViaAddr("169.254.42.41", 4)), - netipx.IPRangeFrom(mapViaAddr("169.254.42.43", 4), mapViaAddr("169.254.169.253", 4)), - netipx.IPRangeFrom(mapViaAddr("169.254.169.255", 4), mapViaAddr("169.254.255.255", 4)), + mapViaRange("169.254.0.0-169.254.42.41", 4), + mapViaRange("169.254.42.43-169.254.169.253", 4), + mapViaRange("169.254.169.255-169.254.255.255", 4), }, }, { @@ -300,9 +307,9 @@ func TestGenerateViaAdvertisedAllowlist(t *testing.T) { mapViaPrefix("100.64.0.0/10", 5), }, want: []netipx.IPRange{ - netipx.IPRangeFrom(mapViaAddr("100.64.0.0", 5), mapViaAddr("100.100.100.99", 5)), - netipx.IPRangeFrom(mapViaAddr("100.100.100.101", 5), mapViaAddr("100.100.100.199", 5)), - netipx.IPRangeFrom(mapViaAddr("100.100.100.201", 5), mapViaAddr("100.127.255.255", 5)), + mapViaRange("100.64.0.0-100.100.100.99", 5), + mapViaRange("100.100.100.101-100.100.100.199", 5), + mapViaRange("100.100.100.201-100.127.255.255", 5), }, }, { @@ -311,6 +318,33 @@ func TestGenerateViaAdvertisedAllowlist(t *testing.T) { netip.MustParsePrefix("8.0.0.0/8"), }, }, + { + name: "many-site-id", + advertisedRoutes: []netip.Prefix{ + mapViaPrefix("169.0.0.0/8", 4), + mapViaPrefix("169.254.169.254/32", 4), + mapViaPrefix("100.100.0.0/16", 4), + mapViaPrefix("192.168.32.0/24", 4), + mapViaPrefix("169.254.0.0/16", 5), + mapViaPrefix("10.0.0.0/8", 5), + mapViaPrefix("169.254.169.254/32", 5), + mapViaPrefix("169.254.169.0/24", 6), + mapViaPrefix("100.0.0.0/8", 7), + mapViaPrefix("100.100.100.100/32", 7), + mapViaPrefix("192.0.2.0/24", 7), + }, + want: []netipx.IPRange{ + mapViaRange("100.100.0.0-100.100.100.99", 4), + mapViaRange("100.100.100.101-100.100.100.199", 4), + mapViaRange("100.100.100.201-100.100.255.255", 4), + mapViaRange("169.254.169.254-169.254.169.254", 4), + mapViaRange("169.254.0.0-169.254.42.41", 5), + mapViaRange("169.254.42.43-169.254.255.255", 5), + mapViaRange("169.254.169.0-169.254.169.253", 6), + mapViaRange("169.254.169.255-169.254.169.255", 6), + mapViaRange("100.100.100.100-100.100.100.100", 7), + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {