]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/libnetlink.c
devlink: Add support for 'fw_load_policy' generic parameter
[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_nsiddump_req(struct rtnl_handle *rth, int family)
334 {
335 struct {
336 struct nlmsghdr nlh;
337 struct rtgenmsg rtm;
338 } req = {
339 .nlh.nlmsg_len = sizeof(req),
340 .nlh.nlmsg_type = RTM_GETNSID,
341 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
342 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
343 .rtm.rtgen_family = family,
344 };
345
346 return send(rth->fd, &req, sizeof(req), 0);
347 }
348
349 int rtnl_linkdump_req(struct rtnl_handle *rth, int family)
350 {
351 return rtnl_linkdump_req_filter(rth, family, RTEXT_FILTER_VF);
352 }
353
354 int rtnl_linkdump_req_filter(struct rtnl_handle *rth, int family,
355 __u32 filt_mask)
356 {
357 struct {
358 struct nlmsghdr nlh;
359 struct ifinfomsg ifm;
360 /* attribute has to be NLMSG aligned */
361 struct rtattr ext_req __attribute__ ((aligned(NLMSG_ALIGNTO)));
362 __u32 ext_filter_mask;
363 } req = {
364 .nlh.nlmsg_len = sizeof(req),
365 .nlh.nlmsg_type = RTM_GETLINK,
366 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
367 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
368 .ifm.ifi_family = family,
369 .ext_req.rta_type = IFLA_EXT_MASK,
370 .ext_req.rta_len = RTA_LENGTH(sizeof(__u32)),
371 .ext_filter_mask = filt_mask,
372 };
373
374 return send(rth->fd, &req, sizeof(req), 0);
375 }
376
377 int rtnl_linkdump_req_filter_fn(struct rtnl_handle *rth, int family,
378 req_filter_fn_t filter_fn)
379 {
380 struct {
381 struct nlmsghdr nlh;
382 struct ifinfomsg ifm;
383 char buf[1024];
384 } req = {
385 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
386 .nlh.nlmsg_type = RTM_GETLINK,
387 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
388 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
389 .ifm.ifi_family = family,
390 };
391 int err;
392
393 if (!filter_fn)
394 return -EINVAL;
395
396 err = filter_fn(&req.nlh, sizeof(req));
397 if (err)
398 return err;
399
400 return send(rth->fd, &req, req.nlh.nlmsg_len, 0);
401 }
402
403 int rtnl_statsdump_req_filter(struct rtnl_handle *rth, int fam, __u32 filt_mask)
404 {
405 struct {
406 struct nlmsghdr nlh;
407 struct if_stats_msg ifsm;
408 } req;
409
410 memset(&req, 0, sizeof(req));
411 req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct if_stats_msg));
412 req.nlh.nlmsg_type = RTM_GETSTATS;
413 req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
414 req.nlh.nlmsg_pid = 0;
415 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
416 req.ifsm.family = fam;
417 req.ifsm.filter_mask = filt_mask;
418
419 return send(rth->fd, &req, sizeof(req), 0);
420 }
421
422 int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
423 {
424 return send(rth->fd, buf, len, 0);
425 }
426
427 int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
428 {
429 struct nlmsghdr *h;
430 int status;
431 char resp[1024];
432
433 status = send(rth->fd, buf, len, 0);
434 if (status < 0)
435 return status;
436
437 /* Check for immediate errors */
438 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
439 if (status < 0) {
440 if (errno == EAGAIN)
441 return 0;
442 return -1;
443 }
444
445 for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status);
446 h = NLMSG_NEXT(h, status)) {
447 if (h->nlmsg_type == NLMSG_ERROR) {
448 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
449
450 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
451 fprintf(stderr, "ERROR truncated\n");
452 else
453 errno = -err->error;
454 return -1;
455 }
456 }
457
458 return 0;
459 }
460
461 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
462 {
463 struct nlmsghdr nlh = {
464 .nlmsg_len = NLMSG_LENGTH(len),
465 .nlmsg_type = type,
466 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
467 .nlmsg_seq = rth->dump = ++rth->seq,
468 };
469 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
470 struct iovec iov[2] = {
471 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
472 { .iov_base = req, .iov_len = len }
473 };
474 struct msghdr msg = {
475 .msg_name = &nladdr,
476 .msg_namelen = sizeof(nladdr),
477 .msg_iov = iov,
478 .msg_iovlen = 2,
479 };
480
481 return sendmsg(rth->fd, &msg, 0);
482 }
483
484 int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n)
485 {
486 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
487 struct iovec iov = {
488 .iov_base = n,
489 .iov_len = n->nlmsg_len
490 };
491 struct msghdr msg = {
492 .msg_name = &nladdr,
493 .msg_namelen = sizeof(nladdr),
494 .msg_iov = &iov,
495 .msg_iovlen = 1,
496 };
497
498 n->nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
499 n->nlmsg_pid = 0;
500 n->nlmsg_seq = rth->dump = ++rth->seq;
501
502 return sendmsg(rth->fd, &msg, 0);
503 }
504
505 static int rtnl_dump_done(struct nlmsghdr *h)
506 {
507 int len = *(int *)NLMSG_DATA(h);
508
509 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(int))) {
510 fprintf(stderr, "DONE truncated\n");
511 return -1;
512 }
513
514 if (len < 0) {
515 errno = -len;
516 switch (errno) {
517 case ENOENT:
518 case EOPNOTSUPP:
519 return -1;
520 case EMSGSIZE:
521 fprintf(stderr,
522 "Error: Buffer too small for object.\n");
523 break;
524 default:
525 perror("RTNETLINK answers");
526 }
527 return len;
528 }
529
530 /* check for any messages returned from kernel */
531 nl_dump_ext_ack(h, NULL);
532
533 return 0;
534 }
535
536 static void rtnl_dump_error(const struct rtnl_handle *rth,
537 struct nlmsghdr *h)
538 {
539
540 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
541 fprintf(stderr, "ERROR truncated\n");
542 } else {
543 const struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
544
545 errno = -err->error;
546 if (rth->proto == NETLINK_SOCK_DIAG &&
547 (errno == ENOENT ||
548 errno == EOPNOTSUPP))
549 return;
550
551 if (!(rth->flags & RTNL_HANDLE_F_SUPPRESS_NLERR))
552 perror("RTNETLINK answers");
553 }
554 }
555
556 static int __rtnl_recvmsg(int fd, struct msghdr *msg, int flags)
557 {
558 int len;
559
560 do {
561 len = recvmsg(fd, msg, flags);
562 } while (len < 0 && (errno == EINTR || errno == EAGAIN));
563
564 if (len < 0) {
565 fprintf(stderr, "netlink receive error %s (%d)\n",
566 strerror(errno), errno);
567 return -errno;
568 }
569
570 if (len == 0) {
571 fprintf(stderr, "EOF on netlink\n");
572 return -ENODATA;
573 }
574
575 return len;
576 }
577
578 static int rtnl_recvmsg(int fd, struct msghdr *msg, char **answer)
579 {
580 struct iovec *iov = msg->msg_iov;
581 char *buf;
582 int len;
583
584 iov->iov_base = NULL;
585 iov->iov_len = 0;
586
587 len = __rtnl_recvmsg(fd, msg, MSG_PEEK | MSG_TRUNC);
588 if (len < 0)
589 return len;
590
591 buf = malloc(len);
592 if (!buf) {
593 fprintf(stderr, "malloc error: not enough buffer\n");
594 return -ENOMEM;
595 }
596
597 iov->iov_base = buf;
598 iov->iov_len = len;
599
600 len = __rtnl_recvmsg(fd, msg, 0);
601 if (len < 0) {
602 free(buf);
603 return len;
604 }
605
606 if (answer)
607 *answer = buf;
608 else
609 free(buf);
610
611 return len;
612 }
613
614 static int rtnl_dump_filter_l(struct rtnl_handle *rth,
615 const struct rtnl_dump_filter_arg *arg)
616 {
617 struct sockaddr_nl nladdr;
618 struct iovec iov;
619 struct msghdr msg = {
620 .msg_name = &nladdr,
621 .msg_namelen = sizeof(nladdr),
622 .msg_iov = &iov,
623 .msg_iovlen = 1,
624 };
625 char *buf;
626 int dump_intr = 0;
627
628 while (1) {
629 int status;
630 const struct rtnl_dump_filter_arg *a;
631 int found_done = 0;
632 int msglen = 0;
633
634 status = rtnl_recvmsg(rth->fd, &msg, &buf);
635 if (status < 0)
636 return status;
637
638 if (rth->dump_fp)
639 fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp);
640
641 for (a = arg; a->filter; a++) {
642 struct nlmsghdr *h = (struct nlmsghdr *)buf;
643
644 msglen = status;
645
646 while (NLMSG_OK(h, msglen)) {
647 int err = 0;
648
649 h->nlmsg_flags &= ~a->nc_flags;
650
651 if (nladdr.nl_pid != 0 ||
652 h->nlmsg_pid != rth->local.nl_pid ||
653 h->nlmsg_seq != rth->dump)
654 goto skip_it;
655
656 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
657 dump_intr = 1;
658
659 if (h->nlmsg_type == NLMSG_DONE) {
660 err = rtnl_dump_done(h);
661 if (err < 0) {
662 free(buf);
663 return -1;
664 }
665
666 found_done = 1;
667 break; /* process next filter */
668 }
669
670 if (h->nlmsg_type == NLMSG_ERROR) {
671 rtnl_dump_error(rth, h);
672 free(buf);
673 return -1;
674 }
675
676 if (!rth->dump_fp) {
677 err = a->filter(h, a->arg1);
678 if (err < 0) {
679 free(buf);
680 return err;
681 }
682 }
683
684 skip_it:
685 h = NLMSG_NEXT(h, msglen);
686 }
687 }
688 free(buf);
689
690 if (found_done) {
691 if (dump_intr)
692 fprintf(stderr,
693 "Dump was interrupted and may be inconsistent.\n");
694 return 0;
695 }
696
697 if (msg.msg_flags & MSG_TRUNC) {
698 fprintf(stderr, "Message truncated\n");
699 continue;
700 }
701 if (msglen) {
702 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
703 exit(1);
704 }
705 }
706 }
707
708 int rtnl_dump_filter_nc(struct rtnl_handle *rth,
709 rtnl_filter_t filter,
710 void *arg1, __u16 nc_flags)
711 {
712 const struct rtnl_dump_filter_arg a[2] = {
713 { .filter = filter, .arg1 = arg1, .nc_flags = nc_flags, },
714 { .filter = NULL, .arg1 = NULL, .nc_flags = 0, },
715 };
716
717 return rtnl_dump_filter_l(rth, a);
718 }
719
720 static void rtnl_talk_error(struct nlmsghdr *h, struct nlmsgerr *err,
721 nl_ext_ack_fn_t errfn)
722 {
723 if (nl_dump_ext_ack(h, errfn))
724 return;
725
726 fprintf(stderr, "RTNETLINK answers: %s\n",
727 strerror(-err->error));
728 }
729
730
731 static int __rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iov,
732 size_t iovlen, struct nlmsghdr **answer,
733 bool show_rtnl_err, nl_ext_ack_fn_t errfn)
734 {
735 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
736 struct iovec riov;
737 struct msghdr msg = {
738 .msg_name = &nladdr,
739 .msg_namelen = sizeof(nladdr),
740 .msg_iov = iov,
741 .msg_iovlen = iovlen,
742 };
743 unsigned int seq = 0;
744 struct nlmsghdr *h;
745 int i, status;
746 char *buf;
747
748 for (i = 0; i < iovlen; i++) {
749 h = iov[i].iov_base;
750 h->nlmsg_seq = seq = ++rtnl->seq;
751 if (answer == NULL)
752 h->nlmsg_flags |= NLM_F_ACK;
753 }
754
755 status = sendmsg(rtnl->fd, &msg, 0);
756 if (status < 0) {
757 perror("Cannot talk to rtnetlink");
758 return -1;
759 }
760
761 /* change msg to use the response iov */
762 msg.msg_iov = &riov;
763 msg.msg_iovlen = 1;
764 i = 0;
765 while (1) {
766 status = rtnl_recvmsg(rtnl->fd, &msg, &buf);
767 ++i;
768
769 if (status < 0)
770 return status;
771
772 if (msg.msg_namelen != sizeof(nladdr)) {
773 fprintf(stderr,
774 "sender address length == %d\n",
775 msg.msg_namelen);
776 exit(1);
777 }
778 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
779 int len = h->nlmsg_len;
780 int l = len - sizeof(*h);
781
782 if (l < 0 || len > status) {
783 if (msg.msg_flags & MSG_TRUNC) {
784 fprintf(stderr, "Truncated message\n");
785 free(buf);
786 return -1;
787 }
788 fprintf(stderr,
789 "!!!malformed message: len=%d\n",
790 len);
791 exit(1);
792 }
793
794 if (nladdr.nl_pid != 0 ||
795 h->nlmsg_pid != rtnl->local.nl_pid ||
796 h->nlmsg_seq > seq || h->nlmsg_seq < seq - iovlen) {
797 /* Don't forget to skip that message. */
798 status -= NLMSG_ALIGN(len);
799 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
800 continue;
801 }
802
803 if (h->nlmsg_type == NLMSG_ERROR) {
804 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
805 int error = err->error;
806
807 if (l < sizeof(struct nlmsgerr)) {
808 fprintf(stderr, "ERROR truncated\n");
809 free(buf);
810 return -1;
811 }
812
813 if (!error) {
814 /* check messages from kernel */
815 nl_dump_ext_ack(h, errfn);
816 } else {
817 errno = -error;
818
819 if (rtnl->proto != NETLINK_SOCK_DIAG &&
820 show_rtnl_err)
821 rtnl_talk_error(h, err, errfn);
822 }
823
824 if (answer)
825 *answer = (struct nlmsghdr *)buf;
826 else
827 free(buf);
828
829 return error ? -i : 0;
830 }
831
832 if (answer) {
833 *answer = (struct nlmsghdr *)buf;
834 return 0;
835 }
836
837 fprintf(stderr, "Unexpected reply!!!\n");
838
839 status -= NLMSG_ALIGN(len);
840 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
841 }
842 free(buf);
843
844 if (msg.msg_flags & MSG_TRUNC) {
845 fprintf(stderr, "Message truncated\n");
846 continue;
847 }
848
849 if (status) {
850 fprintf(stderr, "!!!Remnant of size %d\n", status);
851 exit(1);
852 }
853 }
854 }
855
856 static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
857 struct nlmsghdr **answer,
858 bool show_rtnl_err, nl_ext_ack_fn_t errfn)
859 {
860 struct iovec iov = {
861 .iov_base = n,
862 .iov_len = n->nlmsg_len
863 };
864
865 return __rtnl_talk_iov(rtnl, &iov, 1, answer, show_rtnl_err, errfn);
866 }
867
868 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
869 struct nlmsghdr **answer)
870 {
871 return __rtnl_talk(rtnl, n, answer, true, NULL);
872 }
873
874 int rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iovec, size_t iovlen,
875 struct nlmsghdr **answer)
876 {
877 return __rtnl_talk_iov(rtnl, iovec, iovlen, answer, true, NULL);
878 }
879
880 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n,
881 struct nlmsghdr **answer)
882 {
883 return __rtnl_talk(rtnl, n, answer, false, NULL);
884 }
885
886 int rtnl_listen_all_nsid(struct rtnl_handle *rth)
887 {
888 unsigned int on = 1;
889
890 if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &on,
891 sizeof(on)) < 0) {
892 perror("NETLINK_LISTEN_ALL_NSID");
893 return -1;
894 }
895 rth->flags |= RTNL_HANDLE_F_LISTEN_ALL_NSID;
896 return 0;
897 }
898
899 int rtnl_listen(struct rtnl_handle *rtnl,
900 rtnl_listen_filter_t handler,
901 void *jarg)
902 {
903 int status;
904 struct nlmsghdr *h;
905 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
906 struct iovec iov;
907 struct msghdr msg = {
908 .msg_name = &nladdr,
909 .msg_namelen = sizeof(nladdr),
910 .msg_iov = &iov,
911 .msg_iovlen = 1,
912 };
913 char buf[16384];
914 char cmsgbuf[BUFSIZ];
915
916 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
917 msg.msg_control = &cmsgbuf;
918 msg.msg_controllen = sizeof(cmsgbuf);
919 }
920
921 iov.iov_base = buf;
922 while (1) {
923 struct rtnl_ctrl_data ctrl;
924 struct cmsghdr *cmsg;
925
926 iov.iov_len = sizeof(buf);
927 status = recvmsg(rtnl->fd, &msg, 0);
928
929 if (status < 0) {
930 if (errno == EINTR || errno == EAGAIN)
931 continue;
932 fprintf(stderr, "netlink receive error %s (%d)\n",
933 strerror(errno), errno);
934 if (errno == ENOBUFS)
935 continue;
936 return -1;
937 }
938 if (status == 0) {
939 fprintf(stderr, "EOF on netlink\n");
940 return -1;
941 }
942 if (msg.msg_namelen != sizeof(nladdr)) {
943 fprintf(stderr,
944 "Sender address length == %d\n",
945 msg.msg_namelen);
946 exit(1);
947 }
948
949 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
950 memset(&ctrl, 0, sizeof(ctrl));
951 ctrl.nsid = -1;
952 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
953 cmsg = CMSG_NXTHDR(&msg, cmsg))
954 if (cmsg->cmsg_level == SOL_NETLINK &&
955 cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID &&
956 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
957 int *data = (int *)CMSG_DATA(cmsg);
958
959 ctrl.nsid = *data;
960 }
961 }
962
963 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
964 int err;
965 int len = h->nlmsg_len;
966 int l = len - sizeof(*h);
967
968 if (l < 0 || len > status) {
969 if (msg.msg_flags & MSG_TRUNC) {
970 fprintf(stderr, "Truncated message\n");
971 return -1;
972 }
973 fprintf(stderr,
974 "!!!malformed message: len=%d\n",
975 len);
976 exit(1);
977 }
978
979 err = handler(&ctrl, h, jarg);
980 if (err < 0)
981 return err;
982
983 status -= NLMSG_ALIGN(len);
984 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
985 }
986 if (msg.msg_flags & MSG_TRUNC) {
987 fprintf(stderr, "Message truncated\n");
988 continue;
989 }
990 if (status) {
991 fprintf(stderr, "!!!Remnant of size %d\n", status);
992 exit(1);
993 }
994 }
995 }
996
997 int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
998 void *jarg)
999 {
1000 int status;
1001 char buf[16384];
1002 struct nlmsghdr *h = (struct nlmsghdr *)buf;
1003
1004 while (1) {
1005 int err, len;
1006 int l;
1007
1008 status = fread(&buf, 1, sizeof(*h), rtnl);
1009
1010 if (status < 0) {
1011 if (errno == EINTR)
1012 continue;
1013 perror("rtnl_from_file: fread");
1014 return -1;
1015 }
1016 if (status == 0)
1017 return 0;
1018
1019 len = h->nlmsg_len;
1020 l = len - sizeof(*h);
1021
1022 if (l < 0 || len > sizeof(buf)) {
1023 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
1024 len, ftell(rtnl));
1025 return -1;
1026 }
1027
1028 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
1029
1030 if (status < 0) {
1031 perror("rtnl_from_file: fread");
1032 return -1;
1033 }
1034 if (status < l) {
1035 fprintf(stderr, "rtnl-from_file: truncated message\n");
1036 return -1;
1037 }
1038
1039 err = handler(NULL, h, jarg);
1040 if (err < 0)
1041 return err;
1042 }
1043 }
1044
1045 int addattr(struct nlmsghdr *n, int maxlen, int type)
1046 {
1047 return addattr_l(n, maxlen, type, NULL, 0);
1048 }
1049
1050 int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
1051 {
1052 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
1053 }
1054
1055 int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
1056 {
1057 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
1058 }
1059
1060 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
1061 {
1062 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
1063 }
1064
1065 int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
1066 {
1067 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
1068 }
1069
1070 int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
1071 {
1072 return addattr_l(n, maxlen, type, str, strlen(str)+1);
1073 }
1074
1075 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
1076 int alen)
1077 {
1078 int len = RTA_LENGTH(alen);
1079 struct rtattr *rta;
1080
1081 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
1082 fprintf(stderr,
1083 "addattr_l ERROR: message exceeded bound of %d\n",
1084 maxlen);
1085 return -1;
1086 }
1087 rta = NLMSG_TAIL(n);
1088 rta->rta_type = type;
1089 rta->rta_len = len;
1090 if (alen)
1091 memcpy(RTA_DATA(rta), data, alen);
1092 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
1093 return 0;
1094 }
1095
1096 int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
1097 {
1098 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
1099 fprintf(stderr,
1100 "addraw_l ERROR: message exceeded bound of %d\n",
1101 maxlen);
1102 return -1;
1103 }
1104
1105 memcpy(NLMSG_TAIL(n), data, len);
1106 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
1107 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
1108 return 0;
1109 }
1110
1111 struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
1112 {
1113 struct rtattr *nest = NLMSG_TAIL(n);
1114
1115 addattr_l(n, maxlen, type, NULL, 0);
1116 return nest;
1117 }
1118
1119 int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
1120 {
1121 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
1122 return n->nlmsg_len;
1123 }
1124
1125 struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
1126 const void *data, int len)
1127 {
1128 struct rtattr *start = NLMSG_TAIL(n);
1129
1130 addattr_l(n, maxlen, type, data, len);
1131 addattr_nest(n, maxlen, type);
1132 return start;
1133 }
1134
1135 int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
1136 {
1137 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
1138
1139 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
1140 addattr_nest_end(n, nest);
1141 return n->nlmsg_len;
1142 }
1143
1144 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
1145 {
1146 int len = RTA_LENGTH(4);
1147 struct rtattr *subrta;
1148
1149 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
1150 fprintf(stderr,
1151 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1152 maxlen);
1153 return -1;
1154 }
1155 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
1156 subrta->rta_type = type;
1157 subrta->rta_len = len;
1158 memcpy(RTA_DATA(subrta), &data, 4);
1159 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
1160 return 0;
1161 }
1162
1163 int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
1164 const void *data, int alen)
1165 {
1166 struct rtattr *subrta;
1167 int len = RTA_LENGTH(alen);
1168
1169 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
1170 fprintf(stderr,
1171 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1172 maxlen);
1173 return -1;
1174 }
1175 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
1176 subrta->rta_type = type;
1177 subrta->rta_len = len;
1178 if (alen)
1179 memcpy(RTA_DATA(subrta), data, alen);
1180 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
1181 return 0;
1182 }
1183
1184 int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data)
1185 {
1186 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u8));
1187 }
1188
1189 int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data)
1190 {
1191 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u16));
1192 }
1193
1194 int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data)
1195 {
1196 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u64));
1197 }
1198
1199 struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type)
1200 {
1201 struct rtattr *nest = RTA_TAIL(rta);
1202
1203 rta_addattr_l(rta, maxlen, type, NULL, 0);
1204
1205 return nest;
1206 }
1207
1208 int rta_nest_end(struct rtattr *rta, struct rtattr *nest)
1209 {
1210 nest->rta_len = (void *)RTA_TAIL(rta) - (void *)nest;
1211
1212 return rta->rta_len;
1213 }
1214
1215 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
1216 {
1217 return parse_rtattr_flags(tb, max, rta, len, 0);
1218 }
1219
1220 int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
1221 int len, unsigned short flags)
1222 {
1223 unsigned short type;
1224
1225 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
1226 while (RTA_OK(rta, len)) {
1227 type = rta->rta_type & ~flags;
1228 if ((type <= max) && (!tb[type]))
1229 tb[type] = rta;
1230 rta = RTA_NEXT(rta, len);
1231 }
1232 if (len)
1233 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1234 len, rta->rta_len);
1235 return 0;
1236 }
1237
1238 struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len)
1239 {
1240 while (RTA_OK(rta, len)) {
1241 if (rta->rta_type == type)
1242 return rta;
1243 rta = RTA_NEXT(rta, len);
1244 }
1245
1246 if (len)
1247 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1248 len, rta->rta_len);
1249 return NULL;
1250 }
1251
1252 int __parse_rtattr_nested_compat(struct rtattr *tb[], int max,
1253 struct rtattr *rta,
1254 int len)
1255 {
1256 if (RTA_PAYLOAD(rta) < len)
1257 return -1;
1258 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
1259 rta = RTA_DATA(rta) + RTA_ALIGN(len);
1260 return parse_rtattr_nested(tb, max, rta);
1261 }
1262 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
1263 return 0;
1264 }