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