blob: 3948da380259538c2fd4823f65a9407241f8af4e [file] [log] [blame]
Patrick McHardy22fe54d2015-04-05 14:41:08 +02001/*
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
19struct 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;
Pablo Neira Ayusodbd2be02016-09-07 12:22:18 +020025 bool invert;
Patrick McHardy22fe54d2015-04-05 14:41:08 +020026 u64 timeout;
Patrick McHardy3e135cd2015-04-11 10:46:42 +010027 struct nft_expr *expr;
Patrick McHardy22fe54d2015-04-05 14:41:08 +020028 struct nft_set_binding binding;
29};
30
31static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
Patrick McHardya55e22e2015-04-11 02:27:31 +010032 struct nft_regs *regs)
Patrick McHardy22fe54d2015-04-05 14:41:08 +020033{
34 const struct nft_dynset *priv = nft_expr_priv(expr);
Patrick McHardy3e135cd2015-04-11 10:46:42 +010035 struct nft_set_ext *ext;
Patrick McHardy22fe54d2015-04-05 14:41:08 +020036 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,
Patrick McHardya55e22e2015-04-11 02:27:31 +010044 &regs->data[priv->sreg_key],
45 &regs->data[priv->sreg_data],
Patrick McHardy22fe54d2015-04-05 14:41:08 +020046 timeout, GFP_ATOMIC);
Liping Zhang61f9e292016-10-22 18:51:25 +080047 if (elem == NULL)
48 goto err1;
Patrick McHardy3e135cd2015-04-11 10:46:42 +010049
50 ext = nft_set_elem_ext(set, elem);
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +010051 if (priv->expr != NULL &&
52 nft_expr_clone(nft_set_ext_expr(ext), priv->expr) < 0)
Liping Zhang61f9e292016-10-22 18:51:25 +080053 goto err2;
Patrick McHardy3e135cd2015-04-11 10:46:42 +010054
Patrick McHardy22fe54d2015-04-05 14:41:08 +020055 return elem;
Liping Zhang61f9e292016-10-22 18:51:25 +080056
57err2:
58 nft_set_elem_destroy(set, elem, false);
59err1:
60 if (set->size)
61 atomic_dec(&set->nelems);
62 return NULL;
Patrick McHardy22fe54d2015-04-05 14:41:08 +020063}
64
65static void nft_dynset_eval(const struct nft_expr *expr,
Patrick McHardya55e22e2015-04-11 02:27:31 +010066 struct nft_regs *regs,
Patrick McHardy22fe54d2015-04-05 14:41:08 +020067 const struct nft_pktinfo *pkt)
68{
69 const struct nft_dynset *priv = nft_expr_priv(expr);
70 struct nft_set *set = priv->set;
71 const struct nft_set_ext *ext;
Patrick McHardy3e135cd2015-04-11 10:46:42 +010072 const struct nft_expr *sexpr;
Patrick McHardy22fe54d2015-04-05 14:41:08 +020073 u64 timeout;
74
Patrick McHardya55e22e2015-04-11 02:27:31 +010075 if (set->ops->update(set, &regs->data[priv->sreg_key], nft_dynset_new,
76 expr, regs, &ext)) {
Patrick McHardy3e135cd2015-04-11 10:46:42 +010077 sexpr = NULL;
78 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
79 sexpr = nft_set_ext_expr(ext);
80
Patrick McHardy22fe54d2015-04-05 14:41:08 +020081 if (priv->op == NFT_DYNSET_OP_UPDATE &&
82 nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
83 timeout = priv->timeout ? : set->timeout;
84 *nft_set_ext_expiration(ext) = jiffies + timeout;
Patrick McHardy3e135cd2015-04-11 10:46:42 +010085 } else if (sexpr == NULL)
86 goto out;
Patrick McHardy22fe54d2015-04-05 14:41:08 +020087
Patrick McHardy3e135cd2015-04-11 10:46:42 +010088 if (sexpr != NULL)
89 sexpr->ops->eval(sexpr, regs, pkt);
Pablo Neira Ayusodbd2be02016-09-07 12:22:18 +020090
91 if (priv->invert)
92 regs->verdict.code = NFT_BREAK;
Patrick McHardy3e135cd2015-04-11 10:46:42 +010093 return;
94 }
95out:
Pablo Neira Ayusodbd2be02016-09-07 12:22:18 +020096 if (!priv->invert)
97 regs->verdict.code = NFT_BREAK;
Patrick McHardy22fe54d2015-04-05 14:41:08 +020098}
99
100static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
Liping Zhangb2fbd042017-01-20 21:03:03 +0800101 [NFTA_DYNSET_SET_NAME] = { .type = NLA_STRING,
102 .len = NFT_SET_MAXNAMELEN - 1 },
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200103 [NFTA_DYNSET_SET_ID] = { .type = NLA_U32 },
104 [NFTA_DYNSET_OP] = { .type = NLA_U32 },
105 [NFTA_DYNSET_SREG_KEY] = { .type = NLA_U32 },
106 [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },
107 [NFTA_DYNSET_TIMEOUT] = { .type = NLA_U64 },
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100108 [NFTA_DYNSET_EXPR] = { .type = NLA_NESTED },
Pablo Neira Ayusodbd2be02016-09-07 12:22:18 +0200109 [NFTA_DYNSET_FLAGS] = { .type = NLA_U32 },
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200110};
111
112static int nft_dynset_init(const struct nft_ctx *ctx,
113 const struct nft_expr *expr,
114 const struct nlattr * const tb[])
115{
116 struct nft_dynset *priv = nft_expr_priv(expr);
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200117 u8 genmask = nft_genmask_next(ctx->net);
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200118 struct nft_set *set;
119 u64 timeout;
120 int err;
121
122 if (tb[NFTA_DYNSET_SET_NAME] == NULL ||
123 tb[NFTA_DYNSET_OP] == NULL ||
124 tb[NFTA_DYNSET_SREG_KEY] == NULL)
125 return -EINVAL;
126
Pablo Neira Ayusodbd2be02016-09-07 12:22:18 +0200127 if (tb[NFTA_DYNSET_FLAGS]) {
128 u32 flags = ntohl(nla_get_be32(tb[NFTA_DYNSET_FLAGS]));
129
130 if (flags & ~NFT_DYNSET_F_INV)
131 return -EINVAL;
132 if (flags & NFT_DYNSET_F_INV)
133 priv->invert = true;
134 }
135
Pablo Neira Ayusoc7a72e32017-03-06 17:46:20 +0100136 set = nft_set_lookup(ctx->net, ctx->table, tb[NFTA_DYNSET_SET_NAME],
137 tb[NFTA_DYNSET_SET_ID], genmask);
138 if (IS_ERR(set))
139 return PTR_ERR(set);
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200140
Liping Zhangbb6a6e82016-10-22 18:51:24 +0800141 if (set->ops->update == NULL)
142 return -EOPNOTSUPP;
143
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200144 if (set->flags & NFT_SET_CONSTANT)
145 return -EBUSY;
146
147 priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
148 switch (priv->op) {
149 case NFT_DYNSET_OP_ADD:
150 break;
151 case NFT_DYNSET_OP_UPDATE:
152 if (!(set->flags & NFT_SET_TIMEOUT))
153 return -EOPNOTSUPP;
154 break;
155 default:
156 return -EOPNOTSUPP;
157 }
158
159 timeout = 0;
160 if (tb[NFTA_DYNSET_TIMEOUT] != NULL) {
161 if (!(set->flags & NFT_SET_TIMEOUT))
162 return -EINVAL;
Anders K. Pedersena8b1e362016-10-09 13:49:02 +0000163 timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64(
164 tb[NFTA_DYNSET_TIMEOUT])));
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200165 }
166
Patrick McHardyb1c96ed2015-04-11 02:27:36 +0100167 priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]);
Patrick McHardyd07db982015-04-11 02:27:30 +0100168 err = nft_validate_register_load(priv->sreg_key, set->klen);;
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200169 if (err < 0)
170 return err;
171
172 if (tb[NFTA_DYNSET_SREG_DATA] != NULL) {
173 if (!(set->flags & NFT_SET_MAP))
174 return -EINVAL;
175 if (set->dtype == NFT_DATA_VERDICT)
176 return -EOPNOTSUPP;
177
Patrick McHardyb1c96ed2015-04-11 02:27:36 +0100178 priv->sreg_data = nft_parse_register(tb[NFTA_DYNSET_SREG_DATA]);
Patrick McHardyd07db982015-04-11 02:27:30 +0100179 err = nft_validate_register_load(priv->sreg_data, set->dlen);
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200180 if (err < 0)
181 return err;
182 } else if (set->flags & NFT_SET_MAP)
183 return -EINVAL;
184
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100185 if (tb[NFTA_DYNSET_EXPR] != NULL) {
186 if (!(set->flags & NFT_SET_EVAL))
187 return -EINVAL;
188 if (!(set->flags & NFT_SET_ANONYMOUS))
189 return -EOPNOTSUPP;
190
191 priv->expr = nft_expr_init(ctx, tb[NFTA_DYNSET_EXPR]);
192 if (IS_ERR(priv->expr))
193 return PTR_ERR(priv->expr);
194
195 err = -EOPNOTSUPP;
196 if (!(priv->expr->ops->type->flags & NFT_EXPR_STATEFUL))
197 goto err1;
198 } else if (set->flags & NFT_SET_EVAL)
199 return -EINVAL;
200
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200201 nft_set_ext_prepare(&priv->tmpl);
202 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_KEY, set->klen);
203 if (set->flags & NFT_SET_MAP)
204 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_DATA, set->dlen);
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100205 if (priv->expr != NULL)
206 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_EXPR,
207 priv->expr->ops->size);
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200208 if (set->flags & NFT_SET_TIMEOUT) {
209 if (timeout || set->timeout)
210 nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION);
211 }
212
213 priv->timeout = timeout;
214
215 err = nf_tables_bind_set(ctx, set, &priv->binding);
216 if (err < 0)
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100217 goto err1;
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200218
219 priv->set = set;
220 return 0;
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100221
222err1:
223 if (priv->expr != NULL)
224 nft_expr_destroy(ctx, priv->expr);
225 return err;
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200226}
227
228static void nft_dynset_destroy(const struct nft_ctx *ctx,
229 const struct nft_expr *expr)
230{
231 struct nft_dynset *priv = nft_expr_priv(expr);
232
233 nf_tables_unbind_set(ctx, priv->set, &priv->binding);
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100234 if (priv->expr != NULL)
235 nft_expr_destroy(ctx, priv->expr);
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200236}
237
238static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
239{
240 const struct nft_dynset *priv = nft_expr_priv(expr);
Pablo Neira Ayusodbd2be02016-09-07 12:22:18 +0200241 u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0;
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200242
Patrick McHardyb1c96ed2015-04-11 02:27:36 +0100243 if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200244 goto nla_put_failure;
245 if (priv->set->flags & NFT_SET_MAP &&
Patrick McHardyb1c96ed2015-04-11 02:27:36 +0100246 nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data))
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200247 goto nla_put_failure;
248 if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op)))
249 goto nla_put_failure;
250 if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
251 goto nla_put_failure;
Anders K. Pedersena8b1e362016-10-09 13:49:02 +0000252 if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT,
253 cpu_to_be64(jiffies_to_msecs(priv->timeout)),
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200254 NFTA_DYNSET_PAD))
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200255 goto nla_put_failure;
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100256 if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
257 goto nla_put_failure;
Pablo Neira Ayusodbd2be02016-09-07 12:22:18 +0200258 if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags)))
259 goto nla_put_failure;
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200260 return 0;
261
262nla_put_failure:
263 return -1;
264}
265
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200266static const struct nft_expr_ops nft_dynset_ops = {
267 .type = &nft_dynset_type,
268 .size = NFT_EXPR_SIZE(sizeof(struct nft_dynset)),
269 .eval = nft_dynset_eval,
270 .init = nft_dynset_init,
271 .destroy = nft_dynset_destroy,
272 .dump = nft_dynset_dump,
273};
274
Liping Zhang4e248772016-11-06 21:15:51 +0800275struct nft_expr_type nft_dynset_type __read_mostly = {
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200276 .name = "dynset",
277 .ops = &nft_dynset_ops,
278 .policy = nft_dynset_policy,
279 .maxattr = NFTA_DYNSET_MAX,
280 .owner = THIS_MODULE,
281};