opensnitch/daemon/firewall/rules.go
Gustavo Iñiguez Goia ba7c4e1878 allow to configure what firewall to use
Before this change, we tried to determine what firewall to use based on
the version of iptables (if -V legacy -> nftables, otherwise iptables).

This caused problems (#455), and as there's no support yet for nftables
system firewall rules, it can't be configured to workaround these
errors.

Now the default firewall to use will be iptables.
If it's not available (installed), can't be used or the configuration
option is empty/missing, we'll use nftables.
2021-08-09 00:32:27 +02:00

85 lines
1.9 KiB
Go

package firewall
import (
"github.com/evilsocket/opensnitch/daemon/firewall/config"
"github.com/evilsocket/opensnitch/daemon/firewall/iptables"
"github.com/evilsocket/opensnitch/daemon/firewall/nftables"
"github.com/evilsocket/opensnitch/daemon/log"
)
// Firewall is the interface that all firewalls (iptables, nftables) must implement.
type Firewall interface {
Init(*int)
Stop()
Name() string
IsRunning() bool
SetQueueNum(num *int)
InsertRules()
QueueDNSResponses(bool, bool) (error, error)
QueueConnections(bool, bool) (error, error)
CleanRules(bool)
AddSystemRules()
DeleteSystemRules(bool, bool)
AddSystemRule(*config.FwRule, bool) (error, error)
CreateSystemRule(*config.FwRule, bool)
}
var fw Firewall
// IsRunning returns if the firewall is running or not.
func IsRunning() bool {
return fw != nil && fw.IsRunning()
}
// CleanRules deletes the rules we added.
func CleanRules(logErrors bool) {
if fw == nil {
return
}
fw.CleanRules(logErrors)
}
// Stop deletes the firewall rules, allowing network traffic.
func Stop() {
if fw == nil {
return
}
fw.Stop()
}
// Init initializes the firewall and loads firewall rules.
func Init(fwType string, qNum *int) {
var err error
if fwType == iptables.Name {
fw, err = iptables.Fw()
if err != nil {
log.Warning("iptables not available: %s", err)
}
}
// if iptables is not installed, we can add nftables rules directly to the kernel,
// without relying on any binaries.
if fwType == nftables.Name || err != nil {
fw, err = nftables.Fw()
if err != nil {
log.Warning("nftables not available: %s", err)
}
}
if err != nil {
log.Warning("firewall error: %s, not iptables nor nftables are available or are usable. Please, report it on github.", err)
return
}
if fw == nil {
log.Error("firewall not initialized.")
return
}
fw.Stop()
fw.Init(qNum)
log.Info("Using %s firewall", fw.Name())
}