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 break; 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); 203 break; 204 case BPF_LINK_TYPE_ITER: 205 show_iter_json(info, json_wtr); 206 break; 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); 211 break; 212 case BPF_LINK_TYPE_NETFILTER: 213 netfilter_dump_json(info, json_wtr); 214 break; 215 case BPF_LINK_TYPE_STRUCT_OPS: 216 jsonw_uint_field(json_wtr, "map_id", 217 info->struct_ops.map_id); 218 break; 219 default: 220 break; 221 } 222 223 if (!hashmap__empty(link_table)) { 224 struct hashmap_entry *entry; 225 226 jsonw_name(json_wtr, "pinned"); 227 jsonw_start_array(json_wtr); 228 hashmap__for_each_key_entry(link_table, entry, info->id) 229 jsonw_string(json_wtr, entry->pvalue); 230 jsonw_end_array(json_wtr); 231 } 232 233 emit_obj_refs_json(refs_table, info->id, json_wtr); 234 235 jsonw_end_object(json_wtr); 236 237 return 0; 238 } 239 240 static void show_link_header_plain(struct bpf_link_info *info) 241 { 242 const char *link_type_str; 243 244 printf("%u: ", info->id); 245 link_type_str = libbpf_bpf_link_type_str(info->type); 246 if (link_type_str) 247 printf("%s ", link_type_str); 248 else 249 printf("type %u ", info->type); 250 251 if (info->type == BPF_LINK_TYPE_STRUCT_OPS) 252 printf("map %u ", info->struct_ops.map_id); 253 else 254 printf("prog %u ", info->prog_id); 255 } 256 257 static void show_link_attach_type_plain(__u32 attach_type) 258 { 259 const char *attach_type_str; 260 261 attach_type_str = libbpf_bpf_attach_type_str(attach_type); 262 if (attach_type_str) 263 printf("attach_type %s ", attach_type_str); 264 else 265 printf("attach_type %u ", attach_type); 266 } 267 268 static void show_iter_plain(struct bpf_link_info *info) 269 { 270 const char *target_name = u64_to_ptr(info->iter.target_name); 271 272 printf("target_name %s ", target_name); 273 274 if (is_iter_map_target(target_name)) 275 printf("map_id %u ", info->iter.map.map_id); 276 else if (is_iter_task_target(target_name)) { 277 if (info->iter.task.tid) 278 printf("tid %u ", info->iter.task.tid); 279 else if (info->iter.task.pid) 280 printf("pid %u ", info->iter.task.pid); 281 } 282 283 if (is_iter_cgroup_target(target_name)) { 284 printf("cgroup_id %llu ", info->iter.cgroup.cgroup_id); 285 printf("order %s ", 286 cgroup_order_string(info->iter.cgroup.order)); 287 } 288 } 289 290 static const char * const pf2name[] = { 291 [NFPROTO_INET] = "inet", 292 [NFPROTO_IPV4] = "ip", 293 [NFPROTO_ARP] = "arp", 294 [NFPROTO_NETDEV] = "netdev", 295 [NFPROTO_BRIDGE] = "bridge", 296 [NFPROTO_IPV6] = "ip6", 297 }; 298 299 static const char * const inethook2name[] = { 300 [NF_INET_PRE_ROUTING] = "prerouting", 301 [NF_INET_LOCAL_IN] = "input", 302 [NF_INET_FORWARD] = "forward", 303 [NF_INET_LOCAL_OUT] = "output", 304 [NF_INET_POST_ROUTING] = "postrouting", 305 }; 306 307 static const char * const arphook2name[] = { 308 [NF_ARP_IN] = "input", 309 [NF_ARP_OUT] = "output", 310 }; 311 312 void netfilter_dump_plain(const struct bpf_link_info *info) 313 { 314 const char *hookname = NULL, *pfname = NULL; 315 unsigned int hook = info->netfilter.hooknum; 316 unsigned int pf = info->netfilter.pf; 317 318 if (pf < ARRAY_SIZE(pf2name)) 319 pfname = pf2name[pf]; 320 321 switch (pf) { 322 case NFPROTO_BRIDGE: /* bridge shares numbers with enum nf_inet_hooks */ 323 case NFPROTO_IPV4: 324 case NFPROTO_IPV6: 325 case NFPROTO_INET: 326 if (hook < ARRAY_SIZE(inethook2name)) 327 hookname = inethook2name[hook]; 328 break; 329 case NFPROTO_ARP: 330 if (hook < ARRAY_SIZE(arphook2name)) 331 hookname = arphook2name[hook]; 332 default: 333 break; 334 } 335 336 if (pfname) 337 printf("\n\t%s", pfname); 338 else 339 printf("\n\tpf: %d", pf); 340 341 if (hookname) 342 printf(" %s", hookname); 343 else 344 printf(", hook %u,", hook); 345 346 printf(" prio %d", info->netfilter.priority); 347 348 if (info->netfilter.flags) 349 printf(" flags 0x%x", info->netfilter.flags); 350 } 351 352 static int show_link_close_plain(int fd, struct bpf_link_info *info) 353 { 354 struct bpf_prog_info prog_info; 355 const char *prog_type_str; 356 int err; 357 358 show_link_header_plain(info); 359 360 switch (info->type) { 361 case BPF_LINK_TYPE_RAW_TRACEPOINT: 362 printf("\n\ttp '%s' ", 363 (const char *)u64_to_ptr(info->raw_tracepoint.tp_name)); 364 break; 365 case BPF_LINK_TYPE_TRACING: 366 err = get_prog_info(info->prog_id, &prog_info); 367 if (err) 368 return err; 369 370 prog_type_str = libbpf_bpf_prog_type_str(prog_info.type); 371 /* libbpf will return NULL for variants unknown to it. */ 372 if (prog_type_str) 373 printf("\n\tprog_type %s ", prog_type_str); 374 else 375 printf("\n\tprog_type %u ", prog_info.type); 376 377 show_link_attach_type_plain(info->tracing.attach_type); 378 break; 379 case BPF_LINK_TYPE_CGROUP: 380 printf("\n\tcgroup_id %zu ", (size_t)info->cgroup.cgroup_id); 381 show_link_attach_type_plain(info->cgroup.attach_type); 382 break; 383 case BPF_LINK_TYPE_ITER: 384 show_iter_plain(info); 385 break; 386 case BPF_LINK_TYPE_NETNS: 387 printf("\n\tnetns_ino %u ", info->netns.netns_ino); 388 show_link_attach_type_plain(info->netns.attach_type); 389 break; 390 case BPF_LINK_TYPE_NETFILTER: 391 netfilter_dump_plain(info); 392 break; 393 default: 394 break; 395 } 396 397 if (!hashmap__empty(link_table)) { 398 struct hashmap_entry *entry; 399 400 hashmap__for_each_key_entry(link_table, entry, info->id) 401 printf("\n\tpinned %s", (char *)entry->pvalue); 402 } 403 emit_obj_refs_plain(refs_table, info->id, "\n\tpids "); 404 405 printf("\n"); 406 407 return 0; 408 } 409 410 static int do_show_link(int fd) 411 { 412 struct bpf_link_info info; 413 __u32 len = sizeof(info); 414 char buf[256]; 415 int err; 416 417 memset(&info, 0, sizeof(info)); 418 again: 419 err = bpf_link_get_info_by_fd(fd, &info, &len); 420 if (err) { 421 p_err("can't get link info: %s", 422 strerror(errno)); 423 close(fd); 424 return err; 425 } 426 if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT && 427 !info.raw_tracepoint.tp_name) { 428 info.raw_tracepoint.tp_name = (unsigned long)&buf; 429 info.raw_tracepoint.tp_name_len = sizeof(buf); 430 goto again; 431 } 432 if (info.type == BPF_LINK_TYPE_ITER && 433 !info.iter.target_name) { 434 info.iter.target_name = (unsigned long)&buf; 435 info.iter.target_name_len = sizeof(buf); 436 goto again; 437 } 438 439 if (json_output) 440 show_link_close_json(fd, &info); 441 else 442 show_link_close_plain(fd, &info); 443 444 close(fd); 445 return 0; 446 } 447 448 static int do_show(int argc, char **argv) 449 { 450 __u32 id = 0; 451 int err, fd; 452 453 if (show_pinned) { 454 link_table = hashmap__new(hash_fn_for_key_as_id, 455 equal_fn_for_key_as_id, NULL); 456 if (IS_ERR(link_table)) { 457 p_err("failed to create hashmap for pinned paths"); 458 return -1; 459 } 460 build_pinned_obj_table(link_table, BPF_OBJ_LINK); 461 } 462 build_obj_refs_table(&refs_table, BPF_OBJ_LINK); 463 464 if (argc == 2) { 465 fd = link_parse_fd(&argc, &argv); 466 if (fd < 0) 467 return fd; 468 return do_show_link(fd); 469 } 470 471 if (argc) 472 return BAD_ARG(); 473 474 if (json_output) 475 jsonw_start_array(json_wtr); 476 while (true) { 477 err = bpf_link_get_next_id(id, &id); 478 if (err) { 479 if (errno == ENOENT) 480 break; 481 p_err("can't get next link: %s%s", strerror(errno), 482 errno == EINVAL ? " -- kernel too old?" : ""); 483 break; 484 } 485 486 fd = bpf_link_get_fd_by_id(id); 487 if (fd < 0) { 488 if (errno == ENOENT) 489 continue; 490 p_err("can't get link by id (%u): %s", 491 id, strerror(errno)); 492 break; 493 } 494 495 err = do_show_link(fd); 496 if (err) 497 break; 498 } 499 if (json_output) 500 jsonw_end_array(json_wtr); 501 502 delete_obj_refs_table(refs_table); 503 504 if (show_pinned) 505 delete_pinned_obj_table(link_table); 506 507 return errno == ENOENT ? 0 : -1; 508 } 509 510 static int do_pin(int argc, char **argv) 511 { 512 int err; 513 514 err = do_pin_any(argc, argv, link_parse_fd); 515 if (!err && json_output) 516 jsonw_null(json_wtr); 517 return err; 518 } 519 520 static int do_detach(int argc, char **argv) 521 { 522 int err, fd; 523 524 if (argc != 2) { 525 p_err("link specifier is invalid or missing\n"); 526 return 1; 527 } 528 529 fd = link_parse_fd(&argc, &argv); 530 if (fd < 0) 531 return 1; 532 533 err = bpf_link_detach(fd); 534 if (err) 535 err = -errno; 536 close(fd); 537 if (err) { 538 p_err("failed link detach: %s", strerror(-err)); 539 return 1; 540 } 541 542 if (json_output) 543 jsonw_null(json_wtr); 544 545 return 0; 546 } 547 548 static int do_help(int argc, char **argv) 549 { 550 if (json_output) { 551 jsonw_null(json_wtr); 552 return 0; 553 } 554 555 fprintf(stderr, 556 "Usage: %1$s %2$s { show | list } [LINK]\n" 557 " %1$s %2$s pin LINK FILE\n" 558 " %1$s %2$s detach LINK\n" 559 " %1$s %2$s help\n" 560 "\n" 561 " " HELP_SPEC_LINK "\n" 562 " " HELP_SPEC_OPTIONS " |\n" 563 " {-f|--bpffs} | {-n|--nomount} }\n" 564 "", 565 bin_name, argv[-2]); 566 567 return 0; 568 } 569 570 static const struct cmd cmds[] = { 571 { "show", do_show }, 572 { "list", do_show }, 573 { "help", do_help }, 574 { "pin", do_pin }, 575 { "detach", do_detach }, 576 { 0 } 577 }; 578 579 int do_link(int argc, char **argv) 580 { 581 return cmd_select(cmds, argc, argv, do_help); 582 } 583