Compare commits
3 Commits
6aeba2c646
...
613ce0ba52
Author | SHA1 | Date |
---|---|---|
|
613ce0ba52 | |
|
6ddeb7caed | |
|
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
|
||||
```
|
||||
|
|
56
expr/ct.go
56
expr/ct.go
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -197,6 +197,8 @@ func exprFromName(name string) Any {
|
|||
e = &Masq{}
|
||||
case "hash":
|
||||
e = &Hash{}
|
||||
case "cthelper":
|
||||
e = &CtHelper{}
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -221,12 +221,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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
want := [][]byte{
|
||||
// batch begin
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
go test ./...
|
||||
go test -c github.com/google/nftables
|
||||
sudo ./nftables.test -test.v -run_system_tests
|
2
obj.go
2
obj.go
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue