Compare commits
3 Commits
8c564b0097
...
6a5756c734
Author | SHA1 | Date |
---|---|---|
|
6a5756c734 | |
|
4195a123ff | |
|
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!
|
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"
|
"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
|
// This code assembles the verdict structure, as expected by the
|
||||||
// nftables netlink API.
|
// nftables netlink API.
|
||||||
// For further information, consult:
|
// For further information, consult:
|
||||||
|
@ -129,3 +138,37 @@ func (e *Verdict) unmarshal(fam byte, data []byte) error {
|
||||||
}
|
}
|
||||||
return ad.Err()
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -307,12 +307,27 @@ func TestRuleOperations(t *testing.T) {
|
||||||
expr.VerdictDrop,
|
expr.VerdictDrop,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wantStrings := []string{
|
||||||
|
"queue",
|
||||||
|
"accept",
|
||||||
|
"queue",
|
||||||
|
"accept",
|
||||||
|
"drop",
|
||||||
|
"drop",
|
||||||
|
}
|
||||||
|
|
||||||
for i, r := range rules {
|
for i, r := range rules {
|
||||||
rr, _ := r.Exprs[0].(*expr.Verdict)
|
rr, _ := r.Exprs[0].(*expr.Verdict)
|
||||||
|
|
||||||
if rr.Kind != want[i] {
|
if rr.Kind != want[i] {
|
||||||
t.Fatalf("bad verdict kind at %d", 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
go test ./...
|
||||||
|
go test -c github.com/google/nftables
|
||||||
|
sudo ./nftables.test -test.v -run_system_tests
|
17
set.go
17
set.go
|
@ -253,6 +253,7 @@ type Set struct {
|
||||||
Anonymous bool
|
Anonymous bool
|
||||||
Constant bool
|
Constant bool
|
||||||
Interval bool
|
Interval bool
|
||||||
|
DataInterval bool
|
||||||
AutoMerge bool
|
AutoMerge bool
|
||||||
IsMap bool
|
IsMap bool
|
||||||
HasTimeout bool
|
HasTimeout bool
|
||||||
|
@ -674,6 +675,10 @@ func (cc *Conn) AddSet(s *Set, vals []SetElement) error {
|
||||||
userData = userdata.AppendUint32(userData, userdata.NFTNL_UDATA_SET_MERGE_ELEMENTS, 1)
|
userData = userdata.AppendUint32(userData, userdata.NFTNL_UDATA_SET_MERGE_ELEMENTS, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.DataInterval {
|
||||||
|
userData = userdata.AppendUint32(userData, userdata.NFTNL_UDATA_SET_DATA_INTERVAL, 1)
|
||||||
|
}
|
||||||
|
|
||||||
if len(s.Comment) != 0 {
|
if len(s.Comment) != 0 {
|
||||||
userData = userdata.AppendString(userData, userdata.NFTNL_UDATA_SET_COMMENT, s.Comment)
|
userData = userdata.AppendString(userData, userdata.NFTNL_UDATA_SET_COMMENT, s.Comment)
|
||||||
}
|
}
|
||||||
|
@ -797,8 +802,16 @@ func setsFromMsg(msg netlink.Message) (*Set, error) {
|
||||||
set.DataType.Bytes = binary.BigEndian.Uint32(ad.Bytes())
|
set.DataType.Bytes = binary.BigEndian.Uint32(ad.Bytes())
|
||||||
case unix.NFTA_SET_USERDATA:
|
case unix.NFTA_SET_USERDATA:
|
||||||
data := ad.Bytes()
|
data := ad.Bytes()
|
||||||
value, ok := userdata.GetUint32(data, userdata.NFTNL_UDATA_SET_MERGE_ELEMENTS)
|
if val, ok := userdata.GetString(data, userdata.NFTNL_UDATA_SET_COMMENT); ok {
|
||||||
set.AutoMerge = ok && value == 1
|
set.Comment = val
|
||||||
|
}
|
||||||
|
if val, ok := userdata.GetUint32(data, userdata.NFTNL_UDATA_SET_MERGE_ELEMENTS); ok {
|
||||||
|
set.AutoMerge = val == 1
|
||||||
|
}
|
||||||
|
if val, ok := userdata.GetUint32(data, userdata.NFTNL_UDATA_SET_DATA_INTERVAL); ok {
|
||||||
|
set.DataInterval = val == 1
|
||||||
|
}
|
||||||
|
|
||||||
case unix.NFTA_SET_DESC:
|
case unix.NFTA_SET_DESC:
|
||||||
nestedAD, err := netlink.NewAttributeDecoder(ad.Bytes())
|
nestedAD, err := netlink.NewAttributeDecoder(ad.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
15
set_test.go
15
set_test.go
|
@ -257,13 +257,26 @@ func TestMarshalSet(t *testing.T) {
|
||||||
name: "Vedict map",
|
name: "Vedict map",
|
||||||
set: Set{
|
set: Set{
|
||||||
Name: "test-map",
|
Name: "test-map",
|
||||||
ID: uint32(3),
|
ID: uint32(4),
|
||||||
Table: tbl,
|
Table: tbl,
|
||||||
KeyType: TypeIPAddr,
|
KeyType: TypeIPAddr,
|
||||||
DataType: TypeVerdict,
|
DataType: TypeVerdict,
|
||||||
IsMap: true,
|
IsMap: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Map ip-ip", // generic case
|
||||||
|
set: Set{
|
||||||
|
Name: "test-map",
|
||||||
|
ID: uint32(5),
|
||||||
|
Table: tbl,
|
||||||
|
KeyType: TypeIPAddr,
|
||||||
|
DataType: TypeIPAddr,
|
||||||
|
DataInterval: true,
|
||||||
|
IsMap: true,
|
||||||
|
Comment: "test-comment",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
|
|
Loading…
Reference in New Issue