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