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