xref: /openbmc/linux/tools/bpf/bpftool/link.c (revision 26b32974)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2020 Facebook */
3 
4 #include <errno.h>
5 #include <linux/err.h>
6 #include <linux/netfilter.h>
7 #include <linux/netfilter_arp.h>
8 #include <net/if.h>
9 #include <stdio.h>
10 #include <unistd.h>
11 
12 #include <bpf/bpf.h>
13 #include <bpf/hashmap.h>
14 
15 #include "json_writer.h"
16 #include "main.h"
17 
18 static struct hashmap *link_table;
19 
20 static int link_parse_fd(int *argc, char ***argv)
21 {
22 	int fd;
23 
24 	if (is_prefix(**argv, "id")) {
25 		unsigned int id;
26 		char *endptr;
27 
28 		NEXT_ARGP();
29 
30 		id = strtoul(**argv, &endptr, 0);
31 		if (*endptr) {
32 			p_err("can't parse %s as ID", **argv);
33 			return -1;
34 		}
35 		NEXT_ARGP();
36 
37 		fd = bpf_link_get_fd_by_id(id);
38 		if (fd < 0)
39 			p_err("failed to get link with ID %d: %s", id, strerror(errno));
40 		return fd;
41 	} else if (is_prefix(**argv, "pinned")) {
42 		char *path;
43 
44 		NEXT_ARGP();
45 
46 		path = **argv;
47 		NEXT_ARGP();
48 
49 		return open_obj_pinned_any(path, BPF_OBJ_LINK);
50 	}
51 
52 	p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
53 	return -1;
54 }
55 
56 static void
57 show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
58 {
59 	const char *link_type_str;
60 
61 	jsonw_uint_field(wtr, "id", info->id);
62 	link_type_str = libbpf_bpf_link_type_str(info->type);
63 	if (link_type_str)
64 		jsonw_string_field(wtr, "type", link_type_str);
65 	else
66 		jsonw_uint_field(wtr, "type", info->type);
67 
68 	jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
69 }
70 
71 static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
72 {
73 	const char *attach_type_str;
74 
75 	attach_type_str = libbpf_bpf_attach_type_str(attach_type);
76 	if (attach_type_str)
77 		jsonw_string_field(wtr, "attach_type", attach_type_str);
78 	else
79 		jsonw_uint_field(wtr, "attach_type", attach_type);
80 }
81 
82 static bool is_iter_map_target(const char *target_name)
83 {
84 	return strcmp(target_name, "bpf_map_elem") == 0 ||
85 	       strcmp(target_name, "bpf_sk_storage_map") == 0;
86 }
87 
88 static bool is_iter_cgroup_target(const char *target_name)
89 {
90 	return strcmp(target_name, "cgroup") == 0;
91 }
92 
93 static const char *cgroup_order_string(__u32 order)
94 {
95 	switch (order) {
96 	case BPF_CGROUP_ITER_ORDER_UNSPEC:
97 		return "order_unspec";
98 	case BPF_CGROUP_ITER_SELF_ONLY:
99 		return "self_only";
100 	case BPF_CGROUP_ITER_DESCENDANTS_PRE:
101 		return "descendants_pre";
102 	case BPF_CGROUP_ITER_DESCENDANTS_POST:
103 		return "descendants_post";
104 	case BPF_CGROUP_ITER_ANCESTORS_UP:
105 		return "ancestors_up";
106 	default: /* won't happen */
107 		return "unknown";
108 	}
109 }
110 
111 static bool is_iter_task_target(const char *target_name)
112 {
113 	return strcmp(target_name, "task") == 0 ||
114 		strcmp(target_name, "task_file") == 0 ||
115 		strcmp(target_name, "task_vma") == 0;
116 }
117 
118 static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
119 {
120 	const char *target_name = u64_to_ptr(info->iter.target_name);
121 
122 	jsonw_string_field(wtr, "target_name", target_name);
123 
124 	if (is_iter_map_target(target_name))
125 		jsonw_uint_field(wtr, "map_id", info->iter.map.map_id);
126 	else if (is_iter_task_target(target_name)) {
127 		if (info->iter.task.tid)
128 			jsonw_uint_field(wtr, "tid", info->iter.task.tid);
129 		else if (info->iter.task.pid)
130 			jsonw_uint_field(wtr, "pid", info->iter.task.pid);
131 	}
132 
133 	if (is_iter_cgroup_target(target_name)) {
134 		jsonw_lluint_field(wtr, "cgroup_id", info->iter.cgroup.cgroup_id);
135 		jsonw_string_field(wtr, "order",
136 				   cgroup_order_string(info->iter.cgroup.order));
137 	}
138 }
139 
140 void netfilter_dump_json(const struct bpf_link_info *info, json_writer_t *wtr)
141 {
142 	jsonw_uint_field(json_wtr, "pf",
143 			 info->netfilter.pf);
144 	jsonw_uint_field(json_wtr, "hook",
145 			 info->netfilter.hooknum);
146 	jsonw_int_field(json_wtr, "prio",
147 			 info->netfilter.priority);
148 	jsonw_uint_field(json_wtr, "flags",
149 			 info->netfilter.flags);
150 }
151 
152 static int get_prog_info(int prog_id, struct bpf_prog_info *info)
153 {
154 	__u32 len = sizeof(*info);
155 	int err, prog_fd;
156 
157 	prog_fd = bpf_prog_get_fd_by_id(prog_id);
158 	if (prog_fd < 0)
159 		return prog_fd;
160 
161 	memset(info, 0, sizeof(*info));
162 	err = bpf_prog_get_info_by_fd(prog_fd, info, &len);
163 	if (err)
164 		p_err("can't get prog info: %s", strerror(errno));
165 	close(prog_fd);
166 	return err;
167 }
168 
169 static int show_link_close_json(int fd, struct bpf_link_info *info)
170 {
171 	struct bpf_prog_info prog_info;
172 	const char *prog_type_str;
173 	int err;
174 
175 	jsonw_start_object(json_wtr);
176 
177 	show_link_header_json(info, json_wtr);
178 
179 	switch (info->type) {
180 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
181 		jsonw_string_field(json_wtr, "tp_name",
182 				   u64_to_ptr(info->raw_tracepoint.tp_name));
183 		break;
184 	case BPF_LINK_TYPE_TRACING:
185 		err = get_prog_info(info->prog_id, &prog_info);
186 		if (err)
187 			return err;
188 
189 		prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
190 		/* libbpf will return NULL for variants unknown to it. */
191 		if (prog_type_str)
192 			jsonw_string_field(json_wtr, "prog_type", prog_type_str);
193 		else
194 			jsonw_uint_field(json_wtr, "prog_type", prog_info.type);
195 
196 		show_link_attach_type_json(info->tracing.attach_type,
197 					   json_wtr);
198 		jsonw_uint_field(json_wtr, "target_obj_id", info->tracing.target_obj_id);
199 		jsonw_uint_field(json_wtr, "target_btf_id", info->tracing.target_btf_id);
200 		break;
201 	case BPF_LINK_TYPE_CGROUP:
202 		jsonw_lluint_field(json_wtr, "cgroup_id",
203 				   info->cgroup.cgroup_id);
204 		show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
205 		break;
206 	case BPF_LINK_TYPE_ITER:
207 		show_iter_json(info, json_wtr);
208 		break;
209 	case BPF_LINK_TYPE_NETNS:
210 		jsonw_uint_field(json_wtr, "netns_ino",
211 				 info->netns.netns_ino);
212 		show_link_attach_type_json(info->netns.attach_type, json_wtr);
213 		break;
214 	case BPF_LINK_TYPE_NETFILTER:
215 		netfilter_dump_json(info, json_wtr);
216 		break;
217 	case BPF_LINK_TYPE_STRUCT_OPS:
218 		jsonw_uint_field(json_wtr, "map_id",
219 				 info->struct_ops.map_id);
220 		break;
221 	default:
222 		break;
223 	}
224 
225 	if (!hashmap__empty(link_table)) {
226 		struct hashmap_entry *entry;
227 
228 		jsonw_name(json_wtr, "pinned");
229 		jsonw_start_array(json_wtr);
230 		hashmap__for_each_key_entry(link_table, entry, info->id)
231 			jsonw_string(json_wtr, entry->pvalue);
232 		jsonw_end_array(json_wtr);
233 	}
234 
235 	emit_obj_refs_json(refs_table, info->id, json_wtr);
236 
237 	jsonw_end_object(json_wtr);
238 
239 	return 0;
240 }
241 
242 static void show_link_header_plain(struct bpf_link_info *info)
243 {
244 	const char *link_type_str;
245 
246 	printf("%u: ", info->id);
247 	link_type_str = libbpf_bpf_link_type_str(info->type);
248 	if (link_type_str)
249 		printf("%s  ", link_type_str);
250 	else
251 		printf("type %u  ", info->type);
252 
253 	if (info->type == BPF_LINK_TYPE_STRUCT_OPS)
254 		printf("map %u  ", info->struct_ops.map_id);
255 	else
256 		printf("prog %u  ", info->prog_id);
257 }
258 
259 static void show_link_attach_type_plain(__u32 attach_type)
260 {
261 	const char *attach_type_str;
262 
263 	attach_type_str = libbpf_bpf_attach_type_str(attach_type);
264 	if (attach_type_str)
265 		printf("attach_type %s  ", attach_type_str);
266 	else
267 		printf("attach_type %u  ", attach_type);
268 }
269 
270 static void show_iter_plain(struct bpf_link_info *info)
271 {
272 	const char *target_name = u64_to_ptr(info->iter.target_name);
273 
274 	printf("target_name %s  ", target_name);
275 
276 	if (is_iter_map_target(target_name))
277 		printf("map_id %u  ", info->iter.map.map_id);
278 	else if (is_iter_task_target(target_name)) {
279 		if (info->iter.task.tid)
280 			printf("tid %u ", info->iter.task.tid);
281 		else if (info->iter.task.pid)
282 			printf("pid %u ", info->iter.task.pid);
283 	}
284 
285 	if (is_iter_cgroup_target(target_name)) {
286 		printf("cgroup_id %llu  ", info->iter.cgroup.cgroup_id);
287 		printf("order %s  ",
288 		       cgroup_order_string(info->iter.cgroup.order));
289 	}
290 }
291 
292 static const char * const pf2name[] = {
293 	[NFPROTO_INET] = "inet",
294 	[NFPROTO_IPV4] = "ip",
295 	[NFPROTO_ARP] = "arp",
296 	[NFPROTO_NETDEV] = "netdev",
297 	[NFPROTO_BRIDGE] = "bridge",
298 	[NFPROTO_IPV6] = "ip6",
299 };
300 
301 static const char * const inethook2name[] = {
302 	[NF_INET_PRE_ROUTING] = "prerouting",
303 	[NF_INET_LOCAL_IN] = "input",
304 	[NF_INET_FORWARD] = "forward",
305 	[NF_INET_LOCAL_OUT] = "output",
306 	[NF_INET_POST_ROUTING] = "postrouting",
307 };
308 
309 static const char * const arphook2name[] = {
310 	[NF_ARP_IN] = "input",
311 	[NF_ARP_OUT] = "output",
312 };
313 
314 void netfilter_dump_plain(const struct bpf_link_info *info)
315 {
316 	const char *hookname = NULL, *pfname = NULL;
317 	unsigned int hook = info->netfilter.hooknum;
318 	unsigned int pf = info->netfilter.pf;
319 
320 	if (pf < ARRAY_SIZE(pf2name))
321 		pfname = pf2name[pf];
322 
323 	switch (pf) {
324 	case NFPROTO_BRIDGE: /* bridge shares numbers with enum nf_inet_hooks */
325 	case NFPROTO_IPV4:
326 	case NFPROTO_IPV6:
327 	case NFPROTO_INET:
328 		if (hook < ARRAY_SIZE(inethook2name))
329 			hookname = inethook2name[hook];
330 		break;
331 	case NFPROTO_ARP:
332 		if (hook < ARRAY_SIZE(arphook2name))
333 			hookname = arphook2name[hook];
334 	default:
335 		break;
336 	}
337 
338 	if (pfname)
339 		printf("\n\t%s", pfname);
340 	else
341 		printf("\n\tpf: %d", pf);
342 
343 	if (hookname)
344 		printf(" %s", hookname);
345 	else
346 		printf(", hook %u,", hook);
347 
348 	printf(" prio %d", info->netfilter.priority);
349 
350 	if (info->netfilter.flags)
351 		printf(" flags 0x%x", info->netfilter.flags);
352 }
353 
354 static int show_link_close_plain(int fd, struct bpf_link_info *info)
355 {
356 	struct bpf_prog_info prog_info;
357 	const char *prog_type_str;
358 	int err;
359 
360 	show_link_header_plain(info);
361 
362 	switch (info->type) {
363 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
364 		printf("\n\ttp '%s'  ",
365 		       (const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
366 		break;
367 	case BPF_LINK_TYPE_TRACING:
368 		err = get_prog_info(info->prog_id, &prog_info);
369 		if (err)
370 			return err;
371 
372 		prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
373 		/* libbpf will return NULL for variants unknown to it. */
374 		if (prog_type_str)
375 			printf("\n\tprog_type %s  ", prog_type_str);
376 		else
377 			printf("\n\tprog_type %u  ", prog_info.type);
378 
379 		show_link_attach_type_plain(info->tracing.attach_type);
380 		if (info->tracing.target_obj_id || info->tracing.target_btf_id)
381 			printf("\n\ttarget_obj_id %u  target_btf_id %u  ",
382 			       info->tracing.target_obj_id,
383 			       info->tracing.target_btf_id);
384 		break;
385 	case BPF_LINK_TYPE_CGROUP:
386 		printf("\n\tcgroup_id %zu  ", (size_t)info->cgroup.cgroup_id);
387 		show_link_attach_type_plain(info->cgroup.attach_type);
388 		break;
389 	case BPF_LINK_TYPE_ITER:
390 		show_iter_plain(info);
391 		break;
392 	case BPF_LINK_TYPE_NETNS:
393 		printf("\n\tnetns_ino %u  ", info->netns.netns_ino);
394 		show_link_attach_type_plain(info->netns.attach_type);
395 		break;
396 	case BPF_LINK_TYPE_NETFILTER:
397 		netfilter_dump_plain(info);
398 		break;
399 	default:
400 		break;
401 	}
402 
403 	if (!hashmap__empty(link_table)) {
404 		struct hashmap_entry *entry;
405 
406 		hashmap__for_each_key_entry(link_table, entry, info->id)
407 			printf("\n\tpinned %s", (char *)entry->pvalue);
408 	}
409 	emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
410 
411 	printf("\n");
412 
413 	return 0;
414 }
415 
416 static int do_show_link(int fd)
417 {
418 	struct bpf_link_info info;
419 	__u32 len = sizeof(info);
420 	char buf[256];
421 	int err;
422 
423 	memset(&info, 0, sizeof(info));
424 again:
425 	err = bpf_link_get_info_by_fd(fd, &info, &len);
426 	if (err) {
427 		p_err("can't get link info: %s",
428 		      strerror(errno));
429 		close(fd);
430 		return err;
431 	}
432 	if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
433 	    !info.raw_tracepoint.tp_name) {
434 		info.raw_tracepoint.tp_name = (unsigned long)&buf;
435 		info.raw_tracepoint.tp_name_len = sizeof(buf);
436 		goto again;
437 	}
438 	if (info.type == BPF_LINK_TYPE_ITER &&
439 	    !info.iter.target_name) {
440 		info.iter.target_name = (unsigned long)&buf;
441 		info.iter.target_name_len = sizeof(buf);
442 		goto again;
443 	}
444 
445 	if (json_output)
446 		show_link_close_json(fd, &info);
447 	else
448 		show_link_close_plain(fd, &info);
449 
450 	close(fd);
451 	return 0;
452 }
453 
454 static int do_show(int argc, char **argv)
455 {
456 	__u32 id = 0;
457 	int err, fd;
458 
459 	if (show_pinned) {
460 		link_table = hashmap__new(hash_fn_for_key_as_id,
461 					  equal_fn_for_key_as_id, NULL);
462 		if (IS_ERR(link_table)) {
463 			p_err("failed to create hashmap for pinned paths");
464 			return -1;
465 		}
466 		build_pinned_obj_table(link_table, BPF_OBJ_LINK);
467 	}
468 	build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
469 
470 	if (argc == 2) {
471 		fd = link_parse_fd(&argc, &argv);
472 		if (fd < 0)
473 			return fd;
474 		return do_show_link(fd);
475 	}
476 
477 	if (argc)
478 		return BAD_ARG();
479 
480 	if (json_output)
481 		jsonw_start_array(json_wtr);
482 	while (true) {
483 		err = bpf_link_get_next_id(id, &id);
484 		if (err) {
485 			if (errno == ENOENT)
486 				break;
487 			p_err("can't get next link: %s%s", strerror(errno),
488 			      errno == EINVAL ? " -- kernel too old?" : "");
489 			break;
490 		}
491 
492 		fd = bpf_link_get_fd_by_id(id);
493 		if (fd < 0) {
494 			if (errno == ENOENT)
495 				continue;
496 			p_err("can't get link by id (%u): %s",
497 			      id, strerror(errno));
498 			break;
499 		}
500 
501 		err = do_show_link(fd);
502 		if (err)
503 			break;
504 	}
505 	if (json_output)
506 		jsonw_end_array(json_wtr);
507 
508 	delete_obj_refs_table(refs_table);
509 
510 	if (show_pinned)
511 		delete_pinned_obj_table(link_table);
512 
513 	return errno == ENOENT ? 0 : -1;
514 }
515 
516 static int do_pin(int argc, char **argv)
517 {
518 	int err;
519 
520 	err = do_pin_any(argc, argv, link_parse_fd);
521 	if (!err && json_output)
522 		jsonw_null(json_wtr);
523 	return err;
524 }
525 
526 static int do_detach(int argc, char **argv)
527 {
528 	int err, fd;
529 
530 	if (argc != 2) {
531 		p_err("link specifier is invalid or missing\n");
532 		return 1;
533 	}
534 
535 	fd = link_parse_fd(&argc, &argv);
536 	if (fd < 0)
537 		return 1;
538 
539 	err = bpf_link_detach(fd);
540 	if (err)
541 		err = -errno;
542 	close(fd);
543 	if (err) {
544 		p_err("failed link detach: %s", strerror(-err));
545 		return 1;
546 	}
547 
548 	if (json_output)
549 		jsonw_null(json_wtr);
550 
551 	return 0;
552 }
553 
554 static int do_help(int argc, char **argv)
555 {
556 	if (json_output) {
557 		jsonw_null(json_wtr);
558 		return 0;
559 	}
560 
561 	fprintf(stderr,
562 		"Usage: %1$s %2$s { show | list }   [LINK]\n"
563 		"       %1$s %2$s pin        LINK  FILE\n"
564 		"       %1$s %2$s detach     LINK\n"
565 		"       %1$s %2$s help\n"
566 		"\n"
567 		"       " HELP_SPEC_LINK "\n"
568 		"       " HELP_SPEC_OPTIONS " |\n"
569 		"                    {-f|--bpffs} | {-n|--nomount} }\n"
570 		"",
571 		bin_name, argv[-2]);
572 
573 	return 0;
574 }
575 
576 static const struct cmd cmds[] = {
577 	{ "show",	do_show },
578 	{ "list",	do_show },
579 	{ "help",	do_help },
580 	{ "pin",	do_pin },
581 	{ "detach",	do_detach },
582 	{ 0 }
583 };
584 
585 int do_link(int argc, char **argv)
586 {
587 	return cmd_select(cmds, argc, argv, do_help);
588 }
589