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