2 * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
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;
30 static void nft_jhash_eval(const struct nft_expr *expr,
31 struct nft_regs *regs,
32 const struct nft_pktinfo *pkt)
34 struct nft_jhash *priv = nft_expr_priv(expr);
35 const void *data = ®s->data[priv->sreg];
38 h = reciprocal_scale(jhash(data, priv->len, priv->seed),
41 regs->data[priv->dreg] = h + priv->offset;
45 enum nft_registers dreg:8;
50 static void nft_symhash_eval(const struct nft_expr *expr,
51 struct nft_regs *regs,
52 const struct nft_pktinfo *pkt)
54 struct nft_symhash *priv = nft_expr_priv(expr);
55 struct sk_buff *skb = pkt->skb;
58 h = reciprocal_scale(__skb_get_hash_symmetric(skb), priv->modulus);
60 regs->data[priv->dreg] = h + priv->offset;
63 static const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = {
64 [NFTA_HASH_SREG] = { .type = NLA_U32 },
65 [NFTA_HASH_DREG] = { .type = NLA_U32 },
66 [NFTA_HASH_LEN] = { .type = NLA_U32 },
67 [NFTA_HASH_MODULUS] = { .type = NLA_U32 },
68 [NFTA_HASH_SEED] = { .type = NLA_U32 },
69 [NFTA_HASH_OFFSET] = { .type = NLA_U32 },
70 [NFTA_HASH_TYPE] = { .type = NLA_U32 },
73 static int nft_jhash_init(const struct nft_ctx *ctx,
74 const struct nft_expr *expr,
75 const struct nlattr * const tb[])
77 struct nft_jhash *priv = nft_expr_priv(expr);
81 if (!tb[NFTA_HASH_SREG] ||
82 !tb[NFTA_HASH_DREG] ||
84 !tb[NFTA_HASH_MODULUS])
87 if (tb[NFTA_HASH_OFFSET])
88 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
90 priv->sreg = nft_parse_register(tb[NFTA_HASH_SREG]);
91 priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]);
93 err = nft_parse_u32_check(tb[NFTA_HASH_LEN], U8_MAX, &len);
101 priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
102 if (priv->modulus < 1)
105 if (priv->offset + priv->modulus - 1 < priv->offset)
108 if (tb[NFTA_HASH_SEED]) {
109 priv->seed = ntohl(nla_get_be32(tb[NFTA_HASH_SEED]));
111 priv->autogen_seed = true;
112 get_random_bytes(&priv->seed, sizeof(priv->seed));
115 return nft_validate_register_load(priv->sreg, len) &&
116 nft_validate_register_store(ctx, priv->dreg, NULL,
117 NFT_DATA_VALUE, sizeof(u32));
120 static int nft_symhash_init(const struct nft_ctx *ctx,
121 const struct nft_expr *expr,
122 const struct nlattr * const tb[])
124 struct nft_symhash *priv = nft_expr_priv(expr);
126 if (!tb[NFTA_HASH_DREG] ||
127 !tb[NFTA_HASH_MODULUS])
130 if (tb[NFTA_HASH_OFFSET])
131 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
133 priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]);
135 priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
136 if (priv->modulus <= 1)
139 if (priv->offset + priv->modulus - 1 < priv->offset)
142 return nft_validate_register_store(ctx, priv->dreg, NULL,
143 NFT_DATA_VALUE, sizeof(u32));
146 static int nft_jhash_dump(struct sk_buff *skb,
147 const struct nft_expr *expr)
149 const struct nft_jhash *priv = nft_expr_priv(expr);
151 if (nft_dump_register(skb, NFTA_HASH_SREG, priv->sreg))
152 goto nla_put_failure;
153 if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg))
154 goto nla_put_failure;
155 if (nla_put_be32(skb, NFTA_HASH_LEN, htonl(priv->len)))
156 goto nla_put_failure;
157 if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus)))
158 goto nla_put_failure;
159 if (!priv->autogen_seed &&
160 nla_put_be32(skb, NFTA_HASH_SEED, htonl(priv->seed)))
161 goto nla_put_failure;
162 if (priv->offset != 0)
163 if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset)))
164 goto nla_put_failure;
165 if (nla_put_be32(skb, NFTA_HASH_TYPE, htonl(NFT_HASH_JENKINS)))
166 goto nla_put_failure;
173 static int nft_symhash_dump(struct sk_buff *skb,
174 const struct nft_expr *expr)
176 const struct nft_symhash *priv = nft_expr_priv(expr);
178 if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg))
179 goto nla_put_failure;
180 if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus)))
181 goto nla_put_failure;
182 if (priv->offset != 0)
183 if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset)))
184 goto nla_put_failure;
185 if (nla_put_be32(skb, NFTA_HASH_TYPE, htonl(NFT_HASH_SYM)))
186 goto nla_put_failure;
193 static struct nft_expr_type nft_hash_type;
194 static const struct nft_expr_ops nft_jhash_ops = {
195 .type = &nft_hash_type,
196 .size = NFT_EXPR_SIZE(sizeof(struct nft_jhash)),
197 .eval = nft_jhash_eval,
198 .init = nft_jhash_init,
199 .dump = nft_jhash_dump,
202 static const struct nft_expr_ops nft_symhash_ops = {
203 .type = &nft_hash_type,
204 .size = NFT_EXPR_SIZE(sizeof(struct nft_symhash)),
205 .eval = nft_symhash_eval,
206 .init = nft_symhash_init,
207 .dump = nft_symhash_dump,
210 static const struct nft_expr_ops *
211 nft_hash_select_ops(const struct nft_ctx *ctx,
212 const struct nlattr * const tb[])
216 if (!tb[NFTA_HASH_TYPE])
217 return &nft_jhash_ops;
219 type = ntohl(nla_get_be32(tb[NFTA_HASH_TYPE]));
222 return &nft_symhash_ops;
223 case NFT_HASH_JENKINS:
224 return &nft_jhash_ops;
228 return ERR_PTR(-EOPNOTSUPP);
231 static struct nft_expr_type nft_hash_type __read_mostly = {
233 .select_ops = nft_hash_select_ops,
234 .policy = nft_hash_policy,
235 .maxattr = NFTA_HASH_MAX,
236 .owner = THIS_MODULE,
239 static int __init nft_hash_module_init(void)
241 return nft_register_expr(&nft_hash_type);
244 static void __exit nft_hash_module_exit(void)
246 nft_unregister_expr(&nft_hash_type);
249 module_init(nft_hash_module_init);
250 module_exit(nft_hash_module_exit);
252 MODULE_LICENSE("GPL");
253 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
254 MODULE_ALIAS_NFT_EXPR("hash");