Linux 6.10-rc3
[linux-block.git] / net / netfilter / nf_tables_api.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
96518518 2/*
20a69341 3 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
96518518 4 *
96518518
PM
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
6 */
7
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/list.h>
11#include <linux/skbuff.h>
12#include <linux/netlink.h>
1ff75a3e 13#include <linux/vmalloc.h>
0eb71a9d 14#include <linux/rhashtable.h>
8e6cf365 15#include <linux/audit.h>
96518518
PM
16#include <linux/netfilter.h>
17#include <linux/netfilter/nfnetlink.h>
18#include <linux/netfilter/nf_tables.h>
3b49e2e9 19#include <net/netfilter/nf_flow_table.h>
96518518
PM
20#include <net/netfilter/nf_tables_core.h>
21#include <net/netfilter/nf_tables.h>
c9626a2c 22#include <net/netfilter/nf_tables_offload.h>
99633ab2 23#include <net/net_namespace.h>
96518518
PM
24#include <net/sock.h>
25
9332d27d 26#define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-"))
b462579b 27#define NFT_SET_MAX_ANONLEN 16
9332d27d 28
0854db2a
FW
29unsigned int nf_tables_net_id __read_mostly;
30
96518518 31static LIST_HEAD(nf_tables_expressions);
e5009240 32static LIST_HEAD(nf_tables_objects);
3b49e2e9 33static LIST_HEAD(nf_tables_flowtables);
0935d558 34static LIST_HEAD(nf_tables_destroy_list);
5f68718b 35static LIST_HEAD(nf_tables_gc_list);
0935d558 36static DEFINE_SPINLOCK(nf_tables_destroy_list_lock);
5f68718b 37static DEFINE_SPINLOCK(nf_tables_gc_list_lock);
96518518 38
a654de8f
PNA
39enum {
40 NFT_VALIDATE_SKIP = 0,
41 NFT_VALIDATE_NEED,
42 NFT_VALIDATE_DO,
43};
44
4d44175a
FW
45static struct rhltable nft_objname_ht;
46
1b2470e5
FW
47static u32 nft_chain_hash(const void *data, u32 len, u32 seed);
48static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed);
49static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *);
50
4d44175a
FW
51static u32 nft_objname_hash(const void *data, u32 len, u32 seed);
52static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed);
53static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *);
54
1b2470e5
FW
55static const struct rhashtable_params nft_chain_ht_params = {
56 .head_offset = offsetof(struct nft_chain, rhlhead),
57 .key_offset = offsetof(struct nft_chain, name),
58 .hashfn = nft_chain_hash,
59 .obj_hashfn = nft_chain_hash_obj,
60 .obj_cmpfn = nft_chain_hash_cmp,
1b2470e5
FW
61 .automatic_shrinking = true,
62};
63
4d44175a
FW
64static const struct rhashtable_params nft_objname_ht_params = {
65 .head_offset = offsetof(struct nft_object, rhlhead),
66 .key_offset = offsetof(struct nft_object, key),
67 .hashfn = nft_objname_hash,
68 .obj_hashfn = nft_objname_hash_obj,
69 .obj_cmpfn = nft_objname_hash_cmp,
70 .automatic_shrinking = true,
71};
72
c520292f
RGB
73struct nft_audit_data {
74 struct nft_table *table;
75 int entries;
76 int op;
77 struct list_head list;
78};
79
80static const u8 nft2audit_op[NFT_MSG_MAX] = { // enum nf_tables_msg_types
81 [NFT_MSG_NEWTABLE] = AUDIT_NFT_OP_TABLE_REGISTER,
82 [NFT_MSG_GETTABLE] = AUDIT_NFT_OP_INVALID,
83 [NFT_MSG_DELTABLE] = AUDIT_NFT_OP_TABLE_UNREGISTER,
84 [NFT_MSG_NEWCHAIN] = AUDIT_NFT_OP_CHAIN_REGISTER,
85 [NFT_MSG_GETCHAIN] = AUDIT_NFT_OP_INVALID,
86 [NFT_MSG_DELCHAIN] = AUDIT_NFT_OP_CHAIN_UNREGISTER,
87 [NFT_MSG_NEWRULE] = AUDIT_NFT_OP_RULE_REGISTER,
88 [NFT_MSG_GETRULE] = AUDIT_NFT_OP_INVALID,
89 [NFT_MSG_DELRULE] = AUDIT_NFT_OP_RULE_UNREGISTER,
90 [NFT_MSG_NEWSET] = AUDIT_NFT_OP_SET_REGISTER,
91 [NFT_MSG_GETSET] = AUDIT_NFT_OP_INVALID,
92 [NFT_MSG_DELSET] = AUDIT_NFT_OP_SET_UNREGISTER,
93 [NFT_MSG_NEWSETELEM] = AUDIT_NFT_OP_SETELEM_REGISTER,
94 [NFT_MSG_GETSETELEM] = AUDIT_NFT_OP_INVALID,
95 [NFT_MSG_DELSETELEM] = AUDIT_NFT_OP_SETELEM_UNREGISTER,
96 [NFT_MSG_NEWGEN] = AUDIT_NFT_OP_GEN_REGISTER,
97 [NFT_MSG_GETGEN] = AUDIT_NFT_OP_INVALID,
98 [NFT_MSG_TRACE] = AUDIT_NFT_OP_INVALID,
99 [NFT_MSG_NEWOBJ] = AUDIT_NFT_OP_OBJ_REGISTER,
100 [NFT_MSG_GETOBJ] = AUDIT_NFT_OP_INVALID,
101 [NFT_MSG_DELOBJ] = AUDIT_NFT_OP_OBJ_UNREGISTER,
102 [NFT_MSG_GETOBJ_RESET] = AUDIT_NFT_OP_OBJ_RESET,
103 [NFT_MSG_NEWFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_REGISTER,
104 [NFT_MSG_GETFLOWTABLE] = AUDIT_NFT_OP_INVALID,
105 [NFT_MSG_DELFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_UNREGISTER,
7e9be112 106 [NFT_MSG_GETSETELEM_RESET] = AUDIT_NFT_OP_SETELEM_RESET,
c520292f
RGB
107};
108
00c320f9 109static void nft_validate_state_update(struct nft_table *table, u8 new_validate_state)
a654de8f 110{
00c320f9 111 switch (table->validate_state) {
a654de8f
PNA
112 case NFT_VALIDATE_SKIP:
113 WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO);
114 break;
115 case NFT_VALIDATE_NEED:
116 break;
117 case NFT_VALIDATE_DO:
118 if (new_validate_state == NFT_VALIDATE_NEED)
119 return;
120 }
121
00c320f9 122 table->validate_state = new_validate_state;
a654de8f 123}
0935d558
FW
124static void nf_tables_trans_destroy_work(struct work_struct *w);
125static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work);
a654de8f 126
5f68718b
PNA
127static void nft_trans_gc_work(struct work_struct *work);
128static DECLARE_WORK(trans_gc_work, nft_trans_gc_work);
129
7c95f6d8 130static void nft_ctx_init(struct nft_ctx *ctx,
633c9a84 131 struct net *net,
7c95f6d8
PNA
132 const struct sk_buff *skb,
133 const struct nlmsghdr *nlh,
36596dad 134 u8 family,
7c95f6d8
PNA
135 struct nft_table *table,
136 struct nft_chain *chain,
137 const struct nlattr * const *nla)
138{
633c9a84 139 ctx->net = net;
36596dad 140 ctx->family = family;
26b2f552 141 ctx->level = 0;
128ad332
PNA
142 ctx->table = table;
143 ctx->chain = chain;
144 ctx->nla = nla;
145 ctx->portid = NETLINK_CB(skb).portid;
146 ctx->report = nlmsg_report(nlh);
c9626a2c 147 ctx->flags = nlh->nlmsg_flags;
128ad332 148 ctx->seq = nlh->nlmsg_seq;
7c95f6d8
PNA
149}
150
8411b644
PNA
151static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx,
152 int msg_type, u32 size, gfp_t gfp)
1081d11b
PNA
153{
154 struct nft_trans *trans;
155
8411b644 156 trans = kzalloc(sizeof(struct nft_trans) + size, gfp);
1081d11b
PNA
157 if (trans == NULL)
158 return NULL;
159
58007785 160 INIT_LIST_HEAD(&trans->list);
938154b9 161 INIT_LIST_HEAD(&trans->binding_list);
b380e5c7 162 trans->msg_type = msg_type;
1081d11b
PNA
163 trans->ctx = *ctx;
164
165 return trans;
166}
167
8411b644
PNA
168static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx,
169 int msg_type, u32 size)
170{
171 return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL);
172}
173
938154b9 174static void nft_trans_list_del(struct nft_trans *trans)
1081d11b
PNA
175{
176 list_del(&trans->list);
938154b9
PNA
177 list_del(&trans->binding_list);
178}
179
180static void nft_trans_destroy(struct nft_trans *trans)
181{
182 nft_trans_list_del(trans);
1081d11b
PNA
183 kfree(trans);
184}
185
26b5a571
PNA
186static void __nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set,
187 bool bind)
f6ac8585 188{
0854db2a 189 struct nftables_pernet *nft_net;
f6ac8585
PNA
190 struct net *net = ctx->net;
191 struct nft_trans *trans;
192
193 if (!nft_set_is_anonymous(set))
194 return;
195
d59d2f82 196 nft_net = nft_pernet(net);
0854db2a 197 list_for_each_entry_reverse(trans, &nft_net->commit_list, list) {
6a0a8d10
PNA
198 switch (trans->msg_type) {
199 case NFT_MSG_NEWSET:
200 if (nft_trans_set(trans) == set)
26b5a571 201 nft_trans_set_bound(trans) = bind;
6a0a8d10
PNA
202 break;
203 case NFT_MSG_NEWSETELEM:
204 if (nft_trans_elem_set(trans) == set)
26b5a571 205 nft_trans_elem_set_bound(trans) = bind;
f6ac8585
PNA
206 break;
207 }
208 }
209}
210
26b5a571
PNA
211static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set)
212{
213 return __nft_set_trans_bind(ctx, set, true);
214}
215
216static void nft_set_trans_unbind(const struct nft_ctx *ctx, struct nft_set *set)
217{
218 return __nft_set_trans_bind(ctx, set, false);
219}
220
221static void __nft_chain_trans_bind(const struct nft_ctx *ctx,
222 struct nft_chain *chain, bool bind)
4bedf9ee
PNA
223{
224 struct nftables_pernet *nft_net;
225 struct net *net = ctx->net;
226 struct nft_trans *trans;
227
228 if (!nft_chain_binding(chain))
229 return;
230
231 nft_net = nft_pernet(net);
232 list_for_each_entry_reverse(trans, &nft_net->commit_list, list) {
233 switch (trans->msg_type) {
234 case NFT_MSG_NEWCHAIN:
235 if (nft_trans_chain(trans) == chain)
26b5a571 236 nft_trans_chain_bound(trans) = bind;
4bedf9ee
PNA
237 break;
238 case NFT_MSG_NEWRULE:
239 if (trans->ctx.chain == chain)
26b5a571 240 nft_trans_rule_bound(trans) = bind;
4bedf9ee
PNA
241 break;
242 }
243 }
244}
245
26b5a571
PNA
246static void nft_chain_trans_bind(const struct nft_ctx *ctx,
247 struct nft_chain *chain)
248{
249 __nft_chain_trans_bind(ctx, chain, true);
250}
251
4bedf9ee
PNA
252int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain)
253{
254 if (!nft_chain_binding(chain))
255 return 0;
256
257 if (nft_chain_binding(ctx->chain))
258 return -EOPNOTSUPP;
259
260 if (chain->bound)
261 return -EBUSY;
262
1689f259
PNA
263 if (!nft_use_inc(&chain->use))
264 return -EMFILE;
265
4bedf9ee 266 chain->bound = true;
4bedf9ee
PNA
267 nft_chain_trans_bind(ctx, chain);
268
269 return 0;
270}
271
26b5a571
PNA
272void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain)
273{
274 __nft_chain_trans_bind(ctx, chain, false);
275}
276
d54725cd
PNA
277static int nft_netdev_register_hooks(struct net *net,
278 struct list_head *hook_list)
279{
280 struct nft_hook *hook;
281 int err, j;
282
283 j = 0;
284 list_for_each_entry(hook, hook_list, list) {
285 err = nf_register_net_hook(net, &hook->ops);
286 if (err < 0)
287 goto err_register;
288
289 j++;
290 }
291 return 0;
292
293err_register:
294 list_for_each_entry(hook, hook_list, list) {
295 if (j-- <= 0)
296 break;
297
298 nf_unregister_net_hook(net, &hook->ops);
299 }
300 return err;
301}
302
303static void nft_netdev_unregister_hooks(struct net *net,
f9a43007
PNA
304 struct list_head *hook_list,
305 bool release_netdev)
d54725cd 306{
f9a43007 307 struct nft_hook *hook, *next;
d54725cd 308
f9a43007 309 list_for_each_entry_safe(hook, next, hook_list, list) {
d54725cd 310 nf_unregister_net_hook(net, &hook->ops);
f9a43007
PNA
311 if (release_netdev) {
312 list_del(&hook->list);
313 kfree_rcu(hook, rcu);
314 }
315 }
d54725cd
PNA
316}
317
c974a3a3
PNA
318static int nf_tables_register_hook(struct net *net,
319 const struct nft_table *table,
320 struct nft_chain *chain)
d8ee8f7c 321{
d54725cd 322 struct nft_base_chain *basechain;
a37061a6 323 const struct nf_hook_ops *ops;
ae6153b5 324
d8ee8f7c 325 if (table->flags & NFT_TABLE_F_DORMANT ||
f323d954 326 !nft_is_base_chain(chain))
d8ee8f7c
PNA
327 return 0;
328
4e25ceb8
FW
329 basechain = nft_base_chain(chain);
330 ops = &basechain->ops;
ae6153b5 331
4e25ceb8
FW
332 if (basechain->type->ops_register)
333 return basechain->type->ops_register(net, ops);
334
d3519cb8 335 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
1e9451cb
FW
336 return nft_netdev_register_hooks(net, &basechain->hook_list);
337
338 return nf_register_net_hook(net, &basechain->ops);
d8ee8f7c
PNA
339}
340
f9a43007
PNA
341static void __nf_tables_unregister_hook(struct net *net,
342 const struct nft_table *table,
343 struct nft_chain *chain,
344 bool release_netdev)
c5598794 345{
d54725cd 346 struct nft_base_chain *basechain;
4e25ceb8
FW
347 const struct nf_hook_ops *ops;
348
d8ee8f7c 349 if (table->flags & NFT_TABLE_F_DORMANT ||
f323d954 350 !nft_is_base_chain(chain))
d8ee8f7c 351 return;
4e25ceb8
FW
352 basechain = nft_base_chain(chain);
353 ops = &basechain->ops;
d8ee8f7c 354
4e25ceb8
FW
355 if (basechain->type->ops_unregister)
356 return basechain->type->ops_unregister(net, ops);
357
d3519cb8 358 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
f9a43007
PNA
359 nft_netdev_unregister_hooks(net, &basechain->hook_list,
360 release_netdev);
1e9451cb
FW
361 else
362 nf_unregister_net_hook(net, &basechain->ops);
c5598794
AB
363}
364
f9a43007
PNA
365static void nf_tables_unregister_hook(struct net *net,
366 const struct nft_table *table,
367 struct nft_chain *chain)
368{
369 return __nf_tables_unregister_hook(net, table, chain, false);
370}
371
0854db2a
FW
372static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *trans)
373{
d59d2f82 374 struct nftables_pernet *nft_net = nft_pernet(net);
0854db2a 375
938154b9
PNA
376 switch (trans->msg_type) {
377 case NFT_MSG_NEWSET:
378 if (!nft_trans_set_update(trans) &&
379 nft_set_is_anonymous(nft_trans_set(trans)))
380 list_add_tail(&trans->binding_list, &nft_net->binding_list);
381 break;
62e1e94b
PNA
382 case NFT_MSG_NEWCHAIN:
383 if (!nft_trans_chain_update(trans) &&
384 nft_chain_binding(nft_trans_chain(trans)))
385 list_add_tail(&trans->binding_list, &nft_net->binding_list);
386 break;
938154b9
PNA
387 }
388
0854db2a
FW
389 list_add_tail(&trans->list, &nft_net->commit_list);
390}
391
ee01d542
AB
392static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
393{
394 struct nft_trans *trans;
395
396 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
397 if (trans == NULL)
398 return -ENOMEM;
399
400 if (msg_type == NFT_MSG_NEWTABLE)
f2a6d766 401 nft_activate_next(ctx->net, ctx->table);
ee01d542 402
0854db2a 403 nft_trans_commit_list_add_tail(ctx->net, trans);
ee01d542
AB
404 return 0;
405}
406
407static int nft_deltable(struct nft_ctx *ctx)
408{
409 int err;
410
411 err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
412 if (err < 0)
413 return err;
414
f2a6d766 415 nft_deactivate_next(ctx->net, ctx->table);
ee01d542
AB
416 return err;
417}
418
66293c46 419static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
ee01d542
AB
420{
421 struct nft_trans *trans;
422
423 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
424 if (trans == NULL)
66293c46 425 return ERR_PTR(-ENOMEM);
ee01d542 426
74cccc3d 427 if (msg_type == NFT_MSG_NEWCHAIN) {
664b0f8c 428 nft_activate_next(ctx->net, ctx->chain);
ee01d542 429
74cccc3d
PNA
430 if (ctx->nla[NFTA_CHAIN_ID]) {
431 nft_trans_chain_id(trans) =
432 ntohl(nla_get_be32(ctx->nla[NFTA_CHAIN_ID]));
433 }
434 }
4bedf9ee 435 nft_trans_chain(trans) = ctx->chain;
0854db2a 436 nft_trans_commit_list_add_tail(ctx->net, trans);
4bedf9ee 437
66293c46 438 return trans;
ee01d542
AB
439}
440
441static int nft_delchain(struct nft_ctx *ctx)
442{
66293c46 443 struct nft_trans *trans;
ee01d542 444
66293c46
FW
445 trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
446 if (IS_ERR(trans))
447 return PTR_ERR(trans);
ee01d542 448
1689f259 449 nft_use_dec(&ctx->table->use);
664b0f8c 450 nft_deactivate_next(ctx->net, ctx->chain);
ee01d542 451
66293c46 452 return 0;
ee01d542
AB
453}
454
4bedf9ee 455void nft_rule_expr_activate(const struct nft_ctx *ctx, struct nft_rule *rule)
bb7b40ae
PNA
456{
457 struct nft_expr *expr;
458
459 expr = nft_expr_first(rule);
31cc578a 460 while (nft_expr_more(rule, expr)) {
bb7b40ae
PNA
461 if (expr->ops->activate)
462 expr->ops->activate(ctx, expr);
463
464 expr = nft_expr_next(expr);
465 }
466}
467
4bedf9ee
PNA
468void nft_rule_expr_deactivate(const struct nft_ctx *ctx, struct nft_rule *rule,
469 enum nft_trans_phase phase)
bb7b40ae
PNA
470{
471 struct nft_expr *expr;
472
473 expr = nft_expr_first(rule);
31cc578a 474 while (nft_expr_more(rule, expr)) {
bb7b40ae 475 if (expr->ops->deactivate)
f6ac8585 476 expr->ops->deactivate(ctx, expr, phase);
bb7b40ae
PNA
477
478 expr = nft_expr_next(expr);
479 }
480}
481
ee01d542
AB
482static int
483nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
484{
485 /* You cannot delete the same rule twice */
889f7ee7
PNA
486 if (nft_is_active_next(ctx->net, rule)) {
487 nft_deactivate_next(ctx->net, rule);
1689f259 488 nft_use_dec(&ctx->chain->use);
ee01d542
AB
489 return 0;
490 }
491 return -ENOENT;
492}
493
494static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
495 struct nft_rule *rule)
496{
497 struct nft_trans *trans;
498
499 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
500 if (trans == NULL)
501 return NULL;
502
1a94e38d
PNA
503 if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) {
504 nft_trans_rule_id(trans) =
505 ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID]));
506 }
ee01d542 507 nft_trans_rule(trans) = rule;
0854db2a 508 nft_trans_commit_list_add_tail(ctx->net, trans);
ee01d542
AB
509
510 return trans;
511}
512
513static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
514{
63b48c73 515 struct nft_flow_rule *flow;
ee01d542
AB
516 struct nft_trans *trans;
517 int err;
518
519 trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
520 if (trans == NULL)
521 return -ENOMEM;
522
63b48c73
PNA
523 if (ctx->chain->flags & NFT_CHAIN_HW_OFFLOAD) {
524 flow = nft_flow_rule_create(ctx->net, rule);
525 if (IS_ERR(flow)) {
526 nft_trans_destroy(trans);
527 return PTR_ERR(flow);
528 }
529
530 nft_trans_flow_rule(trans) = flow;
531 }
532
ee01d542
AB
533 err = nf_tables_delrule_deactivate(ctx, rule);
534 if (err < 0) {
535 nft_trans_destroy(trans);
536 return err;
537 }
f6ac8585 538 nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE);
ee01d542
AB
539
540 return 0;
541}
542
543static int nft_delrule_by_chain(struct nft_ctx *ctx)
544{
545 struct nft_rule *rule;
546 int err;
547
548 list_for_each_entry(rule, &ctx->chain->rules, list) {
23b7ca4f
PNA
549 if (!nft_is_active_next(ctx->net, rule))
550 continue;
551
ee01d542
AB
552 err = nft_delrule(ctx, rule);
553 if (err < 0)
554 return err;
555 }
556 return 0;
557}
558
123b9961
PNA
559static int __nft_trans_set_add(const struct nft_ctx *ctx, int msg_type,
560 struct nft_set *set,
561 const struct nft_set_desc *desc)
ee01d542
AB
562{
563 struct nft_trans *trans;
564
565 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
566 if (trans == NULL)
567 return -ENOMEM;
568
123b9961 569 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] && !desc) {
ee01d542
AB
570 nft_trans_set_id(trans) =
571 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
37a9cc52 572 nft_activate_next(ctx->net, set);
ee01d542
AB
573 }
574 nft_trans_set(trans) = set;
123b9961
PNA
575 if (desc) {
576 nft_trans_set_update(trans) = true;
577 nft_trans_set_gc_int(trans) = desc->gc_int;
578 nft_trans_set_timeout(trans) = desc->timeout;
96b2ef9b 579 nft_trans_set_size(trans) = desc->size;
123b9961 580 }
0854db2a 581 nft_trans_commit_list_add_tail(ctx->net, trans);
ee01d542
AB
582
583 return 0;
584}
585
123b9961
PNA
586static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type,
587 struct nft_set *set)
588{
589 return __nft_trans_set_add(ctx, msg_type, set, NULL);
590}
591
628bd3e4
PNA
592static int nft_mapelem_deactivate(const struct nft_ctx *ctx,
593 struct nft_set *set,
594 const struct nft_set_iter *iter,
0e1ea651 595 struct nft_elem_priv *elem_priv)
628bd3e4 596{
e79b47a8
PNA
597 struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
598
599 if (!nft_set_elem_active(ext, iter->genmask))
600 return 0;
601
602 nft_set_elem_change_active(ctx->net, set, ext);
0e1ea651 603 nft_setelem_data_deactivate(ctx->net, set, elem_priv);
628bd3e4
PNA
604
605 return 0;
606}
607
608struct nft_set_elem_catchall {
609 struct list_head list;
610 struct rcu_head rcu;
9dad402b 611 struct nft_elem_priv *elem;
628bd3e4
PNA
612};
613
614static void nft_map_catchall_deactivate(const struct nft_ctx *ctx,
615 struct nft_set *set)
616{
617 u8 genmask = nft_genmask_next(ctx->net);
618 struct nft_set_elem_catchall *catchall;
628bd3e4
PNA
619 struct nft_set_ext *ext;
620
621 list_for_each_entry(catchall, &set->catchall_list, list) {
622 ext = nft_set_elem_ext(set, catchall->elem);
623 if (!nft_set_elem_active(ext, genmask))
624 continue;
625
e79b47a8 626 nft_set_elem_change_active(ctx->net, set, ext);
0e1ea651 627 nft_setelem_data_deactivate(ctx->net, set, catchall->elem);
628bd3e4
PNA
628 break;
629 }
630}
631
632static void nft_map_deactivate(const struct nft_ctx *ctx, struct nft_set *set)
633{
634 struct nft_set_iter iter = {
635 .genmask = nft_genmask_next(ctx->net),
29b359cf 636 .type = NFT_ITER_UPDATE,
628bd3e4
PNA
637 .fn = nft_mapelem_deactivate,
638 };
639
640 set->ops->walk(ctx, set, &iter);
641 WARN_ON_ONCE(iter.err);
642
643 nft_map_catchall_deactivate(ctx, set);
644}
645
cd5125d8 646static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set)
ee01d542
AB
647{
648 int err;
649
650 err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
651 if (err < 0)
652 return err;
653
628bd3e4
PNA
654 if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
655 nft_map_deactivate(ctx, set);
656
37a9cc52 657 nft_deactivate_next(ctx->net, set);
1689f259 658 nft_use_dec(&ctx->table->use);
ee01d542
AB
659
660 return err;
661}
662
e5009240
PNA
663static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type,
664 struct nft_object *obj)
665{
666 struct nft_trans *trans;
667
668 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj));
669 if (trans == NULL)
670 return -ENOMEM;
671
672 if (msg_type == NFT_MSG_NEWOBJ)
673 nft_activate_next(ctx->net, obj);
674
675 nft_trans_obj(trans) = obj;
0854db2a 676 nft_trans_commit_list_add_tail(ctx->net, trans);
e5009240
PNA
677
678 return 0;
679}
680
681static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj)
682{
683 int err;
684
685 err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj);
686 if (err < 0)
687 return err;
688
689 nft_deactivate_next(ctx->net, obj);
1689f259 690 nft_use_dec(&ctx->table->use);
e5009240
PNA
691
692 return err;
693}
694
d472e985
PNA
695static struct nft_trans *
696nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type,
697 struct nft_flowtable *flowtable)
3b49e2e9
PNA
698{
699 struct nft_trans *trans;
700
701 trans = nft_trans_alloc(ctx, msg_type,
702 sizeof(struct nft_trans_flowtable));
703 if (trans == NULL)
d472e985 704 return ERR_PTR(-ENOMEM);
3b49e2e9
PNA
705
706 if (msg_type == NFT_MSG_NEWFLOWTABLE)
707 nft_activate_next(ctx->net, flowtable);
708
2c9e4559 709 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
3b49e2e9 710 nft_trans_flowtable(trans) = flowtable;
0854db2a 711 nft_trans_commit_list_add_tail(ctx->net, trans);
3b49e2e9 712
d472e985 713 return trans;
3b49e2e9
PNA
714}
715
716static int nft_delflowtable(struct nft_ctx *ctx,
717 struct nft_flowtable *flowtable)
718{
d472e985 719 struct nft_trans *trans;
3b49e2e9 720
d472e985
PNA
721 trans = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable);
722 if (IS_ERR(trans))
723 return PTR_ERR(trans);
3b49e2e9
PNA
724
725 nft_deactivate_next(ctx->net, flowtable);
1689f259 726 nft_use_dec(&ctx->table->use);
3b49e2e9 727
d472e985 728 return 0;
3b49e2e9
PNA
729}
730
34cc9e52
PNA
731static void __nft_reg_track_clobber(struct nft_regs_track *track, u8 dreg)
732{
733 int i;
734
735 for (i = track->regs[dreg].num_reg; i > 0; i--)
736 __nft_reg_track_cancel(track, dreg - i);
737}
738
739static void __nft_reg_track_update(struct nft_regs_track *track,
740 const struct nft_expr *expr,
741 u8 dreg, u8 num_reg)
742{
743 track->regs[dreg].selector = expr;
744 track->regs[dreg].bitwise = NULL;
745 track->regs[dreg].num_reg = num_reg;
746}
747
748void nft_reg_track_update(struct nft_regs_track *track,
749 const struct nft_expr *expr, u8 dreg, u8 len)
750{
751 unsigned int regcount;
752 int i;
753
754 __nft_reg_track_clobber(track, dreg);
755
756 regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE);
757 for (i = 0; i < regcount; i++, dreg++)
758 __nft_reg_track_update(track, expr, dreg, i);
759}
760EXPORT_SYMBOL_GPL(nft_reg_track_update);
761
762void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len)
763{
764 unsigned int regcount;
765 int i;
766
767 __nft_reg_track_clobber(track, dreg);
768
769 regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE);
770 for (i = 0; i < regcount; i++, dreg++)
771 __nft_reg_track_cancel(track, dreg);
772}
773EXPORT_SYMBOL_GPL(nft_reg_track_cancel);
774
775void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg)
776{
777 track->regs[dreg].selector = NULL;
778 track->regs[dreg].bitwise = NULL;
779 track->regs[dreg].num_reg = 0;
780}
781EXPORT_SYMBOL_GPL(__nft_reg_track_cancel);
782
96518518
PM
783/*
784 * Tables
785 */
786
36596dad 787static struct nft_table *nft_table_lookup(const struct net *net,
f2a6d766 788 const struct nlattr *nla,
6001a930 789 u8 family, u8 genmask, u32 nlpid)
96518518 790{
0854db2a 791 struct nftables_pernet *nft_net;
96518518
PM
792 struct nft_table *table;
793
cac20fcd
PNA
794 if (nla == NULL)
795 return ERR_PTR(-EINVAL);
796
d59d2f82 797 nft_net = nft_pernet(net);
0854db2a
FW
798 list_for_each_entry_rcu(table, &nft_net->tables, list,
799 lockdep_is_held(&nft_net->commit_mutex)) {
f2a6d766 800 if (!nla_strcmp(nla, table->name) &&
98319cb9 801 table->family == family &&
6001a930
PNA
802 nft_active_genmask(table, genmask)) {
803 if (nft_table_has_owner(table) &&
53479909 804 nlpid && table->nlpid != nlpid)
6001a930
PNA
805 return ERR_PTR(-EPERM);
806
96518518 807 return table;
6001a930 808 }
96518518 809 }
cac20fcd
PNA
810
811 return ERR_PTR(-ENOENT);
96518518
PM
812}
813
3ecbfd65
HS
814static struct nft_table *nft_table_lookup_byhandle(const struct net *net,
815 const struct nlattr *nla,
f6e1532a 816 int family, u8 genmask, u32 nlpid)
3ecbfd65 817{
0854db2a 818 struct nftables_pernet *nft_net;
3ecbfd65
HS
819 struct nft_table *table;
820
d59d2f82 821 nft_net = nft_pernet(net);
0854db2a 822 list_for_each_entry(table, &nft_net->tables, list) {
3ecbfd65 823 if (be64_to_cpu(nla_get_be64(nla)) == table->handle &&
f6e1532a 824 table->family == family &&
e31f072f
PNA
825 nft_active_genmask(table, genmask)) {
826 if (nft_table_has_owner(table) &&
827 nlpid && table->nlpid != nlpid)
828 return ERR_PTR(-EPERM);
829
3ecbfd65 830 return table;
e31f072f 831 }
3ecbfd65 832 }
3ecbfd65
HS
833
834 return ERR_PTR(-ENOENT);
835}
836
96518518
PM
837static inline u64 nf_tables_alloc_handle(struct nft_table *table)
838{
839 return ++table->hgenerator;
840}
841
32537e91 842static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX];
9370761c 843
82603549
PNA
844static const struct nft_chain_type *
845__nft_chain_type_get(u8 family, enum nft_chain_types type)
846{
847 if (family >= NFPROTO_NUMPROTO ||
848 type >= NFT_CHAIN_T_MAX)
849 return NULL;
850
851 return chain_type[family][type];
852}
853
32537e91 854static const struct nft_chain_type *
1ea26cca 855__nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family)
9370761c 856{
82603549 857 const struct nft_chain_type *type;
9370761c
PNA
858 int i;
859
baae3e62 860 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
82603549
PNA
861 type = __nft_chain_type_get(family, i);
862 if (!type)
863 continue;
864 if (!nla_strcmp(nla, type->name))
865 return type;
9370761c 866 }
baae3e62 867 return NULL;
9370761c
PNA
868}
869
eb014de4
PNA
870struct nft_module_request {
871 struct list_head list;
872 char module[MODULE_NAME_LEN];
873 bool done;
874};
875
452238e8 876#ifdef CONFIG_MODULES
cefa31a9
FW
877__printf(2, 3) int nft_request_module(struct net *net, const char *fmt,
878 ...)
452238e8
FW
879{
880 char module_name[MODULE_NAME_LEN];
0854db2a 881 struct nftables_pernet *nft_net;
eb014de4 882 struct nft_module_request *req;
452238e8
FW
883 va_list args;
884 int ret;
885
452238e8
FW
886 va_start(args, fmt);
887 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
888 va_end(args);
9332d27d 889 if (ret >= MODULE_NAME_LEN)
eb014de4 890 return 0;
452238e8 891
d59d2f82 892 nft_net = nft_pernet(net);
0854db2a 893 list_for_each_entry(req, &nft_net->module_list, list) {
eb014de4
PNA
894 if (!strcmp(req->module, module_name)) {
895 if (req->done)
896 return 0;
ec7470b8 897
eb014de4
PNA
898 /* A request to load this module already exists. */
899 return -EAGAIN;
900 }
901 }
902
903 req = kmalloc(sizeof(*req), GFP_KERNEL);
904 if (!req)
905 return -ENOMEM;
906
907 req->done = false;
8556bceb 908 strscpy(req->module, module_name, MODULE_NAME_LEN);
0854db2a 909 list_add_tail(&req->list, &nft_net->module_list);
eb014de4
PNA
910
911 return -EAGAIN;
452238e8 912}
cefa31a9 913EXPORT_SYMBOL_GPL(nft_request_module);
452238e8
FW
914#endif
915
f102d66b
FW
916static void lockdep_nfnl_nft_mutex_not_held(void)
917{
918#ifdef CONFIG_PROVE_LOCKING
c0700dfa
FW
919 if (debug_locks)
920 WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES));
f102d66b
FW
921#endif
922}
923
32537e91 924static const struct nft_chain_type *
452238e8
FW
925nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla,
926 u8 family, bool autoload)
9370761c 927{
32537e91 928 const struct nft_chain_type *type;
9370761c 929
1ea26cca 930 type = __nf_tables_chain_type_lookup(nla, family);
93b0806f
PM
931 if (type != NULL)
932 return type;
f102d66b
FW
933
934 lockdep_nfnl_nft_mutex_not_held();
9370761c 935#ifdef CONFIG_MODULES
93b0806f 936 if (autoload) {
eb014de4
PNA
937 if (nft_request_module(net, "nft-chain-%u-%.*s", family,
938 nla_len(nla),
939 (const char *)nla_data(nla)) == -EAGAIN)
93b0806f 940 return ERR_PTR(-EAGAIN);
9370761c
PNA
941 }
942#endif
93b0806f 943 return ERR_PTR(-ENOENT);
9370761c
PNA
944}
945
802b8051
PNA
946static __be16 nft_base_seq(const struct net *net)
947{
d59d2f82 948 struct nftables_pernet *nft_net = nft_pernet(net);
0854db2a
FW
949
950 return htons(nft_net->base_seq & 0xffff);
802b8051
PNA
951}
952
96518518 953static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
1cae565e
PNA
954 [NFTA_TABLE_NAME] = { .type = NLA_STRING,
955 .len = NFT_TABLE_MAXNAMELEN - 1 },
9ddf6323 956 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
3ecbfd65 957 [NFTA_TABLE_HANDLE] = { .type = NLA_U64 },
7a81575b
JGG
958 [NFTA_TABLE_USERDATA] = { .type = NLA_BINARY,
959 .len = NFT_USERDATA_MAXLEN }
96518518
PM
960};
961
84d7fce6
PNA
962static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
963 u32 portid, u32 seq, int event, u32 flags,
964 int family, const struct nft_table *table)
96518518
PM
965{
966 struct nlmsghdr *nlh;
96518518 967
dedb67c4 968 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
19c28b13
PNA
969 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
970 NFNETLINK_V0, nft_base_seq(net));
971 if (!nlh)
96518518
PM
972 goto nla_put_failure;
973
9ddf6323 974 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
3ecbfd65
HS
975 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) ||
976 nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle),
977 NFTA_TABLE_PAD))
96518518 978 goto nla_put_failure;
28339b21
PNA
979
980 if (event == NFT_MSG_DELTABLE) {
981 nlmsg_end(skb, nlh);
982 return 0;
983 }
984
985 if (nla_put_be32(skb, NFTA_TABLE_FLAGS,
986 htonl(table->flags & NFT_TABLE_F_MASK)))
987 goto nla_put_failure;
988
6001a930
PNA
989 if (nft_table_has_owner(table) &&
990 nla_put_be32(skb, NFTA_TABLE_OWNER, htonl(table->nlpid)))
991 goto nla_put_failure;
96518518 992
7a81575b
JGG
993 if (table->udata) {
994 if (nla_put(skb, NFTA_TABLE_USERDATA, table->udlen, table->udata))
995 goto nla_put_failure;
996 }
997
053c095a
JB
998 nlmsg_end(skb, nlh);
999 return 0;
96518518
PM
1000
1001nla_put_failure:
1002 nlmsg_trim(skb, nlh);
1003 return -1;
1004}
1005
67cc570e
PNA
1006struct nftnl_skb_parms {
1007 bool report;
1008};
1009#define NFT_CB(skb) (*(struct nftnl_skb_parms*)&((skb)->cb))
1010
1011static void nft_notify_enqueue(struct sk_buff *skb, bool report,
1012 struct list_head *notify_list)
1013{
1014 NFT_CB(skb).report = report;
1015 list_add_tail(&skb->list, notify_list);
1016}
1017
25e94a99 1018static void nf_tables_table_notify(const struct nft_ctx *ctx, int event)
96518518 1019{
0854db2a 1020 struct nftables_pernet *nft_net;
96518518 1021 struct sk_buff *skb;
6fb721cf 1022 u16 flags = 0;
96518518
PM
1023 int err;
1024
128ad332
PNA
1025 if (!ctx->report &&
1026 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
25e94a99 1027 return;
96518518 1028
96518518
PM
1029 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1030 if (skb == NULL)
1031 goto err;
1032
6fb721cf
PNA
1033 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
1034 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
1035
84d7fce6 1036 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
6fb721cf 1037 event, flags, ctx->family, ctx->table);
96518518
PM
1038 if (err < 0) {
1039 kfree_skb(skb);
1040 goto err;
1041 }
1042
d59d2f82 1043 nft_net = nft_pernet(ctx->net);
0854db2a 1044 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
25e94a99 1045 return;
96518518 1046err:
25e94a99 1047 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
96518518
PM
1048}
1049
1050static int nf_tables_dump_tables(struct sk_buff *skb,
1051 struct netlink_callback *cb)
1052{
1053 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
0854db2a 1054 struct nftables_pernet *nft_net;
96518518
PM
1055 const struct nft_table *table;
1056 unsigned int idx = 0, s_idx = cb->args[0];
99633ab2 1057 struct net *net = sock_net(skb->sk);
96518518
PM
1058 int family = nfmsg->nfgen_family;
1059
e688a7f8 1060 rcu_read_lock();
d59d2f82 1061 nft_net = nft_pernet(net);
34002783 1062 cb->seq = READ_ONCE(nft_net->base_seq);
38e029f1 1063
0854db2a 1064 list_for_each_entry_rcu(table, &nft_net->tables, list) {
98319cb9 1065 if (family != NFPROTO_UNSPEC && family != table->family)
96518518
PM
1066 continue;
1067
36596dad
PNA
1068 if (idx < s_idx)
1069 goto cont;
1070 if (idx > s_idx)
1071 memset(&cb->args[1], 0,
1072 sizeof(cb->args) - sizeof(cb->args[0]));
1073 if (!nft_is_active(net, table))
1074 continue;
1075 if (nf_tables_fill_table_info(skb, net,
1076 NETLINK_CB(cb->skb).portid,
1077 cb->nlh->nlmsg_seq,
1078 NFT_MSG_NEWTABLE, NLM_F_MULTI,
98319cb9 1079 table->family, table) < 0)
36596dad
PNA
1080 goto done;
1081
1082 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
96518518 1083cont:
36596dad 1084 idx++;
96518518
PM
1085 }
1086done:
e688a7f8 1087 rcu_read_unlock();
96518518
PM
1088 cb->args[0] = idx;
1089 return skb->len;
1090}
1091
d9adf22a
FW
1092static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb,
1093 const struct nlmsghdr *nlh,
1094 struct netlink_dump_control *c)
1095{
1096 int err;
1097
1098 if (!try_module_get(THIS_MODULE))
1099 return -EINVAL;
1100
1101 rcu_read_unlock();
1102 err = netlink_dump_start(nlsk, skb, nlh, c);
1103 rcu_read_lock();
1104 module_put(THIS_MODULE);
1105
1106 return err;
1107}
1108
1109/* called with rcu_read_lock held */
797d4980
PNA
1110static int nf_tables_gettable(struct sk_buff *skb, const struct nfnl_info *info,
1111 const struct nlattr * const nla[])
96518518 1112{
797d4980
PNA
1113 struct netlink_ext_ack *extack = info->extack;
1114 u8 genmask = nft_genmask_cur(info->net);
ef4b65e5 1115 u8 family = info->nfmsg->nfgen_family;
96518518 1116 const struct nft_table *table;
797d4980 1117 struct net *net = info->net;
96518518 1118 struct sk_buff *skb2;
96518518
PM
1119 int err;
1120
797d4980 1121 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
96518518
PM
1122 struct netlink_dump_control c = {
1123 .dump = nf_tables_dump_tables,
d9adf22a 1124 .module = THIS_MODULE,
96518518 1125 };
d9adf22a 1126
797d4980 1127 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
96518518
PM
1128 }
1129
6001a930 1130 table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask, 0);
36dd1bcc
PNA
1131 if (IS_ERR(table)) {
1132 NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]);
96518518 1133 return PTR_ERR(table);
36dd1bcc 1134 }
96518518 1135
d9adf22a 1136 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
96518518
PM
1137 if (!skb2)
1138 return -ENOMEM;
1139
84d7fce6 1140 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
797d4980
PNA
1141 info->nlh->nlmsg_seq, NFT_MSG_NEWTABLE,
1142 0, family, table);
96518518 1143 if (err < 0)
ee921183 1144 goto err_fill_table_info;
96518518 1145
ee921183 1146 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
96518518 1147
ee921183 1148err_fill_table_info:
96518518
PM
1149 kfree_skb(skb2);
1150 return err;
1151}
1152
c9c17211 1153static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt)
10435c11
F
1154{
1155 struct nft_chain *chain;
1156 u32 i = 0;
1157
1158 list_for_each_entry(chain, &table->chains, list) {
1159 if (!nft_is_active_next(net, chain))
1160 continue;
f323d954 1161 if (!nft_is_base_chain(chain))
10435c11
F
1162 continue;
1163
1164 if (cnt && i++ == cnt)
1165 break;
1166
1e9451cb 1167 nf_tables_unregister_hook(net, table, chain);
10435c11
F
1168 }
1169}
1170
c9c17211 1171static int nf_tables_table_enable(struct net *net, struct nft_table *table)
9ddf6323
PNA
1172{
1173 struct nft_chain *chain;
1174 int err, i = 0;
1175
1176 list_for_each_entry(chain, &table->chains, list) {
664b0f8c
PNA
1177 if (!nft_is_active_next(net, chain))
1178 continue;
f323d954 1179 if (!nft_is_base_chain(chain))
d2012975
PNA
1180 continue;
1181
1e9451cb 1182 err = nf_tables_register_hook(net, table, chain);
9ddf6323 1183 if (err < 0)
d54725cd 1184 goto err_register_hooks;
9ddf6323
PNA
1185
1186 i++;
1187 }
1188 return 0;
d54725cd
PNA
1189
1190err_register_hooks:
10435c11 1191 if (i)
c9c17211 1192 nft_table_disable(net, table, i);
9ddf6323
PNA
1193 return err;
1194}
1195
c9c17211 1196static void nf_tables_table_disable(struct net *net, struct nft_table *table)
9ddf6323 1197{
179d9ba5 1198 table->flags &= ~NFT_TABLE_F_DORMANT;
c9c17211 1199 nft_table_disable(net, table, 0);
179d9ba5 1200 table->flags |= NFT_TABLE_F_DORMANT;
9ddf6323
PNA
1201}
1202
179d9ba5
PNA
1203#define __NFT_TABLE_F_INTERNAL (NFT_TABLE_F_MASK + 1)
1204#define __NFT_TABLE_F_WAS_DORMANT (__NFT_TABLE_F_INTERNAL << 0)
1205#define __NFT_TABLE_F_WAS_AWAKEN (__NFT_TABLE_F_INTERNAL << 1)
31bf508b 1206#define __NFT_TABLE_F_WAS_ORPHAN (__NFT_TABLE_F_INTERNAL << 2)
179d9ba5 1207#define __NFT_TABLE_F_UPDATE (__NFT_TABLE_F_WAS_DORMANT | \
31bf508b
PS
1208 __NFT_TABLE_F_WAS_AWAKEN | \
1209 __NFT_TABLE_F_WAS_ORPHAN)
0ce7cf41 1210
1e1fb6f0
PNA
1211static bool nft_table_pending_update(const struct nft_ctx *ctx)
1212{
1213 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
1214 struct nft_trans *trans;
1215
1216 if (ctx->table->flags & __NFT_TABLE_F_UPDATE)
1217 return true;
1218
1219 list_for_each_entry(trans, &nft_net->commit_list, list) {
1bc83a01
PNA
1220 if (trans->ctx.table == ctx->table &&
1221 ((trans->msg_type == NFT_MSG_NEWCHAIN &&
1222 nft_trans_chain_update(trans)) ||
1223 (trans->msg_type == NFT_MSG_DELCHAIN &&
1224 nft_is_base_chain(trans->ctx.chain))))
1e1fb6f0
PNA
1225 return true;
1226 }
1227
1228 return false;
1229}
1230
e1aaca93 1231static int nf_tables_updtable(struct nft_ctx *ctx)
9ddf6323 1232{
55dd6f93 1233 struct nft_trans *trans;
e1aaca93 1234 u32 flags;
179d9ba5 1235 int ret;
9ddf6323 1236
e1aaca93
PNA
1237 if (!ctx->nla[NFTA_TABLE_FLAGS])
1238 return 0;
9ddf6323 1239
e1aaca93 1240 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
6001a930
PNA
1241 if (flags & ~NFT_TABLE_F_MASK)
1242 return -EOPNOTSUPP;
e1aaca93 1243
4a0e7f2d 1244 if (flags == (ctx->table->flags & NFT_TABLE_F_MASK))
63283dd2
PNA
1245 return 0;
1246
9cc0001a
PNA
1247 if ((nft_table_has_owner(ctx->table) &&
1248 !(flags & NFT_TABLE_F_OWNER)) ||
31bf508b
PS
1249 (flags & NFT_TABLE_F_OWNER &&
1250 !nft_table_is_orphan(ctx->table)))
9cc0001a
PNA
1251 return -EOPNOTSUPP;
1252
da5141bb
PS
1253 if ((flags ^ ctx->table->flags) & NFT_TABLE_F_PERSIST)
1254 return -EOPNOTSUPP;
1255
c9bd2651 1256 /* No dormant off/on/off/on games in single transaction */
1e1fb6f0 1257 if (nft_table_pending_update(ctx))
c9bd2651
FW
1258 return -EINVAL;
1259
55dd6f93
PNA
1260 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
1261 sizeof(struct nft_trans_table));
1262 if (trans == NULL)
1263 return -ENOMEM;
9ddf6323 1264
e1aaca93
PNA
1265 if ((flags & NFT_TABLE_F_DORMANT) &&
1266 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
179d9ba5
PNA
1267 ctx->table->flags |= NFT_TABLE_F_DORMANT;
1268 if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE))
1269 ctx->table->flags |= __NFT_TABLE_F_WAS_AWAKEN;
e1aaca93
PNA
1270 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
1271 ctx->table->flags & NFT_TABLE_F_DORMANT) {
179d9ba5
PNA
1272 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
1273 if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) {
1274 ret = nf_tables_table_enable(ctx->net, ctx->table);
1275 if (ret < 0)
1276 goto err_register_hooks;
1277
1278 ctx->table->flags |= __NFT_TABLE_F_WAS_DORMANT;
1279 }
9ddf6323
PNA
1280 }
1281
31bf508b
PS
1282 if ((flags & NFT_TABLE_F_OWNER) &&
1283 !nft_table_has_owner(ctx->table)) {
1284 ctx->table->nlpid = ctx->portid;
1285 ctx->table->flags |= NFT_TABLE_F_OWNER |
1286 __NFT_TABLE_F_WAS_ORPHAN;
1287 }
1288
55dd6f93 1289 nft_trans_table_update(trans) = true;
0854db2a 1290 nft_trans_commit_list_add_tail(ctx->net, trans);
179d9ba5 1291
55dd6f93 1292 return 0;
179d9ba5
PNA
1293
1294err_register_hooks:
bccebf64 1295 ctx->table->flags |= NFT_TABLE_F_DORMANT;
55dd6f93 1296 nft_trans_destroy(trans);
9ddf6323
PNA
1297 return ret;
1298}
1299
1b2470e5
FW
1300static u32 nft_chain_hash(const void *data, u32 len, u32 seed)
1301{
1302 const char *name = data;
1303
1304 return jhash(name, strlen(name), seed);
1305}
1306
1307static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed)
1308{
1309 const struct nft_chain *chain = data;
1310
1311 return nft_chain_hash(chain->name, 0, seed);
1312}
1313
1314static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg,
1315 const void *ptr)
1316{
1317 const struct nft_chain *chain = ptr;
1318 const char *name = arg->key;
1319
1320 return strcmp(chain->name, name);
1321}
1322
4d44175a
FW
1323static u32 nft_objname_hash(const void *data, u32 len, u32 seed)
1324{
1325 const struct nft_object_hash_key *k = data;
1326
1327 seed ^= hash_ptr(k->table, 32);
1328
1329 return jhash(k->name, strlen(k->name), seed);
1330}
1331
1332static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed)
1333{
1334 const struct nft_object *obj = data;
1335
1336 return nft_objname_hash(&obj->key, 0, seed);
1337}
1338
1339static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg,
1340 const void *ptr)
1341{
1342 const struct nft_object_hash_key *k = arg->key;
1343 const struct nft_object *obj = ptr;
1344
1345 if (obj->key.table != k->table)
1346 return -1;
1347
1348 return strcmp(obj->key.name, k->name);
1349}
1350
f1082dd3
PS
1351static bool nft_supported_family(u8 family)
1352{
1353 return false
1354#ifdef CONFIG_NF_TABLES_INET
1355 || family == NFPROTO_INET
1356#endif
1357#ifdef CONFIG_NF_TABLES_IPV4
1358 || family == NFPROTO_IPV4
1359#endif
1360#ifdef CONFIG_NF_TABLES_ARP
1361 || family == NFPROTO_ARP
1362#endif
1363#ifdef CONFIG_NF_TABLES_NETDEV
1364 || family == NFPROTO_NETDEV
1365#endif
1366#if IS_ENABLED(CONFIG_NF_TABLES_BRIDGE)
1367 || family == NFPROTO_BRIDGE
1368#endif
1369#ifdef CONFIG_NF_TABLES_IPV6
1370 || family == NFPROTO_IPV6
1371#endif
1372 ;
1373}
1374
7dab8ee3
PNA
1375static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info,
1376 const struct nlattr * const nla[])
96518518 1377{
7dab8ee3 1378 struct nftables_pernet *nft_net = nft_pernet(info->net);
7dab8ee3
PNA
1379 struct netlink_ext_ack *extack = info->extack;
1380 u8 genmask = nft_genmask_next(info->net);
ef4b65e5 1381 u8 family = info->nfmsg->nfgen_family;
7dab8ee3 1382 struct net *net = info->net;
36dd1bcc
PNA
1383 const struct nlattr *attr;
1384 struct nft_table *table;
e1aaca93 1385 struct nft_ctx ctx;
7a81575b 1386 u32 flags = 0;
55dd6f93 1387 int err;
96518518 1388
f1082dd3
PS
1389 if (!nft_supported_family(family))
1390 return -EOPNOTSUPP;
1391
0854db2a 1392 lockdep_assert_held(&nft_net->commit_mutex);
36dd1bcc 1393 attr = nla[NFTA_TABLE_NAME];
6001a930
PNA
1394 table = nft_table_lookup(net, attr, family, genmask,
1395 NETLINK_CB(skb).portid);
96518518
PM
1396 if (IS_ERR(table)) {
1397 if (PTR_ERR(table) != -ENOENT)
1398 return PTR_ERR(table);
1a28ad74 1399 } else {
7dab8ee3 1400 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
36dd1bcc 1401 NL_SET_BAD_ATTR(extack, attr);
96518518 1402 return -EEXIST;
36dd1bcc 1403 }
7dab8ee3 1404 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
96518518 1405 return -EOPNOTSUPP;
e1aaca93 1406
7dab8ee3
PNA
1407 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
1408
e1aaca93 1409 return nf_tables_updtable(&ctx);
96518518
PM
1410 }
1411
c5c1f975
PM
1412 if (nla[NFTA_TABLE_FLAGS]) {
1413 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
6001a930
PNA
1414 if (flags & ~NFT_TABLE_F_MASK)
1415 return -EOPNOTSUPP;
c5c1f975
PM
1416 }
1417
ffdb210e 1418 err = -ENOMEM;
33758c89 1419 table = kzalloc(sizeof(*table), GFP_KERNEL_ACCOUNT);
ffdb210e 1420 if (table == NULL)
98319cb9 1421 goto err_kzalloc;
96518518 1422
4b80ced9 1423 table->validate_state = nft_net->validate_state;
33758c89 1424 table->name = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
e46abbcc 1425 if (table->name == NULL)
98319cb9 1426 goto err_strdup;
e46abbcc 1427
7a81575b 1428 if (nla[NFTA_TABLE_USERDATA]) {
33758c89 1429 table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL_ACCOUNT);
7a81575b
JGG
1430 if (table->udata == NULL)
1431 goto err_table_udata;
1432
85db827a 1433 table->udlen = nla_len(nla[NFTA_TABLE_USERDATA]);
7a81575b
JGG
1434 }
1435
1b2470e5
FW
1436 err = rhltable_init(&table->chains_ht, &nft_chain_ht_params);
1437 if (err)
1438 goto err_chain_ht;
1439
96518518 1440 INIT_LIST_HEAD(&table->chains);
20a69341 1441 INIT_LIST_HEAD(&table->sets);
e5009240 1442 INIT_LIST_HEAD(&table->objects);
3b49e2e9 1443 INIT_LIST_HEAD(&table->flowtables);
98319cb9 1444 table->family = family;
c5c1f975 1445 table->flags = flags;
ab482c6b 1446 table->handle = ++nft_net->table_handle;
6001a930
PNA
1447 if (table->flags & NFT_TABLE_F_OWNER)
1448 table->nlpid = NETLINK_CB(skb).portid;
9ddf6323 1449
7dab8ee3 1450 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
55dd6f93 1451 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
ffdb210e 1452 if (err < 0)
98319cb9 1453 goto err_trans;
ffdb210e 1454
0854db2a 1455 list_add_tail_rcu(&table->list, &nft_net->tables);
96518518 1456 return 0;
98319cb9 1457err_trans:
1b2470e5
FW
1458 rhltable_destroy(&table->chains_ht);
1459err_chain_ht:
7a81575b
JGG
1460 kfree(table->udata);
1461err_table_udata:
e46abbcc 1462 kfree(table->name);
98319cb9 1463err_strdup:
ffdb210e 1464 kfree(table);
98319cb9 1465err_kzalloc:
ffdb210e 1466 return err;
96518518
PM
1467}
1468
b9ac12ef
AB
1469static int nft_flush_table(struct nft_ctx *ctx)
1470{
3b49e2e9 1471 struct nft_flowtable *flowtable, *nft;
b9ac12ef 1472 struct nft_chain *chain, *nc;
e5009240 1473 struct nft_object *obj, *ne;
b9ac12ef 1474 struct nft_set *set, *ns;
3b49e2e9 1475 int err;
b9ac12ef 1476
a2f18db0 1477 list_for_each_entry(chain, &ctx->table->chains, list) {
664b0f8c
PNA
1478 if (!nft_is_active_next(ctx->net, chain))
1479 continue;
1480
f15f29fd 1481 if (nft_chain_binding(chain))
d0e2c7de
PNA
1482 continue;
1483
b9ac12ef
AB
1484 ctx->chain = chain;
1485
1486 err = nft_delrule_by_chain(ctx);
1487 if (err < 0)
1488 goto out;
b9ac12ef
AB
1489 }
1490
1491 list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
37a9cc52
PNA
1492 if (!nft_is_active_next(ctx->net, set))
1493 continue;
1494
23a3bfd4 1495 if (nft_set_is_anonymous(set))
b9ac12ef
AB
1496 continue;
1497
1498 err = nft_delset(ctx, set);
1499 if (err < 0)
1500 goto out;
1501 }
1502
3b49e2e9 1503 list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) {
335178d5
FW
1504 if (!nft_is_active_next(ctx->net, flowtable))
1505 continue;
1506
3b49e2e9
PNA
1507 err = nft_delflowtable(ctx, flowtable);
1508 if (err < 0)
1509 goto out;
1510 }
1511
e5009240 1512 list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) {
335178d5
FW
1513 if (!nft_is_active_next(ctx->net, obj))
1514 continue;
1515
e5009240
PNA
1516 err = nft_delobj(ctx, obj);
1517 if (err < 0)
1518 goto out;
1519 }
1520
a2f18db0 1521 list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
664b0f8c
PNA
1522 if (!nft_is_active_next(ctx->net, chain))
1523 continue;
1524
f15f29fd 1525 if (nft_chain_binding(chain))
d0e2c7de
PNA
1526 continue;
1527
a2f18db0
PNA
1528 ctx->chain = chain;
1529
1530 err = nft_delchain(ctx);
1531 if (err < 0)
1532 goto out;
1533 }
1534
b9ac12ef
AB
1535 err = nft_deltable(ctx);
1536out:
1537 return err;
1538}
1539
1540static int nft_flush(struct nft_ctx *ctx, int family)
1541{
d59d2f82 1542 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
b9ac12ef 1543 const struct nlattr * const *nla = ctx->nla;
d59d2f82 1544 struct nft_table *table, *nt;
b9ac12ef
AB
1545 int err = 0;
1546
0854db2a 1547 list_for_each_entry_safe(table, nt, &nft_net->tables, list) {
98319cb9 1548 if (family != AF_UNSPEC && table->family != family)
b9ac12ef
AB
1549 continue;
1550
98319cb9 1551 ctx->family = table->family;
f2a6d766 1552
36596dad
PNA
1553 if (!nft_is_active_next(ctx->net, table))
1554 continue;
b9ac12ef 1555
6001a930
PNA
1556 if (nft_table_has_owner(table) && table->nlpid != ctx->portid)
1557 continue;
1558
36596dad
PNA
1559 if (nla[NFTA_TABLE_NAME] &&
1560 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
1561 continue;
b9ac12ef 1562
36596dad
PNA
1563 ctx->table = table;
1564
1565 err = nft_flush_table(ctx);
1566 if (err < 0)
1567 goto out;
b9ac12ef
AB
1568 }
1569out:
1570 return err;
1571}
1572
7dab8ee3
PNA
1573static int nf_tables_deltable(struct sk_buff *skb, const struct nfnl_info *info,
1574 const struct nlattr * const nla[])
96518518 1575{
7dab8ee3
PNA
1576 struct netlink_ext_ack *extack = info->extack;
1577 u8 genmask = nft_genmask_next(info->net);
ef4b65e5 1578 u8 family = info->nfmsg->nfgen_family;
7dab8ee3 1579 struct net *net = info->net;
36dd1bcc
PNA
1580 const struct nlattr *attr;
1581 struct nft_table *table;
55dd6f93 1582 struct nft_ctx ctx;
96518518 1583
7dab8ee3 1584 nft_ctx_init(&ctx, net, skb, info->nlh, 0, NULL, NULL, nla);
3ecbfd65
HS
1585 if (family == AF_UNSPEC ||
1586 (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE]))
b9ac12ef
AB
1587 return nft_flush(&ctx, family);
1588
36dd1bcc
PNA
1589 if (nla[NFTA_TABLE_HANDLE]) {
1590 attr = nla[NFTA_TABLE_HANDLE];
f6e1532a 1591 table = nft_table_lookup_byhandle(net, attr, family, genmask,
e31f072f 1592 NETLINK_CB(skb).portid);
36dd1bcc
PNA
1593 } else {
1594 attr = nla[NFTA_TABLE_NAME];
6001a930
PNA
1595 table = nft_table_lookup(net, attr, family, genmask,
1596 NETLINK_CB(skb).portid);
36dd1bcc 1597 }
3ecbfd65 1598
36dd1bcc 1599 if (IS_ERR(table)) {
f80a612d
FFM
1600 if (PTR_ERR(table) == -ENOENT &&
1601 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYTABLE)
1602 return 0;
1603
36dd1bcc 1604 NL_SET_BAD_ATTR(extack, attr);
96518518 1605 return PTR_ERR(table);
36dd1bcc 1606 }
96518518 1607
7dab8ee3 1608 if (info->nlh->nlmsg_flags & NLM_F_NONREC &&
a8278400
PNA
1609 table->use > 0)
1610 return -EBUSY;
1611
98319cb9 1612 ctx.family = family;
b9ac12ef 1613 ctx.table = table;
55dd6f93 1614
b9ac12ef 1615 return nft_flush_table(&ctx);
96518518
PM
1616}
1617
55dd6f93
PNA
1618static void nf_tables_table_destroy(struct nft_ctx *ctx)
1619{
fa5950e4
FW
1620 if (WARN_ON(ctx->table->use > 0))
1621 return;
4fefee57 1622
1b2470e5 1623 rhltable_destroy(&ctx->table->chains_ht);
e46abbcc 1624 kfree(ctx->table->name);
bc7a7082 1625 kfree(ctx->table->udata);
55dd6f93 1626 kfree(ctx->table);
55dd6f93
PNA
1627}
1628
cc07eeb0 1629void nft_register_chain_type(const struct nft_chain_type *ctype)
96518518 1630{
96518518 1631 nfnl_lock(NFNL_SUBSYS_NFTABLES);
82603549 1632 if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) {
cc07eeb0
PNA
1633 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1634 return;
96518518 1635 }
9370761c 1636 chain_type[ctype->family][ctype->type] = ctype;
96518518 1637 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
96518518 1638}
9370761c 1639EXPORT_SYMBOL_GPL(nft_register_chain_type);
96518518 1640
32537e91 1641void nft_unregister_chain_type(const struct nft_chain_type *ctype)
96518518 1642{
96518518 1643 nfnl_lock(NFNL_SUBSYS_NFTABLES);
9370761c 1644 chain_type[ctype->family][ctype->type] = NULL;
96518518
PM
1645 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1646}
9370761c 1647EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
96518518
PM
1648
1649/*
1650 * Chains
1651 */
1652
1653static struct nft_chain *
cac20fcd 1654nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask)
96518518
PM
1655{
1656 struct nft_chain *chain;
1657
1658 list_for_each_entry(chain, &table->chains, list) {
664b0f8c
PNA
1659 if (chain->handle == handle &&
1660 nft_active_genmask(chain, genmask))
96518518
PM
1661 return chain;
1662 }
1663
1664 return ERR_PTR(-ENOENT);
1665}
1666
4d44175a 1667static bool lockdep_commit_lock_is_held(const struct net *net)
f102d66b
FW
1668{
1669#ifdef CONFIG_PROVE_LOCKING
d59d2f82 1670 struct nftables_pernet *nft_net = nft_pernet(net);
0854db2a
FW
1671
1672 return lockdep_is_held(&nft_net->commit_mutex);
f102d66b
FW
1673#else
1674 return true;
1675#endif
1676}
1677
1678static struct nft_chain *nft_chain_lookup(struct net *net,
1679 struct nft_table *table,
cac20fcd 1680 const struct nlattr *nla, u8 genmask)
96518518 1681{
1b2470e5
FW
1682 char search[NFT_CHAIN_MAXNAMELEN + 1];
1683 struct rhlist_head *tmp, *list;
96518518
PM
1684 struct nft_chain *chain;
1685
1686 if (nla == NULL)
1687 return ERR_PTR(-EINVAL);
1688
872f6903 1689 nla_strscpy(search, nla, sizeof(search));
96518518 1690
1b2470e5 1691 WARN_ON(!rcu_read_lock_held() &&
f102d66b 1692 !lockdep_commit_lock_is_held(net));
1b2470e5
FW
1693
1694 chain = ERR_PTR(-ENOENT);
1695 rcu_read_lock();
1696 list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params);
1697 if (!list)
1698 goto out_unlock;
1699
1700 rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
1701 if (nft_active_genmask(chain, genmask))
1702 goto out_unlock;
1703 }
1704 chain = ERR_PTR(-ENOENT);
1705out_unlock:
1706 rcu_read_unlock();
1707 return chain;
96518518
PM
1708}
1709
1710static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
b2fbd044
LZ
1711 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING,
1712 .len = NFT_TABLE_MAXNAMELEN - 1 },
96518518
PM
1713 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
1714 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
1715 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1716 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
0ca743a5 1717 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
9332d27d
FW
1718 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING,
1719 .len = NFT_MODULE_AUTOLOAD_LIMIT },
0ca743a5 1720 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
c9626a2c 1721 [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 },
74cccc3d 1722 [NFTA_CHAIN_ID] = { .type = NLA_U32 },
002f2176
JGG
1723 [NFTA_CHAIN_USERDATA] = { .type = NLA_BINARY,
1724 .len = NFT_USERDATA_MAXLEN },
96518518
PM
1725};
1726
1727static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
1728 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
1729 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
2cbce139
PNA
1730 [NFTA_HOOK_DEV] = { .type = NLA_STRING,
1731 .len = IFNAMSIZ - 1 },
96518518
PM
1732};
1733
0ca743a5
PNA
1734static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
1735{
1736 struct nft_stats *cpu_stats, total;
1737 struct nlattr *nest;
ce355e20
ED
1738 unsigned int seq;
1739 u64 pkts, bytes;
0ca743a5
PNA
1740 int cpu;
1741
edbd82c5
FW
1742 if (!stats)
1743 return 0;
1744
0ca743a5
PNA
1745 memset(&total, 0, sizeof(total));
1746 for_each_possible_cpu(cpu) {
1747 cpu_stats = per_cpu_ptr(stats, cpu);
ce355e20 1748 do {
d120d1a6 1749 seq = u64_stats_fetch_begin(&cpu_stats->syncp);
ce355e20
ED
1750 pkts = cpu_stats->pkts;
1751 bytes = cpu_stats->bytes;
d120d1a6 1752 } while (u64_stats_fetch_retry(&cpu_stats->syncp, seq));
ce355e20
ED
1753 total.pkts += pkts;
1754 total.bytes += bytes;
0ca743a5 1755 }
ae0be8de 1756 nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS);
0ca743a5
PNA
1757 if (nest == NULL)
1758 goto nla_put_failure;
1759
b46f6ded
ND
1760 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts),
1761 NFTA_COUNTER_PAD) ||
1762 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
1763 NFTA_COUNTER_PAD))
0ca743a5
PNA
1764 goto nla_put_failure;
1765
1766 nla_nest_end(skb, nest);
1767 return 0;
1768
1769nla_put_failure:
1770 return -ENOSPC;
1771}
1772
d54725cd 1773static int nft_dump_basechain_hook(struct sk_buff *skb, int family,
b9703ed4
PNA
1774 const struct nft_base_chain *basechain,
1775 const struct list_head *hook_list)
d54725cd
PNA
1776{
1777 const struct nf_hook_ops *ops = &basechain->ops;
1778 struct nft_hook *hook, *first = NULL;
1779 struct nlattr *nest, *nest_devs;
1780 int n = 0;
1781
1782 nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK);
1783 if (nest == NULL)
1784 goto nla_put_failure;
1785 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
1786 goto nla_put_failure;
1787 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
1788 goto nla_put_failure;
1789
d3519cb8 1790 if (nft_base_chain_netdev(family, ops->hooknum)) {
d54725cd 1791 nest_devs = nla_nest_start_noflag(skb, NFTA_HOOK_DEVS);
bd058763
GI
1792 if (!nest_devs)
1793 goto nla_put_failure;
b9703ed4
PNA
1794
1795 if (!hook_list)
1796 hook_list = &basechain->hook_list;
1797
1798 list_for_each_entry(hook, hook_list, list) {
d54725cd
PNA
1799 if (!first)
1800 first = hook;
1801
1802 if (nla_put_string(skb, NFTA_DEVICE_NAME,
1803 hook->ops.dev->name))
1804 goto nla_put_failure;
1805 n++;
1806 }
1807 nla_nest_end(skb, nest_devs);
1808
1809 if (n == 1 &&
1810 nla_put_string(skb, NFTA_HOOK_DEV, first->ops.dev->name))
1811 goto nla_put_failure;
1812 }
1813 nla_nest_end(skb, nest);
1814
1815 return 0;
1816nla_put_failure:
1817 return -1;
1818}
1819
84d7fce6
PNA
1820static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
1821 u32 portid, u32 seq, int event, u32 flags,
1822 int family, const struct nft_table *table,
b9703ed4
PNA
1823 const struct nft_chain *chain,
1824 const struct list_head *hook_list)
96518518
PM
1825{
1826 struct nlmsghdr *nlh;
96518518 1827
dedb67c4 1828 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
19c28b13
PNA
1829 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
1830 NFNETLINK_V0, nft_base_seq(net));
1831 if (!nlh)
96518518
PM
1832 goto nla_put_failure;
1833
28339b21
PNA
1834 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name) ||
1835 nla_put_string(skb, NFTA_CHAIN_NAME, chain->name) ||
1836 nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle),
b46f6ded 1837 NFTA_CHAIN_PAD))
96518518 1838 goto nla_put_failure;
28339b21 1839
7d937b10 1840 if (event == NFT_MSG_DELCHAIN && !hook_list) {
28339b21
PNA
1841 nlmsg_end(skb, nlh);
1842 return 0;
1843 }
96518518 1844
f323d954 1845 if (nft_is_base_chain(chain)) {
0ca743a5 1846 const struct nft_base_chain *basechain = nft_base_chain(chain);
edbd82c5 1847 struct nft_stats __percpu *stats;
0ca743a5 1848
b9703ed4 1849 if (nft_dump_basechain_hook(skb, family, basechain, hook_list))
2cbce139 1850 goto nla_put_failure;
9370761c 1851
0ca743a5
PNA
1852 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
1853 htonl(basechain->policy)))
1854 goto nla_put_failure;
1855
baae3e62
PM
1856 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
1857 goto nla_put_failure;
0ca743a5 1858
edbd82c5
FW
1859 stats = rcu_dereference_check(basechain->stats,
1860 lockdep_commit_lock_is_held(net));
1861 if (nft_dump_stats(skb, stats))
0ca743a5 1862 goto nla_put_failure;
96518518
PM
1863 }
1864
d0e2c7de
PNA
1865 if (chain->flags &&
1866 nla_put_be32(skb, NFTA_CHAIN_FLAGS, htonl(chain->flags)))
1867 goto nla_put_failure;
1868
0ca743a5
PNA
1869 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
1870 goto nla_put_failure;
1871
002f2176
JGG
1872 if (chain->udata &&
1873 nla_put(skb, NFTA_CHAIN_USERDATA, chain->udlen, chain->udata))
1874 goto nla_put_failure;
1875
053c095a
JB
1876 nlmsg_end(skb, nlh);
1877 return 0;
96518518
PM
1878
1879nla_put_failure:
1880 nlmsg_trim(skb, nlh);
1881 return -1;
1882}
1883
b9703ed4
PNA
1884static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event,
1885 const struct list_head *hook_list)
96518518 1886{
0854db2a 1887 struct nftables_pernet *nft_net;
96518518 1888 struct sk_buff *skb;
6fb721cf 1889 u16 flags = 0;
96518518
PM
1890 int err;
1891
128ad332
PNA
1892 if (!ctx->report &&
1893 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
25e94a99 1894 return;
96518518 1895
96518518
PM
1896 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1897 if (skb == NULL)
1898 goto err;
1899
6fb721cf
PNA
1900 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
1901 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
1902
84d7fce6 1903 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
6fb721cf 1904 event, flags, ctx->family, ctx->table,
b9703ed4 1905 ctx->chain, hook_list);
96518518
PM
1906 if (err < 0) {
1907 kfree_skb(skb);
1908 goto err;
1909 }
1910
d59d2f82 1911 nft_net = nft_pernet(ctx->net);
0854db2a 1912 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
25e94a99 1913 return;
96518518 1914err:
25e94a99 1915 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
96518518
PM
1916}
1917
1918static int nf_tables_dump_chains(struct sk_buff *skb,
1919 struct netlink_callback *cb)
1920{
1921 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
96518518 1922 unsigned int idx = 0, s_idx = cb->args[0];
99633ab2 1923 struct net *net = sock_net(skb->sk);
96518518 1924 int family = nfmsg->nfgen_family;
0854db2a 1925 struct nftables_pernet *nft_net;
d59d2f82
PNA
1926 const struct nft_table *table;
1927 const struct nft_chain *chain;
96518518 1928
e688a7f8 1929 rcu_read_lock();
d59d2f82 1930 nft_net = nft_pernet(net);
34002783 1931 cb->seq = READ_ONCE(nft_net->base_seq);
38e029f1 1932
0854db2a 1933 list_for_each_entry_rcu(table, &nft_net->tables, list) {
98319cb9 1934 if (family != NFPROTO_UNSPEC && family != table->family)
96518518
PM
1935 continue;
1936
36596dad
PNA
1937 list_for_each_entry_rcu(chain, &table->chains, list) {
1938 if (idx < s_idx)
1939 goto cont;
1940 if (idx > s_idx)
1941 memset(&cb->args[1], 0,
1942 sizeof(cb->args) - sizeof(cb->args[0]));
1943 if (!nft_is_active(net, chain))
1944 continue;
1945 if (nf_tables_fill_chain_info(skb, net,
1946 NETLINK_CB(cb->skb).portid,
1947 cb->nlh->nlmsg_seq,
1948 NFT_MSG_NEWCHAIN,
1949 NLM_F_MULTI,
98319cb9 1950 table->family, table,
b9703ed4 1951 chain, NULL) < 0)
36596dad 1952 goto done;
38e029f1 1953
36596dad 1954 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
96518518 1955cont:
36596dad 1956 idx++;
96518518
PM
1957 }
1958 }
1959done:
e688a7f8 1960 rcu_read_unlock();
96518518
PM
1961 cb->args[0] = idx;
1962 return skb->len;
1963}
1964
d9adf22a 1965/* called with rcu_read_lock held */
797d4980
PNA
1966static int nf_tables_getchain(struct sk_buff *skb, const struct nfnl_info *info,
1967 const struct nlattr * const nla[])
96518518 1968{
797d4980
PNA
1969 struct netlink_ext_ack *extack = info->extack;
1970 u8 genmask = nft_genmask_cur(info->net);
ef4b65e5 1971 u8 family = info->nfmsg->nfgen_family;
96518518 1972 const struct nft_chain *chain;
797d4980 1973 struct net *net = info->net;
1b2470e5 1974 struct nft_table *table;
96518518 1975 struct sk_buff *skb2;
96518518
PM
1976 int err;
1977
797d4980 1978 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
96518518
PM
1979 struct netlink_dump_control c = {
1980 .dump = nf_tables_dump_chains,
d9adf22a 1981 .module = THIS_MODULE,
96518518 1982 };
d9adf22a 1983
797d4980 1984 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
96518518
PM
1985 }
1986
6001a930 1987 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, 0);
36dd1bcc
PNA
1988 if (IS_ERR(table)) {
1989 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
96518518 1990 return PTR_ERR(table);
36dd1bcc 1991 }
96518518 1992
f102d66b 1993 chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask);
36dd1bcc
PNA
1994 if (IS_ERR(chain)) {
1995 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
96518518 1996 return PTR_ERR(chain);
36dd1bcc 1997 }
96518518 1998
d9adf22a 1999 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
96518518
PM
2000 if (!skb2)
2001 return -ENOMEM;
2002
84d7fce6 2003 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
797d4980 2004 info->nlh->nlmsg_seq, NFT_MSG_NEWCHAIN,
b9703ed4 2005 0, family, table, chain, NULL);
96518518 2006 if (err < 0)
ee921183 2007 goto err_fill_chain_info;
96518518 2008
ee921183 2009 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
96518518 2010
ee921183 2011err_fill_chain_info:
96518518
PM
2012 kfree_skb(skb2);
2013 return err;
2014}
2015
0ca743a5
PNA
2016static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
2017 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
2018 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
2019};
2020
ff3cd7b3 2021static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
0ca743a5
PNA
2022{
2023 struct nlattr *tb[NFTA_COUNTER_MAX+1];
2024 struct nft_stats __percpu *newstats;
2025 struct nft_stats *stats;
2026 int err;
2027
8cb08174
JB
2028 err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr,
2029 nft_counter_policy, NULL);
0ca743a5 2030 if (err < 0)
ff3cd7b3 2031 return ERR_PTR(err);
0ca743a5
PNA
2032
2033 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
ff3cd7b3 2034 return ERR_PTR(-EINVAL);
0ca743a5 2035
ce355e20 2036 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
0ca743a5 2037 if (newstats == NULL)
ff3cd7b3 2038 return ERR_PTR(-ENOMEM);
0ca743a5
PNA
2039
2040 /* Restore old counters on this cpu, no problem. Per-cpu statistics
2041 * are not exposed to userspace.
2042 */
e8781f70 2043 preempt_disable();
0ca743a5
PNA
2044 stats = this_cpu_ptr(newstats);
2045 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
2046 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
e8781f70 2047 preempt_enable();
0ca743a5 2048
ff3cd7b3
PNA
2049 return newstats;
2050}
2051
53315ac6 2052static void nft_chain_stats_replace(struct nft_trans *trans)
ff3cd7b3 2053{
53315ac6 2054 struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain);
0befd061 2055
53315ac6 2056 if (!nft_trans_chain_stats(trans))
b88825de
PNA
2057 return;
2058
b685b534
PM
2059 nft_trans_chain_stats(trans) =
2060 rcu_replace_pointer(chain->stats, nft_trans_chain_stats(trans),
2061 lockdep_commit_lock_is_held(trans->ctx.net));
53315ac6
FW
2062
2063 if (!nft_trans_chain_stats(trans))
bbb8c61f 2064 static_branch_inc(&nft_counters_enabled);
0ca743a5
PNA
2065}
2066
0cbc06b3
FW
2067static void nf_tables_chain_free_chain_rules(struct nft_chain *chain)
2068{
2c865a8a
PNA
2069 struct nft_rule_blob *g0 = rcu_dereference_raw(chain->blob_gen_0);
2070 struct nft_rule_blob *g1 = rcu_dereference_raw(chain->blob_gen_1);
0cbc06b3
FW
2071
2072 if (g0 != g1)
2073 kvfree(g1);
2074 kvfree(g0);
2075
2076 /* should be NULL either via abort or via successful commit */
2c865a8a
PNA
2077 WARN_ON_ONCE(chain->blob_next);
2078 kvfree(chain->blob_next);
0cbc06b3
FW
2079}
2080
d0e2c7de 2081void nf_tables_chain_destroy(struct nft_ctx *ctx)
91c7b38d 2082{
43a605f2 2083 struct nft_chain *chain = ctx->chain;
d54725cd 2084 struct nft_hook *hook, *next;
43a605f2 2085
fa5950e4
FW
2086 if (WARN_ON(chain->use > 0))
2087 return;
91c7b38d 2088
0cbc06b3
FW
2089 /* no concurrent access possible anymore */
2090 nf_tables_chain_free_chain_rules(chain);
2091
f323d954 2092 if (nft_is_base_chain(chain)) {
2cbce139
PNA
2093 struct nft_base_chain *basechain = nft_base_chain(chain);
2094
d3519cb8 2095 if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) {
d54725cd
PNA
2096 list_for_each_entry_safe(hook, next,
2097 &basechain->hook_list, list) {
2098 list_del_rcu(&hook->list);
2099 kfree_rcu(hook, rcu);
2100 }
2101 }
2cbce139 2102 module_put(basechain->type->owner);
4c05ec47 2103 if (rcu_access_pointer(basechain->stats)) {
9f08ea84 2104 static_branch_dec(&nft_counters_enabled);
4c05ec47
TY
2105 free_percpu(rcu_dereference_raw(basechain->stats));
2106 }
b7263e07 2107 kfree(chain->name);
002f2176 2108 kfree(chain->udata);
2cbce139 2109 kfree(basechain);
91c7b38d 2110 } else {
b7263e07 2111 kfree(chain->name);
002f2176 2112 kfree(chain->udata);
91c7b38d
PNA
2113 kfree(chain);
2114 }
2115}
2116
3f0465a9
PNA
2117static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
2118 const struct nlattr *attr)
2119{
2120 struct net_device *dev;
2121 char ifname[IFNAMSIZ];
2122 struct nft_hook *hook;
2123 int err;
2124
195e5f88 2125 hook = kzalloc(sizeof(struct nft_hook), GFP_KERNEL_ACCOUNT);
3f0465a9
PNA
2126 if (!hook) {
2127 err = -ENOMEM;
2128 goto err_hook_alloc;
2129 }
2130
872f6903 2131 nla_strscpy(ifname, attr, IFNAMSIZ);
42f1c271
PNA
2132 /* nf_tables_netdev_event() is called under rtnl_mutex, this is
2133 * indirectly serializing all the other holders of the commit_mutex with
2134 * the rtnl_mutex.
2135 */
3f0465a9
PNA
2136 dev = __dev_get_by_name(net, ifname);
2137 if (!dev) {
2138 err = -ENOENT;
2139 goto err_hook_dev;
2140 }
2141 hook->ops.dev = dev;
2142
2143 return hook;
2144
2145err_hook_dev:
2146 kfree(hook);
2147err_hook_alloc:
2148 return ERR_PTR(err);
2149}
2150
abadb2f8
PNA
2151static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,
2152 const struct nft_hook *this)
b75a3e83
PNA
2153{
2154 struct nft_hook *hook;
2155
2156 list_for_each_entry(hook, hook_list, list) {
2157 if (this->ops.dev == hook->ops.dev)
abadb2f8 2158 return hook;
b75a3e83
PNA
2159 }
2160
abadb2f8 2161 return NULL;
b75a3e83
PNA
2162}
2163
3f0465a9
PNA
2164static int nf_tables_parse_netdev_hooks(struct net *net,
2165 const struct nlattr *attr,
c3c060ad
PNA
2166 struct list_head *hook_list,
2167 struct netlink_ext_ack *extack)
3f0465a9
PNA
2168{
2169 struct nft_hook *hook, *next;
2170 const struct nlattr *tmp;
2171 int rem, n = 0, err;
2172
2173 nla_for_each_nested(tmp, attr, rem) {
2174 if (nla_type(tmp) != NFTA_DEVICE_NAME) {
2175 err = -EINVAL;
2176 goto err_hook;
2177 }
2178
2179 hook = nft_netdev_hook_alloc(net, tmp);
2180 if (IS_ERR(hook)) {
c3c060ad 2181 NL_SET_BAD_ATTR(extack, tmp);
3f0465a9
PNA
2182 err = PTR_ERR(hook);
2183 goto err_hook;
2184 }
b75a3e83 2185 if (nft_hook_list_find(hook_list, hook)) {
c3c060ad 2186 NL_SET_BAD_ATTR(extack, tmp);
cd77e75b 2187 kfree(hook);
b75a3e83
PNA
2188 err = -EEXIST;
2189 goto err_hook;
2190 }
3f0465a9
PNA
2191 list_add_tail(&hook->list, hook_list);
2192 n++;
2193
cb662ac6 2194 if (n == NFT_NETDEVICE_MAX) {
3f0465a9
PNA
2195 err = -EFBIG;
2196 goto err_hook;
2197 }
2198 }
3f0465a9
PNA
2199
2200 return 0;
2201
2202err_hook:
2203 list_for_each_entry_safe(hook, next, hook_list, list) {
2204 list_del(&hook->list);
2205 kfree(hook);
2206 }
2207 return err;
2208}
2209
508f8ccd
PNA
2210struct nft_chain_hook {
2211 u32 num;
84ba7dd7 2212 s32 priority;
32537e91 2213 const struct nft_chain_type *type;
d54725cd 2214 struct list_head list;
508f8ccd
PNA
2215};
2216
207296f1 2217static int nft_chain_parse_netdev(struct net *net, struct nlattr *tb[],
c3c060ad 2218 struct list_head *hook_list,
207296f1 2219 struct netlink_ext_ack *extack, u32 flags)
d54725cd
PNA
2220{
2221 struct nft_hook *hook;
2222 int err;
2223
2224 if (tb[NFTA_HOOK_DEV]) {
2225 hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]);
c3c060ad
PNA
2226 if (IS_ERR(hook)) {
2227 NL_SET_BAD_ATTR(extack, tb[NFTA_HOOK_DEV]);
d54725cd 2228 return PTR_ERR(hook);
c3c060ad 2229 }
d54725cd
PNA
2230
2231 list_add_tail(&hook->list, hook_list);
2232 } else if (tb[NFTA_HOOK_DEVS]) {
2233 err = nf_tables_parse_netdev_hooks(net, tb[NFTA_HOOK_DEVS],
c3c060ad 2234 hook_list, extack);
d54725cd
PNA
2235 if (err < 0)
2236 return err;
05abe445 2237
d54725cd
PNA
2238 }
2239
207296f1
PNA
2240 if (flags & NFT_CHAIN_HW_OFFLOAD &&
2241 list_empty(hook_list))
2242 return -EINVAL;
2243
d54725cd
PNA
2244 return 0;
2245}
2246
508f8ccd 2247static int nft_chain_parse_hook(struct net *net,
b9703ed4 2248 struct nft_base_chain *basechain,
508f8ccd 2249 const struct nlattr * const nla[],
36596dad 2250 struct nft_chain_hook *hook, u8 family,
207296f1 2251 u32 flags, struct netlink_ext_ack *extack)
508f8ccd 2252{
d59d2f82 2253 struct nftables_pernet *nft_net = nft_pernet(net);
508f8ccd 2254 struct nlattr *ha[NFTA_HOOK_MAX + 1];
32537e91 2255 const struct nft_chain_type *type;
508f8ccd
PNA
2256 int err;
2257
0854db2a 2258 lockdep_assert_held(&nft_net->commit_mutex);
f102d66b
FW
2259 lockdep_nfnl_nft_mutex_not_held();
2260
8cb08174
JB
2261 err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX,
2262 nla[NFTA_CHAIN_HOOK],
2263 nft_hook_policy, NULL);
508f8ccd
PNA
2264 if (err < 0)
2265 return err;
2266
b9703ed4
PNA
2267 if (!basechain) {
2268 if (!ha[NFTA_HOOK_HOOKNUM] ||
8509f62b
PNA
2269 !ha[NFTA_HOOK_PRIORITY]) {
2270 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
2271 return -ENOENT;
2272 }
508f8ccd 2273
b9703ed4
PNA
2274 hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
2275 hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
508f8ccd 2276
b9703ed4
PNA
2277 type = __nft_chain_type_get(family, NFT_CHAIN_T_DEFAULT);
2278 if (!type)
2279 return -EOPNOTSUPP;
82603549 2280
b9703ed4
PNA
2281 if (nla[NFTA_CHAIN_TYPE]) {
2282 type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE],
2283 family, true);
2284 if (IS_ERR(type)) {
2285 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]);
2286 return PTR_ERR(type);
2287 }
983c4fcb 2288 }
b9703ed4
PNA
2289 if (hook->num >= NFT_MAX_HOOKS || !(type->hook_mask & (1 << hook->num)))
2290 return -EOPNOTSUPP;
84ba7dd7 2291
b9703ed4
PNA
2292 if (type->type == NFT_CHAIN_T_NAT &&
2293 hook->priority <= NF_IP_PRI_CONNTRACK)
2294 return -EOPNOTSUPP;
2295 } else {
2296 if (ha[NFTA_HOOK_HOOKNUM]) {
2297 hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
2298 if (hook->num != basechain->ops.hooknum)
2299 return -EOPNOTSUPP;
2300 }
2301 if (ha[NFTA_HOOK_PRIORITY]) {
2302 hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
2303 if (hook->priority != basechain->ops.priority)
2304 return -EOPNOTSUPP;
2305 }
2306
aaba7ddc
PNA
2307 if (nla[NFTA_CHAIN_TYPE]) {
2308 type = __nf_tables_chain_type_lookup(nla[NFTA_CHAIN_TYPE],
2309 family);
2310 if (!type) {
2311 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]);
2312 return -ENOENT;
2313 }
2314 } else {
2315 type = basechain->type;
2316 }
b9703ed4 2317 }
84ba7dd7 2318
983c4fcb
PNA
2319 if (!try_module_get(type->owner)) {
2320 if (nla[NFTA_CHAIN_TYPE])
2321 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]);
508f8ccd 2322 return -ENOENT;
983c4fcb 2323 }
508f8ccd
PNA
2324
2325 hook->type = type;
2326
d54725cd 2327 INIT_LIST_HEAD(&hook->list);
d3519cb8 2328 if (nft_base_chain_netdev(family, hook->num)) {
207296f1 2329 err = nft_chain_parse_netdev(net, ha, &hook->list, extack, flags);
d54725cd 2330 if (err < 0) {
508f8ccd 2331 module_put(type->owner);
d54725cd 2332 return err;
508f8ccd 2333 }
d54725cd 2334 } else if (ha[NFTA_HOOK_DEV] || ha[NFTA_HOOK_DEVS]) {
508f8ccd
PNA
2335 module_put(type->owner);
2336 return -EOPNOTSUPP;
2337 }
2338
2339 return 0;
2340}
2341
2342static void nft_chain_release_hook(struct nft_chain_hook *hook)
2343{
d54725cd
PNA
2344 struct nft_hook *h, *next;
2345
2346 list_for_each_entry_safe(h, next, &hook->list, list) {
2347 list_del(&h->list);
2348 kfree(h);
2349 }
508f8ccd 2350 module_put(hook->type->owner);
508f8ccd
PNA
2351}
2352
e38fbfa9 2353static void nft_last_rule(const struct nft_chain *chain, const void *ptr)
0cbc06b3 2354{
e38fbfa9
FW
2355 struct nft_rule_dp_last *lrule;
2356
2357 BUILD_BUG_ON(offsetof(struct nft_rule_dp_last, end) != 0);
2c865a8a 2358
e38fbfa9
FW
2359 lrule = (struct nft_rule_dp_last *)ptr;
2360 lrule->end.is_last = 1;
2361 lrule->chain = chain;
2c865a8a
PNA
2362 /* blob size does not include the trailer rule */
2363}
2364
e38fbfa9
FW
2365static struct nft_rule_blob *nf_tables_chain_alloc_rules(const struct nft_chain *chain,
2366 unsigned int size)
2c865a8a
PNA
2367{
2368 struct nft_rule_blob *blob;
2369
2c865a8a 2370 if (size > INT_MAX)
0cbc06b3
FW
2371 return NULL;
2372
e38fbfa9
FW
2373 size += sizeof(struct nft_rule_blob) + sizeof(struct nft_rule_dp_last);
2374
33758c89 2375 blob = kvmalloc(size, GFP_KERNEL_ACCOUNT);
2c865a8a 2376 if (!blob)
0cbc06b3
FW
2377 return NULL;
2378
2c865a8a 2379 blob->size = 0;
e38fbfa9 2380 nft_last_rule(chain, blob->data);
0cbc06b3 2381
2c865a8a 2382 return blob;
0cbc06b3
FW
2383}
2384
d54725cd
PNA
2385static void nft_basechain_hook_init(struct nf_hook_ops *ops, u8 family,
2386 const struct nft_chain_hook *hook,
2387 struct nft_chain *chain)
2388{
c5c6accd
FW
2389 ops->pf = family;
2390 ops->hooknum = hook->num;
2391 ops->priority = hook->priority;
2392 ops->priv = chain;
2393 ops->hook = hook->type->hooks[ops->hooknum];
2394 ops->hook_ops_type = NF_HOOK_OP_NF_TABLES;
d54725cd
PNA
2395}
2396
2397static int nft_basechain_init(struct nft_base_chain *basechain, u8 family,
2398 struct nft_chain_hook *hook, u32 flags)
2399{
2400 struct nft_chain *chain;
2401 struct nft_hook *h;
2402
2403 basechain->type = hook->type;
2404 INIT_LIST_HEAD(&basechain->hook_list);
2405 chain = &basechain->chain;
2406
d3519cb8 2407 if (nft_base_chain_netdev(family, hook->num)) {
d54725cd
PNA
2408 list_splice_init(&hook->list, &basechain->hook_list);
2409 list_for_each_entry(h, &basechain->hook_list, list)
2410 nft_basechain_hook_init(&h->ops, family, hook, chain);
d54725cd 2411 }
b9703ed4 2412 nft_basechain_hook_init(&basechain->ops, family, hook, chain);
d54725cd 2413
67c49de4 2414 chain->flags |= NFT_CHAIN_BASE | flags;
d54725cd
PNA
2415 basechain->policy = NF_ACCEPT;
2416 if (chain->flags & NFT_CHAIN_HW_OFFLOAD &&
77972a36
PNA
2417 !nft_chain_offload_support(basechain)) {
2418 list_splice_init(&basechain->hook_list, &hook->list);
d54725cd 2419 return -EOPNOTSUPP;
77972a36 2420 }
d54725cd
PNA
2421
2422 flow_block_init(&basechain->flow_block);
2423
2424 return 0;
2425}
2426
4bedf9ee 2427int nft_chain_add(struct nft_table *table, struct nft_chain *chain)
04b7db41
PNA
2428{
2429 int err;
2430
2431 err = rhltable_insert_key(&table->chains_ht, chain->name,
2432 &chain->rhlhead, nft_chain_ht_params);
2433 if (err)
2434 return err;
2435
2436 list_add_tail_rcu(&chain->list, &table->chains);
2437
2438 return 0;
2439}
2440
d0e2c7de
PNA
2441static u64 chain_id;
2442
4035285f 2443static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
983c4fcb
PNA
2444 u8 policy, u32 flags,
2445 struct netlink_ext_ack *extack)
4035285f
PNA
2446{
2447 const struct nlattr * const *nla = ctx->nla;
2448 struct nft_table *table = ctx->table;
4035285f 2449 struct nft_base_chain *basechain;
4035285f 2450 struct net *net = ctx->net;
d0e2c7de 2451 char name[NFT_NAME_MAXLEN];
2c865a8a 2452 struct nft_rule_blob *blob;
66293c46 2453 struct nft_trans *trans;
4035285f 2454 struct nft_chain *chain;
4035285f
PNA
2455 int err;
2456
4035285f 2457 if (nla[NFTA_CHAIN_HOOK]) {
921ebde3 2458 struct nft_stats __percpu *stats = NULL;
b9703ed4 2459 struct nft_chain_hook hook = {};
4035285f 2460
994209dd
PNA
2461 if (table->flags & __NFT_TABLE_F_UPDATE)
2462 return -EINVAL;
2463
d0e2c7de
PNA
2464 if (flags & NFT_CHAIN_BINDING)
2465 return -EOPNOTSUPP;
2466
207296f1 2467 err = nft_chain_parse_hook(net, NULL, nla, &hook, family, flags,
b9703ed4 2468 extack);
4035285f
PNA
2469 if (err < 0)
2470 return err;
2471
33758c89 2472 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL_ACCOUNT);
4035285f
PNA
2473 if (basechain == NULL) {
2474 nft_chain_release_hook(&hook);
2475 return -ENOMEM;
2476 }
d54725cd 2477 chain = &basechain->chain;
4035285f
PNA
2478
2479 if (nla[NFTA_CHAIN_COUNTERS]) {
2480 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
2481 if (IS_ERR(stats)) {
2482 nft_chain_release_hook(&hook);
2483 kfree(basechain);
2484 return PTR_ERR(stats);
2485 }
4c05ec47 2486 rcu_assign_pointer(basechain->stats, stats);
4035285f
PNA
2487 }
2488
d54725cd
PNA
2489 err = nft_basechain_init(basechain, family, &hook, flags);
2490 if (err < 0) {
2491 nft_chain_release_hook(&hook);
2492 kfree(basechain);
9a4d6dd5 2493 free_percpu(stats);
d54725cd
PNA
2494 return err;
2495 }
921ebde3
TH
2496 if (stats)
2497 static_branch_inc(&nft_counters_enabled);
4035285f 2498 } else {
d0e2c7de
PNA
2499 if (flags & NFT_CHAIN_BASE)
2500 return -EINVAL;
2501 if (flags & NFT_CHAIN_HW_OFFLOAD)
2502 return -EOPNOTSUPP;
2503
33758c89 2504 chain = kzalloc(sizeof(*chain), GFP_KERNEL_ACCOUNT);
4035285f
PNA
2505 if (chain == NULL)
2506 return -ENOMEM;
d0e2c7de
PNA
2507
2508 chain->flags = flags;
4035285f 2509 }
43a605f2
PNA
2510 ctx->chain = chain;
2511
4035285f
PNA
2512 INIT_LIST_HEAD(&chain->rules);
2513 chain->handle = nf_tables_alloc_handle(table);
2514 chain->table = table;
d0e2c7de
PNA
2515
2516 if (nla[NFTA_CHAIN_NAME]) {
33758c89 2517 chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT);
d0e2c7de 2518 } else {
59136aa3
FW
2519 if (!(flags & NFT_CHAIN_BINDING)) {
2520 err = -EINVAL;
002f2176 2521 goto err_destroy_chain;
59136aa3 2522 }
d0e2c7de
PNA
2523
2524 snprintf(name, sizeof(name), "__chain%llu", ++chain_id);
33758c89 2525 chain->name = kstrdup(name, GFP_KERNEL_ACCOUNT);
d0e2c7de
PNA
2526 }
2527
4035285f
PNA
2528 if (!chain->name) {
2529 err = -ENOMEM;
002f2176
JGG
2530 goto err_destroy_chain;
2531 }
2532
2533 if (nla[NFTA_CHAIN_USERDATA]) {
33758c89 2534 chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL_ACCOUNT);
002f2176
JGG
2535 if (chain->udata == NULL) {
2536 err = -ENOMEM;
2537 goto err_destroy_chain;
2538 }
2539 chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]);
4035285f
PNA
2540 }
2541
e38fbfa9 2542 blob = nf_tables_chain_alloc_rules(chain, 0);
2c865a8a 2543 if (!blob) {
0cbc06b3 2544 err = -ENOMEM;
002f2176 2545 goto err_destroy_chain;
0cbc06b3
FW
2546 }
2547
2c865a8a
PNA
2548 RCU_INIT_POINTER(chain->blob_gen_0, blob);
2549 RCU_INIT_POINTER(chain->blob_gen_1, blob);
0cbc06b3 2550
1689f259
PNA
2551 if (!nft_use_inc(&table->use)) {
2552 err = -EMFILE;
d472e985 2553 goto err_destroy_chain;
1689f259
PNA
2554 }
2555
66293c46
FW
2556 trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN);
2557 if (IS_ERR(trans)) {
2558 err = PTR_ERR(trans);
d472e985 2559 goto err_trans;
1b2470e5 2560 }
4035285f 2561
ad652f38 2562 nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET;
66293c46
FW
2563 if (nft_is_base_chain(chain))
2564 nft_trans_chain_policy(trans) = policy;
2565
04b7db41 2566 err = nft_chain_add(table, chain);
d472e985
PNA
2567 if (err < 0)
2568 goto err_chain_add;
2569
2570 /* This must be LAST to ensure no packets are walking over this chain. */
2571 err = nf_tables_register_hook(net, table, chain);
2572 if (err < 0)
2573 goto err_register_hook;
04b7db41 2574
4035285f 2575 return 0;
1689f259 2576
d472e985
PNA
2577err_register_hook:
2578 nft_chain_del(chain);
2579err_chain_add:
2580 nft_trans_destroy(trans);
2581err_trans:
1689f259 2582 nft_use_dec_restore(&table->use);
002f2176 2583err_destroy_chain:
43a605f2 2584 nf_tables_chain_destroy(ctx);
4035285f
PNA
2585
2586 return err;
2587}
2588
c9626a2c 2589static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy,
98a381a7
PNA
2590 u32 flags, const struct nlattr *attr,
2591 struct netlink_ext_ack *extack)
2c4a488a
PNA
2592{
2593 const struct nlattr * const *nla = ctx->nla;
b9703ed4 2594 struct nft_base_chain *basechain = NULL;
2c4a488a
PNA
2595 struct nft_table *table = ctx->table;
2596 struct nft_chain *chain = ctx->chain;
b9703ed4 2597 struct nft_chain_hook hook = {};
2c4a488a 2598 struct nft_stats *stats = NULL;
b9703ed4 2599 struct nft_hook *h, *next;
2c4a488a
PNA
2600 struct nf_hook_ops *ops;
2601 struct nft_trans *trans;
b9703ed4 2602 bool unregister = false;
c974a3a3 2603 int err;
2c4a488a 2604
c9626a2c
PNA
2605 if (chain->flags ^ flags)
2606 return -EOPNOTSUPP;
2607
b9703ed4
PNA
2608 INIT_LIST_HEAD(&hook.list);
2609
2c4a488a 2610 if (nla[NFTA_CHAIN_HOOK]) {
98a381a7
PNA
2611 if (!nft_is_base_chain(chain)) {
2612 NL_SET_BAD_ATTR(extack, attr);
77a92189 2613 return -EEXIST;
98a381a7 2614 }
b9703ed4
PNA
2615
2616 basechain = nft_base_chain(chain);
2617 err = nft_chain_parse_hook(ctx->net, basechain, nla, &hook,
207296f1 2618 ctx->family, flags, extack);
2c4a488a
PNA
2619 if (err < 0)
2620 return err;
2621
2c4a488a
PNA
2622 if (basechain->type != hook.type) {
2623 nft_chain_release_hook(&hook);
98a381a7 2624 NL_SET_BAD_ATTR(extack, attr);
77a92189 2625 return -EEXIST;
2c4a488a
PNA
2626 }
2627
b9703ed4
PNA
2628 if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) {
2629 list_for_each_entry_safe(h, next, &hook.list, list) {
2630 h->ops.pf = basechain->ops.pf;
2631 h->ops.hooknum = basechain->ops.hooknum;
2632 h->ops.priority = basechain->ops.priority;
2633 h->ops.priv = basechain->ops.priv;
2634 h->ops.hook = basechain->ops.hook;
2635
2636 if (nft_hook_list_find(&basechain->hook_list, h)) {
2637 list_del(&h->list);
2638 kfree(h);
2639 }
d54725cd
PNA
2640 }
2641 } else {
2642 ops = &basechain->ops;
2643 if (ops->hooknum != hook.num ||
2644 ops->priority != hook.priority) {
2645 nft_chain_release_hook(&hook);
98a381a7 2646 NL_SET_BAD_ATTR(extack, attr);
77a92189 2647 return -EEXIST;
d54725cd 2648 }
2c4a488a 2649 }
2c4a488a
PNA
2650 }
2651
2652 if (nla[NFTA_CHAIN_HANDLE] &&
2653 nla[NFTA_CHAIN_NAME]) {
2654 struct nft_chain *chain2;
2655
f102d66b
FW
2656 chain2 = nft_chain_lookup(ctx->net, table,
2657 nla[NFTA_CHAIN_NAME], genmask);
98a381a7
PNA
2658 if (!IS_ERR(chain2)) {
2659 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
b9703ed4
PNA
2660 err = -EEXIST;
2661 goto err_hooks;
98a381a7 2662 }
2c4a488a
PNA
2663 }
2664
1e1fb6f0
PNA
2665 if (table->flags & __NFT_TABLE_F_UPDATE &&
2666 !list_empty(&hook.list)) {
2667 NL_SET_BAD_ATTR(extack, attr);
2668 err = -EOPNOTSUPP;
2669 goto err_hooks;
2670 }
2671
b9703ed4
PNA
2672 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
2673 nft_is_base_chain(chain) &&
2674 !list_empty(&hook.list)) {
2675 basechain = nft_base_chain(chain);
2676 ops = &basechain->ops;
2677
2678 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) {
2679 err = nft_netdev_register_hooks(ctx->net, &hook.list);
2680 if (err < 0)
2681 goto err_hooks;
2682 }
2683 }
2684
2685 unregister = true;
7eaf837a
QT
2686
2687 if (nla[NFTA_CHAIN_COUNTERS]) {
2688 if (!nft_is_base_chain(chain)) {
2689 err = -EOPNOTSUPP;
2690 goto err_hooks;
2691 }
2692
2693 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
2694 if (IS_ERR(stats)) {
2695 err = PTR_ERR(stats);
2696 goto err_hooks;
2697 }
2698 }
2699
c6cc94df 2700 err = -ENOMEM;
2c4a488a
PNA
2701 trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN,
2702 sizeof(struct nft_trans_chain));
c6cc94df 2703 if (trans == NULL)
b9703ed4 2704 goto err_trans;
2c4a488a
PNA
2705
2706 nft_trans_chain_stats(trans) = stats;
2707 nft_trans_chain_update(trans) = true;
2708
2709 if (nla[NFTA_CHAIN_POLICY])
2710 nft_trans_chain_policy(trans) = policy;
2711 else
2712 nft_trans_chain_policy(trans) = -1;
2713
c6cc94df
FW
2714 if (nla[NFTA_CHAIN_HANDLE] &&
2715 nla[NFTA_CHAIN_NAME]) {
d59d2f82 2716 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
c6cc94df
FW
2717 struct nft_trans *tmp;
2718 char *name;
2719
2720 err = -ENOMEM;
33758c89 2721 name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT);
c6cc94df 2722 if (!name)
b9703ed4 2723 goto err_trans;
c6cc94df
FW
2724
2725 err = -EEXIST;
0854db2a 2726 list_for_each_entry(tmp, &nft_net->commit_list, list) {
c6cc94df
FW
2727 if (tmp->msg_type == NFT_MSG_NEWCHAIN &&
2728 tmp->ctx.table == table &&
2729 nft_trans_chain_update(tmp) &&
2730 nft_trans_chain_name(tmp) &&
2731 strcmp(name, nft_trans_chain_name(tmp)) == 0) {
98a381a7 2732 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
c6cc94df 2733 kfree(name);
b9703ed4 2734 goto err_trans;
c6cc94df 2735 }
2c4a488a 2736 }
c6cc94df
FW
2737
2738 nft_trans_chain_name(trans) = name;
2c4a488a 2739 }
b9703ed4
PNA
2740
2741 nft_trans_basechain(trans) = basechain;
2742 INIT_LIST_HEAD(&nft_trans_chain_hooks(trans));
2743 list_splice(&hook.list, &nft_trans_chain_hooks(trans));
043d2acf
PNA
2744 if (nla[NFTA_CHAIN_HOOK])
2745 module_put(hook.type->owner);
b9703ed4 2746
0854db2a 2747 nft_trans_commit_list_add_tail(ctx->net, trans);
2c4a488a
PNA
2748
2749 return 0;
b9703ed4
PNA
2750
2751err_trans:
c6cc94df
FW
2752 free_percpu(stats);
2753 kfree(trans);
b9703ed4
PNA
2754err_hooks:
2755 if (nla[NFTA_CHAIN_HOOK]) {
2756 list_for_each_entry_safe(h, next, &hook.list, list) {
2757 if (unregister)
2758 nf_unregister_net_hook(ctx->net, &h->ops);
2759 list_del(&h->list);
2760 kfree_rcu(h, rcu);
2761 }
2762 module_put(hook.type->owner);
2763 }
2764
c6cc94df 2765 return err;
2c4a488a
PNA
2766}
2767
837830a4 2768static struct nft_chain *nft_chain_lookup_byid(const struct net *net,
95f466d2 2769 const struct nft_table *table,
515ad530 2770 const struct nlattr *nla, u8 genmask)
837830a4 2771{
d59d2f82 2772 struct nftables_pernet *nft_net = nft_pernet(net);
837830a4
PNA
2773 u32 id = ntohl(nla_get_be32(nla));
2774 struct nft_trans *trans;
2775
0854db2a 2776 list_for_each_entry(trans, &nft_net->commit_list, list) {
837830a4
PNA
2777 struct nft_chain *chain = trans->ctx.chain;
2778
2779 if (trans->msg_type == NFT_MSG_NEWCHAIN &&
95f466d2 2780 chain->table == table &&
515ad530
TLSC
2781 id == nft_trans_chain_id(trans) &&
2782 nft_active_genmask(chain, genmask))
837830a4
PNA
2783 return chain;
2784 }
2785 return ERR_PTR(-ENOENT);
2786}
2787
7dab8ee3
PNA
2788static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info,
2789 const struct nlattr * const nla[])
96518518 2790{
7dab8ee3 2791 struct nftables_pernet *nft_net = nft_pernet(info->net);
7dab8ee3
PNA
2792 struct netlink_ext_ack *extack = info->extack;
2793 u8 genmask = nft_genmask_next(info->net);
ef4b65e5 2794 u8 family = info->nfmsg->nfgen_family;
74cccc3d 2795 struct nft_chain *chain = NULL;
7dab8ee3 2796 struct net *net = info->net;
36dd1bcc 2797 const struct nlattr *attr;
96518518 2798 struct nft_table *table;
57de2a0c 2799 u8 policy = NF_ACCEPT;
4035285f 2800 struct nft_ctx ctx;
96518518 2801 u64 handle = 0;
c9626a2c 2802 u32 flags = 0;
96518518 2803
0854db2a 2804 lockdep_assert_held(&nft_net->commit_mutex);
f102d66b 2805
6001a930
PNA
2806 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask,
2807 NETLINK_CB(skb).portid);
36dd1bcc
PNA
2808 if (IS_ERR(table)) {
2809 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
96518518 2810 return PTR_ERR(table);
36dd1bcc 2811 }
96518518 2812
96518518 2813 chain = NULL;
36dd1bcc 2814 attr = nla[NFTA_CHAIN_NAME];
96518518
PM
2815
2816 if (nla[NFTA_CHAIN_HANDLE]) {
2817 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
cac20fcd 2818 chain = nft_chain_lookup_byhandle(table, handle, genmask);
36dd1bcc
PNA
2819 if (IS_ERR(chain)) {
2820 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]);
96518518 2821 return PTR_ERR(chain);
36dd1bcc
PNA
2822 }
2823 attr = nla[NFTA_CHAIN_HANDLE];
74cccc3d 2824 } else if (nla[NFTA_CHAIN_NAME]) {
f102d66b 2825 chain = nft_chain_lookup(net, table, attr, genmask);
96518518 2826 if (IS_ERR(chain)) {
36dd1bcc
PNA
2827 if (PTR_ERR(chain) != -ENOENT) {
2828 NL_SET_BAD_ATTR(extack, attr);
96518518 2829 return PTR_ERR(chain);
36dd1bcc 2830 }
96518518
PM
2831 chain = NULL;
2832 }
74cccc3d
PNA
2833 } else if (!nla[NFTA_CHAIN_ID]) {
2834 return -EINVAL;
96518518
PM
2835 }
2836
57de2a0c 2837 if (nla[NFTA_CHAIN_POLICY]) {
f323d954 2838 if (chain != NULL &&
36dd1bcc
PNA
2839 !nft_is_base_chain(chain)) {
2840 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
d6b6cb1d 2841 return -EOPNOTSUPP;
36dd1bcc 2842 }
d6b6cb1d
PNA
2843
2844 if (chain == NULL &&
36dd1bcc
PNA
2845 nla[NFTA_CHAIN_HOOK] == NULL) {
2846 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
57de2a0c 2847 return -EOPNOTSUPP;
36dd1bcc 2848 }
57de2a0c 2849
8f46df18 2850 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
57de2a0c
PM
2851 switch (policy) {
2852 case NF_DROP:
2853 case NF_ACCEPT:
2854 break;
2855 default:
2856 return -EINVAL;
2857 }
2858 }
2859
c9626a2c
PNA
2860 if (nla[NFTA_CHAIN_FLAGS])
2861 flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS]));
b717273d
FW
2862 else if (chain)
2863 flags = chain->flags;
c9626a2c 2864
c1f79a2e
PNA
2865 if (flags & ~NFT_CHAIN_FLAGS)
2866 return -EOPNOTSUPP;
2867
7dab8ee3 2868 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
4035285f 2869
96518518 2870 if (chain != NULL) {
5dc52d83
PNA
2871 if (chain->flags & NFT_CHAIN_BINDING)
2872 return -EINVAL;
2873
7dab8ee3 2874 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
36dd1bcc 2875 NL_SET_BAD_ATTR(extack, attr);
96518518 2876 return -EEXIST;
36dd1bcc 2877 }
7dab8ee3 2878 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
96518518
PM
2879 return -EOPNOTSUPP;
2880
67c49de4 2881 flags |= chain->flags & NFT_CHAIN_BASE;
98a381a7
PNA
2882 return nf_tables_updchain(&ctx, genmask, policy, flags, attr,
2883 extack);
96518518
PM
2884 }
2885
983c4fcb 2886 return nf_tables_addchain(&ctx, family, genmask, policy, flags, extack);
96518518
PM
2887}
2888
42e344f0
PS
2889static int nft_delchain_hook(struct nft_ctx *ctx,
2890 struct nft_base_chain *basechain,
7d937b10
PNA
2891 struct netlink_ext_ack *extack)
2892{
42e344f0 2893 const struct nft_chain *chain = &basechain->chain;
7d937b10
PNA
2894 const struct nlattr * const *nla = ctx->nla;
2895 struct nft_chain_hook chain_hook = {};
7d937b10
PNA
2896 struct nft_hook *this, *hook;
2897 LIST_HEAD(chain_del_list);
2898 struct nft_trans *trans;
2899 int err;
2900
1e1fb6f0
PNA
2901 if (ctx->table->flags & __NFT_TABLE_F_UPDATE)
2902 return -EOPNOTSUPP;
2903
7d937b10 2904 err = nft_chain_parse_hook(ctx->net, basechain, nla, &chain_hook,
207296f1 2905 ctx->family, chain->flags, extack);
7d937b10
PNA
2906 if (err < 0)
2907 return err;
2908
2909 list_for_each_entry(this, &chain_hook.list, list) {
2910 hook = nft_hook_list_find(&basechain->hook_list, this);
2911 if (!hook) {
2912 err = -ENOENT;
2913 goto err_chain_del_hook;
2914 }
2915 list_move(&hook->list, &chain_del_list);
2916 }
2917
2918 trans = nft_trans_alloc(ctx, NFT_MSG_DELCHAIN,
2919 sizeof(struct nft_trans_chain));
2920 if (!trans) {
2921 err = -ENOMEM;
2922 goto err_chain_del_hook;
2923 }
2924
2925 nft_trans_basechain(trans) = basechain;
2926 nft_trans_chain_update(trans) = true;
2927 INIT_LIST_HEAD(&nft_trans_chain_hooks(trans));
2928 list_splice(&chain_del_list, &nft_trans_chain_hooks(trans));
2929 nft_chain_release_hook(&chain_hook);
2930
2931 nft_trans_commit_list_add_tail(ctx->net, trans);
2932
2933 return 0;
2934
2935err_chain_del_hook:
2936 list_splice(&chain_del_list, &basechain->hook_list);
2937 nft_chain_release_hook(&chain_hook);
2938
2939 return err;
2940}
2941
7dab8ee3
PNA
2942static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info,
2943 const struct nlattr * const nla[])
96518518 2944{
7dab8ee3
PNA
2945 struct netlink_ext_ack *extack = info->extack;
2946 u8 genmask = nft_genmask_next(info->net);
ef4b65e5 2947 u8 family = info->nfmsg->nfgen_family;
7dab8ee3 2948 struct net *net = info->net;
36dd1bcc 2949 const struct nlattr *attr;
96518518
PM
2950 struct nft_table *table;
2951 struct nft_chain *chain;
9dee1474 2952 struct nft_rule *rule;
91c7b38d 2953 struct nft_ctx ctx;
3ecbfd65 2954 u64 handle;
9dee1474
PNA
2955 u32 use;
2956 int err;
96518518 2957
6001a930
PNA
2958 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask,
2959 NETLINK_CB(skb).portid);
36dd1bcc
PNA
2960 if (IS_ERR(table)) {
2961 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
96518518 2962 return PTR_ERR(table);
36dd1bcc 2963 }
96518518 2964
3ecbfd65 2965 if (nla[NFTA_CHAIN_HANDLE]) {
36dd1bcc
PNA
2966 attr = nla[NFTA_CHAIN_HANDLE];
2967 handle = be64_to_cpu(nla_get_be64(attr));
cac20fcd 2968 chain = nft_chain_lookup_byhandle(table, handle, genmask);
3ecbfd65 2969 } else {
36dd1bcc 2970 attr = nla[NFTA_CHAIN_NAME];
f102d66b 2971 chain = nft_chain_lookup(net, table, attr, genmask);
3ecbfd65 2972 }
36dd1bcc 2973 if (IS_ERR(chain)) {
f80a612d
FFM
2974 if (PTR_ERR(chain) == -ENOENT &&
2975 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYCHAIN)
2976 return 0;
2977
36dd1bcc 2978 NL_SET_BAD_ATTR(extack, attr);
96518518 2979 return PTR_ERR(chain);
36dd1bcc 2980 }
9dee1474 2981
f15f29fd
PNA
2982 if (nft_chain_binding(chain))
2983 return -EOPNOTSUPP;
2984
7d937b10
PNA
2985 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
2986
2987 if (nla[NFTA_CHAIN_HOOK]) {
b32ca27f
PNA
2988 if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYCHAIN ||
2989 chain->flags & NFT_CHAIN_HW_OFFLOAD)
7d937b10
PNA
2990 return -EOPNOTSUPP;
2991
42e344f0
PS
2992 if (nft_is_base_chain(chain)) {
2993 struct nft_base_chain *basechain = nft_base_chain(chain);
2994
2995 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
2996 return nft_delchain_hook(&ctx, basechain, extack);
2997 }
7d937b10
PNA
2998 }
2999
7dab8ee3 3000 if (info->nlh->nlmsg_flags & NLM_F_NONREC &&
9dee1474 3001 chain->use > 0)
96518518
PM
3002 return -EBUSY;
3003
9dee1474
PNA
3004 use = chain->use;
3005 list_for_each_entry(rule, &chain->rules, list) {
3006 if (!nft_is_active_next(net, rule))
3007 continue;
3008 use--;
3009
3010 err = nft_delrule(&ctx, rule);
3011 if (err < 0)
3012 return err;
3013 }
3014
3015 /* There are rules and elements that are still holding references to us,
3016 * we cannot do a recursive removal in this case.
3017 */
36dd1bcc
PNA
3018 if (use > 0) {
3019 NL_SET_BAD_ATTR(extack, attr);
9dee1474 3020 return -EBUSY;
36dd1bcc 3021 }
9dee1474 3022
ee01d542 3023 return nft_delchain(&ctx);
96518518
PM
3024}
3025
96518518
PM
3026/*
3027 * Expressions
3028 */
3029
3030/**
ef1f7df9 3031 * nft_register_expr - register nf_tables expr type
3db86c39 3032 * @type: expr type
96518518 3033 *
ef1f7df9 3034 * Registers the expr type for use with nf_tables. Returns zero on
96518518
PM
3035 * success or a negative errno code otherwise.
3036 */
ef1f7df9 3037int nft_register_expr(struct nft_expr_type *type)
96518518 3038{
65b3bd60
PNA
3039 if (WARN_ON_ONCE(type->maxattr > NFT_EXPR_MAXATTR))
3040 return -ENOMEM;
3041
96518518 3042 nfnl_lock(NFNL_SUBSYS_NFTABLES);
758dbcec 3043 if (type->family == NFPROTO_UNSPEC)
e688a7f8 3044 list_add_tail_rcu(&type->list, &nf_tables_expressions);
758dbcec 3045 else
e688a7f8 3046 list_add_rcu(&type->list, &nf_tables_expressions);
96518518
PM
3047 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
3048 return 0;
3049}
3050EXPORT_SYMBOL_GPL(nft_register_expr);
3051
3052/**
ef1f7df9 3053 * nft_unregister_expr - unregister nf_tables expr type
3db86c39 3054 * @type: expr type
96518518 3055 *
ef1f7df9 3056 * Unregisters the expr typefor use with nf_tables.
96518518 3057 */
ef1f7df9 3058void nft_unregister_expr(struct nft_expr_type *type)
96518518
PM
3059{
3060 nfnl_lock(NFNL_SUBSYS_NFTABLES);
e688a7f8 3061 list_del_rcu(&type->list);
96518518
PM
3062 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
3063}
3064EXPORT_SYMBOL_GPL(nft_unregister_expr);
3065
64d46806
PM
3066static const struct nft_expr_type *__nft_expr_type_get(u8 family,
3067 struct nlattr *nla)
96518518 3068{
9cff126f 3069 const struct nft_expr_type *type, *candidate = NULL;
96518518 3070
f969eb84 3071 list_for_each_entry_rcu(type, &nf_tables_expressions, list) {
9cff126f
PNA
3072 if (!nla_strcmp(nla, type->name)) {
3073 if (!type->family && !candidate)
3074 candidate = type;
3075 else if (type->family == family)
3076 candidate = type;
3077 }
96518518 3078 }
9cff126f 3079 return candidate;
96518518
PM
3080}
3081
b9c04ae7
PNA
3082#ifdef CONFIG_MODULES
3083static int nft_expr_type_request_module(struct net *net, u8 family,
3084 struct nlattr *nla)
3085{
eb014de4
PNA
3086 if (nft_request_module(net, "nft-expr-%u-%.*s", family,
3087 nla_len(nla), (char *)nla_data(nla)) == -EAGAIN)
b9c04ae7
PNA
3088 return -EAGAIN;
3089
3090 return 0;
3091}
3092#endif
3093
452238e8
FW
3094static const struct nft_expr_type *nft_expr_type_get(struct net *net,
3095 u8 family,
64d46806 3096 struct nlattr *nla)
96518518 3097{
ef1f7df9 3098 const struct nft_expr_type *type;
96518518
PM
3099
3100 if (nla == NULL)
3101 return ERR_PTR(-EINVAL);
3102
f969eb84 3103 rcu_read_lock();
64d46806 3104 type = __nft_expr_type_get(family, nla);
f969eb84
ZX
3105 if (type != NULL && try_module_get(type->owner)) {
3106 rcu_read_unlock();
ef1f7df9 3107 return type;
f969eb84
ZX
3108 }
3109 rcu_read_unlock();
96518518 3110
f102d66b 3111 lockdep_nfnl_nft_mutex_not_held();
96518518 3112#ifdef CONFIG_MODULES
ef1f7df9 3113 if (type == NULL) {
b9c04ae7 3114 if (nft_expr_type_request_module(net, family, nla) == -EAGAIN)
64d46806
PM
3115 return ERR_PTR(-EAGAIN);
3116
eb014de4
PNA
3117 if (nft_request_module(net, "nft-expr-%.*s",
3118 nla_len(nla),
3119 (char *)nla_data(nla)) == -EAGAIN)
96518518
PM
3120 return ERR_PTR(-EAGAIN);
3121 }
3122#endif
3123 return ERR_PTR(-ENOENT);
3124}
3125
3126static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
9332d27d
FW
3127 [NFTA_EXPR_NAME] = { .type = NLA_STRING,
3128 .len = NFT_MODULE_AUTOLOAD_LIMIT },
96518518
PM
3129 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
3130};
3131
3132static int nf_tables_fill_expr_info(struct sk_buff *skb,
8daa8fde 3133 const struct nft_expr *expr, bool reset)
96518518 3134{
ef1f7df9 3135 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
96518518
PM
3136 goto nla_put_failure;
3137
3138 if (expr->ops->dump) {
ae0be8de
MK
3139 struct nlattr *data = nla_nest_start_noflag(skb,
3140 NFTA_EXPR_DATA);
96518518
PM
3141 if (data == NULL)
3142 goto nla_put_failure;
8daa8fde 3143 if (expr->ops->dump(skb, expr, reset) < 0)
96518518
PM
3144 goto nla_put_failure;
3145 nla_nest_end(skb, data);
3146 }
3147
3148 return skb->len;
3149
3150nla_put_failure:
3151 return -1;
3152};
3153
0b2d8a7b 3154int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
8daa8fde 3155 const struct nft_expr *expr, bool reset)
0b2d8a7b
PM
3156{
3157 struct nlattr *nest;
3158
ae0be8de 3159 nest = nla_nest_start_noflag(skb, attr);
0b2d8a7b
PM
3160 if (!nest)
3161 goto nla_put_failure;
8daa8fde 3162 if (nf_tables_fill_expr_info(skb, expr, reset) < 0)
0b2d8a7b
PM
3163 goto nla_put_failure;
3164 nla_nest_end(skb, nest);
3165 return 0;
3166
3167nla_put_failure:
3168 return -1;
3169}
3170
96518518
PM
3171struct nft_expr_info {
3172 const struct nft_expr_ops *ops;
83d9dcba 3173 const struct nlattr *attr;
ef1f7df9 3174 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
96518518
PM
3175};
3176
0ca743a5
PNA
3177static int nf_tables_expr_parse(const struct nft_ctx *ctx,
3178 const struct nlattr *nla,
96518518
PM
3179 struct nft_expr_info *info)
3180{
ef1f7df9 3181 const struct nft_expr_type *type;
96518518 3182 const struct nft_expr_ops *ops;
ef1f7df9 3183 struct nlattr *tb[NFTA_EXPR_MAX + 1];
96518518
PM
3184 int err;
3185
8cb08174
JB
3186 err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla,
3187 nft_expr_policy, NULL);
96518518
PM
3188 if (err < 0)
3189 return err;
3190
452238e8 3191 type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]);
ef1f7df9
PM
3192 if (IS_ERR(type))
3193 return PTR_ERR(type);
3194
3195 if (tb[NFTA_EXPR_DATA]) {
8cb08174
JB
3196 err = nla_parse_nested_deprecated(info->tb, type->maxattr,
3197 tb[NFTA_EXPR_DATA],
3198 type->policy, NULL);
ef1f7df9
PM
3199 if (err < 0)
3200 goto err1;
3201 } else
3202 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
3203
3204 if (type->select_ops != NULL) {
0ca743a5
PNA
3205 ops = type->select_ops(ctx,
3206 (const struct nlattr * const *)info->tb);
ef1f7df9
PM
3207 if (IS_ERR(ops)) {
3208 err = PTR_ERR(ops);
0ef1efd1
PNA
3209#ifdef CONFIG_MODULES
3210 if (err == -EAGAIN)
eb014de4
PNA
3211 if (nft_expr_type_request_module(ctx->net,
3212 ctx->family,
3213 tb[NFTA_EXPR_NAME]) != -EAGAIN)
3214 err = -ENOENT;
0ef1efd1 3215#endif
ef1f7df9
PM
3216 goto err1;
3217 }
3218 } else
3219 ops = type->ops;
3220
83d9dcba 3221 info->attr = nla;
96518518 3222 info->ops = ops;
83d9dcba 3223
96518518 3224 return 0;
ef1f7df9
PM
3225
3226err1:
3227 module_put(type->owner);
3228 return err;
96518518
PM
3229}
3230
3a07327d
PNA
3231int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla,
3232 struct nft_expr_info *info)
3233{
3234 struct nlattr *tb[NFTA_EXPR_MAX + 1];
3235 const struct nft_expr_type *type;
3236 int err;
3237
3238 err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla,
3239 nft_expr_policy, NULL);
3240 if (err < 0)
3241 return err;
3242
505ce063 3243 if (!tb[NFTA_EXPR_DATA] || !tb[NFTA_EXPR_NAME])
3a07327d
PNA
3244 return -EINVAL;
3245
3246 type = __nft_expr_type_get(ctx->family, tb[NFTA_EXPR_NAME]);
98cbc40e
DC
3247 if (!type)
3248 return -ENOENT;
3a07327d
PNA
3249
3250 if (!type->inner_ops)
3251 return -EOPNOTSUPP;
3252
3253 err = nla_parse_nested_deprecated(info->tb, type->maxattr,
3254 tb[NFTA_EXPR_DATA],
3255 type->policy, NULL);
3256 if (err < 0)
3257 goto err_nla_parse;
3258
3259 info->attr = nla;
3260 info->ops = type->inner_ops;
3261
3262 return 0;
3263
3264err_nla_parse:
3265 return err;
3266}
3267
96518518 3268static int nf_tables_newexpr(const struct nft_ctx *ctx,
7dab8ee3 3269 const struct nft_expr_info *expr_info,
96518518
PM
3270 struct nft_expr *expr)
3271{
7dab8ee3 3272 const struct nft_expr_ops *ops = expr_info->ops;
96518518
PM
3273 int err;
3274
3275 expr->ops = ops;
3276 if (ops->init) {
7dab8ee3 3277 err = ops->init(ctx, expr, (const struct nlattr **)expr_info->tb);
96518518
PM
3278 if (err < 0)
3279 goto err1;
3280 }
3281
96518518 3282 return 0;
96518518
PM
3283err1:
3284 expr->ops = NULL;
3285 return err;
3286}
3287
62472bce
PM
3288static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
3289 struct nft_expr *expr)
96518518 3290{
3f3a390d
PNA
3291 const struct nft_expr_type *type = expr->ops->type;
3292
96518518 3293 if (expr->ops->destroy)
62472bce 3294 expr->ops->destroy(ctx, expr);
3f3a390d 3295 module_put(type->owner);
96518518
PM
3296}
3297
795a6d6b
PNA
3298static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
3299 const struct nlattr *nla)
0b2d8a7b 3300{
7dab8ee3 3301 struct nft_expr_info expr_info;
0b2d8a7b 3302 struct nft_expr *expr;
b8e20400 3303 struct module *owner;
0b2d8a7b
PM
3304 int err;
3305
7dab8ee3 3306 err = nf_tables_expr_parse(ctx, nla, &expr_info);
0b2d8a7b 3307 if (err < 0)
52077804
PNA
3308 goto err_expr_parse;
3309
3310 err = -EOPNOTSUPP;
3311 if (!(expr_info.ops->type->flags & NFT_EXPR_STATEFUL))
3312 goto err_expr_stateful;
0b2d8a7b
PM
3313
3314 err = -ENOMEM;
33758c89 3315 expr = kzalloc(expr_info.ops->size, GFP_KERNEL_ACCOUNT);
0b2d8a7b 3316 if (expr == NULL)
52077804 3317 goto err_expr_stateful;
0b2d8a7b 3318
7dab8ee3 3319 err = nf_tables_newexpr(ctx, &expr_info, expr);
0b2d8a7b 3320 if (err < 0)
52077804 3321 goto err_expr_new;
0b2d8a7b
PM
3322
3323 return expr;
52077804 3324err_expr_new:
6cafaf47 3325 kfree(expr);
52077804 3326err_expr_stateful:
7dab8ee3
PNA
3327 owner = expr_info.ops->type->owner;
3328 if (expr_info.ops->type->release_ops)
3329 expr_info.ops->type->release_ops(expr_info.ops);
b8e20400
PNA
3330
3331 module_put(owner);
52077804 3332err_expr_parse:
0b2d8a7b
PM
3333 return ERR_PTR(err);
3334}
3335
fa23e0d4 3336int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src, gfp_t gfp)
c604cc69
PNA
3337{
3338 int err;
3339
3c13725f
PNA
3340 if (WARN_ON_ONCE(!src->ops->clone))
3341 return -EINVAL;
3342
3343 dst->ops = src->ops;
fa23e0d4 3344 err = src->ops->clone(dst, src, gfp);
3c13725f
PNA
3345 if (err < 0)
3346 return err;
c604cc69
PNA
3347
3348 __module_get(src->ops->type->owner);
3349
3350 return 0;
3351}
3352
0b2d8a7b
PM
3353void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
3354{
3355 nf_tables_expr_destroy(ctx, expr);
3356 kfree(expr);
3357}
3358
96518518
PM
3359/*
3360 * Rules
3361 */
3362
cac20fcd
PNA
3363static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain,
3364 u64 handle)
96518518
PM
3365{
3366 struct nft_rule *rule;
3367
3368 // FIXME: this sucks
d9adf22a 3369 list_for_each_entry_rcu(rule, &chain->rules, list) {
96518518
PM
3370 if (handle == rule->handle)
3371 return rule;
3372 }
3373
3374 return ERR_PTR(-ENOENT);
3375}
3376
cac20fcd
PNA
3377static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain,
3378 const struct nlattr *nla)
96518518
PM
3379{
3380 if (nla == NULL)
3381 return ERR_PTR(-EINVAL);
3382
cac20fcd 3383 return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
96518518
PM
3384}
3385
3386static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
b2fbd044
LZ
3387 [NFTA_RULE_TABLE] = { .type = NLA_STRING,
3388 .len = NFT_TABLE_MAXNAMELEN - 1 },
96518518
PM
3389 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
3390 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3391 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
013714bf 3392 [NFTA_RULE_EXPRESSIONS] = NLA_POLICY_NESTED_ARRAY(nft_expr_policy),
0ca743a5 3393 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
5e948466 3394 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
0768b3b3
PNA
3395 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
3396 .len = NFT_USERDATA_MAXLEN },
467697d2 3397 [NFTA_RULE_ID] = { .type = NLA_U32 },
0604628b 3398 [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 },
837830a4 3399 [NFTA_RULE_CHAIN_ID] = { .type = NLA_U32 },
96518518
PM
3400};
3401
84d7fce6
PNA
3402static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
3403 u32 portid, u32 seq, int event,
3404 u32 flags, int family,
96518518
PM
3405 const struct nft_table *table,
3406 const struct nft_chain *chain,
8daa8fde
PS
3407 const struct nft_rule *rule, u64 handle,
3408 bool reset)
96518518
PM
3409{
3410 struct nlmsghdr *nlh;
96518518
PM
3411 const struct nft_expr *expr, *next;
3412 struct nlattr *list;
dedb67c4 3413 u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
96518518 3414
19c28b13
PNA
3415 nlh = nfnl_msg_put(skb, portid, seq, type, flags, family, NFNETLINK_V0,
3416 nft_base_seq(net));
3417 if (!nlh)
96518518
PM
3418 goto nla_put_failure;
3419
96518518
PM
3420 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
3421 goto nla_put_failure;
3422 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
3423 goto nla_put_failure;
b46f6ded
ND
3424 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle),
3425 NFTA_RULE_PAD))
96518518
PM
3426 goto nla_put_failure;
3427
e189ae16
PNA
3428 if (event != NFT_MSG_DELRULE && handle) {
3429 if (nla_put_be64(skb, NFTA_RULE_POSITION, cpu_to_be64(handle),
b46f6ded 3430 NFTA_RULE_PAD))
5e948466
EL
3431 goto nla_put_failure;
3432 }
3433
b72920f6
PNA
3434 if (chain->flags & NFT_CHAIN_HW_OFFLOAD)
3435 nft_flow_rule_stats(chain, rule);
3436
ae0be8de 3437 list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS);
96518518
PM
3438 if (list == NULL)
3439 goto nla_put_failure;
3440 nft_rule_for_each_expr(expr, next, rule) {
8daa8fde 3441 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, reset) < 0)
96518518 3442 goto nla_put_failure;
96518518
PM
3443 }
3444 nla_nest_end(skb, list);
3445
86f1ec32
PM
3446 if (rule->udata) {
3447 struct nft_userdata *udata = nft_userdata(rule);
3448 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
3449 udata->data) < 0)
3450 goto nla_put_failure;
3451 }
0768b3b3 3452
053c095a
JB
3453 nlmsg_end(skb, nlh);
3454 return 0;
96518518
PM
3455
3456nla_put_failure:
3457 nlmsg_trim(skb, nlh);
3458 return -1;
3459}
3460
25e94a99
PNA
3461static void nf_tables_rule_notify(const struct nft_ctx *ctx,
3462 const struct nft_rule *rule, int event)
96518518 3463{
d59d2f82 3464 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
e189ae16 3465 const struct nft_rule *prule;
96518518 3466 struct sk_buff *skb;
e189ae16
PNA
3467 u64 handle = 0;
3468 u16 flags = 0;
96518518
PM
3469 int err;
3470
128ad332
PNA
3471 if (!ctx->report &&
3472 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
25e94a99 3473 return;
96518518 3474
96518518
PM
3475 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3476 if (skb == NULL)
3477 goto err;
3478
e189ae16
PNA
3479 if (event == NFT_MSG_NEWRULE &&
3480 !list_is_first(&rule->list, &ctx->chain->rules) &&
3481 !list_is_last(&rule->list, &ctx->chain->rules)) {
3482 prule = list_prev_entry(rule, list);
3483 handle = prule->handle;
3484 }
3485 if (ctx->flags & (NLM_F_APPEND | NLM_F_REPLACE))
3486 flags |= NLM_F_APPEND;
6fb721cf
PNA
3487 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
3488 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
e189ae16 3489
84d7fce6 3490 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
e189ae16 3491 event, flags, ctx->family, ctx->table,
8daa8fde 3492 ctx->chain, rule, handle, false);
96518518
PM
3493 if (err < 0) {
3494 kfree_skb(skb);
3495 goto err;
3496 }
3497
0854db2a 3498 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
25e94a99 3499 return;
96518518 3500err:
25e94a99 3501 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
96518518
PM
3502}
3503
ea078ae9
PS
3504static void audit_log_rule_reset(const struct nft_table *table,
3505 unsigned int base_seq,
3506 unsigned int nentries)
3507{
3508 char *buf = kasprintf(GFP_ATOMIC, "%s:%u",
3509 table->name, base_seq);
3510
3511 audit_log_nfcfg(buf, table->family, nentries,
3512 AUDIT_NFT_OP_RULE_RESET, GFP_ATOMIC);
3513 kfree(buf);
3514}
3515
6e1f760e 3516struct nft_rule_dump_ctx {
8194d599 3517 unsigned int s_idx;
e46abbcc 3518 char *table;
b7263e07 3519 char *chain;
405c8fd6 3520 bool reset;
6e1f760e
PNA
3521};
3522
241faece
PS
3523static int __nf_tables_dump_rules(struct sk_buff *skb,
3524 unsigned int *idx,
3525 struct netlink_callback *cb,
3526 const struct nft_table *table,
405c8fd6 3527 const struct nft_chain *chain)
241faece 3528{
99ab9f84 3529 struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;
241faece 3530 struct net *net = sock_net(skb->sk);
2c82c7e7 3531 const struct nft_rule *rule, *prule;
7fb818f2
PS
3532 unsigned int entries = 0;
3533 int ret = 0;
e189ae16 3534 u64 handle;
241faece 3535
2c82c7e7 3536 prule = NULL;
241faece
PS
3537 list_for_each_entry_rcu(rule, &chain->rules, list) {
3538 if (!nft_is_active(net, rule))
2c82c7e7 3539 goto cont_skip;
8194d599 3540 if (*idx < ctx->s_idx)
241faece 3541 goto cont;
e189ae16
PNA
3542 if (prule)
3543 handle = prule->handle;
3544 else
3545 handle = 0;
3546
241faece
PS
3547 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
3548 cb->nlh->nlmsg_seq,
3549 NFT_MSG_NEWRULE,
3550 NLM_F_MULTI | NLM_F_APPEND,
3551 table->family,
405c8fd6 3552 table, chain, rule, handle, ctx->reset) < 0) {
7fb818f2
PS
3553 ret = 1;
3554 break;
3555 }
3556 entries++;
241faece
PS
3557 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3558cont:
2c82c7e7
FW
3559 prule = rule;
3560cont_skip:
241faece
PS
3561 (*idx)++;
3562 }
9b5ba5c9 3563
405c8fd6 3564 if (ctx->reset && entries)
7fb818f2 3565 audit_log_rule_reset(table, cb->seq, entries);
9b5ba5c9 3566
7fb818f2 3567 return ret;
241faece
PS
3568}
3569
96518518
PM
3570static int nf_tables_dump_rules(struct sk_buff *skb,
3571 struct netlink_callback *cb)
3572{
3573 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
99ab9f84 3574 struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;
241faece 3575 struct nft_table *table;
96518518 3576 const struct nft_chain *chain;
241faece 3577 unsigned int idx = 0;
99633ab2 3578 struct net *net = sock_net(skb->sk);
96518518 3579 int family = nfmsg->nfgen_family;
0854db2a 3580 struct nftables_pernet *nft_net;
96518518 3581
e688a7f8 3582 rcu_read_lock();
d59d2f82 3583 nft_net = nft_pernet(net);
34002783 3584 cb->seq = READ_ONCE(nft_net->base_seq);
38e029f1 3585
0854db2a 3586 list_for_each_entry_rcu(table, &nft_net->tables, list) {
98319cb9 3587 if (family != NFPROTO_UNSPEC && family != table->family)
36596dad
PNA
3588 continue;
3589
afed2b54 3590 if (ctx->table && strcmp(ctx->table, table->name) != 0)
96518518
PM
3591 continue;
3592
afed2b54 3593 if (ctx->table && ctx->chain) {
241faece 3594 struct rhlist_head *list, *tmp;
6e1f760e 3595
241faece
PS
3596 list = rhltable_lookup(&table->chains_ht, ctx->chain,
3597 nft_chain_ht_params);
3598 if (!list)
3599 goto done;
3600
3601 rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
3602 if (!nft_is_active(net, chain))
3603 continue;
3604 __nf_tables_dump_rules(skb, &idx,
405c8fd6 3605 cb, table, chain);
241faece 3606 break;
96518518 3607 }
241faece 3608 goto done;
96518518 3609 }
241faece
PS
3610
3611 list_for_each_entry_rcu(chain, &table->chains, list) {
8daa8fde 3612 if (__nf_tables_dump_rules(skb, &idx,
405c8fd6 3613 cb, table, chain))
241faece
PS
3614 goto done;
3615 }
3616
afed2b54 3617 if (ctx->table)
241faece 3618 break;
96518518
PM
3619 }
3620done:
e688a7f8 3621 rcu_read_unlock();
310529e6 3622
8194d599 3623 ctx->s_idx = idx;
96518518
PM
3624 return skb->len;
3625}
3626
3cb03edb
PS
3627static int nf_tables_dumpreset_rules(struct sk_buff *skb,
3628 struct netlink_callback *cb)
3629{
3630 struct nftables_pernet *nft_net = nft_pernet(sock_net(skb->sk));
3631 int ret;
3632
3633 /* Mutex is held is to prevent that two concurrent dump-and-reset calls
3634 * do not underrun counters and quotas. The commit_mutex is used for
3635 * the lack a better lock, this is not transaction path.
3636 */
3637 mutex_lock(&nft_net->commit_mutex);
3638 ret = nf_tables_dump_rules(skb, cb);
3639 mutex_unlock(&nft_net->commit_mutex);
3640
3641 return ret;
3642}
3643
90fd131a
FW
3644static int nf_tables_dump_rules_start(struct netlink_callback *cb)
3645{
99ab9f84 3646 struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;
90fd131a 3647 const struct nlattr * const *nla = cb->data;
90fd131a 3648
99ab9f84 3649 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
90fd131a 3650
afed2b54
PS
3651 if (nla[NFTA_RULE_TABLE]) {
3652 ctx->table = nla_strdup(nla[NFTA_RULE_TABLE], GFP_ATOMIC);
99ab9f84 3653 if (!ctx->table)
afed2b54 3654 return -ENOMEM;
afed2b54
PS
3655 }
3656 if (nla[NFTA_RULE_CHAIN]) {
3657 ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN], GFP_ATOMIC);
3658 if (!ctx->chain) {
3659 kfree(ctx->table);
afed2b54 3660 return -ENOMEM;
90fd131a
FW
3661 }
3662 }
90fd131a
FW
3663 return 0;
3664}
3665
3cb03edb
PS
3666static int nf_tables_dumpreset_rules_start(struct netlink_callback *cb)
3667{
3668 struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;
3669
3670 ctx->reset = true;
3671
3672 return nf_tables_dump_rules_start(cb);
3673}
3674
6e1f760e
PNA
3675static int nf_tables_dump_rules_done(struct netlink_callback *cb)
3676{
99ab9f84 3677 struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;
e46abbcc 3678
afed2b54
PS
3679 kfree(ctx->table);
3680 kfree(ctx->chain);
6e1f760e
PNA
3681 return 0;
3682}
3683
d9adf22a 3684/* called with rcu_read_lock held */
1578c328
PS
3685static struct sk_buff *
3686nf_tables_getrule_single(u32 portid, const struct nfnl_info *info,
3687 const struct nlattr * const nla[], bool reset)
96518518 3688{
797d4980
PNA
3689 struct netlink_ext_ack *extack = info->extack;
3690 u8 genmask = nft_genmask_cur(info->net);
ef4b65e5 3691 u8 family = info->nfmsg->nfgen_family;
96518518
PM
3692 const struct nft_chain *chain;
3693 const struct nft_rule *rule;
797d4980 3694 struct net *net = info->net;
1b2470e5 3695 struct nft_table *table;
96518518 3696 struct sk_buff *skb2;
96518518
PM
3697 int err;
3698
6001a930 3699 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, 0);
36dd1bcc
PNA
3700 if (IS_ERR(table)) {
3701 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
1578c328 3702 return ERR_CAST(table);
36dd1bcc 3703 }
96518518 3704
f102d66b 3705 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask);
36dd1bcc
PNA
3706 if (IS_ERR(chain)) {
3707 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
1578c328 3708 return ERR_CAST(chain);
36dd1bcc 3709 }
96518518 3710
cac20fcd 3711 rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
36dd1bcc
PNA
3712 if (IS_ERR(rule)) {
3713 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
1578c328 3714 return ERR_CAST(rule);
36dd1bcc 3715 }
96518518 3716
d9adf22a 3717 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
96518518 3718 if (!skb2)
1578c328 3719 return ERR_PTR(-ENOMEM);
8daa8fde 3720
88773930 3721 err = nf_tables_fill_rule_info(skb2, net, portid,
797d4980 3722 info->nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
8daa8fde 3723 family, table, chain, rule, 0, reset);
1578c328
PS
3724 if (err < 0) {
3725 kfree_skb(skb2);
3726 return ERR_PTR(err);
3727 }
3728
3729 return skb2;
3730}
3731
3732static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info,
3733 const struct nlattr * const nla[])
3734{
1578c328
PS
3735 u32 portid = NETLINK_CB(skb).portid;
3736 struct net *net = info->net;
3737 struct sk_buff *skb2;
1578c328
PS
3738
3739 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
3740 struct netlink_dump_control c = {
3741 .start= nf_tables_dump_rules_start,
3742 .dump = nf_tables_dump_rules,
3743 .done = nf_tables_dump_rules_done,
3744 .module = THIS_MODULE,
3745 .data = (void *)nla,
3746 };
3747
3748 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
3749 }
3750
3cb03edb 3751 skb2 = nf_tables_getrule_single(portid, info, nla, false);
1578c328
PS
3752 if (IS_ERR(skb2))
3753 return PTR_ERR(skb2);
96518518 3754
3cb03edb
PS
3755 return nfnetlink_unicast(skb2, net, portid);
3756}
3757
3758static int nf_tables_getrule_reset(struct sk_buff *skb,
3759 const struct nfnl_info *info,
3760 const struct nlattr * const nla[])
3761{
3762 struct nftables_pernet *nft_net = nft_pernet(info->net);
3763 u32 portid = NETLINK_CB(skb).portid;
3764 struct net *net = info->net;
3765 struct sk_buff *skb2;
3766 char *buf;
3767
3768 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
3769 struct netlink_dump_control c = {
3770 .start= nf_tables_dumpreset_rules_start,
3771 .dump = nf_tables_dumpreset_rules,
3772 .done = nf_tables_dump_rules_done,
3773 .module = THIS_MODULE,
3774 .data = (void *)nla,
3775 };
3776
3777 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
3778 }
3779
3780 if (!try_module_get(THIS_MODULE))
3781 return -EINVAL;
3782 rcu_read_unlock();
3783 mutex_lock(&nft_net->commit_mutex);
3784 skb2 = nf_tables_getrule_single(portid, info, nla, true);
3785 mutex_unlock(&nft_net->commit_mutex);
3786 rcu_read_lock();
3787 module_put(THIS_MODULE);
3788
3789 if (IS_ERR(skb2))
3790 return PTR_ERR(skb2);
ea078ae9 3791
88773930
PS
3792 buf = kasprintf(GFP_ATOMIC, "%.*s:%u",
3793 nla_len(nla[NFTA_RULE_TABLE]),
3794 (char *)nla_data(nla[NFTA_RULE_TABLE]),
3795 nft_net->base_seq);
3796 audit_log_nfcfg(buf, info->nfmsg->nfgen_family, 1,
3797 AUDIT_NFT_OP_RULE_RESET, GFP_ATOMIC);
3798 kfree(buf);
3799
3800 return nfnetlink_unicast(skb2, net, portid);
96518518
PM
3801}
3802
4bedf9ee 3803void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule)
96518518 3804{
29e38801 3805 struct nft_expr *expr, *next;
96518518
PM
3806
3807 /*
3808 * Careful: some expressions might not be initialized in case this
3809 * is called on error from nf_tables_newrule().
3810 */
3811 expr = nft_expr_first(rule);
31cc578a 3812 while (nft_expr_more(rule, expr)) {
29e38801 3813 next = nft_expr_next(expr);
62472bce 3814 nf_tables_expr_destroy(ctx, expr);
29e38801 3815 expr = next;
96518518
PM
3816 }
3817 kfree(rule);
3818}
3819
4bedf9ee 3820static void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule)
bb7b40ae 3821{
f6ac8585 3822 nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE);
bb7b40ae
PNA
3823 nf_tables_rule_destroy(ctx, rule);
3824}
3825
a654de8f
PNA
3826int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain)
3827{
3828 struct nft_expr *expr, *last;
3829 const struct nft_data *data;
3830 struct nft_rule *rule;
3831 int err;
3832
26b2f552
TY
3833 if (ctx->level == NFT_JUMP_STACK_SIZE)
3834 return -EMLINK;
3835
a654de8f 3836 list_for_each_entry(rule, &chain->rules, list) {
169384fb
FW
3837 if (fatal_signal_pending(current))
3838 return -EINTR;
3839
a654de8f
PNA
3840 if (!nft_is_active_next(ctx->net, rule))
3841 continue;
3842
3843 nft_rule_for_each_expr(expr, last, rule) {
3844 if (!expr->ops->validate)
3845 continue;
3846
3847 err = expr->ops->validate(ctx, expr, &data);
3848 if (err < 0)
3849 return err;
3850 }
3851 }
3852
3853 return 0;
3854}
3855EXPORT_SYMBOL_GPL(nft_chain_validate);
3856
3857static int nft_table_validate(struct net *net, const struct nft_table *table)
3858{
3859 struct nft_chain *chain;
3860 struct nft_ctx ctx = {
3861 .net = net,
3862 .family = table->family,
3863 };
3864 int err;
3865
3866 list_for_each_entry(chain, &table->chains, list) {
3867 if (!nft_is_base_chain(chain))
3868 continue;
3869
3870 ctx.chain = chain;
3871 err = nft_chain_validate(&ctx, chain);
3872 if (err < 0)
3873 return err;
314c8284
FW
3874
3875 cond_resched();
a654de8f
PNA
3876 }
3877
3878 return 0;
3879}
3880
d46fc894
PNA
3881int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,
3882 const struct nft_set_iter *iter,
0e1ea651 3883 struct nft_elem_priv *elem_priv)
d46fc894 3884{
0e1ea651 3885 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
d46fc894
PNA
3886 struct nft_ctx *pctx = (struct nft_ctx *)ctx;
3887 const struct nft_data *data;
3888 int err;
3889
e79b47a8
PNA
3890 if (!nft_set_elem_active(ext, iter->genmask))
3891 return 0;
3892
d46fc894
PNA
3893 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
3894 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
3895 return 0;
3896
3897 data = nft_set_ext_data(ext);
3898 switch (data->verdict.code) {
3899 case NFT_JUMP:
3900 case NFT_GOTO:
3901 pctx->level++;
3902 err = nft_chain_validate(ctx, data->verdict.chain);
3903 if (err < 0)
3904 return err;
3905 pctx->level--;
3906 break;
3907 default:
3908 break;
3909 }
3910
3911 return 0;
3912}
3913
d46fc894
PNA
3914int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set)
3915{
e79b47a8
PNA
3916 struct nft_set_iter dummy_iter = {
3917 .genmask = nft_genmask_next(ctx->net),
3918 };
d46fc894 3919 struct nft_set_elem_catchall *catchall;
e79b47a8 3920
d46fc894
PNA
3921 struct nft_set_ext *ext;
3922 int ret = 0;
3923
3924 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
3925 ext = nft_set_elem_ext(set, catchall->elem);
e79b47a8 3926 if (!nft_set_elem_active(ext, dummy_iter.genmask))
d46fc894
PNA
3927 continue;
3928
e79b47a8 3929 ret = nft_setelem_validate(ctx, set, &dummy_iter, catchall->elem);
d46fc894
PNA
3930 if (ret < 0)
3931 return ret;
3932 }
3933
3934 return ret;
3935}
3936
75dd48e2 3937static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
36d5b291 3938 const struct nft_chain *chain,
75dd48e2
PS
3939 const struct nlattr *nla);
3940
1081d11b
PNA
3941#define NFT_RULE_MAXEXPRS 128
3942
7dab8ee3
PNA
3943static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info,
3944 const struct nlattr * const nla[])
96518518 3945{
7dab8ee3 3946 struct nftables_pernet *nft_net = nft_pernet(info->net);
7dab8ee3
PNA
3947 struct netlink_ext_ack *extack = info->extack;
3948 unsigned int size, i, n, ulen = 0, usize = 0;
3949 u8 genmask = nft_genmask_next(info->net);
3950 struct nft_rule *rule, *old_rule = NULL;
3951 struct nft_expr_info *expr_info = NULL;
ef4b65e5 3952 u8 family = info->nfmsg->nfgen_family;
3c5e4462 3953 struct nft_flow_rule *flow = NULL;
7dab8ee3 3954 struct net *net = info->net;
7dab8ee3 3955 struct nft_userdata *udata;
96518518
PM
3956 struct nft_table *table;
3957 struct nft_chain *chain;
7dab8ee3
PNA
3958 struct nft_trans *trans;
3959 u64 handle, pos_handle;
96518518
PM
3960 struct nft_expr *expr;
3961 struct nft_ctx ctx;
3962 struct nlattr *tmp;
96518518 3963 int err, rem;
96518518 3964
0854db2a 3965 lockdep_assert_held(&nft_net->commit_mutex);
f102d66b 3966
6001a930
PNA
3967 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask,
3968 NETLINK_CB(skb).portid);
36dd1bcc
PNA
3969 if (IS_ERR(table)) {
3970 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
96518518 3971 return PTR_ERR(table);
36dd1bcc 3972 }
96518518 3973
837830a4
PNA
3974 if (nla[NFTA_RULE_CHAIN]) {
3975 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
3976 genmask);
3977 if (IS_ERR(chain)) {
3978 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
3979 return PTR_ERR(chain);
3980 }
d0e2c7de 3981
837830a4 3982 } else if (nla[NFTA_RULE_CHAIN_ID]) {
515ad530
TLSC
3983 chain = nft_chain_lookup_byid(net, table, nla[NFTA_RULE_CHAIN_ID],
3984 genmask);
837830a4
PNA
3985 if (IS_ERR(chain)) {
3986 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN_ID]);
3987 return PTR_ERR(chain);
3988 }
3989 } else {
3990 return -EINVAL;
36dd1bcc 3991 }
96518518 3992
0ebc1064
PNA
3993 if (nft_chain_is_bound(chain))
3994 return -EOPNOTSUPP;
3995
96518518
PM
3996 if (nla[NFTA_RULE_HANDLE]) {
3997 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
cac20fcd 3998 rule = __nft_rule_lookup(chain, handle);
36dd1bcc
PNA
3999 if (IS_ERR(rule)) {
4000 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
96518518 4001 return PTR_ERR(rule);
36dd1bcc 4002 }
96518518 4003
7dab8ee3 4004 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
36dd1bcc 4005 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
96518518 4006 return -EEXIST;
36dd1bcc 4007 }
7dab8ee3 4008 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
96518518
PM
4009 old_rule = rule;
4010 else
4011 return -EOPNOTSUPP;
4012 } else {
7dab8ee3
PNA
4013 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE) ||
4014 info->nlh->nlmsg_flags & NLM_F_REPLACE)
96518518
PM
4015 return -EINVAL;
4016 handle = nf_tables_alloc_handle(table);
a0a7379e 4017
447750f2
FW
4018 if (nla[NFTA_RULE_POSITION]) {
4019 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
4020 old_rule = __nft_rule_lookup(chain, pos_handle);
4021 if (IS_ERR(old_rule)) {
4022 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]);
4023 return PTR_ERR(old_rule);
4024 }
75dd48e2 4025 } else if (nla[NFTA_RULE_POSITION_ID]) {
36d5b291 4026 old_rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_POSITION_ID]);
75dd48e2
PS
4027 if (IS_ERR(old_rule)) {
4028 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]);
4029 return PTR_ERR(old_rule);
4030 }
36dd1bcc 4031 }
5e948466
EL
4032 }
4033
7dab8ee3 4034 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
0ca743a5 4035
96518518
PM
4036 n = 0;
4037 size = 0;
4038 if (nla[NFTA_RULE_EXPRESSIONS]) {
7dab8ee3
PNA
4039 expr_info = kvmalloc_array(NFT_RULE_MAXEXPRS,
4040 sizeof(struct nft_expr_info),
4041 GFP_KERNEL);
4042 if (!expr_info)
2a43ecf9
FW
4043 return -ENOMEM;
4044
96518518
PM
4045 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
4046 err = -EINVAL;
4047 if (nla_type(tmp) != NFTA_LIST_ELEM)
3c5e4462 4048 goto err_release_expr;
96518518 4049 if (n == NFT_RULE_MAXEXPRS)
3c5e4462 4050 goto err_release_expr;
7dab8ee3 4051 err = nf_tables_expr_parse(&ctx, tmp, &expr_info[n]);
c781471d
PNA
4052 if (err < 0) {
4053 NL_SET_BAD_ATTR(extack, tmp);
3c5e4462 4054 goto err_release_expr;
c781471d 4055 }
7dab8ee3 4056 size += expr_info[n].ops->size;
96518518
PM
4057 n++;
4058 }
4059 }
9889840f
PM
4060 /* Check for overflow of dlen field */
4061 err = -EFBIG;
4062 if (size >= 1 << 12)
3c5e4462 4063 goto err_release_expr;
96518518 4064
86f1ec32 4065 if (nla[NFTA_RULE_USERDATA]) {
0768b3b3 4066 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
86f1ec32
PM
4067 if (ulen > 0)
4068 usize = sizeof(struct nft_userdata) + ulen;
4069 }
0768b3b3 4070
96518518 4071 err = -ENOMEM;
33758c89 4072 rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL_ACCOUNT);
96518518 4073 if (rule == NULL)
3c5e4462 4074 goto err_release_expr;
96518518 4075
889f7ee7 4076 nft_activate_next(net, rule);
0628b123 4077
96518518
PM
4078 rule->handle = handle;
4079 rule->dlen = size;
86f1ec32 4080 rule->udata = ulen ? 1 : 0;
0768b3b3 4081
86f1ec32
PM
4082 if (ulen) {
4083 udata = nft_userdata(rule);
4084 udata->len = ulen - 1;
4085 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
4086 }
96518518 4087
96518518
PM
4088 expr = nft_expr_first(rule);
4089 for (i = 0; i < n; i++) {
7dab8ee3 4090 err = nf_tables_newexpr(&ctx, &expr_info[i], expr);
83d9dcba 4091 if (err < 0) {
7dab8ee3 4092 NL_SET_BAD_ATTR(extack, expr_info[i].attr);
3c5e4462 4093 goto err_release_rule;
83d9dcba 4094 }
a654de8f 4095
7dab8ee3 4096 if (expr_info[i].ops->validate)
00c320f9 4097 nft_validate_state_update(table, NFT_VALIDATE_NEED);
a654de8f 4098
7dab8ee3 4099 expr_info[i].ops = NULL;
96518518
PM
4100 expr = nft_expr_next(expr);
4101 }
4102
3c5e4462
PNA
4103 if (chain->flags & NFT_CHAIN_HW_OFFLOAD) {
4104 flow = nft_flow_rule_create(net, rule);
4105 if (IS_ERR(flow)) {
4106 err = PTR_ERR(flow);
4107 goto err_release_rule;
4108 }
4109 }
4110
1689f259
PNA
4111 if (!nft_use_inc(&chain->use)) {
4112 err = -EMFILE;
4113 goto err_release_rule;
4114 }
4115
7dab8ee3 4116 if (info->nlh->nlmsg_flags & NLM_F_REPLACE) {
f15f29fd
PNA
4117 if (nft_chain_binding(chain)) {
4118 err = -EOPNOTSUPP;
4119 goto err_destroy_flow_rule;
4120 }
4121
2c964c55
PNA
4122 err = nft_delrule(&ctx, old_rule);
4123 if (err < 0)
4124 goto err_destroy_flow_rule;
4125
ca089878 4126 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
569ccae6
FW
4127 if (trans == NULL) {
4128 err = -ENOMEM;
3c5e4462 4129 goto err_destroy_flow_rule;
569ccae6 4130 }
569ccae6
FW
4131 list_add_tail_rcu(&rule->list, &old_rule->list);
4132 } else {
c9626a2c
PNA
4133 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
4134 if (!trans) {
569ccae6 4135 err = -ENOMEM;
3c5e4462 4136 goto err_destroy_flow_rule;
569ccae6
FW
4137 }
4138
7dab8ee3 4139 if (info->nlh->nlmsg_flags & NLM_F_APPEND) {
569ccae6
FW
4140 if (old_rule)
4141 list_add_rcu(&rule->list, &old_rule->list);
4142 else
4143 list_add_tail_rcu(&rule->list, &chain->rules);
4144 } else {
4145 if (old_rule)
4146 list_add_tail_rcu(&rule->list, &old_rule->list);
4147 else
4148 list_add_rcu(&rule->list, &chain->rules);
4149 }
0628b123 4150 }
7dab8ee3 4151 kvfree(expr_info);
96518518 4152
3c5e4462
PNA
4153 if (flow)
4154 nft_trans_flow_rule(trans) = flow;
4155
00c320f9 4156 if (table->validate_state == NFT_VALIDATE_DO)
a654de8f
PNA
4157 return nft_table_validate(net, table);
4158
4159 return 0;
3c5e4462
PNA
4160
4161err_destroy_flow_rule:
1689f259 4162 nft_use_dec_restore(&chain->use);
4ca041f9
CIK
4163 if (flow)
4164 nft_flow_rule_destroy(flow);
3c5e4462 4165err_release_rule:
26b5a571 4166 nft_rule_expr_deactivate(&ctx, rule, NFT_TRANS_PREPARE_ERROR);
1240eb93 4167 nf_tables_rule_destroy(&ctx, rule);
3c5e4462 4168err_release_expr:
96518518 4169 for (i = 0; i < n; i++) {
7dab8ee3
PNA
4170 if (expr_info[i].ops) {
4171 module_put(expr_info[i].ops->type->owner);
4172 if (expr_info[i].ops->type->release_ops)
4173 expr_info[i].ops->type->release_ops(expr_info[i].ops);
b25a31bf 4174 }
96518518 4175 }
7dab8ee3
PNA
4176 kvfree(expr_info);
4177
96518518
PM
4178 return err;
4179}
4180
1a94e38d 4181static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
36d5b291 4182 const struct nft_chain *chain,
1a94e38d
PNA
4183 const struct nlattr *nla)
4184{
d59d2f82 4185 struct nftables_pernet *nft_net = nft_pernet(net);
1a94e38d
PNA
4186 u32 id = ntohl(nla_get_be32(nla));
4187 struct nft_trans *trans;
4188
0854db2a 4189 list_for_each_entry(trans, &nft_net->commit_list, list) {
1a94e38d 4190 if (trans->msg_type == NFT_MSG_NEWRULE &&
36d5b291 4191 trans->ctx.chain == chain &&
1a94e38d 4192 id == nft_trans_rule_id(trans))
e3c361b8 4193 return nft_trans_rule(trans);
1a94e38d
PNA
4194 }
4195 return ERR_PTR(-ENOENT);
4196}
4197
7dab8ee3
PNA
4198static int nf_tables_delrule(struct sk_buff *skb, const struct nfnl_info *info,
4199 const struct nlattr * const nla[])
96518518 4200{
7dab8ee3 4201 struct netlink_ext_ack *extack = info->extack;
7dab8ee3 4202 u8 genmask = nft_genmask_next(info->net);
ef4b65e5 4203 u8 family = info->nfmsg->nfgen_family;
cf9dc09d 4204 struct nft_chain *chain = NULL;
7dab8ee3
PNA
4205 struct net *net = info->net;
4206 struct nft_table *table;
cf9dc09d 4207 struct nft_rule *rule;
0628b123 4208 struct nft_ctx ctx;
ef4b65e5 4209 int err = 0;
96518518 4210
6001a930
PNA
4211 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask,
4212 NETLINK_CB(skb).portid);
36dd1bcc
PNA
4213 if (IS_ERR(table)) {
4214 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
96518518 4215 return PTR_ERR(table);
36dd1bcc 4216 }
96518518 4217
cf9dc09d 4218 if (nla[NFTA_RULE_CHAIN]) {
f102d66b
FW
4219 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
4220 genmask);
36dd1bcc 4221 if (IS_ERR(chain)) {
1fb7696a 4222 if (PTR_ERR(chain) == -ENOENT &&
f80a612d
FFM
4223 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE)
4224 return 0;
4225
36dd1bcc 4226 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
cf9dc09d 4227 return PTR_ERR(chain);
36dd1bcc 4228 }
f15f29fd 4229 if (nft_chain_binding(chain))
d0e2c7de 4230 return -EOPNOTSUPP;
cf9dc09d 4231 }
96518518 4232
7dab8ee3 4233 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
0628b123 4234
cf9dc09d
PNA
4235 if (chain) {
4236 if (nla[NFTA_RULE_HANDLE]) {
cac20fcd 4237 rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
36dd1bcc 4238 if (IS_ERR(rule)) {
f80a612d
FFM
4239 if (PTR_ERR(rule) == -ENOENT &&
4240 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE)
4241 return 0;
4242
36dd1bcc 4243 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
cf9dc09d 4244 return PTR_ERR(rule);
36dd1bcc 4245 }
96518518 4246
1a94e38d
PNA
4247 err = nft_delrule(&ctx, rule);
4248 } else if (nla[NFTA_RULE_ID]) {
36d5b291 4249 rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_ID]);
36dd1bcc
PNA
4250 if (IS_ERR(rule)) {
4251 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]);
1a94e38d 4252 return PTR_ERR(rule);
36dd1bcc 4253 }
1a94e38d 4254
5e266fe7 4255 err = nft_delrule(&ctx, rule);
cf9dc09d 4256 } else {
ce24b721 4257 err = nft_delrule_by_chain(&ctx);
cf9dc09d
PNA
4258 }
4259 } else {
4260 list_for_each_entry(chain, &table->chains, list) {
664b0f8c
PNA
4261 if (!nft_is_active_next(net, chain))
4262 continue;
f15f29fd 4263 if (nft_chain_binding(chain))
6eaf41e8 4264 continue;
664b0f8c 4265
cf9dc09d 4266 ctx.chain = chain;
ce24b721 4267 err = nft_delrule_by_chain(&ctx);
0628b123
PNA
4268 if (err < 0)
4269 break;
4270 }
4271 }
4272
4273 return err;
4274}
4275
20a69341
PM
4276/*
4277 * Sets
4278 */
e32a4dc6
FW
4279static const struct nft_set_type *nft_set_types[] = {
4280 &nft_set_hash_fast_type,
4281 &nft_set_hash_type,
4282 &nft_set_rhash_type,
4283 &nft_set_bitmap_type,
4284 &nft_set_rbtree_type,
e6abef61 4285#if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
7400b063
SB
4286 &nft_set_pipapo_avx2_type,
4287#endif
e32a4dc6
FW
4288 &nft_set_pipapo_type,
4289};
20a69341 4290
2b664957 4291#define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \
71cc0873
PS
4292 NFT_SET_TIMEOUT | NFT_SET_OBJECT | \
4293 NFT_SET_EVAL)
2b664957 4294
71cc0873 4295static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags)
2b664957 4296{
71cc0873 4297 return (flags & type->features) == (flags & NFT_SET_FEATURES);
2b664957
PNA
4298}
4299
c50b960c
PM
4300/*
4301 * Select a set implementation based on the data characteristics and the
4302 * given policy. The total memory use might not be known if no size is
4303 * given, in that case the amount of memory per element is used.
4304 */
4305static const struct nft_set_ops *
a128885a 4306nft_select_set_ops(const struct nft_ctx *ctx, u32 flags,
bed4a63e 4307 const struct nft_set_desc *desc)
20a69341 4308{
d59d2f82 4309 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
c50b960c
PM
4310 const struct nft_set_ops *ops, *bops;
4311 struct nft_set_estimate est, best;
2b664957 4312 const struct nft_set_type *type;
e32a4dc6 4313 int i;
20a69341 4314
0854db2a 4315 lockdep_assert_held(&nft_net->commit_mutex);
f102d66b 4316 lockdep_nfnl_nft_mutex_not_held();
e32a4dc6 4317
55af753c
PNA
4318 bops = NULL;
4319 best.size = ~0;
4320 best.lookup = ~0;
0b5a7874 4321 best.space = ~0;
c50b960c 4322
e32a4dc6
FW
4323 for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) {
4324 type = nft_set_types[i];
71cc0873 4325 ops = &type->ops;
2b664957 4326
71cc0873 4327 if (!nft_set_ops_candidate(type, flags))
20a69341 4328 continue;
2b664957 4329 if (!ops->estimate(desc, flags, &est))
c50b960c
PM
4330 continue;
4331
bed4a63e 4332 switch (desc->policy) {
c50b960c 4333 case NFT_SET_POL_PERFORMANCE:
55af753c 4334 if (est.lookup < best.lookup)
c50b960c 4335 break;
644e334e
PNA
4336 if (est.lookup == best.lookup &&
4337 est.space < best.space)
4338 break;
c50b960c
PM
4339 continue;
4340 case NFT_SET_POL_MEMORY:
0b5a7874
PNA
4341 if (!desc->size) {
4342 if (est.space < best.space)
4343 break;
4344 if (est.space == best.space &&
4345 est.lookup < best.lookup)
4346 break;
4f2921ca 4347 } else if (est.size < best.size || !bops) {
c50b960c 4348 break;
0b5a7874 4349 }
c50b960c
PM
4350 continue;
4351 default:
4352 break;
4353 }
4354
c50b960c
PM
4355 bops = ops;
4356 best = est;
20a69341
PM
4357 }
4358
c50b960c
PM
4359 if (bops != NULL)
4360 return bops;
4361
20a69341
PM
4362 return ERR_PTR(-EOPNOTSUPP);
4363}
4364
4365static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
b2fbd044
LZ
4366 [NFTA_SET_TABLE] = { .type = NLA_STRING,
4367 .len = NFT_TABLE_MAXNAMELEN - 1 },
a9bdd836 4368 [NFTA_SET_NAME] = { .type = NLA_STRING,
cb39ad8b 4369 .len = NFT_SET_MAXNAMELEN - 1 },
20a69341
PM
4370 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
4371 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
4372 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
4373 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
4374 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
c50b960c
PM
4375 [NFTA_SET_POLICY] = { .type = NLA_U32 },
4376 [NFTA_SET_DESC] = { .type = NLA_NESTED },
958bee14 4377 [NFTA_SET_ID] = { .type = NLA_U32 },
761da293
PM
4378 [NFTA_SET_TIMEOUT] = { .type = NLA_U64 },
4379 [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 },
e6d8ecac
CFG
4380 [NFTA_SET_USERDATA] = { .type = NLA_BINARY,
4381 .len = NFT_USERDATA_MAXLEN },
8aeff920 4382 [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 },
3ecbfd65 4383 [NFTA_SET_HANDLE] = { .type = NLA_U64 },
65038428 4384 [NFTA_SET_EXPR] = { .type = NLA_NESTED },
013714bf
PS
4385 [NFTA_SET_EXPRESSIONS] = NLA_POLICY_NESTED_ARRAY(nft_expr_policy),
4386};
4387
4388static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = {
4389 [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 },
c50b960c
PM
4390};
4391
4392static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
4393 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
013714bf 4394 [NFTA_SET_DESC_CONCAT] = NLA_POLICY_NESTED_ARRAY(nft_concat_policy),
20a69341
PM
4395};
4396
cac20fcd
PNA
4397static struct nft_set *nft_set_lookup(const struct nft_table *table,
4398 const struct nlattr *nla, u8 genmask)
20a69341
PM
4399{
4400 struct nft_set *set;
4401
4402 if (nla == NULL)
4403 return ERR_PTR(-EINVAL);
4404
d9adf22a 4405 list_for_each_entry_rcu(set, &table->sets, list) {
37a9cc52
PNA
4406 if (!nla_strcmp(nla, set->name) &&
4407 nft_active_genmask(set, genmask))
20a69341
PM
4408 return set;
4409 }
4410 return ERR_PTR(-ENOENT);
4411}
4412
cac20fcd
PNA
4413static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table,
4414 const struct nlattr *nla,
4415 u8 genmask)
3ecbfd65
HS
4416{
4417 struct nft_set *set;
4418
3ecbfd65
HS
4419 list_for_each_entry(set, &table->sets, list) {
4420 if (be64_to_cpu(nla_get_be64(nla)) == set->handle &&
4421 nft_active_genmask(set, genmask))
4422 return set;
4423 }
4424 return ERR_PTR(-ENOENT);
4425}
4426
cac20fcd 4427static struct nft_set *nft_set_lookup_byid(const struct net *net,
470ee20e 4428 const struct nft_table *table,
cac20fcd 4429 const struct nlattr *nla, u8 genmask)
958bee14 4430{
d59d2f82 4431 struct nftables_pernet *nft_net = nft_pernet(net);
958bee14 4432 u32 id = ntohl(nla_get_be32(nla));
d59d2f82 4433 struct nft_trans *trans;
958bee14 4434
0854db2a 4435 list_for_each_entry(trans, &nft_net->commit_list, list) {
9c7f96fd
AK
4436 if (trans->msg_type == NFT_MSG_NEWSET) {
4437 struct nft_set *set = nft_trans_set(trans);
37a9cc52 4438
9c7f96fd 4439 if (id == nft_trans_set_id(trans) &&
470ee20e 4440 set->table == table &&
9c7f96fd
AK
4441 nft_active_genmask(set, genmask))
4442 return set;
4443 }
958bee14
PNA
4444 }
4445 return ERR_PTR(-ENOENT);
4446}
c7a72e3f 4447
10659cba
PNA
4448struct nft_set *nft_set_lookup_global(const struct net *net,
4449 const struct nft_table *table,
4450 const struct nlattr *nla_set_name,
4451 const struct nlattr *nla_set_id,
4452 u8 genmask)
c7a72e3f
PNA
4453{
4454 struct nft_set *set;
4455
cac20fcd 4456 set = nft_set_lookup(table, nla_set_name, genmask);
c7a72e3f
PNA
4457 if (IS_ERR(set)) {
4458 if (!nla_set_id)
4459 return set;
4460
470ee20e 4461 set = nft_set_lookup_byid(net, table, nla_set_id, genmask);
c7a72e3f
PNA
4462 }
4463 return set;
4464}
10659cba 4465EXPORT_SYMBOL_GPL(nft_set_lookup_global);
958bee14 4466
20a69341
PM
4467static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
4468 const char *name)
4469{
4470 const struct nft_set *i;
4471 const char *p;
4472 unsigned long *inuse;
60eb1894 4473 unsigned int n = 0, min = 0;
20a69341 4474
38745490 4475 p = strchr(name, '%');
20a69341
PM
4476 if (p != NULL) {
4477 if (p[1] != 'd' || strchr(p + 2, '%'))
4478 return -EINVAL;
4479
b462579b
FW
4480 if (strnlen(name, NFT_SET_MAX_ANONLEN) >= NFT_SET_MAX_ANONLEN)
4481 return -EINVAL;
4482
20a69341
PM
4483 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
4484 if (inuse == NULL)
4485 return -ENOMEM;
60eb1894 4486cont:
20a69341 4487 list_for_each_entry(i, &ctx->table->sets, list) {
14662917
DB
4488 int tmp;
4489
271c5ca8 4490 if (!nft_is_active_next(ctx->net, i))
37a9cc52 4491 continue;
14662917 4492 if (!sscanf(i->name, name, &tmp))
20a69341 4493 continue;
60eb1894 4494 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
20a69341 4495 continue;
14662917 4496
60eb1894 4497 set_bit(tmp - min, inuse);
20a69341
PM
4498 }
4499
53b70287 4500 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
60eb1894
PM
4501 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
4502 min += BITS_PER_BYTE * PAGE_SIZE;
4503 memset(inuse, 0, PAGE_SIZE);
4504 goto cont;
4505 }
20a69341
PM
4506 free_page((unsigned long)inuse);
4507 }
4508
33758c89 4509 set->name = kasprintf(GFP_KERNEL_ACCOUNT, name, min + n);
38745490
PS
4510 if (!set->name)
4511 return -ENOMEM;
4512
20a69341 4513 list_for_each_entry(i, &ctx->table->sets, list) {
37a9cc52
PNA
4514 if (!nft_is_active_next(ctx->net, i))
4515 continue;
e63aaaa6
AY
4516 if (!strcmp(set->name, i->name)) {
4517 kfree(set->name);
7fb6f78d 4518 set->name = NULL;
20a69341 4519 return -ENFILE;
e63aaaa6 4520 }
20a69341
PM
4521 }
4522 return 0;
4523}
4524
917d80d3 4525int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result)
8e1102d5
FW
4526{
4527 u64 ms = be64_to_cpu(nla_get_be64(nla));
4528 u64 max = (u64)(~((u64)0));
4529
4530 max = div_u64(max, NSEC_PER_MSEC);
4531 if (ms >= max)
4532 return -ERANGE;
4533
4534 ms *= NSEC_PER_MSEC;
4535 *result = nsecs_to_jiffies64(ms);
4536 return 0;
4537}
4538
917d80d3 4539__be64 nf_jiffies64_to_msecs(u64 input)
8e1102d5 4540{
3b15d09f 4541 return cpu_to_be64(jiffies64_to_msecs(input));
8e1102d5
FW
4542}
4543
f3a2181e
SB
4544static int nf_tables_fill_set_concat(struct sk_buff *skb,
4545 const struct nft_set *set)
4546{
4547 struct nlattr *concat, *field;
4548 int i;
4549
4550 concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT);
4551 if (!concat)
4552 return -ENOMEM;
4553
4554 for (i = 0; i < set->field_count; i++) {
4555 field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
4556 if (!field)
4557 return -ENOMEM;
4558
4559 if (nla_put_be32(skb, NFTA_SET_FIELD_LEN,
4560 htonl(set->field_len[i])))
4561 return -ENOMEM;
4562
4563 nla_nest_end(skb, field);
4564 }
4565
4566 nla_nest_end(skb, concat);
4567
4568 return 0;
4569}
4570
20a69341
PM
4571static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
4572 const struct nft_set *set, u16 event, u16 flags)
4573{
123b9961
PNA
4574 u64 timeout = READ_ONCE(set->timeout);
4575 u32 gc_int = READ_ONCE(set->gc_int);
128ad332 4576 u32 portid = ctx->portid;
123b9961 4577 struct nlmsghdr *nlh;
65038428 4578 struct nlattr *nest;
128ad332 4579 u32 seq = ctx->seq;
48b0ae04 4580 int i;
20a69341 4581
dedb67c4 4582 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
19c28b13
PNA
4583 nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family,
4584 NFNETLINK_V0, nft_base_seq(ctx->net));
4585 if (!nlh)
20a69341
PM
4586 goto nla_put_failure;
4587
20a69341
PM
4588 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
4589 goto nla_put_failure;
4590 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
4591 goto nla_put_failure;
3ecbfd65
HS
4592 if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle),
4593 NFTA_SET_PAD))
4594 goto nla_put_failure;
28339b21
PNA
4595
4596 if (event == NFT_MSG_DELSET) {
4597 nlmsg_end(skb, nlh);
4598 return 0;
4599 }
4600
20a69341
PM
4601 if (set->flags != 0)
4602 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
4603 goto nla_put_failure;
4604
4605 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
4606 goto nla_put_failure;
4607 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
4608 goto nla_put_failure;
4609 if (set->flags & NFT_SET_MAP) {
4610 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
4611 goto nla_put_failure;
4612 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
4613 goto nla_put_failure;
4614 }
8aeff920
PNA
4615 if (set->flags & NFT_SET_OBJECT &&
4616 nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype)))
4617 goto nla_put_failure;
20a69341 4618
123b9961 4619 if (timeout &&
d3e2a111 4620 nla_put_be64(skb, NFTA_SET_TIMEOUT,
123b9961 4621 nf_jiffies64_to_msecs(timeout),
b46f6ded 4622 NFTA_SET_PAD))
761da293 4623 goto nla_put_failure;
123b9961
PNA
4624 if (gc_int &&
4625 nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(gc_int)))
761da293
PM
4626 goto nla_put_failure;
4627
9363dc4b
AB
4628 if (set->policy != NFT_SET_POL_PERFORMANCE) {
4629 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
4630 goto nla_put_failure;
4631 }
4632
6f03bf43
PNA
4633 if (set->udata &&
4634 nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata))
e6d8ecac
CFG
4635 goto nla_put_failure;
4636
65038428
PNA
4637 nest = nla_nest_start_noflag(skb, NFTA_SET_DESC);
4638 if (!nest)
c50b960c
PM
4639 goto nla_put_failure;
4640 if (set->size &&
4641 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
4642 goto nla_put_failure;
f3a2181e
SB
4643
4644 if (set->field_count > 1 &&
4645 nf_tables_fill_set_concat(skb, set))
4646 goto nla_put_failure;
4647
65038428
PNA
4648 nla_nest_end(skb, nest);
4649
8cfd9b0f 4650 if (set->num_exprs == 1) {
65038428 4651 nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR);
8daa8fde 4652 if (nf_tables_fill_expr_info(skb, set->exprs[0], false) < 0)
65038428
PNA
4653 goto nla_put_failure;
4654
4655 nla_nest_end(skb, nest);
48b0ae04
PNA
4656 } else if (set->num_exprs > 1) {
4657 nest = nla_nest_start_noflag(skb, NFTA_SET_EXPRESSIONS);
4658 if (nest == NULL)
65038428
PNA
4659 goto nla_put_failure;
4660
48b0ae04
PNA
4661 for (i = 0; i < set->num_exprs; i++) {
4662 if (nft_expr_dump(skb, NFTA_LIST_ELEM,
8daa8fde 4663 set->exprs[i], false) < 0)
48b0ae04
PNA
4664 goto nla_put_failure;
4665 }
65038428
PNA
4666 nla_nest_end(skb, nest);
4667 }
c50b960c 4668
053c095a
JB
4669 nlmsg_end(skb, nlh);
4670 return 0;
20a69341
PM
4671
4672nla_put_failure:
4673 nlmsg_trim(skb, nlh);
4674 return -1;
4675}
4676
25e94a99
PNA
4677static void nf_tables_set_notify(const struct nft_ctx *ctx,
4678 const struct nft_set *set, int event,
4679 gfp_t gfp_flags)
20a69341 4680{
d59d2f82 4681 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
128ad332 4682 u32 portid = ctx->portid;
6fb721cf
PNA
4683 struct sk_buff *skb;
4684 u16 flags = 0;
20a69341
PM
4685 int err;
4686
128ad332
PNA
4687 if (!ctx->report &&
4688 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
25e94a99 4689 return;
20a69341 4690
31f8441c 4691 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
20a69341
PM
4692 if (skb == NULL)
4693 goto err;
4694
6fb721cf
PNA
4695 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
4696 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
4697
4698 err = nf_tables_fill_set(skb, ctx, set, event, flags);
20a69341
PM
4699 if (err < 0) {
4700 kfree_skb(skb);
4701 goto err;
4702 }
4703
0854db2a 4704 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
25e94a99 4705 return;
20a69341 4706err:
25e94a99 4707 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
20a69341
PM
4708}
4709
5b96af77 4710static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
c9c8e485
PNA
4711{
4712 const struct nft_set *set;
4713 unsigned int idx, s_idx = cb->args[0];
c9c8e485
PNA
4714 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
4715 struct net *net = sock_net(skb->sk);
5b96af77 4716 struct nft_ctx *ctx = cb->data, ctx_set;
0854db2a 4717 struct nftables_pernet *nft_net;
c9c8e485
PNA
4718
4719 if (cb->args[1])
4720 return skb->len;
4721
e688a7f8 4722 rcu_read_lock();
d59d2f82 4723 nft_net = nft_pernet(net);
34002783 4724 cb->seq = READ_ONCE(nft_net->base_seq);
38e029f1 4725
0854db2a 4726 list_for_each_entry_rcu(table, &nft_net->tables, list) {
36596dad 4727 if (ctx->family != NFPROTO_UNSPEC &&
98319cb9 4728 ctx->family != table->family)
36596dad
PNA
4729 continue;
4730
4731 if (ctx->table && ctx->table != table)
5b96af77
PNA
4732 continue;
4733
36596dad
PNA
4734 if (cur_table) {
4735 if (cur_table != table)
c9c8e485
PNA
4736 continue;
4737
36596dad 4738 cur_table = NULL;
c9c8e485 4739 }
36596dad
PNA
4740 idx = 0;
4741 list_for_each_entry_rcu(set, &table->sets, list) {
4742 if (idx < s_idx)
4743 goto cont;
4744 if (!nft_is_active(net, set))
4745 goto cont;
5b96af77 4746
36596dad
PNA
4747 ctx_set = *ctx;
4748 ctx_set.table = table;
98319cb9 4749 ctx_set.family = table->family;
c9c8e485 4750
36596dad
PNA
4751 if (nf_tables_fill_set(skb, &ctx_set, set,
4752 NFT_MSG_NEWSET,
4753 NLM_F_MULTI) < 0) {
4754 cb->args[0] = idx;
4755 cb->args[2] = (unsigned long) table;
4756 goto done;
c9c8e485 4757 }
36596dad 4758 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
c9c8e485 4759cont:
36596dad 4760 idx++;
c9c8e485 4761 }
36596dad
PNA
4762 if (s_idx)
4763 s_idx = 0;
c9c8e485
PNA
4764 }
4765 cb->args[1] = 1;
4766done:
e688a7f8 4767 rcu_read_unlock();
c9c8e485
PNA
4768 return skb->len;
4769}
4770
90fd131a
FW
4771static int nf_tables_dump_sets_start(struct netlink_callback *cb)
4772{
4773 struct nft_ctx *ctx_dump = NULL;
4774
4775 ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC);
4776 if (ctx_dump == NULL)
4777 return -ENOMEM;
4778
4779 cb->data = ctx_dump;
4780 return 0;
4781}
4782
5b96af77 4783static int nf_tables_dump_sets_done(struct netlink_callback *cb)
20a69341 4784{
5b96af77
PNA
4785 kfree(cb->data);
4786 return 0;
20a69341
PM
4787}
4788
d9adf22a 4789/* called with rcu_read_lock held */
797d4980
PNA
4790static int nf_tables_getset(struct sk_buff *skb, const struct nfnl_info *info,
4791 const struct nlattr * const nla[])
20a69341 4792{
797d4980
PNA
4793 struct netlink_ext_ack *extack = info->extack;
4794 u8 genmask = nft_genmask_cur(info->net);
67086651
PNA
4795 u8 family = info->nfmsg->nfgen_family;
4796 struct nft_table *table = NULL;
797d4980 4797 struct net *net = info->net;
20a69341 4798 const struct nft_set *set;
20a69341 4799 struct sk_buff *skb2;
797d4980 4800 struct nft_ctx ctx;
20a69341
PM
4801 int err;
4802
67086651
PNA
4803 if (nla[NFTA_SET_TABLE]) {
4804 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family,
4805 genmask, 0);
4806 if (IS_ERR(table)) {
4807 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
4808 return PTR_ERR(table);
4809 }
4810 }
4811
4812 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
20a69341 4813
797d4980 4814 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
20a69341 4815 struct netlink_dump_control c = {
90fd131a 4816 .start = nf_tables_dump_sets_start,
20a69341 4817 .dump = nf_tables_dump_sets,
5b96af77 4818 .done = nf_tables_dump_sets_done,
90fd131a 4819 .data = &ctx,
d9adf22a 4820 .module = THIS_MODULE,
20a69341 4821 };
5b96af77 4822
797d4980 4823 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
20a69341
PM
4824 }
4825
c9c8e485 4826 /* Only accept unspec with dump */
ef4b65e5 4827 if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC)
c9c8e485 4828 return -EAFNOSUPPORT;
eaa2bcd6
PT
4829 if (!nla[NFTA_SET_TABLE])
4830 return -EINVAL;
c9c8e485 4831
67086651 4832 set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask);
aee1f692
PNA
4833 if (IS_ERR(set)) {
4834 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
20a69341 4835 return PTR_ERR(set);
aee1f692 4836 }
20a69341 4837
d9adf22a 4838 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
20a69341
PM
4839 if (skb2 == NULL)
4840 return -ENOMEM;
4841
4842 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
4843 if (err < 0)
ee921183 4844 goto err_fill_set_info;
20a69341 4845
ee921183 4846 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
20a69341 4847
ee921183 4848err_fill_set_info:
20a69341
PM
4849 kfree_skb(skb2);
4850 return err;
4851}
4852
f3a2181e
SB
4853static int nft_set_desc_concat_parse(const struct nlattr *attr,
4854 struct nft_set_desc *desc)
4855{
4856 struct nlattr *tb[NFTA_SET_FIELD_MAX + 1];
4857 u32 len;
4858 int err;
4859
fecf31ee
PNA
4860 if (desc->field_count >= ARRAY_SIZE(desc->field_len))
4861 return -E2BIG;
4862
f3a2181e
SB
4863 err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr,
4864 nft_concat_policy, NULL);
4865 if (err < 0)
4866 return err;
4867
4868 if (!tb[NFTA_SET_FIELD_LEN])
4869 return -EINVAL;
4870
4871 len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN]));
fecf31ee
PNA
4872 if (!len || len > U8_MAX)
4873 return -EINVAL;
f3a2181e
SB
4874
4875 desc->field_len[desc->field_count++] = len;
4876
4877 return 0;
4878}
4879
4880static int nft_set_desc_concat(struct nft_set_desc *desc,
4881 const struct nlattr *nla)
4882{
3ce67e37 4883 u32 num_regs = 0, key_num_regs = 0;
f3a2181e 4884 struct nlattr *attr;
fecf31ee 4885 int rem, err, i;
f3a2181e
SB
4886
4887 nla_for_each_nested(attr, nla, rem) {
4888 if (nla_type(attr) != NFTA_LIST_ELEM)
4889 return -EINVAL;
4890
4891 err = nft_set_desc_concat_parse(attr, desc);
4892 if (err < 0)
4893 return err;
4894 }
4895
fecf31ee
PNA
4896 for (i = 0; i < desc->field_count; i++)
4897 num_regs += DIV_ROUND_UP(desc->field_len[i], sizeof(u32));
4898
3ce67e37
PNA
4899 key_num_regs = DIV_ROUND_UP(desc->klen, sizeof(u32));
4900 if (key_num_regs != num_regs)
4901 return -EINVAL;
4902
fecf31ee
PNA
4903 if (num_regs > NFT_REG32_COUNT)
4904 return -E2BIG;
4905
f3a2181e
SB
4906 return 0;
4907}
4908
f7e840ee 4909static int nf_tables_set_desc_parse(struct nft_set_desc *desc,
c50b960c
PM
4910 const struct nlattr *nla)
4911{
4912 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
4913 int err;
4914
8cb08174
JB
4915 err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla,
4916 nft_set_desc_policy, NULL);
c50b960c
PM
4917 if (err < 0)
4918 return err;
4919
4920 if (da[NFTA_SET_DESC_SIZE] != NULL)
4921 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
f3a2181e
SB
4922 if (da[NFTA_SET_DESC_CONCAT])
4923 err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]);
c50b960c 4924
f3a2181e 4925 return err;
c50b960c
PM
4926}
4927
a8fe4154
PNA
4928static int nft_set_expr_alloc(struct nft_ctx *ctx, struct nft_set *set,
4929 const struct nlattr * const *nla,
4930 struct nft_expr **exprs, int *num_exprs,
4931 u32 flags)
4932{
4933 struct nft_expr *expr;
4934 int err, i;
4935
4936 if (nla[NFTA_SET_EXPR]) {
4937 expr = nft_set_elem_expr_alloc(ctx, set, nla[NFTA_SET_EXPR]);
4938 if (IS_ERR(expr)) {
4939 err = PTR_ERR(expr);
4940 goto err_set_expr_alloc;
4941 }
4942 exprs[0] = expr;
4943 (*num_exprs)++;
4944 } else if (nla[NFTA_SET_EXPRESSIONS]) {
4945 struct nlattr *tmp;
4946 int left;
4947
4948 if (!(flags & NFT_SET_EXPR)) {
4949 err = -EINVAL;
4950 goto err_set_expr_alloc;
4951 }
4952 i = 0;
4953 nla_for_each_nested(tmp, nla[NFTA_SET_EXPRESSIONS], left) {
4954 if (i == NFT_SET_EXPR_MAX) {
4955 err = -E2BIG;
4956 goto err_set_expr_alloc;
4957 }
4958 if (nla_type(tmp) != NFTA_LIST_ELEM) {
4959 err = -EINVAL;
4960 goto err_set_expr_alloc;
4961 }
4962 expr = nft_set_elem_expr_alloc(ctx, set, tmp);
4963 if (IS_ERR(expr)) {
4964 err = PTR_ERR(expr);
4965 goto err_set_expr_alloc;
4966 }
4967 exprs[i++] = expr;
4968 (*num_exprs)++;
4969 }
4970 }
4971
4972 return 0;
4973
4974err_set_expr_alloc:
4975 for (i = 0; i < *num_exprs; i++)
4976 nft_expr_destroy(ctx, exprs[i]);
4977
4978 return err;
4979}
4980
f6594c37
PNA
4981static bool nft_set_is_same(const struct nft_set *set,
4982 const struct nft_set_desc *desc,
4983 struct nft_expr *exprs[], u32 num_exprs, u32 flags)
4984{
4985 int i;
4986
4987 if (set->ktype != desc->ktype ||
4988 set->dtype != desc->dtype ||
4989 set->flags != flags ||
4990 set->klen != desc->klen ||
4991 set->dlen != desc->dlen ||
4992 set->field_count != desc->field_count ||
4993 set->num_exprs != num_exprs)
4994 return false;
4995
4996 for (i = 0; i < desc->field_count; i++) {
4997 if (set->field_len[i] != desc->field_len[i])
4998 return false;
4999 }
5000
5001 for (i = 0; i < num_exprs; i++) {
5002 if (set->exprs[i]->ops != exprs[i]->ops)
5003 return false;
5004 }
5005
5006 return true;
5007}
5008
7dab8ee3
PNA
5009static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,
5010 const struct nlattr * const nla[])
20a69341 5011{
7dab8ee3
PNA
5012 struct netlink_ext_ack *extack = info->extack;
5013 u8 genmask = nft_genmask_next(info->net);
ef4b65e5 5014 u8 family = info->nfmsg->nfgen_family;
20a69341 5015 const struct nft_set_ops *ops;
7dab8ee3
PNA
5016 struct net *net = info->net;
5017 struct nft_set_desc desc;
20a69341 5018 struct nft_table *table;
7dab8ee3 5019 unsigned char *udata;
20a69341
PM
5020 struct nft_set *set;
5021 struct nft_ctx ctx;
6c8774a9 5022 size_t alloc_size;
a8fe4154 5023 int num_exprs = 0;
7dab8ee3
PNA
5024 char *name;
5025 int err, i;
e6d8ecac 5026 u16 udlen;
bed4a63e 5027 u32 flags;
7dab8ee3 5028 u64 size;
20a69341
PM
5029
5030 if (nla[NFTA_SET_TABLE] == NULL ||
5031 nla[NFTA_SET_NAME] == NULL ||
958bee14
PNA
5032 nla[NFTA_SET_KEY_LEN] == NULL ||
5033 nla[NFTA_SET_ID] == NULL)
20a69341
PM
5034 return -EINVAL;
5035
c50b960c
PM
5036 memset(&desc, 0, sizeof(desc));
5037
bed4a63e 5038 desc.ktype = NFT_DATA_VALUE;
20a69341 5039 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
bed4a63e
PNA
5040 desc.ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
5041 if ((desc.ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
20a69341
PM
5042 return -EINVAL;
5043 }
5044
c50b960c 5045 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
7d740264 5046 if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
20a69341
PM
5047 return -EINVAL;
5048
5049 flags = 0;
5050 if (nla[NFTA_SET_FLAGS] != NULL) {
5051 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
5052 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
7c6c6e95 5053 NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
8aeff920 5054 NFT_SET_MAP | NFT_SET_EVAL |
b4e70d8d 5055 NFT_SET_OBJECT | NFT_SET_CONCAT | NFT_SET_EXPR))
d9583cdf 5056 return -EOPNOTSUPP;
8aeff920 5057 /* Only one of these operations is supported */
acab7131
FW
5058 if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) ==
5059 (NFT_SET_MAP | NFT_SET_OBJECT))
5060 return -EOPNOTSUPP;
5061 if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) ==
5062 (NFT_SET_EVAL | NFT_SET_OBJECT))
7c6c6e95 5063 return -EOPNOTSUPP;
16603605
PNA
5064 if ((flags & (NFT_SET_ANONYMOUS | NFT_SET_TIMEOUT | NFT_SET_EVAL)) ==
5065 (NFT_SET_ANONYMOUS | NFT_SET_TIMEOUT))
5066 return -EOPNOTSUPP;
5f4fc4bd
PNA
5067 if ((flags & (NFT_SET_CONSTANT | NFT_SET_TIMEOUT)) ==
5068 (NFT_SET_CONSTANT | NFT_SET_TIMEOUT))
5069 return -EOPNOTSUPP;
20a69341
PM
5070 }
5071
bed4a63e 5072 desc.dtype = 0;
20a69341
PM
5073 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
5074 if (!(flags & NFT_SET_MAP))
5075 return -EINVAL;
5076
bed4a63e
PNA
5077 desc.dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
5078 if ((desc.dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
5079 desc.dtype != NFT_DATA_VERDICT)
20a69341
PM
5080 return -EINVAL;
5081
bed4a63e 5082 if (desc.dtype != NFT_DATA_VERDICT) {
20a69341
PM
5083 if (nla[NFTA_SET_DATA_LEN] == NULL)
5084 return -EINVAL;
c50b960c 5085 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
7d740264 5086 if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
20a69341
PM
5087 return -EINVAL;
5088 } else
7d740264 5089 desc.dlen = sizeof(struct nft_verdict);
20a69341
PM
5090 } else if (flags & NFT_SET_MAP)
5091 return -EINVAL;
5092
8aeff920
PNA
5093 if (nla[NFTA_SET_OBJ_TYPE] != NULL) {
5094 if (!(flags & NFT_SET_OBJECT))
5095 return -EINVAL;
5096
bed4a63e
PNA
5097 desc.objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE]));
5098 if (desc.objtype == NFT_OBJECT_UNSPEC ||
5099 desc.objtype > NFT_OBJECT_MAX)
d9583cdf 5100 return -EOPNOTSUPP;
8aeff920
PNA
5101 } else if (flags & NFT_SET_OBJECT)
5102 return -EINVAL;
5103 else
bed4a63e 5104 desc.objtype = NFT_OBJECT_UNSPEC;
8aeff920 5105
bed4a63e 5106 desc.timeout = 0;
761da293
PM
5107 if (nla[NFTA_SET_TIMEOUT] != NULL) {
5108 if (!(flags & NFT_SET_TIMEOUT))
5109 return -EINVAL;
8e1102d5 5110
e26d3009
PNA
5111 if (flags & NFT_SET_ANONYMOUS)
5112 return -EOPNOTSUPP;
5113
bed4a63e 5114 err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &desc.timeout);
8e1102d5
FW
5115 if (err)
5116 return err;
761da293 5117 }
bed4a63e 5118 desc.gc_int = 0;
761da293
PM
5119 if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
5120 if (!(flags & NFT_SET_TIMEOUT))
5121 return -EINVAL;
e26d3009
PNA
5122
5123 if (flags & NFT_SET_ANONYMOUS)
5124 return -EOPNOTSUPP;
5125
bed4a63e 5126 desc.gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
761da293
PM
5127 }
5128
bed4a63e 5129 desc.policy = NFT_SET_POL_PERFORMANCE;
0617c3de 5130 if (nla[NFTA_SET_POLICY] != NULL) {
bed4a63e 5131 desc.policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
0617c3de
PNA
5132 switch (desc.policy) {
5133 case NFT_SET_POL_PERFORMANCE:
5134 case NFT_SET_POL_MEMORY:
5135 break;
5136 default:
5137 return -EOPNOTSUPP;
5138 }
5139 }
c50b960c
PM
5140
5141 if (nla[NFTA_SET_DESC] != NULL) {
f7e840ee 5142 err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]);
c50b960c
PM
5143 if (err < 0)
5144 return err;
1b6345d4 5145
113661e0
PNA
5146 if (desc.field_count > 1) {
5147 if (!(flags & NFT_SET_CONCAT))
5148 return -EINVAL;
5149 } else if (flags & NFT_SET_CONCAT) {
1b6345d4 5150 return -EINVAL;
113661e0 5151 }
1b6345d4
PNA
5152 } else if (flags & NFT_SET_CONCAT) {
5153 return -EINVAL;
c50b960c
PM
5154 }
5155
48b0ae04 5156 if (nla[NFTA_SET_EXPR] || nla[NFTA_SET_EXPRESSIONS])
d56aab26
PNA
5157 desc.expr = true;
5158
6001a930
PNA
5159 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask,
5160 NETLINK_CB(skb).portid);
36dd1bcc
PNA
5161 if (IS_ERR(table)) {
5162 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
20a69341 5163 return PTR_ERR(table);
36dd1bcc 5164 }
20a69341 5165
7dab8ee3 5166 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
20a69341 5167
cac20fcd 5168 set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask);
20a69341 5169 if (IS_ERR(set)) {
36dd1bcc
PNA
5170 if (PTR_ERR(set) != -ENOENT) {
5171 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
20a69341 5172 return PTR_ERR(set);
36dd1bcc 5173 }
1a28ad74 5174 } else {
a8fe4154
PNA
5175 struct nft_expr *exprs[NFT_SET_EXPR_MAX] = {};
5176
7dab8ee3 5177 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
36dd1bcc 5178 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
20a69341 5179 return -EEXIST;
36dd1bcc 5180 }
7dab8ee3 5181 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
20a69341 5182 return -EOPNOTSUPP;
36dd1bcc 5183
b770283c
PNA
5184 if (nft_set_is_anonymous(set))
5185 return -EOPNOTSUPP;
5186
a8fe4154
PNA
5187 err = nft_set_expr_alloc(&ctx, set, nla, exprs, &num_exprs, flags);
5188 if (err < 0)
5189 return err;
5190
f6594c37
PNA
5191 err = 0;
5192 if (!nft_set_is_same(set, &desc, exprs, num_exprs, flags)) {
5193 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
5194 err = -EEXIST;
5195 }
5196
a8fe4154
PNA
5197 for (i = 0; i < num_exprs; i++)
5198 nft_expr_destroy(&ctx, exprs[i]);
5199
123b9961
PNA
5200 if (err < 0)
5201 return err;
5202
5203 return __nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set, &desc);
20a69341
PM
5204 }
5205
7dab8ee3 5206 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
20a69341
PM
5207 return -ENOENT;
5208
a128885a 5209 ops = nft_select_set_ops(&ctx, flags, &desc);
20a69341
PM
5210 if (IS_ERR(ops))
5211 return PTR_ERR(ops);
5212
e6d8ecac
CFG
5213 udlen = 0;
5214 if (nla[NFTA_SET_USERDATA])
5215 udlen = nla_len(nla[NFTA_SET_USERDATA]);
5216
20a69341
PM
5217 size = 0;
5218 if (ops->privsize != NULL)
347b408d 5219 size = ops->privsize(nla, &desc);
6c8774a9 5220 alloc_size = sizeof(*set) + size + udlen;
45928afe 5221 if (alloc_size < size || alloc_size > INT_MAX)
6c8774a9 5222 return -ENOMEM;
1689f259
PNA
5223
5224 if (!nft_use_inc(&table->use))
5225 return -EMFILE;
5226
33758c89 5227 set = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT);
1689f259
PNA
5228 if (!set) {
5229 err = -ENOMEM;
5230 goto err_alloc;
5231 }
20a69341 5232
33758c89 5233 name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL_ACCOUNT);
38745490
PS
5234 if (!name) {
5235 err = -ENOMEM;
65038428 5236 goto err_set_name;
38745490
PS
5237 }
5238
20a69341 5239 err = nf_tables_set_alloc_name(&ctx, set, name);
38745490 5240 kfree(name);
20a69341 5241 if (err < 0)
ad9f151e
PNA
5242 goto err_set_name;
5243
5244 udata = NULL;
5245 if (udlen) {
5246 udata = set->data + size;
5247 nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen);
5248 }
5249
5250 INIT_LIST_HEAD(&set->bindings);
5251 INIT_LIST_HEAD(&set->catchall_list);
5f68718b 5252 refcount_set(&set->refs, 1);
ad9f151e
PNA
5253 set->table = table;
5254 write_pnet(&set->net, net);
5255 set->ops = ops;
bed4a63e 5256 set->ktype = desc.ktype;
ad9f151e 5257 set->klen = desc.klen;
bed4a63e
PNA
5258 set->dtype = desc.dtype;
5259 set->objtype = desc.objtype;
ad9f151e
PNA
5260 set->dlen = desc.dlen;
5261 set->flags = flags;
5262 set->size = desc.size;
bed4a63e 5263 set->policy = desc.policy;
ad9f151e
PNA
5264 set->udlen = udlen;
5265 set->udata = udata;
bed4a63e
PNA
5266 set->timeout = desc.timeout;
5267 set->gc_int = desc.gc_int;
ad9f151e
PNA
5268
5269 set->field_count = desc.field_count;
5270 for (i = 0; i < desc.field_count; i++)
5271 set->field_len[i] = desc.field_len[i];
5272
5273 err = ops->init(set, &desc, nla);
5274 if (err < 0)
5275 goto err_set_init;
65038428 5276
a8fe4154
PNA
5277 err = nft_set_expr_alloc(&ctx, set, nla, set->exprs, &num_exprs, flags);
5278 if (err < 0)
5279 goto err_set_destroy;
20a69341 5280
a8fe4154 5281 set->num_exprs = num_exprs;
3ecbfd65 5282 set->handle = nf_tables_alloc_handle(table);
212ed75d 5283 INIT_LIST_HEAD(&set->pending_update);
20a69341 5284
958bee14 5285 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
20a69341 5286 if (err < 0)
ad9f151e 5287 goto err_set_expr_alloc;
20a69341 5288
e688a7f8 5289 list_add_tail_rcu(&set->list, &table->sets);
1689f259 5290
20a69341
PM
5291 return 0;
5292
ad9f151e 5293err_set_expr_alloc:
8cfd9b0f
PNA
5294 for (i = 0; i < set->num_exprs; i++)
5295 nft_expr_destroy(&ctx, set->exprs[i]);
a8fe4154 5296err_set_destroy:
628bd3e4 5297 ops->destroy(&ctx, set);
ad9f151e 5298err_set_init:
2f6adf48 5299 kfree(set->name);
65038428 5300err_set_name:
1ff75a3e 5301 kvfree(set);
1689f259
PNA
5302err_alloc:
5303 nft_use_dec_restore(&table->use);
5304
20a69341
PM
5305 return err;
5306}
5307
aaa31047
PNA
5308static void nft_set_catchall_destroy(const struct nft_ctx *ctx,
5309 struct nft_set *set)
5310{
0f7d9b31 5311 struct nft_set_elem_catchall *next, *catchall;
aaa31047 5312
0f7d9b31 5313 list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
aaa31047 5314 list_del_rcu(&catchall->list);
628bd3e4 5315 nf_tables_set_elem_destroy(ctx, set, catchall->elem);
ae089831 5316 kfree_rcu(catchall, rcu);
aaa31047
PNA
5317 }
5318}
5319
5f68718b
PNA
5320static void nft_set_put(struct nft_set *set)
5321{
5322 if (refcount_dec_and_test(&set->refs)) {
5323 kfree(set->name);
5324 kvfree(set);
5325 }
5326}
5327
0c2a85ed 5328static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
20a69341 5329{
8cfd9b0f
PNA
5330 int i;
5331
273fe3f1
PNA
5332 if (WARN_ON(set->use > 0))
5333 return;
5334
8cfd9b0f
PNA
5335 for (i = 0; i < set->num_exprs; i++)
5336 nft_expr_destroy(ctx, set->exprs[i]);
65038428 5337
628bd3e4 5338 set->ops->destroy(ctx, set);
aaa31047 5339 nft_set_catchall_destroy(ctx, set);
5f68718b 5340 nft_set_put(set);
20a69341
PM
5341}
5342
7dab8ee3
PNA
5343static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info,
5344 const struct nlattr * const nla[])
20a69341 5345{
7dab8ee3
PNA
5346 struct netlink_ext_ack *extack = info->extack;
5347 u8 genmask = nft_genmask_next(info->net);
67086651 5348 u8 family = info->nfmsg->nfgen_family;
7dab8ee3 5349 struct net *net = info->net;
36dd1bcc 5350 const struct nlattr *attr;
67086651 5351 struct nft_table *table;
20a69341
PM
5352 struct nft_set *set;
5353 struct nft_ctx ctx;
20a69341 5354
ef4b65e5 5355 if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC)
ec2c9935 5356 return -EAFNOSUPPORT;
20a69341 5357
67086651
PNA
5358 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family,
5359 genmask, NETLINK_CB(skb).portid);
5360 if (IS_ERR(table)) {
5361 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
5362 return PTR_ERR(table);
5363 }
20a69341 5364
36dd1bcc
PNA
5365 if (nla[NFTA_SET_HANDLE]) {
5366 attr = nla[NFTA_SET_HANDLE];
67086651 5367 set = nft_set_lookup_byhandle(table, attr, genmask);
36dd1bcc
PNA
5368 } else {
5369 attr = nla[NFTA_SET_NAME];
67086651 5370 set = nft_set_lookup(table, attr, genmask);
36dd1bcc 5371 }
a8278400 5372
36dd1bcc 5373 if (IS_ERR(set)) {
f80a612d
FFM
5374 if (PTR_ERR(set) == -ENOENT &&
5375 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSET)
5376 return 0;
5377
36dd1bcc
PNA
5378 NL_SET_BAD_ATTR(extack, attr);
5379 return PTR_ERR(set);
5380 }
273fe3f1 5381 if (set->use ||
7dab8ee3
PNA
5382 (info->nlh->nlmsg_flags & NLM_F_NONREC &&
5383 atomic_read(&set->nelems) > 0)) {
36dd1bcc 5384 NL_SET_BAD_ATTR(extack, attr);
20a69341 5385 return -EBUSY;
36dd1bcc 5386 }
20a69341 5387
67086651
PNA
5388 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
5389
ee01d542 5390 return nft_delset(&ctx, set);
20a69341
PM
5391}
5392
345023b0
PNA
5393static int nft_validate_register_store(const struct nft_ctx *ctx,
5394 enum nft_registers reg,
5395 const struct nft_data *data,
5396 enum nft_data_types type,
5397 unsigned int len);
5398
97c976d6
PNA
5399static int nft_setelem_data_validate(const struct nft_ctx *ctx,
5400 struct nft_set *set,
0e1ea651 5401 struct nft_elem_priv *elem_priv)
20a69341 5402{
0e1ea651 5403 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
20a69341
PM
5404 enum nft_registers dreg;
5405
5406 dreg = nft_type_to_reg(set->dtype);
1ec10212
PM
5407 return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
5408 set->dtype == NFT_DATA_VERDICT ?
5409 NFT_DATA_VERDICT : NFT_DATA_VALUE,
5410 set->dlen);
20a69341
PM
5411}
5412
97c976d6
PNA
5413static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
5414 struct nft_set *set,
5415 const struct nft_set_iter *iter,
0e1ea651 5416 struct nft_elem_priv *elem_priv)
97c976d6 5417{
e79b47a8
PNA
5418 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
5419
5420 if (!nft_set_elem_active(ext, iter->genmask))
5421 return 0;
5422
0e1ea651 5423 return nft_setelem_data_validate(ctx, set, elem_priv);
97c976d6
PNA
5424}
5425
aaa31047
PNA
5426static int nft_set_catchall_bind_check(const struct nft_ctx *ctx,
5427 struct nft_set *set)
5428{
5429 u8 genmask = nft_genmask_next(ctx->net);
5430 struct nft_set_elem_catchall *catchall;
aaa31047
PNA
5431 struct nft_set_ext *ext;
5432 int ret = 0;
5433
5434 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
5435 ext = nft_set_elem_ext(set, catchall->elem);
5436 if (!nft_set_elem_active(ext, genmask))
5437 continue;
5438
0e1ea651 5439 ret = nft_setelem_data_validate(ctx, set, catchall->elem);
aaa31047
PNA
5440 if (ret < 0)
5441 break;
5442 }
5443
5444 return ret;
5445}
5446
20a69341
PM
5447int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
5448 struct nft_set_binding *binding)
5449{
5450 struct nft_set_binding *i;
5451 struct nft_set_iter iter;
5452
408070d6 5453 if (!list_empty(&set->bindings) && nft_set_is_anonymous(set))
20a69341
PM
5454 return -EBUSY;
5455
11113e19 5456 if (binding->flags & NFT_SET_MAP) {
20a69341
PM
5457 /* If the set is already bound to the same chain all
5458 * jumps are already validated for that chain.
5459 */
5460 list_for_each_entry(i, &set->bindings, list) {
a4684402 5461 if (i->flags & NFT_SET_MAP &&
11113e19 5462 i->chain == binding->chain)
20a69341
PM
5463 goto bind;
5464 }
5465
8588ac09 5466 iter.genmask = nft_genmask_next(ctx->net);
29b359cf 5467 iter.type = NFT_ITER_UPDATE;
20a69341
PM
5468 iter.skip = 0;
5469 iter.count = 0;
5470 iter.err = 0;
5471 iter.fn = nf_tables_bind_check_setelem;
5472
5473 set->ops->walk(ctx, set, &iter);
aaa31047
PNA
5474 if (!iter.err)
5475 iter.err = nft_set_catchall_bind_check(ctx, set);
5476
a02f4248 5477 if (iter.err < 0)
20a69341 5478 return iter.err;
20a69341
PM
5479 }
5480bind:
1689f259
PNA
5481 if (!nft_use_inc(&set->use))
5482 return -EMFILE;
5483
20a69341 5484 binding->chain = ctx->chain;
e688a7f8 5485 list_add_tail_rcu(&binding->list, &set->bindings);
f6ac8585
PNA
5486 nft_set_trans_bind(ctx, set);
5487
20a69341
PM
5488 return 0;
5489}
63aea290 5490EXPORT_SYMBOL_GPL(nf_tables_bind_set);
20a69341 5491
3b0a081d
FW
5492static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
5493 struct nft_set_binding *binding, bool event)
20a69341 5494{
e688a7f8 5495 list_del_rcu(&binding->list);
20a69341 5496
f6ac8585 5497 if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) {
cd5125d8 5498 list_del_rcu(&set->list);
552705a3 5499 set->dead = 1;
f6ac8585
PNA
5500 if (event)
5501 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET,
5502 GFP_KERNEL);
5503 }
20a69341
PM
5504}
5505
628bd3e4
PNA
5506static void nft_setelem_data_activate(const struct net *net,
5507 const struct nft_set *set,
0e1ea651 5508 struct nft_elem_priv *elem_priv);
628bd3e4
PNA
5509
5510static int nft_mapelem_activate(const struct nft_ctx *ctx,
5511 struct nft_set *set,
5512 const struct nft_set_iter *iter,
0e1ea651 5513 struct nft_elem_priv *elem_priv)
628bd3e4 5514{
e79b47a8
PNA
5515 struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
5516
5517 /* called from abort path, reverse check to undo changes. */
5518 if (nft_set_elem_active(ext, iter->genmask))
5519 return 0;
5520
5521 nft_clear(ctx->net, ext);
0e1ea651 5522 nft_setelem_data_activate(ctx->net, set, elem_priv);
628bd3e4
PNA
5523
5524 return 0;
5525}
5526
5527static void nft_map_catchall_activate(const struct nft_ctx *ctx,
5528 struct nft_set *set)
5529{
5530 u8 genmask = nft_genmask_next(ctx->net);
5531 struct nft_set_elem_catchall *catchall;
628bd3e4
PNA
5532 struct nft_set_ext *ext;
5533
5534 list_for_each_entry(catchall, &set->catchall_list, list) {
5535 ext = nft_set_elem_ext(set, catchall->elem);
5536 if (!nft_set_elem_active(ext, genmask))
5537 continue;
5538
e79b47a8 5539 nft_clear(ctx->net, ext);
0e1ea651 5540 nft_setelem_data_activate(ctx->net, set, catchall->elem);
628bd3e4
PNA
5541 break;
5542 }
5543}
5544
5545static void nft_map_activate(const struct nft_ctx *ctx, struct nft_set *set)
5546{
5547 struct nft_set_iter iter = {
5548 .genmask = nft_genmask_next(ctx->net),
29b359cf 5549 .type = NFT_ITER_UPDATE,
628bd3e4
PNA
5550 .fn = nft_mapelem_activate,
5551 };
5552
5553 set->ops->walk(ctx, set, &iter);
5554 WARN_ON_ONCE(iter.err);
5555
5556 nft_map_catchall_activate(ctx, set);
5557}
5558
c1592a89
PNA
5559void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set)
5560{
628bd3e4
PNA
5561 if (nft_set_is_anonymous(set)) {
5562 if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
5563 nft_map_activate(ctx, set);
5564
c1592a89 5565 nft_clear(ctx->net, set);
628bd3e4 5566 }
c1592a89 5567
1689f259 5568 nft_use_inc_restore(&set->use);
c1592a89
PNA
5569}
5570EXPORT_SYMBOL_GPL(nf_tables_activate_set);
5571
273fe3f1
PNA
5572void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
5573 struct nft_set_binding *binding,
5574 enum nft_trans_phase phase)
5575{
5576 switch (phase) {
26b5a571
PNA
5577 case NFT_TRANS_PREPARE_ERROR:
5578 nft_set_trans_unbind(ctx, set);
c1592a89
PNA
5579 if (nft_set_is_anonymous(set))
5580 nft_deactivate_next(ctx->net, set);
3e704897
PNA
5581 else
5582 list_del_rcu(&binding->list);
c1592a89 5583
1689f259 5584 nft_use_dec(&set->use);
26b5a571 5585 break;
273fe3f1 5586 case NFT_TRANS_PREPARE:
628bd3e4
PNA
5587 if (nft_set_is_anonymous(set)) {
5588 if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
5589 nft_map_deactivate(ctx, set);
c1592a89 5590
628bd3e4
PNA
5591 nft_deactivate_next(ctx->net, set);
5592 }
1689f259 5593 nft_use_dec(&set->use);
273fe3f1
PNA
5594 return;
5595 case NFT_TRANS_ABORT:
5596 case NFT_TRANS_RELEASE:
628bd3e4
PNA
5597 if (nft_set_is_anonymous(set) &&
5598 set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
5599 nft_map_deactivate(ctx, set);
5600
1689f259 5601 nft_use_dec(&set->use);
954d8297 5602 fallthrough;
273fe3f1
PNA
5603 default:
5604 nf_tables_unbind_set(ctx, set, binding,
5605 phase == NFT_TRANS_COMMIT);
5606 }
5607}
5608EXPORT_SYMBOL_GPL(nf_tables_deactivate_set);
5609
cd5125d8
FW
5610void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set)
5611{
f6ac8585 5612 if (list_empty(&set->bindings) && nft_set_is_anonymous(set))
0c2a85ed 5613 nft_set_destroy(ctx, set);
cd5125d8
FW
5614}
5615EXPORT_SYMBOL_GPL(nf_tables_destroy_set);
5616
3ac4c07a
PM
5617const struct nft_set_ext_type nft_set_ext_types[] = {
5618 [NFT_SET_EXT_KEY] = {
7d740264 5619 .align = __alignof__(u32),
3ac4c07a
PM
5620 },
5621 [NFT_SET_EXT_DATA] = {
7d740264 5622 .align = __alignof__(u32),
3ac4c07a 5623 },
563125a7
PNA
5624 [NFT_SET_EXT_EXPRESSIONS] = {
5625 .align = __alignof__(struct nft_set_elem_expr),
f25ad2e9 5626 },
8aeff920
PNA
5627 [NFT_SET_EXT_OBJREF] = {
5628 .len = sizeof(struct nft_object *),
5629 .align = __alignof__(struct nft_object *),
5630 },
3ac4c07a
PM
5631 [NFT_SET_EXT_FLAGS] = {
5632 .len = sizeof(u8),
5633 .align = __alignof__(u8),
5634 },
c3e1b005
PM
5635 [NFT_SET_EXT_TIMEOUT] = {
5636 .len = sizeof(u64),
5637 .align = __alignof__(u64),
5638 },
5639 [NFT_SET_EXT_EXPIRATION] = {
8e1102d5
FW
5640 .len = sizeof(u64),
5641 .align = __alignof__(u64),
c3e1b005 5642 },
68e942e8
PM
5643 [NFT_SET_EXT_USERDATA] = {
5644 .len = sizeof(struct nft_userdata),
5645 .align = __alignof__(struct nft_userdata),
5646 },
7b225d0b
PNA
5647 [NFT_SET_EXT_KEY_END] = {
5648 .align = __alignof__(u32),
5649 },
3ac4c07a 5650};
3ac4c07a 5651
20a69341
PM
5652/*
5653 * Set elements
5654 */
5655
5656static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
5657 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
5658 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
5659 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
c3e1b005 5660 [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 },
79ebb5bb 5661 [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 },
68e942e8
PM
5662 [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY,
5663 .len = NFT_USERDATA_MAXLEN },
467697d2 5664 [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED },
9332d27d
FW
5665 [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING,
5666 .len = NFT_OBJ_MAXNAMELEN - 1 },
7b225d0b 5667 [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED },
013714bf 5668 [NFTA_SET_ELEM_EXPRESSIONS] = NLA_POLICY_NESTED_ARRAY(nft_expr_policy),
20a69341
PM
5669};
5670
5671static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
b2fbd044
LZ
5672 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING,
5673 .len = NFT_TABLE_MAXNAMELEN - 1 },
5674 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING,
5675 .len = NFT_SET_MAXNAMELEN - 1 },
013714bf 5676 [NFTA_SET_ELEM_LIST_ELEMENTS] = NLA_POLICY_NESTED_ARRAY(nft_set_elem_policy),
958bee14 5677 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
20a69341
PM
5678};
5679
563125a7
PNA
5680static int nft_set_elem_expr_dump(struct sk_buff *skb,
5681 const struct nft_set *set,
079cd633
PS
5682 const struct nft_set_ext *ext,
5683 bool reset)
563125a7
PNA
5684{
5685 struct nft_set_elem_expr *elem_expr;
5686 u32 size, num_exprs = 0;
5687 struct nft_expr *expr;
48b0ae04 5688 struct nlattr *nest;
563125a7
PNA
5689
5690 elem_expr = nft_set_ext_expr(ext);
5691 nft_setelem_expr_foreach(expr, elem_expr, size)
5692 num_exprs++;
5693
5694 if (num_exprs == 1) {
5695 expr = nft_setelem_expr_at(elem_expr, 0);
079cd633 5696 if (nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, expr, reset) < 0)
563125a7
PNA
5697 return -1;
5698
5699 return 0;
48b0ae04
PNA
5700 } else if (num_exprs > 1) {
5701 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_EXPRESSIONS);
5702 if (nest == NULL)
5703 goto nla_put_failure;
563125a7 5704
48b0ae04
PNA
5705 nft_setelem_expr_foreach(expr, elem_expr, size) {
5706 expr = nft_setelem_expr_at(elem_expr, size);
079cd633 5707 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, reset) < 0)
48b0ae04
PNA
5708 goto nla_put_failure;
5709 }
5710 nla_nest_end(skb, nest);
5711 }
563125a7 5712 return 0;
48b0ae04
PNA
5713
5714nla_put_failure:
5715 return -1;
563125a7
PNA
5716}
5717
20a69341
PM
5718static int nf_tables_fill_setelem(struct sk_buff *skb,
5719 const struct nft_set *set,
0e1ea651 5720 const struct nft_elem_priv *elem_priv,
079cd633 5721 bool reset)
20a69341 5722{
0e1ea651 5723 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
20a69341
PM
5724 unsigned char *b = skb_tail_pointer(skb);
5725 struct nlattr *nest;
5726
ae0be8de 5727 nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
20a69341
PM
5728 if (nest == NULL)
5729 goto nla_put_failure;
5730
aaa31047
PNA
5731 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) &&
5732 nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
fe2811eb 5733 NFT_DATA_VALUE, set->klen) < 0)
20a69341
PM
5734 goto nla_put_failure;
5735
7b225d0b
PNA
5736 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) &&
5737 nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext),
5738 NFT_DATA_VALUE, set->klen) < 0)
5739 goto nla_put_failure;
5740
fe2811eb
PM
5741 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
5742 nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
20a69341
PM
5743 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
5744 set->dlen) < 0)
5745 goto nla_put_failure;
5746
563125a7 5747 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS) &&
079cd633 5748 nft_set_elem_expr_dump(skb, set, ext, reset))
f25ad2e9
PM
5749 goto nla_put_failure;
5750
8aeff920
PNA
5751 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
5752 nla_put_string(skb, NFTA_SET_ELEM_OBJREF,
d152159b 5753 (*nft_set_ext_obj(ext))->key.name) < 0)
8aeff920
PNA
5754 goto nla_put_failure;
5755
fe2811eb
PM
5756 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
5757 nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
5758 htonl(*nft_set_ext_flags(ext))))
5759 goto nla_put_failure;
20a69341 5760
4c90bba6
PNA
5761 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
5762 nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
5763 nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)),
5764 NFTA_SET_ELEM_PAD))
5765 goto nla_put_failure;
c3e1b005
PM
5766
5767 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
8e1102d5 5768 u64 expires, now = get_jiffies_64();
c3e1b005
PM
5769
5770 expires = *nft_set_ext_expiration(ext);
8e1102d5 5771 if (time_before64(now, expires))
c3e1b005
PM
5772 expires -= now;
5773 else
5774 expires = 0;
5775
5776 if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
8e1102d5 5777 nf_jiffies64_to_msecs(expires),
b46f6ded 5778 NFTA_SET_ELEM_PAD))
c3e1b005
PM
5779 goto nla_put_failure;
5780 }
5781
68e942e8
PM
5782 if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
5783 struct nft_userdata *udata;
5784
5785 udata = nft_set_ext_userdata(ext);
5786 if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
5787 udata->len + 1, udata->data))
5788 goto nla_put_failure;
5789 }
5790
20a69341
PM
5791 nla_nest_end(skb, nest);
5792 return 0;
5793
5794nla_put_failure:
5795 nlmsg_trim(skb, b);
5796 return -EMSGSIZE;
5797}
5798
5799struct nft_set_dump_args {
5800 const struct netlink_callback *cb;
5801 struct nft_set_iter iter;
5802 struct sk_buff *skb;
079cd633 5803 bool reset;
20a69341
PM
5804};
5805
5806static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
de70185d 5807 struct nft_set *set,
20a69341 5808 const struct nft_set_iter *iter,
0e1ea651 5809 struct nft_elem_priv *elem_priv)
20a69341 5810{
0e1ea651 5811 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
20a69341
PM
5812 struct nft_set_dump_args *args;
5813
e79b47a8
PNA
5814 if (!nft_set_elem_active(ext, iter->genmask))
5815 return 0;
5816
6b1ca88e 5817 if (nft_set_elem_expired(ext) || nft_set_elem_is_dead(ext))
24138933
FW
5818 return 0;
5819
20a69341 5820 args = container_of(iter, struct nft_set_dump_args, iter);
0e1ea651 5821 return nf_tables_fill_setelem(args->skb, set, elem_priv, args->reset);
20a69341
PM
5822}
5823
7e9be112
PS
5824static void audit_log_nft_set_reset(const struct nft_table *table,
5825 unsigned int base_seq,
5826 unsigned int nentries)
5827{
5828 char *buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, base_seq);
5829
5830 audit_log_nfcfg(buf, table->family, nentries,
5831 AUDIT_NFT_OP_SETELEM_RESET, GFP_ATOMIC);
5832 kfree(buf);
5833}
5834
fa803605
LZ
5835struct nft_set_dump_ctx {
5836 const struct nft_set *set;
5837 struct nft_ctx ctx;
9cdee063 5838 bool reset;
fa803605
LZ
5839};
5840
aaa31047 5841static int nft_set_catchall_dump(struct net *net, struct sk_buff *skb,
7e9be112
PS
5842 const struct nft_set *set, bool reset,
5843 unsigned int base_seq)
aaa31047
PNA
5844{
5845 struct nft_set_elem_catchall *catchall;
5846 u8 genmask = nft_genmask_cur(net);
aaa31047
PNA
5847 struct nft_set_ext *ext;
5848 int ret = 0;
5849
5850 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
5851 ext = nft_set_elem_ext(set, catchall->elem);
5852 if (!nft_set_elem_active(ext, genmask) ||
5853 nft_set_elem_expired(ext))
5854 continue;
5855
0e1ea651 5856 ret = nf_tables_fill_setelem(skb, set, catchall->elem, reset);
7e9be112
PS
5857 if (reset && !ret)
5858 audit_log_nft_set_reset(set->table, base_seq, 1);
aaa31047
PNA
5859 break;
5860 }
5861
5862 return ret;
5863}
5864
20a69341
PM
5865static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
5866{
fa803605 5867 struct nft_set_dump_ctx *dump_ctx = cb->data;
633c9a84 5868 struct net *net = sock_net(skb->sk);
0854db2a 5869 struct nftables_pernet *nft_net;
fa803605 5870 struct nft_table *table;
de70185d 5871 struct nft_set *set;
20a69341 5872 struct nft_set_dump_args args;
fa803605 5873 bool set_found = false;
20a69341
PM
5874 struct nlmsghdr *nlh;
5875 struct nlattr *nest;
5876 u32 portid, seq;
fa803605 5877 int event;
20a69341 5878
fa803605 5879 rcu_read_lock();
d59d2f82 5880 nft_net = nft_pernet(net);
34002783
PNA
5881 cb->seq = READ_ONCE(nft_net->base_seq);
5882
0854db2a 5883 list_for_each_entry_rcu(table, &nft_net->tables, list) {
36596dad 5884 if (dump_ctx->ctx.family != NFPROTO_UNSPEC &&
98319cb9 5885 dump_ctx->ctx.family != table->family)
fa803605 5886 continue;
20a69341 5887
36596dad
PNA
5888 if (table != dump_ctx->ctx.table)
5889 continue;
20a69341 5890
36596dad
PNA
5891 list_for_each_entry_rcu(set, &table->sets, list) {
5892 if (set == dump_ctx->set) {
5893 set_found = true;
5894 break;
fa803605 5895 }
fa803605
LZ
5896 }
5897 break;
5898 }
5899
5900 if (!set_found) {
5901 rcu_read_unlock();
5902 return -ENOENT;
5903 }
20a69341 5904
dedb67c4 5905 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM);
20a69341
PM
5906 portid = NETLINK_CB(cb->skb).portid;
5907 seq = cb->nlh->nlmsg_seq;
5908
19c28b13
PNA
5909 nlh = nfnl_msg_put(skb, portid, seq, event, NLM_F_MULTI,
5910 table->family, NFNETLINK_V0, nft_base_seq(net));
5911 if (!nlh)
20a69341
PM
5912 goto nla_put_failure;
5913
fa803605 5914 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name))
20a69341
PM
5915 goto nla_put_failure;
5916 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
5917 goto nla_put_failure;
5918
ae0be8de 5919 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
20a69341
PM
5920 if (nest == NULL)
5921 goto nla_put_failure;
5922
8588ac09
PNA
5923 args.cb = cb;
5924 args.skb = skb;
9cdee063 5925 args.reset = dump_ctx->reset;
fa803605 5926 args.iter.genmask = nft_genmask_cur(net);
29b359cf 5927 args.iter.type = NFT_ITER_READ;
8588ac09
PNA
5928 args.iter.skip = cb->args[0];
5929 args.iter.count = 0;
5930 args.iter.err = 0;
5931 args.iter.fn = nf_tables_dump_setelem;
fa803605 5932 set->ops->walk(&dump_ctx->ctx, set, &args.iter);
aaa31047
PNA
5933
5934 if (!args.iter.err && args.iter.count == cb->args[0])
7e9be112 5935 args.iter.err = nft_set_catchall_dump(net, skb, set,
9cdee063 5936 dump_ctx->reset, cb->seq);
20a69341
PM
5937 nla_nest_end(skb, nest);
5938 nlmsg_end(skb, nlh);
5939
9b5ba5c9
PNA
5940 rcu_read_unlock();
5941
20a69341
PM
5942 if (args.iter.err && args.iter.err != -EMSGSIZE)
5943 return args.iter.err;
5944 if (args.iter.count == cb->args[0])
5945 return 0;
5946
5947 cb->args[0] = args.iter.count;
5948 return skb->len;
5949
5950nla_put_failure:
fa803605 5951 rcu_read_unlock();
20a69341
PM
5952 return -ENOSPC;
5953}
5954
3d483faa
PS
5955static int nf_tables_dumpreset_set(struct sk_buff *skb,
5956 struct netlink_callback *cb)
5957{
5958 struct nftables_pernet *nft_net = nft_pernet(sock_net(skb->sk));
5959 struct nft_set_dump_ctx *dump_ctx = cb->data;
5960 int ret, skip = cb->args[0];
5961
5962 mutex_lock(&nft_net->commit_mutex);
5963
5964 ret = nf_tables_dump_set(skb, cb);
5965
5966 if (cb->args[0] > skip)
5967 audit_log_nft_set_reset(dump_ctx->ctx.table, cb->seq,
5968 cb->args[0] - skip);
5969
5970 mutex_unlock(&nft_net->commit_mutex);
5971
5972 return ret;
5973}
5974
90fd131a
FW
5975static int nf_tables_dump_set_start(struct netlink_callback *cb)
5976{
5977 struct nft_set_dump_ctx *dump_ctx = cb->data;
5978
5979 cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC);
5980
5981 return cb->data ? 0 : -ENOMEM;
5982}
5983
fa803605
LZ
5984static int nf_tables_dump_set_done(struct netlink_callback *cb)
5985{
5986 kfree(cb->data);
5987 return 0;
5988}
5989
d60ce62f
AB
5990static int nf_tables_fill_setelem_info(struct sk_buff *skb,
5991 const struct nft_ctx *ctx, u32 seq,
5992 u32 portid, int event, u16 flags,
5993 const struct nft_set *set,
0e1ea651 5994 const struct nft_elem_priv *elem_priv,
079cd633 5995 bool reset)
d60ce62f 5996{
d60ce62f
AB
5997 struct nlmsghdr *nlh;
5998 struct nlattr *nest;
5999 int err;
6000
dedb67c4 6001 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
19c28b13
PNA
6002 nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family,
6003 NFNETLINK_V0, nft_base_seq(ctx->net));
6004 if (!nlh)
d60ce62f
AB
6005 goto nla_put_failure;
6006
d60ce62f
AB
6007 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
6008 goto nla_put_failure;
6009 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
6010 goto nla_put_failure;
6011
ae0be8de 6012 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
d60ce62f
AB
6013 if (nest == NULL)
6014 goto nla_put_failure;
6015
0e1ea651 6016 err = nf_tables_fill_setelem(skb, set, elem_priv, reset);
d60ce62f
AB
6017 if (err < 0)
6018 goto nla_put_failure;
6019
6020 nla_nest_end(skb, nest);
6021
053c095a
JB
6022 nlmsg_end(skb, nlh);
6023 return 0;
d60ce62f
AB
6024
6025nla_put_failure:
6026 nlmsg_trim(skb, nlh);
6027 return -1;
6028}
6029
ba0e4d99
PNA
6030static int nft_setelem_parse_flags(const struct nft_set *set,
6031 const struct nlattr *attr, u32 *flags)
6032{
6033 if (attr == NULL)
6034 return 0;
6035
6036 *flags = ntohl(nla_get_be32(attr));
aaa31047
PNA
6037 if (*flags & ~(NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL))
6038 return -EOPNOTSUPP;
ba0e4d99
PNA
6039 if (!(set->flags & NFT_SET_INTERVAL) &&
6040 *flags & NFT_SET_ELEM_INTERVAL_END)
6041 return -EINVAL;
fc0ae524
PNA
6042 if ((*flags & (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) ==
6043 (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL))
6044 return -EINVAL;
ba0e4d99
PNA
6045
6046 return 0;
6047}
6048
5896e861 6049static int nft_setelem_parse_key(struct nft_ctx *ctx, const struct nft_set *set,
20a1452c
PNA
6050 struct nft_data *key, struct nlattr *attr)
6051{
341b6941
PNA
6052 struct nft_data_desc desc = {
6053 .type = NFT_DATA_VALUE,
6054 .size = NFT_DATA_VALUE_MAXLEN,
6055 .len = set->klen,
6056 };
20a1452c 6057
341b6941 6058 return nft_data_init(ctx, key, &desc, attr);
20a1452c
PNA
6059}
6060
fdb9c405
PNA
6061static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set,
6062 struct nft_data_desc *desc,
6063 struct nft_data *data,
6064 struct nlattr *attr)
6065{
7e6bc1f6 6066 u32 dtype;
fdb9c405 6067
7e6bc1f6
PNA
6068 if (set->dtype == NFT_DATA_VERDICT)
6069 dtype = NFT_DATA_VERDICT;
6070 else
6071 dtype = NFT_DATA_VALUE;
6072
341b6941
PNA
6073 desc->type = dtype;
6074 desc->size = NFT_DATA_VALUE_MAXLEN;
6075 desc->len = set->dlen;
f323ef3a 6076 desc->flags = NFT_DATA_DESC_SETELEM;
fdb9c405 6077
341b6941 6078 return nft_data_init(ctx, data, desc, attr);
fdb9c405
PNA
6079}
6080
aaa31047
PNA
6081static void *nft_setelem_catchall_get(const struct net *net,
6082 const struct nft_set *set)
6083{
6084 struct nft_set_elem_catchall *catchall;
6085 u8 genmask = nft_genmask_cur(net);
6086 struct nft_set_ext *ext;
6087 void *priv = NULL;
6088
6089 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
6090 ext = nft_set_elem_ext(set, catchall->elem);
6091 if (!nft_set_elem_active(ext, genmask) ||
6092 nft_set_elem_expired(ext))
6093 continue;
6094
6095 priv = catchall->elem;
6096 break;
6097 }
6098
6099 return priv;
6100}
6101
5896e861 6102static int nft_setelem_get(struct nft_ctx *ctx, const struct nft_set *set,
aaa31047
PNA
6103 struct nft_set_elem *elem, u32 flags)
6104{
6105 void *priv;
6106
6107 if (!(flags & NFT_SET_ELEM_CATCHALL)) {
6108 priv = set->ops->get(ctx->net, set, elem, flags);
6109 if (IS_ERR(priv))
6110 return PTR_ERR(priv);
6111 } else {
6112 priv = nft_setelem_catchall_get(ctx->net, set);
6113 if (!priv)
6114 return -ENOENT;
6115 }
6116 elem->priv = priv;
6117
6118 return 0;
6119}
6120
5896e861 6121static int nft_get_set_elem(struct nft_ctx *ctx, const struct nft_set *set,
079cd633 6122 const struct nlattr *attr, bool reset)
ba0e4d99
PNA
6123{
6124 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
ba0e4d99
PNA
6125 struct nft_set_elem elem;
6126 struct sk_buff *skb;
6127 uint32_t flags = 0;
ba0e4d99
PNA
6128 int err;
6129
8cb08174
JB
6130 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
6131 nft_set_elem_policy, NULL);
ba0e4d99
PNA
6132 if (err < 0)
6133 return err;
6134
ba0e4d99
PNA
6135 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
6136 if (err < 0)
6137 return err;
6138
aaa31047
PNA
6139 if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL))
6140 return -EINVAL;
6141
6142 if (nla[NFTA_SET_ELEM_KEY]) {
6143 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
6144 nla[NFTA_SET_ELEM_KEY]);
6145 if (err < 0)
6146 return err;
6147 }
ba0e4d99 6148
7b225d0b
PNA
6149 if (nla[NFTA_SET_ELEM_KEY_END]) {
6150 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
6151 nla[NFTA_SET_ELEM_KEY_END]);
6152 if (err < 0)
6153 return err;
6154 }
6155
aaa31047
PNA
6156 err = nft_setelem_get(ctx, set, &elem, flags);
6157 if (err < 0)
6158 return err;
ba0e4d99
PNA
6159
6160 err = -ENOMEM;
d9adf22a 6161 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
ba0e4d99 6162 if (skb == NULL)
ee921183 6163 return err;
ba0e4d99
PNA
6164
6165 err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid,
0e1ea651 6166 NFT_MSG_NEWSETELEM, 0, set, elem.priv,
079cd633 6167 reset);
ba0e4d99 6168 if (err < 0)
ee921183 6169 goto err_fill_setelem;
ba0e4d99 6170
ee921183 6171 return nfnetlink_unicast(skb, ctx->net, ctx->portid);
ba0e4d99 6172
ee921183 6173err_fill_setelem:
ba0e4d99 6174 kfree_skb(skb);
ee921183 6175 return err;
ba0e4d99
PNA
6176}
6177
f649be6d
PS
6178static int nft_set_dump_ctx_init(struct nft_set_dump_ctx *dump_ctx,
6179 const struct sk_buff *skb,
6180 const struct nfnl_info *info,
6181 const struct nlattr * const nla[],
6182 bool reset)
ba0e4d99 6183{
797d4980
PNA
6184 struct netlink_ext_ack *extack = info->extack;
6185 u8 genmask = nft_genmask_cur(info->net);
e2b750d7 6186 u8 family = info->nfmsg->nfgen_family;
797d4980 6187 struct net *net = info->net;
e2b750d7 6188 struct nft_table *table;
ba0e4d99 6189 struct nft_set *set;
ba0e4d99 6190
e2b750d7 6191 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
92f3e96d 6192 genmask, 0);
e2b750d7
PNA
6193 if (IS_ERR(table)) {
6194 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
6195 return PTR_ERR(table);
6196 }
ba0e4d99 6197
e2b750d7 6198 set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
aee1f692
PNA
6199 if (IS_ERR(set)) {
6200 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_SET]);
ba0e4d99 6201 return PTR_ERR(set);
aee1f692 6202 }
ba0e4d99 6203
f649be6d
PS
6204 nft_ctx_init(&dump_ctx->ctx, net, skb,
6205 info->nlh, family, table, NULL, nla);
6206 dump_ctx->set = set;
6207 dump_ctx->reset = reset;
6208 return 0;
6209}
6210
6211/* called with rcu_read_lock held */
6212static int nf_tables_getsetelem(struct sk_buff *skb,
6213 const struct nfnl_info *info,
6214 const struct nlattr * const nla[])
6215{
6216 struct netlink_ext_ack *extack = info->extack;
6217 struct nft_set_dump_ctx dump_ctx;
f649be6d 6218 struct nlattr *attr;
3d483faa 6219 int rem, err = 0;
9cdee063 6220
797d4980 6221 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
ba0e4d99 6222 struct netlink_dump_control c = {
90fd131a 6223 .start = nf_tables_dump_set_start,
ba0e4d99
PNA
6224 .dump = nf_tables_dump_set,
6225 .done = nf_tables_dump_set_done,
d9adf22a 6226 .module = THIS_MODULE,
ba0e4d99 6227 };
f649be6d 6228
3d483faa 6229 err = nft_set_dump_ctx_init(&dump_ctx, skb, info, nla, false);
f649be6d
PS
6230 if (err)
6231 return err;
ba0e4d99 6232
90fd131a 6233 c.data = &dump_ctx;
797d4980 6234 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
ba0e4d99
PNA
6235 }
6236
6237 if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
6238 return -EINVAL;
6239
3d483faa 6240 err = nft_set_dump_ctx_init(&dump_ctx, skb, info, nla, false);
f649be6d
PS
6241 if (err)
6242 return err;
6243
ba0e4d99 6244 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3d483faa
PS
6245 err = nft_get_set_elem(&dump_ctx.ctx, dump_ctx.set, attr, false);
6246 if (err < 0) {
6247 NL_SET_BAD_ATTR(extack, attr);
6248 break;
6249 }
6250 }
6251
6252 return err;
6253}
6254
6255static int nf_tables_getsetelem_reset(struct sk_buff *skb,
6256 const struct nfnl_info *info,
6257 const struct nlattr * const nla[])
6258{
6259 struct nftables_pernet *nft_net = nft_pernet(info->net);
6260 struct netlink_ext_ack *extack = info->extack;
6261 struct nft_set_dump_ctx dump_ctx;
6262 int rem, err = 0, nelems = 0;
6263 struct nlattr *attr;
6264
6265 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
6266 struct netlink_dump_control c = {
6267 .start = nf_tables_dump_set_start,
6268 .dump = nf_tables_dumpreset_set,
6269 .done = nf_tables_dump_set_done,
6270 .module = THIS_MODULE,
6271 };
6272
6273 err = nft_set_dump_ctx_init(&dump_ctx, skb, info, nla, true);
6274 if (err)
6275 return err;
6276
6277 c.data = &dump_ctx;
6278 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
6279 }
6280
6281 if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
6282 return -EINVAL;
6283
6284 if (!try_module_get(THIS_MODULE))
6285 return -EINVAL;
6286 rcu_read_unlock();
6287 mutex_lock(&nft_net->commit_mutex);
6288 rcu_read_lock();
6289
6290 err = nft_set_dump_ctx_init(&dump_ctx, skb, info, nla, true);
6291 if (err)
6292 goto out_unlock;
6293
6294 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
6295 err = nft_get_set_elem(&dump_ctx.ctx, dump_ctx.set, attr, true);
b53c1166
PNA
6296 if (err < 0) {
6297 NL_SET_BAD_ATTR(extack, attr);
ba0e4d99 6298 break;
b53c1166 6299 }
7e9be112 6300 nelems++;
ba0e4d99 6301 }
3d483faa 6302 audit_log_nft_set_reset(dump_ctx.ctx.table, nft_net->base_seq, nelems);
ba0e4d99 6303
3d483faa
PS
6304out_unlock:
6305 rcu_read_unlock();
6306 mutex_unlock(&nft_net->commit_mutex);
6307 rcu_read_lock();
6308 module_put(THIS_MODULE);
7e9be112 6309
ba0e4d99
PNA
6310 return err;
6311}
6312
25e94a99
PNA
6313static void nf_tables_setelem_notify(const struct nft_ctx *ctx,
6314 const struct nft_set *set,
0e1ea651 6315 const struct nft_elem_priv *elem_priv,
6fb721cf 6316 int event)
d60ce62f 6317{
0854db2a 6318 struct nftables_pernet *nft_net;
128ad332
PNA
6319 struct net *net = ctx->net;
6320 u32 portid = ctx->portid;
d60ce62f 6321 struct sk_buff *skb;
6fb721cf 6322 u16 flags = 0;
d60ce62f
AB
6323 int err;
6324
128ad332 6325 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
25e94a99 6326 return;
d60ce62f 6327
d60ce62f
AB
6328 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6329 if (skb == NULL)
6330 goto err;
6331
6fb721cf
PNA
6332 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
6333 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
6334
d60ce62f 6335 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
0e1ea651 6336 set, elem_priv, false);
d60ce62f
AB
6337 if (err < 0) {
6338 kfree_skb(skb);
6339 goto err;
6340 }
6341
d59d2f82 6342 nft_net = nft_pernet(net);
0854db2a 6343 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
25e94a99 6344 return;
d60ce62f 6345err:
25e94a99 6346 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
d60ce62f
AB
6347}
6348
60319eb1
PNA
6349static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
6350 int msg_type,
6351 struct nft_set *set)
6352{
6353 struct nft_trans *trans;
6354
6355 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
6356 if (trans == NULL)
6357 return NULL;
6358
6359 nft_trans_elem_set(trans) = set;
6360 return trans;
6361}
6362
a7fc9368
PNA
6363struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
6364 const struct nft_set *set,
6365 const struct nlattr *attr)
6366{
6367 struct nft_expr *expr;
6368 int err;
6369
6370 expr = nft_expr_init(ctx, attr);
6371 if (IS_ERR(expr))
6372 return expr;
6373
6374 err = -EOPNOTSUPP;
a7fc9368
PNA
6375 if (expr->ops->type->flags & NFT_EXPR_GC) {
6376 if (set->flags & NFT_SET_TIMEOUT)
6377 goto err_set_elem_expr;
6378 if (!set->ops->gc_init)
6379 goto err_set_elem_expr;
6380 set->ops->gc_init(set);
6381 }
6382
6383 return expr;
6384
6385err_set_elem_expr:
6386 nft_expr_destroy(ctx, expr);
6387 return ERR_PTR(err);
6388}
6389
34aae2c2
PNA
6390static int nft_set_ext_check(const struct nft_set_ext_tmpl *tmpl, u8 id, u32 len)
6391{
6392 len += nft_set_ext_types[id].len;
6393 if (len > tmpl->ext_len[id] ||
6394 len > U8_MAX)
6395 return -1;
6396
6397 return 0;
6398}
6399
6400static int nft_set_ext_memcpy(const struct nft_set_ext_tmpl *tmpl, u8 id,
6401 void *to, const void *from, u32 len)
6402{
6403 if (nft_set_ext_check(tmpl, id, len) < 0)
6404 return -1;
6405
6406 memcpy(to, from, len);
6407
6408 return 0;
6409}
6410
9dad402b
PNA
6411struct nft_elem_priv *nft_set_elem_init(const struct nft_set *set,
6412 const struct nft_set_ext_tmpl *tmpl,
6413 const u32 *key, const u32 *key_end,
6414 const u32 *data,
6415 u64 timeout, u64 expiration, gfp_t gfp)
fe2811eb
PM
6416{
6417 struct nft_set_ext *ext;
6418 void *elem;
6419
6420 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
6421 if (elem == NULL)
34aae2c2 6422 return ERR_PTR(-ENOMEM);
fe2811eb
PM
6423
6424 ext = nft_set_elem_ext(set, elem);
6425 nft_set_ext_init(ext, tmpl);
6426
34aae2c2
PNA
6427 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) &&
6428 nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY,
6429 nft_set_ext_key(ext), key, set->klen) < 0)
6430 goto err_ext_check;
6431
6432 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) &&
6433 nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY_END,
6434 nft_set_ext_key_end(ext), key_end, set->klen) < 0)
6435 goto err_ext_check;
6436
6437 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
6438 nft_set_ext_memcpy(tmpl, NFT_SET_EXT_DATA,
6439 nft_set_ext_data(ext), data, set->dlen) < 0)
6440 goto err_ext_check;
6441
79ebb5bb
LGL
6442 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
6443 *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration;
6444 if (expiration == 0)
6445 *nft_set_ext_expiration(ext) += timeout;
6446 }
c3e1b005
PM
6447 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
6448 *nft_set_ext_timeout(ext) = timeout;
fe2811eb
PM
6449
6450 return elem;
34aae2c2
PNA
6451
6452err_ext_check:
6453 kfree(elem);
6454
6455 return ERR_PTR(-EINVAL);
fe2811eb
PM
6456}
6457
563125a7
PNA
6458static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
6459 struct nft_expr *expr)
475beb9c
PNA
6460{
6461 if (expr->ops->destroy_clone) {
6462 expr->ops->destroy_clone(ctx, expr);
6463 module_put(expr->ops->type->owner);
6464 } else {
6465 nf_tables_expr_destroy(ctx, expr);
6466 }
6467}
6468
563125a7
PNA
6469static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
6470 struct nft_set_elem_expr *elem_expr)
6471{
6472 struct nft_expr *expr;
6473 u32 size;
6474
6475 nft_setelem_expr_foreach(expr, elem_expr, size)
6476 __nft_set_elem_expr_destroy(ctx, expr);
6477}
6478
628bd3e4 6479/* Drop references and destroy. Called from gc, dynset and abort path. */
9dad402b
PNA
6480void nft_set_elem_destroy(const struct nft_set *set,
6481 const struct nft_elem_priv *elem_priv,
61f9e292 6482 bool destroy_expr)
61edafbb 6483{
9dad402b 6484 struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
3453c927
PNA
6485 struct nft_ctx ctx = {
6486 .net = read_pnet(&set->net),
6487 .family = set->table->family,
6488 };
61edafbb 6489
59105446 6490 nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE);
61edafbb 6491 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
59105446 6492 nft_data_release(nft_set_ext_data(ext), set->dtype);
563125a7 6493 if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
475beb9c 6494 nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext));
8aeff920 6495 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
1689f259 6496 nft_use_dec(&(*nft_set_ext_obj(ext))->use);
9dad402b
PNA
6497
6498 kfree(elem_priv);
61edafbb
PM
6499}
6500EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
6501
628bd3e4
PNA
6502/* Destroy element. References have been already dropped in the preparation
6503 * path via nft_setelem_data_deactivate().
59105446 6504 */
628bd3e4 6505void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
9dad402b
PNA
6506 const struct nft_set *set,
6507 const struct nft_elem_priv *elem_priv)
59105446 6508{
9dad402b 6509 struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
59105446 6510
563125a7 6511 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
475beb9c
PNA
6512 nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext));
6513
9dad402b 6514 kfree(elem_priv);
59105446
PNA
6515}
6516
fca05d4d
PNA
6517int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
6518 struct nft_expr *expr_array[])
8cfd9b0f
PNA
6519{
6520 struct nft_expr *expr;
6521 int err, i, k;
6522
6523 for (i = 0; i < set->num_exprs; i++) {
42193ffd 6524 expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL_ACCOUNT);
8cfd9b0f
PNA
6525 if (!expr)
6526 goto err_expr;
6527
fa23e0d4 6528 err = nft_expr_clone(expr, set->exprs[i], GFP_KERNEL_ACCOUNT);
8cfd9b0f 6529 if (err < 0) {
c485c35f 6530 kfree(expr);
8cfd9b0f
PNA
6531 goto err_expr;
6532 }
6533 expr_array[i] = expr;
6534 }
6535
6536 return 0;
6537
6538err_expr:
161b838e
CIK
6539 for (k = i - 1; k >= 0; k--)
6540 nft_expr_destroy(ctx, expr_array[k]);
8cfd9b0f
PNA
6541
6542 return -ENOMEM;
6543}
6544
4d8f9065 6545static int nft_set_elem_expr_setup(struct nft_ctx *ctx,
34aae2c2 6546 const struct nft_set_ext_tmpl *tmpl,
4d8f9065
PNA
6547 const struct nft_set_ext *ext,
6548 struct nft_expr *expr_array[],
6549 u32 num_exprs)
563125a7
PNA
6550{
6551 struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext);
34aae2c2 6552 u32 len = sizeof(struct nft_set_elem_expr);
4d8f9065
PNA
6553 struct nft_expr *expr;
6554 int i, err;
6555
34aae2c2
PNA
6556 if (num_exprs == 0)
6557 return 0;
6558
6559 for (i = 0; i < num_exprs; i++)
6560 len += expr_array[i]->ops->size;
6561
6562 if (nft_set_ext_check(tmpl, NFT_SET_EXT_EXPRESSIONS, len) < 0)
6563 return -EINVAL;
6564
4d8f9065
PNA
6565 for (i = 0; i < num_exprs; i++) {
6566 expr = nft_setelem_expr_at(elem_expr, elem_expr->size);
fa23e0d4 6567 err = nft_expr_clone(expr, expr_array[i], GFP_KERNEL_ACCOUNT);
4d8f9065
PNA
6568 if (err < 0)
6569 goto err_elem_expr_setup;
6570
6571 elem_expr->size += expr_array[i]->ops->size;
6572 nft_expr_destroy(ctx, expr_array[i]);
6573 expr_array[i] = NULL;
6574 }
6575
6576 return 0;
6577
6578err_elem_expr_setup:
6579 for (; i < num_exprs; i++) {
6580 nft_expr_destroy(ctx, expr_array[i]);
6581 expr_array[i] = NULL;
6582 }
563125a7 6583
4d8f9065 6584 return -ENOMEM;
563125a7
PNA
6585}
6586
aaa31047
PNA
6587struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
6588 const struct nft_set *set)
6589{
6590 struct nft_set_elem_catchall *catchall;
6591 u8 genmask = nft_genmask_cur(net);
6592 struct nft_set_ext *ext;
6593
6594 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
6595 ext = nft_set_elem_ext(set, catchall->elem);
6596 if (nft_set_elem_active(ext, genmask) &&
5f68718b
PNA
6597 !nft_set_elem_expired(ext) &&
6598 !nft_set_elem_is_dead(ext))
aaa31047
PNA
6599 return ext;
6600 }
6601
6602 return NULL;
6603}
6604EXPORT_SYMBOL_GPL(nft_set_catchall_lookup);
6605
aaa31047
PNA
6606static int nft_setelem_catchall_insert(const struct net *net,
6607 struct nft_set *set,
6608 const struct nft_set_elem *elem,
078996fc 6609 struct nft_elem_priv **priv)
aaa31047
PNA
6610{
6611 struct nft_set_elem_catchall *catchall;
6612 u8 genmask = nft_genmask_next(net);
6613 struct nft_set_ext *ext;
6614
6615 list_for_each_entry(catchall, &set->catchall_list, list) {
6616 ext = nft_set_elem_ext(set, catchall->elem);
6617 if (nft_set_elem_active(ext, genmask)) {
078996fc 6618 *priv = catchall->elem;
aaa31047
PNA
6619 return -EEXIST;
6620 }
6621 }
6622
6623 catchall = kmalloc(sizeof(*catchall), GFP_KERNEL);
6624 if (!catchall)
6625 return -ENOMEM;
6626
6627 catchall->elem = elem->priv;
6628 list_add_tail_rcu(&catchall->list, &set->catchall_list);
6629
6630 return 0;
6631}
6632
6633static int nft_setelem_insert(const struct net *net,
6634 struct nft_set *set,
6635 const struct nft_set_elem *elem,
078996fc
PNA
6636 struct nft_elem_priv **elem_priv,
6637 unsigned int flags)
aaa31047
PNA
6638{
6639 int ret;
6640
6641 if (flags & NFT_SET_ELEM_CATCHALL)
078996fc 6642 ret = nft_setelem_catchall_insert(net, set, elem, elem_priv);
aaa31047 6643 else
078996fc 6644 ret = set->ops->insert(net, set, elem, elem_priv);
aaa31047
PNA
6645
6646 return ret;
6647}
6648
6649static bool nft_setelem_is_catchall(const struct nft_set *set,
0e1ea651 6650 const struct nft_elem_priv *elem_priv)
aaa31047 6651{
0e1ea651 6652 struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
aaa31047
PNA
6653
6654 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
6655 *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL)
6656 return true;
6657
6658 return false;
6659}
6660
6661static void nft_setelem_activate(struct net *net, struct nft_set *set,
0e1ea651 6662 struct nft_elem_priv *elem_priv)
aaa31047 6663{
0e1ea651 6664 struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
aaa31047 6665
0e1ea651 6666 if (nft_setelem_is_catchall(set, elem_priv)) {
e79b47a8 6667 nft_clear(net, ext);
aaa31047 6668 } else {
0e1ea651 6669 set->ops->activate(net, set, elem_priv);
aaa31047
PNA
6670 }
6671}
6672
6673static int nft_setelem_catchall_deactivate(const struct net *net,
6674 struct nft_set *set,
6675 struct nft_set_elem *elem)
6676{
6677 struct nft_set_elem_catchall *catchall;
6678 struct nft_set_ext *ext;
6679
6680 list_for_each_entry(catchall, &set->catchall_list, list) {
6681 ext = nft_set_elem_ext(set, catchall->elem);
b1db244f 6682 if (!nft_is_active_next(net, ext))
aaa31047
PNA
6683 continue;
6684
6685 kfree(elem->priv);
6686 elem->priv = catchall->elem;
6687 nft_set_elem_change_active(net, set, ext);
6688 return 0;
6689 }
6690
6691 return -ENOENT;
6692}
6693
6694static int __nft_setelem_deactivate(const struct net *net,
6695 struct nft_set *set,
6696 struct nft_set_elem *elem)
6697{
6698 void *priv;
6699
6700 priv = set->ops->deactivate(net, set, elem);
6701 if (!priv)
6702 return -ENOENT;
6703
6704 kfree(elem->priv);
6705 elem->priv = priv;
6706 set->ndeact++;
6707
6708 return 0;
6709}
6710
6711static int nft_setelem_deactivate(const struct net *net,
6712 struct nft_set *set,
6713 struct nft_set_elem *elem, u32 flags)
6714{
6715 int ret;
6716
6717 if (flags & NFT_SET_ELEM_CATCHALL)
6718 ret = nft_setelem_catchall_deactivate(net, set, elem);
6719 else
6720 ret = __nft_setelem_deactivate(net, set, elem);
6721
6722 return ret;
6723}
6724
93995bf4
PNA
6725static void nft_setelem_catchall_destroy(struct nft_set_elem_catchall *catchall)
6726{
6727 list_del_rcu(&catchall->list);
6728 kfree_rcu(catchall, rcu);
6729}
6730
aaa31047
PNA
6731static void nft_setelem_catchall_remove(const struct net *net,
6732 const struct nft_set *set,
0e1ea651 6733 struct nft_elem_priv *elem_priv)
aaa31047
PNA
6734{
6735 struct nft_set_elem_catchall *catchall, *next;
6736
6737 list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
0e1ea651 6738 if (catchall->elem == elem_priv) {
93995bf4 6739 nft_setelem_catchall_destroy(catchall);
aaa31047
PNA
6740 break;
6741 }
6742 }
6743}
6744
6745static void nft_setelem_remove(const struct net *net,
6746 const struct nft_set *set,
0e1ea651 6747 struct nft_elem_priv *elem_priv)
aaa31047 6748{
0e1ea651
PNA
6749 if (nft_setelem_is_catchall(set, elem_priv))
6750 nft_setelem_catchall_remove(net, set, elem_priv);
aaa31047 6751 else
0e1ea651 6752 set->ops->remove(net, set, elem_priv);
aaa31047
PNA
6753}
6754
88cccd90
PNA
6755static bool nft_setelem_valid_key_end(const struct nft_set *set,
6756 struct nlattr **nla, u32 flags)
6757{
6758 if ((set->flags & (NFT_SET_CONCAT | NFT_SET_INTERVAL)) ==
6759 (NFT_SET_CONCAT | NFT_SET_INTERVAL)) {
6760 if (flags & NFT_SET_ELEM_INTERVAL_END)
6761 return false;
96df8360
PNA
6762
6763 if (nla[NFTA_SET_ELEM_KEY_END] &&
6764 flags & NFT_SET_ELEM_CATCHALL)
88cccd90
PNA
6765 return false;
6766 } else {
6767 if (nla[NFTA_SET_ELEM_KEY_END])
6768 return false;
6769 }
6770
6771 return true;
6772}
6773
60319eb1 6774static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
c016c7e4 6775 const struct nlattr *attr, u32 nlmsg_flags)
20a69341 6776{
8cfd9b0f 6777 struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {};
20a69341 6778 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
8aeff920 6779 u8 genmask = nft_genmask_next(ctx->net);
664899e8 6780 u32 flags = 0, size = 0, num_exprs = 0;
fe2811eb 6781 struct nft_set_ext_tmpl tmpl;
c016c7e4 6782 struct nft_set_ext *ext, *ext2;
20a69341
PM
6783 struct nft_set_elem elem;
6784 struct nft_set_binding *binding;
078996fc 6785 struct nft_elem_priv *elem_priv;
8aeff920 6786 struct nft_object *obj = NULL;
68e942e8 6787 struct nft_userdata *udata;
20a1452c 6788 struct nft_data_desc desc;
20a69341 6789 enum nft_registers dreg;
60319eb1 6790 struct nft_trans *trans;
79ebb5bb 6791 u64 expiration;
078996fc 6792 u64 timeout;
8cfd9b0f 6793 int err, i;
68e942e8 6794 u8 ulen;
20a69341 6795
8cb08174
JB
6796 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
6797 nft_set_elem_policy, NULL);
20a69341
PM
6798 if (err < 0)
6799 return err;
6800
fe2811eb
PM
6801 nft_set_ext_prepare(&tmpl);
6802
0e9091d6
PNA
6803 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
6804 if (err < 0)
6805 return err;
aaa31047 6806
d4eb7e39
PNA
6807 if (((flags & NFT_SET_ELEM_CATCHALL) && nla[NFTA_SET_ELEM_KEY]) ||
6808 (!(flags & NFT_SET_ELEM_CATCHALL) && !nla[NFTA_SET_ELEM_KEY]))
aaa31047
PNA
6809 return -EINVAL;
6810
c39ba4de
PNA
6811 if (flags != 0) {
6812 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
6813 if (err < 0)
6814 return err;
6815 }
20a69341
PM
6816
6817 if (set->flags & NFT_SET_MAP) {
6818 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
fe2811eb 6819 !(flags & NFT_SET_ELEM_INTERVAL_END))
20a69341
PM
6820 return -EINVAL;
6821 } else {
6822 if (nla[NFTA_SET_ELEM_DATA] != NULL)
6823 return -EINVAL;
6824 }
6825
5a2f3dc3
PNA
6826 if (set->flags & NFT_SET_OBJECT) {
6827 if (!nla[NFTA_SET_ELEM_OBJREF] &&
6828 !(flags & NFT_SET_ELEM_INTERVAL_END))
6829 return -EINVAL;
6830 } else {
6831 if (nla[NFTA_SET_ELEM_OBJREF])
6832 return -EINVAL;
6833 }
6834
88cccd90
PNA
6835 if (!nft_setelem_valid_key_end(set, nla, flags))
6836 return -EINVAL;
6837
bffc124b
PNA
6838 if ((flags & NFT_SET_ELEM_INTERVAL_END) &&
6839 (nla[NFTA_SET_ELEM_DATA] ||
6840 nla[NFTA_SET_ELEM_OBJREF] ||
6841 nla[NFTA_SET_ELEM_TIMEOUT] ||
6842 nla[NFTA_SET_ELEM_EXPIRATION] ||
6843 nla[NFTA_SET_ELEM_USERDATA] ||
48b0ae04 6844 nla[NFTA_SET_ELEM_EXPR] ||
4963674c 6845 nla[NFTA_SET_ELEM_KEY_END] ||
48b0ae04 6846 nla[NFTA_SET_ELEM_EXPRESSIONS]))
bffc124b
PNA
6847 return -EINVAL;
6848
c3e1b005
PM
6849 timeout = 0;
6850 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
6851 if (!(set->flags & NFT_SET_TIMEOUT))
6852 return -EINVAL;
8e1102d5
FW
6853 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT],
6854 &timeout);
6855 if (err)
6856 return err;
33c7aba0
PNA
6857 } else if (set->flags & NFT_SET_TIMEOUT &&
6858 !(flags & NFT_SET_ELEM_INTERVAL_END)) {
123b9961 6859 timeout = READ_ONCE(set->timeout);
c3e1b005
PM
6860 }
6861
79ebb5bb
LGL
6862 expiration = 0;
6863 if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) {
6864 if (!(set->flags & NFT_SET_TIMEOUT))
6865 return -EINVAL;
6866 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION],
6867 &expiration);
6868 if (err)
6869 return err;
6870 }
6871
8cfd9b0f
PNA
6872 if (nla[NFTA_SET_ELEM_EXPR]) {
6873 struct nft_expr *expr;
6874
664899e8 6875 if (set->num_exprs && set->num_exprs != 1)
8cfd9b0f
PNA
6876 return -EOPNOTSUPP;
6877
40944452
PNA
6878 expr = nft_set_elem_expr_alloc(ctx, set,
6879 nla[NFTA_SET_ELEM_EXPR]);
6880 if (IS_ERR(expr))
6881 return PTR_ERR(expr);
65038428 6882
8cfd9b0f 6883 expr_array[0] = expr;
664899e8 6884 num_exprs = 1;
65038428 6885
664899e8 6886 if (set->num_exprs && set->exprs[0]->ops != expr->ops) {
8cfd9b0f 6887 err = -EOPNOTSUPP;
65038428 6888 goto err_set_elem_expr;
8cfd9b0f 6889 }
48b0ae04
PNA
6890 } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) {
6891 struct nft_expr *expr;
6892 struct nlattr *tmp;
6893 int left;
65038428 6894
48b0ae04
PNA
6895 i = 0;
6896 nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) {
664899e8
PNA
6897 if (i == NFT_SET_EXPR_MAX ||
6898 (set->num_exprs && set->num_exprs == i)) {
48b0ae04
PNA
6899 err = -E2BIG;
6900 goto err_set_elem_expr;
6901 }
6902 if (nla_type(tmp) != NFTA_LIST_ELEM) {
6903 err = -EINVAL;
6904 goto err_set_elem_expr;
6905 }
6906 expr = nft_set_elem_expr_alloc(ctx, set, tmp);
6907 if (IS_ERR(expr)) {
6908 err = PTR_ERR(expr);
6909 goto err_set_elem_expr;
6910 }
6911 expr_array[i] = expr;
664899e8 6912 num_exprs++;
48b0ae04 6913
664899e8 6914 if (set->num_exprs && expr->ops != set->exprs[i]->ops) {
48b0ae04
PNA
6915 err = -EOPNOTSUPP;
6916 goto err_set_elem_expr;
6917 }
6918 i++;
6919 }
664899e8 6920 if (set->num_exprs && set->num_exprs != i) {
48b0ae04 6921 err = -EOPNOTSUPP;
65038428 6922 goto err_set_elem_expr;
48b0ae04 6923 }
33c7aba0
PNA
6924 } else if (set->num_exprs > 0 &&
6925 !(flags & NFT_SET_ELEM_INTERVAL_END)) {
8cfd9b0f
PNA
6926 err = nft_set_elem_expr_clone(ctx, set, expr_array);
6927 if (err < 0)
6928 goto err_set_elem_expr_clone;
664899e8
PNA
6929
6930 num_exprs = set->num_exprs;
40944452
PNA
6931 }
6932
aaa31047
PNA
6933 if (nla[NFTA_SET_ELEM_KEY]) {
6934 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
6935 nla[NFTA_SET_ELEM_KEY]);
6936 if (err < 0)
6937 goto err_set_elem_expr;
20a69341 6938
c39ba4de
PNA
6939 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
6940 if (err < 0)
6941 goto err_parse_key;
aaa31047 6942 }
7b225d0b
PNA
6943
6944 if (nla[NFTA_SET_ELEM_KEY_END]) {
6945 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
6946 nla[NFTA_SET_ELEM_KEY_END]);
6947 if (err < 0)
6948 goto err_parse_key;
6949
c39ba4de
PNA
6950 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
6951 if (err < 0)
6952 goto err_parse_key_end;
7b225d0b
PNA
6953 }
6954
c3e1b005 6955 if (timeout > 0) {
c39ba4de
PNA
6956 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
6957 if (err < 0)
6958 goto err_parse_key_end;
6959
123b9961 6960 if (timeout != READ_ONCE(set->timeout)) {
c39ba4de
PNA
6961 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
6962 if (err < 0)
6963 goto err_parse_key_end;
6964 }
c3e1b005 6965 }
fe2811eb 6966
664899e8
PNA
6967 if (num_exprs) {
6968 for (i = 0; i < num_exprs; i++)
563125a7
PNA
6969 size += expr_array[i]->ops->size;
6970
c39ba4de
PNA
6971 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS,
6972 sizeof(struct nft_set_elem_expr) + size);
6973 if (err < 0)
6974 goto err_parse_key_end;
563125a7 6975 }
40944452 6976
8aeff920 6977 if (nla[NFTA_SET_ELEM_OBJREF] != NULL) {
4d44175a
FW
6978 obj = nft_obj_lookup(ctx->net, ctx->table,
6979 nla[NFTA_SET_ELEM_OBJREF],
cac20fcd 6980 set->objtype, genmask);
8aeff920
PNA
6981 if (IS_ERR(obj)) {
6982 err = PTR_ERR(obj);
1689f259 6983 obj = NULL;
7b225d0b 6984 goto err_parse_key_end;
8aeff920 6985 }
1689f259
PNA
6986
6987 if (!nft_use_inc(&obj->use)) {
6988 err = -EMFILE;
6989 obj = NULL;
6990 goto err_parse_key_end;
6991 }
6992
c39ba4de
PNA
6993 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF);
6994 if (err < 0)
6995 goto err_parse_key_end;
8aeff920
PNA
6996 }
6997
20a69341 6998 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
fdb9c405
PNA
6999 err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val,
7000 nla[NFTA_SET_ELEM_DATA]);
20a69341 7001 if (err < 0)
7b225d0b 7002 goto err_parse_key_end;
20a69341 7003
20a69341
PM
7004 dreg = nft_type_to_reg(set->dtype);
7005 list_for_each_entry(binding, &set->bindings, list) {
7006 struct nft_ctx bind_ctx = {
58c78e10 7007 .net = ctx->net,
36596dad 7008 .family = ctx->family,
20a69341 7009 .table = ctx->table,
7c95f6d8 7010 .chain = (struct nft_chain *)binding->chain,
20a69341
PM
7011 };
7012
11113e19
PM
7013 if (!(binding->flags & NFT_SET_MAP))
7014 continue;
7015
1ec10212 7016 err = nft_validate_register_store(&bind_ctx, dreg,
fdb9c405 7017 &elem.data.val,
20a1452c 7018 desc.type, desc.len);
20a69341 7019 if (err < 0)
7b225d0b 7020 goto err_parse_data;
a654de8f 7021
20a1452c 7022 if (desc.type == NFT_DATA_VERDICT &&
fdb9c405
PNA
7023 (elem.data.val.verdict.code == NFT_GOTO ||
7024 elem.data.val.verdict.code == NFT_JUMP))
00c320f9 7025 nft_validate_state_update(ctx->table,
a654de8f 7026 NFT_VALIDATE_NEED);
20a69341 7027 }
fe2811eb 7028
c39ba4de
PNA
7029 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len);
7030 if (err < 0)
7031 goto err_parse_data;
20a69341
PM
7032 }
7033
68e942e8
PM
7034 /* The full maximum length of userdata can exceed the maximum
7035 * offset value (U8_MAX) for following extensions, therefor it
7036 * must be the last extension added.
7037 */
7038 ulen = 0;
7039 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
7040 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
c39ba4de
PNA
7041 if (ulen > 0) {
7042 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
7043 ulen);
7044 if (err < 0)
7045 goto err_parse_data;
7046 }
68e942e8
PM
7047 }
7048
7b225d0b 7049 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
fdb9c405 7050 elem.key_end.val.data, elem.data.val.data,
33758c89 7051 timeout, expiration, GFP_KERNEL_ACCOUNT);
34aae2c2
PNA
7052 if (IS_ERR(elem.priv)) {
7053 err = PTR_ERR(elem.priv);
7b225d0b 7054 goto err_parse_data;
34aae2c2 7055 }
fe2811eb
PM
7056
7057 ext = nft_set_elem_ext(set, elem.priv);
7058 if (flags)
7059 *nft_set_ext_flags(ext) = flags;
34aae2c2 7060
1689f259 7061 if (obj)
d6b47866 7062 *nft_set_ext_obj(ext) = obj;
1689f259 7063
68e942e8 7064 if (ulen > 0) {
34aae2c2
PNA
7065 if (nft_set_ext_check(&tmpl, NFT_SET_EXT_USERDATA, ulen) < 0) {
7066 err = -EINVAL;
d6b47866 7067 goto err_elem_free;
34aae2c2 7068 }
68e942e8
PM
7069 udata = nft_set_ext_userdata(ext);
7070 udata->len = ulen - 1;
7071 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
7072 }
34aae2c2 7073 err = nft_set_elem_expr_setup(ctx, &tmpl, ext, expr_array, num_exprs);
4d8f9065 7074 if (err < 0)
34aae2c2 7075 goto err_elem_free;
fe2811eb 7076
60319eb1 7077 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
4d8f9065
PNA
7078 if (trans == NULL) {
7079 err = -ENOMEM;
34aae2c2 7080 goto err_elem_free;
4d8f9065 7081 }
60319eb1 7082
a2dd0233 7083 ext->genmask = nft_genmask_cur(ctx->net);
aaa31047 7084
078996fc 7085 err = nft_setelem_insert(ctx->net, set, &elem, &elem_priv, flags);
c016c7e4
PNA
7086 if (err) {
7087 if (err == -EEXIST) {
078996fc 7088 ext2 = nft_set_elem_ext(set, elem_priv);
9744a6fc
PNA
7089 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^
7090 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) ||
7091 nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^
77a92189 7092 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF))
7b225d0b 7093 goto err_element_clash;
8aeff920
PNA
7094 if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
7095 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) &&
7096 memcmp(nft_set_ext_data(ext),
7097 nft_set_ext_data(ext2), set->dlen) != 0) ||
7098 (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
7099 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) &&
7100 *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2)))
77a92189 7101 goto err_element_clash;
c016c7e4
PNA
7102 else if (!(nlmsg_flags & NLM_F_EXCL))
7103 err = 0;
8c2d45b2
PNA
7104 } else if (err == -ENOTEMPTY) {
7105 /* ENOTEMPTY reports overlapping between this element
7106 * and an existing one.
7107 */
7108 err = -EEXIST;
c016c7e4 7109 }
7b225d0b 7110 goto err_element_clash;
c016c7e4 7111 }
20a69341 7112
d4b7f29e
FW
7113 if (!(flags & NFT_SET_ELEM_CATCHALL)) {
7114 unsigned int max = set->size ? set->size + set->ndeact : UINT_MAX;
7115
7116 if (!atomic_add_unless(&set->nelems, 1, max)) {
7117 err = -ENFILE;
7118 goto err_set_full;
7119 }
35d0ac90
PNA
7120 }
7121
0e1ea651 7122 nft_trans_elem_priv(trans) = elem.priv;
0854db2a 7123 nft_trans_commit_list_add_tail(ctx->net, trans);
20a69341
PM
7124 return 0;
7125
7b225d0b 7126err_set_full:
0e1ea651 7127 nft_setelem_remove(ctx->net, set, elem.priv);
7b225d0b 7128err_element_clash:
60319eb1 7129 kfree(trans);
34aae2c2 7130err_elem_free:
b389139f 7131 nf_tables_set_elem_destroy(ctx, set, elem.priv);
7b225d0b 7132err_parse_data:
20a69341 7133 if (nla[NFTA_SET_ELEM_DATA] != NULL)
fdb9c405 7134 nft_data_release(&elem.data.val, desc.type);
7b225d0b 7135err_parse_key_end:
1689f259
PNA
7136 if (obj)
7137 nft_use_dec_restore(&obj->use);
7138
7b225d0b
PNA
7139 nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
7140err_parse_key:
20a1452c 7141 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
40944452 7142err_set_elem_expr:
664899e8 7143 for (i = 0; i < num_exprs && expr_array[i]; i++)
8cfd9b0f
PNA
7144 nft_expr_destroy(ctx, expr_array[i]);
7145err_set_elem_expr_clone:
20a69341
PM
7146 return err;
7147}
7148
7dab8ee3
PNA
7149static int nf_tables_newsetelem(struct sk_buff *skb,
7150 const struct nfnl_info *info,
7151 const struct nlattr * const nla[])
20a69341 7152{
7dab8ee3
PNA
7153 struct netlink_ext_ack *extack = info->extack;
7154 u8 genmask = nft_genmask_next(info->net);
e2b750d7 7155 u8 family = info->nfmsg->nfgen_family;
7dab8ee3 7156 struct net *net = info->net;
20a69341 7157 const struct nlattr *attr;
e2b750d7 7158 struct nft_table *table;
20a69341
PM
7159 struct nft_set *set;
7160 struct nft_ctx ctx;
a654de8f 7161 int rem, err;
20a69341 7162
7d5570ca
PNA
7163 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
7164 return -EINVAL;
7165
e2b750d7
PNA
7166 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
7167 genmask, NETLINK_CB(skb).portid);
7168 if (IS_ERR(table)) {
7169 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
7170 return PTR_ERR(table);
7171 }
20a69341 7172
e2b750d7 7173 set = nft_set_lookup_global(net, table, nla[NFTA_SET_ELEM_LIST_SET],
a3073c17 7174 nla[NFTA_SET_ELEM_LIST_SET_ID], genmask);
aee1f692
PNA
7175 if (IS_ERR(set)) {
7176 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_SET]);
a3073c17 7177 return PTR_ERR(set);
aee1f692 7178 }
958bee14 7179
c88c535b
PNA
7180 if (!list_empty(&set->bindings) &&
7181 (set->flags & (NFT_SET_CONSTANT | NFT_SET_ANONYMOUS)))
20a69341
PM
7182 return -EBUSY;
7183
e2b750d7
PNA
7184 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7185
20a69341 7186 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
7dab8ee3 7187 err = nft_add_set_elem(&ctx, set, attr, info->nlh->nlmsg_flags);
b53c1166
PNA
7188 if (err < 0) {
7189 NL_SET_BAD_ATTR(extack, attr);
a654de8f 7190 return err;
b53c1166 7191 }
20a69341 7192 }
a654de8f 7193
00c320f9 7194 if (table->validate_state == NFT_VALIDATE_DO)
e2b750d7 7195 return nft_table_validate(net, table);
a654de8f
PNA
7196
7197 return 0;
20a69341
PM
7198}
7199
59105446
PNA
7200/**
7201 * nft_data_hold - hold a nft_data item
7202 *
7203 * @data: struct nft_data to release
7204 * @type: type of data
7205 *
7206 * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded,
7207 * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and
7208 * NFT_GOTO verdicts. This function must be called on active data objects
7209 * from the second phase of the commit protocol.
7210 */
bb7b40ae 7211void nft_data_hold(const struct nft_data *data, enum nft_data_types type)
59105446 7212{
d0e2c7de 7213 struct nft_chain *chain;
d0e2c7de 7214
59105446
PNA
7215 if (type == NFT_DATA_VERDICT) {
7216 switch (data->verdict.code) {
7217 case NFT_JUMP:
7218 case NFT_GOTO:
d0e2c7de 7219 chain = data->verdict.chain;
1689f259 7220 nft_use_inc_restore(&chain->use);
59105446
PNA
7221 break;
7222 }
7223 }
7224}
7225
86a1471d
PNA
7226static int nft_setelem_active_next(const struct net *net,
7227 const struct nft_set *set,
7228 struct nft_elem_priv *elem_priv)
7229{
7230 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
7231 u8 genmask = nft_genmask_next(net);
7232
7233 return nft_set_elem_active(ext, genmask);
7234}
7235
f8bb7889
PNA
7236static void nft_setelem_data_activate(const struct net *net,
7237 const struct nft_set *set,
0e1ea651 7238 struct nft_elem_priv *elem_priv)
59105446 7239{
0e1ea651 7240 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
59105446
PNA
7241
7242 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
7243 nft_data_hold(nft_set_ext_data(ext), set->dtype);
7244 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
1689f259 7245 nft_use_inc_restore(&(*nft_set_ext_obj(ext))->use);
59105446
PNA
7246}
7247
5f68718b
PNA
7248void nft_setelem_data_deactivate(const struct net *net,
7249 const struct nft_set *set,
0e1ea651 7250 struct nft_elem_priv *elem_priv)
59105446 7251{
0e1ea651 7252 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
59105446
PNA
7253
7254 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
7255 nft_data_release(nft_set_ext_data(ext), set->dtype);
7256 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
1689f259 7257 nft_use_dec(&(*nft_set_ext_obj(ext))->use);
59105446
PNA
7258}
7259
60319eb1 7260static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
20a69341
PM
7261 const struct nlattr *attr)
7262{
7263 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3971ca14 7264 struct nft_set_ext_tmpl tmpl;
20a69341 7265 struct nft_set_elem elem;
3971ca14 7266 struct nft_set_ext *ext;
60319eb1 7267 struct nft_trans *trans;
3971ca14 7268 u32 flags = 0;
20a69341
PM
7269 int err;
7270
8cb08174
JB
7271 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
7272 nft_set_elem_policy, NULL);
20a69341 7273 if (err < 0)
20a1452c 7274 return err;
20a69341 7275
aaa31047
PNA
7276 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
7277 if (err < 0)
7278 return err;
7279
7280 if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL))
20a1452c 7281 return -EINVAL;
20a69341 7282
88cccd90
PNA
7283 if (!nft_setelem_valid_key_end(set, nla, flags))
7284 return -EINVAL;
7285
3971ca14
PNA
7286 nft_set_ext_prepare(&tmpl);
7287
c39ba4de
PNA
7288 if (flags != 0) {
7289 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
7290 if (err < 0)
7291 return err;
7292 }
3971ca14 7293
aaa31047
PNA
7294 if (nla[NFTA_SET_ELEM_KEY]) {
7295 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
7296 nla[NFTA_SET_ELEM_KEY]);
7297 if (err < 0)
7298 return err;
20a69341 7299
c39ba4de
PNA
7300 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
7301 if (err < 0)
7302 goto fail_elem;
aaa31047 7303 }
3971ca14 7304
7b225d0b
PNA
7305 if (nla[NFTA_SET_ELEM_KEY_END]) {
7306 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
7307 nla[NFTA_SET_ELEM_KEY_END]);
7308 if (err < 0)
c39ba4de 7309 goto fail_elem;
7b225d0b 7310
c39ba4de
PNA
7311 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
7312 if (err < 0)
7313 goto fail_elem_key_end;
7b225d0b
PNA
7314 }
7315
3971ca14 7316 err = -ENOMEM;
7b225d0b
PNA
7317 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
7318 elem.key_end.val.data, NULL, 0, 0,
33758c89 7319 GFP_KERNEL_ACCOUNT);
34aae2c2
PNA
7320 if (IS_ERR(elem.priv)) {
7321 err = PTR_ERR(elem.priv);
c39ba4de 7322 goto fail_elem_key_end;
34aae2c2 7323 }
3971ca14
PNA
7324
7325 ext = nft_set_elem_ext(set, elem.priv);
7326 if (flags)
7327 *nft_set_ext_flags(ext) = flags;
7328
60319eb1 7329 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
20a1452c
PNA
7330 if (trans == NULL)
7331 goto fail_trans;
20a69341 7332
aaa31047
PNA
7333 err = nft_setelem_deactivate(ctx->net, set, &elem, flags);
7334 if (err < 0)
20a1452c 7335 goto fail_ops;
cc02e457 7336
0e1ea651 7337 nft_setelem_data_deactivate(ctx->net, set, elem.priv);
59105446 7338
0e1ea651 7339 nft_trans_elem_priv(trans) = elem.priv;
0854db2a 7340 nft_trans_commit_list_add_tail(ctx->net, trans);
0dc13625 7341 return 0;
cc02e457 7342
20a1452c 7343fail_ops:
cc02e457 7344 kfree(trans);
20a1452c 7345fail_trans:
3971ca14 7346 kfree(elem.priv);
c39ba4de
PNA
7347fail_elem_key_end:
7348 nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
20a1452c
PNA
7349fail_elem:
7350 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
20a69341
PM
7351 return err;
7352}
7353
e6ba7cb6
PNA
7354static int nft_setelem_flush(const struct nft_ctx *ctx,
7355 struct nft_set *set,
7356 const struct nft_set_iter *iter,
0e1ea651 7357 struct nft_elem_priv *elem_priv)
8411b644 7358{
e79b47a8 7359 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
8411b644 7360 struct nft_trans *trans;
8411b644 7361
e79b47a8
PNA
7362 if (!nft_set_elem_active(ext, iter->genmask))
7363 return 0;
7364
8411b644
PNA
7365 trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
7366 sizeof(struct nft_trans_elem), GFP_ATOMIC);
7367 if (!trans)
7368 return -ENOMEM;
7369
0e1ea651 7370 set->ops->flush(ctx->net, set, elem_priv);
b2c11e4b 7371 set->ndeact++;
8411b644 7372
0e1ea651 7373 nft_setelem_data_deactivate(ctx->net, set, elem_priv);
de70185d 7374 nft_trans_elem_set(trans) = set;
0e1ea651 7375 nft_trans_elem_priv(trans) = elem_priv;
0854db2a 7376 nft_trans_commit_list_add_tail(ctx->net, trans);
8411b644
PNA
7377
7378 return 0;
8411b644
PNA
7379}
7380
aaa31047
PNA
7381static int __nft_set_catchall_flush(const struct nft_ctx *ctx,
7382 struct nft_set *set,
0e1ea651 7383 struct nft_elem_priv *elem_priv)
aaa31047
PNA
7384{
7385 struct nft_trans *trans;
7386
7387 trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
7388 sizeof(struct nft_trans_elem), GFP_KERNEL);
7389 if (!trans)
7390 return -ENOMEM;
7391
0e1ea651 7392 nft_setelem_data_deactivate(ctx->net, set, elem_priv);
aaa31047 7393 nft_trans_elem_set(trans) = set;
0e1ea651 7394 nft_trans_elem_priv(trans) = elem_priv;
aaa31047
PNA
7395 nft_trans_commit_list_add_tail(ctx->net, trans);
7396
7397 return 0;
7398}
7399
7400static int nft_set_catchall_flush(const struct nft_ctx *ctx,
7401 struct nft_set *set)
7402{
7403 u8 genmask = nft_genmask_next(ctx->net);
7404 struct nft_set_elem_catchall *catchall;
aaa31047
PNA
7405 struct nft_set_ext *ext;
7406 int ret = 0;
7407
7408 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
7409 ext = nft_set_elem_ext(set, catchall->elem);
f6c383b8 7410 if (!nft_set_elem_active(ext, genmask))
aaa31047
PNA
7411 continue;
7412
0e1ea651 7413 ret = __nft_set_catchall_flush(ctx, set, catchall->elem);
aaa31047
PNA
7414 if (ret < 0)
7415 break;
90e5b346 7416 nft_set_elem_change_active(ctx->net, set, ext);
aaa31047
PNA
7417 }
7418
7419 return ret;
7420}
7421
e6ba7cb6
PNA
7422static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask)
7423{
7424 struct nft_set_iter iter = {
7425 .genmask = genmask,
29b359cf 7426 .type = NFT_ITER_UPDATE,
e6ba7cb6
PNA
7427 .fn = nft_setelem_flush,
7428 };
7429
7430 set->ops->walk(ctx, set, &iter);
aaa31047
PNA
7431 if (!iter.err)
7432 iter.err = nft_set_catchall_flush(ctx, set);
e6ba7cb6
PNA
7433
7434 return iter.err;
7435}
7436
7dab8ee3
PNA
7437static int nf_tables_delsetelem(struct sk_buff *skb,
7438 const struct nfnl_info *info,
7439 const struct nlattr * const nla[])
20a69341 7440{
7dab8ee3
PNA
7441 struct netlink_ext_ack *extack = info->extack;
7442 u8 genmask = nft_genmask_next(info->net);
e2b750d7 7443 u8 family = info->nfmsg->nfgen_family;
7dab8ee3 7444 struct net *net = info->net;
20a69341 7445 const struct nlattr *attr;
e2b750d7 7446 struct nft_table *table;
20a69341
PM
7447 struct nft_set *set;
7448 struct nft_ctx ctx;
60319eb1 7449 int rem, err = 0;
20a69341 7450
e2b750d7
PNA
7451 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
7452 genmask, NETLINK_CB(skb).portid);
7453 if (IS_ERR(table)) {
7454 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
7455 return PTR_ERR(table);
7456 }
20a69341 7457
e2b750d7 7458 set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
aee1f692
PNA
7459 if (IS_ERR(set)) {
7460 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_SET]);
20a69341 7461 return PTR_ERR(set);
aee1f692 7462 }
c88c535b 7463
23a3bfd4
PNA
7464 if (nft_set_is_anonymous(set))
7465 return -EOPNOTSUPP;
7466
7467 if (!list_empty(&set->bindings) && (set->flags & NFT_SET_CONSTANT))
20a69341
PM
7468 return -EBUSY;
7469
e2b750d7
PNA
7470 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7471
e6ba7cb6
PNA
7472 if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
7473 return nft_set_flush(&ctx, set, genmask);
8411b644 7474
20a69341
PM
7475 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
7476 err = nft_del_setelem(&ctx, set, attr);
f80a612d
FFM
7477 if (err == -ENOENT &&
7478 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSETELEM)
7479 continue;
7480
b53c1166
PNA
7481 if (err < 0) {
7482 NL_SET_BAD_ATTR(extack, attr);
a7d5a955 7483 return err;
b53c1166 7484 }
20a69341 7485 }
a7d5a955
PNA
7486
7487 return 0;
20a69341
PM
7488}
7489
e5009240
PNA
7490/*
7491 * Stateful objects
7492 */
7493
7494/**
7495 * nft_register_obj- register nf_tables stateful object type
3db86c39 7496 * @obj_type: object type
e5009240
PNA
7497 *
7498 * Registers the object type for use with nf_tables. Returns zero on
7499 * success or a negative errno code otherwise.
7500 */
7501int nft_register_obj(struct nft_object_type *obj_type)
7502{
7503 if (obj_type->type == NFT_OBJECT_UNSPEC)
7504 return -EINVAL;
7505
7506 nfnl_lock(NFNL_SUBSYS_NFTABLES);
7507 list_add_rcu(&obj_type->list, &nf_tables_objects);
7508 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
7509 return 0;
7510}
7511EXPORT_SYMBOL_GPL(nft_register_obj);
7512
7513/**
7514 * nft_unregister_obj - unregister nf_tables object type
3db86c39 7515 * @obj_type: object type
e5009240
PNA
7516 *
7517 * Unregisters the object type for use with nf_tables.
7518 */
7519void nft_unregister_obj(struct nft_object_type *obj_type)
7520{
7521 nfnl_lock(NFNL_SUBSYS_NFTABLES);
7522 list_del_rcu(&obj_type->list);
7523 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
7524}
7525EXPORT_SYMBOL_GPL(nft_unregister_obj);
7526
4d44175a
FW
7527struct nft_object *nft_obj_lookup(const struct net *net,
7528 const struct nft_table *table,
cac20fcd
PNA
7529 const struct nlattr *nla, u32 objtype,
7530 u8 genmask)
e5009240 7531{
4d44175a
FW
7532 struct nft_object_hash_key k = { .table = table };
7533 char search[NFT_OBJ_MAXNAMELEN];
7534 struct rhlist_head *tmp, *list;
e5009240
PNA
7535 struct nft_object *obj;
7536
872f6903 7537 nla_strscpy(search, nla, sizeof(search));
4d44175a
FW
7538 k.name = search;
7539
7540 WARN_ON_ONCE(!rcu_read_lock_held() &&
7541 !lockdep_commit_lock_is_held(net));
7542
7543 rcu_read_lock();
7544 list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params);
7545 if (!list)
7546 goto out;
7547
7548 rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) {
7549 if (objtype == obj->ops->type->type &&
7550 nft_active_genmask(obj, genmask)) {
7551 rcu_read_unlock();
e5009240 7552 return obj;
4d44175a 7553 }
e5009240 7554 }
4d44175a
FW
7555out:
7556 rcu_read_unlock();
e5009240
PNA
7557 return ERR_PTR(-ENOENT);
7558}
cac20fcd 7559EXPORT_SYMBOL_GPL(nft_obj_lookup);
e5009240 7560
cac20fcd
PNA
7561static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table,
7562 const struct nlattr *nla,
7563 u32 objtype, u8 genmask)
3ecbfd65
HS
7564{
7565 struct nft_object *obj;
7566
7567 list_for_each_entry(obj, &table->objects, list) {
7568 if (be64_to_cpu(nla_get_be64(nla)) == obj->handle &&
7569 objtype == obj->ops->type->type &&
7570 nft_active_genmask(obj, genmask))
7571 return obj;
7572 }
7573 return ERR_PTR(-ENOENT);
7574}
7575
e5009240 7576static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = {
b2fbd044
LZ
7577 [NFTA_OBJ_TABLE] = { .type = NLA_STRING,
7578 .len = NFT_TABLE_MAXNAMELEN - 1 },
7579 [NFTA_OBJ_NAME] = { .type = NLA_STRING,
7580 .len = NFT_OBJ_MAXNAMELEN - 1 },
e5009240
PNA
7581 [NFTA_OBJ_TYPE] = { .type = NLA_U32 },
7582 [NFTA_OBJ_DATA] = { .type = NLA_NESTED },
3ecbfd65 7583 [NFTA_OBJ_HANDLE] = { .type = NLA_U64},
b131c964
JGG
7584 [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY,
7585 .len = NFT_USERDATA_MAXLEN },
e5009240
PNA
7586};
7587
84fba055
FW
7588static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
7589 const struct nft_object_type *type,
e5009240
PNA
7590 const struct nlattr *attr)
7591{
5b4c6e38 7592 struct nlattr **tb;
dfc46034 7593 const struct nft_object_ops *ops;
e5009240 7594 struct nft_object *obj;
5b4c6e38
GS
7595 int err = -ENOMEM;
7596
7597 tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL);
7598 if (!tb)
7599 goto err1;
e5009240
PNA
7600
7601 if (attr) {
8cb08174
JB
7602 err = nla_parse_nested_deprecated(tb, type->maxattr, attr,
7603 type->policy, NULL);
e5009240 7604 if (err < 0)
5b4c6e38 7605 goto err2;
e5009240
PNA
7606 } else {
7607 memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1));
7608 }
7609
dfc46034
PBG
7610 if (type->select_ops) {
7611 ops = type->select_ops(ctx, (const struct nlattr * const *)tb);
7612 if (IS_ERR(ops)) {
7613 err = PTR_ERR(ops);
5b4c6e38 7614 goto err2;
dfc46034
PBG
7615 }
7616 } else {
7617 ops = type->ops;
7618 }
7619
e5009240 7620 err = -ENOMEM;
33758c89 7621 obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL_ACCOUNT);
5b4c6e38
GS
7622 if (!obj)
7623 goto err2;
e5009240 7624
dfc46034 7625 err = ops->init(ctx, (const struct nlattr * const *)tb, obj);
e5009240 7626 if (err < 0)
5b4c6e38 7627 goto err3;
e5009240 7628
dfc46034
PBG
7629 obj->ops = ops;
7630
5b4c6e38 7631 kfree(tb);
e5009240 7632 return obj;
5b4c6e38 7633err3:
e5009240 7634 kfree(obj);
5b4c6e38
GS
7635err2:
7636 kfree(tb);
e5009240
PNA
7637err1:
7638 return ERR_PTR(err);
7639}
7640
7641static int nft_object_dump(struct sk_buff *skb, unsigned int attr,
43da04a5 7642 struct nft_object *obj, bool reset)
e5009240
PNA
7643{
7644 struct nlattr *nest;
7645
ae0be8de 7646 nest = nla_nest_start_noflag(skb, attr);
e5009240
PNA
7647 if (!nest)
7648 goto nla_put_failure;
dfc46034 7649 if (obj->ops->dump(skb, obj, reset) < 0)
e5009240
PNA
7650 goto nla_put_failure;
7651 nla_nest_end(skb, nest);
7652 return 0;
7653
7654nla_put_failure:
7655 return -1;
7656}
7657
776d4516 7658static const struct nft_object_type *__nft_obj_type_get(u32 objtype, u8 family)
e5009240
PNA
7659{
7660 const struct nft_object_type *type;
7661
d78d867d 7662 list_for_each_entry_rcu(type, &nf_tables_objects, list) {
776d4516
PNA
7663 if (type->family != NFPROTO_UNSPEC &&
7664 type->family != family)
7665 continue;
7666
e5009240
PNA
7667 if (objtype == type->type)
7668 return type;
7669 }
7670 return NULL;
7671}
7672
452238e8 7673static const struct nft_object_type *
776d4516 7674nft_obj_type_get(struct net *net, u32 objtype, u8 family)
e5009240
PNA
7675{
7676 const struct nft_object_type *type;
7677
d78d867d 7678 rcu_read_lock();
776d4516 7679 type = __nft_obj_type_get(objtype, family);
d78d867d
ZX
7680 if (type != NULL && try_module_get(type->owner)) {
7681 rcu_read_unlock();
e5009240 7682 return type;
d78d867d
ZX
7683 }
7684 rcu_read_unlock();
e5009240 7685
f102d66b 7686 lockdep_nfnl_nft_mutex_not_held();
e5009240
PNA
7687#ifdef CONFIG_MODULES
7688 if (type == NULL) {
eb014de4 7689 if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN)
e5009240
PNA
7690 return ERR_PTR(-EAGAIN);
7691 }
7692#endif
7693 return ERR_PTR(-ENOENT);
7694}
7695
d62d0ba9
FFM
7696static int nf_tables_updobj(const struct nft_ctx *ctx,
7697 const struct nft_object_type *type,
7698 const struct nlattr *attr,
7699 struct nft_object *obj)
7700{
7701 struct nft_object *newobj;
7702 struct nft_trans *trans;
dad3bdee
FW
7703 int err = -ENOMEM;
7704
7705 if (!try_module_get(type->owner))
7706 return -ENOENT;
d62d0ba9
FFM
7707
7708 trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ,
7709 sizeof(struct nft_trans_obj));
7710 if (!trans)
dad3bdee 7711 goto err_trans;
d62d0ba9
FFM
7712
7713 newobj = nft_obj_init(ctx, type, attr);
7714 if (IS_ERR(newobj)) {
7715 err = PTR_ERR(newobj);
b74ae961 7716 goto err_free_trans;
d62d0ba9
FFM
7717 }
7718
7719 nft_trans_obj(trans) = obj;
7720 nft_trans_obj_update(trans) = true;
7721 nft_trans_obj_newobj(trans) = newobj;
0854db2a 7722 nft_trans_commit_list_add_tail(ctx->net, trans);
d62d0ba9
FFM
7723
7724 return 0;
b74ae961
DC
7725
7726err_free_trans:
d62d0ba9 7727 kfree(trans);
dad3bdee
FW
7728err_trans:
7729 module_put(type->owner);
d62d0ba9
FFM
7730 return err;
7731}
7732
7dab8ee3
PNA
7733static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info,
7734 const struct nlattr * const nla[])
e5009240 7735{
7dab8ee3
PNA
7736 struct netlink_ext_ack *extack = info->extack;
7737 u8 genmask = nft_genmask_next(info->net);
ef4b65e5 7738 u8 family = info->nfmsg->nfgen_family;
e5009240 7739 const struct nft_object_type *type;
7dab8ee3 7740 struct net *net = info->net;
e5009240
PNA
7741 struct nft_table *table;
7742 struct nft_object *obj;
7743 struct nft_ctx ctx;
7744 u32 objtype;
7745 int err;
7746
7747 if (!nla[NFTA_OBJ_TYPE] ||
7748 !nla[NFTA_OBJ_NAME] ||
7749 !nla[NFTA_OBJ_DATA])
7750 return -EINVAL;
7751
6001a930
PNA
7752 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask,
7753 NETLINK_CB(skb).portid);
36dd1bcc
PNA
7754 if (IS_ERR(table)) {
7755 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
e5009240 7756 return PTR_ERR(table);
36dd1bcc 7757 }
e5009240
PNA
7758
7759 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
4d44175a 7760 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
e5009240
PNA
7761 if (IS_ERR(obj)) {
7762 err = PTR_ERR(obj);
36dd1bcc
PNA
7763 if (err != -ENOENT) {
7764 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
e5009240 7765 return err;
36dd1bcc 7766 }
1a28ad74 7767 } else {
7dab8ee3 7768 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
36dd1bcc 7769 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
e5009240 7770 return -EEXIST;
36dd1bcc 7771 }
7dab8ee3 7772 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
d62d0ba9
FFM
7773 return -EOPNOTSUPP;
7774
776d4516 7775 type = __nft_obj_type_get(objtype, family);
dac7f50a
AT
7776 if (WARN_ON_ONCE(!type))
7777 return -ENOENT;
7778
84b1a0c0
PNA
7779 if (!obj->ops->update)
7780 return 0;
7781
7dab8ee3 7782 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
d62d0ba9
FFM
7783
7784 return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj);
e5009240
PNA
7785 }
7786
7dab8ee3 7787 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
e5009240 7788
1689f259
PNA
7789 if (!nft_use_inc(&table->use))
7790 return -EMFILE;
7791
776d4516 7792 type = nft_obj_type_get(net, objtype, family);
1689f259
PNA
7793 if (IS_ERR(type)) {
7794 err = PTR_ERR(type);
7795 goto err_type;
7796 }
e5009240 7797
84fba055 7798 obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]);
e5009240
PNA
7799 if (IS_ERR(obj)) {
7800 err = PTR_ERR(obj);
b131c964 7801 goto err_init;
e5009240 7802 }
d152159b 7803 obj->key.table = table;
3ecbfd65
HS
7804 obj->handle = nf_tables_alloc_handle(table);
7805
33758c89 7806 obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL_ACCOUNT);
d152159b 7807 if (!obj->key.name) {
61509575 7808 err = -ENOMEM;
b131c964
JGG
7809 goto err_strdup;
7810 }
7811
7812 if (nla[NFTA_OBJ_USERDATA]) {
af0acf22 7813 obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL_ACCOUNT);
b131c964
JGG
7814 if (obj->udata == NULL)
7815 goto err_userdata;
7816
85db827a 7817 obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]);
61509575 7818 }
e5009240
PNA
7819
7820 err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj);
7821 if (err < 0)
b131c964 7822 goto err_trans;
e5009240 7823
4d44175a
FW
7824 err = rhltable_insert(&nft_objname_ht, &obj->rhlhead,
7825 nft_objname_ht_params);
7826 if (err < 0)
b131c964 7827 goto err_obj_ht;
4d44175a 7828
e5009240 7829 list_add_tail_rcu(&obj->list, &table->objects);
1689f259 7830
e5009240 7831 return 0;
b131c964 7832err_obj_ht:
4d44175a
FW
7833 /* queued in transaction log */
7834 INIT_LIST_HEAD(&obj->list);
7835 return err;
b131c964 7836err_trans:
b131c964 7837 kfree(obj->udata);
85dfd816
PNA
7838err_userdata:
7839 kfree(obj->key.name);
b131c964 7840err_strdup:
dfc46034 7841 if (obj->ops->destroy)
00bfb320 7842 obj->ops->destroy(&ctx, obj);
e5009240 7843 kfree(obj);
b131c964 7844err_init:
e5009240 7845 module_put(type->owner);
1689f259
PNA
7846err_type:
7847 nft_use_dec_restore(&table->use);
7848
e5009240
PNA
7849 return err;
7850}
7851
7852static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net,
7853 u32 portid, u32 seq, int event, u32 flags,
7854 int family, const struct nft_table *table,
43da04a5 7855 struct nft_object *obj, bool reset)
e5009240 7856{
e5009240
PNA
7857 struct nlmsghdr *nlh;
7858
dedb67c4 7859 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
19c28b13
PNA
7860 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
7861 NFNETLINK_V0, nft_base_seq(net));
7862 if (!nlh)
e5009240
PNA
7863 goto nla_put_failure;
7864
e5009240 7865 if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) ||
d152159b 7866 nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) ||
3ecbfd65
HS
7867 nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle),
7868 NFTA_OBJ_PAD))
e5009240
PNA
7869 goto nla_put_failure;
7870
28339b21
PNA
7871 if (event == NFT_MSG_DELOBJ) {
7872 nlmsg_end(skb, nlh);
7873 return 0;
7874 }
7875
7876 if (nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) ||
7877 nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) ||
7878 nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset))
7879 goto nla_put_failure;
7880
b131c964
JGG
7881 if (obj->udata &&
7882 nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata))
7883 goto nla_put_failure;
7884
e5009240
PNA
7885 nlmsg_end(skb, nlh);
7886 return 0;
7887
7888nla_put_failure:
7889 nlmsg_trim(skb, nlh);
7890 return -1;
7891}
7892
1baf0152
PS
7893static void audit_log_obj_reset(const struct nft_table *table,
7894 unsigned int base_seq, unsigned int nentries)
7895{
7896 char *buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, base_seq);
7897
7898 audit_log_nfcfg(buf, table->family, nentries,
7899 AUDIT_NFT_OP_OBJ_RESET, GFP_ATOMIC);
7900 kfree(buf);
7901}
7902
ecf49cad 7903struct nft_obj_dump_ctx {
2eda95cf 7904 unsigned int s_idx;
e46abbcc 7905 char *table;
a9fea2a3 7906 u32 type;
a5523390 7907 bool reset;
a9fea2a3
PNA
7908};
7909
e5009240
PNA
7910static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)
7911{
7912 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
5a893b9c 7913 struct nft_obj_dump_ctx *ctx = (void *)cb->ctx;
e5009240
PNA
7914 struct net *net = sock_net(skb->sk);
7915 int family = nfmsg->nfgen_family;
0854db2a 7916 struct nftables_pernet *nft_net;
2eda95cf 7917 const struct nft_table *table;
1baf0152 7918 unsigned int entries = 0;
43da04a5 7919 struct nft_object *obj;
2eda95cf 7920 unsigned int idx = 0;
1baf0152 7921 int rc = 0;
43da04a5 7922
e5009240 7923 rcu_read_lock();
d59d2f82 7924 nft_net = nft_pernet(net);
34002783 7925 cb->seq = READ_ONCE(nft_net->base_seq);
e5009240 7926
0854db2a 7927 list_for_each_entry_rcu(table, &nft_net->tables, list) {
98319cb9 7928 if (family != NFPROTO_UNSPEC && family != table->family)
e5009240
PNA
7929 continue;
7930
1baf0152 7931 entries = 0;
36596dad
PNA
7932 list_for_each_entry_rcu(obj, &table->objects, list) {
7933 if (!nft_is_active(net, obj))
7934 goto cont;
2eda95cf 7935 if (idx < ctx->s_idx)
36596dad 7936 goto cont;
ecf49cad 7937 if (ctx->table && strcmp(ctx->table, table->name))
36596dad 7938 goto cont;
ecf49cad
PS
7939 if (ctx->type != NFT_OBJECT_UNSPEC &&
7940 obj->ops->type->type != ctx->type)
36596dad 7941 goto cont;
8e6cf365 7942
1baf0152
PS
7943 rc = nf_tables_fill_obj_info(skb, net,
7944 NETLINK_CB(cb->skb).portid,
7945 cb->nlh->nlmsg_seq,
7946 NFT_MSG_NEWOBJ,
7947 NLM_F_MULTI | NLM_F_APPEND,
7948 table->family, table,
a5523390 7949 obj, ctx->reset);
1baf0152
PS
7950 if (rc < 0)
7951 break;
e5009240 7952
1baf0152 7953 entries++;
36596dad 7954 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
e5009240 7955cont:
36596dad 7956 idx++;
e5009240 7957 }
a5523390 7958 if (ctx->reset && entries)
1baf0152
PS
7959 audit_log_obj_reset(table, nft_net->base_seq, entries);
7960 if (rc < 0)
7961 break;
e5009240 7962 }
e5009240
PNA
7963 rcu_read_unlock();
7964
2eda95cf 7965 ctx->s_idx = idx;
e5009240
PNA
7966 return skb->len;
7967}
7968
90fd131a 7969static int nf_tables_dump_obj_start(struct netlink_callback *cb)
a9fea2a3 7970{
5a893b9c 7971 struct nft_obj_dump_ctx *ctx = (void *)cb->ctx;
90fd131a 7972 const struct nlattr * const *nla = cb->data;
e46abbcc 7973
5a893b9c 7974 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
90fd131a 7975
4279cc60 7976 if (nla[NFTA_OBJ_TABLE]) {
ecf49cad 7977 ctx->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC);
5a893b9c 7978 if (!ctx->table)
4279cc60 7979 return -ENOMEM;
8bea728d 7980 }
a9fea2a3 7981
4279cc60 7982 if (nla[NFTA_OBJ_TYPE])
ecf49cad 7983 ctx->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
4279cc60 7984
a5523390
PS
7985 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
7986 ctx->reset = true;
7987
a9fea2a3
PNA
7988 return 0;
7989}
7990
90fd131a 7991static int nf_tables_dump_obj_done(struct netlink_callback *cb)
a9fea2a3 7992{
5a893b9c 7993 struct nft_obj_dump_ctx *ctx = (void *)cb->ctx;
a9fea2a3 7994
ecf49cad 7995 kfree(ctx->table);
a9fea2a3 7996
90fd131a 7997 return 0;
a9fea2a3
PNA
7998}
7999
d9adf22a 8000/* called with rcu_read_lock held */
797d4980
PNA
8001static int nf_tables_getobj(struct sk_buff *skb, const struct nfnl_info *info,
8002 const struct nlattr * const nla[])
e5009240 8003{
797d4980
PNA
8004 struct netlink_ext_ack *extack = info->extack;
8005 u8 genmask = nft_genmask_cur(info->net);
ef4b65e5 8006 u8 family = info->nfmsg->nfgen_family;
e5009240 8007 const struct nft_table *table;
797d4980 8008 struct net *net = info->net;
e5009240
PNA
8009 struct nft_object *obj;
8010 struct sk_buff *skb2;
43da04a5 8011 bool reset = false;
e5009240
PNA
8012 u32 objtype;
8013 int err;
8014
797d4980 8015 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
e5009240 8016 struct netlink_dump_control c = {
90fd131a 8017 .start = nf_tables_dump_obj_start,
e5009240 8018 .dump = nf_tables_dump_obj,
a9fea2a3 8019 .done = nf_tables_dump_obj_done,
d9adf22a 8020 .module = THIS_MODULE,
90fd131a 8021 .data = (void *)nla,
e5009240 8022 };
a9fea2a3 8023
797d4980 8024 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
e5009240
PNA
8025 }
8026
8027 if (!nla[NFTA_OBJ_NAME] ||
8028 !nla[NFTA_OBJ_TYPE])
8029 return -EINVAL;
8030
6001a930 8031 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, 0);
36dd1bcc
PNA
8032 if (IS_ERR(table)) {
8033 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
e5009240 8034 return PTR_ERR(table);
36dd1bcc 8035 }
e5009240
PNA
8036
8037 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
4d44175a 8038 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
36dd1bcc
PNA
8039 if (IS_ERR(obj)) {
8040 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
e5009240 8041 return PTR_ERR(obj);
36dd1bcc 8042 }
e5009240 8043
d9adf22a 8044 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
e5009240
PNA
8045 if (!skb2)
8046 return -ENOMEM;
8047
797d4980 8048 if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
43da04a5
PNA
8049 reset = true;
8050
8e6cf365 8051 if (reset) {
0854db2a
FW
8052 const struct nftables_pernet *nft_net;
8053 char *buf;
8054
d59d2f82 8055 nft_net = nft_pernet(net);
0854db2a 8056 buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, nft_net->base_seq);
8e6cf365
RGB
8057
8058 audit_log_nfcfg(buf,
8059 family,
1baf0152 8060 1,
14224039 8061 AUDIT_NFT_OP_OBJ_RESET,
68df2ed5 8062 GFP_ATOMIC);
8e6cf365
RGB
8063 kfree(buf);
8064 }
8065
e5009240 8066 err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid,
797d4980 8067 info->nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0,
43da04a5 8068 family, table, obj, reset);
e5009240 8069 if (err < 0)
ee921183 8070 goto err_fill_obj_info;
e5009240 8071
ee921183
PNA
8072 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
8073
8074err_fill_obj_info:
e5009240
PNA
8075 kfree_skb(skb2);
8076 return err;
e5009240
PNA
8077}
8078
00bfb320 8079static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj)
e5009240 8080{
dfc46034 8081 if (obj->ops->destroy)
00bfb320 8082 obj->ops->destroy(ctx, obj);
e5009240 8083
dfc46034 8084 module_put(obj->ops->type->owner);
d152159b 8085 kfree(obj->key.name);
bc7a7082 8086 kfree(obj->udata);
e5009240
PNA
8087 kfree(obj);
8088}
8089
7dab8ee3
PNA
8090static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info,
8091 const struct nlattr * const nla[])
e5009240 8092{
7dab8ee3
PNA
8093 struct netlink_ext_ack *extack = info->extack;
8094 u8 genmask = nft_genmask_next(info->net);
ef4b65e5 8095 u8 family = info->nfmsg->nfgen_family;
7dab8ee3 8096 struct net *net = info->net;
36dd1bcc 8097 const struct nlattr *attr;
e5009240
PNA
8098 struct nft_table *table;
8099 struct nft_object *obj;
8100 struct nft_ctx ctx;
8101 u32 objtype;
8102
8103 if (!nla[NFTA_OBJ_TYPE] ||
3ecbfd65 8104 (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE]))
e5009240
PNA
8105 return -EINVAL;
8106
6001a930
PNA
8107 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask,
8108 NETLINK_CB(skb).portid);
36dd1bcc
PNA
8109 if (IS_ERR(table)) {
8110 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
e5009240 8111 return PTR_ERR(table);
36dd1bcc 8112 }
e5009240
PNA
8113
8114 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
36dd1bcc
PNA
8115 if (nla[NFTA_OBJ_HANDLE]) {
8116 attr = nla[NFTA_OBJ_HANDLE];
8117 obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask);
8118 } else {
8119 attr = nla[NFTA_OBJ_NAME];
4d44175a 8120 obj = nft_obj_lookup(net, table, attr, objtype, genmask);
36dd1bcc
PNA
8121 }
8122
8123 if (IS_ERR(obj)) {
f80a612d
FFM
8124 if (PTR_ERR(obj) == -ENOENT &&
8125 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYOBJ)
8126 return 0;
8127
36dd1bcc 8128 NL_SET_BAD_ATTR(extack, attr);
e5009240 8129 return PTR_ERR(obj);
36dd1bcc
PNA
8130 }
8131 if (obj->use > 0) {
8132 NL_SET_BAD_ATTR(extack, attr);
e5009240 8133 return -EBUSY;
36dd1bcc 8134 }
e5009240 8135
7dab8ee3 8136 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
e5009240
PNA
8137
8138 return nft_delobj(&ctx, obj);
8139}
8140
0d880dc6
PS
8141static void
8142__nft_obj_notify(struct net *net, const struct nft_table *table,
8143 struct nft_object *obj, u32 portid, u32 seq, int event,
8144 u16 flags, int family, int report, gfp_t gfp)
e5009240 8145{
d59d2f82 8146 struct nftables_pernet *nft_net = nft_pernet(net);
e5009240
PNA
8147 struct sk_buff *skb;
8148 int err;
8149
2599e989
PNA
8150 if (!report &&
8151 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
25e94a99 8152 return;
e5009240 8153
2599e989 8154 skb = nlmsg_new(NLMSG_GOODSIZE, gfp);
e5009240
PNA
8155 if (skb == NULL)
8156 goto err;
8157
6fb721cf
PNA
8158 err = nf_tables_fill_obj_info(skb, net, portid, seq, event,
8159 flags & (NLM_F_CREATE | NLM_F_EXCL),
8160 family, table, obj, false);
e5009240
PNA
8161 if (err < 0) {
8162 kfree_skb(skb);
8163 goto err;
8164 }
8165
0854db2a 8166 nft_notify_enqueue(skb, report, &nft_net->notify_list);
25e94a99 8167 return;
e5009240 8168err:
25e94a99 8169 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
e5009240 8170}
0d880dc6
PS
8171
8172void nft_obj_notify(struct net *net, const struct nft_table *table,
8173 struct nft_object *obj, u32 portid, u32 seq, int event,
8174 u16 flags, int family, int report, gfp_t gfp)
8175{
8176 struct nftables_pernet *nft_net = nft_pernet(net);
8177 char *buf = kasprintf(gfp, "%s:%u",
8178 table->name, nft_net->base_seq);
8179
8180 audit_log_nfcfg(buf,
8181 family,
8182 obj->handle,
8183 event == NFT_MSG_NEWOBJ ?
8184 AUDIT_NFT_OP_OBJ_REGISTER :
8185 AUDIT_NFT_OP_OBJ_UNREGISTER,
8186 gfp);
8187 kfree(buf);
8188
8189 __nft_obj_notify(net, table, obj, portid, seq, event,
8190 flags, family, report, gfp);
8191}
2599e989
PNA
8192EXPORT_SYMBOL_GPL(nft_obj_notify);
8193
25e94a99
PNA
8194static void nf_tables_obj_notify(const struct nft_ctx *ctx,
8195 struct nft_object *obj, int event)
2599e989 8196{
0d880dc6
PS
8197 __nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid,
8198 ctx->seq, event, ctx->flags, ctx->family,
8199 ctx->report, GFP_KERNEL);
2599e989 8200}
e5009240 8201
3b49e2e9
PNA
8202/*
8203 * Flow tables
8204 */
8205void nft_register_flowtable_type(struct nf_flowtable_type *type)
8206{
8207 nfnl_lock(NFNL_SUBSYS_NFTABLES);
8208 list_add_tail_rcu(&type->list, &nf_tables_flowtables);
8209 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
8210}
8211EXPORT_SYMBOL_GPL(nft_register_flowtable_type);
8212
8213void nft_unregister_flowtable_type(struct nf_flowtable_type *type)
8214{
8215 nfnl_lock(NFNL_SUBSYS_NFTABLES);
8216 list_del_rcu(&type->list);
8217 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
8218}
8219EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type);
8220
8221static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = {
8222 [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING,
8223 .len = NFT_NAME_MAXLEN - 1 },
8224 [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING,
8225 .len = NFT_NAME_MAXLEN - 1 },
8226 [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED },
3ecbfd65 8227 [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 },
8bb69f3b 8228 [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 },
3b49e2e9
PNA
8229};
8230
cac20fcd
PNA
8231struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
8232 const struct nlattr *nla, u8 genmask)
3b49e2e9
PNA
8233{
8234 struct nft_flowtable *flowtable;
8235
d9adf22a 8236 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
3b49e2e9
PNA
8237 if (!nla_strcmp(nla, flowtable->name) &&
8238 nft_active_genmask(flowtable, genmask))
8239 return flowtable;
8240 }
8241 return ERR_PTR(-ENOENT);
8242}
cac20fcd 8243EXPORT_SYMBOL_GPL(nft_flowtable_lookup);
3b49e2e9 8244
9b05b6e1
LGL
8245void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
8246 struct nft_flowtable *flowtable,
8247 enum nft_trans_phase phase)
8248{
8249 switch (phase) {
26b5a571 8250 case NFT_TRANS_PREPARE_ERROR:
9b05b6e1
LGL
8251 case NFT_TRANS_PREPARE:
8252 case NFT_TRANS_ABORT:
8253 case NFT_TRANS_RELEASE:
1689f259 8254 nft_use_dec(&flowtable->use);
954d8297 8255 fallthrough;
9b05b6e1
LGL
8256 default:
8257 return;
8258 }
8259}
8260EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable);
8261
ae0662f8 8262static struct nft_flowtable *
cac20fcd
PNA
8263nft_flowtable_lookup_byhandle(const struct nft_table *table,
8264 const struct nlattr *nla, u8 genmask)
3ecbfd65
HS
8265{
8266 struct nft_flowtable *flowtable;
8267
8268 list_for_each_entry(flowtable, &table->flowtables, list) {
8269 if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle &&
8270 nft_active_genmask(flowtable, genmask))
8271 return flowtable;
8272 }
8273 return ERR_PTR(-ENOENT);
8274}
8275
d9246a53
PNA
8276struct nft_flowtable_hook {
8277 u32 num;
8278 int priority;
8279 struct list_head list;
8280};
8281
3b49e2e9
PNA
8282static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = {
8283 [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 },
8284 [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 },
8285 [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED },
8286};
8287
d9246a53 8288static int nft_flowtable_parse_hook(const struct nft_ctx *ctx,
8509f62b 8289 const struct nlattr * const nla[],
d9246a53 8290 struct nft_flowtable_hook *flowtable_hook,
c3c060ad
PNA
8291 struct nft_flowtable *flowtable,
8292 struct netlink_ext_ack *extack, bool add)
3b49e2e9 8293{
3b49e2e9 8294 struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1];
3f0465a9 8295 struct nft_hook *hook;
3b49e2e9 8296 int hooknum, priority;
3f0465a9 8297 int err;
3b49e2e9 8298
d9246a53
PNA
8299 INIT_LIST_HEAD(&flowtable_hook->list);
8300
8509f62b
PNA
8301 err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX,
8302 nla[NFTA_FLOWTABLE_HOOK],
8cb08174 8303 nft_flowtable_hook_policy, NULL);
3b49e2e9
PNA
8304 if (err < 0)
8305 return err;
8306
5b6743fb
PNA
8307 if (add) {
8308 if (!tb[NFTA_FLOWTABLE_HOOK_NUM] ||
8509f62b
PNA
8309 !tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) {
8310 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
8311 return -ENOENT;
8312 }
3b49e2e9 8313
5b6743fb
PNA
8314 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
8315 if (hooknum != NF_NETDEV_INGRESS)
8316 return -EOPNOTSUPP;
8317
8318 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
8319
8320 flowtable_hook->priority = priority;
8321 flowtable_hook->num = hooknum;
8322 } else {
8323 if (tb[NFTA_FLOWTABLE_HOOK_NUM]) {
8324 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
8325 if (hooknum != flowtable->hooknum)
8326 return -EOPNOTSUPP;
8327 }
8328
8329 if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) {
8330 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
8331 if (priority != flowtable->data.priority)
8332 return -EOPNOTSUPP;
8333 }
3b49e2e9 8334
5b6743fb
PNA
8335 flowtable_hook->priority = flowtable->data.priority;
8336 flowtable_hook->num = flowtable->hooknum;
8337 }
3b49e2e9 8338
05abe445
PNA
8339 if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) {
8340 err = nf_tables_parse_netdev_hooks(ctx->net,
8341 tb[NFTA_FLOWTABLE_HOOK_DEVS],
c3c060ad
PNA
8342 &flowtable_hook->list,
8343 extack);
05abe445
PNA
8344 if (err < 0)
8345 return err;
8346 }
3b49e2e9 8347
d9246a53 8348 list_for_each_entry(hook, &flowtable_hook->list, list) {
3f0465a9 8349 hook->ops.pf = NFPROTO_NETDEV;
5b6743fb
PNA
8350 hook->ops.hooknum = flowtable_hook->num;
8351 hook->ops.priority = flowtable_hook->priority;
8352 hook->ops.priv = &flowtable->data;
8353 hook->ops.hook = flowtable->data.type->hook;
3b49e2e9
PNA
8354 }
8355
3b49e2e9
PNA
8356 return err;
8357}
8358
24225011 8359/* call under rcu_read_lock */
98319cb9 8360static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family)
3b49e2e9
PNA
8361{
8362 const struct nf_flowtable_type *type;
8363
24225011 8364 list_for_each_entry_rcu(type, &nf_tables_flowtables, list) {
98319cb9 8365 if (family == type->family)
3b49e2e9
PNA
8366 return type;
8367 }
8368 return NULL;
8369}
8370
452238e8
FW
8371static const struct nf_flowtable_type *
8372nft_flowtable_type_get(struct net *net, u8 family)
3b49e2e9
PNA
8373{
8374 const struct nf_flowtable_type *type;
8375
24225011 8376 rcu_read_lock();
98319cb9 8377 type = __nft_flowtable_type_get(family);
24225011
ZX
8378 if (type != NULL && try_module_get(type->owner)) {
8379 rcu_read_unlock();
3b49e2e9 8380 return type;
24225011
ZX
8381 }
8382 rcu_read_unlock();
3b49e2e9 8383
f102d66b 8384 lockdep_nfnl_nft_mutex_not_held();
3b49e2e9
PNA
8385#ifdef CONFIG_MODULES
8386 if (type == NULL) {
eb014de4 8387 if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN)
3b49e2e9
PNA
8388 return ERR_PTR(-EAGAIN);
8389 }
8390#endif
8391 return ERR_PTR(-ENOENT);
8392}
8393
5acab914 8394/* Only called from error and netdev event paths. */
ff4bf2f4
PNA
8395static void nft_unregister_flowtable_hook(struct net *net,
8396 struct nft_flowtable *flowtable,
8397 struct nft_hook *hook)
8398{
8399 nf_unregister_net_hook(net, &hook->ops);
8400 flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
8401 FLOW_BLOCK_UNBIND);
8402}
8403
f9a43007
PNA
8404static void __nft_unregister_flowtable_net_hooks(struct net *net,
8405 struct list_head *hook_list,
8406 bool release_netdev)
3b49e2e9 8407{
f9a43007 8408 struct nft_hook *hook, *next;
3b49e2e9 8409
f9a43007 8410 list_for_each_entry_safe(hook, next, hook_list, list) {
5acab914 8411 nf_unregister_net_hook(net, &hook->ops);
f9a43007
PNA
8412 if (release_netdev) {
8413 list_del(&hook->list);
ab5e5c06 8414 kfree_rcu(hook, rcu);
f9a43007
PNA
8415 }
8416 }
8417}
8418
8419static void nft_unregister_flowtable_net_hooks(struct net *net,
8420 struct list_head *hook_list)
8421{
8422 __nft_unregister_flowtable_net_hooks(net, hook_list, false);
3f0465a9
PNA
8423}
8424
8425static int nft_register_flowtable_net_hooks(struct net *net,
8426 struct nft_table *table,
f9382669 8427 struct list_head *hook_list,
3f0465a9
PNA
8428 struct nft_flowtable *flowtable)
8429{
8430 struct nft_hook *hook, *hook2, *next;
8431 struct nft_flowtable *ft;
8432 int err, i = 0;
8433
f9382669 8434 list_for_each_entry(hook, hook_list, list) {
3f0465a9 8435 list_for_each_entry(ft, &table->flowtables, list) {
86fe2c19
PNA
8436 if (!nft_is_active_next(net, ft))
8437 continue;
8438
3f0465a9
PNA
8439 list_for_each_entry(hook2, &ft->hook_list, list) {
8440 if (hook->ops.dev == hook2->ops.dev &&
8441 hook->ops.pf == hook2->ops.pf) {
77a92189 8442 err = -EEXIST;
3f0465a9
PNA
8443 goto err_unregister_net_hooks;
8444 }
8445 }
8446 }
3b49e2e9 8447
d7c03a9f 8448 err = flowtable->data.type->setup(&flowtable->data,
8449 hook->ops.dev,
8450 FLOW_BLOCK_BIND);
3f0465a9
PNA
8451 if (err < 0)
8452 goto err_unregister_net_hooks;
8453
d7c03a9f 8454 err = nf_register_net_hook(net, &hook->ops);
8455 if (err < 0) {
8456 flowtable->data.type->setup(&flowtable->data,
8457 hook->ops.dev,
8458 FLOW_BLOCK_UNBIND);
8459 goto err_unregister_net_hooks;
8460 }
8461
3f0465a9 8462 i++;
3b49e2e9 8463 }
3f0465a9
PNA
8464
8465 return 0;
8466
8467err_unregister_net_hooks:
f9382669 8468 list_for_each_entry_safe(hook, next, hook_list, list) {
3f0465a9
PNA
8469 if (i-- <= 0)
8470 break;
8471
ff4bf2f4 8472 nft_unregister_flowtable_hook(net, flowtable, hook);
3f0465a9
PNA
8473 list_del_rcu(&hook->list);
8474 kfree_rcu(hook, rcu);
8475 }
8476
8477 return err;
3b49e2e9
PNA
8478}
8479
cdc32546 8480static void nft_hooks_destroy(struct list_head *hook_list)
389a2cbc
PNA
8481{
8482 struct nft_hook *hook, *next;
8483
8484 list_for_each_entry_safe(hook, next, hook_list, list) {
8485 list_del_rcu(&hook->list);
8486 kfree_rcu(hook, rcu);
8487 }
8488}
8489
78d9f48f 8490static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh,
c3c060ad
PNA
8491 struct nft_flowtable *flowtable,
8492 struct netlink_ext_ack *extack)
78d9f48f
PNA
8493{
8494 const struct nlattr * const *nla = ctx->nla;
8495 struct nft_flowtable_hook flowtable_hook;
8496 struct nft_hook *hook, *next;
8497 struct nft_trans *trans;
8498 bool unregister = false;
7b35582c 8499 u32 flags;
78d9f48f
PNA
8500 int err;
8501
8509f62b
PNA
8502 err = nft_flowtable_parse_hook(ctx, nla, &flowtable_hook, flowtable,
8503 extack, false);
78d9f48f
PNA
8504 if (err < 0)
8505 return err;
8506
8507 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
8508 if (nft_hook_list_find(&flowtable->hook_list, hook)) {
8509 list_del(&hook->list);
8510 kfree(hook);
8511 }
8512 }
8513
7b35582c
PNA
8514 if (nla[NFTA_FLOWTABLE_FLAGS]) {
8515 flags = ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
c271cc9f
PNA
8516 if (flags & ~NFT_FLOWTABLE_MASK) {
8517 err = -EOPNOTSUPP;
8518 goto err_flowtable_update_hook;
8519 }
7b35582c 8520 if ((flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD) ^
c271cc9f
PNA
8521 (flags & NFT_FLOWTABLE_HW_OFFLOAD)) {
8522 err = -EOPNOTSUPP;
8523 goto err_flowtable_update_hook;
8524 }
7b35582c
PNA
8525 } else {
8526 flags = flowtable->data.flags;
8527 }
8528
78d9f48f
PNA
8529 err = nft_register_flowtable_net_hooks(ctx->net, ctx->table,
8530 &flowtable_hook.list, flowtable);
8531 if (err < 0)
8532 goto err_flowtable_update_hook;
8533
8534 trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE,
8535 sizeof(struct nft_trans_flowtable));
8536 if (!trans) {
8537 unregister = true;
8538 err = -ENOMEM;
8539 goto err_flowtable_update_hook;
8540 }
8541
7b35582c 8542 nft_trans_flowtable_flags(trans) = flags;
78d9f48f
PNA
8543 nft_trans_flowtable(trans) = flowtable;
8544 nft_trans_flowtable_update(trans) = true;
8545 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
8546 list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans));
8547
0854db2a 8548 nft_trans_commit_list_add_tail(ctx->net, trans);
78d9f48f
PNA
8549
8550 return 0;
8551
8552err_flowtable_update_hook:
8553 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
8554 if (unregister)
8555 nft_unregister_flowtable_hook(ctx->net, flowtable, hook);
8556 list_del_rcu(&hook->list);
8557 kfree_rcu(hook, rcu);
8558 }
8559
8560 return err;
8561
8562}
8563
7dab8ee3
PNA
8564static int nf_tables_newflowtable(struct sk_buff *skb,
8565 const struct nfnl_info *info,
8566 const struct nlattr * const nla[])
3b49e2e9 8567{
7dab8ee3 8568 struct netlink_ext_ack *extack = info->extack;
d9246a53 8569 struct nft_flowtable_hook flowtable_hook;
7dab8ee3 8570 u8 genmask = nft_genmask_next(info->net);
ef4b65e5 8571 u8 family = info->nfmsg->nfgen_family;
3b49e2e9 8572 const struct nf_flowtable_type *type;
3f0465a9 8573 struct nft_flowtable *flowtable;
7dab8ee3 8574 struct net *net = info->net;
3b49e2e9 8575 struct nft_table *table;
d472e985 8576 struct nft_trans *trans;
3b49e2e9 8577 struct nft_ctx ctx;
3f0465a9 8578 int err;
3b49e2e9
PNA
8579
8580 if (!nla[NFTA_FLOWTABLE_TABLE] ||
8581 !nla[NFTA_FLOWTABLE_NAME] ||
8582 !nla[NFTA_FLOWTABLE_HOOK])
8583 return -EINVAL;
8584
cac20fcd 8585 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
6001a930 8586 genmask, NETLINK_CB(skb).portid);
36dd1bcc
PNA
8587 if (IS_ERR(table)) {
8588 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
3b49e2e9 8589 return PTR_ERR(table);
36dd1bcc 8590 }
3b49e2e9 8591
cac20fcd
PNA
8592 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
8593 genmask);
3b49e2e9
PNA
8594 if (IS_ERR(flowtable)) {
8595 err = PTR_ERR(flowtable);
36dd1bcc
PNA
8596 if (err != -ENOENT) {
8597 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
3b49e2e9 8598 return err;
36dd1bcc 8599 }
3b49e2e9 8600 } else {
7dab8ee3 8601 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
36dd1bcc 8602 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
3b49e2e9 8603 return -EEXIST;
36dd1bcc 8604 }
3b49e2e9 8605
7dab8ee3 8606 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
78d9f48f 8607
c3c060ad 8608 return nft_flowtable_update(&ctx, info->nlh, flowtable, extack);
3b49e2e9
PNA
8609 }
8610
7dab8ee3 8611 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
3b49e2e9 8612
1689f259
PNA
8613 if (!nft_use_inc(&table->use))
8614 return -EMFILE;
8615
33758c89 8616 flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL_ACCOUNT);
1689f259
PNA
8617 if (!flowtable) {
8618 err = -ENOMEM;
8619 goto flowtable_alloc;
8620 }
3b49e2e9
PNA
8621
8622 flowtable->table = table;
3ecbfd65 8623 flowtable->handle = nf_tables_alloc_handle(table);
3f0465a9 8624 INIT_LIST_HEAD(&flowtable->hook_list);
3ecbfd65 8625
33758c89 8626 flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL_ACCOUNT);
3b49e2e9
PNA
8627 if (!flowtable->name) {
8628 err = -ENOMEM;
8629 goto err1;
8630 }
8631
452238e8 8632 type = nft_flowtable_type_get(net, family);
3b49e2e9
PNA
8633 if (IS_ERR(type)) {
8634 err = PTR_ERR(type);
8635 goto err2;
8636 }
8637
8bb69f3b
PNA
8638 if (nla[NFTA_FLOWTABLE_FLAGS]) {
8639 flowtable->data.flags =
8640 ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
7e6136f1
PNA
8641 if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) {
8642 err = -EOPNOTSUPP;
8bb69f3b 8643 goto err3;
7e6136f1 8644 }
8bb69f3b
PNA
8645 }
8646
8647 write_pnet(&flowtable->data.net, net);
3b49e2e9 8648 flowtable->data.type = type;
a268de77 8649 err = type->init(&flowtable->data);
3b49e2e9
PNA
8650 if (err < 0)
8651 goto err3;
8652
8509f62b
PNA
8653 err = nft_flowtable_parse_hook(&ctx, nla, &flowtable_hook, flowtable,
8654 extack, true);
3b49e2e9 8655 if (err < 0)
d472e985 8656 goto err_flowtable_parse_hooks;
3b49e2e9 8657
d9246a53
PNA
8658 list_splice(&flowtable_hook.list, &flowtable->hook_list);
8659 flowtable->data.priority = flowtable_hook.priority;
8660 flowtable->hooknum = flowtable_hook.num;
8661
d472e985
PNA
8662 trans = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable);
8663 if (IS_ERR(trans)) {
8664 err = PTR_ERR(trans);
8665 goto err_flowtable_trans;
8666 }
8667
8668 /* This must be LAST to ensure no packets are walking over this flowtable. */
f9382669
PNA
8669 err = nft_register_flowtable_net_hooks(ctx.net, table,
8670 &flowtable->hook_list,
8671 flowtable);
3b49e2e9 8672 if (err < 0)
d472e985 8673 goto err_flowtable_hooks;
3b49e2e9
PNA
8674
8675 list_add_tail_rcu(&flowtable->list, &table->flowtables);
3b49e2e9
PNA
8676
8677 return 0;
d472e985
PNA
8678
8679err_flowtable_hooks:
8680 nft_trans_destroy(trans);
8681err_flowtable_trans:
8682 nft_hooks_destroy(&flowtable->hook_list);
8683err_flowtable_parse_hooks:
a268de77 8684 flowtable->data.type->free(&flowtable->data);
3b49e2e9
PNA
8685err3:
8686 module_put(type->owner);
8687err2:
8688 kfree(flowtable->name);
8689err1:
8690 kfree(flowtable);
1689f259
PNA
8691flowtable_alloc:
8692 nft_use_dec_restore(&table->use);
8693
3b49e2e9
PNA
8694 return err;
8695}
8696
3003055f
PNA
8697static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook)
8698{
8699 struct nft_hook *this, *next;
8700
8701 list_for_each_entry_safe(this, next, &flowtable_hook->list, list) {
8702 list_del(&this->list);
8703 kfree(this);
8704 }
8705}
8706
abadb2f8 8707static int nft_delflowtable_hook(struct nft_ctx *ctx,
c3c060ad
PNA
8708 struct nft_flowtable *flowtable,
8709 struct netlink_ext_ack *extack)
abadb2f8
PNA
8710{
8711 const struct nlattr * const *nla = ctx->nla;
8712 struct nft_flowtable_hook flowtable_hook;
b6d9014a 8713 LIST_HEAD(flowtable_del_list);
3003055f 8714 struct nft_hook *this, *hook;
abadb2f8
PNA
8715 struct nft_trans *trans;
8716 int err;
8717
8509f62b
PNA
8718 err = nft_flowtable_parse_hook(ctx, nla, &flowtable_hook, flowtable,
8719 extack, false);
abadb2f8
PNA
8720 if (err < 0)
8721 return err;
8722
3003055f 8723 list_for_each_entry(this, &flowtable_hook.list, list) {
abadb2f8
PNA
8724 hook = nft_hook_list_find(&flowtable->hook_list, this);
8725 if (!hook) {
8726 err = -ENOENT;
8727 goto err_flowtable_del_hook;
8728 }
b6d9014a 8729 list_move(&hook->list, &flowtable_del_list);
abadb2f8
PNA
8730 }
8731
8732 trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE,
8733 sizeof(struct nft_trans_flowtable));
3003055f
PNA
8734 if (!trans) {
8735 err = -ENOMEM;
8736 goto err_flowtable_del_hook;
8737 }
abadb2f8
PNA
8738
8739 nft_trans_flowtable(trans) = flowtable;
8740 nft_trans_flowtable_update(trans) = true;
8741 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
b6d9014a 8742 list_splice(&flowtable_del_list, &nft_trans_flowtable_hooks(trans));
3003055f 8743 nft_flowtable_hook_release(&flowtable_hook);
abadb2f8 8744
0854db2a 8745 nft_trans_commit_list_add_tail(ctx->net, trans);
abadb2f8
PNA
8746
8747 return 0;
8748
8749err_flowtable_del_hook:
b6d9014a 8750 list_splice(&flowtable_del_list, &flowtable->hook_list);
3003055f 8751 nft_flowtable_hook_release(&flowtable_hook);
abadb2f8
PNA
8752
8753 return err;
8754}
8755
7dab8ee3
PNA
8756static int nf_tables_delflowtable(struct sk_buff *skb,
8757 const struct nfnl_info *info,
8758 const struct nlattr * const nla[])
3b49e2e9 8759{
7dab8ee3
PNA
8760 struct netlink_ext_ack *extack = info->extack;
8761 u8 genmask = nft_genmask_next(info->net);
ef4b65e5 8762 u8 family = info->nfmsg->nfgen_family;
3b49e2e9 8763 struct nft_flowtable *flowtable;
7dab8ee3 8764 struct net *net = info->net;
36dd1bcc 8765 const struct nlattr *attr;
3b49e2e9
PNA
8766 struct nft_table *table;
8767 struct nft_ctx ctx;
8768
e603ea4b
PNA
8769 if (!nla[NFTA_FLOWTABLE_TABLE] ||
8770 (!nla[NFTA_FLOWTABLE_NAME] &&
8771 !nla[NFTA_FLOWTABLE_HANDLE]))
8772 return -EINVAL;
8773
cac20fcd 8774 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
6001a930 8775 genmask, NETLINK_CB(skb).portid);
36dd1bcc
PNA
8776 if (IS_ERR(table)) {
8777 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
3b49e2e9 8778 return PTR_ERR(table);
36dd1bcc 8779 }
3b49e2e9 8780
36dd1bcc
PNA
8781 if (nla[NFTA_FLOWTABLE_HANDLE]) {
8782 attr = nla[NFTA_FLOWTABLE_HANDLE];
8783 flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask);
8784 } else {
8785 attr = nla[NFTA_FLOWTABLE_NAME];
8786 flowtable = nft_flowtable_lookup(table, attr, genmask);
8787 }
8788
8789 if (IS_ERR(flowtable)) {
f80a612d
FFM
8790 if (PTR_ERR(flowtable) == -ENOENT &&
8791 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYFLOWTABLE)
8792 return 0;
8793
36dd1bcc
PNA
8794 NL_SET_BAD_ATTR(extack, attr);
8795 return PTR_ERR(flowtable);
8796 }
abadb2f8 8797
7dab8ee3 8798 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
abadb2f8
PNA
8799
8800 if (nla[NFTA_FLOWTABLE_HOOK])
c3c060ad 8801 return nft_delflowtable_hook(&ctx, flowtable, extack);
abadb2f8 8802
36dd1bcc
PNA
8803 if (flowtable->use > 0) {
8804 NL_SET_BAD_ATTR(extack, attr);
3b49e2e9 8805 return -EBUSY;
36dd1bcc 8806 }
3b49e2e9 8807
3b49e2e9
PNA
8808 return nft_delflowtable(&ctx, flowtable);
8809}
8810
8811static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,
8812 u32 portid, u32 seq, int event,
8813 u32 flags, int family,
c42d8bda
PNA
8814 struct nft_flowtable *flowtable,
8815 struct list_head *hook_list)
3b49e2e9
PNA
8816{
8817 struct nlattr *nest, *nest_devs;
3f0465a9 8818 struct nft_hook *hook;
3b49e2e9 8819 struct nlmsghdr *nlh;
3b49e2e9
PNA
8820
8821 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
19c28b13
PNA
8822 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
8823 NFNETLINK_V0, nft_base_seq(net));
8824 if (!nlh)
3b49e2e9
PNA
8825 goto nla_put_failure;
8826
3b49e2e9
PNA
8827 if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) ||
8828 nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) ||
3ecbfd65 8829 nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle),
28339b21
PNA
8830 NFTA_FLOWTABLE_PAD))
8831 goto nla_put_failure;
8832
8833 if (event == NFT_MSG_DELFLOWTABLE && !hook_list) {
8834 nlmsg_end(skb, nlh);
8835 return 0;
8836 }
8837
8838 if (nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) ||
8bb69f3b 8839 nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags)))
3b49e2e9
PNA
8840 goto nla_put_failure;
8841
ae0be8de 8842 nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK);
eb895086
KL
8843 if (!nest)
8844 goto nla_put_failure;
3b49e2e9 8845 if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) ||
71a8a63b 8846 nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority)))
3b49e2e9
PNA
8847 goto nla_put_failure;
8848
ae0be8de 8849 nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS);
3b49e2e9
PNA
8850 if (!nest_devs)
8851 goto nla_put_failure;
8852
28339b21
PNA
8853 if (!hook_list)
8854 hook_list = &flowtable->hook_list;
8855
c42d8bda 8856 list_for_each_entry_rcu(hook, hook_list, list) {
3f0465a9 8857 if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name))
3b49e2e9
PNA
8858 goto nla_put_failure;
8859 }
8860 nla_nest_end(skb, nest_devs);
8861 nla_nest_end(skb, nest);
8862
8863 nlmsg_end(skb, nlh);
8864 return 0;
8865
8866nla_put_failure:
8867 nlmsg_trim(skb, nlh);
8868 return -1;
8869}
8870
8871struct nft_flowtable_filter {
8872 char *table;
8873};
8874
8875static int nf_tables_dump_flowtable(struct sk_buff *skb,
8876 struct netlink_callback *cb)
8877{
8878 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
8879 struct nft_flowtable_filter *filter = cb->data;
8880 unsigned int idx = 0, s_idx = cb->args[0];
8881 struct net *net = sock_net(skb->sk);
8882 int family = nfmsg->nfgen_family;
8883 struct nft_flowtable *flowtable;
0854db2a 8884 struct nftables_pernet *nft_net;
3b49e2e9
PNA
8885 const struct nft_table *table;
8886
8887 rcu_read_lock();
d59d2f82 8888 nft_net = nft_pernet(net);
34002783 8889 cb->seq = READ_ONCE(nft_net->base_seq);
3b49e2e9 8890
0854db2a 8891 list_for_each_entry_rcu(table, &nft_net->tables, list) {
98319cb9 8892 if (family != NFPROTO_UNSPEC && family != table->family)
3b49e2e9
PNA
8893 continue;
8894
36596dad
PNA
8895 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
8896 if (!nft_is_active(net, flowtable))
8897 goto cont;
8898 if (idx < s_idx)
8899 goto cont;
8900 if (idx > s_idx)
8901 memset(&cb->args[1], 0,
8902 sizeof(cb->args) - sizeof(cb->args[0]));
360cc79d 8903 if (filter && filter->table &&
36596dad
PNA
8904 strcmp(filter->table, table->name))
8905 goto cont;
3b49e2e9 8906
36596dad
PNA
8907 if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid,
8908 cb->nlh->nlmsg_seq,
8909 NFT_MSG_NEWFLOWTABLE,
8910 NLM_F_MULTI | NLM_F_APPEND,
c42d8bda 8911 table->family,
28339b21 8912 flowtable, NULL) < 0)
36596dad 8913 goto done;
3b49e2e9 8914
36596dad 8915 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3b49e2e9 8916cont:
36596dad 8917 idx++;
3b49e2e9
PNA
8918 }
8919 }
8920done:
8921 rcu_read_unlock();
8922
8923 cb->args[0] = idx;
8924 return skb->len;
8925}
8926
90fd131a 8927static int nf_tables_dump_flowtable_start(struct netlink_callback *cb)
3b49e2e9 8928{
90fd131a
FW
8929 const struct nlattr * const *nla = cb->data;
8930 struct nft_flowtable_filter *filter = NULL;
3b49e2e9 8931
90fd131a
FW
8932 if (nla[NFTA_FLOWTABLE_TABLE]) {
8933 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
8934 if (!filter)
8935 return -ENOMEM;
3b49e2e9 8936
90fd131a
FW
8937 filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE],
8938 GFP_ATOMIC);
8939 if (!filter->table) {
8940 kfree(filter);
8941 return -ENOMEM;
8942 }
8943 }
3b49e2e9 8944
90fd131a 8945 cb->data = filter;
3b49e2e9
PNA
8946 return 0;
8947}
8948
90fd131a 8949static int nf_tables_dump_flowtable_done(struct netlink_callback *cb)
3b49e2e9 8950{
90fd131a 8951 struct nft_flowtable_filter *filter = cb->data;
3b49e2e9 8952
3b49e2e9 8953 if (!filter)
90fd131a 8954 return 0;
3b49e2e9 8955
90fd131a
FW
8956 kfree(filter->table);
8957 kfree(filter);
8958
8959 return 0;
3b49e2e9
PNA
8960}
8961
d9adf22a 8962/* called with rcu_read_lock held */
797d4980
PNA
8963static int nf_tables_getflowtable(struct sk_buff *skb,
8964 const struct nfnl_info *info,
8965 const struct nlattr * const nla[])
3b49e2e9 8966{
aee1f692 8967 struct netlink_ext_ack *extack = info->extack;
797d4980 8968 u8 genmask = nft_genmask_cur(info->net);
ef4b65e5 8969 u8 family = info->nfmsg->nfgen_family;
3b49e2e9 8970 struct nft_flowtable *flowtable;
3b49e2e9 8971 const struct nft_table *table;
797d4980 8972 struct net *net = info->net;
3b49e2e9
PNA
8973 struct sk_buff *skb2;
8974 int err;
8975
797d4980 8976 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
3b49e2e9 8977 struct netlink_dump_control c = {
90fd131a 8978 .start = nf_tables_dump_flowtable_start,
3b49e2e9
PNA
8979 .dump = nf_tables_dump_flowtable,
8980 .done = nf_tables_dump_flowtable_done,
d9adf22a 8981 .module = THIS_MODULE,
90fd131a 8982 .data = (void *)nla,
3b49e2e9
PNA
8983 };
8984
797d4980 8985 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
3b49e2e9
PNA
8986 }
8987
8988 if (!nla[NFTA_FLOWTABLE_NAME])
8989 return -EINVAL;
8990
cac20fcd 8991 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
6001a930 8992 genmask, 0);
aee1f692
PNA
8993 if (IS_ERR(table)) {
8994 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
3b49e2e9 8995 return PTR_ERR(table);
aee1f692 8996 }
3b49e2e9 8997
cac20fcd
PNA
8998 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
8999 genmask);
aee1f692
PNA
9000 if (IS_ERR(flowtable)) {
9001 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
3b49e2e9 9002 return PTR_ERR(flowtable);
aee1f692 9003 }
3b49e2e9 9004
d9adf22a 9005 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
3b49e2e9
PNA
9006 if (!skb2)
9007 return -ENOMEM;
9008
9009 err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid,
797d4980 9010 info->nlh->nlmsg_seq,
3b49e2e9 9011 NFT_MSG_NEWFLOWTABLE, 0, family,
28339b21 9012 flowtable, NULL);
3b49e2e9 9013 if (err < 0)
ee921183 9014 goto err_fill_flowtable_info;
3b49e2e9 9015
ee921183
PNA
9016 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
9017
9018err_fill_flowtable_info:
3b49e2e9
PNA
9019 kfree_skb(skb2);
9020 return err;
9021}
9022
9023static void nf_tables_flowtable_notify(struct nft_ctx *ctx,
9024 struct nft_flowtable *flowtable,
28339b21 9025 struct list_head *hook_list, int event)
3b49e2e9 9026{
d59d2f82 9027 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
3b49e2e9 9028 struct sk_buff *skb;
6fb721cf 9029 u16 flags = 0;
3b49e2e9
PNA
9030 int err;
9031
dceababa 9032 if (!ctx->report &&
3b49e2e9
PNA
9033 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
9034 return;
9035
9036 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
9037 if (skb == NULL)
9038 goto err;
9039
6fb721cf
PNA
9040 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
9041 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
9042
3b49e2e9 9043 err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid,
6fb721cf 9044 ctx->seq, event, flags,
c42d8bda 9045 ctx->family, flowtable, hook_list);
3b49e2e9
PNA
9046 if (err < 0) {
9047 kfree_skb(skb);
9048 goto err;
9049 }
9050
0854db2a 9051 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
3b49e2e9
PNA
9052 return;
9053err:
9054 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
9055}
9056
3b49e2e9
PNA
9057static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
9058{
3f0465a9
PNA
9059 struct nft_hook *hook, *next;
9060
5acab914 9061 flowtable->data.type->free(&flowtable->data);
3f0465a9 9062 list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
5acab914
PNA
9063 flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
9064 FLOW_BLOCK_UNBIND);
3f0465a9
PNA
9065 list_del_rcu(&hook->list);
9066 kfree(hook);
9067 }
3b49e2e9 9068 kfree(flowtable->name);
3b49e2e9 9069 module_put(flowtable->data.type->owner);
a12486eb 9070 kfree(flowtable);
3b49e2e9
PNA
9071}
9072
84d7fce6
PNA
9073static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
9074 u32 portid, u32 seq)
9075{
d59d2f82 9076 struct nftables_pernet *nft_net = nft_pernet(net);
84d7fce6 9077 struct nlmsghdr *nlh;
784b4e61 9078 char buf[TASK_COMM_LEN];
dedb67c4 9079 int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN);
84d7fce6 9080
19c28b13
PNA
9081 nlh = nfnl_msg_put(skb, portid, seq, event, 0, AF_UNSPEC,
9082 NFNETLINK_V0, nft_base_seq(net));
9083 if (!nlh)
84d7fce6
PNA
9084 goto nla_put_failure;
9085
0854db2a 9086 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(nft_net->base_seq)) ||
784b4e61
PS
9087 nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) ||
9088 nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current)))
84d7fce6
PNA
9089 goto nla_put_failure;
9090
053c095a
JB
9091 nlmsg_end(skb, nlh);
9092 return 0;
84d7fce6
PNA
9093
9094nla_put_failure:
9095 nlmsg_trim(skb, nlh);
9096 return -EMSGSIZE;
9097}
9098
3b49e2e9
PNA
9099static void nft_flowtable_event(unsigned long event, struct net_device *dev,
9100 struct nft_flowtable *flowtable)
9101{
3f0465a9 9102 struct nft_hook *hook;
3b49e2e9 9103
3f0465a9
PNA
9104 list_for_each_entry(hook, &flowtable->hook_list, list) {
9105 if (hook->ops.dev != dev)
3b49e2e9
PNA
9106 continue;
9107
5acab914 9108 /* flow_offload_netdev_event() cleans up entries for us. */
ff4bf2f4 9109 nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook);
3f0465a9
PNA
9110 list_del_rcu(&hook->list);
9111 kfree_rcu(hook, rcu);
3b49e2e9
PNA
9112 break;
9113 }
9114}
9115
9116static int nf_tables_flowtable_event(struct notifier_block *this,
9117 unsigned long event, void *ptr)
9118{
9119 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
9120 struct nft_flowtable *flowtable;
0854db2a 9121 struct nftables_pernet *nft_net;
3b49e2e9 9122 struct nft_table *table;
0a2cf5ee 9123 struct net *net;
3b49e2e9
PNA
9124
9125 if (event != NETDEV_UNREGISTER)
9126 return 0;
9127
6a48de01 9128 net = dev_net(dev);
d59d2f82 9129 nft_net = nft_pernet(net);
0854db2a
FW
9130 mutex_lock(&nft_net->commit_mutex);
9131 list_for_each_entry(table, &nft_net->tables, list) {
36596dad
PNA
9132 list_for_each_entry(flowtable, &table->flowtables, list) {
9133 nft_flowtable_event(event, dev, flowtable);
3b49e2e9
PNA
9134 }
9135 }
0854db2a 9136 mutex_unlock(&nft_net->commit_mutex);
6a48de01 9137
3b49e2e9
PNA
9138 return NOTIFY_DONE;
9139}
9140
9141static struct notifier_block nf_tables_flowtable_notifier = {
9142 .notifier_call = nf_tables_flowtable_event,
9143};
9144
25e94a99
PNA
9145static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb,
9146 int event)
84d7fce6
PNA
9147{
9148 struct nlmsghdr *nlh = nlmsg_hdr(skb);
9149 struct sk_buff *skb2;
9150 int err;
9151
dceababa 9152 if (!nlmsg_report(nlh) &&
84d7fce6 9153 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
25e94a99 9154 return;
84d7fce6 9155
84d7fce6
PNA
9156 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
9157 if (skb2 == NULL)
9158 goto err;
9159
9160 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
9161 nlh->nlmsg_seq);
9162 if (err < 0) {
9163 kfree_skb(skb2);
9164 goto err;
9165 }
9166
25e94a99
PNA
9167 nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
9168 nlmsg_report(nlh), GFP_KERNEL);
9169 return;
84d7fce6 9170err:
25e94a99
PNA
9171 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
9172 -ENOBUFS);
84d7fce6
PNA
9173}
9174
797d4980
PNA
9175static int nf_tables_getgen(struct sk_buff *skb, const struct nfnl_info *info,
9176 const struct nlattr * const nla[])
84d7fce6 9177{
84d7fce6
PNA
9178 struct sk_buff *skb2;
9179 int err;
9180
d9adf22a 9181 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
84d7fce6
PNA
9182 if (skb2 == NULL)
9183 return -ENOMEM;
9184
797d4980
PNA
9185 err = nf_tables_fill_gen_info(skb2, info->net, NETLINK_CB(skb).portid,
9186 info->nlh->nlmsg_seq);
84d7fce6 9187 if (err < 0)
ee921183 9188 goto err_fill_gen_info;
84d7fce6 9189
797d4980 9190 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
ee921183
PNA
9191
9192err_fill_gen_info:
84d7fce6
PNA
9193 kfree_skb(skb2);
9194 return err;
9195}
9196
96518518
PM
9197static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
9198 [NFT_MSG_NEWTABLE] = {
50f2db9e
PNA
9199 .call = nf_tables_newtable,
9200 .type = NFNL_CB_BATCH,
96518518
PM
9201 .attr_count = NFTA_TABLE_MAX,
9202 .policy = nft_table_policy,
9203 },
9204 [NFT_MSG_GETTABLE] = {
50f2db9e
PNA
9205 .call = nf_tables_gettable,
9206 .type = NFNL_CB_RCU,
96518518
PM
9207 .attr_count = NFTA_TABLE_MAX,
9208 .policy = nft_table_policy,
9209 },
9210 [NFT_MSG_DELTABLE] = {
50f2db9e
PNA
9211 .call = nf_tables_deltable,
9212 .type = NFNL_CB_BATCH,
96518518
PM
9213 .attr_count = NFTA_TABLE_MAX,
9214 .policy = nft_table_policy,
9215 },
f80a612d
FFM
9216 [NFT_MSG_DESTROYTABLE] = {
9217 .call = nf_tables_deltable,
9218 .type = NFNL_CB_BATCH,
9219 .attr_count = NFTA_TABLE_MAX,
9220 .policy = nft_table_policy,
9221 },
96518518 9222 [NFT_MSG_NEWCHAIN] = {
50f2db9e
PNA
9223 .call = nf_tables_newchain,
9224 .type = NFNL_CB_BATCH,
96518518
PM
9225 .attr_count = NFTA_CHAIN_MAX,
9226 .policy = nft_chain_policy,
9227 },
9228 [NFT_MSG_GETCHAIN] = {
50f2db9e
PNA
9229 .call = nf_tables_getchain,
9230 .type = NFNL_CB_RCU,
96518518
PM
9231 .attr_count = NFTA_CHAIN_MAX,
9232 .policy = nft_chain_policy,
9233 },
9234 [NFT_MSG_DELCHAIN] = {
50f2db9e
PNA
9235 .call = nf_tables_delchain,
9236 .type = NFNL_CB_BATCH,
96518518
PM
9237 .attr_count = NFTA_CHAIN_MAX,
9238 .policy = nft_chain_policy,
9239 },
f80a612d
FFM
9240 [NFT_MSG_DESTROYCHAIN] = {
9241 .call = nf_tables_delchain,
9242 .type = NFNL_CB_BATCH,
9243 .attr_count = NFTA_CHAIN_MAX,
9244 .policy = nft_chain_policy,
9245 },
96518518 9246 [NFT_MSG_NEWRULE] = {
50f2db9e
PNA
9247 .call = nf_tables_newrule,
9248 .type = NFNL_CB_BATCH,
96518518
PM
9249 .attr_count = NFTA_RULE_MAX,
9250 .policy = nft_rule_policy,
9251 },
9252 [NFT_MSG_GETRULE] = {
50f2db9e
PNA
9253 .call = nf_tables_getrule,
9254 .type = NFNL_CB_RCU,
96518518
PM
9255 .attr_count = NFTA_RULE_MAX,
9256 .policy = nft_rule_policy,
9257 },
8daa8fde 9258 [NFT_MSG_GETRULE_RESET] = {
3cb03edb 9259 .call = nf_tables_getrule_reset,
8daa8fde
PS
9260 .type = NFNL_CB_RCU,
9261 .attr_count = NFTA_RULE_MAX,
9262 .policy = nft_rule_policy,
9263 },
96518518 9264 [NFT_MSG_DELRULE] = {
50f2db9e
PNA
9265 .call = nf_tables_delrule,
9266 .type = NFNL_CB_BATCH,
96518518
PM
9267 .attr_count = NFTA_RULE_MAX,
9268 .policy = nft_rule_policy,
9269 },
f80a612d
FFM
9270 [NFT_MSG_DESTROYRULE] = {
9271 .call = nf_tables_delrule,
9272 .type = NFNL_CB_BATCH,
9273 .attr_count = NFTA_RULE_MAX,
9274 .policy = nft_rule_policy,
9275 },
20a69341 9276 [NFT_MSG_NEWSET] = {
50f2db9e
PNA
9277 .call = nf_tables_newset,
9278 .type = NFNL_CB_BATCH,
20a69341
PM
9279 .attr_count = NFTA_SET_MAX,
9280 .policy = nft_set_policy,
9281 },
9282 [NFT_MSG_GETSET] = {
50f2db9e
PNA
9283 .call = nf_tables_getset,
9284 .type = NFNL_CB_RCU,
20a69341
PM
9285 .attr_count = NFTA_SET_MAX,
9286 .policy = nft_set_policy,
9287 },
9288 [NFT_MSG_DELSET] = {
50f2db9e
PNA
9289 .call = nf_tables_delset,
9290 .type = NFNL_CB_BATCH,
20a69341
PM
9291 .attr_count = NFTA_SET_MAX,
9292 .policy = nft_set_policy,
9293 },
f80a612d
FFM
9294 [NFT_MSG_DESTROYSET] = {
9295 .call = nf_tables_delset,
9296 .type = NFNL_CB_BATCH,
9297 .attr_count = NFTA_SET_MAX,
9298 .policy = nft_set_policy,
9299 },
20a69341 9300 [NFT_MSG_NEWSETELEM] = {
50f2db9e
PNA
9301 .call = nf_tables_newsetelem,
9302 .type = NFNL_CB_BATCH,
20a69341
PM
9303 .attr_count = NFTA_SET_ELEM_LIST_MAX,
9304 .policy = nft_set_elem_list_policy,
9305 },
9306 [NFT_MSG_GETSETELEM] = {
50f2db9e
PNA
9307 .call = nf_tables_getsetelem,
9308 .type = NFNL_CB_RCU,
20a69341
PM
9309 .attr_count = NFTA_SET_ELEM_LIST_MAX,
9310 .policy = nft_set_elem_list_policy,
9311 },
079cd633 9312 [NFT_MSG_GETSETELEM_RESET] = {
3d483faa 9313 .call = nf_tables_getsetelem_reset,
079cd633
PS
9314 .type = NFNL_CB_RCU,
9315 .attr_count = NFTA_SET_ELEM_LIST_MAX,
9316 .policy = nft_set_elem_list_policy,
9317 },
20a69341 9318 [NFT_MSG_DELSETELEM] = {
50f2db9e
PNA
9319 .call = nf_tables_delsetelem,
9320 .type = NFNL_CB_BATCH,
20a69341
PM
9321 .attr_count = NFTA_SET_ELEM_LIST_MAX,
9322 .policy = nft_set_elem_list_policy,
9323 },
f80a612d
FFM
9324 [NFT_MSG_DESTROYSETELEM] = {
9325 .call = nf_tables_delsetelem,
9326 .type = NFNL_CB_BATCH,
9327 .attr_count = NFTA_SET_ELEM_LIST_MAX,
9328 .policy = nft_set_elem_list_policy,
9329 },
84d7fce6 9330 [NFT_MSG_GETGEN] = {
50f2db9e
PNA
9331 .call = nf_tables_getgen,
9332 .type = NFNL_CB_RCU,
84d7fce6 9333 },
e5009240 9334 [NFT_MSG_NEWOBJ] = {
50f2db9e
PNA
9335 .call = nf_tables_newobj,
9336 .type = NFNL_CB_BATCH,
e5009240
PNA
9337 .attr_count = NFTA_OBJ_MAX,
9338 .policy = nft_obj_policy,
9339 },
9340 [NFT_MSG_GETOBJ] = {
50f2db9e
PNA
9341 .call = nf_tables_getobj,
9342 .type = NFNL_CB_RCU,
e5009240
PNA
9343 .attr_count = NFTA_OBJ_MAX,
9344 .policy = nft_obj_policy,
9345 },
9346 [NFT_MSG_DELOBJ] = {
50f2db9e
PNA
9347 .call = nf_tables_delobj,
9348 .type = NFNL_CB_BATCH,
e5009240
PNA
9349 .attr_count = NFTA_OBJ_MAX,
9350 .policy = nft_obj_policy,
9351 },
f80a612d
FFM
9352 [NFT_MSG_DESTROYOBJ] = {
9353 .call = nf_tables_delobj,
9354 .type = NFNL_CB_BATCH,
9355 .attr_count = NFTA_OBJ_MAX,
9356 .policy = nft_obj_policy,
9357 },
43da04a5 9358 [NFT_MSG_GETOBJ_RESET] = {
50f2db9e
PNA
9359 .call = nf_tables_getobj,
9360 .type = NFNL_CB_RCU,
43da04a5
PNA
9361 .attr_count = NFTA_OBJ_MAX,
9362 .policy = nft_obj_policy,
9363 },
3b49e2e9 9364 [NFT_MSG_NEWFLOWTABLE] = {
50f2db9e
PNA
9365 .call = nf_tables_newflowtable,
9366 .type = NFNL_CB_BATCH,
3b49e2e9
PNA
9367 .attr_count = NFTA_FLOWTABLE_MAX,
9368 .policy = nft_flowtable_policy,
9369 },
9370 [NFT_MSG_GETFLOWTABLE] = {
50f2db9e
PNA
9371 .call = nf_tables_getflowtable,
9372 .type = NFNL_CB_RCU,
3b49e2e9
PNA
9373 .attr_count = NFTA_FLOWTABLE_MAX,
9374 .policy = nft_flowtable_policy,
9375 },
9376 [NFT_MSG_DELFLOWTABLE] = {
50f2db9e
PNA
9377 .call = nf_tables_delflowtable,
9378 .type = NFNL_CB_BATCH,
3b49e2e9
PNA
9379 .attr_count = NFTA_FLOWTABLE_MAX,
9380 .policy = nft_flowtable_policy,
9381 },
f80a612d
FFM
9382 [NFT_MSG_DESTROYFLOWTABLE] = {
9383 .call = nf_tables_delflowtable,
9384 .type = NFNL_CB_BATCH,
9385 .attr_count = NFTA_FLOWTABLE_MAX,
9386 .policy = nft_flowtable_policy,
9387 },
96518518
PM
9388};
9389
a654de8f
PNA
9390static int nf_tables_validate(struct net *net)
9391{
d59d2f82 9392 struct nftables_pernet *nft_net = nft_pernet(net);
a654de8f
PNA
9393 struct nft_table *table;
9394
00c320f9
FW
9395 list_for_each_entry(table, &nft_net->tables, list) {
9396 switch (table->validate_state) {
9397 case NFT_VALIDATE_SKIP:
9398 continue;
9399 case NFT_VALIDATE_NEED:
9400 nft_validate_state_update(table, NFT_VALIDATE_DO);
9401 fallthrough;
9402 case NFT_VALIDATE_DO:
a654de8f
PNA
9403 if (nft_table_validate(net, table) < 0)
9404 return -EAGAIN;
00c320f9
FW
9405
9406 nft_validate_state_update(table, NFT_VALIDATE_SKIP);
4b80ced9 9407 break;
a654de8f 9408 }
a654de8f
PNA
9409 }
9410
9411 return 0;
9412}
9413
66293c46
FW
9414/* a drop policy has to be deferred until all rules have been activated,
9415 * otherwise a large ruleset that contains a drop-policy base chain will
9416 * cause all packets to get dropped until the full transaction has been
9417 * processed.
9418 *
9419 * We defer the drop policy until the transaction has been finalized.
9420 */
9421static void nft_chain_commit_drop_policy(struct nft_trans *trans)
9422{
9423 struct nft_base_chain *basechain;
9424
9425 if (nft_trans_chain_policy(trans) != NF_DROP)
9426 return;
9427
9428 if (!nft_is_base_chain(trans->ctx.chain))
9429 return;
9430
9431 basechain = nft_base_chain(trans->ctx.chain);
9432 basechain->policy = NF_DROP;
9433}
9434
91c7b38d
PNA
9435static void nft_chain_commit_update(struct nft_trans *trans)
9436{
9437 struct nft_base_chain *basechain;
9438
1b2470e5
FW
9439 if (nft_trans_chain_name(trans)) {
9440 rhltable_remove(&trans->ctx.table->chains_ht,
9441 &trans->ctx.chain->rhlhead,
9442 nft_chain_ht_params);
d71efb59 9443 swap(trans->ctx.chain->name, nft_trans_chain_name(trans));
1b2470e5
FW
9444 rhltable_insert_key(&trans->ctx.table->chains_ht,
9445 trans->ctx.chain->name,
9446 &trans->ctx.chain->rhlhead,
9447 nft_chain_ht_params);
9448 }
91c7b38d 9449
f323d954 9450 if (!nft_is_base_chain(trans->ctx.chain))
91c7b38d
PNA
9451 return;
9452
53315ac6
FW
9453 nft_chain_stats_replace(trans);
9454
91c7b38d 9455 basechain = nft_base_chain(trans->ctx.chain);
91c7b38d
PNA
9456
9457 switch (nft_trans_chain_policy(trans)) {
9458 case NF_DROP:
9459 case NF_ACCEPT:
9460 basechain->policy = nft_trans_chain_policy(trans);
9461 break;
9462 }
9463}
9464
d62d0ba9
FFM
9465static void nft_obj_commit_update(struct nft_trans *trans)
9466{
9467 struct nft_object *newobj;
9468 struct nft_object *obj;
9469
9470 obj = nft_trans_obj(trans);
9471 newobj = nft_trans_obj_newobj(trans);
9472
84b1a0c0
PNA
9473 if (WARN_ON_ONCE(!obj->ops->update))
9474 return;
d62d0ba9 9475
84b1a0c0 9476 obj->ops->update(obj, newobj);
dad3bdee 9477 nft_obj_destroy(&trans->ctx, newobj);
d62d0ba9
FFM
9478}
9479
2f99aa31 9480static void nft_commit_release(struct nft_trans *trans)
c7c32e72 9481{
c7c32e72
PNA
9482 switch (trans->msg_type) {
9483 case NFT_MSG_DELTABLE:
f80a612d 9484 case NFT_MSG_DESTROYTABLE:
c7c32e72
PNA
9485 nf_tables_table_destroy(&trans->ctx);
9486 break;
9f8aac0b 9487 case NFT_MSG_NEWCHAIN:
53315ac6 9488 free_percpu(nft_trans_chain_stats(trans));
9f8aac0b
FW
9489 kfree(nft_trans_chain_name(trans));
9490 break;
c7c32e72 9491 case NFT_MSG_DELCHAIN:
f80a612d 9492 case NFT_MSG_DESTROYCHAIN:
7d937b10
PNA
9493 if (nft_trans_chain_update(trans))
9494 nft_hooks_destroy(&nft_trans_chain_hooks(trans));
9495 else
9496 nf_tables_chain_destroy(&trans->ctx);
c7c32e72
PNA
9497 break;
9498 case NFT_MSG_DELRULE:
f80a612d 9499 case NFT_MSG_DESTROYRULE:
c7c32e72
PNA
9500 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
9501 break;
9502 case NFT_MSG_DELSET:
f80a612d 9503 case NFT_MSG_DESTROYSET:
0c2a85ed 9504 nft_set_destroy(&trans->ctx, nft_trans_set(trans));
c7c32e72 9505 break;
61edafbb 9506 case NFT_MSG_DELSETELEM:
f80a612d 9507 case NFT_MSG_DESTROYSETELEM:
3453c927
PNA
9508 nf_tables_set_elem_destroy(&trans->ctx,
9509 nft_trans_elem_set(trans),
0e1ea651 9510 nft_trans_elem_priv(trans));
61edafbb 9511 break;
e5009240 9512 case NFT_MSG_DELOBJ:
f80a612d 9513 case NFT_MSG_DESTROYOBJ:
00bfb320 9514 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
e5009240 9515 break;
3b49e2e9 9516 case NFT_MSG_DELFLOWTABLE:
f80a612d 9517 case NFT_MSG_DESTROYFLOWTABLE:
abadb2f8 9518 if (nft_trans_flowtable_update(trans))
cdc32546 9519 nft_hooks_destroy(&nft_trans_flowtable_hooks(trans));
abadb2f8
PNA
9520 else
9521 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
3b49e2e9 9522 break;
c7c32e72 9523 }
0935d558
FW
9524
9525 if (trans->put_net)
9526 put_net(trans->ctx.net);
9527
c7c32e72
PNA
9528 kfree(trans);
9529}
9530
0935d558 9531static void nf_tables_trans_destroy_work(struct work_struct *w)
2f99aa31
FW
9532{
9533 struct nft_trans *trans, *next;
0935d558
FW
9534 LIST_HEAD(head);
9535
9536 spin_lock(&nf_tables_destroy_list_lock);
9537 list_splice_init(&nf_tables_destroy_list, &head);
9538 spin_unlock(&nf_tables_destroy_list_lock);
2f99aa31 9539
0935d558 9540 if (list_empty(&head))
2f99aa31
FW
9541 return;
9542
9543 synchronize_rcu();
9544
0935d558 9545 list_for_each_entry_safe(trans, next, &head, list) {
938154b9 9546 nft_trans_list_del(trans);
2f99aa31
FW
9547 nft_commit_release(trans);
9548 }
9549}
9550
ffe8923f
FW
9551void nf_tables_trans_destroy_flush_work(void)
9552{
9553 flush_work(&trans_destroy_work);
9554}
9555EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work);
9556
ed5f85d4
PNA
9557static bool nft_expr_reduce(struct nft_regs_track *track,
9558 const struct nft_expr *expr)
9559{
9e539c5b 9560 return false;
ed5f85d4
PNA
9561}
9562
0cbc06b3
FW
9563static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain)
9564{
2c865a8a 9565 const struct nft_expr *expr, *last;
12e4ecfa 9566 struct nft_regs_track track = {};
2c865a8a
PNA
9567 unsigned int size, data_size;
9568 void *data, *data_boundary;
9569 struct nft_rule_dp *prule;
0cbc06b3 9570 struct nft_rule *rule;
0cbc06b3
FW
9571
9572 /* already handled or inactive chain? */
2c865a8a 9573 if (chain->blob_next || !nft_is_active_next(net, chain))
0cbc06b3
FW
9574 return 0;
9575
63045bfd 9576 data_size = 0;
10377d42 9577 list_for_each_entry(rule, &chain->rules, list) {
2c865a8a
PNA
9578 if (nft_is_active_next(net, rule)) {
9579 data_size += sizeof(*prule) + rule->dlen;
9580 if (data_size > INT_MAX)
9581 return -ENOMEM;
9582 }
0cbc06b3
FW
9583 }
9584
e38fbfa9 9585 chain->blob_next = nf_tables_chain_alloc_rules(chain, data_size);
2c865a8a 9586 if (!chain->blob_next)
0cbc06b3
FW
9587 return -ENOMEM;
9588
2c865a8a
PNA
9589 data = (void *)chain->blob_next->data;
9590 data_boundary = data + data_size;
9591 size = 0;
9592
10377d42 9593 list_for_each_entry(rule, &chain->rules, list) {
2c865a8a
PNA
9594 if (!nft_is_active_next(net, rule))
9595 continue;
9596
9597 prule = (struct nft_rule_dp *)data;
9598 data += offsetof(struct nft_rule_dp, data);
9599 if (WARN_ON_ONCE(data > data_boundary))
9600 return -ENOMEM;
9601
12e4ecfa 9602 size = 0;
fe75e84a 9603 track.last = nft_expr_last(rule);
2c865a8a 9604 nft_rule_for_each_expr(expr, last, rule) {
12e4ecfa
PNA
9605 track.cur = expr;
9606
ed5f85d4 9607 if (nft_expr_reduce(&track, expr)) {
12e4ecfa
PNA
9608 expr = track.cur;
9609 continue;
9610 }
9611
08e42a0d 9612 if (WARN_ON_ONCE(data + size + expr->ops->size > data_boundary))
2c865a8a
PNA
9613 return -ENOMEM;
9614
9615 memcpy(data + size, expr, expr->ops->size);
9616 size += expr->ops->size;
9617 }
9618 if (WARN_ON_ONCE(size >= 1 << 12))
9619 return -ENOMEM;
9620
9621 prule->handle = rule->handle;
9622 prule->dlen = size;
9623 prule->is_last = 0;
9624
9625 data += size;
9626 size = 0;
9627 chain->blob_next->size += (unsigned long)(data - (void *)prule);
0cbc06b3
FW
9628 }
9629
2c865a8a
PNA
9630 if (WARN_ON_ONCE(data > data_boundary))
9631 return -ENOMEM;
9632
e38fbfa9
FW
9633 prule = (struct nft_rule_dp *)data;
9634 nft_last_rule(chain, prule);
2c865a8a 9635
0cbc06b3
FW
9636 return 0;
9637}
9638
9639static void nf_tables_commit_chain_prepare_cancel(struct net *net)
9640{
d59d2f82 9641 struct nftables_pernet *nft_net = nft_pernet(net);
0cbc06b3
FW
9642 struct nft_trans *trans, *next;
9643
0854db2a 9644 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
0cbc06b3
FW
9645 struct nft_chain *chain = trans->ctx.chain;
9646
9647 if (trans->msg_type == NFT_MSG_NEWRULE ||
9648 trans->msg_type == NFT_MSG_DELRULE) {
2c865a8a
PNA
9649 kvfree(chain->blob_next);
9650 chain->blob_next = NULL;
0cbc06b3
FW
9651 }
9652 }
9653}
9654
e38fbfa9 9655static void __nf_tables_commit_chain_free_rules(struct rcu_head *h)
0cbc06b3 9656{
e38fbfa9 9657 struct nft_rule_dp_last *l = container_of(h, struct nft_rule_dp_last, h);
0cbc06b3 9658
e38fbfa9 9659 kvfree(l->blob);
0cbc06b3
FW
9660}
9661
2c865a8a 9662static void nf_tables_commit_chain_free_rules_old(struct nft_rule_blob *blob)
0cbc06b3 9663{
e38fbfa9 9664 struct nft_rule_dp_last *last;
0cbc06b3 9665
e38fbfa9
FW
9666 /* last rule trailer is after end marker */
9667 last = (void *)blob + sizeof(*blob) + blob->size;
9668 last->blob = blob;
0cbc06b3 9669
e38fbfa9 9670 call_rcu(&last->h, __nf_tables_commit_chain_free_rules);
0cbc06b3
FW
9671}
9672
0fb39bbe 9673static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain)
0cbc06b3 9674{
2c865a8a 9675 struct nft_rule_blob *g0, *g1;
0cbc06b3
FW
9676 bool next_genbit;
9677
9678 next_genbit = nft_gencursor_next(net);
9679
2c865a8a 9680 g0 = rcu_dereference_protected(chain->blob_gen_0,
f102d66b 9681 lockdep_commit_lock_is_held(net));
2c865a8a 9682 g1 = rcu_dereference_protected(chain->blob_gen_1,
f102d66b 9683 lockdep_commit_lock_is_held(net));
0cbc06b3
FW
9684
9685 /* No changes to this chain? */
2c865a8a 9686 if (chain->blob_next == NULL) {
0cbc06b3
FW
9687 /* chain had no change in last or next generation */
9688 if (g0 == g1)
9689 return;
9690 /*
9691 * chain had no change in this generation; make sure next
9692 * one uses same rules as current generation.
9693 */
9694 if (next_genbit) {
2c865a8a 9695 rcu_assign_pointer(chain->blob_gen_1, g0);
0cbc06b3
FW
9696 nf_tables_commit_chain_free_rules_old(g1);
9697 } else {
2c865a8a 9698 rcu_assign_pointer(chain->blob_gen_0, g1);
0cbc06b3
FW
9699 nf_tables_commit_chain_free_rules_old(g0);
9700 }
9701
9702 return;
9703 }
9704
9705 if (next_genbit)
2c865a8a 9706 rcu_assign_pointer(chain->blob_gen_1, chain->blob_next);
0cbc06b3 9707 else
2c865a8a 9708 rcu_assign_pointer(chain->blob_gen_0, chain->blob_next);
0cbc06b3 9709
2c865a8a 9710 chain->blob_next = NULL;
0cbc06b3
FW
9711
9712 if (g0 == g1)
9713 return;
9714
9715 if (next_genbit)
9716 nf_tables_commit_chain_free_rules_old(g1);
9717 else
9718 nf_tables_commit_chain_free_rules_old(g0);
9719}
9720
d152159b
FW
9721static void nft_obj_del(struct nft_object *obj)
9722{
4d44175a 9723 rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params);
d152159b
FW
9724 list_del_rcu(&obj->list);
9725}
9726
d0e2c7de 9727void nft_chain_del(struct nft_chain *chain)
1b2470e5
FW
9728{
9729 struct nft_table *table = chain->table;
9730
9731 WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead,
9732 nft_chain_ht_params));
9733 list_del_rcu(&chain->list);
9734}
9735
5f68718b
PNA
9736static void nft_trans_gc_setelem_remove(struct nft_ctx *ctx,
9737 struct nft_trans_gc *trans)
9738{
0e1ea651 9739 struct nft_elem_priv **priv = trans->priv;
5f68718b
PNA
9740 unsigned int i;
9741
9742 for (i = 0; i < trans->count; i++) {
0e1ea651
PNA
9743 nft_setelem_data_deactivate(ctx->net, trans->set, priv[i]);
9744 nft_setelem_remove(ctx->net, trans->set, priv[i]);
5f68718b
PNA
9745 }
9746}
9747
9748void nft_trans_gc_destroy(struct nft_trans_gc *trans)
9749{
9750 nft_set_put(trans->set);
9751 put_net(trans->net);
9752 kfree(trans);
9753}
9754
9755static void nft_trans_gc_trans_free(struct rcu_head *rcu)
9756{
0e1ea651 9757 struct nft_elem_priv *elem_priv;
5f68718b
PNA
9758 struct nft_trans_gc *trans;
9759 struct nft_ctx ctx = {};
9760 unsigned int i;
9761
9762 trans = container_of(rcu, struct nft_trans_gc, rcu);
9763 ctx.net = read_pnet(&trans->set->net);
9764
9765 for (i = 0; i < trans->count; i++) {
0e1ea651
PNA
9766 elem_priv = trans->priv[i];
9767 if (!nft_setelem_is_catchall(trans->set, elem_priv))
5f68718b
PNA
9768 atomic_dec(&trans->set->nelems);
9769
0e1ea651 9770 nf_tables_set_elem_destroy(&ctx, trans->set, elem_priv);
5f68718b
PNA
9771 }
9772
9773 nft_trans_gc_destroy(trans);
9774}
9775
9776static bool nft_trans_gc_work_done(struct nft_trans_gc *trans)
9777{
9778 struct nftables_pernet *nft_net;
9779 struct nft_ctx ctx = {};
9780
9781 nft_net = nft_pernet(trans->net);
9782
9783 mutex_lock(&nft_net->commit_mutex);
9784
9785 /* Check for race with transaction, otherwise this batch refers to
9786 * stale objects that might not be there anymore. Skip transaction if
9787 * set has been destroyed from control plane transaction in case gc
9788 * worker loses race.
9789 */
9790 if (READ_ONCE(nft_net->gc_seq) != trans->seq || trans->set->dead) {
9791 mutex_unlock(&nft_net->commit_mutex);
9792 return false;
9793 }
9794
9795 ctx.net = trans->net;
9796 ctx.table = trans->set->table;
9797
9798 nft_trans_gc_setelem_remove(&ctx, trans);
9799 mutex_unlock(&nft_net->commit_mutex);
9800
9801 return true;
9802}
9803
9804static void nft_trans_gc_work(struct work_struct *work)
9805{
9806 struct nft_trans_gc *trans, *next;
9807 LIST_HEAD(trans_gc_list);
9808
8357bc94 9809 spin_lock(&nf_tables_gc_list_lock);
5f68718b 9810 list_splice_init(&nf_tables_gc_list, &trans_gc_list);
8357bc94 9811 spin_unlock(&nf_tables_gc_list_lock);
5f68718b
PNA
9812
9813 list_for_each_entry_safe(trans, next, &trans_gc_list, list) {
9814 list_del(&trans->list);
9815 if (!nft_trans_gc_work_done(trans)) {
9816 nft_trans_gc_destroy(trans);
9817 continue;
9818 }
9819 call_rcu(&trans->rcu, nft_trans_gc_trans_free);
9820 }
9821}
9822
9823struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set,
9824 unsigned int gc_seq, gfp_t gfp)
9825{
9826 struct net *net = read_pnet(&set->net);
9827 struct nft_trans_gc *trans;
9828
9829 trans = kzalloc(sizeof(*trans), gfp);
9830 if (!trans)
9831 return NULL;
9832
02c6c244
PNA
9833 trans->net = maybe_get_net(net);
9834 if (!trans->net) {
9835 kfree(trans);
9836 return NULL;
9837 }
9838
5f68718b
PNA
9839 refcount_inc(&set->refs);
9840 trans->set = set;
5f68718b
PNA
9841 trans->seq = gc_seq;
9842
9843 return trans;
9844}
9845
9846void nft_trans_gc_elem_add(struct nft_trans_gc *trans, void *priv)
9847{
9848 trans->priv[trans->count++] = priv;
9849}
9850
9851static void nft_trans_gc_queue_work(struct nft_trans_gc *trans)
9852{
9853 spin_lock(&nf_tables_gc_list_lock);
9854 list_add_tail(&trans->list, &nf_tables_gc_list);
9855 spin_unlock(&nf_tables_gc_list_lock);
9856
9857 schedule_work(&trans_gc_work);
9858}
9859
9860static int nft_trans_gc_space(struct nft_trans_gc *trans)
9861{
9862 return NFT_TRANS_GC_BATCHCOUNT - trans->count;
9863}
9864
9865struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc,
9866 unsigned int gc_seq, gfp_t gfp)
9867{
cf5000a7
FW
9868 struct nft_set *set;
9869
5f68718b
PNA
9870 if (nft_trans_gc_space(gc))
9871 return gc;
9872
cf5000a7 9873 set = gc->set;
5f68718b
PNA
9874 nft_trans_gc_queue_work(gc);
9875
cf5000a7 9876 return nft_trans_gc_alloc(set, gc_seq, gfp);
5f68718b
PNA
9877}
9878
9879void nft_trans_gc_queue_async_done(struct nft_trans_gc *trans)
9880{
9881 if (trans->count == 0) {
9882 nft_trans_gc_destroy(trans);
9883 return;
9884 }
9885
9886 nft_trans_gc_queue_work(trans);
9887}
9888
9889struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp)
9890{
cf5000a7
FW
9891 struct nft_set *set;
9892
5f68718b
PNA
9893 if (WARN_ON_ONCE(!lockdep_commit_lock_is_held(gc->net)))
9894 return NULL;
9895
9896 if (nft_trans_gc_space(gc))
9897 return gc;
9898
cf5000a7 9899 set = gc->set;
5f68718b
PNA
9900 call_rcu(&gc->rcu, nft_trans_gc_trans_free);
9901
cf5000a7 9902 return nft_trans_gc_alloc(set, 0, gfp);
5f68718b
PNA
9903}
9904
9905void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans)
9906{
9907 WARN_ON_ONCE(!lockdep_commit_lock_is_held(trans->net));
9908
9909 if (trans->count == 0) {
9910 nft_trans_gc_destroy(trans);
9911 return;
9912 }
9913
9914 call_rcu(&trans->rcu, nft_trans_gc_trans_free);
9915}
9916
8837ba3e
PNA
9917struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,
9918 unsigned int gc_seq)
5f68718b 9919{
8837ba3e 9920 struct nft_set_elem_catchall *catchall;
5f68718b
PNA
9921 const struct nft_set *set = gc->set;
9922 struct nft_set_ext *ext;
9923
8837ba3e 9924 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
5f68718b
PNA
9925 ext = nft_set_elem_ext(set, catchall->elem);
9926
9927 if (!nft_set_elem_expired(ext))
9928 continue;
9929 if (nft_set_elem_is_dead(ext))
9930 goto dead_elem;
9931
9932 nft_set_elem_dead(ext);
9933dead_elem:
8837ba3e 9934 gc = nft_trans_gc_queue_async(gc, gc_seq, GFP_ATOMIC);
5f68718b
PNA
9935 if (!gc)
9936 return NULL;
9937
8837ba3e 9938 nft_trans_gc_elem_add(gc, catchall->elem);
5f68718b
PNA
9939 }
9940
9941 return gc;
9942}
9943
4a9e12ea
PNA
9944struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc)
9945{
8837ba3e 9946 struct nft_set_elem_catchall *catchall, *next;
7395dfac 9947 u64 tstamp = nft_net_tstamp(gc->net);
8837ba3e
PNA
9948 const struct nft_set *set = gc->set;
9949 struct nft_elem_priv *elem_priv;
9950 struct nft_set_ext *ext;
9951
9952 WARN_ON_ONCE(!lockdep_commit_lock_is_held(gc->net));
9953
9954 list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
9955 ext = nft_set_elem_ext(set, catchall->elem);
9956
7395dfac 9957 if (!__nft_set_elem_expired(ext, tstamp))
8837ba3e
PNA
9958 continue;
9959
9960 gc = nft_trans_gc_queue_sync(gc, GFP_KERNEL);
9961 if (!gc)
9962 return NULL;
9963
9964 elem_priv = catchall->elem;
9965 nft_setelem_data_deactivate(gc->net, gc->set, elem_priv);
9966 nft_setelem_catchall_destroy(catchall);
9967 nft_trans_gc_elem_add(gc, elem_priv);
9968 }
9969
9970 return gc;
4a9e12ea
PNA
9971}
9972
eb014de4
PNA
9973static void nf_tables_module_autoload_cleanup(struct net *net)
9974{
d59d2f82 9975 struct nftables_pernet *nft_net = nft_pernet(net);
eb014de4
PNA
9976 struct nft_module_request *req, *next;
9977
0854db2a
FW
9978 WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
9979 list_for_each_entry_safe(req, next, &nft_net->module_list, list) {
eb014de4
PNA
9980 WARN_ON_ONCE(!req->done);
9981 list_del(&req->list);
9982 kfree(req);
9983 }
9984}
9985
0935d558
FW
9986static void nf_tables_commit_release(struct net *net)
9987{
d59d2f82 9988 struct nftables_pernet *nft_net = nft_pernet(net);
0935d558
FW
9989 struct nft_trans *trans;
9990
9991 /* all side effects have to be made visible.
9992 * For example, if a chain named 'foo' has been deleted, a
9993 * new transaction must not find it anymore.
9994 *
9995 * Memory reclaim happens asynchronously from work queue
9996 * to prevent expensive synchronize_rcu() in commit phase.
9997 */
0854db2a 9998 if (list_empty(&nft_net->commit_list)) {
eb014de4 9999 nf_tables_module_autoload_cleanup(net);
0854db2a 10000 mutex_unlock(&nft_net->commit_mutex);
0935d558
FW
10001 return;
10002 }
10003
0854db2a 10004 trans = list_last_entry(&nft_net->commit_list,
0935d558
FW
10005 struct nft_trans, list);
10006 get_net(trans->ctx.net);
10007 WARN_ON_ONCE(trans->put_net);
10008
10009 trans->put_net = true;
10010 spin_lock(&nf_tables_destroy_list_lock);
0854db2a 10011 list_splice_tail_init(&nft_net->commit_list, &nf_tables_destroy_list);
0935d558
FW
10012 spin_unlock(&nf_tables_destroy_list_lock);
10013
eb014de4 10014 nf_tables_module_autoload_cleanup(net);
0935d558 10015 schedule_work(&trans_destroy_work);
ffe8923f 10016
0854db2a 10017 mutex_unlock(&nft_net->commit_mutex);
0935d558
FW
10018}
10019
67cc570e
PNA
10020static void nft_commit_notify(struct net *net, u32 portid)
10021{
d59d2f82 10022 struct nftables_pernet *nft_net = nft_pernet(net);
67cc570e
PNA
10023 struct sk_buff *batch_skb = NULL, *nskb, *skb;
10024 unsigned char *data;
10025 int len;
10026
0854db2a 10027 list_for_each_entry_safe(skb, nskb, &nft_net->notify_list, list) {
67cc570e
PNA
10028 if (!batch_skb) {
10029new_batch:
10030 batch_skb = skb;
10031 len = NLMSG_GOODSIZE - skb->len;
10032 list_del(&skb->list);
10033 continue;
10034 }
10035 len -= skb->len;
10036 if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) {
10037 data = skb_put(batch_skb, skb->len);
10038 memcpy(data, skb->data, skb->len);
10039 list_del(&skb->list);
10040 kfree_skb(skb);
10041 continue;
10042 }
10043 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
10044 NFT_CB(batch_skb).report, GFP_KERNEL);
10045 goto new_batch;
10046 }
10047
10048 if (batch_skb) {
10049 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
10050 NFT_CB(batch_skb).report, GFP_KERNEL);
10051 }
10052
0854db2a 10053 WARN_ON_ONCE(!list_empty(&nft_net->notify_list));
67cc570e
PNA
10054}
10055
c520292f
RGB
10056static int nf_tables_commit_audit_alloc(struct list_head *adl,
10057 struct nft_table *table)
10058{
10059 struct nft_audit_data *adp;
10060
10061 list_for_each_entry(adp, adl, list) {
10062 if (adp->table == table)
10063 return 0;
10064 }
10065 adp = kzalloc(sizeof(*adp), GFP_KERNEL);
10066 if (!adp)
10067 return -ENOMEM;
10068 adp->table = table;
10069 list_add(&adp->list, adl);
10070 return 0;
10071}
10072
cfbe3650
DM
10073static void nf_tables_commit_audit_free(struct list_head *adl)
10074{
10075 struct nft_audit_data *adp, *adn;
10076
10077 list_for_each_entry_safe(adp, adn, adl, list) {
10078 list_del(&adp->list);
10079 kfree(adp);
10080 }
10081}
10082
c520292f
RGB
10083static void nf_tables_commit_audit_collect(struct list_head *adl,
10084 struct nft_table *table, u32 op)
10085{
10086 struct nft_audit_data *adp;
10087
10088 list_for_each_entry(adp, adl, list) {
10089 if (adp->table == table)
10090 goto found;
10091 }
dadf33c9 10092 WARN_ONCE(1, "table=%s not expected in commit list", table->name);
c520292f
RGB
10093 return;
10094found:
10095 adp->entries++;
10096 if (!adp->op || adp->op > op)
10097 adp->op = op;
10098}
10099
10100#define AUNFTABLENAMELEN (NFT_TABLE_MAXNAMELEN + 22)
10101
10102static void nf_tables_commit_audit_log(struct list_head *adl, u32 generation)
10103{
10104 struct nft_audit_data *adp, *adn;
10105 char aubuf[AUNFTABLENAMELEN];
10106
10107 list_for_each_entry_safe(adp, adn, adl, list) {
10108 snprintf(aubuf, AUNFTABLENAMELEN, "%s:%u", adp->table->name,
10109 generation);
10110 audit_log_nfcfg(aubuf, adp->table->family, adp->entries,
10111 nft2audit_op[adp->op], GFP_KERNEL);
10112 list_del(&adp->list);
10113 kfree(adp);
10114 }
10115}
10116
212ed75d
PNA
10117static void nft_set_commit_update(struct list_head *set_update_list)
10118{
10119 struct nft_set *set, *next;
10120
10121 list_for_each_entry_safe(set, next, set_update_list, pending_update) {
10122 list_del_init(&set->pending_update);
10123
7315dc1e 10124 if (!set->ops->commit || set->dead)
212ed75d
PNA
10125 continue;
10126
10127 set->ops->commit(set);
10128 }
10129}
10130
6a33d8b7
PNA
10131static unsigned int nft_gc_seq_begin(struct nftables_pernet *nft_net)
10132{
10133 unsigned int gc_seq;
10134
10135 /* Bump gc counter, it becomes odd, this is the busy mark. */
10136 gc_seq = READ_ONCE(nft_net->gc_seq);
10137 WRITE_ONCE(nft_net->gc_seq, ++gc_seq);
10138
10139 return gc_seq;
10140}
10141
10142static void nft_gc_seq_end(struct nftables_pernet *nft_net, unsigned int gc_seq)
10143{
10144 WRITE_ONCE(nft_net->gc_seq, ++gc_seq);
10145}
10146
5913beaf 10147static int nf_tables_commit(struct net *net, struct sk_buff *skb)
37082f93 10148{
d59d2f82 10149 struct nftables_pernet *nft_net = nft_pernet(net);
37082f93 10150 struct nft_trans *trans, *next;
5f68718b 10151 unsigned int base_seq, gc_seq;
212ed75d 10152 LIST_HEAD(set_update_list);
a3716e70 10153 struct nft_trans_elem *te;
0cbc06b3
FW
10154 struct nft_chain *chain;
10155 struct nft_table *table;
c520292f 10156 LIST_HEAD(adl);
c9626a2c 10157 int err;
37082f93 10158
0854db2a
FW
10159 if (list_empty(&nft_net->commit_list)) {
10160 mutex_unlock(&nft_net->commit_mutex);
b8b27498
FW
10161 return 0;
10162 }
10163
938154b9
PNA
10164 list_for_each_entry(trans, &nft_net->binding_list, binding_list) {
10165 switch (trans->msg_type) {
10166 case NFT_MSG_NEWSET:
10167 if (!nft_trans_set_update(trans) &&
10168 nft_set_is_anonymous(nft_trans_set(trans)) &&
10169 !nft_trans_set_bound(trans)) {
10170 pr_warn_once("nftables ruleset with unbound set\n");
10171 return -EINVAL;
10172 }
10173 break;
62e1e94b
PNA
10174 case NFT_MSG_NEWCHAIN:
10175 if (!nft_trans_chain_update(trans) &&
10176 nft_chain_binding(nft_trans_chain(trans)) &&
10177 !nft_trans_chain_bound(trans)) {
10178 pr_warn_once("nftables ruleset with unbound chain\n");
10179 return -EINVAL;
10180 }
10181 break;
938154b9
PNA
10182 }
10183 }
10184
a654de8f 10185 /* 0. Validate ruleset, otherwise roll back for error reporting. */
4b80ced9
FW
10186 if (nf_tables_validate(net) < 0) {
10187 nft_net->validate_state = NFT_VALIDATE_DO;
a654de8f 10188 return -EAGAIN;
4b80ced9 10189 }
a654de8f 10190
c9626a2c
PNA
10191 err = nft_flow_rule_offload_commit(net);
10192 if (err < 0)
10193 return err;
10194
0cbc06b3 10195 /* 1. Allocate space for next generation rules_gen_X[] */
0854db2a 10196 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
0cbc06b3 10197 int ret;
37082f93 10198
c520292f
RGB
10199 ret = nf_tables_commit_audit_alloc(&adl, trans->ctx.table);
10200 if (ret) {
10201 nf_tables_commit_chain_prepare_cancel(net);
cfbe3650 10202 nf_tables_commit_audit_free(&adl);
c520292f
RGB
10203 return ret;
10204 }
0cbc06b3
FW
10205 if (trans->msg_type == NFT_MSG_NEWRULE ||
10206 trans->msg_type == NFT_MSG_DELRULE) {
10207 chain = trans->ctx.chain;
10208
10209 ret = nf_tables_commit_chain_prepare(net, chain);
10210 if (ret < 0) {
10211 nf_tables_commit_chain_prepare_cancel(net);
cfbe3650 10212 nf_tables_commit_audit_free(&adl);
0cbc06b3
FW
10213 return ret;
10214 }
10215 }
10216 }
37082f93 10217
0cbc06b3 10218 /* step 2. Make rules_gen_X visible to packet path */
0854db2a 10219 list_for_each_entry(table, &nft_net->tables, list) {
0fb39bbe
FW
10220 list_for_each_entry(chain, &table->chains, list)
10221 nf_tables_commit_chain(net, chain);
0cbc06b3
FW
10222 }
10223
10224 /*
10225 * Bump generation counter, invalidate any dump in progress.
10226 * Cannot fail after this point.
37082f93 10227 */
34002783
PNA
10228 base_seq = READ_ONCE(nft_net->base_seq);
10229 while (++base_seq == 0)
0854db2a 10230 ;
0cbc06b3 10231
34002783
PNA
10232 WRITE_ONCE(nft_net->base_seq, base_seq);
10233
6a33d8b7 10234 gc_seq = nft_gc_seq_begin(nft_net);
5f68718b 10235
0cbc06b3
FW
10236 /* step 3. Start new generation, rules_gen_X now in use. */
10237 net->nft.gencursor = nft_gencursor_next(net);
37082f93 10238
0854db2a 10239 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
c520292f
RGB
10240 nf_tables_commit_audit_collect(&adl, trans->ctx.table,
10241 trans->msg_type);
b380e5c7 10242 switch (trans->msg_type) {
55dd6f93
PNA
10243 case NFT_MSG_NEWTABLE:
10244 if (nft_trans_table_update(trans)) {
179d9ba5
PNA
10245 if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) {
10246 nft_trans_destroy(trans);
10247 break;
10248 }
10249 if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT)
0ce7cf41
PNA
10250 nf_tables_table_disable(net, trans->ctx.table);
10251
179d9ba5 10252 trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE;
55dd6f93 10253 } else {
f2a6d766 10254 nft_clear(net, trans->ctx.table);
55dd6f93 10255 }
35151d84 10256 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
55dd6f93
PNA
10257 nft_trans_destroy(trans);
10258 break;
10259 case NFT_MSG_DELTABLE:
f80a612d 10260 case NFT_MSG_DESTROYTABLE:
f2a6d766 10261 list_del_rcu(&trans->ctx.table->list);
f80a612d 10262 nf_tables_table_notify(&trans->ctx, trans->msg_type);
55dd6f93 10263 break;
91c7b38d 10264 case NFT_MSG_NEWCHAIN:
9f8aac0b 10265 if (nft_trans_chain_update(trans)) {
91c7b38d 10266 nft_chain_commit_update(trans);
b9703ed4
PNA
10267 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN,
10268 &nft_trans_chain_hooks(trans));
10269 list_splice(&nft_trans_chain_hooks(trans),
10270 &nft_trans_basechain(trans)->hook_list);
9f8aac0b
FW
10271 /* trans destroyed after rcu grace period */
10272 } else {
66293c46 10273 nft_chain_commit_drop_policy(trans);
664b0f8c 10274 nft_clear(net, trans->ctx.chain);
b9703ed4 10275 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN, NULL);
9f8aac0b
FW
10276 nft_trans_destroy(trans);
10277 }
91c7b38d
PNA
10278 break;
10279 case NFT_MSG_DELCHAIN:
f80a612d 10280 case NFT_MSG_DESTROYCHAIN:
7d937b10
PNA
10281 if (nft_trans_chain_update(trans)) {
10282 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN,
10283 &nft_trans_chain_hooks(trans));
216e7bf7
PNA
10284 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT)) {
10285 nft_netdev_unregister_hooks(net,
10286 &nft_trans_chain_hooks(trans),
10287 true);
10288 }
7d937b10
PNA
10289 } else {
10290 nft_chain_del(trans->ctx.chain);
10291 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN,
10292 NULL);
10293 nf_tables_unregister_hook(trans->ctx.net,
10294 trans->ctx.table,
10295 trans->ctx.chain);
10296 }
91c7b38d 10297 break;
b380e5c7 10298 case NFT_MSG_NEWRULE:
889f7ee7 10299 nft_clear(trans->ctx.net, nft_trans_rule(trans));
35151d84 10300 nf_tables_rule_notify(&trans->ctx,
37082f93 10301 nft_trans_rule(trans),
35151d84 10302 NFT_MSG_NEWRULE);
9dd732e0
PNA
10303 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
10304 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
10305
37082f93 10306 nft_trans_destroy(trans);
b380e5c7
PNA
10307 break;
10308 case NFT_MSG_DELRULE:
f80a612d 10309 case NFT_MSG_DESTROYRULE:
b380e5c7 10310 list_del_rcu(&nft_trans_rule(trans)->list);
35151d84
PNA
10311 nf_tables_rule_notify(&trans->ctx,
10312 nft_trans_rule(trans),
f80a612d 10313 trans->msg_type);
f6ac8585
PNA
10314 nft_rule_expr_deactivate(&trans->ctx,
10315 nft_trans_rule(trans),
10316 NFT_TRANS_COMMIT);
26b5934f
PNA
10317
10318 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
10319 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
b380e5c7 10320 break;
958bee14 10321 case NFT_MSG_NEWSET:
123b9961
PNA
10322 if (nft_trans_set_update(trans)) {
10323 struct nft_set *set = nft_trans_set(trans);
4fefee57 10324
123b9961
PNA
10325 WRITE_ONCE(set->timeout, nft_trans_set_timeout(trans));
10326 WRITE_ONCE(set->gc_int, nft_trans_set_gc_int(trans));
96b2ef9b
FW
10327
10328 if (nft_trans_set_size(trans))
10329 WRITE_ONCE(set->size, nft_trans_set_size(trans));
123b9961
PNA
10330 } else {
10331 nft_clear(net, nft_trans_set(trans));
10332 /* This avoids hitting -EBUSY when deleting the table
10333 * from the transaction.
10334 */
10335 if (nft_set_is_anonymous(nft_trans_set(trans)) &&
10336 !list_empty(&nft_trans_set(trans)->bindings))
1689f259 10337 nft_use_dec(&trans->ctx.table->use);
123b9961 10338 }
958bee14 10339 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
31f8441c 10340 NFT_MSG_NEWSET, GFP_KERNEL);
958bee14
PNA
10341 nft_trans_destroy(trans);
10342 break;
10343 case NFT_MSG_DELSET:
f80a612d 10344 case NFT_MSG_DESTROYSET:
5f68718b 10345 nft_trans_set(trans)->dead = 1;
37a9cc52 10346 list_del_rcu(&nft_trans_set(trans)->list);
958bee14 10347 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
f80a612d 10348 trans->msg_type, GFP_KERNEL);
958bee14 10349 break;
60319eb1 10350 case NFT_MSG_NEWSETELEM:
cc02e457
PM
10351 te = (struct nft_trans_elem *)trans->data;
10352
0e1ea651 10353 nft_setelem_activate(net, te->set, te->elem_priv);
cc02e457 10354 nf_tables_setelem_notify(&trans->ctx, te->set,
0e1ea651 10355 te->elem_priv,
6fb721cf 10356 NFT_MSG_NEWSETELEM);
212ed75d
PNA
10357 if (te->set->ops->commit &&
10358 list_empty(&te->set->pending_update)) {
10359 list_add_tail(&te->set->pending_update,
10360 &set_update_list);
10361 }
60319eb1
PNA
10362 nft_trans_destroy(trans);
10363 break;
10364 case NFT_MSG_DELSETELEM:
f80a612d 10365 case NFT_MSG_DESTROYSETELEM:
a3716e70 10366 te = (struct nft_trans_elem *)trans->data;
fe2811eb 10367
a3716e70 10368 nf_tables_setelem_notify(&trans->ctx, te->set,
0e1ea651 10369 te->elem_priv,
f80a612d 10370 trans->msg_type);
0e1ea651
PNA
10371 nft_setelem_remove(net, te->set, te->elem_priv);
10372 if (!nft_setelem_is_catchall(te->set, te->elem_priv)) {
aaa31047
PNA
10373 atomic_dec(&te->set->nelems);
10374 te->set->ndeact--;
10375 }
212ed75d
PNA
10376 if (te->set->ops->commit &&
10377 list_empty(&te->set->pending_update)) {
10378 list_add_tail(&te->set->pending_update,
10379 &set_update_list);
10380 }
60319eb1 10381 break;
e5009240 10382 case NFT_MSG_NEWOBJ:
d62d0ba9
FFM
10383 if (nft_trans_obj_update(trans)) {
10384 nft_obj_commit_update(trans);
10385 nf_tables_obj_notify(&trans->ctx,
10386 nft_trans_obj(trans),
10387 NFT_MSG_NEWOBJ);
10388 } else {
10389 nft_clear(net, nft_trans_obj(trans));
10390 nf_tables_obj_notify(&trans->ctx,
10391 nft_trans_obj(trans),
10392 NFT_MSG_NEWOBJ);
10393 nft_trans_destroy(trans);
10394 }
e5009240
PNA
10395 break;
10396 case NFT_MSG_DELOBJ:
f80a612d 10397 case NFT_MSG_DESTROYOBJ:
d152159b 10398 nft_obj_del(nft_trans_obj(trans));
e5009240 10399 nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
f80a612d 10400 trans->msg_type);
e5009240 10401 break;
3b49e2e9 10402 case NFT_MSG_NEWFLOWTABLE:
78d9f48f 10403 if (nft_trans_flowtable_update(trans)) {
7b35582c
PNA
10404 nft_trans_flowtable(trans)->data.flags =
10405 nft_trans_flowtable_flags(trans);
78d9f48f
PNA
10406 nf_tables_flowtable_notify(&trans->ctx,
10407 nft_trans_flowtable(trans),
10408 &nft_trans_flowtable_hooks(trans),
10409 NFT_MSG_NEWFLOWTABLE);
10410 list_splice(&nft_trans_flowtable_hooks(trans),
10411 &nft_trans_flowtable(trans)->hook_list);
10412 } else {
10413 nft_clear(net, nft_trans_flowtable(trans));
10414 nf_tables_flowtable_notify(&trans->ctx,
10415 nft_trans_flowtable(trans),
28339b21 10416 NULL,
78d9f48f
PNA
10417 NFT_MSG_NEWFLOWTABLE);
10418 }
3b49e2e9
PNA
10419 nft_trans_destroy(trans);
10420 break;
10421 case NFT_MSG_DELFLOWTABLE:
f80a612d 10422 case NFT_MSG_DESTROYFLOWTABLE:
abadb2f8 10423 if (nft_trans_flowtable_update(trans)) {
abadb2f8
PNA
10424 nf_tables_flowtable_notify(&trans->ctx,
10425 nft_trans_flowtable(trans),
10426 &nft_trans_flowtable_hooks(trans),
f80a612d 10427 trans->msg_type);
abadb2f8
PNA
10428 nft_unregister_flowtable_net_hooks(net,
10429 &nft_trans_flowtable_hooks(trans));
10430 } else {
10431 list_del_rcu(&nft_trans_flowtable(trans)->list);
10432 nf_tables_flowtable_notify(&trans->ctx,
10433 nft_trans_flowtable(trans),
28339b21 10434 NULL,
f80a612d 10435 trans->msg_type);
abadb2f8
PNA
10436 nft_unregister_flowtable_net_hooks(net,
10437 &nft_trans_flowtable(trans)->hook_list);
10438 }
3b49e2e9 10439 break;
37082f93 10440 }
37082f93
PNA
10441 }
10442
212ed75d
PNA
10443 nft_set_commit_update(&set_update_list);
10444
67cc570e 10445 nft_commit_notify(net, NETLINK_CB(skb).portid);
84d7fce6 10446 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
0854db2a 10447 nf_tables_commit_audit_log(&adl, nft_net->base_seq);
5f68718b 10448
6a33d8b7 10449 nft_gc_seq_end(nft_net, gc_seq);
4b80ced9 10450 nft_net->validate_state = NFT_VALIDATE_SKIP;
0935d558 10451 nf_tables_commit_release(net);
37082f93
PNA
10452
10453 return 0;
10454}
10455
eb014de4
PNA
10456static void nf_tables_module_autoload(struct net *net)
10457{
d59d2f82 10458 struct nftables_pernet *nft_net = nft_pernet(net);
eb014de4
PNA
10459 struct nft_module_request *req, *next;
10460 LIST_HEAD(module_list);
10461
0854db2a
FW
10462 list_splice_init(&nft_net->module_list, &module_list);
10463 mutex_unlock(&nft_net->commit_mutex);
eb014de4 10464 list_for_each_entry_safe(req, next, &module_list, list) {
1d305ba4
FW
10465 request_module("%s", req->module);
10466 req->done = true;
eb014de4 10467 }
0854db2a
FW
10468 mutex_lock(&nft_net->commit_mutex);
10469 list_splice(&module_list, &nft_net->module_list);
eb014de4
PNA
10470}
10471
b326dd37 10472static void nf_tables_abort_release(struct nft_trans *trans)
c7c32e72 10473{
c7c32e72
PNA
10474 switch (trans->msg_type) {
10475 case NFT_MSG_NEWTABLE:
10476 nf_tables_table_destroy(&trans->ctx);
10477 break;
10478 case NFT_MSG_NEWCHAIN:
b9703ed4
PNA
10479 if (nft_trans_chain_update(trans))
10480 nft_hooks_destroy(&nft_trans_chain_hooks(trans));
10481 else
10482 nf_tables_chain_destroy(&trans->ctx);
c7c32e72
PNA
10483 break;
10484 case NFT_MSG_NEWRULE:
10485 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
10486 break;
10487 case NFT_MSG_NEWSET:
0c2a85ed 10488 nft_set_destroy(&trans->ctx, nft_trans_set(trans));
c7c32e72 10489 break;
61edafbb
PM
10490 case NFT_MSG_NEWSETELEM:
10491 nft_set_elem_destroy(nft_trans_elem_set(trans),
0e1ea651 10492 nft_trans_elem_priv(trans), true);
61edafbb 10493 break;
e5009240 10494 case NFT_MSG_NEWOBJ:
00bfb320 10495 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
e5009240 10496 break;
3b49e2e9 10497 case NFT_MSG_NEWFLOWTABLE:
78d9f48f 10498 if (nft_trans_flowtable_update(trans))
cdc32546 10499 nft_hooks_destroy(&nft_trans_flowtable_hooks(trans));
78d9f48f
PNA
10500 else
10501 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
3b49e2e9 10502 break;
c7c32e72
PNA
10503 }
10504 kfree(trans);
10505}
10506
212ed75d
PNA
10507static void nft_set_abort_update(struct list_head *set_update_list)
10508{
10509 struct nft_set *set, *next;
10510
10511 list_for_each_entry_safe(set, next, set_update_list, pending_update) {
10512 list_del_init(&set->pending_update);
10513
10514 if (!set->ops->abort)
10515 continue;
10516
10517 set->ops->abort(set);
10518 }
10519}
10520
c0391b6a 10521static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
37082f93 10522{
d59d2f82 10523 struct nftables_pernet *nft_net = nft_pernet(net);
37082f93 10524 struct nft_trans *trans, *next;
212ed75d 10525 LIST_HEAD(set_update_list);
02263db0 10526 struct nft_trans_elem *te;
a45e6889 10527 int err = 0;
37082f93 10528
c0391b6a
PNA
10529 if (action == NFNL_ABORT_VALIDATE &&
10530 nf_tables_validate(net) < 0)
a45e6889 10531 err = -EAGAIN;
c0391b6a 10532
0854db2a 10533 list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list,
a907e36d 10534 list) {
b380e5c7 10535 switch (trans->msg_type) {
55dd6f93
PNA
10536 case NFT_MSG_NEWTABLE:
10537 if (nft_trans_table_update(trans)) {
179d9ba5
PNA
10538 if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) {
10539 nft_trans_destroy(trans);
10540 break;
10541 }
10542 if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_DORMANT) {
0ce7cf41 10543 nf_tables_table_disable(net, trans->ctx.table);
179d9ba5
PNA
10544 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
10545 } else if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_AWAKEN) {
10546 trans->ctx.table->flags &= ~NFT_TABLE_F_DORMANT;
10547 }
31bf508b
PS
10548 if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_ORPHAN) {
10549 trans->ctx.table->flags &= ~NFT_TABLE_F_OWNER;
10550 trans->ctx.table->nlpid = 0;
10551 }
179d9ba5 10552 trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE;
55dd6f93
PNA
10553 nft_trans_destroy(trans);
10554 } else {
e688a7f8 10555 list_del_rcu(&trans->ctx.table->list);
55dd6f93
PNA
10556 }
10557 break;
10558 case NFT_MSG_DELTABLE:
f80a612d 10559 case NFT_MSG_DESTROYTABLE:
f2a6d766 10560 nft_clear(trans->ctx.net, trans->ctx.table);
55dd6f93
PNA
10561 nft_trans_destroy(trans);
10562 break;
91c7b38d
PNA
10563 case NFT_MSG_NEWCHAIN:
10564 if (nft_trans_chain_update(trans)) {
216e7bf7
PNA
10565 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT)) {
10566 nft_netdev_unregister_hooks(net,
10567 &nft_trans_chain_hooks(trans),
10568 true);
10569 }
982f4051 10570 free_percpu(nft_trans_chain_stats(trans));
9f8aac0b 10571 kfree(nft_trans_chain_name(trans));
91c7b38d
PNA
10572 nft_trans_destroy(trans);
10573 } else {
4bedf9ee 10574 if (nft_trans_chain_bound(trans)) {
d0e2c7de
PNA
10575 nft_trans_destroy(trans);
10576 break;
10577 }
1689f259 10578 nft_use_dec_restore(&trans->ctx.table->use);
1b2470e5 10579 nft_chain_del(trans->ctx.chain);
c974a3a3
PNA
10580 nf_tables_unregister_hook(trans->ctx.net,
10581 trans->ctx.table,
10582 trans->ctx.chain);
91c7b38d
PNA
10583 }
10584 break;
10585 case NFT_MSG_DELCHAIN:
f80a612d 10586 case NFT_MSG_DESTROYCHAIN:
7d937b10
PNA
10587 if (nft_trans_chain_update(trans)) {
10588 list_splice(&nft_trans_chain_hooks(trans),
10589 &nft_trans_basechain(trans)->hook_list);
10590 } else {
1689f259 10591 nft_use_inc_restore(&trans->ctx.table->use);
7d937b10
PNA
10592 nft_clear(trans->ctx.net, trans->ctx.chain);
10593 }
91c7b38d
PNA
10594 nft_trans_destroy(trans);
10595 break;
b380e5c7 10596 case NFT_MSG_NEWRULE:
4bedf9ee
PNA
10597 if (nft_trans_rule_bound(trans)) {
10598 nft_trans_destroy(trans);
10599 break;
10600 }
1689f259 10601 nft_use_dec_restore(&trans->ctx.chain->use);
b380e5c7 10602 list_del_rcu(&nft_trans_rule(trans)->list);
f6ac8585
PNA
10603 nft_rule_expr_deactivate(&trans->ctx,
10604 nft_trans_rule(trans),
10605 NFT_TRANS_ABORT);
3c5e4462
PNA
10606 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
10607 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
b380e5c7
PNA
10608 break;
10609 case NFT_MSG_DELRULE:
f80a612d 10610 case NFT_MSG_DESTROYRULE:
1689f259 10611 nft_use_inc_restore(&trans->ctx.chain->use);
889f7ee7 10612 nft_clear(trans->ctx.net, nft_trans_rule(trans));
bb7b40ae 10613 nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans));
3c5e4462
PNA
10614 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
10615 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
10616
37082f93 10617 nft_trans_destroy(trans);
b380e5c7 10618 break;
958bee14 10619 case NFT_MSG_NEWSET:
123b9961
PNA
10620 if (nft_trans_set_update(trans)) {
10621 nft_trans_destroy(trans);
10622 break;
10623 }
1689f259 10624 nft_use_dec_restore(&trans->ctx.table->use);
6a0a8d10 10625 if (nft_trans_set_bound(trans)) {
40ba1d9b
PNA
10626 nft_trans_destroy(trans);
10627 break;
10628 }
08e4c8c5 10629 nft_trans_set(trans)->dead = 1;
40ba1d9b 10630 list_del_rcu(&nft_trans_set(trans)->list);
958bee14
PNA
10631 break;
10632 case NFT_MSG_DELSET:
f80a612d 10633 case NFT_MSG_DESTROYSET:
1689f259 10634 nft_use_inc_restore(&trans->ctx.table->use);
37a9cc52 10635 nft_clear(trans->ctx.net, nft_trans_set(trans));
628bd3e4
PNA
10636 if (nft_trans_set(trans)->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
10637 nft_map_activate(&trans->ctx, nft_trans_set(trans));
10638
958bee14
PNA
10639 nft_trans_destroy(trans);
10640 break;
60319eb1 10641 case NFT_MSG_NEWSETELEM:
6a0a8d10 10642 if (nft_trans_elem_set_bound(trans)) {
40ba1d9b
PNA
10643 nft_trans_destroy(trans);
10644 break;
10645 }
02263db0 10646 te = (struct nft_trans_elem *)trans->data;
0e1ea651
PNA
10647 nft_setelem_remove(net, te->set, te->elem_priv);
10648 if (!nft_setelem_is_catchall(te->set, te->elem_priv))
aaa31047 10649 atomic_dec(&te->set->nelems);
212ed75d
PNA
10650
10651 if (te->set->ops->abort &&
10652 list_empty(&te->set->pending_update)) {
10653 list_add_tail(&te->set->pending_update,
10654 &set_update_list);
10655 }
60319eb1
PNA
10656 break;
10657 case NFT_MSG_DELSETELEM:
f80a612d 10658 case NFT_MSG_DESTROYSETELEM:
cc02e457
PM
10659 te = (struct nft_trans_elem *)trans->data;
10660
86a1471d
PNA
10661 if (!nft_setelem_active_next(net, te->set, te->elem_priv)) {
10662 nft_setelem_data_activate(net, te->set, te->elem_priv);
10663 nft_setelem_activate(net, te->set, te->elem_priv);
10664 }
0e1ea651 10665 if (!nft_setelem_is_catchall(te->set, te->elem_priv))
aaa31047 10666 te->set->ndeact--;
cc02e457 10667
212ed75d
PNA
10668 if (te->set->ops->abort &&
10669 list_empty(&te->set->pending_update)) {
10670 list_add_tail(&te->set->pending_update,
10671 &set_update_list);
10672 }
e5009240
PNA
10673 nft_trans_destroy(trans);
10674 break;
10675 case NFT_MSG_NEWOBJ:
d62d0ba9 10676 if (nft_trans_obj_update(trans)) {
dad3bdee 10677 nft_obj_destroy(&trans->ctx, nft_trans_obj_newobj(trans));
d62d0ba9
FFM
10678 nft_trans_destroy(trans);
10679 } else {
1689f259 10680 nft_use_dec_restore(&trans->ctx.table->use);
d62d0ba9
FFM
10681 nft_obj_del(nft_trans_obj(trans));
10682 }
e5009240
PNA
10683 break;
10684 case NFT_MSG_DELOBJ:
f80a612d 10685 case NFT_MSG_DESTROYOBJ:
1689f259 10686 nft_use_inc_restore(&trans->ctx.table->use);
e5009240 10687 nft_clear(trans->ctx.net, nft_trans_obj(trans));
60319eb1
PNA
10688 nft_trans_destroy(trans);
10689 break;
3b49e2e9 10690 case NFT_MSG_NEWFLOWTABLE:
78d9f48f
PNA
10691 if (nft_trans_flowtable_update(trans)) {
10692 nft_unregister_flowtable_net_hooks(net,
10693 &nft_trans_flowtable_hooks(trans));
10694 } else {
1689f259 10695 nft_use_dec_restore(&trans->ctx.table->use);
78d9f48f
PNA
10696 list_del_rcu(&nft_trans_flowtable(trans)->list);
10697 nft_unregister_flowtable_net_hooks(net,
10698 &nft_trans_flowtable(trans)->hook_list);
10699 }
3b49e2e9
PNA
10700 break;
10701 case NFT_MSG_DELFLOWTABLE:
f80a612d 10702 case NFT_MSG_DESTROYFLOWTABLE:
abadb2f8 10703 if (nft_trans_flowtable_update(trans)) {
b6d9014a
PNA
10704 list_splice(&nft_trans_flowtable_hooks(trans),
10705 &nft_trans_flowtable(trans)->hook_list);
abadb2f8 10706 } else {
1689f259 10707 nft_use_inc_restore(&trans->ctx.table->use);
abadb2f8
PNA
10708 nft_clear(trans->ctx.net, nft_trans_flowtable(trans));
10709 }
3b49e2e9
PNA
10710 nft_trans_destroy(trans);
10711 break;
37082f93 10712 }
37082f93
PNA
10713 }
10714
212ed75d
PNA
10715 nft_set_abort_update(&set_update_list);
10716
b326dd37
PNA
10717 synchronize_rcu();
10718
a1cee076 10719 list_for_each_entry_safe_reverse(trans, next,
0854db2a 10720 &nft_net->commit_list, list) {
938154b9 10721 nft_trans_list_del(trans);
b326dd37 10722 nf_tables_abort_release(trans);
37082f93
PNA
10723 }
10724
a45e6889 10725 return err;
37082f93
PNA
10726}
10727
c0391b6a
PNA
10728static int nf_tables_abort(struct net *net, struct sk_buff *skb,
10729 enum nfnl_abort_action action)
71ad00c5 10730{
d59d2f82 10731 struct nftables_pernet *nft_net = nft_pernet(net);
72034434
PNA
10732 unsigned int gc_seq;
10733 int ret;
f102d66b 10734
72034434
PNA
10735 gc_seq = nft_gc_seq_begin(nft_net);
10736 ret = __nf_tables_abort(net, action);
10737 nft_gc_seq_end(nft_net, gc_seq);
a45e6889
PNA
10738
10739 WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
10740
0d459e2f
PNA
10741 /* module autoload needs to happen after GC sequence update because it
10742 * temporarily releases and grabs mutex again.
10743 */
10744 if (action == NFNL_ABORT_AUTOLOAD)
10745 nf_tables_module_autoload(net);
10746 else
10747 nf_tables_module_autoload_cleanup(net);
10748
0854db2a 10749 mutex_unlock(&nft_net->commit_mutex);
f102d66b
FW
10750
10751 return ret;
71ad00c5
FW
10752}
10753
74e8bcd2
PNA
10754static bool nf_tables_valid_genid(struct net *net, u32 genid)
10755{
d59d2f82 10756 struct nftables_pernet *nft_net = nft_pernet(net);
f102d66b
FW
10757 bool genid_ok;
10758
0854db2a 10759 mutex_lock(&nft_net->commit_mutex);
7395dfac 10760 nft_net->tstamp = get_jiffies_64();
f102d66b 10761
0854db2a 10762 genid_ok = genid == 0 || nft_net->base_seq == genid;
f102d66b 10763 if (!genid_ok)
0854db2a 10764 mutex_unlock(&nft_net->commit_mutex);
f102d66b
FW
10765
10766 /* else, commit mutex has to be released by commit or abort function */
10767 return genid_ok;
74e8bcd2
PNA
10768}
10769
96518518
PM
10770static const struct nfnetlink_subsystem nf_tables_subsys = {
10771 .name = "nf_tables",
10772 .subsys_id = NFNL_SUBSYS_NFTABLES,
10773 .cb_count = NFT_MSG_MAX,
10774 .cb = nf_tables_cb,
0628b123
PNA
10775 .commit = nf_tables_commit,
10776 .abort = nf_tables_abort,
74e8bcd2 10777 .valid_genid = nf_tables_valid_genid,
be2ab5b4 10778 .owner = THIS_MODULE,
96518518
PM
10779};
10780
7210e4e3 10781int nft_chain_validate_dependency(const struct nft_chain *chain,
32537e91 10782 enum nft_chain_types type)
7210e4e3
PNA
10783{
10784 const struct nft_base_chain *basechain;
10785
f323d954 10786 if (nft_is_base_chain(chain)) {
7210e4e3
PNA
10787 basechain = nft_base_chain(chain);
10788 if (basechain->type->type != type)
10789 return -EOPNOTSUPP;
10790 }
10791 return 0;
10792}
10793EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
10794
75e8d06d
PNA
10795int nft_chain_validate_hooks(const struct nft_chain *chain,
10796 unsigned int hook_flags)
10797{
10798 struct nft_base_chain *basechain;
10799
f323d954 10800 if (nft_is_base_chain(chain)) {
75e8d06d
PNA
10801 basechain = nft_base_chain(chain);
10802
c974a3a3 10803 if ((1 << basechain->ops.hooknum) & hook_flags)
75e8d06d
PNA
10804 return 0;
10805
10806 return -EOPNOTSUPP;
10807 }
10808
10809 return 0;
10810}
10811EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
10812
20a69341
PM
10813/*
10814 * Loop detection - walk through the ruleset beginning at the destination chain
10815 * of a new jump until either the source chain is reached (loop) or all
10816 * reachable chains have been traversed.
10817 *
10818 * The loop check is performed whenever a new jump verdict is added to an
10819 * expression or verdict map or a verdict map is bound to a new chain.
10820 */
10821
10822static int nf_tables_check_loops(const struct nft_ctx *ctx,
10823 const struct nft_chain *chain);
10824
6387aa6e
PNA
10825static int nft_check_loops(const struct nft_ctx *ctx,
10826 const struct nft_set_ext *ext)
10827{
10828 const struct nft_data *data;
10829 int ret;
10830
10831 data = nft_set_ext_data(ext);
10832 switch (data->verdict.code) {
10833 case NFT_JUMP:
10834 case NFT_GOTO:
10835 ret = nf_tables_check_loops(ctx, data->verdict.chain);
10836 break;
10837 default:
10838 ret = 0;
10839 break;
10840 }
10841
10842 return ret;
10843}
10844
20a69341 10845static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
de70185d 10846 struct nft_set *set,
20a69341 10847 const struct nft_set_iter *iter,
0e1ea651 10848 struct nft_elem_priv *elem_priv)
20a69341 10849{
0e1ea651 10850 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
fe2811eb 10851
e79b47a8
PNA
10852 if (!nft_set_elem_active(ext, iter->genmask))
10853 return 0;
10854
fe2811eb
PM
10855 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
10856 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
62f9c8b4
PNA
10857 return 0;
10858
6387aa6e 10859 return nft_check_loops(ctx, ext);
20a69341
PM
10860}
10861
aaa31047
PNA
10862static int nft_set_catchall_loops(const struct nft_ctx *ctx,
10863 struct nft_set *set)
10864{
10865 u8 genmask = nft_genmask_next(ctx->net);
10866 struct nft_set_elem_catchall *catchall;
10867 struct nft_set_ext *ext;
10868 int ret = 0;
10869
10870 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
10871 ext = nft_set_elem_ext(set, catchall->elem);
10872 if (!nft_set_elem_active(ext, genmask))
10873 continue;
10874
10875 ret = nft_check_loops(ctx, ext);
10876 if (ret < 0)
10877 return ret;
10878 }
10879
10880 return ret;
10881}
10882
20a69341
PM
10883static int nf_tables_check_loops(const struct nft_ctx *ctx,
10884 const struct nft_chain *chain)
10885{
10886 const struct nft_rule *rule;
10887 const struct nft_expr *expr, *last;
de70185d 10888 struct nft_set *set;
20a69341
PM
10889 struct nft_set_binding *binding;
10890 struct nft_set_iter iter;
20a69341
PM
10891
10892 if (ctx->chain == chain)
10893 return -ELOOP;
10894
169384fb
FW
10895 if (fatal_signal_pending(current))
10896 return -EINTR;
10897
20a69341
PM
10898 list_for_each_entry(rule, &chain->rules, list) {
10899 nft_rule_for_each_expr(expr, last, rule) {
a654de8f
PNA
10900 struct nft_immediate_expr *priv;
10901 const struct nft_data *data;
0ca743a5
PNA
10902 int err;
10903
a654de8f 10904 if (strcmp(expr->ops->type->name, "immediate"))
20a69341
PM
10905 continue;
10906
a654de8f
PNA
10907 priv = nft_expr_priv(expr);
10908 if (priv->dreg != NFT_REG_VERDICT)
0ca743a5 10909 continue;
20a69341 10910
a654de8f 10911 data = &priv->data;
1ca2e170 10912 switch (data->verdict.code) {
20a69341
PM
10913 case NFT_JUMP:
10914 case NFT_GOTO:
1ca2e170
PM
10915 err = nf_tables_check_loops(ctx,
10916 data->verdict.chain);
20a69341
PM
10917 if (err < 0)
10918 return err;
c2168e6b 10919 break;
20a69341
PM
10920 default:
10921 break;
10922 }
10923 }
10924 }
10925
10926 list_for_each_entry(set, &ctx->table->sets, list) {
37a9cc52
PNA
10927 if (!nft_is_active_next(ctx->net, set))
10928 continue;
20a69341
PM
10929 if (!(set->flags & NFT_SET_MAP) ||
10930 set->dtype != NFT_DATA_VERDICT)
10931 continue;
10932
10933 list_for_each_entry(binding, &set->bindings, list) {
11113e19
PM
10934 if (!(binding->flags & NFT_SET_MAP) ||
10935 binding->chain != chain)
20a69341
PM
10936 continue;
10937
8588ac09 10938 iter.genmask = nft_genmask_next(ctx->net);
29b359cf 10939 iter.type = NFT_ITER_UPDATE;
20a69341
PM
10940 iter.skip = 0;
10941 iter.count = 0;
10942 iter.err = 0;
10943 iter.fn = nf_tables_loop_check_setelem;
10944
10945 set->ops->walk(ctx, set, &iter);
aaa31047
PNA
10946 if (!iter.err)
10947 iter.err = nft_set_catchall_loops(ctx, set);
10948
20a69341
PM
10949 if (iter.err < 0)
10950 return iter.err;
10951 }
10952 }
10953
10954 return 0;
10955}
10956
36b701fa
LGL
10957/**
10958 * nft_parse_u32_check - fetch u32 attribute and check for maximum value
10959 *
10960 * @attr: netlink attribute to fetch value from
10961 * @max: maximum value to be stored in dest
10962 * @dest: pointer to the variable
10963 *
10964 * Parse, check and store a given u32 netlink attribute into variable.
10965 * This function returns -ERANGE if the value goes over maximum value.
10966 * Otherwise a 0 is returned and the attribute value is stored in the
10967 * destination variable.
10968 */
f1d505bb 10969int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest)
36b701fa 10970{
09525a09 10971 u32 val;
36b701fa
LGL
10972
10973 val = ntohl(nla_get_be32(attr));
10974 if (val > max)
10975 return -ERANGE;
10976
10977 *dest = val;
10978 return 0;
10979}
10980EXPORT_SYMBOL_GPL(nft_parse_u32_check);
10981
6c6f9f31 10982static int nft_parse_register(const struct nlattr *attr, u32 *preg)
b1c96ed3 10983{
49499c3e
PM
10984 unsigned int reg;
10985
10986 reg = ntohl(nla_get_be32(attr));
10987 switch (reg) {
10988 case NFT_REG_VERDICT...NFT_REG_4:
6e1acfa3
PNA
10989 *preg = reg * NFT_REG_SIZE / NFT_REG32_SIZE;
10990 break;
10991 case NFT_REG32_00...NFT_REG32_15:
10992 *preg = reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
10993 break;
49499c3e 10994 default:
6e1acfa3 10995 return -ERANGE;
49499c3e 10996 }
6e1acfa3
PNA
10997
10998 return 0;
b1c96ed3 10999}
b1c96ed3 11000
49499c3e
PM
11001/**
11002 * nft_dump_register - dump a register value to a netlink attribute
11003 *
11004 * @skb: socket buffer
11005 * @attr: attribute number
11006 * @reg: register number
11007 *
11008 * Construct a netlink attribute containing the register number. For
11009 * compatibility reasons, register numbers being a multiple of 4 are
11010 * translated to the corresponding 128 bit register numbers.
11011 */
b1c96ed3
PM
11012int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
11013{
49499c3e
PM
11014 if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
11015 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
11016 else
11017 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
11018
b1c96ed3
PM
11019 return nla_put_be32(skb, attr, htonl(reg));
11020}
11021EXPORT_SYMBOL_GPL(nft_dump_register);
11022
4f16d25c 11023static int nft_validate_register_load(enum nft_registers reg, unsigned int len)
96518518 11024{
49499c3e 11025 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
96518518 11026 return -EINVAL;
d07db988
PM
11027 if (len == 0)
11028 return -EINVAL;
c593642c 11029 if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data))
d07db988 11030 return -ERANGE;
49499c3e 11031
96518518
PM
11032 return 0;
11033}
4f16d25c
PNA
11034
11035int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len)
11036{
11037 u32 reg;
11038 int err;
11039
6e1acfa3
PNA
11040 err = nft_parse_register(attr, &reg);
11041 if (err < 0)
11042 return err;
11043
4f16d25c
PNA
11044 err = nft_validate_register_load(reg, len);
11045 if (err < 0)
11046 return err;
11047
11048 *sreg = reg;
11049 return 0;
11050}
11051EXPORT_SYMBOL_GPL(nft_parse_register_load);
96518518 11052
345023b0
PNA
11053static int nft_validate_register_store(const struct nft_ctx *ctx,
11054 enum nft_registers reg,
11055 const struct nft_data *data,
11056 enum nft_data_types type,
11057 unsigned int len)
96518518 11058{
20a69341
PM
11059 int err;
11060
96518518
PM
11061 switch (reg) {
11062 case NFT_REG_VERDICT:
58f40ab6 11063 if (type != NFT_DATA_VERDICT)
96518518 11064 return -EINVAL;
20a69341 11065
58f40ab6 11066 if (data != NULL &&
1ca2e170
PM
11067 (data->verdict.code == NFT_GOTO ||
11068 data->verdict.code == NFT_JUMP)) {
11069 err = nf_tables_check_loops(ctx, data->verdict.chain);
20a69341
PM
11070 if (err < 0)
11071 return err;
20a69341
PM
11072 }
11073
96518518
PM
11074 return 0;
11075 default:
49499c3e 11076 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
27e6d201 11077 return -EINVAL;
45d9bcda
PM
11078 if (len == 0)
11079 return -EINVAL;
49499c3e 11080 if (reg * NFT_REG32_SIZE + len >
c593642c 11081 sizeof_field(struct nft_regs, data))
45d9bcda 11082 return -ERANGE;
27e6d201 11083
96518518
PM
11084 if (data != NULL && type != NFT_DATA_VALUE)
11085 return -EINVAL;
11086 return 0;
11087 }
11088}
345023b0
PNA
11089
11090int nft_parse_register_store(const struct nft_ctx *ctx,
11091 const struct nlattr *attr, u8 *dreg,
11092 const struct nft_data *data,
11093 enum nft_data_types type, unsigned int len)
11094{
11095 int err;
11096 u32 reg;
11097
6e1acfa3
PNA
11098 err = nft_parse_register(attr, &reg);
11099 if (err < 0)
11100 return err;
11101
345023b0
PNA
11102 err = nft_validate_register_store(ctx, reg, data, type, len);
11103 if (err < 0)
11104 return err;
11105
11106 *dreg = reg;
11107 return 0;
11108}
11109EXPORT_SYMBOL_GPL(nft_parse_register_store);
96518518
PM
11110
11111static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
11112 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
11113 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
11114 .len = NFT_CHAIN_MAXNAMELEN - 1 },
51d70f18 11115 [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 },
96518518
PM
11116};
11117
11118static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
11119 struct nft_data_desc *desc, const struct nlattr *nla)
11120{
664b0f8c 11121 u8 genmask = nft_genmask_next(ctx->net);
96518518
PM
11122 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
11123 struct nft_chain *chain;
11124 int err;
11125
8cb08174
JB
11126 err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla,
11127 nft_verdict_policy, NULL);
96518518
PM
11128 if (err < 0)
11129 return err;
11130
11131 if (!tb[NFTA_VERDICT_CODE])
11132 return -EINVAL;
ddbd8be6
FW
11133
11134 /* zero padding hole for memcmp */
11135 memset(data, 0, sizeof(*data));
1ca2e170 11136 data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
96518518 11137
1ca2e170 11138 switch (data->verdict.code) {
f342de4e
FW
11139 case NF_ACCEPT:
11140 case NF_DROP:
11141 case NF_QUEUE:
11142 break;
96518518
PM
11143 case NFT_CONTINUE:
11144 case NFT_BREAK:
11145 case NFT_RETURN:
96518518
PM
11146 break;
11147 case NFT_JUMP:
11148 case NFT_GOTO:
51d70f18
PNA
11149 if (tb[NFTA_VERDICT_CHAIN]) {
11150 chain = nft_chain_lookup(ctx->net, ctx->table,
11151 tb[NFTA_VERDICT_CHAIN],
11152 genmask);
11153 } else if (tb[NFTA_VERDICT_CHAIN_ID]) {
95f466d2 11154 chain = nft_chain_lookup_byid(ctx->net, ctx->table,
515ad530
TLSC
11155 tb[NFTA_VERDICT_CHAIN_ID],
11156 genmask);
51d70f18
PNA
11157 if (IS_ERR(chain))
11158 return PTR_ERR(chain);
11159 } else {
96518518 11160 return -EINVAL;
51d70f18
PNA
11161 }
11162
96518518
PM
11163 if (IS_ERR(chain))
11164 return PTR_ERR(chain);
f323d954 11165 if (nft_is_base_chain(chain))
96518518 11166 return -EOPNOTSUPP;
e02f0d39
PNA
11167 if (nft_chain_is_bound(chain))
11168 return -EINVAL;
f323ef3a
PNA
11169 if (desc->flags & NFT_DATA_DESC_SETELEM &&
11170 chain->flags & NFT_CHAIN_BINDING)
11171 return -EINVAL;
1689f259
PNA
11172 if (!nft_use_inc(&chain->use))
11173 return -EMFILE;
96518518 11174
1ca2e170 11175 data->verdict.chain = chain;
96518518 11176 break;
f342de4e
FW
11177 default:
11178 return -EINVAL;
96518518
PM
11179 }
11180
4c4ed074 11181 desc->len = sizeof(data->verdict);
341b6941 11182
96518518
PM
11183 return 0;
11184}
11185
11186static void nft_verdict_uninit(const struct nft_data *data)
11187{
d0e2c7de 11188 struct nft_chain *chain;
d0e2c7de 11189
1ca2e170 11190 switch (data->verdict.code) {
96518518
PM
11191 case NFT_JUMP:
11192 case NFT_GOTO:
d0e2c7de 11193 chain = data->verdict.chain;
1689f259 11194 nft_use_dec(&chain->use);
96518518
PM
11195 break;
11196 }
11197}
11198
33d5a7b1 11199int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
96518518
PM
11200{
11201 struct nlattr *nest;
11202
ae0be8de 11203 nest = nla_nest_start_noflag(skb, type);
96518518
PM
11204 if (!nest)
11205 goto nla_put_failure;
11206
33d5a7b1 11207 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
96518518
PM
11208 goto nla_put_failure;
11209
33d5a7b1 11210 switch (v->code) {
96518518
PM
11211 case NFT_JUMP:
11212 case NFT_GOTO:
1ca2e170 11213 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
33d5a7b1 11214 v->chain->name))
96518518
PM
11215 goto nla_put_failure;
11216 }
11217 nla_nest_end(skb, nest);
11218 return 0;
11219
11220nla_put_failure:
11221 return -1;
11222}
11223
d0a11fc3 11224static int nft_value_init(const struct nft_ctx *ctx,
341b6941
PNA
11225 struct nft_data *data, struct nft_data_desc *desc,
11226 const struct nlattr *nla)
96518518
PM
11227{
11228 unsigned int len;
11229
11230 len = nla_len(nla);
11231 if (len == 0)
11232 return -EINVAL;
341b6941 11233 if (len > desc->size)
96518518 11234 return -EOVERFLOW;
341b6941
PNA
11235 if (desc->len) {
11236 if (len != desc->len)
11237 return -EINVAL;
11238 } else {
11239 desc->len = len;
11240 }
96518518 11241
d0a11fc3 11242 nla_memcpy(data->data, nla, len);
341b6941 11243
96518518
PM
11244 return 0;
11245}
11246
11247static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
11248 unsigned int len)
11249{
11250 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
11251}
11252
11253static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
d0a11fc3 11254 [NFTA_DATA_VALUE] = { .type = NLA_BINARY },
96518518
PM
11255 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
11256};
11257
11258/**
11259 * nft_data_init - parse nf_tables data netlink attributes
11260 *
11261 * @ctx: context of the expression using the data
11262 * @data: destination struct nft_data
11263 * @desc: data description
11264 * @nla: netlink attribute containing data
11265 *
11266 * Parse the netlink data attributes and initialize a struct nft_data.
11267 * The type and length of data are returned in the data description.
11268 *
11269 * The caller can indicate that it only wants to accept data of type
11270 * NFT_DATA_VALUE by passing NULL for the ctx argument.
11271 */
341b6941 11272int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
96518518
PM
11273 struct nft_data_desc *desc, const struct nlattr *nla)
11274{
11275 struct nlattr *tb[NFTA_DATA_MAX + 1];
11276 int err;
11277
341b6941
PNA
11278 if (WARN_ON_ONCE(!desc->size))
11279 return -EINVAL;
11280
8cb08174
JB
11281 err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla,
11282 nft_data_policy, NULL);
96518518
PM
11283 if (err < 0)
11284 return err;
11285
341b6941
PNA
11286 if (tb[NFTA_DATA_VALUE]) {
11287 if (desc->type != NFT_DATA_VALUE)
11288 return -EINVAL;
11289
11290 err = nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
11291 } else if (tb[NFTA_DATA_VERDICT] && ctx != NULL) {
11292 if (desc->type != NFT_DATA_VERDICT)
11293 return -EINVAL;
11294
11295 err = nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
11296 } else {
11297 err = -EINVAL;
11298 }
11299
11300 return err;
96518518
PM
11301}
11302EXPORT_SYMBOL_GPL(nft_data_init);
11303
11304/**
59105446 11305 * nft_data_release - release a nft_data item
96518518
PM
11306 *
11307 * @data: struct nft_data to release
11308 * @type: type of data
11309 *
11310 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
11311 * all others need to be released by calling this function.
11312 */
59105446 11313void nft_data_release(const struct nft_data *data, enum nft_data_types type)
96518518 11314{
960bd2c2 11315 if (type < NFT_DATA_VERDICT)
96518518 11316 return;
960bd2c2 11317 switch (type) {
96518518
PM
11318 case NFT_DATA_VERDICT:
11319 return nft_verdict_uninit(data);
11320 default:
11321 WARN_ON(1);
11322 }
11323}
59105446 11324EXPORT_SYMBOL_GPL(nft_data_release);
96518518
PM
11325
11326int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
11327 enum nft_data_types type, unsigned int len)
11328{
11329 struct nlattr *nest;
11330 int err;
11331
ae0be8de 11332 nest = nla_nest_start_noflag(skb, attr);
96518518
PM
11333 if (nest == NULL)
11334 return -1;
11335
11336 switch (type) {
11337 case NFT_DATA_VALUE:
11338 err = nft_value_dump(skb, data, len);
11339 break;
11340 case NFT_DATA_VERDICT:
33d5a7b1 11341 err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
96518518
PM
11342 break;
11343 default:
11344 err = -EINVAL;
11345 WARN_ON(1);
11346 }
11347
11348 nla_nest_end(skb, nest);
11349 return err;
11350}
11351EXPORT_SYMBOL_GPL(nft_data_dump);
11352
5ebe0b0e
PNA
11353int __nft_release_basechain(struct nft_ctx *ctx)
11354{
11355 struct nft_rule *rule, *nr;
11356
fa5950e4
FW
11357 if (WARN_ON(!nft_is_base_chain(ctx->chain)))
11358 return 0;
5ebe0b0e 11359
c974a3a3 11360 nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain);
5ebe0b0e
PNA
11361 list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
11362 list_del(&rule->list);
1689f259 11363 nft_use_dec(&ctx->chain->use);
bb7b40ae 11364 nf_tables_rule_release(ctx, rule);
5ebe0b0e 11365 }
1b2470e5 11366 nft_chain_del(ctx->chain);
1689f259 11367 nft_use_dec(&ctx->table->use);
43a605f2 11368 nf_tables_chain_destroy(ctx);
5ebe0b0e
PNA
11369
11370 return 0;
11371}
11372EXPORT_SYMBOL_GPL(__nft_release_basechain);
11373
00dfe9be
PNA
11374static void __nft_release_hook(struct net *net, struct nft_table *table)
11375{
6069da44 11376 struct nft_flowtable *flowtable;
00dfe9be
PNA
11377 struct nft_chain *chain;
11378
11379 list_for_each_entry(chain, &table->chains, list)
f9a43007 11380 __nf_tables_unregister_hook(net, table, chain, true);
6069da44 11381 list_for_each_entry(flowtable, &table->flowtables, list)
f9a43007
PNA
11382 __nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list,
11383 true);
00dfe9be
PNA
11384}
11385
767d1216
PNA
11386static void __nft_release_hooks(struct net *net)
11387{
d59d2f82 11388 struct nftables_pernet *nft_net = nft_pernet(net);
767d1216 11389 struct nft_table *table;
767d1216 11390
0854db2a 11391 list_for_each_entry(table, &nft_net->tables, list) {
2888b080
PNA
11392 if (nft_table_has_owner(table))
11393 continue;
11394
00dfe9be 11395 __nft_release_hook(net, table);
2888b080 11396 }
767d1216
PNA
11397}
11398
fd020332 11399static void __nft_release_table(struct net *net, struct nft_table *table)
df05ef87 11400{
3b49e2e9 11401 struct nft_flowtable *flowtable, *nf;
df05ef87 11402 struct nft_chain *chain, *nc;
e5009240 11403 struct nft_object *obj, *ne;
df05ef87
PNA
11404 struct nft_rule *rule, *nr;
11405 struct nft_set *set, *ns;
11406 struct nft_ctx ctx = {
11407 .net = net,
43a605f2 11408 .family = NFPROTO_NETDEV,
df05ef87
PNA
11409 };
11410
fd020332
PNA
11411 ctx.family = table->family;
11412 ctx.table = table;
11413 list_for_each_entry(chain, &table->chains, list) {
f15f29fd 11414 if (nft_chain_binding(chain))
751d460c
PNA
11415 continue;
11416
fd020332
PNA
11417 ctx.chain = chain;
11418 list_for_each_entry_safe(rule, nr, &chain->rules, list) {
11419 list_del(&rule->list);
1689f259 11420 nft_use_dec(&chain->use);
fd020332 11421 nf_tables_rule_release(&ctx, rule);
df05ef87 11422 }
df05ef87 11423 }
fd020332
PNA
11424 list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) {
11425 list_del(&flowtable->list);
1689f259 11426 nft_use_dec(&table->use);
fd020332
PNA
11427 nf_tables_flowtable_destroy(flowtable);
11428 }
11429 list_for_each_entry_safe(set, ns, &table->sets, list) {
11430 list_del(&set->list);
1689f259 11431 nft_use_dec(&table->use);
628bd3e4
PNA
11432 if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
11433 nft_map_deactivate(&ctx, set);
11434
fd020332
PNA
11435 nft_set_destroy(&ctx, set);
11436 }
11437 list_for_each_entry_safe(obj, ne, &table->objects, list) {
11438 nft_obj_del(obj);
1689f259 11439 nft_use_dec(&table->use);
fd020332
PNA
11440 nft_obj_destroy(&ctx, obj);
11441 }
11442 list_for_each_entry_safe(chain, nc, &table->chains, list) {
11443 ctx.chain = chain;
11444 nft_chain_del(chain);
1689f259 11445 nft_use_dec(&table->use);
fd020332
PNA
11446 nf_tables_chain_destroy(&ctx);
11447 }
fd020332
PNA
11448 nf_tables_table_destroy(&ctx);
11449}
11450
bd1777b3 11451static void __nft_release_tables(struct net *net)
fd020332 11452{
d59d2f82 11453 struct nftables_pernet *nft_net = nft_pernet(net);
fd020332
PNA
11454 struct nft_table *table, *nt;
11455
0854db2a 11456 list_for_each_entry_safe(table, nt, &nft_net->tables, list) {
bd1777b3 11457 if (nft_table_has_owner(table))
6001a930
PNA
11458 continue;
11459
a499b03b
FW
11460 list_del(&table->list);
11461
fd020332 11462 __nft_release_table(net, table);
6001a930
PNA
11463 }
11464}
11465
11466static int nft_rcv_nl_event(struct notifier_block *this, unsigned long event,
11467 void *ptr)
11468{
a499b03b 11469 struct nft_table *table, *to_delete[8];
0854db2a 11470 struct nftables_pernet *nft_net;
6001a930 11471 struct netlink_notify *n = ptr;
6001a930 11472 struct net *net = n->net;
a499b03b
FW
11473 unsigned int deleted;
11474 bool restart = false;
6a33d8b7 11475 unsigned int gc_seq;
6001a930
PNA
11476
11477 if (event != NETLINK_URELEASE || n->protocol != NETLINK_NETFILTER)
11478 return NOTIFY_DONE;
11479
d59d2f82 11480 nft_net = nft_pernet(net);
a499b03b 11481 deleted = 0;
0854db2a 11482 mutex_lock(&nft_net->commit_mutex);
6a33d8b7
PNA
11483
11484 gc_seq = nft_gc_seq_begin(nft_net);
11485
d4bc8271 11486 if (!list_empty(&nf_tables_destroy_list))
2c9f0293 11487 nf_tables_trans_destroy_flush_work();
a499b03b 11488again:
0854db2a 11489 list_for_each_entry(table, &nft_net->tables, list) {
6001a930
PNA
11490 if (nft_table_has_owner(table) &&
11491 n->portid == table->nlpid) {
da5141bb
PS
11492 if (table->flags & NFT_TABLE_F_PERSIST) {
11493 table->flags &= ~NFT_TABLE_F_OWNER;
11494 continue;
11495 }
6001a930 11496 __nft_release_hook(net, table);
a499b03b
FW
11497 list_del_rcu(&table->list);
11498 to_delete[deleted++] = table;
11499 if (deleted >= ARRAY_SIZE(to_delete))
11500 break;
6001a930
PNA
11501 }
11502 }
a499b03b
FW
11503 if (deleted) {
11504 restart = deleted >= ARRAY_SIZE(to_delete);
6001a930 11505 synchronize_rcu();
a499b03b
FW
11506 while (deleted)
11507 __nft_release_table(net, to_delete[--deleted]);
11508
11509 if (restart)
11510 goto again;
6001a930 11511 }
6a33d8b7
PNA
11512 nft_gc_seq_end(nft_net, gc_seq);
11513
0854db2a 11514 mutex_unlock(&nft_net->commit_mutex);
6001a930
PNA
11515
11516 return NOTIFY_DONE;
df05ef87
PNA
11517}
11518
6001a930
PNA
11519static struct notifier_block nft_nl_notifier = {
11520 .notifier_call = nft_rcv_nl_event,
11521};
11522
dd4cbef7
PNA
11523static int __net_init nf_tables_init_net(struct net *net)
11524{
d59d2f82 11525 struct nftables_pernet *nft_net = nft_pernet(net);
0854db2a
FW
11526
11527 INIT_LIST_HEAD(&nft_net->tables);
11528 INIT_LIST_HEAD(&nft_net->commit_list);
938154b9 11529 INIT_LIST_HEAD(&nft_net->binding_list);
0854db2a
FW
11530 INIT_LIST_HEAD(&nft_net->module_list);
11531 INIT_LIST_HEAD(&nft_net->notify_list);
11532 mutex_init(&nft_net->commit_mutex);
11533 nft_net->base_seq = 1;
5f68718b 11534 nft_net->gc_seq = 0;
4b80ced9 11535 nft_net->validate_state = NFT_VALIDATE_SKIP;
a654de8f 11536
dd4cbef7
PNA
11537 return 0;
11538}
11539
767d1216
PNA
11540static void __net_exit nf_tables_pre_exit_net(struct net *net)
11541{
3923b1e4
PNA
11542 struct nftables_pernet *nft_net = nft_pernet(net);
11543
11544 mutex_lock(&nft_net->commit_mutex);
767d1216 11545 __nft_release_hooks(net);
3923b1e4 11546 mutex_unlock(&nft_net->commit_mutex);
767d1216
PNA
11547}
11548
dd4cbef7
PNA
11549static void __net_exit nf_tables_exit_net(struct net *net)
11550{
d59d2f82 11551 struct nftables_pernet *nft_net = nft_pernet(net);
6a33d8b7 11552 unsigned int gc_seq;
0854db2a
FW
11553
11554 mutex_lock(&nft_net->commit_mutex);
6a33d8b7
PNA
11555
11556 gc_seq = nft_gc_seq_begin(nft_net);
11557
a45e6889
PNA
11558 WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
11559
11560 if (!list_empty(&nft_net->module_list))
11561 nf_tables_module_autoload_cleanup(net);
6a33d8b7 11562
bd1777b3 11563 __nft_release_tables(net);
6a33d8b7
PNA
11564
11565 nft_gc_seq_end(nft_net, gc_seq);
11566
0854db2a
FW
11567 mutex_unlock(&nft_net->commit_mutex);
11568 WARN_ON_ONCE(!list_empty(&nft_net->tables));
11569 WARN_ON_ONCE(!list_empty(&nft_net->module_list));
11570 WARN_ON_ONCE(!list_empty(&nft_net->notify_list));
dd4cbef7
PNA
11571}
11572
5f68718b
PNA
11573static void nf_tables_exit_batch(struct list_head *net_exit_list)
11574{
11575 flush_work(&trans_gc_work);
11576}
11577
99633ab2 11578static struct pernet_operations nf_tables_net_ops = {
767d1216
PNA
11579 .init = nf_tables_init_net,
11580 .pre_exit = nf_tables_pre_exit_net,
11581 .exit = nf_tables_exit_net,
5f68718b 11582 .exit_batch = nf_tables_exit_batch,
0854db2a
FW
11583 .id = &nf_tables_net_id,
11584 .size = sizeof(struct nftables_pernet),
99633ab2
PNA
11585};
11586
96518518
PM
11587static int __init nf_tables_module_init(void)
11588{
11589 int err;
11590
d209df3e
FW
11591 err = register_pernet_subsys(&nf_tables_net_ops);
11592 if (err < 0)
11593 return err;
11594
11595 err = nft_chain_filter_init();
11596 if (err < 0)
6001a930 11597 goto err_chain_filter;
02c7b25e 11598
96518518
PM
11599 err = nf_tables_core_module_init();
11600 if (err < 0)
6001a930 11601 goto err_core_module;
96518518 11602
d209df3e 11603 err = register_netdevice_notifier(&nf_tables_flowtable_notifier);
96518518 11604 if (err < 0)
6001a930 11605 goto err_netdev_notifier;
96518518 11606
4d44175a
FW
11607 err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params);
11608 if (err < 0)
6001a930 11609 goto err_rht_objname;
4d44175a 11610
06d392cb 11611 err = nft_offload_init();
11612 if (err < 0)
6001a930
PNA
11613 goto err_offload;
11614
11615 err = netlink_register_notifier(&nft_nl_notifier);
11616 if (err < 0)
11617 goto err_netlink_notifier;
06d392cb 11618
d209df3e
FW
11619 /* must be last */
11620 err = nfnetlink_subsys_register(&nf_tables_subsys);
11621 if (err < 0)
6001a930 11622 goto err_nfnl_subsys;
3b49e2e9 11623
c1deb065 11624 nft_chain_route_init();
3474a2c6 11625
d209df3e 11626 return err;
6001a930
PNA
11627
11628err_nfnl_subsys:
11629 netlink_unregister_notifier(&nft_nl_notifier);
11630err_netlink_notifier:
06d392cb 11631 nft_offload_exit();
6001a930 11632err_offload:
4d44175a 11633 rhltable_destroy(&nft_objname_ht);
6001a930 11634err_rht_objname:
d209df3e 11635 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
6001a930 11636err_netdev_notifier:
96518518 11637 nf_tables_core_module_exit();
6001a930 11638err_core_module:
d209df3e 11639 nft_chain_filter_fini();
6001a930 11640err_chain_filter:
d209df3e 11641 unregister_pernet_subsys(&nf_tables_net_ops);
96518518
PM
11642 return err;
11643}
11644
11645static void __exit nf_tables_module_exit(void)
11646{
11647 nfnetlink_subsys_unregister(&nf_tables_subsys);
6001a930 11648 netlink_unregister_notifier(&nft_nl_notifier);
06d392cb 11649 nft_offload_exit();
3b49e2e9 11650 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
0a2cf5ee 11651 nft_chain_filter_fini();
c1deb065 11652 nft_chain_route_fini();
24cea967 11653 nf_tables_trans_destroy_flush_work();
71ad00c5 11654 unregister_pernet_subsys(&nf_tables_net_ops);
5f68718b 11655 cancel_work_sync(&trans_gc_work);
0935d558 11656 cancel_work_sync(&trans_destroy_work);
1b1bc49c 11657 rcu_barrier();
4d44175a 11658 rhltable_destroy(&nft_objname_ht);
96518518 11659 nf_tables_core_module_exit();
96518518
PM
11660}
11661
11662module_init(nf_tables_module_init);
11663module_exit(nf_tables_module_exit);
11664
11665MODULE_LICENSE("GPL");
11666MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
94090b23 11667MODULE_DESCRIPTION("Framework for packet filtering and classification");
96518518 11668MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);