1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2020 Facebook */
6 #include <linux/netfilter.h>
7 #include <linux/netfilter_arp.h>
13 #include <bpf/hashmap.h>
15 #include "json_writer.h"
18 static struct hashmap *link_table;
20 static int link_parse_fd(int *argc, char ***argv)
24 if (is_prefix(**argv, "id")) {
30 id = strtoul(**argv, &endptr, 0);
32 p_err("can't parse %s as ID", **argv);
37 fd = bpf_link_get_fd_by_id(id);
39 p_err("failed to get link with ID %d: %s", id, strerror(errno));
41 } else if (is_prefix(**argv, "pinned")) {
49 return open_obj_pinned_any(path, BPF_OBJ_LINK);
52 p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
57 show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
59 const char *link_type_str;
61 jsonw_uint_field(wtr, "id", info->id);
62 link_type_str = libbpf_bpf_link_type_str(info->type);
64 jsonw_string_field(wtr, "type", link_type_str);
66 jsonw_uint_field(wtr, "type", info->type);
68 jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
71 static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
73 const char *attach_type_str;
75 attach_type_str = libbpf_bpf_attach_type_str(attach_type);
77 jsonw_string_field(wtr, "attach_type", attach_type_str);
79 jsonw_uint_field(wtr, "attach_type", attach_type);
82 static bool is_iter_map_target(const char *target_name)
84 return strcmp(target_name, "bpf_map_elem") == 0 ||
85 strcmp(target_name, "bpf_sk_storage_map") == 0;
88 static bool is_iter_cgroup_target(const char *target_name)
90 return strcmp(target_name, "cgroup") == 0;
93 static const char *cgroup_order_string(__u32 order)
96 case BPF_CGROUP_ITER_ORDER_UNSPEC:
97 return "order_unspec";
98 case BPF_CGROUP_ITER_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 */
111 static bool is_iter_task_target(const char *target_name)
113 return strcmp(target_name, "task") == 0 ||
114 strcmp(target_name, "task_file") == 0 ||
115 strcmp(target_name, "task_vma") == 0;
118 static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
120 const char *target_name = u64_to_ptr(info->iter.target_name);
122 jsonw_string_field(wtr, "target_name", target_name);
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);
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));
140 void netfilter_dump_json(const struct bpf_link_info *info, json_writer_t *wtr)
142 jsonw_uint_field(json_wtr, "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);
152 static int get_prog_info(int prog_id, struct bpf_prog_info *info)
154 __u32 len = sizeof(*info);
157 prog_fd = bpf_prog_get_fd_by_id(prog_id);
161 memset(info, 0, sizeof(*info));
162 err = bpf_prog_get_info_by_fd(prog_fd, info, &len);
164 p_err("can't get prog info: %s", strerror(errno));
169 static int show_link_close_json(int fd, struct bpf_link_info *info)
171 struct bpf_prog_info prog_info;
172 const char *prog_type_str;
175 jsonw_start_object(json_wtr);
177 show_link_header_json(info, json_wtr);
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));
184 case BPF_LINK_TYPE_TRACING:
185 err = get_prog_info(info->prog_id, &prog_info);
189 prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
190 /* libbpf will return NULL for variants unknown to it. */
192 jsonw_string_field(json_wtr, "prog_type", prog_type_str);
194 jsonw_uint_field(json_wtr, "prog_type", prog_info.type);
196 show_link_attach_type_json(info->tracing.attach_type,
199 case BPF_LINK_TYPE_CGROUP:
200 jsonw_lluint_field(json_wtr, "cgroup_id",
201 info->cgroup.cgroup_id);
202 show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
204 case BPF_LINK_TYPE_ITER:
205 show_iter_json(info, json_wtr);
207 case BPF_LINK_TYPE_NETNS:
208 jsonw_uint_field(json_wtr, "netns_ino",
209 info->netns.netns_ino);
210 show_link_attach_type_json(info->netns.attach_type, json_wtr);
212 case BPF_LINK_TYPE_NETFILTER:
213 netfilter_dump_json(info, json_wtr);
220 if (!hashmap__empty(link_table)) {
221 struct hashmap_entry *entry;
223 jsonw_name(json_wtr, "pinned");
224 jsonw_start_array(json_wtr);
225 hashmap__for_each_key_entry(link_table, entry, info->id)
226 jsonw_string(json_wtr, entry->pvalue);
227 jsonw_end_array(json_wtr);
230 emit_obj_refs_json(refs_table, info->id, json_wtr);
232 jsonw_end_object(json_wtr);
237 static void show_link_header_plain(struct bpf_link_info *info)
239 const char *link_type_str;
241 printf("%u: ", info->id);
242 link_type_str = libbpf_bpf_link_type_str(info->type);
244 printf("%s ", link_type_str);
246 printf("type %u ", info->type);
248 printf("prog %u ", info->prog_id);
251 static void show_link_attach_type_plain(__u32 attach_type)
253 const char *attach_type_str;
255 attach_type_str = libbpf_bpf_attach_type_str(attach_type);
257 printf("attach_type %s ", attach_type_str);
259 printf("attach_type %u ", attach_type);
262 static void show_iter_plain(struct bpf_link_info *info)
264 const char *target_name = u64_to_ptr(info->iter.target_name);
266 printf("target_name %s ", target_name);
268 if (is_iter_map_target(target_name))
269 printf("map_id %u ", info->iter.map.map_id);
270 else if (is_iter_task_target(target_name)) {
271 if (info->iter.task.tid)
272 printf("tid %u ", info->iter.task.tid);
273 else if (info->iter.task.pid)
274 printf("pid %u ", info->iter.task.pid);
277 if (is_iter_cgroup_target(target_name)) {
278 printf("cgroup_id %llu ", info->iter.cgroup.cgroup_id);
280 cgroup_order_string(info->iter.cgroup.order));
284 static const char * const pf2name[] = {
285 [NFPROTO_INET] = "inet",
286 [NFPROTO_IPV4] = "ip",
287 [NFPROTO_ARP] = "arp",
288 [NFPROTO_NETDEV] = "netdev",
289 [NFPROTO_BRIDGE] = "bridge",
290 [NFPROTO_IPV6] = "ip6",
293 static const char * const inethook2name[] = {
294 [NF_INET_PRE_ROUTING] = "prerouting",
295 [NF_INET_LOCAL_IN] = "input",
296 [NF_INET_FORWARD] = "forward",
297 [NF_INET_LOCAL_OUT] = "output",
298 [NF_INET_POST_ROUTING] = "postrouting",
301 static const char * const arphook2name[] = {
302 [NF_ARP_IN] = "input",
303 [NF_ARP_OUT] = "output",
306 void netfilter_dump_plain(const struct bpf_link_info *info)
308 const char *hookname = NULL, *pfname = NULL;
309 unsigned int hook = info->netfilter.hooknum;
310 unsigned int pf = info->netfilter.pf;
312 if (pf < ARRAY_SIZE(pf2name))
313 pfname = pf2name[pf];
316 case NFPROTO_BRIDGE: /* bridge shares numbers with enum nf_inet_hooks */
320 if (hook < ARRAY_SIZE(inethook2name))
321 hookname = inethook2name[hook];
324 if (hook < ARRAY_SIZE(arphook2name))
325 hookname = arphook2name[hook];
331 printf("\n\t%s", pfname);
333 printf("\n\tpf: %d", pf);
336 printf(" %s", hookname);
338 printf(", hook %u,", hook);
340 printf(" prio %d", info->netfilter.priority);
342 if (info->netfilter.flags)
343 printf(" flags 0x%x", info->netfilter.flags);
346 static int show_link_close_plain(int fd, struct bpf_link_info *info)
348 struct bpf_prog_info prog_info;
349 const char *prog_type_str;
352 show_link_header_plain(info);
354 switch (info->type) {
355 case BPF_LINK_TYPE_RAW_TRACEPOINT:
356 printf("\n\ttp '%s' ",
357 (const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
359 case BPF_LINK_TYPE_TRACING:
360 err = get_prog_info(info->prog_id, &prog_info);
364 prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
365 /* libbpf will return NULL for variants unknown to it. */
367 printf("\n\tprog_type %s ", prog_type_str);
369 printf("\n\tprog_type %u ", prog_info.type);
371 show_link_attach_type_plain(info->tracing.attach_type);
373 case BPF_LINK_TYPE_CGROUP:
374 printf("\n\tcgroup_id %zu ", (size_t)info->cgroup.cgroup_id);
375 show_link_attach_type_plain(info->cgroup.attach_type);
377 case BPF_LINK_TYPE_ITER:
378 show_iter_plain(info);
380 case BPF_LINK_TYPE_NETNS:
381 printf("\n\tnetns_ino %u ", info->netns.netns_ino);
382 show_link_attach_type_plain(info->netns.attach_type);
384 case BPF_LINK_TYPE_NETFILTER:
385 netfilter_dump_plain(info);
391 if (!hashmap__empty(link_table)) {
392 struct hashmap_entry *entry;
394 hashmap__for_each_key_entry(link_table, entry, info->id)
395 printf("\n\tpinned %s", (char *)entry->pvalue);
397 emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
404 static int do_show_link(int fd)
406 struct bpf_link_info info;
407 __u32 len = sizeof(info);
411 memset(&info, 0, sizeof(info));
413 err = bpf_link_get_info_by_fd(fd, &info, &len);
415 p_err("can't get link info: %s",
420 if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
421 !info.raw_tracepoint.tp_name) {
422 info.raw_tracepoint.tp_name = (unsigned long)&buf;
423 info.raw_tracepoint.tp_name_len = sizeof(buf);
426 if (info.type == BPF_LINK_TYPE_ITER &&
427 !info.iter.target_name) {
428 info.iter.target_name = (unsigned long)&buf;
429 info.iter.target_name_len = sizeof(buf);
434 show_link_close_json(fd, &info);
436 show_link_close_plain(fd, &info);
442 static int do_show(int argc, char **argv)
448 link_table = hashmap__new(hash_fn_for_key_as_id,
449 equal_fn_for_key_as_id, NULL);
450 if (IS_ERR(link_table)) {
451 p_err("failed to create hashmap for pinned paths");
454 build_pinned_obj_table(link_table, BPF_OBJ_LINK);
456 build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
459 fd = link_parse_fd(&argc, &argv);
462 return do_show_link(fd);
469 jsonw_start_array(json_wtr);
471 err = bpf_link_get_next_id(id, &id);
475 p_err("can't get next link: %s%s", strerror(errno),
476 errno == EINVAL ? " -- kernel too old?" : "");
480 fd = bpf_link_get_fd_by_id(id);
484 p_err("can't get link by id (%u): %s",
485 id, strerror(errno));
489 err = do_show_link(fd);
494 jsonw_end_array(json_wtr);
496 delete_obj_refs_table(refs_table);
499 delete_pinned_obj_table(link_table);
501 return errno == ENOENT ? 0 : -1;
504 static int do_pin(int argc, char **argv)
508 err = do_pin_any(argc, argv, link_parse_fd);
509 if (!err && json_output)
510 jsonw_null(json_wtr);
514 static int do_detach(int argc, char **argv)
519 p_err("link specifier is invalid or missing\n");
523 fd = link_parse_fd(&argc, &argv);
527 err = bpf_link_detach(fd);
532 p_err("failed link detach: %s", strerror(-err));
537 jsonw_null(json_wtr);
542 static int do_help(int argc, char **argv)
545 jsonw_null(json_wtr);
550 "Usage: %1$s %2$s { show | list } [LINK]\n"
551 " %1$s %2$s pin LINK FILE\n"
552 " %1$s %2$s detach LINK\n"
555 " " HELP_SPEC_LINK "\n"
556 " " HELP_SPEC_OPTIONS " |\n"
557 " {-f|--bpffs} | {-n|--nomount} }\n"
564 static const struct cmd cmds[] = {
569 { "detach", do_detach },
573 int do_link(int argc, char **argv)
575 return cmd_select(cmds, argc, argv, do_help);