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