summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/cgroup_tcp_skb.c
blob: 1e2e73f3b7493489a623723e6bcf323db58f7f19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
#include <linux/bpf.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>

#include <linux/if_ether.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/ipv6.h>
#include <linux/tcp.h>

#include <sys/types.h>
#include <sys/socket.h>

#include "cgroup_tcp_skb.h"

char _license[] SEC("license") = "GPL";

__u16 g_sock_port = 0;
__u32 g_sock_state = 0;
int g_unexpected = 0;
__u32 g_packet_count = 0;

int needed_tcp_pkt(struct __sk_buff *skb, struct tcphdr *tcph)
{
	struct ipv6hdr ip6h;

	if (skb->protocol != bpf_htons(ETH_P_IPV6))
		return 0;
	if (bpf_skb_load_bytes(skb, 0, &ip6h, sizeof(ip6h)))
		return 0;

	if (ip6h.nexthdr != IPPROTO_TCP)
		return 0;

	if (bpf_skb_load_bytes(skb, sizeof(ip6h), tcph, sizeof(*tcph)))
		return 0;

	if (tcph->source != bpf_htons(g_sock_port) &&
	    tcph->dest != bpf_htons(g_sock_port))
		return 0;

	return 1;
}

/* Run accept() on a socket in the cgroup to receive a new connection. */
static int egress_accept(struct tcphdr *tcph)
{
	if (g_sock_state ==  SYN_RECV_SENDING_SYN_ACK) {
		if (tcph->fin || !tcph->syn || !tcph->ack)
			g_unexpected++;
		else
			g_sock_state = SYN_RECV;
		return 1;
	}

	return 0;
}

static int ingress_accept(struct tcphdr *tcph)
{
	switch (g_sock_state) {
	case INIT:
		if (!tcph->syn || tcph->fin || tcph->ack)
			g_unexpected++;
		else
			g_sock_state = SYN_RECV_SENDING_SYN_ACK;
		break;
	case SYN_RECV:
		if (tcph->fin || tcph->syn || !tcph->ack)
			g_unexpected++;
		else
			g_sock_state = ESTABLISHED;
		break;
	default:
		return 0;
	}

	return 1;
}

/* Run connect() on a socket in the cgroup to start a new connection. */
static int egress_connect(struct tcphdr *tcph)
{
	if (g_sock_state == INIT) {
		if (!tcph->syn || tcph->fin || tcph->ack)
			g_unexpected++;
		else
			g_sock_state = SYN_SENT;
		return 1;
	}

	return 0;
}

static int ingress_connect(struct tcphdr *tcph)
{
	if (g_sock_state == SYN_SENT) {
		if (tcph->fin || !tcph->syn || !tcph->ack)
			g_unexpected++;
		else
			g_sock_state = ESTABLISHED;
		return 1;
	}

	return 0;
}

/* The connection is closed by the peer outside the cgroup. */
static int egress_close_remote(struct tcphdr *tcph)
{
	switch (g_sock_state) {
	case ESTABLISHED:
		break;
	case CLOSE_WAIT_SENDING_ACK:
		if (tcph->fin || tcph->syn || !tcph->ack)
			g_unexpected++;
		else
			g_sock_state = CLOSE_WAIT;
		break;
	case CLOSE_WAIT:
		if (!tcph->fin)
			g_unexpected++;
		else
			g_sock_state = LAST_ACK;
		break;
	default:
		return 0;
	}

	return 1;
}

static int ingress_close_remote(struct tcphdr *tcph)
{
	switch (g_sock_state) {
	case ESTABLISHED:
		if (tcph->fin)
			g_sock_state = CLOSE_WAIT_SENDING_ACK;
		break;
	case LAST_ACK:
		if (tcph->fin || tcph->syn || !tcph->ack)
			g_unexpected++;
		else
			g_sock_state = CLOSED;
		break;
	default:
		return 0;
	}

	return 1;
}

/* The connection is closed by the endpoint inside the cgroup. */
static int egress_close_local(struct tcphdr *tcph)
{
	switch (g_sock_state) {
	case ESTABLISHED:
		if (tcph->fin)
			g_sock_state = FIN_WAIT1;
		break;
	case TIME_WAIT_SENDING_ACK:
		if (tcph->fin || tcph->syn || !tcph->ack)
			g_unexpected++;
		else
			g_sock_state = TIME_WAIT;
		break;
	default:
		return 0;
	}

	return 1;
}

