]> Git Repo - linux.git/blame - net/netfilter/nft_numgen.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
[linux.git] / net / netfilter / nft_numgen.c
CommitLineData
91dbc6be
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 <linux/static_key.h>
17#include <net/netfilter/nf_tables.h>
18#include <net/netfilter/nf_tables_core.h>
19
20static DEFINE_PER_CPU(struct rnd_state, nft_numgen_prandom_state);
21
22struct nft_ng_inc {
23 enum nft_registers dreg:8;
0d9932b2 24 u32 modulus;
91dbc6be 25 atomic_t counter;
2b03bf73 26 u32 offset;
91dbc6be
LGL
27};
28
d734a288 29static u32 nft_ng_inc_gen(struct nft_ng_inc *priv)
91dbc6be 30{
91dbc6be
LGL
31 u32 nval, oval;
32
33 do {
34 oval = atomic_read(&priv->counter);
0d9932b2 35 nval = (oval + 1 < priv->modulus) ? oval + 1 : 0;
91dbc6be
LGL
36 } while (atomic_cmpxchg(&priv->counter, oval, nval) != oval);
37
d734a288
LGL
38 return nval + priv->offset;
39}
40
41static void nft_ng_inc_eval(const struct nft_expr *expr,
42 struct nft_regs *regs,
43 const struct nft_pktinfo *pkt)
44{
45 struct nft_ng_inc *priv = nft_expr_priv(expr);
46
47 regs->data[priv->dreg] = nft_ng_inc_gen(priv);
48}
49
91dbc6be
LGL
50static const struct nla_policy nft_ng_policy[NFTA_NG_MAX + 1] = {
51 [NFTA_NG_DREG] = { .type = NLA_U32 },
0d9932b2 52 [NFTA_NG_MODULUS] = { .type = NLA_U32 },
91dbc6be 53 [NFTA_NG_TYPE] = { .type = NLA_U32 },
2b03bf73 54 [NFTA_NG_OFFSET] = { .type = NLA_U32 },
91dbc6be
LGL
55};
56
57static int nft_ng_inc_init(const struct nft_ctx *ctx,
58 const struct nft_expr *expr,
59 const struct nlattr * const tb[])
60{
61 struct nft_ng_inc *priv = nft_expr_priv(expr);
62
2b03bf73
LGL
63 if (tb[NFTA_NG_OFFSET])
64 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
65
0d9932b2
LGL
66 priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
67 if (priv->modulus == 0)
91dbc6be
LGL
68 return -ERANGE;
69
2b03bf73
LGL
70 if (priv->offset + priv->modulus - 1 < priv->offset)
71 return -EOVERFLOW;
72
91dbc6be 73 priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]);
0ecba4d9 74 atomic_set(&priv->counter, priv->modulus - 1);
91dbc6be
LGL
75
76 return nft_validate_register_store(ctx, priv->dreg, NULL,
77 NFT_DATA_VALUE, sizeof(u32));
78}
79
80static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg,
2b03bf73 81 u32 modulus, enum nft_ng_types type, u32 offset)
91dbc6be
LGL
82{
83 if (nft_dump_register(skb, NFTA_NG_DREG, dreg))
84 goto nla_put_failure;
0d9932b2 85 if (nla_put_be32(skb, NFTA_NG_MODULUS, htonl(modulus)))
91dbc6be 86 goto nla_put_failure;
7073b16f 87 if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type)))
91dbc6be 88 goto nla_put_failure;
2b03bf73
LGL
89 if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset)))
90 goto nla_put_failure;
91dbc6be
LGL
91
92 return 0;
93
94nla_put_failure:
95 return -1;
96}
97
98static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr)
99{
100 const struct nft_ng_inc *priv = nft_expr_priv(expr);
101
2b03bf73
LGL
102 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL,
103 priv->offset);
91dbc6be
LGL
104}
105
106struct nft_ng_random {
107 enum nft_registers dreg:8;
0d9932b2 108 u32 modulus;
2b03bf73 109 u32 offset;
91dbc6be
LGL
110};
111
978d8f90
LGL
112static u32 nft_ng_random_gen(struct nft_ng_random *priv)
113{
114 struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state);
115
116 return reciprocal_scale(prandom_u32_state(state), priv->modulus) +
117 priv->offset;
118}
119
91dbc6be
LGL
120static void nft_ng_random_eval(const struct nft_expr *expr,
121 struct nft_regs *regs,
122 const struct nft_pktinfo *pkt)
123{
124 struct nft_ng_random *priv = nft_expr_priv(expr);
91dbc6be 125
978d8f90
LGL
126 regs->data[priv->dreg] = nft_ng_random_gen(priv);
127}
128
91dbc6be
LGL
129static int nft_ng_random_init(const struct nft_ctx *ctx,
130 const struct nft_expr *expr,
131 const struct nlattr * const tb[])
132{
133 struct nft_ng_random *priv = nft_expr_priv(expr);
134
2b03bf73
LGL
135 if (tb[NFTA_NG_OFFSET])
136 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
137
0d9932b2
LGL
138 priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
139 if (priv->modulus == 0)
91dbc6be
LGL
140 return -ERANGE;
141
2b03bf73
LGL
142 if (priv->offset + priv->modulus - 1 < priv->offset)
143 return -EOVERFLOW;
144
91dbc6be
LGL
145 prandom_init_once(&nft_numgen_prandom_state);
146
147 priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]);
148
149 return nft_validate_register_store(ctx, priv->dreg, NULL,
150 NFT_DATA_VALUE, sizeof(u32));
151}
152
153static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr)
154{
155 const struct nft_ng_random *priv = nft_expr_priv(expr);
156
2b03bf73
LGL
157 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM,
158 priv->offset);
91dbc6be
LGL
159}
160
161static struct nft_expr_type nft_ng_type;
162static const struct nft_expr_ops nft_ng_inc_ops = {
163 .type = &nft_ng_type,
164 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)),
165 .eval = nft_ng_inc_eval,
166 .init = nft_ng_inc_init,
167 .dump = nft_ng_inc_dump,
168};
169
170static const struct nft_expr_ops nft_ng_random_ops = {
171 .type = &nft_ng_type,
172 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_random)),
173 .eval = nft_ng_random_eval,
174 .init = nft_ng_random_init,
175 .dump = nft_ng_random_dump,
176};
177
178static const struct nft_expr_ops *
179nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
180{
181 u32 type;
182
0d9932b2
LGL
183 if (!tb[NFTA_NG_DREG] ||
184 !tb[NFTA_NG_MODULUS] ||
91dbc6be
LGL
185 !tb[NFTA_NG_TYPE])
186 return ERR_PTR(-EINVAL);
187
188 type = ntohl(nla_get_be32(tb[NFTA_NG_TYPE]));
189
190 switch (type) {
191 case NFT_NG_INCREMENTAL:
192 return &nft_ng_inc_ops;
193 case NFT_NG_RANDOM:
194 return &nft_ng_random_ops;
195 }
196
197 return ERR_PTR(-EINVAL);
198}
199
200static struct nft_expr_type nft_ng_type __read_mostly = {
201 .name = "numgen",
d4ef3835 202 .select_ops = nft_ng_select_ops,
91dbc6be
LGL
203 .policy = nft_ng_policy,
204 .maxattr = NFTA_NG_MAX,
205 .owner = THIS_MODULE,
206};
207
208static int __init nft_ng_module_init(void)
209{
210 return nft_register_expr(&nft_ng_type);
211}
212
213static void __exit nft_ng_module_exit(void)
214{
215 nft_unregister_expr(&nft_ng_type);
216}
217
218module_init(nft_ng_module_init);
219module_exit(nft_ng_module_exit);
220
221MODULE_LICENSE("GPL");
222MODULE_AUTHOR("Laura Garcia <[email protected]>");
223MODULE_ALIAS_NFT_EXPR("numgen");
This page took 0.215919 seconds and 4 git commands to generate.