2023-05-21 10:37:59 -06:00
|
|
|
package db
|
2021-04-28 08:55:29 -06:00
|
|
|
|
|
|
|
import (
|
2022-11-25 08:29:45 -07:00
|
|
|
"errors"
|
2024-04-10 06:49:34 -06:00
|
|
|
"fmt"
|
2022-09-01 16:06:19 -06:00
|
|
|
"net/netip"
|
2022-11-23 11:50:30 -07:00
|
|
|
|
2023-05-21 10:37:59 -06:00
|
|
|
"github.com/juanfont/headscale/hscontrol/policy"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2022-11-24 15:41:11 -07:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-11-23 11:50:30 -07:00
|
|
|
"gorm.io/gorm"
|
2021-04-28 08:55:29 -06:00
|
|
|
)
|
|
|
|
|
2023-05-21 10:37:59 -06:00
|
|
|
var ErrRouteIsNotAvailable = errors.New("route is not available")
|
2021-08-21 07:49:46 -06:00
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
func GetRoutes(tx *gorm.DB) (types.Routes, error) {
|
2023-05-21 10:37:59 -06:00
|
|
|
var routes types.Routes
|
2024-02-08 09:28:19 -07:00
|
|
|
err := tx.
|
2023-12-09 10:09:24 -07:00
|
|
|
Preload("Node").
|
|
|
|
Preload("Node.User").
|
|
|
|
Find(&routes).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
func getAdvertisedAndEnabledRoutes(tx *gorm.DB) (types.Routes, error) {
|
2023-12-09 10:09:24 -07:00
|
|
|
var routes types.Routes
|
2024-02-08 09:28:19 -07:00
|
|
|
err := tx.
|
2023-12-09 10:09:24 -07:00
|
|
|
Preload("Node").
|
|
|
|
Preload("Node.User").
|
|
|
|
Where("advertised = ? AND enabled = ?", true, true).
|
|
|
|
Find(&routes).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
func getRoutesByPrefix(tx *gorm.DB, pref netip.Prefix) (types.Routes, error) {
|
2023-12-09 10:09:24 -07:00
|
|
|
var routes types.Routes
|
2024-02-08 09:28:19 -07:00
|
|
|
err := tx.
|
2023-12-09 10:09:24 -07:00
|
|
|
Preload("Node").
|
|
|
|
Preload("Node.User").
|
|
|
|
Where("prefix = ?", types.IPPrefix(pref)).
|
|
|
|
Find(&routes).Error
|
2023-05-21 10:37:59 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-08-21 07:49:46 -06:00
|
|
|
}
|
2022-11-25 08:29:45 -07:00
|
|
|
|
2023-05-21 10:37:59 -06:00
|
|
|
return routes, nil
|
2021-08-21 07:49:46 -06:00
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
func GetNodeAdvertisedRoutes(tx *gorm.DB, node *types.Node) (types.Routes, error) {
|
2023-05-21 10:37:59 -06:00
|
|
|
var routes types.Routes
|
2024-02-08 09:28:19 -07:00
|
|
|
err := tx.
|
2023-09-24 05:42:05 -06:00
|
|
|
Preload("Node").
|
2023-12-09 10:09:24 -07:00
|
|
|
Preload("Node.User").
|
2023-09-24 05:42:05 -06:00
|
|
|
Where("node_id = ? AND advertised = true", node.ID).
|
2023-05-21 10:37:59 -06:00
|
|
|
Find(&routes).Error
|
2022-11-25 10:50:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
func (hsdb *HSDatabase) GetNodeRoutes(node *types.Node) (types.Routes, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (types.Routes, error) {
|
|
|
|
return GetNodeRoutes(rx, node)
|
|
|
|
})
|
2023-07-17 05:35:05 -06:00
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
func GetNodeRoutes(tx *gorm.DB, node *types.Node) (types.Routes, error) {
|
2023-05-21 10:37:59 -06:00
|
|
|
var routes types.Routes
|
2024-02-08 09:28:19 -07:00
|
|
|
err := tx.
|
2023-09-24 05:42:05 -06:00
|
|
|
Preload("Node").
|
2023-12-09 10:09:24 -07:00
|
|
|
Preload("Node.User").
|
2023-09-24 05:42:05 -06:00
|
|
|
Where("node_id = ?", node.ID).
|
2022-11-25 10:50:12 -07:00
|
|
|
Find(&routes).Error
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
func GetRoute(tx *gorm.DB, id uint64) (*types.Route, error) {
|
2023-05-21 10:37:59 -06:00
|
|
|
var route types.Route
|
2024-02-08 09:28:19 -07:00
|
|
|
err := tx.
|
2023-12-09 10:09:24 -07:00
|
|
|
Preload("Node").
|
|
|
|
Preload("Node.User").
|
|
|
|
First(&route, id).Error
|
2022-11-25 10:50:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &route, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
func EnableRoute(tx *gorm.DB, id uint64) (*types.StateUpdate, error) {
|
|
|
|
route, err := GetRoute(tx, id)
|
2022-11-25 10:50:12 -07:00
|
|
|
if err != nil {
|
2024-02-08 09:28:19 -07:00
|
|
|
return nil, err
|
2022-11-25 10:50:12 -07:00
|
|
|
}
|
|
|
|
|
2023-01-22 15:39:42 -07:00
|
|
|
// Tailscale requires both IPv4 and IPv6 exit routes to
|
|
|
|
// be enabled at the same time, as per
|
|
|
|
// https://github.com/juanfont/headscale/issues/804#issuecomment-1399314002
|
2023-05-21 10:37:59 -06:00
|
|
|
if route.IsExitRoute() {
|
2024-02-08 09:28:19 -07:00
|
|
|
return enableRoutes(
|
|
|
|
tx,
|
2023-09-24 05:42:05 -06:00
|
|
|
&route.Node,
|
2023-05-21 10:37:59 -06:00
|
|
|
types.ExitRouteV4.String(),
|
|
|
|
types.ExitRouteV6.String(),
|
|
|
|
)
|
2023-01-22 15:39:42 -07:00
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
return enableRoutes(tx, &route.Node, netip.Prefix(route.Prefix).String())
|
2022-11-25 10:50:12 -07:00
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
func DisableRoute(tx *gorm.DB,
|
|
|
|
id uint64,
|
2024-02-23 02:59:24 -07:00
|
|
|
isConnected types.NodeConnectedMap,
|
|
|
|
) ([]types.NodeID, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
route, err := GetRoute(tx, id)
|
2022-11-25 10:50:12 -07:00
|
|
|
if err != nil {
|
2024-02-08 09:28:19 -07:00
|
|
|
return nil, err
|
2022-11-25 10:50:12 -07:00
|
|
|
}
|
|
|
|
|
2023-12-09 10:09:24 -07:00
|
|
|
var routes types.Routes
|
|
|
|
node := route.Node
|
|
|
|
|
2023-05-07 04:17:16 -06:00
|
|
|
// Tailscale requires both IPv4 and IPv6 exit routes to
|
|
|
|
// be enabled at the same time, as per
|
|
|
|
// https://github.com/juanfont/headscale/issues/804#issuecomment-1399314002
|
2024-02-23 02:59:24 -07:00
|
|
|
var update []types.NodeID
|
2023-05-21 10:37:59 -06:00
|
|
|
if !route.IsExitRoute() {
|
2024-02-23 02:59:24 -07:00
|
|
|
route.Enabled = false
|
|
|
|
err = tx.Save(route).Error
|
2023-12-09 10:09:24 -07:00
|
|
|
if err != nil {
|
2024-02-08 09:28:19 -07:00
|
|
|
return nil, err
|
2023-12-09 10:09:24 -07:00
|
|
|
}
|
|
|
|
|
2024-02-23 02:59:24 -07:00
|
|
|
update, err = failoverRouteTx(tx, isConnected, route)
|
2023-05-07 04:17:16 -06:00
|
|
|
if err != nil {
|
2024-02-08 09:28:19 -07:00
|
|
|
return nil, err
|
2023-05-07 04:17:16 -06:00
|
|
|
}
|
2023-12-09 10:09:24 -07:00
|
|
|
} else {
|
2024-02-08 09:28:19 -07:00
|
|
|
routes, err = GetNodeRoutes(tx, &node)
|
2023-12-09 10:09:24 -07:00
|
|
|
if err != nil {
|
2024-02-08 09:28:19 -07:00
|
|
|
return nil, err
|
2023-12-09 10:09:24 -07:00
|
|
|
}
|
2023-05-07 04:17:16 -06:00
|
|
|
|
2023-12-09 10:09:24 -07:00
|
|
|
for i := range routes {
|
|
|
|
if routes[i].IsExitRoute() {
|
|
|
|
routes[i].Enabled = false
|
|
|
|
routes[i].IsPrimary = false
|
2024-02-23 02:59:24 -07:00
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
err = tx.Save(&routes[i]).Error
|
2023-12-09 10:09:24 -07:00
|
|
|
if err != nil {
|
2024-02-08 09:28:19 -07:00
|
|
|
return nil, err
|
2023-12-09 10:09:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-07 04:17:16 -06:00
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
// If update is empty, it means that one was not created
|
|
|
|
// by failover (as a failover was not necessary), create
|
|
|
|
// one and return to the caller.
|
|
|
|
if update == nil {
|
2024-02-23 02:59:24 -07:00
|
|
|
update = []types.NodeID{node.ID}
|
2023-05-07 04:17:16 -06:00
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
return update, nil
|
2022-11-25 10:50:12 -07:00
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
func (hsdb *HSDatabase) DeleteRoute(
|
|
|
|
id uint64,
|
2024-02-23 02:59:24 -07:00
|
|
|
isConnected types.NodeConnectedMap,
|
|
|
|
) ([]types.NodeID, error) {
|
|
|
|
return Write(hsdb.DB, func(tx *gorm.DB) ([]types.NodeID, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
return DeleteRoute(tx, id, isConnected)
|
|
|
|
})
|
|
|
|
}
|
2023-07-17 05:35:05 -06:00
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
func DeleteRoute(
|
|
|
|
tx *gorm.DB,
|
|
|
|
id uint64,
|
2024-02-23 02:59:24 -07:00
|
|
|
isConnected types.NodeConnectedMap,
|
|
|
|
) ([]types.NodeID, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
route, err := GetRoute(tx, id)
|
2023-03-06 01:05:40 -07:00
|
|
|
if err != nil {
|
2024-02-08 09:28:19 -07:00
|
|
|
return nil, err
|
2023-03-06 01:05:40 -07:00
|
|
|
}
|
|
|
|
|
2023-12-09 10:09:24 -07:00
|
|
|
var routes types.Routes
|
|
|
|
node := route.Node
|
|
|
|
|
2023-05-07 04:17:16 -06:00
|
|
|
// Tailscale requires both IPv4 and IPv6 exit routes to
|
|
|
|
// be enabled at the same time, as per
|
|
|
|
// https://github.com/juanfont/headscale/issues/804#issuecomment-1399314002
|
2024-02-23 02:59:24 -07:00
|
|
|
var update []types.NodeID
|
2023-05-21 10:37:59 -06:00
|
|
|
if !route.IsExitRoute() {
|
2024-02-23 02:59:24 -07:00
|
|
|
update, err = failoverRouteTx(tx, isConnected, route)
|
2023-12-09 10:09:24 -07:00
|
|
|
if err != nil {
|
2024-02-08 09:28:19 -07:00
|
|
|
return nil, nil
|
2023-12-09 10:09:24 -07:00
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
if err := tx.Unscoped().Delete(&route).Error; err != nil {
|
|
|
|
return nil, err
|
2023-05-07 04:17:16 -06:00
|
|
|
}
|
2023-12-09 10:09:24 -07:00
|
|
|
} else {
|
2024-02-23 02:59:24 -07:00
|
|
|
routes, err = GetNodeRoutes(tx, &node)
|
2023-12-09 10:09:24 -07:00
|
|
|
if err != nil {
|
2024-02-08 09:28:19 -07:00
|
|
|
return nil, err
|
2023-12-09 10:09:24 -07:00
|
|
|
}
|
2023-05-07 04:17:16 -06:00
|
|
|
|
2023-12-09 10:09:24 -07:00
|
|
|
routesToDelete := types.Routes{}
|
|
|
|
for _, r := range routes {
|
|
|
|
if r.IsExitRoute() {
|
|
|
|
routesToDelete = append(routesToDelete, r)
|
|
|
|
}
|
|
|
|
}
|
2023-05-07 04:17:16 -06:00
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
if err := tx.Unscoped().Delete(&routesToDelete).Error; err != nil {
|
|
|
|
return nil, err
|
2023-12-09 10:09:24 -07:00
|
|
|
}
|
2023-05-07 04:17:16 -06:00
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
// If update is empty, it means that one was not created
|
|
|
|
// by failover (as a failover was not necessary), create
|
|
|
|
// one and return to the caller.
|
2023-12-09 10:09:24 -07:00
|
|
|
if routes == nil {
|
2024-02-08 09:28:19 -07:00
|
|
|
routes, err = GetNodeRoutes(tx, &node)
|
2023-12-09 10:09:24 -07:00
|
|
|
if err != nil {
|
2024-02-08 09:28:19 -07:00
|
|
|
return nil, err
|
2023-05-07 04:17:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-09 10:09:24 -07:00
|
|
|
node.Routes = routes
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
if update == nil {
|
2024-02-23 02:59:24 -07:00
|
|
|
update = []types.NodeID{node.ID}
|
2023-03-06 01:05:40 -07:00
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
return update, nil
|
2023-03-06 01:05:40 -07:00
|
|
|
}
|
|
|
|
|
2024-02-23 02:59:24 -07:00
|
|
|
func deleteNodeRoutes(tx *gorm.DB, node *types.Node, isConnected types.NodeConnectedMap) ([]types.NodeID, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
routes, err := GetNodeRoutes(tx, node)
|
2023-03-06 01:05:40 -07:00
|
|
|
if err != nil {
|
2024-04-10 06:49:34 -06:00
|
|
|
return nil, fmt.Errorf("getting node routes: %w", err)
|
2023-03-06 01:05:40 -07:00
|
|
|
}
|
|
|
|
|
2024-02-23 02:59:24 -07:00
|
|
|
var changed []types.NodeID
|
2023-03-06 01:05:40 -07:00
|
|
|
for i := range routes {
|
2024-02-08 09:28:19 -07:00
|
|
|
if err := tx.Unscoped().Delete(&routes[i]).Error; err != nil {
|
2024-04-10 06:49:34 -06:00
|
|
|
return nil, fmt.Errorf("deleting route(%d): %w", &routes[i].ID, err)
|
2023-03-06 01:05:40 -07:00
|
|
|
}
|
2023-12-09 10:09:24 -07:00
|
|
|
|
|
|
|
// TODO(kradalby): This is a bit too aggressive, we could probably
|
|
|
|
// figure out which routes needs to be failed over rather than all.
|
2024-02-23 02:59:24 -07:00
|
|
|
chn, err := failoverRouteTx(tx, isConnected, &routes[i])
|
|
|
|
if err != nil {
|
2024-04-10 06:49:34 -06:00
|
|
|
return changed, fmt.Errorf("failing over route after delete: %w", err)
|
2024-02-23 02:59:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if chn != nil {
|
|
|
|
changed = append(changed, chn...)
|
|
|
|
}
|
2023-03-06 01:05:40 -07:00
|
|
|
}
|
|
|
|
|
2024-02-23 02:59:24 -07:00
|
|
|
return changed, nil
|
2023-03-06 01:05:40 -07:00
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
// isUniquePrefix returns if there is another node providing the same route already.
|
2024-02-08 09:28:19 -07:00
|
|
|
func isUniquePrefix(tx *gorm.DB, route types.Route) bool {
|
2022-11-24 09:00:40 -07:00
|
|
|
var count int64
|
2024-02-08 09:28:19 -07:00
|
|
|
tx.Model(&types.Route{}).
|
2023-09-24 05:42:05 -06:00
|
|
|
Where("prefix = ? AND node_id != ? AND advertised = ? AND enabled = ?",
|
2022-11-24 09:00:40 -07:00
|
|
|
route.Prefix,
|
2023-09-24 05:42:05 -06:00
|
|
|
route.NodeID,
|
2022-11-24 09:00:40 -07:00
|
|
|
true, true).Count(&count)
|
2022-11-25 08:29:45 -07:00
|
|
|
|
2022-11-24 09:00:40 -07:00
|
|
|
return count == 0
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
func getPrimaryRoute(tx *gorm.DB, prefix netip.Prefix) (*types.Route, error) {
|
2023-05-21 10:37:59 -06:00
|
|
|
var route types.Route
|
2024-02-08 09:28:19 -07:00
|
|
|
err := tx.
|
2023-09-24 05:42:05 -06:00
|
|
|
Preload("Node").
|
2023-05-21 10:37:59 -06:00
|
|
|
Where("prefix = ? AND advertised = ? AND enabled = ? AND is_primary = ?", types.IPPrefix(prefix), true, true, true).
|
2022-11-24 15:41:11 -07:00
|
|
|
First(&route).Error
|
2022-11-25 08:29:45 -07:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 15:41:11 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-25 08:29:45 -07:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 15:41:11 -07:00
|
|
|
return nil, gorm.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return &route, nil
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
func (hsdb *HSDatabase) GetNodePrimaryRoutes(node *types.Node) (types.Routes, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (types.Routes, error) {
|
|
|
|
return GetNodePrimaryRoutes(rx, node)
|
|
|
|
})
|
|
|
|
}
|
2023-07-17 05:35:05 -06:00
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
// getNodePrimaryRoutes returns the routes that are enabled and marked as primary (for subnet failover)
|
|
|
|
// Exit nodes are not considered for this, as they are never marked as Primary.
|
|
|
|
func GetNodePrimaryRoutes(tx *gorm.DB, node *types.Node) (types.Routes, error) {
|
2023-05-21 10:37:59 -06:00
|
|
|
var routes types.Routes
|
2024-02-08 09:28:19 -07:00
|
|
|
err := tx.
|
2023-09-24 05:42:05 -06:00
|
|
|
Preload("Node").
|
|
|
|
Where("node_id = ? AND advertised = ? AND enabled = ? AND is_primary = ?", node.ID, true, true, true).
|
2022-11-23 11:50:30 -07:00
|
|
|
Find(&routes).Error
|
2021-08-21 07:49:46 -06:00
|
|
|
if err != nil {
|
2022-11-23 11:50:30 -07:00
|
|
|
return nil, err
|
2021-08-21 07:49:46 -06:00
|
|
|
}
|
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
return routes, nil
|
|
|
|
}
|
2021-08-21 07:49:46 -06:00
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
func (hsdb *HSDatabase) SaveNodeRoutes(node *types.Node) (bool, error) {
|
|
|
|
return Write(hsdb.DB, func(tx *gorm.DB) (bool, error) {
|
|
|
|
return SaveNodeRoutes(tx, node)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
// SaveNodeRoutes takes a node and updates the database with
|
2023-08-09 14:20:05 -06:00
|
|
|
// the new routes.
|
2024-01-16 08:04:03 -07:00
|
|
|
// It returns a bool whether an update should be sent as the
|
2023-12-09 10:09:24 -07:00
|
|
|
// saved route impacts nodes.
|
2024-02-08 09:28:19 -07:00
|
|
|
func SaveNodeRoutes(tx *gorm.DB, node *types.Node) (bool, error) {
|
2023-12-09 10:09:24 -07:00
|
|
|
sendUpdate := false
|
|
|
|
|
2023-05-21 10:37:59 -06:00
|
|
|
currentRoutes := types.Routes{}
|
2024-02-08 09:28:19 -07:00
|
|
|
err := tx.Where("node_id = ?", node.ID).Find(¤tRoutes).Error
|
2021-08-21 07:49:46 -06:00
|
|
|
if err != nil {
|
2023-12-09 10:09:24 -07:00
|
|
|
return sendUpdate, err
|
2021-08-21 07:49:46 -06:00
|
|
|
}
|
2021-04-28 08:55:29 -06:00
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
advertisedRoutes := map[netip.Prefix]bool{}
|
2023-11-21 10:20:06 -07:00
|
|
|
for _, prefix := range node.Hostinfo.RoutableIPs {
|
2022-11-23 11:50:30 -07:00
|
|
|
advertisedRoutes[prefix] = false
|
2021-08-21 07:49:46 -06:00
|
|
|
}
|
|
|
|
|
2023-09-11 10:45:46 -06:00
|
|
|
log.Trace().
|
2023-09-24 05:42:05 -06:00
|
|
|
Str("node", node.Hostname).
|
2023-09-11 10:45:46 -06:00
|
|
|
Interface("advertisedRoutes", advertisedRoutes).
|
|
|
|
Interface("currentRoutes", currentRoutes).
|
|
|
|
Msg("updating routes")
|
|
|
|
|
2022-11-25 08:29:45 -07:00
|
|
|
for pos, route := range currentRoutes {
|
2022-11-23 11:50:30 -07:00
|
|
|
if _, ok := advertisedRoutes[netip.Prefix(route.Prefix)]; ok {
|
|
|
|
if !route.Advertised {
|
2022-11-25 08:29:45 -07:00
|
|
|
currentRoutes[pos].Advertised = true
|
2024-02-08 09:28:19 -07:00
|
|
|
err := tx.Save(¤tRoutes[pos]).Error
|
2022-11-23 11:50:30 -07:00
|
|
|
if err != nil {
|
2023-12-09 10:09:24 -07:00
|
|
|
return sendUpdate, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a route that is newly "saved" is already
|
|
|
|
// enabled, set sendUpdate to true as it is now
|
|
|
|
// available.
|
|
|
|
if route.Enabled {
|
|
|
|
sendUpdate = true
|
2022-11-23 11:50:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
advertisedRoutes[netip.Prefix(route.Prefix)] = true
|
2022-11-25 08:29:45 -07:00
|
|
|
} else if route.Advertised {
|
|
|
|
currentRoutes[pos].Advertised = false
|
|
|
|
currentRoutes[pos].Enabled = false
|
2024-02-08 09:28:19 -07:00
|
|
|
err := tx.Save(¤tRoutes[pos]).Error
|
2022-11-25 08:29:45 -07:00
|
|
|
if err != nil {
|
2023-12-09 10:09:24 -07:00
|
|
|
return sendUpdate, err
|
2021-04-28 08:55:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 07:49:46 -06:00
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
for prefix, exists := range advertisedRoutes {
|
|
|
|
if !exists {
|
2023-05-21 10:37:59 -06:00
|
|
|
route := types.Route{
|
2024-02-23 02:59:24 -07:00
|
|
|
NodeID: node.ID.Uint64(),
|
2023-05-21 10:37:59 -06:00
|
|
|
Prefix: types.IPPrefix(prefix),
|
2022-11-23 11:50:30 -07:00
|
|
|
Advertised: true,
|
|
|
|
Enabled: false,
|
|
|
|
}
|
2024-02-08 09:28:19 -07:00
|
|
|
err := tx.Create(&route).Error
|
2022-11-23 11:50:30 -07:00
|
|
|
if err != nil {
|
2023-12-09 10:09:24 -07:00
|
|
|
return sendUpdate, err
|
2022-11-23 11:50:30 -07:00
|
|
|
}
|
|
|
|
}
|
2022-05-30 07:31:06 -06:00
|
|
|
}
|
2021-08-21 07:49:46 -06:00
|
|
|
|
2023-12-09 10:09:24 -07:00
|
|
|
return sendUpdate, nil
|
2021-08-21 07:49:46 -06:00
|
|
|
}
|
2022-11-24 15:41:11 -07:00
|
|
|
|
2024-02-23 02:59:24 -07:00
|
|
|
// FailoverRouteIfAvailable takes a node and checks if the node's route
|
2023-12-09 10:09:24 -07:00
|
|
|
// currently have a functioning host that exposes the network.
|
2024-02-23 02:59:24 -07:00
|
|
|
// If it does not, it is failed over to another suitable route if there
|
|
|
|
// is one.
|
|
|
|
func FailoverRouteIfAvailable(
|
2024-02-08 09:28:19 -07:00
|
|
|
tx *gorm.DB,
|
2024-02-23 02:59:24 -07:00
|
|
|
isConnected types.NodeConnectedMap,
|
2024-02-08 09:28:19 -07:00
|
|
|
node *types.Node,
|
|
|
|
) (*types.StateUpdate, error) {
|
|
|
|
nodeRoutes, err := GetNodeRoutes(tx, node)
|
2023-12-09 10:09:24 -07:00
|
|
|
if err != nil {
|
2024-02-08 09:28:19 -07:00
|
|
|
return nil, nil
|
2022-11-24 15:41:11 -07:00
|
|
|
}
|
|
|
|
|
2024-02-23 02:59:24 -07:00
|
|
|
var changedNodes []types.NodeID
|
2023-12-09 10:09:24 -07:00
|
|
|
for _, nodeRoute := range nodeRoutes {
|
2024-02-08 09:28:19 -07:00
|
|
|
routes, err := getRoutesByPrefix(tx, netip.Prefix(nodeRoute.Prefix))
|
2023-12-09 10:09:24 -07:00
|
|
|
if err != nil {
|
2024-04-10 06:49:34 -06:00
|
|
|
return nil, fmt.Errorf("getting routes by prefix: %w", err)
|
2022-11-24 15:41:11 -07:00
|
|
|
}
|
|
|
|
|
2023-12-09 10:09:24 -07:00
|
|
|
for _, route := range routes {
|
|
|
|
if route.IsPrimary {
|
|
|
|
// if we have a primary route, and the node is connected
|
|
|
|
// nothing needs to be done.
|
2024-02-23 02:59:24 -07:00
|
|
|
if isConnected[route.Node.ID] {
|
|
|
|
return nil, nil
|
2023-12-09 10:09:24 -07:00
|
|
|
}
|
2022-11-24 15:41:11 -07:00
|
|
|
|
2023-12-09 10:09:24 -07:00
|
|
|
// if not, we need to failover the route
|
2024-04-10 06:49:34 -06:00
|
|
|
failover := failoverRoute(isConnected, &route, routes)
|
|
|
|
if failover != nil {
|
|
|
|
failover.save(tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("saving failover routes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
changedNodes = append(changedNodes, failover.old.Node.ID, failover.new.Node.ID)
|
2024-02-08 09:28:19 -07:00
|
|
|
}
|
|
|
|
}
|
2023-12-09 10:09:24 -07:00
|
|
|
}
|
|
|
|
}
|
2022-11-25 08:29:45 -07:00
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
if len(changedNodes) != 0 {
|
|
|
|
return &types.StateUpdate{
|
2023-12-09 10:09:24 -07:00
|
|
|
Type: types.StatePeerChanged,
|
2024-02-08 09:28:19 -07:00
|
|
|
ChangeNodes: changedNodes,
|
2024-02-23 02:59:24 -07:00
|
|
|
Message: "called from db.FailoverRouteIfAvailable",
|
2024-02-08 09:28:19 -07:00
|
|
|
}, nil
|
2023-12-09 10:09:24 -07:00
|
|
|
}
|
2022-11-24 15:41:11 -07:00
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
return nil, nil
|
2023-12-09 10:09:24 -07:00
|
|
|
}
|
2022-11-24 15:41:11 -07:00
|
|
|
|
2024-02-23 02:59:24 -07:00
|
|
|
// failoverRouteTx takes a route that is no longer available,
|
2023-12-09 10:09:24 -07:00
|
|
|
// this can be either from:
|
|
|
|
// - being disabled
|
|
|
|
// - being deleted
|
|
|
|
// - host going offline
|
|
|
|
//
|
|
|
|
// and tries to find a new route to take over its place.
|
|
|
|
// If the given route was not primary, it returns early.
|
2024-02-23 02:59:24 -07:00
|
|
|
func failoverRouteTx(
|
2024-02-08 09:28:19 -07:00
|
|
|
tx *gorm.DB,
|
2024-02-23 02:59:24 -07:00
|
|
|
isConnected types.NodeConnectedMap,
|
2024-02-08 09:28:19 -07:00
|
|
|
r *types.Route,
|
2024-02-23 02:59:24 -07:00
|
|
|
) ([]types.NodeID, error) {
|
2023-12-09 10:09:24 -07:00
|
|
|
if r == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
// This route is not a primary route, and it is not
|
2023-12-09 10:09:24 -07:00
|
|
|
// being served to nodes.
|
|
|
|
if !r.IsPrimary {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not have to failover exit nodes
|
|
|
|
if r.IsExitRoute() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
routes, err := getRoutesByPrefix(tx, netip.Prefix(r.Prefix))
|
2023-12-09 10:09:24 -07:00
|
|
|
if err != nil {
|
2024-04-10 06:49:34 -06:00
|
|
|
return nil, fmt.Errorf("getting routes by prefix: %w", err)
|
2023-12-09 10:09:24 -07:00
|
|
|
}
|
|
|
|
|
2024-02-23 02:59:24 -07:00
|
|
|
fo := failoverRoute(isConnected, r, routes)
|
|
|
|
if fo == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-04-10 06:49:34 -06:00
|
|
|
err = fo.save(tx)
|
2024-02-23 02:59:24 -07:00
|
|
|
if err != nil {
|
2024-04-10 06:49:34 -06:00
|
|
|
return nil, fmt.Errorf("saving failover route: %w", err)
|
2024-02-23 02:59:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace().
|
|
|
|
Str("hostname", fo.new.Node.Hostname).
|
|
|
|
Msgf("set primary to new route, was: id(%d), host(%s), now: id(%d), host(%s)", fo.old.ID, fo.old.Node.Hostname, fo.new.ID, fo.new.Node.Hostname)
|
|
|
|
|
|
|
|
// Return a list of the machinekeys of the changed nodes.
|
|
|
|
return []types.NodeID{fo.old.Node.ID, fo.new.Node.ID}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type failover struct {
|
|
|
|
old *types.Route
|
|
|
|
new *types.Route
|
|
|
|
}
|
|
|
|
|
2024-04-10 06:49:34 -06:00
|
|
|
func (f *failover) save(tx *gorm.DB) error {
|
|
|
|
err := tx.Save(f.old).Error
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("saving old primary: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Save(f.new).Error
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("saving new primary: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-23 02:59:24 -07:00
|
|
|
func failoverRoute(
|
|
|
|
isConnected types.NodeConnectedMap,
|
|
|
|
routeToReplace *types.Route,
|
|
|
|
altRoutes types.Routes,
|
|
|
|
|
|
|
|
) *failover {
|
|
|
|
if routeToReplace == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This route is not a primary route, and it is not
|
|
|
|
// being served to nodes.
|
|
|
|
if !routeToReplace.IsPrimary {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not have to failover exit nodes
|
|
|
|
if routeToReplace.IsExitRoute() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-09 10:09:24 -07:00
|
|
|
var newPrimary *types.Route
|
|
|
|
|
|
|
|
// Find a new suitable route
|
2024-02-23 02:59:24 -07:00
|
|
|
for idx, route := range altRoutes {
|
|
|
|
if routeToReplace.ID == route.ID {
|
2023-12-09 10:09:24 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-03 07:30:15 -07:00
|
|
|
if !route.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-23 02:59:24 -07:00
|
|
|
if isConnected != nil && isConnected[route.Node.ID] {
|
|
|
|
newPrimary = &altRoutes[idx]
|
2023-12-09 10:09:24 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a new route was not found/available,
|
2024-02-08 09:28:19 -07:00
|
|
|
// return without an error.
|
2023-12-09 10:09:24 -07:00
|
|
|
// We do not want to update the database as
|
|
|
|
// the one currently marked as primary is the
|
|
|
|
// best we got.
|
|
|
|
if newPrimary == nil {
|
2024-02-23 02:59:24 -07:00
|
|
|
return nil
|
2023-12-09 10:09:24 -07:00
|
|
|
}
|
|
|
|
|
2024-02-23 02:59:24 -07:00
|
|
|
routeToReplace.IsPrimary = false
|
2023-12-09 10:09:24 -07:00
|
|
|
newPrimary.IsPrimary = true
|
|
|
|
|
2024-02-23 02:59:24 -07:00
|
|
|
return &failover{
|
|
|
|
old: routeToReplace,
|
|
|
|
new: newPrimary,
|
2023-12-09 10:09:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 10:37:59 -06:00
|
|
|
func (hsdb *HSDatabase) EnableAutoApprovedRoutes(
|
|
|
|
aclPolicy *policy.ACLPolicy,
|
2023-09-24 05:42:05 -06:00
|
|
|
node *types.Node,
|
2024-02-23 02:59:24 -07:00
|
|
|
) error {
|
|
|
|
return hsdb.Write(func(tx *gorm.DB) error {
|
2024-02-08 09:28:19 -07:00
|
|
|
return EnableAutoApprovedRoutes(tx, aclPolicy, node)
|
|
|
|
})
|
|
|
|
}
|
2023-07-17 05:35:05 -06:00
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
// EnableAutoApprovedRoutes enables any routes advertised by a node that match the ACL autoApprovers policy.
|
|
|
|
func EnableAutoApprovedRoutes(
|
|
|
|
tx *gorm.DB,
|
|
|
|
aclPolicy *policy.ACLPolicy,
|
|
|
|
node *types.Node,
|
2024-02-23 02:59:24 -07:00
|
|
|
) error {
|
2023-09-24 05:42:05 -06:00
|
|
|
if len(node.IPAddresses) == 0 {
|
2024-02-23 02:59:24 -07:00
|
|
|
return nil // This node has no IPAddresses, so can't possibly match any autoApprovers ACLs
|
2023-05-21 10:37:59 -06:00
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
routes, err := GetNodeAdvertisedRoutes(tx, node)
|
2023-05-21 10:37:59 -06:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
2024-04-10 06:49:34 -06:00
|
|
|
return fmt.Errorf("getting advertised routes for node(%s %d): %w", node.Hostname, node.ID, err)
|
2023-05-21 10:37:59 -06:00
|
|
|
}
|
|
|
|
|
2024-01-18 08:36:47 -07:00
|
|
|
log.Trace().Interface("routes", routes).Msg("routes for autoapproving")
|
|
|
|
|
2023-05-21 10:37:59 -06:00
|
|
|
approvedRoutes := types.Routes{}
|
|
|
|
|
|
|
|
for _, advertisedRoute := range routes {
|
|
|
|
if advertisedRoute.Enabled {
|
|
|
|
continue
|
2022-11-25 10:50:12 -07:00
|
|
|
}
|
|
|
|
|
2023-05-21 10:37:59 -06:00
|
|
|
routeApprovers, err := aclPolicy.AutoApprovers.GetRouteApprovers(
|
|
|
|
netip.Prefix(advertisedRoute.Prefix),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-04-10 06:49:34 -06:00
|
|
|
return fmt.Errorf("failed to resolve autoApprovers for route(%d) for node(%s %d): %w", advertisedRoute.ID, node.Hostname, node.ID, err)
|
2023-05-21 10:37:59 -06:00
|
|
|
}
|
|
|
|
|
2024-01-18 08:36:47 -07:00
|
|
|
log.Trace().
|
|
|
|
Str("node", node.Hostname).
|
|
|
|
Str("user", node.User.Name).
|
|
|
|
Strs("routeApprovers", routeApprovers).
|
|
|
|
Str("prefix", netip.Prefix(advertisedRoute.Prefix).String()).
|
|
|
|
Msg("looking up route for autoapproving")
|
|
|
|
|
2023-05-21 10:37:59 -06:00
|
|
|
for _, approvedAlias := range routeApprovers {
|
2023-09-24 05:42:05 -06:00
|
|
|
if approvedAlias == node.User.Name {
|
2023-05-21 10:37:59 -06:00
|
|
|
approvedRoutes = append(approvedRoutes, advertisedRoute)
|
|
|
|
} else {
|
|
|
|
// TODO(kradalby): figure out how to get this to depend on less stuff
|
2023-09-24 05:42:05 -06:00
|
|
|
approvedIps, err := aclPolicy.ExpandAlias(types.Nodes{node}, approvedAlias)
|
2023-05-21 10:37:59 -06:00
|
|
|
if err != nil {
|
2024-04-10 06:49:34 -06:00
|
|
|
return fmt.Errorf("expanding alias %q for autoApprovers: %w", approvedAlias, err)
|
2023-05-21 10:37:59 -06:00
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
// approvedIPs should contain all of node's IPs if it matches the rule, so check for first
|
|
|
|
if approvedIps.Contains(node.IPAddresses[0]) {
|
2023-05-21 10:37:59 -06:00
|
|
|
approvedRoutes = append(approvedRoutes, advertisedRoute)
|
|
|
|
}
|
|
|
|
}
|
2022-11-25 10:50:12 -07:00
|
|
|
}
|
2023-05-21 10:37:59 -06:00
|
|
|
}
|
2022-11-25 10:50:12 -07:00
|
|
|
|
2023-05-21 10:37:59 -06:00
|
|
|
for _, approvedRoute := range approvedRoutes {
|
2024-02-23 02:59:24 -07:00
|
|
|
_, err := EnableRoute(tx, uint64(approvedRoute.ID))
|
2023-05-21 10:37:59 -06:00
|
|
|
if err != nil {
|
2024-04-10 06:49:34 -06:00
|
|
|
return fmt.Errorf("enabling approved route(%d): %w", approvedRoute.ID, err)
|
2023-05-21 10:37:59 -06:00
|
|
|
}
|
2022-11-25 10:50:12 -07:00
|
|
|
}
|
|
|
|
|
2024-02-23 02:59:24 -07:00
|
|
|
return nil
|
2022-11-25 10:50:12 -07:00
|
|
|
}
|