Fix Fib parsing (#296)

This commit is contained in:
turekt 2025-01-16 09:15:33 +01:00 committed by GitHub
parent c9a67ccd0c
commit 1c789726cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 74 additions and 12 deletions

View File

@ -207,6 +207,8 @@ func exprFromName(name string) Any {
e = &SecMark{}
case "cttimeout":
e = &CtTimeout{}
case "fib":
e = &Fib{}
}
return e
}

View File

@ -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()

View File

@ -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")
}

View File

@ -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()

View File

@ -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

View File

@ -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