Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) // Copyright (C) 2018 Facebook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <net/if.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/tc_act/tc_bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <sys/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "bpf/nlattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "netlink_dumper.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #ifndef SOL_NETLINK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define SOL_NETLINK 270
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) struct ip_devname_ifindex {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	char	devname[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	int	ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) struct bpf_netdev_t {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct ip_devname_ifindex *devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	int	used_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	int	array_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	int	filter_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) struct tc_kind_handle {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	char	kind[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	int	handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) struct bpf_tcinfo_t {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct tc_kind_handle	*handle_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int			used_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int			array_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	bool			is_qdisc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) struct bpf_filter_t {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	const char	*kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	const char	*devname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	int		ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) struct bpf_attach_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	__u32 flow_dissector_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) enum net_attach_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	NET_ATTACH_TYPE_XDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	NET_ATTACH_TYPE_XDP_GENERIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	NET_ATTACH_TYPE_XDP_DRIVER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	NET_ATTACH_TYPE_XDP_OFFLOAD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static const char * const attach_type_strings[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	[NET_ATTACH_TYPE_XDP]		= "xdp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	[NET_ATTACH_TYPE_XDP_GENERIC]	= "xdpgeneric",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	[NET_ATTACH_TYPE_XDP_DRIVER]	= "xdpdrv",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	[NET_ATTACH_TYPE_XDP_OFFLOAD]	= "xdpoffload",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) const size_t net_attach_type_size = ARRAY_SIZE(attach_type_strings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static enum net_attach_type parse_attach_type(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	enum net_attach_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	for (type = 0; type < net_attach_type_size; type++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		if (attach_type_strings[type] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		    is_prefix(str, attach_type_strings[type]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	return net_attach_type_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) typedef int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, dump_nlmsg_t, void *cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static int netlink_open(__u32 *nl_pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct sockaddr_nl sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	socklen_t addrlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	int one = 1, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	int sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	memset(&sa, 0, sizeof(sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	sa.nl_family = AF_NETLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (sock < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		       &one, sizeof(one)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		p_err("Netlink error reporting not supported");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	addrlen = sizeof(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (addrlen != sizeof(sa)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		ret = -LIBBPF_ERRNO__INTERNAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	*nl_pid = sa.nl_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	return sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	close(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int netlink_recv(int sock, __u32 nl_pid, __u32 seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			    __dump_nlmsg_t _fn, dump_nlmsg_t fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			    void *cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	bool multipart = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct nlmsgerr *err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	struct nlmsghdr *nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	char buf[4096];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	int len, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	while (multipart) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		multipart = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		len = recv(sock, buf, sizeof(buf), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		if (len < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		     nh = NLMSG_NEXT(nh, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			if (nh->nlmsg_pid != nl_pid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				ret = -LIBBPF_ERRNO__WRNGPID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			if (nh->nlmsg_seq != seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 				ret = -LIBBPF_ERRNO__INVSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 				goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			if (nh->nlmsg_flags & NLM_F_MULTI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				multipart = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			switch (nh->nlmsg_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			case NLMSG_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 				err = (struct nlmsgerr *)NLMSG_DATA(nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 				if (!err->error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 				ret = err->error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 				libbpf_nla_dump_errormsg(nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 				goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			case NLMSG_DONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			if (_fn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 				ret = _fn(nh, fn, cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 				if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 					return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int __dump_class_nlmsg(struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			      dump_nlmsg_t dump_class_nlmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			      void *cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	struct nlattr *tb[TCA_MAX + 1], *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	struct tcmsg *t = NLMSG_DATA(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return -LIBBPF_ERRNO__NLPARSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return dump_class_nlmsg(cookie, t, tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static int netlink_get_class(int sock, unsigned int nl_pid, int ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			     dump_nlmsg_t dump_class_nlmsg, void *cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		struct nlmsghdr nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		struct tcmsg t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	} req = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		.nlh.nlmsg_type = RTM_GETTCLASS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		.t.tcm_family = AF_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		.t.tcm_ifindex = ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	int seq = time(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	req.nlh.nlmsg_seq = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	return netlink_recv(sock, nl_pid, seq, __dump_class_nlmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			    dump_class_nlmsg, cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			      dump_nlmsg_t dump_qdisc_nlmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			      void *cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct nlattr *tb[TCA_MAX + 1], *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct tcmsg *t = NLMSG_DATA(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		return -LIBBPF_ERRNO__NLPARSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return dump_qdisc_nlmsg(cookie, t, tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static int netlink_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			     dump_nlmsg_t dump_qdisc_nlmsg, void *cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		struct nlmsghdr nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		struct tcmsg t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	} req = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		.nlh.nlmsg_type = RTM_GETQDISC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		.t.tcm_family = AF_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		.t.tcm_ifindex = ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	int seq = time(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	req.nlh.nlmsg_seq = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	return netlink_recv(sock, nl_pid, seq, __dump_qdisc_nlmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			    dump_qdisc_nlmsg, cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static int __dump_filter_nlmsg(struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			       dump_nlmsg_t dump_filter_nlmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			       void *cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	struct nlattr *tb[TCA_MAX + 1], *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	struct tcmsg *t = NLMSG_DATA(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		return -LIBBPF_ERRNO__NLPARSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	return dump_filter_nlmsg(cookie, t, tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static int netlink_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			      dump_nlmsg_t dump_filter_nlmsg, void *cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		struct nlmsghdr nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		struct tcmsg t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	} req = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		.nlh.nlmsg_type = RTM_GETTFILTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		.t.tcm_family = AF_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		.t.tcm_ifindex = ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		.t.tcm_parent = handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	int seq = time(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	req.nlh.nlmsg_seq = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	return netlink_recv(sock, nl_pid, seq, __dump_filter_nlmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			    dump_filter_nlmsg, cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static int __dump_link_nlmsg(struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			     dump_nlmsg_t dump_link_nlmsg, void *cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	struct nlattr *tb[IFLA_MAX + 1], *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	struct ifinfomsg *ifi = NLMSG_DATA(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		return -LIBBPF_ERRNO__NLPARSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	return dump_link_nlmsg(cookie, ifi, tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static int netlink_get_link(int sock, unsigned int nl_pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			    dump_nlmsg_t dump_link_nlmsg, void *cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		struct nlmsghdr nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		struct ifinfomsg ifm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	} req = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		.nlh.nlmsg_type = RTM_GETLINK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		.ifm.ifi_family = AF_PACKET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	int seq = time(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	req.nlh.nlmsg_seq = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	return netlink_recv(sock, nl_pid, seq, __dump_link_nlmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			    dump_link_nlmsg, cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	struct bpf_netdev_t *netinfo = cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	struct ifinfomsg *ifinfo = msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	if (netinfo->filter_idx > 0 && netinfo->filter_idx != ifinfo->ifi_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	if (netinfo->used_len == netinfo->array_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		netinfo->devices = realloc(netinfo->devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			(netinfo->array_len + 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			sizeof(struct ip_devname_ifindex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		if (!netinfo->devices)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		netinfo->array_len += 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	netinfo->devices[netinfo->used_len].ifindex = ifinfo->ifi_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	snprintf(netinfo->devices[netinfo->used_len].devname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		 sizeof(netinfo->devices[netinfo->used_len].devname),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		 "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		 tb[IFLA_IFNAME]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			 ? libbpf_nla_getattr_str(tb[IFLA_IFNAME])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			 : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	netinfo->used_len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	return do_xdp_dump(ifinfo, tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	struct bpf_tcinfo_t *tcinfo = cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	struct tcmsg *info = msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (tcinfo->is_qdisc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		/* skip clsact qdisc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		if (tb[TCA_KIND] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		    strcmp(libbpf_nla_data(tb[TCA_KIND]), "clsact") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		if (info->tcm_handle == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	if (tcinfo->used_len == tcinfo->array_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		tcinfo->handle_array = realloc(tcinfo->handle_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			(tcinfo->array_len + 16) * sizeof(struct tc_kind_handle));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		if (!tcinfo->handle_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		tcinfo->array_len += 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	tcinfo->handle_array[tcinfo->used_len].handle = info->tcm_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	snprintf(tcinfo->handle_array[tcinfo->used_len].kind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		 sizeof(tcinfo->handle_array[tcinfo->used_len].kind),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		 "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		 tb[TCA_KIND]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			 ? libbpf_nla_getattr_str(tb[TCA_KIND])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			 : "unknown");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	tcinfo->used_len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static int dump_filter_nlmsg(void *cookie, void *msg, struct nlattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	const struct bpf_filter_t *filter_info = cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	return do_filter_dump((struct tcmsg *)msg, tb, filter_info->kind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			      filter_info->devname, filter_info->ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 			   struct ip_devname_ifindex *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	struct bpf_filter_t filter_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	struct bpf_tcinfo_t tcinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	int i, handle, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	tcinfo.handle_array = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	tcinfo.used_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	tcinfo.array_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	tcinfo.is_qdisc = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	ret = netlink_get_class(sock, nl_pid, dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 				dump_class_qdisc_nlmsg, &tcinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	tcinfo.is_qdisc = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	ret = netlink_get_qdisc(sock, nl_pid, dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 				dump_class_qdisc_nlmsg, &tcinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	filter_info.devname = dev->devname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	filter_info.ifindex = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	for (i = 0; i < tcinfo.used_len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		filter_info.kind = tcinfo.handle_array[i].kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		ret = netlink_get_filter(sock, nl_pid, dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 					 tcinfo.handle_array[i].handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 					 dump_filter_nlmsg, &filter_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	/* root, ingress and egress handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	handle = TC_H_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	filter_info.kind = "root";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 				 dump_filter_nlmsg, &filter_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	filter_info.kind = "clsact/ingress";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 				 dump_filter_nlmsg, &filter_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	filter_info.kind = "clsact/egress";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 				 dump_filter_nlmsg, &filter_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	free(tcinfo.handle_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static int query_flow_dissector(struct bpf_attach_info *attach_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	__u32 attach_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	__u32 prog_ids[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	__u32 prog_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	fd = open("/proc/self/ns/net", O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		p_err("can't open /proc/self/ns/net: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		      strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	prog_cnt = ARRAY_SIZE(prog_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	err = bpf_prog_query(fd, BPF_FLOW_DISSECTOR, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			     &attach_flags, prog_ids, &prog_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		if (errno == EINVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 			/* Older kernel's don't support querying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 			 * flow dissector programs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 			errno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		p_err("can't query prog: %s", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	if (prog_cnt == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		attach_info->flow_dissector_id = prog_ids[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static int net_parse_dev(int *argc, char ***argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	int ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (is_prefix(**argv, "dev")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		NEXT_ARGP();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		ifindex = if_nametoindex(**argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		if (!ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			p_err("invalid devname %s", **argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		NEXT_ARGP();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		p_err("expected 'dev', got: '%s'?", **argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	return ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) static int do_attach_detach_xdp(int progfd, enum net_attach_type attach_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 				int ifindex, bool overwrite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	__u32 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	if (!overwrite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	if (attach_type == NET_ATTACH_TYPE_XDP_GENERIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		flags |= XDP_FLAGS_SKB_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	if (attach_type == NET_ATTACH_TYPE_XDP_DRIVER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		flags |= XDP_FLAGS_DRV_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	if (attach_type == NET_ATTACH_TYPE_XDP_OFFLOAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		flags |= XDP_FLAGS_HW_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	return bpf_set_link_xdp_fd(ifindex, progfd, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) static int do_attach(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	enum net_attach_type attach_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	int progfd, ifindex, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	bool overwrite = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	/* parse attach args */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	if (!REQ_ARGS(5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	attach_type = parse_attach_type(*argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	if (attach_type == net_attach_type_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		p_err("invalid net attach/detach type: %s", *argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	NEXT_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	progfd = prog_parse_fd(&argc, &argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	if (progfd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	ifindex = net_parse_dev(&argc, &argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	if (ifindex < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	if (argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		if (is_prefix(*argv, "overwrite")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 			overwrite = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 			p_err("expected 'overwrite', got: '%s'?", *argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	/* attach xdp prog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	if (is_prefix("xdp", attach_type_strings[attach_type]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		err = do_attach_detach_xdp(progfd, attach_type, ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 					   overwrite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		p_err("interface %s attach failed: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		      attach_type_strings[attach_type], strerror(-err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 		jsonw_null(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	close(progfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) static int do_detach(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	enum net_attach_type attach_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	int progfd, ifindex, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	/* parse detach args */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	if (!REQ_ARGS(3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	attach_type = parse_attach_type(*argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	if (attach_type == net_attach_type_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		p_err("invalid net attach/detach type: %s", *argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	NEXT_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	ifindex = net_parse_dev(&argc, &argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	if (ifindex < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	/* detach xdp prog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	progfd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	if (is_prefix("xdp", attach_type_strings[attach_type]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		err = do_attach_detach_xdp(progfd, attach_type, ifindex, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		p_err("interface %s detach failed: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		      attach_type_strings[attach_type], strerror(-err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		jsonw_null(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) static int do_show(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	struct bpf_attach_info attach_info = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	int i, sock, ret, filter_idx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	struct bpf_netdev_t dev_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	unsigned int nl_pid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	char err_buf[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	if (argc == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		filter_idx = net_parse_dev(&argc, &argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		if (filter_idx < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	} else if (argc != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	ret = query_flow_dissector(&attach_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	sock = netlink_open(&nl_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	if (sock < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		fprintf(stderr, "failed to open netlink sock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	dev_array.devices = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	dev_array.used_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	dev_array.array_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	dev_array.filter_idx = filter_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		jsonw_start_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	NET_START_OBJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	NET_START_ARRAY("xdp", "%s:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	ret = netlink_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	NET_END_ARRAY("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 		NET_START_ARRAY("tc", "%s:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 		for (i = 0; i < dev_array.used_len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 			ret = show_dev_tc_bpf(sock, nl_pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 					      &dev_array.devices[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 		NET_END_ARRAY("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	NET_START_ARRAY("flow_dissector", "%s:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	if (attach_info.flow_dissector_id > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		NET_DUMP_UINT("id", "id %u", attach_info.flow_dissector_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	NET_END_ARRAY("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	NET_END_OBJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		jsonw_end_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 			jsonw_null(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 		libbpf_strerror(ret, err_buf, sizeof(err_buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 		fprintf(stderr, "Error: %s\n", err_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	free(dev_array.devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	close(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) static int do_help(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 		jsonw_null(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		"Usage: %1$s %2$s { show | list } [dev <devname>]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		"       %1$s %2$s attach ATTACH_TYPE PROG dev <devname> [ overwrite ]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		"       %1$s %2$s detach ATTACH_TYPE dev <devname>\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 		"       %1$s %2$s help\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		"       " HELP_SPEC_PROGRAM "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 		"       ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload }\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 		"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		"Note: Only xdp and tc attachments are supported now.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		"      For progs attached to cgroups, use \"bpftool cgroup\"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		"      to dump program attachments. For program types\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 		"      sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		"      consult iproute2.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 		"",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 		bin_name, argv[-2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) static const struct cmd cmds[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	{ "show",	do_show },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	{ "list",	do_show },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	{ "attach",	do_attach },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	{ "detach",	do_detach },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	{ "help",	do_help },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	{ 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) int do_net(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	return cmd_select(cmds, argc, argv, do_help);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) }