~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/nft_dynset.c

Version: ~ [ linux-5.2-rc1 ] ~ [ linux-5.1.2 ] ~ [ linux-5.0.16 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.43 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.119 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.176 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.179 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.139 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.67 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
 32 {
 33         int err;
 34 
 35         if (src->ops->clone) {
 36                 dst->ops = src->ops;
 37                 err = src->ops->clone(dst, src);
 38                 if (err < 0)
 39                         return err;
 40         } else {
 41                 memcpy(dst, src, src->ops->size);
 42         }
 43 
 44         __module_get(src->ops->type->owner);
 45         return 0;
 46 }
 47 
 48 static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
 49                             struct nft_regs *regs)
 50 {
 51         const struct nft_dynset *priv = nft_expr_priv(expr);
 52         struct nft_set_ext *ext;
 53         u64 timeout;
 54         void *elem;
 55 
 56         if (!atomic_add_unless(&set->nelems, 1, set->size))
 57                 return NULL;
 58 
 59         timeout = priv->timeout ? : set->timeout;
 60         elem = nft_set_elem_init(set, &priv->tmpl,
 61                                  &regs->data[priv->sreg_key],
 62                                  &regs->data[priv->sreg_data],
 63                                  timeout, GFP_ATOMIC);
 64         if (elem == NULL)
 65                 goto err1;
 66 
 67         ext = nft_set_elem_ext(set, elem);
 68         if (priv->expr != NULL &&
 69             nft_expr_clone(nft_set_ext_expr(ext), priv->expr) < 0)
 70                 goto err2;
 71 
 72         return elem;
 73 
 74 err2:
 75         nft_set_elem_destroy(set, elem, false);
 76 err1:
 77         if (set->size)
 78                 atomic_dec(&set->nelems);
 79         return NULL;
 80 }
 81 
 82 void nft_dynset_eval(const struct nft_expr *expr,
 83                      struct nft_regs *regs, const struct nft_pktinfo *pkt)
 84 {
 85         const struct nft_dynset *priv = nft_expr_priv(expr);
 86         struct nft_set *set = priv->set;
 87         const struct nft_set_ext *ext;
 88         const struct nft_expr *sexpr;
 89         u64 timeout;
 90 
 91         if (set->ops->update(set, &regs->data[priv->sreg_key], nft_dynset_new,
 92                              expr, regs, &ext)) {
 93                 sexpr = NULL;
 94                 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
 95                         sexpr = nft_set_ext_expr(ext);
 96 
 97                 if (priv->op == NFT_DYNSET_OP_UPDATE &&
 98                     nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
 99                         timeout = priv->timeout ? : set->timeout;
100                         *nft_set_ext_expiration(ext) = get_jiffies_64() + timeout;
101                 }
102 
103                 if (sexpr != NULL)
104                         sexpr->ops->eval(sexpr, regs, pkt);
105 
106                 if (priv->invert)
107                         regs->verdict.code = NFT_BREAK;
108                 return;
109         }
110 
111         if (!priv->invert)
112                 regs->verdict.code = NFT_BREAK;
113 }
114 
115 static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
116         [NFTA_DYNSET_SET_NAME]  = { .type = NLA_STRING,
117                                     .len = NFT_SET_MAXNAMELEN - 1 },
118         [NFTA_DYNSET_SET_ID]    = { .type = NLA_U32 },
119         [NFTA_DYNSET_OP]        = { .type = NLA_U32 },
120         [NFTA_DYNSET_SREG_KEY]  = { .type = NLA_U32 },
121         [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },
122         [NFTA_DYNSET_TIMEOUT]   = { .type = NLA_U64 },
123         [NFTA_DYNSET_EXPR]      = { .type = NLA_NESTED },
124         [NFTA_DYNSET_FLAGS]     = { .type = NLA_U32 },
125 };
126 
127 static int nft_dynset_init(const struct nft_ctx *ctx,
128                            const struct nft_expr *expr,
129                            const struct nlattr * const tb[])
130 {
131         struct nft_dynset *priv = nft_expr_priv(expr);
132         u8 genmask = nft_genmask_next(ctx->net);
133         struct nft_set *set;
134         u64 timeout;
135         int err;
136 
137         lockdep_assert_held(&ctx->net->nft.commit_mutex);
138 
139         if (tb[NFTA_DYNSET_SET_NAME] == NULL ||
140             tb[NFTA_DYNSET_OP] == NULL ||
141             tb[NFTA_DYNSET_SREG_KEY] == NULL)
142                 return -EINVAL;
143 
144         if (tb[NFTA_DYNSET_FLAGS]) {
145                 u32 flags = ntohl(nla_get_be32(tb[NFTA_DYNSET_FLAGS]));
146 
147                 if (flags & ~NFT_DYNSET_F_INV)
148                         return -EINVAL;
149                 if (flags & NFT_DYNSET_F_INV)
150                         priv->invert = true;
151         }
152 
153         set = nft_set_lookup_global(ctx->net, ctx->table,
154                                     tb[NFTA_DYNSET_SET_NAME],
155                                     tb[NFTA_DYNSET_SET_ID], genmask);
156         if (IS_ERR(set))
157                 return PTR_ERR(set);
158 
159         if (set->ops->update == NULL)
160                 return -EOPNOTSUPP;
161 
162         if (set->flags & NFT_SET_CONSTANT)
163                 return -EBUSY;
164 
165         priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
166         switch (priv->op) {
167         case NFT_DYNSET_OP_ADD:
168                 break;
169         case NFT_DYNSET_OP_UPDATE:
170                 if (!(set->flags & NFT_SET_TIMEOUT))
171                         return -EOPNOTSUPP;
172                 break;
173         default:
174                 return -EOPNOTSUPP;
175         }
176 
177         timeout = 0;
178         if (tb[NFTA_DYNSET_TIMEOUT] != NULL) {
179                 if (!(set->flags & NFT_SET_TIMEOUT))
180                         return -EINVAL;
181                 timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64(
182                                                 tb[NFTA_DYNSET_TIMEOUT])));
183         }
184 
185         priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]);
186         err = nft_validate_register_load(priv->sreg_key, set->klen);
187         if (err < 0)
188                 return err;
189 
190         if (tb[NFTA_DYNSET_SREG_DATA] != NULL) {
191                 if (!(set->flags & NFT_SET_MAP))
192                         return -EINVAL;
193                 if (set->dtype == NFT_DATA_VERDICT)
194                         return -EOPNOTSUPP;
195 
196                 priv->sreg_data = nft_parse_register(tb[NFTA_DYNSET_SREG_DATA]);
197                 err = nft_validate_register_load(priv->sreg_data, set->dlen);
198                 if (err < 0)
199                         return err;
200         } else if (set->flags & NFT_SET_MAP)
201                 return -EINVAL;
202 
203         if (tb[NFTA_DYNSET_EXPR] != NULL) {
204                 if (!(set->flags & NFT_SET_EVAL))
205                         return -EINVAL;
206 
207                 priv->expr = nft_expr_init(ctx, tb[NFTA_DYNSET_EXPR]);
208                 if (IS_ERR(priv->expr))
209                         return PTR_ERR(priv->expr);
210 
211                 err = -EOPNOTSUPP;
212                 if (!(priv->expr->ops->type->flags & NFT_EXPR_STATEFUL))
213                         goto err1;
214 
215                 if (priv->expr->ops->type->flags & NFT_EXPR_GC) {
216                         if (set->flags & NFT_SET_TIMEOUT)
217                                 goto err1;
218                         if (!set->ops->gc_init)
219                                 goto err1;
220                         set->ops->gc_init(set);
221                 }
222         }
223 
224         nft_set_ext_prepare(&priv->tmpl);
225         nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_KEY, set->klen);
226         if (set->flags & NFT_SET_MAP)
227                 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_DATA, set->dlen);
228         if (priv->expr != NULL)
229                 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_EXPR,
230                                        priv->expr->ops->size);
231         if (set->flags & NFT_SET_TIMEOUT) {
232                 if (timeout || set->timeout)
233                         nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION);
234         }
235 
236         priv->timeout = timeout;
237 
238         err = nf_tables_bind_set(ctx, set, &priv->binding);
239         if (err < 0)
240                 goto err1;
241 
242         if (set->size == 0)
243                 set->size = 0xffff;
244 
245         priv->set = set;
246         return 0;
247 
248 err1:
249         if (priv->expr != NULL)
250                 nft_expr_destroy(ctx, priv->expr);
251         return err;
252 }
253 
254 static void nft_dynset_deactivate(const struct nft_ctx *ctx,
255                                   const struct nft_expr *expr,
256                                   enum nft_trans_phase phase)
257 {
258         struct nft_dynset *priv = nft_expr_priv(expr);
259 
260         nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
261 }
262 
263 static void nft_dynset_activate(const struct nft_ctx *ctx,
264                                 const struct nft_expr *expr)
265 {
266         struct nft_dynset *priv = nft_expr_priv(expr);
267 
268         priv->set->use++;
269 }
270 
271 static void nft_dynset_destroy(const struct nft_ctx *ctx,
272                                const struct nft_expr *expr)
273 {
274         struct nft_dynset *priv = nft_expr_priv(expr);
275 
276         if (priv->expr != NULL)
277                 nft_expr_destroy(ctx, priv->expr);
278 
279         nf_tables_destroy_set(ctx, priv->set);
280 }
281 
282 static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
283 {
284         const struct nft_dynset *priv = nft_expr_priv(expr);
285         u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0;
286 
287         if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
288                 goto nla_put_failure;
289         if (priv->set->flags & NFT_SET_MAP &&
290             nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data))
291                 goto nla_put_failure;
292         if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op)))
293                 goto nla_put_failure;
294         if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
295                 goto nla_put_failure;
296         if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT,
297                          cpu_to_be64(jiffies_to_msecs(priv->timeout)),
298                          NFTA_DYNSET_PAD))
299                 goto nla_put_failure;
300         if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
301                 goto nla_put_failure;
302         if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags)))
303                 goto nla_put_failure;
304         return 0;
305 
306 nla_put_failure:
307         return -1;
308 }
309 
310 static const struct nft_expr_ops nft_dynset_ops = {
311         .type           = &nft_dynset_type,
312         .size           = NFT_EXPR_SIZE(sizeof(struct nft_dynset)),
313         .eval           = nft_dynset_eval,
314         .init           = nft_dynset_init,
315         .destroy        = nft_dynset_destroy,
316         .activate       = nft_dynset_activate,
317         .deactivate     = nft_dynset_deactivate,
318         .dump           = nft_dynset_dump,
319 };
320 
321 struct nft_expr_type nft_dynset_type __read_mostly = {
322         .name           = "dynset",
323         .ops            = &nft_dynset_ops,
324         .policy         = nft_dynset_policy,
325         .maxattr        = NFTA_DYNSET_MAX,
326         .owner          = THIS_MODULE,
327 };
328 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp