2019-11-19 18:00:20 +01:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-07-28 14:53:16 +02:00
|
|
|
"io"
|
|
|
|
"net"
|
2019-11-19 18:00:20 +01:00
|
|
|
"os"
|
2021-02-25 21:01:14 +01:00
|
|
|
"runtime"
|
2019-11-19 18:00:20 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rcrowley/go-metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
const mtu = 9001
|
|
|
|
|
2020-07-28 14:53:16 +02:00
|
|
|
type Inside interface {
|
|
|
|
io.ReadWriteCloser
|
|
|
|
Activate() error
|
|
|
|
CidrNet() *net.IPNet
|
|
|
|
DeviceName() string
|
|
|
|
WriteRaw([]byte) error
|
2021-02-25 21:01:14 +01:00
|
|
|
NewMultiQueueReader() (io.ReadWriteCloser, error)
|
2020-07-28 14:53:16 +02:00
|
|
|
}
|
|
|
|
|
2019-11-19 18:00:20 +01:00
|
|
|
type InterfaceConfig struct {
|
2019-11-23 17:50:36 +01:00
|
|
|
HostMap *HostMap
|
|
|
|
Outside *udpConn
|
2020-07-28 14:53:16 +02:00
|
|
|
Inside Inside
|
2019-11-23 17:50:36 +01:00
|
|
|
certState *CertState
|
|
|
|
Cipher string
|
|
|
|
Firewall *Firewall
|
|
|
|
ServeDns bool
|
|
|
|
HandshakeManager *HandshakeManager
|
|
|
|
lightHouse *LightHouse
|
|
|
|
checkInterval int
|
|
|
|
pendingDeletionInterval int
|
|
|
|
DropLocalBroadcast bool
|
|
|
|
DropMulticast bool
|
|
|
|
UDPBatchSize int
|
2021-02-25 21:01:14 +01:00
|
|
|
routines int
|
2020-06-26 19:45:48 +02:00
|
|
|
MessageMetrics *MessageMetrics
|
2020-09-18 16:20:09 +02:00
|
|
|
version string
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Interface struct {
|
2019-11-23 17:50:36 +01:00
|
|
|
hostMap *HostMap
|
|
|
|
outside *udpConn
|
2020-07-28 14:53:16 +02:00
|
|
|
inside Inside
|
2019-11-23 17:50:36 +01:00
|
|
|
certState *CertState
|
|
|
|
cipher string
|
|
|
|
firewall *Firewall
|
|
|
|
connectionManager *connectionManager
|
|
|
|
handshakeManager *HandshakeManager
|
|
|
|
serveDns bool
|
|
|
|
createTime time.Time
|
|
|
|
lightHouse *LightHouse
|
|
|
|
localBroadcast uint32
|
|
|
|
dropLocalBroadcast bool
|
|
|
|
dropMulticast bool
|
|
|
|
udpBatchSize int
|
2021-02-25 21:01:14 +01:00
|
|
|
routines int
|
2019-11-23 17:50:36 +01:00
|
|
|
version string
|
2019-11-19 18:00:20 +01:00
|
|
|
|
2021-02-25 21:01:14 +01:00
|
|
|
writers []*udpConn
|
|
|
|
readers []io.ReadWriteCloser
|
|
|
|
|
2020-06-26 19:45:48 +02:00
|
|
|
metricHandshakes metrics.Histogram
|
|
|
|
messageMetrics *MessageMetrics
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewInterface(c *InterfaceConfig) (*Interface, error) {
|
|
|
|
if c.Outside == nil {
|
|
|
|
return nil, errors.New("no outside connection")
|
|
|
|
}
|
|
|
|
if c.Inside == nil {
|
|
|
|
return nil, errors.New("no inside interface (tun)")
|
|
|
|
}
|
|
|
|
if c.certState == nil {
|
|
|
|
return nil, errors.New("no certificate state")
|
|
|
|
}
|
|
|
|
if c.Firewall == nil {
|
|
|
|
return nil, errors.New("no firewall rules")
|
|
|
|
}
|
|
|
|
|
|
|
|
ifce := &Interface{
|
2019-11-23 17:50:36 +01:00
|
|
|
hostMap: c.HostMap,
|
|
|
|
outside: c.Outside,
|
|
|
|
inside: c.Inside,
|
|
|
|
certState: c.certState,
|
|
|
|
cipher: c.Cipher,
|
|
|
|
firewall: c.Firewall,
|
|
|
|
serveDns: c.ServeDns,
|
|
|
|
handshakeManager: c.HandshakeManager,
|
|
|
|
createTime: time.Now(),
|
|
|
|
lightHouse: c.lightHouse,
|
|
|
|
localBroadcast: ip2int(c.certState.certificate.Details.Ips[0].IP) | ^ip2int(c.certState.certificate.Details.Ips[0].Mask),
|
|
|
|
dropLocalBroadcast: c.DropLocalBroadcast,
|
|
|
|
dropMulticast: c.DropMulticast,
|
|
|
|
udpBatchSize: c.UDPBatchSize,
|
2021-02-25 21:01:14 +01:00
|
|
|
routines: c.routines,
|
2020-09-18 16:20:09 +02:00
|
|
|
version: c.version,
|
2021-02-25 21:01:14 +01:00
|
|
|
writers: make([]*udpConn, c.routines),
|
|
|
|
readers: make([]io.ReadWriteCloser, c.routines),
|
2019-11-19 18:00:20 +01:00
|
|
|
|
2020-06-26 19:45:48 +02:00
|
|
|
metricHandshakes: metrics.GetOrRegisterHistogram("handshakes", nil, metrics.NewExpDecaySample(1028, 0.015)),
|
|
|
|
messageMetrics: c.MessageMetrics,
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ifce.connectionManager = newConnectionManager(ifce, c.checkInterval, c.pendingDeletionInterval)
|
|
|
|
|
|
|
|
return ifce, nil
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:20:09 +02:00
|
|
|
func (f *Interface) run() {
|
2019-11-19 18:00:20 +01:00
|
|
|
// actually turn on tun dev
|
|
|
|
|
2020-02-07 06:17:43 +01:00
|
|
|
addr, err := f.outside.LocalAddr()
|
|
|
|
if err != nil {
|
|
|
|
l.WithError(err).Error("Failed to get udp listen address")
|
|
|
|
}
|
|
|
|
|
2020-07-28 14:53:16 +02:00
|
|
|
l.WithField("interface", f.inside.DeviceName()).WithField("network", f.inside.CidrNet().String()).
|
2020-09-18 16:20:09 +02:00
|
|
|
WithField("build", f.version).WithField("udpAddr", addr).
|
2019-11-19 18:00:20 +01:00
|
|
|
Info("Nebula interface is active")
|
|
|
|
|
2021-02-25 21:01:14 +01:00
|
|
|
metrics.GetOrRegisterGauge("routines", nil).Update(int64(f.routines))
|
|
|
|
|
2019-11-19 18:00:20 +01:00
|
|
|
// Launch n queues to read packets from udp
|
2021-02-25 21:01:14 +01:00
|
|
|
for i := 0; i < f.routines; i++ {
|
2019-11-19 18:00:20 +01:00
|
|
|
go f.listenOut(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch n queues to read packets from tun dev
|
2021-02-25 21:01:14 +01:00
|
|
|
var reader io.ReadWriteCloser = f.inside
|
|
|
|
for i := 0; i < f.routines; i++ {
|
|
|
|
if i > 0 {
|
|
|
|
reader, err = f.inside.NewMultiQueueReader()
|
|
|
|
if err != nil {
|
|
|
|
l.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.readers[i] = reader
|
|
|
|
go f.listenIn(reader, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.inside.Activate(); err != nil {
|
|
|
|
l.Fatal(err)
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Interface) listenOut(i int) {
|
2021-02-25 21:01:14 +01:00
|
|
|
runtime.LockOSThread()
|
2019-11-19 18:00:20 +01:00
|
|
|
|
|
|
|
var li *udpConn
|
2021-02-25 21:01:14 +01:00
|
|
|
// TODO clean this up with a coherent interface for each outside connection
|
2019-11-19 18:00:20 +01:00
|
|
|
if i > 0 {
|
2021-02-25 21:01:14 +01:00
|
|
|
li = f.writers[i]
|
2019-11-19 18:00:20 +01:00
|
|
|
} else {
|
|
|
|
li = f.outside
|
|
|
|
}
|
2021-02-25 21:01:14 +01:00
|
|
|
li.ListenOut(f, i)
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 21:01:14 +01:00
|
|
|
func (f *Interface) listenIn(reader io.ReadWriteCloser, i int) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
2019-11-19 18:00:20 +01:00
|
|
|
packet := make([]byte, mtu)
|
|
|
|
out := make([]byte, mtu)
|
|
|
|
fwPacket := &FirewallPacket{}
|
|
|
|
nb := make([]byte, 12, 12)
|
|
|
|
|
|
|
|
for {
|
2021-02-25 21:01:14 +01:00
|
|
|
n, err := reader.Read(packet)
|
2019-11-19 18:00:20 +01:00
|
|
|
if err != nil {
|
|
|
|
l.WithError(err).Error("Error while reading outbound packet")
|
|
|
|
// This only seems to happen when something fatal happens to the fd, so exit.
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
2021-02-25 21:01:14 +01:00
|
|
|
f.consumeInsidePacket(packet[:n], fwPacket, nb, out, i)
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Interface) RegisterConfigChangeCallbacks(c *Config) {
|
|
|
|
c.RegisterReloadCallback(f.reloadCA)
|
|
|
|
c.RegisterReloadCallback(f.reloadCertKey)
|
|
|
|
c.RegisterReloadCallback(f.reloadFirewall)
|
2021-02-25 21:01:14 +01:00
|
|
|
for _, udpConn := range f.writers {
|
|
|
|
c.RegisterReloadCallback(udpConn.reloadConfig)
|
|
|
|
}
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Interface) reloadCA(c *Config) {
|
|
|
|
// reload and check regardless
|
|
|
|
// todo: need mutex?
|
|
|
|
newCAs, err := loadCAFromConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
l.WithError(err).Error("Could not refresh trusted CA certificates")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
trustedCAs = newCAs
|
|
|
|
l.WithField("fingerprints", trustedCAs.GetFingerprints()).Info("Trusted CA certificates refreshed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Interface) reloadCertKey(c *Config) {
|
|
|
|
// reload and check in all cases
|
|
|
|
cs, err := NewCertStateFromConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
l.WithError(err).Error("Could not refresh client cert")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// did IP in cert change? if so, don't set
|
|
|
|
oldIPs := f.certState.certificate.Details.Ips
|
|
|
|
newIPs := cs.certificate.Details.Ips
|
|
|
|
if len(oldIPs) > 0 && len(newIPs) > 0 && oldIPs[0].String() != newIPs[0].String() {
|
|
|
|
l.WithField("new_ip", newIPs[0]).WithField("old_ip", oldIPs[0]).Error("IP in new cert was different from old")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f.certState = cs
|
|
|
|
l.WithField("cert", cs.certificate).Info("Client cert refreshed from disk")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Interface) reloadFirewall(c *Config) {
|
|
|
|
//TODO: need to trigger/detect if the certificate changed too
|
|
|
|
if c.HasChanged("firewall") == false {
|
|
|
|
l.Debug("No firewall config change detected")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fw, err := NewFirewallFromConfig(f.certState.certificate, c)
|
|
|
|
if err != nil {
|
|
|
|
l.WithError(err).Error("Error while creating firewall during reload")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
oldFw := f.firewall
|
2020-08-01 00:53:36 +02:00
|
|
|
conntrack := oldFw.Conntrack
|
|
|
|
conntrack.Lock()
|
|
|
|
defer conntrack.Unlock()
|
|
|
|
|
|
|
|
fw.rulesVersion = oldFw.rulesVersion + 1
|
|
|
|
// If rulesVersion is back to zero, we have wrapped all the way around. Be
|
|
|
|
// safe and just reset conntrack in this case.
|
|
|
|
if fw.rulesVersion == 0 {
|
|
|
|
l.WithField("firewallHash", fw.GetRuleHash()).
|
|
|
|
WithField("oldFirewallHash", oldFw.GetRuleHash()).
|
|
|
|
WithField("rulesVersion", fw.rulesVersion).
|
|
|
|
Warn("firewall rulesVersion has overflowed, resetting conntrack")
|
|
|
|
} else {
|
|
|
|
fw.Conntrack = conntrack
|
|
|
|
}
|
|
|
|
|
2019-11-19 18:00:20 +01:00
|
|
|
f.firewall = fw
|
|
|
|
|
|
|
|
oldFw.Destroy()
|
|
|
|
l.WithField("firewallHash", fw.GetRuleHash()).
|
|
|
|
WithField("oldFirewallHash", oldFw.GetRuleHash()).
|
2020-08-01 00:53:36 +02:00
|
|
|
WithField("rulesVersion", fw.rulesVersion).
|
2019-11-19 18:00:20 +01:00
|
|
|
Info("New firewall has been installed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Interface) emitStats(i time.Duration) {
|
|
|
|
ticker := time.NewTicker(i)
|
|
|
|
for range ticker.C {
|
|
|
|
f.firewall.EmitStats()
|
|
|
|
f.handshakeManager.EmitStats()
|
|
|
|
}
|
|
|
|
}
|