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 <sys/socket.h>
18 #include "libbpf_internal.h"
22 #define SOL_NETLINK 270
25 typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
27 typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, libbpf_dump_nlmsg_t,
33 struct xdp_link_info info;
36 static int libbpf_netlink_open(__u32 *nl_pid)
38 struct sockaddr_nl sa;
43 memset(&sa, 0, sizeof(sa));
44 sa.nl_family = AF_NETLINK;
46 sock = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
50 if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
51 &one, sizeof(one)) < 0) {
52 pr_warn("Netlink error reporting not supported\n");
55 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
61 if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
66 if (addrlen != sizeof(sa)) {
67 ret = -LIBBPF_ERRNO__INTERNAL;
79 static void libbpf_netlink_close(int sock)
90 static int libbpf_netlink_recv(int sock, __u32 nl_pid, int seq,
91 __dump_nlmsg_t _fn, libbpf_dump_nlmsg_t fn,
94 bool multipart = true;
103 len = recv(sock, buf, sizeof(buf), 0);
112 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
113 nh = NLMSG_NEXT(nh, len)) {
114 if (nh->nlmsg_pid != nl_pid) {
115 ret = -LIBBPF_ERRNO__WRNGPID;
118 if (nh->nlmsg_seq != seq) {
119 ret = -LIBBPF_ERRNO__INVSEQ;
122 if (nh->nlmsg_flags & NLM_F_MULTI)
124 switch (nh->nlmsg_type) {
126 err = (struct nlmsgerr *)NLMSG_DATA(nh);
130 libbpf_nla_dump_errormsg(nh);
138 ret = _fn(nh, fn, cookie);
157 static int libbpf_netlink_send_recv(struct libbpf_nla_req *req,
158 __dump_nlmsg_t parse_msg,
159 libbpf_dump_nlmsg_t parse_attr,
165 sock = libbpf_netlink_open(&nl_pid);
169 req->nh.nlmsg_pid = 0;
170 req->nh.nlmsg_seq = time(NULL);
172 if (send(sock, req, req->nh.nlmsg_len, 0) < 0) {
177 ret = libbpf_netlink_recv(sock, nl_pid, req->nh.nlmsg_seq,
178 parse_msg, parse_attr, cookie);
180 libbpf_netlink_close(sock);
184 static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd,
189 struct libbpf_nla_req req;
191 memset(&req, 0, sizeof(req));
192 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
193 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
194 req.nh.nlmsg_type = RTM_SETLINK;
195 req.ifinfo.ifi_family = AF_UNSPEC;
196 req.ifinfo.ifi_index = ifindex;
198 nla = nlattr_begin_nested(&req, IFLA_XDP);
201 ret = nlattr_add(&req, IFLA_XDP_FD, &fd, sizeof(fd));
205 ret = nlattr_add(&req, IFLA_XDP_FLAGS, &flags, sizeof(flags));
209 if (flags & XDP_FLAGS_REPLACE) {
210 ret = nlattr_add(&req, IFLA_XDP_EXPECTED_FD, &old_fd,
215 nlattr_end_nested(&req, nla);
217 return libbpf_netlink_send_recv(&req, NULL, NULL, NULL);
220 int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
221 const struct bpf_xdp_set_link_opts *opts)
223 int old_fd = -1, ret;
225 if (!OPTS_VALID(opts, bpf_xdp_set_link_opts))
226 return libbpf_err(-EINVAL);
228 if (OPTS_HAS(opts, old_fd)) {
229 old_fd = OPTS_GET(opts, old_fd, -1);
230 flags |= XDP_FLAGS_REPLACE;
233 ret = __bpf_set_link_xdp_fd_replace(ifindex, fd, old_fd, flags);
234 return libbpf_err(ret);
237 int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
241 ret = __bpf_set_link_xdp_fd_replace(ifindex, fd, 0, flags);
242 return libbpf_err(ret);
245 static int __dump_link_nlmsg(struct nlmsghdr *nlh,
246 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie)
248 struct nlattr *tb[IFLA_MAX + 1], *attr;
249 struct ifinfomsg *ifi = NLMSG_DATA(nlh);
252 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
253 attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
255 if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0)
256 return -LIBBPF_ERRNO__NLPARSE;
258 return dump_link_nlmsg(cookie, ifi, tb);
261 static int get_xdp_info(void *cookie, void *msg, struct nlattr **tb)
263 struct nlattr *xdp_tb[IFLA_XDP_MAX + 1];
264 struct xdp_id_md *xdp_id = cookie;
265 struct ifinfomsg *ifinfo = msg;
268 if (xdp_id->ifindex && xdp_id->ifindex != ifinfo->ifi_index)
274 ret = libbpf_nla_parse_nested(xdp_tb, IFLA_XDP_MAX, tb[IFLA_XDP], NULL);
278 if (!xdp_tb[IFLA_XDP_ATTACHED])
281 xdp_id->info.attach_mode = libbpf_nla_getattr_u8(
282 xdp_tb[IFLA_XDP_ATTACHED]);
284 if (xdp_id->info.attach_mode == XDP_ATTACHED_NONE)
287 if (xdp_tb[IFLA_XDP_PROG_ID])
288 xdp_id->info.prog_id = libbpf_nla_getattr_u32(
289 xdp_tb[IFLA_XDP_PROG_ID]);
291 if (xdp_tb[IFLA_XDP_SKB_PROG_ID])
292 xdp_id->info.skb_prog_id = libbpf_nla_getattr_u32(
293 xdp_tb[IFLA_XDP_SKB_PROG_ID]);
295 if (xdp_tb[IFLA_XDP_DRV_PROG_ID])
296 xdp_id->info.drv_prog_id = libbpf_nla_getattr_u32(
297 xdp_tb[IFLA_XDP_DRV_PROG_ID]);
299 if (xdp_tb[IFLA_XDP_HW_PROG_ID])
300 xdp_id->info.hw_prog_id = libbpf_nla_getattr_u32(
301 xdp_tb[IFLA_XDP_HW_PROG_ID]);
306 int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
307 size_t info_size, __u32 flags)
309 struct xdp_id_md xdp_id = {};
312 struct libbpf_nla_req req = {
313 .nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
314 .nh.nlmsg_type = RTM_GETLINK,
315 .nh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
316 .ifinfo.ifi_family = AF_PACKET,
319 if (flags & ~XDP_FLAGS_MASK || !info_size)
320 return libbpf_err(-EINVAL);
322 /* Check whether the single {HW,DRV,SKB} mode is set */
323 flags &= (XDP_FLAGS_SKB_MODE | XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE);
325 if (flags && flags & mask)
326 return libbpf_err(-EINVAL);
328 xdp_id.ifindex = ifindex;
329 xdp_id.flags = flags;
331 ret = libbpf_netlink_send_recv(&req, __dump_link_nlmsg,
332 get_xdp_info, &xdp_id);
334 size_t sz = min(info_size, sizeof(xdp_id.info));
336 memcpy(info, &xdp_id.info, sz);
337 memset((void *) info + sz, 0, info_size - sz);
340 return libbpf_err(ret);
343 static __u32 get_xdp_id(struct xdp_link_info *info, __u32 flags)
345 flags &= XDP_FLAGS_MODES;
347 if (info->attach_mode != XDP_ATTACHED_MULTI && !flags)
348 return info->prog_id;
349 if (flags & XDP_FLAGS_DRV_MODE)
350 return info->drv_prog_id;
351 if (flags & XDP_FLAGS_HW_MODE)
352 return info->hw_prog_id;
353 if (flags & XDP_FLAGS_SKB_MODE)
354 return info->skb_prog_id;
359 int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags)
361 struct xdp_link_info info;
364 ret = bpf_get_link_xdp_info(ifindex, &info, sizeof(info), flags);
366 *prog_id = get_xdp_id(&info, flags);
368 return libbpf_err(ret);
371 typedef int (*qdisc_config_t)(struct libbpf_nla_req *req);
373 static int clsact_config(struct libbpf_nla_req *req)
375 req->tc.tcm_parent = TC_H_CLSACT;
376 req->tc.tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0);
378 return nlattr_add(req, TCA_KIND, "clsact", sizeof("clsact"));
381 static int attach_point_to_config(struct bpf_tc_hook *hook,
382 qdisc_config_t *config)
384 switch (OPTS_GET(hook, attach_point, 0)) {
387 case BPF_TC_INGRESS | BPF_TC_EGRESS:
388 if (OPTS_GET(hook, parent, 0))
390 *config = &clsact_config;
399 static int tc_get_tcm_parent(enum bpf_tc_attach_point attach_point,
402 switch (attach_point) {
407 *parent = TC_H_MAKE(TC_H_CLSACT,
408 attach_point == BPF_TC_INGRESS ?
409 TC_H_MIN_INGRESS : TC_H_MIN_EGRESS);
421 static int tc_qdisc_modify(struct bpf_tc_hook *hook, int cmd, int flags)
423 qdisc_config_t config;
425 struct libbpf_nla_req req;
427 ret = attach_point_to_config(hook, &config);
431 memset(&req, 0, sizeof(req));
432 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
433 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
434 req.nh.nlmsg_type = cmd;
435 req.tc.tcm_family = AF_UNSPEC;
436 req.tc.tcm_ifindex = OPTS_GET(hook, ifindex, 0);
442 return libbpf_netlink_send_recv(&req, NULL, NULL, NULL);
445 static int tc_qdisc_create_excl(struct bpf_tc_hook *hook)
447 return tc_qdisc_modify(hook, RTM_NEWQDISC, NLM_F_CREATE | NLM_F_EXCL);
450 static int tc_qdisc_delete(struct bpf_tc_hook *hook)
452 return tc_qdisc_modify(hook, RTM_DELQDISC, 0);
455 int bpf_tc_hook_create(struct bpf_tc_hook *hook)
459 if (!hook || !OPTS_VALID(hook, bpf_tc_hook) ||
460 OPTS_GET(hook, ifindex, 0) <= 0)
461 return libbpf_err(-EINVAL);
463 ret = tc_qdisc_create_excl(hook);
464 return libbpf_err(ret);
467 static int __bpf_tc_detach(const struct bpf_tc_hook *hook,
468 const struct bpf_tc_opts *opts,
471 int bpf_tc_hook_destroy(struct bpf_tc_hook *hook)
473 if (!hook || !OPTS_VALID(hook, bpf_tc_hook) ||
474 OPTS_GET(hook, ifindex, 0) <= 0)
475 return libbpf_err(-EINVAL);
477 switch (OPTS_GET(hook, attach_point, 0)) {
480 return libbpf_err(__bpf_tc_detach(hook, NULL, true));
481 case BPF_TC_INGRESS | BPF_TC_EGRESS:
482 return libbpf_err(tc_qdisc_delete(hook));
484 return libbpf_err(-EOPNOTSUPP);
486 return libbpf_err(-EINVAL);
491 struct bpf_tc_opts *opts;
495 static int __get_tc_info(void *cookie, struct tcmsg *tc, struct nlattr **tb,
498 struct nlattr *tbb[TCA_BPF_MAX + 1];
499 struct bpf_cb_ctx *info = cookie;
501 if (!info || !info->opts)
503 if (unicast && info->processed)
505 if (!tb[TCA_OPTIONS])
508 libbpf_nla_parse_nested(tbb, TCA_BPF_MAX, tb[TCA_OPTIONS], NULL);
509 if (!tbb[TCA_BPF_ID])
512 OPTS_SET(info->opts, prog_id, libbpf_nla_getattr_u32(tbb[TCA_BPF_ID]));
513 OPTS_SET(info->opts, handle, tc->tcm_handle);
514 OPTS_SET(info->opts, priority, TC_H_MAJ(tc->tcm_info) >> 16);
516 info->processed = true;
517 return unicast ? NL_NEXT : NL_DONE;
520 static int get_tc_info(struct nlmsghdr *nh, libbpf_dump_nlmsg_t fn,
523 struct tcmsg *tc = NLMSG_DATA(nh);
524 struct nlattr *tb[TCA_MAX + 1];
526 libbpf_nla_parse(tb, TCA_MAX,
527 (struct nlattr *)((void *)tc + NLMSG_ALIGN(sizeof(*tc))),
528 NLMSG_PAYLOAD(nh, sizeof(*tc)), NULL);
531 return __get_tc_info(cookie, tc, tb, nh->nlmsg_flags & NLM_F_ECHO);
534 static int tc_add_fd_and_name(struct libbpf_nla_req *req, int fd)
536 struct bpf_prog_info info = {};
537 __u32 info_len = sizeof(info);
541 ret = bpf_obj_get_info_by_fd(fd, &info, &info_len);
545 ret = nlattr_add(req, TCA_BPF_FD, &fd, sizeof(fd));
548 len = snprintf(name, sizeof(name), "%s:[%u]", info.name, info.id);
551 if (len >= sizeof(name))
552 return -ENAMETOOLONG;
553 return nlattr_add(req, TCA_BPF_NAME, name, len + 1);
556 int bpf_tc_attach(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts)
558 __u32 protocol, bpf_flags, handle, priority, parent, prog_id, flags;
559 int ret, ifindex, attach_point, prog_fd;
560 struct bpf_cb_ctx info = {};
561 struct libbpf_nla_req req;
564 if (!hook || !opts ||
565 !OPTS_VALID(hook, bpf_tc_hook) ||
566 !OPTS_VALID(opts, bpf_tc_opts))
567 return libbpf_err(-EINVAL);
569 ifindex = OPTS_GET(hook, ifindex, 0);
570 parent = OPTS_GET(hook, parent, 0);
571 attach_point = OPTS_GET(hook, attach_point, 0);
573 handle = OPTS_GET(opts, handle, 0);
574 priority = OPTS_GET(opts, priority, 0);
575 prog_fd = OPTS_GET(opts, prog_fd, 0);
576 prog_id = OPTS_GET(opts, prog_id, 0);
577 flags = OPTS_GET(opts, flags, 0);
579 if (ifindex <= 0 || !prog_fd || prog_id)
580 return libbpf_err(-EINVAL);
581 if (priority > UINT16_MAX)
582 return libbpf_err(-EINVAL);
583 if (flags & ~BPF_TC_F_REPLACE)
584 return libbpf_err(-EINVAL);
586 flags = (flags & BPF_TC_F_REPLACE) ? NLM_F_REPLACE : NLM_F_EXCL;
587 protocol = ETH_P_ALL;
589 memset(&req, 0, sizeof(req));
590 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
591 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE |
593 req.nh.nlmsg_type = RTM_NEWTFILTER;
594 req.tc.tcm_family = AF_UNSPEC;
595 req.tc.tcm_ifindex = ifindex;
596 req.tc.tcm_handle = handle;
597 req.tc.tcm_info = TC_H_MAKE(priority << 16, htons(protocol));
599 ret = tc_get_tcm_parent(attach_point, &parent);
601 return libbpf_err(ret);
602 req.tc.tcm_parent = parent;
604 ret = nlattr_add(&req, TCA_KIND, "bpf", sizeof("bpf"));
606 return libbpf_err(ret);
607 nla = nlattr_begin_nested(&req, TCA_OPTIONS);
609 return libbpf_err(-EMSGSIZE);
610 ret = tc_add_fd_and_name(&req, prog_fd);
612 return libbpf_err(ret);
613 bpf_flags = TCA_BPF_FLAG_ACT_DIRECT;
614 ret = nlattr_add(&req, TCA_BPF_FLAGS, &bpf_flags, sizeof(bpf_flags));
616 return libbpf_err(ret);
617 nlattr_end_nested(&req, nla);
621 ret = libbpf_netlink_send_recv(&req, get_tc_info, NULL, &info);
623 return libbpf_err(ret);
625 return libbpf_err(-ENOENT);
629 static int __bpf_tc_detach(const struct bpf_tc_hook *hook,
630 const struct bpf_tc_opts *opts,
633 __u32 protocol = 0, handle, priority, parent, prog_id, flags;
634 int ret, ifindex, attach_point, prog_fd;
635 struct libbpf_nla_req req;
638 !OPTS_VALID(hook, bpf_tc_hook) ||
639 !OPTS_VALID(opts, bpf_tc_opts))
642 ifindex = OPTS_GET(hook, ifindex, 0);
643 parent = OPTS_GET(hook, parent, 0);
644 attach_point = OPTS_GET(hook, attach_point, 0);
646 handle = OPTS_GET(opts, handle, 0);
647 priority = OPTS_GET(opts, priority, 0);
648 prog_fd = OPTS_GET(opts, prog_fd, 0);
649 prog_id = OPTS_GET(opts, prog_id, 0);
650 flags = OPTS_GET(opts, flags, 0);
652 if (ifindex <= 0 || flags || prog_fd || prog_id)
654 if (priority > UINT16_MAX)
657 if (!handle || !priority)
659 protocol = ETH_P_ALL;
661 if (handle || priority)
665 memset(&req, 0, sizeof(req));
666 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
667 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
668 req.nh.nlmsg_type = RTM_DELTFILTER;
669 req.tc.tcm_family = AF_UNSPEC;
670 req.tc.tcm_ifindex = ifindex;
672 req.tc.tcm_handle = handle;
673 req.tc.tcm_info = TC_H_MAKE(priority << 16, htons(protocol));
676 ret = tc_get_tcm_parent(attach_point, &parent);
679 req.tc.tcm_parent = parent;
682 ret = nlattr_add(&req, TCA_KIND, "bpf", sizeof("bpf"));
687 return libbpf_netlink_send_recv(&req, NULL, NULL, NULL);
690 int bpf_tc_detach(const struct bpf_tc_hook *hook,
691 const struct bpf_tc_opts *opts)
696 return libbpf_err(-EINVAL);
698 ret = __bpf_tc_detach(hook, opts, false);
699 return libbpf_err(ret);
702 int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts)
704 __u32 protocol, handle, priority, parent, prog_id, flags;
705 int ret, ifindex, attach_point, prog_fd;
706 struct bpf_cb_ctx info = {};
707 struct libbpf_nla_req req;
709 if (!hook || !opts ||
710 !OPTS_VALID(hook, bpf_tc_hook) ||
711 !OPTS_VALID(opts, bpf_tc_opts))
712 return libbpf_err(-EINVAL);
714 ifindex = OPTS_GET(hook, ifindex, 0);
715 parent = OPTS_GET(hook, parent, 0);
716 attach_point = OPTS_GET(hook, attach_point, 0);
718 handle = OPTS_GET(opts, handle, 0);
719 priority = OPTS_GET(opts, priority, 0);
720 prog_fd = OPTS_GET(opts, prog_fd, 0);
721 prog_id = OPTS_GET(opts, prog_id, 0);
722 flags = OPTS_GET(opts, flags, 0);
724 if (ifindex <= 0 || flags || prog_fd || prog_id ||
725 !handle || !priority)
726 return libbpf_err(-EINVAL);
727 if (priority > UINT16_MAX)
728 return libbpf_err(-EINVAL);
730 protocol = ETH_P_ALL;
732 memset(&req, 0, sizeof(req));
733 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
734 req.nh.nlmsg_flags = NLM_F_REQUEST;
735 req.nh.nlmsg_type = RTM_GETTFILTER;
736 req.tc.tcm_family = AF_UNSPEC;
737 req.tc.tcm_ifindex = ifindex;
738 req.tc.tcm_handle = handle;
739 req.tc.tcm_info = TC_H_MAKE(priority << 16, htons(protocol));
741 ret = tc_get_tcm_parent(attach_point, &parent);
743 return libbpf_err(ret);
744 req.tc.tcm_parent = parent;
746 ret = nlattr_add(&req, TCA_KIND, "bpf", sizeof("bpf"));
748 return libbpf_err(ret);
752 ret = libbpf_netlink_send_recv(&req, get_tc_info, NULL, &info);
754 return libbpf_err(ret);
756 return libbpf_err(-ENOENT);