Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / net / netfilter / nft_dynset.c
blobfc83e29d6634eab5ec65e22808f5d13cc615a058
1 /*
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.
8 */
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>
19 struct nft_dynset {
20 struct nft_set *set;
21 struct nft_set_ext_tmpl tmpl;
22 enum nft_dynset_ops op:8;
23 enum nft_registers sreg_key:8;
24 enum nft_registers sreg_data:8;
25 bool invert;
26 u64 timeout;
27 struct nft_expr *expr;
28 struct nft_set_binding binding;
31 static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
32 struct nft_regs *regs)
34 const struct nft_dynset *priv = nft_expr_priv(expr);
35 struct nft_set_ext *ext;
36 u64 timeout;
37 void *elem;
39 if (set->size && !atomic_add_unless(&set->nelems, 1, set->size))
40 return NULL;
42 timeout = priv->timeout ? : set->timeout;
43 elem = nft_set_elem_init(set, &priv->tmpl,
44 &regs->data[priv->sreg_key],
45 &regs->data[priv->sreg_data],
46 timeout, GFP_ATOMIC);
47 if (elem == NULL)
48 goto err1;
50 ext = nft_set_elem_ext(set, elem);
51 if (priv->expr != NULL &&
52 nft_expr_clone(nft_set_ext_expr(ext), priv->expr) < 0)
53 goto err2;
55 return elem;
57 err2:
58 nft_set_elem_destroy(set, elem, false);
59 err1:
60 if (set->size)
61 atomic_dec(&set->nelems);
62 return NULL;
65 static void nft_dynset_eval(const struct nft_expr *expr,
66 struct nft_regs *regs,
67 const struct nft_pktinfo *pkt)
69 const struct nft_dynset *priv = nft_expr_priv(expr);
70 struct nft_set *set = priv->set;
71 const struct nft_set_ext *ext;
72 const struct nft_expr *sexpr;
73 u64 timeout;
75 if (set->ops->update(set, &regs->data[priv->sreg_key], nft_dynset_new,
76 expr, regs, &ext)) {
77 sexpr = NULL;
78 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
79 sexpr = nft_set_ext_expr(ext);
81 if (priv->op == NFT_DYNSET_OP_UPDATE &&
82 nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
83 timeout = priv->timeout ? : set->timeout;
84 *nft_set_ext_expiration(ext) = jiffies + timeout;
87 if (sexpr != NULL)
88 sexpr->ops->eval(sexpr, regs, pkt);
90 if (priv->invert)
91 regs->verdict.code = NFT_BREAK;
92 return;
95 if (!priv->invert)
96 regs->verdict.code = NFT_BREAK;
99 static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
100 [NFTA_DYNSET_SET_NAME] = { .type = NLA_STRING,
101 .len = NFT_SET_MAXNAMELEN - 1 },
102 [NFTA_DYNSET_SET_ID] = { .type = NLA_U32 },
103 [NFTA_DYNSET_OP] = { .type = NLA_U32 },
104 [NFTA_DYNSET_SREG_KEY] = { .type = NLA_U32 },
105 [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },
106 [NFTA_DYNSET_TIMEOUT] = { .type = NLA_U64 },
107 [NFTA_DYNSET_EXPR] = { .type = NLA_NESTED },
108 [NFTA_DYNSET_FLAGS] = { .type = NLA_U32 },
111 static int nft_dynset_init(const struct nft_ctx *ctx,
112 const struct nft_expr *expr,
113 const struct nlattr * const tb[])
115 struct nft_dynset *priv = nft_expr_priv(expr);
116 u8 genmask = nft_genmask_next(ctx->net);
117 struct nft_set *set;
118 u64 timeout;
119 int err;
121 if (tb[NFTA_DYNSET_SET_NAME] == NULL ||
122 tb[NFTA_DYNSET_OP] == NULL ||
123 tb[NFTA_DYNSET_SREG_KEY] == NULL)
124 return -EINVAL;
126 if (tb[NFTA_DYNSET_FLAGS]) {
127 u32 flags = ntohl(nla_get_be32(tb[NFTA_DYNSET_FLAGS]));
129 if (flags & ~NFT_DYNSET_F_INV)
130 return -EINVAL;
131 if (flags & NFT_DYNSET_F_INV)
132 priv->invert = true;
135 set = nft_set_lookup(ctx->net, ctx->table, tb[NFTA_DYNSET_SET_NAME],
136 tb[NFTA_DYNSET_SET_ID], genmask);
137 if (IS_ERR(set))
138 return PTR_ERR(set);
140 if (set->ops->update == NULL)
141 return -EOPNOTSUPP;
143 if (set->flags & NFT_SET_CONSTANT)
144 return -EBUSY;
146 priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
147 switch (priv->op) {
148 case NFT_DYNSET_OP_ADD:
149 break;
150 case NFT_DYNSET_OP_UPDATE:
151 if (!(set->flags & NFT_SET_TIMEOUT))
152 return -EOPNOTSUPP;
153 break;
154 default:
155 return -EOPNOTSUPP;
158 timeout = 0;
159 if (tb[NFTA_DYNSET_TIMEOUT] != NULL) {
160 if (!(set->flags & NFT_SET_TIMEOUT))
161 return -EINVAL;
162 timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64(
163 tb[NFTA_DYNSET_TIMEOUT])));
166 priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]);
167 err = nft_validate_register_load(priv->sreg_key, set->klen);
168 if (err < 0)
169 return err;
171 if (tb[NFTA_DYNSET_SREG_DATA] != NULL) {
172 if (!(set->flags & NFT_SET_MAP))
173 return -EINVAL;
174 if (set->dtype == NFT_DATA_VERDICT)
175 return -EOPNOTSUPP;
177 priv->sreg_data = nft_parse_register(tb[NFTA_DYNSET_SREG_DATA]);
178 err = nft_validate_register_load(priv->sreg_data, set->dlen);
179 if (err < 0)
180 return err;
181 } else if (set->flags & NFT_SET_MAP)
182 return -EINVAL;
184 if (tb[NFTA_DYNSET_EXPR] != NULL) {
185 if (!(set->flags & NFT_SET_EVAL))
186 return -EINVAL;
187 if (!nft_set_is_anonymous(set))
188 return -EOPNOTSUPP;
190 priv->expr = nft_expr_init(ctx, tb[NFTA_DYNSET_EXPR]);
191 if (IS_ERR(priv->expr))
192 return PTR_ERR(priv->expr);
194 err = -EOPNOTSUPP;
195 if (!(priv->expr->ops->type->flags & NFT_EXPR_STATEFUL))
196 goto err1;
197 } else if (set->flags & NFT_SET_EVAL)
198 return -EINVAL;
200 nft_set_ext_prepare(&priv->tmpl);
201 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_KEY, set->klen);
202 if (set->flags & NFT_SET_MAP)
203 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_DATA, set->dlen);
204 if (priv->expr != NULL)
205 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_EXPR,
206 priv->expr->ops->size);
207 if (set->flags & NFT_SET_TIMEOUT) {
208 if (timeout || set->timeout)
209 nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION);
212 priv->timeout = timeout;
214 err = nf_tables_bind_set(ctx, set, &priv->binding);
215 if (err < 0)
216 goto err1;
218 priv->set = set;
219 return 0;
221 err1:
222 if (priv->expr != NULL)
223 nft_expr_destroy(ctx, priv->expr);
224 return err;
227 static void nft_dynset_destroy(const struct nft_ctx *ctx,
228 const struct nft_expr *expr)
230 struct nft_dynset *priv = nft_expr_priv(expr);
232 nf_tables_unbind_set(ctx, priv->set, &priv->binding);
233 if (priv->expr != NULL)
234 nft_expr_destroy(ctx, priv->expr);
237 static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
239 const struct nft_dynset *priv = nft_expr_priv(expr);
240 u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0;
242 if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
243 goto nla_put_failure;
244 if (priv->set->flags & NFT_SET_MAP &&
245 nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data))
246 goto nla_put_failure;
247 if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op)))
248 goto nla_put_failure;
249 if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
250 goto nla_put_failure;
251 if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT,
252 cpu_to_be64(jiffies_to_msecs(priv->timeout)),
253 NFTA_DYNSET_PAD))
254 goto nla_put_failure;
255 if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
256 goto nla_put_failure;
257 if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags)))
258 goto nla_put_failure;
259 return 0;
261 nla_put_failure:
262 return -1;
265 static const struct nft_expr_ops nft_dynset_ops = {
266 .type = &nft_dynset_type,
267 .size = NFT_EXPR_SIZE(sizeof(struct nft_dynset)),
268 .eval = nft_dynset_eval,
269 .init = nft_dynset_init,
270 .destroy = nft_dynset_destroy,
271 .dump = nft_dynset_dump,
274 struct nft_expr_type nft_dynset_type __read_mostly = {
275 .name = "dynset",
276 .ops = &nft_dynset_ops,
277 .policy = nft_dynset_policy,
278 .maxattr = NFTA_DYNSET_MAX,
279 .owner = THIS_MODULE,