2022-09-05 09:06:35 +02:00
|
|
|
package routemanager
|
|
|
|
|
2023-04-17 11:15:37 +02:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
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"
|
|
|
|
"github.com/netbirdio/netbird/iface"
|
|
|
|
"github.com/netbirdio/netbird/route"
|
|
|
|
"github.com/netbirdio/netbird/version"
|
|
|
|
)
|
2022-09-05 09:06:35 +02:00
|
|
|
|
|
|
|
// Manager is a route manager interface
|
|
|
|
type Manager interface {
|
|
|
|
UpdateRoutes(updateSerial uint64, newRoutes []*route.Route) error
|
2023-11-02 19:04:33 +01:00
|
|
|
SetRouteChangeListener(listener listener.NetworkChangeListener)
|
2023-06-12 14:43:55 +02:00
|
|
|
InitialRouteRange() []string
|
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
|
|
|
|
clientNetworks map[string]*clientNetwork
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// NewManager returns a new route manager
|
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-09-05 21:07:32 +02:00
|
|
|
srvRouter, err := newServerRouter(ctx, wgInterface)
|
2023-08-12 11:42:36 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("server router is not supported: %s", err)
|
|
|
|
}
|
2023-05-31 18:25:24 +02:00
|
|
|
|
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,
|
|
|
|
clientNetworks: make(map[string]*clientNetwork),
|
2023-09-05 21:07:32 +02:00
|
|
|
serverRouter: srvRouter,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
2023-05-31 18:25:24 +02:00
|
|
|
m.ctx = nil
|
2023-04-17 11:15:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateRoutes compares received routes with existing routes and remove, update or add them to the client and server maps
|
|
|
|
func (m *DefaultManager) UpdateRoutes(updateSerial uint64, newRoutes []*route.Route) error {
|
|
|
|
select {
|
|
|
|
case <-m.ctx.Done():
|
|
|
|
log.Infof("not updating routes as context is closed")
|
|
|
|
return m.ctx.Err()
|
|
|
|
default:
|
|
|
|
m.mux.Lock()
|
|
|
|
defer m.mux.Unlock()
|
|
|
|
|
2023-05-31 18:25:24 +02:00
|
|
|
newServerRoutesMap, newClientRoutesIDMap := m.classifiesRoutes(newRoutes)
|
2023-04-17 11:15:37 +02:00
|
|
|
|
|
|
|
m.updateClientNetworks(updateSerial, newClientRoutesIDMap)
|
2023-05-31 18:25:24 +02:00
|
|
|
m.notifier.onNewRoutes(newClientRoutesIDMap)
|
2023-08-12 11:42:36 +02:00
|
|
|
|
|
|
|
if m.serverRouter != nil {
|
|
|
|
err := m.serverRouter.updateRoutes(newServerRoutesMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-17 11:15:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-04-17 11:15:37 +02:00
|
|
|
func (m *DefaultManager) updateClientNetworks(updateSerial uint64, networks map[string][]*route.Route) {
|
|
|
|
// removing routes that do not exist as per the update from the Management service.
|
|
|
|
for id, client := range m.clientNetworks {
|
|
|
|
_, found := networks[id]
|
|
|
|
if !found {
|
|
|
|
log.Debugf("stopping client network watcher, %s", id)
|
|
|
|
client.stop()
|
|
|
|
delete(m.clientNetworks, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func (m *DefaultManager) classifiesRoutes(newRoutes []*route.Route) (map[string]*route.Route, map[string][]*route.Route) {
|
|
|
|
newClientRoutesIDMap := make(map[string][]*route.Route)
|
|
|
|
newServerRoutesMap := make(map[string]*route.Route)
|
|
|
|
ownNetworkIDs := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, newRoute := range newRoutes {
|
|
|
|
networkID := route.GetHAUniqueID(newRoute)
|
|
|
|
if newRoute.Peer == m.pubKey {
|
|
|
|
ownNetworkIDs[networkID] = true
|
|
|
|
// 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 {
|
|
|
|
networkID := route.GetHAUniqueID(newRoute)
|
|
|
|
if !ownNetworkIDs[networkID] {
|
|
|
|
// if prefix is too small, lets assume is a possible default route which is not yet supported
|
|
|
|
// we skip this route management
|
2023-11-24 11:31:22 +01:00
|
|
|
if newRoute.Network.Bits() < minRangeBits {
|
2023-11-01 17:11:16 +01:00
|
|
|
log.Errorf("this agent version: %s, doesn't support default routes, received %s, skipping this route",
|
2023-05-31 18:25:24 +02:00
|
|
|
version.NetbirdVersion(), newRoute.Network)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newClientRoutesIDMap[networkID] = append(newClientRoutesIDMap[networkID], newRoute)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newServerRoutesMap, newClientRoutesIDMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DefaultManager) clientRoutes(initialRoutes []*route.Route) []*route.Route {
|
|
|
|
_, crMap := m.classifiesRoutes(initialRoutes)
|
|
|
|
rs := make([]*route.Route, 0)
|
|
|
|
for _, routes := range crMap {
|
|
|
|
rs = append(rs, routes...)
|
|
|
|
}
|
|
|
|
return rs
|
|
|
|
}
|