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