Skip to content

Commit ae4f2f5

Browse files
q2vendavem330
authored andcommitted
tcp: Restrict SO_TXREHASH to TCP socket.
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 <[email protected]> Reviewed-by: Willem de Bruijn <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 38b95d5 commit ae4f2f5

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

net/core/sock.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,8 @@ int sk_setsockopt(struct sock *sk, int level, int optname,
12761276
return 0;
12771277
}
12781278
case SO_TXREHASH:
1279+
if (!sk_is_tcp(sk))
1280+
return -EOPNOTSUPP;
12791281
if (val < -1 || val > 1)
12801282
return -EINVAL;
12811283
if ((u8)val == SOCK_TXREHASH_DEFAULT)
@@ -2102,6 +2104,9 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
21022104
break;
21032105

21042106
case SO_TXREHASH:
2107+
if (!sk_is_tcp(sk))
2108+
return -EOPNOTSUPP;
2109+
21052110
/* Paired with WRITE_ONCE() in sk_setsockopt() */
21062111
v.val = READ_ONCE(sk->sk_txrehash);
21072112
break;

tools/testing/selftests/bpf/progs/setget_sockopt.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ struct loop_ctx {
8383
struct sock *sk;
8484
};
8585

86+
static bool sk_is_tcp(struct sock *sk)
87+
{
88+
return (sk->__sk_common.skc_family == AF_INET ||
89+
sk->__sk_common.skc_family == AF_INET6) &&
90+
sk->sk_type == SOCK_STREAM &&
91+
sk->sk_protocol == IPPROTO_TCP;
92+
}
93+
8694
static int bpf_test_sockopt_flip(void *ctx, struct sock *sk,
8795
const struct sockopt_test *t,
8896
int level)
@@ -91,6 +99,9 @@ static int bpf_test_sockopt_flip(void *ctx, struct sock *sk,
9199

92100
opt = t->opt;
93101

102+
if (opt == SO_TXREHASH && !sk_is_tcp(sk))
103+
return 0;
104+
94105
if (bpf_getsockopt(ctx, level, opt, &old, sizeof(old)))
95106
return 1;
96107
/* kernel initialized txrehash to 255 */

0 commit comments

Comments
 (0)