Compare commits
3 Commits
46640a47c1
...
ea3208a367
Author | SHA1 | Date |
---|---|---|
|
ea3208a367 | |
|
8a8ad2be81 | |
|
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
|
||||||
|
```
|
||||||
|
|
26
expr/ct.go
26
expr/ct.go
|
@ -110,6 +110,12 @@ const (
|
||||||
CtStateUDPREPLIED
|
CtStateUDPREPLIED
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// https://git.netfilter.org/libnftnl/tree/src/expr/ct.c?id=116e95aa7b6358c917de8c69f6f173874030b46b#n31
|
||||||
|
CtDirOriginal = iota
|
||||||
|
CtDirReply
|
||||||
|
)
|
||||||
|
|
||||||
// https://git.netfilter.org/libnftnl/tree/src/obj/ct_timeout.c?id=116e95aa7b6358c917de8c69f6f173874030b46b#n57
|
// https://git.netfilter.org/libnftnl/tree/src/obj/ct_timeout.c?id=116e95aa7b6358c917de8c69f6f173874030b46b#n57
|
||||||
var CtStateUDPTimeoutDefaults CtStatePolicyTimeout = map[uint16]uint32{
|
var CtStateUDPTimeoutDefaults CtStatePolicyTimeout = map[uint16]uint32{
|
||||||
CtStateUDPUNREPLIED: 30,
|
CtStateUDPUNREPLIED: 30,
|
||||||
|
@ -122,6 +128,7 @@ type Ct struct {
|
||||||
SourceRegister bool
|
SourceRegister bool
|
||||||
Key CtKey
|
Key CtKey
|
||||||
Direction uint32
|
Direction uint32
|
||||||
|
OptDirection bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Ct) marshal(fam byte) ([]byte, error) {
|
func (e *Ct) marshal(fam byte) ([]byte, error) {
|
||||||
|
@ -165,10 +172,16 @@ func (e *Ct) marshalData(fam byte) ([]byte, error) {
|
||||||
exprData = append(exprData, regData...)
|
exprData = append(exprData, regData...)
|
||||||
|
|
||||||
switch e.Key {
|
switch e.Key {
|
||||||
|
case CtKeyPKTS, CtKeyBYTES, CtKeyAVGPKT, CtKeyL3PROTOCOL, CtKeyPROTOCOL:
|
||||||
|
if !e.OptDirection {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
fallthrough
|
||||||
case CtKeySRC, CtKeyDST, CtKeyPROTOSRC, CtKeyPROTODST, CtKeySRCIP, CtKeyDSTIP, CtKeySRCIP6, CtKeyDSTIP6:
|
case CtKeySRC, CtKeyDST, CtKeyPROTOSRC, CtKeyPROTODST, CtKeySRCIP, CtKeyDSTIP, CtKeySRCIP6, CtKeyDSTIP6:
|
||||||
regData, err = netlink.MarshalAttributes(
|
regData, err = netlink.MarshalAttributes(
|
||||||
[]netlink.Attribute{
|
[]netlink.Attribute{
|
||||||
{Type: unix.NFTA_CT_DIRECTION, Data: binaryutil.BigEndian.PutUint32(e.Direction)},
|
{Type: unix.NFTA_CT_DIRECTION, Data: []byte{uint8(e.Direction)}},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -186,6 +199,8 @@ func (e *Ct) unmarshal(fam byte, data []byte) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ad.ByteOrder = binary.BigEndian
|
ad.ByteOrder = binary.BigEndian
|
||||||
|
|
||||||
|
var hasDirection bool
|
||||||
for ad.Next() {
|
for ad.Next() {
|
||||||
switch ad.Type() {
|
switch ad.Type() {
|
||||||
case unix.NFTA_CT_KEY:
|
case unix.NFTA_CT_KEY:
|
||||||
|
@ -193,12 +208,19 @@ func (e *Ct) unmarshal(fam byte, data []byte) error {
|
||||||
case unix.NFTA_CT_DREG:
|
case unix.NFTA_CT_DREG:
|
||||||
e.Register = ad.Uint32()
|
e.Register = ad.Uint32()
|
||||||
case unix.NFTA_CT_DIRECTION:
|
case unix.NFTA_CT_DIRECTION:
|
||||||
e.Direction = ad.Uint32()
|
e.Direction = uint32(ad.Uint8())
|
||||||
|
hasDirection = true
|
||||||
case unix.NFTA_CT_SREG:
|
case unix.NFTA_CT_SREG:
|
||||||
e.SourceRegister = true
|
e.SourceRegister = true
|
||||||
e.Register = ad.Uint32()
|
e.Register = ad.Uint32()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch e.Key {
|
||||||
|
case CtKeyPKTS, CtKeyBYTES, CtKeyAVGPKT, CtKeyL3PROTOCOL, CtKeyPROTOCOL:
|
||||||
|
e.OptDirection = hasDirection
|
||||||
|
}
|
||||||
|
|
||||||
return ad.Err()
|
return ad.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,6 +78,22 @@ func TestCt(t *testing.T) {
|
||||||
Direction: 1,
|
Direction: 1,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Unmarshal Ct packets direction original case",
|
||||||
|
ct: Ct{
|
||||||
|
Register: 1,
|
||||||
|
Key: CtKeyPKTS,
|
||||||
|
Direction: CtDirOriginal,
|
||||||
|
OptDirection: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Unmarshal Ct bytes without direction case",
|
||||||
|
ct: Ct{
|
||||||
|
Register: 1,
|
||||||
|
Key: CtKeyBYTES,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range 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
|
||||||
|
}
|
||||||
|
|
|
@ -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