4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/netlink.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter/nf_tables.h>
16 #include <net/netfilter/nf_tables.h>
17 #include <net/netfilter/nf_tables_core.h>
18 #include <linux/jhash.h>
21 enum nft_registers sreg:8;
22 enum nft_registers dreg:8;
29 static void nft_hash_eval(const struct nft_expr *expr,
30 struct nft_regs *regs,
31 const struct nft_pktinfo *pkt)
33 struct nft_hash *priv = nft_expr_priv(expr);
34 const void *data = ®s->data[priv->sreg];
37 h = reciprocal_scale(jhash(data, priv->len, priv->seed), priv->modulus);
38 regs->data[priv->dreg] = h + priv->offset;
41 static const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = {
42 [NFTA_HASH_SREG] = { .type = NLA_U32 },
43 [NFTA_HASH_DREG] = { .type = NLA_U32 },
44 [NFTA_HASH_LEN] = { .type = NLA_U32 },
45 [NFTA_HASH_MODULUS] = { .type = NLA_U32 },
46 [NFTA_HASH_SEED] = { .type = NLA_U32 },
47 [NFTA_HASH_OFFSET] = { .type = NLA_U32 },
50 static int nft_hash_init(const struct nft_ctx *ctx,
51 const struct nft_expr *expr,
52 const struct nlattr * const tb[])
54 struct nft_hash *priv = nft_expr_priv(expr);
58 if (!tb[NFTA_HASH_SREG] ||
59 !tb[NFTA_HASH_DREG] ||
61 !tb[NFTA_HASH_MODULUS])
64 if (tb[NFTA_HASH_OFFSET])
65 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
67 priv->sreg = nft_parse_register(tb[NFTA_HASH_SREG]);
68 priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]);
70 err = nft_parse_u32_check(tb[NFTA_HASH_LEN], U8_MAX, &len);
78 priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
79 if (priv->modulus <= 1)
82 if (priv->offset + priv->modulus - 1 < priv->offset)
85 if (tb[NFTA_HASH_SEED])
86 priv->seed = ntohl(nla_get_be32(tb[NFTA_HASH_SEED]));
88 get_random_bytes(&priv->seed, sizeof(priv->seed));
90 return nft_validate_register_load(priv->sreg, len) &&
91 nft_validate_register_store(ctx, priv->dreg, NULL,
92 NFT_DATA_VALUE, sizeof(u32));
95 static int nft_hash_dump(struct sk_buff *skb,
96 const struct nft_expr *expr)
98 const struct nft_hash *priv = nft_expr_priv(expr);
100 if (nft_dump_register(skb, NFTA_HASH_SREG, priv->sreg))
101 goto nla_put_failure;
102 if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg))
103 goto nla_put_failure;
104 if (nla_put_be32(skb, NFTA_HASH_LEN, htonl(priv->len)))
105 goto nla_put_failure;
106 if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus)))
107 goto nla_put_failure;
108 if (nla_put_be32(skb, NFTA_HASH_SEED, htonl(priv->seed)))
109 goto nla_put_failure;
110 if (priv->offset != 0)
111 if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset)))
112 goto nla_put_failure;
119 static struct nft_expr_type nft_hash_type;
120 static const struct nft_expr_ops nft_hash_ops = {
121 .type = &nft_hash_type,
122 .size = NFT_EXPR_SIZE(sizeof(struct nft_hash)),
123 .eval = nft_hash_eval,
124 .init = nft_hash_init,
125 .dump = nft_hash_dump,
128 static struct nft_expr_type nft_hash_type __read_mostly = {
130 .ops = &nft_hash_ops,
131 .policy = nft_hash_policy,
132 .maxattr = NFTA_HASH_MAX,
133 .owner = THIS_MODULE,
136 static int __init nft_hash_module_init(void)
138 return nft_register_expr(&nft_hash_type);
141 static void __exit nft_hash_module_exit(void)
143 nft_unregister_expr(&nft_hash_type);
146 module_init(nft_hash_module_init);
147 module_exit(nft_hash_module_exit);
149 MODULE_LICENSE("GPL");
151 MODULE_ALIAS_NFT_EXPR("hash");