2 * Copyright (c) 2015 Patrick McHardy <kaber@trash.net>
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/module.h>
12 #include <linux/init.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 <net/netns/generic.h>
20 extern unsigned int nf_tables_net_id;
24 struct nft_set_ext_tmpl tmpl;
25 enum nft_dynset_ops op:8;
30 struct nft_expr *expr;
31 struct nft_set_binding binding;
34 static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
35 struct nft_regs *regs)
37 const struct nft_dynset *priv = nft_expr_priv(expr);
38 struct nft_set_ext *ext;
42 if (!atomic_add_unless(&set->nelems, 1, set->size))
45 timeout = priv->timeout ? : set->timeout;
46 elem = nft_set_elem_init(set, &priv->tmpl,
47 ®s->data[priv->sreg_key],
48 ®s->data[priv->sreg_data],
53 ext = nft_set_elem_ext(set, elem);
54 if (priv->expr != NULL &&
55 nft_expr_clone(nft_set_ext_expr(ext), priv->expr) < 0)
61 nft_set_elem_destroy(set, elem, false);
64 atomic_dec(&set->nelems);
68 static void nft_dynset_eval(const struct nft_expr *expr,
69 struct nft_regs *regs,
70 const struct nft_pktinfo *pkt)
72 const struct nft_dynset *priv = nft_expr_priv(expr);
73 struct nft_set *set = priv->set;
74 const struct nft_set_ext *ext;
75 const struct nft_expr *sexpr;
78 if (set->ops->update(set, ®s->data[priv->sreg_key], nft_dynset_new,
81 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
82 sexpr = nft_set_ext_expr(ext);
84 if (priv->op == NFT_DYNSET_OP_UPDATE &&
85 nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
86 timeout = priv->timeout ? : set->timeout;
87 *nft_set_ext_expiration(ext) = get_jiffies_64() + timeout;
91 sexpr->ops->eval(sexpr, regs, pkt);
94 regs->verdict.code = NFT_BREAK;
99 regs->verdict.code = NFT_BREAK;
102 static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
103 [NFTA_DYNSET_SET_NAME] = { .type = NLA_STRING,
104 .len = NFT_SET_MAXNAMELEN - 1 },
105 [NFTA_DYNSET_SET_ID] = { .type = NLA_U32 },
106 [NFTA_DYNSET_OP] = { .type = NLA_U32 },
107 [NFTA_DYNSET_SREG_KEY] = { .type = NLA_U32 },
108 [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },
109 [NFTA_DYNSET_TIMEOUT] = { .type = NLA_U64 },
110 [NFTA_DYNSET_EXPR] = { .type = NLA_NESTED },
111 [NFTA_DYNSET_FLAGS] = { .type = NLA_U32 },
114 static int nft_dynset_init(const struct nft_ctx *ctx,
115 const struct nft_expr *expr,
116 const struct nlattr * const tb[])
118 struct nftables_pernet *nft_net = net_generic(ctx->net, nf_tables_net_id);
119 struct nft_dynset *priv = nft_expr_priv(expr);
120 u8 genmask = nft_genmask_next(ctx->net);
125 lockdep_assert_held(&nft_net->commit_mutex);
127 if (tb[NFTA_DYNSET_SET_NAME] == NULL ||
128 tb[NFTA_DYNSET_OP] == NULL ||
129 tb[NFTA_DYNSET_SREG_KEY] == NULL)
132 if (tb[NFTA_DYNSET_FLAGS]) {
133 u32 flags = ntohl(nla_get_be32(tb[NFTA_DYNSET_FLAGS]));
135 if (flags & ~NFT_DYNSET_F_INV)
137 if (flags & NFT_DYNSET_F_INV)
141 set = nft_set_lookup_global(ctx->net, ctx->table,
142 tb[NFTA_DYNSET_SET_NAME],
143 tb[NFTA_DYNSET_SET_ID], genmask);
147 if (set->flags & NFT_SET_OBJECT)
150 if (set->ops->update == NULL)
153 if (set->flags & NFT_SET_CONSTANT)
156 priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
158 case NFT_DYNSET_OP_ADD:
160 case NFT_DYNSET_OP_UPDATE:
161 if (!(set->flags & NFT_SET_TIMEOUT))
169 if (tb[NFTA_DYNSET_TIMEOUT] != NULL) {
170 if (!(set->flags & NFT_SET_TIMEOUT))
172 timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64(
173 tb[NFTA_DYNSET_TIMEOUT])));
176 err = nft_parse_register_load(tb[NFTA_DYNSET_SREG_KEY], &priv->sreg_key,
181 if (tb[NFTA_DYNSET_SREG_DATA] != NULL) {
182 if (!(set->flags & NFT_SET_MAP))
184 if (set->dtype == NFT_DATA_VERDICT)
187 err = nft_parse_register_load(tb[NFTA_DYNSET_SREG_DATA],
188 &priv->sreg_data, set->dlen);
191 } else if (set->flags & NFT_SET_MAP)
194 if (tb[NFTA_DYNSET_EXPR] != NULL) {
195 if (!(set->flags & NFT_SET_EVAL))
198 priv->expr = nft_expr_init(ctx, tb[NFTA_DYNSET_EXPR]);
199 if (IS_ERR(priv->expr))
200 return PTR_ERR(priv->expr);
203 if (priv->expr->ops->type->flags & NFT_EXPR_GC) {
204 if (set->flags & NFT_SET_TIMEOUT)
206 if (!set->ops->gc_init)
208 set->ops->gc_init(set);
212 nft_set_ext_prepare(&priv->tmpl);
213 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_KEY, set->klen);
214 if (set->flags & NFT_SET_MAP)
215 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_DATA, set->dlen);
216 if (priv->expr != NULL)
217 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_EXPR,
218 priv->expr->ops->size);
219 if (set->flags & NFT_SET_TIMEOUT) {
220 if (timeout || set->timeout) {
221 nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_TIMEOUT);
222 nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION);
226 priv->timeout = timeout;
228 err = nf_tables_bind_set(ctx, set, &priv->binding);
239 if (priv->expr != NULL)
240 nft_expr_destroy(ctx, priv->expr);
244 static void nft_dynset_deactivate(const struct nft_ctx *ctx,
245 const struct nft_expr *expr,
246 enum nft_trans_phase phase)
248 struct nft_dynset *priv = nft_expr_priv(expr);
250 nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
253 static void nft_dynset_activate(const struct nft_ctx *ctx,
254 const struct nft_expr *expr)
256 struct nft_dynset *priv = nft_expr_priv(expr);
258 nf_tables_activate_set(ctx, priv->set);
261 static void nft_dynset_destroy(const struct nft_ctx *ctx,
262 const struct nft_expr *expr)
264 struct nft_dynset *priv = nft_expr_priv(expr);
266 if (priv->expr != NULL)
267 nft_expr_destroy(ctx, priv->expr);
269 nf_tables_destroy_set(ctx, priv->set);
272 static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
274 const struct nft_dynset *priv = nft_expr_priv(expr);
275 u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0;
277 if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
278 goto nla_put_failure;
279 if (priv->set->flags & NFT_SET_MAP &&
280 nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data))
281 goto nla_put_failure;
282 if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op)))
283 goto nla_put_failure;
284 if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
285 goto nla_put_failure;
286 if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT,
287 cpu_to_be64(jiffies_to_msecs(priv->timeout)),
289 goto nla_put_failure;
290 if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
291 goto nla_put_failure;
292 if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags)))
293 goto nla_put_failure;
300 static const struct nft_expr_ops nft_dynset_ops = {
301 .type = &nft_dynset_type,
302 .size = NFT_EXPR_SIZE(sizeof(struct nft_dynset)),
303 .eval = nft_dynset_eval,
304 .init = nft_dynset_init,
305 .destroy = nft_dynset_destroy,
306 .activate = nft_dynset_activate,
307 .deactivate = nft_dynset_deactivate,
308 .dump = nft_dynset_dump,
311 struct nft_expr_type nft_dynset_type __read_mostly = {
313 .ops = &nft_dynset_ops,
314 .policy = nft_dynset_policy,
315 .maxattr = NFTA_DYNSET_MAX,
316 .owner = THIS_MODULE,