Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement cthelper object expr #268

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions expr/ct.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,59 @@ func (e *Ct) unmarshal(fam byte, data []byte) error {
}
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()
}
2 changes: 2 additions & 0 deletions expr/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ func exprFromName(name string) Any {
e = &Masq{}
case "hash":
e = &Hash{}
case "cthelper":
e = &CtHelper{}
}
return e
}
Expand Down
59 changes: 59 additions & 0 deletions nftables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,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) {
want := [][]byte{
// batch begin
Expand Down
2 changes: 1 addition & 1 deletion obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var objByObjTypeMagic = map[ObjType]string{
ObjTypeQuota: "quota",
ObjTypeLimit: "limit",
ObjTypeConnLimit: "connlimit",
ObjTypeCtHelper: "cthelper", // not implemented in expr
ObjTypeCtHelper: "cthelper",
ObjTypeTunnel: "tunnel", // not implemented in expr
ObjTypeCtTimeout: "cttimeout", // not implemented in expr
ObjTypeSecMark: "secmark", // not implemented in expr
Expand Down
Loading