Compare commits

...

3 Commits

Author SHA1 Message Date
Paul Greenberg 613ce0ba52
Merge dae73eaa9c into 6ddeb7caed 2024-08-15 09:45:08 +02:00
turekt 6ddeb7caed
Implement cthelper object expr (#268) 2024-08-15 08:47:35 +02:00
Paul Greenberg dae73eaa9c rule: add String() method
Before this commit: the printing of a rule results in
a pointer address.

After this commit: the printing of a rules results in
a human-readable text.

Resolves: #104

Signed-off-by: Paul Greenberg <greenpau@outlook.com>
2020-08-03 10:59:40 -04:00
8 changed files with 188 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
nftables.test

View File

@ -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
```

View File

@ -138,3 +138,59 @@ func (e *Ct) unmarshal(fam byte, data []byte) error {
} }
return ad.Err() return ad.Err()
} }
type CtHelper struct {
Name string
L3Proto uint16
L4Proto uint8
}
func (c *CtHelper) marshal(fam byte) ([]byte, error) {
exprData, err := c.marshalData(fam)
if err != nil {
return nil, err
}
return netlink.MarshalAttributes([]netlink.Attribute{
{Type: unix.NFTA_EXPR_NAME, Data: []byte("cthelper\x00")},
{Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: exprData},
})
}
func (c *CtHelper) marshalData(fam byte) ([]byte, error) {
exprData := []netlink.Attribute{
{Type: unix.NFTA_CT_HELPER_NAME, Data: []byte(c.Name)},
}
if c.L3Proto != 0 {
exprData = append(exprData, netlink.Attribute{
Type: unix.NFTA_CT_HELPER_L3PROTO, Data: binaryutil.BigEndian.PutUint16(c.L3Proto),
})
}
if c.L4Proto != 0 {
exprData = append(exprData, netlink.Attribute{
Type: unix.NFTA_CT_HELPER_L4PROTO, Data: []byte{c.L4Proto},
})
}
return netlink.MarshalAttributes(exprData)
}
func (c *CtHelper) unmarshal(fam byte, data []byte) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
ad.ByteOrder = binary.BigEndian
for ad.Next() {
switch ad.Type() {
case unix.NFTA_CT_HELPER_NAME:
c.Name = ad.String()
case unix.NFTA_CT_HELPER_L3PROTO:
c.L3Proto = ad.Uint16()
case unix.NFTA_CT_HELPER_L4PROTO:
c.L4Proto = ad.Uint8()
}
}
return ad.Err()
}

View File

@ -197,6 +197,8 @@ func exprFromName(name string) Any {
e = &Masq{} e = &Masq{}
case "hash": case "hash":
e = &Hash{} e = &Hash{}
case "cthelper":
e = &CtHelper{}
} }
return e return e
} }

View File

@ -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
}

View File

@ -221,12 +221,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)
} }
} }
@ -1383,6 +1398,65 @@ func TestCt(t *testing.T) {
} }
} }
func TestCtHelper(t *testing.T) {
conn, newNS := nftest.OpenSystemConn(t, *enableSysTests)
defer nftest.CleanupSystemConn(t, newNS)
conn.FlushRuleset()
defer conn.FlushRuleset()
table := conn.AddTable(&nftables.Table{
Family: nftables.TableFamilyIPv4,
Name: "filter",
})
cthelp1 := conn.AddObj(&nftables.NamedObj{
Table: table,
Name: "ftp-standard",
Type: nftables.ObjTypeCtHelper,
Obj: &expr.CtHelper{
Name: "ftp",
L4Proto: unix.IPPROTO_TCP,
L3Proto: unix.NFPROTO_IPV4,
},
})
if err := conn.Flush(); err != nil {
t.Fatalf(err.Error())
}
obj1, err := conn.GetObject(cthelp1)
if err != nil {
t.Errorf("c.GetObject(cthelp1) failed: %v failed", err)
}
helper, ok := obj1.(*nftables.NamedObj)
if !ok {
t.Fatalf("unexpected type: got %T, want *nftables.ObjAttr", obj1)
}
if got, want := helper.Name, "ftp-standard"; got != want {
t.Fatalf("unexpected counter name: got %s, want %s", got, want)
}
if _, err = conn.ResetObject(cthelp1); err != nil {
t.Errorf("c.ResetObjects(cthelp1) failed: %v failed", err)
}
obj1, err = conn.GetObject(cthelp1)
if err != nil {
t.Errorf("c.GetObject(cthelp1) failed: %v failed", err)
}
help := obj1.(*nftables.NamedObj).Obj.(*expr.CtHelper)
if got, want := help.L4Proto, uint8(unix.IPPROTO_TCP); got != want {
t.Errorf("unexpected l4proto number: got %d, want %d", got, want)
}
if got, want := help.L3Proto, uint16(unix.NFPROTO_IPV4); got != want {
t.Errorf("unexpected l3proto number: got %d, want %d", got, want)
}
}
func TestCtSet(t *testing.T) { func TestCtSet(t *testing.T) {
want := [][]byte{ want := [][]byte{
// batch begin // batch begin

3
nftables_test.sh Executable file
View File

@ -0,0 +1,3 @@
go test ./...
go test -c github.com/google/nftables
sudo ./nftables.test -test.v -run_system_tests

2
obj.go
View File

@ -51,7 +51,7 @@ var objByObjTypeMagic = map[ObjType]string{
ObjTypeQuota: "quota", ObjTypeQuota: "quota",
ObjTypeLimit: "limit", ObjTypeLimit: "limit",
ObjTypeConnLimit: "connlimit", ObjTypeConnLimit: "connlimit",
ObjTypeCtHelper: "cthelper", // not implemented in expr ObjTypeCtHelper: "cthelper",
ObjTypeTunnel: "tunnel", // not implemented in expr ObjTypeTunnel: "tunnel", // not implemented in expr
ObjTypeCtTimeout: "cttimeout", // not implemented in expr ObjTypeCtTimeout: "cttimeout", // not implemented in expr
ObjTypeSecMark: "secmark", // not implemented in expr ObjTypeSecMark: "secmark", // not implemented in expr