1 // SPDX-License-Identifier: GPL-2.0-only
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/netlink.h>
10 #include <linux/netfilter.h>
11 #include <linux/netfilter/nf_tables.h>
12 #include <net/netfilter/nf_tables.h>
13 #include <net/netfilter/nf_tables_core.h>
14 #include <linux/jhash.h>
26 static void nft_jhash_eval(const struct nft_expr *expr,
27 struct nft_regs *regs,
28 const struct nft_pktinfo *pkt)
30 struct nft_jhash *priv = nft_expr_priv(expr);
31 const void *data = ®s->data[priv->sreg];
34 h = reciprocal_scale(jhash(data, priv->len, priv->seed),
37 regs->data[priv->dreg] = h + priv->offset;
46 static void nft_symhash_eval(const struct nft_expr *expr,
47 struct nft_regs *regs,
48 const struct nft_pktinfo *pkt)
50 struct nft_symhash *priv = nft_expr_priv(expr);
51 struct sk_buff *skb = pkt->skb;
54 h = reciprocal_scale(__skb_get_hash_symmetric(skb), priv->modulus);
56 regs->data[priv->dreg] = h + priv->offset;
59 static const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = {
60 [NFTA_HASH_SREG] = { .type = NLA_U32 },
61 [NFTA_HASH_DREG] = { .type = NLA_U32 },
62 [NFTA_HASH_LEN] = { .type = NLA_U32 },
63 [NFTA_HASH_MODULUS] = { .type = NLA_U32 },
64 [NFTA_HASH_SEED] = { .type = NLA_U32 },
65 [NFTA_HASH_OFFSET] = { .type = NLA_U32 },
66 [NFTA_HASH_TYPE] = { .type = NLA_U32 },
69 static int nft_jhash_init(const struct nft_ctx *ctx,
70 const struct nft_expr *expr,
71 const struct nlattr * const tb[])
73 struct nft_jhash *priv = nft_expr_priv(expr);
77 if (!tb[NFTA_HASH_SREG] ||
78 !tb[NFTA_HASH_DREG] ||
80 !tb[NFTA_HASH_MODULUS])
83 if (tb[NFTA_HASH_OFFSET])
84 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
86 err = nft_parse_u32_check(tb[NFTA_HASH_LEN], U8_MAX, &len);
94 err = nft_parse_register_load(tb[NFTA_HASH_SREG], &priv->sreg, len);
98 priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
99 if (priv->modulus < 1)
102 if (priv->offset + priv->modulus - 1 < priv->offset)
105 if (tb[NFTA_HASH_SEED]) {
106 priv->seed = ntohl(nla_get_be32(tb[NFTA_HASH_SEED]));
108 priv->autogen_seed = true;
109 get_random_bytes(&priv->seed, sizeof(priv->seed));
112 return nft_parse_register_store(ctx, tb[NFTA_HASH_DREG], &priv->dreg,
113 NULL, NFT_DATA_VALUE, sizeof(u32));
116 static int nft_symhash_init(const struct nft_ctx *ctx,
117 const struct nft_expr *expr,
118 const struct nlattr * const tb[])
120 struct nft_symhash *priv = nft_expr_priv(expr);
122 if (!tb[NFTA_HASH_DREG] ||
123 !tb[NFTA_HASH_MODULUS])
126 if (tb[NFTA_HASH_OFFSET])
127 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
129 priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
130 if (priv->modulus < 1)
133 if (priv->offset + priv->modulus - 1 < priv->offset)
136 return nft_parse_register_store(ctx, tb[NFTA_HASH_DREG],
137 &priv->dreg, NULL, NFT_DATA_VALUE,
141 static int nft_jhash_dump(struct sk_buff *skb,
142 const struct nft_expr *expr, bool reset)
144 const struct nft_jhash *priv = nft_expr_priv(expr);
146 if (nft_dump_register(skb, NFTA_HASH_SREG, priv->sreg))
147 goto nla_put_failure;
148 if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg))
149 goto nla_put_failure;
150 if (nla_put_be32(skb, NFTA_HASH_LEN, htonl(priv->len)))
151 goto nla_put_failure;
152 if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus)))
153 goto nla_put_failure;
154 if (!priv->autogen_seed &&
155 nla_put_be32(skb, NFTA_HASH_SEED, htonl(priv->seed)))
156 goto nla_put_failure;
157 if (priv->offset != 0)
158 if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset)))
159 goto nla_put_failure;
160 if (nla_put_be32(skb, NFTA_HASH_TYPE, htonl(NFT_HASH_JENKINS)))
161 goto nla_put_failure;
168 static bool nft_jhash_reduce(struct nft_regs_track *track,
169 const struct nft_expr *expr)
171 const struct nft_jhash *priv = nft_expr_priv(expr);
173 nft_reg_track_cancel(track, priv->dreg, sizeof(u32));
178 static int nft_symhash_dump(struct sk_buff *skb,
179 const struct nft_expr *expr, bool reset)
181 const struct nft_symhash *priv = nft_expr_priv(expr);
183 if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg))
184 goto nla_put_failure;
185 if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus)))
186 goto nla_put_failure;
187 if (priv->offset != 0)
188 if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset)))
189 goto nla_put_failure;
190 if (nla_put_be32(skb, NFTA_HASH_TYPE, htonl(NFT_HASH_SYM)))
191 goto nla_put_failure;
198 static bool nft_symhash_reduce(struct nft_regs_track *track,
199 const struct nft_expr *expr)
201 struct nft_symhash *priv = nft_expr_priv(expr);
202 struct nft_symhash *symhash;
204 if (!nft_reg_track_cmp(track, expr, priv->dreg)) {
205 nft_reg_track_update(track, expr, priv->dreg, sizeof(u32));
209 symhash = nft_expr_priv(track->regs[priv->dreg].selector);
210 if (priv->offset != symhash->offset ||
211 priv->modulus != symhash->modulus) {
212 nft_reg_track_update(track, expr, priv->dreg, sizeof(u32));
216 if (!track->regs[priv->dreg].bitwise)
222 static struct nft_expr_type nft_hash_type;
223 static const struct nft_expr_ops nft_jhash_ops = {
224 .type = &nft_hash_type,
225 .size = NFT_EXPR_SIZE(sizeof(struct nft_jhash)),
226 .eval = nft_jhash_eval,
227 .init = nft_jhash_init,
228 .dump = nft_jhash_dump,
229 .reduce = nft_jhash_reduce,
232 static const struct nft_expr_ops nft_symhash_ops = {
233 .type = &nft_hash_type,
234 .size = NFT_EXPR_SIZE(sizeof(struct nft_symhash)),
235 .eval = nft_symhash_eval,
236 .init = nft_symhash_init,
237 .dump = nft_symhash_dump,
238 .reduce = nft_symhash_reduce,
241 static const struct nft_expr_ops *
242 nft_hash_select_ops(const struct nft_ctx *ctx,
243 const struct nlattr * const tb[])
247 if (!tb[NFTA_HASH_TYPE])
248 return &nft_jhash_ops;
250 type = ntohl(nla_get_be32(tb[NFTA_HASH_TYPE]));
253 return &nft_symhash_ops;
254 case NFT_HASH_JENKINS:
255 return &nft_jhash_ops;
259 return ERR_PTR(-EOPNOTSUPP);
262 static struct nft_expr_type nft_hash_type __read_mostly = {
264 .select_ops = nft_hash_select_ops,
265 .policy = nft_hash_policy,
266 .maxattr = NFTA_HASH_MAX,
267 .owner = THIS_MODULE,
270 static int __init nft_hash_module_init(void)
272 return nft_register_expr(&nft_hash_type);
275 static void __exit nft_hash_module_exit(void)
277 nft_unregister_expr(&nft_hash_type);
280 module_init(nft_hash_module_init);
281 module_exit(nft_hash_module_exit);
283 MODULE_LICENSE("GPL");
285 MODULE_ALIAS_NFT_EXPR("hash");
286 MODULE_DESCRIPTION("Netfilter nftables hash module");