mirror of
https://github.com/slackhq/nebula.git
synced 2026-08-16 23:57:02 +02:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dfe94c6269 | |||
| 1c601d776a | |||
| 17d8ebff93 | |||
| 612d3ef931 | |||
| 8282a629e5 | |||
| c62f27d4b4 | |||
| f5db77f214 | |||
| b9a7d1edf3 | |||
| d1ea33659a | |||
| 8fdd98f639 | |||
| 45bc0fc055 | |||
| 24af30bd78 | |||
| 1d84b81032 | |||
| b155f4b7e1 | |||
| 194d58cd46 | |||
| a476b1fa07 | |||
| 8b02b8128e |
@@ -163,55 +163,3 @@ func P256Keypair() ([]byte, []byte) {
|
|||||||
pubkey := privkey.PublicKey()
|
pubkey := privkey.PublicKey()
|
||||||
return pubkey.Bytes(), privkey.Bytes()
|
return pubkey.Bytes(), privkey.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
// DummyCert is a minimal cert.Certificate implementation for testing error paths.
|
|
||||||
type DummyCert struct {
|
|
||||||
Version_ cert.Version
|
|
||||||
Curve_ cert.Curve
|
|
||||||
Groups_ []string
|
|
||||||
IsCA_ bool
|
|
||||||
Issuer_ string
|
|
||||||
Name_ string
|
|
||||||
Networks_ []netip.Prefix
|
|
||||||
NotAfter_ time.Time
|
|
||||||
NotBefore_ time.Time
|
|
||||||
PublicKey_ []byte
|
|
||||||
Signature_ []byte
|
|
||||||
UnsafeNetworks_ []netip.Prefix
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DummyCert) Version() cert.Version { return d.Version_ }
|
|
||||||
func (d *DummyCert) Curve() cert.Curve { return d.Curve_ }
|
|
||||||
func (d *DummyCert) Groups() []string { return d.Groups_ }
|
|
||||||
func (d *DummyCert) IsCA() bool { return d.IsCA_ }
|
|
||||||
func (d *DummyCert) Issuer() string { return d.Issuer_ }
|
|
||||||
func (d *DummyCert) Name() string { return d.Name_ }
|
|
||||||
func (d *DummyCert) Networks() []netip.Prefix { return d.Networks_ }
|
|
||||||
func (d *DummyCert) NotAfter() time.Time { return d.NotAfter_ }
|
|
||||||
func (d *DummyCert) NotBefore() time.Time { return d.NotBefore_ }
|
|
||||||
func (d *DummyCert) PublicKey() []byte { return d.PublicKey_ }
|
|
||||||
func (d *DummyCert) Signature() []byte { return d.Signature_ }
|
|
||||||
func (d *DummyCert) UnsafeNetworks() []netip.Prefix { return d.UnsafeNetworks_ }
|
|
||||||
func (d *DummyCert) Fingerprint() (string, error) { return "", nil }
|
|
||||||
func (d *DummyCert) CheckSignature(key []byte) bool { return false }
|
|
||||||
func (d *DummyCert) MarshalForHandshakes() ([]byte, error) { return nil, nil }
|
|
||||||
func (d *DummyCert) MarshalPEM() ([]byte, error) { return nil, nil }
|
|
||||||
func (d *DummyCert) MarshalJSON() ([]byte, error) { return nil, nil }
|
|
||||||
func (d *DummyCert) Marshal() ([]byte, error) { return nil, nil }
|
|
||||||
func (d *DummyCert) String() string { return "dummy" }
|
|
||||||
func (d *DummyCert) Copy() cert.Certificate { return d }
|
|
||||||
func (d *DummyCert) VerifyPrivateKey(c cert.Curve, k []byte) error { return nil }
|
|
||||||
func (d *DummyCert) Expired(time.Time) bool { return false }
|
|
||||||
func (d *DummyCert) MarshalPublicKeyPEM() []byte { return nil }
|
|
||||||
func (d *DummyCert) PublicKeyPEM() []byte { return nil }
|
|
||||||
|
|
||||||
// NewTestCAPool creates a CAPool from the given CA certificates, panicking on error.
|
|
||||||
func NewTestCAPool(cas ...cert.Certificate) *cert.CAPool {
|
|
||||||
pool := cert.NewCAPool()
|
|
||||||
for _, ca := range cas {
|
|
||||||
if err := pool.AddCA(ca); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return pool
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -145,7 +145,6 @@ func (cm *connectionManager) getAndResetTrafficCheck(h *HostInfo, now time.Time)
|
|||||||
func (cm *connectionManager) AddTrafficWatch(h *HostInfo) {
|
func (cm *connectionManager) AddTrafficWatch(h *HostInfo) {
|
||||||
if h.out.Swap(true) == false {
|
if h.out.Swap(true) == false {
|
||||||
cm.trafficTimer.Add(h.localIndexId, cm.checkInterval)
|
cm.trafficTimer.Add(h.localIndexId, cm.checkInterval)
|
||||||
cm.intf.pmtudManager.OnTunnelUp(h)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +180,6 @@ func (cm *connectionManager) doTrafficCheck(localIndex uint32, p, nb, out []byte
|
|||||||
|
|
||||||
switch decision {
|
switch decision {
|
||||||
case deleteTunnel:
|
case deleteTunnel:
|
||||||
cm.intf.pmtudManager.OnTunnelDown(hostinfo)
|
|
||||||
if cm.hostMap.DeleteHostInfo(hostinfo) {
|
if cm.hostMap.DeleteHostInfo(hostinfo) {
|
||||||
// Only clearing the lighthouse cache if this is the last hostinfo for this vpn ip in the hostmap
|
// Only clearing the lighthouse cache if this is the last hostinfo for this vpn ip in the hostmap
|
||||||
cm.intf.lightHouse.DeleteVpnAddrs(hostinfo.vpnAddrs)
|
cm.intf.lightHouse.DeleteVpnAddrs(hostinfo.vpnAddrs)
|
||||||
@@ -201,15 +199,8 @@ func (cm *connectionManager) doTrafficCheck(localIndex uint32, p, nb, out []byte
|
|||||||
cm.tryRehandshake(hostinfo)
|
cm.tryRehandshake(hostinfo)
|
||||||
|
|
||||||
case sendTestPacket:
|
case sendTestPacket:
|
||||||
// Defer to pmtud if it has a confirmed PMTU > floor for this peer:
|
|
||||||
// the probe at the confirmed size verifies both liveness AND that
|
|
||||||
// the discovered PMTU still fits, so we don't burn a separate test
|
|
||||||
// packet on top of it. If pmtud declines (disabled, peer unsupported,
|
|
||||||
// or no confirmed size yet) we fall back to the regular test.
|
|
||||||
if !cm.intf.pmtudManager.MaybeProbeAsTest(hostinfo) {
|
|
||||||
cm.intf.SendMessageToHostInfo(header.Test, header.TestRequest, hostinfo, p, nb, out)
|
cm.intf.SendMessageToHostInfo(header.Test, header.TestRequest, hostinfo, p, nb, out)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
cm.resetRelayTrafficCheck(hostinfo)
|
cm.resetRelayTrafficCheck(hostinfo)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/flynn/noise"
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/overlay/overlaytest"
|
"github.com/slackhq/nebula/overlay/overlaytest"
|
||||||
@@ -46,7 +47,7 @@ func Test_NewConnectionManagerTest(t *testing.T) {
|
|||||||
initiatingVersion: cert.Version1,
|
initiatingVersion: cert.Version1,
|
||||||
privateKey: []byte{},
|
privateKey: []byte{},
|
||||||
v1Cert: &dummyCert{version: cert.Version1},
|
v1Cert: &dummyCert{version: cert.Version1},
|
||||||
v1Credential: nil,
|
v1HandshakeBytes: []byte{},
|
||||||
}
|
}
|
||||||
|
|
||||||
lh := newTestLighthouse()
|
lh := newTestLighthouse()
|
||||||
@@ -67,8 +68,6 @@ func Test_NewConnectionManagerTest(t *testing.T) {
|
|||||||
punchy := NewPunchyFromConfig(test.NewLogger(), conf)
|
punchy := NewPunchyFromConfig(test.NewLogger(), conf)
|
||||||
nc := newConnectionManagerFromConfig(test.NewLogger(), conf, hostMap, punchy)
|
nc := newConnectionManagerFromConfig(test.NewLogger(), conf, hostMap, punchy)
|
||||||
nc.intf = ifce
|
nc.intf = ifce
|
||||||
ifce.pmtudManager = newPMTUDManagerFromConfig(test.NewLogger(), conf, ifce.inside)
|
|
||||||
ifce.pmtudManager.intf = ifce
|
|
||||||
p := []byte("")
|
p := []byte("")
|
||||||
nb := make([]byte, 12, 12)
|
nb := make([]byte, 12, 12)
|
||||||
out := make([]byte, mtu)
|
out := make([]byte, mtu)
|
||||||
@@ -81,6 +80,7 @@ func Test_NewConnectionManagerTest(t *testing.T) {
|
|||||||
}
|
}
|
||||||
hostinfo.ConnectionState = &ConnectionState{
|
hostinfo.ConnectionState = &ConnectionState{
|
||||||
myCert: &dummyCert{version: cert.Version1},
|
myCert: &dummyCert{version: cert.Version1},
|
||||||
|
H: &noise.HandshakeState{},
|
||||||
}
|
}
|
||||||
nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
|
nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
|
||||||
|
|
||||||
@@ -130,7 +130,7 @@ func Test_NewConnectionManagerTest2(t *testing.T) {
|
|||||||
initiatingVersion: cert.Version1,
|
initiatingVersion: cert.Version1,
|
||||||
privateKey: []byte{},
|
privateKey: []byte{},
|
||||||
v1Cert: &dummyCert{version: cert.Version1},
|
v1Cert: &dummyCert{version: cert.Version1},
|
||||||
v1Credential: nil,
|
v1HandshakeBytes: []byte{},
|
||||||
}
|
}
|
||||||
|
|
||||||
lh := newTestLighthouse()
|
lh := newTestLighthouse()
|
||||||
@@ -151,8 +151,6 @@ func Test_NewConnectionManagerTest2(t *testing.T) {
|
|||||||
punchy := NewPunchyFromConfig(test.NewLogger(), conf)
|
punchy := NewPunchyFromConfig(test.NewLogger(), conf)
|
||||||
nc := newConnectionManagerFromConfig(test.NewLogger(), conf, hostMap, punchy)
|
nc := newConnectionManagerFromConfig(test.NewLogger(), conf, hostMap, punchy)
|
||||||
nc.intf = ifce
|
nc.intf = ifce
|
||||||
ifce.pmtudManager = newPMTUDManagerFromConfig(test.NewLogger(), conf, ifce.inside)
|
|
||||||
ifce.pmtudManager.intf = ifce
|
|
||||||
p := []byte("")
|
p := []byte("")
|
||||||
nb := make([]byte, 12, 12)
|
nb := make([]byte, 12, 12)
|
||||||
out := make([]byte, mtu)
|
out := make([]byte, mtu)
|
||||||
@@ -165,6 +163,7 @@ func Test_NewConnectionManagerTest2(t *testing.T) {
|
|||||||
}
|
}
|
||||||
hostinfo.ConnectionState = &ConnectionState{
|
hostinfo.ConnectionState = &ConnectionState{
|
||||||
myCert: &dummyCert{version: cert.Version1},
|
myCert: &dummyCert{version: cert.Version1},
|
||||||
|
H: &noise.HandshakeState{},
|
||||||
}
|
}
|
||||||
nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
|
nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
|
||||||
|
|
||||||
@@ -216,7 +215,7 @@ func Test_NewConnectionManager_DisconnectInactive(t *testing.T) {
|
|||||||
initiatingVersion: cert.Version1,
|
initiatingVersion: cert.Version1,
|
||||||
privateKey: []byte{},
|
privateKey: []byte{},
|
||||||
v1Cert: &dummyCert{version: cert.Version1},
|
v1Cert: &dummyCert{version: cert.Version1},
|
||||||
v1Credential: nil,
|
v1HandshakeBytes: []byte{},
|
||||||
}
|
}
|
||||||
|
|
||||||
lh := newTestLighthouse()
|
lh := newTestLighthouse()
|
||||||
@@ -250,6 +249,7 @@ func Test_NewConnectionManager_DisconnectInactive(t *testing.T) {
|
|||||||
}
|
}
|
||||||
hostinfo.ConnectionState = &ConnectionState{
|
hostinfo.ConnectionState = &ConnectionState{
|
||||||
myCert: &dummyCert{version: cert.Version1},
|
myCert: &dummyCert{version: cert.Version1},
|
||||||
|
H: &noise.HandshakeState{},
|
||||||
}
|
}
|
||||||
nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
|
nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
|
||||||
|
|
||||||
@@ -342,7 +342,7 @@ func Test_NewConnectionManagerTest_DisconnectInvalid(t *testing.T) {
|
|||||||
cs := &CertState{
|
cs := &CertState{
|
||||||
privateKey: []byte{},
|
privateKey: []byte{},
|
||||||
v1Cert: &dummyCert{},
|
v1Cert: &dummyCert{},
|
||||||
v1Credential: nil,
|
v1HandshakeBytes: []byte{},
|
||||||
}
|
}
|
||||||
|
|
||||||
lh := newTestLighthouse()
|
lh := newTestLighthouse()
|
||||||
@@ -365,8 +365,6 @@ func Test_NewConnectionManagerTest_DisconnectInvalid(t *testing.T) {
|
|||||||
punchy := NewPunchyFromConfig(test.NewLogger(), conf)
|
punchy := NewPunchyFromConfig(test.NewLogger(), conf)
|
||||||
nc := newConnectionManagerFromConfig(test.NewLogger(), conf, hostMap, punchy)
|
nc := newConnectionManagerFromConfig(test.NewLogger(), conf, hostMap, punchy)
|
||||||
nc.intf = ifce
|
nc.intf = ifce
|
||||||
ifce.pmtudManager = newPMTUDManagerFromConfig(test.NewLogger(), conf, ifce.inside)
|
|
||||||
ifce.pmtudManager.intf = ifce
|
|
||||||
ifce.connectionManager = nc
|
ifce.connectionManager = nc
|
||||||
|
|
||||||
hostinfo := &HostInfo{
|
hostinfo := &HostInfo{
|
||||||
@@ -374,6 +372,7 @@ func Test_NewConnectionManagerTest_DisconnectInvalid(t *testing.T) {
|
|||||||
ConnectionState: &ConnectionState{
|
ConnectionState: &ConnectionState{
|
||||||
myCert: &dummyCert{},
|
myCert: &dummyCert{},
|
||||||
peerCert: cachedPeerCert,
|
peerCert: cachedPeerCert,
|
||||||
|
H: &noise.HandshakeState{},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
|
nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
|
||||||
|
|||||||
+52
-17
@@ -1,19 +1,23 @@
|
|||||||
package nebula
|
package nebula
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
|
"github.com/flynn/noise"
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
"github.com/slackhq/nebula/handshake"
|
"github.com/slackhq/nebula/noiseutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
const ReplayWindow = 1024
|
const ReplayWindow = 1024 //todo I've started seeing out-of-window messages in testing?
|
||||||
|
|
||||||
type ConnectionState struct {
|
type ConnectionState struct {
|
||||||
eKey *NebulaCipherState
|
eKey *NebulaCipherState
|
||||||
dKey *NebulaCipherState
|
dKey *NebulaCipherState
|
||||||
|
H *noise.HandshakeState
|
||||||
myCert cert.Certificate
|
myCert cert.Certificate
|
||||||
peerCert *cert.CachedCertificate
|
peerCert *cert.CachedCertificate
|
||||||
initiator bool
|
initiator bool
|
||||||
@@ -22,24 +26,55 @@ type ConnectionState struct {
|
|||||||
writeLock sync.Mutex
|
writeLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// newConnectionStateFromResult builds a fully-populated ConnectionState from a
|
func NewConnectionState(cs *CertState, crt cert.Certificate, initiator bool, pattern noise.HandshakePattern) (*ConnectionState, error) {
|
||||||
// completed handshake.Result. It seeds messageCounter and the replay window so
|
var dhFunc noise.DHFunc
|
||||||
// that the post-handshake message indices already used on the wire don't count
|
switch crt.Curve() {
|
||||||
// as missed traffic in the data plane.
|
case cert.Curve_CURVE25519:
|
||||||
func newConnectionStateFromResult(r *handshake.Result) *ConnectionState {
|
dhFunc = noise.DH25519
|
||||||
|
case cert.Curve_P256:
|
||||||
|
if cs.pkcs11Backed {
|
||||||
|
dhFunc = noiseutil.DHP256PKCS11
|
||||||
|
} else {
|
||||||
|
dhFunc = noiseutil.DHP256
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("invalid curve: %s", crt.Curve())
|
||||||
|
}
|
||||||
|
|
||||||
|
var ncs noise.CipherSuite
|
||||||
|
if cs.cipher == "chachapoly" {
|
||||||
|
ncs = noise.NewCipherSuite(dhFunc, noise.CipherChaChaPoly, noise.HashSHA256)
|
||||||
|
} else {
|
||||||
|
ncs = noise.NewCipherSuite(dhFunc, noiseutil.CipherAESGCM, noise.HashSHA256)
|
||||||
|
}
|
||||||
|
|
||||||
|
static := noise.DHKey{Private: cs.privateKey, Public: crt.PublicKey()}
|
||||||
|
hs, err := noise.NewHandshakeState(noise.Config{
|
||||||
|
CipherSuite: ncs,
|
||||||
|
Random: rand.Reader,
|
||||||
|
Pattern: pattern,
|
||||||
|
Initiator: initiator,
|
||||||
|
StaticKeypair: static,
|
||||||
|
//NOTE: These should come from CertState (pki.go) when we finally implement it
|
||||||
|
PresharedKey: []byte{},
|
||||||
|
PresharedKeyPlacement: 0,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("NewConnectionState: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The queue and ready params prevent a counter race that would happen when
|
||||||
|
// sending stored packets and simultaneously accepting new traffic.
|
||||||
ci := &ConnectionState{
|
ci := &ConnectionState{
|
||||||
myCert: r.MyCert,
|
H: hs,
|
||||||
initiator: r.Initiator,
|
initiator: initiator,
|
||||||
peerCert: r.RemoteCert,
|
|
||||||
eKey: NewNebulaCipherState(r.EKey),
|
|
||||||
dKey: NewNebulaCipherState(r.DKey),
|
|
||||||
window: NewBits(ReplayWindow),
|
window: NewBits(ReplayWindow),
|
||||||
|
myCert: crt,
|
||||||
}
|
}
|
||||||
ci.messageCounter.Add(r.MessageIndex)
|
// always start the counter from 2, as packet 1 and packet 2 are handshake packets.
|
||||||
for i := uint64(1); i <= r.MessageIndex; i++ {
|
ci.messageCounter.Add(2)
|
||||||
ci.window.Update(nil, i)
|
|
||||||
}
|
return ci, nil
|
||||||
return ci
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *ConnectionState) MarshalJSON() ([]byte, error) {
|
func (cs *ConnectionState) MarshalJSON() ([]byte, error) {
|
||||||
|
|||||||
@@ -1,114 +0,0 @@
|
|||||||
package nebula
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/netip"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
|
||||||
"github.com/slackhq/nebula/cert"
|
|
||||||
ct "github.com/slackhq/nebula/cert_test"
|
|
||||||
"github.com/slackhq/nebula/handshake"
|
|
||||||
"github.com/slackhq/nebula/header"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
// runTestHandshake runs a complete IX handshake between two freshly-built
|
|
||||||
// peers and returns the initiator and responder Results. Used to produce
|
|
||||||
// real cipher states for tests that need to exercise post-handshake glue.
|
|
||||||
func runTestHandshake(t *testing.T) (initR, respR *handshake.Result) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
ca, _, caKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca)
|
|
||||||
|
|
||||||
makeCreds := func(name string, networks []netip.Prefix) handshake.GetCredentialFunc {
|
|
||||||
c, _, rawKey, _ := ct.NewTestCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, ca, caKey,
|
|
||||||
name, ca.NotBefore(), ca.NotAfter(), networks, nil, nil,
|
|
||||||
)
|
|
||||||
priv, _, _, err := cert.UnmarshalPrivateKeyFromPEM(rawKey)
|
|
||||||
require.NoError(t, err)
|
|
||||||
hsBytes, err := c.MarshalForHandshakes()
|
|
||||||
require.NoError(t, err)
|
|
||||||
ncs := noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashSHA256)
|
|
||||||
cred := handshake.NewCredential(c, hsBytes, priv, ncs)
|
|
||||||
return func(v cert.Version) *handshake.Credential {
|
|
||||||
if v == cert.Version2 {
|
|
||||||
return cred
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
verifier := func(c cert.Certificate) (*cert.CachedCertificate, error) {
|
|
||||||
return caPool.VerifyCertificate(time.Now(), c)
|
|
||||||
}
|
|
||||||
|
|
||||||
initCreds := makeCreds("initiator", []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")})
|
|
||||||
respCreds := makeCreds("responder", []netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")})
|
|
||||||
|
|
||||||
initM, err := handshake.NewMachine(
|
|
||||||
cert.Version2, initCreds, verifier,
|
|
||||||
func() (uint32, error) { return 1000, nil },
|
|
||||||
true, header.HandshakeIXPSK0,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
respM, err := handshake.NewMachine(
|
|
||||||
cert.Version2, respCreds, verifier,
|
|
||||||
func() (uint32, error) { return 2000, nil },
|
|
||||||
false, header.HandshakeIXPSK0,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
msg1, err := initM.Initiate(nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
resp, respR, err := respM.ProcessPacket(nil, msg1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, respR)
|
|
||||||
|
|
||||||
_, initR, err = initM.ProcessPacket(nil, resp)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, initR)
|
|
||||||
|
|
||||||
return initR, respR
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewConnectionStateFromResult(t *testing.T) {
|
|
||||||
initR, respR := runTestHandshake(t)
|
|
||||||
|
|
||||||
t.Run("initiator", func(t *testing.T) {
|
|
||||||
ci := newConnectionStateFromResult(initR)
|
|
||||||
assert.True(t, ci.initiator)
|
|
||||||
assert.Equal(t, initR.MyCert, ci.myCert)
|
|
||||||
assert.Equal(t, initR.RemoteCert, ci.peerCert)
|
|
||||||
assert.NotNil(t, ci.eKey)
|
|
||||||
assert.NotNil(t, ci.dKey)
|
|
||||||
|
|
||||||
// IX has 2 handshake messages; the next data-plane send is counter=3.
|
|
||||||
assert.Equal(t, uint64(2), ci.messageCounter.Load(),
|
|
||||||
"messageCounter must equal Result.MessageIndex so the next send is N+1")
|
|
||||||
|
|
||||||
// Both handshake counters must be marked seen so they don't appear lost.
|
|
||||||
// Check returns false if an index has already been recorded.
|
|
||||||
assert.False(t, ci.window.Check(nil, 1), "counter 1 must already be seen")
|
|
||||||
assert.False(t, ci.window.Check(nil, 2), "counter 2 must already be seen")
|
|
||||||
// Counter 3 is the next data-plane message and must NOT be pre-marked.
|
|
||||||
assert.True(t, ci.window.Check(nil, 3), "counter 3 must not be pre-seeded")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("responder", func(t *testing.T) {
|
|
||||||
ci := newConnectionStateFromResult(respR)
|
|
||||||
assert.False(t, ci.initiator)
|
|
||||||
assert.Equal(t, respR.MyCert, ci.myCert)
|
|
||||||
assert.Equal(t, respR.RemoteCert, ci.peerCert)
|
|
||||||
assert.NotNil(t, ci.eKey)
|
|
||||||
assert.NotNil(t, ci.dKey)
|
|
||||||
assert.Equal(t, uint64(2), ci.messageCounter.Load())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -54,7 +54,6 @@ type Control struct {
|
|||||||
dnsStart func()
|
dnsStart func()
|
||||||
lighthouseStart func()
|
lighthouseStart func()
|
||||||
connectionManagerStart func(context.Context)
|
connectionManagerStart func(context.Context)
|
||||||
pmtudManagerStart func(context.Context)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ControlHostInfo struct {
|
type ControlHostInfo struct {
|
||||||
@@ -108,9 +107,6 @@ func (c *Control) Start() (func() error, error) {
|
|||||||
if c.connectionManagerStart != nil {
|
if c.connectionManagerStart != nil {
|
||||||
go c.connectionManagerStart(c.ctx)
|
go c.connectionManagerStart(c.ctx)
|
||||||
}
|
}
|
||||||
if c.pmtudManagerStart != nil {
|
|
||||||
go c.pmtudManagerStart(c.ctx)
|
|
||||||
}
|
|
||||||
if c.lighthouseStart != nil {
|
if c.lighthouseStart != nil {
|
||||||
c.lighthouseStart()
|
c.lighthouseStart()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ func makeHandshakePacket(from, to netip.AddrPort, subtype header.MessageSubType,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshakeRetransmitDuplicate(t *testing.T) {
|
func TestHandshakeRetransmitDuplicate(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// Verify the responder correctly handles receiving the same msg1 multiple times
|
// Verify the responder correctly handles receiving the same msg1 multiple times
|
||||||
// (retransmission). The duplicate goes through CheckAndComplete -> ErrAlreadySeen
|
// (retransmission). The duplicate goes through CheckAndComplete -> ErrAlreadySeen
|
||||||
// and the cached response is resent.
|
// and the cached response is resent.
|
||||||
@@ -79,7 +78,6 @@ func TestHandshakeRetransmitDuplicate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshakeTruncatedPacketRecovery(t *testing.T) {
|
func TestHandshakeTruncatedPacketRecovery(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// Verify that a truncated handshake packet is ignored and the real
|
// Verify that a truncated handshake packet is ignored and the real
|
||||||
// packet can still complete the handshake.
|
// packet can still complete the handshake.
|
||||||
|
|
||||||
@@ -128,7 +126,6 @@ func TestHandshakeTruncatedPacketRecovery(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshakeOrphanedMsg2Dropped(t *testing.T) {
|
func TestHandshakeOrphanedMsg2Dropped(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// A msg2 arriving with no matching pending index should be silently dropped
|
// A msg2 arriving with no matching pending index should be silently dropped
|
||||||
// with no response sent and no state changes.
|
// with no response sent and no state changes.
|
||||||
|
|
||||||
@@ -171,7 +168,6 @@ func TestHandshakeOrphanedMsg2Dropped(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshakeUnknownMessageCounter(t *testing.T) {
|
func TestHandshakeUnknownMessageCounter(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// A handshake packet with an unexpected message counter should be silently
|
// A handshake packet with an unexpected message counter should be silently
|
||||||
// dropped with no side effects and no UDP response.
|
// dropped with no side effects and no UDP response.
|
||||||
|
|
||||||
@@ -203,7 +199,6 @@ func TestHandshakeUnknownMessageCounter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshakeUnknownSubtype(t *testing.T) {
|
func TestHandshakeUnknownSubtype(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// A handshake packet with an unknown subtype should be silently dropped.
|
// A handshake packet with an unknown subtype should be silently dropped.
|
||||||
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
@@ -229,7 +224,6 @@ func TestHandshakeUnknownSubtype(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshakeLateResponse(t *testing.T) {
|
func TestHandshakeLateResponse(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// After a handshake times out, a late response should be silently ignored
|
// After a handshake times out, a late response should be silently ignored
|
||||||
// with no new tunnels created.
|
// with no new tunnels created.
|
||||||
|
|
||||||
@@ -279,7 +273,6 @@ func TestHandshakeLateResponse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshakeSelfConnectionRejected(t *testing.T) {
|
func TestHandshakeSelfConnectionRejected(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// Verify that a node rejects a handshake containing its own VPN IP in the
|
// Verify that a node rejects a handshake containing its own VPN IP in the
|
||||||
// peer cert. We do this by sending the initiator's own msg1 back to itself.
|
// peer cert. We do this by sending the initiator's own msg1 back to itself.
|
||||||
|
|
||||||
@@ -328,7 +321,6 @@ func TestHandshakeSelfConnectionRejected(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshakeMessageCounter0Dropped(t *testing.T) {
|
func TestHandshakeMessageCounter0Dropped(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// MessageCounter=0 is not a valid handshake message and should be dropped.
|
// MessageCounter=0 is not a valid handshake message and should be dropped.
|
||||||
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
@@ -349,7 +341,6 @@ func TestHandshakeMessageCounter0Dropped(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshakeRemoteAllowList(t *testing.T) {
|
func TestHandshakeRemoteAllowList(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// Verify that a handshake from a blocked underlay IP is dropped with no
|
// Verify that a handshake from a blocked underlay IP is dropped with no
|
||||||
// response and no state changes. Then verify the same packet from an
|
// response and no state changes. Then verify the same packet from an
|
||||||
// allowed IP succeeds.
|
// allowed IP succeeds.
|
||||||
@@ -408,7 +399,6 @@ func TestHandshakeRemoteAllowList(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshakeAlreadySeenPreferredRemote(t *testing.T) {
|
func TestHandshakeAlreadySeenPreferredRemote(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// When a duplicate msg1 arrives via ErrAlreadySeen, verify the tunnel
|
// When a duplicate msg1 arrives via ErrAlreadySeen, verify the tunnel
|
||||||
// remains functional and hostmap index count is stable.
|
// remains functional and hostmap index count is stable.
|
||||||
|
|
||||||
@@ -455,7 +445,6 @@ func TestHandshakeAlreadySeenPreferredRemote(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshakeWrongResponderPacketStore(t *testing.T) {
|
func TestHandshakeWrongResponderPacketStore(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// Verify that when the wrong host responds, the cached packets are
|
// Verify that when the wrong host responds, the cached packets are
|
||||||
// transferred to the new handshake, the evil tunnel is closed, evil's
|
// transferred to the new handshake, the evil tunnel is closed, evil's
|
||||||
// address is blocked, and the correct tunnel is eventually established.
|
// address is blocked, and the correct tunnel is eventually established.
|
||||||
@@ -519,7 +508,6 @@ func TestHandshakeWrongResponderPacketStore(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshakeRelayComplete(t *testing.T) {
|
func TestHandshakeRelayComplete(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// Verify that a relay handshake completes correctly and relay state is
|
// Verify that a relay handshake completes correctly and relay state is
|
||||||
// properly maintained on all three nodes.
|
// properly maintained on all three nodes.
|
||||||
|
|
||||||
|
|||||||
@@ -84,7 +84,6 @@ func BenchmarkHotPathRelay(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGoodHandshake(t *testing.T) {
|
func TestGoodHandshake(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
|
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
|
||||||
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
|
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
|
||||||
@@ -135,7 +134,6 @@ func TestGoodHandshake(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGoodHandshakeNoOverlap(t *testing.T) {
|
func TestGoodHandshakeNoOverlap(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.1/24", nil)
|
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.1/24", nil)
|
||||||
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "2001::69/24", nil) //look ma, cross-stack!
|
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "2001::69/24", nil) //look ma, cross-stack!
|
||||||
@@ -171,7 +169,6 @@ func TestGoodHandshakeNoOverlap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestWrongResponderHandshake(t *testing.T) {
|
func TestWrongResponderHandshake(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
|
|
||||||
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.100/24", nil)
|
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.100/24", nil)
|
||||||
@@ -248,7 +245,6 @@ func TestWrongResponderHandshake(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestWrongResponderHandshakeStaticHostMap(t *testing.T) {
|
func TestWrongResponderHandshakeStaticHostMap(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
|
|
||||||
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.99/24", nil)
|
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.99/24", nil)
|
||||||
@@ -331,7 +327,6 @@ func TestWrongResponderHandshakeStaticHostMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStage1Race(t *testing.T) {
|
func TestStage1Race(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// This tests ensures that two hosts handshaking with each other at the same time will allow traffic to flow
|
// This tests ensures that two hosts handshaking with each other at the same time will allow traffic to flow
|
||||||
// But will eventually collapse down to a single tunnel
|
// But will eventually collapse down to a single tunnel
|
||||||
|
|
||||||
@@ -412,7 +407,6 @@ func TestStage1Race(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUncleanShutdownRaceLoser(t *testing.T) {
|
func TestUncleanShutdownRaceLoser(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", nil)
|
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", nil)
|
||||||
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
|
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
|
||||||
@@ -462,7 +456,6 @@ func TestUncleanShutdownRaceLoser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUncleanShutdownRaceWinner(t *testing.T) {
|
func TestUncleanShutdownRaceWinner(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", nil)
|
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", nil)
|
||||||
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
|
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
|
||||||
@@ -514,7 +507,6 @@ func TestUncleanShutdownRaceWinner(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRelays(t *testing.T) {
|
func TestRelays(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
|
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
|
||||||
relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
|
relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
|
||||||
@@ -544,7 +536,6 @@ func TestRelays(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRelaysDontCareAboutIps(t *testing.T) {
|
func TestRelaysDontCareAboutIps(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
|
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
|
||||||
relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "relay ", "2001::9999/24", m{"relay": m{"am_relay": true}})
|
relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "relay ", "2001::9999/24", m{"relay": m{"am_relay": true}})
|
||||||
@@ -574,7 +565,6 @@ func TestRelaysDontCareAboutIps(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReestablishRelays(t *testing.T) {
|
func TestReestablishRelays(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
|
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
|
||||||
relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
|
relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
|
||||||
@@ -706,7 +696,6 @@ func TestReestablishRelays(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStage1RaceRelays(t *testing.T) {
|
func TestStage1RaceRelays(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
//NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
|
//NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
|
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
|
||||||
@@ -754,7 +743,6 @@ func TestStage1RaceRelays(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStage1RaceRelays2(t *testing.T) {
|
func TestStage1RaceRelays2(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
//NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
|
//NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
|
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
|
||||||
@@ -831,7 +819,6 @@ func TestStage1RaceRelays2(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRehandshakingRelays(t *testing.T) {
|
func TestRehandshakingRelays(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
|
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
|
||||||
relayControl, relayVpnIpNet, relayUdpAddr, relayConfig := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
|
relayControl, relayVpnIpNet, relayUdpAddr, relayConfig := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
|
||||||
@@ -935,7 +922,6 @@ func TestRehandshakingRelays(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRehandshakingRelaysPrimary(t *testing.T) {
|
func TestRehandshakingRelaysPrimary(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// This test is the same as TestRehandshakingRelays but one of the terminal types is a primary swap winner
|
// This test is the same as TestRehandshakingRelays but one of the terminal types is a primary swap winner
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.128/24", m{"relay": m{"use_relays": true}})
|
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.128/24", m{"relay": m{"use_relays": true}})
|
||||||
@@ -1040,7 +1026,6 @@ func TestRehandshakingRelaysPrimary(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRehandshaking(t *testing.T) {
|
func TestRehandshaking(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.2/24", nil)
|
myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.2/24", nil)
|
||||||
theirControl, theirVpnIpNet, theirUdpAddr, theirConfig := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.1/24", nil)
|
theirControl, theirVpnIpNet, theirUdpAddr, theirConfig := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.1/24", nil)
|
||||||
@@ -1136,7 +1121,6 @@ func TestRehandshaking(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRehandshakingLoser(t *testing.T) {
|
func TestRehandshakingLoser(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// The purpose of this test is that the race loser renews their certificate and rehandshakes. The final tunnel
|
// The purpose of this test is that the race loser renews their certificate and rehandshakes. The final tunnel
|
||||||
// Should be the one with the new certificate
|
// Should be the one with the new certificate
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
@@ -1235,7 +1219,6 @@ func TestRehandshakingLoser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRaceRegression(t *testing.T) {
|
func TestRaceRegression(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// This test forces stage 1, stage 2, stage 1 to be received by me from them
|
// This test forces stage 1, stage 2, stage 1 to be received by me from them
|
||||||
// We had a bug where we were not finding the duplicate handshake and responding to the final stage 1 which
|
// We had a bug where we were not finding the duplicate handshake and responding to the final stage 1 which
|
||||||
// caused a cross-linked hostinfo
|
// caused a cross-linked hostinfo
|
||||||
@@ -1296,7 +1279,6 @@ func TestRaceRegression(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestV2NonPrimaryWithLighthouse(t *testing.T) {
|
func TestV2NonPrimaryWithLighthouse(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh ", "10.128.0.1/24, ff::1/64", m{"lighthouse": m{"am_lighthouse": true}})
|
lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh ", "10.128.0.1/24, ff::1/64", m{"lighthouse": m{"am_lighthouse": true}})
|
||||||
|
|
||||||
@@ -1337,7 +1319,6 @@ func TestV2NonPrimaryWithLighthouse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestV2NonPrimaryWithOffNetLighthouse(t *testing.T) {
|
func TestV2NonPrimaryWithOffNetLighthouse(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh ", "2001::1/64", m{"lighthouse": m{"am_lighthouse": true}})
|
lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh ", "2001::1/64", m{"lighthouse": m{"am_lighthouse": true}})
|
||||||
|
|
||||||
@@ -1378,7 +1359,6 @@ func TestV2NonPrimaryWithOffNetLighthouse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLighthouseUpdateOnReload(t *testing.T) {
|
func TestLighthouseUpdateOnReload(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
|
|
||||||
// Create the lighthouse
|
// Create the lighthouse
|
||||||
@@ -1454,7 +1434,6 @@ func TestLighthouseUpdateOnReload(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGoodHandshakeUnsafeDest(t *testing.T) {
|
func TestGoodHandshakeUnsafeDest(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
unsafePrefix := "192.168.6.0/24"
|
unsafePrefix := "192.168.6.0/24"
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServerWithUdpAndUnsafeNetworks(cert.Version2, ca, caKey, "spooky", "10.128.0.2/24", netip.MustParseAddrPort("10.64.0.2:4242"), unsafePrefix, nil)
|
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServerWithUdpAndUnsafeNetworks(cert.Version2, ca, caKey, "spooky", "10.128.0.2/24", netip.MustParseAddrPort("10.64.0.2:4242"), unsafePrefix, nil)
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
//go:build e2e_testing
|
|
||||||
// +build e2e_testing
|
|
||||||
|
|
||||||
package e2e
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/slackhq/nebula/cert"
|
|
||||||
"github.com/slackhq/nebula/cert_test"
|
|
||||||
"github.com/slackhq/nebula/e2e/router"
|
|
||||||
"go.uber.org/goleak"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestNoGoroutineLeaks brings up two nebula instances, completes a tunnel,
|
|
||||||
// stops both, and asserts no goroutines leak past the shutdown. goleak's
|
|
||||||
// retry mechanism gives the wg.Wait()-driven goroutines a moment to drain
|
|
||||||
// before failing the assertion.
|
|
||||||
//
|
|
||||||
// IgnoreCurrent is necessary in the parallelized suite: other tests can
|
|
||||||
// leave goroutines mid-shutdown when this one runs (Stop is async, the
|
|
||||||
// wg.Wait() drain is not blocking on test return). We're checking that
|
|
||||||
// *this* test's setup tears down cleanly, not that the whole suite is
|
|
||||||
// idle at this moment. Intentionally NOT t.Parallel()'d for the same
|
|
||||||
// reason — concurrent test goroutines would always show up.
|
|
||||||
func TestNoGoroutineLeaks(t *testing.T) {
|
|
||||||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
|
|
||||||
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
|
||||||
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
|
|
||||||
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
|
|
||||||
|
|
||||||
myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
|
|
||||||
theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
|
|
||||||
|
|
||||||
myControl.Start()
|
|
||||||
theirControl.Start()
|
|
||||||
|
|
||||||
r := router.NewR(t, myControl, theirControl)
|
|
||||||
assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
|
|
||||||
|
|
||||||
myControl.Stop()
|
|
||||||
theirControl.Stop()
|
|
||||||
r.RenderFlow()
|
|
||||||
|
|
||||||
// Settle period: Stop() is non-blocking; the wg-driven goroutines need
|
|
||||||
// a moment to drain. goleak retries internally too, but a short explicit
|
|
||||||
// settle reduces flakes when the suite is busy.
|
|
||||||
time.Sleep(50 * time.Millisecond)
|
|
||||||
}
|
|
||||||
@@ -19,7 +19,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestDropInactiveTunnels(t *testing.T) {
|
func TestDropInactiveTunnels(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides
|
// The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides
|
||||||
// under ideal conditions
|
// under ideal conditions
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
@@ -64,7 +63,6 @@ func TestDropInactiveTunnels(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCertUpgrade(t *testing.T) {
|
func TestCertUpgrade(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides
|
// The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides
|
||||||
// under ideal conditions
|
// under ideal conditions
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
@@ -159,7 +157,6 @@ func TestCertUpgrade(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCertDowngrade(t *testing.T) {
|
func TestCertDowngrade(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides
|
// The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides
|
||||||
// under ideal conditions
|
// under ideal conditions
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
@@ -258,7 +255,6 @@ func TestCertDowngrade(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCertMismatchCorrection(t *testing.T) {
|
func TestCertMismatchCorrection(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
// The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides
|
// The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides
|
||||||
// under ideal conditions
|
// under ideal conditions
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
@@ -326,7 +322,6 @@ func TestCertMismatchCorrection(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCrossStackRelaysWork(t *testing.T) {
|
func TestCrossStackRelaysWork(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.1/24,fc00::1/64", m{"relay": m{"use_relays": true}})
|
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.1/24,fc00::1/64", m{"relay": m{"use_relays": true}})
|
||||||
relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "relay ", "10.128.0.128/24,fc00::128/64", m{"relay": m{"am_relay": true}})
|
relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "relay ", "10.128.0.128/24,fc00::128/64", m{"relay": m{"am_relay": true}})
|
||||||
@@ -374,7 +369,6 @@ func TestCrossStackRelaysWork(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCloseTunnelAuthenticated(t *testing.T) {
|
func TestCloseTunnelAuthenticated(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", m{"tunnels": m{"drop_inactive": true, "inactivity_timeout": "5s"}})
|
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", m{"tunnels": m{"drop_inactive": true, "inactivity_timeout": "5s"}})
|
||||||
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", m{"tunnels": m{"drop_inactive": true, "inactivity_timeout": "10m"}})
|
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", m{"tunnels": m{"drop_inactive": true, "inactivity_timeout": "10m"}})
|
||||||
|
|||||||
+1
-1
@@ -1033,7 +1033,7 @@ func TestNewFirewallFromConfig(t *testing.T) {
|
|||||||
l := test.NewLogger()
|
l := test.NewLogger()
|
||||||
// Test a bad rule definition
|
// Test a bad rule definition
|
||||||
c := &dummyCert{}
|
c := &dummyCert{}
|
||||||
cs, err := newCertState(cert.Version2, nil, c, false, cert.Curve_CURVE25519, nil, "aes")
|
cs, err := newCertState(cert.Version2, nil, c, false, cert.Curve_CURVE25519, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
conf := config.NewC(test.NewLogger())
|
conf := config.NewC(test.NewLogger())
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ require (
|
|||||||
github.com/stefanberger/go-pkcs11uri v0.0.0-20230803200340-78284954bff6
|
github.com/stefanberger/go-pkcs11uri v0.0.0-20230803200340-78284954bff6
|
||||||
github.com/stretchr/testify v1.11.1
|
github.com/stretchr/testify v1.11.1
|
||||||
github.com/vishvananda/netlink v1.3.1
|
github.com/vishvananda/netlink v1.3.1
|
||||||
go.uber.org/goleak v1.3.0
|
|
||||||
go.yaml.in/yaml/v3 v3.0.4
|
go.yaml.in/yaml/v3 v3.0.4
|
||||||
golang.org/x/crypto v0.50.0
|
golang.org/x/crypto v0.50.0
|
||||||
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
|
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
|
||||||
|
|||||||
@@ -1,57 +0,0 @@
|
|||||||
package handshake
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/rand"
|
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
|
||||||
"github.com/slackhq/nebula/cert"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Credential holds everything needed to participate in a handshake
|
|
||||||
// at a given cert version. Version and Curve are read from Cert; the public
|
|
||||||
// half of the static keypair likewise comes from Cert.PublicKey().
|
|
||||||
type Credential struct {
|
|
||||||
Cert cert.Certificate // the certificate
|
|
||||||
Bytes []byte // pre-marshaled certificate bytes
|
|
||||||
privateKey []byte // static private key (public half lives in Cert)
|
|
||||||
cipherSuite noise.CipherSuite // pre-built cipher suite (DH + cipher + hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewCredential creates a Credential with all material needed for handshake
|
|
||||||
// participation. The cipherSuite should be pre-built by the caller with the
|
|
||||||
// appropriate DH function, cipher, and hash.
|
|
||||||
func NewCredential(
|
|
||||||
c cert.Certificate,
|
|
||||||
hsBytes []byte,
|
|
||||||
privateKey []byte,
|
|
||||||
cipherSuite noise.CipherSuite,
|
|
||||||
) *Credential {
|
|
||||||
return &Credential{
|
|
||||||
Cert: c,
|
|
||||||
Bytes: hsBytes,
|
|
||||||
privateKey: privateKey,
|
|
||||||
cipherSuite: cipherSuite,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// buildHandshakeState creates a noise.HandshakeState from this credential.
|
|
||||||
func (hc *Credential) buildHandshakeState(initiator bool, pattern noise.HandshakePattern) (*noise.HandshakeState, error) {
|
|
||||||
return noise.NewHandshakeState(noise.Config{
|
|
||||||
CipherSuite: hc.cipherSuite,
|
|
||||||
Random: rand.Reader,
|
|
||||||
Pattern: pattern,
|
|
||||||
Initiator: initiator,
|
|
||||||
StaticKeypair: noise.DHKey{Private: hc.privateKey, Public: hc.Cert.PublicKey()},
|
|
||||||
PresharedKey: []byte{},
|
|
||||||
PresharedKeyPlacement: 0,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetCredentialFunc returns the handshake credential for the given version,
|
|
||||||
// or nil if that version is not available.
|
|
||||||
//
|
|
||||||
// Implementations must return credentials drawn from a snapshot stable for
|
|
||||||
// the lifetime of any single Machine. The Machine may call this multiple
|
|
||||||
// times during a handshake (e.g. when negotiating to the peer's version)
|
|
||||||
// and assumes the underlying static keypair is consistent across calls.
|
|
||||||
type GetCredentialFunc func(v cert.Version) *Credential
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
package handshake
|
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrInitiateOnResponder = errors.New("initiate called on responder")
|
|
||||||
ErrInitiateAlreadyCalled = errors.New("initiate already called")
|
|
||||||
ErrInitiateNotCalled = errors.New("initiate must be called before ProcessPacket for initiators")
|
|
||||||
ErrPacketTooShort = errors.New("packet too short")
|
|
||||||
ErrPublicKeyMismatch = errors.New("public key mismatch between certificate and handshake")
|
|
||||||
ErrIncompleteHandshake = errors.New("handshake completed without receiving required content")
|
|
||||||
ErrMachineFailed = errors.New("handshake machine has failed")
|
|
||||||
ErrUnknownSubtype = errors.New("unknown handshake subtype")
|
|
||||||
ErrMissingContent = errors.New("expected handshake content but message was empty")
|
|
||||||
ErrUnexpectedContent = errors.New("received unexpected handshake content")
|
|
||||||
ErrIndexAllocation = errors.New("failed to allocate local index")
|
|
||||||
ErrNoCredential = errors.New("no handshake credential available for cert version")
|
|
||||||
ErrAsymmetricCipherKeys = errors.New("noise produced only one cipher key")
|
|
||||||
ErrMultiMessageUnsupported = errors.New("multi-message handshake patterns are not yet supported by the manager")
|
|
||||||
ErrSubtypeMismatch = errors.New("packet subtype does not match handshake machine subtype")
|
|
||||||
)
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
// This file documents the wire format the nebula handshake speaks. It is
|
|
||||||
// not run through protoc; the encoder/decoder in payload.go is hand-written
|
|
||||||
// against this shape directly to keep the parser narrow and panic-free.
|
|
||||||
//
|
|
||||||
// Any change to the wire format must be reflected here, and adding a new
|
|
||||||
// field requires updating MarshalPayload / unmarshalPayloadDetails together
|
|
||||||
// with the field-uniqueness and wire-type checks in those functions.
|
|
||||||
|
|
||||||
syntax = "proto3";
|
|
||||||
package nebula.handshake;
|
|
||||||
|
|
||||||
message NebulaHandshake {
|
|
||||||
NebulaHandshakeDetails Details = 1;
|
|
||||||
bytes Hmac = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
message NebulaHandshakeDetails {
|
|
||||||
bytes Cert = 1;
|
|
||||||
uint32 InitiatorIndex = 2;
|
|
||||||
uint32 ResponderIndex = 3;
|
|
||||||
// Cookie was reserved for an anti-DoS mechanism that was never
|
|
||||||
// implemented. No released version of nebula has ever populated it; the
|
|
||||||
// hand-written parser silently skips it on read.
|
|
||||||
uint64 Cookie = 4 [deprecated = true];
|
|
||||||
uint64 Time = 5;
|
|
||||||
uint32 CertVersion = 8;
|
|
||||||
// reserved for WIP multiport
|
|
||||||
reserved 6, 7;
|
|
||||||
}
|
|
||||||
@@ -1,116 +0,0 @@
|
|||||||
package handshake
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/netip"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
|
||||||
"github.com/slackhq/nebula/cert"
|
|
||||||
ct "github.com/slackhq/nebula/cert_test"
|
|
||||||
"github.com/slackhq/nebula/header"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
// testCertState holds cert material for a test peer.
|
|
||||||
type testCertState struct {
|
|
||||||
version cert.Version
|
|
||||||
creds map[cert.Version]*Credential
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *testCertState) getCredential(v cert.Version) *Credential {
|
|
||||||
return s.creds[v]
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTestCertState(
|
|
||||||
t *testing.T, ca cert.Certificate, caKey []byte, name string, networks []netip.Prefix,
|
|
||||||
) *testCertState {
|
|
||||||
return newTestCertStateWithCipher(t, ca, caKey, name, networks, noise.CipherChaChaPoly)
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTestCertStateWithCipher(
|
|
||||||
t *testing.T, ca cert.Certificate, caKey []byte, name string, networks []netip.Prefix,
|
|
||||||
cipher noise.CipherFunc,
|
|
||||||
) *testCertState {
|
|
||||||
t.Helper()
|
|
||||||
c, _, rawPrivKey, _ := ct.NewTestCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, ca, caKey,
|
|
||||||
name, ca.NotBefore(), ca.NotAfter(), networks, nil, nil,
|
|
||||||
)
|
|
||||||
|
|
||||||
priv, _, _, err := cert.UnmarshalPrivateKeyFromPEM(rawPrivKey)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
hsBytes, err := c.MarshalForHandshakes()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
ncs := noise.NewCipherSuite(noise.DH25519, cipher, noise.HashSHA256)
|
|
||||||
return &testCertState{
|
|
||||||
version: cert.Version2,
|
|
||||||
creds: map[cert.Version]*Credential{
|
|
||||||
cert.Version2: NewCredential(c, hsBytes, priv, ncs),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testVerifier(pool *cert.CAPool) CertVerifier {
|
|
||||||
return func(c cert.Certificate) (*cert.CachedCertificate, error) {
|
|
||||||
return pool.VerifyCertificate(time.Now(), c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTestMachine(
|
|
||||||
t *testing.T,
|
|
||||||
cs *testCertState,
|
|
||||||
verifier CertVerifier,
|
|
||||||
initiator bool,
|
|
||||||
localIndex uint32,
|
|
||||||
) *Machine {
|
|
||||||
t.Helper()
|
|
||||||
m, err := NewMachine(
|
|
||||||
cs.version, cs.getCredential,
|
|
||||||
verifier, func() (uint32, error) { return localIndex, nil },
|
|
||||||
initiator, header.HandshakeIXPSK0,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func initiateHandshake(
|
|
||||||
t *testing.T,
|
|
||||||
initCS *testCertState, initVerifier CertVerifier,
|
|
||||||
respCS *testCertState, respVerifier CertVerifier,
|
|
||||||
) (initM, respM *Machine, respResult *Result, resp []byte, err error) {
|
|
||||||
t.Helper()
|
|
||||||
initM = newTestMachine(t, initCS, initVerifier, true, 100)
|
|
||||||
msg1, merr := initM.Initiate(nil)
|
|
||||||
require.NoError(t, merr)
|
|
||||||
|
|
||||||
respM = newTestMachine(t, respCS, respVerifier, false, 200)
|
|
||||||
resp, respResult, err = respM.ProcessPacket(nil, msg1)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func doFullHandshake(
|
|
||||||
t *testing.T, initCS, respCS *testCertState, caPool *cert.CAPool,
|
|
||||||
) (initResult, respResult *Result) {
|
|
||||||
t.Helper()
|
|
||||||
v := testVerifier(caPool)
|
|
||||||
|
|
||||||
initM := newTestMachine(t, initCS, v, true, 1000)
|
|
||||||
respM := newTestMachine(t, respCS, v, false, 2000)
|
|
||||||
|
|
||||||
msg1, err := initM.Initiate(nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
resp, respResult, err := respM.ProcessPacket(nil, msg1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, respResult)
|
|
||||||
require.NotEmpty(t, resp)
|
|
||||||
|
|
||||||
_, initResult, err = initM.ProcessPacket(nil, resp)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, initResult)
|
|
||||||
|
|
||||||
return initResult, respResult
|
|
||||||
}
|
|
||||||
@@ -1,444 +0,0 @@
|
|||||||
package handshake
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"slices"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
|
||||||
"github.com/slackhq/nebula/cert"
|
|
||||||
"github.com/slackhq/nebula/header"
|
|
||||||
)
|
|
||||||
|
|
||||||
// IndexAllocator is called by the Machine to allocate a local index for the
|
|
||||||
// handshake. It is called at most once, when the first outgoing message that
|
|
||||||
// carries a payload is built.
|
|
||||||
//
|
|
||||||
// Implementations MUST NOT return 0. Zero is reserved as a sentinel meaning
|
|
||||||
// "no index assigned" on the wire and in the payload-presence checks. If an
|
|
||||||
// allocator ever returned 0, a legitimate handshake's payload could be
|
|
||||||
// indistinguishable from an empty one and would be rejected.
|
|
||||||
type IndexAllocator func() (uint32, error)
|
|
||||||
|
|
||||||
// CertVerifier is called by the Machine after reconstructing the peer's
|
|
||||||
// certificate from the handshake. The verifier performs all validation
|
|
||||||
// (CA trust, expiry, policy checks, allow lists).
|
|
||||||
type CertVerifier func(cert.Certificate) (*cert.CachedCertificate, error)
|
|
||||||
|
|
||||||
// Result contains the results of a successful handshake.
|
|
||||||
// Returned by ProcessPacket when the handshake is complete.
|
|
||||||
type Result struct {
|
|
||||||
EKey *noise.CipherState
|
|
||||||
DKey *noise.CipherState
|
|
||||||
MyCert cert.Certificate
|
|
||||||
RemoteCert *cert.CachedCertificate
|
|
||||||
RemoteIndex uint32
|
|
||||||
LocalIndex uint32
|
|
||||||
HandshakeTime uint64
|
|
||||||
MessageIndex uint64 // number of messages exchanged during the handshake
|
|
||||||
Initiator bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Machine drives a Noise handshake through N messages. It handles Noise
|
|
||||||
// protocol operations, certificate reconstruction, and payload encoding.
|
|
||||||
// Certificate validation is delegated to the caller via CertVerifier.
|
|
||||||
//
|
|
||||||
// A Machine is not safe for concurrent use. The caller must ensure that
|
|
||||||
// Initiate and ProcessPacket are not called concurrently.
|
|
||||||
//
|
|
||||||
// Error contract: when ProcessPacket or Initiate returns an error, callers
|
|
||||||
// must check Failed() to decide what to do next. If Failed() is false the
|
|
||||||
// underlying noise state was not advanced (the packet was rejected before
|
|
||||||
// ReadMessage took effect, or the rejection is non-fatal like a stale
|
|
||||||
// retransmit) and the Machine can accept another packet. If Failed() is
|
|
||||||
// true the Machine is unrecoverable and the caller must abandon it.
|
|
||||||
type Machine struct {
|
|
||||||
hs *noise.HandshakeState
|
|
||||||
getCred GetCredentialFunc
|
|
||||||
allocIndex IndexAllocator
|
|
||||||
verifier CertVerifier
|
|
||||||
result *Result
|
|
||||||
msgs []msgFlags
|
|
||||||
myVersion cert.Version
|
|
||||||
subtype header.MessageSubType
|
|
||||||
indexAllocated bool
|
|
||||||
remoteCertSet bool
|
|
||||||
payloadSet bool
|
|
||||||
failed bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMachine creates a handshake state machine. The subtype determines both
|
|
||||||
// the noise pattern and the per-message content layout. The credential for
|
|
||||||
// `version` is fetched via getCred and used to seed the noise.HandshakeState.
|
|
||||||
// IndexAllocator is called lazily when the first outgoing payload is built.
|
|
||||||
func NewMachine(
|
|
||||||
version cert.Version,
|
|
||||||
getCred GetCredentialFunc,
|
|
||||||
verifier CertVerifier,
|
|
||||||
allocIndex IndexAllocator,
|
|
||||||
initiator bool,
|
|
||||||
subtype header.MessageSubType,
|
|
||||||
) (*Machine, error) {
|
|
||||||
info, err := subtypeInfoFor(subtype)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cred := getCred(version)
|
|
||||||
if cred == nil {
|
|
||||||
return nil, fmt.Errorf("%w: %v", ErrNoCredential, version)
|
|
||||||
}
|
|
||||||
|
|
||||||
hs, err := cred.buildHandshakeState(initiator, info.pattern)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("build noise state: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Machine{
|
|
||||||
hs: hs,
|
|
||||||
subtype: subtype,
|
|
||||||
msgs: info.msgs,
|
|
||||||
getCred: getCred,
|
|
||||||
allocIndex: allocIndex,
|
|
||||||
verifier: verifier,
|
|
||||||
myVersion: version,
|
|
||||||
result: &Result{
|
|
||||||
Initiator: initiator,
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Failed returns true if the Machine is in an unrecoverable state.
|
|
||||||
func (m *Machine) Failed() bool {
|
|
||||||
return m.failed
|
|
||||||
}
|
|
||||||
|
|
||||||
// Subtype returns the handshake subtype this Machine was built for.
|
|
||||||
func (m *Machine) Subtype() header.MessageSubType {
|
|
||||||
return m.subtype
|
|
||||||
}
|
|
||||||
|
|
||||||
// MessageIndex returns the noise handshake message index, which equals the
|
|
||||||
// wire counter of the most recently sent or received message.
|
|
||||||
func (m *Machine) MessageIndex() int {
|
|
||||||
return m.hs.MessageIndex()
|
|
||||||
}
|
|
||||||
|
|
||||||
// requireComplete checks that both a peer cert and payload have been received.
|
|
||||||
// Marks the machine as failed if not.
|
|
||||||
func (m *Machine) requireComplete() error {
|
|
||||||
if !m.payloadSet || !m.remoteCertSet {
|
|
||||||
m.failed = true
|
|
||||||
return ErrIncompleteHandshake
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// myMsgFlags returns the flags for the current outgoing message.
|
|
||||||
func (m *Machine) myMsgFlags() msgFlags {
|
|
||||||
idx := m.hs.MessageIndex()
|
|
||||||
if idx < len(m.msgs) {
|
|
||||||
return m.msgs[idx]
|
|
||||||
}
|
|
||||||
return msgFlags{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// peerMsgFlags returns the flags for the message we just read.
|
|
||||||
func (m *Machine) peerMsgFlags() msgFlags {
|
|
||||||
idx := m.hs.MessageIndex() - 1
|
|
||||||
if idx >= 0 && idx < len(m.msgs) {
|
|
||||||
return m.msgs[idx]
|
|
||||||
}
|
|
||||||
return msgFlags{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initiate produces the first handshake message. Only valid for initiators,
|
|
||||||
// and must be called exactly once before ProcessPacket.
|
|
||||||
//
|
|
||||||
// out is a destination buffer the message is appended to and returned. Pass
|
|
||||||
// nil to allocate fresh, or pass a re-used buffer sliced to length 0 (e.g.
|
|
||||||
// buf[:0]) with sufficient capacity to avoid allocation.
|
|
||||||
//
|
|
||||||
// An error return may not indicate a fatal condition, check Failed() to
|
|
||||||
// determine if the Machine can still be used.
|
|
||||||
func (m *Machine) Initiate(out []byte) ([]byte, error) {
|
|
||||||
if m.failed {
|
|
||||||
return nil, ErrMachineFailed
|
|
||||||
}
|
|
||||||
if !m.result.Initiator {
|
|
||||||
m.failed = true
|
|
||||||
return nil, ErrInitiateOnResponder
|
|
||||||
}
|
|
||||||
if m.hs.MessageIndex() != 0 {
|
|
||||||
m.failed = true
|
|
||||||
return nil, ErrInitiateAlreadyCalled
|
|
||||||
}
|
|
||||||
|
|
||||||
// At MessageIndex=0 with RemoteIndex still zero, buildResponse produces
|
|
||||||
// header counter 1 and remote index 0, which is what the initial message needs.
|
|
||||||
out, _, _, err := m.buildResponse(out)
|
|
||||||
if err != nil {
|
|
||||||
m.failed = true
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProcessPacket handles an incoming handshake message. It advances the Noise
|
|
||||||
// state, validates the peer certificate via the verifier, and optionally
|
|
||||||
// produces a response.
|
|
||||||
//
|
|
||||||
// out is a destination buffer the response is appended to and returned. Pass
|
|
||||||
// nil to allocate fresh, or pass a re-used buffer sliced to length 0 (e.g.
|
|
||||||
// buf[:0]) with sufficient capacity to avoid allocation. The returned slice
|
|
||||||
// is nil when no outgoing message is produced (handshake complete on this
|
|
||||||
// side, or final message of a multi-message pattern).
|
|
||||||
//
|
|
||||||
// Returns a non-nil Result when the handshake is complete.
|
|
||||||
// An error return may not indicate a fatal condition, check Failed() to
|
|
||||||
// determine if the Machine can still be used.
|
|
||||||
func (m *Machine) ProcessPacket(out, packet []byte) ([]byte, *Result, error) {
|
|
||||||
if m.failed {
|
|
||||||
return nil, nil, ErrMachineFailed
|
|
||||||
}
|
|
||||||
if len(packet) < header.Len {
|
|
||||||
return nil, nil, ErrPacketTooShort
|
|
||||||
}
|
|
||||||
// Reject packets whose subtype doesn't match the one this Machine was
|
|
||||||
// built for. A pending handshake that suddenly receives a different
|
|
||||||
// subtype on its index is either a stray packet that matched by chance
|
|
||||||
// or a peer protocol violation; drop it without failing the Machine so
|
|
||||||
// the legitimate retransmit can still complete.
|
|
||||||
if header.MessageSubType(packet[1]) != m.subtype {
|
|
||||||
return nil, nil, ErrSubtypeMismatch
|
|
||||||
}
|
|
||||||
if m.result.Initiator && m.hs.MessageIndex() == 0 {
|
|
||||||
m.failed = true
|
|
||||||
return nil, nil, ErrInitiateNotCalled
|
|
||||||
}
|
|
||||||
|
|
||||||
// The (eKey, dKey) ordering here is correct for IX, where the initiator
|
|
||||||
// completes the handshake by reading the responder's stage-2 message.
|
|
||||||
// noise returns (cs1, cs2) where cs1 is the initiator->responder cipher.
|
|
||||||
// For 3-message patterns where a responder finishes by reading the final
|
|
||||||
// message, this ordering would be wrong; revisit when XX/pqIX lands.
|
|
||||||
msg, eKey, dKey, err := m.hs.ReadMessage(nil, packet[header.Len:])
|
|
||||||
if err != nil {
|
|
||||||
// Noise ReadMessage failed. The noise library checkpoints and rolls back
|
|
||||||
// on failure, so the Machine is still alive. The caller can retry with
|
|
||||||
// a different packet.
|
|
||||||
return nil, nil, fmt.Errorf("noise ReadMessage: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// From here on, noise state has advanced. Any error is fatal.
|
|
||||||
flags := m.peerMsgFlags()
|
|
||||||
|
|
||||||
if err := m.processPayload(msg, flags); err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// If ReadMessage derived keys, the handshake is complete. Noise should
|
|
||||||
// always produce both keys together; asymmetry is a protocol invariant
|
|
||||||
// violation.
|
|
||||||
if eKey != nil || dKey != nil {
|
|
||||||
if eKey == nil || dKey == nil {
|
|
||||||
m.failed = true
|
|
||||||
return nil, nil, ErrAsymmetricCipherKeys
|
|
||||||
}
|
|
||||||
if err := m.requireComplete(); err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
return nil, m.completed(eKey, dKey), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadMessage didn't complete, produce the next outgoing message
|
|
||||||
out, dk, ek, err := m.buildResponse(out)
|
|
||||||
if err != nil {
|
|
||||||
m.failed = true
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if ek != nil || dk != nil {
|
|
||||||
if ek == nil || dk == nil {
|
|
||||||
m.failed = true
|
|
||||||
return nil, nil, ErrAsymmetricCipherKeys
|
|
||||||
}
|
|
||||||
if err := m.requireComplete(); err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
return out, m.completed(ek, dk), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return out, nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Machine) completed(eKey, dKey *noise.CipherState) *Result {
|
|
||||||
m.result.EKey = eKey
|
|
||||||
m.result.DKey = dKey
|
|
||||||
m.result.MessageIndex = uint64(m.hs.MessageIndex())
|
|
||||||
return m.result
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Machine) processPayload(msg []byte, flags msgFlags) error {
|
|
||||||
if len(msg) == 0 {
|
|
||||||
if flags.expectsPayload || flags.expectsCert {
|
|
||||||
m.failed = true
|
|
||||||
return ErrMissingContent
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
payload, err := UnmarshalPayload(msg)
|
|
||||||
if err != nil {
|
|
||||||
m.failed = true
|
|
||||||
return fmt.Errorf("unmarshal handshake: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assert the payload contains exactly what we expect
|
|
||||||
hasPayloadData := payload.InitiatorIndex != 0 || payload.ResponderIndex != 0 || payload.Time != 0
|
|
||||||
if hasPayloadData != flags.expectsPayload {
|
|
||||||
m.failed = true
|
|
||||||
return ErrUnexpectedContent
|
|
||||||
}
|
|
||||||
|
|
||||||
hasCertData := len(payload.Cert) > 0
|
|
||||||
if hasCertData != flags.expectsCert {
|
|
||||||
m.failed = true
|
|
||||||
return ErrUnexpectedContent
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process payload
|
|
||||||
if flags.expectsPayload {
|
|
||||||
if m.result.Initiator {
|
|
||||||
m.result.RemoteIndex = payload.ResponderIndex
|
|
||||||
} else {
|
|
||||||
m.result.RemoteIndex = payload.InitiatorIndex
|
|
||||||
}
|
|
||||||
m.result.HandshakeTime = payload.Time
|
|
||||||
m.payloadSet = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process certificate
|
|
||||||
if flags.expectsCert {
|
|
||||||
if err := m.validateCert(payload); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Machine) validateCert(payload Payload) error {
|
|
||||||
cred := m.getCred(m.myVersion)
|
|
||||||
if cred == nil {
|
|
||||||
m.failed = true
|
|
||||||
return fmt.Errorf("%w: %v", ErrNoCredential, m.myVersion)
|
|
||||||
}
|
|
||||||
rc, err := cert.Recombine(
|
|
||||||
cert.Version(payload.CertVersion),
|
|
||||||
payload.Cert,
|
|
||||||
m.hs.PeerStatic(),
|
|
||||||
cred.Cert.Curve(),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
m.failed = true
|
|
||||||
return fmt.Errorf("recombine cert: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !bytes.Equal(rc.PublicKey(), m.hs.PeerStatic()) {
|
|
||||||
m.failed = true
|
|
||||||
return ErrPublicKeyMismatch
|
|
||||||
}
|
|
||||||
|
|
||||||
// Version negotiation, if the peer sent a different version and we have it, switch
|
|
||||||
if rc.Version() != m.myVersion {
|
|
||||||
if m.getCred(rc.Version()) != nil {
|
|
||||||
m.myVersion = rc.Version()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
verified, err := m.verifier(rc)
|
|
||||||
if err != nil {
|
|
||||||
m.failed = true
|
|
||||||
return fmt.Errorf("verify cert: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
m.result.RemoteCert = verified
|
|
||||||
m.remoteCertSet = true
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Machine) marshalOutgoing(flags msgFlags) ([]byte, error) {
|
|
||||||
if !flags.expectsPayload && !flags.expectsCert {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var p Payload
|
|
||||||
if flags.expectsPayload {
|
|
||||||
if !m.indexAllocated {
|
|
||||||
index, err := m.allocIndex()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%w: %w", ErrIndexAllocation, err)
|
|
||||||
}
|
|
||||||
m.result.LocalIndex = index
|
|
||||||
m.indexAllocated = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.result.Initiator {
|
|
||||||
p.InitiatorIndex = m.result.LocalIndex
|
|
||||||
} else {
|
|
||||||
p.ResponderIndex = m.result.LocalIndex
|
|
||||||
p.InitiatorIndex = m.result.RemoteIndex
|
|
||||||
}
|
|
||||||
p.Time = uint64(time.Now().UnixNano())
|
|
||||||
}
|
|
||||||
if flags.expectsCert {
|
|
||||||
cred := m.getCred(m.myVersion)
|
|
||||||
if cred == nil {
|
|
||||||
return nil, fmt.Errorf("%w: %v", ErrNoCredential, m.myVersion)
|
|
||||||
}
|
|
||||||
p.Cert = cred.Bytes
|
|
||||||
p.CertVersion = uint32(cred.Cert.Version())
|
|
||||||
m.result.MyCert = cred.Cert
|
|
||||||
}
|
|
||||||
|
|
||||||
return MarshalPayload(nil, p), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Machine) buildResponse(out []byte) ([]byte, *noise.CipherState, *noise.CipherState, error) {
|
|
||||||
flags := m.myMsgFlags()
|
|
||||||
hsBytes, err := m.marshalOutgoing(flags)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extend out by header.Len to make room for the header. slices.Grow is a
|
|
||||||
// no-op when the cap is already sufficient (the zero-copy case where the
|
|
||||||
// caller passed a pre-sized buffer). header.Encode overwrites the new
|
|
||||||
// bytes, so they don't need to be zeroed.
|
|
||||||
start := len(out)
|
|
||||||
out = slices.Grow(out, header.Len)[:start+header.Len]
|
|
||||||
header.Encode(
|
|
||||||
out[start:],
|
|
||||||
header.Version, header.Handshake, m.subtype,
|
|
||||||
m.result.RemoteIndex,
|
|
||||||
uint64(m.hs.MessageIndex()+1),
|
|
||||||
)
|
|
||||||
|
|
||||||
// noise.WriteMessage appends the encrypted handshake message to out,
|
|
||||||
// reusing capacity when present.
|
|
||||||
//
|
|
||||||
// The (dKey, eKey) ordering here is correct for IX, where the responder
|
|
||||||
// completes the handshake by writing the stage-2 message. noise returns
|
|
||||||
// (cs1, cs2) where cs1 is the initiator->responder cipher (which is the
|
|
||||||
// responder's decrypt key). For 3-message patterns where an initiator
|
|
||||||
// finishes by writing the final message, this ordering would be wrong;
|
|
||||||
// revisit when XX/pqIX lands.
|
|
||||||
out, dKey, eKey, err := m.hs.WriteMessage(out, hsBytes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, nil, fmt.Errorf("noise WriteMessage: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return out, dKey, eKey, nil
|
|
||||||
}
|
|
||||||
@@ -1,662 +0,0 @@
|
|||||||
package handshake
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/netip"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
|
||||||
"github.com/slackhq/nebula/cert"
|
|
||||||
ct "github.com/slackhq/nebula/cert_test"
|
|
||||||
"github.com/slackhq/nebula/header"
|
|
||||||
"github.com/slackhq/nebula/noiseutil"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMachineIXHappyPath(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca)
|
|
||||||
|
|
||||||
initCS := newTestCertState(t, ca, caKey, "initiator", []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")})
|
|
||||||
respCS := newTestCertState(t, ca, caKey, "responder", []netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")})
|
|
||||||
|
|
||||||
initR, respR := doFullHandshake(t, initCS, respCS, caPool)
|
|
||||||
|
|
||||||
assert.Equal(t, "responder", initR.RemoteCert.Certificate.Name())
|
|
||||||
assert.Equal(t, "initiator", respR.RemoteCert.Certificate.Name())
|
|
||||||
|
|
||||||
assert.Equal(t, uint32(1000), initR.LocalIndex)
|
|
||||||
assert.Equal(t, uint32(2000), initR.RemoteIndex)
|
|
||||||
assert.Equal(t, uint32(2000), respR.LocalIndex)
|
|
||||||
assert.Equal(t, uint32(1000), respR.RemoteIndex)
|
|
||||||
|
|
||||||
assert.Equal(t, uint64(2), initR.MessageIndex, "IX has 2 messages")
|
|
||||||
assert.Equal(t, uint64(2), respR.MessageIndex, "IX has 2 messages")
|
|
||||||
|
|
||||||
ct1, err := initR.EKey.Encrypt(nil, nil, []byte("hello"))
|
|
||||||
require.NoError(t, err)
|
|
||||||
pt1, err := respR.DKey.Decrypt(nil, nil, ct1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, []byte("hello"), pt1)
|
|
||||||
|
|
||||||
ct2, err := respR.EKey.Encrypt(nil, nil, []byte("world"))
|
|
||||||
require.NoError(t, err)
|
|
||||||
pt2, err := initR.DKey.Decrypt(nil, nil, ct2)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, []byte("world"), pt2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMachineInitiateErrors(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca)
|
|
||||||
cs := newTestCertState(t, ca, caKey, "test", []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")})
|
|
||||||
v := testVerifier(caPool)
|
|
||||||
|
|
||||||
t.Run("initiate on responder", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, false, 100)
|
|
||||||
_, err := m.Initiate(nil)
|
|
||||||
require.ErrorIs(t, err, ErrInitiateOnResponder)
|
|
||||||
assert.True(t, m.Failed())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("initiate called twice", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, true, 100)
|
|
||||||
_, err := m.Initiate(nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
_, err = m.Initiate(nil)
|
|
||||||
require.ErrorIs(t, err, ErrInitiateAlreadyCalled)
|
|
||||||
assert.True(t, m.Failed())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("process packet before initiate on initiator", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, true, 100)
|
|
||||||
_, _, err := m.ProcessPacket(nil, make([]byte, 100))
|
|
||||||
require.ErrorIs(t, err, ErrInitiateNotCalled)
|
|
||||||
assert.True(t, m.Failed())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("calling failed machine", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, false, 100)
|
|
||||||
_, err := m.Initiate(nil) // fails: responder
|
|
||||||
require.Error(t, err)
|
|
||||||
_, err = m.Initiate(nil) // fails: already failed
|
|
||||||
require.ErrorIs(t, err, ErrMachineFailed)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMachineProcessPacketErrors(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca)
|
|
||||||
cs := newTestCertState(t, ca, caKey, "test", []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")})
|
|
||||||
v := testVerifier(caPool)
|
|
||||||
|
|
||||||
t.Run("packet too short", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, false, 100)
|
|
||||||
_, _, err := m.ProcessPacket(nil, []byte{1, 2, 3})
|
|
||||||
require.ErrorIs(t, err, ErrPacketTooShort)
|
|
||||||
assert.False(t, m.Failed(), "short packet should not kill machine")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("noise decryption failure is recoverable", func(t *testing.T) {
|
|
||||||
initCS := newTestCertState(t, ca, caKey, "init", []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")})
|
|
||||||
initM := newTestMachine(t, initCS, v, true, 100)
|
|
||||||
msg1, err := initM.Initiate(nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
respM := newTestMachine(t, cs, v, false, 200)
|
|
||||||
resp, _, err := respM.ProcessPacket(nil, msg1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
corrupted := make([]byte, len(resp))
|
|
||||||
copy(corrupted, resp)
|
|
||||||
for i := header.Len; i < len(corrupted); i++ {
|
|
||||||
corrupted[i] ^= 0xff
|
|
||||||
}
|
|
||||||
_, _, err = initM.ProcessPacket(nil, corrupted)
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.False(t, initM.Failed(), "noise failure should be recoverable")
|
|
||||||
|
|
||||||
// And the machine should still complete a real handshake afterward.
|
|
||||||
_, result, err := initM.ProcessPacket(nil, resp)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, result, "initiator should complete on the legitimate response")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("invalid cert is fatal", func(t *testing.T) {
|
|
||||||
otherCA, _, otherCAKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
otherCS := newTestCertState(t, otherCA, otherCAKey, "other", []netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")})
|
|
||||||
|
|
||||||
initM := newTestMachine(t, otherCS, testVerifier(ct.NewTestCAPool(otherCA)), true, 100)
|
|
||||||
msg1, err := initM.Initiate(nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
respM := newTestMachine(t, cs, v, false, 200)
|
|
||||||
_, _, err = respM.ProcessPacket(nil, msg1)
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.True(t, respM.Failed(), "cert validation failure should kill machine")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("subtype mismatch is recoverable", func(t *testing.T) {
|
|
||||||
initCS := newTestCertState(t, ca, caKey, "init", []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")})
|
|
||||||
initM := newTestMachine(t, initCS, v, true, 100)
|
|
||||||
msg1, err := initM.Initiate(nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Mutate the subtype byte (offset 1 in the header) to a value the
|
|
||||||
// responder Machine wasn't built for.
|
|
||||||
bad := make([]byte, len(msg1))
|
|
||||||
copy(bad, msg1)
|
|
||||||
bad[1] = 0xff
|
|
||||||
|
|
||||||
respM := newTestMachine(t, cs, v, false, 200)
|
|
||||||
_, _, err = respM.ProcessPacket(nil, bad)
|
|
||||||
require.ErrorIs(t, err, ErrSubtypeMismatch)
|
|
||||||
assert.False(t, respM.Failed(), "subtype mismatch should not kill the machine")
|
|
||||||
|
|
||||||
// And the machine should still complete a real handshake afterward.
|
|
||||||
resp, result, err := respM.ProcessPacket(nil, msg1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, result, "responder should complete on the legitimate stage-1 packet")
|
|
||||||
assert.NotEmpty(t, resp, "responder should produce a stage-2 reply")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestMachineProcessPayload exercises processPayload's internal validation
|
|
||||||
// directly. Most of these failure modes can't be reached black-box once the
|
|
||||||
// subtype check at the top of ProcessPacket gates external callers, so we
|
|
||||||
// drive them by hand here for coverage.
|
|
||||||
func TestMachineProcessPayload(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca)
|
|
||||||
cs := newTestCertState(t, ca, caKey, "test", []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")})
|
|
||||||
v := testVerifier(caPool)
|
|
||||||
|
|
||||||
t.Run("empty message with expects fails", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, false, 100)
|
|
||||||
err := m.processPayload(nil, msgFlags{expectsPayload: true, expectsCert: true})
|
|
||||||
require.ErrorIs(t, err, ErrMissingContent)
|
|
||||||
assert.True(t, m.Failed())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("empty message with no expects passes", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, false, 100)
|
|
||||||
err := m.processPayload(nil, msgFlags{})
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.False(t, m.Failed())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("malformed protobuf is fatal", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, false, 100)
|
|
||||||
err := m.processPayload([]byte{0xff, 0xff, 0xff}, msgFlags{expectsPayload: true, expectsCert: true})
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.True(t, m.Failed())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("unexpected payload data is fatal", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, false, 100)
|
|
||||||
// A payload with index data when none was expected.
|
|
||||||
bytes := MarshalPayload(nil, Payload{InitiatorIndex: 42, Time: 1})
|
|
||||||
err := m.processPayload(bytes, msgFlags{expectsPayload: false, expectsCert: false})
|
|
||||||
require.ErrorIs(t, err, ErrUnexpectedContent)
|
|
||||||
assert.True(t, m.Failed())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("unexpected cert data is fatal", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, false, 100)
|
|
||||||
// A payload with cert when none was expected.
|
|
||||||
bytes := MarshalPayload(nil, Payload{Cert: []byte{1, 2, 3}, CertVersion: 2})
|
|
||||||
err := m.processPayload(bytes, msgFlags{expectsPayload: false, expectsCert: false})
|
|
||||||
require.ErrorIs(t, err, ErrUnexpectedContent)
|
|
||||||
assert.True(t, m.Failed())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("missing payload data when expected is fatal", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, false, 100)
|
|
||||||
// Cert present, but no index/time fields.
|
|
||||||
bytes := MarshalPayload(nil, Payload{Cert: []byte{1, 2, 3}, CertVersion: 2})
|
|
||||||
err := m.processPayload(bytes, msgFlags{expectsPayload: true, expectsCert: true})
|
|
||||||
require.ErrorIs(t, err, ErrUnexpectedContent)
|
|
||||||
assert.True(t, m.Failed())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestMachineRequireComplete checks the fail-on-incomplete-handshake path
|
|
||||||
// directly. Like processPayload above this isn't reachable from a normal IX
|
|
||||||
// flow, so we drive it by hand.
|
|
||||||
func TestMachineRequireComplete(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca)
|
|
||||||
cs := newTestCertState(t, ca, caKey, "test", []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")})
|
|
||||||
v := testVerifier(caPool)
|
|
||||||
|
|
||||||
t.Run("missing both fails", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, false, 100)
|
|
||||||
err := m.requireComplete()
|
|
||||||
require.ErrorIs(t, err, ErrIncompleteHandshake)
|
|
||||||
assert.True(t, m.Failed())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("payload only fails", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, false, 100)
|
|
||||||
m.payloadSet = true
|
|
||||||
err := m.requireComplete()
|
|
||||||
require.ErrorIs(t, err, ErrIncompleteHandshake)
|
|
||||||
assert.True(t, m.Failed())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("cert only fails", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, false, 100)
|
|
||||||
m.remoteCertSet = true
|
|
||||||
err := m.requireComplete()
|
|
||||||
require.ErrorIs(t, err, ErrIncompleteHandshake)
|
|
||||||
assert.True(t, m.Failed())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("both set passes", func(t *testing.T) {
|
|
||||||
m := newTestMachine(t, cs, v, false, 100)
|
|
||||||
m.payloadSet = true
|
|
||||||
m.remoteCertSet = true
|
|
||||||
err := m.requireComplete()
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.False(t, m.Failed())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMachineAESCipher(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca)
|
|
||||||
|
|
||||||
initCS := newTestCertStateWithCipher(
|
|
||||||
t, ca, caKey, "init",
|
|
||||||
[]netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")},
|
|
||||||
noiseutil.CipherAESGCM,
|
|
||||||
)
|
|
||||||
respCS := newTestCertStateWithCipher(
|
|
||||||
t, ca, caKey, "resp",
|
|
||||||
[]netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")},
|
|
||||||
noiseutil.CipherAESGCM,
|
|
||||||
)
|
|
||||||
|
|
||||||
initR, respR := doFullHandshake(t, initCS, respCS, caPool)
|
|
||||||
|
|
||||||
ct1, err := initR.EKey.Encrypt(nil, nil, []byte("works"))
|
|
||||||
require.NoError(t, err)
|
|
||||||
pt1, err := respR.DKey.Decrypt(nil, nil, ct1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, []byte("works"), pt1)
|
|
||||||
|
|
||||||
ct2, err := respR.EKey.Encrypt(nil, nil, []byte("back"))
|
|
||||||
require.NoError(t, err)
|
|
||||||
pt2, err := initR.DKey.Decrypt(nil, nil, ct2)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, []byte("back"), pt2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestResultFields(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca)
|
|
||||||
initCS := newTestCertState(t, ca, caKey, "init", []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")})
|
|
||||||
respCS := newTestCertState(t, ca, caKey, "resp", []netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")})
|
|
||||||
|
|
||||||
initR, respR := doFullHandshake(t, initCS, respCS, caPool)
|
|
||||||
|
|
||||||
assert.True(t, initR.Initiator)
|
|
||||||
assert.False(t, respR.Initiator)
|
|
||||||
assert.NotZero(t, initR.HandshakeTime)
|
|
||||||
assert.NotZero(t, respR.HandshakeTime)
|
|
||||||
assert.NotNil(t, initR.RemoteCert)
|
|
||||||
assert.NotNil(t, respR.RemoteCert)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMachineBufferReuse(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca)
|
|
||||||
initCS := newTestCertState(t, ca, caKey, "init", []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")})
|
|
||||||
respCS := newTestCertState(t, ca, caKey, "resp", []netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")})
|
|
||||||
v := testVerifier(caPool)
|
|
||||||
|
|
||||||
initM := newTestMachine(t, initCS, v, true, 1000)
|
|
||||||
respM := newTestMachine(t, respCS, v, false, 2000)
|
|
||||||
|
|
||||||
msg1, err := initM.Initiate(nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
t.Run("response writes into provided buffer", func(t *testing.T) {
|
|
||||||
buf := make([]byte, 0, 4096)
|
|
||||||
resp, result, err := respM.ProcessPacket(buf, msg1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, result)
|
|
||||||
|
|
||||||
assert.NotEmpty(t, resp, "response should have content")
|
|
||||||
assert.Equal(t, &buf[:1][0], &resp[:1][0],
|
|
||||||
"response should reuse the provided buffer's backing array")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("initiate writes into provided buffer", func(t *testing.T) {
|
|
||||||
initM2 := newTestMachine(t, initCS, v, true, 3000)
|
|
||||||
buf := make([]byte, 0, 4096)
|
|
||||||
msg, err := initM2.Initiate(buf)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
assert.NotEmpty(t, msg, "initiate should have content")
|
|
||||||
assert.Equal(t, &buf[:1][0], &msg[:1][0],
|
|
||||||
"initiate should reuse the provided buffer's backing array")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("nil out still works", func(t *testing.T) {
|
|
||||||
initM2 := newTestMachine(t, initCS, v, true, 4000)
|
|
||||||
respM2 := newTestMachine(t, respCS, v, false, 5000)
|
|
||||||
|
|
||||||
msg1, err := initM2.Initiate(nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
resp, _, err := respM2.ProcessPacket(nil, msg1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
out, result, err := initM2.ProcessPacket(nil, resp)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.NotNil(t, result)
|
|
||||||
assert.Nil(t, out, "initiator should have no response for IX msg2")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMachineMsgIndexTracking(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca)
|
|
||||||
initCS := newTestCertState(t, ca, caKey, "init", []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")})
|
|
||||||
respCS := newTestCertState(t, ca, caKey, "resp", []netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")})
|
|
||||||
v := testVerifier(caPool)
|
|
||||||
|
|
||||||
initM := newTestMachine(t, initCS, v, true, 100)
|
|
||||||
respM := newTestMachine(t, respCS, v, false, 200)
|
|
||||||
|
|
||||||
msg1, err := initM.Initiate(nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
resp1, result1, err := respM.ProcessPacket(nil, msg1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.NotNil(t, result1)
|
|
||||||
|
|
||||||
_, result2, err := initM.ProcessPacket(nil, resp1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.NotNil(t, result2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMachineThreeMessagePattern(t *testing.T) {
|
|
||||||
registerTestXXInfo(t)
|
|
||||||
|
|
||||||
// Use HandshakeXX (3 messages) to verify the Machine handles multi-message
|
|
||||||
// patterns correctly. XX flow:
|
|
||||||
// msg1 (I->R): [E] - payload only, no cert
|
|
||||||
// msg2 (R->I): [E, ee, S, es] - payload + cert
|
|
||||||
// msg3 (I->R): [S, se] - cert only (no payload, not first two)
|
|
||||||
|
|
||||||
ca, _, caKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca)
|
|
||||||
v := testVerifier(caPool)
|
|
||||||
|
|
||||||
initCS := newTestCertState(t, ca, caKey, "init", []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")})
|
|
||||||
respCS := newTestCertState(t, ca, caKey, "resp", []netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")})
|
|
||||||
|
|
||||||
initM, err := NewMachine(
|
|
||||||
cert.Version2,
|
|
||||||
initCS.getCredential, v,
|
|
||||||
func() (uint32, error) { return 1000, nil },
|
|
||||||
true, header.HandshakeXXPSK0,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
respM, err := NewMachine(
|
|
||||||
cert.Version2,
|
|
||||||
respCS.getCredential, v,
|
|
||||||
func() (uint32, error) { return 2000, nil },
|
|
||||||
false, header.HandshakeXXPSK0,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// msg1: initiator -> responder (E only, no cert)
|
|
||||||
msg1, err := initM.Initiate(nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.NotEmpty(t, msg1)
|
|
||||||
|
|
||||||
// Responder processes msg1, should not complete yet, should produce msg2
|
|
||||||
msg2, result, err := respM.ProcessPacket(nil, msg1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Nil(t, result, "XX should not complete on msg1")
|
|
||||||
assert.NotEmpty(t, msg2, "responder should produce msg2")
|
|
||||||
|
|
||||||
// Initiator processes msg2: gets responder's cert, produces msg3, and
|
|
||||||
// completes (WriteMessage for msg3 derives keys)
|
|
||||||
msg3, initResult, err := initM.ProcessPacket(nil, msg2)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, initResult, "XX initiator should complete after reading msg2 and writing msg3")
|
|
||||||
assert.NotEmpty(t, msg3, "initiator should produce msg3")
|
|
||||||
assert.Equal(t, "resp", initResult.RemoteCert.Certificate.Name())
|
|
||||||
|
|
||||||
// Responder processes msg3: gets initiator's cert and completes
|
|
||||||
_, respResult, err := respM.ProcessPacket(nil, msg3)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, respResult, "XX responder should complete on msg3")
|
|
||||||
assert.Equal(t, "init", respResult.RemoteCert.Certificate.Name())
|
|
||||||
|
|
||||||
assert.Equal(t, uint64(3), initResult.MessageIndex, "XX has 3 messages")
|
|
||||||
assert.Equal(t, uint64(3), respResult.MessageIndex, "XX has 3 messages")
|
|
||||||
|
|
||||||
// Verify keys work
|
|
||||||
ct1, err := initResult.EKey.Encrypt(nil, nil, []byte("three messages"))
|
|
||||||
require.NoError(t, err)
|
|
||||||
pt1, err := respResult.DKey.Decrypt(nil, nil, ct1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, []byte("three messages"), pt1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE: ErrIncompleteHandshake is tested implicitly. It can't be triggered with
|
|
||||||
// IX since the cert is always in the payload. A 3-message pattern test (HybridIX)
|
|
||||||
// should exercise the case where cert arrives in msg3 and verify that completing
|
|
||||||
// without it fails.
|
|
||||||
|
|
||||||
func TestMachineExpiredCert(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519,
|
|
||||||
time.Now().Add(-24*time.Hour), time.Now().Add(24*time.Hour),
|
|
||||||
nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca)
|
|
||||||
|
|
||||||
expCert, _, expKeyPEM, _ := ct.NewTestCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, ca, caKey,
|
|
||||||
"expired", time.Now().Add(-2*time.Hour), time.Now().Add(-1*time.Hour),
|
|
||||||
[]netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")}, nil, nil,
|
|
||||||
)
|
|
||||||
expKey, _, _, err := cert.UnmarshalPrivateKeyFromPEM(expKeyPEM)
|
|
||||||
require.NoError(t, err)
|
|
||||||
expHsBytes, err := expCert.MarshalForHandshakes()
|
|
||||||
require.NoError(t, err)
|
|
||||||
ncs := noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashSHA256)
|
|
||||||
|
|
||||||
expiredCS := &testCertState{
|
|
||||||
version: cert.Version2,
|
|
||||||
creds: map[cert.Version]*Credential{
|
|
||||||
cert.Version2: NewCredential(expCert, expHsBytes, expKey, ncs),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
respCS := newTestCertState(
|
|
||||||
t, ca, caKey, "responder",
|
|
||||||
[]netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")},
|
|
||||||
)
|
|
||||||
|
|
||||||
_, respM, _, _, err := initiateHandshake(
|
|
||||||
t, expiredCS, testVerifier(caPool),
|
|
||||||
respCS, testVerifier(caPool),
|
|
||||||
)
|
|
||||||
require.ErrorContains(t, err, "verify cert")
|
|
||||||
assert.True(t, respM.Failed())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMachineNoCertNetworks(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca)
|
|
||||||
|
|
||||||
caHsBytes, err := ca.MarshalForHandshakes()
|
|
||||||
require.NoError(t, err)
|
|
||||||
ncs := noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashSHA256)
|
|
||||||
|
|
||||||
noNetCS := &testCertState{
|
|
||||||
version: cert.Version2,
|
|
||||||
creds: map[cert.Version]*Credential{
|
|
||||||
cert.Version2: NewCredential(ca, caHsBytes, caKey, ncs),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
respCS := newTestCertState(
|
|
||||||
t, ca, caKey, "responder",
|
|
||||||
[]netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")},
|
|
||||||
)
|
|
||||||
|
|
||||||
_, respM, _, _, err := initiateHandshake(
|
|
||||||
t, noNetCS, testVerifier(caPool),
|
|
||||||
respCS, testVerifier(caPool),
|
|
||||||
)
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.True(t, respM.Failed())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMachineDifferentCAs(t *testing.T) {
|
|
||||||
ca1, _, caKey1, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
ca2, _, caKey2, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
|
|
||||||
initCS := newTestCertState(
|
|
||||||
t, ca1, caKey1, "init",
|
|
||||||
[]netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")},
|
|
||||||
)
|
|
||||||
respCS := newTestCertState(
|
|
||||||
t, ca2, caKey2, "resp",
|
|
||||||
[]netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")},
|
|
||||||
)
|
|
||||||
|
|
||||||
_, respM, _, _, err := initiateHandshake(
|
|
||||||
t, initCS, testVerifier(ct.NewTestCAPool(ca1)),
|
|
||||||
respCS, testVerifier(ct.NewTestCAPool(ca2)),
|
|
||||||
)
|
|
||||||
require.ErrorContains(t, err, "verify cert")
|
|
||||||
assert.True(t, respM.Failed())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMachineVersionNegotiation(t *testing.T) {
|
|
||||||
ca1, _, caKey1, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version1, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
ca2, _, caKey2, _ := ct.NewTestCaCert(
|
|
||||||
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil,
|
|
||||||
)
|
|
||||||
caPool := ct.NewTestCAPool(ca1, ca2)
|
|
||||||
|
|
||||||
makeMultiVersionResp := func(t *testing.T) *testCertState {
|
|
||||||
t.Helper()
|
|
||||||
respCertV1, _, respKeyPEM, _ := ct.NewTestCert(
|
|
||||||
cert.Version1, cert.Curve_CURVE25519, ca1, caKey1, "resp",
|
|
||||||
ca1.NotBefore(), ca1.NotAfter(),
|
|
||||||
[]netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")}, nil, nil,
|
|
||||||
)
|
|
||||||
respKey, _, _, _ := cert.UnmarshalPrivateKeyFromPEM(respKeyPEM)
|
|
||||||
respCertV2, _ := ct.NewTestCertDifferentVersion(respCertV1, cert.Version2, ca2, caKey2)
|
|
||||||
respHsV1, _ := respCertV1.MarshalForHandshakes()
|
|
||||||
respHsV2, _ := respCertV2.MarshalForHandshakes()
|
|
||||||
ncs := noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashSHA256)
|
|
||||||
return &testCertState{
|
|
||||||
version: cert.Version1,
|
|
||||||
creds: map[cert.Version]*Credential{
|
|
||||||
cert.Version1: NewCredential(respCertV1, respHsV1, respKey, ncs),
|
|
||||||
cert.Version2: NewCredential(respCertV2, respHsV2, respKey, ncs),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Run("responder matches initiator version", func(t *testing.T) {
|
|
||||||
initCS := newTestCertState(
|
|
||||||
t, ca2, caKey2, "init",
|
|
||||||
[]netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")},
|
|
||||||
)
|
|
||||||
respCS := makeMultiVersionResp(t)
|
|
||||||
v := testVerifier(caPool)
|
|
||||||
|
|
||||||
initM, _, respResult, resp, err := initiateHandshake(
|
|
||||||
t, initCS, v,
|
|
||||||
respCS, v,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, respResult)
|
|
||||||
|
|
||||||
assert.Equal(t, cert.Version2, respResult.MyCert.Version(),
|
|
||||||
"responder should negotiate to initiator's version")
|
|
||||||
|
|
||||||
_, initResult, err := initM.ProcessPacket(nil, resp)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, initResult)
|
|
||||||
assert.Equal(t, cert.Version2, initResult.RemoteCert.Certificate.Version(),
|
|
||||||
"initiator should see V2 cert from responder")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("responder keeps version when no match available", func(t *testing.T) {
|
|
||||||
initCS := newTestCertState(
|
|
||||||
t, ca2, caKey2, "init",
|
|
||||||
[]netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")},
|
|
||||||
)
|
|
||||||
|
|
||||||
respCert, _, respKeyPEM, _ := ct.NewTestCert(
|
|
||||||
cert.Version1, cert.Curve_CURVE25519, ca1, caKey1, "resp",
|
|
||||||
ca1.NotBefore(), ca1.NotAfter(),
|
|
||||||
[]netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")}, nil, nil,
|
|
||||||
)
|
|
||||||
respKey, _, _, _ := cert.UnmarshalPrivateKeyFromPEM(respKeyPEM)
|
|
||||||
respHs, _ := respCert.MarshalForHandshakes()
|
|
||||||
ncs := noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashSHA256)
|
|
||||||
respCS := &testCertState{
|
|
||||||
version: cert.Version1,
|
|
||||||
creds: map[cert.Version]*Credential{
|
|
||||||
cert.Version1: NewCredential(respCert, respHs, respKey, ncs),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
v := testVerifier(caPool)
|
|
||||||
_, _, respResult, _, err := initiateHandshake(
|
|
||||||
t, initCS, v,
|
|
||||||
respCS, v,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, respResult)
|
|
||||||
|
|
||||||
assert.Equal(t, cert.Version1, respResult.MyCert.Version(),
|
|
||||||
"responder should keep V1 when V2 not available")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
package handshake
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
|
||||||
"github.com/slackhq/nebula/header"
|
|
||||||
)
|
|
||||||
|
|
||||||
// msgFlags tracks what application data a handshake message carries.
|
|
||||||
type msgFlags struct {
|
|
||||||
expectsPayload bool // message carries indexes and time
|
|
||||||
expectsCert bool // message carries the certificate
|
|
||||||
}
|
|
||||||
|
|
||||||
// subtypeInfo bundles the noise pattern with the per-message flags for a
|
|
||||||
// given handshake subtype.
|
|
||||||
type subtypeInfo struct {
|
|
||||||
pattern noise.HandshakePattern
|
|
||||||
msgs []msgFlags
|
|
||||||
}
|
|
||||||
|
|
||||||
// subtypeInfos defines the noise pattern and message content layout for each
|
|
||||||
// handshake subtype.
|
|
||||||
var subtypeInfos = map[header.MessageSubType]subtypeInfo{
|
|
||||||
// IX: 2 messages, both carry payload and cert
|
|
||||||
header.HandshakeIXPSK0: {
|
|
||||||
pattern: noise.HandshakeIX,
|
|
||||||
msgs: []msgFlags{
|
|
||||||
{expectsPayload: true, expectsCert: true},
|
|
||||||
{expectsPayload: true, expectsCert: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// XX: 3 messages
|
|
||||||
// msg1 (I->R): payload only
|
|
||||||
// msg2 (R->I): payload + cert
|
|
||||||
// msg3 (I->R): cert only
|
|
||||||
//header.HandshakeXXPSK0: {
|
|
||||||
// pattern: noise.HandshakeXX,
|
|
||||||
// msgs: []msgFlags{
|
|
||||||
// {expectsPayload: true, expectsCert: false},
|
|
||||||
// {expectsPayload: true, expectsCert: true},
|
|
||||||
// {expectsPayload: false, expectsCert: true},
|
|
||||||
// },
|
|
||||||
//},
|
|
||||||
}
|
|
||||||
|
|
||||||
func subtypeInfoFor(subtype header.MessageSubType) (subtypeInfo, error) {
|
|
||||||
if info, ok := subtypeInfos[subtype]; ok {
|
|
||||||
return info, nil
|
|
||||||
}
|
|
||||||
return subtypeInfo{}, fmt.Errorf("%w: %d", ErrUnknownSubtype, subtype)
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
package handshake
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
|
||||||
"github.com/slackhq/nebula/header"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSubtypeInfo(t *testing.T) {
|
|
||||||
t.Run("IX", func(t *testing.T) {
|
|
||||||
info, err := subtypeInfoFor(header.HandshakeIXPSK0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, noise.HandshakeIX.Name, info.pattern.Name)
|
|
||||||
require.Len(t, info.msgs, 2)
|
|
||||||
// msg1: payload + cert
|
|
||||||
assert.True(t, info.msgs[0].expectsPayload)
|
|
||||||
assert.True(t, info.msgs[0].expectsCert)
|
|
||||||
// msg2: payload + cert
|
|
||||||
assert.True(t, info.msgs[1].expectsPayload)
|
|
||||||
assert.True(t, info.msgs[1].expectsCert)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("XX", func(t *testing.T) {
|
|
||||||
registerTestXXInfo(t)
|
|
||||||
info, err := subtypeInfoFor(header.HandshakeXXPSK0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, noise.HandshakeXX.Name, info.pattern.Name)
|
|
||||||
require.Len(t, info.msgs, 3)
|
|
||||||
// msg1: payload only
|
|
||||||
assert.True(t, info.msgs[0].expectsPayload)
|
|
||||||
assert.False(t, info.msgs[0].expectsCert)
|
|
||||||
// msg2: payload + cert
|
|
||||||
assert.True(t, info.msgs[1].expectsPayload)
|
|
||||||
assert.True(t, info.msgs[1].expectsCert)
|
|
||||||
// msg3: cert only
|
|
||||||
assert.False(t, info.msgs[2].expectsPayload)
|
|
||||||
assert.True(t, info.msgs[2].expectsCert)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("unknown subtype returns error", func(t *testing.T) {
|
|
||||||
_, err := subtypeInfoFor(99)
|
|
||||||
require.ErrorIs(t, err, ErrUnknownSubtype)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// registerTestXXInfo temporarily registers XX subtype info for testing.
|
|
||||||
func registerTestXXInfo(t *testing.T) {
|
|
||||||
t.Helper()
|
|
||||||
subtypeInfos[header.HandshakeXXPSK0] = subtypeInfo{
|
|
||||||
pattern: noise.HandshakeXX,
|
|
||||||
msgs: []msgFlags{
|
|
||||||
{expectsPayload: true, expectsCert: false},
|
|
||||||
{expectsPayload: true, expectsCert: true},
|
|
||||||
{expectsPayload: false, expectsCert: true},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
delete(subtypeInfos, header.HandshakeXXPSK0)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,173 +0,0 @@
|
|||||||
package handshake
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"math"
|
|
||||||
|
|
||||||
"google.golang.org/protobuf/encoding/protowire"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
errInvalidHandshakeMessage = errors.New("invalid handshake message")
|
|
||||||
errInvalidHandshakeDetails = errors.New("invalid handshake details")
|
|
||||||
)
|
|
||||||
|
|
||||||
// Payload represents the decoded fields of a handshake message.
|
|
||||||
// Wire format is protobuf-compatible with NebulaHandshake{Details: NebulaHandshakeDetails{...}}.
|
|
||||||
type Payload struct {
|
|
||||||
Cert []byte
|
|
||||||
InitiatorIndex uint32
|
|
||||||
ResponderIndex uint32
|
|
||||||
Time uint64
|
|
||||||
CertVersion uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
// Proto field numbers for NebulaHandshakeDetails
|
|
||||||
const (
|
|
||||||
fieldCert = 1 // bytes
|
|
||||||
fieldInitiatorIndex = 2 // uint32
|
|
||||||
fieldResponderIndex = 3 // uint32
|
|
||||||
fieldTime = 5 // uint64
|
|
||||||
fieldCertVersion = 8 // uint32
|
|
||||||
)
|
|
||||||
|
|
||||||
// MarshalPayload encodes a handshake payload in protobuf wire format compatible
|
|
||||||
// with NebulaHandshake{Details: NebulaHandshakeDetails{...}}.
|
|
||||||
// Returns out (which may be nil), with the marshalled Payload appended to it.
|
|
||||||
func MarshalPayload(out []byte, p Payload) []byte {
|
|
||||||
var details []byte
|
|
||||||
|
|
||||||
if len(p.Cert) > 0 {
|
|
||||||
details = protowire.AppendTag(details, fieldCert, protowire.BytesType)
|
|
||||||
details = protowire.AppendBytes(details, p.Cert)
|
|
||||||
}
|
|
||||||
if p.InitiatorIndex != 0 {
|
|
||||||
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, uint64(p.InitiatorIndex))
|
|
||||||
}
|
|
||||||
if p.ResponderIndex != 0 {
|
|
||||||
details = protowire.AppendTag(details, fieldResponderIndex, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, uint64(p.ResponderIndex))
|
|
||||||
}
|
|
||||||
if p.Time != 0 {
|
|
||||||
details = protowire.AppendTag(details, fieldTime, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, p.Time)
|
|
||||||
}
|
|
||||||
if p.CertVersion != 0 {
|
|
||||||
details = protowire.AppendTag(details, fieldCertVersion, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, uint64(p.CertVersion))
|
|
||||||
}
|
|
||||||
|
|
||||||
out = protowire.AppendTag(out, 1, protowire.BytesType)
|
|
||||||
out = protowire.AppendBytes(out, details)
|
|
||||||
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalPayload decodes a protobuf-encoded NebulaHandshake message.
|
|
||||||
func UnmarshalPayload(b []byte) (Payload, error) {
|
|
||||||
var p Payload
|
|
||||||
|
|
||||||
for len(b) > 0 {
|
|
||||||
num, typ, n := protowire.ConsumeTag(b)
|
|
||||||
if n < 0 {
|
|
||||||
return p, errInvalidHandshakeMessage
|
|
||||||
}
|
|
||||||
b = b[n:]
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case num == 1 && typ == protowire.BytesType:
|
|
||||||
details, n := protowire.ConsumeBytes(b)
|
|
||||||
if n < 0 {
|
|
||||||
return p, errInvalidHandshakeMessage
|
|
||||||
}
|
|
||||||
b = b[n:]
|
|
||||||
if err := unmarshalPayloadDetails(&p, details); err != nil {
|
|
||||||
return p, err
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
n := protowire.ConsumeFieldValue(num, typ, b)
|
|
||||||
if n < 0 {
|
|
||||||
return p, errInvalidHandshakeMessage
|
|
||||||
}
|
|
||||||
b = b[n:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return p, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func unmarshalPayloadDetails(p *Payload, b []byte) error {
|
|
||||||
for len(b) > 0 {
|
|
||||||
num, typ, n := protowire.ConsumeTag(b)
|
|
||||||
if n < 0 {
|
|
||||||
return errInvalidHandshakeDetails
|
|
||||||
}
|
|
||||||
b = b[n:]
|
|
||||||
|
|
||||||
// For known field numbers, reject any non-matching wire type as a
|
|
||||||
// hard error rather than silently skipping. The caller will catch
|
|
||||||
// missing-field cases downstream, but a wire-type mismatch on a tag
|
|
||||||
// we know is a peer protocol violation worth flagging here.
|
|
||||||
// Repeated occurrences of a singular field follow proto3 last-wins.
|
|
||||||
switch num {
|
|
||||||
case fieldCert:
|
|
||||||
if typ != protowire.BytesType {
|
|
||||||
return errInvalidHandshakeDetails
|
|
||||||
}
|
|
||||||
v, n := protowire.ConsumeBytes(b)
|
|
||||||
if n < 0 {
|
|
||||||
return errInvalidHandshakeDetails
|
|
||||||
}
|
|
||||||
p.Cert = append([]byte(nil), v...)
|
|
||||||
b = b[n:]
|
|
||||||
case fieldInitiatorIndex:
|
|
||||||
if typ != protowire.VarintType {
|
|
||||||
return errInvalidHandshakeDetails
|
|
||||||
}
|
|
||||||
v, n := protowire.ConsumeVarint(b)
|
|
||||||
if n < 0 || v > math.MaxUint32 {
|
|
||||||
return errInvalidHandshakeDetails
|
|
||||||
}
|
|
||||||
p.InitiatorIndex = uint32(v)
|
|
||||||
b = b[n:]
|
|
||||||
case fieldResponderIndex:
|
|
||||||
if typ != protowire.VarintType {
|
|
||||||
return errInvalidHandshakeDetails
|
|
||||||
}
|
|
||||||
v, n := protowire.ConsumeVarint(b)
|
|
||||||
if n < 0 || v > math.MaxUint32 {
|
|
||||||
return errInvalidHandshakeDetails
|
|
||||||
}
|
|
||||||
p.ResponderIndex = uint32(v)
|
|
||||||
b = b[n:]
|
|
||||||
case fieldTime:
|
|
||||||
if typ != protowire.VarintType {
|
|
||||||
return errInvalidHandshakeDetails
|
|
||||||
}
|
|
||||||
v, n := protowire.ConsumeVarint(b)
|
|
||||||
if n < 0 {
|
|
||||||
return errInvalidHandshakeDetails
|
|
||||||
}
|
|
||||||
p.Time = v
|
|
||||||
b = b[n:]
|
|
||||||
case fieldCertVersion:
|
|
||||||
if typ != protowire.VarintType {
|
|
||||||
return errInvalidHandshakeDetails
|
|
||||||
}
|
|
||||||
v, n := protowire.ConsumeVarint(b)
|
|
||||||
if n < 0 || v > math.MaxUint32 {
|
|
||||||
return errInvalidHandshakeDetails
|
|
||||||
}
|
|
||||||
p.CertVersion = uint32(v)
|
|
||||||
b = b[n:]
|
|
||||||
default:
|
|
||||||
n := protowire.ConsumeFieldValue(num, typ, b)
|
|
||||||
if n < 0 {
|
|
||||||
return errInvalidHandshakeDetails
|
|
||||||
}
|
|
||||||
b = b[n:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,361 +0,0 @@
|
|||||||
package handshake
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"math"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"google.golang.org/protobuf/encoding/protowire"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPayloadRoundTrip(t *testing.T) {
|
|
||||||
t.Run("all fields set", func(t *testing.T) {
|
|
||||||
data := MarshalPayload(nil, Payload{
|
|
||||||
Cert: []byte("test-cert-bytes"),
|
|
||||||
CertVersion: 2,
|
|
||||||
InitiatorIndex: 12345,
|
|
||||||
ResponderIndex: 67890,
|
|
||||||
Time: 1234567890,
|
|
||||||
})
|
|
||||||
|
|
||||||
got, err := UnmarshalPayload(data)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
assert.Equal(t, []byte("test-cert-bytes"), got.Cert)
|
|
||||||
assert.Equal(t, uint32(12345), got.InitiatorIndex)
|
|
||||||
assert.Equal(t, uint32(67890), got.ResponderIndex)
|
|
||||||
assert.Equal(t, uint64(1234567890), got.Time)
|
|
||||||
assert.Equal(t, uint32(2), got.CertVersion)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("minimal fields", func(t *testing.T) {
|
|
||||||
data := MarshalPayload(nil, Payload{InitiatorIndex: 1})
|
|
||||||
|
|
||||||
got, err := UnmarshalPayload(data)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
assert.Equal(t, uint32(1), got.InitiatorIndex)
|
|
||||||
assert.Equal(t, uint32(0), got.ResponderIndex)
|
|
||||||
assert.Equal(t, uint64(0), got.Time)
|
|
||||||
assert.Nil(t, got.Cert)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("empty payload", func(t *testing.T) {
|
|
||||||
data := MarshalPayload(nil, Payload{})
|
|
||||||
|
|
||||||
got, err := UnmarshalPayload(data)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
assert.Equal(t, uint32(0), got.InitiatorIndex)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("large cert bytes", func(t *testing.T) {
|
|
||||||
bigCert := make([]byte, 4096)
|
|
||||||
for i := range bigCert {
|
|
||||||
bigCert[i] = byte(i % 256)
|
|
||||||
}
|
|
||||||
|
|
||||||
data := MarshalPayload(nil, Payload{
|
|
||||||
Cert: bigCert,
|
|
||||||
CertVersion: 2,
|
|
||||||
InitiatorIndex: 999,
|
|
||||||
})
|
|
||||||
|
|
||||||
got, err := UnmarshalPayload(data)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
assert.Equal(t, bigCert, got.Cert)
|
|
||||||
assert.Equal(t, uint32(999), got.InitiatorIndex)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("append to existing buffer", func(t *testing.T) {
|
|
||||||
prefix := []byte("prefix")
|
|
||||||
data := MarshalPayload(prefix, Payload{InitiatorIndex: 42})
|
|
||||||
|
|
||||||
assert.Equal(t, []byte("prefix"), data[:6])
|
|
||||||
|
|
||||||
got, err := UnmarshalPayload(data[6:])
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, uint32(42), got.InitiatorIndex)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPayloadUnknownFields(t *testing.T) {
|
|
||||||
t.Run("unknown field in outer message is skipped", func(t *testing.T) {
|
|
||||||
// Marshal a normal payload then append an unknown field (field 99, varint)
|
|
||||||
data := MarshalPayload(nil, Payload{InitiatorIndex: 42})
|
|
||||||
data = protowire.AppendTag(data, 99, protowire.VarintType)
|
|
||||||
data = protowire.AppendVarint(data, 12345)
|
|
||||||
|
|
||||||
got, err := UnmarshalPayload(data)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, uint32(42), got.InitiatorIndex)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("unknown field in details is skipped", func(t *testing.T) {
|
|
||||||
// Build details with a known field + unknown field
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, 77)
|
|
||||||
// Unknown field 50, varint
|
|
||||||
details = protowire.AppendTag(details, 50, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, 9999)
|
|
||||||
// Another known field after the unknown one
|
|
||||||
details = protowire.AppendTag(details, fieldResponderIndex, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, 88)
|
|
||||||
|
|
||||||
// Wrap in outer message
|
|
||||||
var data []byte
|
|
||||||
data = protowire.AppendTag(data, 1, protowire.BytesType)
|
|
||||||
data = protowire.AppendBytes(data, details)
|
|
||||||
|
|
||||||
got, err := UnmarshalPayload(data)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, uint32(77), got.InitiatorIndex)
|
|
||||||
assert.Equal(t, uint32(88), got.ResponderIndex)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("reserved fields 6 and 7 are skipped", func(t *testing.T) {
|
|
||||||
// Fields 6 and 7 are reserved in the proto definition
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, 100)
|
|
||||||
details = protowire.AppendTag(details, 6, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, 1)
|
|
||||||
details = protowire.AppendTag(details, 7, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, 2)
|
|
||||||
|
|
||||||
var data []byte
|
|
||||||
data = protowire.AppendTag(data, 1, protowire.BytesType)
|
|
||||||
data = protowire.AppendBytes(data, details)
|
|
||||||
|
|
||||||
got, err := UnmarshalPayload(data)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, uint32(100), got.InitiatorIndex)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPayloadBytesConsumed(t *testing.T) {
|
|
||||||
t.Run("all bytes consumed on valid input", func(t *testing.T) {
|
|
||||||
original := Payload{
|
|
||||||
Cert: []byte("cert"),
|
|
||||||
CertVersion: 2,
|
|
||||||
InitiatorIndex: 100,
|
|
||||||
ResponderIndex: 200,
|
|
||||||
Time: 999,
|
|
||||||
}
|
|
||||||
data := MarshalPayload(nil, original)
|
|
||||||
|
|
||||||
got, err := UnmarshalPayload(data)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Re-marshal and compare — proves we consumed and reproduced all fields
|
|
||||||
remarshaled := MarshalPayload(nil, got)
|
|
||||||
assert.Equal(t, data, remarshaled)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// wrapDetails wraps raw detail bytes in the outer NebulaHandshake envelope
|
|
||||||
// so UnmarshalPayload can reach unmarshalPayloadDetails.
|
|
||||||
func wrapDetails(details []byte) []byte {
|
|
||||||
var out []byte
|
|
||||||
out = protowire.AppendTag(out, 1, protowire.BytesType)
|
|
||||||
out = protowire.AppendBytes(out, details)
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPayloadUnmarshalErrors(t *testing.T) {
|
|
||||||
t.Run("nil input", func(t *testing.T) {
|
|
||||||
got, err := UnmarshalPayload(nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, uint32(0), got.InitiatorIndex)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("truncated outer tag", func(t *testing.T) {
|
|
||||||
_, err := UnmarshalPayload([]byte{0x80})
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("truncated outer details field", func(t *testing.T) {
|
|
||||||
_, err := UnmarshalPayload([]byte{0x0a, 0x64, 0x01, 0x02, 0x03, 0x04, 0x05})
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("truncated outer unknown field", func(t *testing.T) {
|
|
||||||
// Valid tag for unknown field 99 varint, but no value follows
|
|
||||||
var data []byte
|
|
||||||
data = protowire.AppendTag(data, 99, protowire.VarintType)
|
|
||||||
_, err := UnmarshalPayload(data)
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("truncated details tag", func(t *testing.T) {
|
|
||||||
_, err := UnmarshalPayload(wrapDetails([]byte{0x80}))
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("truncated cert bytes", func(t *testing.T) {
|
|
||||||
// Field 1 (cert), bytes type, length 10 but only 2 bytes
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldCert, protowire.BytesType)
|
|
||||||
details = append(details, 0x0a, 0x01, 0x02) // length 10, only 2 bytes
|
|
||||||
_, err := UnmarshalPayload(wrapDetails(details))
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("truncated initiator index varint", func(t *testing.T) {
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType)
|
|
||||||
details = append(details, 0x80) // incomplete varint
|
|
||||||
_, err := UnmarshalPayload(wrapDetails(details))
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("truncated responder index varint", func(t *testing.T) {
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldResponderIndex, protowire.VarintType)
|
|
||||||
details = append(details, 0x80)
|
|
||||||
_, err := UnmarshalPayload(wrapDetails(details))
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("truncated time varint", func(t *testing.T) {
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldTime, protowire.VarintType)
|
|
||||||
details = append(details, 0x80)
|
|
||||||
_, err := UnmarshalPayload(wrapDetails(details))
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("truncated cert version varint", func(t *testing.T) {
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldCertVersion, protowire.VarintType)
|
|
||||||
details = append(details, 0x80)
|
|
||||||
_, err := UnmarshalPayload(wrapDetails(details))
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("truncated unknown field in details", func(t *testing.T) {
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, 50, protowire.VarintType)
|
|
||||||
details = append(details, 0x80) // incomplete varint
|
|
||||||
_, err := UnmarshalPayload(wrapDetails(details))
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("cert with wrong wire type rejected", func(t *testing.T) {
|
|
||||||
// fieldCert as Varint instead of Bytes.
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldCert, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, 42)
|
|
||||||
_, err := UnmarshalPayload(wrapDetails(details))
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("initiator index with wrong wire type rejected", func(t *testing.T) {
|
|
||||||
// fieldInitiatorIndex as Bytes instead of Varint.
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.BytesType)
|
|
||||||
details = protowire.AppendBytes(details, []byte{1, 2, 3})
|
|
||||||
_, err := UnmarshalPayload(wrapDetails(details))
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("time with wrong wire type rejected", func(t *testing.T) {
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldTime, protowire.BytesType)
|
|
||||||
details = protowire.AppendBytes(details, []byte{1, 2, 3})
|
|
||||||
_, err := UnmarshalPayload(wrapDetails(details))
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("cert version with wrong wire type rejected", func(t *testing.T) {
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldCertVersion, protowire.BytesType)
|
|
||||||
details = protowire.AppendBytes(details, []byte{1, 2, 3})
|
|
||||||
_, err := UnmarshalPayload(wrapDetails(details))
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("repeated singular field follows proto3 last-wins", func(t *testing.T) {
|
|
||||||
// Per proto3, multiple instances of a singular field are accepted and
|
|
||||||
// the last value wins. We keep this behavior so that peers using
|
|
||||||
// alternative encoders aren't rejected.
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, 1)
|
|
||||||
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, 42)
|
|
||||||
got, err := UnmarshalPayload(wrapDetails(details))
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, uint32(42), got.InitiatorIndex)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("initiator index varint overflow rejected", func(t *testing.T) {
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, math.MaxUint32+1)
|
|
||||||
_, err := UnmarshalPayload(wrapDetails(details))
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("cert version varint overflow rejected", func(t *testing.T) {
|
|
||||||
var details []byte
|
|
||||||
details = protowire.AppendTag(details, fieldCertVersion, protowire.VarintType)
|
|
||||||
details = protowire.AppendVarint(details, math.MaxUint32+1)
|
|
||||||
_, err := UnmarshalPayload(wrapDetails(details))
|
|
||||||
assert.Error(t, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// FuzzPayload feeds arbitrary bytes through UnmarshalPayload to confirm it
|
|
||||||
// never panics, and for any input that parses cleanly, that re-marshal +
|
|
||||||
// re-parse is a fix-point. Inputs come from an authenticated peer (post-
|
|
||||||
// noise-decrypt), so the threat model is "valid peer behaving arbitrarily,"
|
|
||||||
// not "unauthenticated injection."
|
|
||||||
func FuzzPayload(f *testing.F) {
|
|
||||||
// Seed corpus with a handful of known-good shapes.
|
|
||||||
f.Add(MarshalPayload(nil, Payload{}))
|
|
||||||
f.Add(MarshalPayload(nil, Payload{Cert: []byte{1, 2, 3}, CertVersion: 2}))
|
|
||||||
f.Add(MarshalPayload(nil, Payload{InitiatorIndex: 42, Time: 1}))
|
|
||||||
f.Add(MarshalPayload(nil, Payload{
|
|
||||||
Cert: []byte("seed-cert"),
|
|
||||||
InitiatorIndex: 1,
|
|
||||||
ResponderIndex: 2,
|
|
||||||
Time: 3,
|
|
||||||
CertVersion: 2,
|
|
||||||
}))
|
|
||||||
f.Add([]byte{})
|
|
||||||
f.Add([]byte{0xff})
|
|
||||||
|
|
||||||
f.Fuzz(func(t *testing.T, data []byte) {
|
|
||||||
p1, err := UnmarshalPayload(data)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// For any input that parses, re-marshaling and re-parsing must
|
|
||||||
// yield an equivalent Payload. This catches dispatch bugs (e.g.
|
|
||||||
// emitting a field on marshal that we don't accept on parse) and
|
|
||||||
// any non-idempotent parsing behavior.
|
|
||||||
b2 := MarshalPayload(nil, p1)
|
|
||||||
p2, err := UnmarshalPayload(b2)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("re-parse of self-marshaled payload failed: %v\nintermediate: %x\n", err, b2)
|
|
||||||
}
|
|
||||||
if !payloadsEqual(p1, p2) {
|
|
||||||
t.Fatalf("re-marshal not idempotent\nfirst: %+v\nsecond: %+v", p1, p2)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func payloadsEqual(a, b Payload) bool {
|
|
||||||
return bytes.Equal(a.Cert, b.Cert) &&
|
|
||||||
a.InitiatorIndex == b.InitiatorIndex &&
|
|
||||||
a.ResponderIndex == b.ResponderIndex &&
|
|
||||||
a.Time == b.Time &&
|
|
||||||
a.CertVersion == b.CertVersion
|
|
||||||
}
|
|
||||||
+813
@@ -0,0 +1,813 @@
|
|||||||
|
package nebula
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"log/slog"
|
||||||
|
"net/netip"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/flynn/noise"
|
||||||
|
"github.com/slackhq/nebula/cert"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NOISE IX Handshakes
|
||||||
|
|
||||||
|
// This function constructs a handshake packet, but does not actually send it
|
||||||
|
// Sending is done by the handshake manager
|
||||||
|
func ixHandshakeStage0(f *Interface, hh *HandshakeHostInfo) bool {
|
||||||
|
err := f.handshakeManager.allocateIndex(hh)
|
||||||
|
if err != nil {
|
||||||
|
f.l.Error("Failed to generate index",
|
||||||
|
"error", err,
|
||||||
|
"vpnAddrs", hh.hostinfo.vpnAddrs,
|
||||||
|
"handshake", m{"stage": 0, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
cs := f.pki.getCertState()
|
||||||
|
v := cs.initiatingVersion
|
||||||
|
if hh.initiatingVersionOverride != cert.VersionPre1 {
|
||||||
|
v = hh.initiatingVersionOverride
|
||||||
|
} else if v < cert.Version2 {
|
||||||
|
// If we're connecting to a v6 address we should encourage use of a V2 cert
|
||||||
|
for _, a := range hh.hostinfo.vpnAddrs {
|
||||||
|
if a.Is6() {
|
||||||
|
v = cert.Version2
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
crt := cs.getCertificate(v)
|
||||||
|
if crt == nil {
|
||||||
|
f.l.Error("Unable to handshake with host because no certificate is available",
|
||||||
|
"vpnAddrs", hh.hostinfo.vpnAddrs,
|
||||||
|
"handshake", m{"stage": 0, "style": "ix_psk0"},
|
||||||
|
"certVersion", v,
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
crtHs := cs.getHandshakeBytes(v)
|
||||||
|
if crtHs == nil {
|
||||||
|
f.l.Error("Unable to handshake with host because no certificate handshake bytes is available",
|
||||||
|
"vpnAddrs", hh.hostinfo.vpnAddrs,
|
||||||
|
"handshake", m{"stage": 0, "style": "ix_psk0"},
|
||||||
|
"certVersion", v,
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
ci, err := NewConnectionState(cs, crt, true, noise.HandshakeIX)
|
||||||
|
if err != nil {
|
||||||
|
f.l.Error("Failed to create connection state",
|
||||||
|
"error", err,
|
||||||
|
"vpnAddrs", hh.hostinfo.vpnAddrs,
|
||||||
|
"handshake", m{"stage": 0, "style": "ix_psk0"},
|
||||||
|
"certVersion", v,
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
hh.hostinfo.ConnectionState = ci
|
||||||
|
|
||||||
|
hs := &NebulaHandshake{
|
||||||
|
Details: &NebulaHandshakeDetails{
|
||||||
|
InitiatorIndex: hh.hostinfo.localIndexId,
|
||||||
|
Time: uint64(time.Now().UnixNano()),
|
||||||
|
Cert: crtHs,
|
||||||
|
CertVersion: uint32(v),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
hsBytes, err := hs.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
f.l.Error("Failed to marshal handshake message",
|
||||||
|
"error", err,
|
||||||
|
"vpnAddrs", hh.hostinfo.vpnAddrs,
|
||||||
|
"certVersion", v,
|
||||||
|
"handshake", m{"stage": 0, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
h := header.Encode(make([]byte, header.Len), header.Version, header.Handshake, header.HandshakeIXPSK0, 0, 1)
|
||||||
|
|
||||||
|
msg, _, _, err := ci.H.WriteMessage(h, hsBytes)
|
||||||
|
if err != nil {
|
||||||
|
f.l.Error("Failed to call noise.WriteMessage",
|
||||||
|
"error", err,
|
||||||
|
"vpnAddrs", hh.hostinfo.vpnAddrs,
|
||||||
|
"handshake", m{"stage": 0, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// We are sending handshake packet 1, so we don't expect to receive
|
||||||
|
// handshake packet 1 from the responder
|
||||||
|
ci.window.Update(f.l, 1)
|
||||||
|
|
||||||
|
hh.hostinfo.HandshakePacket[0] = msg
|
||||||
|
hh.ready = true
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ixHandshakeStage1(f *Interface, via ViaSender, packet []byte, h *header.H) {
|
||||||
|
cs := f.pki.getCertState()
|
||||||
|
crt := cs.GetDefaultCertificate()
|
||||||
|
if crt == nil {
|
||||||
|
f.l.Error("Unable to handshake with host because no certificate is available",
|
||||||
|
"from", via,
|
||||||
|
"handshake", m{"stage": 0, "style": "ix_psk0"},
|
||||||
|
"certVersion", cs.initiatingVersion,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ci, err := NewConnectionState(cs, crt, false, noise.HandshakeIX)
|
||||||
|
if err != nil {
|
||||||
|
f.l.Error("Failed to create connection state",
|
||||||
|
"error", err,
|
||||||
|
"from", via,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark packet 1 as seen so it doesn't show up as missed
|
||||||
|
ci.window.Update(f.l, 1)
|
||||||
|
|
||||||
|
msg, _, _, err := ci.H.ReadMessage(nil, packet[header.Len:])
|
||||||
|
if err != nil {
|
||||||
|
f.l.Error("Failed to call noise.ReadMessage",
|
||||||
|
"error", err,
|
||||||
|
"from", via,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hs := &NebulaHandshake{}
|
||||||
|
err = hs.Unmarshal(msg)
|
||||||
|
if err != nil || hs.Details == nil {
|
||||||
|
f.l.Error("Failed unmarshal handshake message",
|
||||||
|
"error", err,
|
||||||
|
"from", via,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rc, err := cert.Recombine(cert.Version(hs.Details.CertVersion), hs.Details.Cert, ci.H.PeerStatic(), ci.Curve())
|
||||||
|
if err != nil {
|
||||||
|
f.l.Info("Handshake did not contain a certificate",
|
||||||
|
"error", err,
|
||||||
|
"from", via,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
remoteCert, err := f.pki.GetCAPool().VerifyCertificate(time.Now(), rc)
|
||||||
|
if err != nil {
|
||||||
|
fp, fperr := rc.Fingerprint()
|
||||||
|
if fperr != nil {
|
||||||
|
fp = "<error generating certificate fingerprint>"
|
||||||
|
}
|
||||||
|
|
||||||
|
attrs := []slog.Attr{
|
||||||
|
slog.Any("error", err),
|
||||||
|
slog.Any("from", via),
|
||||||
|
slog.Any("handshake", m{"stage": 1, "style": "ix_psk0"}),
|
||||||
|
slog.Any("certVpnNetworks", rc.Networks()),
|
||||||
|
slog.String("certFingerprint", fp),
|
||||||
|
}
|
||||||
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
|
attrs = append(attrs, slog.Any("cert", rc))
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogAttrs is intentional: attrs is a pre-built []slog.Attr slice that
|
||||||
|
// callers grow conditionally, which has no pair-form equivalent.
|
||||||
|
//nolint:sloglint
|
||||||
|
f.l.LogAttrs(context.Background(), slog.LevelInfo, "Invalid certificate from host", attrs...)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal(remoteCert.Certificate.PublicKey(), ci.H.PeerStatic()) {
|
||||||
|
f.l.Info("public key mismatch between certificate and handshake",
|
||||||
|
"from", via,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
"cert", remoteCert,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if remoteCert.Certificate.Version() != ci.myCert.Version() {
|
||||||
|
// We started off using the wrong certificate version, lets see if we can match the version that was sent to us
|
||||||
|
myCertOtherVersion := cs.getCertificate(remoteCert.Certificate.Version())
|
||||||
|
if myCertOtherVersion == nil {
|
||||||
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
|
f.l.Debug("Might be unable to handshake with host due to missing certificate version",
|
||||||
|
"error", err,
|
||||||
|
"from", via,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
"cert", remoteCert,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Record the certificate we are actually using
|
||||||
|
ci.myCert = myCertOtherVersion
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(remoteCert.Certificate.Networks()) == 0 {
|
||||||
|
f.l.Info("No networks in certificate",
|
||||||
|
"error", err,
|
||||||
|
"from", via,
|
||||||
|
"cert", remoteCert,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
certName := remoteCert.Certificate.Name()
|
||||||
|
certVersion := remoteCert.Certificate.Version()
|
||||||
|
fingerprint := remoteCert.Fingerprint
|
||||||
|
issuer := remoteCert.Certificate.Issuer()
|
||||||
|
vpnNetworks := remoteCert.Certificate.Networks()
|
||||||
|
|
||||||
|
anyVpnAddrsInCommon := false
|
||||||
|
vpnAddrs := make([]netip.Addr, len(vpnNetworks))
|
||||||
|
for i, network := range vpnNetworks {
|
||||||
|
if f.myVpnAddrsTable.Contains(network.Addr()) {
|
||||||
|
f.l.Error("Refusing to handshake with myself",
|
||||||
|
"vpnNetworks", vpnNetworks,
|
||||||
|
"from", via,
|
||||||
|
"certName", certName,
|
||||||
|
"certVersion", certVersion,
|
||||||
|
"fingerprint", fingerprint,
|
||||||
|
"issuer", issuer,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vpnAddrs[i] = network.Addr()
|
||||||
|
if f.myVpnNetworksTable.Contains(network.Addr()) {
|
||||||
|
anyVpnAddrsInCommon = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !via.IsRelayed {
|
||||||
|
// We only want to apply the remote allow list for direct tunnels here
|
||||||
|
if !f.lightHouse.GetRemoteAllowList().AllowAll(vpnAddrs, via.UdpAddr.Addr()) {
|
||||||
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
|
f.l.Debug("lighthouse.remote_allow_list denied incoming handshake",
|
||||||
|
"vpnAddrs", vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
myIndex, err := generateIndex(f.l)
|
||||||
|
if err != nil {
|
||||||
|
f.l.Error("Failed to generate index",
|
||||||
|
"error", err,
|
||||||
|
"vpnAddrs", vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"certName", certName,
|
||||||
|
"certVersion", certVersion,
|
||||||
|
"fingerprint", fingerprint,
|
||||||
|
"issuer", issuer,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hostinfo := &HostInfo{
|
||||||
|
ConnectionState: ci,
|
||||||
|
localIndexId: myIndex,
|
||||||
|
remoteIndexId: hs.Details.InitiatorIndex,
|
||||||
|
vpnAddrs: vpnAddrs,
|
||||||
|
HandshakePacket: make(map[uint8][]byte, 0),
|
||||||
|
lastHandshakeTime: hs.Details.Time,
|
||||||
|
relayState: RelayState{
|
||||||
|
relays: nil,
|
||||||
|
relayForByAddr: map[netip.Addr]*Relay{},
|
||||||
|
relayForByIdx: map[uint32]*Relay{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
msgRxL := f.l.With(
|
||||||
|
"vpnAddrs", vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"certName", certName,
|
||||||
|
"certVersion", certVersion,
|
||||||
|
"fingerprint", fingerprint,
|
||||||
|
"issuer", issuer,
|
||||||
|
"initiatorIndex", hs.Details.InitiatorIndex,
|
||||||
|
"responderIndex", hs.Details.ResponderIndex,
|
||||||
|
"remoteIndex", h.RemoteIndex,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
|
||||||
|
if anyVpnAddrsInCommon {
|
||||||
|
msgRxL.Info("Handshake message received")
|
||||||
|
} else {
|
||||||
|
//todo warn if not lighthouse or relay?
|
||||||
|
msgRxL.Info("Handshake message received, but no vpnNetworks in common.")
|
||||||
|
}
|
||||||
|
|
||||||
|
hs.Details.ResponderIndex = myIndex
|
||||||
|
hs.Details.Cert = cs.getHandshakeBytes(ci.myCert.Version())
|
||||||
|
if hs.Details.Cert == nil {
|
||||||
|
msgRxL.Error("Unable to handshake with host because no certificate handshake bytes is available",
|
||||||
|
"myCertVersion", ci.myCert.Version(),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hs.Details.CertVersion = uint32(ci.myCert.Version())
|
||||||
|
// Update the time in case their clock is way off from ours
|
||||||
|
hs.Details.Time = uint64(time.Now().UnixNano())
|
||||||
|
|
||||||
|
hsBytes, err := hs.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
f.l.Error("Failed to marshal handshake message",
|
||||||
|
"error", err,
|
||||||
|
"vpnAddrs", hostinfo.vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"certName", certName,
|
||||||
|
"certVersion", certVersion,
|
||||||
|
"fingerprint", fingerprint,
|
||||||
|
"issuer", issuer,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
nh := header.Encode(make([]byte, header.Len), header.Version, header.Handshake, header.HandshakeIXPSK0, hs.Details.InitiatorIndex, 2)
|
||||||
|
msg, dKey, eKey, err := ci.H.WriteMessage(nh, hsBytes)
|
||||||
|
if err != nil {
|
||||||
|
f.l.Error("Failed to call noise.WriteMessage",
|
||||||
|
"error", err,
|
||||||
|
"vpnAddrs", hostinfo.vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"certName", certName,
|
||||||
|
"certVersion", certVersion,
|
||||||
|
"fingerprint", fingerprint,
|
||||||
|
"issuer", issuer,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
} else if dKey == nil || eKey == nil {
|
||||||
|
f.l.Error("Noise did not arrive at a key",
|
||||||
|
"vpnAddrs", hostinfo.vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"certName", certName,
|
||||||
|
"certVersion", certVersion,
|
||||||
|
"fingerprint", fingerprint,
|
||||||
|
"issuer", issuer,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hostinfo.HandshakePacket[0] = make([]byte, len(packet[header.Len:]))
|
||||||
|
copy(hostinfo.HandshakePacket[0], packet[header.Len:])
|
||||||
|
|
||||||
|
// Regardless of whether you are the sender or receiver, you should arrive here
|
||||||
|
// and complete standing up the connection.
|
||||||
|
hostinfo.HandshakePacket[2] = make([]byte, len(msg))
|
||||||
|
copy(hostinfo.HandshakePacket[2], msg)
|
||||||
|
|
||||||
|
// We are sending handshake packet 2, so we don't expect to receive
|
||||||
|
// handshake packet 2 from the initiator.
|
||||||
|
ci.window.Update(f.l, 2)
|
||||||
|
|
||||||
|
ci.peerCert = remoteCert
|
||||||
|
ci.dKey = NewNebulaCipherState(dKey)
|
||||||
|
ci.eKey = NewNebulaCipherState(eKey)
|
||||||
|
|
||||||
|
hostinfo.remotes = f.lightHouse.QueryCache(vpnAddrs)
|
||||||
|
if !via.IsRelayed {
|
||||||
|
hostinfo.SetRemote(via.UdpAddr)
|
||||||
|
}
|
||||||
|
hostinfo.buildNetworks(f.myVpnNetworksTable, remoteCert.Certificate)
|
||||||
|
|
||||||
|
existing, err := f.handshakeManager.CheckAndComplete(hostinfo, 0, f)
|
||||||
|
if err != nil {
|
||||||
|
switch err {
|
||||||
|
case ErrAlreadySeen:
|
||||||
|
// Update remote if preferred
|
||||||
|
if existing.SetRemoteIfPreferred(f.hostMap, via) {
|
||||||
|
// Send a test packet to ensure the other side has also switched to
|
||||||
|
// the preferred remote
|
||||||
|
f.SendMessageToVpnAddr(header.Test, header.TestRequest, vpnAddrs[0], []byte(""), make([]byte, 12, 12), make([]byte, mtu))
|
||||||
|
}
|
||||||
|
|
||||||
|
msg = existing.HandshakePacket[2]
|
||||||
|
f.messageMetrics.Tx(header.Handshake, header.MessageSubType(msg[1]), 1)
|
||||||
|
if !via.IsRelayed {
|
||||||
|
err := f.outside.WriteTo(msg, via.UdpAddr)
|
||||||
|
if err != nil {
|
||||||
|
f.l.Error("Failed to send handshake message",
|
||||||
|
"vpnAddrs", existing.vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"handshake", m{"stage": 2, "style": "ix_psk0"},
|
||||||
|
"cached", true,
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
f.l.Info("Handshake message sent",
|
||||||
|
"vpnAddrs", existing.vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"handshake", m{"stage": 2, "style": "ix_psk0"},
|
||||||
|
"cached", true,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
if via.relay == nil {
|
||||||
|
f.l.Error("Handshake send failed: both addr and via.relay are nil.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
|
||||||
|
f.SendVia(via.relayHI, via.relay, msg, make([]byte, 12), make([]byte, mtu), false)
|
||||||
|
f.l.Info("Handshake message sent",
|
||||||
|
"vpnAddrs", existing.vpnAddrs,
|
||||||
|
"relay", via.relayHI.vpnAddrs[0],
|
||||||
|
"handshake", m{"stage": 2, "style": "ix_psk0"},
|
||||||
|
"cached", true,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case ErrExistingHostInfo:
|
||||||
|
// This means there was an existing tunnel and this handshake was older than the one we are currently based on
|
||||||
|
f.l.Info("Handshake too old",
|
||||||
|
"vpnAddrs", vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"certName", certName,
|
||||||
|
"certVersion", certVersion,
|
||||||
|
"oldHandshakeTime", existing.lastHandshakeTime,
|
||||||
|
"newHandshakeTime", hostinfo.lastHandshakeTime,
|
||||||
|
"fingerprint", fingerprint,
|
||||||
|
"issuer", issuer,
|
||||||
|
"initiatorIndex", hs.Details.InitiatorIndex,
|
||||||
|
"responderIndex", hs.Details.ResponderIndex,
|
||||||
|
"remoteIndex", h.RemoteIndex,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
|
||||||
|
// Send a test packet to trigger an authenticated tunnel test, this should suss out any lingering tunnel issues
|
||||||
|
f.SendMessageToVpnAddr(header.Test, header.TestRequest, vpnAddrs[0], []byte(""), make([]byte, 12, 12), make([]byte, mtu))
|
||||||
|
return
|
||||||
|
case ErrLocalIndexCollision:
|
||||||
|
// This means we failed to insert because of collision on localIndexId. Just let the next handshake packet retry
|
||||||
|
f.l.Error("Failed to add HostInfo due to localIndex collision",
|
||||||
|
"vpnAddrs", vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"certName", certName,
|
||||||
|
"certVersion", certVersion,
|
||||||
|
"fingerprint", fingerprint,
|
||||||
|
"issuer", issuer,
|
||||||
|
"initiatorIndex", hs.Details.InitiatorIndex,
|
||||||
|
"responderIndex", hs.Details.ResponderIndex,
|
||||||
|
"remoteIndex", h.RemoteIndex,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
"localIndex", hostinfo.localIndexId,
|
||||||
|
"collision", existing.vpnAddrs,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
// Shouldn't happen, but just in case someone adds a new error type to CheckAndComplete
|
||||||
|
// And we forget to update it here
|
||||||
|
f.l.Error("Failed to add HostInfo to HostMap",
|
||||||
|
"error", err,
|
||||||
|
"vpnAddrs", vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"certName", certName,
|
||||||
|
"certVersion", certVersion,
|
||||||
|
"fingerprint", fingerprint,
|
||||||
|
"issuer", issuer,
|
||||||
|
"initiatorIndex", hs.Details.InitiatorIndex,
|
||||||
|
"responderIndex", hs.Details.ResponderIndex,
|
||||||
|
"remoteIndex", h.RemoteIndex,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do the send
|
||||||
|
f.messageMetrics.Tx(header.Handshake, header.MessageSubType(msg[1]), 1)
|
||||||
|
if !via.IsRelayed {
|
||||||
|
err = f.outside.WriteTo(msg, via.UdpAddr)
|
||||||
|
log := f.l.With(
|
||||||
|
"vpnAddrs", vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"certName", certName,
|
||||||
|
"certVersion", certVersion,
|
||||||
|
"fingerprint", fingerprint,
|
||||||
|
"issuer", issuer,
|
||||||
|
"initiatorIndex", hs.Details.InitiatorIndex,
|
||||||
|
"responderIndex", hs.Details.ResponderIndex,
|
||||||
|
"remoteIndex", h.RemoteIndex,
|
||||||
|
"handshake", m{"stage": 2, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed to send handshake", "error", err)
|
||||||
|
} else {
|
||||||
|
log.Info("Handshake message sent")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if via.relay == nil {
|
||||||
|
f.l.Error("Handshake send failed: both addr and via.relay are nil.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
|
||||||
|
// I successfully received a handshake. Just in case I marked this tunnel as 'Disestablished', ensure
|
||||||
|
// it's correctly marked as working.
|
||||||
|
via.relayHI.relayState.UpdateRelayForByIdxState(via.remoteIdx, Established)
|
||||||
|
f.SendVia(via.relayHI, via.relay, msg, make([]byte, 12), make([]byte, mtu), false)
|
||||||
|
f.l.Info("Handshake message sent",
|
||||||
|
"vpnAddrs", vpnAddrs,
|
||||||
|
"relay", via.relayHI.vpnAddrs[0],
|
||||||
|
"certName", certName,
|
||||||
|
"certVersion", certVersion,
|
||||||
|
"fingerprint", fingerprint,
|
||||||
|
"issuer", issuer,
|
||||||
|
"initiatorIndex", hs.Details.InitiatorIndex,
|
||||||
|
"responderIndex", hs.Details.ResponderIndex,
|
||||||
|
"remoteIndex", h.RemoteIndex,
|
||||||
|
"handshake", m{"stage": 2, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.connectionManager.AddTrafficWatch(hostinfo)
|
||||||
|
|
||||||
|
hostinfo.remotes.RefreshFromHandshake(vpnAddrs)
|
||||||
|
|
||||||
|
// Don't wait for UpdateWorker
|
||||||
|
if f.lightHouse.IsAnyLighthouseAddr(vpnAddrs) {
|
||||||
|
f.lightHouse.TriggerUpdate()
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ixHandshakeStage2(f *Interface, via ViaSender, hh *HandshakeHostInfo, packet []byte, h *header.H) bool {
|
||||||
|
if hh == nil {
|
||||||
|
// Nothing here to tear down, got a bogus stage 2 packet
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
hh.Lock()
|
||||||
|
defer hh.Unlock()
|
||||||
|
|
||||||
|
hostinfo := hh.hostinfo
|
||||||
|
if !via.IsRelayed {
|
||||||
|
// The vpnAddr we know about is the one we tried to handshake with, use it to apply the remote allow list.
|
||||||
|
if !f.lightHouse.GetRemoteAllowList().AllowAll(hostinfo.vpnAddrs, via.UdpAddr.Addr()) {
|
||||||
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
|
f.l.Debug("lighthouse.remote_allow_list denied incoming handshake",
|
||||||
|
"vpnAddrs", hostinfo.vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ci := hostinfo.ConnectionState
|
||||||
|
msg, eKey, dKey, err := ci.H.ReadMessage(nil, packet[header.Len:])
|
||||||
|
if err != nil {
|
||||||
|
f.l.Error("Failed to call noise.ReadMessage",
|
||||||
|
"error", err,
|
||||||
|
"vpnAddrs", hostinfo.vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"handshake", m{"stage": 2, "style": "ix_psk0"},
|
||||||
|
"header", h,
|
||||||
|
)
|
||||||
|
|
||||||
|
// We don't want to tear down the connection on a bad ReadMessage because it could be an attacker trying
|
||||||
|
// to DOS us. Every other error condition after should to allow a possible good handshake to complete in the
|
||||||
|
// near future
|
||||||
|
return false
|
||||||
|
} else if dKey == nil || eKey == nil {
|
||||||
|
f.l.Error("Noise did not arrive at a key",
|
||||||
|
"vpnAddrs", hostinfo.vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"handshake", m{"stage": 2, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
|
||||||
|
// This should be impossible in IX but just in case, if we get here then there is no chance to recover
|
||||||
|
// the handshake state machine. Tear it down
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
hs := &NebulaHandshake{}
|
||||||
|
err = hs.Unmarshal(msg)
|
||||||
|
if err != nil || hs.Details == nil {
|
||||||
|
f.l.Error("Failed unmarshal handshake message",
|
||||||
|
"error", err,
|
||||||
|
"vpnAddrs", hostinfo.vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"handshake", m{"stage": 2, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
|
||||||
|
// The handshake state machine is complete, if things break now there is no chance to recover. Tear down and start again
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
rc, err := cert.Recombine(cert.Version(hs.Details.CertVersion), hs.Details.Cert, ci.H.PeerStatic(), ci.Curve())
|
||||||
|
if err != nil {
|
||||||
|
f.l.Info("Handshake did not contain a certificate",
|
||||||
|
"error", err,
|
||||||
|
"from", via,
|
||||||
|
"vpnAddrs", hostinfo.vpnAddrs,
|
||||||
|
"handshake", m{"stage": 2, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
remoteCert, err := f.pki.GetCAPool().VerifyCertificate(time.Now(), rc)
|
||||||
|
if err != nil {
|
||||||
|
fp, err := rc.Fingerprint()
|
||||||
|
if err != nil {
|
||||||
|
fp = "<error generating certificate fingerprint>"
|
||||||
|
}
|
||||||
|
|
||||||
|
attrs := []slog.Attr{
|
||||||
|
slog.Any("error", err),
|
||||||
|
slog.Any("from", via),
|
||||||
|
slog.Any("vpnAddrs", hostinfo.vpnAddrs),
|
||||||
|
slog.Any("handshake", m{"stage": 2, "style": "ix_psk0"}),
|
||||||
|
slog.String("certFingerprint", fp),
|
||||||
|
slog.Any("certVpnNetworks", rc.Networks()),
|
||||||
|
}
|
||||||
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
|
attrs = append(attrs, slog.Any("cert", rc))
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogAttrs is intentional: attrs is a pre-built []slog.Attr slice that
|
||||||
|
// callers grow conditionally, which has no pair-form equivalent.
|
||||||
|
//nolint:sloglint
|
||||||
|
f.l.LogAttrs(context.Background(), slog.LevelInfo, "Invalid certificate from host", attrs...)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if !bytes.Equal(remoteCert.Certificate.PublicKey(), ci.H.PeerStatic()) {
|
||||||
|
f.l.Info("public key mismatch between certificate and handshake",
|
||||||
|
"from", via,
|
||||||
|
"handshake", m{"stage": 2, "style": "ix_psk0"},
|
||||||
|
"cert", remoteCert,
|
||||||
|
)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(remoteCert.Certificate.Networks()) == 0 {
|
||||||
|
f.l.Info("No networks in certificate",
|
||||||
|
"error", err,
|
||||||
|
"from", via,
|
||||||
|
"vpnAddrs", hostinfo.vpnAddrs,
|
||||||
|
"cert", remoteCert,
|
||||||
|
"handshake", m{"stage": 2, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
vpnNetworks := remoteCert.Certificate.Networks()
|
||||||
|
certName := remoteCert.Certificate.Name()
|
||||||
|
certVersion := remoteCert.Certificate.Version()
|
||||||
|
fingerprint := remoteCert.Fingerprint
|
||||||
|
issuer := remoteCert.Certificate.Issuer()
|
||||||
|
|
||||||
|
hostinfo.remoteIndexId = hs.Details.ResponderIndex
|
||||||
|
hostinfo.lastHandshakeTime = hs.Details.Time
|
||||||
|
|
||||||
|
// Store their cert and our symmetric keys
|
||||||
|
ci.peerCert = remoteCert
|
||||||
|
ci.dKey = NewNebulaCipherState(dKey)
|
||||||
|
ci.eKey = NewNebulaCipherState(eKey)
|
||||||
|
|
||||||
|
// Make sure the current udpAddr being used is set for responding
|
||||||
|
if !via.IsRelayed {
|
||||||
|
hostinfo.SetRemote(via.UdpAddr)
|
||||||
|
} else {
|
||||||
|
hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
correctHostResponded := false
|
||||||
|
anyVpnAddrsInCommon := false
|
||||||
|
vpnAddrs := make([]netip.Addr, len(vpnNetworks))
|
||||||
|
for i, network := range vpnNetworks {
|
||||||
|
vpnAddrs[i] = network.Addr()
|
||||||
|
if f.myVpnNetworksTable.Contains(network.Addr()) {
|
||||||
|
anyVpnAddrsInCommon = true
|
||||||
|
}
|
||||||
|
if hostinfo.vpnAddrs[0] == network.Addr() {
|
||||||
|
// todo is it more correct to see if any of hostinfo.vpnAddrs are in the cert? it should have len==1, but one day it might not?
|
||||||
|
correctHostResponded = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the right host responded
|
||||||
|
if !correctHostResponded {
|
||||||
|
f.l.Info("Incorrect host responded to handshake",
|
||||||
|
"intendedVpnAddrs", hostinfo.vpnAddrs,
|
||||||
|
"haveVpnNetworks", vpnNetworks,
|
||||||
|
"from", via,
|
||||||
|
"certName", certName,
|
||||||
|
"certVersion", certVersion,
|
||||||
|
"handshake", m{"stage": 2, "style": "ix_psk0"},
|
||||||
|
)
|
||||||
|
|
||||||
|
// Release our old handshake from pending, it should not continue
|
||||||
|
f.handshakeManager.DeleteHostInfo(hostinfo)
|
||||||
|
|
||||||
|
// Create a new hostinfo/handshake for the intended vpn ip
|
||||||
|
//TODO is hostinfo.vpnAddrs[0] always the address to use?
|
||||||
|
f.handshakeManager.StartHandshake(hostinfo.vpnAddrs[0], func(newHH *HandshakeHostInfo) {
|
||||||
|
// Block the current used address
|
||||||
|
newHH.hostinfo.remotes = hostinfo.remotes
|
||||||
|
newHH.hostinfo.remotes.BlockRemote(via)
|
||||||
|
|
||||||
|
f.l.Info("Blocked addresses for handshakes",
|
||||||
|
"blockedUdpAddrs", newHH.hostinfo.remotes.CopyBlockedRemotes(),
|
||||||
|
"vpnNetworks", vpnNetworks,
|
||||||
|
"remotes", newHH.hostinfo.remotes.CopyAddrs(f.hostMap.GetPreferredRanges()),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Swap the packet store to benefit the original intended recipient
|
||||||
|
newHH.packetStore = hh.packetStore
|
||||||
|
hh.packetStore = []*cachedPacket{}
|
||||||
|
|
||||||
|
// Finally, put the correct vpn addrs in the host info, tell them to close the tunnel, and return true to tear down
|
||||||
|
hostinfo.vpnAddrs = vpnAddrs
|
||||||
|
f.sendCloseTunnel(hostinfo)
|
||||||
|
})
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark packet 2 as seen so it doesn't show up as missed
|
||||||
|
ci.window.Update(f.l, 2)
|
||||||
|
|
||||||
|
duration := time.Since(hh.startTime).Nanoseconds()
|
||||||
|
msgRxL := f.l.With(
|
||||||
|
"vpnAddrs", vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"certName", certName,
|
||||||
|
"certVersion", certVersion,
|
||||||
|
"fingerprint", fingerprint,
|
||||||
|
"issuer", issuer,
|
||||||
|
"initiatorIndex", hs.Details.InitiatorIndex,
|
||||||
|
"responderIndex", hs.Details.ResponderIndex,
|
||||||
|
"remoteIndex", h.RemoteIndex,
|
||||||
|
"handshake", m{"stage": 2, "style": "ix_psk0"},
|
||||||
|
"durationNs", duration,
|
||||||
|
"sentCachedPackets", len(hh.packetStore),
|
||||||
|
)
|
||||||
|
if anyVpnAddrsInCommon {
|
||||||
|
msgRxL.Info("Handshake message received")
|
||||||
|
} else {
|
||||||
|
//todo warn if not lighthouse or relay?
|
||||||
|
msgRxL.Info("Handshake message received, but no vpnNetworks in common.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build up the radix for the firewall if we have subnets in the cert
|
||||||
|
hostinfo.vpnAddrs = vpnAddrs
|
||||||
|
hostinfo.buildNetworks(f.myVpnNetworksTable, remoteCert.Certificate)
|
||||||
|
|
||||||
|
// Complete our handshake and update metrics, this will replace any existing tunnels for the vpnAddrs here
|
||||||
|
f.handshakeManager.Complete(hostinfo, f)
|
||||||
|
f.connectionManager.AddTrafficWatch(hostinfo)
|
||||||
|
|
||||||
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
|
hostinfo.logger(f.l).Debug("Sending stored packets",
|
||||||
|
"count", len(hh.packetStore),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(hh.packetStore) > 0 {
|
||||||
|
nb := make([]byte, 12, 12)
|
||||||
|
out := make([]byte, mtu)
|
||||||
|
for _, cp := range hh.packetStore {
|
||||||
|
cp.callback(cp.messageType, cp.messageSubType, hostinfo, cp.packet, nb, out)
|
||||||
|
}
|
||||||
|
f.cachedPacketMetrics.sent.Inc(int64(len(hh.packetStore)))
|
||||||
|
}
|
||||||
|
|
||||||
|
hostinfo.remotes.RefreshFromHandshake(vpnAddrs)
|
||||||
|
f.metricHandshakes.Update(duration)
|
||||||
|
|
||||||
|
// Don't wait for UpdateWorker
|
||||||
|
if f.lightHouse.IsAnyLighthouseAddr(vpnAddrs) {
|
||||||
|
f.lightHouse.TriggerUpdate()
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
+169
-604
@@ -14,7 +14,6 @@ import (
|
|||||||
|
|
||||||
"github.com/rcrowley/go-metrics"
|
"github.com/rcrowley/go-metrics"
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
"github.com/slackhq/nebula/handshake"
|
|
||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
"github.com/slackhq/nebula/udp"
|
"github.com/slackhq/nebula/udp"
|
||||||
)
|
)
|
||||||
@@ -23,18 +22,7 @@ const (
|
|||||||
DefaultHandshakeTryInterval = time.Millisecond * 100
|
DefaultHandshakeTryInterval = time.Millisecond * 100
|
||||||
DefaultHandshakeRetries = 10
|
DefaultHandshakeRetries = 10
|
||||||
DefaultHandshakeTriggerBuffer = 64
|
DefaultHandshakeTriggerBuffer = 64
|
||||||
|
DefaultUseRelays = true
|
||||||
// maxCachedPackets is how many unsent packets we'll buffer per pending
|
|
||||||
// handshake before dropping further ones.
|
|
||||||
maxCachedPackets = 100
|
|
||||||
|
|
||||||
// HandshakePacket map keys mirror the IX protocol stage convention:
|
|
||||||
// stage 0 = the initiator's first message (and what the responder
|
|
||||||
// receives, stripped of header)
|
|
||||||
// stage 2 = the responder's reply
|
|
||||||
// Other handshake patterns will need new keys when added.
|
|
||||||
handshakePacketStage0 uint8 = 0
|
|
||||||
handshakePacketStage2 uint8 = 2
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -42,6 +30,7 @@ var (
|
|||||||
tryInterval: DefaultHandshakeTryInterval,
|
tryInterval: DefaultHandshakeTryInterval,
|
||||||
retries: DefaultHandshakeRetries,
|
retries: DefaultHandshakeRetries,
|
||||||
triggerBuffer: DefaultHandshakeTriggerBuffer,
|
triggerBuffer: DefaultHandshakeTriggerBuffer,
|
||||||
|
useRelays: DefaultUseRelays,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -49,6 +38,7 @@ type HandshakeConfig struct {
|
|||||||
tryInterval time.Duration
|
tryInterval time.Duration
|
||||||
retries int64
|
retries int64
|
||||||
triggerBuffer int
|
triggerBuffer int
|
||||||
|
useRelays bool
|
||||||
|
|
||||||
messageMetrics *MessageMetrics
|
messageMetrics *MessageMetrics
|
||||||
}
|
}
|
||||||
@@ -86,11 +76,10 @@ type HandshakeHostInfo struct {
|
|||||||
packetStore []*cachedPacket // A set of packets to be transmitted once the handshake completes
|
packetStore []*cachedPacket // A set of packets to be transmitted once the handshake completes
|
||||||
|
|
||||||
hostinfo *HostInfo
|
hostinfo *HostInfo
|
||||||
machine *handshake.Machine // The handshake state machine, set during stage 0 (initiator) or beginHandshake (responder multi-message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hh *HandshakeHostInfo) cachePacket(l *slog.Logger, t header.MessageType, st header.MessageSubType, packet []byte, f packetCallback, m *cachedPacketMetrics) {
|
func (hh *HandshakeHostInfo) cachePacket(l *slog.Logger, t header.MessageType, st header.MessageSubType, packet []byte, f packetCallback, m *cachedPacketMetrics) {
|
||||||
if len(hh.packetStore) < maxCachedPackets {
|
if len(hh.packetStore) < 100 {
|
||||||
tempPacket := make([]byte, len(packet))
|
tempPacket := make([]byte, len(packet))
|
||||||
copy(tempPacket, packet)
|
copy(tempPacket, packet)
|
||||||
|
|
||||||
@@ -148,18 +137,6 @@ func (hm *HandshakeManager) Run(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (hm *HandshakeManager) HandleIncoming(via ViaSender, packet []byte, h *header.H) {
|
func (hm *HandshakeManager) HandleIncoming(via ViaSender, packet []byte, h *header.H) {
|
||||||
// Gate on known handshake subtypes. Unknown subtypes (or future ones we
|
|
||||||
// don't yet support) are dropped here rather than silently routed through
|
|
||||||
// the IX path. Add a case when introducing a new pattern.
|
|
||||||
switch h.Subtype {
|
|
||||||
case header.HandshakeIXPSK0:
|
|
||||||
// supported
|
|
||||||
default:
|
|
||||||
hm.l.Debug("dropping handshake with unsupported subtype",
|
|
||||||
"from", via, "subtype", h.Subtype)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// First remote allow list check before we know the vpnIp
|
// First remote allow list check before we know the vpnIp
|
||||||
if !via.IsRelayed {
|
if !via.IsRelayed {
|
||||||
if !hm.lightHouse.GetRemoteAllowList().AllowUnknownVpnAddr(via.UdpAddr.Addr()) {
|
if !hm.lightHouse.GetRemoteAllowList().AllowUnknownVpnAddr(via.UdpAddr.Addr()) {
|
||||||
@@ -168,27 +145,19 @@ func (hm *HandshakeManager) HandleIncoming(via ViaSender, packet []byte, h *head
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// First message of a new handshake. The wire format requires RemoteIndex
|
switch h.Subtype {
|
||||||
// to be zero here (the initiator has no responder index to fill in yet),
|
case header.HandshakeIXPSK0:
|
||||||
// and generateIndex never allocates 0, so any non-zero RemoteIndex on a
|
switch h.MessageCounter {
|
||||||
// stage-1 packet is malformed or someone probing for an index collision.
|
case 1:
|
||||||
// Drop without paying the cost of running noise on a pending Machine.
|
ixHandshakeStage1(hm.f, via, packet, h)
|
||||||
if h.MessageCounter == 1 {
|
|
||||||
if h.RemoteIndex != 0 {
|
|
||||||
hm.l.Debug("dropping stage-1 handshake with non-zero RemoteIndex",
|
|
||||||
"from", via, "remoteIndex", h.RemoteIndex)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
hm.beginHandshake(via, packet, h)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Continuation message must match a pending handshake by index.
|
case 2:
|
||||||
// Anything else is an orphaned packet (e.g., late retransmit after
|
newHostinfo := hm.queryIndex(h.RemoteIndex)
|
||||||
// timeout) and is dropped.
|
tearDown := ixHandshakeStage2(hm.f, via, newHostinfo, packet, h)
|
||||||
if hh := hm.queryIndex(h.RemoteIndex); hh != nil {
|
if tearDown && newHostinfo != nil {
|
||||||
hm.continueHandshake(via, hh, packet)
|
hm.DeleteHostInfo(newHostinfo.hostinfo)
|
||||||
return
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,22 +183,13 @@ func (hm *HandshakeManager) handleOutbound(vpnIp netip.Addr, lighthouseTriggered
|
|||||||
hostinfo := hh.hostinfo
|
hostinfo := hh.hostinfo
|
||||||
// If we are out of time, clean up
|
// If we are out of time, clean up
|
||||||
if hh.counter >= hm.config.retries {
|
if hh.counter >= hm.config.retries {
|
||||||
fields := []any{
|
hh.hostinfo.logger(hm.l).Info("Handshake timed out",
|
||||||
"udpAddrs", hh.hostinfo.remotes.CopyAddrs(hm.mainHostMap.GetPreferredRanges()),
|
"udpAddrs", hh.hostinfo.remotes.CopyAddrs(hm.mainHostMap.GetPreferredRanges()),
|
||||||
"initiatorIndex", hh.hostinfo.localIndexId,
|
"initiatorIndex", hh.hostinfo.localIndexId,
|
||||||
"remoteIndex", hh.hostinfo.remoteIndexId,
|
"remoteIndex", hh.hostinfo.remoteIndexId,
|
||||||
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
"durationNs", time.Since(hh.startTime).Nanoseconds(),
|
"durationNs", time.Since(hh.startTime).Nanoseconds(),
|
||||||
}
|
)
|
||||||
// hh.machine can be nil here if buildStage0Packet never succeeded
|
|
||||||
// (e.g., no certificate available). In that case there's no useful
|
|
||||||
// handshake metadata to log.
|
|
||||||
if hh.machine != nil {
|
|
||||||
fields = append(fields, "handshake", m{
|
|
||||||
"stage": uint64(hh.machine.MessageIndex()),
|
|
||||||
"style": header.SubTypeName(header.Handshake, hh.machine.Subtype()),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
hh.hostinfo.logger(hm.l).Info("Handshake timed out", fields...)
|
|
||||||
hm.metricTimedOut.Inc(1)
|
hm.metricTimedOut.Inc(1)
|
||||||
hm.DeleteHostInfo(hostinfo)
|
hm.DeleteHostInfo(hostinfo)
|
||||||
return
|
return
|
||||||
@@ -240,25 +200,12 @@ func (hm *HandshakeManager) handleOutbound(vpnIp netip.Addr, lighthouseTriggered
|
|||||||
|
|
||||||
// Check if we have a handshake packet to transmit yet
|
// Check if we have a handshake packet to transmit yet
|
||||||
if !hh.ready {
|
if !hh.ready {
|
||||||
if !hm.buildStage0Packet(hh) {
|
if !ixHandshakeStage0(hm.f, hh) {
|
||||||
hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval*time.Duration(hh.counter))
|
hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval*time.Duration(hh.counter))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this hardcodes "always retransmit stage 0", which is correct for
|
|
||||||
// IX (the initiator only ever sends one packet, msg1) but wrong the
|
|
||||||
// moment a 3+ message pattern lands. The retry loop should resend the
|
|
||||||
// most recent outgoing message, not always stage 0. That implies
|
|
||||||
// HandshakeHostInfo tracking a single "currentOutbound" packet (bytes +
|
|
||||||
// header metadata) that gets replaced as the handshake progresses,
|
|
||||||
// instead of indexing into HandshakePacket.
|
|
||||||
stage0 := hostinfo.HandshakePacket[handshakePacketStage0]
|
|
||||||
hsFields := m{
|
|
||||||
"stage": uint64(hh.machine.MessageIndex()),
|
|
||||||
"style": header.SubTypeName(header.Handshake, hh.machine.Subtype()),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get a remotes object if we don't already have one.
|
// Get a remotes object if we don't already have one.
|
||||||
// This is mainly to protect us as this should never be the case
|
// This is mainly to protect us as this should never be the case
|
||||||
// NB ^ This comment doesn't jive. It's how the thing gets initialized.
|
// NB ^ This comment doesn't jive. It's how the thing gets initialized.
|
||||||
@@ -292,13 +239,13 @@ func (hm *HandshakeManager) handleOutbound(vpnIp netip.Addr, lighthouseTriggered
|
|||||||
// Send the handshake to all known ips, stage 2 takes care of assigning the hostinfo.remote based on the first to reply
|
// Send the handshake to all known ips, stage 2 takes care of assigning the hostinfo.remote based on the first to reply
|
||||||
var sentTo []netip.AddrPort
|
var sentTo []netip.AddrPort
|
||||||
hostinfo.remotes.ForEach(hm.mainHostMap.GetPreferredRanges(), func(addr netip.AddrPort, _ bool) {
|
hostinfo.remotes.ForEach(hm.mainHostMap.GetPreferredRanges(), func(addr netip.AddrPort, _ bool) {
|
||||||
hm.messageMetrics.Tx(header.Handshake, hh.machine.Subtype(), 1)
|
hm.messageMetrics.Tx(header.Handshake, header.MessageSubType(hostinfo.HandshakePacket[0][1]), 1)
|
||||||
err := hm.outside.WriteTo(stage0, addr)
|
err := hm.outside.WriteTo(hostinfo.HandshakePacket[0], addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
hostinfo.logger(hm.l).Error("Failed to send handshake message",
|
hostinfo.logger(hm.l).Error("Failed to send handshake message",
|
||||||
"udpAddr", addr,
|
"udpAddr", addr,
|
||||||
"initiatorIndex", hostinfo.localIndexId,
|
"initiatorIndex", hostinfo.localIndexId,
|
||||||
"handshake", hsFields,
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -313,17 +260,156 @@ func (hm *HandshakeManager) handleOutbound(vpnIp netip.Addr, lighthouseTriggered
|
|||||||
hostinfo.logger(hm.l).Info("Handshake message sent",
|
hostinfo.logger(hm.l).Info("Handshake message sent",
|
||||||
"udpAddrs", sentTo,
|
"udpAddrs", sentTo,
|
||||||
"initiatorIndex", hostinfo.localIndexId,
|
"initiatorIndex", hostinfo.localIndexId,
|
||||||
"handshake", hsFields,
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
)
|
)
|
||||||
} else if hm.l.Enabled(context.Background(), slog.LevelDebug) {
|
} else if hm.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
hostinfo.logger(hm.l).Debug("Handshake message sent",
|
hostinfo.logger(hm.l).Debug("Handshake message sent",
|
||||||
"udpAddrs", sentTo,
|
"udpAddrs", sentTo,
|
||||||
"initiatorIndex", hostinfo.localIndexId,
|
"initiatorIndex", hostinfo.localIndexId,
|
||||||
"handshake", hsFields,
|
"handshake", m{"stage": 1, "style": "ix_psk0"},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
hm.f.relayManager.StartRelays(hm.f, vpnIp, hostinfo, stage0)
|
if hm.config.useRelays && len(hostinfo.remotes.relays) > 0 {
|
||||||
|
hostinfo.logger(hm.l).Info("Attempt to relay through hosts", "relays", hostinfo.remotes.relays)
|
||||||
|
// Send a RelayRequest to all known Relay IP's
|
||||||
|
for _, relay := range hostinfo.remotes.relays {
|
||||||
|
// Don't relay through the host I'm trying to connect to
|
||||||
|
if relay == vpnIp {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't relay to myself
|
||||||
|
if hm.f.myVpnAddrsTable.Contains(relay) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
relayHostInfo := hm.mainHostMap.QueryVpnAddr(relay)
|
||||||
|
if relayHostInfo == nil || !relayHostInfo.remote.IsValid() {
|
||||||
|
hostinfo.logger(hm.l).Info("Establish tunnel to relay target", "relay", relay.String())
|
||||||
|
hm.f.Handshake(relay)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Check the relay HostInfo to see if we already established a relay through
|
||||||
|
existingRelay, ok := relayHostInfo.relayState.QueryRelayForByIp(vpnIp)
|
||||||
|
if !ok {
|
||||||
|
// No relays exist or requested yet.
|
||||||
|
if relayHostInfo.remote.IsValid() {
|
||||||
|
idx, err := AddRelay(hm.l, relayHostInfo, hm.mainHostMap, vpnIp, nil, TerminalType, Requested)
|
||||||
|
if err != nil {
|
||||||
|
hostinfo.logger(hm.l).Info("Failed to add relay to hostmap", "relay", relay.String(), "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
m := NebulaControl{
|
||||||
|
Type: NebulaControl_CreateRelayRequest,
|
||||||
|
InitiatorRelayIndex: idx,
|
||||||
|
}
|
||||||
|
|
||||||
|
switch relayHostInfo.GetCert().Certificate.Version() {
|
||||||
|
case cert.Version1:
|
||||||
|
if !hm.f.myVpnAddrs[0].Is4() {
|
||||||
|
hostinfo.logger(hm.l).Error("can not establish v1 relay with a v6 network because the relay is not running a current nebula version")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !vpnIp.Is4() {
|
||||||
|
hostinfo.logger(hm.l).Error("can not establish v1 relay with a v6 remote network because the relay is not running a current nebula version")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
b := hm.f.myVpnAddrs[0].As4()
|
||||||
|
m.OldRelayFromAddr = binary.BigEndian.Uint32(b[:])
|
||||||
|
b = vpnIp.As4()
|
||||||
|
m.OldRelayToAddr = binary.BigEndian.Uint32(b[:])
|
||||||
|
case cert.Version2:
|
||||||
|
m.RelayFromAddr = netAddrToProtoAddr(hm.f.myVpnAddrs[0])
|
||||||
|
m.RelayToAddr = netAddrToProtoAddr(vpnIp)
|
||||||
|
default:
|
||||||
|
hostinfo.logger(hm.l).Error("Unknown certificate version found while creating relay")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, err := m.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
hostinfo.logger(hm.l).Error("Failed to marshal Control message to create relay", "error", err)
|
||||||
|
} else {
|
||||||
|
hm.f.SendMessageToHostInfo(header.Control, 0, relayHostInfo, msg, make([]byte, 12), make([]byte, mtu))
|
||||||
|
hm.l.Info("send CreateRelayRequest",
|
||||||
|
"relayFrom", hm.f.myVpnAddrs[0],
|
||||||
|
"relayTo", vpnIp,
|
||||||
|
"initiatorRelayIndex", idx,
|
||||||
|
"relay", relay,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch existingRelay.State {
|
||||||
|
case Established:
|
||||||
|
hostinfo.logger(hm.l).Info("Send handshake via relay", "relay", relay.String())
|
||||||
|
hm.f.SendVia(relayHostInfo, existingRelay, hostinfo.HandshakePacket[0], make([]byte, 12), make([]byte, mtu), false)
|
||||||
|
case Disestablished:
|
||||||
|
// Mark this relay as 'requested'
|
||||||
|
relayHostInfo.relayState.UpdateRelayForByIpState(vpnIp, Requested)
|
||||||
|
fallthrough
|
||||||
|
case Requested:
|
||||||
|
hostinfo.logger(hm.l).Info("Re-send CreateRelay request", "relay", relay.String())
|
||||||
|
// Re-send the CreateRelay request, in case the previous one was lost.
|
||||||
|
m := NebulaControl{
|
||||||
|
Type: NebulaControl_CreateRelayRequest,
|
||||||
|
InitiatorRelayIndex: existingRelay.LocalIndex,
|
||||||
|
}
|
||||||
|
|
||||||
|
switch relayHostInfo.GetCert().Certificate.Version() {
|
||||||
|
case cert.Version1:
|
||||||
|
if !hm.f.myVpnAddrs[0].Is4() {
|
||||||
|
hostinfo.logger(hm.l).Error("can not establish v1 relay with a v6 network because the relay is not running a current nebula version")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !vpnIp.Is4() {
|
||||||
|
hostinfo.logger(hm.l).Error("can not establish v1 relay with a v6 remote network because the relay is not running a current nebula version")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
b := hm.f.myVpnAddrs[0].As4()
|
||||||
|
m.OldRelayFromAddr = binary.BigEndian.Uint32(b[:])
|
||||||
|
b = vpnIp.As4()
|
||||||
|
m.OldRelayToAddr = binary.BigEndian.Uint32(b[:])
|
||||||
|
case cert.Version2:
|
||||||
|
m.RelayFromAddr = netAddrToProtoAddr(hm.f.myVpnAddrs[0])
|
||||||
|
m.RelayToAddr = netAddrToProtoAddr(vpnIp)
|
||||||
|
default:
|
||||||
|
hostinfo.logger(hm.l).Error("Unknown certificate version found while creating relay")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
msg, err := m.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
hostinfo.logger(hm.l).Error("Failed to marshal Control message to create relay", "error", err)
|
||||||
|
} else {
|
||||||
|
// This must send over the hostinfo, not over hm.Hosts[ip]
|
||||||
|
hm.f.SendMessageToHostInfo(header.Control, 0, relayHostInfo, msg, make([]byte, 12), make([]byte, mtu))
|
||||||
|
hm.l.Info("send CreateRelayRequest",
|
||||||
|
"relayFrom", hm.f.myVpnAddrs[0],
|
||||||
|
"relayTo", vpnIp,
|
||||||
|
"initiatorRelayIndex", existingRelay.LocalIndex,
|
||||||
|
"relay", relay,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
case PeerRequested:
|
||||||
|
// PeerRequested only occurs in Forwarding relays, not Terminal relays, and this is a Terminal relay case.
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
hostinfo.logger(hm.l).Error("Relay unexpected state",
|
||||||
|
"vpnIp", vpnIp,
|
||||||
|
"state", existingRelay.State,
|
||||||
|
"relay", relay,
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If a lighthouse triggered this attempt then we are still in the timer wheel and do not need to re-add
|
// If a lighthouse triggered this attempt then we are still in the timer wheel and do not need to re-add
|
||||||
if !lighthouseTriggered {
|
if !lighthouseTriggered {
|
||||||
@@ -501,7 +587,7 @@ func (hm *HandshakeManager) Complete(hostinfo *HostInfo, f *Interface) {
|
|||||||
// allocateIndex generates a unique localIndexId for this HostInfo
|
// allocateIndex generates a unique localIndexId for this HostInfo
|
||||||
// and adds it to the pendingHostMap. Will error if we are unable to generate
|
// and adds it to the pendingHostMap. Will error if we are unable to generate
|
||||||
// a unique localIndexId
|
// a unique localIndexId
|
||||||
func (hm *HandshakeManager) allocateIndex(hh *HandshakeHostInfo) (uint32, error) {
|
func (hm *HandshakeManager) allocateIndex(hh *HandshakeHostInfo) error {
|
||||||
hm.mainHostMap.RLock()
|
hm.mainHostMap.RLock()
|
||||||
defer hm.mainHostMap.RUnlock()
|
defer hm.mainHostMap.RUnlock()
|
||||||
hm.Lock()
|
hm.Lock()
|
||||||
@@ -510,7 +596,7 @@ func (hm *HandshakeManager) allocateIndex(hh *HandshakeHostInfo) (uint32, error)
|
|||||||
for range 32 {
|
for range 32 {
|
||||||
index, err := generateIndex(hm.l)
|
index, err := generateIndex(hm.l)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, inPending := hm.indexes[index]
|
_, inPending := hm.indexes[index]
|
||||||
@@ -519,11 +605,11 @@ func (hm *HandshakeManager) allocateIndex(hh *HandshakeHostInfo) (uint32, error)
|
|||||||
if !inMain && !inPending {
|
if !inMain && !inPending {
|
||||||
hh.hostinfo.localIndexId = index
|
hh.hostinfo.localIndexId = index
|
||||||
hm.indexes[index] = hh
|
hm.indexes[index] = hh
|
||||||
return index, nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0, errors.New("failed to generate unique localIndexId")
|
return errors.New("failed to generate unique localIndexId")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hm *HandshakeManager) DeleteHostInfo(hostinfo *HostInfo) {
|
func (hm *HandshakeManager) DeleteHostInfo(hostinfo *HostInfo) {
|
||||||
@@ -642,524 +728,3 @@ func generateIndex(l *slog.Logger) (uint32, error) {
|
|||||||
func hsTimeout(tries int64, interval time.Duration) time.Duration {
|
func hsTimeout(tries int64, interval time.Duration) time.Duration {
|
||||||
return time.Duration(tries / 2 * ((2 * int64(interval)) + (tries-1)*int64(interval)))
|
return time.Duration(tries / 2 * ((2 * int64(interval)) + (tries-1)*int64(interval)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildStage0Packet creates the initial handshake packet for the initiator.
|
|
||||||
func (hm *HandshakeManager) buildStage0Packet(hh *HandshakeHostInfo) bool {
|
|
||||||
cs := hm.f.pki.getCertState()
|
|
||||||
v := cs.DefaultVersion()
|
|
||||||
if hh.initiatingVersionOverride != cert.VersionPre1 {
|
|
||||||
v = hh.initiatingVersionOverride
|
|
||||||
} else if v < cert.Version2 {
|
|
||||||
for _, a := range hh.hostinfo.vpnAddrs {
|
|
||||||
if a.Is6() {
|
|
||||||
v = cert.Version2
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cred := cs.GetCredential(v)
|
|
||||||
if cred == nil {
|
|
||||||
hm.f.l.Error("Unable to handshake with host because no certificate is available",
|
|
||||||
"vpnAddrs", hh.hostinfo.vpnAddrs, "certVersion", v)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
machine, err := handshake.NewMachine(
|
|
||||||
v, cs.GetCredential,
|
|
||||||
hm.certVerifier(), func() (uint32, error) { return hm.allocateIndex(hh) },
|
|
||||||
true, header.HandshakeIXPSK0,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
hm.f.l.Error("Failed to create handshake machine",
|
|
||||||
"vpnAddrs", hh.hostinfo.vpnAddrs, "error", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
msg, err := machine.Initiate(nil)
|
|
||||||
if err != nil {
|
|
||||||
hm.f.l.Error("Failed to initiate handshake",
|
|
||||||
"vpnAddrs", hh.hostinfo.vpnAddrs, "error", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// hostinfo.ConnectionState stays nil until the handshake completes in
|
|
||||||
// continueHandshake. Pre-completion control surfaces guard with nil
|
|
||||||
// checks; the data plane never observes a pending hostinfo.
|
|
||||||
hh.hostinfo.HandshakePacket[handshakePacketStage0] = msg
|
|
||||||
hh.machine = machine
|
|
||||||
hh.ready = true
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// beginHandshake handles an incoming handshake packet that doesn't match any
|
|
||||||
// existing pending handshake. It creates a new responder Machine and processes
|
|
||||||
// the first message.
|
|
||||||
func (hm *HandshakeManager) beginHandshake(via ViaSender, packet []byte, h *header.H) {
|
|
||||||
f := hm.f
|
|
||||||
cs := f.pki.getCertState()
|
|
||||||
|
|
||||||
v := cs.DefaultVersion()
|
|
||||||
if cs.GetCredential(v) == nil {
|
|
||||||
f.l.Error("Unable to handshake with host because no certificate is available",
|
|
||||||
"from", via, "certVersion", v)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
machine, err := handshake.NewMachine(
|
|
||||||
v, cs.GetCredential,
|
|
||||||
hm.certVerifier(), func() (uint32, error) { return generateIndex(f.l) },
|
|
||||||
false, header.HandshakeIXPSK0,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
f.l.Error("Failed to create handshake machine", "from", via, "error", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
response, result, err := machine.ProcessPacket(nil, packet)
|
|
||||||
if err != nil {
|
|
||||||
f.l.Error("Failed to process handshake packet", "from", via, "error", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if result == nil {
|
|
||||||
// Multi-message pattern: the responder Machine would need to be
|
|
||||||
// registered in hm.indexes so a future inbound packet finds it via
|
|
||||||
// continueHandshake. The current manager doesn't do that yet, so
|
|
||||||
// fail loudly rather than silently dropping the in-flight handshake.
|
|
||||||
// TODO: support multi-message responder flows (XX, pqIX, etc.).
|
|
||||||
// See also the IX-shaped cipher key assignment in handshake.Machine.
|
|
||||||
f.l.Error("multi-message handshake responder is not supported",
|
|
||||||
"from", via, "error", handshake.ErrMultiMessageUnsupported)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
remoteCert := result.RemoteCert
|
|
||||||
if remoteCert == nil {
|
|
||||||
f.l.Error("Handshake did not produce a peer certificate", "from", via)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate peer identity
|
|
||||||
vpnAddrs, anyVpnAddrsInCommon, ok := hm.validatePeerCert(via, remoteCert)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
hostinfo := &HostInfo{
|
|
||||||
ConnectionState: newConnectionStateFromResult(result),
|
|
||||||
localIndexId: result.LocalIndex,
|
|
||||||
remoteIndexId: result.RemoteIndex,
|
|
||||||
vpnAddrs: vpnAddrs,
|
|
||||||
HandshakePacket: make(map[uint8][]byte, 0),
|
|
||||||
lastHandshakeTime: result.HandshakeTime,
|
|
||||||
relayState: RelayState{
|
|
||||||
relays: nil,
|
|
||||||
relayForByAddr: map[netip.Addr]*Relay{},
|
|
||||||
relayForByIdx: map[uint32]*Relay{},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := "Handshake message received"
|
|
||||||
if !anyVpnAddrsInCommon {
|
|
||||||
msg = "Handshake message received, but no vpnNetworks in common."
|
|
||||||
}
|
|
||||||
f.l.Info(msg,
|
|
||||||
"vpnAddrs", vpnAddrs,
|
|
||||||
"from", via,
|
|
||||||
"certName", remoteCert.Certificate.Name(),
|
|
||||||
"certVersion", remoteCert.Certificate.Version(),
|
|
||||||
"fingerprint", remoteCert.Fingerprint,
|
|
||||||
"issuer", remoteCert.Certificate.Issuer(),
|
|
||||||
"initiatorIndex", result.RemoteIndex,
|
|
||||||
"responderIndex", result.LocalIndex,
|
|
||||||
"handshake", m{"stage": uint64(machine.MessageIndex()), "style": header.SubTypeName(header.Handshake, machine.Subtype())},
|
|
||||||
)
|
|
||||||
|
|
||||||
// packet aliases the listener's incoming buffer, so this copy must stay.
|
|
||||||
hostinfo.HandshakePacket[handshakePacketStage0] = make([]byte, len(packet[header.Len:]))
|
|
||||||
copy(hostinfo.HandshakePacket[handshakePacketStage0], packet[header.Len:])
|
|
||||||
|
|
||||||
// response was freshly allocated by ProcessPacket; safe to retain directly.
|
|
||||||
if response != nil {
|
|
||||||
hostinfo.HandshakePacket[handshakePacketStage2] = response
|
|
||||||
}
|
|
||||||
|
|
||||||
hostinfo.remotes = f.lightHouse.QueryCache(vpnAddrs)
|
|
||||||
if !via.IsRelayed {
|
|
||||||
hostinfo.SetRemote(via.UdpAddr)
|
|
||||||
}
|
|
||||||
hostinfo.buildNetworks(f.myVpnNetworksTable, remoteCert.Certificate)
|
|
||||||
|
|
||||||
existing, err := hm.CheckAndComplete(hostinfo, handshakePacketStage0, f)
|
|
||||||
if err != nil {
|
|
||||||
hm.handleCheckAndCompleteError(err, existing, hostinfo, via)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
hm.sendHandshakeResponse(via, response, hostinfo, false)
|
|
||||||
f.connectionManager.AddTrafficWatch(hostinfo)
|
|
||||||
hostinfo.remotes.RefreshFromHandshake(vpnAddrs)
|
|
||||||
|
|
||||||
// Don't wait for UpdateWorker
|
|
||||||
if f.lightHouse.IsAnyLighthouseAddr(vpnAddrs) {
|
|
||||||
f.lightHouse.TriggerUpdate()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// continueHandshake feeds an incoming packet to an existing pending handshake Machine.
|
|
||||||
func (hm *HandshakeManager) continueHandshake(via ViaSender, hh *HandshakeHostInfo, packet []byte) {
|
|
||||||
f := hm.f
|
|
||||||
|
|
||||||
hh.Lock()
|
|
||||||
defer hh.Unlock()
|
|
||||||
|
|
||||||
// Re-verify hh is still tracked. Between queryIndex returning and us taking
|
|
||||||
// hh.Lock, handleOutbound may have timed out and deleted it. Once we hold
|
|
||||||
// hh.Lock no other deleter can race our index: handleOutbound also takes
|
|
||||||
// hh.Lock first, and handleRecvError targets a main-hostmap entry with a
|
|
||||||
// different localIndexId.
|
|
||||||
hm.RLock()
|
|
||||||
cur, ok := hm.indexes[hh.hostinfo.localIndexId]
|
|
||||||
hm.RUnlock()
|
|
||||||
if !ok || cur != hh {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
hostinfo := hh.hostinfo
|
|
||||||
if !via.IsRelayed {
|
|
||||||
if !f.lightHouse.GetRemoteAllowList().AllowAll(hostinfo.vpnAddrs, via.UdpAddr.Addr()) {
|
|
||||||
f.l.Debug("lighthouse.remote_allow_list denied incoming handshake",
|
|
||||||
"vpnAddrs", hostinfo.vpnAddrs, "from", via)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
machine := hh.machine
|
|
||||||
if machine == nil {
|
|
||||||
f.l.Error("No handshake machine available for continuation",
|
|
||||||
"vpnAddrs", hostinfo.vpnAddrs, "from", via)
|
|
||||||
hm.DeleteHostInfo(hostinfo)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
response, result, err := machine.ProcessPacket(nil, packet)
|
|
||||||
if err != nil {
|
|
||||||
// Recoverable errors are routine noise, log at Debug. Fatal errors get a Warn.
|
|
||||||
if machine.Failed() {
|
|
||||||
f.l.Warn("Failed to process handshake packet, abandoning",
|
|
||||||
"vpnAddrs", hostinfo.vpnAddrs, "from", via, "error", err)
|
|
||||||
hm.DeleteHostInfo(hostinfo)
|
|
||||||
} else {
|
|
||||||
f.l.Debug("Failed to process handshake packet",
|
|
||||||
"vpnAddrs", hostinfo.vpnAddrs, "from", via, "error", err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if response != nil {
|
|
||||||
hm.sendHandshakeResponse(via, response, hostinfo, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
if result == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handshake complete; build the ConnectionState now that we have keys and a verified peer cert.
|
|
||||||
hostinfo.ConnectionState = newConnectionStateFromResult(result)
|
|
||||||
|
|
||||||
remoteCert := result.RemoteCert
|
|
||||||
if remoteCert == nil {
|
|
||||||
f.l.Error("Handshake completed without peer certificate",
|
|
||||||
"vpnAddrs", hostinfo.vpnAddrs, "from", via)
|
|
||||||
hm.DeleteHostInfo(hostinfo)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
vpnNetworks := remoteCert.Certificate.Networks()
|
|
||||||
hostinfo.remoteIndexId = result.RemoteIndex
|
|
||||||
hostinfo.lastHandshakeTime = result.HandshakeTime
|
|
||||||
|
|
||||||
if !via.IsRelayed {
|
|
||||||
hostinfo.SetRemote(via.UdpAddr)
|
|
||||||
} else {
|
|
||||||
hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify correct host responded (initiator check)
|
|
||||||
vpnAddrs := make([]netip.Addr, len(vpnNetworks))
|
|
||||||
correctHostResponded := false
|
|
||||||
anyVpnAddrsInCommon := false
|
|
||||||
for i, network := range vpnNetworks {
|
|
||||||
// inside.go drops self-routed packets at the firewall stage, but we'd
|
|
||||||
// rather not let a self-handshake complete in the first place: it
|
|
||||||
// wastes a hostmap slot, suppresses no log, and obscures routing
|
|
||||||
// misconfig. Explicit refusal here mirrors the responder-side check
|
|
||||||
// in validatePeerCert.
|
|
||||||
if f.myVpnAddrsTable.Contains(network.Addr()) {
|
|
||||||
f.l.Error("Refusing to handshake with myself",
|
|
||||||
"vpnNetworks", vpnNetworks,
|
|
||||||
"from", via,
|
|
||||||
"certName", remoteCert.Certificate.Name(),
|
|
||||||
"certVersion", remoteCert.Certificate.Version(),
|
|
||||||
"fingerprint", remoteCert.Fingerprint,
|
|
||||||
"issuer", remoteCert.Certificate.Issuer(),
|
|
||||||
"handshake", m{"stage": uint64(machine.MessageIndex()), "style": header.SubTypeName(header.Handshake, machine.Subtype())},
|
|
||||||
)
|
|
||||||
hm.DeleteHostInfo(hostinfo)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
vpnAddrs[i] = network.Addr()
|
|
||||||
if hostinfo.vpnAddrs[0] == network.Addr() {
|
|
||||||
correctHostResponded = true
|
|
||||||
}
|
|
||||||
if f.myVpnNetworksTable.Contains(network.Addr()) {
|
|
||||||
anyVpnAddrsInCommon = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !correctHostResponded {
|
|
||||||
f.l.Info("Incorrect host responded to handshake",
|
|
||||||
"intendedVpnAddrs", hostinfo.vpnAddrs,
|
|
||||||
"haveVpnNetworks", vpnNetworks,
|
|
||||||
"from", via,
|
|
||||||
"certName", remoteCert.Certificate.Name(),
|
|
||||||
"certVersion", remoteCert.Certificate.Version(),
|
|
||||||
"fingerprint", remoteCert.Fingerprint,
|
|
||||||
"issuer", remoteCert.Certificate.Issuer(),
|
|
||||||
"handshake", m{"stage": uint64(machine.MessageIndex()), "style": header.SubTypeName(header.Handshake, machine.Subtype())},
|
|
||||||
)
|
|
||||||
|
|
||||||
hm.DeleteHostInfo(hostinfo)
|
|
||||||
hm.StartHandshake(hostinfo.vpnAddrs[0], func(newHH *HandshakeHostInfo) {
|
|
||||||
newHH.hostinfo.remotes = hostinfo.remotes
|
|
||||||
newHH.hostinfo.remotes.BlockRemote(via)
|
|
||||||
newHH.packetStore = hh.packetStore
|
|
||||||
hh.packetStore = []*cachedPacket{}
|
|
||||||
hostinfo.vpnAddrs = vpnAddrs
|
|
||||||
f.sendCloseTunnel(hostinfo)
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
duration := time.Since(hh.startTime).Nanoseconds()
|
|
||||||
msg := "Handshake message received"
|
|
||||||
if !anyVpnAddrsInCommon {
|
|
||||||
msg = "Handshake message received, but no vpnNetworks in common."
|
|
||||||
}
|
|
||||||
f.l.Info(msg,
|
|
||||||
"vpnAddrs", vpnAddrs,
|
|
||||||
"from", via,
|
|
||||||
"certName", remoteCert.Certificate.Name(),
|
|
||||||
"certVersion", remoteCert.Certificate.Version(),
|
|
||||||
"fingerprint", remoteCert.Fingerprint,
|
|
||||||
"issuer", remoteCert.Certificate.Issuer(),
|
|
||||||
"initiatorIndex", result.LocalIndex,
|
|
||||||
"responderIndex", result.RemoteIndex,
|
|
||||||
"handshake", m{"stage": uint64(machine.MessageIndex()), "style": header.SubTypeName(header.Handshake, machine.Subtype())},
|
|
||||||
"durationNs", duration,
|
|
||||||
"sentCachedPackets", len(hh.packetStore),
|
|
||||||
)
|
|
||||||
|
|
||||||
hostinfo.vpnAddrs = vpnAddrs
|
|
||||||
hostinfo.buildNetworks(f.myVpnNetworksTable, remoteCert.Certificate)
|
|
||||||
|
|
||||||
hm.Complete(hostinfo, f)
|
|
||||||
f.connectionManager.AddTrafficWatch(hostinfo)
|
|
||||||
|
|
||||||
if len(hh.packetStore) > 0 {
|
|
||||||
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
|
||||||
hostinfo.logger(f.l).Debug("Sending stored packets", "count", len(hh.packetStore))
|
|
||||||
}
|
|
||||||
nb := make([]byte, 12, 12)
|
|
||||||
out := make([]byte, mtu)
|
|
||||||
for _, cp := range hh.packetStore {
|
|
||||||
cp.callback(cp.messageType, cp.messageSubType, hostinfo, cp.packet, nb, out)
|
|
||||||
}
|
|
||||||
f.cachedPacketMetrics.sent.Inc(int64(len(hh.packetStore)))
|
|
||||||
}
|
|
||||||
|
|
||||||
hostinfo.remotes.RefreshFromHandshake(vpnAddrs)
|
|
||||||
f.metricHandshakes.Update(duration)
|
|
||||||
|
|
||||||
// Don't wait for UpdateWorker
|
|
||||||
if f.lightHouse.IsAnyLighthouseAddr(vpnAddrs) {
|
|
||||||
f.lightHouse.TriggerUpdate()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// validatePeerCert checks the peer certificate for self-connection and remote allow list.
|
|
||||||
// Returns the VPN addrs, whether any of them fall within one of our own VPN
|
|
||||||
// networks, and true if valid; false if rejected.
|
|
||||||
func (hm *HandshakeManager) validatePeerCert(via ViaSender, remoteCert *cert.CachedCertificate) ([]netip.Addr, bool, bool) {
|
|
||||||
f := hm.f
|
|
||||||
vpnNetworks := remoteCert.Certificate.Networks()
|
|
||||||
|
|
||||||
// The cert package rejects host certs with no networks at parse time, so
|
|
||||||
// reaching this state would mean an invariant was bypassed elsewhere.
|
|
||||||
// Refuse explicitly so downstream code (which indexes vpnAddrs[0]) can't
|
|
||||||
// panic if that invariant ever changes.
|
|
||||||
if len(vpnNetworks) == 0 {
|
|
||||||
f.l.Info("No networks in certificate",
|
|
||||||
"from", via, "cert", remoteCert)
|
|
||||||
return nil, false, false
|
|
||||||
}
|
|
||||||
|
|
||||||
vpnAddrs := make([]netip.Addr, len(vpnNetworks))
|
|
||||||
anyVpnAddrsInCommon := false
|
|
||||||
|
|
||||||
for i, network := range vpnNetworks {
|
|
||||||
if f.myVpnAddrsTable.Contains(network.Addr()) {
|
|
||||||
f.l.Error("Refusing to handshake with myself",
|
|
||||||
"vpnNetworks", vpnNetworks,
|
|
||||||
"from", via,
|
|
||||||
"certName", remoteCert.Certificate.Name(),
|
|
||||||
"certVersion", remoteCert.Certificate.Version(),
|
|
||||||
"fingerprint", remoteCert.Fingerprint,
|
|
||||||
"issuer", remoteCert.Certificate.Issuer(),
|
|
||||||
)
|
|
||||||
return nil, false, false
|
|
||||||
}
|
|
||||||
vpnAddrs[i] = network.Addr()
|
|
||||||
if f.myVpnNetworksTable.Contains(network.Addr()) {
|
|
||||||
anyVpnAddrsInCommon = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !via.IsRelayed {
|
|
||||||
if !f.lightHouse.GetRemoteAllowList().AllowAll(vpnAddrs, via.UdpAddr.Addr()) {
|
|
||||||
f.l.Debug("lighthouse.remote_allow_list denied incoming handshake",
|
|
||||||
"vpnAddrs", vpnAddrs, "from", via)
|
|
||||||
return nil, false, false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return vpnAddrs, anyVpnAddrsInCommon, true
|
|
||||||
}
|
|
||||||
|
|
||||||
// sendHandshakeResponse sends a handshake response via the appropriate transport.
|
|
||||||
// cached is true when msg is a stored response being retransmitted because
|
|
||||||
// the peer's stage-1 retransmit landed (the ErrAlreadySeen path); false on a
|
|
||||||
// fresh response.
|
|
||||||
func (hm *HandshakeManager) sendHandshakeResponse(via ViaSender, msg []byte, hostinfo *HostInfo, cached bool) {
|
|
||||||
if msg == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
f := hm.f
|
|
||||||
f.messageMetrics.Tx(header.Handshake, header.MessageSubType(msg[1]), 1)
|
|
||||||
|
|
||||||
// Common log fields. peerCert may be nil during intermediate
|
|
||||||
// multi-message flows (handshake hasn't completed yet); skip the cert
|
|
||||||
// block if so.
|
|
||||||
logFields := []any{
|
|
||||||
"vpnAddrs", hostinfo.vpnAddrs,
|
|
||||||
"handshake", m{"stage": uint64(2), "style": header.SubTypeName(header.Handshake, header.HandshakeIXPSK0)},
|
|
||||||
"cached", cached,
|
|
||||||
"initiatorIndex", hostinfo.remoteIndexId,
|
|
||||||
"responderIndex", hostinfo.localIndexId,
|
|
||||||
}
|
|
||||||
if peerCert := hostinfo.ConnectionState.peerCert; peerCert != nil {
|
|
||||||
logFields = append(logFields,
|
|
||||||
"certName", peerCert.Certificate.Name(),
|
|
||||||
"certVersion", peerCert.Certificate.Version(),
|
|
||||||
"fingerprint", peerCert.Fingerprint,
|
|
||||||
"issuer", peerCert.Certificate.Issuer(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !via.IsRelayed {
|
|
||||||
fields := append(logFields, "from", via)
|
|
||||||
err := f.outside.WriteTo(msg, via.UdpAddr)
|
|
||||||
if err != nil {
|
|
||||||
f.l.Error("Failed to send handshake message", append(fields, "error", err)...)
|
|
||||||
} else {
|
|
||||||
f.l.Info("Handshake message sent", fields...)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if via.relay == nil {
|
|
||||||
f.l.Error("Handshake send failed: both addr and via.relay are nil.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
|
|
||||||
// We received a valid handshake on this relay, so make sure the relay
|
|
||||||
// state reflects that, in case it had been marked Disestablished.
|
|
||||||
via.relayHI.relayState.UpdateRelayForByIdxState(via.remoteIdx, Established)
|
|
||||||
f.SendVia(via.relayHI, via.relay, msg, make([]byte, 12), make([]byte, mtu), false)
|
|
||||||
f.l.Info("Handshake message sent", append(logFields, "relay", via.relayHI.vpnAddrs[0])...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleCheckAndCompleteError handles errors from CheckAndComplete.
|
|
||||||
// This only fires from the responder-side beginHandshake path, after the
|
|
||||||
// peer cert has been validated and ConnectionState populated, so peerCert
|
|
||||||
// is always non-nil for the cases that log it.
|
|
||||||
func (hm *HandshakeManager) handleCheckAndCompleteError(err error, existing, hostinfo *HostInfo, via ViaSender) {
|
|
||||||
f := hm.f
|
|
||||||
peerCert := hostinfo.ConnectionState.peerCert
|
|
||||||
hsFields := m{"stage": uint64(1), "style": header.SubTypeName(header.Handshake, header.HandshakeIXPSK0)}
|
|
||||||
|
|
||||||
switch err {
|
|
||||||
case ErrAlreadySeen:
|
|
||||||
if existing.SetRemoteIfPreferred(f.hostMap, via) {
|
|
||||||
f.SendMessageToVpnAddr(header.Test, header.TestRequest, hostinfo.vpnAddrs[0], []byte(""), make([]byte, 12, 12), make([]byte, mtu))
|
|
||||||
}
|
|
||||||
// Resend the original response. The peer is committed to that response's
|
|
||||||
// ephemeral keys; a freshly-built one would have different keys and break
|
|
||||||
// the tunnel even though both sides "completed" the handshake.
|
|
||||||
if msg := existing.HandshakePacket[handshakePacketStage2]; msg != nil {
|
|
||||||
hm.sendHandshakeResponse(via, msg, existing, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
case ErrExistingHostInfo:
|
|
||||||
f.l.Info("Handshake too old",
|
|
||||||
"vpnAddrs", hostinfo.vpnAddrs,
|
|
||||||
"from", via,
|
|
||||||
"certName", peerCert.Certificate.Name(),
|
|
||||||
"certVersion", peerCert.Certificate.Version(),
|
|
||||||
"fingerprint", peerCert.Fingerprint,
|
|
||||||
"issuer", peerCert.Certificate.Issuer(),
|
|
||||||
"oldHandshakeTime", existing.lastHandshakeTime,
|
|
||||||
"newHandshakeTime", hostinfo.lastHandshakeTime,
|
|
||||||
"initiatorIndex", hostinfo.remoteIndexId,
|
|
||||||
"responderIndex", hostinfo.localIndexId,
|
|
||||||
"handshake", hsFields,
|
|
||||||
)
|
|
||||||
f.SendMessageToVpnAddr(header.Test, header.TestRequest, hostinfo.vpnAddrs[0], []byte(""), make([]byte, 12, 12), make([]byte, mtu))
|
|
||||||
|
|
||||||
case ErrLocalIndexCollision:
|
|
||||||
f.l.Error("Failed to add HostInfo due to localIndex collision",
|
|
||||||
"vpnAddrs", hostinfo.vpnAddrs,
|
|
||||||
"from", via,
|
|
||||||
"certName", peerCert.Certificate.Name(),
|
|
||||||
"certVersion", peerCert.Certificate.Version(),
|
|
||||||
"fingerprint", peerCert.Fingerprint,
|
|
||||||
"issuer", peerCert.Certificate.Issuer(),
|
|
||||||
"localIndex", hostinfo.localIndexId,
|
|
||||||
"initiatorIndex", hostinfo.remoteIndexId,
|
|
||||||
"responderIndex", hostinfo.localIndexId,
|
|
||||||
"handshake", hsFields,
|
|
||||||
)
|
|
||||||
|
|
||||||
default:
|
|
||||||
f.l.Error("Failed to add HostInfo to HostMap",
|
|
||||||
"vpnAddrs", hostinfo.vpnAddrs,
|
|
||||||
"from", via,
|
|
||||||
"error", err,
|
|
||||||
"certName", peerCert.Certificate.Name(),
|
|
||||||
"certVersion", peerCert.Certificate.Version(),
|
|
||||||
"fingerprint", peerCert.Fingerprint,
|
|
||||||
"issuer", peerCert.Certificate.Issuer(),
|
|
||||||
"initiatorIndex", hostinfo.remoteIndexId,
|
|
||||||
"responderIndex", hostinfo.localIndexId,
|
|
||||||
"handshake", hsFields,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// certVerifier returns a CertVerifier that validates certs against the current CA pool.
|
|
||||||
func (hm *HandshakeManager) certVerifier() handshake.CertVerifier {
|
|
||||||
return func(c cert.Certificate) (*cert.CachedCertificate, error) {
|
|
||||||
return hm.f.pki.GetCAPool().VerifyCertificate(time.Now(), c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+1
-136
@@ -5,7 +5,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gaissmai/bart"
|
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
"github.com/slackhq/nebula/test"
|
"github.com/slackhq/nebula/test"
|
||||||
@@ -28,7 +27,7 @@ func Test_NewHandshakeManagerVpnIp(t *testing.T) {
|
|||||||
initiatingVersion: cert.Version1,
|
initiatingVersion: cert.Version1,
|
||||||
privateKey: []byte{},
|
privateKey: []byte{},
|
||||||
v1Cert: &dummyCert{version: cert.Version1},
|
v1Cert: &dummyCert{version: cert.Version1},
|
||||||
v1Credential: nil,
|
v1HandshakeBytes: []byte{},
|
||||||
}
|
}
|
||||||
|
|
||||||
blah := NewHandshakeManager(l, mainHM, lh, &udp.NoopConn{}, defaultHandshakeConfig)
|
blah := NewHandshakeManager(l, mainHM, lh, &udp.NoopConn{}, defaultHandshakeConfig)
|
||||||
@@ -101,137 +100,3 @@ func (mw *mockEncWriter) GetHostInfo(_ netip.Addr) *HostInfo {
|
|||||||
func (mw *mockEncWriter) GetCertState() *CertState {
|
func (mw *mockEncWriter) GetCertState() *CertState {
|
||||||
return &CertState{initiatingVersion: cert.Version2}
|
return &CertState{initiatingVersion: cert.Version2}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidatePeerCert(t *testing.T) {
|
|
||||||
l := test.NewLogger()
|
|
||||||
|
|
||||||
myNetwork := netip.MustParsePrefix("10.0.0.1/24")
|
|
||||||
myAddrTable := new(bart.Lite)
|
|
||||||
myAddrTable.Insert(netip.PrefixFrom(myNetwork.Addr(), myNetwork.Addr().BitLen()))
|
|
||||||
myNetTable := new(bart.Lite)
|
|
||||||
myNetTable.Insert(myNetwork.Masked())
|
|
||||||
|
|
||||||
newHM := func() *HandshakeManager {
|
|
||||||
hm := NewHandshakeManager(l, newHostMap(l), newTestLighthouse(), &udp.NoopConn{}, defaultHandshakeConfig)
|
|
||||||
hm.f = &Interface{
|
|
||||||
handshakeManager: hm,
|
|
||||||
pki: &PKI{},
|
|
||||||
l: l,
|
|
||||||
myVpnAddrsTable: myAddrTable,
|
|
||||||
myVpnNetworksTable: myNetTable,
|
|
||||||
lightHouse: hm.lightHouse,
|
|
||||||
}
|
|
||||||
return hm
|
|
||||||
}
|
|
||||||
|
|
||||||
cached := func(networks ...netip.Prefix) *cert.CachedCertificate {
|
|
||||||
return &cert.CachedCertificate{
|
|
||||||
Certificate: &dummyCert{name: "peer", networks: networks},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
via := ViaSender{
|
|
||||||
UdpAddr: netip.MustParseAddrPort("198.51.100.7:4242"),
|
|
||||||
IsRelayed: true, // skip the remote allow list (covered separately)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Run("addr inside our networks sets anyVpnAddrsInCommon", func(t *testing.T) {
|
|
||||||
hm := newHM()
|
|
||||||
// 10.0.0.2 falls inside our 10.0.0.0/24
|
|
||||||
addrs, common, ok := hm.validatePeerCert(via, cached(netip.MustParsePrefix("10.0.0.2/24")))
|
|
||||||
assert.True(t, ok)
|
|
||||||
assert.True(t, common)
|
|
||||||
assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.0.0.2")}, addrs)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("addr outside our networks leaves anyVpnAddrsInCommon false", func(t *testing.T) {
|
|
||||||
hm := newHM()
|
|
||||||
addrs, common, ok := hm.validatePeerCert(via, cached(netip.MustParsePrefix("192.168.1.5/24")))
|
|
||||||
assert.True(t, ok)
|
|
||||||
assert.False(t, common)
|
|
||||||
assert.Equal(t, []netip.Addr{netip.MustParseAddr("192.168.1.5")}, addrs)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("any matching network is enough", func(t *testing.T) {
|
|
||||||
hm := newHM()
|
|
||||||
addrs, common, ok := hm.validatePeerCert(via, cached(
|
|
||||||
netip.MustParsePrefix("192.168.1.5/24"),
|
|
||||||
netip.MustParsePrefix("10.0.0.42/24"),
|
|
||||||
))
|
|
||||||
assert.True(t, ok)
|
|
||||||
assert.True(t, common)
|
|
||||||
assert.Len(t, addrs, 2)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("self-handshake is rejected", func(t *testing.T) {
|
|
||||||
hm := newHM()
|
|
||||||
// 10.0.0.1 is in myVpnAddrsTable
|
|
||||||
addrs, common, ok := hm.validatePeerCert(via, cached(netip.MustParsePrefix("10.0.0.1/24")))
|
|
||||||
assert.False(t, ok)
|
|
||||||
assert.False(t, common)
|
|
||||||
assert.Nil(t, addrs)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("cert with no networks is rejected", func(t *testing.T) {
|
|
||||||
hm := newHM()
|
|
||||||
addrs, common, ok := hm.validatePeerCert(via, cached())
|
|
||||||
assert.False(t, ok)
|
|
||||||
assert.False(t, common)
|
|
||||||
assert.Nil(t, addrs)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleIncomingDispatch(t *testing.T) {
|
|
||||||
l := test.NewLogger()
|
|
||||||
|
|
||||||
newHM := func() *HandshakeManager {
|
|
||||||
hm := NewHandshakeManager(l, newHostMap(l), newTestLighthouse(), &udp.NoopConn{}, defaultHandshakeConfig)
|
|
||||||
hm.f = &Interface{
|
|
||||||
handshakeManager: hm,
|
|
||||||
pki: &PKI{},
|
|
||||||
l: l,
|
|
||||||
}
|
|
||||||
return hm
|
|
||||||
}
|
|
||||||
|
|
||||||
via := ViaSender{
|
|
||||||
UdpAddr: netip.MustParseAddrPort("198.51.100.7:4242"),
|
|
||||||
IsRelayed: true, // bypass remote allow list
|
|
||||||
}
|
|
||||||
|
|
||||||
// A packet body of zero length is fine for these tests: dispatch is
|
|
||||||
// gated on header fields, and we assert that we never reach noise/cert
|
|
||||||
// processing for any of the malformed shapes here.
|
|
||||||
pkt := make([]byte, header.Len)
|
|
||||||
|
|
||||||
t.Run("unsupported subtype dropped", func(t *testing.T) {
|
|
||||||
hm := newHM()
|
|
||||||
h := &header.H{Type: header.Handshake, Subtype: header.MessageSubType(99), MessageCounter: 1}
|
|
||||||
hm.HandleIncoming(via, pkt, h)
|
|
||||||
assert.Empty(t, hm.indexes, "no pending handshake should be created")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("stage-1 with non-zero RemoteIndex dropped", func(t *testing.T) {
|
|
||||||
hm := newHM()
|
|
||||||
h := &header.H{
|
|
||||||
Type: header.Handshake,
|
|
||||||
Subtype: header.HandshakeIXPSK0,
|
|
||||||
RemoteIndex: 0xdeadbeef,
|
|
||||||
MessageCounter: 1,
|
|
||||||
}
|
|
||||||
hm.HandleIncoming(via, pkt, h)
|
|
||||||
assert.Empty(t, hm.indexes, "spoofed stage-1 must not create a pending machine")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("continuation with no matching pending index dropped", func(t *testing.T) {
|
|
||||||
hm := newHM()
|
|
||||||
h := &header.H{
|
|
||||||
Type: header.Handshake,
|
|
||||||
Subtype: header.HandshakeIXPSK0,
|
|
||||||
RemoteIndex: 0xcafef00d,
|
|
||||||
MessageCounter: 2,
|
|
||||||
}
|
|
||||||
hm.HandleIncoming(via, pkt, h)
|
|
||||||
assert.Empty(t, hm.indexes, "orphan stage-2 must not create state")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -57,8 +57,6 @@ const (
|
|||||||
const (
|
const (
|
||||||
TestRequest MessageSubType = 0
|
TestRequest MessageSubType = 0
|
||||||
TestReply MessageSubType = 1
|
TestReply MessageSubType = 1
|
||||||
MTUDProbeRequest MessageSubType = 2
|
|
||||||
MTUDProbeReply MessageSubType = 3
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -71,8 +69,6 @@ var ErrHeaderTooShort = errors.New("header is too short")
|
|||||||
var subTypeTestMap = map[MessageSubType]string{
|
var subTypeTestMap = map[MessageSubType]string{
|
||||||
TestRequest: "testRequest",
|
TestRequest: "testRequest",
|
||||||
TestReply: "testReply",
|
TestReply: "testReply",
|
||||||
MTUDProbeRequest: "mtudProbeRequest",
|
|
||||||
MTUDProbeReply: "mtudProbeReply",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var subTypeNoneMap = map[MessageSubType]string{0: "none"}
|
var subTypeNoneMap = map[MessageSubType]string{0: "none"}
|
||||||
|
|||||||
@@ -9,10 +9,11 @@ import (
|
|||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
"github.com/slackhq/nebula/iputil"
|
"github.com/slackhq/nebula/iputil"
|
||||||
"github.com/slackhq/nebula/noiseutil"
|
"github.com/slackhq/nebula/noiseutil"
|
||||||
|
"github.com/slackhq/nebula/overlay/batch"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *firewall.Packet, nb, out []byte, q int, localCache firewall.ConntrackCache) {
|
func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *firewall.Packet, nb []byte, sendBatch batch.TxBatcher, rejectBuf []byte, q int, localCache firewall.ConntrackCache) {
|
||||||
err := newPacket(packet, false, fwPacket)
|
err := newPacket(packet, false, fwPacket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
@@ -57,7 +58,7 @@ func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *firewall.Packet
|
|||||||
})
|
})
|
||||||
|
|
||||||
if hostinfo == nil {
|
if hostinfo == nil {
|
||||||
f.rejectInside(packet, out, q)
|
f.rejectInside(packet, rejectBuf, q)
|
||||||
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
f.l.Debug("dropping outbound packet, vpnAddr not in our vpn networks or in unsafe networks",
|
f.l.Debug("dropping outbound packet, vpnAddr not in our vpn networks or in unsafe networks",
|
||||||
"vpnAddr", fwPacket.RemoteAddr,
|
"vpnAddr", fwPacket.RemoteAddr,
|
||||||
@@ -73,10 +74,9 @@ func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *firewall.Packet
|
|||||||
|
|
||||||
dropReason := f.firewall.Drop(*fwPacket, false, hostinfo, f.pki.GetCAPool(), localCache)
|
dropReason := f.firewall.Drop(*fwPacket, false, hostinfo, f.pki.GetCAPool(), localCache)
|
||||||
if dropReason == nil {
|
if dropReason == nil {
|
||||||
f.sendNoMetrics(header.Message, 0, hostinfo.ConnectionState, hostinfo, netip.AddrPort{}, packet, nb, out, q)
|
f.sendInsideMessage(hostinfo, packet, nb, sendBatch, rejectBuf, q)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
f.rejectInside(packet, out, 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",
|
||||||
"fwPacket", fwPacket,
|
"fwPacket", fwPacket,
|
||||||
@@ -86,6 +86,67 @@ func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *firewall.Packet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sendInsideMessage encrypts a firewall-approved inside packet into the
|
||||||
|
// caller's batch slot for later sendmmsg flush. When hostinfo.remote is not
|
||||||
|
// valid we fall through to the relay slow path via the unbatched sendNoMetrics
|
||||||
|
// so relay behavior is unchanged.
|
||||||
|
func (f *Interface) sendInsideMessage(hostinfo *HostInfo, p, nb []byte, sendBatch batch.TxBatcher, rejectBuf []byte, q int) {
|
||||||
|
ci := hostinfo.ConnectionState
|
||||||
|
if ci.eKey == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !hostinfo.remote.IsValid() {
|
||||||
|
// Slow path: relay fallback. Reuse rejectBuf as the ciphertext
|
||||||
|
// scratch; sendNoMetrics arranges header space for SendVia.
|
||||||
|
f.sendNoMetrics(header.Message, 0, ci, hostinfo, netip.AddrPort{}, p, nb, rejectBuf, q)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
scratch := sendBatch.Next()
|
||||||
|
if scratch == nil {
|
||||||
|
// Batch full: bypass batching and send this packet directly so we
|
||||||
|
// never drop traffic on over-subscribed iterations.
|
||||||
|
f.sendNoMetrics(header.Message, 0, ci, hostinfo, netip.AddrPort{}, p, nb, rejectBuf, q)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if noiseutil.EncryptLockNeeded {
|
||||||
|
ci.writeLock.Lock()
|
||||||
|
}
|
||||||
|
c := ci.messageCounter.Add(1)
|
||||||
|
|
||||||
|
out := header.Encode(scratch, header.Version, header.Message, 0, hostinfo.remoteIndexId, c)
|
||||||
|
f.connectionManager.Out(hostinfo)
|
||||||
|
|
||||||
|
if hostinfo.lastRebindCount != f.rebindCount {
|
||||||
|
//NOTE: there is an update hole if a tunnel isn't used and exactly 256 rebinds occur before the tunnel is
|
||||||
|
// finally used again. This tunnel would eventually be torn down and recreated if this action didn't help.
|
||||||
|
f.lightHouse.QueryServer(hostinfo.vpnAddrs[0])
|
||||||
|
hostinfo.lastRebindCount = f.rebindCount
|
||||||
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
|
hostinfo.logger(f.l).Debug("Lighthouse update triggered for punch due to rebind counter",
|
||||||
|
"vpnAddrs", hostinfo.vpnAddrs,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := ci.eKey.EncryptDanger(out, out, p, c, nb)
|
||||||
|
if noiseutil.EncryptLockNeeded {
|
||||||
|
ci.writeLock.Unlock()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
hostinfo.logger(f.l).Error("Failed to encrypt outgoing packet",
|
||||||
|
"error", err,
|
||||||
|
"udpAddr", hostinfo.remote,
|
||||||
|
"counter", c,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sendBatch.Commit(len(out), hostinfo.remote)
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Interface) rejectInside(packet []byte, out []byte, q int) {
|
func (f *Interface) rejectInside(packet []byte, out []byte, q int) {
|
||||||
if !f.firewall.InSendReject {
|
if !f.firewall.InSendReject {
|
||||||
return
|
return
|
||||||
|
|||||||
+49
-19
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -18,6 +17,8 @@ import (
|
|||||||
"github.com/slackhq/nebula/firewall"
|
"github.com/slackhq/nebula/firewall"
|
||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
"github.com/slackhq/nebula/overlay"
|
"github.com/slackhq/nebula/overlay"
|
||||||
|
"github.com/slackhq/nebula/overlay/batch"
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/udp"
|
"github.com/slackhq/nebula/udp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,7 +35,6 @@ type InterfaceConfig struct {
|
|||||||
HandshakeManager *HandshakeManager
|
HandshakeManager *HandshakeManager
|
||||||
lightHouse *LightHouse
|
lightHouse *LightHouse
|
||||||
connectionManager *connectionManager
|
connectionManager *connectionManager
|
||||||
pmtudManager *pmtudManager
|
|
||||||
DropLocalBroadcast bool
|
DropLocalBroadcast bool
|
||||||
DropMulticast bool
|
DropMulticast bool
|
||||||
routines int
|
routines int
|
||||||
@@ -58,7 +58,6 @@ type Interface struct {
|
|||||||
pki *PKI
|
pki *PKI
|
||||||
firewall *Firewall
|
firewall *Firewall
|
||||||
connectionManager *connectionManager
|
connectionManager *connectionManager
|
||||||
pmtudManager *pmtudManager
|
|
||||||
handshakeManager *HandshakeManager
|
handshakeManager *HandshakeManager
|
||||||
dnsServer *dnsServer
|
dnsServer *dnsServer
|
||||||
createTime time.Time
|
createTime time.Time
|
||||||
@@ -90,7 +89,11 @@ type Interface struct {
|
|||||||
|
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
writers []udp.Conn
|
writers []udp.Conn
|
||||||
readers []io.ReadWriteCloser
|
readers []tio.Queue
|
||||||
|
// batchers is one per tun queue, wrapping readers[i].
|
||||||
|
// decryptToTun sends plaintext into the batch.RxBatcher;
|
||||||
|
// listenOut calls its Flush at the end of each UDP recvmmsg batch.
|
||||||
|
batchers []batch.RxBatcher
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
|
|
||||||
// fatalErr holds the first unexpected reader error that caused shutdown.
|
// fatalErr holds the first unexpected reader error that caused shutdown.
|
||||||
@@ -189,7 +192,8 @@ func NewInterface(ctx context.Context, c *InterfaceConfig) (*Interface, error) {
|
|||||||
routines: c.routines,
|
routines: c.routines,
|
||||||
version: c.version,
|
version: c.version,
|
||||||
writers: make([]udp.Conn, c.routines),
|
writers: make([]udp.Conn, c.routines),
|
||||||
readers: make([]io.ReadWriteCloser, c.routines),
|
readers: make([]tio.Queue, c.routines),
|
||||||
|
batchers: make([]batch.RxBatcher, c.routines),
|
||||||
myVpnNetworks: cs.myVpnNetworks,
|
myVpnNetworks: cs.myVpnNetworks,
|
||||||
myVpnNetworksTable: cs.myVpnNetworksTable,
|
myVpnNetworksTable: cs.myVpnNetworksTable,
|
||||||
myVpnAddrs: cs.myVpnAddrs,
|
myVpnAddrs: cs.myVpnAddrs,
|
||||||
@@ -197,7 +201,6 @@ func NewInterface(ctx context.Context, c *InterfaceConfig) (*Interface, error) {
|
|||||||
myBroadcastAddrsTable: cs.myVpnBroadcastAddrsTable,
|
myBroadcastAddrsTable: cs.myVpnBroadcastAddrsTable,
|
||||||
relayManager: c.relayManager,
|
relayManager: c.relayManager,
|
||||||
connectionManager: c.connectionManager,
|
connectionManager: c.connectionManager,
|
||||||
pmtudManager: c.pmtudManager,
|
|
||||||
conntrackCacheTimeout: c.ConntrackCacheTimeout,
|
conntrackCacheTimeout: c.ConntrackCacheTimeout,
|
||||||
|
|
||||||
metricHandshakes: metrics.GetOrRegisterHistogram("handshakes", nil, metrics.NewExpDecaySample(1028, 0.015)),
|
metricHandshakes: metrics.GetOrRegisterHistogram("handshakes", nil, metrics.NewExpDecaySample(1028, 0.015)),
|
||||||
@@ -215,7 +218,6 @@ func NewInterface(ctx context.Context, c *InterfaceConfig) (*Interface, error) {
|
|||||||
ifce.reQueryWait.Store(int64(c.reQueryWait))
|
ifce.reQueryWait.Store(int64(c.reQueryWait))
|
||||||
|
|
||||||
ifce.connectionManager.intf = ifce
|
ifce.connectionManager.intf = ifce
|
||||||
ifce.pmtudManager.intf = ifce
|
|
||||||
|
|
||||||
return ifce, nil
|
return ifce, nil
|
||||||
}
|
}
|
||||||
@@ -249,15 +251,16 @@ func (f *Interface) activate() error {
|
|||||||
metrics.GetOrRegisterGauge("routines", nil).Update(int64(f.routines))
|
metrics.GetOrRegisterGauge("routines", nil).Update(int64(f.routines))
|
||||||
|
|
||||||
// Prepare n tun queues
|
// Prepare n tun queues
|
||||||
var reader io.ReadWriteCloser = f.inside
|
|
||||||
for i := 0; i < f.routines; i++ {
|
for i := 0; i < f.routines; i++ {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
reader, err = f.inside.NewMultiQueueReader()
|
if err = f.inside.NewMultiQueueReader(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.readers[i] = reader
|
}
|
||||||
|
f.readers = f.inside.Readers()
|
||||||
|
for i := range f.readers {
|
||||||
|
f.batchers[i] = batch.NewTCPCoalescer(f.readers[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
f.wg.Add(1) // for us to wait on Close() to return
|
f.wg.Add(1) // for us to wait on Close() to return
|
||||||
@@ -315,14 +318,24 @@ func (f *Interface) listenOut(i int) {
|
|||||||
|
|
||||||
ctCache := firewall.NewConntrackCacheTicker(f.ctx, f.l, f.conntrackCacheTimeout)
|
ctCache := firewall.NewConntrackCacheTicker(f.ctx, f.l, f.conntrackCacheTimeout)
|
||||||
lhh := f.lightHouse.NewRequestHandler()
|
lhh := f.lightHouse.NewRequestHandler()
|
||||||
plaintext := make([]byte, udp.MTU)
|
|
||||||
h := &header.H{}
|
h := &header.H{}
|
||||||
fwPacket := &firewall.Packet{}
|
fwPacket := &firewall.Packet{}
|
||||||
nb := make([]byte, 12, 12)
|
nb := make([]byte, 12, 12)
|
||||||
|
|
||||||
err := li.ListenOut(func(fromUdpAddr netip.AddrPort, payload []byte) {
|
coalescer := f.batchers[i]
|
||||||
|
|
||||||
|
listener := func(fromUdpAddr netip.AddrPort, payload []byte) {
|
||||||
|
plaintext := f.batchers[i].Reserve(len(payload))
|
||||||
f.readOutsidePackets(ViaSender{UdpAddr: fromUdpAddr}, plaintext[:0], payload, h, fwPacket, lhh, nb, i, ctCache.Get())
|
f.readOutsidePackets(ViaSender{UdpAddr: fromUdpAddr}, plaintext[:0], payload, h, fwPacket, lhh, nb, i, ctCache.Get())
|
||||||
})
|
}
|
||||||
|
|
||||||
|
flusher := func() {
|
||||||
|
if err := coalescer.Flush(); err != nil {
|
||||||
|
f.l.Error("Failed to flush tun coalescer", "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err := li.ListenOut(listener, flusher)
|
||||||
|
|
||||||
if err != nil && !f.closed.Load() {
|
if err != nil && !f.closed.Load() {
|
||||||
f.l.Error("Error while reading inbound packet, closing", "error", err)
|
f.l.Error("Error while reading inbound packet, closing", "error", err)
|
||||||
@@ -332,16 +345,16 @@ func (f *Interface) listenOut(i int) {
|
|||||||
f.l.Debug("underlay reader is done", "reader", i)
|
f.l.Debug("underlay reader is done", "reader", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Interface) listenIn(reader io.ReadWriteCloser, i int) {
|
func (f *Interface) listenIn(reader tio.Queue, i int) {
|
||||||
packet := make([]byte, mtu)
|
rejectBuf := make([]byte, mtu)
|
||||||
out := make([]byte, mtu)
|
sb := batch.NewSendBatch(batch.SendBatchCap, udp.MTU+32)
|
||||||
fwPacket := &firewall.Packet{}
|
fwPacket := &firewall.Packet{}
|
||||||
nb := make([]byte, 12, 12)
|
nb := make([]byte, 12, 12)
|
||||||
|
|
||||||
conntrackCache := firewall.NewConntrackCacheTicker(f.ctx, f.l, f.conntrackCacheTimeout)
|
conntrackCache := firewall.NewConntrackCacheTicker(f.ctx, f.l, f.conntrackCacheTimeout)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
n, err := reader.Read(packet)
|
pkts, err := reader.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !f.closed.Load() {
|
if !f.closed.Load() {
|
||||||
f.l.Error("Error while reading outbound packet, closing", "error", err, "reader", i)
|
f.l.Error("Error while reading outbound packet, closing", "error", err, "reader", i)
|
||||||
@@ -350,12 +363,29 @@ func (f *Interface) listenIn(reader io.ReadWriteCloser, i int) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
f.consumeInsidePacket(packet[:n], fwPacket, nb, out, i, conntrackCache.Get())
|
sb.Reset()
|
||||||
|
for _, pkt := range pkts {
|
||||||
|
if sb.Len() >= sb.Cap() {
|
||||||
|
f.flushBatch(sb, i)
|
||||||
|
sb.Reset()
|
||||||
|
}
|
||||||
|
f.consumeInsidePacket(pkt, fwPacket, nb, sb, rejectBuf, i, conntrackCache.Get())
|
||||||
|
}
|
||||||
|
if sb.Len() > 0 {
|
||||||
|
f.flushBatch(sb, i)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f.l.Debug("overlay reader is done", "reader", i)
|
f.l.Debug("overlay reader is done", "reader", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *Interface) flushBatch(sb batch.TxBatcher, q int) {
|
||||||
|
bufs, dsts := sb.Get()
|
||||||
|
if err := f.writers[q].WriteBatch(bufs, dsts); err != nil {
|
||||||
|
f.l.Error("Failed to write outgoing batch", "error", err, "writer", q)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Interface) RegisterConfigChangeCallbacks(c *config.C) {
|
func (f *Interface) RegisterConfigChangeCallbacks(c *config.C) {
|
||||||
c.RegisterReloadCallback(f.reloadFirewall)
|
c.RegisterReloadCallback(f.reloadFirewall)
|
||||||
c.RegisterReloadCallback(f.reloadSendRecvError)
|
c.RegisterReloadCallback(f.reloadSendRecvError)
|
||||||
|
|||||||
@@ -172,7 +172,6 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
hostMap := NewHostMapFromConfig(l, c)
|
hostMap := NewHostMapFromConfig(l, c)
|
||||||
punchy := NewPunchyFromConfig(l, c)
|
punchy := NewPunchyFromConfig(l, c)
|
||||||
connManager := newConnectionManagerFromConfig(l, c, hostMap, punchy)
|
connManager := newConnectionManagerFromConfig(l, c, hostMap, punchy)
|
||||||
pmtudMgr := newPMTUDManagerFromConfig(l, c, tun)
|
|
||||||
lightHouse, err := NewLightHouseFromConfig(ctx, l, c, pki.getCertState(), udpConns[0], punchy)
|
lightHouse, err := NewLightHouseFromConfig(ctx, l, c, pki.getCertState(), udpConns[0], punchy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, util.ContextualizeIfNeeded("Failed to initialize lighthouse handler", err)
|
return nil, util.ContextualizeIfNeeded("Failed to initialize lighthouse handler", err)
|
||||||
@@ -185,10 +184,14 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
messageMetrics = newMessageMetricsOnlyRecvError()
|
messageMetrics = newMessageMetricsOnlyRecvError()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useRelays := c.GetBool("relay.use_relays", DefaultUseRelays) && !c.GetBool("relay.am_relay", false)
|
||||||
|
|
||||||
handshakeConfig := HandshakeConfig{
|
handshakeConfig := HandshakeConfig{
|
||||||
tryInterval: c.GetDuration("handshakes.try_interval", DefaultHandshakeTryInterval),
|
tryInterval: c.GetDuration("handshakes.try_interval", DefaultHandshakeTryInterval),
|
||||||
retries: int64(c.GetInt("handshakes.retries", DefaultHandshakeRetries)),
|
retries: int64(c.GetInt("handshakes.retries", DefaultHandshakeRetries)),
|
||||||
triggerBuffer: c.GetInt("handshakes.trigger_buffer", DefaultHandshakeTriggerBuffer),
|
triggerBuffer: c.GetInt("handshakes.trigger_buffer", DefaultHandshakeTriggerBuffer),
|
||||||
|
useRelays: useRelays,
|
||||||
|
|
||||||
messageMetrics: messageMetrics,
|
messageMetrics: messageMetrics,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,7 +212,6 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
DnsServer: ds,
|
DnsServer: ds,
|
||||||
HandshakeManager: handshakeManager,
|
HandshakeManager: handshakeManager,
|
||||||
connectionManager: connManager,
|
connectionManager: connManager,
|
||||||
pmtudManager: pmtudMgr,
|
|
||||||
lightHouse: lightHouse,
|
lightHouse: lightHouse,
|
||||||
tryPromoteEvery: c.GetUint32("counters.try_promote", defaultPromoteEvery),
|
tryPromoteEvery: c.GetUint32("counters.try_promote", defaultPromoteEvery),
|
||||||
reQueryEvery: c.GetUint32("counters.requery_every_packets", defaultReQueryEvery),
|
reQueryEvery: c.GetUint32("counters.requery_every_packets", defaultReQueryEvery),
|
||||||
@@ -268,7 +270,6 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
dnsStart: ds.Start,
|
dnsStart: ds.Start,
|
||||||
lighthouseStart: lightHouse.StartUpdateWorker,
|
lighthouseStart: lightHouse.StartUpdateWorker,
|
||||||
connectionManagerStart: connManager.Start,
|
connectionManagerStart: connManager.Start,
|
||||||
pmtudManagerStart: pmtudMgr.Start,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+632
-45
@@ -124,7 +124,7 @@ func (x NebulaControl_MessageType) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (NebulaControl_MessageType) EnumDescriptor() ([]byte, []int) {
|
func (NebulaControl_MessageType) EnumDescriptor() ([]byte, []int) {
|
||||||
return fileDescriptor_2d65afa7693df5ef, []int{6, 0}
|
return fileDescriptor_2d65afa7693df5ef, []int{8, 0}
|
||||||
}
|
}
|
||||||
|
|
||||||
type NebulaMeta struct {
|
type NebulaMeta struct {
|
||||||
@@ -489,6 +489,142 @@ func (m *NebulaPing) GetTime() uint64 {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NebulaHandshake struct {
|
||||||
|
Details *NebulaHandshakeDetails `protobuf:"bytes,1,opt,name=Details,proto3" json:"Details,omitempty"`
|
||||||
|
Hmac []byte `protobuf:"bytes,2,opt,name=Hmac,proto3" json:"Hmac,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshake) Reset() { *m = NebulaHandshake{} }
|
||||||
|
func (m *NebulaHandshake) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*NebulaHandshake) ProtoMessage() {}
|
||||||
|
func (*NebulaHandshake) Descriptor() ([]byte, []int) {
|
||||||
|
return fileDescriptor_2d65afa7693df5ef, []int{6}
|
||||||
|
}
|
||||||
|
func (m *NebulaHandshake) XXX_Unmarshal(b []byte) error {
|
||||||
|
return m.Unmarshal(b)
|
||||||
|
}
|
||||||
|
func (m *NebulaHandshake) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||||
|
if deterministic {
|
||||||
|
return xxx_messageInfo_NebulaHandshake.Marshal(b, m, deterministic)
|
||||||
|
} else {
|
||||||
|
b = b[:cap(b)]
|
||||||
|
n, err := m.MarshalToSizedBuffer(b)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return b[:n], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (m *NebulaHandshake) XXX_Merge(src proto.Message) {
|
||||||
|
xxx_messageInfo_NebulaHandshake.Merge(m, src)
|
||||||
|
}
|
||||||
|
func (m *NebulaHandshake) XXX_Size() int {
|
||||||
|
return m.Size()
|
||||||
|
}
|
||||||
|
func (m *NebulaHandshake) XXX_DiscardUnknown() {
|
||||||
|
xxx_messageInfo_NebulaHandshake.DiscardUnknown(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
var xxx_messageInfo_NebulaHandshake proto.InternalMessageInfo
|
||||||
|
|
||||||
|
func (m *NebulaHandshake) GetDetails() *NebulaHandshakeDetails {
|
||||||
|
if m != nil {
|
||||||
|
return m.Details
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshake) GetHmac() []byte {
|
||||||
|
if m != nil {
|
||||||
|
return m.Hmac
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type NebulaHandshakeDetails struct {
|
||||||
|
Cert []byte `protobuf:"bytes,1,opt,name=Cert,proto3" json:"Cert,omitempty"`
|
||||||
|
InitiatorIndex uint32 `protobuf:"varint,2,opt,name=InitiatorIndex,proto3" json:"InitiatorIndex,omitempty"`
|
||||||
|
ResponderIndex uint32 `protobuf:"varint,3,opt,name=ResponderIndex,proto3" json:"ResponderIndex,omitempty"`
|
||||||
|
Cookie uint64 `protobuf:"varint,4,opt,name=Cookie,proto3" json:"Cookie,omitempty"`
|
||||||
|
Time uint64 `protobuf:"varint,5,opt,name=Time,proto3" json:"Time,omitempty"`
|
||||||
|
CertVersion uint32 `protobuf:"varint,8,opt,name=CertVersion,proto3" json:"CertVersion,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshakeDetails) Reset() { *m = NebulaHandshakeDetails{} }
|
||||||
|
func (m *NebulaHandshakeDetails) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*NebulaHandshakeDetails) ProtoMessage() {}
|
||||||
|
func (*NebulaHandshakeDetails) Descriptor() ([]byte, []int) {
|
||||||
|
return fileDescriptor_2d65afa7693df5ef, []int{7}
|
||||||
|
}
|
||||||
|
func (m *NebulaHandshakeDetails) XXX_Unmarshal(b []byte) error {
|
||||||
|
return m.Unmarshal(b)
|
||||||
|
}
|
||||||
|
func (m *NebulaHandshakeDetails) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||||
|
if deterministic {
|
||||||
|
return xxx_messageInfo_NebulaHandshakeDetails.Marshal(b, m, deterministic)
|
||||||
|
} else {
|
||||||
|
b = b[:cap(b)]
|
||||||
|
n, err := m.MarshalToSizedBuffer(b)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return b[:n], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (m *NebulaHandshakeDetails) XXX_Merge(src proto.Message) {
|
||||||
|
xxx_messageInfo_NebulaHandshakeDetails.Merge(m, src)
|
||||||
|
}
|
||||||
|
func (m *NebulaHandshakeDetails) XXX_Size() int {
|
||||||
|
return m.Size()
|
||||||
|
}
|
||||||
|
func (m *NebulaHandshakeDetails) XXX_DiscardUnknown() {
|
||||||
|
xxx_messageInfo_NebulaHandshakeDetails.DiscardUnknown(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
var xxx_messageInfo_NebulaHandshakeDetails proto.InternalMessageInfo
|
||||||
|
|
||||||
|
func (m *NebulaHandshakeDetails) GetCert() []byte {
|
||||||
|
if m != nil {
|
||||||
|
return m.Cert
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshakeDetails) GetInitiatorIndex() uint32 {
|
||||||
|
if m != nil {
|
||||||
|
return m.InitiatorIndex
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshakeDetails) GetResponderIndex() uint32 {
|
||||||
|
if m != nil {
|
||||||
|
return m.ResponderIndex
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshakeDetails) GetCookie() uint64 {
|
||||||
|
if m != nil {
|
||||||
|
return m.Cookie
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshakeDetails) GetTime() uint64 {
|
||||||
|
if m != nil {
|
||||||
|
return m.Time
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshakeDetails) GetCertVersion() uint32 {
|
||||||
|
if m != nil {
|
||||||
|
return m.CertVersion
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
type NebulaControl struct {
|
type NebulaControl struct {
|
||||||
Type NebulaControl_MessageType `protobuf:"varint,1,opt,name=Type,proto3,enum=nebula.NebulaControl_MessageType" json:"Type,omitempty"`
|
Type NebulaControl_MessageType `protobuf:"varint,1,opt,name=Type,proto3,enum=nebula.NebulaControl_MessageType" json:"Type,omitempty"`
|
||||||
InitiatorRelayIndex uint32 `protobuf:"varint,2,opt,name=InitiatorRelayIndex,proto3" json:"InitiatorRelayIndex,omitempty"`
|
InitiatorRelayIndex uint32 `protobuf:"varint,2,opt,name=InitiatorRelayIndex,proto3" json:"InitiatorRelayIndex,omitempty"`
|
||||||
@@ -503,7 +639,7 @@ func (m *NebulaControl) Reset() { *m = NebulaControl{} }
|
|||||||
func (m *NebulaControl) String() string { return proto.CompactTextString(m) }
|
func (m *NebulaControl) String() string { return proto.CompactTextString(m) }
|
||||||
func (*NebulaControl) ProtoMessage() {}
|
func (*NebulaControl) ProtoMessage() {}
|
||||||
func (*NebulaControl) Descriptor() ([]byte, []int) {
|
func (*NebulaControl) Descriptor() ([]byte, []int) {
|
||||||
return fileDescriptor_2d65afa7693df5ef, []int{6}
|
return fileDescriptor_2d65afa7693df5ef, []int{8}
|
||||||
}
|
}
|
||||||
func (m *NebulaControl) XXX_Unmarshal(b []byte) error {
|
func (m *NebulaControl) XXX_Unmarshal(b []byte) error {
|
||||||
return m.Unmarshal(b)
|
return m.Unmarshal(b)
|
||||||
@@ -593,55 +729,65 @@ func init() {
|
|||||||
proto.RegisterType((*V4AddrPort)(nil), "nebula.V4AddrPort")
|
proto.RegisterType((*V4AddrPort)(nil), "nebula.V4AddrPort")
|
||||||
proto.RegisterType((*V6AddrPort)(nil), "nebula.V6AddrPort")
|
proto.RegisterType((*V6AddrPort)(nil), "nebula.V6AddrPort")
|
||||||
proto.RegisterType((*NebulaPing)(nil), "nebula.NebulaPing")
|
proto.RegisterType((*NebulaPing)(nil), "nebula.NebulaPing")
|
||||||
|
proto.RegisterType((*NebulaHandshake)(nil), "nebula.NebulaHandshake")
|
||||||
|
proto.RegisterType((*NebulaHandshakeDetails)(nil), "nebula.NebulaHandshakeDetails")
|
||||||
proto.RegisterType((*NebulaControl)(nil), "nebula.NebulaControl")
|
proto.RegisterType((*NebulaControl)(nil), "nebula.NebulaControl")
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() { proto.RegisterFile("nebula.proto", fileDescriptor_2d65afa7693df5ef) }
|
func init() { proto.RegisterFile("nebula.proto", fileDescriptor_2d65afa7693df5ef) }
|
||||||
|
|
||||||
var fileDescriptor_2d65afa7693df5ef = []byte{
|
var fileDescriptor_2d65afa7693df5ef = []byte{
|
||||||
// 665 bytes of a gzipped FileDescriptorProto
|
// 785 bytes of a gzipped FileDescriptorProto
|
||||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x54, 0xcd, 0x6e, 0xd3, 0x5c,
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x55, 0xcd, 0x6e, 0xeb, 0x44,
|
||||||
0x10, 0x8d, 0x1d, 0x27, 0x69, 0x27, 0x4d, 0x3e, 0x7f, 0x53, 0x51, 0x12, 0x24, 0xac, 0xe0, 0x45,
|
0x14, 0x8e, 0x1d, 0x27, 0x4e, 0x4f, 0x7e, 0xae, 0x39, 0x15, 0xc1, 0x41, 0x22, 0x0a, 0x5e, 0x54,
|
||||||
0x55, 0xb1, 0x48, 0x51, 0x5a, 0xba, 0xa6, 0x2d, 0x42, 0xa9, 0xd4, 0x9f, 0x70, 0x55, 0x8a, 0xc4,
|
0x57, 0x2c, 0x72, 0x51, 0x5a, 0xae, 0x58, 0x72, 0x1b, 0x84, 0xd2, 0xaa, 0x3f, 0x61, 0x54, 0x8a,
|
||||||
0xce, 0xb5, 0x2f, 0x8d, 0x55, 0xc7, 0x37, 0xb5, 0x6f, 0x50, 0xf3, 0x16, 0x3c, 0x0c, 0x0f, 0x01,
|
0xc4, 0x06, 0xb9, 0xf6, 0xd0, 0x58, 0x71, 0x3c, 0xa9, 0x3d, 0x41, 0xcd, 0x5b, 0xf0, 0x30, 0x3c,
|
||||||
0xbb, 0x2e, 0x59, 0xa2, 0x66, 0xc9, 0x92, 0x17, 0x40, 0xf7, 0xfa, 0xbf, 0x31, 0xb0, 0xbb, 0x33,
|
0x04, 0xec, 0xba, 0x42, 0x2c, 0x51, 0xbb, 0x64, 0xc9, 0x0b, 0xa0, 0x19, 0xff, 0x27, 0x86, 0xbb,
|
||||||
0xe7, 0x9c, 0x99, 0xc9, 0xc9, 0x8c, 0x61, 0xcd, 0xa7, 0x97, 0x33, 0xcf, 0xea, 0x4f, 0x03, 0xc6,
|
0x9b, 0x73, 0xbe, 0xef, 0x3b, 0x73, 0xe6, 0xf3, 0x9c, 0x31, 0x74, 0x02, 0x7a, 0xb7, 0xf1, 0xed,
|
||||||
0x19, 0xd6, 0xa3, 0xc8, 0xfc, 0xa9, 0x02, 0x9c, 0xca, 0xe7, 0x09, 0xe5, 0x16, 0x0e, 0x40, 0x3b,
|
0xf1, 0x3a, 0x64, 0x9c, 0x61, 0x33, 0x8e, 0xac, 0xbf, 0x55, 0x80, 0x2b, 0xb9, 0xbc, 0xa4, 0xdc,
|
||||||
0x9f, 0x4f, 0x69, 0x47, 0xe9, 0x29, 0x5b, 0xed, 0x81, 0xd1, 0x8f, 0x35, 0x19, 0xa3, 0x7f, 0x42,
|
0xc6, 0x09, 0x68, 0x37, 0xdb, 0x35, 0x35, 0x95, 0x91, 0xf2, 0xba, 0x37, 0x19, 0x8e, 0x13, 0x4d,
|
||||||
0xc3, 0xd0, 0xba, 0xa2, 0x82, 0x45, 0x24, 0x17, 0x77, 0xa0, 0xf1, 0x9a, 0x72, 0xcb, 0xf5, 0xc2,
|
0xce, 0x18, 0x5f, 0xd2, 0x28, 0xb2, 0xef, 0xa9, 0x60, 0x11, 0xc9, 0xc5, 0x63, 0xd0, 0xbf, 0xa6,
|
||||||
0x8e, 0xda, 0x53, 0xb6, 0x9a, 0x83, 0xee, 0xb2, 0x2c, 0x26, 0x90, 0x84, 0x69, 0xfe, 0x52, 0xa0,
|
0xdc, 0xf6, 0xfc, 0xc8, 0x54, 0x47, 0xca, 0xeb, 0xf6, 0x64, 0xb0, 0x2f, 0x4b, 0x08, 0x24, 0x65,
|
||||||
0x99, 0x2b, 0x85, 0x2b, 0xa0, 0x9d, 0x32, 0x9f, 0xea, 0x15, 0x6c, 0xc1, 0xea, 0x90, 0x85, 0xfc,
|
0x5a, 0xff, 0x28, 0xd0, 0x2e, 0x94, 0xc2, 0x16, 0x68, 0x57, 0x2c, 0xa0, 0x46, 0x0d, 0xbb, 0x70,
|
||||||
0xed, 0x8c, 0x06, 0x73, 0x5d, 0x41, 0x84, 0x76, 0x1a, 0x12, 0x3a, 0xf5, 0xe6, 0xba, 0x8a, 0x4f,
|
0x30, 0x63, 0x11, 0xff, 0x76, 0x43, 0xc3, 0xad, 0xa1, 0x20, 0x42, 0x2f, 0x0b, 0x09, 0x5d, 0xfb,
|
||||||
0x60, 0x43, 0xe4, 0xde, 0x4d, 0x1d, 0x8b, 0xd3, 0x53, 0xc6, 0xdd, 0x8f, 0xae, 0x6d, 0x71, 0x97,
|
0x5b, 0x43, 0xc5, 0x8f, 0xa1, 0x2f, 0x72, 0xdf, 0xad, 0x5d, 0x9b, 0xd3, 0x2b, 0xc6, 0xbd, 0x9f,
|
||||||
0xf9, 0x7a, 0x15, 0xbb, 0xf0, 0x48, 0x60, 0x27, 0xec, 0x13, 0x75, 0x0a, 0x90, 0x96, 0x40, 0xa3,
|
0x3c, 0xc7, 0xe6, 0x1e, 0x0b, 0x8c, 0x3a, 0x0e, 0xe0, 0x43, 0x81, 0x5d, 0xb2, 0x9f, 0xa9, 0x5b,
|
||||||
0x99, 0x6f, 0x8f, 0x0b, 0x50, 0x0d, 0xdb, 0x00, 0x02, 0x7a, 0x3f, 0x66, 0xd6, 0xc4, 0xd5, 0xeb,
|
0x82, 0xb4, 0x14, 0x9a, 0x6f, 0x02, 0x67, 0x51, 0x82, 0x1a, 0xd8, 0x03, 0x10, 0xd0, 0xf7, 0x0b,
|
||||||
0xb8, 0x0e, 0xff, 0x65, 0x71, 0xd4, 0xb6, 0x21, 0x26, 0x1b, 0x59, 0x7c, 0x7c, 0x38, 0xa6, 0xf6,
|
0x66, 0xaf, 0x3c, 0xa3, 0x89, 0x87, 0xf0, 0x2a, 0x8f, 0xe3, 0x6d, 0x75, 0xd1, 0xd9, 0xdc, 0xe6,
|
||||||
0xb5, 0xbe, 0x22, 0x26, 0x4b, 0xc3, 0x88, 0xb2, 0x8a, 0x4f, 0xa1, 0x5b, 0x3e, 0xd9, 0xbe, 0x7d,
|
0x8b, 0xe9, 0x82, 0x3a, 0x4b, 0xa3, 0x25, 0x3a, 0xcb, 0xc2, 0x98, 0x72, 0x80, 0x9f, 0xc0, 0xa0,
|
||||||
0xad, 0x83, 0xf9, 0x4d, 0x85, 0xff, 0x97, 0x4c, 0x41, 0x13, 0xe0, 0xcc, 0x73, 0x2e, 0xa6, 0xfe,
|
0xba, 0xb3, 0x77, 0xce, 0xd2, 0x00, 0xeb, 0x77, 0x15, 0x3e, 0xd8, 0x33, 0x05, 0x2d, 0x80, 0x6b,
|
||||||
0xbe, 0xe3, 0x04, 0xd2, 0xfa, 0xd6, 0x81, 0xda, 0x51, 0x48, 0x2e, 0x8b, 0x9b, 0xd0, 0x48, 0x08,
|
0xdf, 0xbd, 0x5d, 0x07, 0xef, 0x5c, 0x37, 0x94, 0xd6, 0x77, 0x4f, 0x55, 0x53, 0x21, 0x85, 0x2c,
|
||||||
0x75, 0x69, 0xf2, 0x5a, 0x62, 0xb2, 0xc8, 0x91, 0x04, 0xc4, 0x3e, 0xe8, 0x67, 0x9e, 0x43, 0xa8,
|
0x1e, 0x81, 0x9e, 0x12, 0x9a, 0xd2, 0xe4, 0x4e, 0x6a, 0xb2, 0xc8, 0x91, 0x14, 0xc4, 0x31, 0x18,
|
||||||
0x67, 0xcd, 0xe3, 0x54, 0xd8, 0xa9, 0xf5, 0xaa, 0x71, 0xc5, 0x25, 0x0c, 0x07, 0xd0, 0x2a, 0x92,
|
0xd7, 0xbe, 0x4b, 0xa8, 0x6f, 0x6f, 0x93, 0x54, 0x64, 0x36, 0x46, 0xf5, 0xa4, 0xe2, 0x1e, 0x86,
|
||||||
0x1b, 0xbd, 0xea, 0x52, 0xf5, 0x22, 0x05, 0x77, 0xa1, 0x79, 0xb1, 0x2b, 0x9e, 0x23, 0x16, 0x70,
|
0x13, 0xe8, 0x96, 0xc9, 0xfa, 0xa8, 0xbe, 0x57, 0xbd, 0x4c, 0xc1, 0x13, 0x68, 0xdf, 0x9e, 0x88,
|
||||||
0xf1, 0xa7, 0x0b, 0x05, 0x26, 0x8a, 0x0c, 0x22, 0x79, 0x9a, 0x54, 0xed, 0x65, 0x2a, 0xed, 0x81,
|
0xe5, 0x9c, 0x85, 0x5c, 0x7c, 0x74, 0xa1, 0xc0, 0x54, 0x91, 0x43, 0xa4, 0x48, 0x93, 0xaa, 0xb7,
|
||||||
0x6a, 0x2f, 0xa7, 0xca, 0x68, 0xd8, 0x81, 0x86, 0xcd, 0x66, 0x3e, 0xa7, 0x41, 0xa7, 0x2a, 0x8c,
|
0xb9, 0x4a, 0xdb, 0x51, 0xbd, 0x2d, 0xa8, 0x72, 0x1a, 0x9a, 0xa0, 0x3b, 0x6c, 0x13, 0x70, 0x1a,
|
||||||
0x21, 0x49, 0x68, 0x6e, 0x82, 0x26, 0x7f, 0x71, 0x1b, 0xd4, 0xa1, 0x2b, 0x5d, 0xd3, 0x88, 0x3a,
|
0x9a, 0x75, 0x61, 0x0c, 0x49, 0x43, 0xeb, 0x08, 0x34, 0x79, 0xe2, 0x1e, 0xa8, 0x33, 0x4f, 0xba,
|
||||||
0x74, 0x45, 0x7c, 0xcc, 0xe4, 0x26, 0x6a, 0x44, 0x3d, 0x66, 0xe6, 0x2e, 0x40, 0x36, 0x06, 0x62,
|
0xa6, 0x11, 0x75, 0xe6, 0x89, 0xf8, 0x82, 0xc9, 0x9b, 0xa8, 0x11, 0xf5, 0x82, 0x59, 0x27, 0x00,
|
||||||
0xa4, 0x8a, 0x5c, 0x26, 0x51, 0x05, 0x04, 0x4d, 0x60, 0x52, 0xd3, 0x22, 0xf2, 0x6d, 0xbe, 0x02,
|
0x79, 0x1b, 0x88, 0xb1, 0x2a, 0x76, 0x99, 0xc4, 0x15, 0x10, 0x34, 0x81, 0x49, 0x4d, 0x97, 0xc8,
|
||||||
0xc8, 0xc6, 0xf8, 0x57, 0x8f, 0xb4, 0x42, 0x35, 0x57, 0xe1, 0x36, 0x39, 0xac, 0x91, 0xeb, 0x5f,
|
0xb5, 0xf5, 0x15, 0x40, 0xde, 0xc6, 0xfb, 0xf6, 0xc8, 0x2a, 0xd4, 0x0b, 0x15, 0x1e, 0xd3, 0xc1,
|
||||||
0xfd, 0xfd, 0xb0, 0x04, 0xa3, 0xe4, 0xb0, 0x10, 0xb4, 0x73, 0x77, 0x42, 0xe3, 0x3e, 0xf2, 0x6d,
|
0x9a, 0x7b, 0xc1, 0xfd, 0xff, 0x0f, 0x96, 0x60, 0x54, 0x0c, 0x16, 0x82, 0x76, 0xe3, 0xad, 0x68,
|
||||||
0x9a, 0x4b, 0x67, 0x23, 0xc4, 0x7a, 0x05, 0x57, 0xa1, 0x16, 0x2d, 0xa1, 0x62, 0x7e, 0xa9, 0x42,
|
0xb2, 0x8f, 0x5c, 0x5b, 0xd6, 0xde, 0xd8, 0x08, 0xb1, 0x51, 0xc3, 0x03, 0x68, 0xc4, 0x97, 0x50,
|
||||||
0x2b, 0x2a, 0x7c, 0xc8, 0x7c, 0x1e, 0x30, 0x0f, 0x5f, 0x16, 0xba, 0x3f, 0x2b, 0x76, 0x8f, 0x49,
|
0xb1, 0x7e, 0x84, 0x57, 0x71, 0xdd, 0x99, 0x1d, 0xb8, 0xd1, 0xc2, 0x5e, 0x52, 0xfc, 0x32, 0x9f,
|
||||||
0x25, 0x03, 0xbc, 0x80, 0xf5, 0x23, 0xdf, 0xe5, 0xae, 0xc5, 0x59, 0x20, 0x57, 0xe0, 0xc8, 0x77,
|
0x51, 0x45, 0x5e, 0x9f, 0x9d, 0x0e, 0x32, 0xe6, 0xee, 0xa0, 0x8a, 0x26, 0x66, 0x2b, 0xdb, 0x91,
|
||||||
0xe8, 0x6d, 0xec, 0x53, 0x19, 0x24, 0x14, 0x84, 0x86, 0x53, 0xe6, 0x3b, 0x34, 0xaf, 0x88, 0x7c,
|
0x4d, 0x74, 0x88, 0x5c, 0x5b, 0x7f, 0x28, 0xd0, 0xaf, 0xd6, 0x09, 0xfa, 0x94, 0x86, 0x5c, 0xee,
|
||||||
0x29, 0x83, 0xf0, 0x39, 0xb4, 0x93, 0xa5, 0x3c, 0x67, 0xf2, 0xaf, 0xd1, 0xd2, 0x03, 0x78, 0x80,
|
0xd2, 0x21, 0x72, 0x8d, 0x47, 0xd0, 0x3b, 0x0b, 0x3c, 0xee, 0xd9, 0x9c, 0x85, 0x67, 0x81, 0x4b,
|
||||||
0xe4, 0x97, 0xfb, 0x4d, 0xc0, 0x26, 0x92, 0x5d, 0x4b, 0xd9, 0x4b, 0x18, 0xf6, 0xa1, 0x99, 0x2f,
|
0x1f, 0x13, 0xa7, 0x77, 0xb2, 0x82, 0x47, 0x68, 0xb4, 0x66, 0x81, 0x4b, 0x13, 0x5e, 0xec, 0xe7,
|
||||||
0x5c, 0x76, 0x38, 0x79, 0x42, 0x7a, 0x0c, 0x69, 0xf1, 0x46, 0x89, 0xa2, 0x48, 0x31, 0x87, 0x7f,
|
0x4e, 0x16, 0xfb, 0xd0, 0x9c, 0x32, 0xb6, 0xf4, 0xa8, 0xa9, 0x49, 0x67, 0x92, 0x28, 0xf3, 0xab,
|
||||||
0xfa, 0x8e, 0x6d, 0x00, 0x1e, 0x06, 0xd4, 0xe2, 0x54, 0xf2, 0x09, 0xbd, 0x99, 0xd1, 0x90, 0xeb,
|
0x91, 0xfb, 0x85, 0x23, 0x68, 0x8b, 0x1e, 0x6e, 0x69, 0x18, 0x79, 0x2c, 0x30, 0x5b, 0xb2, 0x60,
|
||||||
0x0a, 0x3e, 0x86, 0xf5, 0x42, 0x5e, 0x58, 0x12, 0x52, 0x5d, 0x3d, 0xd8, 0xf9, 0x7a, 0x6f, 0x28,
|
0x31, 0x75, 0xae, 0xb5, 0x9a, 0x86, 0x7e, 0xae, 0xb5, 0x74, 0xa3, 0x65, 0xfd, 0x5a, 0x87, 0x6e,
|
||||||
0x77, 0xf7, 0x86, 0xf2, 0xe3, 0xde, 0x50, 0x3e, 0x2f, 0x8c, 0xca, 0xdd, 0xc2, 0xa8, 0x7c, 0x5f,
|
0x7c, 0xb0, 0x29, 0x0b, 0x78, 0xc8, 0x7c, 0xfc, 0xa2, 0xf4, 0xdd, 0x3e, 0x2d, 0xbb, 0x96, 0x90,
|
||||||
0x18, 0x95, 0x0f, 0xdd, 0x2b, 0x97, 0x8f, 0x67, 0x97, 0x7d, 0x9b, 0x4d, 0xb6, 0x43, 0xcf, 0xb2,
|
0x2a, 0x3e, 0xdd, 0xe7, 0x70, 0x98, 0x1d, 0x4e, 0x0e, 0x4f, 0xf1, 0xdc, 0x55, 0x90, 0x50, 0x64,
|
||||||
0xaf, 0xc7, 0x37, 0xdb, 0xd1, 0x48, 0x97, 0x75, 0xf9, 0x39, 0xdf, 0xf9, 0x1d, 0x00, 0x00, 0xff,
|
0xc7, 0x2c, 0x28, 0x62, 0x07, 0xaa, 0x20, 0xfc, 0x0c, 0x7a, 0xe9, 0x38, 0xdf, 0x30, 0x79, 0xa9,
|
||||||
0xff, 0x51, 0x0a, 0xe3, 0xd7, 0xde, 0x05, 0x00, 0x00,
|
0xb5, 0xec, 0xe9, 0xd8, 0x41, 0x8a, 0xcf, 0xc2, 0x37, 0x21, 0x5b, 0x49, 0x76, 0x23, 0x63, 0xef,
|
||||||
|
0x61, 0x38, 0x86, 0x76, 0xb1, 0x70, 0xd5, 0x93, 0x53, 0x24, 0x64, 0xcf, 0x48, 0x56, 0x5c, 0xaf,
|
||||||
|
0x50, 0x94, 0x29, 0xd6, 0xec, 0xbf, 0xfe, 0x00, 0x7d, 0xc0, 0x69, 0x48, 0x6d, 0x4e, 0x25, 0x9f,
|
||||||
|
0xd0, 0x87, 0x0d, 0x8d, 0xb8, 0xa1, 0xe0, 0x47, 0x70, 0x58, 0xca, 0x0b, 0x4b, 0x22, 0x6a, 0xa8,
|
||||||
|
0xa7, 0xc7, 0xbf, 0x3d, 0x0f, 0x95, 0xa7, 0xe7, 0xa1, 0xf2, 0xd7, 0xf3, 0x50, 0xf9, 0xe5, 0x65,
|
||||||
|
0x58, 0x7b, 0x7a, 0x19, 0xd6, 0xfe, 0x7c, 0x19, 0xd6, 0x7e, 0x18, 0xdc, 0x7b, 0x7c, 0xb1, 0xb9,
|
||||||
|
0x1b, 0x3b, 0x6c, 0xf5, 0x26, 0xf2, 0x6d, 0x67, 0xb9, 0x78, 0x78, 0x13, 0xb7, 0x74, 0xd7, 0x94,
|
||||||
|
0x3f, 0xc2, 0xe3, 0x7f, 0x03, 0x00, 0x00, 0xff, 0xff, 0xea, 0x6f, 0xbc, 0x50, 0x18, 0x07, 0x00,
|
||||||
|
0x00,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *NebulaMeta) Marshal() (dAtA []byte, err error) {
|
func (m *NebulaMeta) Marshal() (dAtA []byte, err error) {
|
||||||
@@ -926,6 +1072,103 @@ func (m *NebulaPing) MarshalToSizedBuffer(dAtA []byte) (int, error) {
|
|||||||
return len(dAtA) - i, nil
|
return len(dAtA) - i, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshake) Marshal() (dAtA []byte, err error) {
|
||||||
|
size := m.Size()
|
||||||
|
dAtA = make([]byte, size)
|
||||||
|
n, err := m.MarshalToSizedBuffer(dAtA[:size])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return dAtA[:n], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshake) MarshalTo(dAtA []byte) (int, error) {
|
||||||
|
size := m.Size()
|
||||||
|
return m.MarshalToSizedBuffer(dAtA[:size])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshake) MarshalToSizedBuffer(dAtA []byte) (int, error) {
|
||||||
|
i := len(dAtA)
|
||||||
|
_ = i
|
||||||
|
var l int
|
||||||
|
_ = l
|
||||||
|
if len(m.Hmac) > 0 {
|
||||||
|
i -= len(m.Hmac)
|
||||||
|
copy(dAtA[i:], m.Hmac)
|
||||||
|
i = encodeVarintNebula(dAtA, i, uint64(len(m.Hmac)))
|
||||||
|
i--
|
||||||
|
dAtA[i] = 0x12
|
||||||
|
}
|
||||||
|
if m.Details != nil {
|
||||||
|
{
|
||||||
|
size, err := m.Details.MarshalToSizedBuffer(dAtA[:i])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
i -= size
|
||||||
|
i = encodeVarintNebula(dAtA, i, uint64(size))
|
||||||
|
}
|
||||||
|
i--
|
||||||
|
dAtA[i] = 0xa
|
||||||
|
}
|
||||||
|
return len(dAtA) - i, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshakeDetails) Marshal() (dAtA []byte, err error) {
|
||||||
|
size := m.Size()
|
||||||
|
dAtA = make([]byte, size)
|
||||||
|
n, err := m.MarshalToSizedBuffer(dAtA[:size])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return dAtA[:n], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshakeDetails) MarshalTo(dAtA []byte) (int, error) {
|
||||||
|
size := m.Size()
|
||||||
|
return m.MarshalToSizedBuffer(dAtA[:size])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshakeDetails) MarshalToSizedBuffer(dAtA []byte) (int, error) {
|
||||||
|
i := len(dAtA)
|
||||||
|
_ = i
|
||||||
|
var l int
|
||||||
|
_ = l
|
||||||
|
if m.CertVersion != 0 {
|
||||||
|
i = encodeVarintNebula(dAtA, i, uint64(m.CertVersion))
|
||||||
|
i--
|
||||||
|
dAtA[i] = 0x40
|
||||||
|
}
|
||||||
|
if m.Time != 0 {
|
||||||
|
i = encodeVarintNebula(dAtA, i, uint64(m.Time))
|
||||||
|
i--
|
||||||
|
dAtA[i] = 0x28
|
||||||
|
}
|
||||||
|
if m.Cookie != 0 {
|
||||||
|
i = encodeVarintNebula(dAtA, i, uint64(m.Cookie))
|
||||||
|
i--
|
||||||
|
dAtA[i] = 0x20
|
||||||
|
}
|
||||||
|
if m.ResponderIndex != 0 {
|
||||||
|
i = encodeVarintNebula(dAtA, i, uint64(m.ResponderIndex))
|
||||||
|
i--
|
||||||
|
dAtA[i] = 0x18
|
||||||
|
}
|
||||||
|
if m.InitiatorIndex != 0 {
|
||||||
|
i = encodeVarintNebula(dAtA, i, uint64(m.InitiatorIndex))
|
||||||
|
i--
|
||||||
|
dAtA[i] = 0x10
|
||||||
|
}
|
||||||
|
if len(m.Cert) > 0 {
|
||||||
|
i -= len(m.Cert)
|
||||||
|
copy(dAtA[i:], m.Cert)
|
||||||
|
i = encodeVarintNebula(dAtA, i, uint64(len(m.Cert)))
|
||||||
|
i--
|
||||||
|
dAtA[i] = 0xa
|
||||||
|
}
|
||||||
|
return len(dAtA) - i, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *NebulaControl) Marshal() (dAtA []byte, err error) {
|
func (m *NebulaControl) Marshal() (dAtA []byte, err error) {
|
||||||
size := m.Size()
|
size := m.Size()
|
||||||
dAtA = make([]byte, size)
|
dAtA = make([]byte, size)
|
||||||
@@ -1132,6 +1375,51 @@ func (m *NebulaPing) Size() (n int) {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshake) Size() (n int) {
|
||||||
|
if m == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
var l int
|
||||||
|
_ = l
|
||||||
|
if m.Details != nil {
|
||||||
|
l = m.Details.Size()
|
||||||
|
n += 1 + l + sovNebula(uint64(l))
|
||||||
|
}
|
||||||
|
l = len(m.Hmac)
|
||||||
|
if l > 0 {
|
||||||
|
n += 1 + l + sovNebula(uint64(l))
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *NebulaHandshakeDetails) Size() (n int) {
|
||||||
|
if m == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
var l int
|
||||||
|
_ = l
|
||||||
|
l = len(m.Cert)
|
||||||
|
if l > 0 {
|
||||||
|
n += 1 + l + sovNebula(uint64(l))
|
||||||
|
}
|
||||||
|
if m.InitiatorIndex != 0 {
|
||||||
|
n += 1 + sovNebula(uint64(m.InitiatorIndex))
|
||||||
|
}
|
||||||
|
if m.ResponderIndex != 0 {
|
||||||
|
n += 1 + sovNebula(uint64(m.ResponderIndex))
|
||||||
|
}
|
||||||
|
if m.Cookie != 0 {
|
||||||
|
n += 1 + sovNebula(uint64(m.Cookie))
|
||||||
|
}
|
||||||
|
if m.Time != 0 {
|
||||||
|
n += 1 + sovNebula(uint64(m.Time))
|
||||||
|
}
|
||||||
|
if m.CertVersion != 0 {
|
||||||
|
n += 1 + sovNebula(uint64(m.CertVersion))
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
func (m *NebulaControl) Size() (n int) {
|
func (m *NebulaControl) Size() (n int) {
|
||||||
if m == nil {
|
if m == nil {
|
||||||
return 0
|
return 0
|
||||||
@@ -1948,6 +2236,305 @@ func (m *NebulaPing) Unmarshal(dAtA []byte) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
func (m *NebulaHandshake) Unmarshal(dAtA []byte) error {
|
||||||
|
l := len(dAtA)
|
||||||
|
iNdEx := 0
|
||||||
|
for iNdEx < l {
|
||||||
|
preIndex := iNdEx
|
||||||
|
var wire uint64
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowNebula
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := dAtA[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
wire |= uint64(b&0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fieldNum := int32(wire >> 3)
|
||||||
|
wireType := int(wire & 0x7)
|
||||||
|
if wireType == 4 {
|
||||||
|
return fmt.Errorf("proto: NebulaHandshake: wiretype end group for non-group")
|
||||||
|
}
|
||||||
|
if fieldNum <= 0 {
|
||||||
|
return fmt.Errorf("proto: NebulaHandshake: illegal tag %d (wire type %d)", fieldNum, wire)
|
||||||
|
}
|
||||||
|
switch fieldNum {
|
||||||
|
case 1:
|
||||||
|
if wireType != 2 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field Details", wireType)
|
||||||
|
}
|
||||||
|
var msglen int
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowNebula
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := dAtA[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
msglen |= int(b&0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if msglen < 0 {
|
||||||
|
return ErrInvalidLengthNebula
|
||||||
|
}
|
||||||
|
postIndex := iNdEx + msglen
|
||||||
|
if postIndex < 0 {
|
||||||
|
return ErrInvalidLengthNebula
|
||||||
|
}
|
||||||
|
if postIndex > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
if m.Details == nil {
|
||||||
|
m.Details = &NebulaHandshakeDetails{}
|
||||||
|
}
|
||||||
|
if err := m.Details.Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
iNdEx = postIndex
|
||||||
|
case 2:
|
||||||
|
if wireType != 2 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field Hmac", wireType)
|
||||||
|
}
|
||||||
|
var byteLen int
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowNebula
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := dAtA[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
byteLen |= int(b&0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if byteLen < 0 {
|
||||||
|
return ErrInvalidLengthNebula
|
||||||
|
}
|
||||||
|
postIndex := iNdEx + byteLen
|
||||||
|
if postIndex < 0 {
|
||||||
|
return ErrInvalidLengthNebula
|
||||||
|
}
|
||||||
|
if postIndex > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
m.Hmac = append(m.Hmac[:0], dAtA[iNdEx:postIndex]...)
|
||||||
|
if m.Hmac == nil {
|
||||||
|
m.Hmac = []byte{}
|
||||||
|
}
|
||||||
|
iNdEx = postIndex
|
||||||
|
default:
|
||||||
|
iNdEx = preIndex
|
||||||
|
skippy, err := skipNebula(dAtA[iNdEx:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if (skippy < 0) || (iNdEx+skippy) < 0 {
|
||||||
|
return ErrInvalidLengthNebula
|
||||||
|
}
|
||||||
|
if (iNdEx + skippy) > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
iNdEx += skippy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if iNdEx > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (m *NebulaHandshakeDetails) Unmarshal(dAtA []byte) error {
|
||||||
|
l := len(dAtA)
|
||||||
|
iNdEx := 0
|
||||||
|
for iNdEx < l {
|
||||||
|
preIndex := iNdEx
|
||||||
|
var wire uint64
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowNebula
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := dAtA[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
wire |= uint64(b&0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fieldNum := int32(wire >> 3)
|
||||||
|
wireType := int(wire & 0x7)
|
||||||
|
if wireType == 4 {
|
||||||
|
return fmt.Errorf("proto: NebulaHandshakeDetails: wiretype end group for non-group")
|
||||||
|
}
|
||||||
|
if fieldNum <= 0 {
|
||||||
|
return fmt.Errorf("proto: NebulaHandshakeDetails: illegal tag %d (wire type %d)", fieldNum, wire)
|
||||||
|
}
|
||||||
|
switch fieldNum {
|
||||||
|
case 1:
|
||||||
|
if wireType != 2 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field Cert", wireType)
|
||||||
|
}
|
||||||
|
var byteLen int
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowNebula
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := dAtA[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
byteLen |= int(b&0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if byteLen < 0 {
|
||||||
|
return ErrInvalidLengthNebula
|
||||||
|
}
|
||||||
|
postIndex := iNdEx + byteLen
|
||||||
|
if postIndex < 0 {
|
||||||
|
return ErrInvalidLengthNebula
|
||||||
|
}
|
||||||
|
if postIndex > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
m.Cert = append(m.Cert[:0], dAtA[iNdEx:postIndex]...)
|
||||||
|
if m.Cert == nil {
|
||||||
|
m.Cert = []byte{}
|
||||||
|
}
|
||||||
|
iNdEx = postIndex
|
||||||
|
case 2:
|
||||||
|
if wireType != 0 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field InitiatorIndex", wireType)
|
||||||
|
}
|
||||||
|
m.InitiatorIndex = 0
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowNebula
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := dAtA[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
m.InitiatorIndex |= uint32(b&0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 3:
|
||||||
|
if wireType != 0 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field ResponderIndex", wireType)
|
||||||
|
}
|
||||||
|
m.ResponderIndex = 0
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowNebula
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := dAtA[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
m.ResponderIndex |= uint32(b&0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 4:
|
||||||
|
if wireType != 0 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field Cookie", wireType)
|
||||||
|
}
|
||||||
|
m.Cookie = 0
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowNebula
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := dAtA[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
m.Cookie |= uint64(b&0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 5:
|
||||||
|
if wireType != 0 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field Time", wireType)
|
||||||
|
}
|
||||||
|
m.Time = 0
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowNebula
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := dAtA[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
m.Time |= uint64(b&0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 8:
|
||||||
|
if wireType != 0 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field CertVersion", wireType)
|
||||||
|
}
|
||||||
|
m.CertVersion = 0
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowNebula
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := dAtA[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
m.CertVersion |= uint32(b&0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
iNdEx = preIndex
|
||||||
|
skippy, err := skipNebula(dAtA[iNdEx:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if (skippy < 0) || (iNdEx+skippy) < 0 {
|
||||||
|
return ErrInvalidLengthNebula
|
||||||
|
}
|
||||||
|
if (iNdEx + skippy) > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
iNdEx += skippy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if iNdEx > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
func (m *NebulaControl) Unmarshal(dAtA []byte) error {
|
func (m *NebulaControl) Unmarshal(dAtA []byte) error {
|
||||||
l := len(dAtA)
|
l := len(dAtA)
|
||||||
iNdEx := 0
|
iNdEx := 0
|
||||||
|
|||||||
+15
-3
@@ -60,9 +60,21 @@ message NebulaPing {
|
|||||||
uint64 Time = 2;
|
uint64 Time = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NebulaHandshake / NebulaHandshakeDetails moved to
|
message NebulaHandshake {
|
||||||
// handshake/handshake.proto. The handshake package speaks that wire format
|
NebulaHandshakeDetails Details = 1;
|
||||||
// directly via a hand-written encoder/decoder.
|
bytes Hmac = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message NebulaHandshakeDetails {
|
||||||
|
bytes Cert = 1;
|
||||||
|
uint32 InitiatorIndex = 2;
|
||||||
|
uint32 ResponderIndex = 3;
|
||||||
|
uint64 Cookie = 4;
|
||||||
|
uint64 Time = 5;
|
||||||
|
uint32 CertVersion = 8;
|
||||||
|
// reserved for WIP multiport
|
||||||
|
reserved 6, 7;
|
||||||
|
}
|
||||||
|
|
||||||
message NebulaControl {
|
message NebulaControl {
|
||||||
enum MessageType {
|
enum MessageType {
|
||||||
|
|||||||
+2
-13
@@ -183,20 +183,11 @@ func (f *Interface) readOutsidePackets(via ViaSender, out []byte, packet []byte,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch h.Subtype {
|
if h.Subtype == header.TestRequest {
|
||||||
case header.TestRequest:
|
|
||||||
// This testRequest might be from TryPromoteBest, so we should roam
|
// This testRequest might be from TryPromoteBest, so we should roam
|
||||||
// to the new IP address before responding
|
// to the new IP address before responding
|
||||||
f.handleHostRoaming(hostinfo, via)
|
f.handleHostRoaming(hostinfo, via)
|
||||||
f.send(header.Test, header.TestReply, ci, hostinfo, d, nb, out)
|
f.send(header.Test, header.TestReply, ci, hostinfo, d, nb, out)
|
||||||
case header.MTUDProbeRequest:
|
|
||||||
// Reply with just the 8-byte ack header so the reverse path doesn't have to
|
|
||||||
// carry the full probe size; we only verify the forward direction.
|
|
||||||
if len(d) >= 8 {
|
|
||||||
f.send(header.Test, header.MTUDProbeReply, ci, hostinfo, d[:8], nb, out)
|
|
||||||
}
|
|
||||||
case header.MTUDProbeReply:
|
|
||||||
f.pmtudManager.HandleReply(hostinfo.localIndexId, d)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallthrough to the bottom to record incoming traffic
|
// Fallthrough to the bottom to record incoming traffic
|
||||||
@@ -266,7 +257,6 @@ func (f *Interface) readOutsidePackets(via ViaSender, out []byte, packet []byte,
|
|||||||
|
|
||||||
// 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) {
|
||||||
f.pmtudManager.OnTunnelDown(hostInfo)
|
|
||||||
final := f.hostMap.DeleteHostInfo(hostInfo)
|
final := f.hostMap.DeleteHostInfo(hostInfo)
|
||||||
if final {
|
if final {
|
||||||
// We no longer have any tunnels with this vpn addr, clear learned lighthouse state to lower memory usage
|
// We no longer have any tunnels with this vpn addr, clear learned lighthouse state to lower memory usage
|
||||||
@@ -306,7 +296,6 @@ func (f *Interface) handleHostRoaming(hostinfo *HostInfo, via ViaSender) {
|
|||||||
hostinfo.lastRoam = time.Now()
|
hostinfo.lastRoam = time.Now()
|
||||||
hostinfo.lastRoamRemote = hostinfo.remote
|
hostinfo.lastRoamRemote = hostinfo.remote
|
||||||
hostinfo.SetRemote(via.UdpAddr)
|
hostinfo.SetRemote(via.UdpAddr)
|
||||||
f.pmtudManager.OnRoam(hostinfo)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -583,7 +572,7 @@ func (f *Interface) decryptToTun(hostinfo *HostInfo, messageCounter uint64, out
|
|||||||
}
|
}
|
||||||
|
|
||||||
f.connectionManager.In(hostinfo)
|
f.connectionManager.In(hostinfo)
|
||||||
_, err = f.readers[q].Write(out)
|
err = f.batchers[q].Commit(out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
f.l.Error("Failed to write to tun", "error", err)
|
f.l.Error("Failed to write to tun", "error", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package batch
|
||||||
|
|
||||||
|
import "net/netip"
|
||||||
|
|
||||||
|
type RxBatcher interface {
|
||||||
|
// Reserve creates a pkt to borrow
|
||||||
|
Reserve(sz int) []byte
|
||||||
|
// Commit borrows pkt. The caller must keep pkt valid until the next Flush
|
||||||
|
Commit(pkt []byte) error
|
||||||
|
// Flush emits every queued packet in arrival order. Returns the
|
||||||
|
// first error observed; keeps draining so one bad packet doesn't hold up
|
||||||
|
// the rest. After Flush returns, borrowed payload slices may be recycled.
|
||||||
|
Flush() error
|
||||||
|
}
|
||||||
|
|
||||||
|
type TxBatcher interface {
|
||||||
|
// Next returns a zero-length slice with slotCap capacity over the next unused
|
||||||
|
// slot's backing bytes. The caller writes into the returned slice and then
|
||||||
|
// calls Commit with the final length and destination. Next returns nil when
|
||||||
|
// the batch is full.
|
||||||
|
Next() []byte
|
||||||
|
// Commit records the slot just returned by Next as a packet of length n
|
||||||
|
// destined for dst.
|
||||||
|
Commit(n int, dst netip.AddrPort)
|
||||||
|
// Reset clears committed slots; backing storage is retained for reuse.
|
||||||
|
Reset()
|
||||||
|
// Len returns the number of committed packets.
|
||||||
|
Len() int
|
||||||
|
// Cap returns the maximum number of slots in the batch.
|
||||||
|
Cap() int
|
||||||
|
// Get returns the buffers needed to send the batch
|
||||||
|
Get() ([][]byte, []netip.AddrPort)
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package batch
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/udp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Passthrough is a RxBatcher that doesn't batch anything, it just accumulates and then sends packets.
|
||||||
|
type Passthrough struct {
|
||||||
|
out io.Writer
|
||||||
|
slots [][]byte
|
||||||
|
backing []byte
|
||||||
|
cursor int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPassthrough(w io.Writer) *Passthrough {
|
||||||
|
const baseNumSlots = 128
|
||||||
|
return &Passthrough{
|
||||||
|
out: w,
|
||||||
|
slots: make([][]byte, 0, baseNumSlots),
|
||||||
|
backing: make([]byte, 0, baseNumSlots*udp.MTU),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Passthrough) Reserve(sz int) []byte {
|
||||||
|
if len(p.backing)+sz > cap(p.backing) {
|
||||||
|
// Grow: allocate a fresh backing. Already-committed slices still
|
||||||
|
// reference the old array and remain valid until Flush drops them.
|
||||||
|
newCap := max(cap(p.backing)*2, sz)
|
||||||
|
p.backing = make([]byte, 0, newCap)
|
||||||
|
}
|
||||||
|
start := len(p.backing)
|
||||||
|
p.backing = p.backing[:start+sz]
|
||||||
|
return p.backing[start : start+sz : start+sz] //return zero length, sz-cap slice
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Passthrough) Commit(pkt []byte) error {
|
||||||
|
p.slots = append(p.slots, pkt)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Passthrough) Flush() error {
|
||||||
|
var firstErr error
|
||||||
|
for _, s := range p.slots {
|
||||||
|
_, err := p.out.Write(s)
|
||||||
|
if err != nil && firstErr == nil {
|
||||||
|
firstErr = err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := range p.slots {
|
||||||
|
p.slots[i] = nil
|
||||||
|
}
|
||||||
|
p.slots = p.slots[:0]
|
||||||
|
p.backing = p.backing[:0]
|
||||||
|
return firstErr
|
||||||
|
}
|
||||||
@@ -0,0 +1,506 @@
|
|||||||
|
package batch
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ipProtoTCP is the IANA protocol number for TCP. Hardcoded instead of
|
||||||
|
// reaching for golang.org/x/sys/unix — that package doesn't define the
|
||||||
|
// constant on Windows, which would break cross-compiles even though this
|
||||||
|
// file runs unchanged on every platform.
|
||||||
|
const ipProtoTCP = 6
|
||||||
|
|
||||||
|
// tcpCoalesceBufSize caps total bytes per superpacket. Mirrors the kernel's
|
||||||
|
// sk_gso_max_size of ~64KiB; anything beyond this would be rejected anyway.
|
||||||
|
const tcpCoalesceBufSize = 65535
|
||||||
|
|
||||||
|
// tcpCoalesceMaxSegs caps how many segments we'll coalesce into a single
|
||||||
|
// superpacket. Keeping this well below the kernel's TSO ceiling bounds
|
||||||
|
// latency.
|
||||||
|
const tcpCoalesceMaxSegs = 64
|
||||||
|
|
||||||
|
// tcpCoalesceHdrCap is the scratch space we copy a seed's IP+TCP header
|
||||||
|
// into. IPv6 (40) + TCP with full options (60) = 100 bytes.
|
||||||
|
const tcpCoalesceHdrCap = 100
|
||||||
|
|
||||||
|
// initialSlots is the starting capacity of the slot pool. One flow per
|
||||||
|
// packet is the worst case so this matches a typical UDP recvmmsg batch.
|
||||||
|
const initialSlots = 64
|
||||||
|
|
||||||
|
// flowKey identifies a TCP flow by {src, dst, sport, dport, family}.
|
||||||
|
// Comparable, so linear scans over the slot list stay tight.
|
||||||
|
type flowKey struct {
|
||||||
|
src, dst [16]byte
|
||||||
|
sport, dport uint16
|
||||||
|
isV6 bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// coalesceSlot is one entry in the coalescer's ordered event queue. When
|
||||||
|
// passthrough is true the slot holds a single borrowed packet that must be
|
||||||
|
// emitted verbatim (non-TCP, non-admissible TCP, or oversize seed). When
|
||||||
|
// passthrough is false the slot is an in-progress coalesced superpacket:
|
||||||
|
// hdrBuf is a mutable copy of the seed's IP+TCP header (we patch total
|
||||||
|
// length and pseudo-header partial at flush), and payIovs are *borrowed*
|
||||||
|
// slices from the caller's plaintext buffers — no payload is ever copied.
|
||||||
|
// The caller (listenOut) must keep those buffers alive until Flush.
|
||||||
|
type coalesceSlot struct {
|
||||||
|
passthrough bool
|
||||||
|
rawPkt []byte // borrowed when passthrough
|
||||||
|
|
||||||
|
fk flowKey
|
||||||
|
hdrBuf [tcpCoalesceHdrCap]byte
|
||||||
|
hdrLen int
|
||||||
|
ipHdrLen int
|
||||||
|
isV6 bool
|
||||||
|
gsoSize int
|
||||||
|
numSeg int
|
||||||
|
totalPay int
|
||||||
|
nextSeq uint32
|
||||||
|
// psh closes the chain: set when the last-accepted segment had PSH or
|
||||||
|
// was sub-gsoSize. No further appends after that.
|
||||||
|
psh bool
|
||||||
|
payIovs [][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// TCPCoalescer accumulates adjacent in-flow TCP data segments across
|
||||||
|
// multiple concurrent flows and emits each flow's run as a single TSO
|
||||||
|
// superpacket via tio.GSOWriter. All output — coalesced or not — is
|
||||||
|
// deferred until Flush so arrival order is preserved on the wire. Owns
|
||||||
|
// no locks; one coalescer per TUN write queue.
|
||||||
|
type TCPCoalescer struct {
|
||||||
|
plainW io.Writer
|
||||||
|
gsoW tio.GSOWriter // nil when the queue doesn't support TSO
|
||||||
|
|
||||||
|
// slots is the ordered event queue. Flush walks it once and emits each
|
||||||
|
// entry as either a WriteGSO (coalesced) or a plainW.Write (passthrough).
|
||||||
|
slots []*coalesceSlot
|
||||||
|
// openSlots maps a flow key to its most recent non-sealed slot, so new
|
||||||
|
// segments can extend an in-progress superpacket in O(1). Slots are
|
||||||
|
// removed from this map when they close (PSH or short-last-segment),
|
||||||
|
// when a non-admissible packet for that flow arrives, or in Flush.
|
||||||
|
openSlots map[flowKey]*coalesceSlot
|
||||||
|
pool []*coalesceSlot // free list for reuse
|
||||||
|
|
||||||
|
backing []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTCPCoalescer(w io.Writer) *TCPCoalescer {
|
||||||
|
c := &TCPCoalescer{
|
||||||
|
plainW: w,
|
||||||
|
slots: make([]*coalesceSlot, 0, initialSlots),
|
||||||
|
openSlots: make(map[flowKey]*coalesceSlot, initialSlots),
|
||||||
|
pool: make([]*coalesceSlot, 0, initialSlots),
|
||||||
|
backing: make([]byte, 0, initialSlots*65535),
|
||||||
|
}
|
||||||
|
if gw, ok := w.(tio.GSOWriter); ok && gw.GSOSupported() {
|
||||||
|
c.gsoW = gw
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// parsedTCP holds the fields extracted from a single parse so later steps
|
||||||
|
// (admission, slot lookup, canAppend) don't re-walk the header.
|
||||||
|
type parsedTCP struct {
|
||||||
|
fk flowKey
|
||||||
|
ipHdrLen int
|
||||||
|
tcpHdrLen int
|
||||||
|
hdrLen int
|
||||||
|
payLen int
|
||||||
|
seq uint32
|
||||||
|
flags byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseTCPBase extracts the flow key and IP/TCP offsets for any TCP packet,
|
||||||
|
// regardless of whether it's admissible for coalescing. Returns ok=false
|
||||||
|
// for non-TCP or malformed input. Accepts IPv4 (no options, no fragmentation)
|
||||||
|
// and IPv6 (no extension headers).
|
||||||
|
func parseTCPBase(pkt []byte) (parsedTCP, bool) {
|
||||||
|
var p parsedTCP
|
||||||
|
if len(pkt) < 20 {
|
||||||
|
return p, false
|
||||||
|
}
|
||||||
|
v := pkt[0] >> 4
|
||||||
|
switch v {
|
||||||
|
case 4:
|
||||||
|
ihl := int(pkt[0]&0x0f) * 4
|
||||||
|
if ihl != 20 {
|
||||||
|
return p, false
|
||||||
|
}
|
||||||
|
if pkt[9] != ipProtoTCP {
|
||||||
|
return p, false
|
||||||
|
}
|
||||||
|
// Reject actual fragmentation (MF or non-zero frag offset).
|
||||||
|
if binary.BigEndian.Uint16(pkt[6:8])&0x3fff != 0 {
|
||||||
|
return p, false
|
||||||
|
}
|
||||||
|
totalLen := int(binary.BigEndian.Uint16(pkt[2:4]))
|
||||||
|
if totalLen > len(pkt) || totalLen < ihl {
|
||||||
|
return p, false
|
||||||
|
}
|
||||||
|
p.ipHdrLen = 20
|
||||||
|
p.fk.isV6 = false
|
||||||
|
copy(p.fk.src[:4], pkt[12:16])
|
||||||
|
copy(p.fk.dst[:4], pkt[16:20])
|
||||||
|
pkt = pkt[:totalLen]
|
||||||
|
case 6:
|
||||||
|
if len(pkt) < 40 {
|
||||||
|
return p, false
|
||||||
|
}
|
||||||
|
if pkt[6] != ipProtoTCP {
|
||||||
|
return p, false
|
||||||
|
}
|
||||||
|
payloadLen := int(binary.BigEndian.Uint16(pkt[4:6]))
|
||||||
|
if 40+payloadLen > len(pkt) {
|
||||||
|
return p, false
|
||||||
|
}
|
||||||
|
p.ipHdrLen = 40
|
||||||
|
p.fk.isV6 = true
|
||||||
|
copy(p.fk.src[:], pkt[8:24])
|
||||||
|
copy(p.fk.dst[:], pkt[24:40])
|
||||||
|
pkt = pkt[:40+payloadLen]
|
||||||
|
default:
|
||||||
|
return p, false
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(pkt) < p.ipHdrLen+20 {
|
||||||
|
return p, false
|
||||||
|
}
|
||||||
|
tcpOff := int(pkt[p.ipHdrLen+12]>>4) * 4
|
||||||
|
if tcpOff < 20 || tcpOff > 60 {
|
||||||
|
return p, false
|
||||||
|
}
|
||||||
|
if len(pkt) < p.ipHdrLen+tcpOff {
|
||||||
|
return p, false
|
||||||
|
}
|
||||||
|
p.tcpHdrLen = tcpOff
|
||||||
|
p.hdrLen = p.ipHdrLen + tcpOff
|
||||||
|
p.payLen = len(pkt) - p.hdrLen
|
||||||
|
p.seq = binary.BigEndian.Uint32(pkt[p.ipHdrLen+4 : p.ipHdrLen+8])
|
||||||
|
p.flags = pkt[p.ipHdrLen+13]
|
||||||
|
p.fk.sport = binary.BigEndian.Uint16(pkt[p.ipHdrLen : p.ipHdrLen+2])
|
||||||
|
p.fk.dport = binary.BigEndian.Uint16(pkt[p.ipHdrLen+2 : p.ipHdrLen+4])
|
||||||
|
return p, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// coalesceable reports whether a parsed TCP segment is eligible for
|
||||||
|
// coalescing. Accepts only ACK or ACK|PSH with a non-empty payload.
|
||||||
|
func (p parsedTCP) coalesceable() bool {
|
||||||
|
const ack = 0x10
|
||||||
|
const psh = 0x08
|
||||||
|
if p.flags&^(ack|psh) != 0 || p.flags&ack == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return p.payLen > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TCPCoalescer) Reserve(sz int) []byte {
|
||||||
|
if len(c.backing)+sz > cap(c.backing) {
|
||||||
|
// Grow: allocate a fresh backing. Already-committed slices still
|
||||||
|
// reference the old array and remain valid until Flush drops them.
|
||||||
|
newCap := max(cap(c.backing)*2, sz)
|
||||||
|
c.backing = make([]byte, 0, newCap)
|
||||||
|
}
|
||||||
|
start := len(c.backing)
|
||||||
|
c.backing = c.backing[:start+sz]
|
||||||
|
return c.backing[start : start+sz : start+sz] //return zero length, sz-cap slice
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit borrows pkt. The caller must keep pkt valid until the next Flush,
|
||||||
|
// whether or not the packet was coalesced — passthrough (non-admissible)
|
||||||
|
// packets are queued and written at Flush time, not synchronously.
|
||||||
|
func (c *TCPCoalescer) Commit(pkt []byte) error {
|
||||||
|
if c.gsoW == nil {
|
||||||
|
c.addPassthrough(pkt)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
info, ok := parseTCPBase(pkt)
|
||||||
|
if !ok {
|
||||||
|
// Non-TCP or malformed — can't possibly collide with an open flow.
|
||||||
|
c.addPassthrough(pkt)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if !info.coalesceable() {
|
||||||
|
// TCP but not admissible (SYN/FIN/RST/URG/CWR/ECE or zero-payload).
|
||||||
|
// Seal this flow's open slot so later in-flow packets don't extend
|
||||||
|
// it and accidentally reorder past this passthrough.
|
||||||
|
delete(c.openSlots, info.fk)
|
||||||
|
c.addPassthrough(pkt)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if open := c.openSlots[info.fk]; open != nil {
|
||||||
|
if c.canAppend(open, pkt, info) {
|
||||||
|
c.appendPayload(open, pkt, info)
|
||||||
|
if open.psh {
|
||||||
|
delete(c.openSlots, info.fk)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// Can't extend — seal it and fall through to seed a fresh slot.
|
||||||
|
delete(c.openSlots, info.fk)
|
||||||
|
}
|
||||||
|
c.seed(pkt, info)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flush emits every queued event in arrival order. Coalesced slots go out
|
||||||
|
// via WriteGSO; passthrough slots go out via plainW.Write. Returns the
|
||||||
|
// first error observed; keeps draining so one bad packet doesn't hold up
|
||||||
|
// the rest. After Flush returns, borrowed payload slices may be recycled.
|
||||||
|
func (c *TCPCoalescer) Flush() error {
|
||||||
|
var first error
|
||||||
|
for _, s := range c.slots {
|
||||||
|
var err error
|
||||||
|
if s.passthrough {
|
||||||
|
_, err = c.plainW.Write(s.rawPkt)
|
||||||
|
} else {
|
||||||
|
err = c.flushSlot(s)
|
||||||
|
}
|
||||||
|
if err != nil && first == nil {
|
||||||
|
first = err
|
||||||
|
}
|
||||||
|
c.release(s)
|
||||||
|
}
|
||||||
|
for i := range c.slots {
|
||||||
|
c.slots[i] = nil
|
||||||
|
}
|
||||||
|
c.slots = c.slots[:0]
|
||||||
|
for k := range c.openSlots {
|
||||||
|
delete(c.openSlots, k)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.backing = c.backing[:0]
|
||||||
|
return first
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TCPCoalescer) addPassthrough(pkt []byte) {
|
||||||
|
s := c.take()
|
||||||
|
s.passthrough = true
|
||||||
|
s.rawPkt = pkt
|
||||||
|
c.slots = append(c.slots, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TCPCoalescer) seed(pkt []byte, info parsedTCP) {
|
||||||
|
if info.hdrLen > tcpCoalesceHdrCap || info.hdrLen+info.payLen > tcpCoalesceBufSize {
|
||||||
|
// Pathological shape — can't fit our scratch, emit as-is.
|
||||||
|
c.addPassthrough(pkt)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s := c.take()
|
||||||
|
s.passthrough = false
|
||||||
|
s.rawPkt = nil
|
||||||
|
copy(s.hdrBuf[:], pkt[:info.hdrLen])
|
||||||
|
s.hdrLen = info.hdrLen
|
||||||
|
s.ipHdrLen = info.ipHdrLen
|
||||||
|
s.isV6 = info.fk.isV6
|
||||||
|
s.fk = info.fk
|
||||||
|
s.gsoSize = info.payLen
|
||||||
|
s.numSeg = 1
|
||||||
|
s.totalPay = info.payLen
|
||||||
|
s.nextSeq = info.seq + uint32(info.payLen)
|
||||||
|
s.psh = info.flags&0x08 != 0
|
||||||
|
s.payIovs = append(s.payIovs[:0], pkt[info.hdrLen:info.hdrLen+info.payLen])
|
||||||
|
c.slots = append(c.slots, s)
|
||||||
|
if !s.psh {
|
||||||
|
c.openSlots[info.fk] = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// canAppend reports whether info's packet extends the slot's seed: same
|
||||||
|
// header shape and stable contents, adjacent seq, not oversized, chain not
|
||||||
|
// closed.
|
||||||
|
func (c *TCPCoalescer) canAppend(s *coalesceSlot, pkt []byte, info parsedTCP) bool {
|
||||||
|
if s.psh {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if info.hdrLen != s.hdrLen {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if info.seq != s.nextSeq {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if s.numSeg >= tcpCoalesceMaxSegs {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if info.payLen > s.gsoSize {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if s.hdrLen+s.totalPay+info.payLen > tcpCoalesceBufSize {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !headersMatch(s.hdrBuf[:s.hdrLen], pkt[:info.hdrLen], s.isV6, s.ipHdrLen) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TCPCoalescer) appendPayload(s *coalesceSlot, pkt []byte, info parsedTCP) {
|
||||||
|
s.payIovs = append(s.payIovs, pkt[info.hdrLen:info.hdrLen+info.payLen])
|
||||||
|
s.numSeg++
|
||||||
|
s.totalPay += info.payLen
|
||||||
|
s.nextSeq = info.seq + uint32(info.payLen)
|
||||||
|
if info.flags&0x08 != 0 {
|
||||||
|
// Propagate PSH into the seed header so kernel TSO sets it on the
|
||||||
|
// last segment. Without this the sender's push signal is dropped.
|
||||||
|
s.hdrBuf[s.ipHdrLen+13] |= 0x08
|
||||||
|
}
|
||||||
|
if info.payLen < s.gsoSize || info.flags&0x08 != 0 {
|
||||||
|
s.psh = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TCPCoalescer) take() *coalesceSlot {
|
||||||
|
if n := len(c.pool); n > 0 {
|
||||||
|
s := c.pool[n-1]
|
||||||
|
c.pool[n-1] = nil
|
||||||
|
c.pool = c.pool[:n-1]
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return &coalesceSlot{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TCPCoalescer) release(s *coalesceSlot) {
|
||||||
|
s.passthrough = false
|
||||||
|
s.rawPkt = nil
|
||||||
|
for i := range s.payIovs {
|
||||||
|
s.payIovs[i] = nil
|
||||||
|
}
|
||||||
|
s.payIovs = s.payIovs[:0]
|
||||||
|
s.numSeg = 0
|
||||||
|
s.totalPay = 0
|
||||||
|
s.psh = false
|
||||||
|
c.pool = append(c.pool, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// flushSlot patches the header and calls WriteGSO. Does not remove the
|
||||||
|
// slot from c.slots.
|
||||||
|
func (c *TCPCoalescer) flushSlot(s *coalesceSlot) error {
|
||||||
|
total := s.hdrLen + s.totalPay
|
||||||
|
l4Len := total - s.ipHdrLen
|
||||||
|
hdr := s.hdrBuf[:s.hdrLen]
|
||||||
|
|
||||||
|
if s.isV6 {
|
||||||
|
binary.BigEndian.PutUint16(hdr[4:6], uint16(l4Len))
|
||||||
|
} else {
|
||||||
|
binary.BigEndian.PutUint16(hdr[2:4], uint16(total))
|
||||||
|
hdr[10] = 0
|
||||||
|
hdr[11] = 0
|
||||||
|
binary.BigEndian.PutUint16(hdr[10:12], ipv4HdrChecksum(hdr[:s.ipHdrLen]))
|
||||||
|
}
|
||||||
|
|
||||||
|
var psum uint32
|
||||||
|
if s.isV6 {
|
||||||
|
psum = pseudoSumIPv6(hdr[8:24], hdr[24:40], ipProtoTCP, l4Len)
|
||||||
|
} else {
|
||||||
|
psum = pseudoSumIPv4(hdr[12:16], hdr[16:20], ipProtoTCP, l4Len)
|
||||||
|
}
|
||||||
|
tcsum := s.ipHdrLen + 16
|
||||||
|
binary.BigEndian.PutUint16(hdr[tcsum:tcsum+2], foldOnceNoInvert(psum))
|
||||||
|
|
||||||
|
return c.gsoW.WriteGSO(hdr[:s.ipHdrLen], hdr[s.ipHdrLen:], s.payIovs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// headersMatch compares two IP+TCP header prefixes for byte-for-byte
|
||||||
|
// equality on every field that must be identical across coalesced
|
||||||
|
// segments. Size/IPID/IPCsum/seq/flags/tcpCsum are masked out.
|
||||||
|
func headersMatch(a, b []byte, isV6 bool, ipHdrLen int) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if isV6 {
|
||||||
|
// IPv6: bytes [0:4] = version/TC/flow-label, [6:8] = next_hdr/hop,
|
||||||
|
// [8:40] = src+dst. Skip [4:6] payload length.
|
||||||
|
if !bytes.Equal(a[0:4], b[0:4]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !bytes.Equal(a[6:40], b[6:40]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// IPv4: [0:2] version/IHL/TOS, [6:10] flags/fragoff/TTL/proto,
|
||||||
|
// [12:20] src+dst. Skip [2:4] total len, [4:6] id, [10:12] csum.
|
||||||
|
if !bytes.Equal(a[0:2], b[0:2]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !bytes.Equal(a[6:10], b[6:10]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !bytes.Equal(a[12:20], b[12:20]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TCP: compare [0:4] ports, [8:13] ack+dataoff, [14:16] window,
|
||||||
|
// [18:tcpHdrLen] options (incl. urgent).
|
||||||
|
tcp := ipHdrLen
|
||||||
|
if !bytes.Equal(a[tcp:tcp+4], b[tcp:tcp+4]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !bytes.Equal(a[tcp+8:tcp+13], b[tcp+8:tcp+13]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !bytes.Equal(a[tcp+14:tcp+16], b[tcp+14:tcp+16]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !bytes.Equal(a[tcp+18:], b[tcp+18:]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// ipv4HdrChecksum computes the IPv4 header checksum over hdr (which must
|
||||||
|
// already have its checksum field zeroed) and returns the folded/inverted
|
||||||
|
// 16-bit value to store.
|
||||||
|
func ipv4HdrChecksum(hdr []byte) uint16 {
|
||||||
|
var sum uint32
|
||||||
|
for i := 0; i+1 < len(hdr); i += 2 {
|
||||||
|
sum += uint32(binary.BigEndian.Uint16(hdr[i : i+2]))
|
||||||
|
}
|
||||||
|
if len(hdr)%2 == 1 {
|
||||||
|
sum += uint32(hdr[len(hdr)-1]) << 8
|
||||||
|
}
|
||||||
|
for sum>>16 != 0 {
|
||||||
|
sum = (sum & 0xffff) + (sum >> 16)
|
||||||
|
}
|
||||||
|
return ^uint16(sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
// pseudoSumIPv4 / pseudoSumIPv6 build the TCP pseudo-header partial sum
|
||||||
|
// expected by the virtio NEEDS_CSUM kernel path: the 32-bit accumulator
|
||||||
|
// before folding.
|
||||||
|
func pseudoSumIPv4(src, dst []byte, proto byte, l4Len int) uint32 {
|
||||||
|
var sum uint32
|
||||||
|
sum += uint32(binary.BigEndian.Uint16(src[0:2]))
|
||||||
|
sum += uint32(binary.BigEndian.Uint16(src[2:4]))
|
||||||
|
sum += uint32(binary.BigEndian.Uint16(dst[0:2]))
|
||||||
|
sum += uint32(binary.BigEndian.Uint16(dst[2:4]))
|
||||||
|
sum += uint32(proto)
|
||||||
|
sum += uint32(l4Len)
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
func pseudoSumIPv6(src, dst []byte, proto byte, l4Len int) uint32 {
|
||||||
|
var sum uint32
|
||||||
|
for i := 0; i < 16; i += 2 {
|
||||||
|
sum += uint32(binary.BigEndian.Uint16(src[i : i+2]))
|
||||||
|
sum += uint32(binary.BigEndian.Uint16(dst[i : i+2]))
|
||||||
|
}
|
||||||
|
sum += uint32(l4Len >> 16)
|
||||||
|
sum += uint32(l4Len & 0xffff)
|
||||||
|
sum += uint32(proto)
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
// foldOnceNoInvert folds the 32-bit accumulator to 16 bits and returns it
|
||||||
|
// unchanged (no one's complement). This is what virtio NEEDS_CSUM wants in
|
||||||
|
// the L4 checksum field — the kernel will add the payload sum and invert.
|
||||||
|
func foldOnceNoInvert(sum uint32) uint16 {
|
||||||
|
for sum>>16 != 0 {
|
||||||
|
sum = (sum & 0xffff) + (sum >> 16)
|
||||||
|
}
|
||||||
|
return uint16(sum)
|
||||||
|
}
|
||||||
@@ -0,0 +1,618 @@
|
|||||||
|
package batch
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// fakeTunWriter records plain Writes and WriteGSO calls without touching a
|
||||||
|
// real TUN fd. WriteGSO records the IP header, transport header, and
|
||||||
|
// borrowed payload fragments separately so tests can inspect each.
|
||||||
|
type fakeTunWriter struct {
|
||||||
|
gsoEnabled bool
|
||||||
|
writes [][]byte
|
||||||
|
gsoWrites []fakeGSOWrite
|
||||||
|
}
|
||||||
|
|
||||||
|
// fakeGSOWrite captures one WriteGSO call. hdr is the concatenation of the
|
||||||
|
// IP and transport headers (in that order), gsoSize / isV6 / csumStart are
|
||||||
|
// derived from the call so existing assertions keep working unchanged.
|
||||||
|
type fakeGSOWrite struct {
|
||||||
|
hdr []byte
|
||||||
|
pays [][]byte
|
||||||
|
gsoSize uint16
|
||||||
|
isV6 bool
|
||||||
|
csumStart uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
// total returns hdrLen + sum of pay lens.
|
||||||
|
func (g fakeGSOWrite) total() int {
|
||||||
|
n := len(g.hdr)
|
||||||
|
for _, p := range g.pays {
|
||||||
|
n += len(p)
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// payLen sums the pays.
|
||||||
|
func (g fakeGSOWrite) payLen() int {
|
||||||
|
var n int
|
||||||
|
for _, p := range g.pays {
|
||||||
|
n += len(p)
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *fakeTunWriter) Write(p []byte) (int, error) {
|
||||||
|
buf := make([]byte, len(p))
|
||||||
|
copy(buf, p)
|
||||||
|
w.writes = append(w.writes, buf)
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *fakeTunWriter) WriteGSO(hdr []byte, transportHdr []byte, pays [][]byte) error {
|
||||||
|
hcopy := make([]byte, len(hdr)+len(transportHdr))
|
||||||
|
copy(hcopy, hdr)
|
||||||
|
copy(hcopy[len(hdr):], transportHdr)
|
||||||
|
paysCopy := make([][]byte, len(pays))
|
||||||
|
for i, p := range pays {
|
||||||
|
pc := make([]byte, len(p))
|
||||||
|
copy(pc, p)
|
||||||
|
paysCopy[i] = pc
|
||||||
|
}
|
||||||
|
var gsoSize uint16
|
||||||
|
if len(pays) > 1 {
|
||||||
|
gsoSize = uint16(len(pays[0]))
|
||||||
|
}
|
||||||
|
isV6 := len(hdr) > 0 && hdr[0]>>4 == 6
|
||||||
|
w.gsoWrites = append(w.gsoWrites, fakeGSOWrite{
|
||||||
|
hdr: hcopy,
|
||||||
|
pays: paysCopy,
|
||||||
|
gsoSize: gsoSize,
|
||||||
|
isV6: isV6,
|
||||||
|
csumStart: uint16(len(hdr)),
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *fakeTunWriter) GSOSupported() bool { return w.gsoEnabled }
|
||||||
|
|
||||||
|
// buildTCPv4 constructs a minimal IPv4+TCP packet with the given payload,
|
||||||
|
// seq, and flags. Assumes no IP options and a 20-byte TCP header.
|
||||||
|
func buildTCPv4(seq uint32, flags byte, payload []byte) []byte {
|
||||||
|
return buildTCPv4Ports(1000, 2000, seq, flags, payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildTCPv4Ports is buildTCPv4 with caller-specified ports so tests can
|
||||||
|
// build distinct flows.
|
||||||
|
func buildTCPv4Ports(sport, dport uint16, seq uint32, flags byte, payload []byte) []byte {
|
||||||
|
const ipHdrLen = 20
|
||||||
|
const tcpHdrLen = 20
|
||||||
|
total := ipHdrLen + tcpHdrLen + len(payload)
|
||||||
|
pkt := make([]byte, total)
|
||||||
|
|
||||||
|
pkt[0] = 0x45
|
||||||
|
pkt[1] = 0x00
|
||||||
|
binary.BigEndian.PutUint16(pkt[2:4], uint16(total))
|
||||||
|
binary.BigEndian.PutUint16(pkt[4:6], 0)
|
||||||
|
binary.BigEndian.PutUint16(pkt[6:8], 0x4000)
|
||||||
|
pkt[8] = 64
|
||||||
|
pkt[9] = ipProtoTCP
|
||||||
|
copy(pkt[12:16], []byte{10, 0, 0, 1})
|
||||||
|
copy(pkt[16:20], []byte{10, 0, 0, 2})
|
||||||
|
|
||||||
|
binary.BigEndian.PutUint16(pkt[20:22], sport)
|
||||||
|
binary.BigEndian.PutUint16(pkt[22:24], dport)
|
||||||
|
binary.BigEndian.PutUint32(pkt[24:28], seq)
|
||||||
|
binary.BigEndian.PutUint32(pkt[28:32], 12345)
|
||||||
|
pkt[32] = 0x50
|
||||||
|
pkt[33] = flags
|
||||||
|
binary.BigEndian.PutUint16(pkt[34:36], 0xffff)
|
||||||
|
|
||||||
|
copy(pkt[40:], payload)
|
||||||
|
return pkt
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
tcpAck = 0x10
|
||||||
|
tcpPsh = 0x08
|
||||||
|
tcpSyn = 0x02
|
||||||
|
tcpFin = 0x01
|
||||||
|
tcpAckPsh = tcpAck | tcpPsh
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCoalescerPassthroughWhenGSOUnavailable(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: false}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
pkt := buildTCPv4(1000, tcpAck, []byte("hello"))
|
||||||
|
if err := c.Commit(pkt); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// No sync write — passthrough is deferred to Flush.
|
||||||
|
if len(w.writes) != 0 || len(w.gsoWrites) != 0 {
|
||||||
|
t.Fatalf("no Add-time writes: got writes=%d gso=%d", len(w.writes), len(w.gsoWrites))
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(w.writes) != 1 || len(w.gsoWrites) != 0 {
|
||||||
|
t.Fatalf("want single plain write, got writes=%d gso=%d", len(w.writes), len(w.gsoWrites))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCoalescerNonTCPPassthrough(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
pkt := make([]byte, 28)
|
||||||
|
pkt[0] = 0x45
|
||||||
|
binary.BigEndian.PutUint16(pkt[2:4], 28)
|
||||||
|
pkt[9] = 1
|
||||||
|
copy(pkt[12:16], []byte{10, 0, 0, 1})
|
||||||
|
copy(pkt[16:20], []byte{10, 0, 0, 2})
|
||||||
|
if err := c.Commit(pkt); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(w.writes) != 1 || len(w.gsoWrites) != 0 {
|
||||||
|
t.Fatalf("ICMP should pass through unchanged")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCoalescerSeedThenFlushAlone(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
pkt := buildTCPv4(1000, tcpAck, make([]byte, 1000))
|
||||||
|
if err := c.Commit(pkt); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(w.writes) != 0 || len(w.gsoWrites) != 0 {
|
||||||
|
t.Fatalf("unexpected output before flush")
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Single-segment flush now goes through WriteGSO with GSO_NONE
|
||||||
|
// (virtio NEEDS_CSUM lets the kernel fill in the L4 csum).
|
||||||
|
if len(w.gsoWrites) != 1 || len(w.writes) != 0 {
|
||||||
|
t.Fatalf("single-seg flush: writes=%d gso=%d", len(w.writes), len(w.gsoWrites))
|
||||||
|
}
|
||||||
|
g := w.gsoWrites[0]
|
||||||
|
if g.total() != 40+1000 {
|
||||||
|
t.Errorf("super total=%d want %d", g.total(), 40+1000)
|
||||||
|
}
|
||||||
|
if g.payLen() != 1000 {
|
||||||
|
t.Errorf("payLen=%d want 1000", g.payLen())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCoalescerCoalescesAdjacentACKs(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
pay := make([]byte, 1200)
|
||||||
|
if err := c.Commit(buildTCPv4(1000, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4(2200, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4(3400, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(w.gsoWrites) != 1 {
|
||||||
|
t.Fatalf("want 1 gso write, got %d (plain=%d)", len(w.gsoWrites), len(w.writes))
|
||||||
|
}
|
||||||
|
g := w.gsoWrites[0]
|
||||||
|
if g.gsoSize != 1200 {
|
||||||
|
t.Errorf("gsoSize=%d want 1200", g.gsoSize)
|
||||||
|
}
|
||||||
|
if len(g.hdr) != 40 {
|
||||||
|
t.Errorf("hdrLen=%d want 40", len(g.hdr))
|
||||||
|
}
|
||||||
|
if g.csumStart != 20 {
|
||||||
|
t.Errorf("csumStart=%d want 20", g.csumStart)
|
||||||
|
}
|
||||||
|
if len(g.pays) != 3 {
|
||||||
|
t.Errorf("pay count=%d want 3", len(g.pays))
|
||||||
|
}
|
||||||
|
if g.total() != 40+3*1200 {
|
||||||
|
t.Errorf("superpacket len=%d want %d", g.total(), 40+3*1200)
|
||||||
|
}
|
||||||
|
if tot := binary.BigEndian.Uint16(g.hdr[2:4]); int(tot) != g.total() {
|
||||||
|
t.Errorf("ip total_length=%d want %d", tot, g.total())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCoalescerRejectsSeqGap(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
pay := make([]byte, 1200)
|
||||||
|
if err := c.Commit(buildTCPv4(1000, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4(3000, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Each packet flushes as its own single-segment WriteGSO now.
|
||||||
|
if len(w.gsoWrites) != 2 || len(w.writes) != 0 {
|
||||||
|
t.Fatalf("seq gap: want 2 gso writes got writes=%d gso=%d", len(w.writes), len(w.gsoWrites))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCoalescerRejectsFlagMismatch(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
pay := make([]byte, 1200)
|
||||||
|
if err := c.Commit(buildTCPv4(1000, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// SYN|ACK is non-admissible. Must flush matching flow's slot (gso)
|
||||||
|
// and then plain-write the SYN packet itself.
|
||||||
|
syn := buildTCPv4(2200, tcpSyn|tcpAck, pay)
|
||||||
|
if err := c.Commit(syn); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(w.writes) != 1 || len(w.gsoWrites) != 1 {
|
||||||
|
t.Fatalf("flag mismatch: want 1 plain + 1 gso, got writes=%d gso=%d", len(w.writes), len(w.gsoWrites))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCoalescerRejectsFIN(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
fin := buildTCPv4(1000, tcpAck|tcpFin, []byte("x"))
|
||||||
|
if err := c.Commit(fin); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// FIN isn't admissible — passthrough as plain, no slot, no gso.
|
||||||
|
if len(w.writes) != 1 || len(w.gsoWrites) != 0 {
|
||||||
|
t.Fatalf("FIN should be passthrough, got writes=%d gso=%d", len(w.writes), len(w.gsoWrites))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCoalescerShortLastSegmentClosesChain(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
full := make([]byte, 1200)
|
||||||
|
half := make([]byte, 500)
|
||||||
|
if err := c.Commit(buildTCPv4(1000, tcpAck, full)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4(2200, tcpAck, half)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Chain now closed; next packet seeds a new slot on the same flow
|
||||||
|
// after flushing the old one.
|
||||||
|
if err := c.Commit(buildTCPv4(2700, tcpAck, full)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Expect two gso writes: the first two packets coalesced, then the
|
||||||
|
// third flushed alone (single-seg via GSO_NONE).
|
||||||
|
if len(w.gsoWrites) != 2 {
|
||||||
|
t.Fatalf("want 2 gso writes got %d", len(w.gsoWrites))
|
||||||
|
}
|
||||||
|
if len(w.writes) != 0 {
|
||||||
|
t.Fatalf("want 0 plain writes got %d", len(w.writes))
|
||||||
|
}
|
||||||
|
if w.gsoWrites[0].gsoSize != 1200 {
|
||||||
|
t.Errorf("gsoSize=%d want 1200", w.gsoWrites[0].gsoSize)
|
||||||
|
}
|
||||||
|
if got, want := w.gsoWrites[0].total(), 40+1200+500; got != want {
|
||||||
|
t.Errorf("super len=%d want %d", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCoalescerPSHFinalizesChain(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
pay := make([]byte, 1200)
|
||||||
|
if err := c.Commit(buildTCPv4(1000, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4(2200, tcpAckPsh, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4(3400, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// First two coalesce; the third seeds a fresh slot that flushes alone.
|
||||||
|
if len(w.gsoWrites) != 2 {
|
||||||
|
t.Fatalf("want 2 gso writes got %d", len(w.gsoWrites))
|
||||||
|
}
|
||||||
|
if len(w.writes) != 0 {
|
||||||
|
t.Fatalf("want 0 plain writes got %d", len(w.writes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCoalescerPropagatesPSHFromAppended ensures that when an appended
|
||||||
|
// segment carries PSH (or is short, sealing the chain), the PSH bit ends
|
||||||
|
// up in the emitted superpacket's TCP flags. The kernel TSO path keeps
|
||||||
|
// PSH only on the last segment iff the input header has it set; if the
|
||||||
|
// coalescer drops it the sender's push signal never reaches the receiver.
|
||||||
|
func TestCoalescerPropagatesPSHFromAppended(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
pay := make([]byte, 1200)
|
||||||
|
// Seed has no PSH; second segment carries PSH and seals the chain.
|
||||||
|
if err := c.Commit(buildTCPv4(1000, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4(2200, tcpAckPsh, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Flush(0); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(w.gsoWrites) != 1 {
|
||||||
|
t.Fatalf("want 1 gso write got %d", len(w.gsoWrites))
|
||||||
|
}
|
||||||
|
g := w.gsoWrites[0]
|
||||||
|
const ipHdrLen = 20
|
||||||
|
flags := g.hdr[ipHdrLen+13]
|
||||||
|
if flags&tcpPsh == 0 {
|
||||||
|
t.Fatalf("PSH lost from coalesced superpacket: flags=0x%02x", flags)
|
||||||
|
}
|
||||||
|
if flags&tcpAck == 0 {
|
||||||
|
t.Fatalf("ACK missing from coalesced superpacket: flags=0x%02x", flags)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCoalescerRejectsDifferentFlow(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
pay := make([]byte, 1200)
|
||||||
|
p1 := buildTCPv4(1000, tcpAck, pay)
|
||||||
|
p2 := buildTCPv4(2200, tcpAck, pay)
|
||||||
|
binary.BigEndian.PutUint16(p2[20:22], 9999)
|
||||||
|
if err := c.Commit(p1); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(p2); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Two independent flows, each flushes its own single-segment WriteGSO.
|
||||||
|
if len(w.gsoWrites) != 2 || len(w.writes) != 0 {
|
||||||
|
t.Fatalf("diff flow: want 2 gso writes got writes=%d gso=%d", len(w.writes), len(w.gsoWrites))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCoalescerRejectsIPOptions(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
pay := make([]byte, 500)
|
||||||
|
pkt := buildTCPv4(1000, tcpAck, pay)
|
||||||
|
// Bump IHL to 6 to simulate 4 bytes of IP options. Don't actually add
|
||||||
|
// bytes — parser should bail before it matters.
|
||||||
|
pkt[0] = 0x46
|
||||||
|
if err := c.Commit(pkt); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Non-admissible parse → passthrough as plain.
|
||||||
|
if len(w.writes) != 1 || len(w.gsoWrites) != 0 {
|
||||||
|
t.Fatalf("IP options should passthrough, got writes=%d gso=%d", len(w.writes), len(w.gsoWrites))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCoalescerCapBySegments(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
pay := make([]byte, 512)
|
||||||
|
seq := uint32(1000)
|
||||||
|
for i := 0; i < tcpCoalesceMaxSegs+5; i++ {
|
||||||
|
if err := c.Commit(buildTCPv4(seq, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
seq += uint32(len(pay))
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, g := range w.gsoWrites {
|
||||||
|
segs := len(g.pays)
|
||||||
|
if segs > tcpCoalesceMaxSegs {
|
||||||
|
t.Fatalf("super exceeded seg cap: %d > %d", segs, tcpCoalesceMaxSegs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCoalescerMultipleFlowsInSameBatch proves two interleaved bulk TCP
|
||||||
|
// flows coalesce independently in a single Flush.
|
||||||
|
func TestCoalescerMultipleFlowsInSameBatch(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
pay := make([]byte, 1200)
|
||||||
|
|
||||||
|
// Flow A: sport 1000. Flow B: sport 3000.
|
||||||
|
if err := c.Commit(buildTCPv4Ports(1000, 2000, 100, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4Ports(3000, 2000, 500, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4Ports(1000, 2000, 1300, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4Ports(3000, 2000, 1700, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4Ports(1000, 2000, 2500, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4Ports(3000, 2000, 2900, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(w.gsoWrites) != 2 {
|
||||||
|
t.Fatalf("want 2 gso writes (one per flow), got %d", len(w.gsoWrites))
|
||||||
|
}
|
||||||
|
if len(w.writes) != 0 {
|
||||||
|
t.Fatalf("want no plain writes, got %d", len(w.writes))
|
||||||
|
}
|
||||||
|
// Each superpacket should carry 3 segments.
|
||||||
|
for i, g := range w.gsoWrites {
|
||||||
|
if len(g.pays) != 3 {
|
||||||
|
t.Errorf("gso[%d]: segs=%d want 3", i, len(g.pays))
|
||||||
|
}
|
||||||
|
if g.gsoSize != 1200 {
|
||||||
|
t.Errorf("gso[%d]: gsoSize=%d want 1200", i, g.gsoSize)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Verify each superpacket carries the source port it was seeded with.
|
||||||
|
seenSports := map[uint16]bool{}
|
||||||
|
for _, g := range w.gsoWrites {
|
||||||
|
sp := binary.BigEndian.Uint16(g.hdr[20:22])
|
||||||
|
seenSports[sp] = true
|
||||||
|
}
|
||||||
|
if !seenSports[1000] || !seenSports[3000] {
|
||||||
|
t.Errorf("expected superpackets for sports 1000 and 3000, got %v", seenSports)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCoalescerPreservesArrivalOrder confirms that with passthrough and
|
||||||
|
// coalesced events both queued, Flush emits them in Add order rather than
|
||||||
|
// writing passthrough packets synchronously.
|
||||||
|
func TestCoalescerPreservesArrivalOrder(t *testing.T) {
|
||||||
|
w := &orderedFakeWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
// Sequence: coalesceable TCP, ICMP (passthrough), coalesceable TCP on
|
||||||
|
// a different flow. Expected emit order: gso(X), plain(ICMP), gso(Y).
|
||||||
|
pay := make([]byte, 1200)
|
||||||
|
if err := c.Commit(buildTCPv4Ports(1000, 2000, 100, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
icmp := make([]byte, 28)
|
||||||
|
icmp[0] = 0x45
|
||||||
|
binary.BigEndian.PutUint16(icmp[2:4], 28)
|
||||||
|
icmp[9] = 1
|
||||||
|
copy(icmp[12:16], []byte{10, 0, 0, 1})
|
||||||
|
copy(icmp[16:20], []byte{10, 0, 0, 3})
|
||||||
|
if err := c.Commit(icmp); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4Ports(3000, 2000, 500, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Nothing should have hit the writer synchronously.
|
||||||
|
if len(w.events) != 0 {
|
||||||
|
t.Fatalf("Add emitted events synchronously: %v", w.events)
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got, want := w.events, []string{"gso", "plain", "gso"}; !stringSliceEq(got, want) {
|
||||||
|
t.Fatalf("flush order=%v want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// orderedFakeWriter records only the sequence of call types so tests can
|
||||||
|
// assert arrival order without inspecting bytes.
|
||||||
|
type orderedFakeWriter struct {
|
||||||
|
gsoEnabled bool
|
||||||
|
events []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *orderedFakeWriter) Write(p []byte) (int, error) {
|
||||||
|
w.events = append(w.events, "plain")
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *orderedFakeWriter) WriteGSO(hdr []byte, transportHdr []byte, pays [][]byte) error {
|
||||||
|
w.events = append(w.events, "gso")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *orderedFakeWriter) GSOSupported() bool { return w.gsoEnabled }
|
||||||
|
|
||||||
|
func stringSliceEq(a, b []string) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i := range a {
|
||||||
|
if a[i] != b[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCoalescerInterleavedFlowsPreserveOrdering checks that a non-admissible
|
||||||
|
// packet (SYN) mid-flow only flushes its own flow, not others.
|
||||||
|
func TestCoalescerInterleavedFlowsPreserveOrdering(t *testing.T) {
|
||||||
|
w := &fakeTunWriter{gsoEnabled: true}
|
||||||
|
c := NewTCPCoalescer(w)
|
||||||
|
pay := make([]byte, 1200)
|
||||||
|
|
||||||
|
// Flow A two segments.
|
||||||
|
if err := c.Commit(buildTCPv4Ports(1000, 2000, 100, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4Ports(1000, 2000, 1300, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Flow B two segments.
|
||||||
|
if err := c.Commit(buildTCPv4Ports(3000, 2000, 500, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Commit(buildTCPv4Ports(3000, 2000, 1700, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Flow A SYN (non-admissible) — must flush only flow A's slot.
|
||||||
|
syn := buildTCPv4Ports(1000, 2000, 9999, tcpSyn|tcpAck, pay)
|
||||||
|
if err := c.Commit(syn); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Flow B continues — should still be coalesced with its seed.
|
||||||
|
if err := c.Commit(buildTCPv4Ports(3000, 2000, 2900, tcpAck, pay)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := c.Flush(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Expected:
|
||||||
|
// - 1 gso for flow A (first 2 segments)
|
||||||
|
// - 1 plain for flow A SYN
|
||||||
|
// - 1 gso for flow B (3 segments)
|
||||||
|
if len(w.gsoWrites) != 2 {
|
||||||
|
t.Fatalf("want 2 gso writes, got %d", len(w.gsoWrites))
|
||||||
|
}
|
||||||
|
if len(w.writes) != 1 {
|
||||||
|
t.Fatalf("want 1 plain write (SYN), got %d", len(w.writes))
|
||||||
|
}
|
||||||
|
// Find the 3-segment gso (flow B) and the 2-segment gso (flow A).
|
||||||
|
var segCounts []int
|
||||||
|
for _, g := range w.gsoWrites {
|
||||||
|
segCounts = append(segCounts, len(g.pays))
|
||||||
|
}
|
||||||
|
if !(segCounts[0] == 2 && segCounts[1] == 3) && !(segCounts[0] == 3 && segCounts[1] == 2) {
|
||||||
|
t.Errorf("unexpected segment counts: %v (want 2 and 3)", segCounts)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package batch
|
||||||
|
|
||||||
|
import "net/netip"
|
||||||
|
|
||||||
|
const SendBatchCap = 128
|
||||||
|
|
||||||
|
// SendBatch accumulates encrypted UDP packets for potential TX offloading.
|
||||||
|
// One SendBatch is owned by each listenIn goroutine; no locking is needed.
|
||||||
|
// The backing storage holds up to batchCap packets of slotCap bytes each;
|
||||||
|
// bufs and dsts are parallel slices of committed slots.
|
||||||
|
type SendBatch struct {
|
||||||
|
bufs [][]byte
|
||||||
|
dsts []netip.AddrPort
|
||||||
|
backing []byte
|
||||||
|
slotCap int
|
||||||
|
batchCap int
|
||||||
|
nextSlot int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSendBatch(batchCap, slotCap int) *SendBatch {
|
||||||
|
return &SendBatch{
|
||||||
|
bufs: make([][]byte, 0, batchCap),
|
||||||
|
dsts: make([]netip.AddrPort, 0, batchCap),
|
||||||
|
backing: make([]byte, batchCap*slotCap),
|
||||||
|
slotCap: slotCap,
|
||||||
|
batchCap: batchCap,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *SendBatch) Next() []byte {
|
||||||
|
if b.nextSlot >= b.batchCap {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
start := b.nextSlot * b.slotCap
|
||||||
|
return b.backing[start : start : start+b.slotCap] //set len to 0 but cap to slotCap
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *SendBatch) Commit(n int, dst netip.AddrPort) {
|
||||||
|
start := b.nextSlot * b.slotCap
|
||||||
|
b.bufs = append(b.bufs, b.backing[start:start+n])
|
||||||
|
b.dsts = append(b.dsts, dst)
|
||||||
|
b.nextSlot++
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *SendBatch) Reset() {
|
||||||
|
b.bufs = b.bufs[:0]
|
||||||
|
b.dsts = b.dsts[:0]
|
||||||
|
b.nextSlot = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *SendBatch) Len() int {
|
||||||
|
return len(b.bufs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *SendBatch) Cap() int {
|
||||||
|
return b.batchCap
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *SendBatch) Get() ([][]byte, []netip.AddrPort) {
|
||||||
|
return b.bufs, b.dsts
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package batch
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/netip"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSendBatchBookkeeping(t *testing.T) {
|
||||||
|
b := NewSendBatch(4, 32)
|
||||||
|
if b.Len() != 0 || b.Cap() != 4 {
|
||||||
|
t.Fatalf("fresh batch: len=%d cap=%d", b.Len(), b.Cap())
|
||||||
|
}
|
||||||
|
|
||||||
|
ap := netip.MustParseAddrPort("10.0.0.1:4242")
|
||||||
|
for i := 0; i < 4; i++ {
|
||||||
|
slot := b.Next()
|
||||||
|
if slot == nil {
|
||||||
|
t.Fatalf("slot %d: Next returned nil before cap", i)
|
||||||
|
}
|
||||||
|
if cap(slot) != 32 || len(slot) != 0 {
|
||||||
|
t.Fatalf("slot %d: got len=%d cap=%d want len=0 cap=32", i, len(slot), cap(slot))
|
||||||
|
}
|
||||||
|
// Write a marker byte.
|
||||||
|
slot = append(slot, byte(i), byte(i+1), byte(i+2))
|
||||||
|
b.Commit(len(slot), ap)
|
||||||
|
}
|
||||||
|
if b.Next() != nil {
|
||||||
|
t.Fatalf("Next should return nil when full")
|
||||||
|
}
|
||||||
|
if b.Len() != 4 {
|
||||||
|
t.Fatalf("Len=%d want 4", b.Len())
|
||||||
|
}
|
||||||
|
for i, buf := range b.bufs {
|
||||||
|
if len(buf) != 3 || buf[0] != byte(i) {
|
||||||
|
t.Errorf("buf %d: %x", i, buf)
|
||||||
|
}
|
||||||
|
if b.dsts[i] != ap {
|
||||||
|
t.Errorf("dst %d: got %v want %v", i, b.dsts[i], ap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset returns empty and Next works again.
|
||||||
|
b.Reset()
|
||||||
|
if b.Len() != 0 {
|
||||||
|
t.Fatalf("after Reset Len=%d want 0", b.Len())
|
||||||
|
}
|
||||||
|
slot := b.Next()
|
||||||
|
if slot == nil || cap(slot) != 32 {
|
||||||
|
t.Fatalf("after Reset Next nil or wrong cap: %v cap=%d", slot == nil, cap(slot))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSendBatchSlotsDoNotOverlap(t *testing.T) {
|
||||||
|
b := NewSendBatch(3, 8)
|
||||||
|
ap := netip.MustParseAddrPort("10.0.0.1:80")
|
||||||
|
|
||||||
|
// Fill three slots, each with its own sentinel byte.
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
s := b.Next()
|
||||||
|
s = append(s, byte(0xA0+i), byte(0xB0+i))
|
||||||
|
b.Commit(len(s), ap)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, buf := range b.bufs {
|
||||||
|
if buf[0] != byte(0xA0+i) || buf[1] != byte(0xB0+i) {
|
||||||
|
t.Errorf("slot %d corrupted: %x", i, buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+9
-12
@@ -4,24 +4,21 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// defaultBatchBufSize is the per-Queue scratch size for Read on backends
|
||||||
|
// that don't do TSO segmentation. 65535 covers any single IP packet.
|
||||||
|
const defaultBatchBufSize = 65535
|
||||||
|
|
||||||
type Device interface {
|
type Device interface {
|
||||||
io.ReadWriteCloser
|
io.Closer
|
||||||
Activate() error
|
Activate() error
|
||||||
Networks() []netip.Prefix
|
Networks() []netip.Prefix
|
||||||
Name() string
|
Name() string
|
||||||
RoutesFor(netip.Addr) routing.Gateways
|
RoutesFor(netip.Addr) routing.Gateways
|
||||||
SupportsMultiqueue() bool
|
SupportsMultiqueue() bool //todo remove?
|
||||||
NewMultiQueueReader() (io.ReadWriteCloser, error)
|
NewMultiQueueReader() error
|
||||||
// SupportsPerPeerMTU reports whether SetPeerMTU is implemented for real on
|
Readers() []tio.Queue
|
||||||
// this platform. PMTUD requires this; the manager will refuse to enable when
|
|
||||||
// false even if the operator set tun.max_mtu, because a discovered MTU we
|
|
||||||
// can't actually install does the operator no good.
|
|
||||||
SupportsPerPeerMTU() bool
|
|
||||||
// SetPeerMTU installs a per-peer MTU on the routing table so the kernel will
|
|
||||||
// surface PTB / EMSGSIZE for inside packets to that peer that would exceed mtu.
|
|
||||||
// Pass mtu=0 to remove the override and let the device default apply.
|
|
||||||
SetPeerMTU(addr netip.Addr, mtu int) error
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ package overlaytest
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -31,28 +31,24 @@ func (NoopTun) Name() string {
|
|||||||
return "noop"
|
return "noop"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (NoopTun) Read([]byte) (int, error) {
|
func (NoopTun) Read() ([][]byte, error) {
|
||||||
return 0, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (NoopTun) Write([]byte) (int, error) {
|
func (NoopTun) Write([]byte) (int, error) {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (NoopTun) SupportsPerPeerMTU() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (NoopTun) SetPeerMTU(addr netip.Addr, mtu int) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (NoopTun) SupportsMultiqueue() bool {
|
func (NoopTun) SupportsMultiqueue() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (NoopTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (NoopTun) NewMultiQueueReader() error {
|
||||||
return nil, errors.New("unsupported")
|
return errors.New("unsupported")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (NoopTun) Readers() []tio.Queue {
|
||||||
|
return []tio.Queue{NoopTun{}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (NoopTun) Close() error {
|
func (NoopTun) Close() error {
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package tio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
type offloadContainer struct {
|
||||||
|
pq []*Offload
|
||||||
|
// pqi is exactly the same as pq, but stored as the interface type
|
||||||
|
pqi []Queue
|
||||||
|
shutdownFd int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewOffloadContainer() (Container, error) {
|
||||||
|
shutdownFd, err := unix.Eventfd(0, unix.EFD_NONBLOCK|unix.EFD_CLOEXEC)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create eventfd: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
out := &offloadContainer{
|
||||||
|
pq: []*Offload{},
|
||||||
|
pqi: []Queue{},
|
||||||
|
shutdownFd: shutdownFd,
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *offloadContainer) Queues() []Queue {
|
||||||
|
return c.pqi
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *offloadContainer) Add(fd int) error {
|
||||||
|
x, err := newOffload(fd, c.shutdownFd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.pq = append(c.pq, x)
|
||||||
|
c.pqi = append(c.pqi, x)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *offloadContainer) wakeForShutdown() error {
|
||||||
|
var buf [8]byte
|
||||||
|
binary.NativeEndian.PutUint64(buf[:], 1)
|
||||||
|
_, err := unix.Write(c.shutdownFd, buf[:])
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *offloadContainer) Close() error {
|
||||||
|
errs := []error{}
|
||||||
|
|
||||||
|
// Signal all readers blocked in poll to wake up and exit
|
||||||
|
if err := c.wakeForShutdown(); err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, x := range c.pq {
|
||||||
|
if err := x.Close(); err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.Join(errs...)
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package tio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
type pollContainer struct {
|
||||||
|
pq []*Poll
|
||||||
|
// pqi is exactly the same as pq, but stored as the interface type
|
||||||
|
pqi []Queue
|
||||||
|
shutdownFd int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPollContainer() (Container, error) {
|
||||||
|
shutdownFd, err := unix.Eventfd(0, unix.EFD_NONBLOCK|unix.EFD_CLOEXEC)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create eventfd: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
out := &pollContainer{
|
||||||
|
pq: []*Poll{},
|
||||||
|
pqi: []Queue{},
|
||||||
|
shutdownFd: shutdownFd,
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *pollContainer) Queues() []Queue {
|
||||||
|
return c.pqi
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *pollContainer) Add(fd int) error {
|
||||||
|
x, err := newPoll(fd, c.shutdownFd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.pq = append(c.pq, x)
|
||||||
|
c.pqi = append(c.pqi, x)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *pollContainer) wakeForShutdown() error {
|
||||||
|
var buf [8]byte
|
||||||
|
binary.NativeEndian.PutUint64(buf[:], 1)
|
||||||
|
_, err := unix.Write(int(c.shutdownFd), buf[:])
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *pollContainer) Close() error {
|
||||||
|
errs := []error{}
|
||||||
|
|
||||||
|
if err := c.wakeForShutdown(); err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, x := range c.pq {
|
||||||
|
if err := x.Close(); err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.Join(errs...)
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package tio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// defaultBatchBufSize is the per-Queue scratch size for Read on backends
|
||||||
|
// that don't do TSO segmentation. 65535 covers any single IP packet.
|
||||||
|
const defaultBatchBufSize = 65535
|
||||||
|
|
||||||
|
// Container holds one or many Queue objects and helps close them in an orderly way
|
||||||
|
type Container interface {
|
||||||
|
io.Closer
|
||||||
|
Queues() []Queue
|
||||||
|
|
||||||
|
// Add takes a tun fd, adds it to the container, and prepares it for use as a Queue
|
||||||
|
Add(fd int) error
|
||||||
|
|
||||||
|
io.Closer
|
||||||
|
}
|
||||||
|
|
||||||
|
// Queue is a readable/writable Poll queue. One Queue is driven by a single
|
||||||
|
// read goroutine plus concurrent writers (see Write / WriteReject below).
|
||||||
|
type Queue interface {
|
||||||
|
io.Closer
|
||||||
|
|
||||||
|
// Read returns one or more packets. The returned slices are borrowed
|
||||||
|
// from the Queue's internal buffer and are only valid until the next
|
||||||
|
// Read or Close on this Queue - callers must encrypt or copy each
|
||||||
|
// slice before the next call. Not safe for concurrent Reads.
|
||||||
|
Read() ([][]byte, error)
|
||||||
|
|
||||||
|
// Write emits a single packet on the plaintext (outside→inside)
|
||||||
|
// delivery path. Not safe for concurrent Writes.
|
||||||
|
Write(p []byte) (int, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GSOWriter is implemented by Queues that can emit a TCP TSO superpacket
|
||||||
|
// assembled from a header prefix plus one or more borrowed payload
|
||||||
|
// fragments, in a single vectored write (writev with a leading
|
||||||
|
// virtio_net_hdr). This lets the coalescer avoid copying payload bytes
|
||||||
|
// between the caller's decrypt buffer and the TUN. Backends without GSO
|
||||||
|
// support return false from GSOSupported and coalescing is skipped.
|
||||||
|
//
|
||||||
|
// hdr contains the IPv4/IPv6 + TCP header prefix (mutable - callers will
|
||||||
|
// have filled in total length and pseudo-header partial). pays are
|
||||||
|
// non-overlapping payload fragments whose concatenation is the full
|
||||||
|
// superpacket payload; they are read-only from the writer's perspective
|
||||||
|
// and must remain valid until the call returns. gsoSize is the MSS:
|
||||||
|
// every segment except possibly the last is exactly that many bytes.
|
||||||
|
// csumStart is the byte offset where the TCP header begins within hdr.
|
||||||
|
//
|
||||||
|
// # TODO fold into Queue
|
||||||
|
//
|
||||||
|
// hdr's TCP checksum field must already hold the pseudo-header partial
|
||||||
|
// sum (single-fold, not inverted), per virtio NEEDS_CSUM semantics.
|
||||||
|
type GSOWriter interface {
|
||||||
|
// WriteGSO emits a TCP TSO superpacket in a single writev. hdr is the
|
||||||
|
// IPv4/IPv6 + TCP header prefix (already finalized — total length, IP csum,
|
||||||
|
// and TCP pseudo-header partial set by the caller). pays are payload
|
||||||
|
// fragments whose concatenation forms the full coalesced payload; each
|
||||||
|
// slice is read-only and must stay valid until return.
|
||||||
|
// every segment in pays except possibly the last is exactly the same size.
|
||||||
|
// csumStart is the byte offset where the TCP header begins within hdr.
|
||||||
|
WriteGSO(hdr []byte, transportHdr []byte, pays [][]byte) error
|
||||||
|
GSOSupported() bool
|
||||||
|
}
|
||||||
@@ -0,0 +1,353 @@
|
|||||||
|
package tio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"syscall"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Space for segmented output. Worst case is many small segments, each paying
|
||||||
|
// an IP+TCP header. Should be a multiple of 64KiB.
|
||||||
|
// const tunSegBufSize = 0xffff * 8 TODO larger? config?
|
||||||
|
const tunSegBufSize = 131072
|
||||||
|
|
||||||
|
// tunSegBufCap is the total size we allocate for the per-reader segment
|
||||||
|
// buffer. It is sized as one worst-case TSO superpacket (tunSegBufSize) plus
|
||||||
|
// the same again as drain headroom so a Read wake can accumulate
|
||||||
|
// additional packets after an initial big read without overflowing.
|
||||||
|
const tunSegBufCap = tunSegBufSize * 2
|
||||||
|
|
||||||
|
// tunDrainCap caps how many packets a single Read will accumulate via
|
||||||
|
// the post-wake drain loop. Sized to soak up a burst of small ACKs while
|
||||||
|
// bounding how much work a single caller holds before handing off.
|
||||||
|
const tunDrainCap = 64 //256
|
||||||
|
|
||||||
|
// gsoInitialPayIovs is the starting capacity (in payload fragments) of
|
||||||
|
// Offload.gsoIovs. Sized to cover the default coalesce segment cap without
|
||||||
|
// any reallocations.
|
||||||
|
const gsoInitialPayIovs = 66
|
||||||
|
|
||||||
|
// validVnetHdr is the 10-byte virtio_net_hdr we prepend to every non-GSO TUN
|
||||||
|
// write. Only flag set is VIRTIO_NET_HDR_F_DATA_VALID, which marks the skb
|
||||||
|
// CHECKSUM_UNNECESSARY so the receiving network stack skips L4 checks
|
||||||
|
// verification. All packets that reach the plain Write paths
|
||||||
|
// already carry a valid L4 checksum (either supplied by a remote peer whose
|
||||||
|
// ciphertext we AEAD-authenticated, or produced by finishChecksum during TSO
|
||||||
|
// segmentation, or built locally by CreateRejectPacket), so trusting them is
|
||||||
|
// safe.
|
||||||
|
var validVnetHdr = [virtioNetHdrLen]byte{unix.VIRTIO_NET_HDR_F_DATA_VALID}
|
||||||
|
|
||||||
|
// Offload wraps a TUN file descriptor with poll-based reads. The FD provided will be changed to non-blocking.
|
||||||
|
// A shared eventfd allows Close to wake all readers blocked in poll.
|
||||||
|
type Offload struct {
|
||||||
|
fd int
|
||||||
|
shutdownFd int
|
||||||
|
readPoll [2]unix.PollFd
|
||||||
|
writePoll [2]unix.PollFd
|
||||||
|
writeLock sync.Mutex //there's more than one potential write source per-routine, so we need this to protect writePoll
|
||||||
|
closed atomic.Bool
|
||||||
|
readBuf []byte // scratch for a single raw read (virtio hdr + superpacket)
|
||||||
|
segBuf []byte // backing store for segmented output
|
||||||
|
segOff int // cursor into segBuf for the current Read drain
|
||||||
|
pending [][]byte // segments returned from the most recent Read
|
||||||
|
|
||||||
|
// gsoHdrBuf is a per-queue 10-byte scratch for the virtio_net_hdr emitted
|
||||||
|
// by WriteGSO. Separate from validVnetHdr so a concurrent non-GSO Write on
|
||||||
|
// another queue never observes a half-written header.
|
||||||
|
gsoHdrBuf [virtioNetHdrLen]byte
|
||||||
|
// gsoIovs is the writev iovec scratch for WriteGSO. Sized to hold the
|
||||||
|
// virtio header + IP/TCP header + up to gsoInitialPayIovs payload
|
||||||
|
// fragments; grown on demand if a coalescer pushes more.
|
||||||
|
gsoIovs []unix.Iovec
|
||||||
|
}
|
||||||
|
|
||||||
|
func newOffload(fd int, shutdownFd int) (*Offload, error) {
|
||||||
|
if err := unix.SetNonblock(fd, true); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to set tun fd non-blocking: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
out := &Offload{
|
||||||
|
fd: fd,
|
||||||
|
shutdownFd: shutdownFd,
|
||||||
|
closed: atomic.Bool{},
|
||||||
|
readBuf: make([]byte, virtioNetHdrLen+tunReadBufSize),
|
||||||
|
readPoll: [2]unix.PollFd{
|
||||||
|
{Fd: int32(fd), Events: unix.POLLIN},
|
||||||
|
{Fd: int32(shutdownFd), Events: unix.POLLIN},
|
||||||
|
},
|
||||||
|
writePoll: [2]unix.PollFd{
|
||||||
|
{Fd: int32(fd), Events: unix.POLLOUT},
|
||||||
|
{Fd: int32(shutdownFd), Events: unix.POLLIN},
|
||||||
|
},
|
||||||
|
writeLock: sync.Mutex{},
|
||||||
|
|
||||||
|
segBuf: make([]byte, tunSegBufCap),
|
||||||
|
gsoIovs: make([]unix.Iovec, 2, 2+gsoInitialPayIovs),
|
||||||
|
}
|
||||||
|
|
||||||
|
out.gsoIovs[0].Base = &out.gsoHdrBuf[0]
|
||||||
|
out.gsoIovs[0].SetLen(virtioNetHdrLen)
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Offload) blockOnRead() error {
|
||||||
|
const problemFlags = unix.POLLHUP | unix.POLLNVAL | unix.POLLERR
|
||||||
|
var err error
|
||||||
|
for {
|
||||||
|
_, err = unix.Poll(r.readPoll[:], -1)
|
||||||
|
if err != unix.EINTR {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//always reset these!
|
||||||
|
tunEvents := r.readPoll[0].Revents
|
||||||
|
shutdownEvents := r.readPoll[1].Revents
|
||||||
|
r.readPoll[0].Revents = 0
|
||||||
|
r.readPoll[1].Revents = 0
|
||||||
|
//do the err check before trusting the potentially bogus bits we just got
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if shutdownEvents&(unix.POLLIN|problemFlags) != 0 {
|
||||||
|
return os.ErrClosed
|
||||||
|
} else if tunEvents&problemFlags != 0 {
|
||||||
|
return os.ErrClosed
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Offload) blockOnWrite() error {
|
||||||
|
const problemFlags = unix.POLLHUP | unix.POLLNVAL | unix.POLLERR
|
||||||
|
var err error
|
||||||
|
for {
|
||||||
|
_, err = unix.Poll(r.writePoll[:], -1)
|
||||||
|
if err != unix.EINTR {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//always reset these!
|
||||||
|
r.writeLock.Lock()
|
||||||
|
tunEvents := r.writePoll[0].Revents
|
||||||
|
shutdownEvents := r.writePoll[1].Revents
|
||||||
|
r.writePoll[0].Revents = 0
|
||||||
|
r.writePoll[1].Revents = 0
|
||||||
|
r.writeLock.Unlock()
|
||||||
|
//do the err check before trusting the potentially bogus bits we just got
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if shutdownEvents&(unix.POLLIN|problemFlags) != 0 {
|
||||||
|
return os.ErrClosed
|
||||||
|
} else if tunEvents&problemFlags != 0 {
|
||||||
|
return os.ErrClosed
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Offload) readRaw(buf []byte) (int, error) {
|
||||||
|
for {
|
||||||
|
if n, err := unix.Read(r.fd, buf); err == nil {
|
||||||
|
return n, nil
|
||||||
|
} else if err == unix.EAGAIN {
|
||||||
|
if err = r.blockOnRead(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
} else if err == unix.EINTR {
|
||||||
|
continue
|
||||||
|
} else if err == unix.EBADF {
|
||||||
|
return 0, os.ErrClosed
|
||||||
|
} else {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read reads one or more superpackets from the tun and returns the
|
||||||
|
// resulting packets. The first read blocks via poll; once the fd is known
|
||||||
|
// readable we drain additional packets non-blocking until the kernel queue
|
||||||
|
// is empty (EAGAIN), we've collected tunDrainCap packets, or we're out of
|
||||||
|
// segBuf headroom. This amortizes the poll wake over bursts of small
|
||||||
|
// packets (e.g. TCP ACKs). Slices point into the Offload's internal buffers
|
||||||
|
// and are only valid until the next Read or Close on this Queue.
|
||||||
|
func (r *Offload) Read() ([][]byte, error) {
|
||||||
|
r.pending = r.pending[:0]
|
||||||
|
r.segOff = 0
|
||||||
|
|
||||||
|
// Initial (blocking) read. Retry on decode errors so a single bad
|
||||||
|
// packet does not stall the reader.
|
||||||
|
for {
|
||||||
|
n, err := r.readRaw(r.readBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := r.decodeRead(n); err != nil {
|
||||||
|
// Drop and read again — a bad packet should not kill the reader.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Drain: non-blocking reads until the kernel queue is empty, the drain
|
||||||
|
// cap is reached, or segBuf no longer has room for another worst-case
|
||||||
|
// superpacket.
|
||||||
|
for len(r.pending) < tunDrainCap && tunSegBufCap-r.segOff >= tunSegBufSize {
|
||||||
|
n, err := unix.Read(r.fd, r.readBuf)
|
||||||
|
if err != nil {
|
||||||
|
// EAGAIN / EINTR / anything else: stop draining. We already
|
||||||
|
// have a valid batch from the first read.
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if n <= 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err := r.decodeRead(n); err != nil {
|
||||||
|
// Drop this packet and stop the drain; we'd rather hand off
|
||||||
|
// what we have than keep spinning here.
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.pending, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// decodeRead decodes the virtio header plus payload in r.readBuf[:n], appends
|
||||||
|
// the segments to r.pending, and advances r.segOff by the total scratch used.
|
||||||
|
func (r *Offload) decodeRead(n int) error {
|
||||||
|
if n < virtioNetHdrLen {
|
||||||
|
return fmt.Errorf("short tun read: %d < %d", n, virtioNetHdrLen)
|
||||||
|
}
|
||||||
|
var hdr VirtioNetHdr
|
||||||
|
hdr.decode(r.readBuf[:virtioNetHdrLen])
|
||||||
|
before := len(r.pending)
|
||||||
|
if err := segmentInto(r.readBuf[virtioNetHdrLen:n], hdr, &r.pending, r.segBuf[r.segOff:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for k := before; k < len(r.pending); k++ {
|
||||||
|
r.segOff += len(r.pending[k])
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Offload) Write(buf []byte) (int, error) {
|
||||||
|
iovs := [2]unix.Iovec{
|
||||||
|
{Base: &validVnetHdr[0]},
|
||||||
|
{Base: &buf[0]},
|
||||||
|
}
|
||||||
|
iovs[0].SetLen(virtioNetHdrLen)
|
||||||
|
iovs[1].SetLen(len(buf))
|
||||||
|
return r.writeWithScratch(buf, &iovs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Offload) writeWithScratch(buf []byte, iovs *[2]unix.Iovec) (int, error) {
|
||||||
|
if len(buf) == 0 {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
// Point the payload iovec at the caller's buffer. iovs[0] is pre-wired
|
||||||
|
// to validVnetHdr during Offload construction so we don't rebuild it here.
|
||||||
|
iovs[1].Base = &buf[0]
|
||||||
|
iovs[1].SetLen(len(buf))
|
||||||
|
return r.rawWrite(unsafe.Slice(&iovs[0], len(iovs)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Offload) rawWrite(iovs []unix.Iovec) (int, error) {
|
||||||
|
for {
|
||||||
|
n, _, errno := syscall.Syscall(unix.SYS_WRITEV, uintptr(r.fd), uintptr(unsafe.Pointer(&iovs[0])), uintptr(len(iovs)))
|
||||||
|
if errno == 0 {
|
||||||
|
if int(n) < virtioNetHdrLen {
|
||||||
|
return 0, io.ErrShortWrite
|
||||||
|
}
|
||||||
|
return int(n) - virtioNetHdrLen, nil
|
||||||
|
}
|
||||||
|
if errno == unix.EAGAIN {
|
||||||
|
if err := r.blockOnWrite(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if errno == unix.EINTR {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if errno == unix.EBADF {
|
||||||
|
return 0, os.ErrClosed
|
||||||
|
}
|
||||||
|
return 0, errno
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GSOSupported reports whether this queue was opened with IFF_VNET_HDR and
|
||||||
|
// can accept WriteGSO. When false, callers should fall back to per-segment
|
||||||
|
// Write calls.
|
||||||
|
func (r *Offload) GSOSupported() bool { return true }
|
||||||
|
|
||||||
|
func (r *Offload) WriteGSO(hdr []byte, transportHdr []byte, pays [][]byte) error {
|
||||||
|
if len(hdr) == 0 || len(pays) == 0 || len(transportHdr) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
vhdr := VirtioNetHdr{
|
||||||
|
Flags: unix.VIRTIO_NET_HDR_F_NEEDS_CSUM,
|
||||||
|
HdrLen: uint16(len(hdr) + len(transportHdr)),
|
||||||
|
GSOSize: uint16(len(pays[0])),
|
||||||
|
CsumStart: uint16(len(hdr)),
|
||||||
|
CsumOffset: 16, // TCP checksum field lives 16 bytes into the TCP header
|
||||||
|
}
|
||||||
|
if len(pays) > 1 {
|
||||||
|
ipVer := hdr[0] >> 4
|
||||||
|
if ipVer == 6 {
|
||||||
|
vhdr.GSOType = unix.VIRTIO_NET_HDR_GSO_TCPV6
|
||||||
|
} else if ipVer == 4 {
|
||||||
|
vhdr.GSOType = unix.VIRTIO_NET_HDR_GSO_TCPV4
|
||||||
|
} else {
|
||||||
|
vhdr.GSOType = unix.VIRTIO_NET_HDR_GSO_NONE
|
||||||
|
vhdr.GSOSize = 0
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
vhdr.GSOType = unix.VIRTIO_NET_HDR_GSO_NONE
|
||||||
|
vhdr.GSOSize = 0
|
||||||
|
}
|
||||||
|
vhdr.encode(r.gsoHdrBuf[:])
|
||||||
|
|
||||||
|
// Build the iovec array: [virtio_hdr, hdr, transportHdr, pays...]. r.gsoIovs[0] is
|
||||||
|
// wired to gsoHdrBuf at construction and never changes.
|
||||||
|
need := 3 + len(pays)
|
||||||
|
if cap(r.gsoIovs) < need {
|
||||||
|
grown := make([]unix.Iovec, need)
|
||||||
|
grown[0] = r.gsoIovs[0]
|
||||||
|
r.gsoIovs = grown
|
||||||
|
} else {
|
||||||
|
r.gsoIovs = r.gsoIovs[:need]
|
||||||
|
}
|
||||||
|
r.gsoIovs[1].Base = &hdr[0]
|
||||||
|
r.gsoIovs[1].SetLen(len(hdr))
|
||||||
|
r.gsoIovs[2].Base = &transportHdr[0]
|
||||||
|
r.gsoIovs[2].SetLen(len(transportHdr))
|
||||||
|
for i, p := range pays {
|
||||||
|
r.gsoIovs[3+i].Base = &p[0]
|
||||||
|
r.gsoIovs[3+i].SetLen(len(p))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := r.rawWrite(r.gsoIovs)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Offload) Close() error {
|
||||||
|
if r.closed.Swap(true) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//shutdownFd is owned by the container, so we should not close it
|
||||||
|
var err error
|
||||||
|
if r.fd >= 0 {
|
||||||
|
err = unix.Close(r.fd)
|
||||||
|
r.fd = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,164 @@
|
|||||||
|
package tio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Maximum size we accept for a single read from a TUN with IFF_VNET_HDR. A
|
||||||
|
// TSO superpacket can be up to 64KiB of payload plus a single L2/L3/L4 header
|
||||||
|
// prefix plus the virtio header.
|
||||||
|
const tunReadBufSize = 65535
|
||||||
|
|
||||||
|
type Poll struct {
|
||||||
|
fd int
|
||||||
|
|
||||||
|
readPoll [2]unix.PollFd
|
||||||
|
writePoll [2]unix.PollFd
|
||||||
|
closed atomic.Bool
|
||||||
|
|
||||||
|
readBuf []byte
|
||||||
|
batchRet [1][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func newPoll(fd int, shutdownFd int) (*Poll, error) {
|
||||||
|
if err := unix.SetNonblock(fd, true); err != nil {
|
||||||
|
_ = unix.Close(fd)
|
||||||
|
return nil, fmt.Errorf("failed to set Poll device as nonblocking: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
out := &Poll{
|
||||||
|
fd: fd,
|
||||||
|
readBuf: make([]byte, tunReadBufSize),
|
||||||
|
readPoll: [2]unix.PollFd{
|
||||||
|
{Fd: int32(fd), Events: unix.POLLIN},
|
||||||
|
{Fd: int32(shutdownFd), Events: unix.POLLIN},
|
||||||
|
},
|
||||||
|
writePoll: [2]unix.PollFd{
|
||||||
|
{Fd: int32(fd), Events: unix.POLLOUT},
|
||||||
|
{Fd: int32(shutdownFd), Events: unix.POLLIN},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// blockOnRead waits until the Poll fd is readable or shutdown has been signaled.
|
||||||
|
// Returns os.ErrClosed if Close was called.
|
||||||
|
func (t *Poll) blockOnRead() error {
|
||||||
|
const problemFlags = unix.POLLHUP | unix.POLLNVAL | unix.POLLERR
|
||||||
|
var err error
|
||||||
|
for {
|
||||||
|
_, err = unix.Poll(t.readPoll[:], -1)
|
||||||
|
if err != unix.EINTR {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tunEvents := t.readPoll[0].Revents
|
||||||
|
shutdownEvents := t.readPoll[1].Revents
|
||||||
|
t.readPoll[0].Revents = 0
|
||||||
|
t.readPoll[1].Revents = 0
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if shutdownEvents&(unix.POLLIN|problemFlags) != 0 {
|
||||||
|
return os.ErrClosed
|
||||||
|
}
|
||||||
|
if tunEvents&problemFlags != 0 {
|
||||||
|
return os.ErrClosed
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Poll) blockOnWrite() error {
|
||||||
|
const problemFlags = unix.POLLHUP | unix.POLLNVAL | unix.POLLERR
|
||||||
|
var err error
|
||||||
|
for {
|
||||||
|
_, err = unix.Poll(t.writePoll[:], -1)
|
||||||
|
if err != unix.EINTR {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tunEvents := t.writePoll[0].Revents
|
||||||
|
shutdownEvents := t.writePoll[1].Revents
|
||||||
|
t.writePoll[0].Revents = 0
|
||||||
|
t.writePoll[1].Revents = 0
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if shutdownEvents&(unix.POLLIN|problemFlags) != 0 {
|
||||||
|
return os.ErrClosed
|
||||||
|
}
|
||||||
|
if tunEvents&problemFlags != 0 {
|
||||||
|
return os.ErrClosed
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Poll) Read() ([][]byte, error) {
|
||||||
|
n, err := t.readOne(t.readBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
t.batchRet[0] = t.readBuf[:n]
|
||||||
|
return t.batchRet[:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Poll) readOne(to []byte) (int, error) {
|
||||||
|
for {
|
||||||
|
n, errno := unix.Read(t.fd, to)
|
||||||
|
if errno == nil {
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
switch errno {
|
||||||
|
case unix.EAGAIN:
|
||||||
|
if err := t.blockOnRead(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
case unix.EINTR:
|
||||||
|
// retry
|
||||||
|
case unix.EBADF:
|
||||||
|
return 0, os.ErrClosed
|
||||||
|
default:
|
||||||
|
return 0, errno
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write is only valid for single threaded use
|
||||||
|
func (t *Poll) Write(from []byte) (int, error) {
|
||||||
|
for {
|
||||||
|
n, errno := unix.Write(t.fd, from)
|
||||||
|
if errno == nil {
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
switch errno {
|
||||||
|
case unix.EAGAIN:
|
||||||
|
if err := t.blockOnWrite(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
case unix.EINTR:
|
||||||
|
// retry
|
||||||
|
case unix.EBADF:
|
||||||
|
return 0, os.ErrClosed
|
||||||
|
default:
|
||||||
|
return 0, errno
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Poll) Close() error {
|
||||||
|
if t.closed.Swap(true) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
//shutdownFd is owned by the container, so we should not close it
|
||||||
|
var err error
|
||||||
|
if t.fd >= 0 {
|
||||||
|
err = unix.Close(t.fd)
|
||||||
|
t.fd = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
//go:build linux && !android && !e2e_testing
|
||||||
|
// +build linux,!android,!e2e_testing
|
||||||
|
|
||||||
|
package tio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// newReadPipe returns a read fd. The matching write fd is registered for cleanup.
|
||||||
|
// The caller takes ownership of the read fd (pass it to newOffload / newFriend).
|
||||||
|
func newReadPipe(t *testing.T) int {
|
||||||
|
t.Helper()
|
||||||
|
var fds [2]int
|
||||||
|
if err := unix.Pipe2(fds[:], unix.O_CLOEXEC); err != nil {
|
||||||
|
t.Fatalf("pipe2: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = unix.Close(fds[1]) })
|
||||||
|
return fds[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPoll_WakeForShutdown_WakesFriends(t *testing.T) {
|
||||||
|
pipe1 := newReadPipe(t)
|
||||||
|
pipe2 := newReadPipe(t)
|
||||||
|
parent, err := NewPollContainer()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, parent.Add(pipe1))
|
||||||
|
require.NoError(t, parent.Add(pipe2))
|
||||||
|
t.Cleanup(func() {
|
||||||
|
_ = unix.Close(pipe1)
|
||||||
|
_ = unix.Close(pipe2)
|
||||||
|
})
|
||||||
|
|
||||||
|
readers := parent.Queues()
|
||||||
|
errs := make([]error, len(readers))
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for i, r := range readers {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(i int, r Queue) {
|
||||||
|
defer wg.Done()
|
||||||
|
_, errs[i] = r.Read()
|
||||||
|
}(i, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
|
||||||
|
if err := parent.Close(); err != nil {
|
||||||
|
t.Fatalf("Close: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() { wg.Wait(); close(done) }()
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("readers did not wake")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, err := range errs {
|
||||||
|
if !errors.Is(err, os.ErrClosed) {
|
||||||
|
t.Errorf("reader %d: expected os.ErrClosed, got %v", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPoll_Close_Idempotent(t *testing.T) {
|
||||||
|
tf, err := newPoll(newReadPipe(t), 1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
if err := tf.Close(); err != nil {
|
||||||
|
t.Fatalf("first Close: %v", err)
|
||||||
|
}
|
||||||
|
if err := tf.Close(); err != nil {
|
||||||
|
t.Fatalf("second Close should be a no-op, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,331 @@
|
|||||||
|
//go:build linux && !android && !e2e_testing
|
||||||
|
// +build linux,!android,!e2e_testing
|
||||||
|
|
||||||
|
package tio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
"gvisor.dev/gvisor/pkg/tcpip/checksum"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Protocol header size bounds used to validate / cap kernel-supplied offsets.
|
||||||
|
const (
|
||||||
|
ipv4HeaderMinLen = 20 // IHL=5, no options
|
||||||
|
ipv4HeaderMaxLen = 60 // IHL=15, max options
|
||||||
|
ipv6FixedLen = 40 // IPv6 base header; extensions would extend this
|
||||||
|
tcpHeaderMinLen = 20 // data-offset=5, no options
|
||||||
|
tcpHeaderMaxLen = 60 // data-offset=15, max options
|
||||||
|
)
|
||||||
|
|
||||||
|
// Byte offsets inside an IPv4 header.
|
||||||
|
const (
|
||||||
|
ipv4TotalLenOff = 2
|
||||||
|
ipv4IDOff = 4
|
||||||
|
ipv4ChecksumOff = 10
|
||||||
|
ipv4SrcOff = 12
|
||||||
|
ipv4AddrsEnd = 20 // end of dst address (ipv4SrcOff + 2*4)
|
||||||
|
)
|
||||||
|
|
||||||
|
// Byte offsets inside an IPv6 header.
|
||||||
|
const (
|
||||||
|
ipv6PayloadLenOff = 4
|
||||||
|
ipv6SrcOff = 8
|
||||||
|
ipv6AddrsEnd = 40 // end of dst address (ipv6SrcOff + 2*16)
|
||||||
|
)
|
||||||
|
|
||||||
|
// Byte offsets inside a TCP header (relative to its start, i.e. csumStart).
|
||||||
|
const (
|
||||||
|
tcpSeqOff = 4
|
||||||
|
tcpDataOffOff = 12 // upper nibble is header len in 32-bit words
|
||||||
|
tcpFlagsOff = 13
|
||||||
|
tcpChecksumOff = 16
|
||||||
|
)
|
||||||
|
|
||||||
|
// tcpFinPshMask is cleared on every segment except the last of a TSO burst.
|
||||||
|
const tcpFinPshMask = 0x09 // FIN(0x01) | PSH(0x08)
|
||||||
|
|
||||||
|
func checkVirtioValid(pkt []byte, hdr VirtioNetHdr) error {
|
||||||
|
// When RSC_INFO is set the csum_start/csum_offset fields are repurposed to
|
||||||
|
// carry coalescing info rather than checksum offsets. A TUN writing via
|
||||||
|
// IFF_VNET_HDR should never emit this, but if it did we would silently
|
||||||
|
// miscompute the segment checksums — refuse the packet instead.
|
||||||
|
if hdr.Flags&unix.VIRTIO_NET_HDR_F_RSC_INFO != 0 {
|
||||||
|
return fmt.Errorf("virtio RSC_INFO flag not supported on TUN reads")
|
||||||
|
}
|
||||||
|
if len(pkt) < ipv4HeaderMinLen {
|
||||||
|
return fmt.Errorf("packet too short")
|
||||||
|
}
|
||||||
|
ipVersion := pkt[0] >> 4
|
||||||
|
switch hdr.GSOType {
|
||||||
|
case unix.VIRTIO_NET_HDR_GSO_TCPV4:
|
||||||
|
if ipVersion != 4 {
|
||||||
|
return fmt.Errorf("invalid IP version %d for GSO type %d", ipVersion, hdr.GSOType)
|
||||||
|
}
|
||||||
|
case unix.VIRTIO_NET_HDR_GSO_TCPV6:
|
||||||
|
if ipVersion != 6 {
|
||||||
|
return fmt.Errorf("invalid IP version %d for GSO type %d", ipVersion, hdr.GSOType)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
if !(ipVersion == 6 || ipVersion == 4) {
|
||||||
|
return fmt.Errorf("invalid IP version %d for GSO type %d", ipVersion, hdr.GSOType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleGSONone(pkt []byte, hdr VirtioNetHdr, out *[][]byte, scratch []byte) error {
|
||||||
|
if len(pkt) > len(scratch) {
|
||||||
|
return fmt.Errorf("packet larger than segment buffer: %d > %d", len(pkt), len(scratch))
|
||||||
|
}
|
||||||
|
copy(scratch, pkt)
|
||||||
|
seg := scratch[:len(pkt)]
|
||||||
|
if hdr.Flags&unix.VIRTIO_NET_HDR_F_NEEDS_CSUM != 0 {
|
||||||
|
if err := finishChecksum(seg, hdr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*out = append(*out, seg)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func correctHdrLen(pkt []byte, hdr *VirtioNetHdr) error {
|
||||||
|
// Thank you wireguard-go for documenting these edge-cases
|
||||||
|
// Don't trust hdr.hdrLen from the kernel as it can be equal to the length
|
||||||
|
// of the entire first packet when the kernel is handling it as part of a
|
||||||
|
// FORWARD path. Instead, parse the transport header length and add it onto
|
||||||
|
// csumStart, which is synonymous for IP header length.
|
||||||
|
const tcpDataOffset = 12
|
||||||
|
|
||||||
|
if hdr.GSOType == unix.VIRTIO_NET_HDR_GSO_UDP_L4 {
|
||||||
|
hdr.HdrLen = hdr.CsumStart + 8
|
||||||
|
} else {
|
||||||
|
if len(pkt) <= int(hdr.CsumStart+tcpDataOffset) {
|
||||||
|
return errors.New("packet is too short")
|
||||||
|
}
|
||||||
|
|
||||||
|
tcpHLen := uint16(pkt[hdr.CsumStart+tcpDataOffset] >> 4 * 4)
|
||||||
|
if tcpHLen < 20 || tcpHLen > 60 {
|
||||||
|
// A TCP header must be between 20 and 60 bytes in length.
|
||||||
|
return fmt.Errorf("tcp header len is invalid: %d", tcpHLen)
|
||||||
|
}
|
||||||
|
hdr.HdrLen = hdr.CsumStart + tcpHLen
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(pkt) < int(hdr.HdrLen) {
|
||||||
|
return fmt.Errorf("length of packet (%d) < virtioNetHdr.HdrLen (%d)", len(pkt), hdr.HdrLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
if hdr.HdrLen < hdr.CsumStart {
|
||||||
|
return fmt.Errorf("virtioNetHdr.HdrLen (%d) < virtioNetHdr.CsumStart (%d)", hdr.HdrLen, hdr.CsumStart)
|
||||||
|
}
|
||||||
|
cSumAt := int(hdr.CsumStart + hdr.CsumStart)
|
||||||
|
if cSumAt+1 >= len(pkt) {
|
||||||
|
return fmt.Errorf("end of checksum offset (%d) exceeds packet length (%d)", cSumAt+1, len(pkt))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// segmentInto splits a TUN-side packet described by hdr into one or more
|
||||||
|
// IP packets, each appended to *out as a slice of scratch. scratch must be
|
||||||
|
// sized to hold every segment (including replicated headers).
|
||||||
|
func segmentInto(pkt []byte, hdr VirtioNetHdr, out *[][]byte, scratch []byte) error {
|
||||||
|
if err := checkVirtioValid(pkt, hdr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if hdr.GSOType == unix.VIRTIO_NET_HDR_GSO_NONE {
|
||||||
|
return handleGSONone(pkt, hdr, out, scratch)
|
||||||
|
}
|
||||||
|
if err := correctHdrLen(pkt, &hdr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
switch hdr.GSOType {
|
||||||
|
case unix.VIRTIO_NET_HDR_GSO_TCPV4, unix.VIRTIO_NET_HDR_GSO_TCPV6:
|
||||||
|
return segmentTCP(pkt, hdr, out, scratch)
|
||||||
|
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported virtio gso type: %d", hdr.GSOType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// finishChecksum computes the L4 checksum for a non-GSO packet that the kernel
|
||||||
|
// handed us with NEEDS_CSUM set. csum_start / csum_offset point at the 16-bit
|
||||||
|
// checksum field; we zero it, fold a full sum (the field was pre-loaded with
|
||||||
|
// the pseudo-header partial sum by the kernel), and store the result.
|
||||||
|
func finishChecksum(seg []byte, hdr VirtioNetHdr) error {
|
||||||
|
cs := int(hdr.CsumStart)
|
||||||
|
co := int(hdr.CsumOffset)
|
||||||
|
if cs+co+2 > len(seg) {
|
||||||
|
return fmt.Errorf("csum offsets out of range: start=%d offset=%d len=%d", cs, co, len(seg))
|
||||||
|
}
|
||||||
|
// The kernel stores a partial pseudo-header sum at [cs+co:]; sum over the
|
||||||
|
// L4 region starting at cs, folding the prior partial in as the seed.
|
||||||
|
partial := binary.BigEndian.Uint16(seg[cs+co : cs+co+2])
|
||||||
|
seg[cs+co] = 0
|
||||||
|
seg[cs+co+1] = 0
|
||||||
|
binary.BigEndian.PutUint16(seg[cs+co:cs+co+2], ^checksum.Checksum(seg[cs:], partial))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// segmentTCP software-segments a TSO superpacket into one IP packet per MSS
|
||||||
|
// chunk. The caller guarantees hdr.GSOType is TCPV4 or TCPV6.
|
||||||
|
//
|
||||||
|
// Hot-path shape: the per-segment loop only sums the payload chunk. The TCP
|
||||||
|
// header, the IPv4 header, and the pseudo-header src/dst/proto contributions
|
||||||
|
// are each summed once up front — every segment reuses those three pre-folded
|
||||||
|
// uint32 values and combines them with small per-segment deltas (seq, flags,
|
||||||
|
// tcpLen, ip_id, total_len) that are cheap to fold in.
|
||||||
|
func segmentTCP(pkt []byte, hdr VirtioNetHdr, out *[][]byte, scratch []byte) error {
|
||||||
|
if hdr.GSOSize == 0 {
|
||||||
|
return fmt.Errorf("gso_size is zero")
|
||||||
|
}
|
||||||
|
if hdr.CsumStart == 0 {
|
||||||
|
return fmt.Errorf("csum_start is zero")
|
||||||
|
}
|
||||||
|
|
||||||
|
isV4 := hdr.GSOType == unix.VIRTIO_NET_HDR_GSO_TCPV4
|
||||||
|
headerLen := int(hdr.HdrLen) // already corrected by the caller
|
||||||
|
csumStart := int(hdr.CsumStart)
|
||||||
|
|
||||||
|
tcpHdrLen := int(pkt[csumStart+tcpDataOffOff]>>4) * 4
|
||||||
|
|
||||||
|
payload := pkt[headerLen:]
|
||||||
|
payLen := len(payload)
|
||||||
|
gsoSize := int(hdr.GSOSize)
|
||||||
|
numSeg := (payLen + gsoSize - 1) / gsoSize
|
||||||
|
if numSeg == 0 {
|
||||||
|
numSeg = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
need := numSeg*headerLen + payLen
|
||||||
|
if need > len(scratch) {
|
||||||
|
return fmt.Errorf("scratch too small for %d segments: need %d have %d", numSeg, need, len(scratch))
|
||||||
|
}
|
||||||
|
|
||||||
|
origSeq := binary.BigEndian.Uint32(pkt[csumStart+tcpSeqOff : csumStart+tcpSeqOff+4])
|
||||||
|
origFlags := pkt[csumStart+tcpFlagsOff]
|
||||||
|
|
||||||
|
// Precompute the TCP header sum with seq/flags/csum zeroed. Copy onto
|
||||||
|
// the stack, zero the per-segment-varying fields, sum once.
|
||||||
|
var tmp [tcpHeaderMaxLen]byte
|
||||||
|
copy(tmp[:tcpHdrLen], pkt[csumStart:headerLen])
|
||||||
|
tmp[tcpSeqOff], tmp[tcpSeqOff+1], tmp[tcpSeqOff+2], tmp[tcpSeqOff+3] = 0, 0, 0, 0
|
||||||
|
tmp[tcpFlagsOff] = 0
|
||||||
|
tmp[tcpChecksumOff], tmp[tcpChecksumOff+1] = 0, 0
|
||||||
|
baseTcpHdrSum := uint32(checksum.Checksum(tmp[:tcpHdrLen], 0))
|
||||||
|
|
||||||
|
// Pseudo-header src+dst+proto contribution (tcpLen varies per segment).
|
||||||
|
var baseProtoSum uint32
|
||||||
|
if isV4 {
|
||||||
|
baseProtoSum = uint32(checksum.Checksum(pkt[ipv4SrcOff:ipv4AddrsEnd], 0))
|
||||||
|
} else {
|
||||||
|
baseProtoSum = uint32(checksum.Checksum(pkt[ipv6SrcOff:ipv6AddrsEnd], 0))
|
||||||
|
}
|
||||||
|
baseProtoSum += uint32(unix.IPPROTO_TCP)
|
||||||
|
|
||||||
|
// Precompute IPv4 header sum with total_len/id/csum zeroed.
|
||||||
|
var origIPID uint16
|
||||||
|
var ihl int
|
||||||
|
var baseIPHdrSum uint32
|
||||||
|
if isV4 {
|
||||||
|
origIPID = binary.BigEndian.Uint16(pkt[ipv4IDOff : ipv4IDOff+2])
|
||||||
|
ihl = int(pkt[0]&0x0f) * 4
|
||||||
|
if ihl < ipv4HeaderMinLen || ihl > csumStart {
|
||||||
|
return fmt.Errorf("bad IPv4 IHL: %d", ihl)
|
||||||
|
}
|
||||||
|
var ipTmp [ipv4HeaderMaxLen]byte
|
||||||
|
copy(ipTmp[:ihl], pkt[:ihl])
|
||||||
|
ipTmp[ipv4TotalLenOff], ipTmp[ipv4TotalLenOff+1] = 0, 0
|
||||||
|
ipTmp[ipv4IDOff], ipTmp[ipv4IDOff+1] = 0, 0
|
||||||
|
ipTmp[ipv4ChecksumOff], ipTmp[ipv4ChecksumOff+1] = 0, 0
|
||||||
|
baseIPHdrSum = uint32(checksum.Checksum(ipTmp[:ihl], 0))
|
||||||
|
}
|
||||||
|
|
||||||
|
off := 0
|
||||||
|
for i := 0; i < numSeg; i++ {
|
||||||
|
segStart := i * gsoSize
|
||||||
|
segEnd := segStart + gsoSize
|
||||||
|
if segEnd > payLen {
|
||||||
|
segEnd = payLen
|
||||||
|
}
|
||||||
|
segPayLen := segEnd - segStart
|
||||||
|
|
||||||
|
copy(scratch[off:], pkt[:headerLen])
|
||||||
|
copy(scratch[off+headerLen:], payload[segStart:segEnd])
|
||||||
|
seg := scratch[off : off+headerLen+segPayLen]
|
||||||
|
off += headerLen + segPayLen
|
||||||
|
|
||||||
|
segSeq := origSeq + uint32(segStart)
|
||||||
|
segFlags := origFlags
|
||||||
|
if i != numSeg-1 {
|
||||||
|
segFlags = origFlags &^ tcpFinPshMask
|
||||||
|
}
|
||||||
|
totalLen := headerLen + segPayLen
|
||||||
|
|
||||||
|
// Patch IP header and write the v4 header checksum from the precomputed base.
|
||||||
|
if isV4 {
|
||||||
|
segID := origIPID + uint16(i)
|
||||||
|
binary.BigEndian.PutUint16(seg[ipv4TotalLenOff:ipv4TotalLenOff+2], uint16(totalLen))
|
||||||
|
binary.BigEndian.PutUint16(seg[ipv4IDOff:ipv4IDOff+2], segID)
|
||||||
|
ipSum := baseIPHdrSum + uint32(totalLen) + uint32(segID)
|
||||||
|
binary.BigEndian.PutUint16(seg[ipv4ChecksumOff:ipv4ChecksumOff+2], foldComplement(ipSum))
|
||||||
|
} else {
|
||||||
|
// IPv6 payload length excludes the fixed header but includes any
|
||||||
|
// extension headers between [ipv6FixedLen:csumStart].
|
||||||
|
binary.BigEndian.PutUint16(seg[ipv6PayloadLenOff:ipv6PayloadLenOff+2], uint16(headerLen-ipv6FixedLen+segPayLen))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Patch TCP header.
|
||||||
|
binary.BigEndian.PutUint32(seg[csumStart+tcpSeqOff:csumStart+tcpSeqOff+4], segSeq)
|
||||||
|
seg[csumStart+tcpFlagsOff] = segFlags
|
||||||
|
// (csum is written below; its prior contents in `seg` don't affect the
|
||||||
|
// computation since we never sum over the segment's own header.)
|
||||||
|
|
||||||
|
tcpLen := tcpHdrLen + segPayLen
|
||||||
|
paySum := uint32(checksum.Checksum(payload[segStart:segEnd], 0))
|
||||||
|
|
||||||
|
// Combine pre-folded uint32s into a wider accumulator, then fold. Using
|
||||||
|
// uint64 guards against overflow when segSeq's high bits set.
|
||||||
|
wide := uint64(baseTcpHdrSum) + uint64(paySum) + uint64(baseProtoSum)
|
||||||
|
wide += uint64(segSeq) + uint64(segFlags) + uint64(tcpLen)
|
||||||
|
wide = (wide & 0xffffffff) + (wide >> 32)
|
||||||
|
wide = (wide & 0xffffffff) + (wide >> 32)
|
||||||
|
binary.BigEndian.PutUint16(seg[csumStart+tcpChecksumOff:csumStart+tcpChecksumOff+2], foldComplement(uint32(wide)))
|
||||||
|
|
||||||
|
*out = append(*out, seg)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// foldComplement folds a 32-bit one's-complement partial sum to 16 bits and
|
||||||
|
// complements it, yielding the on-wire Internet checksum value.
|
||||||
|
func foldComplement(sum uint32) uint16 {
|
||||||
|
sum = (sum & 0xffff) + (sum >> 16)
|
||||||
|
sum = (sum & 0xffff) + (sum >> 16)
|
||||||
|
return ^uint16(sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
// pseudoHeaderIPv4 returns the folded pseudo-header sum used to verify a TCP
|
||||||
|
// segment's checksum in tests. src/dst are 4 bytes each.
|
||||||
|
func pseudoHeaderIPv4(src, dst []byte, proto byte, tcpLen int) uint16 {
|
||||||
|
s := uint32(checksum.Checksum(src, 0)) + uint32(checksum.Checksum(dst, 0))
|
||||||
|
s += uint32(proto) + uint32(tcpLen)
|
||||||
|
s = (s & 0xffff) + (s >> 16)
|
||||||
|
s = (s & 0xffff) + (s >> 16)
|
||||||
|
return uint16(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// pseudoHeaderIPv6 returns the folded pseudo-header sum used to verify a TCP
|
||||||
|
// segment's checksum in tests. src/dst are 16 bytes each.
|
||||||
|
func pseudoHeaderIPv6(src, dst []byte, proto byte, tcpLen int) uint16 {
|
||||||
|
s := uint32(checksum.Checksum(src, 0)) + uint32(checksum.Checksum(dst, 0))
|
||||||
|
s += uint32(tcpLen>>16) + uint32(tcpLen&0xffff) + uint32(proto)
|
||||||
|
s = (s & 0xffff) + (s >> 16)
|
||||||
|
s = (s & 0xffff) + (s >> 16)
|
||||||
|
return uint16(s)
|
||||||
|
}
|
||||||
@@ -0,0 +1,328 @@
|
|||||||
|
//go:build linux && !android && !e2e_testing
|
||||||
|
// +build linux,!android,!e2e_testing
|
||||||
|
|
||||||
|
package tio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
"gvisor.dev/gvisor/pkg/tcpip/checksum"
|
||||||
|
)
|
||||||
|
|
||||||
|
// verifyChecksum confirms that the one's-complement sum across `b`, seeded
|
||||||
|
// with a folded pseudo-header sum, equals all-ones (valid).
|
||||||
|
func verifyChecksum(b []byte, pseudo uint16) bool {
|
||||||
|
return checksum.Checksum(b, pseudo) == 0xffff
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildTSOv4 builds a synthetic IPv4/TCP TSO superpacket with a payload of
|
||||||
|
// `payLen` bytes split at `mss`.
|
||||||
|
func buildTSOv4(t *testing.T, payLen, mss int) ([]byte, VirtioNetHdr) {
|
||||||
|
t.Helper()
|
||||||
|
const ipLen = 20
|
||||||
|
const tcpLen = 20
|
||||||
|
pkt := make([]byte, ipLen+tcpLen+payLen)
|
||||||
|
|
||||||
|
// IPv4 header
|
||||||
|
pkt[0] = 0x45 // version 4, IHL 5
|
||||||
|
// total length is meaningless for TSO but set it anyway
|
||||||
|
binary.BigEndian.PutUint16(pkt[2:4], uint16(ipLen+tcpLen+payLen))
|
||||||
|
binary.BigEndian.PutUint16(pkt[4:6], 0x4242) // original ID
|
||||||
|
pkt[8] = 64 // TTL
|
||||||
|
pkt[9] = unix.IPPROTO_TCP
|
||||||
|
copy(pkt[12:16], []byte{10, 0, 0, 1}) // src
|
||||||
|
copy(pkt[16:20], []byte{10, 0, 0, 2}) // dst
|
||||||
|
|
||||||
|
// TCP header
|
||||||
|
binary.BigEndian.PutUint16(pkt[20:22], 12345) // sport
|
||||||
|
binary.BigEndian.PutUint16(pkt[22:24], 80) // dport
|
||||||
|
binary.BigEndian.PutUint32(pkt[24:28], 10000) // seq
|
||||||
|
binary.BigEndian.PutUint32(pkt[28:32], 20000) // ack
|
||||||
|
pkt[32] = 0x50 // data offset 5 words
|
||||||
|
pkt[33] = 0x18 // ACK | PSH
|
||||||
|
binary.BigEndian.PutUint16(pkt[34:36], 65535) // window
|
||||||
|
|
||||||
|
// payload
|
||||||
|
for i := 0; i < payLen; i++ {
|
||||||
|
pkt[ipLen+tcpLen+i] = byte(i & 0xff)
|
||||||
|
}
|
||||||
|
|
||||||
|
return pkt, VirtioNetHdr{
|
||||||
|
Flags: unix.VIRTIO_NET_HDR_F_NEEDS_CSUM,
|
||||||
|
GSOType: unix.VIRTIO_NET_HDR_GSO_TCPV4,
|
||||||
|
HdrLen: uint16(ipLen + tcpLen),
|
||||||
|
GSOSize: uint16(mss),
|
||||||
|
CsumStart: uint16(ipLen),
|
||||||
|
CsumOffset: 16,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSegmentTCPv4(t *testing.T) {
|
||||||
|
const mss = 100
|
||||||
|
const numSeg = 3
|
||||||
|
pkt, hdr := buildTSOv4(t, mss*numSeg, mss)
|
||||||
|
|
||||||
|
scratch := make([]byte, tunSegBufSize)
|
||||||
|
var out [][]byte
|
||||||
|
if err := segmentTCP(pkt, hdr, &out, scratch); err != nil {
|
||||||
|
t.Fatalf("segmentTCP: %v", err)
|
||||||
|
}
|
||||||
|
if len(out) != numSeg {
|
||||||
|
t.Fatalf("expected %d segments, got %d", numSeg, len(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, seg := range out {
|
||||||
|
if len(seg) != 40+mss {
|
||||||
|
t.Errorf("seg %d: unexpected len %d", i, len(seg))
|
||||||
|
}
|
||||||
|
totalLen := binary.BigEndian.Uint16(seg[2:4])
|
||||||
|
if totalLen != uint16(40+mss) {
|
||||||
|
t.Errorf("seg %d: total_len=%d want %d", i, totalLen, 40+mss)
|
||||||
|
}
|
||||||
|
id := binary.BigEndian.Uint16(seg[4:6])
|
||||||
|
if id != 0x4242+uint16(i) {
|
||||||
|
t.Errorf("seg %d: ip id=%#x want %#x", i, id, 0x4242+uint16(i))
|
||||||
|
}
|
||||||
|
seq := binary.BigEndian.Uint32(seg[24:28])
|
||||||
|
wantSeq := uint32(10000 + i*mss)
|
||||||
|
if seq != wantSeq {
|
||||||
|
t.Errorf("seg %d: seq=%d want %d", i, seq, wantSeq)
|
||||||
|
}
|
||||||
|
flags := seg[33]
|
||||||
|
wantFlags := byte(0x10) // ACK only, PSH cleared
|
||||||
|
if i == numSeg-1 {
|
||||||
|
wantFlags = 0x18 // ACK | PSH preserved on last
|
||||||
|
}
|
||||||
|
if flags != wantFlags {
|
||||||
|
t.Errorf("seg %d: flags=%#x want %#x", i, flags, wantFlags)
|
||||||
|
}
|
||||||
|
// IPv4 header checksum must verify against itself.
|
||||||
|
if !verifyChecksum(seg[:20], 0) {
|
||||||
|
t.Errorf("seg %d: bad IPv4 header checksum", i)
|
||||||
|
}
|
||||||
|
// TCP checksum must verify against the pseudo-header.
|
||||||
|
psum := pseudoHeaderIPv4(seg[12:16], seg[16:20], unix.IPPROTO_TCP, 20+mss)
|
||||||
|
if !verifyChecksum(seg[20:], psum) {
|
||||||
|
t.Errorf("seg %d: bad TCP checksum", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSegmentTCPv4OddTail(t *testing.T) {
|
||||||
|
// Payload of 250 bytes with MSS 100 → segments of 100, 100, 50.
|
||||||
|
pkt, hdr := buildTSOv4(t, 250, 100)
|
||||||
|
scratch := make([]byte, tunSegBufSize)
|
||||||
|
var out [][]byte
|
||||||
|
if err := segmentTCP(pkt, hdr, &out, scratch); err != nil {
|
||||||
|
t.Fatalf("segmentTCP: %v", err)
|
||||||
|
}
|
||||||
|
if len(out) != 3 {
|
||||||
|
t.Fatalf("want 3 segments, got %d", len(out))
|
||||||
|
}
|
||||||
|
wantPayLens := []int{100, 100, 50}
|
||||||
|
for i, seg := range out {
|
||||||
|
if len(seg)-40 != wantPayLens[i] {
|
||||||
|
t.Errorf("seg %d: pay len %d want %d", i, len(seg)-40, wantPayLens[i])
|
||||||
|
}
|
||||||
|
if !verifyChecksum(seg[:20], 0) {
|
||||||
|
t.Errorf("seg %d: bad IPv4 header checksum", i)
|
||||||
|
}
|
||||||
|
psum := pseudoHeaderIPv4(seg[12:16], seg[16:20], unix.IPPROTO_TCP, 20+wantPayLens[i])
|
||||||
|
if !verifyChecksum(seg[20:], psum) {
|
||||||
|
t.Errorf("seg %d: bad TCP checksum", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSegmentTCPv6(t *testing.T) {
|
||||||
|
const ipLen = 40
|
||||||
|
const tcpLen = 20
|
||||||
|
const mss = 120
|
||||||
|
const numSeg = 2
|
||||||
|
payLen := mss * numSeg
|
||||||
|
pkt := make([]byte, ipLen+tcpLen+payLen)
|
||||||
|
|
||||||
|
// IPv6 header
|
||||||
|
pkt[0] = 0x60 // version 6
|
||||||
|
binary.BigEndian.PutUint16(pkt[4:6], uint16(tcpLen+payLen))
|
||||||
|
pkt[6] = unix.IPPROTO_TCP
|
||||||
|
pkt[7] = 64
|
||||||
|
// src/dst fe80::1 / fe80::2
|
||||||
|
pkt[8] = 0xfe
|
||||||
|
pkt[9] = 0x80
|
||||||
|
pkt[23] = 1
|
||||||
|
pkt[24] = 0xfe
|
||||||
|
pkt[25] = 0x80
|
||||||
|
pkt[39] = 2
|
||||||
|
|
||||||
|
// TCP header
|
||||||
|
binary.BigEndian.PutUint16(pkt[40:42], 12345)
|
||||||
|
binary.BigEndian.PutUint16(pkt[42:44], 80)
|
||||||
|
binary.BigEndian.PutUint32(pkt[44:48], 7)
|
||||||
|
binary.BigEndian.PutUint32(pkt[48:52], 99)
|
||||||
|
pkt[52] = 0x50
|
||||||
|
pkt[53] = 0x19 // FIN | ACK | PSH — exercise FIN clearing too
|
||||||
|
binary.BigEndian.PutUint16(pkt[54:56], 65535)
|
||||||
|
|
||||||
|
for i := 0; i < payLen; i++ {
|
||||||
|
pkt[ipLen+tcpLen+i] = byte(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
hdr := VirtioNetHdr{
|
||||||
|
Flags: unix.VIRTIO_NET_HDR_F_NEEDS_CSUM,
|
||||||
|
GSOType: unix.VIRTIO_NET_HDR_GSO_TCPV6,
|
||||||
|
HdrLen: uint16(ipLen + tcpLen),
|
||||||
|
GSOSize: uint16(mss),
|
||||||
|
CsumStart: uint16(ipLen),
|
||||||
|
CsumOffset: 16,
|
||||||
|
}
|
||||||
|
|
||||||
|
scratch := make([]byte, tunSegBufSize)
|
||||||
|
var out [][]byte
|
||||||
|
if err := segmentTCP(pkt, hdr, &out, scratch); err != nil {
|
||||||
|
t.Fatalf("segmentTCP: %v", err)
|
||||||
|
}
|
||||||
|
if len(out) != numSeg {
|
||||||
|
t.Fatalf("want %d segments, got %d", numSeg, len(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, seg := range out {
|
||||||
|
if len(seg) != ipLen+tcpLen+mss {
|
||||||
|
t.Errorf("seg %d: len %d want %d", i, len(seg), ipLen+tcpLen+mss)
|
||||||
|
}
|
||||||
|
pl := binary.BigEndian.Uint16(seg[4:6])
|
||||||
|
if pl != uint16(tcpLen+mss) {
|
||||||
|
t.Errorf("seg %d: payload_length=%d want %d", i, pl, tcpLen+mss)
|
||||||
|
}
|
||||||
|
seq := binary.BigEndian.Uint32(seg[44:48])
|
||||||
|
if seq != uint32(7+i*mss) {
|
||||||
|
t.Errorf("seg %d: seq=%d want %d", i, seq, 7+i*mss)
|
||||||
|
}
|
||||||
|
flags := seg[53]
|
||||||
|
// Original flags = 0x19 (FIN|ACK|PSH). FIN(0x01)+PSH(0x08) should be
|
||||||
|
// cleared on all but the last; ACK(0x10) always preserved.
|
||||||
|
wantFlags := byte(0x10)
|
||||||
|
if i == numSeg-1 {
|
||||||
|
wantFlags = 0x19
|
||||||
|
}
|
||||||
|
if flags != wantFlags {
|
||||||
|
t.Errorf("seg %d: flags=%#x want %#x", i, flags, wantFlags)
|
||||||
|
}
|
||||||
|
psum := pseudoHeaderIPv6(seg[8:24], seg[24:40], unix.IPPROTO_TCP, tcpLen+mss)
|
||||||
|
if !verifyChecksum(seg[ipLen:], psum) {
|
||||||
|
t.Errorf("seg %d: bad TCP checksum", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSegmentGSONonePassesThrough(t *testing.T) {
|
||||||
|
pkt, hdr := buildTSOv4(t, 100, 100)
|
||||||
|
hdr.GSOType = unix.VIRTIO_NET_HDR_GSO_NONE
|
||||||
|
hdr.Flags = 0 // no NEEDS_CSUM, leave packet untouched
|
||||||
|
|
||||||
|
scratch := make([]byte, tunSegBufSize)
|
||||||
|
var out [][]byte
|
||||||
|
if err := segmentInto(pkt, hdr, &out, scratch); err != nil {
|
||||||
|
t.Fatalf("segmentInto: %v", err)
|
||||||
|
}
|
||||||
|
if len(out) != 1 {
|
||||||
|
t.Fatalf("want 1 segment, got %d", len(out))
|
||||||
|
}
|
||||||
|
if len(out[0]) != len(pkt) {
|
||||||
|
t.Fatalf("unexpected length: %d vs %d", len(out[0]), len(pkt))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSegmentRejectsUDP(t *testing.T) {
|
||||||
|
hdr := VirtioNetHdr{GSOType: unix.VIRTIO_NET_HDR_GSO_UDP}
|
||||||
|
var out [][]byte
|
||||||
|
if err := segmentInto(nil, hdr, &out, nil); err == nil {
|
||||||
|
t.Fatalf("expected rejection for UDP GSO")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSegmentTCPv4(b *testing.B) {
|
||||||
|
sizes := []struct {
|
||||||
|
name string
|
||||||
|
payLen int
|
||||||
|
mss int
|
||||||
|
}{
|
||||||
|
{"64KiB_MSS1460", 65000, 1460},
|
||||||
|
{"16KiB_MSS1460", 16384, 1460},
|
||||||
|
{"4KiB_MSS1460", 4096, 1460},
|
||||||
|
}
|
||||||
|
for _, sz := range sizes {
|
||||||
|
b.Run(sz.name, func(b *testing.B) {
|
||||||
|
const ipLen = 20
|
||||||
|
const tcpLen = 20
|
||||||
|
pkt := make([]byte, ipLen+tcpLen+sz.payLen)
|
||||||
|
pkt[0] = 0x45
|
||||||
|
binary.BigEndian.PutUint16(pkt[2:4], uint16(ipLen+tcpLen+sz.payLen))
|
||||||
|
binary.BigEndian.PutUint16(pkt[4:6], 0x4242)
|
||||||
|
pkt[8] = 64
|
||||||
|
pkt[9] = unix.IPPROTO_TCP
|
||||||
|
copy(pkt[12:16], []byte{10, 0, 0, 1})
|
||||||
|
copy(pkt[16:20], []byte{10, 0, 0, 2})
|
||||||
|
binary.BigEndian.PutUint16(pkt[20:22], 12345)
|
||||||
|
binary.BigEndian.PutUint16(pkt[22:24], 80)
|
||||||
|
binary.BigEndian.PutUint32(pkt[24:28], 10000)
|
||||||
|
binary.BigEndian.PutUint32(pkt[28:32], 20000)
|
||||||
|
pkt[32] = 0x50
|
||||||
|
pkt[33] = 0x18
|
||||||
|
binary.BigEndian.PutUint16(pkt[34:36], 65535)
|
||||||
|
for i := 0; i < sz.payLen; i++ {
|
||||||
|
pkt[ipLen+tcpLen+i] = byte(i)
|
||||||
|
}
|
||||||
|
hdr := VirtioNetHdr{
|
||||||
|
Flags: unix.VIRTIO_NET_HDR_F_NEEDS_CSUM,
|
||||||
|
GSOType: unix.VIRTIO_NET_HDR_GSO_TCPV4,
|
||||||
|
HdrLen: uint16(ipLen + tcpLen),
|
||||||
|
GSOSize: uint16(sz.mss),
|
||||||
|
CsumStart: uint16(ipLen),
|
||||||
|
CsumOffset: 16,
|
||||||
|
}
|
||||||
|
|
||||||
|
scratch := make([]byte, tunSegBufSize)
|
||||||
|
out := make([][]byte, 0, 64)
|
||||||
|
|
||||||
|
b.SetBytes(int64(len(pkt)))
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
out = out[:0]
|
||||||
|
if err := segmentTCP(pkt, hdr, &out, scratch); err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestTunFileWriteVnetHdrNoAlloc verifies the IFF_VNET_HDR fast-path write is
|
||||||
|
// allocation-free. We write to /dev/null so every call succeeds synchronously.
|
||||||
|
func TestTunFileWriteVnetHdrNoAlloc(t *testing.T) {
|
||||||
|
fd, err := unix.Open("/dev/null", os.O_WRONLY, 0)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open /dev/null: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = unix.Close(fd) })
|
||||||
|
|
||||||
|
tf := &Offload{fd: fd}
|
||||||
|
|
||||||
|
payload := make([]byte, 1400)
|
||||||
|
// Warm up (first call may trigger one-time internal allocations elsewhere).
|
||||||
|
if _, err := tf.Write(payload); err != nil {
|
||||||
|
t.Fatalf("Write: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
allocs := testing.AllocsPerRun(1000, func() {
|
||||||
|
if _, err := tf.Write(payload); err != nil {
|
||||||
|
t.Fatalf("Write: %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if allocs != 0 {
|
||||||
|
t.Fatalf("Write allocated %.1f times per call, want 0", allocs)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package tio
|
||||||
|
|
||||||
|
import "encoding/binary"
|
||||||
|
|
||||||
|
// Size of the legacy struct virtio_net_hdr that the kernel prepends/expects on
|
||||||
|
// a TUN opened with IFF_VNET_HDR (TUNSETVNETHDRSZ not set).
|
||||||
|
const virtioNetHdrLen = 10
|
||||||
|
|
||||||
|
type VirtioNetHdr struct {
|
||||||
|
Flags uint8
|
||||||
|
GSOType uint8
|
||||||
|
HdrLen uint16
|
||||||
|
GSOSize uint16
|
||||||
|
CsumStart uint16
|
||||||
|
CsumOffset uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
// decode reads a virtio_net_hdr in host byte order (TUN default; we never
|
||||||
|
// call TUNSETVNETLE so the kernel matches our endianness).
|
||||||
|
func (h *VirtioNetHdr) decode(b []byte) {
|
||||||
|
h.Flags = b[0]
|
||||||
|
h.GSOType = b[1]
|
||||||
|
h.HdrLen = binary.NativeEndian.Uint16(b[2:4])
|
||||||
|
h.GSOSize = binary.NativeEndian.Uint16(b[4:6])
|
||||||
|
h.CsumStart = binary.NativeEndian.Uint16(b[6:8])
|
||||||
|
h.CsumOffset = binary.NativeEndian.Uint16(b[8:10])
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode is the inverse of decode: writes the virtio_net_hdr fields into b
|
||||||
|
// (must be at least virtioNetHdrLen bytes). Used to emit a TSO superpacket
|
||||||
|
// on egress.
|
||||||
|
func (h *VirtioNetHdr) encode(b []byte) {
|
||||||
|
b[0] = h.Flags
|
||||||
|
b[1] = h.GSOType
|
||||||
|
binary.NativeEndian.PutUint16(b[2:4], h.HdrLen)
|
||||||
|
binary.NativeEndian.PutUint16(b[4:6], h.GSOSize)
|
||||||
|
binary.NativeEndian.PutUint16(b[6:8], h.CsumStart)
|
||||||
|
binary.NativeEndian.PutUint16(b[8:10], h.CsumOffset)
|
||||||
|
}
|
||||||
+31
-13
@@ -13,17 +13,38 @@ import (
|
|||||||
|
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tun struct {
|
type tun struct {
|
||||||
io.ReadWriteCloser
|
rwc io.ReadWriteCloser
|
||||||
fd int
|
fd int
|
||||||
vpnNetworks []netip.Prefix
|
vpnNetworks []netip.Prefix
|
||||||
Routes atomic.Pointer[[]Route]
|
Routes atomic.Pointer[[]Route]
|
||||||
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
||||||
l *slog.Logger
|
l *slog.Logger
|
||||||
|
|
||||||
|
readBuf []byte
|
||||||
|
batchRet [1][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) Read() ([][]byte, error) {
|
||||||
|
n, err := t.rwc.Read(t.readBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
t.batchRet[0] = t.readBuf[:n]
|
||||||
|
return t.batchRet[:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) Write(p []byte) (int, error) {
|
||||||
|
return t.rwc.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) Close() error {
|
||||||
|
return t.rwc.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTunFromFd(c *config.C, l *slog.Logger, deviceFd int, vpnNetworks []netip.Prefix) (*tun, error) {
|
func newTunFromFd(c *config.C, l *slog.Logger, deviceFd int, vpnNetworks []netip.Prefix) (*tun, error) {
|
||||||
@@ -32,10 +53,11 @@ func newTunFromFd(c *config.C, l *slog.Logger, deviceFd int, vpnNetworks []netip
|
|||||||
file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")
|
file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")
|
||||||
|
|
||||||
t := &tun{
|
t := &tun{
|
||||||
ReadWriteCloser: file,
|
rwc: file,
|
||||||
fd: deviceFd,
|
fd: deviceFd,
|
||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
l: l,
|
l: l,
|
||||||
|
readBuf: make([]byte, defaultBatchBufSize),
|
||||||
}
|
}
|
||||||
|
|
||||||
err := t.reload(c, true)
|
err := t.reload(c, true)
|
||||||
@@ -62,7 +84,7 @@ func (t *tun) RoutesFor(ip netip.Addr) routing.Gateways {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t tun) Activate() error {
|
func (t *tun) Activate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,18 +117,14 @@ func (t *tun) Name() string {
|
|||||||
return "android"
|
return "android"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) SupportsPerPeerMTU() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) SetPeerMTU(addr netip.Addr, mtu int) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) SupportsMultiqueue() bool {
|
func (t *tun) SupportsMultiqueue() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *tun) NewMultiQueueReader() error {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for android")
|
return fmt.Errorf("TODO: multiqueue not implemented for android")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) Readers() []tio.Queue {
|
||||||
|
return []tio.Queue{t}
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-17
@@ -16,6 +16,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
netroute "golang.org/x/net/route"
|
netroute "golang.org/x/net/route"
|
||||||
@@ -23,7 +24,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type tun struct {
|
type tun struct {
|
||||||
io.ReadWriteCloser
|
rwc io.ReadWriteCloser
|
||||||
Device string
|
Device string
|
||||||
vpnNetworks []netip.Prefix
|
vpnNetworks []netip.Prefix
|
||||||
DefaultMTU int
|
DefaultMTU int
|
||||||
@@ -34,6 +35,9 @@ type tun struct {
|
|||||||
|
|
||||||
// cache out buffer since we need to prepend 4 bytes for tun metadata
|
// cache out buffer since we need to prepend 4 bytes for tun metadata
|
||||||
out []byte
|
out []byte
|
||||||
|
|
||||||
|
readBuf []byte
|
||||||
|
batchRet [1][]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
type ifReq struct {
|
type ifReq struct {
|
||||||
@@ -124,11 +128,12 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*t
|
|||||||
}
|
}
|
||||||
|
|
||||||
t := &tun{
|
t := &tun{
|
||||||
ReadWriteCloser: os.NewFile(uintptr(fd), ""),
|
rwc: os.NewFile(uintptr(fd), ""),
|
||||||
Device: name,
|
Device: name,
|
||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
DefaultMTU: c.GetInt("tun.mtu", DefaultMTU),
|
DefaultMTU: c.GetInt("tun.mtu", DefaultMTU),
|
||||||
l: l,
|
l: l,
|
||||||
|
readBuf: make([]byte, defaultBatchBufSize),
|
||||||
}
|
}
|
||||||
|
|
||||||
err = t.reload(c, true)
|
err = t.reload(c, true)
|
||||||
@@ -158,8 +163,8 @@ func newTunFromFd(_ *config.C, _ *slog.Logger, _ int, _ []netip.Prefix) (*tun, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Close() error {
|
func (t *tun) Close() error {
|
||||||
if t.ReadWriteCloser != nil {
|
if t.rwc != nil {
|
||||||
return t.ReadWriteCloser.Close()
|
return t.rwc.Close()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -502,15 +507,24 @@ func delRoute(prefix netip.Prefix, gateway netroute.Addr) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Read(to []byte) (int, error) {
|
func (t *tun) readOne(to []byte) (int, error) {
|
||||||
buf := make([]byte, len(to)+4)
|
buf := make([]byte, len(to)+4)
|
||||||
|
|
||||||
n, err := t.ReadWriteCloser.Read(buf)
|
n, err := t.rwc.Read(buf)
|
||||||
|
|
||||||
copy(to, buf[4:])
|
copy(to, buf[4:])
|
||||||
return n - 4, err
|
return n - 4, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *tun) Read() ([][]byte, error) {
|
||||||
|
n, err := t.readOne(t.readBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
t.batchRet[0] = t.readBuf[:n]
|
||||||
|
return t.batchRet[:], nil
|
||||||
|
}
|
||||||
|
|
||||||
// Write is only valid for single threaded use
|
// Write is only valid for single threaded use
|
||||||
func (t *tun) Write(from []byte) (int, error) {
|
func (t *tun) Write(from []byte) (int, error) {
|
||||||
buf := t.out
|
buf := t.out
|
||||||
@@ -536,7 +550,7 @@ func (t *tun) Write(from []byte) (int, error) {
|
|||||||
|
|
||||||
copy(buf[4:], from)
|
copy(buf[4:], from)
|
||||||
|
|
||||||
n, err := t.ReadWriteCloser.Write(buf)
|
n, err := t.rwc.Write(buf)
|
||||||
return n - 4, err
|
return n - 4, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -548,18 +562,14 @@ func (t *tun) Name() string {
|
|||||||
return t.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) SupportsPerPeerMTU() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) SetPeerMTU(addr netip.Addr, mtu int) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) SupportsMultiqueue() bool {
|
func (t *tun) SupportsMultiqueue() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *tun) NewMultiQueueReader() error {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for darwin")
|
return fmt.Errorf("TODO: multiqueue not implemented for darwin")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) Readers() []tio.Queue {
|
||||||
|
return []tio.Queue{t}
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-28
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/rcrowley/go-metrics"
|
"github.com/rcrowley/go-metrics"
|
||||||
"github.com/slackhq/nebula/iputil"
|
"github.com/slackhq/nebula/iputil"
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,6 +22,24 @@ type disabledTun struct {
|
|||||||
tx metrics.Counter
|
tx metrics.Counter
|
||||||
rx metrics.Counter
|
rx metrics.Counter
|
||||||
l *slog.Logger
|
l *slog.Logger
|
||||||
|
numReaders int
|
||||||
|
|
||||||
|
batchRet [1][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *disabledTun) Read() ([][]byte, error) {
|
||||||
|
r, ok := <-t.read
|
||||||
|
if !ok {
|
||||||
|
return nil, io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
t.tx.Inc(1)
|
||||||
|
if t.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
|
t.l.Debug("Write payload", "raw", prettyPacket(r))
|
||||||
|
}
|
||||||
|
|
||||||
|
t.batchRet[0] = r
|
||||||
|
return t.batchRet[:], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDisabledTun(vpnNetworks []netip.Prefix, queueLen int, metricsEnabled bool, l *slog.Logger) *disabledTun {
|
func newDisabledTun(vpnNetworks []netip.Prefix, queueLen int, metricsEnabled bool, l *slog.Logger) *disabledTun {
|
||||||
@@ -28,6 +47,7 @@ func newDisabledTun(vpnNetworks []netip.Prefix, queueLen int, metricsEnabled boo
|
|||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
read: make(chan []byte, queueLen),
|
read: make(chan []byte, queueLen),
|
||||||
l: l,
|
l: l,
|
||||||
|
numReaders: 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
if metricsEnabled {
|
if metricsEnabled {
|
||||||
@@ -57,24 +77,6 @@ func (*disabledTun) Name() string {
|
|||||||
return "disabled"
|
return "disabled"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *disabledTun) Read(b []byte) (int, error) {
|
|
||||||
r, ok := <-t.read
|
|
||||||
if !ok {
|
|
||||||
return 0, io.EOF
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(r) > len(b) {
|
|
||||||
return 0, fmt.Errorf("packet larger than mtu: %d > %d bytes", len(r), len(b))
|
|
||||||
}
|
|
||||||
|
|
||||||
t.tx.Inc(1)
|
|
||||||
if t.l.Enabled(context.Background(), slog.LevelDebug) {
|
|
||||||
t.l.Debug("Write payload", "raw", prettyPacket(r))
|
|
||||||
}
|
|
||||||
|
|
||||||
return copy(b, r), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *disabledTun) handleICMPEchoRequest(b []byte) bool {
|
func (t *disabledTun) handleICMPEchoRequest(b []byte) bool {
|
||||||
out := make([]byte, len(b))
|
out := make([]byte, len(b))
|
||||||
out = iputil.CreateICMPEchoResponse(b, out)
|
out = iputil.CreateICMPEchoResponse(b, out)
|
||||||
@@ -106,20 +108,21 @@ func (t *disabledTun) Write(b []byte) (int, error) {
|
|||||||
return len(b), nil
|
return len(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *disabledTun) SupportsPerPeerMTU() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *disabledTun) SetPeerMTU(addr netip.Addr, mtu int) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *disabledTun) SupportsMultiqueue() bool {
|
func (t *disabledTun) SupportsMultiqueue() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *disabledTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *disabledTun) NewMultiQueueReader() error {
|
||||||
return t, nil
|
t.numReaders++
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *disabledTun) Readers() []tio.Queue {
|
||||||
|
out := make([]tio.Queue, t.numReaders)
|
||||||
|
for i := range t.numReaders {
|
||||||
|
out[i] = t
|
||||||
|
}
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *disabledTun) Close() error {
|
func (t *disabledTun) Close() error {
|
||||||
|
|||||||
@@ -1,120 +0,0 @@
|
|||||||
//go:build linux && !android && !e2e_testing
|
|
||||||
// +build linux,!android,!e2e_testing
|
|
||||||
|
|
||||||
package overlay
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"os"
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
// newReadPipe returns a read fd. The matching write fd is registered for cleanup.
|
|
||||||
// The caller takes ownership of the read fd (pass it to newTunFd / newFriend).
|
|
||||||
func newReadPipe(t *testing.T) int {
|
|
||||||
t.Helper()
|
|
||||||
var fds [2]int
|
|
||||||
if err := unix.Pipe2(fds[:], unix.O_CLOEXEC); err != nil {
|
|
||||||
t.Fatalf("pipe2: %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { _ = unix.Close(fds[1]) })
|
|
||||||
return fds[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTunFile_WakeForShutdown_UnblocksRead(t *testing.T) {
|
|
||||||
tf, err := newTunFd(newReadPipe(t))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("newTunFd: %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { _ = tf.Close() })
|
|
||||||
|
|
||||||
done := make(chan error, 1)
|
|
||||||
go func() {
|
|
||||||
_, err := tf.Read(make([]byte, 64))
|
|
||||||
done <- err
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Verify Read is actually blocked in poll.
|
|
||||||
select {
|
|
||||||
case err := <-done:
|
|
||||||
t.Fatalf("Read returned before shutdown signal: %v", err)
|
|
||||||
case <-time.After(50 * time.Millisecond):
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := tf.wakeForShutdown(); err != nil {
|
|
||||||
t.Fatalf("wakeForShutdown: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case err := <-done:
|
|
||||||
if !errors.Is(err, os.ErrClosed) {
|
|
||||||
t.Fatalf("expected os.ErrClosed, got %v", err)
|
|
||||||
}
|
|
||||||
case <-time.After(2 * time.Second):
|
|
||||||
t.Fatal("Read did not wake on shutdown")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTunFile_WakeForShutdown_WakesFriends(t *testing.T) {
|
|
||||||
parent, err := newTunFd(newReadPipe(t))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("newTunFd: %v", err)
|
|
||||||
}
|
|
||||||
friend, err := parent.newFriend(newReadPipe(t))
|
|
||||||
if err != nil {
|
|
||||||
_ = parent.Close()
|
|
||||||
t.Fatalf("newFriend: %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
_ = friend.Close()
|
|
||||||
_ = parent.Close()
|
|
||||||
})
|
|
||||||
|
|
||||||
readers := []*tunFile{parent, friend}
|
|
||||||
errs := make([]error, len(readers))
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
for i, r := range readers {
|
|
||||||
wg.Add(1)
|
|
||||||
go func(i int, r *tunFile) {
|
|
||||||
defer wg.Done()
|
|
||||||
_, errs[i] = r.Read(make([]byte, 64))
|
|
||||||
}(i, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(50 * time.Millisecond)
|
|
||||||
|
|
||||||
if err := parent.wakeForShutdown(); err != nil {
|
|
||||||
t.Fatalf("wakeForShutdown: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
done := make(chan struct{})
|
|
||||||
go func() { wg.Wait(); close(done) }()
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
case <-time.After(2 * time.Second):
|
|
||||||
t.Fatal("readers did not wake")
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, err := range errs {
|
|
||||||
if !errors.Is(err, os.ErrClosed) {
|
|
||||||
t.Errorf("reader %d: expected os.ErrClosed, got %v", i, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTunFile_Close_Idempotent(t *testing.T) {
|
|
||||||
tf, err := newTunFd(newReadPipe(t))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("newTunFd: %v", err)
|
|
||||||
}
|
|
||||||
if err := tf.Close(); err != nil {
|
|
||||||
t.Fatalf("first Close: %v", err)
|
|
||||||
}
|
|
||||||
if err := tf.Close(); err != nil {
|
|
||||||
t.Fatalf("second Close should be a no-op, got %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+21
-13
@@ -7,7 +7,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
@@ -20,7 +19,7 @@ import (
|
|||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
|
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
netroute "golang.org/x/net/route"
|
netroute "golang.org/x/net/route"
|
||||||
@@ -103,6 +102,9 @@ type tun struct {
|
|||||||
readPoll [2]unix.PollFd
|
readPoll [2]unix.PollFd
|
||||||
writePoll [2]unix.PollFd
|
writePoll [2]unix.PollFd
|
||||||
closed atomic.Bool
|
closed atomic.Bool
|
||||||
|
|
||||||
|
readBuf []byte
|
||||||
|
batchRet [1][]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// blockOnRead waits until the tun fd is readable or shutdown has been signaled.
|
// blockOnRead waits until the tun fd is readable or shutdown has been signaled.
|
||||||
@@ -157,7 +159,16 @@ func (t *tun) blockOnWrite() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Read(to []byte) (int, error) {
|
func (t *tun) Read() ([][]byte, error) {
|
||||||
|
n, err := t.readOne(t.readBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
t.batchRet[0] = t.readBuf[:n]
|
||||||
|
return t.batchRet[:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) readOne(to []byte) (int, error) {
|
||||||
// first 4 bytes is protocol family, in network byte order
|
// first 4 bytes is protocol family, in network byte order
|
||||||
var head [4]byte
|
var head [4]byte
|
||||||
iovecs := [2]syscall.Iovec{
|
iovecs := [2]syscall.Iovec{
|
||||||
@@ -375,6 +386,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*t
|
|||||||
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
||||||
l: l,
|
l: l,
|
||||||
fd: fd,
|
fd: fd,
|
||||||
|
readBuf: make([]byte, defaultBatchBufSize),
|
||||||
shutdownR: shutdownR,
|
shutdownR: shutdownR,
|
||||||
shutdownW: shutdownW,
|
shutdownW: shutdownW,
|
||||||
readPoll: [2]unix.PollFd{
|
readPoll: [2]unix.PollFd{
|
||||||
@@ -561,20 +573,12 @@ func (t *tun) Name() string {
|
|||||||
return t.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) SupportsPerPeerMTU() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) SetPeerMTU(addr netip.Addr, mtu int) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) SupportsMultiqueue() bool {
|
func (t *tun) SupportsMultiqueue() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *tun) NewMultiQueueReader() error {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for freebsd")
|
return fmt.Errorf("TODO: multiqueue not implemented for freebsd")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) addRoutes(logErrors bool) error {
|
func (t *tun) addRoutes(logErrors bool) error {
|
||||||
@@ -601,6 +605,10 @@ func (t *tun) addRoutes(logErrors bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *tun) Readers() []tio.Queue {
|
||||||
|
return []tio.Queue{t}
|
||||||
|
}
|
||||||
|
|
||||||
func (t *tun) removeRoutes(routes []Route) error {
|
func (t *tun) removeRoutes(routes []Route) error {
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if !r.Install {
|
if !r.Install {
|
||||||
|
|||||||
+30
-12
@@ -16,16 +16,37 @@ import (
|
|||||||
|
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tun struct {
|
type tun struct {
|
||||||
io.ReadWriteCloser
|
rwc io.ReadWriteCloser
|
||||||
vpnNetworks []netip.Prefix
|
vpnNetworks []netip.Prefix
|
||||||
Routes atomic.Pointer[[]Route]
|
Routes atomic.Pointer[[]Route]
|
||||||
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
||||||
l *slog.Logger
|
l *slog.Logger
|
||||||
|
|
||||||
|
readBuf []byte
|
||||||
|
batchRet [1][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) Read() ([][]byte, error) {
|
||||||
|
n, err := t.rwc.Read(t.readBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
t.batchRet[0] = t.readBuf[:n]
|
||||||
|
return t.batchRet[:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) Write(p []byte) (int, error) {
|
||||||
|
return t.rwc.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) Close() error {
|
||||||
|
return t.rwc.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTun(_ *config.C, _ *slog.Logger, _ []netip.Prefix, _ bool) (*tun, error) {
|
func newTun(_ *config.C, _ *slog.Logger, _ []netip.Prefix, _ bool) (*tun, error) {
|
||||||
@@ -36,8 +57,9 @@ func newTunFromFd(c *config.C, l *slog.Logger, deviceFd int, vpnNetworks []netip
|
|||||||
file := os.NewFile(uintptr(deviceFd), "/dev/tun")
|
file := os.NewFile(uintptr(deviceFd), "/dev/tun")
|
||||||
t := &tun{
|
t := &tun{
|
||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
ReadWriteCloser: &tunReadCloser{f: file},
|
rwc: &tunReadCloser{f: file},
|
||||||
l: l,
|
l: l,
|
||||||
|
readBuf: make([]byte, defaultBatchBufSize),
|
||||||
}
|
}
|
||||||
|
|
||||||
err := t.reload(c, true)
|
err := t.reload(c, true)
|
||||||
@@ -151,18 +173,14 @@ func (t *tun) Name() string {
|
|||||||
return "iOS"
|
return "iOS"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) SupportsPerPeerMTU() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) SetPeerMTU(addr netip.Addr, mtu int) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) SupportsMultiqueue() bool {
|
func (t *tun) SupportsMultiqueue() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *tun) NewMultiQueueReader() error {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for ios")
|
return fmt.Errorf("TODO: multiqueue not implemented for ios")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) Readers() []tio.Queue {
|
||||||
|
return []tio.Queue{t}
|
||||||
}
|
}
|
||||||
|
|||||||
+115
-299
@@ -4,9 +4,7 @@
|
|||||||
package overlay
|
package overlay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
@@ -19,180 +17,15 @@ import (
|
|||||||
|
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
// tunFile wraps a TUN file descriptor with poll-based reads. The FD provided will be changed to non-blocking.
|
|
||||||
// A shared eventfd allows Close to wake all readers blocked in poll.
|
|
||||||
type tunFile struct {
|
|
||||||
fd int
|
|
||||||
shutdownFd int
|
|
||||||
lastOne bool
|
|
||||||
readPoll [2]unix.PollFd
|
|
||||||
writePoll [2]unix.PollFd
|
|
||||||
closed bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// newFriend makes a tunFile for a MultiQueueReader that copies the shutdown eventfd from the parent tun
|
|
||||||
func (r *tunFile) newFriend(fd int) (*tunFile, error) {
|
|
||||||
if err := unix.SetNonblock(fd, true); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to set tun fd non-blocking: %w", err)
|
|
||||||
}
|
|
||||||
return &tunFile{
|
|
||||||
fd: fd,
|
|
||||||
shutdownFd: r.shutdownFd,
|
|
||||||
readPoll: [2]unix.PollFd{
|
|
||||||
{Fd: int32(fd), Events: unix.POLLIN},
|
|
||||||
{Fd: int32(r.shutdownFd), Events: unix.POLLIN},
|
|
||||||
},
|
|
||||||
writePoll: [2]unix.PollFd{
|
|
||||||
{Fd: int32(fd), Events: unix.POLLOUT},
|
|
||||||
{Fd: int32(r.shutdownFd), Events: unix.POLLIN},
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTunFd(fd int) (*tunFile, error) {
|
|
||||||
if err := unix.SetNonblock(fd, true); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to set tun fd non-blocking: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
shutdownFd, err := unix.Eventfd(0, unix.EFD_NONBLOCK|unix.EFD_CLOEXEC)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to create eventfd: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
out := &tunFile{
|
|
||||||
fd: fd,
|
|
||||||
shutdownFd: shutdownFd,
|
|
||||||
lastOne: true,
|
|
||||||
readPoll: [2]unix.PollFd{
|
|
||||||
{Fd: int32(fd), Events: unix.POLLIN},
|
|
||||||
{Fd: int32(shutdownFd), Events: unix.POLLIN},
|
|
||||||
},
|
|
||||||
writePoll: [2]unix.PollFd{
|
|
||||||
{Fd: int32(fd), Events: unix.POLLOUT},
|
|
||||||
{Fd: int32(shutdownFd), Events: unix.POLLIN},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *tunFile) blockOnRead() error {
|
|
||||||
const problemFlags = unix.POLLHUP | unix.POLLNVAL | unix.POLLERR
|
|
||||||
var err error
|
|
||||||
for {
|
|
||||||
_, err = unix.Poll(r.readPoll[:], -1)
|
|
||||||
if err != unix.EINTR {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//always reset these!
|
|
||||||
tunEvents := r.readPoll[0].Revents
|
|
||||||
shutdownEvents := r.readPoll[1].Revents
|
|
||||||
r.readPoll[0].Revents = 0
|
|
||||||
r.readPoll[1].Revents = 0
|
|
||||||
//do the err check before trusting the potentially bogus bits we just got
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if shutdownEvents&(unix.POLLIN|problemFlags) != 0 {
|
|
||||||
return os.ErrClosed
|
|
||||||
} else if tunEvents&problemFlags != 0 {
|
|
||||||
return os.ErrClosed
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *tunFile) blockOnWrite() error {
|
|
||||||
const problemFlags = unix.POLLHUP | unix.POLLNVAL | unix.POLLERR
|
|
||||||
var err error
|
|
||||||
for {
|
|
||||||
_, err = unix.Poll(r.writePoll[:], -1)
|
|
||||||
if err != unix.EINTR {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//always reset these!
|
|
||||||
tunEvents := r.writePoll[0].Revents
|
|
||||||
shutdownEvents := r.writePoll[1].Revents
|
|
||||||
r.writePoll[0].Revents = 0
|
|
||||||
r.writePoll[1].Revents = 0
|
|
||||||
//do the err check before trusting the potentially bogus bits we just got
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if shutdownEvents&(unix.POLLIN|problemFlags) != 0 {
|
|
||||||
return os.ErrClosed
|
|
||||||
} else if tunEvents&problemFlags != 0 {
|
|
||||||
return os.ErrClosed
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *tunFile) Read(buf []byte) (int, error) {
|
|
||||||
for {
|
|
||||||
if n, err := unix.Read(r.fd, buf); err == nil {
|
|
||||||
return n, nil
|
|
||||||
} else if err == unix.EAGAIN {
|
|
||||||
if err = r.blockOnRead(); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
} else if err == unix.EINTR {
|
|
||||||
continue
|
|
||||||
} else if err == unix.EBADF {
|
|
||||||
return 0, os.ErrClosed
|
|
||||||
} else {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *tunFile) Write(buf []byte) (int, error) {
|
|
||||||
for {
|
|
||||||
if n, err := unix.Write(r.fd, buf); err == nil {
|
|
||||||
return n, nil
|
|
||||||
} else if err == unix.EAGAIN {
|
|
||||||
if err = r.blockOnWrite(); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
} else if err == unix.EINTR {
|
|
||||||
continue
|
|
||||||
} else if err == unix.EBADF {
|
|
||||||
return 0, os.ErrClosed
|
|
||||||
} else {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *tunFile) wakeForShutdown() error {
|
|
||||||
var buf [8]byte
|
|
||||||
binary.NativeEndian.PutUint64(buf[:], 1)
|
|
||||||
_, err := unix.Write(int(r.readPoll[1].Fd), buf[:])
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *tunFile) Close() error {
|
|
||||||
if r.closed { // avoid closing more than once. Technically a fd could get re-used, which would be a problem
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
r.closed = true
|
|
||||||
if r.lastOne {
|
|
||||||
_ = unix.Close(r.shutdownFd)
|
|
||||||
}
|
|
||||||
return unix.Close(r.fd)
|
|
||||||
}
|
|
||||||
|
|
||||||
type tun struct {
|
type tun struct {
|
||||||
*tunFile
|
readers tio.Container
|
||||||
readers []*tunFile
|
|
||||||
closeLock sync.Mutex
|
closeLock sync.Mutex
|
||||||
Device string
|
Device string
|
||||||
vpnNetworks []netip.Prefix
|
vpnNetworks []netip.Prefix
|
||||||
@@ -201,6 +34,7 @@ type tun struct {
|
|||||||
TXQueueLen int
|
TXQueueLen int
|
||||||
deviceIndex int
|
deviceIndex int
|
||||||
ioctlFd uintptr
|
ioctlFd uintptr
|
||||||
|
vnetHdr bool
|
||||||
|
|
||||||
Routes atomic.Pointer[[]Route]
|
Routes atomic.Pointer[[]Route]
|
||||||
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
||||||
@@ -239,7 +73,9 @@ type ifreqQLEN struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newTunFromFd(c *config.C, l *slog.Logger, deviceFd int, vpnNetworks []netip.Prefix) (*tun, error) {
|
func newTunFromFd(c *config.C, l *slog.Logger, deviceFd int, vpnNetworks []netip.Prefix) (*tun, error) {
|
||||||
t, err := newTunGeneric(c, l, deviceFd, vpnNetworks)
|
// We don't know what flags the caller opened this fd with and can't turn
|
||||||
|
// on IFF_VNET_HDR after TUNSETIFF, so skip offload on inherited fds.
|
||||||
|
t, err := newTunGeneric(c, l, deviceFd, false, vpnNetworks)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -249,46 +85,83 @@ func newTunFromFd(c *config.C, l *slog.Logger, deviceFd int, vpnNetworks []netip
|
|||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, multiqueue bool) (*tun, error) {
|
// openTunDev opens /dev/net/tun, creating the device node first if it's
|
||||||
|
// missing (docker containers occasionally omit it).
|
||||||
|
func openTunDev() (int, error) {
|
||||||
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
// If /dev/net/tun doesn't exist, try to create it (will happen in docker)
|
return fd, nil
|
||||||
if os.IsNotExist(err) {
|
|
||||||
err = os.MkdirAll("/dev/net", 0755)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("/dev/net/tun doesn't exist, failed to mkdir -p /dev/net: %w", err)
|
|
||||||
}
|
}
|
||||||
err = unix.Mknod("/dev/net/tun", unix.S_IFCHR|0600, int(unix.Mkdev(10, 200)))
|
if !os.IsNotExist(err) {
|
||||||
if err != nil {
|
return -1, err
|
||||||
return nil, fmt.Errorf("failed to create /dev/net/tun: %w", err)
|
}
|
||||||
|
if err = os.MkdirAll("/dev/net", 0755); err != nil {
|
||||||
|
return -1, fmt.Errorf("/dev/net/tun doesn't exist, failed to mkdir -p /dev/net: %w", err)
|
||||||
|
}
|
||||||
|
if err = unix.Mknod("/dev/net/tun", unix.S_IFCHR|0600, int(unix.Mkdev(10, 200))); err != nil {
|
||||||
|
return -1, fmt.Errorf("failed to create /dev/net/tun: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fd, err = unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
fd, err = unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("created /dev/net/tun, but still failed: %w", err)
|
return -1, fmt.Errorf("created /dev/net/tun, but still failed: %w", err)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
return fd, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tunSetIff runs TUNSETIFF with the given flags and returns the kernel-chosen
|
||||||
|
// device name on success.
|
||||||
|
func tunSetIff(fd int, name string, flags uint16) (string, error) {
|
||||||
var req ifReq
|
var req ifReq
|
||||||
req.Flags = uint16(unix.IFF_TUN | unix.IFF_NO_PI)
|
req.Flags = flags
|
||||||
|
copy(req.Name[:], name)
|
||||||
|
if err := ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return strings.Trim(string(req.Name[:]), "\x00"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// tsoOffloadFlags are the TUN_F_* bits we ask the kernel to enable when a
|
||||||
|
// TSO-capable TUN is available. CSUM is required as a prerequisite for TSO.
|
||||||
|
const tsoOffloadFlags = unix.TUN_F_CSUM | unix.TUN_F_TSO4 | unix.TUN_F_TSO6
|
||||||
|
|
||||||
|
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, multiqueue bool) (*tun, error) {
|
||||||
|
baseFlags := uint16(unix.IFF_TUN | unix.IFF_NO_PI)
|
||||||
if multiqueue {
|
if multiqueue {
|
||||||
req.Flags |= unix.IFF_MULTI_QUEUE
|
baseFlags |= unix.IFF_MULTI_QUEUE
|
||||||
}
|
}
|
||||||
nameStr := c.GetString("tun.dev", "")
|
nameStr := c.GetString("tun.dev", "")
|
||||||
copy(req.Name[:], nameStr)
|
|
||||||
if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
|
|
||||||
_ = unix.Close(fd)
|
|
||||||
return nil, &NameError{
|
|
||||||
Name: nameStr,
|
|
||||||
Underlying: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
name := strings.Trim(string(req.Name[:]), "\x00")
|
|
||||||
|
|
||||||
t, err := newTunGeneric(c, l, fd, vpnNetworks)
|
// First try to open with IFF_VNET_HDR + TUNSETOFFLOAD so we can receive
|
||||||
|
// TSO superpackets. If either step fails (older kernel, unprivileged
|
||||||
|
// container, etc.) we close and fall back to a plain TUN.
|
||||||
|
fd, err := openTunDev()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
vnetHdr := true
|
||||||
|
name, err := tunSetIff(fd, nameStr, baseFlags|unix.IFF_VNET_HDR)
|
||||||
|
if err != nil {
|
||||||
|
_ = unix.Close(fd)
|
||||||
|
vnetHdr = false
|
||||||
|
} else if err = ioctl(uintptr(fd), unix.TUNSETOFFLOAD, uintptr(tsoOffloadFlags)); err != nil {
|
||||||
|
l.Warn("Failed to enable TUN offload (TSO); proceeding without virtio headers", "error", err)
|
||||||
|
_ = unix.Close(fd)
|
||||||
|
vnetHdr = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if !vnetHdr {
|
||||||
|
fd, err = openTunDev()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
name, err = tunSetIff(fd, nameStr, baseFlags)
|
||||||
|
if err != nil {
|
||||||
|
_ = unix.Close(fd)
|
||||||
|
return nil, &NameError{Name: nameStr, Underlying: err}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err := newTunGeneric(c, l, fd, vnetHdr, vpnNetworks)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -299,16 +172,29 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, multiqueue
|
|||||||
}
|
}
|
||||||
|
|
||||||
// newTunGeneric does all the stuff common to different tun initialization paths. It will close your files on error.
|
// newTunGeneric does all the stuff common to different tun initialization paths. It will close your files on error.
|
||||||
func newTunGeneric(c *config.C, l *slog.Logger, fd int, vpnNetworks []netip.Prefix) (*tun, error) {
|
func newTunGeneric(c *config.C, l *slog.Logger, fd int, vnetHdr bool, vpnNetworks []netip.Prefix) (*tun, error) {
|
||||||
tfd, err := newTunFd(fd)
|
var container tio.Container
|
||||||
|
var err error
|
||||||
|
if vnetHdr {
|
||||||
|
container, err = tio.NewOffloadContainer()
|
||||||
|
} else {
|
||||||
|
container, err = tio.NewPollContainer()
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = unix.Close(fd)
|
_ = unix.Close(fd)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
err = container.Add(fd)
|
||||||
|
if err != nil {
|
||||||
|
_ = unix.Close(fd)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
t := &tun{
|
t := &tun{
|
||||||
tunFile: tfd,
|
readers: container,
|
||||||
readers: []*tunFile{tfd},
|
|
||||||
closeLock: sync.Mutex{},
|
closeLock: sync.Mutex{},
|
||||||
|
vnetHdr: vnetHdr,
|
||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
TXQueueLen: c.GetInt("tun.tx_queue", 500),
|
TXQueueLen: c.GetInt("tun.tx_queue", 500),
|
||||||
useSystemRoutes: c.GetBool("tun.use_system_route_table", false),
|
useSystemRoutes: c.GetBool("tun.use_system_route_table", false),
|
||||||
@@ -368,13 +254,6 @@ func (t *tun) reload(c *config.C, initial bool) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// tun.max_mtu raises the device MTU above tun.mtu so PMTUD has headroom to
|
|
||||||
// install per-peer routes between tun.mtu (floor) and tun.max_mtu (ceiling).
|
|
||||||
// When unset (default 0) the device MTU is unchanged from existing behavior.
|
|
||||||
if pmtudCeiling := c.GetInt("tun.max_mtu", 0); pmtudCeiling > newMaxMTU {
|
|
||||||
newMaxMTU = pmtudCeiling
|
|
||||||
}
|
|
||||||
|
|
||||||
t.MaxMTU = newMaxMTU
|
t.MaxMTU = newMaxMTU
|
||||||
t.DefaultMTU = newDefaultMTU
|
t.DefaultMTU = newDefaultMTU
|
||||||
|
|
||||||
@@ -417,32 +296,38 @@ func (t *tun) SupportsMultiqueue() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *tun) NewMultiQueueReader() error {
|
||||||
t.closeLock.Lock()
|
t.closeLock.Lock()
|
||||||
defer t.closeLock.Unlock()
|
defer t.closeLock.Unlock()
|
||||||
|
|
||||||
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var req ifReq
|
flags := uint16(unix.IFF_TUN | unix.IFF_NO_PI | unix.IFF_MULTI_QUEUE)
|
||||||
req.Flags = uint16(unix.IFF_TUN | unix.IFF_NO_PI | unix.IFF_MULTI_QUEUE)
|
if t.vnetHdr {
|
||||||
copy(req.Name[:], t.Device)
|
flags |= unix.IFF_VNET_HDR
|
||||||
if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
|
}
|
||||||
|
if _, err = tunSetIff(fd, t.Device, flags); err != nil {
|
||||||
_ = unix.Close(fd)
|
_ = unix.Close(fd)
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := t.tunFile.newFriend(fd)
|
if t.vnetHdr {
|
||||||
|
if err = ioctl(uintptr(fd), unix.TUNSETOFFLOAD, uintptr(tsoOffloadFlags)); err != nil {
|
||||||
|
_ = unix.Close(fd)
|
||||||
|
return fmt.Errorf("failed to enable offload on multiqueue tun fd: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = t.readers.Add(fd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = unix.Close(fd)
|
_ = unix.Close(fd)
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
t.readers = append(t.readers, out)
|
return nil
|
||||||
|
|
||||||
return out, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) RoutesFor(ip netip.Addr) routing.Gateways {
|
func (t *tun) RoutesFor(ip netip.Addr) routing.Gateways {
|
||||||
@@ -603,7 +488,7 @@ func (t *tun) setDefaultRoute(cidr netip.Prefix) error {
|
|||||||
LinkIndex: t.deviceIndex,
|
LinkIndex: t.deviceIndex,
|
||||||
Dst: dr,
|
Dst: dr,
|
||||||
MTU: t.DefaultMTU,
|
MTU: t.DefaultMTU,
|
||||||
AdvMSS: t.advMSS(Route{Cidr: cidr}),
|
AdvMSS: t.advMSS(Route{}),
|
||||||
Scope: unix.RT_SCOPE_LINK,
|
Scope: unix.RT_SCOPE_LINK,
|
||||||
Src: net.IP(cidr.Addr().AsSlice()),
|
Src: net.IP(cidr.Addr().AsSlice()),
|
||||||
Protocol: unix.RTPROT_KERNEL,
|
Protocol: unix.RTPROT_KERNEL,
|
||||||
@@ -712,69 +597,18 @@ func (t *tun) Name() string {
|
|||||||
return t.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) SupportsPerPeerMTU() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetPeerMTU installs a host route (/32 for an IPv4 vpn address, /128 for an IPv6
|
|
||||||
// vpn address) to addr through this tun device with the given MTU. This causes
|
|
||||||
// the kernel to reject (or surface PTB to apps for) inside packets to addr that
|
|
||||||
// would exceed mtu. Pass mtu=0 to remove the override and let the per-vpn-network
|
|
||||||
// route apply again. PoC: assumes addr is reachable directly via this device.
|
|
||||||
func (t *tun) SetPeerMTU(addr netip.Addr, mtu int) error {
|
|
||||||
bits := addr.BitLen()
|
|
||||||
prefix := netip.PrefixFrom(addr, bits)
|
|
||||||
|
|
||||||
dr := &net.IPNet{
|
|
||||||
IP: addr.AsSlice(),
|
|
||||||
Mask: net.CIDRMask(bits, bits),
|
|
||||||
}
|
|
||||||
|
|
||||||
if mtu == 0 {
|
|
||||||
nr := netlink.Route{
|
|
||||||
LinkIndex: t.deviceIndex,
|
|
||||||
Dst: dr,
|
|
||||||
Scope: unix.RT_SCOPE_LINK,
|
|
||||||
}
|
|
||||||
if err := netlink.RouteDel(&nr); err != nil {
|
|
||||||
return fmt.Errorf("failed to remove per-peer mtu route %v: %w", prefix, err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
nr := netlink.Route{
|
|
||||||
LinkIndex: t.deviceIndex,
|
|
||||||
Dst: dr,
|
|
||||||
MTU: mtu,
|
|
||||||
AdvMSS: t.advMSS(Route{Cidr: prefix, MTU: mtu}),
|
|
||||||
Scope: unix.RT_SCOPE_LINK,
|
|
||||||
}
|
|
||||||
if err := netlink.RouteReplace(&nr); err != nil {
|
|
||||||
return fmt.Errorf("failed to set per-peer mtu route %v mtu=%d: %w", prefix, mtu, err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) advMSS(r Route) int {
|
func (t *tun) advMSS(r Route) int {
|
||||||
mtu := r.MTU
|
mtu := r.MTU
|
||||||
if r.MTU == 0 {
|
if r.MTU == 0 {
|
||||||
mtu = t.DefaultMTU
|
mtu = t.DefaultMTU
|
||||||
}
|
}
|
||||||
|
|
||||||
// We only need to set advmss if the route MTU does not match the device MTU.
|
// We only need to set advmss if the route MTU does not match the device MTU
|
||||||
if mtu == t.MaxMTU {
|
if mtu != t.MaxMTU {
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// MSS = MTU - (IP header + TCP header). TCP is always 20 bytes; IP is 20 for
|
|
||||||
// v4 and 40 for v6. r.Cidr is the route destination so it tells us which
|
|
||||||
// family this route is in. If Cidr is unset (empty Route) we default to v4.
|
|
||||||
addr := r.Cidr.Addr()
|
|
||||||
if addr.Is6() && !addr.Is4In6() {
|
|
||||||
return mtu - 60
|
|
||||||
}
|
|
||||||
return mtu - 40
|
return mtu - 40
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func (t *tun) watchRoutes() {
|
func (t *tun) watchRoutes() {
|
||||||
rch := make(chan netlink.RouteUpdate)
|
rch := make(chan netlink.RouteUpdate)
|
||||||
@@ -927,6 +761,10 @@ func (t *tun) updateRoutes(r netlink.RouteUpdate) {
|
|||||||
t.routeTree.Store(newTree)
|
t.routeTree.Store(newTree)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *tun) Readers() []tio.Queue {
|
||||||
|
return t.readers.Queues()
|
||||||
|
}
|
||||||
|
|
||||||
func (t *tun) Close() error {
|
func (t *tun) Close() error {
|
||||||
t.closeLock.Lock()
|
t.closeLock.Lock()
|
||||||
defer t.closeLock.Unlock()
|
defer t.closeLock.Unlock()
|
||||||
@@ -936,32 +774,10 @@ func (t *tun) Close() error {
|
|||||||
t.routeChan = nil
|
t.routeChan = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Signal all readers blocked in poll to wake up and exit
|
|
||||||
_ = t.tunFile.wakeForShutdown()
|
|
||||||
|
|
||||||
if t.ioctlFd > 0 {
|
if t.ioctlFd > 0 {
|
||||||
_ = unix.Close(int(t.ioctlFd))
|
_ = unix.Close(int(t.ioctlFd))
|
||||||
t.ioctlFd = 0
|
t.ioctlFd = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range t.readers {
|
return t.readers.Close()
|
||||||
if i == 0 {
|
|
||||||
continue //we want to close the zeroth reader last
|
|
||||||
}
|
|
||||||
err := t.readers[i].Close()
|
|
||||||
if err != nil {
|
|
||||||
t.l.Error("error closing tun reader", "reader", i, "error", err)
|
|
||||||
} else {
|
|
||||||
t.l.Info("closed tun reader", "reader", i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//this is t.readers[0] too
|
|
||||||
err := t.tunFile.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.l.Error("error closing tun reader", "reader", 0, "error", err)
|
|
||||||
} else {
|
|
||||||
t.l.Info("closed tun reader", "reader", 0)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
package overlay
|
package overlay
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
var runAdvMSSTests = []struct {
|
var runAdvMSSTests = []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
+21
-12
@@ -6,7 +6,6 @@ package overlay
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
@@ -17,6 +16,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
netroute "golang.org/x/net/route"
|
netroute "golang.org/x/net/route"
|
||||||
@@ -66,6 +66,22 @@ type tun struct {
|
|||||||
l *slog.Logger
|
l *slog.Logger
|
||||||
f *os.File
|
f *os.File
|
||||||
fd int
|
fd int
|
||||||
|
|
||||||
|
readBuf []byte
|
||||||
|
batchRet [1][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) Read() ([][]byte, error) {
|
||||||
|
n, err := t.readOne(t.readBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
t.batchRet[0] = t.readBuf[:n]
|
||||||
|
return t.batchRet[:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) Readers() []tio.Queue {
|
||||||
|
return []tio.Queue{t}
|
||||||
}
|
}
|
||||||
|
|
||||||
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
|
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
|
||||||
@@ -102,6 +118,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*t
|
|||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
||||||
l: l,
|
l: l,
|
||||||
|
readBuf: make([]byte, defaultBatchBufSize),
|
||||||
}
|
}
|
||||||
|
|
||||||
err = t.reload(c, true)
|
err = t.reload(c, true)
|
||||||
@@ -141,7 +158,7 @@ func (t *tun) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Read(to []byte) (int, error) {
|
func (t *tun) readOne(to []byte) (int, error) {
|
||||||
rc, err := t.f.SyscallConn()
|
rc, err := t.f.SyscallConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("failed to get syscall conn for tun: %w", err)
|
return 0, fmt.Errorf("failed to get syscall conn for tun: %w", err)
|
||||||
@@ -390,20 +407,12 @@ func (t *tun) Name() string {
|
|||||||
return t.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) SupportsPerPeerMTU() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) SetPeerMTU(addr netip.Addr, mtu int) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) SupportsMultiqueue() bool {
|
func (t *tun) SupportsMultiqueue() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *tun) NewMultiQueueReader() error {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for netbsd")
|
return fmt.Errorf("TODO: multiqueue not implemented for netbsd")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) addRoutes(logErrors bool) error {
|
func (t *tun) addRoutes(logErrors bool) error {
|
||||||
|
|||||||
+21
-12
@@ -6,7 +6,6 @@ package overlay
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
@@ -17,6 +16,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
netroute "golang.org/x/net/route"
|
netroute "golang.org/x/net/route"
|
||||||
@@ -59,6 +59,18 @@ type tun struct {
|
|||||||
fd int
|
fd int
|
||||||
// cache out buffer since we need to prepend 4 bytes for tun metadata
|
// cache out buffer since we need to prepend 4 bytes for tun metadata
|
||||||
out []byte
|
out []byte
|
||||||
|
|
||||||
|
readBuf []byte
|
||||||
|
batchRet [1][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tun) Read() ([][]byte, error) {
|
||||||
|
n, err := t.readOne(t.readBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
t.batchRet[0] = t.readBuf[:n]
|
||||||
|
return t.batchRet[:], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
|
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
|
||||||
@@ -95,6 +107,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*t
|
|||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
||||||
l: l,
|
l: l,
|
||||||
|
readBuf: make([]byte, defaultBatchBufSize),
|
||||||
}
|
}
|
||||||
|
|
||||||
err = t.reload(c, true)
|
err = t.reload(c, true)
|
||||||
@@ -124,7 +137,7 @@ func (t *tun) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Read(to []byte) (int, error) {
|
func (t *tun) readOne(to []byte) (int, error) {
|
||||||
buf := make([]byte, len(to)+4)
|
buf := make([]byte, len(to)+4)
|
||||||
|
|
||||||
n, err := t.f.Read(buf)
|
n, err := t.f.Read(buf)
|
||||||
@@ -310,20 +323,12 @@ func (t *tun) Name() string {
|
|||||||
return t.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) SupportsPerPeerMTU() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) SetPeerMTU(addr netip.Addr, mtu int) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) SupportsMultiqueue() bool {
|
func (t *tun) SupportsMultiqueue() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *tun) NewMultiQueueReader() error {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for openbsd")
|
return fmt.Errorf("TODO: multiqueue not implemented for openbsd")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) addRoutes(logErrors bool) error {
|
func (t *tun) addRoutes(logErrors bool) error {
|
||||||
@@ -374,6 +379,10 @@ func (t *tun) deviceBytes() (o [16]byte) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *tun) Readers() []tio.Queue {
|
||||||
|
return []tio.Queue{t}
|
||||||
|
}
|
||||||
|
|
||||||
func addRoute(prefix netip.Prefix, gateways []netip.Prefix) error {
|
func addRoute(prefix netip.Prefix, gateways []netip.Prefix) error {
|
||||||
sock, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
sock, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+16
-17
@@ -14,6 +14,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,6 +28,17 @@ type TestTun struct {
|
|||||||
closed atomic.Bool
|
closed atomic.Bool
|
||||||
rxPackets chan []byte // Packets to receive into nebula
|
rxPackets chan []byte // Packets to receive into nebula
|
||||||
TxPackets chan []byte // Packets transmitted outside by nebula
|
TxPackets chan []byte // Packets transmitted outside by nebula
|
||||||
|
|
||||||
|
batchRet [1][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TestTun) Read() ([][]byte, error) {
|
||||||
|
p, ok := <-t.rxPackets
|
||||||
|
if !ok {
|
||||||
|
return nil, os.ErrClosed
|
||||||
|
}
|
||||||
|
t.batchRet[0] = p
|
||||||
|
return t.batchRet[:], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*TestTun, error) {
|
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*TestTun, error) {
|
||||||
@@ -105,14 +117,6 @@ func (t *TestTun) Name() string {
|
|||||||
return t.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TestTun) SupportsPerPeerMTU() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TestTun) SetPeerMTU(addr netip.Addr, mtu int) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TestTun) Write(b []byte) (n int, err error) {
|
func (t *TestTun) Write(b []byte) (n int, err error) {
|
||||||
if t.closed.Load() {
|
if t.closed.Load() {
|
||||||
return 0, io.ErrClosedPipe
|
return 0, io.ErrClosedPipe
|
||||||
@@ -132,19 +136,14 @@ func (t *TestTun) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TestTun) Read(b []byte) (int, error) {
|
func (t *TestTun) Readers() []tio.Queue {
|
||||||
p, ok := <-t.rxPackets
|
return []tio.Queue{t}
|
||||||
if !ok {
|
|
||||||
return 0, os.ErrClosed
|
|
||||||
}
|
|
||||||
copy(b, p)
|
|
||||||
return len(p), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TestTun) SupportsMultiqueue() bool {
|
func (t *TestTun) SupportsMultiqueue() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TestTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *TestTun) NewMultiQueueReader() error {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented")
|
return fmt.Errorf("TODO: multiqueue not implemented")
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-15
@@ -6,7 +6,6 @@ package overlay
|
|||||||
import (
|
import (
|
||||||
"crypto"
|
"crypto"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
@@ -18,6 +17,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
"github.com/slackhq/nebula/wintun"
|
"github.com/slackhq/nebula/wintun"
|
||||||
@@ -36,6 +36,18 @@ type winTun struct {
|
|||||||
l *slog.Logger
|
l *slog.Logger
|
||||||
|
|
||||||
tun *wintun.NativeTun
|
tun *wintun.NativeTun
|
||||||
|
|
||||||
|
readBuf []byte
|
||||||
|
batchRet [1][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *winTun) Read() ([][]byte, error) {
|
||||||
|
n, err := t.tun.Read(t.readBuf, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
t.batchRet[0] = t.readBuf[:n]
|
||||||
|
return t.batchRet[:], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTunFromFd(_ *config.C, _ *slog.Logger, _ int, _ []netip.Prefix) (Device, error) {
|
func newTunFromFd(_ *config.C, _ *slog.Logger, _ int, _ []netip.Prefix) (Device, error) {
|
||||||
@@ -55,6 +67,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*w
|
|||||||
}
|
}
|
||||||
|
|
||||||
t := &winTun{
|
t := &winTun{
|
||||||
|
readBuf: make([]byte, defaultBatchBufSize),
|
||||||
Device: deviceName,
|
Device: deviceName,
|
||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
||||||
@@ -229,18 +242,6 @@ func (t *winTun) Name() string {
|
|||||||
return t.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *winTun) SupportsPerPeerMTU() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *winTun) SetPeerMTU(addr netip.Addr, mtu int) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *winTun) Read(b []byte) (int, error) {
|
|
||||||
return t.tun.Read(b, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *winTun) Write(b []byte) (int, error) {
|
func (t *winTun) Write(b []byte) (int, error) {
|
||||||
return t.tun.Write(b, 0)
|
return t.tun.Write(b, 0)
|
||||||
}
|
}
|
||||||
@@ -249,8 +250,12 @@ func (t *winTun) SupportsMultiqueue() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *winTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *winTun) NewMultiQueueReader() error {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for windows")
|
return fmt.Errorf("TODO: multiqueue not implemented for windows")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *winTun) Readers() []tio.Queue {
|
||||||
|
return []tio.Queue{t}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *winTun) Close() error {
|
func (t *winTun) Close() error {
|
||||||
|
|||||||
+30
-13
@@ -6,6 +6,7 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,17 +24,34 @@ func NewUserDevice(vpnNetworks []netip.Prefix) (Device, error) {
|
|||||||
outboundWriter: ow,
|
outboundWriter: ow,
|
||||||
inboundReader: ir,
|
inboundReader: ir,
|
||||||
inboundWriter: iw,
|
inboundWriter: iw,
|
||||||
|
numReaders: 1,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserDevice struct {
|
type UserDevice struct {
|
||||||
vpnNetworks []netip.Prefix
|
vpnNetworks []netip.Prefix
|
||||||
|
numReaders int
|
||||||
|
|
||||||
outboundReader *io.PipeReader
|
outboundReader *io.PipeReader
|
||||||
outboundWriter *io.PipeWriter
|
outboundWriter *io.PipeWriter
|
||||||
|
|
||||||
inboundReader *io.PipeReader
|
inboundReader *io.PipeReader
|
||||||
inboundWriter *io.PipeWriter
|
inboundWriter *io.PipeWriter
|
||||||
|
|
||||||
|
readBuf []byte
|
||||||
|
batchRet [1][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *UserDevice) Read() ([][]byte, error) {
|
||||||
|
if d.readBuf == nil {
|
||||||
|
d.readBuf = make([]byte, defaultBatchBufSize)
|
||||||
|
}
|
||||||
|
n, err := d.outboundReader.Read(d.readBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
d.batchRet[0] = d.readBuf[:n]
|
||||||
|
return d.batchRet[:], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *UserDevice) Activate() error {
|
func (d *UserDevice) Activate() error {
|
||||||
@@ -46,32 +64,31 @@ func (d *UserDevice) RoutesFor(ip netip.Addr) routing.Gateways {
|
|||||||
return routing.Gateways{routing.NewGateway(ip, 1)}
|
return routing.Gateways{routing.NewGateway(ip, 1)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *UserDevice) SupportsPerPeerMTU() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *UserDevice) SetPeerMTU(addr netip.Addr, mtu int) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *UserDevice) SupportsMultiqueue() bool {
|
func (d *UserDevice) SupportsMultiqueue() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *UserDevice) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (d *UserDevice) NewMultiQueueReader() error {
|
||||||
return d, nil
|
d.numReaders++
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *UserDevice) Readers() []tio.Queue {
|
||||||
|
out := make([]tio.Queue, d.numReaders)
|
||||||
|
for i := range d.numReaders {
|
||||||
|
out[i] = d
|
||||||
|
}
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *UserDevice) Pipe() (*io.PipeReader, *io.PipeWriter) {
|
func (d *UserDevice) Pipe() (*io.PipeReader, *io.PipeWriter) {
|
||||||
return d.inboundReader, d.outboundWriter
|
return d.inboundReader, d.outboundWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *UserDevice) Read(p []byte) (n int, err error) {
|
|
||||||
return d.outboundReader.Read(p)
|
|
||||||
}
|
|
||||||
func (d *UserDevice) Write(p []byte) (n int, err error) {
|
func (d *UserDevice) Write(p []byte) (n int, err error) {
|
||||||
return d.inboundWriter.Write(p)
|
return d.inboundWriter.Write(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *UserDevice) Close() error {
|
func (d *UserDevice) Close() error {
|
||||||
d.inboundWriter.Close()
|
d.inboundWriter.Close()
|
||||||
d.outboundWriter.Close()
|
d.outboundWriter.Close()
|
||||||
|
|||||||
@@ -15,12 +15,9 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/handshake"
|
|
||||||
"github.com/slackhq/nebula/noiseutil"
|
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -32,10 +29,10 @@ type PKI struct {
|
|||||||
|
|
||||||
type CertState struct {
|
type CertState struct {
|
||||||
v1Cert cert.Certificate
|
v1Cert cert.Certificate
|
||||||
v1Credential *handshake.Credential
|
v1HandshakeBytes []byte
|
||||||
|
|
||||||
v2Cert cert.Certificate
|
v2Cert cert.Certificate
|
||||||
v2Credential *handshake.Credential
|
v2HandshakeBytes []byte
|
||||||
|
|
||||||
initiatingVersion cert.Version
|
initiatingVersion cert.Version
|
||||||
privateKey []byte
|
privateKey []byte
|
||||||
@@ -95,35 +92,13 @@ func (p *PKI) reload(c *config.C, initial bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *PKI) reloadCerts(c *config.C, initial bool) *util.ContextualError {
|
func (p *PKI) reloadCerts(c *config.C, initial bool) *util.ContextualError {
|
||||||
var cipher string
|
newState, err := newCertStateFromConfig(c)
|
||||||
var currentState *CertState
|
|
||||||
if initial {
|
|
||||||
cipher = c.GetString("cipher", "aes")
|
|
||||||
//TODO: this sucks and we should make it not a global
|
|
||||||
switch cipher {
|
|
||||||
case "aes":
|
|
||||||
noiseEndianness = binary.BigEndian
|
|
||||||
case "chachapoly":
|
|
||||||
noiseEndianness = binary.LittleEndian
|
|
||||||
default:
|
|
||||||
return util.NewContextualError(
|
|
||||||
"unknown cipher",
|
|
||||||
m{"cipher": cipher},
|
|
||||||
nil,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Cipher cant be hot swapped so just leave it at what it was before
|
|
||||||
currentState = p.cs.Load()
|
|
||||||
cipher = currentState.cipher
|
|
||||||
}
|
|
||||||
|
|
||||||
newState, err := newCertStateFromConfig(c, cipher)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return util.NewContextualError("Could not load client cert", nil, err)
|
return util.NewContextualError("Could not load client cert", nil, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if currentState != nil {
|
if !initial {
|
||||||
|
currentState := p.cs.Load()
|
||||||
if newState.v1Cert != nil {
|
if newState.v1Cert != nil {
|
||||||
if currentState.v1Cert == nil {
|
if currentState.v1Cert == nil {
|
||||||
//adding certs is fine, actually. Networks-in-common confirmed in newCertState().
|
//adding certs is fine, actually. Networks-in-common confirmed in newCertState().
|
||||||
@@ -183,6 +158,25 @@ func (p *PKI) reloadCerts(c *config.C, initial bool) *util.ContextualError {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cipher cant be hot swapped so just leave it at what it was before
|
||||||
|
newState.cipher = currentState.cipher
|
||||||
|
|
||||||
|
} else {
|
||||||
|
newState.cipher = c.GetString("cipher", "aes")
|
||||||
|
//TODO: this sucks and we should make it not a global
|
||||||
|
switch newState.cipher {
|
||||||
|
case "aes":
|
||||||
|
noiseEndianness = binary.BigEndian
|
||||||
|
case "chachapoly":
|
||||||
|
noiseEndianness = binary.LittleEndian
|
||||||
|
default:
|
||||||
|
return util.NewContextualError(
|
||||||
|
"unknown cipher",
|
||||||
|
m{"cipher": newState.cipher},
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
p.cs.Store(newState)
|
p.cs.Store(newState)
|
||||||
@@ -214,20 +208,6 @@ func (cs *CertState) GetDefaultCertificate() cert.Certificate {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultVersion returns the preferred cert version for initiating handshakes.
|
|
||||||
func (cs *CertState) DefaultVersion() cert.Version { return cs.initiatingVersion }
|
|
||||||
|
|
||||||
// GetCredential returns the pre-computed handshake credential for the given version, or nil.
|
|
||||||
func (cs *CertState) GetCredential(v cert.Version) *handshake.Credential {
|
|
||||||
switch v {
|
|
||||||
case cert.Version1:
|
|
||||||
return cs.v1Credential
|
|
||||||
case cert.Version2:
|
|
||||||
return cs.v2Credential
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cs *CertState) getCertificate(v cert.Version) cert.Certificate {
|
func (cs *CertState) getCertificate(v cert.Version) cert.Certificate {
|
||||||
switch v {
|
switch v {
|
||||||
case cert.Version1:
|
case cert.Version1:
|
||||||
@@ -239,25 +219,17 @@ func (cs *CertState) getCertificate(v cert.Version) cert.Certificate {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCipherSuite(curve cert.Curve, pkcs11backed bool, cipher string) (noise.CipherSuite, error) {
|
// getHandshakeBytes returns the cached bytes to be used in a handshake message for the requested version.
|
||||||
var dhFunc noise.DHFunc
|
// Callers must check if the return []byte is nil.
|
||||||
switch curve {
|
func (cs *CertState) getHandshakeBytes(v cert.Version) []byte {
|
||||||
case cert.Curve_CURVE25519:
|
switch v {
|
||||||
dhFunc = noise.DH25519
|
case cert.Version1:
|
||||||
case cert.Curve_P256:
|
return cs.v1HandshakeBytes
|
||||||
if pkcs11backed {
|
case cert.Version2:
|
||||||
dhFunc = noiseutil.DHP256PKCS11
|
return cs.v2HandshakeBytes
|
||||||
} else {
|
|
||||||
dhFunc = noiseutil.DHP256
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupported curve: %s", curve)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if cipher == "chachapoly" {
|
|
||||||
return noise.NewCipherSuite(dhFunc, noise.CipherChaChaPoly, noise.HashSHA256), nil
|
|
||||||
}
|
|
||||||
return noise.NewCipherSuite(dhFunc, noiseutil.CipherAESGCM, noise.HashSHA256), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *CertState) String() string {
|
func (cs *CertState) String() string {
|
||||||
@@ -289,7 +261,7 @@ func (cs *CertState) MarshalJSON() ([]byte, error) {
|
|||||||
return json.Marshal(msg)
|
return json.Marshal(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCertStateFromConfig(c *config.C, cipher string) (*CertState, error) {
|
func newCertStateFromConfig(c *config.C) (*CertState, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
privPathOrPEM := c.GetString("pki.key", "")
|
privPathOrPEM := c.GetString("pki.key", "")
|
||||||
@@ -373,14 +345,13 @@ func newCertStateFromConfig(c *config.C, cipher string) (*CertState, error) {
|
|||||||
return nil, fmt.Errorf("unknown pki.initiating_version: %v", rawInitiatingVersion)
|
return nil, fmt.Errorf("unknown pki.initiating_version: %v", rawInitiatingVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
return newCertState(initiatingVersion, v1, v2, isPkcs11, curve, rawKey, cipher)
|
return newCertState(initiatingVersion, v1, v2, isPkcs11, curve, rawKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCertState(dv cert.Version, v1, v2 cert.Certificate, pkcs11backed bool, privateKeyCurve cert.Curve, privateKey []byte, cipher string) (*CertState, error) {
|
func newCertState(dv cert.Version, v1, v2 cert.Certificate, pkcs11backed bool, privateKeyCurve cert.Curve, privateKey []byte) (*CertState, error) {
|
||||||
cs := CertState{
|
cs := CertState{
|
||||||
privateKey: privateKey,
|
privateKey: privateKey,
|
||||||
pkcs11Backed: pkcs11backed,
|
pkcs11Backed: pkcs11backed,
|
||||||
cipher: cipher,
|
|
||||||
myVpnNetworksTable: new(bart.Lite),
|
myVpnNetworksTable: new(bart.Lite),
|
||||||
myVpnAddrsTable: new(bart.Lite),
|
myVpnAddrsTable: new(bart.Lite),
|
||||||
myVpnBroadcastAddrsTable: new(bart.Lite),
|
myVpnBroadcastAddrsTable: new(bart.Lite),
|
||||||
@@ -413,14 +384,10 @@ func newCertState(dv cert.Version, v1, v2 cert.Certificate, pkcs11backed bool, p
|
|||||||
|
|
||||||
v1hs, err := v1.MarshalForHandshakes()
|
v1hs, err := v1.MarshalForHandshakes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error marshalling v1 certificate for handshake: %w", err)
|
return nil, fmt.Errorf("error marshalling certificate for handshake: %w", err)
|
||||||
}
|
|
||||||
ncs, err := newCipherSuite(v1.Curve(), pkcs11backed, cipher)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
cs.v1Cert = v1
|
cs.v1Cert = v1
|
||||||
cs.v1Credential = handshake.NewCredential(v1, v1hs, privateKey, ncs)
|
cs.v1HandshakeBytes = v1hs
|
||||||
|
|
||||||
if cs.initiatingVersion == 0 {
|
if cs.initiatingVersion == 0 {
|
||||||
cs.initiatingVersion = cert.Version1
|
cs.initiatingVersion = cert.Version1
|
||||||
@@ -438,14 +405,10 @@ func newCertState(dv cert.Version, v1, v2 cert.Certificate, pkcs11backed bool, p
|
|||||||
|
|
||||||
v2hs, err := v2.MarshalForHandshakes()
|
v2hs, err := v2.MarshalForHandshakes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error marshalling v2 certificate for handshake: %w", err)
|
return nil, fmt.Errorf("error marshalling certificate for handshake: %w", err)
|
||||||
}
|
|
||||||
ncs, err := newCipherSuite(v2.Curve(), pkcs11backed, cipher)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
cs.v2Cert = v2
|
cs.v2Cert = v2
|
||||||
cs.v2Credential = handshake.NewCredential(v2, v2hs, privateKey, ncs)
|
cs.v2HandshakeBytes = v2hs
|
||||||
|
|
||||||
if cs.initiatingVersion == 0 {
|
if cs.initiatingVersion == 0 {
|
||||||
cs.initiatingVersion = cert.Version2
|
cs.initiatingVersion = cert.Version2
|
||||||
|
|||||||
@@ -1,623 +0,0 @@
|
|||||||
package nebula
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/binary"
|
|
||||||
"log/slog"
|
|
||||||
"math/rand/v2"
|
|
||||||
"net/netip"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/slackhq/nebula/config"
|
|
||||||
"github.com/slackhq/nebula/header"
|
|
||||||
"github.com/slackhq/nebula/overlay"
|
|
||||||
)
|
|
||||||
|
|
||||||
// PMTUD PoC: discover the path MTU per-tunnel via authenticated probes that ride
|
|
||||||
// the existing crypto session. We follow RFC 8899 PLPMTUD: a binary search
|
|
||||||
// between a known-good floor and a configured ceiling, with N consecutive probe
|
|
||||||
// losses at a size treated as "doesn't fit." Confirmed PMTU is pushed to the
|
|
||||||
// overlay device, which on Linux installs a per-host route with the discovered
|
|
||||||
// MTU. The kernel then surfaces EMSGSIZE / PTB to apps writing to the tun.
|
|
||||||
//
|
|
||||||
// Probe payload format (request):
|
|
||||||
//
|
|
||||||
// [magic uint32 BE][probeID uint32 BE][padding 0x00...]
|
|
||||||
//
|
|
||||||
// Reply is a small ack with the same magic and probeID and no padding. We do not
|
|
||||||
// verify the reverse-path MTU; only the forward direction matters for the
|
|
||||||
// receiver's MTU on the inside.
|
|
||||||
|
|
||||||
const (
|
|
||||||
pmtudMagic uint32 = 0x504D5544 // 'P' 'M' 'U' 'D'
|
|
||||||
pmtudFloor = 1280 // IPv6 minimum payload, also a safe internet MTU floor
|
|
||||||
|
|
||||||
// pmtudConverged is the bytes-tolerance for stopping the search.
|
|
||||||
pmtudConverged = 8
|
|
||||||
|
|
||||||
// pmtudMaxLoss matches RFC 8899 MAX_PROBES (default 3).
|
|
||||||
pmtudMaxLoss = 3
|
|
||||||
|
|
||||||
// pmtudProbeInterval is the time between probe ticks during the search phase.
|
|
||||||
// Once a peer converges the wheel stops ticking it; re-validation is driven
|
|
||||||
// by connection_manager via MaybeProbeAsTest at its natural test cadence.
|
|
||||||
pmtudProbeInterval = 500 * time.Millisecond
|
|
||||||
|
|
||||||
// pmtudWheelMax is the wheel's maximum supported scheduling duration. We
|
|
||||||
// only ever schedule at pmtudProbeInterval today, but the wheel needs a
|
|
||||||
// max greater than its tick to allocate its slot ring sensibly.
|
|
||||||
pmtudWheelMax = 5 * time.Second
|
|
||||||
|
|
||||||
// pmtudOverheadPessimistic assumes IPv6 underlay + relay framing:
|
|
||||||
// IPv6(40) + UDP(8) + outer nebula(16) + outer AEAD tag(16)
|
|
||||||
// + inner nebula(16) + inner AEAD tag(16) = 112 bytes.
|
|
||||||
// TODO: track underlay address family and per-peer relay state on the HostInfo
|
|
||||||
// so the manager can use the actual overhead for that tunnel and recover the
|
|
||||||
// 32 bytes we pessimistically give up on direct IPv6 paths and the 52 bytes on
|
|
||||||
// direct IPv4 paths.
|
|
||||||
pmtudOverheadPessimistic = 112
|
|
||||||
|
|
||||||
// pmtudUnsupportedAfter is the number of consecutive lost probes (across any
|
|
||||||
// sizes) without ever receiving a reply that we treat as evidence the peer
|
|
||||||
// does not understand the MTUDProbeRequest subtype (i.e. it's running an
|
|
||||||
// older nebula). After this many failures with everReplied=false we mark the
|
|
||||||
// peer pmtud-unsupported and stop scheduling probes. K is small enough that
|
|
||||||
// it fires before the binary search would naturally converge to floor (which
|
|
||||||
// would otherwise be ~30 wasted probes), but large enough to absorb a few
|
|
||||||
// transient probe losses on a path that's just starting to settle.
|
|
||||||
pmtudUnsupportedAfter = 5
|
|
||||||
)
|
|
||||||
|
|
||||||
// pmtudPeer tracks the binary-search state for one tunnel.
|
|
||||||
type pmtudPeer struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
addr netip.Addr
|
|
||||||
localIdx uint32
|
|
||||||
|
|
||||||
// low is the largest outer IP packet size we have a confirmed ack for.
|
|
||||||
// high is the smallest size we believe fails (the search ceiling to start).
|
|
||||||
low, high int
|
|
||||||
|
|
||||||
// inFlightSize is the outer IP packet size of the probe currently awaiting
|
|
||||||
// an ack. 0 means no probe in flight.
|
|
||||||
inFlightSize int
|
|
||||||
// inFlightID matches the probeID echoed in the reply.
|
|
||||||
inFlightID uint32
|
|
||||||
// losses counts consecutive failures at inFlightSize.
|
|
||||||
losses int
|
|
||||||
|
|
||||||
// firstProbe is true until we have sent the first probe of a search. The
|
|
||||||
// first probe targets the ceiling directly (RFC 8899 permits this Search
|
|
||||||
// Algorithm choice); operators who set tun.max_mtu typically have a path
|
|
||||||
// that supports it, so we converge in one probe in the common case.
|
|
||||||
firstProbe bool
|
|
||||||
// everReplied is true once we have ever received any MTUDProbeReply from
|
|
||||||
// this peer. Combined with consecutiveFailures, this lets us detect peers
|
|
||||||
// that don't understand the new subtype and stop probing them.
|
|
||||||
everReplied bool
|
|
||||||
// consecutiveFailures counts probes lost without an intervening reply.
|
|
||||||
// Resets to 0 on any successful reply.
|
|
||||||
consecutiveFailures int
|
|
||||||
// unsupported is set true once we conclude the peer doesn't speak PMTUD.
|
|
||||||
// The manager skips probes for unsupported peers.
|
|
||||||
unsupported bool
|
|
||||||
|
|
||||||
// converged means we have a confirmed PMTU and are in the slow re-validation phase.
|
|
||||||
converged bool
|
|
||||||
// applied is the inner MTU we last pushed to the overlay device (0 if never).
|
|
||||||
applied int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pmtudPeer) overhead() int {
|
|
||||||
// TODO: branch on actual underlay family + relay state for this peer.
|
|
||||||
return pmtudOverheadPessimistic
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pmtudPeer) midpoint() int {
|
|
||||||
return (p.low + p.high) / 2
|
|
||||||
}
|
|
||||||
|
|
||||||
type pmtudManager struct {
|
|
||||||
intf *Interface
|
|
||||||
device overlay.Device
|
|
||||||
|
|
||||||
// peers is keyed by HostInfo.localIndexId.
|
|
||||||
peers sync.Map // map[uint32]*pmtudPeer
|
|
||||||
|
|
||||||
wheel *LockingTimerWheel[uint32]
|
|
||||||
|
|
||||||
// floor is the always-safe inner MTU (= tun.mtu). Per-peer routes start here
|
|
||||||
// on tunnel-up so unprobed traffic is always small enough to fit. Stored as
|
|
||||||
// atomic int64 so reload can update it without coordinating with the readers
|
|
||||||
// in tick/HandleReply/OnTunnelUp.
|
|
||||||
floor atomic.Int64
|
|
||||||
// ceiling is the search ceiling expressed as an outer IP packet size, derived
|
|
||||||
// from tun.max_mtu (which is the kernel's device MTU on the tun) plus our
|
|
||||||
// pessimistic overhead. PMTUD will not probe larger than this.
|
|
||||||
ceiling atomic.Int64
|
|
||||||
|
|
||||||
enabled atomic.Bool
|
|
||||||
|
|
||||||
l *slog.Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func newPMTUDManagerFromConfig(l *slog.Logger, c *config.C, device overlay.Device) *pmtudManager {
|
|
||||||
m := &pmtudManager{
|
|
||||||
device: device,
|
|
||||||
wheel: NewLockingTimerWheel[uint32](pmtudProbeInterval, pmtudWheelMax),
|
|
||||||
l: l,
|
|
||||||
}
|
|
||||||
c.RegisterReloadCallback(func(c *config.C) { m.reload(c, false) })
|
|
||||||
m.reload(c, true)
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
// reload applies tun.mtu / tun.max_mtu changes to the manager. On the initial
|
|
||||||
// call (during construction) it just snapshots state; on a live reload it also
|
|
||||||
// transitions in-flight peers to match the new bounds: clearing per-peer routes
|
|
||||||
// when newly disabled, seeding peers from the hostmap and flipping DF on
|
|
||||||
// outside sockets when newly enabled, and rebounding existing searches in
|
|
||||||
// place when only the ceiling moved.
|
|
||||||
func (m *pmtudManager) reload(c *config.C, initial bool) {
|
|
||||||
if !initial && !c.HasChanged("tun.mtu") && !c.HasChanged("tun.max_mtu") {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
floor := c.GetInt("tun.mtu", overlay.DefaultMTU)
|
|
||||||
maxMTU := c.GetInt("tun.max_mtu", 0)
|
|
||||||
|
|
||||||
enable := maxMTU > floor && m.device.SupportsPerPeerMTU()
|
|
||||||
var ceiling int
|
|
||||||
if enable {
|
|
||||||
ceiling = maxMTU + pmtudOverheadPessimistic
|
|
||||||
}
|
|
||||||
|
|
||||||
if initial {
|
|
||||||
m.floor.Store(int64(floor))
|
|
||||||
m.ceiling.Store(int64(ceiling))
|
|
||||||
m.enabled.Store(enable)
|
|
||||||
switch {
|
|
||||||
case enable:
|
|
||||||
m.l.Info("pmtud enabled", "floor", floor, "ceiling", ceiling, "tun.max_mtu", maxMTU)
|
|
||||||
case maxMTU > floor:
|
|
||||||
m.l.Warn("pmtud disabled: this platform does not yet support per-peer MTU routes",
|
|
||||||
"tun.max_mtu", maxMTU)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
wasEnabled := m.enabled.Load()
|
|
||||||
m.floor.Store(int64(floor))
|
|
||||||
m.ceiling.Store(int64(ceiling))
|
|
||||||
m.enabled.Store(enable)
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case wasEnabled && !enable:
|
|
||||||
m.disableLive(floor, maxMTU)
|
|
||||||
case !wasEnabled && enable:
|
|
||||||
m.enableLive(floor, ceiling, maxMTU)
|
|
||||||
case wasEnabled && enable:
|
|
||||||
m.reboundLive(floor, ceiling, maxMTU)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// disableLive clears per-peer routes and drops all peer state. We do not
|
|
||||||
// disable DF on the outside sockets; once on, it stays on for the life of the
|
|
||||||
// process. Operators flipping pmtud off live get correct routing behavior; if
|
|
||||||
// they want the historical no-DF behavior back they need to restart.
|
|
||||||
func (m *pmtudManager) disableLive(floor, maxMTU int) {
|
|
||||||
m.peers.Range(func(k, v any) bool {
|
|
||||||
p := v.(*pmtudPeer)
|
|
||||||
p.mu.Lock()
|
|
||||||
applied := p.applied
|
|
||||||
addr := p.addr
|
|
||||||
p.applied = 0
|
|
||||||
p.mu.Unlock()
|
|
||||||
if applied != 0 {
|
|
||||||
if err := m.device.SetPeerMTU(addr, 0); err != nil {
|
|
||||||
m.l.Warn("pmtud: failed to clear per-peer mtu on disable", "addr", addr, "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m.peers.Delete(k)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
m.l.Info("pmtud disabled (tun.max_mtu <= tun.mtu)", "tun.mtu", floor, "tun.max_mtu", maxMTU)
|
|
||||||
}
|
|
||||||
|
|
||||||
// enableLive flips DF on every outside socket. We don't pre-seed existing
|
|
||||||
// tunnels here; connection_manager's normal test cadence will eventually call
|
|
||||||
// MaybeProbeAsTest for each peer, which seeds on miss and lets the wheel pick
|
|
||||||
// up the search from there. New tunnels established after this point still
|
|
||||||
// take the OnTunnelUp fast path.
|
|
||||||
func (m *pmtudManager) enableLive(floor, ceiling, maxMTU int) {
|
|
||||||
m.enableDF()
|
|
||||||
m.l.Info("pmtud enabled", "floor", floor, "ceiling", ceiling, "tun.max_mtu", maxMTU)
|
|
||||||
}
|
|
||||||
|
|
||||||
// reboundLive resets each peer's search state to the new bounds. Peers whose
|
|
||||||
// confirmed PMTU still fits under the new ceiling keep their applied route in
|
|
||||||
// place during the new search; peers whose confirmed PMTU exceeds the new
|
|
||||||
// ceiling get cleared back to floor and re-search from scratch. The unsupported
|
|
||||||
// flag is preserved because peer software version doesn't change on reload.
|
|
||||||
func (m *pmtudManager) reboundLive(floor, ceiling, maxMTU int) {
|
|
||||||
overhead := pmtudOverheadPessimistic
|
|
||||||
m.peers.Range(func(k, v any) bool {
|
|
||||||
p := v.(*pmtudPeer)
|
|
||||||
p.mu.Lock()
|
|
||||||
if p.applied > 0 && p.applied+overhead > ceiling {
|
|
||||||
if err := m.device.SetPeerMTU(p.addr, 0); err != nil {
|
|
||||||
m.l.Warn("pmtud: failed to clear per-peer mtu on rebound", "addr", p.addr, "error", err)
|
|
||||||
} else {
|
|
||||||
p.applied = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
p.low = floor + overhead
|
|
||||||
p.high = ceiling
|
|
||||||
p.inFlightSize = 0
|
|
||||||
p.inFlightID = 0
|
|
||||||
p.losses = 0
|
|
||||||
p.firstProbe = !p.unsupported
|
|
||||||
p.converged = false
|
|
||||||
idx := p.localIdx
|
|
||||||
unsupported := p.unsupported
|
|
||||||
p.mu.Unlock()
|
|
||||||
if !unsupported {
|
|
||||||
m.wheel.Add(idx, pmtudProbeInterval)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
m.l.Info("pmtud reloaded", "floor", floor, "ceiling", ceiling, "tun.max_mtu", maxMTU)
|
|
||||||
}
|
|
||||||
|
|
||||||
// enableDF asks every outside socket to set the don't-fragment bit on outbound
|
|
||||||
// packets. Idempotent: safe to call from both Start (initial enable) and from a
|
|
||||||
// live reload that flips pmtud on.
|
|
||||||
func (m *pmtudManager) enableDF() {
|
|
||||||
for i, w := range m.intf.writers {
|
|
||||||
if err := w.EnablePathMTUDiscovery(); err != nil {
|
|
||||||
m.l.Warn("pmtud: failed to enable path mtu discovery on outside socket; pmtud will not work correctly",
|
|
||||||
"writer", i, "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start runs the probe scheduler until ctx is done. The loop runs even when PMTUD
|
|
||||||
// is disabled at startup so a hot reload can turn it on without restarting nebula.
|
|
||||||
//
|
|
||||||
// When PMTUD is enabled at startup we ask each outside socket to enable
|
|
||||||
// path-MTU discovery (DF on every send). This is intentionally gated on the
|
|
||||||
// feature being on so that operators who haven't opted in keep the historical
|
|
||||||
// behavior where the kernel may fragment outbound nebula UDP packets. A live
|
|
||||||
// reload from disabled to enabled will also flip DF on via enableLive; the
|
|
||||||
// reverse direction does not turn DF off, so flipping pmtud back off live
|
|
||||||
// keeps DF on until restart.
|
|
||||||
func (m *pmtudManager) Start(ctx context.Context) {
|
|
||||||
if m.enabled.Load() {
|
|
||||||
m.enableDF()
|
|
||||||
}
|
|
||||||
|
|
||||||
ticker := time.NewTicker(m.wheel.t.tickDuration)
|
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case now := <-ticker.C:
|
|
||||||
m.wheel.Advance(now)
|
|
||||||
for {
|
|
||||||
idx, has := m.wheel.Purge()
|
|
||||||
if !has {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
m.tick(idx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// OnTunnelUp is called when a HostInfo becomes traffic-watched. The kernel
|
|
||||||
// already routes packets to this peer through the per-vpn-network route (mtu =
|
|
||||||
// tun.mtu), so the floor is in effect implicitly. We just kick off the search
|
|
||||||
// here; HandleReply will install a per-host /32 (or /128) route once a larger
|
|
||||||
// size is confirmed.
|
|
||||||
func (m *pmtudManager) OnTunnelUp(hi *HostInfo) {
|
|
||||||
if !m.enabled.Load() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
m.seedPeer(hi)
|
|
||||||
}
|
|
||||||
|
|
||||||
// seedPeer is the shared body of OnTunnelUp and the live-reload enable path.
|
|
||||||
// LoadOrStore protects against double-seeding the same localIndexId from a
|
|
||||||
// race between OnTunnelUp and a reload-driven hostmap walk.
|
|
||||||
func (m *pmtudManager) seedPeer(hi *HostInfo) {
|
|
||||||
if hi == nil || len(hi.vpnAddrs) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
floor := int(m.floor.Load())
|
|
||||||
ceiling := int(m.ceiling.Load())
|
|
||||||
p := &pmtudPeer{
|
|
||||||
addr: hi.vpnAddrs[0],
|
|
||||||
localIdx: hi.localIndexId,
|
|
||||||
low: floor + pmtudOverheadPessimistic,
|
|
||||||
high: ceiling,
|
|
||||||
firstProbe: true,
|
|
||||||
}
|
|
||||||
if _, loaded := m.peers.LoadOrStore(hi.localIndexId, p); loaded {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
m.wheel.Add(hi.localIndexId, pmtudProbeInterval)
|
|
||||||
}
|
|
||||||
|
|
||||||
// OnTunnelDown is called when a HostInfo is being torn down. Removes any per-host
|
|
||||||
// MTU override so the device default applies again.
|
|
||||||
func (m *pmtudManager) OnTunnelDown(hi *HostInfo) {
|
|
||||||
if hi == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
v, ok := m.peers.LoadAndDelete(hi.localIndexId)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
p := v.(*pmtudPeer)
|
|
||||||
p.mu.Lock()
|
|
||||||
applied := p.applied
|
|
||||||
addr := p.addr
|
|
||||||
p.applied = 0
|
|
||||||
p.mu.Unlock()
|
|
||||||
if applied != 0 {
|
|
||||||
if err := m.device.SetPeerMTU(addr, 0); err != nil {
|
|
||||||
m.l.Warn("pmtud: failed to clear per-peer mtu", "addr", addr, "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// OnRoam is called when a HostInfo's remote underlay address changes. The path
|
|
||||||
// MTU may now be different; drop the per-host route so the kernel falls back to
|
|
||||||
// the per-vpn-network route (mtu = tun.mtu floor), then restart the search.
|
|
||||||
// We do not reset the unsupported flag: peer software version doesn't change on
|
|
||||||
// roam, so once we've decided a peer doesn't speak PMTUD we stay decided.
|
|
||||||
func (m *pmtudManager) OnRoam(hi *HostInfo) {
|
|
||||||
if !m.enabled.Load() || hi == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
v, ok := m.peers.Load(hi.localIndexId)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
p := v.(*pmtudPeer)
|
|
||||||
p.mu.Lock()
|
|
||||||
if p.unsupported {
|
|
||||||
p.mu.Unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
p.low = int(m.floor.Load()) + pmtudOverheadPessimistic
|
|
||||||
p.high = int(m.ceiling.Load())
|
|
||||||
p.inFlightSize = 0
|
|
||||||
p.inFlightID = 0
|
|
||||||
p.losses = 0
|
|
||||||
p.consecutiveFailures = 0
|
|
||||||
p.firstProbe = true
|
|
||||||
p.converged = false
|
|
||||||
if p.applied != 0 {
|
|
||||||
if err := m.device.SetPeerMTU(p.addr, 0); err != nil {
|
|
||||||
m.l.Warn("pmtud: failed to clear per-peer mtu on roam", "addr", p.addr, "error", err)
|
|
||||||
} else {
|
|
||||||
p.applied = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
p.mu.Unlock()
|
|
||||||
m.wheel.Add(hi.localIndexId, pmtudProbeInterval)
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaybeProbeAsTest is called by connection_manager when it would otherwise send
|
|
||||||
// a TestRequest because a tunnel has gone silent. If we have a confirmed PMTU
|
|
||||||
// for this peer that's larger than the floor, we send a probe at that size
|
|
||||||
// instead. The reply confirms both liveness (consumed by connection_manager via
|
|
||||||
// the existing inbound traffic accounting fallthrough in outside.go) and that
|
|
||||||
// the confirmed PMTU still fits (consumed by HandleReply here). One synthetic
|
|
||||||
// packet does the work of two.
|
|
||||||
//
|
|
||||||
// Returns true if a probe was sent. False means the caller should send a
|
|
||||||
// regular TestRequest at the floor.
|
|
||||||
//
|
|
||||||
// On probe failure, connection_manager's existing pendingDeletion timeout will
|
|
||||||
// tear the tunnel down. Heavy hammer, but correct: a re-handshake re-runs PMTUD
|
|
||||||
// discovery against the now-shrunken path. A future EMSGSIZE-capture followup
|
|
||||||
// can replace this with a soft-drop-and-research flow.
|
|
||||||
func (m *pmtudManager) MaybeProbeAsTest(hi *HostInfo) bool {
|
|
||||||
if !m.enabled.Load() || hi == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
v, ok := m.peers.Load(hi.localIndexId)
|
|
||||||
if !ok {
|
|
||||||
// Tunnel pre-dates the manager being aware of it (e.g. pmtud was just
|
|
||||||
// enabled live, or AddTrafficWatch fired before this call). Seed the
|
|
||||||
// peer so the wheel picks up the search; let connection_manager send
|
|
||||||
// its regular TestRequest this cycle.
|
|
||||||
m.seedPeer(hi)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
p := v.(*pmtudPeer)
|
|
||||||
p.mu.Lock()
|
|
||||||
if p.unsupported || p.applied == 0 {
|
|
||||||
p.mu.Unlock()
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
overhead := p.overhead()
|
|
||||||
size := p.applied + overhead
|
|
||||||
id := rand.Uint32()
|
|
||||||
p.inFlightSize = size
|
|
||||||
p.inFlightID = id
|
|
||||||
p.mu.Unlock()
|
|
||||||
|
|
||||||
m.sendProbe(hi, size, id, overhead)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleReply consumes an MTUDProbeReply payload from the receive path.
|
|
||||||
func (m *pmtudManager) HandleReply(localIdx uint32, payload []byte) {
|
|
||||||
if !m.enabled.Load() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(payload) < 8 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if binary.BigEndian.Uint32(payload[0:4]) != pmtudMagic {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
id := binary.BigEndian.Uint32(payload[4:8])
|
|
||||||
|
|
||||||
v, ok := m.peers.Load(localIdx)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
p := v.(*pmtudPeer)
|
|
||||||
p.mu.Lock()
|
|
||||||
defer p.mu.Unlock()
|
|
||||||
|
|
||||||
if p.inFlightSize == 0 || p.inFlightID != id {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
confirmed := p.inFlightSize
|
|
||||||
p.low = confirmed
|
|
||||||
p.inFlightSize = 0
|
|
||||||
p.losses = 0
|
|
||||||
p.everReplied = true
|
|
||||||
p.consecutiveFailures = 0
|
|
||||||
|
|
||||||
innerMTU := confirmed - p.overhead()
|
|
||||||
// Only install a /32 override when it would actually raise the MTU above the
|
|
||||||
// per-vpn-network floor route. If the discovered MTU is <= floor, the /24
|
|
||||||
// already covers it; installing a /32 at floor would just create roam churn.
|
|
||||||
if innerMTU > int(m.floor.Load()) && p.applied != innerMTU {
|
|
||||||
if err := m.device.SetPeerMTU(p.addr, innerMTU); err != nil {
|
|
||||||
m.l.Warn("pmtud: failed to apply per-peer mtu", "addr", p.addr, "innerMTU", innerMTU, "error", err)
|
|
||||||
} else {
|
|
||||||
m.l.Info("pmtud probe confirmed",
|
|
||||||
"addr", p.addr,
|
|
||||||
"outerMTU", confirmed,
|
|
||||||
"innerMTU", innerMTU,
|
|
||||||
"low", p.low,
|
|
||||||
"high", p.high,
|
|
||||||
)
|
|
||||||
p.applied = innerMTU
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.high-p.low <= pmtudConverged {
|
|
||||||
p.converged = true
|
|
||||||
} else {
|
|
||||||
p.converged = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// tick handles one wheel firing for a single peer.
|
|
||||||
func (m *pmtudManager) tick(localIdx uint32) {
|
|
||||||
v, ok := m.peers.Load(localIdx)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
p := v.(*pmtudPeer)
|
|
||||||
p.mu.Lock()
|
|
||||||
|
|
||||||
if p.unsupported {
|
|
||||||
p.mu.Unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// If a probe was outstanding, this tick is the loss timeout.
|
|
||||||
if p.inFlightSize != 0 {
|
|
||||||
p.losses++
|
|
||||||
p.consecutiveFailures++
|
|
||||||
if p.losses >= pmtudMaxLoss {
|
|
||||||
p.high = p.inFlightSize
|
|
||||||
p.inFlightSize = 0
|
|
||||||
p.losses = 0
|
|
||||||
if p.high-p.low <= pmtudConverged {
|
|
||||||
p.converged = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we've never gotten a reply from this peer and we've burned through our
|
|
||||||
// failure budget, conclude the peer doesn't understand the MTUDProbeRequest
|
|
||||||
// subtype and stop scheduling probes for it.
|
|
||||||
if !p.everReplied && p.consecutiveFailures >= pmtudUnsupportedAfter {
|
|
||||||
p.unsupported = true
|
|
||||||
addr := p.addr
|
|
||||||
p.mu.Unlock()
|
|
||||||
m.l.Info("pmtud: peer not responding to probes, marking unsupported",
|
|
||||||
"addr", addr, "failures", pmtudUnsupportedAfter)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
hi := m.intf.hostMap.QueryIndex(localIdx)
|
|
||||||
if hi == nil {
|
|
||||||
p.mu.Unlock()
|
|
||||||
m.peers.Delete(localIdx)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Once a peer converges, the wheel stops scheduling for it. Re-validation
|
|
||||||
// (and the resulting black hole detection) is driven by connection_manager
|
|
||||||
// via MaybeProbeAsTest at its natural test cadence, so a converged peer
|
|
||||||
// has nothing for the wheel to do until OnRoam or a tunnel down/up cycle
|
|
||||||
// triggers a fresh search.
|
|
||||||
if p.converged {
|
|
||||||
p.mu.Unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ceiling := int(m.ceiling.Load())
|
|
||||||
var size int
|
|
||||||
switch {
|
|
||||||
case p.firstProbe:
|
|
||||||
// Probe the ceiling directly. If the path supports it (the common case
|
|
||||||
// when an operator has explicitly configured tun.max_mtu), we converge
|
|
||||||
// in one round trip. If it fails, the standard binary search resumes
|
|
||||||
// on the next tick from the (low, ceiling) bounds.
|
|
||||||
size = ceiling
|
|
||||||
p.firstProbe = false
|
|
||||||
case p.losses > 0 && p.inFlightSize != 0:
|
|
||||||
size = p.inFlightSize
|
|
||||||
default:
|
|
||||||
size = p.midpoint()
|
|
||||||
}
|
|
||||||
if size < pmtudFloor {
|
|
||||||
size = pmtudFloor
|
|
||||||
}
|
|
||||||
if size > ceiling {
|
|
||||||
size = ceiling
|
|
||||||
}
|
|
||||||
|
|
||||||
id := rand.Uint32()
|
|
||||||
p.inFlightSize = size
|
|
||||||
p.inFlightID = id
|
|
||||||
overhead := p.overhead()
|
|
||||||
p.mu.Unlock()
|
|
||||||
|
|
||||||
m.sendProbe(hi, size, id, overhead)
|
|
||||||
m.wheel.Add(localIdx, pmtudProbeInterval)
|
|
||||||
}
|
|
||||||
|
|
||||||
// sendProbe builds an MTUDProbeRequest payload that will produce an outer IP
|
|
||||||
// packet of approximately `outerSize` bytes, then sends it.
|
|
||||||
func (m *pmtudManager) sendProbe(hi *HostInfo, outerSize int, id uint32, overhead int) {
|
|
||||||
payloadLen := outerSize - overhead
|
|
||||||
if payloadLen < 8 {
|
|
||||||
payloadLen = 8
|
|
||||||
}
|
|
||||||
p := make([]byte, payloadLen)
|
|
||||||
binary.BigEndian.PutUint32(p[0:4], pmtudMagic)
|
|
||||||
binary.BigEndian.PutUint32(p[4:8], id)
|
|
||||||
// remaining bytes are zero-padding
|
|
||||||
|
|
||||||
nb := make([]byte, 12)
|
|
||||||
out := make([]byte, outerSize+128) // headroom for header/tag/relay framing
|
|
||||||
m.intf.SendMessageToHostInfo(header.Test, header.MTUDProbeRequest, hi, p, nb, out)
|
|
||||||
}
|
|
||||||
+4
-156
@@ -18,7 +18,6 @@ type relayManager struct {
|
|||||||
l *slog.Logger
|
l *slog.Logger
|
||||||
hostmap *HostMap
|
hostmap *HostMap
|
||||||
amRelay atomic.Bool
|
amRelay atomic.Bool
|
||||||
useRelays atomic.Bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRelayManager(ctx context.Context, l *slog.Logger, hostmap *HostMap, c *config.C) *relayManager {
|
func NewRelayManager(ctx context.Context, l *slog.Logger, hostmap *HostMap, c *config.C) *relayManager {
|
||||||
@@ -37,10 +36,8 @@ func NewRelayManager(ctx context.Context, l *slog.Logger, hostmap *HostMap, c *c
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rm *relayManager) reload(c *config.C, initial bool) error {
|
func (rm *relayManager) reload(c *config.C, initial bool) error {
|
||||||
if initial || c.HasChanged("relay.am_relay") || c.HasChanged("relay.use_relays") {
|
if initial || c.HasChanged("relay.am_relay") {
|
||||||
amRelay := c.GetBool("relay.am_relay", false)
|
rm.setAmRelay(c.GetBool("relay.am_relay", false))
|
||||||
rm.amRelay.Store(amRelay)
|
|
||||||
rm.useRelays.Store(c.GetBool("relay.use_relays", true) && !amRelay)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -49,157 +46,8 @@ func (rm *relayManager) GetAmRelay() bool {
|
|||||||
return rm.amRelay.Load()
|
return rm.amRelay.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rm *relayManager) GetUseRelays() bool {
|
func (rm *relayManager) setAmRelay(v bool) {
|
||||||
return rm.useRelays.Load()
|
rm.amRelay.Store(v)
|
||||||
}
|
|
||||||
|
|
||||||
// StartRelays drives the relay-establishment side of an outbound handshake attempt.
|
|
||||||
// For each candidate relay it either kicks off a handshake to the relay, sends a CreateRelayRequest, retransmits
|
|
||||||
// one that may have been lost, or, once the relay is Established, forwards the in-progress
|
|
||||||
// stage 0 handshake packet for vpnIp through it.
|
|
||||||
func (rm *relayManager) StartRelays(f *Interface, vpnIp netip.Addr, hostinfo *HostInfo, stage0 []byte) {
|
|
||||||
if !rm.GetUseRelays() || len(hostinfo.remotes.relays) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
hostinfo.logger(rm.l).Info("Attempt to relay through hosts", "relays", hostinfo.remotes.relays)
|
|
||||||
// Send a RelayRequest to all known Relay IP's
|
|
||||||
for _, relay := range hostinfo.remotes.relays {
|
|
||||||
// Don't relay through the host I'm trying to connect to
|
|
||||||
if relay == vpnIp {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Don't relay to myself
|
|
||||||
if f.myVpnAddrsTable.Contains(relay) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
relayHostInfo := rm.hostmap.QueryVpnAddr(relay)
|
|
||||||
if relayHostInfo == nil || !relayHostInfo.remote.IsValid() {
|
|
||||||
hostinfo.logger(rm.l).Info("Establish tunnel to relay target", "relay", relay.String())
|
|
||||||
f.Handshake(relay)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// Check the relay HostInfo to see if we already established a relay through
|
|
||||||
existingRelay, ok := relayHostInfo.relayState.QueryRelayForByIp(vpnIp)
|
|
||||||
if !ok {
|
|
||||||
// No relays exist or requested yet.
|
|
||||||
if relayHostInfo.remote.IsValid() {
|
|
||||||
idx, err := AddRelay(rm.l, relayHostInfo, rm.hostmap, vpnIp, nil, TerminalType, Requested)
|
|
||||||
if err != nil {
|
|
||||||
hostinfo.logger(rm.l).Info("Failed to add relay to hostmap", "relay", relay.String(), "error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
m := NebulaControl{
|
|
||||||
Type: NebulaControl_CreateRelayRequest,
|
|
||||||
InitiatorRelayIndex: idx,
|
|
||||||
}
|
|
||||||
|
|
||||||
switch relayHostInfo.GetCert().Certificate.Version() {
|
|
||||||
case cert.Version1:
|
|
||||||
if !f.myVpnAddrs[0].Is4() {
|
|
||||||
hostinfo.logger(rm.l).Error("can not establish v1 relay with a v6 network because the relay is not running a current nebula version")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !vpnIp.Is4() {
|
|
||||||
hostinfo.logger(rm.l).Error("can not establish v1 relay with a v6 remote network because the relay is not running a current nebula version")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
b := f.myVpnAddrs[0].As4()
|
|
||||||
m.OldRelayFromAddr = binary.BigEndian.Uint32(b[:])
|
|
||||||
b = vpnIp.As4()
|
|
||||||
m.OldRelayToAddr = binary.BigEndian.Uint32(b[:])
|
|
||||||
case cert.Version2:
|
|
||||||
m.RelayFromAddr = netAddrToProtoAddr(f.myVpnAddrs[0])
|
|
||||||
m.RelayToAddr = netAddrToProtoAddr(vpnIp)
|
|
||||||
default:
|
|
||||||
hostinfo.logger(rm.l).Error("Unknown certificate version found while creating relay")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
msg, err := m.Marshal()
|
|
||||||
if err != nil {
|
|
||||||
hostinfo.logger(rm.l).Error("Failed to marshal Control message to create relay", "error", err)
|
|
||||||
} else {
|
|
||||||
f.SendMessageToHostInfo(header.Control, 0, relayHostInfo, msg, make([]byte, 12), make([]byte, mtu))
|
|
||||||
rm.l.Info("send CreateRelayRequest",
|
|
||||||
"relayFrom", f.myVpnAddrs[0],
|
|
||||||
"relayTo", vpnIp,
|
|
||||||
"initiatorRelayIndex", idx,
|
|
||||||
"relay", relay,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
switch existingRelay.State {
|
|
||||||
case Established:
|
|
||||||
hostinfo.logger(rm.l).Info("Send handshake via relay", "relay", relay.String())
|
|
||||||
f.SendVia(relayHostInfo, existingRelay, stage0, make([]byte, 12), make([]byte, mtu), false)
|
|
||||||
case Disestablished:
|
|
||||||
// Mark this relay as 'requested'
|
|
||||||
relayHostInfo.relayState.UpdateRelayForByIpState(vpnIp, Requested)
|
|
||||||
fallthrough
|
|
||||||
case Requested:
|
|
||||||
hostinfo.logger(rm.l).Info("Re-send CreateRelay request", "relay", relay.String())
|
|
||||||
// Re-send the CreateRelay request, in case the previous one was lost.
|
|
||||||
m := NebulaControl{
|
|
||||||
Type: NebulaControl_CreateRelayRequest,
|
|
||||||
InitiatorRelayIndex: existingRelay.LocalIndex,
|
|
||||||
}
|
|
||||||
|
|
||||||
switch relayHostInfo.GetCert().Certificate.Version() {
|
|
||||||
case cert.Version1:
|
|
||||||
if !f.myVpnAddrs[0].Is4() {
|
|
||||||
hostinfo.logger(rm.l).Error("can not establish v1 relay with a v6 network because the relay is not running a current nebula version")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !vpnIp.Is4() {
|
|
||||||
hostinfo.logger(rm.l).Error("can not establish v1 relay with a v6 remote network because the relay is not running a current nebula version")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
b := f.myVpnAddrs[0].As4()
|
|
||||||
m.OldRelayFromAddr = binary.BigEndian.Uint32(b[:])
|
|
||||||
b = vpnIp.As4()
|
|
||||||
m.OldRelayToAddr = binary.BigEndian.Uint32(b[:])
|
|
||||||
case cert.Version2:
|
|
||||||
m.RelayFromAddr = netAddrToProtoAddr(f.myVpnAddrs[0])
|
|
||||||
m.RelayToAddr = netAddrToProtoAddr(vpnIp)
|
|
||||||
default:
|
|
||||||
hostinfo.logger(rm.l).Error("Unknown certificate version found while creating relay")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
msg, err := m.Marshal()
|
|
||||||
if err != nil {
|
|
||||||
hostinfo.logger(rm.l).Error("Failed to marshal Control message to create relay", "error", err)
|
|
||||||
} else {
|
|
||||||
// This must send over the hostinfo, not over hm.Hosts[ip]
|
|
||||||
f.SendMessageToHostInfo(header.Control, 0, relayHostInfo, msg, make([]byte, 12), make([]byte, mtu))
|
|
||||||
rm.l.Info("send CreateRelayRequest",
|
|
||||||
"relayFrom", f.myVpnAddrs[0],
|
|
||||||
"relayTo", vpnIp,
|
|
||||||
"initiatorRelayIndex", existingRelay.LocalIndex,
|
|
||||||
"relay", relay,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
case PeerRequested:
|
|
||||||
// PeerRequested only occurs in Forwarding relays, not Terminal relays, and this is a Terminal relay case.
|
|
||||||
fallthrough
|
|
||||||
default:
|
|
||||||
hostinfo.logger(rm.l).Error("Relay unexpected state",
|
|
||||||
"vpnIp", vpnIp,
|
|
||||||
"state", existingRelay.State,
|
|
||||||
"relay", relay,
|
|
||||||
)
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddRelay finds an available relay index on the hostmap, and associates the relay info with it.
|
// AddRelay finds an available relay index on the hostmap, and associates the relay info with it.
|
||||||
|
|||||||
+22
-11
@@ -8,6 +8,12 @@ import (
|
|||||||
|
|
||||||
const MTU = 9001
|
const MTU = 9001
|
||||||
|
|
||||||
|
// MaxWriteBatch is the largest batch any Conn.WriteBatch implementation is
|
||||||
|
// required to accept. Callers SHOULD NOT pass more than this per call; Linux
|
||||||
|
// backends preallocate sendmmsg scratch sized to this value, so exceeding it
|
||||||
|
// only costs a chunked retry.
|
||||||
|
const MaxWriteBatch = 128
|
||||||
|
|
||||||
type EncReader func(
|
type EncReader func(
|
||||||
addr netip.AddrPort,
|
addr netip.AddrPort,
|
||||||
payload []byte,
|
payload []byte,
|
||||||
@@ -16,16 +22,21 @@ type EncReader func(
|
|||||||
type Conn interface {
|
type Conn interface {
|
||||||
Rebind() error
|
Rebind() error
|
||||||
LocalAddr() (netip.AddrPort, error)
|
LocalAddr() (netip.AddrPort, error)
|
||||||
ListenOut(r EncReader) error
|
// ListenOut invokes r for each received packet. On batch-capable
|
||||||
|
// backends (recvmmsg), flush is called after each batch is fully
|
||||||
|
// delivered — callers use it to flush per-batch accumulators such as
|
||||||
|
// TUN write coalescers. Single-packet backends call flush after each
|
||||||
|
// packet. flush must not be nil.
|
||||||
|
ListenOut(r EncReader, flush func()) error
|
||||||
WriteTo(b []byte, addr netip.AddrPort) error
|
WriteTo(b []byte, addr netip.AddrPort) error
|
||||||
|
// WriteBatch sends a contiguous batch of packets, each with its own
|
||||||
|
// destination. bufs and addrs must have the same length. Linux uses
|
||||||
|
// sendmmsg(2) for a single syscall; other backends fall back to a
|
||||||
|
// WriteTo loop. Returns on the first error; callers may observe a
|
||||||
|
// partial send if some packets went out before the error.
|
||||||
|
WriteBatch(bufs [][]byte, addrs []netip.AddrPort) error
|
||||||
ReloadConfig(c *config.C)
|
ReloadConfig(c *config.C)
|
||||||
SupportsMultipleReaders() bool
|
SupportsMultipleReaders() bool
|
||||||
// EnablePathMTUDiscovery sets the don't-fragment bit on outgoing packets for
|
|
||||||
// this socket. Called by the pmtud manager when PMTUD is enabled. A no-op on
|
|
||||||
// platforms that don't support it; nebula's default behavior (no DF, kernel
|
|
||||||
// fragmentation allowed) is preserved on those platforms and on this one when
|
|
||||||
// PMTUD is disabled.
|
|
||||||
EnablePathMTUDiscovery() error
|
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,7 +48,7 @@ func (NoopConn) Rebind() error {
|
|||||||
func (NoopConn) LocalAddr() (netip.AddrPort, error) {
|
func (NoopConn) LocalAddr() (netip.AddrPort, error) {
|
||||||
return netip.AddrPort{}, nil
|
return netip.AddrPort{}, nil
|
||||||
}
|
}
|
||||||
func (NoopConn) ListenOut(_ EncReader) error {
|
func (NoopConn) ListenOut(_ EncReader, _ func()) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (NoopConn) SupportsMultipleReaders() bool {
|
func (NoopConn) SupportsMultipleReaders() bool {
|
||||||
@@ -46,12 +57,12 @@ func (NoopConn) SupportsMultipleReaders() bool {
|
|||||||
func (NoopConn) WriteTo(_ []byte, _ netip.AddrPort) error {
|
func (NoopConn) WriteTo(_ []byte, _ netip.AddrPort) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
func (NoopConn) WriteBatch(_ [][]byte, _ []netip.AddrPort) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
func (NoopConn) ReloadConfig(_ *config.C) {
|
func (NoopConn) ReloadConfig(_ *config.C) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
func (NoopConn) EnablePathMTUDiscovery() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
func (NoopConn) Close() error {
|
func (NoopConn) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,17 +44,3 @@ func NewListenConfig(multi bool) net.ListenConfig {
|
|||||||
func (u *GenericConn) Rebind() error {
|
func (u *GenericConn) Rebind() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnablePathMTUDiscovery sets the don't-fragment bit on outbound packets.
|
|
||||||
// Android is Linux underneath, so we use IP_PMTUDISC_PROBE (kernel sets DF but
|
|
||||||
// does not consume incoming ICMP frag-needed for its PMTU cache; the manager
|
|
||||||
// drives discovery via authenticated probes).
|
|
||||||
func (u *GenericConn) EnablePathMTUDiscovery() error {
|
|
||||||
v4 := u.isV4Socket()
|
|
||||||
return u.controlFD(func(fd uintptr) error {
|
|
||||||
if v4 {
|
|
||||||
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_MTU_DISCOVER, unix.IP_PMTUDISC_PROBE)
|
|
||||||
}
|
|
||||||
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_MTU_DISCOVER, unix.IPV6_PMTUDISC_PROBE)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -47,8 +47,3 @@ func NewListenConfig(multi bool) net.ListenConfig {
|
|||||||
func (u *GenericConn) Rebind() error {
|
func (u *GenericConn) Rebind() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnablePathMTUDiscovery is split into per-OS files: udp_freebsd.go handles
|
|
||||||
// FreeBSD (which has both IP_DONTFRAG and IPV6_DONTFRAG in the unix package);
|
|
||||||
// udp_openbsd.go handles OpenBSD (v6 only; the kernel doesn't expose a v4 DF
|
|
||||||
// sockopt).
|
|
||||||
|
|||||||
+11
-12
@@ -140,6 +140,15 @@ func (u *StdConn) WriteTo(b []byte, ap netip.AddrPort) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *StdConn) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) error {
|
||||||
|
for i, b := range bufs {
|
||||||
|
if err := u.WriteTo(b, addrs[i]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (u *StdConn) LocalAddr() (netip.AddrPort, error) {
|
func (u *StdConn) LocalAddr() (netip.AddrPort, error) {
|
||||||
a := u.UDPConn.LocalAddr()
|
a := u.UDPConn.LocalAddr()
|
||||||
|
|
||||||
@@ -165,7 +174,7 @@ func NewUDPStatsEmitter(udpConns []Conn) func() {
|
|||||||
return func() {}
|
return func() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *StdConn) ListenOut(r EncReader) error {
|
func (u *StdConn) ListenOut(r EncReader, flush func()) error {
|
||||||
buffer := make([]byte, MTU)
|
buffer := make([]byte, MTU)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -180,6 +189,7 @@ func (u *StdConn) ListenOut(r EncReader) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r(netip.AddrPortFrom(rua.Addr().Unmap(), rua.Port()), buffer[:n])
|
r(netip.AddrPortFrom(rua.Addr().Unmap(), rua.Port()), buffer[:n])
|
||||||
|
flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,17 +197,6 @@ func (u *StdConn) SupportsMultipleReaders() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnablePathMTUDiscovery sets the don't-fragment bit on every outbound packet.
|
|
||||||
// On darwin we use IP_DONTFRAG (v4) / IPV6_DONTFRAG (v6). The kernel will return
|
|
||||||
// EMSGSIZE for sends that exceed the local interface MTU; ICMP-driven PMTU
|
|
||||||
// updates from upstream routers are processed by the kernel as usual.
|
|
||||||
func (u *StdConn) EnablePathMTUDiscovery() error {
|
|
||||||
if u.isV4 {
|
|
||||||
return syscall.SetsockoptInt(int(u.sysFd), syscall.IPPROTO_IP, unix.IP_DONTFRAG, 1)
|
|
||||||
}
|
|
||||||
return syscall.SetsockoptInt(int(u.sysFd), syscall.IPPROTO_IPV6, unix.IPV6_DONTFRAG, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *StdConn) Rebind() error {
|
func (u *StdConn) Rebind() error {
|
||||||
var err error
|
var err error
|
||||||
if u.isV4 {
|
if u.isV4 {
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
//go:build freebsd && !e2e_testing
|
|
||||||
// +build freebsd,!e2e_testing
|
|
||||||
|
|
||||||
package udp
|
|
||||||
|
|
||||||
import (
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
// EnablePathMTUDiscovery sets the don't-fragment bit on outbound packets.
|
|
||||||
// FreeBSD exposes IP_DONTFRAG (v4) and IPV6_DONTFRAG (v6) in golang.org/x/sys/unix.
|
|
||||||
// Unlike Linux, BSDs don't have an explicit "don't consume incoming ICMP
|
|
||||||
// frag-needed" knob for unconnected UDP sockets; the kernel's PMTU cache will
|
|
||||||
// be updated from ICMP, which is benign for our usage (the cache only affects
|
|
||||||
// what EMSGSIZE gets surfaced for; the manager drives its own discovery via
|
|
||||||
// authenticated probes).
|
|
||||||
func (u *GenericConn) EnablePathMTUDiscovery() error {
|
|
||||||
v4 := u.isV4Socket()
|
|
||||||
return u.controlFD(func(fd uintptr) error {
|
|
||||||
if v4 {
|
|
||||||
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_DONTFRAG, 1)
|
|
||||||
}
|
|
||||||
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_DONTFRAG, 1)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
+11
-39
@@ -44,6 +44,15 @@ func (u *GenericConn) WriteTo(b []byte, addr netip.AddrPort) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *GenericConn) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) error {
|
||||||
|
for i, b := range bufs {
|
||||||
|
if _, err := u.UDPConn.WriteToUDPAddrPort(b, addrs[i]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (u *GenericConn) LocalAddr() (netip.AddrPort, error) {
|
func (u *GenericConn) LocalAddr() (netip.AddrPort, error) {
|
||||||
a := u.UDPConn.LocalAddr()
|
a := u.UDPConn.LocalAddr()
|
||||||
|
|
||||||
@@ -73,7 +82,7 @@ type rawMessage struct {
|
|||||||
Len uint32
|
Len uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *GenericConn) ListenOut(r EncReader) error {
|
func (u *GenericConn) ListenOut(r EncReader, flush func()) error {
|
||||||
buffer := make([]byte, MTU)
|
buffer := make([]byte, MTU)
|
||||||
|
|
||||||
var lastRecvErr time.Time
|
var lastRecvErr time.Time
|
||||||
@@ -94,47 +103,10 @@ func (u *GenericConn) ListenOut(r EncReader) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r(netip.AddrPortFrom(rua.Addr().Unmap(), rua.Port()), buffer[:n])
|
r(netip.AddrPortFrom(rua.Addr().Unmap(), rua.Port()), buffer[:n])
|
||||||
|
flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *GenericConn) SupportsMultipleReaders() bool {
|
func (u *GenericConn) SupportsMultipleReaders() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnablePathMTUDiscovery is implemented per-platform alongside Rebind, in
|
|
||||||
// udp_android.go / udp_bsd.go / udp_netbsd.go / udp_windows.go.
|
|
||||||
|
|
||||||
// controlFD invokes f with the underlying UDP socket file descriptor (or
|
|
||||||
// handle, on Windows). Used by platform files for setsockopt calls that the
|
|
||||||
// stdlib net.UDPConn does not expose directly.
|
|
||||||
func (u *GenericConn) controlFD(f func(fd uintptr) error) error {
|
|
||||||
rc, err := u.UDPConn.SyscallConn()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
var sockErr error
|
|
||||||
err = rc.Control(func(fd uintptr) {
|
|
||||||
sockErr = f(fd)
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return sockErr
|
|
||||||
}
|
|
||||||
|
|
||||||
// isV4Socket reports whether the local bind address looks like an IPv4 socket.
|
|
||||||
// Used by EnablePathMTUDiscovery to pick IPPROTO_IP vs IPPROTO_IPV6 socket
|
|
||||||
// options. Assumes pure-v4 or pure-v6 sockets; a dual-stack v6 socket bound to
|
|
||||||
// :: will be treated as v6 (correct: setting IPV6_DONTFRAG covers v4-mapped
|
|
||||||
// traffic too on most stacks).
|
|
||||||
func (u *GenericConn) isV4Socket() bool {
|
|
||||||
la := u.UDPConn.LocalAddr()
|
|
||||||
if la == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
ua, ok := la.(*net.UDPAddr)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return ua.IP.To4() != nil
|
|
||||||
}
|
|
||||||
|
|||||||
+405
-34
@@ -24,6 +24,43 @@ type StdConn struct {
|
|||||||
isV4 bool
|
isV4 bool
|
||||||
l *slog.Logger
|
l *slog.Logger
|
||||||
batch int
|
batch int
|
||||||
|
|
||||||
|
// sendmmsg scratch. Each queue has its own StdConn, so no locking is
|
||||||
|
// needed. Sized to MaxWriteBatch at construction; WriteBatch chunks
|
||||||
|
// larger inputs.
|
||||||
|
writeMsgs []rawMessage
|
||||||
|
writeIovs []iovec
|
||||||
|
writeNames [][]byte
|
||||||
|
|
||||||
|
// Per-entry UDP_SEGMENT cmsg scratch. writeCmsg is one contiguous slab
|
||||||
|
// of MaxWriteBatch * writeCmsgSpace bytes; each entry's cmsg header is
|
||||||
|
// pre-filled once in prepareWriteMessages. WriteBatch only rewrites the
|
||||||
|
// 2-byte gso_size payload (and toggles Hdr.Control on/off) per call.
|
||||||
|
writeCmsg []byte
|
||||||
|
writeCmsgSpace int
|
||||||
|
|
||||||
|
// writeEntryEnd[e] is the bufs index *after* the last packet packed
|
||||||
|
// into mmsghdr entry e. Used to rewind `i` on partial sendmmsg success.
|
||||||
|
writeEntryEnd []int
|
||||||
|
|
||||||
|
// Preallocated closure + in/out slots for sendmmsg, so the hot path
|
||||||
|
// does not heap-allocate a fresh closure per call.
|
||||||
|
writeChunk int
|
||||||
|
writeSent int
|
||||||
|
writeErrno syscall.Errno
|
||||||
|
writeFunc func(fd uintptr) bool
|
||||||
|
|
||||||
|
// UDP GSO (sendmsg with UDP_SEGMENT cmsg) support. gsoSupported is
|
||||||
|
// probed once at socket creation. When true, WriteSegmented takes a
|
||||||
|
// single-syscall GSO path; otherwise it falls back to a WriteTo loop.
|
||||||
|
gsoSupported bool
|
||||||
|
|
||||||
|
// UDP GRO (recvmsg with UDP_GRO cmsg) support. groSupported is probed
|
||||||
|
// once at socket creation. When true, listenOutBatch allocates larger
|
||||||
|
// RX buffers and a per-entry cmsg slot so the kernel can coalesce
|
||||||
|
// consecutive same-flow datagrams into a single recvmmsg entry; the
|
||||||
|
// delivered cmsg carries the gso_size used to split them back apart.
|
||||||
|
groSupported bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func setReusePort(network, address string, c syscall.RawConn) error {
|
func setReusePort(network, address string, c syscall.RawConn) error {
|
||||||
@@ -70,22 +107,100 @@ func NewListener(l *slog.Logger, ip netip.Addr, port int, multi bool, batch int)
|
|||||||
}
|
}
|
||||||
out.isV4 = af == unix.AF_INET
|
out.isV4 = af == unix.AF_INET
|
||||||
|
|
||||||
|
out.prepareWriteMessages(MaxWriteBatch)
|
||||||
|
out.writeFunc = out.sendmmsgRawWrite
|
||||||
|
|
||||||
|
out.prepareGSO()
|
||||||
|
// GRO delivers coalesced superpackets that need a cmsg to split back
|
||||||
|
// into segments. The single-packet RX path uses ReadFromUDPAddrPort
|
||||||
|
// and cannot see that cmsg, so only enable GRO for the batch path.
|
||||||
|
if batch > 1 {
|
||||||
|
out.prepareGRO()
|
||||||
|
}
|
||||||
|
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnablePathMTUDiscovery sets IP_MTU_DISCOVER=IP_PMTUDISC_PROBE (IPV6 equivalent
|
// prepareWriteMessages allocates one mmsghdr/iovec/sockaddr/cmsg scratch
|
||||||
// for v6 sockets). This sets the don't-fragment bit on every outbound packet but
|
// slot per sendmmsg entry. The iovec slab is sized to the same n so a
|
||||||
// tells the kernel not to consume incoming ICMP frag-needed for its own PMTU
|
// single entry can fan out to up to n iovecs (needed for UDP_SEGMENT runs
|
||||||
// cache; we drive PMTU discovery from the application via authenticated probes
|
// that coalesce consecutive bufs into one entry). Hdr.Iov / Hdr.Iovlen /
|
||||||
// (RFC 8899). Called by the pmtud manager when PMTUD is enabled. Without this
|
// Hdr.Control / Hdr.Controllen are wired per call since each entry can
|
||||||
// call the socket retains nebula's historical behavior (no DF, kernel may
|
// span a variable number of iovecs and may or may not carry a cmsg.
|
||||||
// fragment), preserving compatibility with deployments that depend on UDP
|
func (u *StdConn) prepareWriteMessages(n int) {
|
||||||
// fragmentation.
|
u.writeMsgs = make([]rawMessage, n)
|
||||||
func (u *StdConn) EnablePathMTUDiscovery() error {
|
u.writeIovs = make([]iovec, n)
|
||||||
if u.isV4 {
|
u.writeNames = make([][]byte, n)
|
||||||
return u.setSockOptIPInt(unix.IPPROTO_IP, unix.IP_MTU_DISCOVER, unix.IP_PMTUDISC_PROBE)
|
u.writeEntryEnd = make([]int, n)
|
||||||
|
|
||||||
|
u.writeCmsgSpace = unix.CmsgSpace(2)
|
||||||
|
u.writeCmsg = make([]byte, n*u.writeCmsgSpace)
|
||||||
|
for k := 0; k < n; k++ {
|
||||||
|
off := k * u.writeCmsgSpace
|
||||||
|
h := (*unix.Cmsghdr)(unsafe.Pointer(&u.writeCmsg[off]))
|
||||||
|
h.Level = unix.SOL_UDP
|
||||||
|
h.Type = unix.UDP_SEGMENT
|
||||||
|
setCmsgLen(h, unix.CmsgLen(2))
|
||||||
}
|
}
|
||||||
return u.setSockOptIPInt(unix.IPPROTO_IPV6, unix.IPV6_MTU_DISCOVER, unix.IPV6_PMTUDISC_PROBE)
|
|
||||||
|
for i := range u.writeMsgs {
|
||||||
|
u.writeNames[i] = make([]byte, unix.SizeofSockaddrInet6)
|
||||||
|
u.writeMsgs[i].Hdr.Name = &u.writeNames[i][0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// maxGSOSegments caps the per-sendmsg GSO fan-out. Linux kernels have
|
||||||
|
// historically capped UDP_MAX_SEGMENTS at 64; newer kernels raise it to 128
|
||||||
|
// but we stay conservative so the same code works everywhere.
|
||||||
|
const maxGSOSegments = 64
|
||||||
|
|
||||||
|
// maxGSOBytes bounds the total payload per sendmsg() when UDP_SEGMENT is
|
||||||
|
// set. The kernel stitches all iovecs into a single skb whose length the
|
||||||
|
// UDP length field can represent, and also enforces sk_gso_max_size (which
|
||||||
|
// on most devices is 65536). We use 65535 so ciphertext + headers always
|
||||||
|
// fits, avoiding EMSGSIZE on large TSO superpackets.
|
||||||
|
const maxGSOBytes = 65535
|
||||||
|
|
||||||
|
// prepareGSO probes UDP_SEGMENT support
|
||||||
|
func (u *StdConn) prepareGSO() {
|
||||||
|
var probeErr error
|
||||||
|
if err := u.rawConn.Control(func(fd uintptr) {
|
||||||
|
probeErr = unix.SetsockoptInt(int(fd), unix.IPPROTO_UDP, unix.UDP_SEGMENT, 0)
|
||||||
|
}); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if probeErr != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u.gsoSupported = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// udpGROBufferSize sizes the per-entry recvmmsg buffer when UDP_GRO is on.
|
||||||
|
// The kernel stitches a run of same-flow datagrams into a single skb whose
|
||||||
|
// length is bounded by sk_gso_max_size (typically 65535); anything larger
|
||||||
|
// would be MSG_TRUNCed. We use the maximum representable UDP length so a
|
||||||
|
// full superpacket always lands intact.
|
||||||
|
const udpGROBufferSize = 65535
|
||||||
|
|
||||||
|
// udpGROCmsgPayload is the size of the UDP_GRO cmsg data delivered by the
|
||||||
|
// kernel: a single int (gso_size in bytes). See udp_cmsg_recv() in
|
||||||
|
// net/ipv4/udp.c.
|
||||||
|
const udpGROCmsgPayload = 4
|
||||||
|
|
||||||
|
// prepareGRO turns on UDP_GRO so the kernel coalesces consecutive same-flow
|
||||||
|
// datagrams into one recvmmsg entry, with a cmsg carrying the gso_size used
|
||||||
|
// to split them back apart on the application side.
|
||||||
|
func (u *StdConn) prepareGRO() {
|
||||||
|
var probeErr error
|
||||||
|
if err := u.rawConn.Control(func(fd uintptr) {
|
||||||
|
probeErr = unix.SetsockoptInt(int(fd), unix.IPPROTO_UDP, unix.UDP_GRO, 1)
|
||||||
|
}); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if probeErr != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u.groSupported = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *StdConn) SupportsMultipleReaders() bool {
|
func (u *StdConn) SupportsMultipleReaders() bool {
|
||||||
@@ -125,21 +240,6 @@ func (u *StdConn) setSockOptInt(opt int, n int) error {
|
|||||||
return opErr
|
return opErr
|
||||||
}
|
}
|
||||||
|
|
||||||
// setSockOptIPInt sets a socket option at a non-SOL_SOCKET level (e.g. IPPROTO_IP).
|
|
||||||
func (u *StdConn) setSockOptIPInt(level, opt, n int) error {
|
|
||||||
if u.rawConn == nil {
|
|
||||||
return fmt.Errorf("no UDP connection")
|
|
||||||
}
|
|
||||||
var opErr error
|
|
||||||
err := u.rawConn.Control(func(fd uintptr) {
|
|
||||||
opErr = unix.SetsockoptInt(int(fd), level, opt, n)
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return opErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *StdConn) SetRecvBuffer(n int) error {
|
func (u *StdConn) SetRecvBuffer(n int) error {
|
||||||
return u.setSockOptInt(unix.SO_RCVBUFFORCE, n)
|
return u.setSockOptInt(unix.SO_RCVBUFFORCE, n)
|
||||||
}
|
}
|
||||||
@@ -201,7 +301,7 @@ func recvmmsg(fd uintptr, msgs []rawMessage) (int, bool, error) {
|
|||||||
return int(n), true, nil
|
return int(n), true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *StdConn) listenOutSingle(r EncReader) error {
|
func (u *StdConn) listenOutSingle(r EncReader, flush func()) error {
|
||||||
var err error
|
var err error
|
||||||
var n int
|
var n int
|
||||||
var from netip.AddrPort
|
var from netip.AddrPort
|
||||||
@@ -214,15 +314,22 @@ func (u *StdConn) listenOutSingle(r EncReader) error {
|
|||||||
}
|
}
|
||||||
from = netip.AddrPortFrom(from.Addr().Unmap(), from.Port())
|
from = netip.AddrPortFrom(from.Addr().Unmap(), from.Port())
|
||||||
r(from, buffer[:n])
|
r(from, buffer[:n])
|
||||||
|
flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *StdConn) listenOutBatch(r EncReader) error {
|
func (u *StdConn) listenOutBatch(r EncReader, flush func()) error {
|
||||||
var ip netip.Addr
|
var ip netip.Addr
|
||||||
var n int
|
var n int
|
||||||
var operr error
|
var operr error
|
||||||
|
|
||||||
msgs, buffers, names := u.PrepareRawMessages(u.batch)
|
bufSize := MTU
|
||||||
|
cmsgSpace := 0
|
||||||
|
if u.groSupported {
|
||||||
|
bufSize = udpGROBufferSize
|
||||||
|
cmsgSpace = unix.CmsgSpace(udpGROCmsgPayload)
|
||||||
|
}
|
||||||
|
msgs, buffers, names, _ := u.PrepareRawMessages(u.batch, bufSize, cmsgSpace)
|
||||||
|
|
||||||
//reader needs to capture variables from this function, since it's used as a lambda with rawConn.Read
|
//reader needs to capture variables from this function, since it's used as a lambda with rawConn.Read
|
||||||
//defining it outside the loop so it gets re-used
|
//defining it outside the loop so it gets re-used
|
||||||
@@ -232,6 +339,11 @@ func (u *StdConn) listenOutBatch(r EncReader) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
if cmsgSpace > 0 {
|
||||||
|
for i := range msgs {
|
||||||
|
setMsgControllen(&msgs[i].Hdr, cmsgSpace)
|
||||||
|
}
|
||||||
|
}
|
||||||
err := u.rawConn.Read(reader)
|
err := u.rawConn.Read(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -247,16 +359,72 @@ func (u *StdConn) listenOutBatch(r EncReader) error {
|
|||||||
} else {
|
} else {
|
||||||
ip, _ = netip.AddrFromSlice(names[i][8:24])
|
ip, _ = netip.AddrFromSlice(names[i][8:24])
|
||||||
}
|
}
|
||||||
r(netip.AddrPortFrom(ip.Unmap(), binary.BigEndian.Uint16(names[i][2:4])), buffers[i][:msgs[i].Len])
|
from := netip.AddrPortFrom(ip.Unmap(), binary.BigEndian.Uint16(names[i][2:4]))
|
||||||
|
payload := buffers[i][:msgs[i].Len]
|
||||||
|
|
||||||
|
segSize := 0
|
||||||
|
if u.groSupported {
|
||||||
|
segSize = parseUDPGRO(&msgs[i].Hdr)
|
||||||
}
|
}
|
||||||
|
if segSize <= 0 || segSize >= len(payload) {
|
||||||
|
// No coalescing happened (or a lone datagram).
|
||||||
|
r(from, payload)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// GRO superpacket: the kernel guarantees every segment is
|
||||||
|
// exactly segSize bytes except for the final one, which may be
|
||||||
|
// short.
|
||||||
|
for off := 0; off < len(payload); off += segSize {
|
||||||
|
end := off + segSize
|
||||||
|
if end > len(payload) {
|
||||||
|
end = len(payload)
|
||||||
|
}
|
||||||
|
r(from, payload[off:end])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// End-of-batch: let callers (e.g. TUN write coalescer) flush any
|
||||||
|
// state they accumulated across this batch.
|
||||||
|
flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *StdConn) ListenOut(r EncReader) error {
|
// parseUDPGRO walks the control buffer on hdr looking for a SOL_UDP/UDP_GRO
|
||||||
|
// cmsg and returns the gso_size (bytes per coalesced segment) it carries.
|
||||||
|
// Returns 0 when no UDP_GRO cmsg is present, which is the normal case for
|
||||||
|
// lone datagrams that the kernel did not coalesce.
|
||||||
|
func parseUDPGRO(hdr *msghdr) int {
|
||||||
|
controllen := int(hdr.Controllen)
|
||||||
|
if controllen < unix.SizeofCmsghdr || hdr.Control == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
ctrl := unsafe.Slice(hdr.Control, controllen)
|
||||||
|
off := 0
|
||||||
|
for off+unix.SizeofCmsghdr <= len(ctrl) {
|
||||||
|
ch := (*unix.Cmsghdr)(unsafe.Pointer(&ctrl[off]))
|
||||||
|
clen := int(ch.Len)
|
||||||
|
if clen < unix.SizeofCmsghdr || off+clen > len(ctrl) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if ch.Level == unix.SOL_UDP && ch.Type == unix.UDP_GRO {
|
||||||
|
dataOff := off + unix.CmsgLen(0)
|
||||||
|
if dataOff+udpGROCmsgPayload <= len(ctrl) {
|
||||||
|
return int(int32(binary.NativeEndian.Uint32(ctrl[dataOff : dataOff+udpGROCmsgPayload])))
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
// Advance by the aligned cmsg space. CmsgSpace(n) is the stride
|
||||||
|
// from one header to the next (len aligned up to the platform's
|
||||||
|
// cmsg alignment).
|
||||||
|
off += unix.CmsgSpace(clen - unix.CmsgLen(0))
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *StdConn) ListenOut(r EncReader, flush func()) error {
|
||||||
if u.batch == 1 {
|
if u.batch == 1 {
|
||||||
return u.listenOutSingle(r)
|
return u.listenOutSingle(r, flush)
|
||||||
} else {
|
} else {
|
||||||
return u.listenOutBatch(r)
|
return u.listenOutBatch(r, flush)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -265,6 +433,209 @@ func (u *StdConn) WriteTo(b []byte, ip netip.AddrPort) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteBatch sends bufs via sendmmsg(2) using the preallocated scratch on
|
||||||
|
// StdConn. Consecutive packets to the same destination with matching segment
|
||||||
|
// sizes (all but possibly the last) are coalesced into a single mmsghdr entry
|
||||||
|
// carrying a UDP_SEGMENT cmsg, so one syscall can mix runs of GSO superpackets
|
||||||
|
// with plain one-off datagrams. Without GSO support every packet is its own
|
||||||
|
// entry, matching the prior behaviour.
|
||||||
|
//
|
||||||
|
// Chunks larger than the scratch are processed across multiple syscalls. If
|
||||||
|
// sendmmsg returns a fatal error before any entry is sent we fall back to
|
||||||
|
// per-packet WriteTo for that chunk so the caller still gets best-effort
|
||||||
|
// delivery.
|
||||||
|
func (u *StdConn) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) error {
|
||||||
|
if len(bufs) != len(addrs) {
|
||||||
|
return fmt.Errorf("WriteBatch: len(bufs)=%d != len(addrs)=%d", len(bufs), len(addrs))
|
||||||
|
}
|
||||||
|
|
||||||
|
i := 0
|
||||||
|
for i < len(bufs) {
|
||||||
|
baseI := i
|
||||||
|
entry := 0
|
||||||
|
iovIdx := 0
|
||||||
|
|
||||||
|
for entry < len(u.writeMsgs) && i < len(bufs) {
|
||||||
|
iovBudget := len(u.writeIovs) - iovIdx
|
||||||
|
if iovBudget < 1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
runLen, segSize := u.planRun(bufs, addrs, i, iovBudget)
|
||||||
|
if runLen == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for k := 0; k < runLen; k++ {
|
||||||
|
b := bufs[i+k]
|
||||||
|
if len(b) == 0 {
|
||||||
|
u.writeIovs[iovIdx+k].Base = nil
|
||||||
|
setIovLen(&u.writeIovs[iovIdx+k], 0)
|
||||||
|
} else {
|
||||||
|
u.writeIovs[iovIdx+k].Base = &b[0]
|
||||||
|
setIovLen(&u.writeIovs[iovIdx+k], len(b))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nlen, err := writeSockaddr(u.writeNames[entry], addrs[i], u.isV4)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hdr := &u.writeMsgs[entry].Hdr
|
||||||
|
hdr.Iov = &u.writeIovs[iovIdx]
|
||||||
|
setMsgIovlen(hdr, runLen)
|
||||||
|
hdr.Namelen = uint32(nlen)
|
||||||
|
|
||||||
|
if runLen >= 2 {
|
||||||
|
off := entry * u.writeCmsgSpace
|
||||||
|
dataOff := off + unix.CmsgLen(0)
|
||||||
|
binary.NativeEndian.PutUint16(u.writeCmsg[dataOff:dataOff+2], uint16(segSize))
|
||||||
|
hdr.Control = &u.writeCmsg[off]
|
||||||
|
setMsgControllen(hdr, u.writeCmsgSpace)
|
||||||
|
} else {
|
||||||
|
hdr.Control = nil
|
||||||
|
setMsgControllen(hdr, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
i += runLen
|
||||||
|
iovIdx += runLen
|
||||||
|
u.writeEntryEnd[entry] = i
|
||||||
|
entry++
|
||||||
|
}
|
||||||
|
|
||||||
|
if entry == 0 {
|
||||||
|
return fmt.Errorf("sendmmsg: no progress")
|
||||||
|
}
|
||||||
|
|
||||||
|
sent, serr := u.sendmmsg(entry)
|
||||||
|
if serr != nil && sent <= 0 {
|
||||||
|
// Nothing went out for this chunk; fall back to WriteTo for each
|
||||||
|
// packet that was queued this iteration.
|
||||||
|
for k := baseI; k < i; k++ {
|
||||||
|
if werr := u.WriteTo(bufs[k], addrs[k]); werr != nil {
|
||||||
|
return werr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if sent == 0 {
|
||||||
|
return fmt.Errorf("sendmmsg made no progress")
|
||||||
|
}
|
||||||
|
// Rewind i to the end of the last successfully sent entry. For a
|
||||||
|
// full-success send this leaves i unchanged; for a partial send it
|
||||||
|
// replays the remainder on the next outer-loop iteration.
|
||||||
|
i = u.writeEntryEnd[sent-1]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// planRun groups consecutive packets starting at `start` that can be sent as
|
||||||
|
// a single UDP GSO superpacket (one sendmmsg entry with UDP_SEGMENT cmsg).
|
||||||
|
// A run of length 1 means the entry carries no cmsg and the kernel treats
|
||||||
|
// it as a plain datagram. Returns the run length and the per-segment size
|
||||||
|
// (which equals len(bufs[start])). Without GSO support every call returns
|
||||||
|
// runLen=1.
|
||||||
|
func (u *StdConn) planRun(bufs [][]byte, addrs []netip.AddrPort, start, iovBudget int) (int, int) {
|
||||||
|
if start >= len(bufs) || iovBudget < 1 {
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
segSize := len(bufs[start])
|
||||||
|
if !u.gsoSupported || segSize == 0 || segSize > maxGSOBytes {
|
||||||
|
return 1, segSize
|
||||||
|
}
|
||||||
|
dst := addrs[start]
|
||||||
|
maxLen := maxGSOSegments
|
||||||
|
if iovBudget < maxLen {
|
||||||
|
maxLen = iovBudget
|
||||||
|
}
|
||||||
|
runLen := 1
|
||||||
|
total := segSize
|
||||||
|
for runLen < maxLen && start+runLen < len(bufs) {
|
||||||
|
nextLen := len(bufs[start+runLen])
|
||||||
|
if nextLen == 0 || nextLen > segSize {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if addrs[start+runLen] != dst {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if total+nextLen > maxGSOBytes {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
total += nextLen
|
||||||
|
runLen++
|
||||||
|
if nextLen < segSize {
|
||||||
|
// A short packet must be the last in the run.
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return runLen, segSize
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendmmsgRawWrite is the preallocated callback passed to rawConn.Write. It
|
||||||
|
// reads its input (u.writeChunk) and writes its outputs (u.writeSent,
|
||||||
|
// u.writeErrno) through StdConn fields so the closure itself does not
|
||||||
|
// capture per-call locals and therefore does not heap-allocate.
|
||||||
|
func (u *StdConn) sendmmsgRawWrite(fd uintptr) bool {
|
||||||
|
r1, _, errno := unix.Syscall6(
|
||||||
|
unix.SYS_SENDMMSG,
|
||||||
|
fd,
|
||||||
|
uintptr(unsafe.Pointer(&u.writeMsgs[0])),
|
||||||
|
uintptr(u.writeChunk),
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
if errno == syscall.EAGAIN || errno == syscall.EWOULDBLOCK {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
u.writeSent = int(r1)
|
||||||
|
u.writeErrno = errno
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *StdConn) sendmmsg(n int) (int, error) {
|
||||||
|
u.writeChunk = n
|
||||||
|
u.writeSent = 0
|
||||||
|
u.writeErrno = 0
|
||||||
|
if err := u.rawConn.Write(u.writeFunc); err != nil {
|
||||||
|
return u.writeSent, err
|
||||||
|
}
|
||||||
|
if u.writeErrno != 0 {
|
||||||
|
return u.writeSent, &net.OpError{Op: "sendmmsg", Err: u.writeErrno}
|
||||||
|
}
|
||||||
|
return u.writeSent, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeSockaddr encodes addr into buf (which must be at least
|
||||||
|
// SizeofSockaddrInet6 bytes). Returns the number of bytes used. If isV4 is
|
||||||
|
// true and addr is not a v4 (or v4-in-v6) address, returns an error.
|
||||||
|
func writeSockaddr(buf []byte, addr netip.AddrPort, isV4 bool) (int, error) {
|
||||||
|
ap := addr.Addr().Unmap()
|
||||||
|
if isV4 {
|
||||||
|
if !ap.Is4() {
|
||||||
|
return 0, ErrInvalidIPv6RemoteForSocket
|
||||||
|
}
|
||||||
|
// struct sockaddr_in: { sa_family_t(2), in_port_t(2, BE), in_addr(4), zero(8) }
|
||||||
|
// sa_family is host endian.
|
||||||
|
binary.NativeEndian.PutUint16(buf[0:2], unix.AF_INET)
|
||||||
|
binary.BigEndian.PutUint16(buf[2:4], addr.Port())
|
||||||
|
ip4 := ap.As4()
|
||||||
|
copy(buf[4:8], ip4[:])
|
||||||
|
for j := 8; j < 16; j++ {
|
||||||
|
buf[j] = 0
|
||||||
|
}
|
||||||
|
return unix.SizeofSockaddrInet4, nil
|
||||||
|
}
|
||||||
|
// struct sockaddr_in6: { sa_family_t(2), in_port_t(2, BE), flowinfo(4), in6_addr(16), scope_id(4) }
|
||||||
|
binary.NativeEndian.PutUint16(buf[0:2], unix.AF_INET6)
|
||||||
|
binary.BigEndian.PutUint16(buf[2:4], addr.Port())
|
||||||
|
binary.NativeEndian.PutUint32(buf[4:8], 0)
|
||||||
|
ip6 := addr.Addr().As16()
|
||||||
|
copy(buf[8:24], ip6[:])
|
||||||
|
binary.NativeEndian.PutUint32(buf[24:28], 0)
|
||||||
|
return unix.SizeofSockaddrInet6, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (u *StdConn) ReloadConfig(c *config.C) {
|
func (u *StdConn) ReloadConfig(c *config.C) {
|
||||||
b := c.GetInt("listen.read_buffer", 0)
|
b := c.GetInt("listen.read_buffer", 0)
|
||||||
if b > 0 {
|
if b > 0 {
|
||||||
|
|||||||
+29
-3
@@ -30,13 +30,18 @@ type rawMessage struct {
|
|||||||
Len uint32
|
Len uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *StdConn) PrepareRawMessages(n int) ([]rawMessage, [][]byte, [][]byte) {
|
func (u *StdConn) PrepareRawMessages(n, bufSize, cmsgSpace int) ([]rawMessage, [][]byte, [][]byte, []byte) {
|
||||||
msgs := make([]rawMessage, n)
|
msgs := make([]rawMessage, n)
|
||||||
buffers := make([][]byte, n)
|
buffers := make([][]byte, n)
|
||||||
names := make([][]byte, n)
|
names := make([][]byte, n)
|
||||||
|
|
||||||
|
var cmsgs []byte
|
||||||
|
if cmsgSpace > 0 {
|
||||||
|
cmsgs = make([]byte, n*cmsgSpace)
|
||||||
|
}
|
||||||
|
|
||||||
for i := range msgs {
|
for i := range msgs {
|
||||||
buffers[i] = make([]byte, MTU)
|
buffers[i] = make([]byte, bufSize)
|
||||||
names[i] = make([]byte, unix.SizeofSockaddrInet6)
|
names[i] = make([]byte, unix.SizeofSockaddrInet6)
|
||||||
|
|
||||||
vs := []iovec{
|
vs := []iovec{
|
||||||
@@ -48,7 +53,28 @@ func (u *StdConn) PrepareRawMessages(n int) ([]rawMessage, [][]byte, [][]byte) {
|
|||||||
|
|
||||||
msgs[i].Hdr.Name = &names[i][0]
|
msgs[i].Hdr.Name = &names[i][0]
|
||||||
msgs[i].Hdr.Namelen = uint32(len(names[i]))
|
msgs[i].Hdr.Namelen = uint32(len(names[i]))
|
||||||
|
|
||||||
|
if cmsgSpace > 0 {
|
||||||
|
msgs[i].Hdr.Control = &cmsgs[i*cmsgSpace]
|
||||||
|
msgs[i].Hdr.Controllen = uint32(cmsgSpace)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return msgs, buffers, names
|
return msgs, buffers, names, cmsgs
|
||||||
|
}
|
||||||
|
|
||||||
|
func setIovLen(v *iovec, n int) {
|
||||||
|
v.Len = uint32(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setMsgIovlen(m *msghdr, n int) {
|
||||||
|
m.Iovlen = uint32(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setMsgControllen(m *msghdr, n int) {
|
||||||
|
m.Controllen = uint32(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setCmsgLen(h *unix.Cmsghdr, n int) {
|
||||||
|
h.Len = uint32(n)
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-3
@@ -33,13 +33,18 @@ type rawMessage struct {
|
|||||||
Pad0 [4]byte
|
Pad0 [4]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *StdConn) PrepareRawMessages(n int) ([]rawMessage, [][]byte, [][]byte) {
|
func (u *StdConn) PrepareRawMessages(n, bufSize, cmsgSpace int) ([]rawMessage, [][]byte, [][]byte, []byte) {
|
||||||
msgs := make([]rawMessage, n)
|
msgs := make([]rawMessage, n)
|
||||||
buffers := make([][]byte, n)
|
buffers := make([][]byte, n)
|
||||||
names := make([][]byte, n)
|
names := make([][]byte, n)
|
||||||
|
|
||||||
|
var cmsgs []byte
|
||||||
|
if cmsgSpace > 0 {
|
||||||
|
cmsgs = make([]byte, n*cmsgSpace)
|
||||||
|
}
|
||||||
|
|
||||||
for i := range msgs {
|
for i := range msgs {
|
||||||
buffers[i] = make([]byte, MTU)
|
buffers[i] = make([]byte, bufSize)
|
||||||
names[i] = make([]byte, unix.SizeofSockaddrInet6)
|
names[i] = make([]byte, unix.SizeofSockaddrInet6)
|
||||||
|
|
||||||
vs := []iovec{
|
vs := []iovec{
|
||||||
@@ -51,7 +56,28 @@ func (u *StdConn) PrepareRawMessages(n int) ([]rawMessage, [][]byte, [][]byte) {
|
|||||||
|
|
||||||
msgs[i].Hdr.Name = &names[i][0]
|
msgs[i].Hdr.Name = &names[i][0]
|
||||||
msgs[i].Hdr.Namelen = uint32(len(names[i]))
|
msgs[i].Hdr.Namelen = uint32(len(names[i]))
|
||||||
|
|
||||||
|
if cmsgSpace > 0 {
|
||||||
|
msgs[i].Hdr.Control = &cmsgs[i*cmsgSpace]
|
||||||
|
msgs[i].Hdr.Controllen = uint64(cmsgSpace)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return msgs, buffers, names
|
return msgs, buffers, names, cmsgs
|
||||||
|
}
|
||||||
|
|
||||||
|
func setIovLen(v *iovec, n int) {
|
||||||
|
v.Len = uint64(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setMsgIovlen(m *msghdr, n int) {
|
||||||
|
m.Iovlen = uint64(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setMsgControllen(m *msghdr, n int) {
|
||||||
|
m.Controllen = uint64(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setCmsgLen(h *unix.Cmsghdr, n int) {
|
||||||
|
h.Len = uint64(n)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,18 +46,3 @@ func NewListenConfig(multi bool) net.ListenConfig {
|
|||||||
func (u *GenericConn) Rebind() error {
|
func (u *GenericConn) Rebind() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnablePathMTUDiscovery sets the don't-fragment bit on outbound packets.
|
|
||||||
// NetBSD exposes IPV6_DONTFRAG via golang.org/x/sys/unix but the kernel does
|
|
||||||
// not provide a socket-level knob for setting DF on v4 UDP. The only IP-layer
|
|
||||||
// constant exposed is IP_DF, which is the wire header flag, not a sockopt.
|
|
||||||
// quic-go skips NetBSD for the same reason. So v4 sockets stay at nebula's
|
|
||||||
// historical behavior (kernel may fragment); v6 gets DF.
|
|
||||||
func (u *GenericConn) EnablePathMTUDiscovery() error {
|
|
||||||
if u.isV4Socket() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return u.controlFD(func(fd uintptr) error {
|
|
||||||
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_DONTFRAG, 1)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
//go:build openbsd && !e2e_testing
|
|
||||||
// +build openbsd,!e2e_testing
|
|
||||||
|
|
||||||
package udp
|
|
||||||
|
|
||||||
import (
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
// EnablePathMTUDiscovery sets the don't-fragment bit on outbound packets.
|
|
||||||
// OpenBSD exposes IPV6_DONTFRAG via golang.org/x/sys/unix but the kernel does
|
|
||||||
// not provide a socket-level knob for setting DF on v4 UDP. The only IP-layer
|
|
||||||
// constant exposed is IP_DF, which is the wire header flag, not a sockopt.
|
|
||||||
// quic-go skips OpenBSD for the same reason. So v4 sockets stay at nebula's
|
|
||||||
// historical behavior (kernel may fragment); v6 gets DF.
|
|
||||||
func (u *GenericConn) EnablePathMTUDiscovery() error {
|
|
||||||
if u.isV4Socket() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return u.controlFD(func(fd uintptr) error {
|
|
||||||
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_DONTFRAG, 1)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
+11
-7
@@ -140,7 +140,7 @@ func (u *RIOConn) bind(l *slog.Logger, sa windows.Sockaddr) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *RIOConn) ListenOut(r EncReader) error {
|
func (u *RIOConn) ListenOut(r EncReader, flush func()) error {
|
||||||
buffer := make([]byte, MTU)
|
buffer := make([]byte, MTU)
|
||||||
|
|
||||||
var lastRecvErr time.Time
|
var lastRecvErr time.Time
|
||||||
@@ -162,6 +162,7 @@ func (u *RIOConn) ListenOut(r EncReader) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r(netip.AddrPortFrom(netip.AddrFrom16(rua.Addr).Unmap(), (rua.Port>>8)|((rua.Port&0xff)<<8)), buffer[:n])
|
r(netip.AddrPortFrom(netip.AddrFrom16(rua.Addr).Unmap(), (rua.Port>>8)|((rua.Port&0xff)<<8)), buffer[:n])
|
||||||
|
flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,6 +317,15 @@ func (u *RIOConn) WriteTo(buf []byte, ip netip.AddrPort) error {
|
|||||||
return winrio.SendEx(u.rq, dataBuffer, 1, nil, addressBuffer, nil, nil, 0, 0)
|
return winrio.SendEx(u.rq, dataBuffer, 1, nil, addressBuffer, nil, nil, 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *RIOConn) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) error {
|
||||||
|
for i, b := range bufs {
|
||||||
|
if err := u.WriteTo(b, addrs[i]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (u *RIOConn) LocalAddr() (netip.AddrPort, error) {
|
func (u *RIOConn) LocalAddr() (netip.AddrPort, error) {
|
||||||
sa, err := windows.Getsockname(u.sock)
|
sa, err := windows.Getsockname(u.sock)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -335,12 +345,6 @@ func (u *RIOConn) Rebind() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnablePathMTUDiscovery is a no-op on Windows for now. PMTUD is Linux-only in
|
|
||||||
// the initial PoC; Windows support would set IP_DONTFRAGMENT here.
|
|
||||||
func (u *RIOConn) EnablePathMTUDiscovery() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *RIOConn) ReloadConfig(*config.C) {}
|
func (u *RIOConn) ReloadConfig(*config.C) {}
|
||||||
|
|
||||||
func (u *RIOConn) Close() error {
|
func (u *RIOConn) Close() error {
|
||||||
|
|||||||
+11
-5
@@ -122,13 +122,23 @@ func (u *TesterConn) WriteTo(b []byte, addr netip.AddrPort) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *TesterConn) ListenOut(r EncReader) error {
|
func (u *TesterConn) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) error {
|
||||||
|
for i, b := range bufs {
|
||||||
|
if err := u.WriteTo(b, addrs[i]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *TesterConn) ListenOut(r EncReader, flush func()) error {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-u.done:
|
case <-u.done:
|
||||||
return os.ErrClosed
|
return os.ErrClosed
|
||||||
case p := <-u.RxPackets:
|
case p := <-u.RxPackets:
|
||||||
r(p.From, p.Data)
|
r(p.From, p.Data)
|
||||||
|
flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -152,10 +162,6 @@ func (u *TesterConn) Rebind() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *TesterConn) EnablePathMTUDiscovery() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *TesterConn) Close() error {
|
func (u *TesterConn) Close() error {
|
||||||
u.closeOnce.Do(func() {
|
u.closeOnce.Do(func() {
|
||||||
close(u.done)
|
close(u.done)
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"golang.org/x/sys/windows"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewListener(l *slog.Logger, ip netip.Addr, port int, multi bool, batch int) (Conn, error) {
|
func NewListener(l *slog.Logger, ip netip.Addr, port int, multi bool, batch int) (Conn, error) {
|
||||||
@@ -46,27 +44,3 @@ func NewListenConfig(multi bool) net.ListenConfig {
|
|||||||
func (u *GenericConn) Rebind() error {
|
func (u *GenericConn) Rebind() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Windows IP_DONTFRAGMENT and IPV6_DONTFRAG are not exposed in the
|
|
||||||
// golang.org/x/sys/windows package. Defined locally per the values in
|
|
||||||
// ws2ipdef.h / ws2tcpip.h. These are stable Win32 constants that have not
|
|
||||||
// changed since at least Windows Vista.
|
|
||||||
const (
|
|
||||||
winIPDontFragment = 14
|
|
||||||
winIPv6DontFrag = 14
|
|
||||||
)
|
|
||||||
|
|
||||||
// EnablePathMTUDiscovery sets the don't-fragment bit on outbound packets.
|
|
||||||
// Windows uses IP_DONTFRAGMENT (v4) and IPV6_DONTFRAG (v6) at IPPROTO_IP /
|
|
||||||
// IPPROTO_IPV6 respectively. Note: this only enables DF on the GenericConn
|
|
||||||
// fallback path. The RIO path (RIOConn) has its own EnablePathMTUDiscovery
|
|
||||||
// in udp_rio_windows.go and is currently a no-op pending RIO-specific work.
|
|
||||||
func (u *GenericConn) EnablePathMTUDiscovery() error {
|
|
||||||
v4 := u.isV4Socket()
|
|
||||||
return u.controlFD(func(fd uintptr) error {
|
|
||||||
if v4 {
|
|
||||||
return windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, winIPDontFragment, 1)
|
|
||||||
}
|
|
||||||
return windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IPV6, winIPv6DontFrag, 1)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user