promote NFPROTO_INET as real family
Subsystems (3)
Pablo Neira Ayuso
maintainercover promote NFPROTO_INET as real family
Hi,
This is a patchset to handle NFPROTO_INET hook registration from the Netfilter core, as we discussed during the NFWS in Faro, Portugal [1].
Quoted 1 line
From the hook path, your code just needs to check for
state->pfto know
if this either IPv4 or IPv6 traffic, alternatively skb->protocol could be used for this purpose too.
This is removing a bit of complexity / ad-hoc code for the nf_tables codebase to handle this family, just to save one single branch in the packet path. And after this patch, NFPROTO_INET is not a pseudo-family anymore.
This patchset applies on top of Florian's netfilter: reduce hook sizes in struct net series.
[1] https://workshop.netfilter.org/2017/wiki/images/4/48/Nf-nfproto-inet.pdf
Pablo Neira Ayuso (5):
netfilter: add nf_remove_net_hook
netfilter: pass hook number and family as parameter to nf_find_hook_list()
netfilter: handle NFPROTO_INET family hook registration from core
netfilter: nf_tables_inet: don't use multihook infrastructure anymore
netfilter: nf_tables: remove multihook chains and families
include/net/netfilter/nf_tables.h | 9 +-- include/net/netfilter/nf_tables_ipv4.h | 2 - include/net/netfilter/nf_tables_ipv6.h | 2 - net/bridge/netfilter/nf_tables_bridge.c | 1 - net/ipv4/netfilter/nf_tables_arp.c | 1 - net/ipv4/netfilter/nf_tables_ipv4.c | 4 +- net/ipv6/netfilter/nf_tables_ipv6.c | 4 +- net/netfilter/core.c | 99 ++++++++++++++++++++++++--------- net/netfilter/nf_tables_api.c | 97 +++++++++++++------------------- net/netfilter/nf_tables_inet.c | 65 ++++++++++++++++++---- net/netfilter/nf_tables_netdev.c | 3 +- net/netfilter/nft_compat.c | 8 +-- 12 files changed, 175 insertions(+), 120 deletions(-)
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 1/5 netfilter: add nf_remove_net_hook
Just a cleanup, __nf_unregister_net_hook() is used by a follow up patch when handling NFPROTO_INET as a real family from the core.
--- net/netfilter/core.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
net/netfilter/core.c +5 -5
@@ -320,7 +320,7 @@ int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)EXPORT_SYMBOL (nf_register_net_hook);/* -* __nf_unregister_net_hook - remove a hook from blob +* nf_remove_net_hook - remove a hook from blob * * @oldp: current address of hook blob * @unreg: hook to unregister
@@ -328,8 +328,8 @@ EXPORT_SYMBOL(nf_register_net_hook);* This cannot fail, hook unregistration must always succeed. * Therefore replace the to-be-removed hook with a dummy hook. */ -static void __nf_unregister_net_hook (struct nf_hook_entries *old, -const struct nf_hook_ops *unreg) +static void nf_remove_net_hook (struct nf_hook_entries *old, +const struct nf_hook_ops *unreg) {struct nf_hook_ops **orig_ops;bool found = false;
@@ -376,7 +376,7 @@ void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)return ; } -__nf_unregister_net_hook (p, reg); +nf_remove_net_hook (p, reg); p =__nf_hook_entries_try_shrink (pp);mutex_unlock (&nf_hook_mutex);
@@ -429,7 +429,7 @@ void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg, p =nf_entry_dereference (*pp);if (WARN_ON_ONCE (!p))continue ; -__nf_unregister_net_hook (p, ®[i]); +nf_remove_net_hook (p, ®[i]); }mutex_unlock (&nf_hook_mutex);
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 2/5 netfilter: pass hook number and family as parameter to nf_find_hook_list()
In reply to Pablo Neira AyusoJust a preparation change, this is needed by follow up patches to handle NFPROTO_INET as a real family from the core.
--- net/netfilter/core.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-)
net/netfilter/core.c +18 -14
@@ -237,26 +237,28 @@ static void *__nf_hook_entries_try_shrink(struct nf_hook_entries __rcu **pp)return old; } -static struct nf_hook_entries __rcu **nf_hook_entry_head (struct net *net,const struct nf_hook_ops *reg) +static struct nf_hook_entries __rcu ** +nf_hook_entry_head (struct net *net,int pf,unsigned int hooknum, +struct net_device *dev) { -switch (reg->pf) { +switch (pf) {case NFPROTO_NETDEV :break ;#if IS_ENABLED (CONFIG_IP_NF_ARPTABLES )case NFPROTO_ARP : -return net->nf.hooks_arp+reg->hooknum; +return net->nf.hooks_arp + hooknum;#endif #if IS_ENABLED (CONFIG_NF_TABLES_BRIDGE )case NFPROTO_BRIDGE : -return net->nf.hooks_bridge+reg->hooknum; +return net->nf.hooks_bridge + hooknum;#endif case NFPROTO_IPV4 : -return net->nf.hooks_ipv4+reg->hooknum; +return net->nf.hooks_ipv4 + hooknum;case NFPROTO_IPV6 : -return net->nf.hooks_ipv6+reg->hooknum; +return net->nf.hooks_ipv6 + hooknum;#if IS_ENABLED (CONFIG_DECNET )case NFPROTO_DECNET : -return net->nf.hooks_decnet+reg->hooknum; +return net->nf.hooks_decnet + hooknum;#endif default :WARN_ON_ONCE (1 );
@@ -264,9 +266,9 @@ static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, const }#ifdef CONFIG_NETFILTER_INGRESS -if (reg->hooknum ==NF_NETDEV_INGRESS ) { -if (reg->dev &&dev_net (reg->dev) == net) -return ®->dev->nf_hooks_ingress; +if (hooknum ==NF_NETDEV_INGRESS ) { +if (dev &&dev_net (dev) == net) +return &dev->nf_hooks_ingress; }#endif WARN_ON_ONCE (1 );
@@ -288,7 +290,7 @@ int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)return -EINVAL ; } - pp =nf_hook_entry_head (net, reg); + pp =nf_hook_entry_head (net, reg->pf, reg->hooknum, reg->dev);if (!pp)return -EINVAL ;
@@ -364,7 +366,7 @@ void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)struct nf_hook_entries *p;unsigned int nfq; - pp =nf_hook_entry_head (net, reg); + pp =nf_hook_entry_head (net, reg->pf, reg->hooknum, reg->dev);if (!pp)return ;
@@ -422,7 +424,8 @@ void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,mutex_lock (&nf_hook_mutex);for (i =0 ; i < hookcount; i++) { - pp =nf_hook_entry_head (net, ®[i]); + pp =nf_hook_entry_head (net, reg[i].pf, reg[i].hooknum, + reg[i].dev);if (!pp)continue ;
@@ -439,7 +442,8 @@ void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,mutex_lock (&nf_hook_mutex);for (i =0 , j =0 ; i < hookcount && j < n; i++) { - pp =nf_hook_entry_head (net, ®[i]); + pp =nf_hook_entry_head (net, reg[i].pf, reg[i].hooknum, + reg->dev);if (!pp)continue ;
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 4/5 netfilter: nf_tables_inet: don't use multihook infrastructure anymore
In reply to Pablo Neira AyusoUse new native NFPROTO_INET support in netfilter core, this gets rid of ad-hoc code in the nf_tables API codebase.
--- include/net/netfilter/nf_tables_ipv4.h | 2 -- include/net/netfilter/nf_tables_ipv6.h | 2 -- net/ipv4/netfilter/nf_tables_ipv4.c | 3 +- net/ipv6/netfilter/nf_tables_ipv6.c | 3 +- net/netfilter/nf_tables_inet.c | 66 +++++++++++++++++++++++++++------- 5 files changed, 56 insertions(+), 20 deletions(-)
include/net/netfilter/nf_tables_ipv4.h +0 -2
@@ -62,6 +62,4 @@ nft_set_pktinfo_ipv4_validate(struct nft_pktinfo *pkt, nft_set_pktinfo_proto_unspec(pkt, skb); } -extern struct nft_af_info nft_af_ipv4; -#endif
include/net/netfilter/nf_tables_ipv6.h +0 -2
@@ -78,6 +78,4 @@ nft_set_pktinfo_ipv6_validate(struct nft_pktinfo *pkt, nft_set_pktinfo_proto_unspec(pkt, skb); } -extern struct nft_af_info nft_af_ipv6; -#endif
net/ipv4/netfilter/nf_tables_ipv4.c +1 -2
@@ -44,7 +44,7 @@ static unsigned int nft_ipv4_output(void *priv,return nft_do_chain_ipv4 (priv, skb, state); } -struct nft_af_info nft_af_ipv4__read_mostly = { +static struct nft_af_info nft_af_ipv4__read_mostly = { .family =NFPROTO_IPV4 , .nhooks =NF_INET_NUMHOOKS , .owner =THIS_MODULE ,
@@ -57,7 +57,6 @@ struct nft_af_info nft_af_ipv4 __read_mostly = { [NF_INET_POST_ROUTING] = nft_do_chain_ipv4, }, }; -EXPORT_SYMBOL_GPL (nft_af_ipv4);static int nf_tables_ipv4_init_net (struct net *net) {
net/ipv6/netfilter/nf_tables_ipv6.c +1 -2
@@ -41,7 +41,7 @@ static unsigned int nft_ipv6_output(void *priv,return nft_do_chain_ipv6 (priv, skb, state); } -struct nft_af_info nft_af_ipv6__read_mostly = { +static struct nft_af_info nft_af_ipv6__read_mostly = { .family =NFPROTO_IPV6 , .nhooks =NF_INET_NUMHOOKS , .owner =THIS_MODULE ,
@@ -54,7 +54,6 @@ struct nft_af_info nft_af_ipv6 __read_mostly = { [NF_INET_POST_ROUTING] = nft_do_chain_ipv6, }, }; -EXPORT_SYMBOL_GPL (nft_af_ipv6);static int nf_tables_ipv6_init_net (struct net *net) {
net/netfilter/nf_tables_inet.c +54 -12
86 lines not on this page · raw message
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 5/5 netfilter: nf_tables: remove multihook chains and families
In reply to Pablo Neira AyusoSince NFPROTO_INET is handled from the core, we don't need to maintain extra infrastructure in nf_tables to handle the double hook registration, one for IPv4 and another for IPv6.
--- include/net/netfilter/nf_tables.h | 9 +-- 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_api.c | 97 ++++++++++++++------------------- net/netfilter/nf_tables_inet.c | 1 - net/netfilter/nf_tables_netdev.c | 3 +- net/netfilter/nft_compat.c | 8 +-- 9 files changed, 46 insertions(+), 76 deletions(-)
include/net/netfilter/nf_tables.h +1 -8
@@ -905,8 +905,6 @@ struct nft_stats {struct u64_stats_sync syncp; }; -#define NFT_HOOK_OPS_MAX 2 -/** * struct nft_base_chain - nf_tables base chain *
@@ -918,7 +916,7 @@ struct nft_stats {* @dev_name: device name that this base chain is attached to (if any) */ struct nft_base_chain { -struct nf_hook_ops ops[NFT_HOOK_OPS_MAX ]; +struct nf_hook_ops ops;const struct nf_chain_type *type;u8 policy;u8 flags;
@@ -979,8 +977,6 @@ enum nft_af_flags {* @owner: module owner * @tables: used internally * @flags: family flags -* @nops: number of hook ops in this family -* @hook_ops_init: initialization function for chain hook ops * @hooks: hookfn overrides for packet validation */ struct nft_af_info {
@@ -990,9 +986,6 @@ struct nft_af_info {struct module *owner;struct list_head tables;u32 flags; -unsigned int nops; -void (*hook_ops_init)(struct nf_hook_ops *, -unsigned int );nf_hookfn *hooks[NF_MAX_HOOKS ]; };
net/bridge/netfilter/nf_tables_bridge.c +0 -1
@@ -44,7 +44,6 @@ static struct nft_af_info nft_af_bridge __read_mostly = { .family = NFPROTO_BRIDGE, .nhooks = NF_BR_NUMHOOKS, .owner = THIS_MODULE, - .nops = 1, .hooks = { [NF_BR_PRE_ROUTING] = nft_do_chain_bridge, [NF_BR_LOCAL_IN] = nft_do_chain_bridge,
net/ipv4/netfilter/nf_tables_arp.c +0 -1
@@ -30,7 +30,6 @@ static struct nft_af_info nft_af_arp __read_mostly = { .family = NFPROTO_ARP, .nhooks = NF_ARP_NUMHOOKS, .owner = THIS_MODULE, - .nops = 1, .hooks = { [NF_ARP_IN] = nft_do_chain_arp, [NF_ARP_OUT] = nft_do_chain_arp,
net/ipv4/netfilter/nf_tables_ipv4.c +0 -1
@@ -48,7 +48,6 @@ static struct nft_af_info nft_af_ipv4 __read_mostly = { .family = NFPROTO_IPV4, .nhooks = NF_INET_NUMHOOKS, .owner = THIS_MODULE, - .nops = 1, .hooks = { [NF_INET_LOCAL_IN] = nft_do_chain_ipv4, [NF_INET_LOCAL_OUT] = nft_ipv4_output,
net/ipv6/netfilter/nf_tables_ipv6.c +0 -1
@@ -45,7 +45,6 @@ static struct nft_af_info nft_af_ipv6 __read_mostly = { .family = NFPROTO_IPV6, .nhooks = NF_INET_NUMHOOKS, .owner = THIS_MODULE, - .nops = 1, .hooks = { [NF_INET_LOCAL_IN] = nft_do_chain_ipv6, [NF_INET_LOCAL_OUT] = nft_ipv6_output,
net/netfilter/nf_tables_api.c +40 -57
211 lines not on this page · raw message
net/netfilter/nf_tables_inet.c +0 -1
@@ -70,7 +70,6 @@ static struct nft_af_info nft_af_inet __read_mostly = { .family = NFPROTO_INET, .nhooks = NF_INET_NUMHOOKS, .owner = THIS_MODULE, - .nops = 1, .hooks = { [NF_INET_LOCAL_IN] = nft_do_chain_inet, [NF_INET_LOCAL_OUT] = nft_inet_output,
net/netfilter/nf_tables_netdev.c +1 -2
@@ -41,7 +41,6 @@ static struct nft_af_info nft_af_netdev __read_mostly = { .nhooks = NF_NETDEV_NUMHOOKS, .owner = THIS_MODULE, .flags = NFT_AF_NEEDS_DEV, - .nops = 1, .hooks = { [NF_NETDEV_INGRESS] = nft_do_chain_netdev, },
@@ -96,7 +95,7 @@ static void nft_netdev_event(unsigned long event, struct net_device *dev,__nft_release_basechain (ctx);break ;case NETDEV_CHANGENAME : -if (dev->ifindex != basechain->ops[0 ].dev->ifindex) +if (dev->ifindex != basechain->ops.dev->ifindex)return ;strncpy (basechain->dev_name, dev->name,IFNAMSIZ );
net/netfilter/nft_compat.c +4 -4
@@ -169,7 +169,7 @@ nft_target_set_tgchk_param(struct xt_tgchk_param *par,if (nft_is_base_chain (ctx->chain)) {const struct nft_base_chain *basechain =nft_base_chain (ctx->chain); -const struct nf_hook_ops *ops = &basechain->ops[0 ]; +const struct nf_hook_ops *ops = &basechain->ops; par->hook_mask =1 << ops->hooknum; }else {
@@ -302,7 +302,7 @@ static int nft_target_validate(const struct nft_ctx *ctx,if (nft_is_base_chain (ctx->chain)) {const struct nft_base_chain *basechain =nft_base_chain (ctx->chain); -const struct nf_hook_ops *ops = &basechain->ops[0 ]; +const struct nf_hook_ops *ops = &basechain->ops; hook_mask =1 << ops->hooknum;if (target->hooks && !(hook_mask & target->hooks))
@@ -383,7 +383,7 @@ nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,if (nft_is_base_chain (ctx->chain)) {const struct nft_base_chain *basechain =nft_base_chain (ctx->chain); -const struct nf_hook_ops *ops = &basechain->ops[0 ]; +const struct nf_hook_ops *ops = &basechain->ops; par->hook_mask =1 << ops->hooknum; }else {
@@ -481,7 +481,7 @@ static int nft_match_validate(const struct nft_ctx *ctx,if (nft_is_base_chain (ctx->chain)) {const struct nft_base_chain *basechain =nft_base_chain (ctx->chain); -const struct nf_hook_ops *ops = &basechain->ops[0 ]; +const struct nf_hook_ops *ops = &basechain->ops; hook_mask =1 << ops->hooknum;if (match->hooks && !(hook_mask & match->hooks))
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 3/5 netfilter: handle NFPROTO_INET family hook registration from core
In reply to Pablo Neira AyusoExpand NFPROTO_INET in two hook registrations, one for NFPROTO_IPV4 and another for NFPROTO_IPV6. Hence, we handle NFPROTO_INET as a real family from the core.
--- net/netfilter/core.c | 61 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 8 deletions(-)
net/netfilter/core.c +53 -8
@@ -275,12 +275,13 @@ nf_hook_entry_head(struct net *net, int pf, unsigned int hooknum,return NULL ; } -int nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) +static int __nf_register_net_hook (struct net *net,int pf, +const struct nf_hook_ops *reg) {struct nf_hook_entries *p, *new_hooks;struct nf_hook_entries __rcu **pp; -if (reg->pf ==NFPROTO_NETDEV ) { +if (pf ==NFPROTO_NETDEV ) {#ifndef CONFIG_NETFILTER_INGRESS if (reg->hooknum ==NF_NETDEV_INGRESS )return -EOPNOTSUPP ;
@@ -290,7 +291,7 @@ int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)return -EINVAL ; } - pp =nf_hook_entry_head (net, reg->pf, reg->hooknum, reg->dev); + pp =nf_hook_entry_head (net, pf, reg->hooknum, reg->dev);if (!pp)return -EINVAL ;
@@ -308,18 +309,17 @@ int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)hooks_validate (new_hooks);#ifdef CONFIG_NETFILTER_INGRESS -if (reg->pf ==NFPROTO_NETDEV && reg->hooknum ==NF_NETDEV_INGRESS ) +if (pf ==NFPROTO_NETDEV && reg->hooknum ==NF_NETDEV_INGRESS )net_inc_ingress_queue ();#endif #ifdef HAVE_JUMP_LABEL -static_key_slow_inc (&nf_hooks_needed[reg->pf][reg->hooknum]); +static_key_slow_inc (&nf_hooks_needed[pf][reg->hooknum]);#endif synchronize_net ();BUG_ON (p == new_hooks);kvfree (p);return 0 ; } -EXPORT_SYMBOL (nf_register_net_hook);/* * nf_remove_net_hook - remove a hook from blob
@@ -360,13 +360,14 @@ static void nf_remove_net_hook(struct nf_hook_entries *old, } } -void nf_unregister_net_hook (struct net *net,const struct nf_hook_ops *reg) +static void __nf_unregister_net_hook (struct net *net,int pf, +const struct nf_hook_ops *reg) {struct nf_hook_entries __rcu **pp;struct nf_hook_entries *p;unsigned int nfq; - pp =nf_hook_entry_head (net, reg->pf, reg->hooknum, reg->dev); + pp =nf_hook_entry_head (net, pf, reg->hooknum, reg->dev);if (!pp)return ;
@@ -393,8 +394,52 @@ void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)synchronize_net ();kvfree (p); } + +void nf_unregister_net_hook (struct net *net,const struct nf_hook_ops *reg) +{ +switch (reg->pf) { +case NFPROTO_INET : +__nf_unregister_net_hook (net,NFPROTO_IPV4 , reg); +__nf_unregister_net_hook (net,NFPROTO_IPV6 , reg); +break ; +default : +__nf_unregister_net_hook (net, reg->pf, reg); +break ; + } +}EXPORT_SYMBOL (nf_unregister_net_hook); +int nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) +{ +int err; + +if (reg->pf ==NFPROTO_INET ) { + err =__nf_register_net_hook (net,NFPROTO_IPV4 , reg); +if (err <0 ) +return err; + + err =__nf_register_net_hook (net,NFPROTO_IPV6 , reg); +if (err <0 ) { +__nf_unregister_net_hook (net,NFPROTO_IPV4 , reg); +return err; + } + }else { + err =__nf_register_net_hook (net, reg->pf, reg); +if (err <0 ) +return err; + } + +#ifdef CONFIG_NETFILTER_INGRESS +if (reg->pf ==NFPROTO_NETDEV && reg->hooknum ==NF_NETDEV_INGRESS ) +net_inc_ingress_queue (); +#endif +#ifdef HAVE_JUMP_LABEL +static_key_slow_inc (&nf_hooks_needed[reg->pf][reg->hooknum]); +#endif +return 0 ; +} +EXPORT_SYMBOL (nf_register_net_hook); +int nf_register_net_hooks (struct net *net,const struct nf_hook_ops *reg,unsigned int n) {
Signature · 1 line
2.11.0