2021-01-29 20:16:36 +00:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package wgcfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2022-03-17 00:16:55 +00:00
|
|
|
"net/netip"
|
2021-01-29 20:16:36 +00:00
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2021-10-29 21:27:29 +00:00
|
|
|
"go4.org/mem"
|
2021-05-25 19:42:22 +00:00
|
|
|
"golang.zx2c4.com/wireguard/conn"
|
|
|
|
"golang.zx2c4.com/wireguard/device"
|
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
2021-10-28 00:42:33 +00:00
|
|
|
"tailscale.com/types/key"
|
2021-01-29 20:16:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDeviceConfig(t *testing.T) {
|
2021-10-28 00:42:33 +00:00
|
|
|
newK := func() (key.NodePublic, key.NodePrivate) {
|
2021-01-29 20:16:36 +00:00
|
|
|
t.Helper()
|
2021-10-28 00:42:33 +00:00
|
|
|
k := key.NewNode()
|
|
|
|
return k.Public(), k
|
2021-01-29 20:16:36 +00:00
|
|
|
}
|
2021-10-28 00:42:33 +00:00
|
|
|
k1, pk1 := newK()
|
2022-07-26 03:55:44 +00:00
|
|
|
ip1 := netip.MustParsePrefix("10.0.0.1/32")
|
2021-01-29 20:16:36 +00:00
|
|
|
|
2021-10-28 00:42:33 +00:00
|
|
|
k2, pk2 := newK()
|
2022-07-26 03:55:44 +00:00
|
|
|
ip2 := netip.MustParsePrefix("10.0.0.2/32")
|
2021-01-29 20:16:36 +00:00
|
|
|
|
2021-10-28 00:42:33 +00:00
|
|
|
k3, _ := newK()
|
2022-07-26 03:55:44 +00:00
|
|
|
ip3 := netip.MustParsePrefix("10.0.0.3/32")
|
2021-01-29 20:16:36 +00:00
|
|
|
|
|
|
|
cfg1 := &Config{
|
2021-10-28 00:42:33 +00:00
|
|
|
PrivateKey: pk1,
|
2021-01-29 20:16:36 +00:00
|
|
|
Peers: []Peer{{
|
|
|
|
PublicKey: k2,
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
AllowedIPs: []netip.Prefix{ip2},
|
2021-01-29 20:16:36 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg2 := &Config{
|
2021-10-28 00:42:33 +00:00
|
|
|
PrivateKey: pk2,
|
2021-01-29 20:16:36 +00:00
|
|
|
Peers: []Peer{{
|
|
|
|
PublicKey: k1,
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
AllowedIPs: []netip.Prefix{ip1},
|
2021-01-29 20:16:36 +00:00
|
|
|
PersistentKeepalive: 5,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2021-11-16 19:35:25 +00:00
|
|
|
device1 := NewDevice(newNilTun(), new(noopBind), device.NewLogger(device.LogLevelError, "device1"))
|
|
|
|
device2 := NewDevice(newNilTun(), new(noopBind), device.NewLogger(device.LogLevelError, "device2"))
|
2021-01-29 20:16:36 +00:00
|
|
|
defer device1.Close()
|
|
|
|
defer device2.Close()
|
|
|
|
|
|
|
|
cmp := func(t *testing.T, d *device.Device, want *Config) {
|
|
|
|
t.Helper()
|
|
|
|
got, err := DeviceConfig(d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
prev := new(Config)
|
|
|
|
gotbuf := new(strings.Builder)
|
2021-11-11 02:42:16 +00:00
|
|
|
err = got.ToUAPI(t.Logf, gotbuf, prev)
|
2021-01-29 20:16:36 +00:00
|
|
|
gotStr := gotbuf.String()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got.ToUAPI(): error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wantbuf := new(strings.Builder)
|
2021-11-11 02:42:16 +00:00
|
|
|
err = want.ToUAPI(t.Logf, wantbuf, prev)
|
2021-01-29 20:16:36 +00:00
|
|
|
wantStr := wantbuf.String()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("want.ToUAPI(): error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if gotStr != wantStr {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
w := bufio.NewWriter(buf)
|
|
|
|
if err := d.IpcGetOperation(w); err != nil {
|
|
|
|
t.Errorf("on error, could not IpcGetOperation: %v", err)
|
|
|
|
}
|
|
|
|
w.Flush()
|
2021-04-30 23:45:36 +00:00
|
|
|
t.Errorf("config mismatch:\n---- got:\n%s\n---- want:\n%s\n---- uapi:\n%s", gotStr, wantStr, buf.String())
|
2021-01-29 20:16:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("device1 config", func(t *testing.T) {
|
|
|
|
if err := ReconfigDevice(device1, cfg1, t.Logf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cmp(t, device1, cfg1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("device2 config", func(t *testing.T) {
|
|
|
|
if err := ReconfigDevice(device2, cfg2, t.Logf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cmp(t, device2, cfg2)
|
|
|
|
})
|
|
|
|
|
|
|
|
// This is only to test that Config and Reconfig are properly synchronized.
|
|
|
|
t.Run("device2 config/reconfig", func(t *testing.T) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
ReconfigDevice(device2, cfg2, t.Logf)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
DeviceConfig(device2)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("device1 modify peer", func(t *testing.T) {
|
2021-10-29 21:27:29 +00:00
|
|
|
cfg1.Peers[0].DiscoKey = key.DiscoPublicFromRaw32(mem.B([]byte{0: 1, 31: 0}))
|
2021-01-29 20:16:36 +00:00
|
|
|
if err := ReconfigDevice(device1, cfg1, t.Logf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cmp(t, device1, cfg1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("device1 replace endpoint", func(t *testing.T) {
|
2021-10-29 21:27:29 +00:00
|
|
|
cfg1.Peers[0].DiscoKey = key.DiscoPublicFromRaw32(mem.B([]byte{0: 2, 31: 0}))
|
2021-01-29 20:16:36 +00:00
|
|
|
if err := ReconfigDevice(device1, cfg1, t.Logf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cmp(t, device1, cfg1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("device1 add new peer", func(t *testing.T) {
|
|
|
|
cfg1.Peers = append(cfg1.Peers, Peer{
|
|
|
|
PublicKey: k3,
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
AllowedIPs: []netip.Prefix{ip3},
|
2021-01-29 20:16:36 +00:00
|
|
|
})
|
|
|
|
sort.Slice(cfg1.Peers, func(i, j int) bool {
|
2021-10-28 00:42:33 +00:00
|
|
|
return cfg1.Peers[i].PublicKey.Less(cfg1.Peers[j].PublicKey)
|
2021-01-29 20:16:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
origCfg, err := DeviceConfig(device1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ReconfigDevice(device1, cfg1, t.Logf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cmp(t, device1, cfg1)
|
|
|
|
|
|
|
|
newCfg, err := DeviceConfig(device1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
peer0 := func(cfg *Config) Peer {
|
|
|
|
p, ok := cfg.PeerWithKey(k2)
|
|
|
|
if !ok {
|
|
|
|
t.Helper()
|
|
|
|
t.Fatal("failed to look up peer 2")
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
peersEqual := func(p, q Peer) bool {
|
2021-09-01 05:37:23 +00:00
|
|
|
return p.PublicKey == q.PublicKey && p.DiscoKey == q.DiscoKey && p.PersistentKeepalive == q.PersistentKeepalive && cidrsEqual(p.AllowedIPs, q.AllowedIPs)
|
2021-01-29 20:16:36 +00:00
|
|
|
}
|
|
|
|
if !peersEqual(peer0(origCfg), peer0(newCfg)) {
|
|
|
|
t.Error("reconfig modified old peer")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("device1 remove peer", func(t *testing.T) {
|
|
|
|
removeKey := cfg1.Peers[len(cfg1.Peers)-1].PublicKey
|
|
|
|
cfg1.Peers = cfg1.Peers[:len(cfg1.Peers)-1]
|
|
|
|
|
|
|
|
if err := ReconfigDevice(device1, cfg1, t.Logf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cmp(t, device1, cfg1)
|
|
|
|
|
|
|
|
newCfg, err := DeviceConfig(device1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := newCfg.PeerWithKey(removeKey)
|
|
|
|
if ok {
|
|
|
|
t.Error("reconfig failed to remove peer")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: replace with a loopback tunnel
|
|
|
|
type nilTun struct {
|
|
|
|
events chan tun.Event
|
|
|
|
closed chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNilTun() tun.Device {
|
|
|
|
return &nilTun{
|
|
|
|
events: make(chan tun.Event),
|
|
|
|
closed: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 01:58:14 +00:00
|
|
|
func (t *nilTun) File() *os.File { return nil }
|
|
|
|
func (t *nilTun) Flush() error { return nil }
|
|
|
|
func (t *nilTun) MTU() (int, error) { return 1420, nil }
|
|
|
|
func (t *nilTun) Name() (string, error) { return "niltun", nil }
|
|
|
|
func (t *nilTun) Events() <-chan tun.Event { return t.events }
|
2021-01-29 20:16:36 +00:00
|
|
|
|
2022-12-09 01:58:14 +00:00
|
|
|
func (t *nilTun) Read(data [][]byte, sizes []int, offset int) (int, error) {
|
2021-01-29 20:16:36 +00:00
|
|
|
<-t.closed
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
|
2022-12-09 01:58:14 +00:00
|
|
|
func (t *nilTun) Write(data [][]byte, offset int) (int, error) {
|
2021-01-29 20:16:36 +00:00
|
|
|
<-t.closed
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *nilTun) Close() error {
|
|
|
|
close(t.events)
|
|
|
|
close(t.closed)
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-30 23:09:49 +00:00
|
|
|
|
2022-12-09 01:58:14 +00:00
|
|
|
func (t *nilTun) BatchSize() int { return 1 }
|
|
|
|
|
2021-04-30 23:09:49 +00:00
|
|
|
// A noopBind is a conn.Bind that does no actual binding work.
|
|
|
|
type noopBind struct{}
|
|
|
|
|
|
|
|
func (noopBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err error) {
|
|
|
|
return nil, 1, nil
|
|
|
|
}
|
2022-12-09 01:58:14 +00:00
|
|
|
func (noopBind) Close() error { return nil }
|
|
|
|
func (noopBind) SetMark(mark uint32) error { return nil }
|
|
|
|
func (noopBind) Send(b [][]byte, ep conn.Endpoint) error { return nil }
|
2021-04-30 23:09:49 +00:00
|
|
|
func (noopBind) ParseEndpoint(s string) (conn.Endpoint, error) {
|
|
|
|
return dummyEndpoint(s), nil
|
|
|
|
}
|
2022-12-09 01:58:14 +00:00
|
|
|
func (noopBind) BatchSize() int { return 1 }
|
2021-04-30 23:09:49 +00:00
|
|
|
|
|
|
|
// A dummyEndpoint is a string holding the endpoint destination.
|
|
|
|
type dummyEndpoint string
|
|
|
|
|
|
|
|
func (e dummyEndpoint) ClearSrc() {}
|
|
|
|
func (e dummyEndpoint) SrcToString() string { return "" }
|
|
|
|
func (e dummyEndpoint) DstToString() string { return string(e) }
|
|
|
|
func (e dummyEndpoint) DstToBytes() []byte { return nil }
|
2022-03-17 00:16:55 +00:00
|
|
|
func (e dummyEndpoint) DstIP() netip.Addr { return netip.Addr{} }
|
|
|
|
func (dummyEndpoint) SrcIP() netip.Addr { return netip.Addr{} }
|