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