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