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

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

Version: ~ [ linux-5.5-rc1 ] ~ [ linux-5.4.2 ] ~ [ linux-5.3.15 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.88 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.158 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.206 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.206 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.78 ] ~ [ 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.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         u64                             timeout;
 26         struct nft_expr                 *expr;
 27         struct nft_set_binding          binding;
 28 };
 29 
 30 static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
 31                             struct nft_regs *regs)
 32 {
 33         const struct nft_dynset *priv = nft_expr_priv(expr);
 34         struct nft_set_ext *ext;
 35         u64 timeout;
 36         void *elem;
 37 
 38         if (set->size && !atomic_add_unless(&set->nelems, 1, set->size))
 39                 return NULL;
 40 
 41         timeout = priv->timeout ? : set->timeout;
 42         elem = nft_set_elem_init(set, &priv->tmpl,
 43                                  &regs->data[priv->sreg_key],
 44                                  &regs->data[priv->sreg_data],
 45                                  timeout, GFP_ATOMIC);
 46         if (elem == NULL) {
 47                 if (set->size)
 48                         atomic_dec(&set->nelems);
 49                 return NULL;
 50         }
 51 
 52         ext = nft_set_elem_ext(set, elem);
 53         if (priv->expr != NULL &&
 54             nft_expr_clone(nft_set_ext_expr(ext), priv->expr) < 0)
 55                 return NULL;
 56 
 57         return elem;
 58 }
 59 
 60 static void nft_dynset_eval(const struct nft_expr *expr,
 61                             struct nft_regs *regs,
 62                             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         const struct nft_expr *sexpr;
 68         u64 timeout;
 69 
 70         if (set->ops->update(set, &regs->data[priv->sreg_key], nft_dynset_new,
 71                              expr, regs, &ext)) {
 72                 sexpr = NULL;
 73                 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
 74                         sexpr = nft_set_ext_expr(ext);
 75 
 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) = jiffies + timeout;
 80                 } else if (sexpr == NULL)
 81                         goto out;
 82 
 83                 if (sexpr != NULL)
 84                         sexpr->ops->eval(sexpr, regs, pkt);
 85                 return;
 86         }
 87 out:
 88         regs->verdict.code = NFT_BREAK;
 89 }
 90 
 91 static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
 92         [NFTA_DYNSET_SET_NAME]  = { .type = NLA_STRING },
 93         [NFTA_DYNSET_SET_ID]    = { .type = NLA_U32 },
 94         [NFTA_DYNSET_OP]        = { .type = NLA_U32 },
 95         [NFTA_DYNSET_SREG_KEY]  = { .type = NLA_U32 },
 96         [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },
 97         [NFTA_DYNSET_TIMEOUT]   = { .type = NLA_U64 },
 98         [NFTA_DYNSET_EXPR]      = { .type = NLA_NESTED },
 99 };
