netfilter: nf_tables: bug fixes and minor cleanups
Subsystems (3)
cover netfilter: nf_tables: bug fixes and minor cleanups
The following patches fix a couple of bugs related to chain types module references, chain modification atomicity, chain type module loading and unloading of modules that are still in use. Detailed descriptions are in the individual changelogs.
The patches obviously also affect the current -rc, but I think its a bit late in the release cycle for bigger fixes like this, so I based them on your nftables.git tree.
Please apply, thanks.
Patrick McHardy (13):
netfilter: nf_tables: split chain policy validation from actually setting it
netfilter: nf_tables: restore chain change atomicity
netfilter: nf_tables: fix check for table overflow
netfilter: nf_tables: fix chain type module reference handling
netfilter: nf_tables: add missing module references to chain types
netfilter: nf_tables: replay request after dropping locks to load chain type
netfilter: nf_tables: constify chain type definitions and pointers
netfilter: nf_tables: minor nf_chain_type cleanups
netfilter: nf_tables: perform flags validation before table allocation
netfilter: nf_tables: take AF module reference when creating a table
netfilter: nf_tables: prohibit deletion of a table with existing sets
netfilter: nf_tables: unininline nft_trace_packet()
netfilter: nf_tables: rename nft_do_chain_pktinfo() to nft_do_chain()
include/net/netfilter/nf_tables.h | 32 +++-- net/bridge/netfilter/nf_tables_bridge.c | 7 +- net/ipv4/netfilter/nf_tables_arp.c | 7 +- net/ipv4/netfilter/nf_tables_ipv4.c | 7 +- net/ipv4/netfilter/nft_chain_nat_ipv4.c | 10 +- net/ipv4/netfilter/nft_chain_route_ipv4.c | 10 +- net/ipv6/netfilter/nf_tables_ipv6.c | 7 +- net/ipv6/netfilter/nft_chain_nat_ipv6.c | 10 +- net/ipv6/netfilter/nft_chain_route_ipv6.c | 10 +- net/netfilter/nf_tables_api.c | 192 ++++++++++++++---------------- net/netfilter/nf_tables_core.c | 10 +- net/netfilter/nf_tables_inet.c | 5 +- 12 files changed, 157 insertions(+), 150 deletions(-)
PATCH 1/13 netfilter: nf_tables: split chain policy validation from actually setting it
Currently nf_tables_newchain() atomicity is broken because of having validation of some netlink attributes performed after changing attributes of the chain. The chain policy is (currently) fine, but split it up as preparation for the following fixes and to avoid future mistakes.
--- net/netfilter/nf_tables_api.c | 56 ++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 36 deletions(-)
net/netfilter/nf_tables_api.c +20 -36
@@ -760,22 +760,6 @@ err:return err; } -static int -nf_tables_chain_policy (struct nft_base_chain *chain,const struct nlattr *attr) -{ -switch (ntohl (nla_get_be32 (attr))) { -case NF_DROP : - chain->policy =NF_DROP ; -break ; -case NF_ACCEPT : - chain->policy =NF_ACCEPT ; -break ; -default : -return -EINVAL ; - } -return 0 ; -} -static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX +1 ] = { [NFTA_COUNTER_PACKETS ] = { .type =NLA_U64 }, [NFTA_COUNTER_BYTES ] = { .type =NLA_U64 },
@@ -834,6 +818,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,struct nlattr *ha[NFTA_HOOK_MAX +1 ];struct net *net =sock_net (skb->sk);int family = nfmsg->nfgen_family; +u8 policy =NF_ACCEPT ;u64 handle =0 ;unsigned int i;int err;
@@ -869,6 +854,22 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, } } +if (nla[NFTA_CHAIN_POLICY ]) { +if ((chain !=NULL && + !(chain->flags &NFT_BASE_CHAIN )) || + nla[NFTA_CHAIN_HOOK ] ==NULL ) +return -EOPNOTSUPP ; + + policy =nla_get_be32 (nla[NFTA_CHAIN_POLICY ]); +switch (policy) { +case NF_DROP : +case NF_ACCEPT : +break ; +default : +return -EINVAL ; + } + } +if (chain !=NULL ) {if (nlh->nlmsg_flags &NLM_F_EXCL )return -EEXIST ;
@@ -879,15 +880,8 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))return -EEXIST ; -if (nla[NFTA_CHAIN_POLICY ]) { -if (!(chain->flags &NFT_BASE_CHAIN )) -return -EOPNOTSUPP ; - - err =nf_tables_chain_policy (nft_base_chain (chain), - nla[NFTA_CHAIN_POLICY ]); -if (err <0 ) -return err; - } +if (nla[NFTA_CHAIN_POLICY ]) +nft_base_chain (chain)->policy = policy;if (nla[NFTA_CHAIN_COUNTERS ]) {if (!(chain->flags &NFT_BASE_CHAIN ))
@@ -958,17 +952,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, } chain->flags |=NFT_BASE_CHAIN ; - -if (nla[NFTA_CHAIN_POLICY ]) { - err =nf_tables_chain_policy (basechain, - nla[NFTA_CHAIN_POLICY ]); -if (err <0 ) { -free_percpu (basechain->stats); -kfree (basechain); -return err; - } - }else - basechain->policy =NF_ACCEPT ; + basechain->policy = policy;if (nla[NFTA_CHAIN_COUNTERS ]) { err = nf_tables_counters(basechain,
Signature · 1 line
1.8.4.2
PATCH 2/13 netfilter: nf_tables: restore chain change atomicity
In reply to Patrick McHardyChain counter validation is performed after the chain policy has potentially been changed. Move counter validation/setting before changing of the chain policy to fix this.
Additionally fix a memory leak if chain counter allocation fails for new chains, remove an unnecessary free_percpu() and move counter allocation for new chains
--- net/netfilter/nf_tables_api.c | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-)
net/netfilter/nf_tables_api.c +21 -22
@@ -880,9 +880,6 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))return -EEXIST ; -if (nla[NFTA_CHAIN_POLICY ]) -nft_base_chain (chain)->policy = policy; -if (nla[NFTA_CHAIN_COUNTERS ]) {if (!(chain->flags &NFT_BASE_CHAIN ))return -EOPNOTSUPP ;
@@ -893,6 +890,9 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,return err; } +if (nla[NFTA_CHAIN_POLICY ]) +nft_base_chain (chain)->policy = policy; +if (nla[NFTA_CHAIN_HANDLE ] && name)nla_strlcpy (chain->name, name,NFT_CHAIN_MAXNAMELEN );
@@ -934,6 +934,24 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,if (basechain ==NULL )return -ENOMEM ; +if (nla[NFTA_CHAIN_COUNTERS ]) { + err =nf_tables_counters (basechain, + nla[NFTA_CHAIN_COUNTERS ]); +if (err <0 ) { +kfree (basechain); +return err; + } + }else { +struct nft_stats __percpu *newstats; + + newstats = alloc_percpu(struct nft_stats); +if (newstats ==NULL ) { +kfree (basechain); +return -ENOMEM ; + } +rcu_assign_pointer (basechain->stats, newstats); + } + basechain->type = type; chain = &basechain->chain;
@@ -953,25 +971,6 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, chain->flags |=NFT_BASE_CHAIN ; basechain->policy = policy; - -if (nla[NFTA_CHAIN_COUNTERS ]) { - err =nf_tables_counters (basechain, - nla[NFTA_CHAIN_COUNTERS ]); -if (err <0 ) { -free_percpu (basechain->stats); -kfree (basechain); -return err; - } - }else { -struct nft_stats __percpu *newstats; - - newstats = alloc_percpu(struct nft_stats); -if (newstats ==NULL ) -return -ENOMEM ; - -rcu_assign_pointer (nft_base_chain (chain)->stats, - newstats); - } }else { chain =kzalloc (sizeof (*chain),GFP_KERNEL );if (chain ==NULL )
Signature · 1 line
1.8.4.2
PATCH 3/13 netfilter: nf_tables: fix check for table overflow
In reply to Patrick McHardyThe table use counter is only increased for new chains, so move the check to the correct position.
--- net/netfilter/nf_tables_api.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
net/netfilter/nf_tables_api.c +3 -3
@@ -834,9 +834,6 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,if (IS_ERR (table))return PTR_ERR (table); -if (table->use ==UINT_MAX ) -return -EOVERFLOW ; - chain =NULL ; name = nla[NFTA_CHAIN_NAME ];
@@ -899,6 +896,9 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, goto notify; } +if (table->use ==UINT_MAX ) +return -EOVERFLOW ; +if (nla[NFTA_CHAIN_HOOK ]) {struct nf_hook_ops *ops;nf_hookfn *hookfn;
Signature · 1 line
1.8.4.2
PATCH 4/13 netfilter: nf_tables: fix chain type module reference handling
In reply to Patrick McHardyThe chain type module reference handling makes no sense at all: we take a reference immediately when the module is registered, preventing the module from ever being unloaded.
Fix by taking a reference when we're actually creating a chain of the chain type and release the reference when destroying the chain.
--- include/net/netfilter/nf_tables.h | 2 +- net/netfilter/nf_tables_api.c | 45 +++++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 22 deletions(-)
include/net/netfilter/nf_tables.h +1 -1
@@ -436,7 +436,7 @@ struct nft_stats {*/ struct nft_base_chain {struct nf_hook_ops ops[NFT_HOOK_OPS_MAX ]; -enum nft_chain_type type; +struct nf_chain_type *type;u8 policy;struct nft_stats __percpu *stats;struct nft_chain chain;
net/netfilter/nf_tables_api.c +24 -21
126 lines not on this page · raw message
Signature · 1 line
1.8.4.2
PATCH 5/13 netfilter: nf_tables: add missing module references to chain types
In reply to Patrick McHardyIn some cases we neither take a reference to the AF info nor to the chain type, allowing the module to be unloaded while in use.
--- net/bridge/netfilter/nf_tables_bridge.c | 1 + net/ipv4/netfilter/nf_tables_arp.c | 1 + net/ipv4/netfilter/nf_tables_ipv4.c | 1 + net/ipv6/netfilter/nf_tables_ipv6.c | 1 + net/netfilter/nf_tables_inet.c | 1 + 5 files changed, 5 insertions(+)
net/bridge/netfilter/nf_tables_bridge.c +1 -0
@@ -72,6 +72,7 @@ static struct nf_chain_type filter_bridge = { .family = NFPROTO_BRIDGE, .name = "filter", .type = NFT_CHAIN_T_DEFAULT, + .me = THIS_MODULE, .hook_mask = (1 <<NF_BR_LOCAL_IN ) | (1 <<NF_BR_FORWARD ) | (1 << NF_BR_LOCAL_OUT),
net/ipv4/netfilter/nf_tables_arp.c +1 -0
@@ -72,6 +72,7 @@ static struct nf_chain_type filter_arp = { .family = NFPROTO_ARP, .name = "filter", .type = NFT_CHAIN_T_DEFAULT, + .me = THIS_MODULE, .hook_mask = (1 <<NF_ARP_IN ) | (1 <<NF_ARP_OUT ) | (1 << NF_ARP_FORWARD),
net/ipv4/netfilter/nf_tables_ipv4.c +1 -0
@@ -95,6 +95,7 @@ static struct nf_chain_type filter_ipv4 = { .family = NFPROTO_IPV4, .name = "filter", .type = NFT_CHAIN_T_DEFAULT, + .me = THIS_MODULE, .hook_mask = (1 <<NF_INET_LOCAL_IN ) | (1 <<NF_INET_LOCAL_OUT ) | (1 << NF_INET_FORWARD) |
net/ipv6/netfilter/nf_tables_ipv6.c +1 -0
@@ -94,6 +94,7 @@ static struct nf_chain_type filter_ipv6 = { .family = NFPROTO_IPV6, .name = "filter", .type = NFT_CHAIN_T_DEFAULT, + .me = THIS_MODULE, .hook_mask = (1 <<NF_INET_LOCAL_IN ) | (1 <<NF_INET_LOCAL_OUT ) | (1 << NF_INET_FORWARD) |
net/netfilter/nf_tables_inet.c +1 -0
@@ -70,6 +70,7 @@ static struct nf_chain_type filter_inet = { .family = NFPROTO_INET, .name = "filter", .type = NFT_CHAIN_T_DEFAULT, + .me = THIS_MODULE, .hook_mask = (1 <<NF_INET_LOCAL_IN ) | (1 <<NF_INET_LOCAL_OUT ) | (1 << NF_INET_FORWARD) |
Signature · 1 line
1.8.4.2
PATCH 6/13 netfilter: nf_tables: replay request after dropping locks to load chain type
In reply to Patrick McHardyTo avoid races, we need to replay to request after dropping the nfnl_mutex to auto-load the chain type module.
--- net/netfilter/nf_tables_api.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
net/netfilter/nf_tables_api.c +8 -4
@@ -147,16 +147,20 @@ nf_tables_chain_type_lookup(const struct nft_af_info *afi,struct nf_chain_type *type; type =__nf_tables_chain_type_lookup (afi->family, nla); +if (type !=NULL ) +return type;#ifdef CONFIG_MODULES -if (type ==NULL && autoload) { +if (autoload) {nfnl_unlock (NFNL_SUBSYS_NFTABLES );request_module ("nft-chain-%u-%*.s" , afi->family,nla_len (nla)-1 , (const char *)nla_data (nla));nfnl_lock (NFNL_SUBSYS_NFTABLES ); type =__nf_tables_chain_type_lookup (afi->family, nla); +if (type !=NULL ) +return ERR_PTR (-EAGAIN ); }#endif -return type; +return ERR_PTR (-ENOENT ); }static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX +1 ] = {
@@ -906,8 +910,8 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, type =nf_tables_chain_type_lookup (afi, nla[NFTA_CHAIN_TYPE ], create); -if (type ==NULL ) -return -ENOENT ; +if (IS_ERR (type)) +return PTR_ERR (type); } err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
Signature · 1 line
1.8.4.2
PATCH 7/13 netfilter: nf_tables: constify chain type definitions and pointers
In reply to Patrick McHardy--- include/net/netfilter/nf_tables.h | 6 +++--- net/bridge/netfilter/nf_tables_bridge.c | 2 +- net/ipv4/netfilter/nf_tables_arp.c | 2 +- net/ipv4/netfilter/nf_tables_ipv4.c | 2 +- net/ipv4/netfilter/nft_chain_nat_ipv4.c | 2 +- net/ipv4/netfilter/nft_chain_route_ipv4.c | 2 +- net/ipv6/netfilter/nf_tables_ipv6.c | 2 +- net/ipv6/netfilter/nft_chain_nat_ipv6.c | 2 +- net/ipv6/netfilter/nft_chain_route_ipv6.c | 2 +- net/netfilter/nf_tables_api.c | 14 +++++++------- net/netfilter/nf_tables_inet.c | 2 +- 11 files changed, 19 insertions(+), 19 deletions(-)
include/net/netfilter/nf_tables.h +3 -3
@@ -436,7 +436,7 @@ struct nft_stats {*/ struct nft_base_chain {struct nf_hook_ops ops[NFT_HOOK_OPS_MAX ]; -struct nf_chain_type *type; +const struct nf_chain_type *type;u8 policy;struct nft_stats __percpu *stats;struct nft_chain chain;
@@ -507,8 +507,8 @@ struct nf_chain_type {int family; }; -int nft_register_chain_type (struct nf_chain_type *); -void nft_unregister_chain_type (struct nf_chain_type *); +int nft_register_chain_type (const struct nf_chain_type *); +void nft_unregister_chain_type (const struct nf_chain_type *);int nft_register_expr (struct nft_expr_type *);void nft_unregister_expr (struct nft_expr_type *);
net/bridge/netfilter/nf_tables_bridge.c +1 -1
@@ -68,7 +68,7 @@ static struct pernet_operations nf_tables_bridge_net_ops = { .exit = nf_tables_bridge_exit_net, }; -static struct nf_chain_type filter_bridge = { +static const struct nf_chain_type filter_bridge = { .family =NFPROTO_BRIDGE , .name ="filter" , .type =NFT_CHAIN_T_DEFAULT ,
net/ipv4/netfilter/nf_tables_arp.c +1 -1
@@ -68,7 +68,7 @@ static struct pernet_operations nf_tables_arp_net_ops = { .exit = nf_tables_arp_exit_net, }; -static struct nf_chain_type filter_arp = { +static const struct nf_chain_type filter_arp = { .family =NFPROTO_ARP , .name ="filter" , .type =NFT_CHAIN_T_DEFAULT ,
net/ipv4/netfilter/nf_tables_ipv4.c +1 -1
@@ -91,7 +91,7 @@ static struct pernet_operations nf_tables_ipv4_net_ops = { .exit = nf_tables_ipv4_exit_net, }; -static struct nf_chain_type filter_ipv4 = { +static const struct nf_chain_type filter_ipv4 = { .family =NFPROTO_IPV4 , .name ="filter" , .type =NFT_CHAIN_T_DEFAULT ,
net/ipv4/netfilter/nft_chain_nat_ipv4.c +1 -1
@@ -164,7 +164,7 @@ static unsigned int nf_nat_output(const struct nf_hook_ops *ops,return ret; } -static struct nf_chain_type nft_chain_nat_ipv4 = { +static const struct nf_chain_type nft_chain_nat_ipv4 = { .family =NFPROTO_IPV4 , .name ="nat" , .type =NFT_CHAIN_T_NAT ,
net/ipv4/netfilter/nft_chain_route_ipv4.c +1 -1
@@ -61,7 +61,7 @@ static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops,return ret; } -static struct nf_chain_type nft_chain_route_ipv4 = { +static const struct nf_chain_type nft_chain_route_ipv4 = { .family =NFPROTO_IPV4 , .name ="route" , .type =NFT_CHAIN_T_ROUTE ,
net/ipv6/netfilter/nf_tables_ipv6.c +1 -1
@@ -90,7 +90,7 @@ static struct pernet_operations nf_tables_ipv6_net_ops = { .exit = nf_tables_ipv6_exit_net, }; -static struct nf_chain_type filter_ipv6 = { +static const struct nf_chain_type filter_ipv6 = { .family =NFPROTO_IPV6 , .name ="filter" , .type =NFT_CHAIN_T_DEFAULT ,
net/ipv6/netfilter/nft_chain_nat_ipv6.c +1 -1
@@ -170,7 +170,7 @@ static unsigned int nf_nat_ipv6_output(const struct nf_hook_ops *ops,return ret; } -static struct nf_chain_type nft_chain_nat_ipv6 = { +static const struct nf_chain_type nft_chain_nat_ipv6 = { .family =NFPROTO_IPV6 , .name ="nat" , .type =NFT_CHAIN_T_NAT ,
net/ipv6/netfilter/nft_chain_route_ipv6.c +1 -1
@@ -59,7 +59,7 @@ static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops,return ret; } -static struct nf_chain_type nft_chain_route_ipv6 = { +static const struct nf_chain_type nft_chain_route_ipv6 = { .family =NFPROTO_IPV6 , .name ="route" , .type =NFT_CHAIN_T_ROUTE ,
net/netfilter/nf_tables_api.c +7 -7
@@ -124,9 +124,9 @@ static inline u64 nf_tables_alloc_handle(struct nft_table *table)return ++table->hgenerator; } -static struct nf_chain_type *chain_type[AF_MAX ][NFT_CHAIN_T_MAX ]; +static const struct nf_chain_type *chain_type[AF_MAX ][NFT_CHAIN_T_MAX ]; -static struct nf_chain_type * +static const struct nf_chain_type *__nf_tables_chain_type_lookup (int family,const struct nlattr *nla) {int i;
@@ -139,12 +139,12 @@ __nf_tables_chain_type_lookup(int family, const struct nlattr *nla)return NULL ; } -static struct nf_chain_type * +static const struct nf_chain_type *nf_tables_chain_type_lookup (const struct nft_af_info *afi,const struct nlattr *nla,bool autoload) { -struct nf_chain_type *type; +const struct nf_chain_type *type; type =__nf_tables_chain_type_lookup (afi->family, nla);if (type !=NULL )
@@ -475,7 +475,7 @@ static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,return 0 ; } -int nft_register_chain_type (struct nf_chain_type *ctype) +int nft_register_chain_type (const struct nf_chain_type *ctype) {int err =0 ;
@@ -491,7 +491,7 @@ out: }EXPORT_SYMBOL_GPL (nft_register_chain_type); -void nft_unregister_chain_type (struct nf_chain_type *ctype) +void nft_unregister_chain_type (const struct nf_chain_type *ctype) {nfnl_lock (NFNL_SUBSYS_NFTABLES ); chain_type[ctype->family][ctype->type] =NULL ;
@@ -900,7 +900,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,return -EOVERFLOW ;if (nla[NFTA_CHAIN_HOOK ]) { -struct nf_chain_type *type; +const struct nf_chain_type *type;struct nf_hook_ops *ops;nf_hookfn *hookfn;u32 hooknum, priority;
net/netfilter/nf_tables_inet.c +1 -1
@@ -66,7 +66,7 @@ static struct pernet_operations nf_tables_inet_net_ops = { .exit = nf_tables_inet_exit_net, }; -static struct nf_chain_type filter_inet = { +static const struct nf_chain_type filter_inet = { .family =NFPROTO_INET , .name ="filter" , .type =NFT_CHAIN_T_DEFAULT ,
Signature · 1 line
1.8.4.2
PATCH 8/13 netfilter: nf_tables: minor nf_chain_type cleanups
In reply to Patrick McHardyMinor nf_chain_type cleanups:
- reorder struct to plug a hoe
- rename struct module member to "owner" for consistency
- rename nf_hookfn array to "hooks" for consistency
- reorder initializers for better readability
--- include/net/netfilter/nf_tables.h | 22 ++++++++++++++++------ net/bridge/netfilter/nf_tables_bridge.c | 4 ++-- net/ipv4/netfilter/nf_tables_arp.c | 4 ++-- net/ipv4/netfilter/nf_tables_ipv4.c | 4 ++-- net/ipv4/netfilter/nft_chain_nat_ipv4.c | 6 +++--- net/ipv4/netfilter/nft_chain_route_ipv4.c | 6 +++--- net/ipv6/netfilter/nf_tables_ipv6.c | 4 ++-- net/ipv6/netfilter/nft_chain_nat_ipv6.c | 6 +++--- net/ipv6/netfilter/nft_chain_route_ipv6.c | 6 +++--- net/netfilter/nf_tables_api.c | 12 ++++++------ net/netfilter/nf_tables_inet.c | 4 ++-- 11 files changed, 44 insertions(+), 34 deletions(-)
include/net/netfilter/nf_tables.h +16 -6
@@ -498,13 +498,23 @@ struct nft_af_info {int nft_register_afinfo (struct net *,struct nft_af_info *);void nft_unregister_afinfo (struct nft_af_info *); +/** +* struct nf_chain_type - nf_tables chain type info +* +* @name: name of the type +* @type: numeric identifier +* @family: address family +* @owner: module owner +* @hook_mask: mask of valid hooks +* @hooks: hookfn overrides +*/ struct nf_chain_type { -unsigned int hook_mask; -const char *name; -enum nft_chain_type type; -nf_hookfn *fn[NF_MAX_HOOKS ]; -struct module *me; -int family; +const char *name; +enum nft_chain_type type; +int family; +struct module *owner; +unsigned int hook_mask; +nf_hookfn *hooks[NF_MAX_HOOKS ]; };int nft_register_chain_type (const struct nf_chain_type *);
net/bridge/netfilter/nf_tables_bridge.c +2 -2
@@ -69,10 +69,10 @@ static struct pernet_operations nf_tables_bridge_net_ops = { };static const struct nf_chain_type filter_bridge = { - .family =NFPROTO_BRIDGE , .name ="filter" , .type =NFT_CHAIN_T_DEFAULT , - .me =THIS_MODULE , + .family =NFPROTO_BRIDGE , + .owner =THIS_MODULE , .hook_mask = (1 <<NF_BR_LOCAL_IN ) | (1 <<NF_BR_FORWARD ) | (1 <<NF_BR_LOCAL_OUT ),
net/ipv4/netfilter/nf_tables_arp.c +2 -2
@@ -69,10 +69,10 @@ static struct pernet_operations nf_tables_arp_net_ops = { };static const struct nf_chain_type filter_arp = { - .family =NFPROTO_ARP , .name ="filter" , .type =NFT_CHAIN_T_DEFAULT , - .me =THIS_MODULE , + .family =NFPROTO_ARP , + .owner =THIS_MODULE , .hook_mask = (1 <<NF_ARP_IN ) | (1 <<NF_ARP_OUT ) | (1 <<NF_ARP_FORWARD ),
net/ipv4/netfilter/nf_tables_ipv4.c +2 -2
@@ -92,10 +92,10 @@ static struct pernet_operations nf_tables_ipv4_net_ops = { };static const struct nf_chain_type filter_ipv4 = { - .family =NFPROTO_IPV4 , .name ="filter" , .type =NFT_CHAIN_T_DEFAULT , - .me =THIS_MODULE , + .family =NFPROTO_IPV4 , + .owner =THIS_MODULE , .hook_mask = (1 <<NF_INET_LOCAL_IN ) | (1 <<NF_INET_LOCAL_OUT ) | (1 << NF_INET_FORWARD) |
net/ipv4/netfilter/nft_chain_nat_ipv4.c +3 -3
@@ -165,20 +165,20 @@ static unsigned int nf_nat_output(const struct nf_hook_ops *ops, }static const struct nf_chain_type nft_chain_nat_ipv4 = { - .family =NFPROTO_IPV4 , .name ="nat" , .type =NFT_CHAIN_T_NAT , + .family =NFPROTO_IPV4 , + .owner =THIS_MODULE , .hook_mask = (1 <<NF_INET_PRE_ROUTING ) | (1 <<NF_INET_POST_ROUTING ) | (1 <<NF_INET_LOCAL_OUT ) | (1 <<NF_INET_LOCAL_IN ), - .fn = { + .hooks = { [NF_INET_PRE_ROUTING ] = nf_nat_prerouting, [NF_INET_POST_ROUTING ] = nf_nat_postrouting, [NF_INET_LOCAL_OUT ] = nf_nat_output, [NF_INET_LOCAL_IN ] = nf_nat_fn, }, - .me =THIS_MODULE , };static int __init nft_chain_nat_init (void )
net/ipv4/netfilter/nft_chain_route_ipv4.c +3 -3
@@ -62,14 +62,14 @@ static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops, }static const struct nf_chain_type nft_chain_route_ipv4 = { - .family =NFPROTO_IPV4 , .name ="route" , .type =NFT_CHAIN_T_ROUTE , + .family =NFPROTO_IPV4 , + .owner =THIS_MODULE , .hook_mask = (1 <<NF_INET_LOCAL_OUT ), - .fn = { + .hooks = { [NF_INET_LOCAL_OUT ] = nf_route_table_hook, }, - .me =THIS_MODULE , };static int __init nft_chain_route_init (void )
net/ipv6/netfilter/nf_tables_ipv6.c +2 -2
@@ -91,10 +91,10 @@ static struct pernet_operations nf_tables_ipv6_net_ops = { };static const struct nf_chain_type filter_ipv6 = { - .family =NFPROTO_IPV6 , .name ="filter" , .type =NFT_CHAIN_T_DEFAULT , - .me =THIS_MODULE , + .family =NFPROTO_IPV6 , + .owner =THIS_MODULE , .hook_mask = (1 <<NF_INET_LOCAL_IN ) | (1 <<NF_INET_LOCAL_OUT ) | (1 << NF_INET_FORWARD) |
net/ipv6/netfilter/nft_chain_nat_ipv6.c +3 -3
@@ -171,20 +171,20 @@ static unsigned int nf_nat_ipv6_output(const struct nf_hook_ops *ops, }static const struct nf_chain_type nft_chain_nat_ipv6 = { - .family =NFPROTO_IPV6 , .name ="nat" , .type =NFT_CHAIN_T_NAT , + .family =NFPROTO_IPV6 , + .owner =THIS_MODULE , .hook_mask = (1 <<NF_INET_PRE_ROUTING ) | (1 <<NF_INET_POST_ROUTING ) | (1 <<NF_INET_LOCAL_OUT ) | (1 <<NF_INET_LOCAL_IN ), - .fn = { + .hooks = { [NF_INET_PRE_ROUTING ] = nf_nat_ipv6_prerouting, [NF_INET_POST_ROUTING ] = nf_nat_ipv6_postrouting, [NF_INET_LOCAL_OUT ] = nf_nat_ipv6_output, [NF_INET_LOCAL_IN ] = nf_nat_ipv6_fn, }, - .me =THIS_MODULE , };static int __init nft_chain_nat_ipv6_init (void )
net/ipv6/netfilter/nft_chain_route_ipv6.c +3 -3
@@ -60,14 +60,14 @@ static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops, }static const struct nf_chain_type nft_chain_route_ipv6 = { - .family =NFPROTO_IPV6 , .name ="route" , .type =NFT_CHAIN_T_ROUTE , + .family =NFPROTO_IPV6 , + .owner =THIS_MODULE , .hook_mask = (1 <<NF_INET_LOCAL_OUT ), - .fn = { + .hooks = { [NF_INET_LOCAL_OUT ] = nf_route_table_hook, }, - .me =THIS_MODULE , };static int __init nft_chain_route_init (void )
net/netfilter/nf_tables_api.c +6 -6
@@ -929,9 +929,9 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,if (!(type->hook_mask & (1 << hooknum)))return -EOPNOTSUPP ; -if (!try_module_get (type->me)) +if (!try_module_get (type->owner))return -ENOENT ; - hookfn = type->fn[hooknum]; + hookfn = type->hooks[hooknum]; basechain =kzalloc (sizeof (*basechain),GFP_KERNEL );if (basechain ==NULL )
@@ -941,7 +941,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, err =nf_tables_counters (basechain, nla[NFTA_CHAIN_COUNTERS ]);if (err <0 ) { -module_put (type->me); +module_put (type->owner);kfree (basechain);return err; }
@@ -950,7 +950,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, newstats = alloc_percpu(struct nft_stats);if (newstats ==NULL ) { -module_put (type->me); +module_put (type->owner);kfree (basechain);return -ENOMEM ; }
@@ -992,7 +992,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, chain->flags & NFT_BASE_CHAIN) { err =nf_register_hooks (nft_base_chain (chain)->ops, afi->nops);if (err <0 ) { -module_put (basechain->type->me); +module_put (basechain->type->owner);free_percpu (basechain->stats);kfree (basechain);return err;
@@ -1013,7 +1013,7 @@ static void nf_tables_rcu_chain_destroy(struct rcu_head *head)BUG_ON (chain->use >0 );if (chain->flags &NFT_BASE_CHAIN ) { -module_put (nft_base_chain (chain)->type->me); +module_put (nft_base_chain (chain)->type->owner);free_percpu (nft_base_chain (chain)->stats);kfree (nft_base_chain (chain)); }else
net/netfilter/nf_tables_inet.c +2 -2
@@ -67,10 +67,10 @@ static struct pernet_operations nf_tables_inet_net_ops = { };static const struct nf_chain_type filter_inet = { - .family =NFPROTO_INET , .name ="filter" , .type =NFT_CHAIN_T_DEFAULT , - .me =THIS_MODULE , + .family =NFPROTO_INET , + .owner =THIS_MODULE , .hook_mask = (1 <<NF_INET_LOCAL_IN ) | (1 <<NF_INET_LOCAL_OUT ) | (1 << NF_INET_FORWARD) |
Signature · 1 line
1.8.4.2
PATCH 9/13 netfilter: nf_tables: perform flags validation before table allocation
In reply to Patrick McHardySimplifies error handling. Additionally use the correct type u32 for the host byte order flags value.
--- net/netfilter/nf_tables_api.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-)
net/netfilter/nf_tables_api.c +9 -13
@@ -366,7 +366,7 @@ static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,int family = nfmsg->nfgen_family, ret =0 ;if (nla[NFTA_TABLE_FLAGS ]) { -__be32 flags; +u32 flags; flags =ntohl (nla_get_be32 (nla[NFTA_TABLE_FLAGS ]));if (flags & ~NFT_TABLE_F_DORMANT )
@@ -402,6 +402,7 @@ static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,struct nft_table *table;struct net *net =sock_net (skb->sk);int family = nfmsg->nfgen_family; +u32 flags =0 ; afi =nf_tables_afinfo_lookup (net, family, true);if (IS_ERR (afi))
@@ -423,6 +424,12 @@ static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,return nf_tables_updtable (nlsk, skb, nlh, nla, afi, table); } +if (nla[NFTA_TABLE_FLAGS ]) { + flags =ntohl (nla_get_be32 (nla[NFTA_TABLE_FLAGS ])); +if (flags & ~NFT_TABLE_F_DORMANT ) +return -EINVAL ; + } + table =kzalloc (sizeof (*table) +nla_len (name),GFP_KERNEL );if (table ==NULL )return -ENOMEM ;
@@ -430,18 +437,7 @@ static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,nla_strlcpy (table->name, name,nla_len (name));INIT_LIST_HEAD (&table->chains);INIT_LIST_HEAD (&table->sets); - -if (nla[NFTA_TABLE_FLAGS ]) { -__be32 flags; - - flags =ntohl (nla_get_be32 (nla[NFTA_TABLE_FLAGS ])); -if (flags & ~NFT_TABLE_F_DORMANT ) { -kfree (table); -return -EINVAL ; - } - - table->flags |= flags; - } + table->flags = flags;list_add_tail (&table->list, &afi->tables);nf_tables_table_notify (skb, nlh, table,NFT_MSG_NEWTABLE , family);
Signature · 1 line
1.8.4.2
PATCH 10/13 netfilter: nf_tables: take AF module reference when creating a table
In reply to Patrick McHardyThe table refers to data of the AF module, so we need to make sure the module isn't unloaded while the table exists.
--- net/netfilter/nf_tables_api.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +7 -1
@@ -430,9 +430,14 @@ static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,return -EINVAL ; } +if (!try_module_get (afi->owner)) +return -EAFNOSUPPORT ; + table =kzalloc (sizeof (*table) +nla_len (name),GFP_KERNEL ); -if (table ==NULL ) +if (table ==NULL ) { +module_put (afi->owner);return -ENOMEM ; + }nla_strlcpy (table->name, name,nla_len (name));INIT_LIST_HEAD (&table->chains);
@@ -468,6 +473,7 @@ static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,list_del (&table->list);nf_tables_table_notify (skb, nlh, table,NFT_MSG_DELTABLE , family);kfree (table); +module_put (afi->owner);return 0 ; }
Signature · 1 line
1.8.4.2
PATCH 11/13 netfilter: nf_tables: prohibit deletion of a table with existing sets
In reply to Patrick McHardyWe currently leak the set memory when deleting a table that still has sets in it. Return EBUSY when attempting to delete a table with sets.
--- net/netfilter/nf_tables_api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +1 -1
@@ -467,7 +467,7 @@ static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,if (IS_ERR (table))return PTR_ERR (table); -if (table->use) +if (!list_empty (&table->chains) || !list_empty (&table->sets))return -EBUSY ;list_del (&table->list);
Signature · 1 line
1.8.4.2
PATCH 12/13 netfilter: nf_tables: unininline nft_trace_packet()
In reply to Patrick McHardyIt makes no sense to inline a rarely used function meant for debugging only that is called a total of five times in the main evaluation loop.
--- net/netfilter/nf_tables_core.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
net/netfilter/nf_tables_core.c +3 -3
@@ -103,9 +103,9 @@ static struct nf_loginfo trace_loginfo = { }, }; -static inline void nft_trace_packet (const struct nft_pktinfo *pkt, -const struct nft_chain *chain, -int rulenum,enum nft_trace type) +static void nft_trace_packet (const struct nft_pktinfo *pkt, +const struct nft_chain *chain, +int rulenum,enum nft_trace type) {struct net *net =dev_net (pkt->in ? pkt->in : pkt->out);
Signature · 1 line
1.8.4.2
PATCH 13/13 netfilter: nf_tables: rename nft_do_chain_pktinfo() to nft_do_chain()
In reply to Patrick McHardyWe don't encode argument types into function names and since besides nft_do_chain() there are only AF-specific versions, there is no risk of confusion.
--- include/net/netfilter/nf_tables.h | 4 ++-- net/bridge/netfilter/nf_tables_bridge.c | 2 +- net/ipv4/netfilter/nf_tables_arp.c | 2 +- net/ipv4/netfilter/nf_tables_ipv4.c | 2 +- net/ipv4/netfilter/nft_chain_nat_ipv4.c | 2 +- net/ipv4/netfilter/nft_chain_route_ipv4.c | 2 +- net/ipv6/netfilter/nf_tables_ipv6.c | 2 +- net/ipv6/netfilter/nft_chain_nat_ipv6.c | 2 +- net/ipv6/netfilter/nft_chain_route_ipv6.c | 2 +- net/netfilter/nf_tables_core.c | 4 ++-- 10 files changed, 12 insertions(+), 12 deletions(-)
include/net/netfilter/nf_tables.h +2 -2
@@ -447,8 +447,8 @@ static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chai return container_of(chain, struct nft_base_chain, chain); } -unsigned int nft_do_chain_pktinfo (struct nft_pktinfo *pkt, -const struct nf_hook_ops *ops); +unsigned int nft_do_chain (struct nft_pktinfo *pkt, +const struct nf_hook_ops *ops);/** * struct nft_table - nf_tables table
net/bridge/netfilter/nf_tables_bridge.c +1 -1
@@ -25,7 +25,7 @@ nft_do_chain_bridge(const struct nf_hook_ops *ops,nft_set_pktinfo (&pkt, ops, skb, in, out); -return nft_do_chain_pktinfo (&pkt, ops); +return nft_do_chain (&pkt, ops); }static struct nft_af_info nft_af_bridge__read_mostly = {
net/ipv4/netfilter/nf_tables_arp.c +1 -1
@@ -25,7 +25,7 @@ nft_do_chain_arp(const struct nf_hook_ops *ops,nft_set_pktinfo (&pkt, ops, skb, in, out); -return nft_do_chain_pktinfo (&pkt, ops); +return nft_do_chain (&pkt, ops); }static struct nft_af_info nft_af_arp__read_mostly = {
net/ipv4/netfilter/nf_tables_ipv4.c +1 -1
@@ -28,7 +28,7 @@ static unsigned int nft_do_chain_ipv4(const struct nf_hook_ops *ops,nft_set_pktinfo_ipv4 (&pkt, ops, skb, in, out); -return nft_do_chain_pktinfo (&pkt, ops); +return nft_do_chain (&pkt, ops); } static unsigned int nft_ipv4_output(const struct nf_hook_ops *ops,
net/ipv4/netfilter/nft_chain_nat_ipv4.c +1 -1
@@ -75,7 +75,7 @@ static unsigned int nf_nat_fn(const struct nf_hook_ops *ops,nft_set_pktinfo_ipv4 (&pkt, ops, skb, in, out); - ret =nft_do_chain_pktinfo (&pkt, ops); + ret =nft_do_chain (&pkt, ops);if (ret !=NF_ACCEPT )return ret;if (!nf_nat_initialized (ct, maniptype)) {
net/ipv4/netfilter/nft_chain_route_ipv4.c +1 -1
@@ -47,7 +47,7 @@ static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops, daddr = iph->daddr; tos = iph->tos; - ret =nft_do_chain_pktinfo (&pkt, ops); + ret =nft_do_chain (&pkt, ops);if (ret !=NF_DROP && ret !=NF_QUEUE ) { iph =ip_hdr (skb);
net/ipv6/netfilter/nf_tables_ipv6.c +1 -1
@@ -28,7 +28,7 @@ static unsigned int nft_do_chain_ipv6(const struct nf_hook_ops *ops,if (nft_set_pktinfo_ipv6 (&pkt, ops, skb, in, out) <0 )return NF_DROP ; -return nft_do_chain_pktinfo (&pkt, ops); +return nft_do_chain (&pkt, ops); } static unsigned int nft_ipv6_output(const struct nf_hook_ops *ops,
net/ipv6/netfilter/nft_chain_nat_ipv6.c +1 -1
@@ -79,7 +79,7 @@ static unsigned int nf_nat_ipv6_fn(const struct nf_hook_ops *ops,nft_set_pktinfo_ipv6 (&pkt, ops, skb, in, out); - ret =nft_do_chain_pktinfo (&pkt, ops); + ret =nft_do_chain (&pkt, ops);if (ret !=NF_ACCEPT )return ret;if (!nf_nat_initialized (ct, maniptype)) {
net/ipv6/netfilter/nft_chain_route_ipv6.c +1 -1
@@ -47,7 +47,7 @@ static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops,/* flowlabel and prio (includes version, which shouldn't change either */ flowlabel = *((u32 *)ipv6_hdr (skb)); - ret =nft_do_chain_pktinfo (&pkt, ops); + ret =nft_do_chain (&pkt, ops); if (ret != NF_DROP && ret != NF_QUEUE && (memcmp(&ipv6_hdr(skb)->saddr, &saddr, sizeof(saddr)) || memcmp(&ipv6_hdr(skb)->daddr, &daddr, sizeof(daddr)) ||
net/netfilter/nf_tables_core.c +2 -2
@@ -116,7 +116,7 @@ static void nft_trace_packet(const struct nft_pktinfo *pkt, }unsigned int -nft_do_chain_pktinfo (struct nft_pktinfo *pkt,const struct nf_hook_ops *ops) +nft_do_chain (struct nft_pktinfo *pkt,const struct nf_hook_ops *ops) {const struct nft_chain *chain = ops->priv;const struct nft_rule *rule;
@@ -216,7 +216,7 @@ next_rule:return nft_base_chain (chain)->policy; } -EXPORT_SYMBOL_GPL (nft_do_chain_pktinfo); +EXPORT_SYMBOL_GPL (nft_do_chain);int __init nf_tables_core_module_init (void ) {
Signature · 1 line
1.8.4.2
Pablo Neira Ayuso
maintainerQuoted 8 lines · Patrick McHardy
The following patches fix a couple of bugs related to chain types module references, chain modification atomicity, chain type module loading and unloading of modules that are still in use. Detailed descriptions are in the individual changelogs.
The patches obviously also affect the current -rc, but I think its a bit late in the release cycle for bigger fixes like this, so I based them on your nftables.git tree.
We can pass part of this several of these bugfixes to -stable if we think it's worth at some point, let's see how things go.
Series applied, thanks a lot.