Skip to contentSkip to conversation

bpf: Allow any source IP in bpf_skb_set_tunnel_key

PATCH Superseded Paul Chaignon 4 participants 10 messages Jul 18, 2022 – Jul 19, 2022

Subsystems (7)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover bpf: Allow any source IP in bpf_skb_set_tunnel_key

Commit 26101f5ab6bd ("bpf: Add source ip in "struct bpf_tunnel_key"") added support for getting and setting the outer source IP of encapsulated packets via the bpf_skb_{get,set}_tunnel_key BPF helper. This change allows BPF programs to set any IP address as the source, including for example the IP address of a container running on the same host.

In that last case, however, the encapsulated packets are dropped when looking up the route because the source IP address isn't assigned to any interface on the host. To avoid this, we need to set the FLOWI_FLAG_ANYSRC flag.

Changes in v2:
- Removed changes to IPv6 code paths as they are unnecessary.

Paul Chaignon (5):
ip_tunnels: Add new flow flags field to ip_tunnel_key
vxlan: Use ip_tunnel_key flow flags in route lookups
geneve: Use ip_tunnel_key flow flags in route lookups
bpf: Set flow flag to allow any source IP in bpf_tunnel_key selftests/bpf: Don't assign outer source IP to host

 drivers/net/geneve.c                                 |  1 +
 drivers/net/vxlan/vxlan_core.c                       | 11 +++++++----
 include/net/ip_tunnels.h                             |  1 +
 net/core/filter.c                                    |  1 +
 tools/testing/selftests/bpf/prog_tests/test_tunnel.c |  1 -
 5 files changed, 10 insertions(+), 5 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/5 ip_tunnels: Add new flow flags field to ip_tunnel_key

This commit extends the ip_tunnel_key struct with a new field for the flow flags, to pass them to the route lookups. This new field will be populated and used in subsequent commits.

---
 include/net/ip_tunnels.h | 1 +
 1 file changed, 1 insertion(+)
include/net/ip_tunnels.h +1 -0
index c24fa934221d..20f60d9da741 100644
@@ -54,6 +54,7 @@ struct ip_tunnel_key {
 	__be32			label;		/* Flow Label for IPv6 */
 	__be16			tp_src;
 	__be16			tp_dst;
+	__u8			flow_flags;
 };
 
 /* Flags for ip_tunnel_info mode. */
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 2/5 vxlan: Use ip_tunnel_key flow flags in route lookups

In reply to Paul Chaignon

Use the new ip_tunnel_key field with the flow flags in the IPv4 route lookups for the encapsulated packet. This will be used by the bpf_skb_set_tunnel_key helper in a subsequent commit.

---
 drivers/net/vxlan/vxlan_core.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/net/vxlan/vxlan_core.c +7 -4
index 265d4a0245e7..6991bf7c1cf0 100644
@@ -2243,7 +2243,7 @@ static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device
 				      struct vxlan_sock *sock4,
 				      struct sk_buff *skb, int oif, u8 tos,
 				      __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
-				      struct dst_cache *dst_cache,
+				      __u8 flow_flags, struct dst_cache *dst_cache,
 				      const struct ip_tunnel_info *info)
 {
 	bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
@@ -2270,6 +2270,7 @@ static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device
 	fl4.saddr = *saddr;
 	fl4.fl4_dport = dport;
 	fl4.fl4_sport = sport;
+	fl4.flowi4_flags = flow_flags;
 
 	rt = ip_route_output_key(vxlan->net, &fl4);
 	if (!IS_ERR(rt)) {
@@ -2459,7 +2460,7 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
 	unsigned int pkt_len = skb->len;
 	__be16 src_port = 0, dst_port;
 	struct dst_entry *ndst = NULL;
-	__u8 tos, ttl;
+	__u8 tos, ttl, flow_flags = 0;
 	int ifindex;
 	int err;
 	u32 flags = vxlan->cfg.flags;
@@ -2525,6 +2526,7 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
 		}
 		dst = &remote_ip;
 		dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
+		flow_flags = info->key.flow_flags;
 		vni = tunnel_id_to_key32(info->key.tun_id);
 		ifindex = 0;
 		dst_cache = &info->dst_cache;
@@ -2555,7 +2557,7 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
 		rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
 				     dst->sin.sin_addr.s_addr,
 				     &local_ip.sin.sin_addr.s_addr,
-				     dst_port, src_port,
+				     dst_port, src_port, flow_flags,
 				     dst_cache, info);
 		if (IS_ERR(rt)) {
 			err = PTR_ERR(rt);
@@ -3061,7 +3063,8 @@ static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
 		rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
 				     info->key.u.ipv4.dst,
 				     &info->key.u.ipv4.src, dport, sport,
-				     &info->dst_cache, info);
+				     info->key.flow_flags, &info->dst_cache,
+				     info);
 		if (IS_ERR(rt))
 			return PTR_ERR(rt);
 		ip_rt_put(rt);
