2020-09-18 22:20:09 +08:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
2021-11-03 02:14:26 +08:00
|
|
|
"context"
|
2020-09-18 22:20:09 +08:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/slackhq/nebula/cert"
|
2021-11-04 09:54:04 +08:00
|
|
|
"github.com/slackhq/nebula/header"
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
|
|
|
"github.com/slackhq/nebula/udp"
|
2020-09-18 22:20:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Every interaction here needs to take extra care to copy memory and not return or use arguments "as is" when touching
|
|
|
|
// core. This means copying IP objects, slices, de-referencing pointers and taking the actual value, etc
|
|
|
|
|
2023-07-25 01:37:52 +08:00
|
|
|
type controlEach func(h *HostInfo)
|
|
|
|
|
|
|
|
type controlHostLister interface {
|
|
|
|
QueryVpnIp(vpnIp iputil.VpnIp) *HostInfo
|
|
|
|
ForEachIndex(each controlEach)
|
|
|
|
ForEachVpnIp(each controlEach)
|
|
|
|
GetPreferredRanges() []*net.IPNet
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:20:09 +08:00
|
|
|
type Control struct {
|
2023-07-28 03:38:10 +08:00
|
|
|
f *Interface
|
|
|
|
l *logrus.Logger
|
|
|
|
cancel context.CancelFunc
|
|
|
|
sshStart func()
|
|
|
|
statsStart func()
|
|
|
|
dnsStart func()
|
|
|
|
lighthouseStart func()
|
2020-09-18 22:20:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type ControlHostInfo struct {
|
2022-06-22 02:35:23 +08:00
|
|
|
VpnIp net.IP `json:"vpnIp"`
|
|
|
|
LocalIndex uint32 `json:"localIndex"`
|
|
|
|
RemoteIndex uint32 `json:"remoteIndex"`
|
|
|
|
RemoteAddrs []*udp.Addr `json:"remoteAddrs"`
|
|
|
|
CachedPackets int `json:"cachedPackets"`
|
|
|
|
Cert *cert.NebulaCertificate `json:"cert"`
|
|
|
|
MessageCounter uint64 `json:"messageCounter"`
|
|
|
|
CurrentRemote *udp.Addr `json:"currentRemote"`
|
|
|
|
CurrentRelaysToMe []iputil.VpnIp `json:"currentRelaysToMe"`
|
|
|
|
CurrentRelaysThroughMe []iputil.VpnIp `json:"currentRelaysThroughMe"`
|
2020-09-18 22:20:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start actually runs nebula, this is a nonblocking call. To block use Control.ShutdownBlock()
|
|
|
|
func (c *Control) Start() {
|
2021-04-16 23:34:28 +08:00
|
|
|
// Activate the interface
|
|
|
|
c.f.activate()
|
|
|
|
|
|
|
|
// Call all the delayed funcs that waited patiently for the interface to be created.
|
|
|
|
if c.sshStart != nil {
|
|
|
|
go c.sshStart()
|
|
|
|
}
|
|
|
|
if c.statsStart != nil {
|
|
|
|
go c.statsStart()
|
|
|
|
}
|
|
|
|
if c.dnsStart != nil {
|
|
|
|
go c.dnsStart()
|
|
|
|
}
|
2023-07-28 03:38:10 +08:00
|
|
|
if c.lighthouseStart != nil {
|
|
|
|
c.lighthouseStart()
|
|
|
|
}
|
2021-04-16 23:34:28 +08:00
|
|
|
|
|
|
|
// Start reading packets.
|
2020-09-18 22:20:09 +08:00
|
|
|
c.f.run()
|
|
|
|
}
|
|
|
|
|
2023-07-28 03:38:10 +08:00
|
|
|
// Stop signals nebula to shutdown and close all tunnels, returns after the shutdown is complete
|
2020-09-18 22:20:09 +08:00
|
|
|
func (c *Control) Stop() {
|
2022-12-20 01:28:27 +08:00
|
|
|
// Stop the handshakeManager (and other services), to prevent new tunnels from
|
2022-06-22 02:35:23 +08:00
|
|
|
// being created while we're shutting them all down.
|
|
|
|
c.cancel()
|
|
|
|
|
2021-03-19 09:37:24 +08:00
|
|
|
c.CloseAllTunnels(false)
|
2021-11-09 02:36:31 +08:00
|
|
|
if err := c.f.Close(); err != nil {
|
|
|
|
c.l.WithError(err).Error("Close interface failed")
|
|
|
|
}
|
2020-09-18 22:20:09 +08:00
|
|
|
c.l.Info("Goodbye")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShutdownBlock will listen for and block on term and interrupt signals, calling Control.Stop() once signalled
|
|
|
|
func (c *Control) ShutdownBlock() {
|
2023-03-14 03:35:12 +08:00
|
|
|
sigChan := make(chan os.Signal, 1)
|
2020-09-18 22:20:09 +08:00
|
|
|
signal.Notify(sigChan, syscall.SIGTERM)
|
|
|
|
signal.Notify(sigChan, syscall.SIGINT)
|
|
|
|
|
|
|
|
rawSig := <-sigChan
|
|
|
|
sig := rawSig.String()
|
|
|
|
c.l.WithField("signal", sig).Info("Caught signal, shutting down")
|
|
|
|
c.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RebindUDPServer asks the UDP listener to rebind it's listener. Mainly used on mobile clients when interfaces change
|
|
|
|
func (c *Control) RebindUDPServer() {
|
|
|
|
_ = c.f.outside.Rebind()
|
2021-03-02 09:06:01 +08:00
|
|
|
|
|
|
|
// Trigger a lighthouse update, useful for mobile clients that should have an update interval of 0
|
2023-07-28 03:38:10 +08:00
|
|
|
c.f.lightHouse.SendUpdate()
|
2021-03-02 09:06:01 +08:00
|
|
|
|
|
|
|
// Let the main interface know that we rebound so that underlying tunnels know to trigger punches from their remotes
|
|
|
|
c.f.rebindCount++
|
2020-09-18 22:20:09 +08:00
|
|
|
}
|
|
|
|
|
2023-03-14 01:35:14 +08:00
|
|
|
// ListHostmapHosts returns details about the actual or pending (handshaking) hostmap by vpn ip
|
|
|
|
func (c *Control) ListHostmapHosts(pendingMap bool) []ControlHostInfo {
|
2020-09-18 22:20:09 +08:00
|
|
|
if pendingMap {
|
2023-07-25 01:37:52 +08:00
|
|
|
return listHostMapHosts(c.f.handshakeManager)
|
2020-09-18 22:20:09 +08:00
|
|
|
} else {
|
2023-03-14 01:35:14 +08:00
|
|
|
return listHostMapHosts(c.f.hostMap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListHostmapIndexes returns details about the actual or pending (handshaking) hostmap by local index id
|
|
|
|
func (c *Control) ListHostmapIndexes(pendingMap bool) []ControlHostInfo {
|
|
|
|
if pendingMap {
|
2023-07-25 01:37:52 +08:00
|
|
|
return listHostMapIndexes(c.f.handshakeManager)
|
2023-03-14 01:35:14 +08:00
|
|
|
} else {
|
|
|
|
return listHostMapIndexes(c.f.hostMap)
|
2020-09-18 22:20:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 09:54:04 +08:00
|
|
|
// GetHostInfoByVpnIp returns a single tunnels hostInfo, or nil if not found
|
|
|
|
func (c *Control) GetHostInfoByVpnIp(vpnIp iputil.VpnIp, pending bool) *ControlHostInfo {
|
2023-07-25 01:37:52 +08:00
|
|
|
var hl controlHostLister
|
2020-09-18 22:20:09 +08:00
|
|
|
if pending {
|
2023-07-25 01:37:52 +08:00
|
|
|
hl = c.f.handshakeManager
|
2020-09-18 22:20:09 +08:00
|
|
|
} else {
|
2023-07-25 01:37:52 +08:00
|
|
|
hl = c.f.hostMap
|
2020-09-18 22:20:09 +08:00
|
|
|
}
|
|
|
|
|
2023-07-25 01:37:52 +08:00
|
|
|
h := hl.QueryVpnIp(vpnIp)
|
|
|
|
if h == nil {
|
2020-09-18 22:20:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:50:09 +08:00
|
|
|
ch := copyHostInfo(h, c.f.hostMap.preferredRanges)
|
2020-09-18 22:20:09 +08:00
|
|
|
return &ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRemoteForTunnel forces a tunnel to use a specific remote
|
2021-11-04 09:54:04 +08:00
|
|
|
func (c *Control) SetRemoteForTunnel(vpnIp iputil.VpnIp, addr udp.Addr) *ControlHostInfo {
|
2023-07-25 01:37:52 +08:00
|
|
|
hostInfo := c.f.hostMap.QueryVpnIp(vpnIp)
|
|
|
|
if hostInfo == nil {
|
2020-09-18 22:20:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hostInfo.SetRemote(addr.Copy())
|
2021-04-15 02:50:09 +08:00
|
|
|
ch := copyHostInfo(hostInfo, c.f.hostMap.preferredRanges)
|
2020-09-18 22:20:09 +08:00
|
|
|
return &ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseTunnel closes a fully established tunnel. If localOnly is false it will notify the remote end as well.
|
2021-11-04 09:54:04 +08:00
|
|
|
func (c *Control) CloseTunnel(vpnIp iputil.VpnIp, localOnly bool) bool {
|
2023-07-25 01:37:52 +08:00
|
|
|
hostInfo := c.f.hostMap.QueryVpnIp(vpnIp)
|
|
|
|
if hostInfo == nil {
|
2020-09-18 22:20:09 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !localOnly {
|
|
|
|
c.f.send(
|
2021-11-04 09:54:04 +08:00
|
|
|
header.CloseTunnel,
|
2020-09-18 22:20:09 +08:00
|
|
|
0,
|
|
|
|
hostInfo.ConnectionState,
|
|
|
|
hostInfo,
|
|
|
|
[]byte{},
|
|
|
|
make([]byte, 12, 12),
|
|
|
|
make([]byte, mtu),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-22 02:35:23 +08:00
|
|
|
c.f.closeTunnel(hostInfo)
|
2020-09-18 22:20:09 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:37:24 +08:00
|
|
|
// CloseAllTunnels is just like CloseTunnel except it goes through and shuts them all down, optionally you can avoid shutting down lighthouse tunnels
|
|
|
|
// the int returned is a count of tunnels closed
|
|
|
|
func (c *Control) CloseAllTunnels(excludeLighthouses bool) (closed int) {
|
|
|
|
//TODO: this is probably better as a function in ConnectionManager or HostMap directly
|
2022-03-15 01:35:13 +08:00
|
|
|
lighthouses := c.f.lightHouse.GetLighthouses()
|
2022-06-22 02:35:23 +08:00
|
|
|
|
|
|
|
shutdown := func(h *HostInfo) {
|
2021-03-19 09:37:24 +08:00
|
|
|
if excludeLighthouses {
|
2022-03-15 01:35:13 +08:00
|
|
|
if _, ok := lighthouses[h.vpnIp]; ok {
|
2022-06-22 02:35:23 +08:00
|
|
|
return
|
2021-03-19 09:37:24 +08:00
|
|
|
}
|
|
|
|
}
|
2022-06-22 02:35:23 +08:00
|
|
|
c.f.send(header.CloseTunnel, 0, h.ConnectionState, h, []byte{}, make([]byte, 12, 12), make([]byte, mtu))
|
|
|
|
c.f.closeTunnel(h)
|
2021-03-19 09:37:24 +08:00
|
|
|
|
2022-06-22 02:35:23 +08:00
|
|
|
c.l.WithField("vpnIp", h.vpnIp).WithField("udpAddr", h.remote).
|
|
|
|
Debug("Sending close tunnel message")
|
|
|
|
closed++
|
|
|
|
}
|
2021-04-26 23:42:24 +08:00
|
|
|
|
2022-06-22 02:35:23 +08:00
|
|
|
// Learn which hosts are being used as relays, so we can shut them down last.
|
|
|
|
relayingHosts := map[iputil.VpnIp]*HostInfo{}
|
|
|
|
// Grab the hostMap lock to access the Relays map
|
|
|
|
c.f.hostMap.Lock()
|
|
|
|
for _, relayingHost := range c.f.hostMap.Relays {
|
|
|
|
relayingHosts[relayingHost.vpnIp] = relayingHost
|
|
|
|
}
|
|
|
|
c.f.hostMap.Unlock()
|
|
|
|
|
|
|
|
hostInfos := []*HostInfo{}
|
|
|
|
// Grab the hostMap lock to access the Hosts map
|
|
|
|
c.f.hostMap.Lock()
|
2023-03-31 00:09:20 +08:00
|
|
|
for _, relayHost := range c.f.hostMap.Indexes {
|
2022-06-22 02:35:23 +08:00
|
|
|
if _, ok := relayingHosts[relayHost.vpnIp]; !ok {
|
|
|
|
hostInfos = append(hostInfos, relayHost)
|
2021-03-19 09:37:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.f.hostMap.Unlock()
|
2022-06-22 02:35:23 +08:00
|
|
|
|
|
|
|
for _, h := range hostInfos {
|
|
|
|
shutdown(h)
|
|
|
|
}
|
|
|
|
for _, h := range relayingHosts {
|
|
|
|
shutdown(h)
|
|
|
|
}
|
2021-03-19 09:37:24 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:50:09 +08:00
|
|
|
func copyHostInfo(h *HostInfo, preferredRanges []*net.IPNet) ControlHostInfo {
|
2022-06-22 02:35:23 +08:00
|
|
|
|
2020-09-18 22:20:09 +08:00
|
|
|
chi := ControlHostInfo{
|
2022-06-22 02:35:23 +08:00
|
|
|
VpnIp: h.vpnIp.ToIP(),
|
|
|
|
LocalIndex: h.localIndexId,
|
|
|
|
RemoteIndex: h.remoteIndexId,
|
|
|
|
RemoteAddrs: h.remotes.CopyAddrs(preferredRanges),
|
|
|
|
CachedPackets: len(h.packetStore),
|
|
|
|
CurrentRelaysToMe: h.relayState.CopyRelayIps(),
|
|
|
|
CurrentRelaysThroughMe: h.relayState.CopyRelayForIps(),
|
2021-04-15 02:50:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if h.ConnectionState != nil {
|
2022-11-01 01:37:41 +08:00
|
|
|
chi.MessageCounter = h.ConnectionState.messageCounter.Load()
|
2020-09-18 22:20:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if c := h.GetCert(); c != nil {
|
|
|
|
chi.Cert = c.Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.remote != nil {
|
2021-03-19 09:37:24 +08:00
|
|
|
chi.CurrentRemote = h.remote.Copy()
|
2020-09-18 22:20:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return chi
|
|
|
|
}
|
2021-04-15 02:50:09 +08:00
|
|
|
|
2023-07-25 01:37:52 +08:00
|
|
|
func listHostMapHosts(hl controlHostLister) []ControlHostInfo {
|
|
|
|
hosts := make([]ControlHostInfo, 0)
|
|
|
|
pr := hl.GetPreferredRanges()
|
|
|
|
hl.ForEachVpnIp(func(hostinfo *HostInfo) {
|
|
|
|
hosts = append(hosts, copyHostInfo(hostinfo, pr))
|
|
|
|
})
|
2021-04-15 02:50:09 +08:00
|
|
|
return hosts
|
|
|
|
}
|
2023-03-14 01:35:14 +08:00
|
|
|
|
2023-07-25 01:37:52 +08:00
|
|
|
func listHostMapIndexes(hl controlHostLister) []ControlHostInfo {
|
|
|
|
hosts := make([]ControlHostInfo, 0)
|
|
|
|
pr := hl.GetPreferredRanges()
|
|
|
|
hl.ForEachIndex(func(hostinfo *HostInfo) {
|
|
|
|
hosts = append(hosts, copyHostInfo(hostinfo, pr))
|
|
|
|
})
|
2023-03-14 01:35:14 +08:00
|
|
|
return hosts
|
|
|
|
}
|