xref: /linux/tools/testing/selftests/bpf/progs/mptcp_sockmap.c (revision cbba5d1b53fb82209feacb459edecb1ef8427119)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "bpf_tracing_net.h"
4 
5 char _license[] SEC("license") = "GPL";
6 
7 int sk_index;
8 int redirect_idx;
9 int trace_port;
10 int helper_ret;
11 struct {
12 	__uint(type, BPF_MAP_TYPE_SOCKMAP);
13 	__uint(key_size, sizeof(__u32));
14 	__uint(value_size, sizeof(__u32));
15 	__uint(max_entries, 100);
16 } sock_map SEC(".maps");
17 
18 SEC("sockops")
mptcp_sockmap_inject(struct bpf_sock_ops * skops)19 int mptcp_sockmap_inject(struct bpf_sock_ops *skops)
20 {
21 	struct bpf_sock *sk;
22 
23 	/* only accept specified connection */
24 	if (skops->local_port != trace_port ||
25 	    skops->op != BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB)
26 		return 1;
27 
28 	sk = skops->sk;
29 	if (!sk)
30 		return 1;
31 
32 	/* update sk handler */
33 	helper_ret = bpf_sock_map_update(skops, &sock_map, &sk_index, BPF_NOEXIST);
34 
35 	return 1;
36 }
37 
38 SEC("sk_skb/stream_verdict")
mptcp_sockmap_redirect(struct __sk_buff * skb)39 int mptcp_sockmap_redirect(struct __sk_buff *skb)
40 {
41 	/* redirect skb to the sk under sock_map[redirect_idx] */
42 	return bpf_sk_redirect_map(skb, &sock_map, redirect_idx, 0);
43 }
44