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

Marshal flow labels, automatically set SequenceAdjust.Direction #37

Merged
merged 4 commits into from
Oct 11, 2023
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
5 changes: 2 additions & 3 deletions attribute_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,10 @@ func (seq *SequenceAdjust) unmarshal(ad *netlink.AttributeDecoder) error {
}

// marshal marshals a SequenceAdjust into a netfilter.Attribute.
func (seq SequenceAdjust) marshal() netfilter.Attribute {

func (seq SequenceAdjust) marshal(reply bool) netfilter.Attribute {
// Set orig/reply AttributeType
at := ctaSeqAdjOrig
if seq.Direction {
if seq.Direction || reply {
at = ctaSeqAdjReply
}

Expand Down
2 changes: 1 addition & 1 deletion attribute_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ func TestAttributeSeqAdj(t *testing.T) {
sa.Direction = false
}

assert.EqualValues(t, nfaSeqAdj, sa.marshal())
assert.EqualValues(t, nfaSeqAdj, sa.marshal(false))
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type tupleType uint8

// enum ctattr_tuple
const (
ctaTupleUnspec tupleType = iota //CTA_TUPLE_UNSPEC
ctaTupleUnspec tupleType = iota // CTA_TUPLE_UNSPEC
ctaTupleIP // CTA_TUPLE_IP
ctaTupleProto // CTA_TUPLE_PROTO
ctaTupleZone // CTA_TUPLE_ZONE
Expand Down
32 changes: 0 additions & 32 deletions enum_test.go

This file was deleted.

18 changes: 15 additions & 3 deletions flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (f Flow) marshal() ([]netfilter.Attribute, error) {
return nil, errNeedTuples
}

attrs := make([]netfilter.Attribute, 0, 12)
attrs := make([]netfilter.Attribute, 0, 14)

if f.TupleOrig.filled() {
to, err := f.TupleOrig.marshal(uint16(ctaTupleOrig))
Expand Down Expand Up @@ -244,17 +244,29 @@ func (f Flow) marshal() ([]netfilter.Attribute, error) {
}

if f.SeqAdjOrig.filled() {
attrs = append(attrs, f.SeqAdjOrig.marshal())
attrs = append(attrs, f.SeqAdjOrig.marshal(false))
}

if f.SeqAdjReply.filled() {
attrs = append(attrs, f.SeqAdjReply.marshal())
attrs = append(attrs, f.SeqAdjReply.marshal(true))
}

if f.SynProxy.filled() {
attrs = append(attrs, f.SynProxy.marshal())
}

if len(f.Labels) > 0 {
a := netfilter.Attribute{Type: uint16(ctaLabels)}
a.Data = f.Labels
attrs = append(attrs, a)
}

if len(f.LabelsMask) > 0 {
a := netfilter.Attribute{Type: uint16(ctaLabelsMask)}
a.Data = f.LabelsMask
attrs = append(attrs, a)
}

return attrs, nil
}

Expand Down
59 changes: 58 additions & 1 deletion flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,17 +422,74 @@ func TestFlowUnmarshal(t *testing.T) {

func TestFlowMarshal(t *testing.T) {
// Expect a marshal without errors
_, err := Flow{
attrs, err := Flow{
TupleOrig: flowIPPT, TupleReply: flowIPPT, TupleMaster: flowIPPT,
ProtoInfo: ProtoInfo{TCP: &ProtoInfoTCP{State: 42}},
Timeout: 123, Status: Status{Value: 1234}, Mark: 0x1234, Zone: 2,
Helper: Helper{Name: "ftp"},
SeqAdjOrig: SequenceAdjust{Position: 1, OffsetBefore: 2, OffsetAfter: 3},
SeqAdjReply: SequenceAdjust{Position: 5, OffsetBefore: 6, OffsetAfter: 7},
SynProxy: SynProxy{ISN: 0x12345678, ITS: 0x87654321, TSOff: 0xabcdef00},
Labels: []byte{0x13, 0x37},
LabelsMask: []byte{0xff, 0xff},
}.marshal()
assert.NoError(t, err)

want := []netfilter.Attribute{
{Type: uint16(ctaTupleOrig), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaTupleIP), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaIPv4Src), Data: []byte{0x1, 0x2, 0x3, 0x4}},
{Type: uint16(ctaIPv4Dst), Data: []byte{0x4, 0x3, 0x2, 0x1}},
}},
{Type: uint16(ctaTupleProto), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaProtoNum), Data: []byte{0x6}},
{Type: uint16(ctaProtoSrcPort), Data: []byte{0xff, 0x0}},
{Type: uint16(ctaProtoDstPort), Data: []byte{0x0, 0xff}}}},
}},
{Type: uint16(ctaTupleReply), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaTupleIP), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaIPv4Src), Data: []byte{0x1, 0x2, 0x3, 0x4}},
{Type: uint16(ctaIPv4Dst), Data: []byte{0x4, 0x3, 0x2, 0x1}}}},
{Type: uint16(ctaTupleProto), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaProtoNum), Data: []byte{0x6}},
{Type: uint16(ctaProtoSrcPort), Data: []byte{0xff, 0x0}},
{Type: uint16(ctaProtoDstPort), Data: []byte{0x0, 0xff}}}}}},
{Type: uint16(ctaTimeout), Data: []byte{0x0, 0x0, 0x0, 0x7b}},
{Type: uint16(ctaStatus), Data: []byte{0x0, 0x0, 0x4, 0xd2}},
{Type: uint16(ctaMark), Data: []byte{0x0, 0x0, 0x12, 0x34}},
{Type: uint16(ctaZone), Data: []byte{0x0, 0x2}},
{Type: uint16(ctaProtoInfo), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaProtoInfoTCP), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaProtoInfoTCPState), Data: []byte{0x2a}},
{Type: uint16(ctaProtoInfoTCPWScaleOriginal), Data: []byte{0x0}},
{Type: uint16(ctaProtoInfoTCPWScaleReply), Data: []byte{0x0}}}}}},
{Type: uint16(ctaHelp), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaHelpName), Data: []byte{0x66, 0x74, 0x70}}}},
{Type: uint16(ctaTupleMaster), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaTupleIP), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaIPv4Src), Data: []byte{0x1, 0x2, 0x3, 0x4}},
{Type: uint16(ctaIPv4Dst), Data: []byte{0x4, 0x3, 0x2, 0x1}}}},
{Type: uint16(ctaTupleProto), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaProtoNum), Data: []byte{0x6}},
{Type: uint16(ctaProtoSrcPort), Data: []byte{0xff, 0x0}},
{Type: uint16(ctaProtoDstPort), Data: []byte{0x0, 0xff}}}}}},
{Type: uint16(ctaSeqAdjOrig), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaSeqAdjCorrectionPos), Data: []byte{0x0, 0x0, 0x0, 0x1}},
{Type: uint16(ctaSeqAdjOffsetBefore), Data: []byte{0x0, 0x0, 0x0, 0x2}},
{Type: uint16(ctaSeqAdjOffsetAfter), Data: []byte{0x0, 0x0, 0x0, 0x3}}}},
{Type: uint16(ctaSeqAdjReply), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaSeqAdjCorrectionPos), Data: []byte{0x0, 0x0, 0x0, 0x5}},
{Type: uint16(ctaSeqAdjOffsetBefore), Data: []byte{0x0, 0x0, 0x0, 0x6}},
{Type: uint16(ctaSeqAdjOffsetAfter), Data: []byte{0x0, 0x0, 0x0, 0x7}}}},
{Type: uint16(ctaSynProxy), Nested: true, Children: []netfilter.Attribute{
{Type: uint16(ctaSynProxyISN), Data: []byte{0x12, 0x34, 0x56, 0x78}},
{Type: uint16(ctaSynProxyITS), Data: []byte{0x87, 0x65, 0x43, 0x21}},
{Type: uint16(ctaSynProxyTSOff), Data: []byte{0xab, 0xcd, 0xef, 0x0}}}},
{Type: uint16(ctaLabels), Data: []byte{0x13, 0x37}},
{Type: uint16(ctaLabelsMask), Data: []byte{0xff, 0xff}}}

assert.Equal(t, attrs, want)

// Can marshal with either orig or reply tuple available
_, err = Flow{TupleOrig: flowIPPT}.marshal()
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ require (
github.com/stretchr/testify v1.8.1
github.com/ti-mo/netfilter v0.5.0
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc
golang.org/x/sys v0.2.0
golang.org/x/sys v0.13.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/josharian/native v1.0.0 // indirect
github.com/mdlayher/socket v0.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.1.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ github.com/ti-mo/netfilter v0.5.0 h1:MZmsUw5bFRecOb0AeyjOPxTHg4UxYzyEs0Ek/6Lxoy8
github.com/ti-mo/netfilter v0.5.0/go.mod h1:nt+8B9hx/QpqHr7Hazq+2qMCCA8u2OTkyc/7+U9ARz8=
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc h1:R83G5ikgLMxrBvLh22JhdfI8K6YXEPHx5P03Uu3DRs4=
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI=
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down