2023-12-08 10:48:21 +01:00
|
|
|
package manager
|
2023-03-16 10:00:08 +01:00
|
|
|
|
|
|
|
import (
|
2024-10-02 13:41:00 +02:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2023-12-08 10:48:21 +01:00
|
|
|
"fmt"
|
2023-03-16 10:00:08 +01:00
|
|
|
"net"
|
2024-10-02 13:41:00 +02:00
|
|
|
"net/netip"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-03-16 10:00:08 +01:00
|
|
|
)
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
const (
|
2024-10-02 13:41:00 +02:00
|
|
|
ForwardingFormatPrefix = "netbird-fwd-"
|
|
|
|
ForwardingFormat = "netbird-fwd-%s-%t"
|
|
|
|
NatFormat = "netbird-nat-%s-%t"
|
2023-12-08 10:48:21 +01:00
|
|
|
)
|
|
|
|
|
2023-03-16 10:00:08 +01:00
|
|
|
// Rule abstraction should be implemented by each firewall manager
|
|
|
|
//
|
|
|
|
// Each firewall type for different OS can use different type
|
|
|
|
// of the properties to hold data of the created rule
|
|
|
|
type Rule interface {
|
|
|
|
// GetRuleID returns the rule id
|
|
|
|
GetRuleID() string
|
|
|
|
}
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
// RuleDirection is the traffic direction which a rule is applied
|
|
|
|
type RuleDirection int
|
2023-03-16 10:00:08 +01:00
|
|
|
|
|
|
|
const (
|
2023-05-29 16:00:18 +02:00
|
|
|
// RuleDirectionIN applies to filters that handlers incoming traffic
|
|
|
|
RuleDirectionIN RuleDirection = iota
|
|
|
|
// RuleDirectionOUT applies to filters that handlers outgoing traffic
|
|
|
|
RuleDirectionOUT
|
2023-03-16 10:00:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Action is the action to be taken on a rule
|
|
|
|
type Action int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ActionAccept is the action to accept a packet
|
2023-12-08 10:48:21 +01:00
|
|
|
ActionAccept Action = iota
|
2023-03-16 10:00:08 +01:00
|
|
|
// ActionDrop is the action to drop a packet
|
|
|
|
ActionDrop
|
|
|
|
)
|
|
|
|
|
|
|
|
// Manager is the high level abstraction of a firewall manager
|
|
|
|
//
|
|
|
|
// It declares methods which handle actions required by the
|
|
|
|
// Netbird client for ACL and routing functionality
|
|
|
|
type Manager interface {
|
2023-09-05 21:07:32 +02:00
|
|
|
// AllowNetbird allows netbird interface traffic
|
|
|
|
AllowNetbird() error
|
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
// AddPeerFiltering adds a rule to the firewall
|
2023-05-29 16:00:18 +02:00
|
|
|
//
|
|
|
|
// If comment argument is empty firewall manager should set
|
|
|
|
// rule ID as comment for the rule
|
2024-10-02 13:41:00 +02:00
|
|
|
AddPeerFiltering(
|
2023-03-16 10:00:08 +01:00
|
|
|
ip net.IP,
|
2023-05-29 16:00:18 +02:00
|
|
|
proto Protocol,
|
|
|
|
sPort *Port,
|
|
|
|
dPort *Port,
|
|
|
|
direction RuleDirection,
|
2023-03-16 10:00:08 +01:00
|
|
|
action Action,
|
2023-07-18 11:12:50 +02:00
|
|
|
ipsetName string,
|
2023-03-16 10:00:08 +01:00
|
|
|
comment string,
|
2023-12-08 10:48:21 +01:00
|
|
|
) ([]Rule, error)
|
2023-03-16 10:00:08 +01:00
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
// DeletePeerRule from the firewall by rule definition
|
|
|
|
DeletePeerRule(rule Rule) error
|
2023-03-16 10:00:08 +01:00
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
// IsServerRouteSupported returns true if the firewall supports server side routing operations
|
|
|
|
IsServerRouteSupported() bool
|
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
AddRouteFiltering(source []netip.Prefix, destination netip.Prefix, proto Protocol, sPort *Port, dPort *Port, action Action) (Rule, error)
|
|
|
|
|
|
|
|
// DeleteRouteRule deletes a routing rule
|
|
|
|
DeleteRouteRule(rule Rule) error
|
|
|
|
|
|
|
|
// AddNatRule inserts a routing NAT rule
|
|
|
|
AddNatRule(pair RouterPair) error
|
2023-12-08 10:48:21 +01:00
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
// RemoveNatRule removes a routing NAT rule
|
|
|
|
RemoveNatRule(pair RouterPair) error
|
|
|
|
|
|
|
|
// SetLegacyManagement sets the legacy management mode
|
|
|
|
SetLegacyManagement(legacy bool) error
|
2023-12-08 10:48:21 +01:00
|
|
|
|
2023-03-16 10:00:08 +01:00
|
|
|
// Reset firewall to the default state
|
|
|
|
Reset() error
|
|
|
|
|
2023-07-18 11:12:50 +02:00
|
|
|
// Flush the changes to firewall controller
|
|
|
|
Flush() error
|
2023-12-08 10:48:21 +01:00
|
|
|
}
|
2023-07-18 11:12:50 +02:00
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
func GenKey(format string, pair RouterPair) string {
|
|
|
|
return fmt.Sprintf(format, pair.ID, pair.Inverse)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LegacyManager defines the interface for legacy management operations
|
|
|
|
type LegacyManager interface {
|
|
|
|
RemoveAllLegacyRouteRules() error
|
|
|
|
GetLegacyManagement() bool
|
|
|
|
SetLegacyManagement(bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLegacyManagement sets the route manager to use legacy management
|
|
|
|
func SetLegacyManagement(router LegacyManager, isLegacy bool) error {
|
|
|
|
oldLegacy := router.GetLegacyManagement()
|
|
|
|
|
|
|
|
if oldLegacy != isLegacy {
|
|
|
|
router.SetLegacyManagement(isLegacy)
|
|
|
|
log.Debugf("Set legacy management to %v", isLegacy)
|
|
|
|
}
|
|
|
|
|
|
|
|
// client reconnected to a newer mgmt, we need to clean up the legacy rules
|
|
|
|
if !isLegacy && oldLegacy {
|
|
|
|
if err := router.RemoveAllLegacyRouteRules(); err != nil {
|
|
|
|
return fmt.Errorf("remove legacy routing rules: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Legacy routing rules removed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateSetName generates a unique name for an ipset based on the given sources.
|
|
|
|
func GenerateSetName(sources []netip.Prefix) string {
|
|
|
|
// sort for consistent naming
|
2024-10-10 15:54:34 +02:00
|
|
|
SortPrefixes(sources)
|
2024-10-02 13:41:00 +02:00
|
|
|
|
|
|
|
var sourcesStr strings.Builder
|
|
|
|
for _, src := range sources {
|
|
|
|
sourcesStr.WriteString(src.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := sha256.Sum256([]byte(sourcesStr.String()))
|
|
|
|
shortHash := hex.EncodeToString(hash[:])[:8]
|
|
|
|
|
|
|
|
return fmt.Sprintf("nb-%s", shortHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MergeIPRanges merges overlapping IP ranges and returns a slice of non-overlapping netip.Prefix
|
|
|
|
func MergeIPRanges(prefixes []netip.Prefix) []netip.Prefix {
|
|
|
|
if len(prefixes) == 0 {
|
|
|
|
return prefixes
|
|
|
|
}
|
|
|
|
|
|
|
|
merged := []netip.Prefix{prefixes[0]}
|
|
|
|
for _, prefix := range prefixes[1:] {
|
|
|
|
last := merged[len(merged)-1]
|
|
|
|
if last.Contains(prefix.Addr()) {
|
|
|
|
// If the current prefix is contained within the last merged prefix, skip it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if prefix.Contains(last.Addr()) {
|
|
|
|
// If the current prefix contains the last merged prefix, replace it
|
|
|
|
merged[len(merged)-1] = prefix
|
|
|
|
} else {
|
|
|
|
// Otherwise, add the current prefix to the merged list
|
|
|
|
merged = append(merged, prefix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return merged
|
|
|
|
}
|
|
|
|
|
2024-10-10 15:54:34 +02:00
|
|
|
// SortPrefixes sorts the given slice of netip.Prefix in place.
|
2024-10-02 13:41:00 +02:00
|
|
|
// It sorts first by IP address, then by prefix length (most specific to least specific).
|
2024-10-10 15:54:34 +02:00
|
|
|
func SortPrefixes(prefixes []netip.Prefix) {
|
2024-10-02 13:41:00 +02:00
|
|
|
sort.Slice(prefixes, func(i, j int) bool {
|
|
|
|
addrCmp := prefixes[i].Addr().Compare(prefixes[j].Addr())
|
|
|
|
if addrCmp != 0 {
|
|
|
|
return addrCmp < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// If IP addresses are the same, compare prefix lengths (longer prefixes first)
|
|
|
|
return prefixes[i].Bits() > prefixes[j].Bits()
|
|
|
|
})
|
2023-03-16 10:00:08 +01:00
|
|
|
}
|