net: netfilter: Use list_{next/prev}_entry instead of list_entry
[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;
24 u32 modulus;
25 u32 seed;
70ca767e 26 u32 offset;
cb1b69b0
LGL
27};
28
511040ee
LGL
29static void nft_jhash_eval(const struct nft_expr *expr,
30 struct nft_regs *regs,
31 const struct nft_pktinfo *pkt)
cb1b69b0 32{
511040ee 33 struct nft_jhash *priv = nft_expr_priv(expr);
cb1b69b0 34 const void *data = &regs->data[priv->sreg];
70ca767e 35 u32 h;
cb1b69b0 36
70ca767e
LGL
37 h = reciprocal_scale(jhash(data, priv->len, priv->seed), priv->modulus);
38 regs->data[priv->dreg] = h + priv->offset;
cb1b69b0
LGL
39}
40
3206cade
LGL
41struct nft_symhash {
42 enum nft_registers dreg:8;
43 u32 modulus;
44 u32 offset;
45};
46
47static 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
a5e57336 60static const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = {
cb1b69b0
LGL
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 },
5751e175 66 [NFTA_HASH_OFFSET] = { .type = NLA_U32 },
3206cade 67 [NFTA_HASH_TYPE] = { .type = NLA_U32 },
cb1b69b0
LGL
68};
69
511040ee
LGL
70static int nft_jhash_init(const struct nft_ctx *ctx,
71 const struct nft_expr *expr,
72 const struct nlattr * const tb[])
cb1b69b0 73{
511040ee 74 struct nft_jhash *priv = nft_expr_priv(expr);
cb1b69b0 75 u32 len;
abd66e9f 76 int err;
cb1b69b0
LGL
77
78 if (!tb[NFTA_HASH_SREG] ||
79 !tb[NFTA_HASH_DREG] ||
80 !tb[NFTA_HASH_LEN] ||
cb1b69b0
LGL
81 !tb[NFTA_HASH_MODULUS])
82 return -EINVAL;
83
70ca767e
LGL
84 if (tb[NFTA_HASH_OFFSET])
85 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
86
cb1b69b0
LGL
87 priv->sreg = nft_parse_register(tb[NFTA_HASH_SREG]);
88 priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]);
89
abd66e9f
LGL
90 err = nft_parse_u32_check(tb[NFTA_HASH_LEN], U8_MAX, &len);
91 if (err < 0)
92 return err;
93 if (len == 0)
cb1b69b0
LGL
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
14e2dee0 102 if (priv->offset + priv->modulus - 1 < priv->offset)
70ca767e
LGL
103 return -EOVERFLOW;
104
f86dab3a
PNA
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));
cb1b69b0
LGL
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
3206cade
LGL
115static 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
511040ee
LGL
141static int nft_jhash_dump(struct sk_buff *skb,
142 const struct nft_expr *expr)
cb1b69b0 143{
511040ee 144 const struct nft_jhash *priv = nft_expr_priv(expr);
cb1b69b0
LGL
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;
7073b16f 150 if (nla_put_be32(skb, NFTA_HASH_LEN, htonl(priv->len)))
cb1b69b0 151 goto nla_put_failure;
7073b16f 152 if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus)))
cb1b69b0 153 goto nla_put_failure;
7073b16f 154 if (nla_put_be32(skb, NFTA_HASH_SEED, htonl(priv->seed)))
cb1b69b0 155 goto nla_put_failure;
70ca767e
LGL
156 if (priv->offset != 0)
157 if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset)))
158 goto nla_put_failure;
3206cade
LGL
159 if (nla_put_be32(skb, NFTA_HASH_TYPE, htonl(NFT_HASH_JENKINS)))
160 goto nla_put_failure;
161 return 0;
162
163nla_put_failure:
164 return -1;
165}
166
167static 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;
cb1b69b0
LGL
181 return 0;
182
183nla_put_failure:
184 return -1;
185}
186
187static struct nft_expr_type nft_hash_type;
511040ee 188static const struct nft_expr_ops nft_jhash_ops = {
cb1b69b0 189 .type = &nft_hash_type,
511040ee
LGL
190 .size = NFT_EXPR_SIZE(sizeof(struct nft_jhash)),
191 .eval = nft_jhash_eval,
192 .init = nft_jhash_init,
193 .dump = nft_jhash_dump,
cb1b69b0
LGL
194};
195
3206cade
LGL
196static 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
204static const struct nft_expr_ops *
205nft_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
cb1b69b0
LGL
225static struct nft_expr_type nft_hash_type __read_mostly = {
226 .name = "hash",
3206cade 227 .select_ops = &nft_hash_select_ops,
cb1b69b0
LGL
228 .policy = nft_hash_policy,
229 .maxattr = NFTA_HASH_MAX,
230 .owner = THIS_MODULE,
231};
232
233static int __init nft_hash_module_init(void)
234{
235 return nft_register_expr(&nft_hash_type);
236}
237
238static void __exit nft_hash_module_exit(void)
239{
240 nft_unregister_expr(&nft_hash_type);
241}
242
243module_init(nft_hash_module_init);
244module_exit(nft_hash_module_exit);
245
246MODULE_LICENSE("GPL");
247MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
248MODULE_ALIAS_NFT_EXPR("hash");