Compare commits
4 Commits
a4241cd73c
...
19efdc42d3
Author | SHA1 | Date |
---|---|---|
|
19efdc42d3 | |
|
385f80f4ef | |
|
594585af33 | |
|
dae73eaa9c |
|
@ -0,0 +1 @@
|
|||
nftables.test
|
|
@ -21,4 +21,12 @@ the data types/API will be identified as more functionality is added.
|
|||
|
||||
Contributions are very welcome!
|
||||
|
||||
### Testing Changes
|
||||
|
||||
Run the following commands to test your changes:
|
||||
|
||||
```bash
|
||||
go test ./...
|
||||
go test -c github.com/google/nftables
|
||||
sudo ./nftables.test -test.v -run_system_tests
|
||||
```
|
||||
|
|
|
@ -24,6 +24,15 @@ import (
|
|||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
const (
|
||||
NFT_DROP = 0
|
||||
NFT_ACCEPT = 1
|
||||
NFT_STOLEN = 2
|
||||
NFT_QUEUE = 3
|
||||
NFT_REPEAT = 4
|
||||
NFT_STOP = 5
|
||||
)
|
||||
|
||||
// This code assembles the verdict structure, as expected by the
|
||||
// nftables netlink API.
|
||||
// For further information, consult:
|
||||
|
@ -129,3 +138,37 @@ func (e *Verdict) unmarshal(fam byte, data []byte) error {
|
|||
}
|
||||
return ad.Err()
|
||||
}
|
||||
|
||||
func (e *Verdict) String() string {
|
||||
var v string
|
||||
switch e.Kind {
|
||||
case unix.NFT_RETURN:
|
||||
v = "return" // -0x5
|
||||
case unix.NFT_GOTO:
|
||||
v = "goto" // -0x4
|
||||
case unix.NFT_JUMP:
|
||||
v = "jump" // NFT_JUMP = -0x3
|
||||
case unix.NFT_BREAK:
|
||||
v = "break" // NFT_BREAK = -0x2
|
||||
case unix.NFT_CONTINUE:
|
||||
v = "continue" // NFT_CONTINUE = -0x1
|
||||
case NFT_DROP:
|
||||
v = "drop"
|
||||
case NFT_ACCEPT:
|
||||
v = "accept"
|
||||
case NFT_STOLEN:
|
||||
v = "stolen"
|
||||
case NFT_QUEUE:
|
||||
v = "queue"
|
||||
case NFT_REPEAT:
|
||||
v = "repeat"
|
||||
case NFT_STOP:
|
||||
v = "stop"
|
||||
default:
|
||||
v = fmt.Sprintf("verdict %v", e.Kind)
|
||||
}
|
||||
if e.Chain != "" {
|
||||
return v + " " + e.Chain
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
|
3
gen.go
3
gen.go
|
@ -3,6 +3,7 @@ package nftables
|
|||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
|
||||
"github.com/mdlayher/netlink"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
@ -13,7 +14,7 @@ type GenMsg struct {
|
|||
ProcComm string // [16]byte - max 16bytes - kernel TASK_COMM_LEN
|
||||
}
|
||||
|
||||
var genHeaderType = netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWGEN)
|
||||
const genHeaderType = netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWGEN)
|
||||
|
||||
func genFromMsg(msg netlink.Message) (*GenMsg, error) {
|
||||
if got, want := msg.Header.Type, genHeaderType; got != want {
|
||||
|
|
|
@ -222,12 +222,27 @@ func TestRuleOperations(t *testing.T) {
|
|||
expr.VerdictDrop,
|
||||
}
|
||||
|
||||
wantStrings := []string{
|
||||
"queue",
|
||||
"accept",
|
||||
"queue",
|
||||
"accept",
|
||||
"drop",
|
||||
"drop",
|
||||
}
|
||||
|
||||
for i, r := range rules {
|
||||
rr, _ := r.Exprs[0].(*expr.Verdict)
|
||||
|
||||
if rr.Kind != want[i] {
|
||||
t.Fatalf("bad verdict kind at %d", i)
|
||||
}
|
||||
|
||||
if rr.String() != wantStrings[i] {
|
||||
t.Fatalf("bad verdict string at %d: %s (received) vs. %s (expected)", i, rr.String(), wantStrings[i])
|
||||
}
|
||||
|
||||
t.Logf("%s", rr)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -622,6 +637,14 @@ func TestMasqMarshalUnmarshal(t *testing.T) {
|
|||
Table: filter,
|
||||
Chain: postrouting,
|
||||
Exprs: []expr.Any{
|
||||
&expr.Immediate{
|
||||
Register: min,
|
||||
Data: binaryutil.BigEndian.PutUint16(4070),
|
||||
},
|
||||
&expr.Immediate{
|
||||
Register: max,
|
||||
Data: binaryutil.BigEndian.PutUint16(4090),
|
||||
},
|
||||
&expr.Masq{
|
||||
ToPorts: true,
|
||||
RegProtoMin: min,
|
||||
|
@ -652,13 +675,13 @@ func TestMasqMarshalUnmarshal(t *testing.T) {
|
|||
}
|
||||
|
||||
rule := rules[0]
|
||||
if got, want := len(rule.Exprs), 1; got != want {
|
||||
if got, want := len(rule.Exprs), 3; got != want {
|
||||
t.Fatalf("unexpected number of exprs: got %d, want %d", got, want)
|
||||
}
|
||||
|
||||
me, ok := rule.Exprs[0].(*expr.Masq)
|
||||
me, ok := rule.Exprs[2].(*expr.Masq)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected expression type: got %T, want *expr.Masq", rule.Exprs[0])
|
||||
t.Fatalf("unexpected expression type: got %T, want *expr.Masq", rule.Exprs[2])
|
||||
}
|
||||
|
||||
if got, want := me.ToPorts, true; got != want {
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
go test ./...
|
||||
go test -c github.com/google/nftables
|
||||
sudo ./nftables.test -test.v -run_system_tests
|
2
obj.go
2
obj.go
|
@ -25,7 +25,7 @@ import (
|
|||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var (
|
||||
const (
|
||||
newObjHeaderType = netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWOBJ)
|
||||
delObjHeaderType = netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_DELOBJ)
|
||||
)
|
||||
|
|
2
rule.go
2
rule.go
|
@ -25,7 +25,7 @@ import (
|
|||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var (
|
||||
const (
|
||||
newRuleHeaderType = netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWRULE)
|
||||
delRuleHeaderType = netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_DELRULE)
|
||||
)
|
||||
|
|
6
set.go
6
set.go
|
@ -166,7 +166,9 @@ var (
|
|||
TypeTimeDay,
|
||||
TypeCGroupV2,
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
// ctLabelBitSize is defined in https://git.netfilter.org/nftables/tree/src/ct.c.
|
||||
ctLabelBitSize uint32 = 128
|
||||
|
||||
|
@ -737,7 +739,7 @@ func (cc *Conn) FlushSet(s *Set) {
|
|||
})
|
||||
}
|
||||
|
||||
var (
|
||||
const (
|
||||
newSetHeaderType = netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWSET)
|
||||
delSetHeaderType = netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_DELSET)
|
||||
)
|
||||
|
@ -837,7 +839,7 @@ func parseSetDatatype(magic uint32) (SetDatatype, error) {
|
|||
return dt, nil
|
||||
}
|
||||
|
||||
var (
|
||||
const (
|
||||
newElemHeaderType = netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWSETELEM)
|
||||
delElemHeaderType = netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_DELSETELEM)
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue