1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Tessares SA. */
3 /* Copyright (c) 2022, SUSE. */
4 
5 #include <linux/bpf.h>
6 #include <bpf/bpf_helpers.h>
7 #include "bpf_tcp_helpers.h"
8 
9 char _license[] SEC("license") = "GPL";
10 __u32 token = 0;
11 
12 struct mptcp_storage {
13 	__u32 invoked;
14 	__u32 is_mptcp;
15 	struct sock *sk;
16 	__u32 token;
17 	struct sock *first;
18 	char ca_name[TCP_CA_NAME_MAX];
19 };
20 
21 struct {
22 	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
23 	__uint(map_flags, BPF_F_NO_PREALLOC);
24 	__type(key, int);
25 	__type(value, struct mptcp_storage);
26 } socket_storage_map SEC(".maps");
27 
28 SEC("sockops")
_sockops(struct bpf_sock_ops * ctx)29 int _sockops(struct bpf_sock_ops *ctx)
30 {
31 	struct mptcp_storage *storage;
32 	struct mptcp_sock *msk;
33 	int op = (int)ctx->op;
34 	struct tcp_sock *tsk;
35 	struct bpf_sock *sk;
36 	bool is_mptcp;
37 
38 	if (op != BPF_SOCK_OPS_TCP_CONNECT_CB)
39 		return 1;
40 
41 	sk = ctx->sk;
42 	if (!sk)
43 		return 1;
44 
45 	tsk = bpf_skc_to_tcp_sock(sk);
46 	if (!tsk)
47 		return 1;
48 
49 	is_mptcp = bpf_core_field_exists(tsk->is_mptcp) ? tsk->is_mptcp : 0;
50 	if (!is_mptcp) {
51 		storage = bpf_sk_storage_get(&socket_storage_map, sk, 0,
52 					     BPF_SK_STORAGE_GET_F_CREATE);
53 		if (!storage)
54 			return 1;
55 
56 		storage->token = 0;
57 		__builtin_memset(storage->ca_name, 0, TCP_CA_NAME_MAX);
58 		storage->first = NULL;
59 	} else {
60 		msk = bpf_skc_to_mptcp_sock(sk);
61 		if (!msk)
62 			return 1;
63 
64 		storage = bpf_sk_storage_get(&socket_storage_map, msk, 0,
65 					     BPF_SK_STORAGE_GET_F_CREATE);
66 		if (!storage)
67 			return 1;
68 
69 		storage->token = msk->token;
70 		__builtin_memcpy(storage->ca_name, msk->ca_name, TCP_CA_NAME_MAX);
71 		storage->first = msk->first;
72 	}
73 	storage->invoked++;
74 	storage->is_mptcp = is_mptcp;
75 	storage->sk = (struct sock *)sk;
76 
77 	return 1;
78 }
79 
80 SEC("fentry/mptcp_pm_new_connection")
BPF_PROG(trace_mptcp_pm_new_connection,struct mptcp_sock * msk,const struct sock * ssk,int server_side)81 int BPF_PROG(trace_mptcp_pm_new_connection, struct mptcp_sock *msk,
82 	     const struct sock *ssk, int server_side)
83 {
84 	if (!server_side)
85 		token = msk->token;
86 
87 	return 0;
88 }
89