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/netfilter.h>
16 #include <linux/netfilter/nfnetlink.h>
17 #include <linux/netfilter/nf_tables.h>
18 #include <net/netfilter/nf_flow_table.h>
19 #include <net/netfilter/nf_tables_core.h>
20 #include <net/netfilter/nf_tables.h>
21 #include <net/net_namespace.h>
24 static LIST_HEAD(nf_tables_expressions
);
25 static LIST_HEAD(nf_tables_objects
);
26 static LIST_HEAD(nf_tables_flowtables
);
27 static LIST_HEAD(nf_tables_destroy_list
);
28 static DEFINE_SPINLOCK(nf_tables_destroy_list_lock
);
29 static u64 table_handle
;
32 NFT_VALIDATE_SKIP
= 0,
37 static struct rhltable nft_objname_ht
;
39 static u32
nft_chain_hash(const void *data
, u32 len
, u32 seed
);
40 static u32
nft_chain_hash_obj(const void *data
, u32 len
, u32 seed
);
41 static int nft_chain_hash_cmp(struct rhashtable_compare_arg
*, const void *);
43 static u32
nft_objname_hash(const void *data
, u32 len
, u32 seed
);
44 static u32
nft_objname_hash_obj(const void *data
, u32 len
, u32 seed
);
45 static int nft_objname_hash_cmp(struct rhashtable_compare_arg
*, const void *);
47 static const struct rhashtable_params nft_chain_ht_params
= {
48 .head_offset
= offsetof(struct nft_chain
, rhlhead
),
49 .key_offset
= offsetof(struct nft_chain
, name
),
50 .hashfn
= nft_chain_hash
,
51 .obj_hashfn
= nft_chain_hash_obj
,
52 .obj_cmpfn
= nft_chain_hash_cmp
,
53 .automatic_shrinking
= true,
56 static const struct rhashtable_params nft_objname_ht_params
= {
57 .head_offset
= offsetof(struct nft_object
, rhlhead
),
58 .key_offset
= offsetof(struct nft_object
, key
),
59 .hashfn
= nft_objname_hash
,
60 .obj_hashfn
= nft_objname_hash_obj
,
61 .obj_cmpfn
= nft_objname_hash_cmp
,
62 .automatic_shrinking
= true,
65 static void nft_validate_state_update(struct net
*net
, u8 new_validate_state
)
67 switch (net
->nft
.validate_state
) {
68 case NFT_VALIDATE_SKIP
:
69 WARN_ON_ONCE(new_validate_state
== NFT_VALIDATE_DO
);
71 case NFT_VALIDATE_NEED
:
74 if (new_validate_state
== NFT_VALIDATE_NEED
)
78 net
->nft
.validate_state
= new_validate_state
;
80 static void nf_tables_trans_destroy_work(struct work_struct
*w
);
81 static DECLARE_WORK(trans_destroy_work
, nf_tables_trans_destroy_work
);
83 static void nft_ctx_init(struct nft_ctx
*ctx
,
85 const struct sk_buff
*skb
,
86 const struct nlmsghdr
*nlh
,
88 struct nft_table
*table
,
89 struct nft_chain
*chain
,
90 const struct nlattr
* const *nla
)
98 ctx
->portid
= NETLINK_CB(skb
).portid
;
99 ctx
->report
= nlmsg_report(nlh
);
100 ctx
->seq
= nlh
->nlmsg_seq
;
103 static struct nft_trans
*nft_trans_alloc_gfp(const struct nft_ctx
*ctx
,
104 int msg_type
, u32 size
, gfp_t gfp
)
106 struct nft_trans
*trans
;
108 trans
= kzalloc(sizeof(struct nft_trans
) + size
, gfp
);
112 trans
->msg_type
= msg_type
;
118 static struct nft_trans
*nft_trans_alloc(const struct nft_ctx
*ctx
,
119 int msg_type
, u32 size
)
121 return nft_trans_alloc_gfp(ctx
, msg_type
, size
, GFP_KERNEL
);
124 static void nft_trans_destroy(struct nft_trans
*trans
)
126 list_del(&trans
->list
);
130 static void nft_set_trans_bind(const struct nft_ctx
*ctx
, struct nft_set
*set
)
132 struct net
*net
= ctx
->net
;
133 struct nft_trans
*trans
;
135 if (!nft_set_is_anonymous(set
))
138 list_for_each_entry_reverse(trans
, &net
->nft
.commit_list
, list
) {
139 if (trans
->msg_type
== NFT_MSG_NEWSET
&&
140 nft_trans_set(trans
) == set
) {
147 static int nf_tables_register_hook(struct net
*net
,
148 const struct nft_table
*table
,
149 struct nft_chain
*chain
)
151 const struct nft_base_chain
*basechain
;
152 const struct nf_hook_ops
*ops
;
154 if (table
->flags
& NFT_TABLE_F_DORMANT
||
155 !nft_is_base_chain(chain
))
158 basechain
= nft_base_chain(chain
);
159 ops
= &basechain
->ops
;
161 if (basechain
->type
->ops_register
)
162 return basechain
->type
->ops_register(net
, ops
);
164 return nf_register_net_hook(net
, ops
);
167 static void nf_tables_unregister_hook(struct net
*net
,
168 const struct nft_table
*table
,
169 struct nft_chain
*chain
)
171 const struct nft_base_chain
*basechain
;
172 const struct nf_hook_ops
*ops
;
174 if (table
->flags
& NFT_TABLE_F_DORMANT
||
175 !nft_is_base_chain(chain
))
177 basechain
= nft_base_chain(chain
);
178 ops
= &basechain
->ops
;
180 if (basechain
->type
->ops_unregister
)
181 return basechain
->type
->ops_unregister(net
, ops
);
183 nf_unregister_net_hook(net
, ops
);
186 static int nft_trans_table_add(struct nft_ctx
*ctx
, int msg_type
)
188 struct nft_trans
*trans
;
190 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_table
));
194 if (msg_type
== NFT_MSG_NEWTABLE
)
195 nft_activate_next(ctx
->net
, ctx
->table
);
197 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
201 static int nft_deltable(struct nft_ctx
*ctx
)
205 err
= nft_trans_table_add(ctx
, NFT_MSG_DELTABLE
);
209 nft_deactivate_next(ctx
->net
, ctx
->table
);
213 static struct nft_trans
*nft_trans_chain_add(struct nft_ctx
*ctx
, int msg_type
)
215 struct nft_trans
*trans
;
217 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_chain
));
219 return ERR_PTR(-ENOMEM
);
221 if (msg_type
== NFT_MSG_NEWCHAIN
)
222 nft_activate_next(ctx
->net
, ctx
->chain
);
224 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
228 static int nft_delchain(struct nft_ctx
*ctx
)
230 struct nft_trans
*trans
;
232 trans
= nft_trans_chain_add(ctx
, NFT_MSG_DELCHAIN
);
234 return PTR_ERR(trans
);
237 nft_deactivate_next(ctx
->net
, ctx
->chain
);
242 static void nft_rule_expr_activate(const struct nft_ctx
*ctx
,
243 struct nft_rule
*rule
)
245 struct nft_expr
*expr
;
247 expr
= nft_expr_first(rule
);
248 while (expr
!= nft_expr_last(rule
) && expr
->ops
) {
249 if (expr
->ops
->activate
)
250 expr
->ops
->activate(ctx
, expr
);
252 expr
= nft_expr_next(expr
);
256 static void nft_rule_expr_deactivate(const struct nft_ctx
*ctx
,
257 struct nft_rule
*rule
,
258 enum nft_trans_phase phase
)
260 struct nft_expr
*expr
;
262 expr
= nft_expr_first(rule
);
263 while (expr
!= nft_expr_last(rule
) && expr
->ops
) {
264 if (expr
->ops
->deactivate
)
265 expr
->ops
->deactivate(ctx
, expr
, phase
);
267 expr
= nft_expr_next(expr
);
272 nf_tables_delrule_deactivate(struct nft_ctx
*ctx
, struct nft_rule
*rule
)
274 /* You cannot delete the same rule twice */
275 if (nft_is_active_next(ctx
->net
, rule
)) {
276 nft_deactivate_next(ctx
->net
, rule
);
283 static struct nft_trans
*nft_trans_rule_add(struct nft_ctx
*ctx
, int msg_type
,
284 struct nft_rule
*rule
)
286 struct nft_trans
*trans
;
288 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_rule
));
292 if (msg_type
== NFT_MSG_NEWRULE
&& ctx
->nla
[NFTA_RULE_ID
] != NULL
) {
293 nft_trans_rule_id(trans
) =
294 ntohl(nla_get_be32(ctx
->nla
[NFTA_RULE_ID
]));
296 nft_trans_rule(trans
) = rule
;
297 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
302 static int nft_delrule(struct nft_ctx
*ctx
, struct nft_rule
*rule
)
304 struct nft_trans
*trans
;
307 trans
= nft_trans_rule_add(ctx
, NFT_MSG_DELRULE
, rule
);
311 err
= nf_tables_delrule_deactivate(ctx
, rule
);
313 nft_trans_destroy(trans
);
316 nft_rule_expr_deactivate(ctx
, rule
, NFT_TRANS_PREPARE
);
321 static int nft_delrule_by_chain(struct nft_ctx
*ctx
)
323 struct nft_rule
*rule
;
326 list_for_each_entry(rule
, &ctx
->chain
->rules
, list
) {
327 if (!nft_is_active_next(ctx
->net
, rule
))
330 err
= nft_delrule(ctx
, rule
);
337 static int nft_trans_set_add(const struct nft_ctx
*ctx
, int msg_type
,
340 struct nft_trans
*trans
;
342 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_set
));
346 if (msg_type
== NFT_MSG_NEWSET
&& ctx
->nla
[NFTA_SET_ID
] != NULL
) {
347 nft_trans_set_id(trans
) =
348 ntohl(nla_get_be32(ctx
->nla
[NFTA_SET_ID
]));
349 nft_activate_next(ctx
->net
, set
);
351 nft_trans_set(trans
) = set
;
352 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
357 static int nft_delset(const struct nft_ctx
*ctx
, struct nft_set
*set
)
361 err
= nft_trans_set_add(ctx
, NFT_MSG_DELSET
, set
);
365 nft_deactivate_next(ctx
->net
, set
);
371 static int nft_trans_obj_add(struct nft_ctx
*ctx
, int msg_type
,
372 struct nft_object
*obj
)
374 struct nft_trans
*trans
;
376 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_obj
));
380 if (msg_type
== NFT_MSG_NEWOBJ
)
381 nft_activate_next(ctx
->net
, obj
);
383 nft_trans_obj(trans
) = obj
;
384 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
389 static int nft_delobj(struct nft_ctx
*ctx
, struct nft_object
*obj
)
393 err
= nft_trans_obj_add(ctx
, NFT_MSG_DELOBJ
, obj
);
397 nft_deactivate_next(ctx
->net
, obj
);
403 static int nft_trans_flowtable_add(struct nft_ctx
*ctx
, int msg_type
,
404 struct nft_flowtable
*flowtable
)
406 struct nft_trans
*trans
;
408 trans
= nft_trans_alloc(ctx
, msg_type
,
409 sizeof(struct nft_trans_flowtable
));
413 if (msg_type
== NFT_MSG_NEWFLOWTABLE
)
414 nft_activate_next(ctx
->net
, flowtable
);
416 nft_trans_flowtable(trans
) = flowtable
;
417 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
422 static int nft_delflowtable(struct nft_ctx
*ctx
,
423 struct nft_flowtable
*flowtable
)
427 err
= nft_trans_flowtable_add(ctx
, NFT_MSG_DELFLOWTABLE
, flowtable
);
431 nft_deactivate_next(ctx
->net
, flowtable
);
441 static struct nft_table
*nft_table_lookup(const struct net
*net
,
442 const struct nlattr
*nla
,
443 u8 family
, u8 genmask
)
445 struct nft_table
*table
;
448 return ERR_PTR(-EINVAL
);
450 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
451 if (!nla_strcmp(nla
, table
->name
) &&
452 table
->family
== family
&&
453 nft_active_genmask(table
, genmask
))
457 return ERR_PTR(-ENOENT
);
460 static struct nft_table
*nft_table_lookup_byhandle(const struct net
*net
,
461 const struct nlattr
*nla
,
464 struct nft_table
*table
;
466 list_for_each_entry(table
, &net
->nft
.tables
, list
) {
467 if (be64_to_cpu(nla_get_be64(nla
)) == table
->handle
&&
468 nft_active_genmask(table
, genmask
))
472 return ERR_PTR(-ENOENT
);
475 static inline u64
nf_tables_alloc_handle(struct nft_table
*table
)
477 return ++table
->hgenerator
;
480 static const struct nft_chain_type
*chain_type
[NFPROTO_NUMPROTO
][NFT_CHAIN_T_MAX
];
482 static const struct nft_chain_type
*
483 __nf_tables_chain_type_lookup(const struct nlattr
*nla
, u8 family
)
487 for (i
= 0; i
< NFT_CHAIN_T_MAX
; i
++) {
488 if (chain_type
[family
][i
] != NULL
&&
489 !nla_strcmp(nla
, chain_type
[family
][i
]->name
))
490 return chain_type
[family
][i
];
496 * Loading a module requires dropping mutex that guards the
498 * We first need to abort any pending transactions as once
499 * mutex is unlocked a different client could start a new
500 * transaction. It must not see any 'future generation'
501 * changes * as these changes will never happen.
503 #ifdef CONFIG_MODULES
504 static int __nf_tables_abort(struct net
*net
);
506 static void nft_request_module(struct net
*net
, const char *fmt
, ...)
508 char module_name
[MODULE_NAME_LEN
];
512 __nf_tables_abort(net
);
515 ret
= vsnprintf(module_name
, MODULE_NAME_LEN
, fmt
, args
);
517 if (WARN(ret
>= MODULE_NAME_LEN
, "truncated: '%s' (len %d)", module_name
, ret
))
520 mutex_unlock(&net
->nft
.commit_mutex
);
521 request_module("%s", module_name
);
522 mutex_lock(&net
->nft
.commit_mutex
);
526 static void lockdep_nfnl_nft_mutex_not_held(void)
528 #ifdef CONFIG_PROVE_LOCKING
529 WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES
));
533 static const struct nft_chain_type
*
534 nf_tables_chain_type_lookup(struct net
*net
, const struct nlattr
*nla
,
535 u8 family
, bool autoload
)
537 const struct nft_chain_type
*type
;
539 type
= __nf_tables_chain_type_lookup(nla
, family
);
543 lockdep_nfnl_nft_mutex_not_held();
544 #ifdef CONFIG_MODULES
546 nft_request_module(net
, "nft-chain-%u-%.*s", family
,
547 nla_len(nla
), (const char *)nla_data(nla
));
548 type
= __nf_tables_chain_type_lookup(nla
, family
);
550 return ERR_PTR(-EAGAIN
);
553 return ERR_PTR(-ENOENT
);
556 static const struct nla_policy nft_table_policy
[NFTA_TABLE_MAX
+ 1] = {
557 [NFTA_TABLE_NAME
] = { .type
= NLA_STRING
,
558 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
559 [NFTA_TABLE_FLAGS
] = { .type
= NLA_U32
},
560 [NFTA_TABLE_HANDLE
] = { .type
= NLA_U64
},
563 static int nf_tables_fill_table_info(struct sk_buff
*skb
, struct net
*net
,
564 u32 portid
, u32 seq
, int event
, u32 flags
,
565 int family
, const struct nft_table
*table
)
567 struct nlmsghdr
*nlh
;
568 struct nfgenmsg
*nfmsg
;
570 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
571 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
573 goto nla_put_failure
;
575 nfmsg
= nlmsg_data(nlh
);
576 nfmsg
->nfgen_family
= family
;
577 nfmsg
->version
= NFNETLINK_V0
;
578 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
580 if (nla_put_string(skb
, NFTA_TABLE_NAME
, table
->name
) ||
581 nla_put_be32(skb
, NFTA_TABLE_FLAGS
, htonl(table
->flags
)) ||
582 nla_put_be32(skb
, NFTA_TABLE_USE
, htonl(table
->use
)) ||
583 nla_put_be64(skb
, NFTA_TABLE_HANDLE
, cpu_to_be64(table
->handle
),
585 goto nla_put_failure
;
591 nlmsg_trim(skb
, nlh
);
595 static void nf_tables_table_notify(const struct nft_ctx
*ctx
, int event
)
601 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
604 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
608 err
= nf_tables_fill_table_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
609 event
, 0, ctx
->family
, ctx
->table
);
615 nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
616 ctx
->report
, GFP_KERNEL
);
619 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
622 static int nf_tables_dump_tables(struct sk_buff
*skb
,
623 struct netlink_callback
*cb
)
625 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
626 const struct nft_table
*table
;
627 unsigned int idx
= 0, s_idx
= cb
->args
[0];
628 struct net
*net
= sock_net(skb
->sk
);
629 int family
= nfmsg
->nfgen_family
;
632 cb
->seq
= net
->nft
.base_seq
;
634 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
635 if (family
!= NFPROTO_UNSPEC
&& family
!= table
->family
)
641 memset(&cb
->args
[1], 0,
642 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
643 if (!nft_is_active(net
, table
))
645 if (nf_tables_fill_table_info(skb
, net
,
646 NETLINK_CB(cb
->skb
).portid
,
648 NFT_MSG_NEWTABLE
, NLM_F_MULTI
,
649 table
->family
, table
) < 0)
652 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
662 static int nft_netlink_dump_start_rcu(struct sock
*nlsk
, struct sk_buff
*skb
,
663 const struct nlmsghdr
*nlh
,
664 struct netlink_dump_control
*c
)
668 if (!try_module_get(THIS_MODULE
))
672 err
= netlink_dump_start(nlsk
, skb
, nlh
, c
);
674 module_put(THIS_MODULE
);
679 /* called with rcu_read_lock held */
680 static int nf_tables_gettable(struct net
*net
, struct sock
*nlsk
,
681 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
682 const struct nlattr
* const nla
[],
683 struct netlink_ext_ack
*extack
)
685 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
686 u8 genmask
= nft_genmask_cur(net
);
687 const struct nft_table
*table
;
688 struct sk_buff
*skb2
;
689 int family
= nfmsg
->nfgen_family
;
692 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
693 struct netlink_dump_control c
= {
694 .dump
= nf_tables_dump_tables
,
695 .module
= THIS_MODULE
,
698 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
701 table
= nft_table_lookup(net
, nla
[NFTA_TABLE_NAME
], family
, genmask
);
703 NL_SET_BAD_ATTR(extack
, nla
[NFTA_TABLE_NAME
]);
704 return PTR_ERR(table
);
707 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
711 err
= nf_tables_fill_table_info(skb2
, net
, NETLINK_CB(skb
).portid
,
712 nlh
->nlmsg_seq
, NFT_MSG_NEWTABLE
, 0,
717 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
724 static void nft_table_disable(struct net
*net
, struct nft_table
*table
, u32 cnt
)
726 struct nft_chain
*chain
;
729 list_for_each_entry(chain
, &table
->chains
, list
) {
730 if (!nft_is_active_next(net
, chain
))
732 if (!nft_is_base_chain(chain
))
735 if (cnt
&& i
++ == cnt
)
738 nf_unregister_net_hook(net
, &nft_base_chain(chain
)->ops
);
742 static int nf_tables_table_enable(struct net
*net
, struct nft_table
*table
)
744 struct nft_chain
*chain
;
747 list_for_each_entry(chain
, &table
->chains
, list
) {
748 if (!nft_is_active_next(net
, chain
))
750 if (!nft_is_base_chain(chain
))
753 err
= nf_register_net_hook(net
, &nft_base_chain(chain
)->ops
);
762 nft_table_disable(net
, table
, i
);
766 static void nf_tables_table_disable(struct net
*net
, struct nft_table
*table
)
768 nft_table_disable(net
, table
, 0);
771 static int nf_tables_updtable(struct nft_ctx
*ctx
)
773 struct nft_trans
*trans
;
777 if (!ctx
->nla
[NFTA_TABLE_FLAGS
])
780 flags
= ntohl(nla_get_be32(ctx
->nla
[NFTA_TABLE_FLAGS
]));
781 if (flags
& ~NFT_TABLE_F_DORMANT
)
784 if (flags
== ctx
->table
->flags
)
787 trans
= nft_trans_alloc(ctx
, NFT_MSG_NEWTABLE
,
788 sizeof(struct nft_trans_table
));
792 if ((flags
& NFT_TABLE_F_DORMANT
) &&
793 !(ctx
->table
->flags
& NFT_TABLE_F_DORMANT
)) {
794 nft_trans_table_enable(trans
) = false;
795 } else if (!(flags
& NFT_TABLE_F_DORMANT
) &&
796 ctx
->table
->flags
& NFT_TABLE_F_DORMANT
) {
797 ret
= nf_tables_table_enable(ctx
->net
, ctx
->table
);
799 ctx
->table
->flags
&= ~NFT_TABLE_F_DORMANT
;
800 nft_trans_table_enable(trans
) = true;
806 nft_trans_table_update(trans
) = true;
807 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
810 nft_trans_destroy(trans
);
814 static u32
nft_chain_hash(const void *data
, u32 len
, u32 seed
)
816 const char *name
= data
;
818 return jhash(name
, strlen(name
), seed
);
821 static u32
nft_chain_hash_obj(const void *data
, u32 len
, u32 seed
)
823 const struct nft_chain
*chain
= data
;
825 return nft_chain_hash(chain
->name
, 0, seed
);
828 static int nft_chain_hash_cmp(struct rhashtable_compare_arg
*arg
,
831 const struct nft_chain
*chain
= ptr
;
832 const char *name
= arg
->key
;
834 return strcmp(chain
->name
, name
);
837 static u32
nft_objname_hash(const void *data
, u32 len
, u32 seed
)
839 const struct nft_object_hash_key
*k
= data
;
841 seed
^= hash_ptr(k
->table
, 32);
843 return jhash(k
->name
, strlen(k
->name
), seed
);
846 static u32
nft_objname_hash_obj(const void *data
, u32 len
, u32 seed
)
848 const struct nft_object
*obj
= data
;
850 return nft_objname_hash(&obj
->key
, 0, seed
);
853 static int nft_objname_hash_cmp(struct rhashtable_compare_arg
*arg
,
856 const struct nft_object_hash_key
*k
= arg
->key
;
857 const struct nft_object
*obj
= ptr
;
859 if (obj
->key
.table
!= k
->table
)
862 return strcmp(obj
->key
.name
, k
->name
);
865 static int nf_tables_newtable(struct net
*net
, struct sock
*nlsk
,
866 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
867 const struct nlattr
* const nla
[],
868 struct netlink_ext_ack
*extack
)
870 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
871 u8 genmask
= nft_genmask_next(net
);
872 int family
= nfmsg
->nfgen_family
;
873 const struct nlattr
*attr
;
874 struct nft_table
*table
;
879 lockdep_assert_held(&net
->nft
.commit_mutex
);
880 attr
= nla
[NFTA_TABLE_NAME
];
881 table
= nft_table_lookup(net
, attr
, family
, genmask
);
883 if (PTR_ERR(table
) != -ENOENT
)
884 return PTR_ERR(table
);
886 if (nlh
->nlmsg_flags
& NLM_F_EXCL
) {
887 NL_SET_BAD_ATTR(extack
, attr
);
890 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
893 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
894 return nf_tables_updtable(&ctx
);
897 if (nla
[NFTA_TABLE_FLAGS
]) {
898 flags
= ntohl(nla_get_be32(nla
[NFTA_TABLE_FLAGS
]));
899 if (flags
& ~NFT_TABLE_F_DORMANT
)
904 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
908 table
->name
= nla_strdup(attr
, GFP_KERNEL
);
909 if (table
->name
== NULL
)
912 err
= rhltable_init(&table
->chains_ht
, &nft_chain_ht_params
);
916 INIT_LIST_HEAD(&table
->chains
);
917 INIT_LIST_HEAD(&table
->sets
);
918 INIT_LIST_HEAD(&table
->objects
);
919 INIT_LIST_HEAD(&table
->flowtables
);
920 table
->family
= family
;
921 table
->flags
= flags
;
922 table
->handle
= ++table_handle
;
924 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
925 err
= nft_trans_table_add(&ctx
, NFT_MSG_NEWTABLE
);
929 list_add_tail_rcu(&table
->list
, &net
->nft
.tables
);
932 rhltable_destroy(&table
->chains_ht
);
941 static int nft_flush_table(struct nft_ctx
*ctx
)
943 struct nft_flowtable
*flowtable
, *nft
;
944 struct nft_chain
*chain
, *nc
;
945 struct nft_object
*obj
, *ne
;
946 struct nft_set
*set
, *ns
;
949 list_for_each_entry(chain
, &ctx
->table
->chains
, list
) {
950 if (!nft_is_active_next(ctx
->net
, chain
))
955 err
= nft_delrule_by_chain(ctx
);
960 list_for_each_entry_safe(set
, ns
, &ctx
->table
->sets
, list
) {
961 if (!nft_is_active_next(ctx
->net
, set
))
964 if (nft_set_is_anonymous(set
) &&
965 !list_empty(&set
->bindings
))
968 err
= nft_delset(ctx
, set
);
973 list_for_each_entry_safe(flowtable
, nft
, &ctx
->table
->flowtables
, list
) {
974 err
= nft_delflowtable(ctx
, flowtable
);
979 list_for_each_entry_safe(obj
, ne
, &ctx
->table
->objects
, list
) {
980 err
= nft_delobj(ctx
, obj
);
985 list_for_each_entry_safe(chain
, nc
, &ctx
->table
->chains
, list
) {
986 if (!nft_is_active_next(ctx
->net
, chain
))
991 err
= nft_delchain(ctx
);
996 err
= nft_deltable(ctx
);
1001 static int nft_flush(struct nft_ctx
*ctx
, int family
)
1003 struct nft_table
*table
, *nt
;
1004 const struct nlattr
* const *nla
= ctx
->nla
;
1007 list_for_each_entry_safe(table
, nt
, &ctx
->net
->nft
.tables
, list
) {
1008 if (family
!= AF_UNSPEC
&& table
->family
!= family
)
1011 ctx
->family
= table
->family
;
1013 if (!nft_is_active_next(ctx
->net
, table
))
1016 if (nla
[NFTA_TABLE_NAME
] &&
1017 nla_strcmp(nla
[NFTA_TABLE_NAME
], table
->name
) != 0)
1022 err
= nft_flush_table(ctx
);
1030 static int nf_tables_deltable(struct net
*net
, struct sock
*nlsk
,
1031 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1032 const struct nlattr
* const nla
[],
1033 struct netlink_ext_ack
*extack
)
1035 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1036 u8 genmask
= nft_genmask_next(net
);
1037 int family
= nfmsg
->nfgen_family
;
1038 const struct nlattr
*attr
;
1039 struct nft_table
*table
;
1042 nft_ctx_init(&ctx
, net
, skb
, nlh
, 0, NULL
, NULL
, nla
);
1043 if (family
== AF_UNSPEC
||
1044 (!nla
[NFTA_TABLE_NAME
] && !nla
[NFTA_TABLE_HANDLE
]))
1045 return nft_flush(&ctx
, family
);
1047 if (nla
[NFTA_TABLE_HANDLE
]) {
1048 attr
= nla
[NFTA_TABLE_HANDLE
];
1049 table
= nft_table_lookup_byhandle(net
, attr
, genmask
);
1051 attr
= nla
[NFTA_TABLE_NAME
];
1052 table
= nft_table_lookup(net
, attr
, family
, genmask
);
1055 if (IS_ERR(table
)) {
1056 NL_SET_BAD_ATTR(extack
, attr
);
1057 return PTR_ERR(table
);
1060 if (nlh
->nlmsg_flags
& NLM_F_NONREC
&&
1064 ctx
.family
= family
;
1067 return nft_flush_table(&ctx
);
1070 static void nf_tables_table_destroy(struct nft_ctx
*ctx
)
1072 if (WARN_ON(ctx
->table
->use
> 0))
1075 rhltable_destroy(&ctx
->table
->chains_ht
);
1076 kfree(ctx
->table
->name
);
1080 void nft_register_chain_type(const struct nft_chain_type
*ctype
)
1082 if (WARN_ON(ctype
->family
>= NFPROTO_NUMPROTO
))
1085 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1086 if (WARN_ON(chain_type
[ctype
->family
][ctype
->type
] != NULL
)) {
1087 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1090 chain_type
[ctype
->family
][ctype
->type
] = ctype
;
1091 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1093 EXPORT_SYMBOL_GPL(nft_register_chain_type
);
1095 void nft_unregister_chain_type(const struct nft_chain_type
*ctype
)
1097 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1098 chain_type
[ctype
->family
][ctype
->type
] = NULL
;
1099 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1101 EXPORT_SYMBOL_GPL(nft_unregister_chain_type
);
1107 static struct nft_chain
*
1108 nft_chain_lookup_byhandle(const struct nft_table
*table
, u64 handle
, u8 genmask
)
1110 struct nft_chain
*chain
;
1112 list_for_each_entry(chain
, &table
->chains
, list
) {
1113 if (chain
->handle
== handle
&&
1114 nft_active_genmask(chain
, genmask
))
1118 return ERR_PTR(-ENOENT
);
1121 static bool lockdep_commit_lock_is_held(const struct net
*net
)
1123 #ifdef CONFIG_PROVE_LOCKING
1124 return lockdep_is_held(&net
->nft
.commit_mutex
);
1130 static struct nft_chain
*nft_chain_lookup(struct net
*net
,
1131 struct nft_table
*table
,
1132 const struct nlattr
*nla
, u8 genmask
)
1134 char search
[NFT_CHAIN_MAXNAMELEN
+ 1];
1135 struct rhlist_head
*tmp
, *list
;
1136 struct nft_chain
*chain
;
1139 return ERR_PTR(-EINVAL
);
1141 nla_strlcpy(search
, nla
, sizeof(search
));
1143 WARN_ON(!rcu_read_lock_held() &&
1144 !lockdep_commit_lock_is_held(net
));
1146 chain
= ERR_PTR(-ENOENT
);
1148 list
= rhltable_lookup(&table
->chains_ht
, search
, nft_chain_ht_params
);
1152 rhl_for_each_entry_rcu(chain
, tmp
, list
, rhlhead
) {
1153 if (nft_active_genmask(chain
, genmask
))
1156 chain
= ERR_PTR(-ENOENT
);
1162 static const struct nla_policy nft_chain_policy
[NFTA_CHAIN_MAX
+ 1] = {
1163 [NFTA_CHAIN_TABLE
] = { .type
= NLA_STRING
,
1164 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
1165 [NFTA_CHAIN_HANDLE
] = { .type
= NLA_U64
},
1166 [NFTA_CHAIN_NAME
] = { .type
= NLA_STRING
,
1167 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
1168 [NFTA_CHAIN_HOOK
] = { .type
= NLA_NESTED
},
1169 [NFTA_CHAIN_POLICY
] = { .type
= NLA_U32
},
1170 [NFTA_CHAIN_TYPE
] = { .type
= NLA_STRING
},
1171 [NFTA_CHAIN_COUNTERS
] = { .type
= NLA_NESTED
},
1174 static const struct nla_policy nft_hook_policy
[NFTA_HOOK_MAX
+ 1] = {
1175 [NFTA_HOOK_HOOKNUM
] = { .type
= NLA_U32
},
1176 [NFTA_HOOK_PRIORITY
] = { .type
= NLA_U32
},
1177 [NFTA_HOOK_DEV
] = { .type
= NLA_STRING
,
1178 .len
= IFNAMSIZ
- 1 },
1181 static int nft_dump_stats(struct sk_buff
*skb
, struct nft_stats __percpu
*stats
)
1183 struct nft_stats
*cpu_stats
, total
;
1184 struct nlattr
*nest
;
1192 memset(&total
, 0, sizeof(total
));
1193 for_each_possible_cpu(cpu
) {
1194 cpu_stats
= per_cpu_ptr(stats
, cpu
);
1196 seq
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
1197 pkts
= cpu_stats
->pkts
;
1198 bytes
= cpu_stats
->bytes
;
1199 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, seq
));
1201 total
.bytes
+= bytes
;
1203 nest
= nla_nest_start_noflag(skb
, NFTA_CHAIN_COUNTERS
);
1205 goto nla_put_failure
;
1207 if (nla_put_be64(skb
, NFTA_COUNTER_PACKETS
, cpu_to_be64(total
.pkts
),
1208 NFTA_COUNTER_PAD
) ||
1209 nla_put_be64(skb
, NFTA_COUNTER_BYTES
, cpu_to_be64(total
.bytes
),
1211 goto nla_put_failure
;
1213 nla_nest_end(skb
, nest
);
1220 static int nf_tables_fill_chain_info(struct sk_buff
*skb
, struct net
*net
,
1221 u32 portid
, u32 seq
, int event
, u32 flags
,
1222 int family
, const struct nft_table
*table
,
1223 const struct nft_chain
*chain
)
1225 struct nlmsghdr
*nlh
;
1226 struct nfgenmsg
*nfmsg
;
1228 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
1229 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
1231 goto nla_put_failure
;
1233 nfmsg
= nlmsg_data(nlh
);
1234 nfmsg
->nfgen_family
= family
;
1235 nfmsg
->version
= NFNETLINK_V0
;
1236 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
1238 if (nla_put_string(skb
, NFTA_CHAIN_TABLE
, table
->name
))
1239 goto nla_put_failure
;
1240 if (nla_put_be64(skb
, NFTA_CHAIN_HANDLE
, cpu_to_be64(chain
->handle
),
1242 goto nla_put_failure
;
1243 if (nla_put_string(skb
, NFTA_CHAIN_NAME
, chain
->name
))
1244 goto nla_put_failure
;
1246 if (nft_is_base_chain(chain
)) {
1247 const struct nft_base_chain
*basechain
= nft_base_chain(chain
);
1248 const struct nf_hook_ops
*ops
= &basechain
->ops
;
1249 struct nft_stats __percpu
*stats
;
1250 struct nlattr
*nest
;
1252 nest
= nla_nest_start_noflag(skb
, NFTA_CHAIN_HOOK
);
1254 goto nla_put_failure
;
1255 if (nla_put_be32(skb
, NFTA_HOOK_HOOKNUM
, htonl(ops
->hooknum
)))
1256 goto nla_put_failure
;
1257 if (nla_put_be32(skb
, NFTA_HOOK_PRIORITY
, htonl(ops
->priority
)))
1258 goto nla_put_failure
;
1259 if (basechain
->dev_name
[0] &&
1260 nla_put_string(skb
, NFTA_HOOK_DEV
, basechain
->dev_name
))
1261 goto nla_put_failure
;
1262 nla_nest_end(skb
, nest
);
1264 if (nla_put_be32(skb
, NFTA_CHAIN_POLICY
,
1265 htonl(basechain
->policy
)))
1266 goto nla_put_failure
;
1268 if (nla_put_string(skb
, NFTA_CHAIN_TYPE
, basechain
->type
->name
))
1269 goto nla_put_failure
;
1271 stats
= rcu_dereference_check(basechain
->stats
,
1272 lockdep_commit_lock_is_held(net
));
1273 if (nft_dump_stats(skb
, stats
))
1274 goto nla_put_failure
;
1277 if (nla_put_be32(skb
, NFTA_CHAIN_USE
, htonl(chain
->use
)))
1278 goto nla_put_failure
;
1280 nlmsg_end(skb
, nlh
);
1284 nlmsg_trim(skb
, nlh
);
1288 static void nf_tables_chain_notify(const struct nft_ctx
*ctx
, int event
)
1290 struct sk_buff
*skb
;
1294 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
1297 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1301 err
= nf_tables_fill_chain_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
1302 event
, 0, ctx
->family
, ctx
->table
,
1309 nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1310 ctx
->report
, GFP_KERNEL
);
1313 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
1316 static int nf_tables_dump_chains(struct sk_buff
*skb
,
1317 struct netlink_callback
*cb
)
1319 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1320 const struct nft_table
*table
;
1321 const struct nft_chain
*chain
;
1322 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1323 struct net
*net
= sock_net(skb
->sk
);
1324 int family
= nfmsg
->nfgen_family
;
1327 cb
->seq
= net
->nft
.base_seq
;
1329 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
1330 if (family
!= NFPROTO_UNSPEC
&& family
!= table
->family
)
1333 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
1337 memset(&cb
->args
[1], 0,
1338 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1339 if (!nft_is_active(net
, chain
))
1341 if (nf_tables_fill_chain_info(skb
, net
,
1342 NETLINK_CB(cb
->skb
).portid
,
1346 table
->family
, table
,
1350 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1361 /* called with rcu_read_lock held */
1362 static int nf_tables_getchain(struct net
*net
, struct sock
*nlsk
,
1363 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1364 const struct nlattr
* const nla
[],
1365 struct netlink_ext_ack
*extack
)
1367 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1368 u8 genmask
= nft_genmask_cur(net
);
1369 const struct nft_chain
*chain
;
1370 struct nft_table
*table
;
1371 struct sk_buff
*skb2
;
1372 int family
= nfmsg
->nfgen_family
;
1375 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1376 struct netlink_dump_control c
= {
1377 .dump
= nf_tables_dump_chains
,
1378 .module
= THIS_MODULE
,
1381 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
1384 table
= nft_table_lookup(net
, nla
[NFTA_CHAIN_TABLE
], family
, genmask
);
1385 if (IS_ERR(table
)) {
1386 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_TABLE
]);
1387 return PTR_ERR(table
);
1390 chain
= nft_chain_lookup(net
, table
, nla
[NFTA_CHAIN_NAME
], genmask
);
1391 if (IS_ERR(chain
)) {
1392 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_NAME
]);
1393 return PTR_ERR(chain
);
1396 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1400 err
= nf_tables_fill_chain_info(skb2
, net
, NETLINK_CB(skb
).portid
,
1401 nlh
->nlmsg_seq
, NFT_MSG_NEWCHAIN
, 0,
1402 family
, table
, chain
);
1406 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
1413 static const struct nla_policy nft_counter_policy
[NFTA_COUNTER_MAX
+ 1] = {
1414 [NFTA_COUNTER_PACKETS
] = { .type
= NLA_U64
},
1415 [NFTA_COUNTER_BYTES
] = { .type
= NLA_U64
},
1418 static struct nft_stats __percpu
*nft_stats_alloc(const struct nlattr
*attr
)
1420 struct nlattr
*tb
[NFTA_COUNTER_MAX
+1];
1421 struct nft_stats __percpu
*newstats
;
1422 struct nft_stats
*stats
;
1425 err
= nla_parse_nested_deprecated(tb
, NFTA_COUNTER_MAX
, attr
,
1426 nft_counter_policy
, NULL
);
1428 return ERR_PTR(err
);
1430 if (!tb
[NFTA_COUNTER_BYTES
] || !tb
[NFTA_COUNTER_PACKETS
])
1431 return ERR_PTR(-EINVAL
);
1433 newstats
= netdev_alloc_pcpu_stats(struct nft_stats
);
1434 if (newstats
== NULL
)
1435 return ERR_PTR(-ENOMEM
);
1437 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1438 * are not exposed to userspace.
1441 stats
= this_cpu_ptr(newstats
);
1442 stats
->bytes
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_BYTES
]));
1443 stats
->pkts
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_PACKETS
]));
1449 static void nft_chain_stats_replace(struct nft_trans
*trans
)
1451 struct nft_base_chain
*chain
= nft_base_chain(trans
->ctx
.chain
);
1453 if (!nft_trans_chain_stats(trans
))
1456 rcu_swap_protected(chain
->stats
, nft_trans_chain_stats(trans
),
1457 lockdep_commit_lock_is_held(trans
->ctx
.net
));
1459 if (!nft_trans_chain_stats(trans
))
1460 static_branch_inc(&nft_counters_enabled
);
1463 static void nf_tables_chain_free_chain_rules(struct nft_chain
*chain
)
1465 struct nft_rule
**g0
= rcu_dereference_raw(chain
->rules_gen_0
);
1466 struct nft_rule
**g1
= rcu_dereference_raw(chain
->rules_gen_1
);
1472 /* should be NULL either via abort or via successful commit */
1473 WARN_ON_ONCE(chain
->rules_next
);
1474 kvfree(chain
->rules_next
);
1477 static void nf_tables_chain_destroy(struct nft_ctx
*ctx
)
1479 struct nft_chain
*chain
= ctx
->chain
;
1481 if (WARN_ON(chain
->use
> 0))
1484 /* no concurrent access possible anymore */
1485 nf_tables_chain_free_chain_rules(chain
);
1487 if (nft_is_base_chain(chain
)) {
1488 struct nft_base_chain
*basechain
= nft_base_chain(chain
);
1490 module_put(basechain
->type
->owner
);
1491 if (rcu_access_pointer(basechain
->stats
)) {
1492 static_branch_dec(&nft_counters_enabled
);
1493 free_percpu(rcu_dereference_raw(basechain
->stats
));
1503 struct nft_chain_hook
{
1506 const struct nft_chain_type
*type
;
1507 struct net_device
*dev
;
1510 static int nft_chain_parse_hook(struct net
*net
,
1511 const struct nlattr
* const nla
[],
1512 struct nft_chain_hook
*hook
, u8 family
,
1515 struct nlattr
*ha
[NFTA_HOOK_MAX
+ 1];
1516 const struct nft_chain_type
*type
;
1517 struct net_device
*dev
;
1520 lockdep_assert_held(&net
->nft
.commit_mutex
);
1521 lockdep_nfnl_nft_mutex_not_held();
1523 err
= nla_parse_nested_deprecated(ha
, NFTA_HOOK_MAX
,
1524 nla
[NFTA_CHAIN_HOOK
],
1525 nft_hook_policy
, NULL
);
1529 if (ha
[NFTA_HOOK_HOOKNUM
] == NULL
||
1530 ha
[NFTA_HOOK_PRIORITY
] == NULL
)
1533 hook
->num
= ntohl(nla_get_be32(ha
[NFTA_HOOK_HOOKNUM
]));
1534 hook
->priority
= ntohl(nla_get_be32(ha
[NFTA_HOOK_PRIORITY
]));
1536 type
= chain_type
[family
][NFT_CHAIN_T_DEFAULT
];
1537 if (nla
[NFTA_CHAIN_TYPE
]) {
1538 type
= nf_tables_chain_type_lookup(net
, nla
[NFTA_CHAIN_TYPE
],
1541 return PTR_ERR(type
);
1543 if (hook
->num
> NF_MAX_HOOKS
|| !(type
->hook_mask
& (1 << hook
->num
)))
1546 if (type
->type
== NFT_CHAIN_T_NAT
&&
1547 hook
->priority
<= NF_IP_PRI_CONNTRACK
)
1550 if (!try_module_get(type
->owner
))
1556 if (family
== NFPROTO_NETDEV
) {
1557 char ifname
[IFNAMSIZ
];
1559 if (!ha
[NFTA_HOOK_DEV
]) {
1560 module_put(type
->owner
);
1564 nla_strlcpy(ifname
, ha
[NFTA_HOOK_DEV
], IFNAMSIZ
);
1565 dev
= __dev_get_by_name(net
, ifname
);
1567 module_put(type
->owner
);
1571 } else if (ha
[NFTA_HOOK_DEV
]) {
1572 module_put(type
->owner
);
1579 static void nft_chain_release_hook(struct nft_chain_hook
*hook
)
1581 module_put(hook
->type
->owner
);
1584 struct nft_rules_old
{
1586 struct nft_rule
**start
;
1589 static struct nft_rule
**nf_tables_chain_alloc_rules(const struct nft_chain
*chain
,
1592 if (alloc
> INT_MAX
)
1595 alloc
+= 1; /* NULL, ends rules */
1596 if (sizeof(struct nft_rule
*) > INT_MAX
/ alloc
)
1599 alloc
*= sizeof(struct nft_rule
*);
1600 alloc
+= sizeof(struct nft_rules_old
);
1602 return kvmalloc(alloc
, GFP_KERNEL
);
1605 static int nf_tables_addchain(struct nft_ctx
*ctx
, u8 family
, u8 genmask
,
1608 const struct nlattr
* const *nla
= ctx
->nla
;
1609 struct nft_table
*table
= ctx
->table
;
1610 struct nft_base_chain
*basechain
;
1611 struct nft_stats __percpu
*stats
;
1612 struct net
*net
= ctx
->net
;
1613 struct nft_trans
*trans
;
1614 struct nft_chain
*chain
;
1615 struct nft_rule
**rules
;
1618 if (table
->use
== UINT_MAX
)
1621 if (nla
[NFTA_CHAIN_HOOK
]) {
1622 struct nft_chain_hook hook
;
1623 struct nf_hook_ops
*ops
;
1625 err
= nft_chain_parse_hook(net
, nla
, &hook
, family
, true);
1629 basechain
= kzalloc(sizeof(*basechain
), GFP_KERNEL
);
1630 if (basechain
== NULL
) {
1631 nft_chain_release_hook(&hook
);
1635 if (hook
.dev
!= NULL
)
1636 strncpy(basechain
->dev_name
, hook
.dev
->name
, IFNAMSIZ
);
1638 if (nla
[NFTA_CHAIN_COUNTERS
]) {
1639 stats
= nft_stats_alloc(nla
[NFTA_CHAIN_COUNTERS
]);
1640 if (IS_ERR(stats
)) {
1641 nft_chain_release_hook(&hook
);
1643 return PTR_ERR(stats
);
1645 rcu_assign_pointer(basechain
->stats
, stats
);
1646 static_branch_inc(&nft_counters_enabled
);
1649 basechain
->type
= hook
.type
;
1650 chain
= &basechain
->chain
;
1652 ops
= &basechain
->ops
;
1654 ops
->hooknum
= hook
.num
;
1655 ops
->priority
= hook
.priority
;
1657 ops
->hook
= hook
.type
->hooks
[ops
->hooknum
];
1658 ops
->dev
= hook
.dev
;
1660 chain
->flags
|= NFT_BASE_CHAIN
;
1661 basechain
->policy
= NF_ACCEPT
;
1663 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
1669 INIT_LIST_HEAD(&chain
->rules
);
1670 chain
->handle
= nf_tables_alloc_handle(table
);
1671 chain
->table
= table
;
1672 chain
->name
= nla_strdup(nla
[NFTA_CHAIN_NAME
], GFP_KERNEL
);
1678 rules
= nf_tables_chain_alloc_rules(chain
, 0);
1685 rcu_assign_pointer(chain
->rules_gen_0
, rules
);
1686 rcu_assign_pointer(chain
->rules_gen_1
, rules
);
1688 err
= nf_tables_register_hook(net
, table
, chain
);
1692 err
= rhltable_insert_key(&table
->chains_ht
, chain
->name
,
1693 &chain
->rhlhead
, nft_chain_ht_params
);
1697 trans
= nft_trans_chain_add(ctx
, NFT_MSG_NEWCHAIN
);
1698 if (IS_ERR(trans
)) {
1699 err
= PTR_ERR(trans
);
1700 rhltable_remove(&table
->chains_ht
, &chain
->rhlhead
,
1701 nft_chain_ht_params
);
1705 nft_trans_chain_policy(trans
) = -1;
1706 if (nft_is_base_chain(chain
))
1707 nft_trans_chain_policy(trans
) = policy
;
1710 list_add_tail_rcu(&chain
->list
, &table
->chains
);
1714 nf_tables_unregister_hook(net
, table
, chain
);
1716 nf_tables_chain_destroy(ctx
);
1721 static int nf_tables_updchain(struct nft_ctx
*ctx
, u8 genmask
, u8 policy
)
1723 const struct nlattr
* const *nla
= ctx
->nla
;
1724 struct nft_table
*table
= ctx
->table
;
1725 struct nft_chain
*chain
= ctx
->chain
;
1726 struct nft_base_chain
*basechain
;
1727 struct nft_stats
*stats
= NULL
;
1728 struct nft_chain_hook hook
;
1729 struct nf_hook_ops
*ops
;
1730 struct nft_trans
*trans
;
1733 if (nla
[NFTA_CHAIN_HOOK
]) {
1734 if (!nft_is_base_chain(chain
))
1737 err
= nft_chain_parse_hook(ctx
->net
, nla
, &hook
, ctx
->family
,
1742 basechain
= nft_base_chain(chain
);
1743 if (basechain
->type
!= hook
.type
) {
1744 nft_chain_release_hook(&hook
);
1748 ops
= &basechain
->ops
;
1749 if (ops
->hooknum
!= hook
.num
||
1750 ops
->priority
!= hook
.priority
||
1751 ops
->dev
!= hook
.dev
) {
1752 nft_chain_release_hook(&hook
);
1755 nft_chain_release_hook(&hook
);
1758 if (nla
[NFTA_CHAIN_HANDLE
] &&
1759 nla
[NFTA_CHAIN_NAME
]) {
1760 struct nft_chain
*chain2
;
1762 chain2
= nft_chain_lookup(ctx
->net
, table
,
1763 nla
[NFTA_CHAIN_NAME
], genmask
);
1764 if (!IS_ERR(chain2
))
1768 if (nla
[NFTA_CHAIN_COUNTERS
]) {
1769 if (!nft_is_base_chain(chain
))
1772 stats
= nft_stats_alloc(nla
[NFTA_CHAIN_COUNTERS
]);
1774 return PTR_ERR(stats
);
1778 trans
= nft_trans_alloc(ctx
, NFT_MSG_NEWCHAIN
,
1779 sizeof(struct nft_trans_chain
));
1783 nft_trans_chain_stats(trans
) = stats
;
1784 nft_trans_chain_update(trans
) = true;
1786 if (nla
[NFTA_CHAIN_POLICY
])
1787 nft_trans_chain_policy(trans
) = policy
;
1789 nft_trans_chain_policy(trans
) = -1;
1791 if (nla
[NFTA_CHAIN_HANDLE
] &&
1792 nla
[NFTA_CHAIN_NAME
]) {
1793 struct nft_trans
*tmp
;
1797 name
= nla_strdup(nla
[NFTA_CHAIN_NAME
], GFP_KERNEL
);
1802 list_for_each_entry(tmp
, &ctx
->net
->nft
.commit_list
, list
) {
1803 if (tmp
->msg_type
== NFT_MSG_NEWCHAIN
&&
1804 tmp
->ctx
.table
== table
&&
1805 nft_trans_chain_update(tmp
) &&
1806 nft_trans_chain_name(tmp
) &&
1807 strcmp(name
, nft_trans_chain_name(tmp
)) == 0) {
1813 nft_trans_chain_name(trans
) = name
;
1815 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
1824 static int nf_tables_newchain(struct net
*net
, struct sock
*nlsk
,
1825 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1826 const struct nlattr
* const nla
[],
1827 struct netlink_ext_ack
*extack
)
1829 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1830 u8 genmask
= nft_genmask_next(net
);
1831 int family
= nfmsg
->nfgen_family
;
1832 const struct nlattr
*attr
;
1833 struct nft_table
*table
;
1834 struct nft_chain
*chain
;
1835 u8 policy
= NF_ACCEPT
;
1839 lockdep_assert_held(&net
->nft
.commit_mutex
);
1841 table
= nft_table_lookup(net
, nla
[NFTA_CHAIN_TABLE
], family
, genmask
);
1842 if (IS_ERR(table
)) {
1843 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_TABLE
]);
1844 return PTR_ERR(table
);
1848 attr
= nla
[NFTA_CHAIN_NAME
];
1850 if (nla
[NFTA_CHAIN_HANDLE
]) {
1851 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_CHAIN_HANDLE
]));
1852 chain
= nft_chain_lookup_byhandle(table
, handle
, genmask
);
1853 if (IS_ERR(chain
)) {
1854 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_HANDLE
]);
1855 return PTR_ERR(chain
);
1857 attr
= nla
[NFTA_CHAIN_HANDLE
];
1859 chain
= nft_chain_lookup(net
, table
, attr
, genmask
);
1860 if (IS_ERR(chain
)) {
1861 if (PTR_ERR(chain
) != -ENOENT
) {
1862 NL_SET_BAD_ATTR(extack
, attr
);
1863 return PTR_ERR(chain
);
1869 if (nla
[NFTA_CHAIN_POLICY
]) {
1870 if (chain
!= NULL
&&
1871 !nft_is_base_chain(chain
)) {
1872 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_POLICY
]);
1876 if (chain
== NULL
&&
1877 nla
[NFTA_CHAIN_HOOK
] == NULL
) {
1878 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_POLICY
]);
1882 policy
= ntohl(nla_get_be32(nla
[NFTA_CHAIN_POLICY
]));
1892 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, chain
, nla
);
1894 if (chain
!= NULL
) {
1895 if (nlh
->nlmsg_flags
& NLM_F_EXCL
) {
1896 NL_SET_BAD_ATTR(extack
, attr
);
1899 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1902 return nf_tables_updchain(&ctx
, genmask
, policy
);
1905 return nf_tables_addchain(&ctx
, family
, genmask
, policy
);
1908 static int nf_tables_delchain(struct net
*net
, struct sock
*nlsk
,
1909 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1910 const struct nlattr
* const nla
[],
1911 struct netlink_ext_ack
*extack
)
1913 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1914 u8 genmask
= nft_genmask_next(net
);
1915 int family
= nfmsg
->nfgen_family
;
1916 const struct nlattr
*attr
;
1917 struct nft_table
*table
;
1918 struct nft_chain
*chain
;
1919 struct nft_rule
*rule
;
1925 table
= nft_table_lookup(net
, nla
[NFTA_CHAIN_TABLE
], family
, genmask
);
1926 if (IS_ERR(table
)) {
1927 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_TABLE
]);
1928 return PTR_ERR(table
);
1931 if (nla
[NFTA_CHAIN_HANDLE
]) {
1932 attr
= nla
[NFTA_CHAIN_HANDLE
];
1933 handle
= be64_to_cpu(nla_get_be64(attr
));
1934 chain
= nft_chain_lookup_byhandle(table
, handle
, genmask
);
1936 attr
= nla
[NFTA_CHAIN_NAME
];
1937 chain
= nft_chain_lookup(net
, table
, attr
, genmask
);
1939 if (IS_ERR(chain
)) {
1940 NL_SET_BAD_ATTR(extack
, attr
);
1941 return PTR_ERR(chain
);
1944 if (nlh
->nlmsg_flags
& NLM_F_NONREC
&&
1948 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, chain
, nla
);
1951 list_for_each_entry(rule
, &chain
->rules
, list
) {
1952 if (!nft_is_active_next(net
, rule
))
1956 err
= nft_delrule(&ctx
, rule
);
1961 /* There are rules and elements that are still holding references to us,
1962 * we cannot do a recursive removal in this case.
1965 NL_SET_BAD_ATTR(extack
, attr
);
1969 return nft_delchain(&ctx
);
1977 * nft_register_expr - register nf_tables expr type
1980 * Registers the expr type for use with nf_tables. Returns zero on
1981 * success or a negative errno code otherwise.
1983 int nft_register_expr(struct nft_expr_type
*type
)
1985 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1986 if (type
->family
== NFPROTO_UNSPEC
)
1987 list_add_tail_rcu(&type
->list
, &nf_tables_expressions
);
1989 list_add_rcu(&type
->list
, &nf_tables_expressions
);
1990 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1993 EXPORT_SYMBOL_GPL(nft_register_expr
);
1996 * nft_unregister_expr - unregister nf_tables expr type
1999 * Unregisters the expr typefor use with nf_tables.
2001 void nft_unregister_expr(struct nft_expr_type
*type
)
2003 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2004 list_del_rcu(&type
->list
);
2005 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2007 EXPORT_SYMBOL_GPL(nft_unregister_expr
);
2009 static const struct nft_expr_type
*__nft_expr_type_get(u8 family
,
2012 const struct nft_expr_type
*type
, *candidate
= NULL
;
2014 list_for_each_entry(type
, &nf_tables_expressions
, list
) {
2015 if (!nla_strcmp(nla
, type
->name
)) {
2016 if (!type
->family
&& !candidate
)
2018 else if (type
->family
== family
)
2025 #ifdef CONFIG_MODULES
2026 static int nft_expr_type_request_module(struct net
*net
, u8 family
,
2029 nft_request_module(net
, "nft-expr-%u-%.*s", family
,
2030 nla_len(nla
), (char *)nla_data(nla
));
2031 if (__nft_expr_type_get(family
, nla
))
2038 static const struct nft_expr_type
*nft_expr_type_get(struct net
*net
,
2042 const struct nft_expr_type
*type
;
2045 return ERR_PTR(-EINVAL
);
2047 type
= __nft_expr_type_get(family
, nla
);
2048 if (type
!= NULL
&& try_module_get(type
->owner
))
2051 lockdep_nfnl_nft_mutex_not_held();
2052 #ifdef CONFIG_MODULES
2054 if (nft_expr_type_request_module(net
, family
, nla
) == -EAGAIN
)
2055 return ERR_PTR(-EAGAIN
);
2057 nft_request_module(net
, "nft-expr-%.*s",
2058 nla_len(nla
), (char *)nla_data(nla
));
2059 if (__nft_expr_type_get(family
, nla
))
2060 return ERR_PTR(-EAGAIN
);
2063 return ERR_PTR(-ENOENT
);
2066 static const struct nla_policy nft_expr_policy
[NFTA_EXPR_MAX
+ 1] = {
2067 [NFTA_EXPR_NAME
] = { .type
= NLA_STRING
},
2068 [NFTA_EXPR_DATA
] = { .type
= NLA_NESTED
},
2071 static int nf_tables_fill_expr_info(struct sk_buff
*skb
,
2072 const struct nft_expr
*expr
)
2074 if (nla_put_string(skb
, NFTA_EXPR_NAME
, expr
->ops
->type
->name
))
2075 goto nla_put_failure
;
2077 if (expr
->ops
->dump
) {
2078 struct nlattr
*data
= nla_nest_start_noflag(skb
,
2081 goto nla_put_failure
;
2082 if (expr
->ops
->dump(skb
, expr
) < 0)
2083 goto nla_put_failure
;
2084 nla_nest_end(skb
, data
);
2093 int nft_expr_dump(struct sk_buff
*skb
, unsigned int attr
,
2094 const struct nft_expr
*expr
)
2096 struct nlattr
*nest
;
2098 nest
= nla_nest_start_noflag(skb
, attr
);
2100 goto nla_put_failure
;
2101 if (nf_tables_fill_expr_info(skb
, expr
) < 0)
2102 goto nla_put_failure
;
2103 nla_nest_end(skb
, nest
);
2110 struct nft_expr_info
{
2111 const struct nft_expr_ops
*ops
;
2112 struct nlattr
*tb
[NFT_EXPR_MAXATTR
+ 1];
2115 static int nf_tables_expr_parse(const struct nft_ctx
*ctx
,
2116 const struct nlattr
*nla
,
2117 struct nft_expr_info
*info
)
2119 const struct nft_expr_type
*type
;
2120 const struct nft_expr_ops
*ops
;
2121 struct nlattr
*tb
[NFTA_EXPR_MAX
+ 1];
2124 err
= nla_parse_nested_deprecated(tb
, NFTA_EXPR_MAX
, nla
,
2125 nft_expr_policy
, NULL
);
2129 type
= nft_expr_type_get(ctx
->net
, ctx
->family
, tb
[NFTA_EXPR_NAME
]);
2131 return PTR_ERR(type
);
2133 if (tb
[NFTA_EXPR_DATA
]) {
2134 err
= nla_parse_nested_deprecated(info
->tb
, type
->maxattr
,
2136 type
->policy
, NULL
);
2140 memset(info
->tb
, 0, sizeof(info
->tb
[0]) * (type
->maxattr
+ 1));
2142 if (type
->select_ops
!= NULL
) {
2143 ops
= type
->select_ops(ctx
,
2144 (const struct nlattr
* const *)info
->tb
);
2147 #ifdef CONFIG_MODULES
2149 nft_expr_type_request_module(ctx
->net
,
2151 tb
[NFTA_EXPR_NAME
]);
2162 module_put(type
->owner
);
2166 static int nf_tables_newexpr(const struct nft_ctx
*ctx
,
2167 const struct nft_expr_info
*info
,
2168 struct nft_expr
*expr
)
2170 const struct nft_expr_ops
*ops
= info
->ops
;
2175 err
= ops
->init(ctx
, expr
, (const struct nlattr
**)info
->tb
);
2186 static void nf_tables_expr_destroy(const struct nft_ctx
*ctx
,
2187 struct nft_expr
*expr
)
2189 const struct nft_expr_type
*type
= expr
->ops
->type
;
2191 if (expr
->ops
->destroy
)
2192 expr
->ops
->destroy(ctx
, expr
);
2193 module_put(type
->owner
);
2196 struct nft_expr
*nft_expr_init(const struct nft_ctx
*ctx
,
2197 const struct nlattr
*nla
)
2199 struct nft_expr_info info
;
2200 struct nft_expr
*expr
;
2201 struct module
*owner
;
2204 err
= nf_tables_expr_parse(ctx
, nla
, &info
);
2209 expr
= kzalloc(info
.ops
->size
, GFP_KERNEL
);
2213 err
= nf_tables_newexpr(ctx
, &info
, expr
);
2221 owner
= info
.ops
->type
->owner
;
2222 if (info
.ops
->type
->release_ops
)
2223 info
.ops
->type
->release_ops(info
.ops
);
2227 return ERR_PTR(err
);
2230 void nft_expr_destroy(const struct nft_ctx
*ctx
, struct nft_expr
*expr
)
2232 nf_tables_expr_destroy(ctx
, expr
);
2240 static struct nft_rule
*__nft_rule_lookup(const struct nft_chain
*chain
,
2243 struct nft_rule
*rule
;
2245 // FIXME: this sucks
2246 list_for_each_entry_rcu(rule
, &chain
->rules
, list
) {
2247 if (handle
== rule
->handle
)
2251 return ERR_PTR(-ENOENT
);
2254 static struct nft_rule
*nft_rule_lookup(const struct nft_chain
*chain
,
2255 const struct nlattr
*nla
)
2258 return ERR_PTR(-EINVAL
);
2260 return __nft_rule_lookup(chain
, be64_to_cpu(nla_get_be64(nla
)));
2263 static const struct nla_policy nft_rule_policy
[NFTA_RULE_MAX
+ 1] = {
2264 [NFTA_RULE_TABLE
] = { .type
= NLA_STRING
,
2265 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
2266 [NFTA_RULE_CHAIN
] = { .type
= NLA_STRING
,
2267 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
2268 [NFTA_RULE_HANDLE
] = { .type
= NLA_U64
},
2269 [NFTA_RULE_EXPRESSIONS
] = { .type
= NLA_NESTED
},
2270 [NFTA_RULE_COMPAT
] = { .type
= NLA_NESTED
},
2271 [NFTA_RULE_POSITION
] = { .type
= NLA_U64
},
2272 [NFTA_RULE_USERDATA
] = { .type
= NLA_BINARY
,
2273 .len
= NFT_USERDATA_MAXLEN
},
2274 [NFTA_RULE_ID
] = { .type
= NLA_U32
},
2275 [NFTA_RULE_POSITION_ID
] = { .type
= NLA_U32
},
2278 static int nf_tables_fill_rule_info(struct sk_buff
*skb
, struct net
*net
,
2279 u32 portid
, u32 seq
, int event
,
2280 u32 flags
, int family
,
2281 const struct nft_table
*table
,
2282 const struct nft_chain
*chain
,
2283 const struct nft_rule
*rule
,
2284 const struct nft_rule
*prule
)
2286 struct nlmsghdr
*nlh
;
2287 struct nfgenmsg
*nfmsg
;
2288 const struct nft_expr
*expr
, *next
;
2289 struct nlattr
*list
;
2290 u16 type
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
2292 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(struct nfgenmsg
), flags
);
2294 goto nla_put_failure
;
2296 nfmsg
= nlmsg_data(nlh
);
2297 nfmsg
->nfgen_family
= family
;
2298 nfmsg
->version
= NFNETLINK_V0
;
2299 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
2301 if (nla_put_string(skb
, NFTA_RULE_TABLE
, table
->name
))
2302 goto nla_put_failure
;
2303 if (nla_put_string(skb
, NFTA_RULE_CHAIN
, chain
->name
))
2304 goto nla_put_failure
;
2305 if (nla_put_be64(skb
, NFTA_RULE_HANDLE
, cpu_to_be64(rule
->handle
),
2307 goto nla_put_failure
;
2309 if (event
!= NFT_MSG_DELRULE
&& prule
) {
2310 if (nla_put_be64(skb
, NFTA_RULE_POSITION
,
2311 cpu_to_be64(prule
->handle
),
2313 goto nla_put_failure
;
2316 list
= nla_nest_start_noflag(skb
, NFTA_RULE_EXPRESSIONS
);
2318 goto nla_put_failure
;
2319 nft_rule_for_each_expr(expr
, next
, rule
) {
2320 if (nft_expr_dump(skb
, NFTA_LIST_ELEM
, expr
) < 0)
2321 goto nla_put_failure
;
2323 nla_nest_end(skb
, list
);
2326 struct nft_userdata
*udata
= nft_userdata(rule
);
2327 if (nla_put(skb
, NFTA_RULE_USERDATA
, udata
->len
+ 1,
2329 goto nla_put_failure
;
2332 nlmsg_end(skb
, nlh
);
2336 nlmsg_trim(skb
, nlh
);
2340 static void nf_tables_rule_notify(const struct nft_ctx
*ctx
,
2341 const struct nft_rule
*rule
, int event
)
2343 struct sk_buff
*skb
;
2347 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
2350 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
2354 err
= nf_tables_fill_rule_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
2355 event
, 0, ctx
->family
, ctx
->table
,
2356 ctx
->chain
, rule
, NULL
);
2362 nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
2363 ctx
->report
, GFP_KERNEL
);
2366 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
2369 struct nft_rule_dump_ctx
{
2374 static int __nf_tables_dump_rules(struct sk_buff
*skb
,
2376 struct netlink_callback
*cb
,
2377 const struct nft_table
*table
,
2378 const struct nft_chain
*chain
)
2380 struct net
*net
= sock_net(skb
->sk
);
2381 const struct nft_rule
*rule
, *prule
;
2382 unsigned int s_idx
= cb
->args
[0];
2385 list_for_each_entry_rcu(rule
, &chain
->rules
, list
) {
2386 if (!nft_is_active(net
, rule
))
2391 memset(&cb
->args
[1], 0,
2392 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
2394 if (nf_tables_fill_rule_info(skb
, net
, NETLINK_CB(cb
->skb
).portid
,
2397 NLM_F_MULTI
| NLM_F_APPEND
,
2399 table
, chain
, rule
, prule
) < 0)
2402 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2411 static int nf_tables_dump_rules(struct sk_buff
*skb
,
2412 struct netlink_callback
*cb
)
2414 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2415 const struct nft_rule_dump_ctx
*ctx
= cb
->data
;
2416 struct nft_table
*table
;
2417 const struct nft_chain
*chain
;
2418 unsigned int idx
= 0;
2419 struct net
*net
= sock_net(skb
->sk
);
2420 int family
= nfmsg
->nfgen_family
;
2423 cb
->seq
= net
->nft
.base_seq
;
2425 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
2426 if (family
!= NFPROTO_UNSPEC
&& family
!= table
->family
)
2429 if (ctx
&& ctx
->table
&& strcmp(ctx
->table
, table
->name
) != 0)
2432 if (ctx
&& ctx
->table
&& ctx
->chain
) {
2433 struct rhlist_head
*list
, *tmp
;
2435 list
= rhltable_lookup(&table
->chains_ht
, ctx
->chain
,
2436 nft_chain_ht_params
);
2440 rhl_for_each_entry_rcu(chain
, tmp
, list
, rhlhead
) {
2441 if (!nft_is_active(net
, chain
))
2443 __nf_tables_dump_rules(skb
, &idx
,
2450 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
2451 if (__nf_tables_dump_rules(skb
, &idx
, cb
, table
, chain
))
2455 if (ctx
&& ctx
->table
)
2465 static int nf_tables_dump_rules_start(struct netlink_callback
*cb
)
2467 const struct nlattr
* const *nla
= cb
->data
;
2468 struct nft_rule_dump_ctx
*ctx
= NULL
;
2470 if (nla
[NFTA_RULE_TABLE
] || nla
[NFTA_RULE_CHAIN
]) {
2471 ctx
= kzalloc(sizeof(*ctx
), GFP_ATOMIC
);
2475 if (nla
[NFTA_RULE_TABLE
]) {
2476 ctx
->table
= nla_strdup(nla
[NFTA_RULE_TABLE
],
2483 if (nla
[NFTA_RULE_CHAIN
]) {
2484 ctx
->chain
= nla_strdup(nla
[NFTA_RULE_CHAIN
],
2498 static int nf_tables_dump_rules_done(struct netlink_callback
*cb
)
2500 struct nft_rule_dump_ctx
*ctx
= cb
->data
;
2510 /* called with rcu_read_lock held */
2511 static int nf_tables_getrule(struct net
*net
, struct sock
*nlsk
,
2512 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2513 const struct nlattr
* const nla
[],
2514 struct netlink_ext_ack
*extack
)
2516 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2517 u8 genmask
= nft_genmask_cur(net
);
2518 const struct nft_chain
*chain
;
2519 const struct nft_rule
*rule
;
2520 struct nft_table
*table
;
2521 struct sk_buff
*skb2
;
2522 int family
= nfmsg
->nfgen_family
;
2525 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2526 struct netlink_dump_control c
= {
2527 .start
= nf_tables_dump_rules_start
,
2528 .dump
= nf_tables_dump_rules
,
2529 .done
= nf_tables_dump_rules_done
,
2530 .module
= THIS_MODULE
,
2531 .data
= (void *)nla
,
2534 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
2537 table
= nft_table_lookup(net
, nla
[NFTA_RULE_TABLE
], family
, genmask
);
2538 if (IS_ERR(table
)) {
2539 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_TABLE
]);
2540 return PTR_ERR(table
);
2543 chain
= nft_chain_lookup(net
, table
, nla
[NFTA_RULE_CHAIN
], genmask
);
2544 if (IS_ERR(chain
)) {
2545 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_CHAIN
]);
2546 return PTR_ERR(chain
);
2549 rule
= nft_rule_lookup(chain
, nla
[NFTA_RULE_HANDLE
]);
2551 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_HANDLE
]);
2552 return PTR_ERR(rule
);
2555 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
2559 err
= nf_tables_fill_rule_info(skb2
, net
, NETLINK_CB(skb
).portid
,
2560 nlh
->nlmsg_seq
, NFT_MSG_NEWRULE
, 0,
2561 family
, table
, chain
, rule
, NULL
);
2565 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
2572 static void nf_tables_rule_destroy(const struct nft_ctx
*ctx
,
2573 struct nft_rule
*rule
)
2575 struct nft_expr
*expr
, *next
;
2578 * Careful: some expressions might not be initialized in case this
2579 * is called on error from nf_tables_newrule().
2581 expr
= nft_expr_first(rule
);
2582 while (expr
!= nft_expr_last(rule
) && expr
->ops
) {
2583 next
= nft_expr_next(expr
);
2584 nf_tables_expr_destroy(ctx
, expr
);
2590 static void nf_tables_rule_release(const struct nft_ctx
*ctx
,
2591 struct nft_rule
*rule
)
2593 nft_rule_expr_deactivate(ctx
, rule
, NFT_TRANS_RELEASE
);
2594 nf_tables_rule_destroy(ctx
, rule
);
2597 int nft_chain_validate(const struct nft_ctx
*ctx
, const struct nft_chain
*chain
)
2599 struct nft_expr
*expr
, *last
;
2600 const struct nft_data
*data
;
2601 struct nft_rule
*rule
;
2604 if (ctx
->level
== NFT_JUMP_STACK_SIZE
)
2607 list_for_each_entry(rule
, &chain
->rules
, list
) {
2608 if (!nft_is_active_next(ctx
->net
, rule
))
2611 nft_rule_for_each_expr(expr
, last
, rule
) {
2612 if (!expr
->ops
->validate
)
2615 err
= expr
->ops
->validate(ctx
, expr
, &data
);
2623 EXPORT_SYMBOL_GPL(nft_chain_validate
);
2625 static int nft_table_validate(struct net
*net
, const struct nft_table
*table
)
2627 struct nft_chain
*chain
;
2628 struct nft_ctx ctx
= {
2630 .family
= table
->family
,
2634 list_for_each_entry(chain
, &table
->chains
, list
) {
2635 if (!nft_is_base_chain(chain
))
2639 err
= nft_chain_validate(&ctx
, chain
);
2647 static struct nft_rule
*nft_rule_lookup_byid(const struct net
*net
,
2648 const struct nlattr
*nla
);
2650 #define NFT_RULE_MAXEXPRS 128
2652 static int nf_tables_newrule(struct net
*net
, struct sock
*nlsk
,
2653 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2654 const struct nlattr
* const nla
[],
2655 struct netlink_ext_ack
*extack
)
2657 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2658 u8 genmask
= nft_genmask_next(net
);
2659 struct nft_expr_info
*info
= NULL
;
2660 int family
= nfmsg
->nfgen_family
;
2661 struct nft_table
*table
;
2662 struct nft_chain
*chain
;
2663 struct nft_rule
*rule
, *old_rule
= NULL
;
2664 struct nft_userdata
*udata
;
2665 struct nft_trans
*trans
= NULL
;
2666 struct nft_expr
*expr
;
2669 unsigned int size
, i
, n
, ulen
= 0, usize
= 0;
2671 u64 handle
, pos_handle
;
2673 lockdep_assert_held(&net
->nft
.commit_mutex
);
2675 table
= nft_table_lookup(net
, nla
[NFTA_RULE_TABLE
], family
, genmask
);
2676 if (IS_ERR(table
)) {
2677 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_TABLE
]);
2678 return PTR_ERR(table
);
2681 chain
= nft_chain_lookup(net
, table
, nla
[NFTA_RULE_CHAIN
], genmask
);
2682 if (IS_ERR(chain
)) {
2683 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_CHAIN
]);
2684 return PTR_ERR(chain
);
2687 if (nla
[NFTA_RULE_HANDLE
]) {
2688 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_HANDLE
]));
2689 rule
= __nft_rule_lookup(chain
, handle
);
2691 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_HANDLE
]);
2692 return PTR_ERR(rule
);
2695 if (nlh
->nlmsg_flags
& NLM_F_EXCL
) {
2696 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_HANDLE
]);
2699 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2704 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
) ||
2705 nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2707 handle
= nf_tables_alloc_handle(table
);
2709 if (chain
->use
== UINT_MAX
)
2712 if (nla
[NFTA_RULE_POSITION
]) {
2713 pos_handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_POSITION
]));
2714 old_rule
= __nft_rule_lookup(chain
, pos_handle
);
2715 if (IS_ERR(old_rule
)) {
2716 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_POSITION
]);
2717 return PTR_ERR(old_rule
);
2719 } else if (nla
[NFTA_RULE_POSITION_ID
]) {
2720 old_rule
= nft_rule_lookup_byid(net
, nla
[NFTA_RULE_POSITION_ID
]);
2721 if (IS_ERR(old_rule
)) {
2722 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_POSITION_ID
]);
2723 return PTR_ERR(old_rule
);
2728 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, chain
, nla
);
2732 if (nla
[NFTA_RULE_EXPRESSIONS
]) {
2733 info
= kvmalloc_array(NFT_RULE_MAXEXPRS
,
2734 sizeof(struct nft_expr_info
),
2739 nla_for_each_nested(tmp
, nla
[NFTA_RULE_EXPRESSIONS
], rem
) {
2741 if (nla_type(tmp
) != NFTA_LIST_ELEM
)
2743 if (n
== NFT_RULE_MAXEXPRS
)
2745 err
= nf_tables_expr_parse(&ctx
, tmp
, &info
[n
]);
2748 size
+= info
[n
].ops
->size
;
2752 /* Check for overflow of dlen field */
2754 if (size
>= 1 << 12)
2757 if (nla
[NFTA_RULE_USERDATA
]) {
2758 ulen
= nla_len(nla
[NFTA_RULE_USERDATA
]);
2760 usize
= sizeof(struct nft_userdata
) + ulen
;
2764 rule
= kzalloc(sizeof(*rule
) + size
+ usize
, GFP_KERNEL
);
2768 nft_activate_next(net
, rule
);
2770 rule
->handle
= handle
;
2772 rule
->udata
= ulen
? 1 : 0;
2775 udata
= nft_userdata(rule
);
2776 udata
->len
= ulen
- 1;
2777 nla_memcpy(udata
->data
, nla
[NFTA_RULE_USERDATA
], ulen
);
2780 expr
= nft_expr_first(rule
);
2781 for (i
= 0; i
< n
; i
++) {
2782 err
= nf_tables_newexpr(&ctx
, &info
[i
], expr
);
2786 if (info
[i
].ops
->validate
)
2787 nft_validate_state_update(net
, NFT_VALIDATE_NEED
);
2790 expr
= nft_expr_next(expr
);
2793 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
2794 trans
= nft_trans_rule_add(&ctx
, NFT_MSG_NEWRULE
, rule
);
2795 if (trans
== NULL
) {
2799 err
= nft_delrule(&ctx
, old_rule
);
2801 nft_trans_destroy(trans
);
2805 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
2807 if (nft_trans_rule_add(&ctx
, NFT_MSG_NEWRULE
, rule
) == NULL
) {
2812 if (nlh
->nlmsg_flags
& NLM_F_APPEND
) {
2814 list_add_rcu(&rule
->list
, &old_rule
->list
);
2816 list_add_tail_rcu(&rule
->list
, &chain
->rules
);
2819 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
2821 list_add_rcu(&rule
->list
, &chain
->rules
);
2827 if (net
->nft
.validate_state
== NFT_VALIDATE_DO
)
2828 return nft_table_validate(net
, table
);
2832 nf_tables_rule_release(&ctx
, rule
);
2834 for (i
= 0; i
< n
; i
++) {
2836 module_put(info
[i
].ops
->type
->owner
);
2837 if (info
[i
].ops
->type
->release_ops
)
2838 info
[i
].ops
->type
->release_ops(info
[i
].ops
);
2845 static struct nft_rule
*nft_rule_lookup_byid(const struct net
*net
,
2846 const struct nlattr
*nla
)
2848 u32 id
= ntohl(nla_get_be32(nla
));
2849 struct nft_trans
*trans
;
2851 list_for_each_entry(trans
, &net
->nft
.commit_list
, list
) {
2852 struct nft_rule
*rule
= nft_trans_rule(trans
);
2854 if (trans
->msg_type
== NFT_MSG_NEWRULE
&&
2855 id
== nft_trans_rule_id(trans
))
2858 return ERR_PTR(-ENOENT
);
2861 static int nf_tables_delrule(struct net
*net
, struct sock
*nlsk
,
2862 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2863 const struct nlattr
* const nla
[],
2864 struct netlink_ext_ack
*extack
)
2866 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2867 u8 genmask
= nft_genmask_next(net
);
2868 struct nft_table
*table
;
2869 struct nft_chain
*chain
= NULL
;
2870 struct nft_rule
*rule
;
2871 int family
= nfmsg
->nfgen_family
, err
= 0;
2874 table
= nft_table_lookup(net
, nla
[NFTA_RULE_TABLE
], family
, genmask
);
2875 if (IS_ERR(table
)) {
2876 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_TABLE
]);
2877 return PTR_ERR(table
);
2880 if (nla
[NFTA_RULE_CHAIN
]) {
2881 chain
= nft_chain_lookup(net
, table
, nla
[NFTA_RULE_CHAIN
],
2883 if (IS_ERR(chain
)) {
2884 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_CHAIN
]);
2885 return PTR_ERR(chain
);
2889 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, chain
, nla
);
2892 if (nla
[NFTA_RULE_HANDLE
]) {
2893 rule
= nft_rule_lookup(chain
, nla
[NFTA_RULE_HANDLE
]);
2895 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_HANDLE
]);
2896 return PTR_ERR(rule
);
2899 err
= nft_delrule(&ctx
, rule
);
2900 } else if (nla
[NFTA_RULE_ID
]) {
2901 rule
= nft_rule_lookup_byid(net
, nla
[NFTA_RULE_ID
]);
2903 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_ID
]);
2904 return PTR_ERR(rule
);
2907 err
= nft_delrule(&ctx
, rule
);
2909 err
= nft_delrule_by_chain(&ctx
);
2912 list_for_each_entry(chain
, &table
->chains
, list
) {
2913 if (!nft_is_active_next(net
, chain
))
2917 err
= nft_delrule_by_chain(&ctx
);
2930 static LIST_HEAD(nf_tables_set_types
);
2932 int nft_register_set(struct nft_set_type
*type
)
2934 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2935 list_add_tail_rcu(&type
->list
, &nf_tables_set_types
);
2936 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2939 EXPORT_SYMBOL_GPL(nft_register_set
);
2941 void nft_unregister_set(struct nft_set_type
*type
)
2943 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2944 list_del_rcu(&type
->list
);
2945 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2947 EXPORT_SYMBOL_GPL(nft_unregister_set
);
2949 #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \
2950 NFT_SET_TIMEOUT | NFT_SET_OBJECT | \
2953 static bool nft_set_ops_candidate(const struct nft_set_type
*type
, u32 flags
)
2955 return (flags
& type
->features
) == (flags
& NFT_SET_FEATURES
);
2959 * Select a set implementation based on the data characteristics and the
2960 * given policy. The total memory use might not be known if no size is
2961 * given, in that case the amount of memory per element is used.
2963 static const struct nft_set_ops
*
2964 nft_select_set_ops(const struct nft_ctx
*ctx
,
2965 const struct nlattr
* const nla
[],
2966 const struct nft_set_desc
*desc
,
2967 enum nft_set_policies policy
)
2969 const struct nft_set_ops
*ops
, *bops
;
2970 struct nft_set_estimate est
, best
;
2971 const struct nft_set_type
*type
;
2974 lockdep_assert_held(&ctx
->net
->nft
.commit_mutex
);
2975 lockdep_nfnl_nft_mutex_not_held();
2976 #ifdef CONFIG_MODULES
2977 if (list_empty(&nf_tables_set_types
)) {
2978 nft_request_module(ctx
->net
, "nft-set");
2979 if (!list_empty(&nf_tables_set_types
))
2980 return ERR_PTR(-EAGAIN
);
2983 if (nla
[NFTA_SET_FLAGS
] != NULL
)
2984 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
2991 list_for_each_entry(type
, &nf_tables_set_types
, list
) {
2994 if (!nft_set_ops_candidate(type
, flags
))
2996 if (!ops
->estimate(desc
, flags
, &est
))
3000 case NFT_SET_POL_PERFORMANCE
:
3001 if (est
.lookup
< best
.lookup
)
3003 if (est
.lookup
== best
.lookup
&&
3004 est
.space
< best
.space
)
3007 case NFT_SET_POL_MEMORY
:
3009 if (est
.space
< best
.space
)
3011 if (est
.space
== best
.space
&&
3012 est
.lookup
< best
.lookup
)
3014 } else if (est
.size
< best
.size
|| !bops
) {
3022 if (!try_module_get(type
->owner
))
3025 module_put(to_set_type(bops
)->owner
);
3034 return ERR_PTR(-EOPNOTSUPP
);
3037 static const struct nla_policy nft_set_policy
[NFTA_SET_MAX
+ 1] = {
3038 [NFTA_SET_TABLE
] = { .type
= NLA_STRING
,
3039 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
3040 [NFTA_SET_NAME
] = { .type
= NLA_STRING
,
3041 .len
= NFT_SET_MAXNAMELEN
- 1 },
3042 [NFTA_SET_FLAGS
] = { .type
= NLA_U32
},
3043 [NFTA_SET_KEY_TYPE
] = { .type
= NLA_U32
},
3044 [NFTA_SET_KEY_LEN
] = { .type
= NLA_U32
},
3045 [NFTA_SET_DATA_TYPE
] = { .type
= NLA_U32
},
3046 [NFTA_SET_DATA_LEN
] = { .type
= NLA_U32
},
3047 [NFTA_SET_POLICY
] = { .type
= NLA_U32
},
3048 [NFTA_SET_DESC
] = { .type
= NLA_NESTED
},
3049 [NFTA_SET_ID
] = { .type
= NLA_U32
},
3050 [NFTA_SET_TIMEOUT
] = { .type
= NLA_U64
},
3051 [NFTA_SET_GC_INTERVAL
] = { .type
= NLA_U32
},
3052 [NFTA_SET_USERDATA
] = { .type
= NLA_BINARY
,
3053 .len
= NFT_USERDATA_MAXLEN
},
3054 [NFTA_SET_OBJ_TYPE
] = { .type
= NLA_U32
},
3055 [NFTA_SET_HANDLE
] = { .type
= NLA_U64
},
3058 static const struct nla_policy nft_set_desc_policy
[NFTA_SET_DESC_MAX
+ 1] = {
3059 [NFTA_SET_DESC_SIZE
] = { .type
= NLA_U32
},
3062 static int nft_ctx_init_from_setattr(struct nft_ctx
*ctx
, struct net
*net
,
3063 const struct sk_buff
*skb
,
3064 const struct nlmsghdr
*nlh
,
3065 const struct nlattr
* const nla
[],
3066 struct netlink_ext_ack
*extack
,
3069 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3070 int family
= nfmsg
->nfgen_family
;
3071 struct nft_table
*table
= NULL
;
3073 if (nla
[NFTA_SET_TABLE
] != NULL
) {
3074 table
= nft_table_lookup(net
, nla
[NFTA_SET_TABLE
], family
,
3076 if (IS_ERR(table
)) {
3077 NL_SET_BAD_ATTR(extack
, nla
[NFTA_SET_TABLE
]);
3078 return PTR_ERR(table
);
3082 nft_ctx_init(ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
3086 static struct nft_set
*nft_set_lookup(const struct nft_table
*table
,
3087 const struct nlattr
*nla
, u8 genmask
)
3089 struct nft_set
*set
;
3092 return ERR_PTR(-EINVAL
);
3094 list_for_each_entry_rcu(set
, &table
->sets
, list
) {
3095 if (!nla_strcmp(nla
, set
->name
) &&
3096 nft_active_genmask(set
, genmask
))
3099 return ERR_PTR(-ENOENT
);
3102 static struct nft_set
*nft_set_lookup_byhandle(const struct nft_table
*table
,
3103 const struct nlattr
*nla
,
3106 struct nft_set
*set
;
3108 list_for_each_entry(set
, &table
->sets
, list
) {
3109 if (be64_to_cpu(nla_get_be64(nla
)) == set
->handle
&&
3110 nft_active_genmask(set
, genmask
))
3113 return ERR_PTR(-ENOENT
);
3116 static struct nft_set
*nft_set_lookup_byid(const struct net
*net
,
3117 const struct nlattr
*nla
, u8 genmask
)
3119 struct nft_trans
*trans
;
3120 u32 id
= ntohl(nla_get_be32(nla
));
3122 list_for_each_entry(trans
, &net
->nft
.commit_list
, list
) {
3123 if (trans
->msg_type
== NFT_MSG_NEWSET
) {
3124 struct nft_set
*set
= nft_trans_set(trans
);
3126 if (id
== nft_trans_set_id(trans
) &&
3127 nft_active_genmask(set
, genmask
))
3131 return ERR_PTR(-ENOENT
);
3134 struct nft_set
*nft_set_lookup_global(const struct net
*net
,
3135 const struct nft_table
*table
,
3136 const struct nlattr
*nla_set_name
,
3137 const struct nlattr
*nla_set_id
,
3140 struct nft_set
*set
;
3142 set
= nft_set_lookup(table
, nla_set_name
, genmask
);
3147 set
= nft_set_lookup_byid(net
, nla_set_id
, genmask
);
3151 EXPORT_SYMBOL_GPL(nft_set_lookup_global
);
3153 static int nf_tables_set_alloc_name(struct nft_ctx
*ctx
, struct nft_set
*set
,
3156 const struct nft_set
*i
;
3158 unsigned long *inuse
;
3159 unsigned int n
= 0, min
= 0;
3161 p
= strchr(name
, '%');
3163 if (p
[1] != 'd' || strchr(p
+ 2, '%'))
3166 inuse
= (unsigned long *)get_zeroed_page(GFP_KERNEL
);
3170 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
3173 if (!nft_is_active_next(ctx
->net
, set
))
3175 if (!sscanf(i
->name
, name
, &tmp
))
3177 if (tmp
< min
|| tmp
>= min
+ BITS_PER_BYTE
* PAGE_SIZE
)
3180 set_bit(tmp
- min
, inuse
);
3183 n
= find_first_zero_bit(inuse
, BITS_PER_BYTE
* PAGE_SIZE
);
3184 if (n
>= BITS_PER_BYTE
* PAGE_SIZE
) {
3185 min
+= BITS_PER_BYTE
* PAGE_SIZE
;
3186 memset(inuse
, 0, PAGE_SIZE
);
3189 free_page((unsigned long)inuse
);
3192 set
->name
= kasprintf(GFP_KERNEL
, name
, min
+ n
);
3196 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
3197 if (!nft_is_active_next(ctx
->net
, i
))
3199 if (!strcmp(set
->name
, i
->name
)) {
3207 static int nf_msecs_to_jiffies64(const struct nlattr
*nla
, u64
*result
)
3209 u64 ms
= be64_to_cpu(nla_get_be64(nla
));
3210 u64 max
= (u64
)(~((u64
)0));
3212 max
= div_u64(max
, NSEC_PER_MSEC
);
3216 ms
*= NSEC_PER_MSEC
;
3217 *result
= nsecs_to_jiffies64(ms
);
3221 static __be64
nf_jiffies64_to_msecs(u64 input
)
3223 return cpu_to_be64(jiffies64_to_msecs(input
));
3226 static int nf_tables_fill_set(struct sk_buff
*skb
, const struct nft_ctx
*ctx
,
3227 const struct nft_set
*set
, u16 event
, u16 flags
)
3229 struct nfgenmsg
*nfmsg
;
3230 struct nlmsghdr
*nlh
;
3231 struct nlattr
*desc
;
3232 u32 portid
= ctx
->portid
;
3235 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
3236 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
3239 goto nla_put_failure
;
3241 nfmsg
= nlmsg_data(nlh
);
3242 nfmsg
->nfgen_family
= ctx
->family
;
3243 nfmsg
->version
= NFNETLINK_V0
;
3244 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
3246 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
3247 goto nla_put_failure
;
3248 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
3249 goto nla_put_failure
;
3250 if (nla_put_be64(skb
, NFTA_SET_HANDLE
, cpu_to_be64(set
->handle
),
3252 goto nla_put_failure
;
3253 if (set
->flags
!= 0)
3254 if (nla_put_be32(skb
, NFTA_SET_FLAGS
, htonl(set
->flags
)))
3255 goto nla_put_failure
;
3257 if (nla_put_be32(skb
, NFTA_SET_KEY_TYPE
, htonl(set
->ktype
)))
3258 goto nla_put_failure
;
3259 if (nla_put_be32(skb
, NFTA_SET_KEY_LEN
, htonl(set
->klen
)))
3260 goto nla_put_failure
;
3261 if (set
->flags
& NFT_SET_MAP
) {
3262 if (nla_put_be32(skb
, NFTA_SET_DATA_TYPE
, htonl(set
->dtype
)))
3263 goto nla_put_failure
;
3264 if (nla_put_be32(skb
, NFTA_SET_DATA_LEN
, htonl(set
->dlen
)))
3265 goto nla_put_failure
;
3267 if (set
->flags
& NFT_SET_OBJECT
&&
3268 nla_put_be32(skb
, NFTA_SET_OBJ_TYPE
, htonl(set
->objtype
)))
3269 goto nla_put_failure
;
3272 nla_put_be64(skb
, NFTA_SET_TIMEOUT
,
3273 nf_jiffies64_to_msecs(set
->timeout
),
3275 goto nla_put_failure
;
3277 nla_put_be32(skb
, NFTA_SET_GC_INTERVAL
, htonl(set
->gc_int
)))
3278 goto nla_put_failure
;
3280 if (set
->policy
!= NFT_SET_POL_PERFORMANCE
) {
3281 if (nla_put_be32(skb
, NFTA_SET_POLICY
, htonl(set
->policy
)))
3282 goto nla_put_failure
;
3285 if (nla_put(skb
, NFTA_SET_USERDATA
, set
->udlen
, set
->udata
))
3286 goto nla_put_failure
;
3288 desc
= nla_nest_start_noflag(skb
, NFTA_SET_DESC
);
3290 goto nla_put_failure
;
3292 nla_put_be32(skb
, NFTA_SET_DESC_SIZE
, htonl(set
->size
)))
3293 goto nla_put_failure
;
3294 nla_nest_end(skb
, desc
);
3296 nlmsg_end(skb
, nlh
);
3300 nlmsg_trim(skb
, nlh
);
3304 static void nf_tables_set_notify(const struct nft_ctx
*ctx
,
3305 const struct nft_set
*set
, int event
,
3308 struct sk_buff
*skb
;
3309 u32 portid
= ctx
->portid
;
3313 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
3316 skb
= nlmsg_new(NLMSG_GOODSIZE
, gfp_flags
);
3320 err
= nf_tables_fill_set(skb
, ctx
, set
, event
, 0);
3326 nfnetlink_send(skb
, ctx
->net
, portid
, NFNLGRP_NFTABLES
, ctx
->report
,
3330 nfnetlink_set_err(ctx
->net
, portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
3333 static int nf_tables_dump_sets(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3335 const struct nft_set
*set
;
3336 unsigned int idx
, s_idx
= cb
->args
[0];
3337 struct nft_table
*table
, *cur_table
= (struct nft_table
*)cb
->args
[2];
3338 struct net
*net
= sock_net(skb
->sk
);
3339 struct nft_ctx
*ctx
= cb
->data
, ctx_set
;
3345 cb
->seq
= net
->nft
.base_seq
;
3347 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
3348 if (ctx
->family
!= NFPROTO_UNSPEC
&&
3349 ctx
->family
!= table
->family
)
3352 if (ctx
->table
&& ctx
->table
!= table
)
3356 if (cur_table
!= table
)
3362 list_for_each_entry_rcu(set
, &table
->sets
, list
) {
3365 if (!nft_is_active(net
, set
))
3369 ctx_set
.table
= table
;
3370 ctx_set
.family
= table
->family
;
3372 if (nf_tables_fill_set(skb
, &ctx_set
, set
,
3376 cb
->args
[2] = (unsigned long) table
;
3379 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
3392 static int nf_tables_dump_sets_start(struct netlink_callback
*cb
)
3394 struct nft_ctx
*ctx_dump
= NULL
;
3396 ctx_dump
= kmemdup(cb
->data
, sizeof(*ctx_dump
), GFP_ATOMIC
);
3397 if (ctx_dump
== NULL
)
3400 cb
->data
= ctx_dump
;
3404 static int nf_tables_dump_sets_done(struct netlink_callback
*cb
)
3410 /* called with rcu_read_lock held */
3411 static int nf_tables_getset(struct net
*net
, struct sock
*nlsk
,
3412 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3413 const struct nlattr
* const nla
[],
3414 struct netlink_ext_ack
*extack
)
3416 u8 genmask
= nft_genmask_cur(net
);
3417 const struct nft_set
*set
;
3419 struct sk_buff
*skb2
;
3420 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3423 /* Verify existence before starting dump */
3424 err
= nft_ctx_init_from_setattr(&ctx
, net
, skb
, nlh
, nla
, extack
,
3429 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3430 struct netlink_dump_control c
= {
3431 .start
= nf_tables_dump_sets_start
,
3432 .dump
= nf_tables_dump_sets
,
3433 .done
= nf_tables_dump_sets_done
,
3435 .module
= THIS_MODULE
,
3438 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
3441 /* Only accept unspec with dump */
3442 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
3443 return -EAFNOSUPPORT
;
3444 if (!nla
[NFTA_SET_TABLE
])
3447 set
= nft_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
], genmask
);
3449 return PTR_ERR(set
);
3451 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
3455 err
= nf_tables_fill_set(skb2
, &ctx
, set
, NFT_MSG_NEWSET
, 0);
3459 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
3466 static int nf_tables_set_desc_parse(struct nft_set_desc
*desc
,
3467 const struct nlattr
*nla
)
3469 struct nlattr
*da
[NFTA_SET_DESC_MAX
+ 1];
3472 err
= nla_parse_nested_deprecated(da
, NFTA_SET_DESC_MAX
, nla
,
3473 nft_set_desc_policy
, NULL
);
3477 if (da
[NFTA_SET_DESC_SIZE
] != NULL
)
3478 desc
->size
= ntohl(nla_get_be32(da
[NFTA_SET_DESC_SIZE
]));
3483 static int nf_tables_newset(struct net
*net
, struct sock
*nlsk
,
3484 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3485 const struct nlattr
* const nla
[],
3486 struct netlink_ext_ack
*extack
)
3488 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3489 u8 genmask
= nft_genmask_next(net
);
3490 int family
= nfmsg
->nfgen_family
;
3491 const struct nft_set_ops
*ops
;
3492 struct nft_table
*table
;
3493 struct nft_set
*set
;
3498 u32 ktype
, dtype
, flags
, policy
, gc_int
, objtype
;
3499 struct nft_set_desc desc
;
3500 unsigned char *udata
;
3504 if (nla
[NFTA_SET_TABLE
] == NULL
||
3505 nla
[NFTA_SET_NAME
] == NULL
||
3506 nla
[NFTA_SET_KEY_LEN
] == NULL
||
3507 nla
[NFTA_SET_ID
] == NULL
)
3510 memset(&desc
, 0, sizeof(desc
));
3512 ktype
= NFT_DATA_VALUE
;
3513 if (nla
[NFTA_SET_KEY_TYPE
] != NULL
) {
3514 ktype
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_TYPE
]));
3515 if ((ktype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
)
3519 desc
.klen
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_LEN
]));
3520 if (desc
.klen
== 0 || desc
.klen
> NFT_DATA_VALUE_MAXLEN
)
3524 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
3525 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
3526 if (flags
& ~(NFT_SET_ANONYMOUS
| NFT_SET_CONSTANT
|
3527 NFT_SET_INTERVAL
| NFT_SET_TIMEOUT
|
3528 NFT_SET_MAP
| NFT_SET_EVAL
|
3531 /* Only one of these operations is supported */
3532 if ((flags
& (NFT_SET_MAP
| NFT_SET_EVAL
| NFT_SET_OBJECT
)) ==
3533 (NFT_SET_MAP
| NFT_SET_EVAL
| NFT_SET_OBJECT
))
3538 if (nla
[NFTA_SET_DATA_TYPE
] != NULL
) {
3539 if (!(flags
& NFT_SET_MAP
))
3542 dtype
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_TYPE
]));
3543 if ((dtype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
&&
3544 dtype
!= NFT_DATA_VERDICT
)
3547 if (dtype
!= NFT_DATA_VERDICT
) {
3548 if (nla
[NFTA_SET_DATA_LEN
] == NULL
)
3550 desc
.dlen
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_LEN
]));
3551 if (desc
.dlen
== 0 || desc
.dlen
> NFT_DATA_VALUE_MAXLEN
)
3554 desc
.dlen
= sizeof(struct nft_verdict
);
3555 } else if (flags
& NFT_SET_MAP
)
3558 if (nla
[NFTA_SET_OBJ_TYPE
] != NULL
) {
3559 if (!(flags
& NFT_SET_OBJECT
))
3562 objtype
= ntohl(nla_get_be32(nla
[NFTA_SET_OBJ_TYPE
]));
3563 if (objtype
== NFT_OBJECT_UNSPEC
||
3564 objtype
> NFT_OBJECT_MAX
)
3566 } else if (flags
& NFT_SET_OBJECT
)
3569 objtype
= NFT_OBJECT_UNSPEC
;
3572 if (nla
[NFTA_SET_TIMEOUT
] != NULL
) {
3573 if (!(flags
& NFT_SET_TIMEOUT
))
3576 err
= nf_msecs_to_jiffies64(nla
[NFTA_SET_TIMEOUT
], &timeout
);
3581 if (nla
[NFTA_SET_GC_INTERVAL
] != NULL
) {
3582 if (!(flags
& NFT_SET_TIMEOUT
))
3584 gc_int
= ntohl(nla_get_be32(nla
[NFTA_SET_GC_INTERVAL
]));
3587 policy
= NFT_SET_POL_PERFORMANCE
;
3588 if (nla
[NFTA_SET_POLICY
] != NULL
)
3589 policy
= ntohl(nla_get_be32(nla
[NFTA_SET_POLICY
]));
3591 if (nla
[NFTA_SET_DESC
] != NULL
) {
3592 err
= nf_tables_set_desc_parse(&desc
, nla
[NFTA_SET_DESC
]);
3597 table
= nft_table_lookup(net
, nla
[NFTA_SET_TABLE
], family
, genmask
);
3598 if (IS_ERR(table
)) {
3599 NL_SET_BAD_ATTR(extack
, nla
[NFTA_SET_TABLE
]);
3600 return PTR_ERR(table
);
3603 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
3605 set
= nft_set_lookup(table
, nla
[NFTA_SET_NAME
], genmask
);
3607 if (PTR_ERR(set
) != -ENOENT
) {
3608 NL_SET_BAD_ATTR(extack
, nla
[NFTA_SET_NAME
]);
3609 return PTR_ERR(set
);
3612 if (nlh
->nlmsg_flags
& NLM_F_EXCL
) {
3613 NL_SET_BAD_ATTR(extack
, nla
[NFTA_SET_NAME
]);
3616 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
3622 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
3625 ops
= nft_select_set_ops(&ctx
, nla
, &desc
, policy
);
3627 return PTR_ERR(ops
);
3630 if (nla
[NFTA_SET_USERDATA
])
3631 udlen
= nla_len(nla
[NFTA_SET_USERDATA
]);
3634 if (ops
->privsize
!= NULL
)
3635 size
= ops
->privsize(nla
, &desc
);
3637 set
= kvzalloc(sizeof(*set
) + size
+ udlen
, GFP_KERNEL
);
3643 name
= nla_strdup(nla
[NFTA_SET_NAME
], GFP_KERNEL
);
3649 err
= nf_tables_set_alloc_name(&ctx
, set
, name
);
3656 udata
= set
->data
+ size
;
3657 nla_memcpy(udata
, nla
[NFTA_SET_USERDATA
], udlen
);
3660 INIT_LIST_HEAD(&set
->bindings
);
3662 write_pnet(&set
->net
, net
);
3665 set
->klen
= desc
.klen
;
3667 set
->objtype
= objtype
;
3668 set
->dlen
= desc
.dlen
;
3670 set
->size
= desc
.size
;
3671 set
->policy
= policy
;
3674 set
->timeout
= timeout
;
3675 set
->gc_int
= gc_int
;
3676 set
->handle
= nf_tables_alloc_handle(table
);
3678 err
= ops
->init(set
, &desc
, nla
);
3682 err
= nft_trans_set_add(&ctx
, NFT_MSG_NEWSET
, set
);
3686 list_add_tail_rcu(&set
->list
, &table
->sets
);
3697 module_put(to_set_type(ops
)->owner
);
3701 static void nft_set_destroy(struct nft_set
*set
)
3703 if (WARN_ON(set
->use
> 0))
3706 set
->ops
->destroy(set
);
3707 module_put(to_set_type(set
->ops
)->owner
);
3712 static int nf_tables_delset(struct net
*net
, struct sock
*nlsk
,
3713 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3714 const struct nlattr
* const nla
[],
3715 struct netlink_ext_ack
*extack
)
3717 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3718 u8 genmask
= nft_genmask_next(net
);
3719 const struct nlattr
*attr
;
3720 struct nft_set
*set
;
3724 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
3725 return -EAFNOSUPPORT
;
3726 if (nla
[NFTA_SET_TABLE
] == NULL
)
3729 err
= nft_ctx_init_from_setattr(&ctx
, net
, skb
, nlh
, nla
, extack
,
3734 if (nla
[NFTA_SET_HANDLE
]) {
3735 attr
= nla
[NFTA_SET_HANDLE
];
3736 set
= nft_set_lookup_byhandle(ctx
.table
, attr
, genmask
);
3738 attr
= nla
[NFTA_SET_NAME
];
3739 set
= nft_set_lookup(ctx
.table
, attr
, genmask
);
3743 NL_SET_BAD_ATTR(extack
, attr
);
3744 return PTR_ERR(set
);
3747 (nlh
->nlmsg_flags
& NLM_F_NONREC
&& atomic_read(&set
->nelems
) > 0)) {
3748 NL_SET_BAD_ATTR(extack
, attr
);
3752 return nft_delset(&ctx
, set
);
3755 static int nf_tables_bind_check_setelem(const struct nft_ctx
*ctx
,
3756 struct nft_set
*set
,
3757 const struct nft_set_iter
*iter
,
3758 struct nft_set_elem
*elem
)
3760 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
3761 enum nft_registers dreg
;
3763 dreg
= nft_type_to_reg(set
->dtype
);
3764 return nft_validate_register_store(ctx
, dreg
, nft_set_ext_data(ext
),
3765 set
->dtype
== NFT_DATA_VERDICT
?
3766 NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
3770 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
3771 struct nft_set_binding
*binding
)
3773 struct nft_set_binding
*i
;
3774 struct nft_set_iter iter
;
3776 if (set
->use
== UINT_MAX
)
3779 if (!list_empty(&set
->bindings
) && nft_set_is_anonymous(set
))
3782 if (binding
->flags
& NFT_SET_MAP
) {
3783 /* If the set is already bound to the same chain all
3784 * jumps are already validated for that chain.
3786 list_for_each_entry(i
, &set
->bindings
, list
) {
3787 if (i
->flags
& NFT_SET_MAP
&&
3788 i
->chain
== binding
->chain
)
3792 iter
.genmask
= nft_genmask_next(ctx
->net
);
3796 iter
.fn
= nf_tables_bind_check_setelem
;
3798 set
->ops
->walk(ctx
, set
, &iter
);
3803 binding
->chain
= ctx
->chain
;
3804 list_add_tail_rcu(&binding
->list
, &set
->bindings
);
3805 nft_set_trans_bind(ctx
, set
);
3810 EXPORT_SYMBOL_GPL(nf_tables_bind_set
);
3812 static void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
3813 struct nft_set_binding
*binding
, bool event
)
3815 list_del_rcu(&binding
->list
);
3817 if (list_empty(&set
->bindings
) && nft_set_is_anonymous(set
)) {
3818 list_del_rcu(&set
->list
);
3820 nf_tables_set_notify(ctx
, set
, NFT_MSG_DELSET
,
3825 void nf_tables_deactivate_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
3826 struct nft_set_binding
*binding
,
3827 enum nft_trans_phase phase
)
3830 case NFT_TRANS_PREPARE
:
3833 case NFT_TRANS_ABORT
:
3834 case NFT_TRANS_RELEASE
:
3838 nf_tables_unbind_set(ctx
, set
, binding
,
3839 phase
== NFT_TRANS_COMMIT
);
3842 EXPORT_SYMBOL_GPL(nf_tables_deactivate_set
);
3844 void nf_tables_destroy_set(const struct nft_ctx
*ctx
, struct nft_set
*set
)
3846 if (list_empty(&set
->bindings
) && nft_set_is_anonymous(set
))
3847 nft_set_destroy(set
);
3849 EXPORT_SYMBOL_GPL(nf_tables_destroy_set
);
3851 const struct nft_set_ext_type nft_set_ext_types
[] = {
3852 [NFT_SET_EXT_KEY
] = {
3853 .align
= __alignof__(u32
),
3855 [NFT_SET_EXT_DATA
] = {
3856 .align
= __alignof__(u32
),
3858 [NFT_SET_EXT_EXPR
] = {
3859 .align
= __alignof__(struct nft_expr
),
3861 [NFT_SET_EXT_OBJREF
] = {
3862 .len
= sizeof(struct nft_object
*),
3863 .align
= __alignof__(struct nft_object
*),
3865 [NFT_SET_EXT_FLAGS
] = {
3867 .align
= __alignof__(u8
),
3869 [NFT_SET_EXT_TIMEOUT
] = {
3871 .align
= __alignof__(u64
),
3873 [NFT_SET_EXT_EXPIRATION
] = {
3875 .align
= __alignof__(u64
),
3877 [NFT_SET_EXT_USERDATA
] = {
3878 .len
= sizeof(struct nft_userdata
),
3879 .align
= __alignof__(struct nft_userdata
),
3882 EXPORT_SYMBOL_GPL(nft_set_ext_types
);
3888 static const struct nla_policy nft_set_elem_policy
[NFTA_SET_ELEM_MAX
+ 1] = {
3889 [NFTA_SET_ELEM_KEY
] = { .type
= NLA_NESTED
},
3890 [NFTA_SET_ELEM_DATA
] = { .type
= NLA_NESTED
},
3891 [NFTA_SET_ELEM_FLAGS
] = { .type
= NLA_U32
},
3892 [NFTA_SET_ELEM_TIMEOUT
] = { .type
= NLA_U64
},
3893 [NFTA_SET_ELEM_EXPIRATION
] = { .type
= NLA_U64
},
3894 [NFTA_SET_ELEM_USERDATA
] = { .type
= NLA_BINARY
,
3895 .len
= NFT_USERDATA_MAXLEN
},
3896 [NFTA_SET_ELEM_EXPR
] = { .type
= NLA_NESTED
},
3897 [NFTA_SET_ELEM_OBJREF
] = { .type
= NLA_STRING
},
3900 static const struct nla_policy nft_set_elem_list_policy
[NFTA_SET_ELEM_LIST_MAX
+ 1] = {
3901 [NFTA_SET_ELEM_LIST_TABLE
] = { .type
= NLA_STRING
,
3902 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
3903 [NFTA_SET_ELEM_LIST_SET
] = { .type
= NLA_STRING
,
3904 .len
= NFT_SET_MAXNAMELEN
- 1 },
3905 [NFTA_SET_ELEM_LIST_ELEMENTS
] = { .type
= NLA_NESTED
},
3906 [NFTA_SET_ELEM_LIST_SET_ID
] = { .type
= NLA_U32
},
3909 static int nft_ctx_init_from_elemattr(struct nft_ctx
*ctx
, struct net
*net
,
3910 const struct sk_buff
*skb
,
3911 const struct nlmsghdr
*nlh
,
3912 const struct nlattr
* const nla
[],
3913 struct netlink_ext_ack
*extack
,
3916 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3917 int family
= nfmsg
->nfgen_family
;
3918 struct nft_table
*table
;
3920 table
= nft_table_lookup(net
, nla
[NFTA_SET_ELEM_LIST_TABLE
], family
,
3922 if (IS_ERR(table
)) {
3923 NL_SET_BAD_ATTR(extack
, nla
[NFTA_SET_ELEM_LIST_TABLE
]);
3924 return PTR_ERR(table
);
3927 nft_ctx_init(ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
3931 static int nf_tables_fill_setelem(struct sk_buff
*skb
,
3932 const struct nft_set
*set
,
3933 const struct nft_set_elem
*elem
)
3935 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
3936 unsigned char *b
= skb_tail_pointer(skb
);
3937 struct nlattr
*nest
;
3939 nest
= nla_nest_start_noflag(skb
, NFTA_LIST_ELEM
);
3941 goto nla_put_failure
;
3943 if (nft_data_dump(skb
, NFTA_SET_ELEM_KEY
, nft_set_ext_key(ext
),
3944 NFT_DATA_VALUE
, set
->klen
) < 0)
3945 goto nla_put_failure
;
3947 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
) &&
3948 nft_data_dump(skb
, NFTA_SET_ELEM_DATA
, nft_set_ext_data(ext
),
3949 set
->dtype
== NFT_DATA_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
3951 goto nla_put_failure
;
3953 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
) &&
3954 nft_expr_dump(skb
, NFTA_SET_ELEM_EXPR
, nft_set_ext_expr(ext
)) < 0)
3955 goto nla_put_failure
;
3957 if (nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
) &&
3958 nla_put_string(skb
, NFTA_SET_ELEM_OBJREF
,
3959 (*nft_set_ext_obj(ext
))->key
.name
) < 0)
3960 goto nla_put_failure
;
3962 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
3963 nla_put_be32(skb
, NFTA_SET_ELEM_FLAGS
,
3964 htonl(*nft_set_ext_flags(ext
))))
3965 goto nla_put_failure
;
3967 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
) &&
3968 nla_put_be64(skb
, NFTA_SET_ELEM_TIMEOUT
,
3969 nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext
)),
3971 goto nla_put_failure
;
3973 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
)) {
3974 u64 expires
, now
= get_jiffies_64();
3976 expires
= *nft_set_ext_expiration(ext
);
3977 if (time_before64(now
, expires
))
3982 if (nla_put_be64(skb
, NFTA_SET_ELEM_EXPIRATION
,
3983 nf_jiffies64_to_msecs(expires
),
3985 goto nla_put_failure
;
3988 if (nft_set_ext_exists(ext
, NFT_SET_EXT_USERDATA
)) {
3989 struct nft_userdata
*udata
;
3991 udata
= nft_set_ext_userdata(ext
);
3992 if (nla_put(skb
, NFTA_SET_ELEM_USERDATA
,
3993 udata
->len
+ 1, udata
->data
))
3994 goto nla_put_failure
;
3997 nla_nest_end(skb
, nest
);
4005 struct nft_set_dump_args
{
4006 const struct netlink_callback
*cb
;
4007 struct nft_set_iter iter
;
4008 struct sk_buff
*skb
;
4011 static int nf_tables_dump_setelem(const struct nft_ctx
*ctx
,
4012 struct nft_set
*set
,
4013 const struct nft_set_iter
*iter
,
4014 struct nft_set_elem
*elem
)
4016 struct nft_set_dump_args
*args
;
4018 args
= container_of(iter
, struct nft_set_dump_args
, iter
);
4019 return nf_tables_fill_setelem(args
->skb
, set
, elem
);
4022 struct nft_set_dump_ctx
{
4023 const struct nft_set
*set
;
4027 static int nf_tables_dump_set(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4029 struct nft_set_dump_ctx
*dump_ctx
= cb
->data
;
4030 struct net
*net
= sock_net(skb
->sk
);
4031 struct nft_table
*table
;
4032 struct nft_set
*set
;
4033 struct nft_set_dump_args args
;
4034 bool set_found
= false;
4035 struct nfgenmsg
*nfmsg
;
4036 struct nlmsghdr
*nlh
;
4037 struct nlattr
*nest
;
4042 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
4043 if (dump_ctx
->ctx
.family
!= NFPROTO_UNSPEC
&&
4044 dump_ctx
->ctx
.family
!= table
->family
)
4047 if (table
!= dump_ctx
->ctx
.table
)
4050 list_for_each_entry_rcu(set
, &table
->sets
, list
) {
4051 if (set
== dump_ctx
->set
) {
4064 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, NFT_MSG_NEWSETELEM
);
4065 portid
= NETLINK_CB(cb
->skb
).portid
;
4066 seq
= cb
->nlh
->nlmsg_seq
;
4068 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
4071 goto nla_put_failure
;
4073 nfmsg
= nlmsg_data(nlh
);
4074 nfmsg
->nfgen_family
= table
->family
;
4075 nfmsg
->version
= NFNETLINK_V0
;
4076 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
4078 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_TABLE
, table
->name
))
4079 goto nla_put_failure
;
4080 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_SET
, set
->name
))
4081 goto nla_put_failure
;
4083 nest
= nla_nest_start_noflag(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
4085 goto nla_put_failure
;
4089 args
.iter
.genmask
= nft_genmask_cur(net
);
4090 args
.iter
.skip
= cb
->args
[0];
4091 args
.iter
.count
= 0;
4093 args
.iter
.fn
= nf_tables_dump_setelem
;
4094 set
->ops
->walk(&dump_ctx
->ctx
, set
, &args
.iter
);
4097 nla_nest_end(skb
, nest
);
4098 nlmsg_end(skb
, nlh
);
4100 if (args
.iter
.err
&& args
.iter
.err
!= -EMSGSIZE
)
4101 return args
.iter
.err
;
4102 if (args
.iter
.count
== cb
->args
[0])
4105 cb
->args
[0] = args
.iter
.count
;
4113 static int nf_tables_dump_set_start(struct netlink_callback
*cb
)
4115 struct nft_set_dump_ctx
*dump_ctx
= cb
->data
;
4117 cb
->data
= kmemdup(dump_ctx
, sizeof(*dump_ctx
), GFP_ATOMIC
);
4119 return cb
->data
? 0 : -ENOMEM
;
4122 static int nf_tables_dump_set_done(struct netlink_callback
*cb
)
4128 static int nf_tables_fill_setelem_info(struct sk_buff
*skb
,
4129 const struct nft_ctx
*ctx
, u32 seq
,
4130 u32 portid
, int event
, u16 flags
,
4131 const struct nft_set
*set
,
4132 const struct nft_set_elem
*elem
)
4134 struct nfgenmsg
*nfmsg
;
4135 struct nlmsghdr
*nlh
;
4136 struct nlattr
*nest
;
4139 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
4140 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
4143 goto nla_put_failure
;
4145 nfmsg
= nlmsg_data(nlh
);
4146 nfmsg
->nfgen_family
= ctx
->family
;
4147 nfmsg
->version
= NFNETLINK_V0
;
4148 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
4150 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
4151 goto nla_put_failure
;
4152 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
4153 goto nla_put_failure
;
4155 nest
= nla_nest_start_noflag(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
4157 goto nla_put_failure
;
4159 err
= nf_tables_fill_setelem(skb
, set
, elem
);
4161 goto nla_put_failure
;
4163 nla_nest_end(skb
, nest
);
4165 nlmsg_end(skb
, nlh
);
4169 nlmsg_trim(skb
, nlh
);
4173 static int nft_setelem_parse_flags(const struct nft_set
*set
,
4174 const struct nlattr
*attr
, u32
*flags
)
4179 *flags
= ntohl(nla_get_be32(attr
));
4180 if (*flags
& ~NFT_SET_ELEM_INTERVAL_END
)
4182 if (!(set
->flags
& NFT_SET_INTERVAL
) &&
4183 *flags
& NFT_SET_ELEM_INTERVAL_END
)
4189 static int nft_get_set_elem(struct nft_ctx
*ctx
, struct nft_set
*set
,
4190 const struct nlattr
*attr
)
4192 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
4193 struct nft_data_desc desc
;
4194 struct nft_set_elem elem
;
4195 struct sk_buff
*skb
;
4200 err
= nla_parse_nested_deprecated(nla
, NFTA_SET_ELEM_MAX
, attr
,
4201 nft_set_elem_policy
, NULL
);
4205 if (!nla
[NFTA_SET_ELEM_KEY
])
4208 err
= nft_setelem_parse_flags(set
, nla
[NFTA_SET_ELEM_FLAGS
], &flags
);
4212 err
= nft_data_init(ctx
, &elem
.key
.val
, sizeof(elem
.key
), &desc
,
4213 nla
[NFTA_SET_ELEM_KEY
]);
4218 if (desc
.type
!= NFT_DATA_VALUE
|| desc
.len
!= set
->klen
)
4221 priv
= set
->ops
->get(ctx
->net
, set
, &elem
, flags
);
4223 return PTR_ERR(priv
);
4228 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_ATOMIC
);
4232 err
= nf_tables_fill_setelem_info(skb
, ctx
, ctx
->seq
, ctx
->portid
,
4233 NFT_MSG_NEWSETELEM
, 0, set
, &elem
);
4237 err
= nfnetlink_unicast(skb
, ctx
->net
, ctx
->portid
, MSG_DONTWAIT
);
4238 /* This avoids a loop in nfnetlink. */
4246 /* this avoids a loop in nfnetlink. */
4247 return err
== -EAGAIN
? -ENOBUFS
: err
;
4250 /* called with rcu_read_lock held */
4251 static int nf_tables_getsetelem(struct net
*net
, struct sock
*nlsk
,
4252 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
4253 const struct nlattr
* const nla
[],
4254 struct netlink_ext_ack
*extack
)
4256 u8 genmask
= nft_genmask_cur(net
);
4257 struct nft_set
*set
;
4258 struct nlattr
*attr
;
4262 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
, extack
,
4267 set
= nft_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
], genmask
);
4269 return PTR_ERR(set
);
4271 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
4272 struct netlink_dump_control c
= {
4273 .start
= nf_tables_dump_set_start
,
4274 .dump
= nf_tables_dump_set
,
4275 .done
= nf_tables_dump_set_done
,
4276 .module
= THIS_MODULE
,
4278 struct nft_set_dump_ctx dump_ctx
= {
4284 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
4287 if (!nla
[NFTA_SET_ELEM_LIST_ELEMENTS
])
4290 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
4291 err
= nft_get_set_elem(&ctx
, set
, attr
);
4299 static void nf_tables_setelem_notify(const struct nft_ctx
*ctx
,
4300 const struct nft_set
*set
,
4301 const struct nft_set_elem
*elem
,
4302 int event
, u16 flags
)
4304 struct net
*net
= ctx
->net
;
4305 u32 portid
= ctx
->portid
;
4306 struct sk_buff
*skb
;
4309 if (!ctx
->report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
4312 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
4316 err
= nf_tables_fill_setelem_info(skb
, ctx
, 0, portid
, event
, flags
,
4323 nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, ctx
->report
,
4327 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
4330 static struct nft_trans
*nft_trans_elem_alloc(struct nft_ctx
*ctx
,
4332 struct nft_set
*set
)
4334 struct nft_trans
*trans
;
4336 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_elem
));
4340 nft_trans_elem_set(trans
) = set
;
4344 void *nft_set_elem_init(const struct nft_set
*set
,
4345 const struct nft_set_ext_tmpl
*tmpl
,
4346 const u32
*key
, const u32
*data
,
4347 u64 timeout
, u64 expiration
, gfp_t gfp
)
4349 struct nft_set_ext
*ext
;
4352 elem
= kzalloc(set
->ops
->elemsize
+ tmpl
->len
, gfp
);
4356 ext
= nft_set_elem_ext(set
, elem
);
4357 nft_set_ext_init(ext
, tmpl
);
4359 memcpy(nft_set_ext_key(ext
), key
, set
->klen
);
4360 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
4361 memcpy(nft_set_ext_data(ext
), data
, set
->dlen
);
4362 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
)) {
4363 *nft_set_ext_expiration(ext
) = get_jiffies_64() + expiration
;
4364 if (expiration
== 0)
4365 *nft_set_ext_expiration(ext
) += timeout
;
4367 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
))
4368 *nft_set_ext_timeout(ext
) = timeout
;
4373 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
,
4376 struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
);
4377 struct nft_ctx ctx
= {
4378 .net
= read_pnet(&set
->net
),
4379 .family
= set
->table
->family
,
4382 nft_data_release(nft_set_ext_key(ext
), NFT_DATA_VALUE
);
4383 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
4384 nft_data_release(nft_set_ext_data(ext
), set
->dtype
);
4385 if (destroy_expr
&& nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
)) {
4386 struct nft_expr
*expr
= nft_set_ext_expr(ext
);
4388 if (expr
->ops
->destroy_clone
) {
4389 expr
->ops
->destroy_clone(&ctx
, expr
);
4390 module_put(expr
->ops
->type
->owner
);
4392 nf_tables_expr_destroy(&ctx
, expr
);
4395 if (nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
))
4396 (*nft_set_ext_obj(ext
))->use
--;
4399 EXPORT_SYMBOL_GPL(nft_set_elem_destroy
);
4401 /* Only called from commit path, nft_set_elem_deactivate() already deals with
4402 * the refcounting from the preparation phase.
4404 static void nf_tables_set_elem_destroy(const struct nft_ctx
*ctx
,
4405 const struct nft_set
*set
, void *elem
)
4407 struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
);
4409 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
))
4410 nf_tables_expr_destroy(ctx
, nft_set_ext_expr(ext
));
4414 static int nft_add_set_elem(struct nft_ctx
*ctx
, struct nft_set
*set
,
4415 const struct nlattr
*attr
, u32 nlmsg_flags
)
4417 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
4418 u8 genmask
= nft_genmask_next(ctx
->net
);
4419 struct nft_data_desc d1
, d2
;
4420 struct nft_set_ext_tmpl tmpl
;
4421 struct nft_set_ext
*ext
, *ext2
;
4422 struct nft_set_elem elem
;
4423 struct nft_set_binding
*binding
;
4424 struct nft_object
*obj
= NULL
;
4425 struct nft_userdata
*udata
;
4426 struct nft_data data
;
4427 enum nft_registers dreg
;
4428 struct nft_trans
*trans
;
4435 err
= nla_parse_nested_deprecated(nla
, NFTA_SET_ELEM_MAX
, attr
,
4436 nft_set_elem_policy
, NULL
);
4440 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
4443 nft_set_ext_prepare(&tmpl
);
4445 err
= nft_setelem_parse_flags(set
, nla
[NFTA_SET_ELEM_FLAGS
], &flags
);
4449 nft_set_ext_add(&tmpl
, NFT_SET_EXT_FLAGS
);
4451 if (set
->flags
& NFT_SET_MAP
) {
4452 if (nla
[NFTA_SET_ELEM_DATA
] == NULL
&&
4453 !(flags
& NFT_SET_ELEM_INTERVAL_END
))
4455 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
&&
4456 flags
& NFT_SET_ELEM_INTERVAL_END
)
4459 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
4464 if (nla
[NFTA_SET_ELEM_TIMEOUT
] != NULL
) {
4465 if (!(set
->flags
& NFT_SET_TIMEOUT
))
4467 err
= nf_msecs_to_jiffies64(nla
[NFTA_SET_ELEM_TIMEOUT
],
4471 } else if (set
->flags
& NFT_SET_TIMEOUT
) {
4472 timeout
= set
->timeout
;
4476 if (nla
[NFTA_SET_ELEM_EXPIRATION
] != NULL
) {
4477 if (!(set
->flags
& NFT_SET_TIMEOUT
))
4479 err
= nf_msecs_to_jiffies64(nla
[NFTA_SET_ELEM_EXPIRATION
],
4485 err
= nft_data_init(ctx
, &elem
.key
.val
, sizeof(elem
.key
), &d1
,
4486 nla
[NFTA_SET_ELEM_KEY
]);
4490 if (d1
.type
!= NFT_DATA_VALUE
|| d1
.len
!= set
->klen
)
4493 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_KEY
, d1
.len
);
4495 nft_set_ext_add(&tmpl
, NFT_SET_EXT_EXPIRATION
);
4496 if (timeout
!= set
->timeout
)
4497 nft_set_ext_add(&tmpl
, NFT_SET_EXT_TIMEOUT
);
4500 if (nla
[NFTA_SET_ELEM_OBJREF
] != NULL
) {
4501 if (!(set
->flags
& NFT_SET_OBJECT
)) {
4505 obj
= nft_obj_lookup(ctx
->net
, ctx
->table
,
4506 nla
[NFTA_SET_ELEM_OBJREF
],
4507 set
->objtype
, genmask
);
4512 nft_set_ext_add(&tmpl
, NFT_SET_EXT_OBJREF
);
4515 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
) {
4516 err
= nft_data_init(ctx
, &data
, sizeof(data
), &d2
,
4517 nla
[NFTA_SET_ELEM_DATA
]);
4522 if (set
->dtype
!= NFT_DATA_VERDICT
&& d2
.len
!= set
->dlen
)
4525 dreg
= nft_type_to_reg(set
->dtype
);
4526 list_for_each_entry(binding
, &set
->bindings
, list
) {
4527 struct nft_ctx bind_ctx
= {
4529 .family
= ctx
->family
,
4530 .table
= ctx
->table
,
4531 .chain
= (struct nft_chain
*)binding
->chain
,
4534 if (!(binding
->flags
& NFT_SET_MAP
))
4537 err
= nft_validate_register_store(&bind_ctx
, dreg
,
4543 if (d2
.type
== NFT_DATA_VERDICT
&&
4544 (data
.verdict
.code
== NFT_GOTO
||
4545 data
.verdict
.code
== NFT_JUMP
))
4546 nft_validate_state_update(ctx
->net
,
4550 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_DATA
, d2
.len
);
4553 /* The full maximum length of userdata can exceed the maximum
4554 * offset value (U8_MAX) for following extensions, therefor it
4555 * must be the last extension added.
4558 if (nla
[NFTA_SET_ELEM_USERDATA
] != NULL
) {
4559 ulen
= nla_len(nla
[NFTA_SET_ELEM_USERDATA
]);
4561 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_USERDATA
,
4566 elem
.priv
= nft_set_elem_init(set
, &tmpl
, elem
.key
.val
.data
, data
.data
,
4567 timeout
, expiration
, GFP_KERNEL
);
4568 if (elem
.priv
== NULL
)
4571 ext
= nft_set_elem_ext(set
, elem
.priv
);
4573 *nft_set_ext_flags(ext
) = flags
;
4575 udata
= nft_set_ext_userdata(ext
);
4576 udata
->len
= ulen
- 1;
4577 nla_memcpy(&udata
->data
, nla
[NFTA_SET_ELEM_USERDATA
], ulen
);
4580 *nft_set_ext_obj(ext
) = obj
;
4584 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_NEWSETELEM
, set
);
4588 ext
->genmask
= nft_genmask_cur(ctx
->net
) | NFT_SET_ELEM_BUSY_MASK
;
4589 err
= set
->ops
->insert(ctx
->net
, set
, &elem
, &ext2
);
4591 if (err
== -EEXIST
) {
4592 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
) ^
4593 nft_set_ext_exists(ext2
, NFT_SET_EXT_DATA
) ||
4594 nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
) ^
4595 nft_set_ext_exists(ext2
, NFT_SET_EXT_OBJREF
)) {
4599 if ((nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
) &&
4600 nft_set_ext_exists(ext2
, NFT_SET_EXT_DATA
) &&
4601 memcmp(nft_set_ext_data(ext
),
4602 nft_set_ext_data(ext2
), set
->dlen
) != 0) ||
4603 (nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
) &&
4604 nft_set_ext_exists(ext2
, NFT_SET_EXT_OBJREF
) &&
4605 *nft_set_ext_obj(ext
) != *nft_set_ext_obj(ext2
)))
4607 else if (!(nlmsg_flags
& NLM_F_EXCL
))
4614 !atomic_add_unless(&set
->nelems
, 1, set
->size
+ set
->ndeact
)) {
4619 nft_trans_elem(trans
) = elem
;
4620 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
4624 set
->ops
->remove(ctx
->net
, set
, &elem
);
4632 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
4633 nft_data_release(&data
, d2
.type
);
4635 nft_data_release(&elem
.key
.val
, d1
.type
);
4640 static int nf_tables_newsetelem(struct net
*net
, struct sock
*nlsk
,
4641 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
4642 const struct nlattr
* const nla
[],
4643 struct netlink_ext_ack
*extack
)
4645 u8 genmask
= nft_genmask_next(net
);
4646 const struct nlattr
*attr
;
4647 struct nft_set
*set
;
4651 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
)
4654 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
, extack
,
4659 set
= nft_set_lookup_global(net
, ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
],
4660 nla
[NFTA_SET_ELEM_LIST_SET_ID
], genmask
);
4662 return PTR_ERR(set
);
4664 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
4667 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
4668 err
= nft_add_set_elem(&ctx
, set
, attr
, nlh
->nlmsg_flags
);
4673 if (net
->nft
.validate_state
== NFT_VALIDATE_DO
)
4674 return nft_table_validate(net
, ctx
.table
);
4680 * nft_data_hold - hold a nft_data item
4682 * @data: struct nft_data to release
4683 * @type: type of data
4685 * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4686 * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and
4687 * NFT_GOTO verdicts. This function must be called on active data objects
4688 * from the second phase of the commit protocol.
4690 void nft_data_hold(const struct nft_data
*data
, enum nft_data_types type
)
4692 if (type
== NFT_DATA_VERDICT
) {
4693 switch (data
->verdict
.code
) {
4696 data
->verdict
.chain
->use
++;
4702 static void nft_set_elem_activate(const struct net
*net
,
4703 const struct nft_set
*set
,
4704 struct nft_set_elem
*elem
)
4706 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
4708 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
4709 nft_data_hold(nft_set_ext_data(ext
), set
->dtype
);
4710 if (nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
))
4711 (*nft_set_ext_obj(ext
))->use
++;
4714 static void nft_set_elem_deactivate(const struct net
*net
,
4715 const struct nft_set
*set
,
4716 struct nft_set_elem
*elem
)
4718 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
4720 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
4721 nft_data_release(nft_set_ext_data(ext
), set
->dtype
);
4722 if (nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
))
4723 (*nft_set_ext_obj(ext
))->use
--;
4726 static int nft_del_setelem(struct nft_ctx
*ctx
, struct nft_set
*set
,
4727 const struct nlattr
*attr
)
4729 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
4730 struct nft_set_ext_tmpl tmpl
;
4731 struct nft_data_desc desc
;
4732 struct nft_set_elem elem
;
4733 struct nft_set_ext
*ext
;
4734 struct nft_trans
*trans
;
4739 err
= nla_parse_nested_deprecated(nla
, NFTA_SET_ELEM_MAX
, attr
,
4740 nft_set_elem_policy
, NULL
);
4745 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
4748 nft_set_ext_prepare(&tmpl
);
4750 err
= nft_setelem_parse_flags(set
, nla
[NFTA_SET_ELEM_FLAGS
], &flags
);
4754 nft_set_ext_add(&tmpl
, NFT_SET_EXT_FLAGS
);
4756 err
= nft_data_init(ctx
, &elem
.key
.val
, sizeof(elem
.key
), &desc
,
4757 nla
[NFTA_SET_ELEM_KEY
]);
4762 if (desc
.type
!= NFT_DATA_VALUE
|| desc
.len
!= set
->klen
)
4765 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_KEY
, desc
.len
);
4768 elem
.priv
= nft_set_elem_init(set
, &tmpl
, elem
.key
.val
.data
, NULL
, 0,
4770 if (elem
.priv
== NULL
)
4773 ext
= nft_set_elem_ext(set
, elem
.priv
);
4775 *nft_set_ext_flags(ext
) = flags
;
4777 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_DELSETELEM
, set
);
4778 if (trans
== NULL
) {
4783 priv
= set
->ops
->deactivate(ctx
->net
, set
, &elem
);
4791 nft_set_elem_deactivate(ctx
->net
, set
, &elem
);
4793 nft_trans_elem(trans
) = elem
;
4794 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
4802 nft_data_release(&elem
.key
.val
, desc
.type
);
4807 static int nft_flush_set(const struct nft_ctx
*ctx
,
4808 struct nft_set
*set
,
4809 const struct nft_set_iter
*iter
,
4810 struct nft_set_elem
*elem
)
4812 struct nft_trans
*trans
;
4815 trans
= nft_trans_alloc_gfp(ctx
, NFT_MSG_DELSETELEM
,
4816 sizeof(struct nft_trans_elem
), GFP_ATOMIC
);
4820 if (!set
->ops
->flush(ctx
->net
, set
, elem
->priv
)) {
4826 nft_set_elem_deactivate(ctx
->net
, set
, elem
);
4827 nft_trans_elem_set(trans
) = set
;
4828 nft_trans_elem(trans
) = *elem
;
4829 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
4837 static int nf_tables_delsetelem(struct net
*net
, struct sock
*nlsk
,
4838 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
4839 const struct nlattr
* const nla
[],
4840 struct netlink_ext_ack
*extack
)
4842 u8 genmask
= nft_genmask_next(net
);
4843 const struct nlattr
*attr
;
4844 struct nft_set
*set
;
4848 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
, extack
,
4853 set
= nft_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
], genmask
);
4855 return PTR_ERR(set
);
4856 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
4859 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
) {
4860 struct nft_set_iter iter
= {
4862 .fn
= nft_flush_set
,
4864 set
->ops
->walk(&ctx
, set
, &iter
);
4869 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
4870 err
= nft_del_setelem(&ctx
, set
, attr
);
4879 void nft_set_gc_batch_release(struct rcu_head
*rcu
)
4881 struct nft_set_gc_batch
*gcb
;
4884 gcb
= container_of(rcu
, struct nft_set_gc_batch
, head
.rcu
);
4885 for (i
= 0; i
< gcb
->head
.cnt
; i
++)
4886 nft_set_elem_destroy(gcb
->head
.set
, gcb
->elems
[i
], true);
4889 EXPORT_SYMBOL_GPL(nft_set_gc_batch_release
);
4891 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
4894 struct nft_set_gc_batch
*gcb
;
4896 gcb
= kzalloc(sizeof(*gcb
), gfp
);
4899 gcb
->head
.set
= set
;
4902 EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc
);
4909 * nft_register_obj- register nf_tables stateful object type
4912 * Registers the object type for use with nf_tables. Returns zero on
4913 * success or a negative errno code otherwise.
4915 int nft_register_obj(struct nft_object_type
*obj_type
)
4917 if (obj_type
->type
== NFT_OBJECT_UNSPEC
)
4920 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
4921 list_add_rcu(&obj_type
->list
, &nf_tables_objects
);
4922 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
4925 EXPORT_SYMBOL_GPL(nft_register_obj
);
4928 * nft_unregister_obj - unregister nf_tables object type
4931 * Unregisters the object type for use with nf_tables.
4933 void nft_unregister_obj(struct nft_object_type
*obj_type
)
4935 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
4936 list_del_rcu(&obj_type
->list
);
4937 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
4939 EXPORT_SYMBOL_GPL(nft_unregister_obj
);
4941 struct nft_object
*nft_obj_lookup(const struct net
*net
,
4942 const struct nft_table
*table
,
4943 const struct nlattr
*nla
, u32 objtype
,
4946 struct nft_object_hash_key k
= { .table
= table
};
4947 char search
[NFT_OBJ_MAXNAMELEN
];
4948 struct rhlist_head
*tmp
, *list
;
4949 struct nft_object
*obj
;
4951 nla_strlcpy(search
, nla
, sizeof(search
));
4954 WARN_ON_ONCE(!rcu_read_lock_held() &&
4955 !lockdep_commit_lock_is_held(net
));
4958 list
= rhltable_lookup(&nft_objname_ht
, &k
, nft_objname_ht_params
);
4962 rhl_for_each_entry_rcu(obj
, tmp
, list
, rhlhead
) {
4963 if (objtype
== obj
->ops
->type
->type
&&
4964 nft_active_genmask(obj
, genmask
)) {
4971 return ERR_PTR(-ENOENT
);
4973 EXPORT_SYMBOL_GPL(nft_obj_lookup
);
4975 static struct nft_object
*nft_obj_lookup_byhandle(const struct nft_table
*table
,
4976 const struct nlattr
*nla
,
4977 u32 objtype
, u8 genmask
)
4979 struct nft_object
*obj
;
4981 list_for_each_entry(obj
, &table
->objects
, list
) {
4982 if (be64_to_cpu(nla_get_be64(nla
)) == obj
->handle
&&
4983 objtype
== obj
->ops
->type
->type
&&
4984 nft_active_genmask(obj
, genmask
))
4987 return ERR_PTR(-ENOENT
);
4990 static const struct nla_policy nft_obj_policy
[NFTA_OBJ_MAX
+ 1] = {
4991 [NFTA_OBJ_TABLE
] = { .type
= NLA_STRING
,
4992 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
4993 [NFTA_OBJ_NAME
] = { .type
= NLA_STRING
,
4994 .len
= NFT_OBJ_MAXNAMELEN
- 1 },
4995 [NFTA_OBJ_TYPE
] = { .type
= NLA_U32
},
4996 [NFTA_OBJ_DATA
] = { .type
= NLA_NESTED
},
4997 [NFTA_OBJ_HANDLE
] = { .type
= NLA_U64
},
5000 static struct nft_object
*nft_obj_init(const struct nft_ctx
*ctx
,
5001 const struct nft_object_type
*type
,
5002 const struct nlattr
*attr
)
5005 const struct nft_object_ops
*ops
;
5006 struct nft_object
*obj
;
5009 tb
= kmalloc_array(type
->maxattr
+ 1, sizeof(*tb
), GFP_KERNEL
);
5014 err
= nla_parse_nested_deprecated(tb
, type
->maxattr
, attr
,
5015 type
->policy
, NULL
);
5019 memset(tb
, 0, sizeof(tb
[0]) * (type
->maxattr
+ 1));
5022 if (type
->select_ops
) {
5023 ops
= type
->select_ops(ctx
, (const struct nlattr
* const *)tb
);
5033 obj
= kzalloc(sizeof(*obj
) + ops
->size
, GFP_KERNEL
);
5037 err
= ops
->init(ctx
, (const struct nlattr
* const *)tb
, obj
);
5050 return ERR_PTR(err
);
5053 static int nft_object_dump(struct sk_buff
*skb
, unsigned int attr
,
5054 struct nft_object
*obj
, bool reset
)
5056 struct nlattr
*nest
;
5058 nest
= nla_nest_start_noflag(skb
, attr
);
5060 goto nla_put_failure
;
5061 if (obj
->ops
->dump(skb
, obj
, reset
) < 0)
5062 goto nla_put_failure
;
5063 nla_nest_end(skb
, nest
);
5070 static const struct nft_object_type
*__nft_obj_type_get(u32 objtype
)
5072 const struct nft_object_type
*type
;
5074 list_for_each_entry(type
, &nf_tables_objects
, list
) {
5075 if (objtype
== type
->type
)
5081 static const struct nft_object_type
*
5082 nft_obj_type_get(struct net
*net
, u32 objtype
)
5084 const struct nft_object_type
*type
;
5086 type
= __nft_obj_type_get(objtype
);
5087 if (type
!= NULL
&& try_module_get(type
->owner
))
5090 lockdep_nfnl_nft_mutex_not_held();
5091 #ifdef CONFIG_MODULES
5093 nft_request_module(net
, "nft-obj-%u", objtype
);
5094 if (__nft_obj_type_get(objtype
))
5095 return ERR_PTR(-EAGAIN
);
5098 return ERR_PTR(-ENOENT
);
5101 static int nf_tables_newobj(struct net
*net
, struct sock
*nlsk
,
5102 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
5103 const struct nlattr
* const nla
[],
5104 struct netlink_ext_ack
*extack
)
5106 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
5107 const struct nft_object_type
*type
;
5108 u8 genmask
= nft_genmask_next(net
);
5109 int family
= nfmsg
->nfgen_family
;
5110 struct nft_table
*table
;
5111 struct nft_object
*obj
;
5116 if (!nla
[NFTA_OBJ_TYPE
] ||
5117 !nla
[NFTA_OBJ_NAME
] ||
5118 !nla
[NFTA_OBJ_DATA
])
5121 table
= nft_table_lookup(net
, nla
[NFTA_OBJ_TABLE
], family
, genmask
);
5122 if (IS_ERR(table
)) {
5123 NL_SET_BAD_ATTR(extack
, nla
[NFTA_OBJ_TABLE
]);
5124 return PTR_ERR(table
);
5127 objtype
= ntohl(nla_get_be32(nla
[NFTA_OBJ_TYPE
]));
5128 obj
= nft_obj_lookup(net
, table
, nla
[NFTA_OBJ_NAME
], objtype
, genmask
);
5131 if (err
!= -ENOENT
) {
5132 NL_SET_BAD_ATTR(extack
, nla
[NFTA_OBJ_NAME
]);
5136 if (nlh
->nlmsg_flags
& NLM_F_EXCL
) {
5137 NL_SET_BAD_ATTR(extack
, nla
[NFTA_OBJ_NAME
]);
5143 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
5145 type
= nft_obj_type_get(net
, objtype
);
5147 return PTR_ERR(type
);
5149 obj
= nft_obj_init(&ctx
, type
, nla
[NFTA_OBJ_DATA
]);
5154 obj
->key
.table
= table
;
5155 obj
->handle
= nf_tables_alloc_handle(table
);
5157 obj
->key
.name
= nla_strdup(nla
[NFTA_OBJ_NAME
], GFP_KERNEL
);
5158 if (!obj
->key
.name
) {
5163 err
= nft_trans_obj_add(&ctx
, NFT_MSG_NEWOBJ
, obj
);
5167 err
= rhltable_insert(&nft_objname_ht
, &obj
->rhlhead
,
5168 nft_objname_ht_params
);
5172 list_add_tail_rcu(&obj
->list
, &table
->objects
);
5176 /* queued in transaction log */
5177 INIT_LIST_HEAD(&obj
->list
);
5180 kfree(obj
->key
.name
);
5182 if (obj
->ops
->destroy
)
5183 obj
->ops
->destroy(&ctx
, obj
);
5186 module_put(type
->owner
);
5190 static int nf_tables_fill_obj_info(struct sk_buff
*skb
, struct net
*net
,
5191 u32 portid
, u32 seq
, int event
, u32 flags
,
5192 int family
, const struct nft_table
*table
,
5193 struct nft_object
*obj
, bool reset
)
5195 struct nfgenmsg
*nfmsg
;
5196 struct nlmsghdr
*nlh
;
5198 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
5199 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
5201 goto nla_put_failure
;
5203 nfmsg
= nlmsg_data(nlh
);
5204 nfmsg
->nfgen_family
= family
;
5205 nfmsg
->version
= NFNETLINK_V0
;
5206 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
5208 if (nla_put_string(skb
, NFTA_OBJ_TABLE
, table
->name
) ||
5209 nla_put_string(skb
, NFTA_OBJ_NAME
, obj
->key
.name
) ||
5210 nla_put_be32(skb
, NFTA_OBJ_TYPE
, htonl(obj
->ops
->type
->type
)) ||
5211 nla_put_be32(skb
, NFTA_OBJ_USE
, htonl(obj
->use
)) ||
5212 nft_object_dump(skb
, NFTA_OBJ_DATA
, obj
, reset
) ||
5213 nla_put_be64(skb
, NFTA_OBJ_HANDLE
, cpu_to_be64(obj
->handle
),
5215 goto nla_put_failure
;
5217 nlmsg_end(skb
, nlh
);
5221 nlmsg_trim(skb
, nlh
);
5225 struct nft_obj_filter
{
5230 static int nf_tables_dump_obj(struct sk_buff
*skb
, struct netlink_callback
*cb
)
5232 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
5233 const struct nft_table
*table
;
5234 unsigned int idx
= 0, s_idx
= cb
->args
[0];
5235 struct nft_obj_filter
*filter
= cb
->data
;
5236 struct net
*net
= sock_net(skb
->sk
);
5237 int family
= nfmsg
->nfgen_family
;
5238 struct nft_object
*obj
;
5241 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) == NFT_MSG_GETOBJ_RESET
)
5245 cb
->seq
= net
->nft
.base_seq
;
5247 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
5248 if (family
!= NFPROTO_UNSPEC
&& family
!= table
->family
)
5251 list_for_each_entry_rcu(obj
, &table
->objects
, list
) {
5252 if (!nft_is_active(net
, obj
))
5257 memset(&cb
->args
[1], 0,
5258 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
5259 if (filter
&& filter
->table
&&
5260 strcmp(filter
->table
, table
->name
))
5263 filter
->type
!= NFT_OBJECT_UNSPEC
&&
5264 obj
->ops
->type
->type
!= filter
->type
)
5267 if (nf_tables_fill_obj_info(skb
, net
, NETLINK_CB(cb
->skb
).portid
,
5270 NLM_F_MULTI
| NLM_F_APPEND
,
5271 table
->family
, table
,
5275 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
5287 static int nf_tables_dump_obj_start(struct netlink_callback
*cb
)
5289 const struct nlattr
* const *nla
= cb
->data
;
5290 struct nft_obj_filter
*filter
= NULL
;
5292 if (nla
[NFTA_OBJ_TABLE
] || nla
[NFTA_OBJ_TYPE
]) {
5293 filter
= kzalloc(sizeof(*filter
), GFP_ATOMIC
);
5297 if (nla
[NFTA_OBJ_TABLE
]) {
5298 filter
->table
= nla_strdup(nla
[NFTA_OBJ_TABLE
], GFP_ATOMIC
);
5299 if (!filter
->table
) {
5305 if (nla
[NFTA_OBJ_TYPE
])
5306 filter
->type
= ntohl(nla_get_be32(nla
[NFTA_OBJ_TYPE
]));
5313 static int nf_tables_dump_obj_done(struct netlink_callback
*cb
)
5315 struct nft_obj_filter
*filter
= cb
->data
;
5318 kfree(filter
->table
);
5325 /* called with rcu_read_lock held */
5326 static int nf_tables_getobj(struct net
*net
, struct sock
*nlsk
,
5327 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
5328 const struct nlattr
* const nla
[],
5329 struct netlink_ext_ack
*extack
)
5331 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
5332 u8 genmask
= nft_genmask_cur(net
);
5333 int family
= nfmsg
->nfgen_family
;
5334 const struct nft_table
*table
;
5335 struct nft_object
*obj
;
5336 struct sk_buff
*skb2
;
5341 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
5342 struct netlink_dump_control c
= {
5343 .start
= nf_tables_dump_obj_start
,
5344 .dump
= nf_tables_dump_obj
,
5345 .done
= nf_tables_dump_obj_done
,
5346 .module
= THIS_MODULE
,
5347 .data
= (void *)nla
,
5350 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
5353 if (!nla
[NFTA_OBJ_NAME
] ||
5354 !nla
[NFTA_OBJ_TYPE
])
5357 table
= nft_table_lookup(net
, nla
[NFTA_OBJ_TABLE
], family
, genmask
);
5358 if (IS_ERR(table
)) {
5359 NL_SET_BAD_ATTR(extack
, nla
[NFTA_OBJ_TABLE
]);
5360 return PTR_ERR(table
);
5363 objtype
= ntohl(nla_get_be32(nla
[NFTA_OBJ_TYPE
]));
5364 obj
= nft_obj_lookup(net
, table
, nla
[NFTA_OBJ_NAME
], objtype
, genmask
);
5366 NL_SET_BAD_ATTR(extack
, nla
[NFTA_OBJ_NAME
]);
5367 return PTR_ERR(obj
);
5370 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
5374 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) == NFT_MSG_GETOBJ_RESET
)
5377 err
= nf_tables_fill_obj_info(skb2
, net
, NETLINK_CB(skb
).portid
,
5378 nlh
->nlmsg_seq
, NFT_MSG_NEWOBJ
, 0,
5379 family
, table
, obj
, reset
);
5383 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
5389 static void nft_obj_destroy(const struct nft_ctx
*ctx
, struct nft_object
*obj
)
5391 if (obj
->ops
->destroy
)
5392 obj
->ops
->destroy(ctx
, obj
);
5394 module_put(obj
->ops
->type
->owner
);
5395 kfree(obj
->key
.name
);
5399 static int nf_tables_delobj(struct net
*net
, struct sock
*nlsk
,
5400 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
5401 const struct nlattr
* const nla
[],
5402 struct netlink_ext_ack
*extack
)
5404 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
5405 u8 genmask
= nft_genmask_next(net
);
5406 int family
= nfmsg
->nfgen_family
;
5407 const struct nlattr
*attr
;
5408 struct nft_table
*table
;
5409 struct nft_object
*obj
;
5413 if (!nla
[NFTA_OBJ_TYPE
] ||
5414 (!nla
[NFTA_OBJ_NAME
] && !nla
[NFTA_OBJ_HANDLE
]))
5417 table
= nft_table_lookup(net
, nla
[NFTA_OBJ_TABLE
], family
, genmask
);
5418 if (IS_ERR(table
)) {
5419 NL_SET_BAD_ATTR(extack
, nla
[NFTA_OBJ_TABLE
]);
5420 return PTR_ERR(table
);
5423 objtype
= ntohl(nla_get_be32(nla
[NFTA_OBJ_TYPE
]));
5424 if (nla
[NFTA_OBJ_HANDLE
]) {
5425 attr
= nla
[NFTA_OBJ_HANDLE
];
5426 obj
= nft_obj_lookup_byhandle(table
, attr
, objtype
, genmask
);
5428 attr
= nla
[NFTA_OBJ_NAME
];
5429 obj
= nft_obj_lookup(net
, table
, attr
, objtype
, genmask
);
5433 NL_SET_BAD_ATTR(extack
, attr
);
5434 return PTR_ERR(obj
);
5437 NL_SET_BAD_ATTR(extack
, attr
);
5441 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
5443 return nft_delobj(&ctx
, obj
);
5446 void nft_obj_notify(struct net
*net
, const struct nft_table
*table
,
5447 struct nft_object
*obj
, u32 portid
, u32 seq
, int event
,
5448 int family
, int report
, gfp_t gfp
)
5450 struct sk_buff
*skb
;
5454 !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
5457 skb
= nlmsg_new(NLMSG_GOODSIZE
, gfp
);
5461 err
= nf_tables_fill_obj_info(skb
, net
, portid
, seq
, event
, 0, family
,
5468 nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, report
, gfp
);
5471 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
5473 EXPORT_SYMBOL_GPL(nft_obj_notify
);
5475 static void nf_tables_obj_notify(const struct nft_ctx
*ctx
,
5476 struct nft_object
*obj
, int event
)
5478 nft_obj_notify(ctx
->net
, ctx
->table
, obj
, ctx
->portid
, ctx
->seq
, event
,
5479 ctx
->family
, ctx
->report
, GFP_KERNEL
);
5485 void nft_register_flowtable_type(struct nf_flowtable_type
*type
)
5487 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
5488 list_add_tail_rcu(&type
->list
, &nf_tables_flowtables
);
5489 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
5491 EXPORT_SYMBOL_GPL(nft_register_flowtable_type
);
5493 void nft_unregister_flowtable_type(struct nf_flowtable_type
*type
)
5495 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
5496 list_del_rcu(&type
->list
);
5497 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
5499 EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type
);
5501 static const struct nla_policy nft_flowtable_policy
[NFTA_FLOWTABLE_MAX
+ 1] = {
5502 [NFTA_FLOWTABLE_TABLE
] = { .type
= NLA_STRING
,
5503 .len
= NFT_NAME_MAXLEN
- 1 },
5504 [NFTA_FLOWTABLE_NAME
] = { .type
= NLA_STRING
,
5505 .len
= NFT_NAME_MAXLEN
- 1 },
5506 [NFTA_FLOWTABLE_HOOK
] = { .type
= NLA_NESTED
},
5507 [NFTA_FLOWTABLE_HANDLE
] = { .type
= NLA_U64
},
5510 struct nft_flowtable
*nft_flowtable_lookup(const struct nft_table
*table
,
5511 const struct nlattr
*nla
, u8 genmask
)
5513 struct nft_flowtable
*flowtable
;
5515 list_for_each_entry_rcu(flowtable
, &table
->flowtables
, list
) {
5516 if (!nla_strcmp(nla
, flowtable
->name
) &&
5517 nft_active_genmask(flowtable
, genmask
))
5520 return ERR_PTR(-ENOENT
);
5522 EXPORT_SYMBOL_GPL(nft_flowtable_lookup
);
5524 static struct nft_flowtable
*
5525 nft_flowtable_lookup_byhandle(const struct nft_table
*table
,
5526 const struct nlattr
*nla
, u8 genmask
)
5528 struct nft_flowtable
*flowtable
;
5530 list_for_each_entry(flowtable
, &table
->flowtables
, list
) {
5531 if (be64_to_cpu(nla_get_be64(nla
)) == flowtable
->handle
&&
5532 nft_active_genmask(flowtable
, genmask
))
5535 return ERR_PTR(-ENOENT
);
5538 static int nf_tables_parse_devices(const struct nft_ctx
*ctx
,
5539 const struct nlattr
*attr
,
5540 struct net_device
*dev_array
[], int *len
)
5542 const struct nlattr
*tmp
;
5543 struct net_device
*dev
;
5544 char ifname
[IFNAMSIZ
];
5545 int rem
, n
= 0, err
;
5547 nla_for_each_nested(tmp
, attr
, rem
) {
5548 if (nla_type(tmp
) != NFTA_DEVICE_NAME
) {
5553 nla_strlcpy(ifname
, tmp
, IFNAMSIZ
);
5554 dev
= __dev_get_by_name(ctx
->net
, ifname
);
5560 dev_array
[n
++] = dev
;
5561 if (n
== NFT_FLOWTABLE_DEVICE_MAX
) {
5575 static const struct nla_policy nft_flowtable_hook_policy
[NFTA_FLOWTABLE_HOOK_MAX
+ 1] = {
5576 [NFTA_FLOWTABLE_HOOK_NUM
] = { .type
= NLA_U32
},
5577 [NFTA_FLOWTABLE_HOOK_PRIORITY
] = { .type
= NLA_U32
},
5578 [NFTA_FLOWTABLE_HOOK_DEVS
] = { .type
= NLA_NESTED
},
5581 static int nf_tables_flowtable_parse_hook(const struct nft_ctx
*ctx
,
5582 const struct nlattr
*attr
,
5583 struct nft_flowtable
*flowtable
)
5585 struct net_device
*dev_array
[NFT_FLOWTABLE_DEVICE_MAX
];
5586 struct nlattr
*tb
[NFTA_FLOWTABLE_HOOK_MAX
+ 1];
5587 struct nf_hook_ops
*ops
;
5588 int hooknum
, priority
;
5591 err
= nla_parse_nested_deprecated(tb
, NFTA_FLOWTABLE_HOOK_MAX
, attr
,
5592 nft_flowtable_hook_policy
, NULL
);
5596 if (!tb
[NFTA_FLOWTABLE_HOOK_NUM
] ||
5597 !tb
[NFTA_FLOWTABLE_HOOK_PRIORITY
] ||
5598 !tb
[NFTA_FLOWTABLE_HOOK_DEVS
])
5601 hooknum
= ntohl(nla_get_be32(tb
[NFTA_FLOWTABLE_HOOK_NUM
]));
5602 if (hooknum
!= NF_NETDEV_INGRESS
)
5605 priority
= ntohl(nla_get_be32(tb
[NFTA_FLOWTABLE_HOOK_PRIORITY
]));
5607 err
= nf_tables_parse_devices(ctx
, tb
[NFTA_FLOWTABLE_HOOK_DEVS
],
5612 ops
= kcalloc(n
, sizeof(struct nf_hook_ops
), GFP_KERNEL
);
5616 flowtable
->hooknum
= hooknum
;
5617 flowtable
->priority
= priority
;
5618 flowtable
->ops
= ops
;
5619 flowtable
->ops_len
= n
;
5621 for (i
= 0; i
< n
; i
++) {
5622 flowtable
->ops
[i
].pf
= NFPROTO_NETDEV
;
5623 flowtable
->ops
[i
].hooknum
= hooknum
;
5624 flowtable
->ops
[i
].priority
= priority
;
5625 flowtable
->ops
[i
].priv
= &flowtable
->data
;
5626 flowtable
->ops
[i
].hook
= flowtable
->data
.type
->hook
;
5627 flowtable
->ops
[i
].dev
= dev_array
[i
];
5633 static const struct nf_flowtable_type
*__nft_flowtable_type_get(u8 family
)
5635 const struct nf_flowtable_type
*type
;
5637 list_for_each_entry(type
, &nf_tables_flowtables
, list
) {
5638 if (family
== type
->family
)
5644 static const struct nf_flowtable_type
*
5645 nft_flowtable_type_get(struct net
*net
, u8 family
)
5647 const struct nf_flowtable_type
*type
;
5649 type
= __nft_flowtable_type_get(family
);
5650 if (type
!= NULL
&& try_module_get(type
->owner
))
5653 lockdep_nfnl_nft_mutex_not_held();
5654 #ifdef CONFIG_MODULES
5656 nft_request_module(net
, "nf-flowtable-%u", family
);
5657 if (__nft_flowtable_type_get(family
))
5658 return ERR_PTR(-EAGAIN
);
5661 return ERR_PTR(-ENOENT
);
5664 static void nft_unregister_flowtable_net_hooks(struct net
*net
,
5665 struct nft_flowtable
*flowtable
)
5669 for (i
= 0; i
< flowtable
->ops_len
; i
++) {
5670 if (!flowtable
->ops
[i
].dev
)
5673 nf_unregister_net_hook(net
, &flowtable
->ops
[i
]);
5677 static int nf_tables_newflowtable(struct net
*net
, struct sock
*nlsk
,
5678 struct sk_buff
*skb
,
5679 const struct nlmsghdr
*nlh
,
5680 const struct nlattr
* const nla
[],
5681 struct netlink_ext_ack
*extack
)
5683 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
5684 const struct nf_flowtable_type
*type
;
5685 struct nft_flowtable
*flowtable
, *ft
;
5686 u8 genmask
= nft_genmask_next(net
);
5687 int family
= nfmsg
->nfgen_family
;
5688 struct nft_table
*table
;
5692 if (!nla
[NFTA_FLOWTABLE_TABLE
] ||
5693 !nla
[NFTA_FLOWTABLE_NAME
] ||
5694 !nla
[NFTA_FLOWTABLE_HOOK
])
5697 table
= nft_table_lookup(net
, nla
[NFTA_FLOWTABLE_TABLE
], family
,
5699 if (IS_ERR(table
)) {
5700 NL_SET_BAD_ATTR(extack
, nla
[NFTA_FLOWTABLE_TABLE
]);
5701 return PTR_ERR(table
);
5704 flowtable
= nft_flowtable_lookup(table
, nla
[NFTA_FLOWTABLE_NAME
],
5706 if (IS_ERR(flowtable
)) {
5707 err
= PTR_ERR(flowtable
);
5708 if (err
!= -ENOENT
) {
5709 NL_SET_BAD_ATTR(extack
, nla
[NFTA_FLOWTABLE_NAME
]);
5713 if (nlh
->nlmsg_flags
& NLM_F_EXCL
) {
5714 NL_SET_BAD_ATTR(extack
, nla
[NFTA_FLOWTABLE_NAME
]);
5721 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
5723 flowtable
= kzalloc(sizeof(*flowtable
), GFP_KERNEL
);
5727 flowtable
->table
= table
;
5728 flowtable
->handle
= nf_tables_alloc_handle(table
);
5730 flowtable
->name
= nla_strdup(nla
[NFTA_FLOWTABLE_NAME
], GFP_KERNEL
);
5731 if (!flowtable
->name
) {
5736 type
= nft_flowtable_type_get(net
, family
);
5738 err
= PTR_ERR(type
);
5742 flowtable
->data
.type
= type
;
5743 err
= type
->init(&flowtable
->data
);
5747 err
= nf_tables_flowtable_parse_hook(&ctx
, nla
[NFTA_FLOWTABLE_HOOK
],
5752 for (i
= 0; i
< flowtable
->ops_len
; i
++) {
5753 if (!flowtable
->ops
[i
].dev
)
5756 list_for_each_entry(ft
, &table
->flowtables
, list
) {
5757 for (k
= 0; k
< ft
->ops_len
; k
++) {
5758 if (!ft
->ops
[k
].dev
)
5761 if (flowtable
->ops
[i
].dev
== ft
->ops
[k
].dev
&&
5762 flowtable
->ops
[i
].pf
== ft
->ops
[k
].pf
) {
5769 err
= nf_register_net_hook(net
, &flowtable
->ops
[i
]);
5774 err
= nft_trans_flowtable_add(&ctx
, NFT_MSG_NEWFLOWTABLE
, flowtable
);
5778 list_add_tail_rcu(&flowtable
->list
, &table
->flowtables
);
5783 i
= flowtable
->ops_len
;
5785 for (k
= i
- 1; k
>= 0; k
--)
5786 nf_unregister_net_hook(net
, &flowtable
->ops
[k
]);
5788 kfree(flowtable
->ops
);
5790 flowtable
->data
.type
->free(&flowtable
->data
);
5792 module_put(type
->owner
);
5794 kfree(flowtable
->name
);
5800 static int nf_tables_delflowtable(struct net
*net
, struct sock
*nlsk
,
5801 struct sk_buff
*skb
,
5802 const struct nlmsghdr
*nlh
,
5803 const struct nlattr
* const nla
[],
5804 struct netlink_ext_ack
*extack
)
5806 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
5807 u8 genmask
= nft_genmask_next(net
);
5808 int family
= nfmsg
->nfgen_family
;
5809 struct nft_flowtable
*flowtable
;
5810 const struct nlattr
*attr
;
5811 struct nft_table
*table
;
5814 if (!nla
[NFTA_FLOWTABLE_TABLE
] ||
5815 (!nla
[NFTA_FLOWTABLE_NAME
] &&
5816 !nla
[NFTA_FLOWTABLE_HANDLE
]))
5819 table
= nft_table_lookup(net
, nla
[NFTA_FLOWTABLE_TABLE
], family
,
5821 if (IS_ERR(table
)) {
5822 NL_SET_BAD_ATTR(extack
, nla
[NFTA_FLOWTABLE_TABLE
]);
5823 return PTR_ERR(table
);
5826 if (nla
[NFTA_FLOWTABLE_HANDLE
]) {
5827 attr
= nla
[NFTA_FLOWTABLE_HANDLE
];
5828 flowtable
= nft_flowtable_lookup_byhandle(table
, attr
, genmask
);
5830 attr
= nla
[NFTA_FLOWTABLE_NAME
];
5831 flowtable
= nft_flowtable_lookup(table
, attr
, genmask
);
5834 if (IS_ERR(flowtable
)) {
5835 NL_SET_BAD_ATTR(extack
, attr
);
5836 return PTR_ERR(flowtable
);
5838 if (flowtable
->use
> 0) {
5839 NL_SET_BAD_ATTR(extack
, attr
);
5843 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
5845 return nft_delflowtable(&ctx
, flowtable
);
5848 static int nf_tables_fill_flowtable_info(struct sk_buff
*skb
, struct net
*net
,
5849 u32 portid
, u32 seq
, int event
,
5850 u32 flags
, int family
,
5851 struct nft_flowtable
*flowtable
)
5853 struct nlattr
*nest
, *nest_devs
;
5854 struct nfgenmsg
*nfmsg
;
5855 struct nlmsghdr
*nlh
;
5858 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
5859 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
5861 goto nla_put_failure
;
5863 nfmsg
= nlmsg_data(nlh
);
5864 nfmsg
->nfgen_family
= family
;
5865 nfmsg
->version
= NFNETLINK_V0
;
5866 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
5868 if (nla_put_string(skb
, NFTA_FLOWTABLE_TABLE
, flowtable
->table
->name
) ||
5869 nla_put_string(skb
, NFTA_FLOWTABLE_NAME
, flowtable
->name
) ||
5870 nla_put_be32(skb
, NFTA_FLOWTABLE_USE
, htonl(flowtable
->use
)) ||
5871 nla_put_be64(skb
, NFTA_FLOWTABLE_HANDLE
, cpu_to_be64(flowtable
->handle
),
5872 NFTA_FLOWTABLE_PAD
))
5873 goto nla_put_failure
;
5875 nest
= nla_nest_start_noflag(skb
, NFTA_FLOWTABLE_HOOK
);
5877 goto nla_put_failure
;
5878 if (nla_put_be32(skb
, NFTA_FLOWTABLE_HOOK_NUM
, htonl(flowtable
->hooknum
)) ||
5879 nla_put_be32(skb
, NFTA_FLOWTABLE_HOOK_PRIORITY
, htonl(flowtable
->priority
)))
5880 goto nla_put_failure
;
5882 nest_devs
= nla_nest_start_noflag(skb
, NFTA_FLOWTABLE_HOOK_DEVS
);
5884 goto nla_put_failure
;
5886 for (i
= 0; i
< flowtable
->ops_len
; i
++) {
5887 const struct net_device
*dev
= READ_ONCE(flowtable
->ops
[i
].dev
);
5890 nla_put_string(skb
, NFTA_DEVICE_NAME
, dev
->name
))
5891 goto nla_put_failure
;
5893 nla_nest_end(skb
, nest_devs
);
5894 nla_nest_end(skb
, nest
);
5896 nlmsg_end(skb
, nlh
);
5900 nlmsg_trim(skb
, nlh
);
5904 struct nft_flowtable_filter
{
5908 static int nf_tables_dump_flowtable(struct sk_buff
*skb
,
5909 struct netlink_callback
*cb
)
5911 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
5912 struct nft_flowtable_filter
*filter
= cb
->data
;
5913 unsigned int idx
= 0, s_idx
= cb
->args
[0];
5914 struct net
*net
= sock_net(skb
->sk
);
5915 int family
= nfmsg
->nfgen_family
;
5916 struct nft_flowtable
*flowtable
;
5917 const struct nft_table
*table
;
5920 cb
->seq
= net
->nft
.base_seq
;
5922 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
5923 if (family
!= NFPROTO_UNSPEC
&& family
!= table
->family
)
5926 list_for_each_entry_rcu(flowtable
, &table
->flowtables
, list
) {
5927 if (!nft_is_active(net
, flowtable
))
5932 memset(&cb
->args
[1], 0,
5933 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
5934 if (filter
&& filter
->table
&&
5935 strcmp(filter
->table
, table
->name
))
5938 if (nf_tables_fill_flowtable_info(skb
, net
, NETLINK_CB(cb
->skb
).portid
,
5940 NFT_MSG_NEWFLOWTABLE
,
5941 NLM_F_MULTI
| NLM_F_APPEND
,
5942 table
->family
, flowtable
) < 0)
5945 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
5957 static int nf_tables_dump_flowtable_start(struct netlink_callback
*cb
)
5959 const struct nlattr
* const *nla
= cb
->data
;
5960 struct nft_flowtable_filter
*filter
= NULL
;
5962 if (nla
[NFTA_FLOWTABLE_TABLE
]) {
5963 filter
= kzalloc(sizeof(*filter
), GFP_ATOMIC
);
5967 filter
->table
= nla_strdup(nla
[NFTA_FLOWTABLE_TABLE
],
5969 if (!filter
->table
) {
5979 static int nf_tables_dump_flowtable_done(struct netlink_callback
*cb
)
5981 struct nft_flowtable_filter
*filter
= cb
->data
;
5986 kfree(filter
->table
);
5992 /* called with rcu_read_lock held */
5993 static int nf_tables_getflowtable(struct net
*net
, struct sock
*nlsk
,
5994 struct sk_buff
*skb
,
5995 const struct nlmsghdr
*nlh
,
5996 const struct nlattr
* const nla
[],
5997 struct netlink_ext_ack
*extack
)
5999 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
6000 u8 genmask
= nft_genmask_cur(net
);
6001 int family
= nfmsg
->nfgen_family
;
6002 struct nft_flowtable
*flowtable
;
6003 const struct nft_table
*table
;
6004 struct sk_buff
*skb2
;
6007 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
6008 struct netlink_dump_control c
= {
6009 .start
= nf_tables_dump_flowtable_start
,
6010 .dump
= nf_tables_dump_flowtable
,
6011 .done
= nf_tables_dump_flowtable_done
,
6012 .module
= THIS_MODULE
,
6013 .data
= (void *)nla
,
6016 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
6019 if (!nla
[NFTA_FLOWTABLE_NAME
])
6022 table
= nft_table_lookup(net
, nla
[NFTA_FLOWTABLE_TABLE
], family
,
6025 return PTR_ERR(table
);
6027 flowtable
= nft_flowtable_lookup(table
, nla
[NFTA_FLOWTABLE_NAME
],
6029 if (IS_ERR(flowtable
))
6030 return PTR_ERR(flowtable
);
6032 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
6036 err
= nf_tables_fill_flowtable_info(skb2
, net
, NETLINK_CB(skb
).portid
,
6038 NFT_MSG_NEWFLOWTABLE
, 0, family
,
6043 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
6049 static void nf_tables_flowtable_notify(struct nft_ctx
*ctx
,
6050 struct nft_flowtable
*flowtable
,
6053 struct sk_buff
*skb
;
6057 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
6060 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
6064 err
= nf_tables_fill_flowtable_info(skb
, ctx
->net
, ctx
->portid
,
6066 ctx
->family
, flowtable
);
6072 nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
6073 ctx
->report
, GFP_KERNEL
);
6076 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
6079 static void nf_tables_flowtable_destroy(struct nft_flowtable
*flowtable
)
6081 kfree(flowtable
->ops
);
6082 kfree(flowtable
->name
);
6083 flowtable
->data
.type
->free(&flowtable
->data
);
6084 module_put(flowtable
->data
.type
->owner
);
6088 static int nf_tables_fill_gen_info(struct sk_buff
*skb
, struct net
*net
,
6089 u32 portid
, u32 seq
)
6091 struct nlmsghdr
*nlh
;
6092 struct nfgenmsg
*nfmsg
;
6093 char buf
[TASK_COMM_LEN
];
6094 int event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, NFT_MSG_NEWGEN
);
6096 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), 0);
6098 goto nla_put_failure
;
6100 nfmsg
= nlmsg_data(nlh
);
6101 nfmsg
->nfgen_family
= AF_UNSPEC
;
6102 nfmsg
->version
= NFNETLINK_V0
;
6103 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
6105 if (nla_put_be32(skb
, NFTA_GEN_ID
, htonl(net
->nft
.base_seq
)) ||
6106 nla_put_be32(skb
, NFTA_GEN_PROC_PID
, htonl(task_pid_nr(current
))) ||
6107 nla_put_string(skb
, NFTA_GEN_PROC_NAME
, get_task_comm(buf
, current
)))
6108 goto nla_put_failure
;
6110 nlmsg_end(skb
, nlh
);
6114 nlmsg_trim(skb
, nlh
);
6118 static void nft_flowtable_event(unsigned long event
, struct net_device
*dev
,
6119 struct nft_flowtable
*flowtable
)
6123 for (i
= 0; i
< flowtable
->ops_len
; i
++) {
6124 if (flowtable
->ops
[i
].dev
!= dev
)
6127 nf_unregister_net_hook(dev_net(dev
), &flowtable
->ops
[i
]);
6128 flowtable
->ops
[i
].dev
= NULL
;
6133 static int nf_tables_flowtable_event(struct notifier_block
*this,
6134 unsigned long event
, void *ptr
)
6136 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
6137 struct nft_flowtable
*flowtable
;
6138 struct nft_table
*table
;
6141 if (event
!= NETDEV_UNREGISTER
)
6145 mutex_lock(&net
->nft
.commit_mutex
);
6146 list_for_each_entry(table
, &net
->nft
.tables
, list
) {
6147 list_for_each_entry(flowtable
, &table
->flowtables
, list
) {
6148 nft_flowtable_event(event
, dev
, flowtable
);
6151 mutex_unlock(&net
->nft
.commit_mutex
);
6156 static struct notifier_block nf_tables_flowtable_notifier
= {
6157 .notifier_call
= nf_tables_flowtable_event
,
6160 static void nf_tables_gen_notify(struct net
*net
, struct sk_buff
*skb
,
6163 struct nlmsghdr
*nlh
= nlmsg_hdr(skb
);
6164 struct sk_buff
*skb2
;
6167 if (nlmsg_report(nlh
) &&
6168 !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
6171 skb2
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
6175 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
6182 nfnetlink_send(skb2
, net
, NETLINK_CB(skb
).portid
, NFNLGRP_NFTABLES
,
6183 nlmsg_report(nlh
), GFP_KERNEL
);
6186 nfnetlink_set_err(net
, NETLINK_CB(skb
).portid
, NFNLGRP_NFTABLES
,
6190 static int nf_tables_getgen(struct net
*net
, struct sock
*nlsk
,
6191 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
6192 const struct nlattr
* const nla
[],
6193 struct netlink_ext_ack
*extack
)
6195 struct sk_buff
*skb2
;
6198 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
6202 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
6207 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
6213 static const struct nfnl_callback nf_tables_cb
[NFT_MSG_MAX
] = {
6214 [NFT_MSG_NEWTABLE
] = {
6215 .call_batch
= nf_tables_newtable
,
6216 .attr_count
= NFTA_TABLE_MAX
,
6217 .policy
= nft_table_policy
,
6219 [NFT_MSG_GETTABLE
] = {
6220 .call_rcu
= nf_tables_gettable
,
6221 .attr_count
= NFTA_TABLE_MAX
,
6222 .policy
= nft_table_policy
,
6224 [NFT_MSG_DELTABLE
] = {
6225 .call_batch
= nf_tables_deltable
,
6226 .attr_count
= NFTA_TABLE_MAX
,
6227 .policy
= nft_table_policy
,
6229 [NFT_MSG_NEWCHAIN
] = {
6230 .call_batch
= nf_tables_newchain
,
6231 .attr_count
= NFTA_CHAIN_MAX
,
6232 .policy
= nft_chain_policy
,
6234 [NFT_MSG_GETCHAIN
] = {
6235 .call_rcu
= nf_tables_getchain
,
6236 .attr_count
= NFTA_CHAIN_MAX
,
6237 .policy
= nft_chain_policy
,
6239 [NFT_MSG_DELCHAIN
] = {
6240 .call_batch
= nf_tables_delchain
,
6241 .attr_count
= NFTA_CHAIN_MAX
,
6242 .policy
= nft_chain_policy
,
6244 [NFT_MSG_NEWRULE
] = {
6245 .call_batch
= nf_tables_newrule
,
6246 .attr_count
= NFTA_RULE_MAX
,
6247 .policy
= nft_rule_policy
,
6249 [NFT_MSG_GETRULE
] = {
6250 .call_rcu
= nf_tables_getrule
,
6251 .attr_count
= NFTA_RULE_MAX
,
6252 .policy
= nft_rule_policy
,
6254 [NFT_MSG_DELRULE
] = {
6255 .call_batch
= nf_tables_delrule
,
6256 .attr_count
= NFTA_RULE_MAX
,
6257 .policy
= nft_rule_policy
,
6259 [NFT_MSG_NEWSET
] = {
6260 .call_batch
= nf_tables_newset
,
6261 .attr_count
= NFTA_SET_MAX
,
6262 .policy
= nft_set_policy
,
6264 [NFT_MSG_GETSET
] = {
6265 .call_rcu
= nf_tables_getset
,
6266 .attr_count
= NFTA_SET_MAX
,
6267 .policy
= nft_set_policy
,
6269 [NFT_MSG_DELSET
] = {
6270 .call_batch
= nf_tables_delset
,
6271 .attr_count
= NFTA_SET_MAX
,
6272 .policy
= nft_set_policy
,
6274 [NFT_MSG_NEWSETELEM
] = {
6275 .call_batch
= nf_tables_newsetelem
,
6276 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
6277 .policy
= nft_set_elem_list_policy
,
6279 [NFT_MSG_GETSETELEM
] = {
6280 .call_rcu
= nf_tables_getsetelem
,
6281 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
6282 .policy
= nft_set_elem_list_policy
,
6284 [NFT_MSG_DELSETELEM
] = {
6285 .call_batch
= nf_tables_delsetelem
,
6286 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
6287 .policy
= nft_set_elem_list_policy
,
6289 [NFT_MSG_GETGEN
] = {
6290 .call_rcu
= nf_tables_getgen
,
6292 [NFT_MSG_NEWOBJ
] = {
6293 .call_batch
= nf_tables_newobj
,
6294 .attr_count
= NFTA_OBJ_MAX
,
6295 .policy
= nft_obj_policy
,
6297 [NFT_MSG_GETOBJ
] = {
6298 .call_rcu
= nf_tables_getobj
,
6299 .attr_count
= NFTA_OBJ_MAX
,
6300 .policy
= nft_obj_policy
,
6302 [NFT_MSG_DELOBJ
] = {
6303 .call_batch
= nf_tables_delobj
,
6304 .attr_count
= NFTA_OBJ_MAX
,
6305 .policy
= nft_obj_policy
,
6307 [NFT_MSG_GETOBJ_RESET
] = {
6308 .call_rcu
= nf_tables_getobj
,
6309 .attr_count
= NFTA_OBJ_MAX
,
6310 .policy
= nft_obj_policy
,
6312 [NFT_MSG_NEWFLOWTABLE
] = {
6313 .call_batch
= nf_tables_newflowtable
,
6314 .attr_count
= NFTA_FLOWTABLE_MAX
,
6315 .policy
= nft_flowtable_policy
,
6317 [NFT_MSG_GETFLOWTABLE
] = {
6318 .call_rcu
= nf_tables_getflowtable
,
6319 .attr_count
= NFTA_FLOWTABLE_MAX
,
6320 .policy
= nft_flowtable_policy
,
6322 [NFT_MSG_DELFLOWTABLE
] = {
6323 .call_batch
= nf_tables_delflowtable
,
6324 .attr_count
= NFTA_FLOWTABLE_MAX
,
6325 .policy
= nft_flowtable_policy
,
6329 static int nf_tables_validate(struct net
*net
)
6331 struct nft_table
*table
;
6333 switch (net
->nft
.validate_state
) {
6334 case NFT_VALIDATE_SKIP
:
6336 case NFT_VALIDATE_NEED
:
6337 nft_validate_state_update(net
, NFT_VALIDATE_DO
);
6339 case NFT_VALIDATE_DO
:
6340 list_for_each_entry(table
, &net
->nft
.tables
, list
) {
6341 if (nft_table_validate(net
, table
) < 0)
6350 /* a drop policy has to be deferred until all rules have been activated,
6351 * otherwise a large ruleset that contains a drop-policy base chain will
6352 * cause all packets to get dropped until the full transaction has been
6355 * We defer the drop policy until the transaction has been finalized.
6357 static void nft_chain_commit_drop_policy(struct nft_trans
*trans
)
6359 struct nft_base_chain
*basechain
;
6361 if (nft_trans_chain_policy(trans
) != NF_DROP
)
6364 if (!nft_is_base_chain(trans
->ctx
.chain
))
6367 basechain
= nft_base_chain(trans
->ctx
.chain
);
6368 basechain
->policy
= NF_DROP
;
6371 static void nft_chain_commit_update(struct nft_trans
*trans
)
6373 struct nft_base_chain
*basechain
;
6375 if (nft_trans_chain_name(trans
)) {
6376 rhltable_remove(&trans
->ctx
.table
->chains_ht
,
6377 &trans
->ctx
.chain
->rhlhead
,
6378 nft_chain_ht_params
);
6379 swap(trans
->ctx
.chain
->name
, nft_trans_chain_name(trans
));
6380 rhltable_insert_key(&trans
->ctx
.table
->chains_ht
,
6381 trans
->ctx
.chain
->name
,
6382 &trans
->ctx
.chain
->rhlhead
,
6383 nft_chain_ht_params
);
6386 if (!nft_is_base_chain(trans
->ctx
.chain
))
6389 nft_chain_stats_replace(trans
);
6391 basechain
= nft_base_chain(trans
->ctx
.chain
);
6393 switch (nft_trans_chain_policy(trans
)) {
6396 basechain
->policy
= nft_trans_chain_policy(trans
);
6401 static void nft_commit_release(struct nft_trans
*trans
)
6403 switch (trans
->msg_type
) {
6404 case NFT_MSG_DELTABLE
:
6405 nf_tables_table_destroy(&trans
->ctx
);
6407 case NFT_MSG_NEWCHAIN
:
6408 free_percpu(nft_trans_chain_stats(trans
));
6409 kfree(nft_trans_chain_name(trans
));
6411 case NFT_MSG_DELCHAIN
:
6412 nf_tables_chain_destroy(&trans
->ctx
);
6414 case NFT_MSG_DELRULE
:
6415 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
6417 case NFT_MSG_DELSET
:
6418 nft_set_destroy(nft_trans_set(trans
));
6420 case NFT_MSG_DELSETELEM
:
6421 nf_tables_set_elem_destroy(&trans
->ctx
,
6422 nft_trans_elem_set(trans
),
6423 nft_trans_elem(trans
).priv
);
6425 case NFT_MSG_DELOBJ
:
6426 nft_obj_destroy(&trans
->ctx
, nft_trans_obj(trans
));
6428 case NFT_MSG_DELFLOWTABLE
:
6429 nf_tables_flowtable_destroy(nft_trans_flowtable(trans
));
6434 put_net(trans
->ctx
.net
);
6439 static void nf_tables_trans_destroy_work(struct work_struct
*w
)
6441 struct nft_trans
*trans
, *next
;
6444 spin_lock(&nf_tables_destroy_list_lock
);
6445 list_splice_init(&nf_tables_destroy_list
, &head
);
6446 spin_unlock(&nf_tables_destroy_list_lock
);
6448 if (list_empty(&head
))
6453 list_for_each_entry_safe(trans
, next
, &head
, list
) {
6454 list_del(&trans
->list
);
6455 nft_commit_release(trans
);
6459 static int nf_tables_commit_chain_prepare(struct net
*net
, struct nft_chain
*chain
)
6461 struct nft_rule
*rule
;
6462 unsigned int alloc
= 0;
6465 /* already handled or inactive chain? */
6466 if (chain
->rules_next
|| !nft_is_active_next(net
, chain
))
6469 rule
= list_entry(&chain
->rules
, struct nft_rule
, list
);
6472 list_for_each_entry_continue(rule
, &chain
->rules
, list
) {
6473 if (nft_is_active_next(net
, rule
))
6477 chain
->rules_next
= nf_tables_chain_alloc_rules(chain
, alloc
);
6478 if (!chain
->rules_next
)
6481 list_for_each_entry_continue(rule
, &chain
->rules
, list
) {
6482 if (nft_is_active_next(net
, rule
))
6483 chain
->rules_next
[i
++] = rule
;
6486 chain
->rules_next
[i
] = NULL
;
6490 static void nf_tables_commit_chain_prepare_cancel(struct net
*net
)
6492 struct nft_trans
*trans
, *next
;
6494 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
6495 struct nft_chain
*chain
= trans
->ctx
.chain
;
6497 if (trans
->msg_type
== NFT_MSG_NEWRULE
||
6498 trans
->msg_type
== NFT_MSG_DELRULE
) {
6499 kvfree(chain
->rules_next
);
6500 chain
->rules_next
= NULL
;
6505 static void __nf_tables_commit_chain_free_rules_old(struct rcu_head
*h
)
6507 struct nft_rules_old
*o
= container_of(h
, struct nft_rules_old
, h
);
6512 static void nf_tables_commit_chain_free_rules_old(struct nft_rule
**rules
)
6514 struct nft_rule
**r
= rules
;
6515 struct nft_rules_old
*old
;
6520 r
++; /* rcu_head is after end marker */
6524 call_rcu(&old
->h
, __nf_tables_commit_chain_free_rules_old
);
6527 static void nf_tables_commit_chain(struct net
*net
, struct nft_chain
*chain
)
6529 struct nft_rule
**g0
, **g1
;
6532 next_genbit
= nft_gencursor_next(net
);
6534 g0
= rcu_dereference_protected(chain
->rules_gen_0
,
6535 lockdep_commit_lock_is_held(net
));
6536 g1
= rcu_dereference_protected(chain
->rules_gen_1
,
6537 lockdep_commit_lock_is_held(net
));
6539 /* No changes to this chain? */
6540 if (chain
->rules_next
== NULL
) {
6541 /* chain had no change in last or next generation */
6545 * chain had no change in this generation; make sure next
6546 * one uses same rules as current generation.
6549 rcu_assign_pointer(chain
->rules_gen_1
, g0
);
6550 nf_tables_commit_chain_free_rules_old(g1
);
6552 rcu_assign_pointer(chain
->rules_gen_0
, g1
);
6553 nf_tables_commit_chain_free_rules_old(g0
);
6560 rcu_assign_pointer(chain
->rules_gen_1
, chain
->rules_next
);
6562 rcu_assign_pointer(chain
->rules_gen_0
, chain
->rules_next
);
6564 chain
->rules_next
= NULL
;
6570 nf_tables_commit_chain_free_rules_old(g1
);
6572 nf_tables_commit_chain_free_rules_old(g0
);
6575 static void nft_obj_del(struct nft_object
*obj
)
6577 rhltable_remove(&nft_objname_ht
, &obj
->rhlhead
, nft_objname_ht_params
);
6578 list_del_rcu(&obj
->list
);
6581 static void nft_chain_del(struct nft_chain
*chain
)
6583 struct nft_table
*table
= chain
->table
;
6585 WARN_ON_ONCE(rhltable_remove(&table
->chains_ht
, &chain
->rhlhead
,
6586 nft_chain_ht_params
));
6587 list_del_rcu(&chain
->list
);
6590 static void nf_tables_commit_release(struct net
*net
)
6592 struct nft_trans
*trans
;
6594 /* all side effects have to be made visible.
6595 * For example, if a chain named 'foo' has been deleted, a
6596 * new transaction must not find it anymore.
6598 * Memory reclaim happens asynchronously from work queue
6599 * to prevent expensive synchronize_rcu() in commit phase.
6601 if (list_empty(&net
->nft
.commit_list
)) {
6602 mutex_unlock(&net
->nft
.commit_mutex
);
6606 trans
= list_last_entry(&net
->nft
.commit_list
,
6607 struct nft_trans
, list
);
6608 get_net(trans
->ctx
.net
);
6609 WARN_ON_ONCE(trans
->put_net
);
6611 trans
->put_net
= true;
6612 spin_lock(&nf_tables_destroy_list_lock
);
6613 list_splice_tail_init(&net
->nft
.commit_list
, &nf_tables_destroy_list
);
6614 spin_unlock(&nf_tables_destroy_list_lock
);
6616 mutex_unlock(&net
->nft
.commit_mutex
);
6618 schedule_work(&trans_destroy_work
);
6621 static int nf_tables_commit(struct net
*net
, struct sk_buff
*skb
)
6623 struct nft_trans
*trans
, *next
;
6624 struct nft_trans_elem
*te
;
6625 struct nft_chain
*chain
;
6626 struct nft_table
*table
;
6628 if (list_empty(&net
->nft
.commit_list
)) {
6629 mutex_unlock(&net
->nft
.commit_mutex
);
6633 /* 0. Validate ruleset, otherwise roll back for error reporting. */
6634 if (nf_tables_validate(net
) < 0)
6637 /* 1. Allocate space for next generation rules_gen_X[] */
6638 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
6641 if (trans
->msg_type
== NFT_MSG_NEWRULE
||
6642 trans
->msg_type
== NFT_MSG_DELRULE
) {
6643 chain
= trans
->ctx
.chain
;
6645 ret
= nf_tables_commit_chain_prepare(net
, chain
);
6647 nf_tables_commit_chain_prepare_cancel(net
);
6653 /* step 2. Make rules_gen_X visible to packet path */
6654 list_for_each_entry(table
, &net
->nft
.tables
, list
) {
6655 list_for_each_entry(chain
, &table
->chains
, list
)
6656 nf_tables_commit_chain(net
, chain
);
6660 * Bump generation counter, invalidate any dump in progress.
6661 * Cannot fail after this point.
6663 while (++net
->nft
.base_seq
== 0);
6665 /* step 3. Start new generation, rules_gen_X now in use. */
6666 net
->nft
.gencursor
= nft_gencursor_next(net
);
6668 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
6669 switch (trans
->msg_type
) {
6670 case NFT_MSG_NEWTABLE
:
6671 if (nft_trans_table_update(trans
)) {
6672 if (!nft_trans_table_enable(trans
)) {
6673 nf_tables_table_disable(net
,
6675 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
6678 nft_clear(net
, trans
->ctx
.table
);
6680 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_NEWTABLE
);
6681 nft_trans_destroy(trans
);
6683 case NFT_MSG_DELTABLE
:
6684 list_del_rcu(&trans
->ctx
.table
->list
);
6685 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_DELTABLE
);
6687 case NFT_MSG_NEWCHAIN
:
6688 if (nft_trans_chain_update(trans
)) {
6689 nft_chain_commit_update(trans
);
6690 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_NEWCHAIN
);
6691 /* trans destroyed after rcu grace period */
6693 nft_chain_commit_drop_policy(trans
);
6694 nft_clear(net
, trans
->ctx
.chain
);
6695 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_NEWCHAIN
);
6696 nft_trans_destroy(trans
);
6699 case NFT_MSG_DELCHAIN
:
6700 nft_chain_del(trans
->ctx
.chain
);
6701 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_DELCHAIN
);
6702 nf_tables_unregister_hook(trans
->ctx
.net
,
6706 case NFT_MSG_NEWRULE
:
6707 nft_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
6708 nf_tables_rule_notify(&trans
->ctx
,
6709 nft_trans_rule(trans
),
6711 nft_trans_destroy(trans
);
6713 case NFT_MSG_DELRULE
:
6714 list_del_rcu(&nft_trans_rule(trans
)->list
);
6715 nf_tables_rule_notify(&trans
->ctx
,
6716 nft_trans_rule(trans
),
6718 nft_rule_expr_deactivate(&trans
->ctx
,
6719 nft_trans_rule(trans
),
6722 case NFT_MSG_NEWSET
:
6723 nft_clear(net
, nft_trans_set(trans
));
6724 /* This avoids hitting -EBUSY when deleting the table
6725 * from the transaction.
6727 if (nft_set_is_anonymous(nft_trans_set(trans
)) &&
6728 !list_empty(&nft_trans_set(trans
)->bindings
))
6729 trans
->ctx
.table
->use
--;
6731 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
6732 NFT_MSG_NEWSET
, GFP_KERNEL
);
6733 nft_trans_destroy(trans
);
6735 case NFT_MSG_DELSET
:
6736 list_del_rcu(&nft_trans_set(trans
)->list
);
6737 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
6738 NFT_MSG_DELSET
, GFP_KERNEL
);
6740 case NFT_MSG_NEWSETELEM
:
6741 te
= (struct nft_trans_elem
*)trans
->data
;
6743 te
->set
->ops
->activate(net
, te
->set
, &te
->elem
);
6744 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
6746 NFT_MSG_NEWSETELEM
, 0);
6747 nft_trans_destroy(trans
);
6749 case NFT_MSG_DELSETELEM
:
6750 te
= (struct nft_trans_elem
*)trans
->data
;
6752 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
6754 NFT_MSG_DELSETELEM
, 0);
6755 te
->set
->ops
->remove(net
, te
->set
, &te
->elem
);
6756 atomic_dec(&te
->set
->nelems
);
6759 case NFT_MSG_NEWOBJ
:
6760 nft_clear(net
, nft_trans_obj(trans
));
6761 nf_tables_obj_notify(&trans
->ctx
, nft_trans_obj(trans
),
6763 nft_trans_destroy(trans
);
6765 case NFT_MSG_DELOBJ
:
6766 nft_obj_del(nft_trans_obj(trans
));
6767 nf_tables_obj_notify(&trans
->ctx
, nft_trans_obj(trans
),
6770 case NFT_MSG_NEWFLOWTABLE
:
6771 nft_clear(net
, nft_trans_flowtable(trans
));
6772 nf_tables_flowtable_notify(&trans
->ctx
,
6773 nft_trans_flowtable(trans
),
6774 NFT_MSG_NEWFLOWTABLE
);
6775 nft_trans_destroy(trans
);
6777 case NFT_MSG_DELFLOWTABLE
:
6778 list_del_rcu(&nft_trans_flowtable(trans
)->list
);
6779 nf_tables_flowtable_notify(&trans
->ctx
,
6780 nft_trans_flowtable(trans
),
6781 NFT_MSG_DELFLOWTABLE
);
6782 nft_unregister_flowtable_net_hooks(net
,
6783 nft_trans_flowtable(trans
));
6788 nf_tables_gen_notify(net
, skb
, NFT_MSG_NEWGEN
);
6789 nf_tables_commit_release(net
);
6794 static void nf_tables_abort_release(struct nft_trans
*trans
)
6796 switch (trans
->msg_type
) {
6797 case NFT_MSG_NEWTABLE
:
6798 nf_tables_table_destroy(&trans
->ctx
);
6800 case NFT_MSG_NEWCHAIN
:
6801 nf_tables_chain_destroy(&trans
->ctx
);
6803 case NFT_MSG_NEWRULE
:
6804 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
6806 case NFT_MSG_NEWSET
:
6807 nft_set_destroy(nft_trans_set(trans
));
6809 case NFT_MSG_NEWSETELEM
:
6810 nft_set_elem_destroy(nft_trans_elem_set(trans
),
6811 nft_trans_elem(trans
).priv
, true);
6813 case NFT_MSG_NEWOBJ
:
6814 nft_obj_destroy(&trans
->ctx
, nft_trans_obj(trans
));
6816 case NFT_MSG_NEWFLOWTABLE
:
6817 nf_tables_flowtable_destroy(nft_trans_flowtable(trans
));
6823 static int __nf_tables_abort(struct net
*net
)
6825 struct nft_trans
*trans
, *next
;
6826 struct nft_trans_elem
*te
;
6828 list_for_each_entry_safe_reverse(trans
, next
, &net
->nft
.commit_list
,
6830 switch (trans
->msg_type
) {
6831 case NFT_MSG_NEWTABLE
:
6832 if (nft_trans_table_update(trans
)) {
6833 if (nft_trans_table_enable(trans
)) {
6834 nf_tables_table_disable(net
,
6836 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
6838 nft_trans_destroy(trans
);
6840 list_del_rcu(&trans
->ctx
.table
->list
);
6843 case NFT_MSG_DELTABLE
:
6844 nft_clear(trans
->ctx
.net
, trans
->ctx
.table
);
6845 nft_trans_destroy(trans
);
6847 case NFT_MSG_NEWCHAIN
:
6848 if (nft_trans_chain_update(trans
)) {
6849 free_percpu(nft_trans_chain_stats(trans
));
6850 kfree(nft_trans_chain_name(trans
));
6851 nft_trans_destroy(trans
);
6853 trans
->ctx
.table
->use
--;
6854 nft_chain_del(trans
->ctx
.chain
);
6855 nf_tables_unregister_hook(trans
->ctx
.net
,
6860 case NFT_MSG_DELCHAIN
:
6861 trans
->ctx
.table
->use
++;
6862 nft_clear(trans
->ctx
.net
, trans
->ctx
.chain
);
6863 nft_trans_destroy(trans
);
6865 case NFT_MSG_NEWRULE
:
6866 trans
->ctx
.chain
->use
--;
6867 list_del_rcu(&nft_trans_rule(trans
)->list
);
6868 nft_rule_expr_deactivate(&trans
->ctx
,
6869 nft_trans_rule(trans
),
6872 case NFT_MSG_DELRULE
:
6873 trans
->ctx
.chain
->use
++;
6874 nft_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
6875 nft_rule_expr_activate(&trans
->ctx
, nft_trans_rule(trans
));
6876 nft_trans_destroy(trans
);
6878 case NFT_MSG_NEWSET
:
6879 trans
->ctx
.table
->use
--;
6880 if (nft_trans_set(trans
)->bound
) {
6881 nft_trans_destroy(trans
);
6884 list_del_rcu(&nft_trans_set(trans
)->list
);
6886 case NFT_MSG_DELSET
:
6887 trans
->ctx
.table
->use
++;
6888 nft_clear(trans
->ctx
.net
, nft_trans_set(trans
));
6889 nft_trans_destroy(trans
);
6891 case NFT_MSG_NEWSETELEM
:
6892 if (nft_trans_elem_set(trans
)->bound
) {
6893 nft_trans_destroy(trans
);
6896 te
= (struct nft_trans_elem
*)trans
->data
;
6897 te
->set
->ops
->remove(net
, te
->set
, &te
->elem
);
6898 atomic_dec(&te
->set
->nelems
);
6900 case NFT_MSG_DELSETELEM
:
6901 te
= (struct nft_trans_elem
*)trans
->data
;
6903 nft_set_elem_activate(net
, te
->set
, &te
->elem
);
6904 te
->set
->ops
->activate(net
, te
->set
, &te
->elem
);
6907 nft_trans_destroy(trans
);
6909 case NFT_MSG_NEWOBJ
:
6910 trans
->ctx
.table
->use
--;
6911 nft_obj_del(nft_trans_obj(trans
));
6913 case NFT_MSG_DELOBJ
:
6914 trans
->ctx
.table
->use
++;
6915 nft_clear(trans
->ctx
.net
, nft_trans_obj(trans
));
6916 nft_trans_destroy(trans
);
6918 case NFT_MSG_NEWFLOWTABLE
:
6919 trans
->ctx
.table
->use
--;
6920 list_del_rcu(&nft_trans_flowtable(trans
)->list
);
6921 nft_unregister_flowtable_net_hooks(net
,
6922 nft_trans_flowtable(trans
));
6924 case NFT_MSG_DELFLOWTABLE
:
6925 trans
->ctx
.table
->use
++;
6926 nft_clear(trans
->ctx
.net
, nft_trans_flowtable(trans
));
6927 nft_trans_destroy(trans
);
6934 list_for_each_entry_safe_reverse(trans
, next
,
6935 &net
->nft
.commit_list
, list
) {
6936 list_del(&trans
->list
);
6937 nf_tables_abort_release(trans
);
6943 static void nf_tables_cleanup(struct net
*net
)
6945 nft_validate_state_update(net
, NFT_VALIDATE_SKIP
);
6948 static int nf_tables_abort(struct net
*net
, struct sk_buff
*skb
)
6950 int ret
= __nf_tables_abort(net
);
6952 mutex_unlock(&net
->nft
.commit_mutex
);
6957 static bool nf_tables_valid_genid(struct net
*net
, u32 genid
)
6961 mutex_lock(&net
->nft
.commit_mutex
);
6963 genid_ok
= genid
== 0 || net
->nft
.base_seq
== genid
;
6965 mutex_unlock(&net
->nft
.commit_mutex
);
6967 /* else, commit mutex has to be released by commit or abort function */
6971 static const struct nfnetlink_subsystem nf_tables_subsys
= {
6972 .name
= "nf_tables",
6973 .subsys_id
= NFNL_SUBSYS_NFTABLES
,
6974 .cb_count
= NFT_MSG_MAX
,
6976 .commit
= nf_tables_commit
,
6977 .abort
= nf_tables_abort
,
6978 .cleanup
= nf_tables_cleanup
,
6979 .valid_genid
= nf_tables_valid_genid
,
6980 .owner
= THIS_MODULE
,
6983 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
6984 enum nft_chain_types type
)
6986 const struct nft_base_chain
*basechain
;
6988 if (nft_is_base_chain(chain
)) {
6989 basechain
= nft_base_chain(chain
);
6990 if (basechain
->type
->type
!= type
)
6995 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency
);
6997 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
6998 unsigned int hook_flags
)
7000 struct nft_base_chain
*basechain
;
7002 if (nft_is_base_chain(chain
)) {
7003 basechain
= nft_base_chain(chain
);
7005 if ((1 << basechain
->ops
.hooknum
) & hook_flags
)
7013 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks
);
7016 * Loop detection - walk through the ruleset beginning at the destination chain
7017 * of a new jump until either the source chain is reached (loop) or all
7018 * reachable chains have been traversed.
7020 * The loop check is performed whenever a new jump verdict is added to an
7021 * expression or verdict map or a verdict map is bound to a new chain.
7024 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
7025 const struct nft_chain
*chain
);
7027 static int nf_tables_loop_check_setelem(const struct nft_ctx
*ctx
,
7028 struct nft_set
*set
,
7029 const struct nft_set_iter
*iter
,
7030 struct nft_set_elem
*elem
)
7032 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
7033 const struct nft_data
*data
;
7035 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
7036 *nft_set_ext_flags(ext
) & NFT_SET_ELEM_INTERVAL_END
)
7039 data
= nft_set_ext_data(ext
);
7040 switch (data
->verdict
.code
) {
7043 return nf_tables_check_loops(ctx
, data
->verdict
.chain
);
7049 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
7050 const struct nft_chain
*chain
)
7052 const struct nft_rule
*rule
;
7053 const struct nft_expr
*expr
, *last
;
7054 struct nft_set
*set
;
7055 struct nft_set_binding
*binding
;
7056 struct nft_set_iter iter
;
7058 if (ctx
->chain
== chain
)
7061 list_for_each_entry(rule
, &chain
->rules
, list
) {
7062 nft_rule_for_each_expr(expr
, last
, rule
) {
7063 struct nft_immediate_expr
*priv
;
7064 const struct nft_data
*data
;
7067 if (strcmp(expr
->ops
->type
->name
, "immediate"))
7070 priv
= nft_expr_priv(expr
);
7071 if (priv
->dreg
!= NFT_REG_VERDICT
)
7075 switch (data
->verdict
.code
) {
7078 err
= nf_tables_check_loops(ctx
,
7079 data
->verdict
.chain
);
7088 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
7089 if (!nft_is_active_next(ctx
->net
, set
))
7091 if (!(set
->flags
& NFT_SET_MAP
) ||
7092 set
->dtype
!= NFT_DATA_VERDICT
)
7095 list_for_each_entry(binding
, &set
->bindings
, list
) {
7096 if (!(binding
->flags
& NFT_SET_MAP
) ||
7097 binding
->chain
!= chain
)
7100 iter
.genmask
= nft_genmask_next(ctx
->net
);
7104 iter
.fn
= nf_tables_loop_check_setelem
;
7106 set
->ops
->walk(ctx
, set
, &iter
);
7116 * nft_parse_u32_check - fetch u32 attribute and check for maximum value
7118 * @attr: netlink attribute to fetch value from
7119 * @max: maximum value to be stored in dest
7120 * @dest: pointer to the variable
7122 * Parse, check and store a given u32 netlink attribute into variable.
7123 * This function returns -ERANGE if the value goes over maximum value.
7124 * Otherwise a 0 is returned and the attribute value is stored in the
7125 * destination variable.
7127 int nft_parse_u32_check(const struct nlattr
*attr
, int max
, u32
*dest
)
7131 val
= ntohl(nla_get_be32(attr
));
7138 EXPORT_SYMBOL_GPL(nft_parse_u32_check
);
7141 * nft_parse_register - parse a register value from a netlink attribute
7143 * @attr: netlink attribute
7145 * Parse and translate a register value from a netlink attribute.
7146 * Registers used to be 128 bit wide, these register numbers will be
7147 * mapped to the corresponding 32 bit register numbers.
7149 unsigned int nft_parse_register(const struct nlattr
*attr
)
7153 reg
= ntohl(nla_get_be32(attr
));
7155 case NFT_REG_VERDICT
...NFT_REG_4
:
7156 return reg
* NFT_REG_SIZE
/ NFT_REG32_SIZE
;
7158 return reg
+ NFT_REG_SIZE
/ NFT_REG32_SIZE
- NFT_REG32_00
;
7161 EXPORT_SYMBOL_GPL(nft_parse_register
);
7164 * nft_dump_register - dump a register value to a netlink attribute
7166 * @skb: socket buffer
7167 * @attr: attribute number
7168 * @reg: register number
7170 * Construct a netlink attribute containing the register number. For
7171 * compatibility reasons, register numbers being a multiple of 4 are
7172 * translated to the corresponding 128 bit register numbers.
7174 int nft_dump_register(struct sk_buff
*skb
, unsigned int attr
, unsigned int reg
)
7176 if (reg
% (NFT_REG_SIZE
/ NFT_REG32_SIZE
) == 0)
7177 reg
= reg
/ (NFT_REG_SIZE
/ NFT_REG32_SIZE
);
7179 reg
= reg
- NFT_REG_SIZE
/ NFT_REG32_SIZE
+ NFT_REG32_00
;
7181 return nla_put_be32(skb
, attr
, htonl(reg
));
7183 EXPORT_SYMBOL_GPL(nft_dump_register
);
7186 * nft_validate_register_load - validate a load from a register
7188 * @reg: the register number
7189 * @len: the length of the data
7191 * Validate that the input register is one of the general purpose
7192 * registers and that the length of the load is within the bounds.
7194 int nft_validate_register_load(enum nft_registers reg
, unsigned int len
)
7196 if (reg
< NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
)
7200 if (reg
* NFT_REG32_SIZE
+ len
> FIELD_SIZEOF(struct nft_regs
, data
))
7205 EXPORT_SYMBOL_GPL(nft_validate_register_load
);
7208 * nft_validate_register_store - validate an expressions' register store
7210 * @ctx: context of the expression performing the load
7211 * @reg: the destination register number
7212 * @data: the data to load
7213 * @type: the data type
7214 * @len: the length of the data
7216 * Validate that a data load uses the appropriate data type for
7217 * the destination register and the length is within the bounds.
7218 * A value of NULL for the data means that its runtime gathered
7221 int nft_validate_register_store(const struct nft_ctx
*ctx
,
7222 enum nft_registers reg
,
7223 const struct nft_data
*data
,
7224 enum nft_data_types type
, unsigned int len
)
7229 case NFT_REG_VERDICT
:
7230 if (type
!= NFT_DATA_VERDICT
)
7234 (data
->verdict
.code
== NFT_GOTO
||
7235 data
->verdict
.code
== NFT_JUMP
)) {
7236 err
= nf_tables_check_loops(ctx
, data
->verdict
.chain
);
7243 if (reg
< NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
)
7247 if (reg
* NFT_REG32_SIZE
+ len
>
7248 FIELD_SIZEOF(struct nft_regs
, data
))
7251 if (data
!= NULL
&& type
!= NFT_DATA_VALUE
)
7256 EXPORT_SYMBOL_GPL(nft_validate_register_store
);
7258 static const struct nla_policy nft_verdict_policy
[NFTA_VERDICT_MAX
+ 1] = {
7259 [NFTA_VERDICT_CODE
] = { .type
= NLA_U32
},
7260 [NFTA_VERDICT_CHAIN
] = { .type
= NLA_STRING
,
7261 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
7264 static int nft_verdict_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
7265 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
7267 u8 genmask
= nft_genmask_next(ctx
->net
);
7268 struct nlattr
*tb
[NFTA_VERDICT_MAX
+ 1];
7269 struct nft_chain
*chain
;
7272 err
= nla_parse_nested_deprecated(tb
, NFTA_VERDICT_MAX
, nla
,
7273 nft_verdict_policy
, NULL
);
7277 if (!tb
[NFTA_VERDICT_CODE
])
7279 data
->verdict
.code
= ntohl(nla_get_be32(tb
[NFTA_VERDICT_CODE
]));
7281 switch (data
->verdict
.code
) {
7283 switch (data
->verdict
.code
& NF_VERDICT_MASK
) {
7298 if (!tb
[NFTA_VERDICT_CHAIN
])
7300 chain
= nft_chain_lookup(ctx
->net
, ctx
->table
,
7301 tb
[NFTA_VERDICT_CHAIN
], genmask
);
7303 return PTR_ERR(chain
);
7304 if (nft_is_base_chain(chain
))
7308 data
->verdict
.chain
= chain
;
7312 desc
->len
= sizeof(data
->verdict
);
7313 desc
->type
= NFT_DATA_VERDICT
;
7317 static void nft_verdict_uninit(const struct nft_data
*data
)
7319 switch (data
->verdict
.code
) {
7322 data
->verdict
.chain
->use
--;
7327 int nft_verdict_dump(struct sk_buff
*skb
, int type
, const struct nft_verdict
*v
)
7329 struct nlattr
*nest
;
7331 nest
= nla_nest_start_noflag(skb
, type
);
7333 goto nla_put_failure
;
7335 if (nla_put_be32(skb
, NFTA_VERDICT_CODE
, htonl(v
->code
)))
7336 goto nla_put_failure
;
7341 if (nla_put_string(skb
, NFTA_VERDICT_CHAIN
,
7343 goto nla_put_failure
;
7345 nla_nest_end(skb
, nest
);
7352 static int nft_value_init(const struct nft_ctx
*ctx
,
7353 struct nft_data
*data
, unsigned int size
,
7354 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
7364 nla_memcpy(data
->data
, nla
, len
);
7365 desc
->type
= NFT_DATA_VALUE
;
7370 static int nft_value_dump(struct sk_buff
*skb
, const struct nft_data
*data
,
7373 return nla_put(skb
, NFTA_DATA_VALUE
, len
, data
->data
);
7376 static const struct nla_policy nft_data_policy
[NFTA_DATA_MAX
+ 1] = {
7377 [NFTA_DATA_VALUE
] = { .type
= NLA_BINARY
},
7378 [NFTA_DATA_VERDICT
] = { .type
= NLA_NESTED
},
7382 * nft_data_init - parse nf_tables data netlink attributes
7384 * @ctx: context of the expression using the data
7385 * @data: destination struct nft_data
7386 * @size: maximum data length
7387 * @desc: data description
7388 * @nla: netlink attribute containing data
7390 * Parse the netlink data attributes and initialize a struct nft_data.
7391 * The type and length of data are returned in the data description.
7393 * The caller can indicate that it only wants to accept data of type
7394 * NFT_DATA_VALUE by passing NULL for the ctx argument.
7396 int nft_data_init(const struct nft_ctx
*ctx
,
7397 struct nft_data
*data
, unsigned int size
,
7398 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
7400 struct nlattr
*tb
[NFTA_DATA_MAX
+ 1];
7403 err
= nla_parse_nested_deprecated(tb
, NFTA_DATA_MAX
, nla
,
7404 nft_data_policy
, NULL
);
7408 if (tb
[NFTA_DATA_VALUE
])
7409 return nft_value_init(ctx
, data
, size
, desc
,
7410 tb
[NFTA_DATA_VALUE
]);
7411 if (tb
[NFTA_DATA_VERDICT
] && ctx
!= NULL
)
7412 return nft_verdict_init(ctx
, data
, desc
, tb
[NFTA_DATA_VERDICT
]);
7415 EXPORT_SYMBOL_GPL(nft_data_init
);
7418 * nft_data_release - release a nft_data item
7420 * @data: struct nft_data to release
7421 * @type: type of data
7423 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
7424 * all others need to be released by calling this function.
7426 void nft_data_release(const struct nft_data
*data
, enum nft_data_types type
)
7428 if (type
< NFT_DATA_VERDICT
)
7431 case NFT_DATA_VERDICT
:
7432 return nft_verdict_uninit(data
);
7437 EXPORT_SYMBOL_GPL(nft_data_release
);
7439 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
7440 enum nft_data_types type
, unsigned int len
)
7442 struct nlattr
*nest
;
7445 nest
= nla_nest_start_noflag(skb
, attr
);
7450 case NFT_DATA_VALUE
:
7451 err
= nft_value_dump(skb
, data
, len
);
7453 case NFT_DATA_VERDICT
:
7454 err
= nft_verdict_dump(skb
, NFTA_DATA_VERDICT
, &data
->verdict
);
7461 nla_nest_end(skb
, nest
);
7464 EXPORT_SYMBOL_GPL(nft_data_dump
);
7466 int __nft_release_basechain(struct nft_ctx
*ctx
)
7468 struct nft_rule
*rule
, *nr
;
7470 if (WARN_ON(!nft_is_base_chain(ctx
->chain
)))
7473 nf_tables_unregister_hook(ctx
->net
, ctx
->chain
->table
, ctx
->chain
);
7474 list_for_each_entry_safe(rule
, nr
, &ctx
->chain
->rules
, list
) {
7475 list_del(&rule
->list
);
7477 nf_tables_rule_release(ctx
, rule
);
7479 nft_chain_del(ctx
->chain
);
7481 nf_tables_chain_destroy(ctx
);
7485 EXPORT_SYMBOL_GPL(__nft_release_basechain
);
7487 static void __nft_release_tables(struct net
*net
)
7489 struct nft_flowtable
*flowtable
, *nf
;
7490 struct nft_table
*table
, *nt
;
7491 struct nft_chain
*chain
, *nc
;
7492 struct nft_object
*obj
, *ne
;
7493 struct nft_rule
*rule
, *nr
;
7494 struct nft_set
*set
, *ns
;
7495 struct nft_ctx ctx
= {
7497 .family
= NFPROTO_NETDEV
,
7500 list_for_each_entry_safe(table
, nt
, &net
->nft
.tables
, list
) {
7501 ctx
.family
= table
->family
;
7503 list_for_each_entry(chain
, &table
->chains
, list
)
7504 nf_tables_unregister_hook(net
, table
, chain
);
7505 /* No packets are walking on these chains anymore. */
7507 list_for_each_entry(chain
, &table
->chains
, list
) {
7509 list_for_each_entry_safe(rule
, nr
, &chain
->rules
, list
) {
7510 list_del(&rule
->list
);
7512 nf_tables_rule_release(&ctx
, rule
);
7515 list_for_each_entry_safe(flowtable
, nf
, &table
->flowtables
, list
) {
7516 list_del(&flowtable
->list
);
7518 nf_tables_flowtable_destroy(flowtable
);
7520 list_for_each_entry_safe(set
, ns
, &table
->sets
, list
) {
7521 list_del(&set
->list
);
7523 nft_set_destroy(set
);
7525 list_for_each_entry_safe(obj
, ne
, &table
->objects
, list
) {
7528 nft_obj_destroy(&ctx
, obj
);
7530 list_for_each_entry_safe(chain
, nc
, &table
->chains
, list
) {
7532 nft_chain_del(chain
);
7534 nf_tables_chain_destroy(&ctx
);
7536 list_del(&table
->list
);
7537 nf_tables_table_destroy(&ctx
);
7541 static int __net_init
nf_tables_init_net(struct net
*net
)
7543 INIT_LIST_HEAD(&net
->nft
.tables
);
7544 INIT_LIST_HEAD(&net
->nft
.commit_list
);
7545 mutex_init(&net
->nft
.commit_mutex
);
7546 net
->nft
.base_seq
= 1;
7547 net
->nft
.validate_state
= NFT_VALIDATE_SKIP
;
7552 static void __net_exit
nf_tables_exit_net(struct net
*net
)
7554 mutex_lock(&net
->nft
.commit_mutex
);
7555 if (!list_empty(&net
->nft
.commit_list
))
7556 __nf_tables_abort(net
);
7557 __nft_release_tables(net
);
7558 mutex_unlock(&net
->nft
.commit_mutex
);
7559 WARN_ON_ONCE(!list_empty(&net
->nft
.tables
));
7562 static struct pernet_operations nf_tables_net_ops
= {
7563 .init
= nf_tables_init_net
,
7564 .exit
= nf_tables_exit_net
,
7567 static int __init
nf_tables_module_init(void)
7571 spin_lock_init(&nf_tables_destroy_list_lock
);
7572 err
= register_pernet_subsys(&nf_tables_net_ops
);
7576 err
= nft_chain_filter_init();
7580 err
= nf_tables_core_module_init();
7584 err
= register_netdevice_notifier(&nf_tables_flowtable_notifier
);
7588 err
= rhltable_init(&nft_objname_ht
, &nft_objname_ht_params
);
7593 err
= nfnetlink_subsys_register(&nf_tables_subsys
);
7597 nft_chain_route_init();
7600 rhltable_destroy(&nft_objname_ht
);
7602 unregister_netdevice_notifier(&nf_tables_flowtable_notifier
);
7604 nf_tables_core_module_exit();
7606 nft_chain_filter_fini();
7608 unregister_pernet_subsys(&nf_tables_net_ops
);
7612 static void __exit
nf_tables_module_exit(void)
7614 nfnetlink_subsys_unregister(&nf_tables_subsys
);
7615 unregister_netdevice_notifier(&nf_tables_flowtable_notifier
);
7616 nft_chain_filter_fini();
7617 nft_chain_route_fini();
7618 unregister_pernet_subsys(&nf_tables_net_ops
);
7619 cancel_work_sync(&trans_destroy_work
);
7621 rhltable_destroy(&nft_objname_ht
);
7622 nf_tables_core_module_exit();
7625 module_init(nf_tables_module_init
);
7626 module_exit(nf_tables_module_exit
);
7628 MODULE_LICENSE("GPL");
7629 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
7630 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES
);