1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
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>
13 #include <linux/vmalloc.h>
14 #include <linux/rhashtable.h>
15 #include <linux/audit.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nfnetlink.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_flow_table.h>
20 #include <net/netfilter/nf_tables_core.h>
21 #include <net/netfilter/nf_tables.h>
22 #include <net/netfilter/nf_tables_offload.h>
23 #include <net/net_namespace.h>
26 #define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-"))
28 unsigned int nf_tables_net_id __read_mostly;
30 static LIST_HEAD(nf_tables_expressions);
31 static LIST_HEAD(nf_tables_objects);
32 static LIST_HEAD(nf_tables_flowtables);
33 static LIST_HEAD(nf_tables_destroy_list);
34 static DEFINE_SPINLOCK(nf_tables_destroy_list_lock);
37 NFT_VALIDATE_SKIP = 0,
42 static struct rhltable nft_objname_ht;
44 static u32 nft_chain_hash(const void *data, u32 len, u32 seed);
45 static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed);
46 static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *);
48 static u32 nft_objname_hash(const void *data, u32 len, u32 seed);
49 static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed);
50 static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *);
52 static const struct rhashtable_params nft_chain_ht_params = {
53 .head_offset = offsetof(struct nft_chain, rhlhead),
54 .key_offset = offsetof(struct nft_chain, name),
55 .hashfn = nft_chain_hash,
56 .obj_hashfn = nft_chain_hash_obj,
57 .obj_cmpfn = nft_chain_hash_cmp,
58 .automatic_shrinking = true,
61 static const struct rhashtable_params nft_objname_ht_params = {
62 .head_offset = offsetof(struct nft_object, rhlhead),
63 .key_offset = offsetof(struct nft_object, key),
64 .hashfn = nft_objname_hash,
65 .obj_hashfn = nft_objname_hash_obj,
66 .obj_cmpfn = nft_objname_hash_cmp,
67 .automatic_shrinking = true,
70 struct nft_audit_data {
71 struct nft_table *table;
74 struct list_head list;
77 static const u8 nft2audit_op[NFT_MSG_MAX] = { // enum nf_tables_msg_types
78 [NFT_MSG_NEWTABLE] = AUDIT_NFT_OP_TABLE_REGISTER,
79 [NFT_MSG_GETTABLE] = AUDIT_NFT_OP_INVALID,
80 [NFT_MSG_DELTABLE] = AUDIT_NFT_OP_TABLE_UNREGISTER,
81 [NFT_MSG_NEWCHAIN] = AUDIT_NFT_OP_CHAIN_REGISTER,
82 [NFT_MSG_GETCHAIN] = AUDIT_NFT_OP_INVALID,
83 [NFT_MSG_DELCHAIN] = AUDIT_NFT_OP_CHAIN_UNREGISTER,
84 [NFT_MSG_NEWRULE] = AUDIT_NFT_OP_RULE_REGISTER,
85 [NFT_MSG_GETRULE] = AUDIT_NFT_OP_INVALID,
86 [NFT_MSG_DELRULE] = AUDIT_NFT_OP_RULE_UNREGISTER,
87 [NFT_MSG_NEWSET] = AUDIT_NFT_OP_SET_REGISTER,
88 [NFT_MSG_GETSET] = AUDIT_NFT_OP_INVALID,
89 [NFT_MSG_DELSET] = AUDIT_NFT_OP_SET_UNREGISTER,
90 [NFT_MSG_NEWSETELEM] = AUDIT_NFT_OP_SETELEM_REGISTER,
91 [NFT_MSG_GETSETELEM] = AUDIT_NFT_OP_INVALID,
92 [NFT_MSG_DELSETELEM] = AUDIT_NFT_OP_SETELEM_UNREGISTER,
93 [NFT_MSG_NEWGEN] = AUDIT_NFT_OP_GEN_REGISTER,
94 [NFT_MSG_GETGEN] = AUDIT_NFT_OP_INVALID,
95 [NFT_MSG_TRACE] = AUDIT_NFT_OP_INVALID,
96 [NFT_MSG_NEWOBJ] = AUDIT_NFT_OP_OBJ_REGISTER,
97 [NFT_MSG_GETOBJ] = AUDIT_NFT_OP_INVALID,
98 [NFT_MSG_DELOBJ] = AUDIT_NFT_OP_OBJ_UNREGISTER,
99 [NFT_MSG_GETOBJ_RESET] = AUDIT_NFT_OP_OBJ_RESET,
100 [NFT_MSG_NEWFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_REGISTER,
101 [NFT_MSG_GETFLOWTABLE] = AUDIT_NFT_OP_INVALID,
102 [NFT_MSG_DELFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_UNREGISTER,
105 static void nft_validate_state_update(struct nft_table *table, u8 new_validate_state)
107 switch (table->validate_state) {
108 case NFT_VALIDATE_SKIP:
109 WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO);
111 case NFT_VALIDATE_NEED:
113 case NFT_VALIDATE_DO:
114 if (new_validate_state == NFT_VALIDATE_NEED)
118 table->validate_state = new_validate_state;
120 static void nf_tables_trans_destroy_work(struct work_struct *w);
121 static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work);
123 static void nft_ctx_init(struct nft_ctx *ctx,
125 const struct sk_buff *skb,
126 const struct nlmsghdr *nlh,
128 struct nft_table *table,
129 struct nft_chain *chain,
130 const struct nlattr * const *nla)
133 ctx->family = family;
138 ctx->portid = NETLINK_CB(skb).portid;
139 ctx->report = nlmsg_report(nlh);
140 ctx->flags = nlh->nlmsg_flags;
141 ctx->seq = nlh->nlmsg_seq;
144 static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx,
145 int msg_type, u32 size, gfp_t gfp)
147 struct nft_trans *trans;
149 trans = kzalloc(sizeof(struct nft_trans) + size, gfp);
153 INIT_LIST_HEAD(&trans->list);
154 trans->msg_type = msg_type;
160 static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx,
161 int msg_type, u32 size)
163 return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL);
166 static void nft_trans_destroy(struct nft_trans *trans)
168 list_del(&trans->list);
172 static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set)
174 struct nftables_pernet *nft_net;
175 struct net *net = ctx->net;
176 struct nft_trans *trans;
178 if (!nft_set_is_anonymous(set))
181 nft_net = nft_pernet(net);
182 list_for_each_entry_reverse(trans, &nft_net->commit_list, list) {
183 switch (trans->msg_type) {
185 if (nft_trans_set(trans) == set)
186 nft_trans_set_bound(trans) = true;
188 case NFT_MSG_NEWSETELEM:
189 if (nft_trans_elem_set(trans) == set)
190 nft_trans_elem_set_bound(trans) = true;
196 static int nft_netdev_register_hooks(struct net *net,
197 struct list_head *hook_list)
199 struct nft_hook *hook;
203 list_for_each_entry(hook, hook_list, list) {
204 err = nf_register_net_hook(net, &hook->ops);
213 list_for_each_entry(hook, hook_list, list) {
217 nf_unregister_net_hook(net, &hook->ops);
222 static void nft_netdev_unregister_hooks(struct net *net,
223 struct list_head *hook_list,
226 struct nft_hook *hook, *next;
228 list_for_each_entry_safe(hook, next, hook_list, list) {
229 nf_unregister_net_hook(net, &hook->ops);
230 if (release_netdev) {
231 list_del(&hook->list);
232 kfree_rcu(hook, rcu);
237 static int nf_tables_register_hook(struct net *net,
238 const struct nft_table *table,
239 struct nft_chain *chain)
241 struct nft_base_chain *basechain;
242 const struct nf_hook_ops *ops;
244 if (table->flags & NFT_TABLE_F_DORMANT ||
245 !nft_is_base_chain(chain))
248 basechain = nft_base_chain(chain);
249 ops = &basechain->ops;
251 if (basechain->type->ops_register)
252 return basechain->type->ops_register(net, ops);
254 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
255 return nft_netdev_register_hooks(net, &basechain->hook_list);
257 return nf_register_net_hook(net, &basechain->ops);
260 static void __nf_tables_unregister_hook(struct net *net,
261 const struct nft_table *table,
262 struct nft_chain *chain,
265 struct nft_base_chain *basechain;
266 const struct nf_hook_ops *ops;
268 if (table->flags & NFT_TABLE_F_DORMANT ||
269 !nft_is_base_chain(chain))
271 basechain = nft_base_chain(chain);
272 ops = &basechain->ops;
274 if (basechain->type->ops_unregister)
275 return basechain->type->ops_unregister(net, ops);
277 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
278 nft_netdev_unregister_hooks(net, &basechain->hook_list,
281 nf_unregister_net_hook(net, &basechain->ops);
284 static void nf_tables_unregister_hook(struct net *net,
285 const struct nft_table *table,
286 struct nft_chain *chain)
288 return __nf_tables_unregister_hook(net, table, chain, false);
291 static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *trans)
293 struct nftables_pernet *nft_net = nft_pernet(net);
295 list_add_tail(&trans->list, &nft_net->commit_list);
298 static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
300 struct nft_trans *trans;
302 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
306 if (msg_type == NFT_MSG_NEWTABLE)
307 nft_activate_next(ctx->net, ctx->table);
309 nft_trans_commit_list_add_tail(ctx->net, trans);
313 static int nft_deltable(struct nft_ctx *ctx)
317 err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
321 nft_deactivate_next(ctx->net, ctx->table);
325 static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
327 struct nft_trans *trans;
329 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
331 return ERR_PTR(-ENOMEM);
333 if (msg_type == NFT_MSG_NEWCHAIN) {
334 nft_activate_next(ctx->net, ctx->chain);
336 if (ctx->nla[NFTA_CHAIN_ID]) {
337 nft_trans_chain_id(trans) =
338 ntohl(nla_get_be32(ctx->nla[NFTA_CHAIN_ID]));
342 nft_trans_commit_list_add_tail(ctx->net, trans);
346 static int nft_delchain(struct nft_ctx *ctx)
348 struct nft_trans *trans;
350 trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
352 return PTR_ERR(trans);
355 nft_deactivate_next(ctx->net, ctx->chain);
360 static void nft_rule_expr_activate(const struct nft_ctx *ctx,
361 struct nft_rule *rule)
363 struct nft_expr *expr;
365 expr = nft_expr_first(rule);
366 while (nft_expr_more(rule, expr)) {
367 if (expr->ops->activate)
368 expr->ops->activate(ctx, expr);
370 expr = nft_expr_next(expr);
374 static void nft_rule_expr_deactivate(const struct nft_ctx *ctx,
375 struct nft_rule *rule,
376 enum nft_trans_phase phase)
378 struct nft_expr *expr;
380 expr = nft_expr_first(rule);
381 while (nft_expr_more(rule, expr)) {
382 if (expr->ops->deactivate)
383 expr->ops->deactivate(ctx, expr, phase);
385 expr = nft_expr_next(expr);
390 nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
392 /* You cannot delete the same rule twice */
393 if (nft_is_active_next(ctx->net, rule)) {
394 nft_deactivate_next(ctx->net, rule);
401 static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
402 struct nft_rule *rule)
404 struct nft_trans *trans;
406 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
410 if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) {
411 nft_trans_rule_id(trans) =
412 ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID]));
414 nft_trans_rule(trans) = rule;
415 nft_trans_commit_list_add_tail(ctx->net, trans);
420 static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
422 struct nft_flow_rule *flow;
423 struct nft_trans *trans;
426 trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
430 if (ctx->chain->flags & NFT_CHAIN_HW_OFFLOAD) {
431 flow = nft_flow_rule_create(ctx->net, rule);
433 nft_trans_destroy(trans);
434 return PTR_ERR(flow);
437 nft_trans_flow_rule(trans) = flow;
440 err = nf_tables_delrule_deactivate(ctx, rule);
442 nft_trans_destroy(trans);
445 nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE);
450 static int nft_delrule_by_chain(struct nft_ctx *ctx)
452 struct nft_rule *rule;
455 list_for_each_entry(rule, &ctx->chain->rules, list) {
456 if (!nft_is_active_next(ctx->net, rule))
459 err = nft_delrule(ctx, rule);
466 static int __nft_trans_set_add(const struct nft_ctx *ctx, int msg_type,
468 const struct nft_set_desc *desc)
470 struct nft_trans *trans;
472 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
476 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] && !desc) {
477 nft_trans_set_id(trans) =
478 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
479 nft_activate_next(ctx->net, set);
481 nft_trans_set(trans) = set;
483 nft_trans_set_update(trans) = true;
484 nft_trans_set_gc_int(trans) = desc->gc_int;
485 nft_trans_set_timeout(trans) = desc->timeout;
487 nft_trans_commit_list_add_tail(ctx->net, trans);
492 static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type,
495 return __nft_trans_set_add(ctx, msg_type, set, NULL);
498 static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set)
502 err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
506 nft_deactivate_next(ctx->net, set);
512 static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type,
513 struct nft_object *obj)
515 struct nft_trans *trans;
517 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj));
521 if (msg_type == NFT_MSG_NEWOBJ)
522 nft_activate_next(ctx->net, obj);
524 nft_trans_obj(trans) = obj;
525 nft_trans_commit_list_add_tail(ctx->net, trans);
530 static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj)
534 err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj);
538 nft_deactivate_next(ctx->net, obj);
544 static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type,
545 struct nft_flowtable *flowtable)
547 struct nft_trans *trans;
549 trans = nft_trans_alloc(ctx, msg_type,
550 sizeof(struct nft_trans_flowtable));
554 if (msg_type == NFT_MSG_NEWFLOWTABLE)
555 nft_activate_next(ctx->net, flowtable);
557 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
558 nft_trans_flowtable(trans) = flowtable;
559 nft_trans_commit_list_add_tail(ctx->net, trans);
564 static int nft_delflowtable(struct nft_ctx *ctx,
565 struct nft_flowtable *flowtable)
569 err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable);
573 nft_deactivate_next(ctx->net, flowtable);
579 static void __nft_reg_track_clobber(struct nft_regs_track *track, u8 dreg)
583 for (i = track->regs[dreg].num_reg; i > 0; i--)
584 __nft_reg_track_cancel(track, dreg - i);
587 static void __nft_reg_track_update(struct nft_regs_track *track,
588 const struct nft_expr *expr,
591 track->regs[dreg].selector = expr;
592 track->regs[dreg].bitwise = NULL;
593 track->regs[dreg].num_reg = num_reg;
596 void nft_reg_track_update(struct nft_regs_track *track,
597 const struct nft_expr *expr, u8 dreg, u8 len)
599 unsigned int regcount;
602 __nft_reg_track_clobber(track, dreg);
604 regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE);
605 for (i = 0; i < regcount; i++, dreg++)
606 __nft_reg_track_update(track, expr, dreg, i);
608 EXPORT_SYMBOL_GPL(nft_reg_track_update);
610 void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len)
612 unsigned int regcount;
615 __nft_reg_track_clobber(track, dreg);
617 regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE);
618 for (i = 0; i < regcount; i++, dreg++)
619 __nft_reg_track_cancel(track, dreg);
621 EXPORT_SYMBOL_GPL(nft_reg_track_cancel);
623 void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg)
625 track->regs[dreg].selector = NULL;
626 track->regs[dreg].bitwise = NULL;
627 track->regs[dreg].num_reg = 0;
629 EXPORT_SYMBOL_GPL(__nft_reg_track_cancel);
635 static struct nft_table *nft_table_lookup(const struct net *net,
636 const struct nlattr *nla,
637 u8 family, u8 genmask, u32 nlpid)
639 struct nftables_pernet *nft_net;
640 struct nft_table *table;
643 return ERR_PTR(-EINVAL);
645 nft_net = nft_pernet(net);
646 list_for_each_entry_rcu(table, &nft_net->tables, list,
647 lockdep_is_held(&nft_net->commit_mutex)) {
648 if (!nla_strcmp(nla, table->name) &&
649 table->family == family &&
650 nft_active_genmask(table, genmask)) {
651 if (nft_table_has_owner(table) &&
652 nlpid && table->nlpid != nlpid)
653 return ERR_PTR(-EPERM);
659 return ERR_PTR(-ENOENT);
662 static struct nft_table *nft_table_lookup_byhandle(const struct net *net,
663 const struct nlattr *nla,
664 u8 genmask, u32 nlpid)
666 struct nftables_pernet *nft_net;
667 struct nft_table *table;
669 nft_net = nft_pernet(net);
670 list_for_each_entry(table, &nft_net->tables, list) {
671 if (be64_to_cpu(nla_get_be64(nla)) == table->handle &&
672 nft_active_genmask(table, genmask)) {
673 if (nft_table_has_owner(table) &&
674 nlpid && table->nlpid != nlpid)
675 return ERR_PTR(-EPERM);
681 return ERR_PTR(-ENOENT);
684 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
686 return ++table->hgenerator;
689 static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX];
691 static const struct nft_chain_type *
692 __nft_chain_type_get(u8 family, enum nft_chain_types type)
694 if (family >= NFPROTO_NUMPROTO ||
695 type >= NFT_CHAIN_T_MAX)
698 return chain_type[family][type];
701 static const struct nft_chain_type *
702 __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family)
704 const struct nft_chain_type *type;
707 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
708 type = __nft_chain_type_get(family, i);
711 if (!nla_strcmp(nla, type->name))
717 struct nft_module_request {
718 struct list_head list;
719 char module[MODULE_NAME_LEN];
723 #ifdef CONFIG_MODULES
724 __printf(2, 3) int nft_request_module(struct net *net, const char *fmt,
727 char module_name[MODULE_NAME_LEN];
728 struct nftables_pernet *nft_net;
729 struct nft_module_request *req;
734 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
736 if (ret >= MODULE_NAME_LEN)
739 nft_net = nft_pernet(net);
740 list_for_each_entry(req, &nft_net->module_list, list) {
741 if (!strcmp(req->module, module_name)) {
745 /* A request to load this module already exists. */
750 req = kmalloc(sizeof(*req), GFP_KERNEL);
755 strscpy(req->module, module_name, MODULE_NAME_LEN);
756 list_add_tail(&req->list, &nft_net->module_list);
760 EXPORT_SYMBOL_GPL(nft_request_module);
763 static void lockdep_nfnl_nft_mutex_not_held(void)
765 #ifdef CONFIG_PROVE_LOCKING
767 WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES));
771 static const struct nft_chain_type *
772 nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla,
773 u8 family, bool autoload)
775 const struct nft_chain_type *type;
777 type = __nf_tables_chain_type_lookup(nla, family);
781 lockdep_nfnl_nft_mutex_not_held();
782 #ifdef CONFIG_MODULES
784 if (nft_request_module(net, "nft-chain-%u-%.*s", family,
786 (const char *)nla_data(nla)) == -EAGAIN)
787 return ERR_PTR(-EAGAIN);
790 return ERR_PTR(-ENOENT);
793 static __be16 nft_base_seq(const struct net *net)
795 struct nftables_pernet *nft_net = nft_pernet(net);
797 return htons(nft_net->base_seq & 0xffff);
800 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
801 [NFTA_TABLE_NAME] = { .type = NLA_STRING,
802 .len = NFT_TABLE_MAXNAMELEN - 1 },
803 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
804 [NFTA_TABLE_HANDLE] = { .type = NLA_U64 },
805 [NFTA_TABLE_USERDATA] = { .type = NLA_BINARY,
806 .len = NFT_USERDATA_MAXLEN }
809 static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
810 u32 portid, u32 seq, int event, u32 flags,
811 int family, const struct nft_table *table)
813 struct nlmsghdr *nlh;
815 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
816 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
817 NFNETLINK_V0, nft_base_seq(net));
819 goto nla_put_failure;
821 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
822 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) ||
823 nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle),
825 goto nla_put_failure;
827 if (event == NFT_MSG_DELTABLE) {
832 if (nla_put_be32(skb, NFTA_TABLE_FLAGS,
833 htonl(table->flags & NFT_TABLE_F_MASK)))
834 goto nla_put_failure;
836 if (nft_table_has_owner(table) &&
837 nla_put_be32(skb, NFTA_TABLE_OWNER, htonl(table->nlpid)))
838 goto nla_put_failure;
841 if (nla_put(skb, NFTA_TABLE_USERDATA, table->udlen, table->udata))
842 goto nla_put_failure;
849 nlmsg_trim(skb, nlh);
853 struct nftnl_skb_parms {
856 #define NFT_CB(skb) (*(struct nftnl_skb_parms*)&((skb)->cb))
858 static void nft_notify_enqueue(struct sk_buff *skb, bool report,
859 struct list_head *notify_list)
861 NFT_CB(skb).report = report;
862 list_add_tail(&skb->list, notify_list);
865 static void nf_tables_table_notify(const struct nft_ctx *ctx, int event)
867 struct nftables_pernet *nft_net;
873 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
876 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
880 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
881 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
883 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
884 event, flags, ctx->family, ctx->table);
890 nft_net = nft_pernet(ctx->net);
891 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
894 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
897 static int nf_tables_dump_tables(struct sk_buff *skb,
898 struct netlink_callback *cb)
900 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
901 struct nftables_pernet *nft_net;
902 const struct nft_table *table;
903 unsigned int idx = 0, s_idx = cb->args[0];
904 struct net *net = sock_net(skb->sk);
905 int family = nfmsg->nfgen_family;
908 nft_net = nft_pernet(net);
909 cb->seq = READ_ONCE(nft_net->base_seq);
911 list_for_each_entry_rcu(table, &nft_net->tables, list) {
912 if (family != NFPROTO_UNSPEC && family != table->family)
918 memset(&cb->args[1], 0,
919 sizeof(cb->args) - sizeof(cb->args[0]));
920 if (!nft_is_active(net, table))
922 if (nf_tables_fill_table_info(skb, net,
923 NETLINK_CB(cb->skb).portid,
925 NFT_MSG_NEWTABLE, NLM_F_MULTI,
926 table->family, table) < 0)
929 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
939 static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb,
940 const struct nlmsghdr *nlh,
941 struct netlink_dump_control *c)
945 if (!try_module_get(THIS_MODULE))
949 err = netlink_dump_start(nlsk, skb, nlh, c);
951 module_put(THIS_MODULE);
956 /* called with rcu_read_lock held */
957 static int nf_tables_gettable(struct sk_buff *skb, const struct nfnl_info *info,
958 const struct nlattr * const nla[])
960 struct netlink_ext_ack *extack = info->extack;
961 u8 genmask = nft_genmask_cur(info->net);
962 u8 family = info->nfmsg->nfgen_family;
963 const struct nft_table *table;
964 struct net *net = info->net;
965 struct sk_buff *skb2;
968 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
969 struct netlink_dump_control c = {
970 .dump = nf_tables_dump_tables,
971 .module = THIS_MODULE,
974 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
977 table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask, 0);
979 NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]);
980 return PTR_ERR(table);
983 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
987 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
988 info->nlh->nlmsg_seq, NFT_MSG_NEWTABLE,
991 goto err_fill_table_info;
993 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
1000 static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt)
1002 struct nft_chain *chain;
1005 list_for_each_entry(chain, &table->chains, list) {
1006 if (!nft_is_active_next(net, chain))
1008 if (!nft_is_base_chain(chain))
1011 if (cnt && i++ == cnt)
1014 nf_tables_unregister_hook(net, table, chain);
1018 static int nf_tables_table_enable(struct net *net, struct nft_table *table)
1020 struct nft_chain *chain;
1023 list_for_each_entry(chain, &table->chains, list) {
1024 if (!nft_is_active_next(net, chain))
1026 if (!nft_is_base_chain(chain))
1029 err = nf_tables_register_hook(net, table, chain);
1031 goto err_register_hooks;
1039 nft_table_disable(net, table, i);
1043 static void nf_tables_table_disable(struct net *net, struct nft_table *table)
1045 table->flags &= ~NFT_TABLE_F_DORMANT;
1046 nft_table_disable(net, table, 0);
1047 table->flags |= NFT_TABLE_F_DORMANT;
1050 #define __NFT_TABLE_F_INTERNAL (NFT_TABLE_F_MASK + 1)
1051 #define __NFT_TABLE_F_WAS_DORMANT (__NFT_TABLE_F_INTERNAL << 0)
1052 #define __NFT_TABLE_F_WAS_AWAKEN (__NFT_TABLE_F_INTERNAL << 1)
1053 #define __NFT_TABLE_F_UPDATE (__NFT_TABLE_F_WAS_DORMANT | \
1054 __NFT_TABLE_F_WAS_AWAKEN)
1056 static int nf_tables_updtable(struct nft_ctx *ctx)
1058 struct nft_trans *trans;
1062 if (!ctx->nla[NFTA_TABLE_FLAGS])
1065 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
1066 if (flags & ~NFT_TABLE_F_MASK)
1069 if (flags == ctx->table->flags)
1072 if ((nft_table_has_owner(ctx->table) &&
1073 !(flags & NFT_TABLE_F_OWNER)) ||
1074 (!nft_table_has_owner(ctx->table) &&
1075 flags & NFT_TABLE_F_OWNER))
1078 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
1079 sizeof(struct nft_trans_table));
1083 if ((flags & NFT_TABLE_F_DORMANT) &&
1084 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
1085 ctx->table->flags |= NFT_TABLE_F_DORMANT;
1086 if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE))
1087 ctx->table->flags |= __NFT_TABLE_F_WAS_AWAKEN;
1088 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
1089 ctx->table->flags & NFT_TABLE_F_DORMANT) {
1090 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
1091 if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) {
1092 ret = nf_tables_table_enable(ctx->net, ctx->table);
1094 goto err_register_hooks;
1096 ctx->table->flags |= __NFT_TABLE_F_WAS_DORMANT;
1100 nft_trans_table_update(trans) = true;
1101 nft_trans_commit_list_add_tail(ctx->net, trans);
1106 nft_trans_destroy(trans);
1110 static u32 nft_chain_hash(const void *data, u32 len, u32 seed)
1112 const char *name = data;
1114 return jhash(name, strlen(name), seed);
1117 static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed)
1119 const struct nft_chain *chain = data;
1121 return nft_chain_hash(chain->name, 0, seed);
1124 static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg,
1127 const struct nft_chain *chain = ptr;
1128 const char *name = arg->key;
1130 return strcmp(chain->name, name);
1133 static u32 nft_objname_hash(const void *data, u32 len, u32 seed)
1135 const struct nft_object_hash_key *k = data;
1137 seed ^= hash_ptr(k->table, 32);
1139 return jhash(k->name, strlen(k->name), seed);
1142 static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed)
1144 const struct nft_object *obj = data;
1146 return nft_objname_hash(&obj->key, 0, seed);
1149 static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg,
1152 const struct nft_object_hash_key *k = arg->key;
1153 const struct nft_object *obj = ptr;
1155 if (obj->key.table != k->table)
1158 return strcmp(obj->key.name, k->name);
1161 static bool nft_supported_family(u8 family)
1164 #ifdef CONFIG_NF_TABLES_INET
1165 || family == NFPROTO_INET
1167 #ifdef CONFIG_NF_TABLES_IPV4
1168 || family == NFPROTO_IPV4
1170 #ifdef CONFIG_NF_TABLES_ARP
1171 || family == NFPROTO_ARP
1173 #ifdef CONFIG_NF_TABLES_NETDEV
1174 || family == NFPROTO_NETDEV
1176 #if IS_ENABLED(CONFIG_NF_TABLES_BRIDGE)
1177 || family == NFPROTO_BRIDGE
1179 #ifdef CONFIG_NF_TABLES_IPV6
1180 || family == NFPROTO_IPV6
1185 static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info,
1186 const struct nlattr * const nla[])
1188 struct nftables_pernet *nft_net = nft_pernet(info->net);
1189 struct netlink_ext_ack *extack = info->extack;
1190 u8 genmask = nft_genmask_next(info->net);
1191 u8 family = info->nfmsg->nfgen_family;
1192 struct net *net = info->net;
1193 const struct nlattr *attr;
1194 struct nft_table *table;
1199 if (!nft_supported_family(family))
1202 lockdep_assert_held(&nft_net->commit_mutex);
1203 attr = nla[NFTA_TABLE_NAME];
1204 table = nft_table_lookup(net, attr, family, genmask,
1205 NETLINK_CB(skb).portid);
1206 if (IS_ERR(table)) {
1207 if (PTR_ERR(table) != -ENOENT)
1208 return PTR_ERR(table);
1210 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
1211 NL_SET_BAD_ATTR(extack, attr);
1214 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1217 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
1219 return nf_tables_updtable(&ctx);
1222 if (nla[NFTA_TABLE_FLAGS]) {
1223 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
1224 if (flags & ~NFT_TABLE_F_MASK)
1229 table = kzalloc(sizeof(*table), GFP_KERNEL_ACCOUNT);
1233 table->validate_state = NFT_VALIDATE_SKIP;
1234 table->name = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
1235 if (table->name == NULL)
1238 if (nla[NFTA_TABLE_USERDATA]) {
1239 table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL_ACCOUNT);
1240 if (table->udata == NULL)
1241 goto err_table_udata;
1243 table->udlen = nla_len(nla[NFTA_TABLE_USERDATA]);
1246 err = rhltable_init(&table->chains_ht, &nft_chain_ht_params);
1250 INIT_LIST_HEAD(&table->chains);
1251 INIT_LIST_HEAD(&table->sets);
1252 INIT_LIST_HEAD(&table->objects);
1253 INIT_LIST_HEAD(&table->flowtables);
1254 table->family = family;
1255 table->flags = flags;
1256 table->handle = ++nft_net->table_handle;
1257 if (table->flags & NFT_TABLE_F_OWNER)
1258 table->nlpid = NETLINK_CB(skb).portid;
1260 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
1261 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
1265 list_add_tail_rcu(&table->list, &nft_net->tables);
1268 rhltable_destroy(&table->chains_ht);
1270 kfree(table->udata);
1279 static int nft_flush_table(struct nft_ctx *ctx)
1281 struct nft_flowtable *flowtable, *nft;
1282 struct nft_chain *chain, *nc;
1283 struct nft_object *obj, *ne;
1284 struct nft_set *set, *ns;
1287 list_for_each_entry(chain, &ctx->table->chains, list) {
1288 if (!nft_is_active_next(ctx->net, chain))
1291 if (nft_chain_is_bound(chain))
1296 err = nft_delrule_by_chain(ctx);
1301 list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
1302 if (!nft_is_active_next(ctx->net, set))
1305 if (nft_set_is_anonymous(set) &&
1306 !list_empty(&set->bindings))
1309 err = nft_delset(ctx, set);
1314 list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) {
1315 if (!nft_is_active_next(ctx->net, flowtable))
1318 err = nft_delflowtable(ctx, flowtable);
1323 list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) {
1324 if (!nft_is_active_next(ctx->net, obj))
1327 err = nft_delobj(ctx, obj);
1332 list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
1333 if (!nft_is_active_next(ctx->net, chain))
1336 if (nft_chain_is_bound(chain))
1341 err = nft_delchain(ctx);
1346 err = nft_deltable(ctx);
1351 static int nft_flush(struct nft_ctx *ctx, int family)
1353 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
1354 const struct nlattr * const *nla = ctx->nla;
1355 struct nft_table *table, *nt;
1358 list_for_each_entry_safe(table, nt, &nft_net->tables, list) {
1359 if (family != AF_UNSPEC && table->family != family)
1362 ctx->family = table->family;
1364 if (!nft_is_active_next(ctx->net, table))
1367 if (nft_table_has_owner(table) && table->nlpid != ctx->portid)
1370 if (nla[NFTA_TABLE_NAME] &&
1371 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
1376 err = nft_flush_table(ctx);
1384 static int nf_tables_deltable(struct sk_buff *skb, const struct nfnl_info *info,
1385 const struct nlattr * const nla[])
1387 struct netlink_ext_ack *extack = info->extack;
1388 u8 genmask = nft_genmask_next(info->net);
1389 u8 family = info->nfmsg->nfgen_family;
1390 struct net *net = info->net;
1391 const struct nlattr *attr;
1392 struct nft_table *table;
1395 nft_ctx_init(&ctx, net, skb, info->nlh, 0, NULL, NULL, nla);
1396 if (family == AF_UNSPEC ||
1397 (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE]))
1398 return nft_flush(&ctx, family);
1400 if (nla[NFTA_TABLE_HANDLE]) {
1401 attr = nla[NFTA_TABLE_HANDLE];
1402 table = nft_table_lookup_byhandle(net, attr, genmask,
1403 NETLINK_CB(skb).portid);
1405 attr = nla[NFTA_TABLE_NAME];
1406 table = nft_table_lookup(net, attr, family, genmask,
1407 NETLINK_CB(skb).portid);
1410 if (IS_ERR(table)) {
1411 if (PTR_ERR(table) == -ENOENT &&
1412 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYTABLE)
1415 NL_SET_BAD_ATTR(extack, attr);
1416 return PTR_ERR(table);
1419 if (info->nlh->nlmsg_flags & NLM_F_NONREC &&
1423 ctx.family = family;
1426 return nft_flush_table(&ctx);
1429 static void nf_tables_table_destroy(struct nft_ctx *ctx)
1431 if (WARN_ON(ctx->table->use > 0))
1434 rhltable_destroy(&ctx->table->chains_ht);
1435 kfree(ctx->table->name);
1436 kfree(ctx->table->udata);
1440 void nft_register_chain_type(const struct nft_chain_type *ctype)
1442 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1443 if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) {
1444 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1447 chain_type[ctype->family][ctype->type] = ctype;
1448 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1450 EXPORT_SYMBOL_GPL(nft_register_chain_type);
1452 void nft_unregister_chain_type(const struct nft_chain_type *ctype)
1454 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1455 chain_type[ctype->family][ctype->type] = NULL;
1456 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1458 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
1464 static struct nft_chain *
1465 nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask)
1467 struct nft_chain *chain;
1469 list_for_each_entry(chain, &table->chains, list) {
1470 if (chain->handle == handle &&
1471 nft_active_genmask(chain, genmask))
1475 return ERR_PTR(-ENOENT);
1478 static bool lockdep_commit_lock_is_held(const struct net *net)
1480 #ifdef CONFIG_PROVE_LOCKING
1481 struct nftables_pernet *nft_net = nft_pernet(net);
1483 return lockdep_is_held(&nft_net->commit_mutex);
1489 static struct nft_chain *nft_chain_lookup(struct net *net,
1490 struct nft_table *table,
1491 const struct nlattr *nla, u8 genmask)
1493 char search[NFT_CHAIN_MAXNAMELEN + 1];
1494 struct rhlist_head *tmp, *list;
1495 struct nft_chain *chain;
1498 return ERR_PTR(-EINVAL);
1500 nla_strscpy(search, nla, sizeof(search));
1502 WARN_ON(!rcu_read_lock_held() &&
1503 !lockdep_commit_lock_is_held(net));
1505 chain = ERR_PTR(-ENOENT);
1507 list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params);
1511 rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
1512 if (nft_active_genmask(chain, genmask))
1515 chain = ERR_PTR(-ENOENT);
1521 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
1522 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING,
1523 .len = NFT_TABLE_MAXNAMELEN - 1 },
1524 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
1525 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
1526 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1527 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
1528 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
1529 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING,
1530 .len = NFT_MODULE_AUTOLOAD_LIMIT },
1531 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
1532 [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 },
1533 [NFTA_CHAIN_ID] = { .type = NLA_U32 },
1534 [NFTA_CHAIN_USERDATA] = { .type = NLA_BINARY,
1535 .len = NFT_USERDATA_MAXLEN },
1538 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
1539 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
1540 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
1541 [NFTA_HOOK_DEV] = { .type = NLA_STRING,
1542 .len = IFNAMSIZ - 1 },
1545 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
1547 struct nft_stats *cpu_stats, total;
1548 struct nlattr *nest;
1556 memset(&total, 0, sizeof(total));
1557 for_each_possible_cpu(cpu) {
1558 cpu_stats = per_cpu_ptr(stats, cpu);
1560 seq = u64_stats_fetch_begin(&cpu_stats->syncp);
1561 pkts = cpu_stats->pkts;
1562 bytes = cpu_stats->bytes;
1563 } while (u64_stats_fetch_retry(&cpu_stats->syncp, seq));
1565 total.bytes += bytes;
1567 nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS);
1569 goto nla_put_failure;
1571 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts),
1572 NFTA_COUNTER_PAD) ||
1573 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
1575 goto nla_put_failure;
1577 nla_nest_end(skb, nest);
1584 static int nft_dump_basechain_hook(struct sk_buff *skb, int family,
1585 const struct nft_base_chain *basechain,
1586 const struct list_head *hook_list)
1588 const struct nf_hook_ops *ops = &basechain->ops;
1589 struct nft_hook *hook, *first = NULL;
1590 struct nlattr *nest, *nest_devs;
1593 nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK);
1595 goto nla_put_failure;
1596 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
1597 goto nla_put_failure;
1598 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
1599 goto nla_put_failure;
1601 if (nft_base_chain_netdev(family, ops->hooknum)) {
1602 nest_devs = nla_nest_start_noflag(skb, NFTA_HOOK_DEVS);
1605 hook_list = &basechain->hook_list;
1607 list_for_each_entry(hook, hook_list, list) {
1611 if (nla_put_string(skb, NFTA_DEVICE_NAME,
1612 hook->ops.dev->name))
1613 goto nla_put_failure;
1616 nla_nest_end(skb, nest_devs);
1619 nla_put_string(skb, NFTA_HOOK_DEV, first->ops.dev->name))
1620 goto nla_put_failure;
1622 nla_nest_end(skb, nest);
1629 static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
1630 u32 portid, u32 seq, int event, u32 flags,
1631 int family, const struct nft_table *table,
1632 const struct nft_chain *chain,
1633 const struct list_head *hook_list)
1635 struct nlmsghdr *nlh;
1637 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
1638 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
1639 NFNETLINK_V0, nft_base_seq(net));
1641 goto nla_put_failure;
1643 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name) ||
1644 nla_put_string(skb, NFTA_CHAIN_NAME, chain->name) ||
1645 nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle),
1647 goto nla_put_failure;
1649 if (event == NFT_MSG_DELCHAIN) {
1650 nlmsg_end(skb, nlh);
1654 if (nft_is_base_chain(chain)) {
1655 const struct nft_base_chain *basechain = nft_base_chain(chain);
1656 struct nft_stats __percpu *stats;
1658 if (nft_dump_basechain_hook(skb, family, basechain, hook_list))
1659 goto nla_put_failure;
1661 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
1662 htonl(basechain->policy)))
1663 goto nla_put_failure;
1665 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
1666 goto nla_put_failure;
1668 stats = rcu_dereference_check(basechain->stats,
1669 lockdep_commit_lock_is_held(net));
1670 if (nft_dump_stats(skb, stats))
1671 goto nla_put_failure;
1675 nla_put_be32(skb, NFTA_CHAIN_FLAGS, htonl(chain->flags)))
1676 goto nla_put_failure;
1678 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
1679 goto nla_put_failure;
1682 nla_put(skb, NFTA_CHAIN_USERDATA, chain->udlen, chain->udata))
1683 goto nla_put_failure;
1685 nlmsg_end(skb, nlh);
1689 nlmsg_trim(skb, nlh);
1693 static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event,
1694 const struct list_head *hook_list)
1696 struct nftables_pernet *nft_net;
1697 struct sk_buff *skb;
1702 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1705 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1709 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
1710 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
1712 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
1713 event, flags, ctx->family, ctx->table,
1714 ctx->chain, hook_list);
1720 nft_net = nft_pernet(ctx->net);
1721 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
1724 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
1727 static int nf_tables_dump_chains(struct sk_buff *skb,
1728 struct netlink_callback *cb)
1730 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1731 unsigned int idx = 0, s_idx = cb->args[0];
1732 struct net *net = sock_net(skb->sk);
1733 int family = nfmsg->nfgen_family;
1734 struct nftables_pernet *nft_net;
1735 const struct nft_table *table;
1736 const struct nft_chain *chain;
1739 nft_net = nft_pernet(net);
1740 cb->seq = READ_ONCE(nft_net->base_seq);
1742 list_for_each_entry_rcu(table, &nft_net->tables, list) {
1743 if (family != NFPROTO_UNSPEC && family != table->family)
1746 list_for_each_entry_rcu(chain, &table->chains, list) {
1750 memset(&cb->args[1], 0,
1751 sizeof(cb->args) - sizeof(cb->args[0]));
1752 if (!nft_is_active(net, chain))
1754 if (nf_tables_fill_chain_info(skb, net,
1755 NETLINK_CB(cb->skb).portid,
1759 table->family, table,
1763 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1774 /* called with rcu_read_lock held */
1775 static int nf_tables_getchain(struct sk_buff *skb, const struct nfnl_info *info,
1776 const struct nlattr * const nla[])
1778 struct netlink_ext_ack *extack = info->extack;
1779 u8 genmask = nft_genmask_cur(info->net);
1780 u8 family = info->nfmsg->nfgen_family;
1781 const struct nft_chain *chain;
1782 struct net *net = info->net;
1783 struct nft_table *table;
1784 struct sk_buff *skb2;
1787 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
1788 struct netlink_dump_control c = {
1789 .dump = nf_tables_dump_chains,
1790 .module = THIS_MODULE,
1793 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
1796 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, 0);
1797 if (IS_ERR(table)) {
1798 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
1799 return PTR_ERR(table);
1802 chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask);
1803 if (IS_ERR(chain)) {
1804 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
1805 return PTR_ERR(chain);
1808 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1812 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
1813 info->nlh->nlmsg_seq, NFT_MSG_NEWCHAIN,
1814 0, family, table, chain, NULL);
1816 goto err_fill_chain_info;
1818 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
1820 err_fill_chain_info:
1825 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1826 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
1827 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
1830 static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
1832 struct nlattr *tb[NFTA_COUNTER_MAX+1];
1833 struct nft_stats __percpu *newstats;
1834 struct nft_stats *stats;
1837 err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr,
1838 nft_counter_policy, NULL);
1840 return ERR_PTR(err);
1842 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
1843 return ERR_PTR(-EINVAL);
1845 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
1846 if (newstats == NULL)
1847 return ERR_PTR(-ENOMEM);
1849 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1850 * are not exposed to userspace.
1853 stats = this_cpu_ptr(newstats);
1854 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1855 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
1861 static void nft_chain_stats_replace(struct nft_trans *trans)
1863 struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain);
1865 if (!nft_trans_chain_stats(trans))
1868 nft_trans_chain_stats(trans) =
1869 rcu_replace_pointer(chain->stats, nft_trans_chain_stats(trans),
1870 lockdep_commit_lock_is_held(trans->ctx.net));
1872 if (!nft_trans_chain_stats(trans))
1873 static_branch_inc(&nft_counters_enabled);
1876 static void nf_tables_chain_free_chain_rules(struct nft_chain *chain)
1878 struct nft_rule_blob *g0 = rcu_dereference_raw(chain->blob_gen_0);
1879 struct nft_rule_blob *g1 = rcu_dereference_raw(chain->blob_gen_1);
1885 /* should be NULL either via abort or via successful commit */
1886 WARN_ON_ONCE(chain->blob_next);
1887 kvfree(chain->blob_next);
1890 void nf_tables_chain_destroy(struct nft_ctx *ctx)
1892 struct nft_chain *chain = ctx->chain;
1893 struct nft_hook *hook, *next;
1895 if (WARN_ON(chain->use > 0))
1898 /* no concurrent access possible anymore */
1899 nf_tables_chain_free_chain_rules(chain);
1901 if (nft_is_base_chain(chain)) {
1902 struct nft_base_chain *basechain = nft_base_chain(chain);
1904 if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) {
1905 list_for_each_entry_safe(hook, next,
1906 &basechain->hook_list, list) {
1907 list_del_rcu(&hook->list);
1908 kfree_rcu(hook, rcu);
1911 module_put(basechain->type->owner);
1912 if (rcu_access_pointer(basechain->stats)) {
1913 static_branch_dec(&nft_counters_enabled);
1914 free_percpu(rcu_dereference_raw(basechain->stats));
1917 kfree(chain->udata);
1921 kfree(chain->udata);
1926 static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
1927 const struct nlattr *attr)
1929 struct net_device *dev;
1930 char ifname[IFNAMSIZ];
1931 struct nft_hook *hook;
1934 hook = kmalloc(sizeof(struct nft_hook), GFP_KERNEL_ACCOUNT);
1937 goto err_hook_alloc;
1940 nla_strscpy(ifname, attr, IFNAMSIZ);
1941 /* nf_tables_netdev_event() is called under rtnl_mutex, this is
1942 * indirectly serializing all the other holders of the commit_mutex with
1945 dev = __dev_get_by_name(net, ifname);
1950 hook->ops.dev = dev;
1957 return ERR_PTR(err);
1960 static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,
1961 const struct nft_hook *this)
1963 struct nft_hook *hook;
1965 list_for_each_entry(hook, hook_list, list) {
1966 if (this->ops.dev == hook->ops.dev)
1973 static int nf_tables_parse_netdev_hooks(struct net *net,
1974 const struct nlattr *attr,
1975 struct list_head *hook_list,
1976 struct netlink_ext_ack *extack)
1978 struct nft_hook *hook, *next;
1979 const struct nlattr *tmp;
1980 int rem, n = 0, err;
1982 nla_for_each_nested(tmp, attr, rem) {
1983 if (nla_type(tmp) != NFTA_DEVICE_NAME) {
1988 hook = nft_netdev_hook_alloc(net, tmp);
1990 NL_SET_BAD_ATTR(extack, tmp);
1991 err = PTR_ERR(hook);
1994 if (nft_hook_list_find(hook_list, hook)) {
1995 NL_SET_BAD_ATTR(extack, tmp);
2000 list_add_tail(&hook->list, hook_list);
2003 if (n == NFT_NETDEVICE_MAX) {
2012 list_for_each_entry_safe(hook, next, hook_list, list) {
2013 list_del(&hook->list);
2019 struct nft_chain_hook {
2022 const struct nft_chain_type *type;
2023 struct list_head list;
2026 static int nft_chain_parse_netdev(struct net *net,
2027 struct nlattr *tb[],
2028 struct list_head *hook_list,
2029 struct netlink_ext_ack *extack)
2031 struct nft_hook *hook;
2034 if (tb[NFTA_HOOK_DEV]) {
2035 hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]);
2037 NL_SET_BAD_ATTR(extack, tb[NFTA_HOOK_DEV]);
2038 return PTR_ERR(hook);
2041 list_add_tail(&hook->list, hook_list);
2042 } else if (tb[NFTA_HOOK_DEVS]) {
2043 err = nf_tables_parse_netdev_hooks(net, tb[NFTA_HOOK_DEVS],
2048 if (list_empty(hook_list))
2057 static int nft_chain_parse_hook(struct net *net,
2058 struct nft_base_chain *basechain,
2059 const struct nlattr * const nla[],
2060 struct nft_chain_hook *hook, u8 family,
2061 struct netlink_ext_ack *extack)
2063 struct nftables_pernet *nft_net = nft_pernet(net);
2064 struct nlattr *ha[NFTA_HOOK_MAX + 1];
2065 const struct nft_chain_type *type;
2068 lockdep_assert_held(&nft_net->commit_mutex);
2069 lockdep_nfnl_nft_mutex_not_held();
2071 err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX,
2072 nla[NFTA_CHAIN_HOOK],
2073 nft_hook_policy, NULL);
2078 if (!ha[NFTA_HOOK_HOOKNUM] ||
2079 !ha[NFTA_HOOK_PRIORITY])
2082 hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
2083 hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
2085 type = __nft_chain_type_get(family, NFT_CHAIN_T_DEFAULT);
2089 if (nla[NFTA_CHAIN_TYPE]) {
2090 type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE],
2093 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]);
2094 return PTR_ERR(type);
2097 if (hook->num >= NFT_MAX_HOOKS || !(type->hook_mask & (1 << hook->num)))
2100 if (type->type == NFT_CHAIN_T_NAT &&
2101 hook->priority <= NF_IP_PRI_CONNTRACK)
2104 if (ha[NFTA_HOOK_HOOKNUM]) {
2105 hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
2106 if (hook->num != basechain->ops.hooknum)
2109 if (ha[NFTA_HOOK_PRIORITY]) {
2110 hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
2111 if (hook->priority != basechain->ops.priority)
2115 type = basechain->type;
2118 if (!try_module_get(type->owner)) {
2119 if (nla[NFTA_CHAIN_TYPE])
2120 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]);
2126 INIT_LIST_HEAD(&hook->list);
2127 if (nft_base_chain_netdev(family, hook->num)) {
2128 err = nft_chain_parse_netdev(net, ha, &hook->list, extack);
2130 module_put(type->owner);
2133 } else if (ha[NFTA_HOOK_DEV] || ha[NFTA_HOOK_DEVS]) {
2134 module_put(type->owner);
2141 static void nft_chain_release_hook(struct nft_chain_hook *hook)
2143 struct nft_hook *h, *next;
2145 list_for_each_entry_safe(h, next, &hook->list, list) {
2149 module_put(hook->type->owner);
2152 static void nft_last_rule(const struct nft_chain *chain, const void *ptr)
2154 struct nft_rule_dp_last *lrule;
2156 BUILD_BUG_ON(offsetof(struct nft_rule_dp_last, end) != 0);
2158 lrule = (struct nft_rule_dp_last *)ptr;
2159 lrule->end.is_last = 1;
2160 lrule->chain = chain;
2161 /* blob size does not include the trailer rule */
2164 static struct nft_rule_blob *nf_tables_chain_alloc_rules(const struct nft_chain *chain,
2167 struct nft_rule_blob *blob;
2172 size += sizeof(struct nft_rule_blob) + sizeof(struct nft_rule_dp_last);
2174 blob = kvmalloc(size, GFP_KERNEL_ACCOUNT);
2179 nft_last_rule(chain, blob->data);
2184 static void nft_basechain_hook_init(struct nf_hook_ops *ops, u8 family,
2185 const struct nft_chain_hook *hook,
2186 struct nft_chain *chain)
2189 ops->hooknum = hook->num;
2190 ops->priority = hook->priority;
2192 ops->hook = hook->type->hooks[ops->hooknum];
2193 ops->hook_ops_type = NF_HOOK_OP_NF_TABLES;
2196 static int nft_basechain_init(struct nft_base_chain *basechain, u8 family,
2197 struct nft_chain_hook *hook, u32 flags)
2199 struct nft_chain *chain;
2202 basechain->type = hook->type;
2203 INIT_LIST_HEAD(&basechain->hook_list);
2204 chain = &basechain->chain;
2206 if (nft_base_chain_netdev(family, hook->num)) {
2207 list_splice_init(&hook->list, &basechain->hook_list);
2208 list_for_each_entry(h, &basechain->hook_list, list)
2209 nft_basechain_hook_init(&h->ops, family, hook, chain);
2211 nft_basechain_hook_init(&basechain->ops, family, hook, chain);
2213 chain->flags |= NFT_CHAIN_BASE | flags;
2214 basechain->policy = NF_ACCEPT;
2215 if (chain->flags & NFT_CHAIN_HW_OFFLOAD &&
2216 !nft_chain_offload_support(basechain)) {
2217 list_splice_init(&basechain->hook_list, &hook->list);
2221 flow_block_init(&basechain->flow_block);
2226 static int nft_chain_add(struct nft_table *table, struct nft_chain *chain)
2230 err = rhltable_insert_key(&table->chains_ht, chain->name,
2231 &chain->rhlhead, nft_chain_ht_params);
2235 list_add_tail_rcu(&chain->list, &table->chains);
2240 static u64 chain_id;
2242 static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
2243 u8 policy, u32 flags,
2244 struct netlink_ext_ack *extack)
2246 const struct nlattr * const *nla = ctx->nla;
2247 struct nft_table *table = ctx->table;
2248 struct nft_base_chain *basechain;
2249 struct net *net = ctx->net;
2250 char name[NFT_NAME_MAXLEN];
2251 struct nft_rule_blob *blob;
2252 struct nft_trans *trans;
2253 struct nft_chain *chain;
2256 if (table->use == UINT_MAX)
2259 if (nla[NFTA_CHAIN_HOOK]) {
2260 struct nft_stats __percpu *stats = NULL;
2261 struct nft_chain_hook hook = {};
2263 if (flags & NFT_CHAIN_BINDING)
2266 err = nft_chain_parse_hook(net, NULL, nla, &hook, family,
2271 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL_ACCOUNT);
2272 if (basechain == NULL) {
2273 nft_chain_release_hook(&hook);
2276 chain = &basechain->chain;
2278 if (nla[NFTA_CHAIN_COUNTERS]) {
2279 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
2280 if (IS_ERR(stats)) {
2281 nft_chain_release_hook(&hook);
2283 return PTR_ERR(stats);
2285 rcu_assign_pointer(basechain->stats, stats);
2288 err = nft_basechain_init(basechain, family, &hook, flags);
2290 nft_chain_release_hook(&hook);
2296 static_branch_inc(&nft_counters_enabled);
2298 if (flags & NFT_CHAIN_BASE)
2300 if (flags & NFT_CHAIN_HW_OFFLOAD)
2303 chain = kzalloc(sizeof(*chain), GFP_KERNEL_ACCOUNT);
2307 chain->flags = flags;
2311 INIT_LIST_HEAD(&chain->rules);
2312 chain->handle = nf_tables_alloc_handle(table);
2313 chain->table = table;
2315 if (nla[NFTA_CHAIN_NAME]) {
2316 chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT);
2318 if (!(flags & NFT_CHAIN_BINDING)) {
2320 goto err_destroy_chain;
2323 snprintf(name, sizeof(name), "__chain%llu", ++chain_id);
2324 chain->name = kstrdup(name, GFP_KERNEL_ACCOUNT);
2329 goto err_destroy_chain;
2332 if (nla[NFTA_CHAIN_USERDATA]) {
2333 chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL_ACCOUNT);
2334 if (chain->udata == NULL) {
2336 goto err_destroy_chain;
2338 chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]);
2341 blob = nf_tables_chain_alloc_rules(chain, 0);
2344 goto err_destroy_chain;
2347 RCU_INIT_POINTER(chain->blob_gen_0, blob);
2348 RCU_INIT_POINTER(chain->blob_gen_1, blob);
2350 err = nf_tables_register_hook(net, table, chain);
2352 goto err_destroy_chain;
2354 trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN);
2355 if (IS_ERR(trans)) {
2356 err = PTR_ERR(trans);
2357 goto err_unregister_hook;
2360 nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET;
2361 if (nft_is_base_chain(chain))
2362 nft_trans_chain_policy(trans) = policy;
2364 err = nft_chain_add(table, chain);
2366 nft_trans_destroy(trans);
2367 goto err_unregister_hook;
2373 err_unregister_hook:
2374 nf_tables_unregister_hook(net, table, chain);
2376 nf_tables_chain_destroy(ctx);
2381 static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy,
2382 u32 flags, const struct nlattr *attr,
2383 struct netlink_ext_ack *extack)
2385 const struct nlattr * const *nla = ctx->nla;
2386 struct nft_base_chain *basechain = NULL;
2387 struct nft_table *table = ctx->table;
2388 struct nft_chain *chain = ctx->chain;
2389 struct nft_chain_hook hook = {};
2390 struct nft_stats *stats = NULL;
2391 struct nft_hook *h, *next;
2392 struct nf_hook_ops *ops;
2393 struct nft_trans *trans;
2394 bool unregister = false;
2397 if (chain->flags ^ flags)
2400 INIT_LIST_HEAD(&hook.list);
2402 if (nla[NFTA_CHAIN_HOOK]) {
2403 if (!nft_is_base_chain(chain)) {
2404 NL_SET_BAD_ATTR(extack, attr);
2408 basechain = nft_base_chain(chain);
2409 err = nft_chain_parse_hook(ctx->net, basechain, nla, &hook,
2410 ctx->family, extack);
2414 if (basechain->type != hook.type) {
2415 nft_chain_release_hook(&hook);
2416 NL_SET_BAD_ATTR(extack, attr);
2420 if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) {
2421 list_for_each_entry_safe(h, next, &hook.list, list) {
2422 h->ops.pf = basechain->ops.pf;
2423 h->ops.hooknum = basechain->ops.hooknum;
2424 h->ops.priority = basechain->ops.priority;
2425 h->ops.priv = basechain->ops.priv;
2426 h->ops.hook = basechain->ops.hook;
2428 if (nft_hook_list_find(&basechain->hook_list, h)) {
2434 ops = &basechain->ops;
2435 if (ops->hooknum != hook.num ||
2436 ops->priority != hook.priority) {
2437 nft_chain_release_hook(&hook);
2438 NL_SET_BAD_ATTR(extack, attr);
2444 if (nla[NFTA_CHAIN_HANDLE] &&
2445 nla[NFTA_CHAIN_NAME]) {
2446 struct nft_chain *chain2;
2448 chain2 = nft_chain_lookup(ctx->net, table,
2449 nla[NFTA_CHAIN_NAME], genmask);
2450 if (!IS_ERR(chain2)) {
2451 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
2457 if (nla[NFTA_CHAIN_COUNTERS]) {
2458 if (!nft_is_base_chain(chain)) {
2463 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
2464 if (IS_ERR(stats)) {
2465 err = PTR_ERR(stats);
2470 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
2471 nft_is_base_chain(chain) &&
2472 !list_empty(&hook.list)) {
2473 basechain = nft_base_chain(chain);
2474 ops = &basechain->ops;
2476 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) {
2477 err = nft_netdev_register_hooks(ctx->net, &hook.list);
2485 trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN,
2486 sizeof(struct nft_trans_chain));
2490 nft_trans_chain_stats(trans) = stats;
2491 nft_trans_chain_update(trans) = true;
2493 if (nla[NFTA_CHAIN_POLICY])
2494 nft_trans_chain_policy(trans) = policy;
2496 nft_trans_chain_policy(trans) = -1;
2498 if (nla[NFTA_CHAIN_HANDLE] &&
2499 nla[NFTA_CHAIN_NAME]) {
2500 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
2501 struct nft_trans *tmp;
2505 name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT);
2510 list_for_each_entry(tmp, &nft_net->commit_list, list) {
2511 if (tmp->msg_type == NFT_MSG_NEWCHAIN &&
2512 tmp->ctx.table == table &&
2513 nft_trans_chain_update(tmp) &&
2514 nft_trans_chain_name(tmp) &&
2515 strcmp(name, nft_trans_chain_name(tmp)) == 0) {
2516 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
2522 nft_trans_chain_name(trans) = name;
2525 nft_trans_basechain(trans) = basechain;
2526 INIT_LIST_HEAD(&nft_trans_chain_hooks(trans));
2527 list_splice(&hook.list, &nft_trans_chain_hooks(trans));
2529 nft_trans_commit_list_add_tail(ctx->net, trans);
2537 if (nla[NFTA_CHAIN_HOOK]) {
2538 list_for_each_entry_safe(h, next, &hook.list, list) {
2540 nf_unregister_net_hook(ctx->net, &h->ops);
2544 module_put(hook.type->owner);
2550 static struct nft_chain *nft_chain_lookup_byid(const struct net *net,
2551 const struct nft_table *table,
2552 const struct nlattr *nla)
2554 struct nftables_pernet *nft_net = nft_pernet(net);
2555 u32 id = ntohl(nla_get_be32(nla));
2556 struct nft_trans *trans;
2558 list_for_each_entry(trans, &nft_net->commit_list, list) {
2559 struct nft_chain *chain = trans->ctx.chain;
2561 if (trans->msg_type == NFT_MSG_NEWCHAIN &&
2562 chain->table == table &&
2563 id == nft_trans_chain_id(trans))
2566 return ERR_PTR(-ENOENT);
2569 static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info,
2570 const struct nlattr * const nla[])
2572 struct nftables_pernet *nft_net = nft_pernet(info->net);
2573 struct netlink_ext_ack *extack = info->extack;
2574 u8 genmask = nft_genmask_next(info->net);
2575 u8 family = info->nfmsg->nfgen_family;
2576 struct nft_chain *chain = NULL;
2577 struct net *net = info->net;
2578 const struct nlattr *attr;
2579 struct nft_table *table;
2580 u8 policy = NF_ACCEPT;
2585 lockdep_assert_held(&nft_net->commit_mutex);
2587 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask,
2588 NETLINK_CB(skb).portid);
2589 if (IS_ERR(table)) {
2590 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
2591 return PTR_ERR(table);
2595 attr = nla[NFTA_CHAIN_NAME];
2597 if (nla[NFTA_CHAIN_HANDLE]) {
2598 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
2599 chain = nft_chain_lookup_byhandle(table, handle, genmask);
2600 if (IS_ERR(chain)) {
2601 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]);
2602 return PTR_ERR(chain);
2604 attr = nla[NFTA_CHAIN_HANDLE];
2605 } else if (nla[NFTA_CHAIN_NAME]) {
2606 chain = nft_chain_lookup(net, table, attr, genmask);
2607 if (IS_ERR(chain)) {
2608 if (PTR_ERR(chain) != -ENOENT) {
2609 NL_SET_BAD_ATTR(extack, attr);
2610 return PTR_ERR(chain);
2614 } else if (!nla[NFTA_CHAIN_ID]) {
2618 if (nla[NFTA_CHAIN_POLICY]) {
2619 if (chain != NULL &&
2620 !nft_is_base_chain(chain)) {
2621 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
2625 if (chain == NULL &&
2626 nla[NFTA_CHAIN_HOOK] == NULL) {
2627 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
2631 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
2641 if (nla[NFTA_CHAIN_FLAGS])
2642 flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS]));
2644 flags = chain->flags;
2646 if (flags & ~NFT_CHAIN_FLAGS)
2649 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
2651 if (chain != NULL) {
2652 if (chain->flags & NFT_CHAIN_BINDING)
2655 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
2656 NL_SET_BAD_ATTR(extack, attr);
2659 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
2662 flags |= chain->flags & NFT_CHAIN_BASE;
2663 return nf_tables_updchain(&ctx, genmask, policy, flags, attr,
2667 return nf_tables_addchain(&ctx, family, genmask, policy, flags, extack);
2670 static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info,
2671 const struct nlattr * const nla[])
2673 struct netlink_ext_ack *extack = info->extack;
2674 u8 genmask = nft_genmask_next(info->net);
2675 u8 family = info->nfmsg->nfgen_family;
2676 struct net *net = info->net;
2677 const struct nlattr *attr;
2678 struct nft_table *table;
2679 struct nft_chain *chain;
2680 struct nft_rule *rule;
2686 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask,
2687 NETLINK_CB(skb).portid);
2688 if (IS_ERR(table)) {
2689 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
2690 return PTR_ERR(table);
2693 if (nla[NFTA_CHAIN_HANDLE]) {
2694 attr = nla[NFTA_CHAIN_HANDLE];
2695 handle = be64_to_cpu(nla_get_be64(attr));
2696 chain = nft_chain_lookup_byhandle(table, handle, genmask);
2698 attr = nla[NFTA_CHAIN_NAME];
2699 chain = nft_chain_lookup(net, table, attr, genmask);
2701 if (IS_ERR(chain)) {
2702 if (PTR_ERR(chain) == -ENOENT &&
2703 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYCHAIN)
2706 NL_SET_BAD_ATTR(extack, attr);
2707 return PTR_ERR(chain);
2710 if (info->nlh->nlmsg_flags & NLM_F_NONREC &&
2714 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
2717 list_for_each_entry(rule, &chain->rules, list) {
2718 if (!nft_is_active_next(net, rule))
2722 err = nft_delrule(&ctx, rule);
2727 /* There are rules and elements that are still holding references to us,
2728 * we cannot do a recursive removal in this case.
2731 NL_SET_BAD_ATTR(extack, attr);
2735 return nft_delchain(&ctx);
2743 * nft_register_expr - register nf_tables expr type
2746 * Registers the expr type for use with nf_tables. Returns zero on
2747 * success or a negative errno code otherwise.
2749 int nft_register_expr(struct nft_expr_type *type)
2751 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2752 if (type->family == NFPROTO_UNSPEC)
2753 list_add_tail_rcu(&type->list, &nf_tables_expressions);
2755 list_add_rcu(&type->list, &nf_tables_expressions);
2756 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2759 EXPORT_SYMBOL_GPL(nft_register_expr);
2762 * nft_unregister_expr - unregister nf_tables expr type
2765 * Unregisters the expr typefor use with nf_tables.
2767 void nft_unregister_expr(struct nft_expr_type *type)
2769 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2770 list_del_rcu(&type->list);
2771 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2773 EXPORT_SYMBOL_GPL(nft_unregister_expr);
2775 static const struct nft_expr_type *__nft_expr_type_get(u8 family,
2778 const struct nft_expr_type *type, *candidate = NULL;
2780 list_for_each_entry(type, &nf_tables_expressions, list) {
2781 if (!nla_strcmp(nla, type->name)) {
2782 if (!type->family && !candidate)
2784 else if (type->family == family)
2791 #ifdef CONFIG_MODULES
2792 static int nft_expr_type_request_module(struct net *net, u8 family,
2795 if (nft_request_module(net, "nft-expr-%u-%.*s", family,
2796 nla_len(nla), (char *)nla_data(nla)) == -EAGAIN)
2803 static const struct nft_expr_type *nft_expr_type_get(struct net *net,
2807 const struct nft_expr_type *type;
2810 return ERR_PTR(-EINVAL);
2812 type = __nft_expr_type_get(family, nla);
2813 if (type != NULL && try_module_get(type->owner))
2816 lockdep_nfnl_nft_mutex_not_held();
2817 #ifdef CONFIG_MODULES
2819 if (nft_expr_type_request_module(net, family, nla) == -EAGAIN)
2820 return ERR_PTR(-EAGAIN);
2822 if (nft_request_module(net, "nft-expr-%.*s",
2824 (char *)nla_data(nla)) == -EAGAIN)
2825 return ERR_PTR(-EAGAIN);
2828 return ERR_PTR(-ENOENT);
2831 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
2832 [NFTA_EXPR_NAME] = { .type = NLA_STRING,
2833 .len = NFT_MODULE_AUTOLOAD_LIMIT },
2834 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
2837 static int nf_tables_fill_expr_info(struct sk_buff *skb,
2838 const struct nft_expr *expr, bool reset)
2840 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
2841 goto nla_put_failure;
2843 if (expr->ops->dump) {
2844 struct nlattr *data = nla_nest_start_noflag(skb,
2847 goto nla_put_failure;
2848 if (expr->ops->dump(skb, expr, reset) < 0)
2849 goto nla_put_failure;
2850 nla_nest_end(skb, data);
2859 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
2860 const struct nft_expr *expr, bool reset)
2862 struct nlattr *nest;
2864 nest = nla_nest_start_noflag(skb, attr);
2866 goto nla_put_failure;
2867 if (nf_tables_fill_expr_info(skb, expr, reset) < 0)
2868 goto nla_put_failure;
2869 nla_nest_end(skb, nest);
2876 struct nft_expr_info {
2877 const struct nft_expr_ops *ops;
2878 const struct nlattr *attr;
2879 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
2882 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
2883 const struct nlattr *nla,
2884 struct nft_expr_info *info)
2886 const struct nft_expr_type *type;
2887 const struct nft_expr_ops *ops;
2888 struct nlattr *tb[NFTA_EXPR_MAX + 1];
2891 err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla,
2892 nft_expr_policy, NULL);
2896 type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]);
2898 return PTR_ERR(type);
2900 if (tb[NFTA_EXPR_DATA]) {
2901 err = nla_parse_nested_deprecated(info->tb, type->maxattr,
2903 type->policy, NULL);
2907 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
2909 if (type->select_ops != NULL) {
2910 ops = type->select_ops(ctx,
2911 (const struct nlattr * const *)info->tb);
2914 #ifdef CONFIG_MODULES
2916 if (nft_expr_type_request_module(ctx->net,
2918 tb[NFTA_EXPR_NAME]) != -EAGAIN)
2932 module_put(type->owner);
2936 int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla,
2937 struct nft_expr_info *info)
2939 struct nlattr *tb[NFTA_EXPR_MAX + 1];
2940 const struct nft_expr_type *type;
2943 err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla,
2944 nft_expr_policy, NULL);
2948 if (!tb[NFTA_EXPR_DATA])
2951 type = __nft_expr_type_get(ctx->family, tb[NFTA_EXPR_NAME]);
2955 if (!type->inner_ops)
2958 err = nla_parse_nested_deprecated(info->tb, type->maxattr,
2960 type->policy, NULL);
2965 info->ops = type->inner_ops;
2973 static int nf_tables_newexpr(const struct nft_ctx *ctx,
2974 const struct nft_expr_info *expr_info,
2975 struct nft_expr *expr)
2977 const struct nft_expr_ops *ops = expr_info->ops;
2982 err = ops->init(ctx, expr, (const struct nlattr **)expr_info->tb);
2993 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
2994 struct nft_expr *expr)
2996 const struct nft_expr_type *type = expr->ops->type;
2998 if (expr->ops->destroy)
2999 expr->ops->destroy(ctx, expr);
3000 module_put(type->owner);
3003 static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
3004 const struct nlattr *nla)
3006 struct nft_expr_info expr_info;
3007 struct nft_expr *expr;
3008 struct module *owner;
3011 err = nf_tables_expr_parse(ctx, nla, &expr_info);
3013 goto err_expr_parse;
3016 if (!(expr_info.ops->type->flags & NFT_EXPR_STATEFUL))
3017 goto err_expr_stateful;
3020 expr = kzalloc(expr_info.ops->size, GFP_KERNEL_ACCOUNT);
3022 goto err_expr_stateful;
3024 err = nf_tables_newexpr(ctx, &expr_info, expr);
3032 owner = expr_info.ops->type->owner;
3033 if (expr_info.ops->type->release_ops)
3034 expr_info.ops->type->release_ops(expr_info.ops);
3038 return ERR_PTR(err);
3041 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
3045 if (src->ops->clone) {
3046 dst->ops = src->ops;
3047 err = src->ops->clone(dst, src);
3051 memcpy(dst, src, src->ops->size);
3054 __module_get(src->ops->type->owner);
3059 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
3061 nf_tables_expr_destroy(ctx, expr);
3069 static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain,
3072 struct nft_rule *rule;
3074 // FIXME: this sucks
3075 list_for_each_entry_rcu(rule, &chain->rules, list) {
3076 if (handle == rule->handle)
3080 return ERR_PTR(-ENOENT);
3083 static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain,
3084 const struct nlattr *nla)
3087 return ERR_PTR(-EINVAL);
3089 return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
3092 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
3093 [NFTA_RULE_TABLE] = { .type = NLA_STRING,
3094 .len = NFT_TABLE_MAXNAMELEN - 1 },
3095 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
3096 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3097 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
3098 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
3099 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
3100 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
3101 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
3102 .len = NFT_USERDATA_MAXLEN },
3103 [NFTA_RULE_ID] = { .type = NLA_U32 },
3104 [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 },
3105 [NFTA_RULE_CHAIN_ID] = { .type = NLA_U32 },
3108 static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
3109 u32 portid, u32 seq, int event,
3110 u32 flags, int family,
3111 const struct nft_table *table,
3112 const struct nft_chain *chain,
3113 const struct nft_rule *rule, u64 handle,
3116 struct nlmsghdr *nlh;
3117 const struct nft_expr *expr, *next;
3118 struct nlattr *list;
3119 u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
3121 nlh = nfnl_msg_put(skb, portid, seq, type, flags, family, NFNETLINK_V0,
3124 goto nla_put_failure;
3126 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
3127 goto nla_put_failure;
3128 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
3129 goto nla_put_failure;
3130 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle),
3132 goto nla_put_failure;
3134 if (event != NFT_MSG_DELRULE && handle) {
3135 if (nla_put_be64(skb, NFTA_RULE_POSITION, cpu_to_be64(handle),
3137 goto nla_put_failure;
3140 if (chain->flags & NFT_CHAIN_HW_OFFLOAD)
3141 nft_flow_rule_stats(chain, rule);
3143 list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS);
3145 goto nla_put_failure;
3146 nft_rule_for_each_expr(expr, next, rule) {
3147 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, reset) < 0)
3148 goto nla_put_failure;
3150 nla_nest_end(skb, list);
3153 struct nft_userdata *udata = nft_userdata(rule);
3154 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
3156 goto nla_put_failure;
3159 nlmsg_end(skb, nlh);
3163 nlmsg_trim(skb, nlh);
3167 static void nf_tables_rule_notify(const struct nft_ctx *ctx,
3168 const struct nft_rule *rule, int event)
3170 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
3171 const struct nft_rule *prule;
3172 struct sk_buff *skb;
3178 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
3181 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3185 if (event == NFT_MSG_NEWRULE &&
3186 !list_is_first(&rule->list, &ctx->chain->rules) &&
3187 !list_is_last(&rule->list, &ctx->chain->rules)) {
3188 prule = list_prev_entry(rule, list);
3189 handle = prule->handle;
3191 if (ctx->flags & (NLM_F_APPEND | NLM_F_REPLACE))
3192 flags |= NLM_F_APPEND;
3193 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
3194 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
3196 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
3197 event, flags, ctx->family, ctx->table,
3198 ctx->chain, rule, handle, false);
3204 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
3207 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
3210 struct nft_rule_dump_ctx {
3215 static int __nf_tables_dump_rules(struct sk_buff *skb,
3217 struct netlink_callback *cb,
3218 const struct nft_table *table,
3219 const struct nft_chain *chain,
3222 struct net *net = sock_net(skb->sk);
3223 const struct nft_rule *rule, *prule;
3224 unsigned int s_idx = cb->args[0];
3228 list_for_each_entry_rcu(rule, &chain->rules, list) {
3229 if (!nft_is_active(net, rule))
3234 memset(&cb->args[1], 0,
3235 sizeof(cb->args) - sizeof(cb->args[0]));
3238 handle = prule->handle;
3242 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
3245 NLM_F_MULTI | NLM_F_APPEND,
3247 table, chain, rule, handle, reset) < 0)
3250 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3259 static int nf_tables_dump_rules(struct sk_buff *skb,
3260 struct netlink_callback *cb)
3262 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
3263 const struct nft_rule_dump_ctx *ctx = cb->data;
3264 struct nft_table *table;
3265 const struct nft_chain *chain;
3266 unsigned int idx = 0;
3267 struct net *net = sock_net(skb->sk);
3268 int family = nfmsg->nfgen_family;
3269 struct nftables_pernet *nft_net;
3272 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET)
3276 nft_net = nft_pernet(net);
3277 cb->seq = READ_ONCE(nft_net->base_seq);
3279 list_for_each_entry_rcu(table, &nft_net->tables, list) {
3280 if (family != NFPROTO_UNSPEC && family != table->family)
3283 if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0)
3286 if (ctx && ctx->table && ctx->chain) {
3287 struct rhlist_head *list, *tmp;
3289 list = rhltable_lookup(&table->chains_ht, ctx->chain,
3290 nft_chain_ht_params);
3294 rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
3295 if (!nft_is_active(net, chain))
3297 __nf_tables_dump_rules(skb, &idx,
3298 cb, table, chain, reset);
3304 list_for_each_entry_rcu(chain, &table->chains, list) {
3305 if (__nf_tables_dump_rules(skb, &idx,
3306 cb, table, chain, reset))
3310 if (ctx && ctx->table)
3320 static int nf_tables_dump_rules_start(struct netlink_callback *cb)
3322 const struct nlattr * const *nla = cb->data;
3323 struct nft_rule_dump_ctx *ctx = NULL;
3325 if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) {
3326 ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
3330 if (nla[NFTA_RULE_TABLE]) {
3331 ctx->table = nla_strdup(nla[NFTA_RULE_TABLE],
3338 if (nla[NFTA_RULE_CHAIN]) {
3339 ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN],
3353 static int nf_tables_dump_rules_done(struct netlink_callback *cb)
3355 struct nft_rule_dump_ctx *ctx = cb->data;
3365 /* called with rcu_read_lock held */
3366 static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info,
3367 const struct nlattr * const nla[])
3369 struct netlink_ext_ack *extack = info->extack;
3370 u8 genmask = nft_genmask_cur(info->net);
3371 u8 family = info->nfmsg->nfgen_family;
3372 const struct nft_chain *chain;
3373 const struct nft_rule *rule;
3374 struct net *net = info->net;
3375 struct nft_table *table;
3376 struct sk_buff *skb2;
3380 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
3381 struct netlink_dump_control c = {
3382 .start= nf_tables_dump_rules_start,
3383 .dump = nf_tables_dump_rules,
3384 .done = nf_tables_dump_rules_done,
3385 .module = THIS_MODULE,
3386 .data = (void *)nla,
3389 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
3392 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, 0);
3393 if (IS_ERR(table)) {
3394 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
3395 return PTR_ERR(table);
3398 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask);
3399 if (IS_ERR(chain)) {
3400 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
3401 return PTR_ERR(chain);
3404 rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
3406 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
3407 return PTR_ERR(rule);
3410 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
3414 if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET)
3417 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
3418 info->nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
3419 family, table, chain, rule, 0, reset);
3421 goto err_fill_rule_info;
3423 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
3430 static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
3431 struct nft_rule *rule)
3433 struct nft_expr *expr, *next;
3436 * Careful: some expressions might not be initialized in case this
3437 * is called on error from nf_tables_newrule().
3439 expr = nft_expr_first(rule);
3440 while (nft_expr_more(rule, expr)) {
3441 next = nft_expr_next(expr);
3442 nf_tables_expr_destroy(ctx, expr);
3448 void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule)
3450 nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE);
3451 nf_tables_rule_destroy(ctx, rule);
3454 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain)
3456 struct nft_expr *expr, *last;
3457 const struct nft_data *data;
3458 struct nft_rule *rule;
3461 if (ctx->level == NFT_JUMP_STACK_SIZE)
3464 list_for_each_entry(rule, &chain->rules, list) {
3465 if (!nft_is_active_next(ctx->net, rule))
3468 nft_rule_for_each_expr(expr, last, rule) {
3469 if (!expr->ops->validate)
3472 err = expr->ops->validate(ctx, expr, &data);
3482 EXPORT_SYMBOL_GPL(nft_chain_validate);
3484 static int nft_table_validate(struct net *net, const struct nft_table *table)
3486 struct nft_chain *chain;
3487 struct nft_ctx ctx = {
3489 .family = table->family,
3493 list_for_each_entry(chain, &table->chains, list) {
3494 if (!nft_is_base_chain(chain))
3498 err = nft_chain_validate(&ctx, chain);
3506 int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,
3507 const struct nft_set_iter *iter,
3508 struct nft_set_elem *elem)
3510 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
3511 struct nft_ctx *pctx = (struct nft_ctx *)ctx;
3512 const struct nft_data *data;
3515 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
3516 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
3519 data = nft_set_ext_data(ext);
3520 switch (data->verdict.code) {
3524 err = nft_chain_validate(ctx, data->verdict.chain);
3536 struct nft_set_elem_catchall {
3537 struct list_head list;
3538 struct rcu_head rcu;
3542 int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set)
3544 u8 genmask = nft_genmask_next(ctx->net);
3545 struct nft_set_elem_catchall *catchall;
3546 struct nft_set_elem elem;
3547 struct nft_set_ext *ext;
3550 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
3551 ext = nft_set_elem_ext(set, catchall->elem);
3552 if (!nft_set_elem_active(ext, genmask))
3555 elem.priv = catchall->elem;
3556 ret = nft_setelem_validate(ctx, set, NULL, &elem);
3564 static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
3565 const struct nft_chain *chain,
3566 const struct nlattr *nla);
3568 #define NFT_RULE_MAXEXPRS 128
3570 static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info,
3571 const struct nlattr * const nla[])
3573 struct nftables_pernet *nft_net = nft_pernet(info->net);
3574 struct netlink_ext_ack *extack = info->extack;
3575 unsigned int size, i, n, ulen = 0, usize = 0;
3576 u8 genmask = nft_genmask_next(info->net);
3577 struct nft_rule *rule, *old_rule = NULL;
3578 struct nft_expr_info *expr_info = NULL;
3579 u8 family = info->nfmsg->nfgen_family;
3580 struct nft_flow_rule *flow = NULL;
3581 struct net *net = info->net;
3582 struct nft_userdata *udata;
3583 struct nft_table *table;
3584 struct nft_chain *chain;
3585 struct nft_trans *trans;
3586 u64 handle, pos_handle;
3587 struct nft_expr *expr;
3592 lockdep_assert_held(&nft_net->commit_mutex);
3594 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask,
3595 NETLINK_CB(skb).portid);
3596 if (IS_ERR(table)) {
3597 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
3598 return PTR_ERR(table);
3601 if (nla[NFTA_RULE_CHAIN]) {
3602 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
3604 if (IS_ERR(chain)) {
3605 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
3606 return PTR_ERR(chain);
3608 if (nft_chain_is_bound(chain))
3611 } else if (nla[NFTA_RULE_CHAIN_ID]) {
3612 chain = nft_chain_lookup_byid(net, table, nla[NFTA_RULE_CHAIN_ID]);
3613 if (IS_ERR(chain)) {
3614 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN_ID]);
3615 return PTR_ERR(chain);
3621 if (nla[NFTA_RULE_HANDLE]) {
3622 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
3623 rule = __nft_rule_lookup(chain, handle);
3625 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
3626 return PTR_ERR(rule);
3629 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
3630 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
3633 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
3638 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE) ||
3639 info->nlh->nlmsg_flags & NLM_F_REPLACE)
3641 handle = nf_tables_alloc_handle(table);
3643 if (chain->use == UINT_MAX)
3646 if (nla[NFTA_RULE_POSITION]) {
3647 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
3648 old_rule = __nft_rule_lookup(chain, pos_handle);
3649 if (IS_ERR(old_rule)) {
3650 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]);
3651 return PTR_ERR(old_rule);
3653 } else if (nla[NFTA_RULE_POSITION_ID]) {
3654 old_rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_POSITION_ID]);
3655 if (IS_ERR(old_rule)) {
3656 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]);
3657 return PTR_ERR(old_rule);
3662 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
3666 if (nla[NFTA_RULE_EXPRESSIONS]) {
3667 expr_info = kvmalloc_array(NFT_RULE_MAXEXPRS,
3668 sizeof(struct nft_expr_info),
3673 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
3675 if (nla_type(tmp) != NFTA_LIST_ELEM)
3676 goto err_release_expr;
3677 if (n == NFT_RULE_MAXEXPRS)
3678 goto err_release_expr;
3679 err = nf_tables_expr_parse(&ctx, tmp, &expr_info[n]);
3681 NL_SET_BAD_ATTR(extack, tmp);
3682 goto err_release_expr;
3684 size += expr_info[n].ops->size;
3688 /* Check for overflow of dlen field */
3690 if (size >= 1 << 12)
3691 goto err_release_expr;
3693 if (nla[NFTA_RULE_USERDATA]) {
3694 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
3696 usize = sizeof(struct nft_userdata) + ulen;
3700 rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL_ACCOUNT);
3702 goto err_release_expr;
3704 nft_activate_next(net, rule);
3706 rule->handle = handle;
3708 rule->udata = ulen ? 1 : 0;
3711 udata = nft_userdata(rule);
3712 udata->len = ulen - 1;
3713 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
3716 expr = nft_expr_first(rule);
3717 for (i = 0; i < n; i++) {
3718 err = nf_tables_newexpr(&ctx, &expr_info[i], expr);
3720 NL_SET_BAD_ATTR(extack, expr_info[i].attr);
3721 goto err_release_rule;
3724 if (expr_info[i].ops->validate)
3725 nft_validate_state_update(table, NFT_VALIDATE_NEED);
3727 expr_info[i].ops = NULL;
3728 expr = nft_expr_next(expr);
3731 if (chain->flags & NFT_CHAIN_HW_OFFLOAD) {
3732 flow = nft_flow_rule_create(net, rule);
3734 err = PTR_ERR(flow);
3735 goto err_release_rule;
3739 if (info->nlh->nlmsg_flags & NLM_F_REPLACE) {
3740 err = nft_delrule(&ctx, old_rule);
3742 goto err_destroy_flow_rule;
3744 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
3745 if (trans == NULL) {
3747 goto err_destroy_flow_rule;
3749 list_add_tail_rcu(&rule->list, &old_rule->list);
3751 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
3754 goto err_destroy_flow_rule;
3757 if (info->nlh->nlmsg_flags & NLM_F_APPEND) {
3759 list_add_rcu(&rule->list, &old_rule->list);
3761 list_add_tail_rcu(&rule->list, &chain->rules);
3764 list_add_tail_rcu(&rule->list, &old_rule->list);
3766 list_add_rcu(&rule->list, &chain->rules);
3773 nft_trans_flow_rule(trans) = flow;
3775 if (table->validate_state == NFT_VALIDATE_DO)
3776 return nft_table_validate(net, table);
3780 err_destroy_flow_rule:
3782 nft_flow_rule_destroy(flow);
3784 nf_tables_rule_release(&ctx, rule);
3786 for (i = 0; i < n; i++) {
3787 if (expr_info[i].ops) {
3788 module_put(expr_info[i].ops->type->owner);
3789 if (expr_info[i].ops->type->release_ops)
3790 expr_info[i].ops->type->release_ops(expr_info[i].ops);
3798 static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
3799 const struct nft_chain *chain,
3800 const struct nlattr *nla)
3802 struct nftables_pernet *nft_net = nft_pernet(net);
3803 u32 id = ntohl(nla_get_be32(nla));
3804 struct nft_trans *trans;
3806 list_for_each_entry(trans, &nft_net->commit_list, list) {
3807 struct nft_rule *rule = nft_trans_rule(trans);
3809 if (trans->msg_type == NFT_MSG_NEWRULE &&
3810 trans->ctx.chain == chain &&
3811 id == nft_trans_rule_id(trans))
3814 return ERR_PTR(-ENOENT);
3817 static int nf_tables_delrule(struct sk_buff *skb, const struct nfnl_info *info,
3818 const struct nlattr * const nla[])
3820 struct netlink_ext_ack *extack = info->extack;
3821 u8 genmask = nft_genmask_next(info->net);
3822 u8 family = info->nfmsg->nfgen_family;
3823 struct nft_chain *chain = NULL;
3824 struct net *net = info->net;
3825 struct nft_table *table;
3826 struct nft_rule *rule;
3830 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask,
3831 NETLINK_CB(skb).portid);
3832 if (IS_ERR(table)) {
3833 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
3834 return PTR_ERR(table);
3837 if (nla[NFTA_RULE_CHAIN]) {
3838 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
3840 if (IS_ERR(chain)) {
3841 if (PTR_ERR(chain) == -ENOENT &&
3842 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE)
3845 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
3846 return PTR_ERR(chain);
3848 if (nft_chain_is_bound(chain))
3852 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
3855 if (nla[NFTA_RULE_HANDLE]) {
3856 rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
3858 if (PTR_ERR(rule) == -ENOENT &&
3859 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE)
3862 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
3863 return PTR_ERR(rule);
3866 err = nft_delrule(&ctx, rule);
3867 } else if (nla[NFTA_RULE_ID]) {
3868 rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_ID]);
3870 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]);
3871 return PTR_ERR(rule);
3874 err = nft_delrule(&ctx, rule);
3876 err = nft_delrule_by_chain(&ctx);
3879 list_for_each_entry(chain, &table->chains, list) {
3880 if (!nft_is_active_next(net, chain))
3884 err = nft_delrule_by_chain(&ctx);
3896 static const struct nft_set_type *nft_set_types[] = {
3897 &nft_set_hash_fast_type,
3899 &nft_set_rhash_type,
3900 &nft_set_bitmap_type,
3901 &nft_set_rbtree_type,
3902 #if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
3903 &nft_set_pipapo_avx2_type,
3905 &nft_set_pipapo_type,
3908 #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \
3909 NFT_SET_TIMEOUT | NFT_SET_OBJECT | \
3912 static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags)
3914 return (flags & type->features) == (flags & NFT_SET_FEATURES);
3918 * Select a set implementation based on the data characteristics and the
3919 * given policy. The total memory use might not be known if no size is
3920 * given, in that case the amount of memory per element is used.
3922 static const struct nft_set_ops *
3923 nft_select_set_ops(const struct nft_ctx *ctx,
3924 const struct nlattr * const nla[],
3925 const struct nft_set_desc *desc)
3927 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
3928 const struct nft_set_ops *ops, *bops;
3929 struct nft_set_estimate est, best;
3930 const struct nft_set_type *type;
3934 lockdep_assert_held(&nft_net->commit_mutex);
3935 lockdep_nfnl_nft_mutex_not_held();
3937 if (nla[NFTA_SET_FLAGS] != NULL)
3938 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
3945 for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) {
3946 type = nft_set_types[i];
3949 if (!nft_set_ops_candidate(type, flags))
3951 if (!ops->estimate(desc, flags, &est))
3954 switch (desc->policy) {
3955 case NFT_SET_POL_PERFORMANCE:
3956 if (est.lookup < best.lookup)
3958 if (est.lookup == best.lookup &&
3959 est.space < best.space)
3962 case NFT_SET_POL_MEMORY:
3964 if (est.space < best.space)
3966 if (est.space == best.space &&
3967 est.lookup < best.lookup)
3969 } else if (est.size < best.size || !bops) {
3984 return ERR_PTR(-EOPNOTSUPP);
3987 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
3988 [NFTA_SET_TABLE] = { .type = NLA_STRING,
3989 .len = NFT_TABLE_MAXNAMELEN - 1 },
3990 [NFTA_SET_NAME] = { .type = NLA_STRING,
3991 .len = NFT_SET_MAXNAMELEN - 1 },
3992 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
3993 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
3994 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
3995 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
3996 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
3997 [NFTA_SET_POLICY] = { .type = NLA_U32 },
3998 [NFTA_SET_DESC] = { .type = NLA_NESTED },
3999 [NFTA_SET_ID] = { .type = NLA_U32 },
4000 [NFTA_SET_TIMEOUT] = { .type = NLA_U64 },
4001 [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 },
4002 [NFTA_SET_USERDATA] = { .type = NLA_BINARY,
4003 .len = NFT_USERDATA_MAXLEN },
4004 [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 },
4005 [NFTA_SET_HANDLE] = { .type = NLA_U64 },
4006 [NFTA_SET_EXPR] = { .type = NLA_NESTED },
4007 [NFTA_SET_EXPRESSIONS] = { .type = NLA_NESTED },
4010 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
4011 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
4012 [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED },
4015 static struct nft_set *nft_set_lookup(const struct nft_table *table,
4016 const struct nlattr *nla, u8 genmask)
4018 struct nft_set *set;
4021 return ERR_PTR(-EINVAL);
4023 list_for_each_entry_rcu(set, &table->sets, list) {
4024 if (!nla_strcmp(nla, set->name) &&
4025 nft_active_genmask(set, genmask))
4028 return ERR_PTR(-ENOENT);
4031 static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table,
4032 const struct nlattr *nla,
4035 struct nft_set *set;
4037 list_for_each_entry(set, &table->sets, list) {
4038 if (be64_to_cpu(nla_get_be64(nla)) == set->handle &&
4039 nft_active_genmask(set, genmask))
4042 return ERR_PTR(-ENOENT);
4045 static struct nft_set *nft_set_lookup_byid(const struct net *net,
4046 const struct nft_table *table,
4047 const struct nlattr *nla, u8 genmask)
4049 struct nftables_pernet *nft_net = nft_pernet(net);
4050 u32 id = ntohl(nla_get_be32(nla));
4051 struct nft_trans *trans;
4053 list_for_each_entry(trans, &nft_net->commit_list, list) {
4054 if (trans->msg_type == NFT_MSG_NEWSET) {
4055 struct nft_set *set = nft_trans_set(trans);
4057 if (id == nft_trans_set_id(trans) &&
4058 set->table == table &&
4059 nft_active_genmask(set, genmask))
4063 return ERR_PTR(-ENOENT);
4066 struct nft_set *nft_set_lookup_global(const struct net *net,
4067 const struct nft_table *table,
4068 const struct nlattr *nla_set_name,
4069 const struct nlattr *nla_set_id,
4072 struct nft_set *set;
4074 set = nft_set_lookup(table, nla_set_name, genmask);
4079 set = nft_set_lookup_byid(net, table, nla_set_id, genmask);
4083 EXPORT_SYMBOL_GPL(nft_set_lookup_global);
4085 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
4088 const struct nft_set *i;
4090 unsigned long *inuse;
4091 unsigned int n = 0, min = 0;
4093 p = strchr(name, '%');
4095 if (p[1] != 'd' || strchr(p + 2, '%'))
4098 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
4102 list_for_each_entry(i, &ctx->table->sets, list) {
4105 if (!nft_is_active_next(ctx->net, i))
4107 if (!sscanf(i->name, name, &tmp))
4109 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
4112 set_bit(tmp - min, inuse);
4115 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
4116 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
4117 min += BITS_PER_BYTE * PAGE_SIZE;
4118 memset(inuse, 0, PAGE_SIZE);
4121 free_page((unsigned long)inuse);
4124 set->name = kasprintf(GFP_KERNEL_ACCOUNT, name, min + n);
4128 list_for_each_entry(i, &ctx->table->sets, list) {
4129 if (!nft_is_active_next(ctx->net, i))
4131 if (!strcmp(set->name, i->name)) {
4140 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result)
4142 u64 ms = be64_to_cpu(nla_get_be64(nla));
4143 u64 max = (u64)(~((u64)0));
4145 max = div_u64(max, NSEC_PER_MSEC);
4149 ms *= NSEC_PER_MSEC;
4150 *result = nsecs_to_jiffies64(ms);
4154 __be64 nf_jiffies64_to_msecs(u64 input)
4156 return cpu_to_be64(jiffies64_to_msecs(input));
4159 static int nf_tables_fill_set_concat(struct sk_buff *skb,
4160 const struct nft_set *set)
4162 struct nlattr *concat, *field;
4165 concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT);
4169 for (i = 0; i < set->field_count; i++) {
4170 field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
4174 if (nla_put_be32(skb, NFTA_SET_FIELD_LEN,
4175 htonl(set->field_len[i])))
4178 nla_nest_end(skb, field);
4181 nla_nest_end(skb, concat);
4186 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
4187 const struct nft_set *set, u16 event, u16 flags)
4189 u64 timeout = READ_ONCE(set->timeout);
4190 u32 gc_int = READ_ONCE(set->gc_int);
4191 u32 portid = ctx->portid;
4192 struct nlmsghdr *nlh;
4193 struct nlattr *nest;
4197 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
4198 nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family,
4199 NFNETLINK_V0, nft_base_seq(ctx->net));
4201 goto nla_put_failure;
4203 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
4204 goto nla_put_failure;
4205 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
4206 goto nla_put_failure;
4207 if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle),
4209 goto nla_put_failure;
4211 if (event == NFT_MSG_DELSET) {
4212 nlmsg_end(skb, nlh);
4216 if (set->flags != 0)
4217 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
4218 goto nla_put_failure;
4220 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
4221 goto nla_put_failure;
4222 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
4223 goto nla_put_failure;
4224 if (set->flags & NFT_SET_MAP) {
4225 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
4226 goto nla_put_failure;
4227 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
4228 goto nla_put_failure;
4230 if (set->flags & NFT_SET_OBJECT &&
4231 nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype)))
4232 goto nla_put_failure;
4235 nla_put_be64(skb, NFTA_SET_TIMEOUT,
4236 nf_jiffies64_to_msecs(timeout),
4238 goto nla_put_failure;
4240 nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(gc_int)))
4241 goto nla_put_failure;
4243 if (set->policy != NFT_SET_POL_PERFORMANCE) {
4244 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
4245 goto nla_put_failure;
4249 nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata))
4250 goto nla_put_failure;
4252 nest = nla_nest_start_noflag(skb, NFTA_SET_DESC);
4254 goto nla_put_failure;
4256 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
4257 goto nla_put_failure;
4259 if (set->field_count > 1 &&
4260 nf_tables_fill_set_concat(skb, set))
4261 goto nla_put_failure;
4263 nla_nest_end(skb, nest);
4265 if (set->num_exprs == 1) {
4266 nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR);
4267 if (nf_tables_fill_expr_info(skb, set->exprs[0], false) < 0)
4268 goto nla_put_failure;
4270 nla_nest_end(skb, nest);
4271 } else if (set->num_exprs > 1) {
4272 nest = nla_nest_start_noflag(skb, NFTA_SET_EXPRESSIONS);
4274 goto nla_put_failure;
4276 for (i = 0; i < set->num_exprs; i++) {
4277 if (nft_expr_dump(skb, NFTA_LIST_ELEM,
4278 set->exprs[i], false) < 0)
4279 goto nla_put_failure;
4281 nla_nest_end(skb, nest);
4284 nlmsg_end(skb, nlh);
4288 nlmsg_trim(skb, nlh);
4292 static void nf_tables_set_notify(const struct nft_ctx *ctx,
4293 const struct nft_set *set, int event,
4296 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
4297 u32 portid = ctx->portid;
4298 struct sk_buff *skb;
4303 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
4306 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
4310 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
4311 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
4313 err = nf_tables_fill_set(skb, ctx, set, event, flags);
4319 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
4322 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
4325 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
4327 const struct nft_set *set;
4328 unsigned int idx, s_idx = cb->args[0];
4329 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
4330 struct net *net = sock_net(skb->sk);
4331 struct nft_ctx *ctx = cb->data, ctx_set;
4332 struct nftables_pernet *nft_net;
4338 nft_net = nft_pernet(net);
4339 cb->seq = READ_ONCE(nft_net->base_seq);
4341 list_for_each_entry_rcu(table, &nft_net->tables, list) {
4342 if (ctx->family != NFPROTO_UNSPEC &&
4343 ctx->family != table->family)
4346 if (ctx->table && ctx->table != table)
4350 if (cur_table != table)
4356 list_for_each_entry_rcu(set, &table->sets, list) {
4359 if (!nft_is_active(net, set))
4363 ctx_set.table = table;
4364 ctx_set.family = table->family;
4366 if (nf_tables_fill_set(skb, &ctx_set, set,
4370 cb->args[2] = (unsigned long) table;
4373 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4386 static int nf_tables_dump_sets_start(struct netlink_callback *cb)
4388 struct nft_ctx *ctx_dump = NULL;
4390 ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC);
4391 if (ctx_dump == NULL)
4394 cb->data = ctx_dump;
4398 static int nf_tables_dump_sets_done(struct netlink_callback *cb)
4404 /* called with rcu_read_lock held */
4405 static int nf_tables_getset(struct sk_buff *skb, const struct nfnl_info *info,
4406 const struct nlattr * const nla[])
4408 struct netlink_ext_ack *extack = info->extack;
4409 u8 genmask = nft_genmask_cur(info->net);
4410 u8 family = info->nfmsg->nfgen_family;
4411 struct nft_table *table = NULL;
4412 struct net *net = info->net;
4413 const struct nft_set *set;
4414 struct sk_buff *skb2;
4418 if (nla[NFTA_SET_TABLE]) {
4419 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family,
4421 if (IS_ERR(table)) {
4422 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
4423 return PTR_ERR(table);
4427 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
4429 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
4430 struct netlink_dump_control c = {
4431 .start = nf_tables_dump_sets_start,
4432 .dump = nf_tables_dump_sets,
4433 .done = nf_tables_dump_sets_done,
4435 .module = THIS_MODULE,
4438 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
4441 /* Only accept unspec with dump */
4442 if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC)
4443 return -EAFNOSUPPORT;
4444 if (!nla[NFTA_SET_TABLE])
4447 set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask);
4449 return PTR_ERR(set);
4451 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
4455 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
4457 goto err_fill_set_info;
4459 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
4466 static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = {
4467 [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 },
4470 static int nft_set_desc_concat_parse(const struct nlattr *attr,
4471 struct nft_set_desc *desc)
4473 struct nlattr *tb[NFTA_SET_FIELD_MAX + 1];
4477 if (desc->field_count >= ARRAY_SIZE(desc->field_len))
4480 err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr,
4481 nft_concat_policy, NULL);
4485 if (!tb[NFTA_SET_FIELD_LEN])
4488 len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN]));
4489 if (!len || len > U8_MAX)
4492 desc->field_len[desc->field_count++] = len;
4497 static int nft_set_desc_concat(struct nft_set_desc *desc,
4498 const struct nlattr *nla)
4500 struct nlattr *attr;
4504 nla_for_each_nested(attr, nla, rem) {
4505 if (nla_type(attr) != NFTA_LIST_ELEM)
4508 err = nft_set_desc_concat_parse(attr, desc);
4513 for (i = 0; i < desc->field_count; i++)
4514 num_regs += DIV_ROUND_UP(desc->field_len[i], sizeof(u32));
4516 if (num_regs > NFT_REG32_COUNT)
4522 static int nf_tables_set_desc_parse(struct nft_set_desc *desc,
4523 const struct nlattr *nla)
4525 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
4528 err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla,
4529 nft_set_desc_policy, NULL);
4533 if (da[NFTA_SET_DESC_SIZE] != NULL)
4534 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
4535 if (da[NFTA_SET_DESC_CONCAT])
4536 err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]);
4541 static int nft_set_expr_alloc(struct nft_ctx *ctx, struct nft_set *set,
4542 const struct nlattr * const *nla,
4543 struct nft_expr **exprs, int *num_exprs,
4546 struct nft_expr *expr;
4549 if (nla[NFTA_SET_EXPR]) {
4550 expr = nft_set_elem_expr_alloc(ctx, set, nla[NFTA_SET_EXPR]);
4552 err = PTR_ERR(expr);
4553 goto err_set_expr_alloc;
4557 } else if (nla[NFTA_SET_EXPRESSIONS]) {
4561 if (!(flags & NFT_SET_EXPR)) {
4563 goto err_set_expr_alloc;
4566 nla_for_each_nested(tmp, nla[NFTA_SET_EXPRESSIONS], left) {
4567 if (i == NFT_SET_EXPR_MAX) {
4569 goto err_set_expr_alloc;
4571 if (nla_type(tmp) != NFTA_LIST_ELEM) {
4573 goto err_set_expr_alloc;
4575 expr = nft_set_elem_expr_alloc(ctx, set, tmp);
4577 err = PTR_ERR(expr);
4578 goto err_set_expr_alloc;
4588 for (i = 0; i < *num_exprs; i++)
4589 nft_expr_destroy(ctx, exprs[i]);
4594 static bool nft_set_is_same(const struct nft_set *set,
4595 const struct nft_set_desc *desc,
4596 struct nft_expr *exprs[], u32 num_exprs, u32 flags)
4600 if (set->ktype != desc->ktype ||
4601 set->dtype != desc->dtype ||
4602 set->flags != flags ||
4603 set->klen != desc->klen ||
4604 set->dlen != desc->dlen ||
4605 set->field_count != desc->field_count ||
4606 set->num_exprs != num_exprs)
4609 for (i = 0; i < desc->field_count; i++) {
4610 if (set->field_len[i] != desc->field_len[i])
4614 for (i = 0; i < num_exprs; i++) {
4615 if (set->exprs[i]->ops != exprs[i]->ops)
4622 static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,
4623 const struct nlattr * const nla[])
4625 struct netlink_ext_ack *extack = info->extack;
4626 u8 genmask = nft_genmask_next(info->net);
4627 u8 family = info->nfmsg->nfgen_family;
4628 const struct nft_set_ops *ops;
4629 struct net *net = info->net;
4630 struct nft_set_desc desc;
4631 struct nft_table *table;
4632 unsigned char *udata;
4633 struct nft_set *set;
4643 if (nla[NFTA_SET_TABLE] == NULL ||
4644 nla[NFTA_SET_NAME] == NULL ||
4645 nla[NFTA_SET_KEY_LEN] == NULL ||
4646 nla[NFTA_SET_ID] == NULL)
4649 memset(&desc, 0, sizeof(desc));
4651 desc.ktype = NFT_DATA_VALUE;
4652 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
4653 desc.ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
4654 if ((desc.ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
4658 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
4659 if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
4663 if (nla[NFTA_SET_FLAGS] != NULL) {
4664 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
4665 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
4666 NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
4667 NFT_SET_MAP | NFT_SET_EVAL |
4668 NFT_SET_OBJECT | NFT_SET_CONCAT | NFT_SET_EXPR))
4670 /* Only one of these operations is supported */
4671 if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) ==
4672 (NFT_SET_MAP | NFT_SET_OBJECT))
4674 if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) ==
4675 (NFT_SET_EVAL | NFT_SET_OBJECT))
4680 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
4681 if (!(flags & NFT_SET_MAP))
4684 desc.dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
4685 if ((desc.dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
4686 desc.dtype != NFT_DATA_VERDICT)
4689 if (desc.dtype != NFT_DATA_VERDICT) {
4690 if (nla[NFTA_SET_DATA_LEN] == NULL)
4692 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
4693 if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
4696 desc.dlen = sizeof(struct nft_verdict);
4697 } else if (flags & NFT_SET_MAP)
4700 if (nla[NFTA_SET_OBJ_TYPE] != NULL) {
4701 if (!(flags & NFT_SET_OBJECT))
4704 desc.objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE]));
4705 if (desc.objtype == NFT_OBJECT_UNSPEC ||
4706 desc.objtype > NFT_OBJECT_MAX)
4708 } else if (flags & NFT_SET_OBJECT)
4711 desc.objtype = NFT_OBJECT_UNSPEC;
4714 if (nla[NFTA_SET_TIMEOUT] != NULL) {
4715 if (!(flags & NFT_SET_TIMEOUT))
4718 err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &desc.timeout);
4723 if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
4724 if (!(flags & NFT_SET_TIMEOUT))
4726 desc.gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
4729 desc.policy = NFT_SET_POL_PERFORMANCE;
4730 if (nla[NFTA_SET_POLICY] != NULL)
4731 desc.policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
4733 if (nla[NFTA_SET_DESC] != NULL) {
4734 err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]);
4738 if (desc.field_count > 1 && !(flags & NFT_SET_CONCAT))
4740 } else if (flags & NFT_SET_CONCAT) {
4744 if (nla[NFTA_SET_EXPR] || nla[NFTA_SET_EXPRESSIONS])
4747 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask,
4748 NETLINK_CB(skb).portid);
4749 if (IS_ERR(table)) {
4750 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
4751 return PTR_ERR(table);
4754 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
4756 set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask);
4758 if (PTR_ERR(set) != -ENOENT) {
4759 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
4760 return PTR_ERR(set);
4763 struct nft_expr *exprs[NFT_SET_EXPR_MAX] = {};
4765 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
4766 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
4769 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
4772 err = nft_set_expr_alloc(&ctx, set, nla, exprs, &num_exprs, flags);
4777 if (!nft_set_is_same(set, &desc, exprs, num_exprs, flags)) {
4778 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
4782 for (i = 0; i < num_exprs; i++)
4783 nft_expr_destroy(&ctx, exprs[i]);
4788 return __nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set, &desc);
4791 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
4794 ops = nft_select_set_ops(&ctx, nla, &desc);
4796 return PTR_ERR(ops);
4799 if (nla[NFTA_SET_USERDATA])
4800 udlen = nla_len(nla[NFTA_SET_USERDATA]);
4803 if (ops->privsize != NULL)
4804 size = ops->privsize(nla, &desc);
4805 alloc_size = sizeof(*set) + size + udlen;
4806 if (alloc_size < size || alloc_size > INT_MAX)
4808 set = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT);
4812 name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL_ACCOUNT);
4818 err = nf_tables_set_alloc_name(&ctx, set, name);
4825 udata = set->data + size;
4826 nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen);
4829 INIT_LIST_HEAD(&set->bindings);
4830 INIT_LIST_HEAD(&set->catchall_list);
4832 write_pnet(&set->net, net);
4834 set->ktype = desc.ktype;
4835 set->klen = desc.klen;
4836 set->dtype = desc.dtype;
4837 set->objtype = desc.objtype;
4838 set->dlen = desc.dlen;
4840 set->size = desc.size;
4841 set->policy = desc.policy;
4844 set->timeout = desc.timeout;
4845 set->gc_int = desc.gc_int;
4847 set->field_count = desc.field_count;
4848 for (i = 0; i < desc.field_count; i++)
4849 set->field_len[i] = desc.field_len[i];
4851 err = ops->init(set, &desc, nla);
4855 err = nft_set_expr_alloc(&ctx, set, nla, set->exprs, &num_exprs, flags);
4857 goto err_set_destroy;
4859 set->num_exprs = num_exprs;
4860 set->handle = nf_tables_alloc_handle(table);
4862 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
4864 goto err_set_expr_alloc;
4866 list_add_tail_rcu(&set->list, &table->sets);
4871 for (i = 0; i < set->num_exprs; i++)
4872 nft_expr_destroy(&ctx, set->exprs[i]);
4882 static void nft_set_catchall_destroy(const struct nft_ctx *ctx,
4883 struct nft_set *set)
4885 struct nft_set_elem_catchall *next, *catchall;
4887 list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
4888 list_del_rcu(&catchall->list);
4889 nft_set_elem_destroy(set, catchall->elem, true);
4890 kfree_rcu(catchall, rcu);
4894 static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
4898 if (WARN_ON(set->use > 0))
4901 for (i = 0; i < set->num_exprs; i++)
4902 nft_expr_destroy(ctx, set->exprs[i]);
4904 set->ops->destroy(set);
4905 nft_set_catchall_destroy(ctx, set);
4910 static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info,
4911 const struct nlattr * const nla[])
4913 struct netlink_ext_ack *extack = info->extack;
4914 u8 genmask = nft_genmask_next(info->net);
4915 u8 family = info->nfmsg->nfgen_family;
4916 struct net *net = info->net;
4917 const struct nlattr *attr;
4918 struct nft_table *table;
4919 struct nft_set *set;
4922 if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC)
4923 return -EAFNOSUPPORT;
4925 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family,
4926 genmask, NETLINK_CB(skb).portid);
4927 if (IS_ERR(table)) {
4928 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
4929 return PTR_ERR(table);
4932 if (nla[NFTA_SET_HANDLE]) {
4933 attr = nla[NFTA_SET_HANDLE];
4934 set = nft_set_lookup_byhandle(table, attr, genmask);
4936 attr = nla[NFTA_SET_NAME];
4937 set = nft_set_lookup(table, attr, genmask);
4941 if (PTR_ERR(set) == -ENOENT &&
4942 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSET)
4945 NL_SET_BAD_ATTR(extack, attr);
4946 return PTR_ERR(set);
4949 (info->nlh->nlmsg_flags & NLM_F_NONREC &&
4950 atomic_read(&set->nelems) > 0)) {
4951 NL_SET_BAD_ATTR(extack, attr);
4955 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
4957 return nft_delset(&ctx, set);
4960 static int nft_validate_register_store(const struct nft_ctx *ctx,
4961 enum nft_registers reg,
4962 const struct nft_data *data,
4963 enum nft_data_types type,
4966 static int nft_setelem_data_validate(const struct nft_ctx *ctx,
4967 struct nft_set *set,
4968 struct nft_set_elem *elem)
4970 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
4971 enum nft_registers dreg;
4973 dreg = nft_type_to_reg(set->dtype);
4974 return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
4975 set->dtype == NFT_DATA_VERDICT ?
4976 NFT_DATA_VERDICT : NFT_DATA_VALUE,
4980 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
4981 struct nft_set *set,
4982 const struct nft_set_iter *iter,
4983 struct nft_set_elem *elem)
4985 return nft_setelem_data_validate(ctx, set, elem);
4988 static int nft_set_catchall_bind_check(const struct nft_ctx *ctx,
4989 struct nft_set *set)
4991 u8 genmask = nft_genmask_next(ctx->net);
4992 struct nft_set_elem_catchall *catchall;
4993 struct nft_set_elem elem;
4994 struct nft_set_ext *ext;
4997 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
4998 ext = nft_set_elem_ext(set, catchall->elem);
4999 if (!nft_set_elem_active(ext, genmask))
5002 elem.priv = catchall->elem;
5003 ret = nft_setelem_data_validate(ctx, set, &elem);
5011 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
5012 struct nft_set_binding *binding)
5014 struct nft_set_binding *i;
5015 struct nft_set_iter iter;
5017 if (set->use == UINT_MAX)
5020 if (!list_empty(&set->bindings) && nft_set_is_anonymous(set))
5023 if (binding->flags & NFT_SET_MAP) {
5024 /* If the set is already bound to the same chain all
5025 * jumps are already validated for that chain.
5027 list_for_each_entry(i, &set->bindings, list) {
5028 if (i->flags & NFT_SET_MAP &&
5029 i->chain == binding->chain)
5033 iter.genmask = nft_genmask_next(ctx->net);
5037 iter.fn = nf_tables_bind_check_setelem;
5039 set->ops->walk(ctx, set, &iter);
5041 iter.err = nft_set_catchall_bind_check(ctx, set);
5047 binding->chain = ctx->chain;
5048 list_add_tail_rcu(&binding->list, &set->bindings);
5049 nft_set_trans_bind(ctx, set);
5054 EXPORT_SYMBOL_GPL(nf_tables_bind_set);
5056 static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
5057 struct nft_set_binding *binding, bool event)
5059 list_del_rcu(&binding->list);
5061 if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) {
5062 list_del_rcu(&set->list);
5064 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET,
5069 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
5070 struct nft_set_binding *binding,
5071 enum nft_trans_phase phase)
5074 case NFT_TRANS_PREPARE:
5077 case NFT_TRANS_ABORT:
5078 case NFT_TRANS_RELEASE:
5082 nf_tables_unbind_set(ctx, set, binding,
5083 phase == NFT_TRANS_COMMIT);
5086 EXPORT_SYMBOL_GPL(nf_tables_deactivate_set);
5088 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set)
5090 if (list_empty(&set->bindings) && nft_set_is_anonymous(set))
5091 nft_set_destroy(ctx, set);
5093 EXPORT_SYMBOL_GPL(nf_tables_destroy_set);
5095 const struct nft_set_ext_type nft_set_ext_types[] = {
5096 [NFT_SET_EXT_KEY] = {
5097 .align = __alignof__(u32),
5099 [NFT_SET_EXT_DATA] = {
5100 .align = __alignof__(u32),
5102 [NFT_SET_EXT_EXPRESSIONS] = {
5103 .align = __alignof__(struct nft_set_elem_expr),
5105 [NFT_SET_EXT_OBJREF] = {
5106 .len = sizeof(struct nft_object *),
5107 .align = __alignof__(struct nft_object *),
5109 [NFT_SET_EXT_FLAGS] = {
5111 .align = __alignof__(u8),
5113 [NFT_SET_EXT_TIMEOUT] = {
5115 .align = __alignof__(u64),
5117 [NFT_SET_EXT_EXPIRATION] = {
5119 .align = __alignof__(u64),
5121 [NFT_SET_EXT_USERDATA] = {
5122 .len = sizeof(struct nft_userdata),
5123 .align = __alignof__(struct nft_userdata),
5125 [NFT_SET_EXT_KEY_END] = {
5126 .align = __alignof__(u32),
5134 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
5135 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
5136 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
5137 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
5138 [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 },
5139 [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 },
5140 [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY,
5141 .len = NFT_USERDATA_MAXLEN },
5142 [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED },
5143 [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING,
5144 .len = NFT_OBJ_MAXNAMELEN - 1 },
5145 [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED },
5146 [NFTA_SET_ELEM_EXPRESSIONS] = { .type = NLA_NESTED },
5149 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
5150 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING,
5151 .len = NFT_TABLE_MAXNAMELEN - 1 },
5152 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING,
5153 .len = NFT_SET_MAXNAMELEN - 1 },
5154 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
5155 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
5158 static int nft_set_elem_expr_dump(struct sk_buff *skb,
5159 const struct nft_set *set,
5160 const struct nft_set_ext *ext)
5162 struct nft_set_elem_expr *elem_expr;
5163 u32 size, num_exprs = 0;
5164 struct nft_expr *expr;
5165 struct nlattr *nest;
5167 elem_expr = nft_set_ext_expr(ext);
5168 nft_setelem_expr_foreach(expr, elem_expr, size)
5171 if (num_exprs == 1) {
5172 expr = nft_setelem_expr_at(elem_expr, 0);
5173 if (nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, expr, false) < 0)
5177 } else if (num_exprs > 1) {
5178 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_EXPRESSIONS);
5180 goto nla_put_failure;
5182 nft_setelem_expr_foreach(expr, elem_expr, size) {
5183 expr = nft_setelem_expr_at(elem_expr, size);
5184 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, false) < 0)
5185 goto nla_put_failure;
5187 nla_nest_end(skb, nest);
5195 static int nf_tables_fill_setelem(struct sk_buff *skb,
5196 const struct nft_set *set,
5197 const struct nft_set_elem *elem)
5199 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5200 unsigned char *b = skb_tail_pointer(skb);
5201 struct nlattr *nest;
5203 nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
5205 goto nla_put_failure;
5207 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) &&
5208 nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
5209 NFT_DATA_VALUE, set->klen) < 0)
5210 goto nla_put_failure;
5212 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) &&
5213 nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext),
5214 NFT_DATA_VALUE, set->klen) < 0)
5215 goto nla_put_failure;
5217 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
5218 nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
5219 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
5221 goto nla_put_failure;
5223 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS) &&
5224 nft_set_elem_expr_dump(skb, set, ext))
5225 goto nla_put_failure;
5227 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
5228 nla_put_string(skb, NFTA_SET_ELEM_OBJREF,
5229 (*nft_set_ext_obj(ext))->key.name) < 0)
5230 goto nla_put_failure;
5232 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
5233 nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
5234 htonl(*nft_set_ext_flags(ext))))
5235 goto nla_put_failure;
5237 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
5238 nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
5239 nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)),
5241 goto nla_put_failure;
5243 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
5244 u64 expires, now = get_jiffies_64();
5246 expires = *nft_set_ext_expiration(ext);
5247 if (time_before64(now, expires))
5252 if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
5253 nf_jiffies64_to_msecs(expires),
5255 goto nla_put_failure;
5258 if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
5259 struct nft_userdata *udata;
5261 udata = nft_set_ext_userdata(ext);
5262 if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
5263 udata->len + 1, udata->data))
5264 goto nla_put_failure;
5267 nla_nest_end(skb, nest);
5275 struct nft_set_dump_args {
5276 const struct netlink_callback *cb;
5277 struct nft_set_iter iter;
5278 struct sk_buff *skb;
5281 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
5282 struct nft_set *set,
5283 const struct nft_set_iter *iter,
5284 struct nft_set_elem *elem)
5286 struct nft_set_dump_args *args;
5288 args = container_of(iter, struct nft_set_dump_args, iter);
5289 return nf_tables_fill_setelem(args->skb, set, elem);
5292 struct nft_set_dump_ctx {
5293 const struct nft_set *set;
5297 static int nft_set_catchall_dump(struct net *net, struct sk_buff *skb,
5298 const struct nft_set *set)
5300 struct nft_set_elem_catchall *catchall;
5301 u8 genmask = nft_genmask_cur(net);
5302 struct nft_set_elem elem;
5303 struct nft_set_ext *ext;
5306 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
5307 ext = nft_set_elem_ext(set, catchall->elem);
5308 if (!nft_set_elem_active(ext, genmask) ||
5309 nft_set_elem_expired(ext))
5312 elem.priv = catchall->elem;
5313 ret = nf_tables_fill_setelem(skb, set, &elem);
5320 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
5322 struct nft_set_dump_ctx *dump_ctx = cb->data;
5323 struct net *net = sock_net(skb->sk);
5324 struct nftables_pernet *nft_net;
5325 struct nft_table *table;
5326 struct nft_set *set;
5327 struct nft_set_dump_args args;
5328 bool set_found = false;
5329 struct nlmsghdr *nlh;
5330 struct nlattr *nest;
5335 nft_net = nft_pernet(net);
5336 cb->seq = READ_ONCE(nft_net->base_seq);
5338 list_for_each_entry_rcu(table, &nft_net->tables, list) {
5339 if (dump_ctx->ctx.family != NFPROTO_UNSPEC &&
5340 dump_ctx->ctx.family != table->family)
5343 if (table != dump_ctx->ctx.table)
5346 list_for_each_entry_rcu(set, &table->sets, list) {
5347 if (set == dump_ctx->set) {
5360 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM);
5361 portid = NETLINK_CB(cb->skb).portid;
5362 seq = cb->nlh->nlmsg_seq;
5364 nlh = nfnl_msg_put(skb, portid, seq, event, NLM_F_MULTI,
5365 table->family, NFNETLINK_V0, nft_base_seq(net));
5367 goto nla_put_failure;
5369 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name))
5370 goto nla_put_failure;
5371 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
5372 goto nla_put_failure;
5374 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
5376 goto nla_put_failure;
5380 args.iter.genmask = nft_genmask_cur(net);
5381 args.iter.skip = cb->args[0];
5382 args.iter.count = 0;
5384 args.iter.fn = nf_tables_dump_setelem;
5385 set->ops->walk(&dump_ctx->ctx, set, &args.iter);
5387 if (!args.iter.err && args.iter.count == cb->args[0])
5388 args.iter.err = nft_set_catchall_dump(net, skb, set);
5391 nla_nest_end(skb, nest);
5392 nlmsg_end(skb, nlh);
5394 if (args.iter.err && args.iter.err != -EMSGSIZE)
5395 return args.iter.err;
5396 if (args.iter.count == cb->args[0])
5399 cb->args[0] = args.iter.count;
5407 static int nf_tables_dump_set_start(struct netlink_callback *cb)
5409 struct nft_set_dump_ctx *dump_ctx = cb->data;
5411 cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC);
5413 return cb->data ? 0 : -ENOMEM;
5416 static int nf_tables_dump_set_done(struct netlink_callback *cb)
5422 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
5423 const struct nft_ctx *ctx, u32 seq,
5424 u32 portid, int event, u16 flags,
5425 const struct nft_set *set,
5426 const struct nft_set_elem *elem)
5428 struct nlmsghdr *nlh;
5429 struct nlattr *nest;
5432 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
5433 nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family,
5434 NFNETLINK_V0, nft_base_seq(ctx->net));
5436 goto nla_put_failure;
5438 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
5439 goto nla_put_failure;
5440 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
5441 goto nla_put_failure;
5443 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
5445 goto nla_put_failure;
5447 err = nf_tables_fill_setelem(skb, set, elem);
5449 goto nla_put_failure;
5451 nla_nest_end(skb, nest);
5453 nlmsg_end(skb, nlh);
5457 nlmsg_trim(skb, nlh);
5461 static int nft_setelem_parse_flags(const struct nft_set *set,
5462 const struct nlattr *attr, u32 *flags)
5467 *flags = ntohl(nla_get_be32(attr));
5468 if (*flags & ~(NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL))
5470 if (!(set->flags & NFT_SET_INTERVAL) &&
5471 *flags & NFT_SET_ELEM_INTERVAL_END)
5473 if ((*flags & (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) ==
5474 (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL))
5480 static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set,
5481 struct nft_data *key, struct nlattr *attr)
5483 struct nft_data_desc desc = {
5484 .type = NFT_DATA_VALUE,
5485 .size = NFT_DATA_VALUE_MAXLEN,
5489 return nft_data_init(ctx, key, &desc, attr);
5492 static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set,
5493 struct nft_data_desc *desc,
5494 struct nft_data *data,
5495 struct nlattr *attr)
5499 if (set->dtype == NFT_DATA_VERDICT)
5500 dtype = NFT_DATA_VERDICT;
5502 dtype = NFT_DATA_VALUE;
5505 desc->size = NFT_DATA_VALUE_MAXLEN;
5506 desc->len = set->dlen;
5507 desc->flags = NFT_DATA_DESC_SETELEM;
5509 return nft_data_init(ctx, data, desc, attr);
5512 static void *nft_setelem_catchall_get(const struct net *net,
5513 const struct nft_set *set)
5515 struct nft_set_elem_catchall *catchall;
5516 u8 genmask = nft_genmask_cur(net);
5517 struct nft_set_ext *ext;
5520 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
5521 ext = nft_set_elem_ext(set, catchall->elem);
5522 if (!nft_set_elem_active(ext, genmask) ||
5523 nft_set_elem_expired(ext))
5526 priv = catchall->elem;
5533 static int nft_setelem_get(struct nft_ctx *ctx, struct nft_set *set,
5534 struct nft_set_elem *elem, u32 flags)
5538 if (!(flags & NFT_SET_ELEM_CATCHALL)) {
5539 priv = set->ops->get(ctx->net, set, elem, flags);
5541 return PTR_ERR(priv);
5543 priv = nft_setelem_catchall_get(ctx->net, set);
5552 static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set,
5553 const struct nlattr *attr)
5555 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
5556 struct nft_set_elem elem;
5557 struct sk_buff *skb;
5561 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
5562 nft_set_elem_policy, NULL);
5566 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
5570 if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL))
5573 if (nla[NFTA_SET_ELEM_KEY]) {
5574 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
5575 nla[NFTA_SET_ELEM_KEY]);
5580 if (nla[NFTA_SET_ELEM_KEY_END]) {
5581 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
5582 nla[NFTA_SET_ELEM_KEY_END]);
5587 err = nft_setelem_get(ctx, set, &elem, flags);
5592 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
5596 err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid,
5597 NFT_MSG_NEWSETELEM, 0, set, &elem);
5599 goto err_fill_setelem;
5601 return nfnetlink_unicast(skb, ctx->net, ctx->portid);
5608 /* called with rcu_read_lock held */
5609 static int nf_tables_getsetelem(struct sk_buff *skb,
5610 const struct nfnl_info *info,
5611 const struct nlattr * const nla[])
5613 struct netlink_ext_ack *extack = info->extack;
5614 u8 genmask = nft_genmask_cur(info->net);
5615 u8 family = info->nfmsg->nfgen_family;
5616 struct net *net = info->net;
5617 struct nft_table *table;
5618 struct nft_set *set;
5619 struct nlattr *attr;
5623 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
5625 if (IS_ERR(table)) {
5626 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
5627 return PTR_ERR(table);
5630 set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
5632 return PTR_ERR(set);
5634 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
5636 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
5637 struct netlink_dump_control c = {
5638 .start = nf_tables_dump_set_start,
5639 .dump = nf_tables_dump_set,
5640 .done = nf_tables_dump_set_done,
5641 .module = THIS_MODULE,
5643 struct nft_set_dump_ctx dump_ctx = {
5649 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
5652 if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
5655 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
5656 err = nft_get_set_elem(&ctx, set, attr);
5658 NL_SET_BAD_ATTR(extack, attr);
5666 static void nf_tables_setelem_notify(const struct nft_ctx *ctx,
5667 const struct nft_set *set,
5668 const struct nft_set_elem *elem,
5671 struct nftables_pernet *nft_net;
5672 struct net *net = ctx->net;
5673 u32 portid = ctx->portid;
5674 struct sk_buff *skb;
5678 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
5681 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5685 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
5686 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
5688 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
5695 nft_net = nft_pernet(net);
5696 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
5699 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
5702 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
5704 struct nft_set *set)
5706 struct nft_trans *trans;
5708 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
5712 nft_trans_elem_set(trans) = set;
5716 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
5717 const struct nft_set *set,
5718 const struct nlattr *attr)
5720 struct nft_expr *expr;
5723 expr = nft_expr_init(ctx, attr);
5728 if (expr->ops->type->flags & NFT_EXPR_GC) {
5729 if (set->flags & NFT_SET_TIMEOUT)
5730 goto err_set_elem_expr;
5731 if (!set->ops->gc_init)
5732 goto err_set_elem_expr;
5733 set->ops->gc_init(set);
5739 nft_expr_destroy(ctx, expr);
5740 return ERR_PTR(err);
5743 static int nft_set_ext_check(const struct nft_set_ext_tmpl *tmpl, u8 id, u32 len)
5745 len += nft_set_ext_types[id].len;
5746 if (len > tmpl->ext_len[id] ||
5753 static int nft_set_ext_memcpy(const struct nft_set_ext_tmpl *tmpl, u8 id,
5754 void *to, const void *from, u32 len)
5756 if (nft_set_ext_check(tmpl, id, len) < 0)
5759 memcpy(to, from, len);
5764 void *nft_set_elem_init(const struct nft_set *set,
5765 const struct nft_set_ext_tmpl *tmpl,
5766 const u32 *key, const u32 *key_end,
5767 const u32 *data, u64 timeout, u64 expiration, gfp_t gfp)
5769 struct nft_set_ext *ext;
5772 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
5774 return ERR_PTR(-ENOMEM);
5776 ext = nft_set_elem_ext(set, elem);
5777 nft_set_ext_init(ext, tmpl);
5779 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) &&
5780 nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY,
5781 nft_set_ext_key(ext), key, set->klen) < 0)
5784 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) &&
5785 nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY_END,
5786 nft_set_ext_key_end(ext), key_end, set->klen) < 0)
5789 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
5790 nft_set_ext_memcpy(tmpl, NFT_SET_EXT_DATA,
5791 nft_set_ext_data(ext), data, set->dlen) < 0)
5794 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
5795 *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration;
5796 if (expiration == 0)
5797 *nft_set_ext_expiration(ext) += timeout;
5799 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
5800 *nft_set_ext_timeout(ext) = timeout;
5807 return ERR_PTR(-EINVAL);
5810 static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
5811 struct nft_expr *expr)
5813 if (expr->ops->destroy_clone) {
5814 expr->ops->destroy_clone(ctx, expr);
5815 module_put(expr->ops->type->owner);
5817 nf_tables_expr_destroy(ctx, expr);
5821 static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
5822 struct nft_set_elem_expr *elem_expr)
5824 struct nft_expr *expr;
5827 nft_setelem_expr_foreach(expr, elem_expr, size)
5828 __nft_set_elem_expr_destroy(ctx, expr);
5831 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
5834 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
5835 struct nft_ctx ctx = {
5836 .net = read_pnet(&set->net),
5837 .family = set->table->family,
5840 nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE);
5841 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
5842 nft_data_release(nft_set_ext_data(ext), set->dtype);
5843 if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
5844 nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext));
5846 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
5847 (*nft_set_ext_obj(ext))->use--;
5850 EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
5852 /* Only called from commit path, nft_setelem_data_deactivate() already deals
5853 * with the refcounting from the preparation phase.
5855 static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
5856 const struct nft_set *set, void *elem)
5858 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
5860 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
5861 nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext));
5866 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
5867 struct nft_expr *expr_array[])
5869 struct nft_expr *expr;
5872 for (i = 0; i < set->num_exprs; i++) {
5873 expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL_ACCOUNT);
5877 err = nft_expr_clone(expr, set->exprs[i]);
5882 expr_array[i] = expr;
5888 for (k = i - 1; k >= 0; k--)
5889 nft_expr_destroy(ctx, expr_array[k]);
5894 static int nft_set_elem_expr_setup(struct nft_ctx *ctx,
5895 const struct nft_set_ext_tmpl *tmpl,
5896 const struct nft_set_ext *ext,
5897 struct nft_expr *expr_array[],
5900 struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext);
5901 u32 len = sizeof(struct nft_set_elem_expr);
5902 struct nft_expr *expr;
5908 for (i = 0; i < num_exprs; i++)
5909 len += expr_array[i]->ops->size;
5911 if (nft_set_ext_check(tmpl, NFT_SET_EXT_EXPRESSIONS, len) < 0)
5914 for (i = 0; i < num_exprs; i++) {
5915 expr = nft_setelem_expr_at(elem_expr, elem_expr->size);
5916 err = nft_expr_clone(expr, expr_array[i]);
5918 goto err_elem_expr_setup;
5920 elem_expr->size += expr_array[i]->ops->size;
5921 nft_expr_destroy(ctx, expr_array[i]);
5922 expr_array[i] = NULL;
5927 err_elem_expr_setup:
5928 for (; i < num_exprs; i++) {
5929 nft_expr_destroy(ctx, expr_array[i]);
5930 expr_array[i] = NULL;
5936 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
5937 const struct nft_set *set)
5939 struct nft_set_elem_catchall *catchall;
5940 u8 genmask = nft_genmask_cur(net);
5941 struct nft_set_ext *ext;
5943 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
5944 ext = nft_set_elem_ext(set, catchall->elem);
5945 if (nft_set_elem_active(ext, genmask) &&
5946 !nft_set_elem_expired(ext))
5952 EXPORT_SYMBOL_GPL(nft_set_catchall_lookup);
5954 void *nft_set_catchall_gc(const struct nft_set *set)
5956 struct nft_set_elem_catchall *catchall, *next;
5957 struct nft_set_ext *ext;
5960 list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
5961 ext = nft_set_elem_ext(set, catchall->elem);
5963 if (!nft_set_elem_expired(ext) ||
5964 nft_set_elem_mark_busy(ext))
5967 elem = catchall->elem;
5968 list_del_rcu(&catchall->list);
5969 kfree_rcu(catchall, rcu);
5975 EXPORT_SYMBOL_GPL(nft_set_catchall_gc);
5977 static int nft_setelem_catchall_insert(const struct net *net,
5978 struct nft_set *set,
5979 const struct nft_set_elem *elem,
5980 struct nft_set_ext **pext)
5982 struct nft_set_elem_catchall *catchall;
5983 u8 genmask = nft_genmask_next(net);
5984 struct nft_set_ext *ext;
5986 list_for_each_entry(catchall, &set->catchall_list, list) {
5987 ext = nft_set_elem_ext(set, catchall->elem);
5988 if (nft_set_elem_active(ext, genmask)) {
5994 catchall = kmalloc(sizeof(*catchall), GFP_KERNEL);
5998 catchall->elem = elem->priv;
5999 list_add_tail_rcu(&catchall->list, &set->catchall_list);
6004 static int nft_setelem_insert(const struct net *net,
6005 struct nft_set *set,
6006 const struct nft_set_elem *elem,
6007 struct nft_set_ext **ext, unsigned int flags)
6011 if (flags & NFT_SET_ELEM_CATCHALL)
6012 ret = nft_setelem_catchall_insert(net, set, elem, ext);
6014 ret = set->ops->insert(net, set, elem, ext);
6019 static bool nft_setelem_is_catchall(const struct nft_set *set,
6020 const struct nft_set_elem *elem)
6022 struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
6024 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
6025 *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL)
6031 static void nft_setelem_activate(struct net *net, struct nft_set *set,
6032 struct nft_set_elem *elem)
6034 struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
6036 if (nft_setelem_is_catchall(set, elem)) {
6037 nft_set_elem_change_active(net, set, ext);
6038 nft_set_elem_clear_busy(ext);
6040 set->ops->activate(net, set, elem);
6044 static int nft_setelem_catchall_deactivate(const struct net *net,
6045 struct nft_set *set,
6046 struct nft_set_elem *elem)
6048 struct nft_set_elem_catchall *catchall;
6049 struct nft_set_ext *ext;
6051 list_for_each_entry(catchall, &set->catchall_list, list) {
6052 ext = nft_set_elem_ext(set, catchall->elem);
6053 if (!nft_is_active(net, ext) ||
6054 nft_set_elem_mark_busy(ext))
6058 elem->priv = catchall->elem;
6059 nft_set_elem_change_active(net, set, ext);
6066 static int __nft_setelem_deactivate(const struct net *net,
6067 struct nft_set *set,
6068 struct nft_set_elem *elem)
6072 priv = set->ops->deactivate(net, set, elem);
6083 static int nft_setelem_deactivate(const struct net *net,
6084 struct nft_set *set,
6085 struct nft_set_elem *elem, u32 flags)
6089 if (flags & NFT_SET_ELEM_CATCHALL)
6090 ret = nft_setelem_catchall_deactivate(net, set, elem);
6092 ret = __nft_setelem_deactivate(net, set, elem);
6097 static void nft_setelem_catchall_remove(const struct net *net,
6098 const struct nft_set *set,
6099 const struct nft_set_elem *elem)
6101 struct nft_set_elem_catchall *catchall, *next;
6103 list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
6104 if (catchall->elem == elem->priv) {
6105 list_del_rcu(&catchall->list);
6106 kfree_rcu(catchall, rcu);
6112 static void nft_setelem_remove(const struct net *net,
6113 const struct nft_set *set,
6114 const struct nft_set_elem *elem)
6116 if (nft_setelem_is_catchall(set, elem))
6117 nft_setelem_catchall_remove(net, set, elem);
6119 set->ops->remove(net, set, elem);
6122 static bool nft_setelem_valid_key_end(const struct nft_set *set,
6123 struct nlattr **nla, u32 flags)
6125 if ((set->flags & (NFT_SET_CONCAT | NFT_SET_INTERVAL)) ==
6126 (NFT_SET_CONCAT | NFT_SET_INTERVAL)) {
6127 if (flags & NFT_SET_ELEM_INTERVAL_END)
6130 if (nla[NFTA_SET_ELEM_KEY_END] &&
6131 flags & NFT_SET_ELEM_CATCHALL)
6134 if (nla[NFTA_SET_ELEM_KEY_END])
6141 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
6142 const struct nlattr *attr, u32 nlmsg_flags)
6144 struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {};
6145 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
6146 u8 genmask = nft_genmask_next(ctx->net);
6147 u32 flags = 0, size = 0, num_exprs = 0;
6148 struct nft_set_ext_tmpl tmpl;
6149 struct nft_set_ext *ext, *ext2;
6150 struct nft_set_elem elem;
6151 struct nft_set_binding *binding;
6152 struct nft_object *obj = NULL;
6153 struct nft_userdata *udata;
6154 struct nft_data_desc desc;
6155 enum nft_registers dreg;
6156 struct nft_trans *trans;
6162 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
6163 nft_set_elem_policy, NULL);
6167 nft_set_ext_prepare(&tmpl);
6169 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
6173 if (((flags & NFT_SET_ELEM_CATCHALL) && nla[NFTA_SET_ELEM_KEY]) ||
6174 (!(flags & NFT_SET_ELEM_CATCHALL) && !nla[NFTA_SET_ELEM_KEY]))
6178 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
6183 if (set->flags & NFT_SET_MAP) {
6184 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
6185 !(flags & NFT_SET_ELEM_INTERVAL_END))
6188 if (nla[NFTA_SET_ELEM_DATA] != NULL)
6192 if (set->flags & NFT_SET_OBJECT) {
6193 if (!nla[NFTA_SET_ELEM_OBJREF] &&
6194 !(flags & NFT_SET_ELEM_INTERVAL_END))
6197 if (nla[NFTA_SET_ELEM_OBJREF])
6201 if (!nft_setelem_valid_key_end(set, nla, flags))
6204 if ((flags & NFT_SET_ELEM_INTERVAL_END) &&
6205 (nla[NFTA_SET_ELEM_DATA] ||
6206 nla[NFTA_SET_ELEM_OBJREF] ||
6207 nla[NFTA_SET_ELEM_TIMEOUT] ||
6208 nla[NFTA_SET_ELEM_EXPIRATION] ||
6209 nla[NFTA_SET_ELEM_USERDATA] ||
6210 nla[NFTA_SET_ELEM_EXPR] ||
6211 nla[NFTA_SET_ELEM_KEY_END] ||
6212 nla[NFTA_SET_ELEM_EXPRESSIONS]))
6216 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
6217 if (!(set->flags & NFT_SET_TIMEOUT))
6219 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT],
6223 } else if (set->flags & NFT_SET_TIMEOUT &&
6224 !(flags & NFT_SET_ELEM_INTERVAL_END)) {
6225 timeout = READ_ONCE(set->timeout);
6229 if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) {
6230 if (!(set->flags & NFT_SET_TIMEOUT))
6232 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION],
6238 if (nla[NFTA_SET_ELEM_EXPR]) {
6239 struct nft_expr *expr;
6241 if (set->num_exprs && set->num_exprs != 1)
6244 expr = nft_set_elem_expr_alloc(ctx, set,
6245 nla[NFTA_SET_ELEM_EXPR]);
6247 return PTR_ERR(expr);
6249 expr_array[0] = expr;
6252 if (set->num_exprs && set->exprs[0]->ops != expr->ops) {
6254 goto err_set_elem_expr;
6256 } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) {
6257 struct nft_expr *expr;
6262 nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) {
6263 if (i == NFT_SET_EXPR_MAX ||
6264 (set->num_exprs && set->num_exprs == i)) {
6266 goto err_set_elem_expr;
6268 if (nla_type(tmp) != NFTA_LIST_ELEM) {
6270 goto err_set_elem_expr;
6272 expr = nft_set_elem_expr_alloc(ctx, set, tmp);
6274 err = PTR_ERR(expr);
6275 goto err_set_elem_expr;
6277 expr_array[i] = expr;
6280 if (set->num_exprs && expr->ops != set->exprs[i]->ops) {
6282 goto err_set_elem_expr;
6286 if (set->num_exprs && set->num_exprs != i) {
6288 goto err_set_elem_expr;
6290 } else if (set->num_exprs > 0 &&
6291 !(flags & NFT_SET_ELEM_INTERVAL_END)) {
6292 err = nft_set_elem_expr_clone(ctx, set, expr_array);
6294 goto err_set_elem_expr_clone;
6296 num_exprs = set->num_exprs;
6299 if (nla[NFTA_SET_ELEM_KEY]) {
6300 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
6301 nla[NFTA_SET_ELEM_KEY]);
6303 goto err_set_elem_expr;
6305 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
6310 if (nla[NFTA_SET_ELEM_KEY_END]) {
6311 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
6312 nla[NFTA_SET_ELEM_KEY_END]);
6316 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
6318 goto err_parse_key_end;
6322 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
6324 goto err_parse_key_end;
6326 if (timeout != READ_ONCE(set->timeout)) {
6327 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
6329 goto err_parse_key_end;
6334 for (i = 0; i < num_exprs; i++)
6335 size += expr_array[i]->ops->size;
6337 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS,
6338 sizeof(struct nft_set_elem_expr) + size);
6340 goto err_parse_key_end;
6343 if (nla[NFTA_SET_ELEM_OBJREF] != NULL) {
6344 obj = nft_obj_lookup(ctx->net, ctx->table,
6345 nla[NFTA_SET_ELEM_OBJREF],
6346 set->objtype, genmask);
6349 goto err_parse_key_end;
6351 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF);
6353 goto err_parse_key_end;
6356 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
6357 err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val,
6358 nla[NFTA_SET_ELEM_DATA]);
6360 goto err_parse_key_end;
6362 dreg = nft_type_to_reg(set->dtype);
6363 list_for_each_entry(binding, &set->bindings, list) {
6364 struct nft_ctx bind_ctx = {
6366 .family = ctx->family,
6367 .table = ctx->table,
6368 .chain = (struct nft_chain *)binding->chain,
6371 if (!(binding->flags & NFT_SET_MAP))
6374 err = nft_validate_register_store(&bind_ctx, dreg,
6376 desc.type, desc.len);
6378 goto err_parse_data;
6380 if (desc.type == NFT_DATA_VERDICT &&
6381 (elem.data.val.verdict.code == NFT_GOTO ||
6382 elem.data.val.verdict.code == NFT_JUMP))
6383 nft_validate_state_update(ctx->table,
6387 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len);
6389 goto err_parse_data;
6392 /* The full maximum length of userdata can exceed the maximum
6393 * offset value (U8_MAX) for following extensions, therefor it
6394 * must be the last extension added.
6397 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
6398 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
6400 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
6403 goto err_parse_data;
6407 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
6408 elem.key_end.val.data, elem.data.val.data,
6409 timeout, expiration, GFP_KERNEL_ACCOUNT);
6410 if (IS_ERR(elem.priv)) {
6411 err = PTR_ERR(elem.priv);
6412 goto err_parse_data;
6415 ext = nft_set_elem_ext(set, elem.priv);
6417 *nft_set_ext_flags(ext) = flags;
6420 if (nft_set_ext_check(&tmpl, NFT_SET_EXT_USERDATA, ulen) < 0) {
6422 goto err_elem_userdata;
6424 udata = nft_set_ext_userdata(ext);
6425 udata->len = ulen - 1;
6426 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
6429 *nft_set_ext_obj(ext) = obj;
6432 err = nft_set_elem_expr_setup(ctx, &tmpl, ext, expr_array, num_exprs);
6436 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
6437 if (trans == NULL) {
6442 ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
6444 err = nft_setelem_insert(ctx->net, set, &elem, &ext2, flags);
6446 if (err == -EEXIST) {
6447 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^
6448 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) ||
6449 nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^
6450 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF))
6451 goto err_element_clash;
6452 if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
6453 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) &&
6454 memcmp(nft_set_ext_data(ext),
6455 nft_set_ext_data(ext2), set->dlen) != 0) ||
6456 (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
6457 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) &&
6458 *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2)))
6459 goto err_element_clash;
6460 else if (!(nlmsg_flags & NLM_F_EXCL))
6462 } else if (err == -ENOTEMPTY) {
6463 /* ENOTEMPTY reports overlapping between this element
6464 * and an existing one.
6468 goto err_element_clash;
6471 if (!(flags & NFT_SET_ELEM_CATCHALL) && set->size &&
6472 !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) {
6477 nft_trans_elem(trans) = elem;
6478 nft_trans_commit_list_add_tail(ctx->net, trans);
6482 nft_setelem_remove(ctx->net, set, &elem);
6489 nf_tables_set_elem_destroy(ctx, set, elem.priv);
6491 if (nla[NFTA_SET_ELEM_DATA] != NULL)
6492 nft_data_release(&elem.data.val, desc.type);
6494 nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
6496 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
6498 for (i = 0; i < num_exprs && expr_array[i]; i++)
6499 nft_expr_destroy(ctx, expr_array[i]);
6500 err_set_elem_expr_clone:
6504 static int nf_tables_newsetelem(struct sk_buff *skb,
6505 const struct nfnl_info *info,
6506 const struct nlattr * const nla[])
6508 struct netlink_ext_ack *extack = info->extack;
6509 u8 genmask = nft_genmask_next(info->net);
6510 u8 family = info->nfmsg->nfgen_family;
6511 struct net *net = info->net;
6512 const struct nlattr *attr;
6513 struct nft_table *table;
6514 struct nft_set *set;
6518 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
6521 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
6522 genmask, NETLINK_CB(skb).portid);
6523 if (IS_ERR(table)) {
6524 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
6525 return PTR_ERR(table);
6528 set = nft_set_lookup_global(net, table, nla[NFTA_SET_ELEM_LIST_SET],
6529 nla[NFTA_SET_ELEM_LIST_SET_ID], genmask);
6531 return PTR_ERR(set);
6533 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
6536 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
6538 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
6539 err = nft_add_set_elem(&ctx, set, attr, info->nlh->nlmsg_flags);
6541 NL_SET_BAD_ATTR(extack, attr);
6546 if (table->validate_state == NFT_VALIDATE_DO)
6547 return nft_table_validate(net, table);
6553 * nft_data_hold - hold a nft_data item
6555 * @data: struct nft_data to release
6556 * @type: type of data
6558 * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded,
6559 * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and
6560 * NFT_GOTO verdicts. This function must be called on active data objects
6561 * from the second phase of the commit protocol.
6563 void nft_data_hold(const struct nft_data *data, enum nft_data_types type)
6565 struct nft_chain *chain;
6566 struct nft_rule *rule;
6568 if (type == NFT_DATA_VERDICT) {
6569 switch (data->verdict.code) {
6572 chain = data->verdict.chain;
6575 if (!nft_chain_is_bound(chain))
6578 chain->table->use++;
6579 list_for_each_entry(rule, &chain->rules, list)
6582 nft_chain_add(chain->table, chain);
6588 static void nft_setelem_data_activate(const struct net *net,
6589 const struct nft_set *set,
6590 struct nft_set_elem *elem)
6592 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
6594 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
6595 nft_data_hold(nft_set_ext_data(ext), set->dtype);
6596 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
6597 (*nft_set_ext_obj(ext))->use++;
6600 static void nft_setelem_data_deactivate(const struct net *net,
6601 const struct nft_set *set,
6602 struct nft_set_elem *elem)
6604 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
6606 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
6607 nft_data_release(nft_set_ext_data(ext), set->dtype);
6608 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
6609 (*nft_set_ext_obj(ext))->use--;
6612 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
6613 const struct nlattr *attr)
6615 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
6616 struct nft_set_ext_tmpl tmpl;
6617 struct nft_set_elem elem;
6618 struct nft_set_ext *ext;
6619 struct nft_trans *trans;
6623 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
6624 nft_set_elem_policy, NULL);
6628 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
6632 if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL))
6635 if (!nft_setelem_valid_key_end(set, nla, flags))
6638 nft_set_ext_prepare(&tmpl);
6641 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
6646 if (nla[NFTA_SET_ELEM_KEY]) {
6647 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
6648 nla[NFTA_SET_ELEM_KEY]);
6652 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
6657 if (nla[NFTA_SET_ELEM_KEY_END]) {
6658 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
6659 nla[NFTA_SET_ELEM_KEY_END]);
6663 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
6665 goto fail_elem_key_end;
6669 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
6670 elem.key_end.val.data, NULL, 0, 0,
6671 GFP_KERNEL_ACCOUNT);
6672 if (IS_ERR(elem.priv)) {
6673 err = PTR_ERR(elem.priv);
6674 goto fail_elem_key_end;
6677 ext = nft_set_elem_ext(set, elem.priv);
6679 *nft_set_ext_flags(ext) = flags;
6681 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
6685 err = nft_setelem_deactivate(ctx->net, set, &elem, flags);
6689 nft_setelem_data_deactivate(ctx->net, set, &elem);
6691 nft_trans_elem(trans) = elem;
6692 nft_trans_commit_list_add_tail(ctx->net, trans);
6700 nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
6702 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
6706 static int nft_setelem_flush(const struct nft_ctx *ctx,
6707 struct nft_set *set,
6708 const struct nft_set_iter *iter,
6709 struct nft_set_elem *elem)
6711 struct nft_trans *trans;
6714 trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
6715 sizeof(struct nft_trans_elem), GFP_ATOMIC);
6719 if (!set->ops->flush(ctx->net, set, elem->priv)) {
6725 nft_setelem_data_deactivate(ctx->net, set, elem);
6726 nft_trans_elem_set(trans) = set;
6727 nft_trans_elem(trans) = *elem;
6728 nft_trans_commit_list_add_tail(ctx->net, trans);
6736 static int __nft_set_catchall_flush(const struct nft_ctx *ctx,
6737 struct nft_set *set,
6738 struct nft_set_elem *elem)
6740 struct nft_trans *trans;
6742 trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
6743 sizeof(struct nft_trans_elem), GFP_KERNEL);
6747 nft_setelem_data_deactivate(ctx->net, set, elem);
6748 nft_trans_elem_set(trans) = set;
6749 nft_trans_elem(trans) = *elem;
6750 nft_trans_commit_list_add_tail(ctx->net, trans);
6755 static int nft_set_catchall_flush(const struct nft_ctx *ctx,
6756 struct nft_set *set)
6758 u8 genmask = nft_genmask_next(ctx->net);
6759 struct nft_set_elem_catchall *catchall;
6760 struct nft_set_elem elem;
6761 struct nft_set_ext *ext;
6764 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
6765 ext = nft_set_elem_ext(set, catchall->elem);
6766 if (!nft_set_elem_active(ext, genmask) ||
6767 nft_set_elem_mark_busy(ext))
6770 elem.priv = catchall->elem;
6771 ret = __nft_set_catchall_flush(ctx, set, &elem);
6779 static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask)
6781 struct nft_set_iter iter = {
6783 .fn = nft_setelem_flush,
6786 set->ops->walk(ctx, set, &iter);
6788 iter.err = nft_set_catchall_flush(ctx, set);
6793 static int nf_tables_delsetelem(struct sk_buff *skb,
6794 const struct nfnl_info *info,
6795 const struct nlattr * const nla[])
6797 struct netlink_ext_ack *extack = info->extack;
6798 u8 genmask = nft_genmask_next(info->net);
6799 u8 family = info->nfmsg->nfgen_family;
6800 struct net *net = info->net;
6801 const struct nlattr *attr;
6802 struct nft_table *table;
6803 struct nft_set *set;
6807 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
6808 genmask, NETLINK_CB(skb).portid);
6809 if (IS_ERR(table)) {
6810 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
6811 return PTR_ERR(table);
6814 set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
6816 return PTR_ERR(set);
6817 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
6820 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
6822 if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
6823 return nft_set_flush(&ctx, set, genmask);
6825 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
6826 err = nft_del_setelem(&ctx, set, attr);
6827 if (err == -ENOENT &&
6828 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSETELEM)
6832 NL_SET_BAD_ATTR(extack, attr);
6839 void nft_set_gc_batch_release(struct rcu_head *rcu)
6841 struct nft_set_gc_batch *gcb;
6844 gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu);
6845 for (i = 0; i < gcb->head.cnt; i++)
6846 nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true);
6850 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
6853 struct nft_set_gc_batch *gcb;
6855 gcb = kzalloc(sizeof(*gcb), gfp);
6858 gcb->head.set = set;
6867 * nft_register_obj- register nf_tables stateful object type
6868 * @obj_type: object type
6870 * Registers the object type for use with nf_tables. Returns zero on
6871 * success or a negative errno code otherwise.
6873 int nft_register_obj(struct nft_object_type *obj_type)
6875 if (obj_type->type == NFT_OBJECT_UNSPEC)
6878 nfnl_lock(NFNL_SUBSYS_NFTABLES);
6879 list_add_rcu(&obj_type->list, &nf_tables_objects);
6880 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
6883 EXPORT_SYMBOL_GPL(nft_register_obj);
6886 * nft_unregister_obj - unregister nf_tables object type
6887 * @obj_type: object type
6889 * Unregisters the object type for use with nf_tables.
6891 void nft_unregister_obj(struct nft_object_type *obj_type)
6893 nfnl_lock(NFNL_SUBSYS_NFTABLES);
6894 list_del_rcu(&obj_type->list);
6895 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
6897 EXPORT_SYMBOL_GPL(nft_unregister_obj);
6899 struct nft_object *nft_obj_lookup(const struct net *net,
6900 const struct nft_table *table,
6901 const struct nlattr *nla, u32 objtype,
6904 struct nft_object_hash_key k = { .table = table };
6905 char search[NFT_OBJ_MAXNAMELEN];
6906 struct rhlist_head *tmp, *list;
6907 struct nft_object *obj;
6909 nla_strscpy(search, nla, sizeof(search));
6912 WARN_ON_ONCE(!rcu_read_lock_held() &&
6913 !lockdep_commit_lock_is_held(net));
6916 list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params);
6920 rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) {
6921 if (objtype == obj->ops->type->type &&
6922 nft_active_genmask(obj, genmask)) {
6929 return ERR_PTR(-ENOENT);
6931 EXPORT_SYMBOL_GPL(nft_obj_lookup);
6933 static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table,
6934 const struct nlattr *nla,
6935 u32 objtype, u8 genmask)
6937 struct nft_object *obj;
6939 list_for_each_entry(obj, &table->objects, list) {
6940 if (be64_to_cpu(nla_get_be64(nla)) == obj->handle &&
6941 objtype == obj->ops->type->type &&
6942 nft_active_genmask(obj, genmask))
6945 return ERR_PTR(-ENOENT);
6948 static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = {
6949 [NFTA_OBJ_TABLE] = { .type = NLA_STRING,
6950 .len = NFT_TABLE_MAXNAMELEN - 1 },
6951 [NFTA_OBJ_NAME] = { .type = NLA_STRING,
6952 .len = NFT_OBJ_MAXNAMELEN - 1 },
6953 [NFTA_OBJ_TYPE] = { .type = NLA_U32 },
6954 [NFTA_OBJ_DATA] = { .type = NLA_NESTED },
6955 [NFTA_OBJ_HANDLE] = { .type = NLA_U64},
6956 [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY,
6957 .len = NFT_USERDATA_MAXLEN },
6960 static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
6961 const struct nft_object_type *type,
6962 const struct nlattr *attr)
6965 const struct nft_object_ops *ops;
6966 struct nft_object *obj;
6969 tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL);
6974 err = nla_parse_nested_deprecated(tb, type->maxattr, attr,
6975 type->policy, NULL);
6979 memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1));
6982 if (type->select_ops) {
6983 ops = type->select_ops(ctx, (const struct nlattr * const *)tb);
6993 obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL_ACCOUNT);
6997 err = ops->init(ctx, (const struct nlattr * const *)tb, obj);
7010 return ERR_PTR(err);
7013 static int nft_object_dump(struct sk_buff *skb, unsigned int attr,
7014 struct nft_object *obj, bool reset)
7016 struct nlattr *nest;
7018 nest = nla_nest_start_noflag(skb, attr);
7020 goto nla_put_failure;
7021 if (obj->ops->dump(skb, obj, reset) < 0)
7022 goto nla_put_failure;
7023 nla_nest_end(skb, nest);
7030 static const struct nft_object_type *__nft_obj_type_get(u32 objtype)
7032 const struct nft_object_type *type;
7034 list_for_each_entry(type, &nf_tables_objects, list) {
7035 if (objtype == type->type)
7041 static const struct nft_object_type *
7042 nft_obj_type_get(struct net *net, u32 objtype)
7044 const struct nft_object_type *type;
7046 type = __nft_obj_type_get(objtype);
7047 if (type != NULL && try_module_get(type->owner))
7050 lockdep_nfnl_nft_mutex_not_held();
7051 #ifdef CONFIG_MODULES
7053 if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN)
7054 return ERR_PTR(-EAGAIN);
7057 return ERR_PTR(-ENOENT);
7060 static int nf_tables_updobj(const struct nft_ctx *ctx,
7061 const struct nft_object_type *type,
7062 const struct nlattr *attr,
7063 struct nft_object *obj)
7065 struct nft_object *newobj;
7066 struct nft_trans *trans;
7069 if (!try_module_get(type->owner))
7072 trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ,
7073 sizeof(struct nft_trans_obj));
7077 newobj = nft_obj_init(ctx, type, attr);
7078 if (IS_ERR(newobj)) {
7079 err = PTR_ERR(newobj);
7080 goto err_free_trans;
7083 nft_trans_obj(trans) = obj;
7084 nft_trans_obj_update(trans) = true;
7085 nft_trans_obj_newobj(trans) = newobj;
7086 nft_trans_commit_list_add_tail(ctx->net, trans);
7093 module_put(type->owner);
7097 static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info,
7098 const struct nlattr * const nla[])
7100 struct netlink_ext_ack *extack = info->extack;
7101 u8 genmask = nft_genmask_next(info->net);
7102 u8 family = info->nfmsg->nfgen_family;
7103 const struct nft_object_type *type;
7104 struct net *net = info->net;
7105 struct nft_table *table;
7106 struct nft_object *obj;
7111 if (!nla[NFTA_OBJ_TYPE] ||
7112 !nla[NFTA_OBJ_NAME] ||
7113 !nla[NFTA_OBJ_DATA])
7116 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask,
7117 NETLINK_CB(skb).portid);
7118 if (IS_ERR(table)) {
7119 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
7120 return PTR_ERR(table);
7123 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7124 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
7127 if (err != -ENOENT) {
7128 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
7132 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
7133 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
7136 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
7139 type = __nft_obj_type_get(objtype);
7140 if (WARN_ON_ONCE(!type))
7143 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7145 return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj);
7148 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7150 type = nft_obj_type_get(net, objtype);
7152 return PTR_ERR(type);
7154 obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]);
7159 obj->key.table = table;
7160 obj->handle = nf_tables_alloc_handle(table);
7162 obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL_ACCOUNT);
7163 if (!obj->key.name) {
7168 if (nla[NFTA_OBJ_USERDATA]) {
7169 obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL_ACCOUNT);
7170 if (obj->udata == NULL)
7173 obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]);
7176 err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj);
7180 err = rhltable_insert(&nft_objname_ht, &obj->rhlhead,
7181 nft_objname_ht_params);
7185 list_add_tail_rcu(&obj->list, &table->objects);
7189 /* queued in transaction log */
7190 INIT_LIST_HEAD(&obj->list);
7195 kfree(obj->key.name);
7197 if (obj->ops->destroy)
7198 obj->ops->destroy(&ctx, obj);
7201 module_put(type->owner);
7205 static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net,
7206 u32 portid, u32 seq, int event, u32 flags,
7207 int family, const struct nft_table *table,
7208 struct nft_object *obj, bool reset)
7210 struct nlmsghdr *nlh;
7212 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
7213 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
7214 NFNETLINK_V0, nft_base_seq(net));
7216 goto nla_put_failure;
7218 if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) ||
7219 nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) ||
7220 nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle),
7222 goto nla_put_failure;
7224 if (event == NFT_MSG_DELOBJ) {
7225 nlmsg_end(skb, nlh);
7229 if (nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) ||
7230 nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) ||
7231 nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset))
7232 goto nla_put_failure;
7235 nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata))
7236 goto nla_put_failure;
7238 nlmsg_end(skb, nlh);
7242 nlmsg_trim(skb, nlh);
7246 struct nft_obj_filter {
7251 static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)
7253 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
7254 const struct nft_table *table;
7255 unsigned int idx = 0, s_idx = cb->args[0];
7256 struct nft_obj_filter *filter = cb->data;
7257 struct net *net = sock_net(skb->sk);
7258 int family = nfmsg->nfgen_family;
7259 struct nftables_pernet *nft_net;
7260 struct nft_object *obj;
7263 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
7267 nft_net = nft_pernet(net);
7268 cb->seq = READ_ONCE(nft_net->base_seq);
7270 list_for_each_entry_rcu(table, &nft_net->tables, list) {
7271 if (family != NFPROTO_UNSPEC && family != table->family)
7274 list_for_each_entry_rcu(obj, &table->objects, list) {
7275 if (!nft_is_active(net, obj))
7280 memset(&cb->args[1], 0,
7281 sizeof(cb->args) - sizeof(cb->args[0]));
7282 if (filter && filter->table &&
7283 strcmp(filter->table, table->name))
7286 filter->type != NFT_OBJECT_UNSPEC &&
7287 obj->ops->type->type != filter->type)
7290 char *buf = kasprintf(GFP_ATOMIC,
7295 audit_log_nfcfg(buf,
7298 AUDIT_NFT_OP_OBJ_RESET,
7303 if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid,
7306 NLM_F_MULTI | NLM_F_APPEND,
7307 table->family, table,
7311 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
7323 static int nf_tables_dump_obj_start(struct netlink_callback *cb)
7325 const struct nlattr * const *nla = cb->data;
7326 struct nft_obj_filter *filter = NULL;
7328 if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) {
7329 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
7333 if (nla[NFTA_OBJ_TABLE]) {
7334 filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC);
7335 if (!filter->table) {
7341 if (nla[NFTA_OBJ_TYPE])
7342 filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7349 static int nf_tables_dump_obj_done(struct netlink_callback *cb)
7351 struct nft_obj_filter *filter = cb->data;
7354 kfree(filter->table);
7361 /* called with rcu_read_lock held */
7362 static int nf_tables_getobj(struct sk_buff *skb, const struct nfnl_info *info,
7363 const struct nlattr * const nla[])
7365 struct netlink_ext_ack *extack = info->extack;
7366 u8 genmask = nft_genmask_cur(info->net);
7367 u8 family = info->nfmsg->nfgen_family;
7368 const struct nft_table *table;
7369 struct net *net = info->net;
7370 struct nft_object *obj;
7371 struct sk_buff *skb2;
7376 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
7377 struct netlink_dump_control c = {
7378 .start = nf_tables_dump_obj_start,
7379 .dump = nf_tables_dump_obj,
7380 .done = nf_tables_dump_obj_done,
7381 .module = THIS_MODULE,
7382 .data = (void *)nla,
7385 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
7388 if (!nla[NFTA_OBJ_NAME] ||
7389 !nla[NFTA_OBJ_TYPE])
7392 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, 0);
7393 if (IS_ERR(table)) {
7394 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
7395 return PTR_ERR(table);
7398 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7399 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
7401 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
7402 return PTR_ERR(obj);
7405 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
7409 if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
7413 const struct nftables_pernet *nft_net;
7416 nft_net = nft_pernet(net);
7417 buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, nft_net->base_seq);
7419 audit_log_nfcfg(buf,
7422 AUDIT_NFT_OP_OBJ_RESET,
7427 err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid,
7428 info->nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0,
7429 family, table, obj, reset);
7431 goto err_fill_obj_info;
7433 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
7440 static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj)
7442 if (obj->ops->destroy)
7443 obj->ops->destroy(ctx, obj);
7445 module_put(obj->ops->type->owner);
7446 kfree(obj->key.name);
7451 static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info,
7452 const struct nlattr * const nla[])
7454 struct netlink_ext_ack *extack = info->extack;
7455 u8 genmask = nft_genmask_next(info->net);
7456 u8 family = info->nfmsg->nfgen_family;
7457 struct net *net = info->net;
7458 const struct nlattr *attr;
7459 struct nft_table *table;
7460 struct nft_object *obj;
7464 if (!nla[NFTA_OBJ_TYPE] ||
7465 (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE]))
7468 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask,
7469 NETLINK_CB(skb).portid);
7470 if (IS_ERR(table)) {
7471 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
7472 return PTR_ERR(table);
7475 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7476 if (nla[NFTA_OBJ_HANDLE]) {
7477 attr = nla[NFTA_OBJ_HANDLE];
7478 obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask);
7480 attr = nla[NFTA_OBJ_NAME];
7481 obj = nft_obj_lookup(net, table, attr, objtype, genmask);
7485 if (PTR_ERR(obj) == -ENOENT &&
7486 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYOBJ)
7489 NL_SET_BAD_ATTR(extack, attr);
7490 return PTR_ERR(obj);
7493 NL_SET_BAD_ATTR(extack, attr);
7497 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7499 return nft_delobj(&ctx, obj);
7502 void nft_obj_notify(struct net *net, const struct nft_table *table,
7503 struct nft_object *obj, u32 portid, u32 seq, int event,
7504 u16 flags, int family, int report, gfp_t gfp)
7506 struct nftables_pernet *nft_net = nft_pernet(net);
7507 struct sk_buff *skb;
7509 char *buf = kasprintf(gfp, "%s:%u",
7510 table->name, nft_net->base_seq);
7512 audit_log_nfcfg(buf,
7515 event == NFT_MSG_NEWOBJ ?
7516 AUDIT_NFT_OP_OBJ_REGISTER :
7517 AUDIT_NFT_OP_OBJ_UNREGISTER,
7522 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
7525 skb = nlmsg_new(NLMSG_GOODSIZE, gfp);
7529 err = nf_tables_fill_obj_info(skb, net, portid, seq, event,
7530 flags & (NLM_F_CREATE | NLM_F_EXCL),
7531 family, table, obj, false);
7537 nft_notify_enqueue(skb, report, &nft_net->notify_list);
7540 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
7542 EXPORT_SYMBOL_GPL(nft_obj_notify);
7544 static void nf_tables_obj_notify(const struct nft_ctx *ctx,
7545 struct nft_object *obj, int event)
7547 nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event,
7548 ctx->flags, ctx->family, ctx->report, GFP_KERNEL);
7554 void nft_register_flowtable_type(struct nf_flowtable_type *type)
7556 nfnl_lock(NFNL_SUBSYS_NFTABLES);
7557 list_add_tail_rcu(&type->list, &nf_tables_flowtables);
7558 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
7560 EXPORT_SYMBOL_GPL(nft_register_flowtable_type);
7562 void nft_unregister_flowtable_type(struct nf_flowtable_type *type)
7564 nfnl_lock(NFNL_SUBSYS_NFTABLES);
7565 list_del_rcu(&type->list);
7566 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
7568 EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type);
7570 static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = {
7571 [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING,
7572 .len = NFT_NAME_MAXLEN - 1 },
7573 [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING,
7574 .len = NFT_NAME_MAXLEN - 1 },
7575 [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED },
7576 [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 },
7577 [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 },
7580 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
7581 const struct nlattr *nla, u8 genmask)
7583 struct nft_flowtable *flowtable;
7585 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
7586 if (!nla_strcmp(nla, flowtable->name) &&
7587 nft_active_genmask(flowtable, genmask))
7590 return ERR_PTR(-ENOENT);
7592 EXPORT_SYMBOL_GPL(nft_flowtable_lookup);
7594 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
7595 struct nft_flowtable *flowtable,
7596 enum nft_trans_phase phase)
7599 case NFT_TRANS_PREPARE:
7600 case NFT_TRANS_ABORT:
7601 case NFT_TRANS_RELEASE:
7608 EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable);
7610 static struct nft_flowtable *
7611 nft_flowtable_lookup_byhandle(const struct nft_table *table,
7612 const struct nlattr *nla, u8 genmask)
7614 struct nft_flowtable *flowtable;
7616 list_for_each_entry(flowtable, &table->flowtables, list) {
7617 if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle &&
7618 nft_active_genmask(flowtable, genmask))
7621 return ERR_PTR(-ENOENT);
7624 struct nft_flowtable_hook {
7627 struct list_head list;
7630 static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = {
7631 [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 },
7632 [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 },
7633 [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED },
7636 static int nft_flowtable_parse_hook(const struct nft_ctx *ctx,
7637 const struct nlattr *attr,
7638 struct nft_flowtable_hook *flowtable_hook,
7639 struct nft_flowtable *flowtable,
7640 struct netlink_ext_ack *extack, bool add)
7642 struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1];
7643 struct nft_hook *hook;
7644 int hooknum, priority;
7647 INIT_LIST_HEAD(&flowtable_hook->list);
7649 err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, attr,
7650 nft_flowtable_hook_policy, NULL);
7655 if (!tb[NFTA_FLOWTABLE_HOOK_NUM] ||
7656 !tb[NFTA_FLOWTABLE_HOOK_PRIORITY])
7659 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
7660 if (hooknum != NF_NETDEV_INGRESS)
7663 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
7665 flowtable_hook->priority = priority;
7666 flowtable_hook->num = hooknum;
7668 if (tb[NFTA_FLOWTABLE_HOOK_NUM]) {
7669 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
7670 if (hooknum != flowtable->hooknum)
7674 if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) {
7675 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
7676 if (priority != flowtable->data.priority)
7680 flowtable_hook->priority = flowtable->data.priority;
7681 flowtable_hook->num = flowtable->hooknum;
7684 if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) {
7685 err = nf_tables_parse_netdev_hooks(ctx->net,
7686 tb[NFTA_FLOWTABLE_HOOK_DEVS],
7687 &flowtable_hook->list,
7693 list_for_each_entry(hook, &flowtable_hook->list, list) {
7694 hook->ops.pf = NFPROTO_NETDEV;
7695 hook->ops.hooknum = flowtable_hook->num;
7696 hook->ops.priority = flowtable_hook->priority;
7697 hook->ops.priv = &flowtable->data;
7698 hook->ops.hook = flowtable->data.type->hook;
7704 static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family)
7706 const struct nf_flowtable_type *type;
7708 list_for_each_entry(type, &nf_tables_flowtables, list) {
7709 if (family == type->family)
7715 static const struct nf_flowtable_type *
7716 nft_flowtable_type_get(struct net *net, u8 family)
7718 const struct nf_flowtable_type *type;
7720 type = __nft_flowtable_type_get(family);
7721 if (type != NULL && try_module_get(type->owner))
7724 lockdep_nfnl_nft_mutex_not_held();
7725 #ifdef CONFIG_MODULES
7727 if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN)
7728 return ERR_PTR(-EAGAIN);
7731 return ERR_PTR(-ENOENT);
7734 /* Only called from error and netdev event paths. */
7735 static void nft_unregister_flowtable_hook(struct net *net,
7736 struct nft_flowtable *flowtable,
7737 struct nft_hook *hook)
7739 nf_unregister_net_hook(net, &hook->ops);
7740 flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
7744 static void __nft_unregister_flowtable_net_hooks(struct net *net,
7745 struct list_head *hook_list,
7746 bool release_netdev)
7748 struct nft_hook *hook, *next;
7750 list_for_each_entry_safe(hook, next, hook_list, list) {
7751 nf_unregister_net_hook(net, &hook->ops);
7752 if (release_netdev) {
7753 list_del(&hook->list);
7754 kfree_rcu(hook, rcu);
7759 static void nft_unregister_flowtable_net_hooks(struct net *net,
7760 struct list_head *hook_list)
7762 __nft_unregister_flowtable_net_hooks(net, hook_list, false);
7765 static int nft_register_flowtable_net_hooks(struct net *net,
7766 struct nft_table *table,
7767 struct list_head *hook_list,
7768 struct nft_flowtable *flowtable)
7770 struct nft_hook *hook, *hook2, *next;
7771 struct nft_flowtable *ft;
7774 list_for_each_entry(hook, hook_list, list) {
7775 list_for_each_entry(ft, &table->flowtables, list) {
7776 if (!nft_is_active_next(net, ft))
7779 list_for_each_entry(hook2, &ft->hook_list, list) {
7780 if (hook->ops.dev == hook2->ops.dev &&
7781 hook->ops.pf == hook2->ops.pf) {
7783 goto err_unregister_net_hooks;
7788 err = flowtable->data.type->setup(&flowtable->data,
7792 goto err_unregister_net_hooks;
7794 err = nf_register_net_hook(net, &hook->ops);
7796 flowtable->data.type->setup(&flowtable->data,
7799 goto err_unregister_net_hooks;
7807 err_unregister_net_hooks:
7808 list_for_each_entry_safe(hook, next, hook_list, list) {
7812 nft_unregister_flowtable_hook(net, flowtable, hook);
7813 list_del_rcu(&hook->list);
7814 kfree_rcu(hook, rcu);
7820 static void nft_hooks_destroy(struct list_head *hook_list)
7822 struct nft_hook *hook, *next;
7824 list_for_each_entry_safe(hook, next, hook_list, list) {
7825 list_del_rcu(&hook->list);
7826 kfree_rcu(hook, rcu);
7830 static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh,
7831 struct nft_flowtable *flowtable,
7832 struct netlink_ext_ack *extack)
7834 const struct nlattr * const *nla = ctx->nla;
7835 struct nft_flowtable_hook flowtable_hook;
7836 struct nft_hook *hook, *next;
7837 struct nft_trans *trans;
7838 bool unregister = false;
7842 err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK],
7843 &flowtable_hook, flowtable, extack, false);
7847 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
7848 if (nft_hook_list_find(&flowtable->hook_list, hook)) {
7849 list_del(&hook->list);
7854 if (nla[NFTA_FLOWTABLE_FLAGS]) {
7855 flags = ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
7856 if (flags & ~NFT_FLOWTABLE_MASK) {
7858 goto err_flowtable_update_hook;
7860 if ((flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD) ^
7861 (flags & NFT_FLOWTABLE_HW_OFFLOAD)) {
7863 goto err_flowtable_update_hook;
7866 flags = flowtable->data.flags;
7869 err = nft_register_flowtable_net_hooks(ctx->net, ctx->table,
7870 &flowtable_hook.list, flowtable);
7872 goto err_flowtable_update_hook;
7874 trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE,
7875 sizeof(struct nft_trans_flowtable));
7879 goto err_flowtable_update_hook;
7882 nft_trans_flowtable_flags(trans) = flags;
7883 nft_trans_flowtable(trans) = flowtable;
7884 nft_trans_flowtable_update(trans) = true;
7885 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
7886 list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans));
7888 nft_trans_commit_list_add_tail(ctx->net, trans);
7892 err_flowtable_update_hook:
7893 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
7895 nft_unregister_flowtable_hook(ctx->net, flowtable, hook);
7896 list_del_rcu(&hook->list);
7897 kfree_rcu(hook, rcu);
7904 static int nf_tables_newflowtable(struct sk_buff *skb,
7905 const struct nfnl_info *info,
7906 const struct nlattr * const nla[])
7908 struct netlink_ext_ack *extack = info->extack;
7909 struct nft_flowtable_hook flowtable_hook;
7910 u8 genmask = nft_genmask_next(info->net);
7911 u8 family = info->nfmsg->nfgen_family;
7912 const struct nf_flowtable_type *type;
7913 struct nft_flowtable *flowtable;
7914 struct nft_hook *hook, *next;
7915 struct net *net = info->net;
7916 struct nft_table *table;
7920 if (!nla[NFTA_FLOWTABLE_TABLE] ||
7921 !nla[NFTA_FLOWTABLE_NAME] ||
7922 !nla[NFTA_FLOWTABLE_HOOK])
7925 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
7926 genmask, NETLINK_CB(skb).portid);
7927 if (IS_ERR(table)) {
7928 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
7929 return PTR_ERR(table);
7932 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
7934 if (IS_ERR(flowtable)) {
7935 err = PTR_ERR(flowtable);
7936 if (err != -ENOENT) {
7937 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
7941 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
7942 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
7946 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7948 return nft_flowtable_update(&ctx, info->nlh, flowtable, extack);
7951 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7953 flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL_ACCOUNT);
7957 flowtable->table = table;
7958 flowtable->handle = nf_tables_alloc_handle(table);
7959 INIT_LIST_HEAD(&flowtable->hook_list);
7961 flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL_ACCOUNT);
7962 if (!flowtable->name) {
7967 type = nft_flowtable_type_get(net, family);
7969 err = PTR_ERR(type);
7973 if (nla[NFTA_FLOWTABLE_FLAGS]) {
7974 flowtable->data.flags =
7975 ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
7976 if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) {
7982 write_pnet(&flowtable->data.net, net);
7983 flowtable->data.type = type;
7984 err = type->init(&flowtable->data);
7988 err = nft_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK],
7989 &flowtable_hook, flowtable, extack, true);
7993 list_splice(&flowtable_hook.list, &flowtable->hook_list);
7994 flowtable->data.priority = flowtable_hook.priority;
7995 flowtable->hooknum = flowtable_hook.num;
7997 err = nft_register_flowtable_net_hooks(ctx.net, table,
7998 &flowtable->hook_list,
8001 nft_hooks_destroy(&flowtable->hook_list);
8005 err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable);
8009 list_add_tail_rcu(&flowtable->list, &table->flowtables);
8014 list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
8015 nft_unregister_flowtable_hook(net, flowtable, hook);
8016 list_del_rcu(&hook->list);
8017 kfree_rcu(hook, rcu);
8020 flowtable->data.type->free(&flowtable->data);
8022 module_put(type->owner);
8024 kfree(flowtable->name);
8030 static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook)
8032 struct nft_hook *this, *next;
8034 list_for_each_entry_safe(this, next, &flowtable_hook->list, list) {
8035 list_del(&this->list);
8040 static int nft_delflowtable_hook(struct nft_ctx *ctx,
8041 struct nft_flowtable *flowtable,
8042 struct netlink_ext_ack *extack)
8044 const struct nlattr * const *nla = ctx->nla;
8045 struct nft_flowtable_hook flowtable_hook;
8046 LIST_HEAD(flowtable_del_list);
8047 struct nft_hook *this, *hook;
8048 struct nft_trans *trans;
8051 err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK],
8052 &flowtable_hook, flowtable, extack, false);
8056 list_for_each_entry(this, &flowtable_hook.list, list) {
8057 hook = nft_hook_list_find(&flowtable->hook_list, this);
8060 goto err_flowtable_del_hook;
8062 list_move(&hook->list, &flowtable_del_list);
8065 trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE,
8066 sizeof(struct nft_trans_flowtable));
8069 goto err_flowtable_del_hook;
8072 nft_trans_flowtable(trans) = flowtable;
8073 nft_trans_flowtable_update(trans) = true;
8074 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
8075 list_splice(&flowtable_del_list, &nft_trans_flowtable_hooks(trans));
8076 nft_flowtable_hook_release(&flowtable_hook);
8078 nft_trans_commit_list_add_tail(ctx->net, trans);
8082 err_flowtable_del_hook:
8083 list_splice(&flowtable_del_list, &flowtable->hook_list);
8084 nft_flowtable_hook_release(&flowtable_hook);
8089 static int nf_tables_delflowtable(struct sk_buff *skb,
8090 const struct nfnl_info *info,
8091 const struct nlattr * const nla[])
8093 struct netlink_ext_ack *extack = info->extack;
8094 u8 genmask = nft_genmask_next(info->net);
8095 u8 family = info->nfmsg->nfgen_family;
8096 struct nft_flowtable *flowtable;
8097 struct net *net = info->net;
8098 const struct nlattr *attr;
8099 struct nft_table *table;
8102 if (!nla[NFTA_FLOWTABLE_TABLE] ||
8103 (!nla[NFTA_FLOWTABLE_NAME] &&
8104 !nla[NFTA_FLOWTABLE_HANDLE]))
8107 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
8108 genmask, NETLINK_CB(skb).portid);
8109 if (IS_ERR(table)) {
8110 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
8111 return PTR_ERR(table);
8114 if (nla[NFTA_FLOWTABLE_HANDLE]) {
8115 attr = nla[NFTA_FLOWTABLE_HANDLE];
8116 flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask);
8118 attr = nla[NFTA_FLOWTABLE_NAME];
8119 flowtable = nft_flowtable_lookup(table, attr, genmask);
8122 if (IS_ERR(flowtable)) {
8123 if (PTR_ERR(flowtable) == -ENOENT &&
8124 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYFLOWTABLE)
8127 NL_SET_BAD_ATTR(extack, attr);
8128 return PTR_ERR(flowtable);
8131 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
8133 if (nla[NFTA_FLOWTABLE_HOOK])
8134 return nft_delflowtable_hook(&ctx, flowtable, extack);
8136 if (flowtable->use > 0) {
8137 NL_SET_BAD_ATTR(extack, attr);
8141 return nft_delflowtable(&ctx, flowtable);
8144 static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,
8145 u32 portid, u32 seq, int event,
8146 u32 flags, int family,
8147 struct nft_flowtable *flowtable,
8148 struct list_head *hook_list)
8150 struct nlattr *nest, *nest_devs;
8151 struct nft_hook *hook;
8152 struct nlmsghdr *nlh;
8154 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
8155 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
8156 NFNETLINK_V0, nft_base_seq(net));
8158 goto nla_put_failure;
8160 if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) ||
8161 nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) ||
8162 nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle),
8163 NFTA_FLOWTABLE_PAD))
8164 goto nla_put_failure;
8166 if (event == NFT_MSG_DELFLOWTABLE && !hook_list) {
8167 nlmsg_end(skb, nlh);
8171 if (nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) ||
8172 nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags)))
8173 goto nla_put_failure;
8175 nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK);
8177 goto nla_put_failure;
8178 if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) ||
8179 nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority)))
8180 goto nla_put_failure;
8182 nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS);
8184 goto nla_put_failure;
8187 hook_list = &flowtable->hook_list;
8189 list_for_each_entry_rcu(hook, hook_list, list) {
8190 if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name))
8191 goto nla_put_failure;
8193 nla_nest_end(skb, nest_devs);
8194 nla_nest_end(skb, nest);
8196 nlmsg_end(skb, nlh);
8200 nlmsg_trim(skb, nlh);
8204 struct nft_flowtable_filter {
8208 static int nf_tables_dump_flowtable(struct sk_buff *skb,
8209 struct netlink_callback *cb)
8211 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
8212 struct nft_flowtable_filter *filter = cb->data;
8213 unsigned int idx = 0, s_idx = cb->args[0];
8214 struct net *net = sock_net(skb->sk);
8215 int family = nfmsg->nfgen_family;
8216 struct nft_flowtable *flowtable;
8217 struct nftables_pernet *nft_net;
8218 const struct nft_table *table;
8221 nft_net = nft_pernet(net);
8222 cb->seq = READ_ONCE(nft_net->base_seq);
8224 list_for_each_entry_rcu(table, &nft_net->tables, list) {
8225 if (family != NFPROTO_UNSPEC && family != table->family)
8228 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
8229 if (!nft_is_active(net, flowtable))
8234 memset(&cb->args[1], 0,
8235 sizeof(cb->args) - sizeof(cb->args[0]));
8236 if (filter && filter->table &&
8237 strcmp(filter->table, table->name))
8240 if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid,
8242 NFT_MSG_NEWFLOWTABLE,
8243 NLM_F_MULTI | NLM_F_APPEND,
8245 flowtable, NULL) < 0)
8248 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
8260 static int nf_tables_dump_flowtable_start(struct netlink_callback *cb)
8262 const struct nlattr * const *nla = cb->data;
8263 struct nft_flowtable_filter *filter = NULL;
8265 if (nla[NFTA_FLOWTABLE_TABLE]) {
8266 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
8270 filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE],
8272 if (!filter->table) {
8282 static int nf_tables_dump_flowtable_done(struct netlink_callback *cb)
8284 struct nft_flowtable_filter *filter = cb->data;
8289 kfree(filter->table);
8295 /* called with rcu_read_lock held */
8296 static int nf_tables_getflowtable(struct sk_buff *skb,
8297 const struct nfnl_info *info,
8298 const struct nlattr * const nla[])
8300 u8 genmask = nft_genmask_cur(info->net);
8301 u8 family = info->nfmsg->nfgen_family;
8302 struct nft_flowtable *flowtable;
8303 const struct nft_table *table;
8304 struct net *net = info->net;
8305 struct sk_buff *skb2;
8308 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
8309 struct netlink_dump_control c = {
8310 .start = nf_tables_dump_flowtable_start,
8311 .dump = nf_tables_dump_flowtable,
8312 .done = nf_tables_dump_flowtable_done,
8313 .module = THIS_MODULE,
8314 .data = (void *)nla,
8317 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
8320 if (!nla[NFTA_FLOWTABLE_NAME])
8323 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
8326 return PTR_ERR(table);
8328 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
8330 if (IS_ERR(flowtable))
8331 return PTR_ERR(flowtable);
8333 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
8337 err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid,
8338 info->nlh->nlmsg_seq,
8339 NFT_MSG_NEWFLOWTABLE, 0, family,
8342 goto err_fill_flowtable_info;
8344 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
8346 err_fill_flowtable_info:
8351 static void nf_tables_flowtable_notify(struct nft_ctx *ctx,
8352 struct nft_flowtable *flowtable,
8353 struct list_head *hook_list, int event)
8355 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
8356 struct sk_buff *skb;
8361 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
8364 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
8368 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
8369 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
8371 err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid,
8372 ctx->seq, event, flags,
8373 ctx->family, flowtable, hook_list);
8379 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
8382 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
8385 static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
8387 struct nft_hook *hook, *next;
8389 flowtable->data.type->free(&flowtable->data);
8390 list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
8391 flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
8393 list_del_rcu(&hook->list);
8396 kfree(flowtable->name);
8397 module_put(flowtable->data.type->owner);
8401 static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
8402 u32 portid, u32 seq)
8404 struct nftables_pernet *nft_net = nft_pernet(net);
8405 struct nlmsghdr *nlh;
8406 char buf[TASK_COMM_LEN];
8407 int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN);
8409 nlh = nfnl_msg_put(skb, portid, seq, event, 0, AF_UNSPEC,
8410 NFNETLINK_V0, nft_base_seq(net));
8412 goto nla_put_failure;
8414 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(nft_net->base_seq)) ||
8415 nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) ||
8416 nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current)))
8417 goto nla_put_failure;
8419 nlmsg_end(skb, nlh);
8423 nlmsg_trim(skb, nlh);
8427 static void nft_flowtable_event(unsigned long event, struct net_device *dev,
8428 struct nft_flowtable *flowtable)
8430 struct nft_hook *hook;
8432 list_for_each_entry(hook, &flowtable->hook_list, list) {
8433 if (hook->ops.dev != dev)
8436 /* flow_offload_netdev_event() cleans up entries for us. */
8437 nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook);
8438 list_del_rcu(&hook->list);
8439 kfree_rcu(hook, rcu);
8444 static int nf_tables_flowtable_event(struct notifier_block *this,
8445 unsigned long event, void *ptr)
8447 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
8448 struct nft_flowtable *flowtable;
8449 struct nftables_pernet *nft_net;
8450 struct nft_table *table;
8453 if (event != NETDEV_UNREGISTER)
8457 nft_net = nft_pernet(net);
8458 mutex_lock(&nft_net->commit_mutex);
8459 list_for_each_entry(table, &nft_net->tables, list) {
8460 list_for_each_entry(flowtable, &table->flowtables, list) {
8461 nft_flowtable_event(event, dev, flowtable);
8464 mutex_unlock(&nft_net->commit_mutex);
8469 static struct notifier_block nf_tables_flowtable_notifier = {
8470 .notifier_call = nf_tables_flowtable_event,
8473 static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb,
8476 struct nlmsghdr *nlh = nlmsg_hdr(skb);
8477 struct sk_buff *skb2;
8480 if (!nlmsg_report(nlh) &&
8481 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
8484 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
8488 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
8495 nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
8496 nlmsg_report(nlh), GFP_KERNEL);
8499 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
8503 static int nf_tables_getgen(struct sk_buff *skb, const struct nfnl_info *info,
8504 const struct nlattr * const nla[])
8506 struct sk_buff *skb2;
8509 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
8513 err = nf_tables_fill_gen_info(skb2, info->net, NETLINK_CB(skb).portid,
8514 info->nlh->nlmsg_seq);
8516 goto err_fill_gen_info;
8518 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
8525 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
8526 [NFT_MSG_NEWTABLE] = {
8527 .call = nf_tables_newtable,
8528 .type = NFNL_CB_BATCH,
8529 .attr_count = NFTA_TABLE_MAX,
8530 .policy = nft_table_policy,
8532 [NFT_MSG_GETTABLE] = {
8533 .call = nf_tables_gettable,
8534 .type = NFNL_CB_RCU,
8535 .attr_count = NFTA_TABLE_MAX,
8536 .policy = nft_table_policy,
8538 [NFT_MSG_DELTABLE] = {
8539 .call = nf_tables_deltable,
8540 .type = NFNL_CB_BATCH,
8541 .attr_count = NFTA_TABLE_MAX,
8542 .policy = nft_table_policy,
8544 [NFT_MSG_DESTROYTABLE] = {
8545 .call = nf_tables_deltable,
8546 .type = NFNL_CB_BATCH,
8547 .attr_count = NFTA_TABLE_MAX,
8548 .policy = nft_table_policy,
8550 [NFT_MSG_NEWCHAIN] = {
8551 .call = nf_tables_newchain,
8552 .type = NFNL_CB_BATCH,
8553 .attr_count = NFTA_CHAIN_MAX,
8554 .policy = nft_chain_policy,
8556 [NFT_MSG_GETCHAIN] = {
8557 .call = nf_tables_getchain,
8558 .type = NFNL_CB_RCU,
8559 .attr_count = NFTA_CHAIN_MAX,
8560 .policy = nft_chain_policy,
8562 [NFT_MSG_DELCHAIN] = {
8563 .call = nf_tables_delchain,
8564 .type = NFNL_CB_BATCH,
8565 .attr_count = NFTA_CHAIN_MAX,
8566 .policy = nft_chain_policy,
8568 [NFT_MSG_DESTROYCHAIN] = {
8569 .call = nf_tables_delchain,
8570 .type = NFNL_CB_BATCH,
8571 .attr_count = NFTA_CHAIN_MAX,
8572 .policy = nft_chain_policy,
8574 [NFT_MSG_NEWRULE] = {
8575 .call = nf_tables_newrule,
8576 .type = NFNL_CB_BATCH,
8577 .attr_count = NFTA_RULE_MAX,
8578 .policy = nft_rule_policy,
8580 [NFT_MSG_GETRULE] = {
8581 .call = nf_tables_getrule,
8582 .type = NFNL_CB_RCU,
8583 .attr_count = NFTA_RULE_MAX,
8584 .policy = nft_rule_policy,
8586 [NFT_MSG_GETRULE_RESET] = {
8587 .call = nf_tables_getrule,
8588 .type = NFNL_CB_RCU,
8589 .attr_count = NFTA_RULE_MAX,
8590 .policy = nft_rule_policy,
8592 [NFT_MSG_DELRULE] = {
8593 .call = nf_tables_delrule,
8594 .type = NFNL_CB_BATCH,
8595 .attr_count = NFTA_RULE_MAX,
8596 .policy = nft_rule_policy,
8598 [NFT_MSG_DESTROYRULE] = {
8599 .call = nf_tables_delrule,
8600 .type = NFNL_CB_BATCH,
8601 .attr_count = NFTA_RULE_MAX,
8602 .policy = nft_rule_policy,
8604 [NFT_MSG_NEWSET] = {
8605 .call = nf_tables_newset,
8606 .type = NFNL_CB_BATCH,
8607 .attr_count = NFTA_SET_MAX,
8608 .policy = nft_set_policy,
8610 [NFT_MSG_GETSET] = {
8611 .call = nf_tables_getset,
8612 .type = NFNL_CB_RCU,
8613 .attr_count = NFTA_SET_MAX,
8614 .policy = nft_set_policy,
8616 [NFT_MSG_DELSET] = {
8617 .call = nf_tables_delset,
8618 .type = NFNL_CB_BATCH,
8619 .attr_count = NFTA_SET_MAX,
8620 .policy = nft_set_policy,
8622 [NFT_MSG_DESTROYSET] = {
8623 .call = nf_tables_delset,
8624 .type = NFNL_CB_BATCH,
8625 .attr_count = NFTA_SET_MAX,
8626 .policy = nft_set_policy,
8628 [NFT_MSG_NEWSETELEM] = {
8629 .call = nf_tables_newsetelem,
8630 .type = NFNL_CB_BATCH,
8631 .attr_count = NFTA_SET_ELEM_LIST_MAX,
8632 .policy = nft_set_elem_list_policy,
8634 [NFT_MSG_GETSETELEM] = {
8635 .call = nf_tables_getsetelem,
8636 .type = NFNL_CB_RCU,
8637 .attr_count = NFTA_SET_ELEM_LIST_MAX,
8638 .policy = nft_set_elem_list_policy,
8640 [NFT_MSG_DELSETELEM] = {
8641 .call = nf_tables_delsetelem,
8642 .type = NFNL_CB_BATCH,
8643 .attr_count = NFTA_SET_ELEM_LIST_MAX,
8644 .policy = nft_set_elem_list_policy,
8646 [NFT_MSG_DESTROYSETELEM] = {
8647 .call = nf_tables_delsetelem,
8648 .type = NFNL_CB_BATCH,
8649 .attr_count = NFTA_SET_ELEM_LIST_MAX,
8650 .policy = nft_set_elem_list_policy,
8652 [NFT_MSG_GETGEN] = {
8653 .call = nf_tables_getgen,
8654 .type = NFNL_CB_RCU,
8656 [NFT_MSG_NEWOBJ] = {
8657 .call = nf_tables_newobj,
8658 .type = NFNL_CB_BATCH,
8659 .attr_count = NFTA_OBJ_MAX,
8660 .policy = nft_obj_policy,
8662 [NFT_MSG_GETOBJ] = {
8663 .call = nf_tables_getobj,
8664 .type = NFNL_CB_RCU,
8665 .attr_count = NFTA_OBJ_MAX,
8666 .policy = nft_obj_policy,
8668 [NFT_MSG_DELOBJ] = {
8669 .call = nf_tables_delobj,
8670 .type = NFNL_CB_BATCH,
8671 .attr_count = NFTA_OBJ_MAX,
8672 .policy = nft_obj_policy,
8674 [NFT_MSG_DESTROYOBJ] = {
8675 .call = nf_tables_delobj,
8676 .type = NFNL_CB_BATCH,
8677 .attr_count = NFTA_OBJ_MAX,
8678 .policy = nft_obj_policy,
8680 [NFT_MSG_GETOBJ_RESET] = {
8681 .call = nf_tables_getobj,
8682 .type = NFNL_CB_RCU,
8683 .attr_count = NFTA_OBJ_MAX,
8684 .policy = nft_obj_policy,
8686 [NFT_MSG_NEWFLOWTABLE] = {
8687 .call = nf_tables_newflowtable,
8688 .type = NFNL_CB_BATCH,
8689 .attr_count = NFTA_FLOWTABLE_MAX,
8690 .policy = nft_flowtable_policy,
8692 [NFT_MSG_GETFLOWTABLE] = {
8693 .call = nf_tables_getflowtable,
8694 .type = NFNL_CB_RCU,
8695 .attr_count = NFTA_FLOWTABLE_MAX,
8696 .policy = nft_flowtable_policy,
8698 [NFT_MSG_DELFLOWTABLE] = {
8699 .call = nf_tables_delflowtable,
8700 .type = NFNL_CB_BATCH,
8701 .attr_count = NFTA_FLOWTABLE_MAX,
8702 .policy = nft_flowtable_policy,
8704 [NFT_MSG_DESTROYFLOWTABLE] = {
8705 .call = nf_tables_delflowtable,
8706 .type = NFNL_CB_BATCH,
8707 .attr_count = NFTA_FLOWTABLE_MAX,
8708 .policy = nft_flowtable_policy,
8712 static int nf_tables_validate(struct net *net)
8714 struct nftables_pernet *nft_net = nft_pernet(net);
8715 struct nft_table *table;
8717 list_for_each_entry(table, &nft_net->tables, list) {
8718 switch (table->validate_state) {
8719 case NFT_VALIDATE_SKIP:
8721 case NFT_VALIDATE_NEED:
8722 nft_validate_state_update(table, NFT_VALIDATE_DO);
8724 case NFT_VALIDATE_DO:
8725 if (nft_table_validate(net, table) < 0)
8728 nft_validate_state_update(table, NFT_VALIDATE_SKIP);
8737 /* a drop policy has to be deferred until all rules have been activated,
8738 * otherwise a large ruleset that contains a drop-policy base chain will
8739 * cause all packets to get dropped until the full transaction has been
8742 * We defer the drop policy until the transaction has been finalized.
8744 static void nft_chain_commit_drop_policy(struct nft_trans *trans)
8746 struct nft_base_chain *basechain;
8748 if (nft_trans_chain_policy(trans) != NF_DROP)
8751 if (!nft_is_base_chain(trans->ctx.chain))
8754 basechain = nft_base_chain(trans->ctx.chain);
8755 basechain->policy = NF_DROP;
8758 static void nft_chain_commit_update(struct nft_trans *trans)
8760 struct nft_base_chain *basechain;
8762 if (nft_trans_chain_name(trans)) {
8763 rhltable_remove(&trans->ctx.table->chains_ht,
8764 &trans->ctx.chain->rhlhead,
8765 nft_chain_ht_params);
8766 swap(trans->ctx.chain->name, nft_trans_chain_name(trans));
8767 rhltable_insert_key(&trans->ctx.table->chains_ht,
8768 trans->ctx.chain->name,
8769 &trans->ctx.chain->rhlhead,
8770 nft_chain_ht_params);
8773 if (!nft_is_base_chain(trans->ctx.chain))
8776 nft_chain_stats_replace(trans);
8778 basechain = nft_base_chain(trans->ctx.chain);
8780 switch (nft_trans_chain_policy(trans)) {
8783 basechain->policy = nft_trans_chain_policy(trans);
8788 static void nft_obj_commit_update(struct nft_trans *trans)
8790 struct nft_object *newobj;
8791 struct nft_object *obj;
8793 obj = nft_trans_obj(trans);
8794 newobj = nft_trans_obj_newobj(trans);
8796 if (obj->ops->update)
8797 obj->ops->update(obj, newobj);
8799 nft_obj_destroy(&trans->ctx, newobj);
8802 static void nft_commit_release(struct nft_trans *trans)
8804 switch (trans->msg_type) {
8805 case NFT_MSG_DELTABLE:
8806 case NFT_MSG_DESTROYTABLE:
8807 nf_tables_table_destroy(&trans->ctx);
8809 case NFT_MSG_NEWCHAIN:
8810 free_percpu(nft_trans_chain_stats(trans));
8811 kfree(nft_trans_chain_name(trans));
8813 case NFT_MSG_DELCHAIN:
8814 case NFT_MSG_DESTROYCHAIN:
8815 nf_tables_chain_destroy(&trans->ctx);
8817 case NFT_MSG_DELRULE:
8818 case NFT_MSG_DESTROYRULE:
8819 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
8821 case NFT_MSG_DELSET:
8822 case NFT_MSG_DESTROYSET:
8823 nft_set_destroy(&trans->ctx, nft_trans_set(trans));
8825 case NFT_MSG_DELSETELEM:
8826 case NFT_MSG_DESTROYSETELEM:
8827 nf_tables_set_elem_destroy(&trans->ctx,
8828 nft_trans_elem_set(trans),
8829 nft_trans_elem(trans).priv);
8831 case NFT_MSG_DELOBJ:
8832 case NFT_MSG_DESTROYOBJ:
8833 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
8835 case NFT_MSG_DELFLOWTABLE:
8836 case NFT_MSG_DESTROYFLOWTABLE:
8837 if (nft_trans_flowtable_update(trans))
8838 nft_hooks_destroy(&nft_trans_flowtable_hooks(trans));
8840 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
8845 put_net(trans->ctx.net);
8850 static void nf_tables_trans_destroy_work(struct work_struct *w)
8852 struct nft_trans *trans, *next;
8855 spin_lock(&nf_tables_destroy_list_lock);
8856 list_splice_init(&nf_tables_destroy_list, &head);
8857 spin_unlock(&nf_tables_destroy_list_lock);
8859 if (list_empty(&head))
8864 list_for_each_entry_safe(trans, next, &head, list) {
8865 list_del(&trans->list);
8866 nft_commit_release(trans);
8870 void nf_tables_trans_destroy_flush_work(void)
8872 flush_work(&trans_destroy_work);
8874 EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work);
8876 static bool nft_expr_reduce(struct nft_regs_track *track,
8877 const struct nft_expr *expr)
8882 static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain)
8884 const struct nft_expr *expr, *last;
8885 struct nft_regs_track track = {};
8886 unsigned int size, data_size;
8887 void *data, *data_boundary;
8888 struct nft_rule_dp *prule;
8889 struct nft_rule *rule;
8891 /* already handled or inactive chain? */
8892 if (chain->blob_next || !nft_is_active_next(net, chain))
8896 list_for_each_entry(rule, &chain->rules, list) {
8897 if (nft_is_active_next(net, rule)) {
8898 data_size += sizeof(*prule) + rule->dlen;
8899 if (data_size > INT_MAX)
8904 chain->blob_next = nf_tables_chain_alloc_rules(chain, data_size);
8905 if (!chain->blob_next)
8908 data = (void *)chain->blob_next->data;
8909 data_boundary = data + data_size;
8912 list_for_each_entry(rule, &chain->rules, list) {
8913 if (!nft_is_active_next(net, rule))
8916 prule = (struct nft_rule_dp *)data;
8917 data += offsetof(struct nft_rule_dp, data);
8918 if (WARN_ON_ONCE(data > data_boundary))
8922 track.last = nft_expr_last(rule);
8923 nft_rule_for_each_expr(expr, last, rule) {
8926 if (nft_expr_reduce(&track, expr)) {
8931 if (WARN_ON_ONCE(data + expr->ops->size > data_boundary))
8934 memcpy(data + size, expr, expr->ops->size);
8935 size += expr->ops->size;
8937 if (WARN_ON_ONCE(size >= 1 << 12))
8940 prule->handle = rule->handle;
8946 chain->blob_next->size += (unsigned long)(data - (void *)prule);
8949 if (WARN_ON_ONCE(data > data_boundary))
8952 prule = (struct nft_rule_dp *)data;
8953 nft_last_rule(chain, prule);
8958 static void nf_tables_commit_chain_prepare_cancel(struct net *net)
8960 struct nftables_pernet *nft_net = nft_pernet(net);
8961 struct nft_trans *trans, *next;
8963 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
8964 struct nft_chain *chain = trans->ctx.chain;
8966 if (trans->msg_type == NFT_MSG_NEWRULE ||
8967 trans->msg_type == NFT_MSG_DELRULE) {
8968 kvfree(chain->blob_next);
8969 chain->blob_next = NULL;
8974 static void __nf_tables_commit_chain_free_rules(struct rcu_head *h)
8976 struct nft_rule_dp_last *l = container_of(h, struct nft_rule_dp_last, h);
8981 static void nf_tables_commit_chain_free_rules_old(struct nft_rule_blob *blob)
8983 struct nft_rule_dp_last *last;
8985 /* last rule trailer is after end marker */
8986 last = (void *)blob + sizeof(*blob) + blob->size;
8989 call_rcu(&last->h, __nf_tables_commit_chain_free_rules);
8992 static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain)
8994 struct nft_rule_blob *g0, *g1;
8997 next_genbit = nft_gencursor_next(net);
8999 g0 = rcu_dereference_protected(chain->blob_gen_0,
9000 lockdep_commit_lock_is_held(net));
9001 g1 = rcu_dereference_protected(chain->blob_gen_1,
9002 lockdep_commit_lock_is_held(net));
9004 /* No changes to this chain? */
9005 if (chain->blob_next == NULL) {
9006 /* chain had no change in last or next generation */
9010 * chain had no change in this generation; make sure next
9011 * one uses same rules as current generation.
9014 rcu_assign_pointer(chain->blob_gen_1, g0);
9015 nf_tables_commit_chain_free_rules_old(g1);
9017 rcu_assign_pointer(chain->blob_gen_0, g1);
9018 nf_tables_commit_chain_free_rules_old(g0);
9025 rcu_assign_pointer(chain->blob_gen_1, chain->blob_next);
9027 rcu_assign_pointer(chain->blob_gen_0, chain->blob_next);
9029 chain->blob_next = NULL;
9035 nf_tables_commit_chain_free_rules_old(g1);
9037 nf_tables_commit_chain_free_rules_old(g0);
9040 static void nft_obj_del(struct nft_object *obj)
9042 rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params);
9043 list_del_rcu(&obj->list);
9046 void nft_chain_del(struct nft_chain *chain)
9048 struct nft_table *table = chain->table;
9050 WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead,
9051 nft_chain_ht_params));
9052 list_del_rcu(&chain->list);
9055 static void nf_tables_module_autoload_cleanup(struct net *net)
9057 struct nftables_pernet *nft_net = nft_pernet(net);
9058 struct nft_module_request *req, *next;
9060 WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
9061 list_for_each_entry_safe(req, next, &nft_net->module_list, list) {
9062 WARN_ON_ONCE(!req->done);
9063 list_del(&req->list);
9068 static void nf_tables_commit_release(struct net *net)
9070 struct nftables_pernet *nft_net = nft_pernet(net);
9071 struct nft_trans *trans;
9073 /* all side effects have to be made visible.
9074 * For example, if a chain named 'foo' has been deleted, a
9075 * new transaction must not find it anymore.
9077 * Memory reclaim happens asynchronously from work queue
9078 * to prevent expensive synchronize_rcu() in commit phase.
9080 if (list_empty(&nft_net->commit_list)) {
9081 nf_tables_module_autoload_cleanup(net);
9082 mutex_unlock(&nft_net->commit_mutex);
9086 trans = list_last_entry(&nft_net->commit_list,
9087 struct nft_trans, list);
9088 get_net(trans->ctx.net);
9089 WARN_ON_ONCE(trans->put_net);
9091 trans->put_net = true;
9092 spin_lock(&nf_tables_destroy_list_lock);
9093 list_splice_tail_init(&nft_net->commit_list, &nf_tables_destroy_list);
9094 spin_unlock(&nf_tables_destroy_list_lock);
9096 nf_tables_module_autoload_cleanup(net);
9097 schedule_work(&trans_destroy_work);
9099 mutex_unlock(&nft_net->commit_mutex);
9102 static void nft_commit_notify(struct net *net, u32 portid)
9104 struct nftables_pernet *nft_net = nft_pernet(net);
9105 struct sk_buff *batch_skb = NULL, *nskb, *skb;
9106 unsigned char *data;
9109 list_for_each_entry_safe(skb, nskb, &nft_net->notify_list, list) {
9113 len = NLMSG_GOODSIZE - skb->len;
9114 list_del(&skb->list);
9118 if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) {
9119 data = skb_put(batch_skb, skb->len);
9120 memcpy(data, skb->data, skb->len);
9121 list_del(&skb->list);
9125 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
9126 NFT_CB(batch_skb).report, GFP_KERNEL);
9131 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
9132 NFT_CB(batch_skb).report, GFP_KERNEL);
9135 WARN_ON_ONCE(!list_empty(&nft_net->notify_list));
9138 static int nf_tables_commit_audit_alloc(struct list_head *adl,
9139 struct nft_table *table)
9141 struct nft_audit_data *adp;
9143 list_for_each_entry(adp, adl, list) {
9144 if (adp->table == table)
9147 adp = kzalloc(sizeof(*adp), GFP_KERNEL);
9151 list_add(&adp->list, adl);
9155 static void nf_tables_commit_audit_free(struct list_head *adl)
9157 struct nft_audit_data *adp, *adn;
9159 list_for_each_entry_safe(adp, adn, adl, list) {
9160 list_del(&adp->list);
9165 static void nf_tables_commit_audit_collect(struct list_head *adl,
9166 struct nft_table *table, u32 op)
9168 struct nft_audit_data *adp;
9170 list_for_each_entry(adp, adl, list) {
9171 if (adp->table == table)
9174 WARN_ONCE(1, "table=%s not expected in commit list", table->name);
9178 if (!adp->op || adp->op > op)
9182 #define AUNFTABLENAMELEN (NFT_TABLE_MAXNAMELEN + 22)
9184 static void nf_tables_commit_audit_log(struct list_head *adl, u32 generation)
9186 struct nft_audit_data *adp, *adn;
9187 char aubuf[AUNFTABLENAMELEN];
9189 list_for_each_entry_safe(adp, adn, adl, list) {
9190 snprintf(aubuf, AUNFTABLENAMELEN, "%s:%u", adp->table->name,
9192 audit_log_nfcfg(aubuf, adp->table->family, adp->entries,
9193 nft2audit_op[adp->op], GFP_KERNEL);
9194 list_del(&adp->list);
9199 static int nf_tables_commit(struct net *net, struct sk_buff *skb)
9201 struct nftables_pernet *nft_net = nft_pernet(net);
9202 struct nft_trans *trans, *next;
9203 struct nft_trans_elem *te;
9204 struct nft_chain *chain;
9205 struct nft_table *table;
9206 unsigned int base_seq;
9210 if (list_empty(&nft_net->commit_list)) {
9211 mutex_unlock(&nft_net->commit_mutex);
9215 /* 0. Validate ruleset, otherwise roll back for error reporting. */
9216 if (nf_tables_validate(net) < 0)
9219 err = nft_flow_rule_offload_commit(net);
9223 /* 1. Allocate space for next generation rules_gen_X[] */
9224 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
9227 ret = nf_tables_commit_audit_alloc(&adl, trans->ctx.table);
9229 nf_tables_commit_chain_prepare_cancel(net);
9230 nf_tables_commit_audit_free(&adl);
9233 if (trans->msg_type == NFT_MSG_NEWRULE ||
9234 trans->msg_type == NFT_MSG_DELRULE) {
9235 chain = trans->ctx.chain;
9237 ret = nf_tables_commit_chain_prepare(net, chain);
9239 nf_tables_commit_chain_prepare_cancel(net);
9240 nf_tables_commit_audit_free(&adl);
9246 /* step 2. Make rules_gen_X visible to packet path */
9247 list_for_each_entry(table, &nft_net->tables, list) {
9248 list_for_each_entry(chain, &table->chains, list)
9249 nf_tables_commit_chain(net, chain);
9253 * Bump generation counter, invalidate any dump in progress.
9254 * Cannot fail after this point.
9256 base_seq = READ_ONCE(nft_net->base_seq);
9257 while (++base_seq == 0)
9260 WRITE_ONCE(nft_net->base_seq, base_seq);
9262 /* step 3. Start new generation, rules_gen_X now in use. */
9263 net->nft.gencursor = nft_gencursor_next(net);
9265 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
9266 nf_tables_commit_audit_collect(&adl, trans->ctx.table,
9268 switch (trans->msg_type) {
9269 case NFT_MSG_NEWTABLE:
9270 if (nft_trans_table_update(trans)) {
9271 if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) {
9272 nft_trans_destroy(trans);
9275 if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT)
9276 nf_tables_table_disable(net, trans->ctx.table);
9278 trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE;
9280 nft_clear(net, trans->ctx.table);
9282 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
9283 nft_trans_destroy(trans);
9285 case NFT_MSG_DELTABLE:
9286 case NFT_MSG_DESTROYTABLE:
9287 list_del_rcu(&trans->ctx.table->list);
9288 nf_tables_table_notify(&trans->ctx, trans->msg_type);
9290 case NFT_MSG_NEWCHAIN:
9291 if (nft_trans_chain_update(trans)) {
9292 nft_chain_commit_update(trans);
9293 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN,
9294 &nft_trans_chain_hooks(trans));
9295 list_splice(&nft_trans_chain_hooks(trans),
9296 &nft_trans_basechain(trans)->hook_list);
9297 /* trans destroyed after rcu grace period */
9299 nft_chain_commit_drop_policy(trans);
9300 nft_clear(net, trans->ctx.chain);
9301 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN, NULL);
9302 nft_trans_destroy(trans);
9305 case NFT_MSG_DELCHAIN:
9306 case NFT_MSG_DESTROYCHAIN:
9307 nft_chain_del(trans->ctx.chain);
9308 nf_tables_chain_notify(&trans->ctx, trans->msg_type, NULL);
9309 nf_tables_unregister_hook(trans->ctx.net,
9313 case NFT_MSG_NEWRULE:
9314 nft_clear(trans->ctx.net, nft_trans_rule(trans));
9315 nf_tables_rule_notify(&trans->ctx,
9316 nft_trans_rule(trans),
9318 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
9319 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
9321 nft_trans_destroy(trans);
9323 case NFT_MSG_DELRULE:
9324 case NFT_MSG_DESTROYRULE:
9325 list_del_rcu(&nft_trans_rule(trans)->list);
9326 nf_tables_rule_notify(&trans->ctx,
9327 nft_trans_rule(trans),
9329 nft_rule_expr_deactivate(&trans->ctx,
9330 nft_trans_rule(trans),
9333 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
9334 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
9336 case NFT_MSG_NEWSET:
9337 if (nft_trans_set_update(trans)) {
9338 struct nft_set *set = nft_trans_set(trans);
9340 WRITE_ONCE(set->timeout, nft_trans_set_timeout(trans));
9341 WRITE_ONCE(set->gc_int, nft_trans_set_gc_int(trans));
9343 nft_clear(net, nft_trans_set(trans));
9344 /* This avoids hitting -EBUSY when deleting the table
9345 * from the transaction.
9347 if (nft_set_is_anonymous(nft_trans_set(trans)) &&
9348 !list_empty(&nft_trans_set(trans)->bindings))
9349 trans->ctx.table->use--;
9351 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
9352 NFT_MSG_NEWSET, GFP_KERNEL);
9353 nft_trans_destroy(trans);
9355 case NFT_MSG_DELSET:
9356 case NFT_MSG_DESTROYSET:
9357 list_del_rcu(&nft_trans_set(trans)->list);
9358 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
9359 trans->msg_type, GFP_KERNEL);
9361 case NFT_MSG_NEWSETELEM:
9362 te = (struct nft_trans_elem *)trans->data;
9364 nft_setelem_activate(net, te->set, &te->elem);
9365 nf_tables_setelem_notify(&trans->ctx, te->set,
9367 NFT_MSG_NEWSETELEM);
9368 nft_trans_destroy(trans);
9370 case NFT_MSG_DELSETELEM:
9371 case NFT_MSG_DESTROYSETELEM:
9372 te = (struct nft_trans_elem *)trans->data;
9374 nf_tables_setelem_notify(&trans->ctx, te->set,
9377 nft_setelem_remove(net, te->set, &te->elem);
9378 if (!nft_setelem_is_catchall(te->set, &te->elem)) {
9379 atomic_dec(&te->set->nelems);
9383 case NFT_MSG_NEWOBJ:
9384 if (nft_trans_obj_update(trans)) {
9385 nft_obj_commit_update(trans);
9386 nf_tables_obj_notify(&trans->ctx,
9387 nft_trans_obj(trans),
9390 nft_clear(net, nft_trans_obj(trans));
9391 nf_tables_obj_notify(&trans->ctx,
9392 nft_trans_obj(trans),
9394 nft_trans_destroy(trans);
9397 case NFT_MSG_DELOBJ:
9398 case NFT_MSG_DESTROYOBJ:
9399 nft_obj_del(nft_trans_obj(trans));
9400 nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
9403 case NFT_MSG_NEWFLOWTABLE:
9404 if (nft_trans_flowtable_update(trans)) {
9405 nft_trans_flowtable(trans)->data.flags =
9406 nft_trans_flowtable_flags(trans);
9407 nf_tables_flowtable_notify(&trans->ctx,
9408 nft_trans_flowtable(trans),
9409 &nft_trans_flowtable_hooks(trans),
9410 NFT_MSG_NEWFLOWTABLE);
9411 list_splice(&nft_trans_flowtable_hooks(trans),
9412 &nft_trans_flowtable(trans)->hook_list);
9414 nft_clear(net, nft_trans_flowtable(trans));
9415 nf_tables_flowtable_notify(&trans->ctx,
9416 nft_trans_flowtable(trans),
9418 NFT_MSG_NEWFLOWTABLE);
9420 nft_trans_destroy(trans);
9422 case NFT_MSG_DELFLOWTABLE:
9423 case NFT_MSG_DESTROYFLOWTABLE:
9424 if (nft_trans_flowtable_update(trans)) {
9425 nf_tables_flowtable_notify(&trans->ctx,
9426 nft_trans_flowtable(trans),
9427 &nft_trans_flowtable_hooks(trans),
9429 nft_unregister_flowtable_net_hooks(net,
9430 &nft_trans_flowtable_hooks(trans));
9432 list_del_rcu(&nft_trans_flowtable(trans)->list);
9433 nf_tables_flowtable_notify(&trans->ctx,
9434 nft_trans_flowtable(trans),
9437 nft_unregister_flowtable_net_hooks(net,
9438 &nft_trans_flowtable(trans)->hook_list);
9444 nft_commit_notify(net, NETLINK_CB(skb).portid);
9445 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
9446 nf_tables_commit_audit_log(&adl, nft_net->base_seq);
9447 nf_tables_commit_release(net);
9452 static void nf_tables_module_autoload(struct net *net)
9454 struct nftables_pernet *nft_net = nft_pernet(net);
9455 struct nft_module_request *req, *next;
9456 LIST_HEAD(module_list);
9458 list_splice_init(&nft_net->module_list, &module_list);
9459 mutex_unlock(&nft_net->commit_mutex);
9460 list_for_each_entry_safe(req, next, &module_list, list) {
9461 request_module("%s", req->module);
9464 mutex_lock(&nft_net->commit_mutex);
9465 list_splice(&module_list, &nft_net->module_list);
9468 static void nf_tables_abort_release(struct nft_trans *trans)
9470 switch (trans->msg_type) {
9471 case NFT_MSG_NEWTABLE:
9472 nf_tables_table_destroy(&trans->ctx);
9474 case NFT_MSG_NEWCHAIN:
9475 if (nft_trans_chain_update(trans))
9476 nft_hooks_destroy(&nft_trans_chain_hooks(trans));
9478 nf_tables_chain_destroy(&trans->ctx);
9480 case NFT_MSG_NEWRULE:
9481 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
9483 case NFT_MSG_NEWSET:
9484 nft_set_destroy(&trans->ctx, nft_trans_set(trans));
9486 case NFT_MSG_NEWSETELEM:
9487 nft_set_elem_destroy(nft_trans_elem_set(trans),
9488 nft_trans_elem(trans).priv, true);
9490 case NFT_MSG_NEWOBJ:
9491 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
9493 case NFT_MSG_NEWFLOWTABLE:
9494 if (nft_trans_flowtable_update(trans))
9495 nft_hooks_destroy(&nft_trans_flowtable_hooks(trans));
9497 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
9503 static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
9505 struct nftables_pernet *nft_net = nft_pernet(net);
9506 struct nft_trans *trans, *next;
9507 struct nft_trans_elem *te;
9509 if (action == NFNL_ABORT_VALIDATE &&
9510 nf_tables_validate(net) < 0)
9513 list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list,
9515 switch (trans->msg_type) {
9516 case NFT_MSG_NEWTABLE:
9517 if (nft_trans_table_update(trans)) {
9518 if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) {
9519 nft_trans_destroy(trans);
9522 if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_DORMANT) {
9523 nf_tables_table_disable(net, trans->ctx.table);
9524 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
9525 } else if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_AWAKEN) {
9526 trans->ctx.table->flags &= ~NFT_TABLE_F_DORMANT;
9528 trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE;
9529 nft_trans_destroy(trans);
9531 list_del_rcu(&trans->ctx.table->list);
9534 case NFT_MSG_DELTABLE:
9535 case NFT_MSG_DESTROYTABLE:
9536 nft_clear(trans->ctx.net, trans->ctx.table);
9537 nft_trans_destroy(trans);
9539 case NFT_MSG_NEWCHAIN:
9540 if (nft_trans_chain_update(trans)) {
9541 nft_netdev_unregister_hooks(net,
9542 &nft_trans_chain_hooks(trans),
9544 free_percpu(nft_trans_chain_stats(trans));
9545 kfree(nft_trans_chain_name(trans));
9546 nft_trans_destroy(trans);
9548 if (nft_chain_is_bound(trans->ctx.chain)) {
9549 nft_trans_destroy(trans);
9552 trans->ctx.table->use--;
9553 nft_chain_del(trans->ctx.chain);
9554 nf_tables_unregister_hook(trans->ctx.net,
9559 case NFT_MSG_DELCHAIN:
9560 case NFT_MSG_DESTROYCHAIN:
9561 trans->ctx.table->use++;
9562 nft_clear(trans->ctx.net, trans->ctx.chain);
9563 nft_trans_destroy(trans);
9565 case NFT_MSG_NEWRULE:
9566 trans->ctx.chain->use--;
9567 list_del_rcu(&nft_trans_rule(trans)->list);
9568 nft_rule_expr_deactivate(&trans->ctx,
9569 nft_trans_rule(trans),
9571 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
9572 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
9574 case NFT_MSG_DELRULE:
9575 case NFT_MSG_DESTROYRULE:
9576 trans->ctx.chain->use++;
9577 nft_clear(trans->ctx.net, nft_trans_rule(trans));
9578 nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans));
9579 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
9580 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
9582 nft_trans_destroy(trans);
9584 case NFT_MSG_NEWSET:
9585 if (nft_trans_set_update(trans)) {
9586 nft_trans_destroy(trans);
9589 trans->ctx.table->use--;
9590 if (nft_trans_set_bound(trans)) {
9591 nft_trans_destroy(trans);
9594 list_del_rcu(&nft_trans_set(trans)->list);
9596 case NFT_MSG_DELSET:
9597 case NFT_MSG_DESTROYSET:
9598 trans->ctx.table->use++;
9599 nft_clear(trans->ctx.net, nft_trans_set(trans));
9600 nft_trans_destroy(trans);
9602 case NFT_MSG_NEWSETELEM:
9603 if (nft_trans_elem_set_bound(trans)) {
9604 nft_trans_destroy(trans);
9607 te = (struct nft_trans_elem *)trans->data;
9608 nft_setelem_remove(net, te->set, &te->elem);
9609 if (!nft_setelem_is_catchall(te->set, &te->elem))
9610 atomic_dec(&te->set->nelems);
9612 case NFT_MSG_DELSETELEM:
9613 case NFT_MSG_DESTROYSETELEM:
9614 te = (struct nft_trans_elem *)trans->data;
9616 nft_setelem_data_activate(net, te->set, &te->elem);
9617 nft_setelem_activate(net, te->set, &te->elem);
9618 if (!nft_setelem_is_catchall(te->set, &te->elem))
9621 nft_trans_destroy(trans);
9623 case NFT_MSG_NEWOBJ:
9624 if (nft_trans_obj_update(trans)) {
9625 nft_obj_destroy(&trans->ctx, nft_trans_obj_newobj(trans));
9626 nft_trans_destroy(trans);
9628 trans->ctx.table->use--;
9629 nft_obj_del(nft_trans_obj(trans));
9632 case NFT_MSG_DELOBJ:
9633 case NFT_MSG_DESTROYOBJ:
9634 trans->ctx.table->use++;
9635 nft_clear(trans->ctx.net, nft_trans_obj(trans));
9636 nft_trans_destroy(trans);
9638 case NFT_MSG_NEWFLOWTABLE:
9639 if (nft_trans_flowtable_update(trans)) {
9640 nft_unregister_flowtable_net_hooks(net,
9641 &nft_trans_flowtable_hooks(trans));
9643 trans->ctx.table->use--;
9644 list_del_rcu(&nft_trans_flowtable(trans)->list);
9645 nft_unregister_flowtable_net_hooks(net,
9646 &nft_trans_flowtable(trans)->hook_list);
9649 case NFT_MSG_DELFLOWTABLE:
9650 case NFT_MSG_DESTROYFLOWTABLE:
9651 if (nft_trans_flowtable_update(trans)) {
9652 list_splice(&nft_trans_flowtable_hooks(trans),
9653 &nft_trans_flowtable(trans)->hook_list);
9655 trans->ctx.table->use++;
9656 nft_clear(trans->ctx.net, nft_trans_flowtable(trans));
9658 nft_trans_destroy(trans);
9665 list_for_each_entry_safe_reverse(trans, next,
9666 &nft_net->commit_list, list) {
9667 list_del(&trans->list);
9668 nf_tables_abort_release(trans);
9671 if (action == NFNL_ABORT_AUTOLOAD)
9672 nf_tables_module_autoload(net);
9674 nf_tables_module_autoload_cleanup(net);
9679 static int nf_tables_abort(struct net *net, struct sk_buff *skb,
9680 enum nfnl_abort_action action)
9682 struct nftables_pernet *nft_net = nft_pernet(net);
9683 int ret = __nf_tables_abort(net, action);
9685 mutex_unlock(&nft_net->commit_mutex);
9690 static bool nf_tables_valid_genid(struct net *net, u32 genid)
9692 struct nftables_pernet *nft_net = nft_pernet(net);
9695 mutex_lock(&nft_net->commit_mutex);
9697 genid_ok = genid == 0 || nft_net->base_seq == genid;
9699 mutex_unlock(&nft_net->commit_mutex);
9701 /* else, commit mutex has to be released by commit or abort function */
9705 static const struct nfnetlink_subsystem nf_tables_subsys = {
9706 .name = "nf_tables",
9707 .subsys_id = NFNL_SUBSYS_NFTABLES,
9708 .cb_count = NFT_MSG_MAX,
9710 .commit = nf_tables_commit,
9711 .abort = nf_tables_abort,
9712 .valid_genid = nf_tables_valid_genid,
9713 .owner = THIS_MODULE,
9716 int nft_chain_validate_dependency(const struct nft_chain *chain,
9717 enum nft_chain_types type)
9719 const struct nft_base_chain *basechain;
9721 if (nft_is_base_chain(chain)) {
9722 basechain = nft_base_chain(chain);
9723 if (basechain->type->type != type)
9728 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
9730 int nft_chain_validate_hooks(const struct nft_chain *chain,
9731 unsigned int hook_flags)
9733 struct nft_base_chain *basechain;
9735 if (nft_is_base_chain(chain)) {
9736 basechain = nft_base_chain(chain);
9738 if ((1 << basechain->ops.hooknum) & hook_flags)
9746 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
9749 * Loop detection - walk through the ruleset beginning at the destination chain
9750 * of a new jump until either the source chain is reached (loop) or all
9751 * reachable chains have been traversed.
9753 * The loop check is performed whenever a new jump verdict is added to an
9754 * expression or verdict map or a verdict map is bound to a new chain.
9757 static int nf_tables_check_loops(const struct nft_ctx *ctx,
9758 const struct nft_chain *chain);
9760 static int nft_check_loops(const struct nft_ctx *ctx,
9761 const struct nft_set_ext *ext)
9763 const struct nft_data *data;
9766 data = nft_set_ext_data(ext);
9767 switch (data->verdict.code) {
9770 ret = nf_tables_check_loops(ctx, data->verdict.chain);
9780 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
9781 struct nft_set *set,
9782 const struct nft_set_iter *iter,
9783 struct nft_set_elem *elem)
9785 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
9787 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
9788 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
9791 return nft_check_loops(ctx, ext);
9794 static int nft_set_catchall_loops(const struct nft_ctx *ctx,
9795 struct nft_set *set)
9797 u8 genmask = nft_genmask_next(ctx->net);
9798 struct nft_set_elem_catchall *catchall;
9799 struct nft_set_ext *ext;
9802 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
9803 ext = nft_set_elem_ext(set, catchall->elem);
9804 if (!nft_set_elem_active(ext, genmask))
9807 ret = nft_check_loops(ctx, ext);
9815 static int nf_tables_check_loops(const struct nft_ctx *ctx,
9816 const struct nft_chain *chain)
9818 const struct nft_rule *rule;
9819 const struct nft_expr *expr, *last;
9820 struct nft_set *set;
9821 struct nft_set_binding *binding;
9822 struct nft_set_iter iter;
9824 if (ctx->chain == chain)
9827 list_for_each_entry(rule, &chain->rules, list) {
9828 nft_rule_for_each_expr(expr, last, rule) {
9829 struct nft_immediate_expr *priv;
9830 const struct nft_data *data;
9833 if (strcmp(expr->ops->type->name, "immediate"))
9836 priv = nft_expr_priv(expr);
9837 if (priv->dreg != NFT_REG_VERDICT)
9841 switch (data->verdict.code) {
9844 err = nf_tables_check_loops(ctx,
9845 data->verdict.chain);
9855 list_for_each_entry(set, &ctx->table->sets, list) {
9856 if (!nft_is_active_next(ctx->net, set))
9858 if (!(set->flags & NFT_SET_MAP) ||
9859 set->dtype != NFT_DATA_VERDICT)
9862 list_for_each_entry(binding, &set->bindings, list) {
9863 if (!(binding->flags & NFT_SET_MAP) ||
9864 binding->chain != chain)
9867 iter.genmask = nft_genmask_next(ctx->net);
9871 iter.fn = nf_tables_loop_check_setelem;
9873 set->ops->walk(ctx, set, &iter);
9875 iter.err = nft_set_catchall_loops(ctx, set);
9886 * nft_parse_u32_check - fetch u32 attribute and check for maximum value
9888 * @attr: netlink attribute to fetch value from
9889 * @max: maximum value to be stored in dest
9890 * @dest: pointer to the variable
9892 * Parse, check and store a given u32 netlink attribute into variable.
9893 * This function returns -ERANGE if the value goes over maximum value.
9894 * Otherwise a 0 is returned and the attribute value is stored in the
9895 * destination variable.
9897 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest)
9901 val = ntohl(nla_get_be32(attr));
9908 EXPORT_SYMBOL_GPL(nft_parse_u32_check);
9910 static int nft_parse_register(const struct nlattr *attr, u32 *preg)
9914 reg = ntohl(nla_get_be32(attr));
9916 case NFT_REG_VERDICT...NFT_REG_4:
9917 *preg = reg * NFT_REG_SIZE / NFT_REG32_SIZE;
9919 case NFT_REG32_00...NFT_REG32_15:
9920 *preg = reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
9930 * nft_dump_register - dump a register value to a netlink attribute
9932 * @skb: socket buffer
9933 * @attr: attribute number
9934 * @reg: register number
9936 * Construct a netlink attribute containing the register number. For
9937 * compatibility reasons, register numbers being a multiple of 4 are
9938 * translated to the corresponding 128 bit register numbers.
9940 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
9942 if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
9943 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
9945 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
9947 return nla_put_be32(skb, attr, htonl(reg));
9949 EXPORT_SYMBOL_GPL(nft_dump_register);
9951 static int nft_validate_register_load(enum nft_registers reg, unsigned int len)
9953 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
9957 if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data))
9963 int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len)
9968 err = nft_parse_register(attr, ®);
9972 err = nft_validate_register_load(reg, len);
9979 EXPORT_SYMBOL_GPL(nft_parse_register_load);
9981 static int nft_validate_register_store(const struct nft_ctx *ctx,
9982 enum nft_registers reg,
9983 const struct nft_data *data,
9984 enum nft_data_types type,
9990 case NFT_REG_VERDICT:
9991 if (type != NFT_DATA_VERDICT)
9995 (data->verdict.code == NFT_GOTO ||
9996 data->verdict.code == NFT_JUMP)) {
9997 err = nf_tables_check_loops(ctx, data->verdict.chain);
10004 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
10008 if (reg * NFT_REG32_SIZE + len >
10009 sizeof_field(struct nft_regs, data))
10012 if (data != NULL && type != NFT_DATA_VALUE)
10018 int nft_parse_register_store(const struct nft_ctx *ctx,
10019 const struct nlattr *attr, u8 *dreg,
10020 const struct nft_data *data,
10021 enum nft_data_types type, unsigned int len)
10026 err = nft_parse_register(attr, ®);
10030 err = nft_validate_register_store(ctx, reg, data, type, len);
10037 EXPORT_SYMBOL_GPL(nft_parse_register_store);
10039 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
10040 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
10041 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
10042 .len = NFT_CHAIN_MAXNAMELEN - 1 },
10043 [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 },
10046 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
10047 struct nft_data_desc *desc, const struct nlattr *nla)
10049 u8 genmask = nft_genmask_next(ctx->net);
10050 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
10051 struct nft_chain *chain;
10054 err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla,
10055 nft_verdict_policy, NULL);
10059 if (!tb[NFTA_VERDICT_CODE])
10061 data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
10063 switch (data->verdict.code) {
10065 switch (data->verdict.code & NF_VERDICT_MASK) {
10080 if (tb[NFTA_VERDICT_CHAIN]) {
10081 chain = nft_chain_lookup(ctx->net, ctx->table,
10082 tb[NFTA_VERDICT_CHAIN],
10084 } else if (tb[NFTA_VERDICT_CHAIN_ID]) {
10085 chain = nft_chain_lookup_byid(ctx->net, ctx->table,
10086 tb[NFTA_VERDICT_CHAIN_ID]);
10088 return PTR_ERR(chain);
10094 return PTR_ERR(chain);
10095 if (nft_is_base_chain(chain))
10096 return -EOPNOTSUPP;
10097 if (nft_chain_is_bound(chain))
10099 if (desc->flags & NFT_DATA_DESC_SETELEM &&
10100 chain->flags & NFT_CHAIN_BINDING)
10104 data->verdict.chain = chain;
10108 desc->len = sizeof(data->verdict);
10113 static void nft_verdict_uninit(const struct nft_data *data)
10115 struct nft_chain *chain;
10116 struct nft_rule *rule;
10118 switch (data->verdict.code) {
10121 chain = data->verdict.chain;
10124 if (!nft_chain_is_bound(chain))
10127 chain->table->use--;
10128 list_for_each_entry(rule, &chain->rules, list)
10131 nft_chain_del(chain);
10136 int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
10138 struct nlattr *nest;
10140 nest = nla_nest_start_noflag(skb, type);
10142 goto nla_put_failure;
10144 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
10145 goto nla_put_failure;
10150 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
10152 goto nla_put_failure;
10154 nla_nest_end(skb, nest);
10161 static int nft_value_init(const struct nft_ctx *ctx,
10162 struct nft_data *data, struct nft_data_desc *desc,
10163 const struct nlattr *nla)
10167 len = nla_len(nla);
10170 if (len > desc->size)
10173 if (len != desc->len)
10179 nla_memcpy(data->data, nla, len);
10184 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
10187 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
10190 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
10191 [NFTA_DATA_VALUE] = { .type = NLA_BINARY },
10192 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
10196 * nft_data_init - parse nf_tables data netlink attributes
10198 * @ctx: context of the expression using the data
10199 * @data: destination struct nft_data
10200 * @desc: data description
10201 * @nla: netlink attribute containing data
10203 * Parse the netlink data attributes and initialize a struct nft_data.
10204 * The type and length of data are returned in the data description.
10206 * The caller can indicate that it only wants to accept data of type
10207 * NFT_DATA_VALUE by passing NULL for the ctx argument.
10209 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
10210 struct nft_data_desc *desc, const struct nlattr *nla)
10212 struct nlattr *tb[NFTA_DATA_MAX + 1];
10215 if (WARN_ON_ONCE(!desc->size))
10218 err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla,
10219 nft_data_policy, NULL);
10223 if (tb[NFTA_DATA_VALUE]) {
10224 if (desc->type != NFT_DATA_VALUE)
10227 err = nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
10228 } else if (tb[NFTA_DATA_VERDICT] && ctx != NULL) {
10229 if (desc->type != NFT_DATA_VERDICT)
10232 err = nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
10239 EXPORT_SYMBOL_GPL(nft_data_init);
10242 * nft_data_release - release a nft_data item
10244 * @data: struct nft_data to release
10245 * @type: type of data
10247 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
10248 * all others need to be released by calling this function.
10250 void nft_data_release(const struct nft_data *data, enum nft_data_types type)
10252 if (type < NFT_DATA_VERDICT)
10255 case NFT_DATA_VERDICT:
10256 return nft_verdict_uninit(data);
10261 EXPORT_SYMBOL_GPL(nft_data_release);
10263 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
10264 enum nft_data_types type, unsigned int len)
10266 struct nlattr *nest;
10269 nest = nla_nest_start_noflag(skb, attr);
10274 case NFT_DATA_VALUE:
10275 err = nft_value_dump(skb, data, len);
10277 case NFT_DATA_VERDICT:
10278 err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
10285 nla_nest_end(skb, nest);
10288 EXPORT_SYMBOL_GPL(nft_data_dump);
10290 int __nft_release_basechain(struct nft_ctx *ctx)
10292 struct nft_rule *rule, *nr;
10294 if (WARN_ON(!nft_is_base_chain(ctx->chain)))
10297 nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain);
10298 list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
10299 list_del(&rule->list);
10301 nf_tables_rule_release(ctx, rule);
10303 nft_chain_del(ctx->chain);
10305 nf_tables_chain_destroy(ctx);
10309 EXPORT_SYMBOL_GPL(__nft_release_basechain);
10311 static void __nft_release_hook(struct net *net, struct nft_table *table)
10313 struct nft_flowtable *flowtable;
10314 struct nft_chain *chain;
10316 list_for_each_entry(chain, &table->chains, list)
10317 __nf_tables_unregister_hook(net, table, chain, true);
10318 list_for_each_entry(flowtable, &table->flowtables, list)
10319 __nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list,
10323 static void __nft_release_hooks(struct net *net)
10325 struct nftables_pernet *nft_net = nft_pernet(net);
10326 struct nft_table *table;
10328 list_for_each_entry(table, &nft_net->tables, list) {
10329 if (nft_table_has_owner(table))
10332 __nft_release_hook(net, table);
10336 static void __nft_release_table(struct net *net, struct nft_table *table)
10338 struct nft_flowtable *flowtable, *nf;
10339 struct nft_chain *chain, *nc;
10340 struct nft_object *obj, *ne;
10341 struct nft_rule *rule, *nr;
10342 struct nft_set *set, *ns;
10343 struct nft_ctx ctx = {
10345 .family = NFPROTO_NETDEV,
10348 ctx.family = table->family;
10350 list_for_each_entry(chain, &table->chains, list) {
10352 list_for_each_entry_safe(rule, nr, &chain->rules, list) {
10353 list_del(&rule->list);
10355 nf_tables_rule_release(&ctx, rule);
10358 list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) {
10359 list_del(&flowtable->list);
10361 nf_tables_flowtable_destroy(flowtable);
10363 list_for_each_entry_safe(set, ns, &table->sets, list) {
10364 list_del(&set->list);
10366 nft_set_destroy(&ctx, set);
10368 list_for_each_entry_safe(obj, ne, &table->objects, list) {
10371 nft_obj_destroy(&ctx, obj);
10373 list_for_each_entry_safe(chain, nc, &table->chains, list) {
10375 nft_chain_del(chain);
10377 nf_tables_chain_destroy(&ctx);
10379 nf_tables_table_destroy(&ctx);
10382 static void __nft_release_tables(struct net *net)
10384 struct nftables_pernet *nft_net = nft_pernet(net);
10385 struct nft_table *table, *nt;
10387 list_for_each_entry_safe(table, nt, &nft_net->tables, list) {
10388 if (nft_table_has_owner(table))
10391 list_del(&table->list);
10393 __nft_release_table(net, table);
10397 static int nft_rcv_nl_event(struct notifier_block *this, unsigned long event,
10400 struct nft_table *table, *to_delete[8];
10401 struct nftables_pernet *nft_net;
10402 struct netlink_notify *n = ptr;
10403 struct net *net = n->net;
10404 unsigned int deleted;
10405 bool restart = false;
10407 if (event != NETLINK_URELEASE || n->protocol != NETLINK_NETFILTER)
10408 return NOTIFY_DONE;
10410 nft_net = nft_pernet(net);
10412 mutex_lock(&nft_net->commit_mutex);
10413 if (!list_empty(&nf_tables_destroy_list))
10416 list_for_each_entry(table, &nft_net->tables, list) {
10417 if (nft_table_has_owner(table) &&
10418 n->portid == table->nlpid) {
10419 __nft_release_hook(net, table);
10420 list_del_rcu(&table->list);
10421 to_delete[deleted++] = table;
10422 if (deleted >= ARRAY_SIZE(to_delete))
10427 restart = deleted >= ARRAY_SIZE(to_delete);
10430 __nft_release_table(net, to_delete[--deleted]);
10435 mutex_unlock(&nft_net->commit_mutex);
10437 return NOTIFY_DONE;
10440 static struct notifier_block nft_nl_notifier = {
10441 .notifier_call = nft_rcv_nl_event,
10444 static int __net_init nf_tables_init_net(struct net *net)
10446 struct nftables_pernet *nft_net = nft_pernet(net);
10448 INIT_LIST_HEAD(&nft_net->tables);
10449 INIT_LIST_HEAD(&nft_net->commit_list);
10450 INIT_LIST_HEAD(&nft_net->module_list);
10451 INIT_LIST_HEAD(&nft_net->notify_list);
10452 mutex_init(&nft_net->commit_mutex);
10453 nft_net->base_seq = 1;
10458 static void __net_exit nf_tables_pre_exit_net(struct net *net)
10460 struct nftables_pernet *nft_net = nft_pernet(net);
10462 mutex_lock(&nft_net->commit_mutex);
10463 __nft_release_hooks(net);
10464 mutex_unlock(&nft_net->commit_mutex);
10467 static void __net_exit nf_tables_exit_net(struct net *net)
10469 struct nftables_pernet *nft_net = nft_pernet(net);
10471 mutex_lock(&nft_net->commit_mutex);
10472 if (!list_empty(&nft_net->commit_list) ||
10473 !list_empty(&nft_net->module_list))
10474 __nf_tables_abort(net, NFNL_ABORT_NONE);
10475 __nft_release_tables(net);
10476 mutex_unlock(&nft_net->commit_mutex);
10477 WARN_ON_ONCE(!list_empty(&nft_net->tables));
10478 WARN_ON_ONCE(!list_empty(&nft_net->module_list));
10479 WARN_ON_ONCE(!list_empty(&nft_net->notify_list));
10482 static struct pernet_operations nf_tables_net_ops = {
10483 .init = nf_tables_init_net,
10484 .pre_exit = nf_tables_pre_exit_net,
10485 .exit = nf_tables_exit_net,
10486 .id = &nf_tables_net_id,
10487 .size = sizeof(struct nftables_pernet),
10490 static int __init nf_tables_module_init(void)
10494 err = register_pernet_subsys(&nf_tables_net_ops);
10498 err = nft_chain_filter_init();
10500 goto err_chain_filter;
10502 err = nf_tables_core_module_init();
10504 goto err_core_module;
10506 err = register_netdevice_notifier(&nf_tables_flowtable_notifier);
10508 goto err_netdev_notifier;
10510 err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params);
10512 goto err_rht_objname;
10514 err = nft_offload_init();
10518 err = netlink_register_notifier(&nft_nl_notifier);
10520 goto err_netlink_notifier;
10523 err = nfnetlink_subsys_register(&nf_tables_subsys);
10525 goto err_nfnl_subsys;
10527 nft_chain_route_init();
10532 netlink_unregister_notifier(&nft_nl_notifier);
10533 err_netlink_notifier:
10534 nft_offload_exit();
10536 rhltable_destroy(&nft_objname_ht);
10538 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
10539 err_netdev_notifier:
10540 nf_tables_core_module_exit();
10542 nft_chain_filter_fini();
10544 unregister_pernet_subsys(&nf_tables_net_ops);
10548 static void __exit nf_tables_module_exit(void)
10550 nfnetlink_subsys_unregister(&nf_tables_subsys);
10551 netlink_unregister_notifier(&nft_nl_notifier);
10552 nft_offload_exit();
10553 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
10554 nft_chain_filter_fini();
10555 nft_chain_route_fini();
10556 unregister_pernet_subsys(&nf_tables_net_ops);
10557 cancel_work_sync(&trans_destroy_work);
10559 rhltable_destroy(&nft_objname_ht);
10560 nf_tables_core_module_exit();
10563 module_init(nf_tables_module_init);
10564 module_exit(nf_tables_module_exit);
10566 MODULE_LICENSE("GPL");
10567 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
10568 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);