diff --git a/include/net/netfilter/nf_socket.h b/include/net/netfilter/nf_socket.h
index f9d7bee9bd4e..39047c73b642 100644
--- a/include/net/netfilter/nf_socket.h
+++ b/include/net/netfilter/nf_socket.h
@@ -10,4 +10,24 @@ struct sock *nf_sk_lookup_slow_v4(struct net *net, const struct sk_buff *skb,
struct sock *nf_sk_lookup_slow_v6(struct net *net, const struct sk_buff *skb,
const struct net_device *indev);
+/**
+ * nf_skb_sk - Return the inet socket associated with an skb
+ * @skb: The skb to fetch sk from.
+ * @net: The network namespace the socket must belong to.
+ *
+ * On the output path, skb->sk may be non-INET when the skb is passing
+ * through an IP tunnel.
+ *
+ * Return:
+ * *sk* if it is an INET socket that belongs to @net, %NULL otherwise.
+ */
+static inline struct sock *nf_skb_sk(const struct sk_buff *skb, const struct net *net)
+{
+ struct sock *sk = skb->sk;
+
+ if (sk && net_eq(net, sock_net(sk)) && sk_is_inet(sk))
+ return sk;
+
+ return NULL;
+}
#endifdiff --git a/net/netfilter/nft_socket.c b/net/netfilter/nft_socket.c
index 52d892e04261..ea6c2f3dc3ff 100644
--- a/net/netfilter/nft_socket.c
+++ b/net/netfilter/nft_socket.c
@@ -111,15 +111,17 @@ static void nft_socket_eval(const struct nft_expr *expr,
const struct nft_pktinfo *pkt)
{
const struct nft_socket *priv = nft_expr_priv(expr);
- struct sk_buff *skb = pkt->skb;
- struct sock *sk = skb->sk;
u32 *dest = ®s->data[priv->dreg];
+ struct sk_buff *skb = pkt->skb;
+ bool refcounted = false;
+ struct sock *sk;
- if (sk && !net_eq(nft_net(pkt), sock_net(sk)))
- sk = NULL;
-
- if (!sk)
+ sk = nf_skb_sk(skb, nft_net(pkt));
+ if (!sk) {
sk = nft_socket_do_lookup(pkt);
+ if (sk)
+ refcounted = true;
+ }
if (!sk) {
regs->verdict.code = NFT_BREAK;@@ -159,7 +161,7 @@ static void nft_socket_eval(const struct nft_expr *expr,
}
out_put_sk:
- if (sk != skb->sk)
+ if (refcounted)
sock_gen_put(sk);
}
diff --git a/net/netfilter/xt_socket.c b/net/netfilter/xt_socket.c
index 811e53bee408..cd3fc333ccf1 100644
--- a/net/netfilter/xt_socket.c
+++ b/net/netfilter/xt_socket.c
@@ -49,14 +49,15 @@ static bool
socket_match(const struct sk_buff *skb, struct xt_action_param *par,
const struct xt_socket_mtinfo1 *info)
{
+ struct sock *sk = nf_skb_sk(skb, xt_net(par));
struct sk_buff *pskb = (struct sk_buff *)skb;
- struct sock *sk = skb->sk;
+ bool refcounted = false;
- if (sk && !net_eq(xt_net(par), sock_net(sk)))
- sk = NULL;
-
- if (!sk)
+ if (!sk) {
sk = nf_sk_lookup_slow_v4(xt_net(par), skb, xt_in(par));
+ if (sk)
+ refcounted = true;
+ }
if (sk) {
bool wildcard;@@ -79,7 +80,7 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
transparent && sk_fullsock(sk))
pskb->mark = READ_ONCE(sk->sk_mark);
- if (sk != skb->sk)
+ if (refcounted)
sock_gen_put(sk);
if (wildcard || !transparent)
@@ -110,14 +111,15 @@ static bool
socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
{
const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
+ struct sock *sk = nf_skb_sk(skb, xt_net(par));
struct sk_buff *pskb = (struct sk_buff *)skb;
- struct sock *sk = skb->sk;
+ bool refcounted = false;
- if (sk && !net_eq(xt_net(par), sock_net(sk)))
- sk = NULL;
-
- if (!sk)
+ if (!sk) {
sk = nf_sk_lookup_slow_v6(xt_net(par), skb, xt_in(par));
+ if (sk)
+ refcounted = true;
+ }
if (sk) {
bool wildcard;@@ -140,7 +142,7 @@ socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
transparent && sk_fullsock(sk))
pskb->mark = READ_ONCE(sk->sk_mark);
- if (sk != skb->sk)
+ if (refcounted)
sock_gen_put(sk);
if (wildcard || !transparent)
--
2.47.3