2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-09-01 20:24:58 +00:00
|
|
|
|
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2020-09-22 16:13:45 +00:00
|
|
|
"fmt"
|
2020-09-01 20:24:58 +00:00
|
|
|
"math/rand"
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2022-08-11 17:23:35 +00:00
|
|
|
"reflect"
|
2020-09-22 16:13:45 +00:00
|
|
|
"strings"
|
2020-09-01 20:24:58 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-09-26 02:11:05 +00:00
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
2020-09-01 20:24:58 +00:00
|
|
|
)
|
|
|
|
|
2022-08-11 17:23:35 +00:00
|
|
|
func randIP() netip.Addr {
|
2020-09-01 20:24:58 +00:00
|
|
|
b := byte(rand.Intn(3))
|
2022-08-11 17:23:35 +00:00
|
|
|
return netip.AddrFrom4([4]byte{b, b, b, b})
|
2020-09-01 20:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func randRouteData() *winipcfg.RouteData {
|
|
|
|
return &winipcfg.RouteData{
|
2022-08-11 17:23:35 +00:00
|
|
|
Destination: netip.PrefixFrom(randIP(), rand.Intn(30)+1),
|
|
|
|
NextHop: randIP(),
|
|
|
|
Metric: uint32(rand.Intn(3)),
|
2020-09-01 20:24:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouteLess(t *testing.T) {
|
|
|
|
type D = winipcfg.RouteData
|
2022-08-11 17:23:35 +00:00
|
|
|
ipnet := netip.MustParsePrefix
|
2020-09-01 20:24:58 +00:00
|
|
|
tests := []struct {
|
|
|
|
ri, rj *winipcfg.RouteData
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
ri: &D{Metric: 1},
|
|
|
|
rj: &D{Metric: 2},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ri: &D{Destination: ipnet("1.1.0.0/16"), Metric: 2},
|
|
|
|
rj: &D{Destination: ipnet("2.2.0.0/16"), Metric: 1},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ri: &D{Destination: ipnet("1.1.0.0/16"), Metric: 1},
|
|
|
|
rj: &D{Destination: ipnet("2.2.0.0/16"), Metric: 1},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ri: &D{Destination: ipnet("1.1.0.0/32"), Metric: 2},
|
|
|
|
rj: &D{Destination: ipnet("1.1.0.0/16"), Metric: 1},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ri: &D{Destination: ipnet("1.1.0.0/32"), Metric: 1},
|
|
|
|
rj: &D{Destination: ipnet("1.1.0.0/16"), Metric: 1},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2022-08-11 17:23:35 +00:00
|
|
|
ri: &D{Destination: ipnet("1.1.0.0/16"), Metric: 1, NextHop: netip.MustParseAddr("3.3.3.3")},
|
|
|
|
rj: &D{Destination: ipnet("1.1.0.0/16"), Metric: 1, NextHop: netip.MustParseAddr("4.4.4.4")},
|
2020-09-01 20:24:58 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2022-08-11 17:23:35 +00:00
|
|
|
got := routeDataLess(tt.ri, tt.rj)
|
2020-09-01 20:24:58 +00:00
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("%v. less = %v; want %v", i, got, tt.want)
|
|
|
|
}
|
2022-08-11 17:23:35 +00:00
|
|
|
back := routeDataLess(tt.rj, tt.ri)
|
2020-09-01 20:24:58 +00:00
|
|
|
if back && got {
|
|
|
|
t.Errorf("%v. less both ways", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 17:23:35 +00:00
|
|
|
func TestRouteDataLessConsistent(t *testing.T) {
|
2020-09-01 20:24:58 +00:00
|
|
|
for i := 0; i < 10000; i++ {
|
|
|
|
ri := randRouteData()
|
|
|
|
rj := randRouteData()
|
2022-08-11 17:23:35 +00:00
|
|
|
if routeDataLess(ri, rj) && routeDataLess(rj, ri) {
|
2020-09-01 20:24:58 +00:00
|
|
|
t.Fatalf("both compare less to each other:\n\t%#v\nand\n\t%#v", ri, rj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 16:13:45 +00:00
|
|
|
|
2022-08-11 17:23:35 +00:00
|
|
|
func nets(cidrs ...string) (ret []netip.Prefix) {
|
|
|
|
for _, s := range cidrs {
|
|
|
|
ret = append(ret, netip.MustParsePrefix(s))
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
2022-08-11 17:23:35 +00:00
|
|
|
return
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 17:23:35 +00:00
|
|
|
func nilIfEmpty[E any](s []E) []E {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return nil
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
2022-08-11 17:23:35 +00:00
|
|
|
return s
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeltaNets(t *testing.T) {
|
|
|
|
tests := []struct {
|
2022-08-11 17:23:35 +00:00
|
|
|
a, b []netip.Prefix
|
|
|
|
wantAdd, wantDel []netip.Prefix
|
2020-09-22 16:13:45 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
a: nets("1.2.3.4/24", "1.2.3.4/31", "1.2.3.3/32", "10.0.1.1/32", "100.0.1.1/32"),
|
|
|
|
b: nets("10.0.1.1/32", "100.0.2.1/32", "1.2.3.3/32", "1.2.3.4/24"),
|
|
|
|
wantAdd: nets("100.0.2.1/32"),
|
|
|
|
wantDel: nets("1.2.3.4/31", "100.0.1.1/32"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: nets("fe80::99d0:ec2d:b2e7:536b/64", "100.84.36.11/32"),
|
|
|
|
b: nets("100.84.36.11/32"),
|
|
|
|
wantDel: nets("fe80::99d0:ec2d:b2e7:536b/64"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: nets("100.84.36.11/32", "fe80::99d0:ec2d:b2e7:536b/64"),
|
|
|
|
b: nets("100.84.36.11/32"),
|
|
|
|
wantDel: nets("fe80::99d0:ec2d:b2e7:536b/64"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: nets("100.84.36.11/32", "fe80::99d0:ec2d:b2e7:536b/64"),
|
2022-08-11 17:23:35 +00:00
|
|
|
b: nets("100.84.36.11/32"),
|
2020-09-22 16:13:45 +00:00
|
|
|
wantDel: nets("fe80::99d0:ec2d:b2e7:536b/64"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
add, del := deltaNets(tt.a, tt.b)
|
2022-08-11 17:23:35 +00:00
|
|
|
if !reflect.DeepEqual(nilIfEmpty(add), nilIfEmpty(tt.wantAdd)) {
|
2020-09-22 16:13:45 +00:00
|
|
|
t.Errorf("[%d] add:\n got: %v\n want: %v\n", i, add, tt.wantAdd)
|
|
|
|
}
|
2022-08-11 17:23:35 +00:00
|
|
|
if !reflect.DeepEqual(nilIfEmpty(del), nilIfEmpty(tt.wantDel)) {
|
2020-09-22 16:13:45 +00:00
|
|
|
t.Errorf("[%d] del:\n got: %v\n want: %v\n", i, del, tt.wantDel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-24 04:23:59 +00:00
|
|
|
func formatRouteData(rds []*winipcfg.RouteData) string {
|
|
|
|
var b strings.Builder
|
|
|
|
for _, rd := range rds {
|
|
|
|
b.WriteString(fmt.Sprintf("%+v", rd))
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2020-09-22 16:13:45 +00:00
|
|
|
func equalRouteDatas(a, b []*winipcfg.RouteData) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range a {
|
|
|
|
if routeDataCompare(a[i], b[i]) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-08-11 17:23:35 +00:00
|
|
|
func ipnet4(ip string, bits int) netip.Prefix {
|
|
|
|
return netip.PrefixFrom(netip.MustParseAddr(ip), bits)
|
|
|
|
}
|
|
|
|
|
2021-04-24 04:23:59 +00:00
|
|
|
func TestFilterRoutes(t *testing.T) {
|
2022-08-11 17:23:35 +00:00
|
|
|
var h0 netip.Addr
|
|
|
|
|
2021-04-24 04:23:59 +00:00
|
|
|
in := []*winipcfg.RouteData{
|
|
|
|
// LinkLocal and Loopback routes.
|
2022-08-11 17:23:35 +00:00
|
|
|
{ipnet4("169.254.0.0", 16), h0, 1},
|
|
|
|
{ipnet4("169.254.255.255", 32), h0, 1},
|
|
|
|
{ipnet4("127.0.0.0", 8), h0, 1},
|
|
|
|
{ipnet4("127.255.255.255", 32), h0, 1},
|
2021-04-24 04:23:59 +00:00
|
|
|
// Local LAN routes.
|
2022-08-11 17:23:35 +00:00
|
|
|
{ipnet4("192.168.0.0", 24), h0, 1},
|
|
|
|
{ipnet4("192.168.0.255", 32), h0, 1},
|
|
|
|
{ipnet4("192.168.1.0", 25), h0, 1},
|
|
|
|
{ipnet4("192.168.1.127", 32), h0, 1},
|
2021-04-24 04:23:59 +00:00
|
|
|
// Some random other route.
|
2022-08-11 17:23:35 +00:00
|
|
|
{ipnet4("192.168.2.23", 32), h0, 1},
|
2021-04-24 04:23:59 +00:00
|
|
|
// Our own tailscale address.
|
2022-08-11 17:23:35 +00:00
|
|
|
{ipnet4("100.100.100.100", 32), h0, 1},
|
2021-04-24 04:23:59 +00:00
|
|
|
// Other tailscale addresses.
|
2022-08-11 17:23:35 +00:00
|
|
|
{ipnet4("100.100.100.101", 32), h0, 1},
|
|
|
|
{ipnet4("100.100.100.102", 32), h0, 1},
|
2021-04-24 04:23:59 +00:00
|
|
|
}
|
|
|
|
want := []*winipcfg.RouteData{
|
2022-08-11 17:23:35 +00:00
|
|
|
{ipnet4("169.254.0.0", 16), h0, 1},
|
|
|
|
{ipnet4("127.0.0.0", 8), h0, 1},
|
|
|
|
{ipnet4("192.168.0.0", 24), h0, 1},
|
|
|
|
{ipnet4("192.168.1.0", 25), h0, 1},
|
|
|
|
{ipnet4("192.168.2.23", 32), h0, 1},
|
|
|
|
{ipnet4("100.100.100.101", 32), h0, 1},
|
|
|
|
{ipnet4("100.100.100.102", 32), h0, 1},
|
2021-04-24 04:23:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
got := filterRoutes(in, mustCIDRs("100.100.100.100/32"))
|
|
|
|
if !equalRouteDatas(got, want) {
|
|
|
|
t.Errorf("\ngot: %v\n want: %v\n", formatRouteData(got), formatRouteData(want))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 16:13:45 +00:00
|
|
|
func TestDeltaRouteData(t *testing.T) {
|
2022-08-11 17:23:35 +00:00
|
|
|
var h0 netip.Addr
|
|
|
|
h1 := netip.MustParseAddr("99.99.99.99")
|
|
|
|
h2 := netip.MustParseAddr("99.99.9.99")
|
2020-09-22 16:13:45 +00:00
|
|
|
|
|
|
|
a := []*winipcfg.RouteData{
|
2022-08-11 17:23:35 +00:00
|
|
|
{ipnet4("1.2.3.4", 32), h0, 1},
|
|
|
|
{ipnet4("1.2.3.4", 24), h1, 2},
|
|
|
|
{ipnet4("1.2.3.4", 24), h2, 1},
|
|
|
|
{ipnet4("1.2.3.5", 32), h0, 1},
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
|
|
|
b := []*winipcfg.RouteData{
|
2022-08-11 17:23:35 +00:00
|
|
|
{ipnet4("1.2.3.5", 32), h0, 1},
|
|
|
|
{ipnet4("1.2.3.4", 24), h1, 2},
|
|
|
|
{ipnet4("1.2.3.4", 24), h2, 2},
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
|
|
|
add, del := deltaRouteData(a, b)
|
|
|
|
|
|
|
|
wantAdd := []*winipcfg.RouteData{
|
2022-08-11 17:23:35 +00:00
|
|
|
{ipnet4("1.2.3.4", 24), h2, 2},
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
|
|
|
wantDel := []*winipcfg.RouteData{
|
2022-08-11 17:23:35 +00:00
|
|
|
{ipnet4("1.2.3.4", 32), h0, 1},
|
|
|
|
{ipnet4("1.2.3.4", 24), h2, 1},
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !equalRouteDatas(add, wantAdd) {
|
2021-04-24 04:23:59 +00:00
|
|
|
t.Errorf("add:\n got: %v\n want: %v\n", formatRouteData(add), formatRouteData(wantAdd))
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
|
|
|
if !equalRouteDatas(del, wantDel) {
|
2021-04-24 04:23:59 +00:00
|
|
|
t.Errorf("del:\n got: %v\n want: %v\n", formatRouteData(del), formatRouteData(wantDel))
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
|
|
|
}
|