Skip to content

Commit 70ca767

Browse files
Laura Garcia Liebanaummakynes
authored andcommitted
netfilter: nft_hash: Add hash offset value
Add support to pass through an offset to the hash value. With this feature, the sysadmin is able to generate a hash with a given offset value. Example: meta mark set jhash ip saddr mod 2 seed 0xabcd offset 100 This option generates marks according to the source address from 100 to 101. Signed-off-by: Laura Garcia Liebana <[email protected]>
1 parent 3c15b8e commit 70ca767

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

include/uapi/linux/netfilter/nf_tables.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ enum nft_meta_keys {
731731
* @NFTA_HASH_LEN: source data length (NLA_U32)
732732
* @NFTA_HASH_MODULUS: modulus value (NLA_U32)
733733
* @NFTA_HASH_SEED: seed value (NLA_U32)
734+
* @NFTA_HASH_OFFSET: add this offset value to hash result (NLA_U32)
734735
*/
735736
enum nft_hash_attributes {
736737
NFTA_HASH_UNSPEC,
@@ -739,6 +740,7 @@ enum nft_hash_attributes {
739740
NFTA_HASH_LEN,
740741
NFTA_HASH_MODULUS,
741742
NFTA_HASH_SEED,
743+
NFTA_HASH_OFFSET,
742744
__NFTA_HASH_MAX,
743745
};
744746
#define NFTA_HASH_MAX (__NFTA_HASH_MAX - 1)

net/netfilter/nft_hash.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct nft_hash {
2323
u8 len;
2424
u32 modulus;
2525
u32 seed;
26+
u32 offset;
2627
};
2728

2829
static void nft_hash_eval(const struct nft_expr *expr,
@@ -31,10 +32,10 @@ static void nft_hash_eval(const struct nft_expr *expr,
3132
{
3233
struct nft_hash *priv = nft_expr_priv(expr);
3334
const void *data = &regs->data[priv->sreg];
35+
u32 h;
3436

35-
regs->data[priv->dreg] =
36-
reciprocal_scale(jhash(data, priv->len, priv->seed),
37-
priv->modulus);
37+
h = reciprocal_scale(jhash(data, priv->len, priv->seed), priv->modulus);
38+
regs->data[priv->dreg] = h + priv->offset;
3839
}
3940

4041
static const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = {
@@ -59,6 +60,9 @@ static int nft_hash_init(const struct nft_ctx *ctx,
5960
!tb[NFTA_HASH_MODULUS])
6061
return -EINVAL;
6162

63+
if (tb[NFTA_HASH_OFFSET])
64+
priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
65+
6266
priv->sreg = nft_parse_register(tb[NFTA_HASH_SREG]);
6367
priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]);
6468

@@ -72,6 +76,9 @@ static int nft_hash_init(const struct nft_ctx *ctx,
7276
if (priv->modulus <= 1)
7377
return -ERANGE;
7478

79+
if (priv->offset + priv->modulus - 1 < U32_MAX)
80+
return -EOVERFLOW;
81+
7582
priv->seed = ntohl(nla_get_be32(tb[NFTA_HASH_SEED]));
7683

7784
return nft_validate_register_load(priv->sreg, len) &&
@@ -94,7 +101,9 @@ static int nft_hash_dump(struct sk_buff *skb,
94101
goto nla_put_failure;
95102
if (nla_put_be32(skb, NFTA_HASH_SEED, htonl(priv->seed)))
96103
goto nla_put_failure;
97-
104+
if (priv->offset != 0)
105+
if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset)))
106+
goto nla_put_failure;
98107
return 0;
99108

100109
nla_put_failure:

0 commit comments

Comments
 (0)