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