Merge tag 'linux-watchdog-5.2-rc1' of git://www.linux-watchdog.org/linux-watchdog
[linux-2.6-block.git] / net / netfilter / nft_hash.c
CommitLineData
cb1b69b0
LGL
1/*
2 * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
3 *
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.
7 *
8 */
9
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>
19
511040ee 20struct nft_jhash {
cb1b69b0
LGL
21 enum nft_registers sreg:8;
22 enum nft_registers dreg:8;
23 u8 len;
79e09ef9 24 bool autogen_seed:1;
cb1b69b0
LGL
25 u32 modulus;
26 u32 seed;
70ca767e 27 u32 offset;
cb1b69b0
LGL
28};
29
511040ee
LGL
30static void nft_jhash_eval(const struct nft_expr *expr,
31 struct nft_regs *regs,
32 const struct nft_pktinfo *pkt)
cb1b69b0 33{
511040ee 34 struct nft_jhash *priv = nft_expr_priv(expr);
cb1b69b0 35 const void *data = &regs->data[priv->sreg];
70ca767e 36 u32 h;
cb1b69b0 37
b9ccc07e
LGL
38 h = reciprocal_scale(jhash(data, priv->len, priv->seed),
39 priv->modulus);
40
70ca767e 41 regs->data[priv->dreg] = h + priv->offset;
cb1b69b0
LGL
42}
43
3206cade
LGL
44struct nft_symhash {
45 enum nft_registers dreg:8;
46 u32 modulus;
47 u32 offset;
48};
49
50static void nft_symhash_eval(const struct nft_expr *expr,
51 struct nft_regs *regs,
52 const struct nft_pktinfo *pkt)
53{
54 struct nft_symhash *priv = nft_expr_priv(expr);
55 struct sk_buff *skb = pkt->skb;
56 u32 h;
57
58 h = reciprocal_scale(__skb_get_hash_symmetric(skb), priv->modulus);
59
60 regs->data[priv->dreg] = h + priv->offset;
61}
62
a5e57336 63static const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = {
cb1b69b0
LGL
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 },
5751e175 69 [NFTA_HASH_OFFSET] = { .type = NLA_U32 },
3206cade 70 [NFTA_HASH_TYPE] = { .type = NLA_U32 },
cb1b69b0
LGL
71};
72
511040ee
LGL
73static int nft_jhash_init(const struct nft_ctx *ctx,
74 const struct nft_expr *expr,
75 const struct nlattr * const tb[])
cb1b69b0 76{
511040ee 77 struct nft_jhash *priv = nft_expr_priv(expr);
cb1b69b0 78 u32 len;
abd66e9f 79 int err;
cb1b69b0
LGL
80
81 if (!tb[NFTA_HASH_SREG] ||
82 !tb[NFTA_HASH_DREG] ||
83 !tb[NFTA_HASH_LEN] ||
cb1b69b0
LGL
84 !tb[NFTA_HASH_MODULUS])
85 return -EINVAL;
86
70ca767e
LGL
87 if (tb[NFTA_HASH_OFFSET])
88 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
89
cb1b69b0
LGL
90 priv->sreg = nft_parse_register(tb[NFTA_HASH_SREG]);
91 priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]);
92
abd66e9f
LGL
93 err = nft_parse_u32_check(tb[NFTA_HASH_LEN], U8_MAX, &len);
94 if (err < 0)
95 return err;
96 if (len == 0)
cb1b69b0
LGL
97 return -ERANGE;
98
99 priv->len = len;
100
101 priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
75e72f05 102 if (priv->modulus < 1)
cb1b69b0
LGL
103 return -ERANGE;
104
14e2dee0 105 if (priv->offset + priv->modulus - 1 < priv->offset)
70ca767e
LGL
106 return -EOVERFLOW;
107
79e09ef9 108 if (tb[NFTA_HASH_SEED]) {
f86dab3a 109 priv->seed = ntohl(nla_get_be32(tb[NFTA_HASH_SEED]));
79e09ef9
LZ
110 } else {
111 priv->autogen_seed = true;
f86dab3a 112 get_random_bytes(&priv->seed, sizeof(priv->seed));
79e09ef9 113 }
cb1b69b0
LGL
114
115 return nft_validate_register_load(priv->sreg, len) &&
116 nft_validate_register_store(ctx, priv->dreg, NULL,
117 NFT_DATA_VALUE, sizeof(u32));
118}
119
3206cade
LGL
120static int nft_symhash_init(const struct nft_ctx *ctx,
121 const struct nft_expr *expr,
122 const struct nlattr * const tb[])
123{
124 struct nft_symhash *priv = nft_expr_priv(expr);
125
126 if (!tb[NFTA_HASH_DREG] ||
127 !tb[NFTA_HASH_MODULUS])
128 return -EINVAL;
129
130 if (tb[NFTA_HASH_OFFSET])
131 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
132
133 priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]);
134
135 priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
136 if (priv->modulus <= 1)
137 return -ERANGE;
138
139 if (priv->offset + priv->modulus - 1 < priv->offset)
140 return -EOVERFLOW;
141
142 return nft_validate_register_store(ctx, priv->dreg, NULL,
143 NFT_DATA_VALUE, sizeof(u32));
144}
145
511040ee
LGL
146static int nft_jhash_dump(struct sk_buff *skb,
147 const struct nft_expr *expr)
cb1b69b0 148{
511040ee 149 const struct nft_jhash *priv = nft_expr_priv(expr);
cb1b69b0
LGL
150
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;
7073b16f 155 if (nla_put_be32(skb, NFTA_HASH_LEN, htonl(priv->len)))
cb1b69b0 156 goto nla_put_failure;
7073b16f 157 if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus)))
cb1b69b0 158 goto nla_put_failure;
79e09ef9
LZ
159 if (!priv->autogen_seed &&
160 nla_put_be32(skb, NFTA_HASH_SEED, htonl(priv->seed)))
cb1b69b0 161 goto nla_put_failure;
70ca767e
LGL
162 if (priv->offset != 0)
163 if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset)))
164 goto nla_put_failure;
3206cade
LGL
165 if (nla_put_be32(skb, NFTA_HASH_TYPE, htonl(NFT_HASH_JENKINS)))
166 goto nla_put_failure;
167 return 0;
168
169nla_put_failure:
170 return -1;
171}
172
173static int nft_symhash_dump(struct sk_buff *skb,
174 const struct nft_expr *expr)
175{
176 const struct nft_symhash *priv = nft_expr_priv(expr);
177
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;
cb1b69b0
LGL
187 return 0;
188
189nla_put_failure:
190 return -1;
191}
192
193static struct nft_expr_type nft_hash_type;
511040ee 194static const struct nft_expr_ops nft_jhash_ops = {
cb1b69b0 195 .type = &nft_hash_type,
511040ee
LGL
196 .size = NFT_EXPR_SIZE(sizeof(struct nft_jhash)),
197 .eval = nft_jhash_eval,
198 .init = nft_jhash_init,
199 .dump = nft_jhash_dump,
cb1b69b0
LGL
200};
201
3206cade
LGL
202static 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,
208};
209
210static const struct nft_expr_ops *
211nft_hash_select_ops(const struct nft_ctx *ctx,
212 const struct nlattr * const tb[])
213{
214 u32 type;
215
216 if (!tb[NFTA_HASH_TYPE])
217 return &nft_jhash_ops;
218
219 type = ntohl(nla_get_be32(tb[NFTA_HASH_TYPE]));
220 switch (type) {
221 case NFT_HASH_SYM:
222 return &nft_symhash_ops;
223 case NFT_HASH_JENKINS:
224 return &nft_jhash_ops;
225 default:
226 break;
227 }
228 return ERR_PTR(-EOPNOTSUPP);
229}
230
cb1b69b0
LGL
231static struct nft_expr_type nft_hash_type __read_mostly = {
232 .name = "hash",
d4ef3835 233 .select_ops = nft_hash_select_ops,
cb1b69b0
LGL
234 .policy = nft_hash_policy,
235 .maxattr = NFTA_HASH_MAX,
236 .owner = THIS_MODULE,
237};
238
239static int __init nft_hash_module_init(void)
240{
241 return nft_register_expr(&nft_hash_type);
242}
243
244static void __exit nft_hash_module_exit(void)
245{
246 nft_unregister_expr(&nft_hash_type);
247}
248
249module_init(nft_hash_module_init);
250module_exit(nft_hash_module_exit);
251
252MODULE_LICENSE("GPL");
253MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
254MODULE_ALIAS_NFT_EXPR("hash");