2019-11-19 10:00:20 -07:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
2021-03-12 12:16:25 -07:00
|
|
|
"bytes"
|
2021-11-02 12:14:26 -06:00
|
|
|
"context"
|
2019-11-19 10:00:20 -07:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
2021-03-12 12:16:25 -07:00
|
|
|
"errors"
|
2019-11-19 10:00:20 -07:00
|
|
|
"net"
|
2023-07-24 11:37:52 -06:00
|
|
|
"sync"
|
2019-11-19 10:00:20 -07:00
|
|
|
"time"
|
|
|
|
|
2021-04-27 20:23:18 -06:00
|
|
|
"github.com/rcrowley/go-metrics"
|
2019-11-19 10:00:20 -07:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/header"
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
|
|
|
"github.com/slackhq/nebula/udp"
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-04-14 12:50:09 -06:00
|
|
|
DefaultHandshakeTryInterval = time.Millisecond * 100
|
|
|
|
DefaultHandshakeRetries = 10
|
2020-07-22 08:35:10 -06:00
|
|
|
DefaultHandshakeTriggerBuffer = 64
|
2022-06-21 12:35:23 -06:00
|
|
|
DefaultUseRelays = true
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
2020-02-21 14:25:11 -07:00
|
|
|
var (
|
|
|
|
defaultHandshakeConfig = HandshakeConfig{
|
2020-07-22 08:35:10 -06:00
|
|
|
tryInterval: DefaultHandshakeTryInterval,
|
|
|
|
retries: DefaultHandshakeRetries,
|
|
|
|
triggerBuffer: DefaultHandshakeTriggerBuffer,
|
2022-06-21 12:35:23 -06:00
|
|
|
useRelays: DefaultUseRelays,
|
2020-02-21 14:25:11 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type HandshakeConfig struct {
|
2020-07-22 08:35:10 -06:00
|
|
|
tryInterval time.Duration
|
|
|
|
retries int
|
|
|
|
triggerBuffer int
|
2022-06-21 12:35:23 -06:00
|
|
|
useRelays bool
|
2020-06-26 11:45:48 -06:00
|
|
|
|
|
|
|
messageMetrics *MessageMetrics
|
2020-02-21 14:25:11 -07:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
type HandshakeManager struct {
|
2023-07-24 11:37:52 -06:00
|
|
|
// Mutex for interacting with the vpnIps and indexes maps
|
|
|
|
sync.RWMutex
|
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
vpnIps map[iputil.VpnIp]*HandshakeHostInfo
|
|
|
|
indexes map[uint32]*HandshakeHostInfo
|
2023-07-24 11:37:52 -06:00
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
mainHostMap *HostMap
|
|
|
|
lightHouse *LightHouse
|
2023-06-14 09:48:52 -06:00
|
|
|
outside udp.Conn
|
2021-04-14 12:50:09 -06:00
|
|
|
config HandshakeConfig
|
2023-01-18 09:56:42 -07:00
|
|
|
OutboundHandshakeTimer *LockingTimerWheel[iputil.VpnIp]
|
2021-04-14 12:50:09 -06:00
|
|
|
messageMetrics *MessageMetrics
|
2021-04-27 20:23:18 -06:00
|
|
|
metricInitiated metrics.Counter
|
|
|
|
metricTimedOut metrics.Counter
|
2023-08-21 17:51:45 -06:00
|
|
|
f *Interface
|
2021-04-14 12:50:09 -06:00
|
|
|
l *logrus.Logger
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
// can be used to trigger outbound handshake for the given vpnIp
|
|
|
|
trigger chan iputil.VpnIp
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
type HandshakeHostInfo struct {
|
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
startTime time.Time // Time that we first started trying with this handshake
|
|
|
|
ready bool // Is the handshake ready
|
|
|
|
counter int // How many attempts have we made so far
|
|
|
|
lastRemotes []*udp.Addr // Remotes that we sent to during the previous attempt
|
|
|
|
packetStore []*cachedPacket // A set of packets to be transmitted once the handshake completes
|
|
|
|
|
|
|
|
hostinfo *HostInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hh *HandshakeHostInfo) cachePacket(l *logrus.Logger, t header.MessageType, st header.MessageSubType, packet []byte, f packetCallback, m *cachedPacketMetrics) {
|
|
|
|
if len(hh.packetStore) < 100 {
|
|
|
|
tempPacket := make([]byte, len(packet))
|
|
|
|
copy(tempPacket, packet)
|
|
|
|
|
|
|
|
hh.packetStore = append(hh.packetStore, &cachedPacket{t, st, f, tempPacket})
|
|
|
|
if l.Level >= logrus.DebugLevel {
|
|
|
|
hh.hostinfo.logger(l).
|
|
|
|
WithField("length", len(hh.packetStore)).
|
|
|
|
WithField("stored", true).
|
|
|
|
Debugf("Packet store")
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
m.dropped.Inc(1)
|
|
|
|
|
|
|
|
if l.Level >= logrus.DebugLevel {
|
|
|
|
hh.hostinfo.logger(l).
|
|
|
|
WithField("length", len(hh.packetStore)).
|
|
|
|
WithField("stored", false).
|
|
|
|
Debugf("Packet store")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:51:45 -06:00
|
|
|
func NewHandshakeManager(l *logrus.Logger, mainHostMap *HostMap, lightHouse *LightHouse, outside udp.Conn, config HandshakeConfig) *HandshakeManager {
|
2019-11-19 10:00:20 -07:00
|
|
|
return &HandshakeManager{
|
2023-11-02 15:53:59 -06:00
|
|
|
vpnIps: map[iputil.VpnIp]*HandshakeHostInfo{},
|
|
|
|
indexes: map[uint32]*HandshakeHostInfo{},
|
2021-04-14 12:50:09 -06:00
|
|
|
mainHostMap: mainHostMap,
|
|
|
|
lightHouse: lightHouse,
|
|
|
|
outside: outside,
|
|
|
|
config: config,
|
2021-11-03 19:54:04 -06:00
|
|
|
trigger: make(chan iputil.VpnIp, config.triggerBuffer),
|
2023-01-18 09:56:42 -07:00
|
|
|
OutboundHandshakeTimer: NewLockingTimerWheel[iputil.VpnIp](config.tryInterval, hsTimeout(config.retries, config.tryInterval)),
|
2021-04-14 12:50:09 -06:00
|
|
|
messageMetrics: config.messageMetrics,
|
2021-04-27 20:23:18 -06:00
|
|
|
metricInitiated: metrics.GetOrRegisterCounter("handshake_manager.initiated", nil),
|
|
|
|
metricTimedOut: metrics.GetOrRegisterCounter("handshake_manager.timed_out", nil),
|
2021-04-14 12:50:09 -06:00
|
|
|
l: l,
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:51:45 -06:00
|
|
|
func (c *HandshakeManager) Run(ctx context.Context) {
|
2021-11-02 12:14:26 -06:00
|
|
|
clockSource := time.NewTicker(c.config.tryInterval)
|
|
|
|
defer clockSource.Stop()
|
|
|
|
|
2020-07-22 08:35:10 -06:00
|
|
|
for {
|
|
|
|
select {
|
2021-11-02 12:14:26 -06:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2020-07-22 08:35:10 -06:00
|
|
|
case vpnIP := <-c.trigger:
|
2023-08-21 17:51:45 -06:00
|
|
|
c.handleOutbound(vpnIP, true)
|
2021-11-02 12:14:26 -06:00
|
|
|
case now := <-clockSource.C:
|
2023-08-21 17:51:45 -06:00
|
|
|
c.NextOutboundHandshakeTimerTick(now)
|
2020-07-22 08:35:10 -06:00
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
func (hm *HandshakeManager) HandleIncoming(addr *udp.Addr, via *ViaSender, packet []byte, h *header.H) {
|
|
|
|
// First remote allow list check before we know the vpnIp
|
|
|
|
if addr != nil {
|
|
|
|
if !hm.lightHouse.GetRemoteAllowList().AllowUnknownVpnIp(addr.IP) {
|
|
|
|
hm.l.WithField("udpAddr", addr).Debug("lighthouse.remote_allow_list denied incoming handshake")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch h.Subtype {
|
|
|
|
case header.HandshakeIXPSK0:
|
|
|
|
switch h.MessageCounter {
|
|
|
|
case 1:
|
|
|
|
ixHandshakeStage1(hm.f, addr, via, packet, h)
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
newHostinfo := hm.queryIndex(h.RemoteIndex)
|
|
|
|
tearDown := ixHandshakeStage2(hm.f, addr, via, newHostinfo, packet, h)
|
|
|
|
if tearDown && newHostinfo != nil {
|
|
|
|
hm.DeleteHostInfo(newHostinfo.hostinfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:51:45 -06:00
|
|
|
func (c *HandshakeManager) NextOutboundHandshakeTimerTick(now time.Time) {
|
2023-01-18 09:56:42 -07:00
|
|
|
c.OutboundHandshakeTimer.Advance(now)
|
2019-11-19 10:00:20 -07:00
|
|
|
for {
|
2023-01-18 09:56:42 -07:00
|
|
|
vpnIp, has := c.OutboundHandshakeTimer.Purge()
|
|
|
|
if !has {
|
2019-11-19 10:00:20 -07:00
|
|
|
break
|
|
|
|
}
|
2023-08-21 17:51:45 -06:00
|
|
|
c.handleOutbound(vpnIp, false)
|
2020-07-22 08:35:10 -06:00
|
|
|
}
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
func (hm *HandshakeManager) handleOutbound(vpnIp iputil.VpnIp, lighthouseTriggered bool) {
|
|
|
|
hh := hm.queryVpnIp(vpnIp)
|
|
|
|
if hh == nil {
|
2021-04-14 12:50:09 -06:00
|
|
|
return
|
|
|
|
}
|
2023-11-02 15:53:59 -06:00
|
|
|
hh.Lock()
|
|
|
|
defer hh.Unlock()
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
hostinfo := hh.hostinfo
|
2021-04-14 12:50:09 -06:00
|
|
|
// If we are out of time, clean up
|
2023-11-02 15:53:59 -06:00
|
|
|
if hh.counter >= hm.config.retries {
|
2024-04-03 21:14:51 -06:00
|
|
|
hh.hostinfo.logger(hm.l).WithField("udpAddrs", hh.hostinfo.remotes.CopyAddrs(hm.mainHostMap.GetPreferredRanges())).
|
2023-11-02 15:53:59 -06:00
|
|
|
WithField("initiatorIndex", hh.hostinfo.localIndexId).
|
|
|
|
WithField("remoteIndex", hh.hostinfo.remoteIndexId).
|
2021-04-14 12:50:09 -06:00
|
|
|
WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
|
2023-11-02 15:53:59 -06:00
|
|
|
WithField("durationNs", time.Since(hh.startTime).Nanoseconds()).
|
2021-04-14 12:50:09 -06:00
|
|
|
Info("Handshake timed out")
|
2023-11-02 15:53:59 -06:00
|
|
|
hm.metricTimedOut.Inc(1)
|
|
|
|
hm.DeleteHostInfo(hostinfo)
|
2021-04-14 12:50:09 -06:00
|
|
|
return
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-08-21 17:51:45 -06:00
|
|
|
// Increment the counter to increase our delay, linear backoff
|
2023-11-02 15:53:59 -06:00
|
|
|
hh.counter++
|
2023-08-21 17:51:45 -06:00
|
|
|
|
|
|
|
// Check if we have a handshake packet to transmit yet
|
2023-11-02 15:53:59 -06:00
|
|
|
if !hh.ready {
|
|
|
|
if !ixHandshakeStage0(hm.f, hh) {
|
|
|
|
hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval*time.Duration(hh.counter))
|
2023-08-21 17:51:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
// Get a remotes object if we don't already have one.
|
|
|
|
// This is mainly to protect us as this should never be the case
|
2022-12-19 10:28:27 -07:00
|
|
|
// NB ^ This comment doesn't jive. It's how the thing gets initialized.
|
2022-06-21 12:35:23 -06:00
|
|
|
// It's the common path. Should it update every time, in case a future LH query/queries give us more info?
|
2021-04-14 12:50:09 -06:00
|
|
|
if hostinfo.remotes == nil {
|
2023-11-02 15:53:59 -06:00
|
|
|
hostinfo.remotes = hm.lightHouse.QueryCache(vpnIp)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2024-04-03 21:14:51 -06:00
|
|
|
remotes := hostinfo.remotes.CopyAddrs(hm.mainHostMap.GetPreferredRanges())
|
2023-11-02 15:53:59 -06:00
|
|
|
remotesHaveChanged := !udp.AddrSlice(remotes).Equal(hh.lastRemotes)
|
2023-03-13 13:09:08 -06:00
|
|
|
|
|
|
|
// We only care about a lighthouse trigger if we have new remotes to send to.
|
|
|
|
// This is a very specific optimization for a fast lighthouse reply.
|
|
|
|
if lighthouseTriggered && !remotesHaveChanged {
|
|
|
|
// If we didn't return here a lighthouse could cause us to aggressively send handshakes
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
hh.lastRemotes = remotes
|
2023-03-13 13:09:08 -06:00
|
|
|
|
|
|
|
// TODO: this will generate a load of queries for hosts with only 1 ip
|
|
|
|
// (such as ones registered to the lighthouse with only a private IP)
|
|
|
|
// So we only do it one time after attempting 5 handshakes already.
|
2023-11-02 15:53:59 -06:00
|
|
|
if len(remotes) <= 1 && hh.counter == 5 {
|
2021-04-14 12:50:09 -06:00
|
|
|
// If we only have 1 remote it is highly likely our query raced with the other host registered within the lighthouse
|
2021-11-03 19:54:04 -06:00
|
|
|
// Our vpnIp here has a tunnel with a lighthouse but has yet to send a host update packet there so we only know about
|
2021-04-14 12:50:09 -06:00
|
|
|
// the learned public ip for them. Query again to short circuit the promotion counter
|
2023-12-19 10:58:31 -07:00
|
|
|
hm.lightHouse.QueryServer(vpnIp)
|
2021-04-14 12:50:09 -06:00
|
|
|
}
|
|
|
|
|
2023-03-13 11:35:14 -06:00
|
|
|
// Send the handshake to all known ips, stage 2 takes care of assigning the hostinfo.remote based on the first to reply
|
2021-11-03 19:54:04 -06:00
|
|
|
var sentTo []*udp.Addr
|
2024-04-03 21:14:51 -06:00
|
|
|
hostinfo.remotes.ForEach(hm.mainHostMap.GetPreferredRanges(), func(addr *udp.Addr, _ bool) {
|
2023-11-02 15:53:59 -06:00
|
|
|
hm.messageMetrics.Tx(header.Handshake, header.MessageSubType(hostinfo.HandshakePacket[0][1]), 1)
|
|
|
|
err := hm.outside.WriteTo(hostinfo.HandshakePacket[0], addr)
|
2021-04-14 12:50:09 -06:00
|
|
|
if err != nil {
|
2023-11-02 15:53:59 -06:00
|
|
|
hostinfo.logger(hm.l).WithField("udpAddr", addr).
|
2021-04-14 12:50:09 -06:00
|
|
|
WithField("initiatorIndex", hostinfo.localIndexId).
|
|
|
|
WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
|
|
|
|
WithError(err).Error("Failed to send handshake message")
|
|
|
|
|
|
|
|
} else {
|
|
|
|
sentTo = append(sentTo, addr)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
2021-04-14 12:50:09 -06:00
|
|
|
})
|
|
|
|
|
2023-03-13 13:09:08 -06:00
|
|
|
// Don't be too noisy or confusing if we fail to send a handshake - if we don't get through we'll eventually log a timeout,
|
|
|
|
// so only log when the list of remotes has changed
|
|
|
|
if remotesHaveChanged {
|
2023-11-02 15:53:59 -06:00
|
|
|
hostinfo.logger(hm.l).WithField("udpAddrs", sentTo).
|
2021-04-30 17:19:40 -06:00
|
|
|
WithField("initiatorIndex", hostinfo.localIndexId).
|
|
|
|
WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
|
|
|
|
Info("Handshake message sent")
|
2023-11-02 15:53:59 -06:00
|
|
|
} else if hm.l.IsLevelEnabled(logrus.DebugLevel) {
|
|
|
|
hostinfo.logger(hm.l).WithField("udpAddrs", sentTo).
|
2023-03-13 13:09:08 -06:00
|
|
|
WithField("initiatorIndex", hostinfo.localIndexId).
|
|
|
|
WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
|
|
|
|
Debug("Handshake message sent")
|
2021-04-30 17:19:40 -06:00
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
if hm.config.useRelays && len(hostinfo.remotes.relays) > 0 {
|
|
|
|
hostinfo.logger(hm.l).WithField("relays", hostinfo.remotes.relays).Info("Attempt to relay through hosts")
|
2022-06-21 12:35:23 -06:00
|
|
|
// Send a RelayRequest to all known Relay IP's
|
|
|
|
for _, relay := range hostinfo.remotes.relays {
|
|
|
|
// Don't relay to myself, and don't relay through the host I'm trying to connect to
|
2023-11-02 15:53:59 -06:00
|
|
|
if *relay == vpnIp || *relay == hm.lightHouse.myVpnIp {
|
2022-06-21 12:35:23 -06:00
|
|
|
continue
|
|
|
|
}
|
2023-11-02 15:53:59 -06:00
|
|
|
relayHostInfo := hm.mainHostMap.QueryVpnIp(*relay)
|
2023-07-24 11:37:52 -06:00
|
|
|
if relayHostInfo == nil || relayHostInfo.remote == nil {
|
2023-11-02 15:53:59 -06:00
|
|
|
hostinfo.logger(hm.l).WithField("relay", relay.String()).Info("Establish tunnel to relay target")
|
|
|
|
hm.f.Handshake(*relay)
|
2022-06-21 12:35:23 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check the relay HostInfo to see if we already established a relay through it
|
|
|
|
if existingRelay, ok := relayHostInfo.relayState.QueryRelayForByIp(vpnIp); ok {
|
|
|
|
switch existingRelay.State {
|
|
|
|
case Established:
|
2023-11-02 15:53:59 -06:00
|
|
|
hostinfo.logger(hm.l).WithField("relay", relay.String()).Info("Send handshake via relay")
|
|
|
|
hm.f.SendVia(relayHostInfo, existingRelay, hostinfo.HandshakePacket[0], make([]byte, 12), make([]byte, mtu), false)
|
2022-06-21 12:35:23 -06:00
|
|
|
case Requested:
|
2023-11-02 15:53:59 -06:00
|
|
|
hostinfo.logger(hm.l).WithField("relay", relay.String()).Info("Re-send CreateRelay request")
|
2022-06-21 12:35:23 -06:00
|
|
|
// Re-send the CreateRelay request, in case the previous one was lost.
|
|
|
|
m := NebulaControl{
|
|
|
|
Type: NebulaControl_CreateRelayRequest,
|
|
|
|
InitiatorRelayIndex: existingRelay.LocalIndex,
|
2023-11-02 15:53:59 -06:00
|
|
|
RelayFromIp: uint32(hm.lightHouse.myVpnIp),
|
2022-06-21 12:35:23 -06:00
|
|
|
RelayToIp: uint32(vpnIp),
|
|
|
|
}
|
|
|
|
msg, err := m.Marshal()
|
|
|
|
if err != nil {
|
2023-11-02 15:53:59 -06:00
|
|
|
hostinfo.logger(hm.l).
|
2022-06-21 12:35:23 -06:00
|
|
|
WithError(err).
|
|
|
|
Error("Failed to marshal Control message to create relay")
|
|
|
|
} else {
|
2023-05-04 14:16:37 -06:00
|
|
|
// This must send over the hostinfo, not over hm.Hosts[ip]
|
2023-11-02 15:53:59 -06:00
|
|
|
hm.f.SendMessageToHostInfo(header.Control, 0, relayHostInfo, msg, make([]byte, 12), make([]byte, mtu))
|
|
|
|
hm.l.WithFields(logrus.Fields{
|
|
|
|
"relayFrom": hm.lightHouse.myVpnIp,
|
2023-03-30 14:07:31 -06:00
|
|
|
"relayTo": vpnIp,
|
|
|
|
"initiatorRelayIndex": existingRelay.LocalIndex,
|
|
|
|
"relay": *relay}).
|
2023-03-30 10:09:20 -06:00
|
|
|
Info("send CreateRelayRequest")
|
2022-06-21 12:35:23 -06:00
|
|
|
}
|
|
|
|
default:
|
2023-11-02 15:53:59 -06:00
|
|
|
hostinfo.logger(hm.l).
|
2022-06-21 12:35:23 -06:00
|
|
|
WithField("vpnIp", vpnIp).
|
|
|
|
WithField("state", existingRelay.State).
|
2023-03-30 14:07:31 -06:00
|
|
|
WithField("relay", relayHostInfo.vpnIp).
|
2022-06-21 12:35:23 -06:00
|
|
|
Errorf("Relay unexpected state")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No relays exist or requested yet.
|
|
|
|
if relayHostInfo.remote != nil {
|
2023-11-02 15:53:59 -06:00
|
|
|
idx, err := AddRelay(hm.l, relayHostInfo, hm.mainHostMap, vpnIp, nil, TerminalType, Requested)
|
2022-06-21 12:35:23 -06:00
|
|
|
if err != nil {
|
2023-11-02 15:53:59 -06:00
|
|
|
hostinfo.logger(hm.l).WithField("relay", relay.String()).WithError(err).Info("Failed to add relay to hostmap")
|
2022-06-21 12:35:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
m := NebulaControl{
|
|
|
|
Type: NebulaControl_CreateRelayRequest,
|
|
|
|
InitiatorRelayIndex: idx,
|
2023-11-02 15:53:59 -06:00
|
|
|
RelayFromIp: uint32(hm.lightHouse.myVpnIp),
|
2022-06-21 12:35:23 -06:00
|
|
|
RelayToIp: uint32(vpnIp),
|
|
|
|
}
|
|
|
|
msg, err := m.Marshal()
|
|
|
|
if err != nil {
|
2023-11-02 15:53:59 -06:00
|
|
|
hostinfo.logger(hm.l).
|
2022-06-21 12:35:23 -06:00
|
|
|
WithError(err).
|
|
|
|
Error("Failed to marshal Control message to create relay")
|
|
|
|
} else {
|
2023-11-02 15:53:59 -06:00
|
|
|
hm.f.SendMessageToHostInfo(header.Control, 0, relayHostInfo, msg, make([]byte, 12), make([]byte, mtu))
|
|
|
|
hm.l.WithFields(logrus.Fields{
|
|
|
|
"relayFrom": hm.lightHouse.myVpnIp,
|
2023-03-30 14:07:31 -06:00
|
|
|
"relayTo": vpnIp,
|
|
|
|
"initiatorRelayIndex": idx,
|
|
|
|
"relay": *relay}).
|
2023-03-30 10:09:20 -06:00
|
|
|
Info("send CreateRelayRequest")
|
2022-06-21 12:35:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
// If a lighthouse triggered this attempt then we are still in the timer wheel and do not need to re-add
|
|
|
|
if !lighthouseTriggered {
|
2023-11-02 15:53:59 -06:00
|
|
|
hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval*time.Duration(hh.counter))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:51:45 -06:00
|
|
|
// GetOrHandshake will try to find a hostinfo with a fully formed tunnel or start a new handshake if one is not present
|
|
|
|
// The 2nd argument will be true if the hostinfo is ready to transmit traffic
|
2023-11-02 15:53:59 -06:00
|
|
|
func (hm *HandshakeManager) GetOrHandshake(vpnIp iputil.VpnIp, cacheCb func(*HandshakeHostInfo)) (*HostInfo, bool) {
|
2023-08-21 17:51:45 -06:00
|
|
|
hm.mainHostMap.RLock()
|
2024-05-29 10:52:52 -06:00
|
|
|
h, ok := hm.mainHostMap.Hosts[vpnIp]
|
|
|
|
hm.mainHostMap.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
2023-08-21 17:51:45 -06:00
|
|
|
// Do not attempt promotion if you are a lighthouse
|
|
|
|
if !hm.lightHouse.amLighthouse {
|
2024-04-03 21:14:51 -06:00
|
|
|
h.TryPromoteBest(hm.mainHostMap.GetPreferredRanges(), hm.f)
|
2023-08-21 17:51:45 -06:00
|
|
|
}
|
|
|
|
return h, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return hm.StartHandshake(vpnIp, cacheCb), false
|
|
|
|
}
|
2023-07-24 11:37:52 -06:00
|
|
|
|
2023-08-21 17:51:45 -06:00
|
|
|
// StartHandshake will ensure a handshake is currently being attempted for the provided vpn ip
|
2023-11-02 15:53:59 -06:00
|
|
|
func (hm *HandshakeManager) StartHandshake(vpnIp iputil.VpnIp, cacheCb func(*HandshakeHostInfo)) *HostInfo {
|
2023-08-21 17:51:45 -06:00
|
|
|
hm.Lock()
|
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
if hh, ok := hm.vpnIps[vpnIp]; ok {
|
2023-08-21 17:51:45 -06:00
|
|
|
// We are already trying to handshake with this vpn ip
|
|
|
|
if cacheCb != nil {
|
2023-11-02 15:53:59 -06:00
|
|
|
cacheCb(hh)
|
2023-08-21 17:51:45 -06:00
|
|
|
}
|
2024-04-11 15:00:01 -06:00
|
|
|
hm.Unlock()
|
2023-11-02 15:53:59 -06:00
|
|
|
return hh.hostinfo
|
2023-07-24 11:37:52 -06:00
|
|
|
}
|
2021-11-08 12:46:22 -07:00
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
hostinfo := &HostInfo{
|
|
|
|
vpnIp: vpnIp,
|
|
|
|
HandshakePacket: make(map[uint8][]byte, 0),
|
|
|
|
relayState: RelayState{
|
|
|
|
relays: map[iputil.VpnIp]struct{}{},
|
|
|
|
relayForByIp: map[iputil.VpnIp]*Relay{},
|
|
|
|
relayForByIdx: map[uint32]*Relay{},
|
|
|
|
},
|
2021-11-08 12:46:22 -07:00
|
|
|
}
|
2020-07-22 08:35:10 -06:00
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
hh := &HandshakeHostInfo{
|
|
|
|
hostinfo: hostinfo,
|
|
|
|
startTime: time.Now(),
|
|
|
|
}
|
|
|
|
hm.vpnIps[vpnIp] = hh
|
2023-08-21 17:51:45 -06:00
|
|
|
hm.metricInitiated.Inc(1)
|
|
|
|
hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval)
|
|
|
|
|
|
|
|
if cacheCb != nil {
|
2023-11-02 15:53:59 -06:00
|
|
|
cacheCb(hh)
|
2023-08-21 17:51:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a static host, we don't need to wait for the HostQueryReply
|
|
|
|
// We can trigger the handshake right now
|
|
|
|
_, doTrigger := hm.lightHouse.GetStaticHostList()[vpnIp]
|
|
|
|
if !doTrigger {
|
|
|
|
// Add any calculated remotes, and trigger early handshake if one found
|
|
|
|
doTrigger = hm.lightHouse.addCalculatedRemotes(vpnIp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if doTrigger {
|
|
|
|
select {
|
|
|
|
case hm.trigger <- vpnIp:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 11:37:52 -06:00
|
|
|
|
2024-04-11 15:00:01 -06:00
|
|
|
hm.Unlock()
|
2023-12-19 10:58:31 -07:00
|
|
|
hm.lightHouse.QueryServer(vpnIp)
|
2019-11-19 10:00:20 -07:00
|
|
|
return hostinfo
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:16:25 -07:00
|
|
|
var (
|
|
|
|
ErrExistingHostInfo = errors.New("existing hostinfo")
|
|
|
|
ErrAlreadySeen = errors.New("already seen")
|
|
|
|
ErrLocalIndexCollision = errors.New("local index collision")
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckAndComplete checks for any conflicts in the main and pending hostmap
|
|
|
|
// before adding hostinfo to main. If err is nil, it was added. Otherwise err will be:
|
2021-11-03 19:54:04 -06:00
|
|
|
//
|
2021-03-12 12:16:25 -07:00
|
|
|
// ErrAlreadySeen if we already have an entry in the hostmap that has seen the
|
|
|
|
// exact same handshake packet
|
|
|
|
//
|
|
|
|
// ErrExistingHostInfo if we already have an entry in the hostmap for this
|
2021-11-03 19:54:04 -06:00
|
|
|
// VpnIp and the new handshake was older than the one we currently have
|
2021-03-12 12:16:25 -07:00
|
|
|
//
|
|
|
|
// ErrLocalIndexCollision if we already have an entry in the main or pending
|
|
|
|
// hostmap for the hostinfo.localIndexId.
|
2023-03-13 11:35:14 -06:00
|
|
|
func (c *HandshakeManager) CheckAndComplete(hostinfo *HostInfo, handshakePacket uint8, f *Interface) (*HostInfo, error) {
|
2021-03-12 12:16:25 -07:00
|
|
|
c.mainHostMap.Lock()
|
|
|
|
defer c.mainHostMap.Unlock()
|
2023-08-21 17:51:45 -06:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2021-03-12 12:16:25 -07:00
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
// Check if we already have a tunnel with this vpn ip
|
2021-11-03 19:54:04 -06:00
|
|
|
existingHostInfo, found := c.mainHostMap.Hosts[hostinfo.vpnIp]
|
2021-03-12 12:16:25 -07:00
|
|
|
if found && existingHostInfo != nil {
|
2023-03-13 11:35:14 -06:00
|
|
|
testHostInfo := existingHostInfo
|
|
|
|
for testHostInfo != nil {
|
|
|
|
// Is it just a delayed handshake packet?
|
2023-05-04 14:16:37 -06:00
|
|
|
if bytes.Equal(hostinfo.HandshakePacket[handshakePacket], testHostInfo.HandshakePacket[handshakePacket]) {
|
|
|
|
return testHostInfo, ErrAlreadySeen
|
2023-03-13 11:35:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
testHostInfo = testHostInfo.next
|
2021-03-12 12:16:25 -07:00
|
|
|
}
|
2021-04-14 12:50:09 -06:00
|
|
|
|
2021-04-27 20:15:34 -06:00
|
|
|
// Is this a newer handshake?
|
2023-03-30 10:09:20 -06:00
|
|
|
if existingHostInfo.lastHandshakeTime >= hostinfo.lastHandshakeTime && !existingHostInfo.ConnectionState.initiator {
|
2021-03-12 12:16:25 -07:00
|
|
|
return existingHostInfo, ErrExistingHostInfo
|
|
|
|
}
|
2021-04-27 20:15:34 -06:00
|
|
|
|
|
|
|
existingHostInfo.logger(c.l).Info("Taking new handshake")
|
2021-03-12 12:16:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
existingIndex, found := c.mainHostMap.Indexes[hostinfo.localIndexId]
|
|
|
|
if found {
|
|
|
|
// We have a collision, but for a different hostinfo
|
|
|
|
return existingIndex, ErrLocalIndexCollision
|
|
|
|
}
|
2021-04-14 12:50:09 -06:00
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
existingPendingIndex, found := c.indexes[hostinfo.localIndexId]
|
|
|
|
if found && existingPendingIndex.hostinfo != hostinfo {
|
2021-03-12 12:16:25 -07:00
|
|
|
// We have a collision, but for a different hostinfo
|
|
|
|
return existingIndex, ErrLocalIndexCollision
|
|
|
|
}
|
|
|
|
|
|
|
|
existingRemoteIndex, found := c.mainHostMap.RemoteIndexes[hostinfo.remoteIndexId]
|
2021-11-03 19:54:04 -06:00
|
|
|
if found && existingRemoteIndex != nil && existingRemoteIndex.vpnIp != hostinfo.vpnIp {
|
2021-03-12 12:16:25 -07:00
|
|
|
// We have a collision, but this can happen since we can't control
|
|
|
|
// the remote ID. Just log about the situation as a note.
|
2021-03-26 08:46:30 -06:00
|
|
|
hostinfo.logger(c.l).
|
2021-11-03 19:54:04 -06:00
|
|
|
WithField("remoteIndex", hostinfo.remoteIndexId).WithField("collision", existingRemoteIndex.vpnIp).
|
2021-03-12 12:16:25 -07:00
|
|
|
Info("New host shadows existing host remoteIndex")
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
2021-03-12 12:16:25 -07:00
|
|
|
|
2023-03-13 11:35:14 -06:00
|
|
|
c.mainHostMap.unlockedAddHostInfo(hostinfo, f)
|
2021-03-12 12:16:25 -07:00
|
|
|
return existingHostInfo, nil
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2021-03-12 12:16:25 -07:00
|
|
|
// Complete is a simpler version of CheckAndComplete when we already know we
|
|
|
|
// won't have a localIndexId collision because we already have an entry in the
|
2023-03-13 11:35:14 -06:00
|
|
|
// pendingHostMap. An existing hostinfo is returned if there was one.
|
2023-08-21 17:51:45 -06:00
|
|
|
func (hm *HandshakeManager) Complete(hostinfo *HostInfo, f *Interface) {
|
|
|
|
hm.mainHostMap.Lock()
|
|
|
|
defer hm.mainHostMap.Unlock()
|
|
|
|
hm.Lock()
|
|
|
|
defer hm.Unlock()
|
2021-03-12 12:16:25 -07:00
|
|
|
|
2023-08-21 17:51:45 -06:00
|
|
|
existingRemoteIndex, found := hm.mainHostMap.RemoteIndexes[hostinfo.remoteIndexId]
|
2021-03-12 12:16:25 -07:00
|
|
|
if found && existingRemoteIndex != nil {
|
|
|
|
// We have a collision, but this can happen since we can't control
|
|
|
|
// the remote ID. Just log about the situation as a note.
|
2023-08-21 17:51:45 -06:00
|
|
|
hostinfo.logger(hm.l).
|
2021-11-03 19:54:04 -06:00
|
|
|
WithField("remoteIndex", hostinfo.remoteIndexId).WithField("collision", existingRemoteIndex.vpnIp).
|
2021-03-12 12:16:25 -07:00
|
|
|
Info("New host shadows existing host remoteIndex")
|
|
|
|
}
|
|
|
|
|
2023-03-17 14:36:24 -06:00
|
|
|
// We need to remove from the pending hostmap first to avoid undoing work when after to the main hostmap.
|
2023-08-21 17:51:45 -06:00
|
|
|
hm.unlockedDeleteHostInfo(hostinfo)
|
|
|
|
hm.mainHostMap.unlockedAddHostInfo(hostinfo, f)
|
2021-03-12 12:16:25 -07:00
|
|
|
}
|
|
|
|
|
2023-08-21 17:51:45 -06:00
|
|
|
// allocateIndex generates a unique localIndexId for this HostInfo
|
2021-03-12 12:16:25 -07:00
|
|
|
// and adds it to the pendingHostMap. Will error if we are unable to generate
|
|
|
|
// a unique localIndexId
|
2023-11-02 15:53:59 -06:00
|
|
|
func (hm *HandshakeManager) allocateIndex(hh *HandshakeHostInfo) error {
|
2023-08-21 17:51:45 -06:00
|
|
|
hm.mainHostMap.RLock()
|
|
|
|
defer hm.mainHostMap.RUnlock()
|
|
|
|
hm.Lock()
|
|
|
|
defer hm.Unlock()
|
2021-03-12 12:16:25 -07:00
|
|
|
|
|
|
|
for i := 0; i < 32; i++ {
|
2023-08-21 17:51:45 -06:00
|
|
|
index, err := generateIndex(hm.l)
|
2021-03-12 12:16:25 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:51:45 -06:00
|
|
|
_, inPending := hm.indexes[index]
|
|
|
|
_, inMain := hm.mainHostMap.Indexes[index]
|
2021-03-12 12:16:25 -07:00
|
|
|
|
|
|
|
if !inMain && !inPending {
|
2023-11-02 15:53:59 -06:00
|
|
|
hh.hostinfo.localIndexId = index
|
|
|
|
hm.indexes[index] = hh
|
2021-03-12 12:16:25 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("failed to generate unique localIndexId")
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
func (c *HandshakeManager) DeleteHostInfo(hostinfo *HostInfo) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
c.unlockedDeleteHostInfo(hostinfo)
|
2020-11-23 12:51:16 -07:00
|
|
|
}
|
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
func (c *HandshakeManager) unlockedDeleteHostInfo(hostinfo *HostInfo) {
|
|
|
|
delete(c.vpnIps, hostinfo.vpnIp)
|
|
|
|
if len(c.vpnIps) == 0 {
|
2023-11-02 15:53:59 -06:00
|
|
|
c.vpnIps = map[iputil.VpnIp]*HandshakeHostInfo{}
|
2023-07-24 11:37:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(c.indexes, hostinfo.localIndexId)
|
|
|
|
if len(c.vpnIps) == 0 {
|
2023-11-02 15:53:59 -06:00
|
|
|
c.indexes = map[uint32]*HandshakeHostInfo{}
|
2023-07-24 11:37:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.l.Level >= logrus.DebugLevel {
|
|
|
|
c.l.WithField("hostMap", m{"mapTotalSize": len(c.vpnIps),
|
|
|
|
"vpnIp": hostinfo.vpnIp, "indexNumber": hostinfo.localIndexId, "remoteIndexNumber": hostinfo.remoteIndexId}).
|
|
|
|
Debug("Pending hostmap hostInfo deleted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
func (hm *HandshakeManager) QueryVpnIp(vpnIp iputil.VpnIp) *HostInfo {
|
|
|
|
hh := hm.queryVpnIp(vpnIp)
|
|
|
|
if hh != nil {
|
|
|
|
return hh.hostinfo
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
}
|
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
func (hm *HandshakeManager) queryVpnIp(vpnIp iputil.VpnIp) *HandshakeHostInfo {
|
|
|
|
hm.RLock()
|
|
|
|
defer hm.RUnlock()
|
|
|
|
return hm.vpnIps[vpnIp]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hm *HandshakeManager) QueryIndex(index uint32) *HostInfo {
|
|
|
|
hh := hm.queryIndex(index)
|
|
|
|
if hh != nil {
|
|
|
|
return hh.hostinfo
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hm *HandshakeManager) queryIndex(index uint32) *HandshakeHostInfo {
|
|
|
|
hm.RLock()
|
|
|
|
defer hm.RUnlock()
|
|
|
|
return hm.indexes[index]
|
2023-07-24 11:37:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HandshakeManager) GetPreferredRanges() []*net.IPNet {
|
2024-04-03 21:14:51 -06:00
|
|
|
return c.mainHostMap.GetPreferredRanges()
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
func (c *HandshakeManager) ForEachVpnIp(f controlEach) {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
for _, v := range c.vpnIps {
|
2023-11-02 15:53:59 -06:00
|
|
|
f(v.hostinfo)
|
2023-07-24 11:37:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HandshakeManager) ForEachIndex(f controlEach) {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
for _, v := range c.indexes {
|
2023-11-02 15:53:59 -06:00
|
|
|
f(v.hostinfo)
|
2023-07-24 11:37:52 -06:00
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HandshakeManager) EmitStats() {
|
2023-07-24 11:37:52 -06:00
|
|
|
c.RLock()
|
|
|
|
hostLen := len(c.vpnIps)
|
|
|
|
indexLen := len(c.indexes)
|
|
|
|
c.RUnlock()
|
|
|
|
|
|
|
|
metrics.GetOrRegisterGauge("hostmap.pending.hosts", nil).Update(int64(hostLen))
|
|
|
|
metrics.GetOrRegisterGauge("hostmap.pending.indexes", nil).Update(int64(indexLen))
|
|
|
|
c.mainHostMap.EmitStats()
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Utility functions below
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
func generateIndex(l *logrus.Logger) (uint32, error) {
|
2019-11-19 10:00:20 -07:00
|
|
|
b := make([]byte, 4)
|
2020-11-23 12:51:16 -07:00
|
|
|
|
|
|
|
// Let zero mean we don't know the ID, so don't generate zero
|
|
|
|
var index uint32
|
|
|
|
for index == 0 {
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
l.Errorln(err)
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
index = binary.BigEndian.Uint32(b)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if l.Level >= logrus.DebugLevel {
|
|
|
|
l.WithField("index", index).
|
|
|
|
Debug("Generated index")
|
|
|
|
}
|
|
|
|
return index, nil
|
|
|
|
}
|
2021-04-14 12:50:09 -06:00
|
|
|
|
|
|
|
func hsTimeout(tries int, interval time.Duration) time.Duration {
|
|
|
|
return time.Duration(tries / 2 * ((2 * int(interval)) + (tries-1)*int(interval)))
|
|
|
|
}
|