xref: /openbmc/linux/tools/lib/bpf/netlink.c (revision a5a1c349)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
3 
4 #include <stdlib.h>
5 #include <memory.h>
6 #include <unistd.h>
7 #include <linux/bpf.h>
8 #include <linux/rtnetlink.h>
9 #include <sys/socket.h>
10 #include <errno.h>
11 #include <time.h>
12 
13 #include "bpf.h"
14 #include "libbpf.h"
15 #include "libbpf_internal.h"
16 #include "nlattr.h"
17 
18 /* make sure libbpf doesn't use kernel-only integer typedefs */
19 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
20 
21 #ifndef SOL_NETLINK
22 #define SOL_NETLINK 270
23 #endif
24 
25 typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, libbpf_dump_nlmsg_t,
26 			      void *cookie);
27 
28 struct xdp_id_md {
29 	int ifindex;
30 	__u32 flags;
31 	struct xdp_link_info info;
32 };
33 
34 int libbpf_netlink_open(__u32 *nl_pid)
35 {
36 	struct sockaddr_nl sa;
37 	socklen_t addrlen;
38 	int one = 1, ret;
39 	int sock;
40 
41 	memset(&sa, 0, sizeof(sa));
42 	sa.nl_family = AF_NETLINK;
43 
44 	sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
45 	if (sock < 0)
46 		return -errno;
47 
48 	if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
49 		       &one, sizeof(one)) < 0) {
50 		pr_warn("Netlink error reporting not supported\n");
51 	}
52 
53 	if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
54 		ret = -errno;
55 		goto cleanup;
56 	}
57 
58 	addrlen = sizeof(sa);
59 	if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
60 		ret = -errno;
61 		goto cleanup;
62 	}
63 
64 	if (addrlen != sizeof(sa)) {
65 		ret = -LIBBPF_ERRNO__INTERNAL;
66 		goto cleanup;
67 	}
68 
69 	*nl_pid = sa.nl_pid;
70 	return sock;
71 
72 cleanup:
73 	close(sock);
74 	return ret;
75 }
76 
77 static int bpf_netlink_recv(int sock, __u32 nl_pid, int seq,
78 			    __dump_nlmsg_t _fn, libbpf_dump_nlmsg_t fn,
79 			    void *cookie)
80 {
81 	bool multipart = true;
82 	struct nlmsgerr *err;
83 	struct nlmsghdr *nh;
84 	char buf[4096];
85 	int len, ret;
86 
87 	while (multipart) {
88 		multipart = false;
89 		len = recv(sock, buf, sizeof(buf), 0);
90 		if (len < 0) {
91 			ret = -errno;
92 			goto done;
93 		}
94 
95 		if (len == 0)
96 			break;
97 
98 		for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
99 		     nh = NLMSG_NEXT(nh, len)) {
100 			if (nh->nlmsg_pid != nl_pid) {
101 				ret = -LIBBPF_ERRNO__WRNGPID;
102 				goto done;
103 			}
104 			if (nh->nlmsg_seq != seq) {
105 				ret = -LIBBPF_ERRNO__INVSEQ;
106 				goto done;
107 			}
108 			if (nh->nlmsg_flags & NLM_F_MULTI)
109 				multipart = true;
110 			switch (nh->nlmsg_type) {
111 			case NLMSG_ERROR:
112 				err = (struct nlmsgerr *)NLMSG_DATA(nh);
113 				if (!err->error)
114 					continue;
115 				ret = err->error;
116 				libbpf_nla_dump_errormsg(nh);
117 				goto done;
118 			case NLMSG_DONE:
119 				return 0;
120 			default:
121 				break;
122 			}
123 			if (_fn) {
124 				ret = _fn(nh, fn, cookie);
125 				if (ret)
126 					return ret;
127 			}
128 		}
129 	}
130 	ret = 0;
131 done:
132 	return ret;
133 }
134 
135 static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd,
136 					 __u32 flags)
137 {
138 	int sock, seq = 0, ret;
139 	struct nlattr *nla, *nla_xdp;
140 	struct {
141 		struct nlmsghdr  nh;
142 		struct ifinfomsg ifinfo;
143 		char             attrbuf[64];
144 	} req;
145 	__u32 nl_pid;
146 
147 	sock = libbpf_netlink_open(&nl_pid);
148 	if (sock < 0)
149 		return sock;
150 
151 	memset(&req, 0, sizeof(req));
152 	req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
153 	req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
154 	req.nh.nlmsg_type = RTM_SETLINK;
155 	req.nh.nlmsg_pid = 0;
156 	req.nh.nlmsg_seq = ++seq;
157 	req.ifinfo.ifi_family = AF_UNSPEC;
158 	req.ifinfo.ifi_index = ifindex;
159 
160 	/* started nested attribute for XDP */
161 	nla = (struct nlattr *)(((char *)&req)
162 				+ NLMSG_ALIGN(req.nh.nlmsg_len));
163 	nla->nla_type = NLA_F_NESTED | IFLA_XDP;
164 	nla->nla_len = NLA_HDRLEN;
165 
166 	/* add XDP fd */
167 	nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
168 	nla_xdp->nla_type = IFLA_XDP_FD;
169 	nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
170 	memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
171 	nla->nla_len += nla_xdp->nla_len;
172 
173 	/* if user passed in any flags, add those too */
174 	if (flags) {
175 		nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
176 		nla_xdp->nla_type = IFLA_XDP_FLAGS;
177 		nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags);
178 		memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags));
179 		nla->nla_len += nla_xdp->nla_len;
180 	}
181 
182 	if (flags & XDP_FLAGS_REPLACE) {
183 		nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
184 		nla_xdp->nla_type = IFLA_XDP_EXPECTED_FD;
185 		nla_xdp->nla_len = NLA_HDRLEN + sizeof(old_fd);
186 		memcpy((char *)nla_xdp + NLA_HDRLEN, &old_fd, sizeof(old_fd));
187 		nla->nla_len += nla_xdp->nla_len;
188 	}
189 
190 	req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
191 
192 	if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
193 		ret = -errno;
194 		goto cleanup;
195 	}
196 	ret = bpf_netlink_recv(sock, nl_pid, seq, NULL, NULL, NULL);
197 
198 cleanup:
199 	close(sock);
200 	return ret;
201 }
202 
203 int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
204 			     const struct bpf_xdp_set_link_opts *opts)
205 {
206 	int old_fd = -1;
207 
208 	if (!OPTS_VALID(opts, bpf_xdp_set_link_opts))
209 		return -EINVAL;
210 
211 	if (OPTS_HAS(opts, old_fd)) {
212 		old_fd = OPTS_GET(opts, old_fd, -1);
213 		flags |= XDP_FLAGS_REPLACE;
214 	}
215 
216 	return __bpf_set_link_xdp_fd_replace(ifindex, fd,
217 					     old_fd,
218 					     flags);
219 }
220 
221 int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
222 {
223 	return __bpf_set_link_xdp_fd_replace(ifindex, fd, 0, flags);
224 }
225 
226 static int __dump_link_nlmsg(struct nlmsghdr *nlh,
227 			     libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie)
228 {
229 	struct nlattr *tb[IFLA_MAX + 1], *attr;
230 	struct ifinfomsg *ifi = NLMSG_DATA(nlh);
231 	int len;
232 
233 	len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
234 	attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
235 	if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0)
236 		return -LIBBPF_ERRNO__NLPARSE;
237 
238 	return dump_link_nlmsg(cookie, ifi, tb);
239 }
240 
241 static int get_xdp_info(void *cookie, void *msg, struct nlattr **tb)
242 {
243 	struct nlattr *xdp_tb[IFLA_XDP_MAX + 1];
244 	struct xdp_id_md *xdp_id = cookie;
245 	struct ifinfomsg *ifinfo = msg;
246 	int ret;
247 
248 	if (xdp_id->ifindex && xdp_id->ifindex != ifinfo->ifi_index)
249 		return 0;
250 
251 	if (!tb[IFLA_XDP])
252 		return 0;
253 
254 	ret = libbpf_nla_parse_nested(xdp_tb, IFLA_XDP_MAX, tb[IFLA_XDP], NULL);
255 	if (ret)
256 		return ret;
257 
258 	if (!xdp_tb[IFLA_XDP_ATTACHED])
259 		return 0;
260 
261 	xdp_id->info.attach_mode = libbpf_nla_getattr_u8(
262 		xdp_tb[IFLA_XDP_ATTACHED]);
263 
264 	if (xdp_id->info.attach_mode == XDP_ATTACHED_NONE)
265 		return 0;
266 
267 	if (xdp_tb[IFLA_XDP_PROG_ID])
268 		xdp_id->info.prog_id = libbpf_nla_getattr_u32(
269 			xdp_tb[IFLA_XDP_PROG_ID]);
270 
271 	if (xdp_tb[IFLA_XDP_SKB_PROG_ID])
272 		xdp_id->info.skb_prog_id = libbpf_nla_getattr_u32(
273 			xdp_tb[IFLA_XDP_SKB_PROG_ID]);
274 
275 	if (xdp_tb[IFLA_XDP_DRV_PROG_ID])
276 		xdp_id->info.drv_prog_id = libbpf_nla_getattr_u32(
277 			xdp_tb[IFLA_XDP_DRV_PROG_ID]);
278 
279 	if (xdp_tb[IFLA_XDP_HW_PROG_ID])
280 		xdp_id->info.hw_prog_id = libbpf_nla_getattr_u32(
281 			xdp_tb[IFLA_XDP_HW_PROG_ID]);
282 
283 	return 0;
284 }
285 
286 int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
287 			  size_t info_size, __u32 flags)
288 {
289 	struct xdp_id_md xdp_id = {};
290 	int sock, ret;
291 	__u32 nl_pid;
292 	__u32 mask;
293 
294 	if (flags & ~XDP_FLAGS_MASK || !info_size)
295 		return -EINVAL;
296 
297 	/* Check whether the single {HW,DRV,SKB} mode is set */
298 	flags &= (XDP_FLAGS_SKB_MODE | XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE);
299 	mask = flags - 1;
300 	if (flags && flags & mask)
301 		return -EINVAL;
302 
303 	sock = libbpf_netlink_open(&nl_pid);
304 	if (sock < 0)
305 		return sock;
306 
307 	xdp_id.ifindex = ifindex;
308 	xdp_id.flags = flags;
309 
310 	ret = libbpf_nl_get_link(sock, nl_pid, get_xdp_info, &xdp_id);
311 	if (!ret) {
312 		size_t sz = min(info_size, sizeof(xdp_id.info));
313 
314 		memcpy(info, &xdp_id.info, sz);
315 		memset((void *) info + sz, 0, info_size - sz);
316 	}
317 
318 	close(sock);
319 	return ret;
320 }
321 
322 static __u32 get_xdp_id(struct xdp_link_info *info, __u32 flags)
323 {
324 	if (info->attach_mode != XDP_ATTACHED_MULTI)
325 		return info->prog_id;
326 	if (flags & XDP_FLAGS_DRV_MODE)
327 		return info->drv_prog_id;
328 	if (flags & XDP_FLAGS_HW_MODE)
329 		return info->hw_prog_id;
330 	if (flags & XDP_FLAGS_SKB_MODE)
331 		return info->skb_prog_id;
332 
333 	return 0;
334 }
335 
336 int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags)
337 {
338 	struct xdp_link_info info;
339 	int ret;
340 
341 	ret = bpf_get_link_xdp_info(ifindex, &info, sizeof(info), flags);
342 	if (!ret)
343 		*prog_id = get_xdp_id(&info, flags);
344 
345 	return ret;
346 }
347 
348 int libbpf_nl_get_link(int sock, unsigned int nl_pid,
349 		       libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie)
350 {
351 	struct {
352 		struct nlmsghdr nlh;
353 		struct ifinfomsg ifm;
354 	} req = {
355 		.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
356 		.nlh.nlmsg_type = RTM_GETLINK,
357 		.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
358 		.ifm.ifi_family = AF_PACKET,
359 	};
360 	int seq = time(NULL);
361 
362 	req.nlh.nlmsg_seq = seq;
363 	if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
364 		return -errno;
365 
366 	return bpf_netlink_recv(sock, nl_pid, seq, __dump_link_nlmsg,
367 				dump_link_nlmsg, cookie);
368 }
369 
370 static int __dump_class_nlmsg(struct nlmsghdr *nlh,
371 			      libbpf_dump_nlmsg_t dump_class_nlmsg,
372 			      void *cookie)
373 {
374 	struct nlattr *tb[TCA_MAX + 1], *attr;
375 	struct tcmsg *t = NLMSG_DATA(nlh);
376 	int len;
377 
378 	len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
379 	attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
380 	if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
381 		return -LIBBPF_ERRNO__NLPARSE;
382 
383 	return dump_class_nlmsg(cookie, t, tb);
384 }
385 
386 int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
387 			libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie)
388 {
389 	struct {
390 		struct nlmsghdr nlh;
391 		struct tcmsg t;
392 	} req = {
393 		.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
394 		.nlh.nlmsg_type = RTM_GETTCLASS,
395 		.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
396 		.t.tcm_family = AF_UNSPEC,
397 		.t.tcm_ifindex = ifindex,
398 	};
399 	int seq = time(NULL);
400 
401 	req.nlh.nlmsg_seq = seq;
402 	if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
403 		return -errno;
404 
405 	return bpf_netlink_recv(sock, nl_pid, seq, __dump_class_nlmsg,
406 				dump_class_nlmsg, cookie);
407 }
408 
409 static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh,
410 			      libbpf_dump_nlmsg_t dump_qdisc_nlmsg,
411 			      void *cookie)
412 {
413 	struct nlattr *tb[TCA_MAX + 1], *attr;
414 	struct tcmsg *t = NLMSG_DATA(nlh);
415 	int len;
416 
417 	len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
418 	attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
419 	if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
420 		return -LIBBPF_ERRNO__NLPARSE;
421 
422 	return dump_qdisc_nlmsg(cookie, t, tb);
423 }
424 
425 int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
426 			libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie)
427 {
428 	struct {
429 		struct nlmsghdr nlh;
430 		struct tcmsg t;
431 	} req = {
432 		.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
433 		.nlh.nlmsg_type = RTM_GETQDISC,
434 		.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
435 		.t.tcm_family = AF_UNSPEC,
436 		.t.tcm_ifindex = ifindex,
437 	};
438 	int seq = time(NULL);
439 
440 	req.nlh.nlmsg_seq = seq;
441 	if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
442 		return -errno;
443 
444 	return bpf_netlink_recv(sock, nl_pid, seq, __dump_qdisc_nlmsg,
445 				dump_qdisc_nlmsg, cookie);
446 }
447 
448 static int __dump_filter_nlmsg(struct nlmsghdr *nlh,
449 			       libbpf_dump_nlmsg_t dump_filter_nlmsg,
450 			       void *cookie)
451 {
452 	struct nlattr *tb[TCA_MAX + 1], *attr;
453 	struct tcmsg *t = NLMSG_DATA(nlh);
454 	int len;
455 
456 	len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
457 	attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
458 	if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
459 		return -LIBBPF_ERRNO__NLPARSE;
460 
461 	return dump_filter_nlmsg(cookie, t, tb);
462 }
463 
464 int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
465 			 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie)
466 {
467 	struct {
468 		struct nlmsghdr nlh;
469 		struct tcmsg t;
470 	} req = {
471 		.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
472 		.nlh.nlmsg_type = RTM_GETTFILTER,
473 		.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
474 		.t.tcm_family = AF_UNSPEC,
475 		.t.tcm_ifindex = ifindex,
476 		.t.tcm_parent = handle,
477 	};
478 	int seq = time(NULL);
479 
480 	req.nlh.nlmsg_seq = seq;
481 	if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
482 		return -errno;
483 
484 	return bpf_netlink_recv(sock, nl_pid, seq, __dump_filter_nlmsg,
485 				dump_filter_nlmsg, cookie);
486 }
487