| 
 | 1 | +// SPDX-License-Identifier: GPL-2.0  | 
 | 2 | +#include <test_progs.h>  | 
 | 3 | + | 
 | 4 | +void test_probe_user(void)  | 
 | 5 | +{  | 
 | 6 | +#define kprobe_name "__sys_connect"  | 
 | 7 | +	const char *prog_name = "kprobe/" kprobe_name;  | 
 | 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 (CHECK(IS_ERR(obj), "obj_open_file", "err %ld\n", PTR_ERR(obj)))  | 
 | 20 | +		return;  | 
 | 21 | + | 
 | 22 | +	kprobe_prog = bpf_object__find_program_by_title(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(kprobe_prog, false,  | 
 | 37 | +						 kprobe_name);  | 
 | 38 | +	if (CHECK(IS_ERR(kprobe_link), "attach_kprobe",  | 
 | 39 | +		  "err %ld\n", PTR_ERR(kprobe_link))) {  | 
 | 40 | +		kprobe_link = NULL;  | 
 | 41 | +		goto cleanup;  | 
 | 42 | +	}  | 
 | 43 | + | 
 | 44 | +	memset(&curr, 0, sizeof(curr));  | 
 | 45 | +	in->sin_family = AF_INET;  | 
 | 46 | +	in->sin_port = htons(5555);  | 
 | 47 | +	in->sin_addr.s_addr = inet_addr("255.255.255.255");  | 
 | 48 | +	memcpy(&orig, &curr, sizeof(curr));  | 
 | 49 | + | 
 | 50 | +	sock_fd = socket(AF_INET, SOCK_STREAM, 0);  | 
 | 51 | +	if (CHECK(sock_fd < 0, "create_sock_fd", "err %d\n", sock_fd))  | 
 | 52 | +		goto cleanup;  | 
 | 53 | + | 
 | 54 | +	connect(sock_fd, &curr, sizeof(curr));  | 
 | 55 | +	close(sock_fd);  | 
 | 56 | + | 
 | 57 | +	err = bpf_map_lookup_elem(results_map_fd, &zero, &tmp);  | 
 | 58 | +	if (CHECK(err, "get_kprobe_res",  | 
 | 59 | +		  "failed to get kprobe res: %d\n", err))  | 
 | 60 | +		goto cleanup;  | 
 | 61 | + | 
 | 62 | +	in = (struct sockaddr_in *)&tmp;  | 
 | 63 | +	if (CHECK(memcmp(&tmp, &orig, sizeof(orig)), "check_kprobe_res",  | 
 | 64 | +		  "wrong kprobe res from probe read: %s:%u\n",  | 
 | 65 | +		  inet_ntoa(in->sin_addr), ntohs(in->sin_port)))  | 
 | 66 | +		goto cleanup;  | 
 | 67 | + | 
 | 68 | +	memset(&tmp, 0xab, sizeof(tmp));  | 
 | 69 | + | 
 | 70 | +	in = (struct sockaddr_in *)&curr;  | 
 | 71 | +	if (CHECK(memcmp(&curr, &tmp, sizeof(tmp)), "check_kprobe_res",  | 
 | 72 | +		  "wrong kprobe res from probe write: %s:%u\n",  | 
 | 73 | +		  inet_ntoa(in->sin_addr), ntohs(in->sin_port)))  | 
 | 74 | +		goto cleanup;  | 
 | 75 | +cleanup:  | 
 | 76 | +	bpf_link__destroy(kprobe_link);  | 
 | 77 | +	bpf_object__close(obj);  | 
 | 78 | +}  | 
0 commit comments