Compare commits
3 Commits
0fdb21a291
...
3900fe312c
Author | SHA1 | Date |
---|---|---|
|
3900fe312c | |
|
508bb1ffd4 | |
|
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
|
||||
}
|
||||
|
|
|
@ -266,12 +266,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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
go test ./...
|
||||
go test -c github.com/google/nftables
|
||||
sudo ./nftables.test -test.v -run_system_tests
|
7
set.go
7
set.go
|
@ -572,7 +572,7 @@ func (cc *Conn) AddSet(s *Set, vals []SetElement) error {
|
|||
if s.IsMap {
|
||||
// Check if it is vmap case
|
||||
if s.DataType.nftMagic == 1 {
|
||||
// For Verdict data type, the expected magic is 0xfffff0
|
||||
// For Verdict data type, the expected magic is 0xffffff00
|
||||
tableInfo = append(tableInfo, netlink.Attribute{Type: unix.NFTA_SET_DATA_TYPE, Data: binaryutil.BigEndian.PutUint32(uint32(unix.NFT_DATA_VERDICT))},
|
||||
netlink.Attribute{Type: unix.NFTA_SET_DATA_LEN, Data: binaryutil.BigEndian.PutUint32(s.DataType.Bytes)})
|
||||
} else {
|
||||
|
@ -772,9 +772,8 @@ func setsFromMsg(msg netlink.Message) (*Set, error) {
|
|||
case unix.NFTA_SET_DATA_TYPE:
|
||||
nftMagic := ad.Uint32()
|
||||
// Special case for the data type verdict, in the message it is stored as 0xffffff00 but it is defined as 1
|
||||
if nftMagic == 0xffffff00 {
|
||||
set.KeyType = TypeVerdict
|
||||
break
|
||||
if nftMagic == unix.NFT_DATA_VERDICT {
|
||||
nftMagic = 1
|
||||
}
|
||||
dt, err := parseSetDatatype(nftMagic)
|
||||
if err != nil {
|
||||
|
|
24
set_test.go
24
set_test.go
|
@ -240,6 +240,30 @@ func TestMarshalSet(t *testing.T) {
|
|||
Timeout: 30 * time.Second,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Map ip-ip", // generic case
|
||||
set: Set{
|
||||
Name: "test-map",
|
||||
ID: uint32(3),
|
||||
Table: tbl,
|
||||
KeyType: TypeIPAddr,
|
||||
DataType: TypeIPAddr,
|
||||
IsMap: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
// special case, see
|
||||
// sets.go:setsFromMsg:(case unix.NFTA_SET_DATA_TYPE) and sets.go:AddSet:(if s.DataType.nftMagic == 1)
|
||||
name: "Vedict map",
|
||||
set: Set{
|
||||
Name: "test-map",
|
||||
ID: uint32(3),
|
||||
Table: tbl,
|
||||
KeyType: TypeIPAddr,
|
||||
DataType: TypeVerdict,
|
||||
IsMap: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
|
|
Loading…
Reference in New Issue