1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
4 void test_probe_user(void)
6 const char *prog_name
= "kprobe/__sys_connect";
7 const char *obj_file
= "./test_probe_user.o";
8 DECLARE_LIBBPF_OPTS(bpf_object_open_opts
, opts
, );
9 int err
, results_map_fd
, sock_fd
, duration
= 0;
10 struct sockaddr curr
, orig
, tmp
;
11 struct sockaddr_in
*in
= (struct sockaddr_in
*)&curr
;
12 struct bpf_link
*kprobe_link
= NULL
;
13 struct bpf_program
*kprobe_prog
;
14 struct bpf_object
*obj
;
15 static const int zero
= 0;
17 obj
= bpf_object__open_file(obj_file
, &opts
);
18 if (CHECK(IS_ERR(obj
), "obj_open_file", "err %ld\n", PTR_ERR(obj
)))
21 kprobe_prog
= bpf_object__find_program_by_title(obj
, prog_name
);
22 if (CHECK(!kprobe_prog
, "find_probe",
23 "prog '%s' not found\n", prog_name
))
26 err
= bpf_object__load(obj
);
27 if (CHECK(err
, "obj_load", "err %d\n", err
))
30 results_map_fd
= bpf_find_map(__func__
, obj
, "test_pro.bss");
31 if (CHECK(results_map_fd
< 0, "find_bss_map",
32 "err %d\n", results_map_fd
))
35 kprobe_link
= bpf_program__attach(kprobe_prog
);
36 if (CHECK(IS_ERR(kprobe_link
), "attach_kprobe",
37 "err %ld\n", PTR_ERR(kprobe_link
))) {
42 memset(&curr
, 0, sizeof(curr
));
43 in
->sin_family
= AF_INET
;
44 in
->sin_port
= htons(5555);
45 in
->sin_addr
.s_addr
= inet_addr("255.255.255.255");
46 memcpy(&orig
, &curr
, sizeof(curr
));
48 sock_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
49 if (CHECK(sock_fd
< 0, "create_sock_fd", "err %d\n", sock_fd
))
52 connect(sock_fd
, &curr
, sizeof(curr
));
55 err
= bpf_map_lookup_elem(results_map_fd
, &zero
, &tmp
);
56 if (CHECK(err
, "get_kprobe_res",
57 "failed to get kprobe res: %d\n", err
))
60 in
= (struct sockaddr_in
*)&tmp
;
61 if (CHECK(memcmp(&tmp
, &orig
, sizeof(orig
)), "check_kprobe_res",
62 "wrong kprobe res from probe read: %s:%u\n",
63 inet_ntoa(in
->sin_addr
), ntohs(in
->sin_port
)))
66 memset(&tmp
, 0xab, sizeof(tmp
));
68 in
= (struct sockaddr_in
*)&curr
;
69 if (CHECK(memcmp(&curr
, &tmp
, sizeof(tmp
)), "check_kprobe_res",
70 "wrong kprobe res from probe write: %s:%u\n",
71 inet_ntoa(in
->sin_addr
), ntohs(in
->sin_port
)))
74 bpf_link__destroy(kprobe_link
);
75 bpf_object__close(obj
);