netfilter: remove need for skb_clone in nf_fwd_netdev_egress
Subsystems (2)
Florian Westphal
maintainerPATCH 1/1 netfilter: remove need for skb_clone in nf_fwd_netdev_egress
forward expression currently clones the skb, then forwards the clone and returns NF_DROP to the core.
First patch allows using NF_STOLEN from ingress/netdev hook, 2nd patch adds a new helper that then forwards the original skb.
Florian Westphal
maintainerPATCH 1/1 netfilter: ingress: translate 0 nf_hook_slow retval to -EINPROGRESS
The caller assumes that < 0 means that skb was stolen (or free'd).
All other return values continue skb processing.
nf_hook_slow returns 3 different return value types:
A) a (negative) errno value: the skb was dropped (NF_DROP, e.g. by iptables '-j DROP' rule).
B) 0. The skb was stolen by the hook or queued to userspace.
C) 1. all hooks returned NF_ACCEPT so the caller should invoke the okfn so packet processing can continue.
nft ingress facility currently doesn't have the 'okfn' that the NF_HOOK() macros use; there is no nfqueue support either.
So 1 means that nf_hook_ingress() caller should go on processing the skb.
In order to allow use of NF_STOLEN from ingress we need to translate this to an errno number, else we'd crash because we continue with already-free'd (or about to be free-d) skb.
Random dice roll has chosen EINPROGRESS. The errno value isn't checked, its just important that its less than 0.
--- include/linux/netfilter_ingress.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
include/linux/netfilter_ingress.h +6 -1
@@ -19,6 +19,7 @@ static inline int nf_hook_ingress(struct sk_buff *skb) {struct nf_hook_entry *e =rcu_dereference (skb->dev->nf_hooks_ingress);struct nf_hook_state state; +int ret;/* Must recheck the ingress hook head, in the event it became NULL * after the check in nf_hook_ingress_active evaluated to true.
@@ -29,7 +30,11 @@ static inline int nf_hook_ingress(struct sk_buff *skb)nf_hook_state_init (&state,NF_NETDEV_INGRESS ,NFPROTO_NETDEV , skb->dev,NULL ,NULL ,dev_net (skb->dev),NULL ); -return nf_hook_slow (skb, &state, e); + ret =nf_hook_slow (skb, &state, e); +if (unlikely (ret ==0 )) +return -EINPROGRESS ; + +return ret; }static inline void nf_hook_ingress_init (struct net_device *dev)
Signature · 1 line
2.7.3
Florian Westphal
maintainerPATCH 1/1 netfilter: add and use nf_fwd_netdev_egress
In reply to Florian Westphal... so we can use current skb instead of working with a clone.
--- include/net/netfilter/nf_dup_netdev.h | 1 + net/netfilter/nf_dup_netdev.c | 33 +++++++++++++++++++++++++-------- net/netfilter/nft_fwd_netdev.c | 4 ++-- 3 files changed, 28 insertions(+), 10 deletions(-)
include/net/netfilter/nf_dup_netdev.h +1 -0
@@ -2,5 +2,6 @@#define _NF_DUP_NETDEV_H_void nf_dup_netdev_egress (const struct nft_pktinfo *pkt,int oif); +void nf_fwd_netdev_egress (const struct nft_pktinfo *pkt,int oif);#endif
net/netfilter/nf_dup_netdev.c +25 -8
@@ -14,6 +14,29 @@#include <linux/netfilter/nf_tables.h> #include <net/netfilter/nf_tables.h> +static void nf_do_netdev_egress (struct sk_buff *skb,struct net_device *dev) +{ +if (skb_mac_header_was_set (skb)) +skb_push (skb, skb->mac_len); + + skb->dev = dev; +dev_queue_xmit (skb); +} + +void nf_fwd_netdev_egress (const struct nft_pktinfo *pkt,int oif) +{ +struct net_device *dev; + + dev =dev_get_by_index_rcu (nft_net (pkt), oif); +if (!dev) { +kfree_skb (pkt->skb); +return ; + } + +nf_do_netdev_egress (pkt->skb, dev); +} +EXPORT_SYMBOL_GPL (nf_fwd_netdev_egress); +void nf_dup_netdev_egress (const struct nft_pktinfo *pkt,int oif) {struct net_device *dev;
@@ -24,14 +47,8 @@ void nf_dup_netdev_egress(const struct nft_pktinfo *pkt, int oif)return ; skb =skb_clone (pkt->skb,GFP_ATOMIC ); -if (skb ==NULL ) -return ; - -if (skb_mac_header_was_set (skb)) -skb_push (skb, skb->mac_len); - - skb->dev = dev; -dev_queue_xmit (skb); +if (skb) +nf_do_netdev_egress (skb, dev); }EXPORT_SYMBOL_GPL (nf_dup_netdev_egress);
net/netfilter/nft_fwd_netdev.c +2 -2
@@ -26,8 +26,8 @@ static void nft_fwd_netdev_eval(const struct nft_expr *expr,struct nft_fwd_netdev *priv =nft_expr_priv (expr);int oif = regs->data[priv->sreg_dev]; -nf_dup_netdev_egress (pkt, oif); - regs->verdict.code =NF_DROP ; +nf_fwd_netdev_egress (pkt, oif); + regs->verdict.code =NF_STOLEN ; }static const struct nla_policy nft_fwd_netdev_policy[NFTA_FWD_MAX +1 ] = {
Signature · 1 line
2.7.3
Pablo Neira Ayuso
maintainerRe: [PATCH nf-next] netfilter: ingress: translate 0 nf_hook_slow retval to -EINPROGRESS
In reply to Florian WestphalQuoted 17 lines · Florian Westphal
The caller assumes that < 0 means that skb was stolen (or free'd).
All other return values continue skb processing.
nf_hook_slowreturns 3 different return value types:A) a (negative) errno value: the skb was dropped (
NF_DROP, e.g. by iptables '-j DROP' rule).B) 0. The skb was stolen by the hook or queued to userspace.
C) 1. all hooks returned
NF_ACCEPTso the caller should invoke the okfn so packet processing can continue.nft ingress facility currently doesn't have the 'okfn' that the
NF_HOOK()macros use; there is no nfqueue support either.
Quoted 8 lines
So 1 means that
nf_hook_ingress()caller should go on processing the skb.In order to allow use of
NF_STOLENfrom ingress we need to translate this to an errno number, else we'd crash because we continue with already-free'd (or about to be free-d) skb.Random dice roll has chosen EINPROGRESS. The errno value isn't checked, its just important that its less than 0.
Not really comments to block anything, so take them lightly.
Probably we can just set this to -1, I know this maps to some errno value, but at least from code reading it will be make think that we meant to return EINPROGRESS. If you agree, I can just mangle the patch.
One more question below.
Quoted 18 lines
Signed-off-by Florian Westphal <[email protected]>--- include/linux/netfilter_ingress.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)include/linux/netfilter_ingress.h +1
-0@@ -19,6 +19,7 @@ static inline int nf_hook_ingress(struct sk_buff *skb) {struct nf_hook_entry *e =rcu_dereference (skb->dev->nf_hooks_ingress);struct nf_hook_state state; +int ret;/* Must recheck the ingress hook head, in the event it became NULL * after the check in nf_hook_ingress_active evaluated to true.
include/linux/netfilter_ingress.h static inline int nf_hook_ingress(struct sk_buff *skb) · line 35
Quoted 7 lines
include/linux/netfilter_ingress.h +3
-1@@ -29,7 +30,11 @@ static inline int nf_hook_ingress(struct sk_buff *skb)nf_hook_state_init (&state,NF_NETDEV_INGRESS ,NFPROTO_NETDEV , skb->dev,NULL ,NULL ,dev_net (skb->dev),NULL ); -return nf_hook_slow (skb, &state, e); + ret =nf_hook_slow (skb, &state, e); +if (unlikely (ret ==0 )) +return -EINPROGRESS ;
Do you prefer to keep this branch as unlikely? I understand most people are not using fwd much so far, but we're targeting to enrich ingress so I would expect users will be using this more and more. Anyway, we can revisit this later on.