100 
101 static int nft_dynset_init(const struct nft_ctx *ctx,
102                            const struct nft_expr *expr,
103                            const struct nlattr * const tb[])
104 {
105         struct nft_dynset *priv = nft_expr_priv(expr);
106         struct nft_set *set;
107         u64 timeout;
108         int err;
109 
110         if (tb[NFTA_DYNSET_SET_NAME] == NULL ||
111             tb[NFTA_DYNSET_OP] == NULL ||
112             tb[NFTA_DYNSET_SREG_KEY] == NULL)
113                 return -EINVAL;
114 
115         set = nf_tables_set_lookup(ctx->table, tb[NFTA_DYNSET_SET_NAME]);
116         if (IS_ERR(set)) {
117                 if (tb[NFTA_DYNSET_SET_ID])
118                         set = nf_tables_set_lookup_byid(ctx->net,
119                                                         tb[NFTA_DYNSET_SET_ID]);
120                 if (IS_ERR(set))
121                         return PTR_ERR(set);
122         }
123 
124         if (set->flags & NFT_SET_CONSTANT)
125                 return -EBUSY;
126 
127         priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
128         switch (priv->op) {
129         case NFT_DYNSET_OP_ADD:
130                 break;
131         case NFT_DYNSET_OP_UPDATE:
132                 if (!(set->flags & NFT_SET_TIMEOUT))
133                         return -EOPNOTSUPP;
134                 break;
135         default:
136                 return -EOPNOTSUPP;
137         }
138 
139         timeout = 0;
140         if (tb[NFTA_DYNSET_TIMEOUT] != NULL) {
141                 if (!(set->flags & NFT_SET_TIMEOUT))
142                         return -EINVAL;
143                 timeout = be64_to_cpu(nla_get_be64(tb[NFTA_DYNSET_TIMEOUT]));
144         }
145 
146         priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]);
147         err = nft_validate_register_load(priv->sreg_key, set->klen);;
148         if (err < 0)
149                 return err;
150 
151         if (tb[NFTA_DYNSET_SREG_DATA] != NULL) {
152                 if (!(set->flags & NFT_SET_MAP))
153                         return -EINVAL;
154                 if (set->dtype == NFT_DATA_VERDICT)
155                         return -EOPNOTSUPP;
156 
157                 priv->sreg_data = nft_parse_register(tb[NFTA_DYNSET_SREG_DATA]);
158                 err = nft_validate_register_load(priv->sreg_data, set->dlen);
159                 if (err < 0)
160                         return err;
161         } else if (set->flags & NFT_SET_MAP)
162                 return -EINVAL;
163 
164         if (tb[NFTA_DYNSET_EXPR] != NULL) {
165                 if (!(set->flags & NFT_SET_EVAL))
166                         return -EINVAL;
167                 if (!(set->flags & NFT_SET_ANONYMOUS))
168                         return -EOPNOTSUPP;
169 
170                 priv->expr = nft_expr_init(ctx, tb[NFTA_DYNSET_EXPR]);
171                 if (IS_ERR(priv->expr))
172                         return PTR_ERR(priv->expr);
173 
174                 err = -EOPNOTSUPP;
175                 if (!(priv->expr->ops->type->flags & NFT_EXPR_STATEFUL))
176                         goto err1;
177         } else if (set->flags & NFT_SET_EVAL)
178                 return -EINVAL;
179 
180         nft_set_ext_prepare(&priv->tmpl);
181         nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_KEY, set->klen);
182         if (set->flags & NFT_SET_MAP)
183                 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_DATA, set->dlen);
184         if (priv->expr != NULL)
185                 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_EXPR,
186                                        priv->expr->ops->size);
187         if (set->flags & NFT_SET_TIMEOUT) {
188                 if (timeout || set->timeout)
189                         nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION);
190         }
191 
192         priv->timeout = timeout;
193 
194         err = nf_tables_bind_set(ctx, set, &priv->binding);
195         if (err < 0)
196                 goto err1;
197 
198         priv->set = set;
199         return 0;
200 
201 err1:
202         if (priv->expr != NULL)
203                 nft_expr_destroy(ctx, priv->expr);
204         return err;
205 }
206 
207 static void nft_dynset_destroy(const struct nft_ctx *ctx,
208                                const struct nft_expr *expr)
209 {
210         struct nft_dynset *priv = nft_expr_priv(expr);
211 
212         nf_tables_unbind_set(ctx, priv->set, &priv->binding);
213         if (priv->expr != NULL)
214                 nft_expr_destroy(ctx, priv->expr);
215 }
216 
217 static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
218 {
219         const struct nft_dynset *priv = nft_expr_priv(expr);
220 
221         if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
222                 goto nla_put_failure;
223         if (priv->set->flags & NFT_SET_MAP &&
224             nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data))
225                 goto nla_put_failure;
226         if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op)))
227                 goto nla_put_failure;
228         if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
229                 goto nla_put_failure;
230         if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT, cpu_to_be64(priv->timeout),
231                          NFTA_DYNSET_PAD))
232                 goto nla_put_failure;
233         if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
234                 goto nla_put_failure;
235         return 0;
236 
237 nla_put_failure:
238         return -1;
239 }
240 
241 static struct nft_expr_type nft_dynset_type;
242 static const struct nft_expr_ops nft_dynset_ops = {
243         .type           = &nft_dynset_type,
244         .size           = NFT_EXPR_SIZE(sizeof(struct nft_dynset)),
245         .eval           = nft_dynset_eval,
246         .init           = nft_dynset_init,
247         .destroy        = nft_dynset_destroy,
248         .dump           = nft_dynset_dump,
249 };
250 
251 static struct nft_expr_type nft_dynset_type __read_mostly = {
252         .name           = "dynset",
253         .ops            = &nft_dynset_ops,
254         .policy         = nft_dynset_policy,
255         .maxattr        = NFTA_DYNSET_MAX,
256         .owner          = THIS_MODULE,
257 };
258 
259 int __init nft_dynset_module_init(void)
260 {
261         return nft_register_expr(&nft_dynset_type);
262 }
263 
264 void nft_dynset_module_exit(void)
265 {
266         nft_unregister_expr(&nft_dynset_type);
267 }
268 

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