net: Add sk_setsockopt() to take the sk ptr instead of the sock ptr
authorMartin KaFai Lau <kafai@fb.com>
Wed, 17 Aug 2022 06:17:11 +0000 (23:17 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 19 Aug 2022 00:06:12 +0000 (17:06 -0700)
A latter patch refactors bpf_setsockopt(SOL_SOCKET) with the
sock_setsockopt() to avoid code duplication and code
drift between the two duplicates.

The current sock_setsockopt() takes sock ptr as the argument.
The very first thing of this function is to get back the sk ptr
by 'sk = sock->sk'.

bpf_setsockopt() could be called when the sk does not have
the sock ptr created.  Meaning sk->sk_socket is NULL.  For example,
when a passive tcp connection has just been established but has yet
been accept()-ed.  Thus, it cannot use the sock_setsockopt(sk->sk_socket)
or else it will pass a NULL ptr.

This patch moves all sock_setsockopt implementation to the newly
added sk_setsockopt().  The new sk_setsockopt() takes a sk ptr
and immediately gets the sock ptr by 'sock = sk->sk_socket'

The existing sock_setsockopt(sock) is changed to call
sk_setsockopt(sock->sk).  All existing callers have both sock->sk
and sk->sk_socket pointer.

The latter patch will make bpf_setsockopt(SOL_SOCKET) call
sk_setsockopt(sk) directly.  The bpf_setsockopt(SOL_SOCKET) does
not use the optnames that require sk->sk_socket, so it will
be safe.

Reviewed-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/r/20220817061711.4175048-1-kafai@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
net/core/sock.c

index 4cb957d..20269c3 100644 (file)
@@ -1041,12 +1041,12 @@ static int sock_reserve_memory(struct sock *sk, int bytes)
  *     at the socket level. Everything here is generic.
  */
 
-int sock_setsockopt(struct socket *sock, int level, int optname,
-                   sockptr_t optval, unsigned int optlen)
+static int sk_setsockopt(struct sock *sk, int level, int optname,
+                        sockptr_t optval, unsigned int optlen)
 {
        struct so_timestamping timestamping;
+       struct socket *sock = sk->sk_socket;
        struct sock_txtime sk_txtime;
-       struct sock *sk = sock->sk;
        int val;
        int valbool;
        struct linger ling;
@@ -1499,6 +1499,13 @@ set_sndbuf:
        release_sock(sk);
        return ret;
 }
+
+int sock_setsockopt(struct socket *sock, int level, int optname,
+                   sockptr_t optval, unsigned int optlen)
+{
+       return sk_setsockopt(sock->sk, level, optname,
+                            optval, optlen);
+}
 EXPORT_SYMBOL(sock_setsockopt);
 
 static const struct cred *sk_get_peer_cred(struct sock *sk)