1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 // Copyright (C) 2018 Facebook 3 4 #ifndef _GNU_SOURCE 5 #define _GNU_SOURCE 6 #endif 7 #include <errno.h> 8 #include <fcntl.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #include <time.h> 12 #include <unistd.h> 13 #include <bpf/bpf.h> 14 #include <bpf/libbpf.h> 15 #include <net/if.h> 16 #include <linux/rtnetlink.h> 17 #include <linux/socket.h> 18 #include <linux/tc_act/tc_bpf.h> 19 #include <sys/socket.h> 20 #include <sys/stat.h> 21 #include <sys/types.h> 22 23 #include "bpf/nlattr.h" 24 #include "main.h" 25 #include "netlink_dumper.h" 26 27 #ifndef SOL_NETLINK 28 #define SOL_NETLINK 270 29 #endif 30 31 struct ip_devname_ifindex { 32 char devname[64]; 33 int ifindex; 34 }; 35 36 struct bpf_netdev_t { 37 struct ip_devname_ifindex *devices; 38 int used_len; 39 int array_len; 40 int filter_idx; 41 }; 42 43 struct tc_kind_handle { 44 char kind[64]; 45 int handle; 46 }; 47 48 struct bpf_tcinfo_t { 49 struct tc_kind_handle *handle_array; 50 int used_len; 51 int array_len; 52 bool is_qdisc; 53 }; 54 55 struct bpf_filter_t { 56 const char *kind; 57 const char *devname; 58 int ifindex; 59 }; 60 61 struct bpf_attach_info { 62 __u32 flow_dissector_id; 63 }; 64 65 enum net_attach_type { 66 NET_ATTACH_TYPE_XDP, 67 NET_ATTACH_TYPE_XDP_GENERIC, 68 NET_ATTACH_TYPE_XDP_DRIVER, 69 NET_ATTACH_TYPE_XDP_OFFLOAD, 70 NET_ATTACH_TYPE_TCX_INGRESS, 71 NET_ATTACH_TYPE_TCX_EGRESS, 72 }; 73 74 static const char * const attach_type_strings[] = { 75 [NET_ATTACH_TYPE_XDP] = "xdp", 76 [NET_ATTACH_TYPE_XDP_GENERIC] = "xdpgeneric", 77 [NET_ATTACH_TYPE_XDP_DRIVER] = "xdpdrv", 78 [NET_ATTACH_TYPE_XDP_OFFLOAD] = "xdpoffload", 79 [NET_ATTACH_TYPE_TCX_INGRESS] = "tcx_ingress", 80 [NET_ATTACH_TYPE_TCX_EGRESS] = "tcx_egress", 81 }; 82 83 static const char * const attach_loc_strings[] = { 84 [BPF_TCX_INGRESS] = "tcx/ingress", 85 [BPF_TCX_EGRESS] = "tcx/egress", 86 [BPF_NETKIT_PRIMARY] = "netkit/primary", 87 [BPF_NETKIT_PEER] = "netkit/peer", 88 }; 89 90 const size_t net_attach_type_size = ARRAY_SIZE(attach_type_strings); 91 92 static enum net_attach_type parse_attach_type(const char *str) 93 { 94 enum net_attach_type type; 95 96 for (type = 0; type < net_attach_type_size; type++) { 97 if (attach_type_strings[type] && 98 is_prefix(str, attach_type_strings[type])) 99 return type; 100 } 101 102 return net_attach_type_size; 103 } 104 105 typedef int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb); 106 107 typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, dump_nlmsg_t, void *cookie); 108 109 static int netlink_open(__u32 *nl_pid) 110 { 111 struct sockaddr_nl sa; 112 socklen_t addrlen; 113 int one = 1, ret; 114 int sock; 115 116 memset(&sa, 0, sizeof(sa)); 117 sa.nl_family = AF_NETLINK; 118 119 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); 120 if (sock < 0) 121 return -errno; 122 123 if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK, 124 &one, sizeof(one)) < 0) { 125 p_err("Netlink error reporting not supported"); 126 } 127 128 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) { 129 ret = -errno; 130 goto cleanup; 131 } 132 133 addrlen = sizeof(sa); 134 if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) { 135 ret = -errno; 136 goto cleanup; 137 } 138 139 if (addrlen != sizeof(sa)) { 140 ret = -LIBBPF_ERRNO__INTERNAL; 141 goto cleanup; 142 } 143 144 *nl_pid = sa.nl_pid; 145 return sock; 146 147 cleanup: 148 close(sock); 149 return ret; 150 } 151 152 static int netlink_recv(int sock, __u32 nl_pid, __u32 seq, 153 __dump_nlmsg_t _fn, dump_nlmsg_t fn, 154 void *cookie) 155 { 156 bool multipart = true; 157 struct nlmsgerr *err; 158 struct nlmsghdr *nh; 159 char buf[4096]; 160 int len, ret; 161 162 while (multipart) { 163 multipart = false; 164 len = recv(sock, buf, sizeof(buf), 0); 165 if (len < 0) { 166 ret = -errno; 167 goto done; 168 } 169 170 if (len == 0) 171 break; 172 173 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, (unsigned int)len); 174 nh = NLMSG_NEXT(nh, len)) { 175 if (nh->nlmsg_pid != nl_pid) { 176 ret = -LIBBPF_ERRNO__WRNGPID; 177 goto done; 178 } 179 if (nh->nlmsg_seq != seq) { 180 ret = -LIBBPF_ERRNO__INVSEQ; 181 goto done; 182 } 183 if (nh->nlmsg_flags & NLM_F_MULTI) 184 multipart = true; 185 switch (nh->nlmsg_type) { 186 case NLMSG_ERROR: 187 err = (struct nlmsgerr *)NLMSG_DATA(nh); 188 if (!err->error) 189 continue; 190 ret = err->error; 191 libbpf_nla_dump_errormsg(nh); 192 goto done; 193 case NLMSG_DONE: 194 return 0; 195 default: 196 break; 197 } 198 if (_fn) { 199 ret = _fn(nh, fn, cookie); 200 if (ret) 201 return ret; 202 } 203 } 204 } 205 ret = 0; 206 done: 207 return ret; 208 } 209 210 static int __dump_class_nlmsg(struct nlmsghdr *nlh, 211 dump_nlmsg_t dump_class_nlmsg, 212 void *cookie) 213 { 214 struct nlattr *tb[TCA_MAX + 1], *attr; 215 struct tcmsg *t = NLMSG_DATA(nlh); 216 int len; 217 218 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 219 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t))); 220 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0) 221 return -LIBBPF_ERRNO__NLPARSE; 222 223 return dump_class_nlmsg(cookie, t, tb); 224 } 225 226 static int netlink_get_class(int sock, unsigned int nl_pid, int ifindex, 227 dump_nlmsg_t dump_class_nlmsg, void *cookie) 228 { 229 struct { 230 struct nlmsghdr nlh; 231 struct tcmsg t; 232 } req = { 233 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)), 234 .nlh.nlmsg_type = RTM_GETTCLASS, 235 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, 236 .t.tcm_family = AF_UNSPEC, 237 .t.tcm_ifindex = ifindex, 238 }; 239 int seq = time(NULL); 240 241 req.nlh.nlmsg_seq = seq; 242 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0) 243 return -errno; 244 245 return netlink_recv(sock, nl_pid, seq, __dump_class_nlmsg, 246 dump_class_nlmsg, cookie); 247 } 248 249 static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh, 250 dump_nlmsg_t dump_qdisc_nlmsg, 251 void *cookie) 252 { 253 struct nlattr *tb[TCA_MAX + 1], *attr; 254 struct tcmsg *t = NLMSG_DATA(nlh); 255 int len; 256 257 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 258 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t))); 259 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0) 260 return -LIBBPF_ERRNO__NLPARSE; 261 262 return dump_qdisc_nlmsg(cookie, t, tb); 263 } 264 265 static int netlink_get_qdisc(int sock, unsigned int nl_pid, int ifindex, 266 dump_nlmsg_t dump_qdisc_nlmsg, void *cookie) 267 { 268 struct { 269 struct nlmsghdr nlh; 270 struct tcmsg t; 271 } req = { 272 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)), 273 .nlh.nlmsg_type = RTM_GETQDISC, 274 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, 275 .t.tcm_family = AF_UNSPEC, 276 .t.tcm_ifindex = ifindex, 277 }; 278 int seq = time(NULL); 279 280 req.nlh.nlmsg_seq = seq; 281 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0) 282 return -errno; 283 284 return netlink_recv(sock, nl_pid, seq, __dump_qdisc_nlmsg, 285 dump_qdisc_nlmsg, cookie); 286 } 287 288 static int __dump_filter_nlmsg(struct nlmsghdr *nlh, 289 dump_nlmsg_t dump_filter_nlmsg, 290 void *cookie) 291 { 292 struct nlattr *tb[TCA_MAX + 1], *attr; 293 struct tcmsg *t = NLMSG_DATA(nlh); 294 int len; 295 296 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t)); 297 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t))); 298 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0) 299 return -LIBBPF_ERRNO__NLPARSE; 300 301 return dump_filter_nlmsg(cookie, t, tb); 302 } 303 304 static int netlink_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, 305 dump_nlmsg_t dump_filter_nlmsg, void *cookie) 306 { 307 struct { 308 struct nlmsghdr nlh; 309 struct tcmsg t; 310 } req = { 311 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)), 312 .nlh.nlmsg_type = RTM_GETTFILTER, 313 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, 314 .t.tcm_family = AF_UNSPEC, 315 .t.tcm_ifindex = ifindex, 316 .t.tcm_parent = handle, 317 }; 318 int seq = time(NULL); 319 320 req.nlh.nlmsg_seq = seq; 321 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0) 322 return -errno; 323 324 return netlink_recv(sock, nl_pid, seq, __dump_filter_nlmsg, 325 dump_filter_nlmsg, cookie); 326 } 327 328 static int __dump_link_nlmsg(struct nlmsghdr *nlh, 329 dump_nlmsg_t dump_link_nlmsg, void *cookie) 330 { 331 struct nlattr *tb[IFLA_MAX + 1], *attr; 332 struct ifinfomsg *ifi = NLMSG_DATA(nlh); 333 int len; 334 335 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)); 336 attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi))); 337 if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0) 338 return -LIBBPF_ERRNO__NLPARSE; 339 340 return dump_link_nlmsg(cookie, ifi, tb); 341 } 342 343 static int netlink_get_link(int sock, unsigned int nl_pid, 344 dump_nlmsg_t dump_link_nlmsg, void *cookie) 345 { 346 struct { 347 struct nlmsghdr nlh; 348 struct ifinfomsg ifm; 349 } req = { 350 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), 351 .nlh.nlmsg_type = RTM_GETLINK, 352 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, 353 .ifm.ifi_family = AF_PACKET, 354 }; 355 int seq = time(NULL); 356 357 req.nlh.nlmsg_seq = seq; 358 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0) 359 return -errno; 360 361 return netlink_recv(sock, nl_pid, seq, __dump_link_nlmsg, 362 dump_link_nlmsg, cookie); 363 } 364 365 static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb) 366 { 367 struct bpf_netdev_t *netinfo = cookie; 368 struct ifinfomsg *ifinfo = msg; 369 struct ip_devname_ifindex *tmp; 370 371 if (netinfo->filter_idx > 0 && netinfo->filter_idx != ifinfo->ifi_index) 372 return 0; 373 374 if (netinfo->used_len == netinfo->array_len) { 375 tmp = realloc(netinfo->devices, 376 (netinfo->array_len + 16) * sizeof(struct ip_devname_ifindex)); 377 if (!tmp) 378 return -ENOMEM; 379 380 netinfo->devices = tmp; 381 netinfo->array_len += 16; 382 } 383 netinfo->devices[netinfo->used_len].ifindex = ifinfo->ifi_index; 384 snprintf(netinfo->devices[netinfo->used_len].devname, 385 sizeof(netinfo->devices[netinfo->used_len].devname), 386 "%s", 387 tb[IFLA_IFNAME] 388 ? libbpf_nla_getattr_str(tb[IFLA_IFNAME]) 389 : ""); 390 netinfo->used_len++; 391 392 return do_xdp_dump(ifinfo, tb); 393 } 394 395 static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb) 396 { 397 struct bpf_tcinfo_t *tcinfo = cookie; 398 struct tcmsg *info = msg; 399 struct tc_kind_handle *tmp; 400 401 if (tcinfo->is_qdisc) { 402 /* skip clsact qdisc */ 403 if (tb[TCA_KIND] && 404 strcmp(libbpf_nla_data(tb[TCA_KIND]), "clsact") == 0) 405 return 0; 406 if (info->tcm_handle == 0) 407 return 0; 408 } 409 410 if (tcinfo->used_len == tcinfo->array_len) { 411 tmp = realloc(tcinfo->handle_array, 412 (tcinfo->array_len + 16) * sizeof(struct tc_kind_handle)); 413 if (!tmp) 414 return -ENOMEM; 415 416 tcinfo->handle_array = tmp; 417 tcinfo->array_len += 16; 418 } 419 tcinfo->handle_array[tcinfo->used_len].handle = info->tcm_handle; 420 snprintf(tcinfo->handle_array[tcinfo->used_len].kind, 421 sizeof(tcinfo->handle_array[tcinfo->used_len].kind), 422 "%s", 423 tb[TCA_KIND] 424 ? libbpf_nla_getattr_str(tb[TCA_KIND]) 425 : "unknown"); 426 tcinfo->used_len++; 427 428 return 0; 429 } 430 431 static int dump_filter_nlmsg(void *cookie, void *msg, struct nlattr **tb) 432 { 433 const struct bpf_filter_t *filter_info = cookie; 434 435 return do_filter_dump((struct tcmsg *)msg, tb, filter_info->kind, 436 filter_info->devname, filter_info->ifindex); 437 } 438 439 static int __show_dev_tc_bpf_name(__u32 id, char *name, size_t len) 440 { 441 struct bpf_prog_info info = {}; 442 __u32 ilen = sizeof(info); 443 int fd, ret; 444 445 fd = bpf_prog_get_fd_by_id(id); 446 if (fd < 0) 447 return fd; 448 ret = bpf_obj_get_info_by_fd(fd, &info, &ilen); 449 if (ret < 0) 450 goto out; 451 ret = -ENOENT; 452 if (info.name[0]) { 453 get_prog_full_name(&info, fd, name, len); 454 ret = 0; 455 } 456 out: 457 close(fd); 458 return ret; 459 } 460 461 static void __show_dev_tc_bpf(const struct ip_devname_ifindex *dev, 462 const enum bpf_attach_type loc) 463 { 464 __u32 prog_flags[64] = {}, link_flags[64] = {}, i, j; 465 __u32 prog_ids[64] = {}, link_ids[64] = {}; 466 LIBBPF_OPTS(bpf_prog_query_opts, optq); 467 char prog_name[MAX_PROG_FULL_NAME]; 468 int ret; 469 470 optq.prog_ids = prog_ids; 471 optq.prog_attach_flags = prog_flags; 472 optq.link_ids = link_ids; 473 optq.link_attach_flags = link_flags; 474 optq.count = ARRAY_SIZE(prog_ids); 475 476 ret = bpf_prog_query_opts(dev->ifindex, loc, &optq); 477 if (ret) 478 return; 479 for (i = 0; i < optq.count; i++) { 480 NET_START_OBJECT; 481 NET_DUMP_STR("devname", "%s", dev->devname); 482 NET_DUMP_UINT("ifindex", "(%u)", (unsigned int)dev->ifindex); 483 NET_DUMP_STR("kind", " %s", attach_loc_strings[loc]); 484 ret = __show_dev_tc_bpf_name(prog_ids[i], prog_name, 485 sizeof(prog_name)); 486 if (!ret) 487 NET_DUMP_STR("name", " %s", prog_name); 488 NET_DUMP_UINT("prog_id", " prog_id %u ", prog_ids[i]); 489 if (prog_flags[i] || json_output) { 490 NET_START_ARRAY("prog_flags", "%s "); 491 for (j = 0; prog_flags[i] && j < 32; j++) { 492 if (!(prog_flags[i] & (1U << j))) 493 continue; 494 NET_DUMP_UINT_ONLY(1U << j); 495 } 496 NET_END_ARRAY(""); 497 } 498 if (link_ids[i] || json_output) { 499 NET_DUMP_UINT("link_id", "link_id %u ", link_ids[i]); 500 if (link_flags[i] || json_output) { 501 NET_START_ARRAY("link_flags", "%s "); 502 for (j = 0; link_flags[i] && j < 32; j++) { 503 if (!(link_flags[i] & (1U << j))) 504 continue; 505 NET_DUMP_UINT_ONLY(1U << j); 506 } 507 NET_END_ARRAY(""); 508 } 509 } 510 NET_END_OBJECT_FINAL; 511 } 512 } 513 514 static void show_dev_tc_bpf(struct ip_devname_ifindex *dev) 515 { 516 __show_dev_tc_bpf(dev, BPF_TCX_INGRESS); 517 __show_dev_tc_bpf(dev, BPF_TCX_EGRESS); 518 519 __show_dev_tc_bpf(dev, BPF_NETKIT_PRIMARY); 520 __show_dev_tc_bpf(dev, BPF_NETKIT_PEER); 521 } 522 523 static int show_dev_tc_bpf_classic(int sock, unsigned int nl_pid, 524 struct ip_devname_ifindex *dev) 525 { 526 struct bpf_filter_t filter_info; 527 struct bpf_tcinfo_t tcinfo; 528 int i, handle, ret = 0; 529 530 tcinfo.handle_array = NULL; 531 tcinfo.used_len = 0; 532 tcinfo.array_len = 0; 533 534 tcinfo.is_qdisc = false; 535 ret = netlink_get_class(sock, nl_pid, dev->ifindex, 536 dump_class_qdisc_nlmsg, &tcinfo); 537 if (ret) 538 goto out; 539 540 tcinfo.is_qdisc = true; 541 ret = netlink_get_qdisc(sock, nl_pid, dev->ifindex, 542 dump_class_qdisc_nlmsg, &tcinfo); 543 if (ret) 544 goto out; 545 546 filter_info.devname = dev->devname; 547 filter_info.ifindex = dev->ifindex; 548 for (i = 0; i < tcinfo.used_len; i++) { 549 filter_info.kind = tcinfo.handle_array[i].kind; 550 ret = netlink_get_filter(sock, nl_pid, dev->ifindex, 551 tcinfo.handle_array[i].handle, 552 dump_filter_nlmsg, &filter_info); 553 if (ret) 554 goto out; 555 } 556 557 /* root, ingress and egress handle */ 558 handle = TC_H_ROOT; 559 filter_info.kind = "root"; 560 ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle, 561 dump_filter_nlmsg, &filter_info); 562 if (ret) 563 goto out; 564 565 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS); 566 filter_info.kind = "clsact/ingress"; 567 ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle, 568 dump_filter_nlmsg, &filter_info); 569 if (ret) 570 goto out; 571 572 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS); 573 filter_info.kind = "clsact/egress"; 574 ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle, 575 dump_filter_nlmsg, &filter_info); 576 if (ret) 577 goto out; 578 579 out: 580 free(tcinfo.handle_array); 581 return 0; 582 } 583 584 static int query_flow_dissector(struct bpf_attach_info *attach_info) 585 { 586 __u32 attach_flags; 587 __u32 prog_ids[1]; 588 __u32 prog_cnt; 589 int err; 590 int fd; 591 592 fd = open("/proc/self/ns/net", O_RDONLY); 593 if (fd < 0) { 594 p_err("can't open /proc/self/ns/net: %s", 595 strerror(errno)); 596 return -1; 597 } 598 prog_cnt = ARRAY_SIZE(prog_ids); 599 err = bpf_prog_query(fd, BPF_FLOW_DISSECTOR, 0, 600 &attach_flags, prog_ids, &prog_cnt); 601 close(fd); 602 if (err) { 603 if (errno == EINVAL) { 604 /* Older kernel's don't support querying 605 * flow dissector programs. 606 */ 607 errno = 0; 608 return 0; 609 } 610 p_err("can't query prog: %s", strerror(errno)); 611 return -1; 612 } 613 614 if (prog_cnt == 1) 615 attach_info->flow_dissector_id = prog_ids[0]; 616 617 return 0; 618 } 619 620 static int net_parse_dev(int *argc, char ***argv) 621 { 622 int ifindex; 623 624 if (is_prefix(**argv, "dev")) { 625 NEXT_ARGP(); 626 627 ifindex = if_nametoindex(**argv); 628 if (!ifindex) 629 p_err("invalid devname %s", **argv); 630 631 NEXT_ARGP(); 632 } else { 633 p_err("expected 'dev', got: '%s'?", **argv); 634 return -1; 635 } 636 637 return ifindex; 638 } 639 640 static int do_attach_detach_xdp(int progfd, enum net_attach_type attach_type, 641 int ifindex, bool overwrite) 642 { 643 __u32 flags = 0; 644 645 if (!overwrite) 646 flags = XDP_FLAGS_UPDATE_IF_NOEXIST; 647 if (attach_type == NET_ATTACH_TYPE_XDP_GENERIC) 648 flags |= XDP_FLAGS_SKB_MODE; 649 if (attach_type == NET_ATTACH_TYPE_XDP_DRIVER) 650 flags |= XDP_FLAGS_DRV_MODE; 651 if (attach_type == NET_ATTACH_TYPE_XDP_OFFLOAD) 652 flags |= XDP_FLAGS_HW_MODE; 653 654 return bpf_xdp_attach(ifindex, progfd, flags, NULL); 655 } 656 657 static int get_tcx_type(enum net_attach_type attach_type) 658 { 659 switch (attach_type) { 660 case NET_ATTACH_TYPE_TCX_INGRESS: 661 return BPF_TCX_INGRESS; 662 case NET_ATTACH_TYPE_TCX_EGRESS: 663 return BPF_TCX_EGRESS; 664 default: 665 return -1; 666 } 667 } 668 669 static int do_attach_tcx(int progfd, enum net_attach_type attach_type, int ifindex, bool prepend) 670 { 671 int type = get_tcx_type(attach_type); 672 673 if (prepend) { 674 LIBBPF_OPTS(bpf_prog_attach_opts, opts, 675 .flags = BPF_F_BEFORE 676 ); 677 return bpf_prog_attach_opts(progfd, ifindex, type, &opts); 678 } 679 return bpf_prog_attach(progfd, ifindex, type, 0); 680 } 681 682 static int do_detach_tcx(int targetfd, enum net_attach_type attach_type) 683 { 684 int type = get_tcx_type(attach_type); 685 686 return bpf_prog_detach(targetfd, type); 687 } 688 689 static int do_attach(int argc, char **argv) 690 { 691 enum net_attach_type attach_type; 692 int progfd, ifindex, err = 0; 693 bool overwrite = false; 694 bool prepend = false; 695 696 /* parse attach args */ 697 if (!REQ_ARGS(5)) 698 return -EINVAL; 699 700 attach_type = parse_attach_type(*argv); 701 if (attach_type == net_attach_type_size) { 702 p_err("invalid net attach/detach type: %s", *argv); 703 return -EINVAL; 704 } 705 NEXT_ARG(); 706 707 progfd = prog_parse_fd(&argc, &argv); 708 if (progfd < 0) 709 return -EINVAL; 710 711 ifindex = net_parse_dev(&argc, &argv); 712 if (ifindex < 1) { 713 err = -EINVAL; 714 goto cleanup; 715 } 716 717 if (argc) { 718 if (is_prefix(*argv, "overwrite")) { 719 if (attach_type != NET_ATTACH_TYPE_XDP && 720 attach_type != NET_ATTACH_TYPE_XDP_GENERIC && 721 attach_type != NET_ATTACH_TYPE_XDP_DRIVER && 722 attach_type != NET_ATTACH_TYPE_XDP_OFFLOAD) { 723 p_err("'overwrite' is only supported for xdp types"); 724 err = -EINVAL; 725 goto cleanup; 726 } 727 overwrite = true; 728 } else if (is_prefix(*argv, "prepend")) { 729 if (attach_type != NET_ATTACH_TYPE_TCX_INGRESS && 730 attach_type != NET_ATTACH_TYPE_TCX_EGRESS) { 731 p_err("'prepend' is only supported for tcx_ingress/tcx_egress"); 732 err = -EINVAL; 733 goto cleanup; 734 } 735 prepend = true; 736 } else { 737 p_err("expected 'overwrite' or 'prepend', got: '%s'?", *argv); 738 err = -EINVAL; 739 goto cleanup; 740 } 741 } 742 743 switch (attach_type) { 744 /* attach xdp prog */ 745 case NET_ATTACH_TYPE_XDP: 746 case NET_ATTACH_TYPE_XDP_GENERIC: 747 case NET_ATTACH_TYPE_XDP_DRIVER: 748 case NET_ATTACH_TYPE_XDP_OFFLOAD: 749 err = do_attach_detach_xdp(progfd, attach_type, ifindex, overwrite); 750 break; 751 /* attach tcx prog */ 752 case NET_ATTACH_TYPE_TCX_INGRESS: 753 case NET_ATTACH_TYPE_TCX_EGRESS: 754 err = do_attach_tcx(progfd, attach_type, ifindex, prepend); 755 break; 756 default: 757 break; 758 } 759 760 if (err) { 761 p_err("interface %s attach failed: %s", 762 attach_type_strings[attach_type], strerror(-err)); 763 goto cleanup; 764 } 765 766 if (json_output) 767 jsonw_null(json_wtr); 768 cleanup: 769 close(progfd); 770 return err; 771 } 772 773 static int do_detach(int argc, char **argv) 774 { 775 enum net_attach_type attach_type; 776 int progfd, ifindex, err = 0; 777 778 /* parse detach args */ 779 if (!REQ_ARGS(3)) 780 return -EINVAL; 781 782 attach_type = parse_attach_type(*argv); 783 if (attach_type == net_attach_type_size) { 784 p_err("invalid net attach/detach type: %s", *argv); 785 return -EINVAL; 786 } 787 NEXT_ARG(); 788 789 ifindex = net_parse_dev(&argc, &argv); 790 if (ifindex < 1) 791 return -EINVAL; 792 793 switch (attach_type) { 794 /* detach xdp prog */ 795 case NET_ATTACH_TYPE_XDP: 796 case NET_ATTACH_TYPE_XDP_GENERIC: 797 case NET_ATTACH_TYPE_XDP_DRIVER: 798 case NET_ATTACH_TYPE_XDP_OFFLOAD: 799 progfd = -1; 800 err = do_attach_detach_xdp(progfd, attach_type, ifindex, NULL); 801 break; 802 /* detach tcx prog */ 803 case NET_ATTACH_TYPE_TCX_INGRESS: 804 case NET_ATTACH_TYPE_TCX_EGRESS: 805 err = do_detach_tcx(ifindex, attach_type); 806 break; 807 default: 808 break; 809 } 810 811 if (err < 0) { 812 p_err("interface %s detach failed: %s", 813 attach_type_strings[attach_type], strerror(-err)); 814 return err; 815 } 816 817 if (json_output) 818 jsonw_null(json_wtr); 819 820 return 0; 821 } 822 823 static int netfilter_link_compar(const void *a, const void *b) 824 { 825 const struct bpf_link_info *nfa = a; 826 const struct bpf_link_info *nfb = b; 827 int delta; 828 829 delta = nfa->netfilter.pf - nfb->netfilter.pf; 830 if (delta) 831 return delta; 832 833 delta = nfa->netfilter.hooknum - nfb->netfilter.hooknum; 834 if (delta) 835 return delta; 836 837 if (nfa->netfilter.priority < nfb->netfilter.priority) 838 return -1; 839 if (nfa->netfilter.priority > nfb->netfilter.priority) 840 return 1; 841 842 return nfa->netfilter.flags - nfb->netfilter.flags; 843 } 844 845 static void show_link_netfilter(void) 846 { 847 unsigned int nf_link_len = 0, nf_link_count = 0; 848 struct bpf_link_info *nf_link_info = NULL; 849 __u32 id = 0; 850 851 while (true) { 852 struct bpf_link_info info; 853 int fd, err; 854 __u32 len; 855 856 err = bpf_link_get_next_id(id, &id); 857 if (err) { 858 if (errno == ENOENT) 859 break; 860 p_err("can't get next link: %s (id %u)", strerror(errno), id); 861 break; 862 } 863 864 fd = bpf_link_get_fd_by_id(id); 865 if (fd < 0) { 866 p_err("can't get link by id (%u): %s", id, strerror(errno)); 867 continue; 868 } 869 870 memset(&info, 0, sizeof(info)); 871 len = sizeof(info); 872 873 err = bpf_link_get_info_by_fd(fd, &info, &len); 874 875 close(fd); 876 877 if (err) { 878 p_err("can't get link info for fd %d: %s", fd, strerror(errno)); 879 continue; 880 } 881 882 if (info.type != BPF_LINK_TYPE_NETFILTER) 883 continue; 884 885 if (nf_link_count >= nf_link_len) { 886 static const unsigned int max_link_count = INT_MAX / sizeof(info); 887 struct bpf_link_info *expand; 888 889 if (nf_link_count > max_link_count) { 890 p_err("cannot handle more than %u links\n", max_link_count); 891 break; 892 } 893 894 nf_link_len += 16; 895 896 expand = realloc(nf_link_info, nf_link_len * sizeof(info)); 897 if (!expand) { 898 p_err("realloc: %s", strerror(errno)); 899 break; 900 } 901 902 nf_link_info = expand; 903 } 904 905 nf_link_info[nf_link_count] = info; 906 nf_link_count++; 907 } 908 909 if (!nf_link_info) 910 return; 911 912 qsort(nf_link_info, nf_link_count, sizeof(*nf_link_info), netfilter_link_compar); 913 914 for (id = 0; id < nf_link_count; id++) { 915 NET_START_OBJECT; 916 if (json_output) 917 netfilter_dump_json(&nf_link_info[id], json_wtr); 918 else 919 netfilter_dump_plain(&nf_link_info[id]); 920 921 NET_DUMP_UINT("id", " prog_id %u", nf_link_info[id].prog_id); 922 NET_END_OBJECT; 923 } 924 925 free(nf_link_info); 926 } 927 928 static int do_show(int argc, char **argv) 929 { 930 struct bpf_attach_info attach_info = {}; 931 int i, sock, ret, filter_idx = -1; 932 struct bpf_netdev_t dev_array; 933 unsigned int nl_pid = 0; 934 char err_buf[256]; 935 936 if (argc == 2) { 937 filter_idx = net_parse_dev(&argc, &argv); 938 if (filter_idx < 1) 939 return -1; 940 } else if (argc != 0) { 941 usage(); 942 } 943 944 ret = query_flow_dissector(&attach_info); 945 if (ret) 946 return -1; 947 948 sock = netlink_open(&nl_pid); 949 if (sock < 0) { 950 fprintf(stderr, "failed to open netlink sock\n"); 951 return -1; 952 } 953 954 dev_array.devices = NULL; 955 dev_array.used_len = 0; 956 dev_array.array_len = 0; 957 dev_array.filter_idx = filter_idx; 958 959 if (json_output) 960 jsonw_start_array(json_wtr); 961 NET_START_OBJECT; 962 NET_START_ARRAY("xdp", "%s:\n"); 963 ret = netlink_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array); 964 NET_END_ARRAY("\n"); 965 966 if (!ret) { 967 NET_START_ARRAY("tc", "%s:\n"); 968 for (i = 0; i < dev_array.used_len; i++) { 969 show_dev_tc_bpf(&dev_array.devices[i]); 970 ret = show_dev_tc_bpf_classic(sock, nl_pid, 971 &dev_array.devices[i]); 972 if (ret) 973 break; 974 } 975 NET_END_ARRAY("\n"); 976 } 977 978 NET_START_ARRAY("flow_dissector", "%s:\n"); 979 if (attach_info.flow_dissector_id > 0) 980 NET_DUMP_UINT("id", "id %u", attach_info.flow_dissector_id); 981 NET_END_ARRAY("\n"); 982 983 NET_START_ARRAY("netfilter", "%s:\n"); 984 show_link_netfilter(); 985 NET_END_ARRAY("\n"); 986 987 NET_END_OBJECT; 988 if (json_output) 989 jsonw_end_array(json_wtr); 990 991 if (ret) { 992 if (json_output) 993 jsonw_null(json_wtr); 994 libbpf_strerror(ret, err_buf, sizeof(err_buf)); 995 fprintf(stderr, "Error: %s\n", err_buf); 996 } 997 free(dev_array.devices); 998 close(sock); 999 return ret; 1000 } 1001 1002 static int do_help(int argc, char **argv) 1003 { 1004 if (json_output) { 1005 jsonw_null(json_wtr); 1006 return 0; 1007 } 1008 1009 fprintf(stderr, 1010 "Usage: %1$s %2$s { show | list } [dev <devname>]\n" 1011 " %1$s %2$s attach ATTACH_TYPE PROG dev <devname> [ overwrite | prepend ]\n" 1012 " %1$s %2$s detach ATTACH_TYPE dev <devname>\n" 1013 " %1$s %2$s help\n" 1014 "\n" 1015 " " HELP_SPEC_PROGRAM "\n" 1016 " ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload | tcx_ingress\n" 1017 " | tcx_egress }\n" 1018 " " HELP_SPEC_OPTIONS " }\n" 1019 "\n" 1020 "Note: Only xdp, tcx, tc, netkit, flow_dissector and netfilter attachments\n" 1021 " are currently supported.\n" 1022 " For progs attached to cgroups, use \"bpftool cgroup\"\n" 1023 " to dump program attachments. For program types\n" 1024 " sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n" 1025 " consult iproute2.\n" 1026 "", 1027 bin_name, argv[-2]); 1028 1029 return 0; 1030 } 1031 1032 static const struct cmd cmds[] = { 1033 { "show", do_show }, 1034 { "list", do_show }, 1035 { "attach", do_attach }, 1036 { "detach", do_detach }, 1037 { "help", do_help }, 1038 { 0 } 1039 }; 1040 1041 int do_net(int argc, char **argv) 1042 { 1043 return cmd_select(cmds, argc, argv, do_help); 1044 } 1045