]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/libnetlink.c
libnetlink: Add filter function to rtnl_neighdump_req
[mirror_iproute2.git] / lib / libnetlink.c
1 /*
2 * libnetlink.c RTnetlink service routines.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdbool.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <net/if_arp.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <time.h>
24 #include <sys/uio.h>
25 #include <linux/fib_rules.h>
26 #include <linux/if_addrlabel.h>
27 #include <linux/if_bridge.h>
28
29 #include "libnetlink.h"
30
31 #define __aligned(x) __attribute__((aligned(x)))
32
33 #ifndef SOL_NETLINK
34 #define SOL_NETLINK 270
35 #endif
36
37 #ifndef MIN
38 #define MIN(a, b) ((a) < (b) ? (a) : (b))
39 #endif
40
41 int rcvbuf = 1024 * 1024;
42
43 #ifdef HAVE_LIBMNL
44 #include <libmnl/libmnl.h>
45
46 static const enum mnl_attr_data_type extack_policy[NLMSGERR_ATTR_MAX + 1] = {
47 [NLMSGERR_ATTR_MSG] = MNL_TYPE_NUL_STRING,
48 [NLMSGERR_ATTR_OFFS] = MNL_TYPE_U32,
49 };
50
51 static int err_attr_cb(const struct nlattr *attr, void *data)
52 {
53 const struct nlattr **tb = data;
54 uint16_t type;
55
56 if (mnl_attr_type_valid(attr, NLMSGERR_ATTR_MAX) < 0) {
57 fprintf(stderr, "Invalid extack attribute\n");
58 return MNL_CB_ERROR;
59 }
60
61 type = mnl_attr_get_type(attr);
62 if (mnl_attr_validate(attr, extack_policy[type]) < 0) {
63 fprintf(stderr, "extack attribute %d failed validation\n",
64 type);
65 return MNL_CB_ERROR;
66 }
67
68 tb[type] = attr;
69 return MNL_CB_OK;
70 }
71
72 static void print_ext_ack_msg(bool is_err, const char *msg)
73 {
74 fprintf(stderr, "%s: %s", is_err ? "Error" : "Warning", msg);
75 if (msg[strlen(msg) - 1] != '.')
76 fprintf(stderr, ".");
77 fprintf(stderr, "\n");
78 }
79
80 /* dump netlink extended ack error message */
81 int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn)
82 {
83 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {};
84 const struct nlmsgerr *err = mnl_nlmsg_get_payload(nlh);
85 const struct nlmsghdr *err_nlh = NULL;
86 unsigned int hlen = sizeof(*err);
87 const char *msg = NULL;
88 uint32_t off = 0;
89
90 /* no TLVs, nothing to do here */
91 if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
92 return 0;
93
94 /* if NLM_F_CAPPED is set then the inner err msg was capped */
95 if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
96 hlen += mnl_nlmsg_get_payload_len(&err->msg);
97
98 if (mnl_attr_parse(nlh, hlen, err_attr_cb, tb) != MNL_CB_OK)
99 return 0;
100
101 if (tb[NLMSGERR_ATTR_MSG])
102 msg = mnl_attr_get_str(tb[NLMSGERR_ATTR_MSG]);
103
104 if (tb[NLMSGERR_ATTR_OFFS]) {
105 off = mnl_attr_get_u32(tb[NLMSGERR_ATTR_OFFS]);
106
107 if (off > nlh->nlmsg_len) {
108 fprintf(stderr,
109 "Invalid offset for NLMSGERR_ATTR_OFFS\n");
110 off = 0;
111 } else if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
112 err_nlh = &err->msg;
113 }
114
115 if (errfn)
116 return errfn(msg, off, err_nlh);
117
118 if (msg && *msg != '\0') {
119 bool is_err = !!err->error;
120
121 print_ext_ack_msg(is_err, msg);
122 return is_err ? 1 : 0;
123 }
124
125 return 0;
126 }
127
128 static int nl_dump_ext_ack_done(const struct nlmsghdr *nlh, int error)
129 {
130 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {};
131 unsigned int hlen = sizeof(int);
132 const char *msg = NULL;
133
134 if (mnl_attr_parse(nlh, hlen, err_attr_cb, tb) != MNL_CB_OK)
135 return 0;
136
137 if (tb[NLMSGERR_ATTR_MSG])
138 msg = mnl_attr_get_str(tb[NLMSGERR_ATTR_MSG]);
139
140 if (msg && *msg != '\0') {
141 bool is_err = !!error;
142
143 print_ext_ack_msg(is_err, msg);
144 return is_err ? 1 : 0;
145 }
146
147 return 0;
148 }
149 #else
150 #warning "libmnl required for error support"
151
152 /* No extended error ack without libmnl */
153 int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn)
154 {
155 return 0;
156 }
157
158 static int nl_dump_ext_ack_done(const struct nlmsghdr *nlh, int error)
159 {
160 return 0;
161 }
162 #endif
163
164 /* Older kernels may not support strict dump and filtering */
165 void rtnl_set_strict_dump(struct rtnl_handle *rth)
166 {
167 int one = 1;
168
169 setsockopt(rth->fd, SOL_NETLINK, NETLINK_GET_STRICT_CHK,
170 &one, sizeof(one));
171 }
172
173 void rtnl_close(struct rtnl_handle *rth)
174 {
175 if (rth->fd >= 0) {
176 close(rth->fd);
177 rth->fd = -1;
178 }
179 }
180
181 int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions,
182 int protocol)
183 {
184 socklen_t addr_len;
185 int sndbuf = 32768;
186 int one = 1;
187
188 memset(rth, 0, sizeof(*rth));
189
190 rth->proto = protocol;
191 rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
192 if (rth->fd < 0) {
193 perror("Cannot open netlink socket");
194 return -1;
195 }
196
197 if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF,
198 &sndbuf, sizeof(sndbuf)) < 0) {
199 perror("SO_SNDBUF");
200 return -1;
201 }
202
203 if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF,
204 &rcvbuf, sizeof(rcvbuf)) < 0) {
205 perror("SO_RCVBUF");
206 return -1;
207 }
208
209 /* Older kernels may no support extended ACK reporting */
210 setsockopt(rth->fd, SOL_NETLINK, NETLINK_EXT_ACK,
211 &one, sizeof(one));
212
213 memset(&rth->local, 0, sizeof(rth->local));
214 rth->local.nl_family = AF_NETLINK;
215 rth->local.nl_groups = subscriptions;
216
217 if (bind(rth->fd, (struct sockaddr *)&rth->local,
218 sizeof(rth->local)) < 0) {
219 perror("Cannot bind netlink socket");
220 return -1;
221 }
222 addr_len = sizeof(rth->local);
223 if (getsockname(rth->fd, (struct sockaddr *)&rth->local,
224 &addr_len) < 0) {
225 perror("Cannot getsockname");
226 return -1;
227 }
228 if (addr_len != sizeof(rth->local)) {
229 fprintf(stderr, "Wrong address length %d\n", addr_len);
230 return -1;
231 }
232 if (rth->local.nl_family != AF_NETLINK) {
233 fprintf(stderr, "Wrong address family %d\n",
234 rth->local.nl_family);
235 return -1;
236 }
237 rth->seq = time(NULL);
238 return 0;
239 }
240
241 int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
242 {
243 return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
244 }
245
246 int rtnl_addrdump_req(struct rtnl_handle *rth, int family,
247 req_filter_fn_t filter_fn)
248 {
249 struct {
250 struct nlmsghdr nlh;
251 struct ifaddrmsg ifm;
252 char buf[128];
253 } req = {
254 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
255 .nlh.nlmsg_type = RTM_GETADDR,
256 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
257 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
258 .ifm.ifa_family = family,
259 };
260
261 if (filter_fn) {
262 int err;
263
264 err = filter_fn(&req.nlh, sizeof(req));
265 if (err)
266 return err;
267 }
268
269 return send(rth->fd, &req, sizeof(req), 0);
270 }
271
272 int rtnl_addrlbldump_req(struct rtnl_handle *rth, int family)
273 {
274 struct {
275 struct nlmsghdr nlh;
276 struct ifaddrlblmsg ifal;
277 } req = {
278 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg)),
279 .nlh.nlmsg_type = RTM_GETADDRLABEL,
280 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
281 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
282 .ifal.ifal_family = family,
283 };
284
285 return send(rth->fd, &req, sizeof(req), 0);
286 }
287
288 int rtnl_routedump_req(struct rtnl_handle *rth, int family,
289 req_filter_fn_t filter_fn)
290 {
291 struct {
292 struct nlmsghdr nlh;
293 struct rtmsg rtm;
294 char buf[128];
295 } req = {
296 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)),
297 .nlh.nlmsg_type = RTM_GETROUTE,
298 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
299 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
300 .rtm.rtm_family = family,
301 };
302
303 if (filter_fn) {
304 int err;
305
306 err = filter_fn(&req.nlh, sizeof(req));
307 if (err)
308 return err;
309 }
310
311 return send(rth->fd, &req, sizeof(req), 0);
312 }
313
314 int rtnl_ruledump_req(struct rtnl_handle *rth, int family)
315 {
316 struct {
317 struct nlmsghdr nlh;
318 struct fib_rule_hdr frh;
319 } req = {
320 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
321 .nlh.nlmsg_type = RTM_GETRULE,
322 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
323 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
324 .frh.family = family
325 };
326
327 return send(rth->fd, &req, sizeof(req), 0);
328 }
329
330 int rtnl_neighdump_req(struct rtnl_handle *rth, int family,
331 req_filter_fn_t filter_fn)
332 {
333 struct {
334 struct nlmsghdr nlh;
335 struct ndmsg ndm;
336 char buf[256];
337 } req = {
338 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)),
339 .nlh.nlmsg_type = RTM_GETNEIGH,
340 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
341 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
342 .ndm.ndm_family = family,
343 };
344
345 if (filter_fn) {
346 int err;
347
348 err = filter_fn(&req.nlh, sizeof(req));
349 if (err)
350 return err;
351 }
352
353 return send(rth->fd, &req, sizeof(req), 0);
354 }
355
356 int rtnl_neightbldump_req(struct rtnl_handle *rth, int family)
357 {
358 struct {
359 struct nlmsghdr nlh;
360 struct ndtmsg ndtmsg;
361 } req = {
362 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndtmsg)),
363 .nlh.nlmsg_type = RTM_GETNEIGHTBL,
364 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
365 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
366 .ndtmsg.ndtm_family = family,
367 };
368
369 return send(rth->fd, &req, sizeof(req), 0);
370 }
371
372 int rtnl_mdbdump_req(struct rtnl_handle *rth, int family)
373 {
374 struct {
375 struct nlmsghdr nlh;
376 struct br_port_msg bpm;
377 } req = {
378 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct br_port_msg)),
379 .nlh.nlmsg_type = RTM_GETMDB,
380 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
381 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
382 .bpm.family = family,
383 };
384
385 return send(rth->fd, &req, sizeof(req), 0);
386 }
387
388 int rtnl_netconfdump_req(struct rtnl_handle *rth, int family)
389 {
390 struct {
391 struct nlmsghdr nlh;
392 struct netconfmsg ncm;
393 char buf[0] __aligned(NLMSG_ALIGNTO);
394 } req = {
395 .nlh.nlmsg_len = NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct netconfmsg))),
396 .nlh.nlmsg_type = RTM_GETNETCONF,
397 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
398 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
399 .ncm.ncm_family = family,
400 };
401
402 return send(rth->fd, &req, sizeof(req), 0);
403 }
404
405 int rtnl_nsiddump_req(struct rtnl_handle *rth, int family)
406 {
407 struct {
408 struct nlmsghdr nlh;
409 struct rtgenmsg rtm;
410 char buf[0] __aligned(NLMSG_ALIGNTO);
411 } req = {
412 .nlh.nlmsg_len = NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct rtgenmsg))),
413 .nlh.nlmsg_type = RTM_GETNSID,
414 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
415 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
416 .rtm.rtgen_family = family,
417 };
418
419 return send(rth->fd, &req, sizeof(req), 0);
420 }
421
422 static int __rtnl_linkdump_req(struct rtnl_handle *rth, int family)
423 {
424 struct {
425 struct nlmsghdr nlh;
426 struct ifinfomsg ifm;
427 } req = {
428 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
429 .nlh.nlmsg_type = RTM_GETLINK,
430 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
431 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
432 .ifm.ifi_family = family,
433 };
434
435 return send(rth->fd, &req, sizeof(req), 0);
436 }
437
438 int rtnl_linkdump_req(struct rtnl_handle *rth, int family)
439 {
440 if (family == AF_UNSPEC)
441 return rtnl_linkdump_req_filter(rth, family, RTEXT_FILTER_VF);
442
443 return __rtnl_linkdump_req(rth, family);
444 }
445
446 int rtnl_linkdump_req_filter(struct rtnl_handle *rth, int family,
447 __u32 filt_mask)
448 {
449 if (family == AF_UNSPEC) {
450 struct {
451 struct nlmsghdr nlh;
452 struct ifinfomsg ifm;
453 /* attribute has to be NLMSG aligned */
454 struct rtattr ext_req __aligned(NLMSG_ALIGNTO);
455 __u32 ext_filter_mask;
456 } req = {
457 .nlh.nlmsg_len = sizeof(req),
458 .nlh.nlmsg_type = RTM_GETLINK,
459 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
460 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
461 .ifm.ifi_family = family,
462 .ext_req.rta_type = IFLA_EXT_MASK,
463 .ext_req.rta_len = RTA_LENGTH(sizeof(__u32)),
464 .ext_filter_mask = filt_mask,
465 };
466
467 return send(rth->fd, &req, sizeof(req), 0);
468 }
469
470 return __rtnl_linkdump_req(rth, family);
471 }
472
473 int rtnl_linkdump_req_filter_fn(struct rtnl_handle *rth, int family,
474 req_filter_fn_t filter_fn)
475 {
476 if (family == AF_UNSPEC) {
477 struct {
478 struct nlmsghdr nlh;
479 struct ifinfomsg ifm;
480 char buf[1024];
481 } req = {
482 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
483 .nlh.nlmsg_type = RTM_GETLINK,
484 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
485 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
486 .ifm.ifi_family = family,
487 };
488 int err;
489
490 if (!filter_fn)
491 return -EINVAL;
492
493 err = filter_fn(&req.nlh, sizeof(req));
494 if (err)
495 return err;
496
497 return send(rth->fd, &req, req.nlh.nlmsg_len, 0);
498 }
499
500 return __rtnl_linkdump_req(rth, family);
501 }
502
503 int rtnl_statsdump_req_filter(struct rtnl_handle *rth, int fam, __u32 filt_mask)
504 {
505 struct {
506 struct nlmsghdr nlh;
507 struct if_stats_msg ifsm;
508 } req;
509
510 memset(&req, 0, sizeof(req));
511 req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct if_stats_msg));
512 req.nlh.nlmsg_type = RTM_GETSTATS;
513 req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
514 req.nlh.nlmsg_pid = 0;
515 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
516 req.ifsm.family = fam;
517 req.ifsm.filter_mask = filt_mask;
518
519 return send(rth->fd, &req, sizeof(req), 0);
520 }
521
522 int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
523 {
524 return send(rth->fd, buf, len, 0);
525 }
526
527 int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
528 {
529 struct nlmsghdr *h;
530 int status;
531 char resp[1024];
532
533 status = send(rth->fd, buf, len, 0);
534 if (status < 0)
535 return status;
536
537 /* Check for immediate errors */
538 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
539 if (status < 0) {
540 if (errno == EAGAIN)
541 return 0;
542 return -1;
543 }
544
545 for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status);
546 h = NLMSG_NEXT(h, status)) {
547 if (h->nlmsg_type == NLMSG_ERROR) {
548 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
549
550 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
551 fprintf(stderr, "ERROR truncated\n");
552 else
553 errno = -err->error;
554 return -1;
555 }
556 }
557
558 return 0;
559 }
560
561 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
562 {
563 struct nlmsghdr nlh = {
564 .nlmsg_len = NLMSG_LENGTH(len),
565 .nlmsg_type = type,
566 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
567 .nlmsg_seq = rth->dump = ++rth->seq,
568 };
569 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
570 struct iovec iov[2] = {
571 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
572 { .iov_base = req, .iov_len = len }
573 };
574 struct msghdr msg = {
575 .msg_name = &nladdr,
576 .msg_namelen = sizeof(nladdr),
577 .msg_iov = iov,
578 .msg_iovlen = 2,
579 };
580
581 return sendmsg(rth->fd, &msg, 0);
582 }
583
584 int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n)
585 {
586 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
587 struct iovec iov = {
588 .iov_base = n,
589 .iov_len = n->nlmsg_len
590 };
591 struct msghdr msg = {
592 .msg_name = &nladdr,
593 .msg_namelen = sizeof(nladdr),
594 .msg_iov = &iov,
595 .msg_iovlen = 1,
596 };
597
598 n->nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
599 n->nlmsg_pid = 0;
600 n->nlmsg_seq = rth->dump = ++rth->seq;
601
602 return sendmsg(rth->fd, &msg, 0);
603 }
604
605 static int rtnl_dump_done(struct nlmsghdr *h)
606 {
607 int len = *(int *)NLMSG_DATA(h);
608
609 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(int))) {
610 fprintf(stderr, "DONE truncated\n");
611 return -1;
612 }
613
614 if (len < 0) {
615 /* check for any messages returned from kernel */
616 if (nl_dump_ext_ack_done(h, len))
617 return len;
618
619 errno = -len;
620 switch (errno) {
621 case ENOENT:
622 case EOPNOTSUPP:
623 return -1;
624 case EMSGSIZE:
625 fprintf(stderr,
626 "Error: Buffer too small for object.\n");
627 break;
628 default:
629 perror("RTNETLINK answers");
630 }
631 return len;
632 }
633
634 /* check for any messages returned from kernel */
635 nl_dump_ext_ack(h, NULL);
636
637 return 0;
638 }
639
640 static void rtnl_dump_error(const struct rtnl_handle *rth,
641 struct nlmsghdr *h)
642 {
643
644 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
645 fprintf(stderr, "ERROR truncated\n");
646 } else {
647 const struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
648
649 errno = -err->error;
650 if (rth->proto == NETLINK_SOCK_DIAG &&
651 (errno == ENOENT ||
652 errno == EOPNOTSUPP))
653 return;
654
655 if (!(rth->flags & RTNL_HANDLE_F_SUPPRESS_NLERR))
656 perror("RTNETLINK answers");
657 }
658 }
659
660 static int __rtnl_recvmsg(int fd, struct msghdr *msg, int flags)
661 {
662 int len;
663
664 do {
665 len = recvmsg(fd, msg, flags);
666 } while (len < 0 && (errno == EINTR || errno == EAGAIN));
667
668 if (len < 0) {
669 fprintf(stderr, "netlink receive error %s (%d)\n",
670 strerror(errno), errno);
671 return -errno;
672 }
673
674 if (len == 0) {
675 fprintf(stderr, "EOF on netlink\n");
676 return -ENODATA;
677 }
678
679 return len;
680 }
681
682 static int rtnl_recvmsg(int fd, struct msghdr *msg, char **answer)
683 {
684 struct iovec *iov = msg->msg_iov;
685 char *buf;
686 int len;
687
688 iov->iov_base = NULL;
689 iov->iov_len = 0;
690
691 len = __rtnl_recvmsg(fd, msg, MSG_PEEK | MSG_TRUNC);
692 if (len < 0)
693 return len;
694
695 buf = malloc(len);
696 if (!buf) {
697 fprintf(stderr, "malloc error: not enough buffer\n");
698 return -ENOMEM;
699 }
700
701 iov->iov_base = buf;
702 iov->iov_len = len;
703
704 len = __rtnl_recvmsg(fd, msg, 0);
705 if (len < 0) {
706 free(buf);
707 return len;
708 }
709
710 if (answer)
711 *answer = buf;
712 else
713 free(buf);
714
715 return len;
716 }
717
718 static int rtnl_dump_filter_l(struct rtnl_handle *rth,
719 const struct rtnl_dump_filter_arg *arg)
720 {
721 struct sockaddr_nl nladdr;
722 struct iovec iov;
723 struct msghdr msg = {
724 .msg_name = &nladdr,
725 .msg_namelen = sizeof(nladdr),
726 .msg_iov = &iov,
727 .msg_iovlen = 1,
728 };
729 char *buf;
730 int dump_intr = 0;
731
732 while (1) {
733 int status;
734 const struct rtnl_dump_filter_arg *a;
735 int found_done = 0;
736 int msglen = 0;
737
738 status = rtnl_recvmsg(rth->fd, &msg, &buf);
739 if (status < 0)
740 return status;
741
742 if (rth->dump_fp)
743 fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp);
744
745 for (a = arg; a->filter; a++) {
746 struct nlmsghdr *h = (struct nlmsghdr *)buf;
747
748 msglen = status;
749
750 while (NLMSG_OK(h, msglen)) {
751 int err = 0;
752
753 h->nlmsg_flags &= ~a->nc_flags;
754
755 if (nladdr.nl_pid != 0 ||
756 h->nlmsg_pid != rth->local.nl_pid ||
757 h->nlmsg_seq != rth->dump)
758 goto skip_it;
759
760 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
761 dump_intr = 1;
762
763 if (h->nlmsg_type == NLMSG_DONE) {
764 err = rtnl_dump_done(h);
765 if (err < 0) {
766 free(buf);
767 return -1;
768 }
769
770 found_done = 1;
771 break; /* process next filter */
772 }
773
774 if (h->nlmsg_type == NLMSG_ERROR) {
775 rtnl_dump_error(rth, h);
776 free(buf);
777 return -1;
778 }
779
780 if (!rth->dump_fp) {
781 err = a->filter(h, a->arg1);
782 if (err < 0) {
783 free(buf);
784 return err;
785 }
786 }
787
788 skip_it:
789 h = NLMSG_NEXT(h, msglen);
790 }
791 }
792 free(buf);
793
794 if (found_done) {
795 if (dump_intr)
796 fprintf(stderr,
797 "Dump was interrupted and may be inconsistent.\n");
798 return 0;
799 }
800
801 if (msg.msg_flags & MSG_TRUNC) {
802 fprintf(stderr, "Message truncated\n");
803 continue;
804 }
805 if (msglen) {
806 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
807 exit(1);
808 }
809 }
810 }
811
812 int rtnl_dump_filter_nc(struct rtnl_handle *rth,
813 rtnl_filter_t filter,
814 void *arg1, __u16 nc_flags)
815 {
816 const struct rtnl_dump_filter_arg a[2] = {
817 { .filter = filter, .arg1 = arg1, .nc_flags = nc_flags, },
818 { .filter = NULL, .arg1 = NULL, .nc_flags = 0, },
819 };
820
821 return rtnl_dump_filter_l(rth, a);
822 }
823
824 static void rtnl_talk_error(struct nlmsghdr *h, struct nlmsgerr *err,
825 nl_ext_ack_fn_t errfn)
826 {
827 if (nl_dump_ext_ack(h, errfn))
828 return;
829
830 fprintf(stderr, "RTNETLINK answers: %s\n",
831 strerror(-err->error));
832 }
833
834
835 static int __rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iov,
836 size_t iovlen, struct nlmsghdr **answer,
837 bool show_rtnl_err, nl_ext_ack_fn_t errfn)
838 {
839 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
840 struct iovec riov;
841 struct msghdr msg = {
842 .msg_name = &nladdr,
843 .msg_namelen = sizeof(nladdr),
844 .msg_iov = iov,
845 .msg_iovlen = iovlen,
846 };
847 unsigned int seq = 0;
848 struct nlmsghdr *h;
849 int i, status;
850 char *buf;
851
852 for (i = 0; i < iovlen; i++) {
853 h = iov[i].iov_base;
854 h->nlmsg_seq = seq = ++rtnl->seq;
855 if (answer == NULL)
856 h->nlmsg_flags |= NLM_F_ACK;
857 }
858
859 status = sendmsg(rtnl->fd, &msg, 0);
860 if (status < 0) {
861 perror("Cannot talk to rtnetlink");
862 return -1;
863 }
864
865 /* change msg to use the response iov */
866 msg.msg_iov = &riov;
867 msg.msg_iovlen = 1;
868 i = 0;
869 while (1) {
870 next:
871 status = rtnl_recvmsg(rtnl->fd, &msg, &buf);
872 ++i;
873
874 if (status < 0)
875 return status;
876
877 if (msg.msg_namelen != sizeof(nladdr)) {
878 fprintf(stderr,
879 "sender address length == %d\n",
880 msg.msg_namelen);
881 exit(1);
882 }
883 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
884 int len = h->nlmsg_len;
885 int l = len - sizeof(*h);
886
887 if (l < 0 || len > status) {
888 if (msg.msg_flags & MSG_TRUNC) {
889 fprintf(stderr, "Truncated message\n");
890 free(buf);
891 return -1;
892 }
893 fprintf(stderr,
894 "!!!malformed message: len=%d\n",
895 len);
896 exit(1);
897 }
898
899 if (nladdr.nl_pid != 0 ||
900 h->nlmsg_pid != rtnl->local.nl_pid ||
901 h->nlmsg_seq > seq || h->nlmsg_seq < seq - iovlen) {
902 /* Don't forget to skip that message. */
903 status -= NLMSG_ALIGN(len);
904 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
905 continue;
906 }
907
908 if (h->nlmsg_type == NLMSG_ERROR) {
909 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
910 int error = err->error;
911
912 if (l < sizeof(struct nlmsgerr)) {
913 fprintf(stderr, "ERROR truncated\n");
914 free(buf);
915 return -1;
916 }
917
918 if (!error) {
919 /* check messages from kernel */
920 nl_dump_ext_ack(h, errfn);
921 } else {
922 errno = -error;
923
924 if (rtnl->proto != NETLINK_SOCK_DIAG &&
925 show_rtnl_err)
926 rtnl_talk_error(h, err, errfn);
927 }
928
929 if (answer)
930 *answer = (struct nlmsghdr *)buf;
931 else
932 free(buf);
933
934 if (i < iovlen)
935 goto next;
936 return error ? -i : 0;
937 }
938
939 if (answer) {
940 *answer = (struct nlmsghdr *)buf;
941 return 0;
942 }
943
944 fprintf(stderr, "Unexpected reply!!!\n");
945
946 status -= NLMSG_ALIGN(len);
947 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
948 }
949 free(buf);
950
951 if (msg.msg_flags & MSG_TRUNC) {
952 fprintf(stderr, "Message truncated\n");
953 continue;
954 }
955
956 if (status) {
957 fprintf(stderr, "!!!Remnant of size %d\n", status);
958 exit(1);
959 }
960 }
961 }
962
963 static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
964 struct nlmsghdr **answer,
965 bool show_rtnl_err, nl_ext_ack_fn_t errfn)
966 {
967 struct iovec iov = {
968 .iov_base = n,
969 .iov_len = n->nlmsg_len
970 };
971
972 return __rtnl_talk_iov(rtnl, &iov, 1, answer, show_rtnl_err, errfn);
973 }
974
975 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
976 struct nlmsghdr **answer)
977 {
978 return __rtnl_talk(rtnl, n, answer, true, NULL);
979 }
980
981 int rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iovec, size_t iovlen,
982 struct nlmsghdr **answer)
983 {
984 return __rtnl_talk_iov(rtnl, iovec, iovlen, answer, true, NULL);
985 }
986
987 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n,
988 struct nlmsghdr **answer)
989 {
990 return __rtnl_talk(rtnl, n, answer, false, NULL);
991 }
992
993 int rtnl_listen_all_nsid(struct rtnl_handle *rth)
994 {
995 unsigned int on = 1;
996
997 if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &on,
998 sizeof(on)) < 0) {
999 perror("NETLINK_LISTEN_ALL_NSID");
1000 return -1;
1001 }
1002 rth->flags |= RTNL_HANDLE_F_LISTEN_ALL_NSID;
1003 return 0;
1004 }
1005
1006 int rtnl_listen(struct rtnl_handle *rtnl,
1007 rtnl_listen_filter_t handler,
1008 void *jarg)
1009 {
1010 int status;
1011 struct nlmsghdr *h;
1012 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
1013 struct iovec iov;
1014 struct msghdr msg = {
1015 .msg_name = &nladdr,
1016 .msg_namelen = sizeof(nladdr),
1017 .msg_iov = &iov,
1018 .msg_iovlen = 1,
1019 };
1020 char buf[16384];
1021 char cmsgbuf[BUFSIZ];
1022
1023 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
1024 msg.msg_control = &cmsgbuf;
1025 msg.msg_controllen = sizeof(cmsgbuf);
1026 }
1027
1028 iov.iov_base = buf;
1029 while (1) {
1030 struct rtnl_ctrl_data ctrl;
1031 struct cmsghdr *cmsg;
1032
1033 iov.iov_len = sizeof(buf);
1034 status = recvmsg(rtnl->fd, &msg, 0);
1035
1036 if (status < 0) {
1037 if (errno == EINTR || errno == EAGAIN)
1038 continue;
1039 fprintf(stderr, "netlink receive error %s (%d)\n",
1040 strerror(errno), errno);
1041 if (errno == ENOBUFS)
1042 continue;
1043 return -1;
1044 }
1045 if (status == 0) {
1046 fprintf(stderr, "EOF on netlink\n");
1047 return -1;
1048 }
1049 if (msg.msg_namelen != sizeof(nladdr)) {
1050 fprintf(stderr,
1051 "Sender address length == %d\n",
1052 msg.msg_namelen);
1053 exit(1);
1054 }
1055
1056 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
1057 memset(&ctrl, 0, sizeof(ctrl));
1058 ctrl.nsid = -1;
1059 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1060 cmsg = CMSG_NXTHDR(&msg, cmsg))
1061 if (cmsg->cmsg_level == SOL_NETLINK &&
1062 cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID &&
1063 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
1064 int *data = (int *)CMSG_DATA(cmsg);
1065
1066 ctrl.nsid = *data;
1067 }
1068 }
1069
1070 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
1071 int err;
1072 int len = h->nlmsg_len;
1073 int l = len - sizeof(*h);
1074
1075 if (l < 0 || len > status) {
1076 if (msg.msg_flags & MSG_TRUNC) {
1077 fprintf(stderr, "Truncated message\n");
1078 return -1;
1079 }
1080 fprintf(stderr,
1081 "!!!malformed message: len=%d\n",
1082 len);
1083 exit(1);
1084 }
1085
1086 err = handler(&ctrl, h, jarg);
1087 if (err < 0)
1088 return err;
1089
1090 status -= NLMSG_ALIGN(len);
1091 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
1092 }
1093 if (msg.msg_flags & MSG_TRUNC) {
1094 fprintf(stderr, "Message truncated\n");
1095 continue;
1096 }
1097 if (status) {
1098 fprintf(stderr, "!!!Remnant of size %d\n", status);
1099 exit(1);
1100 }
1101 }
1102 }
1103
1104 int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
1105 void *jarg)
1106 {
1107 int status;
1108 char buf[16384];
1109 struct nlmsghdr *h = (struct nlmsghdr *)buf;
1110
1111 while (1) {
1112 int err, len;
1113 int l;
1114
1115 status = fread(&buf, 1, sizeof(*h), rtnl);
1116
1117 if (status < 0) {
1118 if (errno == EINTR)
1119 continue;
1120 perror("rtnl_from_file: fread");
1121 return -1;
1122 }
1123 if (status == 0)
1124 return 0;
1125
1126 len = h->nlmsg_len;
1127 l = len - sizeof(*h);
1128
1129 if (l < 0 || len > sizeof(buf)) {
1130 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
1131 len, ftell(rtnl));
1132 return -1;
1133 }
1134
1135 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
1136
1137 if (status < 0) {
1138 perror("rtnl_from_file: fread");
1139 return -1;
1140 }
1141 if (status < l) {
1142 fprintf(stderr, "rtnl-from_file: truncated message\n");
1143 return -1;
1144 }
1145
1146 err = handler(NULL, h, jarg);
1147 if (err < 0)
1148 return err;
1149 }
1150 }
1151
1152 int addattr(struct nlmsghdr *n, int maxlen, int type)
1153 {
1154 return addattr_l(n, maxlen, type, NULL, 0);
1155 }
1156
1157 int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
1158 {
1159 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
1160 }
1161
1162 int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
1163 {
1164 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
1165 }
1166
1167 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
1168 {
1169 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
1170 }
1171
1172 int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
1173 {
1174 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
1175 }
1176
1177 int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
1178 {
1179 return addattr_l(n, maxlen, type, str, strlen(str)+1);
1180 }
1181
1182 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
1183 int alen)
1184 {
1185 int len = RTA_LENGTH(alen);
1186 struct rtattr *rta;
1187
1188 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
1189 fprintf(stderr,
1190 "addattr_l ERROR: message exceeded bound of %d\n",
1191 maxlen);
1192 return -1;
1193 }
1194 rta = NLMSG_TAIL(n);
1195 rta->rta_type = type;
1196 rta->rta_len = len;
1197 if (alen)
1198 memcpy(RTA_DATA(rta), data, alen);
1199 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
1200 return 0;
1201 }
1202
1203 int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
1204 {
1205 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
1206 fprintf(stderr,
1207 "addraw_l ERROR: message exceeded bound of %d\n",
1208 maxlen);
1209 return -1;
1210 }
1211
1212 memcpy(NLMSG_TAIL(n), data, len);
1213 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
1214 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
1215 return 0;
1216 }
1217
1218 struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
1219 {
1220 struct rtattr *nest = NLMSG_TAIL(n);
1221
1222 addattr_l(n, maxlen, type, NULL, 0);
1223 return nest;
1224 }
1225
1226 int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
1227 {
1228 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
1229 return n->nlmsg_len;
1230 }
1231
1232 struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
1233 const void *data, int len)
1234 {
1235 struct rtattr *start = NLMSG_TAIL(n);
1236
1237 addattr_l(n, maxlen, type, data, len);
1238 addattr_nest(n, maxlen, type);
1239 return start;
1240 }
1241
1242 int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
1243 {
1244 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
1245
1246 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
1247 addattr_nest_end(n, nest);
1248 return n->nlmsg_len;
1249 }
1250
1251 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
1252 {
1253 int len = RTA_LENGTH(4);
1254 struct rtattr *subrta;
1255
1256 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
1257 fprintf(stderr,
1258 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1259 maxlen);
1260 return -1;
1261 }
1262 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
1263 subrta->rta_type = type;
1264 subrta->rta_len = len;
1265 memcpy(RTA_DATA(subrta), &data, 4);
1266 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
1267 return 0;
1268 }
1269
1270 int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
1271 const void *data, int alen)
1272 {
1273 struct rtattr *subrta;
1274 int len = RTA_LENGTH(alen);
1275
1276 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
1277 fprintf(stderr,
1278 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1279 maxlen);
1280 return -1;
1281 }
1282 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
1283 subrta->rta_type = type;
1284 subrta->rta_len = len;
1285 if (alen)
1286 memcpy(RTA_DATA(subrta), data, alen);
1287 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
1288 return 0;
1289 }
1290
1291 int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data)
1292 {
1293 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u8));
1294 }
1295
1296 int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data)
1297 {
1298 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u16));
1299 }
1300
1301 int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data)
1302 {
1303 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u64));
1304 }
1305
1306 struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type)
1307 {
1308 struct rtattr *nest = RTA_TAIL(rta);
1309
1310 rta_addattr_l(rta, maxlen, type, NULL, 0);
1311
1312 return nest;
1313 }
1314
1315 int rta_nest_end(struct rtattr *rta, struct rtattr *nest)
1316 {
1317 nest->rta_len = (void *)RTA_TAIL(rta) - (void *)nest;
1318
1319 return rta->rta_len;
1320 }
1321
1322 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
1323 {
1324 return parse_rtattr_flags(tb, max, rta, len, 0);
1325 }
1326
1327 int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
1328 int len, unsigned short flags)
1329 {
1330 unsigned short type;
1331
1332 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
1333 while (RTA_OK(rta, len)) {
1334 type = rta->rta_type & ~flags;
1335 if ((type <= max) && (!tb[type]))
1336 tb[type] = rta;
1337 rta = RTA_NEXT(rta, len);
1338 }
1339 if (len)
1340 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1341 len, rta->rta_len);
1342 return 0;
1343 }
1344
1345 struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len)
1346 {
1347 while (RTA_OK(rta, len)) {
1348 if (rta->rta_type == type)
1349 return rta;
1350 rta = RTA_NEXT(rta, len);
1351 }
1352
1353 if (len)
1354 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1355 len, rta->rta_len);
1356 return NULL;
1357 }
1358
1359 int __parse_rtattr_nested_compat(struct rtattr *tb[], int max,
1360 struct rtattr *rta,
1361 int len)
1362 {
1363 if (RTA_PAYLOAD(rta) < len)
1364 return -1;
1365 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
1366 rta = RTA_DATA(rta) + RTA_ALIGN(len);
1367 return parse_rtattr_nested(tb, max, rta);
1368 }
1369 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
1370 return 0;
1371 }