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
);
45 * nft_unregister_afinfo - unregister nf_tables address family info
47 * @afi: address family info to unregister
49 * Unregister the address family for use with nf_tables.
51 void nft_unregister_afinfo(struct nft_af_info
*afi
)
53 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
54 list_del_rcu(&afi
->list
);
55 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
57 EXPORT_SYMBOL_GPL(nft_unregister_afinfo
);
59 static struct nft_af_info
*nft_afinfo_lookup(struct net
*net
, int family
)
61 struct nft_af_info
*afi
;
63 list_for_each_entry(afi
, &net
->nft
.af_info
, list
) {
64 if (afi
->family
== family
)
70 static struct nft_af_info
*
71 nf_tables_afinfo_lookup(struct net
*net
, int family
, bool autoload
)
73 struct nft_af_info
*afi
;
75 afi
= nft_afinfo_lookup(net
, family
);
80 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
81 request_module("nft-afinfo-%u", family
);
82 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
83 afi
= nft_afinfo_lookup(net
, family
);
85 return ERR_PTR(-EAGAIN
);
88 return ERR_PTR(-EAFNOSUPPORT
);
91 static void nft_ctx_init(struct nft_ctx
*ctx
,
93 const struct sk_buff
*skb
,
94 const struct nlmsghdr
*nlh
,
95 struct nft_af_info
*afi
,
96 struct nft_table
*table
,
97 struct nft_chain
*chain
,
98 const struct nlattr
* const *nla
)
105 ctx
->portid
= NETLINK_CB(skb
).portid
;
106 ctx
->report
= nlmsg_report(nlh
);
107 ctx
->seq
= nlh
->nlmsg_seq
;
110 static struct nft_trans
*nft_trans_alloc(struct nft_ctx
*ctx
, int msg_type
,
113 struct nft_trans
*trans
;
115 trans
= kzalloc(sizeof(struct nft_trans
) + size
, GFP_KERNEL
);
119 trans
->msg_type
= msg_type
;
125 static void nft_trans_destroy(struct nft_trans
*trans
)
127 list_del(&trans
->list
);
131 int nft_register_basechain(struct nft_base_chain
*basechain
,
132 unsigned int hook_nops
)
134 struct net
*net
= read_pnet(&basechain
->pnet
);
136 if (basechain
->flags
& NFT_BASECHAIN_DISABLED
)
139 return nf_register_net_hooks(net
, basechain
->ops
, hook_nops
);
141 EXPORT_SYMBOL_GPL(nft_register_basechain
);
143 void nft_unregister_basechain(struct nft_base_chain
*basechain
,
144 unsigned int hook_nops
)
146 struct net
*net
= read_pnet(&basechain
->pnet
);
148 if (basechain
->flags
& NFT_BASECHAIN_DISABLED
)
151 nf_unregister_net_hooks(net
, basechain
->ops
, hook_nops
);
153 EXPORT_SYMBOL_GPL(nft_unregister_basechain
);
155 static int nf_tables_register_hooks(const struct nft_table
*table
,
156 struct nft_chain
*chain
,
157 unsigned int hook_nops
)
159 if (table
->flags
& NFT_TABLE_F_DORMANT
||
160 !(chain
->flags
& NFT_BASE_CHAIN
))
163 return nft_register_basechain(nft_base_chain(chain
), hook_nops
);
166 static void nf_tables_unregister_hooks(const struct nft_table
*table
,
167 struct nft_chain
*chain
,
168 unsigned int hook_nops
)
170 if (table
->flags
& NFT_TABLE_F_DORMANT
||
171 !(chain
->flags
& NFT_BASE_CHAIN
))
174 nft_unregister_basechain(nft_base_chain(chain
), hook_nops
);
177 /* Internal table flags */
178 #define NFT_TABLE_INACTIVE (1 << 15)
180 static int nft_trans_table_add(struct nft_ctx
*ctx
, int msg_type
)
182 struct nft_trans
*trans
;
184 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_table
));
188 if (msg_type
== NFT_MSG_NEWTABLE
)
189 ctx
->table
->flags
|= NFT_TABLE_INACTIVE
;
191 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
195 static int nft_deltable(struct nft_ctx
*ctx
)
199 err
= nft_trans_table_add(ctx
, NFT_MSG_DELTABLE
);
203 list_del_rcu(&ctx
->table
->list
);
207 static int nft_trans_chain_add(struct nft_ctx
*ctx
, int msg_type
)
209 struct nft_trans
*trans
;
211 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_chain
));
215 if (msg_type
== NFT_MSG_NEWCHAIN
)
216 ctx
->chain
->flags
|= NFT_CHAIN_INACTIVE
;
218 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
222 static int nft_delchain(struct nft_ctx
*ctx
)
226 err
= nft_trans_chain_add(ctx
, NFT_MSG_DELCHAIN
);
231 list_del_rcu(&ctx
->chain
->list
);
237 nft_rule_is_active(struct net
*net
, const struct nft_rule
*rule
)
239 return (rule
->genmask
& nft_genmask_cur(net
)) == 0;
243 nft_rule_is_active_next(struct net
*net
, const struct nft_rule
*rule
)
245 return (rule
->genmask
& nft_genmask_next(net
)) == 0;
249 nft_rule_activate_next(struct net
*net
, struct nft_rule
*rule
)
251 /* Now inactive, will be active in the future */
252 rule
->genmask
= nft_genmask_cur(net
);
256 nft_rule_deactivate_next(struct net
*net
, struct nft_rule
*rule
)
258 rule
->genmask
= nft_genmask_next(net
);
261 static inline void nft_rule_clear(struct net
*net
, struct nft_rule
*rule
)
263 rule
->genmask
&= ~nft_genmask_next(net
);
267 nf_tables_delrule_deactivate(struct nft_ctx
*ctx
, struct nft_rule
*rule
)
269 /* You cannot delete the same rule twice */
270 if (nft_rule_is_active_next(ctx
->net
, rule
)) {
271 nft_rule_deactivate_next(ctx
->net
, rule
);
278 static struct nft_trans
*nft_trans_rule_add(struct nft_ctx
*ctx
, int msg_type
,
279 struct nft_rule
*rule
)
281 struct nft_trans
*trans
;
283 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_rule
));
287 nft_trans_rule(trans
) = rule
;
288 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
293 static int nft_delrule(struct nft_ctx
*ctx
, struct nft_rule
*rule
)
295 struct nft_trans
*trans
;
298 trans
= nft_trans_rule_add(ctx
, NFT_MSG_DELRULE
, rule
);
302 err
= nf_tables_delrule_deactivate(ctx
, rule
);
304 nft_trans_destroy(trans
);
311 static int nft_delrule_by_chain(struct nft_ctx
*ctx
)
313 struct nft_rule
*rule
;
316 list_for_each_entry(rule
, &ctx
->chain
->rules
, list
) {
317 err
= nft_delrule(ctx
, rule
);
324 /* Internal set flag */
325 #define NFT_SET_INACTIVE (1 << 15)
327 static int nft_trans_set_add(struct nft_ctx
*ctx
, int msg_type
,
330 struct nft_trans
*trans
;
332 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_set
));
336 if (msg_type
== NFT_MSG_NEWSET
&& ctx
->nla
[NFTA_SET_ID
] != NULL
) {
337 nft_trans_set_id(trans
) =
338 ntohl(nla_get_be32(ctx
->nla
[NFTA_SET_ID
]));
339 set
->flags
|= NFT_SET_INACTIVE
;
341 nft_trans_set(trans
) = set
;
342 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
347 static int nft_delset(struct nft_ctx
*ctx
, struct nft_set
*set
)
351 err
= nft_trans_set_add(ctx
, NFT_MSG_DELSET
, set
);
355 list_del_rcu(&set
->list
);
365 static struct nft_table
*nft_table_lookup(const struct nft_af_info
*afi
,
366 const struct nlattr
*nla
)
368 struct nft_table
*table
;
370 list_for_each_entry(table
, &afi
->tables
, list
) {
371 if (!nla_strcmp(nla
, table
->name
))
377 static struct nft_table
*nf_tables_table_lookup(const struct nft_af_info
*afi
,
378 const struct nlattr
*nla
)
380 struct nft_table
*table
;
383 return ERR_PTR(-EINVAL
);
385 table
= nft_table_lookup(afi
, nla
);
389 return ERR_PTR(-ENOENT
);
392 static inline u64
nf_tables_alloc_handle(struct nft_table
*table
)
394 return ++table
->hgenerator
;
397 static const struct nf_chain_type
*chain_type
[AF_MAX
][NFT_CHAIN_T_MAX
];
399 static const struct nf_chain_type
*
400 __nf_tables_chain_type_lookup(int family
, const struct nlattr
*nla
)
404 for (i
= 0; i
< NFT_CHAIN_T_MAX
; i
++) {
405 if (chain_type
[family
][i
] != NULL
&&
406 !nla_strcmp(nla
, chain_type
[family
][i
]->name
))
407 return chain_type
[family
][i
];
412 static const struct nf_chain_type
*
413 nf_tables_chain_type_lookup(const struct nft_af_info
*afi
,
414 const struct nlattr
*nla
,
417 const struct nf_chain_type
*type
;
419 type
= __nf_tables_chain_type_lookup(afi
->family
, nla
);
422 #ifdef CONFIG_MODULES
424 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
425 request_module("nft-chain-%u-%.*s", afi
->family
,
426 nla_len(nla
), (const char *)nla_data(nla
));
427 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
428 type
= __nf_tables_chain_type_lookup(afi
->family
, nla
);
430 return ERR_PTR(-EAGAIN
);
433 return ERR_PTR(-ENOENT
);
436 static const struct nla_policy nft_table_policy
[NFTA_TABLE_MAX
+ 1] = {
437 [NFTA_TABLE_NAME
] = { .type
= NLA_STRING
,
438 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
439 [NFTA_TABLE_FLAGS
] = { .type
= NLA_U32
},
442 static int nf_tables_fill_table_info(struct sk_buff
*skb
, struct net
*net
,
443 u32 portid
, u32 seq
, int event
, u32 flags
,
444 int family
, const struct nft_table
*table
)
446 struct nlmsghdr
*nlh
;
447 struct nfgenmsg
*nfmsg
;
449 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
450 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
452 goto nla_put_failure
;
454 nfmsg
= nlmsg_data(nlh
);
455 nfmsg
->nfgen_family
= family
;
456 nfmsg
->version
= NFNETLINK_V0
;
457 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
459 if (nla_put_string(skb
, NFTA_TABLE_NAME
, table
->name
) ||
460 nla_put_be32(skb
, NFTA_TABLE_FLAGS
, htonl(table
->flags
)) ||
461 nla_put_be32(skb
, NFTA_TABLE_USE
, htonl(table
->use
)))
462 goto nla_put_failure
;
468 nlmsg_trim(skb
, nlh
);
472 static int nf_tables_table_notify(const struct nft_ctx
*ctx
, int event
)
478 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
482 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
486 err
= nf_tables_fill_table_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
487 event
, 0, ctx
->afi
->family
, ctx
->table
);
493 err
= nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
494 ctx
->report
, GFP_KERNEL
);
497 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
503 static int nf_tables_dump_tables(struct sk_buff
*skb
,
504 struct netlink_callback
*cb
)
506 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
507 const struct nft_af_info
*afi
;
508 const struct nft_table
*table
;
509 unsigned int idx
= 0, s_idx
= cb
->args
[0];
510 struct net
*net
= sock_net(skb
->sk
);
511 int family
= nfmsg
->nfgen_family
;
514 cb
->seq
= net
->nft
.base_seq
;
516 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
517 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
520 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
524 memset(&cb
->args
[1], 0,
525 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
526 if (nf_tables_fill_table_info(skb
, net
,
527 NETLINK_CB(cb
->skb
).portid
,
531 afi
->family
, table
) < 0)
534 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
545 static int nf_tables_gettable(struct sock
*nlsk
, struct sk_buff
*skb
,
546 const struct nlmsghdr
*nlh
,
547 const struct nlattr
* const nla
[])
549 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
550 const struct nft_af_info
*afi
;
551 const struct nft_table
*table
;
552 struct sk_buff
*skb2
;
553 struct net
*net
= sock_net(skb
->sk
);
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
);
834 if (table
->flags
& NFT_TABLE_INACTIVE
)
840 return nft_flush_table(&ctx
);
843 static void nf_tables_table_destroy(struct nft_ctx
*ctx
)
845 BUG_ON(ctx
->table
->use
> 0);
848 module_put(ctx
->afi
->owner
);
851 int nft_register_chain_type(const struct nf_chain_type
*ctype
)
855 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
856 if (chain_type
[ctype
->family
][ctype
->type
] != NULL
) {
860 chain_type
[ctype
->family
][ctype
->type
] = ctype
;
862 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
865 EXPORT_SYMBOL_GPL(nft_register_chain_type
);
867 void nft_unregister_chain_type(const struct nf_chain_type
*ctype
)
869 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
870 chain_type
[ctype
->family
][ctype
->type
] = NULL
;
871 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
873 EXPORT_SYMBOL_GPL(nft_unregister_chain_type
);
879 static struct nft_chain
*
880 nf_tables_chain_lookup_byhandle(const struct nft_table
*table
, u64 handle
)
882 struct nft_chain
*chain
;
884 list_for_each_entry(chain
, &table
->chains
, list
) {
885 if (chain
->handle
== handle
)
889 return ERR_PTR(-ENOENT
);
892 static struct nft_chain
*nf_tables_chain_lookup(const struct nft_table
*table
,
893 const struct nlattr
*nla
)
895 struct nft_chain
*chain
;
898 return ERR_PTR(-EINVAL
);
900 list_for_each_entry(chain
, &table
->chains
, list
) {
901 if (!nla_strcmp(nla
, chain
->name
))
905 return ERR_PTR(-ENOENT
);
908 static const struct nla_policy nft_chain_policy
[NFTA_CHAIN_MAX
+ 1] = {
909 [NFTA_CHAIN_TABLE
] = { .type
= NLA_STRING
},
910 [NFTA_CHAIN_HANDLE
] = { .type
= NLA_U64
},
911 [NFTA_CHAIN_NAME
] = { .type
= NLA_STRING
,
912 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
913 [NFTA_CHAIN_HOOK
] = { .type
= NLA_NESTED
},
914 [NFTA_CHAIN_POLICY
] = { .type
= NLA_U32
},
915 [NFTA_CHAIN_TYPE
] = { .type
= NLA_STRING
},
916 [NFTA_CHAIN_COUNTERS
] = { .type
= NLA_NESTED
},
919 static const struct nla_policy nft_hook_policy
[NFTA_HOOK_MAX
+ 1] = {
920 [NFTA_HOOK_HOOKNUM
] = { .type
= NLA_U32
},
921 [NFTA_HOOK_PRIORITY
] = { .type
= NLA_U32
},
922 [NFTA_HOOK_DEV
] = { .type
= NLA_STRING
,
923 .len
= IFNAMSIZ
- 1 },
926 static int nft_dump_stats(struct sk_buff
*skb
, struct nft_stats __percpu
*stats
)
928 struct nft_stats
*cpu_stats
, total
;
934 memset(&total
, 0, sizeof(total
));
935 for_each_possible_cpu(cpu
) {
936 cpu_stats
= per_cpu_ptr(stats
, cpu
);
938 seq
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
939 pkts
= cpu_stats
->pkts
;
940 bytes
= cpu_stats
->bytes
;
941 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, seq
));
943 total
.bytes
+= bytes
;
945 nest
= nla_nest_start(skb
, NFTA_CHAIN_COUNTERS
);
947 goto nla_put_failure
;
949 if (nla_put_be64(skb
, NFTA_COUNTER_PACKETS
, cpu_to_be64(total
.pkts
)) ||
950 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
)))
981 goto nla_put_failure
;
982 if (nla_put_string(skb
, NFTA_CHAIN_NAME
, chain
->name
))
983 goto nla_put_failure
;
985 if (chain
->flags
& NFT_BASE_CHAIN
) {
986 const struct nft_base_chain
*basechain
= nft_base_chain(chain
);
987 const struct nf_hook_ops
*ops
= &basechain
->ops
[0];
990 nest
= nla_nest_start(skb
, NFTA_CHAIN_HOOK
);
992 goto nla_put_failure
;
993 if (nla_put_be32(skb
, NFTA_HOOK_HOOKNUM
, htonl(ops
->hooknum
)))
994 goto nla_put_failure
;
995 if (nla_put_be32(skb
, NFTA_HOOK_PRIORITY
, htonl(ops
->priority
)))
996 goto nla_put_failure
;
997 if (basechain
->dev_name
[0] &&
998 nla_put_string(skb
, NFTA_HOOK_DEV
, basechain
->dev_name
))
999 goto nla_put_failure
;
1000 nla_nest_end(skb
, nest
);
1002 if (nla_put_be32(skb
, NFTA_CHAIN_POLICY
,
1003 htonl(basechain
->policy
)))
1004 goto nla_put_failure
;
1006 if (nla_put_string(skb
, NFTA_CHAIN_TYPE
, basechain
->type
->name
))
1007 goto nla_put_failure
;
1009 if (nft_dump_stats(skb
, nft_base_chain(chain
)->stats
))
1010 goto nla_put_failure
;
1013 if (nla_put_be32(skb
, NFTA_CHAIN_USE
, htonl(chain
->use
)))
1014 goto nla_put_failure
;
1016 nlmsg_end(skb
, nlh
);
1020 nlmsg_trim(skb
, nlh
);
1024 static int nf_tables_chain_notify(const struct nft_ctx
*ctx
, int event
)
1026 struct sk_buff
*skb
;
1030 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
1034 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1038 err
= nf_tables_fill_chain_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
1039 event
, 0, ctx
->afi
->family
, ctx
->table
,
1046 err
= nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1047 ctx
->report
, GFP_KERNEL
);
1050 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1056 static int nf_tables_dump_chains(struct sk_buff
*skb
,
1057 struct netlink_callback
*cb
)
1059 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1060 const struct nft_af_info
*afi
;
1061 const struct nft_table
*table
;
1062 const struct nft_chain
*chain
;
1063 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1064 struct net
*net
= sock_net(skb
->sk
);
1065 int family
= nfmsg
->nfgen_family
;
1068 cb
->seq
= net
->nft
.base_seq
;
1070 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
1071 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
1074 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
1075 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
1079 memset(&cb
->args
[1], 0,
1080 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1081 if (nf_tables_fill_chain_info(skb
, net
,
1082 NETLINK_CB(cb
->skb
).portid
,
1086 afi
->family
, table
, chain
) < 0)
1089 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1101 static int nf_tables_getchain(struct sock
*nlsk
, struct sk_buff
*skb
,
1102 const struct nlmsghdr
*nlh
,
1103 const struct nlattr
* const nla
[])
1105 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1106 const struct nft_af_info
*afi
;
1107 const struct nft_table
*table
;
1108 const struct nft_chain
*chain
;
1109 struct sk_buff
*skb2
;
1110 struct net
*net
= sock_net(skb
->sk
);
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
);
1495 if (table
->flags
& NFT_TABLE_INACTIVE
)
1498 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
]);
1500 return PTR_ERR(chain
);
1501 if (chain
->flags
& NFT_CHAIN_INACTIVE
)
1506 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
1508 return nft_delchain(&ctx
);
1516 * nft_register_expr - register nf_tables expr type
1519 * Registers the expr type for use with nf_tables. Returns zero on
1520 * success or a negative errno code otherwise.
1522 int nft_register_expr(struct nft_expr_type
*type
)
1524 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1525 if (type
->family
== NFPROTO_UNSPEC
)
1526 list_add_tail_rcu(&type
->list
, &nf_tables_expressions
);
1528 list_add_rcu(&type
->list
, &nf_tables_expressions
);
1529 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1532 EXPORT_SYMBOL_GPL(nft_register_expr
);
1535 * nft_unregister_expr - unregister nf_tables expr type
1538 * Unregisters the expr typefor use with nf_tables.
1540 void nft_unregister_expr(struct nft_expr_type
*type
)
1542 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1543 list_del_rcu(&type
->list
);
1544 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1546 EXPORT_SYMBOL_GPL(nft_unregister_expr
);
1548 static const struct nft_expr_type
*__nft_expr_type_get(u8 family
,
1551 const struct nft_expr_type
*type
;
1553 list_for_each_entry(type
, &nf_tables_expressions
, list
) {
1554 if (!nla_strcmp(nla
, type
->name
) &&
1555 (!type
->family
|| type
->family
== family
))
1561 static const struct nft_expr_type
*nft_expr_type_get(u8 family
,
1564 const struct nft_expr_type
*type
;
1567 return ERR_PTR(-EINVAL
);
1569 type
= __nft_expr_type_get(family
, nla
);
1570 if (type
!= NULL
&& try_module_get(type
->owner
))
1573 #ifdef CONFIG_MODULES
1575 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1576 request_module("nft-expr-%u-%.*s", family
,
1577 nla_len(nla
), (char *)nla_data(nla
));
1578 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1579 if (__nft_expr_type_get(family
, nla
))
1580 return ERR_PTR(-EAGAIN
);
1582 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1583 request_module("nft-expr-%.*s",
1584 nla_len(nla
), (char *)nla_data(nla
));
1585 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1586 if (__nft_expr_type_get(family
, nla
))
1587 return ERR_PTR(-EAGAIN
);
1590 return ERR_PTR(-ENOENT
);
1593 static const struct nla_policy nft_expr_policy
[NFTA_EXPR_MAX
+ 1] = {
1594 [NFTA_EXPR_NAME
] = { .type
= NLA_STRING
},
1595 [NFTA_EXPR_DATA
] = { .type
= NLA_NESTED
},
1598 static int nf_tables_fill_expr_info(struct sk_buff
*skb
,
1599 const struct nft_expr
*expr
)
1601 if (nla_put_string(skb
, NFTA_EXPR_NAME
, expr
->ops
->type
->name
))
1602 goto nla_put_failure
;
1604 if (expr
->ops
->dump
) {
1605 struct nlattr
*data
= nla_nest_start(skb
, NFTA_EXPR_DATA
);
1607 goto nla_put_failure
;
1608 if (expr
->ops
->dump(skb
, expr
) < 0)
1609 goto nla_put_failure
;
1610 nla_nest_end(skb
, data
);
1619 int nft_expr_dump(struct sk_buff
*skb
, unsigned int attr
,
1620 const struct nft_expr
*expr
)
1622 struct nlattr
*nest
;
1624 nest
= nla_nest_start(skb
, attr
);
1626 goto nla_put_failure
;
1627 if (nf_tables_fill_expr_info(skb
, expr
) < 0)
1628 goto nla_put_failure
;
1629 nla_nest_end(skb
, nest
);
1636 struct nft_expr_info
{
1637 const struct nft_expr_ops
*ops
;
1638 struct nlattr
*tb
[NFT_EXPR_MAXATTR
+ 1];
1641 static int nf_tables_expr_parse(const struct nft_ctx
*ctx
,
1642 const struct nlattr
*nla
,
1643 struct nft_expr_info
*info
)
1645 const struct nft_expr_type
*type
;
1646 const struct nft_expr_ops
*ops
;
1647 struct nlattr
*tb
[NFTA_EXPR_MAX
+ 1];
1650 err
= nla_parse_nested(tb
, NFTA_EXPR_MAX
, nla
, nft_expr_policy
);
1654 type
= nft_expr_type_get(ctx
->afi
->family
, tb
[NFTA_EXPR_NAME
]);
1656 return PTR_ERR(type
);
1658 if (tb
[NFTA_EXPR_DATA
]) {
1659 err
= nla_parse_nested(info
->tb
, type
->maxattr
,
1660 tb
[NFTA_EXPR_DATA
], type
->policy
);
1664 memset(info
->tb
, 0, sizeof(info
->tb
[0]) * (type
->maxattr
+ 1));
1666 if (type
->select_ops
!= NULL
) {
1667 ops
= type
->select_ops(ctx
,
1668 (const struct nlattr
* const *)info
->tb
);
1680 module_put(type
->owner
);
1684 static int nf_tables_newexpr(const struct nft_ctx
*ctx
,
1685 const struct nft_expr_info
*info
,
1686 struct nft_expr
*expr
)
1688 const struct nft_expr_ops
*ops
= info
->ops
;
1693 err
= ops
->init(ctx
, expr
, (const struct nlattr
**)info
->tb
);
1705 static void nf_tables_expr_destroy(const struct nft_ctx
*ctx
,
1706 struct nft_expr
*expr
)
1708 if (expr
->ops
->destroy
)
1709 expr
->ops
->destroy(ctx
, expr
);
1710 module_put(expr
->ops
->type
->owner
);
1713 struct nft_expr
*nft_expr_init(const struct nft_ctx
*ctx
,
1714 const struct nlattr
*nla
)
1716 struct nft_expr_info info
;
1717 struct nft_expr
*expr
;
1720 err
= nf_tables_expr_parse(ctx
, nla
, &info
);
1725 expr
= kzalloc(info
.ops
->size
, GFP_KERNEL
);
1729 err
= nf_tables_newexpr(ctx
, &info
, expr
);
1735 module_put(info
.ops
->type
->owner
);
1737 return ERR_PTR(err
);
1740 void nft_expr_destroy(const struct nft_ctx
*ctx
, struct nft_expr
*expr
)
1742 nf_tables_expr_destroy(ctx
, expr
);
1750 static struct nft_rule
*__nf_tables_rule_lookup(const struct nft_chain
*chain
,
1753 struct nft_rule
*rule
;
1755 // FIXME: this sucks
1756 list_for_each_entry(rule
, &chain
->rules
, list
) {
1757 if (handle
== rule
->handle
)
1761 return ERR_PTR(-ENOENT
);
1764 static struct nft_rule
*nf_tables_rule_lookup(const struct nft_chain
*chain
,
1765 const struct nlattr
*nla
)
1768 return ERR_PTR(-EINVAL
);
1770 return __nf_tables_rule_lookup(chain
, be64_to_cpu(nla_get_be64(nla
)));
1773 static const struct nla_policy nft_rule_policy
[NFTA_RULE_MAX
+ 1] = {
1774 [NFTA_RULE_TABLE
] = { .type
= NLA_STRING
},
1775 [NFTA_RULE_CHAIN
] = { .type
= NLA_STRING
,
1776 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
1777 [NFTA_RULE_HANDLE
] = { .type
= NLA_U64
},
1778 [NFTA_RULE_EXPRESSIONS
] = { .type
= NLA_NESTED
},
1779 [NFTA_RULE_COMPAT
] = { .type
= NLA_NESTED
},
1780 [NFTA_RULE_POSITION
] = { .type
= NLA_U64
},
1781 [NFTA_RULE_USERDATA
] = { .type
= NLA_BINARY
,
1782 .len
= NFT_USERDATA_MAXLEN
},
1785 static int nf_tables_fill_rule_info(struct sk_buff
*skb
, struct net
*net
,
1786 u32 portid
, u32 seq
, int event
,
1787 u32 flags
, int family
,
1788 const struct nft_table
*table
,
1789 const struct nft_chain
*chain
,
1790 const struct nft_rule
*rule
)
1792 struct nlmsghdr
*nlh
;
1793 struct nfgenmsg
*nfmsg
;
1794 const struct nft_expr
*expr
, *next
;
1795 struct nlattr
*list
;
1796 const struct nft_rule
*prule
;
1797 int type
= event
| NFNL_SUBSYS_NFTABLES
<< 8;
1799 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(struct nfgenmsg
),
1802 goto nla_put_failure
;
1804 nfmsg
= nlmsg_data(nlh
);
1805 nfmsg
->nfgen_family
= family
;
1806 nfmsg
->version
= NFNETLINK_V0
;
1807 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
1809 if (nla_put_string(skb
, NFTA_RULE_TABLE
, table
->name
))
1810 goto nla_put_failure
;
1811 if (nla_put_string(skb
, NFTA_RULE_CHAIN
, chain
->name
))
1812 goto nla_put_failure
;
1813 if (nla_put_be64(skb
, NFTA_RULE_HANDLE
, cpu_to_be64(rule
->handle
)))
1814 goto nla_put_failure
;
1816 if ((event
!= NFT_MSG_DELRULE
) && (rule
->list
.prev
!= &chain
->rules
)) {
1817 prule
= list_entry(rule
->list
.prev
, struct nft_rule
, list
);
1818 if (nla_put_be64(skb
, NFTA_RULE_POSITION
,
1819 cpu_to_be64(prule
->handle
)))
1820 goto nla_put_failure
;
1823 list
= nla_nest_start(skb
, NFTA_RULE_EXPRESSIONS
);
1825 goto nla_put_failure
;
1826 nft_rule_for_each_expr(expr
, next
, rule
) {
1827 if (nft_expr_dump(skb
, NFTA_LIST_ELEM
, expr
) < 0)
1828 goto nla_put_failure
;
1830 nla_nest_end(skb
, list
);
1833 struct nft_userdata
*udata
= nft_userdata(rule
);
1834 if (nla_put(skb
, NFTA_RULE_USERDATA
, udata
->len
+ 1,
1836 goto nla_put_failure
;
1839 nlmsg_end(skb
, nlh
);
1843 nlmsg_trim(skb
, nlh
);
1847 static int nf_tables_rule_notify(const struct nft_ctx
*ctx
,
1848 const struct nft_rule
*rule
,
1851 struct sk_buff
*skb
;
1855 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
1859 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1863 err
= nf_tables_fill_rule_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
1864 event
, 0, ctx
->afi
->family
, ctx
->table
,
1871 err
= nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1872 ctx
->report
, GFP_KERNEL
);
1875 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1881 static int nf_tables_dump_rules(struct sk_buff
*skb
,
1882 struct netlink_callback
*cb
)
1884 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1885 const struct nft_af_info
*afi
;
1886 const struct nft_table
*table
;
1887 const struct nft_chain
*chain
;
1888 const struct nft_rule
*rule
;
1889 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1890 struct net
*net
= sock_net(skb
->sk
);
1891 int family
= nfmsg
->nfgen_family
;
1894 cb
->seq
= net
->nft
.base_seq
;
1896 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
1897 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
1900 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
1901 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
1902 list_for_each_entry_rcu(rule
, &chain
->rules
, list
) {
1903 if (!nft_rule_is_active(net
, rule
))
1908 memset(&cb
->args
[1], 0,
1909 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1910 if (nf_tables_fill_rule_info(skb
, net
, NETLINK_CB(cb
->skb
).portid
,
1913 NLM_F_MULTI
| NLM_F_APPEND
,
1914 afi
->family
, table
, chain
, rule
) < 0)
1917 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1931 static int nf_tables_getrule(struct sock
*nlsk
, struct sk_buff
*skb
,
1932 const struct nlmsghdr
*nlh
,
1933 const struct nlattr
* const nla
[])
1935 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1936 const struct nft_af_info
*afi
;
1937 const struct nft_table
*table
;
1938 const struct nft_chain
*chain
;
1939 const struct nft_rule
*rule
;
1940 struct sk_buff
*skb2
;
1941 struct net
*net
= sock_net(skb
->sk
);
1942 int family
= nfmsg
->nfgen_family
;
1945 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1946 struct netlink_dump_control c
= {
1947 .dump
= nf_tables_dump_rules
,
1949 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
1952 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1954 return PTR_ERR(afi
);
1956 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
1958 return PTR_ERR(table
);
1959 if (table
->flags
& NFT_TABLE_INACTIVE
)
1962 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
1964 return PTR_ERR(chain
);
1965 if (chain
->flags
& NFT_CHAIN_INACTIVE
)
1968 rule
= nf_tables_rule_lookup(chain
, nla
[NFTA_RULE_HANDLE
]);
1970 return PTR_ERR(rule
);
1972 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1976 err
= nf_tables_fill_rule_info(skb2
, net
, NETLINK_CB(skb
).portid
,
1977 nlh
->nlmsg_seq
, NFT_MSG_NEWRULE
, 0,
1978 family
, table
, chain
, rule
);
1982 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
1989 static void nf_tables_rule_destroy(const struct nft_ctx
*ctx
,
1990 struct nft_rule
*rule
)
1992 struct nft_expr
*expr
;
1995 * Careful: some expressions might not be initialized in case this
1996 * is called on error from nf_tables_newrule().
1998 expr
= nft_expr_first(rule
);
1999 while (expr
!= nft_expr_last(rule
) && expr
->ops
) {
2000 nf_tables_expr_destroy(ctx
, expr
);
2001 expr
= nft_expr_next(expr
);
2006 #define NFT_RULE_MAXEXPRS 128
2008 static struct nft_expr_info
*info
;
2010 static int nf_tables_newrule(struct net
*net
, struct sock
*nlsk
,
2011 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2012 const struct nlattr
* const nla
[])
2014 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2015 struct nft_af_info
*afi
;
2016 struct nft_table
*table
;
2017 struct nft_chain
*chain
;
2018 struct nft_rule
*rule
, *old_rule
= NULL
;
2019 struct nft_userdata
*udata
;
2020 struct nft_trans
*trans
= NULL
;
2021 struct nft_expr
*expr
;
2024 unsigned int size
, i
, n
, ulen
= 0, usize
= 0;
2027 u64 handle
, pos_handle
;
2029 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
2031 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, create
);
2033 return PTR_ERR(afi
);
2035 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
2037 return PTR_ERR(table
);
2039 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
2041 return PTR_ERR(chain
);
2043 if (nla
[NFTA_RULE_HANDLE
]) {
2044 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_HANDLE
]));
2045 rule
= __nf_tables_rule_lookup(chain
, handle
);
2047 return PTR_ERR(rule
);
2049 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2051 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2056 if (!create
|| nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2058 handle
= nf_tables_alloc_handle(table
);
2060 if (chain
->use
== UINT_MAX
)
2064 if (nla
[NFTA_RULE_POSITION
]) {
2065 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
2068 pos_handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_POSITION
]));
2069 old_rule
= __nf_tables_rule_lookup(chain
, pos_handle
);
2070 if (IS_ERR(old_rule
))
2071 return PTR_ERR(old_rule
);
2074 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
2078 if (nla
[NFTA_RULE_EXPRESSIONS
]) {
2079 nla_for_each_nested(tmp
, nla
[NFTA_RULE_EXPRESSIONS
], rem
) {
2081 if (nla_type(tmp
) != NFTA_LIST_ELEM
)
2083 if (n
== NFT_RULE_MAXEXPRS
)
2085 err
= nf_tables_expr_parse(&ctx
, tmp
, &info
[n
]);
2088 size
+= info
[n
].ops
->size
;
2092 /* Check for overflow of dlen field */
2094 if (size
>= 1 << 12)
2097 if (nla
[NFTA_RULE_USERDATA
]) {
2098 ulen
= nla_len(nla
[NFTA_RULE_USERDATA
]);
2100 usize
= sizeof(struct nft_userdata
) + ulen
;
2104 rule
= kzalloc(sizeof(*rule
) + size
+ usize
, GFP_KERNEL
);
2108 nft_rule_activate_next(net
, rule
);
2110 rule
->handle
= handle
;
2112 rule
->udata
= ulen
? 1 : 0;
2115 udata
= nft_userdata(rule
);
2116 udata
->len
= ulen
- 1;
2117 nla_memcpy(udata
->data
, nla
[NFTA_RULE_USERDATA
], ulen
);
2120 expr
= nft_expr_first(rule
);
2121 for (i
= 0; i
< n
; i
++) {
2122 err
= nf_tables_newexpr(&ctx
, &info
[i
], expr
);
2126 expr
= nft_expr_next(expr
);
2129 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
2130 if (nft_rule_is_active_next(net
, old_rule
)) {
2131 trans
= nft_trans_rule_add(&ctx
, NFT_MSG_DELRULE
,
2133 if (trans
== NULL
) {
2137 nft_rule_deactivate_next(net
, old_rule
);
2139 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
2144 } else if (nlh
->nlmsg_flags
& NLM_F_APPEND
)
2146 list_add_rcu(&rule
->list
, &old_rule
->list
);
2148 list_add_tail_rcu(&rule
->list
, &chain
->rules
);
2151 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
2153 list_add_rcu(&rule
->list
, &chain
->rules
);
2156 if (nft_trans_rule_add(&ctx
, NFT_MSG_NEWRULE
, rule
) == NULL
) {
2164 list_del_rcu(&rule
->list
);
2166 nf_tables_rule_destroy(&ctx
, rule
);
2168 for (i
= 0; i
< n
; i
++) {
2169 if (info
[i
].ops
!= NULL
)
2170 module_put(info
[i
].ops
->type
->owner
);
2175 static int nf_tables_delrule(struct net
*net
, struct sock
*nlsk
,
2176 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2177 const struct nlattr
* const nla
[])
2179 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2180 struct nft_af_info
*afi
;
2181 struct nft_table
*table
;
2182 struct nft_chain
*chain
= NULL
;
2183 struct nft_rule
*rule
;
2184 int family
= nfmsg
->nfgen_family
, err
= 0;
2187 afi
= nf_tables_afinfo_lookup(net
, family
, false);
2189 return PTR_ERR(afi
);
2191 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
2193 return PTR_ERR(table
);
2194 if (table
->flags
& NFT_TABLE_INACTIVE
)
2197 if (nla
[NFTA_RULE_CHAIN
]) {
2198 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
2200 return PTR_ERR(chain
);
2203 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
2206 if (nla
[NFTA_RULE_HANDLE
]) {
2207 rule
= nf_tables_rule_lookup(chain
,
2208 nla
[NFTA_RULE_HANDLE
]);
2210 return PTR_ERR(rule
);
2212 err
= nft_delrule(&ctx
, rule
);
2214 err
= nft_delrule_by_chain(&ctx
);
2217 list_for_each_entry(chain
, &table
->chains
, list
) {
2219 err
= nft_delrule_by_chain(&ctx
);
2232 static LIST_HEAD(nf_tables_set_ops
);
2234 int nft_register_set(struct nft_set_ops
*ops
)
2236 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2237 list_add_tail_rcu(&ops
->list
, &nf_tables_set_ops
);
2238 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2241 EXPORT_SYMBOL_GPL(nft_register_set
);
2243 void nft_unregister_set(struct nft_set_ops
*ops
)
2245 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2246 list_del_rcu(&ops
->list
);
2247 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2249 EXPORT_SYMBOL_GPL(nft_unregister_set
);
2252 * Select a set implementation based on the data characteristics and the
2253 * given policy. The total memory use might not be known if no size is
2254 * given, in that case the amount of memory per element is used.
2256 static const struct nft_set_ops
*
2257 nft_select_set_ops(const struct nlattr
* const nla
[],
2258 const struct nft_set_desc
*desc
,
2259 enum nft_set_policies policy
)
2261 const struct nft_set_ops
*ops
, *bops
;
2262 struct nft_set_estimate est
, best
;
2265 #ifdef CONFIG_MODULES
2266 if (list_empty(&nf_tables_set_ops
)) {
2267 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2268 request_module("nft-set");
2269 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2270 if (!list_empty(&nf_tables_set_ops
))
2271 return ERR_PTR(-EAGAIN
);
2275 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
2276 features
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
2277 features
&= NFT_SET_INTERVAL
| NFT_SET_MAP
| NFT_SET_TIMEOUT
;
2284 list_for_each_entry(ops
, &nf_tables_set_ops
, list
) {
2285 if ((ops
->features
& features
) != features
)
2287 if (!ops
->estimate(desc
, features
, &est
))
2291 case NFT_SET_POL_PERFORMANCE
:
2292 if (est
.class < best
.class)
2294 if (est
.class == best
.class && est
.size
< best
.size
)
2297 case NFT_SET_POL_MEMORY
:
2298 if (est
.size
< best
.size
)
2300 if (est
.size
== best
.size
&& est
.class < best
.class)
2307 if (!try_module_get(ops
->owner
))
2310 module_put(bops
->owner
);
2319 return ERR_PTR(-EOPNOTSUPP
);
2322 static const struct nla_policy nft_set_policy
[NFTA_SET_MAX
+ 1] = {
2323 [NFTA_SET_TABLE
] = { .type
= NLA_STRING
},
2324 [NFTA_SET_NAME
] = { .type
= NLA_STRING
,
2325 .len
= IFNAMSIZ
- 1 },
2326 [NFTA_SET_FLAGS
] = { .type
= NLA_U32
},
2327 [NFTA_SET_KEY_TYPE
] = { .type
= NLA_U32
},
2328 [NFTA_SET_KEY_LEN
] = { .type
= NLA_U32
},
2329 [NFTA_SET_DATA_TYPE
] = { .type
= NLA_U32
},
2330 [NFTA_SET_DATA_LEN
] = { .type
= NLA_U32
},
2331 [NFTA_SET_POLICY
] = { .type
= NLA_U32
},
2332 [NFTA_SET_DESC
] = { .type
= NLA_NESTED
},
2333 [NFTA_SET_ID
] = { .type
= NLA_U32
},
2334 [NFTA_SET_TIMEOUT
] = { .type
= NLA_U64
},
2335 [NFTA_SET_GC_INTERVAL
] = { .type
= NLA_U32
},
2338 static const struct nla_policy nft_set_desc_policy
[NFTA_SET_DESC_MAX
+ 1] = {
2339 [NFTA_SET_DESC_SIZE
] = { .type
= NLA_U32
},
2342 static int nft_ctx_init_from_setattr(struct nft_ctx
*ctx
, struct net
*net
,
2343 const struct sk_buff
*skb
,
2344 const struct nlmsghdr
*nlh
,
2345 const struct nlattr
* const nla
[])
2347 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2348 struct nft_af_info
*afi
= NULL
;
2349 struct nft_table
*table
= NULL
;
2351 if (nfmsg
->nfgen_family
!= NFPROTO_UNSPEC
) {
2352 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, false);
2354 return PTR_ERR(afi
);
2357 if (nla
[NFTA_SET_TABLE
] != NULL
) {
2359 return -EAFNOSUPPORT
;
2361 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
]);
2363 return PTR_ERR(table
);
2364 if (table
->flags
& NFT_TABLE_INACTIVE
)
2368 nft_ctx_init(ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
2372 struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
2373 const struct nlattr
*nla
)
2375 struct nft_set
*set
;
2378 return ERR_PTR(-EINVAL
);
2380 list_for_each_entry(set
, &table
->sets
, list
) {
2381 if (!nla_strcmp(nla
, set
->name
))
2384 return ERR_PTR(-ENOENT
);
2387 struct nft_set
*nf_tables_set_lookup_byid(const struct net
*net
,
2388 const struct nlattr
*nla
)
2390 struct nft_trans
*trans
;
2391 u32 id
= ntohl(nla_get_be32(nla
));
2393 list_for_each_entry(trans
, &net
->nft
.commit_list
, list
) {
2394 if (trans
->msg_type
== NFT_MSG_NEWSET
&&
2395 id
== nft_trans_set_id(trans
))
2396 return nft_trans_set(trans
);
2398 return ERR_PTR(-ENOENT
);
2401 static int nf_tables_set_alloc_name(struct nft_ctx
*ctx
, struct nft_set
*set
,
2404 const struct nft_set
*i
;
2406 unsigned long *inuse
;
2407 unsigned int n
= 0, min
= 0;
2409 p
= strnchr(name
, IFNAMSIZ
, '%');
2411 if (p
[1] != 'd' || strchr(p
+ 2, '%'))
2414 inuse
= (unsigned long *)get_zeroed_page(GFP_KERNEL
);
2418 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
2421 if (!sscanf(i
->name
, name
, &tmp
))
2423 if (tmp
< min
|| tmp
>= min
+ BITS_PER_BYTE
* PAGE_SIZE
)
2426 set_bit(tmp
- min
, inuse
);
2429 n
= find_first_zero_bit(inuse
, BITS_PER_BYTE
* PAGE_SIZE
);
2430 if (n
>= BITS_PER_BYTE
* PAGE_SIZE
) {
2431 min
+= BITS_PER_BYTE
* PAGE_SIZE
;
2432 memset(inuse
, 0, PAGE_SIZE
);
2435 free_page((unsigned long)inuse
);
2438 snprintf(set
->name
, sizeof(set
->name
), name
, min
+ n
);
2439 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
2440 if (!strcmp(set
->name
, i
->name
))
2446 static int nf_tables_fill_set(struct sk_buff
*skb
, const struct nft_ctx
*ctx
,
2447 const struct nft_set
*set
, u16 event
, u16 flags
)
2449 struct nfgenmsg
*nfmsg
;
2450 struct nlmsghdr
*nlh
;
2451 struct nlattr
*desc
;
2452 u32 portid
= ctx
->portid
;
2455 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
2456 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
2459 goto nla_put_failure
;
2461 nfmsg
= nlmsg_data(nlh
);
2462 nfmsg
->nfgen_family
= ctx
->afi
->family
;
2463 nfmsg
->version
= NFNETLINK_V0
;
2464 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
2466 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
2467 goto nla_put_failure
;
2468 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
2469 goto nla_put_failure
;
2470 if (set
->flags
!= 0)
2471 if (nla_put_be32(skb
, NFTA_SET_FLAGS
, htonl(set
->flags
)))
2472 goto nla_put_failure
;
2474 if (nla_put_be32(skb
, NFTA_SET_KEY_TYPE
, htonl(set
->ktype
)))
2475 goto nla_put_failure
;
2476 if (nla_put_be32(skb
, NFTA_SET_KEY_LEN
, htonl(set
->klen
)))
2477 goto nla_put_failure
;
2478 if (set
->flags
& NFT_SET_MAP
) {
2479 if (nla_put_be32(skb
, NFTA_SET_DATA_TYPE
, htonl(set
->dtype
)))
2480 goto nla_put_failure
;
2481 if (nla_put_be32(skb
, NFTA_SET_DATA_LEN
, htonl(set
->dlen
)))
2482 goto nla_put_failure
;
2486 nla_put_be64(skb
, NFTA_SET_TIMEOUT
, cpu_to_be64(set
->timeout
)))
2487 goto nla_put_failure
;
2489 nla_put_be32(skb
, NFTA_SET_GC_INTERVAL
, htonl(set
->gc_int
)))
2490 goto nla_put_failure
;
2492 if (set
->policy
!= NFT_SET_POL_PERFORMANCE
) {
2493 if (nla_put_be32(skb
, NFTA_SET_POLICY
, htonl(set
->policy
)))
2494 goto nla_put_failure
;
2497 desc
= nla_nest_start(skb
, NFTA_SET_DESC
);
2499 goto nla_put_failure
;
2501 nla_put_be32(skb
, NFTA_SET_DESC_SIZE
, htonl(set
->size
)))
2502 goto nla_put_failure
;
2503 nla_nest_end(skb
, desc
);
2505 nlmsg_end(skb
, nlh
);
2509 nlmsg_trim(skb
, nlh
);
2513 static int nf_tables_set_notify(const struct nft_ctx
*ctx
,
2514 const struct nft_set
*set
,
2515 int event
, gfp_t gfp_flags
)
2517 struct sk_buff
*skb
;
2518 u32 portid
= ctx
->portid
;
2522 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
2526 skb
= nlmsg_new(NLMSG_GOODSIZE
, gfp_flags
);
2530 err
= nf_tables_fill_set(skb
, ctx
, set
, event
, 0);
2536 err
= nfnetlink_send(skb
, ctx
->net
, portid
, NFNLGRP_NFTABLES
,
2537 ctx
->report
, gfp_flags
);
2540 nfnetlink_set_err(ctx
->net
, portid
, NFNLGRP_NFTABLES
, err
);
2544 static int nf_tables_dump_sets(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2546 const struct nft_set
*set
;
2547 unsigned int idx
, s_idx
= cb
->args
[0];
2548 struct nft_af_info
*afi
;
2549 struct nft_table
*table
, *cur_table
= (struct nft_table
*)cb
->args
[2];
2550 struct net
*net
= sock_net(skb
->sk
);
2551 int cur_family
= cb
->args
[3];
2552 struct nft_ctx
*ctx
= cb
->data
, ctx_set
;
2558 cb
->seq
= net
->nft
.base_seq
;
2560 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
2561 if (ctx
->afi
&& ctx
->afi
!= afi
)
2565 if (afi
->family
!= cur_family
)
2570 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
2571 if (ctx
->table
&& ctx
->table
!= table
)
2575 if (cur_table
!= table
)
2581 list_for_each_entry_rcu(set
, &table
->sets
, list
) {
2586 ctx_set
.table
= table
;
2588 if (nf_tables_fill_set(skb
, &ctx_set
, set
,
2592 cb
->args
[2] = (unsigned long) table
;
2593 cb
->args
[3] = afi
->family
;
2596 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2610 static int nf_tables_dump_sets_done(struct netlink_callback
*cb
)
2616 static int nf_tables_getset(struct sock
*nlsk
, struct sk_buff
*skb
,
2617 const struct nlmsghdr
*nlh
,
2618 const struct nlattr
* const nla
[])
2620 struct net
*net
= sock_net(skb
->sk
);
2621 const struct nft_set
*set
;
2623 struct sk_buff
*skb2
;
2624 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2627 /* Verify existence before starting dump */
2628 err
= nft_ctx_init_from_setattr(&ctx
, net
, skb
, nlh
, nla
);
2632 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2633 struct netlink_dump_control c
= {
2634 .dump
= nf_tables_dump_sets
,
2635 .done
= nf_tables_dump_sets_done
,
2637 struct nft_ctx
*ctx_dump
;
2639 ctx_dump
= kmalloc(sizeof(*ctx_dump
), GFP_KERNEL
);
2640 if (ctx_dump
== NULL
)
2646 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
2649 /* Only accept unspec with dump */
2650 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
2651 return -EAFNOSUPPORT
;
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
[IFNAMSIZ
];
2705 u32 ktype
, dtype
, flags
, policy
, gc_int
;
2706 struct nft_set_desc desc
;
2709 if (nla
[NFTA_SET_TABLE
] == NULL
||
2710 nla
[NFTA_SET_NAME
] == NULL
||
2711 nla
[NFTA_SET_KEY_LEN
] == NULL
||
2712 nla
[NFTA_SET_ID
] == NULL
)
2715 memset(&desc
, 0, sizeof(desc
));
2717 ktype
= NFT_DATA_VALUE
;
2718 if (nla
[NFTA_SET_KEY_TYPE
] != NULL
) {
2719 ktype
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_TYPE
]));
2720 if ((ktype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
)
2724 desc
.klen
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_LEN
]));
2725 if (desc
.klen
== 0 || desc
.klen
> NFT_DATA_VALUE_MAXLEN
)
2729 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
2730 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
2731 if (flags
& ~(NFT_SET_ANONYMOUS
| NFT_SET_CONSTANT
|
2732 NFT_SET_INTERVAL
| NFT_SET_TIMEOUT
|
2733 NFT_SET_MAP
| NFT_SET_EVAL
))
2735 /* Only one of both operations is supported */
2736 if ((flags
& (NFT_SET_MAP
| NFT_SET_EVAL
)) ==
2737 (NFT_SET_MAP
| NFT_SET_EVAL
))
2742 if (nla
[NFTA_SET_DATA_TYPE
] != NULL
) {
2743 if (!(flags
& NFT_SET_MAP
))
2746 dtype
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_TYPE
]));
2747 if ((dtype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
&&
2748 dtype
!= NFT_DATA_VERDICT
)
2751 if (dtype
!= NFT_DATA_VERDICT
) {
2752 if (nla
[NFTA_SET_DATA_LEN
] == NULL
)
2754 desc
.dlen
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_LEN
]));
2755 if (desc
.dlen
== 0 || desc
.dlen
> NFT_DATA_VALUE_MAXLEN
)
2758 desc
.dlen
= sizeof(struct nft_verdict
);
2759 } else if (flags
& NFT_SET_MAP
)
2763 if (nla
[NFTA_SET_TIMEOUT
] != NULL
) {
2764 if (!(flags
& NFT_SET_TIMEOUT
))
2766 timeout
= be64_to_cpu(nla_get_be64(nla
[NFTA_SET_TIMEOUT
]));
2769 if (nla
[NFTA_SET_GC_INTERVAL
] != NULL
) {
2770 if (!(flags
& NFT_SET_TIMEOUT
))
2772 gc_int
= ntohl(nla_get_be32(nla
[NFTA_SET_GC_INTERVAL
]));
2775 policy
= NFT_SET_POL_PERFORMANCE
;
2776 if (nla
[NFTA_SET_POLICY
] != NULL
)
2777 policy
= ntohl(nla_get_be32(nla
[NFTA_SET_POLICY
]));
2779 if (nla
[NFTA_SET_DESC
] != NULL
) {
2780 err
= nf_tables_set_desc_parse(&ctx
, &desc
, nla
[NFTA_SET_DESC
]);
2785 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
2787 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, create
);
2789 return PTR_ERR(afi
);
2791 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
]);
2793 return PTR_ERR(table
);
2795 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
2797 set
= nf_tables_set_lookup(table
, nla
[NFTA_SET_NAME
]);
2799 if (PTR_ERR(set
) != -ENOENT
)
2800 return PTR_ERR(set
);
2805 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2807 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2812 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
2815 ops
= nft_select_set_ops(nla
, &desc
, policy
);
2817 return PTR_ERR(ops
);
2820 if (ops
->privsize
!= NULL
)
2821 size
= ops
->privsize(nla
);
2824 set
= kzalloc(sizeof(*set
) + size
, GFP_KERNEL
);
2828 nla_strlcpy(name
, nla
[NFTA_SET_NAME
], sizeof(set
->name
));
2829 err
= nf_tables_set_alloc_name(&ctx
, set
, name
);
2833 INIT_LIST_HEAD(&set
->bindings
);
2834 write_pnet(&set
->pnet
, net
);
2837 set
->klen
= desc
.klen
;
2839 set
->dlen
= desc
.dlen
;
2841 set
->size
= desc
.size
;
2842 set
->policy
= policy
;
2843 set
->timeout
= timeout
;
2844 set
->gc_int
= gc_int
;
2846 err
= ops
->init(set
, &desc
, nla
);
2850 err
= nft_trans_set_add(&ctx
, NFT_MSG_NEWSET
, set
);
2854 list_add_tail_rcu(&set
->list
, &table
->sets
);
2861 module_put(ops
->owner
);
2865 static void nft_set_destroy(struct nft_set
*set
)
2867 set
->ops
->destroy(set
);
2868 module_put(set
->ops
->owner
);
2872 static void nf_tables_set_destroy(const struct nft_ctx
*ctx
, struct nft_set
*set
)
2874 list_del_rcu(&set
->list
);
2875 nf_tables_set_notify(ctx
, set
, NFT_MSG_DELSET
, GFP_ATOMIC
);
2876 nft_set_destroy(set
);
2879 static int nf_tables_delset(struct net
*net
, struct sock
*nlsk
,
2880 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2881 const struct nlattr
* const nla
[])
2883 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2884 struct nft_set
*set
;
2888 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
2889 return -EAFNOSUPPORT
;
2890 if (nla
[NFTA_SET_TABLE
] == NULL
)
2893 err
= nft_ctx_init_from_setattr(&ctx
, net
, skb
, nlh
, nla
);
2897 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
]);
2899 return PTR_ERR(set
);
2900 if (set
->flags
& NFT_SET_INACTIVE
)
2902 if (!list_empty(&set
->bindings
))
2905 return nft_delset(&ctx
, set
);
2908 static int nf_tables_bind_check_setelem(const struct nft_ctx
*ctx
,
2909 const struct nft_set
*set
,
2910 const struct nft_set_iter
*iter
,
2911 const struct nft_set_elem
*elem
)
2913 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
2914 enum nft_registers dreg
;
2916 dreg
= nft_type_to_reg(set
->dtype
);
2917 return nft_validate_register_store(ctx
, dreg
, nft_set_ext_data(ext
),
2918 set
->dtype
== NFT_DATA_VERDICT
?
2919 NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
2923 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2924 struct nft_set_binding
*binding
)
2926 struct nft_set_binding
*i
;
2927 struct nft_set_iter iter
;
2929 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
)
2932 if (binding
->flags
& NFT_SET_MAP
) {
2933 /* If the set is already bound to the same chain all
2934 * jumps are already validated for that chain.
2936 list_for_each_entry(i
, &set
->bindings
, list
) {
2937 if (binding
->flags
& NFT_SET_MAP
&&
2938 i
->chain
== binding
->chain
)
2945 iter
.fn
= nf_tables_bind_check_setelem
;
2947 set
->ops
->walk(ctx
, set
, &iter
);
2949 /* Destroy anonymous sets if binding fails */
2950 if (set
->flags
& NFT_SET_ANONYMOUS
)
2951 nf_tables_set_destroy(ctx
, set
);
2957 binding
->chain
= ctx
->chain
;
2958 list_add_tail_rcu(&binding
->list
, &set
->bindings
);
2962 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2963 struct nft_set_binding
*binding
)
2965 list_del_rcu(&binding
->list
);
2967 if (list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
&&
2968 !(set
->flags
& NFT_SET_INACTIVE
))
2969 nf_tables_set_destroy(ctx
, set
);
2972 const struct nft_set_ext_type nft_set_ext_types
[] = {
2973 [NFT_SET_EXT_KEY
] = {
2974 .align
= __alignof__(u32
),
2976 [NFT_SET_EXT_DATA
] = {
2977 .align
= __alignof__(u32
),
2979 [NFT_SET_EXT_EXPR
] = {
2980 .align
= __alignof__(struct nft_expr
),
2982 [NFT_SET_EXT_FLAGS
] = {
2984 .align
= __alignof__(u8
),
2986 [NFT_SET_EXT_TIMEOUT
] = {
2988 .align
= __alignof__(u64
),
2990 [NFT_SET_EXT_EXPIRATION
] = {
2991 .len
= sizeof(unsigned long),
2992 .align
= __alignof__(unsigned long),
2994 [NFT_SET_EXT_USERDATA
] = {
2995 .len
= sizeof(struct nft_userdata
),
2996 .align
= __alignof__(struct nft_userdata
),
2999 EXPORT_SYMBOL_GPL(nft_set_ext_types
);
3005 static const struct nla_policy nft_set_elem_policy
[NFTA_SET_ELEM_MAX
+ 1] = {
3006 [NFTA_SET_ELEM_KEY
] = { .type
= NLA_NESTED
},
3007 [NFTA_SET_ELEM_DATA
] = { .type
= NLA_NESTED
},
3008 [NFTA_SET_ELEM_FLAGS
] = { .type
= NLA_U32
},
3009 [NFTA_SET_ELEM_TIMEOUT
] = { .type
= NLA_U64
},
3010 [NFTA_SET_ELEM_USERDATA
] = { .type
= NLA_BINARY
,
3011 .len
= NFT_USERDATA_MAXLEN
},
3014 static const struct nla_policy nft_set_elem_list_policy
[NFTA_SET_ELEM_LIST_MAX
+ 1] = {
3015 [NFTA_SET_ELEM_LIST_TABLE
] = { .type
= NLA_STRING
},
3016 [NFTA_SET_ELEM_LIST_SET
] = { .type
= NLA_STRING
},
3017 [NFTA_SET_ELEM_LIST_ELEMENTS
] = { .type
= NLA_NESTED
},
3018 [NFTA_SET_ELEM_LIST_SET_ID
] = { .type
= NLA_U32
},
3021 static int nft_ctx_init_from_elemattr(struct nft_ctx
*ctx
, struct net
*net
,
3022 const struct sk_buff
*skb
,
3023 const struct nlmsghdr
*nlh
,
3024 const struct nlattr
* const nla
[],
3027 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3028 struct nft_af_info
*afi
;
3029 struct nft_table
*table
;
3031 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, false);
3033 return PTR_ERR(afi
);
3035 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_ELEM_LIST_TABLE
]);
3037 return PTR_ERR(table
);
3038 if (!trans
&& (table
->flags
& NFT_TABLE_INACTIVE
))
3041 nft_ctx_init(ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
3045 static int nf_tables_fill_setelem(struct sk_buff
*skb
,
3046 const struct nft_set
*set
,
3047 const struct nft_set_elem
*elem
)
3049 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
3050 unsigned char *b
= skb_tail_pointer(skb
);
3051 struct nlattr
*nest
;
3053 nest
= nla_nest_start(skb
, NFTA_LIST_ELEM
);
3055 goto nla_put_failure
;
3057 if (nft_data_dump(skb
, NFTA_SET_ELEM_KEY
, nft_set_ext_key(ext
),
3058 NFT_DATA_VALUE
, set
->klen
) < 0)
3059 goto nla_put_failure
;
3061 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
) &&
3062 nft_data_dump(skb
, NFTA_SET_ELEM_DATA
, nft_set_ext_data(ext
),
3063 set
->dtype
== NFT_DATA_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
3065 goto nla_put_failure
;
3067 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
) &&
3068 nft_expr_dump(skb
, NFTA_SET_ELEM_EXPR
, nft_set_ext_expr(ext
)) < 0)
3069 goto nla_put_failure
;
3071 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
3072 nla_put_be32(skb
, NFTA_SET_ELEM_FLAGS
,
3073 htonl(*nft_set_ext_flags(ext
))))
3074 goto nla_put_failure
;
3076 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
) &&
3077 nla_put_be64(skb
, NFTA_SET_ELEM_TIMEOUT
,
3078 cpu_to_be64(*nft_set_ext_timeout(ext
))))
3079 goto nla_put_failure
;
3081 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
)) {
3082 unsigned long expires
, now
= jiffies
;
3084 expires
= *nft_set_ext_expiration(ext
);
3085 if (time_before(now
, expires
))
3090 if (nla_put_be64(skb
, NFTA_SET_ELEM_EXPIRATION
,
3091 cpu_to_be64(jiffies_to_msecs(expires
))))
3092 goto nla_put_failure
;
3095 if (nft_set_ext_exists(ext
, NFT_SET_EXT_USERDATA
)) {
3096 struct nft_userdata
*udata
;
3098 udata
= nft_set_ext_userdata(ext
);
3099 if (nla_put(skb
, NFTA_SET_ELEM_USERDATA
,
3100 udata
->len
+ 1, udata
->data
))
3101 goto nla_put_failure
;
3104 nla_nest_end(skb
, nest
);
3112 struct nft_set_dump_args
{
3113 const struct netlink_callback
*cb
;
3114 struct nft_set_iter iter
;
3115 struct sk_buff
*skb
;
3118 static int nf_tables_dump_setelem(const struct nft_ctx
*ctx
,
3119 const struct nft_set
*set
,
3120 const struct nft_set_iter
*iter
,
3121 const struct nft_set_elem
*elem
)
3123 struct nft_set_dump_args
*args
;
3125 args
= container_of(iter
, struct nft_set_dump_args
, iter
);
3126 return nf_tables_fill_setelem(args
->skb
, set
, elem
);
3129 static int nf_tables_dump_set(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3131 struct net
*net
= sock_net(skb
->sk
);
3132 const struct nft_set
*set
;
3133 struct nft_set_dump_args args
;
3135 struct nlattr
*nla
[NFTA_SET_ELEM_LIST_MAX
+ 1];
3136 struct nfgenmsg
*nfmsg
;
3137 struct nlmsghdr
*nlh
;
3138 struct nlattr
*nest
;
3142 err
= nlmsg_parse(cb
->nlh
, sizeof(struct nfgenmsg
), nla
,
3143 NFTA_SET_ELEM_LIST_MAX
, nft_set_elem_list_policy
);
3147 err
= nft_ctx_init_from_elemattr(&ctx
, net
, cb
->skb
, cb
->nlh
,
3148 (void *)nla
, false);
3152 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3154 return PTR_ERR(set
);
3155 if (set
->flags
& NFT_SET_INACTIVE
)
3158 event
= NFT_MSG_NEWSETELEM
;
3159 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
3160 portid
= NETLINK_CB(cb
->skb
).portid
;
3161 seq
= cb
->nlh
->nlmsg_seq
;
3163 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
3166 goto nla_put_failure
;
3168 nfmsg
= nlmsg_data(nlh
);
3169 nfmsg
->nfgen_family
= ctx
.afi
->family
;
3170 nfmsg
->version
= NFNETLINK_V0
;
3171 nfmsg
->res_id
= htons(ctx
.net
->nft
.base_seq
& 0xffff);
3173 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_TABLE
, ctx
.table
->name
))
3174 goto nla_put_failure
;
3175 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_SET
, set
->name
))
3176 goto nla_put_failure
;
3178 nest
= nla_nest_start(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
3180 goto nla_put_failure
;
3184 args
.iter
.skip
= cb
->args
[0];
3185 args
.iter
.count
= 0;
3187 args
.iter
.fn
= nf_tables_dump_setelem
;
3188 set
->ops
->walk(&ctx
, set
, &args
.iter
);
3190 nla_nest_end(skb
, nest
);
3191 nlmsg_end(skb
, nlh
);
3193 if (args
.iter
.err
&& args
.iter
.err
!= -EMSGSIZE
)
3194 return args
.iter
.err
;
3195 if (args
.iter
.count
== cb
->args
[0])
3198 cb
->args
[0] = args
.iter
.count
;
3205 static int nf_tables_getsetelem(struct sock
*nlsk
, struct sk_buff
*skb
,
3206 const struct nlmsghdr
*nlh
,
3207 const struct nlattr
* const nla
[])
3209 struct net
*net
= sock_net(skb
->sk
);
3210 const struct nft_set
*set
;
3214 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
, false);
3218 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3220 return PTR_ERR(set
);
3221 if (set
->flags
& NFT_SET_INACTIVE
)
3224 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3225 struct netlink_dump_control c
= {
3226 .dump
= nf_tables_dump_set
,
3228 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
3233 static int nf_tables_fill_setelem_info(struct sk_buff
*skb
,
3234 const struct nft_ctx
*ctx
, u32 seq
,
3235 u32 portid
, int event
, u16 flags
,
3236 const struct nft_set
*set
,
3237 const struct nft_set_elem
*elem
)
3239 struct nfgenmsg
*nfmsg
;
3240 struct nlmsghdr
*nlh
;
3241 struct nlattr
*nest
;
3244 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
3245 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
3248 goto nla_put_failure
;
3250 nfmsg
= nlmsg_data(nlh
);
3251 nfmsg
->nfgen_family
= ctx
->afi
->family
;
3252 nfmsg
->version
= NFNETLINK_V0
;
3253 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
3255 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
3256 goto nla_put_failure
;
3257 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
3258 goto nla_put_failure
;
3260 nest
= nla_nest_start(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
3262 goto nla_put_failure
;
3264 err
= nf_tables_fill_setelem(skb
, set
, elem
);
3266 goto nla_put_failure
;
3268 nla_nest_end(skb
, nest
);
3270 nlmsg_end(skb
, nlh
);
3274 nlmsg_trim(skb
, nlh
);
3278 static int nf_tables_setelem_notify(const struct nft_ctx
*ctx
,
3279 const struct nft_set
*set
,
3280 const struct nft_set_elem
*elem
,
3281 int event
, u16 flags
)
3283 struct net
*net
= ctx
->net
;
3284 u32 portid
= ctx
->portid
;
3285 struct sk_buff
*skb
;
3288 if (!ctx
->report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
3292 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
3296 err
= nf_tables_fill_setelem_info(skb
, ctx
, 0, portid
, event
, flags
,
3303 err
= nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, ctx
->report
,
3307 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, err
);
3311 static struct nft_trans
*nft_trans_elem_alloc(struct nft_ctx
*ctx
,
3313 struct nft_set
*set
)
3315 struct nft_trans
*trans
;
3317 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_elem
));
3321 nft_trans_elem_set(trans
) = set
;
3325 void *nft_set_elem_init(const struct nft_set
*set
,
3326 const struct nft_set_ext_tmpl
*tmpl
,
3327 const u32
*key
, const u32
*data
,
3328 u64 timeout
, gfp_t gfp
)
3330 struct nft_set_ext
*ext
;
3333 elem
= kzalloc(set
->ops
->elemsize
+ tmpl
->len
, gfp
);
3337 ext
= nft_set_elem_ext(set
, elem
);
3338 nft_set_ext_init(ext
, tmpl
);
3340 memcpy(nft_set_ext_key(ext
), key
, set
->klen
);
3341 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
3342 memcpy(nft_set_ext_data(ext
), data
, set
->dlen
);
3343 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
))
3344 *nft_set_ext_expiration(ext
) =
3345 jiffies
+ msecs_to_jiffies(timeout
);
3346 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
))
3347 *nft_set_ext_timeout(ext
) = timeout
;
3352 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
)
3354 struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
);
3356 nft_data_uninit(nft_set_ext_key(ext
), NFT_DATA_VALUE
);
3357 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
3358 nft_data_uninit(nft_set_ext_data(ext
), set
->dtype
);
3359 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
))
3360 nf_tables_expr_destroy(NULL
, nft_set_ext_expr(ext
));
3364 EXPORT_SYMBOL_GPL(nft_set_elem_destroy
);
3366 static int nft_add_set_elem(struct nft_ctx
*ctx
, struct nft_set
*set
,
3367 const struct nlattr
*attr
)
3369 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
3370 struct nft_data_desc d1
, d2
;
3371 struct nft_set_ext_tmpl tmpl
;
3372 struct nft_set_ext
*ext
;
3373 struct nft_set_elem elem
;
3374 struct nft_set_binding
*binding
;
3375 struct nft_userdata
*udata
;
3376 struct nft_data data
;
3377 enum nft_registers dreg
;
3378 struct nft_trans
*trans
;
3384 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
3385 nft_set_elem_policy
);
3389 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
3392 nft_set_ext_prepare(&tmpl
);
3395 if (nla
[NFTA_SET_ELEM_FLAGS
] != NULL
) {
3396 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_ELEM_FLAGS
]));
3397 if (flags
& ~NFT_SET_ELEM_INTERVAL_END
)
3399 if (!(set
->flags
& NFT_SET_INTERVAL
) &&
3400 flags
& NFT_SET_ELEM_INTERVAL_END
)
3403 nft_set_ext_add(&tmpl
, NFT_SET_EXT_FLAGS
);
3406 if (set
->flags
& NFT_SET_MAP
) {
3407 if (nla
[NFTA_SET_ELEM_DATA
] == NULL
&&
3408 !(flags
& NFT_SET_ELEM_INTERVAL_END
))
3410 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
&&
3411 flags
& NFT_SET_ELEM_INTERVAL_END
)
3414 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
3419 if (nla
[NFTA_SET_ELEM_TIMEOUT
] != NULL
) {
3420 if (!(set
->flags
& NFT_SET_TIMEOUT
))
3422 timeout
= be64_to_cpu(nla_get_be64(nla
[NFTA_SET_ELEM_TIMEOUT
]));
3423 } else if (set
->flags
& NFT_SET_TIMEOUT
) {
3424 timeout
= set
->timeout
;
3427 err
= nft_data_init(ctx
, &elem
.key
.val
, sizeof(elem
.key
), &d1
,
3428 nla
[NFTA_SET_ELEM_KEY
]);
3432 if (d1
.type
!= NFT_DATA_VALUE
|| d1
.len
!= set
->klen
)
3435 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_KEY
, d1
.len
);
3437 nft_set_ext_add(&tmpl
, NFT_SET_EXT_EXPIRATION
);
3438 if (timeout
!= set
->timeout
)
3439 nft_set_ext_add(&tmpl
, NFT_SET_EXT_TIMEOUT
);
3442 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
) {
3443 err
= nft_data_init(ctx
, &data
, sizeof(data
), &d2
,
3444 nla
[NFTA_SET_ELEM_DATA
]);
3449 if (set
->dtype
!= NFT_DATA_VERDICT
&& d2
.len
!= set
->dlen
)
3452 dreg
= nft_type_to_reg(set
->dtype
);
3453 list_for_each_entry(binding
, &set
->bindings
, list
) {
3454 struct nft_ctx bind_ctx
= {
3456 .table
= ctx
->table
,
3457 .chain
= (struct nft_chain
*)binding
->chain
,
3460 if (!(binding
->flags
& NFT_SET_MAP
))
3463 err
= nft_validate_register_store(&bind_ctx
, dreg
,
3470 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_DATA
, d2
.len
);
3473 /* The full maximum length of userdata can exceed the maximum
3474 * offset value (U8_MAX) for following extensions, therefor it
3475 * must be the last extension added.
3478 if (nla
[NFTA_SET_ELEM_USERDATA
] != NULL
) {
3479 ulen
= nla_len(nla
[NFTA_SET_ELEM_USERDATA
]);
3481 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_USERDATA
,
3486 elem
.priv
= nft_set_elem_init(set
, &tmpl
, elem
.key
.val
.data
, data
.data
,
3487 timeout
, GFP_KERNEL
);
3488 if (elem
.priv
== NULL
)
3491 ext
= nft_set_elem_ext(set
, elem
.priv
);
3493 *nft_set_ext_flags(ext
) = flags
;
3495 udata
= nft_set_ext_userdata(ext
);
3496 udata
->len
= ulen
- 1;
3497 nla_memcpy(&udata
->data
, nla
[NFTA_SET_ELEM_USERDATA
], ulen
);
3500 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_NEWSETELEM
, set
);
3504 ext
->genmask
= nft_genmask_cur(ctx
->net
) | NFT_SET_ELEM_BUSY_MASK
;
3505 err
= set
->ops
->insert(set
, &elem
);
3509 nft_trans_elem(trans
) = elem
;
3510 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
3518 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
3519 nft_data_uninit(&data
, d2
.type
);
3521 nft_data_uninit(&elem
.key
.val
, d1
.type
);
3526 static int nf_tables_newsetelem(struct net
*net
, struct sock
*nlsk
,
3527 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3528 const struct nlattr
* const nla
[])
3530 const struct nlattr
*attr
;
3531 struct nft_set
*set
;
3535 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
)
3538 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
, true);
3542 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3544 if (nla
[NFTA_SET_ELEM_LIST_SET_ID
]) {
3545 set
= nf_tables_set_lookup_byid(net
,
3546 nla
[NFTA_SET_ELEM_LIST_SET_ID
]);
3549 return PTR_ERR(set
);
3552 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
3555 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
3557 !atomic_add_unless(&set
->nelems
, 1, set
->size
+ set
->ndeact
))
3560 err
= nft_add_set_elem(&ctx
, set
, attr
);
3562 atomic_dec(&set
->nelems
);
3569 static int nft_del_setelem(struct nft_ctx
*ctx
, struct nft_set
*set
,
3570 const struct nlattr
*attr
)
3572 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
3573 struct nft_data_desc desc
;
3574 struct nft_set_elem elem
;
3575 struct nft_trans
*trans
;
3578 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
3579 nft_set_elem_policy
);
3584 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
3587 err
= nft_data_init(ctx
, &elem
.key
.val
, sizeof(elem
.key
), &desc
,
3588 nla
[NFTA_SET_ELEM_KEY
]);
3593 if (desc
.type
!= NFT_DATA_VALUE
|| desc
.len
!= set
->klen
)
3596 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_DELSETELEM
, set
);
3597 if (trans
== NULL
) {
3602 elem
.priv
= set
->ops
->deactivate(set
, &elem
);
3603 if (elem
.priv
== NULL
) {
3608 nft_trans_elem(trans
) = elem
;
3609 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
3615 nft_data_uninit(&elem
.key
.val
, desc
.type
);
3620 static int nf_tables_delsetelem(struct net
*net
, struct sock
*nlsk
,
3621 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3622 const struct nlattr
* const nla
[])
3624 const struct nlattr
*attr
;
3625 struct nft_set
*set
;
3629 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
)
3632 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
, false);
3636 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3638 return PTR_ERR(set
);
3639 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
3642 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
3643 err
= nft_del_setelem(&ctx
, set
, attr
);
3652 void nft_set_gc_batch_release(struct rcu_head
*rcu
)
3654 struct nft_set_gc_batch
*gcb
;
3657 gcb
= container_of(rcu
, struct nft_set_gc_batch
, head
.rcu
);
3658 for (i
= 0; i
< gcb
->head
.cnt
; i
++)
3659 nft_set_elem_destroy(gcb
->head
.set
, gcb
->elems
[i
]);
3662 EXPORT_SYMBOL_GPL(nft_set_gc_batch_release
);
3664 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
3667 struct nft_set_gc_batch
*gcb
;
3669 gcb
= kzalloc(sizeof(*gcb
), gfp
);
3672 gcb
->head
.set
= set
;
3675 EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc
);
3677 static int nf_tables_fill_gen_info(struct sk_buff
*skb
, struct net
*net
,
3678 u32 portid
, u32 seq
)
3680 struct nlmsghdr
*nlh
;
3681 struct nfgenmsg
*nfmsg
;
3682 int event
= (NFNL_SUBSYS_NFTABLES
<< 8) | NFT_MSG_NEWGEN
;
3684 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), 0);
3686 goto nla_put_failure
;
3688 nfmsg
= nlmsg_data(nlh
);
3689 nfmsg
->nfgen_family
= AF_UNSPEC
;
3690 nfmsg
->version
= NFNETLINK_V0
;
3691 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
3693 if (nla_put_be32(skb
, NFTA_GEN_ID
, htonl(net
->nft
.base_seq
)))
3694 goto nla_put_failure
;
3696 nlmsg_end(skb
, nlh
);
3700 nlmsg_trim(skb
, nlh
);
3704 static int nf_tables_gen_notify(struct net
*net
, struct sk_buff
*skb
, int event
)
3706 struct nlmsghdr
*nlh
= nlmsg_hdr(skb
);
3707 struct sk_buff
*skb2
;
3710 if (nlmsg_report(nlh
) &&
3711 !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
3715 skb2
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
3719 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
3726 err
= nfnetlink_send(skb2
, net
, NETLINK_CB(skb
).portid
,
3727 NFNLGRP_NFTABLES
, nlmsg_report(nlh
), GFP_KERNEL
);
3730 nfnetlink_set_err(net
, NETLINK_CB(skb
).portid
, NFNLGRP_NFTABLES
,
3736 static int nf_tables_getgen(struct sock
*nlsk
, struct sk_buff
*skb
,
3737 const struct nlmsghdr
*nlh
,
3738 const struct nlattr
* const nla
[])
3740 struct net
*net
= sock_net(skb
->sk
);
3741 struct sk_buff
*skb2
;
3744 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
3748 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
3753 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
3759 static const struct nfnl_callback nf_tables_cb
[NFT_MSG_MAX
] = {
3760 [NFT_MSG_NEWTABLE
] = {
3761 .call_batch
= nf_tables_newtable
,
3762 .attr_count
= NFTA_TABLE_MAX
,
3763 .policy
= nft_table_policy
,
3765 [NFT_MSG_GETTABLE
] = {
3766 .call
= nf_tables_gettable
,
3767 .attr_count
= NFTA_TABLE_MAX
,
3768 .policy
= nft_table_policy
,
3770 [NFT_MSG_DELTABLE
] = {
3771 .call_batch
= nf_tables_deltable
,
3772 .attr_count
= NFTA_TABLE_MAX
,
3773 .policy
= nft_table_policy
,
3775 [NFT_MSG_NEWCHAIN
] = {
3776 .call_batch
= nf_tables_newchain
,
3777 .attr_count
= NFTA_CHAIN_MAX
,
3778 .policy
= nft_chain_policy
,
3780 [NFT_MSG_GETCHAIN
] = {
3781 .call
= nf_tables_getchain
,
3782 .attr_count
= NFTA_CHAIN_MAX
,
3783 .policy
= nft_chain_policy
,
3785 [NFT_MSG_DELCHAIN
] = {
3786 .call_batch
= nf_tables_delchain
,
3787 .attr_count
= NFTA_CHAIN_MAX
,
3788 .policy
= nft_chain_policy
,
3790 [NFT_MSG_NEWRULE
] = {
3791 .call_batch
= nf_tables_newrule
,
3792 .attr_count
= NFTA_RULE_MAX
,
3793 .policy
= nft_rule_policy
,
3795 [NFT_MSG_GETRULE
] = {
3796 .call
= nf_tables_getrule
,
3797 .attr_count
= NFTA_RULE_MAX
,
3798 .policy
= nft_rule_policy
,
3800 [NFT_MSG_DELRULE
] = {
3801 .call_batch
= nf_tables_delrule
,
3802 .attr_count
= NFTA_RULE_MAX
,
3803 .policy
= nft_rule_policy
,
3805 [NFT_MSG_NEWSET
] = {
3806 .call_batch
= nf_tables_newset
,
3807 .attr_count
= NFTA_SET_MAX
,
3808 .policy
= nft_set_policy
,
3810 [NFT_MSG_GETSET
] = {
3811 .call
= nf_tables_getset
,
3812 .attr_count
= NFTA_SET_MAX
,
3813 .policy
= nft_set_policy
,
3815 [NFT_MSG_DELSET
] = {
3816 .call_batch
= nf_tables_delset
,
3817 .attr_count
= NFTA_SET_MAX
,
3818 .policy
= nft_set_policy
,
3820 [NFT_MSG_NEWSETELEM
] = {
3821 .call_batch
= nf_tables_newsetelem
,
3822 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
3823 .policy
= nft_set_elem_list_policy
,
3825 [NFT_MSG_GETSETELEM
] = {
3826 .call
= nf_tables_getsetelem
,
3827 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
3828 .policy
= nft_set_elem_list_policy
,
3830 [NFT_MSG_DELSETELEM
] = {
3831 .call_batch
= nf_tables_delsetelem
,
3832 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
3833 .policy
= nft_set_elem_list_policy
,
3835 [NFT_MSG_GETGEN
] = {
3836 .call
= nf_tables_getgen
,
3840 static void nft_chain_commit_update(struct nft_trans
*trans
)
3842 struct nft_base_chain
*basechain
;
3844 if (nft_trans_chain_name(trans
)[0])
3845 strcpy(trans
->ctx
.chain
->name
, nft_trans_chain_name(trans
));
3847 if (!(trans
->ctx
.chain
->flags
& NFT_BASE_CHAIN
))
3850 basechain
= nft_base_chain(trans
->ctx
.chain
);
3851 nft_chain_stats_replace(basechain
, nft_trans_chain_stats(trans
));
3853 switch (nft_trans_chain_policy(trans
)) {
3856 basechain
->policy
= nft_trans_chain_policy(trans
);
3861 static void nf_tables_commit_release(struct nft_trans
*trans
)
3863 switch (trans
->msg_type
) {
3864 case NFT_MSG_DELTABLE
:
3865 nf_tables_table_destroy(&trans
->ctx
);
3867 case NFT_MSG_DELCHAIN
:
3868 nf_tables_chain_destroy(trans
->ctx
.chain
);
3870 case NFT_MSG_DELRULE
:
3871 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
3873 case NFT_MSG_DELSET
:
3874 nft_set_destroy(nft_trans_set(trans
));
3876 case NFT_MSG_DELSETELEM
:
3877 nft_set_elem_destroy(nft_trans_elem_set(trans
),
3878 nft_trans_elem(trans
).priv
);
3884 static int nf_tables_commit(struct sk_buff
*skb
)
3886 struct net
*net
= sock_net(skb
->sk
);
3887 struct nft_trans
*trans
, *next
;
3888 struct nft_trans_elem
*te
;
3890 /* Bump generation counter, invalidate any dump in progress */
3891 while (++net
->nft
.base_seq
== 0);
3893 /* A new generation has just started */
3894 net
->nft
.gencursor
= nft_gencursor_next(net
);
3896 /* Make sure all packets have left the previous generation before
3897 * purging old rules.
3901 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
3902 switch (trans
->msg_type
) {
3903 case NFT_MSG_NEWTABLE
:
3904 if (nft_trans_table_update(trans
)) {
3905 if (!nft_trans_table_enable(trans
)) {
3906 nf_tables_table_disable(trans
->ctx
.afi
,
3908 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
3911 trans
->ctx
.table
->flags
&= ~NFT_TABLE_INACTIVE
;
3913 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_NEWTABLE
);
3914 nft_trans_destroy(trans
);
3916 case NFT_MSG_DELTABLE
:
3917 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_DELTABLE
);
3919 case NFT_MSG_NEWCHAIN
:
3920 if (nft_trans_chain_update(trans
))
3921 nft_chain_commit_update(trans
);
3923 trans
->ctx
.chain
->flags
&= ~NFT_CHAIN_INACTIVE
;
3925 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_NEWCHAIN
);
3926 nft_trans_destroy(trans
);
3928 case NFT_MSG_DELCHAIN
:
3929 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_DELCHAIN
);
3930 nf_tables_unregister_hooks(trans
->ctx
.table
,
3932 trans
->ctx
.afi
->nops
);
3934 case NFT_MSG_NEWRULE
:
3935 nft_rule_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
3936 nf_tables_rule_notify(&trans
->ctx
,
3937 nft_trans_rule(trans
),
3939 nft_trans_destroy(trans
);
3941 case NFT_MSG_DELRULE
:
3942 list_del_rcu(&nft_trans_rule(trans
)->list
);
3943 nf_tables_rule_notify(&trans
->ctx
,
3944 nft_trans_rule(trans
),
3947 case NFT_MSG_NEWSET
:
3948 nft_trans_set(trans
)->flags
&= ~NFT_SET_INACTIVE
;
3949 /* This avoids hitting -EBUSY when deleting the table
3950 * from the transaction.
3952 if (nft_trans_set(trans
)->flags
& NFT_SET_ANONYMOUS
&&
3953 !list_empty(&nft_trans_set(trans
)->bindings
))
3954 trans
->ctx
.table
->use
--;
3956 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
3957 NFT_MSG_NEWSET
, GFP_KERNEL
);
3958 nft_trans_destroy(trans
);
3960 case NFT_MSG_DELSET
:
3961 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
3962 NFT_MSG_DELSET
, GFP_KERNEL
);
3964 case NFT_MSG_NEWSETELEM
:
3965 te
= (struct nft_trans_elem
*)trans
->data
;
3967 te
->set
->ops
->activate(te
->set
, &te
->elem
);
3968 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
3970 NFT_MSG_NEWSETELEM
, 0);
3971 nft_trans_destroy(trans
);
3973 case NFT_MSG_DELSETELEM
:
3974 te
= (struct nft_trans_elem
*)trans
->data
;
3976 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
3978 NFT_MSG_DELSETELEM
, 0);
3979 te
->set
->ops
->remove(te
->set
, &te
->elem
);
3980 atomic_dec(&te
->set
->nelems
);
3988 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
3989 list_del(&trans
->list
);
3990 nf_tables_commit_release(trans
);
3993 nf_tables_gen_notify(net
, skb
, NFT_MSG_NEWGEN
);
3998 static void nf_tables_abort_release(struct nft_trans
*trans
)
4000 switch (trans
->msg_type
) {
4001 case NFT_MSG_NEWTABLE
:
4002 nf_tables_table_destroy(&trans
->ctx
);
4004 case NFT_MSG_NEWCHAIN
:
4005 nf_tables_chain_destroy(trans
->ctx
.chain
);
4007 case NFT_MSG_NEWRULE
:
4008 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
4010 case NFT_MSG_NEWSET
:
4011 nft_set_destroy(nft_trans_set(trans
));
4013 case NFT_MSG_NEWSETELEM
:
4014 nft_set_elem_destroy(nft_trans_elem_set(trans
),
4015 nft_trans_elem(trans
).priv
);
4021 static int nf_tables_abort(struct sk_buff
*skb
)
4023 struct net
*net
= sock_net(skb
->sk
);
4024 struct nft_trans
*trans
, *next
;
4025 struct nft_trans_elem
*te
;
4027 list_for_each_entry_safe_reverse(trans
, next
, &net
->nft
.commit_list
,
4029 switch (trans
->msg_type
) {
4030 case NFT_MSG_NEWTABLE
:
4031 if (nft_trans_table_update(trans
)) {
4032 if (nft_trans_table_enable(trans
)) {
4033 nf_tables_table_disable(trans
->ctx
.afi
,
4035 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
4037 nft_trans_destroy(trans
);
4039 list_del_rcu(&trans
->ctx
.table
->list
);
4042 case NFT_MSG_DELTABLE
:
4043 list_add_tail_rcu(&trans
->ctx
.table
->list
,
4044 &trans
->ctx
.afi
->tables
);
4045 nft_trans_destroy(trans
);
4047 case NFT_MSG_NEWCHAIN
:
4048 if (nft_trans_chain_update(trans
)) {
4049 free_percpu(nft_trans_chain_stats(trans
));
4051 nft_trans_destroy(trans
);
4053 trans
->ctx
.table
->use
--;
4054 list_del_rcu(&trans
->ctx
.chain
->list
);
4055 nf_tables_unregister_hooks(trans
->ctx
.table
,
4057 trans
->ctx
.afi
->nops
);
4060 case NFT_MSG_DELCHAIN
:
4061 trans
->ctx
.table
->use
++;
4062 list_add_tail_rcu(&trans
->ctx
.chain
->list
,
4063 &trans
->ctx
.table
->chains
);
4064 nft_trans_destroy(trans
);
4066 case NFT_MSG_NEWRULE
:
4067 trans
->ctx
.chain
->use
--;
4068 list_del_rcu(&nft_trans_rule(trans
)->list
);
4070 case NFT_MSG_DELRULE
:
4071 trans
->ctx
.chain
->use
++;
4072 nft_rule_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
4073 nft_trans_destroy(trans
);
4075 case NFT_MSG_NEWSET
:
4076 trans
->ctx
.table
->use
--;
4077 list_del_rcu(&nft_trans_set(trans
)->list
);
4079 case NFT_MSG_DELSET
:
4080 trans
->ctx
.table
->use
++;
4081 list_add_tail_rcu(&nft_trans_set(trans
)->list
,
4082 &trans
->ctx
.table
->sets
);
4083 nft_trans_destroy(trans
);
4085 case NFT_MSG_NEWSETELEM
:
4086 te
= (struct nft_trans_elem
*)trans
->data
;
4088 te
->set
->ops
->remove(te
->set
, &te
->elem
);
4089 atomic_dec(&te
->set
->nelems
);
4091 case NFT_MSG_DELSETELEM
:
4092 te
= (struct nft_trans_elem
*)trans
->data
;
4094 te
->set
->ops
->activate(te
->set
, &te
->elem
);
4097 nft_trans_destroy(trans
);
4104 list_for_each_entry_safe_reverse(trans
, next
,
4105 &net
->nft
.commit_list
, list
) {
4106 list_del(&trans
->list
);
4107 nf_tables_abort_release(trans
);
4113 static const struct nfnetlink_subsystem nf_tables_subsys
= {
4114 .name
= "nf_tables",
4115 .subsys_id
= NFNL_SUBSYS_NFTABLES
,
4116 .cb_count
= NFT_MSG_MAX
,
4118 .commit
= nf_tables_commit
,
4119 .abort
= nf_tables_abort
,
4122 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
4123 enum nft_chain_type type
)
4125 const struct nft_base_chain
*basechain
;
4127 if (chain
->flags
& NFT_BASE_CHAIN
) {
4128 basechain
= nft_base_chain(chain
);
4129 if (basechain
->type
->type
!= type
)
4134 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency
);
4136 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
4137 unsigned int hook_flags
)
4139 struct nft_base_chain
*basechain
;
4141 if (chain
->flags
& NFT_BASE_CHAIN
) {
4142 basechain
= nft_base_chain(chain
);
4144 if ((1 << basechain
->ops
[0].hooknum
) & hook_flags
)
4152 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks
);
4155 * Loop detection - walk through the ruleset beginning at the destination chain
4156 * of a new jump until either the source chain is reached (loop) or all
4157 * reachable chains have been traversed.
4159 * The loop check is performed whenever a new jump verdict is added to an
4160 * expression or verdict map or a verdict map is bound to a new chain.
4163 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
4164 const struct nft_chain
*chain
);
4166 static int nf_tables_loop_check_setelem(const struct nft_ctx
*ctx
,
4167 const struct nft_set
*set
,
4168 const struct nft_set_iter
*iter
,
4169 const struct nft_set_elem
*elem
)
4171 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
4172 const struct nft_data
*data
;
4174 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
4175 *nft_set_ext_flags(ext
) & NFT_SET_ELEM_INTERVAL_END
)
4178 data
= nft_set_ext_data(ext
);
4179 switch (data
->verdict
.code
) {
4182 return nf_tables_check_loops(ctx
, data
->verdict
.chain
);
4188 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
4189 const struct nft_chain
*chain
)
4191 const struct nft_rule
*rule
;
4192 const struct nft_expr
*expr
, *last
;
4193 const struct nft_set
*set
;
4194 struct nft_set_binding
*binding
;
4195 struct nft_set_iter iter
;
4197 if (ctx
->chain
== chain
)
4200 list_for_each_entry(rule
, &chain
->rules
, list
) {
4201 nft_rule_for_each_expr(expr
, last
, rule
) {
4202 const struct nft_data
*data
= NULL
;
4205 if (!expr
->ops
->validate
)
4208 err
= expr
->ops
->validate(ctx
, expr
, &data
);
4215 switch (data
->verdict
.code
) {
4218 err
= nf_tables_check_loops(ctx
,
4219 data
->verdict
.chain
);
4228 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
4229 if (!(set
->flags
& NFT_SET_MAP
) ||
4230 set
->dtype
!= NFT_DATA_VERDICT
)
4233 list_for_each_entry(binding
, &set
->bindings
, list
) {
4234 if (!(binding
->flags
& NFT_SET_MAP
) ||
4235 binding
->chain
!= chain
)
4241 iter
.fn
= nf_tables_loop_check_setelem
;
4243 set
->ops
->walk(ctx
, set
, &iter
);
4253 * nft_parse_register - parse a register value from a netlink attribute
4255 * @attr: netlink attribute
4257 * Parse and translate a register value from a netlink attribute.
4258 * Registers used to be 128 bit wide, these register numbers will be
4259 * mapped to the corresponding 32 bit register numbers.
4261 unsigned int nft_parse_register(const struct nlattr
*attr
)
4265 reg
= ntohl(nla_get_be32(attr
));
4267 case NFT_REG_VERDICT
...NFT_REG_4
:
4268 return reg
* NFT_REG_SIZE
/ NFT_REG32_SIZE
;
4270 return reg
+ NFT_REG_SIZE
/ NFT_REG32_SIZE
- NFT_REG32_00
;
4273 EXPORT_SYMBOL_GPL(nft_parse_register
);
4276 * nft_dump_register - dump a register value to a netlink attribute
4278 * @skb: socket buffer
4279 * @attr: attribute number
4280 * @reg: register number
4282 * Construct a netlink attribute containing the register number. For
4283 * compatibility reasons, register numbers being a multiple of 4 are
4284 * translated to the corresponding 128 bit register numbers.
4286 int nft_dump_register(struct sk_buff
*skb
, unsigned int attr
, unsigned int reg
)
4288 if (reg
% (NFT_REG_SIZE
/ NFT_REG32_SIZE
) == 0)
4289 reg
= reg
/ (NFT_REG_SIZE
/ NFT_REG32_SIZE
);
4291 reg
= reg
- NFT_REG_SIZE
/ NFT_REG32_SIZE
+ NFT_REG32_00
;
4293 return nla_put_be32(skb
, attr
, htonl(reg
));
4295 EXPORT_SYMBOL_GPL(nft_dump_register
);
4298 * nft_validate_register_load - validate a load from a register
4300 * @reg: the register number
4301 * @len: the length of the data
4303 * Validate that the input register is one of the general purpose
4304 * registers and that the length of the load is within the bounds.
4306 int nft_validate_register_load(enum nft_registers reg
, unsigned int len
)
4308 if (reg
< NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
)
4312 if (reg
* NFT_REG32_SIZE
+ len
> FIELD_SIZEOF(struct nft_regs
, data
))
4317 EXPORT_SYMBOL_GPL(nft_validate_register_load
);
4320 * nft_validate_register_store - validate an expressions' register store
4322 * @ctx: context of the expression performing the load
4323 * @reg: the destination register number
4324 * @data: the data to load
4325 * @type: the data type
4326 * @len: the length of the data
4328 * Validate that a data load uses the appropriate data type for
4329 * the destination register and the length is within the bounds.
4330 * A value of NULL for the data means that its runtime gathered
4333 int nft_validate_register_store(const struct nft_ctx
*ctx
,
4334 enum nft_registers reg
,
4335 const struct nft_data
*data
,
4336 enum nft_data_types type
, unsigned int len
)
4341 case NFT_REG_VERDICT
:
4342 if (type
!= NFT_DATA_VERDICT
)
4346 (data
->verdict
.code
== NFT_GOTO
||
4347 data
->verdict
.code
== NFT_JUMP
)) {
4348 err
= nf_tables_check_loops(ctx
, data
->verdict
.chain
);
4352 if (ctx
->chain
->level
+ 1 >
4353 data
->verdict
.chain
->level
) {
4354 if (ctx
->chain
->level
+ 1 == NFT_JUMP_STACK_SIZE
)
4356 data
->verdict
.chain
->level
= ctx
->chain
->level
+ 1;
4362 if (reg
< NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
)
4366 if (reg
* NFT_REG32_SIZE
+ len
>
4367 FIELD_SIZEOF(struct nft_regs
, data
))
4370 if (data
!= NULL
&& type
!= NFT_DATA_VALUE
)
4375 EXPORT_SYMBOL_GPL(nft_validate_register_store
);
4377 static const struct nla_policy nft_verdict_policy
[NFTA_VERDICT_MAX
+ 1] = {
4378 [NFTA_VERDICT_CODE
] = { .type
= NLA_U32
},
4379 [NFTA_VERDICT_CHAIN
] = { .type
= NLA_STRING
,
4380 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
4383 static int nft_verdict_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
4384 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
4386 struct nlattr
*tb
[NFTA_VERDICT_MAX
+ 1];
4387 struct nft_chain
*chain
;
4390 err
= nla_parse_nested(tb
, NFTA_VERDICT_MAX
, nla
, nft_verdict_policy
);
4394 if (!tb
[NFTA_VERDICT_CODE
])
4396 data
->verdict
.code
= ntohl(nla_get_be32(tb
[NFTA_VERDICT_CODE
]));
4398 switch (data
->verdict
.code
) {
4400 switch (data
->verdict
.code
& NF_VERDICT_MASK
) {
4415 if (!tb
[NFTA_VERDICT_CHAIN
])
4417 chain
= nf_tables_chain_lookup(ctx
->table
,
4418 tb
[NFTA_VERDICT_CHAIN
]);
4420 return PTR_ERR(chain
);
4421 if (chain
->flags
& NFT_BASE_CHAIN
)
4425 data
->verdict
.chain
= chain
;
4429 desc
->len
= sizeof(data
->verdict
);
4430 desc
->type
= NFT_DATA_VERDICT
;
4434 static void nft_verdict_uninit(const struct nft_data
*data
)
4436 switch (data
->verdict
.code
) {
4439 data
->verdict
.chain
->use
--;
4444 static int nft_verdict_dump(struct sk_buff
*skb
, const struct nft_data
*data
)
4446 struct nlattr
*nest
;
4448 nest
= nla_nest_start(skb
, NFTA_DATA_VERDICT
);
4450 goto nla_put_failure
;
4452 if (nla_put_be32(skb
, NFTA_VERDICT_CODE
, htonl(data
->verdict
.code
)))
4453 goto nla_put_failure
;
4455 switch (data
->verdict
.code
) {
4458 if (nla_put_string(skb
, NFTA_VERDICT_CHAIN
,
4459 data
->verdict
.chain
->name
))
4460 goto nla_put_failure
;
4462 nla_nest_end(skb
, nest
);
4469 static int nft_value_init(const struct nft_ctx
*ctx
,
4470 struct nft_data
*data
, unsigned int size
,
4471 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
4481 nla_memcpy(data
->data
, nla
, len
);
4482 desc
->type
= NFT_DATA_VALUE
;
4487 static int nft_value_dump(struct sk_buff
*skb
, const struct nft_data
*data
,
4490 return nla_put(skb
, NFTA_DATA_VALUE
, len
, data
->data
);
4493 static const struct nla_policy nft_data_policy
[NFTA_DATA_MAX
+ 1] = {
4494 [NFTA_DATA_VALUE
] = { .type
= NLA_BINARY
},
4495 [NFTA_DATA_VERDICT
] = { .type
= NLA_NESTED
},
4499 * nft_data_init - parse nf_tables data netlink attributes
4501 * @ctx: context of the expression using the data
4502 * @data: destination struct nft_data
4503 * @size: maximum data length
4504 * @desc: data description
4505 * @nla: netlink attribute containing data
4507 * Parse the netlink data attributes and initialize a struct nft_data.
4508 * The type and length of data are returned in the data description.
4510 * The caller can indicate that it only wants to accept data of type
4511 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4513 int nft_data_init(const struct nft_ctx
*ctx
,
4514 struct nft_data
*data
, unsigned int size
,
4515 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
4517 struct nlattr
*tb
[NFTA_DATA_MAX
+ 1];
4520 err
= nla_parse_nested(tb
, NFTA_DATA_MAX
, nla
, nft_data_policy
);
4524 if (tb
[NFTA_DATA_VALUE
])
4525 return nft_value_init(ctx
, data
, size
, desc
,
4526 tb
[NFTA_DATA_VALUE
]);
4527 if (tb
[NFTA_DATA_VERDICT
] && ctx
!= NULL
)
4528 return nft_verdict_init(ctx
, data
, desc
, tb
[NFTA_DATA_VERDICT
]);
4531 EXPORT_SYMBOL_GPL(nft_data_init
);
4534 * nft_data_uninit - release a nft_data item
4536 * @data: struct nft_data to release
4537 * @type: type of data
4539 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4540 * all others need to be released by calling this function.
4542 void nft_data_uninit(const struct nft_data
*data
, enum nft_data_types type
)
4544 if (type
< NFT_DATA_VERDICT
)
4547 case NFT_DATA_VERDICT
:
4548 return nft_verdict_uninit(data
);
4553 EXPORT_SYMBOL_GPL(nft_data_uninit
);
4555 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
4556 enum nft_data_types type
, unsigned int len
)
4558 struct nlattr
*nest
;
4561 nest
= nla_nest_start(skb
, attr
);
4566 case NFT_DATA_VALUE
:
4567 err
= nft_value_dump(skb
, data
, len
);
4569 case NFT_DATA_VERDICT
:
4570 err
= nft_verdict_dump(skb
, data
);
4577 nla_nest_end(skb
, nest
);
4580 EXPORT_SYMBOL_GPL(nft_data_dump
);
4582 static int nf_tables_init_net(struct net
*net
)
4584 INIT_LIST_HEAD(&net
->nft
.af_info
);
4585 INIT_LIST_HEAD(&net
->nft
.commit_list
);
4586 net
->nft
.base_seq
= 1;
4590 static struct pernet_operations nf_tables_net_ops
= {
4591 .init
= nf_tables_init_net
,
4594 static int __init
nf_tables_module_init(void)
4598 info
= kmalloc(sizeof(struct nft_expr_info
) * NFT_RULE_MAXEXPRS
,
4605 err
= nf_tables_core_module_init();
4609 err
= nfnetlink_subsys_register(&nf_tables_subsys
);
4613 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
4614 return register_pernet_subsys(&nf_tables_net_ops
);
4616 nf_tables_core_module_exit();
4623 static void __exit
nf_tables_module_exit(void)
4625 unregister_pernet_subsys(&nf_tables_net_ops
);
4626 nfnetlink_subsys_unregister(&nf_tables_subsys
);
4628 nf_tables_core_module_exit();
4632 module_init(nf_tables_module_init
);
4633 module_exit(nf_tables_module_exit
);
4635 MODULE_LICENSE("GPL");
4636 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4637 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES
);