Signature · 1 line
2.25.1
#3 lore raw

PATCH v2 3/5 geneve: Use ip_tunnel_key flow flags in route lookups

In reply to Paul Chaignon

Use the new ip_tunnel_key field with the flow flags in the IPv4 route lookups for the encapsulated packet. This will be used by the bpf_skb_set_tunnel_key helper in the subsequent commit.

---
 drivers/net/geneve.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/geneve.c +1 -0
index 2495a5719e1c..018d365f9deb 100644
@@ -815,6 +815,7 @@ static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
 	fl4->saddr = info->key.u.ipv4.src;
 	fl4->fl4_dport = dport;
 	fl4->fl4_sport = sport;
+	fl4->flowi4_flags = info->key.flow_flags;
 
 	tos = info->key.tos;
 	if ((tos == 1) && !geneve->cfg.collect_md) {
Signature · 1 line
2.25.1
#4 lore raw

PATCH v2 4/5 bpf: Set flow flag to allow any source IP in bpf_tunnel_key

In reply to Paul Chaignon

Commit 26101f5ab6bd ("bpf: Add source ip in "struct bpf_tunnel_key"") added support for getting and setting the outer source IP of encapsulated packets via the bpf_skb_{get,set}_tunnel_key BPF helper. This change allows BPF programs to set any IP address as the source, including for example the IP address of a container running on the same host.

In that last case, however, the encapsulated packets are dropped when looking up the route because the source IP address isn't assigned to any interface on the host. To avoid this, we need to set the FLOWI_FLAG_ANYSRC flag.

Fixes 26101f5ab6bd ("bpf: Add source ip in "struct bpf_tunnel_key"")
---
 net/core/filter.c | 1 +
 1 file changed, 1 insertion(+)
net/core/filter.c +1 -0
index 5d16d66727fc..2e3dc9b8e612 100644
@@ -4653,6 +4653,7 @@ BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
 	} else {
 		info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
 		info->key.u.ipv4.src = cpu_to_be32(from->local_ipv4);
+		info->key.flow_flags = FLOWI_FLAG_ANYSRC;
 	}
 
 	return 0;
Signature · 1 line
2.25.1
#5 lore raw

PATCH v2 5/5 selftests/bpf: Don't assign outer source IP to host

In reply to Paul Chaignon

The previous commit fixed a bug in the bpf_skb_set_tunnel_key helper to avoid dropping packets whose outer source IP address isn't assigned to a host interface. This commit changes the corresponding selftest to not assign the outer source IP address to an interface.

With this change and without the bugfix, the ICMP echo packets sent as part of the test are dropped.

---
 tools/testing/selftests/bpf/prog_tests/test_tunnel.c | 1 -
 1 file changed, 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/test_tunnel.c +0 -1
index 3bba4a2a0530..14ccb41a9f59 100644
@@ -111,7 +111,6 @@ static int config_device(void)
 	SYS("ip link add veth0 type veth peer name veth1");
 	SYS("ip link set veth0 netns at_ns0");
 	SYS("ip addr add " IP4_ADDR1_VETH1 "/24 dev veth1");
-	SYS("ip addr add " IP4_ADDR2_VETH1 "/24 dev veth1");
 	SYS("ip link set dev veth1 up mtu 1500");
 	SYS("ip netns exec at_ns0 ip addr add " IP4_ADDR_VETH0 "/24 dev veth0");
 	SYS("ip netns exec at_ns0 ip link set dev veth0 up mtu 1500");
Signature · 1 line
2.25.1
Acked-by on the v2 series
#6 lore raw
In reply to Paul Chaignon
Quoted 10 lines · Paul Chaignon

Commit 26101f5ab6bd ("bpf: Add source ip in "struct bpf_tunnel_key"") added support for getting and setting the outer source IP of encapsulated packets via the bpf_skb_{get,set}_tunnel_key BPF helper. This change allows BPF programs to set any IP address as the source, including for example the IP address of a container running on the same host.

In that last case, however, the encapsulated packets are dropped when looking up the route because the source IP address isn't assigned to any interface on the host. To avoid this, we need to set the FLOWI_FLAG_ANYSRC flag.

Reviewed-by on the v2 series
#7 lore raw
In reply to Paul Chaignon
Quoted 21 lines · Paul Chaignon

Commit 26101f5ab6bd ("bpf: Add source ip in "struct bpf_tunnel_key"") added support for getting and setting the outer source IP of encapsulated packets via the bpf_skb_{get,set}_tunnel_key BPF helper. This change allows BPF programs to set any IP address as the source, including for example the IP address of a container running on the same host.

