2 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/list.h>
14 #include <linux/skbuff.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nfnetlink.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_tables_core.h>
20 #include <net/netfilter/nf_tables.h>
21 #include <net/net_namespace.h>
24 static LIST_HEAD(nf_tables_expressions
);
27 * nft_register_afinfo - register nf_tables address family info
29 * @afi: address family info to register
31 * Register the address family for use with nf_tables. Returns zero on
32 * success or a negative errno code otherwise.
34 int nft_register_afinfo(struct net
*net
, struct nft_af_info
*afi
)
36 INIT_LIST_HEAD(&afi
->tables
);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
38 list_add_tail_rcu(&afi
->list
, &net
->nft
.af_info
);
39 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
42 EXPORT_SYMBOL_GPL(nft_register_afinfo
);
44 static void __nft_release_afinfo(struct net
*net
, struct nft_af_info
*afi
);
47 * nft_unregister_afinfo - unregister nf_tables address family info
49 * @afi: address family info to unregister
51 * Unregister the address family for use with nf_tables.
53 void nft_unregister_afinfo(struct net
*net
, struct nft_af_info
*afi
)
55 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
56 __nft_release_afinfo(net
, afi
);
57 list_del_rcu(&afi
->list
);
58 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
60 EXPORT_SYMBOL_GPL(nft_unregister_afinfo
);
62 static struct nft_af_info
*nft_afinfo_lookup(struct net
*net
, int family
)
64 struct nft_af_info
*afi
;
66 list_for_each_entry(afi
, &net
->nft
.af_info
, list
) {
67 if (afi
->family
== family
)
73 static struct nft_af_info
*
74 nf_tables_afinfo_lookup(struct net
*net
, int family
, bool autoload
)
76 struct nft_af_info
*afi
;
78 afi
= nft_afinfo_lookup(net
, family
);
83 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
84 request_module("nft-afinfo-%u", family
);
85 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
86 afi
= nft_afinfo_lookup(net
, family
);
88 return ERR_PTR(-EAGAIN
);
91 return ERR_PTR(-EAFNOSUPPORT
);
94 static void nft_ctx_init(struct nft_ctx
*ctx
,
96 const struct sk_buff
*skb
,
97 const struct nlmsghdr
*nlh
,
98 struct nft_af_info
*afi
,
99 struct nft_table
*table
,
100 struct nft_chain
*chain
,
101 const struct nlattr
* const *nla
)
108 ctx
->portid
= NETLINK_CB(skb
).portid
;
109 ctx
->report
= nlmsg_report(nlh
);
110 ctx
->seq
= nlh
->nlmsg_seq
;
113 static struct nft_trans
*nft_trans_alloc(struct nft_ctx
*ctx
, int msg_type
,
116 struct nft_trans
*trans
;
118 trans
= kzalloc(sizeof(struct nft_trans
) + size
, GFP_KERNEL
);
122 trans
->msg_type
= msg_type
;
128 static void nft_trans_destroy(struct nft_trans
*trans
)
130 list_del(&trans
->list
);
134 static int nft_register_basechain(struct nft_base_chain
*basechain
,
135 unsigned int hook_nops
)
137 struct net
*net
= read_pnet(&basechain
->pnet
);
139 if (basechain
->flags
& NFT_BASECHAIN_DISABLED
)
142 return nf_register_net_hooks(net
, basechain
->ops
, hook_nops
);
145 static void nft_unregister_basechain(struct nft_base_chain
*basechain
,
146 unsigned int hook_nops
)
148 struct net
*net
= read_pnet(&basechain
->pnet
);
150 if (basechain
->flags
& NFT_BASECHAIN_DISABLED
)
153 nf_unregister_net_hooks(net
, basechain
->ops
, hook_nops
);
156 static int nf_tables_register_hooks(const struct nft_table
*table
,
157 struct nft_chain
*chain
,
158 unsigned int hook_nops
)
160 if (table
->flags
& NFT_TABLE_F_DORMANT
||
161 !(chain
->flags
& NFT_BASE_CHAIN
))
164 return nft_register_basechain(nft_base_chain(chain
), hook_nops
);
167 static void nf_tables_unregister_hooks(const struct nft_table
*table
,
168 struct nft_chain
*chain
,
169 unsigned int hook_nops
)
171 if (table
->flags
& NFT_TABLE_F_DORMANT
||
172 !(chain
->flags
& NFT_BASE_CHAIN
))
175 nft_unregister_basechain(nft_base_chain(chain
), hook_nops
);
178 /* Internal table flags */
179 #define NFT_TABLE_INACTIVE (1 << 15)
181 static int nft_trans_table_add(struct nft_ctx
*ctx
, int msg_type
)
183 struct nft_trans
*trans
;
185 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_table
));
189 if (msg_type
== NFT_MSG_NEWTABLE
)
190 ctx
->table
->flags
|= NFT_TABLE_INACTIVE
;
192 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
196 static int nft_deltable(struct nft_ctx
*ctx
)
200 err
= nft_trans_table_add(ctx
, NFT_MSG_DELTABLE
);
204 list_del_rcu(&ctx
->table
->list
);
208 static int nft_trans_chain_add(struct nft_ctx
*ctx
, int msg_type
)
210 struct nft_trans
*trans
;
212 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_chain
));
216 if (msg_type
== NFT_MSG_NEWCHAIN
)
217 ctx
->chain
->flags
|= NFT_CHAIN_INACTIVE
;
219 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
223 static int nft_delchain(struct nft_ctx
*ctx
)
227 err
= nft_trans_chain_add(ctx
, NFT_MSG_DELCHAIN
);
232 list_del_rcu(&ctx
->chain
->list
);
238 nft_rule_is_active(struct net
*net
, const struct nft_rule
*rule
)
240 return (rule
->genmask
& nft_genmask_cur(net
)) == 0;
244 nft_rule_is_active_next(struct net
*net
, const struct nft_rule
*rule
)
246 return (rule
->genmask
& nft_genmask_next(net
)) == 0;
250 nft_rule_activate_next(struct net
*net
, struct nft_rule
*rule
)
252 /* Now inactive, will be active in the future */
253 rule
->genmask
= nft_genmask_cur(net
);
257 nft_rule_deactivate_next(struct net
*net
, struct nft_rule
*rule
)
259 rule
->genmask
= nft_genmask_next(net
);
262 static inline void nft_rule_clear(struct net
*net
, struct nft_rule
*rule
)
264 rule
->genmask
&= ~nft_genmask_next(net
);
268 nf_tables_delrule_deactivate(struct nft_ctx
*ctx
, struct nft_rule
*rule
)
270 /* You cannot delete the same rule twice */
271 if (nft_rule_is_active_next(ctx
->net
, rule
)) {
272 nft_rule_deactivate_next(ctx
->net
, rule
);
279 static struct nft_trans
*nft_trans_rule_add(struct nft_ctx
*ctx
, int msg_type
,
280 struct nft_rule
*rule
)
282 struct nft_trans
*trans
;
284 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_rule
));
288 nft_trans_rule(trans
) = rule
;
289 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
294 static int nft_delrule(struct nft_ctx
*ctx
, struct nft_rule
*rule
)
296 struct nft_trans
*trans
;
299 trans
= nft_trans_rule_add(ctx
, NFT_MSG_DELRULE
, rule
);
303 err
= nf_tables_delrule_deactivate(ctx
, rule
);
305 nft_trans_destroy(trans
);
312 static int nft_delrule_by_chain(struct nft_ctx
*ctx
)
314 struct nft_rule
*rule
;
317 list_for_each_entry(rule
, &ctx
->chain
->rules
, list
) {
318 err
= nft_delrule(ctx
, rule
);
325 /* Internal set flag */
326 #define NFT_SET_INACTIVE (1 << 15)
328 static int nft_trans_set_add(struct nft_ctx
*ctx
, int msg_type
,
331 struct nft_trans
*trans
;
333 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_set
));
337 if (msg_type
== NFT_MSG_NEWSET
&& ctx
->nla
[NFTA_SET_ID
] != NULL
) {
338 nft_trans_set_id(trans
) =
339 ntohl(nla_get_be32(ctx
->nla
[NFTA_SET_ID
]));
340 set
->flags
|= NFT_SET_INACTIVE
;
342 nft_trans_set(trans
) = set
;
343 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
348 static int nft_delset(struct nft_ctx
*ctx
, struct nft_set
*set
)
352 err
= nft_trans_set_add(ctx
, NFT_MSG_DELSET
, set
);
356 list_del_rcu(&set
->list
);
366 static struct nft_table
*nft_table_lookup(const struct nft_af_info
*afi
,
367 const struct nlattr
*nla
)
369 struct nft_table
*table
;
371 list_for_each_entry(table
, &afi
->tables
, list
) {
372 if (!nla_strcmp(nla
, table
->name
))
378 static struct nft_table
*nf_tables_table_lookup(const struct nft_af_info
*afi
,
379 const struct nlattr
*nla
)
381 struct nft_table
*table
;
384 return ERR_PTR(-EINVAL
);
386 table
= nft_table_lookup(afi
, nla
);
390 return ERR_PTR(-ENOENT
);
393 static inline u64
nf_tables_alloc_handle(struct nft_table
*table
)
395 return ++table
->hgenerator
;
398 static const struct nf_chain_type
*chain_type
[AF_MAX
][NFT_CHAIN_T_MAX
];
400 static const struct nf_chain_type
*
401 __nf_tables_chain_type_lookup(int family
, const struct nlattr
*nla
)
405 for (i
= 0; i
< NFT_CHAIN_T_MAX
; i
++) {
406 if (chain_type
[family
][i
] != NULL
&&
407 !nla_strcmp(nla
, chain_type
[family
][i
]->name
))
408 return chain_type
[family
][i
];
413 static const struct nf_chain_type
*
414 nf_tables_chain_type_lookup(const struct nft_af_info
*afi
,
415 const struct nlattr
*nla
,
418 const struct nf_chain_type
*type
;
420 type
= __nf_tables_chain_type_lookup(afi
->family
, nla
);
423 #ifdef CONFIG_MODULES
425 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
426 request_module("nft-chain-%u-%.*s", afi
->family
,
427 nla_len(nla
), (const char *)nla_data(nla
));
428 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
429 type
= __nf_tables_chain_type_lookup(afi
->family
, nla
);
431 return ERR_PTR(-EAGAIN
);
434 return ERR_PTR(-ENOENT
);
437 static const struct nla_policy nft_table_policy
[NFTA_TABLE_MAX
+ 1] = {
438 [NFTA_TABLE_NAME
] = { .type
= NLA_STRING
,
439 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
440 [NFTA_TABLE_FLAGS
] = { .type
= NLA_U32
},
443 static int nf_tables_fill_table_info(struct sk_buff
*skb
, struct net
*net
,
444 u32 portid
, u32 seq
, int event
, u32 flags
,
445 int family
, const struct nft_table
*table
)
447 struct nlmsghdr
*nlh
;
448 struct nfgenmsg
*nfmsg
;
450 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
451 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
453 goto nla_put_failure
;
455 nfmsg
= nlmsg_data(nlh
);
456 nfmsg
->nfgen_family
= family
;
457 nfmsg
->version
= NFNETLINK_V0
;
458 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
460 if (nla_put_string(skb
, NFTA_TABLE_NAME
, table
->name
) ||
461 nla_put_be32(skb
, NFTA_TABLE_FLAGS
, htonl(table
->flags
)) ||
462 nla_put_be32(skb
, NFTA_TABLE_USE
, htonl(table
->use
)))
463 goto nla_put_failure
;
469 nlmsg_trim(skb
, nlh
);
473 static int nf_tables_table_notify(const struct nft_ctx
*ctx
, int event
)
479 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
483 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
487 err
= nf_tables_fill_table_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
488 event
, 0, ctx
->afi
->family
, ctx
->table
);
494 err
= nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
495 ctx
->report
, GFP_KERNEL
);
498 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
504 static int nf_tables_dump_tables(struct sk_buff
*skb
,
505 struct netlink_callback
*cb
)
507 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
508 const struct nft_af_info
*afi
;
509 const struct nft_table
*table
;
510 unsigned int idx
= 0, s_idx
= cb
->args
[0];
511 struct net
*net
= sock_net(skb
->sk
);
512 int family
= nfmsg
->nfgen_family
;
515 cb
->seq
= net
->nft
.base_seq
;
517 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
518 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
521 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
525 memset(&cb
->args
[1], 0,
526 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
527 if (nf_tables_fill_table_info(skb
, net
,
528 NETLINK_CB(cb
->skb
).portid
,
532 afi
->family
, table
) < 0)
535 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
546 static int nf_tables_gettable(struct net
*net
, struct sock
*nlsk
,
547 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
548 const struct nlattr
* const nla
[])
550 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
551 const struct nft_af_info
*afi
;
552 const struct nft_table
*table
;
553 struct sk_buff
*skb2
;
554 int family
= nfmsg
->nfgen_family
;
557 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
558 struct netlink_dump_control c
= {
559 .dump
= nf_tables_dump_tables
,
561 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
564 afi
= nf_tables_afinfo_lookup(net
, family
, false);
568 table
= nf_tables_table_lookup(afi
, nla
[NFTA_TABLE_NAME
]);
570 return PTR_ERR(table
);
571 if (table
->flags
& NFT_TABLE_INACTIVE
)
574 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
578 err
= nf_tables_fill_table_info(skb2
, net
, NETLINK_CB(skb
).portid
,
579 nlh
->nlmsg_seq
, NFT_MSG_NEWTABLE
, 0,
584 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
591 static int nf_tables_table_enable(const struct nft_af_info
*afi
,
592 struct nft_table
*table
)
594 struct nft_chain
*chain
;
597 list_for_each_entry(chain
, &table
->chains
, list
) {
598 if (!(chain
->flags
& NFT_BASE_CHAIN
))
601 err
= nft_register_basechain(nft_base_chain(chain
), afi
->nops
);
609 list_for_each_entry(chain
, &table
->chains
, list
) {
610 if (!(chain
->flags
& NFT_BASE_CHAIN
))
616 nft_unregister_basechain(nft_base_chain(chain
), afi
->nops
);
621 static void nf_tables_table_disable(const struct nft_af_info
*afi
,
622 struct nft_table
*table
)
624 struct nft_chain
*chain
;
626 list_for_each_entry(chain
, &table
->chains
, list
) {
627 if (chain
->flags
& NFT_BASE_CHAIN
)
628 nft_unregister_basechain(nft_base_chain(chain
),
633 static int nf_tables_updtable(struct nft_ctx
*ctx
)
635 struct nft_trans
*trans
;
639 if (!ctx
->nla
[NFTA_TABLE_FLAGS
])
642 flags
= ntohl(nla_get_be32(ctx
->nla
[NFTA_TABLE_FLAGS
]));
643 if (flags
& ~NFT_TABLE_F_DORMANT
)
646 if (flags
== ctx
->table
->flags
)
649 trans
= nft_trans_alloc(ctx
, NFT_MSG_NEWTABLE
,
650 sizeof(struct nft_trans_table
));
654 if ((flags
& NFT_TABLE_F_DORMANT
) &&
655 !(ctx
->table
->flags
& NFT_TABLE_F_DORMANT
)) {
656 nft_trans_table_enable(trans
) = false;
657 } else if (!(flags
& NFT_TABLE_F_DORMANT
) &&
658 ctx
->table
->flags
& NFT_TABLE_F_DORMANT
) {
659 ret
= nf_tables_table_enable(ctx
->afi
, ctx
->table
);
661 ctx
->table
->flags
&= ~NFT_TABLE_F_DORMANT
;
662 nft_trans_table_enable(trans
) = true;
668 nft_trans_table_update(trans
) = true;
669 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
672 nft_trans_destroy(trans
);
676 static int nf_tables_newtable(struct net
*net
, struct sock
*nlsk
,
677 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
678 const struct nlattr
* const nla
[])
680 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
681 const struct nlattr
*name
;
682 struct nft_af_info
*afi
;
683 struct nft_table
*table
;
684 int family
= nfmsg
->nfgen_family
;
689 afi
= nf_tables_afinfo_lookup(net
, family
, true);
693 name
= nla
[NFTA_TABLE_NAME
];
694 table
= nf_tables_table_lookup(afi
, name
);
696 if (PTR_ERR(table
) != -ENOENT
)
697 return PTR_ERR(table
);
702 if (table
->flags
& NFT_TABLE_INACTIVE
)
704 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
706 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
709 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
710 return nf_tables_updtable(&ctx
);
713 if (nla
[NFTA_TABLE_FLAGS
]) {
714 flags
= ntohl(nla_get_be32(nla
[NFTA_TABLE_FLAGS
]));
715 if (flags
& ~NFT_TABLE_F_DORMANT
)
720 if (!try_module_get(afi
->owner
))
724 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
728 nla_strlcpy(table
->name
, name
, NFT_TABLE_MAXNAMELEN
);
729 INIT_LIST_HEAD(&table
->chains
);
730 INIT_LIST_HEAD(&table
->sets
);
731 table
->flags
= flags
;
733 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
734 err
= nft_trans_table_add(&ctx
, NFT_MSG_NEWTABLE
);
738 list_add_tail_rcu(&table
->list
, &afi
->tables
);
743 module_put(afi
->owner
);
748 static int nft_flush_table(struct nft_ctx
*ctx
)
751 struct nft_chain
*chain
, *nc
;
752 struct nft_set
*set
, *ns
;
754 list_for_each_entry(chain
, &ctx
->table
->chains
, list
) {
757 err
= nft_delrule_by_chain(ctx
);
762 list_for_each_entry_safe(set
, ns
, &ctx
->table
->sets
, list
) {
763 if (set
->flags
& NFT_SET_ANONYMOUS
&&
764 !list_empty(&set
->bindings
))
767 err
= nft_delset(ctx
, set
);
772 list_for_each_entry_safe(chain
, nc
, &ctx
->table
->chains
, list
) {
775 err
= nft_delchain(ctx
);
780 err
= nft_deltable(ctx
);
785 static int nft_flush(struct nft_ctx
*ctx
, int family
)
787 struct nft_af_info
*afi
;
788 struct nft_table
*table
, *nt
;
789 const struct nlattr
* const *nla
= ctx
->nla
;
792 list_for_each_entry(afi
, &ctx
->net
->nft
.af_info
, list
) {
793 if (family
!= AF_UNSPEC
&& afi
->family
!= family
)
797 list_for_each_entry_safe(table
, nt
, &afi
->tables
, list
) {
798 if (nla
[NFTA_TABLE_NAME
] &&
799 nla_strcmp(nla
[NFTA_TABLE_NAME
], table
->name
) != 0)
804 err
= nft_flush_table(ctx
);
813 static int nf_tables_deltable(struct net
*net
, struct sock
*nlsk
,
814 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
815 const struct nlattr
* const nla
[])
817 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
818 struct nft_af_info
*afi
;
819 struct nft_table
*table
;
820 int family
= nfmsg
->nfgen_family
;
823 nft_ctx_init(&ctx
, net
, skb
, nlh
, NULL
, NULL
, NULL
, nla
);
824 if (family
== AF_UNSPEC
|| nla
[NFTA_TABLE_NAME
] == NULL
)
825 return nft_flush(&ctx
, family
);
827 afi
= nf_tables_afinfo_lookup(net
, family
, false);
831 table
= nf_tables_table_lookup(afi
, nla
[NFTA_TABLE_NAME
]);
833 return PTR_ERR(table
);
838 return nft_flush_table(&ctx
);
841 static void nf_tables_table_destroy(struct nft_ctx
*ctx
)
843 BUG_ON(ctx
->table
->use
> 0);
846 module_put(ctx
->afi
->owner
);
849 int nft_register_chain_type(const struct nf_chain_type
*ctype
)
853 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
854 if (chain_type
[ctype
->family
][ctype
->type
] != NULL
) {
858 chain_type
[ctype
->family
][ctype
->type
] = ctype
;
860 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
863 EXPORT_SYMBOL_GPL(nft_register_chain_type
);
865 void nft_unregister_chain_type(const struct nf_chain_type
*ctype
)
867 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
868 chain_type
[ctype
->family
][ctype
->type
] = NULL
;
869 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
871 EXPORT_SYMBOL_GPL(nft_unregister_chain_type
);
877 static struct nft_chain
*
878 nf_tables_chain_lookup_byhandle(const struct nft_table
*table
, u64 handle
)
880 struct nft_chain
*chain
;
882 list_for_each_entry(chain
, &table
->chains
, list
) {
883 if (chain
->handle
== handle
)
887 return ERR_PTR(-ENOENT
);
890 static struct nft_chain
*nf_tables_chain_lookup(const struct nft_table
*table
,
891 const struct nlattr
*nla
)
893 struct nft_chain
*chain
;
896 return ERR_PTR(-EINVAL
);
898 list_for_each_entry(chain
, &table
->chains
, list
) {
899 if (!nla_strcmp(nla
, chain
->name
))
903 return ERR_PTR(-ENOENT
);
906 static const struct nla_policy nft_chain_policy
[NFTA_CHAIN_MAX
+ 1] = {
907 [NFTA_CHAIN_TABLE
] = { .type
= NLA_STRING
},
908 [NFTA_CHAIN_HANDLE
] = { .type
= NLA_U64
},
909 [NFTA_CHAIN_NAME
] = { .type
= NLA_STRING
,
910 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
911 [NFTA_CHAIN_HOOK
] = { .type
= NLA_NESTED
},
912 [NFTA_CHAIN_POLICY
] = { .type
= NLA_U32
},
913 [NFTA_CHAIN_TYPE
] = { .type
= NLA_STRING
},
914 [NFTA_CHAIN_COUNTERS
] = { .type
= NLA_NESTED
},
917 static const struct nla_policy nft_hook_policy
[NFTA_HOOK_MAX
+ 1] = {
918 [NFTA_HOOK_HOOKNUM
] = { .type
= NLA_U32
},
919 [NFTA_HOOK_PRIORITY
] = { .type
= NLA_U32
},
920 [NFTA_HOOK_DEV
] = { .type
= NLA_STRING
,
921 .len
= IFNAMSIZ
- 1 },
924 static int nft_dump_stats(struct sk_buff
*skb
, struct nft_stats __percpu
*stats
)
926 struct nft_stats
*cpu_stats
, total
;
932 memset(&total
, 0, sizeof(total
));
933 for_each_possible_cpu(cpu
) {
934 cpu_stats
= per_cpu_ptr(stats
, cpu
);
936 seq
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
937 pkts
= cpu_stats
->pkts
;
938 bytes
= cpu_stats
->bytes
;
939 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, seq
));
941 total
.bytes
+= bytes
;
943 nest
= nla_nest_start(skb
, NFTA_CHAIN_COUNTERS
);
945 goto nla_put_failure
;
947 if (nla_put_be64(skb
, NFTA_COUNTER_PACKETS
, cpu_to_be64(total
.pkts
)) ||
948 nla_put_be64(skb
, NFTA_COUNTER_BYTES
, cpu_to_be64(total
.bytes
)))
949 goto nla_put_failure
;
951 nla_nest_end(skb
, nest
);
958 static int nf_tables_fill_chain_info(struct sk_buff
*skb
, struct net
*net
,
959 u32 portid
, u32 seq
, int event
, u32 flags
,
960 int family
, const struct nft_table
*table
,
961 const struct nft_chain
*chain
)
963 struct nlmsghdr
*nlh
;
964 struct nfgenmsg
*nfmsg
;
966 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
967 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
969 goto nla_put_failure
;
971 nfmsg
= nlmsg_data(nlh
);
972 nfmsg
->nfgen_family
= family
;
973 nfmsg
->version
= NFNETLINK_V0
;
974 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
976 if (nla_put_string(skb
, NFTA_CHAIN_TABLE
, table
->name
))
977 goto nla_put_failure
;
978 if (nla_put_be64(skb
, NFTA_CHAIN_HANDLE
, cpu_to_be64(chain
->handle
)))
979 goto nla_put_failure
;
980 if (nla_put_string(skb
, NFTA_CHAIN_NAME
, chain
->name
))
981 goto nla_put_failure
;
983 if (chain
->flags
& NFT_BASE_CHAIN
) {
984 const struct nft_base_chain
*basechain
= nft_base_chain(chain
);
985 const struct nf_hook_ops
*ops
= &basechain
->ops
[0];
988 nest
= nla_nest_start(skb
, NFTA_CHAIN_HOOK
);
990 goto nla_put_failure
;
991 if (nla_put_be32(skb
, NFTA_HOOK_HOOKNUM
, htonl(ops
->hooknum
)))
992 goto nla_put_failure
;
993 if (nla_put_be32(skb
, NFTA_HOOK_PRIORITY
, htonl(ops
->priority
)))
994 goto nla_put_failure
;
995 if (basechain
->dev_name
[0] &&
996 nla_put_string(skb
, NFTA_HOOK_DEV
, basechain
->dev_name
))
997 goto nla_put_failure
;
998 nla_nest_end(skb
, nest
);
1000 if (nla_put_be32(skb
, NFTA_CHAIN_POLICY
,
1001 htonl(basechain
->policy
)))
1002 goto nla_put_failure
;
1004 if (nla_put_string(skb
, NFTA_CHAIN_TYPE
, basechain
->type
->name
))
1005 goto nla_put_failure
;
1007 if (nft_dump_stats(skb
, nft_base_chain(chain
)->stats
))
1008 goto nla_put_failure
;
1011 if (nla_put_be32(skb
, NFTA_CHAIN_USE
, htonl(chain
->use
)))
1012 goto nla_put_failure
;
1014 nlmsg_end(skb
, nlh
);
1018 nlmsg_trim(skb
, nlh
);
1022 static int nf_tables_chain_notify(const struct nft_ctx
*ctx
, int event
)
1024 struct sk_buff
*skb
;
1028 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
1032 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1036 err
= nf_tables_fill_chain_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
1037 event
, 0, ctx
->afi
->family
, ctx
->table
,
1044 err
= nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1045 ctx
->report
, GFP_KERNEL
);
1048 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1054 static int nf_tables_dump_chains(struct sk_buff
*skb
,
1055 struct netlink_callback
*cb
)
1057 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1058 const struct nft_af_info
*afi
;
1059 const struct nft_table
*table
;
1060 const struct nft_chain
*chain
;
1061 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1062 struct net
*net
= sock_net(skb
->sk
);
1063 int family
= nfmsg
->nfgen_family
;
1066 cb
->seq
= net
->nft
.base_seq
;
1068 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
1069 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
1072 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
1073 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
1077 memset(&cb
->args
[1], 0,
1078 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1079 if (nf_tables_fill_chain_info(skb
, net
,
1080 NETLINK_CB(cb
->skb
).portid
,
1084 afi
->family
, table
, chain
) < 0)
1087 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1099 static int nf_tables_getchain(struct net
*net
, struct sock
*nlsk
,
1100 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1101 const struct nlattr
* const nla
[])
1103 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1104 const struct nft_af_info
*afi
;
1105 const struct nft_table
*table
;
1106 const struct nft_chain
*chain
;
1107 struct sk_buff
*skb2
;
1108 int family
= nfmsg
->nfgen_family
;
1111 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1112 struct netlink_dump_control c
= {
1113 .dump
= nf_tables_dump_chains
,
1115 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
1118 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1120 return PTR_ERR(afi
);
1122 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
1124 return PTR_ERR(table
);
1125 if (table
->flags
& NFT_TABLE_INACTIVE
)
1128 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
]);
1130 return PTR_ERR(chain
);
1131 if (chain
->flags
& NFT_CHAIN_INACTIVE
)
1134 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1138 err
= nf_tables_fill_chain_info(skb2
, net
, NETLINK_CB(skb
).portid
,
1139 nlh
->nlmsg_seq
, NFT_MSG_NEWCHAIN
, 0,
1140 family
, table
, chain
);
1144 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
1151 static const struct nla_policy nft_counter_policy
[NFTA_COUNTER_MAX
+ 1] = {
1152 [NFTA_COUNTER_PACKETS
] = { .type
= NLA_U64
},
1153 [NFTA_COUNTER_BYTES
] = { .type
= NLA_U64
},
1156 static struct nft_stats __percpu
*nft_stats_alloc(const struct nlattr
*attr
)
1158 struct nlattr
*tb
[NFTA_COUNTER_MAX
+1];
1159 struct nft_stats __percpu
*newstats
;
1160 struct nft_stats
*stats
;
1163 err
= nla_parse_nested(tb
, NFTA_COUNTER_MAX
, attr
, nft_counter_policy
);
1165 return ERR_PTR(err
);
1167 if (!tb
[NFTA_COUNTER_BYTES
] || !tb
[NFTA_COUNTER_PACKETS
])
1168 return ERR_PTR(-EINVAL
);
1170 newstats
= netdev_alloc_pcpu_stats(struct nft_stats
);
1171 if (newstats
== NULL
)
1172 return ERR_PTR(-ENOMEM
);
1174 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1175 * are not exposed to userspace.
1178 stats
= this_cpu_ptr(newstats
);
1179 stats
->bytes
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_BYTES
]));
1180 stats
->pkts
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_PACKETS
]));
1186 static void nft_chain_stats_replace(struct nft_base_chain
*chain
,
1187 struct nft_stats __percpu
*newstats
)
1189 if (newstats
== NULL
)
1193 struct nft_stats __percpu
*oldstats
=
1194 nft_dereference(chain
->stats
);
1196 rcu_assign_pointer(chain
->stats
, newstats
);
1198 free_percpu(oldstats
);
1200 rcu_assign_pointer(chain
->stats
, newstats
);
1203 static void nf_tables_chain_destroy(struct nft_chain
*chain
)
1205 BUG_ON(chain
->use
> 0);
1207 if (chain
->flags
& NFT_BASE_CHAIN
) {
1208 struct nft_base_chain
*basechain
= nft_base_chain(chain
);
1210 module_put(basechain
->type
->owner
);
1211 free_percpu(basechain
->stats
);
1212 if (basechain
->ops
[0].dev
!= NULL
)
1213 dev_put(basechain
->ops
[0].dev
);
1220 static int nf_tables_newchain(struct net
*net
, struct sock
*nlsk
,
1221 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1222 const struct nlattr
* const nla
[])
1224 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1225 const struct nlattr
* uninitialized_var(name
);
1226 struct nft_af_info
*afi
;
1227 struct nft_table
*table
;
1228 struct nft_chain
*chain
;
1229 struct nft_base_chain
*basechain
= NULL
;
1230 struct nlattr
*ha
[NFTA_HOOK_MAX
+ 1];
1231 int family
= nfmsg
->nfgen_family
;
1232 struct net_device
*dev
= NULL
;
1233 u8 policy
= NF_ACCEPT
;
1236 struct nft_stats __percpu
*stats
;
1241 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
1243 afi
= nf_tables_afinfo_lookup(net
, family
, true);
1245 return PTR_ERR(afi
);
1247 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
1249 return PTR_ERR(table
);
1252 name
= nla
[NFTA_CHAIN_NAME
];
1254 if (nla
[NFTA_CHAIN_HANDLE
]) {
1255 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_CHAIN_HANDLE
]));
1256 chain
= nf_tables_chain_lookup_byhandle(table
, handle
);
1258 return PTR_ERR(chain
);
1260 chain
= nf_tables_chain_lookup(table
, name
);
1261 if (IS_ERR(chain
)) {
1262 if (PTR_ERR(chain
) != -ENOENT
)
1263 return PTR_ERR(chain
);
1268 if (nla
[NFTA_CHAIN_POLICY
]) {
1269 if ((chain
!= NULL
&&
1270 !(chain
->flags
& NFT_BASE_CHAIN
)))
1273 if (chain
== NULL
&&
1274 nla
[NFTA_CHAIN_HOOK
] == NULL
)
1277 policy
= ntohl(nla_get_be32(nla
[NFTA_CHAIN_POLICY
]));
1287 if (chain
!= NULL
) {
1288 struct nft_stats
*stats
= NULL
;
1289 struct nft_trans
*trans
;
1291 if (chain
->flags
& NFT_CHAIN_INACTIVE
)
1293 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
1295 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1298 if (nla
[NFTA_CHAIN_HANDLE
] && name
&&
1299 !IS_ERR(nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
])))
1302 if (nla
[NFTA_CHAIN_COUNTERS
]) {
1303 if (!(chain
->flags
& NFT_BASE_CHAIN
))
1306 stats
= nft_stats_alloc(nla
[NFTA_CHAIN_COUNTERS
]);
1308 return PTR_ERR(stats
);
1311 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
1312 trans
= nft_trans_alloc(&ctx
, NFT_MSG_NEWCHAIN
,
1313 sizeof(struct nft_trans_chain
));
1314 if (trans
== NULL
) {
1319 nft_trans_chain_stats(trans
) = stats
;
1320 nft_trans_chain_update(trans
) = true;
1322 if (nla
[NFTA_CHAIN_POLICY
])
1323 nft_trans_chain_policy(trans
) = policy
;
1325 nft_trans_chain_policy(trans
) = -1;
1327 if (nla
[NFTA_CHAIN_HANDLE
] && name
) {
1328 nla_strlcpy(nft_trans_chain_name(trans
), name
,
1329 NFT_CHAIN_MAXNAMELEN
);
1331 list_add_tail(&trans
->list
, &net
->nft
.commit_list
);
1335 if (table
->use
== UINT_MAX
)
1338 if (nla
[NFTA_CHAIN_HOOK
]) {
1339 const struct nf_chain_type
*type
;
1340 struct nf_hook_ops
*ops
;
1342 u32 hooknum
, priority
;
1344 type
= chain_type
[family
][NFT_CHAIN_T_DEFAULT
];
1345 if (nla
[NFTA_CHAIN_TYPE
]) {
1346 type
= nf_tables_chain_type_lookup(afi
,
1347 nla
[NFTA_CHAIN_TYPE
],
1350 return PTR_ERR(type
);
1353 err
= nla_parse_nested(ha
, NFTA_HOOK_MAX
, nla
[NFTA_CHAIN_HOOK
],
1357 if (ha
[NFTA_HOOK_HOOKNUM
] == NULL
||
1358 ha
[NFTA_HOOK_PRIORITY
] == NULL
)
1361 hooknum
= ntohl(nla_get_be32(ha
[NFTA_HOOK_HOOKNUM
]));
1362 if (hooknum
>= afi
->nhooks
)
1364 priority
= ntohl(nla_get_be32(ha
[NFTA_HOOK_PRIORITY
]));
1366 if (!(type
->hook_mask
& (1 << hooknum
)))
1368 if (!try_module_get(type
->owner
))
1370 hookfn
= type
->hooks
[hooknum
];
1372 if (afi
->flags
& NFT_AF_NEEDS_DEV
) {
1373 char ifname
[IFNAMSIZ
];
1375 if (!ha
[NFTA_HOOK_DEV
]) {
1376 module_put(type
->owner
);
1380 nla_strlcpy(ifname
, ha
[NFTA_HOOK_DEV
], IFNAMSIZ
);
1381 dev
= dev_get_by_name(net
, ifname
);
1383 module_put(type
->owner
);
1386 } else if (ha
[NFTA_HOOK_DEV
]) {
1387 module_put(type
->owner
);
1391 basechain
= kzalloc(sizeof(*basechain
), GFP_KERNEL
);
1392 if (basechain
== NULL
) {
1393 module_put(type
->owner
);
1400 strncpy(basechain
->dev_name
, dev
->name
, IFNAMSIZ
);
1402 if (nla
[NFTA_CHAIN_COUNTERS
]) {
1403 stats
= nft_stats_alloc(nla
[NFTA_CHAIN_COUNTERS
]);
1404 if (IS_ERR(stats
)) {
1405 module_put(type
->owner
);
1409 return PTR_ERR(stats
);
1411 basechain
->stats
= stats
;
1413 stats
= netdev_alloc_pcpu_stats(struct nft_stats
);
1414 if (stats
== NULL
) {
1415 module_put(type
->owner
);
1421 rcu_assign_pointer(basechain
->stats
, stats
);
1424 write_pnet(&basechain
->pnet
, net
);
1425 basechain
->type
= type
;
1426 chain
= &basechain
->chain
;
1428 for (i
= 0; i
< afi
->nops
; i
++) {
1429 ops
= &basechain
->ops
[i
];
1431 ops
->hooknum
= hooknum
;
1432 ops
->priority
= priority
;
1434 ops
->hook
= afi
->hooks
[ops
->hooknum
];
1438 if (afi
->hook_ops_init
)
1439 afi
->hook_ops_init(ops
, i
);
1442 chain
->flags
|= NFT_BASE_CHAIN
;
1443 basechain
->policy
= policy
;
1445 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
1450 INIT_LIST_HEAD(&chain
->rules
);
1451 chain
->handle
= nf_tables_alloc_handle(table
);
1452 chain
->table
= table
;
1453 nla_strlcpy(chain
->name
, name
, NFT_CHAIN_MAXNAMELEN
);
1455 err
= nf_tables_register_hooks(table
, chain
, afi
->nops
);
1459 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
1460 err
= nft_trans_chain_add(&ctx
, NFT_MSG_NEWCHAIN
);
1465 list_add_tail_rcu(&chain
->list
, &table
->chains
);
1468 nf_tables_unregister_hooks(table
, chain
, afi
->nops
);
1470 nf_tables_chain_destroy(chain
);
1474 static int nf_tables_delchain(struct net
*net
, struct sock
*nlsk
,
1475 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1476 const struct nlattr
* const nla
[])
1478 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1479 struct nft_af_info
*afi
;
1480 struct nft_table
*table
;
1481 struct nft_chain
*chain
;
1482 int family
= nfmsg
->nfgen_family
;
1485 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1487 return PTR_ERR(afi
);
1489 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
1491 return PTR_ERR(table
);
1493 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
]);
1495 return PTR_ERR(chain
);
1499 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
1501 return nft_delchain(&ctx
);
1509 * nft_register_expr - register nf_tables expr type
1512 * Registers the expr type for use with nf_tables. Returns zero on
1513 * success or a negative errno code otherwise.
1515 int nft_register_expr(struct nft_expr_type
*type
)
1517 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1518 if (type
->family
== NFPROTO_UNSPEC
)
1519 list_add_tail_rcu(&type
->list
, &nf_tables_expressions
);
1521 list_add_rcu(&type
->list
, &nf_tables_expressions
);
1522 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1525 EXPORT_SYMBOL_GPL(nft_register_expr
);
1528 * nft_unregister_expr - unregister nf_tables expr type
1531 * Unregisters the expr typefor use with nf_tables.
1533 void nft_unregister_expr(struct nft_expr_type
*type
)
1535 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1536 list_del_rcu(&type
->list
);
1537 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1539 EXPORT_SYMBOL_GPL(nft_unregister_expr
);
1541 static const struct nft_expr_type
*__nft_expr_type_get(u8 family
,
1544 const struct nft_expr_type
*type
;
1546 list_for_each_entry(type
, &nf_tables_expressions
, list
) {
1547 if (!nla_strcmp(nla
, type
->name
) &&
1548 (!type
->family
|| type
->family
== family
))
1554 static const struct nft_expr_type
*nft_expr_type_get(u8 family
,
1557 const struct nft_expr_type
*type
;
1560 return ERR_PTR(-EINVAL
);
1562 type
= __nft_expr_type_get(family
, nla
);
1563 if (type
!= NULL
&& try_module_get(type
->owner
))
1566 #ifdef CONFIG_MODULES
1568 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1569 request_module("nft-expr-%u-%.*s", family
,
1570 nla_len(nla
), (char *)nla_data(nla
));
1571 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1572 if (__nft_expr_type_get(family
, nla
))
1573 return ERR_PTR(-EAGAIN
);
1575 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1576 request_module("nft-expr-%.*s",
1577 nla_len(nla
), (char *)nla_data(nla
));
1578 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1579 if (__nft_expr_type_get(family
, nla
))
1580 return ERR_PTR(-EAGAIN
);
1583 return ERR_PTR(-ENOENT
);
1586 static const struct nla_policy nft_expr_policy
[NFTA_EXPR_MAX
+ 1] = {
1587 [NFTA_EXPR_NAME
] = { .type
= NLA_STRING
},
1588 [NFTA_EXPR_DATA
] = { .type
= NLA_NESTED
},
1591 static int nf_tables_fill_expr_info(struct sk_buff
*skb
,
1592 const struct nft_expr
*expr
)
1594 if (nla_put_string(skb
, NFTA_EXPR_NAME
, expr
->ops
->type
->name
))
1595 goto nla_put_failure
;
1597 if (expr
->ops
->dump
) {
1598 struct nlattr
*data
= nla_nest_start(skb
, NFTA_EXPR_DATA
);
1600 goto nla_put_failure
;
1601 if (expr
->ops
->dump(skb
, expr
) < 0)
1602 goto nla_put_failure
;
1603 nla_nest_end(skb
, data
);
1612 int nft_expr_dump(struct sk_buff
*skb
, unsigned int attr
,
1613 const struct nft_expr
*expr
)
1615 struct nlattr
*nest
;
1617 nest
= nla_nest_start(skb
, attr
);
1619 goto nla_put_failure
;
1620 if (nf_tables_fill_expr_info(skb
, expr
) < 0)
1621 goto nla_put_failure
;
1622 nla_nest_end(skb
, nest
);
1629 struct nft_expr_info
{
1630 const struct nft_expr_ops
*ops
;
1631 struct nlattr
*tb
[NFT_EXPR_MAXATTR
+ 1];
1634 static int nf_tables_expr_parse(const struct nft_ctx
*ctx
,
1635 const struct nlattr
*nla
,
1636 struct nft_expr_info
*info
)
1638 const struct nft_expr_type
*type
;
1639 const struct nft_expr_ops
*ops
;
1640 struct nlattr
*tb
[NFTA_EXPR_MAX
+ 1];
1643 err
= nla_parse_nested(tb
, NFTA_EXPR_MAX
, nla
, nft_expr_policy
);
1647 type
= nft_expr_type_get(ctx
->afi
->family
, tb
[NFTA_EXPR_NAME
]);
1649 return PTR_ERR(type
);
1651 if (tb
[NFTA_EXPR_DATA
]) {
1652 err
= nla_parse_nested(info
->tb
, type
->maxattr
,
1653 tb
[NFTA_EXPR_DATA
], type
->policy
);
1657 memset(info
->tb
, 0, sizeof(info
->tb
[0]) * (type
->maxattr
+ 1));
1659 if (type
->select_ops
!= NULL
) {
1660 ops
= type
->select_ops(ctx
,
1661 (const struct nlattr
* const *)info
->tb
);
1673 module_put(type
->owner
);
1677 static int nf_tables_newexpr(const struct nft_ctx
*ctx
,
1678 const struct nft_expr_info
*info
,
1679 struct nft_expr
*expr
)
1681 const struct nft_expr_ops
*ops
= info
->ops
;
1686 err
= ops
->init(ctx
, expr
, (const struct nlattr
**)info
->tb
);
1698 static void nf_tables_expr_destroy(const struct nft_ctx
*ctx
,
1699 struct nft_expr
*expr
)
1701 if (expr
->ops
->destroy
)
1702 expr
->ops
->destroy(ctx
, expr
);
1703 module_put(expr
->ops
->type
->owner
);
1706 struct nft_expr
*nft_expr_init(const struct nft_ctx
*ctx
,
1707 const struct nlattr
*nla
)
1709 struct nft_expr_info info
;
1710 struct nft_expr
*expr
;
1713 err
= nf_tables_expr_parse(ctx
, nla
, &info
);
1718 expr
= kzalloc(info
.ops
->size
, GFP_KERNEL
);
1722 err
= nf_tables_newexpr(ctx
, &info
, expr
);
1728 module_put(info
.ops
->type
->owner
);
1730 return ERR_PTR(err
);
1733 void nft_expr_destroy(const struct nft_ctx
*ctx
, struct nft_expr
*expr
)
1735 nf_tables_expr_destroy(ctx
, expr
);
1743 static struct nft_rule
*__nf_tables_rule_lookup(const struct nft_chain
*chain
,
1746 struct nft_rule
*rule
;
1748 // FIXME: this sucks
1749 list_for_each_entry(rule
, &chain
->rules
, list
) {
1750 if (handle
== rule
->handle
)
1754 return ERR_PTR(-ENOENT
);
1757 static struct nft_rule
*nf_tables_rule_lookup(const struct nft_chain
*chain
,
1758 const struct nlattr
*nla
)
1761 return ERR_PTR(-EINVAL
);
1763 return __nf_tables_rule_lookup(chain
, be64_to_cpu(nla_get_be64(nla
)));
1766 static const struct nla_policy nft_rule_policy
[NFTA_RULE_MAX
+ 1] = {
1767 [NFTA_RULE_TABLE
] = { .type
= NLA_STRING
},
1768 [NFTA_RULE_CHAIN
] = { .type
= NLA_STRING
,
1769 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
1770 [NFTA_RULE_HANDLE
] = { .type
= NLA_U64
},
1771 [NFTA_RULE_EXPRESSIONS
] = { .type
= NLA_NESTED
},
1772 [NFTA_RULE_COMPAT
] = { .type
= NLA_NESTED
},
1773 [NFTA_RULE_POSITION
] = { .type
= NLA_U64
},
1774 [NFTA_RULE_USERDATA
] = { .type
= NLA_BINARY
,
1775 .len
= NFT_USERDATA_MAXLEN
},
1778 static int nf_tables_fill_rule_info(struct sk_buff
*skb
, struct net
*net
,
1779 u32 portid
, u32 seq
, int event
,
1780 u32 flags
, int family
,
1781 const struct nft_table
*table
,
1782 const struct nft_chain
*chain
,
1783 const struct nft_rule
*rule
)
1785 struct nlmsghdr
*nlh
;
1786 struct nfgenmsg
*nfmsg
;
1787 const struct nft_expr
*expr
, *next
;
1788 struct nlattr
*list
;
1789 const struct nft_rule
*prule
;
1790 int type
= event
| NFNL_SUBSYS_NFTABLES
<< 8;
1792 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(struct nfgenmsg
),
1795 goto nla_put_failure
;
1797 nfmsg
= nlmsg_data(nlh
);
1798 nfmsg
->nfgen_family
= family
;
1799 nfmsg
->version
= NFNETLINK_V0
;
1800 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
1802 if (nla_put_string(skb
, NFTA_RULE_TABLE
, table
->name
))
1803 goto nla_put_failure
;
1804 if (nla_put_string(skb
, NFTA_RULE_CHAIN
, chain
->name
))
1805 goto nla_put_failure
;
1806 if (nla_put_be64(skb
, NFTA_RULE_HANDLE
, cpu_to_be64(rule
->handle
)))
1807 goto nla_put_failure
;
1809 if ((event
!= NFT_MSG_DELRULE
) && (rule
->list
.prev
!= &chain
->rules
)) {
1810 prule
= list_entry(rule
->list
.prev
, struct nft_rule
, list
);
1811 if (nla_put_be64(skb
, NFTA_RULE_POSITION
,
1812 cpu_to_be64(prule
->handle
)))
1813 goto nla_put_failure
;
1816 list
= nla_nest_start(skb
, NFTA_RULE_EXPRESSIONS
);
1818 goto nla_put_failure
;
1819 nft_rule_for_each_expr(expr
, next
, rule
) {
1820 if (nft_expr_dump(skb
, NFTA_LIST_ELEM
, expr
) < 0)
1821 goto nla_put_failure
;
1823 nla_nest_end(skb
, list
);
1826 struct nft_userdata
*udata
= nft_userdata(rule
);
1827 if (nla_put(skb
, NFTA_RULE_USERDATA
, udata
->len
+ 1,
1829 goto nla_put_failure
;
1832 nlmsg_end(skb
, nlh
);
1836 nlmsg_trim(skb
, nlh
);
1840 static int nf_tables_rule_notify(const struct nft_ctx
*ctx
,
1841 const struct nft_rule
*rule
,
1844 struct sk_buff
*skb
;
1848 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
1852 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1856 err
= nf_tables_fill_rule_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
1857 event
, 0, ctx
->afi
->family
, ctx
->table
,
1864 err
= nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1865 ctx
->report
, GFP_KERNEL
);
1868 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1874 static int nf_tables_dump_rules(struct sk_buff
*skb
,
1875 struct netlink_callback
*cb
)
1877 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1878 const struct nft_af_info
*afi
;
1879 const struct nft_table
*table
;
1880 const struct nft_chain
*chain
;
1881 const struct nft_rule
*rule
;
1882 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1883 struct net
*net
= sock_net(skb
->sk
);
1884 int family
= nfmsg
->nfgen_family
;
1887 cb
->seq
= net
->nft
.base_seq
;
1889 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
1890 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
1893 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
1894 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
1895 list_for_each_entry_rcu(rule
, &chain
->rules
, list
) {
1896 if (!nft_rule_is_active(net
, rule
))
1901 memset(&cb
->args
[1], 0,
1902 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1903 if (nf_tables_fill_rule_info(skb
, net
, NETLINK_CB(cb
->skb
).portid
,
1906 NLM_F_MULTI
| NLM_F_APPEND
,
1907 afi
->family
, table
, chain
, rule
) < 0)
1910 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1924 static int nf_tables_getrule(struct net
*net
, struct sock
*nlsk
,
1925 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1926 const struct nlattr
* const nla
[])
1928 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1929 const struct nft_af_info
*afi
;
1930 const struct nft_table
*table
;
1931 const struct nft_chain
*chain
;
1932 const struct nft_rule
*rule
;
1933 struct sk_buff
*skb2
;
1934 int family
= nfmsg
->nfgen_family
;
1937 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1938 struct netlink_dump_control c
= {
1939 .dump
= nf_tables_dump_rules
,
1941 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
1944 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1946 return PTR_ERR(afi
);
1948 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
1950 return PTR_ERR(table
);
1951 if (table
->flags
& NFT_TABLE_INACTIVE
)
1954 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
1956 return PTR_ERR(chain
);
1957 if (chain
->flags
& NFT_CHAIN_INACTIVE
)
1960 rule
= nf_tables_rule_lookup(chain
, nla
[NFTA_RULE_HANDLE
]);
1962 return PTR_ERR(rule
);
1964 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1968 err
= nf_tables_fill_rule_info(skb2
, net
, NETLINK_CB(skb
).portid
,
1969 nlh
->nlmsg_seq
, NFT_MSG_NEWRULE
, 0,
1970 family
, table
, chain
, rule
);
1974 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
1981 static void nf_tables_rule_destroy(const struct nft_ctx
*ctx
,
1982 struct nft_rule
*rule
)
1984 struct nft_expr
*expr
;
1987 * Careful: some expressions might not be initialized in case this
1988 * is called on error from nf_tables_newrule().
1990 expr
= nft_expr_first(rule
);
1991 while (expr
->ops
&& expr
!= nft_expr_last(rule
)) {
1992 nf_tables_expr_destroy(ctx
, expr
);
1993 expr
= nft_expr_next(expr
);
1998 #define NFT_RULE_MAXEXPRS 128
2000 static struct nft_expr_info
*info
;
2002 static int nf_tables_newrule(struct net
*net
, struct sock
*nlsk
,
2003 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2004 const struct nlattr
* const nla
[])
2006 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2007 struct nft_af_info
*afi
;
2008 struct nft_table
*table
;
2009 struct nft_chain
*chain
;
2010 struct nft_rule
*rule
, *old_rule
= NULL
;
2011 struct nft_userdata
*udata
;
2012 struct nft_trans
*trans
= NULL
;
2013 struct nft_expr
*expr
;
2016 unsigned int size
, i
, n
, ulen
= 0, usize
= 0;
2019 u64 handle
, pos_handle
;
2021 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
2023 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, create
);
2025 return PTR_ERR(afi
);
2027 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
2029 return PTR_ERR(table
);
2031 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
2033 return PTR_ERR(chain
);
2035 if (nla
[NFTA_RULE_HANDLE
]) {
2036 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_HANDLE
]));
2037 rule
= __nf_tables_rule_lookup(chain
, handle
);
2039 return PTR_ERR(rule
);
2041 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2043 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2048 if (!create
|| nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2050 handle
= nf_tables_alloc_handle(table
);
2052 if (chain
->use
== UINT_MAX
)
2056 if (nla
[NFTA_RULE_POSITION
]) {
2057 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
2060 pos_handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_POSITION
]));
2061 old_rule
= __nf_tables_rule_lookup(chain
, pos_handle
);
2062 if (IS_ERR(old_rule
))
2063 return PTR_ERR(old_rule
);
2066 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
2070 if (nla
[NFTA_RULE_EXPRESSIONS
]) {
2071 nla_for_each_nested(tmp
, nla
[NFTA_RULE_EXPRESSIONS
], rem
) {
2073 if (nla_type(tmp
) != NFTA_LIST_ELEM
)
2075 if (n
== NFT_RULE_MAXEXPRS
)
2077 err
= nf_tables_expr_parse(&ctx
, tmp
, &info
[n
]);
2080 size
+= info
[n
].ops
->size
;
2084 /* Check for overflow of dlen field */
2086 if (size
>= 1 << 12)
2089 if (nla
[NFTA_RULE_USERDATA
]) {
2090 ulen
= nla_len(nla
[NFTA_RULE_USERDATA
]);
2092 usize
= sizeof(struct nft_userdata
) + ulen
;
2096 rule
= kzalloc(sizeof(*rule
) + size
+ usize
, GFP_KERNEL
);
2100 nft_rule_activate_next(net
, rule
);
2102 rule
->handle
= handle
;
2104 rule
->udata
= ulen
? 1 : 0;
2107 udata
= nft_userdata(rule
);
2108 udata
->len
= ulen
- 1;
2109 nla_memcpy(udata
->data
, nla
[NFTA_RULE_USERDATA
], ulen
);
2112 expr
= nft_expr_first(rule
);
2113 for (i
= 0; i
< n
; i
++) {
2114 err
= nf_tables_newexpr(&ctx
, &info
[i
], expr
);
2118 expr
= nft_expr_next(expr
);
2121 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
2122 if (nft_rule_is_active_next(net
, old_rule
)) {
2123 trans
= nft_trans_rule_add(&ctx
, NFT_MSG_DELRULE
,
2125 if (trans
== NULL
) {
2129 nft_rule_deactivate_next(net
, old_rule
);
2131 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
2136 } else if (nlh
->nlmsg_flags
& NLM_F_APPEND
)
2138 list_add_rcu(&rule
->list
, &old_rule
->list
);
2140 list_add_tail_rcu(&rule
->list
, &chain
->rules
);
2143 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
2145 list_add_rcu(&rule
->list
, &chain
->rules
);
2148 if (nft_trans_rule_add(&ctx
, NFT_MSG_NEWRULE
, rule
) == NULL
) {
2156 list_del_rcu(&rule
->list
);
2158 nf_tables_rule_destroy(&ctx
, rule
);
2160 for (i
= 0; i
< n
; i
++) {
2161 if (info
[i
].ops
!= NULL
)
2162 module_put(info
[i
].ops
->type
->owner
);
2167 static int nf_tables_delrule(struct net
*net
, struct sock
*nlsk
,
2168 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2169 const struct nlattr
* const nla
[])
2171 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2172 struct nft_af_info
*afi
;
2173 struct nft_table
*table
;
2174 struct nft_chain
*chain
= NULL
;
2175 struct nft_rule
*rule
;
2176 int family
= nfmsg
->nfgen_family
, err
= 0;
2179 afi
= nf_tables_afinfo_lookup(net
, family
, false);
2181 return PTR_ERR(afi
);
2183 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
2185 return PTR_ERR(table
);
2187 if (nla
[NFTA_RULE_CHAIN
]) {
2188 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
2190 return PTR_ERR(chain
);
2193 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
2196 if (nla
[NFTA_RULE_HANDLE
]) {
2197 rule
= nf_tables_rule_lookup(chain
,
2198 nla
[NFTA_RULE_HANDLE
]);
2200 return PTR_ERR(rule
);
2202 err
= nft_delrule(&ctx
, rule
);
2204 err
= nft_delrule_by_chain(&ctx
);
2207 list_for_each_entry(chain
, &table
->chains
, list
) {
2209 err
= nft_delrule_by_chain(&ctx
);
2222 static LIST_HEAD(nf_tables_set_ops
);
2224 int nft_register_set(struct nft_set_ops
*ops
)
2226 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2227 list_add_tail_rcu(&ops
->list
, &nf_tables_set_ops
);
2228 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2231 EXPORT_SYMBOL_GPL(nft_register_set
);
2233 void nft_unregister_set(struct nft_set_ops
*ops
)
2235 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2236 list_del_rcu(&ops
->list
);
2237 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2239 EXPORT_SYMBOL_GPL(nft_unregister_set
);
2242 * Select a set implementation based on the data characteristics and the
2243 * given policy. The total memory use might not be known if no size is
2244 * given, in that case the amount of memory per element is used.
2246 static const struct nft_set_ops
*
2247 nft_select_set_ops(const struct nlattr
* const nla
[],
2248 const struct nft_set_desc
*desc
,
2249 enum nft_set_policies policy
)
2251 const struct nft_set_ops
*ops
, *bops
;
2252 struct nft_set_estimate est
, best
;
2255 #ifdef CONFIG_MODULES
2256 if (list_empty(&nf_tables_set_ops
)) {
2257 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2258 request_module("nft-set");
2259 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2260 if (!list_empty(&nf_tables_set_ops
))
2261 return ERR_PTR(-EAGAIN
);
2265 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
2266 features
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
2267 features
&= NFT_SET_INTERVAL
| NFT_SET_MAP
| NFT_SET_TIMEOUT
;
2274 list_for_each_entry(ops
, &nf_tables_set_ops
, list
) {
2275 if ((ops
->features
& features
) != features
)
2277 if (!ops
->estimate(desc
, features
, &est
))
2281 case NFT_SET_POL_PERFORMANCE
:
2282 if (est
.class < best
.class)
2284 if (est
.class == best
.class && est
.size
< best
.size
)
2287 case NFT_SET_POL_MEMORY
:
2288 if (est
.size
< best
.size
)
2290 if (est
.size
== best
.size
&& est
.class < best
.class)
2297 if (!try_module_get(ops
->owner
))
2300 module_put(bops
->owner
);
2309 return ERR_PTR(-EOPNOTSUPP
);
2312 static const struct nla_policy nft_set_policy
[NFTA_SET_MAX
+ 1] = {
2313 [NFTA_SET_TABLE
] = { .type
= NLA_STRING
},
2314 [NFTA_SET_NAME
] = { .type
= NLA_STRING
,
2315 .len
= IFNAMSIZ
- 1 },
2316 [NFTA_SET_FLAGS
] = { .type
= NLA_U32
},
2317 [NFTA_SET_KEY_TYPE
] = { .type
= NLA_U32
},
2318 [NFTA_SET_KEY_LEN
] = { .type
= NLA_U32
},
2319 [NFTA_SET_DATA_TYPE
] = { .type
= NLA_U32
},
2320 [NFTA_SET_DATA_LEN
] = { .type
= NLA_U32
},
2321 [NFTA_SET_POLICY
] = { .type
= NLA_U32
},
2322 [NFTA_SET_DESC
] = { .type
= NLA_NESTED
},
2323 [NFTA_SET_ID
] = { .type
= NLA_U32
},
2324 [NFTA_SET_TIMEOUT
] = { .type
= NLA_U64
},
2325 [NFTA_SET_GC_INTERVAL
] = { .type
= NLA_U32
},
2326 [NFTA_SET_USERDATA
] = { .type
= NLA_BINARY
,
2327 .len
= NFT_USERDATA_MAXLEN
},
2330 static const struct nla_policy nft_set_desc_policy
[NFTA_SET_DESC_MAX
+ 1] = {
2331 [NFTA_SET_DESC_SIZE
] = { .type
= NLA_U32
},
2334 static int nft_ctx_init_from_setattr(struct nft_ctx
*ctx
, struct net
*net
,
2335 const struct sk_buff
*skb
,
2336 const struct nlmsghdr
*nlh
,
2337 const struct nlattr
* const nla
[])
2339 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2340 struct nft_af_info
*afi
= NULL
;
2341 struct nft_table
*table
= NULL
;
2343 if (nfmsg
->nfgen_family
!= NFPROTO_UNSPEC
) {
2344 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, false);
2346 return PTR_ERR(afi
);
2349 if (nla
[NFTA_SET_TABLE
] != NULL
) {
2351 return -EAFNOSUPPORT
;
2353 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
]);
2355 return PTR_ERR(table
);
2358 nft_ctx_init(ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
2362 struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
2363 const struct nlattr
*nla
)
2365 struct nft_set
*set
;
2368 return ERR_PTR(-EINVAL
);
2370 list_for_each_entry(set
, &table
->sets
, list
) {
2371 if (!nla_strcmp(nla
, set
->name
))
2374 return ERR_PTR(-ENOENT
);
2377 struct nft_set
*nf_tables_set_lookup_byid(const struct net
*net
,
2378 const struct nlattr
*nla
)
2380 struct nft_trans
*trans
;
2381 u32 id
= ntohl(nla_get_be32(nla
));
2383 list_for_each_entry(trans
, &net
->nft
.commit_list
, list
) {
2384 if (trans
->msg_type
== NFT_MSG_NEWSET
&&
2385 id
== nft_trans_set_id(trans
))
2386 return nft_trans_set(trans
);
2388 return ERR_PTR(-ENOENT
);
2391 static int nf_tables_set_alloc_name(struct nft_ctx
*ctx
, struct nft_set
*set
,
2394 const struct nft_set
*i
;
2396 unsigned long *inuse
;
2397 unsigned int n
= 0, min
= 0;
2399 p
= strnchr(name
, IFNAMSIZ
, '%');
2401 if (p
[1] != 'd' || strchr(p
+ 2, '%'))
2404 inuse
= (unsigned long *)get_zeroed_page(GFP_KERNEL
);
2408 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
2411 if (!sscanf(i
->name
, name
, &tmp
))
2413 if (tmp
< min
|| tmp
>= min
+ BITS_PER_BYTE
* PAGE_SIZE
)
2416 set_bit(tmp
- min
, inuse
);
2419 n
= find_first_zero_bit(inuse
, BITS_PER_BYTE
* PAGE_SIZE
);
2420 if (n
>= BITS_PER_BYTE
* PAGE_SIZE
) {
2421 min
+= BITS_PER_BYTE
* PAGE_SIZE
;
2422 memset(inuse
, 0, PAGE_SIZE
);
2425 free_page((unsigned long)inuse
);
2428 snprintf(set
->name
, sizeof(set
->name
), name
, min
+ n
);
2429 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
2430 if (!strcmp(set
->name
, i
->name
))
2436 static int nf_tables_fill_set(struct sk_buff
*skb
, const struct nft_ctx
*ctx
,
2437 const struct nft_set
*set
, u16 event
, u16 flags
)
2439 struct nfgenmsg
*nfmsg
;
2440 struct nlmsghdr
*nlh
;
2441 struct nlattr
*desc
;
2442 u32 portid
= ctx
->portid
;
2445 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
2446 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
2449 goto nla_put_failure
;
2451 nfmsg
= nlmsg_data(nlh
);
2452 nfmsg
->nfgen_family
= ctx
->afi
->family
;
2453 nfmsg
->version
= NFNETLINK_V0
;
2454 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
2456 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
2457 goto nla_put_failure
;
2458 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
2459 goto nla_put_failure
;
2460 if (set
->flags
!= 0)
2461 if (nla_put_be32(skb
, NFTA_SET_FLAGS
, htonl(set
->flags
)))
2462 goto nla_put_failure
;
2464 if (nla_put_be32(skb
, NFTA_SET_KEY_TYPE
, htonl(set
->ktype
)))
2465 goto nla_put_failure
;
2466 if (nla_put_be32(skb
, NFTA_SET_KEY_LEN
, htonl(set
->klen
)))
2467 goto nla_put_failure
;
2468 if (set
->flags
& NFT_SET_MAP
) {
2469 if (nla_put_be32(skb
, NFTA_SET_DATA_TYPE
, htonl(set
->dtype
)))
2470 goto nla_put_failure
;
2471 if (nla_put_be32(skb
, NFTA_SET_DATA_LEN
, htonl(set
->dlen
)))
2472 goto nla_put_failure
;
2476 nla_put_be64(skb
, NFTA_SET_TIMEOUT
, cpu_to_be64(set
->timeout
)))
2477 goto nla_put_failure
;
2479 nla_put_be32(skb
, NFTA_SET_GC_INTERVAL
, htonl(set
->gc_int
)))
2480 goto nla_put_failure
;
2482 if (set
->policy
!= NFT_SET_POL_PERFORMANCE
) {
2483 if (nla_put_be32(skb
, NFTA_SET_POLICY
, htonl(set
->policy
)))
2484 goto nla_put_failure
;
2487 if (nla_put(skb
, NFTA_SET_USERDATA
, set
->udlen
, set
->udata
))
2488 goto nla_put_failure
;
2490 desc
= nla_nest_start(skb
, NFTA_SET_DESC
);
2492 goto nla_put_failure
;
2494 nla_put_be32(skb
, NFTA_SET_DESC_SIZE
, htonl(set
->size
)))
2495 goto nla_put_failure
;
2496 nla_nest_end(skb
, desc
);
2498 nlmsg_end(skb
, nlh
);
2502 nlmsg_trim(skb
, nlh
);
2506 static int nf_tables_set_notify(const struct nft_ctx
*ctx
,
2507 const struct nft_set
*set
,
2508 int event
, gfp_t gfp_flags
)
2510 struct sk_buff
*skb
;
2511 u32 portid
= ctx
->portid
;
2515 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
2519 skb
= nlmsg_new(NLMSG_GOODSIZE
, gfp_flags
);
2523 err
= nf_tables_fill_set(skb
, ctx
, set
, event
, 0);
2529 err
= nfnetlink_send(skb
, ctx
->net
, portid
, NFNLGRP_NFTABLES
,
2530 ctx
->report
, gfp_flags
);
2533 nfnetlink_set_err(ctx
->net
, portid
, NFNLGRP_NFTABLES
, err
);
2537 static int nf_tables_dump_sets(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2539 const struct nft_set
*set
;
2540 unsigned int idx
, s_idx
= cb
->args
[0];
2541 struct nft_af_info
*afi
;
2542 struct nft_table
*table
, *cur_table
= (struct nft_table
*)cb
->args
[2];
2543 struct net
*net
= sock_net(skb
->sk
);
2544 int cur_family
= cb
->args
[3];
2545 struct nft_ctx
*ctx
= cb
->data
, ctx_set
;
2551 cb
->seq
= net
->nft
.base_seq
;
2553 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
2554 if (ctx
->afi
&& ctx
->afi
!= afi
)
2558 if (afi
->family
!= cur_family
)
2563 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
2564 if (ctx
->table
&& ctx
->table
!= table
)
2568 if (cur_table
!= table
)
2574 list_for_each_entry_rcu(set
, &table
->sets
, list
) {
2579 ctx_set
.table
= table
;
2581 if (nf_tables_fill_set(skb
, &ctx_set
, set
,
2585 cb
->args
[2] = (unsigned long) table
;
2586 cb
->args
[3] = afi
->family
;
2589 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2603 static int nf_tables_dump_sets_done(struct netlink_callback
*cb
)
2609 static int nf_tables_getset(struct net
*net
, struct sock
*nlsk
,
2610 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2611 const struct nlattr
* const nla
[])
2613 const struct nft_set
*set
;
2615 struct sk_buff
*skb2
;
2616 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2619 /* Verify existence before starting dump */
2620 err
= nft_ctx_init_from_setattr(&ctx
, net
, skb
, nlh
, nla
);
2624 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2625 struct netlink_dump_control c
= {
2626 .dump
= nf_tables_dump_sets
,
2627 .done
= nf_tables_dump_sets_done
,
2629 struct nft_ctx
*ctx_dump
;
2631 ctx_dump
= kmalloc(sizeof(*ctx_dump
), GFP_KERNEL
);
2632 if (ctx_dump
== NULL
)
2638 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
2641 /* Only accept unspec with dump */
2642 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
2643 return -EAFNOSUPPORT
;
2645 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
]);
2647 return PTR_ERR(set
);
2648 if (set
->flags
& NFT_SET_INACTIVE
)
2651 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2655 err
= nf_tables_fill_set(skb2
, &ctx
, set
, NFT_MSG_NEWSET
, 0);
2659 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
2666 static int nf_tables_set_desc_parse(const struct nft_ctx
*ctx
,
2667 struct nft_set_desc
*desc
,
2668 const struct nlattr
*nla
)
2670 struct nlattr
*da
[NFTA_SET_DESC_MAX
+ 1];
2673 err
= nla_parse_nested(da
, NFTA_SET_DESC_MAX
, nla
, nft_set_desc_policy
);
2677 if (da
[NFTA_SET_DESC_SIZE
] != NULL
)
2678 desc
->size
= ntohl(nla_get_be32(da
[NFTA_SET_DESC_SIZE
]));
2683 static int nf_tables_newset(struct net
*net
, struct sock
*nlsk
,
2684 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2685 const struct nlattr
* const nla
[])
2687 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2688 const struct nft_set_ops
*ops
;
2689 struct nft_af_info
*afi
;
2690 struct nft_table
*table
;
2691 struct nft_set
*set
;
2693 char name
[IFNAMSIZ
];
2697 u32 ktype
, dtype
, flags
, policy
, gc_int
;
2698 struct nft_set_desc desc
;
2699 unsigned char *udata
;
2703 if (nla
[NFTA_SET_TABLE
] == NULL
||
2704 nla
[NFTA_SET_NAME
] == NULL
||
2705 nla
[NFTA_SET_KEY_LEN
] == NULL
||
2706 nla
[NFTA_SET_ID
] == NULL
)
2709 memset(&desc
, 0, sizeof(desc
));
2711 ktype
= NFT_DATA_VALUE
;
2712 if (nla
[NFTA_SET_KEY_TYPE
] != NULL
) {
2713 ktype
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_TYPE
]));
2714 if ((ktype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
)
2718 desc
.klen
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_LEN
]));
2719 if (desc
.klen
== 0 || desc
.klen
> NFT_DATA_VALUE_MAXLEN
)
2723 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
2724 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
2725 if (flags
& ~(NFT_SET_ANONYMOUS
| NFT_SET_CONSTANT
|
2726 NFT_SET_INTERVAL
| NFT_SET_TIMEOUT
|
2727 NFT_SET_MAP
| NFT_SET_EVAL
))
2729 /* Only one of both operations is supported */
2730 if ((flags
& (NFT_SET_MAP
| NFT_SET_EVAL
)) ==
2731 (NFT_SET_MAP
| NFT_SET_EVAL
))
2736 if (nla
[NFTA_SET_DATA_TYPE
] != NULL
) {
2737 if (!(flags
& NFT_SET_MAP
))
2740 dtype
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_TYPE
]));
2741 if ((dtype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
&&
2742 dtype
!= NFT_DATA_VERDICT
)
2745 if (dtype
!= NFT_DATA_VERDICT
) {
2746 if (nla
[NFTA_SET_DATA_LEN
] == NULL
)
2748 desc
.dlen
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_LEN
]));
2749 if (desc
.dlen
== 0 || desc
.dlen
> NFT_DATA_VALUE_MAXLEN
)
2752 desc
.dlen
= sizeof(struct nft_verdict
);
2753 } else if (flags
& NFT_SET_MAP
)
2757 if (nla
[NFTA_SET_TIMEOUT
] != NULL
) {
2758 if (!(flags
& NFT_SET_TIMEOUT
))
2760 timeout
= be64_to_cpu(nla_get_be64(nla
[NFTA_SET_TIMEOUT
]));
2763 if (nla
[NFTA_SET_GC_INTERVAL
] != NULL
) {
2764 if (!(flags
& NFT_SET_TIMEOUT
))
2766 gc_int
= ntohl(nla_get_be32(nla
[NFTA_SET_GC_INTERVAL
]));
2769 policy
= NFT_SET_POL_PERFORMANCE
;
2770 if (nla
[NFTA_SET_POLICY
] != NULL
)
2771 policy
= ntohl(nla_get_be32(nla
[NFTA_SET_POLICY
]));
2773 if (nla
[NFTA_SET_DESC
] != NULL
) {
2774 err
= nf_tables_set_desc_parse(&ctx
, &desc
, nla
[NFTA_SET_DESC
]);
2779 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
2781 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, create
);
2783 return PTR_ERR(afi
);
2785 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
]);
2787 return PTR_ERR(table
);
2789 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
2791 set
= nf_tables_set_lookup(table
, nla
[NFTA_SET_NAME
]);
2793 if (PTR_ERR(set
) != -ENOENT
)
2794 return PTR_ERR(set
);
2799 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2801 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2806 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
2809 ops
= nft_select_set_ops(nla
, &desc
, policy
);
2811 return PTR_ERR(ops
);
2814 if (nla
[NFTA_SET_USERDATA
])
2815 udlen
= nla_len(nla
[NFTA_SET_USERDATA
]);
2818 if (ops
->privsize
!= NULL
)
2819 size
= ops
->privsize(nla
);
2822 set
= kzalloc(sizeof(*set
) + size
+ udlen
, GFP_KERNEL
);
2826 nla_strlcpy(name
, nla
[NFTA_SET_NAME
], sizeof(set
->name
));
2827 err
= nf_tables_set_alloc_name(&ctx
, set
, name
);
2833 udata
= set
->data
+ size
;
2834 nla_memcpy(udata
, nla
[NFTA_SET_USERDATA
], udlen
);
2837 INIT_LIST_HEAD(&set
->bindings
);
2838 write_pnet(&set
->pnet
, net
);
2841 set
->klen
= desc
.klen
;
2843 set
->dlen
= desc
.dlen
;
2845 set
->size
= desc
.size
;
2846 set
->policy
= policy
;
2849 set
->timeout
= timeout
;
2850 set
->gc_int
= gc_int
;
2852 err
= ops
->init(set
, &desc
, nla
);
2856 err
= nft_trans_set_add(&ctx
, NFT_MSG_NEWSET
, set
);
2860 list_add_tail_rcu(&set
->list
, &table
->sets
);
2867 module_put(ops
->owner
);
2871 static void nft_set_destroy(struct nft_set
*set
)
2873 set
->ops
->destroy(set
);
2874 module_put(set
->ops
->owner
);
2878 static void nf_tables_set_destroy(const struct nft_ctx
*ctx
, struct nft_set
*set
)
2880 list_del_rcu(&set
->list
);
2881 nf_tables_set_notify(ctx
, set
, NFT_MSG_DELSET
, GFP_ATOMIC
);
2882 nft_set_destroy(set
);
2885 static int nf_tables_delset(struct net
*net
, struct sock
*nlsk
,
2886 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2887 const struct nlattr
* const nla
[])
2889 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2890 struct nft_set
*set
;
2894 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
2895 return -EAFNOSUPPORT
;
2896 if (nla
[NFTA_SET_TABLE
] == NULL
)
2899 err
= nft_ctx_init_from_setattr(&ctx
, net
, skb
, nlh
, nla
);
2903 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
]);
2905 return PTR_ERR(set
);
2906 if (!list_empty(&set
->bindings
))
2909 return nft_delset(&ctx
, set
);
2912 static int nf_tables_bind_check_setelem(const struct nft_ctx
*ctx
,
2913 const struct nft_set
*set
,
2914 const struct nft_set_iter
*iter
,
2915 const struct nft_set_elem
*elem
)
2917 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
2918 enum nft_registers dreg
;
2920 dreg
= nft_type_to_reg(set
->dtype
);
2921 return nft_validate_register_store(ctx
, dreg
, nft_set_ext_data(ext
),
2922 set
->dtype
== NFT_DATA_VERDICT
?
2923 NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
2927 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2928 struct nft_set_binding
*binding
)
2930 struct nft_set_binding
*i
;
2931 struct nft_set_iter iter
;
2933 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
)
2936 if (binding
->flags
& NFT_SET_MAP
) {
2937 /* If the set is already bound to the same chain all
2938 * jumps are already validated for that chain.
2940 list_for_each_entry(i
, &set
->bindings
, list
) {
2941 if (binding
->flags
& NFT_SET_MAP
&&
2942 i
->chain
== binding
->chain
)
2949 iter
.fn
= nf_tables_bind_check_setelem
;
2951 set
->ops
->walk(ctx
, set
, &iter
);
2953 /* Destroy anonymous sets if binding fails */
2954 if (set
->flags
& NFT_SET_ANONYMOUS
)
2955 nf_tables_set_destroy(ctx
, set
);
2961 binding
->chain
= ctx
->chain
;
2962 list_add_tail_rcu(&binding
->list
, &set
->bindings
);
2966 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2967 struct nft_set_binding
*binding
)
2969 list_del_rcu(&binding
->list
);
2971 if (list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
&&
2972 !(set
->flags
& NFT_SET_INACTIVE
))
2973 nf_tables_set_destroy(ctx
, set
);
2976 const struct nft_set_ext_type nft_set_ext_types
[] = {
2977 [NFT_SET_EXT_KEY
] = {
2978 .align
= __alignof__(u32
),
2980 [NFT_SET_EXT_DATA
] = {
2981 .align
= __alignof__(u32
),
2983 [NFT_SET_EXT_EXPR
] = {
2984 .align
= __alignof__(struct nft_expr
),
2986 [NFT_SET_EXT_FLAGS
] = {
2988 .align
= __alignof__(u8
),
2990 [NFT_SET_EXT_TIMEOUT
] = {
2992 .align
= __alignof__(u64
),
2994 [NFT_SET_EXT_EXPIRATION
] = {
2995 .len
= sizeof(unsigned long),
2996 .align
= __alignof__(unsigned long),
2998 [NFT_SET_EXT_USERDATA
] = {
2999 .len
= sizeof(struct nft_userdata
),
3000 .align
= __alignof__(struct nft_userdata
),
3003 EXPORT_SYMBOL_GPL(nft_set_ext_types
);
3009 static const struct nla_policy nft_set_elem_policy
[NFTA_SET_ELEM_MAX
+ 1] = {
3010 [NFTA_SET_ELEM_KEY
] = { .type
= NLA_NESTED
},
3011 [NFTA_SET_ELEM_DATA
] = { .type
= NLA_NESTED
},
3012 [NFTA_SET_ELEM_FLAGS
] = { .type
= NLA_U32
},
3013 [NFTA_SET_ELEM_TIMEOUT
] = { .type
= NLA_U64
},
3014 [NFTA_SET_ELEM_USERDATA
] = { .type
= NLA_BINARY
,
3015 .len
= NFT_USERDATA_MAXLEN
},
3018 static const struct nla_policy nft_set_elem_list_policy
[NFTA_SET_ELEM_LIST_MAX
+ 1] = {
3019 [NFTA_SET_ELEM_LIST_TABLE
] = { .type
= NLA_STRING
},
3020 [NFTA_SET_ELEM_LIST_SET
] = { .type
= NLA_STRING
},
3021 [NFTA_SET_ELEM_LIST_ELEMENTS
] = { .type
= NLA_NESTED
},
3022 [NFTA_SET_ELEM_LIST_SET_ID
] = { .type
= NLA_U32
},
3025 static int nft_ctx_init_from_elemattr(struct nft_ctx
*ctx
, struct net
*net
,
3026 const struct sk_buff
*skb
,
3027 const struct nlmsghdr
*nlh
,
3028 const struct nlattr
* const nla
[])
3030 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3031 struct nft_af_info
*afi
;
3032 struct nft_table
*table
;
3034 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, false);
3036 return PTR_ERR(afi
);
3038 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_ELEM_LIST_TABLE
]);
3040 return PTR_ERR(table
);
3042 nft_ctx_init(ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
3046 static int nf_tables_fill_setelem(struct sk_buff
*skb
,
3047 const struct nft_set
*set
,
3048 const struct nft_set_elem
*elem
)
3050 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
3051 unsigned char *b
= skb_tail_pointer(skb
);
3052 struct nlattr
*nest
;
3054 nest
= nla_nest_start(skb
, NFTA_LIST_ELEM
);
3056 goto nla_put_failure
;
3058 if (nft_data_dump(skb
, NFTA_SET_ELEM_KEY
, nft_set_ext_key(ext
),
3059 NFT_DATA_VALUE
, set
->klen
) < 0)
3060 goto nla_put_failure
;
3062 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
) &&
3063 nft_data_dump(skb
, NFTA_SET_ELEM_DATA
, nft_set_ext_data(ext
),
3064 set
->dtype
== NFT_DATA_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
3066 goto nla_put_failure
;
3068 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
) &&
3069 nft_expr_dump(skb
, NFTA_SET_ELEM_EXPR
, nft_set_ext_expr(ext
)) < 0)
3070 goto nla_put_failure
;
3072 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
3073 nla_put_be32(skb
, NFTA_SET_ELEM_FLAGS
,
3074 htonl(*nft_set_ext_flags(ext
))))
3075 goto nla_put_failure
;
3077 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
) &&
3078 nla_put_be64(skb
, NFTA_SET_ELEM_TIMEOUT
,
3079 cpu_to_be64(*nft_set_ext_timeout(ext
))))
3080 goto nla_put_failure
;
3082 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
)) {
3083 unsigned long expires
, now
= jiffies
;
3085 expires
= *nft_set_ext_expiration(ext
);
3086 if (time_before(now
, expires
))
3091 if (nla_put_be64(skb
, NFTA_SET_ELEM_EXPIRATION
,
3092 cpu_to_be64(jiffies_to_msecs(expires
))))
3093 goto nla_put_failure
;
3096 if (nft_set_ext_exists(ext
, NFT_SET_EXT_USERDATA
)) {
3097 struct nft_userdata
*udata
;
3099 udata
= nft_set_ext_userdata(ext
);
3100 if (nla_put(skb
, NFTA_SET_ELEM_USERDATA
,
3101 udata
->len
+ 1, udata
->data
))
3102 goto nla_put_failure
;
3105 nla_nest_end(skb
, nest
);
3113 struct nft_set_dump_args
{
3114 const struct netlink_callback
*cb
;
3115 struct nft_set_iter iter
;
3116 struct sk_buff
*skb
;
3119 static int nf_tables_dump_setelem(const struct nft_ctx
*ctx
,
3120 const struct nft_set
*set
,
3121 const struct nft_set_iter
*iter
,
3122 const struct nft_set_elem
*elem
)
3124 struct nft_set_dump_args
*args
;
3126 args
= container_of(iter
, struct nft_set_dump_args
, iter
);
3127 return nf_tables_fill_setelem(args
->skb
, set
, elem
);
3130 static int nf_tables_dump_set(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3132 struct net
*net
= sock_net(skb
->sk
);
3133 const struct nft_set
*set
;
3134 struct nft_set_dump_args args
;
3136 struct nlattr
*nla
[NFTA_SET_ELEM_LIST_MAX
+ 1];
3137 struct nfgenmsg
*nfmsg
;
3138 struct nlmsghdr
*nlh
;
3139 struct nlattr
*nest
;
3143 err
= nlmsg_parse(cb
->nlh
, sizeof(struct nfgenmsg
), nla
,
3144 NFTA_SET_ELEM_LIST_MAX
, nft_set_elem_list_policy
);
3148 err
= nft_ctx_init_from_elemattr(&ctx
, net
, cb
->skb
, cb
->nlh
,
3152 if (ctx
.table
->flags
& NFT_TABLE_INACTIVE
)
3155 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3157 return PTR_ERR(set
);
3158 if (set
->flags
& NFT_SET_INACTIVE
)
3161 event
= NFT_MSG_NEWSETELEM
;
3162 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
3163 portid
= NETLINK_CB(cb
->skb
).portid
;
3164 seq
= cb
->nlh
->nlmsg_seq
;
3166 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
3169 goto nla_put_failure
;
3171 nfmsg
= nlmsg_data(nlh
);
3172 nfmsg
->nfgen_family
= ctx
.afi
->family
;
3173 nfmsg
->version
= NFNETLINK_V0
;
3174 nfmsg
->res_id
= htons(ctx
.net
->nft
.base_seq
& 0xffff);
3176 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_TABLE
, ctx
.table
->name
))
3177 goto nla_put_failure
;
3178 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_SET
, set
->name
))
3179 goto nla_put_failure
;
3181 nest
= nla_nest_start(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
3183 goto nla_put_failure
;
3187 args
.iter
.skip
= cb
->args
[0];
3188 args
.iter
.count
= 0;
3190 args
.iter
.fn
= nf_tables_dump_setelem
;
3191 set
->ops
->walk(&ctx
, set
, &args
.iter
);
3193 nla_nest_end(skb
, nest
);
3194 nlmsg_end(skb
, nlh
);
3196 if (args
.iter
.err
&& args
.iter
.err
!= -EMSGSIZE
)
3197 return args
.iter
.err
;
3198 if (args
.iter
.count
== cb
->args
[0])
3201 cb
->args
[0] = args
.iter
.count
;
3208 static int nf_tables_getsetelem(struct net
*net
, struct sock
*nlsk
,
3209 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3210 const struct nlattr
* const nla
[])
3212 const struct nft_set
*set
;
3216 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
);
3219 if (ctx
.table
->flags
& NFT_TABLE_INACTIVE
)
3222 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3224 return PTR_ERR(set
);
3225 if (set
->flags
& NFT_SET_INACTIVE
)
3228 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3229 struct netlink_dump_control c
= {
3230 .dump
= nf_tables_dump_set
,
3232 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
3237 static int nf_tables_fill_setelem_info(struct sk_buff
*skb
,
3238 const struct nft_ctx
*ctx
, u32 seq
,
3239 u32 portid
, int event
, u16 flags
,
3240 const struct nft_set
*set
,
3241 const struct nft_set_elem
*elem
)
3243 struct nfgenmsg
*nfmsg
;
3244 struct nlmsghdr
*nlh
;
3245 struct nlattr
*nest
;
3248 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
3249 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
3252 goto nla_put_failure
;
3254 nfmsg
= nlmsg_data(nlh
);
3255 nfmsg
->nfgen_family
= ctx
->afi
->family
;
3256 nfmsg
->version
= NFNETLINK_V0
;
3257 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
3259 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
3260 goto nla_put_failure
;
3261 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
3262 goto nla_put_failure
;
3264 nest
= nla_nest_start(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
3266 goto nla_put_failure
;
3268 err
= nf_tables_fill_setelem(skb
, set
, elem
);
3270 goto nla_put_failure
;
3272 nla_nest_end(skb
, nest
);
3274 nlmsg_end(skb
, nlh
);
3278 nlmsg_trim(skb
, nlh
);
3282 static int nf_tables_setelem_notify(const struct nft_ctx
*ctx
,
3283 const struct nft_set
*set
,
3284 const struct nft_set_elem
*elem
,
3285 int event
, u16 flags
)
3287 struct net
*net
= ctx
->net
;
3288 u32 portid
= ctx
->portid
;
3289 struct sk_buff
*skb
;
3292 if (!ctx
->report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
3296 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
3300 err
= nf_tables_fill_setelem_info(skb
, ctx
, 0, portid
, event
, flags
,
3307 err
= nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, ctx
->report
,
3311 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, err
);
3315 static struct nft_trans
*nft_trans_elem_alloc(struct nft_ctx
*ctx
,
3317 struct nft_set
*set
)
3319 struct nft_trans
*trans
;
3321 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_elem
));
3325 nft_trans_elem_set(trans
) = set
;
3329 void *nft_set_elem_init(const struct nft_set
*set
,
3330 const struct nft_set_ext_tmpl
*tmpl
,
3331 const u32
*key
, const u32
*data
,
3332 u64 timeout
, gfp_t gfp
)
3334 struct nft_set_ext
*ext
;
3337 elem
= kzalloc(set
->ops
->elemsize
+ tmpl
->len
, gfp
);
3341 ext
= nft_set_elem_ext(set
, elem
);
3342 nft_set_ext_init(ext
, tmpl
);
3344 memcpy(nft_set_ext_key(ext
), key
, set
->klen
);
3345 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
3346 memcpy(nft_set_ext_data(ext
), data
, set
->dlen
);
3347 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
))
3348 *nft_set_ext_expiration(ext
) =
3349 jiffies
+ msecs_to_jiffies(timeout
);
3350 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
))
3351 *nft_set_ext_timeout(ext
) = timeout
;
3356 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
)
3358 struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
);
3360 nft_data_uninit(nft_set_ext_key(ext
), NFT_DATA_VALUE
);
3361 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
3362 nft_data_uninit(nft_set_ext_data(ext
), set
->dtype
);
3363 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
))
3364 nf_tables_expr_destroy(NULL
, nft_set_ext_expr(ext
));
3368 EXPORT_SYMBOL_GPL(nft_set_elem_destroy
);
3370 static int nft_add_set_elem(struct nft_ctx
*ctx
, struct nft_set
*set
,
3371 const struct nlattr
*attr
)
3373 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
3374 struct nft_data_desc d1
, d2
;
3375 struct nft_set_ext_tmpl tmpl
;
3376 struct nft_set_ext
*ext
;
3377 struct nft_set_elem elem
;
3378 struct nft_set_binding
*binding
;
3379 struct nft_userdata
*udata
;
3380 struct nft_data data
;
3381 enum nft_registers dreg
;
3382 struct nft_trans
*trans
;
3388 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
3389 nft_set_elem_policy
);
3393 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
3396 nft_set_ext_prepare(&tmpl
);
3399 if (nla
[NFTA_SET_ELEM_FLAGS
] != NULL
) {
3400 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_ELEM_FLAGS
]));
3401 if (flags
& ~NFT_SET_ELEM_INTERVAL_END
)
3403 if (!(set
->flags
& NFT_SET_INTERVAL
) &&
3404 flags
& NFT_SET_ELEM_INTERVAL_END
)
3407 nft_set_ext_add(&tmpl
, NFT_SET_EXT_FLAGS
);
3410 if (set
->flags
& NFT_SET_MAP
) {
3411 if (nla
[NFTA_SET_ELEM_DATA
] == NULL
&&
3412 !(flags
& NFT_SET_ELEM_INTERVAL_END
))
3414 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
&&
3415 flags
& NFT_SET_ELEM_INTERVAL_END
)
3418 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
3423 if (nla
[NFTA_SET_ELEM_TIMEOUT
] != NULL
) {
3424 if (!(set
->flags
& NFT_SET_TIMEOUT
))
3426 timeout
= be64_to_cpu(nla_get_be64(nla
[NFTA_SET_ELEM_TIMEOUT
]));
3427 } else if (set
->flags
& NFT_SET_TIMEOUT
) {
3428 timeout
= set
->timeout
;
3431 err
= nft_data_init(ctx
, &elem
.key
.val
, sizeof(elem
.key
), &d1
,
3432 nla
[NFTA_SET_ELEM_KEY
]);
3436 if (d1
.type
!= NFT_DATA_VALUE
|| d1
.len
!= set
->klen
)
3439 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_KEY
, d1
.len
);
3441 nft_set_ext_add(&tmpl
, NFT_SET_EXT_EXPIRATION
);
3442 if (timeout
!= set
->timeout
)
3443 nft_set_ext_add(&tmpl
, NFT_SET_EXT_TIMEOUT
);
3446 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
) {
3447 err
= nft_data_init(ctx
, &data
, sizeof(data
), &d2
,
3448 nla
[NFTA_SET_ELEM_DATA
]);
3453 if (set
->dtype
!= NFT_DATA_VERDICT
&& d2
.len
!= set
->dlen
)
3456 dreg
= nft_type_to_reg(set
->dtype
);
3457 list_for_each_entry(binding
, &set
->bindings
, list
) {
3458 struct nft_ctx bind_ctx
= {
3460 .table
= ctx
->table
,
3461 .chain
= (struct nft_chain
*)binding
->chain
,
3464 if (!(binding
->flags
& NFT_SET_MAP
))
3467 err
= nft_validate_register_store(&bind_ctx
, dreg
,
3474 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_DATA
, d2
.len
);
3477 /* The full maximum length of userdata can exceed the maximum
3478 * offset value (U8_MAX) for following extensions, therefor it
3479 * must be the last extension added.
3482 if (nla
[NFTA_SET_ELEM_USERDATA
] != NULL
) {
3483 ulen
= nla_len(nla
[NFTA_SET_ELEM_USERDATA
]);
3485 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_USERDATA
,
3490 elem
.priv
= nft_set_elem_init(set
, &tmpl
, elem
.key
.val
.data
, data
.data
,
3491 timeout
, GFP_KERNEL
);
3492 if (elem
.priv
== NULL
)
3495 ext
= nft_set_elem_ext(set
, elem
.priv
);
3497 *nft_set_ext_flags(ext
) = flags
;
3499 udata
= nft_set_ext_userdata(ext
);
3500 udata
->len
= ulen
- 1;
3501 nla_memcpy(&udata
->data
, nla
[NFTA_SET_ELEM_USERDATA
], ulen
);
3504 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_NEWSETELEM
, set
);
3508 ext
->genmask
= nft_genmask_cur(ctx
->net
) | NFT_SET_ELEM_BUSY_MASK
;
3509 err
= set
->ops
->insert(set
, &elem
);
3513 nft_trans_elem(trans
) = elem
;
3514 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
3522 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
3523 nft_data_uninit(&data
, d2
.type
);
3525 nft_data_uninit(&elem
.key
.val
, d1
.type
);
3530 static int nf_tables_newsetelem(struct net
*net
, struct sock
*nlsk
,
3531 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3532 const struct nlattr
* const nla
[])
3534 const struct nlattr
*attr
;
3535 struct nft_set
*set
;
3539 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
)
3542 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
);
3546 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3548 if (nla
[NFTA_SET_ELEM_LIST_SET_ID
]) {
3549 set
= nf_tables_set_lookup_byid(net
,
3550 nla
[NFTA_SET_ELEM_LIST_SET_ID
]);
3553 return PTR_ERR(set
);
3556 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
3559 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
3561 !atomic_add_unless(&set
->nelems
, 1, set
->size
+ set
->ndeact
))
3564 err
= nft_add_set_elem(&ctx
, set
, attr
);
3566 atomic_dec(&set
->nelems
);
3573 static int nft_del_setelem(struct nft_ctx
*ctx
, struct nft_set
*set
,
3574 const struct nlattr
*attr
)
3576 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
3577 struct nft_data_desc desc
;
3578 struct nft_set_elem elem
;
3579 struct nft_trans
*trans
;
3582 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
3583 nft_set_elem_policy
);
3588 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
3591 err
= nft_data_init(ctx
, &elem
.key
.val
, sizeof(elem
.key
), &desc
,
3592 nla
[NFTA_SET_ELEM_KEY
]);
3597 if (desc
.type
!= NFT_DATA_VALUE
|| desc
.len
!= set
->klen
)
3600 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_DELSETELEM
, set
);
3601 if (trans
== NULL
) {
3606 elem
.priv
= set
->ops
->deactivate(set
, &elem
);
3607 if (elem
.priv
== NULL
) {
3612 nft_trans_elem(trans
) = elem
;
3613 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
3619 nft_data_uninit(&elem
.key
.val
, desc
.type
);
3624 static int nf_tables_delsetelem(struct net
*net
, struct sock
*nlsk
,
3625 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3626 const struct nlattr
* const nla
[])
3628 const struct nlattr
*attr
;
3629 struct nft_set
*set
;
3633 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
)
3636 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
);
3640 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3642 return PTR_ERR(set
);
3643 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
3646 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
3647 err
= nft_del_setelem(&ctx
, set
, attr
);
3656 void nft_set_gc_batch_release(struct rcu_head
*rcu
)
3658 struct nft_set_gc_batch
*gcb
;
3661 gcb
= container_of(rcu
, struct nft_set_gc_batch
, head
.rcu
);
3662 for (i
= 0; i
< gcb
->head
.cnt
; i
++)
3663 nft_set_elem_destroy(gcb
->head
.set
, gcb
->elems
[i
]);
3666 EXPORT_SYMBOL_GPL(nft_set_gc_batch_release
);
3668 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
3671 struct nft_set_gc_batch
*gcb
;
3673 gcb
= kzalloc(sizeof(*gcb
), gfp
);
3676 gcb
->head
.set
= set
;
3679 EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc
);
3681 static int nf_tables_fill_gen_info(struct sk_buff
*skb
, struct net
*net
,
3682 u32 portid
, u32 seq
)
3684 struct nlmsghdr
*nlh
;
3685 struct nfgenmsg
*nfmsg
;
3686 int event
= (NFNL_SUBSYS_NFTABLES
<< 8) | NFT_MSG_NEWGEN
;
3688 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), 0);
3690 goto nla_put_failure
;
3692 nfmsg
= nlmsg_data(nlh
);
3693 nfmsg
->nfgen_family
= AF_UNSPEC
;
3694 nfmsg
->version
= NFNETLINK_V0
;
3695 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
3697 if (nla_put_be32(skb
, NFTA_GEN_ID
, htonl(net
->nft
.base_seq
)))
3698 goto nla_put_failure
;
3700 nlmsg_end(skb
, nlh
);
3704 nlmsg_trim(skb
, nlh
);
3708 static int nf_tables_gen_notify(struct net
*net
, struct sk_buff
*skb
, int event
)
3710 struct nlmsghdr
*nlh
= nlmsg_hdr(skb
);
3711 struct sk_buff
*skb2
;
3714 if (nlmsg_report(nlh
) &&
3715 !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
3719 skb2
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
3723 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
3730 err
= nfnetlink_send(skb2
, net
, NETLINK_CB(skb
).portid
,
3731 NFNLGRP_NFTABLES
, nlmsg_report(nlh
), GFP_KERNEL
);
3734 nfnetlink_set_err(net
, NETLINK_CB(skb
).portid
, NFNLGRP_NFTABLES
,
3740 static int nf_tables_getgen(struct net
*net
, struct sock
*nlsk
,
3741 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3742 const struct nlattr
* const nla
[])
3744 struct sk_buff
*skb2
;
3747 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
3751 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
3756 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
3762 static const struct nfnl_callback nf_tables_cb
[NFT_MSG_MAX
] = {
3763 [NFT_MSG_NEWTABLE
] = {
3764 .call_batch
= nf_tables_newtable
,
3765 .attr_count
= NFTA_TABLE_MAX
,
3766 .policy
= nft_table_policy
,
3768 [NFT_MSG_GETTABLE
] = {
3769 .call
= nf_tables_gettable
,
3770 .attr_count
= NFTA_TABLE_MAX
,
3771 .policy
= nft_table_policy
,
3773 [NFT_MSG_DELTABLE
] = {
3774 .call_batch
= nf_tables_deltable
,
3775 .attr_count
= NFTA_TABLE_MAX
,
3776 .policy
= nft_table_policy
,
3778 [NFT_MSG_NEWCHAIN
] = {
3779 .call_batch
= nf_tables_newchain
,
3780 .attr_count
= NFTA_CHAIN_MAX
,
3781 .policy
= nft_chain_policy
,
3783 [NFT_MSG_GETCHAIN
] = {
3784 .call
= nf_tables_getchain
,
3785 .attr_count
= NFTA_CHAIN_MAX
,
3786 .policy
= nft_chain_policy
,
3788 [NFT_MSG_DELCHAIN
] = {
3789 .call_batch
= nf_tables_delchain
,
3790 .attr_count
= NFTA_CHAIN_MAX
,
3791 .policy
= nft_chain_policy
,
3793 [NFT_MSG_NEWRULE
] = {
3794 .call_batch
= nf_tables_newrule
,
3795 .attr_count
= NFTA_RULE_MAX
,
3796 .policy
= nft_rule_policy
,
3798 [NFT_MSG_GETRULE
] = {
3799 .call
= nf_tables_getrule
,
3800 .attr_count
= NFTA_RULE_MAX
,
3801 .policy
= nft_rule_policy
,
3803 [NFT_MSG_DELRULE
] = {
3804 .call_batch
= nf_tables_delrule
,
3805 .attr_count
= NFTA_RULE_MAX
,
3806 .policy
= nft_rule_policy
,
3808 [NFT_MSG_NEWSET
] = {
3809 .call_batch
= nf_tables_newset
,
3810 .attr_count
= NFTA_SET_MAX
,
3811 .policy
= nft_set_policy
,
3813 [NFT_MSG_GETSET
] = {
3814 .call
= nf_tables_getset
,
3815 .attr_count
= NFTA_SET_MAX
,
3816 .policy
= nft_set_policy
,
3818 [NFT_MSG_DELSET
] = {
3819 .call_batch
= nf_tables_delset
,
3820 .attr_count
= NFTA_SET_MAX
,
3821 .policy
= nft_set_policy
,
3823 [NFT_MSG_NEWSETELEM
] = {
3824 .call_batch
= nf_tables_newsetelem
,
3825 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
3826 .policy
= nft_set_elem_list_policy
,
3828 [NFT_MSG_GETSETELEM
] = {
3829 .call
= nf_tables_getsetelem
,
3830 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
3831 .policy
= nft_set_elem_list_policy
,
3833 [NFT_MSG_DELSETELEM
] = {
3834 .call_batch
= nf_tables_delsetelem
,
3835 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
3836 .policy
= nft_set_elem_list_policy
,
3838 [NFT_MSG_GETGEN
] = {
3839 .call
= nf_tables_getgen
,
3843 static void nft_chain_commit_update(struct nft_trans
*trans
)
3845 struct nft_base_chain
*basechain
;
3847 if (nft_trans_chain_name(trans
)[0])
3848 strcpy(trans
->ctx
.chain
->name
, nft_trans_chain_name(trans
));
3850 if (!(trans
->ctx
.chain
->flags
& NFT_BASE_CHAIN
))
3853 basechain
= nft_base_chain(trans
->ctx
.chain
);
3854 nft_chain_stats_replace(basechain
, nft_trans_chain_stats(trans
));
3856 switch (nft_trans_chain_policy(trans
)) {
3859 basechain
->policy
= nft_trans_chain_policy(trans
);
3864 static void nf_tables_commit_release(struct nft_trans
*trans
)
3866 switch (trans
->msg_type
) {
3867 case NFT_MSG_DELTABLE
:
3868 nf_tables_table_destroy(&trans
->ctx
);
3870 case NFT_MSG_DELCHAIN
:
3871 nf_tables_chain_destroy(trans
->ctx
.chain
);
3873 case NFT_MSG_DELRULE
:
3874 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
3876 case NFT_MSG_DELSET
:
3877 nft_set_destroy(nft_trans_set(trans
));
3879 case NFT_MSG_DELSETELEM
:
3880 nft_set_elem_destroy(nft_trans_elem_set(trans
),
3881 nft_trans_elem(trans
).priv
);
3887 static int nf_tables_commit(struct net
*net
, struct sk_buff
*skb
)
3889 struct nft_trans
*trans
, *next
;
3890 struct nft_trans_elem
*te
;
3892 /* Bump generation counter, invalidate any dump in progress */
3893 while (++net
->nft
.base_seq
== 0);
3895 /* A new generation has just started */
3896 net
->nft
.gencursor
= nft_gencursor_next(net
);
3898 /* Make sure all packets have left the previous generation before
3899 * purging old rules.
3903 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
3904 switch (trans
->msg_type
) {
3905 case NFT_MSG_NEWTABLE
:
3906 if (nft_trans_table_update(trans
)) {
3907 if (!nft_trans_table_enable(trans
)) {
3908 nf_tables_table_disable(trans
->ctx
.afi
,
3910 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
3913 trans
->ctx
.table
->flags
&= ~NFT_TABLE_INACTIVE
;
3915 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_NEWTABLE
);
3916 nft_trans_destroy(trans
);
3918 case NFT_MSG_DELTABLE
:
3919 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_DELTABLE
);
3921 case NFT_MSG_NEWCHAIN
:
3922 if (nft_trans_chain_update(trans
))
3923 nft_chain_commit_update(trans
);
3925 trans
->ctx
.chain
->flags
&= ~NFT_CHAIN_INACTIVE
;
3927 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_NEWCHAIN
);
3928 nft_trans_destroy(trans
);
3930 case NFT_MSG_DELCHAIN
:
3931 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_DELCHAIN
);
3932 nf_tables_unregister_hooks(trans
->ctx
.table
,
3934 trans
->ctx
.afi
->nops
);
3936 case NFT_MSG_NEWRULE
:
3937 nft_rule_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
3938 nf_tables_rule_notify(&trans
->ctx
,
3939 nft_trans_rule(trans
),
3941 nft_trans_destroy(trans
);
3943 case NFT_MSG_DELRULE
:
3944 list_del_rcu(&nft_trans_rule(trans
)->list
);
3945 nf_tables_rule_notify(&trans
->ctx
,
3946 nft_trans_rule(trans
),
3949 case NFT_MSG_NEWSET
:
3950 nft_trans_set(trans
)->flags
&= ~NFT_SET_INACTIVE
;
3951 /* This avoids hitting -EBUSY when deleting the table
3952 * from the transaction.
3954 if (nft_trans_set(trans
)->flags
& NFT_SET_ANONYMOUS
&&
3955 !list_empty(&nft_trans_set(trans
)->bindings
))
3956 trans
->ctx
.table
->use
--;
3958 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
3959 NFT_MSG_NEWSET
, GFP_KERNEL
);
3960 nft_trans_destroy(trans
);
3962 case NFT_MSG_DELSET
:
3963 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
3964 NFT_MSG_DELSET
, GFP_KERNEL
);
3966 case NFT_MSG_NEWSETELEM
:
3967 te
= (struct nft_trans_elem
*)trans
->data
;
3969 te
->set
->ops
->activate(te
->set
, &te
->elem
);
3970 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
3972 NFT_MSG_NEWSETELEM
, 0);
3973 nft_trans_destroy(trans
);
3975 case NFT_MSG_DELSETELEM
:
3976 te
= (struct nft_trans_elem
*)trans
->data
;
3978 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
3980 NFT_MSG_DELSETELEM
, 0);
3981 te
->set
->ops
->remove(te
->set
, &te
->elem
);
3982 atomic_dec(&te
->set
->nelems
);
3990 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
3991 list_del(&trans
->list
);
3992 nf_tables_commit_release(trans
);
3995 nf_tables_gen_notify(net
, skb
, NFT_MSG_NEWGEN
);
4000 static void nf_tables_abort_release(struct nft_trans
*trans
)
4002 switch (trans
->msg_type
) {
4003 case NFT_MSG_NEWTABLE
:
4004 nf_tables_table_destroy(&trans
->ctx
);
4006 case NFT_MSG_NEWCHAIN
:
4007 nf_tables_chain_destroy(trans
->ctx
.chain
);
4009 case NFT_MSG_NEWRULE
:
4010 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
4012 case NFT_MSG_NEWSET
:
4013 nft_set_destroy(nft_trans_set(trans
));
4015 case NFT_MSG_NEWSETELEM
:
4016 nft_set_elem_destroy(nft_trans_elem_set(trans
),
4017 nft_trans_elem(trans
).priv
);
4023 static int nf_tables_abort(struct net
*net
, struct sk_buff
*skb
)
4025 struct nft_trans
*trans
, *next
;
4026 struct nft_trans_elem
*te
;
4028 list_for_each_entry_safe_reverse(trans
, next
, &net
->nft
.commit_list
,
4030 switch (trans
->msg_type
) {
4031 case NFT_MSG_NEWTABLE
:
4032 if (nft_trans_table_update(trans
)) {
4033 if (nft_trans_table_enable(trans
)) {
4034 nf_tables_table_disable(trans
->ctx
.afi
,
4036 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
4038 nft_trans_destroy(trans
);
4040 list_del_rcu(&trans
->ctx
.table
->list
);
4043 case NFT_MSG_DELTABLE
:
4044 list_add_tail_rcu(&trans
->ctx
.table
->list
,
4045 &trans
->ctx
.afi
->tables
);
4046 nft_trans_destroy(trans
);
4048 case NFT_MSG_NEWCHAIN
:
4049 if (nft_trans_chain_update(trans
)) {
4050 free_percpu(nft_trans_chain_stats(trans
));
4052 nft_trans_destroy(trans
);
4054 trans
->ctx
.table
->use
--;
4055 list_del_rcu(&trans
->ctx
.chain
->list
);
4056 nf_tables_unregister_hooks(trans
->ctx
.table
,
4058 trans
->ctx
.afi
->nops
);
4061 case NFT_MSG_DELCHAIN
:
4062 trans
->ctx
.table
->use
++;
4063 list_add_tail_rcu(&trans
->ctx
.chain
->list
,
4064 &trans
->ctx
.table
->chains
);
4065 nft_trans_destroy(trans
);
4067 case NFT_MSG_NEWRULE
:
4068 trans
->ctx
.chain
->use
--;
4069 list_del_rcu(&nft_trans_rule(trans
)->list
);
4071 case NFT_MSG_DELRULE
:
4072 trans
->ctx
.chain
->use
++;
4073 nft_rule_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
4074 nft_trans_destroy(trans
);
4076 case NFT_MSG_NEWSET
:
4077 trans
->ctx
.table
->use
--;
4078 list_del_rcu(&nft_trans_set(trans
)->list
);
4080 case NFT_MSG_DELSET
:
4081 trans
->ctx
.table
->use
++;
4082 list_add_tail_rcu(&nft_trans_set(trans
)->list
,
4083 &trans
->ctx
.table
->sets
);
4084 nft_trans_destroy(trans
);
4086 case NFT_MSG_NEWSETELEM
:
4087 te
= (struct nft_trans_elem
*)trans
->data
;
4089 te
->set
->ops
->remove(te
->set
, &te
->elem
);
4090 atomic_dec(&te
->set
->nelems
);
4092 case NFT_MSG_DELSETELEM
:
4093 te
= (struct nft_trans_elem
*)trans
->data
;
4095 te
->set
->ops
->activate(te
->set
, &te
->elem
);
4098 nft_trans_destroy(trans
);
4105 list_for_each_entry_safe_reverse(trans
, next
,
4106 &net
->nft
.commit_list
, list
) {
4107 list_del(&trans
->list
);
4108 nf_tables_abort_release(trans
);
4114 static const struct nfnetlink_subsystem nf_tables_subsys
= {
4115 .name
= "nf_tables",
4116 .subsys_id
= NFNL_SUBSYS_NFTABLES
,
4117 .cb_count
= NFT_MSG_MAX
,
4119 .commit
= nf_tables_commit
,
4120 .abort
= nf_tables_abort
,
4123 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
4124 enum nft_chain_type type
)
4126 const struct nft_base_chain
*basechain
;
4128 if (chain
->flags
& NFT_BASE_CHAIN
) {
4129 basechain
= nft_base_chain(chain
);
4130 if (basechain
->type
->type
!= type
)
4135 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency
);
4137 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
4138 unsigned int hook_flags
)
4140 struct nft_base_chain
*basechain
;
4142 if (chain
->flags
& NFT_BASE_CHAIN
) {
4143 basechain
= nft_base_chain(chain
);
4145 if ((1 << basechain
->ops
[0].hooknum
) & hook_flags
)
4153 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks
);
4156 * Loop detection - walk through the ruleset beginning at the destination chain
4157 * of a new jump until either the source chain is reached (loop) or all
4158 * reachable chains have been traversed.
4160 * The loop check is performed whenever a new jump verdict is added to an
4161 * expression or verdict map or a verdict map is bound to a new chain.
4164 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
4165 const struct nft_chain
*chain
);
4167 static int nf_tables_loop_check_setelem(const struct nft_ctx
*ctx
,
4168 const struct nft_set
*set
,
4169 const struct nft_set_iter
*iter
,
4170 const struct nft_set_elem
*elem
)
4172 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
4173 const struct nft_data
*data
;
4175 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
4176 *nft_set_ext_flags(ext
) & NFT_SET_ELEM_INTERVAL_END
)
4179 data
= nft_set_ext_data(ext
);
4180 switch (data
->verdict
.code
) {
4183 return nf_tables_check_loops(ctx
, data
->verdict
.chain
);
4189 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
4190 const struct nft_chain
*chain
)
4192 const struct nft_rule
*rule
;
4193 const struct nft_expr
*expr
, *last
;
4194 const struct nft_set
*set
;
4195 struct nft_set_binding
*binding
;
4196 struct nft_set_iter iter
;
4198 if (ctx
->chain
== chain
)
4201 list_for_each_entry(rule
, &chain
->rules
, list
) {
4202 nft_rule_for_each_expr(expr
, last
, rule
) {
4203 const struct nft_data
*data
= NULL
;
4206 if (!expr
->ops
->validate
)
4209 err
= expr
->ops
->validate(ctx
, expr
, &data
);
4216 switch (data
->verdict
.code
) {
4219 err
= nf_tables_check_loops(ctx
,
4220 data
->verdict
.chain
);
4229 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
4230 if (!(set
->flags
& NFT_SET_MAP
) ||
4231 set
->dtype
!= NFT_DATA_VERDICT
)
4234 list_for_each_entry(binding
, &set
->bindings
, list
) {
4235 if (!(binding
->flags
& NFT_SET_MAP
) ||
4236 binding
->chain
!= chain
)
4242 iter
.fn
= nf_tables_loop_check_setelem
;
4244 set
->ops
->walk(ctx
, set
, &iter
);
4254 * nft_parse_register - parse a register value from a netlink attribute
4256 * @attr: netlink attribute
4258 * Parse and translate a register value from a netlink attribute.
4259 * Registers used to be 128 bit wide, these register numbers will be
4260 * mapped to the corresponding 32 bit register numbers.
4262 unsigned int nft_parse_register(const struct nlattr
*attr
)
4266 reg
= ntohl(nla_get_be32(attr
));
4268 case NFT_REG_VERDICT
...NFT_REG_4
:
4269 return reg
* NFT_REG_SIZE
/ NFT_REG32_SIZE
;
4271 return reg
+ NFT_REG_SIZE
/ NFT_REG32_SIZE
- NFT_REG32_00
;
4274 EXPORT_SYMBOL_GPL(nft_parse_register
);
4277 * nft_dump_register - dump a register value to a netlink attribute
4279 * @skb: socket buffer
4280 * @attr: attribute number
4281 * @reg: register number
4283 * Construct a netlink attribute containing the register number. For
4284 * compatibility reasons, register numbers being a multiple of 4 are
4285 * translated to the corresponding 128 bit register numbers.
4287 int nft_dump_register(struct sk_buff
*skb
, unsigned int attr
, unsigned int reg
)
4289 if (reg
% (NFT_REG_SIZE
/ NFT_REG32_SIZE
) == 0)
4290 reg
= reg
/ (NFT_REG_SIZE
/ NFT_REG32_SIZE
);
4292 reg
= reg
- NFT_REG_SIZE
/ NFT_REG32_SIZE
+ NFT_REG32_00
;
4294 return nla_put_be32(skb
, attr
, htonl(reg
));
4296 EXPORT_SYMBOL_GPL(nft_dump_register
);
4299 * nft_validate_register_load - validate a load from a register
4301 * @reg: the register number
4302 * @len: the length of the data
4304 * Validate that the input register is one of the general purpose
4305 * registers and that the length of the load is within the bounds.
4307 int nft_validate_register_load(enum nft_registers reg
, unsigned int len
)
4309 if (reg
< NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
)
4313 if (reg
* NFT_REG32_SIZE
+ len
> FIELD_SIZEOF(struct nft_regs
, data
))
4318 EXPORT_SYMBOL_GPL(nft_validate_register_load
);
4321 * nft_validate_register_store - validate an expressions' register store
4323 * @ctx: context of the expression performing the load
4324 * @reg: the destination register number
4325 * @data: the data to load
4326 * @type: the data type
4327 * @len: the length of the data
4329 * Validate that a data load uses the appropriate data type for
4330 * the destination register and the length is within the bounds.
4331 * A value of NULL for the data means that its runtime gathered
4334 int nft_validate_register_store(const struct nft_ctx
*ctx
,
4335 enum nft_registers reg
,
4336 const struct nft_data
*data
,
4337 enum nft_data_types type
, unsigned int len
)
4342 case NFT_REG_VERDICT
:
4343 if (type
!= NFT_DATA_VERDICT
)
4347 (data
->verdict
.code
== NFT_GOTO
||
4348 data
->verdict
.code
== NFT_JUMP
)) {
4349 err
= nf_tables_check_loops(ctx
, data
->verdict
.chain
);
4353 if (ctx
->chain
->level
+ 1 >
4354 data
->verdict
.chain
->level
) {
4355 if (ctx
->chain
->level
+ 1 == NFT_JUMP_STACK_SIZE
)
4357 data
->verdict
.chain
->level
= ctx
->chain
->level
+ 1;
4363 if (reg
< NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
)
4367 if (reg
* NFT_REG32_SIZE
+ len
>
4368 FIELD_SIZEOF(struct nft_regs
, data
))
4371 if (data
!= NULL
&& type
!= NFT_DATA_VALUE
)
4376 EXPORT_SYMBOL_GPL(nft_validate_register_store
);
4378 static const struct nla_policy nft_verdict_policy
[NFTA_VERDICT_MAX
+ 1] = {
4379 [NFTA_VERDICT_CODE
] = { .type
= NLA_U32
},
4380 [NFTA_VERDICT_CHAIN
] = { .type
= NLA_STRING
,
4381 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
4384 static int nft_verdict_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
4385 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
4387 struct nlattr
*tb
[NFTA_VERDICT_MAX
+ 1];
4388 struct nft_chain
*chain
;
4391 err
= nla_parse_nested(tb
, NFTA_VERDICT_MAX
, nla
, nft_verdict_policy
);
4395 if (!tb
[NFTA_VERDICT_CODE
])
4397 data
->verdict
.code
= ntohl(nla_get_be32(tb
[NFTA_VERDICT_CODE
]));
4399 switch (data
->verdict
.code
) {
4401 switch (data
->verdict
.code
& NF_VERDICT_MASK
) {
4416 if (!tb
[NFTA_VERDICT_CHAIN
])
4418 chain
= nf_tables_chain_lookup(ctx
->table
,
4419 tb
[NFTA_VERDICT_CHAIN
]);
4421 return PTR_ERR(chain
);
4422 if (chain
->flags
& NFT_BASE_CHAIN
)
4426 data
->verdict
.chain
= chain
;
4430 desc
->len
= sizeof(data
->verdict
);
4431 desc
->type
= NFT_DATA_VERDICT
;
4435 static void nft_verdict_uninit(const struct nft_data
*data
)
4437 switch (data
->verdict
.code
) {
4440 data
->verdict
.chain
->use
--;
4445 int nft_verdict_dump(struct sk_buff
*skb
, int type
, const struct nft_verdict
*v
)
4447 struct nlattr
*nest
;
4449 nest
= nla_nest_start(skb
, type
);
4451 goto nla_put_failure
;
4453 if (nla_put_be32(skb
, NFTA_VERDICT_CODE
, htonl(v
->code
)))
4454 goto nla_put_failure
;
4459 if (nla_put_string(skb
, NFTA_VERDICT_CHAIN
,
4461 goto nla_put_failure
;
4463 nla_nest_end(skb
, nest
);
4470 static int nft_value_init(const struct nft_ctx
*ctx
,
4471 struct nft_data
*data
, unsigned int size
,
4472 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
4482 nla_memcpy(data
->data
, nla
, len
);
4483 desc
->type
= NFT_DATA_VALUE
;
4488 static int nft_value_dump(struct sk_buff
*skb
, const struct nft_data
*data
,
4491 return nla_put(skb
, NFTA_DATA_VALUE
, len
, data
->data
);
4494 static const struct nla_policy nft_data_policy
[NFTA_DATA_MAX
+ 1] = {
4495 [NFTA_DATA_VALUE
] = { .type
= NLA_BINARY
},
4496 [NFTA_DATA_VERDICT
] = { .type
= NLA_NESTED
},
4500 * nft_data_init - parse nf_tables data netlink attributes
4502 * @ctx: context of the expression using the data
4503 * @data: destination struct nft_data
4504 * @size: maximum data length
4505 * @desc: data description
4506 * @nla: netlink attribute containing data
4508 * Parse the netlink data attributes and initialize a struct nft_data.
4509 * The type and length of data are returned in the data description.
4511 * The caller can indicate that it only wants to accept data of type
4512 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4514 int nft_data_init(const struct nft_ctx
*ctx
,
4515 struct nft_data
*data
, unsigned int size
,
4516 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
4518 struct nlattr
*tb
[NFTA_DATA_MAX
+ 1];
4521 err
= nla_parse_nested(tb
, NFTA_DATA_MAX
, nla
, nft_data_policy
);
4525 if (tb
[NFTA_DATA_VALUE
])
4526 return nft_value_init(ctx
, data
, size
, desc
,
4527 tb
[NFTA_DATA_VALUE
]);
4528 if (tb
[NFTA_DATA_VERDICT
] && ctx
!= NULL
)
4529 return nft_verdict_init(ctx
, data
, desc
, tb
[NFTA_DATA_VERDICT
]);
4532 EXPORT_SYMBOL_GPL(nft_data_init
);
4535 * nft_data_uninit - release a nft_data item
4537 * @data: struct nft_data to release
4538 * @type: type of data
4540 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4541 * all others need to be released by calling this function.
4543 void nft_data_uninit(const struct nft_data
*data
, enum nft_data_types type
)
4545 if (type
< NFT_DATA_VERDICT
)
4548 case NFT_DATA_VERDICT
:
4549 return nft_verdict_uninit(data
);
4554 EXPORT_SYMBOL_GPL(nft_data_uninit
);
4556 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
4557 enum nft_data_types type
, unsigned int len
)
4559 struct nlattr
*nest
;
4562 nest
= nla_nest_start(skb
, attr
);
4567 case NFT_DATA_VALUE
:
4568 err
= nft_value_dump(skb
, data
, len
);
4570 case NFT_DATA_VERDICT
:
4571 err
= nft_verdict_dump(skb
, NFTA_DATA_VERDICT
, &data
->verdict
);
4578 nla_nest_end(skb
, nest
);
4581 EXPORT_SYMBOL_GPL(nft_data_dump
);
4583 static int __net_init
nf_tables_init_net(struct net
*net
)
4585 INIT_LIST_HEAD(&net
->nft
.af_info
);
4586 INIT_LIST_HEAD(&net
->nft
.commit_list
);
4587 net
->nft
.base_seq
= 1;
4591 int __nft_release_basechain(struct nft_ctx
*ctx
)
4593 struct nft_rule
*rule
, *nr
;
4595 BUG_ON(!(ctx
->chain
->flags
& NFT_BASE_CHAIN
));
4597 nf_tables_unregister_hooks(ctx
->chain
->table
, ctx
->chain
,
4599 list_for_each_entry_safe(rule
, nr
, &ctx
->chain
->rules
, list
) {
4600 list_del(&rule
->list
);
4602 nf_tables_rule_destroy(ctx
, rule
);
4604 list_del(&ctx
->chain
->list
);
4606 nf_tables_chain_destroy(ctx
->chain
);
4610 EXPORT_SYMBOL_GPL(__nft_release_basechain
);
4612 /* Called by nft_unregister_afinfo() from __net_exit path, nfnl_lock is held. */
4613 static void __nft_release_afinfo(struct net
*net
, struct nft_af_info
*afi
)
4615 struct nft_table
*table
, *nt
;
4616 struct nft_chain
*chain
, *nc
;
4617 struct nft_rule
*rule
, *nr
;
4618 struct nft_set
*set
, *ns
;
4619 struct nft_ctx ctx
= {
4624 list_for_each_entry_safe(table
, nt
, &afi
->tables
, list
) {
4625 list_for_each_entry(chain
, &table
->chains
, list
)
4626 nf_tables_unregister_hooks(table
, chain
, afi
->nops
);
4627 /* No packets are walking on these chains anymore. */
4629 list_for_each_entry(chain
, &table
->chains
, list
) {
4631 list_for_each_entry_safe(rule
, nr
, &chain
->rules
, list
) {
4632 list_del(&rule
->list
);
4634 nf_tables_rule_destroy(&ctx
, rule
);
4637 list_for_each_entry_safe(set
, ns
, &table
->sets
, list
) {
4638 list_del(&set
->list
);
4640 nft_set_destroy(set
);
4642 list_for_each_entry_safe(chain
, nc
, &table
->chains
, list
) {
4643 list_del(&chain
->list
);
4645 nf_tables_chain_destroy(chain
);
4647 list_del(&table
->list
);
4648 nf_tables_table_destroy(&ctx
);
4652 static struct pernet_operations nf_tables_net_ops
= {
4653 .init
= nf_tables_init_net
,
4656 static int __init
nf_tables_module_init(void)
4660 info
= kmalloc(sizeof(struct nft_expr_info
) * NFT_RULE_MAXEXPRS
,
4667 err
= nf_tables_core_module_init();
4671 err
= nfnetlink_subsys_register(&nf_tables_subsys
);
4675 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
4676 return register_pernet_subsys(&nf_tables_net_ops
);
4678 nf_tables_core_module_exit();
4685 static void __exit
nf_tables_module_exit(void)
4687 unregister_pernet_subsys(&nf_tables_net_ops
);
4688 nfnetlink_subsys_unregister(&nf_tables_subsys
);
4690 nf_tables_core_module_exit();
4694 module_init(nf_tables_module_init
);
4695 module_exit(nf_tables_module_exit
);
4697 MODULE_LICENSE("GPL");
4698 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4699 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES
);