mirror of
https://github.com/hardkernel/linux.git
synced 2026-03-25 20:10:23 +09:00
bpf: Fix bpf socket lookup from tc/xdp to respect socket VRF bindings
[ Upstream commit9a5cb79762] 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:6acc9b432e("bpf: Add helper to retrieve socket in BPF") Signed-off-by: Gilad Sever <gilad9366@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Shmulik Ladkani <shmulik.ladkani@gmail.com> Reviewed-by: Eyal Birger <eyal.birger@gmail.com> Acked-by: Stanislav Fomichev <sdf@google.com> Cc: David Ahern <dsahern@kernel.org> Link: https://lore.kernel.org/bpf/20230621104211.301902-4-gilad9366@gmail.com Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
a254e029b7
commit
4bc4797017
@@ -5196,6 +5196,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)
|
||||
{
|
||||
#ifdef CONFIG_NET_L3_MASTER_DEV
|
||||
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;
|
||||
|
||||
@@ -6174,12 +6174,11 @@ static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
|
||||
static struct sock *
|
||||
__bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
|
||||
struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
|
||||
u64 flags)
|
||||
u64 flags, int sdif)
|
||||
{
|
||||
struct sock *sk = NULL;
|
||||
struct net *net;
|
||||
u8 family;
|
||||
int sdif;
|
||||
|
||||
if (len == sizeof(tuple->ipv4))
|
||||
family = AF_INET;
|
||||
@@ -6191,10 +6190,12 @@ __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
|
||||
if (unlikely(flags || !((s32)netns_id < 0 || netns_id <= S32_MAX)))
|
||||
goto out;
|
||||
|
||||
if (family == AF_INET)
|
||||
sdif = inet_sdif(skb);
|
||||
else
|
||||
sdif = inet6_sdif(skb);
|
||||
if (sdif < 0) {
|
||||
if (family == AF_INET)
|
||||
sdif = inet_sdif(skb);
|
||||
else
|
||||
sdif = inet6_sdif(skb);
|
||||
}
|
||||
|
||||
if ((s32)netns_id < 0) {
|
||||
net = caller_net;
|
||||
@@ -6214,10 +6215,11 @@ out:
|
||||
static struct sock *
|
||||
__bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
|
||||
struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
|
||||
u64 flags)
|
||||
u64 flags, int sdif)
|
||||
{
|
||||
struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
|
||||
ifindex, proto, netns_id, flags);
|
||||
ifindex, proto, netns_id, flags,
|
||||
sdif);
|
||||
|
||||
if (sk) {
|
||||
struct sock *sk2 = sk_to_full_sk(sk);
|
||||
@@ -6257,7 +6259,7 @@ bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
|
||||
}
|
||||
|
||||
return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
|
||||
netns_id, flags);
|
||||
netns_id, flags, -1);
|
||||
}
|
||||
|
||||
static struct sock *
|
||||
@@ -6349,12 +6351,13 @@ static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
|
||||
BPF_CALL_5(bpf_tc_skc_lookup_tcp, struct sk_buff *, skb,
|
||||
struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
|
||||
{
|
||||
struct net *caller_net = dev_net(skb->dev);
|
||||
int ifindex = skb->dev->ifindex;
|
||||
struct net_device *dev = skb->dev;
|
||||
int ifindex = dev->ifindex, sdif = dev_sdif(dev);
|
||||
struct net *caller_net = dev_net(dev);
|
||||
|
||||
return (unsigned long)__bpf_skc_lookup(skb, tuple, len, caller_net,
|
||||
ifindex, IPPROTO_TCP, netns_id,
|
||||
flags);
|
||||
flags, sdif);
|
||||
}
|
||||
|
||||
static const struct bpf_func_proto bpf_tc_skc_lookup_tcp_proto = {
|
||||
@@ -6372,12 +6375,13 @@ static const struct bpf_func_proto bpf_tc_skc_lookup_tcp_proto = {
|
||||
BPF_CALL_5(bpf_tc_sk_lookup_tcp, struct sk_buff *, skb,
|
||||
struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
|
||||
{
|
||||
struct net *caller_net = dev_net(skb->dev);
|
||||
int ifindex = skb->dev->ifindex;
|
||||
struct net_device *dev = skb->dev;
|
||||
int ifindex = dev->ifindex, sdif = dev_sdif(dev);
|
||||
struct net *caller_net = dev_net(dev);
|
||||
|
||||
return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net,
|
||||
ifindex, IPPROTO_TCP, netns_id,
|
||||
flags);
|
||||
flags, sdif);
|
||||
}
|
||||
|
||||
static const struct bpf_func_proto bpf_tc_sk_lookup_tcp_proto = {
|
||||
@@ -6395,12 +6399,13 @@ static const struct bpf_func_proto bpf_tc_sk_lookup_tcp_proto = {
|
||||
BPF_CALL_5(bpf_tc_sk_lookup_udp, struct sk_buff *, skb,
|
||||
struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
|
||||
{
|
||||
struct net *caller_net = dev_net(skb->dev);
|
||||
int ifindex = skb->dev->ifindex;
|
||||
struct net_device *dev = skb->dev;
|
||||
int ifindex = dev->ifindex, sdif = dev_sdif(dev);
|
||||
struct net *caller_net = dev_net(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 = {
|
||||
@@ -6432,12 +6437,13 @@ static const struct bpf_func_proto bpf_sk_release_proto = {
|
||||
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;
|
||||
int ifindex = dev->ifindex, sdif = dev_sdif(dev);
|
||||
struct net *caller_net = dev_net(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 = {
|
||||
@@ -6455,12 +6461,13 @@ static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
|
||||
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, sdif = dev_sdif(dev);
|
||||
struct net *caller_net = dev_net(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 = {
|
||||
@@ -6478,12 +6485,13 @@ static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
|
||||
BPF_CALL_5(bpf_xdp_sk_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, sdif = dev_sdif(dev);
|
||||
struct net *caller_net = dev_net(dev);
|
||||
|
||||
return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
|
||||
ifindex, IPPROTO_TCP, netns_id,
|
||||
flags);
|
||||
flags, sdif);
|
||||
}
|
||||
|
||||
static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
|
||||
@@ -6503,7 +6511,8 @@ BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
|
||||
{
|
||||
return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
|
||||
sock_net(ctx->sk), 0,
|
||||
IPPROTO_TCP, netns_id, flags);
|
||||
IPPROTO_TCP, netns_id, flags,
|
||||
-1);
|
||||
}
|
||||
|
||||
static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
|
||||
@@ -6522,7 +6531,7 @@ BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
|
||||
{
|
||||
return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
|
||||
sock_net(ctx->sk), 0, IPPROTO_TCP,
|
||||
netns_id, flags);
|
||||
netns_id, flags, -1);
|
||||
}
|
||||
|
||||
static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
|
||||
@@ -6541,7 +6550,7 @@ BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
|
||||
{
|
||||
return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
|
||||
sock_net(ctx->sk), 0, IPPROTO_UDP,
|
||||
netns_id, flags);
|
||||
netns_id, flags, -1);
|
||||
}
|
||||
|
||||
static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
|
||||
|
||||
Reference in New Issue
Block a user