net: allow unbound socket for packets in VRF when tcp_l3mdev_accept set

The commit 3c82a21f43 ("net: allow binding socket in a VRF when
there's an unbound socket") changed the inet socket lookup to avoid
packets in a VRF from matching an unbound socket. This is to ensure the
necessary isolation between the default and other VRFs for routing and
forwarding. VRF-unaware processes running in the default VRF cannot
access another VRF and have to be run with 'ip vrf exec <vrf>'. This is
to be expected with tcp_l3mdev_accept disabled, but could be reallowed
when this sysctl option is enabled. So instead of directly checking dif
and sdif in inet[6]_match, here call inet_sk_bound_dev_eq(). This
allows a match on unbound socket for non-zero sdif i.e. for packets in
a VRF, if tcp_l3mdev_accept is enabled.

Fixes: 3c82a21f43 ("net: allow binding socket in a VRF when there's an unbound socket")
Signed-off-by: Mike Manning <mvrmanning@gmail.com>
Link: https://lore.kernel.org/netdev/a54c149aed38fded2d3b5fdb1a6c89e36a083b74.camel@lasnet.de/
Reviewed-by: David Ahern <dsahern@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Mike Manning 2022-07-25 19:14:42 +01:00 committed by David S. Miller
parent d7c4c9e075
commit 944fd1aeac
3 changed files with 17 additions and 20 deletions

View File

@ -110,8 +110,6 @@ static inline bool inet6_match(struct net *net, const struct sock *sk,
const __portpair ports, const __portpair ports,
const int dif, const int sdif) const int dif, const int sdif)
{ {
int bound_dev_if;
if (!net_eq(sock_net(sk), net) || if (!net_eq(sock_net(sk), net) ||
sk->sk_family != AF_INET6 || sk->sk_family != AF_INET6 ||
sk->sk_portpair != ports || sk->sk_portpair != ports ||
@ -119,8 +117,9 @@ static inline bool inet6_match(struct net *net, const struct sock *sk,
!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr)) !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
return false; return false;
bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); /* READ_ONCE() paired with WRITE_ONCE() in sock_bindtoindex_locked() */
return bound_dev_if == dif || bound_dev_if == sdif; return inet_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif,
sdif);
} }
#endif /* IS_ENABLED(CONFIG_IPV6) */ #endif /* IS_ENABLED(CONFIG_IPV6) */

View File

@ -175,17 +175,6 @@ static inline void inet_ehash_locks_free(struct inet_hashinfo *hashinfo)
hashinfo->ehash_locks = NULL; hashinfo->ehash_locks = NULL;
} }
static inline bool inet_sk_bound_dev_eq(struct net *net, int bound_dev_if,
int dif, int sdif)
{
#if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
return inet_bound_dev_eq(!!READ_ONCE(net->ipv4.sysctl_tcp_l3mdev_accept),
bound_dev_if, dif, sdif);
#else
return inet_bound_dev_eq(true, bound_dev_if, dif, sdif);
#endif
}
struct inet_bind_bucket * struct inet_bind_bucket *
inet_bind_bucket_create(struct kmem_cache *cachep, struct net *net, inet_bind_bucket_create(struct kmem_cache *cachep, struct net *net,
struct inet_bind_hashbucket *head, struct inet_bind_hashbucket *head,
@ -271,16 +260,14 @@ static inline bool inet_match(struct net *net, const struct sock *sk,
const __addrpair cookie, const __portpair ports, const __addrpair cookie, const __portpair ports,
int dif, int sdif) int dif, int sdif)
{ {
int bound_dev_if;
if (!net_eq(sock_net(sk), net) || if (!net_eq(sock_net(sk), net) ||
sk->sk_portpair != ports || sk->sk_portpair != ports ||
sk->sk_addrpair != cookie) sk->sk_addrpair != cookie)
return false; return false;
/* Paired with WRITE_ONCE() from sock_bindtoindex_locked() */ /* READ_ONCE() paired with WRITE_ONCE() in sock_bindtoindex_locked() */
bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); return inet_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif,
return bound_dev_if == dif || bound_dev_if == sdif; sdif);
} }
/* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we need /* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we need

View File

@ -149,6 +149,17 @@ static inline bool inet_bound_dev_eq(bool l3mdev_accept, int bound_dev_if,
return bound_dev_if == dif || bound_dev_if == sdif; return bound_dev_if == dif || bound_dev_if == sdif;
} }
static inline bool inet_sk_bound_dev_eq(struct net *net, int bound_dev_if,
int dif, int sdif)
{
#if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
return inet_bound_dev_eq(!!READ_ONCE(net->ipv4.sysctl_tcp_l3mdev_accept),
bound_dev_if, dif, sdif);
#else
return inet_bound_dev_eq(true, bound_dev_if, dif, sdif);
#endif
}
struct inet_cork { struct inet_cork {
unsigned int flags; unsigned int flags;
__be32 addr; __be32 addr;