]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/netfilter/nf_tables_api.c
netfilter: nftables: add nft_parse_register_load() and use it
[mirror_ubuntu-jammy-kernel.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
FW
26#define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-"))
27
96518518 28static LIST_HEAD(nf_tables_expressions);
e5009240 29static LIST_HEAD(nf_tables_objects);
3b49e2e9 30static LIST_HEAD(nf_tables_flowtables);
0935d558
FW
31static LIST_HEAD(nf_tables_destroy_list);
32static DEFINE_SPINLOCK(nf_tables_destroy_list_lock);
3ecbfd65 33static u64 table_handle;
96518518 34
a654de8f
PNA
35enum {
36 NFT_VALIDATE_SKIP = 0,
37 NFT_VALIDATE_NEED,
38 NFT_VALIDATE_DO,
39};
40
4d44175a
FW
41static struct rhltable nft_objname_ht;
42
1b2470e5
FW
43static u32 nft_chain_hash(const void *data, u32 len, u32 seed);
44static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed);
45static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *);
46
4d44175a
FW
47static u32 nft_objname_hash(const void *data, u32 len, u32 seed);
48static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed);
49static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *);
50
1b2470e5
FW
51static const struct rhashtable_params nft_chain_ht_params = {
52 .head_offset = offsetof(struct nft_chain, rhlhead),
53 .key_offset = offsetof(struct nft_chain, name),
54 .hashfn = nft_chain_hash,
55 .obj_hashfn = nft_chain_hash_obj,
56 .obj_cmpfn = nft_chain_hash_cmp,
1b2470e5
FW
57 .automatic_shrinking = true,
58};
59
4d44175a
FW
60static const struct rhashtable_params nft_objname_ht_params = {
61 .head_offset = offsetof(struct nft_object, rhlhead),
62 .key_offset = offsetof(struct nft_object, key),
63 .hashfn = nft_objname_hash,
64 .obj_hashfn = nft_objname_hash_obj,
65 .obj_cmpfn = nft_objname_hash_cmp,
66 .automatic_shrinking = true,
67};
68
a654de8f
PNA
69static void nft_validate_state_update(struct net *net, u8 new_validate_state)
70{
71 switch (net->nft.validate_state) {
72 case NFT_VALIDATE_SKIP:
73 WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO);
74 break;
75 case NFT_VALIDATE_NEED:
76 break;
77 case NFT_VALIDATE_DO:
78 if (new_validate_state == NFT_VALIDATE_NEED)
79 return;
80 }
81
82 net->nft.validate_state = new_validate_state;
83}
0935d558
FW
84static void nf_tables_trans_destroy_work(struct work_struct *w);
85static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work);
a654de8f 86
7c95f6d8 87static void nft_ctx_init(struct nft_ctx *ctx,
633c9a84 88 struct net *net,
7c95f6d8
PNA
89 const struct sk_buff *skb,
90 const struct nlmsghdr *nlh,
36596dad 91 u8 family,
7c95f6d8
PNA
92 struct nft_table *table,
93 struct nft_chain *chain,
94 const struct nlattr * const *nla)
95{
633c9a84 96 ctx->net = net;
36596dad 97 ctx->family = family;
26b2f552 98 ctx->level = 0;
128ad332
PNA
99 ctx->table = table;
100 ctx->chain = chain;
101 ctx->nla = nla;
102 ctx->portid = NETLINK_CB(skb).portid;
103 ctx->report = nlmsg_report(nlh);
c9626a2c 104 ctx->flags = nlh->nlmsg_flags;
128ad332 105 ctx->seq = nlh->nlmsg_seq;
7c95f6d8
PNA
106}
107
8411b644
PNA
108static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx,
109 int msg_type, u32 size, gfp_t gfp)
1081d11b
PNA
110{
111 struct nft_trans *trans;
112
8411b644 113 trans = kzalloc(sizeof(struct nft_trans) + size, gfp);
1081d11b
PNA
114 if (trans == NULL)
115 return NULL;
116
b380e5c7 117 trans->msg_type = msg_type;
1081d11b
PNA
118 trans->ctx = *ctx;
119
120 return trans;
121}
122
8411b644
PNA
123static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx,
124 int msg_type, u32 size)
125{
126 return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL);
127}
128
1081d11b
PNA
129static void nft_trans_destroy(struct nft_trans *trans)
130{
131 list_del(&trans->list);
132 kfree(trans);
133}
134
f6ac8585
PNA
135static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set)
136{
137 struct net *net = ctx->net;
138 struct nft_trans *trans;
139
140 if (!nft_set_is_anonymous(set))
141 return;
142
143 list_for_each_entry_reverse(trans, &net->nft.commit_list, list) {
6a0a8d10
PNA
144 switch (trans->msg_type) {
145 case NFT_MSG_NEWSET:
146 if (nft_trans_set(trans) == set)
147 nft_trans_set_bound(trans) = true;
148 break;
149 case NFT_MSG_NEWSETELEM:
150 if (nft_trans_elem_set(trans) == set)
151 nft_trans_elem_set_bound(trans) = true;
f6ac8585
PNA
152 break;
153 }
154 }
155}
156
d54725cd
PNA
157static int nft_netdev_register_hooks(struct net *net,
158 struct list_head *hook_list)
159{
160 struct nft_hook *hook;
161 int err, j;
162
163 j = 0;
164 list_for_each_entry(hook, hook_list, list) {
165 err = nf_register_net_hook(net, &hook->ops);
166 if (err < 0)
167 goto err_register;
168
169 j++;
170 }
171 return 0;
172
173err_register:
174 list_for_each_entry(hook, hook_list, list) {
175 if (j-- <= 0)
176 break;
177
178 nf_unregister_net_hook(net, &hook->ops);
179 }
180 return err;
181}
182
183static void nft_netdev_unregister_hooks(struct net *net,
184 struct list_head *hook_list)
185{
186 struct nft_hook *hook;
187
188 list_for_each_entry(hook, hook_list, list)
189 nf_unregister_net_hook(net, &hook->ops);
190}
191
c974a3a3
PNA
192static int nf_tables_register_hook(struct net *net,
193 const struct nft_table *table,
194 struct nft_chain *chain)
d8ee8f7c 195{
d54725cd 196 struct nft_base_chain *basechain;
a37061a6 197 const struct nf_hook_ops *ops;
ae6153b5 198
d8ee8f7c 199 if (table->flags & NFT_TABLE_F_DORMANT ||
f323d954 200 !nft_is_base_chain(chain))
d8ee8f7c
PNA
201 return 0;
202
4e25ceb8
FW
203 basechain = nft_base_chain(chain);
204 ops = &basechain->ops;
ae6153b5 205
4e25ceb8
FW
206 if (basechain->type->ops_register)
207 return basechain->type->ops_register(net, ops);
208
d3519cb8 209 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
1e9451cb
FW
210 return nft_netdev_register_hooks(net, &basechain->hook_list);
211
212 return nf_register_net_hook(net, &basechain->ops);
d8ee8f7c
PNA
213}
214
c974a3a3
PNA
215static void nf_tables_unregister_hook(struct net *net,
216 const struct nft_table *table,
217 struct nft_chain *chain)
c5598794 218{
d54725cd 219 struct nft_base_chain *basechain;
4e25ceb8
FW
220 const struct nf_hook_ops *ops;
221
d8ee8f7c 222 if (table->flags & NFT_TABLE_F_DORMANT ||
f323d954 223 !nft_is_base_chain(chain))
d8ee8f7c 224 return;
4e25ceb8
FW
225 basechain = nft_base_chain(chain);
226 ops = &basechain->ops;
d8ee8f7c 227
4e25ceb8
FW
228 if (basechain->type->ops_unregister)
229 return basechain->type->ops_unregister(net, ops);
230
d3519cb8 231 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
1e9451cb
FW
232 nft_netdev_unregister_hooks(net, &basechain->hook_list);
233 else
234 nf_unregister_net_hook(net, &basechain->ops);
c5598794
AB
235}
236
ee01d542
AB
237static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
238{
239 struct nft_trans *trans;
240
241 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
242 if (trans == NULL)
243 return -ENOMEM;
244
245 if (msg_type == NFT_MSG_NEWTABLE)
f2a6d766 246 nft_activate_next(ctx->net, ctx->table);
ee01d542
AB
247
248 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
249 return 0;
250}
251
252static int nft_deltable(struct nft_ctx *ctx)
253{
254 int err;
255
256 err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
257 if (err < 0)
258 return err;
259
f2a6d766 260 nft_deactivate_next(ctx->net, ctx->table);
ee01d542
AB
261 return err;
262}
263
66293c46 264static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
ee01d542
AB
265{
266 struct nft_trans *trans;
267
268 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
269 if (trans == NULL)
66293c46 270 return ERR_PTR(-ENOMEM);
ee01d542 271
74cccc3d 272 if (msg_type == NFT_MSG_NEWCHAIN) {
664b0f8c 273 nft_activate_next(ctx->net, ctx->chain);
ee01d542 274
74cccc3d
PNA
275 if (ctx->nla[NFTA_CHAIN_ID]) {
276 nft_trans_chain_id(trans) =
277 ntohl(nla_get_be32(ctx->nla[NFTA_CHAIN_ID]));
278 }
279 }
280
ee01d542 281 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
66293c46 282 return trans;
ee01d542
AB
283}
284
285static int nft_delchain(struct nft_ctx *ctx)
286{
66293c46 287 struct nft_trans *trans;
ee01d542 288
66293c46
FW
289 trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
290 if (IS_ERR(trans))
291 return PTR_ERR(trans);
ee01d542
AB
292
293 ctx->table->use--;
664b0f8c 294 nft_deactivate_next(ctx->net, ctx->chain);
ee01d542 295
66293c46 296 return 0;
ee01d542
AB
297}
298
bb7b40ae
PNA
299static void nft_rule_expr_activate(const struct nft_ctx *ctx,
300 struct nft_rule *rule)
301{
302 struct nft_expr *expr;
303
304 expr = nft_expr_first(rule);
31cc578a 305 while (nft_expr_more(rule, expr)) {
bb7b40ae
PNA
306 if (expr->ops->activate)
307 expr->ops->activate(ctx, expr);
308
309 expr = nft_expr_next(expr);
310 }
311}
312
313static void nft_rule_expr_deactivate(const struct nft_ctx *ctx,
f6ac8585
PNA
314 struct nft_rule *rule,
315 enum nft_trans_phase phase)
bb7b40ae
PNA
316{
317 struct nft_expr *expr;
318
319 expr = nft_expr_first(rule);
31cc578a 320 while (nft_expr_more(rule, expr)) {
bb7b40ae 321 if (expr->ops->deactivate)
f6ac8585 322 expr->ops->deactivate(ctx, expr, phase);
bb7b40ae
PNA
323
324 expr = nft_expr_next(expr);
325 }
326}
327
ee01d542
AB
328static int
329nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
330{
331 /* You cannot delete the same rule twice */
889f7ee7
PNA
332 if (nft_is_active_next(ctx->net, rule)) {
333 nft_deactivate_next(ctx->net, rule);
ee01d542
AB
334 ctx->chain->use--;
335 return 0;
336 }
337 return -ENOENT;
338}
339
340static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
341 struct nft_rule *rule)
342{
343 struct nft_trans *trans;
344
345 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
346 if (trans == NULL)
347 return NULL;
348
1a94e38d
PNA
349 if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) {
350 nft_trans_rule_id(trans) =
351 ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID]));
352 }
ee01d542
AB
353 nft_trans_rule(trans) = rule;
354 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
355
356 return trans;
357}
358
359static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
360{
63b48c73 361 struct nft_flow_rule *flow;
ee01d542
AB
362 struct nft_trans *trans;
363 int err;
364
365 trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
366 if (trans == NULL)
367 return -ENOMEM;
368
63b48c73
PNA
369 if (ctx->chain->flags & NFT_CHAIN_HW_OFFLOAD) {
370 flow = nft_flow_rule_create(ctx->net, rule);
371 if (IS_ERR(flow)) {
372 nft_trans_destroy(trans);
373 return PTR_ERR(flow);
374 }
375
376 nft_trans_flow_rule(trans) = flow;
377 }
378
ee01d542
AB
379 err = nf_tables_delrule_deactivate(ctx, rule);
380 if (err < 0) {
381 nft_trans_destroy(trans);
382 return err;
383 }
f6ac8585 384 nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE);
ee01d542
AB
385
386 return 0;
387}
388
389static int nft_delrule_by_chain(struct nft_ctx *ctx)
390{
391 struct nft_rule *rule;
392 int err;
393
394 list_for_each_entry(rule, &ctx->chain->rules, list) {
23b7ca4f
PNA
395 if (!nft_is_active_next(ctx->net, rule))
396 continue;
397
ee01d542
AB
398 err = nft_delrule(ctx, rule);
399 if (err < 0)
400 return err;
401 }
402 return 0;
403}
404
cd5125d8 405static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type,
ee01d542
AB
406 struct nft_set *set)
407{
408 struct nft_trans *trans;
409
410 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
411 if (trans == NULL)
412 return -ENOMEM;
413
414 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
415 nft_trans_set_id(trans) =
416 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
37a9cc52 417 nft_activate_next(ctx->net, set);
ee01d542
AB
418 }
419 nft_trans_set(trans) = set;
420 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
421
422 return 0;
423}
424
cd5125d8 425static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set)
ee01d542
AB
426{
427 int err;
428
429 err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
430 if (err < 0)
431 return err;
432
37a9cc52 433 nft_deactivate_next(ctx->net, set);
ee01d542
AB
434 ctx->table->use--;
435
436 return err;
437}
438
e5009240
PNA
439static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type,
440 struct nft_object *obj)
441{
442 struct nft_trans *trans;
443
444 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj));
445 if (trans == NULL)
446 return -ENOMEM;
447
448 if (msg_type == NFT_MSG_NEWOBJ)
449 nft_activate_next(ctx->net, obj);
450
451 nft_trans_obj(trans) = obj;
452 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
453
454 return 0;
455}
456
457static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj)
458{
459 int err;
460
461 err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj);
462 if (err < 0)
463 return err;
464
465 nft_deactivate_next(ctx->net, obj);
466 ctx->table->use--;
467
468 return err;
469}
470
3b49e2e9
PNA
471static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type,
472 struct nft_flowtable *flowtable)
473{
474 struct nft_trans *trans;
475
476 trans = nft_trans_alloc(ctx, msg_type,
477 sizeof(struct nft_trans_flowtable));
478 if (trans == NULL)
479 return -ENOMEM;
480
481 if (msg_type == NFT_MSG_NEWFLOWTABLE)
482 nft_activate_next(ctx->net, flowtable);
483
484 nft_trans_flowtable(trans) = flowtable;
485 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
486
487 return 0;
488}
489
490static int nft_delflowtable(struct nft_ctx *ctx,
491 struct nft_flowtable *flowtable)
492{
493 int err;
494
495 err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable);
496 if (err < 0)
497 return err;
498
499 nft_deactivate_next(ctx->net, flowtable);
500 ctx->table->use--;
501
502 return err;
503}
504
96518518
PM
505/*
506 * Tables
507 */
508
36596dad 509static struct nft_table *nft_table_lookup(const struct net *net,
f2a6d766 510 const struct nlattr *nla,
36596dad 511 u8 family, u8 genmask)
96518518
PM
512{
513 struct nft_table *table;
514
cac20fcd
PNA
515 if (nla == NULL)
516 return ERR_PTR(-EINVAL);
517
0a6a9515
QC
518 list_for_each_entry_rcu(table, &net->nft.tables, list,
519 lockdep_is_held(&net->nft.commit_mutex)) {
f2a6d766 520 if (!nla_strcmp(nla, table->name) &&
98319cb9 521 table->family == family &&
f2a6d766 522 nft_active_genmask(table, genmask))
96518518
PM
523 return table;
524 }
cac20fcd
PNA
525
526 return ERR_PTR(-ENOENT);
96518518
PM
527}
528
3ecbfd65
HS
529static struct nft_table *nft_table_lookup_byhandle(const struct net *net,
530 const struct nlattr *nla,
531 u8 genmask)
532{
533 struct nft_table *table;
534
535 list_for_each_entry(table, &net->nft.tables, list) {
536 if (be64_to_cpu(nla_get_be64(nla)) == table->handle &&
537 nft_active_genmask(table, genmask))
538 return table;
539 }
3ecbfd65
HS
540
541 return ERR_PTR(-ENOENT);
542}
543
96518518
PM
544static inline u64 nf_tables_alloc_handle(struct nft_table *table)
545{
546 return ++table->hgenerator;
547}
548
32537e91 549static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX];
9370761c 550
82603549
PNA
551static const struct nft_chain_type *
552__nft_chain_type_get(u8 family, enum nft_chain_types type)
553{
554 if (family >= NFPROTO_NUMPROTO ||
555 type >= NFT_CHAIN_T_MAX)
556 return NULL;
557
558 return chain_type[family][type];
559}
560
32537e91 561static const struct nft_chain_type *
1ea26cca 562__nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family)
9370761c 563{
82603549 564 const struct nft_chain_type *type;
9370761c
PNA
565 int i;
566
baae3e62 567 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
82603549
PNA
568 type = __nft_chain_type_get(family, i);
569 if (!type)
570 continue;
571 if (!nla_strcmp(nla, type->name))
572 return type;
9370761c 573 }
baae3e62 574 return NULL;
9370761c
PNA
575}
576
eb014de4
PNA
577struct nft_module_request {
578 struct list_head list;
579 char module[MODULE_NAME_LEN];
580 bool done;
581};
582
452238e8 583#ifdef CONFIG_MODULES
35b7ee34
AL
584static __printf(2, 3) int nft_request_module(struct net *net, const char *fmt,
585 ...)
452238e8
FW
586{
587 char module_name[MODULE_NAME_LEN];
eb014de4 588 struct nft_module_request *req;
452238e8
FW
589 va_list args;
590 int ret;
591
452238e8
FW
592 va_start(args, fmt);
593 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
594 va_end(args);
9332d27d 595 if (ret >= MODULE_NAME_LEN)
eb014de4 596 return 0;
452238e8 597
eb014de4
PNA
598 list_for_each_entry(req, &net->nft.module_list, list) {
599 if (!strcmp(req->module, module_name)) {
600 if (req->done)
601 return 0;
ec7470b8 602
eb014de4
PNA
603 /* A request to load this module already exists. */
604 return -EAGAIN;
605 }
606 }
607
608 req = kmalloc(sizeof(*req), GFP_KERNEL);
609 if (!req)
610 return -ENOMEM;
611
612 req->done = false;
613 strlcpy(req->module, module_name, MODULE_NAME_LEN);
614 list_add_tail(&req->list, &net->nft.module_list);
615
616 return -EAGAIN;
452238e8
FW
617}
618#endif
619
f102d66b
FW
620static void lockdep_nfnl_nft_mutex_not_held(void)
621{
622#ifdef CONFIG_PROVE_LOCKING
c0700dfa
FW
623 if (debug_locks)
624 WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES));
f102d66b
FW
625#endif
626}
627
32537e91 628static const struct nft_chain_type *
452238e8
FW
629nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla,
630 u8 family, bool autoload)
9370761c 631{
32537e91 632 const struct nft_chain_type *type;
9370761c 633
1ea26cca 634 type = __nf_tables_chain_type_lookup(nla, family);
93b0806f
PM
635 if (type != NULL)
636 return type;
f102d66b
FW
637
638 lockdep_nfnl_nft_mutex_not_held();
9370761c 639#ifdef CONFIG_MODULES
93b0806f 640 if (autoload) {
eb014de4
PNA
641 if (nft_request_module(net, "nft-chain-%u-%.*s", family,
642 nla_len(nla),
643 (const char *)nla_data(nla)) == -EAGAIN)
93b0806f 644 return ERR_PTR(-EAGAIN);
9370761c
PNA
645 }
646#endif
93b0806f 647 return ERR_PTR(-ENOENT);
9370761c
PNA
648}
649
96518518 650static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
1cae565e
PNA
651 [NFTA_TABLE_NAME] = { .type = NLA_STRING,
652 .len = NFT_TABLE_MAXNAMELEN - 1 },
9ddf6323 653 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
3ecbfd65 654 [NFTA_TABLE_HANDLE] = { .type = NLA_U64 },
7a81575b
JGG
655 [NFTA_TABLE_USERDATA] = { .type = NLA_BINARY,
656 .len = NFT_USERDATA_MAXLEN }
96518518
PM
657};
658
84d7fce6
PNA
659static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
660 u32 portid, u32 seq, int event, u32 flags,
661 int family, const struct nft_table *table)
96518518
PM
662{
663 struct nlmsghdr *nlh;
664 struct nfgenmsg *nfmsg;
665
dedb67c4 666 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
96518518
PM
667 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
668 if (nlh == NULL)
669 goto nla_put_failure;
670
671 nfmsg = nlmsg_data(nlh);
672 nfmsg->nfgen_family = family;
673 nfmsg->version = NFNETLINK_V0;
84d7fce6 674 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
96518518 675
9ddf6323 676 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
d8bcc768 677 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
3ecbfd65
HS
678 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) ||
679 nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle),
680 NFTA_TABLE_PAD))
96518518
PM
681 goto nla_put_failure;
682
7a81575b
JGG
683 if (table->udata) {
684 if (nla_put(skb, NFTA_TABLE_USERDATA, table->udlen, table->udata))
685 goto nla_put_failure;
686 }
687
053c095a
JB
688 nlmsg_end(skb, nlh);
689 return 0;
96518518
PM
690
691nla_put_failure:
692 nlmsg_trim(skb, nlh);
693 return -1;
694}
695
67cc570e
PNA
696struct nftnl_skb_parms {
697 bool report;
698};
699#define NFT_CB(skb) (*(struct nftnl_skb_parms*)&((skb)->cb))
700
701static void nft_notify_enqueue(struct sk_buff *skb, bool report,
702 struct list_head *notify_list)
703{
704 NFT_CB(skb).report = report;
705 list_add_tail(&skb->list, notify_list);
706}
707
25e94a99 708static void nf_tables_table_notify(const struct nft_ctx *ctx, int event)
96518518
PM
709{
710 struct sk_buff *skb;
96518518 711 int err;
8e6cf365
RGB
712 char *buf = kasprintf(GFP_KERNEL, "%s:%llu;?:0",
713 ctx->table->name, ctx->table->handle);
714
715 audit_log_nfcfg(buf,
716 ctx->family,
717 ctx->table->use,
718 event == NFT_MSG_NEWTABLE ?
719 AUDIT_NFT_OP_TABLE_REGISTER :
14224039
RGB
720 AUDIT_NFT_OP_TABLE_UNREGISTER,
721 GFP_KERNEL);
8e6cf365 722 kfree(buf);
96518518 723
128ad332
PNA
724 if (!ctx->report &&
725 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
25e94a99 726 return;
96518518 727
96518518
PM
728 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
729 if (skb == NULL)
730 goto err;
731
84d7fce6 732 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
36596dad 733 event, 0, ctx->family, ctx->table);
96518518
PM
734 if (err < 0) {
735 kfree_skb(skb);
736 goto err;
737 }
738
67cc570e 739 nft_notify_enqueue(skb, ctx->report, &ctx->net->nft.notify_list);
25e94a99 740 return;
96518518 741err:
25e94a99 742 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
96518518
PM
743}
744
745static int nf_tables_dump_tables(struct sk_buff *skb,
746 struct netlink_callback *cb)
747{
748 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
96518518
PM
749 const struct nft_table *table;
750 unsigned int idx = 0, s_idx = cb->args[0];
99633ab2 751 struct net *net = sock_net(skb->sk);
96518518
PM
752 int family = nfmsg->nfgen_family;
753
e688a7f8 754 rcu_read_lock();
38e029f1
PNA
755 cb->seq = net->nft.base_seq;
756
36596dad 757 list_for_each_entry_rcu(table, &net->nft.tables, list) {
98319cb9 758 if (family != NFPROTO_UNSPEC && family != table->family)
96518518
PM
759 continue;
760
36596dad
PNA
761 if (idx < s_idx)
762 goto cont;
763 if (idx > s_idx)
764 memset(&cb->args[1], 0,
765 sizeof(cb->args) - sizeof(cb->args[0]));
766 if (!nft_is_active(net, table))
767 continue;
768 if (nf_tables_fill_table_info(skb, net,
769 NETLINK_CB(cb->skb).portid,
770 cb->nlh->nlmsg_seq,
771 NFT_MSG_NEWTABLE, NLM_F_MULTI,
98319cb9 772 table->family, table) < 0)
36596dad
PNA
773 goto done;
774
775 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
96518518 776cont:
36596dad 777 idx++;
96518518
PM
778 }
779done:
e688a7f8 780 rcu_read_unlock();
96518518
PM
781 cb->args[0] = idx;
782 return skb->len;
783}
784
d9adf22a
FW
785static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb,
786 const struct nlmsghdr *nlh,
787 struct netlink_dump_control *c)
788{
789 int err;
790
791 if (!try_module_get(THIS_MODULE))
792 return -EINVAL;
793
794 rcu_read_unlock();
795 err = netlink_dump_start(nlsk, skb, nlh, c);
796 rcu_read_lock();
797 module_put(THIS_MODULE);
798
799 return err;
800}
801
802/* called with rcu_read_lock held */
7b8002a1
PNA
803static int nf_tables_gettable(struct net *net, struct sock *nlsk,
804 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
805 const struct nlattr * const nla[],
806 struct netlink_ext_ack *extack)
96518518
PM
807{
808 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 809 u8 genmask = nft_genmask_cur(net);
96518518
PM
810 const struct nft_table *table;
811 struct sk_buff *skb2;
812 int family = nfmsg->nfgen_family;
813 int err;
814
815 if (nlh->nlmsg_flags & NLM_F_DUMP) {
816 struct netlink_dump_control c = {
817 .dump = nf_tables_dump_tables,
d9adf22a 818 .module = THIS_MODULE,
96518518 819 };
d9adf22a
FW
820
821 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
96518518
PM
822 }
823
cac20fcd 824 table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask);
36dd1bcc
PNA
825 if (IS_ERR(table)) {
826 NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]);
96518518 827 return PTR_ERR(table);
36dd1bcc 828 }
96518518 829
d9adf22a 830 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
96518518
PM
831 if (!skb2)
832 return -ENOMEM;
833
84d7fce6 834 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
96518518
PM
835 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
836 family, table);
837 if (err < 0)
ee921183 838 goto err_fill_table_info;
96518518 839
ee921183 840 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
96518518 841
ee921183 842err_fill_table_info:
96518518
PM
843 kfree_skb(skb2);
844 return err;
845}
846
c9c17211 847static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt)
10435c11
F
848{
849 struct nft_chain *chain;
850 u32 i = 0;
851
852 list_for_each_entry(chain, &table->chains, list) {
853 if (!nft_is_active_next(net, chain))
854 continue;
f323d954 855 if (!nft_is_base_chain(chain))
10435c11
F
856 continue;
857
858 if (cnt && i++ == cnt)
859 break;
860
1e9451cb 861 nf_tables_unregister_hook(net, table, chain);
10435c11
F
862 }
863}
864
c9c17211 865static int nf_tables_table_enable(struct net *net, struct nft_table *table)
9ddf6323
PNA
866{
867 struct nft_chain *chain;
868 int err, i = 0;
869
870 list_for_each_entry(chain, &table->chains, list) {
664b0f8c
PNA
871 if (!nft_is_active_next(net, chain))
872 continue;
f323d954 873 if (!nft_is_base_chain(chain))
d2012975
PNA
874 continue;
875
1e9451cb 876 err = nf_tables_register_hook(net, table, chain);
9ddf6323 877 if (err < 0)
d54725cd 878 goto err_register_hooks;
9ddf6323
PNA
879
880 i++;
881 }
882 return 0;
d54725cd
PNA
883
884err_register_hooks:
10435c11 885 if (i)
c9c17211 886 nft_table_disable(net, table, i);
9ddf6323
PNA
887 return err;
888}
889
c9c17211 890static void nf_tables_table_disable(struct net *net, struct nft_table *table)
9ddf6323 891{
c9c17211 892 nft_table_disable(net, table, 0);
9ddf6323
PNA
893}
894
e1aaca93 895static int nf_tables_updtable(struct nft_ctx *ctx)
9ddf6323 896{
55dd6f93 897 struct nft_trans *trans;
e1aaca93 898 u32 flags;
55dd6f93 899 int ret = 0;
9ddf6323 900
e1aaca93
PNA
901 if (!ctx->nla[NFTA_TABLE_FLAGS])
902 return 0;
9ddf6323 903
e1aaca93
PNA
904 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
905 if (flags & ~NFT_TABLE_F_DORMANT)
906 return -EINVAL;
907
63283dd2
PNA
908 if (flags == ctx->table->flags)
909 return 0;
910
55dd6f93
PNA
911 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
912 sizeof(struct nft_trans_table));
913 if (trans == NULL)
914 return -ENOMEM;
9ddf6323 915
e1aaca93
PNA
916 if ((flags & NFT_TABLE_F_DORMANT) &&
917 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
55dd6f93 918 nft_trans_table_enable(trans) = false;
e1aaca93
PNA
919 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
920 ctx->table->flags & NFT_TABLE_F_DORMANT) {
1e9451cb 921 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
c9c17211 922 ret = nf_tables_table_enable(ctx->net, ctx->table);
1e9451cb 923 if (ret >= 0)
55dd6f93 924 nft_trans_table_enable(trans) = true;
1e9451cb
FW
925 else
926 ctx->table->flags |= NFT_TABLE_F_DORMANT;
9ddf6323 927 }
e1aaca93
PNA
928 if (ret < 0)
929 goto err;
9ddf6323 930
55dd6f93
PNA
931 nft_trans_table_update(trans) = true;
932 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
933 return 0;
9ddf6323 934err:
55dd6f93 935 nft_trans_destroy(trans);
9ddf6323
PNA
936 return ret;
937}
938
1b2470e5
FW
939static u32 nft_chain_hash(const void *data, u32 len, u32 seed)
940{
941 const char *name = data;
942
943 return jhash(name, strlen(name), seed);
944}
945
946static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed)
947{
948 const struct nft_chain *chain = data;
949
950 return nft_chain_hash(chain->name, 0, seed);
951}
952
953static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg,
954 const void *ptr)
955{
956 const struct nft_chain *chain = ptr;
957 const char *name = arg->key;
958
959 return strcmp(chain->name, name);
960}
961
4d44175a
FW
962static u32 nft_objname_hash(const void *data, u32 len, u32 seed)
963{
964 const struct nft_object_hash_key *k = data;
965
966 seed ^= hash_ptr(k->table, 32);
967
968 return jhash(k->name, strlen(k->name), seed);
969}
970
971static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed)
972{
973 const struct nft_object *obj = data;
974
975 return nft_objname_hash(&obj->key, 0, seed);
976}
977
978static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg,
979 const void *ptr)
980{
981 const struct nft_object_hash_key *k = arg->key;
982 const struct nft_object *obj = ptr;
983
984 if (obj->key.table != k->table)
985 return -1;
986
987 return strcmp(obj->key.name, k->name);
988}
989
633c9a84
PNA
990static int nf_tables_newtable(struct net *net, struct sock *nlsk,
991 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
992 const struct nlattr * const nla[],
993 struct netlink_ext_ack *extack)
96518518
PM
994{
995 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 996 u8 genmask = nft_genmask_next(net);
96518518 997 int family = nfmsg->nfgen_family;
36dd1bcc
PNA
998 const struct nlattr *attr;
999 struct nft_table *table;
e1aaca93 1000 struct nft_ctx ctx;
7a81575b 1001 u32 flags = 0;
55dd6f93 1002 int err;
96518518 1003
f102d66b 1004 lockdep_assert_held(&net->nft.commit_mutex);
36dd1bcc
PNA
1005 attr = nla[NFTA_TABLE_NAME];
1006 table = nft_table_lookup(net, attr, family, genmask);
96518518
PM
1007 if (IS_ERR(table)) {
1008 if (PTR_ERR(table) != -ENOENT)
1009 return PTR_ERR(table);
1a28ad74 1010 } else {
36dd1bcc
PNA
1011 if (nlh->nlmsg_flags & NLM_F_EXCL) {
1012 NL_SET_BAD_ATTR(extack, attr);
96518518 1013 return -EEXIST;
36dd1bcc 1014 }
96518518
PM
1015 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1016 return -EOPNOTSUPP;
e1aaca93 1017
98319cb9 1018 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
e1aaca93 1019 return nf_tables_updtable(&ctx);
96518518
PM
1020 }
1021
c5c1f975
PM
1022 if (nla[NFTA_TABLE_FLAGS]) {
1023 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
1024 if (flags & ~NFT_TABLE_F_DORMANT)
1025 return -EINVAL;
1026 }
1027
ffdb210e 1028 err = -ENOMEM;
1cae565e 1029 table = kzalloc(sizeof(*table), GFP_KERNEL);
ffdb210e 1030 if (table == NULL)
98319cb9 1031 goto err_kzalloc;
96518518 1032
36dd1bcc 1033 table->name = nla_strdup(attr, GFP_KERNEL);
e46abbcc 1034 if (table->name == NULL)
98319cb9 1035 goto err_strdup;
e46abbcc 1036
7a81575b 1037 if (nla[NFTA_TABLE_USERDATA]) {
85db827a 1038 table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL);
7a81575b
JGG
1039 if (table->udata == NULL)
1040 goto err_table_udata;
1041
85db827a 1042 table->udlen = nla_len(nla[NFTA_TABLE_USERDATA]);
7a81575b
JGG
1043 }
1044
1b2470e5
FW
1045 err = rhltable_init(&table->chains_ht, &nft_chain_ht_params);
1046 if (err)
1047 goto err_chain_ht;
1048
96518518 1049 INIT_LIST_HEAD(&table->chains);
20a69341 1050 INIT_LIST_HEAD(&table->sets);
e5009240 1051 INIT_LIST_HEAD(&table->objects);
3b49e2e9 1052 INIT_LIST_HEAD(&table->flowtables);
98319cb9 1053 table->family = family;
c5c1f975 1054 table->flags = flags;
3ecbfd65 1055 table->handle = ++table_handle;
9ddf6323 1056
98319cb9 1057 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
55dd6f93 1058 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
ffdb210e 1059 if (err < 0)
98319cb9 1060 goto err_trans;
ffdb210e 1061
36596dad 1062 list_add_tail_rcu(&table->list, &net->nft.tables);
96518518 1063 return 0;
98319cb9 1064err_trans:
1b2470e5
FW
1065 rhltable_destroy(&table->chains_ht);
1066err_chain_ht:
7a81575b
JGG
1067 kfree(table->udata);
1068err_table_udata:
e46abbcc 1069 kfree(table->name);
98319cb9 1070err_strdup:
ffdb210e 1071 kfree(table);
98319cb9 1072err_kzalloc:
ffdb210e 1073 return err;
96518518
PM
1074}
1075
b9ac12ef
AB
1076static int nft_flush_table(struct nft_ctx *ctx)
1077{
3b49e2e9 1078 struct nft_flowtable *flowtable, *nft;
b9ac12ef 1079 struct nft_chain *chain, *nc;
e5009240 1080 struct nft_object *obj, *ne;
b9ac12ef 1081 struct nft_set *set, *ns;
3b49e2e9 1082 int err;
b9ac12ef 1083
a2f18db0 1084 list_for_each_entry(chain, &ctx->table->chains, list) {
664b0f8c
PNA
1085 if (!nft_is_active_next(ctx->net, chain))
1086 continue;
1087
d0e2c7de
PNA
1088 if (nft_chain_is_bound(chain))
1089 continue;
1090
b9ac12ef
AB
1091 ctx->chain = chain;
1092
1093 err = nft_delrule_by_chain(ctx);
1094 if (err < 0)
1095 goto out;
b9ac12ef
AB
1096 }
1097
1098 list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
37a9cc52
PNA
1099 if (!nft_is_active_next(ctx->net, set))
1100 continue;
1101
408070d6 1102 if (nft_set_is_anonymous(set) &&
b9ac12ef
AB
1103 !list_empty(&set->bindings))
1104 continue;
1105
1106 err = nft_delset(ctx, set);
1107 if (err < 0)
1108 goto out;
1109 }
1110
3b49e2e9 1111 list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) {
335178d5
FW
1112 if (!nft_is_active_next(ctx->net, flowtable))
1113 continue;
1114
3b49e2e9
PNA
1115 err = nft_delflowtable(ctx, flowtable);
1116 if (err < 0)
1117 goto out;
1118 }
1119
e5009240 1120 list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) {
335178d5
FW
1121 if (!nft_is_active_next(ctx->net, obj))
1122 continue;
1123
e5009240
PNA
1124 err = nft_delobj(ctx, obj);
1125 if (err < 0)
1126 goto out;
1127 }
1128
a2f18db0 1129 list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
664b0f8c
PNA
1130 if (!nft_is_active_next(ctx->net, chain))
1131 continue;
1132
d0e2c7de
PNA
1133 if (nft_chain_is_bound(chain))
1134 continue;
1135
a2f18db0
PNA
1136 ctx->chain = chain;
1137
1138 err = nft_delchain(ctx);
1139 if (err < 0)
1140 goto out;
1141 }
1142
b9ac12ef
AB
1143 err = nft_deltable(ctx);
1144out:
1145 return err;
1146}
1147
1148static int nft_flush(struct nft_ctx *ctx, int family)
1149{
b9ac12ef
AB
1150 struct nft_table *table, *nt;
1151 const struct nlattr * const *nla = ctx->nla;
1152 int err = 0;
1153
36596dad 1154 list_for_each_entry_safe(table, nt, &ctx->net->nft.tables, list) {
98319cb9 1155 if (family != AF_UNSPEC && table->family != family)
b9ac12ef
AB
1156 continue;
1157
98319cb9 1158 ctx->family = table->family;
f2a6d766 1159
36596dad
PNA
1160 if (!nft_is_active_next(ctx->net, table))
1161 continue;
b9ac12ef 1162
36596dad
PNA
1163 if (nla[NFTA_TABLE_NAME] &&
1164 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
1165 continue;
b9ac12ef 1166
36596dad
PNA
1167 ctx->table = table;
1168
1169 err = nft_flush_table(ctx);
1170 if (err < 0)
1171 goto out;
b9ac12ef
AB
1172 }
1173out:
1174 return err;
1175}
1176
633c9a84
PNA
1177static int nf_tables_deltable(struct net *net, struct sock *nlsk,
1178 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
1179 const struct nlattr * const nla[],
1180 struct netlink_ext_ack *extack)
96518518
PM
1181{
1182 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 1183 u8 genmask = nft_genmask_next(net);
ee01d542 1184 int family = nfmsg->nfgen_family;
36dd1bcc
PNA
1185 const struct nlattr *attr;
1186 struct nft_table *table;
55dd6f93 1187 struct nft_ctx ctx;
96518518 1188
36596dad 1189 nft_ctx_init(&ctx, net, skb, nlh, 0, NULL, NULL, nla);
3ecbfd65
HS
1190 if (family == AF_UNSPEC ||
1191 (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE]))
b9ac12ef
AB
1192 return nft_flush(&ctx, family);
1193
36dd1bcc
PNA
1194 if (nla[NFTA_TABLE_HANDLE]) {
1195 attr = nla[NFTA_TABLE_HANDLE];
1196 table = nft_table_lookup_byhandle(net, attr, genmask);
1197 } else {
1198 attr = nla[NFTA_TABLE_NAME];
1199 table = nft_table_lookup(net, attr, family, genmask);
1200 }
3ecbfd65 1201
36dd1bcc
PNA
1202 if (IS_ERR(table)) {
1203 NL_SET_BAD_ATTR(extack, attr);
96518518 1204 return PTR_ERR(table);
36dd1bcc 1205 }
96518518 1206
a8278400
PNA
1207 if (nlh->nlmsg_flags & NLM_F_NONREC &&
1208 table->use > 0)
1209 return -EBUSY;
1210
98319cb9 1211 ctx.family = family;
b9ac12ef 1212 ctx.table = table;
55dd6f93 1213
b9ac12ef 1214 return nft_flush_table(&ctx);
96518518
PM
1215}
1216
55dd6f93
PNA
1217static void nf_tables_table_destroy(struct nft_ctx *ctx)
1218{
fa5950e4
FW
1219 if (WARN_ON(ctx->table->use > 0))
1220 return;
4fefee57 1221
1b2470e5 1222 rhltable_destroy(&ctx->table->chains_ht);
e46abbcc 1223 kfree(ctx->table->name);
bc7a7082 1224 kfree(ctx->table->udata);
55dd6f93 1225 kfree(ctx->table);
55dd6f93
PNA
1226}
1227
cc07eeb0 1228void nft_register_chain_type(const struct nft_chain_type *ctype)
96518518 1229{
96518518 1230 nfnl_lock(NFNL_SUBSYS_NFTABLES);
82603549 1231 if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) {
cc07eeb0
PNA
1232 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1233 return;
96518518 1234 }
9370761c 1235 chain_type[ctype->family][ctype->type] = ctype;
96518518 1236 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
96518518 1237}
9370761c 1238EXPORT_SYMBOL_GPL(nft_register_chain_type);
96518518 1239
32537e91 1240void nft_unregister_chain_type(const struct nft_chain_type *ctype)
96518518 1241{
96518518 1242 nfnl_lock(NFNL_SUBSYS_NFTABLES);
9370761c 1243 chain_type[ctype->family][ctype->type] = NULL;
96518518
PM
1244 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1245}
9370761c 1246EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
96518518
PM
1247
1248/*
1249 * Chains
1250 */
1251
1252static struct nft_chain *
cac20fcd 1253nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask)
96518518
PM
1254{
1255 struct nft_chain *chain;
1256
1257 list_for_each_entry(chain, &table->chains, list) {
664b0f8c
PNA
1258 if (chain->handle == handle &&
1259 nft_active_genmask(chain, genmask))
96518518
PM
1260 return chain;
1261 }
1262
1263 return ERR_PTR(-ENOENT);
1264}
1265
4d44175a 1266static bool lockdep_commit_lock_is_held(const struct net *net)
f102d66b
FW
1267{
1268#ifdef CONFIG_PROVE_LOCKING
1269 return lockdep_is_held(&net->nft.commit_mutex);
1270#else
1271 return true;
1272#endif
1273}
1274
1275static struct nft_chain *nft_chain_lookup(struct net *net,
1276 struct nft_table *table,
cac20fcd 1277 const struct nlattr *nla, u8 genmask)
96518518 1278{
1b2470e5
FW
1279 char search[NFT_CHAIN_MAXNAMELEN + 1];
1280 struct rhlist_head *tmp, *list;
96518518
PM
1281 struct nft_chain *chain;
1282
1283 if (nla == NULL)
1284 return ERR_PTR(-EINVAL);
1285
872f6903 1286 nla_strscpy(search, nla, sizeof(search));
96518518 1287
1b2470e5 1288 WARN_ON(!rcu_read_lock_held() &&
f102d66b 1289 !lockdep_commit_lock_is_held(net));
1b2470e5
FW
1290
1291 chain = ERR_PTR(-ENOENT);
1292 rcu_read_lock();
1293 list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params);
1294 if (!list)
1295 goto out_unlock;
1296
1297 rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
1298 if (nft_active_genmask(chain, genmask))
1299 goto out_unlock;
1300 }
1301 chain = ERR_PTR(-ENOENT);
1302out_unlock:
1303 rcu_read_unlock();
1304 return chain;
96518518
PM
1305}
1306
1307static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
b2fbd044
LZ
1308 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING,
1309 .len = NFT_TABLE_MAXNAMELEN - 1 },
96518518
PM
1310 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
1311 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
1312 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1313 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
0ca743a5 1314 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
9332d27d
FW
1315 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING,
1316 .len = NFT_MODULE_AUTOLOAD_LIMIT },
0ca743a5 1317 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
c9626a2c 1318 [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 },
74cccc3d 1319 [NFTA_CHAIN_ID] = { .type = NLA_U32 },
002f2176
JGG
1320 [NFTA_CHAIN_USERDATA] = { .type = NLA_BINARY,
1321 .len = NFT_USERDATA_MAXLEN },
96518518
PM
1322};
1323
1324static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
1325 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
1326 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
2cbce139
PNA
1327 [NFTA_HOOK_DEV] = { .type = NLA_STRING,
1328 .len = IFNAMSIZ - 1 },
96518518
PM
1329};
1330
0ca743a5
PNA
1331static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
1332{
1333 struct nft_stats *cpu_stats, total;
1334 struct nlattr *nest;
ce355e20
ED
1335 unsigned int seq;
1336 u64 pkts, bytes;
0ca743a5
PNA
1337 int cpu;
1338
edbd82c5
FW
1339 if (!stats)
1340 return 0;
1341
0ca743a5
PNA
1342 memset(&total, 0, sizeof(total));
1343 for_each_possible_cpu(cpu) {
1344 cpu_stats = per_cpu_ptr(stats, cpu);
ce355e20
ED
1345 do {
1346 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1347 pkts = cpu_stats->pkts;
1348 bytes = cpu_stats->bytes;
1349 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
1350 total.pkts += pkts;
1351 total.bytes += bytes;
0ca743a5 1352 }
ae0be8de 1353 nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS);
0ca743a5
PNA
1354 if (nest == NULL)
1355 goto nla_put_failure;
1356
b46f6ded
ND
1357 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts),
1358 NFTA_COUNTER_PAD) ||
1359 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
1360 NFTA_COUNTER_PAD))
0ca743a5
PNA
1361 goto nla_put_failure;
1362
1363 nla_nest_end(skb, nest);
1364 return 0;
1365
1366nla_put_failure:
1367 return -ENOSPC;
1368}
1369
d54725cd
PNA
1370static int nft_dump_basechain_hook(struct sk_buff *skb, int family,
1371 const struct nft_base_chain *basechain)
1372{
1373 const struct nf_hook_ops *ops = &basechain->ops;
1374 struct nft_hook *hook, *first = NULL;
1375 struct nlattr *nest, *nest_devs;
1376 int n = 0;
1377
1378 nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK);
1379 if (nest == NULL)
1380 goto nla_put_failure;
1381 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
1382 goto nla_put_failure;
1383 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
1384 goto nla_put_failure;
1385
d3519cb8 1386 if (nft_base_chain_netdev(family, ops->hooknum)) {
d54725cd
PNA
1387 nest_devs = nla_nest_start_noflag(skb, NFTA_HOOK_DEVS);
1388 list_for_each_entry(hook, &basechain->hook_list, list) {
1389 if (!first)
1390 first = hook;
1391
1392 if (nla_put_string(skb, NFTA_DEVICE_NAME,
1393 hook->ops.dev->name))
1394 goto nla_put_failure;
1395 n++;
1396 }
1397 nla_nest_end(skb, nest_devs);
1398
1399 if (n == 1 &&
1400 nla_put_string(skb, NFTA_HOOK_DEV, first->ops.dev->name))
1401 goto nla_put_failure;
1402 }
1403 nla_nest_end(skb, nest);
1404
1405 return 0;
1406nla_put_failure:
1407 return -1;
1408}
1409
84d7fce6
PNA
1410static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
1411 u32 portid, u32 seq, int event, u32 flags,
1412 int family, const struct nft_table *table,
96518518
PM
1413 const struct nft_chain *chain)
1414{
1415 struct nlmsghdr *nlh;
1416 struct nfgenmsg *nfmsg;
1417
dedb67c4 1418 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
96518518
PM
1419 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
1420 if (nlh == NULL)
1421 goto nla_put_failure;
1422
1423 nfmsg = nlmsg_data(nlh);
1424 nfmsg->nfgen_family = family;
1425 nfmsg->version = NFNETLINK_V0;
84d7fce6 1426 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
96518518
PM
1427
1428 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
1429 goto nla_put_failure;
b46f6ded
ND
1430 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle),
1431 NFTA_CHAIN_PAD))
96518518
PM
1432 goto nla_put_failure;
1433 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
1434 goto nla_put_failure;
1435
f323d954 1436 if (nft_is_base_chain(chain)) {
0ca743a5 1437 const struct nft_base_chain *basechain = nft_base_chain(chain);
edbd82c5 1438 struct nft_stats __percpu *stats;
0ca743a5 1439
d54725cd 1440 if (nft_dump_basechain_hook(skb, family, basechain))
2cbce139 1441 goto nla_put_failure;
9370761c 1442
0ca743a5
PNA
1443 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
1444 htonl(basechain->policy)))
1445 goto nla_put_failure;
1446
baae3e62
PM
1447 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
1448 goto nla_put_failure;
0ca743a5 1449
edbd82c5
FW
1450 stats = rcu_dereference_check(basechain->stats,
1451 lockdep_commit_lock_is_held(net));
1452 if (nft_dump_stats(skb, stats))
0ca743a5 1453 goto nla_put_failure;
96518518
PM
1454 }
1455
d0e2c7de
PNA
1456 if (chain->flags &&
1457 nla_put_be32(skb, NFTA_CHAIN_FLAGS, htonl(chain->flags)))
1458 goto nla_put_failure;
1459
0ca743a5
PNA
1460 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
1461 goto nla_put_failure;
1462
002f2176
JGG
1463 if (chain->udata &&
1464 nla_put(skb, NFTA_CHAIN_USERDATA, chain->udlen, chain->udata))
1465 goto nla_put_failure;
1466
053c095a
JB
1467 nlmsg_end(skb, nlh);
1468 return 0;
96518518
PM
1469
1470nla_put_failure:
1471 nlmsg_trim(skb, nlh);
1472 return -1;
1473}
1474
25e94a99 1475static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
96518518
PM
1476{
1477 struct sk_buff *skb;
96518518 1478 int err;
8e6cf365
RGB
1479 char *buf = kasprintf(GFP_KERNEL, "%s:%llu;%s:%llu",
1480 ctx->table->name, ctx->table->handle,
1481 ctx->chain->name, ctx->chain->handle);
1482
1483 audit_log_nfcfg(buf,
1484 ctx->family,
1485 ctx->chain->use,
1486 event == NFT_MSG_NEWCHAIN ?
1487 AUDIT_NFT_OP_CHAIN_REGISTER :
14224039
RGB
1488 AUDIT_NFT_OP_CHAIN_UNREGISTER,
1489 GFP_KERNEL);
8e6cf365 1490 kfree(buf);
96518518 1491
128ad332
PNA
1492 if (!ctx->report &&
1493 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
25e94a99 1494 return;
96518518 1495
96518518
PM
1496 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1497 if (skb == NULL)
1498 goto err;
1499
84d7fce6 1500 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
36596dad 1501 event, 0, ctx->family, ctx->table,
35151d84 1502 ctx->chain);
96518518
PM
1503 if (err < 0) {
1504 kfree_skb(skb);
1505 goto err;
1506 }
1507
67cc570e 1508 nft_notify_enqueue(skb, ctx->report, &ctx->net->nft.notify_list);
25e94a99 1509 return;
96518518 1510err:
25e94a99 1511 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
96518518
PM
1512}
1513
1514static int nf_tables_dump_chains(struct sk_buff *skb,
1515 struct netlink_callback *cb)
1516{
1517 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
96518518
PM
1518 const struct nft_table *table;
1519 const struct nft_chain *chain;
1520 unsigned int idx = 0, s_idx = cb->args[0];
99633ab2 1521 struct net *net = sock_net(skb->sk);
96518518
PM
1522 int family = nfmsg->nfgen_family;
1523
e688a7f8 1524 rcu_read_lock();
38e029f1
PNA
1525 cb->seq = net->nft.base_seq;
1526
36596dad 1527 list_for_each_entry_rcu(table, &net->nft.tables, list) {
98319cb9 1528 if (family != NFPROTO_UNSPEC && family != table->family)
96518518
PM
1529 continue;
1530
36596dad
PNA
1531 list_for_each_entry_rcu(chain, &table->chains, list) {
1532 if (idx < s_idx)
1533 goto cont;
1534 if (idx > s_idx)
1535 memset(&cb->args[1], 0,
1536 sizeof(cb->args) - sizeof(cb->args[0]));
1537 if (!nft_is_active(net, chain))
1538 continue;
1539 if (nf_tables_fill_chain_info(skb, net,
1540 NETLINK_CB(cb->skb).portid,
1541 cb->nlh->nlmsg_seq,
1542 NFT_MSG_NEWCHAIN,
1543 NLM_F_MULTI,
98319cb9 1544 table->family, table,
36596dad
PNA
1545 chain) < 0)
1546 goto done;
38e029f1 1547
36596dad 1548 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
96518518 1549cont:
36596dad 1550 idx++;
96518518
PM
1551 }
1552 }
1553done:
e688a7f8 1554 rcu_read_unlock();
96518518
PM
1555 cb->args[0] = idx;
1556 return skb->len;
1557}
1558
d9adf22a 1559/* called with rcu_read_lock held */
7b8002a1
PNA
1560static int nf_tables_getchain(struct net *net, struct sock *nlsk,
1561 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
1562 const struct nlattr * const nla[],
1563 struct netlink_ext_ack *extack)
96518518
PM
1564{
1565 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 1566 u8 genmask = nft_genmask_cur(net);
96518518 1567 const struct nft_chain *chain;
1b2470e5 1568 struct nft_table *table;
96518518
PM
1569 struct sk_buff *skb2;
1570 int family = nfmsg->nfgen_family;
1571 int err;
1572
1573 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1574 struct netlink_dump_control c = {
1575 .dump = nf_tables_dump_chains,
d9adf22a 1576 .module = THIS_MODULE,
96518518 1577 };
d9adf22a
FW
1578
1579 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
96518518
PM
1580 }
1581
cac20fcd 1582 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask);
36dd1bcc
PNA
1583 if (IS_ERR(table)) {
1584 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
96518518 1585 return PTR_ERR(table);
36dd1bcc 1586 }
96518518 1587
f102d66b 1588 chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask);
36dd1bcc
PNA
1589 if (IS_ERR(chain)) {
1590 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
96518518 1591 return PTR_ERR(chain);
36dd1bcc 1592 }
96518518 1593
d9adf22a 1594 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
96518518
PM
1595 if (!skb2)
1596 return -ENOMEM;
1597
84d7fce6 1598 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
96518518
PM
1599 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
1600 family, table, chain);
1601 if (err < 0)
ee921183 1602 goto err_fill_chain_info;
96518518 1603
ee921183 1604 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
96518518 1605
ee921183 1606err_fill_chain_info:
96518518
PM
1607 kfree_skb(skb2);
1608 return err;
1609}
1610
0ca743a5
PNA
1611static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1612 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
1613 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
1614};
1615
ff3cd7b3 1616static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
0ca743a5
PNA
1617{
1618 struct nlattr *tb[NFTA_COUNTER_MAX+1];
1619 struct nft_stats __percpu *newstats;
1620 struct nft_stats *stats;
1621 int err;
1622
8cb08174
JB
1623 err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr,
1624 nft_counter_policy, NULL);
0ca743a5 1625 if (err < 0)
ff3cd7b3 1626 return ERR_PTR(err);
0ca743a5
PNA
1627
1628 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
ff3cd7b3 1629 return ERR_PTR(-EINVAL);
0ca743a5 1630
ce355e20 1631 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
0ca743a5 1632 if (newstats == NULL)
ff3cd7b3 1633 return ERR_PTR(-ENOMEM);
0ca743a5
PNA
1634
1635 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1636 * are not exposed to userspace.
1637 */
e8781f70 1638 preempt_disable();
0ca743a5
PNA
1639 stats = this_cpu_ptr(newstats);
1640 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1641 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
e8781f70 1642 preempt_enable();
0ca743a5 1643
ff3cd7b3
PNA
1644 return newstats;
1645}
1646
53315ac6 1647static void nft_chain_stats_replace(struct nft_trans *trans)
ff3cd7b3 1648{
53315ac6 1649 struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain);
0befd061 1650
53315ac6 1651 if (!nft_trans_chain_stats(trans))
b88825de
PNA
1652 return;
1653
b685b534
PM
1654 nft_trans_chain_stats(trans) =
1655 rcu_replace_pointer(chain->stats, nft_trans_chain_stats(trans),
1656 lockdep_commit_lock_is_held(trans->ctx.net));
53315ac6
FW
1657
1658 if (!nft_trans_chain_stats(trans))
bbb8c61f 1659 static_branch_inc(&nft_counters_enabled);
0ca743a5
PNA
1660}
1661
0cbc06b3
FW
1662static void nf_tables_chain_free_chain_rules(struct nft_chain *chain)
1663{
1664 struct nft_rule **g0 = rcu_dereference_raw(chain->rules_gen_0);
1665 struct nft_rule **g1 = rcu_dereference_raw(chain->rules_gen_1);
1666
1667 if (g0 != g1)
1668 kvfree(g1);
1669 kvfree(g0);
1670
1671 /* should be NULL either via abort or via successful commit */
1672 WARN_ON_ONCE(chain->rules_next);
1673 kvfree(chain->rules_next);
1674}
1675
d0e2c7de 1676void nf_tables_chain_destroy(struct nft_ctx *ctx)
91c7b38d 1677{
43a605f2 1678 struct nft_chain *chain = ctx->chain;
d54725cd 1679 struct nft_hook *hook, *next;
43a605f2 1680
fa5950e4
FW
1681 if (WARN_ON(chain->use > 0))
1682 return;
91c7b38d 1683
0cbc06b3
FW
1684 /* no concurrent access possible anymore */
1685 nf_tables_chain_free_chain_rules(chain);
1686
f323d954 1687 if (nft_is_base_chain(chain)) {
2cbce139
PNA
1688 struct nft_base_chain *basechain = nft_base_chain(chain);
1689
d3519cb8 1690 if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) {
d54725cd
PNA
1691 list_for_each_entry_safe(hook, next,
1692 &basechain->hook_list, list) {
1693 list_del_rcu(&hook->list);
1694 kfree_rcu(hook, rcu);
1695 }
1696 }
2cbce139 1697 module_put(basechain->type->owner);
4c05ec47 1698 if (rcu_access_pointer(basechain->stats)) {
9f08ea84 1699 static_branch_dec(&nft_counters_enabled);
4c05ec47
TY
1700 free_percpu(rcu_dereference_raw(basechain->stats));
1701 }
b7263e07 1702 kfree(chain->name);
002f2176 1703 kfree(chain->udata);
2cbce139 1704 kfree(basechain);
91c7b38d 1705 } else {
b7263e07 1706 kfree(chain->name);
002f2176 1707 kfree(chain->udata);
91c7b38d
PNA
1708 kfree(chain);
1709 }
1710}
1711
3f0465a9
PNA
1712static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
1713 const struct nlattr *attr)
1714{
1715 struct net_device *dev;
1716 char ifname[IFNAMSIZ];
1717 struct nft_hook *hook;
1718 int err;
1719
1720 hook = kmalloc(sizeof(struct nft_hook), GFP_KERNEL);
1721 if (!hook) {
1722 err = -ENOMEM;
1723 goto err_hook_alloc;
1724 }
1725
872f6903 1726 nla_strscpy(ifname, attr, IFNAMSIZ);
42f1c271
PNA
1727 /* nf_tables_netdev_event() is called under rtnl_mutex, this is
1728 * indirectly serializing all the other holders of the commit_mutex with
1729 * the rtnl_mutex.
1730 */
3f0465a9
PNA
1731 dev = __dev_get_by_name(net, ifname);
1732 if (!dev) {
1733 err = -ENOENT;
1734 goto err_hook_dev;
1735 }
1736 hook->ops.dev = dev;
abadb2f8 1737 hook->inactive = false;
3f0465a9
PNA
1738
1739 return hook;
1740
1741err_hook_dev:
1742 kfree(hook);
1743err_hook_alloc:
1744 return ERR_PTR(err);
1745}
1746
abadb2f8
PNA
1747static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,
1748 const struct nft_hook *this)
b75a3e83
PNA
1749{
1750 struct nft_hook *hook;
1751
1752 list_for_each_entry(hook, hook_list, list) {
1753 if (this->ops.dev == hook->ops.dev)
abadb2f8 1754 return hook;
b75a3e83
PNA
1755 }
1756
abadb2f8 1757 return NULL;
b75a3e83
PNA
1758}
1759
3f0465a9
PNA
1760static int nf_tables_parse_netdev_hooks(struct net *net,
1761 const struct nlattr *attr,
1762 struct list_head *hook_list)
1763{
1764 struct nft_hook *hook, *next;
1765 const struct nlattr *tmp;
1766 int rem, n = 0, err;
1767
1768 nla_for_each_nested(tmp, attr, rem) {
1769 if (nla_type(tmp) != NFTA_DEVICE_NAME) {
1770 err = -EINVAL;
1771 goto err_hook;
1772 }
1773
1774 hook = nft_netdev_hook_alloc(net, tmp);
1775 if (IS_ERR(hook)) {
1776 err = PTR_ERR(hook);
1777 goto err_hook;
1778 }
b75a3e83 1779 if (nft_hook_list_find(hook_list, hook)) {
cd77e75b 1780 kfree(hook);
b75a3e83
PNA
1781 err = -EEXIST;
1782 goto err_hook;
1783 }
3f0465a9
PNA
1784 list_add_tail(&hook->list, hook_list);
1785 n++;
1786
cb662ac6 1787 if (n == NFT_NETDEVICE_MAX) {
3f0465a9
PNA
1788 err = -EFBIG;
1789 goto err_hook;
1790 }
1791 }
3f0465a9
PNA
1792
1793 return 0;
1794
1795err_hook:
1796 list_for_each_entry_safe(hook, next, hook_list, list) {
1797 list_del(&hook->list);
1798 kfree(hook);
1799 }
1800 return err;
1801}
1802
508f8ccd
PNA
1803struct nft_chain_hook {
1804 u32 num;
84ba7dd7 1805 s32 priority;
32537e91 1806 const struct nft_chain_type *type;
d54725cd 1807 struct list_head list;
508f8ccd
PNA
1808};
1809
d54725cd
PNA
1810static int nft_chain_parse_netdev(struct net *net,
1811 struct nlattr *tb[],
1812 struct list_head *hook_list)
1813{
1814 struct nft_hook *hook;
1815 int err;
1816
1817 if (tb[NFTA_HOOK_DEV]) {
1818 hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]);
1819 if (IS_ERR(hook))
1820 return PTR_ERR(hook);
1821
1822 list_add_tail(&hook->list, hook_list);
1823 } else if (tb[NFTA_HOOK_DEVS]) {
1824 err = nf_tables_parse_netdev_hooks(net, tb[NFTA_HOOK_DEVS],
1825 hook_list);
1826 if (err < 0)
1827 return err;
05abe445
PNA
1828
1829 if (list_empty(hook_list))
1830 return -EINVAL;
d54725cd
PNA
1831 } else {
1832 return -EINVAL;
1833 }
1834
1835 return 0;
1836}
1837
508f8ccd
PNA
1838static int nft_chain_parse_hook(struct net *net,
1839 const struct nlattr * const nla[],
36596dad 1840 struct nft_chain_hook *hook, u8 family,
445509eb 1841 bool autoload)
508f8ccd
PNA
1842{
1843 struct nlattr *ha[NFTA_HOOK_MAX + 1];
32537e91 1844 const struct nft_chain_type *type;
508f8ccd
PNA
1845 int err;
1846
f102d66b
FW
1847 lockdep_assert_held(&net->nft.commit_mutex);
1848 lockdep_nfnl_nft_mutex_not_held();
1849
8cb08174
JB
1850 err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX,
1851 nla[NFTA_CHAIN_HOOK],
1852 nft_hook_policy, NULL);
508f8ccd
PNA
1853 if (err < 0)
1854 return err;
1855
1856 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1857 ha[NFTA_HOOK_PRIORITY] == NULL)
1858 return -EINVAL;
1859
1860 hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
508f8ccd
PNA
1861 hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
1862
82603549
PNA
1863 type = __nft_chain_type_get(family, NFT_CHAIN_T_DEFAULT);
1864 if (!type)
1865 return -EOPNOTSUPP;
1866
508f8ccd 1867 if (nla[NFTA_CHAIN_TYPE]) {
452238e8 1868 type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE],
445509eb 1869 family, autoload);
508f8ccd
PNA
1870 if (IS_ERR(type))
1871 return PTR_ERR(type);
1872 }
d25e2e93 1873 if (hook->num >= NFT_MAX_HOOKS || !(type->hook_mask & (1 << hook->num)))
508f8ccd 1874 return -EOPNOTSUPP;
84ba7dd7
FW
1875
1876 if (type->type == NFT_CHAIN_T_NAT &&
1877 hook->priority <= NF_IP_PRI_CONNTRACK)
1878 return -EOPNOTSUPP;
1879
508f8ccd
PNA
1880 if (!try_module_get(type->owner))
1881 return -ENOENT;
1882
1883 hook->type = type;
1884
d54725cd 1885 INIT_LIST_HEAD(&hook->list);
d3519cb8 1886 if (nft_base_chain_netdev(family, hook->num)) {
d54725cd
PNA
1887 err = nft_chain_parse_netdev(net, ha, &hook->list);
1888 if (err < 0) {
508f8ccd 1889 module_put(type->owner);
d54725cd 1890 return err;
508f8ccd 1891 }
d54725cd 1892 } else if (ha[NFTA_HOOK_DEV] || ha[NFTA_HOOK_DEVS]) {
508f8ccd
PNA
1893 module_put(type->owner);
1894 return -EOPNOTSUPP;
1895 }
1896
1897 return 0;
1898}
1899
1900static void nft_chain_release_hook(struct nft_chain_hook *hook)
1901{
d54725cd
PNA
1902 struct nft_hook *h, *next;
1903
1904 list_for_each_entry_safe(h, next, &hook->list, list) {
1905 list_del(&h->list);
1906 kfree(h);
1907 }
508f8ccd 1908 module_put(hook->type->owner);
508f8ccd
PNA
1909}
1910
0cbc06b3
FW
1911struct nft_rules_old {
1912 struct rcu_head h;
1913 struct nft_rule **start;
1914};
1915
1916static struct nft_rule **nf_tables_chain_alloc_rules(const struct nft_chain *chain,
1917 unsigned int alloc)
1918{
1919 if (alloc > INT_MAX)
1920 return NULL;
1921
1922 alloc += 1; /* NULL, ends rules */
1923 if (sizeof(struct nft_rule *) > INT_MAX / alloc)
1924 return NULL;
1925
1926 alloc *= sizeof(struct nft_rule *);
1927 alloc += sizeof(struct nft_rules_old);
1928
1929 return kvmalloc(alloc, GFP_KERNEL);
1930}
1931
d54725cd
PNA
1932static void nft_basechain_hook_init(struct nf_hook_ops *ops, u8 family,
1933 const struct nft_chain_hook *hook,
1934 struct nft_chain *chain)
1935{
1936 ops->pf = family;
1937 ops->hooknum = hook->num;
1938 ops->priority = hook->priority;
1939 ops->priv = chain;
1940 ops->hook = hook->type->hooks[ops->hooknum];
1941}
1942
1943static int nft_basechain_init(struct nft_base_chain *basechain, u8 family,
1944 struct nft_chain_hook *hook, u32 flags)
1945{
1946 struct nft_chain *chain;
1947 struct nft_hook *h;
1948
1949 basechain->type = hook->type;
1950 INIT_LIST_HEAD(&basechain->hook_list);
1951 chain = &basechain->chain;
1952
d3519cb8 1953 if (nft_base_chain_netdev(family, hook->num)) {
d54725cd
PNA
1954 list_splice_init(&hook->list, &basechain->hook_list);
1955 list_for_each_entry(h, &basechain->hook_list, list)
1956 nft_basechain_hook_init(&h->ops, family, hook, chain);
1957
1958 basechain->ops.hooknum = hook->num;
1959 basechain->ops.priority = hook->priority;
1960 } else {
1961 nft_basechain_hook_init(&basechain->ops, family, hook, chain);
1962 }
1963
67c49de4 1964 chain->flags |= NFT_CHAIN_BASE | flags;
d54725cd
PNA
1965 basechain->policy = NF_ACCEPT;
1966 if (chain->flags & NFT_CHAIN_HW_OFFLOAD &&
1967 nft_chain_offload_priority(basechain) < 0)
1968 return -EOPNOTSUPP;
1969
1970 flow_block_init(&basechain->flow_block);
1971
1972 return 0;
1973}
1974
04b7db41
PNA
1975static int nft_chain_add(struct nft_table *table, struct nft_chain *chain)
1976{
1977 int err;
1978
1979 err = rhltable_insert_key(&table->chains_ht, chain->name,
1980 &chain->rhlhead, nft_chain_ht_params);
1981 if (err)
1982 return err;
1983
1984 list_add_tail_rcu(&chain->list, &table->chains);
1985
1986 return 0;
1987}
1988
d0e2c7de
PNA
1989static u64 chain_id;
1990
4035285f 1991static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
c9626a2c 1992 u8 policy, u32 flags)
4035285f
PNA
1993{
1994 const struct nlattr * const *nla = ctx->nla;
1995 struct nft_table *table = ctx->table;
4035285f
PNA
1996 struct nft_base_chain *basechain;
1997 struct nft_stats __percpu *stats;
1998 struct net *net = ctx->net;
d0e2c7de 1999 char name[NFT_NAME_MAXLEN];
66293c46 2000 struct nft_trans *trans;
4035285f 2001 struct nft_chain *chain;
0cbc06b3 2002 struct nft_rule **rules;
4035285f
PNA
2003 int err;
2004
2005 if (table->use == UINT_MAX)
2006 return -EOVERFLOW;
2007
2008 if (nla[NFTA_CHAIN_HOOK]) {
2009 struct nft_chain_hook hook;
4035285f 2010
d0e2c7de
PNA
2011 if (flags & NFT_CHAIN_BINDING)
2012 return -EOPNOTSUPP;
2013
445509eb 2014 err = nft_chain_parse_hook(net, nla, &hook, family, true);
4035285f
PNA
2015 if (err < 0)
2016 return err;
2017
2018 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
2019 if (basechain == NULL) {
2020 nft_chain_release_hook(&hook);
2021 return -ENOMEM;
2022 }
d54725cd 2023 chain = &basechain->chain;
4035285f
PNA
2024
2025 if (nla[NFTA_CHAIN_COUNTERS]) {
2026 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
2027 if (IS_ERR(stats)) {
2028 nft_chain_release_hook(&hook);
2029 kfree(basechain);
2030 return PTR_ERR(stats);
2031 }
4c05ec47 2032 rcu_assign_pointer(basechain->stats, stats);
4035285f
PNA
2033 static_branch_inc(&nft_counters_enabled);
2034 }
2035
d54725cd
PNA
2036 err = nft_basechain_init(basechain, family, &hook, flags);
2037 if (err < 0) {
2038 nft_chain_release_hook(&hook);
2039 kfree(basechain);
2040 return err;
2041 }
4035285f 2042 } else {
d0e2c7de
PNA
2043 if (flags & NFT_CHAIN_BASE)
2044 return -EINVAL;
2045 if (flags & NFT_CHAIN_HW_OFFLOAD)
2046 return -EOPNOTSUPP;
2047
4035285f
PNA
2048 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
2049 if (chain == NULL)
2050 return -ENOMEM;
d0e2c7de
PNA
2051
2052 chain->flags = flags;
4035285f 2053 }
43a605f2
PNA
2054 ctx->chain = chain;
2055
4035285f
PNA
2056 INIT_LIST_HEAD(&chain->rules);
2057 chain->handle = nf_tables_alloc_handle(table);
2058 chain->table = table;
d0e2c7de
PNA
2059
2060 if (nla[NFTA_CHAIN_NAME]) {
2061 chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL);
2062 } else {
59136aa3
FW
2063 if (!(flags & NFT_CHAIN_BINDING)) {
2064 err = -EINVAL;
002f2176 2065 goto err_destroy_chain;
59136aa3 2066 }
d0e2c7de
PNA
2067
2068 snprintf(name, sizeof(name), "__chain%llu", ++chain_id);
2069 chain->name = kstrdup(name, GFP_KERNEL);
2070 }
2071
4035285f
PNA
2072 if (!chain->name) {
2073 err = -ENOMEM;
002f2176
JGG
2074 goto err_destroy_chain;
2075 }
2076
2077 if (nla[NFTA_CHAIN_USERDATA]) {
2078 chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL);
2079 if (chain->udata == NULL) {
2080 err = -ENOMEM;
2081 goto err_destroy_chain;
2082 }
2083 chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]);
4035285f
PNA
2084 }
2085
0cbc06b3
FW
2086 rules = nf_tables_chain_alloc_rules(chain, 0);
2087 if (!rules) {
2088 err = -ENOMEM;
002f2176 2089 goto err_destroy_chain;
0cbc06b3
FW
2090 }
2091
2092 *rules = NULL;
2093 rcu_assign_pointer(chain->rules_gen_0, rules);
2094 rcu_assign_pointer(chain->rules_gen_1, rules);
2095
c974a3a3 2096 err = nf_tables_register_hook(net, table, chain);
4035285f 2097 if (err < 0)
002f2176 2098 goto err_destroy_chain;
4035285f 2099
66293c46
FW
2100 trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN);
2101 if (IS_ERR(trans)) {
2102 err = PTR_ERR(trans);
002f2176 2103 goto err_unregister_hook;
1b2470e5 2104 }
4035285f 2105
ad652f38 2106 nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET;
66293c46
FW
2107 if (nft_is_base_chain(chain))
2108 nft_trans_chain_policy(trans) = policy;
2109
04b7db41
PNA
2110 err = nft_chain_add(table, chain);
2111 if (err < 0) {
2112 nft_trans_destroy(trans);
002f2176 2113 goto err_unregister_hook;
04b7db41
PNA
2114 }
2115
4035285f 2116 table->use++;
4035285f
PNA
2117
2118 return 0;
002f2176 2119err_unregister_hook:
c974a3a3 2120 nf_tables_unregister_hook(net, table, chain);
002f2176 2121err_destroy_chain:
43a605f2 2122 nf_tables_chain_destroy(ctx);
4035285f
PNA
2123
2124 return err;
2125}
2126
d54725cd
PNA
2127static bool nft_hook_list_equal(struct list_head *hook_list1,
2128 struct list_head *hook_list2)
2129{
2130 struct nft_hook *hook;
2131 int n = 0, m = 0;
2132
2133 n = 0;
2134 list_for_each_entry(hook, hook_list2, list) {
2135 if (!nft_hook_list_find(hook_list1, hook))
2136 return false;
2137
2138 n++;
2139 }
2140 list_for_each_entry(hook, hook_list1, list)
2141 m++;
2142
2143 return n == m;
2144}
2145
c9626a2c 2146static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy,
98a381a7
PNA
2147 u32 flags, const struct nlattr *attr,
2148 struct netlink_ext_ack *extack)
2c4a488a
PNA
2149{
2150 const struct nlattr * const *nla = ctx->nla;
2151 struct nft_table *table = ctx->table;
2152 struct nft_chain *chain = ctx->chain;
2c4a488a
PNA
2153 struct nft_base_chain *basechain;
2154 struct nft_stats *stats = NULL;
2155 struct nft_chain_hook hook;
2c4a488a
PNA
2156 struct nf_hook_ops *ops;
2157 struct nft_trans *trans;
c974a3a3 2158 int err;
2c4a488a 2159
c9626a2c
PNA
2160 if (chain->flags ^ flags)
2161 return -EOPNOTSUPP;
2162
2c4a488a 2163 if (nla[NFTA_CHAIN_HOOK]) {
98a381a7
PNA
2164 if (!nft_is_base_chain(chain)) {
2165 NL_SET_BAD_ATTR(extack, attr);
77a92189 2166 return -EEXIST;
98a381a7 2167 }
36596dad 2168 err = nft_chain_parse_hook(ctx->net, nla, &hook, ctx->family,
445509eb 2169 false);
2c4a488a
PNA
2170 if (err < 0)
2171 return err;
2172
2173 basechain = nft_base_chain(chain);
2174 if (basechain->type != hook.type) {
2175 nft_chain_release_hook(&hook);
98a381a7 2176 NL_SET_BAD_ATTR(extack, attr);
77a92189 2177 return -EEXIST;
2c4a488a
PNA
2178 }
2179
d3519cb8 2180 if (nft_base_chain_netdev(ctx->family, hook.num)) {
d54725cd
PNA
2181 if (!nft_hook_list_equal(&basechain->hook_list,
2182 &hook.list)) {
2183 nft_chain_release_hook(&hook);
98a381a7 2184 NL_SET_BAD_ATTR(extack, attr);
77a92189 2185 return -EEXIST;
d54725cd
PNA
2186 }
2187 } else {
2188 ops = &basechain->ops;
2189 if (ops->hooknum != hook.num ||
2190 ops->priority != hook.priority) {
2191 nft_chain_release_hook(&hook);
98a381a7 2192 NL_SET_BAD_ATTR(extack, attr);
77a92189 2193 return -EEXIST;
d54725cd 2194 }
2c4a488a
PNA
2195 }
2196 nft_chain_release_hook(&hook);
2197 }
2198
2199 if (nla[NFTA_CHAIN_HANDLE] &&
2200 nla[NFTA_CHAIN_NAME]) {
2201 struct nft_chain *chain2;
2202
f102d66b
FW
2203 chain2 = nft_chain_lookup(ctx->net, table,
2204 nla[NFTA_CHAIN_NAME], genmask);
98a381a7
PNA
2205 if (!IS_ERR(chain2)) {
2206 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
0d18779b 2207 return -EEXIST;
98a381a7 2208 }
2c4a488a
PNA
2209 }
2210
2211 if (nla[NFTA_CHAIN_COUNTERS]) {
2212 if (!nft_is_base_chain(chain))
2213 return -EOPNOTSUPP;
2214
2215 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
2216 if (IS_ERR(stats))
2217 return PTR_ERR(stats);
2218 }
2219
c6cc94df 2220 err = -ENOMEM;
2c4a488a
PNA
2221 trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN,
2222 sizeof(struct nft_trans_chain));
c6cc94df
FW
2223 if (trans == NULL)
2224 goto err;
2c4a488a
PNA
2225
2226 nft_trans_chain_stats(trans) = stats;
2227 nft_trans_chain_update(trans) = true;
2228
2229 if (nla[NFTA_CHAIN_POLICY])
2230 nft_trans_chain_policy(trans) = policy;
2231 else
2232 nft_trans_chain_policy(trans) = -1;
2233
c6cc94df
FW
2234 if (nla[NFTA_CHAIN_HANDLE] &&
2235 nla[NFTA_CHAIN_NAME]) {
2236 struct nft_trans *tmp;
2237 char *name;
2238
2239 err = -ENOMEM;
2240 name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL);
2241 if (!name)
2242 goto err;
2243
2244 err = -EEXIST;
2245 list_for_each_entry(tmp, &ctx->net->nft.commit_list, list) {
2246 if (tmp->msg_type == NFT_MSG_NEWCHAIN &&
2247 tmp->ctx.table == table &&
2248 nft_trans_chain_update(tmp) &&
2249 nft_trans_chain_name(tmp) &&
2250 strcmp(name, nft_trans_chain_name(tmp)) == 0) {
98a381a7 2251 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
c6cc94df
FW
2252 kfree(name);
2253 goto err;
2254 }
2c4a488a 2255 }
c6cc94df
FW
2256
2257 nft_trans_chain_name(trans) = name;
2c4a488a
PNA
2258 }
2259 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
2260
2261 return 0;
c6cc94df
FW
2262err:
2263 free_percpu(stats);
2264 kfree(trans);
2265 return err;
2c4a488a
PNA
2266}
2267
837830a4
PNA
2268static struct nft_chain *nft_chain_lookup_byid(const struct net *net,
2269 const struct nlattr *nla)
2270{
2271 u32 id = ntohl(nla_get_be32(nla));
2272 struct nft_trans *trans;
2273
2274 list_for_each_entry(trans, &net->nft.commit_list, list) {
2275 struct nft_chain *chain = trans->ctx.chain;
2276
2277 if (trans->msg_type == NFT_MSG_NEWCHAIN &&
2278 id == nft_trans_chain_id(trans))
2279 return chain;
2280 }
2281 return ERR_PTR(-ENOENT);
2282}
2283
633c9a84
PNA
2284static int nf_tables_newchain(struct net *net, struct sock *nlsk,
2285 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
2286 const struct nlattr * const nla[],
2287 struct netlink_ext_ack *extack)
96518518
PM
2288{
2289 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
4035285f
PNA
2290 u8 genmask = nft_genmask_next(net);
2291 int family = nfmsg->nfgen_family;
74cccc3d 2292 struct nft_chain *chain = NULL;
36dd1bcc 2293 const struct nlattr *attr;
96518518 2294 struct nft_table *table;
57de2a0c 2295 u8 policy = NF_ACCEPT;
4035285f 2296 struct nft_ctx ctx;
96518518 2297 u64 handle = 0;
c9626a2c 2298 u32 flags = 0;
96518518 2299
f102d66b
FW
2300 lockdep_assert_held(&net->nft.commit_mutex);
2301
cac20fcd 2302 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask);
36dd1bcc
PNA
2303 if (IS_ERR(table)) {
2304 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
96518518 2305 return PTR_ERR(table);
36dd1bcc 2306 }
96518518 2307
96518518 2308 chain = NULL;
36dd1bcc 2309 attr = nla[NFTA_CHAIN_NAME];
96518518
PM
2310
2311 if (nla[NFTA_CHAIN_HANDLE]) {
2312 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
cac20fcd 2313 chain = nft_chain_lookup_byhandle(table, handle, genmask);
36dd1bcc
PNA
2314 if (IS_ERR(chain)) {
2315 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]);
96518518 2316 return PTR_ERR(chain);
36dd1bcc
PNA
2317 }
2318 attr = nla[NFTA_CHAIN_HANDLE];
74cccc3d 2319 } else if (nla[NFTA_CHAIN_NAME]) {
f102d66b 2320 chain = nft_chain_lookup(net, table, attr, genmask);
96518518 2321 if (IS_ERR(chain)) {
36dd1bcc
PNA
2322 if (PTR_ERR(chain) != -ENOENT) {
2323 NL_SET_BAD_ATTR(extack, attr);
96518518 2324 return PTR_ERR(chain);
36dd1bcc 2325 }
96518518
PM
2326 chain = NULL;
2327 }
74cccc3d
PNA
2328 } else if (!nla[NFTA_CHAIN_ID]) {
2329 return -EINVAL;
96518518
PM
2330 }
2331
57de2a0c 2332 if (nla[NFTA_CHAIN_POLICY]) {
f323d954 2333 if (chain != NULL &&
36dd1bcc
PNA
2334 !nft_is_base_chain(chain)) {
2335 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
d6b6cb1d 2336 return -EOPNOTSUPP;
36dd1bcc 2337 }
d6b6cb1d
PNA
2338
2339 if (chain == NULL &&
36dd1bcc
PNA
2340 nla[NFTA_CHAIN_HOOK] == NULL) {
2341 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
57de2a0c 2342 return -EOPNOTSUPP;
36dd1bcc 2343 }
57de2a0c 2344
8f46df18 2345 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
57de2a0c
PM
2346 switch (policy) {
2347 case NF_DROP:
2348 case NF_ACCEPT:
2349 break;
2350 default:
2351 return -EINVAL;
2352 }
2353 }
2354
c9626a2c
PNA
2355 if (nla[NFTA_CHAIN_FLAGS])
2356 flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS]));
b717273d
FW
2357 else if (chain)
2358 flags = chain->flags;
c9626a2c 2359
c1f79a2e
PNA
2360 if (flags & ~NFT_CHAIN_FLAGS)
2361 return -EOPNOTSUPP;
2362
98319cb9 2363 nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
4035285f 2364
96518518 2365 if (chain != NULL) {
36dd1bcc
PNA
2366 if (nlh->nlmsg_flags & NLM_F_EXCL) {
2367 NL_SET_BAD_ATTR(extack, attr);
96518518 2368 return -EEXIST;
36dd1bcc 2369 }
96518518
PM
2370 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2371 return -EOPNOTSUPP;
2372
67c49de4 2373 flags |= chain->flags & NFT_CHAIN_BASE;
98a381a7
PNA
2374 return nf_tables_updchain(&ctx, genmask, policy, flags, attr,
2375 extack);
96518518
PM
2376 }
2377
c9626a2c 2378 return nf_tables_addchain(&ctx, family, genmask, policy, flags);
96518518
PM
2379}
2380
633c9a84
PNA
2381static int nf_tables_delchain(struct net *net, struct sock *nlsk,
2382 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
2383 const struct nlattr * const nla[],
2384 struct netlink_ext_ack *extack)
96518518
PM
2385{
2386 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 2387 u8 genmask = nft_genmask_next(net);
36dd1bcc
PNA
2388 int family = nfmsg->nfgen_family;
2389 const struct nlattr *attr;
96518518
PM
2390 struct nft_table *table;
2391 struct nft_chain *chain;
9dee1474 2392 struct nft_rule *rule;
91c7b38d 2393 struct nft_ctx ctx;
3ecbfd65 2394 u64 handle;
9dee1474
PNA
2395 u32 use;
2396 int err;
96518518 2397
cac20fcd 2398 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask);
36dd1bcc
PNA
2399 if (IS_ERR(table)) {
2400 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
96518518 2401 return PTR_ERR(table);
36dd1bcc 2402 }
96518518 2403
3ecbfd65 2404 if (nla[NFTA_CHAIN_HANDLE]) {
36dd1bcc
PNA
2405 attr = nla[NFTA_CHAIN_HANDLE];
2406 handle = be64_to_cpu(nla_get_be64(attr));
cac20fcd 2407 chain = nft_chain_lookup_byhandle(table, handle, genmask);
3ecbfd65 2408 } else {
36dd1bcc 2409 attr = nla[NFTA_CHAIN_NAME];
f102d66b 2410 chain = nft_chain_lookup(net, table, attr, genmask);
3ecbfd65 2411 }
36dd1bcc
PNA
2412 if (IS_ERR(chain)) {
2413 NL_SET_BAD_ATTR(extack, attr);
96518518 2414 return PTR_ERR(chain);
36dd1bcc 2415 }
9dee1474
PNA
2416
2417 if (nlh->nlmsg_flags & NLM_F_NONREC &&
2418 chain->use > 0)
96518518
PM
2419 return -EBUSY;
2420
98319cb9 2421 nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
0165d932 2422
9dee1474
PNA
2423 use = chain->use;
2424 list_for_each_entry(rule, &chain->rules, list) {
2425 if (!nft_is_active_next(net, rule))
2426 continue;
2427 use--;
2428
2429 err = nft_delrule(&ctx, rule);
2430 if (err < 0)
2431 return err;
2432 }
2433
2434 /* There are rules and elements that are still holding references to us,
2435 * we cannot do a recursive removal in this case.
2436 */
36dd1bcc
PNA
2437 if (use > 0) {
2438 NL_SET_BAD_ATTR(extack, attr);
9dee1474 2439 return -EBUSY;
36dd1bcc 2440 }
9dee1474 2441
ee01d542 2442 return nft_delchain(&ctx);
96518518
PM
2443}
2444
96518518
PM
2445/*
2446 * Expressions
2447 */
2448
2449/**
ef1f7df9 2450 * nft_register_expr - register nf_tables expr type
3db86c39 2451 * @type: expr type
96518518 2452 *
ef1f7df9 2453 * Registers the expr type for use with nf_tables. Returns zero on
96518518
PM
2454 * success or a negative errno code otherwise.
2455 */
ef1f7df9 2456int nft_register_expr(struct nft_expr_type *type)
96518518
PM
2457{
2458 nfnl_lock(NFNL_SUBSYS_NFTABLES);
758dbcec 2459 if (type->family == NFPROTO_UNSPEC)
e688a7f8 2460 list_add_tail_rcu(&type->list, &nf_tables_expressions);
758dbcec 2461 else
e688a7f8 2462 list_add_rcu(&type->list, &nf_tables_expressions);
96518518
PM
2463 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2464 return 0;
2465}
2466EXPORT_SYMBOL_GPL(nft_register_expr);
2467
2468/**
ef1f7df9 2469 * nft_unregister_expr - unregister nf_tables expr type
3db86c39 2470 * @type: expr type
96518518 2471 *
ef1f7df9 2472 * Unregisters the expr typefor use with nf_tables.
96518518 2473 */
ef1f7df9 2474void nft_unregister_expr(struct nft_expr_type *type)
96518518
PM
2475{
2476 nfnl_lock(NFNL_SUBSYS_NFTABLES);
e688a7f8 2477 list_del_rcu(&type->list);
96518518
PM
2478 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2479}
2480EXPORT_SYMBOL_GPL(nft_unregister_expr);
2481
64d46806
PM
2482static const struct nft_expr_type *__nft_expr_type_get(u8 family,
2483 struct nlattr *nla)
96518518 2484{
9cff126f 2485 const struct nft_expr_type *type, *candidate = NULL;
96518518 2486
ef1f7df9 2487 list_for_each_entry(type, &nf_tables_expressions, list) {
9cff126f
PNA
2488 if (!nla_strcmp(nla, type->name)) {
2489 if (!type->family && !candidate)
2490 candidate = type;
2491 else if (type->family == family)
2492 candidate = type;
2493 }
96518518 2494 }
9cff126f 2495 return candidate;
96518518
PM
2496}
2497
b9c04ae7
PNA
2498#ifdef CONFIG_MODULES
2499static int nft_expr_type_request_module(struct net *net, u8 family,
2500 struct nlattr *nla)
2501{
eb014de4
PNA
2502 if (nft_request_module(net, "nft-expr-%u-%.*s", family,
2503 nla_len(nla), (char *)nla_data(nla)) == -EAGAIN)
b9c04ae7
PNA
2504 return -EAGAIN;
2505
2506 return 0;
2507}
2508#endif
2509
452238e8
FW
2510static const struct nft_expr_type *nft_expr_type_get(struct net *net,
2511 u8 family,
64d46806 2512 struct nlattr *nla)
96518518 2513{
ef1f7df9 2514 const struct nft_expr_type *type;
96518518
PM
2515
2516 if (nla == NULL)
2517 return ERR_PTR(-EINVAL);
2518
64d46806 2519 type = __nft_expr_type_get(family, nla);
ef1f7df9
PM
2520 if (type != NULL && try_module_get(type->owner))
2521 return type;
96518518 2522
f102d66b 2523 lockdep_nfnl_nft_mutex_not_held();
96518518 2524#ifdef CONFIG_MODULES
ef1f7df9 2525 if (type == NULL) {
b9c04ae7 2526 if (nft_expr_type_request_module(net, family, nla) == -EAGAIN)
64d46806
PM
2527 return ERR_PTR(-EAGAIN);
2528
eb014de4
PNA
2529 if (nft_request_module(net, "nft-expr-%.*s",
2530 nla_len(nla),
2531 (char *)nla_data(nla)) == -EAGAIN)
96518518
PM
2532 return ERR_PTR(-EAGAIN);
2533 }
2534#endif
2535 return ERR_PTR(-ENOENT);
2536}
2537
2538static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
9332d27d
FW
2539 [NFTA_EXPR_NAME] = { .type = NLA_STRING,
2540 .len = NFT_MODULE_AUTOLOAD_LIMIT },
96518518
PM
2541 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
2542};
2543
2544static int nf_tables_fill_expr_info(struct sk_buff *skb,
2545 const struct nft_expr *expr)
2546{
ef1f7df9 2547 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
96518518
PM
2548 goto nla_put_failure;
2549
2550 if (expr->ops->dump) {
ae0be8de
MK
2551 struct nlattr *data = nla_nest_start_noflag(skb,
2552 NFTA_EXPR_DATA);
96518518
PM
2553 if (data == NULL)
2554 goto nla_put_failure;
2555 if (expr->ops->dump(skb, expr) < 0)
2556 goto nla_put_failure;
2557 nla_nest_end(skb, data);
2558 }
2559
2560 return skb->len;
2561
2562nla_put_failure:
2563 return -1;
2564};
2565
0b2d8a7b
PM
2566int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
2567 const struct nft_expr *expr)
2568{
2569 struct nlattr *nest;
2570
ae0be8de 2571 nest = nla_nest_start_noflag(skb, attr);
0b2d8a7b
PM
2572 if (!nest)
2573 goto nla_put_failure;
2574 if (nf_tables_fill_expr_info(skb, expr) < 0)
2575 goto nla_put_failure;
2576 nla_nest_end(skb, nest);
2577 return 0;
2578
2579nla_put_failure:
2580 return -1;
2581}
2582
96518518
PM
2583struct nft_expr_info {
2584 const struct nft_expr_ops *ops;
83d9dcba 2585 const struct nlattr *attr;
ef1f7df9 2586 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
96518518
PM
2587};
2588
0ca743a5
PNA
2589static int nf_tables_expr_parse(const struct nft_ctx *ctx,
2590 const struct nlattr *nla,
96518518
PM
2591 struct nft_expr_info *info)
2592{
ef1f7df9 2593 const struct nft_expr_type *type;
96518518 2594 const struct nft_expr_ops *ops;
ef1f7df9 2595 struct nlattr *tb[NFTA_EXPR_MAX + 1];
96518518
PM
2596 int err;
2597
8cb08174
JB
2598 err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla,
2599 nft_expr_policy, NULL);
96518518
PM
2600 if (err < 0)
2601 return err;
2602
452238e8 2603 type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]);
ef1f7df9
PM
2604 if (IS_ERR(type))
2605 return PTR_ERR(type);
2606
2607 if (tb[NFTA_EXPR_DATA]) {
8cb08174
JB
2608 err = nla_parse_nested_deprecated(info->tb, type->maxattr,
2609 tb[NFTA_EXPR_DATA],
2610 type->policy, NULL);
ef1f7df9
PM
2611 if (err < 0)
2612 goto err1;
2613 } else
2614 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
2615
2616 if (type->select_ops != NULL) {
0ca743a5
PNA
2617 ops = type->select_ops(ctx,
2618 (const struct nlattr * const *)info->tb);
ef1f7df9
PM
2619 if (IS_ERR(ops)) {
2620 err = PTR_ERR(ops);
0ef1efd1
PNA
2621#ifdef CONFIG_MODULES
2622 if (err == -EAGAIN)
eb014de4
PNA
2623 if (nft_expr_type_request_module(ctx->net,
2624 ctx->family,
2625 tb[NFTA_EXPR_NAME]) != -EAGAIN)
2626 err = -ENOENT;
0ef1efd1 2627#endif
ef1f7df9
PM
2628 goto err1;
2629 }
2630 } else
2631 ops = type->ops;
2632
83d9dcba 2633 info->attr = nla;
96518518 2634 info->ops = ops;
83d9dcba 2635
96518518 2636 return 0;
ef1f7df9
PM
2637
2638err1:
2639 module_put(type->owner);
2640 return err;
96518518
PM
2641}
2642
2643static int nf_tables_newexpr(const struct nft_ctx *ctx,
ef1f7df9 2644 const struct nft_expr_info *info,
96518518
PM
2645 struct nft_expr *expr)
2646{
2647 const struct nft_expr_ops *ops = info->ops;
2648 int err;
2649
2650 expr->ops = ops;
2651 if (ops->init) {
ef1f7df9 2652 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
96518518
PM
2653 if (err < 0)
2654 goto err1;
2655 }
2656
96518518 2657 return 0;
96518518
PM
2658err1:
2659 expr->ops = NULL;
2660 return err;
2661}
2662
62472bce
PM
2663static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
2664 struct nft_expr *expr)
96518518 2665{
3f3a390d
PNA
2666 const struct nft_expr_type *type = expr->ops->type;
2667
96518518 2668 if (expr->ops->destroy)
62472bce 2669 expr->ops->destroy(ctx, expr);
3f3a390d 2670 module_put(type->owner);
96518518
PM
2671}
2672
795a6d6b
PNA
2673static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
2674 const struct nlattr *nla)
0b2d8a7b
PM
2675{
2676 struct nft_expr_info info;
2677 struct nft_expr *expr;
b8e20400 2678 struct module *owner;
0b2d8a7b
PM
2679 int err;
2680
2681 err = nf_tables_expr_parse(ctx, nla, &info);
2682 if (err < 0)
2683 goto err1;
2684
2685 err = -ENOMEM;
2686 expr = kzalloc(info.ops->size, GFP_KERNEL);
2687 if (expr == NULL)
2688 goto err2;
2689
2690 err = nf_tables_newexpr(ctx, &info, expr);
2691 if (err < 0)
6cafaf47 2692 goto err3;
0b2d8a7b
PM
2693
2694 return expr;
6cafaf47
LZ
2695err3:
2696 kfree(expr);
0b2d8a7b 2697err2:
b8e20400
PNA
2698 owner = info.ops->type->owner;
2699 if (info.ops->type->release_ops)
2700 info.ops->type->release_ops(info.ops);
2701
2702 module_put(owner);
0b2d8a7b
PM
2703err1:
2704 return ERR_PTR(err);
2705}
2706
c604cc69
PNA
2707int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
2708{
2709 int err;
2710
2711 if (src->ops->clone) {
2712 dst->ops = src->ops;
2713 err = src->ops->clone(dst, src);
2714 if (err < 0)
2715 return err;
2716 } else {
2717 memcpy(dst, src, src->ops->size);
2718 }
2719
2720 __module_get(src->ops->type->owner);
2721
2722 return 0;
2723}
2724
0b2d8a7b
PM
2725void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
2726{
2727 nf_tables_expr_destroy(ctx, expr);
2728 kfree(expr);
2729}
2730
96518518
PM
2731/*
2732 * Rules
2733 */
2734
cac20fcd
PNA
2735static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain,
2736 u64 handle)
96518518
PM
2737{
2738 struct nft_rule *rule;
2739
2740 // FIXME: this sucks
d9adf22a 2741 list_for_each_entry_rcu(rule, &chain->rules, list) {
96518518
PM
2742 if (handle == rule->handle)
2743 return rule;
2744 }
2745
2746 return ERR_PTR(-ENOENT);
2747}
2748
cac20fcd
PNA
2749static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain,
2750 const struct nlattr *nla)
96518518
PM
2751{
2752 if (nla == NULL)
2753 return ERR_PTR(-EINVAL);
2754
cac20fcd 2755 return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
96518518
PM
2756}
2757
2758static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
b2fbd044
LZ
2759 [NFTA_RULE_TABLE] = { .type = NLA_STRING,
2760 .len = NFT_TABLE_MAXNAMELEN - 1 },
96518518
PM
2761 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
2762 .len = NFT_CHAIN_MAXNAMELEN - 1 },
2763 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
2764 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
0ca743a5 2765 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
5e948466 2766 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
0768b3b3
PNA
2767 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
2768 .len = NFT_USERDATA_MAXLEN },
467697d2 2769 [NFTA_RULE_ID] = { .type = NLA_U32 },
0604628b 2770 [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 },
837830a4 2771 [NFTA_RULE_CHAIN_ID] = { .type = NLA_U32 },
96518518
PM
2772};
2773
84d7fce6
PNA
2774static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
2775 u32 portid, u32 seq, int event,
2776 u32 flags, int family,
96518518
PM
2777 const struct nft_table *table,
2778 const struct nft_chain *chain,
2c82c7e7
FW
2779 const struct nft_rule *rule,
2780 const struct nft_rule *prule)
96518518
PM
2781{
2782 struct nlmsghdr *nlh;
2783 struct nfgenmsg *nfmsg;
2784 const struct nft_expr *expr, *next;
2785 struct nlattr *list;
dedb67c4 2786 u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
96518518 2787
dedb67c4 2788 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), flags);
96518518
PM
2789 if (nlh == NULL)
2790 goto nla_put_failure;
2791
2792 nfmsg = nlmsg_data(nlh);
2793 nfmsg->nfgen_family = family;
2794 nfmsg->version = NFNETLINK_V0;
84d7fce6 2795 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
96518518
PM
2796
2797 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
2798 goto nla_put_failure;
2799 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
2800 goto nla_put_failure;
b46f6ded
ND
2801 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle),
2802 NFTA_RULE_PAD))
96518518
PM
2803 goto nla_put_failure;
2804
2c82c7e7 2805 if (event != NFT_MSG_DELRULE && prule) {
5e948466 2806 if (nla_put_be64(skb, NFTA_RULE_POSITION,
b46f6ded
ND
2807 cpu_to_be64(prule->handle),
2808 NFTA_RULE_PAD))
5e948466
EL
2809 goto nla_put_failure;
2810 }
2811
ae0be8de 2812 list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS);
96518518
PM
2813 if (list == NULL)
2814 goto nla_put_failure;
2815 nft_rule_for_each_expr(expr, next, rule) {
0b2d8a7b 2816 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0)
96518518 2817 goto nla_put_failure;
96518518
PM
2818 }
2819 nla_nest_end(skb, list);
2820
86f1ec32
PM
2821 if (rule->udata) {
2822 struct nft_userdata *udata = nft_userdata(rule);
2823 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
2824 udata->data) < 0)
2825 goto nla_put_failure;
2826 }
0768b3b3 2827
053c095a
JB
2828 nlmsg_end(skb, nlh);
2829 return 0;
96518518
PM
2830
2831nla_put_failure:
2832 nlmsg_trim(skb, nlh);
2833 return -1;
2834}
2835
25e94a99
PNA
2836static void nf_tables_rule_notify(const struct nft_ctx *ctx,
2837 const struct nft_rule *rule, int event)
96518518
PM
2838{
2839 struct sk_buff *skb;
96518518 2840 int err;
8e6cf365
RGB
2841 char *buf = kasprintf(GFP_KERNEL, "%s:%llu;%s:%llu",
2842 ctx->table->name, ctx->table->handle,
2843 ctx->chain->name, ctx->chain->handle);
2844
2845 audit_log_nfcfg(buf,
2846 ctx->family,
2847 rule->handle,
2848 event == NFT_MSG_NEWRULE ?
2849 AUDIT_NFT_OP_RULE_REGISTER :
14224039
RGB
2850 AUDIT_NFT_OP_RULE_UNREGISTER,
2851 GFP_KERNEL);
8e6cf365 2852 kfree(buf);
96518518 2853
128ad332
PNA
2854 if (!ctx->report &&
2855 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
25e94a99 2856 return;
96518518 2857
96518518
PM
2858 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2859 if (skb == NULL)
2860 goto err;
2861
84d7fce6 2862 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
36596dad 2863 event, 0, ctx->family, ctx->table,
2c82c7e7 2864 ctx->chain, rule, NULL);
96518518
PM
2865 if (err < 0) {
2866 kfree_skb(skb);
2867 goto err;
2868 }
2869
67cc570e 2870 nft_notify_enqueue(skb, ctx->report, &ctx->net->nft.notify_list);
25e94a99 2871 return;
96518518 2872err:
25e94a99 2873 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
96518518
PM
2874}
2875
6e1f760e 2876struct nft_rule_dump_ctx {
e46abbcc 2877 char *table;
b7263e07 2878 char *chain;
6e1f760e
PNA
2879};
2880
241faece
PS
2881static int __nf_tables_dump_rules(struct sk_buff *skb,
2882 unsigned int *idx,
2883 struct netlink_callback *cb,
2884 const struct nft_table *table,
2885 const struct nft_chain *chain)
2886{
2887 struct net *net = sock_net(skb->sk);
2c82c7e7 2888 const struct nft_rule *rule, *prule;
241faece 2889 unsigned int s_idx = cb->args[0];
241faece 2890
2c82c7e7 2891 prule = NULL;
241faece
PS
2892 list_for_each_entry_rcu(rule, &chain->rules, list) {
2893 if (!nft_is_active(net, rule))
2c82c7e7 2894 goto cont_skip;
241faece
PS
2895 if (*idx < s_idx)
2896 goto cont;
2897 if (*idx > s_idx) {
2898 memset(&cb->args[1], 0,
2899 sizeof(cb->args) - sizeof(cb->args[0]));
2900 }
2901 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
2902 cb->nlh->nlmsg_seq,
2903 NFT_MSG_NEWRULE,
2904 NLM_F_MULTI | NLM_F_APPEND,
2905 table->family,
2c82c7e7 2906 table, chain, rule, prule) < 0)
310529e6 2907 return 1;
241faece
PS
2908
2909 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2910cont:
2c82c7e7
FW
2911 prule = rule;
2912cont_skip:
241faece
PS
2913 (*idx)++;
2914 }
310529e6 2915 return 0;
241faece
PS
2916}
2917
96518518
PM
2918static int nf_tables_dump_rules(struct sk_buff *skb,
2919 struct netlink_callback *cb)
2920{
2921 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
6e1f760e 2922 const struct nft_rule_dump_ctx *ctx = cb->data;
241faece 2923 struct nft_table *table;
96518518 2924 const struct nft_chain *chain;
241faece 2925 unsigned int idx = 0;
99633ab2 2926 struct net *net = sock_net(skb->sk);
96518518
PM
2927 int family = nfmsg->nfgen_family;
2928
e688a7f8 2929 rcu_read_lock();
38e029f1
PNA
2930 cb->seq = net->nft.base_seq;
2931
36596dad 2932 list_for_each_entry_rcu(table, &net->nft.tables, list) {
98319cb9 2933 if (family != NFPROTO_UNSPEC && family != table->family)
36596dad
PNA
2934 continue;
2935
2936 if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0)
96518518
PM
2937 continue;
2938
715849ab 2939 if (ctx && ctx->table && ctx->chain) {
241faece 2940 struct rhlist_head *list, *tmp;
6e1f760e 2941
241faece
PS
2942 list = rhltable_lookup(&table->chains_ht, ctx->chain,
2943 nft_chain_ht_params);
2944 if (!list)
2945 goto done;
2946
2947 rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
2948 if (!nft_is_active(net, chain))
2949 continue;
2950 __nf_tables_dump_rules(skb, &idx,
2951 cb, table, chain);
2952 break;
96518518 2953 }
241faece 2954 goto done;
96518518 2955 }
241faece
PS
2956
2957 list_for_each_entry_rcu(chain, &table->chains, list) {
2958 if (__nf_tables_dump_rules(skb, &idx, cb, table, chain))
2959 goto done;
2960 }
2961
2962 if (ctx && ctx->table)
2963 break;
96518518
PM
2964 }
2965done:
e688a7f8 2966 rcu_read_unlock();
310529e6
PS
2967
2968 cb->args[0] = idx;
96518518
PM
2969 return skb->len;
2970}
2971
90fd131a
FW
2972static int nf_tables_dump_rules_start(struct netlink_callback *cb)
2973{
2974 const struct nlattr * const *nla = cb->data;
2975 struct nft_rule_dump_ctx *ctx = NULL;
2976
2977 if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) {
2978 ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
2979 if (!ctx)
2980 return -ENOMEM;
2981
2982 if (nla[NFTA_RULE_TABLE]) {
2983 ctx->table = nla_strdup(nla[NFTA_RULE_TABLE],
2984 GFP_ATOMIC);
2985 if (!ctx->table) {
2986 kfree(ctx);
2987 return -ENOMEM;
2988 }
2989 }
2990 if (nla[NFTA_RULE_CHAIN]) {
2991 ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN],
2992 GFP_ATOMIC);
2993 if (!ctx->chain) {
2994 kfree(ctx->table);
2995 kfree(ctx);
2996 return -ENOMEM;
2997 }
2998 }
2999 }
3000
3001 cb->data = ctx;
3002 return 0;
3003}
3004
6e1f760e
PNA
3005static int nf_tables_dump_rules_done(struct netlink_callback *cb)
3006{
e46abbcc
PS
3007 struct nft_rule_dump_ctx *ctx = cb->data;
3008
3009 if (ctx) {
3010 kfree(ctx->table);
b7263e07 3011 kfree(ctx->chain);
e46abbcc
PS
3012 kfree(ctx);
3013 }
6e1f760e
PNA
3014 return 0;
3015}
3016
d9adf22a 3017/* called with rcu_read_lock held */
7b8002a1
PNA
3018static int nf_tables_getrule(struct net *net, struct sock *nlsk,
3019 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
3020 const struct nlattr * const nla[],
3021 struct netlink_ext_ack *extack)
96518518
PM
3022{
3023 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 3024 u8 genmask = nft_genmask_cur(net);
96518518
PM
3025 const struct nft_chain *chain;
3026 const struct nft_rule *rule;
1b2470e5 3027 struct nft_table *table;
96518518
PM
3028 struct sk_buff *skb2;
3029 int family = nfmsg->nfgen_family;
3030 int err;
3031
3032 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3033 struct netlink_dump_control c = {
90fd131a 3034 .start= nf_tables_dump_rules_start,
96518518 3035 .dump = nf_tables_dump_rules,
6e1f760e 3036 .done = nf_tables_dump_rules_done,
d9adf22a 3037 .module = THIS_MODULE,
90fd131a 3038 .data = (void *)nla,
96518518 3039 };
6e1f760e 3040
d9adf22a 3041 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
96518518
PM
3042 }
3043
cac20fcd 3044 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask);
36dd1bcc
PNA
3045 if (IS_ERR(table)) {
3046 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
96518518 3047 return PTR_ERR(table);
36dd1bcc 3048 }
96518518 3049
f102d66b 3050 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask);
36dd1bcc
PNA
3051 if (IS_ERR(chain)) {
3052 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
96518518 3053 return PTR_ERR(chain);
36dd1bcc 3054 }
96518518 3055
cac20fcd 3056 rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
36dd1bcc
PNA
3057 if (IS_ERR(rule)) {
3058 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
96518518 3059 return PTR_ERR(rule);
36dd1bcc 3060 }
96518518 3061
d9adf22a 3062 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
96518518
PM
3063 if (!skb2)
3064 return -ENOMEM;
3065
84d7fce6 3066 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
96518518 3067 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
2c82c7e7 3068 family, table, chain, rule, NULL);
96518518 3069 if (err < 0)
ee921183 3070 goto err_fill_rule_info;
96518518 3071
ee921183 3072 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
96518518 3073
ee921183 3074err_fill_rule_info:
96518518
PM
3075 kfree_skb(skb2);
3076 return err;
3077}
3078
62472bce
PM
3079static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
3080 struct nft_rule *rule)
96518518 3081{
29e38801 3082 struct nft_expr *expr, *next;
96518518
PM
3083
3084 /*
3085 * Careful: some expressions might not be initialized in case this
3086 * is called on error from nf_tables_newrule().
3087 */
3088 expr = nft_expr_first(rule);
31cc578a 3089 while (nft_expr_more(rule, expr)) {
29e38801 3090 next = nft_expr_next(expr);
62472bce 3091 nf_tables_expr_destroy(ctx, expr);
29e38801 3092 expr = next;
96518518
PM
3093 }
3094 kfree(rule);
3095}
3096
d0e2c7de 3097void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule)
bb7b40ae 3098{
f6ac8585 3099 nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE);
bb7b40ae
PNA
3100 nf_tables_rule_destroy(ctx, rule);
3101}
3102
a654de8f
PNA
3103int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain)
3104{
3105 struct nft_expr *expr, *last;
3106 const struct nft_data *data;
3107 struct nft_rule *rule;
3108 int err;
3109
26b2f552
TY
3110 if (ctx->level == NFT_JUMP_STACK_SIZE)
3111 return -EMLINK;
3112
a654de8f
PNA
3113 list_for_each_entry(rule, &chain->rules, list) {
3114 if (!nft_is_active_next(ctx->net, rule))
3115 continue;
3116
3117 nft_rule_for_each_expr(expr, last, rule) {
3118 if (!expr->ops->validate)
3119 continue;
3120
3121 err = expr->ops->validate(ctx, expr, &data);
3122 if (err < 0)
3123 return err;
3124 }
3125 }
3126
3127 return 0;
3128}
3129EXPORT_SYMBOL_GPL(nft_chain_validate);
3130
3131static int nft_table_validate(struct net *net, const struct nft_table *table)
3132{
3133 struct nft_chain *chain;
3134 struct nft_ctx ctx = {
3135 .net = net,
3136 .family = table->family,
3137 };
3138 int err;
3139
3140 list_for_each_entry(chain, &table->chains, list) {
3141 if (!nft_is_base_chain(chain))
3142 continue;
3143
3144 ctx.chain = chain;
3145 err = nft_chain_validate(&ctx, chain);
3146 if (err < 0)
3147 return err;
3148 }
3149
3150 return 0;
3151}
3152
75dd48e2
PS
3153static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
3154 const struct nlattr *nla);
3155
1081d11b
PNA
3156#define NFT_RULE_MAXEXPRS 128
3157
633c9a84
PNA
3158static int nf_tables_newrule(struct net *net, struct sock *nlsk,
3159 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
3160 const struct nlattr * const nla[],
3161 struct netlink_ext_ack *extack)
96518518
PM
3162{
3163 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 3164 u8 genmask = nft_genmask_next(net);
2a43ecf9 3165 struct nft_expr_info *info = NULL;
98319cb9 3166 int family = nfmsg->nfgen_family;
c9626a2c 3167 struct nft_flow_rule *flow;
96518518
PM
3168 struct nft_table *table;
3169 struct nft_chain *chain;
3170 struct nft_rule *rule, *old_rule = NULL;
86f1ec32 3171 struct nft_userdata *udata;
1081d11b 3172 struct nft_trans *trans = NULL;
96518518
PM
3173 struct nft_expr *expr;
3174 struct nft_ctx ctx;
3175 struct nlattr *tmp;
86f1ec32 3176 unsigned int size, i, n, ulen = 0, usize = 0;
96518518 3177 int err, rem;
5e948466 3178 u64 handle, pos_handle;
96518518 3179
f102d66b
FW
3180 lockdep_assert_held(&net->nft.commit_mutex);
3181
cac20fcd 3182 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask);
36dd1bcc
PNA
3183 if (IS_ERR(table)) {
3184 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
96518518 3185 return PTR_ERR(table);
36dd1bcc 3186 }
96518518 3187
837830a4
PNA
3188 if (nla[NFTA_RULE_CHAIN]) {
3189 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
3190 genmask);
3191 if (IS_ERR(chain)) {
3192 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
3193 return PTR_ERR(chain);
3194 }
d0e2c7de
PNA
3195 if (nft_chain_is_bound(chain))
3196 return -EOPNOTSUPP;
3197
837830a4
PNA
3198 } else if (nla[NFTA_RULE_CHAIN_ID]) {
3199 chain = nft_chain_lookup_byid(net, nla[NFTA_RULE_CHAIN_ID]);
3200 if (IS_ERR(chain)) {
3201 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN_ID]);
3202 return PTR_ERR(chain);
3203 }
3204 } else {
3205 return -EINVAL;
36dd1bcc 3206 }
96518518
PM
3207
3208 if (nla[NFTA_RULE_HANDLE]) {
3209 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
cac20fcd 3210 rule = __nft_rule_lookup(chain, handle);
36dd1bcc
PNA
3211 if (IS_ERR(rule)) {
3212 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
96518518 3213 return PTR_ERR(rule);
36dd1bcc 3214 }
96518518 3215
36dd1bcc
PNA
3216 if (nlh->nlmsg_flags & NLM_F_EXCL) {
3217 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
96518518 3218 return -EEXIST;
36dd1bcc 3219 }
96518518
PM
3220 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3221 old_rule = rule;
3222 else
3223 return -EOPNOTSUPP;
3224 } else {
445509eb
PNA
3225 if (!(nlh->nlmsg_flags & NLM_F_CREATE) ||
3226 nlh->nlmsg_flags & NLM_F_REPLACE)
96518518
PM
3227 return -EINVAL;
3228 handle = nf_tables_alloc_handle(table);
a0a7379e
PNA
3229
3230 if (chain->use == UINT_MAX)
3231 return -EOVERFLOW;
5e948466 3232
447750f2
FW
3233 if (nla[NFTA_RULE_POSITION]) {
3234 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
3235 old_rule = __nft_rule_lookup(chain, pos_handle);
3236 if (IS_ERR(old_rule)) {
3237 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]);
3238 return PTR_ERR(old_rule);
3239 }
75dd48e2
PS
3240 } else if (nla[NFTA_RULE_POSITION_ID]) {
3241 old_rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_POSITION_ID]);
3242 if (IS_ERR(old_rule)) {
3243 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]);
3244 return PTR_ERR(old_rule);
3245 }
36dd1bcc 3246 }
5e948466
EL
3247 }
3248
98319cb9 3249 nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
0ca743a5 3250
96518518
PM
3251 n = 0;
3252 size = 0;
3253 if (nla[NFTA_RULE_EXPRESSIONS]) {
2a43ecf9
FW
3254 info = kvmalloc_array(NFT_RULE_MAXEXPRS,
3255 sizeof(struct nft_expr_info),
3256 GFP_KERNEL);
3257 if (!info)
3258 return -ENOMEM;
3259
96518518
PM
3260 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
3261 err = -EINVAL;
3262 if (nla_type(tmp) != NFTA_LIST_ELEM)
3263 goto err1;
3264 if (n == NFT_RULE_MAXEXPRS)
3265 goto err1;
0ca743a5 3266 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
96518518
PM
3267 if (err < 0)
3268 goto err1;
3269 size += info[n].ops->size;
3270 n++;
3271 }
3272 }
9889840f
PM
3273 /* Check for overflow of dlen field */
3274 err = -EFBIG;
3275 if (size >= 1 << 12)
3276 goto err1;
96518518 3277
86f1ec32 3278 if (nla[NFTA_RULE_USERDATA]) {
0768b3b3 3279 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
86f1ec32
PM
3280 if (ulen > 0)
3281 usize = sizeof(struct nft_userdata) + ulen;
3282 }
0768b3b3 3283
96518518 3284 err = -ENOMEM;
86f1ec32 3285 rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL);
96518518
PM
3286 if (rule == NULL)
3287 goto err1;
3288
889f7ee7 3289 nft_activate_next(net, rule);
0628b123 3290
96518518
PM
3291 rule->handle = handle;
3292 rule->dlen = size;
86f1ec32 3293 rule->udata = ulen ? 1 : 0;
0768b3b3 3294
86f1ec32
PM
3295 if (ulen) {
3296 udata = nft_userdata(rule);
3297 udata->len = ulen - 1;
3298 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
3299 }
96518518 3300
96518518
PM
3301 expr = nft_expr_first(rule);
3302 for (i = 0; i < n; i++) {
3303 err = nf_tables_newexpr(&ctx, &info[i], expr);
83d9dcba
PNA
3304 if (err < 0) {
3305 NL_SET_BAD_ATTR(extack, info[i].attr);
96518518 3306 goto err2;
83d9dcba 3307 }
a654de8f
PNA
3308
3309 if (info[i].ops->validate)
3310 nft_validate_state_update(net, NFT_VALIDATE_NEED);
3311
ef1f7df9 3312 info[i].ops = NULL;
96518518
PM
3313 expr = nft_expr_next(expr);
3314 }
3315
96518518 3316 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
ca089878 3317 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
569ccae6
FW
3318 if (trans == NULL) {
3319 err = -ENOMEM;
3320 goto err2;
3321 }
ca089878
TY
3322 err = nft_delrule(&ctx, old_rule);
3323 if (err < 0) {
3324 nft_trans_destroy(trans);
569ccae6
FW
3325 goto err2;
3326 }
3327
3328 list_add_tail_rcu(&rule->list, &old_rule->list);
3329 } else {
c9626a2c
PNA
3330 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
3331 if (!trans) {
569ccae6
FW
3332 err = -ENOMEM;
3333 goto err2;
3334 }
3335
3336 if (nlh->nlmsg_flags & NLM_F_APPEND) {
3337 if (old_rule)
3338 list_add_rcu(&rule->list, &old_rule->list);
3339 else
3340 list_add_tail_rcu(&rule->list, &chain->rules);
3341 } else {
3342 if (old_rule)
3343 list_add_tail_rcu(&rule->list, &old_rule->list);
3344 else
3345 list_add_rcu(&rule->list, &chain->rules);
3346 }
0628b123 3347 }
2a43ecf9 3348 kvfree(info);
4fefee57 3349 chain->use++;
96518518 3350
a654de8f
PNA
3351 if (net->nft.validate_state == NFT_VALIDATE_DO)
3352 return nft_table_validate(net, table);
3353
c9626a2c 3354 if (chain->flags & NFT_CHAIN_HW_OFFLOAD) {
be2861dc 3355 flow = nft_flow_rule_create(net, rule);
c9626a2c
PNA
3356 if (IS_ERR(flow))
3357 return PTR_ERR(flow);
3358
3359 nft_trans_flow_rule(trans) = flow;
3360 }
3361
a654de8f 3362 return 0;
96518518 3363err2:
bb7b40ae 3364 nf_tables_rule_release(&ctx, rule);
96518518
PM
3365err1:
3366 for (i = 0; i < n; i++) {
b25a31bf 3367 if (info[i].ops) {
ef1f7df9 3368 module_put(info[i].ops->type->owner);
b25a31bf
TY
3369 if (info[i].ops->type->release_ops)
3370 info[i].ops->type->release_ops(info[i].ops);
3371 }
96518518 3372 }
2a43ecf9 3373 kvfree(info);
96518518
PM
3374 return err;
3375}
3376
1a94e38d
PNA
3377static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
3378 const struct nlattr *nla)
3379{
3380 u32 id = ntohl(nla_get_be32(nla));
3381 struct nft_trans *trans;
3382
3383 list_for_each_entry(trans, &net->nft.commit_list, list) {
3384 struct nft_rule *rule = nft_trans_rule(trans);
3385
3386 if (trans->msg_type == NFT_MSG_NEWRULE &&
3387 id == nft_trans_rule_id(trans))
3388 return rule;
3389 }
3390 return ERR_PTR(-ENOENT);
3391}
3392
633c9a84
PNA
3393static int nf_tables_delrule(struct net *net, struct sock *nlsk,
3394 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
3395 const struct nlattr * const nla[],
3396 struct netlink_ext_ack *extack)
96518518
PM
3397{
3398 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 3399 u8 genmask = nft_genmask_next(net);
7c95f6d8 3400 struct nft_table *table;
cf9dc09d
PNA
3401 struct nft_chain *chain = NULL;
3402 struct nft_rule *rule;
0628b123
PNA
3403 int family = nfmsg->nfgen_family, err = 0;
3404 struct nft_ctx ctx;
96518518 3405
cac20fcd 3406 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask);
36dd1bcc
PNA
3407 if (IS_ERR(table)) {
3408 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
96518518 3409 return PTR_ERR(table);
36dd1bcc 3410 }
96518518 3411
cf9dc09d 3412 if (nla[NFTA_RULE_CHAIN]) {
f102d66b
FW
3413 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
3414 genmask);
36dd1bcc
PNA
3415 if (IS_ERR(chain)) {
3416 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
cf9dc09d 3417 return PTR_ERR(chain);
36dd1bcc 3418 }
d0e2c7de
PNA
3419 if (nft_chain_is_bound(chain))
3420 return -EOPNOTSUPP;
cf9dc09d 3421 }
96518518 3422
98319cb9 3423 nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
0628b123 3424
cf9dc09d
PNA
3425 if (chain) {
3426 if (nla[NFTA_RULE_HANDLE]) {
cac20fcd 3427 rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
36dd1bcc
PNA
3428 if (IS_ERR(rule)) {
3429 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
cf9dc09d 3430 return PTR_ERR(rule);
36dd1bcc 3431 }
96518518 3432
1a94e38d
PNA
3433 err = nft_delrule(&ctx, rule);
3434 } else if (nla[NFTA_RULE_ID]) {
3435 rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_ID]);
36dd1bcc
PNA
3436 if (IS_ERR(rule)) {
3437 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]);
1a94e38d 3438 return PTR_ERR(rule);
36dd1bcc 3439 }
1a94e38d 3440
5e266fe7 3441 err = nft_delrule(&ctx, rule);
cf9dc09d 3442 } else {
ce24b721 3443 err = nft_delrule_by_chain(&ctx);
cf9dc09d
PNA
3444 }
3445 } else {
3446 list_for_each_entry(chain, &table->chains, list) {
664b0f8c
PNA
3447 if (!nft_is_active_next(net, chain))
3448 continue;
3449
cf9dc09d 3450 ctx.chain = chain;
ce24b721 3451 err = nft_delrule_by_chain(&ctx);
0628b123
PNA
3452 if (err < 0)
3453 break;
3454 }
3455 }
3456
3457 return err;
3458}
3459
20a69341
PM
3460/*
3461 * Sets
3462 */
e32a4dc6
FW
3463static const struct nft_set_type *nft_set_types[] = {
3464 &nft_set_hash_fast_type,
3465 &nft_set_hash_type,
3466 &nft_set_rhash_type,
3467 &nft_set_bitmap_type,
3468 &nft_set_rbtree_type,
e6abef61 3469#if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
7400b063
SB
3470 &nft_set_pipapo_avx2_type,
3471#endif
e32a4dc6
FW
3472 &nft_set_pipapo_type,
3473};
20a69341 3474
2b664957 3475#define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \
71cc0873
PS
3476 NFT_SET_TIMEOUT | NFT_SET_OBJECT | \
3477 NFT_SET_EVAL)
2b664957 3478
71cc0873 3479static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags)
2b664957 3480{
71cc0873 3481 return (flags & type->features) == (flags & NFT_SET_FEATURES);
2b664957
PNA
3482}
3483
c50b960c
PM
3484/*
3485 * Select a set implementation based on the data characteristics and the
3486 * given policy. The total memory use might not be known if no size is
3487 * given, in that case the amount of memory per element is used.
3488 */
3489static const struct nft_set_ops *
2b664957
PNA
3490nft_select_set_ops(const struct nft_ctx *ctx,
3491 const struct nlattr * const nla[],
c50b960c
PM
3492 const struct nft_set_desc *desc,
3493 enum nft_set_policies policy)
20a69341 3494{
c50b960c
PM
3495 const struct nft_set_ops *ops, *bops;
3496 struct nft_set_estimate est, best;
2b664957
PNA
3497 const struct nft_set_type *type;
3498 u32 flags = 0;
e32a4dc6 3499 int i;
20a69341 3500
f102d66b
FW
3501 lockdep_assert_held(&ctx->net->nft.commit_mutex);
3502 lockdep_nfnl_nft_mutex_not_held();
e32a4dc6 3503
2b664957
PNA
3504 if (nla[NFTA_SET_FLAGS] != NULL)
3505 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
20a69341 3506
55af753c
PNA
3507 bops = NULL;
3508 best.size = ~0;
3509 best.lookup = ~0;
0b5a7874 3510 best.space = ~0;
c50b960c 3511
e32a4dc6
FW
3512 for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) {
3513 type = nft_set_types[i];
71cc0873 3514 ops = &type->ops;
2b664957 3515
71cc0873 3516 if (!nft_set_ops_candidate(type, flags))
20a69341 3517 continue;
2b664957 3518 if (!ops->estimate(desc, flags, &est))
c50b960c
PM
3519 continue;
3520
3521 switch (policy) {
3522 case NFT_SET_POL_PERFORMANCE:
55af753c 3523 if (est.lookup < best.lookup)
c50b960c 3524 break;
644e334e
PNA
3525 if (est.lookup == best.lookup &&
3526 est.space < best.space)
3527 break;
c50b960c
PM
3528 continue;
3529 case NFT_SET_POL_MEMORY:
0b5a7874
PNA
3530 if (!desc->size) {
3531 if (est.space < best.space)
3532 break;
3533 if (est.space == best.space &&
3534 est.lookup < best.lookup)
3535 break;
4f2921ca 3536 } else if (est.size < best.size || !bops) {
c50b960c 3537 break;
0b5a7874 3538 }
c50b960c
PM
3539 continue;
3540 default:
3541 break;
3542 }
3543
c50b960c
PM
3544 bops = ops;
3545 best = est;
20a69341
PM
3546 }
3547
c50b960c
PM
3548 if (bops != NULL)
3549 return bops;
3550
20a69341
PM
3551 return ERR_PTR(-EOPNOTSUPP);
3552}
3553
3554static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
b2fbd044
LZ
3555 [NFTA_SET_TABLE] = { .type = NLA_STRING,
3556 .len = NFT_TABLE_MAXNAMELEN - 1 },
a9bdd836 3557 [NFTA_SET_NAME] = { .type = NLA_STRING,
cb39ad8b 3558 .len = NFT_SET_MAXNAMELEN - 1 },
20a69341
PM
3559 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
3560 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
3561 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
3562 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
3563 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
c50b960c
PM
3564 [NFTA_SET_POLICY] = { .type = NLA_U32 },
3565 [NFTA_SET_DESC] = { .type = NLA_NESTED },
958bee14 3566 [NFTA_SET_ID] = { .type = NLA_U32 },
761da293
PM
3567 [NFTA_SET_TIMEOUT] = { .type = NLA_U64 },
3568 [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 },
e6d8ecac
CFG
3569 [NFTA_SET_USERDATA] = { .type = NLA_BINARY,
3570 .len = NFT_USERDATA_MAXLEN },
8aeff920 3571 [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 },
3ecbfd65 3572 [NFTA_SET_HANDLE] = { .type = NLA_U64 },
65038428 3573 [NFTA_SET_EXPR] = { .type = NLA_NESTED },
48b0ae04 3574 [NFTA_SET_EXPRESSIONS] = { .type = NLA_NESTED },
c50b960c
PM
3575};
3576
3577static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
3578 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
f3a2181e 3579 [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED },
20a69341
PM
3580};
3581
633c9a84 3582static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net,
20a69341
PM
3583 const struct sk_buff *skb,
3584 const struct nlmsghdr *nlh,
f2a6d766 3585 const struct nlattr * const nla[],
36dd1bcc 3586 struct netlink_ext_ack *extack,
f2a6d766 3587 u8 genmask)
20a69341
PM
3588{
3589 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
98319cb9 3590 int family = nfmsg->nfgen_family;
7c95f6d8 3591 struct nft_table *table = NULL;
20a69341 3592
20a69341 3593 if (nla[NFTA_SET_TABLE] != NULL) {
cac20fcd
PNA
3594 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family,
3595 genmask);
36dd1bcc
PNA
3596 if (IS_ERR(table)) {
3597 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
20a69341 3598 return PTR_ERR(table);
36dd1bcc 3599 }
20a69341
PM
3600 }
3601
98319cb9 3602 nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla);
20a69341
PM
3603 return 0;
3604}
3605
cac20fcd
PNA
3606static struct nft_set *nft_set_lookup(const struct nft_table *table,
3607 const struct nlattr *nla, u8 genmask)
20a69341
PM
3608{
3609 struct nft_set *set;
3610
3611 if (nla == NULL)
3612 return ERR_PTR(-EINVAL);
3613
d9adf22a 3614 list_for_each_entry_rcu(set, &table->sets, list) {
37a9cc52
PNA
3615 if (!nla_strcmp(nla, set->name) &&
3616 nft_active_genmask(set, genmask))
20a69341
PM
3617 return set;
3618 }
3619 return ERR_PTR(-ENOENT);
3620}
3621
cac20fcd
PNA
3622static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table,
3623 const struct nlattr *nla,
3624 u8 genmask)
3ecbfd65
HS
3625{
3626 struct nft_set *set;
3627
3ecbfd65
HS
3628 list_for_each_entry(set, &table->sets, list) {
3629 if (be64_to_cpu(nla_get_be64(nla)) == set->handle &&
3630 nft_active_genmask(set, genmask))
3631 return set;
3632 }
3633 return ERR_PTR(-ENOENT);
3634}
3635
cac20fcd
PNA
3636static struct nft_set *nft_set_lookup_byid(const struct net *net,
3637 const struct nlattr *nla, u8 genmask)
958bee14
PNA
3638{
3639 struct nft_trans *trans;
3640 u32 id = ntohl(nla_get_be32(nla));
3641
3642 list_for_each_entry(trans, &net->nft.commit_list, list) {
9c7f96fd
AK
3643 if (trans->msg_type == NFT_MSG_NEWSET) {
3644 struct nft_set *set = nft_trans_set(trans);
37a9cc52 3645
9c7f96fd
AK
3646 if (id == nft_trans_set_id(trans) &&
3647 nft_active_genmask(set, genmask))
3648 return set;
3649 }
958bee14
PNA
3650 }
3651 return ERR_PTR(-ENOENT);
3652}
c7a72e3f 3653
10659cba
PNA
3654struct nft_set *nft_set_lookup_global(const struct net *net,
3655 const struct nft_table *table,
3656 const struct nlattr *nla_set_name,
3657 const struct nlattr *nla_set_id,
3658 u8 genmask)
c7a72e3f
PNA
3659{
3660 struct nft_set *set;
3661
cac20fcd 3662 set = nft_set_lookup(table, nla_set_name, genmask);
c7a72e3f
PNA
3663 if (IS_ERR(set)) {
3664 if (!nla_set_id)
3665 return set;
3666
cac20fcd 3667 set = nft_set_lookup_byid(net, nla_set_id, genmask);
c7a72e3f
PNA
3668 }
3669 return set;
3670}
10659cba 3671EXPORT_SYMBOL_GPL(nft_set_lookup_global);
958bee14 3672
20a69341
PM
3673static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
3674 const char *name)
3675{
3676 const struct nft_set *i;
3677 const char *p;
3678 unsigned long *inuse;
60eb1894 3679 unsigned int n = 0, min = 0;
20a69341 3680
38745490 3681 p = strchr(name, '%');
20a69341
PM
3682 if (p != NULL) {
3683 if (p[1] != 'd' || strchr(p + 2, '%'))
3684 return -EINVAL;
3685
3686 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
3687 if (inuse == NULL)
3688 return -ENOMEM;
60eb1894 3689cont:
20a69341 3690 list_for_each_entry(i, &ctx->table->sets, list) {
14662917
DB
3691 int tmp;
3692
37a9cc52
PNA
3693 if (!nft_is_active_next(ctx->net, set))
3694 continue;
14662917 3695 if (!sscanf(i->name, name, &tmp))
20a69341 3696 continue;
60eb1894 3697 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
20a69341 3698 continue;
14662917 3699
60eb1894 3700 set_bit(tmp - min, inuse);
20a69341
PM
3701 }
3702
53b70287 3703 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
60eb1894
PM
3704 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
3705 min += BITS_PER_BYTE * PAGE_SIZE;
3706 memset(inuse, 0, PAGE_SIZE);
3707 goto cont;
3708 }
20a69341
PM
3709 free_page((unsigned long)inuse);
3710 }
3711
38745490
PS
3712 set->name = kasprintf(GFP_KERNEL, name, min + n);
3713 if (!set->name)
3714 return -ENOMEM;
3715
20a69341 3716 list_for_each_entry(i, &ctx->table->sets, list) {
37a9cc52
PNA
3717 if (!nft_is_active_next(ctx->net, i))
3718 continue;
e63aaaa6
AY
3719 if (!strcmp(set->name, i->name)) {
3720 kfree(set->name);
7fb6f78d 3721 set->name = NULL;
20a69341 3722 return -ENFILE;
e63aaaa6 3723 }
20a69341
PM
3724 }
3725 return 0;
3726}
3727
917d80d3 3728int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result)
8e1102d5
FW
3729{
3730 u64 ms = be64_to_cpu(nla_get_be64(nla));
3731 u64 max = (u64)(~((u64)0));
3732
3733 max = div_u64(max, NSEC_PER_MSEC);
3734 if (ms >= max)
3735 return -ERANGE;
3736
3737 ms *= NSEC_PER_MSEC;
3738 *result = nsecs_to_jiffies64(ms);
3739 return 0;
3740}
3741
917d80d3 3742__be64 nf_jiffies64_to_msecs(u64 input)
8e1102d5 3743{
3b15d09f 3744 return cpu_to_be64(jiffies64_to_msecs(input));
8e1102d5
FW
3745}
3746
f3a2181e
SB
3747static int nf_tables_fill_set_concat(struct sk_buff *skb,
3748 const struct nft_set *set)
3749{
3750 struct nlattr *concat, *field;
3751 int i;
3752
3753 concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT);
3754 if (!concat)
3755 return -ENOMEM;
3756
3757 for (i = 0; i < set->field_count; i++) {
3758 field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
3759 if (!field)
3760 return -ENOMEM;
3761
3762 if (nla_put_be32(skb, NFTA_SET_FIELD_LEN,
3763 htonl(set->field_len[i])))
3764 return -ENOMEM;
3765
3766 nla_nest_end(skb, field);
3767 }
3768
3769 nla_nest_end(skb, concat);
3770
3771 return 0;
3772}
3773
20a69341
PM
3774static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
3775 const struct nft_set *set, u16 event, u16 flags)
3776{
3777 struct nfgenmsg *nfmsg;
3778 struct nlmsghdr *nlh;
128ad332 3779 u32 portid = ctx->portid;
65038428 3780 struct nlattr *nest;
128ad332 3781 u32 seq = ctx->seq;
48b0ae04 3782 int i;
20a69341 3783
dedb67c4 3784 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
20a69341
PM
3785 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3786 flags);
3787 if (nlh == NULL)
3788 goto nla_put_failure;
3789
3790 nfmsg = nlmsg_data(nlh);
36596dad 3791 nfmsg->nfgen_family = ctx->family;
20a69341 3792 nfmsg->version = NFNETLINK_V0;
84d7fce6 3793 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
20a69341
PM
3794
3795 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3796 goto nla_put_failure;
3797 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3798 goto nla_put_failure;
3ecbfd65
HS
3799 if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle),
3800 NFTA_SET_PAD))
3801 goto nla_put_failure;
20a69341
PM
3802 if (set->flags != 0)
3803 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
3804 goto nla_put_failure;
3805
3806 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
3807 goto nla_put_failure;
3808 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
3809 goto nla_put_failure;
3810 if (set->flags & NFT_SET_MAP) {
3811 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
3812 goto nla_put_failure;
3813 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
3814 goto nla_put_failure;
3815 }
8aeff920
PNA
3816 if (set->flags & NFT_SET_OBJECT &&
3817 nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype)))
3818 goto nla_put_failure;
20a69341 3819
761da293 3820 if (set->timeout &&
d3e2a111 3821 nla_put_be64(skb, NFTA_SET_TIMEOUT,
8e1102d5 3822 nf_jiffies64_to_msecs(set->timeout),
b46f6ded 3823 NFTA_SET_PAD))
761da293
PM
3824 goto nla_put_failure;
3825 if (set->gc_int &&
3826 nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int)))
3827 goto nla_put_failure;
3828
9363dc4b
AB
3829 if (set->policy != NFT_SET_POL_PERFORMANCE) {
3830 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
3831 goto nla_put_failure;
3832 }
3833
6f03bf43
PNA
3834 if (set->udata &&
3835 nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata))
e6d8ecac
CFG
3836 goto nla_put_failure;
3837
65038428
PNA
3838 nest = nla_nest_start_noflag(skb, NFTA_SET_DESC);
3839 if (!nest)
c50b960c
PM
3840 goto nla_put_failure;
3841 if (set->size &&
3842 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
3843 goto nla_put_failure;
f3a2181e
SB
3844
3845 if (set->field_count > 1 &&
3846 nf_tables_fill_set_concat(skb, set))
3847 goto nla_put_failure;
3848
65038428
PNA
3849 nla_nest_end(skb, nest);
3850
8cfd9b0f 3851 if (set->num_exprs == 1) {
65038428 3852 nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR);
8cfd9b0f 3853 if (nf_tables_fill_expr_info(skb, set->exprs[0]) < 0)
65038428
PNA
3854 goto nla_put_failure;
3855
3856 nla_nest_end(skb, nest);
48b0ae04
PNA
3857 } else if (set->num_exprs > 1) {
3858 nest = nla_nest_start_noflag(skb, NFTA_SET_EXPRESSIONS);
3859 if (nest == NULL)
65038428
PNA
3860 goto nla_put_failure;
3861
48b0ae04
PNA
3862 for (i = 0; i < set->num_exprs; i++) {
3863 if (nft_expr_dump(skb, NFTA_LIST_ELEM,
3864 set->exprs[i]) < 0)
3865 goto nla_put_failure;
3866 }
65038428
PNA
3867 nla_nest_end(skb, nest);
3868 }
c50b960c 3869
053c095a
JB
3870 nlmsg_end(skb, nlh);
3871 return 0;
20a69341
PM
3872
3873nla_put_failure:
3874 nlmsg_trim(skb, nlh);
3875 return -1;
3876}
3877
25e94a99
PNA
3878static void nf_tables_set_notify(const struct nft_ctx *ctx,
3879 const struct nft_set *set, int event,
3880 gfp_t gfp_flags)
20a69341
PM
3881{
3882 struct sk_buff *skb;
128ad332 3883 u32 portid = ctx->portid;
20a69341 3884 int err;
8e6cf365
RGB
3885 char *buf = kasprintf(gfp_flags, "%s:%llu;%s:%llu",
3886 ctx->table->name, ctx->table->handle,
3887 set->name, set->handle);
3888
3889 audit_log_nfcfg(buf,
3890 ctx->family,
3891 set->field_count,
3892 event == NFT_MSG_NEWSET ?
3893 AUDIT_NFT_OP_SET_REGISTER :
14224039
RGB
3894 AUDIT_NFT_OP_SET_UNREGISTER,
3895 gfp_flags);
8e6cf365 3896 kfree(buf);
20a69341 3897
128ad332
PNA
3898 if (!ctx->report &&
3899 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
25e94a99 3900 return;
20a69341 3901
31f8441c 3902 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
20a69341
PM
3903 if (skb == NULL)
3904 goto err;
3905
3906 err = nf_tables_fill_set(skb, ctx, set, event, 0);
3907 if (err < 0) {
3908 kfree_skb(skb);
3909 goto err;
3910 }
3911
67cc570e 3912 nft_notify_enqueue(skb, ctx->report, &ctx->net->nft.notify_list);
25e94a99 3913 return;
20a69341 3914err:
25e94a99 3915 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
20a69341
PM
3916}
3917
5b96af77 3918static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
c9c8e485
PNA
3919{
3920 const struct nft_set *set;
3921 unsigned int idx, s_idx = cb->args[0];
c9c8e485
PNA
3922 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
3923 struct net *net = sock_net(skb->sk);
5b96af77 3924 struct nft_ctx *ctx = cb->data, ctx_set;
c9c8e485
PNA
3925
3926 if (cb->args[1])
3927 return skb->len;
3928
e688a7f8 3929 rcu_read_lock();
38e029f1
PNA
3930 cb->seq = net->nft.base_seq;
3931
36596dad
PNA
3932 list_for_each_entry_rcu(table, &net->nft.tables, list) {
3933 if (ctx->family != NFPROTO_UNSPEC &&
98319cb9 3934 ctx->family != table->family)
36596dad
PNA
3935 continue;
3936
3937 if (ctx->table && ctx->table != table)
5b96af77
PNA
3938 continue;
3939
36596dad
PNA
3940 if (cur_table) {
3941 if (cur_table != table)
c9c8e485
PNA
3942 continue;
3943
36596dad 3944 cur_table = NULL;
c9c8e485 3945 }
36596dad
PNA
3946 idx = 0;
3947 list_for_each_entry_rcu(set, &table->sets, list) {
3948 if (idx < s_idx)
3949 goto cont;
3950 if (!nft_is_active(net, set))
3951 goto cont;
5b96af77 3952
36596dad
PNA
3953 ctx_set = *ctx;
3954 ctx_set.table = table;
98319cb9 3955 ctx_set.family = table->family;
c9c8e485 3956
36596dad
PNA
3957 if (nf_tables_fill_set(skb, &ctx_set, set,
3958 NFT_MSG_NEWSET,
3959 NLM_F_MULTI) < 0) {
3960 cb->args[0] = idx;
3961 cb->args[2] = (unsigned long) table;
3962 goto done;
c9c8e485 3963 }
36596dad 3964 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
c9c8e485 3965cont:
36596dad 3966 idx++;
c9c8e485 3967 }
36596dad
PNA
3968 if (s_idx)
3969 s_idx = 0;
c9c8e485
PNA
3970 }
3971 cb->args[1] = 1;
3972done:
e688a7f8 3973 rcu_read_unlock();
c9c8e485
PNA
3974 return skb->len;
3975}
3976
90fd131a
FW
3977static int nf_tables_dump_sets_start(struct netlink_callback *cb)
3978{
3979 struct nft_ctx *ctx_dump = NULL;
3980
3981 ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC);
3982 if (ctx_dump == NULL)
3983 return -ENOMEM;
3984
3985 cb->data = ctx_dump;
3986 return 0;
3987}
3988
5b96af77 3989static int nf_tables_dump_sets_done(struct netlink_callback *cb)
20a69341 3990{
5b96af77
PNA
3991 kfree(cb->data);
3992 return 0;
20a69341
PM
3993}
3994
d9adf22a 3995/* called with rcu_read_lock held */
7b8002a1
PNA
3996static int nf_tables_getset(struct net *net, struct sock *nlsk,
3997 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
3998 const struct nlattr * const nla[],
3999 struct netlink_ext_ack *extack)
20a69341 4000{
f2a6d766 4001 u8 genmask = nft_genmask_cur(net);
20a69341
PM
4002 const struct nft_set *set;
4003 struct nft_ctx ctx;
4004 struct sk_buff *skb2;
c9c8e485 4005 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
20a69341
PM
4006 int err;
4007
01cfa0a4 4008 /* Verify existence before starting dump */
36dd1bcc
PNA
4009 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack,
4010 genmask);
20a69341
PM
4011 if (err < 0)
4012 return err;
4013
4014 if (nlh->nlmsg_flags & NLM_F_DUMP) {
4015 struct netlink_dump_control c = {
90fd131a 4016 .start = nf_tables_dump_sets_start,
20a69341 4017 .dump = nf_tables_dump_sets,
5b96af77 4018 .done = nf_tables_dump_sets_done,
90fd131a 4019 .data = &ctx,
d9adf22a 4020 .module = THIS_MODULE,
20a69341 4021 };
5b96af77 4022
d9adf22a 4023 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
20a69341
PM
4024 }
4025
c9c8e485
PNA
4026 /* Only accept unspec with dump */
4027 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
4028 return -EAFNOSUPPORT;
eaa2bcd6
PT
4029 if (!nla[NFTA_SET_TABLE])
4030 return -EINVAL;
c9c8e485 4031
cac20fcd 4032 set = nft_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask);
20a69341
PM
4033 if (IS_ERR(set))
4034 return PTR_ERR(set);
4035
d9adf22a 4036 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
20a69341
PM
4037 if (skb2 == NULL)
4038 return -ENOMEM;
4039
4040 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
4041 if (err < 0)
ee921183 4042 goto err_fill_set_info;
20a69341 4043
ee921183 4044 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
20a69341 4045
ee921183 4046err_fill_set_info:
20a69341
PM
4047 kfree_skb(skb2);
4048 return err;
4049}
4050
f3a2181e
SB
4051static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = {
4052 [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 },
4053};
4054
4055static int nft_set_desc_concat_parse(const struct nlattr *attr,
4056 struct nft_set_desc *desc)
4057{
4058 struct nlattr *tb[NFTA_SET_FIELD_MAX + 1];
4059 u32 len;
4060 int err;
4061
4062 err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr,
4063 nft_concat_policy, NULL);
4064 if (err < 0)
4065 return err;
4066
4067 if (!tb[NFTA_SET_FIELD_LEN])
4068 return -EINVAL;
4069
4070 len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN]));
4071
4072 if (len * BITS_PER_BYTE / 32 > NFT_REG32_COUNT)
4073 return -E2BIG;
4074
4075 desc->field_len[desc->field_count++] = len;
4076
4077 return 0;
4078}
4079
4080static int nft_set_desc_concat(struct nft_set_desc *desc,
4081 const struct nlattr *nla)
4082{
4083 struct nlattr *attr;
4084 int rem, err;
4085
4086 nla_for_each_nested(attr, nla, rem) {
4087 if (nla_type(attr) != NFTA_LIST_ELEM)
4088 return -EINVAL;
4089
4090 err = nft_set_desc_concat_parse(attr, desc);
4091 if (err < 0)
4092 return err;
4093 }
4094
4095 return 0;
4096}
4097
f7e840ee 4098static int nf_tables_set_desc_parse(struct nft_set_desc *desc,
c50b960c
PM
4099 const struct nlattr *nla)
4100{
4101 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
4102 int err;
4103
8cb08174
JB
4104 err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla,
4105 nft_set_desc_policy, NULL);
c50b960c
PM
4106 if (err < 0)
4107 return err;
4108
4109 if (da[NFTA_SET_DESC_SIZE] != NULL)
4110 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
f3a2181e
SB
4111 if (da[NFTA_SET_DESC_CONCAT])
4112 err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]);
c50b960c 4113
f3a2181e 4114 return err;
c50b960c
PM
4115}
4116
633c9a84
PNA
4117static int nf_tables_newset(struct net *net, struct sock *nlsk,
4118 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
4119 const struct nlattr * const nla[],
4120 struct netlink_ext_ack *extack)
20a69341
PM
4121{
4122 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 4123 u8 genmask = nft_genmask_next(net);
98319cb9 4124 int family = nfmsg->nfgen_family;
20a69341 4125 const struct nft_set_ops *ops;
65038428 4126 struct nft_expr *expr = NULL;
20a69341
PM
4127 struct nft_table *table;
4128 struct nft_set *set;
4129 struct nft_ctx ctx;
38745490 4130 char *name;
4ef360dd 4131 u64 size;
761da293 4132 u64 timeout;
8aeff920 4133 u32 ktype, dtype, flags, policy, gc_int, objtype;
c50b960c 4134 struct nft_set_desc desc;
e6d8ecac
CFG
4135 unsigned char *udata;
4136 u16 udlen;
20a69341 4137 int err;
f3a2181e 4138 int i;
20a69341
PM
4139
4140 if (nla[NFTA_SET_TABLE] == NULL ||
4141 nla[NFTA_SET_NAME] == NULL ||
958bee14
PNA
4142 nla[NFTA_SET_KEY_LEN] == NULL ||
4143 nla[NFTA_SET_ID] == NULL)
20a69341
PM
4144 return -EINVAL;
4145
c50b960c
PM
4146 memset(&desc, 0, sizeof(desc));
4147
20a69341
PM
4148 ktype = NFT_DATA_VALUE;
4149 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
4150 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
4151 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
4152 return -EINVAL;
4153 }
4154
c50b960c 4155 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
7d740264 4156 if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
20a69341
PM
4157 return -EINVAL;
4158
4159 flags = 0;
4160 if (nla[NFTA_SET_FLAGS] != NULL) {
4161 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
4162 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
7c6c6e95 4163 NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
8aeff920 4164 NFT_SET_MAP | NFT_SET_EVAL |
b4e70d8d 4165 NFT_SET_OBJECT | NFT_SET_CONCAT | NFT_SET_EXPR))
d9583cdf 4166 return -EOPNOTSUPP;
8aeff920 4167 /* Only one of these operations is supported */
acab7131
FW
4168 if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) ==
4169 (NFT_SET_MAP | NFT_SET_OBJECT))
4170 return -EOPNOTSUPP;
4171 if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) ==
4172 (NFT_SET_EVAL | NFT_SET_OBJECT))
7c6c6e95 4173 return -EOPNOTSUPP;
20a69341
PM
4174 }
4175
4176 dtype = 0;
20a69341
PM
4177 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
4178 if (!(flags & NFT_SET_MAP))
4179 return -EINVAL;
4180
4181 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
4182 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
4183 dtype != NFT_DATA_VERDICT)
4184 return -EINVAL;
4185
4186 if (dtype != NFT_DATA_VERDICT) {
4187 if (nla[NFTA_SET_DATA_LEN] == NULL)
4188 return -EINVAL;
c50b960c 4189 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
7d740264 4190 if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
20a69341
PM
4191 return -EINVAL;
4192 } else
7d740264 4193 desc.dlen = sizeof(struct nft_verdict);
20a69341
PM
4194 } else if (flags & NFT_SET_MAP)
4195 return -EINVAL;
4196
8aeff920
PNA
4197 if (nla[NFTA_SET_OBJ_TYPE] != NULL) {
4198 if (!(flags & NFT_SET_OBJECT))
4199 return -EINVAL;
4200
4201 objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE]));
4202 if (objtype == NFT_OBJECT_UNSPEC ||
4203 objtype > NFT_OBJECT_MAX)
d9583cdf 4204 return -EOPNOTSUPP;
8aeff920
PNA
4205 } else if (flags & NFT_SET_OBJECT)
4206 return -EINVAL;
4207 else
4208 objtype = NFT_OBJECT_UNSPEC;
4209
761da293
PM
4210 timeout = 0;
4211 if (nla[NFTA_SET_TIMEOUT] != NULL) {
4212 if (!(flags & NFT_SET_TIMEOUT))
4213 return -EINVAL;
8e1102d5
FW
4214
4215 err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &timeout);
4216 if (err)
4217 return err;
761da293
PM
4218 }
4219 gc_int = 0;
4220 if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
4221 if (!(flags & NFT_SET_TIMEOUT))
4222 return -EINVAL;
4223 gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
4224 }
4225
c50b960c
PM
4226 policy = NFT_SET_POL_PERFORMANCE;
4227 if (nla[NFTA_SET_POLICY] != NULL)
4228 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
4229
4230 if (nla[NFTA_SET_DESC] != NULL) {
f7e840ee 4231 err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]);
c50b960c
PM
4232 if (err < 0)
4233 return err;
4234 }
4235
48b0ae04 4236 if (nla[NFTA_SET_EXPR] || nla[NFTA_SET_EXPRESSIONS])
d56aab26
PNA
4237 desc.expr = true;
4238
cac20fcd 4239 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask);
36dd1bcc
PNA
4240 if (IS_ERR(table)) {
4241 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
20a69341 4242 return PTR_ERR(table);
36dd1bcc 4243 }
20a69341 4244
98319cb9 4245 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
20a69341 4246
cac20fcd 4247 set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask);
20a69341 4248 if (IS_ERR(set)) {
36dd1bcc
PNA
4249 if (PTR_ERR(set) != -ENOENT) {
4250 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
20a69341 4251 return PTR_ERR(set);
36dd1bcc 4252 }
1a28ad74 4253 } else {
36dd1bcc
PNA
4254 if (nlh->nlmsg_flags & NLM_F_EXCL) {
4255 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
20a69341 4256 return -EEXIST;
36dd1bcc 4257 }
20a69341
PM
4258 if (nlh->nlmsg_flags & NLM_F_REPLACE)
4259 return -EOPNOTSUPP;
36dd1bcc 4260
20a69341
PM
4261 return 0;
4262 }
4263
4264 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
4265 return -ENOENT;
4266
2b664957 4267 ops = nft_select_set_ops(&ctx, nla, &desc, policy);
20a69341
PM
4268 if (IS_ERR(ops))
4269 return PTR_ERR(ops);
4270
e6d8ecac
CFG
4271 udlen = 0;
4272 if (nla[NFTA_SET_USERDATA])
4273 udlen = nla_len(nla[NFTA_SET_USERDATA]);
4274
20a69341
PM
4275 size = 0;
4276 if (ops->privsize != NULL)
347b408d 4277 size = ops->privsize(nla, &desc);
20a69341 4278
1ff75a3e 4279 set = kvzalloc(sizeof(*set) + size + udlen, GFP_KERNEL);
24d19826
FW
4280 if (!set)
4281 return -ENOMEM;
20a69341 4282
38745490
PS
4283 name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL);
4284 if (!name) {
4285 err = -ENOMEM;
65038428 4286 goto err_set_name;
38745490
PS
4287 }
4288
20a69341 4289 err = nf_tables_set_alloc_name(&ctx, set, name);
38745490 4290 kfree(name);
20a69341 4291 if (err < 0)
65038428
PNA
4292 goto err_set_alloc_name;
4293
4294 if (nla[NFTA_SET_EXPR]) {
4295 expr = nft_set_elem_expr_alloc(&ctx, set, nla[NFTA_SET_EXPR]);
4296 if (IS_ERR(expr)) {
4297 err = PTR_ERR(expr);
4298 goto err_set_alloc_name;
4299 }
8cfd9b0f
PNA
4300 set->exprs[0] = expr;
4301 set->num_exprs++;
48b0ae04
PNA
4302 } else if (nla[NFTA_SET_EXPRESSIONS]) {
4303 struct nft_expr *expr;
4304 struct nlattr *tmp;
4305 int left;
4306
b4e70d8d
PNA
4307 if (!(flags & NFT_SET_EXPR)) {
4308 err = -EINVAL;
4309 goto err_set_alloc_name;
4310 }
48b0ae04
PNA
4311 i = 0;
4312 nla_for_each_nested(tmp, nla[NFTA_SET_EXPRESSIONS], left) {
4313 if (i == NFT_SET_EXPR_MAX) {
4314 err = -E2BIG;
4315 goto err_set_init;
4316 }
4317 if (nla_type(tmp) != NFTA_LIST_ELEM) {
4318 err = -EINVAL;
4319 goto err_set_init;
4320 }
4321 expr = nft_set_elem_expr_alloc(&ctx, set, tmp);
4322 if (IS_ERR(expr)) {
4323 err = PTR_ERR(expr);
4324 goto err_set_init;
4325 }
4326 set->exprs[i++] = expr;
4327 set->num_exprs++;
4328 }
65038428 4329 }
20a69341 4330
e6d8ecac
CFG
4331 udata = NULL;
4332 if (udlen) {
4333 udata = set->data + size;
4334 nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen);
4335 }
4336
20a69341 4337 INIT_LIST_HEAD(&set->bindings);
3453c927
PNA
4338 set->table = table;
4339 write_pnet(&set->net, net);
20a69341
PM
4340 set->ops = ops;
4341 set->ktype = ktype;
c50b960c 4342 set->klen = desc.klen;
20a69341 4343 set->dtype = dtype;
8aeff920 4344 set->objtype = objtype;
c50b960c 4345 set->dlen = desc.dlen;
20a69341 4346 set->flags = flags;
c50b960c 4347 set->size = desc.size;
9363dc4b 4348 set->policy = policy;
e6d8ecac
CFG
4349 set->udlen = udlen;
4350 set->udata = udata;
761da293
PM
4351 set->timeout = timeout;
4352 set->gc_int = gc_int;
3ecbfd65 4353 set->handle = nf_tables_alloc_handle(table);
20a69341 4354
f3a2181e
SB
4355 set->field_count = desc.field_count;
4356 for (i = 0; i < desc.field_count; i++)
4357 set->field_len[i] = desc.field_len[i];
4358
c50b960c 4359 err = ops->init(set, &desc, nla);
20a69341 4360 if (err < 0)
65038428 4361 goto err_set_init;
20a69341 4362
958bee14 4363 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
20a69341 4364 if (err < 0)
65038428 4365 goto err_set_trans;
20a69341 4366
e688a7f8 4367 list_add_tail_rcu(&set->list, &table->sets);
4fefee57 4368 table->use++;
20a69341
PM
4369 return 0;
4370
65038428 4371err_set_trans:
c17c3cdf 4372 ops->destroy(set);
65038428 4373err_set_init:
8cfd9b0f
PNA
4374 for (i = 0; i < set->num_exprs; i++)
4375 nft_expr_destroy(&ctx, set->exprs[i]);
65038428 4376err_set_alloc_name:
2f6adf48 4377 kfree(set->name);
65038428 4378err_set_name:
1ff75a3e 4379 kvfree(set);
20a69341
PM
4380 return err;
4381}
4382
0c2a85ed 4383static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
20a69341 4384{
8cfd9b0f
PNA
4385 int i;
4386
273fe3f1
PNA
4387 if (WARN_ON(set->use > 0))
4388 return;
4389
8cfd9b0f
PNA
4390 for (i = 0; i < set->num_exprs; i++)
4391 nft_expr_destroy(ctx, set->exprs[i]);
65038428 4392
20a69341 4393 set->ops->destroy(set);
38745490 4394 kfree(set->name);
1ff75a3e 4395 kvfree(set);
20a69341
PM
4396}
4397
633c9a84
PNA
4398static int nf_tables_delset(struct net *net, struct sock *nlsk,
4399 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
4400 const struct nlattr * const nla[],
4401 struct netlink_ext_ack *extack)
20a69341 4402{
c9c8e485 4403 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 4404 u8 genmask = nft_genmask_next(net);
36dd1bcc 4405 const struct nlattr *attr;
20a69341
PM
4406 struct nft_set *set;
4407 struct nft_ctx ctx;
4408 int err;
4409
ec2c9935
PM
4410 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
4411 return -EAFNOSUPPORT;
20a69341
PM
4412 if (nla[NFTA_SET_TABLE] == NULL)
4413 return -EINVAL;
4414
36dd1bcc
PNA
4415 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack,
4416 genmask);
20a69341
PM
4417 if (err < 0)
4418 return err;
4419
36dd1bcc
PNA
4420 if (nla[NFTA_SET_HANDLE]) {
4421 attr = nla[NFTA_SET_HANDLE];
4422 set = nft_set_lookup_byhandle(ctx.table, attr, genmask);
4423 } else {
4424 attr = nla[NFTA_SET_NAME];
4425 set = nft_set_lookup(ctx.table, attr, genmask);
4426 }
a8278400 4427
36dd1bcc
PNA
4428 if (IS_ERR(set)) {
4429 NL_SET_BAD_ATTR(extack, attr);
4430 return PTR_ERR(set);
4431 }
273fe3f1 4432 if (set->use ||
36dd1bcc
PNA
4433 (nlh->nlmsg_flags & NLM_F_NONREC && atomic_read(&set->nelems) > 0)) {
4434 NL_SET_BAD_ATTR(extack, attr);
20a69341 4435 return -EBUSY;
36dd1bcc 4436 }
20a69341 4437
ee01d542 4438 return nft_delset(&ctx, set);
20a69341
PM
4439}
4440
4441static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
de70185d 4442 struct nft_set *set,
20a69341 4443 const struct nft_set_iter *iter,
de70185d 4444 struct nft_set_elem *elem)
20a69341 4445{
fe2811eb 4446 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
20a69341
PM
4447 enum nft_registers dreg;
4448
4449 dreg = nft_type_to_reg(set->dtype);
1ec10212
PM
4450 return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
4451 set->dtype == NFT_DATA_VERDICT ?
4452 NFT_DATA_VERDICT : NFT_DATA_VALUE,
4453 set->dlen);
20a69341
PM
4454}
4455
4456int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
4457 struct nft_set_binding *binding)
4458{
4459 struct nft_set_binding *i;
4460 struct nft_set_iter iter;
4461
273fe3f1
PNA
4462 if (set->use == UINT_MAX)
4463 return -EOVERFLOW;
4464
408070d6 4465 if (!list_empty(&set->bindings) && nft_set_is_anonymous(set))
20a69341
PM
4466 return -EBUSY;
4467
11113e19 4468 if (binding->flags & NFT_SET_MAP) {
20a69341
PM
4469 /* If the set is already bound to the same chain all
4470 * jumps are already validated for that chain.
4471 */
4472 list_for_each_entry(i, &set->bindings, list) {
a4684402 4473 if (i->flags & NFT_SET_MAP &&
11113e19 4474 i->chain == binding->chain)
20a69341
PM
4475 goto bind;
4476 }
4477
8588ac09 4478 iter.genmask = nft_genmask_next(ctx->net);
20a69341
PM
4479 iter.skip = 0;
4480 iter.count = 0;
4481 iter.err = 0;
4482 iter.fn = nf_tables_bind_check_setelem;
4483
4484 set->ops->walk(ctx, set, &iter);
a02f4248 4485 if (iter.err < 0)
20a69341 4486 return iter.err;
20a69341
PM
4487 }
4488bind:
4489 binding->chain = ctx->chain;
e688a7f8 4490 list_add_tail_rcu(&binding->list, &set->bindings);
f6ac8585 4491 nft_set_trans_bind(ctx, set);
273fe3f1 4492 set->use++;
f6ac8585 4493
20a69341
PM
4494 return 0;
4495}
63aea290 4496EXPORT_SYMBOL_GPL(nf_tables_bind_set);
20a69341 4497
3b0a081d
FW
4498static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
4499 struct nft_set_binding *binding, bool event)
20a69341 4500{
e688a7f8 4501 list_del_rcu(&binding->list);
20a69341 4502
f6ac8585 4503 if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) {
cd5125d8 4504 list_del_rcu(&set->list);
f6ac8585
PNA
4505 if (event)
4506 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET,
4507 GFP_KERNEL);
4508 }
20a69341
PM
4509}
4510
273fe3f1
PNA
4511void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
4512 struct nft_set_binding *binding,
4513 enum nft_trans_phase phase)
4514{
4515 switch (phase) {
4516 case NFT_TRANS_PREPARE:
4517 set->use--;
4518 return;
4519 case NFT_TRANS_ABORT:
4520 case NFT_TRANS_RELEASE:
4521 set->use--;
954d8297 4522 fallthrough;
273fe3f1
PNA
4523 default:
4524 nf_tables_unbind_set(ctx, set, binding,
4525 phase == NFT_TRANS_COMMIT);
4526 }
4527}
4528EXPORT_SYMBOL_GPL(nf_tables_deactivate_set);
4529
cd5125d8
FW
4530void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set)
4531{
f6ac8585 4532 if (list_empty(&set->bindings) && nft_set_is_anonymous(set))
0c2a85ed 4533 nft_set_destroy(ctx, set);
cd5125d8
FW
4534}
4535EXPORT_SYMBOL_GPL(nf_tables_destroy_set);
4536
3ac4c07a
PM
4537const struct nft_set_ext_type nft_set_ext_types[] = {
4538 [NFT_SET_EXT_KEY] = {
7d740264 4539 .align = __alignof__(u32),
3ac4c07a
PM
4540 },
4541 [NFT_SET_EXT_DATA] = {
7d740264 4542 .align = __alignof__(u32),
3ac4c07a 4543 },
563125a7
PNA
4544 [NFT_SET_EXT_EXPRESSIONS] = {
4545 .align = __alignof__(struct nft_set_elem_expr),
f25ad2e9 4546 },
8aeff920
PNA
4547 [NFT_SET_EXT_OBJREF] = {
4548 .len = sizeof(struct nft_object *),
4549 .align = __alignof__(struct nft_object *),
4550 },
3ac4c07a
PM
4551 [NFT_SET_EXT_FLAGS] = {
4552 .len = sizeof(u8),
4553 .align = __alignof__(u8),
4554 },
c3e1b005
PM
4555 [NFT_SET_EXT_TIMEOUT] = {
4556 .len = sizeof(u64),
4557 .align = __alignof__(u64),
4558 },
4559 [NFT_SET_EXT_EXPIRATION] = {
8e1102d5
FW
4560 .len = sizeof(u64),
4561 .align = __alignof__(u64),
c3e1b005 4562 },
68e942e8
PM
4563 [NFT_SET_EXT_USERDATA] = {
4564 .len = sizeof(struct nft_userdata),
4565 .align = __alignof__(struct nft_userdata),
4566 },
7b225d0b
PNA
4567 [NFT_SET_EXT_KEY_END] = {
4568 .align = __alignof__(u32),
4569 },
3ac4c07a 4570};
3ac4c07a 4571
20a69341
PM
4572/*
4573 * Set elements
4574 */
4575
4576static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
4577 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
4578 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
4579 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
c3e1b005 4580 [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 },
79ebb5bb 4581 [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 },
68e942e8
PM
4582 [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY,
4583 .len = NFT_USERDATA_MAXLEN },
467697d2 4584 [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED },
9332d27d
FW
4585 [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING,
4586 .len = NFT_OBJ_MAXNAMELEN - 1 },
7b225d0b 4587 [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED },
48b0ae04 4588 [NFTA_SET_ELEM_EXPRESSIONS] = { .type = NLA_NESTED },
20a69341
PM
4589};
4590
4591static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
b2fbd044
LZ
4592 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING,
4593 .len = NFT_TABLE_MAXNAMELEN - 1 },
4594 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING,
4595 .len = NFT_SET_MAXNAMELEN - 1 },
20a69341 4596 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
958bee14 4597 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
20a69341
PM
4598};
4599
633c9a84 4600static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net,
20a69341
PM
4601 const struct sk_buff *skb,
4602 const struct nlmsghdr *nlh,
f2a6d766 4603 const struct nlattr * const nla[],
36dd1bcc 4604 struct netlink_ext_ack *extack,
f2a6d766 4605 u8 genmask)
20a69341
PM
4606{
4607 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
98319cb9 4608 int family = nfmsg->nfgen_family;
7c95f6d8 4609 struct nft_table *table;
20a69341 4610
cac20fcd
PNA
4611 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
4612 genmask);
36dd1bcc
PNA
4613 if (IS_ERR(table)) {
4614 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
20a69341 4615 return PTR_ERR(table);
36dd1bcc 4616 }
20a69341 4617
98319cb9 4618 nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla);
20a69341
PM
4619 return 0;
4620}
4621
563125a7
PNA
4622static int nft_set_elem_expr_dump(struct sk_buff *skb,
4623 const struct nft_set *set,
4624 const struct nft_set_ext *ext)
4625{
4626 struct nft_set_elem_expr *elem_expr;
4627 u32 size, num_exprs = 0;
4628 struct nft_expr *expr;
48b0ae04 4629 struct nlattr *nest;
563125a7
PNA
4630
4631 elem_expr = nft_set_ext_expr(ext);
4632 nft_setelem_expr_foreach(expr, elem_expr, size)
4633 num_exprs++;
4634
4635 if (num_exprs == 1) {
4636 expr = nft_setelem_expr_at(elem_expr, 0);
4637 if (nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, expr) < 0)
4638 return -1;
4639
4640 return 0;
48b0ae04
PNA
4641 } else if (num_exprs > 1) {
4642 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_EXPRESSIONS);
4643 if (nest == NULL)
4644 goto nla_put_failure;
563125a7 4645
48b0ae04
PNA
4646 nft_setelem_expr_foreach(expr, elem_expr, size) {
4647 expr = nft_setelem_expr_at(elem_expr, size);
4648 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0)
4649 goto nla_put_failure;
4650 }
4651 nla_nest_end(skb, nest);
4652 }
563125a7 4653 return 0;
48b0ae04
PNA
4654
4655nla_put_failure:
4656 return -1;
563125a7
PNA
4657}
4658
20a69341
PM
4659static int nf_tables_fill_setelem(struct sk_buff *skb,
4660 const struct nft_set *set,
4661 const struct nft_set_elem *elem)
4662{
fe2811eb 4663 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
20a69341
PM
4664 unsigned char *b = skb_tail_pointer(skb);
4665 struct nlattr *nest;
4666
ae0be8de 4667 nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
20a69341
PM
4668 if (nest == NULL)
4669 goto nla_put_failure;
4670
fe2811eb
PM
4671 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
4672 NFT_DATA_VALUE, set->klen) < 0)
20a69341
PM
4673 goto nla_put_failure;
4674
7b225d0b
PNA
4675 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) &&
4676 nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext),
4677 NFT_DATA_VALUE, set->klen) < 0)
4678 goto nla_put_failure;
4679
fe2811eb
PM
4680 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
4681 nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
20a69341
PM
4682 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
4683 set->dlen) < 0)
4684 goto nla_put_failure;
4685
563125a7
PNA
4686 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS) &&
4687 nft_set_elem_expr_dump(skb, set, ext))
f25ad2e9
PM
4688 goto nla_put_failure;
4689
8aeff920
PNA
4690 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
4691 nla_put_string(skb, NFTA_SET_ELEM_OBJREF,
d152159b 4692 (*nft_set_ext_obj(ext))->key.name) < 0)
8aeff920
PNA
4693 goto nla_put_failure;
4694
fe2811eb
PM
4695 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
4696 nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
4697 htonl(*nft_set_ext_flags(ext))))
4698 goto nla_put_failure;
20a69341 4699
c3e1b005
PM
4700 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
4701 nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
8e1102d5 4702 nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)),
b46f6ded 4703 NFTA_SET_ELEM_PAD))
c3e1b005
PM
4704 goto nla_put_failure;
4705
4706 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
8e1102d5 4707 u64 expires, now = get_jiffies_64();
c3e1b005
PM
4708
4709 expires = *nft_set_ext_expiration(ext);
8e1102d5 4710 if (time_before64(now, expires))
c3e1b005
PM
4711 expires -= now;
4712 else
4713 expires = 0;
4714
4715 if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
8e1102d5 4716 nf_jiffies64_to_msecs(expires),
b46f6ded 4717 NFTA_SET_ELEM_PAD))
c3e1b005
PM
4718 goto nla_put_failure;
4719 }
4720
68e942e8
PM
4721 if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
4722 struct nft_userdata *udata;
4723
4724 udata = nft_set_ext_userdata(ext);
4725 if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
4726 udata->len + 1, udata->data))
4727 goto nla_put_failure;
4728 }
4729
20a69341
PM
4730 nla_nest_end(skb, nest);
4731 return 0;
4732
4733nla_put_failure:
4734 nlmsg_trim(skb, b);
4735 return -EMSGSIZE;
4736}
4737
4738struct nft_set_dump_args {
4739 const struct netlink_callback *cb;
4740 struct nft_set_iter iter;
4741 struct sk_buff *skb;
4742};
4743
4744static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
de70185d 4745 struct nft_set *set,
20a69341 4746 const struct nft_set_iter *iter,
de70185d 4747 struct nft_set_elem *elem)
20a69341
PM
4748{
4749 struct nft_set_dump_args *args;
4750
4751 args = container_of(iter, struct nft_set_dump_args, iter);
4752 return nf_tables_fill_setelem(args->skb, set, elem);
4753}
4754
fa803605
LZ
4755struct nft_set_dump_ctx {
4756 const struct nft_set *set;
4757 struct nft_ctx ctx;
4758};
4759
20a69341
PM
4760static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
4761{
fa803605 4762 struct nft_set_dump_ctx *dump_ctx = cb->data;
633c9a84 4763 struct net *net = sock_net(skb->sk);
fa803605 4764 struct nft_table *table;
de70185d 4765 struct nft_set *set;
20a69341 4766 struct nft_set_dump_args args;
fa803605 4767 bool set_found = false;
20a69341
PM
4768 struct nfgenmsg *nfmsg;
4769 struct nlmsghdr *nlh;
4770 struct nlattr *nest;
4771 u32 portid, seq;
fa803605 4772 int event;
20a69341 4773
fa803605 4774 rcu_read_lock();
36596dad
PNA
4775 list_for_each_entry_rcu(table, &net->nft.tables, list) {
4776 if (dump_ctx->ctx.family != NFPROTO_UNSPEC &&
98319cb9 4777 dump_ctx->ctx.family != table->family)
fa803605 4778 continue;
20a69341 4779
36596dad
PNA
4780 if (table != dump_ctx->ctx.table)
4781 continue;
20a69341 4782
36596dad
PNA
4783 list_for_each_entry_rcu(set, &table->sets, list) {
4784 if (set == dump_ctx->set) {
4785 set_found = true;
4786 break;
fa803605 4787 }
fa803605
LZ
4788 }
4789 break;
4790 }
4791
4792 if (!set_found) {
4793 rcu_read_unlock();
4794 return -ENOENT;
4795 }
20a69341 4796
dedb67c4 4797 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM);
20a69341
PM
4798 portid = NETLINK_CB(cb->skb).portid;
4799 seq = cb->nlh->nlmsg_seq;
4800
4801 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
4802 NLM_F_MULTI);
4803 if (nlh == NULL)
4804 goto nla_put_failure;
4805
4806 nfmsg = nlmsg_data(nlh);
98319cb9 4807 nfmsg->nfgen_family = table->family;
20a69341 4808 nfmsg->version = NFNETLINK_V0;
fa803605 4809 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
20a69341 4810
fa803605 4811 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name))
20a69341
PM
4812 goto nla_put_failure;
4813 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
4814 goto nla_put_failure;
4815
ae0be8de 4816 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
20a69341
PM
4817 if (nest == NULL)
4818 goto nla_put_failure;
4819
8588ac09
PNA
4820 args.cb = cb;
4821 args.skb = skb;
fa803605 4822 args.iter.genmask = nft_genmask_cur(net);
8588ac09
PNA
4823 args.iter.skip = cb->args[0];
4824 args.iter.count = 0;
4825 args.iter.err = 0;
4826 args.iter.fn = nf_tables_dump_setelem;
fa803605
LZ
4827 set->ops->walk(&dump_ctx->ctx, set, &args.iter);
4828 rcu_read_unlock();
20a69341
PM
4829
4830 nla_nest_end(skb, nest);
4831 nlmsg_end(skb, nlh);
4832
4833 if (args.iter.err && args.iter.err != -EMSGSIZE)
4834 return args.iter.err;
4835 if (args.iter.count == cb->args[0])
4836 return 0;
4837
4838 cb->args[0] = args.iter.count;
4839 return skb->len;
4840
4841nla_put_failure:
fa803605 4842 rcu_read_unlock();
20a69341
PM
4843 return -ENOSPC;
4844}
4845
90fd131a
FW
4846static int nf_tables_dump_set_start(struct netlink_callback *cb)
4847{
4848 struct nft_set_dump_ctx *dump_ctx = cb->data;
4849
4850 cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC);
4851
4852 return cb->data ? 0 : -ENOMEM;
4853}
4854
fa803605
LZ
4855static int nf_tables_dump_set_done(struct netlink_callback *cb)
4856{
4857 kfree(cb->data);
4858 return 0;
4859}
4860
d60ce62f
AB
4861static int nf_tables_fill_setelem_info(struct sk_buff *skb,
4862 const struct nft_ctx *ctx, u32 seq,
4863 u32 portid, int event, u16 flags,
4864 const struct nft_set *set,
4865 const struct nft_set_elem *elem)
4866{
4867 struct nfgenmsg *nfmsg;
4868 struct nlmsghdr *nlh;
4869 struct nlattr *nest;
4870 int err;
4871
dedb67c4 4872 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
d60ce62f
AB
4873 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
4874 flags);
4875 if (nlh == NULL)
4876 goto nla_put_failure;
4877
4878 nfmsg = nlmsg_data(nlh);
36596dad 4879 nfmsg->nfgen_family = ctx->family;
d60ce62f 4880 nfmsg->version = NFNETLINK_V0;
84d7fce6 4881 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
d60ce62f
AB
4882
4883 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
4884 goto nla_put_failure;
4885 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
4886 goto nla_put_failure;
4887
ae0be8de 4888 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
d60ce62f
AB
4889 if (nest == NULL)
4890 goto nla_put_failure;
4891
4892 err = nf_tables_fill_setelem(skb, set, elem);
4893 if (err < 0)
4894 goto nla_put_failure;
4895
4896 nla_nest_end(skb, nest);
4897
053c095a
JB
4898 nlmsg_end(skb, nlh);
4899 return 0;
d60ce62f
AB
4900
4901nla_put_failure:
4902 nlmsg_trim(skb, nlh);
4903 return -1;
4904}
4905
ba0e4d99
PNA
4906static int nft_setelem_parse_flags(const struct nft_set *set,
4907 const struct nlattr *attr, u32 *flags)
4908{
4909 if (attr == NULL)
4910 return 0;
4911
4912 *flags = ntohl(nla_get_be32(attr));
4913 if (*flags & ~NFT_SET_ELEM_INTERVAL_END)
4914 return -EINVAL;
4915 if (!(set->flags & NFT_SET_INTERVAL) &&
4916 *flags & NFT_SET_ELEM_INTERVAL_END)
4917 return -EINVAL;
4918
4919 return 0;
4920}
4921
20a1452c
PNA
4922static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set,
4923 struct nft_data *key, struct nlattr *attr)
4924{
4925 struct nft_data_desc desc;
4926 int err;
4927
4928 err = nft_data_init(ctx, key, NFT_DATA_VALUE_MAXLEN, &desc, attr);
4929 if (err < 0)
4930 return err;
4931
4932 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen) {
4933 nft_data_release(key, desc.type);
4934 return -EINVAL;
4935 }
4936
4937 return 0;
4938}
4939
fdb9c405
PNA
4940static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set,
4941 struct nft_data_desc *desc,
4942 struct nft_data *data,
4943 struct nlattr *attr)
4944{
4945 int err;
4946
4947 err = nft_data_init(ctx, data, NFT_DATA_VALUE_MAXLEN, desc, attr);
4948 if (err < 0)
4949 return err;
4950
4951 if (desc->type != NFT_DATA_VERDICT && desc->len != set->dlen) {
4952 nft_data_release(data, desc->type);
4953 return -EINVAL;
4954 }
4955
4956 return 0;
4957}
4958
ba0e4d99
PNA
4959static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set,
4960 const struct nlattr *attr)
4961{
4962 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
ba0e4d99
PNA
4963 struct nft_set_elem elem;
4964 struct sk_buff *skb;
4965 uint32_t flags = 0;
4966 void *priv;
4967 int err;
4968
8cb08174
JB
4969 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
4970 nft_set_elem_policy, NULL);
ba0e4d99
PNA
4971 if (err < 0)
4972 return err;
4973
4974 if (!nla[NFTA_SET_ELEM_KEY])
4975 return -EINVAL;
4976
4977 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
4978 if (err < 0)
4979 return err;
4980
20a1452c
PNA
4981 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
4982 nla[NFTA_SET_ELEM_KEY]);
ba0e4d99
PNA
4983 if (err < 0)
4984 return err;
4985
7b225d0b
PNA
4986 if (nla[NFTA_SET_ELEM_KEY_END]) {
4987 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
4988 nla[NFTA_SET_ELEM_KEY_END]);
4989 if (err < 0)
4990 return err;
4991 }
4992
ba0e4d99
PNA
4993 priv = set->ops->get(ctx->net, set, &elem, flags);
4994 if (IS_ERR(priv))
4995 return PTR_ERR(priv);
4996
4997 elem.priv = priv;
ba0e4d99
PNA
4998
4999 err = -ENOMEM;
d9adf22a 5000 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
ba0e4d99 5001 if (skb == NULL)
ee921183 5002 return err;
ba0e4d99
PNA
5003
5004 err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid,
5005 NFT_MSG_NEWSETELEM, 0, set, &elem);
5006 if (err < 0)
ee921183 5007 goto err_fill_setelem;
ba0e4d99 5008
ee921183 5009 return nfnetlink_unicast(skb, ctx->net, ctx->portid);
ba0e4d99 5010
ee921183 5011err_fill_setelem:
ba0e4d99 5012 kfree_skb(skb);
ee921183 5013 return err;
ba0e4d99
PNA
5014}
5015
d9adf22a 5016/* called with rcu_read_lock held */
ba0e4d99
PNA
5017static int nf_tables_getsetelem(struct net *net, struct sock *nlsk,
5018 struct sk_buff *skb, const struct nlmsghdr *nlh,
5019 const struct nlattr * const nla[],
5020 struct netlink_ext_ack *extack)
5021{
5022 u8 genmask = nft_genmask_cur(net);
5023 struct nft_set *set;
5024 struct nlattr *attr;
5025 struct nft_ctx ctx;
5026 int rem, err = 0;
5027
36dd1bcc
PNA
5028 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack,
5029 genmask);
ba0e4d99
PNA
5030 if (err < 0)
5031 return err;
5032
cac20fcd 5033 set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
ba0e4d99
PNA
5034 if (IS_ERR(set))
5035 return PTR_ERR(set);
5036
5037 if (nlh->nlmsg_flags & NLM_F_DUMP) {
5038 struct netlink_dump_control c = {
90fd131a 5039 .start = nf_tables_dump_set_start,
ba0e4d99
PNA
5040 .dump = nf_tables_dump_set,
5041 .done = nf_tables_dump_set_done,
d9adf22a 5042 .module = THIS_MODULE,
ba0e4d99 5043 };
90fd131a
FW
5044 struct nft_set_dump_ctx dump_ctx = {
5045 .set = set,
5046 .ctx = ctx,
5047 };
ba0e4d99 5048
90fd131a 5049 c.data = &dump_ctx;
d9adf22a 5050 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
ba0e4d99
PNA
5051 }
5052
5053 if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
5054 return -EINVAL;
5055
5056 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
5057 err = nft_get_set_elem(&ctx, set, attr);
5058 if (err < 0)
5059 break;
5060 }
5061
5062 return err;
5063}
5064
25e94a99
PNA
5065static void nf_tables_setelem_notify(const struct nft_ctx *ctx,
5066 const struct nft_set *set,
5067 const struct nft_set_elem *elem,
5068 int event, u16 flags)
d60ce62f 5069{
128ad332
PNA
5070 struct net *net = ctx->net;
5071 u32 portid = ctx->portid;
d60ce62f
AB
5072 struct sk_buff *skb;
5073 int err;
8e6cf365
RGB
5074 char *buf = kasprintf(GFP_KERNEL, "%s:%llu;%s:%llu",
5075 ctx->table->name, ctx->table->handle,
5076 set->name, set->handle);
5077
5078 audit_log_nfcfg(buf,
5079 ctx->family,
5080 set->handle,
5081 event == NFT_MSG_NEWSETELEM ?
5082 AUDIT_NFT_OP_SETELEM_REGISTER :
14224039
RGB
5083 AUDIT_NFT_OP_SETELEM_UNREGISTER,
5084 GFP_KERNEL);
8e6cf365 5085 kfree(buf);
d60ce62f 5086
128ad332 5087 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
25e94a99 5088 return;
d60ce62f 5089
d60ce62f
AB
5090 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5091 if (skb == NULL)
5092 goto err;
5093
5094 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
5095 set, elem);
5096 if (err < 0) {
5097 kfree_skb(skb);
5098 goto err;
5099 }
5100
67cc570e 5101 nft_notify_enqueue(skb, ctx->report, &ctx->net->nft.notify_list);
25e94a99 5102 return;
d60ce62f 5103err:
25e94a99 5104 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
d60ce62f
AB
5105}
5106
60319eb1
PNA
5107static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
5108 int msg_type,
5109 struct nft_set *set)
5110{
5111 struct nft_trans *trans;
5112
5113 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
5114 if (trans == NULL)
5115 return NULL;
5116
5117 nft_trans_elem_set(trans) = set;
5118 return trans;
5119}
5120
a7fc9368
PNA
5121struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
5122 const struct nft_set *set,
5123 const struct nlattr *attr)
5124{
5125 struct nft_expr *expr;
5126 int err;
5127
5128 expr = nft_expr_init(ctx, attr);
5129 if (IS_ERR(expr))
5130 return expr;
5131
5132 err = -EOPNOTSUPP;
5133 if (!(expr->ops->type->flags & NFT_EXPR_STATEFUL))
5134 goto err_set_elem_expr;
5135
5136 if (expr->ops->type->flags & NFT_EXPR_GC) {
5137 if (set->flags & NFT_SET_TIMEOUT)
5138 goto err_set_elem_expr;
5139 if (!set->ops->gc_init)
5140 goto err_set_elem_expr;
5141 set->ops->gc_init(set);
5142 }
5143
5144 return expr;
5145
5146err_set_elem_expr:
5147 nft_expr_destroy(ctx, expr);
5148 return ERR_PTR(err);
5149}
5150
22fe54d5
PM
5151void *nft_set_elem_init(const struct nft_set *set,
5152 const struct nft_set_ext_tmpl *tmpl,
7b225d0b
PNA
5153 const u32 *key, const u32 *key_end,
5154 const u32 *data, u64 timeout, u64 expiration, gfp_t gfp)
fe2811eb
PM
5155{
5156 struct nft_set_ext *ext;
5157 void *elem;
5158
5159 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
5160 if (elem == NULL)
5161 return NULL;
5162
5163 ext = nft_set_elem_ext(set, elem);
5164 nft_set_ext_init(ext, tmpl);
5165
5166 memcpy(nft_set_ext_key(ext), key, set->klen);
7b225d0b
PNA
5167 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END))
5168 memcpy(nft_set_ext_key_end(ext), key_end, set->klen);
fe2811eb
PM
5169 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
5170 memcpy(nft_set_ext_data(ext), data, set->dlen);
79ebb5bb
LGL
5171 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
5172 *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration;
5173 if (expiration == 0)
5174 *nft_set_ext_expiration(ext) += timeout;
5175 }
c3e1b005
PM
5176 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
5177 *nft_set_ext_timeout(ext) = timeout;
fe2811eb
PM
5178
5179 return elem;
5180}
5181
563125a7
PNA
5182static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
5183 struct nft_expr *expr)
475beb9c
PNA
5184{
5185 if (expr->ops->destroy_clone) {
5186 expr->ops->destroy_clone(ctx, expr);
5187 module_put(expr->ops->type->owner);
5188 } else {
5189 nf_tables_expr_destroy(ctx, expr);
5190 }
5191}
5192
563125a7
PNA
5193static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
5194 struct nft_set_elem_expr *elem_expr)
5195{
5196 struct nft_expr *expr;
5197 u32 size;
5198
5199 nft_setelem_expr_foreach(expr, elem_expr, size)
5200 __nft_set_elem_expr_destroy(ctx, expr);
5201}
5202
61f9e292
LZ
5203void nft_set_elem_destroy(const struct nft_set *set, void *elem,
5204 bool destroy_expr)
61edafbb
PM
5205{
5206 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
3453c927
PNA
5207 struct nft_ctx ctx = {
5208 .net = read_pnet(&set->net),
5209 .family = set->table->family,
5210 };
61edafbb 5211
59105446 5212 nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE);
61edafbb 5213 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
59105446 5214 nft_data_release(nft_set_ext_data(ext), set->dtype);
563125a7 5215 if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
475beb9c 5216 nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext));
371ebcbb 5217
8aeff920
PNA
5218 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
5219 (*nft_set_ext_obj(ext))->use--;
61edafbb
PM
5220 kfree(elem);
5221}
5222EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
5223
59105446
PNA
5224/* Only called from commit path, nft_set_elem_deactivate() already deals with
5225 * the refcounting from the preparation phase.
5226 */
3453c927
PNA
5227static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
5228 const struct nft_set *set, void *elem)
59105446
PNA
5229{
5230 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
5231
563125a7 5232 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
475beb9c
PNA
5233 nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext));
5234
59105446
PNA
5235 kfree(elem);
5236}
5237
8cfd9b0f
PNA
5238static int nft_set_elem_expr_clone(const struct nft_ctx *ctx,
5239 struct nft_set *set,
5240 struct nft_expr *expr_array[])
5241{
5242 struct nft_expr *expr;
5243 int err, i, k;
5244
5245 for (i = 0; i < set->num_exprs; i++) {
5246 expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL);
5247 if (!expr)
5248 goto err_expr;
5249
5250 err = nft_expr_clone(expr, set->exprs[i]);
5251 if (err < 0) {
5252 nft_expr_destroy(ctx, expr);
5253 goto err_expr;
5254 }
5255 expr_array[i] = expr;
5256 }
5257
5258 return 0;
5259
5260err_expr:
161b838e
CIK
5261 for (k = i - 1; k >= 0; k--)
5262 nft_expr_destroy(ctx, expr_array[k]);
8cfd9b0f
PNA
5263
5264 return -ENOMEM;
5265}
5266
563125a7
PNA
5267static void nft_set_elem_expr_setup(const struct nft_set_ext *ext, int i,
5268 struct nft_expr *expr_array[])
5269{
5270 struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext);
5271 struct nft_expr *expr = nft_setelem_expr_at(elem_expr, elem_expr->size);
5272
5273 memcpy(expr, expr_array[i], expr_array[i]->ops->size);
5274 elem_expr->size += expr_array[i]->ops->size;
5275 kfree(expr_array[i]);
5276 expr_array[i] = NULL;
5277}
5278
60319eb1 5279static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
c016c7e4 5280 const struct nlattr *attr, u32 nlmsg_flags)
20a69341 5281{
8cfd9b0f 5282 struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {};
20a69341 5283 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
8aeff920 5284 u8 genmask = nft_genmask_next(ctx->net);
fe2811eb 5285 struct nft_set_ext_tmpl tmpl;
c016c7e4 5286 struct nft_set_ext *ext, *ext2;
20a69341
PM
5287 struct nft_set_elem elem;
5288 struct nft_set_binding *binding;
8aeff920 5289 struct nft_object *obj = NULL;
68e942e8 5290 struct nft_userdata *udata;
20a1452c 5291 struct nft_data_desc desc;
20a69341 5292 enum nft_registers dreg;
60319eb1 5293 struct nft_trans *trans;
563125a7 5294 u32 flags = 0, size = 0;
c3e1b005 5295 u64 timeout;
79ebb5bb 5296 u64 expiration;
8cfd9b0f 5297 int err, i;
68e942e8 5298 u8 ulen;
20a69341 5299
8cb08174
JB
5300 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
5301 nft_set_elem_policy, NULL);
20a69341
PM
5302 if (err < 0)
5303 return err;
5304
5305 if (nla[NFTA_SET_ELEM_KEY] == NULL)
5306 return -EINVAL;
5307
fe2811eb
PM
5308 nft_set_ext_prepare(&tmpl);
5309
0e9091d6
PNA
5310 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
5311 if (err < 0)
5312 return err;
5313 if (flags != 0)
5314 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
20a69341
PM
5315
5316 if (set->flags & NFT_SET_MAP) {
5317 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
fe2811eb 5318 !(flags & NFT_SET_ELEM_INTERVAL_END))
20a69341
PM
5319 return -EINVAL;
5320 } else {
5321 if (nla[NFTA_SET_ELEM_DATA] != NULL)
5322 return -EINVAL;
5323 }
5324
bffc124b
PNA
5325 if ((flags & NFT_SET_ELEM_INTERVAL_END) &&
5326 (nla[NFTA_SET_ELEM_DATA] ||
5327 nla[NFTA_SET_ELEM_OBJREF] ||
5328 nla[NFTA_SET_ELEM_TIMEOUT] ||
5329 nla[NFTA_SET_ELEM_EXPIRATION] ||
5330 nla[NFTA_SET_ELEM_USERDATA] ||
48b0ae04
PNA
5331 nla[NFTA_SET_ELEM_EXPR] ||
5332 nla[NFTA_SET_ELEM_EXPRESSIONS]))
bffc124b
PNA
5333 return -EINVAL;
5334
c3e1b005
PM
5335 timeout = 0;
5336 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
5337 if (!(set->flags & NFT_SET_TIMEOUT))
5338 return -EINVAL;
8e1102d5
FW
5339 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT],
5340 &timeout);
5341 if (err)
5342 return err;
c3e1b005
PM
5343 } else if (set->flags & NFT_SET_TIMEOUT) {
5344 timeout = set->timeout;
5345 }
5346
79ebb5bb
LGL
5347 expiration = 0;
5348 if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) {
5349 if (!(set->flags & NFT_SET_TIMEOUT))
5350 return -EINVAL;
5351 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION],
5352 &expiration);
5353 if (err)
5354 return err;
5355 }
5356
8cfd9b0f
PNA
5357 if (nla[NFTA_SET_ELEM_EXPR]) {
5358 struct nft_expr *expr;
5359
5360 if (set->num_exprs != 1)
5361 return -EOPNOTSUPP;
5362
40944452
PNA
5363 expr = nft_set_elem_expr_alloc(ctx, set,
5364 nla[NFTA_SET_ELEM_EXPR]);
5365 if (IS_ERR(expr))
5366 return PTR_ERR(expr);
65038428 5367
8cfd9b0f 5368 expr_array[0] = expr;
65038428 5369
8cfd9b0f
PNA
5370 if (set->exprs[0] && set->exprs[0]->ops != expr->ops) {
5371 err = -EOPNOTSUPP;
65038428 5372 goto err_set_elem_expr;
8cfd9b0f 5373 }
48b0ae04
PNA
5374 } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) {
5375 struct nft_expr *expr;
5376 struct nlattr *tmp;
5377 int left;
65038428 5378
48b0ae04
PNA
5379 if (set->num_exprs == 0)
5380 return -EOPNOTSUPP;
5381
5382 i = 0;
5383 nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) {
5384 if (i == set->num_exprs) {
5385 err = -E2BIG;
5386 goto err_set_elem_expr;
5387 }
5388 if (nla_type(tmp) != NFTA_LIST_ELEM) {
5389 err = -EINVAL;
5390 goto err_set_elem_expr;
5391 }
5392 expr = nft_set_elem_expr_alloc(ctx, set, tmp);
5393 if (IS_ERR(expr)) {
5394 err = PTR_ERR(expr);
5395 goto err_set_elem_expr;
5396 }
5397 expr_array[i] = expr;
5398
5399 if (expr->ops != set->exprs[i]->ops) {
5400 err = -EOPNOTSUPP;
5401 goto err_set_elem_expr;
5402 }
5403 i++;
5404 }
5405 if (set->num_exprs != i) {
5406 err = -EOPNOTSUPP;
65038428 5407 goto err_set_elem_expr;
48b0ae04 5408 }
8cfd9b0f
PNA
5409 } else if (set->num_exprs > 0) {
5410 err = nft_set_elem_expr_clone(ctx, set, expr_array);
5411 if (err < 0)
5412 goto err_set_elem_expr_clone;
40944452
PNA
5413 }
5414
20a1452c
PNA
5415 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
5416 nla[NFTA_SET_ELEM_KEY]);
20a69341 5417 if (err < 0)
40944452 5418 goto err_set_elem_expr;
20a69341 5419
20a1452c 5420 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
7b225d0b
PNA
5421
5422 if (nla[NFTA_SET_ELEM_KEY_END]) {
5423 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
5424 nla[NFTA_SET_ELEM_KEY_END]);
5425 if (err < 0)
5426 goto err_parse_key;
5427
5428 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
5429 }
5430
c3e1b005
PM
5431 if (timeout > 0) {
5432 nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
5433 if (timeout != set->timeout)
5434 nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
5435 }
fe2811eb 5436
563125a7
PNA
5437 if (set->num_exprs) {
5438 for (i = 0; i < set->num_exprs; i++)
5439 size += expr_array[i]->ops->size;
5440
5441 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS,
5442 sizeof(struct nft_set_elem_expr) +
5443 size);
5444 }
40944452 5445
8aeff920
PNA
5446 if (nla[NFTA_SET_ELEM_OBJREF] != NULL) {
5447 if (!(set->flags & NFT_SET_OBJECT)) {
5448 err = -EINVAL;
7b225d0b 5449 goto err_parse_key_end;
8aeff920 5450 }
4d44175a
FW
5451 obj = nft_obj_lookup(ctx->net, ctx->table,
5452 nla[NFTA_SET_ELEM_OBJREF],
cac20fcd 5453 set->objtype, genmask);
8aeff920
PNA
5454 if (IS_ERR(obj)) {
5455 err = PTR_ERR(obj);
7b225d0b 5456 goto err_parse_key_end;
8aeff920
PNA
5457 }
5458 nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF);
5459 }
5460
20a69341 5461 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
fdb9c405
PNA
5462 err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val,
5463 nla[NFTA_SET_ELEM_DATA]);
20a69341 5464 if (err < 0)
7b225d0b 5465 goto err_parse_key_end;
20a69341 5466
20a69341
PM
5467 dreg = nft_type_to_reg(set->dtype);
5468 list_for_each_entry(binding, &set->bindings, list) {
5469 struct nft_ctx bind_ctx = {
58c78e10 5470 .net = ctx->net,
36596dad 5471 .family = ctx->family,
20a69341 5472 .table = ctx->table,
7c95f6d8 5473 .chain = (struct nft_chain *)binding->chain,
20a69341
PM
5474 };
5475
11113e19
PM
5476 if (!(binding->flags & NFT_SET_MAP))
5477 continue;
5478
1ec10212 5479 err = nft_validate_register_store(&bind_ctx, dreg,
fdb9c405 5480 &elem.data.val,
20a1452c 5481 desc.type, desc.len);
20a69341 5482 if (err < 0)
7b225d0b 5483 goto err_parse_data;
a654de8f 5484
20a1452c 5485 if (desc.type == NFT_DATA_VERDICT &&
fdb9c405
PNA
5486 (elem.data.val.verdict.code == NFT_GOTO ||
5487 elem.data.val.verdict.code == NFT_JUMP))
a654de8f
PNA
5488 nft_validate_state_update(ctx->net,
5489 NFT_VALIDATE_NEED);
20a69341 5490 }
fe2811eb 5491
20a1452c 5492 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len);
20a69341
PM
5493 }
5494
68e942e8
PM
5495 /* The full maximum length of userdata can exceed the maximum
5496 * offset value (U8_MAX) for following extensions, therefor it
5497 * must be the last extension added.
5498 */
5499 ulen = 0;
5500 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
5501 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
5502 if (ulen > 0)
5503 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
5504 ulen);
5505 }
5506
fe2811eb 5507 err = -ENOMEM;
7b225d0b 5508 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
fdb9c405 5509 elem.key_end.val.data, elem.data.val.data,
79ebb5bb 5510 timeout, expiration, GFP_KERNEL);
fe2811eb 5511 if (elem.priv == NULL)
7b225d0b 5512 goto err_parse_data;
fe2811eb
PM
5513
5514 ext = nft_set_elem_ext(set, elem.priv);
5515 if (flags)
5516 *nft_set_ext_flags(ext) = flags;
68e942e8
PM
5517 if (ulen > 0) {
5518 udata = nft_set_ext_userdata(ext);
5519 udata->len = ulen - 1;
5520 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
5521 }
8aeff920
PNA
5522 if (obj) {
5523 *nft_set_ext_obj(ext) = obj;
5524 obj->use++;
5525 }
563125a7
PNA
5526 for (i = 0; i < set->num_exprs; i++)
5527 nft_set_elem_expr_setup(ext, i, expr_array);
fe2811eb 5528
60319eb1
PNA
5529 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
5530 if (trans == NULL)
7b225d0b 5531 goto err_trans;
60319eb1 5532
69086658 5533 ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
c016c7e4
PNA
5534 err = set->ops->insert(ctx->net, set, &elem, &ext2);
5535 if (err) {
5536 if (err == -EEXIST) {
9744a6fc
PNA
5537 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^
5538 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) ||
5539 nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^
77a92189 5540 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF))
7b225d0b 5541 goto err_element_clash;
8aeff920
PNA
5542 if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
5543 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) &&
5544 memcmp(nft_set_ext_data(ext),
5545 nft_set_ext_data(ext2), set->dlen) != 0) ||
5546 (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
5547 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) &&
5548 *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2)))
77a92189 5549 goto err_element_clash;
c016c7e4
PNA
5550 else if (!(nlmsg_flags & NLM_F_EXCL))
5551 err = 0;
8c2d45b2
PNA
5552 } else if (err == -ENOTEMPTY) {
5553 /* ENOTEMPTY reports overlapping between this element
5554 * and an existing one.
5555 */
5556 err = -EEXIST;
c016c7e4 5557 }
7b225d0b 5558 goto err_element_clash;
c016c7e4 5559 }
20a69341 5560
35d0ac90
PNA
5561 if (set->size &&
5562 !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) {
5563 err = -ENFILE;
7b225d0b 5564 goto err_set_full;
35d0ac90
PNA
5565 }
5566
60319eb1 5567 nft_trans_elem(trans) = elem;
46bbafce 5568 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
20a69341
PM
5569 return 0;
5570
7b225d0b 5571err_set_full:
5cb82a38 5572 set->ops->remove(ctx->net, set, &elem);
7b225d0b 5573err_element_clash:
60319eb1 5574 kfree(trans);
7b225d0b 5575err_trans:
b91d9036
TY
5576 if (obj)
5577 obj->use--;
475beb9c
PNA
5578
5579 nf_tables_set_elem_destroy(ctx, set, elem.priv);
7b225d0b 5580err_parse_data:
20a69341 5581 if (nla[NFTA_SET_ELEM_DATA] != NULL)
fdb9c405 5582 nft_data_release(&elem.data.val, desc.type);
7b225d0b
PNA
5583err_parse_key_end:
5584 nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
5585err_parse_key:
20a1452c 5586 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
40944452 5587err_set_elem_expr:
8cfd9b0f
PNA
5588 for (i = 0; i < set->num_exprs && expr_array[i]; i++)
5589 nft_expr_destroy(ctx, expr_array[i]);
5590err_set_elem_expr_clone:
20a69341
PM
5591 return err;
5592}
5593
633c9a84
PNA
5594static int nf_tables_newsetelem(struct net *net, struct sock *nlsk,
5595 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
5596 const struct nlattr * const nla[],
5597 struct netlink_ext_ack *extack)
20a69341 5598{
f2a6d766 5599 u8 genmask = nft_genmask_next(net);
20a69341
PM
5600 const struct nlattr *attr;
5601 struct nft_set *set;
5602 struct nft_ctx ctx;
a654de8f 5603 int rem, err;
20a69341 5604
7d5570ca
PNA
5605 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
5606 return -EINVAL;
5607
36dd1bcc
PNA
5608 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack,
5609 genmask);
20a69341
PM
5610 if (err < 0)
5611 return err;
5612
a3073c17
PNA
5613 set = nft_set_lookup_global(net, ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
5614 nla[NFTA_SET_ELEM_LIST_SET_ID], genmask);
5615 if (IS_ERR(set))
5616 return PTR_ERR(set);
958bee14 5617
20a69341
PM
5618 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
5619 return -EBUSY;
5620
5621 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
c016c7e4 5622 err = nft_add_set_elem(&ctx, set, attr, nlh->nlmsg_flags);
35d0ac90 5623 if (err < 0)
a654de8f 5624 return err;
20a69341 5625 }
a654de8f
PNA
5626
5627 if (net->nft.validate_state == NFT_VALIDATE_DO)
5628 return nft_table_validate(net, ctx.table);
5629
5630 return 0;
20a69341
PM
5631}
5632
59105446
PNA
5633/**
5634 * nft_data_hold - hold a nft_data item
5635 *
5636 * @data: struct nft_data to release
5637 * @type: type of data
5638 *
5639 * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded,
5640 * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and
5641 * NFT_GOTO verdicts. This function must be called on active data objects
5642 * from the second phase of the commit protocol.
5643 */
bb7b40ae 5644void nft_data_hold(const struct nft_data *data, enum nft_data_types type)
59105446 5645{
d0e2c7de
PNA
5646 struct nft_chain *chain;
5647 struct nft_rule *rule;
5648
59105446
PNA
5649 if (type == NFT_DATA_VERDICT) {
5650 switch (data->verdict.code) {
5651 case NFT_JUMP:
5652 case NFT_GOTO:
d0e2c7de
PNA
5653 chain = data->verdict.chain;
5654 chain->use++;
5655
5656 if (!nft_chain_is_bound(chain))
5657 break;
5658
5659 chain->table->use++;
5660 list_for_each_entry(rule, &chain->rules, list)
5661 chain->use++;
5662
5663 nft_chain_add(chain->table, chain);
59105446
PNA
5664 break;
5665 }
5666 }
5667}
5668
5669static void nft_set_elem_activate(const struct net *net,
5670 const struct nft_set *set,
5671 struct nft_set_elem *elem)
5672{
5673 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5674
5675 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
5676 nft_data_hold(nft_set_ext_data(ext), set->dtype);
5677 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
5678 (*nft_set_ext_obj(ext))->use++;
5679}
5680
5681static void nft_set_elem_deactivate(const struct net *net,
5682 const struct nft_set *set,
5683 struct nft_set_elem *elem)
5684{
5685 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5686
5687 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
5688 nft_data_release(nft_set_ext_data(ext), set->dtype);
5689 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
5690 (*nft_set_ext_obj(ext))->use--;
5691}
5692
60319eb1 5693static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
20a69341
PM
5694 const struct nlattr *attr)
5695{
5696 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3971ca14 5697 struct nft_set_ext_tmpl tmpl;
20a69341 5698 struct nft_set_elem elem;
3971ca14 5699 struct nft_set_ext *ext;
60319eb1 5700 struct nft_trans *trans;
3971ca14
PNA
5701 u32 flags = 0;
5702 void *priv;
20a69341
PM
5703 int err;
5704
8cb08174
JB
5705 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
5706 nft_set_elem_policy, NULL);
20a69341 5707 if (err < 0)
20a1452c 5708 return err;
20a69341 5709
20a69341 5710 if (nla[NFTA_SET_ELEM_KEY] == NULL)
20a1452c 5711 return -EINVAL;
20a69341 5712
3971ca14
PNA
5713 nft_set_ext_prepare(&tmpl);
5714
5715 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
5716 if (err < 0)
5717 return err;
5718 if (flags != 0)
5719 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
5720
20a1452c
PNA
5721 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
5722 nla[NFTA_SET_ELEM_KEY]);
20a69341 5723 if (err < 0)
20a1452c 5724 return err;
20a69341 5725
20a1452c 5726 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
3971ca14 5727
7b225d0b
PNA
5728 if (nla[NFTA_SET_ELEM_KEY_END]) {
5729 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
5730 nla[NFTA_SET_ELEM_KEY_END]);
5731 if (err < 0)
5732 return err;
5733
5734 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
5735 }
5736
3971ca14 5737 err = -ENOMEM;
7b225d0b
PNA
5738 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
5739 elem.key_end.val.data, NULL, 0, 0,
5740 GFP_KERNEL);
3971ca14 5741 if (elem.priv == NULL)
20a1452c 5742 goto fail_elem;
3971ca14
PNA
5743
5744 ext = nft_set_elem_ext(set, elem.priv);
5745 if (flags)
5746 *nft_set_ext_flags(ext) = flags;
5747
60319eb1 5748 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
20a1452c
PNA
5749 if (trans == NULL)
5750 goto fail_trans;
20a69341 5751
42a55769 5752 priv = set->ops->deactivate(ctx->net, set, &elem);
3971ca14 5753 if (priv == NULL) {
cc02e457 5754 err = -ENOENT;
20a1452c 5755 goto fail_ops;
cc02e457 5756 }
3971ca14
PNA
5757 kfree(elem.priv);
5758 elem.priv = priv;
cc02e457 5759
59105446
PNA
5760 nft_set_elem_deactivate(ctx->net, set, &elem);
5761
60319eb1 5762 nft_trans_elem(trans) = elem;
46bbafce 5763 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
0dc13625 5764 return 0;
cc02e457 5765
20a1452c 5766fail_ops:
cc02e457 5767 kfree(trans);
20a1452c 5768fail_trans:
3971ca14 5769 kfree(elem.priv);
20a1452c
PNA
5770fail_elem:
5771 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
20a69341
PM
5772 return err;
5773}
5774
8411b644 5775static int nft_flush_set(const struct nft_ctx *ctx,
de70185d 5776 struct nft_set *set,
8411b644 5777 const struct nft_set_iter *iter,
de70185d 5778 struct nft_set_elem *elem)
8411b644
PNA
5779{
5780 struct nft_trans *trans;
5781 int err;
5782
5783 trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
5784 sizeof(struct nft_trans_elem), GFP_ATOMIC);
5785 if (!trans)
5786 return -ENOMEM;
5787
1ba1c414 5788 if (!set->ops->flush(ctx->net, set, elem->priv)) {
8411b644
PNA
5789 err = -ENOENT;
5790 goto err1;
5791 }
b2c11e4b 5792 set->ndeact++;
8411b644 5793
7acfda53 5794 nft_set_elem_deactivate(ctx->net, set, elem);
de70185d
PNA
5795 nft_trans_elem_set(trans) = set;
5796 nft_trans_elem(trans) = *elem;
8411b644
PNA
5797 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
5798
5799 return 0;
5800err1:
5801 kfree(trans);
5802 return err;
5803}
5804
633c9a84
PNA
5805static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,
5806 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
5807 const struct nlattr * const nla[],
5808 struct netlink_ext_ack *extack)
20a69341 5809{
f2a6d766 5810 u8 genmask = nft_genmask_next(net);
20a69341
PM
5811 const struct nlattr *attr;
5812 struct nft_set *set;
5813 struct nft_ctx ctx;
60319eb1 5814 int rem, err = 0;
20a69341 5815
36dd1bcc
PNA
5816 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack,
5817 genmask);
20a69341
PM
5818 if (err < 0)
5819 return err;
5820
cac20fcd 5821 set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
20a69341
PM
5822 if (IS_ERR(set))
5823 return PTR_ERR(set);
5824 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
5825 return -EBUSY;
5826
8411b644 5827 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) {
baa2d42c
PNA
5828 struct nft_set_iter iter = {
5829 .genmask = genmask,
5830 .fn = nft_flush_set,
8411b644 5831 };
baa2d42c 5832 set->ops->walk(&ctx, set, &iter);
8411b644 5833
baa2d42c 5834 return iter.err;
8411b644
PNA
5835 }
5836
20a69341
PM
5837 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
5838 err = nft_del_setelem(&ctx, set, attr);
5839 if (err < 0)
60319eb1 5840 break;
4fefee57 5841
3dd0673a 5842 set->ndeact++;
20a69341 5843 }
60319eb1 5844 return err;
20a69341
PM
5845}
5846
cfed7e1b
PM
5847void nft_set_gc_batch_release(struct rcu_head *rcu)
5848{
5849 struct nft_set_gc_batch *gcb;
5850 unsigned int i;
5851
5852 gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu);
5853 for (i = 0; i < gcb->head.cnt; i++)
61f9e292 5854 nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true);
cfed7e1b
PM
5855 kfree(gcb);
5856}
cfed7e1b
PM
5857
5858struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
5859 gfp_t gfp)
5860{
5861 struct nft_set_gc_batch *gcb;
5862
5863 gcb = kzalloc(sizeof(*gcb), gfp);
5864 if (gcb == NULL)
5865 return gcb;
5866 gcb->head.set = set;
5867 return gcb;
5868}
cfed7e1b 5869
e5009240
PNA
5870/*
5871 * Stateful objects
5872 */
5873
5874/**
5875 * nft_register_obj- register nf_tables stateful object type
3db86c39 5876 * @obj_type: object type
e5009240
PNA
5877 *
5878 * Registers the object type for use with nf_tables. Returns zero on
5879 * success or a negative errno code otherwise.
5880 */
5881int nft_register_obj(struct nft_object_type *obj_type)
5882{
5883 if (obj_type->type == NFT_OBJECT_UNSPEC)
5884 return -EINVAL;
5885
5886 nfnl_lock(NFNL_SUBSYS_NFTABLES);
5887 list_add_rcu(&obj_type->list, &nf_tables_objects);
5888 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
5889 return 0;
5890}
5891EXPORT_SYMBOL_GPL(nft_register_obj);
5892
5893/**
5894 * nft_unregister_obj - unregister nf_tables object type
3db86c39 5895 * @obj_type: object type
e5009240
PNA
5896 *
5897 * Unregisters the object type for use with nf_tables.
5898 */
5899void nft_unregister_obj(struct nft_object_type *obj_type)
5900{
5901 nfnl_lock(NFNL_SUBSYS_NFTABLES);
5902 list_del_rcu(&obj_type->list);
5903 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
5904}
5905EXPORT_SYMBOL_GPL(nft_unregister_obj);
5906
4d44175a
FW
5907struct nft_object *nft_obj_lookup(const struct net *net,
5908 const struct nft_table *table,
cac20fcd
PNA
5909 const struct nlattr *nla, u32 objtype,
5910 u8 genmask)
e5009240 5911{
4d44175a
FW
5912 struct nft_object_hash_key k = { .table = table };
5913 char search[NFT_OBJ_MAXNAMELEN];
5914 struct rhlist_head *tmp, *list;
e5009240
PNA
5915 struct nft_object *obj;
5916
872f6903 5917 nla_strscpy(search, nla, sizeof(search));
4d44175a
FW
5918 k.name = search;
5919
5920 WARN_ON_ONCE(!rcu_read_lock_held() &&
5921 !lockdep_commit_lock_is_held(net));
5922
5923 rcu_read_lock();
5924 list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params);
5925 if (!list)
5926 goto out;
5927
5928 rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) {
5929 if (objtype == obj->ops->type->type &&
5930 nft_active_genmask(obj, genmask)) {
5931 rcu_read_unlock();
e5009240 5932 return obj;
4d44175a 5933 }
e5009240 5934 }
4d44175a
FW
5935out:
5936 rcu_read_unlock();
e5009240
PNA
5937 return ERR_PTR(-ENOENT);
5938}
cac20fcd 5939EXPORT_SYMBOL_GPL(nft_obj_lookup);
e5009240 5940
cac20fcd
PNA
5941static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table,
5942 const struct nlattr *nla,
5943 u32 objtype, u8 genmask)
3ecbfd65
HS
5944{
5945 struct nft_object *obj;
5946
5947 list_for_each_entry(obj, &table->objects, list) {
5948 if (be64_to_cpu(nla_get_be64(nla)) == obj->handle &&
5949 objtype == obj->ops->type->type &&
5950 nft_active_genmask(obj, genmask))
5951 return obj;
5952 }
5953 return ERR_PTR(-ENOENT);
5954}
5955
e5009240 5956static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = {
b2fbd044
LZ
5957 [NFTA_OBJ_TABLE] = { .type = NLA_STRING,
5958 .len = NFT_TABLE_MAXNAMELEN - 1 },
5959 [NFTA_OBJ_NAME] = { .type = NLA_STRING,
5960 .len = NFT_OBJ_MAXNAMELEN - 1 },
e5009240
PNA
5961 [NFTA_OBJ_TYPE] = { .type = NLA_U32 },
5962 [NFTA_OBJ_DATA] = { .type = NLA_NESTED },
3ecbfd65 5963 [NFTA_OBJ_HANDLE] = { .type = NLA_U64},
b131c964
JGG
5964 [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY,
5965 .len = NFT_USERDATA_MAXLEN },
e5009240
PNA
5966};
5967
84fba055
FW
5968static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
5969 const struct nft_object_type *type,
e5009240
PNA
5970 const struct nlattr *attr)
5971{
5b4c6e38 5972 struct nlattr **tb;
dfc46034 5973 const struct nft_object_ops *ops;
e5009240 5974 struct nft_object *obj;
5b4c6e38
GS
5975 int err = -ENOMEM;
5976
5977 tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL);
5978 if (!tb)
5979 goto err1;
e5009240
PNA
5980
5981 if (attr) {
8cb08174
JB
5982 err = nla_parse_nested_deprecated(tb, type->maxattr, attr,
5983 type->policy, NULL);
e5009240 5984 if (err < 0)
5b4c6e38 5985 goto err2;
e5009240
PNA
5986 } else {
5987 memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1));
5988 }
5989
dfc46034
PBG
5990 if (type->select_ops) {
5991 ops = type->select_ops(ctx, (const struct nlattr * const *)tb);
5992 if (IS_ERR(ops)) {
5993 err = PTR_ERR(ops);
5b4c6e38 5994 goto err2;
dfc46034
PBG
5995 }
5996 } else {
5997 ops = type->ops;
5998 }
5999
e5009240 6000 err = -ENOMEM;
dfc46034 6001 obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL);
5b4c6e38
GS
6002 if (!obj)
6003 goto err2;
e5009240 6004
dfc46034 6005 err = ops->init(ctx, (const struct nlattr * const *)tb, obj);
e5009240 6006 if (err < 0)
5b4c6e38 6007 goto err3;
e5009240 6008
dfc46034
PBG
6009 obj->ops = ops;
6010
5b4c6e38 6011 kfree(tb);
e5009240 6012 return obj;
5b4c6e38 6013err3:
e5009240 6014 kfree(obj);
5b4c6e38
GS
6015err2:
6016 kfree(tb);
e5009240
PNA
6017err1:
6018 return ERR_PTR(err);
6019}
6020
6021static int nft_object_dump(struct sk_buff *skb, unsigned int attr,
43da04a5 6022 struct nft_object *obj, bool reset)
e5009240
PNA
6023{
6024 struct nlattr *nest;
6025
ae0be8de 6026 nest = nla_nest_start_noflag(skb, attr);
e5009240
PNA
6027 if (!nest)
6028 goto nla_put_failure;
dfc46034 6029 if (obj->ops->dump(skb, obj, reset) < 0)
e5009240
PNA
6030 goto nla_put_failure;
6031 nla_nest_end(skb, nest);
6032 return 0;
6033
6034nla_put_failure:
6035 return -1;
6036}
6037
6038static const struct nft_object_type *__nft_obj_type_get(u32 objtype)
6039{
6040 const struct nft_object_type *type;
6041
6042 list_for_each_entry(type, &nf_tables_objects, list) {
6043 if (objtype == type->type)
6044 return type;
6045 }
6046 return NULL;
6047}
6048
452238e8
FW
6049static const struct nft_object_type *
6050nft_obj_type_get(struct net *net, u32 objtype)
e5009240
PNA
6051{
6052 const struct nft_object_type *type;
6053
6054 type = __nft_obj_type_get(objtype);
6055 if (type != NULL && try_module_get(type->owner))
6056 return type;
6057
f102d66b 6058 lockdep_nfnl_nft_mutex_not_held();
e5009240
PNA
6059#ifdef CONFIG_MODULES
6060 if (type == NULL) {
eb014de4 6061 if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN)
e5009240
PNA
6062 return ERR_PTR(-EAGAIN);
6063 }
6064#endif
6065 return ERR_PTR(-ENOENT);
6066}
6067
d62d0ba9
FFM
6068static int nf_tables_updobj(const struct nft_ctx *ctx,
6069 const struct nft_object_type *type,
6070 const struct nlattr *attr,
6071 struct nft_object *obj)
6072{
6073 struct nft_object *newobj;
6074 struct nft_trans *trans;
6075 int err;
6076
6077 trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ,
6078 sizeof(struct nft_trans_obj));
6079 if (!trans)
6080 return -ENOMEM;
6081
6082 newobj = nft_obj_init(ctx, type, attr);
6083 if (IS_ERR(newobj)) {
6084 err = PTR_ERR(newobj);
b74ae961 6085 goto err_free_trans;
d62d0ba9
FFM
6086 }
6087
6088 nft_trans_obj(trans) = obj;
6089 nft_trans_obj_update(trans) = true;
6090 nft_trans_obj_newobj(trans) = newobj;
6091 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
6092
6093 return 0;
b74ae961
DC
6094
6095err_free_trans:
d62d0ba9 6096 kfree(trans);
d62d0ba9
FFM
6097 return err;
6098}
6099
e5009240
PNA
6100static int nf_tables_newobj(struct net *net, struct sock *nlsk,
6101 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
6102 const struct nlattr * const nla[],
6103 struct netlink_ext_ack *extack)
e5009240
PNA
6104{
6105 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
6106 const struct nft_object_type *type;
6107 u8 genmask = nft_genmask_next(net);
6108 int family = nfmsg->nfgen_family;
e5009240
PNA
6109 struct nft_table *table;
6110 struct nft_object *obj;
6111 struct nft_ctx ctx;
6112 u32 objtype;
6113 int err;
6114
6115 if (!nla[NFTA_OBJ_TYPE] ||
6116 !nla[NFTA_OBJ_NAME] ||
6117 !nla[NFTA_OBJ_DATA])
6118 return -EINVAL;
6119
cac20fcd 6120 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask);
36dd1bcc
PNA
6121 if (IS_ERR(table)) {
6122 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
e5009240 6123 return PTR_ERR(table);
36dd1bcc 6124 }
e5009240
PNA
6125
6126 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
4d44175a 6127 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
e5009240
PNA
6128 if (IS_ERR(obj)) {
6129 err = PTR_ERR(obj);
36dd1bcc
PNA
6130 if (err != -ENOENT) {
6131 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
e5009240 6132 return err;
36dd1bcc 6133 }
1a28ad74 6134 } else {
36dd1bcc
PNA
6135 if (nlh->nlmsg_flags & NLM_F_EXCL) {
6136 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
e5009240 6137 return -EEXIST;
36dd1bcc 6138 }
d62d0ba9
FFM
6139 if (nlh->nlmsg_flags & NLM_F_REPLACE)
6140 return -EOPNOTSUPP;
6141
fd57d0cb 6142 type = __nft_obj_type_get(objtype);
d62d0ba9
FFM
6143 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
6144
6145 return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj);
e5009240
PNA
6146 }
6147
98319cb9 6148 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
e5009240 6149
452238e8 6150 type = nft_obj_type_get(net, objtype);
e5009240
PNA
6151 if (IS_ERR(type))
6152 return PTR_ERR(type);
6153
84fba055 6154 obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]);
e5009240
PNA
6155 if (IS_ERR(obj)) {
6156 err = PTR_ERR(obj);
b131c964 6157 goto err_init;
e5009240 6158 }
d152159b 6159 obj->key.table = table;
3ecbfd65
HS
6160 obj->handle = nf_tables_alloc_handle(table);
6161
d152159b
FW
6162 obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL);
6163 if (!obj->key.name) {
61509575 6164 err = -ENOMEM;
b131c964
JGG
6165 goto err_strdup;
6166 }
6167
6168 if (nla[NFTA_OBJ_USERDATA]) {
85db827a 6169 obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL);
b131c964
JGG
6170 if (obj->udata == NULL)
6171 goto err_userdata;
6172
85db827a 6173 obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]);
61509575 6174 }
e5009240
PNA
6175
6176 err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj);
6177 if (err < 0)
b131c964 6178 goto err_trans;
e5009240 6179
4d44175a
FW
6180 err = rhltable_insert(&nft_objname_ht, &obj->rhlhead,
6181 nft_objname_ht_params);
6182 if (err < 0)
b131c964 6183 goto err_obj_ht;
4d44175a 6184
e5009240
PNA
6185 list_add_tail_rcu(&obj->list, &table->objects);
6186 table->use++;
6187 return 0;
b131c964 6188err_obj_ht:
4d44175a
FW
6189 /* queued in transaction log */
6190 INIT_LIST_HEAD(&obj->list);
6191 return err;
b131c964 6192err_trans:
d152159b 6193 kfree(obj->key.name);
b131c964
JGG
6194err_userdata:
6195 kfree(obj->udata);
6196err_strdup:
dfc46034 6197 if (obj->ops->destroy)
00bfb320 6198 obj->ops->destroy(&ctx, obj);
e5009240 6199 kfree(obj);
b131c964 6200err_init:
e5009240
PNA
6201 module_put(type->owner);
6202 return err;
6203}
6204
6205static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net,
6206 u32 portid, u32 seq, int event, u32 flags,
6207 int family, const struct nft_table *table,
43da04a5 6208 struct nft_object *obj, bool reset)
e5009240
PNA
6209{
6210 struct nfgenmsg *nfmsg;
6211 struct nlmsghdr *nlh;
6212
dedb67c4 6213 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
e5009240
PNA
6214 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
6215 if (nlh == NULL)
6216 goto nla_put_failure;
6217
6218 nfmsg = nlmsg_data(nlh);
6219 nfmsg->nfgen_family = family;
6220 nfmsg->version = NFNETLINK_V0;
6221 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
6222
6223 if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) ||
d152159b 6224 nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) ||
dfc46034 6225 nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) ||
e5009240 6226 nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) ||
3ecbfd65
HS
6227 nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) ||
6228 nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle),
6229 NFTA_OBJ_PAD))
e5009240
PNA
6230 goto nla_put_failure;
6231
b131c964
JGG
6232 if (obj->udata &&
6233 nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata))
6234 goto nla_put_failure;
6235
e5009240
PNA
6236 nlmsg_end(skb, nlh);
6237 return 0;
6238
6239nla_put_failure:
6240 nlmsg_trim(skb, nlh);
6241 return -1;
6242}
6243
a9fea2a3 6244struct nft_obj_filter {
e46abbcc 6245 char *table;
a9fea2a3
PNA
6246 u32 type;
6247};
6248
e5009240
PNA
6249static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)
6250{
6251 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
e5009240 6252 const struct nft_table *table;
e5009240 6253 unsigned int idx = 0, s_idx = cb->args[0];
a9fea2a3 6254 struct nft_obj_filter *filter = cb->data;
e5009240
PNA
6255 struct net *net = sock_net(skb->sk);
6256 int family = nfmsg->nfgen_family;
43da04a5
PNA
6257 struct nft_object *obj;
6258 bool reset = false;
6259
6260 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
6261 reset = true;
e5009240
PNA
6262
6263 rcu_read_lock();
6264 cb->seq = net->nft.base_seq;
6265
36596dad 6266 list_for_each_entry_rcu(table, &net->nft.tables, list) {
98319cb9 6267 if (family != NFPROTO_UNSPEC && family != table->family)
e5009240
PNA
6268 continue;
6269
36596dad
PNA
6270 list_for_each_entry_rcu(obj, &table->objects, list) {
6271 if (!nft_is_active(net, obj))
6272 goto cont;
6273 if (idx < s_idx)
6274 goto cont;
6275 if (idx > s_idx)
6276 memset(&cb->args[1], 0,
6277 sizeof(cb->args) - sizeof(cb->args[0]));
360cc79d 6278 if (filter && filter->table &&
36596dad
PNA
6279 strcmp(filter->table, table->name))
6280 goto cont;
6281 if (filter &&
6282 filter->type != NFT_OBJECT_UNSPEC &&
6283 obj->ops->type->type != filter->type)
6284 goto cont;
a9fea2a3 6285
8e6cf365 6286 if (reset) {
68df2ed5 6287 char *buf = kasprintf(GFP_ATOMIC,
8e6cf365
RGB
6288 "%s:%llu;?:0",
6289 table->name,
6290 table->handle);
6291
6292 audit_log_nfcfg(buf,
6293 family,
6294 obj->handle,
14224039 6295 AUDIT_NFT_OP_OBJ_RESET,
68df2ed5 6296 GFP_ATOMIC);
8e6cf365
RGB
6297 kfree(buf);
6298 }
6299
36596dad
PNA
6300 if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid,
6301 cb->nlh->nlmsg_seq,
6302 NFT_MSG_NEWOBJ,
6303 NLM_F_MULTI | NLM_F_APPEND,
98319cb9 6304 table->family, table,
36596dad
PNA
6305 obj, reset) < 0)
6306 goto done;
e5009240 6307
36596dad 6308 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
e5009240 6309cont:
36596dad 6310 idx++;
e5009240
PNA
6311 }
6312 }
6313done:
6314 rcu_read_unlock();
6315
6316 cb->args[0] = idx;
6317 return skb->len;
6318}
6319
90fd131a 6320static int nf_tables_dump_obj_start(struct netlink_callback *cb)
a9fea2a3 6321{
90fd131a
FW
6322 const struct nlattr * const *nla = cb->data;
6323 struct nft_obj_filter *filter = NULL;
e46abbcc 6324
90fd131a
FW
6325 if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) {
6326 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
6327 if (!filter)
6328 return -ENOMEM;
6329
6330 if (nla[NFTA_OBJ_TABLE]) {
6331 filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC);
6332 if (!filter->table) {
6333 kfree(filter);
6334 return -ENOMEM;
6335 }
6336 }
6337
6338 if (nla[NFTA_OBJ_TYPE])
6339 filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
8bea728d 6340 }
a9fea2a3 6341
90fd131a 6342 cb->data = filter;
a9fea2a3
PNA
6343 return 0;
6344}
6345
90fd131a 6346static int nf_tables_dump_obj_done(struct netlink_callback *cb)
a9fea2a3 6347{
90fd131a 6348 struct nft_obj_filter *filter = cb->data;
a9fea2a3 6349
90fd131a
FW
6350 if (filter) {
6351 kfree(filter->table);
6352 kfree(filter);
e46abbcc 6353 }
a9fea2a3 6354
90fd131a 6355 return 0;
a9fea2a3
PNA
6356}
6357
d9adf22a 6358/* called with rcu_read_lock held */
e5009240
PNA
6359static int nf_tables_getobj(struct net *net, struct sock *nlsk,
6360 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
6361 const struct nlattr * const nla[],
6362 struct netlink_ext_ack *extack)
e5009240
PNA
6363{
6364 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
6365 u8 genmask = nft_genmask_cur(net);
6366 int family = nfmsg->nfgen_family;
e5009240
PNA
6367 const struct nft_table *table;
6368 struct nft_object *obj;
6369 struct sk_buff *skb2;
43da04a5 6370 bool reset = false;
e5009240
PNA
6371 u32 objtype;
6372 int err;
6373
6374 if (nlh->nlmsg_flags & NLM_F_DUMP) {
6375 struct netlink_dump_control c = {
90fd131a 6376 .start = nf_tables_dump_obj_start,
e5009240 6377 .dump = nf_tables_dump_obj,
a9fea2a3 6378 .done = nf_tables_dump_obj_done,
d9adf22a 6379 .module = THIS_MODULE,
90fd131a 6380 .data = (void *)nla,
e5009240 6381 };
a9fea2a3 6382
d9adf22a 6383 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
e5009240
PNA
6384 }
6385
6386 if (!nla[NFTA_OBJ_NAME] ||
6387 !nla[NFTA_OBJ_TYPE])
6388 return -EINVAL;
6389
cac20fcd 6390 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask);
36dd1bcc
PNA
6391 if (IS_ERR(table)) {
6392 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
e5009240 6393 return PTR_ERR(table);
36dd1bcc 6394 }
e5009240
PNA
6395
6396 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
4d44175a 6397 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
36dd1bcc
PNA
6398 if (IS_ERR(obj)) {
6399 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
e5009240 6400 return PTR_ERR(obj);
36dd1bcc 6401 }
e5009240 6402
d9adf22a 6403 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
e5009240
PNA
6404 if (!skb2)
6405 return -ENOMEM;
6406
43da04a5
PNA
6407 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
6408 reset = true;
6409
8e6cf365 6410 if (reset) {
14224039 6411 char *buf = kasprintf(GFP_ATOMIC, "%s:%llu;?:0",
8e6cf365
RGB
6412 table->name, table->handle);
6413
6414 audit_log_nfcfg(buf,
6415 family,
6416 obj->handle,
14224039 6417 AUDIT_NFT_OP_OBJ_RESET,
68df2ed5 6418 GFP_ATOMIC);
8e6cf365
RGB
6419 kfree(buf);
6420 }
6421
e5009240
PNA
6422 err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid,
6423 nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0,
43da04a5 6424 family, table, obj, reset);
e5009240 6425 if (err < 0)
ee921183 6426 goto err_fill_obj_info;
e5009240 6427
ee921183
PNA
6428 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
6429
6430err_fill_obj_info:
e5009240
PNA
6431 kfree_skb(skb2);
6432 return err;
e5009240
PNA
6433}
6434
00bfb320 6435static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj)
e5009240 6436{
dfc46034 6437 if (obj->ops->destroy)
00bfb320 6438 obj->ops->destroy(ctx, obj);
e5009240 6439
dfc46034 6440 module_put(obj->ops->type->owner);
d152159b 6441 kfree(obj->key.name);
bc7a7082 6442 kfree(obj->udata);
e5009240
PNA
6443 kfree(obj);
6444}
6445
6446static int nf_tables_delobj(struct net *net, struct sock *nlsk,
04ba724b
PNA
6447 struct sk_buff *skb, const struct nlmsghdr *nlh,
6448 const struct nlattr * const nla[],
6449 struct netlink_ext_ack *extack)
e5009240
PNA
6450{
6451 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
6452 u8 genmask = nft_genmask_next(net);
6453 int family = nfmsg->nfgen_family;
36dd1bcc 6454 const struct nlattr *attr;
e5009240
PNA
6455 struct nft_table *table;
6456 struct nft_object *obj;
6457 struct nft_ctx ctx;
6458 u32 objtype;
6459
6460 if (!nla[NFTA_OBJ_TYPE] ||
3ecbfd65 6461 (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE]))
e5009240
PNA
6462 return -EINVAL;
6463
cac20fcd 6464 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask);
36dd1bcc
PNA
6465 if (IS_ERR(table)) {
6466 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
e5009240 6467 return PTR_ERR(table);
36dd1bcc 6468 }
e5009240
PNA
6469
6470 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
36dd1bcc
PNA
6471 if (nla[NFTA_OBJ_HANDLE]) {
6472 attr = nla[NFTA_OBJ_HANDLE];
6473 obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask);
6474 } else {
6475 attr = nla[NFTA_OBJ_NAME];
4d44175a 6476 obj = nft_obj_lookup(net, table, attr, objtype, genmask);
36dd1bcc
PNA
6477 }
6478
6479 if (IS_ERR(obj)) {
6480 NL_SET_BAD_ATTR(extack, attr);
e5009240 6481 return PTR_ERR(obj);
36dd1bcc
PNA
6482 }
6483 if (obj->use > 0) {
6484 NL_SET_BAD_ATTR(extack, attr);
e5009240 6485 return -EBUSY;
36dd1bcc 6486 }
e5009240 6487
98319cb9 6488 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
e5009240
PNA
6489
6490 return nft_delobj(&ctx, obj);
6491}
6492
d152159b 6493void nft_obj_notify(struct net *net, const struct nft_table *table,
25e94a99
PNA
6494 struct nft_object *obj, u32 portid, u32 seq, int event,
6495 int family, int report, gfp_t gfp)
e5009240
PNA
6496{
6497 struct sk_buff *skb;
6498 int err;
14224039 6499 char *buf = kasprintf(gfp, "%s:%llu;?:0",
8e6cf365
RGB
6500 table->name, table->handle);
6501
6502 audit_log_nfcfg(buf,
6503 family,
6504 obj->handle,
6505 event == NFT_MSG_NEWOBJ ?
6506 AUDIT_NFT_OP_OBJ_REGISTER :
14224039 6507 AUDIT_NFT_OP_OBJ_UNREGISTER,
68df2ed5 6508 gfp);
8e6cf365 6509 kfree(buf);
e5009240 6510
2599e989
PNA
6511 if (!report &&
6512 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
25e94a99 6513 return;
e5009240 6514
2599e989 6515 skb = nlmsg_new(NLMSG_GOODSIZE, gfp);
e5009240
PNA
6516 if (skb == NULL)
6517 goto err;
6518
2599e989
PNA
6519 err = nf_tables_fill_obj_info(skb, net, portid, seq, event, 0, family,
6520 table, obj, false);
e5009240
PNA
6521 if (err < 0) {
6522 kfree_skb(skb);
6523 goto err;
6524 }
6525
67cc570e 6526 nft_notify_enqueue(skb, report, &net->nft.notify_list);
25e94a99 6527 return;
e5009240 6528err:
25e94a99 6529 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
e5009240 6530}
2599e989
PNA
6531EXPORT_SYMBOL_GPL(nft_obj_notify);
6532
25e94a99
PNA
6533static void nf_tables_obj_notify(const struct nft_ctx *ctx,
6534 struct nft_object *obj, int event)
2599e989 6535{
25e94a99 6536 nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event,
36596dad 6537 ctx->family, ctx->report, GFP_KERNEL);
2599e989 6538}
e5009240 6539
3b49e2e9
PNA
6540/*
6541 * Flow tables
6542 */
6543void nft_register_flowtable_type(struct nf_flowtable_type *type)
6544{
6545 nfnl_lock(NFNL_SUBSYS_NFTABLES);
6546 list_add_tail_rcu(&type->list, &nf_tables_flowtables);
6547 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
6548}
6549EXPORT_SYMBOL_GPL(nft_register_flowtable_type);
6550
6551void nft_unregister_flowtable_type(struct nf_flowtable_type *type)
6552{
6553 nfnl_lock(NFNL_SUBSYS_NFTABLES);
6554 list_del_rcu(&type->list);
6555 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
6556}
6557EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type);
6558
6559static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = {
6560 [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING,
6561 .len = NFT_NAME_MAXLEN - 1 },
6562 [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING,
6563 .len = NFT_NAME_MAXLEN - 1 },
6564 [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED },
3ecbfd65 6565 [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 },
8bb69f3b 6566 [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 },
3b49e2e9
PNA
6567};
6568
cac20fcd
PNA
6569struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
6570 const struct nlattr *nla, u8 genmask)
3b49e2e9
PNA
6571{
6572 struct nft_flowtable *flowtable;
6573
d9adf22a 6574 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
3b49e2e9
PNA
6575 if (!nla_strcmp(nla, flowtable->name) &&
6576 nft_active_genmask(flowtable, genmask))
6577 return flowtable;
6578 }
6579 return ERR_PTR(-ENOENT);
6580}
cac20fcd 6581EXPORT_SYMBOL_GPL(nft_flowtable_lookup);
3b49e2e9 6582
9b05b6e1
LGL
6583void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
6584 struct nft_flowtable *flowtable,
6585 enum nft_trans_phase phase)
6586{
6587 switch (phase) {
6588 case NFT_TRANS_PREPARE:
6589 case NFT_TRANS_ABORT:
6590 case NFT_TRANS_RELEASE:
6591 flowtable->use--;
954d8297 6592 fallthrough;
9b05b6e1
LGL
6593 default:
6594 return;
6595 }
6596}
6597EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable);
6598
ae0662f8 6599static struct nft_flowtable *
cac20fcd
PNA
6600nft_flowtable_lookup_byhandle(const struct nft_table *table,
6601 const struct nlattr *nla, u8 genmask)
3ecbfd65
HS
6602{
6603 struct nft_flowtable *flowtable;
6604
6605 list_for_each_entry(flowtable, &table->flowtables, list) {
6606 if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle &&
6607 nft_active_genmask(flowtable, genmask))
6608 return flowtable;
6609 }
6610 return ERR_PTR(-ENOENT);
6611}
6612
d9246a53
PNA
6613struct nft_flowtable_hook {
6614 u32 num;
6615 int priority;
6616 struct list_head list;
6617};
6618
3b49e2e9
PNA
6619static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = {
6620 [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 },
6621 [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 },
6622 [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED },
6623};
6624
d9246a53
PNA
6625static int nft_flowtable_parse_hook(const struct nft_ctx *ctx,
6626 const struct nlattr *attr,
6627 struct nft_flowtable_hook *flowtable_hook,
5b6743fb 6628 struct nft_flowtable *flowtable, bool add)
3b49e2e9 6629{
3b49e2e9 6630 struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1];
3f0465a9 6631 struct nft_hook *hook;
3b49e2e9 6632 int hooknum, priority;
3f0465a9 6633 int err;
3b49e2e9 6634
d9246a53
PNA
6635 INIT_LIST_HEAD(&flowtable_hook->list);
6636
8cb08174
JB
6637 err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, attr,
6638 nft_flowtable_hook_policy, NULL);
3b49e2e9
PNA
6639 if (err < 0)
6640 return err;
6641
5b6743fb
PNA
6642 if (add) {
6643 if (!tb[NFTA_FLOWTABLE_HOOK_NUM] ||
6644 !tb[NFTA_FLOWTABLE_HOOK_PRIORITY])
6645 return -EINVAL;
3b49e2e9 6646
5b6743fb
PNA
6647 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
6648 if (hooknum != NF_NETDEV_INGRESS)
6649 return -EOPNOTSUPP;
6650
6651 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
6652
6653 flowtable_hook->priority = priority;
6654 flowtable_hook->num = hooknum;
6655 } else {
6656 if (tb[NFTA_FLOWTABLE_HOOK_NUM]) {
6657 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
6658 if (hooknum != flowtable->hooknum)
6659 return -EOPNOTSUPP;
6660 }
6661
6662 if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) {
6663 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
6664 if (priority != flowtable->data.priority)
6665 return -EOPNOTSUPP;
6666 }
3b49e2e9 6667
5b6743fb
PNA
6668 flowtable_hook->priority = flowtable->data.priority;
6669 flowtable_hook->num = flowtable->hooknum;
6670 }
3b49e2e9 6671
05abe445
PNA
6672 if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) {
6673 err = nf_tables_parse_netdev_hooks(ctx->net,
6674 tb[NFTA_FLOWTABLE_HOOK_DEVS],
6675 &flowtable_hook->list);
6676 if (err < 0)
6677 return err;
6678 }
3b49e2e9 6679
d9246a53 6680 list_for_each_entry(hook, &flowtable_hook->list, list) {
3f0465a9 6681 hook->ops.pf = NFPROTO_NETDEV;
5b6743fb
PNA
6682 hook->ops.hooknum = flowtable_hook->num;
6683 hook->ops.priority = flowtable_hook->priority;
6684 hook->ops.priv = &flowtable->data;
6685 hook->ops.hook = flowtable->data.type->hook;
3b49e2e9
PNA
6686 }
6687
3b49e2e9
PNA
6688 return err;
6689}
6690
98319cb9 6691static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family)
3b49e2e9
PNA
6692{
6693 const struct nf_flowtable_type *type;
6694
6695 list_for_each_entry(type, &nf_tables_flowtables, list) {
98319cb9 6696 if (family == type->family)
3b49e2e9
PNA
6697 return type;
6698 }
6699 return NULL;
6700}
6701
452238e8
FW
6702static const struct nf_flowtable_type *
6703nft_flowtable_type_get(struct net *net, u8 family)
3b49e2e9
PNA
6704{
6705 const struct nf_flowtable_type *type;
6706
98319cb9 6707 type = __nft_flowtable_type_get(family);
3b49e2e9
PNA
6708 if (type != NULL && try_module_get(type->owner))
6709 return type;
6710
f102d66b 6711 lockdep_nfnl_nft_mutex_not_held();
3b49e2e9
PNA
6712#ifdef CONFIG_MODULES
6713 if (type == NULL) {
eb014de4 6714 if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN)
3b49e2e9
PNA
6715 return ERR_PTR(-EAGAIN);
6716 }
6717#endif
6718 return ERR_PTR(-ENOENT);
6719}
6720
5acab914 6721/* Only called from error and netdev event paths. */
ff4bf2f4
PNA
6722static void nft_unregister_flowtable_hook(struct net *net,
6723 struct nft_flowtable *flowtable,
6724 struct nft_hook *hook)
6725{
6726 nf_unregister_net_hook(net, &hook->ops);
6727 flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
6728 FLOW_BLOCK_UNBIND);
6729}
6730
3b49e2e9 6731static void nft_unregister_flowtable_net_hooks(struct net *net,
f9382669 6732 struct list_head *hook_list)
3b49e2e9 6733{
3f0465a9 6734 struct nft_hook *hook;
3b49e2e9 6735
f9382669 6736 list_for_each_entry(hook, hook_list, list)
5acab914 6737 nf_unregister_net_hook(net, &hook->ops);
3f0465a9
PNA
6738}
6739
6740static int nft_register_flowtable_net_hooks(struct net *net,
6741 struct nft_table *table,
f9382669 6742 struct list_head *hook_list,
3f0465a9
PNA
6743 struct nft_flowtable *flowtable)
6744{
6745 struct nft_hook *hook, *hook2, *next;
6746 struct nft_flowtable *ft;
6747 int err, i = 0;
6748
f9382669 6749 list_for_each_entry(hook, hook_list, list) {
3f0465a9
PNA
6750 list_for_each_entry(ft, &table->flowtables, list) {
6751 list_for_each_entry(hook2, &ft->hook_list, list) {
6752 if (hook->ops.dev == hook2->ops.dev &&
6753 hook->ops.pf == hook2->ops.pf) {
77a92189 6754 err = -EEXIST;
3f0465a9
PNA
6755 goto err_unregister_net_hooks;
6756 }
6757 }
6758 }
3b49e2e9 6759
d7c03a9f 6760 err = flowtable->data.type->setup(&flowtable->data,
6761 hook->ops.dev,
6762 FLOW_BLOCK_BIND);
3f0465a9
PNA
6763 if (err < 0)
6764 goto err_unregister_net_hooks;
6765
d7c03a9f 6766 err = nf_register_net_hook(net, &hook->ops);
6767 if (err < 0) {
6768 flowtable->data.type->setup(&flowtable->data,
6769 hook->ops.dev,
6770 FLOW_BLOCK_UNBIND);
6771 goto err_unregister_net_hooks;
6772 }
6773
3f0465a9 6774 i++;
3b49e2e9 6775 }
3f0465a9
PNA
6776
6777 return 0;
6778
6779err_unregister_net_hooks:
f9382669 6780 list_for_each_entry_safe(hook, next, hook_list, list) {
3f0465a9
PNA
6781 if (i-- <= 0)
6782 break;
6783
ff4bf2f4 6784 nft_unregister_flowtable_hook(net, flowtable, hook);
3f0465a9
PNA
6785 list_del_rcu(&hook->list);
6786 kfree_rcu(hook, rcu);
6787 }
6788
6789 return err;
3b49e2e9
PNA
6790}
6791
389a2cbc
PNA
6792static void nft_flowtable_hooks_destroy(struct list_head *hook_list)
6793{
6794 struct nft_hook *hook, *next;
6795
6796 list_for_each_entry_safe(hook, next, hook_list, list) {
6797 list_del_rcu(&hook->list);
6798 kfree_rcu(hook, rcu);
6799 }
6800}
6801
78d9f48f
PNA
6802static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh,
6803 struct nft_flowtable *flowtable)
6804{
6805 const struct nlattr * const *nla = ctx->nla;
6806 struct nft_flowtable_hook flowtable_hook;
6807 struct nft_hook *hook, *next;
6808 struct nft_trans *trans;
6809 bool unregister = false;
6810 int err;
6811
6812 err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK],
5b6743fb 6813 &flowtable_hook, flowtable, false);
78d9f48f
PNA
6814 if (err < 0)
6815 return err;
6816
6817 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
6818 if (nft_hook_list_find(&flowtable->hook_list, hook)) {
6819 list_del(&hook->list);
6820 kfree(hook);
6821 }
6822 }
6823
6824 err = nft_register_flowtable_net_hooks(ctx->net, ctx->table,
6825 &flowtable_hook.list, flowtable);
6826 if (err < 0)
6827 goto err_flowtable_update_hook;
6828
6829 trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE,
6830 sizeof(struct nft_trans_flowtable));
6831 if (!trans) {
6832 unregister = true;
6833 err = -ENOMEM;
6834 goto err_flowtable_update_hook;
6835 }
6836
6837 nft_trans_flowtable(trans) = flowtable;
6838 nft_trans_flowtable_update(trans) = true;
6839 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
6840 list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans));
6841
6842 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
6843
6844 return 0;
6845
6846err_flowtable_update_hook:
6847 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
6848 if (unregister)
6849 nft_unregister_flowtable_hook(ctx->net, flowtable, hook);
6850 list_del_rcu(&hook->list);
6851 kfree_rcu(hook, rcu);
6852 }
6853
6854 return err;
6855
6856}
6857
3b49e2e9
PNA
6858static int nf_tables_newflowtable(struct net *net, struct sock *nlsk,
6859 struct sk_buff *skb,
6860 const struct nlmsghdr *nlh,
6861 const struct nlattr * const nla[],
6862 struct netlink_ext_ack *extack)
6863{
6864 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
d9246a53 6865 struct nft_flowtable_hook flowtable_hook;
3b49e2e9
PNA
6866 const struct nf_flowtable_type *type;
6867 u8 genmask = nft_genmask_next(net);
6868 int family = nfmsg->nfgen_family;
3f0465a9
PNA
6869 struct nft_flowtable *flowtable;
6870 struct nft_hook *hook, *next;
3b49e2e9
PNA
6871 struct nft_table *table;
6872 struct nft_ctx ctx;
3f0465a9 6873 int err;
3b49e2e9
PNA
6874
6875 if (!nla[NFTA_FLOWTABLE_TABLE] ||
6876 !nla[NFTA_FLOWTABLE_NAME] ||
6877 !nla[NFTA_FLOWTABLE_HOOK])
6878 return -EINVAL;
6879
cac20fcd
PNA
6880 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
6881 genmask);
36dd1bcc
PNA
6882 if (IS_ERR(table)) {
6883 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
3b49e2e9 6884 return PTR_ERR(table);
36dd1bcc 6885 }
3b49e2e9 6886
cac20fcd
PNA
6887 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
6888 genmask);
3b49e2e9
PNA
6889 if (IS_ERR(flowtable)) {
6890 err = PTR_ERR(flowtable);
36dd1bcc
PNA
6891 if (err != -ENOENT) {
6892 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
3b49e2e9 6893 return err;
36dd1bcc 6894 }
3b49e2e9 6895 } else {
36dd1bcc
PNA
6896 if (nlh->nlmsg_flags & NLM_F_EXCL) {
6897 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
3b49e2e9 6898 return -EEXIST;
36dd1bcc 6899 }
3b49e2e9 6900
78d9f48f
PNA
6901 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
6902
6903 return nft_flowtable_update(&ctx, nlh, flowtable);
3b49e2e9
PNA
6904 }
6905
98319cb9 6906 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
3b49e2e9
PNA
6907
6908 flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL);
6909 if (!flowtable)
6910 return -ENOMEM;
6911
6912 flowtable->table = table;
3ecbfd65 6913 flowtable->handle = nf_tables_alloc_handle(table);
3f0465a9 6914 INIT_LIST_HEAD(&flowtable->hook_list);
3ecbfd65 6915
3b49e2e9
PNA
6916 flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL);
6917 if (!flowtable->name) {
6918 err = -ENOMEM;
6919 goto err1;
6920 }
6921
452238e8 6922 type = nft_flowtable_type_get(net, family);
3b49e2e9
PNA
6923 if (IS_ERR(type)) {
6924 err = PTR_ERR(type);
6925 goto err2;
6926 }
6927
8bb69f3b
PNA
6928 if (nla[NFTA_FLOWTABLE_FLAGS]) {
6929 flowtable->data.flags =
6930 ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
cfbd1125 6931 if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK)
8bb69f3b
PNA
6932 goto err3;
6933 }
6934
6935 write_pnet(&flowtable->data.net, net);
3b49e2e9 6936 flowtable->data.type = type;
a268de77 6937 err = type->init(&flowtable->data);
3b49e2e9
PNA
6938 if (err < 0)
6939 goto err3;
6940
d9246a53 6941 err = nft_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK],
5b6743fb 6942 &flowtable_hook, flowtable, true);
3b49e2e9 6943 if (err < 0)
a268de77 6944 goto err4;
3b49e2e9 6945
d9246a53
PNA
6946 list_splice(&flowtable_hook.list, &flowtable->hook_list);
6947 flowtable->data.priority = flowtable_hook.priority;
6948 flowtable->hooknum = flowtable_hook.num;
6949
f9382669
PNA
6950 err = nft_register_flowtable_net_hooks(ctx.net, table,
6951 &flowtable->hook_list,
6952 flowtable);
2d285f26 6953 if (err < 0) {
389a2cbc 6954 nft_flowtable_hooks_destroy(&flowtable->hook_list);
3f0465a9 6955 goto err4;
2d285f26 6956 }
3b49e2e9
PNA
6957
6958 err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable);
6959 if (err < 0)
3f0465a9 6960 goto err5;
3b49e2e9
PNA
6961
6962 list_add_tail_rcu(&flowtable->list, &table->flowtables);
6963 table->use++;
6964
6965 return 0;
a268de77 6966err5:
3f0465a9 6967 list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
ff4bf2f4 6968 nft_unregister_flowtable_hook(net, flowtable, hook);
3f0465a9
PNA
6969 list_del_rcu(&hook->list);
6970 kfree_rcu(hook, rcu);
6971 }
a268de77
FF
6972err4:
6973 flowtable->data.type->free(&flowtable->data);
3b49e2e9
PNA
6974err3:
6975 module_put(type->owner);
6976err2:
6977 kfree(flowtable->name);
6978err1:
6979 kfree(flowtable);
6980 return err;
6981}
6982
3003055f
PNA
6983static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook)
6984{
6985 struct nft_hook *this, *next;
6986
6987 list_for_each_entry_safe(this, next, &flowtable_hook->list, list) {
6988 list_del(&this->list);
6989 kfree(this);
6990 }
6991}
6992
abadb2f8
PNA
6993static int nft_delflowtable_hook(struct nft_ctx *ctx,
6994 struct nft_flowtable *flowtable)
6995{
6996 const struct nlattr * const *nla = ctx->nla;
6997 struct nft_flowtable_hook flowtable_hook;
3003055f 6998 struct nft_hook *this, *hook;
abadb2f8
PNA
6999 struct nft_trans *trans;
7000 int err;
7001
7002 err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK],
5b6743fb 7003 &flowtable_hook, flowtable, false);
abadb2f8
PNA
7004 if (err < 0)
7005 return err;
7006
3003055f 7007 list_for_each_entry(this, &flowtable_hook.list, list) {
abadb2f8
PNA
7008 hook = nft_hook_list_find(&flowtable->hook_list, this);
7009 if (!hook) {
7010 err = -ENOENT;
7011 goto err_flowtable_del_hook;
7012 }
7013 hook->inactive = true;
abadb2f8
PNA
7014 }
7015
7016 trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE,
7017 sizeof(struct nft_trans_flowtable));
3003055f
PNA
7018 if (!trans) {
7019 err = -ENOMEM;
7020 goto err_flowtable_del_hook;
7021 }
abadb2f8
PNA
7022
7023 nft_trans_flowtable(trans) = flowtable;
7024 nft_trans_flowtable_update(trans) = true;
7025 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
3003055f 7026 nft_flowtable_hook_release(&flowtable_hook);
abadb2f8
PNA
7027
7028 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
7029
7030 return 0;
7031
7032err_flowtable_del_hook:
3003055f
PNA
7033 list_for_each_entry(this, &flowtable_hook.list, list) {
7034 hook = nft_hook_list_find(&flowtable->hook_list, this);
7035 if (!hook)
7036 break;
7037
abadb2f8 7038 hook->inactive = false;
3003055f
PNA
7039 }
7040 nft_flowtable_hook_release(&flowtable_hook);
abadb2f8
PNA
7041
7042 return err;
7043}
7044
3b49e2e9
PNA
7045static int nf_tables_delflowtable(struct net *net, struct sock *nlsk,
7046 struct sk_buff *skb,
7047 const struct nlmsghdr *nlh,
7048 const struct nlattr * const nla[],
7049 struct netlink_ext_ack *extack)
7050{
7051 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7052 u8 genmask = nft_genmask_next(net);
7053 int family = nfmsg->nfgen_family;
7054 struct nft_flowtable *flowtable;
36dd1bcc 7055 const struct nlattr *attr;
3b49e2e9
PNA
7056 struct nft_table *table;
7057 struct nft_ctx ctx;
7058
e603ea4b
PNA
7059 if (!nla[NFTA_FLOWTABLE_TABLE] ||
7060 (!nla[NFTA_FLOWTABLE_NAME] &&
7061 !nla[NFTA_FLOWTABLE_HANDLE]))
7062 return -EINVAL;
7063
cac20fcd
PNA
7064 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
7065 genmask);
36dd1bcc
PNA
7066 if (IS_ERR(table)) {
7067 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
3b49e2e9 7068 return PTR_ERR(table);
36dd1bcc 7069 }
3b49e2e9 7070
36dd1bcc
PNA
7071 if (nla[NFTA_FLOWTABLE_HANDLE]) {
7072 attr = nla[NFTA_FLOWTABLE_HANDLE];
7073 flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask);
7074 } else {
7075 attr = nla[NFTA_FLOWTABLE_NAME];
7076 flowtable = nft_flowtable_lookup(table, attr, genmask);
7077 }
7078
7079 if (IS_ERR(flowtable)) {
7080 NL_SET_BAD_ATTR(extack, attr);
7081 return PTR_ERR(flowtable);
7082 }
abadb2f8
PNA
7083
7084 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
7085
7086 if (nla[NFTA_FLOWTABLE_HOOK])
7087 return nft_delflowtable_hook(&ctx, flowtable);
7088
36dd1bcc
PNA
7089 if (flowtable->use > 0) {
7090 NL_SET_BAD_ATTR(extack, attr);
3b49e2e9 7091 return -EBUSY;
36dd1bcc 7092 }
3b49e2e9 7093
3b49e2e9
PNA
7094 return nft_delflowtable(&ctx, flowtable);
7095}
7096
7097static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,
7098 u32 portid, u32 seq, int event,
7099 u32 flags, int family,
c42d8bda
PNA
7100 struct nft_flowtable *flowtable,
7101 struct list_head *hook_list)
3b49e2e9
PNA
7102{
7103 struct nlattr *nest, *nest_devs;
7104 struct nfgenmsg *nfmsg;
3f0465a9 7105 struct nft_hook *hook;
3b49e2e9 7106 struct nlmsghdr *nlh;
3b49e2e9
PNA
7107
7108 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
7109 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
7110 if (nlh == NULL)
7111 goto nla_put_failure;
7112
7113 nfmsg = nlmsg_data(nlh);
7114 nfmsg->nfgen_family = family;
7115 nfmsg->version = NFNETLINK_V0;
7116 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
7117
7118 if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) ||
7119 nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) ||
3ecbfd65
HS
7120 nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) ||
7121 nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle),
8bb69f3b
PNA
7122 NFTA_FLOWTABLE_PAD) ||
7123 nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags)))
3b49e2e9
PNA
7124 goto nla_put_failure;
7125
ae0be8de 7126 nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK);
eb895086
KL
7127 if (!nest)
7128 goto nla_put_failure;
3b49e2e9 7129 if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) ||
71a8a63b 7130 nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority)))
3b49e2e9
PNA
7131 goto nla_put_failure;
7132
ae0be8de 7133 nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS);
3b49e2e9
PNA
7134 if (!nest_devs)
7135 goto nla_put_failure;
7136
c42d8bda 7137 list_for_each_entry_rcu(hook, hook_list, list) {
3f0465a9 7138 if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name))
3b49e2e9
PNA
7139 goto nla_put_failure;
7140 }
7141 nla_nest_end(skb, nest_devs);
7142 nla_nest_end(skb, nest);
7143
7144 nlmsg_end(skb, nlh);
7145 return 0;
7146
7147nla_put_failure:
7148 nlmsg_trim(skb, nlh);
7149 return -1;
7150}
7151
7152struct nft_flowtable_filter {
7153 char *table;
7154};
7155
7156static int nf_tables_dump_flowtable(struct sk_buff *skb,
7157 struct netlink_callback *cb)
7158{
7159 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
7160 struct nft_flowtable_filter *filter = cb->data;
7161 unsigned int idx = 0, s_idx = cb->args[0];
7162 struct net *net = sock_net(skb->sk);
7163 int family = nfmsg->nfgen_family;
7164 struct nft_flowtable *flowtable;
3b49e2e9
PNA
7165 const struct nft_table *table;
7166
7167 rcu_read_lock();
7168 cb->seq = net->nft.base_seq;
7169
36596dad 7170 list_for_each_entry_rcu(table, &net->nft.tables, list) {
98319cb9 7171 if (family != NFPROTO_UNSPEC && family != table->family)
3b49e2e9
PNA
7172 continue;
7173
36596dad
PNA
7174 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
7175 if (!nft_is_active(net, flowtable))
7176 goto cont;
7177 if (idx < s_idx)
7178 goto cont;
7179 if (idx > s_idx)
7180 memset(&cb->args[1], 0,
7181 sizeof(cb->args) - sizeof(cb->args[0]));
360cc79d 7182 if (filter && filter->table &&
36596dad
PNA
7183 strcmp(filter->table, table->name))
7184 goto cont;
3b49e2e9 7185
36596dad
PNA
7186 if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid,
7187 cb->nlh->nlmsg_seq,
7188 NFT_MSG_NEWFLOWTABLE,
7189 NLM_F_MULTI | NLM_F_APPEND,
c42d8bda
PNA
7190 table->family,
7191 flowtable,
7192 &flowtable->hook_list) < 0)
36596dad 7193 goto done;
3b49e2e9 7194
36596dad 7195 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3b49e2e9 7196cont:
36596dad 7197 idx++;
3b49e2e9
PNA
7198 }
7199 }
7200done:
7201 rcu_read_unlock();
7202
7203 cb->args[0] = idx;
7204 return skb->len;
7205}
7206
90fd131a 7207static int nf_tables_dump_flowtable_start(struct netlink_callback *cb)
3b49e2e9 7208{
90fd131a
FW
7209 const struct nlattr * const *nla = cb->data;
7210 struct nft_flowtable_filter *filter = NULL;
3b49e2e9 7211
90fd131a
FW
7212 if (nla[NFTA_FLOWTABLE_TABLE]) {
7213 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
7214 if (!filter)
7215 return -ENOMEM;
3b49e2e9 7216
90fd131a
FW
7217 filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE],
7218 GFP_ATOMIC);
7219 if (!filter->table) {
7220 kfree(filter);
7221 return -ENOMEM;
7222 }
7223 }
3b49e2e9 7224
90fd131a 7225 cb->data = filter;
3b49e2e9
PNA
7226 return 0;
7227}
7228
90fd131a 7229static int nf_tables_dump_flowtable_done(struct netlink_callback *cb)
3b49e2e9 7230{
90fd131a 7231 struct nft_flowtable_filter *filter = cb->data;
3b49e2e9 7232
3b49e2e9 7233 if (!filter)
90fd131a 7234 return 0;
3b49e2e9 7235
90fd131a
FW
7236 kfree(filter->table);
7237 kfree(filter);
7238
7239 return 0;
3b49e2e9
PNA
7240}
7241
d9adf22a 7242/* called with rcu_read_lock held */
3b49e2e9
PNA
7243static int nf_tables_getflowtable(struct net *net, struct sock *nlsk,
7244 struct sk_buff *skb,
7245 const struct nlmsghdr *nlh,
7246 const struct nlattr * const nla[],
7247 struct netlink_ext_ack *extack)
7248{
7249 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7250 u8 genmask = nft_genmask_cur(net);
7251 int family = nfmsg->nfgen_family;
7252 struct nft_flowtable *flowtable;
3b49e2e9
PNA
7253 const struct nft_table *table;
7254 struct sk_buff *skb2;
7255 int err;
7256
7257 if (nlh->nlmsg_flags & NLM_F_DUMP) {
7258 struct netlink_dump_control c = {
90fd131a 7259 .start = nf_tables_dump_flowtable_start,
3b49e2e9
PNA
7260 .dump = nf_tables_dump_flowtable,
7261 .done = nf_tables_dump_flowtable_done,
d9adf22a 7262 .module = THIS_MODULE,
90fd131a 7263 .data = (void *)nla,
3b49e2e9
PNA
7264 };
7265
d9adf22a 7266 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
3b49e2e9
PNA
7267 }
7268
7269 if (!nla[NFTA_FLOWTABLE_NAME])
7270 return -EINVAL;
7271
cac20fcd
PNA
7272 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
7273 genmask);
3b49e2e9
PNA
7274 if (IS_ERR(table))
7275 return PTR_ERR(table);
7276
cac20fcd
PNA
7277 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
7278 genmask);
03a0120f 7279 if (IS_ERR(flowtable))
3b49e2e9
PNA
7280 return PTR_ERR(flowtable);
7281
d9adf22a 7282 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
3b49e2e9
PNA
7283 if (!skb2)
7284 return -ENOMEM;
7285
7286 err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid,
7287 nlh->nlmsg_seq,
7288 NFT_MSG_NEWFLOWTABLE, 0, family,
c42d8bda 7289 flowtable, &flowtable->hook_list);
3b49e2e9 7290 if (err < 0)
ee921183 7291 goto err_fill_flowtable_info;
3b49e2e9 7292
ee921183
PNA
7293 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
7294
7295err_fill_flowtable_info:
3b49e2e9
PNA
7296 kfree_skb(skb2);
7297 return err;
7298}
7299
7300static void nf_tables_flowtable_notify(struct nft_ctx *ctx,
7301 struct nft_flowtable *flowtable,
c42d8bda 7302 struct list_head *hook_list,
3b49e2e9
PNA
7303 int event)
7304{
7305 struct sk_buff *skb;
7306 int err;
8e6cf365
RGB
7307 char *buf = kasprintf(GFP_KERNEL, "%s:%llu;%s:%llu",
7308 flowtable->table->name, flowtable->table->handle,
7309 flowtable->name, flowtable->handle);
7310
7311 audit_log_nfcfg(buf,
7312 ctx->family,
7313 flowtable->hooknum,
7314 event == NFT_MSG_NEWFLOWTABLE ?
7315 AUDIT_NFT_OP_FLOWTABLE_REGISTER :
14224039
RGB
7316 AUDIT_NFT_OP_FLOWTABLE_UNREGISTER,
7317 GFP_KERNEL);
8e6cf365 7318 kfree(buf);
3b49e2e9 7319
dceababa 7320 if (!ctx->report &&
3b49e2e9
PNA
7321 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
7322 return;
7323
7324 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7325 if (skb == NULL)
7326 goto err;
7327
7328 err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid,
7329 ctx->seq, event, 0,
c42d8bda 7330 ctx->family, flowtable, hook_list);
3b49e2e9
PNA
7331 if (err < 0) {
7332 kfree_skb(skb);
7333 goto err;
7334 }
7335
67cc570e 7336 nft_notify_enqueue(skb, ctx->report, &ctx->net->nft.notify_list);
3b49e2e9
PNA
7337 return;
7338err:
7339 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
7340}
7341
3b49e2e9
PNA
7342static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
7343{
3f0465a9
PNA
7344 struct nft_hook *hook, *next;
7345
5acab914 7346 flowtable->data.type->free(&flowtable->data);
3f0465a9 7347 list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
5acab914
PNA
7348 flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
7349 FLOW_BLOCK_UNBIND);
3f0465a9
PNA
7350 list_del_rcu(&hook->list);
7351 kfree(hook);
7352 }
3b49e2e9 7353 kfree(flowtable->name);
3b49e2e9 7354 module_put(flowtable->data.type->owner);
a12486eb 7355 kfree(flowtable);
3b49e2e9
PNA
7356}
7357
84d7fce6
PNA
7358static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
7359 u32 portid, u32 seq)
7360{
7361 struct nlmsghdr *nlh;
7362 struct nfgenmsg *nfmsg;
784b4e61 7363 char buf[TASK_COMM_LEN];
dedb67c4 7364 int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN);
84d7fce6
PNA
7365
7366 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
7367 if (nlh == NULL)
7368 goto nla_put_failure;
7369
7370 nfmsg = nlmsg_data(nlh);
7371 nfmsg->nfgen_family = AF_UNSPEC;
7372 nfmsg->version = NFNETLINK_V0;
7373 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
7374
784b4e61
PS
7375 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)) ||
7376 nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) ||
7377 nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current)))
84d7fce6
PNA
7378 goto nla_put_failure;
7379
053c095a
JB
7380 nlmsg_end(skb, nlh);
7381 return 0;
84d7fce6
PNA
7382
7383nla_put_failure:
7384 nlmsg_trim(skb, nlh);
7385 return -EMSGSIZE;
7386}
7387
3b49e2e9
PNA
7388static void nft_flowtable_event(unsigned long event, struct net_device *dev,
7389 struct nft_flowtable *flowtable)
7390{
3f0465a9 7391 struct nft_hook *hook;
3b49e2e9 7392
3f0465a9
PNA
7393 list_for_each_entry(hook, &flowtable->hook_list, list) {
7394 if (hook->ops.dev != dev)
3b49e2e9
PNA
7395 continue;
7396
5acab914 7397 /* flow_offload_netdev_event() cleans up entries for us. */
ff4bf2f4 7398 nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook);
3f0465a9
PNA
7399 list_del_rcu(&hook->list);
7400 kfree_rcu(hook, rcu);
3b49e2e9
PNA
7401 break;
7402 }
7403}
7404
7405static int nf_tables_flowtable_event(struct notifier_block *this,
7406 unsigned long event, void *ptr)
7407{
7408 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
7409 struct nft_flowtable *flowtable;
7410 struct nft_table *table;
0a2cf5ee 7411 struct net *net;
3b49e2e9
PNA
7412
7413 if (event != NETDEV_UNREGISTER)
7414 return 0;
7415
6a48de01 7416 net = dev_net(dev);
9e619d87 7417 mutex_lock(&net->nft.commit_mutex);
0a2cf5ee 7418 list_for_each_entry(table, &net->nft.tables, list) {
36596dad
PNA
7419 list_for_each_entry(flowtable, &table->flowtables, list) {
7420 nft_flowtable_event(event, dev, flowtable);
3b49e2e9
PNA
7421 }
7422 }
9e619d87 7423 mutex_unlock(&net->nft.commit_mutex);
6a48de01 7424
3b49e2e9
PNA
7425 return NOTIFY_DONE;
7426}
7427
7428static struct notifier_block nf_tables_flowtable_notifier = {
7429 .notifier_call = nf_tables_flowtable_event,
7430};
7431
25e94a99
PNA
7432static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb,
7433 int event)
84d7fce6
PNA
7434{
7435 struct nlmsghdr *nlh = nlmsg_hdr(skb);
7436 struct sk_buff *skb2;
7437 int err;
7438
8e6cf365 7439 audit_log_nfcfg("?:0;?:0", 0, net->nft.base_seq,
14224039 7440 AUDIT_NFT_OP_GEN_REGISTER, GFP_KERNEL);
8e6cf365 7441
dceababa 7442 if (!nlmsg_report(nlh) &&
84d7fce6 7443 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
25e94a99 7444 return;
84d7fce6 7445
84d7fce6
PNA
7446 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7447 if (skb2 == NULL)
7448 goto err;
7449
7450 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
7451 nlh->nlmsg_seq);
7452 if (err < 0) {
7453 kfree_skb(skb2);
7454 goto err;
7455 }
7456
25e94a99
PNA
7457 nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
7458 nlmsg_report(nlh), GFP_KERNEL);
7459 return;
84d7fce6 7460err:
25e94a99
PNA
7461 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
7462 -ENOBUFS);
84d7fce6
PNA
7463}
7464
7b8002a1
PNA
7465static int nf_tables_getgen(struct net *net, struct sock *nlsk,
7466 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
7467 const struct nlattr * const nla[],
7468 struct netlink_ext_ack *extack)
84d7fce6 7469{
84d7fce6
PNA
7470 struct sk_buff *skb2;
7471 int err;
7472
d9adf22a 7473 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
84d7fce6
PNA
7474 if (skb2 == NULL)
7475 return -ENOMEM;
7476
7477 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
7478 nlh->nlmsg_seq);
7479 if (err < 0)
ee921183 7480 goto err_fill_gen_info;
84d7fce6 7481
ee921183
PNA
7482 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
7483
7484err_fill_gen_info:
84d7fce6
PNA
7485 kfree_skb(skb2);
7486 return err;
7487}
7488
96518518
PM
7489static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
7490 [NFT_MSG_NEWTABLE] = {
55dd6f93 7491 .call_batch = nf_tables_newtable,
96518518
PM
7492 .attr_count = NFTA_TABLE_MAX,
7493 .policy = nft_table_policy,
7494 },
7495 [NFT_MSG_GETTABLE] = {
d9adf22a 7496 .call_rcu = nf_tables_gettable,
96518518
PM
7497 .attr_count = NFTA_TABLE_MAX,
7498 .policy = nft_table_policy,
7499 },
7500 [NFT_MSG_DELTABLE] = {
55dd6f93 7501 .call_batch = nf_tables_deltable,
96518518
PM
7502 .attr_count = NFTA_TABLE_MAX,
7503 .policy = nft_table_policy,
7504 },
7505 [NFT_MSG_NEWCHAIN] = {
91c7b38d 7506 .call_batch = nf_tables_newchain,
96518518
PM
7507 .attr_count = NFTA_CHAIN_MAX,
7508 .policy = nft_chain_policy,
7509 },
7510 [NFT_MSG_GETCHAIN] = {
d9adf22a 7511 .call_rcu = nf_tables_getchain,
96518518
PM
7512 .attr_count = NFTA_CHAIN_MAX,
7513 .policy = nft_chain_policy,
7514 },
7515 [NFT_MSG_DELCHAIN] = {
91c7b38d 7516 .call_batch = nf_tables_delchain,
96518518
PM
7517 .attr_count = NFTA_CHAIN_MAX,
7518 .policy = nft_chain_policy,
7519 },
7520 [NFT_MSG_NEWRULE] = {
0628b123 7521 .call_batch = nf_tables_newrule,
96518518
PM
7522 .attr_count = NFTA_RULE_MAX,
7523 .policy = nft_rule_policy,
7524 },
7525 [NFT_MSG_GETRULE] = {
d9adf22a 7526 .call_rcu = nf_tables_getrule,
96518518
PM
7527 .attr_count = NFTA_RULE_MAX,
7528 .policy = nft_rule_policy,
7529 },
7530 [NFT_MSG_DELRULE] = {
0628b123 7531 .call_batch = nf_tables_delrule,
96518518
PM
7532 .attr_count = NFTA_RULE_MAX,
7533 .policy = nft_rule_policy,
7534 },
20a69341 7535 [NFT_MSG_NEWSET] = {
958bee14 7536 .call_batch = nf_tables_newset,
20a69341
PM
7537 .attr_count = NFTA_SET_MAX,
7538 .policy = nft_set_policy,
7539 },
7540 [NFT_MSG_GETSET] = {
d9adf22a 7541 .call_rcu = nf_tables_getset,
20a69341
PM
7542 .attr_count = NFTA_SET_MAX,
7543 .policy = nft_set_policy,
7544 },
7545 [NFT_MSG_DELSET] = {
958bee14 7546 .call_batch = nf_tables_delset,
20a69341
PM
7547 .attr_count = NFTA_SET_MAX,
7548 .policy = nft_set_policy,
7549 },
7550 [NFT_MSG_NEWSETELEM] = {
958bee14 7551 .call_batch = nf_tables_newsetelem,
20a69341
PM
7552 .attr_count = NFTA_SET_ELEM_LIST_MAX,
7553 .policy = nft_set_elem_list_policy,
7554 },
7555 [NFT_MSG_GETSETELEM] = {
d9adf22a 7556 .call_rcu = nf_tables_getsetelem,
20a69341
PM
7557 .attr_count = NFTA_SET_ELEM_LIST_MAX,
7558 .policy = nft_set_elem_list_policy,
7559 },
7560 [NFT_MSG_DELSETELEM] = {
958bee14 7561 .call_batch = nf_tables_delsetelem,
20a69341
PM
7562 .attr_count = NFTA_SET_ELEM_LIST_MAX,
7563 .policy = nft_set_elem_list_policy,
7564 },
84d7fce6 7565 [NFT_MSG_GETGEN] = {
d9adf22a 7566 .call_rcu = nf_tables_getgen,
84d7fce6 7567 },
e5009240
PNA
7568 [NFT_MSG_NEWOBJ] = {
7569 .call_batch = nf_tables_newobj,
7570 .attr_count = NFTA_OBJ_MAX,
7571 .policy = nft_obj_policy,
7572 },
7573 [NFT_MSG_GETOBJ] = {
d9adf22a 7574 .call_rcu = nf_tables_getobj,
e5009240
PNA
7575 .attr_count = NFTA_OBJ_MAX,
7576 .policy = nft_obj_policy,
7577 },
7578 [NFT_MSG_DELOBJ] = {
7579 .call_batch = nf_tables_delobj,
7580 .attr_count = NFTA_OBJ_MAX,
7581 .policy = nft_obj_policy,
7582 },
43da04a5 7583 [NFT_MSG_GETOBJ_RESET] = {
d9adf22a 7584 .call_rcu = nf_tables_getobj,
43da04a5
PNA
7585 .attr_count = NFTA_OBJ_MAX,
7586 .policy = nft_obj_policy,
7587 },
3b49e2e9
PNA
7588 [NFT_MSG_NEWFLOWTABLE] = {
7589 .call_batch = nf_tables_newflowtable,
7590 .attr_count = NFTA_FLOWTABLE_MAX,
7591 .policy = nft_flowtable_policy,
7592 },
7593 [NFT_MSG_GETFLOWTABLE] = {
d9adf22a 7594 .call_rcu = nf_tables_getflowtable,
3b49e2e9
PNA
7595 .attr_count = NFTA_FLOWTABLE_MAX,
7596 .policy = nft_flowtable_policy,
7597 },
7598 [NFT_MSG_DELFLOWTABLE] = {
7599 .call_batch = nf_tables_delflowtable,
7600 .attr_count = NFTA_FLOWTABLE_MAX,
7601 .policy = nft_flowtable_policy,
7602 },
96518518
PM
7603};
7604
a654de8f
PNA
7605static int nf_tables_validate(struct net *net)
7606{
7607 struct nft_table *table;
7608
7609 switch (net->nft.validate_state) {
7610 case NFT_VALIDATE_SKIP:
7611 break;
7612 case NFT_VALIDATE_NEED:
7613 nft_validate_state_update(net, NFT_VALIDATE_DO);
954d8297 7614 fallthrough;
a654de8f
PNA
7615 case NFT_VALIDATE_DO:
7616 list_for_each_entry(table, &net->nft.tables, list) {
7617 if (nft_table_validate(net, table) < 0)
7618 return -EAGAIN;
7619 }
7620 break;
7621 }
7622
7623 return 0;
7624}
7625
66293c46
FW
7626/* a drop policy has to be deferred until all rules have been activated,
7627 * otherwise a large ruleset that contains a drop-policy base chain will
7628 * cause all packets to get dropped until the full transaction has been
7629 * processed.
7630 *
7631 * We defer the drop policy until the transaction has been finalized.
7632 */
7633static void nft_chain_commit_drop_policy(struct nft_trans *trans)
7634{
7635 struct nft_base_chain *basechain;
7636
7637 if (nft_trans_chain_policy(trans) != NF_DROP)
7638 return;
7639
7640 if (!nft_is_base_chain(trans->ctx.chain))
7641 return;
7642
7643 basechain = nft_base_chain(trans->ctx.chain);
7644 basechain->policy = NF_DROP;
7645}
7646
91c7b38d
PNA
7647static void nft_chain_commit_update(struct nft_trans *trans)
7648{
7649 struct nft_base_chain *basechain;
7650
1b2470e5
FW
7651 if (nft_trans_chain_name(trans)) {
7652 rhltable_remove(&trans->ctx.table->chains_ht,
7653 &trans->ctx.chain->rhlhead,
7654 nft_chain_ht_params);
d71efb59 7655 swap(trans->ctx.chain->name, nft_trans_chain_name(trans));
1b2470e5
FW
7656 rhltable_insert_key(&trans->ctx.table->chains_ht,
7657 trans->ctx.chain->name,
7658 &trans->ctx.chain->rhlhead,
7659 nft_chain_ht_params);
7660 }
91c7b38d 7661
f323d954 7662 if (!nft_is_base_chain(trans->ctx.chain))
91c7b38d
PNA
7663 return;
7664
53315ac6
FW
7665 nft_chain_stats_replace(trans);
7666
91c7b38d 7667 basechain = nft_base_chain(trans->ctx.chain);
91c7b38d
PNA
7668
7669 switch (nft_trans_chain_policy(trans)) {
7670 case NF_DROP:
7671 case NF_ACCEPT:
7672 basechain->policy = nft_trans_chain_policy(trans);
7673 break;
7674 }
7675}
7676
d62d0ba9
FFM
7677static void nft_obj_commit_update(struct nft_trans *trans)
7678{
7679 struct nft_object *newobj;
7680 struct nft_object *obj;
7681
7682 obj = nft_trans_obj(trans);
7683 newobj = nft_trans_obj_newobj(trans);
7684
9fedd894
FFM
7685 if (obj->ops->update)
7686 obj->ops->update(obj, newobj);
d62d0ba9
FFM
7687
7688 kfree(newobj);
7689}
7690
2f99aa31 7691static void nft_commit_release(struct nft_trans *trans)
c7c32e72 7692{
c7c32e72
PNA
7693 switch (trans->msg_type) {
7694 case NFT_MSG_DELTABLE:
7695 nf_tables_table_destroy(&trans->ctx);
7696 break;
9f8aac0b 7697 case NFT_MSG_NEWCHAIN:
53315ac6 7698 free_percpu(nft_trans_chain_stats(trans));
9f8aac0b
FW
7699 kfree(nft_trans_chain_name(trans));
7700 break;
c7c32e72 7701 case NFT_MSG_DELCHAIN:
43a605f2 7702 nf_tables_chain_destroy(&trans->ctx);
c7c32e72
PNA
7703 break;
7704 case NFT_MSG_DELRULE:
7705 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
7706 break;
7707 case NFT_MSG_DELSET:
0c2a85ed 7708 nft_set_destroy(&trans->ctx, nft_trans_set(trans));
c7c32e72 7709 break;
61edafbb 7710 case NFT_MSG_DELSETELEM:
3453c927
PNA
7711 nf_tables_set_elem_destroy(&trans->ctx,
7712 nft_trans_elem_set(trans),
59105446 7713 nft_trans_elem(trans).priv);
61edafbb 7714 break;
e5009240 7715 case NFT_MSG_DELOBJ:
00bfb320 7716 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
e5009240 7717 break;
3b49e2e9 7718 case NFT_MSG_DELFLOWTABLE:
abadb2f8
PNA
7719 if (nft_trans_flowtable_update(trans))
7720 nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans));
7721 else
7722 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
3b49e2e9 7723 break;
c7c32e72 7724 }
0935d558
FW
7725
7726 if (trans->put_net)
7727 put_net(trans->ctx.net);
7728
c7c32e72
PNA
7729 kfree(trans);
7730}
7731
0935d558 7732static void nf_tables_trans_destroy_work(struct work_struct *w)
2f99aa31
FW
7733{
7734 struct nft_trans *trans, *next;
0935d558
FW
7735 LIST_HEAD(head);
7736
7737 spin_lock(&nf_tables_destroy_list_lock);
7738 list_splice_init(&nf_tables_destroy_list, &head);
7739 spin_unlock(&nf_tables_destroy_list_lock);
2f99aa31 7740
0935d558 7741 if (list_empty(&head))
2f99aa31
FW
7742 return;
7743
7744 synchronize_rcu();
7745
0935d558 7746 list_for_each_entry_safe(trans, next, &head, list) {
2f99aa31
FW
7747 list_del(&trans->list);
7748 nft_commit_release(trans);
7749 }
7750}
7751
ffe8923f
FW
7752void nf_tables_trans_destroy_flush_work(void)
7753{
7754 flush_work(&trans_destroy_work);
7755}
7756EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work);
7757
0cbc06b3
FW
7758static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain)
7759{
7760 struct nft_rule *rule;
7761 unsigned int alloc = 0;
7762 int i;
7763
7764 /* already handled or inactive chain? */
7765 if (chain->rules_next || !nft_is_active_next(net, chain))
7766 return 0;
7767
7768 rule = list_entry(&chain->rules, struct nft_rule, list);
7769 i = 0;
7770
7771 list_for_each_entry_continue(rule, &chain->rules, list) {
7772 if (nft_is_active_next(net, rule))
7773 alloc++;
7774 }
7775
7776 chain->rules_next = nf_tables_chain_alloc_rules(chain, alloc);
7777 if (!chain->rules_next)
7778 return -ENOMEM;
7779
7780 list_for_each_entry_continue(rule, &chain->rules, list) {
7781 if (nft_is_active_next(net, rule))
7782 chain->rules_next[i++] = rule;
7783 }
7784
7785 chain->rules_next[i] = NULL;
7786 return 0;
7787}
7788
7789static void nf_tables_commit_chain_prepare_cancel(struct net *net)
7790{
7791 struct nft_trans *trans, *next;
7792
7793 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
7794 struct nft_chain *chain = trans->ctx.chain;
7795
7796 if (trans->msg_type == NFT_MSG_NEWRULE ||
7797 trans->msg_type == NFT_MSG_DELRULE) {
7798 kvfree(chain->rules_next);
7799 chain->rules_next = NULL;
7800 }
7801 }
7802}
7803
7804static void __nf_tables_commit_chain_free_rules_old(struct rcu_head *h)
7805{
7806 struct nft_rules_old *o = container_of(h, struct nft_rules_old, h);
7807
7808 kvfree(o->start);
7809}
7810
7811static void nf_tables_commit_chain_free_rules_old(struct nft_rule **rules)
7812{
7813 struct nft_rule **r = rules;
7814 struct nft_rules_old *old;
7815
7816 while (*r)
7817 r++;
7818
7819 r++; /* rcu_head is after end marker */
7820 old = (void *) r;
7821 old->start = rules;
7822
7823 call_rcu(&old->h, __nf_tables_commit_chain_free_rules_old);
7824}
7825
0fb39bbe 7826static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain)
0cbc06b3
FW
7827{
7828 struct nft_rule **g0, **g1;
7829 bool next_genbit;
7830
7831 next_genbit = nft_gencursor_next(net);
7832
7833 g0 = rcu_dereference_protected(chain->rules_gen_0,
f102d66b 7834 lockdep_commit_lock_is_held(net));
0cbc06b3 7835 g1 = rcu_dereference_protected(chain->rules_gen_1,
f102d66b 7836 lockdep_commit_lock_is_held(net));
0cbc06b3
FW
7837
7838 /* No changes to this chain? */
7839 if (chain->rules_next == NULL) {
7840 /* chain had no change in last or next generation */
7841 if (g0 == g1)
7842 return;
7843 /*
7844 * chain had no change in this generation; make sure next
7845 * one uses same rules as current generation.
7846 */
7847 if (next_genbit) {
7848 rcu_assign_pointer(chain->rules_gen_1, g0);
7849 nf_tables_commit_chain_free_rules_old(g1);
7850 } else {
7851 rcu_assign_pointer(chain->rules_gen_0, g1);
7852 nf_tables_commit_chain_free_rules_old(g0);
7853 }
7854
7855 return;
7856 }
7857
7858 if (next_genbit)
7859 rcu_assign_pointer(chain->rules_gen_1, chain->rules_next);
7860 else
7861 rcu_assign_pointer(chain->rules_gen_0, chain->rules_next);
7862
7863 chain->rules_next = NULL;
7864
7865 if (g0 == g1)
7866 return;
7867
7868 if (next_genbit)
7869 nf_tables_commit_chain_free_rules_old(g1);
7870 else
7871 nf_tables_commit_chain_free_rules_old(g0);
7872}
7873
d152159b
FW
7874static void nft_obj_del(struct nft_object *obj)
7875{
4d44175a 7876 rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params);
d152159b
FW
7877 list_del_rcu(&obj->list);
7878}
7879
d0e2c7de 7880void nft_chain_del(struct nft_chain *chain)
1b2470e5
FW
7881{
7882 struct nft_table *table = chain->table;
7883
7884 WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead,
7885 nft_chain_ht_params));
7886 list_del_rcu(&chain->list);
7887}
7888
abadb2f8
PNA
7889static void nft_flowtable_hooks_del(struct nft_flowtable *flowtable,
7890 struct list_head *hook_list)
7891{
7892 struct nft_hook *hook, *next;
7893
7894 list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
7895 if (hook->inactive)
7896 list_move(&hook->list, hook_list);
7897 }
7898}
7899
eb014de4
PNA
7900static void nf_tables_module_autoload_cleanup(struct net *net)
7901{
7902 struct nft_module_request *req, *next;
7903
7904 WARN_ON_ONCE(!list_empty(&net->nft.commit_list));
7905 list_for_each_entry_safe(req, next, &net->nft.module_list, list) {
7906 WARN_ON_ONCE(!req->done);
7907 list_del(&req->list);
7908 kfree(req);
7909 }
7910}
7911
0935d558
FW
7912static void nf_tables_commit_release(struct net *net)
7913{
7914 struct nft_trans *trans;
7915
7916 /* all side effects have to be made visible.
7917 * For example, if a chain named 'foo' has been deleted, a
7918 * new transaction must not find it anymore.
7919 *
7920 * Memory reclaim happens asynchronously from work queue
7921 * to prevent expensive synchronize_rcu() in commit phase.
7922 */
7923 if (list_empty(&net->nft.commit_list)) {
eb014de4 7924 nf_tables_module_autoload_cleanup(net);
0935d558
FW
7925 mutex_unlock(&net->nft.commit_mutex);
7926 return;
7927 }
7928
7929 trans = list_last_entry(&net->nft.commit_list,
7930 struct nft_trans, list);
7931 get_net(trans->ctx.net);
7932 WARN_ON_ONCE(trans->put_net);
7933
7934 trans->put_net = true;
7935 spin_lock(&nf_tables_destroy_list_lock);
7936 list_splice_tail_init(&net->nft.commit_list, &nf_tables_destroy_list);
7937 spin_unlock(&nf_tables_destroy_list_lock);
7938
eb014de4 7939 nf_tables_module_autoload_cleanup(net);
0935d558 7940 schedule_work(&trans_destroy_work);
ffe8923f
FW
7941
7942 mutex_unlock(&net->nft.commit_mutex);
0935d558
FW
7943}
7944
67cc570e
PNA
7945static void nft_commit_notify(struct net *net, u32 portid)
7946{
7947 struct sk_buff *batch_skb = NULL, *nskb, *skb;
7948 unsigned char *data;
7949 int len;
7950
7951 list_for_each_entry_safe(skb, nskb, &net->nft.notify_list, list) {
7952 if (!batch_skb) {
7953new_batch:
7954 batch_skb = skb;
7955 len = NLMSG_GOODSIZE - skb->len;
7956 list_del(&skb->list);
7957 continue;
7958 }
7959 len -= skb->len;
7960 if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) {
7961 data = skb_put(batch_skb, skb->len);
7962 memcpy(data, skb->data, skb->len);
7963 list_del(&skb->list);
7964 kfree_skb(skb);
7965 continue;
7966 }
7967 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
7968 NFT_CB(batch_skb).report, GFP_KERNEL);
7969 goto new_batch;
7970 }
7971
7972 if (batch_skb) {
7973 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
7974 NFT_CB(batch_skb).report, GFP_KERNEL);
7975 }
7976
7977 WARN_ON_ONCE(!list_empty(&net->nft.notify_list));
7978}
7979
5913beaf 7980static int nf_tables_commit(struct net *net, struct sk_buff *skb)
37082f93 7981{
37082f93 7982 struct nft_trans *trans, *next;
a3716e70 7983 struct nft_trans_elem *te;
0cbc06b3
FW
7984 struct nft_chain *chain;
7985 struct nft_table *table;
c9626a2c 7986 int err;
37082f93 7987
b8b27498
FW
7988 if (list_empty(&net->nft.commit_list)) {
7989 mutex_unlock(&net->nft.commit_mutex);
7990 return 0;
7991 }
7992
a654de8f
PNA
7993 /* 0. Validate ruleset, otherwise roll back for error reporting. */
7994 if (nf_tables_validate(net) < 0)
7995 return -EAGAIN;
7996
c9626a2c
PNA
7997 err = nft_flow_rule_offload_commit(net);
7998 if (err < 0)
7999 return err;
8000
0cbc06b3
FW
8001 /* 1. Allocate space for next generation rules_gen_X[] */
8002 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
8003 int ret;
37082f93 8004
0cbc06b3
FW
8005 if (trans->msg_type == NFT_MSG_NEWRULE ||
8006 trans->msg_type == NFT_MSG_DELRULE) {
8007 chain = trans->ctx.chain;
8008
8009 ret = nf_tables_commit_chain_prepare(net, chain);
8010 if (ret < 0) {
8011 nf_tables_commit_chain_prepare_cancel(net);
8012 return ret;
8013 }
8014 }
8015 }
37082f93 8016
0cbc06b3
FW
8017 /* step 2. Make rules_gen_X visible to packet path */
8018 list_for_each_entry(table, &net->nft.tables, list) {
0fb39bbe
FW
8019 list_for_each_entry(chain, &table->chains, list)
8020 nf_tables_commit_chain(net, chain);
0cbc06b3
FW
8021 }
8022
8023 /*
8024 * Bump generation counter, invalidate any dump in progress.
8025 * Cannot fail after this point.
37082f93 8026 */
0cbc06b3
FW
8027 while (++net->nft.base_seq == 0);
8028
8029 /* step 3. Start new generation, rules_gen_X now in use. */
8030 net->nft.gencursor = nft_gencursor_next(net);
37082f93
PNA
8031
8032 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
b380e5c7 8033 switch (trans->msg_type) {
55dd6f93
PNA
8034 case NFT_MSG_NEWTABLE:
8035 if (nft_trans_table_update(trans)) {
8036 if (!nft_trans_table_enable(trans)) {
664b0f8c 8037 nf_tables_table_disable(net,
55dd6f93
PNA
8038 trans->ctx.table);
8039 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
8040 }
8041 } else {
f2a6d766 8042 nft_clear(net, trans->ctx.table);
55dd6f93 8043 }
35151d84 8044 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
55dd6f93
PNA
8045 nft_trans_destroy(trans);
8046 break;
8047 case NFT_MSG_DELTABLE:
f2a6d766 8048 list_del_rcu(&trans->ctx.table->list);
35151d84 8049 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
55dd6f93 8050 break;
91c7b38d 8051 case NFT_MSG_NEWCHAIN:
9f8aac0b 8052 if (nft_trans_chain_update(trans)) {
91c7b38d 8053 nft_chain_commit_update(trans);
9f8aac0b
FW
8054 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
8055 /* trans destroyed after rcu grace period */
8056 } else {
66293c46 8057 nft_chain_commit_drop_policy(trans);
664b0f8c 8058 nft_clear(net, trans->ctx.chain);
9f8aac0b
FW
8059 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
8060 nft_trans_destroy(trans);
8061 }
91c7b38d
PNA
8062 break;
8063 case NFT_MSG_DELCHAIN:
1b2470e5 8064 nft_chain_del(trans->ctx.chain);
35151d84 8065 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
c974a3a3
PNA
8066 nf_tables_unregister_hook(trans->ctx.net,
8067 trans->ctx.table,
8068 trans->ctx.chain);
91c7b38d 8069 break;
b380e5c7 8070 case NFT_MSG_NEWRULE:
889f7ee7 8071 nft_clear(trans->ctx.net, nft_trans_rule(trans));
35151d84 8072 nf_tables_rule_notify(&trans->ctx,
37082f93 8073 nft_trans_rule(trans),
35151d84 8074 NFT_MSG_NEWRULE);
37082f93 8075 nft_trans_destroy(trans);
b380e5c7
PNA
8076 break;
8077 case NFT_MSG_DELRULE:
8078 list_del_rcu(&nft_trans_rule(trans)->list);
35151d84
PNA
8079 nf_tables_rule_notify(&trans->ctx,
8080 nft_trans_rule(trans),
8081 NFT_MSG_DELRULE);
f6ac8585
PNA
8082 nft_rule_expr_deactivate(&trans->ctx,
8083 nft_trans_rule(trans),
8084 NFT_TRANS_COMMIT);
b380e5c7 8085 break;
958bee14 8086 case NFT_MSG_NEWSET:
37a9cc52 8087 nft_clear(net, nft_trans_set(trans));
4fefee57
PNA
8088 /* This avoids hitting -EBUSY when deleting the table
8089 * from the transaction.
8090 */
408070d6 8091 if (nft_set_is_anonymous(nft_trans_set(trans)) &&
4fefee57
PNA
8092 !list_empty(&nft_trans_set(trans)->bindings))
8093 trans->ctx.table->use--;
8094
958bee14 8095 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
31f8441c 8096 NFT_MSG_NEWSET, GFP_KERNEL);
958bee14
PNA
8097 nft_trans_destroy(trans);
8098 break;
8099 case NFT_MSG_DELSET:
37a9cc52 8100 list_del_rcu(&nft_trans_set(trans)->list);
958bee14 8101 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
31f8441c 8102 NFT_MSG_DELSET, GFP_KERNEL);
958bee14 8103 break;
60319eb1 8104 case NFT_MSG_NEWSETELEM:
cc02e457
PM
8105 te = (struct nft_trans_elem *)trans->data;
8106
42a55769 8107 te->set->ops->activate(net, te->set, &te->elem);
cc02e457
PM
8108 nf_tables_setelem_notify(&trans->ctx, te->set,
8109 &te->elem,
60319eb1
PNA
8110 NFT_MSG_NEWSETELEM, 0);
8111 nft_trans_destroy(trans);
8112 break;
8113 case NFT_MSG_DELSETELEM:
a3716e70 8114 te = (struct nft_trans_elem *)trans->data;
fe2811eb 8115
a3716e70
PNA
8116 nf_tables_setelem_notify(&trans->ctx, te->set,
8117 &te->elem,
60319eb1 8118 NFT_MSG_DELSETELEM, 0);
5cb82a38 8119 te->set->ops->remove(net, te->set, &te->elem);
3dd0673a
PM
8120 atomic_dec(&te->set->nelems);
8121 te->set->ndeact--;
60319eb1 8122 break;
e5009240 8123 case NFT_MSG_NEWOBJ:
d62d0ba9
FFM
8124 if (nft_trans_obj_update(trans)) {
8125 nft_obj_commit_update(trans);
8126 nf_tables_obj_notify(&trans->ctx,
8127 nft_trans_obj(trans),
8128 NFT_MSG_NEWOBJ);
8129 } else {
8130 nft_clear(net, nft_trans_obj(trans));
8131 nf_tables_obj_notify(&trans->ctx,
8132 nft_trans_obj(trans),
8133 NFT_MSG_NEWOBJ);
8134 nft_trans_destroy(trans);
8135 }
e5009240
PNA
8136 break;
8137 case NFT_MSG_DELOBJ:
d152159b 8138 nft_obj_del(nft_trans_obj(trans));
e5009240
PNA
8139 nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
8140 NFT_MSG_DELOBJ);
8141 break;
3b49e2e9 8142 case NFT_MSG_NEWFLOWTABLE:
78d9f48f
PNA
8143 if (nft_trans_flowtable_update(trans)) {
8144 nf_tables_flowtable_notify(&trans->ctx,
8145 nft_trans_flowtable(trans),
8146 &nft_trans_flowtable_hooks(trans),
8147 NFT_MSG_NEWFLOWTABLE);
8148 list_splice(&nft_trans_flowtable_hooks(trans),
8149 &nft_trans_flowtable(trans)->hook_list);
8150 } else {
8151 nft_clear(net, nft_trans_flowtable(trans));
8152 nf_tables_flowtable_notify(&trans->ctx,
8153 nft_trans_flowtable(trans),
8154 &nft_trans_flowtable(trans)->hook_list,
8155 NFT_MSG_NEWFLOWTABLE);
8156 }
3b49e2e9
PNA
8157 nft_trans_destroy(trans);
8158 break;
8159 case NFT_MSG_DELFLOWTABLE:
abadb2f8
PNA
8160 if (nft_trans_flowtable_update(trans)) {
8161 nft_flowtable_hooks_del(nft_trans_flowtable(trans),
8162 &nft_trans_flowtable_hooks(trans));
8163 nf_tables_flowtable_notify(&trans->ctx,
8164 nft_trans_flowtable(trans),
8165 &nft_trans_flowtable_hooks(trans),
8166 NFT_MSG_DELFLOWTABLE);
8167 nft_unregister_flowtable_net_hooks(net,
8168 &nft_trans_flowtable_hooks(trans));
8169 } else {
8170 list_del_rcu(&nft_trans_flowtable(trans)->list);
8171 nf_tables_flowtable_notify(&trans->ctx,
8172 nft_trans_flowtable(trans),
8173 &nft_trans_flowtable(trans)->hook_list,
8174 NFT_MSG_DELFLOWTABLE);
8175 nft_unregister_flowtable_net_hooks(net,
8176 &nft_trans_flowtable(trans)->hook_list);
8177 }
3b49e2e9 8178 break;
37082f93 8179 }
37082f93
PNA
8180 }
8181
67cc570e 8182 nft_commit_notify(net, NETLINK_CB(skb).portid);
84d7fce6 8183 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
0935d558 8184 nf_tables_commit_release(net);
37082f93
PNA
8185
8186 return 0;
8187}
8188
eb014de4
PNA
8189static void nf_tables_module_autoload(struct net *net)
8190{
8191 struct nft_module_request *req, *next;
8192 LIST_HEAD(module_list);
8193
8194 list_splice_init(&net->nft.module_list, &module_list);
8195 mutex_unlock(&net->nft.commit_mutex);
8196 list_for_each_entry_safe(req, next, &module_list, list) {
1d305ba4
FW
8197 request_module("%s", req->module);
8198 req->done = true;
eb014de4
PNA
8199 }
8200 mutex_lock(&net->nft.commit_mutex);
8201 list_splice(&module_list, &net->nft.module_list);
8202}
8203
b326dd37 8204static void nf_tables_abort_release(struct nft_trans *trans)
c7c32e72 8205{
c7c32e72
PNA
8206 switch (trans->msg_type) {
8207 case NFT_MSG_NEWTABLE:
8208 nf_tables_table_destroy(&trans->ctx);
8209 break;
8210 case NFT_MSG_NEWCHAIN:
43a605f2 8211 nf_tables_chain_destroy(&trans->ctx);
c7c32e72
PNA
8212 break;
8213 case NFT_MSG_NEWRULE:
8214 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
8215 break;
8216 case NFT_MSG_NEWSET:
0c2a85ed 8217 nft_set_destroy(&trans->ctx, nft_trans_set(trans));
c7c32e72 8218 break;
61edafbb
PM
8219 case NFT_MSG_NEWSETELEM:
8220 nft_set_elem_destroy(nft_trans_elem_set(trans),
61f9e292 8221 nft_trans_elem(trans).priv, true);
61edafbb 8222 break;
e5009240 8223 case NFT_MSG_NEWOBJ:
00bfb320 8224 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
e5009240 8225 break;
3b49e2e9 8226 case NFT_MSG_NEWFLOWTABLE:
78d9f48f
PNA
8227 if (nft_trans_flowtable_update(trans))
8228 nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans));
8229 else
8230 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
3b49e2e9 8231 break;
c7c32e72
PNA
8232 }
8233 kfree(trans);
8234}
8235
c0391b6a 8236static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
37082f93 8237{
37082f93 8238 struct nft_trans *trans, *next;
02263db0 8239 struct nft_trans_elem *te;
abadb2f8 8240 struct nft_hook *hook;
37082f93 8241
c0391b6a
PNA
8242 if (action == NFNL_ABORT_VALIDATE &&
8243 nf_tables_validate(net) < 0)
8244 return -EAGAIN;
8245
a907e36d
XL
8246 list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list,
8247 list) {
b380e5c7 8248 switch (trans->msg_type) {
55dd6f93
PNA
8249 case NFT_MSG_NEWTABLE:
8250 if (nft_trans_table_update(trans)) {
8251 if (nft_trans_table_enable(trans)) {
664b0f8c 8252 nf_tables_table_disable(net,
55dd6f93
PNA
8253 trans->ctx.table);
8254 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
8255 }
8256 nft_trans_destroy(trans);
8257 } else {
e688a7f8 8258 list_del_rcu(&trans->ctx.table->list);
55dd6f93
PNA
8259 }
8260 break;
8261 case NFT_MSG_DELTABLE:
f2a6d766 8262 nft_clear(trans->ctx.net, trans->ctx.table);
55dd6f93
PNA
8263 nft_trans_destroy(trans);
8264 break;
91c7b38d
PNA
8265 case NFT_MSG_NEWCHAIN:
8266 if (nft_trans_chain_update(trans)) {
982f4051 8267 free_percpu(nft_trans_chain_stats(trans));
9f8aac0b 8268 kfree(nft_trans_chain_name(trans));
91c7b38d
PNA
8269 nft_trans_destroy(trans);
8270 } else {
d0e2c7de
PNA
8271 if (nft_chain_is_bound(trans->ctx.chain)) {
8272 nft_trans_destroy(trans);
8273 break;
8274 }
4fefee57 8275 trans->ctx.table->use--;
1b2470e5 8276 nft_chain_del(trans->ctx.chain);
c974a3a3
PNA
8277 nf_tables_unregister_hook(trans->ctx.net,
8278 trans->ctx.table,
8279 trans->ctx.chain);
91c7b38d
PNA
8280 }
8281 break;
8282 case NFT_MSG_DELCHAIN:
4fefee57 8283 trans->ctx.table->use++;
664b0f8c 8284 nft_clear(trans->ctx.net, trans->ctx.chain);
91c7b38d
PNA
8285 nft_trans_destroy(trans);
8286 break;
b380e5c7 8287 case NFT_MSG_NEWRULE:
4fefee57 8288 trans->ctx.chain->use--;
b380e5c7 8289 list_del_rcu(&nft_trans_rule(trans)->list);
f6ac8585
PNA
8290 nft_rule_expr_deactivate(&trans->ctx,
8291 nft_trans_rule(trans),
8292 NFT_TRANS_ABORT);
b380e5c7
PNA
8293 break;
8294 case NFT_MSG_DELRULE:
4fefee57 8295 trans->ctx.chain->use++;
889f7ee7 8296 nft_clear(trans->ctx.net, nft_trans_rule(trans));
bb7b40ae 8297 nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans));
37082f93 8298 nft_trans_destroy(trans);
b380e5c7 8299 break;
958bee14 8300 case NFT_MSG_NEWSET:
4fefee57 8301 trans->ctx.table->use--;
6a0a8d10 8302 if (nft_trans_set_bound(trans)) {
40ba1d9b
PNA
8303 nft_trans_destroy(trans);
8304 break;
8305 }
8306 list_del_rcu(&nft_trans_set(trans)->list);
958bee14
PNA
8307 break;
8308 case NFT_MSG_DELSET:
4fefee57 8309 trans->ctx.table->use++;
37a9cc52 8310 nft_clear(trans->ctx.net, nft_trans_set(trans));
958bee14
PNA
8311 nft_trans_destroy(trans);
8312 break;
60319eb1 8313 case NFT_MSG_NEWSETELEM:
6a0a8d10 8314 if (nft_trans_elem_set_bound(trans)) {
40ba1d9b
PNA
8315 nft_trans_destroy(trans);
8316 break;
8317 }
02263db0 8318 te = (struct nft_trans_elem *)trans->data;
5cb82a38 8319 te->set->ops->remove(net, te->set, &te->elem);
3dd0673a 8320 atomic_dec(&te->set->nelems);
60319eb1
PNA
8321 break;
8322 case NFT_MSG_DELSETELEM:
cc02e457
PM
8323 te = (struct nft_trans_elem *)trans->data;
8324
59105446 8325 nft_set_elem_activate(net, te->set, &te->elem);
42a55769 8326 te->set->ops->activate(net, te->set, &te->elem);
3dd0673a 8327 te->set->ndeact--;
cc02e457 8328
e5009240
PNA
8329 nft_trans_destroy(trans);
8330 break;
8331 case NFT_MSG_NEWOBJ:
d62d0ba9
FFM
8332 if (nft_trans_obj_update(trans)) {
8333 kfree(nft_trans_obj_newobj(trans));
8334 nft_trans_destroy(trans);
8335 } else {
8336 trans->ctx.table->use--;
8337 nft_obj_del(nft_trans_obj(trans));
8338 }
e5009240
PNA
8339 break;
8340 case NFT_MSG_DELOBJ:
8341 trans->ctx.table->use++;
8342 nft_clear(trans->ctx.net, nft_trans_obj(trans));
60319eb1
PNA
8343 nft_trans_destroy(trans);
8344 break;
3b49e2e9 8345 case NFT_MSG_NEWFLOWTABLE:
78d9f48f
PNA
8346 if (nft_trans_flowtable_update(trans)) {
8347 nft_unregister_flowtable_net_hooks(net,
8348 &nft_trans_flowtable_hooks(trans));
8349 } else {
8350 trans->ctx.table->use--;
8351 list_del_rcu(&nft_trans_flowtable(trans)->list);
8352 nft_unregister_flowtable_net_hooks(net,
8353 &nft_trans_flowtable(trans)->hook_list);
8354 }
3b49e2e9
PNA
8355 break;
8356 case NFT_MSG_DELFLOWTABLE:
abadb2f8
PNA
8357 if (nft_trans_flowtable_update(trans)) {
8358 list_for_each_entry(hook, &nft_trans_flowtable(trans)->hook_list, list)
8359 hook->inactive = false;
8360 } else {
8361 trans->ctx.table->use++;
8362 nft_clear(trans->ctx.net, nft_trans_flowtable(trans));
8363 }
3b49e2e9
PNA
8364 nft_trans_destroy(trans);
8365 break;
37082f93 8366 }
37082f93
PNA
8367 }
8368
b326dd37
PNA
8369 synchronize_rcu();
8370
a1cee076
PNA
8371 list_for_each_entry_safe_reverse(trans, next,
8372 &net->nft.commit_list, list) {
c7c32e72 8373 list_del(&trans->list);
b326dd37 8374 nf_tables_abort_release(trans);
37082f93
PNA
8375 }
8376
c0391b6a 8377 if (action == NFNL_ABORT_AUTOLOAD)
eb014de4
PNA
8378 nf_tables_module_autoload(net);
8379 else
8380 nf_tables_module_autoload_cleanup(net);
8381
37082f93
PNA
8382 return 0;
8383}
8384
a654de8f
PNA
8385static void nf_tables_cleanup(struct net *net)
8386{
8387 nft_validate_state_update(net, NFT_VALIDATE_SKIP);
8388}
8389
c0391b6a
PNA
8390static int nf_tables_abort(struct net *net, struct sk_buff *skb,
8391 enum nfnl_abort_action action)
71ad00c5 8392{
c0391b6a 8393 int ret = __nf_tables_abort(net, action);
f102d66b
FW
8394
8395 mutex_unlock(&net->nft.commit_mutex);
8396
8397 return ret;
71ad00c5
FW
8398}
8399
74e8bcd2
PNA
8400static bool nf_tables_valid_genid(struct net *net, u32 genid)
8401{
f102d66b
FW
8402 bool genid_ok;
8403
8404 mutex_lock(&net->nft.commit_mutex);
8405
8406 genid_ok = genid == 0 || net->nft.base_seq == genid;
8407 if (!genid_ok)
8408 mutex_unlock(&net->nft.commit_mutex);
8409
8410 /* else, commit mutex has to be released by commit or abort function */
8411 return genid_ok;
74e8bcd2
PNA
8412}
8413
96518518
PM
8414static const struct nfnetlink_subsystem nf_tables_subsys = {
8415 .name = "nf_tables",
8416 .subsys_id = NFNL_SUBSYS_NFTABLES,
8417 .cb_count = NFT_MSG_MAX,
8418 .cb = nf_tables_cb,
0628b123
PNA
8419 .commit = nf_tables_commit,
8420 .abort = nf_tables_abort,
a654de8f 8421 .cleanup = nf_tables_cleanup,
74e8bcd2 8422 .valid_genid = nf_tables_valid_genid,
be2ab5b4 8423 .owner = THIS_MODULE,
96518518
PM
8424};
8425
7210e4e3 8426int nft_chain_validate_dependency(const struct nft_chain *chain,
32537e91 8427 enum nft_chain_types type)
7210e4e3
PNA
8428{
8429 const struct nft_base_chain *basechain;
8430
f323d954 8431 if (nft_is_base_chain(chain)) {
7210e4e3
PNA
8432 basechain = nft_base_chain(chain);
8433 if (basechain->type->type != type)
8434 return -EOPNOTSUPP;
8435 }
8436 return 0;
8437}
8438EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
8439
75e8d06d
PNA
8440int nft_chain_validate_hooks(const struct nft_chain *chain,
8441 unsigned int hook_flags)
8442{
8443 struct nft_base_chain *basechain;
8444
f323d954 8445 if (nft_is_base_chain(chain)) {
75e8d06d
PNA
8446 basechain = nft_base_chain(chain);
8447
c974a3a3 8448 if ((1 << basechain->ops.hooknum) & hook_flags)
75e8d06d
PNA
8449 return 0;
8450
8451 return -EOPNOTSUPP;
8452 }
8453
8454 return 0;
8455}
8456EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
8457
20a69341
PM
8458/*
8459 * Loop detection - walk through the ruleset beginning at the destination chain
8460 * of a new jump until either the source chain is reached (loop) or all
8461 * reachable chains have been traversed.
8462 *
8463 * The loop check is performed whenever a new jump verdict is added to an
8464 * expression or verdict map or a verdict map is bound to a new chain.
8465 */
8466
8467static int nf_tables_check_loops(const struct nft_ctx *ctx,
8468 const struct nft_chain *chain);
8469
8470static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
de70185d 8471 struct nft_set *set,
20a69341 8472 const struct nft_set_iter *iter,
de70185d 8473 struct nft_set_elem *elem)
20a69341 8474{
fe2811eb
PM
8475 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
8476 const struct nft_data *data;
8477
8478 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
8479 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
62f9c8b4
PNA
8480 return 0;
8481
fe2811eb 8482 data = nft_set_ext_data(ext);
1ca2e170 8483 switch (data->verdict.code) {
20a69341
PM
8484 case NFT_JUMP:
8485 case NFT_GOTO:
1ca2e170 8486 return nf_tables_check_loops(ctx, data->verdict.chain);
20a69341
PM
8487 default:
8488 return 0;
8489 }
8490}
8491
8492static int nf_tables_check_loops(const struct nft_ctx *ctx,
8493 const struct nft_chain *chain)
8494{
8495 const struct nft_rule *rule;
8496 const struct nft_expr *expr, *last;
de70185d 8497 struct nft_set *set;
20a69341
PM
8498 struct nft_set_binding *binding;
8499 struct nft_set_iter iter;
20a69341
PM
8500
8501 if (ctx->chain == chain)
8502 return -ELOOP;
8503
8504 list_for_each_entry(rule, &chain->rules, list) {
8505 nft_rule_for_each_expr(expr, last, rule) {
a654de8f
PNA
8506 struct nft_immediate_expr *priv;
8507 const struct nft_data *data;
0ca743a5
PNA
8508 int err;
8509
a654de8f 8510 if (strcmp(expr->ops->type->name, "immediate"))
20a69341
PM
8511 continue;
8512
a654de8f
PNA
8513 priv = nft_expr_priv(expr);
8514 if (priv->dreg != NFT_REG_VERDICT)
0ca743a5 8515 continue;
20a69341 8516
a654de8f 8517 data = &priv->data;
1ca2e170 8518 switch (data->verdict.code) {
20a69341
PM
8519 case NFT_JUMP:
8520 case NFT_GOTO:
1ca2e170
PM
8521 err = nf_tables_check_loops(ctx,
8522 data->verdict.chain);
20a69341
PM
8523 if (err < 0)
8524 return err;
8525 default:
8526 break;
8527 }
8528 }
8529 }
8530
8531 list_for_each_entry(set, &ctx->table->sets, list) {
37a9cc52
PNA
8532 if (!nft_is_active_next(ctx->net, set))
8533 continue;
20a69341
PM
8534 if (!(set->flags & NFT_SET_MAP) ||
8535 set->dtype != NFT_DATA_VERDICT)
8536 continue;
8537
8538 list_for_each_entry(binding, &set->bindings, list) {
11113e19
PM
8539 if (!(binding->flags & NFT_SET_MAP) ||
8540 binding->chain != chain)
20a69341
PM
8541 continue;
8542
8588ac09 8543 iter.genmask = nft_genmask_next(ctx->net);
20a69341
PM
8544 iter.skip = 0;
8545 iter.count = 0;
8546 iter.err = 0;
8547 iter.fn = nf_tables_loop_check_setelem;
8548
8549 set->ops->walk(ctx, set, &iter);
8550 if (iter.err < 0)
8551 return iter.err;
8552 }
8553 }
8554
8555 return 0;
8556}
8557
36b701fa
LGL
8558/**
8559 * nft_parse_u32_check - fetch u32 attribute and check for maximum value
8560 *
8561 * @attr: netlink attribute to fetch value from
8562 * @max: maximum value to be stored in dest
8563 * @dest: pointer to the variable
8564 *
8565 * Parse, check and store a given u32 netlink attribute into variable.
8566 * This function returns -ERANGE if the value goes over maximum value.
8567 * Otherwise a 0 is returned and the attribute value is stored in the
8568 * destination variable.
8569 */
f1d505bb 8570int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest)
36b701fa 8571{
09525a09 8572 u32 val;
36b701fa
LGL
8573
8574 val = ntohl(nla_get_be32(attr));
8575 if (val > max)
8576 return -ERANGE;
8577
8578 *dest = val;
8579 return 0;
8580}
8581EXPORT_SYMBOL_GPL(nft_parse_u32_check);
8582
49499c3e
PM
8583/**
8584 * nft_parse_register - parse a register value from a netlink attribute
8585 *
8586 * @attr: netlink attribute
8587 *
8588 * Parse and translate a register value from a netlink attribute.
8589 * Registers used to be 128 bit wide, these register numbers will be
8590 * mapped to the corresponding 32 bit register numbers.
8591 */
b1c96ed3
PM
8592unsigned int nft_parse_register(const struct nlattr *attr)
8593{
49499c3e
PM
8594 unsigned int reg;
8595
8596 reg = ntohl(nla_get_be32(attr));
8597 switch (reg) {
8598 case NFT_REG_VERDICT...NFT_REG_4:
8599 return reg * NFT_REG_SIZE / NFT_REG32_SIZE;
8600 default:
8601 return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
8602 }
b1c96ed3
PM
8603}
8604EXPORT_SYMBOL_GPL(nft_parse_register);
8605
49499c3e
PM
8606/**
8607 * nft_dump_register - dump a register value to a netlink attribute
8608 *
8609 * @skb: socket buffer
8610 * @attr: attribute number
8611 * @reg: register number
8612 *
8613 * Construct a netlink attribute containing the register number. For
8614 * compatibility reasons, register numbers being a multiple of 4 are
8615 * translated to the corresponding 128 bit register numbers.
8616 */
b1c96ed3
PM
8617int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
8618{
49499c3e
PM
8619 if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
8620 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
8621 else
8622 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
8623
b1c96ed3
PM
8624 return nla_put_be32(skb, attr, htonl(reg));
8625}
8626EXPORT_SYMBOL_GPL(nft_dump_register);
8627
96518518 8628/**
d07db988 8629 * nft_validate_register_load - validate a load from a register
96518518
PM
8630 *
8631 * @reg: the register number
d07db988 8632 * @len: the length of the data
96518518
PM
8633 *
8634 * Validate that the input register is one of the general purpose
d07db988 8635 * registers and that the length of the load is within the bounds.
96518518 8636 */
4f16d25c 8637static int nft_validate_register_load(enum nft_registers reg, unsigned int len)
96518518 8638{
49499c3e 8639 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
96518518 8640 return -EINVAL;
d07db988
PM
8641 if (len == 0)
8642 return -EINVAL;
c593642c 8643 if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data))
d07db988 8644 return -ERANGE;
49499c3e 8645
96518518
PM
8646 return 0;
8647}
4f16d25c
PNA
8648
8649int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len)
8650{
8651 u32 reg;
8652 int err;
8653
8654 reg = nft_parse_register(attr);
8655 err = nft_validate_register_load(reg, len);
8656 if (err < 0)
8657 return err;
8658
8659 *sreg = reg;
8660 return 0;
8661}
8662EXPORT_SYMBOL_GPL(nft_parse_register_load);
96518518 8663
96518518 8664/**
1ec10212 8665 * nft_validate_register_store - validate an expressions' register store
96518518
PM
8666 *
8667 * @ctx: context of the expression performing the load
8668 * @reg: the destination register number
8669 * @data: the data to load
8670 * @type: the data type
45d9bcda 8671 * @len: the length of the data
96518518
PM
8672 *
8673 * Validate that a data load uses the appropriate data type for
45d9bcda
PM
8674 * the destination register and the length is within the bounds.
8675 * A value of NULL for the data means that its runtime gathered
58f40ab6 8676 * data.
96518518 8677 */
1ec10212
PM
8678int nft_validate_register_store(const struct nft_ctx *ctx,
8679 enum nft_registers reg,
8680 const struct nft_data *data,
8681 enum nft_data_types type, unsigned int len)
96518518 8682{
20a69341
PM
8683 int err;
8684
96518518
PM
8685 switch (reg) {
8686 case NFT_REG_VERDICT:
58f40ab6 8687 if (type != NFT_DATA_VERDICT)
96518518 8688 return -EINVAL;
20a69341 8689
58f40ab6 8690 if (data != NULL &&
1ca2e170
PM
8691 (data->verdict.code == NFT_GOTO ||
8692 data->verdict.code == NFT_JUMP)) {
8693 err = nf_tables_check_loops(ctx, data->verdict.chain);
20a69341
PM
8694 if (err < 0)
8695 return err;
20a69341
PM
8696 }
8697
96518518
PM
8698 return 0;
8699 default:
49499c3e 8700 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
27e6d201 8701 return -EINVAL;
45d9bcda
PM
8702 if (len == 0)
8703 return -EINVAL;
49499c3e 8704 if (reg * NFT_REG32_SIZE + len >
c593642c 8705 sizeof_field(struct nft_regs, data))
45d9bcda 8706 return -ERANGE;
27e6d201 8707
96518518
PM
8708 if (data != NULL && type != NFT_DATA_VALUE)
8709 return -EINVAL;
8710 return 0;
8711 }
8712}
1ec10212 8713EXPORT_SYMBOL_GPL(nft_validate_register_store);
96518518
PM
8714
8715static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
8716 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
8717 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
8718 .len = NFT_CHAIN_MAXNAMELEN - 1 },
51d70f18 8719 [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 },
96518518
PM
8720};
8721
8722static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
8723 struct nft_data_desc *desc, const struct nlattr *nla)
8724{
664b0f8c 8725 u8 genmask = nft_genmask_next(ctx->net);
96518518
PM
8726 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
8727 struct nft_chain *chain;
8728 int err;
8729
8cb08174
JB
8730 err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla,
8731 nft_verdict_policy, NULL);
96518518
PM
8732 if (err < 0)
8733 return err;
8734
8735 if (!tb[NFTA_VERDICT_CODE])
8736 return -EINVAL;
1ca2e170 8737 data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
96518518 8738
1ca2e170 8739 switch (data->verdict.code) {
e0abdadc 8740 default:
1ca2e170 8741 switch (data->verdict.code & NF_VERDICT_MASK) {
e0abdadc
PM
8742 case NF_ACCEPT:
8743 case NF_DROP:
8744 case NF_QUEUE:
8745 break;
8746 default:
8747 return -EINVAL;
8748 }
954d8297 8749 fallthrough;
96518518
PM
8750 case NFT_CONTINUE:
8751 case NFT_BREAK:
8752 case NFT_RETURN:
96518518
PM
8753 break;
8754 case NFT_JUMP:
8755 case NFT_GOTO:
51d70f18
PNA
8756 if (tb[NFTA_VERDICT_CHAIN]) {
8757 chain = nft_chain_lookup(ctx->net, ctx->table,
8758 tb[NFTA_VERDICT_CHAIN],
8759 genmask);
8760 } else if (tb[NFTA_VERDICT_CHAIN_ID]) {
8761 chain = nft_chain_lookup_byid(ctx->net,
8762 tb[NFTA_VERDICT_CHAIN_ID]);
8763 if (IS_ERR(chain))
8764 return PTR_ERR(chain);
8765 } else {
96518518 8766 return -EINVAL;
51d70f18
PNA
8767 }
8768
96518518
PM
8769 if (IS_ERR(chain))
8770 return PTR_ERR(chain);
f323d954 8771 if (nft_is_base_chain(chain))
96518518
PM
8772 return -EOPNOTSUPP;
8773
96518518 8774 chain->use++;
1ca2e170 8775 data->verdict.chain = chain;
96518518 8776 break;
96518518
PM
8777 }
8778
4c4ed074 8779 desc->len = sizeof(data->verdict);
96518518
PM
8780 desc->type = NFT_DATA_VERDICT;
8781 return 0;
8782}
8783
8784static void nft_verdict_uninit(const struct nft_data *data)
8785{
d0e2c7de
PNA
8786 struct nft_chain *chain;
8787 struct nft_rule *rule;
8788
1ca2e170 8789 switch (data->verdict.code) {
96518518
PM
8790 case NFT_JUMP:
8791 case NFT_GOTO:
d0e2c7de
PNA
8792 chain = data->verdict.chain;
8793 chain->use--;
8794
8795 if (!nft_chain_is_bound(chain))
8796 break;
8797
8798 chain->table->use--;
8799 list_for_each_entry(rule, &chain->rules, list)
8800 chain->use--;
8801
8802 nft_chain_del(chain);
96518518
PM
8803 break;
8804 }
8805}
8806
33d5a7b1 8807int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
96518518
PM
8808{
8809 struct nlattr *nest;
8810
ae0be8de 8811 nest = nla_nest_start_noflag(skb, type);
96518518
PM
8812 if (!nest)
8813 goto nla_put_failure;
8814
33d5a7b1 8815 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
96518518
PM
8816 goto nla_put_failure;
8817
33d5a7b1 8818 switch (v->code) {
96518518
PM
8819 case NFT_JUMP:
8820 case NFT_GOTO:
1ca2e170 8821 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
33d5a7b1 8822 v->chain->name))
96518518
PM
8823 goto nla_put_failure;
8824 }
8825 nla_nest_end(skb, nest);
8826 return 0;
8827
8828nla_put_failure:
8829 return -1;
8830}
8831
d0a11fc3
PM
8832static int nft_value_init(const struct nft_ctx *ctx,
8833 struct nft_data *data, unsigned int size,
96518518
PM
8834 struct nft_data_desc *desc, const struct nlattr *nla)
8835{
8836 unsigned int len;
8837
8838 len = nla_len(nla);
8839 if (len == 0)
8840 return -EINVAL;
d0a11fc3 8841 if (len > size)
96518518
PM
8842 return -EOVERFLOW;
8843
d0a11fc3 8844 nla_memcpy(data->data, nla, len);
96518518
PM
8845 desc->type = NFT_DATA_VALUE;
8846 desc->len = len;
8847 return 0;
8848}
8849
8850static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
8851 unsigned int len)
8852{
8853 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
8854}
8855
8856static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
d0a11fc3 8857 [NFTA_DATA_VALUE] = { .type = NLA_BINARY },
96518518
PM
8858 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
8859};
8860
8861/**
8862 * nft_data_init - parse nf_tables data netlink attributes
8863 *
8864 * @ctx: context of the expression using the data
8865 * @data: destination struct nft_data
d0a11fc3 8866 * @size: maximum data length
96518518
PM
8867 * @desc: data description
8868 * @nla: netlink attribute containing data
8869 *
8870 * Parse the netlink data attributes and initialize a struct nft_data.
8871 * The type and length of data are returned in the data description.
8872 *
8873 * The caller can indicate that it only wants to accept data of type
8874 * NFT_DATA_VALUE by passing NULL for the ctx argument.
8875 */
d0a11fc3
PM
8876int nft_data_init(const struct nft_ctx *ctx,
8877 struct nft_data *data, unsigned int size,
96518518
PM
8878 struct nft_data_desc *desc, const struct nlattr *nla)
8879{
8880 struct nlattr *tb[NFTA_DATA_MAX + 1];
8881 int err;
8882
8cb08174
JB
8883 err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla,
8884 nft_data_policy, NULL);
96518518
PM
8885 if (err < 0)
8886 return err;
8887
8888 if (tb[NFTA_DATA_VALUE])
d0a11fc3
PM
8889 return nft_value_init(ctx, data, size, desc,
8890 tb[NFTA_DATA_VALUE]);
96518518
PM
8891 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
8892 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
8893 return -EINVAL;
8894}
8895EXPORT_SYMBOL_GPL(nft_data_init);
8896
8897/**
59105446 8898 * nft_data_release - release a nft_data item
96518518
PM
8899 *
8900 * @data: struct nft_data to release
8901 * @type: type of data
8902 *
8903 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
8904 * all others need to be released by calling this function.
8905 */
59105446 8906void nft_data_release(const struct nft_data *data, enum nft_data_types type)
96518518 8907{
960bd2c2 8908 if (type < NFT_DATA_VERDICT)
96518518 8909 return;
960bd2c2 8910 switch (type) {
96518518
PM
8911 case NFT_DATA_VERDICT:
8912 return nft_verdict_uninit(data);
8913 default:
8914 WARN_ON(1);
8915 }
8916}
59105446 8917EXPORT_SYMBOL_GPL(nft_data_release);
96518518
PM
8918
8919int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
8920 enum nft_data_types type, unsigned int len)
8921{
8922 struct nlattr *nest;
8923 int err;
8924
ae0be8de 8925 nest = nla_nest_start_noflag(skb, attr);
96518518
PM
8926 if (nest == NULL)
8927 return -1;
8928
8929 switch (type) {
8930 case NFT_DATA_VALUE:
8931 err = nft_value_dump(skb, data, len);
8932 break;
8933 case NFT_DATA_VERDICT:
33d5a7b1 8934 err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
96518518
PM
8935 break;
8936 default:
8937 err = -EINVAL;
8938 WARN_ON(1);
8939 }
8940
8941 nla_nest_end(skb, nest);
8942 return err;
8943}
8944EXPORT_SYMBOL_GPL(nft_data_dump);
8945
5ebe0b0e
PNA
8946int __nft_release_basechain(struct nft_ctx *ctx)
8947{
8948 struct nft_rule *rule, *nr;
8949
fa5950e4
FW
8950 if (WARN_ON(!nft_is_base_chain(ctx->chain)))
8951 return 0;
5ebe0b0e 8952
c974a3a3 8953 nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain);
5ebe0b0e
PNA
8954 list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
8955 list_del(&rule->list);
8956 ctx->chain->use--;
bb7b40ae 8957 nf_tables_rule_release(ctx, rule);
5ebe0b0e 8958 }
1b2470e5 8959 nft_chain_del(ctx->chain);
5ebe0b0e 8960 ctx->table->use--;
43a605f2 8961 nf_tables_chain_destroy(ctx);
5ebe0b0e
PNA
8962
8963 return 0;
8964}
8965EXPORT_SYMBOL_GPL(__nft_release_basechain);
8966
98319cb9 8967static void __nft_release_tables(struct net *net)
df05ef87 8968{
3b49e2e9 8969 struct nft_flowtable *flowtable, *nf;
df05ef87
PNA
8970 struct nft_table *table, *nt;
8971 struct nft_chain *chain, *nc;
e5009240 8972 struct nft_object *obj, *ne;
df05ef87
PNA
8973 struct nft_rule *rule, *nr;
8974 struct nft_set *set, *ns;
8975 struct nft_ctx ctx = {
8976 .net = net,
43a605f2 8977 .family = NFPROTO_NETDEV,
df05ef87
PNA
8978 };
8979
36596dad 8980 list_for_each_entry_safe(table, nt, &net->nft.tables, list) {
98319cb9 8981 ctx.family = table->family;
dd4cbef7 8982
df05ef87 8983 list_for_each_entry(chain, &table->chains, list)
c974a3a3 8984 nf_tables_unregister_hook(net, table, chain);
df05ef87
PNA
8985 /* No packets are walking on these chains anymore. */
8986 ctx.table = table;
8987 list_for_each_entry(chain, &table->chains, list) {
8988 ctx.chain = chain;
8989 list_for_each_entry_safe(rule, nr, &chain->rules, list) {
8990 list_del(&rule->list);
8991 chain->use--;
bb7b40ae 8992 nf_tables_rule_release(&ctx, rule);
df05ef87
PNA
8993 }
8994 }
3b49e2e9
PNA
8995 list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) {
8996 list_del(&flowtable->list);
8997 table->use--;
8998 nf_tables_flowtable_destroy(flowtable);
8999 }
df05ef87
PNA
9000 list_for_each_entry_safe(set, ns, &table->sets, list) {
9001 list_del(&set->list);
9002 table->use--;
0c2a85ed 9003 nft_set_destroy(&ctx, set);
df05ef87 9004 }
e5009240 9005 list_for_each_entry_safe(obj, ne, &table->objects, list) {
d152159b 9006 nft_obj_del(obj);
e5009240 9007 table->use--;
00bfb320 9008 nft_obj_destroy(&ctx, obj);
e5009240 9009 }
df05ef87 9010 list_for_each_entry_safe(chain, nc, &table->chains, list) {
43a605f2 9011 ctx.chain = chain;
1b2470e5 9012 nft_chain_del(chain);
df05ef87 9013 table->use--;
43a605f2 9014 nf_tables_chain_destroy(&ctx);
df05ef87
PNA
9015 }
9016 list_del(&table->list);
9017 nf_tables_table_destroy(&ctx);
9018 }
9019}
9020
dd4cbef7
PNA
9021static int __net_init nf_tables_init_net(struct net *net)
9022{
9023 INIT_LIST_HEAD(&net->nft.tables);
9024 INIT_LIST_HEAD(&net->nft.commit_list);
eb014de4 9025 INIT_LIST_HEAD(&net->nft.module_list);
67cc570e 9026 INIT_LIST_HEAD(&net->nft.notify_list);
f102d66b 9027 mutex_init(&net->nft.commit_mutex);
dd4cbef7 9028 net->nft.base_seq = 1;
a654de8f
PNA
9029 net->nft.validate_state = NFT_VALIDATE_SKIP;
9030
dd4cbef7
PNA
9031 return 0;
9032}
9033
9034static void __net_exit nf_tables_exit_net(struct net *net)
9035{
f102d66b 9036 mutex_lock(&net->nft.commit_mutex);
71ad00c5 9037 if (!list_empty(&net->nft.commit_list))
c0391b6a 9038 __nf_tables_abort(net, NFNL_ABORT_NONE);
98319cb9 9039 __nft_release_tables(net);
f102d66b 9040 mutex_unlock(&net->nft.commit_mutex);
dd4cbef7 9041 WARN_ON_ONCE(!list_empty(&net->nft.tables));
1d305ba4 9042 WARN_ON_ONCE(!list_empty(&net->nft.module_list));
67cc570e 9043 WARN_ON_ONCE(!list_empty(&net->nft.notify_list));
dd4cbef7
PNA
9044}
9045
99633ab2
PNA
9046static struct pernet_operations nf_tables_net_ops = {
9047 .init = nf_tables_init_net,
613d0776 9048 .exit = nf_tables_exit_net,
99633ab2
PNA
9049};
9050
96518518
PM
9051static int __init nf_tables_module_init(void)
9052{
9053 int err;
9054
0935d558 9055 spin_lock_init(&nf_tables_destroy_list_lock);
d209df3e
FW
9056 err = register_pernet_subsys(&nf_tables_net_ops);
9057 if (err < 0)
9058 return err;
9059
9060 err = nft_chain_filter_init();
9061 if (err < 0)
9062 goto err1;
02c7b25e 9063
96518518
PM
9064 err = nf_tables_core_module_init();
9065 if (err < 0)
d209df3e 9066 goto err2;
96518518 9067
d209df3e 9068 err = register_netdevice_notifier(&nf_tables_flowtable_notifier);
96518518 9069 if (err < 0)
d209df3e 9070 goto err3;
96518518 9071
4d44175a
FW
9072 err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params);
9073 if (err < 0)
9074 goto err4;
9075
06d392cb 9076 err = nft_offload_init();
9077 if (err < 0)
9078 goto err5;
9079
d209df3e
FW
9080 /* must be last */
9081 err = nfnetlink_subsys_register(&nf_tables_subsys);
9082 if (err < 0)
06d392cb 9083 goto err6;
3b49e2e9 9084
c1deb065 9085 nft_chain_route_init();
3474a2c6 9086
d209df3e 9087 return err;
06d392cb 9088err6:
9089 nft_offload_exit();
4d44175a
FW
9090err5:
9091 rhltable_destroy(&nft_objname_ht);
d209df3e
FW
9092err4:
9093 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
9094err3:
96518518 9095 nf_tables_core_module_exit();
d209df3e
FW
9096err2:
9097 nft_chain_filter_fini();
9098err1:
9099 unregister_pernet_subsys(&nf_tables_net_ops);
96518518
PM
9100 return err;
9101}
9102
9103static void __exit nf_tables_module_exit(void)
9104{
9105 nfnetlink_subsys_unregister(&nf_tables_subsys);
06d392cb 9106 nft_offload_exit();
3b49e2e9 9107 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
0a2cf5ee 9108 nft_chain_filter_fini();
c1deb065 9109 nft_chain_route_fini();
71ad00c5 9110 unregister_pernet_subsys(&nf_tables_net_ops);
0935d558 9111 cancel_work_sync(&trans_destroy_work);
1b1bc49c 9112 rcu_barrier();
4d44175a 9113 rhltable_destroy(&nft_objname_ht);
96518518 9114 nf_tables_core_module_exit();
96518518
PM
9115}
9116
9117module_init(nf_tables_module_init);
9118module_exit(nf_tables_module_exit);
9119
9120MODULE_LICENSE("GPL");
9121MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
9122MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);