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