2023-05-29 16:00:18 +02:00
|
|
|
package nftables
|
|
|
|
|
|
|
|
import (
|
2023-12-08 10:48:21 +01:00
|
|
|
"context"
|
2023-05-29 16:00:18 +02:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/nftables"
|
|
|
|
"github.com/google/nftables/expr"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
fw "github.com/netbirdio/netbird/client/firewall/manager"
|
2023-06-07 15:24:27 +02:00
|
|
|
"github.com/netbirdio/netbird/iface"
|
2023-05-29 16:00:18 +02:00
|
|
|
)
|
|
|
|
|
2023-06-07 15:24:27 +02:00
|
|
|
// iFaceMapper defines subset methods of interface required for manager
|
|
|
|
type iFaceMock struct {
|
|
|
|
NameFunc func() string
|
|
|
|
AddressFunc func() iface.WGAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *iFaceMock) Name() string {
|
|
|
|
if i.NameFunc != nil {
|
|
|
|
return i.NameFunc()
|
|
|
|
}
|
|
|
|
panic("NameFunc is not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *iFaceMock) Address() iface.WGAddress {
|
|
|
|
if i.AddressFunc != nil {
|
|
|
|
return i.AddressFunc()
|
|
|
|
}
|
|
|
|
panic("AddressFunc is not set")
|
|
|
|
}
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
func TestNftablesManager(t *testing.T) {
|
2023-06-07 15:24:27 +02:00
|
|
|
mock := &iFaceMock{
|
|
|
|
NameFunc: func() string {
|
|
|
|
return "lo"
|
|
|
|
},
|
|
|
|
AddressFunc: func() iface.WGAddress {
|
|
|
|
return iface.WGAddress{
|
|
|
|
IP: net.ParseIP("100.96.0.1"),
|
|
|
|
Network: &net.IPNet{
|
|
|
|
IP: net.ParseIP("100.96.0.0"),
|
|
|
|
Mask: net.IPv4Mask(255, 255, 255, 0),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
// just check on the local interface
|
2023-12-08 10:48:21 +01:00
|
|
|
manager, err := Create(context.Background(), mock)
|
2023-05-29 16:00:18 +02:00
|
|
|
require.NoError(t, err)
|
2023-07-18 11:12:50 +02:00
|
|
|
time.Sleep(time.Second * 3)
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = manager.Reset()
|
|
|
|
require.NoError(t, err, "failed to reset")
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ip := net.ParseIP("100.96.0.1")
|
|
|
|
|
|
|
|
testClient := &nftables.Conn{}
|
|
|
|
|
|
|
|
rule, err := manager.AddFiltering(
|
|
|
|
ip,
|
|
|
|
fw.ProtocolTCP,
|
|
|
|
nil,
|
|
|
|
&fw.Port{Values: []int{53}},
|
|
|
|
fw.RuleDirectionIN,
|
|
|
|
fw.ActionDrop,
|
|
|
|
"",
|
2023-07-18 11:12:50 +02:00
|
|
|
"",
|
2023-05-29 16:00:18 +02:00
|
|
|
)
|
|
|
|
require.NoError(t, err, "failed to add rule")
|
|
|
|
|
2023-07-18 11:12:50 +02:00
|
|
|
err = manager.Flush()
|
|
|
|
require.NoError(t, err, "failed to flush")
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
rules, err := testClient.GetRules(manager.aclManager.workTable, manager.aclManager.chainInputRules)
|
2023-05-29 16:00:18 +02:00
|
|
|
require.NoError(t, err, "failed to get rules")
|
2023-07-18 11:12:50 +02:00
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
require.Len(t, rules, 1, "expected 1 rules")
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
ipToAdd, _ := netip.AddrFromSlice(ip)
|
|
|
|
add := ipToAdd.Unmap()
|
|
|
|
expectedExprs := []expr.Any{
|
|
|
|
&expr.Meta{Key: expr.MetaKeyIIFNAME, Register: 1},
|
|
|
|
&expr.Cmp{
|
|
|
|
Op: expr.CmpOpEq,
|
|
|
|
Register: 1,
|
|
|
|
Data: ifname("lo"),
|
|
|
|
},
|
|
|
|
&expr.Payload{
|
|
|
|
DestRegister: 1,
|
|
|
|
Base: expr.PayloadBaseNetworkHeader,
|
|
|
|
Offset: uint32(9),
|
|
|
|
Len: uint32(1),
|
|
|
|
},
|
|
|
|
&expr.Cmp{
|
|
|
|
Register: 1,
|
|
|
|
Op: expr.CmpOpEq,
|
|
|
|
Data: []byte{unix.IPPROTO_TCP},
|
|
|
|
},
|
|
|
|
&expr.Payload{
|
|
|
|
DestRegister: 1,
|
|
|
|
Base: expr.PayloadBaseNetworkHeader,
|
|
|
|
Offset: 12,
|
|
|
|
Len: 4,
|
|
|
|
},
|
|
|
|
&expr.Cmp{
|
|
|
|
Op: expr.CmpOpEq,
|
|
|
|
Register: 1,
|
|
|
|
Data: add.AsSlice(),
|
|
|
|
},
|
|
|
|
&expr.Payload{
|
|
|
|
DestRegister: 1,
|
|
|
|
Base: expr.PayloadBaseTransportHeader,
|
|
|
|
Offset: 2,
|
|
|
|
Len: 2,
|
|
|
|
},
|
|
|
|
&expr.Cmp{
|
|
|
|
Op: expr.CmpOpEq,
|
|
|
|
Register: 1,
|
|
|
|
Data: []byte{0, 53},
|
|
|
|
},
|
|
|
|
&expr.Verdict{Kind: expr.VerdictDrop},
|
|
|
|
}
|
|
|
|
require.ElementsMatch(t, rules[0].Exprs, expectedExprs, "expected the same expressions")
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
for _, r := range rule {
|
|
|
|
err = manager.DeleteRule(r)
|
|
|
|
require.NoError(t, err, "failed to delete rule")
|
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
|
2023-07-18 11:12:50 +02:00
|
|
|
err = manager.Flush()
|
|
|
|
require.NoError(t, err, "failed to flush")
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
rules, err = testClient.GetRules(manager.aclManager.workTable, manager.aclManager.chainInputRules)
|
2023-05-29 16:00:18 +02:00
|
|
|
require.NoError(t, err, "failed to get rules")
|
2023-12-08 10:48:21 +01:00
|
|
|
require.Len(t, rules, 0, "expected 0 rules after deletion")
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
err = manager.Reset()
|
|
|
|
require.NoError(t, err, "failed to reset")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNFtablesCreatePerformance(t *testing.T) {
|
2023-06-07 15:24:27 +02:00
|
|
|
mock := &iFaceMock{
|
|
|
|
NameFunc: func() string {
|
|
|
|
return "lo"
|
|
|
|
},
|
|
|
|
AddressFunc: func() iface.WGAddress {
|
|
|
|
return iface.WGAddress{
|
|
|
|
IP: net.ParseIP("100.96.0.1"),
|
|
|
|
Network: &net.IPNet{
|
|
|
|
IP: net.ParseIP("100.96.0.0"),
|
|
|
|
Mask: net.IPv4Mask(255, 255, 255, 0),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
for _, testMax := range []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000} {
|
|
|
|
t.Run(fmt.Sprintf("Testing %d rules", testMax), func(t *testing.T) {
|
|
|
|
// just check on the local interface
|
2023-12-08 10:48:21 +01:00
|
|
|
manager, err := Create(context.Background(), mock)
|
2023-05-29 16:00:18 +02:00
|
|
|
require.NoError(t, err)
|
2023-07-18 11:12:50 +02:00
|
|
|
time.Sleep(time.Second * 3)
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := manager.Reset(); err != nil {
|
|
|
|
t.Errorf("clear the manager state: %v", err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ip := net.ParseIP("10.20.0.100")
|
|
|
|
start := time.Now()
|
|
|
|
for i := 0; i < testMax; i++ {
|
|
|
|
port := &fw.Port{Values: []int{1000 + i}}
|
|
|
|
if i%2 == 0 {
|
2023-07-18 11:12:50 +02:00
|
|
|
_, err = manager.AddFiltering(ip, "tcp", nil, port, fw.RuleDirectionOUT, fw.ActionAccept, "", "accept HTTP traffic")
|
2023-05-29 16:00:18 +02:00
|
|
|
} else {
|
2023-07-18 11:12:50 +02:00
|
|
|
_, err = manager.AddFiltering(ip, "tcp", nil, port, fw.RuleDirectionIN, fw.ActionAccept, "", "accept HTTP traffic")
|
2023-05-29 16:00:18 +02:00
|
|
|
}
|
|
|
|
require.NoError(t, err, "failed to add rule")
|
2023-07-18 11:12:50 +02:00
|
|
|
|
|
|
|
if i%100 == 0 {
|
|
|
|
err = manager.Flush()
|
|
|
|
require.NoError(t, err, "failed to flush")
|
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
}
|
2023-07-18 11:12:50 +02:00
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
t.Logf("execution avg per rule: %s", time.Since(start)/time.Duration(testMax))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|