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