2022-09-05 09:06:35 +02:00
|
|
|
package routemanager
|
|
|
|
|
2023-04-17 11:15:37 +02:00
|
|
|
import (
|
|
|
|
"context"
|
2024-04-08 18:56:52 +02:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
"net/url"
|
2023-04-17 11:15:37 +02:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
firewall "github.com/netbirdio/netbird/client/firewall/manager"
|
2023-11-02 19:04:33 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal/listener"
|
2023-04-17 11:15:37 +02:00
|
|
|
"github.com/netbirdio/netbird/client/internal/peer"
|
2024-04-23 14:42:53 +02:00
|
|
|
"github.com/netbirdio/netbird/client/internal/routeselector"
|
2023-04-17 11:15:37 +02:00
|
|
|
"github.com/netbirdio/netbird/iface"
|
|
|
|
"github.com/netbirdio/netbird/route"
|
2024-04-12 16:53:11 +02:00
|
|
|
nbnet "github.com/netbirdio/netbird/util/net"
|
2023-04-17 11:15:37 +02:00
|
|
|
"github.com/netbirdio/netbird/version"
|
|
|
|
)
|
2022-09-05 09:06:35 +02:00
|
|
|
|
2024-04-08 18:56:52 +02:00
|
|
|
var defaultv4 = netip.PrefixFrom(netip.IPv4Unspecified(), 0)
|
|
|
|
|
|
|
|
// nolint:unused
|
|
|
|
var defaultv6 = netip.PrefixFrom(netip.IPv6Unspecified(), 0)
|
|
|
|
|
2022-09-05 09:06:35 +02:00
|
|
|
// Manager is a route manager interface
|
|
|
|
type Manager interface {
|
2024-04-08 18:56:52 +02:00
|
|
|
Init() (peer.BeforeAddPeerHookFunc, peer.AfterRemovePeerHookFunc, error)
|
2024-05-06 14:47:49 +02:00
|
|
|
UpdateRoutes(updateSerial uint64, newRoutes []*route.Route) (map[route.ID]*route.Route, route.HAMap, error)
|
|
|
|
TriggerSelection(route.HAMap)
|
2024-04-23 14:42:53 +02:00
|
|
|
GetRouteSelector() *routeselector.RouteSelector
|
2023-11-02 19:04:33 +01:00
|
|
|
SetRouteChangeListener(listener listener.NetworkChangeListener)
|
2023-06-12 14:43:55 +02:00
|
|
|
InitialRouteRange() []string
|
2023-12-08 10:48:21 +01:00
|
|
|
EnableServerRouter(firewall firewall.Manager) error
|
2022-09-05 09:06:35 +02:00
|
|
|
Stop()
|
|
|
|
}
|
2023-04-17 11:15:37 +02:00
|
|
|
|
|
|
|
// DefaultManager is the default instance of a route manager
|
|
|
|
type DefaultManager struct {
|
|
|
|
ctx context.Context
|
|
|
|
stop context.CancelFunc
|
|
|
|
mux sync.Mutex
|
2024-05-06 14:47:49 +02:00
|
|
|
clientNetworks map[route.HAUniqueID]*clientNetwork
|
2024-04-23 14:42:53 +02:00
|
|
|
routeSelector *routeselector.RouteSelector
|
2023-08-12 11:42:36 +02:00
|
|
|
serverRouter serverRouter
|
2023-04-17 11:15:37 +02:00
|
|
|
statusRecorder *peer.Status
|
|
|
|
wgInterface *iface.WGIface
|
|
|
|
pubKey string
|
2023-05-31 18:25:24 +02:00
|
|
|
notifier *notifier
|
2023-04-17 11:15:37 +02:00
|
|
|
}
|
|
|
|
|
2023-05-31 18:25:24 +02:00
|
|
|
func NewManager(ctx context.Context, pubKey string, wgInterface *iface.WGIface, statusRecorder *peer.Status, initialRoutes []*route.Route) *DefaultManager {
|
2023-08-12 11:42:36 +02:00
|
|
|
mCTX, cancel := context.WithCancel(ctx)
|
2023-05-31 18:25:24 +02:00
|
|
|
dm := &DefaultManager{
|
2023-04-17 11:15:37 +02:00
|
|
|
ctx: mCTX,
|
|
|
|
stop: cancel,
|
2024-05-06 14:47:49 +02:00
|
|
|
clientNetworks: make(map[route.HAUniqueID]*clientNetwork),
|
2024-04-23 14:42:53 +02:00
|
|
|
routeSelector: routeselector.NewRouteSelector(),
|
2023-04-17 11:15:37 +02:00
|
|
|
statusRecorder: statusRecorder,
|
|
|
|
wgInterface: wgInterface,
|
|
|
|
pubKey: pubKey,
|
2023-05-31 18:25:24 +02:00
|
|
|
notifier: newNotifier(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
cr := dm.clientRoutes(initialRoutes)
|
|
|
|
dm.notifier.setInitialClientRoutes(cr)
|
2023-04-17 11:15:37 +02:00
|
|
|
}
|
2023-05-31 18:25:24 +02:00
|
|
|
return dm
|
2023-04-17 11:15:37 +02:00
|
|
|
}
|
|
|
|
|
2024-04-08 18:56:52 +02:00
|
|
|
// Init sets up the routing
|
|
|
|
func (m *DefaultManager) Init() (peer.BeforeAddPeerHookFunc, peer.AfterRemovePeerHookFunc, error) {
|
2024-04-12 16:53:11 +02:00
|
|
|
if nbnet.CustomRoutingDisabled() {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2024-04-08 18:56:52 +02:00
|
|
|
if err := cleanupRouting(); err != nil {
|
|
|
|
log.Warnf("Failed cleaning up routing: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mgmtAddress := m.statusRecorder.GetManagementState().URL
|
|
|
|
signalAddress := m.statusRecorder.GetSignalState().URL
|
|
|
|
ips := resolveURLsToIPs([]string{mgmtAddress, signalAddress})
|
|
|
|
|
|
|
|
beforePeerHook, afterPeerHook, err := setupRouting(ips, m.wgInterface)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("setup routing: %w", err)
|
|
|
|
}
|
|
|
|
log.Info("Routing setup complete")
|
|
|
|
return beforePeerHook, afterPeerHook, nil
|
|
|
|
}
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
func (m *DefaultManager) EnableServerRouter(firewall firewall.Manager) error {
|
|
|
|
var err error
|
2024-03-12 19:06:16 +01:00
|
|
|
m.serverRouter, err = newServerRouter(m.ctx, m.wgInterface, firewall, m.statusRecorder)
|
2023-12-08 10:48:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-17 11:15:37 +02:00
|
|
|
// Stop stops the manager watchers and clean firewall rules
|
|
|
|
func (m *DefaultManager) Stop() {
|
|
|
|
m.stop()
|
2023-08-12 11:42:36 +02:00
|
|
|
if m.serverRouter != nil {
|
|
|
|
m.serverRouter.cleanUp()
|
|
|
|
}
|
2024-04-12 16:53:11 +02:00
|
|
|
|
|
|
|
if !nbnet.CustomRoutingDisabled() {
|
|
|
|
if err := cleanupRouting(); err != nil {
|
|
|
|
log.Errorf("Error cleaning up routing: %v", err)
|
|
|
|
} else {
|
|
|
|
log.Info("Routing cleanup complete")
|
|
|
|
}
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
2024-04-12 16:53:11 +02:00
|
|
|
|
2024-04-08 18:56:52 +02:00
|
|
|
m.ctx = nil
|
2023-04-17 11:15:37 +02:00
|
|
|
}
|
|
|
|
|
2024-04-08 18:56:52 +02:00
|
|
|
// UpdateRoutes compares received routes with existing routes and removes, updates or adds them to the client and server maps
|
2024-05-06 14:47:49 +02:00
|
|
|
func (m *DefaultManager) UpdateRoutes(updateSerial uint64, newRoutes []*route.Route) (map[route.ID]*route.Route, route.HAMap, error) {
|
2023-04-17 11:15:37 +02:00
|
|
|
select {
|
|
|
|
case <-m.ctx.Done():
|
|
|
|
log.Infof("not updating routes as context is closed")
|
2024-04-23 14:42:53 +02:00
|
|
|
return nil, nil, m.ctx.Err()
|
2023-04-17 11:15:37 +02:00
|
|
|
default:
|
|
|
|
m.mux.Lock()
|
|
|
|
defer m.mux.Unlock()
|
|
|
|
|
2024-04-23 14:42:53 +02:00
|
|
|
newServerRoutesMap, newClientRoutesIDMap := m.classifyRoutes(newRoutes)
|
2023-04-17 11:15:37 +02:00
|
|
|
|
2024-04-23 14:42:53 +02:00
|
|
|
filteredClientRoutes := m.routeSelector.FilterSelected(newClientRoutesIDMap)
|
|
|
|
m.updateClientNetworks(updateSerial, filteredClientRoutes)
|
|
|
|
m.notifier.onNewRoutes(filteredClientRoutes)
|
2023-08-12 11:42:36 +02:00
|
|
|
|
|
|
|
if m.serverRouter != nil {
|
|
|
|
err := m.serverRouter.updateRoutes(newServerRoutesMap)
|
|
|
|
if err != nil {
|
2024-04-23 14:42:53 +02:00
|
|
|
return nil, nil, fmt.Errorf("update routes: %w", err)
|
2023-08-12 11:42:36 +02:00
|
|
|
}
|
2023-04-17 11:15:37 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 14:42:53 +02:00
|
|
|
return newServerRoutesMap, newClientRoutesIDMap, nil
|
2023-04-17 11:15:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 18:25:24 +02:00
|
|
|
// SetRouteChangeListener set RouteListener for route change notifier
|
2023-11-02 19:04:33 +01:00
|
|
|
func (m *DefaultManager) SetRouteChangeListener(listener listener.NetworkChangeListener) {
|
2023-05-31 18:25:24 +02:00
|
|
|
m.notifier.setListener(listener)
|
|
|
|
}
|
|
|
|
|
2023-06-12 14:43:55 +02:00
|
|
|
// InitialRouteRange return the list of initial routes. It used by mobile systems
|
|
|
|
func (m *DefaultManager) InitialRouteRange() []string {
|
|
|
|
return m.notifier.initialRouteRanges()
|
|
|
|
}
|
|
|
|
|
2024-04-23 14:42:53 +02:00
|
|
|
// GetRouteSelector returns the route selector
|
|
|
|
func (m *DefaultManager) GetRouteSelector() *routeselector.RouteSelector {
|
|
|
|
return m.routeSelector
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetClientRoutes returns the client routes
|
2024-05-06 14:47:49 +02:00
|
|
|
func (m *DefaultManager) GetClientRoutes() map[route.HAUniqueID]*clientNetwork {
|
2024-04-23 14:42:53 +02:00
|
|
|
return m.clientNetworks
|
|
|
|
}
|
|
|
|
|
|
|
|
// TriggerSelection triggers the selection of routes, stopping deselected watchers and starting newly selected ones
|
2024-05-06 14:47:49 +02:00
|
|
|
func (m *DefaultManager) TriggerSelection(networks route.HAMap) {
|
2024-04-23 14:42:53 +02:00
|
|
|
m.mux.Lock()
|
|
|
|
defer m.mux.Unlock()
|
|
|
|
|
|
|
|
networks = m.routeSelector.FilterSelected(networks)
|
|
|
|
m.stopObsoleteClients(networks)
|
|
|
|
|
|
|
|
for id, routes := range networks {
|
|
|
|
if _, found := m.clientNetworks[id]; found {
|
|
|
|
// don't touch existing client network watchers
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
clientNetworkWatcher := newClientNetworkWatcher(m.ctx, m.wgInterface, m.statusRecorder, routes[0].Network)
|
|
|
|
m.clientNetworks[id] = clientNetworkWatcher
|
|
|
|
go clientNetworkWatcher.peersStateAndUpdateWatcher()
|
|
|
|
clientNetworkWatcher.sendUpdateToClientNetworkWatcher(routesUpdate{routes: routes})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stopObsoleteClients stops the client network watcher for the networks that are not in the new list
|
2024-05-06 14:47:49 +02:00
|
|
|
func (m *DefaultManager) stopObsoleteClients(networks route.HAMap) {
|
2023-04-17 11:15:37 +02:00
|
|
|
for id, client := range m.clientNetworks {
|
2024-04-23 14:42:53 +02:00
|
|
|
if _, ok := networks[id]; !ok {
|
|
|
|
log.Debugf("Stopping client network watcher, %s", id)
|
2023-04-17 11:15:37 +02:00
|
|
|
client.stop()
|
|
|
|
delete(m.clientNetworks, id)
|
|
|
|
}
|
|
|
|
}
|
2024-04-23 14:42:53 +02:00
|
|
|
}
|
|
|
|
|
2024-05-06 14:47:49 +02:00
|
|
|
func (m *DefaultManager) updateClientNetworks(updateSerial uint64, networks route.HAMap) {
|
2024-04-23 14:42:53 +02:00
|
|
|
// removing routes that do not exist as per the update from the Management service.
|
|
|
|
m.stopObsoleteClients(networks)
|
2023-04-17 11:15:37 +02:00
|
|
|
|
|
|
|
for id, routes := range networks {
|
|
|
|
clientNetworkWatcher, found := m.clientNetworks[id]
|
|
|
|
if !found {
|
|
|
|
clientNetworkWatcher = newClientNetworkWatcher(m.ctx, m.wgInterface, m.statusRecorder, routes[0].Network)
|
|
|
|
m.clientNetworks[id] = clientNetworkWatcher
|
|
|
|
go clientNetworkWatcher.peersStateAndUpdateWatcher()
|
|
|
|
}
|
|
|
|
update := routesUpdate{
|
|
|
|
updateSerial: updateSerial,
|
|
|
|
routes: routes,
|
|
|
|
}
|
|
|
|
clientNetworkWatcher.sendUpdateToClientNetworkWatcher(update)
|
|
|
|
}
|
|
|
|
}
|
2023-05-31 18:25:24 +02:00
|
|
|
|
2024-05-06 14:47:49 +02:00
|
|
|
func (m *DefaultManager) classifyRoutes(newRoutes []*route.Route) (map[route.ID]*route.Route, route.HAMap) {
|
|
|
|
newClientRoutesIDMap := make(route.HAMap)
|
|
|
|
newServerRoutesMap := make(map[route.ID]*route.Route)
|
|
|
|
ownNetworkIDs := make(map[route.HAUniqueID]bool)
|
2023-05-31 18:25:24 +02:00
|
|
|
|
|
|
|
for _, newRoute := range newRoutes {
|
2024-05-06 14:47:49 +02:00
|
|
|
haID := route.GetHAUniqueID(newRoute)
|
2023-05-31 18:25:24 +02:00
|
|
|
if newRoute.Peer == m.pubKey {
|
2024-05-06 14:47:49 +02:00
|
|
|
ownNetworkIDs[haID] = true
|
2023-05-31 18:25:24 +02:00
|
|
|
// only linux is supported for now
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
log.Warnf("received a route to manage, but agent doesn't support router mode on %s OS", runtime.GOOS)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newServerRoutesMap[newRoute.ID] = newRoute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, newRoute := range newRoutes {
|
2024-05-06 14:47:49 +02:00
|
|
|
haID := route.GetHAUniqueID(newRoute)
|
|
|
|
if !ownNetworkIDs[haID] {
|
2024-04-08 18:56:52 +02:00
|
|
|
if !isPrefixSupported(newRoute.Network) {
|
2023-05-31 18:25:24 +02:00
|
|
|
continue
|
|
|
|
}
|
2024-05-06 14:47:49 +02:00
|
|
|
newClientRoutesIDMap[haID] = append(newClientRoutesIDMap[haID], newRoute)
|
2023-05-31 18:25:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newServerRoutesMap, newClientRoutesIDMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DefaultManager) clientRoutes(initialRoutes []*route.Route) []*route.Route {
|
2024-04-23 14:42:53 +02:00
|
|
|
_, crMap := m.classifyRoutes(initialRoutes)
|
2023-05-31 18:25:24 +02:00
|
|
|
rs := make([]*route.Route, 0)
|
|
|
|
for _, routes := range crMap {
|
|
|
|
rs = append(rs, routes...)
|
|
|
|
}
|
|
|
|
return rs
|
|
|
|
}
|
2024-04-08 18:56:52 +02:00
|
|
|
|
|
|
|
func isPrefixSupported(prefix netip.Prefix) bool {
|
2024-04-12 16:53:11 +02:00
|
|
|
if !nbnet.CustomRoutingDisabled() {
|
|
|
|
switch runtime.GOOS {
|
2024-04-23 19:12:16 +02:00
|
|
|
case "linux", "windows", "darwin", "ios":
|
2024-04-12 16:53:11 +02:00
|
|
|
return true
|
|
|
|
}
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If prefix is too small, lets assume it is a possible default prefix which is not yet supported
|
|
|
|
// we skip this prefix management
|
|
|
|
if prefix.Bits() <= minRangeBits {
|
|
|
|
log.Warnf("This agent version: %s, doesn't support default routes, received %s, skipping this prefix",
|
|
|
|
version.NetbirdVersion(), prefix)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolveURLsToIPs takes a slice of URLs, resolves them to IP addresses and returns a slice of IPs.
|
|
|
|
func resolveURLsToIPs(urls []string) []net.IP {
|
|
|
|
var ips []net.IP
|
|
|
|
for _, rawurl := range urls {
|
|
|
|
u, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to parse url %s: %v", rawurl, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ipAddrs, err := net.LookupIP(u.Hostname())
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to resolve host %s: %v", u.Hostname(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ips = append(ips, ipAddrs...)
|
|
|
|
}
|
|
|
|
return ips
|
|
|
|
}
|