blob: b9dd4e96042616c5e61f797a0b7da09e856180df [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;
Liping Zhang7ad1c432017-04-23 18:29:30 +080085 }
Patrick McHardy22fe54d2015-04-05 14:41:08 +020086
Patrick McHardy3e135cd2015-04-11 10:46:42 +010087 if (sexpr != NULL)
88 sexpr->ops->eval(sexpr, regs, pkt);
Pablo Neira Ayusodbd2be02016-09-07 12:22:18 +020089
90 if (priv->invert)
91 regs->verdict.code = NFT_BREAK;
Patrick McHardy3e135cd2015-04-11 10:46:42 +010092 return;
93 }
Liping Zhang7ad1c432017-04-23 18:29:30 +080094
Pablo Neira Ayusodbd2be02016-09-07 12:22:18 +020095 if (!priv->invert)
96 regs->verdict.code = NFT_BREAK;
Patrick McHardy22fe54d2015-04-05 14:41:08 +020097}
98
99static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
100 [NFTA_DYNSET_SET_NAME] = { .type = NLA_STRING },
101 [NFTA_DYNSET_SET_ID] = { .type = NLA_U32 },
102 [NFTA_DYNSET_OP] = { .type = NLA_U32 },
103 [NFTA_DYNSET_SREG_KEY] = { .type = NLA_U32 },
104 [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },
105 [NFTA_DYNSET_TIMEOUT] = { .type = NLA_U64 },
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100106 [NFTA_DYNSET_EXPR] = { .type = NLA_NESTED },
Pablo Neira Ayusodbd2be02016-09-07 12:22:18 +0200107 [NFTA_DYNSET_FLAGS] = { .type = NLA_U32 },
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200108};
109
110static int nft_dynset_init(const struct nft_ctx *ctx,
111 const struct nft_expr *expr,
112 const struct nlattr * const tb[])
113{
114 struct nft_dynset *priv = nft_expr_priv(expr);
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200115 u8 genmask = nft_genmask_next(ctx->net);
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200116 struct nft_set *set;
117 u64 timeout;
118 int err;
119
120 if (tb[NFTA_DYNSET_SET_NAME] == NULL ||
121 tb[NFTA_DYNSET_OP] == NULL ||
122 tb[NFTA_DYNSET_SREG_KEY] == NULL)
123 return -EINVAL;
124
Pablo Neira Ayusodbd2be02016-09-07 12:22:18 +0200125 if (tb[NFTA_DYNSET_FLAGS]) {
126 u32 flags = ntohl(nla_get_be32(tb[NFTA_DYNSET_FLAGS]));
127
128 if (flags & ~NFT_DYNSET_F_INV)
129 return -EINVAL;
130 if (flags & NFT_DYNSET_F_INV)
131 priv->invert = true;
132 }
133
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200134 set = nf_tables_set_lookup(ctx->table, tb[NFTA_DYNSET_SET_NAME],
135 genmask);
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200136 if (IS_ERR(set)) {
137 if (tb[NFTA_DYNSET_SET_ID])
138 set = nf_tables_set_lookup_byid(ctx->net,
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200139 tb[NFTA_DYNSET_SET_ID],
140 genmask);
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200141 if (IS_ERR(set))
142 return PTR_ERR(set);
143 }
144
Liping Zhangbb6a6e82016-10-22 18:51:24 +0800145 if (set->ops->update == NULL)
146 return -EOPNOTSUPP;
147
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200148 if (set->flags & NFT_SET_CONSTANT)
149 return -EBUSY;
150
151 priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
152 switch (priv->op) {
153 case NFT_DYNSET_OP_ADD:
154 break;
155 case NFT_DYNSET_OP_UPDATE:
156 if (!(set->flags & NFT_SET_TIMEOUT))
157 return -EOPNOTSUPP;
158 break;
159 default:
160 return -EOPNOTSUPP;
161 }
162
163 timeout = 0;
164 if (tb[NFTA_DYNSET_TIMEOUT] != NULL) {
165 if (!(set->flags & NFT_SET_TIMEOUT))
166 return -EINVAL;
Anders K. Pedersena8b1e362016-10-09 13:49:02 +0000167 timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64(
168 tb[NFTA_DYNSET_TIMEOUT])));
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200169 }
170
Patrick McHardyb1c96ed2015-04-11 02:27:36 +0100171 priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]);
Patrick McHardyd07db982015-04-11 02:27:30 +0100172 err = nft_validate_register_load(priv->sreg_key, set->klen);;
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200173 if (err < 0)
174 return err;
175
176 if (tb[NFTA_DYNSET_SREG_DATA] != NULL) {
177 if (!(set->flags & NFT_SET_MAP))
178 return -EINVAL;
179 if (set->dtype == NFT_DATA_VERDICT)
180 return -EOPNOTSUPP;
181
Patrick McHardyb1c96ed2015-04-11 02:27:36 +0100182 priv->sreg_data = nft_parse_register(tb[NFTA_DYNSET_SREG_DATA]);
Patrick McHardyd07db982015-04-11 02:27:30 +0100183 err = nft_validate_register_load(priv->sreg_data, set->dlen);
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200184 if (err < 0)
185 return err;
186 } else if (set->flags & NFT_SET_MAP)
187 return -EINVAL;
188
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100189 if (tb[NFTA_DYNSET_EXPR] != NULL) {
190 if (!(set->flags & NFT_SET_EVAL))
191 return -EINVAL;
192 if (!(set->flags & NFT_SET_ANONYMOUS))
193 return -EOPNOTSUPP;
194
195 priv->expr = nft_expr_init(ctx, tb[NFTA_DYNSET_EXPR]);
196 if (IS_ERR(priv->expr))
197 return PTR_ERR(priv->expr);
198
199 err = -EOPNOTSUPP;
200 if (!(priv->expr->ops->type->flags & NFT_EXPR_STATEFUL))
201 goto err1;
202 } else if (set->flags & NFT_SET_EVAL)
203 return -EINVAL;
204
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200205 nft_set_ext_prepare(&priv->tmpl);
206 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_KEY, set->klen);
207 if (set->flags & NFT_SET_MAP)
208 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_DATA, set->dlen);
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100209 if (priv->expr != NULL)
210 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_EXPR,
211 priv->expr->ops->size);
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200212 if (set->flags & NFT_SET_TIMEOUT) {
213 if (timeout || set->timeout)
214 nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION);
215 }
216
217 priv->timeout = timeout;
218
219 err = nf_tables_bind_set(ctx, set, &priv->binding);
220 if (err < 0)
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100221 goto err1;
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200222
223 priv->set = set;
224 return 0;
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100225
226err1:
227 if (priv->expr != NULL)
228 nft_expr_destroy(ctx, priv->expr);
229 return err;
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200230}
231
232static void nft_dynset_destroy(const struct nft_ctx *ctx,
233 const struct nft_expr *expr)
234{
235 struct nft_dynset *priv = nft_expr_priv(expr);
236
237 nf_tables_unbind_set(ctx, priv->set, &priv->binding);
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100238 if (priv->expr != NULL)
239 nft_expr_destroy(ctx, priv->expr);
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200240}
241
242static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
243{
244 const struct nft_dynset *priv = nft_expr_priv(expr);
Pablo Neira Ayusodbd2be02016-09-07 12:22:18 +0200245 u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0;
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200246
Patrick McHardyb1c96ed2015-04-11 02:27:36 +0100247 if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200248 goto nla_put_failure;
249 if (priv->set->flags & NFT_SET_MAP &&
Patrick McHardyb1c96ed2015-04-11 02:27:36 +0100250 nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data))
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200251 goto nla_put_failure;
252 if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op)))
253 goto nla_put_failure;
254 if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
255 goto nla_put_failure;
Anders K. Pedersena8b1e362016-10-09 13:49:02 +0000256 if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT,
257 cpu_to_be64(jiffies_to_msecs(priv->timeout)),
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200258 NFTA_DYNSET_PAD))
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200259 goto nla_put_failure;
Patrick McHardy3e135cd2015-04-11 10:46:42 +0100260 if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
261 goto nla_put_failure;
Pablo Neira Ayusodbd2be02016-09-07 12:22:18 +0200262 if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags)))
263 goto nla_put_failure;
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200264 return 0;
265
266nla_put_failure:
267 return -1;
268}
269
270static struct nft_expr_type nft_dynset_type;
271static const struct nft_expr_ops nft_dynset_ops = {
272 .type = &nft_dynset_type,
273 .size = NFT_EXPR_SIZE(sizeof(struct nft_dynset)),
274 .eval = nft_dynset_eval,
275 .init = nft_dynset_init,
276 .destroy = nft_dynset_destroy,
277 .dump = nft_dynset_dump,
278};
279
280static struct nft_expr_type nft_dynset_type __read_mostly = {
281 .name = "dynset",
282 .ops = &nft_dynset_ops,
283 .policy = nft_dynset_policy,
284 .maxattr = NFTA_DYNSET_MAX,
285 .owner = THIS_MODULE,
286};
287
288int __init nft_dynset_module_init(void)
289{
290 return nft_register_expr(&nft_dynset_type);
291}
292
293void nft_dynset_module_exit(void)
294{
295 nft_unregister_expr(&nft_dynset_type);
296}