tcp: Restrict SO_TXREHASH to TCP socket.
authorKuniyuki Iwashima <kuniyu@amazon.com>
Mon, 19 May 2025 20:57:55 +0000 (13:57 -0700)
committerDavid S. Miller <davem@davemloft.net>
Fri, 23 May 2025 09:24:18 +0000 (10:24 +0100)
sk->sk_txrehash is only used for TCP.

Let's restrict SO_TXREHASH to TCP to reflect this.

Later, we will make sk_txrehash a part of the union for other
protocol families.

Note that we need to modify BPF selftest not to get/set
SO_TEREHASH for non-TCP sockets.

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Reviewed-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/core/sock.c
tools/testing/selftests/bpf/progs/setget_sockopt.c

index 347ce75482f56e3431e23af942b111f3ae861920..d7d6d3a8efe5849fd2f20bd43e1f55281de0a55f 100644 (file)
@@ -1276,6 +1276,8 @@ int sk_setsockopt(struct sock *sk, int level, int optname,
                return 0;
                }
        case SO_TXREHASH:
+               if (!sk_is_tcp(sk))
+                       return -EOPNOTSUPP;
                if (val < -1 || val > 1)
                        return -EINVAL;
                if ((u8)val == SOCK_TXREHASH_DEFAULT)
@@ -2102,6 +2104,9 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
                break;
 
        case SO_TXREHASH:
+               if (!sk_is_tcp(sk))
+                       return -EOPNOTSUPP;
+
                /* Paired with WRITE_ONCE() in sk_setsockopt() */
                v.val = READ_ONCE(sk->sk_txrehash);
                break;
index 0107a24b7522b3b5e8ed215d481571f534ca1f70..d330b15119794d31ab335c7078b8b0f08a8a9857 100644 (file)
@@ -83,6 +83,14 @@ struct loop_ctx {
        struct sock *sk;
 };
 
+static bool sk_is_tcp(struct sock *sk)
+{
+       return (sk->__sk_common.skc_family == AF_INET ||
+               sk->__sk_common.skc_family == AF_INET6) &&
+               sk->sk_type == SOCK_STREAM &&
+               sk->sk_protocol == IPPROTO_TCP;
+}
+
 static int bpf_test_sockopt_flip(void *ctx, struct sock *sk,
                                 const struct sockopt_test *t,
                                 int level)
@@ -91,6 +99,9 @@ static int bpf_test_sockopt_flip(void *ctx, struct sock *sk,
 
        opt = t->opt;
 
+       if (opt == SO_TXREHASH && !sk_is_tcp(sk))
+               return 0;
+
        if (bpf_getsockopt(ctx, level, opt, &old, sizeof(old)))
                return 1;
        /* kernel initialized txrehash to 255 */