Merge tag 'spi-fix-v4.9-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi
[cascardo/linux.git] / net / netfilter / nft_dynset.c
1 /*
2  * Copyright (c) 2015 Patrick McHardy <kaber@trash.net>
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/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
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;
29 };
30
31 static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
32                             struct nft_regs *regs)
33 {
34         const struct nft_dynset *priv = nft_expr_priv(expr);
35         struct nft_set_ext *ext;
36         u64 timeout;
37         void *elem;
38
39         if (set->size && !atomic_add_unless(&set->nelems, 1, set->size))
40                 return NULL;
41
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                 if (set->size)
49                         atomic_dec(&set->nelems);
50                 return NULL;
51         }
52
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)
56                 return NULL;
57
58         return elem;
59 }
60
61 static void nft_dynset_eval(const struct nft_expr *expr,
62                             struct nft_regs *regs,
63                             const struct nft_pktinfo *pkt)
64 {
65         const struct nft_dynset *priv = nft_expr_priv(expr);
66         struct nft_set *set = priv->set;
67         const struct nft_set_ext *ext;
68         const struct nft_expr *sexpr;
69         u64 timeout;
70
71         if (set->ops->update(set, &regs->data[priv->sreg_key], nft_dynset_new,
72                              expr, regs, &ext)) {
73                 sexpr = NULL;
74                 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
75                         sexpr = nft_set_ext_expr(ext);
76
77                 if (priv->op == NFT_DYNSET_OP_UPDATE &&
78                     nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
79                         timeout = priv->timeout ? : set->timeout;
80                         *nft_set_ext_expiration(ext) = jiffies + timeout;
81                 } else if (sexpr == NULL)
82                         goto out;
83
84                 if (sexpr != NULL)
85                         sexpr->ops->eval(sexpr, regs, pkt);
86
87                 if (priv->invert)
88                         regs->verdict.code = NFT_BREAK;
89                 return;
90         }
91 out:
92         if (!priv->invert)
93                 regs->verdict.code = NFT_BREAK;
94 }
95
96 static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
97         [NFTA_DYNSET_SET_NAME]  = { .type = NLA_STRING },
98         [NFTA_DYNSET_SET_ID]    = { .type = NLA_U32 },
99         [NFTA_DYNSET_OP]        = { .type = NLA_U32 },
100         [NFTA_DYNSET_SREG_KEY]  = { .type = NLA_U32 },
101         [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },
102         [NFTA_DYNSET_TIMEOUT]   = { .type = NLA_U64 },
103         [NFTA_DYNSET_EXPR]      = { .type = NLA_NESTED },
104         [NFTA_DYNSET_FLAGS]     = { .type = NLA_U32 },
105 };
106
107 static int nft_dynset_init(const struct nft_ctx *ctx,
108                            const struct nft_expr *expr,
109                            const struct nlattr * const tb[])
110 {
111         struct nft_dynset *priv = nft_expr_priv(expr);
112         u8 genmask = nft_genmask_next(ctx->net);
113         struct nft_set *set;
114         u64 timeout;
115         int err;
116
117         if (tb[NFTA_DYNSET_SET_NAME] == NULL ||
118             tb[NFTA_DYNSET_OP] == NULL ||
119             tb[NFTA_DYNSET_SREG_KEY] == NULL)
120                 return -EINVAL;
121
122         if (tb[NFTA_DYNSET_FLAGS]) {
123                 u32 flags = ntohl(nla_get_be32(tb[NFTA_DYNSET_FLAGS]));
124
125                 if (flags & ~NFT_DYNSET_F_INV)
126                         return -EINVAL;
127                 if (flags & NFT_DYNSET_F_INV)
128                         priv->invert = true;
129         }
130
131         set = nf_tables_set_lookup(ctx->table, tb[NFTA_DYNSET_SET_NAME],
132                                    genmask);
133         if (IS_ERR(set)) {
134                 if (tb[NFTA_DYNSET_SET_ID])
135                         set = nf_tables_set_lookup_byid(ctx->net,
136                                                         tb[NFTA_DYNSET_SET_ID],
137                                                         genmask);
138                 if (IS_ERR(set))
139                         return PTR_ERR(set);
140         }
141
142         if (set->flags & NFT_SET_CONSTANT)
143                 return -EBUSY;
144
145         priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
146         switch (priv->op) {
147         case NFT_DYNSET_OP_ADD:
148                 break;
149         case NFT_DYNSET_OP_UPDATE:
150                 if (!(set->flags & NFT_SET_TIMEOUT))
151                         return -EOPNOTSUPP;
152                 break;
153         default:
154                 return -EOPNOTSUPP;
155         }
156
157         timeout = 0;
158         if (tb[NFTA_DYNSET_TIMEOUT] != NULL) {
159                 if (!(set->flags & NFT_SET_TIMEOUT))
160                         return -EINVAL;
161                 timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64(
162                                                 tb[NFTA_DYNSET_TIMEOUT])));
163         }
164
165         priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]);
166         err = nft_validate_register_load(priv->sreg_key, set->klen);;
167         if (err < 0)
168                 return err;
169
170         if (tb[NFTA_DYNSET_SREG_DATA] != NULL) {
171                 if (!(set->flags & NFT_SET_MAP))
172                         return -EINVAL;
173                 if (set->dtype == NFT_DATA_VERDICT)
174                         return -EOPNOTSUPP;
175
176                 priv->sreg_data = nft_parse_register(tb[NFTA_DYNSET_SREG_DATA]);
177                 err = nft_validate_register_load(priv->sreg_data, set->dlen);
178                 if (err < 0)
179                         return err;
180         } else if (set->flags & NFT_SET_MAP)
181                 return -EINVAL;
182
183         if (tb[NFTA_DYNSET_EXPR] != NULL) {
184                 if (!(set->flags & NFT_SET_EVAL))
185                         return -EINVAL;
186                 if (!(set->flags & NFT_SET_ANONYMOUS))
187                         return -EOPNOTSUPP;
188
189                 priv->expr = nft_expr_init(ctx, tb[NFTA_DYNSET_EXPR]);
190                 if (IS_ERR(priv->expr))
191                         return PTR_ERR(priv->expr);
192
193                 err = -EOPNOTSUPP;
194                 if (!(priv->expr->ops->type->flags & NFT_EXPR_STATEFUL))
195                         goto err1;
196         } else if (set->flags & NFT_SET_EVAL)
197                 return -EINVAL;
198
199         nft_set_ext_prepare(&priv->tmpl);
200         nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_KEY, set->klen);
201         if (set->flags & NFT_SET_MAP)
202                 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_DATA, set->dlen);
203         if (priv->expr != NULL)
204                 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_EXPR,
205                                        priv->expr->ops->size);
206         if (set->flags & NFT_SET_TIMEOUT) {
207                 if (timeout || set->timeout)
208                         nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION);
209         }
210
211         priv->timeout = timeout;
212
213         err = nf_tables_bind_set(ctx, set, &priv->binding);
214         if (err < 0)
215                 goto err1;
216
217         priv->set = set;
218         return 0;
219
220 err1:
221         if (priv->expr != NULL)
222                 nft_expr_destroy(ctx, priv->expr);
223         return err;
224 }
225
226 static void nft_dynset_destroy(const struct nft_ctx *ctx,
227                                const struct nft_expr *expr)
228 {
229         struct nft_dynset *priv = nft_expr_priv(expr);
230
231         nf_tables_unbind_set(ctx, priv->set, &priv->binding);
232         if (priv->expr != NULL)
233                 nft_expr_destroy(ctx, priv->expr);
234 }
235
236 static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
237 {
238         const struct nft_dynset *priv = nft_expr_priv(expr);
239         u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0;
240
241         if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
242                 goto nla_put_failure;
243         if (priv->set->flags & NFT_SET_MAP &&
244             nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data))
245                 goto nla_put_failure;
246         if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op)))
247                 goto nla_put_failure;
248         if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
249                 goto nla_put_failure;
250         if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT,
251                          cpu_to_be64(jiffies_to_msecs(priv->timeout)),
252                          NFTA_DYNSET_PAD))
253                 goto nla_put_failure;
254         if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
255                 goto nla_put_failure;
256         if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags)))
257                 goto nla_put_failure;
258         return 0;
259
260 nla_put_failure:
261         return -1;
262 }
263
264 static struct nft_expr_type nft_dynset_type;
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,
272 };
273
274 static 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,
280 };
281
282 int __init nft_dynset_module_init(void)
283 {
284         return nft_register_expr(&nft_dynset_type);
285 }
286
287 void nft_dynset_module_exit(void)
288 {
289         nft_unregister_expr(&nft_dynset_type);
290 }