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