2023-12-13 01:23:07 -06:00
|
|
|
package nftables_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-13 01:31:13 -06:00
|
|
|
"log"
|
2023-12-13 01:23:07 -06:00
|
|
|
"net"
|
2024-11-09 05:07:36 -06:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-12-13 01:23:07 -06:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/nftables"
|
|
|
|
"github.com/google/nftables/expr"
|
|
|
|
"github.com/google/nftables/internal/nftest"
|
|
|
|
)
|
|
|
|
|
2023-12-13 01:31:13 -06:00
|
|
|
func ExampleNewMonitor() {
|
|
|
|
conn, err := nftables.New()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mon := nftables.NewMonitor()
|
|
|
|
defer mon.Close()
|
2024-11-09 05:07:36 -06:00
|
|
|
events, err := conn.AddGenerationalMonitor(mon)
|
2023-12-13 01:31:13 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
for ev := range events {
|
2024-11-09 05:07:36 -06:00
|
|
|
log.Printf("ev: %+v, data = %T", ev, ev.Changes)
|
2023-12-13 01:31:13 -06:00
|
|
|
|
2024-11-09 05:07:36 -06:00
|
|
|
for _, change := range ev.Changes {
|
|
|
|
switch change.Type {
|
|
|
|
case nftables.MonitorEventTypeNewTable:
|
|
|
|
log.Printf("data = %+v", change.Data.(*nftables.Table))
|
|
|
|
// …more cases if needed…
|
|
|
|
}
|
2023-12-13 01:31:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 01:23:07 -06:00
|
|
|
func TestMonitor(t *testing.T) {
|
|
|
|
// Create a new network namespace to test these operations,
|
|
|
|
// and tear down the namespace at test completion.
|
|
|
|
c, newNS := nftest.OpenSystemConn(t, *enableSysTests)
|
|
|
|
defer nftest.CleanupSystemConn(t, newNS)
|
|
|
|
// Clear all rules at the beginning + end of the test.
|
|
|
|
c.FlushRuleset()
|
|
|
|
defer c.FlushRuleset()
|
|
|
|
// default to monitor all
|
|
|
|
monitor := nftables.NewMonitor()
|
2024-11-09 05:07:36 -06:00
|
|
|
events, err := c.AddGenerationalMonitor(monitor)
|
2023-12-13 01:23:07 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer monitor.Close()
|
|
|
|
|
|
|
|
var gotTable *nftables.Table
|
|
|
|
var gotChain *nftables.Chain
|
|
|
|
var gotRule *nftables.Rule
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
2024-11-09 05:07:36 -06:00
|
|
|
var errMonitor error
|
2023-12-13 01:23:07 -06:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
count := int32(0)
|
|
|
|
for {
|
2024-08-11 03:00:48 -05:00
|
|
|
event, ok := <-events
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2024-11-09 05:07:36 -06:00
|
|
|
|
|
|
|
genMsg := event.GeneratedBy.Data.(*nftables.GenMsg)
|
|
|
|
fileName := filepath.Base(os.Args[0])
|
|
|
|
|
|
|
|
if genMsg.ProcComm != fileName {
|
|
|
|
errMonitor = fmt.Errorf("procComm: %s, want: %s", genMsg.ProcComm, fileName)
|
2024-08-11 03:00:48 -05:00
|
|
|
return
|
|
|
|
}
|
2024-11-09 05:07:36 -06:00
|
|
|
|
|
|
|
for _, change := range event.Changes {
|
|
|
|
if change.Error != nil {
|
|
|
|
errMonitor = fmt.Errorf("monitor err: %s", change.Error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch change.Type {
|
|
|
|
case nftables.MonitorEventTypeNewTable:
|
|
|
|
gotTable = change.Data.(*nftables.Table)
|
|
|
|
atomic.AddInt32(&count, 1)
|
|
|
|
case nftables.MonitorEventTypeNewChain:
|
|
|
|
gotChain = change.Data.(*nftables.Chain)
|
|
|
|
atomic.AddInt32(&count, 1)
|
|
|
|
case nftables.MonitorEventTypeNewRule:
|
|
|
|
gotRule = change.Data.(*nftables.Rule)
|
|
|
|
atomic.AddInt32(&count, 1)
|
|
|
|
}
|
|
|
|
if atomic.LoadInt32(&count) == 3 {
|
|
|
|
return
|
|
|
|
}
|
2023-12-13 01:23:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
nat := c.AddTable(&nftables.Table{
|
|
|
|
Family: nftables.TableFamilyIPv4,
|
|
|
|
Name: "nat",
|
|
|
|
})
|
|
|
|
|
|
|
|
postrouting := c.AddChain(&nftables.Chain{
|
|
|
|
Name: "postrouting",
|
|
|
|
Hooknum: nftables.ChainHookPostrouting,
|
|
|
|
Priority: nftables.ChainPriorityNATSource,
|
|
|
|
Table: nat,
|
|
|
|
Type: nftables.ChainTypeNAT,
|
|
|
|
})
|
|
|
|
|
|
|
|
rule := c.AddRule(&nftables.Rule{
|
|
|
|
Table: nat,
|
|
|
|
Chain: postrouting,
|
|
|
|
Exprs: []expr.Any{
|
|
|
|
// payload load 4b @ network header + 12 => reg 1
|
|
|
|
&expr.Payload{
|
|
|
|
DestRegister: 1,
|
|
|
|
Base: expr.PayloadBaseNetworkHeader,
|
|
|
|
Offset: 12,
|
|
|
|
Len: 4,
|
|
|
|
},
|
|
|
|
// cmp eq reg 1 0x0245a8c0
|
|
|
|
&expr.Cmp{
|
|
|
|
Op: expr.CmpOpEq,
|
|
|
|
Register: 1,
|
|
|
|
Data: net.ParseIP("192.168.69.2").To4(),
|
|
|
|
},
|
|
|
|
|
|
|
|
// masq
|
|
|
|
&expr.Masq{},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := c.Flush(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-11-09 05:07:36 -06:00
|
|
|
|
2023-12-13 01:23:07 -06:00
|
|
|
wg.Wait()
|
2024-11-09 05:07:36 -06:00
|
|
|
|
|
|
|
if errMonitor != nil {
|
|
|
|
t.Fatal("monitor err", errMonitor)
|
|
|
|
}
|
|
|
|
|
2023-12-13 01:23:07 -06:00
|
|
|
if gotTable.Family != nat.Family || gotTable.Name != nat.Name {
|
|
|
|
t.Fatal("no want table", gotTable.Family, gotTable.Name)
|
|
|
|
}
|
|
|
|
if gotChain.Type != postrouting.Type || gotChain.Name != postrouting.Name ||
|
|
|
|
*gotChain.Hooknum != *postrouting.Hooknum {
|
|
|
|
t.Fatal("no want chain", gotChain.Type, gotChain.Name, gotChain.Hooknum)
|
|
|
|
}
|
|
|
|
if len(gotRule.Exprs) != len(rule.Exprs) {
|
|
|
|
t.Fatal("no want rule")
|
|
|
|
}
|
|
|
|
}
|