Commit ae4f2f59 authored by Kuniyuki Iwashima's avatar Kuniyuki Iwashima Committed by David S. Miller
Browse files

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: default avatarKuniyuki Iwashima <kuniyu@amazon.com>
Reviewed-by: default avatarWillem de Bruijn <willemb@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 38b95d58
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -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;
+11 −0
Original line number Diff line number Diff line
@@ -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 */