1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2020 Facebook */
3 
4 #include <errno.h>
5 #include <net/if.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 
9 #include <bpf/bpf.h>
10 #include <bpf/hashmap.h>
11 
12 #include "json_writer.h"
13 #include "main.h"
14 
15 static const char * const link_type_name[] = {
16 	[BPF_LINK_TYPE_UNSPEC]			= "unspec",
17 	[BPF_LINK_TYPE_RAW_TRACEPOINT]		= "raw_tracepoint",
18 	[BPF_LINK_TYPE_TRACING]			= "tracing",
19 	[BPF_LINK_TYPE_CGROUP]			= "cgroup",
20 	[BPF_LINK_TYPE_ITER]			= "iter",
21 	[BPF_LINK_TYPE_NETNS]			= "netns",
22 };
23 
24 static struct hashmap *link_table;
25 
link_parse_fd(int * argc,char *** argv)26 static int link_parse_fd(int *argc, char ***argv)
27 {
28 	int fd;
29 
30 	if (is_prefix(**argv, "id")) {
31 		unsigned int id;
32 		char *endptr;
33 
34 		NEXT_ARGP();
35 
36 		id = strtoul(**argv, &endptr, 0);
37 		if (*endptr) {
38 			p_err("can't parse %s as ID", **argv);
39 			return -1;
40 		}
41 		NEXT_ARGP();
42 
43 		fd = bpf_link_get_fd_by_id(id);
44 		if (fd < 0)
45 			p_err("failed to get link with ID %d: %s", id, strerror(errno));
46 		return fd;
47 	} else if (is_prefix(**argv, "pinned")) {
48 		char *path;
49 
50 		NEXT_ARGP();
51 
52 		path = **argv;
53 		NEXT_ARGP();
54 
55 		return open_obj_pinned_any(path, BPF_OBJ_LINK);
56 	}
57 
58 	p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
59 	return -1;
60 }
61 
62 static void
show_link_header_json(struct bpf_link_info * info,json_writer_t * wtr)63 show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
64 {
65 	jsonw_uint_field(wtr, "id", info->id);
66 	if (info->type < ARRAY_SIZE(link_type_name))
67 		jsonw_string_field(wtr, "type", link_type_name[info->type]);
68 	else
69 		jsonw_uint_field(wtr, "type", info->type);
70 
71 	jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
72 }
73 
show_link_attach_type_json(__u32 attach_type,json_writer_t * wtr)74 static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
75 {
76 	if (attach_type < ARRAY_SIZE(attach_type_name))
77 		jsonw_string_field(wtr, "attach_type",
78 				   attach_type_name[attach_type]);
79 	else
80 		jsonw_uint_field(wtr, "attach_type", attach_type);
81 }
82 
is_iter_map_target(const char * target_name)83 static bool is_iter_map_target(const char *target_name)
84 {
85 	return strcmp(target_name, "bpf_map_elem") == 0 ||
86 	       strcmp(target_name, "bpf_sk_storage_map") == 0;
87 }
88 
show_iter_json(struct bpf_link_info * info,json_writer_t * wtr)89 static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
90 {
91 	const char *target_name = u64_to_ptr(info->iter.target_name);
92 
93 	jsonw_string_field(wtr, "target_name", target_name);
94 
95 	if (is_iter_map_target(target_name))
96 		jsonw_uint_field(wtr, "map_id", info->iter.map.map_id);
97 }
98 
get_prog_info(int prog_id,struct bpf_prog_info * info)99 static int get_prog_info(int prog_id, struct bpf_prog_info *info)
100 {
101 	__u32 len = sizeof(*info);
102 	int err, prog_fd;
103 
104 	prog_fd = bpf_prog_get_fd_by_id(prog_id);
105 	if (prog_fd < 0)
106 		return prog_fd;
107 
108 	memset(info, 0, sizeof(*info));
109 	err = bpf_obj_get_info_by_fd(prog_fd, info, &len);
110 	if (err)
111 		p_err("can't get prog info: %s", strerror(errno));
112 	close(prog_fd);
113 	return err;
114 }
115 
show_link_close_json(int fd,struct bpf_link_info * info)116 static int show_link_close_json(int fd, struct bpf_link_info *info)
117 {
118 	struct bpf_prog_info prog_info;
119 	int err;
120 
121 	jsonw_start_object(json_wtr);
122 
123 	show_link_header_json(info, json_wtr);
124 
125 	switch (info->type) {
126 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
127 		jsonw_string_field(json_wtr, "tp_name",
128 				   u64_to_ptr(info->raw_tracepoint.tp_name));
129 		break;
130 	case BPF_LINK_TYPE_TRACING:
131 		err = get_prog_info(info->prog_id, &prog_info);
132 		if (err)
133 			return err;
134 
135 		if (prog_info.type < prog_type_name_size)
136 			jsonw_string_field(json_wtr, "prog_type",
137 					   prog_type_name[prog_info.type]);
138 		else
139 			jsonw_uint_field(json_wtr, "prog_type",
140 					 prog_info.type);
141 
142 		show_link_attach_type_json(info->tracing.attach_type,
143 					   json_wtr);
144 		break;
145 	case BPF_LINK_TYPE_CGROUP:
146 		jsonw_lluint_field(json_wtr, "cgroup_id",
147 				   info->cgroup.cgroup_id);
148 		show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
149 		break;
150 	case BPF_LINK_TYPE_ITER:
151 		show_iter_json(info, json_wtr);
152 		break;
153 	case BPF_LINK_TYPE_NETNS:
154 		jsonw_uint_field(json_wtr, "netns_ino",
155 				 info->netns.netns_ino);
156 		show_link_attach_type_json(info->netns.attach_type, json_wtr);
157 		break;
158 	default:
159 		break;
160 	}
161 
162 	if (!hashmap__empty(link_table)) {
163 		struct hashmap_entry *entry;
164 
165 		jsonw_name(json_wtr, "pinned");
166 		jsonw_start_array(json_wtr);
167 		hashmap__for_each_key_entry(link_table, entry,
168 					    u32_as_hash_field(info->id))
169 			jsonw_string(json_wtr, entry->value);
170 		jsonw_end_array(json_wtr);
171 	}
172 
173 	emit_obj_refs_json(refs_table, info->id, json_wtr);
174 
175 	jsonw_end_object(json_wtr);
176 
177 	return 0;
178 }
179 
show_link_header_plain(struct bpf_link_info * info)180 static void show_link_header_plain(struct bpf_link_info *info)
181 {
182 	printf("%u: ", info->id);
183 	if (info->type < ARRAY_SIZE(link_type_name))
184 		printf("%s  ", link_type_name[info->type]);
185 	else
186 		printf("type %u  ", info->type);
187 
188 	printf("prog %u  ", info->prog_id);
189 }
190 
show_link_attach_type_plain(__u32 attach_type)191 static void show_link_attach_type_plain(__u32 attach_type)
192 {
193 	if (attach_type < ARRAY_SIZE(attach_type_name))
194 		printf("attach_type %s  ", attach_type_name[attach_type]);
195 	else
196 		printf("attach_type %u  ", attach_type);
197 }
198 
show_iter_plain(struct bpf_link_info * info)199 static void show_iter_plain(struct bpf_link_info *info)
200 {
201 	const char *target_name = u64_to_ptr(info->iter.target_name);
202 
203 	printf("target_name %s  ", target_name);
204 
205 	if (is_iter_map_target(target_name))
206 		printf("map_id %u  ", info->iter.map.map_id);
207 }
208 
show_link_close_plain(int fd,struct bpf_link_info * info)209 static int show_link_close_plain(int fd, struct bpf_link_info *info)
210 {
211 	struct bpf_prog_info prog_info;
212 	int err;
213 
214 	show_link_header_plain(info);
215 
216 	switch (info->type) {
217 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
218 		printf("\n\ttp '%s'  ",
219 		       (const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
220 		break;
221 	case BPF_LINK_TYPE_TRACING:
222 		err = get_prog_info(info->prog_id, &prog_info);
223 		if (err)
224 			return err;
225 
226 		if (prog_info.type < prog_type_name_size)
227 			printf("\n\tprog_type %s  ",
228 			       prog_type_name[prog_info.type]);
229 		else
230 			printf("\n\tprog_type %u  ", prog_info.type);
231 
232 		show_link_attach_type_plain(info->tracing.attach_type);
233 		break;
234 	case BPF_LINK_TYPE_CGROUP:
235 		printf("\n\tcgroup_id %zu  ", (size_t)info->cgroup.cgroup_id);
236 		show_link_attach_type_plain(info->cgroup.attach_type);
237 		break;
238 	case BPF_LINK_TYPE_ITER:
239 		show_iter_plain(info);
240 		break;
241 	case BPF_LINK_TYPE_NETNS:
242 		printf("\n\tnetns_ino %u  ", info->netns.netns_ino);
243 		show_link_attach_type_plain(info->netns.attach_type);
244 		break;
245 	default:
246 		break;
247 	}
248 
249 	if (!hashmap__empty(link_table)) {
250 		struct hashmap_entry *entry;
251 
252 		hashmap__for_each_key_entry(link_table, entry,
253 					    u32_as_hash_field(info->id))
254 			printf("\n\tpinned %s", (char *)entry->value);
255 	}
256 	emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
257 
258 	printf("\n");
259 
260 	return 0;
261 }
262 
do_show_link(int fd)263 static int do_show_link(int fd)
264 {
265 	struct bpf_link_info info;
266 	__u32 len = sizeof(info);
267 	char buf[256];
268 	int err;
269 
270 	memset(&info, 0, sizeof(info));
271 again:
272 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
273 	if (err) {
274 		p_err("can't get link info: %s",
275 		      strerror(errno));
276 		close(fd);
277 		return err;
278 	}
279 	if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
280 	    !info.raw_tracepoint.tp_name) {
281 		info.raw_tracepoint.tp_name = (unsigned long)&buf;
282 		info.raw_tracepoint.tp_name_len = sizeof(buf);
283 		goto again;
284 	}
285 	if (info.type == BPF_LINK_TYPE_ITER &&
286 	    !info.iter.target_name) {
287 		info.iter.target_name = (unsigned long)&buf;
288 		info.iter.target_name_len = sizeof(buf);
289 		goto again;
290 	}
291 
292 	if (json_output)
293 		show_link_close_json(fd, &info);
294 	else
295 		show_link_close_plain(fd, &info);
296 
297 	close(fd);
298 	return 0;
299 }
300 
do_show(int argc,char ** argv)301 static int do_show(int argc, char **argv)
302 {
303 	__u32 id = 0;
304 	int err, fd;
305 
306 	if (show_pinned) {
307 		link_table = hashmap__new(hash_fn_for_key_as_id,
308 					  equal_fn_for_key_as_id, NULL);
309 		if (!link_table) {
310 			p_err("failed to create hashmap for pinned paths");
311 			return -1;
312 		}
313 		build_pinned_obj_table(link_table, BPF_OBJ_LINK);
314 	}
315 	build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
316 
317 	if (argc == 2) {
318 		fd = link_parse_fd(&argc, &argv);
319 		if (fd < 0)
320 			return fd;
321 		return do_show_link(fd);
322 	}
323 
324 	if (argc)
325 		return BAD_ARG();
326 
327 	if (json_output)
328 		jsonw_start_array(json_wtr);
329 	while (true) {
330 		err = bpf_link_get_next_id(id, &id);
331 		if (err) {
332 			if (errno == ENOENT)
333 				break;
334 			p_err("can't get next link: %s%s", strerror(errno),
335 			      errno == EINVAL ? " -- kernel too old?" : "");
336 			break;
337 		}
338 
339 		fd = bpf_link_get_fd_by_id(id);
340 		if (fd < 0) {
341 			if (errno == ENOENT)
342 				continue;
343 			p_err("can't get link by id (%u): %s",
344 			      id, strerror(errno));
345 			break;
346 		}
347 
348 		err = do_show_link(fd);
349 		if (err)
350 			break;
351 	}
352 	if (json_output)
353 		jsonw_end_array(json_wtr);
354 
355 	delete_obj_refs_table(refs_table);
356 
357 	if (show_pinned)
358 		delete_pinned_obj_table(link_table);
359 
360 	return errno == ENOENT ? 0 : -1;
361 }
362 
do_pin(int argc,char ** argv)363 static int do_pin(int argc, char **argv)
364 {
365 	int err;
366 
367 	err = do_pin_any(argc, argv, link_parse_fd);
368 	if (!err && json_output)
369 		jsonw_null(json_wtr);
370 	return err;
371 }
372 
do_detach(int argc,char ** argv)373 static int do_detach(int argc, char **argv)
374 {
375 	int err, fd;
376 
377 	if (argc != 2) {
378 		p_err("link specifier is invalid or missing\n");
379 		return 1;
380 	}
381 
382 	fd = link_parse_fd(&argc, &argv);
383 	if (fd < 0)
384 		return 1;
385 
386 	err = bpf_link_detach(fd);
387 	if (err)
388 		err = -errno;
389 	close(fd);
390 	if (err) {
391 		p_err("failed link detach: %s", strerror(-err));
392 		return 1;
393 	}
394 
395 	if (json_output)
396 		jsonw_null(json_wtr);
397 
398 	return 0;
399 }
400 
do_help(int argc,char ** argv)401 static int do_help(int argc, char **argv)
402 {
403 	if (json_output) {
404 		jsonw_null(json_wtr);
405 		return 0;
406 	}
407 
408 	fprintf(stderr,
409 		"Usage: %1$s %2$s { show | list }   [LINK]\n"
410 		"       %1$s %2$s pin        LINK  FILE\n"
411 		"       %1$s %2$s detach     LINK\n"
412 		"       %1$s %2$s help\n"
413 		"\n"
414 		"       " HELP_SPEC_LINK "\n"
415 		"       " HELP_SPEC_OPTIONS " |\n"
416 		"                    {-f|--bpffs} | {-n|--nomount} }\n"
417 		"",
418 		bin_name, argv[-2]);
419 
420 	return 0;
421 }
422 
423 static const struct cmd cmds[] = {
424 	{ "show",	do_show },
425 	{ "list",	do_show },
426 	{ "help",	do_help },
427 	{ "pin",	do_pin },
428 	{ "detach",	do_detach },
429 	{ 0 }
430 };
431 
do_link(int argc,char ** argv)432 int do_link(int argc, char **argv)
433 {
434 	return cmd_select(cmds, argc, argv, do_help);
435 }
436