2020-09-18 08:20:09 -06:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
2021-11-02 12:14:26 -06:00
|
|
|
"context"
|
2020-09-18 08:20:09 -06:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2021-03-05 19:18:33 -07:00
|
|
|
"sync/atomic"
|
2020-09-18 08:20:09 -06:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/slackhq/nebula/cert"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/header"
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
|
|
|
"github.com/slackhq/nebula/udp"
|
2020-09-18 08:20:09 -06: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
|
|
|
|
|
|
|
|
type Control struct {
|
2021-04-16 09:34:28 -06:00
|
|
|
f *Interface
|
|
|
|
l *logrus.Logger
|
2021-11-02 12:14:26 -06:00
|
|
|
cancel context.CancelFunc
|
2021-04-16 09:34:28 -06:00
|
|
|
sshStart func()
|
|
|
|
statsStart func()
|
|
|
|
dnsStart func()
|
2020-09-18 08:20:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type ControlHostInfo struct {
|
2021-11-03 19:54:04 -06:00
|
|
|
VpnIp net.IP `json:"vpnIp"`
|
2020-09-18 08:20:09 -06:00
|
|
|
LocalIndex uint32 `json:"localIndex"`
|
|
|
|
RemoteIndex uint32 `json:"remoteIndex"`
|
2021-11-03 19:54:04 -06:00
|
|
|
RemoteAddrs []*udp.Addr `json:"remoteAddrs"`
|
2020-09-18 08:20:09 -06:00
|
|
|
CachedPackets int `json:"cachedPackets"`
|
|
|
|
Cert *cert.NebulaCertificate `json:"cert"`
|
|
|
|
MessageCounter uint64 `json:"messageCounter"`
|
2021-11-03 19:54:04 -06:00
|
|
|
CurrentRemote *udp.Addr `json:"currentRemote"`
|
2020-09-18 08:20:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start actually runs nebula, this is a nonblocking call. To block use Control.ShutdownBlock()
|
|
|
|
func (c *Control) Start() {
|
2021-04-16 09:34:28 -06: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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start reading packets.
|
2020-09-18 08:20:09 -06:00
|
|
|
c.f.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop signals nebula to shutdown, returns after the shutdown is complete
|
|
|
|
func (c *Control) Stop() {
|
|
|
|
//TODO: stop tun and udp routines, the lock on hostMap effectively does that though
|
2021-03-18 19:37:24 -06:00
|
|
|
c.CloseAllTunnels(false)
|
2021-11-08 11:36:31 -07:00
|
|
|
if err := c.f.Close(); err != nil {
|
|
|
|
c.l.WithError(err).Error("Close interface failed")
|
|
|
|
}
|
2021-11-02 12:14:26 -06:00
|
|
|
c.cancel()
|
2020-09-18 08:20:09 -06: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() {
|
|
|
|
sigChan := make(chan os.Signal)
|
|
|
|
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-01 18:06:01 -07:00
|
|
|
|
|
|
|
// Trigger a lighthouse update, useful for mobile clients that should have an update interval of 0
|
|
|
|
c.f.lightHouse.SendUpdate(c.f)
|
|
|
|
|
|
|
|
// 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 08:20:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListHostmap returns details about the actual or pending (handshaking) hostmap
|
|
|
|
func (c *Control) ListHostmap(pendingMap bool) []ControlHostInfo {
|
|
|
|
if pendingMap {
|
2021-04-14 12:50:09 -06:00
|
|
|
return listHostMap(c.f.handshakeManager.pendingHostMap)
|
2020-09-18 08:20:09 -06:00
|
|
|
} else {
|
2021-04-14 12:50:09 -06:00
|
|
|
return listHostMap(c.f.hostMap)
|
2020-09-18 08:20:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
// GetHostInfoByVpnIp returns a single tunnels hostInfo, or nil if not found
|
|
|
|
func (c *Control) GetHostInfoByVpnIp(vpnIp iputil.VpnIp, pending bool) *ControlHostInfo {
|
2020-09-18 08:20:09 -06:00
|
|
|
var hm *HostMap
|
|
|
|
if pending {
|
|
|
|
hm = c.f.handshakeManager.pendingHostMap
|
|
|
|
} else {
|
|
|
|
hm = c.f.hostMap
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
h, err := hm.QueryVpnIp(vpnIp)
|
2020-09-18 08:20:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
ch := copyHostInfo(h, c.f.hostMap.preferredRanges)
|
2020-09-18 08:20:09 -06:00
|
|
|
return &ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRemoteForTunnel forces a tunnel to use a specific remote
|
2021-11-03 19:54:04 -06:00
|
|
|
func (c *Control) SetRemoteForTunnel(vpnIp iputil.VpnIp, addr udp.Addr) *ControlHostInfo {
|
|
|
|
hostInfo, err := c.f.hostMap.QueryVpnIp(vpnIp)
|
2020-09-18 08:20:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hostInfo.SetRemote(addr.Copy())
|
2021-04-14 12:50:09 -06:00
|
|
|
ch := copyHostInfo(hostInfo, c.f.hostMap.preferredRanges)
|
2020-09-18 08:20:09 -06:00
|
|
|
return &ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseTunnel closes a fully established tunnel. If localOnly is false it will notify the remote end as well.
|
2021-11-03 19:54:04 -06:00
|
|
|
func (c *Control) CloseTunnel(vpnIp iputil.VpnIp, localOnly bool) bool {
|
|
|
|
hostInfo, err := c.f.hostMap.QueryVpnIp(vpnIp)
|
2020-09-18 08:20:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !localOnly {
|
|
|
|
c.f.send(
|
2021-11-03 19:54:04 -06:00
|
|
|
header.CloseTunnel,
|
2020-09-18 08:20:09 -06:00
|
|
|
0,
|
|
|
|
hostInfo.ConnectionState,
|
|
|
|
hostInfo,
|
|
|
|
hostInfo.remote,
|
|
|
|
[]byte{},
|
|
|
|
make([]byte, 12, 12),
|
|
|
|
make([]byte, mtu),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-26 09:42:24 -06:00
|
|
|
c.f.closeTunnel(hostInfo, false)
|
2020-09-18 08:20:09 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-18 19:37:24 -06: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
|
|
|
|
c.f.hostMap.Lock()
|
|
|
|
for _, h := range c.f.hostMap.Hosts {
|
|
|
|
if excludeLighthouses {
|
2021-11-03 19:54:04 -06:00
|
|
|
if _, ok := c.f.lightHouse.lighthouses[h.vpnIp]; ok {
|
2021-03-18 19:37:24 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.ConnectionState.ready {
|
2021-11-03 19:54:04 -06:00
|
|
|
c.f.send(header.CloseTunnel, 0, h.ConnectionState, h, h.remote, []byte{}, make([]byte, 12, 12), make([]byte, mtu))
|
2021-04-26 09:42:24 -06:00
|
|
|
c.f.closeTunnel(h, true)
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
c.l.WithField("vpnIp", h.vpnIp).WithField("udpAddr", h.remote).
|
2021-03-18 19:37:24 -06:00
|
|
|
Debug("Sending close tunnel message")
|
|
|
|
closed++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.f.hostMap.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
func copyHostInfo(h *HostInfo, preferredRanges []*net.IPNet) ControlHostInfo {
|
2020-09-18 08:20:09 -06:00
|
|
|
chi := ControlHostInfo{
|
2021-11-03 19:54:04 -06:00
|
|
|
VpnIp: h.vpnIp.ToIP(),
|
2021-04-14 12:50:09 -06:00
|
|
|
LocalIndex: h.localIndexId,
|
|
|
|
RemoteIndex: h.remoteIndexId,
|
|
|
|
RemoteAddrs: h.remotes.CopyAddrs(preferredRanges),
|
|
|
|
CachedPackets: len(h.packetStore),
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.ConnectionState != nil {
|
|
|
|
chi.MessageCounter = atomic.LoadUint64(&h.ConnectionState.atomicMessageCounter)
|
2020-09-18 08:20:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if c := h.GetCert(); c != nil {
|
|
|
|
chi.Cert = c.Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.remote != nil {
|
2021-03-18 19:37:24 -06:00
|
|
|
chi.CurrentRemote = h.remote.Copy()
|
2020-09-18 08:20:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return chi
|
|
|
|
}
|
2021-04-14 12:50:09 -06:00
|
|
|
|
|
|
|
func listHostMap(hm *HostMap) []ControlHostInfo {
|
|
|
|
hm.RLock()
|
|
|
|
hosts := make([]ControlHostInfo, len(hm.Hosts))
|
|
|
|
i := 0
|
|
|
|
for _, v := range hm.Hosts {
|
|
|
|
hosts[i] = copyHostInfo(v, hm.preferredRanges)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
hm.RUnlock()
|
|
|
|
|
|
|
|
return hosts
|
|
|
|
}
|