Skip to content

Commit ad8c43f

Browse files
Geliang Tangintel-lab-lkp
authored andcommitted
bpf: Extend bpf_skc_to_mptcp_sock to MPTCP sock
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]>
1 parent 7033e7f commit ad8c43f

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

include/net/mptcp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,9 @@ static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) { }
322322
#endif
323323

324324
#if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_SYSCALL)
325-
struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk);
325+
struct mptcp_sock *bpf_mptcp_sock_from_sock(struct sock *sk);
326326
#else
327-
static inline struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk) { return NULL; }
327+
static inline struct mptcp_sock *bpf_mptcp_sock_from_sock(struct sock *sk) { return NULL; }
328328
#endif
329329

330330
#if !IS_ENABLED(CONFIG_MPTCP)

net/core/filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11836,7 +11836,7 @@ const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
1183611836
BPF_CALL_1(bpf_skc_to_mptcp_sock, struct sock *, sk)
1183711837
{
1183811838
BTF_TYPE_EMIT(struct mptcp_sock);
11839-
return (unsigned long)bpf_mptcp_sock_from_subflow(sk);
11839+
return (unsigned long)bpf_mptcp_sock_from_sock(sk);
1184011840
}
1184111841

1184211842
const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = {

net/mptcp/bpf.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,15 @@ static struct bpf_struct_ops bpf_mptcp_sched_ops = {
188188
};
189189
#endif /* CONFIG_BPF_JIT */
190190

191-
struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
191+
struct mptcp_sock *bpf_mptcp_sock_from_sock(struct sock *sk)
192192
{
193-
if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
193+
if (unlikely(!sk || !sk_fullsock(sk)))
194+
return NULL;
195+
196+
if (sk->sk_protocol == IPPROTO_MPTCP)
197+
return mptcp_sk(sk);
198+
199+
if (sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
194200
return mptcp_sk(mptcp_subflow_ctx(sk)->conn);
195201

196202
return NULL;

0 commit comments

Comments
 (0)