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