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