2023-12-08 10:48:21 +01:00
|
|
|
//go:build !android
|
|
|
|
|
|
|
|
package firewall
|
|
|
|
|
|
|
|
import (
|
2024-10-28 10:02:27 +01:00
|
|
|
"errors"
|
2023-12-08 10:48:21 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/coreos/go-iptables/iptables"
|
|
|
|
"github.com/google/nftables"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
nbiptables "github.com/netbirdio/netbird/client/firewall/iptables"
|
|
|
|
firewall "github.com/netbirdio/netbird/client/firewall/manager"
|
|
|
|
nbnftables "github.com/netbirdio/netbird/client/firewall/nftables"
|
|
|
|
"github.com/netbirdio/netbird/client/firewall/uspfilter"
|
2024-10-24 14:46:24 +02:00
|
|
|
"github.com/netbirdio/netbird/client/internal/statemanager"
|
2023-12-08 10:48:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// UNKNOWN is the default value for the firewall type for unknown firewall type
|
|
|
|
UNKNOWN FWType = iota
|
|
|
|
// IPTABLES is the value for the iptables firewall type
|
|
|
|
IPTABLES
|
|
|
|
// NFTABLES is the value for the nftables firewall type
|
|
|
|
NFTABLES
|
|
|
|
)
|
|
|
|
|
|
|
|
// SKIP_NFTABLES_ENV is the environment variable to skip nftables check
|
|
|
|
const SKIP_NFTABLES_ENV = "NB_SKIP_NFTABLES_CHECK"
|
|
|
|
|
|
|
|
// FWType is the type for the firewall type
|
|
|
|
type FWType int
|
|
|
|
|
2024-10-24 14:46:24 +02:00
|
|
|
func NewFirewall(iface IFaceMapper, stateManager *statemanager.Manager) (firewall.Manager, error) {
|
2023-12-08 10:48:21 +01:00
|
|
|
// on the linux system we try to user nftables or iptables
|
|
|
|
// in any case, because we need to allow netbird interface traffic
|
|
|
|
// so we use AllowNetbird traffic from these firewall managers
|
|
|
|
// for the userspace packet filtering firewall
|
2024-10-28 10:02:27 +01:00
|
|
|
fm, err := createNativeFirewall(iface, stateManager)
|
2023-12-08 10:48:21 +01:00
|
|
|
|
2024-10-28 10:02:27 +01:00
|
|
|
if !iface.IsUserspaceBind() {
|
|
|
|
return fm, err
|
2024-10-24 14:46:24 +02:00
|
|
|
}
|
|
|
|
|
2024-10-28 10:02:27 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("failed to create native firewall: %v. Proceeding with userspace", err)
|
2024-10-24 14:46:24 +02:00
|
|
|
}
|
2024-10-28 10:02:27 +01:00
|
|
|
return createUserspaceFirewall(iface, fm)
|
|
|
|
}
|
2024-10-24 14:46:24 +02:00
|
|
|
|
2024-10-28 10:02:27 +01:00
|
|
|
func createNativeFirewall(iface IFaceMapper, stateManager *statemanager.Manager) (firewall.Manager, error) {
|
|
|
|
fm, err := createFW(iface)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("create firewall: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = fm.Init(stateManager); err != nil {
|
|
|
|
return nil, fmt.Errorf("init firewall: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fm, nil
|
2024-10-24 14:46:24 +02:00
|
|
|
}
|
|
|
|
|
2024-10-28 10:02:27 +01:00
|
|
|
func createFW(iface IFaceMapper) (firewall.Manager, error) {
|
2023-12-08 10:48:21 +01:00
|
|
|
switch check() {
|
|
|
|
case IPTABLES:
|
2024-10-28 10:02:27 +01:00
|
|
|
log.Info("creating an iptables firewall manager")
|
|
|
|
return nbiptables.Create(iface)
|
2023-12-08 10:48:21 +01:00
|
|
|
case NFTABLES:
|
2024-10-28 10:02:27 +01:00
|
|
|
log.Info("creating an nftables firewall manager")
|
|
|
|
return nbnftables.Create(iface)
|
2023-12-08 10:48:21 +01:00
|
|
|
default:
|
2024-05-24 13:03:19 +02:00
|
|
|
log.Info("no firewall manager found, trying to use userspace packet filtering firewall")
|
2024-10-28 10:02:27 +01:00
|
|
|
return nil, errors.New("no firewall manager found")
|
2023-12-08 10:48:21 +01:00
|
|
|
}
|
2024-10-24 14:46:24 +02:00
|
|
|
}
|
2023-12-08 10:48:21 +01:00
|
|
|
|
2024-10-28 10:02:27 +01:00
|
|
|
func createUserspaceFirewall(iface IFaceMapper, fm firewall.Manager) (firewall.Manager, error) {
|
2024-10-24 14:46:24 +02:00
|
|
|
var errUsp error
|
2024-10-28 10:02:27 +01:00
|
|
|
if fm != nil {
|
2024-10-24 14:46:24 +02:00
|
|
|
fm, errUsp = uspfilter.CreateWithNativeFirewall(iface, fm)
|
|
|
|
} else {
|
|
|
|
fm, errUsp = uspfilter.Create(iface)
|
2023-12-08 10:48:21 +01:00
|
|
|
}
|
|
|
|
|
2024-10-24 14:46:24 +02:00
|
|
|
if errUsp != nil {
|
2024-10-28 10:02:27 +01:00
|
|
|
return nil, fmt.Errorf("create userspace firewall: %s", errUsp)
|
2024-10-24 14:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := fm.AllowNetbird(); err != nil {
|
|
|
|
log.Errorf("failed to allow netbird interface traffic: %v", err)
|
|
|
|
}
|
2023-12-08 10:48:21 +01:00
|
|
|
return fm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// check returns the firewall type based on common lib checks. It returns UNKNOWN if no firewall is found.
|
|
|
|
func check() FWType {
|
2024-05-23 16:09:51 +02:00
|
|
|
useIPTABLES := false
|
2024-05-27 08:37:32 +02:00
|
|
|
var iptablesChains []string
|
2024-05-23 16:09:51 +02:00
|
|
|
ip, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
|
|
|
|
if err == nil && isIptablesClientAvailable(ip) {
|
|
|
|
major, minor, _ := ip.GetIptablesVersion()
|
|
|
|
// use iptables when its version is lower than 1.8.0 which doesn't work well with our nftables manager
|
|
|
|
if major < 1 || (major == 1 && minor < 8) {
|
|
|
|
return IPTABLES
|
|
|
|
}
|
|
|
|
|
|
|
|
useIPTABLES = true
|
|
|
|
|
2024-05-27 08:37:32 +02:00
|
|
|
iptablesChains, err = ip.ListChains("filter")
|
2024-05-23 16:09:51 +02:00
|
|
|
if err != nil {
|
2024-05-27 08:37:32 +02:00
|
|
|
log.Errorf("failed to list iptables chains: %s", err)
|
2024-05-23 16:09:51 +02:00
|
|
|
useIPTABLES = false
|
|
|
|
}
|
2023-12-08 10:48:21 +01:00
|
|
|
}
|
|
|
|
|
2024-05-23 16:09:51 +02:00
|
|
|
nf := nftables.Conn{}
|
|
|
|
if chains, err := nf.ListChains(); err == nil && os.Getenv(SKIP_NFTABLES_ENV) != "true" {
|
|
|
|
if !useIPTABLES {
|
|
|
|
return NFTABLES
|
|
|
|
}
|
2024-05-27 08:37:32 +02:00
|
|
|
|
|
|
|
// search for chains where table is filter
|
|
|
|
// if we find one, we assume that nftables manager can be used with iptables
|
2024-05-23 16:09:51 +02:00
|
|
|
for _, chain := range chains {
|
2024-05-27 08:37:32 +02:00
|
|
|
if chain.Table.Name == "filter" {
|
2024-05-23 16:09:51 +02:00
|
|
|
return NFTABLES
|
|
|
|
}
|
|
|
|
}
|
2024-05-27 08:37:32 +02:00
|
|
|
|
|
|
|
// check tables for the following constraints:
|
|
|
|
// 1. there is no chain in nftables for the filter table and there is at least one chain in iptables, we assume that nftables manager can not be used
|
|
|
|
// 2. there is no tables or more than one table, we assume that nftables manager can be used
|
|
|
|
// 3. there is only one table and its name is filter, we assume that nftables manager can not be used, since there was no chain in it
|
|
|
|
// 4. if we find an error we log and continue with iptables check
|
|
|
|
nbTablesList, err := nf.ListTables()
|
|
|
|
switch {
|
|
|
|
case err == nil && len(iptablesChains) > 0:
|
|
|
|
return IPTABLES
|
|
|
|
case err == nil && len(nbTablesList) != 1:
|
|
|
|
return NFTABLES
|
|
|
|
case err == nil && len(nbTablesList) == 1 && nbTablesList[0].Name == "filter":
|
|
|
|
return IPTABLES
|
|
|
|
case err != nil:
|
|
|
|
log.Errorf("failed to list nftables tables on fw manager discovery: %s", err)
|
|
|
|
}
|
2023-12-08 10:48:21 +01:00
|
|
|
}
|
2024-05-23 16:09:51 +02:00
|
|
|
|
|
|
|
if useIPTABLES {
|
2023-12-08 10:48:21 +01:00
|
|
|
return IPTABLES
|
|
|
|
}
|
|
|
|
|
|
|
|
return UNKNOWN
|
|
|
|
}
|
|
|
|
|
|
|
|
func isIptablesClientAvailable(client *iptables.IPTables) bool {
|
|
|
|
_, err := client.ListChains("filter")
|
|
|
|
return err == nil
|
|
|
|
}
|