1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 // Copyright (C) 2018 Facebook
12 #include <bpf/libbpf.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/socket.h>
17 #include <linux/tc_act/tc_bpf.h>
18 #include <sys/socket.h>
20 #include <sys/types.h>
22 #include "bpf/nlattr.h"
24 #include "netlink_dumper.h"
27 #define SOL_NETLINK 270
30 struct ip_devname_ifindex {
36 struct ip_devname_ifindex *devices;
42 struct tc_kind_handle {
48 struct tc_kind_handle *handle_array;
60 struct bpf_attach_info {
61 __u32 flow_dissector_id;
64 enum net_attach_type {
66 NET_ATTACH_TYPE_XDP_GENERIC,
67 NET_ATTACH_TYPE_XDP_DRIVER,
68 NET_ATTACH_TYPE_XDP_OFFLOAD,
71 static const char * const attach_type_strings[] = {
72 [NET_ATTACH_TYPE_XDP] = "xdp",
73 [NET_ATTACH_TYPE_XDP_GENERIC] = "xdpgeneric",
74 [NET_ATTACH_TYPE_XDP_DRIVER] = "xdpdrv",
75 [NET_ATTACH_TYPE_XDP_OFFLOAD] = "xdpoffload",
78 const size_t net_attach_type_size = ARRAY_SIZE(attach_type_strings);
80 static enum net_attach_type parse_attach_type(const char *str)
82 enum net_attach_type type;
84 for (type = 0; type < net_attach_type_size; type++) {
85 if (attach_type_strings[type] &&
86 is_prefix(str, attach_type_strings[type]))
90 return net_attach_type_size;
93 typedef int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
95 typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, dump_nlmsg_t, void *cookie);
97 static int netlink_open(__u32 *nl_pid)
99 struct sockaddr_nl sa;
104 memset(&sa, 0, sizeof(sa));
105 sa.nl_family = AF_NETLINK;
107 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
111 if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
112 &one, sizeof(one)) < 0) {
113 p_err("Netlink error reporting not supported");
116 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
121 addrlen = sizeof(sa);
122 if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
127 if (addrlen != sizeof(sa)) {
128 ret = -LIBBPF_ERRNO__INTERNAL;
140 static int netlink_recv(int sock, __u32 nl_pid, __u32 seq,
141 __dump_nlmsg_t _fn, dump_nlmsg_t fn,
144 bool multipart = true;
145 struct nlmsgerr *err;
152 len = recv(sock, buf, sizeof(buf), 0);
161 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
162 nh = NLMSG_NEXT(nh, len)) {
163 if (nh->nlmsg_pid != nl_pid) {
164 ret = -LIBBPF_ERRNO__WRNGPID;
167 if (nh->nlmsg_seq != seq) {
168 ret = -LIBBPF_ERRNO__INVSEQ;
171 if (nh->nlmsg_flags & NLM_F_MULTI)
173 switch (nh->nlmsg_type) {
175 err = (struct nlmsgerr *)NLMSG_DATA(nh);
179 libbpf_nla_dump_errormsg(nh);
187 ret = _fn(nh, fn, cookie);
198 static int __dump_class_nlmsg(struct nlmsghdr *nlh,
199 dump_nlmsg_t dump_class_nlmsg,
202 struct nlattr *tb[TCA_MAX + 1], *attr;
203 struct tcmsg *t = NLMSG_DATA(nlh);
206 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
207 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
208 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
209 return -LIBBPF_ERRNO__NLPARSE;
211 return dump_class_nlmsg(cookie, t, tb);
214 static int netlink_get_class(int sock, unsigned int nl_pid, int ifindex,
215 dump_nlmsg_t dump_class_nlmsg, void *cookie)
221 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
222 .nlh.nlmsg_type = RTM_GETTCLASS,
223 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
224 .t.tcm_family = AF_UNSPEC,
225 .t.tcm_ifindex = ifindex,
227 int seq = time(NULL);
229 req.nlh.nlmsg_seq = seq;
230 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
233 return netlink_recv(sock, nl_pid, seq, __dump_class_nlmsg,
234 dump_class_nlmsg, cookie);
237 static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh,
238 dump_nlmsg_t dump_qdisc_nlmsg,
241 struct nlattr *tb[TCA_MAX + 1], *attr;
242 struct tcmsg *t = NLMSG_DATA(nlh);
245 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
246 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
247 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
248 return -LIBBPF_ERRNO__NLPARSE;
250 return dump_qdisc_nlmsg(cookie, t, tb);
253 static int netlink_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
254 dump_nlmsg_t dump_qdisc_nlmsg, void *cookie)
260 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
261 .nlh.nlmsg_type = RTM_GETQDISC,
262 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
263 .t.tcm_family = AF_UNSPEC,
264 .t.tcm_ifindex = ifindex,
266 int seq = time(NULL);
268 req.nlh.nlmsg_seq = seq;
269 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
272 return netlink_recv(sock, nl_pid, seq, __dump_qdisc_nlmsg,
273 dump_qdisc_nlmsg, cookie);
276 static int __dump_filter_nlmsg(struct nlmsghdr *nlh,
277 dump_nlmsg_t dump_filter_nlmsg,
280 struct nlattr *tb[TCA_MAX + 1], *attr;
281 struct tcmsg *t = NLMSG_DATA(nlh);
284 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
285 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
286 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
287 return -LIBBPF_ERRNO__NLPARSE;
289 return dump_filter_nlmsg(cookie, t, tb);
292 static int netlink_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
293 dump_nlmsg_t dump_filter_nlmsg, void *cookie)
299 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
300 .nlh.nlmsg_type = RTM_GETTFILTER,
301 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
302 .t.tcm_family = AF_UNSPEC,
303 .t.tcm_ifindex = ifindex,
304 .t.tcm_parent = handle,
306 int seq = time(NULL);
308 req.nlh.nlmsg_seq = seq;
309 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
312 return netlink_recv(sock, nl_pid, seq, __dump_filter_nlmsg,
313 dump_filter_nlmsg, cookie);
316 static int __dump_link_nlmsg(struct nlmsghdr *nlh,
317 dump_nlmsg_t dump_link_nlmsg, void *cookie)
319 struct nlattr *tb[IFLA_MAX + 1], *attr;
320 struct ifinfomsg *ifi = NLMSG_DATA(nlh);
323 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
324 attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
325 if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0)
326 return -LIBBPF_ERRNO__NLPARSE;
328 return dump_link_nlmsg(cookie, ifi, tb);
331 static int netlink_get_link(int sock, unsigned int nl_pid,
332 dump_nlmsg_t dump_link_nlmsg, void *cookie)
336 struct ifinfomsg ifm;
338 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
339 .nlh.nlmsg_type = RTM_GETLINK,
340 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
341 .ifm.ifi_family = AF_PACKET,
343 int seq = time(NULL);
345 req.nlh.nlmsg_seq = seq;
346 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
349 return netlink_recv(sock, nl_pid, seq, __dump_link_nlmsg,
350 dump_link_nlmsg, cookie);
353 static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
355 struct bpf_netdev_t *netinfo = cookie;
356 struct ifinfomsg *ifinfo = msg;
358 if (netinfo->filter_idx > 0 && netinfo->filter_idx != ifinfo->ifi_index)
361 if (netinfo->used_len == netinfo->array_len) {
362 netinfo->devices = realloc(netinfo->devices,
363 (netinfo->array_len + 16) *
364 sizeof(struct ip_devname_ifindex));
365 if (!netinfo->devices)
368 netinfo->array_len += 16;
370 netinfo->devices[netinfo->used_len].ifindex = ifinfo->ifi_index;
371 snprintf(netinfo->devices[netinfo->used_len].devname,
372 sizeof(netinfo->devices[netinfo->used_len].devname),
375 ? libbpf_nla_getattr_str(tb[IFLA_IFNAME])
379 return do_xdp_dump(ifinfo, tb);
382 static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
384 struct bpf_tcinfo_t *tcinfo = cookie;
385 struct tcmsg *info = msg;
387 if (tcinfo->is_qdisc) {
388 /* skip clsact qdisc */
390 strcmp(libbpf_nla_data(tb[TCA_KIND]), "clsact") == 0)
392 if (info->tcm_handle == 0)
396 if (tcinfo->used_len == tcinfo->array_len) {
397 tcinfo->handle_array = realloc(tcinfo->handle_array,
398 (tcinfo->array_len + 16) * sizeof(struct tc_kind_handle));
399 if (!tcinfo->handle_array)
402 tcinfo->array_len += 16;
404 tcinfo->handle_array[tcinfo->used_len].handle = info->tcm_handle;
405 snprintf(tcinfo->handle_array[tcinfo->used_len].kind,
406 sizeof(tcinfo->handle_array[tcinfo->used_len].kind),
409 ? libbpf_nla_getattr_str(tb[TCA_KIND])
416 static int dump_filter_nlmsg(void *cookie, void *msg, struct nlattr **tb)
418 const struct bpf_filter_t *filter_info = cookie;
420 return do_filter_dump((struct tcmsg *)msg, tb, filter_info->kind,
421 filter_info->devname, filter_info->ifindex);
424 static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
425 struct ip_devname_ifindex *dev)
427 struct bpf_filter_t filter_info;
428 struct bpf_tcinfo_t tcinfo;
429 int i, handle, ret = 0;
431 tcinfo.handle_array = NULL;
433 tcinfo.array_len = 0;
435 tcinfo.is_qdisc = false;
436 ret = netlink_get_class(sock, nl_pid, dev->ifindex,
437 dump_class_qdisc_nlmsg, &tcinfo);
441 tcinfo.is_qdisc = true;
442 ret = netlink_get_qdisc(sock, nl_pid, dev->ifindex,
443 dump_class_qdisc_nlmsg, &tcinfo);
447 filter_info.devname = dev->devname;
448 filter_info.ifindex = dev->ifindex;
449 for (i = 0; i < tcinfo.used_len; i++) {
450 filter_info.kind = tcinfo.handle_array[i].kind;
451 ret = netlink_get_filter(sock, nl_pid, dev->ifindex,
452 tcinfo.handle_array[i].handle,
453 dump_filter_nlmsg, &filter_info);
458 /* root, ingress and egress handle */
460 filter_info.kind = "root";
461 ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
462 dump_filter_nlmsg, &filter_info);
466 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
467 filter_info.kind = "clsact/ingress";
468 ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
469 dump_filter_nlmsg, &filter_info);
473 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS);
474 filter_info.kind = "clsact/egress";
475 ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
476 dump_filter_nlmsg, &filter_info);
481 free(tcinfo.handle_array);
485 static int query_flow_dissector(struct bpf_attach_info *attach_info)
493 fd = open("/proc/self/ns/net", O_RDONLY);
495 p_err("can't open /proc/self/ns/net: %s",
499 prog_cnt = ARRAY_SIZE(prog_ids);
500 err = bpf_prog_query(fd, BPF_FLOW_DISSECTOR, 0,
501 &attach_flags, prog_ids, &prog_cnt);
504 if (errno == EINVAL) {
505 /* Older kernel's don't support querying
506 * flow dissector programs.
511 p_err("can't query prog: %s", strerror(errno));
516 attach_info->flow_dissector_id = prog_ids[0];
521 static int net_parse_dev(int *argc, char ***argv)
525 if (is_prefix(**argv, "dev")) {
528 ifindex = if_nametoindex(**argv);
530 p_err("invalid devname %s", **argv);
534 p_err("expected 'dev', got: '%s'?", **argv);
541 static int do_attach_detach_xdp(int progfd, enum net_attach_type attach_type,
542 int ifindex, bool overwrite)
547 flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
548 if (attach_type == NET_ATTACH_TYPE_XDP_GENERIC)
549 flags |= XDP_FLAGS_SKB_MODE;
550 if (attach_type == NET_ATTACH_TYPE_XDP_DRIVER)
551 flags |= XDP_FLAGS_DRV_MODE;
552 if (attach_type == NET_ATTACH_TYPE_XDP_OFFLOAD)
553 flags |= XDP_FLAGS_HW_MODE;
555 return bpf_set_link_xdp_fd(ifindex, progfd, flags);
558 static int do_attach(int argc, char **argv)
560 enum net_attach_type attach_type;
561 int progfd, ifindex, err = 0;
562 bool overwrite = false;
564 /* parse attach args */
568 attach_type = parse_attach_type(*argv);
569 if (attach_type == net_attach_type_size) {
570 p_err("invalid net attach/detach type: %s", *argv);
575 progfd = prog_parse_fd(&argc, &argv);
579 ifindex = net_parse_dev(&argc, &argv);
586 if (is_prefix(*argv, "overwrite")) {
589 p_err("expected 'overwrite', got: '%s'?", *argv);
595 /* attach xdp prog */
596 if (is_prefix("xdp", attach_type_strings[attach_type]))
597 err = do_attach_detach_xdp(progfd, attach_type, ifindex,
601 p_err("interface %s attach failed: %s",
602 attach_type_strings[attach_type], strerror(-err));
607 jsonw_null(json_wtr);
612 static int do_detach(int argc, char **argv)
614 enum net_attach_type attach_type;
615 int progfd, ifindex, err = 0;
617 /* parse detach args */
621 attach_type = parse_attach_type(*argv);
622 if (attach_type == net_attach_type_size) {
623 p_err("invalid net attach/detach type: %s", *argv);
628 ifindex = net_parse_dev(&argc, &argv);
632 /* detach xdp prog */
634 if (is_prefix("xdp", attach_type_strings[attach_type]))
635 err = do_attach_detach_xdp(progfd, attach_type, ifindex, NULL);
638 p_err("interface %s detach failed: %s",
639 attach_type_strings[attach_type], strerror(-err));
644 jsonw_null(json_wtr);
649 static int do_show(int argc, char **argv)
651 struct bpf_attach_info attach_info = {};
652 int i, sock, ret, filter_idx = -1;
653 struct bpf_netdev_t dev_array;
654 unsigned int nl_pid = 0;
658 filter_idx = net_parse_dev(&argc, &argv);
661 } else if (argc != 0) {
665 ret = query_flow_dissector(&attach_info);
669 sock = netlink_open(&nl_pid);
671 fprintf(stderr, "failed to open netlink sock\n");
675 dev_array.devices = NULL;
676 dev_array.used_len = 0;
677 dev_array.array_len = 0;
678 dev_array.filter_idx = filter_idx;
681 jsonw_start_array(json_wtr);
683 NET_START_ARRAY("xdp", "%s:\n");
684 ret = netlink_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array);
688 NET_START_ARRAY("tc", "%s:\n");
689 for (i = 0; i < dev_array.used_len; i++) {
690 ret = show_dev_tc_bpf(sock, nl_pid,
691 &dev_array.devices[i]);
698 NET_START_ARRAY("flow_dissector", "%s:\n");
699 if (attach_info.flow_dissector_id > 0)
700 NET_DUMP_UINT("id", "id %u", attach_info.flow_dissector_id);
705 jsonw_end_array(json_wtr);
709 jsonw_null(json_wtr);
710 libbpf_strerror(ret, err_buf, sizeof(err_buf));
711 fprintf(stderr, "Error: %s\n", err_buf);
713 free(dev_array.devices);
718 static int do_help(int argc, char **argv)
721 jsonw_null(json_wtr);
726 "Usage: %1$s %2$s { show | list } [dev <devname>]\n"
727 " %1$s %2$s attach ATTACH_TYPE PROG dev <devname> [ overwrite ]\n"
728 " %1$s %2$s detach ATTACH_TYPE dev <devname>\n"
731 " " HELP_SPEC_PROGRAM "\n"
732 " ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload }\n"
734 "Note: Only xdp and tc attachments are supported now.\n"
735 " For progs attached to cgroups, use \"bpftool cgroup\"\n"
736 " to dump program attachments. For program types\n"
737 " sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n"
738 " consult iproute2.\n"
745 static const struct cmd cmds[] = {
748 { "attach", do_attach },
749 { "detach", do_detach },
754 int do_net(int argc, char **argv)
756 return cmd_select(cmds, argc, argv, do_help);