2 * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
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.
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>
20 static DEFINE_PER_CPU(struct rnd_state
, nft_numgen_prandom_state
);
23 enum nft_registers dreg
:8;
29 static void nft_ng_inc_eval(const struct nft_expr
*expr
,
30 struct nft_regs
*regs
,
31 const struct nft_pktinfo
*pkt
)
33 struct nft_ng_inc
*priv
= nft_expr_priv(expr
);
37 oval
= atomic_read(&priv
->counter
);
38 nval
= (oval
+ 1 < priv
->modulus
) ? oval
+ 1 : 0;
39 } while (atomic_cmpxchg(&priv
->counter
, oval
, nval
) != oval
);
41 regs
->data
[priv
->dreg
] = nval
+ priv
->offset
;
44 static const struct nla_policy nft_ng_policy
[NFTA_NG_MAX
+ 1] = {
45 [NFTA_NG_DREG
] = { .type
= NLA_U32
},
46 [NFTA_NG_MODULUS
] = { .type
= NLA_U32
},
47 [NFTA_NG_TYPE
] = { .type
= NLA_U32
},
48 [NFTA_NG_OFFSET
] = { .type
= NLA_U32
},
51 static int nft_ng_inc_init(const struct nft_ctx
*ctx
,
52 const struct nft_expr
*expr
,
53 const struct nlattr
* const tb
[])
55 struct nft_ng_inc
*priv
= nft_expr_priv(expr
);
57 if (tb
[NFTA_NG_OFFSET
])
58 priv
->offset
= ntohl(nla_get_be32(tb
[NFTA_NG_OFFSET
]));
60 priv
->modulus
= ntohl(nla_get_be32(tb
[NFTA_NG_MODULUS
]));
61 if (priv
->modulus
== 0)
64 if (priv
->offset
+ priv
->modulus
- 1 < priv
->offset
)
67 priv
->dreg
= nft_parse_register(tb
[NFTA_NG_DREG
]);
68 atomic_set(&priv
->counter
, 0);
70 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
71 NFT_DATA_VALUE
, sizeof(u32
));
74 static int nft_ng_dump(struct sk_buff
*skb
, enum nft_registers dreg
,
75 u32 modulus
, enum nft_ng_types type
, u32 offset
)
77 if (nft_dump_register(skb
, NFTA_NG_DREG
, dreg
))
79 if (nla_put_be32(skb
, NFTA_NG_MODULUS
, htonl(modulus
)))
81 if (nla_put_be32(skb
, NFTA_NG_TYPE
, htonl(type
)))
83 if (nla_put_be32(skb
, NFTA_NG_OFFSET
, htonl(offset
)))
92 static int nft_ng_inc_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
94 const struct nft_ng_inc
*priv
= nft_expr_priv(expr
);
96 return nft_ng_dump(skb
, priv
->dreg
, priv
->modulus
, NFT_NG_INCREMENTAL
,
100 struct nft_ng_random
{
101 enum nft_registers dreg
:8;
106 static void nft_ng_random_eval(const struct nft_expr
*expr
,
107 struct nft_regs
*regs
,
108 const struct nft_pktinfo
*pkt
)
110 struct nft_ng_random
*priv
= nft_expr_priv(expr
);
111 struct rnd_state
*state
= this_cpu_ptr(&nft_numgen_prandom_state
);
114 val
= reciprocal_scale(prandom_u32_state(state
), priv
->modulus
);
115 regs
->data
[priv
->dreg
] = val
+ priv
->offset
;
118 static int nft_ng_random_init(const struct nft_ctx
*ctx
,
119 const struct nft_expr
*expr
,
120 const struct nlattr
* const tb
[])
122 struct nft_ng_random
*priv
= nft_expr_priv(expr
);
124 if (tb
[NFTA_NG_OFFSET
])
125 priv
->offset
= ntohl(nla_get_be32(tb
[NFTA_NG_OFFSET
]));
127 priv
->modulus
= ntohl(nla_get_be32(tb
[NFTA_NG_MODULUS
]));
128 if (priv
->modulus
== 0)
131 if (priv
->offset
+ priv
->modulus
- 1 < priv
->offset
)
134 prandom_init_once(&nft_numgen_prandom_state
);
136 priv
->dreg
= nft_parse_register(tb
[NFTA_NG_DREG
]);
138 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
139 NFT_DATA_VALUE
, sizeof(u32
));
142 static int nft_ng_random_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
144 const struct nft_ng_random
*priv
= nft_expr_priv(expr
);
146 return nft_ng_dump(skb
, priv
->dreg
, priv
->modulus
, NFT_NG_RANDOM
,
150 static struct nft_expr_type nft_ng_type
;
151 static const struct nft_expr_ops nft_ng_inc_ops
= {
152 .type
= &nft_ng_type
,
153 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ng_inc
)),
154 .eval
= nft_ng_inc_eval
,
155 .init
= nft_ng_inc_init
,
156 .dump
= nft_ng_inc_dump
,
159 static const struct nft_expr_ops nft_ng_random_ops
= {
160 .type
= &nft_ng_type
,
161 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ng_random
)),
162 .eval
= nft_ng_random_eval
,
163 .init
= nft_ng_random_init
,
164 .dump
= nft_ng_random_dump
,
167 static const struct nft_expr_ops
*
168 nft_ng_select_ops(const struct nft_ctx
*ctx
, const struct nlattr
* const tb
[])
172 if (!tb
[NFTA_NG_DREG
] ||
173 !tb
[NFTA_NG_MODULUS
] ||
175 return ERR_PTR(-EINVAL
);
177 type
= ntohl(nla_get_be32(tb
[NFTA_NG_TYPE
]));
180 case NFT_NG_INCREMENTAL
:
181 return &nft_ng_inc_ops
;
183 return &nft_ng_random_ops
;
186 return ERR_PTR(-EINVAL
);
189 static struct nft_expr_type nft_ng_type __read_mostly
= {
191 .select_ops
= &nft_ng_select_ops
,
192 .policy
= nft_ng_policy
,
193 .maxattr
= NFTA_NG_MAX
,
194 .owner
= THIS_MODULE
,
197 static int __init
nft_ng_module_init(void)
199 return nft_register_expr(&nft_ng_type
);
202 static void __exit
nft_ng_module_exit(void)
204 nft_unregister_expr(&nft_ng_type
);
207 module_init(nft_ng_module_init
);
208 module_exit(nft_ng_module_exit
);
210 MODULE_LICENSE("GPL");
211 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
212 MODULE_ALIAS_NFT_EXPR("numgen");