2019-11-19 18:00:20 +01:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
2021-11-02 19:14:26 +01:00
|
|
|
"context"
|
2019-11-19 18:00:20 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
2019-12-12 17:34:17 +01:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-04 02:54:04 +01:00
|
|
|
"github.com/slackhq/nebula/header"
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
2019-11-19 18:00:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: incount and outcount are intended as a shortcut to locking the mutexes for every single packet
|
|
|
|
// and something like every 10 packets we could lock, send 10, then unlock for a moment
|
|
|
|
|
|
|
|
type connectionManager struct {
|
|
|
|
hostMap *HostMap
|
2021-11-04 02:54:04 +01:00
|
|
|
in map[iputil.VpnIp]struct{}
|
2019-11-19 18:00:20 +01:00
|
|
|
inLock *sync.RWMutex
|
|
|
|
inCount int
|
2021-11-04 02:54:04 +01:00
|
|
|
out map[iputil.VpnIp]struct{}
|
2019-11-19 18:00:20 +01:00
|
|
|
outLock *sync.RWMutex
|
|
|
|
outCount int
|
|
|
|
TrafficTimer *SystemTimerWheel
|
|
|
|
intf *Interface
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
pendingDeletion map[iputil.VpnIp]int
|
2019-11-19 18:00:20 +01:00
|
|
|
pendingDeletionLock *sync.RWMutex
|
|
|
|
pendingDeletionTimer *SystemTimerWheel
|
|
|
|
|
|
|
|
checkInterval int
|
|
|
|
pendingDeletionInterval int
|
|
|
|
|
2021-03-26 15:46:30 +01:00
|
|
|
l *logrus.Logger
|
2019-11-19 18:00:20 +01:00
|
|
|
// I wanted to call one matLock
|
|
|
|
}
|
|
|
|
|
2021-11-02 19:14:26 +01:00
|
|
|
func newConnectionManager(ctx context.Context, l *logrus.Logger, intf *Interface, checkInterval, pendingDeletionInterval int) *connectionManager {
|
2019-11-19 18:00:20 +01:00
|
|
|
nc := &connectionManager{
|
|
|
|
hostMap: intf.hostMap,
|
2021-11-04 02:54:04 +01:00
|
|
|
in: make(map[iputil.VpnIp]struct{}),
|
2019-11-19 18:00:20 +01:00
|
|
|
inLock: &sync.RWMutex{},
|
|
|
|
inCount: 0,
|
2021-11-04 02:54:04 +01:00
|
|
|
out: make(map[iputil.VpnIp]struct{}),
|
2019-11-19 18:00:20 +01:00
|
|
|
outLock: &sync.RWMutex{},
|
|
|
|
outCount: 0,
|
|
|
|
TrafficTimer: NewSystemTimerWheel(time.Millisecond*500, time.Second*60),
|
|
|
|
intf: intf,
|
2021-11-04 02:54:04 +01:00
|
|
|
pendingDeletion: make(map[iputil.VpnIp]int),
|
2019-11-19 18:00:20 +01:00
|
|
|
pendingDeletionLock: &sync.RWMutex{},
|
|
|
|
pendingDeletionTimer: NewSystemTimerWheel(time.Millisecond*500, time.Second*60),
|
|
|
|
checkInterval: checkInterval,
|
|
|
|
pendingDeletionInterval: pendingDeletionInterval,
|
2021-03-26 15:46:30 +01:00
|
|
|
l: l,
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
2021-11-02 19:14:26 +01:00
|
|
|
nc.Start(ctx)
|
2019-11-19 18:00:20 +01:00
|
|
|
return nc
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
func (n *connectionManager) In(ip iputil.VpnIp) {
|
2019-11-19 18:00:20 +01:00
|
|
|
n.inLock.RLock()
|
|
|
|
// If this already exists, return
|
|
|
|
if _, ok := n.in[ip]; ok {
|
|
|
|
n.inLock.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n.inLock.RUnlock()
|
|
|
|
n.inLock.Lock()
|
|
|
|
n.in[ip] = struct{}{}
|
|
|
|
n.inLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
func (n *connectionManager) Out(ip iputil.VpnIp) {
|
2019-11-19 18:00:20 +01:00
|
|
|
n.outLock.RLock()
|
|
|
|
// If this already exists, return
|
|
|
|
if _, ok := n.out[ip]; ok {
|
|
|
|
n.outLock.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n.outLock.RUnlock()
|
|
|
|
n.outLock.Lock()
|
|
|
|
// double check since we dropped the lock temporarily
|
|
|
|
if _, ok := n.out[ip]; ok {
|
|
|
|
n.outLock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n.out[ip] = struct{}{}
|
|
|
|
n.AddTrafficWatch(ip, n.checkInterval)
|
|
|
|
n.outLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
func (n *connectionManager) CheckIn(vpnIp iputil.VpnIp) bool {
|
2019-11-19 18:00:20 +01:00
|
|
|
n.inLock.RLock()
|
2021-11-04 02:54:04 +01:00
|
|
|
if _, ok := n.in[vpnIp]; ok {
|
2019-11-19 18:00:20 +01:00
|
|
|
n.inLock.RUnlock()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
n.inLock.RUnlock()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
func (n *connectionManager) ClearIP(ip iputil.VpnIp) {
|
2019-11-19 18:00:20 +01:00
|
|
|
n.inLock.Lock()
|
|
|
|
n.outLock.Lock()
|
|
|
|
delete(n.in, ip)
|
|
|
|
delete(n.out, ip)
|
|
|
|
n.inLock.Unlock()
|
|
|
|
n.outLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
func (n *connectionManager) ClearPendingDeletion(ip iputil.VpnIp) {
|
2019-11-19 18:00:20 +01:00
|
|
|
n.pendingDeletionLock.Lock()
|
|
|
|
delete(n.pendingDeletion, ip)
|
|
|
|
n.pendingDeletionLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
func (n *connectionManager) AddPendingDeletion(ip iputil.VpnIp) {
|
2019-11-19 18:00:20 +01:00
|
|
|
n.pendingDeletionLock.Lock()
|
|
|
|
if _, ok := n.pendingDeletion[ip]; ok {
|
|
|
|
n.pendingDeletion[ip] += 1
|
|
|
|
} else {
|
|
|
|
n.pendingDeletion[ip] = 0
|
|
|
|
}
|
|
|
|
n.pendingDeletionTimer.Add(ip, time.Second*time.Duration(n.pendingDeletionInterval))
|
|
|
|
n.pendingDeletionLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
func (n *connectionManager) checkPendingDeletion(ip iputil.VpnIp) bool {
|
2019-11-19 18:00:20 +01:00
|
|
|
n.pendingDeletionLock.RLock()
|
|
|
|
if _, ok := n.pendingDeletion[ip]; ok {
|
|
|
|
|
|
|
|
n.pendingDeletionLock.RUnlock()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
n.pendingDeletionLock.RUnlock()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
func (n *connectionManager) AddTrafficWatch(vpnIp iputil.VpnIp, seconds int) {
|
|
|
|
n.TrafficTimer.Add(vpnIp, time.Second*time.Duration(seconds))
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
|
|
|
|
2021-11-02 19:14:26 +01:00
|
|
|
func (n *connectionManager) Start(ctx context.Context) {
|
|
|
|
go n.Run(ctx)
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
|
|
|
|
2021-11-02 19:14:26 +01:00
|
|
|
func (n *connectionManager) Run(ctx context.Context) {
|
|
|
|
clockSource := time.NewTicker(500 * time.Millisecond)
|
|
|
|
defer clockSource.Stop()
|
|
|
|
|
2020-11-20 00:44:05 +01:00
|
|
|
p := []byte("")
|
|
|
|
nb := make([]byte, 12, 12)
|
|
|
|
out := make([]byte, mtu)
|
2019-11-19 18:00:20 +01:00
|
|
|
|
2021-11-02 19:14:26 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case now := <-clockSource.C:
|
|
|
|
n.HandleMonitorTick(now, p, nb, out)
|
|
|
|
n.HandleDeletionTick(now)
|
|
|
|
}
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 00:44:05 +01:00
|
|
|
func (n *connectionManager) HandleMonitorTick(now time.Time, p, nb, out []byte) {
|
2019-11-19 18:00:20 +01:00
|
|
|
n.TrafficTimer.advance(now)
|
|
|
|
for {
|
|
|
|
ep := n.TrafficTimer.Purge()
|
|
|
|
if ep == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
vpnIp := ep.(iputil.VpnIp)
|
2019-11-19 18:00:20 +01:00
|
|
|
|
|
|
|
// Check for traffic coming back in from this host.
|
2021-11-04 02:54:04 +01:00
|
|
|
traf := n.CheckIn(vpnIp)
|
2019-11-19 18:00:20 +01:00
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
hostinfo, err := n.hostMap.QueryVpnIp(vpnIp)
|
2021-10-20 20:23:33 +02:00
|
|
|
if err != nil {
|
2021-11-04 02:54:04 +01:00
|
|
|
n.l.Debugf("Not found in hostmap: %s", vpnIp)
|
2021-10-20 20:23:33 +02:00
|
|
|
|
|
|
|
if !n.intf.disconnectInvalid {
|
2021-11-04 02:54:04 +01:00
|
|
|
n.ClearIP(vpnIp)
|
|
|
|
n.ClearPendingDeletion(vpnIp)
|
2021-10-20 20:23:33 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
if n.handleInvalidCertificate(now, vpnIp, hostinfo) {
|
2021-10-20 20:23:33 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we saw an incoming packets from this ip and peer's certificate is not
|
|
|
|
// expired, just ignore.
|
2019-11-19 18:00:20 +01:00
|
|
|
if traf {
|
2021-03-26 15:46:30 +01:00
|
|
|
if n.l.Level >= logrus.DebugLevel {
|
2021-11-04 02:54:04 +01:00
|
|
|
n.l.WithField("vpnIp", vpnIp).
|
2019-11-19 18:00:20 +01:00
|
|
|
WithField("tunnelCheck", m{"state": "alive", "method": "passive"}).
|
|
|
|
Debug("Tunnel status")
|
|
|
|
}
|
2021-11-04 02:54:04 +01:00
|
|
|
n.ClearIP(vpnIp)
|
|
|
|
n.ClearPendingDeletion(vpnIp)
|
2019-11-19 18:00:20 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-26 15:46:30 +01:00
|
|
|
hostinfo.logger(n.l).
|
2019-11-19 18:00:20 +01:00
|
|
|
WithField("tunnelCheck", m{"state": "testing", "method": "active"}).
|
|
|
|
Debug("Tunnel status")
|
|
|
|
|
|
|
|
if hostinfo != nil && hostinfo.ConnectionState != nil {
|
|
|
|
// Send a test packet to trigger an authenticated tunnel test, this should suss out any lingering tunnel issues
|
2021-11-04 02:54:04 +01:00
|
|
|
n.intf.SendMessageToVpnIp(header.Test, header.TestRequest, vpnIp, p, nb, out)
|
2019-11-19 18:00:20 +01:00
|
|
|
|
|
|
|
} else {
|
2021-11-04 02:54:04 +01:00
|
|
|
hostinfo.logger(n.l).Debugf("Hostinfo sadness: %s", vpnIp)
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
2021-11-04 02:54:04 +01:00
|
|
|
n.AddPendingDeletion(vpnIp)
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *connectionManager) HandleDeletionTick(now time.Time) {
|
|
|
|
n.pendingDeletionTimer.advance(now)
|
|
|
|
for {
|
|
|
|
ep := n.pendingDeletionTimer.Purge()
|
|
|
|
if ep == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
vpnIp := ep.(iputil.VpnIp)
|
2019-11-19 18:00:20 +01:00
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
hostinfo, err := n.hostMap.QueryVpnIp(vpnIp)
|
2021-10-20 20:23:33 +02:00
|
|
|
if err != nil {
|
2021-11-04 02:54:04 +01:00
|
|
|
n.l.Debugf("Not found in hostmap: %s", vpnIp)
|
2021-10-20 20:23:33 +02:00
|
|
|
|
|
|
|
if !n.intf.disconnectInvalid {
|
2021-11-04 02:54:04 +01:00
|
|
|
n.ClearIP(vpnIp)
|
|
|
|
n.ClearPendingDeletion(vpnIp)
|
2021-10-20 20:23:33 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
if n.handleInvalidCertificate(now, vpnIp, hostinfo) {
|
2021-10-20 20:23:33 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we saw an incoming packets from this ip and peer's certificate is not
|
|
|
|
// expired, just ignore.
|
2021-11-04 02:54:04 +01:00
|
|
|
traf := n.CheckIn(vpnIp)
|
2019-11-19 18:00:20 +01:00
|
|
|
if traf {
|
2021-11-04 02:54:04 +01:00
|
|
|
n.l.WithField("vpnIp", vpnIp).
|
2019-11-19 18:00:20 +01:00
|
|
|
WithField("tunnelCheck", m{"state": "alive", "method": "active"}).
|
|
|
|
Debug("Tunnel status")
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
n.ClearIP(vpnIp)
|
|
|
|
n.ClearPendingDeletion(vpnIp)
|
2019-11-19 18:00:20 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it comes around on deletion wheel and hasn't resolved itself, delete
|
2021-11-04 02:54:04 +01:00
|
|
|
if n.checkPendingDeletion(vpnIp) {
|
2019-11-19 18:00:20 +01:00
|
|
|
cn := ""
|
|
|
|
if hostinfo.ConnectionState != nil && hostinfo.ConnectionState.peerCert != nil {
|
|
|
|
cn = hostinfo.ConnectionState.peerCert.Details.Name
|
|
|
|
}
|
2021-03-26 15:46:30 +01:00
|
|
|
hostinfo.logger(n.l).
|
2019-11-19 18:00:20 +01:00
|
|
|
WithField("tunnelCheck", m{"state": "dead", "method": "active"}).
|
|
|
|
WithField("certName", cn).
|
|
|
|
Info("Tunnel status")
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
n.ClearIP(vpnIp)
|
|
|
|
n.ClearPendingDeletion(vpnIp)
|
2019-11-19 18:00:20 +01:00
|
|
|
// TODO: This is only here to let tests work. Should do proper mocking
|
|
|
|
if n.intf.lightHouse != nil {
|
2021-11-04 02:54:04 +01:00
|
|
|
n.intf.lightHouse.DeleteVpnIp(vpnIp)
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
2020-11-23 20:51:16 +01:00
|
|
|
n.hostMap.DeleteHostInfo(hostinfo)
|
2019-11-19 18:00:20 +01:00
|
|
|
} else {
|
2021-11-04 02:54:04 +01:00
|
|
|
n.ClearIP(vpnIp)
|
|
|
|
n.ClearPendingDeletion(vpnIp)
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-20 20:23:33 +02:00
|
|
|
|
|
|
|
// handleInvalidCertificates will destroy a tunnel if pki.disconnect_invalid is true and the certificate is no longer valid
|
2021-11-04 02:54:04 +01:00
|
|
|
func (n *connectionManager) handleInvalidCertificate(now time.Time, vpnIp iputil.VpnIp, hostinfo *HostInfo) bool {
|
2021-10-20 20:23:33 +02: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()
|
2021-11-04 02:54:04 +01:00
|
|
|
n.l.WithField("vpnIp", vpnIp).WithError(err).
|
2021-10-20 20:23:33 +02:00
|
|
|
WithField("certName", remoteCert.Details.Name).
|
|
|
|
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)
|
|
|
|
n.intf.closeTunnel(hostinfo, false)
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
n.ClearIP(vpnIp)
|
|
|
|
n.ClearPendingDeletion(vpnIp)
|
2021-10-20 20:23:33 +02:00
|
|
|
return true
|
|
|
|
}
|