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

Fix Fib parsing #296

Merged
merged 1 commit into from
Jan 16, 2025
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
2 changes: 2 additions & 0 deletions expr/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ func exprFromName(name string) Any {
e = &SecMark{}
case "cttimeout":
e = &CtTimeout{}
case "fib":
e = &Fib{}
}
return e
}
Expand Down
23 changes: 14 additions & 9 deletions expr/fib.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,22 @@ func (e *Fib) unmarshal(fam byte, data []byte) error {
e.Register = ad.Uint32()
case unix.NFTA_FIB_RESULT:
result := ad.Uint32()
e.ResultOIF = (result & unix.NFT_FIB_RESULT_OIF) == 1
e.ResultOIFNAME = (result & unix.NFT_FIB_RESULT_OIFNAME) == 1
e.ResultADDRTYPE = (result & unix.NFT_FIB_RESULT_ADDRTYPE) == 1
switch result {
case unix.NFT_FIB_RESULT_OIF:
e.ResultOIF = true
case unix.NFT_FIB_RESULT_OIFNAME:
e.ResultOIFNAME = true
case unix.NFT_FIB_RESULT_ADDRTYPE:
e.ResultADDRTYPE = true
}
case unix.NFTA_FIB_FLAGS:
flags := ad.Uint32()
e.FlagSADDR = (flags & unix.NFTA_FIB_F_SADDR) == 1
e.FlagDADDR = (flags & unix.NFTA_FIB_F_DADDR) == 1
e.FlagMARK = (flags & unix.NFTA_FIB_F_MARK) == 1
e.FlagIIF = (flags & unix.NFTA_FIB_F_IIF) == 1
e.FlagOIF = (flags & unix.NFTA_FIB_F_OIF) == 1
e.FlagPRESENT = (flags & unix.NFTA_FIB_F_PRESENT) == 1
e.FlagSADDR = (flags & unix.NFTA_FIB_F_SADDR) != 0
e.FlagDADDR = (flags & unix.NFTA_FIB_F_DADDR) != 0
e.FlagMARK = (flags & unix.NFTA_FIB_F_MARK) != 0
e.FlagIIF = (flags & unix.NFTA_FIB_F_IIF) != 0
e.FlagOIF = (flags & unix.NFTA_FIB_F_OIF) != 0
e.FlagPRESENT = (flags & unix.NFTA_FIB_F_PRESENT) != 0
}
}
return ad.Err()
Expand Down
2 changes: 1 addition & 1 deletion expr/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (l *Limit) unmarshal(fam byte, data []byte) error {
return fmt.Errorf("expr: invalid limit type %d", l.Type)
}
case unix.NFTA_LIMIT_FLAGS:
l.Over = (ad.Uint32() & unix.NFT_LIMIT_F_INV) == 1
l.Over = (ad.Uint32() & unix.NFT_LIMIT_F_INV) != 0
default:
return errors.New("expr: unhandled limit netlink attribute")
}
Expand Down
2 changes: 1 addition & 1 deletion expr/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (q *Quota) unmarshal(fam byte, data []byte) error {
case unix.NFTA_QUOTA_CONSUMED:
q.Consumed = ad.Uint64()
case unix.NFTA_QUOTA_FLAGS:
q.Over = (ad.Uint32() & unix.NFT_QUOTA_F_INV) == 1
q.Over = (ad.Uint32() & unix.NFT_QUOTA_F_INV) != 0
}
}
return ad.Err()
Expand Down
55 changes: 55 additions & 0 deletions nftables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6387,6 +6387,61 @@ func TestFib(t *testing.T) {
}
}

func TestFibSystem(t *testing.T) {
c, newNS := nftest.OpenSystemConn(t, *enableSysTests)
defer nftest.CleanupSystemConn(t, newNS)
c.FlushRuleset()
defer c.FlushRuleset()

filter := c.AddTable(&nftables.Table{
Family: nftables.TableFamilyIPv4,
Name: "filter",
})

chain := c.AddChain(&nftables.Chain{
Name: "test-chain",
Table: filter,
})

expect := &expr.Fib{
Register: 1,
FlagDADDR: true,
ResultADDRTYPE: true,
}

c.AddRule(&nftables.Rule{
Table: filter,
Chain: chain,
Exprs: []expr.Any{expect},
})

if err := c.Flush(); err != nil {
t.Fatalf("c.Flush() failed with error %+v", err)
}

rules, err := c.GetRules(filter, chain)
if err != nil {
t.Fatalf("GetRules failed: %v", err)
}

if got, want := len(rules), 1; got != want {
t.Fatalf("unexpected number of rules: got %d, want %d", got, want)
}

if got, want := len(rules[0].Exprs), 1; got != want {
t.Fatalf("unexpected number of exprs: got %d, want %d", got, want)
}

fib := rules[0].Exprs[0].(*expr.Fib)
if got, want := fib.FlagDADDR, expect.FlagDADDR; got != want {
t.Errorf("fib daddr not equal: got %+v, want %+v", got, want)
}

if got, want := fib.ResultADDRTYPE, expect.ResultADDRTYPE; got != want {
t.Errorf("fib addr type not equal: got %+v, want %+v", got, want)
}
}

func TestNumgen(t *testing.T) {
tests := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (q *QuotaObj) unmarshal(ad *netlink.AttributeDecoder) error {
case unix.NFTA_QUOTA_CONSUMED:
q.Consumed = ad.Uint64()
case unix.NFTA_QUOTA_FLAGS:
q.Over = (ad.Uint32() & unix.NFT_QUOTA_F_INV) == 1
q.Over = (ad.Uint32() & unix.NFT_QUOTA_F_INV) != 0
}
}
return nil
Expand Down
Loading