On 6/8/23 1:41 PM, Gilad Sever wrote:
When calling bpf_sk_lookup_tcp(), bpf_sk_lookup_udp() or bpf_skc_lookup_tcp() from tc/xdp ingress, VRF socket bindings aren't respoected, i.e. unbound sockets are returned, and bound sockets aren't found.
VRF binding is determined by the sdif argument to sk_lookup(), however when called from tc the IP SKB control block isn't initialized and thus inet{,6}_sdif() always returns 0.
Fix by calculating sdif for the tc/xdp flows by observing the device's l3 enslaved state.
The cg/sk_skb hooking points which are expected to support inet{,6}_sdif() pass sdif=-1 which makes __bpf_skc_lookup() use the existing logic.
Fixes: 6acc9b432e67 ("bpf: Add helper to retrieve socket in BPF") Acked-by: Stanislav Fomichev sdf@google.com Reviewed-by: Shmulik Ladkani shmulik.ladkani@gmail.com Reviewed-by: Eyal Birger eyal.birger@gmail.com Signed-off-by: Gilad Sever gilad9366@gmail.com
Overall this series looks good to go, just small nits which would be great to still address.
v5: Use reverse xmas tree indentation
v4: Move dev_sdif() to include/linux/netdevice.h as suggested by Stanislav Fomichev
v3: Rename bpf_l2_sdif() to dev_sdif() as suggested by Stanislav Fomichev
include/linux/netdevice.h | 9 +++++++ net/core/filter.c | 54 ++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 21 deletions(-)
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index c2f0c6002a84..db1bfca6b8b4 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -5093,6 +5093,15 @@ static inline bool netif_is_l3_slave(const struct net_device *dev) return dev->priv_flags & IFF_L3MDEV_SLAVE; } +static inline int dev_sdif(const struct net_device *dev) +{ +#if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
Why IS_ENABLED? config NET_L3_MASTER_DEV says bool, so #ifdef CONFIG_NET_L3_MASTER_DEV should suffice?
- if (netif_is_l3_slave(dev))
return dev->ifindex;
+#endif
- return 0;
+}
- static inline bool netif_is_bridge_master(const struct net_device *dev) { return dev->priv_flags & IFF_EBRIDGE;
[...]
static const struct bpf_func_proto bpf_tc_sk_lookup_tcp_proto = { @@ -6778,10 +6782,11 @@ BPF_CALL_5(bpf_tc_sk_lookup_udp, struct sk_buff *, skb, { struct net *caller_net = dev_net(skb->dev); int ifindex = skb->dev->ifindex;
- int sdif = dev_sdif(skb->dev);
return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net, ifindex, IPPROTO_UDP, netns_id,
flags);
}flags, sdif);
static const struct bpf_func_proto bpf_tc_sk_lookup_udp_proto = { @@ -6814,11 +6819,13 @@ BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx, struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) { struct net *caller_net = dev_net(ctx->rxq->dev);
- int ifindex = ctx->rxq->dev->ifindex;
- struct net_device *dev = ctx->rxq->dev;
Why not doing this before the struct net *caller_net and also use it there for the dev_net()? Same in other XDP places. It would be nice to also do the same for the tc helpers.
- int ifindex = dev->ifindex;
- int sdif = dev_sdif(dev);
return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net, ifindex, IPPROTO_UDP, netns_id,
flags);
}flags, sdif);
static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = { @@ -6837,11 +6844,13 @@ BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx, struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) { struct net *caller_net = dev_net(ctx->rxq->dev);
- int ifindex = ctx->rxq->dev->ifindex;
- struct net_device *dev = ctx->rxq->dev;
- int ifindex = dev->ifindex;
- int sdif = dev_sdif(dev);
return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net, ifindex, IPPROTO_TCP, netns_id,
flags);
}flags, sdif);
static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {