mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-16 08:36:52 +01:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6826c04103 |
No files matched your search
@@ -14,8 +14,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
- Signing a v2 certificate with an empty name, a name longer than 253 bytes, or an empty group now fails.
|
|
||||||
These certificates were signed successfully but could not be loaded.
|
|
||||||
- `create-tunnel -address <ip:port>` panicked on a nil RemoteList and, over the ssh debug server, took nebula
|
- `create-tunnel -address <ip:port>` panicked on a nil RemoteList and, over the ssh debug server, took nebula
|
||||||
down with it. A hostinfo returned by StartHandshake had no RemoteList until the handshake loop's first
|
down with it. A hostinfo returned by StartHandshake had no RemoteList until the handshake loop's first
|
||||||
pass attached one, and `create-tunnel -address` set the remote before that. The list is now attached when
|
pass attached one, and `create-tunnel -address` set the remote before that. The list is now attached when
|
||||||
|
|||||||
+1
-10
@@ -389,16 +389,7 @@ func (c *certificateV2) fromTBSCertificate(t *TBSCertificate) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *certificateV2) validate() error {
|
func (c *certificateV2) validate() error {
|
||||||
// Mirror the name and group rules in unmarshalDetails so signing fails instead of producing an unparseable certificate
|
// Empty names are allowed
|
||||||
if c.details.name == "" || len(c.details.name) > MaxNameLength {
|
|
||||||
return NewErrInvalidCertificateProperties("name must be between 1 and %d bytes", MaxNameLength)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, group := range c.details.groups {
|
|
||||||
if group == "" {
|
|
||||||
return NewErrInvalidCertificateProperties("groups may not be empty")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(c.publicKey) == 0 {
|
if len(c.publicKey) == 0 {
|
||||||
return ErrInvalidPublicKey
|
return ErrInvalidPublicKey
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"crypto/elliptic"
|
"crypto/elliptic"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -136,47 +135,3 @@ func TestCertificate_SignP256_AlwaysNormalized(t *testing.T) {
|
|||||||
assert.True(t, normie)
|
assert.True(t, normie)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCertificateV2_SignRejectsUnparseable(t *testing.T) {
|
|
||||||
_, priv, err := ed25519.GenerateKey(rand.Reader)
|
|
||||||
require.NoError(t, err)
|
|
||||||
var invalid *ErrInvalidCertificateProperties
|
|
||||||
|
|
||||||
newTBS := func() TBSCertificate {
|
|
||||||
return TBSCertificate{
|
|
||||||
Version: Version2,
|
|
||||||
Name: "testing",
|
|
||||||
Networks: []netip.Prefix{mustParsePrefixUnmapped("10.1.1.1/24")},
|
|
||||||
Groups: []string{"test-group1"},
|
|
||||||
NotBefore: time.Now().Add(-time.Minute),
|
|
||||||
NotAfter: time.Now().Add(time.Minute),
|
|
||||||
PublicKey: []byte("1234567890abcedfghij1234567890ab"),
|
|
||||||
Curve: Curve_CURVE25519,
|
|
||||||
IsCA: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tbs := newTBS()
|
|
||||||
tbs.Name = strings.Repeat("a", MaxNameLength)
|
|
||||||
c, err := tbs.Sign(nil, Curve_CURVE25519, priv)
|
|
||||||
require.NoError(t, err)
|
|
||||||
b, err := c.Marshal()
|
|
||||||
require.NoError(t, err)
|
|
||||||
_, err = unmarshalCertificateV2(b, nil, Curve_CURVE25519)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
tbs = newTBS()
|
|
||||||
tbs.Name = strings.Repeat("a", MaxNameLength+1)
|
|
||||||
_, err = tbs.Sign(nil, Curve_CURVE25519, priv)
|
|
||||||
require.ErrorAs(t, err, &invalid)
|
|
||||||
|
|
||||||
tbs = newTBS()
|
|
||||||
tbs.Name = ""
|
|
||||||
_, err = tbs.Sign(nil, Curve_CURVE25519, priv)
|
|
||||||
require.ErrorAs(t, err, &invalid)
|
|
||||||
|
|
||||||
tbs = newTBS()
|
|
||||||
tbs.Groups = []string{"test-group1", ""}
|
|
||||||
_, err = tbs.Sign(nil, Curve_CURVE25519, priv)
|
|
||||||
require.ErrorAs(t, err, &invalid)
|
|
||||||
}
|
|
||||||
@@ -18,12 +18,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (f *Interface) consumeInsidePacket(pkt tio.Packet, fwPacket *firewall.ParsedPacket, nb []byte, sendBatch *batch.SendBatch, rejectBuf []byte, q int, localCache firewall.ConntrackCache) {
|
func (f *Interface) consumeInsidePacket(pkt tio.Packet, fwPacket *firewall.ParsedPacket, nb []byte, sendBatch *batch.SendBatch, rejectBuf []byte, q int, localCache firewall.ConntrackCache) {
|
||||||
|
if hostinfo := f.filterInsidePacket(pkt, fwPacket, rejectBuf, q, localCache); hostinfo != nil {
|
||||||
|
f.sendInsideMessage(hostinfo, pkt, nb, sendBatch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// filterInsidePacket parses and filters pkt, handling local traffic, destination lookup, pending handshakes and
|
||||||
|
// the outbound firewall. It returns the hostinfo to send pkt to, or nil when pkt was dropped, rejected, forwarded
|
||||||
|
// to self or cached for a pending handshake.
|
||||||
|
func (f *Interface) filterInsidePacket(pkt tio.Packet, fwPacket *firewall.ParsedPacket, rejectBuf []byte, q int, localCache firewall.ConntrackCache) *HostInfo {
|
||||||
// borrowed: pkt.Bytes is owned by the originating tio.Queue and is
|
// borrowed: pkt.Bytes is owned by the originating tio.Queue and is
|
||||||
// only valid until the next Read on that queue. Every consumer below
|
// only valid until the next Read on that queue. Filtering and sending
|
||||||
// (parse, self-forward, handshake cache, sendInsideMessage) reads it
|
// must finish before then. Self-forwarding writes synchronously and the
|
||||||
// synchronously; do not retain pkt outside this call. If a future
|
// handshake cache copies each segment. To retain the packet beyond the
|
||||||
// caller needs to keep the packet, use pkt.Clone() to detach it from
|
// next Read, use pkt.Clone() to detach it from the borrow.
|
||||||
// the borrow.
|
|
||||||
//
|
//
|
||||||
// pkt.Bytes is either one IP datagram (GSO zero) or a TSO/USO
|
// pkt.Bytes is either one IP datagram (GSO zero) or a TSO/USO
|
||||||
// superpacket. In both cases the L3+L4 headers at the start describe
|
// superpacket. In both cases the L3+L4 headers at the start describe
|
||||||
@@ -38,13 +46,13 @@ func (f *Interface) consumeInsidePacket(pkt tio.Packet, fwPacket *firewall.Parse
|
|||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore local broadcast packets
|
// Ignore local broadcast packets
|
||||||
if f.dropLocalBroadcast {
|
if f.dropLocalBroadcast {
|
||||||
if f.myBroadcastAddrsTable.Contains(fwPacket.RemoteAddr) {
|
if f.myBroadcastAddrsTable.Contains(fwPacket.RemoteAddr) {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,12 +79,12 @@ func (f *Interface) consumeInsidePacket(pkt tio.Packet, fwPacket *firewall.Parse
|
|||||||
}
|
}
|
||||||
// Otherwise, drop. On linux, we should never see these packets - Linux
|
// Otherwise, drop. On linux, we should never see these packets - Linux
|
||||||
// routes packets from the nebula addr to the nebula addr through the loopback device.
|
// routes packets from the nebula addr to the nebula addr through the loopback device.
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore multicast packets
|
// Ignore multicast packets
|
||||||
if f.dropMulticast && fwPacket.RemoteAddr.IsMulticast() {
|
if f.dropMulticast && fwPacket.RemoteAddr.IsMulticast() {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
hostinfo, ready := f.getOrHandshakeConsiderRouting(&fwPacket.Packet, func(hh *HandshakeHostInfo) {
|
hostinfo, ready := f.getOrHandshakeConsiderRouting(&fwPacket.Packet, func(hh *HandshakeHostInfo) {
|
||||||
@@ -103,17 +111,18 @@ func (f *Interface) consumeInsidePacket(pkt tio.Packet, fwPacket *firewall.Parse
|
|||||||
"fwPacket", fwPacket,
|
"fwPacket", fwPacket,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ready {
|
if !ready {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
dropReason := f.firewall.Drop(fwPacket.Packet, false, hostinfo, f.pki.GetCAPool(), localCache)
|
dropReason := f.firewall.Drop(fwPacket.Packet, false, hostinfo, f.pki.GetCAPool(), localCache)
|
||||||
if dropReason == nil {
|
if dropReason == nil {
|
||||||
f.sendInsideMessage(hostinfo, pkt, nb, sendBatch)
|
return hostinfo
|
||||||
} else {
|
}
|
||||||
|
|
||||||
f.rejectInside(packet, rejectBuf, q)
|
f.rejectInside(packet, rejectBuf, q)
|
||||||
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
hostinfo.logger(f.l).Debug("dropping outbound packet",
|
hostinfo.logger(f.l).Debug("dropping outbound packet",
|
||||||
@@ -121,7 +130,7 @@ func (f *Interface) consumeInsidePacket(pkt tio.Packet, fwPacket *firewall.Parse
|
|||||||
"reason", dropReason,
|
"reason", dropReason,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Interface) sendInsideEncrypt(hostinfo *HostInfo, ci *ConnectionState, seg, scratch, nb []byte) []byte {
|
func (f *Interface) sendInsideEncrypt(hostinfo *HostInfo, ci *ConnectionState, seg, scratch, nb []byte) []byte {
|
||||||
|
|||||||
@@ -0,0 +1,180 @@
|
|||||||
|
package nebula
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/netip"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gaissmai/bart"
|
||||||
|
"github.com/slackhq/nebula/cert"
|
||||||
|
"github.com/slackhq/nebula/firewall"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
"github.com/slackhq/nebula/overlay/batch"
|
||||||
|
"github.com/slackhq/nebula/overlay/overlaytest"
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
|
"github.com/slackhq/nebula/udp"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newInsideFilterTestInterface(t *testing.T, allow bool) (*Interface, *HostInfo, *capturingTun) {
|
||||||
|
t.Helper()
|
||||||
|
local := netip.MustParseAddr("192.0.2.1")
|
||||||
|
remote := netip.MustParseAddr("192.0.2.2")
|
||||||
|
f, tun := newSelfForwardInterface(local)
|
||||||
|
f.inside = overlaytest.NoopTun{}
|
||||||
|
f.myVpnNetworksTable = &bart.Lite{}
|
||||||
|
f.myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.0/24"))
|
||||||
|
f.myBroadcastAddrsTable.Insert(netip.MustParsePrefix("192.0.2.255/32"))
|
||||||
|
f.dropLocalBroadcast, f.dropMulticast = true, true
|
||||||
|
localCert := &dummyCert{version: cert.Version1, networks: []netip.Prefix{netip.PrefixFrom(local, 24)}}
|
||||||
|
peerCert := &dummyCert{version: cert.Version1, networks: []netip.Prefix{netip.PrefixFrom(remote, 24)}}
|
||||||
|
f.firewall = NewFirewall(f.l, time.Minute, time.Minute, time.Minute, localCert)
|
||||||
|
if allow {
|
||||||
|
require.NoError(t, f.firewall.AddRule(false, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
|
||||||
|
}
|
||||||
|
f.pki = &PKI{}
|
||||||
|
f.pki.caPool.Store(cert.NewCAPool())
|
||||||
|
f.hostMap = newHostMap(f.l)
|
||||||
|
f.lightHouse = newTestLighthouse()
|
||||||
|
f.lightHouse.amLighthouse = true // Keep destination lookup from probing other remotes.
|
||||||
|
f.handshakeManager = NewHandshakeManager(f.l, f.hostMap, f.lightHouse, udp.NoopConn{}, defaultHandshakeConfig)
|
||||||
|
f.handshakeManager.f = f
|
||||||
|
f.connectionManager = &connectionManager{intf: f}
|
||||||
|
h := &HostInfo{
|
||||||
|
vpnAddrs: []netip.Addr{remote},
|
||||||
|
ConnectionState: &ConnectionState{
|
||||||
|
peerCert: &cert.CachedCertificate{Certificate: peerCert},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
f.hostMap.Hosts[remote] = h
|
||||||
|
return f, h, tun
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterInsidePacket(t *testing.T) {
|
||||||
|
local := netip.MustParseAddr("192.0.2.1")
|
||||||
|
remote := netip.MustParseAddr("192.0.2.2")
|
||||||
|
for _, tt := range []struct {
|
||||||
|
name string
|
||||||
|
packet []byte
|
||||||
|
allow bool
|
||||||
|
reject bool
|
||||||
|
ready bool
|
||||||
|
writes int
|
||||||
|
}{
|
||||||
|
{name: "allowed", packet: buildIPv4(local, remote, tcpSyn), allow: true, ready: true},
|
||||||
|
{name: "malformed", packet: []byte{0x45}, allow: true},
|
||||||
|
{name: "broadcast", packet: buildIPv4(local, netip.MustParseAddr("192.0.2.255"), udpDatagram), allow: true},
|
||||||
|
{name: "multicast", packet: buildIPv4(local, netip.MustParseAddr("224.0.0.1"), udpDatagram), allow: true},
|
||||||
|
{name: "firewall drop", packet: buildIPv4(local, remote, tcpSyn)},
|
||||||
|
{name: "firewall reject", packet: buildIPv4(local, remote, tcpSyn), reject: true, writes: 1},
|
||||||
|
{name: "unroutable drop", packet: buildIPv4(local, netip.MustParseAddr("198.51.100.1"), tcpSyn), allow: true},
|
||||||
|
{name: "unroutable reject", packet: buildIPv4(local, netip.MustParseAddr("198.51.100.1"), tcpSyn), allow: true, reject: true, writes: 1},
|
||||||
|
} {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
f, h, tun := newInsideFilterTestInterface(t, tt.allow)
|
||||||
|
f.firewall.OutboundSendReject = tt.reject
|
||||||
|
fp := &firewall.ParsedPacket{}
|
||||||
|
got := f.filterInsidePacket(tio.Packet{Bytes: tt.packet}, fp, make([]byte, mtu), 0, nil)
|
||||||
|
if tt.ready {
|
||||||
|
require.Same(t, h, got)
|
||||||
|
assert.Equal(t, local, fp.LocalAddr)
|
||||||
|
assert.Equal(t, remote, fp.RemoteAddr)
|
||||||
|
} else {
|
||||||
|
require.Nil(t, got)
|
||||||
|
}
|
||||||
|
assert.Zero(t, h.ConnectionState.messageCounter.Load(), "filtering must not allocate encryption counters")
|
||||||
|
assert.False(t, h.sentSinceCheck(), "filtering must not mark outbound traffic")
|
||||||
|
require.Len(t, tun.writes, tt.writes)
|
||||||
|
if tt.writes > 0 {
|
||||||
|
require.GreaterOrEqual(t, len(tun.writes[0]), 40)
|
||||||
|
assert.Equal(t, tt.packet[16:20], tun.writes[0][12:16])
|
||||||
|
assert.Equal(t, tt.packet[12:16], tun.writes[0][16:20])
|
||||||
|
assert.NotZero(t, tun.writes[0][33]&0x04, "reject must carry a TCP reset")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterInsidePacketSelf(t *testing.T) {
|
||||||
|
local := netip.MustParseAddr("192.0.2.1")
|
||||||
|
f, _, tun := newInsideFilterTestInterface(t, true)
|
||||||
|
pkt := buildIPv4(local, local, tcpSyn)
|
||||||
|
require.Nil(t, f.filterInsidePacket(tio.Packet{Bytes: pkt}, &firewall.ParsedPacket{}, nil, 0, nil))
|
||||||
|
if immediatelyForwardToSelf {
|
||||||
|
require.Len(t, tun.writes, 1)
|
||||||
|
assert.Equal(t, pkt, tun.writes[0])
|
||||||
|
} else {
|
||||||
|
assert.Empty(t, tun.writes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterInsidePacketPendingHandshake(t *testing.T) {
|
||||||
|
f, h, tun := newInsideFilterTestInterface(t, true)
|
||||||
|
remote := h.vpnAddrs[0]
|
||||||
|
delete(f.hostMap.Hosts, remote)
|
||||||
|
hh := &HandshakeHostInfo{hostinfo: h}
|
||||||
|
f.handshakeManager.vpnIps[remote] = hh
|
||||||
|
pkt := buildIPv4(netip.MustParseAddr("192.0.2.1"), remote, udpDatagram)
|
||||||
|
want := append([]byte(nil), pkt...)
|
||||||
|
require.Nil(t, f.filterInsidePacket(tio.Packet{Bytes: pkt}, &firewall.ParsedPacket{}, nil, 0, nil))
|
||||||
|
require.Len(t, hh.packetStore, 1)
|
||||||
|
clear(pkt)
|
||||||
|
assert.Equal(t, want, hh.packetStore[0].packet, "the handshake cache must own its packet")
|
||||||
|
assert.Equal(t, header.Message, hh.packetStore[0].messageType)
|
||||||
|
assert.Empty(t, tun.writes)
|
||||||
|
assert.Zero(t, h.ConnectionState.messageCounter.Load())
|
||||||
|
}
|
||||||
|
|
||||||
|
type insideFilterConn struct {
|
||||||
|
udp.NoopConn
|
||||||
|
packets [][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *insideFilterConn) WriteBatch(bufs [][]byte, _ []netip.AddrPort) (int, error) {
|
||||||
|
for _, b := range bufs {
|
||||||
|
c.packets = append(c.packets, append([]byte(nil), b...))
|
||||||
|
}
|
||||||
|
return len(bufs), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConsumeInsidePacketFilterAndSend(t *testing.T) {
|
||||||
|
for _, allow := range []bool{false, true} {
|
||||||
|
t.Run(map[bool]string{false: "denied", true: "allowed"}[allow], func(t *testing.T) {
|
||||||
|
f, h, _ := newInsideFilterTestInterface(t, allow)
|
||||||
|
initR, respR := runTestHandshake(t)
|
||||||
|
ci, err := newConnectionStateFromResult(initR)
|
||||||
|
require.NoError(t, err)
|
||||||
|
rci, err := newConnectionStateFromResult(respR)
|
||||||
|
require.NoError(t, err)
|
||||||
|
ci.peerCert = h.ConnectionState.peerCert
|
||||||
|
h.ConnectionState = ci
|
||||||
|
h.remoteIndexId = 100
|
||||||
|
remote := netip.MustParseAddrPort("198.51.100.2:4242")
|
||||||
|
h.remote.Store(&remote)
|
||||||
|
conn := &insideFilterConn{}
|
||||||
|
sb := batch.NewSendBatch(conn, 1, mtu)
|
||||||
|
pkt := buildIPv4(netip.MustParseAddr("192.0.2.1"), h.vpnAddrs[0], udpDatagram)
|
||||||
|
before := ci.messageCounter.Load()
|
||||||
|
f.consumeInsidePacket(tio.Packet{Bytes: pkt}, &firewall.ParsedPacket{}, make([]byte, 12), sb, nil, 0, nil)
|
||||||
|
_, err = sb.Flush()
|
||||||
|
require.NoError(t, err)
|
||||||
|
if !allow {
|
||||||
|
assert.Empty(t, conn.packets)
|
||||||
|
assert.Equal(t, before, ci.messageCounter.Load())
|
||||||
|
assert.False(t, h.sentSinceCheck())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
require.Len(t, conn.packets, 1)
|
||||||
|
assert.True(t, h.sentSinceCheck())
|
||||||
|
hdr := &header.H{}
|
||||||
|
require.NoError(t, hdr.Parse(conn.packets[0]))
|
||||||
|
assert.Equal(t, header.Message, hdr.Type)
|
||||||
|
assert.Equal(t, h.remoteIndexId, hdr.RemoteIndex)
|
||||||
|
assert.Equal(t, before+1, hdr.MessageCounter)
|
||||||
|
plain, err := rci.Decrypt(f.l, hdr.MessageCounter, conn.packets[0], make([]byte, 12))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, pkt, plain)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -384,8 +384,6 @@ type rxContext struct {
|
|||||||
hostmapCache map[uint32]*HostInfo
|
hostmapCache map[uint32]*HostInfo
|
||||||
lhh *LightHouseHandler
|
lhh *LightHouseHandler
|
||||||
ctCache *firewall.ConntrackCacheTicker
|
ctCache *firewall.ConntrackCacheTicker
|
||||||
relayBatch *batch.SendBatch
|
|
||||||
relayRemote netip.AddrPort
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRxContext(f *Interface, q int) *rxContext {
|
func newRxContext(f *Interface, q int) *rxContext {
|
||||||
@@ -416,7 +414,6 @@ func (f *Interface) listenOut(i int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
flusher := func() {
|
flusher := func() {
|
||||||
f.flushRelayBatch(rxc)
|
|
||||||
if err := f.batchers[i].Flush(); err != nil {
|
if err := f.batchers[i].Flush(); err != nil {
|
||||||
f.l.Error("Failed to flush tun coalescer", "error", err)
|
f.l.Error("Failed to flush tun coalescer", "error", err)
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-35
@@ -25,7 +25,7 @@ var ErrOutOfWindow = errors.New("out of window packet")
|
|||||||
|
|
||||||
// readOutsidePackets processes one received underlay packet.
|
// readOutsidePackets processes one received underlay packet.
|
||||||
// Message payloads are decrypted IN PLACE, so packet must stay untouched
|
// Message payloads are decrypted IN PLACE, so packet must stay untouched
|
||||||
// by the caller until the TUN and relay batches for queue q have been flushed.
|
// by the caller until the batcher for queue q has been flushed
|
||||||
func (f *Interface) readOutsidePackets(via ViaSender, packet []byte, rxc *rxContext) {
|
func (f *Interface) readOutsidePackets(via ViaSender, packet []byte, rxc *rxContext) {
|
||||||
h := rxc.h
|
h := rxc.h
|
||||||
err := h.Parse(packet)
|
err := h.Parse(packet)
|
||||||
@@ -77,12 +77,6 @@ func (f *Interface) readOutsidePackets(via ViaSender, packet []byte, rxc *rxCont
|
|||||||
f.messageMetrics.Rx(h.Type, h.Subtype, 1)
|
f.messageMetrics.Rx(h.Type, h.Subtype, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Earlier forwards must leave before this packet can generate a response.
|
|
||||||
isMessageRelay := h.Type == header.Message && h.Subtype == header.MessageRelay
|
|
||||||
if !isMessageRelay {
|
|
||||||
f.flushRelayBatch(rxc)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unencrypted packets
|
// Unencrypted packets
|
||||||
switch h.Type {
|
switch h.Type {
|
||||||
case header.Handshake:
|
case header.Handshake:
|
||||||
@@ -94,6 +88,9 @@ func (f *Interface) readOutsidePackets(via ViaSender, packet []byte, rxc *rxCont
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Relay packets are special
|
||||||
|
isMessageRelay := (h.Type == header.Message && h.Subtype == header.MessageRelay)
|
||||||
|
|
||||||
var hostinfo *HostInfo
|
var hostinfo *HostInfo
|
||||||
if isMessageRelay {
|
if isMessageRelay {
|
||||||
hostinfo = f.hostMap.QueryRelayIndex(h.RemoteIndex)
|
hostinfo = f.hostMap.QueryRelayIndex(h.RemoteIndex)
|
||||||
@@ -105,8 +102,6 @@ func (f *Interface) readOutsidePackets(via ViaSender, packet []byte, rxc *rxCont
|
|||||||
// recvError if necessary
|
// recvError if necessary
|
||||||
if hostinfo == nil || hostinfo.ConnectionState == nil {
|
if hostinfo == nil || hostinfo.ConnectionState == nil {
|
||||||
if !via.IsRelayed {
|
if !via.IsRelayed {
|
||||||
// An unknown relay index can also produce a response.
|
|
||||||
f.flushRelayBatch(rxc)
|
|
||||||
f.maybeSendRecvError(via.UdpAddr, h.RemoteIndex)
|
f.maybeSendRecvError(via.UdpAddr, h.RemoteIndex)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@@ -238,10 +233,8 @@ func (f *Interface) handleOutsideRelayPacket(hostinfo *HostInfo, via ViaSender,
|
|||||||
// Forward this packet through the relay tunnel, rebuilding it in place.
|
// Forward this packet through the relay tunnel, rebuilding it in place.
|
||||||
// Encode overwrites the old outer header, and the new AEAD tag lands where the old one was
|
// Encode overwrites the old outer header, and the new AEAD tag lands where the old one was
|
||||||
fwdBuf := packet[:0]
|
fwdBuf := packet[:0]
|
||||||
out, err := f.prepareSendVia(targetHI, targetRelay, signedPayload, rxc.nb, fwdBuf, true)
|
//todo it would potentially be nice to batch these
|
||||||
if err == nil {
|
f.SendVia(targetHI, targetRelay, signedPayload, rxc.nb, fwdBuf, true, rxc.q)
|
||||||
f.queueRelayPacket(out, targetHI.GetRemote(), rxc)
|
|
||||||
}
|
|
||||||
case TerminalType:
|
case TerminalType:
|
||||||
hostinfo.logger(f.l).Error("Unexpected Relay Type of Terminal")
|
hostinfo.logger(f.l).Error("Unexpected Relay Type of Terminal")
|
||||||
return
|
return
|
||||||
@@ -266,27 +259,6 @@ func (f *Interface) handleOutsideRelayPacket(hostinfo *HostInfo, via ViaSender,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// queueRelayPacket borrows a receive buffer until the batch is flushed. Destination changes and the send batch
|
|
||||||
// limit end a run, preserving arrival order while allowing WriteBatch to use UDP segmentation offload.
|
|
||||||
func (f *Interface) queueRelayPacket(packet []byte, remote netip.AddrPort, rxc *rxContext) {
|
|
||||||
if rxc.relayBatch == nil {
|
|
||||||
rxc.relayBatch = batch.NewSendBatch(f.writers[rxc.q], batch.SendBatchCap, 0)
|
|
||||||
} else if rxc.relayRemote != remote {
|
|
||||||
f.flushRelayBatch(rxc)
|
|
||||||
}
|
|
||||||
rxc.relayRemote = remote
|
|
||||||
rxc.relayBatch.Commit(packet, remote)
|
|
||||||
if rxc.relayBatch.Len() >= batch.SendBatchCap {
|
|
||||||
f.flushRelayBatch(rxc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Interface) flushRelayBatch(rxc *rxContext) {
|
|
||||||
if rxc.relayBatch != nil {
|
|
||||||
f.flushSendBatch(rxc.relayBatch, rxc.q)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// closeTunnel closes a tunnel locally, it does not send a closeTunnel packet to the remote
|
// closeTunnel closes a tunnel locally, it does not send a closeTunnel packet to the remote
|
||||||
func (f *Interface) closeTunnel(hostInfo *HostInfo) {
|
func (f *Interface) closeTunnel(hostInfo *HostInfo) {
|
||||||
// Hold the hostmap lock across both the delete and the lighthouse eviction,
|
// Hold the hostmap lock across both the delete and the lighthouse eviction,
|
||||||
@@ -506,9 +478,9 @@ func (f *Interface) handleOutsideMessagePacket(hostinfo *HostInfo, messageCounte
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
hostinfo.logger(f.l).Debug("Error while validating inbound packet", "error", err, "packet", out)
|
hostinfo.logger(f.l).Debug("Error while validating inbound packet", "error", err, "packet", out)
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dropReason := f.firewall.Drop(rxc.fwPacket.Packet, true, hostinfo, f.pki.GetCAPool(), rxc.ctCache.Get())
|
dropReason := f.firewall.Drop(rxc.fwPacket.Packet, true, hostinfo, f.pki.GetCAPool(), rxc.ctCache.Get())
|
||||||
if dropReason != nil {
|
if dropReason != nil {
|
||||||
|
|||||||
@@ -1,291 +0,0 @@
|
|||||||
package nebula
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"net/netip"
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
|
||||||
"github.com/gaissmai/bart"
|
|
||||||
"github.com/rcrowley/go-metrics"
|
|
||||||
"github.com/slackhq/nebula/header"
|
|
||||||
"github.com/slackhq/nebula/noiseutil"
|
|
||||||
"github.com/slackhq/nebula/overlay/batch"
|
|
||||||
"github.com/slackhq/nebula/test"
|
|
||||||
"github.com/slackhq/nebula/udp"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
type relayBatchWrite struct {
|
|
||||||
packets [][]byte
|
|
||||||
remotes []netip.AddrPort
|
|
||||||
batched bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type relayBatchConn struct {
|
|
||||||
udp.NoopConn
|
|
||||||
read func(udp.EncReader, func())
|
|
||||||
writes []relayBatchWrite
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *relayBatchConn) ListenOut(r udp.EncReader, flush func()) error {
|
|
||||||
c.read(r, flush)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *relayBatchConn) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) (int, error) {
|
|
||||||
w := relayBatchWrite{remotes: append([]netip.AddrPort(nil), addrs...), batched: true}
|
|
||||||
for _, b := range bufs {
|
|
||||||
w.packets = append(w.packets, bytes.Clone(b))
|
|
||||||
}
|
|
||||||
c.writes = append(c.writes, w)
|
|
||||||
return len(bufs), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *relayBatchConn) WriteTo(b []byte, addr netip.AddrPort) error {
|
|
||||||
c.writes = append(c.writes, relayBatchWrite{packets: [][]byte{bytes.Clone(b)}, remotes: []netip.AddrPort{addr}})
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *relayBatchConn) packets() [][]byte {
|
|
||||||
var packets [][]byte
|
|
||||||
for _, w := range c.writes {
|
|
||||||
packets = append(packets, w.packets...)
|
|
||||||
}
|
|
||||||
return packets
|
|
||||||
}
|
|
||||||
|
|
||||||
type relayBatchPath struct {
|
|
||||||
f *Interface
|
|
||||||
conn *relayBatchConn
|
|
||||||
source, target *HostInfo
|
|
||||||
incoming, outgoing *Relay
|
|
||||||
sourceKey noiseutil.CipherState
|
|
||||||
cipher noise.CipherFunc
|
|
||||||
counter uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// Separate cipher instances let the reference SendVia path use the same keys and
|
|
||||||
// counters without rewinding an AEAD instance (including FIPS nonce checks).
|
|
||||||
func relayBatchKey(cipher noise.CipherFunc, id byte) noiseutil.CipherState {
|
|
||||||
suite := noise.NewCipherSuite(noise.DH25519, cipher, noise.HashSHA256)
|
|
||||||
return noiseutil.NewCipherState(noise.UnsafeNewCipherState(suite, [32]byte{id}, 0), cipher)
|
|
||||||
}
|
|
||||||
|
|
||||||
func newRelayBatchPath(t *testing.T, cipher noise.CipherFunc) *relayBatchPath {
|
|
||||||
t.Helper()
|
|
||||||
l := test.NewLogger()
|
|
||||||
conn := &relayBatchConn{}
|
|
||||||
f := &Interface{
|
|
||||||
ctx: t.Context(), l: l, hostMap: newHostMap(l), outside: conn,
|
|
||||||
writers: []udp.Conn{conn}, lightHouse: &LightHouse{l: l},
|
|
||||||
myVpnNetworksTable: &bart.Lite{}, metricTxDropped: metrics.NewCounter(),
|
|
||||||
batchers: []*batch.MultiCoalescer{batch.NewMultiCoalescer(&capturingTun{}, l)},
|
|
||||||
}
|
|
||||||
f.connectionManager = &connectionManager{
|
|
||||||
intf: f, relayUsed: map[uint32]struct{}{}, relayUsedLock: &sync.RWMutex{},
|
|
||||||
}
|
|
||||||
newHost := func(addr string, remote string, id byte) *HostInfo {
|
|
||||||
h := &HostInfo{
|
|
||||||
vpnAddrs: []netip.Addr{netip.MustParseAddr(addr)},
|
|
||||||
ConnectionState: &ConnectionState{
|
|
||||||
eKey: relayBatchKey(cipher, id), dKey: relayBatchKey(cipher, id), window: NewBits(ReplayWindow),
|
|
||||||
},
|
|
||||||
relayState: RelayState{relayForByAddr: map[netip.Addr]*Relay{}, relayForByIdx: map[uint32]*Relay{}},
|
|
||||||
}
|
|
||||||
u := netip.MustParseAddrPort(remote)
|
|
||||||
h.remote.Store(&u)
|
|
||||||
f.hostMap.Hosts[h.vpnAddrs[0]] = h
|
|
||||||
return h
|
|
||||||
}
|
|
||||||
source := newHost("192.0.2.1", "198.51.100.1:4242", 1)
|
|
||||||
target := newHost("192.0.2.2", "198.51.100.2:4242", 2)
|
|
||||||
incoming := &Relay{Type: ForwardingType, State: Established, LocalIndex: 301, RemoteIndex: 302, PeerAddr: target.vpnAddrs[0]}
|
|
||||||
outgoing := &Relay{Type: ForwardingType, State: Established, LocalIndex: 303, RemoteIndex: 304, PeerAddr: source.vpnAddrs[0]}
|
|
||||||
source.relayState.InsertRelay(incoming.PeerAddr, incoming.LocalIndex, incoming)
|
|
||||||
target.relayState.InsertRelay(outgoing.PeerAddr, outgoing.LocalIndex, outgoing)
|
|
||||||
f.hostMap.Relays[incoming.LocalIndex] = source
|
|
||||||
f.hostMap.Indexes[100] = source
|
|
||||||
return &relayBatchPath{f: f, conn: conn, source: source, target: target, incoming: incoming, outgoing: outgoing, sourceKey: relayBatchKey(cipher, 1), cipher: cipher}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *relayBatchPath) wrap(t *testing.T, payload []byte) []byte {
|
|
||||||
t.Helper()
|
|
||||||
p.counter++
|
|
||||||
b := header.Encode(make([]byte, header.Len), header.Version, header.Message, header.MessageRelay, p.incoming.LocalIndex, p.counter)
|
|
||||||
b = append(b, payload...)
|
|
||||||
b, err := p.sourceKey.EncryptDanger(b, b, nil, p.counter, make([]byte, 12))
|
|
||||||
require.NoError(t, err)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *relayBatchPath) reference(payloads [][]byte) [][]byte {
|
|
||||||
conn := &relayBatchConn{}
|
|
||||||
f := &Interface{writers: []udp.Conn{conn}, connectionManager: p.f.connectionManager, l: p.f.l}
|
|
||||||
h := &HostInfo{ConnectionState: &ConnectionState{eKey: relayBatchKey(p.cipher, 2)}}
|
|
||||||
u := p.target.GetRemote()
|
|
||||||
h.remote.Store(&u)
|
|
||||||
for _, payload := range payloads {
|
|
||||||
f.SendVia(h, p.outgoing, payload, make([]byte, 12), make([]byte, 0, len(payload)+header.MaxOverhead), false, 0)
|
|
||||||
}
|
|
||||||
return conn.packets()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRelayReceiveBatch(t *testing.T) {
|
|
||||||
for _, cipher := range []noise.CipherFunc{noiseutil.CipherAESGCM, noise.CipherChaChaPoly} {
|
|
||||||
t.Run(cipher.CipherName(), func(t *testing.T) {
|
|
||||||
p := newRelayBatchPath(t, cipher)
|
|
||||||
const count = 8
|
|
||||||
var payloads, wire [][]byte
|
|
||||||
for i := range count * 2 {
|
|
||||||
payload := bytes.Repeat([]byte{byte(i)}, 64)
|
|
||||||
payloads = append(payloads, payload)
|
|
||||||
wire = append(wire, p.wrap(t, payload))
|
|
||||||
}
|
|
||||||
want := p.reference(payloads)
|
|
||||||
// Model a listener reusing the same slots for the next receive batch.
|
|
||||||
buffers := make([][]byte, count)
|
|
||||||
for i := range buffers {
|
|
||||||
buffers[i] = make([]byte, len(wire[i]))
|
|
||||||
}
|
|
||||||
p.conn.read = func(r udp.EncReader, flush func()) {
|
|
||||||
for b := range 2 {
|
|
||||||
for i := range count {
|
|
||||||
copy(buffers[i], wire[b*count+i])
|
|
||||||
r(p.source.GetRemote(), buffers[i])
|
|
||||||
require.Len(t, p.conn.writes, b, "forwarding waits for the batch boundary")
|
|
||||||
}
|
|
||||||
flush()
|
|
||||||
require.Len(t, p.conn.writes, b+1, "flush must send before receive slots are reused")
|
|
||||||
for _, buffer := range buffers {
|
|
||||||
clear(buffer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
p.f.listenOut(0)
|
|
||||||
require.Equal(t, want, p.conn.packets())
|
|
||||||
for _, w := range p.conn.writes {
|
|
||||||
require.True(t, w.batched)
|
|
||||||
require.Len(t, w.packets, count)
|
|
||||||
for _, remote := range w.remotes {
|
|
||||||
require.Equal(t, p.target.GetRemote(), remote)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRelayBatchDestinationChange(t *testing.T) {
|
|
||||||
p := newRelayBatchPath(t, noise.CipherChaChaPoly)
|
|
||||||
first, second := p.target.GetRemote(), netip.MustParseAddrPort("198.51.100.3:4242")
|
|
||||||
remotes := []netip.AddrPort{first, first, second, second, first}
|
|
||||||
var payloads, wire [][]byte
|
|
||||||
for i := range remotes {
|
|
||||||
payload := []byte{byte(i)}
|
|
||||||
payloads = append(payloads, payload)
|
|
||||||
wire = append(wire, p.wrap(t, payload))
|
|
||||||
}
|
|
||||||
want := p.reference(payloads)
|
|
||||||
p.conn.read = func(r udp.EncReader, flush func()) {
|
|
||||||
for i, remote := range remotes {
|
|
||||||
p.target.remote.Store(&remote)
|
|
||||||
r(p.source.GetRemote(), wire[i])
|
|
||||||
}
|
|
||||||
flush()
|
|
||||||
}
|
|
||||||
p.f.listenOut(0)
|
|
||||||
require.Equal(t, want, p.conn.packets())
|
|
||||||
require.Len(t, p.conn.writes, 3)
|
|
||||||
for i, wantRemotes := range [][]netip.AddrPort{{first, first}, {second, second}, {first}} {
|
|
||||||
require.Equal(t, wantRemotes, p.conn.writes[i].remotes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRelayBatchCapacity(t *testing.T) {
|
|
||||||
p := newRelayBatchPath(t, noise.CipherChaChaPoly)
|
|
||||||
p.conn.read = func(r udp.EncReader, flush func()) {
|
|
||||||
for i := range batch.SendBatchCap + 1 {
|
|
||||||
r(p.source.GetRemote(), p.wrap(t, []byte{byte(i)}))
|
|
||||||
}
|
|
||||||
require.Len(t, p.conn.writes, 1)
|
|
||||||
require.Len(t, p.conn.writes[0].packets, batch.SendBatchCap)
|
|
||||||
flush()
|
|
||||||
}
|
|
||||||
p.f.listenOut(0)
|
|
||||||
require.Len(t, p.conn.writes, 2)
|
|
||||||
require.Len(t, p.conn.writes[1].packets, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRelayBatchBeforeResponse(t *testing.T) {
|
|
||||||
for _, unknownRelay := range []bool{false, true} {
|
|
||||||
name := "test request"
|
|
||||||
if unknownRelay {
|
|
||||||
name = "unknown relay"
|
|
||||||
}
|
|
||||||
t.Run(name, func(t *testing.T) {
|
|
||||||
p := newRelayBatchPath(t, noise.CipherChaChaPoly)
|
|
||||||
first, last := p.wrap(t, []byte("first")), p.wrap(t, []byte("last"))
|
|
||||||
p.counter++
|
|
||||||
request := header.Encode(make([]byte, header.Len), header.Version, header.Test, header.TestRequest, 100, p.counter)
|
|
||||||
request, err := p.sourceKey.EncryptDanger(request, request, []byte("ping"), p.counter, make([]byte, 12))
|
|
||||||
require.NoError(t, err)
|
|
||||||
if unknownRelay {
|
|
||||||
request = header.Encode(make([]byte, header.Len), header.Version, header.Message, header.MessageRelay, 999, p.counter)
|
|
||||||
}
|
|
||||||
p.conn.read = func(r udp.EncReader, flush func()) {
|
|
||||||
r(p.source.GetRemote(), first)
|
|
||||||
require.Empty(t, p.conn.writes)
|
|
||||||
r(p.source.GetRemote(), request)
|
|
||||||
require.Len(t, p.conn.writes, 2)
|
|
||||||
r(p.source.GetRemote(), last)
|
|
||||||
flush()
|
|
||||||
}
|
|
||||||
p.f.listenOut(0)
|
|
||||||
require.Len(t, p.conn.writes, 3)
|
|
||||||
require.True(t, p.conn.writes[0].batched)
|
|
||||||
require.False(t, p.conn.writes[1].batched)
|
|
||||||
require.True(t, p.conn.writes[2].batched)
|
|
||||||
require.Equal(t, p.source.GetRemote(), p.conn.writes[1].remotes[0])
|
|
||||||
h := &header.H{}
|
|
||||||
require.NoError(t, h.Parse(p.conn.writes[1].packets[0]))
|
|
||||||
if unknownRelay {
|
|
||||||
require.Equal(t, header.RecvError, h.Type)
|
|
||||||
require.EqualValues(t, 999, h.RemoteIndex)
|
|
||||||
} else {
|
|
||||||
require.Equal(t, header.Test, h.Type)
|
|
||||||
require.Equal(t, header.TestReply, h.Subtype)
|
|
||||||
reply := p.conn.writes[1].packets[0]
|
|
||||||
out, err := p.sourceKey.DecryptDanger(nil, reply[:header.Len], reply[header.Len:], h.MessageCounter, make([]byte, 12))
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, []byte("ping"), out)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRelayBatchRemovedTarget(t *testing.T) {
|
|
||||||
for _, remove := range []string{"host", "relay", "disestablished"} {
|
|
||||||
t.Run(remove, func(t *testing.T) {
|
|
||||||
p := newRelayBatchPath(t, noise.CipherChaChaPoly)
|
|
||||||
first, removed := p.wrap(t, []byte("first")), p.wrap(t, []byte("removed"))
|
|
||||||
want := p.reference([][]byte{[]byte("first")})
|
|
||||||
p.conn.read = func(r udp.EncReader, flush func()) {
|
|
||||||
r(p.source.GetRemote(), first)
|
|
||||||
switch remove {
|
|
||||||
case "host":
|
|
||||||
delete(p.f.hostMap.Hosts, p.target.vpnAddrs[0])
|
|
||||||
case "relay":
|
|
||||||
delete(p.target.relayState.relayForByAddr, p.source.vpnAddrs[0])
|
|
||||||
case "disestablished":
|
|
||||||
p.target.relayState.UpdateRelayForByIpState(p.source.vpnAddrs[0], Disestablished)
|
|
||||||
}
|
|
||||||
r(p.source.GetRemote(), removed)
|
|
||||||
flush()
|
|
||||||
}
|
|
||||||
p.f.listenOut(0)
|
|
||||||
require.Len(t, p.conn.writes, 1)
|
|
||||||
require.Equal(t, want, p.conn.packets(), "only the packet accepted before removal is sent")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -10,8 +10,8 @@ type batchWriter interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SendBatch accumulates encrypted UDP packets and flushes them via WriteBatch.
|
// SendBatch accumulates encrypted UDP packets and flushes them via WriteBatch.
|
||||||
// Each reader goroutine owns its own SendBatch; no locking is needed.
|
// One SendBatch is owned by each listenIn goroutine; no locking is needed.
|
||||||
// Slots can borrow receive buffers or be backed by an Arena (see its docs).
|
// Slots are backed by an Arena (see its docs)
|
||||||
type SendBatch struct {
|
type SendBatch struct {
|
||||||
out batchWriter
|
out batchWriter
|
||||||
bufs [][]byte
|
bufs [][]byte
|
||||||
|
|||||||
@@ -1,121 +0,0 @@
|
|||||||
//go:build !ios && !e2e_testing
|
|
||||||
|
|
||||||
package overlay
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"os"
|
|
||||||
"syscall"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestTun_WriteFraming(t *testing.T) {
|
|
||||||
r, w, err := os.Pipe()
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer r.Close()
|
|
||||||
defer w.Close()
|
|
||||||
tn := &tun{f: w}
|
|
||||||
|
|
||||||
for _, tc := range []struct {
|
|
||||||
name string
|
|
||||||
pkt []byte
|
|
||||||
af byte
|
|
||||||
}{
|
|
||||||
// Literal families rather than syscall.AF_*, so a wrong constant in Write can't also fix the expectation.
|
|
||||||
{"ipv4", []byte{0x45, 1, 2, 3, 4, 5}, 2},
|
|
||||||
{"ipv6", []byte{0x60, 1, 2, 3, 4, 5}, 30},
|
|
||||||
} {
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
|
||||||
n, err := tn.Write(tc.pkt)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, len(tc.pkt), n)
|
|
||||||
|
|
||||||
got := make([]byte, 64)
|
|
||||||
n, err = r.Read(got)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, append([]byte{0, 0, 0, tc.af}, tc.pkt...), got[:n])
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = tn.Write([]byte{0x10, 1, 2, 3})
|
|
||||||
require.Error(t, err)
|
|
||||||
_, err = tn.Write([]byte{})
|
|
||||||
require.ErrorIs(t, err, syscall.EIO)
|
|
||||||
|
|
||||||
// The rejected writes must not have put anything on the fd, so the next read sees only this packet.
|
|
||||||
pkt := []byte{0x45, 9, 9}
|
|
||||||
_, err = tn.Write(pkt)
|
|
||||||
require.NoError(t, err)
|
|
||||||
got := make([]byte, 64)
|
|
||||||
n, err := r.Read(got)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, append([]byte{0, 0, 0, 2}, pkt...), got[:n])
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTun_ReadStripsHeader(t *testing.T) {
|
|
||||||
r, w, err := os.Pipe()
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer r.Close()
|
|
||||||
defer w.Close()
|
|
||||||
tn := &tun{f: r}
|
|
||||||
|
|
||||||
pkt := []byte{0x45, 1, 2, 3, 4, 5}
|
|
||||||
_, err = w.Write(append([]byte{0, 0, 0, syscall.AF_INET}, pkt...))
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
got := make([]byte, 64)
|
|
||||||
n, err := tn.Read(got)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, pkt, got[:n])
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTun_ReadShort(t *testing.T) {
|
|
||||||
r, w, err := os.Pipe()
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer r.Close()
|
|
||||||
defer w.Close()
|
|
||||||
tn := &tun{f: r}
|
|
||||||
|
|
||||||
// Fewer bytes than the protocol header is not a packet.
|
|
||||||
_, err = w.Write([]byte{0, 0})
|
|
||||||
require.NoError(t, err)
|
|
||||||
n, err := tn.Read(make([]byte, 64))
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Zero(t, n)
|
|
||||||
|
|
||||||
// The next packet reads cleanly only if the short read consumed exactly what was written.
|
|
||||||
pkt := []byte{0x45, 1, 2, 3}
|
|
||||||
_, err = w.Write(append([]byte{0, 0, 0, syscall.AF_INET}, pkt...))
|
|
||||||
require.NoError(t, err)
|
|
||||||
got := make([]byte, 64)
|
|
||||||
n, err = tn.Read(got)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, pkt, got[:n])
|
|
||||||
}
|
|
||||||
|
|
||||||
// A heap allocation per packet rarely shows in a short throughput run, only across GC pauses, so
|
|
||||||
// this pins Read and Write to zero allocations directly.
|
|
||||||
func TestTun_ReadWriteDoNotAllocate(t *testing.T) {
|
|
||||||
r, w, err := os.Pipe()
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer r.Close()
|
|
||||||
defer w.Close()
|
|
||||||
tw := &tun{f: w}
|
|
||||||
tr := &tun{f: r}
|
|
||||||
|
|
||||||
pkts := [][]byte{bytes.Repeat([]byte{0x45}, 1300), bytes.Repeat([]byte{0x60}, 1300)}
|
|
||||||
buf := make([]byte, 1500)
|
|
||||||
i := 0
|
|
||||||
allocs := testing.AllocsPerRun(1000, func() {
|
|
||||||
i++
|
|
||||||
if _, err := tw.Write(pkts[i%len(pkts)]); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := tr.Read(buf); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
require.Zero(t, allocs)
|
|
||||||
}
|
|
||||||
+2
-4
@@ -36,13 +36,11 @@ func (l *tcpListener) Accept() (net.Conn, error) {
|
|||||||
|
|
||||||
func (l *tcpListener) Close() error {
|
func (l *tcpListener) Close() error {
|
||||||
l.closeOnce.Do(func() {
|
l.closeOnce.Do(func() {
|
||||||
// Close before leaving the map so a concurrent closeListeners that
|
|
||||||
// no longer sees this listener knows Accept is already released.
|
|
||||||
close(l.closed)
|
|
||||||
|
|
||||||
l.s.mu.Lock()
|
l.s.mu.Lock()
|
||||||
delete(l.s.mu.listeners, l.port)
|
delete(l.s.mu.listeners, l.port)
|
||||||
l.s.mu.Unlock()
|
l.s.mu.Unlock()
|
||||||
|
|
||||||
|
close(l.closed)
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-70
@@ -32,18 +32,13 @@ import (
|
|||||||
const nicID = 1
|
const nicID = 1
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
|
eg *errgroup.Group
|
||||||
control *nebula.Control
|
control *nebula.Control
|
||||||
ipstack *stack.Stack
|
ipstack *stack.Stack
|
||||||
|
|
||||||
// done is closed once the forwarding goroutines have exited and the
|
|
||||||
// netstack is torn down, at which point err holds the first fatal error.
|
|
||||||
done chan struct{}
|
|
||||||
err error
|
|
||||||
|
|
||||||
mu struct {
|
mu struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|
||||||
closed bool
|
|
||||||
listeners map[uint16]*tcpListener
|
listeners map[uint16]*tcpListener
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -70,6 +65,7 @@ func New(control *nebula.Control) (_ *Service, reterr error) {
|
|||||||
ctx := control.Context()
|
ctx := control.Context()
|
||||||
eg, ctx := errgroup.WithContext(ctx)
|
eg, ctx := errgroup.WithContext(ctx)
|
||||||
s := Service{
|
s := Service{
|
||||||
|
eg: eg,
|
||||||
control: control,
|
control: control,
|
||||||
}
|
}
|
||||||
s.mu.listeners = map[uint16]*tcpListener{}
|
s.mu.listeners = map[uint16]*tcpListener{}
|
||||||
@@ -78,12 +74,6 @@ func New(control *nebula.Control) (_ *Service, reterr error) {
|
|||||||
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
|
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
|
||||||
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol, icmp.NewProtocol4, icmp.NewProtocol6},
|
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol, icmp.NewProtocol4, icmp.NewProtocol6},
|
||||||
})
|
})
|
||||||
// The teardown goroutine only owns the stack once New succeeds
|
|
||||||
defer func() {
|
|
||||||
if reterr != nil {
|
|
||||||
s.ipstack.Destroy()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
sackEnabledOpt := tcpip.TCPSACKEnabled(true) // TCP SACK is disabled by default
|
sackEnabledOpt := tcpip.TCPSACKEnabled(true) // TCP SACK is disabled by default
|
||||||
tcpipErr := s.ipstack.SetTransportProtocolOption(tcp.ProtocolNumber, &sackEnabledOpt)
|
tcpipErr := s.ipstack.SetTransportProtocolOption(tcp.ProtocolNumber, &sackEnabledOpt)
|
||||||
if tcpipErr != nil {
|
if tcpipErr != nil {
|
||||||
@@ -133,12 +123,6 @@ func New(control *nebula.Control) (_ *Service, reterr error) {
|
|||||||
// this will read exactly one packet
|
// this will read exactly one packet
|
||||||
n, err := reader.Read(buf)
|
n, err := reader.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// An error after shutdown began is noise from the device pipes
|
|
||||||
// closing, anything else is fatal and must stop nebula too
|
|
||||||
if ctx.Err() != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
s.control.Stop()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
packetBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{
|
packetBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{
|
||||||
@@ -146,8 +130,8 @@ func New(control *nebula.Control) (_ *Service, reterr error) {
|
|||||||
})
|
})
|
||||||
linkEP.InjectInbound(header.IPv4ProtocolNumber, packetBuf)
|
linkEP.InjectInbound(header.IPv4ProtocolNumber, packetBuf)
|
||||||
|
|
||||||
if ctx.Err() != nil {
|
if err := ctx.Err(); err != nil {
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -155,18 +139,13 @@ func New(control *nebula.Control) (_ *Service, reterr error) {
|
|||||||
for {
|
for {
|
||||||
packet := linkEP.ReadContext(ctx)
|
packet := linkEP.ReadContext(ctx)
|
||||||
if packet == nil {
|
if packet == nil {
|
||||||
if ctx.Err() != nil {
|
if err := ctx.Err(); err != nil {
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
bufView := packet.ToView()
|
bufView := packet.ToView()
|
||||||
if _, err := bufView.WriteTo(writer); err != nil {
|
if _, err := bufView.WriteTo(writer); err != nil {
|
||||||
// Same shutdown noise handling as the inbound reader
|
|
||||||
if ctx.Err() != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
s.control.Stop()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
bufView.Release()
|
bufView.Release()
|
||||||
@@ -179,22 +158,6 @@ func New(control *nebula.Control) (_ *Service, reterr error) {
|
|||||||
return control.Wait()
|
return control.Wait()
|
||||||
})
|
})
|
||||||
|
|
||||||
// Tear down here rather than in Close so it also happens when nebula stops
|
|
||||||
// on its own. The forwarding goroutines must exit before the stack they feed
|
|
||||||
// is destroyed.
|
|
||||||
s.done = make(chan struct{})
|
|
||||||
go func() {
|
|
||||||
s.err = eg.Wait()
|
|
||||||
s.closeListeners()
|
|
||||||
// Destroy aborts the endpoints that exist when it starts, then waits on
|
|
||||||
// every endpoint, so a Dial registering in between would hold Wait until
|
|
||||||
// its connect timed out. Without the NIC new dials fail route lookup.
|
|
||||||
// RemoveNIC also closes linkEP.
|
|
||||||
_ = s.ipstack.RemoveNIC(nicID)
|
|
||||||
s.ipstack.Destroy()
|
|
||||||
close(s.done)
|
|
||||||
}()
|
|
||||||
|
|
||||||
return &s, nil
|
return &s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,9 +237,6 @@ func (s *Service) Listen(network, address string) (net.Listener, error) {
|
|||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
if s.mu.closed {
|
|
||||||
return nil, net.ErrClosed
|
|
||||||
}
|
|
||||||
if _, ok := s.mu.listeners[port]; ok {
|
if _, ok := s.mu.listeners[port]; ok {
|
||||||
return nil, fmt.Errorf("already listening on port %d", port)
|
return nil, fmt.Errorf("already listening on port %d", port)
|
||||||
}
|
}
|
||||||
@@ -285,38 +245,15 @@ func (s *Service) Listen(network, address string) (net.Listener, error) {
|
|||||||
return l, nil
|
return l, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait blocks until the service has fully stopped, either via Close or a fatal
|
|
||||||
// error, and returns the first fatal error if there was one.
|
|
||||||
func (s *Service) Wait() error {
|
func (s *Service) Wait() error {
|
||||||
<-s.done
|
return s.eg.Wait()
|
||||||
return s.err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes all listeners and stops nebula. Use Wait to block until
|
|
||||||
// shutdown has completed.
|
|
||||||
func (s *Service) Close() error {
|
func (s *Service) Close() error {
|
||||||
s.closeListeners()
|
|
||||||
s.control.Stop()
|
s.control.Stop()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// closeListeners unblocks pending Accept calls and rejects new listeners.
|
|
||||||
func (s *Service) closeListeners() {
|
|
||||||
// Every caller closes every listener rather than returning early once
|
|
||||||
// closed is set, so none returns while another is still closing them.
|
|
||||||
s.mu.Lock()
|
|
||||||
s.mu.closed = true
|
|
||||||
listeners := make([]*tcpListener, 0, len(s.mu.listeners))
|
|
||||||
for _, l := range s.mu.listeners {
|
|
||||||
listeners = append(listeners, l)
|
|
||||||
}
|
|
||||||
s.mu.Unlock()
|
|
||||||
|
|
||||||
for _, l := range listeners {
|
|
||||||
_ = l.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Service) tcpHandler(r *tcp.ForwarderRequest) {
|
func (s *Service) tcpHandler(r *tcp.ForwarderRequest) {
|
||||||
endpointID := r.ID()
|
endpointID := r.ID()
|
||||||
|
|
||||||
|
|||||||
@@ -327,162 +327,3 @@ func TestListenerAcceptAfterClose(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close must release blocked Accept calls and let Wait report a clean shutdown.
|
|
||||||
func TestServiceClose(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
|
||||||
a := newSimpleService(ca, caKey, "a", netip.MustParseAddr("10.0.0.1"), m{
|
|
||||||
"static_host_map": m{},
|
|
||||||
"listen": m{
|
|
||||||
"host": "127.0.0.1",
|
|
||||||
"port": 0,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ln, err := a.Listen("tcp", ":1234")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
accepted := make(chan error, 1)
|
|
||||||
go func() {
|
|
||||||
_, err := ln.Accept()
|
|
||||||
accepted <- err
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err := a.Close(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case err := <-accepted:
|
|
||||||
if !errors.Is(err, net.ErrClosed) {
|
|
||||||
t.Fatalf("expected net.ErrClosed from Accept, got %v", err)
|
|
||||||
}
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
t.Fatal("Accept still blocked after Close")
|
|
||||||
}
|
|
||||||
|
|
||||||
waited := make(chan error, 1)
|
|
||||||
go func() { waited <- a.Wait() }()
|
|
||||||
select {
|
|
||||||
case err := <-waited:
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("expected clean shutdown from Wait, got %v", err)
|
|
||||||
}
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
t.Fatal("Wait still blocked after Close")
|
|
||||||
}
|
|
||||||
|
|
||||||
if a.ipstack.HasNIC(nicID) {
|
|
||||||
t.Fatal("netstack not torn down after Wait")
|
|
||||||
}
|
|
||||||
if err := a.Close(); err != nil {
|
|
||||||
t.Fatalf("second close: %v", err)
|
|
||||||
}
|
|
||||||
if _, err := a.Listen("tcp", ":1235"); !errors.Is(err, net.ErrClosed) {
|
|
||||||
t.Fatalf("expected net.ErrClosed from Listen after Close, got %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If nebula stops without Close being called, the service must still release
|
|
||||||
// Accept and tear down the netstack.
|
|
||||||
func TestServiceNebulaStopped(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
|
||||||
a := newSimpleService(ca, caKey, "a", netip.MustParseAddr("10.0.0.1"), m{
|
|
||||||
"static_host_map": m{},
|
|
||||||
"listen": m{
|
|
||||||
"host": "127.0.0.1",
|
|
||||||
"port": 0,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ln, err := a.Listen("tcp", ":1234")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
accepted := make(chan error, 1)
|
|
||||||
go func() {
|
|
||||||
_, err := ln.Accept()
|
|
||||||
accepted <- err
|
|
||||||
}()
|
|
||||||
|
|
||||||
a.control.Stop()
|
|
||||||
|
|
||||||
waited := make(chan error, 1)
|
|
||||||
go func() { waited <- a.Wait() }()
|
|
||||||
select {
|
|
||||||
case err := <-waited:
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("expected clean shutdown from Wait, got %v", err)
|
|
||||||
}
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
t.Fatal("Wait still blocked after nebula stopped")
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case err := <-accepted:
|
|
||||||
if !errors.Is(err, net.ErrClosed) {
|
|
||||||
t.Fatalf("expected net.ErrClosed from Accept, got %v", err)
|
|
||||||
}
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
t.Fatal("Accept still blocked after nebula stopped")
|
|
||||||
}
|
|
||||||
|
|
||||||
if a.ipstack.HasNIC(nicID) {
|
|
||||||
t.Fatal("netstack not torn down after Wait")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dials racing Close must not hold Wait until their connect times out.
|
|
||||||
func TestServiceDialDuringClose(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
|
||||||
a := newSimpleService(ca, caKey, "a", netip.MustParseAddr("10.0.0.1"), m{
|
|
||||||
"static_host_map": m{},
|
|
||||||
"listen": m{
|
|
||||||
"host": "127.0.0.1",
|
|
||||||
"port": 0,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
stop := make(chan struct{})
|
|
||||||
var eg errgroup.Group
|
|
||||||
for range 8 {
|
|
||||||
eg.Go(func() error {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-stop:
|
|
||||||
return nil
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
// 10.0.0.2 has no tunnel, so each dial sits in SYN-SENT until aborted
|
|
||||||
if c, err := a.Dial("tcp", "10.0.0.2:80"); err == nil {
|
|
||||||
_ = c.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
time.Sleep(50 * time.Millisecond)
|
|
||||||
|
|
||||||
if err := a.Close(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
waited := make(chan error, 1)
|
|
||||||
go func() { waited <- a.Wait() }()
|
|
||||||
select {
|
|
||||||
case err := <-waited:
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("expected clean shutdown from Wait, got %v", err)
|
|
||||||
}
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
t.Fatal("Wait blocked behind a dial that raced Close")
|
|
||||||
}
|
|
||||||
|
|
||||||
close(stop)
|
|
||||||
dialersDone := make(chan error, 1)
|
|
||||||
go func() { dialersDone <- eg.Wait() }()
|
|
||||||
select {
|
|
||||||
case <-dialersDone:
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
t.Fatal("Dial still blocked after Wait")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-1
@@ -41,7 +41,7 @@ type Conn interface {
|
|||||||
//
|
//
|
||||||
// Returns the number of packets successfully written. A destination the kernel rejects costs only
|
// Returns the number of packets successfully written. A destination the kernel rejects costs only
|
||||||
// its own packet, so a short count means some peers were undeliverable, not that the batch failed.
|
// its own packet, so a short count means some peers were undeliverable, not that the batch failed.
|
||||||
// Safe for concurrent use on the same Conn.
|
// Not safe for concurrent use on the same Conn.
|
||||||
WriteBatch(bufs [][]byte, addrs []netip.AddrPort) (int, error)
|
WriteBatch(bufs [][]byte, addrs []netip.AddrPort) (int, error)
|
||||||
// ReloadConfig applies the listen settings that can change, r is nil at startup
|
// ReloadConfig applies the listen settings that can change, r is nil at startup
|
||||||
ReloadConfig(c *config.C, r *config.Reload)
|
ReloadConfig(c *config.C, r *config.Reload)
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
@@ -21,7 +20,7 @@ import (
|
|||||||
// batchWriter owns the sendmmsg(2)/UDP-GSO transmit path for a StdConn: the
|
// batchWriter owns the sendmmsg(2)/UDP-GSO transmit path for a StdConn: the
|
||||||
// scratch WriteBatch packs mmsghdr entries into, plus the GSO capability
|
// scratch WriteBatch packs mmsghdr entries into, plus the GSO capability
|
||||||
// state probed at socket creation. Each queue has its own StdConn and
|
// state probed at socket creation. Each queue has its own StdConn and
|
||||||
// batchWriter; mu serializes batches from the TUN reader and relay forwarding.
|
// batchWriter, so no locking is needed.
|
||||||
//
|
//
|
||||||
// Terminology, smallest to largest:
|
// Terminology, smallest to largest:
|
||||||
//
|
//
|
||||||
@@ -40,7 +39,6 @@ import (
|
|||||||
// chunk the entries packed for one sendmmsg call, at most MaxWriteBatch.
|
// chunk the entries packed for one sendmmsg call, at most MaxWriteBatch.
|
||||||
// batch the caller's whole bufs/addrs pair, processed as one or more chunks.
|
// batch the caller's whole bufs/addrs pair, processed as one or more chunks.
|
||||||
type batchWriter struct {
|
type batchWriter struct {
|
||||||
mu sync.Mutex
|
|
||||||
fd int
|
fd int
|
||||||
isV4 bool
|
isV4 bool
|
||||||
|
|
||||||
@@ -194,9 +192,6 @@ func parseRelease(r string) (major, minor int) {
|
|||||||
// Returns the number of packets sent. An error means the call itself failed.
|
// Returns the number of packets sent. An error means the call itself failed.
|
||||||
// A short count means some destinations were undeliverable.
|
// A short count means some destinations were undeliverable.
|
||||||
func (w *batchWriter) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) (int, error) {
|
func (w *batchWriter) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) (int, error) {
|
||||||
w.mu.Lock()
|
|
||||||
defer w.mu.Unlock()
|
|
||||||
|
|
||||||
if len(bufs) != len(addrs) {
|
if len(bufs) != len(addrs) {
|
||||||
return 0, fmt.Errorf("WriteBatch: len(bufs)=%d != len(addrs)=%d", len(bufs), len(addrs))
|
return 0, fmt.Errorf("WriteBatch: len(bufs)=%d != len(addrs)=%d", len(bufs), len(addrs))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
//go:build linux && !android && !e2e_testing
|
|
||||||
|
|
||||||
package udp
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/netip"
|
|
||||||
"runtime"
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The TUN reader and relay forwarding share a socket. Exercise their concurrent
|
|
||||||
// batches against the actual GSO planner and scratch, without relying on a NIC.
|
|
||||||
func TestWriteBatchConcurrent(t *testing.T) {
|
|
||||||
const batches = 64
|
|
||||||
w := newRewindTestWriter()
|
|
||||||
sent := make(chan [][]byte, 2*batches)
|
|
||||||
w.sendFn = func(start, n int) (int, error) {
|
|
||||||
runtime.Gosched()
|
|
||||||
sent <- capturePrepared(w, start, n)
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
conn := &StdConn{bw: w}
|
|
||||||
remote := netip.MustParseAddrPort("192.0.2.1:4242")
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
start := make(chan struct{})
|
|
||||||
for producer := range 2 {
|
|
||||||
wg.Go(func() {
|
|
||||||
<-start
|
|
||||||
for i := range batches {
|
|
||||||
var bufs [][]byte
|
|
||||||
var addrs []netip.AddrPort
|
|
||||||
for j := range 4 {
|
|
||||||
bufs = append(bufs, []byte{byte(producer), byte(i), byte(j), 0})
|
|
||||||
addrs = append(addrs, remote)
|
|
||||||
}
|
|
||||||
n, err := conn.WriteBatch(bufs, addrs)
|
|
||||||
if err != nil || n != len(bufs) {
|
|
||||||
t.Errorf("WriteBatch = %d, %v; want %d, nil", n, err, len(bufs))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
close(start)
|
|
||||||
wg.Wait()
|
|
||||||
close(sent)
|
|
||||||
var counts [2]int
|
|
||||||
for packets := range sent {
|
|
||||||
require.Len(t, packets, 4)
|
|
||||||
producer := packets[0][0]
|
|
||||||
require.Less(t, int(producer), len(counts))
|
|
||||||
for j, packet := range packets {
|
|
||||||
require.Equal(t, []byte{producer, byte(counts[producer]), byte(j), 0}, packet)
|
|
||||||
}
|
|
||||||
counts[producer]++
|
|
||||||
}
|
|
||||||
require.Equal(t, [2]int{batches, batches}, counts)
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user