18039d353SNicolas Rybowski // SPDX-License-Identifier: GPL-2.0
28039d353SNicolas Rybowski /* Copyright (c) 2020, Tessares SA. */
38039d353SNicolas Rybowski /* Copyright (c) 2022, SUSE. */
48039d353SNicolas Rybowski 
58039d353SNicolas Rybowski #include <linux/bpf.h>
68039d353SNicolas Rybowski #include <bpf/bpf_helpers.h>
78039d353SNicolas Rybowski #include "bpf_tcp_helpers.h"
88039d353SNicolas Rybowski 
98039d353SNicolas Rybowski char _license[] SEC("license") = "GPL";
1002662234SGeliang Tang __u32 token = 0;
118039d353SNicolas Rybowski 
128039d353SNicolas Rybowski struct mptcp_storage {
138039d353SNicolas Rybowski 	__u32 invoked;
148039d353SNicolas Rybowski 	__u32 is_mptcp;
15*4f90d034SGeliang Tang 	struct sock *sk;
1602662234SGeliang Tang 	__u32 token;
17*4f90d034SGeliang Tang 	struct sock *first;
18ccc090f4SGeliang Tang 	char ca_name[TCP_CA_NAME_MAX];
198039d353SNicolas Rybowski };
208039d353SNicolas Rybowski 
218039d353SNicolas Rybowski struct {
228039d353SNicolas Rybowski 	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
238039d353SNicolas Rybowski 	__uint(map_flags, BPF_F_NO_PREALLOC);
248039d353SNicolas Rybowski 	__type(key, int);
258039d353SNicolas Rybowski 	__type(value, struct mptcp_storage);
268039d353SNicolas Rybowski } socket_storage_map SEC(".maps");
278039d353SNicolas Rybowski 
288039d353SNicolas Rybowski SEC("sockops")
_sockops(struct bpf_sock_ops * ctx)298039d353SNicolas Rybowski int _sockops(struct bpf_sock_ops *ctx)
308039d353SNicolas Rybowski {
318039d353SNicolas Rybowski 	struct mptcp_storage *storage;
323bc48b56SGeliang Tang 	struct mptcp_sock *msk;
338039d353SNicolas Rybowski 	int op = (int)ctx->op;
348039d353SNicolas Rybowski 	struct tcp_sock *tsk;
358039d353SNicolas Rybowski 	struct bpf_sock *sk;
368039d353SNicolas Rybowski 	bool is_mptcp;
378039d353SNicolas Rybowski 
388039d353SNicolas Rybowski 	if (op != BPF_SOCK_OPS_TCP_CONNECT_CB)
398039d353SNicolas Rybowski 		return 1;
408039d353SNicolas Rybowski 
418039d353SNicolas Rybowski 	sk = ctx->sk;
428039d353SNicolas Rybowski 	if (!sk)
438039d353SNicolas Rybowski 		return 1;
448039d353SNicolas Rybowski 
458039d353SNicolas Rybowski 	tsk = bpf_skc_to_tcp_sock(sk);
468039d353SNicolas Rybowski 	if (!tsk)
478039d353SNicolas Rybowski 		return 1;
488039d353SNicolas Rybowski 
498039d353SNicolas Rybowski 	is_mptcp = bpf_core_field_exists(tsk->is_mptcp) ? tsk->is_mptcp : 0;
503bc48b56SGeliang Tang 	if (!is_mptcp) {
518039d353SNicolas Rybowski 		storage = bpf_sk_storage_get(&socket_storage_map, sk, 0,
528039d353SNicolas Rybowski 					     BPF_SK_STORAGE_GET_F_CREATE);
538039d353SNicolas Rybowski 		if (!storage)
548039d353SNicolas Rybowski 			return 1;
5502662234SGeliang Tang 
5602662234SGeliang Tang 		storage->token = 0;
57ccc090f4SGeliang Tang 		__builtin_memset(storage->ca_name, 0, TCP_CA_NAME_MAX);
58*4f90d034SGeliang Tang 		storage->first = NULL;
593bc48b56SGeliang Tang 	} else {
603bc48b56SGeliang Tang 		msk = bpf_skc_to_mptcp_sock(sk);
613bc48b56SGeliang Tang 		if (!msk)
623bc48b56SGeliang Tang 			return 1;
638039d353SNicolas Rybowski 
643bc48b56SGeliang Tang 		storage = bpf_sk_storage_get(&socket_storage_map, msk, 0,
653bc48b56SGeliang Tang 					     BPF_SK_STORAGE_GET_F_CREATE);
663bc48b56SGeliang Tang 		if (!storage)
673bc48b56SGeliang Tang 			return 1;
6802662234SGeliang Tang 
6902662234SGeliang Tang 		storage->token = msk->token;
70ccc090f4SGeliang Tang 		__builtin_memcpy(storage->ca_name, msk->ca_name, TCP_CA_NAME_MAX);
71*4f90d034SGeliang Tang 		storage->first = msk->first;
723bc48b56SGeliang Tang 	}
738039d353SNicolas Rybowski 	storage->invoked++;
748039d353SNicolas Rybowski 	storage->is_mptcp = is_mptcp;
75*4f90d034SGeliang Tang 	storage->sk = (struct sock *)sk;
768039d353SNicolas Rybowski 
778039d353SNicolas Rybowski 	return 1;
788039d353SNicolas Rybowski }
7902662234SGeliang Tang 
8002662234SGeliang Tang SEC("fentry/mptcp_pm_new_connection")
BPF_PROG(trace_mptcp_pm_new_connection,struct mptcp_sock * msk,const struct sock * ssk,int server_side)8102662234SGeliang Tang int BPF_PROG(trace_mptcp_pm_new_connection, struct mptcp_sock *msk,
8202662234SGeliang Tang 	     const struct sock *ssk, int server_side)
8302662234SGeliang Tang {
8402662234SGeliang Tang 	if (!server_side)
8502662234SGeliang Tang 		token = msk->token;
8602662234SGeliang Tang 
8702662234SGeliang Tang 	return 0;
8802662234SGeliang Tang }
89