Compare commits
3 Commits
334ca9e5d3
...
46640a47c1
Author | SHA1 | Date |
---|---|---|
|
46640a47c1 | |
|
68e1406c13 | |
|
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
|
||||||
|
}
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -12,6 +12,6 @@ require (
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/mdlayher/socket v0.5.0 // indirect
|
github.com/mdlayher/socket v0.5.0 // indirect
|
||||||
golang.org/x/net v0.37.0 // indirect
|
golang.org/x/net v0.38.0 // indirect
|
||||||
golang.org/x/sync v0.6.0 // indirect
|
golang.org/x/sync v0.6.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -8,8 +8,8 @@ github.com/vishvananda/netlink v1.3.0 h1:X7l42GfcV4S6E4vHTsw48qbrV+9PVojNfIhZcwQ
|
||||||
github.com/vishvananda/netlink v1.3.0/go.mod h1:i6NetklAujEcC6fK0JPjT8qSwWyO0HLn4UKG+hGqeJs=
|
github.com/vishvananda/netlink v1.3.0/go.mod h1:i6NetklAujEcC6fK0JPjT8qSwWyO0HLn4UKG+hGqeJs=
|
||||||
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
|
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
|
||||||
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
|
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
|
||||||
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
|
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
|
||||||
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
||||||
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
|
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
|
||||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
|
|
@ -266,12 +266,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
|
Loading…
Reference in New Issue