xref: /linux/tools/bpf/bpftool/feature.c (revision 05ee19c18c2bb3dea69e29219017367c4a77e65a)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (c) 2019 Netronome Systems, Inc. */
3 
4 #include <ctype.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <net/if.h>
9 #ifdef USE_LIBCAP
10 #include <sys/capability.h>
11 #endif
12 #include <sys/utsname.h>
13 #include <sys/vfs.h>
14 
15 #include <linux/filter.h>
16 #include <linux/limits.h>
17 
18 #include <bpf/bpf.h>
19 #include <bpf/libbpf.h>
20 #include <zlib.h>
21 
22 #include "main.h"
23 
24 #ifndef PROC_SUPER_MAGIC
25 # define PROC_SUPER_MAGIC	0x9fa0
26 #endif
27 
28 enum probe_component {
29 	COMPONENT_UNSPEC,
30 	COMPONENT_KERNEL,
31 	COMPONENT_DEVICE,
32 };
33 
34 #define BPF_HELPER_MAKE_ENTRY(name)	[BPF_FUNC_ ## name] = "bpf_" # name
35 static const char * const helper_name[] = {
36 	__BPF_FUNC_MAPPER(BPF_HELPER_MAKE_ENTRY)
37 };
38 
39 #undef BPF_HELPER_MAKE_ENTRY
40 
41 static bool full_mode;
42 #ifdef USE_LIBCAP
43 static bool run_as_unprivileged;
44 #endif
45 
46 /* Miscellaneous utility functions */
47 
48 static bool check_procfs(void)
49 {
50 	struct statfs st_fs;
51 
52 	if (statfs("/proc", &st_fs) < 0)
53 		return false;
54 	if ((unsigned long)st_fs.f_type != PROC_SUPER_MAGIC)
55 		return false;
56 
57 	return true;
58 }
59 
60 static void uppercase(char *str, size_t len)
61 {
62 	size_t i;
63 
64 	for (i = 0; i < len && str[i] != '\0'; i++)
65 		str[i] = toupper(str[i]);
66 }
67 
68 /* Printing utility functions */
69 
70 static void
71 print_bool_feature(const char *feat_name, const char *plain_name,
72 		   const char *define_name, bool res, const char *define_prefix)
73 {
74 	if (json_output)
75 		jsonw_bool_field(json_wtr, feat_name, res);
76 	else if (define_prefix)
77 		printf("#define %s%sHAVE_%s\n", define_prefix,
78 		       res ? "" : "NO_", define_name);
79 	else
80 		printf("%s is %savailable\n", plain_name, res ? "" : "NOT ");
81 }
82 
83 static void print_kernel_option(const char *name, const char *value,
84 				const char *define_prefix)
85 {
86 	char *endptr;
87 	int res;
88 
89 	if (json_output) {
90 		if (!value) {
91 			jsonw_null_field(json_wtr, name);
92 			return;
93 		}
94 		errno = 0;
95 		res = strtol(value, &endptr, 0);
96 		if (!errno && *endptr == '\n')
97 			jsonw_int_field(json_wtr, name, res);
98 		else
99 			jsonw_string_field(json_wtr, name, value);
100 	} else if (define_prefix) {
101 		if (value)
102 			printf("#define %s%s %s\n", define_prefix,
103 			       name, value);
104 		else
105 			printf("/* %s%s is not set */\n", define_prefix, name);
106 	} else {
107 		if (value)
108 			printf("%s is set to %s\n", name, value);
109 		else
110 			printf("%s is not set\n", name);
111 	}
112 }
113 
114 static void
115 print_start_section(const char *json_title, const char *plain_title,
116 		    const char *define_comment, const char *define_prefix)
117 {
118 	if (json_output) {
119 		jsonw_name(json_wtr, json_title);
120 		jsonw_start_object(json_wtr);
121 	} else if (define_prefix) {
122 		printf("%s\n", define_comment);
123 	} else {
124 		printf("%s\n", plain_title);
125 	}
126 }
127 
128 static void print_end_section(void)
129 {
130 	if (json_output)
131 		jsonw_end_object(json_wtr);
132 	else
133 		printf("\n");
134 }
135 
136 /* Probing functions */
137 
138 static int read_procfs(const char *path)
139 {
140 	char *endptr, *line = NULL;
141 	size_t len = 0;
142 	FILE *fd;
143 	int res;
144 
145 	fd = fopen(path, "r");
146 	if (!fd)
147 		return -1;
148 
149 	res = getline(&line, &len, fd);
150 	fclose(fd);
151 	if (res < 0)
152 		return -1;
153 
154 	errno = 0;
155 	res = strtol(line, &endptr, 10);
156 	if (errno || *line == '\0' || *endptr != '\n')
157 		res = -1;
158 	free(line);
159 
160 	return res;
161 }
162 
163 static void probe_unprivileged_disabled(void)
164 {
165 	int res;
166 
167 	/* No support for C-style ouptut */
168 
169 	res = read_procfs("/proc/sys/kernel/unprivileged_bpf_disabled");
170 	if (json_output) {
171 		jsonw_int_field(json_wtr, "unprivileged_bpf_disabled", res);
172 	} else {
173 		switch (res) {
174 		case 0:
175 			printf("bpf() syscall for unprivileged users is enabled\n");
176 			break;
177 		case 1:
178 			printf("bpf() syscall restricted to privileged users\n");
179 			break;
180 		case -1:
181 			printf("Unable to retrieve required privileges for bpf() syscall\n");
182 			break;
183 		default:
184 			printf("bpf() syscall restriction has unknown value %d\n", res);
185 		}
186 	}
187 }
188 
189 static void probe_jit_enable(void)
190 {
191 	int res;
192 
193 	/* No support for C-style ouptut */
194 
195 	res = read_procfs("/proc/sys/net/core/bpf_jit_enable");
196 	if (json_output) {
197 		jsonw_int_field(json_wtr, "bpf_jit_enable", res);
198 	} else {
199 		switch (res) {
200 		case 0:
201 			printf("JIT compiler is disabled\n");
202 			break;
203 		case 1:
204 			printf("JIT compiler is enabled\n");
205 			break;
206 		case 2:
207 			printf("JIT compiler is enabled with debugging traces in kernel logs\n");
208 			break;
209 		case -1:
210 			printf("Unable to retrieve JIT-compiler status\n");
211 			break;
212 		default:
213 			printf("JIT-compiler status has unknown value %d\n",
214 			       res);
215 		}
216 	}
217 }
218 
219 static void probe_jit_harden(void)
220 {
221 	int res;
222 
223 	/* No support for C-style ouptut */
224 
225 	res = read_procfs("/proc/sys/net/core/bpf_jit_harden");
226 	if (json_output) {
227 		jsonw_int_field(json_wtr, "bpf_jit_harden", res);
228 	} else {
229 		switch (res) {
230 		case 0:
231 			printf("JIT compiler hardening is disabled\n");
232 			break;
233 		case 1:
234 			printf("JIT compiler hardening is enabled for unprivileged users\n");
235 			break;
236 		case 2:
237 			printf("JIT compiler hardening is enabled for all users\n");
238 			break;
239 		case -1:
240 			printf("Unable to retrieve JIT hardening status\n");
241 			break;
242 		default:
243 			printf("JIT hardening status has unknown value %d\n",
244 			       res);
245 		}
246 	}
247 }
248 
249 static void probe_jit_kallsyms(void)
250 {
251 	int res;
252 
253 	/* No support for C-style ouptut */
254 
255 	res = read_procfs("/proc/sys/net/core/bpf_jit_kallsyms");
256 	if (json_output) {
257 		jsonw_int_field(json_wtr, "bpf_jit_kallsyms", res);
258 	} else {
259 		switch (res) {
260 		case 0:
261 			printf("JIT compiler kallsyms exports are disabled\n");
262 			break;
263 		case 1:
264 			printf("JIT compiler kallsyms exports are enabled for root\n");
265 			break;
266 		case -1:
267 			printf("Unable to retrieve JIT kallsyms export status\n");
268 			break;
269 		default:
270 			printf("JIT kallsyms exports status has unknown value %d\n", res);
271 		}
272 	}
273 }
274 
275 static void probe_jit_limit(void)
276 {
277 	int res;
278 
279 	/* No support for C-style ouptut */
280 
281 	res = read_procfs("/proc/sys/net/core/bpf_jit_limit");
282 	if (json_output) {
283 		jsonw_int_field(json_wtr, "bpf_jit_limit", res);
284 	} else {
285 		switch (res) {
286 		case -1:
287 			printf("Unable to retrieve global memory limit for JIT compiler for unprivileged users\n");
288 			break;
289 		default:
290 			printf("Global memory limit for JIT compiler for unprivileged users is %d bytes\n", res);
291 		}
292 	}
293 }
294 
295 static bool read_next_kernel_config_option(gzFile file, char *buf, size_t n,
296 					   char **value)
297 {
298 	char *sep;
299 
300 	while (gzgets(file, buf, n)) {
301 		if (strncmp(buf, "CONFIG_", 7))
302 			continue;
303 
304 		sep = strchr(buf, '=');
305 		if (!sep)
306 			continue;
307 
308 		/* Trim ending '\n' */
309 		buf[strlen(buf) - 1] = '\0';
310 
311 		/* Split on '=' and ensure that a value is present. */
312 		*sep = '\0';
313 		if (!sep[1])
314 			continue;
315 
316 		*value = sep + 1;
317 		return true;
318 	}
319 
320 	return false;
321 }
322 
323 static void probe_kernel_image_config(const char *define_prefix)
324 {
325 	static const struct {
326 		const char * const name;
327 		bool macro_dump;
328 	} options[] = {
329 		/* Enable BPF */
330 		{ "CONFIG_BPF", },
331 		/* Enable bpf() syscall */
332 		{ "CONFIG_BPF_SYSCALL", },
333 		/* Does selected architecture support eBPF JIT compiler */
334 		{ "CONFIG_HAVE_EBPF_JIT", },
335 		/* Compile eBPF JIT compiler */
336 		{ "CONFIG_BPF_JIT", },
337 		/* Avoid compiling eBPF interpreter (use JIT only) */
338 		{ "CONFIG_BPF_JIT_ALWAYS_ON", },
339 
340 		/* cgroups */
341 		{ "CONFIG_CGROUPS", },
342 		/* BPF programs attached to cgroups */
343 		{ "CONFIG_CGROUP_BPF", },
344 		/* bpf_get_cgroup_classid() helper */
345 		{ "CONFIG_CGROUP_NET_CLASSID", },
346 		/* bpf_skb_{,ancestor_}cgroup_id() helpers */
347 		{ "CONFIG_SOCK_CGROUP_DATA", },
348 
349 		/* Tracing: attach BPF to kprobes, tracepoints, etc. */
350 		{ "CONFIG_BPF_EVENTS", },
351 		/* Kprobes */
352 		{ "CONFIG_KPROBE_EVENTS", },
353 		/* Uprobes */
354 		{ "CONFIG_UPROBE_EVENTS", },
355 		/* Tracepoints */
356 		{ "CONFIG_TRACING", },
357 		/* Syscall tracepoints */
358 		{ "CONFIG_FTRACE_SYSCALLS", },
359 		/* bpf_override_return() helper support for selected arch */
360 		{ "CONFIG_FUNCTION_ERROR_INJECTION", },
361 		/* bpf_override_return() helper */
362 		{ "CONFIG_BPF_KPROBE_OVERRIDE", },
363 
364 		/* Network */
365 		{ "CONFIG_NET", },
366 		/* AF_XDP sockets */
367 		{ "CONFIG_XDP_SOCKETS", },
368 		/* BPF_PROG_TYPE_LWT_* and related helpers */
369 		{ "CONFIG_LWTUNNEL_BPF", },
370 		/* BPF_PROG_TYPE_SCHED_ACT, TC (traffic control) actions */
371 		{ "CONFIG_NET_ACT_BPF", },
372 		/* BPF_PROG_TYPE_SCHED_CLS, TC filters */
373 		{ "CONFIG_NET_CLS_BPF", },
374 		/* TC clsact qdisc */
375 		{ "CONFIG_NET_CLS_ACT", },
376 		/* Ingress filtering with TC */
377 		{ "CONFIG_NET_SCH_INGRESS", },
378 		/* bpf_skb_get_xfrm_state() helper */
379 		{ "CONFIG_XFRM", },
380 		/* bpf_get_route_realm() helper */
381 		{ "CONFIG_IP_ROUTE_CLASSID", },
382 		/* BPF_PROG_TYPE_LWT_SEG6_LOCAL and related helpers */
383 		{ "CONFIG_IPV6_SEG6_BPF", },
384 		/* BPF_PROG_TYPE_LIRC_MODE2 and related helpers */
385 		{ "CONFIG_BPF_LIRC_MODE2", },
386 		/* BPF stream parser and BPF socket maps */
387 		{ "CONFIG_BPF_STREAM_PARSER", },
388 		/* xt_bpf module for passing BPF programs to netfilter  */
389 		{ "CONFIG_NETFILTER_XT_MATCH_BPF", },
390 		/* bpfilter back-end for iptables */
391 		{ "CONFIG_BPFILTER", },
392 		/* bpftilter module with "user mode helper" */
393 		{ "CONFIG_BPFILTER_UMH", },
394 
395 		/* test_bpf module for BPF tests */
396 		{ "CONFIG_TEST_BPF", },
397 
398 		/* Misc configs useful in BPF C programs */
399 		/* jiffies <-> sec conversion for bpf_jiffies64() helper */
400 		{ "CONFIG_HZ", true, }
401 	};
402 	char *values[ARRAY_SIZE(options)] = { };
403 	struct utsname utsn;
404 	char path[PATH_MAX];
405 	gzFile file = NULL;
406 	char buf[4096];
407 	char *value;
408 	size_t i;
409 
410 	if (!uname(&utsn)) {
411 		snprintf(path, sizeof(path), "/boot/config-%s", utsn.release);
412 
413 		/* gzopen also accepts uncompressed files. */
414 		file = gzopen(path, "r");
415 	}
416 
417 	if (!file) {
418 		/* Some distributions build with CONFIG_IKCONFIG=y and put the
419 		 * config file at /proc/config.gz.
420 		 */
421 		file = gzopen("/proc/config.gz", "r");
422 	}
423 	if (!file) {
424 		p_info("skipping kernel config, can't open file: %s",
425 		       strerror(errno));
426 		goto end_parse;
427 	}
428 	/* Sanity checks */
429 	if (!gzgets(file, buf, sizeof(buf)) ||
430 	    !gzgets(file, buf, sizeof(buf))) {
431 		p_info("skipping kernel config, can't read from file: %s",
432 		       strerror(errno));
433 		goto end_parse;
434 	}
435 	if (strcmp(buf, "# Automatically generated file; DO NOT EDIT.\n")) {
436 		p_info("skipping kernel config, can't find correct file");
437 		goto end_parse;
438 	}
439 
440 	while (read_next_kernel_config_option(file, buf, sizeof(buf), &value)) {
441 		for (i = 0; i < ARRAY_SIZE(options); i++) {
442 			if ((define_prefix && !options[i].macro_dump) ||
443 			    values[i] || strcmp(buf, options[i].name))
444 				continue;
445 
446 			values[i] = strdup(value);
447 		}
448 	}
449 
450 end_parse:
451 	if (file)
452 		gzclose(file);
453 
454 	for (i = 0; i < ARRAY_SIZE(options); i++) {
455 		if (define_prefix && !options[i].macro_dump)
456 			continue;
457 		print_kernel_option(options[i].name, values[i], define_prefix);
458 		free(values[i]);
459 	}
460 }
461 
462 static bool probe_bpf_syscall(const char *define_prefix)
463 {
464 	bool res;
465 
466 	bpf_load_program(BPF_PROG_TYPE_UNSPEC, NULL, 0, NULL, 0, NULL, 0);
467 	res = (errno != ENOSYS);
468 
469 	print_bool_feature("have_bpf_syscall",
470 			   "bpf() syscall",
471 			   "BPF_SYSCALL",
472 			   res, define_prefix);
473 
474 	return res;
475 }
476 
477 static void
478 probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types,
479 		const char *define_prefix, __u32 ifindex)
480 {
481 	char feat_name[128], plain_desc[128], define_name[128];
482 	const char *plain_comment = "eBPF program_type ";
483 	size_t maxlen;
484 	bool res;
485 
486 	if (ifindex)
487 		/* Only test offload-able program types */
488 		switch (prog_type) {
489 		case BPF_PROG_TYPE_SCHED_CLS:
490 		case BPF_PROG_TYPE_XDP:
491 			break;
492 		default:
493 			return;
494 		}
495 
496 	res = bpf_probe_prog_type(prog_type, ifindex);
497 #ifdef USE_LIBCAP
498 	/* Probe may succeed even if program load fails, for unprivileged users
499 	 * check that we did not fail because of insufficient permissions
500 	 */
501 	if (run_as_unprivileged && errno == EPERM)
502 		res = false;
503 #endif
504 
505 	supported_types[prog_type] |= res;
506 
507 	maxlen = sizeof(plain_desc) - strlen(plain_comment) - 1;
508 	if (strlen(prog_type_name[prog_type]) > maxlen) {
509 		p_info("program type name too long");
510 		return;
511 	}
512 
513 	sprintf(feat_name, "have_%s_prog_type", prog_type_name[prog_type]);
514 	sprintf(define_name, "%s_prog_type", prog_type_name[prog_type]);
515 	uppercase(define_name, sizeof(define_name));
516 	sprintf(plain_desc, "%s%s", plain_comment, prog_type_name[prog_type]);
517 	print_bool_feature(feat_name, plain_desc, define_name, res,
518 			   define_prefix);
519 }
520 
521 static void
522 probe_map_type(enum bpf_map_type map_type, const char *define_prefix,
523 	       __u32 ifindex)
524 {
525 	char feat_name[128], plain_desc[128], define_name[128];
526 	const char *plain_comment = "eBPF map_type ";
527 	size_t maxlen;
528 	bool res;
529 
530 	res = bpf_probe_map_type(map_type, ifindex);
531 
532 	/* Probe result depends on the success of map creation, no additional
533 	 * check required for unprivileged users
534 	 */
535 
536 	maxlen = sizeof(plain_desc) - strlen(plain_comment) - 1;
537 	if (strlen(map_type_name[map_type]) > maxlen) {
538 		p_info("map type name too long");
539 		return;
540 	}
541 
542 	sprintf(feat_name, "have_%s_map_type", map_type_name[map_type]);
543 	sprintf(define_name, "%s_map_type", map_type_name[map_type]);
544 	uppercase(define_name, sizeof(define_name));
545 	sprintf(plain_desc, "%s%s", plain_comment, map_type_name[map_type]);
546 	print_bool_feature(feat_name, plain_desc, define_name, res,
547 			   define_prefix);
548 }
549 
550 static void
551 probe_helper_for_progtype(enum bpf_prog_type prog_type, bool supported_type,
552 			  const char *define_prefix, unsigned int id,
553 			  const char *ptype_name, __u32 ifindex)
554 {
555 	bool res = false;
556 
557 	if (supported_type) {
558 		res = bpf_probe_helper(id, prog_type, ifindex);
559 #ifdef USE_LIBCAP
560 		/* Probe may succeed even if program load fails, for
561 		 * unprivileged users check that we did not fail because of
562 		 * insufficient permissions
563 		 */
564 		if (run_as_unprivileged && errno == EPERM)
565 			res = false;
566 #endif
567 	}
568 
569 	if (json_output) {
570 		if (res)
571 			jsonw_string(json_wtr, helper_name[id]);
572 	} else if (define_prefix) {
573 		printf("#define %sBPF__PROG_TYPE_%s__HELPER_%s %s\n",
574 		       define_prefix, ptype_name, helper_name[id],
575 		       res ? "1" : "0");
576 	} else {
577 		if (res)
578 			printf("\n\t- %s", helper_name[id]);
579 	}
580 }
581 
582 static void
583 probe_helpers_for_progtype(enum bpf_prog_type prog_type, bool supported_type,
584 			   const char *define_prefix, __u32 ifindex)
585 {
586 	const char *ptype_name = prog_type_name[prog_type];
587 	char feat_name[128];
588 	unsigned int id;
589 
590 	if (ifindex)
591 		/* Only test helpers for offload-able program types */
592 		switch (prog_type) {
593 		case BPF_PROG_TYPE_SCHED_CLS:
594 		case BPF_PROG_TYPE_XDP:
595 			break;
596 		default:
597 			return;
598 		}
599 
600 	if (json_output) {
601 		sprintf(feat_name, "%s_available_helpers", ptype_name);
602 		jsonw_name(json_wtr, feat_name);
603 		jsonw_start_array(json_wtr);
604 	} else if (!define_prefix) {
605 		printf("eBPF helpers supported for program type %s:",
606 		       ptype_name);
607 	}
608 
609 	for (id = 1; id < ARRAY_SIZE(helper_name); id++) {
610 		/* Skip helper functions which emit dmesg messages when not in
611 		 * the full mode.
612 		 */
613 		switch (id) {
614 		case BPF_FUNC_trace_printk:
615 		case BPF_FUNC_probe_write_user:
616 			if (!full_mode)
617 				continue;
618 			/* fallthrough */
619 		default:
620 			probe_helper_for_progtype(prog_type, supported_type,
621 						  define_prefix, id, ptype_name,
622 						  ifindex);
623 		}
624 	}
625 
626 	if (json_output)
627 		jsonw_end_array(json_wtr);
628 	else if (!define_prefix)
629 		printf("\n");
630 }
631 
632 static void
633 probe_large_insn_limit(const char *define_prefix, __u32 ifindex)
634 {
635 	bool res;
636 
637 	res = bpf_probe_large_insn_limit(ifindex);
638 	print_bool_feature("have_large_insn_limit",
639 			   "Large program size limit",
640 			   "LARGE_INSN_LIMIT",
641 			   res, define_prefix);
642 }
643 
644 static void
645 section_system_config(enum probe_component target, const char *define_prefix)
646 {
647 	switch (target) {
648 	case COMPONENT_KERNEL:
649 	case COMPONENT_UNSPEC:
650 		print_start_section("system_config",
651 				    "Scanning system configuration...",
652 				    "/*** Misc kernel config items ***/",
653 				    define_prefix);
654 		if (!define_prefix) {
655 			if (check_procfs()) {
656 				probe_unprivileged_disabled();
657 				probe_jit_enable();
658 				probe_jit_harden();
659 				probe_jit_kallsyms();
660 				probe_jit_limit();
661 			} else {
662 				p_info("/* procfs not mounted, skipping related probes */");
663 			}
664 		}
665 		probe_kernel_image_config(define_prefix);
666 		print_end_section();
667 		break;
668 	default:
669 		break;
670 	}
671 }
672 
673 static bool section_syscall_config(const char *define_prefix)
674 {
675 	bool res;
676 
677 	print_start_section("syscall_config",
678 			    "Scanning system call availability...",
679 			    "/*** System call availability ***/",
680 			    define_prefix);
681 	res = probe_bpf_syscall(define_prefix);
682 	print_end_section();
683 
684 	return res;
685 }
686 
687 static void
688 section_program_types(bool *supported_types, const char *define_prefix,
689 		      __u32 ifindex)
690 {
691 	unsigned int i;
692 
693 	print_start_section("program_types",
694 			    "Scanning eBPF program types...",
695 			    "/*** eBPF program types ***/",
696 			    define_prefix);
697 
698 	for (i = BPF_PROG_TYPE_UNSPEC + 1; i < ARRAY_SIZE(prog_type_name); i++)
699 		probe_prog_type(i, supported_types, define_prefix, ifindex);
700 
701 	print_end_section();
702 }
703 
704 static void section_map_types(const char *define_prefix, __u32 ifindex)
705 {
706 	unsigned int i;
707 
708 	print_start_section("map_types",
709 			    "Scanning eBPF map types...",
710 			    "/*** eBPF map types ***/",
711 			    define_prefix);
712 
713 	for (i = BPF_MAP_TYPE_UNSPEC + 1; i < map_type_name_size; i++)
714 		probe_map_type(i, define_prefix, ifindex);
715 
716 	print_end_section();
717 }
718 
719 static void
720 section_helpers(bool *supported_types, const char *define_prefix, __u32 ifindex)
721 {
722 	unsigned int i;
723 
724 	print_start_section("helpers",
725 			    "Scanning eBPF helper functions...",
726 			    "/*** eBPF helper functions ***/",
727 			    define_prefix);
728 
729 	if (define_prefix)
730 		printf("/*\n"
731 		       " * Use %sHAVE_PROG_TYPE_HELPER(prog_type_name, helper_name)\n"
732 		       " * to determine if <helper_name> is available for <prog_type_name>,\n"
733 		       " * e.g.\n"
734 		       " *	#if %sHAVE_PROG_TYPE_HELPER(xdp, bpf_redirect)\n"
735 		       " *		// do stuff with this helper\n"
736 		       " *	#elif\n"
737 		       " *		// use a workaround\n"
738 		       " *	#endif\n"
739 		       " */\n"
740 		       "#define %sHAVE_PROG_TYPE_HELPER(prog_type, helper)	\\\n"
741 		       "	%sBPF__PROG_TYPE_ ## prog_type ## __HELPER_ ## helper\n",
742 		       define_prefix, define_prefix, define_prefix,
743 		       define_prefix);
744 	for (i = BPF_PROG_TYPE_UNSPEC + 1; i < ARRAY_SIZE(prog_type_name); i++)
745 		probe_helpers_for_progtype(i, supported_types[i], define_prefix,
746 					   ifindex);
747 
748 	print_end_section();
749 }
750 
751 static void section_misc(const char *define_prefix, __u32 ifindex)
752 {
753 	print_start_section("misc",
754 			    "Scanning miscellaneous eBPF features...",
755 			    "/*** eBPF misc features ***/",
756 			    define_prefix);
757 	probe_large_insn_limit(define_prefix, ifindex);
758 	print_end_section();
759 }
760 
761 static int handle_perms(void)
762 {
763 #ifdef USE_LIBCAP
764 	cap_value_t cap_list[1] = { CAP_SYS_ADMIN };
765 	bool has_sys_admin_cap = false;
766 	cap_flag_value_t val;
767 	int res = -1;
768 	cap_t caps;
769 
770 	caps = cap_get_proc();
771 	if (!caps) {
772 		p_err("failed to get capabilities for process: %s",
773 		      strerror(errno));
774 		return -1;
775 	}
776 
777 	if (cap_get_flag(caps, CAP_SYS_ADMIN, CAP_EFFECTIVE, &val)) {
778 		p_err("bug: failed to retrieve CAP_SYS_ADMIN status");
779 		goto exit_free;
780 	}
781 	if (val == CAP_SET)
782 		has_sys_admin_cap = true;
783 
784 	if (!run_as_unprivileged && !has_sys_admin_cap) {
785 		p_err("full feature probing requires CAP_SYS_ADMIN, run as root or use 'unprivileged'");
786 		goto exit_free;
787 	}
788 
789 	if ((run_as_unprivileged && !has_sys_admin_cap) ||
790 	    (!run_as_unprivileged && has_sys_admin_cap)) {
791 		/* We are all good, exit now */
792 		res = 0;
793 		goto exit_free;
794 	}
795 
796 	/* if (run_as_unprivileged && has_sys_admin_cap), drop CAP_SYS_ADMIN */
797 
798 	if (cap_set_flag(caps, CAP_EFFECTIVE, ARRAY_SIZE(cap_list), cap_list,
799 			 CAP_CLEAR)) {
800 		p_err("bug: failed to clear CAP_SYS_ADMIN from capabilities");
801 		goto exit_free;
802 	}
803 
804 	if (cap_set_proc(caps)) {
805 		p_err("failed to drop CAP_SYS_ADMIN: %s", strerror(errno));
806 		goto exit_free;
807 	}
808 
809 	res = 0;
810 
811 exit_free:
812 	if (cap_free(caps) && !res) {
813 		p_err("failed to clear storage object for capabilities: %s",
814 		      strerror(errno));
815 		res = -1;
816 	}
817 
818 	return res;
819 #else
820 	/* Detection assumes user has sufficient privileges (CAP_SYS_ADMIN).
821 	 * We do not use libpcap so let's approximate, and restrict usage to
822 	 * root user only.
823 	 */
824 	if (geteuid()) {
825 		p_err("full feature probing requires root privileges");
826 		return -1;
827 	}
828 
829 	return 0;
830 #endif /* USE_LIBCAP */
831 }
832 
833 static int do_probe(int argc, char **argv)
834 {
835 	enum probe_component target = COMPONENT_UNSPEC;
836 	const char *define_prefix = NULL;
837 	bool supported_types[128] = {};
838 	__u32 ifindex = 0;
839 	char *ifname;
840 
841 	set_max_rlimit();
842 
843 	while (argc) {
844 		if (is_prefix(*argv, "kernel")) {
845 			if (target != COMPONENT_UNSPEC) {
846 				p_err("component to probe already specified");
847 				return -1;
848 			}
849 			target = COMPONENT_KERNEL;
850 			NEXT_ARG();
851 		} else if (is_prefix(*argv, "dev")) {
852 			NEXT_ARG();
853 
854 			if (target != COMPONENT_UNSPEC || ifindex) {
855 				p_err("component to probe already specified");
856 				return -1;
857 			}
858 			if (!REQ_ARGS(1))
859 				return -1;
860 
861 			target = COMPONENT_DEVICE;
862 			ifname = GET_ARG();
863 			ifindex = if_nametoindex(ifname);
864 			if (!ifindex) {
865 				p_err("unrecognized netdevice '%s': %s", ifname,
866 				      strerror(errno));
867 				return -1;
868 			}
869 		} else if (is_prefix(*argv, "full")) {
870 			full_mode = true;
871 			NEXT_ARG();
872 		} else if (is_prefix(*argv, "macros") && !define_prefix) {
873 			define_prefix = "";
874 			NEXT_ARG();
875 		} else if (is_prefix(*argv, "prefix")) {
876 			if (!define_prefix) {
877 				p_err("'prefix' argument can only be use after 'macros'");
878 				return -1;
879 			}
880 			if (strcmp(define_prefix, "")) {
881 				p_err("'prefix' already defined");
882 				return -1;
883 			}
884 			NEXT_ARG();
885 
886 			if (!REQ_ARGS(1))
887 				return -1;
888 			define_prefix = GET_ARG();
889 		} else if (is_prefix(*argv, "unprivileged")) {
890 #ifdef USE_LIBCAP
891 			run_as_unprivileged = true;
892 			NEXT_ARG();
893 #else
894 			p_err("unprivileged run not supported, recompile bpftool with libcap");
895 			return -1;
896 #endif
897 		} else {
898 			p_err("expected no more arguments, 'kernel', 'dev', 'macros' or 'prefix', got: '%s'?",
899 			      *argv);
900 			return -1;
901 		}
902 	}
903 
904 	/* Full feature detection requires CAP_SYS_ADMIN privilege.
905 	 * Let's approximate, and warn if user is not root.
906 	 */
907 	if (handle_perms())
908 		return -1;
909 
910 	if (json_output) {
911 		define_prefix = NULL;
912 		jsonw_start_object(json_wtr);
913 	}
914 
915 	section_system_config(target, define_prefix);
916 	if (!section_syscall_config(define_prefix))
917 		/* bpf() syscall unavailable, don't probe other BPF features */
918 		goto exit_close_json;
919 	section_program_types(supported_types, define_prefix, ifindex);
920 	section_map_types(define_prefix, ifindex);
921 	section_helpers(supported_types, define_prefix, ifindex);
922 	section_misc(define_prefix, ifindex);
923 
924 exit_close_json:
925 	if (json_output)
926 		/* End root object */
927 		jsonw_end_object(json_wtr);
928 
929 	return 0;
930 }
931 
932 static int do_help(int argc, char **argv)
933 {
934 	if (json_output) {
935 		jsonw_null(json_wtr);
936 		return 0;
937 	}
938 
939 	fprintf(stderr,
940 		"Usage: %s %s probe [COMPONENT] [full] [unprivileged] [macros [prefix PREFIX]]\n"
941 		"       %s %s help\n"
942 		"\n"
943 		"       COMPONENT := { kernel | dev NAME }\n"
944 		"",
945 		bin_name, argv[-2], bin_name, argv[-2]);
946 
947 	return 0;
948 }
949 
950 static const struct cmd cmds[] = {
951 	{ "probe",	do_probe },
952 	{ "help",	do_help },
953 	{ 0 }
954 };
955 
956 int do_feature(int argc, char **argv)
957 {
958 	return cmd_select(cmds, argc, argv, do_help);
959 }
960