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
),
949 nla_put_be64(skb
, NFTA_COUNTER_BYTES
, cpu_to_be64(total
.bytes
),
951 goto nla_put_failure
;
953 nla_nest_end(skb
, nest
);
960 static int nf_tables_fill_chain_info(struct sk_buff
*skb
, struct net
*net
,
961 u32 portid
, u32 seq
, int event
, u32 flags
,
962 int family
, const struct nft_table
*table
,
963 const struct nft_chain
*chain
)
965 struct nlmsghdr
*nlh
;
966 struct nfgenmsg
*nfmsg
;
968 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
969 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
971 goto nla_put_failure
;
973 nfmsg
= nlmsg_data(nlh
);
974 nfmsg
->nfgen_family
= family
;
975 nfmsg
->version
= NFNETLINK_V0
;
976 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
978 if (nla_put_string(skb
, NFTA_CHAIN_TABLE
, table
->name
))
979 goto nla_put_failure
;
980 if (nla_put_be64(skb
, NFTA_CHAIN_HANDLE
, cpu_to_be64(chain
->handle
),
982 goto nla_put_failure
;
983 if (nla_put_string(skb
, NFTA_CHAIN_NAME
, chain
->name
))
984 goto nla_put_failure
;
986 if (chain
->flags
& NFT_BASE_CHAIN
) {
987 const struct nft_base_chain
*basechain
= nft_base_chain(chain
);
988 const struct nf_hook_ops
*ops
= &basechain
->ops
[0];
991 nest
= nla_nest_start(skb
, NFTA_CHAIN_HOOK
);
993 goto nla_put_failure
;
994 if (nla_put_be32(skb
, NFTA_HOOK_HOOKNUM
, htonl(ops
->hooknum
)))
995 goto nla_put_failure
;
996 if (nla_put_be32(skb
, NFTA_HOOK_PRIORITY
, htonl(ops
->priority
)))
997 goto nla_put_failure
;
998 if (basechain
->dev_name
[0] &&
999 nla_put_string(skb
, NFTA_HOOK_DEV
, basechain
->dev_name
))
1000 goto nla_put_failure
;
1001 nla_nest_end(skb
, nest
);
1003 if (nla_put_be32(skb
, NFTA_CHAIN_POLICY
,
1004 htonl(basechain
->policy
)))
1005 goto nla_put_failure
;
1007 if (nla_put_string(skb
, NFTA_CHAIN_TYPE
, basechain
->type
->name
))
1008 goto nla_put_failure
;
1010 if (nft_dump_stats(skb
, nft_base_chain(chain
)->stats
))
1011 goto nla_put_failure
;
1014 if (nla_put_be32(skb
, NFTA_CHAIN_USE
, htonl(chain
->use
)))
1015 goto nla_put_failure
;
1017 nlmsg_end(skb
, nlh
);
1021 nlmsg_trim(skb
, nlh
);
1025 static int nf_tables_chain_notify(const struct nft_ctx
*ctx
, int event
)
1027 struct sk_buff
*skb
;
1031 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
1035 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1039 err
= nf_tables_fill_chain_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
1040 event
, 0, ctx
->afi
->family
, ctx
->table
,
1047 err
= nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1048 ctx
->report
, GFP_KERNEL
);
1051 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1057 static int nf_tables_dump_chains(struct sk_buff
*skb
,
1058 struct netlink_callback
*cb
)
1060 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1061 const struct nft_af_info
*afi
;
1062 const struct nft_table
*table
;
1063 const struct nft_chain
*chain
;
1064 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1065 struct net
*net
= sock_net(skb
->sk
);
1066 int family
= nfmsg
->nfgen_family
;
1069 cb
->seq
= net
->nft
.base_seq
;
1071 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
1072 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
1075 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
1076 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
1080 memset(&cb
->args
[1], 0,
1081 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1082 if (nf_tables_fill_chain_info(skb
, net
,
1083 NETLINK_CB(cb
->skb
).portid
,
1087 afi
->family
, table
, chain
) < 0)
1090 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1102 static int nf_tables_getchain(struct net
*net
, struct sock
*nlsk
,
1103 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1104 const struct nlattr
* const nla
[])
1106 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1107 const struct nft_af_info
*afi
;
1108 const struct nft_table
*table
;
1109 const struct nft_chain
*chain
;
1110 struct sk_buff
*skb2
;
1111 int family
= nfmsg
->nfgen_family
;
1114 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1115 struct netlink_dump_control c
= {
1116 .dump
= nf_tables_dump_chains
,
1118 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
1121 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1123 return PTR_ERR(afi
);
1125 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
1127 return PTR_ERR(table
);
1128 if (table
->flags
& NFT_TABLE_INACTIVE
)
1131 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
]);
1133 return PTR_ERR(chain
);
1134 if (chain
->flags
& NFT_CHAIN_INACTIVE
)
1137 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1141 err
= nf_tables_fill_chain_info(skb2
, net
, NETLINK_CB(skb
).portid
,
1142 nlh
->nlmsg_seq
, NFT_MSG_NEWCHAIN
, 0,
1143 family
, table
, chain
);
1147 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
1154 static const struct nla_policy nft_counter_policy
[NFTA_COUNTER_MAX
+ 1] = {
1155 [NFTA_COUNTER_PACKETS
] = { .type
= NLA_U64
},
1156 [NFTA_COUNTER_BYTES
] = { .type
= NLA_U64
},
1159 static struct nft_stats __percpu
*nft_stats_alloc(const struct nlattr
*attr
)
1161 struct nlattr
*tb
[NFTA_COUNTER_MAX
+1];
1162 struct nft_stats __percpu
*newstats
;
1163 struct nft_stats
*stats
;
1166 err
= nla_parse_nested(tb
, NFTA_COUNTER_MAX
, attr
, nft_counter_policy
);
1168 return ERR_PTR(err
);
1170 if (!tb
[NFTA_COUNTER_BYTES
] || !tb
[NFTA_COUNTER_PACKETS
])
1171 return ERR_PTR(-EINVAL
);
1173 newstats
= netdev_alloc_pcpu_stats(struct nft_stats
);
1174 if (newstats
== NULL
)
1175 return ERR_PTR(-ENOMEM
);
1177 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1178 * are not exposed to userspace.
1181 stats
= this_cpu_ptr(newstats
);
1182 stats
->bytes
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_BYTES
]));
1183 stats
->pkts
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_PACKETS
]));
1189 static void nft_chain_stats_replace(struct nft_base_chain
*chain
,
1190 struct nft_stats __percpu
*newstats
)
1192 if (newstats
== NULL
)
1196 struct nft_stats __percpu
*oldstats
=
1197 nft_dereference(chain
->stats
);
1199 rcu_assign_pointer(chain
->stats
, newstats
);
1201 free_percpu(oldstats
);
1203 rcu_assign_pointer(chain
->stats
, newstats
);
1206 static void nf_tables_chain_destroy(struct nft_chain
*chain
)
1208 BUG_ON(chain
->use
> 0);
1210 if (chain
->flags
& NFT_BASE_CHAIN
) {
1211 struct nft_base_chain
*basechain
= nft_base_chain(chain
);
1213 module_put(basechain
->type
->owner
);
1214 free_percpu(basechain
->stats
);
1215 if (basechain
->ops
[0].dev
!= NULL
)
1216 dev_put(basechain
->ops
[0].dev
);
1223 static int nf_tables_newchain(struct net
*net
, struct sock
*nlsk
,
1224 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1225 const struct nlattr
* const nla
[])
1227 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1228 const struct nlattr
* uninitialized_var(name
);
1229 struct nft_af_info
*afi
;
1230 struct nft_table
*table
;
1231 struct nft_chain
*chain
;
1232 struct nft_base_chain
*basechain
= NULL
;
1233 struct nlattr
*ha
[NFTA_HOOK_MAX
+ 1];
1234 int family
= nfmsg
->nfgen_family
;
1235 struct net_device
*dev
= NULL
;
1236 u8 policy
= NF_ACCEPT
;
1239 struct nft_stats __percpu
*stats
;
1244 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
1246 afi
= nf_tables_afinfo_lookup(net
, family
, true);
1248 return PTR_ERR(afi
);
1250 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
1252 return PTR_ERR(table
);
1255 name
= nla
[NFTA_CHAIN_NAME
];
1257 if (nla
[NFTA_CHAIN_HANDLE
]) {
1258 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_CHAIN_HANDLE
]));
1259 chain
= nf_tables_chain_lookup_byhandle(table
, handle
);
1261 return PTR_ERR(chain
);
1263 chain
= nf_tables_chain_lookup(table
, name
);
1264 if (IS_ERR(chain
)) {
1265 if (PTR_ERR(chain
) != -ENOENT
)
1266 return PTR_ERR(chain
);
1271 if (nla
[NFTA_CHAIN_POLICY
]) {
1272 if ((chain
!= NULL
&&
1273 !(chain
->flags
& NFT_BASE_CHAIN
)))
1276 if (chain
== NULL
&&
1277 nla
[NFTA_CHAIN_HOOK
] == NULL
)
1280 policy
= ntohl(nla_get_be32(nla
[NFTA_CHAIN_POLICY
]));
1290 if (chain
!= NULL
) {
1291 struct nft_stats
*stats
= NULL
;
1292 struct nft_trans
*trans
;
1294 if (chain
->flags
& NFT_CHAIN_INACTIVE
)
1296 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
1298 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1301 if (nla
[NFTA_CHAIN_HANDLE
] && name
&&
1302 !IS_ERR(nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
])))
1305 if (nla
[NFTA_CHAIN_COUNTERS
]) {
1306 if (!(chain
->flags
& NFT_BASE_CHAIN
))
1309 stats
= nft_stats_alloc(nla
[NFTA_CHAIN_COUNTERS
]);
1311 return PTR_ERR(stats
);
1314 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
1315 trans
= nft_trans_alloc(&ctx
, NFT_MSG_NEWCHAIN
,
1316 sizeof(struct nft_trans_chain
));
1317 if (trans
== NULL
) {
1322 nft_trans_chain_stats(trans
) = stats
;
1323 nft_trans_chain_update(trans
) = true;
1325 if (nla
[NFTA_CHAIN_POLICY
])
1326 nft_trans_chain_policy(trans
) = policy
;
1328 nft_trans_chain_policy(trans
) = -1;
1330 if (nla
[NFTA_CHAIN_HANDLE
] && name
) {
1331 nla_strlcpy(nft_trans_chain_name(trans
), name
,
1332 NFT_CHAIN_MAXNAMELEN
);
1334 list_add_tail(&trans
->list
, &net
->nft
.commit_list
);
1338 if (table
->use
== UINT_MAX
)
1341 if (nla
[NFTA_CHAIN_HOOK
]) {
1342 const struct nf_chain_type
*type
;
1343 struct nf_hook_ops
*ops
;
1345 u32 hooknum
, priority
;
1347 type
= chain_type
[family
][NFT_CHAIN_T_DEFAULT
];
1348 if (nla
[NFTA_CHAIN_TYPE
]) {
1349 type
= nf_tables_chain_type_lookup(afi
,
1350 nla
[NFTA_CHAIN_TYPE
],
1353 return PTR_ERR(type
);
1356 err
= nla_parse_nested(ha
, NFTA_HOOK_MAX
, nla
[NFTA_CHAIN_HOOK
],
1360 if (ha
[NFTA_HOOK_HOOKNUM
] == NULL
||
1361 ha
[NFTA_HOOK_PRIORITY
] == NULL
)
1364 hooknum
= ntohl(nla_get_be32(ha
[NFTA_HOOK_HOOKNUM
]));
1365 if (hooknum
>= afi
->nhooks
)
1367 priority
= ntohl(nla_get_be32(ha
[NFTA_HOOK_PRIORITY
]));
1369 if (!(type
->hook_mask
& (1 << hooknum
)))
1371 if (!try_module_get(type
->owner
))
1373 hookfn
= type
->hooks
[hooknum
];
1375 if (afi
->flags
& NFT_AF_NEEDS_DEV
) {
1376 char ifname
[IFNAMSIZ
];
1378 if (!ha
[NFTA_HOOK_DEV
]) {
1379 module_put(type
->owner
);
1383 nla_strlcpy(ifname
, ha
[NFTA_HOOK_DEV
], IFNAMSIZ
);
1384 dev
= dev_get_by_name(net
, ifname
);
1386 module_put(type
->owner
);
1389 } else if (ha
[NFTA_HOOK_DEV
]) {
1390 module_put(type
->owner
);
1394 basechain
= kzalloc(sizeof(*basechain
), GFP_KERNEL
);
1395 if (basechain
== NULL
) {
1396 module_put(type
->owner
);
1403 strncpy(basechain
->dev_name
, dev
->name
, IFNAMSIZ
);
1405 if (nla
[NFTA_CHAIN_COUNTERS
]) {
1406 stats
= nft_stats_alloc(nla
[NFTA_CHAIN_COUNTERS
]);
1407 if (IS_ERR(stats
)) {
1408 module_put(type
->owner
);
1412 return PTR_ERR(stats
);
1414 basechain
->stats
= stats
;
1416 stats
= netdev_alloc_pcpu_stats(struct nft_stats
);
1417 if (stats
== NULL
) {
1418 module_put(type
->owner
);
1424 rcu_assign_pointer(basechain
->stats
, stats
);
1427 write_pnet(&basechain
->pnet
, net
);
1428 basechain
->type
= type
;
1429 chain
= &basechain
->chain
;
1431 for (i
= 0; i
< afi
->nops
; i
++) {
1432 ops
= &basechain
->ops
[i
];
1434 ops
->hooknum
= hooknum
;
1435 ops
->priority
= priority
;
1437 ops
->hook
= afi
->hooks
[ops
->hooknum
];
1441 if (afi
->hook_ops_init
)
1442 afi
->hook_ops_init(ops
, i
);
1445 chain
->flags
|= NFT_BASE_CHAIN
;
1446 basechain
->policy
= policy
;
1448 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
1453 INIT_LIST_HEAD(&chain
->rules
);
1454 chain
->handle
= nf_tables_alloc_handle(table
);
1455 chain
->table
= table
;
1456 nla_strlcpy(chain
->name
, name
, NFT_CHAIN_MAXNAMELEN
);
1458 err
= nf_tables_register_hooks(table
, chain
, afi
->nops
);
1462 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
1463 err
= nft_trans_chain_add(&ctx
, NFT_MSG_NEWCHAIN
);
1468 list_add_tail_rcu(&chain
->list
, &table
->chains
);
1471 nf_tables_unregister_hooks(table
, chain
, afi
->nops
);
1473 nf_tables_chain_destroy(chain
);
1477 static int nf_tables_delchain(struct net
*net
, struct sock
*nlsk
,
1478 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1479 const struct nlattr
* const nla
[])
1481 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1482 struct nft_af_info
*afi
;
1483 struct nft_table
*table
;
1484 struct nft_chain
*chain
;
1485 int family
= nfmsg
->nfgen_family
;
1488 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1490 return PTR_ERR(afi
);
1492 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
1494 return PTR_ERR(table
);
1496 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
]);
1498 return PTR_ERR(chain
);
1502 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
1504 return nft_delchain(&ctx
);
1512 * nft_register_expr - register nf_tables expr type
1515 * Registers the expr type for use with nf_tables. Returns zero on
1516 * success or a negative errno code otherwise.
1518 int nft_register_expr(struct nft_expr_type
*type
)
1520 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1521 if (type
->family
== NFPROTO_UNSPEC
)
1522 list_add_tail_rcu(&type
->list
, &nf_tables_expressions
);
1524 list_add_rcu(&type
->list
, &nf_tables_expressions
);
1525 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1528 EXPORT_SYMBOL_GPL(nft_register_expr
);
1531 * nft_unregister_expr - unregister nf_tables expr type
1534 * Unregisters the expr typefor use with nf_tables.
1536 void nft_unregister_expr(struct nft_expr_type
*type
)
1538 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1539 list_del_rcu(&type
->list
);
1540 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1542 EXPORT_SYMBOL_GPL(nft_unregister_expr
);
1544 static const struct nft_expr_type
*__nft_expr_type_get(u8 family
,
1547 const struct nft_expr_type
*type
;
1549 list_for_each_entry(type
, &nf_tables_expressions
, list
) {
1550 if (!nla_strcmp(nla
, type
->name
) &&
1551 (!type
->family
|| type
->family
== family
))
1557 static const struct nft_expr_type
*nft_expr_type_get(u8 family
,
1560 const struct nft_expr_type
*type
;
1563 return ERR_PTR(-EINVAL
);
1565 type
= __nft_expr_type_get(family
, nla
);
1566 if (type
!= NULL
&& try_module_get(type
->owner
))
1569 #ifdef CONFIG_MODULES
1571 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1572 request_module("nft-expr-%u-%.*s", family
,
1573 nla_len(nla
), (char *)nla_data(nla
));
1574 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1575 if (__nft_expr_type_get(family
, nla
))
1576 return ERR_PTR(-EAGAIN
);
1578 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1579 request_module("nft-expr-%.*s",
1580 nla_len(nla
), (char *)nla_data(nla
));
1581 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1582 if (__nft_expr_type_get(family
, nla
))
1583 return ERR_PTR(-EAGAIN
);
1586 return ERR_PTR(-ENOENT
);
1589 static const struct nla_policy nft_expr_policy
[NFTA_EXPR_MAX
+ 1] = {
1590 [NFTA_EXPR_NAME
] = { .type
= NLA_STRING
},
1591 [NFTA_EXPR_DATA
] = { .type
= NLA_NESTED
},
1594 static int nf_tables_fill_expr_info(struct sk_buff
*skb
,
1595 const struct nft_expr
*expr
)
1597 if (nla_put_string(skb
, NFTA_EXPR_NAME
, expr
->ops
->type
->name
))
1598 goto nla_put_failure
;
1600 if (expr
->ops
->dump
) {
1601 struct nlattr
*data
= nla_nest_start(skb
, NFTA_EXPR_DATA
);
1603 goto nla_put_failure
;
1604 if (expr
->ops
->dump(skb
, expr
) < 0)
1605 goto nla_put_failure
;
1606 nla_nest_end(skb
, data
);
1615 int nft_expr_dump(struct sk_buff
*skb
, unsigned int attr
,
1616 const struct nft_expr
*expr
)
1618 struct nlattr
*nest
;
1620 nest
= nla_nest_start(skb
, attr
);
1622 goto nla_put_failure
;
1623 if (nf_tables_fill_expr_info(skb
, expr
) < 0)
1624 goto nla_put_failure
;
1625 nla_nest_end(skb
, nest
);
1632 struct nft_expr_info
{
1633 const struct nft_expr_ops
*ops
;
1634 struct nlattr
*tb
[NFT_EXPR_MAXATTR
+ 1];
1637 static int nf_tables_expr_parse(const struct nft_ctx
*ctx
,
1638 const struct nlattr
*nla
,
1639 struct nft_expr_info
*info
)
1641 const struct nft_expr_type
*type
;
1642 const struct nft_expr_ops
*ops
;
1643 struct nlattr
*tb
[NFTA_EXPR_MAX
+ 1];
1646 err
= nla_parse_nested(tb
, NFTA_EXPR_MAX
, nla
, nft_expr_policy
);
1650 type
= nft_expr_type_get(ctx
->afi
->family
, tb
[NFTA_EXPR_NAME
]);
1652 return PTR_ERR(type
);
1654 if (tb
[NFTA_EXPR_DATA
]) {
1655 err
= nla_parse_nested(info
->tb
, type
->maxattr
,
1656 tb
[NFTA_EXPR_DATA
], type
->policy
);
1660 memset(info
->tb
, 0, sizeof(info
->tb
[0]) * (type
->maxattr
+ 1));
1662 if (type
->select_ops
!= NULL
) {
1663 ops
= type
->select_ops(ctx
,
1664 (const struct nlattr
* const *)info
->tb
);
1676 module_put(type
->owner
);
1680 static int nf_tables_newexpr(const struct nft_ctx
*ctx
,
1681 const struct nft_expr_info
*info
,
1682 struct nft_expr
*expr
)
1684 const struct nft_expr_ops
*ops
= info
->ops
;
1689 err
= ops
->init(ctx
, expr
, (const struct nlattr
**)info
->tb
);
1701 static void nf_tables_expr_destroy(const struct nft_ctx
*ctx
,
1702 struct nft_expr
*expr
)
1704 if (expr
->ops
->destroy
)
1705 expr
->ops
->destroy(ctx
, expr
);
1706 module_put(expr
->ops
->type
->owner
);
1709 struct nft_expr
*nft_expr_init(const struct nft_ctx
*ctx
,
1710 const struct nlattr
*nla
)
1712 struct nft_expr_info info
;
1713 struct nft_expr
*expr
;
1716 err
= nf_tables_expr_parse(ctx
, nla
, &info
);
1721 expr
= kzalloc(info
.ops
->size
, GFP_KERNEL
);
1725 err
= nf_tables_newexpr(ctx
, &info
, expr
);
1731 module_put(info
.ops
->type
->owner
);
1733 return ERR_PTR(err
);
1736 void nft_expr_destroy(const struct nft_ctx
*ctx
, struct nft_expr
*expr
)
1738 nf_tables_expr_destroy(ctx
, expr
);
1746 static struct nft_rule
*__nf_tables_rule_lookup(const struct nft_chain
*chain
,
1749 struct nft_rule
*rule
;
1751 // FIXME: this sucks
1752 list_for_each_entry(rule
, &chain
->rules
, list
) {
1753 if (handle
== rule
->handle
)
1757 return ERR_PTR(-ENOENT
);
1760 static struct nft_rule
*nf_tables_rule_lookup(const struct nft_chain
*chain
,
1761 const struct nlattr
*nla
)
1764 return ERR_PTR(-EINVAL
);
1766 return __nf_tables_rule_lookup(chain
, be64_to_cpu(nla_get_be64(nla
)));
1769 static const struct nla_policy nft_rule_policy
[NFTA_RULE_MAX
+ 1] = {
1770 [NFTA_RULE_TABLE
] = { .type
= NLA_STRING
},
1771 [NFTA_RULE_CHAIN
] = { .type
= NLA_STRING
,
1772 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
1773 [NFTA_RULE_HANDLE
] = { .type
= NLA_U64
},
1774 [NFTA_RULE_EXPRESSIONS
] = { .type
= NLA_NESTED
},
1775 [NFTA_RULE_COMPAT
] = { .type
= NLA_NESTED
},
1776 [NFTA_RULE_POSITION
] = { .type
= NLA_U64
},
1777 [NFTA_RULE_USERDATA
] = { .type
= NLA_BINARY
,
1778 .len
= NFT_USERDATA_MAXLEN
},
1781 static int nf_tables_fill_rule_info(struct sk_buff
*skb
, struct net
*net
,
1782 u32 portid
, u32 seq
, int event
,
1783 u32 flags
, int family
,
1784 const struct nft_table
*table
,
1785 const struct nft_chain
*chain
,
1786 const struct nft_rule
*rule
)
1788 struct nlmsghdr
*nlh
;
1789 struct nfgenmsg
*nfmsg
;
1790 const struct nft_expr
*expr
, *next
;
1791 struct nlattr
*list
;
1792 const struct nft_rule
*prule
;
1793 int type
= event
| NFNL_SUBSYS_NFTABLES
<< 8;
1795 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(struct nfgenmsg
),
1798 goto nla_put_failure
;
1800 nfmsg
= nlmsg_data(nlh
);
1801 nfmsg
->nfgen_family
= family
;
1802 nfmsg
->version
= NFNETLINK_V0
;
1803 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
1805 if (nla_put_string(skb
, NFTA_RULE_TABLE
, table
->name
))
1806 goto nla_put_failure
;
1807 if (nla_put_string(skb
, NFTA_RULE_CHAIN
, chain
->name
))
1808 goto nla_put_failure
;
1809 if (nla_put_be64(skb
, NFTA_RULE_HANDLE
, cpu_to_be64(rule
->handle
),
1811 goto nla_put_failure
;
1813 if ((event
!= NFT_MSG_DELRULE
) && (rule
->list
.prev
!= &chain
->rules
)) {
1814 prule
= list_entry(rule
->list
.prev
, struct nft_rule
, list
);
1815 if (nla_put_be64(skb
, NFTA_RULE_POSITION
,
1816 cpu_to_be64(prule
->handle
),
1818 goto nla_put_failure
;
1821 list
= nla_nest_start(skb
, NFTA_RULE_EXPRESSIONS
);
1823 goto nla_put_failure
;
1824 nft_rule_for_each_expr(expr
, next
, rule
) {
1825 if (nft_expr_dump(skb
, NFTA_LIST_ELEM
, expr
) < 0)
1826 goto nla_put_failure
;
1828 nla_nest_end(skb
, list
);
1831 struct nft_userdata
*udata
= nft_userdata(rule
);
1832 if (nla_put(skb
, NFTA_RULE_USERDATA
, udata
->len
+ 1,
1834 goto nla_put_failure
;
1837 nlmsg_end(skb
, nlh
);
1841 nlmsg_trim(skb
, nlh
);
1845 static int nf_tables_rule_notify(const struct nft_ctx
*ctx
,
1846 const struct nft_rule
*rule
,
1849 struct sk_buff
*skb
;
1853 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
1857 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1861 err
= nf_tables_fill_rule_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
1862 event
, 0, ctx
->afi
->family
, ctx
->table
,
1869 err
= nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1870 ctx
->report
, GFP_KERNEL
);
1873 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1879 static int nf_tables_dump_rules(struct sk_buff
*skb
,
1880 struct netlink_callback
*cb
)
1882 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1883 const struct nft_af_info
*afi
;
1884 const struct nft_table
*table
;
1885 const struct nft_chain
*chain
;
1886 const struct nft_rule
*rule
;
1887 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1888 struct net
*net
= sock_net(skb
->sk
);
1889 int family
= nfmsg
->nfgen_family
;
1892 cb
->seq
= net
->nft
.base_seq
;
1894 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
1895 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
1898 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
1899 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
1900 list_for_each_entry_rcu(rule
, &chain
->rules
, list
) {
1901 if (!nft_rule_is_active(net
, rule
))
1906 memset(&cb
->args
[1], 0,
1907 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1908 if (nf_tables_fill_rule_info(skb
, net
, NETLINK_CB(cb
->skb
).portid
,
1911 NLM_F_MULTI
| NLM_F_APPEND
,
1912 afi
->family
, table
, chain
, rule
) < 0)
1915 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1929 static int nf_tables_getrule(struct net
*net
, struct sock
*nlsk
,
1930 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1931 const struct nlattr
* const nla
[])
1933 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1934 const struct nft_af_info
*afi
;
1935 const struct nft_table
*table
;
1936 const struct nft_chain
*chain
;
1937 const struct nft_rule
*rule
;
1938 struct sk_buff
*skb2
;
1939 int family
= nfmsg
->nfgen_family
;
1942 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1943 struct netlink_dump_control c
= {
1944 .dump
= nf_tables_dump_rules
,
1946 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
1949 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1951 return PTR_ERR(afi
);
1953 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
1955 return PTR_ERR(table
);
1956 if (table
->flags
& NFT_TABLE_INACTIVE
)
1959 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
1961 return PTR_ERR(chain
);
1962 if (chain
->flags
& NFT_CHAIN_INACTIVE
)
1965 rule
= nf_tables_rule_lookup(chain
, nla
[NFTA_RULE_HANDLE
]);
1967 return PTR_ERR(rule
);
1969 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1973 err
= nf_tables_fill_rule_info(skb2
, net
, NETLINK_CB(skb
).portid
,
1974 nlh
->nlmsg_seq
, NFT_MSG_NEWRULE
, 0,
1975 family
, table
, chain
, rule
);
1979 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
1986 static void nf_tables_rule_destroy(const struct nft_ctx
*ctx
,
1987 struct nft_rule
*rule
)
1989 struct nft_expr
*expr
;
1992 * Careful: some expressions might not be initialized in case this
1993 * is called on error from nf_tables_newrule().
1995 expr
= nft_expr_first(rule
);
1996 while (expr
->ops
&& expr
!= nft_expr_last(rule
)) {
1997 nf_tables_expr_destroy(ctx
, expr
);
1998 expr
= nft_expr_next(expr
);
2003 #define NFT_RULE_MAXEXPRS 128
2005 static struct nft_expr_info
*info
;
2007 static int nf_tables_newrule(struct net
*net
, struct sock
*nlsk
,
2008 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2009 const struct nlattr
* const nla
[])
2011 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2012 struct nft_af_info
*afi
;
2013 struct nft_table
*table
;
2014 struct nft_chain
*chain
;
2015 struct nft_rule
*rule
, *old_rule
= NULL
;
2016 struct nft_userdata
*udata
;
2017 struct nft_trans
*trans
= NULL
;
2018 struct nft_expr
*expr
;
2021 unsigned int size
, i
, n
, ulen
= 0, usize
= 0;
2024 u64 handle
, pos_handle
;
2026 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
2028 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, create
);
2030 return PTR_ERR(afi
);
2032 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
2034 return PTR_ERR(table
);
2036 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
2038 return PTR_ERR(chain
);
2040 if (nla
[NFTA_RULE_HANDLE
]) {
2041 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_HANDLE
]));
2042 rule
= __nf_tables_rule_lookup(chain
, handle
);
2044 return PTR_ERR(rule
);
2046 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2048 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2053 if (!create
|| nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2055 handle
= nf_tables_alloc_handle(table
);
2057 if (chain
->use
== UINT_MAX
)
2061 if (nla
[NFTA_RULE_POSITION
]) {
2062 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
2065 pos_handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_POSITION
]));
2066 old_rule
= __nf_tables_rule_lookup(chain
, pos_handle
);
2067 if (IS_ERR(old_rule
))
2068 return PTR_ERR(old_rule
);
2071 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
2075 if (nla
[NFTA_RULE_EXPRESSIONS
]) {
2076 nla_for_each_nested(tmp
, nla
[NFTA_RULE_EXPRESSIONS
], rem
) {
2078 if (nla_type(tmp
) != NFTA_LIST_ELEM
)
2080 if (n
== NFT_RULE_MAXEXPRS
)
2082 err
= nf_tables_expr_parse(&ctx
, tmp
, &info
[n
]);
2085 size
+= info
[n
].ops
->size
;
2089 /* Check for overflow of dlen field */
2091 if (size
>= 1 << 12)
2094 if (nla
[NFTA_RULE_USERDATA
]) {
2095 ulen
= nla_len(nla
[NFTA_RULE_USERDATA
]);
2097 usize
= sizeof(struct nft_userdata
) + ulen
;
2101 rule
= kzalloc(sizeof(*rule
) + size
+ usize
, GFP_KERNEL
);
2105 nft_rule_activate_next(net
, rule
);
2107 rule
->handle
= handle
;
2109 rule
->udata
= ulen
? 1 : 0;
2112 udata
= nft_userdata(rule
);
2113 udata
->len
= ulen
- 1;
2114 nla_memcpy(udata
->data
, nla
[NFTA_RULE_USERDATA
], ulen
);
2117 expr
= nft_expr_first(rule
);
2118 for (i
= 0; i
< n
; i
++) {
2119 err
= nf_tables_newexpr(&ctx
, &info
[i
], expr
);
2123 expr
= nft_expr_next(expr
);
2126 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
2127 if (nft_rule_is_active_next(net
, old_rule
)) {
2128 trans
= nft_trans_rule_add(&ctx
, NFT_MSG_DELRULE
,
2130 if (trans
== NULL
) {
2134 nft_rule_deactivate_next(net
, old_rule
);
2136 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
2141 } else if (nlh
->nlmsg_flags
& NLM_F_APPEND
)
2143 list_add_rcu(&rule
->list
, &old_rule
->list
);
2145 list_add_tail_rcu(&rule
->list
, &chain
->rules
);
2148 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
2150 list_add_rcu(&rule
->list
, &chain
->rules
);
2153 if (nft_trans_rule_add(&ctx
, NFT_MSG_NEWRULE
, rule
) == NULL
) {
2161 list_del_rcu(&rule
->list
);
2163 nf_tables_rule_destroy(&ctx
, rule
);
2165 for (i
= 0; i
< n
; i
++) {
2166 if (info
[i
].ops
!= NULL
)
2167 module_put(info
[i
].ops
->type
->owner
);
2172 static int nf_tables_delrule(struct net
*net
, struct sock
*nlsk
,
2173 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2174 const struct nlattr
* const nla
[])
2176 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2177 struct nft_af_info
*afi
;
2178 struct nft_table
*table
;
2179 struct nft_chain
*chain
= NULL
;
2180 struct nft_rule
*rule
;
2181 int family
= nfmsg
->nfgen_family
, err
= 0;
2184 afi
= nf_tables_afinfo_lookup(net
, family
, false);
2186 return PTR_ERR(afi
);
2188 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
2190 return PTR_ERR(table
);
2192 if (nla
[NFTA_RULE_CHAIN
]) {
2193 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
2195 return PTR_ERR(chain
);
2198 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
2201 if (nla
[NFTA_RULE_HANDLE
]) {
2202 rule
= nf_tables_rule_lookup(chain
,
2203 nla
[NFTA_RULE_HANDLE
]);
2205 return PTR_ERR(rule
);
2207 err
= nft_delrule(&ctx
, rule
);
2209 err
= nft_delrule_by_chain(&ctx
);
2212 list_for_each_entry(chain
, &table
->chains
, list
) {
2214 err
= nft_delrule_by_chain(&ctx
);
2227 static LIST_HEAD(nf_tables_set_ops
);
2229 int nft_register_set(struct nft_set_ops
*ops
)
2231 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2232 list_add_tail_rcu(&ops
->list
, &nf_tables_set_ops
);
2233 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2236 EXPORT_SYMBOL_GPL(nft_register_set
);
2238 void nft_unregister_set(struct nft_set_ops
*ops
)
2240 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2241 list_del_rcu(&ops
->list
);
2242 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2244 EXPORT_SYMBOL_GPL(nft_unregister_set
);
2247 * Select a set implementation based on the data characteristics and the
2248 * given policy. The total memory use might not be known if no size is
2249 * given, in that case the amount of memory per element is used.
2251 static const struct nft_set_ops
*
2252 nft_select_set_ops(const struct nlattr
* const nla
[],
2253 const struct nft_set_desc
*desc
,
2254 enum nft_set_policies policy
)
2256 const struct nft_set_ops
*ops
, *bops
;
2257 struct nft_set_estimate est
, best
;
2260 #ifdef CONFIG_MODULES
2261 if (list_empty(&nf_tables_set_ops
)) {
2262 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2263 request_module("nft-set");
2264 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2265 if (!list_empty(&nf_tables_set_ops
))
2266 return ERR_PTR(-EAGAIN
);
2270 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
2271 features
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
2272 features
&= NFT_SET_INTERVAL
| NFT_SET_MAP
| NFT_SET_TIMEOUT
;
2279 list_for_each_entry(ops
, &nf_tables_set_ops
, list
) {
2280 if ((ops
->features
& features
) != features
)
2282 if (!ops
->estimate(desc
, features
, &est
))
2286 case NFT_SET_POL_PERFORMANCE
:
2287 if (est
.class < best
.class)
2289 if (est
.class == best
.class && est
.size
< best
.size
)
2292 case NFT_SET_POL_MEMORY
:
2293 if (est
.size
< best
.size
)
2295 if (est
.size
== best
.size
&& est
.class < best
.class)
2302 if (!try_module_get(ops
->owner
))
2305 module_put(bops
->owner
);
2314 return ERR_PTR(-EOPNOTSUPP
);
2317 static const struct nla_policy nft_set_policy
[NFTA_SET_MAX
+ 1] = {
2318 [NFTA_SET_TABLE
] = { .type
= NLA_STRING
},
2319 [NFTA_SET_NAME
] = { .type
= NLA_STRING
,
2320 .len
= NFT_SET_MAXNAMELEN
- 1 },
2321 [NFTA_SET_FLAGS
] = { .type
= NLA_U32
},
2322 [NFTA_SET_KEY_TYPE
] = { .type
= NLA_U32
},
2323 [NFTA_SET_KEY_LEN
] = { .type
= NLA_U32
},
2324 [NFTA_SET_DATA_TYPE
] = { .type
= NLA_U32
},
2325 [NFTA_SET_DATA_LEN
] = { .type
= NLA_U32
},
2326 [NFTA_SET_POLICY
] = { .type
= NLA_U32
},
2327 [NFTA_SET_DESC
] = { .type
= NLA_NESTED
},
2328 [NFTA_SET_ID
] = { .type
= NLA_U32
},
2329 [NFTA_SET_TIMEOUT
] = { .type
= NLA_U64
},
2330 [NFTA_SET_GC_INTERVAL
] = { .type
= NLA_U32
},
2331 [NFTA_SET_USERDATA
] = { .type
= NLA_BINARY
,
2332 .len
= NFT_USERDATA_MAXLEN
},
2335 static const struct nla_policy nft_set_desc_policy
[NFTA_SET_DESC_MAX
+ 1] = {
2336 [NFTA_SET_DESC_SIZE
] = { .type
= NLA_U32
},
2339 static int nft_ctx_init_from_setattr(struct nft_ctx
*ctx
, struct net
*net
,
2340 const struct sk_buff
*skb
,
2341 const struct nlmsghdr
*nlh
,
2342 const struct nlattr
* const nla
[])
2344 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2345 struct nft_af_info
*afi
= NULL
;
2346 struct nft_table
*table
= NULL
;
2348 if (nfmsg
->nfgen_family
!= NFPROTO_UNSPEC
) {
2349 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, false);
2351 return PTR_ERR(afi
);
2354 if (nla
[NFTA_SET_TABLE
] != NULL
) {
2356 return -EAFNOSUPPORT
;
2358 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
]);
2360 return PTR_ERR(table
);
2363 nft_ctx_init(ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
2367 struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
2368 const struct nlattr
*nla
)
2370 struct nft_set
*set
;
2373 return ERR_PTR(-EINVAL
);
2375 list_for_each_entry(set
, &table
->sets
, list
) {
2376 if (!nla_strcmp(nla
, set
->name
))
2379 return ERR_PTR(-ENOENT
);
2382 struct nft_set
*nf_tables_set_lookup_byid(const struct net
*net
,
2383 const struct nlattr
*nla
)
2385 struct nft_trans
*trans
;
2386 u32 id
= ntohl(nla_get_be32(nla
));
2388 list_for_each_entry(trans
, &net
->nft
.commit_list
, list
) {
2389 if (trans
->msg_type
== NFT_MSG_NEWSET
&&
2390 id
== nft_trans_set_id(trans
))
2391 return nft_trans_set(trans
);
2393 return ERR_PTR(-ENOENT
);
2396 static int nf_tables_set_alloc_name(struct nft_ctx
*ctx
, struct nft_set
*set
,
2399 const struct nft_set
*i
;
2401 unsigned long *inuse
;
2402 unsigned int n
= 0, min
= 0;
2404 p
= strnchr(name
, NFT_SET_MAXNAMELEN
, '%');
2406 if (p
[1] != 'd' || strchr(p
+ 2, '%'))
2409 inuse
= (unsigned long *)get_zeroed_page(GFP_KERNEL
);
2413 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
2416 if (!sscanf(i
->name
, name
, &tmp
))
2418 if (tmp
< min
|| tmp
>= min
+ BITS_PER_BYTE
* PAGE_SIZE
)
2421 set_bit(tmp
- min
, inuse
);
2424 n
= find_first_zero_bit(inuse
, BITS_PER_BYTE
* PAGE_SIZE
);
2425 if (n
>= BITS_PER_BYTE
* PAGE_SIZE
) {
2426 min
+= BITS_PER_BYTE
* PAGE_SIZE
;
2427 memset(inuse
, 0, PAGE_SIZE
);
2430 free_page((unsigned long)inuse
);
2433 snprintf(set
->name
, sizeof(set
->name
), name
, min
+ n
);
2434 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
2435 if (!strcmp(set
->name
, i
->name
))
2441 static int nf_tables_fill_set(struct sk_buff
*skb
, const struct nft_ctx
*ctx
,
2442 const struct nft_set
*set
, u16 event
, u16 flags
)
2444 struct nfgenmsg
*nfmsg
;
2445 struct nlmsghdr
*nlh
;
2446 struct nlattr
*desc
;
2447 u32 portid
= ctx
->portid
;
2450 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
2451 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
2454 goto nla_put_failure
;
2456 nfmsg
= nlmsg_data(nlh
);
2457 nfmsg
->nfgen_family
= ctx
->afi
->family
;
2458 nfmsg
->version
= NFNETLINK_V0
;
2459 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
2461 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
2462 goto nla_put_failure
;
2463 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
2464 goto nla_put_failure
;
2465 if (set
->flags
!= 0)
2466 if (nla_put_be32(skb
, NFTA_SET_FLAGS
, htonl(set
->flags
)))
2467 goto nla_put_failure
;
2469 if (nla_put_be32(skb
, NFTA_SET_KEY_TYPE
, htonl(set
->ktype
)))
2470 goto nla_put_failure
;
2471 if (nla_put_be32(skb
, NFTA_SET_KEY_LEN
, htonl(set
->klen
)))
2472 goto nla_put_failure
;
2473 if (set
->flags
& NFT_SET_MAP
) {
2474 if (nla_put_be32(skb
, NFTA_SET_DATA_TYPE
, htonl(set
->dtype
)))
2475 goto nla_put_failure
;
2476 if (nla_put_be32(skb
, NFTA_SET_DATA_LEN
, htonl(set
->dlen
)))
2477 goto nla_put_failure
;
2481 nla_put_be64(skb
, NFTA_SET_TIMEOUT
, cpu_to_be64(set
->timeout
),
2483 goto nla_put_failure
;
2485 nla_put_be32(skb
, NFTA_SET_GC_INTERVAL
, htonl(set
->gc_int
)))
2486 goto nla_put_failure
;
2488 if (set
->policy
!= NFT_SET_POL_PERFORMANCE
) {
2489 if (nla_put_be32(skb
, NFTA_SET_POLICY
, htonl(set
->policy
)))
2490 goto nla_put_failure
;
2493 if (nla_put(skb
, NFTA_SET_USERDATA
, set
->udlen
, set
->udata
))
2494 goto nla_put_failure
;
2496 desc
= nla_nest_start(skb
, NFTA_SET_DESC
);
2498 goto nla_put_failure
;
2500 nla_put_be32(skb
, NFTA_SET_DESC_SIZE
, htonl(set
->size
)))
2501 goto nla_put_failure
;
2502 nla_nest_end(skb
, desc
);
2504 nlmsg_end(skb
, nlh
);
2508 nlmsg_trim(skb
, nlh
);
2512 static int nf_tables_set_notify(const struct nft_ctx
*ctx
,
2513 const struct nft_set
*set
,
2514 int event
, gfp_t gfp_flags
)
2516 struct sk_buff
*skb
;
2517 u32 portid
= ctx
->portid
;
2521 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
2525 skb
= nlmsg_new(NLMSG_GOODSIZE
, gfp_flags
);
2529 err
= nf_tables_fill_set(skb
, ctx
, set
, event
, 0);
2535 err
= nfnetlink_send(skb
, ctx
->net
, portid
, NFNLGRP_NFTABLES
,
2536 ctx
->report
, gfp_flags
);
2539 nfnetlink_set_err(ctx
->net
, portid
, NFNLGRP_NFTABLES
, err
);
2543 static int nf_tables_dump_sets(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2545 const struct nft_set
*set
;
2546 unsigned int idx
, s_idx
= cb
->args
[0];
2547 struct nft_af_info
*afi
;
2548 struct nft_table
*table
, *cur_table
= (struct nft_table
*)cb
->args
[2];
2549 struct net
*net
= sock_net(skb
->sk
);
2550 int cur_family
= cb
->args
[3];
2551 struct nft_ctx
*ctx
= cb
->data
, ctx_set
;
2557 cb
->seq
= net
->nft
.base_seq
;
2559 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
2560 if (ctx
->afi
&& ctx
->afi
!= afi
)
2564 if (afi
->family
!= cur_family
)
2569 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
2570 if (ctx
->table
&& ctx
->table
!= table
)
2574 if (cur_table
!= table
)
2580 list_for_each_entry_rcu(set
, &table
->sets
, list
) {
2585 ctx_set
.table
= table
;
2587 if (nf_tables_fill_set(skb
, &ctx_set
, set
,
2591 cb
->args
[2] = (unsigned long) table
;
2592 cb
->args
[3] = afi
->family
;
2595 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2609 static int nf_tables_dump_sets_done(struct netlink_callback
*cb
)
2615 static int nf_tables_getset(struct net
*net
, struct sock
*nlsk
,
2616 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2617 const struct nlattr
* const nla
[])
2619 const struct nft_set
*set
;
2621 struct sk_buff
*skb2
;
2622 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2625 /* Verify existence before starting dump */
2626 err
= nft_ctx_init_from_setattr(&ctx
, net
, skb
, nlh
, nla
);
2630 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2631 struct netlink_dump_control c
= {
2632 .dump
= nf_tables_dump_sets
,
2633 .done
= nf_tables_dump_sets_done
,
2635 struct nft_ctx
*ctx_dump
;
2637 ctx_dump
= kmalloc(sizeof(*ctx_dump
), GFP_KERNEL
);
2638 if (ctx_dump
== NULL
)
2644 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
2647 /* Only accept unspec with dump */
2648 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
2649 return -EAFNOSUPPORT
;
2650 if (!nla
[NFTA_SET_TABLE
])
2653 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
]);
2655 return PTR_ERR(set
);
2656 if (set
->flags
& NFT_SET_INACTIVE
)
2659 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2663 err
= nf_tables_fill_set(skb2
, &ctx
, set
, NFT_MSG_NEWSET
, 0);
2667 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
2674 static int nf_tables_set_desc_parse(const struct nft_ctx
*ctx
,
2675 struct nft_set_desc
*desc
,
2676 const struct nlattr
*nla
)
2678 struct nlattr
*da
[NFTA_SET_DESC_MAX
+ 1];
2681 err
= nla_parse_nested(da
, NFTA_SET_DESC_MAX
, nla
, nft_set_desc_policy
);
2685 if (da
[NFTA_SET_DESC_SIZE
] != NULL
)
2686 desc
->size
= ntohl(nla_get_be32(da
[NFTA_SET_DESC_SIZE
]));
2691 static int nf_tables_newset(struct net
*net
, struct sock
*nlsk
,
2692 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2693 const struct nlattr
* const nla
[])
2695 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2696 const struct nft_set_ops
*ops
;
2697 struct nft_af_info
*afi
;
2698 struct nft_table
*table
;
2699 struct nft_set
*set
;
2701 char name
[NFT_SET_MAXNAMELEN
];
2705 u32 ktype
, dtype
, flags
, policy
, gc_int
;
2706 struct nft_set_desc desc
;
2707 unsigned char *udata
;
2711 if (nla
[NFTA_SET_TABLE
] == NULL
||
2712 nla
[NFTA_SET_NAME
] == NULL
||
2713 nla
[NFTA_SET_KEY_LEN
] == NULL
||
2714 nla
[NFTA_SET_ID
] == NULL
)
2717 memset(&desc
, 0, sizeof(desc
));
2719 ktype
= NFT_DATA_VALUE
;
2720 if (nla
[NFTA_SET_KEY_TYPE
] != NULL
) {
2721 ktype
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_TYPE
]));
2722 if ((ktype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
)
2726 desc
.klen
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_LEN
]));
2727 if (desc
.klen
== 0 || desc
.klen
> NFT_DATA_VALUE_MAXLEN
)
2731 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
2732 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
2733 if (flags
& ~(NFT_SET_ANONYMOUS
| NFT_SET_CONSTANT
|
2734 NFT_SET_INTERVAL
| NFT_SET_TIMEOUT
|
2735 NFT_SET_MAP
| NFT_SET_EVAL
))
2737 /* Only one of both operations is supported */
2738 if ((flags
& (NFT_SET_MAP
| NFT_SET_EVAL
)) ==
2739 (NFT_SET_MAP
| NFT_SET_EVAL
))
2744 if (nla
[NFTA_SET_DATA_TYPE
] != NULL
) {
2745 if (!(flags
& NFT_SET_MAP
))
2748 dtype
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_TYPE
]));
2749 if ((dtype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
&&
2750 dtype
!= NFT_DATA_VERDICT
)
2753 if (dtype
!= NFT_DATA_VERDICT
) {
2754 if (nla
[NFTA_SET_DATA_LEN
] == NULL
)
2756 desc
.dlen
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_LEN
]));
2757 if (desc
.dlen
== 0 || desc
.dlen
> NFT_DATA_VALUE_MAXLEN
)
2760 desc
.dlen
= sizeof(struct nft_verdict
);
2761 } else if (flags
& NFT_SET_MAP
)
2765 if (nla
[NFTA_SET_TIMEOUT
] != NULL
) {
2766 if (!(flags
& NFT_SET_TIMEOUT
))
2768 timeout
= be64_to_cpu(nla_get_be64(nla
[NFTA_SET_TIMEOUT
]));
2771 if (nla
[NFTA_SET_GC_INTERVAL
] != NULL
) {
2772 if (!(flags
& NFT_SET_TIMEOUT
))
2774 gc_int
= ntohl(nla_get_be32(nla
[NFTA_SET_GC_INTERVAL
]));
2777 policy
= NFT_SET_POL_PERFORMANCE
;
2778 if (nla
[NFTA_SET_POLICY
] != NULL
)
2779 policy
= ntohl(nla_get_be32(nla
[NFTA_SET_POLICY
]));
2781 if (nla
[NFTA_SET_DESC
] != NULL
) {
2782 err
= nf_tables_set_desc_parse(&ctx
, &desc
, nla
[NFTA_SET_DESC
]);
2787 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
2789 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, create
);
2791 return PTR_ERR(afi
);
2793 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
]);
2795 return PTR_ERR(table
);
2797 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
2799 set
= nf_tables_set_lookup(table
, nla
[NFTA_SET_NAME
]);
2801 if (PTR_ERR(set
) != -ENOENT
)
2802 return PTR_ERR(set
);
2807 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2809 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2814 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
2817 ops
= nft_select_set_ops(nla
, &desc
, policy
);
2819 return PTR_ERR(ops
);
2822 if (nla
[NFTA_SET_USERDATA
])
2823 udlen
= nla_len(nla
[NFTA_SET_USERDATA
]);
2826 if (ops
->privsize
!= NULL
)
2827 size
= ops
->privsize(nla
);
2830 set
= kzalloc(sizeof(*set
) + size
+ udlen
, GFP_KERNEL
);
2834 nla_strlcpy(name
, nla
[NFTA_SET_NAME
], sizeof(set
->name
));
2835 err
= nf_tables_set_alloc_name(&ctx
, set
, name
);
2841 udata
= set
->data
+ size
;
2842 nla_memcpy(udata
, nla
[NFTA_SET_USERDATA
], udlen
);
2845 INIT_LIST_HEAD(&set
->bindings
);
2846 write_pnet(&set
->pnet
, net
);
2849 set
->klen
= desc
.klen
;
2851 set
->dlen
= desc
.dlen
;
2853 set
->size
= desc
.size
;
2854 set
->policy
= policy
;
2857 set
->timeout
= timeout
;
2858 set
->gc_int
= gc_int
;
2860 err
= ops
->init(set
, &desc
, nla
);
2864 err
= nft_trans_set_add(&ctx
, NFT_MSG_NEWSET
, set
);
2868 list_add_tail_rcu(&set
->list
, &table
->sets
);
2875 module_put(ops
->owner
);
2879 static void nft_set_destroy(struct nft_set
*set
)
2881 set
->ops
->destroy(set
);
2882 module_put(set
->ops
->owner
);
2886 static void nf_tables_set_destroy(const struct nft_ctx
*ctx
, struct nft_set
*set
)
2888 list_del_rcu(&set
->list
);
2889 nf_tables_set_notify(ctx
, set
, NFT_MSG_DELSET
, GFP_ATOMIC
);
2890 nft_set_destroy(set
);
2893 static int nf_tables_delset(struct net
*net
, struct sock
*nlsk
,
2894 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2895 const struct nlattr
* const nla
[])
2897 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2898 struct nft_set
*set
;
2902 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
2903 return -EAFNOSUPPORT
;
2904 if (nla
[NFTA_SET_TABLE
] == NULL
)
2907 err
= nft_ctx_init_from_setattr(&ctx
, net
, skb
, nlh
, nla
);
2911 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
]);
2913 return PTR_ERR(set
);
2914 if (!list_empty(&set
->bindings
))
2917 return nft_delset(&ctx
, set
);
2920 static int nf_tables_bind_check_setelem(const struct nft_ctx
*ctx
,
2921 const struct nft_set
*set
,
2922 const struct nft_set_iter
*iter
,
2923 const struct nft_set_elem
*elem
)
2925 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
2926 enum nft_registers dreg
;
2928 dreg
= nft_type_to_reg(set
->dtype
);
2929 return nft_validate_register_store(ctx
, dreg
, nft_set_ext_data(ext
),
2930 set
->dtype
== NFT_DATA_VERDICT
?
2931 NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
2935 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2936 struct nft_set_binding
*binding
)
2938 struct nft_set_binding
*i
;
2939 struct nft_set_iter iter
;
2941 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
)
2944 if (binding
->flags
& NFT_SET_MAP
) {
2945 /* If the set is already bound to the same chain all
2946 * jumps are already validated for that chain.
2948 list_for_each_entry(i
, &set
->bindings
, list
) {
2949 if (binding
->flags
& NFT_SET_MAP
&&
2950 i
->chain
== binding
->chain
)
2957 iter
.fn
= nf_tables_bind_check_setelem
;
2959 set
->ops
->walk(ctx
, set
, &iter
);
2961 /* Destroy anonymous sets if binding fails */
2962 if (set
->flags
& NFT_SET_ANONYMOUS
)
2963 nf_tables_set_destroy(ctx
, set
);
2969 binding
->chain
= ctx
->chain
;
2970 list_add_tail_rcu(&binding
->list
, &set
->bindings
);
2974 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2975 struct nft_set_binding
*binding
)
2977 list_del_rcu(&binding
->list
);
2979 if (list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
&&
2980 !(set
->flags
& NFT_SET_INACTIVE
))
2981 nf_tables_set_destroy(ctx
, set
);
2984 const struct nft_set_ext_type nft_set_ext_types
[] = {
2985 [NFT_SET_EXT_KEY
] = {
2986 .align
= __alignof__(u32
),
2988 [NFT_SET_EXT_DATA
] = {
2989 .align
= __alignof__(u32
),
2991 [NFT_SET_EXT_EXPR
] = {
2992 .align
= __alignof__(struct nft_expr
),
2994 [NFT_SET_EXT_FLAGS
] = {
2996 .align
= __alignof__(u8
),
2998 [NFT_SET_EXT_TIMEOUT
] = {
3000 .align
= __alignof__(u64
),
3002 [NFT_SET_EXT_EXPIRATION
] = {
3003 .len
= sizeof(unsigned long),
3004 .align
= __alignof__(unsigned long),
3006 [NFT_SET_EXT_USERDATA
] = {
3007 .len
= sizeof(struct nft_userdata
),
3008 .align
= __alignof__(struct nft_userdata
),
3011 EXPORT_SYMBOL_GPL(nft_set_ext_types
);
3017 static const struct nla_policy nft_set_elem_policy
[NFTA_SET_ELEM_MAX
+ 1] = {
3018 [NFTA_SET_ELEM_KEY
] = { .type
= NLA_NESTED
},
3019 [NFTA_SET_ELEM_DATA
] = { .type
= NLA_NESTED
},
3020 [NFTA_SET_ELEM_FLAGS
] = { .type
= NLA_U32
},
3021 [NFTA_SET_ELEM_TIMEOUT
] = { .type
= NLA_U64
},
3022 [NFTA_SET_ELEM_USERDATA
] = { .type
= NLA_BINARY
,
3023 .len
= NFT_USERDATA_MAXLEN
},
3026 static const struct nla_policy nft_set_elem_list_policy
[NFTA_SET_ELEM_LIST_MAX
+ 1] = {
3027 [NFTA_SET_ELEM_LIST_TABLE
] = { .type
= NLA_STRING
},
3028 [NFTA_SET_ELEM_LIST_SET
] = { .type
= NLA_STRING
},
3029 [NFTA_SET_ELEM_LIST_ELEMENTS
] = { .type
= NLA_NESTED
},
3030 [NFTA_SET_ELEM_LIST_SET_ID
] = { .type
= NLA_U32
},
3033 static int nft_ctx_init_from_elemattr(struct nft_ctx
*ctx
, struct net
*net
,
3034 const struct sk_buff
*skb
,
3035 const struct nlmsghdr
*nlh
,
3036 const struct nlattr
* const nla
[])
3038 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3039 struct nft_af_info
*afi
;
3040 struct nft_table
*table
;
3042 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, false);
3044 return PTR_ERR(afi
);
3046 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_ELEM_LIST_TABLE
]);
3048 return PTR_ERR(table
);
3050 nft_ctx_init(ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
3054 static int nf_tables_fill_setelem(struct sk_buff
*skb
,
3055 const struct nft_set
*set
,
3056 const struct nft_set_elem
*elem
)
3058 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
3059 unsigned char *b
= skb_tail_pointer(skb
);
3060 struct nlattr
*nest
;
3062 nest
= nla_nest_start(skb
, NFTA_LIST_ELEM
);
3064 goto nla_put_failure
;
3066 if (nft_data_dump(skb
, NFTA_SET_ELEM_KEY
, nft_set_ext_key(ext
),
3067 NFT_DATA_VALUE
, set
->klen
) < 0)
3068 goto nla_put_failure
;
3070 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
) &&
3071 nft_data_dump(skb
, NFTA_SET_ELEM_DATA
, nft_set_ext_data(ext
),
3072 set
->dtype
== NFT_DATA_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
3074 goto nla_put_failure
;
3076 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
) &&
3077 nft_expr_dump(skb
, NFTA_SET_ELEM_EXPR
, nft_set_ext_expr(ext
)) < 0)
3078 goto nla_put_failure
;
3080 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
3081 nla_put_be32(skb
, NFTA_SET_ELEM_FLAGS
,
3082 htonl(*nft_set_ext_flags(ext
))))
3083 goto nla_put_failure
;
3085 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
) &&
3086 nla_put_be64(skb
, NFTA_SET_ELEM_TIMEOUT
,
3087 cpu_to_be64(*nft_set_ext_timeout(ext
)),
3089 goto nla_put_failure
;
3091 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
)) {
3092 unsigned long expires
, now
= jiffies
;
3094 expires
= *nft_set_ext_expiration(ext
);
3095 if (time_before(now
, expires
))
3100 if (nla_put_be64(skb
, NFTA_SET_ELEM_EXPIRATION
,
3101 cpu_to_be64(jiffies_to_msecs(expires
)),
3103 goto nla_put_failure
;
3106 if (nft_set_ext_exists(ext
, NFT_SET_EXT_USERDATA
)) {
3107 struct nft_userdata
*udata
;
3109 udata
= nft_set_ext_userdata(ext
);
3110 if (nla_put(skb
, NFTA_SET_ELEM_USERDATA
,
3111 udata
->len
+ 1, udata
->data
))
3112 goto nla_put_failure
;
3115 nla_nest_end(skb
, nest
);
3123 struct nft_set_dump_args
{
3124 const struct netlink_callback
*cb
;
3125 struct nft_set_iter iter
;
3126 struct sk_buff
*skb
;
3129 static int nf_tables_dump_setelem(const struct nft_ctx
*ctx
,
3130 const struct nft_set
*set
,
3131 const struct nft_set_iter
*iter
,
3132 const struct nft_set_elem
*elem
)
3134 struct nft_set_dump_args
*args
;
3136 args
= container_of(iter
, struct nft_set_dump_args
, iter
);
3137 return nf_tables_fill_setelem(args
->skb
, set
, elem
);
3140 static int nf_tables_dump_set(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3142 struct net
*net
= sock_net(skb
->sk
);
3143 const struct nft_set
*set
;
3144 struct nft_set_dump_args args
;
3146 struct nlattr
*nla
[NFTA_SET_ELEM_LIST_MAX
+ 1];
3147 struct nfgenmsg
*nfmsg
;
3148 struct nlmsghdr
*nlh
;
3149 struct nlattr
*nest
;
3153 err
= nlmsg_parse(cb
->nlh
, sizeof(struct nfgenmsg
), nla
,
3154 NFTA_SET_ELEM_LIST_MAX
, nft_set_elem_list_policy
);
3158 err
= nft_ctx_init_from_elemattr(&ctx
, net
, cb
->skb
, cb
->nlh
,
3162 if (ctx
.table
->flags
& NFT_TABLE_INACTIVE
)
3165 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3167 return PTR_ERR(set
);
3168 if (set
->flags
& NFT_SET_INACTIVE
)
3171 event
= NFT_MSG_NEWSETELEM
;
3172 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
3173 portid
= NETLINK_CB(cb
->skb
).portid
;
3174 seq
= cb
->nlh
->nlmsg_seq
;
3176 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
3179 goto nla_put_failure
;
3181 nfmsg
= nlmsg_data(nlh
);
3182 nfmsg
->nfgen_family
= ctx
.afi
->family
;
3183 nfmsg
->version
= NFNETLINK_V0
;
3184 nfmsg
->res_id
= htons(ctx
.net
->nft
.base_seq
& 0xffff);
3186 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_TABLE
, ctx
.table
->name
))
3187 goto nla_put_failure
;
3188 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_SET
, set
->name
))
3189 goto nla_put_failure
;
3191 nest
= nla_nest_start(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
3193 goto nla_put_failure
;
3197 args
.iter
.skip
= cb
->args
[0];
3198 args
.iter
.count
= 0;
3200 args
.iter
.fn
= nf_tables_dump_setelem
;
3201 set
->ops
->walk(&ctx
, set
, &args
.iter
);
3203 nla_nest_end(skb
, nest
);
3204 nlmsg_end(skb
, nlh
);
3206 if (args
.iter
.err
&& args
.iter
.err
!= -EMSGSIZE
)
3207 return args
.iter
.err
;
3208 if (args
.iter
.count
== cb
->args
[0])
3211 cb
->args
[0] = args
.iter
.count
;
3218 static int nf_tables_getsetelem(struct net
*net
, struct sock
*nlsk
,
3219 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3220 const struct nlattr
* const nla
[])
3222 const struct nft_set
*set
;
3226 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
);
3229 if (ctx
.table
->flags
& NFT_TABLE_INACTIVE
)
3232 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3234 return PTR_ERR(set
);
3235 if (set
->flags
& NFT_SET_INACTIVE
)
3238 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3239 struct netlink_dump_control c
= {
3240 .dump
= nf_tables_dump_set
,
3242 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
3247 static int nf_tables_fill_setelem_info(struct sk_buff
*skb
,
3248 const struct nft_ctx
*ctx
, u32 seq
,
3249 u32 portid
, int event
, u16 flags
,
3250 const struct nft_set
*set
,
3251 const struct nft_set_elem
*elem
)
3253 struct nfgenmsg
*nfmsg
;
3254 struct nlmsghdr
*nlh
;
3255 struct nlattr
*nest
;
3258 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
3259 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
3262 goto nla_put_failure
;
3264 nfmsg
= nlmsg_data(nlh
);
3265 nfmsg
->nfgen_family
= ctx
->afi
->family
;
3266 nfmsg
->version
= NFNETLINK_V0
;
3267 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
3269 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
3270 goto nla_put_failure
;
3271 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
3272 goto nla_put_failure
;
3274 nest
= nla_nest_start(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
3276 goto nla_put_failure
;
3278 err
= nf_tables_fill_setelem(skb
, set
, elem
);
3280 goto nla_put_failure
;
3282 nla_nest_end(skb
, nest
);
3284 nlmsg_end(skb
, nlh
);
3288 nlmsg_trim(skb
, nlh
);
3292 static int nf_tables_setelem_notify(const struct nft_ctx
*ctx
,
3293 const struct nft_set
*set
,
3294 const struct nft_set_elem
*elem
,
3295 int event
, u16 flags
)
3297 struct net
*net
= ctx
->net
;
3298 u32 portid
= ctx
->portid
;
3299 struct sk_buff
*skb
;
3302 if (!ctx
->report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
3306 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
3310 err
= nf_tables_fill_setelem_info(skb
, ctx
, 0, portid
, event
, flags
,
3317 err
= nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, ctx
->report
,
3321 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, err
);
3325 static struct nft_trans
*nft_trans_elem_alloc(struct nft_ctx
*ctx
,
3327 struct nft_set
*set
)
3329 struct nft_trans
*trans
;
3331 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_elem
));
3335 nft_trans_elem_set(trans
) = set
;
3339 void *nft_set_elem_init(const struct nft_set
*set
,
3340 const struct nft_set_ext_tmpl
*tmpl
,
3341 const u32
*key
, const u32
*data
,
3342 u64 timeout
, gfp_t gfp
)
3344 struct nft_set_ext
*ext
;
3347 elem
= kzalloc(set
->ops
->elemsize
+ tmpl
->len
, gfp
);
3351 ext
= nft_set_elem_ext(set
, elem
);
3352 nft_set_ext_init(ext
, tmpl
);
3354 memcpy(nft_set_ext_key(ext
), key
, set
->klen
);
3355 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
3356 memcpy(nft_set_ext_data(ext
), data
, set
->dlen
);
3357 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
))
3358 *nft_set_ext_expiration(ext
) =
3359 jiffies
+ msecs_to_jiffies(timeout
);
3360 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
))
3361 *nft_set_ext_timeout(ext
) = timeout
;
3366 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
)
3368 struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
);
3370 nft_data_uninit(nft_set_ext_key(ext
), NFT_DATA_VALUE
);
3371 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
3372 nft_data_uninit(nft_set_ext_data(ext
), set
->dtype
);
3373 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
))
3374 nf_tables_expr_destroy(NULL
, nft_set_ext_expr(ext
));
3378 EXPORT_SYMBOL_GPL(nft_set_elem_destroy
);
3380 static int nft_setelem_parse_flags(const struct nft_set
*set
,
3381 const struct nlattr
*attr
, u32
*flags
)
3386 *flags
= ntohl(nla_get_be32(attr
));
3387 if (*flags
& ~NFT_SET_ELEM_INTERVAL_END
)
3389 if (!(set
->flags
& NFT_SET_INTERVAL
) &&
3390 *flags
& NFT_SET_ELEM_INTERVAL_END
)
3396 static int nft_add_set_elem(struct nft_ctx
*ctx
, struct nft_set
*set
,
3397 const struct nlattr
*attr
)
3399 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
3400 struct nft_data_desc d1
, d2
;
3401 struct nft_set_ext_tmpl tmpl
;
3402 struct nft_set_ext
*ext
;
3403 struct nft_set_elem elem
;
3404 struct nft_set_binding
*binding
;
3405 struct nft_userdata
*udata
;
3406 struct nft_data data
;
3407 enum nft_registers dreg
;
3408 struct nft_trans
*trans
;
3414 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
3415 nft_set_elem_policy
);
3419 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
3422 nft_set_ext_prepare(&tmpl
);
3424 err
= nft_setelem_parse_flags(set
, nla
[NFTA_SET_ELEM_FLAGS
], &flags
);
3428 nft_set_ext_add(&tmpl
, NFT_SET_EXT_FLAGS
);
3430 if (set
->flags
& NFT_SET_MAP
) {
3431 if (nla
[NFTA_SET_ELEM_DATA
] == NULL
&&
3432 !(flags
& NFT_SET_ELEM_INTERVAL_END
))
3434 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
&&
3435 flags
& NFT_SET_ELEM_INTERVAL_END
)
3438 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
3443 if (nla
[NFTA_SET_ELEM_TIMEOUT
] != NULL
) {
3444 if (!(set
->flags
& NFT_SET_TIMEOUT
))
3446 timeout
= be64_to_cpu(nla_get_be64(nla
[NFTA_SET_ELEM_TIMEOUT
]));
3447 } else if (set
->flags
& NFT_SET_TIMEOUT
) {
3448 timeout
= set
->timeout
;
3451 err
= nft_data_init(ctx
, &elem
.key
.val
, sizeof(elem
.key
), &d1
,
3452 nla
[NFTA_SET_ELEM_KEY
]);
3456 if (d1
.type
!= NFT_DATA_VALUE
|| d1
.len
!= set
->klen
)
3459 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_KEY
, d1
.len
);
3461 nft_set_ext_add(&tmpl
, NFT_SET_EXT_EXPIRATION
);
3462 if (timeout
!= set
->timeout
)
3463 nft_set_ext_add(&tmpl
, NFT_SET_EXT_TIMEOUT
);
3466 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
) {
3467 err
= nft_data_init(ctx
, &data
, sizeof(data
), &d2
,
3468 nla
[NFTA_SET_ELEM_DATA
]);
3473 if (set
->dtype
!= NFT_DATA_VERDICT
&& d2
.len
!= set
->dlen
)
3476 dreg
= nft_type_to_reg(set
->dtype
);
3477 list_for_each_entry(binding
, &set
->bindings
, list
) {
3478 struct nft_ctx bind_ctx
= {
3480 .table
= ctx
->table
,
3481 .chain
= (struct nft_chain
*)binding
->chain
,
3484 if (!(binding
->flags
& NFT_SET_MAP
))
3487 err
= nft_validate_register_store(&bind_ctx
, dreg
,
3494 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_DATA
, d2
.len
);
3497 /* The full maximum length of userdata can exceed the maximum
3498 * offset value (U8_MAX) for following extensions, therefor it
3499 * must be the last extension added.
3502 if (nla
[NFTA_SET_ELEM_USERDATA
] != NULL
) {
3503 ulen
= nla_len(nla
[NFTA_SET_ELEM_USERDATA
]);
3505 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_USERDATA
,
3510 elem
.priv
= nft_set_elem_init(set
, &tmpl
, elem
.key
.val
.data
, data
.data
,
3511 timeout
, GFP_KERNEL
);
3512 if (elem
.priv
== NULL
)
3515 ext
= nft_set_elem_ext(set
, elem
.priv
);
3517 *nft_set_ext_flags(ext
) = flags
;
3519 udata
= nft_set_ext_userdata(ext
);
3520 udata
->len
= ulen
- 1;
3521 nla_memcpy(&udata
->data
, nla
[NFTA_SET_ELEM_USERDATA
], ulen
);
3524 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_NEWSETELEM
, set
);
3528 ext
->genmask
= nft_genmask_cur(ctx
->net
) | NFT_SET_ELEM_BUSY_MASK
;
3529 err
= set
->ops
->insert(set
, &elem
);
3533 nft_trans_elem(trans
) = elem
;
3534 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
3542 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
3543 nft_data_uninit(&data
, d2
.type
);
3545 nft_data_uninit(&elem
.key
.val
, d1
.type
);
3550 static int nf_tables_newsetelem(struct net
*net
, struct sock
*nlsk
,
3551 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3552 const struct nlattr
* const nla
[])
3554 const struct nlattr
*attr
;
3555 struct nft_set
*set
;
3559 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
)
3562 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
);
3566 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3568 if (nla
[NFTA_SET_ELEM_LIST_SET_ID
]) {
3569 set
= nf_tables_set_lookup_byid(net
,
3570 nla
[NFTA_SET_ELEM_LIST_SET_ID
]);
3573 return PTR_ERR(set
);
3576 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
3579 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
3581 !atomic_add_unless(&set
->nelems
, 1, set
->size
+ set
->ndeact
))
3584 err
= nft_add_set_elem(&ctx
, set
, attr
);
3586 atomic_dec(&set
->nelems
);
3593 static int nft_del_setelem(struct nft_ctx
*ctx
, struct nft_set
*set
,
3594 const struct nlattr
*attr
)
3596 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
3597 struct nft_set_ext_tmpl tmpl
;
3598 struct nft_data_desc desc
;
3599 struct nft_set_elem elem
;
3600 struct nft_set_ext
*ext
;
3601 struct nft_trans
*trans
;
3606 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
3607 nft_set_elem_policy
);
3612 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
3615 nft_set_ext_prepare(&tmpl
);
3617 err
= nft_setelem_parse_flags(set
, nla
[NFTA_SET_ELEM_FLAGS
], &flags
);
3621 nft_set_ext_add(&tmpl
, NFT_SET_EXT_FLAGS
);
3623 err
= nft_data_init(ctx
, &elem
.key
.val
, sizeof(elem
.key
), &desc
,
3624 nla
[NFTA_SET_ELEM_KEY
]);
3629 if (desc
.type
!= NFT_DATA_VALUE
|| desc
.len
!= set
->klen
)
3632 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_KEY
, desc
.len
);
3635 elem
.priv
= nft_set_elem_init(set
, &tmpl
, elem
.key
.val
.data
, NULL
, 0,
3637 if (elem
.priv
== NULL
)
3640 ext
= nft_set_elem_ext(set
, elem
.priv
);
3642 *nft_set_ext_flags(ext
) = flags
;
3644 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_DELSETELEM
, set
);
3645 if (trans
== NULL
) {
3650 priv
= set
->ops
->deactivate(set
, &elem
);
3658 nft_trans_elem(trans
) = elem
;
3659 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
3667 nft_data_uninit(&elem
.key
.val
, desc
.type
);
3672 static int nf_tables_delsetelem(struct net
*net
, struct sock
*nlsk
,
3673 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3674 const struct nlattr
* const nla
[])
3676 const struct nlattr
*attr
;
3677 struct nft_set
*set
;
3681 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
)
3684 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
);
3688 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3690 return PTR_ERR(set
);
3691 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
3694 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
3695 err
= nft_del_setelem(&ctx
, set
, attr
);
3704 void nft_set_gc_batch_release(struct rcu_head
*rcu
)
3706 struct nft_set_gc_batch
*gcb
;
3709 gcb
= container_of(rcu
, struct nft_set_gc_batch
, head
.rcu
);
3710 for (i
= 0; i
< gcb
->head
.cnt
; i
++)
3711 nft_set_elem_destroy(gcb
->head
.set
, gcb
->elems
[i
]);
3714 EXPORT_SYMBOL_GPL(nft_set_gc_batch_release
);
3716 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
3719 struct nft_set_gc_batch
*gcb
;
3721 gcb
= kzalloc(sizeof(*gcb
), gfp
);
3724 gcb
->head
.set
= set
;
3727 EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc
);
3729 static int nf_tables_fill_gen_info(struct sk_buff
*skb
, struct net
*net
,
3730 u32 portid
, u32 seq
)
3732 struct nlmsghdr
*nlh
;
3733 struct nfgenmsg
*nfmsg
;
3734 int event
= (NFNL_SUBSYS_NFTABLES
<< 8) | NFT_MSG_NEWGEN
;
3736 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), 0);
3738 goto nla_put_failure
;
3740 nfmsg
= nlmsg_data(nlh
);
3741 nfmsg
->nfgen_family
= AF_UNSPEC
;
3742 nfmsg
->version
= NFNETLINK_V0
;
3743 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
3745 if (nla_put_be32(skb
, NFTA_GEN_ID
, htonl(net
->nft
.base_seq
)))
3746 goto nla_put_failure
;
3748 nlmsg_end(skb
, nlh
);
3752 nlmsg_trim(skb
, nlh
);
3756 static int nf_tables_gen_notify(struct net
*net
, struct sk_buff
*skb
, int event
)
3758 struct nlmsghdr
*nlh
= nlmsg_hdr(skb
);
3759 struct sk_buff
*skb2
;
3762 if (nlmsg_report(nlh
) &&
3763 !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
3767 skb2
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
3771 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
3778 err
= nfnetlink_send(skb2
, net
, NETLINK_CB(skb
).portid
,
3779 NFNLGRP_NFTABLES
, nlmsg_report(nlh
), GFP_KERNEL
);
3782 nfnetlink_set_err(net
, NETLINK_CB(skb
).portid
, NFNLGRP_NFTABLES
,
3788 static int nf_tables_getgen(struct net
*net
, struct sock
*nlsk
,
3789 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3790 const struct nlattr
* const nla
[])
3792 struct sk_buff
*skb2
;
3795 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
3799 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
3804 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
3810 static const struct nfnl_callback nf_tables_cb
[NFT_MSG_MAX
] = {
3811 [NFT_MSG_NEWTABLE
] = {
3812 .call_batch
= nf_tables_newtable
,
3813 .attr_count
= NFTA_TABLE_MAX
,
3814 .policy
= nft_table_policy
,
3816 [NFT_MSG_GETTABLE
] = {
3817 .call
= nf_tables_gettable
,
3818 .attr_count
= NFTA_TABLE_MAX
,
3819 .policy
= nft_table_policy
,
3821 [NFT_MSG_DELTABLE
] = {
3822 .call_batch
= nf_tables_deltable
,
3823 .attr_count
= NFTA_TABLE_MAX
,
3824 .policy
= nft_table_policy
,
3826 [NFT_MSG_NEWCHAIN
] = {
3827 .call_batch
= nf_tables_newchain
,
3828 .attr_count
= NFTA_CHAIN_MAX
,
3829 .policy
= nft_chain_policy
,
3831 [NFT_MSG_GETCHAIN
] = {
3832 .call
= nf_tables_getchain
,
3833 .attr_count
= NFTA_CHAIN_MAX
,
3834 .policy
= nft_chain_policy
,
3836 [NFT_MSG_DELCHAIN
] = {
3837 .call_batch
= nf_tables_delchain
,
3838 .attr_count
= NFTA_CHAIN_MAX
,
3839 .policy
= nft_chain_policy
,
3841 [NFT_MSG_NEWRULE
] = {
3842 .call_batch
= nf_tables_newrule
,
3843 .attr_count
= NFTA_RULE_MAX
,
3844 .policy
= nft_rule_policy
,
3846 [NFT_MSG_GETRULE
] = {
3847 .call
= nf_tables_getrule
,
3848 .attr_count
= NFTA_RULE_MAX
,
3849 .policy
= nft_rule_policy
,
3851 [NFT_MSG_DELRULE
] = {
3852 .call_batch
= nf_tables_delrule
,
3853 .attr_count
= NFTA_RULE_MAX
,
3854 .policy
= nft_rule_policy
,
3856 [NFT_MSG_NEWSET
] = {
3857 .call_batch
= nf_tables_newset
,
3858 .attr_count
= NFTA_SET_MAX
,
3859 .policy
= nft_set_policy
,
3861 [NFT_MSG_GETSET
] = {
3862 .call
= nf_tables_getset
,
3863 .attr_count
= NFTA_SET_MAX
,
3864 .policy
= nft_set_policy
,
3866 [NFT_MSG_DELSET
] = {
3867 .call_batch
= nf_tables_delset
,
3868 .attr_count
= NFTA_SET_MAX
,
3869 .policy
= nft_set_policy
,
3871 [NFT_MSG_NEWSETELEM
] = {
3872 .call_batch
= nf_tables_newsetelem
,
3873 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
3874 .policy
= nft_set_elem_list_policy
,
3876 [NFT_MSG_GETSETELEM
] = {
3877 .call
= nf_tables_getsetelem
,
3878 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
3879 .policy
= nft_set_elem_list_policy
,
3881 [NFT_MSG_DELSETELEM
] = {
3882 .call_batch
= nf_tables_delsetelem
,
3883 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
3884 .policy
= nft_set_elem_list_policy
,
3886 [NFT_MSG_GETGEN
] = {
3887 .call
= nf_tables_getgen
,
3891 static void nft_chain_commit_update(struct nft_trans
*trans
)
3893 struct nft_base_chain
*basechain
;
3895 if (nft_trans_chain_name(trans
)[0])
3896 strcpy(trans
->ctx
.chain
->name
, nft_trans_chain_name(trans
));
3898 if (!(trans
->ctx
.chain
->flags
& NFT_BASE_CHAIN
))
3901 basechain
= nft_base_chain(trans
->ctx
.chain
);
3902 nft_chain_stats_replace(basechain
, nft_trans_chain_stats(trans
));
3904 switch (nft_trans_chain_policy(trans
)) {
3907 basechain
->policy
= nft_trans_chain_policy(trans
);
3912 static void nf_tables_commit_release(struct nft_trans
*trans
)
3914 switch (trans
->msg_type
) {
3915 case NFT_MSG_DELTABLE
:
3916 nf_tables_table_destroy(&trans
->ctx
);
3918 case NFT_MSG_DELCHAIN
:
3919 nf_tables_chain_destroy(trans
->ctx
.chain
);
3921 case NFT_MSG_DELRULE
:
3922 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
3924 case NFT_MSG_DELSET
:
3925 nft_set_destroy(nft_trans_set(trans
));
3927 case NFT_MSG_DELSETELEM
:
3928 nft_set_elem_destroy(nft_trans_elem_set(trans
),
3929 nft_trans_elem(trans
).priv
);
3935 static int nf_tables_commit(struct net
*net
, struct sk_buff
*skb
)
3937 struct nft_trans
*trans
, *next
;
3938 struct nft_trans_elem
*te
;
3940 /* Bump generation counter, invalidate any dump in progress */
3941 while (++net
->nft
.base_seq
== 0);
3943 /* A new generation has just started */
3944 net
->nft
.gencursor
= nft_gencursor_next(net
);
3946 /* Make sure all packets have left the previous generation before
3947 * purging old rules.
3951 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
3952 switch (trans
->msg_type
) {
3953 case NFT_MSG_NEWTABLE
:
3954 if (nft_trans_table_update(trans
)) {
3955 if (!nft_trans_table_enable(trans
)) {
3956 nf_tables_table_disable(trans
->ctx
.afi
,
3958 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
3961 trans
->ctx
.table
->flags
&= ~NFT_TABLE_INACTIVE
;
3963 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_NEWTABLE
);
3964 nft_trans_destroy(trans
);
3966 case NFT_MSG_DELTABLE
:
3967 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_DELTABLE
);
3969 case NFT_MSG_NEWCHAIN
:
3970 if (nft_trans_chain_update(trans
))
3971 nft_chain_commit_update(trans
);
3973 trans
->ctx
.chain
->flags
&= ~NFT_CHAIN_INACTIVE
;
3975 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_NEWCHAIN
);
3976 nft_trans_destroy(trans
);
3978 case NFT_MSG_DELCHAIN
:
3979 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_DELCHAIN
);
3980 nf_tables_unregister_hooks(trans
->ctx
.table
,
3982 trans
->ctx
.afi
->nops
);
3984 case NFT_MSG_NEWRULE
:
3985 nft_rule_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
3986 nf_tables_rule_notify(&trans
->ctx
,
3987 nft_trans_rule(trans
),
3989 nft_trans_destroy(trans
);
3991 case NFT_MSG_DELRULE
:
3992 list_del_rcu(&nft_trans_rule(trans
)->list
);
3993 nf_tables_rule_notify(&trans
->ctx
,
3994 nft_trans_rule(trans
),
3997 case NFT_MSG_NEWSET
:
3998 nft_trans_set(trans
)->flags
&= ~NFT_SET_INACTIVE
;
3999 /* This avoids hitting -EBUSY when deleting the table
4000 * from the transaction.
4002 if (nft_trans_set(trans
)->flags
& NFT_SET_ANONYMOUS
&&
4003 !list_empty(&nft_trans_set(trans
)->bindings
))
4004 trans
->ctx
.table
->use
--;
4006 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
4007 NFT_MSG_NEWSET
, GFP_KERNEL
);
4008 nft_trans_destroy(trans
);
4010 case NFT_MSG_DELSET
:
4011 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
4012 NFT_MSG_DELSET
, GFP_KERNEL
);
4014 case NFT_MSG_NEWSETELEM
:
4015 te
= (struct nft_trans_elem
*)trans
->data
;
4017 te
->set
->ops
->activate(te
->set
, &te
->elem
);
4018 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
4020 NFT_MSG_NEWSETELEM
, 0);
4021 nft_trans_destroy(trans
);
4023 case NFT_MSG_DELSETELEM
:
4024 te
= (struct nft_trans_elem
*)trans
->data
;
4026 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
4028 NFT_MSG_DELSETELEM
, 0);
4029 te
->set
->ops
->remove(te
->set
, &te
->elem
);
4030 atomic_dec(&te
->set
->nelems
);
4038 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
4039 list_del(&trans
->list
);
4040 nf_tables_commit_release(trans
);
4043 nf_tables_gen_notify(net
, skb
, NFT_MSG_NEWGEN
);
4048 static void nf_tables_abort_release(struct nft_trans
*trans
)
4050 switch (trans
->msg_type
) {
4051 case NFT_MSG_NEWTABLE
:
4052 nf_tables_table_destroy(&trans
->ctx
);
4054 case NFT_MSG_NEWCHAIN
:
4055 nf_tables_chain_destroy(trans
->ctx
.chain
);
4057 case NFT_MSG_NEWRULE
:
4058 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
4060 case NFT_MSG_NEWSET
:
4061 nft_set_destroy(nft_trans_set(trans
));
4063 case NFT_MSG_NEWSETELEM
:
4064 nft_set_elem_destroy(nft_trans_elem_set(trans
),
4065 nft_trans_elem(trans
).priv
);
4071 static int nf_tables_abort(struct net
*net
, struct sk_buff
*skb
)
4073 struct nft_trans
*trans
, *next
;
4074 struct nft_trans_elem
*te
;
4076 list_for_each_entry_safe_reverse(trans
, next
, &net
->nft
.commit_list
,
4078 switch (trans
->msg_type
) {
4079 case NFT_MSG_NEWTABLE
:
4080 if (nft_trans_table_update(trans
)) {
4081 if (nft_trans_table_enable(trans
)) {
4082 nf_tables_table_disable(trans
->ctx
.afi
,
4084 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
4086 nft_trans_destroy(trans
);
4088 list_del_rcu(&trans
->ctx
.table
->list
);
4091 case NFT_MSG_DELTABLE
:
4092 list_add_tail_rcu(&trans
->ctx
.table
->list
,
4093 &trans
->ctx
.afi
->tables
);
4094 nft_trans_destroy(trans
);
4096 case NFT_MSG_NEWCHAIN
:
4097 if (nft_trans_chain_update(trans
)) {
4098 free_percpu(nft_trans_chain_stats(trans
));
4100 nft_trans_destroy(trans
);
4102 trans
->ctx
.table
->use
--;
4103 list_del_rcu(&trans
->ctx
.chain
->list
);
4104 nf_tables_unregister_hooks(trans
->ctx
.table
,
4106 trans
->ctx
.afi
->nops
);
4109 case NFT_MSG_DELCHAIN
:
4110 trans
->ctx
.table
->use
++;
4111 list_add_tail_rcu(&trans
->ctx
.chain
->list
,
4112 &trans
->ctx
.table
->chains
);
4113 nft_trans_destroy(trans
);
4115 case NFT_MSG_NEWRULE
:
4116 trans
->ctx
.chain
->use
--;
4117 list_del_rcu(&nft_trans_rule(trans
)->list
);
4119 case NFT_MSG_DELRULE
:
4120 trans
->ctx
.chain
->use
++;
4121 nft_rule_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
4122 nft_trans_destroy(trans
);
4124 case NFT_MSG_NEWSET
:
4125 trans
->ctx
.table
->use
--;
4126 list_del_rcu(&nft_trans_set(trans
)->list
);
4128 case NFT_MSG_DELSET
:
4129 trans
->ctx
.table
->use
++;
4130 list_add_tail_rcu(&nft_trans_set(trans
)->list
,
4131 &trans
->ctx
.table
->sets
);
4132 nft_trans_destroy(trans
);
4134 case NFT_MSG_NEWSETELEM
:
4135 te
= (struct nft_trans_elem
*)trans
->data
;
4137 te
->set
->ops
->remove(te
->set
, &te
->elem
);
4138 atomic_dec(&te
->set
->nelems
);
4140 case NFT_MSG_DELSETELEM
:
4141 te
= (struct nft_trans_elem
*)trans
->data
;
4143 te
->set
->ops
->activate(te
->set
, &te
->elem
);
4146 nft_trans_destroy(trans
);
4153 list_for_each_entry_safe_reverse(trans
, next
,
4154 &net
->nft
.commit_list
, list
) {
4155 list_del(&trans
->list
);
4156 nf_tables_abort_release(trans
);
4162 static const struct nfnetlink_subsystem nf_tables_subsys
= {
4163 .name
= "nf_tables",
4164 .subsys_id
= NFNL_SUBSYS_NFTABLES
,
4165 .cb_count
= NFT_MSG_MAX
,
4167 .commit
= nf_tables_commit
,
4168 .abort
= nf_tables_abort
,
4171 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
4172 enum nft_chain_type type
)
4174 const struct nft_base_chain
*basechain
;
4176 if (chain
->flags
& NFT_BASE_CHAIN
) {
4177 basechain
= nft_base_chain(chain
);
4178 if (basechain
->type
->type
!= type
)
4183 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency
);
4185 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
4186 unsigned int hook_flags
)
4188 struct nft_base_chain
*basechain
;
4190 if (chain
->flags
& NFT_BASE_CHAIN
) {
4191 basechain
= nft_base_chain(chain
);
4193 if ((1 << basechain
->ops
[0].hooknum
) & hook_flags
)
4201 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks
);
4204 * Loop detection - walk through the ruleset beginning at the destination chain
4205 * of a new jump until either the source chain is reached (loop) or all
4206 * reachable chains have been traversed.
4208 * The loop check is performed whenever a new jump verdict is added to an
4209 * expression or verdict map or a verdict map is bound to a new chain.
4212 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
4213 const struct nft_chain
*chain
);
4215 static int nf_tables_loop_check_setelem(const struct nft_ctx
*ctx
,
4216 const struct nft_set
*set
,
4217 const struct nft_set_iter
*iter
,
4218 const struct nft_set_elem
*elem
)
4220 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
4221 const struct nft_data
*data
;
4223 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
4224 *nft_set_ext_flags(ext
) & NFT_SET_ELEM_INTERVAL_END
)
4227 data
= nft_set_ext_data(ext
);
4228 switch (data
->verdict
.code
) {
4231 return nf_tables_check_loops(ctx
, data
->verdict
.chain
);
4237 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
4238 const struct nft_chain
*chain
)
4240 const struct nft_rule
*rule
;
4241 const struct nft_expr
*expr
, *last
;
4242 const struct nft_set
*set
;
4243 struct nft_set_binding
*binding
;
4244 struct nft_set_iter iter
;
4246 if (ctx
->chain
== chain
)
4249 list_for_each_entry(rule
, &chain
->rules
, list
) {
4250 nft_rule_for_each_expr(expr
, last
, rule
) {
4251 const struct nft_data
*data
= NULL
;
4254 if (!expr
->ops
->validate
)
4257 err
= expr
->ops
->validate(ctx
, expr
, &data
);
4264 switch (data
->verdict
.code
) {
4267 err
= nf_tables_check_loops(ctx
,
4268 data
->verdict
.chain
);
4277 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
4278 if (!(set
->flags
& NFT_SET_MAP
) ||
4279 set
->dtype
!= NFT_DATA_VERDICT
)
4282 list_for_each_entry(binding
, &set
->bindings
, list
) {
4283 if (!(binding
->flags
& NFT_SET_MAP
) ||
4284 binding
->chain
!= chain
)
4290 iter
.fn
= nf_tables_loop_check_setelem
;
4292 set
->ops
->walk(ctx
, set
, &iter
);
4302 * nft_parse_register - parse a register value from a netlink attribute
4304 * @attr: netlink attribute
4306 * Parse and translate a register value from a netlink attribute.
4307 * Registers used to be 128 bit wide, these register numbers will be
4308 * mapped to the corresponding 32 bit register numbers.
4310 unsigned int nft_parse_register(const struct nlattr
*attr
)
4314 reg
= ntohl(nla_get_be32(attr
));
4316 case NFT_REG_VERDICT
...NFT_REG_4
:
4317 return reg
* NFT_REG_SIZE
/ NFT_REG32_SIZE
;
4319 return reg
+ NFT_REG_SIZE
/ NFT_REG32_SIZE
- NFT_REG32_00
;
4322 EXPORT_SYMBOL_GPL(nft_parse_register
);
4325 * nft_dump_register - dump a register value to a netlink attribute
4327 * @skb: socket buffer
4328 * @attr: attribute number
4329 * @reg: register number
4331 * Construct a netlink attribute containing the register number. For
4332 * compatibility reasons, register numbers being a multiple of 4 are
4333 * translated to the corresponding 128 bit register numbers.
4335 int nft_dump_register(struct sk_buff
*skb
, unsigned int attr
, unsigned int reg
)
4337 if (reg
% (NFT_REG_SIZE
/ NFT_REG32_SIZE
) == 0)
4338 reg
= reg
/ (NFT_REG_SIZE
/ NFT_REG32_SIZE
);
4340 reg
= reg
- NFT_REG_SIZE
/ NFT_REG32_SIZE
+ NFT_REG32_00
;
4342 return nla_put_be32(skb
, attr
, htonl(reg
));
4344 EXPORT_SYMBOL_GPL(nft_dump_register
);
4347 * nft_validate_register_load - validate a load from a register
4349 * @reg: the register number
4350 * @len: the length of the data
4352 * Validate that the input register is one of the general purpose
4353 * registers and that the length of the load is within the bounds.
4355 int nft_validate_register_load(enum nft_registers reg
, unsigned int len
)
4357 if (reg
< NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
)
4361 if (reg
* NFT_REG32_SIZE
+ len
> FIELD_SIZEOF(struct nft_regs
, data
))
4366 EXPORT_SYMBOL_GPL(nft_validate_register_load
);
4369 * nft_validate_register_store - validate an expressions' register store
4371 * @ctx: context of the expression performing the load
4372 * @reg: the destination register number
4373 * @data: the data to load
4374 * @type: the data type
4375 * @len: the length of the data
4377 * Validate that a data load uses the appropriate data type for
4378 * the destination register and the length is within the bounds.
4379 * A value of NULL for the data means that its runtime gathered
4382 int nft_validate_register_store(const struct nft_ctx
*ctx
,
4383 enum nft_registers reg
,
4384 const struct nft_data
*data
,
4385 enum nft_data_types type
, unsigned int len
)
4390 case NFT_REG_VERDICT
:
4391 if (type
!= NFT_DATA_VERDICT
)
4395 (data
->verdict
.code
== NFT_GOTO
||
4396 data
->verdict
.code
== NFT_JUMP
)) {
4397 err
= nf_tables_check_loops(ctx
, data
->verdict
.chain
);
4401 if (ctx
->chain
->level
+ 1 >
4402 data
->verdict
.chain
->level
) {
4403 if (ctx
->chain
->level
+ 1 == NFT_JUMP_STACK_SIZE
)
4405 data
->verdict
.chain
->level
= ctx
->chain
->level
+ 1;
4411 if (reg
< NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
)
4415 if (reg
* NFT_REG32_SIZE
+ len
>
4416 FIELD_SIZEOF(struct nft_regs
, data
))
4419 if (data
!= NULL
&& type
!= NFT_DATA_VALUE
)
4424 EXPORT_SYMBOL_GPL(nft_validate_register_store
);
4426 static const struct nla_policy nft_verdict_policy
[NFTA_VERDICT_MAX
+ 1] = {
4427 [NFTA_VERDICT_CODE
] = { .type
= NLA_U32
},
4428 [NFTA_VERDICT_CHAIN
] = { .type
= NLA_STRING
,
4429 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
4432 static int nft_verdict_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
4433 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
4435 struct nlattr
*tb
[NFTA_VERDICT_MAX
+ 1];
4436 struct nft_chain
*chain
;
4439 err
= nla_parse_nested(tb
, NFTA_VERDICT_MAX
, nla
, nft_verdict_policy
);
4443 if (!tb
[NFTA_VERDICT_CODE
])
4445 data
->verdict
.code
= ntohl(nla_get_be32(tb
[NFTA_VERDICT_CODE
]));
4447 switch (data
->verdict
.code
) {
4449 switch (data
->verdict
.code
& NF_VERDICT_MASK
) {
4464 if (!tb
[NFTA_VERDICT_CHAIN
])
4466 chain
= nf_tables_chain_lookup(ctx
->table
,
4467 tb
[NFTA_VERDICT_CHAIN
]);
4469 return PTR_ERR(chain
);
4470 if (chain
->flags
& NFT_BASE_CHAIN
)
4474 data
->verdict
.chain
= chain
;
4478 desc
->len
= sizeof(data
->verdict
);
4479 desc
->type
= NFT_DATA_VERDICT
;
4483 static void nft_verdict_uninit(const struct nft_data
*data
)
4485 switch (data
->verdict
.code
) {
4488 data
->verdict
.chain
->use
--;
4493 int nft_verdict_dump(struct sk_buff
*skb
, int type
, const struct nft_verdict
*v
)
4495 struct nlattr
*nest
;
4497 nest
= nla_nest_start(skb
, type
);
4499 goto nla_put_failure
;
4501 if (nla_put_be32(skb
, NFTA_VERDICT_CODE
, htonl(v
->code
)))
4502 goto nla_put_failure
;
4507 if (nla_put_string(skb
, NFTA_VERDICT_CHAIN
,
4509 goto nla_put_failure
;
4511 nla_nest_end(skb
, nest
);
4518 static int nft_value_init(const struct nft_ctx
*ctx
,
4519 struct nft_data
*data
, unsigned int size
,
4520 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
4530 nla_memcpy(data
->data
, nla
, len
);
4531 desc
->type
= NFT_DATA_VALUE
;
4536 static int nft_value_dump(struct sk_buff
*skb
, const struct nft_data
*data
,
4539 return nla_put(skb
, NFTA_DATA_VALUE
, len
, data
->data
);
4542 static const struct nla_policy nft_data_policy
[NFTA_DATA_MAX
+ 1] = {
4543 [NFTA_DATA_VALUE
] = { .type
= NLA_BINARY
},
4544 [NFTA_DATA_VERDICT
] = { .type
= NLA_NESTED
},
4548 * nft_data_init - parse nf_tables data netlink attributes
4550 * @ctx: context of the expression using the data
4551 * @data: destination struct nft_data
4552 * @size: maximum data length
4553 * @desc: data description
4554 * @nla: netlink attribute containing data
4556 * Parse the netlink data attributes and initialize a struct nft_data.
4557 * The type and length of data are returned in the data description.
4559 * The caller can indicate that it only wants to accept data of type
4560 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4562 int nft_data_init(const struct nft_ctx
*ctx
,
4563 struct nft_data
*data
, unsigned int size
,
4564 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
4566 struct nlattr
*tb
[NFTA_DATA_MAX
+ 1];
4569 err
= nla_parse_nested(tb
, NFTA_DATA_MAX
, nla
, nft_data_policy
);
4573 if (tb
[NFTA_DATA_VALUE
])
4574 return nft_value_init(ctx
, data
, size
, desc
,
4575 tb
[NFTA_DATA_VALUE
]);
4576 if (tb
[NFTA_DATA_VERDICT
] && ctx
!= NULL
)
4577 return nft_verdict_init(ctx
, data
, desc
, tb
[NFTA_DATA_VERDICT
]);
4580 EXPORT_SYMBOL_GPL(nft_data_init
);
4583 * nft_data_uninit - release a nft_data item
4585 * @data: struct nft_data to release
4586 * @type: type of data
4588 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4589 * all others need to be released by calling this function.
4591 void nft_data_uninit(const struct nft_data
*data
, enum nft_data_types type
)
4593 if (type
< NFT_DATA_VERDICT
)
4596 case NFT_DATA_VERDICT
:
4597 return nft_verdict_uninit(data
);
4602 EXPORT_SYMBOL_GPL(nft_data_uninit
);
4604 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
4605 enum nft_data_types type
, unsigned int len
)
4607 struct nlattr
*nest
;
4610 nest
= nla_nest_start(skb
, attr
);
4615 case NFT_DATA_VALUE
:
4616 err
= nft_value_dump(skb
, data
, len
);
4618 case NFT_DATA_VERDICT
:
4619 err
= nft_verdict_dump(skb
, NFTA_DATA_VERDICT
, &data
->verdict
);
4626 nla_nest_end(skb
, nest
);
4629 EXPORT_SYMBOL_GPL(nft_data_dump
);
4631 static int __net_init
nf_tables_init_net(struct net
*net
)
4633 INIT_LIST_HEAD(&net
->nft
.af_info
);
4634 INIT_LIST_HEAD(&net
->nft
.commit_list
);
4635 net
->nft
.base_seq
= 1;
4639 int __nft_release_basechain(struct nft_ctx
*ctx
)
4641 struct nft_rule
*rule
, *nr
;
4643 BUG_ON(!(ctx
->chain
->flags
& NFT_BASE_CHAIN
));
4645 nf_tables_unregister_hooks(ctx
->chain
->table
, ctx
->chain
,
4647 list_for_each_entry_safe(rule
, nr
, &ctx
->chain
->rules
, list
) {
4648 list_del(&rule
->list
);
4650 nf_tables_rule_destroy(ctx
, rule
);
4652 list_del(&ctx
->chain
->list
);
4654 nf_tables_chain_destroy(ctx
->chain
);
4658 EXPORT_SYMBOL_GPL(__nft_release_basechain
);
4660 /* Called by nft_unregister_afinfo() from __net_exit path, nfnl_lock is held. */
4661 static void __nft_release_afinfo(struct net
*net
, struct nft_af_info
*afi
)
4663 struct nft_table
*table
, *nt
;
4664 struct nft_chain
*chain
, *nc
;
4665 struct nft_rule
*rule
, *nr
;
4666 struct nft_set
*set
, *ns
;
4667 struct nft_ctx ctx
= {
4672 list_for_each_entry_safe(table
, nt
, &afi
->tables
, list
) {
4673 list_for_each_entry(chain
, &table
->chains
, list
)
4674 nf_tables_unregister_hooks(table
, chain
, afi
->nops
);
4675 /* No packets are walking on these chains anymore. */
4677 list_for_each_entry(chain
, &table
->chains
, list
) {
4679 list_for_each_entry_safe(rule
, nr
, &chain
->rules
, list
) {
4680 list_del(&rule
->list
);
4682 nf_tables_rule_destroy(&ctx
, rule
);
4685 list_for_each_entry_safe(set
, ns
, &table
->sets
, list
) {
4686 list_del(&set
->list
);
4688 nft_set_destroy(set
);
4690 list_for_each_entry_safe(chain
, nc
, &table
->chains
, list
) {
4691 list_del(&chain
->list
);
4693 nf_tables_chain_destroy(chain
);
4695 list_del(&table
->list
);
4696 nf_tables_table_destroy(&ctx
);
4700 static struct pernet_operations nf_tables_net_ops
= {
4701 .init
= nf_tables_init_net
,
4704 static int __init
nf_tables_module_init(void)
4708 info
= kmalloc(sizeof(struct nft_expr_info
) * NFT_RULE_MAXEXPRS
,
4715 err
= nf_tables_core_module_init();
4719 err
= nfnetlink_subsys_register(&nf_tables_subsys
);
4723 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
4724 return register_pernet_subsys(&nf_tables_net_ops
);
4726 nf_tables_core_module_exit();
4733 static void __exit
nf_tables_module_exit(void)
4735 unregister_pernet_subsys(&nf_tables_net_ops
);
4736 nfnetlink_subsys_unregister(&nf_tables_subsys
);
4738 nf_tables_core_module_exit();
4742 module_init(nf_tables_module_init
);
4743 module_exit(nf_tables_module_exit
);
4745 MODULE_LICENSE("GPL");
4746 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4747 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES
);