static int ingress_close_local(struct tcphdr *tcph)
{
	switch (g_sock_state) {
	case ESTABLISHED:
		break;
	case FIN_WAIT1:
		if (tcph->fin || tcph->syn || !tcph->ack)
			g_unexpected++;
		else
			g_sock_state = FIN_WAIT2;
		break;
	case FIN_WAIT2:
		if (!tcph->fin || tcph->syn || !tcph->ack)
			g_unexpected++;
		else
			g_sock_state = TIME_WAIT_SENDING_ACK;
		break;
	default:
		return 0;
	}

	return 1;
}

/* Check the types of outgoing packets of a server socket to make sure they
 * are consistent with the state of the server socket.
 *
 * The connection is closed by the client side.
 */
SEC("cgroup_skb/egress")
int server_egress(struct __sk_buff *skb)
{
	struct tcphdr tcph;

	if (!needed_tcp_pkt(skb, &tcph))
		return 1;

	g_packet_count++;

	/* Egress of the server socket. */
	if (egress_accept(&tcph) || egress_close_remote(&tcph))
		return 1;

	g_unexpected++;
	return 1;
}

/* Check the types of incoming packets of a server socket to make sure they
 * are consistent with the state of the server socket.
 *
 * The connection is closed by the client side.
 */
SEC("cgroup_skb/ingress")
int server_ingress(struct __sk_buff *skb)
{
	struct tcphdr tcph;

	if (!needed_tcp_pkt(skb, &tcph))
		return 1;

	g_packet_count++;

	/* Ingress of the server socket. */
	if (ingress_accept(&tcph) || ingress_close_remote(&tcph))
		return 1;

	g_unexpected++;
	return 1;
}

/* Check the types of outgoing packets of a server socket to make sure they
 * are consistent with the state of the server socket.
 *
 * The connection is closed by the server side.
 */
SEC("cgroup_skb/egress")
int server_egress_srv(struct __sk_buff *skb)
{
	struct tcphdr tcph;

	if (!needed_tcp_pkt(skb, &tcph))
		return 1;

	g_packet_count++;

	/* Egress of the server socket. */
	if (egress_accept(&tcph) || egress_close_local(&tcph))
		return 1;

	g_unexpected++;
	return 1;
}

/* Check the types of incoming packets of a server socket to make sure they
 * are consistent with the state of the server socket.
 *
 * The connection is closed by the server side.
 */
SEC("cgroup_skb/ingress")
int server_ingress_srv(struct __sk_buff *skb)
{
	struct tcphdr tcph;

	if (!needed_tcp_pkt(skb, &tcph))
		return 1;

	g_packet_count++;

	/* Ingress of the server socket. */
	if (ingress_accept(&tcph) || ingress_close_local(&tcph))
		return 1;

	g_unexpected++;
	return 1;
}

/* Check the types of outgoing packets of a client socket to make sure they
 * are consistent with the state of the client socket.
 *
 * The connection is closed by the server side.
 */
SEC("cgroup_skb/egress")
int client_egress_srv(struct __sk_buff *skb)
{
	struct tcphdr tcph;

	if (!needed_tcp_pkt(skb, &tcph))
		return 1;

	g_packet_count++;

	/* Egress of the server socket. */
	if (egress_connect(&tcph) || egress_close_remote(&tcph))
		return 1;

	g_unexpected++;
	return 1;
}

/* Check the types of incoming packets of a client socket to make sure they
 * are consistent with the state of the client socket.
 *
 * The connection is closed by the server side.
 */
SEC("cgroup_skb/ingress")
int client_ingress_srv(struct __sk_buff *skb)
{
	struct tcphdr tcph;

	if (!needed_tcp_pkt(skb, &tcph))
		return 1;

	g_packet_count++;

	/* Ingress of the server socket. */
	if (ingress_connect(&tcph) || ingress_close_remote(&tcph))
		return 1;

	g_unexpected++;
	return 1;
}

/* Check the types of outgoing packets of a client socket to make sure they
 * are consistent with the state of the client socket.
 *
 * The connection is closed by the client side.
 */
SEC("cgroup_skb/egress")
int client_egress(struct __sk_buff *skb)
{
	struct tcphdr tcph;

	if (!needed_tcp_pkt(skb, &tcph))
		return 1;

	g_packet_count++;

	/* Egress of the server socket. */
	if (egress_connect(&tcph) || egress_close_local(&tcph))
		return 1;

	g_unexpected++;
	return 1;
}

/* Check the types of incoming packets of a client socket to make sure they
 * are consistent with the state of the client socket.
 *
 * The connection is closed by the client side.
 */
SEC("cgroup_skb/ingress")
int client_ingress(struct __sk_buff *skb)
{
	struct tcphdr tcph;

	if (!needed_tcp_pkt(skb, &tcph))
		return 1;

	g_packet_count++;

	/* Ingress of the server socket. */
	if (ingress_connect(&tcph) || ingress_close_local(&tcph))
		return 1;

	g_unexpected++;
	return 1;
}