2019-11-19 10:00:20 -07:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
2021-11-02 12:14:26 -06:00
|
|
|
"context"
|
2024-07-31 09:18:56 -06:00
|
|
|
"encoding/binary"
|
2019-11-19 10:00:20 -07:00
|
|
|
"errors"
|
2022-06-27 10:37:54 -06:00
|
|
|
"fmt"
|
2020-07-28 06:53:16 -06:00
|
|
|
"io"
|
2024-10-10 17:00:22 -06:00
|
|
|
"net"
|
2024-07-31 09:18:56 -06:00
|
|
|
"net/netip"
|
2019-11-19 10:00:20 -07:00
|
|
|
"os"
|
2021-02-25 13:01:14 -07:00
|
|
|
"runtime"
|
2021-11-08 11:36:31 -07:00
|
|
|
"sync/atomic"
|
2019-11-19 10:00:20 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rcrowley/go-metrics"
|
2021-03-26 08:46:30 -06:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/config"
|
|
|
|
"github.com/slackhq/nebula/firewall"
|
2023-04-07 12:28:37 -06:00
|
|
|
"github.com/slackhq/nebula/header"
|
2021-11-10 20:52:26 -07:00
|
|
|
"github.com/slackhq/nebula/overlay"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/udp"
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const mtu = 9001
|
|
|
|
|
|
|
|
type InterfaceConfig struct {
|
2019-11-23 09:50:36 -07:00
|
|
|
HostMap *HostMap
|
2023-06-14 09:48:52 -06:00
|
|
|
Outside udp.Conn
|
2021-11-10 20:52:26 -07:00
|
|
|
Inside overlay.Device
|
2023-08-14 20:32:40 -06:00
|
|
|
pki *PKI
|
2019-11-23 09:50:36 -07:00
|
|
|
Cipher string
|
|
|
|
Firewall *Firewall
|
|
|
|
ServeDns bool
|
|
|
|
HandshakeManager *HandshakeManager
|
|
|
|
lightHouse *LightHouse
|
2023-03-31 14:45:05 -06:00
|
|
|
checkInterval time.Duration
|
|
|
|
pendingDeletionInterval time.Duration
|
2019-11-23 09:50:36 -07:00
|
|
|
DropLocalBroadcast bool
|
|
|
|
DropMulticast bool
|
2021-02-25 13:01:14 -07:00
|
|
|
routines int
|
2020-06-26 11:45:48 -06:00
|
|
|
MessageMetrics *MessageMetrics
|
2020-09-18 08:20:09 -06:00
|
|
|
version string
|
2022-06-21 12:35:23 -06:00
|
|
|
relayManager *relayManager
|
2023-03-31 14:45:05 -06:00
|
|
|
punchy *Punchy
|
2021-03-01 17:52:17 -07:00
|
|
|
|
2023-08-08 12:26:41 -06:00
|
|
|
tryPromoteEvery uint32
|
|
|
|
reQueryEvery uint32
|
|
|
|
reQueryWait time.Duration
|
|
|
|
|
2021-03-01 17:52:17 -07:00
|
|
|
ConntrackCacheTimeout time.Duration
|
2021-03-26 08:46:30 -06:00
|
|
|
l *logrus.Logger
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type Interface struct {
|
2019-11-23 09:50:36 -07:00
|
|
|
hostMap *HostMap
|
2023-06-14 09:48:52 -06:00
|
|
|
outside udp.Conn
|
2021-11-10 20:52:26 -07:00
|
|
|
inside overlay.Device
|
2023-08-14 20:32:40 -06:00
|
|
|
pki *PKI
|
2019-11-23 09:50:36 -07:00
|
|
|
cipher string
|
|
|
|
firewall *Firewall
|
|
|
|
connectionManager *connectionManager
|
|
|
|
handshakeManager *HandshakeManager
|
|
|
|
serveDns bool
|
|
|
|
createTime time.Time
|
|
|
|
lightHouse *LightHouse
|
2024-07-31 09:18:56 -06:00
|
|
|
myBroadcastAddr netip.Addr
|
|
|
|
myVpnNet netip.Prefix
|
2019-11-23 09:50:36 -07:00
|
|
|
dropLocalBroadcast bool
|
|
|
|
dropMulticast bool
|
2021-02-25 13:01:14 -07:00
|
|
|
routines int
|
2023-11-13 11:39:38 -07:00
|
|
|
disconnectInvalid atomic.Bool
|
2022-10-31 11:37:41 -06:00
|
|
|
closed atomic.Bool
|
2022-06-21 12:35:23 -06:00
|
|
|
relayManager *relayManager
|
2021-03-01 18:06:01 -07:00
|
|
|
|
2023-08-08 12:26:41 -06:00
|
|
|
tryPromoteEvery atomic.Uint32
|
|
|
|
reQueryEvery atomic.Uint32
|
|
|
|
reQueryWait atomic.Int64
|
|
|
|
|
2022-06-27 10:37:54 -06:00
|
|
|
sendRecvErrorConfig sendRecvErrorConfig
|
|
|
|
|
2021-03-01 18:06:01 -07:00
|
|
|
// rebindCount is used to decide if an active tunnel should trigger a punch notification through a lighthouse
|
|
|
|
rebindCount int8
|
|
|
|
version string
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-01 17:52:17 -07:00
|
|
|
conntrackCacheTimeout time.Duration
|
|
|
|
|
2023-06-14 09:48:52 -06:00
|
|
|
writers []udp.Conn
|
2021-02-25 13:01:14 -07:00
|
|
|
readers []io.ReadWriteCloser
|
|
|
|
|
2021-04-27 20:23:18 -06:00
|
|
|
metricHandshakes metrics.Histogram
|
|
|
|
messageMetrics *MessageMetrics
|
|
|
|
cachedPacketMetrics *cachedPacketMetrics
|
|
|
|
|
|
|
|
l *logrus.Logger
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-04-07 12:28:37 -06:00
|
|
|
type EncWriter interface {
|
|
|
|
SendVia(via *HostInfo,
|
|
|
|
relay *Relay,
|
|
|
|
ad,
|
|
|
|
nb,
|
|
|
|
out []byte,
|
|
|
|
nocopy bool,
|
|
|
|
)
|
2024-07-31 09:18:56 -06:00
|
|
|
SendMessageToVpnIp(t header.MessageType, st header.MessageSubType, vpnIp netip.Addr, p, nb, out []byte)
|
2023-05-04 14:16:37 -06:00
|
|
|
SendMessageToHostInfo(t header.MessageType, st header.MessageSubType, hostinfo *HostInfo, p, nb, out []byte)
|
2024-07-31 09:18:56 -06:00
|
|
|
Handshake(vpnIp netip.Addr)
|
2023-04-07 12:28:37 -06:00
|
|
|
}
|
|
|
|
|
2022-06-27 10:37:54 -06:00
|
|
|
type sendRecvErrorConfig uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
sendRecvErrorAlways sendRecvErrorConfig = iota
|
|
|
|
sendRecvErrorNever
|
|
|
|
sendRecvErrorPrivate
|
|
|
|
)
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func (s sendRecvErrorConfig) ShouldSendRecvError(ip netip.AddrPort) bool {
|
2022-06-27 10:37:54 -06:00
|
|
|
switch s {
|
|
|
|
case sendRecvErrorPrivate:
|
2024-07-31 09:18:56 -06:00
|
|
|
return ip.Addr().IsPrivate()
|
2022-06-27 10:37:54 -06:00
|
|
|
case sendRecvErrorAlways:
|
|
|
|
return true
|
|
|
|
case sendRecvErrorNever:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("invalid sendRecvErrorConfig value: %d", s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sendRecvErrorConfig) String() string {
|
|
|
|
switch s {
|
|
|
|
case sendRecvErrorAlways:
|
|
|
|
return "always"
|
|
|
|
case sendRecvErrorNever:
|
|
|
|
return "never"
|
|
|
|
case sendRecvErrorPrivate:
|
|
|
|
return "private"
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("invalid(%d)", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 12:14:26 -06:00
|
|
|
func NewInterface(ctx context.Context, c *InterfaceConfig) (*Interface, error) {
|
2019-11-19 10:00:20 -07:00
|
|
|
if c.Outside == nil {
|
|
|
|
return nil, errors.New("no outside connection")
|
|
|
|
}
|
|
|
|
if c.Inside == nil {
|
|
|
|
return nil, errors.New("no inside interface (tun)")
|
|
|
|
}
|
2023-08-14 20:32:40 -06:00
|
|
|
if c.pki == nil {
|
2019-11-19 10:00:20 -07:00
|
|
|
return nil, errors.New("no certificate state")
|
|
|
|
}
|
|
|
|
if c.Firewall == nil {
|
|
|
|
return nil, errors.New("no firewall rules")
|
|
|
|
}
|
|
|
|
|
2023-08-14 20:32:40 -06:00
|
|
|
certificate := c.pki.GetCertState().Certificate
|
2024-07-31 09:18:56 -06:00
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
ifce := &Interface{
|
2023-08-14 20:32:40 -06:00
|
|
|
pki: c.pki,
|
2019-11-23 09:50:36 -07:00
|
|
|
hostMap: c.HostMap,
|
|
|
|
outside: c.Outside,
|
|
|
|
inside: c.Inside,
|
|
|
|
cipher: c.Cipher,
|
|
|
|
firewall: c.Firewall,
|
|
|
|
serveDns: c.ServeDns,
|
|
|
|
handshakeManager: c.HandshakeManager,
|
|
|
|
createTime: time.Now(),
|
|
|
|
lightHouse: c.lightHouse,
|
|
|
|
dropLocalBroadcast: c.DropLocalBroadcast,
|
|
|
|
dropMulticast: c.DropMulticast,
|
2021-02-25 13:01:14 -07:00
|
|
|
routines: c.routines,
|
2020-09-18 08:20:09 -06:00
|
|
|
version: c.version,
|
2023-06-14 09:48:52 -06:00
|
|
|
writers: make([]udp.Conn, c.routines),
|
2021-02-25 13:01:14 -07:00
|
|
|
readers: make([]io.ReadWriteCloser, c.routines),
|
2024-10-10 17:00:22 -06:00
|
|
|
myVpnNet: certificate.Networks()[0],
|
2022-06-21 12:35:23 -06:00
|
|
|
relayManager: c.relayManager,
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-01 17:52:17 -07:00
|
|
|
conntrackCacheTimeout: c.ConntrackCacheTimeout,
|
|
|
|
|
2020-06-26 11:45:48 -06:00
|
|
|
metricHandshakes: metrics.GetOrRegisterHistogram("handshakes", nil, metrics.NewExpDecaySample(1028, 0.015)),
|
|
|
|
messageMetrics: c.MessageMetrics,
|
2021-04-27 20:23:18 -06:00
|
|
|
cachedPacketMetrics: &cachedPacketMetrics{
|
|
|
|
sent: metrics.GetOrRegisterCounter("hostinfo.cached_packets.sent", nil),
|
|
|
|
dropped: metrics.GetOrRegisterCounter("hostinfo.cached_packets.dropped", nil),
|
|
|
|
},
|
|
|
|
|
|
|
|
l: c.l,
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2024-10-10 17:00:22 -06:00
|
|
|
if ifce.myVpnNet.Addr().Is4() {
|
|
|
|
maskedAddr := certificate.Networks()[0].Masked()
|
|
|
|
addr := maskedAddr.Addr().As4()
|
|
|
|
mask := net.CIDRMask(maskedAddr.Bits(), maskedAddr.Addr().BitLen())
|
|
|
|
binary.BigEndian.PutUint32(addr[:], binary.BigEndian.Uint32(addr[:])|^binary.BigEndian.Uint32(mask))
|
2024-07-31 09:18:56 -06:00
|
|
|
ifce.myBroadcastAddr = netip.AddrFrom4(addr)
|
|
|
|
}
|
|
|
|
|
2023-08-08 12:26:41 -06:00
|
|
|
ifce.tryPromoteEvery.Store(c.tryPromoteEvery)
|
|
|
|
ifce.reQueryEvery.Store(c.reQueryEvery)
|
|
|
|
ifce.reQueryWait.Store(int64(c.reQueryWait))
|
|
|
|
|
2023-03-31 14:45:05 -06:00
|
|
|
ifce.connectionManager = newConnectionManager(ctx, c.l, ifce, c.checkInterval, c.pendingDeletionInterval, c.punchy)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
return ifce, nil
|
|
|
|
}
|
|
|
|
|
2021-04-16 09:34:28 -06:00
|
|
|
// activate creates the interface on the host. After the interface is created, any
|
|
|
|
// other services that want to bind listeners to its IP may do so successfully. However,
|
|
|
|
// the interface isn't going to process anything until run() is called.
|
|
|
|
func (f *Interface) activate() {
|
2019-11-19 10:00:20 -07:00
|
|
|
// actually turn on tun dev
|
|
|
|
|
2020-02-06 22:17:43 -07:00
|
|
|
addr, err := f.outside.LocalAddr()
|
|
|
|
if err != nil {
|
2021-03-26 08:46:30 -06:00
|
|
|
f.l.WithError(err).Error("Failed to get udp listen address")
|
2020-02-06 22:17:43 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:47:09 -07:00
|
|
|
f.l.WithField("interface", f.inside.Name()).WithField("network", f.inside.Cidr().String()).
|
2020-09-18 08:20:09 -06:00
|
|
|
WithField("build", f.version).WithField("udpAddr", addr).
|
2023-05-04 13:42:45 -06:00
|
|
|
WithField("boringcrypto", boringEnabled()).
|
2019-11-19 10:00:20 -07:00
|
|
|
Info("Nebula interface is active")
|
|
|
|
|
2021-02-25 13:01:14 -07:00
|
|
|
metrics.GetOrRegisterGauge("routines", nil).Update(int64(f.routines))
|
|
|
|
|
2021-03-01 14:57:05 -07:00
|
|
|
// Prepare n tun queues
|
2021-02-25 13:01:14 -07: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 {
|
2021-03-26 08:46:30 -06:00
|
|
|
f.l.Fatal(err)
|
2021-02-25 13:01:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f.readers[i] = reader
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.inside.Activate(); err != nil {
|
2021-11-08 11:36:31 -07:00
|
|
|
f.inside.Close()
|
2021-03-26 08:46:30 -06:00
|
|
|
f.l.Fatal(err)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
2021-04-16 09:34:28 -06:00
|
|
|
}
|
2021-03-01 14:57:05 -07:00
|
|
|
|
2021-04-16 09:34:28 -06:00
|
|
|
func (f *Interface) run() {
|
2021-03-05 19:18:33 -07:00
|
|
|
// Launch n queues to read packets from udp
|
|
|
|
for i := 0; i < f.routines; i++ {
|
|
|
|
go f.listenOut(i)
|
|
|
|
}
|
|
|
|
|
2021-03-01 14:57:05 -07:00
|
|
|
// Launch n queues to read packets from tun dev
|
|
|
|
for i := 0; i < f.routines; i++ {
|
|
|
|
go f.listenIn(f.readers[i], i)
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Interface) listenOut(i int) {
|
2021-02-25 13:01:14 -07:00
|
|
|
runtime.LockOSThread()
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-06-14 09:48:52 -06:00
|
|
|
var li udp.Conn
|
2021-02-25 13:01:14 -07:00
|
|
|
// TODO clean this up with a coherent interface for each outside connection
|
2019-11-19 10:00:20 -07:00
|
|
|
if i > 0 {
|
2021-02-25 13:01:14 -07:00
|
|
|
li = f.writers[i]
|
2019-11-19 10:00:20 -07:00
|
|
|
} else {
|
|
|
|
li = f.outside
|
|
|
|
}
|
2021-11-03 19:54:04 -06:00
|
|
|
|
|
|
|
lhh := f.lightHouse.NewRequestHandler()
|
|
|
|
conntrackCache := firewall.NewConntrackCacheTicker(f.conntrackCacheTimeout)
|
2023-04-07 12:28:37 -06:00
|
|
|
li.ListenOut(readOutsidePackets(f), lhHandleRequest(lhh, f), conntrackCache, i)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2021-02-25 13:01:14 -07:00
|
|
|
func (f *Interface) listenIn(reader io.ReadWriteCloser, i int) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
packet := make([]byte, mtu)
|
|
|
|
out := make([]byte, mtu)
|
2021-11-03 19:54:04 -06:00
|
|
|
fwPacket := &firewall.Packet{}
|
2019-11-19 10:00:20 -07:00
|
|
|
nb := make([]byte, 12, 12)
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
conntrackCache := firewall.NewConntrackCacheTicker(f.conntrackCacheTimeout)
|
2021-03-01 17:52:17 -07:00
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
for {
|
2021-02-25 13:01:14 -07:00
|
|
|
n, err := reader.Read(packet)
|
2019-11-19 10:00:20 -07:00
|
|
|
if err != nil {
|
2022-10-31 11:37:41 -06:00
|
|
|
if errors.Is(err, os.ErrClosed) && f.closed.Load() {
|
2021-11-08 11:36:31 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
f.l.WithError(err).Error("Error while reading outbound packet")
|
2019-11-19 10:00:20 -07:00
|
|
|
// This only seems to happen when something fatal happens to the fd, so exit.
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
f.consumeInsidePacket(packet[:n], fwPacket, nb, out, i, conntrackCache.Get(f.l))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func (f *Interface) RegisterConfigChangeCallbacks(c *config.C) {
|
2019-11-19 10:00:20 -07:00
|
|
|
c.RegisterReloadCallback(f.reloadFirewall)
|
2022-06-27 10:37:54 -06:00
|
|
|
c.RegisterReloadCallback(f.reloadSendRecvError)
|
2023-11-13 11:39:38 -07:00
|
|
|
c.RegisterReloadCallback(f.reloadDisconnectInvalid)
|
2023-08-08 12:26:41 -06:00
|
|
|
c.RegisterReloadCallback(f.reloadMisc)
|
2023-11-13 11:39:38 -07:00
|
|
|
|
2021-02-25 13:01:14 -07:00
|
|
|
for _, udpConn := range f.writers {
|
2021-11-03 19:54:04 -06:00
|
|
|
c.RegisterReloadCallback(udpConn.ReloadConfig)
|
2021-02-25 13:01:14 -07:00
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-11-13 11:39:38 -07:00
|
|
|
func (f *Interface) reloadDisconnectInvalid(c *config.C) {
|
|
|
|
initial := c.InitialLoad()
|
|
|
|
if initial || c.HasChanged("pki.disconnect_invalid") {
|
|
|
|
f.disconnectInvalid.Store(c.GetBool("pki.disconnect_invalid", true))
|
|
|
|
if !initial {
|
|
|
|
f.l.Infof("pki.disconnect_invalid changed to %v", f.disconnectInvalid.Load())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func (f *Interface) reloadFirewall(c *config.C) {
|
2019-11-19 10:00:20 -07:00
|
|
|
//TODO: need to trigger/detect if the certificate changed too
|
|
|
|
if c.HasChanged("firewall") == false {
|
2021-03-26 08:46:30 -06:00
|
|
|
f.l.Debug("No firewall config change detected")
|
2019-11-19 10:00:20 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-14 20:32:40 -06:00
|
|
|
fw, err := NewFirewallFromConfig(f.l, f.pki.GetCertState().Certificate, c)
|
2019-11-19 10:00:20 -07:00
|
|
|
if err != nil {
|
2021-03-26 08:46:30 -06:00
|
|
|
f.l.WithError(err).Error("Error while creating firewall during reload")
|
2019-11-19 10:00:20 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
oldFw := f.firewall
|
2020-07-31 16:53:36 -06: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 {
|
2023-11-28 09:56:47 -07:00
|
|
|
f.l.WithField("firewallHashes", fw.GetRuleHashes()).
|
|
|
|
WithField("oldFirewallHashes", oldFw.GetRuleHashes()).
|
2020-07-31 16:53:36 -06:00
|
|
|
WithField("rulesVersion", fw.rulesVersion).
|
|
|
|
Warn("firewall rulesVersion has overflowed, resetting conntrack")
|
|
|
|
} else {
|
|
|
|
fw.Conntrack = conntrack
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
f.firewall = fw
|
|
|
|
|
|
|
|
oldFw.Destroy()
|
2023-11-28 09:56:47 -07:00
|
|
|
f.l.WithField("firewallHashes", fw.GetRuleHashes()).
|
|
|
|
WithField("oldFirewallHashes", oldFw.GetRuleHashes()).
|
2020-07-31 16:53:36 -06:00
|
|
|
WithField("rulesVersion", fw.rulesVersion).
|
2019-11-19 10:00:20 -07:00
|
|
|
Info("New firewall has been installed")
|
|
|
|
}
|
|
|
|
|
2022-06-27 10:37:54 -06:00
|
|
|
func (f *Interface) reloadSendRecvError(c *config.C) {
|
|
|
|
if c.InitialLoad() || c.HasChanged("listen.send_recv_error") {
|
|
|
|
stringValue := c.GetString("listen.send_recv_error", "always")
|
|
|
|
|
|
|
|
switch stringValue {
|
|
|
|
case "always":
|
|
|
|
f.sendRecvErrorConfig = sendRecvErrorAlways
|
|
|
|
case "never":
|
|
|
|
f.sendRecvErrorConfig = sendRecvErrorNever
|
|
|
|
case "private":
|
|
|
|
f.sendRecvErrorConfig = sendRecvErrorPrivate
|
|
|
|
default:
|
|
|
|
if c.GetBool("listen.send_recv_error", true) {
|
|
|
|
f.sendRecvErrorConfig = sendRecvErrorAlways
|
|
|
|
} else {
|
|
|
|
f.sendRecvErrorConfig = sendRecvErrorNever
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f.l.WithField("sendRecvError", f.sendRecvErrorConfig.String()).
|
|
|
|
Info("Loaded send_recv_error config")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 12:26:41 -06:00
|
|
|
func (f *Interface) reloadMisc(c *config.C) {
|
|
|
|
if c.HasChanged("counters.try_promote") {
|
|
|
|
n := c.GetUint32("counters.try_promote", defaultPromoteEvery)
|
|
|
|
f.tryPromoteEvery.Store(n)
|
|
|
|
f.l.Info("counters.try_promote has changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HasChanged("counters.requery_every_packets") {
|
|
|
|
n := c.GetUint32("counters.requery_every_packets", defaultReQueryEvery)
|
|
|
|
f.reQueryEvery.Store(n)
|
|
|
|
f.l.Info("counters.requery_every_packets has changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HasChanged("timers.requery_wait_duration") {
|
|
|
|
n := c.GetDuration("timers.requery_wait_duration", defaultReQueryWait)
|
|
|
|
f.reQueryWait.Store(int64(n))
|
|
|
|
f.l.Info("timers.requery_wait_duration has changed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 12:14:26 -06:00
|
|
|
func (f *Interface) emitStats(ctx context.Context, i time.Duration) {
|
2019-11-19 10:00:20 -07:00
|
|
|
ticker := time.NewTicker(i)
|
2021-11-02 12:14:26 -06:00
|
|
|
defer ticker.Stop()
|
2021-03-01 17:51:33 -07:00
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
udpStats := udp.NewUDPStatsEmitter(f.writers)
|
2021-03-01 17:51:33 -07:00
|
|
|
|
2023-04-03 19:18:16 -06:00
|
|
|
certExpirationGauge := metrics.GetOrRegisterGauge("certificate.ttl_seconds", nil)
|
|
|
|
|
2021-11-02 12:14:26 -06:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
f.firewall.EmitStats()
|
|
|
|
f.handshakeManager.EmitStats()
|
|
|
|
udpStats()
|
2024-10-10 17:00:22 -06:00
|
|
|
certExpirationGauge.Update(int64(f.pki.GetCertState().Certificate.NotAfter().Sub(time.Now()) / time.Second))
|
2021-11-02 12:14:26 -06:00
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
}
|
2021-11-08 11:36:31 -07:00
|
|
|
|
|
|
|
func (f *Interface) Close() error {
|
2022-10-31 11:37:41 -06:00
|
|
|
f.closed.Store(true)
|
2021-11-08 11:36:31 -07:00
|
|
|
|
2023-07-10 11:43:48 -06:00
|
|
|
for _, u := range f.writers {
|
|
|
|
err := u.Close()
|
|
|
|
if err != nil {
|
|
|
|
f.l.WithError(err).Error("Error while closing udp socket")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 11:36:31 -07:00
|
|
|
// Release the tun device
|
|
|
|
return f.inside.Close()
|
|
|
|
}
|