2019-11-19 10:00:20 -07:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
2021-11-02 12:14:26 -06:00
|
|
|
"context"
|
2019-11-19 10:00:20 -07:00
|
|
|
"sync"
|
|
|
|
"time"
|
2019-12-12 09:34:17 -07:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
"github.com/rcrowley/go-metrics"
|
2019-12-12 09:34:17 -07:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/header"
|
2023-03-31 14:45:05 -06:00
|
|
|
"github.com/slackhq/nebula/udp"
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type connectionManager struct {
|
2023-03-31 14:45:05 -06:00
|
|
|
in map[uint32]struct{}
|
|
|
|
inLock *sync.RWMutex
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
out map[uint32]struct{}
|
|
|
|
outLock *sync.RWMutex
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
hostMap *HostMap
|
|
|
|
trafficTimer *LockingTimerWheel[uint32]
|
|
|
|
intf *Interface
|
|
|
|
pendingDeletion map[uint32]struct{}
|
|
|
|
punchy *Punchy
|
|
|
|
checkInterval time.Duration
|
|
|
|
pendingDeletionInterval time.Duration
|
|
|
|
metricsTxPunchy metrics.Counter
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
l *logrus.Logger
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
func newConnectionManager(ctx context.Context, l *logrus.Logger, intf *Interface, checkInterval, pendingDeletionInterval time.Duration, punchy *Punchy) *connectionManager {
|
|
|
|
var max time.Duration
|
|
|
|
if checkInterval < pendingDeletionInterval {
|
|
|
|
max = pendingDeletionInterval
|
|
|
|
} else {
|
|
|
|
max = checkInterval
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
nc := &connectionManager{
|
|
|
|
hostMap: intf.hostMap,
|
2023-02-13 13:41:05 -07:00
|
|
|
in: make(map[uint32]struct{}),
|
2019-11-19 10:00:20 -07:00
|
|
|
inLock: &sync.RWMutex{},
|
2023-02-13 13:41:05 -07:00
|
|
|
out: make(map[uint32]struct{}),
|
2019-11-19 10:00:20 -07:00
|
|
|
outLock: &sync.RWMutex{},
|
2023-03-31 14:45:05 -06:00
|
|
|
trafficTimer: NewLockingTimerWheel[uint32](time.Millisecond*500, max),
|
2019-11-19 10:00:20 -07:00
|
|
|
intf: intf,
|
2023-03-31 14:45:05 -06:00
|
|
|
pendingDeletion: make(map[uint32]struct{}),
|
2019-11-19 10:00:20 -07:00
|
|
|
checkInterval: checkInterval,
|
|
|
|
pendingDeletionInterval: pendingDeletionInterval,
|
2023-03-31 14:45:05 -06:00
|
|
|
punchy: punchy,
|
|
|
|
metricsTxPunchy: metrics.GetOrRegisterCounter("messages.tx.punchy", nil),
|
2021-03-26 08:46:30 -06:00
|
|
|
l: l,
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
2023-03-31 14:45:05 -06:00
|
|
|
|
2021-11-02 12:14:26 -06:00
|
|
|
nc.Start(ctx)
|
2019-11-19 10:00:20 -07:00
|
|
|
return nc
|
|
|
|
}
|
|
|
|
|
2023-02-13 13:41:05 -07:00
|
|
|
func (n *connectionManager) In(localIndex uint32) {
|
2019-11-19 10:00:20 -07:00
|
|
|
n.inLock.RLock()
|
|
|
|
// If this already exists, return
|
2023-02-13 13:41:05 -07:00
|
|
|
if _, ok := n.in[localIndex]; ok {
|
2019-11-19 10:00:20 -07:00
|
|
|
n.inLock.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n.inLock.RUnlock()
|
|
|
|
n.inLock.Lock()
|
2023-02-13 13:41:05 -07:00
|
|
|
n.in[localIndex] = struct{}{}
|
2019-11-19 10:00:20 -07:00
|
|
|
n.inLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-02-13 13:41:05 -07:00
|
|
|
func (n *connectionManager) Out(localIndex uint32) {
|
2019-11-19 10:00:20 -07:00
|
|
|
n.outLock.RLock()
|
|
|
|
// If this already exists, return
|
2023-02-13 13:41:05 -07:00
|
|
|
if _, ok := n.out[localIndex]; ok {
|
2019-11-19 10:00:20 -07:00
|
|
|
n.outLock.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n.outLock.RUnlock()
|
|
|
|
n.outLock.Lock()
|
2023-02-13 13:41:05 -07:00
|
|
|
n.out[localIndex] = struct{}{}
|
2019-11-19 10:00:20 -07:00
|
|
|
n.outLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
// getAndResetTrafficCheck returns if there was any inbound or outbound traffic within the last tick and
|
|
|
|
// resets the state for this local index
|
|
|
|
func (n *connectionManager) getAndResetTrafficCheck(localIndex uint32) (bool, bool) {
|
2019-11-19 10:00:20 -07:00
|
|
|
n.inLock.Lock()
|
|
|
|
n.outLock.Lock()
|
2023-03-31 14:45:05 -06:00
|
|
|
_, in := n.in[localIndex]
|
|
|
|
_, out := n.out[localIndex]
|
2023-02-13 13:41:05 -07:00
|
|
|
delete(n.in, localIndex)
|
|
|
|
delete(n.out, localIndex)
|
2019-11-19 10:00:20 -07:00
|
|
|
n.inLock.Unlock()
|
|
|
|
n.outLock.Unlock()
|
2023-03-31 14:45:05 -06:00
|
|
|
return in, out
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
func (n *connectionManager) AddTrafficWatch(localIndex uint32) {
|
|
|
|
n.Out(localIndex)
|
|
|
|
n.trafficTimer.Add(localIndex, n.checkInterval)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2021-11-02 12:14:26 -06:00
|
|
|
func (n *connectionManager) Start(ctx context.Context) {
|
|
|
|
go n.Run(ctx)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2021-11-02 12:14:26 -06:00
|
|
|
func (n *connectionManager) Run(ctx context.Context) {
|
2023-03-31 14:45:05 -06:00
|
|
|
//TODO: this tick should be based on the min wheel tick? Check firewall
|
2021-11-02 12:14:26 -06:00
|
|
|
clockSource := time.NewTicker(500 * time.Millisecond)
|
|
|
|
defer clockSource.Stop()
|
|
|
|
|
2020-11-19 16:44:05 -07:00
|
|
|
p := []byte("")
|
|
|
|
nb := make([]byte, 12, 12)
|
|
|
|
out := make([]byte, mtu)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-11-02 12:14:26 -06:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2023-03-31 14:45:05 -06:00
|
|
|
|
2021-11-02 12:14:26 -06:00
|
|
|
case now := <-clockSource.C:
|
2023-03-31 14:45:05 -06:00
|
|
|
n.trafficTimer.Advance(now)
|
|
|
|
for {
|
|
|
|
localIndex, has := n.trafficTimer.Purge()
|
|
|
|
if !has {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
n.doTrafficCheck(localIndex, p, nb, out, now)
|
|
|
|
}
|
2021-11-02 12:14:26 -06:00
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
func (n *connectionManager) doTrafficCheck(localIndex uint32, p, nb, out []byte, now time.Time) {
|
|
|
|
hostinfo, err := n.hostMap.QueryIndex(localIndex)
|
|
|
|
if err != nil {
|
|
|
|
n.l.WithField("localIndex", localIndex).Debugf("Not found in hostmap")
|
|
|
|
delete(n.pendingDeletion, localIndex)
|
|
|
|
return
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
if n.handleInvalidCertificate(now, hostinfo) {
|
|
|
|
return
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
primary, _ := n.hostMap.QueryVpnIp(hostinfo.vpnIp)
|
|
|
|
mainHostInfo := true
|
|
|
|
if primary != nil && primary != hostinfo {
|
|
|
|
mainHostInfo = false
|
|
|
|
}
|
2021-10-20 12:23:33 -06:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
// Check for traffic on this hostinfo
|
|
|
|
inTraffic, outTraffic := n.getAndResetTrafficCheck(localIndex)
|
2021-10-20 12:23:33 -06:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
// A hostinfo is determined alive if there is incoming traffic
|
|
|
|
if inTraffic {
|
|
|
|
if n.l.Level >= logrus.DebugLevel {
|
|
|
|
hostinfo.logger(n.l).
|
|
|
|
WithField("tunnelCheck", m{"state": "alive", "method": "passive"}).
|
|
|
|
Debug("Tunnel status")
|
2023-03-13 11:35:14 -06:00
|
|
|
}
|
2023-03-31 14:45:05 -06:00
|
|
|
delete(n.pendingDeletion, hostinfo.localIndexId)
|
2023-03-13 11:35:14 -06:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
if !mainHostInfo {
|
|
|
|
if hostinfo.vpnIp > n.intf.myVpnIp {
|
|
|
|
// We are receiving traffic on the non primary hostinfo and we really just want 1 tunnel. Make
|
|
|
|
// This the primary and prime the old primary hostinfo for testing
|
|
|
|
n.hostMap.MakePrimary(hostinfo)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
2023-03-29 14:09:36 -06:00
|
|
|
}
|
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
n.trafficTimer.Add(hostinfo.localIndexId, n.checkInterval)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
if !outTraffic {
|
|
|
|
// Send a punch packet to keep the NAT state alive
|
|
|
|
n.sendPunch(hostinfo)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
return
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
if _, ok := n.pendingDeletion[hostinfo.localIndexId]; ok {
|
|
|
|
// We have already sent a test packet and nothing was returned, this hostinfo is dead
|
|
|
|
hostinfo.logger(n.l).
|
|
|
|
WithField("tunnelCheck", m{"state": "dead", "method": "active"}).
|
|
|
|
Info("Tunnel status")
|
2021-10-20 12:23:33 -06:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
n.hostMap.DeleteHostInfo(hostinfo)
|
|
|
|
delete(n.pendingDeletion, hostinfo.localIndexId)
|
|
|
|
return
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
hostinfo.logger(n.l).
|
|
|
|
WithField("tunnelCheck", m{"state": "testing", "method": "active"}).
|
|
|
|
Debug("Tunnel status")
|
2023-03-30 10:09:20 -06:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
if hostinfo != nil && hostinfo.ConnectionState != nil && mainHostInfo {
|
2023-04-04 12:42:24 -06:00
|
|
|
if !outTraffic {
|
|
|
|
// If we aren't sending or receiving traffic then its an unused tunnel and we don't to test the tunnel.
|
|
|
|
// Just maintain NAT state if configured to do so.
|
|
|
|
n.sendPunch(hostinfo)
|
|
|
|
n.trafficTimer.Add(hostinfo.localIndexId, n.checkInterval)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
if n.punchy.GetTargetEverything() {
|
2023-04-04 12:42:24 -06:00
|
|
|
// This is similar to the old punchy behavior with a slight optimization.
|
|
|
|
// We aren't receiving traffic but we are sending it, punch on all known
|
|
|
|
// ips in case we need to re-prime NAT state
|
2023-03-31 14:45:05 -06:00
|
|
|
n.sendPunch(hostinfo)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-04-04 12:42:24 -06:00
|
|
|
if n.intf.lightHouse.IsLighthouseIP(hostinfo.vpnIp) {
|
|
|
|
// We are sending traffic to the lighthouse, let recv_error sort out any issues instead of testing the tunnel
|
|
|
|
n.trafficTimer.Add(hostinfo.localIndexId, n.checkInterval)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
// Send a test packet to trigger an authenticated tunnel test, this should suss out any lingering tunnel issues
|
|
|
|
n.intf.sendMessageToVpnIp(header.Test, header.TestRequest, hostinfo, p, nb, out)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
} else {
|
|
|
|
hostinfo.logger(n.l).Debugf("Hostinfo sadness")
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
2023-03-31 14:45:05 -06:00
|
|
|
|
|
|
|
n.pendingDeletion[hostinfo.localIndexId] = struct{}{}
|
|
|
|
n.trafficTimer.Add(hostinfo.localIndexId, n.pendingDeletionInterval)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
2021-10-20 12:23:33 -06:00
|
|
|
|
|
|
|
// handleInvalidCertificates will destroy a tunnel if pki.disconnect_invalid is true and the certificate is no longer valid
|
2023-02-13 13:41:05 -07:00
|
|
|
func (n *connectionManager) handleInvalidCertificate(now time.Time, hostinfo *HostInfo) bool {
|
2021-10-20 12:23:33 -06:00
|
|
|
if !n.intf.disconnectInvalid {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteCert := hostinfo.GetCert()
|
|
|
|
if remoteCert == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
valid, err := remoteCert.Verify(now, n.intf.caPool)
|
|
|
|
if valid {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fingerprint, _ := remoteCert.Sha256Sum()
|
2023-02-13 13:41:05 -07:00
|
|
|
hostinfo.logger(n.l).WithError(err).
|
2021-10-20 12:23:33 -06:00
|
|
|
WithField("fingerprint", fingerprint).
|
|
|
|
Info("Remote certificate is no longer valid, tearing down the tunnel")
|
|
|
|
|
|
|
|
// Inform the remote and close the tunnel locally
|
|
|
|
n.intf.sendCloseTunnel(hostinfo)
|
2022-06-21 12:35:23 -06:00
|
|
|
n.intf.closeTunnel(hostinfo)
|
2023-03-31 14:45:05 -06:00
|
|
|
delete(n.pendingDeletion, hostinfo.localIndexId)
|
2021-10-20 12:23:33 -06:00
|
|
|
return true
|
|
|
|
}
|
2023-03-31 14:45:05 -06:00
|
|
|
|
|
|
|
func (n *connectionManager) sendPunch(hostinfo *HostInfo) {
|
|
|
|
if !n.punchy.GetPunch() {
|
|
|
|
// Punching is disabled
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.punchy.GetTargetEverything() {
|
|
|
|
hostinfo.remotes.ForEach(n.hostMap.preferredRanges, func(addr *udp.Addr, preferred bool) {
|
|
|
|
n.metricsTxPunchy.Inc(1)
|
|
|
|
n.intf.outside.WriteTo([]byte{1}, addr)
|
|
|
|
})
|
|
|
|
|
|
|
|
} else if hostinfo.remote != nil {
|
|
|
|
n.metricsTxPunchy.Inc(1)
|
|
|
|
n.intf.outside.WriteTo([]byte{1}, hostinfo.remote)
|
|
|
|
}
|
|
|
|
}
|