Skip to content

Commit

Permalink
bpf: Extend bpf_skc_to_mptcp_sock to MPTCP sock
Browse files Browse the repository at this point in the history
Currently, bpf_skc_to_mptcp_sock() can only be used with sockets that
are MPTCP subflows: TCP sockets with tp->is_mptcp, created by the kernel
from an MPTCP socket (IPPROTO_MPTCP). Typically used with BPF sock_ops
operators.

Here, this helper is extended to support MPTCP sockets, the ones created
by the userspace (IPPROTO_MPTCP). This is useful for BPF hooks involving
these sockets, e.g. [gs]etsocktopt.

bpf_skc_to_mptcp_sock() uses bpf_mptcp_sock_from_subflow(). The former
suggests any MPTCP type/subtype can be used, but the latter only accepts
subflow ones. So bpf_mptcp_sock_from_subflow is modified here to support
MPTCP socket, and renamed to avoid confusions.

Signed-off-by: Geliang Tang <[email protected]>
Reviewed-by: Matthieu Baerts (NGI0) <[email protected]>
  • Loading branch information
Geliang Tang authored and matttbe committed Feb 24, 2025
1 parent 3965fbe commit fcd9c56
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions include/net/mptcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) { }
#endif

#if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_SYSCALL)
struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk);
struct mptcp_sock *bpf_mptcp_sock_from_sock(struct sock *sk);
#else
static inline struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk) { return NULL; }
static inline struct mptcp_sock *bpf_mptcp_sock_from_sock(struct sock *sk) { return NULL; }
#endif

#if !IS_ENABLED(CONFIG_MPTCP)
Expand Down
2 changes: 1 addition & 1 deletion net/core/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -11884,7 +11884,7 @@ const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
BPF_CALL_1(bpf_skc_to_mptcp_sock, struct sock *, sk)
{
BTF_TYPE_EMIT(struct mptcp_sock);
return (unsigned long)bpf_mptcp_sock_from_subflow(sk);
return (unsigned long)bpf_mptcp_sock_from_sock(sk);
}

const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = {
Expand Down
10 changes: 8 additions & 2 deletions net/mptcp/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
#include <linux/bpf.h>
#include "protocol.h"

struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
struct mptcp_sock *bpf_mptcp_sock_from_sock(struct sock *sk)
{
if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
if (unlikely(!sk || !sk_fullsock(sk)))
return NULL;

if (sk->sk_protocol == IPPROTO_MPTCP)
return mptcp_sk(sk);

if (sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
return mptcp_sk(mptcp_subflow_ctx(sk)->conn);

return NULL;
Expand Down

0 comments on commit fcd9c56

Please sign in to comment.