Netfilter updates for net-next
Subsystems (4)
Pablo Neira Ayuso
maintainercover Netfilter updates for net-next
Hi David,
The following patchset contains Netfilter updates for net-next, they are:
1) A couple of cleanups for the netfilter core hook from Eric Biederman.
2) Net namespace hook registration, also from Eric. This adds a dependency with the rtnl_lock. This should be fine by now but we have to keep an eye on this because if we ever get the per-subsys nfnl_lock before rtnl we have may problems in the future. But we have room to remove this in the future by propagating the complexity to the clients, by registering hooks for the init netns functions.
3) Update nf_tables to use the new net namespace hook infrastructure, also from Eric.
4) Three patches to refine and to address problems from the new net namespace hook infrastructure.
5) Switch to alternate jumpstack in xtables iff the packet is reentering. This only applies to a very special case, the TEE target, but Eric Dumazet reports that this is slowing down things for everyone else. So let's only switch to the alternate jumpstack if the tee target is in used through a static key. This batch also comes with offline precalculation of the jumpstack based on the callchain depth. From Florian Westphal.
6) Minimal SCTP multihoming support for our conntrack helper, from Michal Kubecek.
7) Reduce nf_bridge_info per skbuff scratchpad area to 32 bytes, from Florian Westphal.
8) Fix several checkpatch errors in bridge netfilter, from Bernhard Thaler.
9) Get rid of useless debug message in ip6t_REJECT, from Subash Abhinov.
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git
Thanks!
----------------------------------------------------------------
The following changes since commit 0d6ef0688d8744454646298b85336407be05e309:
ipvs: Delete an unnecessary check before the function call "module_put" (2015-07-15 17:51:22 +0200)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git master
for you to fetch changes up to a6cd379b4d68867295ea35a719008e86d7a2ee9f:
netfilter: ip6t_REJECT: Remove debug messages from reject_tg6() (2015-08-04 11:12:51 +0200)
----------------------------------------------------------------
Bernhard Thaler (1):
netfilter: bridge: do not initialize statics to 0 or NULL
Eric W. Biederman (6):
netfilter: kill nf_hooks_active
netfilter: Simply the tests for enabling and disabling the ingress queue hook
netfilter: Factor out the hook list selection from nf_register_hook
netfilter: Per network namespace netfilter hooks.
netfilter: nftables: Only run the nftables chains in the proper netns
netfilter: Fix memory leak in nf_register_net_hook
Florian Westphal (6):
netfilter: xtables: compute exact size needed for jumpstack
netfilter: move tee_active to core
netfilter: xtables: don't save/restore jumpstack offset
netfilter: add and use jump label for xt_tee
netfilter: xtables: remove __pure annotation
netfilter: bridge: reduce nf_bridge_info to 32 bytes again
Michal Kubeček (1):
netfilter: nf_ct_sctp: minimal multihoming support
Pablo Neira Ayuso (3):
netfilter: nf_queue: fix nf_queue_nf_hook_drop()
netfilter: fix possible removal of wrong hook
netfilter: rename local nf_hook_list to hook_list
Subash Abhinov Kasiviswanathan (1):
netfilter: ip6t_REJECT: Remove debug messages from reject_tg6()
include/linux/netfilter.h | 42 ++-- include/linux/netfilter/x_tables.h | 8 +- include/linux/netfilter_bridge.h | 12 +- include/linux/skbuff.h | 19 +- include/net/netns/netfilter.h | 1 + include/uapi/linux/netfilter/nf_conntrack_sctp.h | 2 + include/uapi/linux/netfilter/nfnetlink_cttimeout.h | 2 + net/bridge/br_netfilter_hooks.c | 20 +- net/bridge/br_netfilter_ipv6.c | 2 +- net/ipv4/netfilter/arp_tables.c | 32 +-- net/ipv4/netfilter/ip_tables.c | 68 +++--- net/ipv4/netfilter/nf_defrag_ipv4.c | 7 +- net/ipv6/netfilter/ip6_tables.c | 52 +++-- net/ipv6/netfilter/ip6t_REJECT.c | 5 - net/ipv6/netfilter/nf_defrag_ipv6_hooks.c | 7 +- net/netfilter/core.c | 225 ++++++++++++++++---- net/netfilter/nf_conntrack_proto_sctp.c | 101 ++++++--- net/netfilter/nf_internals.h | 2 +- net/netfilter/nf_queue.c | 12 +- net/netfilter/nf_tables_api.c | 8 +- net/netfilter/nf_tables_core.c | 5 - net/netfilter/x_tables.c | 29 ++- net/netfilter/xt_TEE.c | 15 +- 23 files changed, 460 insertions(+), 216 deletions(-)
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
maintainerPATCH 1/18 netfilter: kill nf_hooks_active
From: "Eric W. Biederman" <[email protected]>
The function obscures what is going on in nf_hook_thresh and it's existence requires computing the hook list twice.
--- include/linux/netfilter.h | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
include/linux/netfilter.h +4 -7
@@ -150,11 +150,6 @@ static inline bool nf_hook_list_active(struct list_head *nf_hook_list, }#endif -static inline bool nf_hooks_active (u_int8_t pf,unsigned int hook) -{ -return nf_hook_list_active (&nf_hooks[pf][hook], pf, hook); -} -int nf_hook_slow (struct sk_buff *skb,struct nf_hook_state *state);/**
@@ -172,10 +167,12 @@ static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook, int (*okfn)(struct sock *, struct sk_buff *), int thresh) { - if (nf_hooks_active(pf, hook)) { +struct list_head *nf_hook_list = &nf_hooks[pf][hook]; + +if (nf_hook_list_active (nf_hook_list, pf, hook)) {struct nf_hook_state state; -nf_hook_state_init (&state, &nf_hooks[pf][hook], hook, thresh, +nf_hook_state_init (&state, nf_hook_list, hook, thresh, pf, indev, outdev, sk, okfn);return nf_hook_slow (skb, &state); }
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 2/18 netfilter: Simply the tests for enabling and disabling the ingress queue hook
In reply to Pablo Neira AyusoFrom: "Eric W. Biederman" <[email protected]>
Replace an overcomplicated switch statement with a simple if statement.
This also removes the ingress queue enable outside of nf_hook_mutex as the protection provided by the mutex is not necessary and the code is clearer having both of the static key increments together.
--- net/netfilter/core.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
net/netfilter/core.c +6 -11
@@ -74,7 +74,6 @@ int nf_register_hook(struct nf_hook_ops *reg)if (reg->hooknum ==NF_NETDEV_INGRESS ) {BUG_ON (reg->dev ==NULL ); nf_hook_list = ®->dev->nf_hooks_ingress; -net_inc_ingress_queue ();break ; }#endif
@@ -90,6 +89,10 @@ int nf_register_hook(struct nf_hook_ops *reg) }list_add_rcu (®->list, elem->list.prev);mutex_unlock (&nf_hook_mutex); +#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
@@ -102,18 +105,10 @@ void nf_unregister_hook(struct nf_hook_ops *reg)mutex_lock (&nf_hook_mutex);list_del_rcu (®->list);mutex_unlock (&nf_hook_mutex); -switch (reg->pf) { -case NFPROTO_NETDEV :#ifdef CONFIG_NETFILTER_INGRESS -if (reg->hooknum ==NF_NETDEV_INGRESS ) { -net_dec_ingress_queue (); -break ; - } -break ; +if (reg->pf ==NFPROTO_NETDEV && reg->hooknum ==NF_NETDEV_INGRESS ) +net_dec_ingress_queue ();#endif -default : -break ; - }#ifdef HAVE_JUMP_LABEL static_key_slow_dec (&nf_hooks_needed[reg->pf][reg->hooknum]);#endif
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 3/18 netfilter: Factor out the hook list selection from nf_register_hook
In reply to Pablo Neira AyusoFrom: "Eric W. Biederman" <[email protected]>
- Add a new function find_nf_hook_list to select the nf_hook_list
- Fail nf_register_hook when asked for a per netdevice hook list when support for per netdevice hook lists is not built into the kernel.
- Move the hook list head selection outside of nf_hook_mutex as nothing in the selection requires the hook list, and error handling is simpler if a mutex is not held.
--- net/netfilter/core.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-)
net/netfilter/core.c +18 -14
@@ -62,27 +62,31 @@ EXPORT_SYMBOL(nf_hooks_needed);static DEFINE_MUTEX (nf_hook_mutex); -int nf_register_hook (struct nf_hook_ops *reg) +static struct list_head *find_nf_hook_list (const struct nf_hook_ops *reg) { -struct list_head *nf_hook_list; -struct nf_hook_ops *elem; +struct list_head *nf_hook_list =NULL ; -mutex_lock (&nf_hook_mutex); -switch (reg->pf) { -case NFPROTO_NETDEV : +if (reg->pf !=NFPROTO_NETDEV ) + nf_hook_list = &nf_hooks[reg->pf][reg->hooknum]; +else if (reg->hooknum ==NF_NETDEV_INGRESS ) {#ifdef CONFIG_NETFILTER_INGRESS -if (reg->hooknum ==NF_NETDEV_INGRESS ) { -BUG_ON (reg->dev ==NULL ); +if (reg->dev) nf_hook_list = ®->dev->nf_hooks_ingress; -break ; - }#endif -/* Fall through. */ -default : - nf_hook_list = &nf_hooks[reg->pf][reg->hooknum]; -break ; } +return nf_hook_list; +} + +int nf_register_hook (struct nf_hook_ops *reg) +{ +struct list_head *nf_hook_list; +struct nf_hook_ops *elem; + nf_hook_list =find_nf_hook_list (reg); +if (!nf_hook_list) +return -ENOENT ; + +mutex_lock (&nf_hook_mutex);list_for_each_entry (elem, nf_hook_list, list) {if (reg->priority < elem->priority)break ;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 4/18 netfilter: Per network namespace netfilter hooks.
In reply to Pablo Neira AyusoFrom: "Eric W. Biederman" <[email protected]>
- Add a new set of functions for registering and unregistering per network namespace hooks.
- Modify the old global namespace hook functions to use the per network namespace hooks in their implementation, so their remains a single list that needs to be walked for any hook (this is important for keeping the hook priority working and for keeping the code walking the hooks simple).
- Only allow registering the per netdevice hooks in the network namespace where the network device lives.
- Dynamically allocate the structures in the per network namespace hook list in nf_register_net_hook, and unregister them in nf_unregister_net_hook.
Dynamic allocate is required somewhere as the number of network namespaces are not fixed so we might as well allocate them in the registration function.
The chain of registered hooks on any list is expected to be small so the cost of walking that list to find the entry we are unregistering should also be small.
Performing the management of the dynamically allocated list entries in the registration and unregistration functions keeps the complexity from spreading.
--- include/linux/netfilter.h | 14 +++- include/net/netns/netfilter.h | 1 + net/netfilter/core.c | 182 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 173 insertions(+), 24 deletions(-)
include/linux/netfilter.h +11 -3
@@ -11,6 +11,8 @@#include <linux/list.h> #include <linux/static_key.h> #include <linux/netfilter_defs.h> +#include <linux/netdevice.h> +#include <net/net_namespace.h> #ifdef CONFIG_NETFILTER static inline int NF_DROP_GETERR (int verdict)
@@ -118,6 +120,13 @@ struct nf_sockopt_ops { };/* Function to register/unregister hook points. */ +int nf_register_net_hook (struct net *net,const struct nf_hook_ops *ops); +void nf_unregister_net_hook (struct net *net,const struct nf_hook_ops *ops); +int nf_register_net_hooks (struct net *net,const struct nf_hook_ops *reg, +unsigned int n); +void nf_unregister_net_hooks (struct net *net,const struct nf_hook_ops *reg, +unsigned int n); +int nf_register_hook (struct nf_hook_ops *reg);void nf_unregister_hook (struct nf_hook_ops *reg);int nf_register_hooks (struct nf_hook_ops *reg,unsigned int n);
@@ -128,8 +137,6 @@ void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);int nf_register_sockopt (struct nf_sockopt_ops *reg);void nf_unregister_sockopt (struct nf_sockopt_ops *reg); -extern struct list_head nf_hooks[NFPROTO_NUMPROTO ][NF_MAX_HOOKS ]; -#ifdef HAVE_JUMP_LABEL extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO ][NF_MAX_HOOKS ];
@@ -167,7 +174,8 @@ static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook, int (*okfn)(struct sock *, struct sk_buff *), int thresh) { -struct list_head *nf_hook_list = &nf_hooks[pf][hook]; +struct net *net =dev_net (indev ? indev : outdev); +struct list_head *nf_hook_list = &net->nf.hooks[pf][hook];if (nf_hook_list_active (nf_hook_list, pf, hook)) {struct nf_hook_state state;
include/net/netns/netfilter.h +1 -0
@@ -14,5 +14,6 @@ struct netns_nf {#ifdef CONFIG_SYSCTL struct ctl_table_header *nf_log_dir_header;#endif + struct list_head hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; };#endif
net/netfilter/core.c +161 -21
255 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 5/18 netfilter: nftables: Only run the nftables chains in the proper netns
In reply to Pablo Neira AyusoFrom: "Eric W. Biederman" <[email protected]>
- Register the nftables chains in the network namespace that they need to run in.
- Remove the hacks that stopped chains running in the wrong network namespace.
--- net/netfilter/nf_tables_api.c | 8 ++++++-- net/netfilter/nf_tables_core.c | 5 ----- 2 files changed, 6 insertions(+), 7 deletions(-)
net/netfilter/nf_tables_api.c +6 -2
@@ -130,20 +130,24 @@ static void nft_trans_destroy(struct nft_trans *trans)int nft_register_basechain (struct nft_base_chain *basechain,unsigned int hook_nops) { +struct net *net =read_pnet (&basechain->pnet); +if (basechain->flags &NFT_BASECHAIN_DISABLED )return 0 ; -return nf_register_hooks (basechain->ops, hook_nops); +return nf_register_net_hooks (net, basechain->ops, hook_nops); }EXPORT_SYMBOL_GPL (nft_register_basechain);void nft_unregister_basechain (struct nft_base_chain *basechain,unsigned int hook_nops) { +struct net *net =read_pnet (&basechain->pnet); +if (basechain->flags &NFT_BASECHAIN_DISABLED )return ; -nf_unregister_hooks (basechain->ops, hook_nops); +nf_unregister_net_hooks (net, basechain->ops, hook_nops); }EXPORT_SYMBOL_GPL (nft_unregister_basechain);
net/netfilter/nf_tables_core.c +0 -5
@@ -114,7 +114,6 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt, const struct nf_hook_ops *ops) { const struct nft_chain *chain = ops->priv, *basechain = chain; -const struct net *chain_net =read_pnet (&nft_base_chain (basechain)->pnet);const struct net *net =dev_net (pkt->in ? pkt->in : pkt->out);const struct nft_rule *rule;const struct nft_expr *expr, *last;
@@ -125,10 +124,6 @@ nft_do_chain(struct nft_pktinfo *pkt, const struct nf_hook_ops *ops)int rulenum;unsigned int gencursor =nft_genmask_cur (net); -/* Ignore chains that are not for the current network namespace */ -if (!net_eq (net, chain_net)) -return NF_ACCEPT ; - do_chain: rulenum =0 ; rule = list_entry(&chain->rules, struct nft_rule, list);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 6/18 netfilter: xtables: compute exact size needed for jumpstack
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
The {arp,ip,ip6tables} jump stack is currently sized based on the number of user chains.
However, its rather unlikely that every user defined chain jumps to the next, so lets use the existing loop detection logic to also track the chain depths.
The stacksize is then set to the largest chain depth seen.
--- net/ipv4/netfilter/arp_tables.c | 19 ++++++++++++------- net/ipv4/netfilter/ip_tables.c | 28 ++++++++++++++++++---------- net/ipv6/netfilter/ip6_tables.c | 23 +++++++++++++++-------- net/netfilter/x_tables.c | 4 ++++ 4 files changed, 49 insertions(+), 25 deletions(-)
net/ipv4/netfilter/arp_tables.c +12 -7
@@ -372,10 +372,13 @@ static inline bool unconditional(const struct arpt_arp *arp)/* Figures out from what hook each rule can be called: returns 0 if * there are loops. Puts hook bitmask in comefrom. +* +* Keeps track of largest call depth seen and stores it in newinfo->stacksize. */ -static int mark_source_chains (const struct xt_table_info *newinfo, +static int mark_source_chains (struct xt_table_info *newinfo,unsigned int valid_hooks,void *entry0) { +unsigned int calldepth, max_calldepth =0 ;unsigned int hook;/* No recursion; use packet counter to save back ptrs (reset
@@ -391,6 +394,7 @@ static int mark_source_chains(const struct xt_table_info *newinfo,/* Set initial back pointer. */ e->counters.pcnt = pos; + calldepth =0 ;for (;;) {const struct xt_standard_target *t
@@ -445,6 +449,8 @@ static int mark_source_chains(const struct xt_table_info *newinfo, (entry0 + pos + size); e->counters.pcnt = pos; pos += size; +if (calldepth >0 ) + --calldepth; }else {int newpos = t->verdict;
@@ -459,6 +465,10 @@ static int mark_source_chains(const struct xt_table_info *newinfo,return 0 ; } +if (entry0 + newpos !=arpt_next_entry (e) && + ++calldepth > max_calldepth) + max_calldepth = calldepth; +/* This a jump; chase it. */ duprintf ("Jump rule %u -> %u\n" , pos, newpos);
@@ -475,6 +485,7 @@ static int mark_source_chains(const struct xt_table_info *newinfo, next:duprintf ("Finished chain %u\n" , hook); } + newinfo->stacksize = max_calldepth;return 1 ; }
@@ -664,9 +675,6 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0,if (ret !=0 )break ; ++i; -if (strcmp (arpt_get_target (iter)->u.user.name, -XT_ERROR_TARGET ) ==0 ) - ++newinfo->stacksize; }duprintf ("translate_table: ARPT_ENTRY_ITERATE gives %d\n" , ret);if (ret !=0 )
@@ -1439,9 +1447,6 @@ static int translate_compat_table(const char *name,break ; } ++i; -if (strcmp (arpt_get_target (iter1)->u.user.name, -XT_ERROR_TARGET ) ==0 ) - ++newinfo->stacksize; }if (ret) {/*
net/ipv4/netfilter/ip_tables.c +18 -10
@@ -439,11 +439,15 @@ ipt_do_table(struct sk_buff *skb, }/* Figures out from what hook each rule can be called: returns 0 if -there are loops. Puts hook bitmask in comefrom. */ +* there are loops. Puts hook bitmask in comefrom. +* +* Keeps track of largest call depth seen and stores it in newinfo->stacksize. +*/ static int -mark_source_chains (const struct xt_table_info *newinfo, +mark_source_chains (struct xt_table_info *newinfo,unsigned int valid_hooks,void *entry0) { +unsigned int calldepth, max_calldepth =0 ;unsigned int hook;/* No recursion; use packet counter to save back ptrs (reset
@@ -457,6 +461,7 @@ mark_source_chains(const struct xt_table_info *newinfo,/* Set initial back pointer. */ e->counters.pcnt = pos; + calldepth =0 ;for (;;) {const struct xt_standard_target *t
@@ -518,6 +523,9 @@ mark_source_chains(const struct xt_table_info *newinfo, (entry0 + pos + size); e->counters.pcnt = pos; pos += size; +WARN_ON_ONCE (calldepth ==0 ); +if (calldepth >0 ) + --calldepth; }else {int newpos = t->verdict;
@@ -531,9 +539,14 @@ mark_source_chains(const struct xt_table_info *newinfo, newpos);return 0 ; } +if (entry0 + newpos !=ipt_next_entry (e) && + !(e->ip.flags &IPT_F_GOTO ) && + ++calldepth > max_calldepth) + max_calldepth = calldepth; +/* This a jump; chase it. */ -duprintf ("Jump rule %u -> %u\n" , - pos, newpos); +duprintf ("Jump rule %u -> %u, calldepth %d\n" , + pos, newpos, calldepth); }else {/* ... this is a fallthru */ newpos = pos + e->next_offset;
@@ -547,6 +560,7 @@ mark_source_chains(const struct xt_table_info *newinfo, next:duprintf ("Finished chain %u\n" , hook); } + newinfo->stacksize = max_calldepth;return 1 ; }
@@ -826,9 +840,6 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,if (ret !=0 )return ret; ++i; -if (strcmp (ipt_get_target (iter)->u.user.name, -XT_ERROR_TARGET ) ==0 ) - ++newinfo->stacksize; }if (i != repl->num_entries) {
@@ -1744,9 +1755,6 @@ translate_compat_table(struct net *net,if (ret !=0 )break ; ++i; -if (strcmp (ipt_get_target (iter1)->u.user.name, -XT_ERROR_TARGET ) ==0 ) - ++newinfo->stacksize; }if (ret) {/*
net/ipv6/netfilter/ip6_tables.c +15 -8
@@ -452,11 +452,15 @@ ip6t_do_table(struct sk_buff *skb, }/* Figures out from what hook each rule can be called: returns 0 if -there are loops. Puts hook bitmask in comefrom. */ +* there are loops. Puts hook bitmask in comefrom. +* +* Keeps track of largest call depth seen and stores it in newinfo->stacksize. +*/ static int -mark_source_chains (const struct xt_table_info *newinfo, +mark_source_chains (struct xt_table_info *newinfo,unsigned int valid_hooks,void *entry0) { +unsigned int calldepth, max_calldepth =0 ;unsigned int hook;/* No recursion; use packet counter to save back ptrs (reset
@@ -470,6 +474,7 @@ mark_source_chains(const struct xt_table_info *newinfo,/* Set initial back pointer. */ e->counters.pcnt = pos; + calldepth =0 ;for (;;) {const struct xt_standard_target *t
@@ -531,6 +536,8 @@ mark_source_chains(const struct xt_table_info *newinfo, (entry0 + pos + size); e->counters.pcnt = pos; pos += size; +if (calldepth >0 ) + --calldepth; }else {int newpos = t->verdict;
@@ -544,6 +551,11 @@ mark_source_chains(const struct xt_table_info *newinfo, newpos);return 0 ; } +if (entry0 + newpos !=ip6t_next_entry (e) && + !(e->ipv6.flags &IP6T_F_GOTO ) && + ++calldepth > max_calldepth) + max_calldepth = calldepth; +/* This a jump; chase it. */ duprintf ("Jump rule %u -> %u\n" , pos, newpos);
@@ -560,6 +572,7 @@ mark_source_chains(const struct xt_table_info *newinfo, next:duprintf ("Finished chain %u\n" , hook); } + newinfo->stacksize = max_calldepth;return 1 ; }
@@ -839,9 +852,6 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,if (ret !=0 )return ret; ++i; -if (strcmp (ip6t_get_target (iter)->u.user.name, -XT_ERROR_TARGET ) ==0 ) - ++newinfo->stacksize; }if (i != repl->num_entries) {
@@ -1754,9 +1764,6 @@ translate_compat_table(struct net *net,if (ret !=0 )break ; ++i; -if (strcmp (ip6t_get_target (iter1)->u.user.name, -XT_ERROR_TARGET ) ==0 ) - ++newinfo->stacksize; }if (ret) {/*
net/netfilter/x_tables.c +4 -0
@@ -749,6 +749,10 @@ static int xt_jumpstack_alloc(struct xt_table_info *i)if (i->jumpstack ==NULL )return -ENOMEM ; +/* ruleset without jumps -- no stack needed */ +if (i->stacksize ==0 ) +return 0 ; + i->stacksize *= xt_jumpstack_multiplier; size =sizeof (void *) * i->stacksize;for_each_possible_cpu (cpu) {
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 7/18 netfilter: move tee_active to core
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
This prepares for a TEE like expression in nftables. We want to ensure only one duplicate is sent, so both will use the same percpu variable to detect duplication.
The other use case is detection of recursive call to xtables, but since we don't want dependency from nft to xtables core its put into core.c instead of the x_tables core.
--- include/linux/netfilter.h | 11 +++++++++++ net/netfilter/core.c | 3 +++ net/netfilter/xt_TEE.c | 13 ++++++------- 3 files changed, 20 insertions(+), 7 deletions(-)
include/linux/netfilter.h +11 -0
@@ -390,4 +390,15 @@ extern struct nfq_ct_hook __rcu *nfq_ct_hook;static inline void nf_ct_attach (struct sk_buff *new,struct sk_buff *skb) {}#endif +/** +* nf_skb_duplicated - TEE target has sent a packet +* +* When a xtables target sends a packet, the OUTPUT and POSTROUTING +* hooks are traversed again, i.e. nft and xtables are invoked recursively. +* +* This is used by xtables TEE target to prevent the duplicated skb from +* being duplicated again. +*/ +DECLARE_PER_CPU (bool, nf_skb_duplicated); +#endif /*__LINUX_NETFILTER_H*/
net/netfilter/core.c +3 -0
@@ -34,6 +34,9 @@ EXPORT_SYMBOL(nf_afinfo);const struct nf_ipv6_ops __rcu *nf_ipv6_ops__read_mostly ;EXPORT_SYMBOL_GPL (nf_ipv6_ops); +DEFINE_PER_CPU (bool, nf_skb_duplicated); +EXPORT_SYMBOL_GPL (nf_skb_duplicated); +int nf_register_afinfo (const struct nf_afinfo *afinfo) {mutex_lock (&afinfo_mutex);
net/netfilter/xt_TEE.c +6 -7
@@ -37,7 +37,6 @@ struct xt_tee_priv { };static const union nf_inet_addr tee_zero_address; -static DEFINE_PER_CPU(bool, tee_active);static struct net *pick_net (struct sk_buff *skb) {
@@ -88,7 +87,7 @@ tee_tg4(struct sk_buff *skb, const struct xt_action_param *par)const struct xt_tee_tginfo *info = par->targinfo;struct iphdr *iph; -if (__this_cpu_read (tee_active)) +if (__this_cpu_read (nf_skb_duplicated))return XT_CONTINUE ;/* * Copy the skb, and route the copy. Will later return %XT_CONTINUE for
@@ -125,9 +124,9 @@ tee_tg4(struct sk_buff *skb, const struct xt_action_param *par)ip_send_check (iph);if (tee_tg_route4 (skb, info)) { -__this_cpu_write (tee_active, true); +__this_cpu_write (nf_skb_duplicated, true);ip_local_out (skb); -__this_cpu_write (tee_active, false); +__this_cpu_write (nf_skb_duplicated, false); }else {kfree_skb (skb); }
@@ -170,7 +169,7 @@ tee_tg6(struct sk_buff *skb, const struct xt_action_param *par) {const struct xt_tee_tginfo *info = par->targinfo; -if (__this_cpu_read (tee_active)) +if (__this_cpu_read (nf_skb_duplicated))return XT_CONTINUE ; skb =pskb_copy (skb,GFP_ATOMIC );if (skb ==NULL )
@@ -188,9 +187,9 @@ tee_tg6(struct sk_buff *skb, const struct xt_action_param *par) --iph->hop_limit; }if (tee_tg_route6 (skb, info)) { -__this_cpu_write (tee_active, true); +__this_cpu_write (nf_skb_duplicated, true);ip6_local_out (skb); -__this_cpu_write (tee_active, false); +__this_cpu_write (nf_skb_duplicated, false); }else {kfree_skb (skb); }
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 8/18 netfilter: xtables: don't save/restore jumpstack offset
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
In most cases there is no reentrancy into ip/ip6tables.
For skbs sent by REJECT or SYNPROXY targets, there is one level of reentrancy, but its not relevant as those targets issue an absolute verdict, i.e. the jumpstack can be clobbered since its not used after the target issues absolute verdict (ACCEPT, DROP, STOLEN, etc).
So the only special case where it is relevant is the TEE target, which returns XT_CONTINUE.
This patch changes ip(6)_do_table to always use the jump stack starting from 0.
When we detect we're operating on an skb sent via TEE (percpu nf_skb_duplicated is 1) we switch to an alternate stack to leave the original one alone.
Since there is no TEE support for arptables, it doesn't need to test if tee is active.
The jump stack overflow tests are no longer needed as well -- since ->stacksize is the largest call depth we cannot exceed it.
A much better alternative to the external jumpstack would be to just declare a jumps[32] stack on the local stack frame, but that would mean we'd have to reject iptables rulesets that used to work before.
Another alternative would be to start rejecting rulesets with a larger call depth, e.g. 1000 -- in this case it would be feasible to allocate the entire stack in the percpu area which would avoid one dereference.
--- include/linux/netfilter/x_tables.h | 1 - net/ipv4/netfilter/arp_tables.c | 11 +++-------- net/ipv4/netfilter/ip_tables.c | 37 +++++++++++++++++++----------------- net/ipv6/netfilter/ip6_tables.c | 26 +++++++++++++------------ net/netfilter/x_tables.c | 22 ++++++++++----------- 5 files changed, 48 insertions(+), 49 deletions(-)
include/linux/netfilter/x_tables.h +0 -1
@@ -222,7 +222,6 @@ struct xt_table_info {* @stacksize jumps (number of user chains) can possibly be made. */ unsigned int stacksize; -unsigned int __percpu *stackptr;void ***jumpstack;unsigned char entries[0 ]__aligned(8) ;
net/ipv4/netfilter/arp_tables.c +3 -8
@@ -280,6 +280,9 @@ unsigned int arpt_do_table(struct sk_buff *skb, table_base = private->entries; jumpstack = (struct arpt_entry **)private->jumpstack[cpu]; +/* No TEE support for arptables, so no need to switch to alternate +* stack. All targets that reenter must return absolute verdicts. +*/ e =get_entry (table_base, private->hook_entry[hook]); acpar.in = state->in;
@@ -325,11 +328,6 @@ unsigned int arpt_do_table(struct sk_buff *skb, }if (table_base + v !=arpt_next_entry (e)) { - -if (stackidx >= private->stacksize) { - verdict =NF_DROP ; -break ; - } jumpstack[stackidx++] = e; }
@@ -337,9 +335,6 @@ unsigned int arpt_do_table(struct sk_buff *skb,continue ; } -/* Targets which reenter must return -* abs. verdicts -*/ acpar.target = t->u.kernel.target; acpar.targinfo = t->data; verdict = t->u.kernel.target->target (skb, &acpar);
net/ipv4/netfilter/ip_tables.c +20 -17
@@ -296,12 +296,13 @@ ipt_do_table(struct sk_buff *skb,const char *indev, *outdev;const void *table_base;struct ipt_entry *e, **jumpstack; -unsigned int *stackptr, origptr, cpu; +unsigned int stackidx, cpu;const struct xt_table_info *private;struct xt_action_param acpar;unsigned int addend;/* Initialization */ + stackidx =0 ; ip =ip_hdr (skb); indev = state->in ? state->in->name : nulldevname; outdev = state->out ? state->out->name : nulldevname;
@@ -331,13 +332,20 @@ ipt_do_table(struct sk_buff *skb,smp_read_barrier_depends (); table_base = private->entries; jumpstack = (struct ipt_entry **)private->jumpstack[cpu]; - stackptr =per_cpu_ptr (private->stackptr, cpu); - origptr = *stackptr; + +/* Switch to alternate jumpstack if we're being invoked via TEE. +* TEE issues XT_CONTINUE verdict on original skb so we must not +* clobber the jumpstack. +* +* For recursion via REJECT or SYNPROXY the stack will be clobbered +* but it is no problem since absolute verdict is issued by these. +*/ + jumpstack += private->stacksize *__this_cpu_read (nf_skb_duplicated); e =get_entry (table_base, private->hook_entry[hook]); -pr_debug ("Entering %s(hook %u); sp at %u (UF %p)\n" , - table->name, hook, origptr, +pr_debug ("Entering %s(hook %u), UF %p\n" , + table->name, hook,get_entry (table_base, private->underflow[hook])); do {
@@ -383,28 +391,24 @@ ipt_do_table(struct sk_buff *skb, verdict = (unsigned int )(-v) -1 ;break ; } -if (*stackptr <= origptr) { +if (stackidx ==0 ) { e =get_entry (table_base, private->underflow[hook]);pr_debug ("Underflow (this is normal) " "to %p\n" , e); }else { - e = jumpstack[--*stackptr]; + e = jumpstack[--stackidx];pr_debug ("Pulled %p out from pos %u\n" , - e, *stackptr); + e, stackidx); e =ipt_next_entry (e); }continue ; }if (table_base + v !=ipt_next_entry (e) && !(e->ip.flags &IPT_F_GOTO )) { -if (*stackptr >= private->stacksize) { - verdict =NF_DROP ; -break ; - } - jumpstack[(*stackptr)++] = e; + jumpstack[stackidx++] = e;pr_debug ("Pushed %p into pos %u\n" , - e, *stackptr -1 ); + e, stackidx -1 ); } e =get_entry (table_base, v);
@@ -423,9 +427,8 @@ ipt_do_table(struct sk_buff *skb,/* Verdict */ break ; }while (!acpar.hotdrop); -pr_debug ("Exiting %s; resetting sp from %u to %u\n" , - __func__, *stackptr, origptr); - *stackptr = origptr; +pr_debug ("Exiting %s; sp at %u\n" , __func__, stackidx); +xt_write_recseq_end (addend);local_bh_enable ();
net/ipv6/netfilter/ip6_tables.c +14 -12
@@ -324,12 +324,13 @@ ip6t_do_table(struct sk_buff *skb,const char *indev, *outdev;const void *table_base;struct ip6t_entry *e, **jumpstack; -unsigned int *stackptr, origptr, cpu; +unsigned int stackidx, cpu;const struct xt_table_info *private;struct xt_action_param acpar;unsigned int addend;/* Initialization */ + stackidx =0 ; indev = state->in ? state->in->name : nulldevname; outdev = state->out ? state->out->name : nulldevname;/* We handle fragments by dealing with the first fragment as
@@ -357,8 +358,15 @@ ip6t_do_table(struct sk_buff *skb, cpu =smp_processor_id (); table_base = private->entries; jumpstack = (struct ip6t_entry **)private->jumpstack[cpu]; - stackptr =per_cpu_ptr (private->stackptr, cpu); - origptr = *stackptr; + +/* Switch to alternate jumpstack if we're being invoked via TEE. +* TEE issues XT_CONTINUE verdict on original skb so we must not +* clobber the jumpstack. +* +* For recursion via REJECT or SYNPROXY the stack will be clobbered +* but it is no problem since absolute verdict is issued by these. +*/ + jumpstack += private->stacksize *__this_cpu_read (nf_skb_duplicated); e =get_entry (table_base, private->hook_entry[hook]);
@@ -406,20 +414,16 @@ ip6t_do_table(struct sk_buff *skb, verdict = (unsigned int )(-v) -1 ;break ; } -if (*stackptr <= origptr) +if (stackidx ==0 ) e =get_entry (table_base, private->underflow[hook]);else - e =ip6t_next_entry (jumpstack[--*stackptr]); + e =ip6t_next_entry (jumpstack[--stackidx]);continue ; }if (table_base + v !=ip6t_next_entry (e) && !(e->ipv6.flags &IP6T_F_GOTO )) { -if (*stackptr >= private->stacksize) { - verdict =NF_DROP ; -break ; - } - jumpstack[(*stackptr)++] = e; + jumpstack[stackidx++] = e; } e =get_entry (table_base, v);
@@ -437,8 +441,6 @@ ip6t_do_table(struct sk_buff *skb,break ; }while (!acpar.hotdrop); - *stackptr = origptr; -xt_write_recseq_end (addend);local_bh_enable ();
net/netfilter/x_tables.c +11 -11
@@ -67,9 +67,6 @@ static const char *const xt_prefix[NFPROTO_NUMPROTO] = { [NFPROTO_IPV6] = "ip6", }; -/* Allow this many total (re)entries. */ -static const unsigned int xt_jumpstack_multiplier =2 ; -/* Registration hooks for targets. */ int xt_register_target (struct xt_target *target) {
@@ -688,8 +685,6 @@ void xt_free_table_info(struct xt_table_info *info)kvfree (info->jumpstack); } -free_percpu (info->stackptr); -kvfree (info); }EXPORT_SYMBOL (xt_free_table_info);
@@ -737,10 +732,6 @@ static int xt_jumpstack_alloc(struct xt_table_info *i)unsigned int size;int cpu; - i->stackptr = alloc_percpu(unsigned int); -if (i->stackptr ==NULL ) -return -ENOMEM ; - size =sizeof (void **) * nr_cpu_ids;if (size >PAGE_SIZE ) i->jumpstack =vzalloc (size);
@@ -753,8 +744,17 @@ static int xt_jumpstack_alloc(struct xt_table_info *i)if (i->stacksize ==0 )return 0 ; - i->stacksize *= xt_jumpstack_multiplier; - size =sizeof (void *) * i->stacksize; +/* Jumpstack needs to be able to record two full callchains, one +* from the first rule set traversal, plus one table reentrancy +* via -j TEE without clobbering the callchain that brought us to +* TEE target. +* +* This is done by allocating two jumpstacks per cpu, on reentry +* the upper half of the stack is used. +* +* see the jumpstack setup in ipt_do_table() for more details. +*/ + size =sizeof (void *) * i->stacksize *2u ;for_each_possible_cpu (cpu) {if (size >PAGE_SIZE ) i->jumpstack[cpu] = vmalloc_node(size,
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 9/18 netfilter: add and use jump label for xt_tee
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Don't bother testing if we need to switch to alternate stack unless TEE target is used.
--- include/linux/netfilter/x_tables.h | 7 +++++++ net/ipv4/netfilter/ip_tables.c | 3 ++- net/ipv6/netfilter/ip6_tables.c | 3 ++- net/netfilter/x_tables.c | 3 +++ net/netfilter/xt_TEE.c | 2 ++ 5 files changed, 16 insertions(+), 2 deletions(-)
include/linux/netfilter/x_tables.h +7 -0
@@ -3,6 +3,7 @@#include <linux/netdevice.h> +#include <linux/static_key.h> #include <uapi/linux/netfilter/x_tables.h> /**
@@ -280,6 +281,12 @@ void xt_free_table_info(struct xt_table_info *info);*/ DECLARE_PER_CPU (seqcount_t, xt_recseq); +/* xt_tee_enabled - true if x_tables needs to handle reentrancy +* +* Enabled if current ip(6)tables ruleset has at least one -j TEE rule. +*/ +extern struct static_key xt_tee_enabled; +/** * xt_write_recseq_begin - start of a write section *
net/ipv4/netfilter/ip_tables.c +2 -1
@@ -340,7 +340,8 @@ ipt_do_table(struct sk_buff *skb,* For recursion via REJECT or SYNPROXY the stack will be clobbered * but it is no problem since absolute verdict is issued by these. */ - jumpstack += private->stacksize *__this_cpu_read (nf_skb_duplicated); +if (static_key_false (&xt_tee_enabled)) + jumpstack += private->stacksize *__this_cpu_read (nf_skb_duplicated); e =get_entry (table_base, private->hook_entry[hook]);
net/ipv6/netfilter/ip6_tables.c +2 -1
@@ -366,7 +366,8 @@ ip6t_do_table(struct sk_buff *skb,* For recursion via REJECT or SYNPROXY the stack will be clobbered * but it is no problem since absolute verdict is issued by these. */ - jumpstack += private->stacksize *__this_cpu_read (nf_skb_duplicated); +if (static_key_false (&xt_tee_enabled)) + jumpstack += private->stacksize *__this_cpu_read (nf_skb_duplicated); e =get_entry (table_base, private->hook_entry[hook]);
net/netfilter/x_tables.c +3 -0
@@ -727,6 +727,9 @@ EXPORT_SYMBOL_GPL(xt_compat_unlock);DEFINE_PER_CPU (seqcount_t, xt_recseq);EXPORT_PER_CPU_SYMBOL_GPL (xt_recseq); +struct static_key xt_tee_enabled__read_mostly ; +EXPORT_SYMBOL_GPL (xt_tee_enabled); +static int xt_jumpstack_alloc (struct xt_table_info *i) {unsigned int size;
net/netfilter/xt_TEE.c +2 -0
@@ -251,6 +251,7 @@ static int tee_tg_check(const struct xt_tgchk_param *par) }else info->priv =NULL ; +static_key_slow_inc (&xt_tee_enabled);return 0 ; }
@@ -262,6 +263,7 @@ static void tee_tg_destroy(const struct xt_tgdtor_param *par)unregister_netdevice_notifier (&info->priv->notifier);kfree (info->priv); } +static_key_slow_dec (&xt_tee_enabled); }static struct xt_target tee_tg_reg[]__read_mostly = {
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 10/18 netfilter: xtables: remove __pure annotation
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
sparse complains:
ip_tables.c:361:27: warning: incorrect type in assignment (different modifiers)
ip_tables.c:361:27: expected struct ipt_entry *[assigned] e
ip_tables.c:361:27: got struct ipt_entry [pure] *
doesn't change generated code.
--- net/ipv4/netfilter/arp_tables.c | 2 +- net/ipv4/netfilter/ip_tables.c | 2 +- net/ipv6/netfilter/ip6_tables.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
net/ipv4/netfilter/arp_tables.c +1 -1
@@ -240,7 +240,7 @@ get_entry(const void *base, unsigned int offset)return (struct arpt_entry *)(base + offset); } -static inline __pure +static inline struct arpt_entry *arpt_next_entry (const struct arpt_entry *entry) {return (void *)entry + entry->next_offset;
net/ipv4/netfilter/ip_tables.c +1 -1
@@ -276,7 +276,7 @@ static void trace_packet(const struct sk_buff *skb, }#endif -static inline __pure +static inline struct ipt_entry *ipt_next_entry (const struct ipt_entry *entry) {return (void *)entry + entry->next_offset;
net/ipv6/netfilter/ip6_tables.c +1 -1
@@ -305,7 +305,7 @@ static void trace_packet(const struct sk_buff *skb, }#endif -static inline __pure struct ip6t_entry * +static inline struct ip6t_entry *ip6t_next_entry (const struct ip6t_entry *entry) {return (void *)entry + entry->next_offset;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 11/18 netfilter: Fix memory leak in nf_register_net_hook
In reply to Pablo Neira AyusoFrom: "Eric W. Biederman" <[email protected]>
In the rare case that when it is a attempted to use a per network device netfilter hook and the network device does not exist the newly allocated structure can leak.
Be a good citizen and free the newly allocated structure in the error handling code.
--- net/netfilter/core.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/core.c +3 -1
@@ -96,8 +96,10 @@ int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg) new->priority = reg->priority; nf_hook_list =find_nf_hook_list (net, reg); -if (!nf_hook_list) +if (!nf_hook_list) { +kfree (new);return -ENOENT ; + }mutex_lock (&nf_hook_mutex);list_for_each_entry (elem, nf_hook_list, list) {
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 12/18 netfilter: nf_queue: fix nf_queue_nf_hook_drop()
In reply to Pablo Neira AyusoThis function reacquires the rtnl_lock() which is already held by nf_unregister_hook().
This can be triggered via: modprobe nf_conntrack_ipv4 && rmmod nf_conntrack_ipv4
[ 720.628746] INFO: task rmmod:3578 blocked for more than 120 seconds. [ 720.628749] Not tainted 4.2.0-rc2+ #113 [ 720.628752] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. [ 720.628754] rmmod D ffff8800ca46fd58 0 3578 3571 0x00000080 [...] [ 720.628783] Call Trace: [ 720.628790] [<ffffffff8152ea0b>] schedule+0x6b/0x90 [ 720.628795] [<ffffffff8152ecb3>] schedule_preempt_disabled+0x13/0x20 [ 720.628799] [<ffffffff8152ff55>] mutex_lock_nested+0x1f5/0x380 [ 720.628803] [<ffffffff81462622>] ? rtnl_lock+0x12/0x20 [ 720.628807] [<ffffffff81462622>] ? rtnl_lock+0x12/0x20 [ 720.628812] [<ffffffff81462622>] rtnl_lock+0x12/0x20 [ 720.628817] [<ffffffff8148ab25>] nf_queue_nf_hook_drop+0x15/0x160 [ 720.628825] [<ffffffff81488d48>] nf_unregister_net_hook+0x168/0x190 [ 720.628831] [<ffffffff81488e24>] nf_unregister_hook+0x64/0x80 [ 720.628837] [<ffffffff81488e60>] nf_unregister_hooks+0x20/0x30
[...]
Moreover, nf_unregister_net_hook() should only destroy the queue for this netns, not for every netns.
--- net/netfilter/core.c | 2 +- net/netfilter/nf_internals.h | 2 +- net/netfilter/nf_queue.c | 12 +++--------- 3 files changed, 5 insertions(+), 11 deletions(-)
net/netfilter/core.c +1 -1
@@ -154,7 +154,7 @@ void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)static_key_slow_dec (&nf_hooks_needed[reg->pf][reg->hooknum]);#endif synchronize_net (); -nf_queue_nf_hook_drop (elem); +nf_queue_nf_hook_drop (net, elem);kfree (elem); }EXPORT_SYMBOL (nf_unregister_net_hook);
net/netfilter/nf_internals.h +1 -1
@@ -19,7 +19,7 @@ unsigned int nf_iterate(struct list_head *head, struct sk_buff *skb,/* nf_queue.c */ int nf_queue (struct sk_buff *skb,struct nf_hook_ops *elem,struct nf_hook_state *state,unsigned int queuenum); -void nf_queue_nf_hook_drop (struct nf_hook_ops *ops); +void nf_queue_nf_hook_drop (struct net *net,struct nf_hook_ops *ops);int __init netfilter_queue_init (void );/* nf_log.c */
net/netfilter/nf_queue.c +3 -9
@@ -105,21 +105,15 @@ bool nf_queue_entry_get_refs(struct nf_queue_entry *entry) }EXPORT_SYMBOL_GPL (nf_queue_entry_get_refs); -void nf_queue_nf_hook_drop (struct nf_hook_ops *ops) +void nf_queue_nf_hook_drop (struct net *net,struct nf_hook_ops *ops) {const struct nf_queue_handler *qh; -struct net *net; -rtnl_lock ();rcu_read_lock (); qh =rcu_dereference (queue_handler); -if (qh) { -for_each_net (net) { - qh->nf_hook_drop (net, ops); - } - } +if (qh) + qh->nf_hook_drop (net, ops);rcu_read_unlock (); -rtnl_unlock (); }/*
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 13/18 netfilter: fix possible removal of wrong hook
In reply to Pablo Neira Ayusonf_unregister_net_hook() uses the nf_hook_ops fields as tuple to look up for the corresponding hook in the list. However, we may have two hooks with exactly the same configuration.
This shouldn't be a problem for nftables since every new chain has an unique priv field set, but this may still cause us problems in the future, so better address this problem now by keeping a reference to the original nf_hook_ops structure to make sure we delete the right hook from nf_unregister_net_hook().
--- net/netfilter/core.c | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-)
net/netfilter/core.c +19 -22
@@ -78,26 +78,27 @@ static struct list_head *find_nf_hook_list(struct net *net,return nf_hook_list; } +struct nf_hook_entry { +const struct nf_hook_ops *orig_ops; +struct nf_hook_ops ops; +}; +int nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) {struct list_head *nf_hook_list; -struct nf_hook_ops *elem, *new; +struct nf_hook_entry *entry; +struct nf_hook_ops *elem; - new =kzalloc (sizeof (*new),GFP_KERNEL ); -if (!new) + entry =kmalloc (sizeof (*entry),GFP_KERNEL ); +if (!entry)return -ENOMEM ; - new->hook = reg->hook; - new->dev = reg->dev; - new->owner = reg->owner; - new->priv = reg->priv; - new->pf = reg->pf; - new->hooknum = reg->hooknum; - new->priority = reg->priority; + entry->orig_ops = reg; + entry->ops = *reg; nf_hook_list =find_nf_hook_list (net, reg);if (!nf_hook_list) { -kfree (new); +kfree (entry);return -ENOENT ; }
@@ -106,7 +107,7 @@ int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)if (reg->priority < elem->priority)break ; } -list_add_rcu (&new->list, elem->list.prev); +list_add_rcu (&entry->ops.list, elem->list.prev);mutex_unlock (&nf_hook_mutex);#ifdef CONFIG_NETFILTER_INGRESS if (reg->pf ==NFPROTO_NETDEV && reg->hooknum ==NF_NETDEV_INGRESS )
@@ -122,6 +123,7 @@ EXPORT_SYMBOL(nf_register_net_hook);void nf_unregister_net_hook (struct net *net,const struct nf_hook_ops *reg) {struct list_head *nf_hook_list; +struct nf_hook_entry *entry;struct nf_hook_ops *elem; nf_hook_list =find_nf_hook_list (net, reg);
@@ -130,14 +132,9 @@ void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)mutex_lock (&nf_hook_mutex);list_for_each_entry (elem, nf_hook_list, list) { -if ((reg->hook == elem->hook) && - (reg->dev == elem->dev) && - (reg->owner == elem->owner) && - (reg->priv == elem->priv) && - (reg->pf == elem->pf) && - (reg->hooknum == elem->hooknum) && - (reg->priority == elem->priority)) { -list_del_rcu (&elem->list); + entry = container_of(elem, struct nf_hook_entry, ops); +if (entry->orig_ops == reg) { +list_del_rcu (&entry->ops.list);break ; } }
@@ -154,8 +151,8 @@ void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)static_key_slow_dec (&nf_hooks_needed[reg->pf][reg->hooknum]);#endif synchronize_net (); -nf_queue_nf_hook_drop (net, elem); -kfree (elem); +nf_queue_nf_hook_drop (net, &entry->ops); +kfree (entry); }EXPORT_SYMBOL (nf_unregister_net_hook);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 14/18 netfilter: rename local nf_hook_list to hook_list
In reply to Pablo Neira Ayuso085db2c04557 ("netfilter: Per network namespace netfilter hooks.") introduced a new nf_hook_list that is global, so let's avoid this overlap.
--- include/linux/netfilter.h | 14 +++++++------- net/netfilter/core.c | 28 ++++++++++++++-------------- 2 files changed, 21 insertions(+), 21 deletions(-)
include/linux/netfilter.h +7 -7
@@ -140,20 +140,20 @@ void nf_unregister_sockopt(struct nf_sockopt_ops *reg);#ifdef HAVE_JUMP_LABEL extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO ][NF_MAX_HOOKS ]; -static inline bool nf_hook_list_active (struct list_head *nf_hook_list, +static inline bool nf_hook_list_active (struct list_head *hook_list,u_int8_t pf,unsigned int hook) {if (__builtin_constant_p (pf) &&__builtin_constant_p (hook))return static_key_false (&nf_hooks_needed[pf][hook]); -return !list_empty (nf_hook_list); +return !list_empty (hook_list); }#else -static inline bool nf_hook_list_active (struct list_head *nf_hook_list, +static inline bool nf_hook_list_active (struct list_head *hook_list,u_int8_t pf,unsigned int hook) { -return !list_empty (nf_hook_list); +return !list_empty (hook_list); }#endif
@@ -175,12 +175,12 @@ static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook, int thresh) {struct net *net =dev_net (indev ? indev : outdev); -struct list_head *nf_hook_list = &net->nf.hooks[pf][hook]; +struct list_head *hook_list = &net->nf.hooks[pf][hook]; -if (nf_hook_list_active (nf_hook_list, pf, hook)) { +if (nf_hook_list_active (hook_list, pf, hook)) {struct nf_hook_state state; -nf_hook_state_init (&state, nf_hook_list, hook, thresh, +nf_hook_state_init (&state, hook_list, hook, thresh, pf, indev, outdev, sk, okfn);return nf_hook_slow (skb, &state); }
net/netfilter/core.c +14 -14
@@ -62,20 +62,20 @@ EXPORT_SYMBOL(nf_hooks_needed);static DEFINE_MUTEX (nf_hook_mutex); -static struct list_head *find_nf_hook_list (struct net *net, +static struct list_head *nf_find_hook_list (struct net *net,const struct nf_hook_ops *reg) { -struct list_head *nf_hook_list =NULL ; +struct list_head *hook_list =NULL ;if (reg->pf !=NFPROTO_NETDEV ) - nf_hook_list = &net->nf.hooks[reg->pf][reg->hooknum]; + hook_list = &net->nf.hooks[reg->pf][reg->hooknum];else if (reg->hooknum ==NF_NETDEV_INGRESS ) {#ifdef CONFIG_NETFILTER_INGRESS if (reg->dev &&dev_net (reg->dev) == net) - nf_hook_list = ®->dev->nf_hooks_ingress; + hook_list = ®->dev->nf_hooks_ingress;#endif } -return nf_hook_list; +return hook_list; }struct nf_hook_entry {
@@ -85,7 +85,7 @@ struct nf_hook_entry {int nf_register_net_hook (struct net *net,const struct nf_hook_ops *reg) { -struct list_head *nf_hook_list; +struct list_head *hook_list;struct nf_hook_entry *entry;struct nf_hook_ops *elem;
@@ -96,14 +96,14 @@ int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg) entry->orig_ops = reg; entry->ops = *reg; - nf_hook_list =find_nf_hook_list (net, reg); -if (!nf_hook_list) { + hook_list =nf_find_hook_list (net, reg); +if (!hook_list) {kfree (entry);return -ENOENT ; }mutex_lock (&nf_hook_mutex); -list_for_each_entry (elem, nf_hook_list, list) { +list_for_each_entry (elem, hook_list, list) {if (reg->priority < elem->priority)break ; }
@@ -122,16 +122,16 @@ EXPORT_SYMBOL(nf_register_net_hook);void nf_unregister_net_hook (struct net *net,const struct nf_hook_ops *reg) { -struct list_head *nf_hook_list; +struct list_head *hook_list;struct nf_hook_entry *entry;struct nf_hook_ops *elem; - nf_hook_list =find_nf_hook_list (net, reg); -if (!nf_hook_list) + hook_list =nf_find_hook_list (net, reg); +if (!hook_list)return ;mutex_lock (&nf_hook_mutex); -list_for_each_entry (elem, nf_hook_list, list) { +list_for_each_entry (elem, hook_list, list) { entry = container_of(elem, struct nf_hook_entry, ops);if (entry->orig_ops == reg) {list_del_rcu (&entry->ops.list);
@@ -139,7 +139,7 @@ void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg) } }mutex_unlock (&nf_hook_mutex); -if (&elem->list == nf_hook_list) { +if (&elem->list == hook_list) {WARN (1 ,"nf_unregister_net_hook: hook not found!\n" );return ; }
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 15/18 netfilter: nf_ct_sctp: minimal multihoming support
In reply to Pablo Neira AyusoFrom: Michal Kubeček <[email protected]>
Currently nf_conntrack_proto_sctp module handles only packets between primary addresses used to establish the connection. Any packets between secondary addresses are classified as invalid so that usual firewall configurations drop them. Allowing HEARTBEAT and HEARTBEAT-ACK chunks to establish a new conntrack would allow traffic between secondary addresses to pass through. A more sophisticated solution based on the addresses advertised in the initial handshake (and possibly also later dynamic address addition and removal) would be much harder to implement. Moreover, in general we cannot assume to always see the initial handshake as it can be routed through a different path.
The patch adds two new conntrack states:
SCTP_CONNTRACK_HEARTBEAT_SENT - a HEARTBEAT chunk seen but not acked SCTP_CONNTRACK_HEARTBEAT_ACKED - a HEARTBEAT acked by HEARTBEAT-ACK
State transition rules:
- HEARTBEAT_SENT responds to usual chunks the same way as NONE (so that the behaviour changes as little as possible)
- HEARTBEAT_ACKED responds to usual chunks the same way as ESTABLISHED does, except the resulting state is HEARTBEAT_ACKED rather than ESTABLISHED
- previously existing states except NONE are preserved when HEARTBEAT or HEARTBEAT-ACK is seen
- NONE (in the initial direction) changes to HEARTBEAT_SENT on HEARTBEAT and to CLOSED on HEARTBEAT-ACK
- HEARTBEAT_SENT changes to HEARTBEAT_ACKED on HEARTBEAT-ACK in the reply direction
- HEARTBEAT_SENT and HEARTBEAT_ACKED are preserved on HEARTBEAT and HEARTBEAT-ACK otherwise
Normally, vtag is set from the INIT chunk for the reply direction and from the INIT-ACK chunk for the originating direction (i.e. each of these defines vtag value for the opposite direction). For secondary conntracks, we can't rely on seeing INIT/INIT-ACK and even if we have seen them, we would need to connect two different conntracks. Therefore simplified logic is applied: vtag of first packet in each direction (HEARTBEAT in the originating and HEARTBEAT-ACK in reply direction) is saved and all following packets in that direction are compared with this saved value. While INIT and INIT-ACK define vtag for the opposite direction, vtags extracted from HEARTBEAT and HEARTBEAT-ACK are always for their direction.
Default timeout values for new states are
HEARTBEAT_SENT: 30 seconds (default hb_interval)
HEARTBEAT_ACKED: 210 seconds (hb_interval * path_max_retry + max_rto)
(We cannot expect to see the shutdown sequence so that, unlike ESTABLISHED, the HEARTBEAT_ACKED timeout shouldn't be too long.)
--- include/uapi/linux/netfilter/nf_conntrack_sctp.h | 2 + include/uapi/linux/netfilter/nfnetlink_cttimeout.h | 2 + net/netfilter/nf_conntrack_proto_sctp.c | 101 +++++++++++++++----- 3 files changed, 81 insertions(+), 24 deletions(-)
include/uapi/linux/netfilter/nf_conntrack_sctp.h +2 -0
@@ -13,6 +13,8 @@ enum sctp_conntrack {SCTP_CONNTRACK_SHUTDOWN_SENT ,SCTP_CONNTRACK_SHUTDOWN_RECD ,SCTP_CONNTRACK_SHUTDOWN_ACK_SENT , +SCTP_CONNTRACK_HEARTBEAT_SENT , +SCTP_CONNTRACK_HEARTBEAT_ACKED , SCTP_CONNTRACK_MAX };
include/uapi/linux/netfilter/nfnetlink_cttimeout.h +2 -0
@@ -92,6 +92,8 @@ enum ctattr_timeout_sctp {CTA_TIMEOUT_SCTP_SHUTDOWN_SENT ,CTA_TIMEOUT_SCTP_SHUTDOWN_RECD ,CTA_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT , +CTA_TIMEOUT_SCTP_HEARTBEAT_SENT , +CTA_TIMEOUT_SCTP_HEARTBEAT_ACKED , __CTA_TIMEOUT_SCTP_MAX };#define CTA_TIMEOUT_SCTP_MAX (__CTA_TIMEOUT_SCTP_MAX - 1)
net/netfilter/nf_conntrack_proto_sctp.c +77 -24
182 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 16/18 netfilter: bridge: reduce nf_bridge_info to 32 bytes again
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
We can use union for most of the temporary cruft (original ipv4/ipv6 address, source mac, physoutdev) since they're used during different stages of br netfilter traversal.
Also get rid of the last two ->mask users.
Shrinks struct from 48 to 32 on 64bit arch.
--- include/linux/netfilter_bridge.h | 12 +++++++++--- include/linux/skbuff.h | 19 +++++++++++++------ net/bridge/br_netfilter_hooks.c | 14 ++++++-------- net/bridge/br_netfilter_ipv6.c | 2 +- net/ipv4/netfilter/nf_defrag_ipv4.c | 7 ++----- net/ipv6/netfilter/nf_defrag_ipv6_hooks.c | 7 ++----- 6 files changed, 33 insertions(+), 28 deletions(-)
include/linux/netfilter_bridge.h +9 -3
@@ -17,9 +17,6 @@ enum nf_br_hook_priorities {#if IS_ENABLED (CONFIG_BRIDGE_NETFILTER ) -#define BRNF_BRIDGED_DNAT 0x02 -#define BRNF_NF_BRIDGE_PREROUTING 0x08 -int br_handle_frame_finish (struct sock *sk,struct sk_buff *skb);static inline void br_drop_fake_rtable (struct sk_buff *skb)
@@ -63,8 +60,17 @@ nf_bridge_get_physoutdev(const struct sk_buff *skb) {return skb->nf_bridge ? skb->nf_bridge->physoutdev :NULL ; } + +static inline bool nf_bridge_in_prerouting (const struct sk_buff *skb) +{ +return skb->nf_bridge && skb->nf_bridge->in_prerouting; +}#else #define br_drop_fake_rtable (skb) do { } while (0) +static inline bool nf_bridge_in_prerouting (const struct sk_buff *skb) +{ +return false; +}#endif /* CONFIG_BRIDGE_NETFILTER */ #endif
include/linux/skbuff.h +13 -6
@@ -173,17 +173,24 @@ struct nf_bridge_info {BRNF_PROTO_8021Q ,BRNF_PROTO_PPPOE } orig_proto:8; -bool pkt_otherhost; + u8 pkt_otherhost:1; + u8 in_prerouting:1; + u8 bridged_dnat:1;__u16 frag_max_size; -unsigned int mask;struct net_device *physindev;union { -struct net_device *physoutdev; -char neigh_header[8 ]; - }; -union { +/* prerouting: detect dnat in orig/reply direction */ __be32 ipv4_daddr;struct in6_addr ipv6_daddr; + +/* after prerouting + nat detected: store original source +* mac since neigh resolution overwrites it, only used while +* skb is out in neigh layer. +*/ +char neigh_header[8 ]; + +/* always valid & non-NULL from FORWARD on, for physdev match */ +struct net_device *physoutdev; }; };#endif
net/bridge/br_netfilter_hooks.c +6 -8
@@ -284,7 +284,7 @@ int br_nf_pre_routing_finish_bridge(struct sock *sk, struct sk_buff *skb) nf_bridge->neigh_header, ETH_HLEN-ETH_ALEN);/* tell br_dev_xmit to continue with forwarding */ - nf_bridge->mask |=BRNF_BRIDGED_DNAT ; + nf_bridge->bridged_dnat =1 ;/* FIXME Need to refragment */ ret = neigh->output (neigh, skb); }
@@ -356,7 +356,7 @@ static int br_nf_pre_routing_finish(struct sock *sk, struct sk_buff *skb) skb->pkt_type =PACKET_OTHERHOST ; nf_bridge->pkt_otherhost = false; } - nf_bridge->mask &= ~BRNF_NF_BRIDGE_PREROUTING ; + nf_bridge->in_prerouting =0 ;if (br_nf_ipv4_daddr_was_changed (skb, nf_bridge)) {if ((err =ip_route_input (skb, iph->daddr, iph->saddr, iph->tos, dev))) {struct in_device *in_dev =__in_dev_get_rcu (dev);
@@ -444,7 +444,7 @@ struct net_device *setup_pre_routing(struct sk_buff *skb) nf_bridge->pkt_otherhost = true; } - nf_bridge->mask |=BRNF_NF_BRIDGE_PREROUTING ; + nf_bridge->in_prerouting =1 ; nf_bridge->physindev = skb->dev; skb->dev =brnf_get_logical_dev (skb, skb->dev);
@@ -850,10 +850,8 @@ static unsigned int ip_sabotage_in(const struct nf_hook_ops *ops, struct sk_buff *skb, const struct nf_hook_state *state) { -if (skb->nf_bridge && - !(skb->nf_bridge->mask &BRNF_NF_BRIDGE_PREROUTING )) { +if (skb->nf_bridge && !skb->nf_bridge->in_prerouting)return NF_STOP ; - }return NF_ACCEPT ; }
@@ -872,7 +870,7 @@ static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)struct nf_bridge_info *nf_bridge =nf_bridge_info_get (skb);skb_pull (skb,ETH_HLEN ); - nf_bridge->mask &= ~BRNF_BRIDGED_DNAT ; + nf_bridge->bridged_dnat =0 ;BUILD_BUG_ON (sizeof (nf_bridge->neigh_header) != (ETH_HLEN -ETH_ALEN ));
@@ -887,7 +885,7 @@ static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)static int br_nf_dev_xmit (struct sk_buff *skb) { -if (skb->nf_bridge && (skb->nf_bridge->mask &BRNF_BRIDGED_DNAT )) { +if (skb->nf_bridge && skb->nf_bridge->bridged_dnat) {br_nf_pre_routing_finish_bridge_slow (skb);return 1 ; }
net/bridge/br_netfilter_ipv6.c +1 -1
@@ -174,7 +174,7 @@ static int br_nf_pre_routing_finish_ipv6(struct sock *sk, struct sk_buff *skb) skb->pkt_type =PACKET_OTHERHOST ; nf_bridge->pkt_otherhost = false; } - nf_bridge->mask &= ~BRNF_NF_BRIDGE_PREROUTING ; + nf_bridge->in_prerouting =0 ;if (br_nf_ipv6_daddr_was_changed (skb, nf_bridge)) {skb_dst_drop (skb); v6ops->route_input (skb);
net/ipv4/netfilter/nf_defrag_ipv4.c +2 -5
@@ -49,12 +49,9 @@ static enum ip_defrag_users nf_ct_defrag_user(unsigned int hooknum,if (skb->nfct) zone =nf_ct_zone ((struct nf_conn *)skb->nfct);#endif - -#if IS_ENABLED (CONFIG_BRIDGE_NETFILTER ) -if (skb->nf_bridge && - skb->nf_bridge->mask &BRNF_NF_BRIDGE_PREROUTING ) +if (nf_bridge_in_prerouting (skb))return IP_DEFRAG_CONNTRACK_BRIDGE_IN + zone; -#endif +if (hooknum ==NF_INET_PRE_ROUTING )return IP_DEFRAG_CONNTRACK_IN + zone;else
net/ipv6/netfilter/nf_defrag_ipv6_hooks.c +2 -5
@@ -39,12 +39,9 @@ static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum,if (skb->nfct) zone =nf_ct_zone ((struct nf_conn *)skb->nfct);#endif - -#if IS_ENABLED (CONFIG_BRIDGE_NETFILTER ) -if (skb->nf_bridge && - skb->nf_bridge->mask &BRNF_NF_BRIDGE_PREROUTING ) +if (nf_bridge_in_prerouting (skb))return IP6_DEFRAG_CONNTRACK_BRIDGE_IN + zone; -#endif +if (hooknum ==NF_INET_PRE_ROUTING )return IP6_DEFRAG_CONNTRACK_IN + zone;else
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 17/18 netfilter: bridge: do not initialize statics to 0 or NULL
In reply to Pablo Neira AyusoFrom: Bernhard Thaler <[email protected]>
Fix checkpatch.pl "ERROR: do not initialise statics to 0 or NULL" for all statics explicitly initialized to 0.
--- net/bridge/br_netfilter_hooks.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
net/bridge/br_netfilter_hooks.c +3 -3
@@ -49,9 +49,9 @@ static struct ctl_table_header *brnf_sysctl_header;static int brnf_call_iptables__read_mostly =1 ;static int brnf_call_ip6tables__read_mostly =1 ;static int brnf_call_arptables__read_mostly =1 ; -static int brnf_filter_vlan_tagged__read_mostly =0 ; -static int brnf_filter_pppoe_tagged__read_mostly =0 ; -static int brnf_pass_vlan_indev__read_mostly =0 ; +static int brnf_filter_vlan_tagged__read_mostly ; +static int brnf_filter_pppoe_tagged__read_mostly ; +static int brnf_pass_vlan_indev__read_mostly ;#else #define brnf_call_iptables 1#define brnf_call_ip6tables 1
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 18/18 netfilter: ip6t_REJECT: Remove debug messages from reject_tg6()
In reply to Pablo Neira AyusoFrom: Subash Abhinov Kasiviswanathan <[email protected]>
Make it similar to reject_tg() in ipt_REJECT.
--- net/ipv6/netfilter/ip6t_REJECT.c | 5 ----- 1 file changed, 5 deletions(-)
net/ipv6/netfilter/ip6t_REJECT.c +0 -5
@@ -35,14 +35,12 @@ MODULE_AUTHOR("Yasuyuki KOZAKAI <[email protected]>");MODULE_DESCRIPTION ("Xtables: packet \"rejection\" target for IPv6" );MODULE_LICENSE ("GPL" ); -static unsigned int reject_tg6 (struct sk_buff *skb,const struct xt_action_param *par) {const struct ip6t_reject_info *reject = par->targinfo;struct net *net =dev_net ((par->in !=NULL ) ? par->in : par->out); -pr_debug ("%s: medium point\n" , __func__);switch (reject->with) {case IP6T_ICMP6_NO_ROUTE :nf_send_unreach6 (net, skb,ICMPV6_NOROUTE , par->hooknum);
@@ -65,9 +63,6 @@ reject_tg6(struct sk_buff *skb, const struct xt_action_param *par)case IP6T_TCP_RESET :nf_send_reset6 (net, skb, par->hooknum);break ; -default : -net_info_ratelimited ("case %u not handled yet\n" , reject->with); -break ; }return NF_DROP ;
Signature · 1 line
1.7.10.4
From: Pablo Neira Ayuso <[email protected]>
Date: Tue, 4 Aug 2015 12:02:30 +0200
Quoted 1 line
The following patchset contains Netfilter updates for net-next, they are:
Applied, thanks Pablo.