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 #include <net/netns/generic.h>
15
16 extern unsigned int nf_tables_net_id;
17
18 struct nft_dynset {
19 struct nft_set *set;
20 struct nft_set_ext_tmpl tmpl;
21 enum nft_dynset_ops op:8;
22 u8 sreg_key;
23 u8 sreg_data;
24 bool invert;
25 u64 timeout;
26 struct nft_expr *expr;
27 struct nft_set_binding binding;
28 };
29
nft_dynset_new(struct nft_set * set,const struct nft_expr * expr,struct nft_regs * regs)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 (!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 ®s->data[priv->sreg_key], NULL,
44 ®s->data[priv->sreg_data],
45 timeout, 0, GFP_ATOMIC);
46 if (elem == NULL)
47 goto err1;
48
49 ext = nft_set_elem_ext(set, elem);
50 if (priv->expr != NULL &&
51 nft_expr_clone(nft_set_ext_expr(ext), priv->expr) < 0)
52 goto err2;
53
54 return elem;
55
56 err2:
57 nft_set_elem_destroy(set, elem, false);
58 err1:
59 if (set->size)
60 atomic_dec(&set->nelems);
61 return NULL;
62 }
63
nft_dynset_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)64 void nft_dynset_eval(const struct nft_expr *expr,
65 struct nft_regs *regs, const struct nft_pktinfo *pkt)
66 {
67 const struct nft_dynset *priv = nft_expr_priv(expr);
68 struct nft_set *set = priv->set;
69 const struct nft_set_ext *ext;
70 u64 timeout;
71
72 if (priv->op == NFT_DYNSET_OP_DELETE) {
73 set->ops->delete(set, ®s->data[priv->sreg_key]);
74 return;
75 }
76
77 if (set->ops->update(set, ®s->data[priv->sreg_key], nft_dynset_new,
78 expr, regs, &ext)) {
79 if (priv->op == NFT_DYNSET_OP_UPDATE &&
80 nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
81 timeout = priv->timeout ? : set->timeout;
82 *nft_set_ext_expiration(ext) = get_jiffies_64() + timeout;
83 }
84
85 nft_set_elem_update_expr(ext, regs, pkt);
86
87 if (priv->invert)
88 regs->verdict.code = NFT_BREAK;
89 return;
90 }
91
92 if (!priv->invert)
93 regs->verdict.code = NFT_BREAK;
94 }
95
96 static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
97 [NFTA_DYNSET_SET_NAME] = { .type = NLA_STRING,
98 .len = NFT_SET_MAXNAMELEN - 1 },
99 [NFTA_DYNSET_SET_ID] = { .type = NLA_U32 },
100 [NFTA_DYNSET_OP] = { .type = NLA_U32 },
101 [NFTA_DYNSET_SREG_KEY] = { .type = NLA_U32 },
102 [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },
103 [NFTA_DYNSET_TIMEOUT] = { .type = NLA_U64 },
104 [NFTA_DYNSET_EXPR] = { .type = NLA_NESTED },
105 [NFTA_DYNSET_FLAGS] = { .type = NLA_U32 },
106 };
107
nft_dynset_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])108 static int nft_dynset_init(const struct nft_ctx *ctx,
109 const struct nft_expr *expr,
110 const struct nlattr * const tb[])
111 {
112 struct nftables_pernet *nft_net = net_generic(ctx->net, nf_tables_net_id);
113 struct nft_dynset *priv = nft_expr_priv(expr);
114 u8 genmask = nft_genmask_next(ctx->net);
115 struct nft_set *set;
116 u64 timeout;
117 int err;
118
119 lockdep_assert_held(&nft_net->commit_mutex);
120
121 if (tb[NFTA_DYNSET_SET_NAME] == NULL ||
122 tb[NFTA_DYNSET_OP] == NULL ||
123 tb[NFTA_DYNSET_SREG_KEY] == NULL)
124 return -EINVAL;
125
126 if (tb[NFTA_DYNSET_FLAGS]) {
127 u32 flags = ntohl(nla_get_be32(tb[NFTA_DYNSET_FLAGS]));
128
129 if (flags & ~NFT_DYNSET_F_INV)
130 return -EOPNOTSUPP;
131 if (flags & NFT_DYNSET_F_INV)
132 priv->invert = true;
133 }
134
135 set = nft_set_lookup_global(ctx->net, ctx->table,
136 tb[NFTA_DYNSET_SET_NAME],
137 tb[NFTA_DYNSET_SET_ID], genmask);
138 if (IS_ERR(set))
139 return PTR_ERR(set);
140
141 if (set->flags & NFT_SET_OBJECT)
142 return -EOPNOTSUPP;
143
144 if (set->ops->update == NULL)
145 return -EOPNOTSUPP;
146
147 if (set->flags & NFT_SET_CONSTANT)
148 return -EBUSY;
149
150 priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
151 if (priv->op > NFT_DYNSET_OP_DELETE)
152 return -EOPNOTSUPP;
153
154 timeout = 0;
155 if (tb[NFTA_DYNSET_TIMEOUT] != NULL) {
156 if (!(set->flags & NFT_SET_TIMEOUT))
157 return -EOPNOTSUPP;
158
159 err = nf_msecs_to_jiffies64(tb[NFTA_DYNSET_TIMEOUT], &timeout);
160 if (err)
161 return err;
162 }
163
164 err = nft_parse_register_load(tb[NFTA_DYNSET_SREG_KEY], &priv->sreg_key,
165 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 -EOPNOTSUPP;
172 if (set->dtype == NFT_DATA_VERDICT)
173 return -EOPNOTSUPP;
174
175 err = nft_parse_register_load(tb[NFTA_DYNSET_SREG_DATA],
176 &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_TIMEOUT);
207 nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION);
208 }
209 }
210
211 priv->timeout = timeout;
212
213 err = nf_tables_bind_set(ctx, set, &priv->binding);
214 if (err < 0)
215 goto err_expr_free;
216
217 if (set->size == 0)
218 set->size = 0xffff;
219
220 priv->set = set;
221 return 0;
222
223 err_expr_free:
224 if (priv->expr != NULL)
225 nft_expr_destroy(ctx, priv->expr);
226 return err;
227 }
228
nft_dynset_deactivate(const struct nft_ctx * ctx,const struct nft_expr * expr,enum nft_trans_phase phase)229 static void nft_dynset_deactivate(const struct nft_ctx *ctx,
230 const struct nft_expr *expr,
231 enum nft_trans_phase phase)
232 {
233 struct nft_dynset *priv = nft_expr_priv(expr);
234
235 nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
236 }
237
nft_dynset_activate(const struct nft_ctx * ctx,const struct nft_expr * expr)238 static void nft_dynset_activate(const struct nft_ctx *ctx,
239 const struct nft_expr *expr)
240 {
241 struct nft_dynset *priv = nft_expr_priv(expr);
242
243 nf_tables_activate_set(ctx, priv->set);
244 }
245
nft_dynset_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)246 static void nft_dynset_destroy(const struct nft_ctx *ctx,
247 const struct nft_expr *expr)
248 {
249 struct nft_dynset *priv = nft_expr_priv(expr);
250
251 if (priv->expr != NULL)
252 nft_expr_destroy(ctx, priv->expr);
253
254 nf_tables_destroy_set(ctx, priv->set);
255 }
256
nft_dynset_dump(struct sk_buff * skb,const struct nft_expr * expr)257 static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
258 {
259 const struct nft_dynset *priv = nft_expr_priv(expr);
260 u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0;
261
262 if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
263 goto nla_put_failure;
264 if (priv->set->flags & NFT_SET_MAP &&
265 nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data))
266 goto nla_put_failure;
267 if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op)))
268 goto nla_put_failure;
269 if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
270 goto nla_put_failure;
271 if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT,
272 nf_jiffies64_to_msecs(priv->timeout),
273 NFTA_DYNSET_PAD))
274 goto nla_put_failure;
275 if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
276 goto nla_put_failure;
277 if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags)))
278 goto nla_put_failure;
279 return 0;
280
281 nla_put_failure:
282 return -1;
283 }
284
285 static const struct nft_expr_ops nft_dynset_ops = {
286 .type = &nft_dynset_type,
287 .size = NFT_EXPR_SIZE(sizeof(struct nft_dynset)),
288 .eval = nft_dynset_eval,
289 .init = nft_dynset_init,
290 .destroy = nft_dynset_destroy,
291 .activate = nft_dynset_activate,
292 .deactivate = nft_dynset_deactivate,
293 .dump = nft_dynset_dump,
294 };
295
296 struct nft_expr_type nft_dynset_type __read_mostly = {
297 .name = "dynset",
298 .ops = &nft_dynset_ops,
299 .policy = nft_dynset_policy,
300 .maxattr = NFTA_DYNSET_MAX,
301 .owner = THIS_MODULE,
302 };
303