octeontx2-pf: Fix error return code in otx2_probe()
[linux/fpc-iii.git] / net / netfilter / nft_dynset.c
blob64ca13a1885bc94622b340f2f58ebccd48307f5e
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2015 Patrick McHardy <kaber@trash.net>
4 */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/netlink.h>
10 #include <linux/netfilter.h>
11 #include <linux/netfilter/nf_tables.h>
12 #include <net/netfilter/nf_tables.h>
13 #include <net/netfilter/nf_tables_core.h>
15 struct nft_dynset {
16 struct nft_set *set;
17 struct nft_set_ext_tmpl tmpl;
18 enum nft_dynset_ops op:8;
19 enum nft_registers sreg_key:8;
20 enum nft_registers sreg_data:8;
21 bool invert;
22 u64 timeout;
23 struct nft_expr *expr;
24 struct nft_set_binding binding;
27 static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
28 struct nft_regs *regs)
30 const struct nft_dynset *priv = nft_expr_priv(expr);
31 struct nft_set_ext *ext;
32 u64 timeout;
33 void *elem;
35 if (!atomic_add_unless(&set->nelems, 1, set->size))
36 return NULL;
38 timeout = priv->timeout ? : set->timeout;
39 elem = nft_set_elem_init(set, &priv->tmpl,
40 &regs->data[priv->sreg_key], NULL,
41 &regs->data[priv->sreg_data],
42 timeout, 0, GFP_ATOMIC);
43 if (elem == NULL)
44 goto err1;
46 ext = nft_set_elem_ext(set, elem);
47 if (priv->expr != NULL &&
48 nft_expr_clone(nft_set_ext_expr(ext), priv->expr) < 0)
49 goto err2;
51 return elem;
53 err2:
54 nft_set_elem_destroy(set, elem, false);
55 err1:
56 if (set->size)
57 atomic_dec(&set->nelems);
58 return NULL;
61 void nft_dynset_eval(const struct nft_expr *expr,
62 struct nft_regs *regs, const struct nft_pktinfo *pkt)
64 const struct nft_dynset *priv = nft_expr_priv(expr);
65 struct nft_set *set = priv->set;
66 const struct nft_set_ext *ext;
67 u64 timeout;
69 if (priv->op == NFT_DYNSET_OP_DELETE) {
70 set->ops->delete(set, &regs->data[priv->sreg_key]);
71 return;
74 if (set->ops->update(set, &regs->data[priv->sreg_key], nft_dynset_new,
75 expr, regs, &ext)) {
76 if (priv->op == NFT_DYNSET_OP_UPDATE &&
77 nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
78 timeout = priv->timeout ? : set->timeout;
79 *nft_set_ext_expiration(ext) = get_jiffies_64() + timeout;
82 nft_set_elem_update_expr(ext, regs, pkt);
84 if (priv->invert)
85 regs->verdict.code = NFT_BREAK;
86 return;
89 if (!priv->invert)
90 regs->verdict.code = NFT_BREAK;
93 static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
94 [NFTA_DYNSET_SET_NAME] = { .type = NLA_STRING,
95 .len = NFT_SET_MAXNAMELEN - 1 },
96 [NFTA_DYNSET_SET_ID] = { .type = NLA_U32 },
97 [NFTA_DYNSET_OP] = { .type = NLA_U32 },
98 [NFTA_DYNSET_SREG_KEY] = { .type = NLA_U32 },
99 [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },
100 [NFTA_DYNSET_TIMEOUT] = { .type = NLA_U64 },
101 [NFTA_DYNSET_EXPR] = { .type = NLA_NESTED },
102 [NFTA_DYNSET_FLAGS] = { .type = NLA_U32 },
105 static int nft_dynset_init(const struct nft_ctx *ctx,
106 const struct nft_expr *expr,
107 const struct nlattr * const tb[])
109 struct nft_dynset *priv = nft_expr_priv(expr);
110 u8 genmask = nft_genmask_next(ctx->net);
111 struct nft_set *set;
112 u64 timeout;
113 int err;
115 lockdep_assert_held(&ctx->net->nft.commit_mutex);
117 if (tb[NFTA_DYNSET_SET_NAME] == NULL ||
118 tb[NFTA_DYNSET_OP] == NULL ||
119 tb[NFTA_DYNSET_SREG_KEY] == NULL)
120 return -EINVAL;
122 if (tb[NFTA_DYNSET_FLAGS]) {
123 u32 flags = ntohl(nla_get_be32(tb[NFTA_DYNSET_FLAGS]));
125 if (flags & ~NFT_DYNSET_F_INV)
126 return -EINVAL;
127 if (flags & NFT_DYNSET_F_INV)
128 priv->invert = true;
131 set = nft_set_lookup_global(ctx->net, ctx->table,
132 tb[NFTA_DYNSET_SET_NAME],
133 tb[NFTA_DYNSET_SET_ID], genmask);
134 if (IS_ERR(set))
135 return PTR_ERR(set);
137 if (set->ops->update == NULL)
138 return -EOPNOTSUPP;
140 if (set->flags & NFT_SET_CONSTANT)
141 return -EBUSY;
143 priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
144 switch (priv->op) {
145 case NFT_DYNSET_OP_ADD:
146 case NFT_DYNSET_OP_DELETE:
147 break;
148 case NFT_DYNSET_OP_UPDATE:
149 if (!(set->flags & NFT_SET_TIMEOUT))
150 return -EOPNOTSUPP;
151 break;
152 default:
153 return -EOPNOTSUPP;
156 timeout = 0;
157 if (tb[NFTA_DYNSET_TIMEOUT] != NULL) {
158 if (!(set->flags & NFT_SET_TIMEOUT))
159 return -EINVAL;
160 timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64(
161 tb[NFTA_DYNSET_TIMEOUT])));
164 priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]);
165 err = nft_validate_register_load(priv->sreg_key, set->klen);
166 if (err < 0)
167 return err;
169 if (tb[NFTA_DYNSET_SREG_DATA] != NULL) {
170 if (!(set->flags & NFT_SET_MAP))
171 return -EINVAL;
172 if (set->dtype == NFT_DATA_VERDICT)
173 return -EOPNOTSUPP;
175 priv->sreg_data = nft_parse_register(tb[NFTA_DYNSET_SREG_DATA]);
176 err = nft_validate_register_load(priv->sreg_data, set->dlen);
177 if (err < 0)
178 return err;
179 } else if (set->flags & NFT_SET_MAP)
180 return -EINVAL;
182 if (tb[NFTA_DYNSET_EXPR] != NULL) {
183 if (!(set->flags & NFT_SET_EVAL))
184 return -EINVAL;
186 priv->expr = nft_set_elem_expr_alloc(ctx, set,
187 tb[NFTA_DYNSET_EXPR]);
188 if (IS_ERR(priv->expr))
189 return PTR_ERR(priv->expr);
191 if (set->expr && set->expr->ops != priv->expr->ops) {
192 err = -EOPNOTSUPP;
193 goto err_expr_free;
197 nft_set_ext_prepare(&priv->tmpl);
198 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_KEY, set->klen);
199 if (set->flags & NFT_SET_MAP)
200 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_DATA, set->dlen);
201 if (priv->expr != NULL)
202 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_EXPR,
203 priv->expr->ops->size);
204 if (set->flags & NFT_SET_TIMEOUT) {
205 if (timeout || set->timeout)
206 nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION);
209 priv->timeout = timeout;
211 err = nf_tables_bind_set(ctx, set, &priv->binding);
212 if (err < 0)
213 goto err_expr_free;
215 if (set->size == 0)
216 set->size = 0xffff;
218 priv->set = set;
219 return 0;
221 err_expr_free:
222 if (priv->expr != NULL)
223 nft_expr_destroy(ctx, priv->expr);
224 return err;
227 static void nft_dynset_deactivate(const struct nft_ctx *ctx,
228 const struct nft_expr *expr,
229 enum nft_trans_phase phase)
231 struct nft_dynset *priv = nft_expr_priv(expr);
233 nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
236 static void nft_dynset_activate(const struct nft_ctx *ctx,
237 const struct nft_expr *expr)
239 struct nft_dynset *priv = nft_expr_priv(expr);
241 priv->set->use++;
244 static void nft_dynset_destroy(const struct nft_ctx *ctx,
245 const struct nft_expr *expr)
247 struct nft_dynset *priv = nft_expr_priv(expr);
249 if (priv->expr != NULL)
250 nft_expr_destroy(ctx, priv->expr);
252 nf_tables_destroy_set(ctx, priv->set);
255 static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
257 const struct nft_dynset *priv = nft_expr_priv(expr);
258 u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0;
260 if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
261 goto nla_put_failure;
262 if (priv->set->flags & NFT_SET_MAP &&
263 nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data))
264 goto nla_put_failure;
265 if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op)))
266 goto nla_put_failure;
267 if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
268 goto nla_put_failure;
269 if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT,
270 cpu_to_be64(jiffies_to_msecs(priv->timeout)),
271 NFTA_DYNSET_PAD))
272 goto nla_put_failure;
273 if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
274 goto nla_put_failure;
275 if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags)))
276 goto nla_put_failure;
277 return 0;
279 nla_put_failure:
280 return -1;
283 static const struct nft_expr_ops nft_dynset_ops = {
284 .type = &nft_dynset_type,
285 .size = NFT_EXPR_SIZE(sizeof(struct nft_dynset)),
286 .eval = nft_dynset_eval,
287 .init = nft_dynset_init,
288 .destroy = nft_dynset_destroy,
289 .activate = nft_dynset_activate,
290 .deactivate = nft_dynset_deactivate,
291 .dump = nft_dynset_dump,
294 struct nft_expr_type nft_dynset_type __read_mostly = {
295 .name = "dynset",
296 .ops = &nft_dynset_ops,
297 .policy = nft_dynset_policy,
298 .maxattr = NFTA_DYNSET_MAX,
299 .owner = THIS_MODULE,