Skip to content

Commit d27cc52

Browse files
authored
hash: Fix hash seed conditional (#234)
Fix the marshall of the hash seed to be conditional, only if it is explicitly set, we need to add it to the kernel as stated on the libnftl and nftables projects. Refence: https://git.netfilter.org/nftables/tree/src/netlink_linearize.c?id=25e7b99cc450490c38becb03d8bddd0199cfd3f9#n174 Otherwise, having a hash expression similar to this: ``` ip daddr set jhash tcp sport mod 2 seed 0x0 map { 0 : 192.168.0.1, 1 : 192.168.2.2 } ``` end up setting only the first IP and ignoring the second one. Signed-off-by: Rafael Campos <[email protected]>
1 parent 8a10f68 commit d27cc52

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

expr/hash.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,22 @@ type Hash struct {
4141
}
4242

4343
func (e *Hash) marshal(fam byte) ([]byte, error) {
44-
data, err := netlink.MarshalAttributes([]netlink.Attribute{
44+
hashAttrs := []netlink.Attribute{
4545
{Type: unix.NFTA_HASH_SREG, Data: binaryutil.BigEndian.PutUint32(uint32(e.SourceRegister))},
4646
{Type: unix.NFTA_HASH_DREG, Data: binaryutil.BigEndian.PutUint32(uint32(e.DestRegister))},
4747
{Type: unix.NFTA_HASH_LEN, Data: binaryutil.BigEndian.PutUint32(uint32(e.Length))},
4848
{Type: unix.NFTA_HASH_MODULUS, Data: binaryutil.BigEndian.PutUint32(uint32(e.Modulus))},
49-
{Type: unix.NFTA_HASH_SEED, Data: binaryutil.BigEndian.PutUint32(uint32(e.Seed))},
49+
}
50+
if e.Seed != 0 {
51+
hashAttrs = append(hashAttrs, netlink.Attribute{
52+
Type: unix.NFTA_HASH_SEED, Data: binaryutil.BigEndian.PutUint32(uint32(e.Seed)),
53+
})
54+
}
55+
hashAttrs = append(hashAttrs, []netlink.Attribute{
5056
{Type: unix.NFTA_HASH_OFFSET, Data: binaryutil.BigEndian.PutUint32(uint32(e.Offset))},
5157
{Type: unix.NFTA_HASH_TYPE, Data: binaryutil.BigEndian.PutUint32(uint32(e.Type))},
52-
})
58+
}...)
59+
data, err := netlink.MarshalAttributes(hashAttrs)
5360
if err != nil {
5461
return nil, err
5562
}

0 commit comments

Comments
 (0)