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