1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
8 #include <linux/rtnetlink.h>
9 #include <sys/socket.h>
18 #define SOL_NETLINK 270
21 typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, libbpf_dump_nlmsg_t,
30 int libbpf_netlink_open(__u32 *nl_pid)
32 struct sockaddr_nl sa;
37 memset(&sa, 0, sizeof(sa));
38 sa.nl_family = AF_NETLINK;
40 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
44 if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
45 &one, sizeof(one)) < 0) {
46 fprintf(stderr, "Netlink error reporting not supported\n");
49 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
55 if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
60 if (addrlen != sizeof(sa)) {
61 ret = -LIBBPF_ERRNO__INTERNAL;
73 static int bpf_netlink_recv(int sock, __u32 nl_pid, int seq,
74 __dump_nlmsg_t _fn, libbpf_dump_nlmsg_t fn,
77 bool multipart = true;
85 len = recv(sock, buf, sizeof(buf), 0);
94 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
95 nh = NLMSG_NEXT(nh, len)) {
96 if (nh->nlmsg_pid != nl_pid) {
97 ret = -LIBBPF_ERRNO__WRNGPID;
100 if (nh->nlmsg_seq != seq) {
101 ret = -LIBBPF_ERRNO__INVSEQ;
104 if (nh->nlmsg_flags & NLM_F_MULTI)
106 switch (nh->nlmsg_type) {
108 err = (struct nlmsgerr *)NLMSG_DATA(nh);
112 libbpf_nla_dump_errormsg(nh);
120 ret = _fn(nh, fn, cookie);
131 int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
133 int sock, seq = 0, ret;
134 struct nlattr *nla, *nla_xdp;
137 struct ifinfomsg ifinfo;
142 sock = libbpf_netlink_open(&nl_pid);
146 memset(&req, 0, sizeof(req));
147 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
148 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
149 req.nh.nlmsg_type = RTM_SETLINK;
150 req.nh.nlmsg_pid = 0;
151 req.nh.nlmsg_seq = ++seq;
152 req.ifinfo.ifi_family = AF_UNSPEC;
153 req.ifinfo.ifi_index = ifindex;
155 /* started nested attribute for XDP */
156 nla = (struct nlattr *)(((char *)&req)
157 + NLMSG_ALIGN(req.nh.nlmsg_len));
158 nla->nla_type = NLA_F_NESTED | IFLA_XDP;
159 nla->nla_len = NLA_HDRLEN;
162 nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
163 nla_xdp->nla_type = IFLA_XDP_FD;
164 nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
165 memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
166 nla->nla_len += nla_xdp->nla_len;
168 /* if user passed in any flags, add those too */
170 nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
171 nla_xdp->nla_type = IFLA_XDP_FLAGS;
172 nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags);
173 memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags));
174 nla->nla_len += nla_xdp->nla_len;
177 req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
179 if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
183 ret = bpf_netlink_recv(sock, nl_pid, seq, NULL, NULL, NULL);
190 static int __dump_link_nlmsg(struct nlmsghdr *nlh,
191 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie)
193 struct nlattr *tb[IFLA_MAX + 1], *attr;
194 struct ifinfomsg *ifi = NLMSG_DATA(nlh);
197 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
198 attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
199 if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0)
200 return -LIBBPF_ERRNO__NLPARSE;
202 return dump_link_nlmsg(cookie, ifi, tb);
205 static unsigned char get_xdp_id_attr(unsigned char mode, __u32 flags)
207 if (mode != XDP_ATTACHED_MULTI)
208 return IFLA_XDP_PROG_ID;
209 if (flags & XDP_FLAGS_DRV_MODE)
210 return IFLA_XDP_DRV_PROG_ID;
211 if (flags & XDP_FLAGS_HW_MODE)
212 return IFLA_XDP_HW_PROG_ID;
213 if (flags & XDP_FLAGS_SKB_MODE)
214 return IFLA_XDP_SKB_PROG_ID;
216 return IFLA_XDP_UNSPEC;
219 static int get_xdp_id(void *cookie, void *msg, struct nlattr **tb)
221 struct nlattr *xdp_tb[IFLA_XDP_MAX + 1];
222 struct xdp_id_md *xdp_id = cookie;
223 struct ifinfomsg *ifinfo = msg;
224 unsigned char mode, xdp_attr;
227 if (xdp_id->ifindex && xdp_id->ifindex != ifinfo->ifi_index)
233 ret = libbpf_nla_parse_nested(xdp_tb, IFLA_XDP_MAX, tb[IFLA_XDP], NULL);
237 if (!xdp_tb[IFLA_XDP_ATTACHED])
240 mode = libbpf_nla_getattr_u8(xdp_tb[IFLA_XDP_ATTACHED]);
241 if (mode == XDP_ATTACHED_NONE)
244 xdp_attr = get_xdp_id_attr(mode, xdp_id->flags);
245 if (!xdp_attr || !xdp_tb[xdp_attr])
248 xdp_id->id = libbpf_nla_getattr_u32(xdp_tb[xdp_attr]);
253 int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags)
255 struct xdp_id_md xdp_id = {};
260 if (flags & ~XDP_FLAGS_MASK)
263 /* Check whether the single {HW,DRV,SKB} mode is set */
264 flags &= (XDP_FLAGS_SKB_MODE | XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE);
266 if (flags && flags & mask)
269 sock = libbpf_netlink_open(&nl_pid);
273 xdp_id.ifindex = ifindex;
274 xdp_id.flags = flags;
276 ret = libbpf_nl_get_link(sock, nl_pid, get_xdp_id, &xdp_id);
278 *prog_id = xdp_id.id;
284 int libbpf_nl_get_link(int sock, unsigned int nl_pid,
285 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie)
289 struct ifinfomsg ifm;
291 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
292 .nlh.nlmsg_type = RTM_GETLINK,
293 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
294 .ifm.ifi_family = AF_PACKET,
296 int seq = time(NULL);
298 req.nlh.nlmsg_seq = seq;
299 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
302 return bpf_netlink_recv(sock, nl_pid, seq, __dump_link_nlmsg,
303 dump_link_nlmsg, cookie);
306 static int __dump_class_nlmsg(struct nlmsghdr *nlh,
307 libbpf_dump_nlmsg_t dump_class_nlmsg,
310 struct nlattr *tb[TCA_MAX + 1], *attr;
311 struct tcmsg *t = NLMSG_DATA(nlh);
314 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
315 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
316 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
317 return -LIBBPF_ERRNO__NLPARSE;
319 return dump_class_nlmsg(cookie, t, tb);
322 int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
323 libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie)
329 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
330 .nlh.nlmsg_type = RTM_GETTCLASS,
331 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
332 .t.tcm_family = AF_UNSPEC,
333 .t.tcm_ifindex = ifindex,
335 int seq = time(NULL);
337 req.nlh.nlmsg_seq = seq;
338 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
341 return bpf_netlink_recv(sock, nl_pid, seq, __dump_class_nlmsg,
342 dump_class_nlmsg, cookie);
345 static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh,
346 libbpf_dump_nlmsg_t dump_qdisc_nlmsg,
349 struct nlattr *tb[TCA_MAX + 1], *attr;
350 struct tcmsg *t = NLMSG_DATA(nlh);
353 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
354 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
355 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
356 return -LIBBPF_ERRNO__NLPARSE;
358 return dump_qdisc_nlmsg(cookie, t, tb);
361 int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
362 libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie)
368 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
369 .nlh.nlmsg_type = RTM_GETQDISC,
370 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
371 .t.tcm_family = AF_UNSPEC,
372 .t.tcm_ifindex = ifindex,
374 int seq = time(NULL);
376 req.nlh.nlmsg_seq = seq;
377 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
380 return bpf_netlink_recv(sock, nl_pid, seq, __dump_qdisc_nlmsg,
381 dump_qdisc_nlmsg, cookie);
384 static int __dump_filter_nlmsg(struct nlmsghdr *nlh,
385 libbpf_dump_nlmsg_t dump_filter_nlmsg,
388 struct nlattr *tb[TCA_MAX + 1], *attr;
389 struct tcmsg *t = NLMSG_DATA(nlh);
392 len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
393 attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
394 if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
395 return -LIBBPF_ERRNO__NLPARSE;
397 return dump_filter_nlmsg(cookie, t, tb);
400 int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
401 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie)
407 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
408 .nlh.nlmsg_type = RTM_GETTFILTER,
409 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
410 .t.tcm_family = AF_UNSPEC,
411 .t.tcm_ifindex = ifindex,
412 .t.tcm_parent = handle,
414 int seq = time(NULL);
416 req.nlh.nlmsg_seq = seq;
417 if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
420 return bpf_netlink_recv(sock, nl_pid, seq, __dump_filter_nlmsg,
421 dump_filter_nlmsg, cookie);