1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
9 #include <linux/if_ether.h>
10 #include <linux/pkt_cls.h>
11 #include <linux/rtnetlink.h>
12 #include <linux/netdev.h>
13 #include <sys/socket.h>
19 #include "libbpf_internal.h"
23 #define SOL_NETLINK 270
26 typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
28 typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, libbpf_dump_nlmsg_t,
31 struct xdp_link_info {
42 struct xdp_link_info info;
46 struct xdp_features_md {
51 static int libbpf_netlink_open(__u32 *nl_pid, int proto)
53 struct sockaddr_nl sa;
58 memset(&sa, 0, sizeof(sa));
59 sa.nl_family = AF_NETLINK;
61 sock = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, proto);
65 if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
66 &one, sizeof(one)) < 0) {
67 pr_warn("Netlink error reporting not supported\n");
70 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
76 if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
81 if (addrlen != sizeof(sa)) {
82 ret = -LIBBPF_ERRNO__INTERNAL;
94 static void libbpf_netlink_close(int sock)
105 static int netlink_recvmsg(int sock, struct msghdr *mhdr, int flags)
110 len = recvmsg(sock, mhdr, flags);
111 } while (len < 0 && (errno == EINTR || errno == EAGAIN));
118 static int alloc_iov(struct iovec *iov, int len)
122 nbuf = realloc(iov->iov_base, len);
126 iov->iov_base = nbuf;
131 static int libbpf_netlink_recv(int sock, __u32 nl_pid, int seq,
132 __dump_nlmsg_t _fn, libbpf_dump_nlmsg_t fn,
135 struct iovec iov = {};
136 struct msghdr mhdr = {
140 bool multipart = true;
141 struct nlmsgerr *err;
145 ret = alloc_iov(&iov, 4096);
152 len = netlink_recvmsg(sock, &mhdr, MSG_PEEK | MSG_TRUNC);
158 if (len > iov.iov_len) {
159 ret = alloc_iov(&iov, len);
164 len = netlink_recvmsg(sock, &mhdr, 0);
173 for (nh = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(nh, len);
174 nh = NLMSG_NEXT(nh, len)) {
175 if (nh->nlmsg_pid != nl_pid) {
176 ret = -LIBBPF_ERRNO__WRNGPID;
179 if (nh->nlmsg_seq != seq) {
180 ret = -LIBBPF_ERRNO__INVSEQ;
183 if (nh->nlmsg_flags & NLM_F_MULTI)
185 switch (nh->nlmsg_type) {
187 err = (struct nlmsgerr *)NLMSG_DATA(nh);
191 libbpf_nla_dump_errormsg(nh);
200 ret = _fn(nh, fn, cookie);
221 static int libbpf_netlink_send_recv(struct libbpf_nla_req *req,
222 int proto, __dump_nlmsg_t parse_msg,
223 libbpf_dump_nlmsg_t parse_attr,
229 sock = libbpf_netlink_open(&nl_pid, proto);
233 req->nh.nlmsg_pid = 0;
234 req->nh.nlmsg_seq = time(NULL);
236 if (send(sock, req, req->nh.nlmsg_len, 0) < 0) {
241 ret = libbpf_netlink_recv(sock, nl_pid, req->nh.nlmsg_seq,
242 parse_msg, parse_attr, cookie);
244 libbpf_netlink_close(sock);
248 static int parse_genl_family_id(struct nlmsghdr *nh, libbpf_dump_nlmsg_t fn,
251 struct genlmsghdr *gnl = NLMSG_DATA(nh);
252 struct nlattr *na = (struct nlattr *)((void *)gnl + GENL_HDRLEN);
253 struct nlattr *tb[CTRL_ATTR_FAMILY_ID + 1];
256 libbpf_nla_parse(tb, CTRL_ATTR_FAMILY_ID, na,
257 NLMSG_PAYLOAD(nh, sizeof(*gnl)), NULL);
258 if (!tb[CTRL_ATTR_FAMILY_ID])
261 *id = libbpf_nla_getattr_u16(tb[CTRL_ATTR_FAMILY_ID]);
265 static int libbpf_netlink_resolve_genl_family_id(const char *name,
266 __u16 len, __u16 *id)
268 struct libbpf_nla_req req = {
269 .nh.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN),
270 .nh.nlmsg_type = GENL_ID_CTRL,
271 .nh.nlmsg_flags = NLM_F_REQUEST,
272 .gnl.cmd = CTRL_CMD_GETFAMILY,
277 err = nlattr_add(&req, CTRL_ATTR_FAMILY_NAME, name, len);
281 return libbpf_netlink_send_recv(&req, NETLINK_GENERIC,
282 parse_genl_family_id, NULL, id);
285 static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd,
290 struct libbpf_nla_req req;
292 memset(&req, 0, sizeof(req));
293 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
294 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
295 req.nh.nlmsg_type = RTM_SETLINK;
296 req.ifinfo.ifi_family = AF_UNSPEC;
297 req.ifinfo.ifi_index = ifindex;
299 nla = nlattr_begin_nested(&req, IFLA_XDP);
302 ret = nlattr_add(&req, IFLA_XDP_FD, &fd, sizeof(fd));
306 ret = nlattr_add(&req, IFLA_XDP_FLAGS, &flags, sizeof(flags));
310 if (flags & XDP_FLAGS_REPLACE) {
311 ret = nlattr_add(&req, IFLA_XDP_EXPECTED_FD, &old_fd,
316 nlattr_end_nested(&req, nla);
318 return libbpf_netlink_send_recv(&req, NETLINK_ROUTE, NULL, NULL, NULL);
321 int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags, const struct bpf_xdp_attach_opts *opts)
323 int old_prog_fd, err;
325 if (!OPTS_VALID(opts, bpf_xdp_attach_opts))
326 return libbpf_err(-EINVAL);
328 old_prog_fd = OPTS_GET(opts, old_prog_fd, 0);
330 flags |= XDP_FLAGS_REPLACE;
334 err = __bpf_set_link_xdp_fd_replace(ifindex, prog_fd, old_prog_fd, flags);
335 return libbpf_err(err);
338 int bpf_xdp_detach(int ifindex, __u32 flags, const struct bpf_xdp_attach_opts *opts)
340 return bpf_xdp_attach(ifindex, -1, flags, opts);
343 static int __dump_link_nlmsg(struct nlmsghdr *nlh,
344 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie)
346 struct nlattr *tb[IFLA_MAX + 1], *attr;
347 struct ifinfomsg *ifi = NLMSG_DATA(nlh);
350 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
351 attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
353 if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0)
354 return -LIBBPF_ERRNO__NLPARSE;
356 return dump_link_nlmsg(cookie, ifi, tb);
359 static int get_xdp_info(void *cookie, void *msg, struct nlattr **tb)
361 struct nlattr *xdp_tb[IFLA_XDP_MAX + 1];
362 struct xdp_id_md *xdp_id = cookie;
363 struct ifinfomsg *ifinfo = msg;
366 if (xdp_id->ifindex && xdp_id->ifindex != ifinfo->ifi_index)
372 ret = libbpf_nla_parse_nested(xdp_tb, IFLA_XDP_MAX, tb[IFLA_XDP], NULL);
376 if (!xdp_tb[IFLA_XDP_ATTACHED])
379 xdp_id->info.attach_mode = libbpf_nla_getattr_u8(
380 xdp_tb[IFLA_XDP_ATTACHED]);
382 if (xdp_id->info.attach_mode == XDP_ATTACHED_NONE)
385 if (xdp_tb[IFLA_XDP_PROG_ID])
386 xdp_id->info.prog_id = libbpf_nla_getattr_u32(
387 xdp_tb[IFLA_XDP_PROG_ID]);
389 if (xdp_tb[IFLA_XDP_SKB_PROG_ID])
390 xdp_id->info.skb_prog_id = libbpf_nla_getattr_u32(
391 xdp_tb[IFLA_XDP_SKB_PROG_ID]);
393 if (xdp_tb[IFLA_XDP_DRV_PROG_ID])
394 xdp_id->info.drv_prog_id = libbpf_nla_getattr_u32(
395 xdp_tb[IFLA_XDP_DRV_PROG_ID]);
397 if (xdp_tb[IFLA_XDP_HW_PROG_ID])
398 xdp_id->info.hw_prog_id = libbpf_nla_getattr_u32(
399 xdp_tb[IFLA_XDP_HW_PROG_ID]);
404 static int parse_xdp_features(struct nlmsghdr *nh, libbpf_dump_nlmsg_t fn,
407 struct genlmsghdr *gnl = NLMSG_DATA(nh);
408 struct nlattr *na = (struct nlattr *)((void *)gnl + GENL_HDRLEN);
409 struct nlattr *tb[NETDEV_CMD_MAX + 1];
410 struct xdp_features_md *md = cookie;
413 libbpf_nla_parse(tb, NETDEV_CMD_MAX, na,
414 NLMSG_PAYLOAD(nh, sizeof(*gnl)), NULL);
416 if (!tb[NETDEV_A_DEV_IFINDEX] || !tb[NETDEV_A_DEV_XDP_FEATURES])
419 ifindex = libbpf_nla_getattr_u32(tb[NETDEV_A_DEV_IFINDEX]);
420 if (ifindex != md->ifindex)
423 md->flags = libbpf_nla_getattr_u64(tb[NETDEV_A_DEV_XDP_FEATURES]);
427 int bpf_xdp_query(int ifindex, int xdp_flags, struct bpf_xdp_query_opts *opts)
429 struct libbpf_nla_req req = {
430 .nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
431 .nh.nlmsg_type = RTM_GETLINK,
432 .nh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
433 .ifinfo.ifi_family = AF_PACKET,
435 struct xdp_id_md xdp_id = {};
436 struct xdp_features_md md = {
442 if (!OPTS_VALID(opts, bpf_xdp_query_opts))
443 return libbpf_err(-EINVAL);
445 if (xdp_flags & ~XDP_FLAGS_MASK)
446 return libbpf_err(-EINVAL);
448 /* Check whether the single {HW,DRV,SKB} mode is set */
449 xdp_flags &= XDP_FLAGS_SKB_MODE | XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE;
450 if (xdp_flags & (xdp_flags - 1))
451 return libbpf_err(-EINVAL);
453 xdp_id.ifindex = ifindex;
454 xdp_id.flags = xdp_flags;
456 err = libbpf_netlink_send_recv(&req, NETLINK_ROUTE, __dump_link_nlmsg,
457 get_xdp_info, &xdp_id);
459 return libbpf_err(err);
461 OPTS_SET(opts, prog_id, xdp_id.info.prog_id);
462 OPTS_SET(opts, drv_prog_id, xdp_id.info.drv_prog_id);
463 OPTS_SET(opts, hw_prog_id, xdp_id.info.hw_prog_id);
464 OPTS_SET(opts, skb_prog_id, xdp_id.info.skb_prog_id);
465 OPTS_SET(opts, attach_mode, xdp_id.info.attach_mode);
467 if (!OPTS_HAS(opts, feature_flags))
470 err = libbpf_netlink_resolve_genl_family_id("netdev", sizeof("netdev"), &id);
472 if (err == -ENOENT) {
473 opts->feature_flags = 0;
474 goto skip_feature_flags;
476 return libbpf_err(err);
479 memset(&req, 0, sizeof(req));
480 req.nh.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
481 req.nh.nlmsg_flags = NLM_F_REQUEST;
482 req.nh.nlmsg_type = id;
483 req.gnl.cmd = NETDEV_CMD_DEV_GET;
486 err = nlattr_add(&req, NETDEV_A_DEV_IFINDEX, &ifindex, sizeof(ifindex));
488 return libbpf_err(err);
490 err = libbpf_netlink_send_recv(&req, NETLINK_GENERIC,
491 parse_xdp_features, NULL, &md);
493 return libbpf_err(err);
495 opts->feature_flags = md.flags;
501 int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id)
503 LIBBPF_OPTS(bpf_xdp_query_opts, opts);
506 ret = bpf_xdp_query(ifindex, flags, &opts);
508 return libbpf_err(ret);
510 flags &= XDP_FLAGS_MODES;
512 if (opts.attach_mode != XDP_ATTACHED_MULTI && !flags)
513 *prog_id = opts.prog_id;
514 else if (flags & XDP_FLAGS_DRV_MODE)
515 *prog_id = opts.drv_prog_id;
516 else if (flags & XDP_FLAGS_HW_MODE)
517 *prog_id = opts.hw_prog_id;
518 else if (flags & XDP_FLAGS_SKB_MODE)
519 *prog_id = opts.skb_prog_id;
527 typedef int (*qdisc_config_t)(struct libbpf_nla_req *req);
529 static int clsact_config(struct libbpf_nla_req *req)
531 req->tc.tcm_parent = TC_H_CLSACT;
532 req->tc.tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0);
534 return nlattr_add(req, TCA_KIND, "clsact", sizeof("clsact"));
537 static int attach_point_to_config(struct bpf_tc_hook *hook,
538 qdisc_config_t *config)
540 switch (OPTS_GET(hook, attach_point, 0)) {
543 case BPF_TC_INGRESS | BPF_TC_EGRESS:
544 if (OPTS_GET(hook, parent, 0))
546 *config = &clsact_config;
555 static int tc_get_tcm_parent(enum bpf_tc_attach_point attach_point,
558 switch (attach_point) {
563 *parent = TC_H_MAKE(TC_H_CLSACT,
564 attach_point == BPF_TC_INGRESS ?
565 TC_H_MIN_INGRESS : TC_H_MIN_EGRESS);
577 static int tc_qdisc_modify(struct bpf_tc_hook *hook, int cmd, int flags)
579 qdisc_config_t config;
581 struct libbpf_nla_req req;
583 ret = attach_point_to_config(hook, &config);
587 memset(&req, 0, sizeof(req));
588 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
589 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
590 req.nh.nlmsg_type = cmd;
591 req.tc.tcm_family = AF_UNSPEC;
592 req.tc.tcm_ifindex = OPTS_GET(hook, ifindex, 0);
598 return libbpf_netlink_send_recv(&req, NETLINK_ROUTE, NULL, NULL, NULL);
601 static int tc_qdisc_create_excl(struct bpf_tc_hook *hook)
603 return tc_qdisc_modify(hook, RTM_NEWQDISC, NLM_F_CREATE | NLM_F_EXCL);
606 static int tc_qdisc_delete(struct bpf_tc_hook *hook)
608 return tc_qdisc_modify(hook, RTM_DELQDISC, 0);
611 int bpf_tc_hook_create(struct bpf_tc_hook *hook)
615 if (!hook || !OPTS_VALID(hook, bpf_tc_hook) ||
616 OPTS_GET(hook, ifindex, 0) <= 0)
617 return libbpf_err(-EINVAL);
619 ret = tc_qdisc_create_excl(hook);
620 return libbpf_err(ret);
623 static int __bpf_tc_detach(const struct bpf_tc_hook *hook,
624 const struct bpf_tc_opts *opts,
627 int bpf_tc_hook_destroy(struct bpf_tc_hook *hook)
629 if (!hook || !OPTS_VALID(hook, bpf_tc_hook) ||
630 OPTS_GET(hook, ifindex, 0) <= 0)
631 return libbpf_err(-EINVAL);
633 switch (OPTS_GET(hook, attach_point, 0)) {
636 return libbpf_err(__bpf_tc_detach(hook, NULL, true));
637 case BPF_TC_INGRESS | BPF_TC_EGRESS:
638 return libbpf_err(tc_qdisc_delete(hook));
640 return libbpf_err(-EOPNOTSUPP);
642 return libbpf_err(-EINVAL);
647 struct bpf_tc_opts *opts;
651 static int __get_tc_info(void *cookie, struct tcmsg *tc, struct nlattr **tb,
654 struct nlattr *tbb[TCA_BPF_MAX + 1];
655 struct bpf_cb_ctx *info = cookie;
657 if (!info || !info->opts)
659 if (unicast && info->processed)
661 if (!tb[TCA_OPTIONS])
664 libbpf_nla_parse_nested(tbb, TCA_BPF_MAX, tb[TCA_OPTIONS], NULL);
665 if (!tbb[TCA_BPF_ID])
668 OPTS_SET(info->opts, prog_id, libbpf_nla_getattr_u32(tbb[TCA_BPF_ID]));
669 OPTS_SET(info->opts, handle, tc->tcm_handle);
670 OPTS_SET(info->opts, priority, TC_H_MAJ(tc->tcm_info) >> 16);
672 info->processed = true;
673 return unicast ? NL_NEXT : NL_DONE;
676 static int get_tc_info(struct nlmsghdr *nh, libbpf_dump_nlmsg_t fn,
679 struct tcmsg *tc = NLMSG_DATA(nh);
680 struct nlattr *tb[TCA_MAX + 1];
682 libbpf_nla_parse(tb, TCA_MAX,
683 (struct nlattr *)((void *)tc + NLMSG_ALIGN(sizeof(*tc))),
684 NLMSG_PAYLOAD(nh, sizeof(*tc)), NULL);
687 return __get_tc_info(cookie, tc, tb, nh->nlmsg_flags & NLM_F_ECHO);
690 static int tc_add_fd_and_name(struct libbpf_nla_req *req, int fd)
692 struct bpf_prog_info info;
693 __u32 info_len = sizeof(info);
697 memset(&info, 0, info_len);
698 ret = bpf_prog_get_info_by_fd(fd, &info, &info_len);
702 ret = nlattr_add(req, TCA_BPF_FD, &fd, sizeof(fd));
705 len = snprintf(name, sizeof(name), "%s:[%u]", info.name, info.id);
708 if (len >= sizeof(name))
709 return -ENAMETOOLONG;
710 return nlattr_add(req, TCA_BPF_NAME, name, len + 1);
713 int bpf_tc_attach(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts)
715 __u32 protocol, bpf_flags, handle, priority, parent, prog_id, flags;
716 int ret, ifindex, attach_point, prog_fd;
717 struct bpf_cb_ctx info = {};
718 struct libbpf_nla_req req;
721 if (!hook || !opts ||
722 !OPTS_VALID(hook, bpf_tc_hook) ||
723 !OPTS_VALID(opts, bpf_tc_opts))
724 return libbpf_err(-EINVAL);
726 ifindex = OPTS_GET(hook, ifindex, 0);
727 parent = OPTS_GET(hook, parent, 0);
728 attach_point = OPTS_GET(hook, attach_point, 0);
730 handle = OPTS_GET(opts, handle, 0);
731 priority = OPTS_GET(opts, priority, 0);
732 prog_fd = OPTS_GET(opts, prog_fd, 0);
733 prog_id = OPTS_GET(opts, prog_id, 0);
734 flags = OPTS_GET(opts, flags, 0);
736 if (ifindex <= 0 || !prog_fd || prog_id)
737 return libbpf_err(-EINVAL);
738 if (priority > UINT16_MAX)
739 return libbpf_err(-EINVAL);
740 if (flags & ~BPF_TC_F_REPLACE)
741 return libbpf_err(-EINVAL);
743 flags = (flags & BPF_TC_F_REPLACE) ? NLM_F_REPLACE : NLM_F_EXCL;
744 protocol = ETH_P_ALL;
746 memset(&req, 0, sizeof(req));
747 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
748 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE |
750 req.nh.nlmsg_type = RTM_NEWTFILTER;
751 req.tc.tcm_family = AF_UNSPEC;
752 req.tc.tcm_ifindex = ifindex;
753 req.tc.tcm_handle = handle;
754 req.tc.tcm_info = TC_H_MAKE(priority << 16, htons(protocol));
756 ret = tc_get_tcm_parent(attach_point, &parent);
758 return libbpf_err(ret);
759 req.tc.tcm_parent = parent;
761 ret = nlattr_add(&req, TCA_KIND, "bpf", sizeof("bpf"));
763 return libbpf_err(ret);
764 nla = nlattr_begin_nested(&req, TCA_OPTIONS);
766 return libbpf_err(-EMSGSIZE);
767 ret = tc_add_fd_and_name(&req, prog_fd);
769 return libbpf_err(ret);
770 bpf_flags = TCA_BPF_FLAG_ACT_DIRECT;
771 ret = nlattr_add(&req, TCA_BPF_FLAGS, &bpf_flags, sizeof(bpf_flags));
773 return libbpf_err(ret);
774 nlattr_end_nested(&req, nla);
778 ret = libbpf_netlink_send_recv(&req, NETLINK_ROUTE, get_tc_info, NULL,
781 return libbpf_err(ret);
783 return libbpf_err(-ENOENT);
787 static int __bpf_tc_detach(const struct bpf_tc_hook *hook,
788 const struct bpf_tc_opts *opts,
791 __u32 protocol = 0, handle, priority, parent, prog_id, flags;
792 int ret, ifindex, attach_point, prog_fd;
793 struct libbpf_nla_req req;
796 !OPTS_VALID(hook, bpf_tc_hook) ||
797 !OPTS_VALID(opts, bpf_tc_opts))
800 ifindex = OPTS_GET(hook, ifindex, 0);
801 parent = OPTS_GET(hook, parent, 0);
802 attach_point = OPTS_GET(hook, attach_point, 0);
804 handle = OPTS_GET(opts, handle, 0);
805 priority = OPTS_GET(opts, priority, 0);
806 prog_fd = OPTS_GET(opts, prog_fd, 0);
807 prog_id = OPTS_GET(opts, prog_id, 0);
808 flags = OPTS_GET(opts, flags, 0);
810 if (ifindex <= 0 || flags || prog_fd || prog_id)
812 if (priority > UINT16_MAX)
815 if (!handle || !priority)
817 protocol = ETH_P_ALL;
819 if (handle || priority)
823 memset(&req, 0, sizeof(req));
824 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
825 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
826 req.nh.nlmsg_type = RTM_DELTFILTER;
827 req.tc.tcm_family = AF_UNSPEC;
828 req.tc.tcm_ifindex = ifindex;
830 req.tc.tcm_handle = handle;
831 req.tc.tcm_info = TC_H_MAKE(priority << 16, htons(protocol));
834 ret = tc_get_tcm_parent(attach_point, &parent);
837 req.tc.tcm_parent = parent;
840 ret = nlattr_add(&req, TCA_KIND, "bpf", sizeof("bpf"));
845 return libbpf_netlink_send_recv(&req, NETLINK_ROUTE, NULL, NULL, NULL);
848 int bpf_tc_detach(const struct bpf_tc_hook *hook,
849 const struct bpf_tc_opts *opts)
854 return libbpf_err(-EINVAL);
856 ret = __bpf_tc_detach(hook, opts, false);
857 return libbpf_err(ret);
860 int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts)
862 __u32 protocol, handle, priority, parent, prog_id, flags;
863 int ret, ifindex, attach_point, prog_fd;
864 struct bpf_cb_ctx info = {};
865 struct libbpf_nla_req req;
867 if (!hook || !opts ||
868 !OPTS_VALID(hook, bpf_tc_hook) ||
869 !OPTS_VALID(opts, bpf_tc_opts))
870 return libbpf_err(-EINVAL);
872 ifindex = OPTS_GET(hook, ifindex, 0);
873 parent = OPTS_GET(hook, parent, 0);
874 attach_point = OPTS_GET(hook, attach_point, 0);
876 handle = OPTS_GET(opts, handle, 0);
877 priority = OPTS_GET(opts, priority, 0);
878 prog_fd = OPTS_GET(opts, prog_fd, 0);
879 prog_id = OPTS_GET(opts, prog_id, 0);
880 flags = OPTS_GET(opts, flags, 0);
882 if (ifindex <= 0 || flags || prog_fd || prog_id ||
883 !handle || !priority)
884 return libbpf_err(-EINVAL);
885 if (priority > UINT16_MAX)
886 return libbpf_err(-EINVAL);
888 protocol = ETH_P_ALL;
890 memset(&req, 0, sizeof(req));
891 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
892 req.nh.nlmsg_flags = NLM_F_REQUEST;
893 req.nh.nlmsg_type = RTM_GETTFILTER;
894 req.tc.tcm_family = AF_UNSPEC;
895 req.tc.tcm_ifindex = ifindex;
896 req.tc.tcm_handle = handle;
897 req.tc.tcm_info = TC_H_MAKE(priority << 16, htons(protocol));
899 ret = tc_get_tcm_parent(attach_point, &parent);
901 return libbpf_err(ret);
902 req.tc.tcm_parent = parent;
904 ret = nlattr_add(&req, TCA_KIND, "bpf", sizeof("bpf"));
906 return libbpf_err(ret);
910 ret = libbpf_netlink_send_recv(&req, NETLINK_ROUTE, get_tc_info, NULL,
913 return libbpf_err(ret);
915 return libbpf_err(-ENOENT);