From: Lorenz Bauer <lmb@cloudflare.com>
To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com
Cc: netdev@vger.kernel.org, bpf@vger.kernel.org,
kernel-team@cloudflare.com, Lorenz Bauer <lmb@cloudflare.com>
Subject: [PATCH bpf-next 1/7] bpf: sockmap: only check ULP for TCP sockets
Date: Tue, 25 Feb 2020 13:56:30 +0000 [thread overview]
Message-ID: <20200225135636.5768-2-lmb@cloudflare.com> (raw)
In-Reply-To: <20200225135636.5768-1-lmb@cloudflare.com>
The sock map code checks that a socket does not have an active upper
layer protocol before inserting it into the map. This requires casting
via inet_csk, which isn't valid for UDP sockets.
Guard checks for ULP by checking inet_sk(sk)->is_icsk first.
Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
---
include/linux/skmsg.h | 8 +++++++-
net/core/sock_map.c | 11 +++++++----
2 files changed, 14 insertions(+), 5 deletions(-)
diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h
index 112765bd146d..54a9a3e36b29 100644
--- a/include/linux/skmsg.h
+++ b/include/linux/skmsg.h
@@ -360,7 +360,13 @@ static inline void sk_psock_restore_proto(struct sock *sk,
struct sk_psock *psock)
{
sk->sk_prot->unhash = psock->saved_unhash;
- tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
+ if (inet_sk(sk)->is_icsk) {
+ tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
+ } else {
+ sk->sk_write_space = psock->saved_write_space;
+ /* Pairs with lockless read in sk_clone_lock() */
+ WRITE_ONCE(sk->sk_prot, psock->sk_proto);
+ }
}
static inline void sk_psock_set_state(struct sk_psock *psock,
diff --git a/net/core/sock_map.c b/net/core/sock_map.c
index 2e0f465295c3..695ecacc7afa 100644
--- a/net/core/sock_map.c
+++ b/net/core/sock_map.c
@@ -94,6 +94,11 @@ static void sock_map_sk_release(struct sock *sk)
release_sock(sk);
}
+static bool sock_map_sk_has_ulp(struct sock *sk)
+{
+ return inet_sk(sk)->is_icsk && !!inet_csk(sk)->icsk_ulp_ops;
+}
+
static void sock_map_add_link(struct sk_psock *psock,
struct sk_psock_link *link,
struct bpf_map *map, void *link_raw)
@@ -384,7 +389,6 @@ static int sock_map_update_common(struct bpf_map *map, u32 idx,
struct sock *sk, u64 flags)
{
struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
- struct inet_connection_sock *icsk = inet_csk(sk);
struct sk_psock_link *link;
struct sk_psock *psock;
struct sock *osk;
@@ -395,7 +399,7 @@ static int sock_map_update_common(struct bpf_map *map, u32 idx,
return -EINVAL;
if (unlikely(idx >= map->max_entries))
return -E2BIG;
- if (unlikely(rcu_access_pointer(icsk->icsk_ulp_data)))
+ if (sock_map_sk_has_ulp(sk))
return -EINVAL;
link = sk_psock_init_link();
@@ -738,7 +742,6 @@ static int sock_hash_update_common(struct bpf_map *map, void *key,
struct sock *sk, u64 flags)
{
struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
- struct inet_connection_sock *icsk = inet_csk(sk);
u32 key_size = map->key_size, hash;
struct bpf_htab_elem *elem, *elem_new;
struct bpf_htab_bucket *bucket;
@@ -749,7 +752,7 @@ static int sock_hash_update_common(struct bpf_map *map, void *key,
WARN_ON_ONCE(!rcu_read_lock_held());
if (unlikely(flags > BPF_EXIST))
return -EINVAL;
- if (unlikely(icsk->icsk_ulp_data))
+ if (sock_map_sk_has_ulp(sk))
return -EINVAL;
link = sk_psock_init_link();
--
2.20.1
next prev parent reply other threads:[~2020-02-25 13:56 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-25 13:56 [PATCH bpf-next 0/7] bpf: sockmap, sockhash: support storing UDP sockets Lorenz Bauer
2020-02-25 13:56 ` Lorenz Bauer [this message]
2020-02-25 16:45 ` [PATCH bpf-next 1/7] bpf: sockmap: only check ULP for TCP sockets Song Liu
2020-02-25 13:56 ` [PATCH bpf-next 2/7] bpf: sockmap: move generic sockmap hooks from BPF TCP Lorenz Bauer
2020-02-25 17:22 ` Song Liu
2020-02-25 13:56 ` [PATCH bpf-next 3/7] skmsg: introduce sk_psock_hooks Lorenz Bauer
2020-02-26 14:57 ` Jakub Sitnicki
2020-02-26 18:37 ` Martin KaFai Lau
2020-02-28 10:48 ` Lorenz Bauer
2020-02-27 9:27 ` Jakub Sitnicki
2020-02-27 9:40 ` Jakub Sitnicki
2020-02-25 13:56 ` [PATCH bpf-next 4/7] bpf: sockmap: allow UDP sockets Lorenz Bauer
2020-02-26 18:47 ` Martin KaFai Lau
2020-02-25 13:56 ` [PATCH bpf-next 5/7] selftests: bpf: don't listen() on " Lorenz Bauer
2020-02-25 13:56 ` [PATCH bpf-next 6/7] selftests: bpf: add tests for UDP sockets in sockmap Lorenz Bauer
2020-02-27 11:49 ` Jakub Sitnicki
2020-02-27 12:02 ` Lorenz Bauer
2020-02-25 13:56 ` [PATCH bpf-next 7/7] selftests: bpf: enable UDP sockmap reuseport tests Lorenz Bauer
2020-02-26 13:12 ` Jakub Sitnicki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200225135636.5768-2-lmb@cloudflare.com \
--to=lmb@cloudflare.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=john.fastabend@gmail.com \
--cc=kernel-team@cloudflare.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).