Documentation
¶
Index ¶
- Constants
- Variables
- func MergeIPRanges(prefixes []netip.Prefix) []netip.Prefix
- func SetLegacyManagement(router LegacyManager, isLegacy bool) error
- func SortPrefixes(prefixes []netip.Prefix)
- func UnmapPrefix(p netip.Prefix) netip.Prefix
- type Action
- type ForwardRule
- type LegacyManager
- type Manager
- type Network
- type Port
- type Protocol
- type RouterPair
- type Rule
- type RuleDirection
- type RuleID
- type Set
Constants ¶
const ( ForwardingFormatPrefix = "netbird-fwd-" ForwardingFormat = "netbird-fwd-%s-%t" PreroutingFormat = "netbird-prerouting-%s-%t" NatFormat = "netbird-nat-%s-%t" )
Variables ¶
var ErrIPv6NotInitialized = errors.New("IPv6 firewall not initialized")
ErrIPv6NotInitialized is returned when an IPv6 address is passed to a firewall method but the IPv6 firewall components were not initialized.
var ErrNoSources = errors.New("rule has no sources")
ErrNoSources is returned when AddFilterRule is called with an empty source list. "Match any source" must be expressed explicitly with a /0 prefix; an empty list is a caller error and is rejected rather than silently widening the rule to every source.
Functions ¶
func MergeIPRanges ¶ added in v0.30.0
MergeIPRanges merges overlapping IP ranges and returns a slice of non-overlapping netip.Prefix
func SetLegacyManagement ¶ added in v0.30.0
func SetLegacyManagement(router LegacyManager, isLegacy bool) error
SetLegacyManagement sets the route manager to use legacy management
func SortPrefixes ¶ added in v0.30.1
SortPrefixes sorts the given slice of netip.Prefix in place. It sorts first by IP address, then by prefix length (most specific to least specific).
func UnmapPrefix ¶ added in v0.78.0
UnmapPrefix normalizes a v4-mapped v6 prefix (::ffff:a.b.c.d) to its plain v4 form, shifting the prefix length out of the 96-bit mapped range. Other prefixes are returned unchanged. Keeping prefixes unmapped ensures v4 rules match consistently and the match builders read the correct address length.
Types ¶
type ForwardRule ¶ added in v0.38.0
type ForwardRule struct {
Protocol Protocol
DestinationPort Port
TranslatedAddress netip.Addr
TranslatedPort Port
}
ForwardRule todo figure out better place to this to avoid circular imports
func (ForwardRule) ID ¶ added in v0.38.0
func (r ForwardRule) ID() RuleID
func (ForwardRule) String ¶ added in v0.38.0
func (r ForwardRule) String() string
type LegacyManager ¶ added in v0.30.0
type LegacyManager interface {
RemoveAllLegacyRouteRules() error
GetLegacyManagement() bool
SetLegacyManagement(bool)
}
LegacyManager defines the interface for legacy management operations
type Manager ¶
type Manager interface {
Init(stateManager *statemanager.Manager) error
// AddFilterRule adds a packet-filtering rule to the firewall.
//
// If destination is the zero Network, the rule applies to traffic
// inbound to this node, i.e. peer ACL semantics, installed in
// the kernel's input chain. If destination is set (prefix or
// set), the rule applies to forwarded traffic with that
// destination, route ACL semantics, installed in the forward
// chain.
//
// sources must be a single address family; the caller splits mixed
// families and calls once per family. "Match any source" must be
// expressed with an explicit /0 prefix; an empty sources list is
// rejected with ErrNoSources so a zeroed list can never widen a
// rule to every source.
//
// Note: callers should call Flush() after adding rules.
AddFilterRule(
id []byte,
sources []netip.Prefix,
destination Network,
proto Protocol,
sPort *Port,
dPort *Port,
action Action,
) (Rule, error)
// DeleteFilterRule removes a filtering rule previously added via
// AddFilterRule. The rule's own type identifies whether it lives
// in the peer (input) or route (forward) path.
DeleteFilterRule(rule Rule) error
// IsServerRouteSupported returns true if the firewall supports server side routing operations
IsServerRouteSupported() bool
IsStateful() bool
// AddNatRule inserts a routing NAT rule
AddNatRule(pair RouterPair) error
// RemoveNatRule removes a routing NAT rule
RemoveNatRule(pair RouterPair) error
// SetLegacyManagement sets the legacy management mode
SetLegacyManagement(legacy bool) error
// Close closes the firewall manager
Close(stateManager *statemanager.Manager) error
// Flush the changes to firewall controller
Flush() error
SetLogLevel(log.Level)
EnableRouting() error
DisableRouting() error
// AddDNATRule adds outbound DNAT rule for forwarding external traffic to the NetBird network.
AddDNATRule(ForwardRule) (Rule, error)
// DeleteDNATRule deletes the outbound DNAT rule.
DeleteDNATRule(Rule) error
// UpdateSet updates the set with the given prefixes
UpdateSet(hash Set, prefixes []netip.Prefix) error
// AddInboundDNAT adds an inbound DNAT rule redirecting traffic from NetBird peers to local services
AddInboundDNAT(localAddr netip.Addr, protocol Protocol, originalPort, translatedPort uint16) error
// RemoveInboundDNAT removes inbound DNAT rule
RemoveInboundDNAT(localAddr netip.Addr, protocol Protocol, originalPort, translatedPort uint16) error
// AddOutputDNAT adds an OUTPUT chain DNAT rule for locally-generated traffic.
AddOutputDNAT(localAddr netip.Addr, protocol Protocol, originalPort, translatedPort uint16) error
// RemoveOutputDNAT removes an OUTPUT chain DNAT rule.
RemoveOutputDNAT(localAddr netip.Addr, protocol Protocol, originalPort, translatedPort uint16) error
// SetupEBPFProxyNoTrack creates static notrack rules for eBPF proxy loopback traffic.
// This prevents conntrack from interfering with WireGuard proxy communication.
SetupEBPFProxyNoTrack(proxyPort, wgPort uint16) error
}
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 Network ¶ added in v0.43.0
Network is a rule destination, either a set or a prefix
func (Network) IsPrefix ¶ added in v0.43.0
IsPrefix returns true if the destination is a valid prefix
type Port ¶
type Port struct {
// IsRange is true Values contains two values, the first is the start port, the second is the end port
IsRange bool
// Values contains one value for single port, multiple values for the list of ports, or two values for the range of ports
Values []uint16
}
Port of the address for firewall rule todo Move Protocol and Port and RouterPair to the Firwall package or a separate package
type Protocol ¶
type Protocol string
Protocol is the protocol of the port todo Move Protocol and Port and RouterPair to the Firwall package or a separate package
type RouterPair ¶
type RouterPair struct {
ID route.ID
Source Network
Destination Network
Masquerade bool
Inverse bool
// Dynamic indicates the route is domain-based. NAT rules for dynamic
// routes are duplicated to the v6 table so that resolved AAAA records
// are masqueraded correctly.
Dynamic bool
}
func GetInversePair ¶ added in v0.30.0
func GetInversePair(pair RouterPair) RouterPair
func ToV6NatPair ¶ added in v0.71.0
func ToV6NatPair(pair RouterPair) RouterPair
ToV6NatPair creates a v6 counterpart of a v4 NAT pair with `::/0` source and, for prefix destinations, `::/0` destination.
func (RouterPair) GenKey ¶ added in v0.78.0
func (p RouterPair) GenKey(format string) RuleID
GenKey builds the rule id for this pair from the given format.
type Rule ¶
type Rule interface {
// ID returns the rule id
ID() RuleID
}
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 RuleDirection ¶
type RuleDirection int
RuleDirection is the traffic direction which a rule is applied
const ( // RuleDirectionIN applies to filters that handlers incoming traffic RuleDirectionIN RuleDirection = iota // RuleDirectionOUT applies to filters that handlers outgoing traffic RuleDirectionOUT )
type RuleID ¶ added in v0.78.0
type RuleID string
RuleID identifies a firewall rule. It is a typed string so the compiler catches accidental mixing with arbitrary string keys. It is only an identifier and does not implement Rule.
type Set ¶ added in v0.43.0
type Set struct {
// contains filtered or unexported fields
}
func NewDomainSet ¶ added in v0.43.0
NewDomainSet generates a unique name for an ipset based on the given domains.
func NewPrefixSet ¶ added in v0.43.0
NewPrefixSet generates a unique name for an ipset based on the given prefixes.
func (Set) HashedName ¶ added in v0.43.0
HashedName returns the string representation of the hash