1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017 Facebook
4  */
5 #include <sys/socket.h>
6 #include <arpa/inet.h>
7 #include <stdint.h>
8 #include <assert.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <bpf/bpf.h>
13 #include <bpf/libbpf.h>
14 
15 static int map_fd[7];
16 
17 #define PORT_A		(map_fd[0])
18 #define PORT_H		(map_fd[1])
19 #define REG_RESULT_H	(map_fd[2])
20 #define INLINE_RESULT_H	(map_fd[3])
21 #define A_OF_PORT_A	(map_fd[4]) /* Test case #0 */
22 #define H_OF_PORT_A	(map_fd[5]) /* Test case #1 */
23 #define H_OF_PORT_H	(map_fd[6]) /* Test case #2 */
24 
25 static const char * const test_names[] = {
26 	"Array of Array",
27 	"Hash of Array",
28 	"Hash of Hash",
29 };
30 
31 #define NR_TESTS (sizeof(test_names) / sizeof(*test_names))
32 
33 static void check_map_id(int inner_map_fd, int map_in_map_fd, uint32_t key)
34 {
35 	struct bpf_map_info info = {};
36 	uint32_t info_len = sizeof(info);
37 	int ret, id;
38 
39 	ret = bpf_obj_get_info_by_fd(inner_map_fd, &info, &info_len);
40 	assert(!ret);
41 
42 	ret = bpf_map_lookup_elem(map_in_map_fd, &key, &id);
43 	assert(!ret);
44 	assert(id == info.id);
45 }
46 
47 static void populate_map(uint32_t port_key, int magic_result)
48 {
49 	int ret;
50 
51 	ret = bpf_map_update_elem(PORT_A, &port_key, &magic_result, BPF_ANY);
52 	assert(!ret);
53 
54 	ret = bpf_map_update_elem(PORT_H, &port_key, &magic_result,
55 				  BPF_NOEXIST);
56 	assert(!ret);
57 
58 	ret = bpf_map_update_elem(A_OF_PORT_A, &port_key, &PORT_A, BPF_ANY);
59 	assert(!ret);
60 	check_map_id(PORT_A, A_OF_PORT_A, port_key);
61 
62 	ret = bpf_map_update_elem(H_OF_PORT_A, &port_key, &PORT_A, BPF_NOEXIST);
63 	assert(!ret);
64 	check_map_id(PORT_A, H_OF_PORT_A, port_key);
65 
66 	ret = bpf_map_update_elem(H_OF_PORT_H, &port_key, &PORT_H, BPF_NOEXIST);
67 	assert(!ret);
68 	check_map_id(PORT_H, H_OF_PORT_H, port_key);
69 }
70 
71 static void test_map_in_map(void)
72 {
73 	struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
74 	uint32_t result_key = 0, port_key;
75 	int result, inline_result;
76 	int magic_result = 0xfaceb00c;
77 	int ret;
78 	int i;
79 
80 	port_key = rand() & 0x00FF;
81 	populate_map(port_key, magic_result);
82 
83 	in6.sin6_addr.s6_addr16[0] = 0xdead;
84 	in6.sin6_addr.s6_addr16[1] = 0xbeef;
85 	in6.sin6_port = port_key;
86 
87 	for (i = 0; i < NR_TESTS; i++) {
88 		printf("%s: ", test_names[i]);
89 
90 		in6.sin6_addr.s6_addr16[7] = i;
91 		ret = connect(-1, (struct sockaddr *)&in6, sizeof(in6));
92 		assert(ret == -1 && errno == EBADF);
93 
94 		ret = bpf_map_lookup_elem(REG_RESULT_H, &result_key, &result);
95 		assert(!ret);
96 
97 		ret = bpf_map_lookup_elem(INLINE_RESULT_H, &result_key,
98 					  &inline_result);
99 		assert(!ret);
100 
101 		if (result != magic_result || inline_result != magic_result) {
102 			printf("Error. result:%d inline_result:%d\n",
103 			       result, inline_result);
104 			exit(1);
105 		}
106 
107 		bpf_map_delete_elem(REG_RESULT_H, &result_key);
108 		bpf_map_delete_elem(INLINE_RESULT_H, &result_key);
109 
110 		printf("Pass\n");
111 	}
112 }
113 
114 int main(int argc, char **argv)
115 {
116 	struct bpf_link *link = NULL;
117 	struct bpf_program *prog;
118 	struct bpf_object *obj;
119 	char filename[256];
120 
121 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
122 	obj = bpf_object__open_file(filename, NULL);
123 	if (libbpf_get_error(obj)) {
124 		fprintf(stderr, "ERROR: opening BPF object file failed\n");
125 		return 0;
126 	}
127 
128 	prog = bpf_object__find_program_by_name(obj, "trace_sys_connect");
129 	if (!prog) {
130 		printf("finding a prog in obj file failed\n");
131 		goto cleanup;
132 	}
133 
134 	/* load BPF program */
135 	if (bpf_object__load(obj)) {
136 		fprintf(stderr, "ERROR: loading BPF object file failed\n");
137 		goto cleanup;
138 	}
139 
140 	map_fd[0] = bpf_object__find_map_fd_by_name(obj, "port_a");
141 	map_fd[1] = bpf_object__find_map_fd_by_name(obj, "port_h");
142 	map_fd[2] = bpf_object__find_map_fd_by_name(obj, "reg_result_h");
143 	map_fd[3] = bpf_object__find_map_fd_by_name(obj, "inline_result_h");
144 	map_fd[4] = bpf_object__find_map_fd_by_name(obj, "a_of_port_a");
145 	map_fd[5] = bpf_object__find_map_fd_by_name(obj, "h_of_port_a");
146 	map_fd[6] = bpf_object__find_map_fd_by_name(obj, "h_of_port_h");
147 	if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0 ||
148 	    map_fd[3] < 0 || map_fd[4] < 0 || map_fd[5] < 0 || map_fd[6] < 0) {
149 		fprintf(stderr, "ERROR: finding a map in obj file failed\n");
150 		goto cleanup;
151 	}
152 
153 	link = bpf_program__attach(prog);
154 	if (libbpf_get_error(link)) {
155 		fprintf(stderr, "ERROR: bpf_program__attach failed\n");
156 		link = NULL;
157 		goto cleanup;
158 	}
159 
160 	test_map_in_map();
161 
162 cleanup:
163 	bpf_link__destroy(link);
164 	bpf_object__close(obj);
165 	return 0;
166 }
167