Skip to contentSkip to conversation

netfilter: flowtable: remove redundant ifidx field in tuple output union

PATCH New Pablo Neira Ayuso 1 participant 3 messages Nov 22

Subsystems (3)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v9 1/3 netfilter: flowtable: remove redundant ifidx field in tuple output union

The XMIT_NEIGH and XMIT_DIRECT types use an output ifidx field to determine the destination netdevice. Remove the redundant ifidx field in the flow_offload_tuple union that represents the output path, use the existing struct.

No functional change, this is just a preparation patch.

---
v9: no changes

 include/net/netfilter/nf_flow_table.h | 3 ---
 net/netfilter/nf_flow_table_core.c    | 2 +-
 net/netfilter/nf_flow_table_ip.c      | 4 ++--
 3 files changed, 3 insertions(+), 6 deletions(-)
include/net/netfilter/nf_flow_table.h +0 -3
index 5b22d31cb140..61ac43b17dc9 100644
@@ -160,9 +160,6 @@ struct flow_offload_tuple {
 	struct dst_entry		*dst_cache;
 
 	union {
-		struct {
-			u32		ifidx;
-		};
 		struct {
 			u32		ifidx;
 			u8		h_source[ETH_ALEN];
net/netfilter/nf_flow_table_core.c +1 -1
index 2aa2647ed9df..a7b4fc5766a9 100644
@@ -142,7 +142,7 @@ static int flow_offload_fill_route(struct flow_offload *flow,
 		break;
 	case FLOW_OFFLOAD_XMIT_XFRM:
 	case FLOW_OFFLOAD_XMIT_NEIGH:
-		flow_tuple->ifidx = route->tuple[dir].out.ifindex;
+		flow_tuple->out.ifidx = route->tuple[dir].out.ifindex;
 		flow_tuple->dst_cache = dst;
 		flow_tuple->dst_cookie = flow_offload_dst_cookie(flow_tuple);
 		break;
net/netfilter/nf_flow_table_ip.c +2 -2
index 19cafae748ab..115c54ad4633 100644
@@ -867,7 +867,7 @@ static int nf_flow_queue_xmit4(struct sk_buff *skb,
 	switch (tuplehash->tuple.xmit_type) {
 	case FLOW_OFFLOAD_XMIT_NEIGH:
 		rt = dst_rtable(tuplehash->tuple.dst_cache);
-		xmit.outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.ifidx);
+		xmit.outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.out.ifidx);
 		if (!xmit.outdev) {
 			flow_offload_teardown(flow);
 			return NF_DROP;
@@ -1196,7 +1196,7 @@ static int nf_flow_queue_xmit6(struct sk_buff *skb,
 	switch (tuplehash->tuple.xmit_type) {
 	case FLOW_OFFLOAD_XMIT_NEIGH:
 		rt = dst_rt6_info(tuplehash->tuple.dst_cache);
-		xmit.outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.ifidx);
+		xmit.outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.out.ifidx);
 		if (!xmit.outdev) {
 			flow_offload_teardown(flow);
 			return NF_DROP;
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH v9 2/3 netfilter: flowtable: cache destination hw address for XMIT_NEIGH type

Since commit b5964aac51e0 ("netfilter: flowtable: consolidate xmit path"), a flow offloaded with FLOW_OFFLOAD_XMIT_NEIGH keeps being forwarded with destination MAC 00:00:00:00:00:00 after the next hop's neighbour entry has been removed. Nothing resolves the next hop again, so the flow stays black-holed for as long as it keeps sending.

Cache the destination hw address in the flow entry, just like XMIT_DIRECT does. Use the flowtable GC to validate that there is a neighbour entry that is still valid. Otherwise, if the neighbour entry is stale, trigger a probe.

To detect non-Ethernet devices check for all-zero destination hw address, then:

- Skip check the stale neighbour check for such as WireGuard.
- Do not set xmit.dest in the flowtable transmit path.

When setting up the fastpath, reject all-zero destination hw address for Ethernet devices.

Fixes b5964aac51e0 ("netfilter: flowtable: consolidate xmit path")

---
v9: - reject !NUD_VALID in ipip and ip6_tnl.
- disallow Ethernet device with all-zero hw address when setting up fastpath.

 net/ipv4/ipip.c                    | 20 +++++++++
 net/ipv6/ip6_tunnel.c              | 43 ++++++++++++++-----
 net/netfilter/nf_flow_table_core.c | 67 ++++++++++++++++++++++++++++--
 net/netfilter/nf_flow_table_ip.c   | 45 ++++++--------------
 net/netfilter/nf_flow_table_path.c | 17 +++++++-
 5 files changed, 144 insertions(+), 48 deletions(-)
net/ipv4/ipip.c +20 -0
index 15189e1fa068..c43d3eab3aca 100644
@@ -358,6 +358,8 @@ static int ipip_fill_forward_path(struct net_device_path_ctx *ctx,
 {
 	struct ip_tunnel *tunnel = netdev_priv(ctx->dev);
 	const struct iphdr *tiph = &tunnel->parms.iph;
+	unsigned char ha[ETH_ALEN];
+	struct neighbour *n;
 	struct rtable *rt;
 
 	if (ctx->ether_type != cpu_to_be16(ETH_P_IP))
@@ -375,6 +377,24 @@ static int ipip_fill_forward_path(struct net_device_path_ctx *ctx,
 	if (IS_ERR(rt))
 		return PTR_ERR(rt);
 
+	n = dst_neigh_lookup(&rt->dst, &tiph->daddr);
+	if (!n) {
+		ip_rt_put(rt);
+		return -ENOENT;
+	}
+
+	if (!(READ_ONCE(n->nud_state) & NUD_VALID)) {
+		neigh_release(n);
+		ip_rt_put(rt);
+		return -ENOENT;
+	}
+
+	if (n->dev->addr_len == ETH_ALEN) {
+		neigh_ha_snapshot(ha, n, n->dev);
+		memcpy(ctx->daddr, ha, ETH_ALEN);
+	}
+	neigh_release(n);
+
 	path->type = DEV_PATH_TUN;
 	path->tun.src_v4.s_addr = tiph->saddr;
 	path->tun.dst_v4.s_addr = tiph->daddr;
net/ipv6/ip6_tunnel.c +32 -11

59 lines not on this page · raw message

net/netfilter/nf_flow_table_core.c +64 -3

92 lines not on this page · raw message

net/netfilter/nf_flow_table_ip.c +13 -32

119 lines not on this page · raw message

net/netfilter/nf_flow_table_path.c +15 -2
index 841f099434d9..7a0884790eaa 100644
@@ -57,6 +57,7 @@ static int nft_dev_fill_forward_path(const struct dst_entry *dst_cache,
 	};
 	struct neighbour *n;
 	u8 nud_state;
+	int ret;
 
 	if (!nft_is_valid_ether_device(dev)) {
 		eth_zero_addr(ha);
@@ -79,7 +80,14 @@ static int nft_dev_fill_forward_path(const struct dst_entry *dst_cache,
 out:
 	ether_addr_copy(ctx.daddr, ha);
 
-	return dev_fill_forward_path(&ctx, stack);
+	ret = dev_fill_forward_path(&ctx, stack);
+	if (ret < 0)
+		return -1;
+
+	if (memcmp(ctx.daddr, ha, ETH_ALEN))
+		ether_addr_copy(ha, ctx.daddr);
+
+	return ret;
 }
 
 struct nft_forward_info {
@@ -238,6 +246,10 @@ static int nft_dev_forward_path(const struct nft_pktinfo *pkt,
 	    nft_dev_path_info(&stack, &info, ha, ft) < 0)
 		return -ENOENT;
 
+	if (info.dev->addr_len == ETH_ALEN &&
+	    is_zero_ether_addr(info.h_dest))
+		return -EINVAL;
+
 	route->tuple[!dir].in.ifindex = info.dev->ifindex;
 	route->tuple[dir].out.ifindex = info.dev->ifindex;
 
@@ -259,9 +271,10 @@ static int nft_dev_forward_path(const struct nft_pktinfo *pkt,
 	route->tuple[!dir].in.num_encaps = info.num_encaps;
 	route->tuple[!dir].in.ingress_vlans = info.ingress_vlans;
 
+	memcpy(route->tuple[dir].out.h_dest, info.h_dest, ETH_ALEN);
+
 	if (info.xmit_type == FLOW_OFFLOAD_XMIT_DIRECT) {
 		memcpy(route->tuple[dir].out.h_source, info.h_source, ETH_ALEN);
-		memcpy(route->tuple[dir].out.h_dest, info.h_dest, ETH_ALEN);
 		route->tuple[dir].xmit_type = info.xmit_type;
 	}
 	route->tuple[dir].out.needs_gso_segment = info.needs_gso_segment;
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH v9 3/3 netfilter: flowtable: unify direct and neigh xmit path

In reply to Pablo Neira Ayuso

Since commit ("netfilter: flowtable: cache destination hw address for XMIT_NEIGH type"), it is possible to possible to consolidate xmit path further since the caching of the neighbour entry. To make it happen, do not attach the cached dst to the skbuff, let's just transmit it via dev_queue_xmit() without it.

---
v9: no changes

 net/netfilter/nf_flow_table_ip.c | 72 ++++++++++----------------------
 1 file changed, 22 insertions(+), 50 deletions(-)
net/netfilter/nf_flow_table_ip.c +22 -50
index da15783a36a4..1948fcd30201 100644
@@ -854,33 +854,19 @@ static int nf_flow_queue_xmit4(struct sk_buff *skb,
 				tuplehash->tuple.dst_cache) < 0)
 		return NF_DROP;
 
-	switch (tuplehash->tuple.xmit_type) {
-	case FLOW_OFFLOAD_XMIT_NEIGH:
-		rt = dst_rtable(tuplehash->tuple.dst_cache);
-		xmit.outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.out.ifidx);
-		if (!xmit.outdev) {
-			flow_offload_teardown(flow);
-			return NF_DROP;
-		}
-		if (!is_zero_ether_addr(tuplehash->tuple.out.h_dest))
-			xmit.dest = tuplehash->tuple.out.h_dest;
-
-		skb_dst_drop(skb);
-		skb_dst_set_noref(skb, &rt->dst);
-		break;
-	case FLOW_OFFLOAD_XMIT_DIRECT:
-		xmit.outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.out.ifidx);
-		if (!xmit.outdev) {
-			flow_offload_teardown(flow);
-			return NF_DROP;
-		}
-		xmit.dest = tuplehash->tuple.out.h_dest;
-		xmit.source = tuplehash->tuple.out.h_source;
-		break;
-	default:
-		WARN_ON_ONCE(1);
+	xmit.outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.out.ifidx);
+	if (!xmit.outdev) {
+		flow_offload_teardown(flow);
 		return NF_DROP;
 	}
+	if (!is_zero_ether_addr(tuplehash->tuple.out.h_dest))
+		xmit.dest = tuplehash->tuple.out.h_dest;
+
+	if (tuplehash->tuple.xmit_type == FLOW_OFFLOAD_XMIT_DIRECT)
+		xmit.source = tuplehash->tuple.out.h_source;
+
+	skb_dst_drop(skb);
+
 	xmit.tuple = other_tuple;
 	xmit.needs_gso_segment = tuplehash->tuple.needs_gso_segment;
 
@@ -1177,33 +1163,19 @@ static int nf_flow_queue_xmit6(struct sk_buff *skb,
 				tuplehash->tuple.dst_cache) < 0)
 		return NF_DROP;
 
-	switch (tuplehash->tuple.xmit_type) {
-	case FLOW_OFFLOAD_XMIT_NEIGH:
-		rt = dst_rt6_info(tuplehash->tuple.dst_cache);
-		xmit.outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.out.ifidx);
-		if (!xmit.outdev) {
-			flow_offload_teardown(flow);
-			return NF_DROP;
-		}
-		if (!is_zero_ether_addr(tuplehash->tuple.out.h_dest))
-			xmit.dest = tuplehash->tuple.out.h_dest;
-
-		skb_dst_drop(skb);
-		skb_dst_set_noref(skb, &rt->dst);
-		break;
-	case FLOW_OFFLOAD_XMIT_DIRECT:
-		xmit.outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.out.ifidx);
-		if (!xmit.outdev) {
-			flow_offload_teardown(flow);
-			return NF_DROP;
-		}
-		xmit.dest = tuplehash->tuple.out.h_dest;
-		xmit.source = tuplehash->tuple.out.h_source;
-		break;
-	default:
-		WARN_ON_ONCE(1);
+	xmit.outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.out.ifidx);
+	if (!xmit.outdev) {
+		flow_offload_teardown(flow);
 		return NF_DROP;
 	}
+	if (!is_zero_ether_addr(tuplehash->tuple.out.h_dest))
+		xmit.dest = tuplehash->tuple.out.h_dest;
+
+	if (tuplehash->tuple.xmit_type == FLOW_OFFLOAD_XMIT_DIRECT)
+		xmit.source = tuplehash->tuple.out.h_source;
+
+	skb_dst_drop(skb);
+
 	xmit.tuple = other_tuple;
 	xmit.needs_gso_segment = tuplehash->tuple.needs_gso_segment;
 
Signature · 1 line
2.47.3