1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 
4 /* TODO: corrupts other tests uses connect() */
5 void serial_test_probe_user(void)
6 {
7 	const char *prog_name = "handle_sys_connect";
8 	const char *obj_file = "./test_probe_user.o";
9 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, );
10 	int err, results_map_fd, sock_fd, duration = 0;
11 	struct sockaddr curr, orig, tmp;
12 	struct sockaddr_in *in = (struct sockaddr_in *)&curr;
13 	struct bpf_link *kprobe_link = NULL;
14 	struct bpf_program *kprobe_prog;
15 	struct bpf_object *obj;
16 	static const int zero = 0;
17 
18 	obj = bpf_object__open_file(obj_file, &opts);
19 	if (!ASSERT_OK_PTR(obj, "obj_open_file"))
20 		return;
21 
22 	kprobe_prog = bpf_object__find_program_by_name(obj, prog_name);
23 	if (CHECK(!kprobe_prog, "find_probe",
24 		  "prog '%s' not found\n", prog_name))
25 		goto cleanup;
26 
27 	err = bpf_object__load(obj);
28 	if (CHECK(err, "obj_load", "err %d\n", err))
29 		goto cleanup;
30 
31 	results_map_fd = bpf_find_map(__func__, obj, "test_pro.bss");
32 	if (CHECK(results_map_fd < 0, "find_bss_map",
33 		  "err %d\n", results_map_fd))
34 		goto cleanup;
35 
36 	kprobe_link = bpf_program__attach(kprobe_prog);
37 	if (!ASSERT_OK_PTR(kprobe_link, "attach_kprobe"))
38 		goto cleanup;
39 
40 	memset(&curr, 0, sizeof(curr));
41 	in->sin_family = AF_INET;
42 	in->sin_port = htons(5555);
43 	in->sin_addr.s_addr = inet_addr("255.255.255.255");
44 	memcpy(&orig, &curr, sizeof(curr));
45 
46 	sock_fd = socket(AF_INET, SOCK_STREAM, 0);
47 	if (CHECK(sock_fd < 0, "create_sock_fd", "err %d\n", sock_fd))
48 		goto cleanup;
49 
50 	connect(sock_fd, &curr, sizeof(curr));
51 	close(sock_fd);
52 
53 	err = bpf_map_lookup_elem(results_map_fd, &zero, &tmp);
54 	if (CHECK(err, "get_kprobe_res",
55 		  "failed to get kprobe res: %d\n", err))
56 		goto cleanup;
57 
58 	in = (struct sockaddr_in *)&tmp;
59 	if (CHECK(memcmp(&tmp, &orig, sizeof(orig)), "check_kprobe_res",
60 		  "wrong kprobe res from probe read: %s:%u\n",
61 		  inet_ntoa(in->sin_addr), ntohs(in->sin_port)))
62 		goto cleanup;
63 
64 	memset(&tmp, 0xab, sizeof(tmp));
65 
66 	in = (struct sockaddr_in *)&curr;
67 	if (CHECK(memcmp(&curr, &tmp, sizeof(tmp)), "check_kprobe_res",
68 		  "wrong kprobe res from probe write: %s:%u\n",
69 		  inet_ntoa(in->sin_addr), ntohs(in->sin_port)))
70 		goto cleanup;
71 cleanup:
72 	bpf_link__destroy(kprobe_link);
73 	bpf_object__close(obj);
74 }
75