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

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

Version: ~ [ linux-5.12 ] ~ [ linux-5.11.16 ] ~ [ linux-5.10.32 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.114 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.188 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.231 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.267 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.267 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ 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 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * Copyright (c) 2015 Patrick McHardy <kaber@trash.net>
  4  */
  5 
  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>
 14 
 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;
 25 };
 26 
 27 static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
 28                             struct nft_regs *regs)
 29 {
 30         const struct nft_dynset *priv = nft_expr_priv(expr);
 31         struct nft_set_ext *ext;
 32         u64 timeout;
 33         void *elem;
 34 
 35         if (!atomic_add_unless(&set->nelems, 1, set->size))
 36                 return NULL;
 37 
 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;
 45 
 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;
 50 
 51         return elem;
 52 
 53 err2:
 54         nft_set_elem_destroy(set, elem, false);
 55 err1:
 56         if (set->size)
 57                 atomic_dec(&set->nelems);
 58         return NULL;
 59 }
 60 
 61 void nft_dynset_eval(const struct nft_expr *expr,
 62                      struct nft_regs *regs, const struct nft_pktinfo *pkt)
 63 {
 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;
 68 
 69         if (priv->op == NFT_DYNSET_OP_DELETE) {
 70                 set->ops->delete(set, &regs->data[priv->sreg_key]);
 71                 return;
 72         }
 73 
 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;
 80                 }
 81 
 82                 nft_set_elem_update_expr(ext, regs, pkt);
 83 
 84                 if (priv->invert)
 85                         regs->verdict.code = NFT_BREAK;
 86                 return;
 87         }
 88 
 89         if (!priv->invert)
 90                 regs->verdict.code = NFT_BREAK;
 91 }
 92 
 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 },
103 };
104 
105 static int nft_dynset_init(const struct nft_ctx *ctx,
106                            const struct nft_expr *expr,
107                            const struct nlattr * const tb[])
108 {
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;
114 
115         lockdep_assert_held(&ctx->net->nft.commit_mutex);
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 = 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);
136 
137         if (set->ops->update == NULL)
138                 return -EOPNOTSUPP;
139 
140         if (set->flags & NFT_SET_CONSTANT)
141                 return -EBUSY;
142 
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;
154         }
155 
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])));
162         }
163 
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;
168 
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;
174 
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;
181 
182         if (tb[NFTA_DYNSET_EXPR] != NULL) {
183                 if (!(set->flags & NFT_SET_EVAL))
184                         return -EINVAL;
185 
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);
190 
191                 if (set->expr && set->expr->ops != priv->expr->ops) {
192                         err = -EOPNOTSUPP;
193                         goto err_expr_free;
194                 }
195         }
196 
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);
207         }
208 
209         priv->timeout = timeout;
210 
211         err = nf_tables_bind_set(ctx, set, &priv->binding);
212         if (err < 0)
213                 goto err_expr_free;
214 
215         if (set->size == 0)
216                 set->size = 0xffff;
217 
218         priv->set = set;
219         return 0;
220 
221 err_expr_free:
222         if (priv->expr != NULL)
223                 nft_expr_destroy(ctx, priv->expr);
224         return err;
225 }
226 
227 static void nft_dynset_deactivate(const struct nft_ctx *ctx,
228                                   const struct nft_expr *expr,
229                                   enum nft_trans_phase phase)
230 {
231         struct nft_dynset *priv = nft_expr_priv(expr);
232 
233         nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
234 }
235 
236 static void nft_dynset_activate(const struct nft_ctx *ctx,
237                                 const struct nft_expr *expr)
238 {
239         struct nft_dynset *priv = nft_expr_priv(expr);
240 
241         priv->set->use++;
242 }
243 
244 static void nft_dynset_destroy(const struct nft_ctx *ctx,
245                                const struct nft_expr *expr)
246 {
247         struct nft_dynset *priv = nft_expr_priv(expr);
248 
249         if (priv->expr != NULL)
250                 nft_expr_destroy(ctx, priv->expr);
251 
252         nf_tables_destroy_set(ctx, priv->set);
253 }
254 
255 static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
256 {
257         const struct nft_dynset *priv = nft_expr_priv(expr);
258         u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0;
259 
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;
278 
279 nla_put_failure:
280         return -1;
281 }
282 
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,
292 };
293 
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,
300 };
301 

~ [ 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