2023-03-16 10:00:08 +01:00
|
|
|
package iptables
|
|
|
|
|
|
|
|
import (
|
2023-12-08 10:48:21 +01:00
|
|
|
"context"
|
2023-03-16 10:00:08 +01:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2024-10-02 13:41:00 +02:00
|
|
|
"net/netip"
|
2023-03-16 10:00:08 +01:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/coreos/go-iptables/iptables"
|
2023-05-29 16:00:18 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-03-16 10:00:08 +01:00
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
firewall "github.com/netbirdio/netbird/client/firewall/manager"
|
2023-06-07 15:24:27 +02:00
|
|
|
"github.com/netbirdio/netbird/iface"
|
2023-03-16 10:00:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Manager of iptables firewall
|
|
|
|
type Manager struct {
|
|
|
|
mutex sync.Mutex
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
wgIface iFaceMapper
|
2023-07-24 13:00:23 +02:00
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
ipv4Client *iptables.IPTables
|
|
|
|
aclMgr *aclManager
|
2024-10-02 13:41:00 +02:00
|
|
|
router *router
|
2023-06-07 15:24:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// iFaceMapper defines subset methods of interface required for manager
|
|
|
|
type iFaceMapper interface {
|
|
|
|
Name() string
|
|
|
|
Address() iface.WGAddress
|
2023-09-05 21:07:32 +02:00
|
|
|
IsUserspaceBind() bool
|
2023-03-16 10:00:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create iptables firewall manager
|
2023-12-08 10:48:21 +01:00
|
|
|
func Create(context context.Context, wgIface iFaceMapper) (*Manager, error) {
|
|
|
|
iptablesClient, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
|
2023-03-16 10:00:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("iptables is not installed in the system or not supported")
|
|
|
|
}
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
m := &Manager{
|
|
|
|
wgIface: wgIface,
|
|
|
|
ipv4Client: iptablesClient,
|
2023-03-16 10:00:08 +01:00
|
|
|
}
|
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
m.router, err = newRouter(context, iptablesClient, wgIface)
|
2023-12-08 10:48:21 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Debugf("failed to initialize route related chains: %s", err)
|
|
|
|
return nil, err
|
2023-09-05 21:07:32 +02:00
|
|
|
}
|
2024-10-02 13:41:00 +02:00
|
|
|
m.aclMgr, err = newAclManager(iptablesClient, wgIface, chainRTFWD)
|
2023-12-08 10:48:21 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Debugf("failed to initialize ACL manager: %s", err)
|
|
|
|
return nil, err
|
2023-03-16 10:00:08 +01:00
|
|
|
}
|
2023-12-08 10:48:21 +01:00
|
|
|
|
2023-03-16 10:00:08 +01:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
// AddPeerFiltering adds a rule to the firewall
|
2023-05-29 16:00:18 +02:00
|
|
|
//
|
2023-09-27 09:51:20 +02:00
|
|
|
// Comment will be ignored because some system this feature is not supported
|
2024-10-02 13:41:00 +02:00
|
|
|
func (m *Manager) AddPeerFiltering(
|
2023-03-16 10:00:08 +01:00
|
|
|
ip net.IP,
|
2023-12-08 10:48:21 +01:00
|
|
|
protocol firewall.Protocol,
|
|
|
|
sPort *firewall.Port,
|
|
|
|
dPort *firewall.Port,
|
|
|
|
direction firewall.RuleDirection,
|
|
|
|
action firewall.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
|
|
|
) ([]firewall.Rule, error) {
|
2023-03-16 10:00:08 +01:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
2023-05-29 16:00:18 +02:00
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
return m.aclMgr.AddPeerFiltering(ip, protocol, sPort, dPort, direction, action, ipsetName)
|
2023-03-16 10:00:08 +01:00
|
|
|
}
|
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
func (m *Manager) AddRouteFiltering(
|
|
|
|
sources [] netip.Prefix,
|
|
|
|
destination netip.Prefix,
|
|
|
|
proto firewall.Protocol,
|
|
|
|
sPort *firewall.Port,
|
|
|
|
dPort *firewall.Port,
|
|
|
|
action firewall.Action,
|
|
|
|
) (firewall.Rule, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
if !destination.Addr().Is4() {
|
|
|
|
return nil, fmt.Errorf("unsupported IP version: %s", destination.Addr().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.router.AddRouteFiltering(sources, destination, proto, sPort, dPort, action)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePeerRule from the firewall by rule definition
|
|
|
|
func (m *Manager) DeletePeerRule(rule firewall.Rule) error {
|
2023-03-16 10:00:08 +01:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
2023-05-29 16:00:18 +02:00
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
return m.aclMgr.DeletePeerRule(rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) DeleteRouteRule(rule firewall.Rule) error {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
return m.router.DeleteRouteRule(rule)
|
2023-12-08 10:48:21 +01:00
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
func (m *Manager) IsServerRouteSupported() bool {
|
|
|
|
return true
|
|
|
|
}
|
2023-07-24 13:00:23 +02:00
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
func (m *Manager) AddNatRule(pair firewall.RouterPair) error {
|
2023-12-08 10:48:21 +01:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
2023-07-24 13:00:23 +02:00
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
return m.router.AddNatRule(pair)
|
2023-12-08 10:48:21 +01:00
|
|
|
}
|
2023-07-24 13:00:23 +02:00
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
func (m *Manager) RemoveNatRule(pair firewall.RouterPair) error {
|
2023-12-08 10:48:21 +01:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
2023-07-24 13:00:23 +02:00
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
return m.router.RemoveNatRule(pair)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) SetLegacyManagement(isLegacy bool) error {
|
|
|
|
return firewall.SetLegacyManagement(m.router, isLegacy)
|
2023-03-16 10:00:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset firewall to the default state
|
|
|
|
func (m *Manager) Reset() error {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
2023-05-29 16:00:18 +02:00
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
errAcl := m.aclMgr.Reset()
|
|
|
|
if errAcl != nil {
|
|
|
|
log.Errorf("failed to clean up ACL rules from firewall: %s", errAcl)
|
2023-03-16 10:00:08 +01:00
|
|
|
}
|
2023-12-08 10:48:21 +01:00
|
|
|
errMgr := m.router.Reset()
|
|
|
|
if errMgr != nil {
|
|
|
|
log.Errorf("failed to clean up router rules from firewall: %s", errMgr)
|
|
|
|
return errMgr
|
2023-03-16 10:00:08 +01:00
|
|
|
}
|
2023-12-08 10:48:21 +01:00
|
|
|
return errAcl
|
2023-03-16 10:00:08 +01:00
|
|
|
}
|
|
|
|
|
2023-09-05 21:07:32 +02:00
|
|
|
// AllowNetbird allows netbird interface traffic
|
|
|
|
func (m *Manager) AllowNetbird() error {
|
2023-12-08 10:48:21 +01:00
|
|
|
if !m.wgIface.IsUserspaceBind() {
|
2023-05-29 16:00:18 +02:00
|
|
|
return nil
|
2023-03-16 10:00:08 +01:00
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
_, err := m.AddPeerFiltering(
|
2023-12-08 10:48:21 +01:00
|
|
|
net.ParseIP("0.0.0.0"),
|
|
|
|
"all",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
firewall.RuleDirectionIN,
|
|
|
|
firewall.ActionAccept,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
)
|
2023-05-29 16:00:18 +02:00
|
|
|
if err != nil {
|
2023-12-08 10:48:21 +01:00
|
|
|
return fmt.Errorf("failed to allow netbird interface traffic: %w", err)
|
|
|
|
}
|
2024-10-02 13:41:00 +02:00
|
|
|
_, err = m.AddPeerFiltering(
|
2023-12-08 10:48:21 +01:00
|
|
|
net.ParseIP("0.0.0.0"),
|
|
|
|
"all",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
firewall.RuleDirectionOUT,
|
|
|
|
firewall.ActionAccept,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
return err
|
2023-03-16 10:00:08 +01:00
|
|
|
}
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
// Flush doesn't need to be implemented for this manager
|
|
|
|
func (m *Manager) Flush() error { return nil }
|
2024-10-02 13:41:00 +02:00
|
|
|
|
|
|
|
func getConntrackEstablished() []string {
|
|
|
|
return []string{"-m", "conntrack", "--ctstate", "RELATED,ESTABLISHED", "-j", "ACCEPT"}
|
|
|
|
}
|