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