netfilter: flowtable: use dst in this direction when pushing IPIP header
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v2 1/3 netfilter: flowtable: use dst in this direction when pushing IPIP header
When pushing the IPIP header, the route of the other direction is used to calculate the headroom, use the route in this direction. Accessing the other tuple to set the IP source and destination is fine because this tuple does not provide such information to avoid storing redundant information. However, this tuple already provides the dst for this direction, this went unnoticed because this bug affects headroom and iph->frag_off only at this stage.
---
v2: no changes
net/netfilter/nf_flow_table_ip.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
net/netfilter/nf_flow_table_ip.c +11 -7
@@ -590,10 +590,10 @@ static int nf_flow_pppoe_push(struct sk_buff *skb, u16 id,static int nf_flow_tunnel_ipip_push (struct net *net,struct sk_buff *skb,struct flow_offload_tuple *tuple, -__be32 *ip_daddr) +struct dst_entry *dst,__be32 *ip_daddr) {struct iphdr *iph = (struct iphdr *)skb_network_header (skb); -struct rtable *rt =dst_rtable (tuple->dst_cache); +struct rtable *rt =dst_rtable (dst);u8 tos = iph->tos, ttl = iph->ttl;__be16 frag_off = iph->frag_off;u32 headroom =sizeof (*iph);
@@ -636,21 +636,22 @@ static int nf_flow_tunnel_ipip_push(struct net *net, struct sk_buff *skb,static int nf_flow_tunnel_v4_push (struct net *net,struct sk_buff *skb,struct flow_offload_tuple *tuple, -__be32 *ip_daddr) +struct dst_entry *dst,__be32 *ip_daddr) {if (tuple->tun_num) -return nf_flow_tunnel_ipip_push (net, skb, tuple, ip_daddr); +return nf_flow_tunnel_ipip_push (net, skb, tuple, dst, ip_daddr);return 0 ; }static int nf_flow_tunnel_ip6ip6_push (struct net *net,struct sk_buff *skb,struct flow_offload_tuple *tuple, +struct dst_entry *dst,struct in6_addr **ip6_daddr) {struct ipv6hdr *ip6h = (struct ipv6hdr *)skb_network_header (skb); -struct rtable *rt =dst_rtable (tuple->dst_cache);__u8 dsfield =ipv6_get_dsfield (ip6h); +struct rtable *rt =dst_rtable (dst);struct flowi6 fl6 = { .daddr = tuple->tun.src_v6, .saddr = tuple->tun.dst_v6,
@@ -696,10 +697,11 @@ static int nf_flow_tunnel_ip6ip6_push(struct net *net, struct sk_buff *skb,static int nf_flow_tunnel_v6_push (struct net *net,struct sk_buff *skb,struct flow_offload_tuple *tuple, +struct dst_entry *dst,struct in6_addr **ip6_daddr) {if (tuple->tun_num) -return nf_flow_tunnel_ip6ip6_push (net, skb, tuple, ip6_daddr); +return nf_flow_tunnel_ip6ip6_push (net, skb, tuple, dst, ip6_daddr);return 0 ; }
@@ -842,7 +844,8 @@ nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb, other_tuple = &flow->tuplehash[!dir].tuple; ip_daddr = other_tuple->src_v4.s_addr; -if (nf_flow_tunnel_v4_push (state->net, skb, other_tuple, &ip_daddr) <0 ) +if (nf_flow_tunnel_v4_push (state->net, skb, other_tuple, + tuplehash->tuple.dst_cache, &ip_daddr) <0 )return NF_DROP ;switch (tuplehash->tuple.xmit_type) {
@@ -1158,6 +1161,7 @@ nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb, ip6_daddr = &other_tuple->src_v6;if (nf_flow_tunnel_v6_push (state->net, skb, other_tuple, + tuplehash->tuple.dst_cache, &ip6_daddr) <0 )return NF_DROP ;
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 2/3 netfilter: flowtable: IPIP tunnel hardware offload is not yet support
No driver supports for IPIP tunnels yet, give up early on setting up the hardware offload for this scenario.
This patch adds a stub that can be enhanced to add more configuration that are currently not supported. As of now, the offload work is enqueued to the worker, then ignored if the hardware offload configuration is not supported.
Check the NF_FLOW_HW flag to know if this entry was already tried once to be offloaded so this is not retried on refresh when unsupported. Move NF_FLOW_HW flag check to nf_flow_offload_add(). If this NF_FLOW_HW flag is unset the _del and _stats variants are never called.
This can be updated later on to skip hardware offload work to be queued in case hardware offload does not support it.
---
v2: - set NF_FLOW_HW flag from nf_flow_offload_add(), after checking if it is supported.
- call nf_flow_offload_refresh() only if NF_FLOW_HW is set on.
include/net/netfilter/nf_flow_table.h | 2 ++ net/netfilter/nf_flow_table_core.c | 7 +++---- net/netfilter/nf_flow_table_offload.c | 22 ++++++++++++++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-)
include/net/netfilter/nf_flow_table.h +2 -0
@@ -357,6 +357,8 @@ static inline int nf_flow_register_bpf(void)void nf_flow_offload_add (struct nf_flowtable *flowtable,struct flow_offload *flow); +void nf_flow_offload_refresh (struct nf_flowtable *flowtable, +struct flow_offload *flow);void nf_flow_offload_del (struct nf_flowtable *flowtable,struct flow_offload *flow); void nf_flow_offload_stats(struct nf_flowtable *flowtable,
net/netfilter/nf_flow_table_core.c +3 -4
@@ -345,10 +345,8 @@ int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)nf_ct_refresh (flow->ct,NF_CT_DAY ); -if (nf_flowtable_hw_offload (flow_table)) { -__set_bit (NF_FLOW_HW , &flow->flags); +if (nf_flowtable_hw_offload (flow_table))nf_flow_offload_add (flow_table, flow); - }return 0 ; }
@@ -369,7 +367,8 @@ void flow_offload_refresh(struct nf_flowtable *flow_table, test_bit(NF_FLOW_CLOSING, &flow->flags))return ; -nf_flow_offload_add (flow_table, flow); +if (test_bit (NF_FLOW_HW , &flow->flags)) +nf_flow_offload_refresh (flow_table, flow); }EXPORT_SYMBOL_GPL (flow_offload_refresh);
net/netfilter/nf_flow_table_offload.c +20 -2
@@ -1101,9 +1101,17 @@ nf_flow_offload_work_alloc(struct nf_flowtable *flowtable,return offload; } +static bool nf_flow_offload_unsupported (struct flow_offload *flow) +{ +if (flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.tun_num || + flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.tun_num) +return true; -void nf_flow_offload_add (struct nf_flowtable *flowtable, -struct flow_offload *flow) +return false; +} + +void nf_flow_offload_refresh (struct nf_flowtable *flowtable, +struct flow_offload *flow) {struct flow_offload_work *offload;
@@ -1114,6 +1122,16 @@ void nf_flow_offload_add(struct nf_flowtable *flowtable,flow_offload_queue_work (offload); } +void nf_flow_offload_add (struct nf_flowtable *flowtable, +struct flow_offload *flow) +{ +if (nf_flow_offload_unsupported (flow)) +return ; + +__set_bit (NF_FLOW_HW , &flow->flags); +nf_flow_offload_refresh (flowtable, flow); +} +void nf_flow_offload_del (struct nf_flowtable *flowtable,struct flow_offload *flow) {
Signature · 1 line
2.47.3
Re: [PATCH nf,v2 2/3] netfilter: flowtable: IPIP tunnel hardware offload is not yet support
Quoted 16 lines
No driver supports for IPIP tunnels yet, give up early on setting up the hardware offload for this scenario.
This patch adds a stub that can be enhanced to add more configuration that are currently not supported. As of now, the offload work is enqueued to the worker, then ignored if the hardware offload configuration is not supported.
Check the
NF_FLOW_HWflag to know if this entry was already tried once to be offloaded so this is not retried on refresh when unsupported. MoveNF_FLOW_HWflag check tonf_flow_offload_add(). If thisNF_FLOW_HWflag is unset the_deland_statsvariants are never called.This can be updated later on to skip hardware offload work to be queued in case hardware offload does not support it.
Quoted 6 lines
Fixes d98103575dcd ("netfilter: flowtable: Add IP6IP6 rx sw acceleration")Fixes ab427db17885 ("netfilter: flowtable: Add IPIP rx sw acceleration")Reported-by Yuan Tan <[email protected]>Reported-by Xin Liu <[email protected]>Reported-by Zhengyang Chen <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>
Quoted 92 lines
---
v2: - setNF_FLOW_HWflag fromnf_flow_offload_add(), after checking if it is supported.
- callnf_flow_offload_refresh()only ifNF_FLOW_HWis set on.include/net/netfilter/nf_flow_table.h | 2 ++ net/netfilter/nf_flow_table_core.c | 7 +++---- net/netfilter/nf_flow_table_offload.c | 22 ++++++++++++++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-)include/net/netfilter/nf_flow_table.h +2
-0@@ -357,6 +357,8 @@ static inline int nf_flow_register_bpf(void)void nf_flow_offload_add (struct nf_flowtable *flowtable,struct flow_offload *flow); +void nf_flow_offload_refresh (struct nf_flowtable *flowtable, +struct flow_offload *flow);void nf_flow_offload_del (struct nf_flowtable *flowtable,struct flow_offload *flow); void nf_flow_offload_stats(struct nf_flowtable *flowtable,net/netfilter/nf_flow_table_core.c +3
-4@@ -345,10 +345,8 @@ int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)nf_ct_refresh (flow->ct,NF_CT_DAY ); -if (nf_flowtable_hw_offload (flow_table)) { -__set_bit (NF_FLOW_HW , &flow->flags); +if (nf_flowtable_hw_offload (flow_table))nf_flow_offload_add (flow_table, flow); - }return 0 ; }@@ -369,7 +367,8 @@ void flow_offload_refresh(struct nf_flowtable *flow_table, test_bit(NF_FLOW_CLOSING, &flow->flags))return ; -nf_flow_offload_add (flow_table, flow); +if (test_bit (NF_FLOW_HW , &flow->flags)) +nf_flow_offload_refresh (flow_table, flow); }EXPORT_SYMBOL_GPL (flow_offload_refresh);net/netfilter/nf_flow_table_offload.c +20
-2@@ -1101,9 +1101,17 @@ nf_flow_offload_work_alloc(struct nf_flowtable *flowtable,return offload; } +static bool nf_flow_offload_unsupported (struct flow_offload *flow) +{ +if (flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.tun_num || + flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.tun_num) +return true; -void nf_flow_offload_add (struct nf_flowtable *flowtable, -struct flow_offload *flow) +return false; +} + +void nf_flow_offload_refresh (struct nf_flowtable *flowtable, +struct flow_offload *flow) {struct flow_offload_work *offload;@@ -1114,6 +1122,16 @@ void nf_flow_offload_add(struct nf_flowtable *flowtable,flow_offload_queue_work (offload); } +void nf_flow_offload_add (struct nf_flowtable *flowtable, +struct flow_offload *flow) +{ +if (nf_flow_offload_unsupported (flow)) +return ; + +__set_bit (NF_FLOW_HW , &flow->flags); +nf_flow_offload_refresh (flowtable, flow); +} +void nf_flow_offload_del (struct nf_flowtable *flowtable,struct flow_offload *flow) {Signature · 1 line
2.47.3
Florian Westphal
maintainerRe: [PATCH nf,v2 2/3] netfilter: flowtable: IPIP tunnel hardware offload is not yet support
In reply to Pablo Neira AyusoQuoted 2 lines · Pablo Neira Ayuso
No driver supports for IPIP tunnels yet, give up early on setting up the hardware offload for this scenario.
This series triggers many drive-by findings.
No big deal, should be addressed later.
net/netfilter/nf_flow_table_offload.c void nf_flow_offload_add(struct nf_flowtable *flowtable, · line 1131
Quoted 1 line
net/netfilter/nf_flow_table_offload.c +1
-0+__set_bit (NF_FLOW_HW , &flow->flags);
Would you mind if I mangle this to use set_bit() ?
Its a low-hanging fruit, the other findings need
more attention.
Otherwise I can also apply this as-is, its an improvement in either case.
Pablo Neira Ayuso
maintainerRe: [PATCH nf,v2 2/3] netfilter: flowtable: IPIP tunnel hardware offload is not yet support
Quoted 11 lines · Florian Westphal
Quoted 2 lines · Pablo Neira Ayuso
No driver supports for IPIP tunnels yet, give up early on setting up the hardware offload for this scenario.
This series triggers many drive-by findings.
No big deal, should be addressed later.
net/netfilter/nf_flow_table_offload.c void nf_flow_offload_add(struct nf_flowtable *flowtable, · line 1131
Quoted 1 line
net/netfilter/nf_flow_table_offload.c +1
-0+__set_bit (NF_FLOW_HW , &flow->flags);Would you mind if I mangle this to use
set_bit()?
Go ahead.
Quoted 2 lines
Its a low-hanging fruit, the other findings need
more attention.
Yes.
Quoted 2 lines
Otherwise I can also apply this as-is, its an improvement in either case.
I will follow with other comments, they are pointing to relevant issues.
Pablo Neira Ayuso
maintainerPATCH v2 3/3 netfilter: flowtable: support IPIP tunnel with direct xmit
In reply to Pablo Neira AyusoThe combination of IPIP tunnel with direct xmit, eg. bridge device, breaks because no dst_entry is provided to check the skb headroom and to set the iph->frag_off field. This leads to invalid dst usage and can trigger a crash in the tunnel transmit path.
Fix this by moving dst_cache and dst_cookie out of the runtime union so that they can be shared by neighbour, xfrm, and direct tunnel flows. For FLOW_OFFLOAD_XMIT_DIRECT tuples carrying tunnel metadata, preserve route state in these shared fields and release it through the common dst release path.
Since dst_entry is now available to the three supported xmit modes and dst_release() already deals with NULL dst, remove the xmit type check in nft_flow_dst_release(). Moreover, skip the check if the dst entry is NULL in nf_flow_dst_check() which is now the case for the direct xmit case.
Based on patch from Rein Wei <[email protected]>.
---
v2: - reorder dst_cookie and dst_cache to fix a 4-byte hole in struct
- remove unnecessary zeroing on dst_cookie and dst_cache, struct is already zeroed at init Both updates per Lorenzo Bianconi's comment.
include/net/netfilter/nf_flow_table.h | 5 +++-- net/netfilter/nf_flow_table_core.c | 12 ++++++++---- net/netfilter/nf_flow_table_ip.c | 3 +-- 3 files changed, 12 insertions(+), 8 deletions(-)
include/net/netfilter/nf_flow_table.h +3 -2
@@ -155,11 +155,12 @@ struct flow_offload_tuple { tun_num:2 , in_vlan_ingress:2;u16 mtu; +u32 dst_cookie; +struct dst_entry *dst_cache; +union {struct { -struct dst_entry *dst_cache;u32 ifidx; -u32 dst_cookie; };struct {u32 ifidx;
net/netfilter/nf_flow_table_core.c +8 -4
@@ -127,12 +127,18 @@ static int flow_offload_fill_route(struct flow_offload *flow,switch (route->tuple[dir].xmit_type) {case FLOW_OFFLOAD_XMIT_DIRECT : +if (flow_tuple->tun_num) { + flow_tuple->dst_cache = dst; + flow_tuple->dst_cookie = +flow_offload_dst_cookie (flow_tuple); + }memcpy (flow_tuple->out.h_dest, route->tuple[dir].out.h_dest,ETH_ALEN );memcpy (flow_tuple->out.h_source, route->tuple[dir].out.h_source,ETH_ALEN ); flow_tuple->out.ifidx = route->tuple[dir].out.ifindex; -dst_release (dst); +if (!flow_tuple->tun_num) +dst_release (dst);break ;case FLOW_OFFLOAD_XMIT_XFRM :case FLOW_OFFLOAD_XMIT_NEIGH :
@@ -152,9 +158,7 @@ static int flow_offload_fill_route(struct flow_offload *flow,static void nft_flow_dst_release (struct flow_offload *flow,enum flow_offload_tuple_dir dir) { -if (flow->tuplehash[dir].tuple.xmit_type ==FLOW_OFFLOAD_XMIT_NEIGH || - flow->tuplehash[dir].tuple.xmit_type ==FLOW_OFFLOAD_XMIT_XFRM ) -dst_release (flow->tuplehash[dir].tuple.dst_cache); +dst_release (flow->tuplehash[dir].tuple.dst_cache); } void flow_offload_route_init(struct flow_offload *flow,
net/netfilter/nf_flow_table_ip.c +1 -2
@@ -299,8 +299,7 @@ static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)static inline bool nf_flow_dst_check (struct flow_offload_tuple *tuple) { -if (tuple->xmit_type !=FLOW_OFFLOAD_XMIT_NEIGH && - tuple->xmit_type !=FLOW_OFFLOAD_XMIT_XFRM ) +if (!tuple->dst_cache)return true;return dst_check (tuple->dst_cache, tuple->dst_cookie);
Signature · 1 line
2.47.3
Re: [PATCH nf,v2 3/3] netfilter: flowtable: support IPIP tunnel with direct xmit
Quoted 17 lines
The combination of IPIP tunnel with direct xmit, eg. bridge device, breaks because no
dst_entryis provided to check the skb headroom and to set theiph->frag_offfield. This leads to invalid dst usage and can trigger a crash in the tunnel transmit path.Fix this by moving
dst_cacheanddst_cookieout of the runtime union so that they can be shared by neighbour, xfrm, and direct tunnel flows. ForFLOW_OFFLOAD_XMIT_DIRECTtuples carrying tunnel metadata, preserve route state in these shared fields and release it through the common dst release path.Since
dst_entryis now available to the three supported xmit modes anddst_release()already deals with NULL dst, remove the xmit type check innft_flow_dst_release(). Moreover, skip the check if the dst entry is NULL innf_flow_dst_check()which is now the case for the direct xmit case.
Quoted 8 lines
Based on patch from Rein Wei <[email protected]>.
Fixes d30301ba4b07 ("netfilter: flowtable: Add IPIP tx sw acceleration")Reported-by Yuan Tan <[email protected]>Reported-by Xin Liu <[email protected]>Reported-by Zhengyang Chen <[email protected]>Reported-by Ren Wei <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>
Quoted 81 lines
---
v2: - reorderdst_cookieanddst_cacheto fix a 4-byte hole in struct
- remove unnecessary zeroing ondst_cookieanddst_cache, struct is already zeroed at init Both updates per Lorenzo Bianconi's comment.include/net/netfilter/nf_flow_table.h | 5 +++-- net/netfilter/nf_flow_table_core.c | 12 ++++++++---- net/netfilter/nf_flow_table_ip.c | 3 +-- 3 files changed, 12 insertions(+), 8 deletions(-)include/net/netfilter/nf_flow_table.h +3
-2@@ -155,11 +155,12 @@ struct flow_offload_tuple { tun_num:2 , in_vlan_ingress:2;u16 mtu; +u32 dst_cookie; +struct dst_entry *dst_cache; +union {struct { -struct dst_entry *dst_cache;u32 ifidx; -u32 dst_cookie; };struct {u32 ifidx;net/netfilter/nf_flow_table_core.c +8
-4@@ -127,12 +127,18 @@ static int flow_offload_fill_route(struct flow_offload *flow,switch (route->tuple[dir].xmit_type) {case FLOW_OFFLOAD_XMIT_DIRECT : +if (flow_tuple->tun_num) { + flow_tuple->dst_cache = dst; + flow_tuple->dst_cookie = +flow_offload_dst_cookie (flow_tuple); + }memcpy (flow_tuple->out.h_dest, route->tuple[dir].out.h_dest,ETH_ALEN );memcpy (flow_tuple->out.h_source, route->tuple[dir].out.h_source,ETH_ALEN ); flow_tuple->out.ifidx = route->tuple[dir].out.ifindex; -dst_release (dst); +if (!flow_tuple->tun_num) +dst_release (dst);break ;case FLOW_OFFLOAD_XMIT_XFRM :case FLOW_OFFLOAD_XMIT_NEIGH :@@ -152,9 +158,7 @@ static int flow_offload_fill_route(struct flow_offload *flow,static void nft_flow_dst_release (struct flow_offload *flow,enum flow_offload_tuple_dir dir) { -if (flow->tuplehash[dir].tuple.xmit_type ==FLOW_OFFLOAD_XMIT_NEIGH || - flow->tuplehash[dir].tuple.xmit_type ==FLOW_OFFLOAD_XMIT_XFRM ) -dst_release (flow->tuplehash[dir].tuple.dst_cache); +dst_release (flow->tuplehash[dir].tuple.dst_cache); } void flow_offload_route_init(struct flow_offload *flow,net/netfilter/nf_flow_table_ip.c +1
-2@@ -299,8 +299,7 @@ static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)static inline bool nf_flow_dst_check (struct flow_offload_tuple *tuple) { -if (tuple->xmit_type !=FLOW_OFFLOAD_XMIT_NEIGH && - tuple->xmit_type !=FLOW_OFFLOAD_XMIT_XFRM ) +if (!tuple->dst_cache)return true;return dst_check (tuple->dst_cache, tuple->dst_cookie);Signature · 1 line
2.47.3