|
@@ -16,21 +16,24 @@ const (
|
|
DefaultHandshakeTryInterval = time.Millisecond * 100
|
|
DefaultHandshakeTryInterval = time.Millisecond * 100
|
|
DefaultHandshakeRetries = 20
|
|
DefaultHandshakeRetries = 20
|
|
// DefaultHandshakeWaitRotation is the number of handshake attempts to do before starting to use other ips addresses
|
|
// DefaultHandshakeWaitRotation is the number of handshake attempts to do before starting to use other ips addresses
|
|
- DefaultHandshakeWaitRotation = 5
|
|
|
|
|
|
+ DefaultHandshakeWaitRotation = 5
|
|
|
|
+ DefaultHandshakeTriggerBuffer = 64
|
|
)
|
|
)
|
|
|
|
|
|
var (
|
|
var (
|
|
defaultHandshakeConfig = HandshakeConfig{
|
|
defaultHandshakeConfig = HandshakeConfig{
|
|
- tryInterval: DefaultHandshakeTryInterval,
|
|
|
|
- retries: DefaultHandshakeRetries,
|
|
|
|
- waitRotation: DefaultHandshakeWaitRotation,
|
|
|
|
|
|
+ tryInterval: DefaultHandshakeTryInterval,
|
|
|
|
+ retries: DefaultHandshakeRetries,
|
|
|
|
+ waitRotation: DefaultHandshakeWaitRotation,
|
|
|
|
+ triggerBuffer: DefaultHandshakeTriggerBuffer,
|
|
}
|
|
}
|
|
)
|
|
)
|
|
|
|
|
|
type HandshakeConfig struct {
|
|
type HandshakeConfig struct {
|
|
- tryInterval time.Duration
|
|
|
|
- retries int
|
|
|
|
- waitRotation int
|
|
|
|
|
|
+ tryInterval time.Duration
|
|
|
|
+ retries int
|
|
|
|
+ waitRotation int
|
|
|
|
+ triggerBuffer int
|
|
|
|
|
|
messageMetrics *MessageMetrics
|
|
messageMetrics *MessageMetrics
|
|
}
|
|
}
|
|
@@ -42,6 +45,9 @@ type HandshakeManager struct {
|
|
outside *udpConn
|
|
outside *udpConn
|
|
config HandshakeConfig
|
|
config HandshakeConfig
|
|
|
|
|
|
|
|
+ // can be used to trigger outbound handshake for the given vpnIP
|
|
|
|
+ trigger chan uint32
|
|
|
|
+
|
|
OutboundHandshakeTimer *SystemTimerWheel
|
|
OutboundHandshakeTimer *SystemTimerWheel
|
|
InboundHandshakeTimer *SystemTimerWheel
|
|
InboundHandshakeTimer *SystemTimerWheel
|
|
|
|
|
|
@@ -57,6 +63,8 @@ func NewHandshakeManager(tunCidr *net.IPNet, preferredRanges []*net.IPNet, mainH
|
|
|
|
|
|
config: config,
|
|
config: config,
|
|
|
|
|
|
|
|
+ trigger: make(chan uint32, config.triggerBuffer),
|
|
|
|
+
|
|
OutboundHandshakeTimer: NewSystemTimerWheel(config.tryInterval, config.tryInterval*time.Duration(config.retries)),
|
|
OutboundHandshakeTimer: NewSystemTimerWheel(config.tryInterval, config.tryInterval*time.Duration(config.retries)),
|
|
InboundHandshakeTimer: NewSystemTimerWheel(config.tryInterval, config.tryInterval*time.Duration(config.retries)),
|
|
InboundHandshakeTimer: NewSystemTimerWheel(config.tryInterval, config.tryInterval*time.Duration(config.retries)),
|
|
|
|
|
|
@@ -66,9 +74,15 @@ func NewHandshakeManager(tunCidr *net.IPNet, preferredRanges []*net.IPNet, mainH
|
|
|
|
|
|
func (c *HandshakeManager) Run(f EncWriter) {
|
|
func (c *HandshakeManager) Run(f EncWriter) {
|
|
clockSource := time.Tick(c.config.tryInterval)
|
|
clockSource := time.Tick(c.config.tryInterval)
|
|
- for now := range clockSource {
|
|
|
|
- c.NextOutboundHandshakeTimerTick(now, f)
|
|
|
|
- c.NextInboundHandshakeTimerTick(now)
|
|
|
|
|
|
+ for {
|
|
|
|
+ select {
|
|
|
|
+ case vpnIP := <-c.trigger:
|
|
|
|
+ l.WithField("vpnIp", IntIp(vpnIP)).Debug("HandshakeManager: triggered")
|
|
|
|
+ c.handleOutbound(vpnIP, f, true)
|
|
|
|
+ case now := <-clockSource:
|
|
|
|
+ c.NextOutboundHandshakeTimerTick(now, f)
|
|
|
|
+ c.NextInboundHandshakeTimerTick(now)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -80,69 +94,86 @@ func (c *HandshakeManager) NextOutboundHandshakeTimerTick(now time.Time, f EncWr
|
|
break
|
|
break
|
|
}
|
|
}
|
|
vpnIP := ep.(uint32)
|
|
vpnIP := ep.(uint32)
|
|
|
|
+ c.handleOutbound(vpnIP, f, false)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
|
|
- index, err := c.pendingHostMap.GetIndexByVpnIP(vpnIP)
|
|
|
|
- if err != nil {
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- hostinfo, err := c.pendingHostMap.QueryVpnIP(vpnIP)
|
|
|
|
- if err != nil {
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
|
|
+func (c *HandshakeManager) handleOutbound(vpnIP uint32, f EncWriter, lighthouseTriggered bool) {
|
|
|
|
+ index, err := c.pendingHostMap.GetIndexByVpnIP(vpnIP)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ hostinfo, err := c.pendingHostMap.QueryVpnIP(vpnIP)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
|
|
- // If we haven't finished the handshake and we haven't hit max retries, query
|
|
|
|
- // lighthouse and then send the handshake packet again.
|
|
|
|
- if hostinfo.HandshakeCounter < c.config.retries && !hostinfo.HandshakeComplete {
|
|
|
|
- if hostinfo.remote == nil {
|
|
|
|
- // We continue to query the lighthouse because hosts may
|
|
|
|
- // come online during handshake retries. If the query
|
|
|
|
- // succeeds (no error), add the lighthouse info to hostinfo
|
|
|
|
- ips, err := c.lightHouse.Query(vpnIP, f)
|
|
|
|
- if err == nil {
|
|
|
|
- for _, ip := range ips {
|
|
|
|
- hostinfo.AddRemote(ip)
|
|
|
|
- }
|
|
|
|
- hostinfo.ForcePromoteBest(c.mainHostMap.preferredRanges)
|
|
|
|
|
|
+ // If we haven't finished the handshake and we haven't hit max retries, query
|
|
|
|
+ // lighthouse and then send the handshake packet again.
|
|
|
|
+ if hostinfo.HandshakeCounter < c.config.retries && !hostinfo.HandshakeComplete {
|
|
|
|
+ if hostinfo.remote == nil {
|
|
|
|
+ // We continue to query the lighthouse because hosts may
|
|
|
|
+ // come online during handshake retries. If the query
|
|
|
|
+ // succeeds (no error), add the lighthouse info to hostinfo
|
|
|
|
+ ips := c.lightHouse.QueryCache(vpnIP)
|
|
|
|
+ // If we have no responses yet, or only one IP (the host hadn't
|
|
|
|
+ // finished reporting its own IPs yet), then send another query to
|
|
|
|
+ // the LH.
|
|
|
|
+ if len(ips) <= 1 {
|
|
|
|
+ ips, err = c.lightHouse.Query(vpnIP, f)
|
|
|
|
+ }
|
|
|
|
+ if err == nil {
|
|
|
|
+ for _, ip := range ips {
|
|
|
|
+ hostinfo.AddRemote(ip)
|
|
}
|
|
}
|
|
|
|
+ hostinfo.ForcePromoteBest(c.mainHostMap.preferredRanges)
|
|
}
|
|
}
|
|
|
|
+ } else if lighthouseTriggered {
|
|
|
|
+ // We were triggered by a lighthouse HostQueryReply packet, but
|
|
|
|
+ // we have already picked a remote for this host (this can happen
|
|
|
|
+ // if we are configured with multiple lighthouses). So we can skip
|
|
|
|
+ // this trigger and let the timerwheel handle the rest of the
|
|
|
|
+ // process
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
|
|
- hostinfo.HandshakeCounter++
|
|
|
|
|
|
+ hostinfo.HandshakeCounter++
|
|
|
|
|
|
- // We want to use the "best" calculated ip for the first 5 attempts, after that we just blindly rotate through
|
|
|
|
- // all the others until we can stand up a connection.
|
|
|
|
- if hostinfo.HandshakeCounter > c.config.waitRotation {
|
|
|
|
- hostinfo.rotateRemote()
|
|
|
|
- }
|
|
|
|
|
|
+ // We want to use the "best" calculated ip for the first 5 attempts, after that we just blindly rotate through
|
|
|
|
+ // all the others until we can stand up a connection.
|
|
|
|
+ if hostinfo.HandshakeCounter > c.config.waitRotation {
|
|
|
|
+ hostinfo.rotateRemote()
|
|
|
|
+ }
|
|
|
|
|
|
- // Ensure the handshake is ready to avoid a race in timer tick and stage 0 handshake generation
|
|
|
|
- if hostinfo.HandshakeReady && hostinfo.remote != nil {
|
|
|
|
- c.messageMetrics.Tx(handshake, NebulaMessageSubType(hostinfo.HandshakePacket[0][1]), 1)
|
|
|
|
- err := c.outside.WriteTo(hostinfo.HandshakePacket[0], hostinfo.remote)
|
|
|
|
- if err != nil {
|
|
|
|
- hostinfo.logger().WithField("udpAddr", hostinfo.remote).
|
|
|
|
- WithField("initiatorIndex", hostinfo.localIndexId).
|
|
|
|
- WithField("remoteIndex", hostinfo.remoteIndexId).
|
|
|
|
- WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
|
|
|
|
- WithError(err).Error("Failed to send handshake message")
|
|
|
|
- } else {
|
|
|
|
- //TODO: this log line is assuming a lot of stuff around the cached stage 0 handshake packet, we should
|
|
|
|
- // keep the real packet struct around for logging purposes
|
|
|
|
- hostinfo.logger().WithField("udpAddr", hostinfo.remote).
|
|
|
|
- WithField("initiatorIndex", hostinfo.localIndexId).
|
|
|
|
- WithField("remoteIndex", hostinfo.remoteIndexId).
|
|
|
|
- WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
|
|
|
|
- Info("Handshake message sent")
|
|
|
|
- }
|
|
|
|
|
|
+ // Ensure the handshake is ready to avoid a race in timer tick and stage 0 handshake generation
|
|
|
|
+ if hostinfo.HandshakeReady && hostinfo.remote != nil {
|
|
|
|
+ c.messageMetrics.Tx(handshake, NebulaMessageSubType(hostinfo.HandshakePacket[0][1]), 1)
|
|
|
|
+ err := c.outside.WriteTo(hostinfo.HandshakePacket[0], hostinfo.remote)
|
|
|
|
+ if err != nil {
|
|
|
|
+ hostinfo.logger().WithField("udpAddr", hostinfo.remote).
|
|
|
|
+ WithField("initiatorIndex", hostinfo.localIndexId).
|
|
|
|
+ WithField("remoteIndex", hostinfo.remoteIndexId).
|
|
|
|
+ WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
|
|
|
|
+ WithError(err).Error("Failed to send handshake message")
|
|
|
|
+ } else {
|
|
|
|
+ //TODO: this log line is assuming a lot of stuff around the cached stage 0 handshake packet, we should
|
|
|
|
+ // keep the real packet struct around for logging purposes
|
|
|
|
+ hostinfo.logger().WithField("udpAddr", hostinfo.remote).
|
|
|
|
+ WithField("initiatorIndex", hostinfo.localIndexId).
|
|
|
|
+ WithField("remoteIndex", hostinfo.remoteIndexId).
|
|
|
|
+ WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
|
|
|
|
+ Info("Handshake message sent")
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
|
|
- // Readd to the timer wheel so we continue trying wait HandshakeTryInterval * counter longer for next try
|
|
|
|
|
|
+ // Readd to the timer wheel so we continue trying wait HandshakeTryInterval * counter longer for next try
|
|
|
|
+ if !lighthouseTriggered {
|
|
//l.Infoln("Interval: ", HandshakeTryInterval*time.Duration(hostinfo.HandshakeCounter))
|
|
//l.Infoln("Interval: ", HandshakeTryInterval*time.Duration(hostinfo.HandshakeCounter))
|
|
c.OutboundHandshakeTimer.Add(vpnIP, c.config.tryInterval*time.Duration(hostinfo.HandshakeCounter))
|
|
c.OutboundHandshakeTimer.Add(vpnIP, c.config.tryInterval*time.Duration(hostinfo.HandshakeCounter))
|
|
- } else {
|
|
|
|
- c.pendingHostMap.DeleteVpnIP(vpnIP)
|
|
|
|
- c.pendingHostMap.DeleteIndex(index)
|
|
|
|
}
|
|
}
|
|
|
|
+ } else {
|
|
|
|
+ c.pendingHostMap.DeleteVpnIP(vpnIP)
|
|
|
|
+ c.pendingHostMap.DeleteIndex(index)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -169,6 +200,15 @@ func (c *HandshakeManager) AddVpnIP(vpnIP uint32) *HostInfo {
|
|
// We lock here and use an array to insert items to prevent locking the
|
|
// We lock here and use an array to insert items to prevent locking the
|
|
// main receive thread for very long by waiting to add items to the pending map
|
|
// main receive thread for very long by waiting to add items to the pending map
|
|
c.OutboundHandshakeTimer.Add(vpnIP, c.config.tryInterval)
|
|
c.OutboundHandshakeTimer.Add(vpnIP, c.config.tryInterval)
|
|
|
|
+
|
|
|
|
+ // If this is a static host, we don't need to wait for the HostQueryReply
|
|
|
|
+ // We can trigger the handshake right now
|
|
|
|
+ if _, ok := c.lightHouse.staticList[vpnIP]; ok {
|
|
|
|
+ select {
|
|
|
|
+ case c.trigger <- vpnIP:
|
|
|
|
+ default:
|
|
|
|
+ }
|
|
|
|
+ }
|
|
return hostinfo
|
|
return hostinfo
|
|
}
|
|
}
|
|
|
|
|