2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-06-05 15:19:03 +00:00
|
|
|
|
|
|
|
package tstun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-12-16 18:14:00 +00:00
|
|
|
"context"
|
2021-04-07 18:32:53 +00:00
|
|
|
"encoding/binary"
|
2022-12-22 20:51:06 +00:00
|
|
|
"encoding/hex"
|
2020-11-10 04:12:21 +00:00
|
|
|
"fmt"
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2023-04-21 14:27:15 +00:00
|
|
|
"reflect"
|
2020-11-10 04:12:21 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-06-05 15:19:03 +00:00
|
|
|
"testing"
|
2023-04-21 14:27:15 +00:00
|
|
|
"time"
|
2022-12-22 20:51:06 +00:00
|
|
|
"unicode"
|
2020-06-25 21:19:12 +00:00
|
|
|
"unsafe"
|
2020-06-05 15:19:03 +00:00
|
|
|
|
2022-12-16 18:14:00 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2022-12-09 23:12:20 +00:00
|
|
|
"github.com/tailscale/wireguard-go/tun/tuntest"
|
2021-11-02 21:41:56 +00:00
|
|
|
"go4.org/mem"
|
2022-07-25 03:08:42 +00:00
|
|
|
"go4.org/netipx"
|
2023-09-06 09:45:52 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/buffer"
|
2023-04-21 14:27:15 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
2021-09-13 21:21:40 +00:00
|
|
|
"tailscale.com/disco"
|
2022-11-28 23:59:33 +00:00
|
|
|
"tailscale.com/net/connstats"
|
2022-07-25 03:08:42 +00:00
|
|
|
"tailscale.com/net/netaddr"
|
2020-11-10 00:16:04 +00:00
|
|
|
"tailscale.com/net/packet"
|
2021-09-13 21:21:40 +00:00
|
|
|
"tailscale.com/tstest"
|
2021-07-21 17:43:53 +00:00
|
|
|
"tailscale.com/tstime/mono"
|
2021-03-20 04:05:51 +00:00
|
|
|
"tailscale.com/types/ipproto"
|
2021-11-02 21:41:56 +00:00
|
|
|
"tailscale.com/types/key"
|
2020-06-05 15:19:03 +00:00
|
|
|
"tailscale.com/types/logger"
|
2022-10-27 21:14:18 +00:00
|
|
|
"tailscale.com/types/netlogtype"
|
2023-04-13 17:12:31 +00:00
|
|
|
"tailscale.com/types/ptr"
|
2024-06-18 19:05:34 +00:00
|
|
|
"tailscale.com/types/views"
|
2022-12-22 20:51:06 +00:00
|
|
|
"tailscale.com/util/must"
|
2023-04-21 14:27:15 +00:00
|
|
|
"tailscale.com/wgengine/capture"
|
2020-06-05 15:19:03 +00:00
|
|
|
"tailscale.com/wgengine/filter"
|
2023-03-29 16:51:18 +00:00
|
|
|
"tailscale.com/wgengine/wgcfg"
|
2020-06-05 15:19:03 +00:00
|
|
|
)
|
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
func udp4(src, dst string, sport, dport uint16) []byte {
|
2022-07-26 03:55:44 +00:00
|
|
|
sip, err := netip.ParseAddr(src)
|
2020-12-20 00:43:25 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-26 03:55:44 +00:00
|
|
|
dip, err := netip.ParseAddr(dst)
|
2020-12-20 00:43:25 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-09 23:34:03 +00:00
|
|
|
header := &packet.UDP4Header{
|
|
|
|
IP4Header: packet.IP4Header{
|
2020-12-20 00:43:25 +00:00
|
|
|
Src: sip,
|
|
|
|
Dst: dip,
|
|
|
|
IPID: 0,
|
2020-06-05 15:19:03 +00:00
|
|
|
},
|
|
|
|
SrcPort: sport,
|
|
|
|
DstPort: dport,
|
|
|
|
}
|
|
|
|
return packet.Generate(header, []byte("udp_payload"))
|
|
|
|
}
|
|
|
|
|
2021-04-07 18:32:53 +00:00
|
|
|
func tcp4syn(src, dst string, sport, dport uint16) []byte {
|
2022-07-26 03:55:44 +00:00
|
|
|
sip, err := netip.ParseAddr(src)
|
2021-04-07 18:32:53 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-26 03:55:44 +00:00
|
|
|
dip, err := netip.ParseAddr(dst)
|
2021-04-07 18:32:53 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ipHeader := packet.IP4Header{
|
|
|
|
IPProto: ipproto.TCP,
|
|
|
|
Src: sip,
|
|
|
|
Dst: dip,
|
|
|
|
IPID: 0,
|
|
|
|
}
|
|
|
|
tcpHeader := make([]byte, 20)
|
|
|
|
binary.BigEndian.PutUint16(tcpHeader[0:], sport)
|
|
|
|
binary.BigEndian.PutUint16(tcpHeader[2:], dport)
|
|
|
|
tcpHeader[13] |= 2 // SYN
|
|
|
|
|
|
|
|
both := packet.Generate(ipHeader, tcpHeader)
|
|
|
|
|
|
|
|
// 20 byte IP4 + 20 byte TCP
|
|
|
|
binary.BigEndian.PutUint16(both[2:4], 40)
|
|
|
|
|
|
|
|
return both
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func nets(nets ...string) (ret []netip.Prefix) {
|
2020-11-10 04:12:21 +00:00
|
|
|
for _, s := range nets {
|
|
|
|
if i := strings.IndexByte(s, '/'); i == -1 {
|
2022-07-26 03:55:44 +00:00
|
|
|
ip, err := netip.ParseAddr(s)
|
2020-11-10 04:12:21 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
bits := uint8(32)
|
|
|
|
if ip.Is6() {
|
|
|
|
bits = 128
|
|
|
|
}
|
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
|
|
|
ret = append(ret, netip.PrefixFrom(ip, int(bits)))
|
2020-11-10 04:12:21 +00:00
|
|
|
} else {
|
2022-07-26 03:55:44 +00:00
|
|
|
pfx, err := netip.ParsePrefix(s)
|
2020-11-10 04:12:21 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ret = append(ret, pfx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
2020-07-16 15:39:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 04:12:21 +00:00
|
|
|
func ports(s string) filter.PortRange {
|
|
|
|
if s == "*" {
|
2020-11-10 06:02:03 +00:00
|
|
|
return filter.PortRange{First: 0, Last: 65535}
|
2020-11-10 04:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var fs, ls string
|
|
|
|
i := strings.IndexByte(s, '-')
|
|
|
|
if i == -1 {
|
|
|
|
fs = s
|
|
|
|
ls = fs
|
|
|
|
} else {
|
|
|
|
fs = s[:i]
|
|
|
|
ls = s[i+1:]
|
|
|
|
}
|
|
|
|
first, err := strconv.ParseInt(fs, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("invalid NetPortRange %q", s))
|
2020-06-05 15:19:03 +00:00
|
|
|
}
|
2020-11-10 04:12:21 +00:00
|
|
|
last, err := strconv.ParseInt(ls, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("invalid NetPortRange %q", s))
|
|
|
|
}
|
|
|
|
return filter.PortRange{First: uint16(first), Last: uint16(last)}
|
2020-06-05 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 04:12:21 +00:00
|
|
|
func netports(netPorts ...string) (ret []filter.NetPortRange) {
|
|
|
|
for _, s := range netPorts {
|
|
|
|
i := strings.LastIndexByte(s, ':')
|
|
|
|
if i == -1 {
|
|
|
|
panic(fmt.Sprintf("invalid NetPortRange %q", s))
|
|
|
|
}
|
|
|
|
|
|
|
|
npr := filter.NetPortRange{
|
|
|
|
Net: nets(s[:i])[0],
|
|
|
|
Ports: ports(s[i+1:]),
|
|
|
|
}
|
|
|
|
ret = append(ret, npr)
|
2020-06-05 15:19:03 +00:00
|
|
|
}
|
2020-11-10 04:12:21 +00:00
|
|
|
return ret
|
2020-06-05 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 06:13:20 +00:00
|
|
|
func setfilter(logf logger.Logf, tun *Wrapper) {
|
2024-06-18 19:05:34 +00:00
|
|
|
protos := views.SliceOf([]ipproto.Proto{
|
2021-03-21 04:45:47 +00:00
|
|
|
ipproto.TCP,
|
|
|
|
ipproto.UDP,
|
2024-06-18 19:05:34 +00:00
|
|
|
})
|
2020-11-10 05:33:41 +00:00
|
|
|
matches := []filter.Match{
|
2021-03-17 21:24:32 +00:00
|
|
|
{IPProto: protos, Srcs: nets("5.6.7.8"), Dsts: netports("1.2.3.4:89-90")},
|
|
|
|
{IPProto: protos, Srcs: nets("1.2.3.4"), Dsts: netports("5.6.7.8:98")},
|
2020-06-05 15:19:03 +00:00
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
var sb netipx.IPSetBuilder
|
2022-07-26 03:55:44 +00:00
|
|
|
sb.AddPrefix(netip.MustParsePrefix("1.2.0.0/16"))
|
2021-06-02 16:04:37 +00:00
|
|
|
ipSet, _ := sb.IPSet()
|
|
|
|
tun.SetFilter(filter.New(matches, ipSet, ipSet, nil, logf))
|
2020-06-05 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 06:13:20 +00:00
|
|
|
func newChannelTUN(logf logger.Logf, secure bool) (*tuntest.ChannelTUN, *Wrapper) {
|
2020-06-05 15:19:03 +00:00
|
|
|
chtun := tuntest.NewChannelTUN()
|
2021-03-27 06:13:20 +00:00
|
|
|
tun := Wrap(logf, chtun.TUN())
|
2020-06-05 15:19:03 +00:00
|
|
|
if secure {
|
|
|
|
setfilter(logf, tun)
|
|
|
|
} else {
|
2020-06-08 22:19:26 +00:00
|
|
|
tun.disableFilter = true
|
2020-06-05 15:19:03 +00:00
|
|
|
}
|
2023-10-13 19:41:10 +00:00
|
|
|
tun.Start()
|
2020-06-05 15:19:03 +00:00
|
|
|
return chtun, tun
|
|
|
|
}
|
|
|
|
|
2021-03-27 06:13:20 +00:00
|
|
|
func newFakeTUN(logf logger.Logf, secure bool) (*fakeTUN, *Wrapper) {
|
2021-03-27 05:46:37 +00:00
|
|
|
ftun := NewFake()
|
2021-03-27 06:13:20 +00:00
|
|
|
tun := Wrap(logf, ftun)
|
2020-06-05 15:19:03 +00:00
|
|
|
if secure {
|
|
|
|
setfilter(logf, tun)
|
|
|
|
} else {
|
2020-06-08 22:19:26 +00:00
|
|
|
tun.disableFilter = true
|
2020-06-05 15:19:03 +00:00
|
|
|
}
|
|
|
|
return ftun.(*fakeTUN), tun
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadAndInject(t *testing.T) {
|
|
|
|
chtun, tun := newChannelTUN(t.Logf, false)
|
|
|
|
defer tun.Close()
|
|
|
|
|
|
|
|
const size = 2 // all payloads have this size
|
|
|
|
written := []string{"w0", "w1"}
|
|
|
|
injected := []string{"i0", "i1"}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for _, packet := range written {
|
|
|
|
payload := []byte(packet)
|
|
|
|
chtun.Outbound <- payload
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, packet := range injected {
|
|
|
|
go func(packet string) {
|
|
|
|
payload := []byte(packet)
|
|
|
|
err := tun.InjectOutbound(payload)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: error: %v", packet, err)
|
|
|
|
}
|
|
|
|
}(packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf [MaxPacketSize]byte
|
|
|
|
var seen = make(map[string]bool)
|
2022-12-09 01:58:14 +00:00
|
|
|
sizes := make([]int, 1)
|
2020-06-05 15:19:03 +00:00
|
|
|
// We expect the same packets back, in no particular order.
|
2024-04-16 20:15:13 +00:00
|
|
|
for i := range len(written) + len(injected) {
|
2022-12-09 01:58:14 +00:00
|
|
|
packet := buf[:]
|
|
|
|
buffs := [][]byte{packet}
|
|
|
|
numPackets, err := tun.Read(buffs, sizes, 0)
|
2020-06-05 15:19:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("read %d: error: %v", i, err)
|
|
|
|
}
|
2022-12-09 01:58:14 +00:00
|
|
|
if numPackets != 1 {
|
|
|
|
t.Fatalf("read %d packets, expected %d", numPackets, 1)
|
2020-06-05 15:19:03 +00:00
|
|
|
}
|
2022-12-09 01:58:14 +00:00
|
|
|
packet = packet[:sizes[0]]
|
|
|
|
packetLen := len(packet)
|
|
|
|
if packetLen != size {
|
|
|
|
t.Errorf("read %d: got size %d; want %d", i, packetLen, size)
|
|
|
|
}
|
|
|
|
got := string(packet)
|
2020-06-05 15:19:03 +00:00
|
|
|
t.Logf("read %d: got %s", i, got)
|
|
|
|
seen[got] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, packet := range written {
|
|
|
|
if !seen[packet] {
|
|
|
|
t.Errorf("%s not received", packet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, packet := range injected {
|
|
|
|
if !seen[packet] {
|
|
|
|
t.Errorf("%s not received", packet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteAndInject(t *testing.T) {
|
|
|
|
chtun, tun := newChannelTUN(t.Logf, false)
|
|
|
|
defer tun.Close()
|
|
|
|
|
|
|
|
written := []string{"w0", "w1"}
|
|
|
|
injected := []string{"i0", "i1"}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for _, packet := range written {
|
|
|
|
payload := []byte(packet)
|
2022-12-09 01:58:14 +00:00
|
|
|
_, err := tun.Write([][]byte{payload}, 0)
|
2020-06-05 15:19:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: error: %v", packet, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, packet := range injected {
|
|
|
|
go func(packet string) {
|
|
|
|
payload := []byte(packet)
|
2020-06-08 22:19:26 +00:00
|
|
|
err := tun.InjectInboundCopy(payload)
|
2020-06-05 15:19:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: error: %v", packet, err)
|
|
|
|
}
|
|
|
|
}(packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
// We expect the same packets back, in no particular order.
|
2024-04-16 20:15:13 +00:00
|
|
|
for i := range len(written) + len(injected) {
|
2020-06-05 15:19:03 +00:00
|
|
|
packet := <-chtun.Inbound
|
|
|
|
got := string(packet)
|
|
|
|
t.Logf("read %d: got %s", i, got)
|
|
|
|
seen[got] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, packet := range written {
|
|
|
|
if !seen[packet] {
|
|
|
|
t.Errorf("%s not received", packet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, packet := range injected {
|
|
|
|
if !seen[packet] {
|
|
|
|
t.Errorf("%s not received", packet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 20:51:06 +00:00
|
|
|
// mustHexDecode is like hex.DecodeString, but panics on error
|
2023-01-14 20:59:47 +00:00
|
|
|
// and ignores whitespace in s.
|
2022-12-22 20:51:06 +00:00
|
|
|
func mustHexDecode(s string) []byte {
|
|
|
|
return must.Get(hex.DecodeString(strings.Map(func(r rune) rune {
|
|
|
|
if unicode.IsSpace(r) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}, s)))
|
|
|
|
}
|
|
|
|
|
2020-06-05 15:19:03 +00:00
|
|
|
func TestFilter(t *testing.T) {
|
|
|
|
chtun, tun := newChannelTUN(t.Logf, true)
|
|
|
|
defer tun.Close()
|
|
|
|
|
|
|
|
type direction int
|
|
|
|
|
|
|
|
const (
|
|
|
|
in direction = iota
|
|
|
|
out
|
|
|
|
)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
dir direction
|
|
|
|
drop bool
|
|
|
|
data []byte
|
|
|
|
}{
|
2022-12-22 20:51:06 +00:00
|
|
|
{"short_in", in, true, []byte("\x45xxx")},
|
|
|
|
{"short_out", out, true, []byte("\x45xxx")},
|
|
|
|
{"ip97_out", out, false, mustHexDecode("4500 0019 d186 4000 4061 751d 644a 4603 6449 e549 6865 6c6c 6f")},
|
2020-12-20 00:43:25 +00:00
|
|
|
{"bad_port_in", in, true, udp4("5.6.7.8", "1.2.3.4", 22, 22)},
|
|
|
|
{"bad_port_out", out, false, udp4("1.2.3.4", "5.6.7.8", 22, 22)},
|
|
|
|
{"bad_ip_in", in, true, udp4("8.1.1.1", "1.2.3.4", 89, 89)},
|
|
|
|
{"bad_ip_out", out, false, udp4("1.2.3.4", "8.1.1.1", 98, 98)},
|
|
|
|
{"good_packet_in", in, false, udp4("5.6.7.8", "1.2.3.4", 89, 89)},
|
|
|
|
{"good_packet_out", out, false, udp4("1.2.3.4", "5.6.7.8", 98, 98)},
|
2020-06-05 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 06:13:20 +00:00
|
|
|
// A reader on the other end of the tun.
|
2020-06-05 15:19:03 +00:00
|
|
|
go func() {
|
|
|
|
var recvbuf []byte
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tun.closed:
|
|
|
|
return
|
|
|
|
case recvbuf = <-chtun.Inbound:
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
if tt.drop && bytes.Equal(recvbuf, tt.data) {
|
|
|
|
t.Errorf("did not drop %s", tt.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var buf [MaxPacketSize]byte
|
2022-12-16 18:14:00 +00:00
|
|
|
stats := connstats.NewStatistics(0, 0, nil)
|
|
|
|
defer stats.Shutdown(context.Background())
|
2022-11-28 23:59:33 +00:00
|
|
|
tun.SetStatistics(stats)
|
2020-06-05 15:19:03 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
var n int
|
|
|
|
var err error
|
|
|
|
var filtered bool
|
2022-12-09 01:58:14 +00:00
|
|
|
sizes := make([]int, 1)
|
2020-06-05 15:19:03 +00:00
|
|
|
|
2022-12-16 18:14:00 +00:00
|
|
|
tunStats, _ := stats.TestExtract()
|
2022-11-28 23:59:33 +00:00
|
|
|
if len(tunStats) > 0 {
|
|
|
|
t.Errorf("connstats.Statistics.Extract = %v, want {}", stats)
|
2022-10-05 19:24:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 15:19:03 +00:00
|
|
|
if tt.dir == in {
|
2021-05-06 04:00:49 +00:00
|
|
|
// Use the side effect of updating the last
|
|
|
|
// activity atomic to determine whether the
|
|
|
|
// data was actually filtered.
|
|
|
|
// If it stays zero, nothing made it through
|
|
|
|
// to the wrapped TUN.
|
2021-07-21 17:43:53 +00:00
|
|
|
tun.lastActivityAtomic.StoreAtomic(0)
|
2022-12-09 01:58:14 +00:00
|
|
|
_, err = tun.Write([][]byte{tt.data}, 0)
|
2021-07-21 17:43:53 +00:00
|
|
|
filtered = tun.lastActivityAtomic.LoadAtomic() == 0
|
2020-06-05 15:19:03 +00:00
|
|
|
} else {
|
|
|
|
chtun.Outbound <- tt.data
|
2022-12-09 01:58:14 +00:00
|
|
|
n, err = tun.Read([][]byte{buf[:]}, sizes, 0)
|
2020-06-05 15:19:03 +00:00
|
|
|
// In the read direction, errors are fatal, so we return n = 0 instead.
|
|
|
|
filtered = (n == 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got err %v; want nil", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if filtered {
|
|
|
|
if !tt.drop {
|
|
|
|
t.Errorf("got drop; want accept")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if tt.drop {
|
|
|
|
t.Errorf("got accept; want drop")
|
|
|
|
}
|
|
|
|
}
|
2022-10-05 19:24:30 +00:00
|
|
|
|
2022-12-16 18:14:00 +00:00
|
|
|
got, _ := stats.TestExtract()
|
2022-10-27 21:14:18 +00:00
|
|
|
want := map[netlogtype.Connection]netlogtype.Counts{}
|
2022-12-22 20:51:06 +00:00
|
|
|
var wasUDP bool
|
2022-10-05 19:24:30 +00:00
|
|
|
if !tt.drop {
|
|
|
|
var p packet.Parsed
|
|
|
|
p.Decode(tt.data)
|
2022-12-22 20:51:06 +00:00
|
|
|
wasUDP = p.IPProto == ipproto.UDP
|
2022-10-05 19:24:30 +00:00
|
|
|
switch tt.dir {
|
|
|
|
case in:
|
2022-10-27 21:14:18 +00:00
|
|
|
conn := netlogtype.Connection{Proto: ipproto.UDP, Src: p.Dst, Dst: p.Src}
|
|
|
|
want[conn] = netlogtype.Counts{RxPackets: 1, RxBytes: uint64(len(tt.data))}
|
2022-10-05 19:24:30 +00:00
|
|
|
case out:
|
2022-10-27 21:14:18 +00:00
|
|
|
conn := netlogtype.Connection{Proto: ipproto.UDP, Src: p.Src, Dst: p.Dst}
|
|
|
|
want[conn] = netlogtype.Counts{TxPackets: 1, TxBytes: uint64(len(tt.data))}
|
2022-10-05 19:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-22 20:51:06 +00:00
|
|
|
if wasUDP {
|
|
|
|
if diff := cmp.Diff(got, want, cmpopts.EquateEmpty()); diff != "" {
|
|
|
|
t.Errorf("stats.TestExtract (-got +want):\n%s", diff)
|
|
|
|
}
|
2022-10-05 19:24:30 +00:00
|
|
|
}
|
2020-06-05 15:19:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocs(t *testing.T) {
|
|
|
|
ftun, tun := newFakeTUN(t.Logf, false)
|
|
|
|
defer tun.Close()
|
|
|
|
|
2022-12-12 14:33:48 +00:00
|
|
|
buf := [][]byte{{0x00}}
|
2021-10-27 23:21:44 +00:00
|
|
|
err := tstest.MinAllocsPerRun(t, 0, func() {
|
2020-10-06 02:36:48 +00:00
|
|
|
_, err := ftun.Write(buf, 0)
|
2020-06-05 15:19:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("write: error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-10-27 23:21:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2020-06-05 15:19:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:13:45 +00:00
|
|
|
func TestClose(t *testing.T) {
|
|
|
|
ftun, tun := newFakeTUN(t.Logf, false)
|
|
|
|
|
2022-12-09 01:58:14 +00:00
|
|
|
data := [][]byte{udp4("1.2.3.4", "5.6.7.8", 98, 98)}
|
2021-01-12 23:13:45 +00:00
|
|
|
_, err := ftun.Write(data, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tun.Close()
|
|
|
|
_, err = ftun.Write(data, 0)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error from ftun.Write() after Close()")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 15:19:03 +00:00
|
|
|
func BenchmarkWrite(b *testing.B) {
|
2021-08-31 03:43:18 +00:00
|
|
|
b.ReportAllocs()
|
2020-06-05 15:19:03 +00:00
|
|
|
ftun, tun := newFakeTUN(b.Logf, true)
|
|
|
|
defer tun.Close()
|
|
|
|
|
2022-12-09 01:58:14 +00:00
|
|
|
packet := [][]byte{udp4("5.6.7.8", "1.2.3.4", 89, 89)}
|
2024-04-16 20:15:13 +00:00
|
|
|
for range b.N {
|
2020-10-06 02:36:48 +00:00
|
|
|
_, err := ftun.Write(packet, 0)
|
2020-06-05 15:19:03 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Errorf("err = %v; want nil", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 21:19:12 +00:00
|
|
|
|
|
|
|
func TestAtomic64Alignment(t *testing.T) {
|
2021-03-27 06:13:20 +00:00
|
|
|
off := unsafe.Offsetof(Wrapper{}.lastActivityAtomic)
|
2020-06-25 21:19:12 +00:00
|
|
|
if off%8 != 0 {
|
|
|
|
t.Errorf("offset %v not 8-byte aligned", off)
|
|
|
|
}
|
|
|
|
|
2021-03-27 06:13:20 +00:00
|
|
|
c := new(Wrapper)
|
2021-07-21 17:43:53 +00:00
|
|
|
c.lastActivityAtomic.StoreAtomic(mono.Now())
|
2020-06-25 21:19:12 +00:00
|
|
|
}
|
2021-04-07 18:32:53 +00:00
|
|
|
|
|
|
|
func TestPeerAPIBypass(t *testing.T) {
|
|
|
|
wrapperWithPeerAPI := &Wrapper{
|
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
|
|
|
PeerAPIPort: func(ip netip.Addr) (port uint16, ok bool) {
|
2022-07-26 03:55:44 +00:00
|
|
|
if ip == netip.MustParseAddr("100.64.1.2") {
|
2021-04-07 18:32:53 +00:00
|
|
|
return 60000, true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
w *Wrapper
|
|
|
|
filter *filter.Filter
|
|
|
|
pkt []byte
|
|
|
|
want filter.Response
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "reject_nil_filter",
|
|
|
|
w: &Wrapper{
|
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
|
|
|
PeerAPIPort: func(netip.Addr) (port uint16, ok bool) {
|
2021-04-07 18:32:53 +00:00
|
|
|
return 60000, true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pkt: tcp4syn("1.2.3.4", "100.64.1.2", 1234, 60000),
|
|
|
|
want: filter.Drop,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reject_with_filter",
|
|
|
|
w: &Wrapper{},
|
2022-07-25 03:08:42 +00:00
|
|
|
filter: filter.NewAllowNone(logger.Discard, new(netipx.IPSet)),
|
2021-04-07 18:32:53 +00:00
|
|
|
pkt: tcp4syn("1.2.3.4", "100.64.1.2", 1234, 60000),
|
|
|
|
want: filter.Drop,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "peerapi_bypass_filter",
|
|
|
|
w: wrapperWithPeerAPI,
|
2022-07-25 03:08:42 +00:00
|
|
|
filter: filter.NewAllowNone(logger.Discard, new(netipx.IPSet)),
|
2021-04-07 18:32:53 +00:00
|
|
|
pkt: tcp4syn("1.2.3.4", "100.64.1.2", 1234, 60000),
|
|
|
|
want: filter.Accept,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "peerapi_dont_bypass_filter_wrong_port",
|
|
|
|
w: wrapperWithPeerAPI,
|
2022-07-25 03:08:42 +00:00
|
|
|
filter: filter.NewAllowNone(logger.Discard, new(netipx.IPSet)),
|
2021-04-07 18:32:53 +00:00
|
|
|
pkt: tcp4syn("1.2.3.4", "100.64.1.2", 1234, 60001),
|
|
|
|
want: filter.Drop,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "peerapi_dont_bypass_filter_wrong_dst_ip",
|
|
|
|
w: wrapperWithPeerAPI,
|
2022-07-25 03:08:42 +00:00
|
|
|
filter: filter.NewAllowNone(logger.Discard, new(netipx.IPSet)),
|
2021-04-07 18:32:53 +00:00
|
|
|
pkt: tcp4syn("1.2.3.4", "100.64.1.3", 1234, 60000),
|
|
|
|
want: filter.Drop,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-12-09 11:46:37 +00:00
|
|
|
p := new(packet.Parsed)
|
|
|
|
p.Decode(tt.pkt)
|
2021-04-07 18:32:53 +00:00
|
|
|
tt.w.SetFilter(tt.filter)
|
|
|
|
tt.w.disableTSMPRejected = true
|
2022-12-22 20:51:06 +00:00
|
|
|
tt.w.logf = t.Logf
|
2024-05-06 18:01:47 +00:00
|
|
|
if got := tt.w.filterPacketInboundFromWireGuard(p, nil, nil); got != tt.want {
|
2021-04-07 18:32:53 +00:00
|
|
|
t.Errorf("got = %v; want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-09-13 21:21:40 +00:00
|
|
|
|
|
|
|
// Issue 1526: drop disco frames from ourselves.
|
|
|
|
func TestFilterDiscoLoop(t *testing.T) {
|
|
|
|
var memLog tstest.MemLogger
|
2021-11-02 21:41:56 +00:00
|
|
|
discoPub := key.DiscoPublicFromRaw32(mem.B([]byte{1: 1, 2: 2, 31: 0}))
|
2021-11-23 19:20:33 +00:00
|
|
|
tw := &Wrapper{logf: memLog.Logf, limitedLogf: memLog.Logf}
|
2021-09-13 21:21:40 +00:00
|
|
|
tw.SetDiscoKey(discoPub)
|
|
|
|
uh := packet.UDP4Header{
|
|
|
|
IP4Header: packet.IP4Header{
|
|
|
|
IPProto: ipproto.UDP,
|
|
|
|
Src: netaddr.IPv4(1, 2, 3, 4),
|
|
|
|
Dst: netaddr.IPv4(5, 6, 7, 8),
|
|
|
|
},
|
|
|
|
SrcPort: 9,
|
|
|
|
DstPort: 10,
|
|
|
|
}
|
2021-11-02 21:41:56 +00:00
|
|
|
discobs := discoPub.Raw32()
|
|
|
|
discoPayload := fmt.Sprintf("%s%s%s", disco.Magic, discobs[:], [disco.NonceLen]byte{})
|
2021-09-13 21:21:40 +00:00
|
|
|
pkt := make([]byte, uh.Len()+len(discoPayload))
|
|
|
|
uh.Marshal(pkt)
|
|
|
|
copy(pkt[uh.Len():], discoPayload)
|
|
|
|
|
2022-12-09 11:46:37 +00:00
|
|
|
p := new(packet.Parsed)
|
|
|
|
p.Decode(pkt)
|
2024-05-06 18:01:47 +00:00
|
|
|
got := tw.filterPacketInboundFromWireGuard(p, nil, nil)
|
2021-09-13 21:21:40 +00:00
|
|
|
if got != filter.DropSilently {
|
|
|
|
t.Errorf("got %v; want DropSilently", got)
|
|
|
|
}
|
2021-09-29 20:58:14 +00:00
|
|
|
if got, want := memLog.String(), "[unexpected] received self disco in packet over tstun; dropping\n"; got != want {
|
|
|
|
t.Errorf("log output mismatch\n got: %q\nwant: %q\n", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
memLog.Reset()
|
|
|
|
pp := new(packet.Parsed)
|
|
|
|
pp.Decode(pkt)
|
2024-05-06 18:01:47 +00:00
|
|
|
got = tw.filterPacketOutboundToWireGuard(pp, nil)
|
2021-09-29 20:58:14 +00:00
|
|
|
if got != filter.DropSilently {
|
|
|
|
t.Errorf("got %v; want DropSilently", got)
|
|
|
|
}
|
|
|
|
if got, want := memLog.String(), "[unexpected] received self disco out packet over tstun; dropping\n"; got != want {
|
2021-09-13 21:21:40 +00:00
|
|
|
t.Errorf("log output mismatch\n got: %q\nwant: %q\n", got, want)
|
|
|
|
}
|
|
|
|
}
|
2022-12-09 20:13:41 +00:00
|
|
|
|
2024-04-30 22:29:27 +00:00
|
|
|
// TODO(andrew-d): refactor this test to no longer use addrFam, after #11945
|
2024-04-30 22:33:59 +00:00
|
|
|
// removed it in peerConfigFromWGConfig
|
|
|
|
func TestPeerCfg_NAT(t *testing.T) {
|
2023-05-02 19:12:44 +00:00
|
|
|
node := func(ip, masqIP netip.Addr, otherAllowedIPs ...netip.Prefix) wgcfg.Peer {
|
2023-03-29 16:51:18 +00:00
|
|
|
p := wgcfg.Peer{
|
|
|
|
PublicKey: key.NewNode().Public(),
|
|
|
|
AllowedIPs: []netip.Prefix{
|
2022-12-09 20:13:41 +00:00
|
|
|
netip.PrefixFrom(ip, ip.BitLen()),
|
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
}
|
|
|
|
if masqIP.Is4() {
|
|
|
|
p.V4MasqAddr = ptr.To(masqIP)
|
|
|
|
} else {
|
|
|
|
p.V6MasqAddr = ptr.To(masqIP)
|
2022-12-09 20:13:41 +00:00
|
|
|
}
|
2023-03-29 16:51:18 +00:00
|
|
|
p.AllowedIPs = append(p.AllowedIPs, otherAllowedIPs...)
|
|
|
|
return p
|
2022-12-09 20:13:41 +00:00
|
|
|
}
|
2023-10-11 05:52:34 +00:00
|
|
|
test := func(addrFam ipproto.Version) {
|
2023-10-03 18:55:06 +00:00
|
|
|
var (
|
|
|
|
noIP netip.Addr
|
2022-12-09 20:13:41 +00:00
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
selfNativeIP = netip.MustParseAddr("100.64.0.1")
|
|
|
|
selfEIP1 = netip.MustParseAddr("100.64.1.1")
|
|
|
|
selfEIP2 = netip.MustParseAddr("100.64.1.2")
|
|
|
|
selfAddrs = []netip.Prefix{netip.PrefixFrom(selfNativeIP, selfNativeIP.BitLen())}
|
2022-12-09 20:13:41 +00:00
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
peer1IP = netip.MustParseAddr("100.64.0.2")
|
|
|
|
peer2IP = netip.MustParseAddr("100.64.0.3")
|
2022-12-09 20:13:41 +00:00
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
subnet = netip.MustParsePrefix("192.168.0.0/24")
|
|
|
|
subnetIP = netip.MustParseAddr("192.168.0.1")
|
2023-03-29 16:51:18 +00:00
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
exitRoute = netip.MustParsePrefix("0.0.0.0/0")
|
|
|
|
publicIP = netip.MustParseAddr("8.8.8.8")
|
|
|
|
)
|
2023-10-11 05:52:34 +00:00
|
|
|
if addrFam == ipproto.Version6 {
|
2023-10-03 18:55:06 +00:00
|
|
|
selfNativeIP = netip.MustParseAddr("fd7a:115c:a1e0::a")
|
|
|
|
selfEIP1 = netip.MustParseAddr("fd7a:115c:a1e0::1a")
|
|
|
|
selfEIP2 = netip.MustParseAddr("fd7a:115c:a1e0::1b")
|
|
|
|
selfAddrs = []netip.Prefix{netip.PrefixFrom(selfNativeIP, selfNativeIP.BitLen())}
|
2022-12-09 20:13:41 +00:00
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
peer1IP = netip.MustParseAddr("fd7a:115c:a1e0::b")
|
|
|
|
peer2IP = netip.MustParseAddr("fd7a:115c:a1e0::c")
|
|
|
|
|
|
|
|
subnet = netip.MustParsePrefix("2001:db8::/32")
|
|
|
|
subnetIP = netip.MustParseAddr("2001:db8::FFFF")
|
|
|
|
|
|
|
|
exitRoute = netip.MustParsePrefix("::/0")
|
|
|
|
publicIP = netip.MustParseAddr("2001:4860:4860::8888")
|
|
|
|
}
|
|
|
|
|
2024-05-06 18:01:47 +00:00
|
|
|
type dnatTest struct {
|
|
|
|
src netip.Addr
|
|
|
|
dst netip.Addr
|
|
|
|
want netip.Addr // new destination after DNAT
|
|
|
|
}
|
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
wcfg *wgcfg.Config
|
|
|
|
snatMap map[netip.Addr]netip.Addr // dst -> src
|
2024-05-06 18:01:47 +00:00
|
|
|
dnat []dnatTest
|
2023-10-03 18:55:06 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no-cfg",
|
|
|
|
wcfg: nil,
|
|
|
|
snatMap: map[netip.Addr]netip.Addr{
|
|
|
|
peer1IP: selfNativeIP,
|
|
|
|
peer2IP: selfNativeIP,
|
|
|
|
subnetIP: selfNativeIP,
|
2022-12-09 20:13:41 +00:00
|
|
|
},
|
2024-05-06 18:01:47 +00:00
|
|
|
dnat: []dnatTest{
|
|
|
|
{selfNativeIP, selfNativeIP, selfNativeIP},
|
|
|
|
{peer1IP, selfEIP1, selfEIP1},
|
|
|
|
{peer2IP, selfEIP2, selfEIP2},
|
2022-12-09 20:13:41 +00:00
|
|
|
},
|
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
{
|
|
|
|
name: "single-peer-requires-nat",
|
|
|
|
wcfg: &wgcfg.Config{
|
|
|
|
Addresses: selfAddrs,
|
|
|
|
Peers: []wgcfg.Peer{
|
|
|
|
node(peer1IP, noIP),
|
2024-05-06 18:01:47 +00:00
|
|
|
node(peer2IP, selfEIP2),
|
2023-10-03 18:55:06 +00:00
|
|
|
},
|
2023-03-29 16:51:18 +00:00
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
snatMap: map[netip.Addr]netip.Addr{
|
|
|
|
peer1IP: selfNativeIP,
|
2024-05-06 18:01:47 +00:00
|
|
|
peer2IP: selfEIP2,
|
2023-10-03 18:55:06 +00:00
|
|
|
subnetIP: selfNativeIP,
|
2023-03-29 16:51:18 +00:00
|
|
|
},
|
2024-05-06 18:01:47 +00:00
|
|
|
dnat: []dnatTest{
|
|
|
|
{selfNativeIP, selfNativeIP, selfNativeIP},
|
|
|
|
{peer1IP, selfEIP1, selfEIP1},
|
|
|
|
{peer2IP, selfEIP2, selfNativeIP}, // NATed
|
|
|
|
{peer2IP, subnetIP, subnetIP},
|
2022-12-09 20:13:41 +00:00
|
|
|
},
|
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
{
|
|
|
|
name: "multiple-peers-require-nat",
|
|
|
|
wcfg: &wgcfg.Config{
|
|
|
|
Addresses: selfAddrs,
|
|
|
|
Peers: []wgcfg.Peer{
|
|
|
|
node(peer1IP, selfEIP1),
|
|
|
|
node(peer2IP, selfEIP2),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
snatMap: map[netip.Addr]netip.Addr{
|
|
|
|
peer1IP: selfEIP1,
|
|
|
|
peer2IP: selfEIP2,
|
|
|
|
subnetIP: selfNativeIP,
|
|
|
|
},
|
2024-05-06 18:01:47 +00:00
|
|
|
dnat: []dnatTest{
|
|
|
|
{selfNativeIP, selfNativeIP, selfNativeIP},
|
|
|
|
{peer1IP, selfEIP1, selfNativeIP},
|
|
|
|
{peer2IP, selfEIP2, selfNativeIP},
|
|
|
|
{peer2IP, subnetIP, subnetIP},
|
2023-10-03 18:55:06 +00:00
|
|
|
},
|
2022-12-09 20:13:41 +00:00
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
{
|
|
|
|
name: "multiple-peers-require-nat-with-subnet",
|
|
|
|
wcfg: &wgcfg.Config{
|
|
|
|
Addresses: selfAddrs,
|
|
|
|
Peers: []wgcfg.Peer{
|
|
|
|
node(peer1IP, selfEIP1),
|
|
|
|
node(peer2IP, selfEIP2, subnet),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
snatMap: map[netip.Addr]netip.Addr{
|
|
|
|
peer1IP: selfEIP1,
|
|
|
|
peer2IP: selfEIP2,
|
|
|
|
subnetIP: selfEIP2,
|
|
|
|
},
|
2024-05-06 18:01:47 +00:00
|
|
|
dnat: []dnatTest{
|
|
|
|
{selfNativeIP, selfNativeIP, selfNativeIP},
|
|
|
|
{peer1IP, selfEIP1, selfNativeIP},
|
|
|
|
{peer2IP, selfEIP2, selfNativeIP},
|
|
|
|
{peer2IP, subnetIP, subnetIP},
|
2023-10-03 18:55:06 +00:00
|
|
|
},
|
2023-05-02 19:12:44 +00:00
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
{
|
|
|
|
name: "multiple-peers-require-nat-with-default-route",
|
|
|
|
wcfg: &wgcfg.Config{
|
|
|
|
Addresses: selfAddrs,
|
|
|
|
Peers: []wgcfg.Peer{
|
|
|
|
node(peer1IP, selfEIP1),
|
|
|
|
node(peer2IP, selfEIP2, exitRoute),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
snatMap: map[netip.Addr]netip.Addr{
|
|
|
|
peer1IP: selfEIP1,
|
|
|
|
peer2IP: selfEIP2,
|
|
|
|
publicIP: selfEIP2,
|
|
|
|
},
|
2024-05-06 18:01:47 +00:00
|
|
|
dnat: []dnatTest{
|
|
|
|
{selfNativeIP, selfNativeIP, selfNativeIP},
|
|
|
|
{peer1IP, selfEIP1, selfNativeIP},
|
|
|
|
{peer2IP, selfEIP2, selfNativeIP},
|
|
|
|
{peer2IP, subnetIP, subnetIP},
|
2023-05-02 19:12:44 +00:00
|
|
|
},
|
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
{
|
|
|
|
name: "no-nat",
|
|
|
|
wcfg: &wgcfg.Config{
|
|
|
|
Addresses: selfAddrs,
|
|
|
|
Peers: []wgcfg.Peer{
|
|
|
|
node(peer1IP, noIP),
|
|
|
|
node(peer2IP, noIP),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
snatMap: map[netip.Addr]netip.Addr{
|
|
|
|
peer1IP: selfNativeIP,
|
|
|
|
peer2IP: selfNativeIP,
|
|
|
|
subnetIP: selfNativeIP,
|
|
|
|
},
|
2024-05-06 18:01:47 +00:00
|
|
|
dnat: []dnatTest{
|
|
|
|
{selfNativeIP, selfNativeIP, selfNativeIP},
|
|
|
|
{peer1IP, selfEIP1, selfEIP1},
|
|
|
|
{peer2IP, selfEIP2, selfEIP2},
|
|
|
|
{peer2IP, subnetIP, subnetIP},
|
2023-10-03 18:55:06 +00:00
|
|
|
},
|
2023-05-02 19:12:44 +00:00
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
{
|
|
|
|
name: "exit-node-require-nat-peer-doesnt",
|
|
|
|
wcfg: &wgcfg.Config{
|
|
|
|
Addresses: selfAddrs,
|
|
|
|
Peers: []wgcfg.Peer{
|
|
|
|
node(peer1IP, noIP),
|
|
|
|
node(peer2IP, selfEIP2, exitRoute),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
snatMap: map[netip.Addr]netip.Addr{
|
|
|
|
peer1IP: selfNativeIP,
|
|
|
|
peer2IP: selfEIP2,
|
|
|
|
publicIP: selfEIP2,
|
|
|
|
},
|
2024-05-06 18:01:47 +00:00
|
|
|
dnat: []dnatTest{
|
|
|
|
{selfNativeIP, selfNativeIP, selfNativeIP},
|
|
|
|
{peer2IP, selfEIP2, selfNativeIP},
|
|
|
|
{peer2IP, subnetIP, subnetIP},
|
2023-10-03 18:55:06 +00:00
|
|
|
},
|
2022-12-09 20:13:41 +00:00
|
|
|
},
|
2023-10-03 18:55:06 +00:00
|
|
|
}
|
2022-12-09 20:13:41 +00:00
|
|
|
|
2023-10-03 18:55:06 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%v/%v", addrFam, tc.name), func(t *testing.T) {
|
2024-05-06 18:01:47 +00:00
|
|
|
pcfg := peerConfigTableFromWGConfig(tc.wcfg)
|
2023-10-03 18:55:06 +00:00
|
|
|
for peer, want := range tc.snatMap {
|
2024-04-30 22:33:59 +00:00
|
|
|
if got := pcfg.selectSrcIP(selfNativeIP, peer); got != want {
|
2023-10-03 18:55:06 +00:00
|
|
|
t.Errorf("selectSrcIP[%v]: got %v; want %v", peer, got, want)
|
|
|
|
}
|
2022-12-09 20:13:41 +00:00
|
|
|
}
|
2024-05-06 18:01:47 +00:00
|
|
|
for i, dt := range tc.dnat {
|
|
|
|
if got := pcfg.mapDstIP(dt.src, dt.dst); got != dt.want {
|
|
|
|
t.Errorf("dnat[%d]: mapDstIP[%v, %v]: got %v; want %v", i, dt.src, dt.dst, got, dt.want)
|
2023-10-03 18:55:06 +00:00
|
|
|
}
|
2022-12-09 20:13:41 +00:00
|
|
|
}
|
2023-10-03 18:55:06 +00:00
|
|
|
if t.Failed() {
|
2024-04-30 22:33:59 +00:00
|
|
|
t.Logf("%v", pcfg)
|
2023-10-03 18:55:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-12-09 20:13:41 +00:00
|
|
|
}
|
2023-10-11 05:52:34 +00:00
|
|
|
test(ipproto.Version4)
|
|
|
|
test(ipproto.Version6)
|
2022-12-09 20:13:41 +00:00
|
|
|
}
|
2023-04-21 14:27:15 +00:00
|
|
|
|
|
|
|
// TestCaptureHook verifies that the Wrapper.captureHook callback is called
|
|
|
|
// with the correct parameters when various packet operations are performed.
|
|
|
|
func TestCaptureHook(t *testing.T) {
|
|
|
|
type captureRecord struct {
|
|
|
|
path capture.Path
|
|
|
|
now time.Time
|
|
|
|
pkt []byte
|
|
|
|
meta packet.CaptureMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
var captured []captureRecord
|
|
|
|
hook := func(path capture.Path, now time.Time, pkt []byte, meta packet.CaptureMeta) {
|
|
|
|
captured = append(captured, captureRecord{
|
|
|
|
path: path,
|
|
|
|
now: now,
|
|
|
|
pkt: pkt,
|
|
|
|
meta: meta,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Unix(1682085856, 0)
|
|
|
|
|
|
|
|
_, w := newFakeTUN(t.Logf, true)
|
|
|
|
w.timeNow = func() time.Time {
|
|
|
|
return now
|
|
|
|
}
|
|
|
|
w.InstallCaptureHook(hook)
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
// Loop reading and discarding packets; this ensures that we don't have
|
|
|
|
// packets stuck in vectorOutbound
|
|
|
|
go func() {
|
|
|
|
var (
|
|
|
|
buf [MaxPacketSize]byte
|
|
|
|
sizes = make([]int, 1)
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
_, err := w.Read([][]byte{buf[:]}, sizes, 0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Do operations that should result in a packet being captured.
|
|
|
|
w.Write([][]byte{
|
|
|
|
[]byte("Write1"),
|
|
|
|
[]byte("Write2"),
|
|
|
|
}, 0)
|
|
|
|
packetBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{
|
2023-09-06 09:45:52 +00:00
|
|
|
Payload: buffer.MakeWithData([]byte("InjectInboundPacketBuffer")),
|
2023-04-21 14:27:15 +00:00
|
|
|
})
|
|
|
|
w.InjectInboundPacketBuffer(packetBuf)
|
|
|
|
|
|
|
|
packetBuf = stack.NewPacketBuffer(stack.PacketBufferOptions{
|
2023-09-06 09:45:52 +00:00
|
|
|
Payload: buffer.MakeWithData([]byte("InjectOutboundPacketBuffer")),
|
2023-04-21 14:27:15 +00:00
|
|
|
})
|
|
|
|
w.InjectOutboundPacketBuffer(packetBuf)
|
|
|
|
|
|
|
|
// TODO: test Read
|
|
|
|
// TODO: determine if we want InjectOutbound to log
|
|
|
|
|
|
|
|
// Assert that the right packets are captured.
|
|
|
|
want := []captureRecord{
|
|
|
|
{
|
|
|
|
path: capture.FromPeer,
|
|
|
|
pkt: []byte("Write1"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: capture.FromPeer,
|
|
|
|
pkt: []byte("Write2"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: capture.SynthesizedToLocal,
|
|
|
|
pkt: []byte("InjectInboundPacketBuffer"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: capture.SynthesizedToPeer,
|
|
|
|
pkt: []byte("InjectOutboundPacketBuffer"),
|
|
|
|
},
|
|
|
|
}
|
2024-04-16 20:15:13 +00:00
|
|
|
for i := range len(want) {
|
2023-04-21 14:27:15 +00:00
|
|
|
want[i].now = now
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(captured, want) {
|
|
|
|
t.Errorf("mismatch between captured and expected packets\ngot: %+v\nwant: %+v",
|
|
|
|
captured, want)
|
|
|
|
}
|
|
|
|
}
|