xref: /linux/tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c (revision 55d0969c451159cff86949b38c39171cab962069)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "bpf_tracing_net.h"
3 #include <bpf/bpf_helpers.h>
4 #include <bpf/bpf_endian.h>
5 #include "test_tcpbpf.h"
6 
7 struct tcpbpf_globals global = {};
8 
9 /**
10  * SOL_TCP is defined in <netinet/tcp.h> while
11  * TCP_SAVED_SYN is defined in already included <linux/tcp.h>
12  */
13 #ifndef SOL_TCP
14 #define SOL_TCP 6
15 #endif
16 
17 static __always_inline int get_tp_window_clamp(struct bpf_sock_ops *skops)
18 {
19 	struct bpf_sock *sk;
20 	struct tcp_sock *tp;
21 
22 	sk = skops->sk;
23 	if (!sk)
24 		return -1;
25 	tp = bpf_skc_to_tcp_sock(sk);
26 	if (!tp)
27 		return -1;
28 	return tp->window_clamp;
29 }
30 
31 SEC("sockops")
32 int bpf_testcb(struct bpf_sock_ops *skops)
33 {
34 	char header[sizeof(struct ipv6hdr) + sizeof(struct tcphdr)];
35 	struct bpf_sock_ops *reuse = skops;
36 	struct tcphdr *thdr;
37 	int window_clamp = 9216;
38 	int save_syn = 1;
39 	int rv = -1;
40 	int v = 0;
41 	int op;
42 
43 	/* Test reading fields in bpf_sock_ops using single register */
44 	asm volatile (
45 		"%[reuse] = *(u32 *)(%[reuse] +96)"
46 		: [reuse] "+r"(reuse)
47 		:);
48 
49 	asm volatile (
50 		"%[op] = *(u32 *)(%[skops] +96)"
51 		: [op] "=r"(op)
52 		: [skops] "r"(skops)
53 		:);
54 
55 	asm volatile (
56 		"r9 = %[skops];\n"
57 		"r8 = *(u32 *)(r9 +164);\n"
58 		"*(u32 *)(r9 +164) = r8;\n"
59 		:: [skops] "r"(skops)
60 		: "r9", "r8");
61 
62 	asm volatile (
63 		"r1 = %[skops];\n"
64 		"r1 = *(u64 *)(r1 +184);\n"
65 		"if r1 == 0 goto +1;\n"
66 		"r1 = *(u32 *)(r1 +4);\n"
67 		:: [skops] "r"(skops):"r1");
68 
69 	asm volatile (
70 		"r9 = %[skops];\n"
71 		"r9 = *(u64 *)(r9 +184);\n"
72 		"if r9 == 0 goto +1;\n"
73 		"r9 = *(u32 *)(r9 +4);\n"
74 		:: [skops] "r"(skops):"r9");
75 
76 	asm volatile (
77 		"r1 = %[skops];\n"
78 		"r2 = *(u64 *)(r1 +184);\n"
79 		"if r2 == 0 goto +1;\n"
80 		"r2 = *(u32 *)(r2 +4);\n"
81 		:: [skops] "r"(skops):"r1", "r2");
82 
83 	op = (int) skops->op;
84 
85 	global.event_map |= (1 << op);
86 
87 	switch (op) {
88 	case BPF_SOCK_OPS_TCP_CONNECT_CB:
89 		rv = bpf_setsockopt(skops, SOL_TCP, TCP_WINDOW_CLAMP,
90 				    &window_clamp, sizeof(window_clamp));
91 		global.window_clamp_client = get_tp_window_clamp(skops);
92 		break;
93 	case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
94 		/* Test failure to set largest cb flag (assumes not defined) */
95 		global.bad_cb_test_rv = bpf_sock_ops_cb_flags_set(skops, 0x80);
96 		/* Set callback */
97 		global.good_cb_test_rv = bpf_sock_ops_cb_flags_set(skops,
98 						 BPF_SOCK_OPS_STATE_CB_FLAG);
99 		break;
100 	case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
101 		skops->sk_txhash = 0x12345f;
102 		v = 0xff;
103 		rv = bpf_setsockopt(skops, SOL_IPV6, IPV6_TCLASS, &v,
104 				    sizeof(v));
105 		if (skops->family == AF_INET6) {
106 			v = bpf_getsockopt(skops, IPPROTO_TCP, TCP_SAVED_SYN,
107 					   header, (sizeof(struct ipv6hdr) +
108 						    sizeof(struct tcphdr)));
109 			if (!v) {
110 				int offset = sizeof(struct ipv6hdr);
111 
112 				thdr = (struct tcphdr *)(header + offset);
113 				v = thdr->syn;
114 
115 				global.tcp_saved_syn = v;
116 			}
117 		}
118 		rv = bpf_setsockopt(skops, SOL_TCP, TCP_WINDOW_CLAMP,
119 				    &window_clamp, sizeof(window_clamp));
120 
121 		global.window_clamp_server = get_tp_window_clamp(skops);
122 		break;
123 	case BPF_SOCK_OPS_RTO_CB:
124 		break;
125 	case BPF_SOCK_OPS_RETRANS_CB:
126 		break;
127 	case BPF_SOCK_OPS_STATE_CB:
128 		if (skops->args[1] == BPF_TCP_CLOSE) {
129 			if (skops->args[0] == BPF_TCP_LISTEN) {
130 				global.num_listen++;
131 			} else {
132 				global.total_retrans = skops->total_retrans;
133 				global.data_segs_in = skops->data_segs_in;
134 				global.data_segs_out = skops->data_segs_out;
135 				global.bytes_received = skops->bytes_received;
136 				global.bytes_acked = skops->bytes_acked;
137 			}
138 			global.num_close_events++;
139 		}
140 		break;
141 	case BPF_SOCK_OPS_TCP_LISTEN_CB:
142 		bpf_sock_ops_cb_flags_set(skops, BPF_SOCK_OPS_STATE_CB_FLAG);
143 		v = bpf_setsockopt(skops, IPPROTO_TCP, TCP_SAVE_SYN,
144 				   &save_syn, sizeof(save_syn));
145 		/* Update global map w/ result of setsock opt */
146 		global.tcp_save_syn = v;
147 		break;
148 	default:
149 		rv = -1;
150 	}
151 	skops->reply = rv;
152 	return 1;
153 }
154 char _license[] SEC("license") = "GPL";
155