Compare commits
3 Commits
4c8ce809eb
...
d477ae97f0
Author | SHA1 | Date |
---|---|---|
|
d477ae97f0 | |
|
e99829fb4f | |
|
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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5921,6 +5936,68 @@ func TestSet4(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSetComment(t *testing.T) {
|
||||
want := [][]byte{
|
||||
// batch begin
|
||||
[]byte("\x00\x00\x00\x0a"),
|
||||
// nft flush ruleset
|
||||
[]byte("\x00\x00\x00\x00"),
|
||||
// nft add table inet filter
|
||||
[]byte("\x01\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x08\x00\x02\x00\x00\x00\x00\x00"),
|
||||
// nft add set inet filter setname { type ipv4_addr\; comment \"test comment\" \; }
|
||||
[]byte("\x01\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x0c\x00\x02\x00\x73\x65\x74\x6e\x61\x6d\x65\x00\x08\x00\x03\x00\x00\x00\x00\x00\x08\x00\x04\x00\x00\x00\x00\x07\x08\x00\x05\x00\x00\x00\x00\x04\x08\x00\x0a\x00\x00\x00\x00\x02\x13\x00\x0d\x00\x07\x0d\x74\x65\x73\x74\x20\x63\x6f\x6d\x6d\x65\x6e\x74\x00\x00"),
|
||||
// batch end
|
||||
[]byte("\x00\x00\x00\x0a"),
|
||||
}
|
||||
|
||||
c, err := nftables.New(nftables.WithTestDial(
|
||||
func(req []netlink.Message) ([]netlink.Message, error) {
|
||||
for idx, msg := range req {
|
||||
b, err := msg.MarshalBinary()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(b) < 16 {
|
||||
continue
|
||||
}
|
||||
b = b[16:]
|
||||
if len(want) == 0 {
|
||||
t.Errorf("no want entry for message %d: %x", idx, b)
|
||||
continue
|
||||
}
|
||||
if got, want := b, want[0]; !bytes.Equal(got, want) {
|
||||
t.Errorf("message %d: %s", idx, linediff(nfdump(got), nfdump(want)))
|
||||
}
|
||||
want = want[1:]
|
||||
}
|
||||
return req, nil
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
c.FlushRuleset()
|
||||
|
||||
filter := c.AddTable(&nftables.Table{
|
||||
Family: nftables.TableFamilyINet,
|
||||
Name: "filter",
|
||||
})
|
||||
|
||||
if err := c.AddSet(&nftables.Set{
|
||||
ID: 2,
|
||||
Table: filter,
|
||||
Name: "setname",
|
||||
KeyType: nftables.TypeIPAddr,
|
||||
Comment: "test comment",
|
||||
}, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := c.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMasq(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
go test ./...
|
||||
go test -c github.com/google/nftables
|
||||
sudo ./nftables.test -test.v -run_system_tests
|
5
set.go
5
set.go
|
@ -266,6 +266,7 @@ type Set struct {
|
|||
// Either host (binaryutil.NativeEndian) or big (binaryutil.BigEndian) endian as per
|
||||
// https://git.netfilter.org/nftables/tree/include/datatype.h?id=d486c9e626405e829221b82d7355558005b26d8a#n109
|
||||
KeyByteOrder binaryutil.ByteOrder
|
||||
Comment string
|
||||
}
|
||||
|
||||
// SetElement represents a data point within a set.
|
||||
|
@ -598,6 +599,10 @@ func (cc *Conn) AddSet(s *Set, vals []SetElement) error {
|
|||
userData = userdata.AppendUint32(userData, userdata.NFTNL_UDATA_SET_MERGE_ELEMENTS, 1)
|
||||
}
|
||||
|
||||
if len(s.Comment) != 0 {
|
||||
userData = userdata.AppendString(userData, userdata.NFTNL_UDATA_SET_COMMENT, s.Comment)
|
||||
}
|
||||
|
||||
if len(userData) > 0 {
|
||||
tableInfo = append(tableInfo, netlink.Attribute{Type: unix.NFTA_SET_USERDATA, Data: userData})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue