1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/list.h>
11 #include <linux/skbuff.h>
12 #include <linux/netlink.h>
13 #include <linux/vmalloc.h>
14 #include <linux/rhashtable.h>
15 #include <linux/audit.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nfnetlink.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_flow_table.h>
20 #include <net/netfilter/nf_tables_core.h>
21 #include <net/netfilter/nf_tables.h>
22 #include <net/netfilter/nf_tables_offload.h>
23 #include <net/net_namespace.h>
26 #define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-"))
28 static LIST_HEAD(nf_tables_expressions
);
29 static LIST_HEAD(nf_tables_objects
);
30 static LIST_HEAD(nf_tables_flowtables
);
31 static LIST_HEAD(nf_tables_destroy_list
);
32 static DEFINE_SPINLOCK(nf_tables_destroy_list_lock
);
33 static u64 table_handle
;
36 NFT_VALIDATE_SKIP
= 0,
41 static struct rhltable nft_objname_ht
;
43 static u32
nft_chain_hash(const void *data
, u32 len
, u32 seed
);
44 static u32
nft_chain_hash_obj(const void *data
, u32 len
, u32 seed
);
45 static int nft_chain_hash_cmp(struct rhashtable_compare_arg
*, const void *);
47 static u32
nft_objname_hash(const void *data
, u32 len
, u32 seed
);
48 static u32
nft_objname_hash_obj(const void *data
, u32 len
, u32 seed
);
49 static int nft_objname_hash_cmp(struct rhashtable_compare_arg
*, const void *);
51 static const struct rhashtable_params nft_chain_ht_params
= {
52 .head_offset
= offsetof(struct nft_chain
, rhlhead
),
53 .key_offset
= offsetof(struct nft_chain
, name
),
54 .hashfn
= nft_chain_hash
,
55 .obj_hashfn
= nft_chain_hash_obj
,
56 .obj_cmpfn
= nft_chain_hash_cmp
,
57 .automatic_shrinking
= true,
60 static const struct rhashtable_params nft_objname_ht_params
= {
61 .head_offset
= offsetof(struct nft_object
, rhlhead
),
62 .key_offset
= offsetof(struct nft_object
, key
),
63 .hashfn
= nft_objname_hash
,
64 .obj_hashfn
= nft_objname_hash_obj
,
65 .obj_cmpfn
= nft_objname_hash_cmp
,
66 .automatic_shrinking
= true,
69 static void nft_validate_state_update(struct net
*net
, u8 new_validate_state
)
71 switch (net
->nft
.validate_state
) {
72 case NFT_VALIDATE_SKIP
:
73 WARN_ON_ONCE(new_validate_state
== NFT_VALIDATE_DO
);
75 case NFT_VALIDATE_NEED
:
78 if (new_validate_state
== NFT_VALIDATE_NEED
)
82 net
->nft
.validate_state
= new_validate_state
;
84 static void nf_tables_trans_destroy_work(struct work_struct
*w
);
85 static DECLARE_WORK(trans_destroy_work
, nf_tables_trans_destroy_work
);
87 static void nft_ctx_init(struct nft_ctx
*ctx
,
89 const struct sk_buff
*skb
,
90 const struct nlmsghdr
*nlh
,
92 struct nft_table
*table
,
93 struct nft_chain
*chain
,
94 const struct nlattr
* const *nla
)
102 ctx
->portid
= NETLINK_CB(skb
).portid
;
103 ctx
->report
= nlmsg_report(nlh
);
104 ctx
->flags
= nlh
->nlmsg_flags
;
105 ctx
->seq
= nlh
->nlmsg_seq
;
108 static struct nft_trans
*nft_trans_alloc_gfp(const struct nft_ctx
*ctx
,
109 int msg_type
, u32 size
, gfp_t gfp
)
111 struct nft_trans
*trans
;
113 trans
= kzalloc(sizeof(struct nft_trans
) + size
, gfp
);
117 trans
->msg_type
= msg_type
;
123 static struct nft_trans
*nft_trans_alloc(const struct nft_ctx
*ctx
,
124 int msg_type
, u32 size
)
126 return nft_trans_alloc_gfp(ctx
, msg_type
, size
, GFP_KERNEL
);
129 static void nft_trans_destroy(struct nft_trans
*trans
)
131 list_del(&trans
->list
);
135 static void nft_set_trans_bind(const struct nft_ctx
*ctx
, struct nft_set
*set
)
137 struct net
*net
= ctx
->net
;
138 struct nft_trans
*trans
;
140 if (!nft_set_is_anonymous(set
))
143 list_for_each_entry_reverse(trans
, &net
->nft
.commit_list
, list
) {
144 switch (trans
->msg_type
) {
146 if (nft_trans_set(trans
) == set
)
147 nft_trans_set_bound(trans
) = true;
149 case NFT_MSG_NEWSETELEM
:
150 if (nft_trans_elem_set(trans
) == set
)
151 nft_trans_elem_set_bound(trans
) = true;
157 static int nft_netdev_register_hooks(struct net
*net
,
158 struct list_head
*hook_list
)
160 struct nft_hook
*hook
;
164 list_for_each_entry(hook
, hook_list
, list
) {
165 err
= nf_register_net_hook(net
, &hook
->ops
);
174 list_for_each_entry(hook
, hook_list
, list
) {
178 nf_unregister_net_hook(net
, &hook
->ops
);
183 static void nft_netdev_unregister_hooks(struct net
*net
,
184 struct list_head
*hook_list
)
186 struct nft_hook
*hook
;
188 list_for_each_entry(hook
, hook_list
, list
)
189 nf_unregister_net_hook(net
, &hook
->ops
);
192 static int nf_tables_register_hook(struct net
*net
,
193 const struct nft_table
*table
,
194 struct nft_chain
*chain
)
196 struct nft_base_chain
*basechain
;
197 const struct nf_hook_ops
*ops
;
199 if (table
->flags
& NFT_TABLE_F_DORMANT
||
200 !nft_is_base_chain(chain
))
203 basechain
= nft_base_chain(chain
);
204 ops
= &basechain
->ops
;
206 if (basechain
->type
->ops_register
)
207 return basechain
->type
->ops_register(net
, ops
);
209 if (nft_base_chain_netdev(table
->family
, basechain
->ops
.hooknum
))
210 return nft_netdev_register_hooks(net
, &basechain
->hook_list
);
212 return nf_register_net_hook(net
, &basechain
->ops
);
215 static void nf_tables_unregister_hook(struct net
*net
,
216 const struct nft_table
*table
,
217 struct nft_chain
*chain
)
219 struct nft_base_chain
*basechain
;
220 const struct nf_hook_ops
*ops
;
222 if (table
->flags
& NFT_TABLE_F_DORMANT
||
223 !nft_is_base_chain(chain
))
225 basechain
= nft_base_chain(chain
);
226 ops
= &basechain
->ops
;
228 if (basechain
->type
->ops_unregister
)
229 return basechain
->type
->ops_unregister(net
, ops
);
231 if (nft_base_chain_netdev(table
->family
, basechain
->ops
.hooknum
))
232 nft_netdev_unregister_hooks(net
, &basechain
->hook_list
);
234 nf_unregister_net_hook(net
, &basechain
->ops
);
237 static int nft_trans_table_add(struct nft_ctx
*ctx
, int msg_type
)
239 struct nft_trans
*trans
;
241 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_table
));
245 if (msg_type
== NFT_MSG_NEWTABLE
)
246 nft_activate_next(ctx
->net
, ctx
->table
);
248 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
252 static int nft_deltable(struct nft_ctx
*ctx
)
256 err
= nft_trans_table_add(ctx
, NFT_MSG_DELTABLE
);
260 nft_deactivate_next(ctx
->net
, ctx
->table
);
264 static struct nft_trans
*nft_trans_chain_add(struct nft_ctx
*ctx
, int msg_type
)
266 struct nft_trans
*trans
;
268 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_chain
));
270 return ERR_PTR(-ENOMEM
);
272 if (msg_type
== NFT_MSG_NEWCHAIN
) {
273 nft_activate_next(ctx
->net
, ctx
->chain
);
275 if (ctx
->nla
[NFTA_CHAIN_ID
]) {
276 nft_trans_chain_id(trans
) =
277 ntohl(nla_get_be32(ctx
->nla
[NFTA_CHAIN_ID
]));
281 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
285 static int nft_delchain(struct nft_ctx
*ctx
)
287 struct nft_trans
*trans
;
289 trans
= nft_trans_chain_add(ctx
, NFT_MSG_DELCHAIN
);
291 return PTR_ERR(trans
);
294 nft_deactivate_next(ctx
->net
, ctx
->chain
);
299 static void nft_rule_expr_activate(const struct nft_ctx
*ctx
,
300 struct nft_rule
*rule
)
302 struct nft_expr
*expr
;
304 expr
= nft_expr_first(rule
);
305 while (nft_expr_more(rule
, expr
)) {
306 if (expr
->ops
->activate
)
307 expr
->ops
->activate(ctx
, expr
);
309 expr
= nft_expr_next(expr
);
313 static void nft_rule_expr_deactivate(const struct nft_ctx
*ctx
,
314 struct nft_rule
*rule
,
315 enum nft_trans_phase phase
)
317 struct nft_expr
*expr
;
319 expr
= nft_expr_first(rule
);
320 while (nft_expr_more(rule
, expr
)) {
321 if (expr
->ops
->deactivate
)
322 expr
->ops
->deactivate(ctx
, expr
, phase
);
324 expr
= nft_expr_next(expr
);
329 nf_tables_delrule_deactivate(struct nft_ctx
*ctx
, struct nft_rule
*rule
)
331 /* You cannot delete the same rule twice */
332 if (nft_is_active_next(ctx
->net
, rule
)) {
333 nft_deactivate_next(ctx
->net
, rule
);
340 static struct nft_trans
*nft_trans_rule_add(struct nft_ctx
*ctx
, int msg_type
,
341 struct nft_rule
*rule
)
343 struct nft_trans
*trans
;
345 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_rule
));
349 if (msg_type
== NFT_MSG_NEWRULE
&& ctx
->nla
[NFTA_RULE_ID
] != NULL
) {
350 nft_trans_rule_id(trans
) =
351 ntohl(nla_get_be32(ctx
->nla
[NFTA_RULE_ID
]));
353 nft_trans_rule(trans
) = rule
;
354 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
359 static int nft_delrule(struct nft_ctx
*ctx
, struct nft_rule
*rule
)
361 struct nft_flow_rule
*flow
;
362 struct nft_trans
*trans
;
365 trans
= nft_trans_rule_add(ctx
, NFT_MSG_DELRULE
, rule
);
369 if (ctx
->chain
->flags
& NFT_CHAIN_HW_OFFLOAD
) {
370 flow
= nft_flow_rule_create(ctx
->net
, rule
);
372 nft_trans_destroy(trans
);
373 return PTR_ERR(flow
);
376 nft_trans_flow_rule(trans
) = flow
;
379 err
= nf_tables_delrule_deactivate(ctx
, rule
);
381 nft_trans_destroy(trans
);
384 nft_rule_expr_deactivate(ctx
, rule
, NFT_TRANS_PREPARE
);
389 static int nft_delrule_by_chain(struct nft_ctx
*ctx
)
391 struct nft_rule
*rule
;
394 list_for_each_entry(rule
, &ctx
->chain
->rules
, list
) {
395 if (!nft_is_active_next(ctx
->net
, rule
))
398 err
= nft_delrule(ctx
, rule
);
405 static int nft_trans_set_add(const struct nft_ctx
*ctx
, int msg_type
,
408 struct nft_trans
*trans
;
410 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_set
));
414 if (msg_type
== NFT_MSG_NEWSET
&& ctx
->nla
[NFTA_SET_ID
] != NULL
) {
415 nft_trans_set_id(trans
) =
416 ntohl(nla_get_be32(ctx
->nla
[NFTA_SET_ID
]));
417 nft_activate_next(ctx
->net
, set
);
419 nft_trans_set(trans
) = set
;
420 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
425 static int nft_delset(const struct nft_ctx
*ctx
, struct nft_set
*set
)
429 err
= nft_trans_set_add(ctx
, NFT_MSG_DELSET
, set
);
433 nft_deactivate_next(ctx
->net
, set
);
439 static int nft_trans_obj_add(struct nft_ctx
*ctx
, int msg_type
,
440 struct nft_object
*obj
)
442 struct nft_trans
*trans
;
444 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_obj
));
448 if (msg_type
== NFT_MSG_NEWOBJ
)
449 nft_activate_next(ctx
->net
, obj
);
451 nft_trans_obj(trans
) = obj
;
452 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
457 static int nft_delobj(struct nft_ctx
*ctx
, struct nft_object
*obj
)
461 err
= nft_trans_obj_add(ctx
, NFT_MSG_DELOBJ
, obj
);
465 nft_deactivate_next(ctx
->net
, obj
);
471 static int nft_trans_flowtable_add(struct nft_ctx
*ctx
, int msg_type
,
472 struct nft_flowtable
*flowtable
)
474 struct nft_trans
*trans
;
476 trans
= nft_trans_alloc(ctx
, msg_type
,
477 sizeof(struct nft_trans_flowtable
));
481 if (msg_type
== NFT_MSG_NEWFLOWTABLE
)
482 nft_activate_next(ctx
->net
, flowtable
);
484 nft_trans_flowtable(trans
) = flowtable
;
485 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
490 static int nft_delflowtable(struct nft_ctx
*ctx
,
491 struct nft_flowtable
*flowtable
)
495 err
= nft_trans_flowtable_add(ctx
, NFT_MSG_DELFLOWTABLE
, flowtable
);
499 nft_deactivate_next(ctx
->net
, flowtable
);
509 static struct nft_table
*nft_table_lookup(const struct net
*net
,
510 const struct nlattr
*nla
,
511 u8 family
, u8 genmask
)
513 struct nft_table
*table
;
516 return ERR_PTR(-EINVAL
);
518 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
,
519 lockdep_is_held(&net
->nft
.commit_mutex
)) {
520 if (!nla_strcmp(nla
, table
->name
) &&
521 table
->family
== family
&&
522 nft_active_genmask(table
, genmask
))
526 return ERR_PTR(-ENOENT
);
529 static struct nft_table
*nft_table_lookup_byhandle(const struct net
*net
,
530 const struct nlattr
*nla
,
533 struct nft_table
*table
;
535 list_for_each_entry(table
, &net
->nft
.tables
, list
) {
536 if (be64_to_cpu(nla_get_be64(nla
)) == table
->handle
&&
537 nft_active_genmask(table
, genmask
))
541 return ERR_PTR(-ENOENT
);
544 static inline u64
nf_tables_alloc_handle(struct nft_table
*table
)
546 return ++table
->hgenerator
;
549 static const struct nft_chain_type
*chain_type
[NFPROTO_NUMPROTO
][NFT_CHAIN_T_MAX
];
551 static const struct nft_chain_type
*
552 __nft_chain_type_get(u8 family
, enum nft_chain_types type
)
554 if (family
>= NFPROTO_NUMPROTO
||
555 type
>= NFT_CHAIN_T_MAX
)
558 return chain_type
[family
][type
];
561 static const struct nft_chain_type
*
562 __nf_tables_chain_type_lookup(const struct nlattr
*nla
, u8 family
)
564 const struct nft_chain_type
*type
;
567 for (i
= 0; i
< NFT_CHAIN_T_MAX
; i
++) {
568 type
= __nft_chain_type_get(family
, i
);
571 if (!nla_strcmp(nla
, type
->name
))
577 struct nft_module_request
{
578 struct list_head list
;
579 char module
[MODULE_NAME_LEN
];
583 #ifdef CONFIG_MODULES
584 static int nft_request_module(struct net
*net
, const char *fmt
, ...)
586 char module_name
[MODULE_NAME_LEN
];
587 struct nft_module_request
*req
;
592 ret
= vsnprintf(module_name
, MODULE_NAME_LEN
, fmt
, args
);
594 if (ret
>= MODULE_NAME_LEN
)
597 list_for_each_entry(req
, &net
->nft
.module_list
, list
) {
598 if (!strcmp(req
->module
, module_name
)) {
602 /* A request to load this module already exists. */
607 req
= kmalloc(sizeof(*req
), GFP_KERNEL
);
612 strlcpy(req
->module
, module_name
, MODULE_NAME_LEN
);
613 list_add_tail(&req
->list
, &net
->nft
.module_list
);
619 static void lockdep_nfnl_nft_mutex_not_held(void)
621 #ifdef CONFIG_PROVE_LOCKING
623 WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES
));
627 static const struct nft_chain_type
*
628 nf_tables_chain_type_lookup(struct net
*net
, const struct nlattr
*nla
,
629 u8 family
, bool autoload
)
631 const struct nft_chain_type
*type
;
633 type
= __nf_tables_chain_type_lookup(nla
, family
);
637 lockdep_nfnl_nft_mutex_not_held();
638 #ifdef CONFIG_MODULES
640 if (nft_request_module(net
, "nft-chain-%u-%.*s", family
,
642 (const char *)nla_data(nla
)) == -EAGAIN
)
643 return ERR_PTR(-EAGAIN
);
646 return ERR_PTR(-ENOENT
);
649 static const struct nla_policy nft_table_policy
[NFTA_TABLE_MAX
+ 1] = {
650 [NFTA_TABLE_NAME
] = { .type
= NLA_STRING
,
651 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
652 [NFTA_TABLE_FLAGS
] = { .type
= NLA_U32
},
653 [NFTA_TABLE_HANDLE
] = { .type
= NLA_U64
},
654 [NFTA_TABLE_USERDATA
] = { .type
= NLA_BINARY
,
655 .len
= NFT_USERDATA_MAXLEN
}
658 static int nf_tables_fill_table_info(struct sk_buff
*skb
, struct net
*net
,
659 u32 portid
, u32 seq
, int event
, u32 flags
,
660 int family
, const struct nft_table
*table
)
662 struct nlmsghdr
*nlh
;
663 struct nfgenmsg
*nfmsg
;
665 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
666 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
668 goto nla_put_failure
;
670 nfmsg
= nlmsg_data(nlh
);
671 nfmsg
->nfgen_family
= family
;
672 nfmsg
->version
= NFNETLINK_V0
;
673 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
675 if (nla_put_string(skb
, NFTA_TABLE_NAME
, table
->name
) ||
676 nla_put_be32(skb
, NFTA_TABLE_FLAGS
, htonl(table
->flags
)) ||
677 nla_put_be32(skb
, NFTA_TABLE_USE
, htonl(table
->use
)) ||
678 nla_put_be64(skb
, NFTA_TABLE_HANDLE
, cpu_to_be64(table
->handle
),
680 goto nla_put_failure
;
683 if (nla_put(skb
, NFTA_TABLE_USERDATA
, table
->udlen
, table
->udata
))
684 goto nla_put_failure
;
691 nlmsg_trim(skb
, nlh
);
695 struct nftnl_skb_parms
{
698 #define NFT_CB(skb) (*(struct nftnl_skb_parms*)&((skb)->cb))
700 static void nft_notify_enqueue(struct sk_buff
*skb
, bool report
,
701 struct list_head
*notify_list
)
703 NFT_CB(skb
).report
= report
;
704 list_add_tail(&skb
->list
, notify_list
);
707 static void nf_tables_table_notify(const struct nft_ctx
*ctx
, int event
)
711 char *buf
= kasprintf(GFP_KERNEL
, "%s:%llu;?:0",
712 ctx
->table
->name
, ctx
->table
->handle
);
717 event
== NFT_MSG_NEWTABLE
?
718 AUDIT_NFT_OP_TABLE_REGISTER
:
719 AUDIT_NFT_OP_TABLE_UNREGISTER
,
724 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
727 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
731 err
= nf_tables_fill_table_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
732 event
, 0, ctx
->family
, ctx
->table
);
738 nft_notify_enqueue(skb
, ctx
->report
, &ctx
->net
->nft
.notify_list
);
741 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
744 static int nf_tables_dump_tables(struct sk_buff
*skb
,
745 struct netlink_callback
*cb
)
747 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
748 const struct nft_table
*table
;
749 unsigned int idx
= 0, s_idx
= cb
->args
[0];
750 struct net
*net
= sock_net(skb
->sk
);
751 int family
= nfmsg
->nfgen_family
;
754 cb
->seq
= net
->nft
.base_seq
;
756 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
757 if (family
!= NFPROTO_UNSPEC
&& family
!= table
->family
)
763 memset(&cb
->args
[1], 0,
764 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
765 if (!nft_is_active(net
, table
))
767 if (nf_tables_fill_table_info(skb
, net
,
768 NETLINK_CB(cb
->skb
).portid
,
770 NFT_MSG_NEWTABLE
, NLM_F_MULTI
,
771 table
->family
, table
) < 0)
774 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
784 static int nft_netlink_dump_start_rcu(struct sock
*nlsk
, struct sk_buff
*skb
,
785 const struct nlmsghdr
*nlh
,
786 struct netlink_dump_control
*c
)
790 if (!try_module_get(THIS_MODULE
))
794 err
= netlink_dump_start(nlsk
, skb
, nlh
, c
);
796 module_put(THIS_MODULE
);
801 /* called with rcu_read_lock held */
802 static int nf_tables_gettable(struct net
*net
, struct sock
*nlsk
,
803 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
804 const struct nlattr
* const nla
[],
805 struct netlink_ext_ack
*extack
)
807 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
808 u8 genmask
= nft_genmask_cur(net
);
809 const struct nft_table
*table
;
810 struct sk_buff
*skb2
;
811 int family
= nfmsg
->nfgen_family
;
814 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
815 struct netlink_dump_control c
= {
816 .dump
= nf_tables_dump_tables
,
817 .module
= THIS_MODULE
,
820 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
823 table
= nft_table_lookup(net
, nla
[NFTA_TABLE_NAME
], family
, genmask
);
825 NL_SET_BAD_ATTR(extack
, nla
[NFTA_TABLE_NAME
]);
826 return PTR_ERR(table
);
829 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
833 err
= nf_tables_fill_table_info(skb2
, net
, NETLINK_CB(skb
).portid
,
834 nlh
->nlmsg_seq
, NFT_MSG_NEWTABLE
, 0,
837 goto err_fill_table_info
;
839 return nfnetlink_unicast(skb2
, net
, NETLINK_CB(skb
).portid
);
846 static void nft_table_disable(struct net
*net
, struct nft_table
*table
, u32 cnt
)
848 struct nft_chain
*chain
;
851 list_for_each_entry(chain
, &table
->chains
, list
) {
852 if (!nft_is_active_next(net
, chain
))
854 if (!nft_is_base_chain(chain
))
857 if (cnt
&& i
++ == cnt
)
860 nf_tables_unregister_hook(net
, table
, chain
);
864 static int nf_tables_table_enable(struct net
*net
, struct nft_table
*table
)
866 struct nft_chain
*chain
;
869 list_for_each_entry(chain
, &table
->chains
, list
) {
870 if (!nft_is_active_next(net
, chain
))
872 if (!nft_is_base_chain(chain
))
875 err
= nf_tables_register_hook(net
, table
, chain
);
877 goto err_register_hooks
;
885 nft_table_disable(net
, table
, i
);
889 static void nf_tables_table_disable(struct net
*net
, struct nft_table
*table
)
891 nft_table_disable(net
, table
, 0);
894 static int nf_tables_updtable(struct nft_ctx
*ctx
)
896 struct nft_trans
*trans
;
900 if (!ctx
->nla
[NFTA_TABLE_FLAGS
])
903 flags
= ntohl(nla_get_be32(ctx
->nla
[NFTA_TABLE_FLAGS
]));
904 if (flags
& ~NFT_TABLE_F_DORMANT
)
907 if (flags
== ctx
->table
->flags
)
910 trans
= nft_trans_alloc(ctx
, NFT_MSG_NEWTABLE
,
911 sizeof(struct nft_trans_table
));
915 if ((flags
& NFT_TABLE_F_DORMANT
) &&
916 !(ctx
->table
->flags
& NFT_TABLE_F_DORMANT
)) {
917 nft_trans_table_enable(trans
) = false;
918 } else if (!(flags
& NFT_TABLE_F_DORMANT
) &&
919 ctx
->table
->flags
& NFT_TABLE_F_DORMANT
) {
920 ctx
->table
->flags
&= ~NFT_TABLE_F_DORMANT
;
921 ret
= nf_tables_table_enable(ctx
->net
, ctx
->table
);
923 nft_trans_table_enable(trans
) = true;
925 ctx
->table
->flags
|= NFT_TABLE_F_DORMANT
;
930 nft_trans_table_update(trans
) = true;
931 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
934 nft_trans_destroy(trans
);
938 static u32
nft_chain_hash(const void *data
, u32 len
, u32 seed
)
940 const char *name
= data
;
942 return jhash(name
, strlen(name
), seed
);
945 static u32
nft_chain_hash_obj(const void *data
, u32 len
, u32 seed
)
947 const struct nft_chain
*chain
= data
;
949 return nft_chain_hash(chain
->name
, 0, seed
);
952 static int nft_chain_hash_cmp(struct rhashtable_compare_arg
*arg
,
955 const struct nft_chain
*chain
= ptr
;
956 const char *name
= arg
->key
;
958 return strcmp(chain
->name
, name
);
961 static u32
nft_objname_hash(const void *data
, u32 len
, u32 seed
)
963 const struct nft_object_hash_key
*k
= data
;
965 seed
^= hash_ptr(k
->table
, 32);
967 return jhash(k
->name
, strlen(k
->name
), seed
);
970 static u32
nft_objname_hash_obj(const void *data
, u32 len
, u32 seed
)
972 const struct nft_object
*obj
= data
;
974 return nft_objname_hash(&obj
->key
, 0, seed
);
977 static int nft_objname_hash_cmp(struct rhashtable_compare_arg
*arg
,
980 const struct nft_object_hash_key
*k
= arg
->key
;
981 const struct nft_object
*obj
= ptr
;
983 if (obj
->key
.table
!= k
->table
)
986 return strcmp(obj
->key
.name
, k
->name
);
989 static int nf_tables_newtable(struct net
*net
, struct sock
*nlsk
,
990 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
991 const struct nlattr
* const nla
[],
992 struct netlink_ext_ack
*extack
)
994 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
995 u8 genmask
= nft_genmask_next(net
);
996 int family
= nfmsg
->nfgen_family
;
997 const struct nlattr
*attr
;
998 struct nft_table
*table
;
1003 lockdep_assert_held(&net
->nft
.commit_mutex
);
1004 attr
= nla
[NFTA_TABLE_NAME
];
1005 table
= nft_table_lookup(net
, attr
, family
, genmask
);
1006 if (IS_ERR(table
)) {
1007 if (PTR_ERR(table
) != -ENOENT
)
1008 return PTR_ERR(table
);
1010 if (nlh
->nlmsg_flags
& NLM_F_EXCL
) {
1011 NL_SET_BAD_ATTR(extack
, attr
);
1014 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1017 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
1018 return nf_tables_updtable(&ctx
);
1021 if (nla
[NFTA_TABLE_FLAGS
]) {
1022 flags
= ntohl(nla_get_be32(nla
[NFTA_TABLE_FLAGS
]));
1023 if (flags
& ~NFT_TABLE_F_DORMANT
)
1028 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
1032 table
->name
= nla_strdup(attr
, GFP_KERNEL
);
1033 if (table
->name
== NULL
)
1036 if (nla
[NFTA_TABLE_USERDATA
]) {
1037 table
->udata
= nla_memdup(nla
[NFTA_TABLE_USERDATA
], GFP_KERNEL
);
1038 if (table
->udata
== NULL
)
1039 goto err_table_udata
;
1041 table
->udlen
= nla_len(nla
[NFTA_TABLE_USERDATA
]);
1044 err
= rhltable_init(&table
->chains_ht
, &nft_chain_ht_params
);
1048 INIT_LIST_HEAD(&table
->chains
);
1049 INIT_LIST_HEAD(&table
->sets
);
1050 INIT_LIST_HEAD(&table
->objects
);
1051 INIT_LIST_HEAD(&table
->flowtables
);
1052 table
->family
= family
;
1053 table
->flags
= flags
;
1054 table
->handle
= ++table_handle
;
1056 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
1057 err
= nft_trans_table_add(&ctx
, NFT_MSG_NEWTABLE
);
1061 list_add_tail_rcu(&table
->list
, &net
->nft
.tables
);
1064 rhltable_destroy(&table
->chains_ht
);
1066 kfree(table
->udata
);
1075 static int nft_flush_table(struct nft_ctx
*ctx
)
1077 struct nft_flowtable
*flowtable
, *nft
;
1078 struct nft_chain
*chain
, *nc
;
1079 struct nft_object
*obj
, *ne
;
1080 struct nft_set
*set
, *ns
;
1083 list_for_each_entry(chain
, &ctx
->table
->chains
, list
) {
1084 if (!nft_is_active_next(ctx
->net
, chain
))
1087 if (nft_chain_is_bound(chain
))
1092 err
= nft_delrule_by_chain(ctx
);
1097 list_for_each_entry_safe(set
, ns
, &ctx
->table
->sets
, list
) {
1098 if (!nft_is_active_next(ctx
->net
, set
))
1101 if (nft_set_is_anonymous(set
) &&
1102 !list_empty(&set
->bindings
))
1105 err
= nft_delset(ctx
, set
);
1110 list_for_each_entry_safe(flowtable
, nft
, &ctx
->table
->flowtables
, list
) {
1111 if (!nft_is_active_next(ctx
->net
, flowtable
))
1114 err
= nft_delflowtable(ctx
, flowtable
);
1119 list_for_each_entry_safe(obj
, ne
, &ctx
->table
->objects
, list
) {
1120 if (!nft_is_active_next(ctx
->net
, obj
))
1123 err
= nft_delobj(ctx
, obj
);
1128 list_for_each_entry_safe(chain
, nc
, &ctx
->table
->chains
, list
) {
1129 if (!nft_is_active_next(ctx
->net
, chain
))
1132 if (nft_chain_is_bound(chain
))
1137 err
= nft_delchain(ctx
);
1142 err
= nft_deltable(ctx
);
1147 static int nft_flush(struct nft_ctx
*ctx
, int family
)
1149 struct nft_table
*table
, *nt
;
1150 const struct nlattr
* const *nla
= ctx
->nla
;
1153 list_for_each_entry_safe(table
, nt
, &ctx
->net
->nft
.tables
, list
) {
1154 if (family
!= AF_UNSPEC
&& table
->family
!= family
)
1157 ctx
->family
= table
->family
;
1159 if (!nft_is_active_next(ctx
->net
, table
))
1162 if (nla
[NFTA_TABLE_NAME
] &&
1163 nla_strcmp(nla
[NFTA_TABLE_NAME
], table
->name
) != 0)
1168 err
= nft_flush_table(ctx
);
1176 static int nf_tables_deltable(struct net
*net
, struct sock
*nlsk
,
1177 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1178 const struct nlattr
* const nla
[],
1179 struct netlink_ext_ack
*extack
)
1181 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1182 u8 genmask
= nft_genmask_next(net
);
1183 int family
= nfmsg
->nfgen_family
;
1184 const struct nlattr
*attr
;
1185 struct nft_table
*table
;
1188 nft_ctx_init(&ctx
, net
, skb
, nlh
, 0, NULL
, NULL
, nla
);
1189 if (family
== AF_UNSPEC
||
1190 (!nla
[NFTA_TABLE_NAME
] && !nla
[NFTA_TABLE_HANDLE
]))
1191 return nft_flush(&ctx
, family
);
1193 if (nla
[NFTA_TABLE_HANDLE
]) {
1194 attr
= nla
[NFTA_TABLE_HANDLE
];
1195 table
= nft_table_lookup_byhandle(net
, attr
, genmask
);
1197 attr
= nla
[NFTA_TABLE_NAME
];
1198 table
= nft_table_lookup(net
, attr
, family
, genmask
);
1201 if (IS_ERR(table
)) {
1202 NL_SET_BAD_ATTR(extack
, attr
);
1203 return PTR_ERR(table
);
1206 if (nlh
->nlmsg_flags
& NLM_F_NONREC
&&
1210 ctx
.family
= family
;
1213 return nft_flush_table(&ctx
);
1216 static void nf_tables_table_destroy(struct nft_ctx
*ctx
)
1218 if (WARN_ON(ctx
->table
->use
> 0))
1221 rhltable_destroy(&ctx
->table
->chains_ht
);
1222 kfree(ctx
->table
->name
);
1223 kfree(ctx
->table
->udata
);
1227 void nft_register_chain_type(const struct nft_chain_type
*ctype
)
1229 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1230 if (WARN_ON(__nft_chain_type_get(ctype
->family
, ctype
->type
))) {
1231 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1234 chain_type
[ctype
->family
][ctype
->type
] = ctype
;
1235 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1237 EXPORT_SYMBOL_GPL(nft_register_chain_type
);
1239 void nft_unregister_chain_type(const struct nft_chain_type
*ctype
)
1241 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1242 chain_type
[ctype
->family
][ctype
->type
] = NULL
;
1243 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1245 EXPORT_SYMBOL_GPL(nft_unregister_chain_type
);
1251 static struct nft_chain
*
1252 nft_chain_lookup_byhandle(const struct nft_table
*table
, u64 handle
, u8 genmask
)
1254 struct nft_chain
*chain
;
1256 list_for_each_entry(chain
, &table
->chains
, list
) {
1257 if (chain
->handle
== handle
&&
1258 nft_active_genmask(chain
, genmask
))
1262 return ERR_PTR(-ENOENT
);
1265 static bool lockdep_commit_lock_is_held(const struct net
*net
)
1267 #ifdef CONFIG_PROVE_LOCKING
1268 return lockdep_is_held(&net
->nft
.commit_mutex
);
1274 static struct nft_chain
*nft_chain_lookup(struct net
*net
,
1275 struct nft_table
*table
,
1276 const struct nlattr
*nla
, u8 genmask
)
1278 char search
[NFT_CHAIN_MAXNAMELEN
+ 1];
1279 struct rhlist_head
*tmp
, *list
;
1280 struct nft_chain
*chain
;
1283 return ERR_PTR(-EINVAL
);
1285 nla_strlcpy(search
, nla
, sizeof(search
));
1287 WARN_ON(!rcu_read_lock_held() &&
1288 !lockdep_commit_lock_is_held(net
));
1290 chain
= ERR_PTR(-ENOENT
);
1292 list
= rhltable_lookup(&table
->chains_ht
, search
, nft_chain_ht_params
);
1296 rhl_for_each_entry_rcu(chain
, tmp
, list
, rhlhead
) {
1297 if (nft_active_genmask(chain
, genmask
))
1300 chain
= ERR_PTR(-ENOENT
);
1306 static const struct nla_policy nft_chain_policy
[NFTA_CHAIN_MAX
+ 1] = {
1307 [NFTA_CHAIN_TABLE
] = { .type
= NLA_STRING
,
1308 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
1309 [NFTA_CHAIN_HANDLE
] = { .type
= NLA_U64
},
1310 [NFTA_CHAIN_NAME
] = { .type
= NLA_STRING
,
1311 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
1312 [NFTA_CHAIN_HOOK
] = { .type
= NLA_NESTED
},
1313 [NFTA_CHAIN_POLICY
] = { .type
= NLA_U32
},
1314 [NFTA_CHAIN_TYPE
] = { .type
= NLA_STRING
,
1315 .len
= NFT_MODULE_AUTOLOAD_LIMIT
},
1316 [NFTA_CHAIN_COUNTERS
] = { .type
= NLA_NESTED
},
1317 [NFTA_CHAIN_FLAGS
] = { .type
= NLA_U32
},
1318 [NFTA_CHAIN_ID
] = { .type
= NLA_U32
},
1319 [NFTA_CHAIN_USERDATA
] = { .type
= NLA_BINARY
,
1320 .len
= NFT_USERDATA_MAXLEN
},
1323 static const struct nla_policy nft_hook_policy
[NFTA_HOOK_MAX
+ 1] = {
1324 [NFTA_HOOK_HOOKNUM
] = { .type
= NLA_U32
},
1325 [NFTA_HOOK_PRIORITY
] = { .type
= NLA_U32
},
1326 [NFTA_HOOK_DEV
] = { .type
= NLA_STRING
,
1327 .len
= IFNAMSIZ
- 1 },
1330 static int nft_dump_stats(struct sk_buff
*skb
, struct nft_stats __percpu
*stats
)
1332 struct nft_stats
*cpu_stats
, total
;
1333 struct nlattr
*nest
;
1341 memset(&total
, 0, sizeof(total
));
1342 for_each_possible_cpu(cpu
) {
1343 cpu_stats
= per_cpu_ptr(stats
, cpu
);
1345 seq
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
1346 pkts
= cpu_stats
->pkts
;
1347 bytes
= cpu_stats
->bytes
;
1348 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, seq
));
1350 total
.bytes
+= bytes
;
1352 nest
= nla_nest_start_noflag(skb
, NFTA_CHAIN_COUNTERS
);
1354 goto nla_put_failure
;
1356 if (nla_put_be64(skb
, NFTA_COUNTER_PACKETS
, cpu_to_be64(total
.pkts
),
1357 NFTA_COUNTER_PAD
) ||
1358 nla_put_be64(skb
, NFTA_COUNTER_BYTES
, cpu_to_be64(total
.bytes
),
1360 goto nla_put_failure
;
1362 nla_nest_end(skb
, nest
);
1369 static int nft_dump_basechain_hook(struct sk_buff
*skb
, int family
,
1370 const struct nft_base_chain
*basechain
)
1372 const struct nf_hook_ops
*ops
= &basechain
->ops
;
1373 struct nft_hook
*hook
, *first
= NULL
;
1374 struct nlattr
*nest
, *nest_devs
;
1377 nest
= nla_nest_start_noflag(skb
, NFTA_CHAIN_HOOK
);
1379 goto nla_put_failure
;
1380 if (nla_put_be32(skb
, NFTA_HOOK_HOOKNUM
, htonl(ops
->hooknum
)))
1381 goto nla_put_failure
;
1382 if (nla_put_be32(skb
, NFTA_HOOK_PRIORITY
, htonl(ops
->priority
)))
1383 goto nla_put_failure
;
1385 if (nft_base_chain_netdev(family
, ops
->hooknum
)) {
1386 nest_devs
= nla_nest_start_noflag(skb
, NFTA_HOOK_DEVS
);
1387 list_for_each_entry(hook
, &basechain
->hook_list
, list
) {
1391 if (nla_put_string(skb
, NFTA_DEVICE_NAME
,
1392 hook
->ops
.dev
->name
))
1393 goto nla_put_failure
;
1396 nla_nest_end(skb
, nest_devs
);
1399 nla_put_string(skb
, NFTA_HOOK_DEV
, first
->ops
.dev
->name
))
1400 goto nla_put_failure
;
1402 nla_nest_end(skb
, nest
);
1409 static int nf_tables_fill_chain_info(struct sk_buff
*skb
, struct net
*net
,
1410 u32 portid
, u32 seq
, int event
, u32 flags
,
1411 int family
, const struct nft_table
*table
,
1412 const struct nft_chain
*chain
)
1414 struct nlmsghdr
*nlh
;
1415 struct nfgenmsg
*nfmsg
;
1417 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
1418 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
1420 goto nla_put_failure
;
1422 nfmsg
= nlmsg_data(nlh
);
1423 nfmsg
->nfgen_family
= family
;
1424 nfmsg
->version
= NFNETLINK_V0
;
1425 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
1427 if (nla_put_string(skb
, NFTA_CHAIN_TABLE
, table
->name
))
1428 goto nla_put_failure
;
1429 if (nla_put_be64(skb
, NFTA_CHAIN_HANDLE
, cpu_to_be64(chain
->handle
),
1431 goto nla_put_failure
;
1432 if (nla_put_string(skb
, NFTA_CHAIN_NAME
, chain
->name
))
1433 goto nla_put_failure
;
1435 if (nft_is_base_chain(chain
)) {
1436 const struct nft_base_chain
*basechain
= nft_base_chain(chain
);
1437 struct nft_stats __percpu
*stats
;
1439 if (nft_dump_basechain_hook(skb
, family
, basechain
))
1440 goto nla_put_failure
;
1442 if (nla_put_be32(skb
, NFTA_CHAIN_POLICY
,
1443 htonl(basechain
->policy
)))
1444 goto nla_put_failure
;
1446 if (nla_put_string(skb
, NFTA_CHAIN_TYPE
, basechain
->type
->name
))
1447 goto nla_put_failure
;
1449 stats
= rcu_dereference_check(basechain
->stats
,
1450 lockdep_commit_lock_is_held(net
));
1451 if (nft_dump_stats(skb
, stats
))
1452 goto nla_put_failure
;
1456 nla_put_be32(skb
, NFTA_CHAIN_FLAGS
, htonl(chain
->flags
)))
1457 goto nla_put_failure
;
1459 if (nla_put_be32(skb
, NFTA_CHAIN_USE
, htonl(chain
->use
)))
1460 goto nla_put_failure
;
1463 nla_put(skb
, NFTA_CHAIN_USERDATA
, chain
->udlen
, chain
->udata
))
1464 goto nla_put_failure
;
1466 nlmsg_end(skb
, nlh
);
1470 nlmsg_trim(skb
, nlh
);
1474 static void nf_tables_chain_notify(const struct nft_ctx
*ctx
, int event
)
1476 struct sk_buff
*skb
;
1478 char *buf
= kasprintf(GFP_KERNEL
, "%s:%llu;%s:%llu",
1479 ctx
->table
->name
, ctx
->table
->handle
,
1480 ctx
->chain
->name
, ctx
->chain
->handle
);
1482 audit_log_nfcfg(buf
,
1485 event
== NFT_MSG_NEWCHAIN
?
1486 AUDIT_NFT_OP_CHAIN_REGISTER
:
1487 AUDIT_NFT_OP_CHAIN_UNREGISTER
,
1492 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
1495 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1499 err
= nf_tables_fill_chain_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
1500 event
, 0, ctx
->family
, ctx
->table
,
1507 nft_notify_enqueue(skb
, ctx
->report
, &ctx
->net
->nft
.notify_list
);
1510 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
1513 static int nf_tables_dump_chains(struct sk_buff
*skb
,
1514 struct netlink_callback
*cb
)
1516 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1517 const struct nft_table
*table
;
1518 const struct nft_chain
*chain
;
1519 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1520 struct net
*net
= sock_net(skb
->sk
);
1521 int family
= nfmsg
->nfgen_family
;
1524 cb
->seq
= net
->nft
.base_seq
;
1526 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
1527 if (family
!= NFPROTO_UNSPEC
&& family
!= table
->family
)
1530 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
1534 memset(&cb
->args
[1], 0,
1535 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1536 if (!nft_is_active(net
, chain
))
1538 if (nf_tables_fill_chain_info(skb
, net
,
1539 NETLINK_CB(cb
->skb
).portid
,
1543 table
->family
, table
,
1547 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1558 /* called with rcu_read_lock held */
1559 static int nf_tables_getchain(struct net
*net
, struct sock
*nlsk
,
1560 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1561 const struct nlattr
* const nla
[],
1562 struct netlink_ext_ack
*extack
)
1564 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1565 u8 genmask
= nft_genmask_cur(net
);
1566 const struct nft_chain
*chain
;
1567 struct nft_table
*table
;
1568 struct sk_buff
*skb2
;
1569 int family
= nfmsg
->nfgen_family
;
1572 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1573 struct netlink_dump_control c
= {
1574 .dump
= nf_tables_dump_chains
,
1575 .module
= THIS_MODULE
,
1578 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
1581 table
= nft_table_lookup(net
, nla
[NFTA_CHAIN_TABLE
], family
, genmask
);
1582 if (IS_ERR(table
)) {
1583 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_TABLE
]);
1584 return PTR_ERR(table
);
1587 chain
= nft_chain_lookup(net
, table
, nla
[NFTA_CHAIN_NAME
], genmask
);
1588 if (IS_ERR(chain
)) {
1589 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_NAME
]);
1590 return PTR_ERR(chain
);
1593 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1597 err
= nf_tables_fill_chain_info(skb2
, net
, NETLINK_CB(skb
).portid
,
1598 nlh
->nlmsg_seq
, NFT_MSG_NEWCHAIN
, 0,
1599 family
, table
, chain
);
1601 goto err_fill_chain_info
;
1603 return nfnetlink_unicast(skb2
, net
, NETLINK_CB(skb
).portid
);
1605 err_fill_chain_info
:
1610 static const struct nla_policy nft_counter_policy
[NFTA_COUNTER_MAX
+ 1] = {
1611 [NFTA_COUNTER_PACKETS
] = { .type
= NLA_U64
},
1612 [NFTA_COUNTER_BYTES
] = { .type
= NLA_U64
},
1615 static struct nft_stats __percpu
*nft_stats_alloc(const struct nlattr
*attr
)
1617 struct nlattr
*tb
[NFTA_COUNTER_MAX
+1];
1618 struct nft_stats __percpu
*newstats
;
1619 struct nft_stats
*stats
;
1622 err
= nla_parse_nested_deprecated(tb
, NFTA_COUNTER_MAX
, attr
,
1623 nft_counter_policy
, NULL
);
1625 return ERR_PTR(err
);
1627 if (!tb
[NFTA_COUNTER_BYTES
] || !tb
[NFTA_COUNTER_PACKETS
])
1628 return ERR_PTR(-EINVAL
);
1630 newstats
= netdev_alloc_pcpu_stats(struct nft_stats
);
1631 if (newstats
== NULL
)
1632 return ERR_PTR(-ENOMEM
);
1634 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1635 * are not exposed to userspace.
1638 stats
= this_cpu_ptr(newstats
);
1639 stats
->bytes
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_BYTES
]));
1640 stats
->pkts
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_PACKETS
]));
1646 static void nft_chain_stats_replace(struct nft_trans
*trans
)
1648 struct nft_base_chain
*chain
= nft_base_chain(trans
->ctx
.chain
);
1650 if (!nft_trans_chain_stats(trans
))
1653 nft_trans_chain_stats(trans
) =
1654 rcu_replace_pointer(chain
->stats
, nft_trans_chain_stats(trans
),
1655 lockdep_commit_lock_is_held(trans
->ctx
.net
));
1657 if (!nft_trans_chain_stats(trans
))
1658 static_branch_inc(&nft_counters_enabled
);
1661 static void nf_tables_chain_free_chain_rules(struct nft_chain
*chain
)
1663 struct nft_rule
**g0
= rcu_dereference_raw(chain
->rules_gen_0
);
1664 struct nft_rule
**g1
= rcu_dereference_raw(chain
->rules_gen_1
);
1670 /* should be NULL either via abort or via successful commit */
1671 WARN_ON_ONCE(chain
->rules_next
);
1672 kvfree(chain
->rules_next
);
1675 void nf_tables_chain_destroy(struct nft_ctx
*ctx
)
1677 struct nft_chain
*chain
= ctx
->chain
;
1678 struct nft_hook
*hook
, *next
;
1680 if (WARN_ON(chain
->use
> 0))
1683 /* no concurrent access possible anymore */
1684 nf_tables_chain_free_chain_rules(chain
);
1686 if (nft_is_base_chain(chain
)) {
1687 struct nft_base_chain
*basechain
= nft_base_chain(chain
);
1689 if (nft_base_chain_netdev(ctx
->family
, basechain
->ops
.hooknum
)) {
1690 list_for_each_entry_safe(hook
, next
,
1691 &basechain
->hook_list
, list
) {
1692 list_del_rcu(&hook
->list
);
1693 kfree_rcu(hook
, rcu
);
1696 module_put(basechain
->type
->owner
);
1697 if (rcu_access_pointer(basechain
->stats
)) {
1698 static_branch_dec(&nft_counters_enabled
);
1699 free_percpu(rcu_dereference_raw(basechain
->stats
));
1702 kfree(chain
->udata
);
1706 kfree(chain
->udata
);
1711 static struct nft_hook
*nft_netdev_hook_alloc(struct net
*net
,
1712 const struct nlattr
*attr
)
1714 struct net_device
*dev
;
1715 char ifname
[IFNAMSIZ
];
1716 struct nft_hook
*hook
;
1719 hook
= kmalloc(sizeof(struct nft_hook
), GFP_KERNEL
);
1722 goto err_hook_alloc
;
1725 nla_strlcpy(ifname
, attr
, IFNAMSIZ
);
1726 dev
= __dev_get_by_name(net
, ifname
);
1731 hook
->ops
.dev
= dev
;
1732 hook
->inactive
= false;
1739 return ERR_PTR(err
);
1742 static struct nft_hook
*nft_hook_list_find(struct list_head
*hook_list
,
1743 const struct nft_hook
*this)
1745 struct nft_hook
*hook
;
1747 list_for_each_entry(hook
, hook_list
, list
) {
1748 if (this->ops
.dev
== hook
->ops
.dev
)
1755 static int nf_tables_parse_netdev_hooks(struct net
*net
,
1756 const struct nlattr
*attr
,
1757 struct list_head
*hook_list
)
1759 struct nft_hook
*hook
, *next
;
1760 const struct nlattr
*tmp
;
1761 int rem
, n
= 0, err
;
1763 nla_for_each_nested(tmp
, attr
, rem
) {
1764 if (nla_type(tmp
) != NFTA_DEVICE_NAME
) {
1769 hook
= nft_netdev_hook_alloc(net
, tmp
);
1771 err
= PTR_ERR(hook
);
1774 if (nft_hook_list_find(hook_list
, hook
)) {
1779 list_add_tail(&hook
->list
, hook_list
);
1782 if (n
== NFT_NETDEVICE_MAX
) {
1791 list_for_each_entry_safe(hook
, next
, hook_list
, list
) {
1792 list_del(&hook
->list
);
1798 struct nft_chain_hook
{
1801 const struct nft_chain_type
*type
;
1802 struct list_head list
;
1805 static int nft_chain_parse_netdev(struct net
*net
,
1806 struct nlattr
*tb
[],
1807 struct list_head
*hook_list
)
1809 struct nft_hook
*hook
;
1812 if (tb
[NFTA_HOOK_DEV
]) {
1813 hook
= nft_netdev_hook_alloc(net
, tb
[NFTA_HOOK_DEV
]);
1815 return PTR_ERR(hook
);
1817 list_add_tail(&hook
->list
, hook_list
);
1818 } else if (tb
[NFTA_HOOK_DEVS
]) {
1819 err
= nf_tables_parse_netdev_hooks(net
, tb
[NFTA_HOOK_DEVS
],
1824 if (list_empty(hook_list
))
1833 static int nft_chain_parse_hook(struct net
*net
,
1834 const struct nlattr
* const nla
[],
1835 struct nft_chain_hook
*hook
, u8 family
,
1838 struct nlattr
*ha
[NFTA_HOOK_MAX
+ 1];
1839 const struct nft_chain_type
*type
;
1842 lockdep_assert_held(&net
->nft
.commit_mutex
);
1843 lockdep_nfnl_nft_mutex_not_held();
1845 err
= nla_parse_nested_deprecated(ha
, NFTA_HOOK_MAX
,
1846 nla
[NFTA_CHAIN_HOOK
],
1847 nft_hook_policy
, NULL
);
1851 if (ha
[NFTA_HOOK_HOOKNUM
] == NULL
||
1852 ha
[NFTA_HOOK_PRIORITY
] == NULL
)
1855 hook
->num
= ntohl(nla_get_be32(ha
[NFTA_HOOK_HOOKNUM
]));
1856 hook
->priority
= ntohl(nla_get_be32(ha
[NFTA_HOOK_PRIORITY
]));
1858 type
= __nft_chain_type_get(family
, NFT_CHAIN_T_DEFAULT
);
1862 if (nla
[NFTA_CHAIN_TYPE
]) {
1863 type
= nf_tables_chain_type_lookup(net
, nla
[NFTA_CHAIN_TYPE
],
1866 return PTR_ERR(type
);
1868 if (hook
->num
>= NFT_MAX_HOOKS
|| !(type
->hook_mask
& (1 << hook
->num
)))
1871 if (type
->type
== NFT_CHAIN_T_NAT
&&
1872 hook
->priority
<= NF_IP_PRI_CONNTRACK
)
1875 if (!try_module_get(type
->owner
))
1880 INIT_LIST_HEAD(&hook
->list
);
1881 if (nft_base_chain_netdev(family
, hook
->num
)) {
1882 err
= nft_chain_parse_netdev(net
, ha
, &hook
->list
);
1884 module_put(type
->owner
);
1887 } else if (ha
[NFTA_HOOK_DEV
] || ha
[NFTA_HOOK_DEVS
]) {
1888 module_put(type
->owner
);
1895 static void nft_chain_release_hook(struct nft_chain_hook
*hook
)
1897 struct nft_hook
*h
, *next
;
1899 list_for_each_entry_safe(h
, next
, &hook
->list
, list
) {
1903 module_put(hook
->type
->owner
);
1906 struct nft_rules_old
{
1908 struct nft_rule
**start
;
1911 static struct nft_rule
**nf_tables_chain_alloc_rules(const struct nft_chain
*chain
,
1914 if (alloc
> INT_MAX
)
1917 alloc
+= 1; /* NULL, ends rules */
1918 if (sizeof(struct nft_rule
*) > INT_MAX
/ alloc
)
1921 alloc
*= sizeof(struct nft_rule
*);
1922 alloc
+= sizeof(struct nft_rules_old
);
1924 return kvmalloc(alloc
, GFP_KERNEL
);
1927 static void nft_basechain_hook_init(struct nf_hook_ops
*ops
, u8 family
,
1928 const struct nft_chain_hook
*hook
,
1929 struct nft_chain
*chain
)
1932 ops
->hooknum
= hook
->num
;
1933 ops
->priority
= hook
->priority
;
1935 ops
->hook
= hook
->type
->hooks
[ops
->hooknum
];
1938 static int nft_basechain_init(struct nft_base_chain
*basechain
, u8 family
,
1939 struct nft_chain_hook
*hook
, u32 flags
)
1941 struct nft_chain
*chain
;
1944 basechain
->type
= hook
->type
;
1945 INIT_LIST_HEAD(&basechain
->hook_list
);
1946 chain
= &basechain
->chain
;
1948 if (nft_base_chain_netdev(family
, hook
->num
)) {
1949 list_splice_init(&hook
->list
, &basechain
->hook_list
);
1950 list_for_each_entry(h
, &basechain
->hook_list
, list
)
1951 nft_basechain_hook_init(&h
->ops
, family
, hook
, chain
);
1953 basechain
->ops
.hooknum
= hook
->num
;
1954 basechain
->ops
.priority
= hook
->priority
;
1956 nft_basechain_hook_init(&basechain
->ops
, family
, hook
, chain
);
1959 chain
->flags
|= NFT_CHAIN_BASE
| flags
;
1960 basechain
->policy
= NF_ACCEPT
;
1961 if (chain
->flags
& NFT_CHAIN_HW_OFFLOAD
&&
1962 nft_chain_offload_priority(basechain
) < 0)
1965 flow_block_init(&basechain
->flow_block
);
1970 static int nft_chain_add(struct nft_table
*table
, struct nft_chain
*chain
)
1974 err
= rhltable_insert_key(&table
->chains_ht
, chain
->name
,
1975 &chain
->rhlhead
, nft_chain_ht_params
);
1979 list_add_tail_rcu(&chain
->list
, &table
->chains
);
1984 static u64 chain_id
;
1986 static int nf_tables_addchain(struct nft_ctx
*ctx
, u8 family
, u8 genmask
,
1987 u8 policy
, u32 flags
)
1989 const struct nlattr
* const *nla
= ctx
->nla
;
1990 struct nft_table
*table
= ctx
->table
;
1991 struct nft_base_chain
*basechain
;
1992 struct nft_stats __percpu
*stats
;
1993 struct net
*net
= ctx
->net
;
1994 char name
[NFT_NAME_MAXLEN
];
1995 struct nft_trans
*trans
;
1996 struct nft_chain
*chain
;
1997 struct nft_rule
**rules
;
2000 if (table
->use
== UINT_MAX
)
2003 if (nla
[NFTA_CHAIN_HOOK
]) {
2004 struct nft_chain_hook hook
;
2006 if (flags
& NFT_CHAIN_BINDING
)
2009 err
= nft_chain_parse_hook(net
, nla
, &hook
, family
, true);
2013 basechain
= kzalloc(sizeof(*basechain
), GFP_KERNEL
);
2014 if (basechain
== NULL
) {
2015 nft_chain_release_hook(&hook
);
2018 chain
= &basechain
->chain
;
2020 if (nla
[NFTA_CHAIN_COUNTERS
]) {
2021 stats
= nft_stats_alloc(nla
[NFTA_CHAIN_COUNTERS
]);
2022 if (IS_ERR(stats
)) {
2023 nft_chain_release_hook(&hook
);
2025 return PTR_ERR(stats
);
2027 rcu_assign_pointer(basechain
->stats
, stats
);
2028 static_branch_inc(&nft_counters_enabled
);
2031 err
= nft_basechain_init(basechain
, family
, &hook
, flags
);
2033 nft_chain_release_hook(&hook
);
2038 if (flags
& NFT_CHAIN_BASE
)
2040 if (flags
& NFT_CHAIN_HW_OFFLOAD
)
2043 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
2047 chain
->flags
= flags
;
2051 INIT_LIST_HEAD(&chain
->rules
);
2052 chain
->handle
= nf_tables_alloc_handle(table
);
2053 chain
->table
= table
;
2055 if (nla
[NFTA_CHAIN_NAME
]) {
2056 chain
->name
= nla_strdup(nla
[NFTA_CHAIN_NAME
], GFP_KERNEL
);
2058 if (!(flags
& NFT_CHAIN_BINDING
)) {
2060 goto err_destroy_chain
;
2063 snprintf(name
, sizeof(name
), "__chain%llu", ++chain_id
);
2064 chain
->name
= kstrdup(name
, GFP_KERNEL
);
2069 goto err_destroy_chain
;
2072 if (nla
[NFTA_CHAIN_USERDATA
]) {
2073 chain
->udata
= nla_memdup(nla
[NFTA_CHAIN_USERDATA
], GFP_KERNEL
);
2074 if (chain
->udata
== NULL
) {
2076 goto err_destroy_chain
;
2078 chain
->udlen
= nla_len(nla
[NFTA_CHAIN_USERDATA
]);
2081 rules
= nf_tables_chain_alloc_rules(chain
, 0);
2084 goto err_destroy_chain
;
2088 rcu_assign_pointer(chain
->rules_gen_0
, rules
);
2089 rcu_assign_pointer(chain
->rules_gen_1
, rules
);
2091 err
= nf_tables_register_hook(net
, table
, chain
);
2093 goto err_destroy_chain
;
2095 trans
= nft_trans_chain_add(ctx
, NFT_MSG_NEWCHAIN
);
2096 if (IS_ERR(trans
)) {
2097 err
= PTR_ERR(trans
);
2098 goto err_unregister_hook
;
2101 nft_trans_chain_policy(trans
) = NFT_CHAIN_POLICY_UNSET
;
2102 if (nft_is_base_chain(chain
))
2103 nft_trans_chain_policy(trans
) = policy
;
2105 err
= nft_chain_add(table
, chain
);
2107 nft_trans_destroy(trans
);
2108 goto err_unregister_hook
;
2114 err_unregister_hook
:
2115 nf_tables_unregister_hook(net
, table
, chain
);
2117 nf_tables_chain_destroy(ctx
);
2122 static bool nft_hook_list_equal(struct list_head
*hook_list1
,
2123 struct list_head
*hook_list2
)
2125 struct nft_hook
*hook
;
2129 list_for_each_entry(hook
, hook_list2
, list
) {
2130 if (!nft_hook_list_find(hook_list1
, hook
))
2135 list_for_each_entry(hook
, hook_list1
, list
)
2141 static int nf_tables_updchain(struct nft_ctx
*ctx
, u8 genmask
, u8 policy
,
2142 u32 flags
, const struct nlattr
*attr
,
2143 struct netlink_ext_ack
*extack
)
2145 const struct nlattr
* const *nla
= ctx
->nla
;
2146 struct nft_table
*table
= ctx
->table
;
2147 struct nft_chain
*chain
= ctx
->chain
;
2148 struct nft_base_chain
*basechain
;
2149 struct nft_stats
*stats
= NULL
;
2150 struct nft_chain_hook hook
;
2151 struct nf_hook_ops
*ops
;
2152 struct nft_trans
*trans
;
2155 if (chain
->flags
^ flags
)
2158 if (nla
[NFTA_CHAIN_HOOK
]) {
2159 if (!nft_is_base_chain(chain
)) {
2160 NL_SET_BAD_ATTR(extack
, attr
);
2163 err
= nft_chain_parse_hook(ctx
->net
, nla
, &hook
, ctx
->family
,
2168 basechain
= nft_base_chain(chain
);
2169 if (basechain
->type
!= hook
.type
) {
2170 nft_chain_release_hook(&hook
);
2171 NL_SET_BAD_ATTR(extack
, attr
);
2175 if (nft_base_chain_netdev(ctx
->family
, hook
.num
)) {
2176 if (!nft_hook_list_equal(&basechain
->hook_list
,
2178 nft_chain_release_hook(&hook
);
2179 NL_SET_BAD_ATTR(extack
, attr
);
2183 ops
= &basechain
->ops
;
2184 if (ops
->hooknum
!= hook
.num
||
2185 ops
->priority
!= hook
.priority
) {
2186 nft_chain_release_hook(&hook
);
2187 NL_SET_BAD_ATTR(extack
, attr
);
2191 nft_chain_release_hook(&hook
);
2194 if (nla
[NFTA_CHAIN_HANDLE
] &&
2195 nla
[NFTA_CHAIN_NAME
]) {
2196 struct nft_chain
*chain2
;
2198 chain2
= nft_chain_lookup(ctx
->net
, table
,
2199 nla
[NFTA_CHAIN_NAME
], genmask
);
2200 if (!IS_ERR(chain2
)) {
2201 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_NAME
]);
2206 if (nla
[NFTA_CHAIN_COUNTERS
]) {
2207 if (!nft_is_base_chain(chain
))
2210 stats
= nft_stats_alloc(nla
[NFTA_CHAIN_COUNTERS
]);
2212 return PTR_ERR(stats
);
2216 trans
= nft_trans_alloc(ctx
, NFT_MSG_NEWCHAIN
,
2217 sizeof(struct nft_trans_chain
));
2221 nft_trans_chain_stats(trans
) = stats
;
2222 nft_trans_chain_update(trans
) = true;
2224 if (nla
[NFTA_CHAIN_POLICY
])
2225 nft_trans_chain_policy(trans
) = policy
;
2227 nft_trans_chain_policy(trans
) = -1;
2229 if (nla
[NFTA_CHAIN_HANDLE
] &&
2230 nla
[NFTA_CHAIN_NAME
]) {
2231 struct nft_trans
*tmp
;
2235 name
= nla_strdup(nla
[NFTA_CHAIN_NAME
], GFP_KERNEL
);
2240 list_for_each_entry(tmp
, &ctx
->net
->nft
.commit_list
, list
) {
2241 if (tmp
->msg_type
== NFT_MSG_NEWCHAIN
&&
2242 tmp
->ctx
.table
== table
&&
2243 nft_trans_chain_update(tmp
) &&
2244 nft_trans_chain_name(tmp
) &&
2245 strcmp(name
, nft_trans_chain_name(tmp
)) == 0) {
2246 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_NAME
]);
2252 nft_trans_chain_name(trans
) = name
;
2254 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
2263 static struct nft_chain
*nft_chain_lookup_byid(const struct net
*net
,
2264 const struct nlattr
*nla
)
2266 u32 id
= ntohl(nla_get_be32(nla
));
2267 struct nft_trans
*trans
;
2269 list_for_each_entry(trans
, &net
->nft
.commit_list
, list
) {
2270 struct nft_chain
*chain
= trans
->ctx
.chain
;
2272 if (trans
->msg_type
== NFT_MSG_NEWCHAIN
&&
2273 id
== nft_trans_chain_id(trans
))
2276 return ERR_PTR(-ENOENT
);
2279 static int nf_tables_newchain(struct net
*net
, struct sock
*nlsk
,
2280 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2281 const struct nlattr
* const nla
[],
2282 struct netlink_ext_ack
*extack
)
2284 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2285 u8 genmask
= nft_genmask_next(net
);
2286 int family
= nfmsg
->nfgen_family
;
2287 struct nft_chain
*chain
= NULL
;
2288 const struct nlattr
*attr
;
2289 struct nft_table
*table
;
2290 u8 policy
= NF_ACCEPT
;
2295 lockdep_assert_held(&net
->nft
.commit_mutex
);
2297 table
= nft_table_lookup(net
, nla
[NFTA_CHAIN_TABLE
], family
, genmask
);
2298 if (IS_ERR(table
)) {
2299 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_TABLE
]);
2300 return PTR_ERR(table
);
2304 attr
= nla
[NFTA_CHAIN_NAME
];
2306 if (nla
[NFTA_CHAIN_HANDLE
]) {
2307 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_CHAIN_HANDLE
]));
2308 chain
= nft_chain_lookup_byhandle(table
, handle
, genmask
);
2309 if (IS_ERR(chain
)) {
2310 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_HANDLE
]);
2311 return PTR_ERR(chain
);
2313 attr
= nla
[NFTA_CHAIN_HANDLE
];
2314 } else if (nla
[NFTA_CHAIN_NAME
]) {
2315 chain
= nft_chain_lookup(net
, table
, attr
, genmask
);
2316 if (IS_ERR(chain
)) {
2317 if (PTR_ERR(chain
) != -ENOENT
) {
2318 NL_SET_BAD_ATTR(extack
, attr
);
2319 return PTR_ERR(chain
);
2323 } else if (!nla
[NFTA_CHAIN_ID
]) {
2327 if (nla
[NFTA_CHAIN_POLICY
]) {
2328 if (chain
!= NULL
&&
2329 !nft_is_base_chain(chain
)) {
2330 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_POLICY
]);
2334 if (chain
== NULL
&&
2335 nla
[NFTA_CHAIN_HOOK
] == NULL
) {
2336 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_POLICY
]);
2340 policy
= ntohl(nla_get_be32(nla
[NFTA_CHAIN_POLICY
]));
2350 if (nla
[NFTA_CHAIN_FLAGS
])
2351 flags
= ntohl(nla_get_be32(nla
[NFTA_CHAIN_FLAGS
]));
2353 flags
= chain
->flags
;
2355 if (flags
& ~NFT_CHAIN_FLAGS
)
2358 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, chain
, nla
);
2360 if (chain
!= NULL
) {
2361 if (nlh
->nlmsg_flags
& NLM_F_EXCL
) {
2362 NL_SET_BAD_ATTR(extack
, attr
);
2365 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2368 flags
|= chain
->flags
& NFT_CHAIN_BASE
;
2369 return nf_tables_updchain(&ctx
, genmask
, policy
, flags
, attr
,
2373 return nf_tables_addchain(&ctx
, family
, genmask
, policy
, flags
);
2376 static int nf_tables_delchain(struct net
*net
, struct sock
*nlsk
,
2377 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2378 const struct nlattr
* const nla
[],
2379 struct netlink_ext_ack
*extack
)
2381 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2382 u8 genmask
= nft_genmask_next(net
);
2383 int family
= nfmsg
->nfgen_family
;
2384 const struct nlattr
*attr
;
2385 struct nft_table
*table
;
2386 struct nft_chain
*chain
;
2387 struct nft_rule
*rule
;
2393 table
= nft_table_lookup(net
, nla
[NFTA_CHAIN_TABLE
], family
, genmask
);
2394 if (IS_ERR(table
)) {
2395 NL_SET_BAD_ATTR(extack
, nla
[NFTA_CHAIN_TABLE
]);
2396 return PTR_ERR(table
);
2399 if (nla
[NFTA_CHAIN_HANDLE
]) {
2400 attr
= nla
[NFTA_CHAIN_HANDLE
];
2401 handle
= be64_to_cpu(nla_get_be64(attr
));
2402 chain
= nft_chain_lookup_byhandle(table
, handle
, genmask
);
2404 attr
= nla
[NFTA_CHAIN_NAME
];
2405 chain
= nft_chain_lookup(net
, table
, attr
, genmask
);
2407 if (IS_ERR(chain
)) {
2408 NL_SET_BAD_ATTR(extack
, attr
);
2409 return PTR_ERR(chain
);
2412 if (nlh
->nlmsg_flags
& NLM_F_NONREC
&&
2416 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, chain
, nla
);
2419 list_for_each_entry(rule
, &chain
->rules
, list
) {
2420 if (!nft_is_active_next(net
, rule
))
2424 err
= nft_delrule(&ctx
, rule
);
2429 /* There are rules and elements that are still holding references to us,
2430 * we cannot do a recursive removal in this case.
2433 NL_SET_BAD_ATTR(extack
, attr
);
2437 return nft_delchain(&ctx
);
2445 * nft_register_expr - register nf_tables expr type
2448 * Registers the expr type for use with nf_tables. Returns zero on
2449 * success or a negative errno code otherwise.
2451 int nft_register_expr(struct nft_expr_type
*type
)
2453 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2454 if (type
->family
== NFPROTO_UNSPEC
)
2455 list_add_tail_rcu(&type
->list
, &nf_tables_expressions
);
2457 list_add_rcu(&type
->list
, &nf_tables_expressions
);
2458 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2461 EXPORT_SYMBOL_GPL(nft_register_expr
);
2464 * nft_unregister_expr - unregister nf_tables expr type
2467 * Unregisters the expr typefor use with nf_tables.
2469 void nft_unregister_expr(struct nft_expr_type
*type
)
2471 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2472 list_del_rcu(&type
->list
);
2473 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2475 EXPORT_SYMBOL_GPL(nft_unregister_expr
);
2477 static const struct nft_expr_type
*__nft_expr_type_get(u8 family
,
2480 const struct nft_expr_type
*type
, *candidate
= NULL
;
2482 list_for_each_entry(type
, &nf_tables_expressions
, list
) {
2483 if (!nla_strcmp(nla
, type
->name
)) {
2484 if (!type
->family
&& !candidate
)
2486 else if (type
->family
== family
)
2493 #ifdef CONFIG_MODULES
2494 static int nft_expr_type_request_module(struct net
*net
, u8 family
,
2497 if (nft_request_module(net
, "nft-expr-%u-%.*s", family
,
2498 nla_len(nla
), (char *)nla_data(nla
)) == -EAGAIN
)
2505 static const struct nft_expr_type
*nft_expr_type_get(struct net
*net
,
2509 const struct nft_expr_type
*type
;
2512 return ERR_PTR(-EINVAL
);
2514 type
= __nft_expr_type_get(family
, nla
);
2515 if (type
!= NULL
&& try_module_get(type
->owner
))
2518 lockdep_nfnl_nft_mutex_not_held();
2519 #ifdef CONFIG_MODULES
2521 if (nft_expr_type_request_module(net
, family
, nla
) == -EAGAIN
)
2522 return ERR_PTR(-EAGAIN
);
2524 if (nft_request_module(net
, "nft-expr-%.*s",
2526 (char *)nla_data(nla
)) == -EAGAIN
)
2527 return ERR_PTR(-EAGAIN
);
2530 return ERR_PTR(-ENOENT
);
2533 static const struct nla_policy nft_expr_policy
[NFTA_EXPR_MAX
+ 1] = {
2534 [NFTA_EXPR_NAME
] = { .type
= NLA_STRING
,
2535 .len
= NFT_MODULE_AUTOLOAD_LIMIT
},
2536 [NFTA_EXPR_DATA
] = { .type
= NLA_NESTED
},
2539 static int nf_tables_fill_expr_info(struct sk_buff
*skb
,
2540 const struct nft_expr
*expr
)
2542 if (nla_put_string(skb
, NFTA_EXPR_NAME
, expr
->ops
->type
->name
))
2543 goto nla_put_failure
;
2545 if (expr
->ops
->dump
) {
2546 struct nlattr
*data
= nla_nest_start_noflag(skb
,
2549 goto nla_put_failure
;
2550 if (expr
->ops
->dump(skb
, expr
) < 0)
2551 goto nla_put_failure
;
2552 nla_nest_end(skb
, data
);
2561 int nft_expr_dump(struct sk_buff
*skb
, unsigned int attr
,
2562 const struct nft_expr
*expr
)
2564 struct nlattr
*nest
;
2566 nest
= nla_nest_start_noflag(skb
, attr
);
2568 goto nla_put_failure
;
2569 if (nf_tables_fill_expr_info(skb
, expr
) < 0)
2570 goto nla_put_failure
;
2571 nla_nest_end(skb
, nest
);
2578 struct nft_expr_info
{
2579 const struct nft_expr_ops
*ops
;
2580 const struct nlattr
*attr
;
2581 struct nlattr
*tb
[NFT_EXPR_MAXATTR
+ 1];
2584 static int nf_tables_expr_parse(const struct nft_ctx
*ctx
,
2585 const struct nlattr
*nla
,
2586 struct nft_expr_info
*info
)
2588 const struct nft_expr_type
*type
;
2589 const struct nft_expr_ops
*ops
;
2590 struct nlattr
*tb
[NFTA_EXPR_MAX
+ 1];
2593 err
= nla_parse_nested_deprecated(tb
, NFTA_EXPR_MAX
, nla
,
2594 nft_expr_policy
, NULL
);
2598 type
= nft_expr_type_get(ctx
->net
, ctx
->family
, tb
[NFTA_EXPR_NAME
]);
2600 return PTR_ERR(type
);
2602 if (tb
[NFTA_EXPR_DATA
]) {
2603 err
= nla_parse_nested_deprecated(info
->tb
, type
->maxattr
,
2605 type
->policy
, NULL
);
2609 memset(info
->tb
, 0, sizeof(info
->tb
[0]) * (type
->maxattr
+ 1));
2611 if (type
->select_ops
!= NULL
) {
2612 ops
= type
->select_ops(ctx
,
2613 (const struct nlattr
* const *)info
->tb
);
2616 #ifdef CONFIG_MODULES
2618 if (nft_expr_type_request_module(ctx
->net
,
2620 tb
[NFTA_EXPR_NAME
]) != -EAGAIN
)
2634 module_put(type
->owner
);
2638 static int nf_tables_newexpr(const struct nft_ctx
*ctx
,
2639 const struct nft_expr_info
*info
,
2640 struct nft_expr
*expr
)
2642 const struct nft_expr_ops
*ops
= info
->ops
;
2647 err
= ops
->init(ctx
, expr
, (const struct nlattr
**)info
->tb
);
2658 static void nf_tables_expr_destroy(const struct nft_ctx
*ctx
,
2659 struct nft_expr
*expr
)
2661 const struct nft_expr_type
*type
= expr
->ops
->type
;
2663 if (expr
->ops
->destroy
)
2664 expr
->ops
->destroy(ctx
, expr
);
2665 module_put(type
->owner
);
2668 static struct nft_expr
*nft_expr_init(const struct nft_ctx
*ctx
,
2669 const struct nlattr
*nla
)
2671 struct nft_expr_info info
;
2672 struct nft_expr
*expr
;
2673 struct module
*owner
;
2676 err
= nf_tables_expr_parse(ctx
, nla
, &info
);
2681 expr
= kzalloc(info
.ops
->size
, GFP_KERNEL
);
2685 err
= nf_tables_newexpr(ctx
, &info
, expr
);
2693 owner
= info
.ops
->type
->owner
;
2694 if (info
.ops
->type
->release_ops
)
2695 info
.ops
->type
->release_ops(info
.ops
);
2699 return ERR_PTR(err
);
2702 int nft_expr_clone(struct nft_expr
*dst
, struct nft_expr
*src
)
2706 if (src
->ops
->clone
) {
2707 dst
->ops
= src
->ops
;
2708 err
= src
->ops
->clone(dst
, src
);
2712 memcpy(dst
, src
, src
->ops
->size
);
2715 __module_get(src
->ops
->type
->owner
);
2720 void nft_expr_destroy(const struct nft_ctx
*ctx
, struct nft_expr
*expr
)
2722 nf_tables_expr_destroy(ctx
, expr
);
2730 static struct nft_rule
*__nft_rule_lookup(const struct nft_chain
*chain
,
2733 struct nft_rule
*rule
;
2735 // FIXME: this sucks
2736 list_for_each_entry_rcu(rule
, &chain
->rules
, list
) {
2737 if (handle
== rule
->handle
)
2741 return ERR_PTR(-ENOENT
);
2744 static struct nft_rule
*nft_rule_lookup(const struct nft_chain
*chain
,
2745 const struct nlattr
*nla
)
2748 return ERR_PTR(-EINVAL
);
2750 return __nft_rule_lookup(chain
, be64_to_cpu(nla_get_be64(nla
)));
2753 static const struct nla_policy nft_rule_policy
[NFTA_RULE_MAX
+ 1] = {
2754 [NFTA_RULE_TABLE
] = { .type
= NLA_STRING
,
2755 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
2756 [NFTA_RULE_CHAIN
] = { .type
= NLA_STRING
,
2757 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
2758 [NFTA_RULE_HANDLE
] = { .type
= NLA_U64
},
2759 [NFTA_RULE_EXPRESSIONS
] = { .type
= NLA_NESTED
},
2760 [NFTA_RULE_COMPAT
] = { .type
= NLA_NESTED
},
2761 [NFTA_RULE_POSITION
] = { .type
= NLA_U64
},
2762 [NFTA_RULE_USERDATA
] = { .type
= NLA_BINARY
,
2763 .len
= NFT_USERDATA_MAXLEN
},
2764 [NFTA_RULE_ID
] = { .type
= NLA_U32
},
2765 [NFTA_RULE_POSITION_ID
] = { .type
= NLA_U32
},
2766 [NFTA_RULE_CHAIN_ID
] = { .type
= NLA_U32
},
2769 static int nf_tables_fill_rule_info(struct sk_buff
*skb
, struct net
*net
,
2770 u32 portid
, u32 seq
, int event
,
2771 u32 flags
, int family
,
2772 const struct nft_table
*table
,
2773 const struct nft_chain
*chain
,
2774 const struct nft_rule
*rule
,
2775 const struct nft_rule
*prule
)
2777 struct nlmsghdr
*nlh
;
2778 struct nfgenmsg
*nfmsg
;
2779 const struct nft_expr
*expr
, *next
;
2780 struct nlattr
*list
;
2781 u16 type
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
2783 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(struct nfgenmsg
), flags
);
2785 goto nla_put_failure
;
2787 nfmsg
= nlmsg_data(nlh
);
2788 nfmsg
->nfgen_family
= family
;
2789 nfmsg
->version
= NFNETLINK_V0
;
2790 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
2792 if (nla_put_string(skb
, NFTA_RULE_TABLE
, table
->name
))
2793 goto nla_put_failure
;
2794 if (nla_put_string(skb
, NFTA_RULE_CHAIN
, chain
->name
))
2795 goto nla_put_failure
;
2796 if (nla_put_be64(skb
, NFTA_RULE_HANDLE
, cpu_to_be64(rule
->handle
),
2798 goto nla_put_failure
;
2800 if (event
!= NFT_MSG_DELRULE
&& prule
) {
2801 if (nla_put_be64(skb
, NFTA_RULE_POSITION
,
2802 cpu_to_be64(prule
->handle
),
2804 goto nla_put_failure
;
2807 list
= nla_nest_start_noflag(skb
, NFTA_RULE_EXPRESSIONS
);
2809 goto nla_put_failure
;
2810 nft_rule_for_each_expr(expr
, next
, rule
) {
2811 if (nft_expr_dump(skb
, NFTA_LIST_ELEM
, expr
) < 0)
2812 goto nla_put_failure
;
2814 nla_nest_end(skb
, list
);
2817 struct nft_userdata
*udata
= nft_userdata(rule
);
2818 if (nla_put(skb
, NFTA_RULE_USERDATA
, udata
->len
+ 1,
2820 goto nla_put_failure
;
2823 nlmsg_end(skb
, nlh
);
2827 nlmsg_trim(skb
, nlh
);
2831 static void nf_tables_rule_notify(const struct nft_ctx
*ctx
,
2832 const struct nft_rule
*rule
, int event
)
2834 struct sk_buff
*skb
;
2836 char *buf
= kasprintf(GFP_KERNEL
, "%s:%llu;%s:%llu",
2837 ctx
->table
->name
, ctx
->table
->handle
,
2838 ctx
->chain
->name
, ctx
->chain
->handle
);
2840 audit_log_nfcfg(buf
,
2843 event
== NFT_MSG_NEWRULE
?
2844 AUDIT_NFT_OP_RULE_REGISTER
:
2845 AUDIT_NFT_OP_RULE_UNREGISTER
,
2850 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
2853 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
2857 err
= nf_tables_fill_rule_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
2858 event
, 0, ctx
->family
, ctx
->table
,
2859 ctx
->chain
, rule
, NULL
);
2865 nft_notify_enqueue(skb
, ctx
->report
, &ctx
->net
->nft
.notify_list
);
2868 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
2871 struct nft_rule_dump_ctx
{
2876 static int __nf_tables_dump_rules(struct sk_buff
*skb
,
2878 struct netlink_callback
*cb
,
2879 const struct nft_table
*table
,
2880 const struct nft_chain
*chain
)
2882 struct net
*net
= sock_net(skb
->sk
);
2883 const struct nft_rule
*rule
, *prule
;
2884 unsigned int s_idx
= cb
->args
[0];
2887 list_for_each_entry_rcu(rule
, &chain
->rules
, list
) {
2888 if (!nft_is_active(net
, rule
))
2893 memset(&cb
->args
[1], 0,
2894 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
2896 if (nf_tables_fill_rule_info(skb
, net
, NETLINK_CB(cb
->skb
).portid
,
2899 NLM_F_MULTI
| NLM_F_APPEND
,
2901 table
, chain
, rule
, prule
) < 0)
2904 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2913 static int nf_tables_dump_rules(struct sk_buff
*skb
,
2914 struct netlink_callback
*cb
)
2916 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2917 const struct nft_rule_dump_ctx
*ctx
= cb
->data
;
2918 struct nft_table
*table
;
2919 const struct nft_chain
*chain
;
2920 unsigned int idx
= 0;
2921 struct net
*net
= sock_net(skb
->sk
);
2922 int family
= nfmsg
->nfgen_family
;
2925 cb
->seq
= net
->nft
.base_seq
;
2927 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
2928 if (family
!= NFPROTO_UNSPEC
&& family
!= table
->family
)
2931 if (ctx
&& ctx
->table
&& strcmp(ctx
->table
, table
->name
) != 0)
2934 if (ctx
&& ctx
->table
&& ctx
->chain
) {
2935 struct rhlist_head
*list
, *tmp
;
2937 list
= rhltable_lookup(&table
->chains_ht
, ctx
->chain
,
2938 nft_chain_ht_params
);
2942 rhl_for_each_entry_rcu(chain
, tmp
, list
, rhlhead
) {
2943 if (!nft_is_active(net
, chain
))
2945 __nf_tables_dump_rules(skb
, &idx
,
2952 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
2953 if (__nf_tables_dump_rules(skb
, &idx
, cb
, table
, chain
))
2957 if (ctx
&& ctx
->table
)
2967 static int nf_tables_dump_rules_start(struct netlink_callback
*cb
)
2969 const struct nlattr
* const *nla
= cb
->data
;
2970 struct nft_rule_dump_ctx
*ctx
= NULL
;
2972 if (nla
[NFTA_RULE_TABLE
] || nla
[NFTA_RULE_CHAIN
]) {
2973 ctx
= kzalloc(sizeof(*ctx
), GFP_ATOMIC
);
2977 if (nla
[NFTA_RULE_TABLE
]) {
2978 ctx
->table
= nla_strdup(nla
[NFTA_RULE_TABLE
],
2985 if (nla
[NFTA_RULE_CHAIN
]) {
2986 ctx
->chain
= nla_strdup(nla
[NFTA_RULE_CHAIN
],
3000 static int nf_tables_dump_rules_done(struct netlink_callback
*cb
)
3002 struct nft_rule_dump_ctx
*ctx
= cb
->data
;
3012 /* called with rcu_read_lock held */
3013 static int nf_tables_getrule(struct net
*net
, struct sock
*nlsk
,
3014 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3015 const struct nlattr
* const nla
[],
3016 struct netlink_ext_ack
*extack
)
3018 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3019 u8 genmask
= nft_genmask_cur(net
);
3020 const struct nft_chain
*chain
;
3021 const struct nft_rule
*rule
;
3022 struct nft_table
*table
;
3023 struct sk_buff
*skb2
;
3024 int family
= nfmsg
->nfgen_family
;
3027 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3028 struct netlink_dump_control c
= {
3029 .start
= nf_tables_dump_rules_start
,
3030 .dump
= nf_tables_dump_rules
,
3031 .done
= nf_tables_dump_rules_done
,
3032 .module
= THIS_MODULE
,
3033 .data
= (void *)nla
,
3036 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
3039 table
= nft_table_lookup(net
, nla
[NFTA_RULE_TABLE
], family
, genmask
);
3040 if (IS_ERR(table
)) {
3041 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_TABLE
]);
3042 return PTR_ERR(table
);
3045 chain
= nft_chain_lookup(net
, table
, nla
[NFTA_RULE_CHAIN
], genmask
);
3046 if (IS_ERR(chain
)) {
3047 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_CHAIN
]);
3048 return PTR_ERR(chain
);
3051 rule
= nft_rule_lookup(chain
, nla
[NFTA_RULE_HANDLE
]);
3053 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_HANDLE
]);
3054 return PTR_ERR(rule
);
3057 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
3061 err
= nf_tables_fill_rule_info(skb2
, net
, NETLINK_CB(skb
).portid
,
3062 nlh
->nlmsg_seq
, NFT_MSG_NEWRULE
, 0,
3063 family
, table
, chain
, rule
, NULL
);
3065 goto err_fill_rule_info
;
3067 return nfnetlink_unicast(skb2
, net
, NETLINK_CB(skb
).portid
);
3074 static void nf_tables_rule_destroy(const struct nft_ctx
*ctx
,
3075 struct nft_rule
*rule
)
3077 struct nft_expr
*expr
, *next
;
3080 * Careful: some expressions might not be initialized in case this
3081 * is called on error from nf_tables_newrule().
3083 expr
= nft_expr_first(rule
);
3084 while (nft_expr_more(rule
, expr
)) {
3085 next
= nft_expr_next(expr
);
3086 nf_tables_expr_destroy(ctx
, expr
);
3092 void nf_tables_rule_release(const struct nft_ctx
*ctx
, struct nft_rule
*rule
)
3094 nft_rule_expr_deactivate(ctx
, rule
, NFT_TRANS_RELEASE
);
3095 nf_tables_rule_destroy(ctx
, rule
);
3098 int nft_chain_validate(const struct nft_ctx
*ctx
, const struct nft_chain
*chain
)
3100 struct nft_expr
*expr
, *last
;
3101 const struct nft_data
*data
;
3102 struct nft_rule
*rule
;
3105 if (ctx
->level
== NFT_JUMP_STACK_SIZE
)
3108 list_for_each_entry(rule
, &chain
->rules
, list
) {
3109 if (!nft_is_active_next(ctx
->net
, rule
))
3112 nft_rule_for_each_expr(expr
, last
, rule
) {
3113 if (!expr
->ops
->validate
)
3116 err
= expr
->ops
->validate(ctx
, expr
, &data
);
3124 EXPORT_SYMBOL_GPL(nft_chain_validate
);
3126 static int nft_table_validate(struct net
*net
, const struct nft_table
*table
)
3128 struct nft_chain
*chain
;
3129 struct nft_ctx ctx
= {
3131 .family
= table
->family
,
3135 list_for_each_entry(chain
, &table
->chains
, list
) {
3136 if (!nft_is_base_chain(chain
))
3140 err
= nft_chain_validate(&ctx
, chain
);
3148 static struct nft_rule
*nft_rule_lookup_byid(const struct net
*net
,
3149 const struct nlattr
*nla
);
3151 #define NFT_RULE_MAXEXPRS 128
3153 static int nf_tables_newrule(struct net
*net
, struct sock
*nlsk
,
3154 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3155 const struct nlattr
* const nla
[],
3156 struct netlink_ext_ack
*extack
)
3158 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3159 u8 genmask
= nft_genmask_next(net
);
3160 struct nft_expr_info
*info
= NULL
;
3161 int family
= nfmsg
->nfgen_family
;
3162 struct nft_flow_rule
*flow
;
3163 struct nft_table
*table
;
3164 struct nft_chain
*chain
;
3165 struct nft_rule
*rule
, *old_rule
= NULL
;
3166 struct nft_userdata
*udata
;
3167 struct nft_trans
*trans
= NULL
;
3168 struct nft_expr
*expr
;
3171 unsigned int size
, i
, n
, ulen
= 0, usize
= 0;
3173 u64 handle
, pos_handle
;
3175 lockdep_assert_held(&net
->nft
.commit_mutex
);
3177 table
= nft_table_lookup(net
, nla
[NFTA_RULE_TABLE
], family
, genmask
);
3178 if (IS_ERR(table
)) {
3179 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_TABLE
]);
3180 return PTR_ERR(table
);
3183 if (nla
[NFTA_RULE_CHAIN
]) {
3184 chain
= nft_chain_lookup(net
, table
, nla
[NFTA_RULE_CHAIN
],
3186 if (IS_ERR(chain
)) {
3187 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_CHAIN
]);
3188 return PTR_ERR(chain
);
3190 if (nft_chain_is_bound(chain
))
3193 } else if (nla
[NFTA_RULE_CHAIN_ID
]) {
3194 chain
= nft_chain_lookup_byid(net
, nla
[NFTA_RULE_CHAIN_ID
]);
3195 if (IS_ERR(chain
)) {
3196 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_CHAIN_ID
]);
3197 return PTR_ERR(chain
);
3203 if (nla
[NFTA_RULE_HANDLE
]) {
3204 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_HANDLE
]));
3205 rule
= __nft_rule_lookup(chain
, handle
);
3207 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_HANDLE
]);
3208 return PTR_ERR(rule
);
3211 if (nlh
->nlmsg_flags
& NLM_F_EXCL
) {
3212 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_HANDLE
]);
3215 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
3220 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
) ||
3221 nlh
->nlmsg_flags
& NLM_F_REPLACE
)
3223 handle
= nf_tables_alloc_handle(table
);
3225 if (chain
->use
== UINT_MAX
)
3228 if (nla
[NFTA_RULE_POSITION
]) {
3229 pos_handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_POSITION
]));
3230 old_rule
= __nft_rule_lookup(chain
, pos_handle
);
3231 if (IS_ERR(old_rule
)) {
3232 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_POSITION
]);
3233 return PTR_ERR(old_rule
);
3235 } else if (nla
[NFTA_RULE_POSITION_ID
]) {
3236 old_rule
= nft_rule_lookup_byid(net
, nla
[NFTA_RULE_POSITION_ID
]);
3237 if (IS_ERR(old_rule
)) {
3238 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_POSITION_ID
]);
3239 return PTR_ERR(old_rule
);
3244 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, chain
, nla
);
3248 if (nla
[NFTA_RULE_EXPRESSIONS
]) {
3249 info
= kvmalloc_array(NFT_RULE_MAXEXPRS
,
3250 sizeof(struct nft_expr_info
),
3255 nla_for_each_nested(tmp
, nla
[NFTA_RULE_EXPRESSIONS
], rem
) {
3257 if (nla_type(tmp
) != NFTA_LIST_ELEM
)
3259 if (n
== NFT_RULE_MAXEXPRS
)
3261 err
= nf_tables_expr_parse(&ctx
, tmp
, &info
[n
]);
3264 size
+= info
[n
].ops
->size
;
3268 /* Check for overflow of dlen field */
3270 if (size
>= 1 << 12)
3273 if (nla
[NFTA_RULE_USERDATA
]) {
3274 ulen
= nla_len(nla
[NFTA_RULE_USERDATA
]);
3276 usize
= sizeof(struct nft_userdata
) + ulen
;
3280 rule
= kzalloc(sizeof(*rule
) + size
+ usize
, GFP_KERNEL
);
3284 nft_activate_next(net
, rule
);
3286 rule
->handle
= handle
;
3288 rule
->udata
= ulen
? 1 : 0;
3291 udata
= nft_userdata(rule
);
3292 udata
->len
= ulen
- 1;
3293 nla_memcpy(udata
->data
, nla
[NFTA_RULE_USERDATA
], ulen
);
3296 expr
= nft_expr_first(rule
);
3297 for (i
= 0; i
< n
; i
++) {
3298 err
= nf_tables_newexpr(&ctx
, &info
[i
], expr
);
3300 NL_SET_BAD_ATTR(extack
, info
[i
].attr
);
3304 if (info
[i
].ops
->validate
)
3305 nft_validate_state_update(net
, NFT_VALIDATE_NEED
);
3308 expr
= nft_expr_next(expr
);
3311 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
3312 trans
= nft_trans_rule_add(&ctx
, NFT_MSG_NEWRULE
, rule
);
3313 if (trans
== NULL
) {
3317 err
= nft_delrule(&ctx
, old_rule
);
3319 nft_trans_destroy(trans
);
3323 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
3325 trans
= nft_trans_rule_add(&ctx
, NFT_MSG_NEWRULE
, rule
);
3331 if (nlh
->nlmsg_flags
& NLM_F_APPEND
) {
3333 list_add_rcu(&rule
->list
, &old_rule
->list
);
3335 list_add_tail_rcu(&rule
->list
, &chain
->rules
);
3338 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
3340 list_add_rcu(&rule
->list
, &chain
->rules
);
3346 if (net
->nft
.validate_state
== NFT_VALIDATE_DO
)
3347 return nft_table_validate(net
, table
);
3349 if (chain
->flags
& NFT_CHAIN_HW_OFFLOAD
) {
3350 flow
= nft_flow_rule_create(net
, rule
);
3352 return PTR_ERR(flow
);
3354 nft_trans_flow_rule(trans
) = flow
;
3359 nf_tables_rule_release(&ctx
, rule
);
3361 for (i
= 0; i
< n
; i
++) {
3363 module_put(info
[i
].ops
->type
->owner
);
3364 if (info
[i
].ops
->type
->release_ops
)
3365 info
[i
].ops
->type
->release_ops(info
[i
].ops
);
3372 static struct nft_rule
*nft_rule_lookup_byid(const struct net
*net
,
3373 const struct nlattr
*nla
)
3375 u32 id
= ntohl(nla_get_be32(nla
));
3376 struct nft_trans
*trans
;
3378 list_for_each_entry(trans
, &net
->nft
.commit_list
, list
) {
3379 struct nft_rule
*rule
= nft_trans_rule(trans
);
3381 if (trans
->msg_type
== NFT_MSG_NEWRULE
&&
3382 id
== nft_trans_rule_id(trans
))
3385 return ERR_PTR(-ENOENT
);
3388 static int nf_tables_delrule(struct net
*net
, struct sock
*nlsk
,
3389 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3390 const struct nlattr
* const nla
[],
3391 struct netlink_ext_ack
*extack
)
3393 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3394 u8 genmask
= nft_genmask_next(net
);
3395 struct nft_table
*table
;
3396 struct nft_chain
*chain
= NULL
;
3397 struct nft_rule
*rule
;
3398 int family
= nfmsg
->nfgen_family
, err
= 0;
3401 table
= nft_table_lookup(net
, nla
[NFTA_RULE_TABLE
], family
, genmask
);
3402 if (IS_ERR(table
)) {
3403 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_TABLE
]);
3404 return PTR_ERR(table
);
3407 if (nla
[NFTA_RULE_CHAIN
]) {
3408 chain
= nft_chain_lookup(net
, table
, nla
[NFTA_RULE_CHAIN
],
3410 if (IS_ERR(chain
)) {
3411 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_CHAIN
]);
3412 return PTR_ERR(chain
);
3414 if (nft_chain_is_bound(chain
))
3418 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, chain
, nla
);
3421 if (nla
[NFTA_RULE_HANDLE
]) {
3422 rule
= nft_rule_lookup(chain
, nla
[NFTA_RULE_HANDLE
]);
3424 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_HANDLE
]);
3425 return PTR_ERR(rule
);
3428 err
= nft_delrule(&ctx
, rule
);
3429 } else if (nla
[NFTA_RULE_ID
]) {
3430 rule
= nft_rule_lookup_byid(net
, nla
[NFTA_RULE_ID
]);
3432 NL_SET_BAD_ATTR(extack
, nla
[NFTA_RULE_ID
]);
3433 return PTR_ERR(rule
);
3436 err
= nft_delrule(&ctx
, rule
);
3438 err
= nft_delrule_by_chain(&ctx
);
3441 list_for_each_entry(chain
, &table
->chains
, list
) {
3442 if (!nft_is_active_next(net
, chain
))
3446 err
= nft_delrule_by_chain(&ctx
);
3458 static const struct nft_set_type
*nft_set_types
[] = {
3459 &nft_set_hash_fast_type
,
3461 &nft_set_rhash_type
,
3462 &nft_set_bitmap_type
,
3463 &nft_set_rbtree_type
,
3464 #if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
3465 &nft_set_pipapo_avx2_type
,
3467 &nft_set_pipapo_type
,
3470 #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \
3471 NFT_SET_TIMEOUT | NFT_SET_OBJECT | \
3474 static bool nft_set_ops_candidate(const struct nft_set_type
*type
, u32 flags
)
3476 return (flags
& type
->features
) == (flags
& NFT_SET_FEATURES
);
3480 * Select a set implementation based on the data characteristics and the
3481 * given policy. The total memory use might not be known if no size is
3482 * given, in that case the amount of memory per element is used.
3484 static const struct nft_set_ops
*
3485 nft_select_set_ops(const struct nft_ctx
*ctx
,
3486 const struct nlattr
* const nla
[],
3487 const struct nft_set_desc
*desc
,
3488 enum nft_set_policies policy
)
3490 const struct nft_set_ops
*ops
, *bops
;
3491 struct nft_set_estimate est
, best
;
3492 const struct nft_set_type
*type
;
3496 lockdep_assert_held(&ctx
->net
->nft
.commit_mutex
);
3497 lockdep_nfnl_nft_mutex_not_held();
3499 if (nla
[NFTA_SET_FLAGS
] != NULL
)
3500 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
3507 for (i
= 0; i
< ARRAY_SIZE(nft_set_types
); i
++) {
3508 type
= nft_set_types
[i
];
3511 if (!nft_set_ops_candidate(type
, flags
))
3513 if (!ops
->estimate(desc
, flags
, &est
))
3517 case NFT_SET_POL_PERFORMANCE
:
3518 if (est
.lookup
< best
.lookup
)
3520 if (est
.lookup
== best
.lookup
&&
3521 est
.space
< best
.space
)
3524 case NFT_SET_POL_MEMORY
:
3526 if (est
.space
< best
.space
)
3528 if (est
.space
== best
.space
&&
3529 est
.lookup
< best
.lookup
)
3531 } else if (est
.size
< best
.size
|| !bops
) {
3546 return ERR_PTR(-EOPNOTSUPP
);
3549 static const struct nla_policy nft_set_policy
[NFTA_SET_MAX
+ 1] = {
3550 [NFTA_SET_TABLE
] = { .type
= NLA_STRING
,
3551 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
3552 [NFTA_SET_NAME
] = { .type
= NLA_STRING
,
3553 .len
= NFT_SET_MAXNAMELEN
- 1 },
3554 [NFTA_SET_FLAGS
] = { .type
= NLA_U32
},
3555 [NFTA_SET_KEY_TYPE
] = { .type
= NLA_U32
},
3556 [NFTA_SET_KEY_LEN
] = { .type
= NLA_U32
},
3557 [NFTA_SET_DATA_TYPE
] = { .type
= NLA_U32
},
3558 [NFTA_SET_DATA_LEN
] = { .type
= NLA_U32
},
3559 [NFTA_SET_POLICY
] = { .type
= NLA_U32
},
3560 [NFTA_SET_DESC
] = { .type
= NLA_NESTED
},
3561 [NFTA_SET_ID
] = { .type
= NLA_U32
},
3562 [NFTA_SET_TIMEOUT
] = { .type
= NLA_U64
},
3563 [NFTA_SET_GC_INTERVAL
] = { .type
= NLA_U32
},
3564 [NFTA_SET_USERDATA
] = { .type
= NLA_BINARY
,
3565 .len
= NFT_USERDATA_MAXLEN
},
3566 [NFTA_SET_OBJ_TYPE
] = { .type
= NLA_U32
},
3567 [NFTA_SET_HANDLE
] = { .type
= NLA_U64
},
3568 [NFTA_SET_EXPR
] = { .type
= NLA_NESTED
},
3571 static const struct nla_policy nft_set_desc_policy
[NFTA_SET_DESC_MAX
+ 1] = {
3572 [NFTA_SET_DESC_SIZE
] = { .type
= NLA_U32
},
3573 [NFTA_SET_DESC_CONCAT
] = { .type
= NLA_NESTED
},
3576 static int nft_ctx_init_from_setattr(struct nft_ctx
*ctx
, struct net
*net
,
3577 const struct sk_buff
*skb
,
3578 const struct nlmsghdr
*nlh
,
3579 const struct nlattr
* const nla
[],
3580 struct netlink_ext_ack
*extack
,
3583 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3584 int family
= nfmsg
->nfgen_family
;
3585 struct nft_table
*table
= NULL
;
3587 if (nla
[NFTA_SET_TABLE
] != NULL
) {
3588 table
= nft_table_lookup(net
, nla
[NFTA_SET_TABLE
], family
,
3590 if (IS_ERR(table
)) {
3591 NL_SET_BAD_ATTR(extack
, nla
[NFTA_SET_TABLE
]);
3592 return PTR_ERR(table
);
3596 nft_ctx_init(ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
3600 static struct nft_set
*nft_set_lookup(const struct nft_table
*table
,
3601 const struct nlattr
*nla
, u8 genmask
)
3603 struct nft_set
*set
;
3606 return ERR_PTR(-EINVAL
);
3608 list_for_each_entry_rcu(set
, &table
->sets
, list
) {
3609 if (!nla_strcmp(nla
, set
->name
) &&
3610 nft_active_genmask(set
, genmask
))
3613 return ERR_PTR(-ENOENT
);
3616 static struct nft_set
*nft_set_lookup_byhandle(const struct nft_table
*table
,
3617 const struct nlattr
*nla
,
3620 struct nft_set
*set
;
3622 list_for_each_entry(set
, &table
->sets
, list
) {
3623 if (be64_to_cpu(nla_get_be64(nla
)) == set
->handle
&&
3624 nft_active_genmask(set
, genmask
))
3627 return ERR_PTR(-ENOENT
);
3630 static struct nft_set
*nft_set_lookup_byid(const struct net
*net
,
3631 const struct nlattr
*nla
, u8 genmask
)
3633 struct nft_trans
*trans
;
3634 u32 id
= ntohl(nla_get_be32(nla
));
3636 list_for_each_entry(trans
, &net
->nft
.commit_list
, list
) {
3637 if (trans
->msg_type
== NFT_MSG_NEWSET
) {
3638 struct nft_set
*set
= nft_trans_set(trans
);
3640 if (id
== nft_trans_set_id(trans
) &&
3641 nft_active_genmask(set
, genmask
))
3645 return ERR_PTR(-ENOENT
);
3648 struct nft_set
*nft_set_lookup_global(const struct net
*net
,
3649 const struct nft_table
*table
,
3650 const struct nlattr
*nla_set_name
,
3651 const struct nlattr
*nla_set_id
,
3654 struct nft_set
*set
;
3656 set
= nft_set_lookup(table
, nla_set_name
, genmask
);
3661 set
= nft_set_lookup_byid(net
, nla_set_id
, genmask
);
3665 EXPORT_SYMBOL_GPL(nft_set_lookup_global
);
3667 static int nf_tables_set_alloc_name(struct nft_ctx
*ctx
, struct nft_set
*set
,
3670 const struct nft_set
*i
;
3672 unsigned long *inuse
;
3673 unsigned int n
= 0, min
= 0;
3675 p
= strchr(name
, '%');
3677 if (p
[1] != 'd' || strchr(p
+ 2, '%'))
3680 inuse
= (unsigned long *)get_zeroed_page(GFP_KERNEL
);
3684 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
3687 if (!nft_is_active_next(ctx
->net
, set
))
3689 if (!sscanf(i
->name
, name
, &tmp
))
3691 if (tmp
< min
|| tmp
>= min
+ BITS_PER_BYTE
* PAGE_SIZE
)
3694 set_bit(tmp
- min
, inuse
);
3697 n
= find_first_zero_bit(inuse
, BITS_PER_BYTE
* PAGE_SIZE
);
3698 if (n
>= BITS_PER_BYTE
* PAGE_SIZE
) {
3699 min
+= BITS_PER_BYTE
* PAGE_SIZE
;
3700 memset(inuse
, 0, PAGE_SIZE
);
3703 free_page((unsigned long)inuse
);
3706 set
->name
= kasprintf(GFP_KERNEL
, name
, min
+ n
);
3710 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
3711 if (!nft_is_active_next(ctx
->net
, i
))
3713 if (!strcmp(set
->name
, i
->name
)) {
3722 static int nf_msecs_to_jiffies64(const struct nlattr
*nla
, u64
*result
)
3724 u64 ms
= be64_to_cpu(nla_get_be64(nla
));
3725 u64 max
= (u64
)(~((u64
)0));
3727 max
= div_u64(max
, NSEC_PER_MSEC
);
3731 ms
*= NSEC_PER_MSEC
;
3732 *result
= nsecs_to_jiffies64(ms
);
3736 static __be64
nf_jiffies64_to_msecs(u64 input
)
3738 return cpu_to_be64(jiffies64_to_msecs(input
));
3741 static int nf_tables_fill_set_concat(struct sk_buff
*skb
,
3742 const struct nft_set
*set
)
3744 struct nlattr
*concat
, *field
;
3747 concat
= nla_nest_start_noflag(skb
, NFTA_SET_DESC_CONCAT
);
3751 for (i
= 0; i
< set
->field_count
; i
++) {
3752 field
= nla_nest_start_noflag(skb
, NFTA_LIST_ELEM
);
3756 if (nla_put_be32(skb
, NFTA_SET_FIELD_LEN
,
3757 htonl(set
->field_len
[i
])))
3760 nla_nest_end(skb
, field
);
3763 nla_nest_end(skb
, concat
);
3768 static int nf_tables_fill_set(struct sk_buff
*skb
, const struct nft_ctx
*ctx
,
3769 const struct nft_set
*set
, u16 event
, u16 flags
)
3771 struct nfgenmsg
*nfmsg
;
3772 struct nlmsghdr
*nlh
;
3773 u32 portid
= ctx
->portid
;
3774 struct nlattr
*nest
;
3777 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
3778 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
3781 goto nla_put_failure
;
3783 nfmsg
= nlmsg_data(nlh
);
3784 nfmsg
->nfgen_family
= ctx
->family
;
3785 nfmsg
->version
= NFNETLINK_V0
;
3786 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
3788 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
3789 goto nla_put_failure
;
3790 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
3791 goto nla_put_failure
;
3792 if (nla_put_be64(skb
, NFTA_SET_HANDLE
, cpu_to_be64(set
->handle
),
3794 goto nla_put_failure
;
3795 if (set
->flags
!= 0)
3796 if (nla_put_be32(skb
, NFTA_SET_FLAGS
, htonl(set
->flags
)))
3797 goto nla_put_failure
;
3799 if (nla_put_be32(skb
, NFTA_SET_KEY_TYPE
, htonl(set
->ktype
)))
3800 goto nla_put_failure
;
3801 if (nla_put_be32(skb
, NFTA_SET_KEY_LEN
, htonl(set
->klen
)))
3802 goto nla_put_failure
;
3803 if (set
->flags
& NFT_SET_MAP
) {
3804 if (nla_put_be32(skb
, NFTA_SET_DATA_TYPE
, htonl(set
->dtype
)))
3805 goto nla_put_failure
;
3806 if (nla_put_be32(skb
, NFTA_SET_DATA_LEN
, htonl(set
->dlen
)))
3807 goto nla_put_failure
;
3809 if (set
->flags
& NFT_SET_OBJECT
&&
3810 nla_put_be32(skb
, NFTA_SET_OBJ_TYPE
, htonl(set
->objtype
)))
3811 goto nla_put_failure
;
3814 nla_put_be64(skb
, NFTA_SET_TIMEOUT
,
3815 nf_jiffies64_to_msecs(set
->timeout
),
3817 goto nla_put_failure
;
3819 nla_put_be32(skb
, NFTA_SET_GC_INTERVAL
, htonl(set
->gc_int
)))
3820 goto nla_put_failure
;
3822 if (set
->policy
!= NFT_SET_POL_PERFORMANCE
) {
3823 if (nla_put_be32(skb
, NFTA_SET_POLICY
, htonl(set
->policy
)))
3824 goto nla_put_failure
;
3828 nla_put(skb
, NFTA_SET_USERDATA
, set
->udlen
, set
->udata
))
3829 goto nla_put_failure
;
3831 nest
= nla_nest_start_noflag(skb
, NFTA_SET_DESC
);
3833 goto nla_put_failure
;
3835 nla_put_be32(skb
, NFTA_SET_DESC_SIZE
, htonl(set
->size
)))
3836 goto nla_put_failure
;
3838 if (set
->field_count
> 1 &&
3839 nf_tables_fill_set_concat(skb
, set
))
3840 goto nla_put_failure
;
3842 nla_nest_end(skb
, nest
);
3845 nest
= nla_nest_start_noflag(skb
, NFTA_SET_EXPR
);
3846 if (nf_tables_fill_expr_info(skb
, set
->expr
) < 0)
3847 goto nla_put_failure
;
3849 nla_nest_end(skb
, nest
);
3852 nlmsg_end(skb
, nlh
);
3856 nlmsg_trim(skb
, nlh
);
3860 static void nf_tables_set_notify(const struct nft_ctx
*ctx
,
3861 const struct nft_set
*set
, int event
,
3864 struct sk_buff
*skb
;
3865 u32 portid
= ctx
->portid
;
3867 char *buf
= kasprintf(gfp_flags
, "%s:%llu;%s:%llu",
3868 ctx
->table
->name
, ctx
->table
->handle
,
3869 set
->name
, set
->handle
);
3871 audit_log_nfcfg(buf
,
3874 event
== NFT_MSG_NEWSET
?
3875 AUDIT_NFT_OP_SET_REGISTER
:
3876 AUDIT_NFT_OP_SET_UNREGISTER
,
3881 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
3884 skb
= nlmsg_new(NLMSG_GOODSIZE
, gfp_flags
);
3888 err
= nf_tables_fill_set(skb
, ctx
, set
, event
, 0);
3894 nft_notify_enqueue(skb
, ctx
->report
, &ctx
->net
->nft
.notify_list
);
3897 nfnetlink_set_err(ctx
->net
, portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
3900 static int nf_tables_dump_sets(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3902 const struct nft_set
*set
;
3903 unsigned int idx
, s_idx
= cb
->args
[0];
3904 struct nft_table
*table
, *cur_table
= (struct nft_table
*)cb
->args
[2];
3905 struct net
*net
= sock_net(skb
->sk
);
3906 struct nft_ctx
*ctx
= cb
->data
, ctx_set
;
3912 cb
->seq
= net
->nft
.base_seq
;
3914 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
3915 if (ctx
->family
!= NFPROTO_UNSPEC
&&
3916 ctx
->family
!= table
->family
)
3919 if (ctx
->table
&& ctx
->table
!= table
)
3923 if (cur_table
!= table
)
3929 list_for_each_entry_rcu(set
, &table
->sets
, list
) {
3932 if (!nft_is_active(net
, set
))
3936 ctx_set
.table
= table
;
3937 ctx_set
.family
= table
->family
;
3939 if (nf_tables_fill_set(skb
, &ctx_set
, set
,
3943 cb
->args
[2] = (unsigned long) table
;
3946 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
3959 static int nf_tables_dump_sets_start(struct netlink_callback
*cb
)
3961 struct nft_ctx
*ctx_dump
= NULL
;
3963 ctx_dump
= kmemdup(cb
->data
, sizeof(*ctx_dump
), GFP_ATOMIC
);
3964 if (ctx_dump
== NULL
)
3967 cb
->data
= ctx_dump
;
3971 static int nf_tables_dump_sets_done(struct netlink_callback
*cb
)
3977 /* called with rcu_read_lock held */
3978 static int nf_tables_getset(struct net
*net
, struct sock
*nlsk
,
3979 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3980 const struct nlattr
* const nla
[],
3981 struct netlink_ext_ack
*extack
)
3983 u8 genmask
= nft_genmask_cur(net
);
3984 const struct nft_set
*set
;
3986 struct sk_buff
*skb2
;
3987 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3990 /* Verify existence before starting dump */
3991 err
= nft_ctx_init_from_setattr(&ctx
, net
, skb
, nlh
, nla
, extack
,
3996 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3997 struct netlink_dump_control c
= {
3998 .start
= nf_tables_dump_sets_start
,
3999 .dump
= nf_tables_dump_sets
,
4000 .done
= nf_tables_dump_sets_done
,
4002 .module
= THIS_MODULE
,
4005 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
4008 /* Only accept unspec with dump */
4009 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
4010 return -EAFNOSUPPORT
;
4011 if (!nla
[NFTA_SET_TABLE
])
4014 set
= nft_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
], genmask
);
4016 return PTR_ERR(set
);
4018 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
4022 err
= nf_tables_fill_set(skb2
, &ctx
, set
, NFT_MSG_NEWSET
, 0);
4024 goto err_fill_set_info
;
4026 return nfnetlink_unicast(skb2
, net
, NETLINK_CB(skb
).portid
);
4033 static const struct nla_policy nft_concat_policy
[NFTA_SET_FIELD_MAX
+ 1] = {
4034 [NFTA_SET_FIELD_LEN
] = { .type
= NLA_U32
},
4037 static int nft_set_desc_concat_parse(const struct nlattr
*attr
,
4038 struct nft_set_desc
*desc
)
4040 struct nlattr
*tb
[NFTA_SET_FIELD_MAX
+ 1];
4044 err
= nla_parse_nested_deprecated(tb
, NFTA_SET_FIELD_MAX
, attr
,
4045 nft_concat_policy
, NULL
);
4049 if (!tb
[NFTA_SET_FIELD_LEN
])
4052 len
= ntohl(nla_get_be32(tb
[NFTA_SET_FIELD_LEN
]));
4054 if (len
* BITS_PER_BYTE
/ 32 > NFT_REG32_COUNT
)
4057 desc
->field_len
[desc
->field_count
++] = len
;
4062 static int nft_set_desc_concat(struct nft_set_desc
*desc
,
4063 const struct nlattr
*nla
)
4065 struct nlattr
*attr
;
4068 nla_for_each_nested(attr
, nla
, rem
) {
4069 if (nla_type(attr
) != NFTA_LIST_ELEM
)
4072 err
= nft_set_desc_concat_parse(attr
, desc
);
4080 static int nf_tables_set_desc_parse(struct nft_set_desc
*desc
,
4081 const struct nlattr
*nla
)
4083 struct nlattr
*da
[NFTA_SET_DESC_MAX
+ 1];
4086 err
= nla_parse_nested_deprecated(da
, NFTA_SET_DESC_MAX
, nla
,
4087 nft_set_desc_policy
, NULL
);
4091 if (da
[NFTA_SET_DESC_SIZE
] != NULL
)
4092 desc
->size
= ntohl(nla_get_be32(da
[NFTA_SET_DESC_SIZE
]));
4093 if (da
[NFTA_SET_DESC_CONCAT
])
4094 err
= nft_set_desc_concat(desc
, da
[NFTA_SET_DESC_CONCAT
]);
4099 static int nf_tables_newset(struct net
*net
, struct sock
*nlsk
,
4100 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
4101 const struct nlattr
* const nla
[],
4102 struct netlink_ext_ack
*extack
)
4104 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
4105 u8 genmask
= nft_genmask_next(net
);
4106 int family
= nfmsg
->nfgen_family
;
4107 const struct nft_set_ops
*ops
;
4108 struct nft_expr
*expr
= NULL
;
4109 struct nft_table
*table
;
4110 struct nft_set
*set
;
4115 u32 ktype
, dtype
, flags
, policy
, gc_int
, objtype
;
4116 struct nft_set_desc desc
;
4117 unsigned char *udata
;
4122 if (nla
[NFTA_SET_TABLE
] == NULL
||
4123 nla
[NFTA_SET_NAME
] == NULL
||
4124 nla
[NFTA_SET_KEY_LEN
] == NULL
||
4125 nla
[NFTA_SET_ID
] == NULL
)
4128 memset(&desc
, 0, sizeof(desc
));
4130 ktype
= NFT_DATA_VALUE
;
4131 if (nla
[NFTA_SET_KEY_TYPE
] != NULL
) {
4132 ktype
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_TYPE
]));
4133 if ((ktype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
)
4137 desc
.klen
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_LEN
]));
4138 if (desc
.klen
== 0 || desc
.klen
> NFT_DATA_VALUE_MAXLEN
)
4142 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
4143 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
4144 if (flags
& ~(NFT_SET_ANONYMOUS
| NFT_SET_CONSTANT
|
4145 NFT_SET_INTERVAL
| NFT_SET_TIMEOUT
|
4146 NFT_SET_MAP
| NFT_SET_EVAL
|
4147 NFT_SET_OBJECT
| NFT_SET_CONCAT
))
4149 /* Only one of these operations is supported */
4150 if ((flags
& (NFT_SET_MAP
| NFT_SET_OBJECT
)) ==
4151 (NFT_SET_MAP
| NFT_SET_OBJECT
))
4153 if ((flags
& (NFT_SET_EVAL
| NFT_SET_OBJECT
)) ==
4154 (NFT_SET_EVAL
| NFT_SET_OBJECT
))
4159 if (nla
[NFTA_SET_DATA_TYPE
] != NULL
) {
4160 if (!(flags
& NFT_SET_MAP
))
4163 dtype
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_TYPE
]));
4164 if ((dtype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
&&
4165 dtype
!= NFT_DATA_VERDICT
)
4168 if (dtype
!= NFT_DATA_VERDICT
) {
4169 if (nla
[NFTA_SET_DATA_LEN
] == NULL
)
4171 desc
.dlen
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_LEN
]));
4172 if (desc
.dlen
== 0 || desc
.dlen
> NFT_DATA_VALUE_MAXLEN
)
4175 desc
.dlen
= sizeof(struct nft_verdict
);
4176 } else if (flags
& NFT_SET_MAP
)
4179 if (nla
[NFTA_SET_OBJ_TYPE
] != NULL
) {
4180 if (!(flags
& NFT_SET_OBJECT
))
4183 objtype
= ntohl(nla_get_be32(nla
[NFTA_SET_OBJ_TYPE
]));
4184 if (objtype
== NFT_OBJECT_UNSPEC
||
4185 objtype
> NFT_OBJECT_MAX
)
4187 } else if (flags
& NFT_SET_OBJECT
)
4190 objtype
= NFT_OBJECT_UNSPEC
;
4193 if (nla
[NFTA_SET_TIMEOUT
] != NULL
) {
4194 if (!(flags
& NFT_SET_TIMEOUT
))
4197 err
= nf_msecs_to_jiffies64(nla
[NFTA_SET_TIMEOUT
], &timeout
);
4202 if (nla
[NFTA_SET_GC_INTERVAL
] != NULL
) {
4203 if (!(flags
& NFT_SET_TIMEOUT
))
4205 gc_int
= ntohl(nla_get_be32(nla
[NFTA_SET_GC_INTERVAL
]));
4208 policy
= NFT_SET_POL_PERFORMANCE
;
4209 if (nla
[NFTA_SET_POLICY
] != NULL
)
4210 policy
= ntohl(nla_get_be32(nla
[NFTA_SET_POLICY
]));
4212 if (nla
[NFTA_SET_DESC
] != NULL
) {
4213 err
= nf_tables_set_desc_parse(&desc
, nla
[NFTA_SET_DESC
]);
4218 if (nla
[NFTA_SET_EXPR
])
4221 table
= nft_table_lookup(net
, nla
[NFTA_SET_TABLE
], family
, genmask
);
4222 if (IS_ERR(table
)) {
4223 NL_SET_BAD_ATTR(extack
, nla
[NFTA_SET_TABLE
]);
4224 return PTR_ERR(table
);
4227 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
4229 set
= nft_set_lookup(table
, nla
[NFTA_SET_NAME
], genmask
);
4231 if (PTR_ERR(set
) != -ENOENT
) {
4232 NL_SET_BAD_ATTR(extack
, nla
[NFTA_SET_NAME
]);
4233 return PTR_ERR(set
);
4236 if (nlh
->nlmsg_flags
& NLM_F_EXCL
) {
4237 NL_SET_BAD_ATTR(extack
, nla
[NFTA_SET_NAME
]);
4240 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
4246 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
4249 ops
= nft_select_set_ops(&ctx
, nla
, &desc
, policy
);
4251 return PTR_ERR(ops
);
4254 if (nla
[NFTA_SET_USERDATA
])
4255 udlen
= nla_len(nla
[NFTA_SET_USERDATA
]);
4258 if (ops
->privsize
!= NULL
)
4259 size
= ops
->privsize(nla
, &desc
);
4261 set
= kvzalloc(sizeof(*set
) + size
+ udlen
, GFP_KERNEL
);
4265 name
= nla_strdup(nla
[NFTA_SET_NAME
], GFP_KERNEL
);
4271 err
= nf_tables_set_alloc_name(&ctx
, set
, name
);
4274 goto err_set_alloc_name
;
4276 if (nla
[NFTA_SET_EXPR
]) {
4277 expr
= nft_set_elem_expr_alloc(&ctx
, set
, nla
[NFTA_SET_EXPR
]);
4279 err
= PTR_ERR(expr
);
4280 goto err_set_alloc_name
;
4286 udata
= set
->data
+ size
;
4287 nla_memcpy(udata
, nla
[NFTA_SET_USERDATA
], udlen
);
4290 INIT_LIST_HEAD(&set
->bindings
);
4292 write_pnet(&set
->net
, net
);
4295 set
->klen
= desc
.klen
;
4297 set
->objtype
= objtype
;
4298 set
->dlen
= desc
.dlen
;
4301 set
->size
= desc
.size
;
4302 set
->policy
= policy
;
4305 set
->timeout
= timeout
;
4306 set
->gc_int
= gc_int
;
4307 set
->handle
= nf_tables_alloc_handle(table
);
4309 set
->field_count
= desc
.field_count
;
4310 for (i
= 0; i
< desc
.field_count
; i
++)
4311 set
->field_len
[i
] = desc
.field_len
[i
];
4313 err
= ops
->init(set
, &desc
, nla
);
4317 err
= nft_trans_set_add(&ctx
, NFT_MSG_NEWSET
, set
);
4321 list_add_tail_rcu(&set
->list
, &table
->sets
);
4329 nft_expr_destroy(&ctx
, expr
);
4337 static void nft_set_destroy(const struct nft_ctx
*ctx
, struct nft_set
*set
)
4339 if (WARN_ON(set
->use
> 0))
4343 nft_expr_destroy(ctx
, set
->expr
);
4345 set
->ops
->destroy(set
);
4350 static int nf_tables_delset(struct net
*net
, struct sock
*nlsk
,
4351 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
4352 const struct nlattr
* const nla
[],
4353 struct netlink_ext_ack
*extack
)
4355 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
4356 u8 genmask
= nft_genmask_next(net
);
4357 const struct nlattr
*attr
;
4358 struct nft_set
*set
;
4362 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
4363 return -EAFNOSUPPORT
;
4364 if (nla
[NFTA_SET_TABLE
] == NULL
)
4367 err
= nft_ctx_init_from_setattr(&ctx
, net
, skb
, nlh
, nla
, extack
,
4372 if (nla
[NFTA_SET_HANDLE
]) {
4373 attr
= nla
[NFTA_SET_HANDLE
];
4374 set
= nft_set_lookup_byhandle(ctx
.table
, attr
, genmask
);
4376 attr
= nla
[NFTA_SET_NAME
];
4377 set
= nft_set_lookup(ctx
.table
, attr
, genmask
);
4381 NL_SET_BAD_ATTR(extack
, attr
);
4382 return PTR_ERR(set
);
4385 (nlh
->nlmsg_flags
& NLM_F_NONREC
&& atomic_read(&set
->nelems
) > 0)) {
4386 NL_SET_BAD_ATTR(extack
, attr
);
4390 return nft_delset(&ctx
, set
);
4393 static int nf_tables_bind_check_setelem(const struct nft_ctx
*ctx
,
4394 struct nft_set
*set
,
4395 const struct nft_set_iter
*iter
,
4396 struct nft_set_elem
*elem
)
4398 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
4399 enum nft_registers dreg
;
4401 dreg
= nft_type_to_reg(set
->dtype
);
4402 return nft_validate_register_store(ctx
, dreg
, nft_set_ext_data(ext
),
4403 set
->dtype
== NFT_DATA_VERDICT
?
4404 NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
4408 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
4409 struct nft_set_binding
*binding
)
4411 struct nft_set_binding
*i
;
4412 struct nft_set_iter iter
;
4414 if (set
->use
== UINT_MAX
)
4417 if (!list_empty(&set
->bindings
) && nft_set_is_anonymous(set
))
4420 if (binding
->flags
& NFT_SET_MAP
) {
4421 /* If the set is already bound to the same chain all
4422 * jumps are already validated for that chain.
4424 list_for_each_entry(i
, &set
->bindings
, list
) {
4425 if (i
->flags
& NFT_SET_MAP
&&
4426 i
->chain
== binding
->chain
)
4430 iter
.genmask
= nft_genmask_next(ctx
->net
);
4434 iter
.fn
= nf_tables_bind_check_setelem
;
4436 set
->ops
->walk(ctx
, set
, &iter
);
4441 binding
->chain
= ctx
->chain
;
4442 list_add_tail_rcu(&binding
->list
, &set
->bindings
);
4443 nft_set_trans_bind(ctx
, set
);
4448 EXPORT_SYMBOL_GPL(nf_tables_bind_set
);
4450 static void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
4451 struct nft_set_binding
*binding
, bool event
)
4453 list_del_rcu(&binding
->list
);
4455 if (list_empty(&set
->bindings
) && nft_set_is_anonymous(set
)) {
4456 list_del_rcu(&set
->list
);
4458 nf_tables_set_notify(ctx
, set
, NFT_MSG_DELSET
,
4463 void nf_tables_deactivate_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
4464 struct nft_set_binding
*binding
,
4465 enum nft_trans_phase phase
)
4468 case NFT_TRANS_PREPARE
:
4471 case NFT_TRANS_ABORT
:
4472 case NFT_TRANS_RELEASE
:
4476 nf_tables_unbind_set(ctx
, set
, binding
,
4477 phase
== NFT_TRANS_COMMIT
);
4480 EXPORT_SYMBOL_GPL(nf_tables_deactivate_set
);
4482 void nf_tables_destroy_set(const struct nft_ctx
*ctx
, struct nft_set
*set
)
4484 if (list_empty(&set
->bindings
) && nft_set_is_anonymous(set
))
4485 nft_set_destroy(ctx
, set
);
4487 EXPORT_SYMBOL_GPL(nf_tables_destroy_set
);
4489 const struct nft_set_ext_type nft_set_ext_types
[] = {
4490 [NFT_SET_EXT_KEY
] = {
4491 .align
= __alignof__(u32
),
4493 [NFT_SET_EXT_DATA
] = {
4494 .align
= __alignof__(u32
),
4496 [NFT_SET_EXT_EXPR
] = {
4497 .align
= __alignof__(struct nft_expr
),
4499 [NFT_SET_EXT_OBJREF
] = {
4500 .len
= sizeof(struct nft_object
*),
4501 .align
= __alignof__(struct nft_object
*),
4503 [NFT_SET_EXT_FLAGS
] = {
4505 .align
= __alignof__(u8
),
4507 [NFT_SET_EXT_TIMEOUT
] = {
4509 .align
= __alignof__(u64
),
4511 [NFT_SET_EXT_EXPIRATION
] = {
4513 .align
= __alignof__(u64
),
4515 [NFT_SET_EXT_USERDATA
] = {
4516 .len
= sizeof(struct nft_userdata
),
4517 .align
= __alignof__(struct nft_userdata
),
4519 [NFT_SET_EXT_KEY_END
] = {
4520 .align
= __alignof__(u32
),
4528 static const struct nla_policy nft_set_elem_policy
[NFTA_SET_ELEM_MAX
+ 1] = {
4529 [NFTA_SET_ELEM_KEY
] = { .type
= NLA_NESTED
},
4530 [NFTA_SET_ELEM_DATA
] = { .type
= NLA_NESTED
},
4531 [NFTA_SET_ELEM_FLAGS
] = { .type
= NLA_U32
},
4532 [NFTA_SET_ELEM_TIMEOUT
] = { .type
= NLA_U64
},
4533 [NFTA_SET_ELEM_EXPIRATION
] = { .type
= NLA_U64
},
4534 [NFTA_SET_ELEM_USERDATA
] = { .type
= NLA_BINARY
,
4535 .len
= NFT_USERDATA_MAXLEN
},
4536 [NFTA_SET_ELEM_EXPR
] = { .type
= NLA_NESTED
},
4537 [NFTA_SET_ELEM_OBJREF
] = { .type
= NLA_STRING
,
4538 .len
= NFT_OBJ_MAXNAMELEN
- 1 },
4539 [NFTA_SET_ELEM_KEY_END
] = { .type
= NLA_NESTED
},
4542 static const struct nla_policy nft_set_elem_list_policy
[NFTA_SET_ELEM_LIST_MAX
+ 1] = {
4543 [NFTA_SET_ELEM_LIST_TABLE
] = { .type
= NLA_STRING
,
4544 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
4545 [NFTA_SET_ELEM_LIST_SET
] = { .type
= NLA_STRING
,
4546 .len
= NFT_SET_MAXNAMELEN
- 1 },
4547 [NFTA_SET_ELEM_LIST_ELEMENTS
] = { .type
= NLA_NESTED
},
4548 [NFTA_SET_ELEM_LIST_SET_ID
] = { .type
= NLA_U32
},
4551 static int nft_ctx_init_from_elemattr(struct nft_ctx
*ctx
, struct net
*net
,
4552 const struct sk_buff
*skb
,
4553 const struct nlmsghdr
*nlh
,
4554 const struct nlattr
* const nla
[],
4555 struct netlink_ext_ack
*extack
,
4558 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
4559 int family
= nfmsg
->nfgen_family
;
4560 struct nft_table
*table
;
4562 table
= nft_table_lookup(net
, nla
[NFTA_SET_ELEM_LIST_TABLE
], family
,
4564 if (IS_ERR(table
)) {
4565 NL_SET_BAD_ATTR(extack
, nla
[NFTA_SET_ELEM_LIST_TABLE
]);
4566 return PTR_ERR(table
);
4569 nft_ctx_init(ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
4573 static int nf_tables_fill_setelem(struct sk_buff
*skb
,
4574 const struct nft_set
*set
,
4575 const struct nft_set_elem
*elem
)
4577 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
4578 unsigned char *b
= skb_tail_pointer(skb
);
4579 struct nlattr
*nest
;
4581 nest
= nla_nest_start_noflag(skb
, NFTA_LIST_ELEM
);
4583 goto nla_put_failure
;
4585 if (nft_data_dump(skb
, NFTA_SET_ELEM_KEY
, nft_set_ext_key(ext
),
4586 NFT_DATA_VALUE
, set
->klen
) < 0)
4587 goto nla_put_failure
;
4589 if (nft_set_ext_exists(ext
, NFT_SET_EXT_KEY_END
) &&
4590 nft_data_dump(skb
, NFTA_SET_ELEM_KEY_END
, nft_set_ext_key_end(ext
),
4591 NFT_DATA_VALUE
, set
->klen
) < 0)
4592 goto nla_put_failure
;
4594 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
) &&
4595 nft_data_dump(skb
, NFTA_SET_ELEM_DATA
, nft_set_ext_data(ext
),
4596 set
->dtype
== NFT_DATA_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
4598 goto nla_put_failure
;
4600 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
) &&
4601 nft_expr_dump(skb
, NFTA_SET_ELEM_EXPR
, nft_set_ext_expr(ext
)) < 0)
4602 goto nla_put_failure
;
4604 if (nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
) &&
4605 nla_put_string(skb
, NFTA_SET_ELEM_OBJREF
,
4606 (*nft_set_ext_obj(ext
))->key
.name
) < 0)
4607 goto nla_put_failure
;
4609 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
4610 nla_put_be32(skb
, NFTA_SET_ELEM_FLAGS
,
4611 htonl(*nft_set_ext_flags(ext
))))
4612 goto nla_put_failure
;
4614 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
) &&
4615 nla_put_be64(skb
, NFTA_SET_ELEM_TIMEOUT
,
4616 nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext
)),
4618 goto nla_put_failure
;
4620 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
)) {
4621 u64 expires
, now
= get_jiffies_64();
4623 expires
= *nft_set_ext_expiration(ext
);
4624 if (time_before64(now
, expires
))
4629 if (nla_put_be64(skb
, NFTA_SET_ELEM_EXPIRATION
,
4630 nf_jiffies64_to_msecs(expires
),
4632 goto nla_put_failure
;
4635 if (nft_set_ext_exists(ext
, NFT_SET_EXT_USERDATA
)) {
4636 struct nft_userdata
*udata
;
4638 udata
= nft_set_ext_userdata(ext
);
4639 if (nla_put(skb
, NFTA_SET_ELEM_USERDATA
,
4640 udata
->len
+ 1, udata
->data
))
4641 goto nla_put_failure
;
4644 nla_nest_end(skb
, nest
);
4652 struct nft_set_dump_args
{
4653 const struct netlink_callback
*cb
;
4654 struct nft_set_iter iter
;
4655 struct sk_buff
*skb
;
4658 static int nf_tables_dump_setelem(const struct nft_ctx
*ctx
,
4659 struct nft_set
*set
,
4660 const struct nft_set_iter
*iter
,
4661 struct nft_set_elem
*elem
)
4663 struct nft_set_dump_args
*args
;
4665 args
= container_of(iter
, struct nft_set_dump_args
, iter
);
4666 return nf_tables_fill_setelem(args
->skb
, set
, elem
);
4669 struct nft_set_dump_ctx
{
4670 const struct nft_set
*set
;
4674 static int nf_tables_dump_set(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4676 struct nft_set_dump_ctx
*dump_ctx
= cb
->data
;
4677 struct net
*net
= sock_net(skb
->sk
);
4678 struct nft_table
*table
;
4679 struct nft_set
*set
;
4680 struct nft_set_dump_args args
;
4681 bool set_found
= false;
4682 struct nfgenmsg
*nfmsg
;
4683 struct nlmsghdr
*nlh
;
4684 struct nlattr
*nest
;
4689 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
4690 if (dump_ctx
->ctx
.family
!= NFPROTO_UNSPEC
&&
4691 dump_ctx
->ctx
.family
!= table
->family
)
4694 if (table
!= dump_ctx
->ctx
.table
)
4697 list_for_each_entry_rcu(set
, &table
->sets
, list
) {
4698 if (set
== dump_ctx
->set
) {
4711 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, NFT_MSG_NEWSETELEM
);
4712 portid
= NETLINK_CB(cb
->skb
).portid
;
4713 seq
= cb
->nlh
->nlmsg_seq
;
4715 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
4718 goto nla_put_failure
;
4720 nfmsg
= nlmsg_data(nlh
);
4721 nfmsg
->nfgen_family
= table
->family
;
4722 nfmsg
->version
= NFNETLINK_V0
;
4723 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
4725 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_TABLE
, table
->name
))
4726 goto nla_put_failure
;
4727 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_SET
, set
->name
))
4728 goto nla_put_failure
;
4730 nest
= nla_nest_start_noflag(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
4732 goto nla_put_failure
;
4736 args
.iter
.genmask
= nft_genmask_cur(net
);
4737 args
.iter
.skip
= cb
->args
[0];
4738 args
.iter
.count
= 0;
4740 args
.iter
.fn
= nf_tables_dump_setelem
;
4741 set
->ops
->walk(&dump_ctx
->ctx
, set
, &args
.iter
);
4744 nla_nest_end(skb
, nest
);
4745 nlmsg_end(skb
, nlh
);
4747 if (args
.iter
.err
&& args
.iter
.err
!= -EMSGSIZE
)
4748 return args
.iter
.err
;
4749 if (args
.iter
.count
== cb
->args
[0])
4752 cb
->args
[0] = args
.iter
.count
;
4760 static int nf_tables_dump_set_start(struct netlink_callback
*cb
)
4762 struct nft_set_dump_ctx
*dump_ctx
= cb
->data
;
4764 cb
->data
= kmemdup(dump_ctx
, sizeof(*dump_ctx
), GFP_ATOMIC
);
4766 return cb
->data
? 0 : -ENOMEM
;
4769 static int nf_tables_dump_set_done(struct netlink_callback
*cb
)
4775 static int nf_tables_fill_setelem_info(struct sk_buff
*skb
,
4776 const struct nft_ctx
*ctx
, u32 seq
,
4777 u32 portid
, int event
, u16 flags
,
4778 const struct nft_set
*set
,
4779 const struct nft_set_elem
*elem
)
4781 struct nfgenmsg
*nfmsg
;
4782 struct nlmsghdr
*nlh
;
4783 struct nlattr
*nest
;
4786 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
4787 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
4790 goto nla_put_failure
;
4792 nfmsg
= nlmsg_data(nlh
);
4793 nfmsg
->nfgen_family
= ctx
->family
;
4794 nfmsg
->version
= NFNETLINK_V0
;
4795 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
4797 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
4798 goto nla_put_failure
;
4799 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
4800 goto nla_put_failure
;
4802 nest
= nla_nest_start_noflag(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
4804 goto nla_put_failure
;
4806 err
= nf_tables_fill_setelem(skb
, set
, elem
);
4808 goto nla_put_failure
;
4810 nla_nest_end(skb
, nest
);
4812 nlmsg_end(skb
, nlh
);
4816 nlmsg_trim(skb
, nlh
);
4820 static int nft_setelem_parse_flags(const struct nft_set
*set
,
4821 const struct nlattr
*attr
, u32
*flags
)
4826 *flags
= ntohl(nla_get_be32(attr
));
4827 if (*flags
& ~NFT_SET_ELEM_INTERVAL_END
)
4829 if (!(set
->flags
& NFT_SET_INTERVAL
) &&
4830 *flags
& NFT_SET_ELEM_INTERVAL_END
)
4836 static int nft_setelem_parse_key(struct nft_ctx
*ctx
, struct nft_set
*set
,
4837 struct nft_data
*key
, struct nlattr
*attr
)
4839 struct nft_data_desc desc
;
4842 err
= nft_data_init(ctx
, key
, NFT_DATA_VALUE_MAXLEN
, &desc
, attr
);
4846 if (desc
.type
!= NFT_DATA_VALUE
|| desc
.len
!= set
->klen
) {
4847 nft_data_release(key
, desc
.type
);
4854 static int nft_setelem_parse_data(struct nft_ctx
*ctx
, struct nft_set
*set
,
4855 struct nft_data_desc
*desc
,
4856 struct nft_data
*data
,
4857 struct nlattr
*attr
)
4861 err
= nft_data_init(ctx
, data
, NFT_DATA_VALUE_MAXLEN
, desc
, attr
);
4865 if (desc
->type
!= NFT_DATA_VERDICT
&& desc
->len
!= set
->dlen
) {
4866 nft_data_release(data
, desc
->type
);
4873 static int nft_get_set_elem(struct nft_ctx
*ctx
, struct nft_set
*set
,
4874 const struct nlattr
*attr
)
4876 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
4877 struct nft_set_elem elem
;
4878 struct sk_buff
*skb
;
4883 err
= nla_parse_nested_deprecated(nla
, NFTA_SET_ELEM_MAX
, attr
,
4884 nft_set_elem_policy
, NULL
);
4888 if (!nla
[NFTA_SET_ELEM_KEY
])
4891 err
= nft_setelem_parse_flags(set
, nla
[NFTA_SET_ELEM_FLAGS
], &flags
);
4895 err
= nft_setelem_parse_key(ctx
, set
, &elem
.key
.val
,
4896 nla
[NFTA_SET_ELEM_KEY
]);
4900 if (nla
[NFTA_SET_ELEM_KEY_END
]) {
4901 err
= nft_setelem_parse_key(ctx
, set
, &elem
.key_end
.val
,
4902 nla
[NFTA_SET_ELEM_KEY_END
]);
4907 priv
= set
->ops
->get(ctx
->net
, set
, &elem
, flags
);
4909 return PTR_ERR(priv
);
4914 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_ATOMIC
);
4918 err
= nf_tables_fill_setelem_info(skb
, ctx
, ctx
->seq
, ctx
->portid
,
4919 NFT_MSG_NEWSETELEM
, 0, set
, &elem
);
4921 goto err_fill_setelem
;
4923 return nfnetlink_unicast(skb
, ctx
->net
, ctx
->portid
);
4930 /* called with rcu_read_lock held */
4931 static int nf_tables_getsetelem(struct net
*net
, struct sock
*nlsk
,
4932 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
4933 const struct nlattr
* const nla
[],
4934 struct netlink_ext_ack
*extack
)
4936 u8 genmask
= nft_genmask_cur(net
);
4937 struct nft_set
*set
;
4938 struct nlattr
*attr
;
4942 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
, extack
,
4947 set
= nft_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
], genmask
);
4949 return PTR_ERR(set
);
4951 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
4952 struct netlink_dump_control c
= {
4953 .start
= nf_tables_dump_set_start
,
4954 .dump
= nf_tables_dump_set
,
4955 .done
= nf_tables_dump_set_done
,
4956 .module
= THIS_MODULE
,
4958 struct nft_set_dump_ctx dump_ctx
= {
4964 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
4967 if (!nla
[NFTA_SET_ELEM_LIST_ELEMENTS
])
4970 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
4971 err
= nft_get_set_elem(&ctx
, set
, attr
);
4979 static void nf_tables_setelem_notify(const struct nft_ctx
*ctx
,
4980 const struct nft_set
*set
,
4981 const struct nft_set_elem
*elem
,
4982 int event
, u16 flags
)
4984 struct net
*net
= ctx
->net
;
4985 u32 portid
= ctx
->portid
;
4986 struct sk_buff
*skb
;
4988 char *buf
= kasprintf(GFP_KERNEL
, "%s:%llu;%s:%llu",
4989 ctx
->table
->name
, ctx
->table
->handle
,
4990 set
->name
, set
->handle
);
4992 audit_log_nfcfg(buf
,
4995 event
== NFT_MSG_NEWSETELEM
?
4996 AUDIT_NFT_OP_SETELEM_REGISTER
:
4997 AUDIT_NFT_OP_SETELEM_UNREGISTER
,
5001 if (!ctx
->report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
5004 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
5008 err
= nf_tables_fill_setelem_info(skb
, ctx
, 0, portid
, event
, flags
,
5015 nft_notify_enqueue(skb
, ctx
->report
, &ctx
->net
->nft
.notify_list
);
5018 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
5021 static struct nft_trans
*nft_trans_elem_alloc(struct nft_ctx
*ctx
,
5023 struct nft_set
*set
)
5025 struct nft_trans
*trans
;
5027 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_elem
));
5031 nft_trans_elem_set(trans
) = set
;
5035 struct nft_expr
*nft_set_elem_expr_alloc(const struct nft_ctx
*ctx
,
5036 const struct nft_set
*set
,
5037 const struct nlattr
*attr
)
5039 struct nft_expr
*expr
;
5042 expr
= nft_expr_init(ctx
, attr
);
5047 if (!(expr
->ops
->type
->flags
& NFT_EXPR_STATEFUL
))
5048 goto err_set_elem_expr
;
5050 if (expr
->ops
->type
->flags
& NFT_EXPR_GC
) {
5051 if (set
->flags
& NFT_SET_TIMEOUT
)
5052 goto err_set_elem_expr
;
5053 if (!set
->ops
->gc_init
)
5054 goto err_set_elem_expr
;
5055 set
->ops
->gc_init(set
);
5061 nft_expr_destroy(ctx
, expr
);
5062 return ERR_PTR(err
);
5065 void *nft_set_elem_init(const struct nft_set
*set
,
5066 const struct nft_set_ext_tmpl
*tmpl
,
5067 const u32
*key
, const u32
*key_end
,
5068 const u32
*data
, u64 timeout
, u64 expiration
, gfp_t gfp
)
5070 struct nft_set_ext
*ext
;
5073 elem
= kzalloc(set
->ops
->elemsize
+ tmpl
->len
, gfp
);
5077 ext
= nft_set_elem_ext(set
, elem
);
5078 nft_set_ext_init(ext
, tmpl
);
5080 memcpy(nft_set_ext_key(ext
), key
, set
->klen
);
5081 if (nft_set_ext_exists(ext
, NFT_SET_EXT_KEY_END
))
5082 memcpy(nft_set_ext_key_end(ext
), key_end
, set
->klen
);
5083 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
5084 memcpy(nft_set_ext_data(ext
), data
, set
->dlen
);
5085 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
)) {
5086 *nft_set_ext_expiration(ext
) = get_jiffies_64() + expiration
;
5087 if (expiration
== 0)
5088 *nft_set_ext_expiration(ext
) += timeout
;
5090 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
))
5091 *nft_set_ext_timeout(ext
) = timeout
;
5096 static void nft_set_elem_expr_destroy(const struct nft_ctx
*ctx
,
5097 struct nft_expr
*expr
)
5099 if (expr
->ops
->destroy_clone
) {
5100 expr
->ops
->destroy_clone(ctx
, expr
);
5101 module_put(expr
->ops
->type
->owner
);
5103 nf_tables_expr_destroy(ctx
, expr
);
5107 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
,
5110 struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
);
5111 struct nft_ctx ctx
= {
5112 .net
= read_pnet(&set
->net
),
5113 .family
= set
->table
->family
,
5116 nft_data_release(nft_set_ext_key(ext
), NFT_DATA_VALUE
);
5117 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
5118 nft_data_release(nft_set_ext_data(ext
), set
->dtype
);
5119 if (destroy_expr
&& nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
))
5120 nft_set_elem_expr_destroy(&ctx
, nft_set_ext_expr(ext
));
5122 if (nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
))
5123 (*nft_set_ext_obj(ext
))->use
--;
5126 EXPORT_SYMBOL_GPL(nft_set_elem_destroy
);
5128 /* Only called from commit path, nft_set_elem_deactivate() already deals with
5129 * the refcounting from the preparation phase.
5131 static void nf_tables_set_elem_destroy(const struct nft_ctx
*ctx
,
5132 const struct nft_set
*set
, void *elem
)
5134 struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
);
5136 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
))
5137 nft_set_elem_expr_destroy(ctx
, nft_set_ext_expr(ext
));
5142 static int nft_add_set_elem(struct nft_ctx
*ctx
, struct nft_set
*set
,
5143 const struct nlattr
*attr
, u32 nlmsg_flags
)
5145 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
5146 u8 genmask
= nft_genmask_next(ctx
->net
);
5147 struct nft_set_ext_tmpl tmpl
;
5148 struct nft_set_ext
*ext
, *ext2
;
5149 struct nft_set_elem elem
;
5150 struct nft_set_binding
*binding
;
5151 struct nft_object
*obj
= NULL
;
5152 struct nft_expr
*expr
= NULL
;
5153 struct nft_userdata
*udata
;
5154 struct nft_data_desc desc
;
5155 enum nft_registers dreg
;
5156 struct nft_trans
*trans
;
5163 err
= nla_parse_nested_deprecated(nla
, NFTA_SET_ELEM_MAX
, attr
,
5164 nft_set_elem_policy
, NULL
);
5168 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
5171 nft_set_ext_prepare(&tmpl
);
5173 err
= nft_setelem_parse_flags(set
, nla
[NFTA_SET_ELEM_FLAGS
], &flags
);
5177 nft_set_ext_add(&tmpl
, NFT_SET_EXT_FLAGS
);
5179 if (set
->flags
& NFT_SET_MAP
) {
5180 if (nla
[NFTA_SET_ELEM_DATA
] == NULL
&&
5181 !(flags
& NFT_SET_ELEM_INTERVAL_END
))
5184 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
5188 if ((flags
& NFT_SET_ELEM_INTERVAL_END
) &&
5189 (nla
[NFTA_SET_ELEM_DATA
] ||
5190 nla
[NFTA_SET_ELEM_OBJREF
] ||
5191 nla
[NFTA_SET_ELEM_TIMEOUT
] ||
5192 nla
[NFTA_SET_ELEM_EXPIRATION
] ||
5193 nla
[NFTA_SET_ELEM_USERDATA
] ||
5194 nla
[NFTA_SET_ELEM_EXPR
]))
5198 if (nla
[NFTA_SET_ELEM_TIMEOUT
] != NULL
) {
5199 if (!(set
->flags
& NFT_SET_TIMEOUT
))
5201 err
= nf_msecs_to_jiffies64(nla
[NFTA_SET_ELEM_TIMEOUT
],
5205 } else if (set
->flags
& NFT_SET_TIMEOUT
) {
5206 timeout
= set
->timeout
;
5210 if (nla
[NFTA_SET_ELEM_EXPIRATION
] != NULL
) {
5211 if (!(set
->flags
& NFT_SET_TIMEOUT
))
5213 err
= nf_msecs_to_jiffies64(nla
[NFTA_SET_ELEM_EXPIRATION
],
5219 if (nla
[NFTA_SET_ELEM_EXPR
] != NULL
) {
5220 expr
= nft_set_elem_expr_alloc(ctx
, set
,
5221 nla
[NFTA_SET_ELEM_EXPR
]);
5223 return PTR_ERR(expr
);
5226 if (set
->expr
&& set
->expr
->ops
!= expr
->ops
)
5227 goto err_set_elem_expr
;
5228 } else if (set
->expr
) {
5229 expr
= kzalloc(set
->expr
->ops
->size
, GFP_KERNEL
);
5233 err
= nft_expr_clone(expr
, set
->expr
);
5235 goto err_set_elem_expr
;
5238 err
= nft_setelem_parse_key(ctx
, set
, &elem
.key
.val
,
5239 nla
[NFTA_SET_ELEM_KEY
]);
5241 goto err_set_elem_expr
;
5243 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_KEY
, set
->klen
);
5245 if (nla
[NFTA_SET_ELEM_KEY_END
]) {
5246 err
= nft_setelem_parse_key(ctx
, set
, &elem
.key_end
.val
,
5247 nla
[NFTA_SET_ELEM_KEY_END
]);
5251 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_KEY_END
, set
->klen
);
5255 nft_set_ext_add(&tmpl
, NFT_SET_EXT_EXPIRATION
);
5256 if (timeout
!= set
->timeout
)
5257 nft_set_ext_add(&tmpl
, NFT_SET_EXT_TIMEOUT
);
5261 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_EXPR
,
5264 if (nla
[NFTA_SET_ELEM_OBJREF
] != NULL
) {
5265 if (!(set
->flags
& NFT_SET_OBJECT
)) {
5267 goto err_parse_key_end
;
5269 obj
= nft_obj_lookup(ctx
->net
, ctx
->table
,
5270 nla
[NFTA_SET_ELEM_OBJREF
],
5271 set
->objtype
, genmask
);
5274 goto err_parse_key_end
;
5276 nft_set_ext_add(&tmpl
, NFT_SET_EXT_OBJREF
);
5279 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
) {
5280 err
= nft_setelem_parse_data(ctx
, set
, &desc
, &elem
.data
.val
,
5281 nla
[NFTA_SET_ELEM_DATA
]);
5283 goto err_parse_key_end
;
5285 dreg
= nft_type_to_reg(set
->dtype
);
5286 list_for_each_entry(binding
, &set
->bindings
, list
) {
5287 struct nft_ctx bind_ctx
= {
5289 .family
= ctx
->family
,
5290 .table
= ctx
->table
,
5291 .chain
= (struct nft_chain
*)binding
->chain
,
5294 if (!(binding
->flags
& NFT_SET_MAP
))
5297 err
= nft_validate_register_store(&bind_ctx
, dreg
,
5299 desc
.type
, desc
.len
);
5301 goto err_parse_data
;
5303 if (desc
.type
== NFT_DATA_VERDICT
&&
5304 (elem
.data
.val
.verdict
.code
== NFT_GOTO
||
5305 elem
.data
.val
.verdict
.code
== NFT_JUMP
))
5306 nft_validate_state_update(ctx
->net
,
5310 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_DATA
, desc
.len
);
5313 /* The full maximum length of userdata can exceed the maximum
5314 * offset value (U8_MAX) for following extensions, therefor it
5315 * must be the last extension added.
5318 if (nla
[NFTA_SET_ELEM_USERDATA
] != NULL
) {
5319 ulen
= nla_len(nla
[NFTA_SET_ELEM_USERDATA
]);
5321 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_USERDATA
,
5326 elem
.priv
= nft_set_elem_init(set
, &tmpl
, elem
.key
.val
.data
,
5327 elem
.key_end
.val
.data
, elem
.data
.val
.data
,
5328 timeout
, expiration
, GFP_KERNEL
);
5329 if (elem
.priv
== NULL
)
5330 goto err_parse_data
;
5332 ext
= nft_set_elem_ext(set
, elem
.priv
);
5334 *nft_set_ext_flags(ext
) = flags
;
5336 udata
= nft_set_ext_userdata(ext
);
5337 udata
->len
= ulen
- 1;
5338 nla_memcpy(&udata
->data
, nla
[NFTA_SET_ELEM_USERDATA
], ulen
);
5341 *nft_set_ext_obj(ext
) = obj
;
5345 memcpy(nft_set_ext_expr(ext
), expr
, expr
->ops
->size
);
5350 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_NEWSETELEM
, set
);
5354 ext
->genmask
= nft_genmask_cur(ctx
->net
) | NFT_SET_ELEM_BUSY_MASK
;
5355 err
= set
->ops
->insert(ctx
->net
, set
, &elem
, &ext2
);
5357 if (err
== -EEXIST
) {
5358 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
) ^
5359 nft_set_ext_exists(ext2
, NFT_SET_EXT_DATA
) ||
5360 nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
) ^
5361 nft_set_ext_exists(ext2
, NFT_SET_EXT_OBJREF
))
5362 goto err_element_clash
;
5363 if ((nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
) &&
5364 nft_set_ext_exists(ext2
, NFT_SET_EXT_DATA
) &&
5365 memcmp(nft_set_ext_data(ext
),
5366 nft_set_ext_data(ext2
), set
->dlen
) != 0) ||
5367 (nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
) &&
5368 nft_set_ext_exists(ext2
, NFT_SET_EXT_OBJREF
) &&
5369 *nft_set_ext_obj(ext
) != *nft_set_ext_obj(ext2
)))
5370 goto err_element_clash
;
5371 else if (!(nlmsg_flags
& NLM_F_EXCL
))
5373 } else if (err
== -ENOTEMPTY
) {
5374 /* ENOTEMPTY reports overlapping between this element
5375 * and an existing one.
5379 goto err_element_clash
;
5383 !atomic_add_unless(&set
->nelems
, 1, set
->size
+ set
->ndeact
)) {
5388 nft_trans_elem(trans
) = elem
;
5389 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
5393 set
->ops
->remove(ctx
->net
, set
, &elem
);
5400 nf_tables_set_elem_destroy(ctx
, set
, elem
.priv
);
5402 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
5403 nft_data_release(&elem
.data
.val
, desc
.type
);
5405 nft_data_release(&elem
.key_end
.val
, NFT_DATA_VALUE
);
5407 nft_data_release(&elem
.key
.val
, NFT_DATA_VALUE
);
5410 nft_expr_destroy(ctx
, expr
);
5415 static int nf_tables_newsetelem(struct net
*net
, struct sock
*nlsk
,
5416 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
5417 const struct nlattr
* const nla
[],
5418 struct netlink_ext_ack
*extack
)
5420 u8 genmask
= nft_genmask_next(net
);
5421 const struct nlattr
*attr
;
5422 struct nft_set
*set
;
5426 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
)
5429 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
, extack
,
5434 set
= nft_set_lookup_global(net
, ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
],
5435 nla
[NFTA_SET_ELEM_LIST_SET_ID
], genmask
);
5437 return PTR_ERR(set
);
5439 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
5442 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
5443 err
= nft_add_set_elem(&ctx
, set
, attr
, nlh
->nlmsg_flags
);
5448 if (net
->nft
.validate_state
== NFT_VALIDATE_DO
)
5449 return nft_table_validate(net
, ctx
.table
);
5455 * nft_data_hold - hold a nft_data item
5457 * @data: struct nft_data to release
5458 * @type: type of data
5460 * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded,
5461 * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and
5462 * NFT_GOTO verdicts. This function must be called on active data objects
5463 * from the second phase of the commit protocol.
5465 void nft_data_hold(const struct nft_data
*data
, enum nft_data_types type
)
5467 struct nft_chain
*chain
;
5468 struct nft_rule
*rule
;
5470 if (type
== NFT_DATA_VERDICT
) {
5471 switch (data
->verdict
.code
) {
5474 chain
= data
->verdict
.chain
;
5477 if (!nft_chain_is_bound(chain
))
5480 chain
->table
->use
++;
5481 list_for_each_entry(rule
, &chain
->rules
, list
)
5484 nft_chain_add(chain
->table
, chain
);
5490 static void nft_set_elem_activate(const struct net
*net
,
5491 const struct nft_set
*set
,
5492 struct nft_set_elem
*elem
)
5494 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
5496 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
5497 nft_data_hold(nft_set_ext_data(ext
), set
->dtype
);
5498 if (nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
))
5499 (*nft_set_ext_obj(ext
))->use
++;
5502 static void nft_set_elem_deactivate(const struct net
*net
,
5503 const struct nft_set
*set
,
5504 struct nft_set_elem
*elem
)
5506 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
5508 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
5509 nft_data_release(nft_set_ext_data(ext
), set
->dtype
);
5510 if (nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
))
5511 (*nft_set_ext_obj(ext
))->use
--;
5514 static int nft_del_setelem(struct nft_ctx
*ctx
, struct nft_set
*set
,
5515 const struct nlattr
*attr
)
5517 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
5518 struct nft_set_ext_tmpl tmpl
;
5519 struct nft_set_elem elem
;
5520 struct nft_set_ext
*ext
;
5521 struct nft_trans
*trans
;
5526 err
= nla_parse_nested_deprecated(nla
, NFTA_SET_ELEM_MAX
, attr
,
5527 nft_set_elem_policy
, NULL
);
5531 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
5534 nft_set_ext_prepare(&tmpl
);
5536 err
= nft_setelem_parse_flags(set
, nla
[NFTA_SET_ELEM_FLAGS
], &flags
);
5540 nft_set_ext_add(&tmpl
, NFT_SET_EXT_FLAGS
);
5542 err
= nft_setelem_parse_key(ctx
, set
, &elem
.key
.val
,
5543 nla
[NFTA_SET_ELEM_KEY
]);
5547 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_KEY
, set
->klen
);
5549 if (nla
[NFTA_SET_ELEM_KEY_END
]) {
5550 err
= nft_setelem_parse_key(ctx
, set
, &elem
.key_end
.val
,
5551 nla
[NFTA_SET_ELEM_KEY_END
]);
5555 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_KEY_END
, set
->klen
);
5559 elem
.priv
= nft_set_elem_init(set
, &tmpl
, elem
.key
.val
.data
,
5560 elem
.key_end
.val
.data
, NULL
, 0, 0,
5562 if (elem
.priv
== NULL
)
5565 ext
= nft_set_elem_ext(set
, elem
.priv
);
5567 *nft_set_ext_flags(ext
) = flags
;
5569 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_DELSETELEM
, set
);
5573 priv
= set
->ops
->deactivate(ctx
->net
, set
, &elem
);
5581 nft_set_elem_deactivate(ctx
->net
, set
, &elem
);
5583 nft_trans_elem(trans
) = elem
;
5584 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
5592 nft_data_release(&elem
.key
.val
, NFT_DATA_VALUE
);
5596 static int nft_flush_set(const struct nft_ctx
*ctx
,
5597 struct nft_set
*set
,
5598 const struct nft_set_iter
*iter
,
5599 struct nft_set_elem
*elem
)
5601 struct nft_trans
*trans
;
5604 trans
= nft_trans_alloc_gfp(ctx
, NFT_MSG_DELSETELEM
,
5605 sizeof(struct nft_trans_elem
), GFP_ATOMIC
);
5609 if (!set
->ops
->flush(ctx
->net
, set
, elem
->priv
)) {
5615 nft_set_elem_deactivate(ctx
->net
, set
, elem
);
5616 nft_trans_elem_set(trans
) = set
;
5617 nft_trans_elem(trans
) = *elem
;
5618 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
5626 static int nf_tables_delsetelem(struct net
*net
, struct sock
*nlsk
,
5627 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
5628 const struct nlattr
* const nla
[],
5629 struct netlink_ext_ack
*extack
)
5631 u8 genmask
= nft_genmask_next(net
);
5632 const struct nlattr
*attr
;
5633 struct nft_set
*set
;
5637 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
, extack
,
5642 set
= nft_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
], genmask
);
5644 return PTR_ERR(set
);
5645 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
5648 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
) {
5649 struct nft_set_iter iter
= {
5651 .fn
= nft_flush_set
,
5653 set
->ops
->walk(&ctx
, set
, &iter
);
5658 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
5659 err
= nft_del_setelem(&ctx
, set
, attr
);
5668 void nft_set_gc_batch_release(struct rcu_head
*rcu
)
5670 struct nft_set_gc_batch
*gcb
;
5673 gcb
= container_of(rcu
, struct nft_set_gc_batch
, head
.rcu
);
5674 for (i
= 0; i
< gcb
->head
.cnt
; i
++)
5675 nft_set_elem_destroy(gcb
->head
.set
, gcb
->elems
[i
], true);
5679 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
5682 struct nft_set_gc_batch
*gcb
;
5684 gcb
= kzalloc(sizeof(*gcb
), gfp
);
5687 gcb
->head
.set
= set
;
5696 * nft_register_obj- register nf_tables stateful object type
5697 * @obj_type: object type
5699 * Registers the object type for use with nf_tables. Returns zero on
5700 * success or a negative errno code otherwise.
5702 int nft_register_obj(struct nft_object_type
*obj_type
)
5704 if (obj_type
->type
== NFT_OBJECT_UNSPEC
)
5707 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
5708 list_add_rcu(&obj_type
->list
, &nf_tables_objects
);
5709 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
5712 EXPORT_SYMBOL_GPL(nft_register_obj
);
5715 * nft_unregister_obj - unregister nf_tables object type
5716 * @obj_type: object type
5718 * Unregisters the object type for use with nf_tables.
5720 void nft_unregister_obj(struct nft_object_type
*obj_type
)
5722 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
5723 list_del_rcu(&obj_type
->list
);
5724 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
5726 EXPORT_SYMBOL_GPL(nft_unregister_obj
);
5728 struct nft_object
*nft_obj_lookup(const struct net
*net
,
5729 const struct nft_table
*table
,
5730 const struct nlattr
*nla
, u32 objtype
,
5733 struct nft_object_hash_key k
= { .table
= table
};
5734 char search
[NFT_OBJ_MAXNAMELEN
];
5735 struct rhlist_head
*tmp
, *list
;
5736 struct nft_object
*obj
;
5738 nla_strlcpy(search
, nla
, sizeof(search
));
5741 WARN_ON_ONCE(!rcu_read_lock_held() &&
5742 !lockdep_commit_lock_is_held(net
));
5745 list
= rhltable_lookup(&nft_objname_ht
, &k
, nft_objname_ht_params
);
5749 rhl_for_each_entry_rcu(obj
, tmp
, list
, rhlhead
) {
5750 if (objtype
== obj
->ops
->type
->type
&&
5751 nft_active_genmask(obj
, genmask
)) {
5758 return ERR_PTR(-ENOENT
);
5760 EXPORT_SYMBOL_GPL(nft_obj_lookup
);
5762 static struct nft_object
*nft_obj_lookup_byhandle(const struct nft_table
*table
,
5763 const struct nlattr
*nla
,
5764 u32 objtype
, u8 genmask
)
5766 struct nft_object
*obj
;
5768 list_for_each_entry(obj
, &table
->objects
, list
) {
5769 if (be64_to_cpu(nla_get_be64(nla
)) == obj
->handle
&&
5770 objtype
== obj
->ops
->type
->type
&&
5771 nft_active_genmask(obj
, genmask
))
5774 return ERR_PTR(-ENOENT
);
5777 static const struct nla_policy nft_obj_policy
[NFTA_OBJ_MAX
+ 1] = {
5778 [NFTA_OBJ_TABLE
] = { .type
= NLA_STRING
,
5779 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
5780 [NFTA_OBJ_NAME
] = { .type
= NLA_STRING
,
5781 .len
= NFT_OBJ_MAXNAMELEN
- 1 },
5782 [NFTA_OBJ_TYPE
] = { .type
= NLA_U32
},
5783 [NFTA_OBJ_DATA
] = { .type
= NLA_NESTED
},
5784 [NFTA_OBJ_HANDLE
] = { .type
= NLA_U64
},
5785 [NFTA_OBJ_USERDATA
] = { .type
= NLA_BINARY
,
5786 .len
= NFT_USERDATA_MAXLEN
},
5789 static struct nft_object
*nft_obj_init(const struct nft_ctx
*ctx
,
5790 const struct nft_object_type
*type
,
5791 const struct nlattr
*attr
)
5794 const struct nft_object_ops
*ops
;
5795 struct nft_object
*obj
;
5798 tb
= kmalloc_array(type
->maxattr
+ 1, sizeof(*tb
), GFP_KERNEL
);
5803 err
= nla_parse_nested_deprecated(tb
, type
->maxattr
, attr
,
5804 type
->policy
, NULL
);
5808 memset(tb
, 0, sizeof(tb
[0]) * (type
->maxattr
+ 1));
5811 if (type
->select_ops
) {
5812 ops
= type
->select_ops(ctx
, (const struct nlattr
* const *)tb
);
5822 obj
= kzalloc(sizeof(*obj
) + ops
->size
, GFP_KERNEL
);
5826 err
= ops
->init(ctx
, (const struct nlattr
* const *)tb
, obj
);
5839 return ERR_PTR(err
);
5842 static int nft_object_dump(struct sk_buff
*skb
, unsigned int attr
,
5843 struct nft_object
*obj
, bool reset
)
5845 struct nlattr
*nest
;
5847 nest
= nla_nest_start_noflag(skb
, attr
);
5849 goto nla_put_failure
;
5850 if (obj
->ops
->dump(skb
, obj
, reset
) < 0)
5851 goto nla_put_failure
;
5852 nla_nest_end(skb
, nest
);
5859 static const struct nft_object_type
*__nft_obj_type_get(u32 objtype
)
5861 const struct nft_object_type
*type
;
5863 list_for_each_entry(type
, &nf_tables_objects
, list
) {
5864 if (objtype
== type
->type
)
5870 static const struct nft_object_type
*
5871 nft_obj_type_get(struct net
*net
, u32 objtype
)
5873 const struct nft_object_type
*type
;
5875 type
= __nft_obj_type_get(objtype
);
5876 if (type
!= NULL
&& try_module_get(type
->owner
))
5879 lockdep_nfnl_nft_mutex_not_held();
5880 #ifdef CONFIG_MODULES
5882 if (nft_request_module(net
, "nft-obj-%u", objtype
) == -EAGAIN
)
5883 return ERR_PTR(-EAGAIN
);
5886 return ERR_PTR(-ENOENT
);
5889 static int nf_tables_updobj(const struct nft_ctx
*ctx
,
5890 const struct nft_object_type
*type
,
5891 const struct nlattr
*attr
,
5892 struct nft_object
*obj
)
5894 struct nft_object
*newobj
;
5895 struct nft_trans
*trans
;
5898 trans
= nft_trans_alloc(ctx
, NFT_MSG_NEWOBJ
,
5899 sizeof(struct nft_trans_obj
));
5903 newobj
= nft_obj_init(ctx
, type
, attr
);
5904 if (IS_ERR(newobj
)) {
5905 err
= PTR_ERR(newobj
);
5906 goto err_free_trans
;
5909 nft_trans_obj(trans
) = obj
;
5910 nft_trans_obj_update(trans
) = true;
5911 nft_trans_obj_newobj(trans
) = newobj
;
5912 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
5921 static int nf_tables_newobj(struct net
*net
, struct sock
*nlsk
,
5922 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
5923 const struct nlattr
* const nla
[],
5924 struct netlink_ext_ack
*extack
)
5926 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
5927 const struct nft_object_type
*type
;
5928 u8 genmask
= nft_genmask_next(net
);
5929 int family
= nfmsg
->nfgen_family
;
5930 struct nft_table
*table
;
5931 struct nft_object
*obj
;
5936 if (!nla
[NFTA_OBJ_TYPE
] ||
5937 !nla
[NFTA_OBJ_NAME
] ||
5938 !nla
[NFTA_OBJ_DATA
])
5941 table
= nft_table_lookup(net
, nla
[NFTA_OBJ_TABLE
], family
, genmask
);
5942 if (IS_ERR(table
)) {
5943 NL_SET_BAD_ATTR(extack
, nla
[NFTA_OBJ_TABLE
]);
5944 return PTR_ERR(table
);
5947 objtype
= ntohl(nla_get_be32(nla
[NFTA_OBJ_TYPE
]));
5948 obj
= nft_obj_lookup(net
, table
, nla
[NFTA_OBJ_NAME
], objtype
, genmask
);
5951 if (err
!= -ENOENT
) {
5952 NL_SET_BAD_ATTR(extack
, nla
[NFTA_OBJ_NAME
]);
5956 if (nlh
->nlmsg_flags
& NLM_F_EXCL
) {
5957 NL_SET_BAD_ATTR(extack
, nla
[NFTA_OBJ_NAME
]);
5960 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
5963 type
= __nft_obj_type_get(objtype
);
5964 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
5966 return nf_tables_updobj(&ctx
, type
, nla
[NFTA_OBJ_DATA
], obj
);
5969 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
5971 type
= nft_obj_type_get(net
, objtype
);
5973 return PTR_ERR(type
);
5975 obj
= nft_obj_init(&ctx
, type
, nla
[NFTA_OBJ_DATA
]);
5980 obj
->key
.table
= table
;
5981 obj
->handle
= nf_tables_alloc_handle(table
);
5983 obj
->key
.name
= nla_strdup(nla
[NFTA_OBJ_NAME
], GFP_KERNEL
);
5984 if (!obj
->key
.name
) {
5989 if (nla
[NFTA_OBJ_USERDATA
]) {
5990 obj
->udata
= nla_memdup(nla
[NFTA_OBJ_USERDATA
], GFP_KERNEL
);
5991 if (obj
->udata
== NULL
)
5994 obj
->udlen
= nla_len(nla
[NFTA_OBJ_USERDATA
]);
5997 err
= nft_trans_obj_add(&ctx
, NFT_MSG_NEWOBJ
, obj
);
6001 err
= rhltable_insert(&nft_objname_ht
, &obj
->rhlhead
,
6002 nft_objname_ht_params
);
6006 list_add_tail_rcu(&obj
->list
, &table
->objects
);
6010 /* queued in transaction log */
6011 INIT_LIST_HEAD(&obj
->list
);
6014 kfree(obj
->key
.name
);
6018 if (obj
->ops
->destroy
)
6019 obj
->ops
->destroy(&ctx
, obj
);
6022 module_put(type
->owner
);
6026 static int nf_tables_fill_obj_info(struct sk_buff
*skb
, struct net
*net
,
6027 u32 portid
, u32 seq
, int event
, u32 flags
,
6028 int family
, const struct nft_table
*table
,
6029 struct nft_object
*obj
, bool reset
)
6031 struct nfgenmsg
*nfmsg
;
6032 struct nlmsghdr
*nlh
;
6034 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
6035 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
6037 goto nla_put_failure
;
6039 nfmsg
= nlmsg_data(nlh
);
6040 nfmsg
->nfgen_family
= family
;
6041 nfmsg
->version
= NFNETLINK_V0
;
6042 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
6044 if (nla_put_string(skb
, NFTA_OBJ_TABLE
, table
->name
) ||
6045 nla_put_string(skb
, NFTA_OBJ_NAME
, obj
->key
.name
) ||
6046 nla_put_be32(skb
, NFTA_OBJ_TYPE
, htonl(obj
->ops
->type
->type
)) ||
6047 nla_put_be32(skb
, NFTA_OBJ_USE
, htonl(obj
->use
)) ||
6048 nft_object_dump(skb
, NFTA_OBJ_DATA
, obj
, reset
) ||
6049 nla_put_be64(skb
, NFTA_OBJ_HANDLE
, cpu_to_be64(obj
->handle
),
6051 goto nla_put_failure
;
6054 nla_put(skb
, NFTA_OBJ_USERDATA
, obj
->udlen
, obj
->udata
))
6055 goto nla_put_failure
;
6057 nlmsg_end(skb
, nlh
);
6061 nlmsg_trim(skb
, nlh
);
6065 struct nft_obj_filter
{
6070 static int nf_tables_dump_obj(struct sk_buff
*skb
, struct netlink_callback
*cb
)
6072 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
6073 const struct nft_table
*table
;
6074 unsigned int idx
= 0, s_idx
= cb
->args
[0];
6075 struct nft_obj_filter
*filter
= cb
->data
;
6076 struct net
*net
= sock_net(skb
->sk
);
6077 int family
= nfmsg
->nfgen_family
;
6078 struct nft_object
*obj
;
6081 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) == NFT_MSG_GETOBJ_RESET
)
6085 cb
->seq
= net
->nft
.base_seq
;
6087 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
6088 if (family
!= NFPROTO_UNSPEC
&& family
!= table
->family
)
6091 list_for_each_entry_rcu(obj
, &table
->objects
, list
) {
6092 if (!nft_is_active(net
, obj
))
6097 memset(&cb
->args
[1], 0,
6098 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
6099 if (filter
&& filter
->table
&&
6100 strcmp(filter
->table
, table
->name
))
6103 filter
->type
!= NFT_OBJECT_UNSPEC
&&
6104 obj
->ops
->type
->type
!= filter
->type
)
6108 char *buf
= kasprintf(GFP_ATOMIC
,
6113 audit_log_nfcfg(buf
,
6116 AUDIT_NFT_OP_OBJ_RESET
,
6121 if (nf_tables_fill_obj_info(skb
, net
, NETLINK_CB(cb
->skb
).portid
,
6124 NLM_F_MULTI
| NLM_F_APPEND
,
6125 table
->family
, table
,
6129 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
6141 static int nf_tables_dump_obj_start(struct netlink_callback
*cb
)
6143 const struct nlattr
* const *nla
= cb
->data
;
6144 struct nft_obj_filter
*filter
= NULL
;
6146 if (nla
[NFTA_OBJ_TABLE
] || nla
[NFTA_OBJ_TYPE
]) {
6147 filter
= kzalloc(sizeof(*filter
), GFP_ATOMIC
);
6151 if (nla
[NFTA_OBJ_TABLE
]) {
6152 filter
->table
= nla_strdup(nla
[NFTA_OBJ_TABLE
], GFP_ATOMIC
);
6153 if (!filter
->table
) {
6159 if (nla
[NFTA_OBJ_TYPE
])
6160 filter
->type
= ntohl(nla_get_be32(nla
[NFTA_OBJ_TYPE
]));
6167 static int nf_tables_dump_obj_done(struct netlink_callback
*cb
)
6169 struct nft_obj_filter
*filter
= cb
->data
;
6172 kfree(filter
->table
);
6179 /* called with rcu_read_lock held */
6180 static int nf_tables_getobj(struct net
*net
, struct sock
*nlsk
,
6181 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
6182 const struct nlattr
* const nla
[],
6183 struct netlink_ext_ack
*extack
)
6185 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
6186 u8 genmask
= nft_genmask_cur(net
);
6187 int family
= nfmsg
->nfgen_family
;
6188 const struct nft_table
*table
;
6189 struct nft_object
*obj
;
6190 struct sk_buff
*skb2
;
6195 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
6196 struct netlink_dump_control c
= {
6197 .start
= nf_tables_dump_obj_start
,
6198 .dump
= nf_tables_dump_obj
,
6199 .done
= nf_tables_dump_obj_done
,
6200 .module
= THIS_MODULE
,
6201 .data
= (void *)nla
,
6204 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
6207 if (!nla
[NFTA_OBJ_NAME
] ||
6208 !nla
[NFTA_OBJ_TYPE
])
6211 table
= nft_table_lookup(net
, nla
[NFTA_OBJ_TABLE
], family
, genmask
);
6212 if (IS_ERR(table
)) {
6213 NL_SET_BAD_ATTR(extack
, nla
[NFTA_OBJ_TABLE
]);
6214 return PTR_ERR(table
);
6217 objtype
= ntohl(nla_get_be32(nla
[NFTA_OBJ_TYPE
]));
6218 obj
= nft_obj_lookup(net
, table
, nla
[NFTA_OBJ_NAME
], objtype
, genmask
);
6220 NL_SET_BAD_ATTR(extack
, nla
[NFTA_OBJ_NAME
]);
6221 return PTR_ERR(obj
);
6224 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
6228 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) == NFT_MSG_GETOBJ_RESET
)
6232 char *buf
= kasprintf(GFP_ATOMIC
, "%s:%llu;?:0",
6233 table
->name
, table
->handle
);
6235 audit_log_nfcfg(buf
,
6238 AUDIT_NFT_OP_OBJ_RESET
,
6243 err
= nf_tables_fill_obj_info(skb2
, net
, NETLINK_CB(skb
).portid
,
6244 nlh
->nlmsg_seq
, NFT_MSG_NEWOBJ
, 0,
6245 family
, table
, obj
, reset
);
6247 goto err_fill_obj_info
;
6249 return nfnetlink_unicast(skb2
, net
, NETLINK_CB(skb
).portid
);
6256 static void nft_obj_destroy(const struct nft_ctx
*ctx
, struct nft_object
*obj
)
6258 if (obj
->ops
->destroy
)
6259 obj
->ops
->destroy(ctx
, obj
);
6261 module_put(obj
->ops
->type
->owner
);
6262 kfree(obj
->key
.name
);
6267 static int nf_tables_delobj(struct net
*net
, struct sock
*nlsk
,
6268 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
6269 const struct nlattr
* const nla
[],
6270 struct netlink_ext_ack
*extack
)
6272 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
6273 u8 genmask
= nft_genmask_next(net
);
6274 int family
= nfmsg
->nfgen_family
;
6275 const struct nlattr
*attr
;
6276 struct nft_table
*table
;
6277 struct nft_object
*obj
;
6281 if (!nla
[NFTA_OBJ_TYPE
] ||
6282 (!nla
[NFTA_OBJ_NAME
] && !nla
[NFTA_OBJ_HANDLE
]))
6285 table
= nft_table_lookup(net
, nla
[NFTA_OBJ_TABLE
], family
, genmask
);
6286 if (IS_ERR(table
)) {
6287 NL_SET_BAD_ATTR(extack
, nla
[NFTA_OBJ_TABLE
]);
6288 return PTR_ERR(table
);
6291 objtype
= ntohl(nla_get_be32(nla
[NFTA_OBJ_TYPE
]));
6292 if (nla
[NFTA_OBJ_HANDLE
]) {
6293 attr
= nla
[NFTA_OBJ_HANDLE
];
6294 obj
= nft_obj_lookup_byhandle(table
, attr
, objtype
, genmask
);
6296 attr
= nla
[NFTA_OBJ_NAME
];
6297 obj
= nft_obj_lookup(net
, table
, attr
, objtype
, genmask
);
6301 NL_SET_BAD_ATTR(extack
, attr
);
6302 return PTR_ERR(obj
);
6305 NL_SET_BAD_ATTR(extack
, attr
);
6309 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
6311 return nft_delobj(&ctx
, obj
);
6314 void nft_obj_notify(struct net
*net
, const struct nft_table
*table
,
6315 struct nft_object
*obj
, u32 portid
, u32 seq
, int event
,
6316 int family
, int report
, gfp_t gfp
)
6318 struct sk_buff
*skb
;
6320 char *buf
= kasprintf(gfp
, "%s:%llu;?:0",
6321 table
->name
, table
->handle
);
6323 audit_log_nfcfg(buf
,
6326 event
== NFT_MSG_NEWOBJ
?
6327 AUDIT_NFT_OP_OBJ_REGISTER
:
6328 AUDIT_NFT_OP_OBJ_UNREGISTER
,
6333 !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
6336 skb
= nlmsg_new(NLMSG_GOODSIZE
, gfp
);
6340 err
= nf_tables_fill_obj_info(skb
, net
, portid
, seq
, event
, 0, family
,
6347 nft_notify_enqueue(skb
, report
, &net
->nft
.notify_list
);
6350 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
6352 EXPORT_SYMBOL_GPL(nft_obj_notify
);
6354 static void nf_tables_obj_notify(const struct nft_ctx
*ctx
,
6355 struct nft_object
*obj
, int event
)
6357 nft_obj_notify(ctx
->net
, ctx
->table
, obj
, ctx
->portid
, ctx
->seq
, event
,
6358 ctx
->family
, ctx
->report
, GFP_KERNEL
);
6364 void nft_register_flowtable_type(struct nf_flowtable_type
*type
)
6366 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
6367 list_add_tail_rcu(&type
->list
, &nf_tables_flowtables
);
6368 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
6370 EXPORT_SYMBOL_GPL(nft_register_flowtable_type
);
6372 void nft_unregister_flowtable_type(struct nf_flowtable_type
*type
)
6374 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
6375 list_del_rcu(&type
->list
);
6376 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
6378 EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type
);
6380 static const struct nla_policy nft_flowtable_policy
[NFTA_FLOWTABLE_MAX
+ 1] = {
6381 [NFTA_FLOWTABLE_TABLE
] = { .type
= NLA_STRING
,
6382 .len
= NFT_NAME_MAXLEN
- 1 },
6383 [NFTA_FLOWTABLE_NAME
] = { .type
= NLA_STRING
,
6384 .len
= NFT_NAME_MAXLEN
- 1 },
6385 [NFTA_FLOWTABLE_HOOK
] = { .type
= NLA_NESTED
},
6386 [NFTA_FLOWTABLE_HANDLE
] = { .type
= NLA_U64
},
6387 [NFTA_FLOWTABLE_FLAGS
] = { .type
= NLA_U32
},
6390 struct nft_flowtable
*nft_flowtable_lookup(const struct nft_table
*table
,
6391 const struct nlattr
*nla
, u8 genmask
)
6393 struct nft_flowtable
*flowtable
;
6395 list_for_each_entry_rcu(flowtable
, &table
->flowtables
, list
) {
6396 if (!nla_strcmp(nla
, flowtable
->name
) &&
6397 nft_active_genmask(flowtable
, genmask
))
6400 return ERR_PTR(-ENOENT
);
6402 EXPORT_SYMBOL_GPL(nft_flowtable_lookup
);
6404 void nf_tables_deactivate_flowtable(const struct nft_ctx
*ctx
,
6405 struct nft_flowtable
*flowtable
,
6406 enum nft_trans_phase phase
)
6409 case NFT_TRANS_PREPARE
:
6410 case NFT_TRANS_ABORT
:
6411 case NFT_TRANS_RELEASE
:
6418 EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable
);
6420 static struct nft_flowtable
*
6421 nft_flowtable_lookup_byhandle(const struct nft_table
*table
,
6422 const struct nlattr
*nla
, u8 genmask
)
6424 struct nft_flowtable
*flowtable
;
6426 list_for_each_entry(flowtable
, &table
->flowtables
, list
) {
6427 if (be64_to_cpu(nla_get_be64(nla
)) == flowtable
->handle
&&
6428 nft_active_genmask(flowtable
, genmask
))
6431 return ERR_PTR(-ENOENT
);
6434 struct nft_flowtable_hook
{
6437 struct list_head list
;
6440 static const struct nla_policy nft_flowtable_hook_policy
[NFTA_FLOWTABLE_HOOK_MAX
+ 1] = {
6441 [NFTA_FLOWTABLE_HOOK_NUM
] = { .type
= NLA_U32
},
6442 [NFTA_FLOWTABLE_HOOK_PRIORITY
] = { .type
= NLA_U32
},
6443 [NFTA_FLOWTABLE_HOOK_DEVS
] = { .type
= NLA_NESTED
},
6446 static int nft_flowtable_parse_hook(const struct nft_ctx
*ctx
,
6447 const struct nlattr
*attr
,
6448 struct nft_flowtable_hook
*flowtable_hook
,
6449 struct nft_flowtable
*flowtable
, bool add
)
6451 struct nlattr
*tb
[NFTA_FLOWTABLE_HOOK_MAX
+ 1];
6452 struct nft_hook
*hook
;
6453 int hooknum
, priority
;
6456 INIT_LIST_HEAD(&flowtable_hook
->list
);
6458 err
= nla_parse_nested_deprecated(tb
, NFTA_FLOWTABLE_HOOK_MAX
, attr
,
6459 nft_flowtable_hook_policy
, NULL
);
6464 if (!tb
[NFTA_FLOWTABLE_HOOK_NUM
] ||
6465 !tb
[NFTA_FLOWTABLE_HOOK_PRIORITY
])
6468 hooknum
= ntohl(nla_get_be32(tb
[NFTA_FLOWTABLE_HOOK_NUM
]));
6469 if (hooknum
!= NF_NETDEV_INGRESS
)
6472 priority
= ntohl(nla_get_be32(tb
[NFTA_FLOWTABLE_HOOK_PRIORITY
]));
6474 flowtable_hook
->priority
= priority
;
6475 flowtable_hook
->num
= hooknum
;
6477 if (tb
[NFTA_FLOWTABLE_HOOK_NUM
]) {
6478 hooknum
= ntohl(nla_get_be32(tb
[NFTA_FLOWTABLE_HOOK_NUM
]));
6479 if (hooknum
!= flowtable
->hooknum
)
6483 if (tb
[NFTA_FLOWTABLE_HOOK_PRIORITY
]) {
6484 priority
= ntohl(nla_get_be32(tb
[NFTA_FLOWTABLE_HOOK_PRIORITY
]));
6485 if (priority
!= flowtable
->data
.priority
)
6489 flowtable_hook
->priority
= flowtable
->data
.priority
;
6490 flowtable_hook
->num
= flowtable
->hooknum
;
6493 if (tb
[NFTA_FLOWTABLE_HOOK_DEVS
]) {
6494 err
= nf_tables_parse_netdev_hooks(ctx
->net
,
6495 tb
[NFTA_FLOWTABLE_HOOK_DEVS
],
6496 &flowtable_hook
->list
);
6501 list_for_each_entry(hook
, &flowtable_hook
->list
, list
) {
6502 hook
->ops
.pf
= NFPROTO_NETDEV
;
6503 hook
->ops
.hooknum
= flowtable_hook
->num
;
6504 hook
->ops
.priority
= flowtable_hook
->priority
;
6505 hook
->ops
.priv
= &flowtable
->data
;
6506 hook
->ops
.hook
= flowtable
->data
.type
->hook
;
6512 static const struct nf_flowtable_type
*__nft_flowtable_type_get(u8 family
)
6514 const struct nf_flowtable_type
*type
;
6516 list_for_each_entry(type
, &nf_tables_flowtables
, list
) {
6517 if (family
== type
->family
)
6523 static const struct nf_flowtable_type
*
6524 nft_flowtable_type_get(struct net
*net
, u8 family
)
6526 const struct nf_flowtable_type
*type
;
6528 type
= __nft_flowtable_type_get(family
);
6529 if (type
!= NULL
&& try_module_get(type
->owner
))
6532 lockdep_nfnl_nft_mutex_not_held();
6533 #ifdef CONFIG_MODULES
6535 if (nft_request_module(net
, "nf-flowtable-%u", family
) == -EAGAIN
)
6536 return ERR_PTR(-EAGAIN
);
6539 return ERR_PTR(-ENOENT
);
6542 /* Only called from error and netdev event paths. */
6543 static void nft_unregister_flowtable_hook(struct net
*net
,
6544 struct nft_flowtable
*flowtable
,
6545 struct nft_hook
*hook
)
6547 nf_unregister_net_hook(net
, &hook
->ops
);
6548 flowtable
->data
.type
->setup(&flowtable
->data
, hook
->ops
.dev
,
6552 static void nft_unregister_flowtable_net_hooks(struct net
*net
,
6553 struct list_head
*hook_list
)
6555 struct nft_hook
*hook
;
6557 list_for_each_entry(hook
, hook_list
, list
)
6558 nf_unregister_net_hook(net
, &hook
->ops
);
6561 static int nft_register_flowtable_net_hooks(struct net
*net
,
6562 struct nft_table
*table
,
6563 struct list_head
*hook_list
,
6564 struct nft_flowtable
*flowtable
)
6566 struct nft_hook
*hook
, *hook2
, *next
;
6567 struct nft_flowtable
*ft
;
6570 list_for_each_entry(hook
, hook_list
, list
) {
6571 list_for_each_entry(ft
, &table
->flowtables
, list
) {
6572 list_for_each_entry(hook2
, &ft
->hook_list
, list
) {
6573 if (hook
->ops
.dev
== hook2
->ops
.dev
&&
6574 hook
->ops
.pf
== hook2
->ops
.pf
) {
6576 goto err_unregister_net_hooks
;
6581 err
= flowtable
->data
.type
->setup(&flowtable
->data
,
6585 goto err_unregister_net_hooks
;
6587 err
= nf_register_net_hook(net
, &hook
->ops
);
6589 flowtable
->data
.type
->setup(&flowtable
->data
,
6592 goto err_unregister_net_hooks
;
6600 err_unregister_net_hooks
:
6601 list_for_each_entry_safe(hook
, next
, hook_list
, list
) {
6605 nft_unregister_flowtable_hook(net
, flowtable
, hook
);
6606 list_del_rcu(&hook
->list
);
6607 kfree_rcu(hook
, rcu
);
6613 static void nft_flowtable_hooks_destroy(struct list_head
*hook_list
)
6615 struct nft_hook
*hook
, *next
;
6617 list_for_each_entry_safe(hook
, next
, hook_list
, list
) {
6618 list_del_rcu(&hook
->list
);
6619 kfree_rcu(hook
, rcu
);
6623 static int nft_flowtable_update(struct nft_ctx
*ctx
, const struct nlmsghdr
*nlh
,
6624 struct nft_flowtable
*flowtable
)
6626 const struct nlattr
* const *nla
= ctx
->nla
;
6627 struct nft_flowtable_hook flowtable_hook
;
6628 struct nft_hook
*hook
, *next
;
6629 struct nft_trans
*trans
;
6630 bool unregister
= false;
6633 err
= nft_flowtable_parse_hook(ctx
, nla
[NFTA_FLOWTABLE_HOOK
],
6634 &flowtable_hook
, flowtable
, false);
6638 list_for_each_entry_safe(hook
, next
, &flowtable_hook
.list
, list
) {
6639 if (nft_hook_list_find(&flowtable
->hook_list
, hook
)) {
6640 list_del(&hook
->list
);
6645 err
= nft_register_flowtable_net_hooks(ctx
->net
, ctx
->table
,
6646 &flowtable_hook
.list
, flowtable
);
6648 goto err_flowtable_update_hook
;
6650 trans
= nft_trans_alloc(ctx
, NFT_MSG_NEWFLOWTABLE
,
6651 sizeof(struct nft_trans_flowtable
));
6655 goto err_flowtable_update_hook
;
6658 nft_trans_flowtable(trans
) = flowtable
;
6659 nft_trans_flowtable_update(trans
) = true;
6660 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans
));
6661 list_splice(&flowtable_hook
.list
, &nft_trans_flowtable_hooks(trans
));
6663 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
6667 err_flowtable_update_hook
:
6668 list_for_each_entry_safe(hook
, next
, &flowtable_hook
.list
, list
) {
6670 nft_unregister_flowtable_hook(ctx
->net
, flowtable
, hook
);
6671 list_del_rcu(&hook
->list
);
6672 kfree_rcu(hook
, rcu
);
6679 static int nf_tables_newflowtable(struct net
*net
, struct sock
*nlsk
,
6680 struct sk_buff
*skb
,
6681 const struct nlmsghdr
*nlh
,
6682 const struct nlattr
* const nla
[],
6683 struct netlink_ext_ack
*extack
)
6685 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
6686 struct nft_flowtable_hook flowtable_hook
;
6687 const struct nf_flowtable_type
*type
;
6688 u8 genmask
= nft_genmask_next(net
);
6689 int family
= nfmsg
->nfgen_family
;
6690 struct nft_flowtable
*flowtable
;
6691 struct nft_hook
*hook
, *next
;
6692 struct nft_table
*table
;
6696 if (!nla
[NFTA_FLOWTABLE_TABLE
] ||
6697 !nla
[NFTA_FLOWTABLE_NAME
] ||
6698 !nla
[NFTA_FLOWTABLE_HOOK
])
6701 table
= nft_table_lookup(net
, nla
[NFTA_FLOWTABLE_TABLE
], family
,
6703 if (IS_ERR(table
)) {
6704 NL_SET_BAD_ATTR(extack
, nla
[NFTA_FLOWTABLE_TABLE
]);
6705 return PTR_ERR(table
);
6708 flowtable
= nft_flowtable_lookup(table
, nla
[NFTA_FLOWTABLE_NAME
],
6710 if (IS_ERR(flowtable
)) {
6711 err
= PTR_ERR(flowtable
);
6712 if (err
!= -ENOENT
) {
6713 NL_SET_BAD_ATTR(extack
, nla
[NFTA_FLOWTABLE_NAME
]);
6717 if (nlh
->nlmsg_flags
& NLM_F_EXCL
) {
6718 NL_SET_BAD_ATTR(extack
, nla
[NFTA_FLOWTABLE_NAME
]);
6722 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
6724 return nft_flowtable_update(&ctx
, nlh
, flowtable
);
6727 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
6729 flowtable
= kzalloc(sizeof(*flowtable
), GFP_KERNEL
);
6733 flowtable
->table
= table
;
6734 flowtable
->handle
= nf_tables_alloc_handle(table
);
6735 INIT_LIST_HEAD(&flowtable
->hook_list
);
6737 flowtable
->name
= nla_strdup(nla
[NFTA_FLOWTABLE_NAME
], GFP_KERNEL
);
6738 if (!flowtable
->name
) {
6743 type
= nft_flowtable_type_get(net
, family
);
6745 err
= PTR_ERR(type
);
6749 if (nla
[NFTA_FLOWTABLE_FLAGS
]) {
6750 flowtable
->data
.flags
=
6751 ntohl(nla_get_be32(nla
[NFTA_FLOWTABLE_FLAGS
]));
6752 if (flowtable
->data
.flags
& ~NFT_FLOWTABLE_MASK
)
6756 write_pnet(&flowtable
->data
.net
, net
);
6757 flowtable
->data
.type
= type
;
6758 err
= type
->init(&flowtable
->data
);
6762 err
= nft_flowtable_parse_hook(&ctx
, nla
[NFTA_FLOWTABLE_HOOK
],
6763 &flowtable_hook
, flowtable
, true);
6767 list_splice(&flowtable_hook
.list
, &flowtable
->hook_list
);
6768 flowtable
->data
.priority
= flowtable_hook
.priority
;
6769 flowtable
->hooknum
= flowtable_hook
.num
;
6771 err
= nft_register_flowtable_net_hooks(ctx
.net
, table
,
6772 &flowtable
->hook_list
,
6775 nft_flowtable_hooks_destroy(&flowtable
->hook_list
);
6779 err
= nft_trans_flowtable_add(&ctx
, NFT_MSG_NEWFLOWTABLE
, flowtable
);
6783 list_add_tail_rcu(&flowtable
->list
, &table
->flowtables
);
6788 list_for_each_entry_safe(hook
, next
, &flowtable
->hook_list
, list
) {
6789 nft_unregister_flowtable_hook(net
, flowtable
, hook
);
6790 list_del_rcu(&hook
->list
);
6791 kfree_rcu(hook
, rcu
);
6794 flowtable
->data
.type
->free(&flowtable
->data
);
6796 module_put(type
->owner
);
6798 kfree(flowtable
->name
);
6804 static void nft_flowtable_hook_release(struct nft_flowtable_hook
*flowtable_hook
)
6806 struct nft_hook
*this, *next
;
6808 list_for_each_entry_safe(this, next
, &flowtable_hook
->list
, list
) {
6809 list_del(&this->list
);
6814 static int nft_delflowtable_hook(struct nft_ctx
*ctx
,
6815 struct nft_flowtable
*flowtable
)
6817 const struct nlattr
* const *nla
= ctx
->nla
;
6818 struct nft_flowtable_hook flowtable_hook
;
6819 struct nft_hook
*this, *hook
;
6820 struct nft_trans
*trans
;
6823 err
= nft_flowtable_parse_hook(ctx
, nla
[NFTA_FLOWTABLE_HOOK
],
6824 &flowtable_hook
, flowtable
, false);
6828 list_for_each_entry(this, &flowtable_hook
.list
, list
) {
6829 hook
= nft_hook_list_find(&flowtable
->hook_list
, this);
6832 goto err_flowtable_del_hook
;
6834 hook
->inactive
= true;
6837 trans
= nft_trans_alloc(ctx
, NFT_MSG_DELFLOWTABLE
,
6838 sizeof(struct nft_trans_flowtable
));
6841 goto err_flowtable_del_hook
;
6844 nft_trans_flowtable(trans
) = flowtable
;
6845 nft_trans_flowtable_update(trans
) = true;
6846 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans
));
6847 nft_flowtable_hook_release(&flowtable_hook
);
6849 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
6853 err_flowtable_del_hook
:
6854 list_for_each_entry(this, &flowtable_hook
.list
, list
) {
6855 hook
= nft_hook_list_find(&flowtable
->hook_list
, this);
6859 hook
->inactive
= false;
6861 nft_flowtable_hook_release(&flowtable_hook
);
6866 static int nf_tables_delflowtable(struct net
*net
, struct sock
*nlsk
,
6867 struct sk_buff
*skb
,
6868 const struct nlmsghdr
*nlh
,
6869 const struct nlattr
* const nla
[],
6870 struct netlink_ext_ack
*extack
)
6872 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
6873 u8 genmask
= nft_genmask_next(net
);
6874 int family
= nfmsg
->nfgen_family
;
6875 struct nft_flowtable
*flowtable
;
6876 const struct nlattr
*attr
;
6877 struct nft_table
*table
;
6880 if (!nla
[NFTA_FLOWTABLE_TABLE
] ||
6881 (!nla
[NFTA_FLOWTABLE_NAME
] &&
6882 !nla
[NFTA_FLOWTABLE_HANDLE
]))
6885 table
= nft_table_lookup(net
, nla
[NFTA_FLOWTABLE_TABLE
], family
,
6887 if (IS_ERR(table
)) {
6888 NL_SET_BAD_ATTR(extack
, nla
[NFTA_FLOWTABLE_TABLE
]);
6889 return PTR_ERR(table
);
6892 if (nla
[NFTA_FLOWTABLE_HANDLE
]) {
6893 attr
= nla
[NFTA_FLOWTABLE_HANDLE
];
6894 flowtable
= nft_flowtable_lookup_byhandle(table
, attr
, genmask
);
6896 attr
= nla
[NFTA_FLOWTABLE_NAME
];
6897 flowtable
= nft_flowtable_lookup(table
, attr
, genmask
);
6900 if (IS_ERR(flowtable
)) {
6901 NL_SET_BAD_ATTR(extack
, attr
);
6902 return PTR_ERR(flowtable
);
6905 nft_ctx_init(&ctx
, net
, skb
, nlh
, family
, table
, NULL
, nla
);
6907 if (nla
[NFTA_FLOWTABLE_HOOK
])
6908 return nft_delflowtable_hook(&ctx
, flowtable
);
6910 if (flowtable
->use
> 0) {
6911 NL_SET_BAD_ATTR(extack
, attr
);
6915 return nft_delflowtable(&ctx
, flowtable
);
6918 static int nf_tables_fill_flowtable_info(struct sk_buff
*skb
, struct net
*net
,
6919 u32 portid
, u32 seq
, int event
,
6920 u32 flags
, int family
,
6921 struct nft_flowtable
*flowtable
,
6922 struct list_head
*hook_list
)
6924 struct nlattr
*nest
, *nest_devs
;
6925 struct nfgenmsg
*nfmsg
;
6926 struct nft_hook
*hook
;
6927 struct nlmsghdr
*nlh
;
6929 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
6930 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
6932 goto nla_put_failure
;
6934 nfmsg
= nlmsg_data(nlh
);
6935 nfmsg
->nfgen_family
= family
;
6936 nfmsg
->version
= NFNETLINK_V0
;
6937 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
6939 if (nla_put_string(skb
, NFTA_FLOWTABLE_TABLE
, flowtable
->table
->name
) ||
6940 nla_put_string(skb
, NFTA_FLOWTABLE_NAME
, flowtable
->name
) ||
6941 nla_put_be32(skb
, NFTA_FLOWTABLE_USE
, htonl(flowtable
->use
)) ||
6942 nla_put_be64(skb
, NFTA_FLOWTABLE_HANDLE
, cpu_to_be64(flowtable
->handle
),
6943 NFTA_FLOWTABLE_PAD
) ||
6944 nla_put_be32(skb
, NFTA_FLOWTABLE_FLAGS
, htonl(flowtable
->data
.flags
)))
6945 goto nla_put_failure
;
6947 nest
= nla_nest_start_noflag(skb
, NFTA_FLOWTABLE_HOOK
);
6949 goto nla_put_failure
;
6950 if (nla_put_be32(skb
, NFTA_FLOWTABLE_HOOK_NUM
, htonl(flowtable
->hooknum
)) ||
6951 nla_put_be32(skb
, NFTA_FLOWTABLE_HOOK_PRIORITY
, htonl(flowtable
->data
.priority
)))
6952 goto nla_put_failure
;
6954 nest_devs
= nla_nest_start_noflag(skb
, NFTA_FLOWTABLE_HOOK_DEVS
);
6956 goto nla_put_failure
;
6958 list_for_each_entry_rcu(hook
, hook_list
, list
) {
6959 if (nla_put_string(skb
, NFTA_DEVICE_NAME
, hook
->ops
.dev
->name
))
6960 goto nla_put_failure
;
6962 nla_nest_end(skb
, nest_devs
);
6963 nla_nest_end(skb
, nest
);
6965 nlmsg_end(skb
, nlh
);
6969 nlmsg_trim(skb
, nlh
);
6973 struct nft_flowtable_filter
{
6977 static int nf_tables_dump_flowtable(struct sk_buff
*skb
,
6978 struct netlink_callback
*cb
)
6980 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
6981 struct nft_flowtable_filter
*filter
= cb
->data
;
6982 unsigned int idx
= 0, s_idx
= cb
->args
[0];
6983 struct net
*net
= sock_net(skb
->sk
);
6984 int family
= nfmsg
->nfgen_family
;
6985 struct nft_flowtable
*flowtable
;
6986 const struct nft_table
*table
;
6989 cb
->seq
= net
->nft
.base_seq
;
6991 list_for_each_entry_rcu(table
, &net
->nft
.tables
, list
) {
6992 if (family
!= NFPROTO_UNSPEC
&& family
!= table
->family
)
6995 list_for_each_entry_rcu(flowtable
, &table
->flowtables
, list
) {
6996 if (!nft_is_active(net
, flowtable
))
7001 memset(&cb
->args
[1], 0,
7002 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
7003 if (filter
&& filter
->table
&&
7004 strcmp(filter
->table
, table
->name
))
7007 if (nf_tables_fill_flowtable_info(skb
, net
, NETLINK_CB(cb
->skb
).portid
,
7009 NFT_MSG_NEWFLOWTABLE
,
7010 NLM_F_MULTI
| NLM_F_APPEND
,
7013 &flowtable
->hook_list
) < 0)
7016 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
7028 static int nf_tables_dump_flowtable_start(struct netlink_callback
*cb
)
7030 const struct nlattr
* const *nla
= cb
->data
;
7031 struct nft_flowtable_filter
*filter
= NULL
;
7033 if (nla
[NFTA_FLOWTABLE_TABLE
]) {
7034 filter
= kzalloc(sizeof(*filter
), GFP_ATOMIC
);
7038 filter
->table
= nla_strdup(nla
[NFTA_FLOWTABLE_TABLE
],
7040 if (!filter
->table
) {
7050 static int nf_tables_dump_flowtable_done(struct netlink_callback
*cb
)
7052 struct nft_flowtable_filter
*filter
= cb
->data
;
7057 kfree(filter
->table
);
7063 /* called with rcu_read_lock held */
7064 static int nf_tables_getflowtable(struct net
*net
, struct sock
*nlsk
,
7065 struct sk_buff
*skb
,
7066 const struct nlmsghdr
*nlh
,
7067 const struct nlattr
* const nla
[],
7068 struct netlink_ext_ack
*extack
)
7070 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
7071 u8 genmask
= nft_genmask_cur(net
);
7072 int family
= nfmsg
->nfgen_family
;
7073 struct nft_flowtable
*flowtable
;
7074 const struct nft_table
*table
;
7075 struct sk_buff
*skb2
;
7078 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
7079 struct netlink_dump_control c
= {
7080 .start
= nf_tables_dump_flowtable_start
,
7081 .dump
= nf_tables_dump_flowtable
,
7082 .done
= nf_tables_dump_flowtable_done
,
7083 .module
= THIS_MODULE
,
7084 .data
= (void *)nla
,
7087 return nft_netlink_dump_start_rcu(nlsk
, skb
, nlh
, &c
);
7090 if (!nla
[NFTA_FLOWTABLE_NAME
])
7093 table
= nft_table_lookup(net
, nla
[NFTA_FLOWTABLE_TABLE
], family
,
7096 return PTR_ERR(table
);
7098 flowtable
= nft_flowtable_lookup(table
, nla
[NFTA_FLOWTABLE_NAME
],
7100 if (IS_ERR(flowtable
))
7101 return PTR_ERR(flowtable
);
7103 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
7107 err
= nf_tables_fill_flowtable_info(skb2
, net
, NETLINK_CB(skb
).portid
,
7109 NFT_MSG_NEWFLOWTABLE
, 0, family
,
7110 flowtable
, &flowtable
->hook_list
);
7112 goto err_fill_flowtable_info
;
7114 return nfnetlink_unicast(skb2
, net
, NETLINK_CB(skb
).portid
);
7116 err_fill_flowtable_info
:
7121 static void nf_tables_flowtable_notify(struct nft_ctx
*ctx
,
7122 struct nft_flowtable
*flowtable
,
7123 struct list_head
*hook_list
,
7126 struct sk_buff
*skb
;
7128 char *buf
= kasprintf(GFP_KERNEL
, "%s:%llu;%s:%llu",
7129 flowtable
->table
->name
, flowtable
->table
->handle
,
7130 flowtable
->name
, flowtable
->handle
);
7132 audit_log_nfcfg(buf
,
7135 event
== NFT_MSG_NEWFLOWTABLE
?
7136 AUDIT_NFT_OP_FLOWTABLE_REGISTER
:
7137 AUDIT_NFT_OP_FLOWTABLE_UNREGISTER
,
7142 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
7145 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
7149 err
= nf_tables_fill_flowtable_info(skb
, ctx
->net
, ctx
->portid
,
7151 ctx
->family
, flowtable
, hook_list
);
7157 nft_notify_enqueue(skb
, ctx
->report
, &ctx
->net
->nft
.notify_list
);
7160 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
7163 static void nf_tables_flowtable_destroy(struct nft_flowtable
*flowtable
)
7165 struct nft_hook
*hook
, *next
;
7167 flowtable
->data
.type
->free(&flowtable
->data
);
7168 list_for_each_entry_safe(hook
, next
, &flowtable
->hook_list
, list
) {
7169 flowtable
->data
.type
->setup(&flowtable
->data
, hook
->ops
.dev
,
7171 list_del_rcu(&hook
->list
);
7174 kfree(flowtable
->name
);
7175 module_put(flowtable
->data
.type
->owner
);
7179 static int nf_tables_fill_gen_info(struct sk_buff
*skb
, struct net
*net
,
7180 u32 portid
, u32 seq
)
7182 struct nlmsghdr
*nlh
;
7183 struct nfgenmsg
*nfmsg
;
7184 char buf
[TASK_COMM_LEN
];
7185 int event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, NFT_MSG_NEWGEN
);
7187 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), 0);
7189 goto nla_put_failure
;
7191 nfmsg
= nlmsg_data(nlh
);
7192 nfmsg
->nfgen_family
= AF_UNSPEC
;
7193 nfmsg
->version
= NFNETLINK_V0
;
7194 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
7196 if (nla_put_be32(skb
, NFTA_GEN_ID
, htonl(net
->nft
.base_seq
)) ||
7197 nla_put_be32(skb
, NFTA_GEN_PROC_PID
, htonl(task_pid_nr(current
))) ||
7198 nla_put_string(skb
, NFTA_GEN_PROC_NAME
, get_task_comm(buf
, current
)))
7199 goto nla_put_failure
;
7201 nlmsg_end(skb
, nlh
);
7205 nlmsg_trim(skb
, nlh
);
7209 static void nft_flowtable_event(unsigned long event
, struct net_device
*dev
,
7210 struct nft_flowtable
*flowtable
)
7212 struct nft_hook
*hook
;
7214 list_for_each_entry(hook
, &flowtable
->hook_list
, list
) {
7215 if (hook
->ops
.dev
!= dev
)
7218 /* flow_offload_netdev_event() cleans up entries for us. */
7219 nft_unregister_flowtable_hook(dev_net(dev
), flowtable
, hook
);
7220 list_del_rcu(&hook
->list
);
7221 kfree_rcu(hook
, rcu
);
7226 static int nf_tables_flowtable_event(struct notifier_block
*this,
7227 unsigned long event
, void *ptr
)
7229 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
7230 struct nft_flowtable
*flowtable
;
7231 struct nft_table
*table
;
7234 if (event
!= NETDEV_UNREGISTER
)
7238 mutex_lock(&net
->nft
.commit_mutex
);
7239 list_for_each_entry(table
, &net
->nft
.tables
, list
) {
7240 list_for_each_entry(flowtable
, &table
->flowtables
, list
) {
7241 nft_flowtable_event(event
, dev
, flowtable
);
7244 mutex_unlock(&net
->nft
.commit_mutex
);
7249 static struct notifier_block nf_tables_flowtable_notifier
= {
7250 .notifier_call
= nf_tables_flowtable_event
,
7253 static void nf_tables_gen_notify(struct net
*net
, struct sk_buff
*skb
,
7256 struct nlmsghdr
*nlh
= nlmsg_hdr(skb
);
7257 struct sk_buff
*skb2
;
7260 audit_log_nfcfg("?:0;?:0", 0, net
->nft
.base_seq
,
7261 AUDIT_NFT_OP_GEN_REGISTER
, GFP_KERNEL
);
7263 if (!nlmsg_report(nlh
) &&
7264 !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
7267 skb2
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
7271 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
7278 nfnetlink_send(skb2
, net
, NETLINK_CB(skb
).portid
, NFNLGRP_NFTABLES
,
7279 nlmsg_report(nlh
), GFP_KERNEL
);
7282 nfnetlink_set_err(net
, NETLINK_CB(skb
).portid
, NFNLGRP_NFTABLES
,
7286 static int nf_tables_getgen(struct net
*net
, struct sock
*nlsk
,
7287 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
7288 const struct nlattr
* const nla
[],
7289 struct netlink_ext_ack
*extack
)
7291 struct sk_buff
*skb2
;
7294 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
7298 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
7301 goto err_fill_gen_info
;
7303 return nfnetlink_unicast(skb2
, net
, NETLINK_CB(skb
).portid
);
7310 static const struct nfnl_callback nf_tables_cb
[NFT_MSG_MAX
] = {
7311 [NFT_MSG_NEWTABLE
] = {
7312 .call_batch
= nf_tables_newtable
,
7313 .attr_count
= NFTA_TABLE_MAX
,
7314 .policy
= nft_table_policy
,
7316 [NFT_MSG_GETTABLE
] = {
7317 .call_rcu
= nf_tables_gettable
,
7318 .attr_count
= NFTA_TABLE_MAX
,
7319 .policy
= nft_table_policy
,
7321 [NFT_MSG_DELTABLE
] = {
7322 .call_batch
= nf_tables_deltable
,
7323 .attr_count
= NFTA_TABLE_MAX
,
7324 .policy
= nft_table_policy
,
7326 [NFT_MSG_NEWCHAIN
] = {
7327 .call_batch
= nf_tables_newchain
,
7328 .attr_count
= NFTA_CHAIN_MAX
,
7329 .policy
= nft_chain_policy
,
7331 [NFT_MSG_GETCHAIN
] = {
7332 .call_rcu
= nf_tables_getchain
,
7333 .attr_count
= NFTA_CHAIN_MAX
,
7334 .policy
= nft_chain_policy
,
7336 [NFT_MSG_DELCHAIN
] = {
7337 .call_batch
= nf_tables_delchain
,
7338 .attr_count
= NFTA_CHAIN_MAX
,
7339 .policy
= nft_chain_policy
,
7341 [NFT_MSG_NEWRULE
] = {
7342 .call_batch
= nf_tables_newrule
,
7343 .attr_count
= NFTA_RULE_MAX
,
7344 .policy
= nft_rule_policy
,
7346 [NFT_MSG_GETRULE
] = {
7347 .call_rcu
= nf_tables_getrule
,
7348 .attr_count
= NFTA_RULE_MAX
,
7349 .policy
= nft_rule_policy
,
7351 [NFT_MSG_DELRULE
] = {
7352 .call_batch
= nf_tables_delrule
,
7353 .attr_count
= NFTA_RULE_MAX
,
7354 .policy
= nft_rule_policy
,
7356 [NFT_MSG_NEWSET
] = {
7357 .call_batch
= nf_tables_newset
,
7358 .attr_count
= NFTA_SET_MAX
,
7359 .policy
= nft_set_policy
,
7361 [NFT_MSG_GETSET
] = {
7362 .call_rcu
= nf_tables_getset
,
7363 .attr_count
= NFTA_SET_MAX
,
7364 .policy
= nft_set_policy
,
7366 [NFT_MSG_DELSET
] = {
7367 .call_batch
= nf_tables_delset
,
7368 .attr_count
= NFTA_SET_MAX
,
7369 .policy
= nft_set_policy
,
7371 [NFT_MSG_NEWSETELEM
] = {
7372 .call_batch
= nf_tables_newsetelem
,
7373 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
7374 .policy
= nft_set_elem_list_policy
,
7376 [NFT_MSG_GETSETELEM
] = {
7377 .call_rcu
= nf_tables_getsetelem
,
7378 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
7379 .policy
= nft_set_elem_list_policy
,
7381 [NFT_MSG_DELSETELEM
] = {
7382 .call_batch
= nf_tables_delsetelem
,
7383 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
7384 .policy
= nft_set_elem_list_policy
,
7386 [NFT_MSG_GETGEN
] = {
7387 .call_rcu
= nf_tables_getgen
,
7389 [NFT_MSG_NEWOBJ
] = {
7390 .call_batch
= nf_tables_newobj
,
7391 .attr_count
= NFTA_OBJ_MAX
,
7392 .policy
= nft_obj_policy
,
7394 [NFT_MSG_GETOBJ
] = {
7395 .call_rcu
= nf_tables_getobj
,
7396 .attr_count
= NFTA_OBJ_MAX
,
7397 .policy
= nft_obj_policy
,
7399 [NFT_MSG_DELOBJ
] = {
7400 .call_batch
= nf_tables_delobj
,
7401 .attr_count
= NFTA_OBJ_MAX
,
7402 .policy
= nft_obj_policy
,
7404 [NFT_MSG_GETOBJ_RESET
] = {
7405 .call_rcu
= nf_tables_getobj
,
7406 .attr_count
= NFTA_OBJ_MAX
,
7407 .policy
= nft_obj_policy
,
7409 [NFT_MSG_NEWFLOWTABLE
] = {
7410 .call_batch
= nf_tables_newflowtable
,
7411 .attr_count
= NFTA_FLOWTABLE_MAX
,
7412 .policy
= nft_flowtable_policy
,
7414 [NFT_MSG_GETFLOWTABLE
] = {
7415 .call_rcu
= nf_tables_getflowtable
,
7416 .attr_count
= NFTA_FLOWTABLE_MAX
,
7417 .policy
= nft_flowtable_policy
,
7419 [NFT_MSG_DELFLOWTABLE
] = {
7420 .call_batch
= nf_tables_delflowtable
,
7421 .attr_count
= NFTA_FLOWTABLE_MAX
,
7422 .policy
= nft_flowtable_policy
,
7426 static int nf_tables_validate(struct net
*net
)
7428 struct nft_table
*table
;
7430 switch (net
->nft
.validate_state
) {
7431 case NFT_VALIDATE_SKIP
:
7433 case NFT_VALIDATE_NEED
:
7434 nft_validate_state_update(net
, NFT_VALIDATE_DO
);
7436 case NFT_VALIDATE_DO
:
7437 list_for_each_entry(table
, &net
->nft
.tables
, list
) {
7438 if (nft_table_validate(net
, table
) < 0)
7447 /* a drop policy has to be deferred until all rules have been activated,
7448 * otherwise a large ruleset that contains a drop-policy base chain will
7449 * cause all packets to get dropped until the full transaction has been
7452 * We defer the drop policy until the transaction has been finalized.
7454 static void nft_chain_commit_drop_policy(struct nft_trans
*trans
)
7456 struct nft_base_chain
*basechain
;
7458 if (nft_trans_chain_policy(trans
) != NF_DROP
)
7461 if (!nft_is_base_chain(trans
->ctx
.chain
))
7464 basechain
= nft_base_chain(trans
->ctx
.chain
);
7465 basechain
->policy
= NF_DROP
;
7468 static void nft_chain_commit_update(struct nft_trans
*trans
)
7470 struct nft_base_chain
*basechain
;
7472 if (nft_trans_chain_name(trans
)) {
7473 rhltable_remove(&trans
->ctx
.table
->chains_ht
,
7474 &trans
->ctx
.chain
->rhlhead
,
7475 nft_chain_ht_params
);
7476 swap(trans
->ctx
.chain
->name
, nft_trans_chain_name(trans
));
7477 rhltable_insert_key(&trans
->ctx
.table
->chains_ht
,
7478 trans
->ctx
.chain
->name
,
7479 &trans
->ctx
.chain
->rhlhead
,
7480 nft_chain_ht_params
);
7483 if (!nft_is_base_chain(trans
->ctx
.chain
))
7486 nft_chain_stats_replace(trans
);
7488 basechain
= nft_base_chain(trans
->ctx
.chain
);
7490 switch (nft_trans_chain_policy(trans
)) {
7493 basechain
->policy
= nft_trans_chain_policy(trans
);
7498 static void nft_obj_commit_update(struct nft_trans
*trans
)
7500 struct nft_object
*newobj
;
7501 struct nft_object
*obj
;
7503 obj
= nft_trans_obj(trans
);
7504 newobj
= nft_trans_obj_newobj(trans
);
7506 if (obj
->ops
->update
)
7507 obj
->ops
->update(obj
, newobj
);
7512 static void nft_commit_release(struct nft_trans
*trans
)
7514 switch (trans
->msg_type
) {
7515 case NFT_MSG_DELTABLE
:
7516 nf_tables_table_destroy(&trans
->ctx
);
7518 case NFT_MSG_NEWCHAIN
:
7519 free_percpu(nft_trans_chain_stats(trans
));
7520 kfree(nft_trans_chain_name(trans
));
7522 case NFT_MSG_DELCHAIN
:
7523 nf_tables_chain_destroy(&trans
->ctx
);
7525 case NFT_MSG_DELRULE
:
7526 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
7528 case NFT_MSG_DELSET
:
7529 nft_set_destroy(&trans
->ctx
, nft_trans_set(trans
));
7531 case NFT_MSG_DELSETELEM
:
7532 nf_tables_set_elem_destroy(&trans
->ctx
,
7533 nft_trans_elem_set(trans
),
7534 nft_trans_elem(trans
).priv
);
7536 case NFT_MSG_DELOBJ
:
7537 nft_obj_destroy(&trans
->ctx
, nft_trans_obj(trans
));
7539 case NFT_MSG_DELFLOWTABLE
:
7540 if (nft_trans_flowtable_update(trans
))
7541 nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans
));
7543 nf_tables_flowtable_destroy(nft_trans_flowtable(trans
));
7548 put_net(trans
->ctx
.net
);
7553 static void nf_tables_trans_destroy_work(struct work_struct
*w
)
7555 struct nft_trans
*trans
, *next
;
7558 spin_lock(&nf_tables_destroy_list_lock
);
7559 list_splice_init(&nf_tables_destroy_list
, &head
);
7560 spin_unlock(&nf_tables_destroy_list_lock
);
7562 if (list_empty(&head
))
7567 list_for_each_entry_safe(trans
, next
, &head
, list
) {
7568 list_del(&trans
->list
);
7569 nft_commit_release(trans
);
7573 void nf_tables_trans_destroy_flush_work(void)
7575 flush_work(&trans_destroy_work
);
7577 EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work
);
7579 static int nf_tables_commit_chain_prepare(struct net
*net
, struct nft_chain
*chain
)
7581 struct nft_rule
*rule
;
7582 unsigned int alloc
= 0;
7585 /* already handled or inactive chain? */
7586 if (chain
->rules_next
|| !nft_is_active_next(net
, chain
))
7589 rule
= list_entry(&chain
->rules
, struct nft_rule
, list
);
7592 list_for_each_entry_continue(rule
, &chain
->rules
, list
) {
7593 if (nft_is_active_next(net
, rule
))
7597 chain
->rules_next
= nf_tables_chain_alloc_rules(chain
, alloc
);
7598 if (!chain
->rules_next
)
7601 list_for_each_entry_continue(rule
, &chain
->rules
, list
) {
7602 if (nft_is_active_next(net
, rule
))
7603 chain
->rules_next
[i
++] = rule
;
7606 chain
->rules_next
[i
] = NULL
;
7610 static void nf_tables_commit_chain_prepare_cancel(struct net
*net
)
7612 struct nft_trans
*trans
, *next
;
7614 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
7615 struct nft_chain
*chain
= trans
->ctx
.chain
;
7617 if (trans
->msg_type
== NFT_MSG_NEWRULE
||
7618 trans
->msg_type
== NFT_MSG_DELRULE
) {
7619 kvfree(chain
->rules_next
);
7620 chain
->rules_next
= NULL
;
7625 static void __nf_tables_commit_chain_free_rules_old(struct rcu_head
*h
)
7627 struct nft_rules_old
*o
= container_of(h
, struct nft_rules_old
, h
);
7632 static void nf_tables_commit_chain_free_rules_old(struct nft_rule
**rules
)
7634 struct nft_rule
**r
= rules
;
7635 struct nft_rules_old
*old
;
7640 r
++; /* rcu_head is after end marker */
7644 call_rcu(&old
->h
, __nf_tables_commit_chain_free_rules_old
);
7647 static void nf_tables_commit_chain(struct net
*net
, struct nft_chain
*chain
)
7649 struct nft_rule
**g0
, **g1
;
7652 next_genbit
= nft_gencursor_next(net
);
7654 g0
= rcu_dereference_protected(chain
->rules_gen_0
,
7655 lockdep_commit_lock_is_held(net
));
7656 g1
= rcu_dereference_protected(chain
->rules_gen_1
,
7657 lockdep_commit_lock_is_held(net
));
7659 /* No changes to this chain? */
7660 if (chain
->rules_next
== NULL
) {
7661 /* chain had no change in last or next generation */
7665 * chain had no change in this generation; make sure next
7666 * one uses same rules as current generation.
7669 rcu_assign_pointer(chain
->rules_gen_1
, g0
);
7670 nf_tables_commit_chain_free_rules_old(g1
);
7672 rcu_assign_pointer(chain
->rules_gen_0
, g1
);
7673 nf_tables_commit_chain_free_rules_old(g0
);
7680 rcu_assign_pointer(chain
->rules_gen_1
, chain
->rules_next
);
7682 rcu_assign_pointer(chain
->rules_gen_0
, chain
->rules_next
);
7684 chain
->rules_next
= NULL
;
7690 nf_tables_commit_chain_free_rules_old(g1
);
7692 nf_tables_commit_chain_free_rules_old(g0
);
7695 static void nft_obj_del(struct nft_object
*obj
)
7697 rhltable_remove(&nft_objname_ht
, &obj
->rhlhead
, nft_objname_ht_params
);
7698 list_del_rcu(&obj
->list
);
7701 void nft_chain_del(struct nft_chain
*chain
)
7703 struct nft_table
*table
= chain
->table
;
7705 WARN_ON_ONCE(rhltable_remove(&table
->chains_ht
, &chain
->rhlhead
,
7706 nft_chain_ht_params
));
7707 list_del_rcu(&chain
->list
);
7710 static void nft_flowtable_hooks_del(struct nft_flowtable
*flowtable
,
7711 struct list_head
*hook_list
)
7713 struct nft_hook
*hook
, *next
;
7715 list_for_each_entry_safe(hook
, next
, &flowtable
->hook_list
, list
) {
7717 list_move(&hook
->list
, hook_list
);
7721 static void nf_tables_module_autoload_cleanup(struct net
*net
)
7723 struct nft_module_request
*req
, *next
;
7725 WARN_ON_ONCE(!list_empty(&net
->nft
.commit_list
));
7726 list_for_each_entry_safe(req
, next
, &net
->nft
.module_list
, list
) {
7727 WARN_ON_ONCE(!req
->done
);
7728 list_del(&req
->list
);
7733 static void nf_tables_commit_release(struct net
*net
)
7735 struct nft_trans
*trans
;
7737 /* all side effects have to be made visible.
7738 * For example, if a chain named 'foo' has been deleted, a
7739 * new transaction must not find it anymore.
7741 * Memory reclaim happens asynchronously from work queue
7742 * to prevent expensive synchronize_rcu() in commit phase.
7744 if (list_empty(&net
->nft
.commit_list
)) {
7745 nf_tables_module_autoload_cleanup(net
);
7746 mutex_unlock(&net
->nft
.commit_mutex
);
7750 trans
= list_last_entry(&net
->nft
.commit_list
,
7751 struct nft_trans
, list
);
7752 get_net(trans
->ctx
.net
);
7753 WARN_ON_ONCE(trans
->put_net
);
7755 trans
->put_net
= true;
7756 spin_lock(&nf_tables_destroy_list_lock
);
7757 list_splice_tail_init(&net
->nft
.commit_list
, &nf_tables_destroy_list
);
7758 spin_unlock(&nf_tables_destroy_list_lock
);
7760 nf_tables_module_autoload_cleanup(net
);
7761 schedule_work(&trans_destroy_work
);
7763 mutex_unlock(&net
->nft
.commit_mutex
);
7766 static void nft_commit_notify(struct net
*net
, u32 portid
)
7768 struct sk_buff
*batch_skb
= NULL
, *nskb
, *skb
;
7769 unsigned char *data
;
7772 list_for_each_entry_safe(skb
, nskb
, &net
->nft
.notify_list
, list
) {
7776 len
= NLMSG_GOODSIZE
- skb
->len
;
7777 list_del(&skb
->list
);
7781 if (len
> 0 && NFT_CB(skb
).report
== NFT_CB(batch_skb
).report
) {
7782 data
= skb_put(batch_skb
, skb
->len
);
7783 memcpy(data
, skb
->data
, skb
->len
);
7784 list_del(&skb
->list
);
7788 nfnetlink_send(batch_skb
, net
, portid
, NFNLGRP_NFTABLES
,
7789 NFT_CB(batch_skb
).report
, GFP_KERNEL
);
7794 nfnetlink_send(batch_skb
, net
, portid
, NFNLGRP_NFTABLES
,
7795 NFT_CB(batch_skb
).report
, GFP_KERNEL
);
7798 WARN_ON_ONCE(!list_empty(&net
->nft
.notify_list
));
7801 static int nf_tables_commit(struct net
*net
, struct sk_buff
*skb
)
7803 struct nft_trans
*trans
, *next
;
7804 struct nft_trans_elem
*te
;
7805 struct nft_chain
*chain
;
7806 struct nft_table
*table
;
7809 if (list_empty(&net
->nft
.commit_list
)) {
7810 mutex_unlock(&net
->nft
.commit_mutex
);
7814 /* 0. Validate ruleset, otherwise roll back for error reporting. */
7815 if (nf_tables_validate(net
) < 0)
7818 err
= nft_flow_rule_offload_commit(net
);
7822 /* 1. Allocate space for next generation rules_gen_X[] */
7823 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
7826 if (trans
->msg_type
== NFT_MSG_NEWRULE
||
7827 trans
->msg_type
== NFT_MSG_DELRULE
) {
7828 chain
= trans
->ctx
.chain
;
7830 ret
= nf_tables_commit_chain_prepare(net
, chain
);
7832 nf_tables_commit_chain_prepare_cancel(net
);
7838 /* step 2. Make rules_gen_X visible to packet path */
7839 list_for_each_entry(table
, &net
->nft
.tables
, list
) {
7840 list_for_each_entry(chain
, &table
->chains
, list
)
7841 nf_tables_commit_chain(net
, chain
);
7845 * Bump generation counter, invalidate any dump in progress.
7846 * Cannot fail after this point.
7848 while (++net
->nft
.base_seq
== 0);
7850 /* step 3. Start new generation, rules_gen_X now in use. */
7851 net
->nft
.gencursor
= nft_gencursor_next(net
);
7853 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
7854 switch (trans
->msg_type
) {
7855 case NFT_MSG_NEWTABLE
:
7856 if (nft_trans_table_update(trans
)) {
7857 if (!nft_trans_table_enable(trans
)) {
7858 nf_tables_table_disable(net
,
7860 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
7863 nft_clear(net
, trans
->ctx
.table
);
7865 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_NEWTABLE
);
7866 nft_trans_destroy(trans
);
7868 case NFT_MSG_DELTABLE
:
7869 list_del_rcu(&trans
->ctx
.table
->list
);
7870 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_DELTABLE
);
7872 case NFT_MSG_NEWCHAIN
:
7873 if (nft_trans_chain_update(trans
)) {
7874 nft_chain_commit_update(trans
);
7875 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_NEWCHAIN
);
7876 /* trans destroyed after rcu grace period */
7878 nft_chain_commit_drop_policy(trans
);
7879 nft_clear(net
, trans
->ctx
.chain
);
7880 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_NEWCHAIN
);
7881 nft_trans_destroy(trans
);
7884 case NFT_MSG_DELCHAIN
:
7885 nft_chain_del(trans
->ctx
.chain
);
7886 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_DELCHAIN
);
7887 nf_tables_unregister_hook(trans
->ctx
.net
,
7891 case NFT_MSG_NEWRULE
:
7892 nft_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
7893 nf_tables_rule_notify(&trans
->ctx
,
7894 nft_trans_rule(trans
),
7896 nft_trans_destroy(trans
);
7898 case NFT_MSG_DELRULE
:
7899 list_del_rcu(&nft_trans_rule(trans
)->list
);
7900 nf_tables_rule_notify(&trans
->ctx
,
7901 nft_trans_rule(trans
),
7903 nft_rule_expr_deactivate(&trans
->ctx
,
7904 nft_trans_rule(trans
),
7907 case NFT_MSG_NEWSET
:
7908 nft_clear(net
, nft_trans_set(trans
));
7909 /* This avoids hitting -EBUSY when deleting the table
7910 * from the transaction.
7912 if (nft_set_is_anonymous(nft_trans_set(trans
)) &&
7913 !list_empty(&nft_trans_set(trans
)->bindings
))
7914 trans
->ctx
.table
->use
--;
7916 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
7917 NFT_MSG_NEWSET
, GFP_KERNEL
);
7918 nft_trans_destroy(trans
);
7920 case NFT_MSG_DELSET
:
7921 list_del_rcu(&nft_trans_set(trans
)->list
);
7922 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
7923 NFT_MSG_DELSET
, GFP_KERNEL
);
7925 case NFT_MSG_NEWSETELEM
:
7926 te
= (struct nft_trans_elem
*)trans
->data
;
7928 te
->set
->ops
->activate(net
, te
->set
, &te
->elem
);
7929 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
7931 NFT_MSG_NEWSETELEM
, 0);
7932 nft_trans_destroy(trans
);
7934 case NFT_MSG_DELSETELEM
:
7935 te
= (struct nft_trans_elem
*)trans
->data
;
7937 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
7939 NFT_MSG_DELSETELEM
, 0);
7940 te
->set
->ops
->remove(net
, te
->set
, &te
->elem
);
7941 atomic_dec(&te
->set
->nelems
);
7944 case NFT_MSG_NEWOBJ
:
7945 if (nft_trans_obj_update(trans
)) {
7946 nft_obj_commit_update(trans
);
7947 nf_tables_obj_notify(&trans
->ctx
,
7948 nft_trans_obj(trans
),
7951 nft_clear(net
, nft_trans_obj(trans
));
7952 nf_tables_obj_notify(&trans
->ctx
,
7953 nft_trans_obj(trans
),
7955 nft_trans_destroy(trans
);
7958 case NFT_MSG_DELOBJ
:
7959 nft_obj_del(nft_trans_obj(trans
));
7960 nf_tables_obj_notify(&trans
->ctx
, nft_trans_obj(trans
),
7963 case NFT_MSG_NEWFLOWTABLE
:
7964 if (nft_trans_flowtable_update(trans
)) {
7965 nf_tables_flowtable_notify(&trans
->ctx
,
7966 nft_trans_flowtable(trans
),
7967 &nft_trans_flowtable_hooks(trans
),
7968 NFT_MSG_NEWFLOWTABLE
);
7969 list_splice(&nft_trans_flowtable_hooks(trans
),
7970 &nft_trans_flowtable(trans
)->hook_list
);
7972 nft_clear(net
, nft_trans_flowtable(trans
));
7973 nf_tables_flowtable_notify(&trans
->ctx
,
7974 nft_trans_flowtable(trans
),
7975 &nft_trans_flowtable(trans
)->hook_list
,
7976 NFT_MSG_NEWFLOWTABLE
);
7978 nft_trans_destroy(trans
);
7980 case NFT_MSG_DELFLOWTABLE
:
7981 if (nft_trans_flowtable_update(trans
)) {
7982 nft_flowtable_hooks_del(nft_trans_flowtable(trans
),
7983 &nft_trans_flowtable_hooks(trans
));
7984 nf_tables_flowtable_notify(&trans
->ctx
,
7985 nft_trans_flowtable(trans
),
7986 &nft_trans_flowtable_hooks(trans
),
7987 NFT_MSG_DELFLOWTABLE
);
7988 nft_unregister_flowtable_net_hooks(net
,
7989 &nft_trans_flowtable_hooks(trans
));
7991 list_del_rcu(&nft_trans_flowtable(trans
)->list
);
7992 nf_tables_flowtable_notify(&trans
->ctx
,
7993 nft_trans_flowtable(trans
),
7994 &nft_trans_flowtable(trans
)->hook_list
,
7995 NFT_MSG_DELFLOWTABLE
);
7996 nft_unregister_flowtable_net_hooks(net
,
7997 &nft_trans_flowtable(trans
)->hook_list
);
8003 nft_commit_notify(net
, NETLINK_CB(skb
).portid
);
8004 nf_tables_gen_notify(net
, skb
, NFT_MSG_NEWGEN
);
8005 nf_tables_commit_release(net
);
8010 static void nf_tables_module_autoload(struct net
*net
)
8012 struct nft_module_request
*req
, *next
;
8013 LIST_HEAD(module_list
);
8015 list_splice_init(&net
->nft
.module_list
, &module_list
);
8016 mutex_unlock(&net
->nft
.commit_mutex
);
8017 list_for_each_entry_safe(req
, next
, &module_list
, list
) {
8018 request_module("%s", req
->module
);
8021 mutex_lock(&net
->nft
.commit_mutex
);
8022 list_splice(&module_list
, &net
->nft
.module_list
);
8025 static void nf_tables_abort_release(struct nft_trans
*trans
)
8027 switch (trans
->msg_type
) {
8028 case NFT_MSG_NEWTABLE
:
8029 nf_tables_table_destroy(&trans
->ctx
);
8031 case NFT_MSG_NEWCHAIN
:
8032 nf_tables_chain_destroy(&trans
->ctx
);
8034 case NFT_MSG_NEWRULE
:
8035 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
8037 case NFT_MSG_NEWSET
:
8038 nft_set_destroy(&trans
->ctx
, nft_trans_set(trans
));
8040 case NFT_MSG_NEWSETELEM
:
8041 nft_set_elem_destroy(nft_trans_elem_set(trans
),
8042 nft_trans_elem(trans
).priv
, true);
8044 case NFT_MSG_NEWOBJ
:
8045 nft_obj_destroy(&trans
->ctx
, nft_trans_obj(trans
));
8047 case NFT_MSG_NEWFLOWTABLE
:
8048 if (nft_trans_flowtable_update(trans
))
8049 nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans
));
8051 nf_tables_flowtable_destroy(nft_trans_flowtable(trans
));
8057 static int __nf_tables_abort(struct net
*net
, enum nfnl_abort_action action
)
8059 struct nft_trans
*trans
, *next
;
8060 struct nft_trans_elem
*te
;
8061 struct nft_hook
*hook
;
8063 if (action
== NFNL_ABORT_VALIDATE
&&
8064 nf_tables_validate(net
) < 0)
8067 list_for_each_entry_safe_reverse(trans
, next
, &net
->nft
.commit_list
,
8069 switch (trans
->msg_type
) {
8070 case NFT_MSG_NEWTABLE
:
8071 if (nft_trans_table_update(trans
)) {
8072 if (nft_trans_table_enable(trans
)) {
8073 nf_tables_table_disable(net
,
8075 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
8077 nft_trans_destroy(trans
);
8079 list_del_rcu(&trans
->ctx
.table
->list
);
8082 case NFT_MSG_DELTABLE
:
8083 nft_clear(trans
->ctx
.net
, trans
->ctx
.table
);
8084 nft_trans_destroy(trans
);
8086 case NFT_MSG_NEWCHAIN
:
8087 if (nft_trans_chain_update(trans
)) {
8088 free_percpu(nft_trans_chain_stats(trans
));
8089 kfree(nft_trans_chain_name(trans
));
8090 nft_trans_destroy(trans
);
8092 if (nft_chain_is_bound(trans
->ctx
.chain
)) {
8093 nft_trans_destroy(trans
);
8096 trans
->ctx
.table
->use
--;
8097 nft_chain_del(trans
->ctx
.chain
);
8098 nf_tables_unregister_hook(trans
->ctx
.net
,
8103 case NFT_MSG_DELCHAIN
:
8104 trans
->ctx
.table
->use
++;
8105 nft_clear(trans
->ctx
.net
, trans
->ctx
.chain
);
8106 nft_trans_destroy(trans
);
8108 case NFT_MSG_NEWRULE
:
8109 trans
->ctx
.chain
->use
--;
8110 list_del_rcu(&nft_trans_rule(trans
)->list
);
8111 nft_rule_expr_deactivate(&trans
->ctx
,
8112 nft_trans_rule(trans
),
8115 case NFT_MSG_DELRULE
:
8116 trans
->ctx
.chain
->use
++;
8117 nft_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
8118 nft_rule_expr_activate(&trans
->ctx
, nft_trans_rule(trans
));
8119 nft_trans_destroy(trans
);
8121 case NFT_MSG_NEWSET
:
8122 trans
->ctx
.table
->use
--;
8123 if (nft_trans_set_bound(trans
)) {
8124 nft_trans_destroy(trans
);
8127 list_del_rcu(&nft_trans_set(trans
)->list
);
8129 case NFT_MSG_DELSET
:
8130 trans
->ctx
.table
->use
++;
8131 nft_clear(trans
->ctx
.net
, nft_trans_set(trans
));
8132 nft_trans_destroy(trans
);
8134 case NFT_MSG_NEWSETELEM
:
8135 if (nft_trans_elem_set_bound(trans
)) {
8136 nft_trans_destroy(trans
);
8139 te
= (struct nft_trans_elem
*)trans
->data
;
8140 te
->set
->ops
->remove(net
, te
->set
, &te
->elem
);
8141 atomic_dec(&te
->set
->nelems
);
8143 case NFT_MSG_DELSETELEM
:
8144 te
= (struct nft_trans_elem
*)trans
->data
;
8146 nft_set_elem_activate(net
, te
->set
, &te
->elem
);
8147 te
->set
->ops
->activate(net
, te
->set
, &te
->elem
);
8150 nft_trans_destroy(trans
);
8152 case NFT_MSG_NEWOBJ
:
8153 if (nft_trans_obj_update(trans
)) {
8154 kfree(nft_trans_obj_newobj(trans
));
8155 nft_trans_destroy(trans
);
8157 trans
->ctx
.table
->use
--;
8158 nft_obj_del(nft_trans_obj(trans
));
8161 case NFT_MSG_DELOBJ
:
8162 trans
->ctx
.table
->use
++;
8163 nft_clear(trans
->ctx
.net
, nft_trans_obj(trans
));
8164 nft_trans_destroy(trans
);
8166 case NFT_MSG_NEWFLOWTABLE
:
8167 if (nft_trans_flowtable_update(trans
)) {
8168 nft_unregister_flowtable_net_hooks(net
,
8169 &nft_trans_flowtable_hooks(trans
));
8171 trans
->ctx
.table
->use
--;
8172 list_del_rcu(&nft_trans_flowtable(trans
)->list
);
8173 nft_unregister_flowtable_net_hooks(net
,
8174 &nft_trans_flowtable(trans
)->hook_list
);
8177 case NFT_MSG_DELFLOWTABLE
:
8178 if (nft_trans_flowtable_update(trans
)) {
8179 list_for_each_entry(hook
, &nft_trans_flowtable(trans
)->hook_list
, list
)
8180 hook
->inactive
= false;
8182 trans
->ctx
.table
->use
++;
8183 nft_clear(trans
->ctx
.net
, nft_trans_flowtable(trans
));
8185 nft_trans_destroy(trans
);
8192 list_for_each_entry_safe_reverse(trans
, next
,
8193 &net
->nft
.commit_list
, list
) {
8194 list_del(&trans
->list
);
8195 nf_tables_abort_release(trans
);
8198 if (action
== NFNL_ABORT_AUTOLOAD
)
8199 nf_tables_module_autoload(net
);
8201 nf_tables_module_autoload_cleanup(net
);
8206 static void nf_tables_cleanup(struct net
*net
)
8208 nft_validate_state_update(net
, NFT_VALIDATE_SKIP
);
8211 static int nf_tables_abort(struct net
*net
, struct sk_buff
*skb
,
8212 enum nfnl_abort_action action
)
8214 int ret
= __nf_tables_abort(net
, action
);
8216 mutex_unlock(&net
->nft
.commit_mutex
);
8221 static bool nf_tables_valid_genid(struct net
*net
, u32 genid
)
8225 mutex_lock(&net
->nft
.commit_mutex
);
8227 genid_ok
= genid
== 0 || net
->nft
.base_seq
== genid
;
8229 mutex_unlock(&net
->nft
.commit_mutex
);
8231 /* else, commit mutex has to be released by commit or abort function */
8235 static const struct nfnetlink_subsystem nf_tables_subsys
= {
8236 .name
= "nf_tables",
8237 .subsys_id
= NFNL_SUBSYS_NFTABLES
,
8238 .cb_count
= NFT_MSG_MAX
,
8240 .commit
= nf_tables_commit
,
8241 .abort
= nf_tables_abort
,
8242 .cleanup
= nf_tables_cleanup
,
8243 .valid_genid
= nf_tables_valid_genid
,
8244 .owner
= THIS_MODULE
,
8247 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
8248 enum nft_chain_types type
)
8250 const struct nft_base_chain
*basechain
;
8252 if (nft_is_base_chain(chain
)) {
8253 basechain
= nft_base_chain(chain
);
8254 if (basechain
->type
->type
!= type
)
8259 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency
);
8261 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
8262 unsigned int hook_flags
)
8264 struct nft_base_chain
*basechain
;
8266 if (nft_is_base_chain(chain
)) {
8267 basechain
= nft_base_chain(chain
);
8269 if ((1 << basechain
->ops
.hooknum
) & hook_flags
)
8277 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks
);
8280 * Loop detection - walk through the ruleset beginning at the destination chain
8281 * of a new jump until either the source chain is reached (loop) or all
8282 * reachable chains have been traversed.
8284 * The loop check is performed whenever a new jump verdict is added to an
8285 * expression or verdict map or a verdict map is bound to a new chain.
8288 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
8289 const struct nft_chain
*chain
);
8291 static int nf_tables_loop_check_setelem(const struct nft_ctx
*ctx
,
8292 struct nft_set
*set
,
8293 const struct nft_set_iter
*iter
,
8294 struct nft_set_elem
*elem
)
8296 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
8297 const struct nft_data
*data
;
8299 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
8300 *nft_set_ext_flags(ext
) & NFT_SET_ELEM_INTERVAL_END
)
8303 data
= nft_set_ext_data(ext
);
8304 switch (data
->verdict
.code
) {
8307 return nf_tables_check_loops(ctx
, data
->verdict
.chain
);
8313 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
8314 const struct nft_chain
*chain
)
8316 const struct nft_rule
*rule
;
8317 const struct nft_expr
*expr
, *last
;
8318 struct nft_set
*set
;
8319 struct nft_set_binding
*binding
;
8320 struct nft_set_iter iter
;
8322 if (ctx
->chain
== chain
)
8325 list_for_each_entry(rule
, &chain
->rules
, list
) {
8326 nft_rule_for_each_expr(expr
, last
, rule
) {
8327 struct nft_immediate_expr
*priv
;
8328 const struct nft_data
*data
;
8331 if (strcmp(expr
->ops
->type
->name
, "immediate"))
8334 priv
= nft_expr_priv(expr
);
8335 if (priv
->dreg
!= NFT_REG_VERDICT
)
8339 switch (data
->verdict
.code
) {
8342 err
= nf_tables_check_loops(ctx
,
8343 data
->verdict
.chain
);
8352 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
8353 if (!nft_is_active_next(ctx
->net
, set
))
8355 if (!(set
->flags
& NFT_SET_MAP
) ||
8356 set
->dtype
!= NFT_DATA_VERDICT
)
8359 list_for_each_entry(binding
, &set
->bindings
, list
) {
8360 if (!(binding
->flags
& NFT_SET_MAP
) ||
8361 binding
->chain
!= chain
)
8364 iter
.genmask
= nft_genmask_next(ctx
->net
);
8368 iter
.fn
= nf_tables_loop_check_setelem
;
8370 set
->ops
->walk(ctx
, set
, &iter
);
8380 * nft_parse_u32_check - fetch u32 attribute and check for maximum value
8382 * @attr: netlink attribute to fetch value from
8383 * @max: maximum value to be stored in dest
8384 * @dest: pointer to the variable
8386 * Parse, check and store a given u32 netlink attribute into variable.
8387 * This function returns -ERANGE if the value goes over maximum value.
8388 * Otherwise a 0 is returned and the attribute value is stored in the
8389 * destination variable.
8391 int nft_parse_u32_check(const struct nlattr
*attr
, int max
, u32
*dest
)
8395 val
= ntohl(nla_get_be32(attr
));
8402 EXPORT_SYMBOL_GPL(nft_parse_u32_check
);
8405 * nft_parse_register - parse a register value from a netlink attribute
8407 * @attr: netlink attribute
8409 * Parse and translate a register value from a netlink attribute.
8410 * Registers used to be 128 bit wide, these register numbers will be
8411 * mapped to the corresponding 32 bit register numbers.
8413 unsigned int nft_parse_register(const struct nlattr
*attr
)
8417 reg
= ntohl(nla_get_be32(attr
));
8419 case NFT_REG_VERDICT
...NFT_REG_4
:
8420 return reg
* NFT_REG_SIZE
/ NFT_REG32_SIZE
;
8422 return reg
+ NFT_REG_SIZE
/ NFT_REG32_SIZE
- NFT_REG32_00
;
8425 EXPORT_SYMBOL_GPL(nft_parse_register
);
8428 * nft_dump_register - dump a register value to a netlink attribute
8430 * @skb: socket buffer
8431 * @attr: attribute number
8432 * @reg: register number
8434 * Construct a netlink attribute containing the register number. For
8435 * compatibility reasons, register numbers being a multiple of 4 are
8436 * translated to the corresponding 128 bit register numbers.
8438 int nft_dump_register(struct sk_buff
*skb
, unsigned int attr
, unsigned int reg
)
8440 if (reg
% (NFT_REG_SIZE
/ NFT_REG32_SIZE
) == 0)
8441 reg
= reg
/ (NFT_REG_SIZE
/ NFT_REG32_SIZE
);
8443 reg
= reg
- NFT_REG_SIZE
/ NFT_REG32_SIZE
+ NFT_REG32_00
;
8445 return nla_put_be32(skb
, attr
, htonl(reg
));
8447 EXPORT_SYMBOL_GPL(nft_dump_register
);
8450 * nft_validate_register_load - validate a load from a register
8452 * @reg: the register number
8453 * @len: the length of the data
8455 * Validate that the input register is one of the general purpose
8456 * registers and that the length of the load is within the bounds.
8458 int nft_validate_register_load(enum nft_registers reg
, unsigned int len
)
8460 if (reg
< NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
)
8464 if (reg
* NFT_REG32_SIZE
+ len
> sizeof_field(struct nft_regs
, data
))
8469 EXPORT_SYMBOL_GPL(nft_validate_register_load
);
8472 * nft_validate_register_store - validate an expressions' register store
8474 * @ctx: context of the expression performing the load
8475 * @reg: the destination register number
8476 * @data: the data to load
8477 * @type: the data type
8478 * @len: the length of the data
8480 * Validate that a data load uses the appropriate data type for
8481 * the destination register and the length is within the bounds.
8482 * A value of NULL for the data means that its runtime gathered
8485 int nft_validate_register_store(const struct nft_ctx
*ctx
,
8486 enum nft_registers reg
,
8487 const struct nft_data
*data
,
8488 enum nft_data_types type
, unsigned int len
)
8493 case NFT_REG_VERDICT
:
8494 if (type
!= NFT_DATA_VERDICT
)
8498 (data
->verdict
.code
== NFT_GOTO
||
8499 data
->verdict
.code
== NFT_JUMP
)) {
8500 err
= nf_tables_check_loops(ctx
, data
->verdict
.chain
);
8507 if (reg
< NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
)
8511 if (reg
* NFT_REG32_SIZE
+ len
>
8512 sizeof_field(struct nft_regs
, data
))
8515 if (data
!= NULL
&& type
!= NFT_DATA_VALUE
)
8520 EXPORT_SYMBOL_GPL(nft_validate_register_store
);
8522 static const struct nla_policy nft_verdict_policy
[NFTA_VERDICT_MAX
+ 1] = {
8523 [NFTA_VERDICT_CODE
] = { .type
= NLA_U32
},
8524 [NFTA_VERDICT_CHAIN
] = { .type
= NLA_STRING
,
8525 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
8526 [NFTA_VERDICT_CHAIN_ID
] = { .type
= NLA_U32
},
8529 static int nft_verdict_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
8530 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
8532 u8 genmask
= nft_genmask_next(ctx
->net
);
8533 struct nlattr
*tb
[NFTA_VERDICT_MAX
+ 1];
8534 struct nft_chain
*chain
;
8537 err
= nla_parse_nested_deprecated(tb
, NFTA_VERDICT_MAX
, nla
,
8538 nft_verdict_policy
, NULL
);
8542 if (!tb
[NFTA_VERDICT_CODE
])
8544 data
->verdict
.code
= ntohl(nla_get_be32(tb
[NFTA_VERDICT_CODE
]));
8546 switch (data
->verdict
.code
) {
8548 switch (data
->verdict
.code
& NF_VERDICT_MASK
) {
8563 if (tb
[NFTA_VERDICT_CHAIN
]) {
8564 chain
= nft_chain_lookup(ctx
->net
, ctx
->table
,
8565 tb
[NFTA_VERDICT_CHAIN
],
8567 } else if (tb
[NFTA_VERDICT_CHAIN_ID
]) {
8568 chain
= nft_chain_lookup_byid(ctx
->net
,
8569 tb
[NFTA_VERDICT_CHAIN_ID
]);
8571 return PTR_ERR(chain
);
8577 return PTR_ERR(chain
);
8578 if (nft_is_base_chain(chain
))
8582 data
->verdict
.chain
= chain
;
8586 desc
->len
= sizeof(data
->verdict
);
8587 desc
->type
= NFT_DATA_VERDICT
;
8591 static void nft_verdict_uninit(const struct nft_data
*data
)
8593 struct nft_chain
*chain
;
8594 struct nft_rule
*rule
;
8596 switch (data
->verdict
.code
) {
8599 chain
= data
->verdict
.chain
;
8602 if (!nft_chain_is_bound(chain
))
8605 chain
->table
->use
--;
8606 list_for_each_entry(rule
, &chain
->rules
, list
)
8609 nft_chain_del(chain
);
8614 int nft_verdict_dump(struct sk_buff
*skb
, int type
, const struct nft_verdict
*v
)
8616 struct nlattr
*nest
;
8618 nest
= nla_nest_start_noflag(skb
, type
);
8620 goto nla_put_failure
;
8622 if (nla_put_be32(skb
, NFTA_VERDICT_CODE
, htonl(v
->code
)))
8623 goto nla_put_failure
;
8628 if (nla_put_string(skb
, NFTA_VERDICT_CHAIN
,
8630 goto nla_put_failure
;
8632 nla_nest_end(skb
, nest
);
8639 static int nft_value_init(const struct nft_ctx
*ctx
,
8640 struct nft_data
*data
, unsigned int size
,
8641 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
8651 nla_memcpy(data
->data
, nla
, len
);
8652 desc
->type
= NFT_DATA_VALUE
;
8657 static int nft_value_dump(struct sk_buff
*skb
, const struct nft_data
*data
,
8660 return nla_put(skb
, NFTA_DATA_VALUE
, len
, data
->data
);
8663 static const struct nla_policy nft_data_policy
[NFTA_DATA_MAX
+ 1] = {
8664 [NFTA_DATA_VALUE
] = { .type
= NLA_BINARY
},
8665 [NFTA_DATA_VERDICT
] = { .type
= NLA_NESTED
},
8669 * nft_data_init - parse nf_tables data netlink attributes
8671 * @ctx: context of the expression using the data
8672 * @data: destination struct nft_data
8673 * @size: maximum data length
8674 * @desc: data description
8675 * @nla: netlink attribute containing data
8677 * Parse the netlink data attributes and initialize a struct nft_data.
8678 * The type and length of data are returned in the data description.
8680 * The caller can indicate that it only wants to accept data of type
8681 * NFT_DATA_VALUE by passing NULL for the ctx argument.
8683 int nft_data_init(const struct nft_ctx
*ctx
,
8684 struct nft_data
*data
, unsigned int size
,
8685 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
8687 struct nlattr
*tb
[NFTA_DATA_MAX
+ 1];
8690 err
= nla_parse_nested_deprecated(tb
, NFTA_DATA_MAX
, nla
,
8691 nft_data_policy
, NULL
);
8695 if (tb
[NFTA_DATA_VALUE
])
8696 return nft_value_init(ctx
, data
, size
, desc
,
8697 tb
[NFTA_DATA_VALUE
]);
8698 if (tb
[NFTA_DATA_VERDICT
] && ctx
!= NULL
)
8699 return nft_verdict_init(ctx
, data
, desc
, tb
[NFTA_DATA_VERDICT
]);
8702 EXPORT_SYMBOL_GPL(nft_data_init
);
8705 * nft_data_release - release a nft_data item
8707 * @data: struct nft_data to release
8708 * @type: type of data
8710 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
8711 * all others need to be released by calling this function.
8713 void nft_data_release(const struct nft_data
*data
, enum nft_data_types type
)
8715 if (type
< NFT_DATA_VERDICT
)
8718 case NFT_DATA_VERDICT
:
8719 return nft_verdict_uninit(data
);
8724 EXPORT_SYMBOL_GPL(nft_data_release
);
8726 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
8727 enum nft_data_types type
, unsigned int len
)
8729 struct nlattr
*nest
;
8732 nest
= nla_nest_start_noflag(skb
, attr
);
8737 case NFT_DATA_VALUE
:
8738 err
= nft_value_dump(skb
, data
, len
);
8740 case NFT_DATA_VERDICT
:
8741 err
= nft_verdict_dump(skb
, NFTA_DATA_VERDICT
, &data
->verdict
);
8748 nla_nest_end(skb
, nest
);
8751 EXPORT_SYMBOL_GPL(nft_data_dump
);
8753 int __nft_release_basechain(struct nft_ctx
*ctx
)
8755 struct nft_rule
*rule
, *nr
;
8757 if (WARN_ON(!nft_is_base_chain(ctx
->chain
)))
8760 nf_tables_unregister_hook(ctx
->net
, ctx
->chain
->table
, ctx
->chain
);
8761 list_for_each_entry_safe(rule
, nr
, &ctx
->chain
->rules
, list
) {
8762 list_del(&rule
->list
);
8764 nf_tables_rule_release(ctx
, rule
);
8766 nft_chain_del(ctx
->chain
);
8768 nf_tables_chain_destroy(ctx
);
8772 EXPORT_SYMBOL_GPL(__nft_release_basechain
);
8774 static void __nft_release_tables(struct net
*net
)
8776 struct nft_flowtable
*flowtable
, *nf
;
8777 struct nft_table
*table
, *nt
;
8778 struct nft_chain
*chain
, *nc
;
8779 struct nft_object
*obj
, *ne
;
8780 struct nft_rule
*rule
, *nr
;
8781 struct nft_set
*set
, *ns
;
8782 struct nft_ctx ctx
= {
8784 .family
= NFPROTO_NETDEV
,
8787 list_for_each_entry_safe(table
, nt
, &net
->nft
.tables
, list
) {
8788 ctx
.family
= table
->family
;
8790 list_for_each_entry(chain
, &table
->chains
, list
)
8791 nf_tables_unregister_hook(net
, table
, chain
);
8792 /* No packets are walking on these chains anymore. */
8794 list_for_each_entry(chain
, &table
->chains
, list
) {
8796 list_for_each_entry_safe(rule
, nr
, &chain
->rules
, list
) {
8797 list_del(&rule
->list
);
8799 nf_tables_rule_release(&ctx
, rule
);
8802 list_for_each_entry_safe(flowtable
, nf
, &table
->flowtables
, list
) {
8803 list_del(&flowtable
->list
);
8805 nf_tables_flowtable_destroy(flowtable
);
8807 list_for_each_entry_safe(set
, ns
, &table
->sets
, list
) {
8808 list_del(&set
->list
);
8810 nft_set_destroy(&ctx
, set
);
8812 list_for_each_entry_safe(obj
, ne
, &table
->objects
, list
) {
8815 nft_obj_destroy(&ctx
, obj
);
8817 list_for_each_entry_safe(chain
, nc
, &table
->chains
, list
) {
8819 nft_chain_del(chain
);
8821 nf_tables_chain_destroy(&ctx
);
8823 list_del(&table
->list
);
8824 nf_tables_table_destroy(&ctx
);
8828 static int __net_init
nf_tables_init_net(struct net
*net
)
8830 INIT_LIST_HEAD(&net
->nft
.tables
);
8831 INIT_LIST_HEAD(&net
->nft
.commit_list
);
8832 INIT_LIST_HEAD(&net
->nft
.module_list
);
8833 INIT_LIST_HEAD(&net
->nft
.notify_list
);
8834 mutex_init(&net
->nft
.commit_mutex
);
8835 net
->nft
.base_seq
= 1;
8836 net
->nft
.validate_state
= NFT_VALIDATE_SKIP
;
8841 static void __net_exit
nf_tables_exit_net(struct net
*net
)
8843 mutex_lock(&net
->nft
.commit_mutex
);
8844 if (!list_empty(&net
->nft
.commit_list
))
8845 __nf_tables_abort(net
, NFNL_ABORT_NONE
);
8846 __nft_release_tables(net
);
8847 mutex_unlock(&net
->nft
.commit_mutex
);
8848 WARN_ON_ONCE(!list_empty(&net
->nft
.tables
));
8849 WARN_ON_ONCE(!list_empty(&net
->nft
.module_list
));
8850 WARN_ON_ONCE(!list_empty(&net
->nft
.notify_list
));
8853 static struct pernet_operations nf_tables_net_ops
= {
8854 .init
= nf_tables_init_net
,
8855 .exit
= nf_tables_exit_net
,
8858 static int __init
nf_tables_module_init(void)
8862 spin_lock_init(&nf_tables_destroy_list_lock
);
8863 err
= register_pernet_subsys(&nf_tables_net_ops
);
8867 err
= nft_chain_filter_init();
8871 err
= nf_tables_core_module_init();
8875 err
= register_netdevice_notifier(&nf_tables_flowtable_notifier
);
8879 err
= rhltable_init(&nft_objname_ht
, &nft_objname_ht_params
);
8883 err
= nft_offload_init();
8888 err
= nfnetlink_subsys_register(&nf_tables_subsys
);
8892 nft_chain_route_init();
8898 rhltable_destroy(&nft_objname_ht
);
8900 unregister_netdevice_notifier(&nf_tables_flowtable_notifier
);
8902 nf_tables_core_module_exit();
8904 nft_chain_filter_fini();
8906 unregister_pernet_subsys(&nf_tables_net_ops
);
8910 static void __exit
nf_tables_module_exit(void)
8912 nfnetlink_subsys_unregister(&nf_tables_subsys
);
8914 unregister_netdevice_notifier(&nf_tables_flowtable_notifier
);
8915 nft_chain_filter_fini();
8916 nft_chain_route_fini();
8917 unregister_pernet_subsys(&nf_tables_net_ops
);
8918 cancel_work_sync(&trans_destroy_work
);
8920 rhltable_destroy(&nft_objname_ht
);
8921 nf_tables_core_module_exit();
8924 module_init(nf_tables_module_init
);
8925 module_exit(nf_tables_module_exit
);
8927 MODULE_LICENSE("GPL");
8928 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
8929 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES
);