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