In that last case, however, the encapsulated packets are dropped when looking up the route because the source IP address isn't assigned to any interface on the host. To avoid this, we need to set the FLOWI_FLAG_ANYSRC flag.

Changes in v2:
- Removed changes to IPv6 code paths as they are unnecessary.

Paul Chaignon (5):
ip_tunnels: Add new flow flags field to ip_tunnel_key
vxlan: Use ip_tunnel_key flow flags in route lookups
geneve: Use ip_tunnel_key flow flags in route lookups
bpf: Set flow flag to allow any source IP in bpf_tunnel_key selftests/bpf: Don't assign outer source IP to host

Quoted 7 lines
 drivers/net/geneve.c                                 |  1 +
 drivers/net/vxlan/vxlan_core.c                       | 11 +++++++----
 include/net/ip_tunnels.h                             |  1 +
 net/core/filter.c                                    |  1 +
 tools/testing/selftests/bpf/prog_tests/test_tunnel.c |  1 -
 5 files changed, 10 insertions(+), 5 deletions(-)

Looks good, for the set:

#8 lore raw
Quoted 31 lines · Nikolay Aleksandrov
Quoted 21 lines · Paul Chaignon

Commit 26101f5ab6bd ("bpf: Add source ip in "struct bpf_tunnel_key"") added support for getting and setting the outer source IP of encapsulated packets via the bpf_skb_{get,set}_tunnel_key BPF helper. This change allows BPF programs to set any IP address as the source, including for example the IP address of a container running on the same host.

In that last case, however, the encapsulated packets are dropped when looking up the route because the source IP address isn't assigned to any interface on the host. To avoid this, we need to set the FLOWI_FLAG_ANYSRC flag.

Changes in v2:
- Removed changes to IPv6 code paths as they are unnecessary.

Paul Chaignon (5):
ip_tunnels: Add new flow flags field to ip_tunnel_key
vxlan: Use ip_tunnel_key flow flags in route lookups
geneve: Use ip_tunnel_key flow flags in route lookups
bpf: Set flow flag to allow any source IP in bpf_tunnel_key selftests/bpf: Don't assign outer source IP to host

Quoted 7 lines
 drivers/net/geneve.c                                 |  1 +
 drivers/net/vxlan/vxlan_core.c                       | 11 +++++++----
 include/net/ip_tunnels.h                             |  1 +
 net/core/filter.c                                    |  1 +
 tools/testing/selftests/bpf/prog_tests/test_tunnel.c |  1 -
 5 files changed, 10 insertions(+), 5 deletions(-)
Quoted 2 lines

Looks good, for the set:

vxlan_test is failing.
See BPF CI link in patchwork.

Also it's too late for 'bpf' tree. Pls tag bpf-next on respin.

#9 lore raw
Quoted 33 lines · Alexei Starovoitov
Quoted 31 lines · Nikolay Aleksandrov
Quoted 28 lines · Paul Chaignon

Commit 26101f5ab6bd ("bpf: Add source ip in "struct bpf_tunnel_key"") added support for getting and setting the outer source IP of encapsulated packets via the bpf_skb_{get,set}_tunnel_key BPF helper. This change allows BPF programs to set any IP address as the source, including for example the IP address of a container running on the same host.

In that last case, however, the encapsulated packets are dropped when looking up the route because the source IP address isn't assigned to any interface on the host. To avoid this, we need to set the FLOWI_FLAG_ANYSRC flag.

Changes in v2:
- Removed changes to IPv6 code paths as they are unnecessary.

Paul Chaignon (5):
ip_tunnels: Add new flow flags field to ip_tunnel_key
vxlan: Use ip_tunnel_key flow flags in route lookups
geneve: Use ip_tunnel_key flow flags in route lookups
bpf: Set flow flag to allow any source IP in bpf_tunnel_key selftests/bpf: Don't assign outer source IP to host

 drivers/net/geneve.c                                 |  1 +
 drivers/net/vxlan/vxlan_core.c                       | 11 +++++++----
 include/net/ip_tunnels.h                             |  1 +
 net/core/filter.c                                    |  1 +
 tools/testing/selftests/bpf/prog_tests/test_tunnel.c |  1 -
 5 files changed, 10 insertions(+), 5 deletions(-)
Quoted 5 lines
Quoted 2 lines

Looks good, for the set:

vxlan_test is failing.
See BPF CI link in patchwork.

I've seen that today and was able to reproduce. It's most likely because the reply packet is dropped by the stack given the destination address is not local. The easiest way to update the test is probably to rewrite the destination address with another BPF program on ingress. Anyway, thanks for the heads up!

I'm also trying to figure out how I got a passing test before, but most likely explanation is that my test changes were overwritten because of the workflow bug [1]

1 - https://github.com/kernel-patches/vmtest/pull/95

Quoted 2 lines

Also it's too late for 'bpf' tree. Pls tag bpf-next on respin.

Will do.