1907b2236SJakub Kicinski // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2f6f3bac0SYonghong Song // Copyright (C) 2018 Facebook
3f6f3bac0SYonghong Song
4b3d84af7SQuentin Monnet #ifndef _GNU_SOURCE
5f6f3bac0SYonghong Song #define _GNU_SOURCE
6b3d84af7SQuentin Monnet #endif
77900efc1SYonghong Song #include <errno.h>
87f0c57feSStanislav Fomichev #include <fcntl.h>
9f6f3bac0SYonghong Song #include <stdlib.h>
10f6f3bac0SYonghong Song #include <string.h>
117084566aSAndrii Nakryiko #include <time.h>
12f6f3bac0SYonghong Song #include <unistd.h>
13229c3b47SToke Høiland-Jørgensen #include <bpf/bpf.h>
14229c3b47SToke Høiland-Jørgensen #include <bpf/libbpf.h>
15f6f3bac0SYonghong Song #include <net/if.h>
16f6f3bac0SYonghong Song #include <linux/rtnetlink.h>
177084566aSAndrii Nakryiko #include <linux/socket.h>
18f6f3bac0SYonghong Song #include <linux/tc_act/tc_bpf.h>
19f6f3bac0SYonghong Song #include <sys/socket.h>
207f0c57feSStanislav Fomichev #include <sys/stat.h>
217f0c57feSStanislav Fomichev #include <sys/types.h>
22f6f3bac0SYonghong Song
23229c3b47SToke Høiland-Jørgensen #include "bpf/nlattr.h"
24f6f3bac0SYonghong Song #include "main.h"
25f6f3bac0SYonghong Song #include "netlink_dumper.h"
26f6f3bac0SYonghong Song
277084566aSAndrii Nakryiko #ifndef SOL_NETLINK
287084566aSAndrii Nakryiko #define SOL_NETLINK 270
297084566aSAndrii Nakryiko #endif
307084566aSAndrii Nakryiko
317900efc1SYonghong Song struct ip_devname_ifindex {
327900efc1SYonghong Song char devname[64];
337900efc1SYonghong Song int ifindex;
347900efc1SYonghong Song };
357900efc1SYonghong Song
36f6f3bac0SYonghong Song struct bpf_netdev_t {
377900efc1SYonghong Song struct ip_devname_ifindex *devices;
38f6f3bac0SYonghong Song int used_len;
39f6f3bac0SYonghong Song int array_len;
40f6f3bac0SYonghong Song int filter_idx;
41f6f3bac0SYonghong Song };
42f6f3bac0SYonghong Song
43f6f3bac0SYonghong Song struct tc_kind_handle {
44f6f3bac0SYonghong Song char kind[64];
45f6f3bac0SYonghong Song int handle;
46f6f3bac0SYonghong Song };
47f6f3bac0SYonghong Song
48f6f3bac0SYonghong Song struct bpf_tcinfo_t {
49f6f3bac0SYonghong Song struct tc_kind_handle *handle_array;
50f6f3bac0SYonghong Song int used_len;
51f6f3bac0SYonghong Song int array_len;
52f6f3bac0SYonghong Song bool is_qdisc;
53f6f3bac0SYonghong Song };
54f6f3bac0SYonghong Song
557900efc1SYonghong Song struct bpf_filter_t {
567900efc1SYonghong Song const char *kind;
577900efc1SYonghong Song const char *devname;
587900efc1SYonghong Song int ifindex;
597900efc1SYonghong Song };
607900efc1SYonghong Song
617f0c57feSStanislav Fomichev struct bpf_attach_info {
627f0c57feSStanislav Fomichev __u32 flow_dissector_id;
637f0c57feSStanislav Fomichev };
647f0c57feSStanislav Fomichev
6504949cccSDaniel T. Lee enum net_attach_type {
6604949cccSDaniel T. Lee NET_ATTACH_TYPE_XDP,
6704949cccSDaniel T. Lee NET_ATTACH_TYPE_XDP_GENERIC,
6804949cccSDaniel T. Lee NET_ATTACH_TYPE_XDP_DRIVER,
6904949cccSDaniel T. Lee NET_ATTACH_TYPE_XDP_OFFLOAD,
703b9d4feeSTao Chen NET_ATTACH_TYPE_TCX_INGRESS,
713b9d4feeSTao Chen NET_ATTACH_TYPE_TCX_EGRESS,
7204949cccSDaniel T. Lee };
7304949cccSDaniel T. Lee
7404949cccSDaniel T. Lee static const char * const attach_type_strings[] = {
7504949cccSDaniel T. Lee [NET_ATTACH_TYPE_XDP] = "xdp",
7604949cccSDaniel T. Lee [NET_ATTACH_TYPE_XDP_GENERIC] = "xdpgeneric",
7704949cccSDaniel T. Lee [NET_ATTACH_TYPE_XDP_DRIVER] = "xdpdrv",
7804949cccSDaniel T. Lee [NET_ATTACH_TYPE_XDP_OFFLOAD] = "xdpoffload",
793b9d4feeSTao Chen [NET_ATTACH_TYPE_TCX_INGRESS] = "tcx_ingress",
803b9d4feeSTao Chen [NET_ATTACH_TYPE_TCX_EGRESS] = "tcx_egress",
8104949cccSDaniel T. Lee };
8204949cccSDaniel T. Lee
8357c61da8SDaniel Borkmann static const char * const attach_loc_strings[] = {
8457c61da8SDaniel Borkmann [BPF_TCX_INGRESS] = "tcx/ingress",
8557c61da8SDaniel Borkmann [BPF_TCX_EGRESS] = "tcx/egress",
86bec981a4SDaniel Borkmann [BPF_NETKIT_PRIMARY] = "netkit/primary",
87bec981a4SDaniel Borkmann [BPF_NETKIT_PEER] = "netkit/peer",
8857c61da8SDaniel Borkmann };
8957c61da8SDaniel Borkmann
9004949cccSDaniel T. Lee const size_t net_attach_type_size = ARRAY_SIZE(attach_type_strings);
9104949cccSDaniel T. Lee
parse_attach_type(const char * str)9204949cccSDaniel T. Lee static enum net_attach_type parse_attach_type(const char *str)
9304949cccSDaniel T. Lee {
9404949cccSDaniel T. Lee enum net_attach_type type;
9504949cccSDaniel T. Lee
9604949cccSDaniel T. Lee for (type = 0; type < net_attach_type_size; type++) {
9704949cccSDaniel T. Lee if (attach_type_strings[type] &&
9804949cccSDaniel T. Lee is_prefix(str, attach_type_strings[type]))
9904949cccSDaniel T. Lee return type;
10004949cccSDaniel T. Lee }
10104949cccSDaniel T. Lee
10204949cccSDaniel T. Lee return net_attach_type_size;
10304949cccSDaniel T. Lee }
10404949cccSDaniel T. Lee
1057084566aSAndrii Nakryiko typedef int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
1067084566aSAndrii Nakryiko
1077084566aSAndrii Nakryiko typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, dump_nlmsg_t, void *cookie);
1087084566aSAndrii Nakryiko
netlink_open(__u32 * nl_pid)1097084566aSAndrii Nakryiko static int netlink_open(__u32 *nl_pid)
1107084566aSAndrii Nakryiko {
1117084566aSAndrii Nakryiko struct sockaddr_nl sa;
1127084566aSAndrii Nakryiko socklen_t addrlen;
1137084566aSAndrii Nakryiko int one = 1, ret;
1147084566aSAndrii Nakryiko int sock;
1157084566aSAndrii Nakryiko
1167084566aSAndrii Nakryiko memset(&sa, 0, sizeof(sa));
1177084566aSAndrii Nakryiko sa.nl_family = AF_NETLINK;
1187084566aSAndrii Nakryiko
1197084566aSAndrii Nakryiko sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1207084566aSAndrii Nakryiko if (sock < 0)
1217084566aSAndrii Nakryiko return -errno;
1227084566aSAndrii Nakryiko
1237084566aSAndrii Nakryiko if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
1247084566aSAndrii Nakryiko &one, sizeof(one)) < 0) {
1257084566aSAndrii Nakryiko p_err("Netlink error reporting not supported");
1267084566aSAndrii Nakryiko }
1277084566aSAndrii Nakryiko
1287084566aSAndrii Nakryiko if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
1297084566aSAndrii Nakryiko ret = -errno;
1307084566aSAndrii Nakryiko goto cleanup;
1317084566aSAndrii Nakryiko }
1327084566aSAndrii Nakryiko
1337084566aSAndrii Nakryiko addrlen = sizeof(sa);
1347084566aSAndrii Nakryiko if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
1357084566aSAndrii Nakryiko ret = -errno;
1367084566aSAndrii Nakryiko goto cleanup;
1377084566aSAndrii Nakryiko }
1387084566aSAndrii Nakryiko
1397084566aSAndrii Nakryiko if (addrlen != sizeof(sa)) {
1407084566aSAndrii Nakryiko ret = -LIBBPF_ERRNO__INTERNAL;
1417084566aSAndrii Nakryiko goto cleanup;
1427084566aSAndrii Nakryiko }
1437084566aSAndrii Nakryiko
1447084566aSAndrii Nakryiko *nl_pid = sa.nl_pid;
1457084566aSAndrii Nakryiko return sock;
1467084566aSAndrii Nakryiko
1477084566aSAndrii Nakryiko cleanup:
1487084566aSAndrii Nakryiko close(sock);
1497084566aSAndrii Nakryiko return ret;
1507084566aSAndrii Nakryiko }
1517084566aSAndrii Nakryiko
netlink_recv(int sock,__u32 nl_pid,__u32 seq,__dump_nlmsg_t _fn,dump_nlmsg_t fn,void * cookie)1527084566aSAndrii Nakryiko static int netlink_recv(int sock, __u32 nl_pid, __u32 seq,
1537084566aSAndrii Nakryiko __dump_nlmsg_t _fn, dump_nlmsg_t fn,
1547084566aSAndrii Nakryiko void *cookie)
1557084566aSAndrii Nakryiko {
1567084566aSAndrii Nakryiko bool multipart = true;
1577084566aSAndrii Nakryiko struct nlmsgerr *err;
1587084566aSAndrii Nakryiko struct nlmsghdr *nh;
1597084566aSAndrii Nakryiko char buf[4096];
1607084566aSAndrii Nakryiko int len, ret;
1617084566aSAndrii Nakryiko
1627084566aSAndrii Nakryiko while (multipart) {
1637084566aSAndrii Nakryiko multipart = false;
1647084566aSAndrii Nakryiko len = recv(sock, buf, sizeof(buf), 0);
1657084566aSAndrii Nakryiko if (len < 0) {
1667084566aSAndrii Nakryiko ret = -errno;
1677084566aSAndrii Nakryiko goto done;
1687084566aSAndrii Nakryiko }
1697084566aSAndrii Nakryiko
1707084566aSAndrii Nakryiko if (len == 0)
1717084566aSAndrii Nakryiko break;
1727084566aSAndrii Nakryiko
1738af50142SYonghong Song for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, (unsigned int)len);
1747084566aSAndrii Nakryiko nh = NLMSG_NEXT(nh, len)) {
1757084566aSAndrii Nakryiko if (nh->nlmsg_pid != nl_pid) {
1767084566aSAndrii Nakryiko ret = -LIBBPF_ERRNO__WRNGPID;
1777084566aSAndrii Nakryiko goto done;
1787084566aSAndrii Nakryiko }
1797084566aSAndrii Nakryiko if (nh->nlmsg_seq != seq) {
1807084566aSAndrii Nakryiko ret = -LIBBPF_ERRNO__INVSEQ;
1817084566aSAndrii Nakryiko goto done;
1827084566aSAndrii Nakryiko }
1837084566aSAndrii Nakryiko if (nh->nlmsg_flags & NLM_F_MULTI)
1847084566aSAndrii Nakryiko multipart = true;
1857084566aSAndrii Nakryiko switch (nh->nlmsg_type) {
1867084566aSAndrii Nakryiko case NLMSG_ERROR:
1877084566aSAndrii Nakryiko err = (struct nlmsgerr *)NLMSG_DATA(nh);
1887084566aSAndrii Nakryiko if (!err->error)
1897084566aSAndrii Nakryiko continue;
1907084566aSAndrii Nakryiko ret = err->error;
1917084566aSAndrii Nakryiko libbpf_nla_dump_errormsg(nh);
1927084566aSAndrii Nakryiko goto done;
1937084566aSAndrii Nakryiko case NLMSG_DONE:
1947084566aSAndrii Nakryiko return 0;
1957084566aSAndrii Nakryiko default:
1967084566aSAndrii Nakryiko break;
1977084566aSAndrii Nakryiko }
1987084566aSAndrii Nakryiko if (_fn) {
1997084566aSAndrii Nakryiko ret = _fn(nh, fn, cookie);
2007084566aSAndrii Nakryiko if (ret)
2017084566aSAndrii Nakryiko return ret;
2027084566aSAndrii Nakryiko }
2037084566aSAndrii Nakryiko }
2047084566aSAndrii Nakryiko }
2057084566aSAndrii Nakryiko ret = 0;
2067084566aSAndrii Nakryiko done:
2077084566aSAndrii Nakryiko return ret;
2087084566aSAndrii Nakryiko }
2097084566aSAndrii Nakryiko
__dump_class_nlmsg(struct nlmsghdr * nlh,dump_nlmsg_t dump_class_nlmsg,void * cookie)2107084566aSAndrii Nakryiko static int __dump_class_nlmsg(struct nlmsghdr *nlh,
2117084566aSAndrii Nakryiko dump_nlmsg_t dump_class_nlmsg,
2127084566aSAndrii Nakryiko void *cookie)
2137084566aSAndrii Nakryiko {
2147084566aSAndrii Nakryiko struct nlattr *tb[TCA_MAX + 1], *attr;
2157084566aSAndrii Nakryiko struct tcmsg *t = NLMSG_DATA(nlh);
2167084566aSAndrii Nakryiko int len;
2177084566aSAndrii Nakryiko
2187084566aSAndrii Nakryiko len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
2197084566aSAndrii Nakryiko attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
2207084566aSAndrii Nakryiko if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
2217084566aSAndrii Nakryiko return -LIBBPF_ERRNO__NLPARSE;
2227084566aSAndrii Nakryiko
2237084566aSAndrii Nakryiko return dump_class_nlmsg(cookie, t, tb);
2247084566aSAndrii Nakryiko }
2257084566aSAndrii Nakryiko
netlink_get_class(int sock,unsigned int nl_pid,int ifindex,dump_nlmsg_t dump_class_nlmsg,void * cookie)2267084566aSAndrii Nakryiko static int netlink_get_class(int sock, unsigned int nl_pid, int ifindex,
2277084566aSAndrii Nakryiko dump_nlmsg_t dump_class_nlmsg, void *cookie)
2287084566aSAndrii Nakryiko {
2297084566aSAndrii Nakryiko struct {
2307084566aSAndrii Nakryiko struct nlmsghdr nlh;
2317084566aSAndrii Nakryiko struct tcmsg t;
2327084566aSAndrii Nakryiko } req = {
2337084566aSAndrii Nakryiko .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
2347084566aSAndrii Nakryiko .nlh.nlmsg_type = RTM_GETTCLASS,
2357084566aSAndrii Nakryiko .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
2367084566aSAndrii Nakryiko .t.tcm_family = AF_UNSPEC,
2377084566aSAndrii Nakryiko .t.tcm_ifindex = ifindex,
2387084566aSAndrii Nakryiko };
2397084566aSAndrii Nakryiko int seq = time(NULL);
2407084566aSAndrii Nakryiko
2417084566aSAndrii Nakryiko req.nlh.nlmsg_seq = seq;
2427084566aSAndrii Nakryiko if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
2437084566aSAndrii Nakryiko return -errno;
2447084566aSAndrii Nakryiko
2457084566aSAndrii Nakryiko return netlink_recv(sock, nl_pid, seq, __dump_class_nlmsg,
2467084566aSAndrii Nakryiko dump_class_nlmsg, cookie);
2477084566aSAndrii Nakryiko }
2487084566aSAndrii Nakryiko
__dump_qdisc_nlmsg(struct nlmsghdr * nlh,dump_nlmsg_t dump_qdisc_nlmsg,void * cookie)2497084566aSAndrii Nakryiko static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh,
2507084566aSAndrii Nakryiko dump_nlmsg_t dump_qdisc_nlmsg,
2517084566aSAndrii Nakryiko void *cookie)
2527084566aSAndrii Nakryiko {
2537084566aSAndrii Nakryiko struct nlattr *tb[TCA_MAX + 1], *attr;
2547084566aSAndrii Nakryiko struct tcmsg *t = NLMSG_DATA(nlh);
2557084566aSAndrii Nakryiko int len;
2567084566aSAndrii Nakryiko
2577084566aSAndrii Nakryiko len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
2587084566aSAndrii Nakryiko attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
2597084566aSAndrii Nakryiko if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
2607084566aSAndrii Nakryiko return -LIBBPF_ERRNO__NLPARSE;
2617084566aSAndrii Nakryiko
2627084566aSAndrii Nakryiko return dump_qdisc_nlmsg(cookie, t, tb);
2637084566aSAndrii Nakryiko }
2647084566aSAndrii Nakryiko
netlink_get_qdisc(int sock,unsigned int nl_pid,int ifindex,dump_nlmsg_t dump_qdisc_nlmsg,void * cookie)2657084566aSAndrii Nakryiko static int netlink_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
2667084566aSAndrii Nakryiko dump_nlmsg_t dump_qdisc_nlmsg, void *cookie)
2677084566aSAndrii Nakryiko {
2687084566aSAndrii Nakryiko struct {
2697084566aSAndrii Nakryiko struct nlmsghdr nlh;
2707084566aSAndrii Nakryiko struct tcmsg t;
2717084566aSAndrii Nakryiko } req = {
2727084566aSAndrii Nakryiko .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
2737084566aSAndrii Nakryiko .nlh.nlmsg_type = RTM_GETQDISC,
2747084566aSAndrii Nakryiko .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
2757084566aSAndrii Nakryiko .t.tcm_family = AF_UNSPEC,
2767084566aSAndrii Nakryiko .t.tcm_ifindex = ifindex,
2777084566aSAndrii Nakryiko };
2787084566aSAndrii Nakryiko int seq = time(NULL);
2797084566aSAndrii Nakryiko
2807084566aSAndrii Nakryiko req.nlh.nlmsg_seq = seq;
2817084566aSAndrii Nakryiko if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
2827084566aSAndrii Nakryiko return -errno;
2837084566aSAndrii Nakryiko
2847084566aSAndrii Nakryiko return netlink_recv(sock, nl_pid, seq, __dump_qdisc_nlmsg,
2857084566aSAndrii Nakryiko dump_qdisc_nlmsg, cookie);
2867084566aSAndrii Nakryiko }
2877084566aSAndrii Nakryiko
__dump_filter_nlmsg(struct nlmsghdr * nlh,dump_nlmsg_t dump_filter_nlmsg,void * cookie)2887084566aSAndrii Nakryiko static int __dump_filter_nlmsg(struct nlmsghdr *nlh,
2897084566aSAndrii Nakryiko dump_nlmsg_t dump_filter_nlmsg,
2907084566aSAndrii Nakryiko void *cookie)
2917084566aSAndrii Nakryiko {
2927084566aSAndrii Nakryiko struct nlattr *tb[TCA_MAX + 1], *attr;
2937084566aSAndrii Nakryiko struct tcmsg *t = NLMSG_DATA(nlh);
2947084566aSAndrii Nakryiko int len;
2957084566aSAndrii Nakryiko
2967084566aSAndrii Nakryiko len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
2977084566aSAndrii Nakryiko attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
2987084566aSAndrii Nakryiko if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
2997084566aSAndrii Nakryiko return -LIBBPF_ERRNO__NLPARSE;
3007084566aSAndrii Nakryiko
3017084566aSAndrii Nakryiko return dump_filter_nlmsg(cookie, t, tb);
3027084566aSAndrii Nakryiko }
3037084566aSAndrii Nakryiko
netlink_get_filter(int sock,unsigned int nl_pid,int ifindex,int handle,dump_nlmsg_t dump_filter_nlmsg,void * cookie)3047084566aSAndrii Nakryiko static int netlink_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
3057084566aSAndrii Nakryiko dump_nlmsg_t dump_filter_nlmsg, void *cookie)
3067084566aSAndrii Nakryiko {
3077084566aSAndrii Nakryiko struct {
3087084566aSAndrii Nakryiko struct nlmsghdr nlh;
3097084566aSAndrii Nakryiko struct tcmsg t;
3107084566aSAndrii Nakryiko } req = {
3117084566aSAndrii Nakryiko .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
3127084566aSAndrii Nakryiko .nlh.nlmsg_type = RTM_GETTFILTER,
3137084566aSAndrii Nakryiko .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
3147084566aSAndrii Nakryiko .t.tcm_family = AF_UNSPEC,
3157084566aSAndrii Nakryiko .t.tcm_ifindex = ifindex,
3167084566aSAndrii Nakryiko .t.tcm_parent = handle,
3177084566aSAndrii Nakryiko };
3187084566aSAndrii Nakryiko int seq = time(NULL);
3197084566aSAndrii Nakryiko
3207084566aSAndrii Nakryiko req.nlh.nlmsg_seq = seq;
3217084566aSAndrii Nakryiko if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
3227084566aSAndrii Nakryiko return -errno;
3237084566aSAndrii Nakryiko
3247084566aSAndrii Nakryiko return netlink_recv(sock, nl_pid, seq, __dump_filter_nlmsg,
3257084566aSAndrii Nakryiko dump_filter_nlmsg, cookie);
3267084566aSAndrii Nakryiko }
3277084566aSAndrii Nakryiko
__dump_link_nlmsg(struct nlmsghdr * nlh,dump_nlmsg_t dump_link_nlmsg,void * cookie)3287084566aSAndrii Nakryiko static int __dump_link_nlmsg(struct nlmsghdr *nlh,
3297084566aSAndrii Nakryiko dump_nlmsg_t dump_link_nlmsg, void *cookie)
3307084566aSAndrii Nakryiko {
3317084566aSAndrii Nakryiko struct nlattr *tb[IFLA_MAX + 1], *attr;
3327084566aSAndrii Nakryiko struct ifinfomsg *ifi = NLMSG_DATA(nlh);
3337084566aSAndrii Nakryiko int len;
3347084566aSAndrii Nakryiko
3357084566aSAndrii Nakryiko len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
3367084566aSAndrii Nakryiko attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
3377084566aSAndrii Nakryiko if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0)
3387084566aSAndrii Nakryiko return -LIBBPF_ERRNO__NLPARSE;
3397084566aSAndrii Nakryiko
3407084566aSAndrii Nakryiko return dump_link_nlmsg(cookie, ifi, tb);
3417084566aSAndrii Nakryiko }
3427084566aSAndrii Nakryiko
netlink_get_link(int sock,unsigned int nl_pid,dump_nlmsg_t dump_link_nlmsg,void * cookie)3437084566aSAndrii Nakryiko static int netlink_get_link(int sock, unsigned int nl_pid,
3447084566aSAndrii Nakryiko dump_nlmsg_t dump_link_nlmsg, void *cookie)
3457084566aSAndrii Nakryiko {
3467084566aSAndrii Nakryiko struct {
3477084566aSAndrii Nakryiko struct nlmsghdr nlh;
3487084566aSAndrii Nakryiko struct ifinfomsg ifm;
3497084566aSAndrii Nakryiko } req = {
3507084566aSAndrii Nakryiko .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
3517084566aSAndrii Nakryiko .nlh.nlmsg_type = RTM_GETLINK,
3527084566aSAndrii Nakryiko .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
3537084566aSAndrii Nakryiko .ifm.ifi_family = AF_PACKET,
3547084566aSAndrii Nakryiko };
3557084566aSAndrii Nakryiko int seq = time(NULL);
3567084566aSAndrii Nakryiko
3577084566aSAndrii Nakryiko req.nlh.nlmsg_seq = seq;
3587084566aSAndrii Nakryiko if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
3597084566aSAndrii Nakryiko return -errno;
3607084566aSAndrii Nakryiko
3617084566aSAndrii Nakryiko return netlink_recv(sock, nl_pid, seq, __dump_link_nlmsg,
3627084566aSAndrii Nakryiko dump_link_nlmsg, cookie);
3637084566aSAndrii Nakryiko }
3647084566aSAndrii Nakryiko
dump_link_nlmsg(void * cookie,void * msg,struct nlattr ** tb)365f6f3bac0SYonghong Song static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
366f6f3bac0SYonghong Song {
367f6f3bac0SYonghong Song struct bpf_netdev_t *netinfo = cookie;
368f6f3bac0SYonghong Song struct ifinfomsg *ifinfo = msg;
369f6f3bac0SYonghong Song
370f6f3bac0SYonghong Song if (netinfo->filter_idx > 0 && netinfo->filter_idx != ifinfo->ifi_index)
371f6f3bac0SYonghong Song return 0;
372f6f3bac0SYonghong Song
373f6f3bac0SYonghong Song if (netinfo->used_len == netinfo->array_len) {
3747900efc1SYonghong Song netinfo->devices = realloc(netinfo->devices,
3757900efc1SYonghong Song (netinfo->array_len + 16) *
3767900efc1SYonghong Song sizeof(struct ip_devname_ifindex));
3777900efc1SYonghong Song if (!netinfo->devices)
3787900efc1SYonghong Song return -ENOMEM;
3797900efc1SYonghong Song
380f6f3bac0SYonghong Song netinfo->array_len += 16;
381f6f3bac0SYonghong Song }
3827900efc1SYonghong Song netinfo->devices[netinfo->used_len].ifindex = ifinfo->ifi_index;
3837900efc1SYonghong Song snprintf(netinfo->devices[netinfo->used_len].devname,
3847900efc1SYonghong Song sizeof(netinfo->devices[netinfo->used_len].devname),
3857900efc1SYonghong Song "%s",
386f04bc8a4SAndrey Ignatov tb[IFLA_IFNAME]
387f04bc8a4SAndrey Ignatov ? libbpf_nla_getattr_str(tb[IFLA_IFNAME])
388f04bc8a4SAndrey Ignatov : "");
3897900efc1SYonghong Song netinfo->used_len++;
390f6f3bac0SYonghong Song
391f6f3bac0SYonghong Song return do_xdp_dump(ifinfo, tb);
392f6f3bac0SYonghong Song }
393f6f3bac0SYonghong Song
dump_class_qdisc_nlmsg(void * cookie,void * msg,struct nlattr ** tb)394f6f3bac0SYonghong Song static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
395f6f3bac0SYonghong Song {
396f6f3bac0SYonghong Song struct bpf_tcinfo_t *tcinfo = cookie;
397f6f3bac0SYonghong Song struct tcmsg *info = msg;
398f6f3bac0SYonghong Song
399f6f3bac0SYonghong Song if (tcinfo->is_qdisc) {
400f6f3bac0SYonghong Song /* skip clsact qdisc */
401f6f3bac0SYonghong Song if (tb[TCA_KIND] &&
402f04bc8a4SAndrey Ignatov strcmp(libbpf_nla_data(tb[TCA_KIND]), "clsact") == 0)
403f6f3bac0SYonghong Song return 0;
404f6f3bac0SYonghong Song if (info->tcm_handle == 0)
405f6f3bac0SYonghong Song return 0;
406f6f3bac0SYonghong Song }
407f6f3bac0SYonghong Song
408f6f3bac0SYonghong Song if (tcinfo->used_len == tcinfo->array_len) {
409f6f3bac0SYonghong Song tcinfo->handle_array = realloc(tcinfo->handle_array,
410f6f3bac0SYonghong Song (tcinfo->array_len + 16) * sizeof(struct tc_kind_handle));
4117900efc1SYonghong Song if (!tcinfo->handle_array)
4127900efc1SYonghong Song return -ENOMEM;
4137900efc1SYonghong Song
414f6f3bac0SYonghong Song tcinfo->array_len += 16;
415f6f3bac0SYonghong Song }
416f6f3bac0SYonghong Song tcinfo->handle_array[tcinfo->used_len].handle = info->tcm_handle;
417f6f3bac0SYonghong Song snprintf(tcinfo->handle_array[tcinfo->used_len].kind,
418f6f3bac0SYonghong Song sizeof(tcinfo->handle_array[tcinfo->used_len].kind),
4197900efc1SYonghong Song "%s",
420f04bc8a4SAndrey Ignatov tb[TCA_KIND]
421f04bc8a4SAndrey Ignatov ? libbpf_nla_getattr_str(tb[TCA_KIND])
422f04bc8a4SAndrey Ignatov : "unknown");
423f6f3bac0SYonghong Song tcinfo->used_len++;
424f6f3bac0SYonghong Song
425f6f3bac0SYonghong Song return 0;
426f6f3bac0SYonghong Song }
427f6f3bac0SYonghong Song
dump_filter_nlmsg(void * cookie,void * msg,struct nlattr ** tb)428f6f3bac0SYonghong Song static int dump_filter_nlmsg(void *cookie, void *msg, struct nlattr **tb)
429f6f3bac0SYonghong Song {
4307900efc1SYonghong Song const struct bpf_filter_t *filter_info = cookie;
431f6f3bac0SYonghong Song
4327900efc1SYonghong Song return do_filter_dump((struct tcmsg *)msg, tb, filter_info->kind,
4337900efc1SYonghong Song filter_info->devname, filter_info->ifindex);
434f6f3bac0SYonghong Song }
435f6f3bac0SYonghong Song
__show_dev_tc_bpf_name(__u32 id,char * name,size_t len)43657c61da8SDaniel Borkmann static int __show_dev_tc_bpf_name(__u32 id, char *name, size_t len)
43757c61da8SDaniel Borkmann {
43857c61da8SDaniel Borkmann struct bpf_prog_info info = {};
43957c61da8SDaniel Borkmann __u32 ilen = sizeof(info);
44057c61da8SDaniel Borkmann int fd, ret;
44157c61da8SDaniel Borkmann
44257c61da8SDaniel Borkmann fd = bpf_prog_get_fd_by_id(id);
44357c61da8SDaniel Borkmann if (fd < 0)
44457c61da8SDaniel Borkmann return fd;
44557c61da8SDaniel Borkmann ret = bpf_obj_get_info_by_fd(fd, &info, &ilen);
44657c61da8SDaniel Borkmann if (ret < 0)
44757c61da8SDaniel Borkmann goto out;
44857c61da8SDaniel Borkmann ret = -ENOENT;
44957c61da8SDaniel Borkmann if (info.name[0]) {
45057c61da8SDaniel Borkmann get_prog_full_name(&info, fd, name, len);
45157c61da8SDaniel Borkmann ret = 0;
45257c61da8SDaniel Borkmann }
45357c61da8SDaniel Borkmann out:
45457c61da8SDaniel Borkmann close(fd);
45557c61da8SDaniel Borkmann return ret;
45657c61da8SDaniel Borkmann }
45757c61da8SDaniel Borkmann
__show_dev_tc_bpf(const struct ip_devname_ifindex * dev,const enum bpf_attach_type loc)45857c61da8SDaniel Borkmann static void __show_dev_tc_bpf(const struct ip_devname_ifindex *dev,
45957c61da8SDaniel Borkmann const enum bpf_attach_type loc)
46057c61da8SDaniel Borkmann {
46157c61da8SDaniel Borkmann __u32 prog_flags[64] = {}, link_flags[64] = {}, i, j;
46257c61da8SDaniel Borkmann __u32 prog_ids[64] = {}, link_ids[64] = {};
46357c61da8SDaniel Borkmann LIBBPF_OPTS(bpf_prog_query_opts, optq);
46457c61da8SDaniel Borkmann char prog_name[MAX_PROG_FULL_NAME];
46557c61da8SDaniel Borkmann int ret;
46657c61da8SDaniel Borkmann
46757c61da8SDaniel Borkmann optq.prog_ids = prog_ids;
46857c61da8SDaniel Borkmann optq.prog_attach_flags = prog_flags;
46957c61da8SDaniel Borkmann optq.link_ids = link_ids;
47057c61da8SDaniel Borkmann optq.link_attach_flags = link_flags;
47157c61da8SDaniel Borkmann optq.count = ARRAY_SIZE(prog_ids);
47257c61da8SDaniel Borkmann
47357c61da8SDaniel Borkmann ret = bpf_prog_query_opts(dev->ifindex, loc, &optq);
47457c61da8SDaniel Borkmann if (ret)
47557c61da8SDaniel Borkmann return;
47657c61da8SDaniel Borkmann for (i = 0; i < optq.count; i++) {
47757c61da8SDaniel Borkmann NET_START_OBJECT;
47857c61da8SDaniel Borkmann NET_DUMP_STR("devname", "%s", dev->devname);
47957c61da8SDaniel Borkmann NET_DUMP_UINT("ifindex", "(%u)", dev->ifindex);
48057c61da8SDaniel Borkmann NET_DUMP_STR("kind", " %s", attach_loc_strings[loc]);
48157c61da8SDaniel Borkmann ret = __show_dev_tc_bpf_name(prog_ids[i], prog_name,
48257c61da8SDaniel Borkmann sizeof(prog_name));
48357c61da8SDaniel Borkmann if (!ret)
48457c61da8SDaniel Borkmann NET_DUMP_STR("name", " %s", prog_name);
48557c61da8SDaniel Borkmann NET_DUMP_UINT("prog_id", " prog_id %u ", prog_ids[i]);
48657c61da8SDaniel Borkmann if (prog_flags[i] || json_output) {
48757c61da8SDaniel Borkmann NET_START_ARRAY("prog_flags", "%s ");
48857c61da8SDaniel Borkmann for (j = 0; prog_flags[i] && j < 32; j++) {
4894cdc0e4cSKuan-Wei Chiu if (!(prog_flags[i] & (1U << j)))
49057c61da8SDaniel Borkmann continue;
4914cdc0e4cSKuan-Wei Chiu NET_DUMP_UINT_ONLY(1U << j);
49257c61da8SDaniel Borkmann }
49357c61da8SDaniel Borkmann NET_END_ARRAY("");
49457c61da8SDaniel Borkmann }
49557c61da8SDaniel Borkmann if (link_ids[i] || json_output) {
49657c61da8SDaniel Borkmann NET_DUMP_UINT("link_id", "link_id %u ", link_ids[i]);
49757c61da8SDaniel Borkmann if (link_flags[i] || json_output) {
49857c61da8SDaniel Borkmann NET_START_ARRAY("link_flags", "%s ");
49957c61da8SDaniel Borkmann for (j = 0; link_flags[i] && j < 32; j++) {
5004cdc0e4cSKuan-Wei Chiu if (!(link_flags[i] & (1U << j)))
50157c61da8SDaniel Borkmann continue;
5024cdc0e4cSKuan-Wei Chiu NET_DUMP_UINT_ONLY(1U << j);
50357c61da8SDaniel Borkmann }
50457c61da8SDaniel Borkmann NET_END_ARRAY("");
50557c61da8SDaniel Borkmann }
50657c61da8SDaniel Borkmann }
50757c61da8SDaniel Borkmann NET_END_OBJECT_FINAL;
50857c61da8SDaniel Borkmann }
50957c61da8SDaniel Borkmann }
51057c61da8SDaniel Borkmann
show_dev_tc_bpf(struct ip_devname_ifindex * dev)51157c61da8SDaniel Borkmann static void show_dev_tc_bpf(struct ip_devname_ifindex *dev)
51257c61da8SDaniel Borkmann {
51357c61da8SDaniel Borkmann __show_dev_tc_bpf(dev, BPF_TCX_INGRESS);
51457c61da8SDaniel Borkmann __show_dev_tc_bpf(dev, BPF_TCX_EGRESS);
515bec981a4SDaniel Borkmann
516bec981a4SDaniel Borkmann __show_dev_tc_bpf(dev, BPF_NETKIT_PRIMARY);
517bec981a4SDaniel Borkmann __show_dev_tc_bpf(dev, BPF_NETKIT_PEER);
51857c61da8SDaniel Borkmann }
51957c61da8SDaniel Borkmann
show_dev_tc_bpf_classic(int sock,unsigned int nl_pid,struct ip_devname_ifindex * dev)52057c61da8SDaniel Borkmann static int show_dev_tc_bpf_classic(int sock, unsigned int nl_pid,
5217900efc1SYonghong Song struct ip_devname_ifindex *dev)
522f6f3bac0SYonghong Song {
5237900efc1SYonghong Song struct bpf_filter_t filter_info;
524f6f3bac0SYonghong Song struct bpf_tcinfo_t tcinfo;
5257900efc1SYonghong Song int i, handle, ret = 0;
526f6f3bac0SYonghong Song
527f6f3bac0SYonghong Song tcinfo.handle_array = NULL;
528f6f3bac0SYonghong Song tcinfo.used_len = 0;
529f6f3bac0SYonghong Song tcinfo.array_len = 0;
530f6f3bac0SYonghong Song
531f6f3bac0SYonghong Song tcinfo.is_qdisc = false;
5327084566aSAndrii Nakryiko ret = netlink_get_class(sock, nl_pid, dev->ifindex,
533aae57780SAndrey Ignatov dump_class_qdisc_nlmsg, &tcinfo);
534f6f3bac0SYonghong Song if (ret)
5357900efc1SYonghong Song goto out;
536f6f3bac0SYonghong Song
537f6f3bac0SYonghong Song tcinfo.is_qdisc = true;
5387084566aSAndrii Nakryiko ret = netlink_get_qdisc(sock, nl_pid, dev->ifindex,
539aae57780SAndrey Ignatov dump_class_qdisc_nlmsg, &tcinfo);
540f6f3bac0SYonghong Song if (ret)
5417900efc1SYonghong Song goto out;
542f6f3bac0SYonghong Song
5437900efc1SYonghong Song filter_info.devname = dev->devname;
5447900efc1SYonghong Song filter_info.ifindex = dev->ifindex;
545f6f3bac0SYonghong Song for (i = 0; i < tcinfo.used_len; i++) {
5467900efc1SYonghong Song filter_info.kind = tcinfo.handle_array[i].kind;
5477084566aSAndrii Nakryiko ret = netlink_get_filter(sock, nl_pid, dev->ifindex,
548f6f3bac0SYonghong Song tcinfo.handle_array[i].handle,
549aae57780SAndrey Ignatov dump_filter_nlmsg, &filter_info);
550f6f3bac0SYonghong Song if (ret)
5517900efc1SYonghong Song goto out;
552f6f3bac0SYonghong Song }
553f6f3bac0SYonghong Song
554f6f3bac0SYonghong Song /* root, ingress and egress handle */
555f6f3bac0SYonghong Song handle = TC_H_ROOT;
5567900efc1SYonghong Song filter_info.kind = "root";
5577084566aSAndrii Nakryiko ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
5587900efc1SYonghong Song dump_filter_nlmsg, &filter_info);
559f6f3bac0SYonghong Song if (ret)
5607900efc1SYonghong Song goto out;
561f6f3bac0SYonghong Song
562f6f3bac0SYonghong Song handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
5637900efc1SYonghong Song filter_info.kind = "clsact/ingress";
5647084566aSAndrii Nakryiko ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
5657900efc1SYonghong Song dump_filter_nlmsg, &filter_info);
566f6f3bac0SYonghong Song if (ret)
5677900efc1SYonghong Song goto out;
568f6f3bac0SYonghong Song
569f6f3bac0SYonghong Song handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS);
5707900efc1SYonghong Song filter_info.kind = "clsact/egress";
5717084566aSAndrii Nakryiko ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
5727900efc1SYonghong Song dump_filter_nlmsg, &filter_info);
573f6f3bac0SYonghong Song if (ret)
5747900efc1SYonghong Song goto out;
575f6f3bac0SYonghong Song
5767900efc1SYonghong Song out:
5777900efc1SYonghong Song free(tcinfo.handle_array);
578f6f3bac0SYonghong Song return 0;
579f6f3bac0SYonghong Song }
580f6f3bac0SYonghong Song
query_flow_dissector(struct bpf_attach_info * attach_info)5817f0c57feSStanislav Fomichev static int query_flow_dissector(struct bpf_attach_info *attach_info)
5827f0c57feSStanislav Fomichev {
5837f0c57feSStanislav Fomichev __u32 attach_flags;
5847f0c57feSStanislav Fomichev __u32 prog_ids[1];
5857f0c57feSStanislav Fomichev __u32 prog_cnt;
5867f0c57feSStanislav Fomichev int err;
5877f0c57feSStanislav Fomichev int fd;
5887f0c57feSStanislav Fomichev
5897f0c57feSStanislav Fomichev fd = open("/proc/self/ns/net", O_RDONLY);
5907f0c57feSStanislav Fomichev if (fd < 0) {
5918a15d5ceSQuentin Monnet p_err("can't open /proc/self/ns/net: %s",
5927f0c57feSStanislav Fomichev strerror(errno));
5937f0c57feSStanislav Fomichev return -1;
5947f0c57feSStanislav Fomichev }
5957f0c57feSStanislav Fomichev prog_cnt = ARRAY_SIZE(prog_ids);
5967f0c57feSStanislav Fomichev err = bpf_prog_query(fd, BPF_FLOW_DISSECTOR, 0,
5977f0c57feSStanislav Fomichev &attach_flags, prog_ids, &prog_cnt);
5987f0c57feSStanislav Fomichev close(fd);
5997f0c57feSStanislav Fomichev if (err) {
6007f0c57feSStanislav Fomichev if (errno == EINVAL) {
6017f0c57feSStanislav Fomichev /* Older kernel's don't support querying
6027f0c57feSStanislav Fomichev * flow dissector programs.
6037f0c57feSStanislav Fomichev */
6047f0c57feSStanislav Fomichev errno = 0;
6057f0c57feSStanislav Fomichev return 0;
6067f0c57feSStanislav Fomichev }
6077f0c57feSStanislav Fomichev p_err("can't query prog: %s", strerror(errno));
6087f0c57feSStanislav Fomichev return -1;
6097f0c57feSStanislav Fomichev }
6107f0c57feSStanislav Fomichev
6117f0c57feSStanislav Fomichev if (prog_cnt == 1)
6127f0c57feSStanislav Fomichev attach_info->flow_dissector_id = prog_ids[0];
6137f0c57feSStanislav Fomichev
6147f0c57feSStanislav Fomichev return 0;
6157f0c57feSStanislav Fomichev }
6167f0c57feSStanislav Fomichev
net_parse_dev(int * argc,char *** argv)61704949cccSDaniel T. Lee static int net_parse_dev(int *argc, char ***argv)
61804949cccSDaniel T. Lee {
61904949cccSDaniel T. Lee int ifindex;
62004949cccSDaniel T. Lee
62104949cccSDaniel T. Lee if (is_prefix(**argv, "dev")) {
62204949cccSDaniel T. Lee NEXT_ARGP();
62304949cccSDaniel T. Lee
62404949cccSDaniel T. Lee ifindex = if_nametoindex(**argv);
62504949cccSDaniel T. Lee if (!ifindex)
62604949cccSDaniel T. Lee p_err("invalid devname %s", **argv);
62704949cccSDaniel T. Lee
62804949cccSDaniel T. Lee NEXT_ARGP();
62904949cccSDaniel T. Lee } else {
63004949cccSDaniel T. Lee p_err("expected 'dev', got: '%s'?", **argv);
63104949cccSDaniel T. Lee return -1;
63204949cccSDaniel T. Lee }
63304949cccSDaniel T. Lee
63404949cccSDaniel T. Lee return ifindex;
63504949cccSDaniel T. Lee }
63604949cccSDaniel T. Lee
do_attach_detach_xdp(int progfd,enum net_attach_type attach_type,int ifindex,bool overwrite)63704949cccSDaniel T. Lee static int do_attach_detach_xdp(int progfd, enum net_attach_type attach_type,
63804949cccSDaniel T. Lee int ifindex, bool overwrite)
63904949cccSDaniel T. Lee {
64004949cccSDaniel T. Lee __u32 flags = 0;
64104949cccSDaniel T. Lee
64204949cccSDaniel T. Lee if (!overwrite)
64304949cccSDaniel T. Lee flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
64404949cccSDaniel T. Lee if (attach_type == NET_ATTACH_TYPE_XDP_GENERIC)
64504949cccSDaniel T. Lee flags |= XDP_FLAGS_SKB_MODE;
64604949cccSDaniel T. Lee if (attach_type == NET_ATTACH_TYPE_XDP_DRIVER)
64704949cccSDaniel T. Lee flags |= XDP_FLAGS_DRV_MODE;
64804949cccSDaniel T. Lee if (attach_type == NET_ATTACH_TYPE_XDP_OFFLOAD)
64904949cccSDaniel T. Lee flags |= XDP_FLAGS_HW_MODE;
65004949cccSDaniel T. Lee
651c86575ecSAndrii Nakryiko return bpf_xdp_attach(ifindex, progfd, flags, NULL);
65204949cccSDaniel T. Lee }
65304949cccSDaniel T. Lee
get_tcx_type(enum net_attach_type attach_type)6543b9d4feeSTao Chen static int get_tcx_type(enum net_attach_type attach_type)
6553b9d4feeSTao Chen {
6563b9d4feeSTao Chen switch (attach_type) {
6573b9d4feeSTao Chen case NET_ATTACH_TYPE_TCX_INGRESS:
6583b9d4feeSTao Chen return BPF_TCX_INGRESS;
6593b9d4feeSTao Chen case NET_ATTACH_TYPE_TCX_EGRESS:
6603b9d4feeSTao Chen return BPF_TCX_EGRESS;
6613b9d4feeSTao Chen default:
6623b9d4feeSTao Chen return -1;
6633b9d4feeSTao Chen }
6643b9d4feeSTao Chen }
6653b9d4feeSTao Chen
do_attach_tcx(int progfd,enum net_attach_type attach_type,int ifindex)6663b9d4feeSTao Chen static int do_attach_tcx(int progfd, enum net_attach_type attach_type, int ifindex)
6673b9d4feeSTao Chen {
6683b9d4feeSTao Chen int type = get_tcx_type(attach_type);
6693b9d4feeSTao Chen
6703b9d4feeSTao Chen return bpf_prog_attach(progfd, ifindex, type, 0);
6713b9d4feeSTao Chen }
6723b9d4feeSTao Chen
do_detach_tcx(int targetfd,enum net_attach_type attach_type)6733b9d4feeSTao Chen static int do_detach_tcx(int targetfd, enum net_attach_type attach_type)
6743b9d4feeSTao Chen {
6753b9d4feeSTao Chen int type = get_tcx_type(attach_type);
6763b9d4feeSTao Chen
6773b9d4feeSTao Chen return bpf_prog_detach(targetfd, type);
6783b9d4feeSTao Chen }
6793b9d4feeSTao Chen
do_attach(int argc,char ** argv)68004949cccSDaniel T. Lee static int do_attach(int argc, char **argv)
68104949cccSDaniel T. Lee {
68204949cccSDaniel T. Lee enum net_attach_type attach_type;
68304949cccSDaniel T. Lee int progfd, ifindex, err = 0;
68404949cccSDaniel T. Lee bool overwrite = false;
68504949cccSDaniel T. Lee
68604949cccSDaniel T. Lee /* parse attach args */
68704949cccSDaniel T. Lee if (!REQ_ARGS(5))
68804949cccSDaniel T. Lee return -EINVAL;
68904949cccSDaniel T. Lee
69004949cccSDaniel T. Lee attach_type = parse_attach_type(*argv);
69104949cccSDaniel T. Lee if (attach_type == net_attach_type_size) {
69204949cccSDaniel T. Lee p_err("invalid net attach/detach type: %s", *argv);
69304949cccSDaniel T. Lee return -EINVAL;
69404949cccSDaniel T. Lee }
69504949cccSDaniel T. Lee NEXT_ARG();
69604949cccSDaniel T. Lee
69704949cccSDaniel T. Lee progfd = prog_parse_fd(&argc, &argv);
69804949cccSDaniel T. Lee if (progfd < 0)
69904949cccSDaniel T. Lee return -EINVAL;
70004949cccSDaniel T. Lee
70104949cccSDaniel T. Lee ifindex = net_parse_dev(&argc, &argv);
70204949cccSDaniel T. Lee if (ifindex < 1) {
70350431b45SWang Hai err = -EINVAL;
70450431b45SWang Hai goto cleanup;
70504949cccSDaniel T. Lee }
70604949cccSDaniel T. Lee
70704949cccSDaniel T. Lee if (argc) {
70804949cccSDaniel T. Lee if (is_prefix(*argv, "overwrite")) {
70904949cccSDaniel T. Lee overwrite = true;
71004949cccSDaniel T. Lee } else {
71104949cccSDaniel T. Lee p_err("expected 'overwrite', got: '%s'?", *argv);
71250431b45SWang Hai err = -EINVAL;
71350431b45SWang Hai goto cleanup;
71404949cccSDaniel T. Lee }
71504949cccSDaniel T. Lee }
71604949cccSDaniel T. Lee
717b7264f87STao Chen switch (attach_type) {
71804949cccSDaniel T. Lee /* attach xdp prog */
719b7264f87STao Chen case NET_ATTACH_TYPE_XDP:
720b7264f87STao Chen case NET_ATTACH_TYPE_XDP_GENERIC:
721b7264f87STao Chen case NET_ATTACH_TYPE_XDP_DRIVER:
722b7264f87STao Chen case NET_ATTACH_TYPE_XDP_OFFLOAD:
723b7264f87STao Chen err = do_attach_detach_xdp(progfd, attach_type, ifindex, overwrite);
724b7264f87STao Chen break;
7253b9d4feeSTao Chen /* attach tcx prog */
7263b9d4feeSTao Chen case NET_ATTACH_TYPE_TCX_INGRESS:
7273b9d4feeSTao Chen case NET_ATTACH_TYPE_TCX_EGRESS:
7283b9d4feeSTao Chen err = do_attach_tcx(progfd, attach_type, ifindex);
7293b9d4feeSTao Chen break;
730b7264f87STao Chen default:
731b7264f87STao Chen break;
732b7264f87STao Chen }
733b7264f87STao Chen
73450431b45SWang Hai if (err) {
73504949cccSDaniel T. Lee p_err("interface %s attach failed: %s",
73604949cccSDaniel T. Lee attach_type_strings[attach_type], strerror(-err));
73750431b45SWang Hai goto cleanup;
73804949cccSDaniel T. Lee }
73904949cccSDaniel T. Lee
74004949cccSDaniel T. Lee if (json_output)
74104949cccSDaniel T. Lee jsonw_null(json_wtr);
74250431b45SWang Hai cleanup:
74350431b45SWang Hai close(progfd);
74450431b45SWang Hai return err;
74504949cccSDaniel T. Lee }
74604949cccSDaniel T. Lee
do_detach(int argc,char ** argv)74737c7f863SDaniel T. Lee static int do_detach(int argc, char **argv)
74837c7f863SDaniel T. Lee {
74937c7f863SDaniel T. Lee enum net_attach_type attach_type;
75037c7f863SDaniel T. Lee int progfd, ifindex, err = 0;
75137c7f863SDaniel T. Lee
75237c7f863SDaniel T. Lee /* parse detach args */
75337c7f863SDaniel T. Lee if (!REQ_ARGS(3))
75437c7f863SDaniel T. Lee return -EINVAL;
75537c7f863SDaniel T. Lee
75637c7f863SDaniel T. Lee attach_type = parse_attach_type(*argv);
75737c7f863SDaniel T. Lee if (attach_type == net_attach_type_size) {
75837c7f863SDaniel T. Lee p_err("invalid net attach/detach type: %s", *argv);
75937c7f863SDaniel T. Lee return -EINVAL;
76037c7f863SDaniel T. Lee }
76137c7f863SDaniel T. Lee NEXT_ARG();
76237c7f863SDaniel T. Lee
76337c7f863SDaniel T. Lee ifindex = net_parse_dev(&argc, &argv);
76437c7f863SDaniel T. Lee if (ifindex < 1)
76537c7f863SDaniel T. Lee return -EINVAL;
76637c7f863SDaniel T. Lee
767b7264f87STao Chen switch (attach_type) {
76837c7f863SDaniel T. Lee /* detach xdp prog */
769b7264f87STao Chen case NET_ATTACH_TYPE_XDP:
770b7264f87STao Chen case NET_ATTACH_TYPE_XDP_GENERIC:
771b7264f87STao Chen case NET_ATTACH_TYPE_XDP_DRIVER:
772b7264f87STao Chen case NET_ATTACH_TYPE_XDP_OFFLOAD:
77337c7f863SDaniel T. Lee progfd = -1;
77437c7f863SDaniel T. Lee err = do_attach_detach_xdp(progfd, attach_type, ifindex, NULL);
775b7264f87STao Chen break;
7763b9d4feeSTao Chen /* detach tcx prog */
7773b9d4feeSTao Chen case NET_ATTACH_TYPE_TCX_INGRESS:
7783b9d4feeSTao Chen case NET_ATTACH_TYPE_TCX_EGRESS:
7793b9d4feeSTao Chen err = do_detach_tcx(ifindex, attach_type);
7803b9d4feeSTao Chen break;
781b7264f87STao Chen default:
782b7264f87STao Chen break;
783b7264f87STao Chen }
78437c7f863SDaniel T. Lee
78537c7f863SDaniel T. Lee if (err < 0) {
78637c7f863SDaniel T. Lee p_err("interface %s detach failed: %s",
78737c7f863SDaniel T. Lee attach_type_strings[attach_type], strerror(-err));
78837c7f863SDaniel T. Lee return err;
78937c7f863SDaniel T. Lee }
79037c7f863SDaniel T. Lee
79137c7f863SDaniel T. Lee if (json_output)
79237c7f863SDaniel T. Lee jsonw_null(json_wtr);
79337c7f863SDaniel T. Lee
79437c7f863SDaniel T. Lee return 0;
79537c7f863SDaniel T. Lee }
79637c7f863SDaniel T. Lee
netfilter_link_compar(const void * a,const void * b)797d0fe92fbSFlorian Westphal static int netfilter_link_compar(const void *a, const void *b)
798d0fe92fbSFlorian Westphal {
799d0fe92fbSFlorian Westphal const struct bpf_link_info *nfa = a;
800d0fe92fbSFlorian Westphal const struct bpf_link_info *nfb = b;
801d0fe92fbSFlorian Westphal int delta;
802d0fe92fbSFlorian Westphal
803d0fe92fbSFlorian Westphal delta = nfa->netfilter.pf - nfb->netfilter.pf;
804d0fe92fbSFlorian Westphal if (delta)
805d0fe92fbSFlorian Westphal return delta;
806d0fe92fbSFlorian Westphal
807d0fe92fbSFlorian Westphal delta = nfa->netfilter.hooknum - nfb->netfilter.hooknum;
808d0fe92fbSFlorian Westphal if (delta)
809d0fe92fbSFlorian Westphal return delta;
810d0fe92fbSFlorian Westphal
811d0fe92fbSFlorian Westphal if (nfa->netfilter.priority < nfb->netfilter.priority)
812d0fe92fbSFlorian Westphal return -1;
813d0fe92fbSFlorian Westphal if (nfa->netfilter.priority > nfb->netfilter.priority)
814d0fe92fbSFlorian Westphal return 1;
815d0fe92fbSFlorian Westphal
816d0fe92fbSFlorian Westphal return nfa->netfilter.flags - nfb->netfilter.flags;
817d0fe92fbSFlorian Westphal }
818d0fe92fbSFlorian Westphal
show_link_netfilter(void)819d0fe92fbSFlorian Westphal static void show_link_netfilter(void)
820d0fe92fbSFlorian Westphal {
821d0fe92fbSFlorian Westphal unsigned int nf_link_len = 0, nf_link_count = 0;
822d0fe92fbSFlorian Westphal struct bpf_link_info *nf_link_info = NULL;
823d0fe92fbSFlorian Westphal __u32 id = 0;
824d0fe92fbSFlorian Westphal
825d0fe92fbSFlorian Westphal while (true) {
826d0fe92fbSFlorian Westphal struct bpf_link_info info;
827d0fe92fbSFlorian Westphal int fd, err;
828d0fe92fbSFlorian Westphal __u32 len;
829d0fe92fbSFlorian Westphal
830d0fe92fbSFlorian Westphal err = bpf_link_get_next_id(id, &id);
831d0fe92fbSFlorian Westphal if (err) {
832d0fe92fbSFlorian Westphal if (errno == ENOENT)
833d0fe92fbSFlorian Westphal break;
834d0fe92fbSFlorian Westphal p_err("can't get next link: %s (id %d)", strerror(errno), id);
835d0fe92fbSFlorian Westphal break;
836d0fe92fbSFlorian Westphal }
837d0fe92fbSFlorian Westphal
838d0fe92fbSFlorian Westphal fd = bpf_link_get_fd_by_id(id);
839d0fe92fbSFlorian Westphal if (fd < 0) {
840d0fe92fbSFlorian Westphal p_err("can't get link by id (%u): %s", id, strerror(errno));
841d0fe92fbSFlorian Westphal continue;
842d0fe92fbSFlorian Westphal }
843d0fe92fbSFlorian Westphal
844d0fe92fbSFlorian Westphal memset(&info, 0, sizeof(info));
845d0fe92fbSFlorian Westphal len = sizeof(info);
846d0fe92fbSFlorian Westphal
847d0fe92fbSFlorian Westphal err = bpf_link_get_info_by_fd(fd, &info, &len);
848d0fe92fbSFlorian Westphal
849d0fe92fbSFlorian Westphal close(fd);
850d0fe92fbSFlorian Westphal
851d0fe92fbSFlorian Westphal if (err) {
852d0fe92fbSFlorian Westphal p_err("can't get link info for fd %d: %s", fd, strerror(errno));
853d0fe92fbSFlorian Westphal continue;
854d0fe92fbSFlorian Westphal }
855d0fe92fbSFlorian Westphal
856d0fe92fbSFlorian Westphal if (info.type != BPF_LINK_TYPE_NETFILTER)
857d0fe92fbSFlorian Westphal continue;
858d0fe92fbSFlorian Westphal
859d0fe92fbSFlorian Westphal if (nf_link_count >= nf_link_len) {
860d0fe92fbSFlorian Westphal static const unsigned int max_link_count = INT_MAX / sizeof(info);
861d0fe92fbSFlorian Westphal struct bpf_link_info *expand;
862d0fe92fbSFlorian Westphal
863d0fe92fbSFlorian Westphal if (nf_link_count > max_link_count) {
864d0fe92fbSFlorian Westphal p_err("cannot handle more than %u links\n", max_link_count);
865d0fe92fbSFlorian Westphal break;
866d0fe92fbSFlorian Westphal }
867d0fe92fbSFlorian Westphal
868d0fe92fbSFlorian Westphal nf_link_len += 16;
869d0fe92fbSFlorian Westphal
870d0fe92fbSFlorian Westphal expand = realloc(nf_link_info, nf_link_len * sizeof(info));
871d0fe92fbSFlorian Westphal if (!expand) {
872d0fe92fbSFlorian Westphal p_err("realloc: %s", strerror(errno));
873d0fe92fbSFlorian Westphal break;
874d0fe92fbSFlorian Westphal }
875d0fe92fbSFlorian Westphal
876d0fe92fbSFlorian Westphal nf_link_info = expand;
877d0fe92fbSFlorian Westphal }
878d0fe92fbSFlorian Westphal
879d0fe92fbSFlorian Westphal nf_link_info[nf_link_count] = info;
880d0fe92fbSFlorian Westphal nf_link_count++;
881d0fe92fbSFlorian Westphal }
882d0fe92fbSFlorian Westphal
883*f04e2ad3SKuan-Wei Chiu if (!nf_link_info)
884*f04e2ad3SKuan-Wei Chiu return;
885*f04e2ad3SKuan-Wei Chiu
886d0fe92fbSFlorian Westphal qsort(nf_link_info, nf_link_count, sizeof(*nf_link_info), netfilter_link_compar);
887d0fe92fbSFlorian Westphal
888d0fe92fbSFlorian Westphal for (id = 0; id < nf_link_count; id++) {
889d0fe92fbSFlorian Westphal NET_START_OBJECT;
890d0fe92fbSFlorian Westphal if (json_output)
891d0fe92fbSFlorian Westphal netfilter_dump_json(&nf_link_info[id], json_wtr);
892d0fe92fbSFlorian Westphal else
893d0fe92fbSFlorian Westphal netfilter_dump_plain(&nf_link_info[id]);
894d0fe92fbSFlorian Westphal
895d0fe92fbSFlorian Westphal NET_DUMP_UINT("id", " prog_id %u", nf_link_info[id].prog_id);
896d0fe92fbSFlorian Westphal NET_END_OBJECT;
897d0fe92fbSFlorian Westphal }
898d0fe92fbSFlorian Westphal
899d0fe92fbSFlorian Westphal free(nf_link_info);
900d0fe92fbSFlorian Westphal }
901d0fe92fbSFlorian Westphal
do_show(int argc,char ** argv)902f6f3bac0SYonghong Song static int do_show(int argc, char **argv)
903f6f3bac0SYonghong Song {
9047f0c57feSStanislav Fomichev struct bpf_attach_info attach_info = {};
905f6f3bac0SYonghong Song int i, sock, ret, filter_idx = -1;
906f6f3bac0SYonghong Song struct bpf_netdev_t dev_array;
9077084566aSAndrii Nakryiko unsigned int nl_pid = 0;
908f6f3bac0SYonghong Song char err_buf[256];
909f6f3bac0SYonghong Song
910f6f3bac0SYonghong Song if (argc == 2) {
91104949cccSDaniel T. Lee filter_idx = net_parse_dev(&argc, &argv);
91204949cccSDaniel T. Lee if (filter_idx < 1)
913f6f3bac0SYonghong Song return -1;
914f6f3bac0SYonghong Song } else if (argc != 0) {
915f6f3bac0SYonghong Song usage();
916f6f3bac0SYonghong Song }
917f6f3bac0SYonghong Song
9187f0c57feSStanislav Fomichev ret = query_flow_dissector(&attach_info);
9197f0c57feSStanislav Fomichev if (ret)
9207f0c57feSStanislav Fomichev return -1;
9217f0c57feSStanislav Fomichev
9227084566aSAndrii Nakryiko sock = netlink_open(&nl_pid);
923f6f3bac0SYonghong Song if (sock < 0) {
924f6f3bac0SYonghong Song fprintf(stderr, "failed to open netlink sock\n");
925f6f3bac0SYonghong Song return -1;
926f6f3bac0SYonghong Song }
927f6f3bac0SYonghong Song
9287900efc1SYonghong Song dev_array.devices = NULL;
929f6f3bac0SYonghong Song dev_array.used_len = 0;
930f6f3bac0SYonghong Song dev_array.array_len = 0;
931f6f3bac0SYonghong Song dev_array.filter_idx = filter_idx;
932f6f3bac0SYonghong Song
933f6f3bac0SYonghong Song if (json_output)
934f6f3bac0SYonghong Song jsonw_start_array(json_wtr);
935f6f3bac0SYonghong Song NET_START_OBJECT;
9367900efc1SYonghong Song NET_START_ARRAY("xdp", "%s:\n");
9377084566aSAndrii Nakryiko ret = netlink_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array);
938f6f3bac0SYonghong Song NET_END_ARRAY("\n");
939f6f3bac0SYonghong Song
940f6f3bac0SYonghong Song if (!ret) {
9417900efc1SYonghong Song NET_START_ARRAY("tc", "%s:\n");
942f6f3bac0SYonghong Song for (i = 0; i < dev_array.used_len; i++) {
94357c61da8SDaniel Borkmann show_dev_tc_bpf(&dev_array.devices[i]);
94457c61da8SDaniel Borkmann ret = show_dev_tc_bpf_classic(sock, nl_pid,
9457900efc1SYonghong Song &dev_array.devices[i]);
946f6f3bac0SYonghong Song if (ret)
947f6f3bac0SYonghong Song break;
948f6f3bac0SYonghong Song }
949f6f3bac0SYonghong Song NET_END_ARRAY("\n");
950f6f3bac0SYonghong Song }
9517f0c57feSStanislav Fomichev
9527f0c57feSStanislav Fomichev NET_START_ARRAY("flow_dissector", "%s:\n");
9537f0c57feSStanislav Fomichev if (attach_info.flow_dissector_id > 0)
9547f0c57feSStanislav Fomichev NET_DUMP_UINT("id", "id %u", attach_info.flow_dissector_id);
9557f0c57feSStanislav Fomichev NET_END_ARRAY("\n");
9567f0c57feSStanislav Fomichev
957d0fe92fbSFlorian Westphal NET_START_ARRAY("netfilter", "%s:\n");
958d0fe92fbSFlorian Westphal show_link_netfilter();
959d0fe92fbSFlorian Westphal NET_END_ARRAY("\n");
960d0fe92fbSFlorian Westphal
961f6f3bac0SYonghong Song NET_END_OBJECT;
962f6f3bac0SYonghong Song if (json_output)
963f6f3bac0SYonghong Song jsonw_end_array(json_wtr);
964f6f3bac0SYonghong Song
965f6f3bac0SYonghong Song if (ret) {
966f6f3bac0SYonghong Song if (json_output)
967f6f3bac0SYonghong Song jsonw_null(json_wtr);
968f6f3bac0SYonghong Song libbpf_strerror(ret, err_buf, sizeof(err_buf));
969f6f3bac0SYonghong Song fprintf(stderr, "Error: %s\n", err_buf);
970f6f3bac0SYonghong Song }
9717900efc1SYonghong Song free(dev_array.devices);
972f6f3bac0SYonghong Song close(sock);
973f6f3bac0SYonghong Song return ret;
974f6f3bac0SYonghong Song }
975f6f3bac0SYonghong Song
do_help(int argc,char ** argv)976f6f3bac0SYonghong Song static int do_help(int argc, char **argv)
977f6f3bac0SYonghong Song {
978f6f3bac0SYonghong Song if (json_output) {
979f6f3bac0SYonghong Song jsonw_null(json_wtr);
980f6f3bac0SYonghong Song return 0;
981f6f3bac0SYonghong Song }
982f6f3bac0SYonghong Song
983f6f3bac0SYonghong Song fprintf(stderr,
98490040351SQuentin Monnet "Usage: %1$s %2$s { show | list } [dev <devname>]\n"
98590040351SQuentin Monnet " %1$s %2$s attach ATTACH_TYPE PROG dev <devname> [ overwrite ]\n"
98690040351SQuentin Monnet " %1$s %2$s detach ATTACH_TYPE dev <devname>\n"
98790040351SQuentin Monnet " %1$s %2$s help\n"
98804949cccSDaniel T. Lee "\n"
98904949cccSDaniel T. Lee " " HELP_SPEC_PROGRAM "\n"
9903b9d4feeSTao Chen " ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload | tcx_ingress\n"
9913b9d4feeSTao Chen " | tcx_egress }\n"
992c07ba629SQuentin Monnet " " HELP_SPEC_OPTIONS " }\n"
99304949cccSDaniel T. Lee "\n"
994bec981a4SDaniel Borkmann "Note: Only xdp, tcx, tc, netkit, flow_dissector and netfilter attachments\n"
99557c61da8SDaniel Borkmann " are currently supported.\n"
9967900efc1SYonghong Song " For progs attached to cgroups, use \"bpftool cgroup\"\n"
9977900efc1SYonghong Song " to dump program attachments. For program types\n"
9987900efc1SYonghong Song " sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n"
99990040351SQuentin Monnet " consult iproute2.\n"
100090040351SQuentin Monnet "",
100137c7f863SDaniel T. Lee bin_name, argv[-2]);
1002f6f3bac0SYonghong Song
1003f6f3bac0SYonghong Song return 0;
1004f6f3bac0SYonghong Song }
1005f6f3bac0SYonghong Song
1006f6f3bac0SYonghong Song static const struct cmd cmds[] = {
1007f6f3bac0SYonghong Song { "show", do_show },
1008f6f3bac0SYonghong Song { "list", do_show },
100904949cccSDaniel T. Lee { "attach", do_attach },
101037c7f863SDaniel T. Lee { "detach", do_detach },
1011f6f3bac0SYonghong Song { "help", do_help },
1012f6f3bac0SYonghong Song { 0 }
1013f6f3bac0SYonghong Song };
1014f6f3bac0SYonghong Song
do_net(int argc,char ** argv)1015f6f3bac0SYonghong Song int do_net(int argc, char **argv)
1016f6f3bac0SYonghong Song {
1017f6f3bac0SYonghong Song return cmd_select(cmds, argc, argv, do_help);
1018f6f3bac0SYonghong Song }
1019