2 * libnetlink.c RTnetlink service routines.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
19 #include <net/if_arp.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
27 #include "libnetlink.h"
30 #define SOL_NETLINK 270
34 #define MIN(a, b) ((a) < (b) ? (a) : (b))
37 int rcvbuf
= 1024 * 1024;
39 void rtnl_close(struct rtnl_handle
*rth
)
47 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
53 memset(rth
, 0, sizeof(*rth
));
55 rth
->proto
= protocol
;
56 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
58 perror("Cannot open netlink socket");
62 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_SNDBUF
,
63 &sndbuf
, sizeof(sndbuf
)) < 0) {
68 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_RCVBUF
,
69 &rcvbuf
, sizeof(rcvbuf
)) < 0) {
74 memset(&rth
->local
, 0, sizeof(rth
->local
));
75 rth
->local
.nl_family
= AF_NETLINK
;
76 rth
->local
.nl_groups
= subscriptions
;
78 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
,
79 sizeof(rth
->local
)) < 0) {
80 perror("Cannot bind netlink socket");
83 addr_len
= sizeof(rth
->local
);
84 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
,
86 perror("Cannot getsockname");
89 if (addr_len
!= sizeof(rth
->local
)) {
90 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
93 if (rth
->local
.nl_family
!= AF_NETLINK
) {
94 fprintf(stderr
, "Wrong address family %d\n",
95 rth
->local
.nl_family
);
98 rth
->seq
= time(NULL
);
102 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
104 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
107 int rtnl_wilddump_request(struct rtnl_handle
*rth
, int family
, int type
)
109 return rtnl_wilddump_req_filter(rth
, family
, type
, RTEXT_FILTER_VF
);
112 int rtnl_wilddump_req_filter(struct rtnl_handle
*rth
, int family
, int type
,
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
;
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
,
132 return send(rth
->fd
, &req
, sizeof(req
), 0);
135 int rtnl_wilddump_req_filter_fn(struct rtnl_handle
*rth
, int family
, int type
,
136 req_filter_fn_t filter_fn
)
140 struct ifinfomsg ifm
;
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
,
154 err
= filter_fn(&req
.nlh
, sizeof(req
));
158 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
161 int rtnl_wilddump_stats_req_filter(struct rtnl_handle
*rth
, int fam
, int type
,
166 struct if_stats_msg ifsm
;
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
;
178 return send(rth
->fd
, &req
, sizeof(req
), 0);
181 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
183 return send(rth
->fd
, buf
, len
, 0);
186 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
192 status
= send(rth
->fd
, buf
, len
, 0);
196 /* Check for immediate errors */
197 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
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
);
209 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
210 fprintf(stderr
, "ERROR truncated\n");
220 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
222 struct nlmsghdr nlh
= {
223 .nlmsg_len
= NLMSG_LENGTH(len
),
225 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
226 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
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
}
233 struct msghdr msg
= {
235 .msg_namelen
= sizeof(nladdr
),
240 return sendmsg(rth
->fd
, &msg
, 0);
243 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
245 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
248 .iov_len
= n
->nlmsg_len
250 struct msghdr msg
= {
252 .msg_namelen
= sizeof(nladdr
),
257 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
259 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
261 return sendmsg(rth
->fd
, &msg
, 0);
264 static int rtnl_dump_done(const struct rtnl_handle
*rth
,
267 int len
= *(int *)NLMSG_DATA(h
);
269 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
270 fprintf(stderr
, "DONE truncated\n");
282 "Error: Buffer too small for object.\n");
285 perror("RTNETLINK answers");
293 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
297 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
298 fprintf(stderr
, "ERROR truncated\n");
300 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
303 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
305 errno
== EOPNOTSUPP
))
308 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
309 perror("RTNETLINK answers");
313 int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
314 const struct rtnl_dump_filter_arg
*arg
)
316 struct sockaddr_nl nladdr
;
318 struct msghdr msg
= {
320 .msg_namelen
= sizeof(nladdr
),
330 const struct rtnl_dump_filter_arg
*a
;
334 iov
.iov_len
= sizeof(buf
);
335 status
= recvmsg(rth
->fd
, &msg
, 0);
338 if (errno
== EINTR
|| errno
== EAGAIN
)
340 fprintf(stderr
, "netlink receive error %s (%d)\n",
341 strerror(errno
), errno
);
346 fprintf(stderr
, "EOF on netlink\n");
351 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
353 for (a
= arg
; a
->filter
; a
++) {
354 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
358 while (NLMSG_OK(h
, msglen
)) {
361 h
->nlmsg_flags
&= ~a
->nc_flags
;
363 if (nladdr
.nl_pid
!= 0 ||
364 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
365 h
->nlmsg_seq
!= rth
->dump
)
368 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
371 if (h
->nlmsg_type
== NLMSG_DONE
) {
372 err
= rtnl_dump_done(rth
, h
);
377 break; /* process next filter */
380 if (h
->nlmsg_type
== NLMSG_ERROR
) {
381 rtnl_dump_error(rth
, h
);
386 err
= a
->filter(&nladdr
, h
, a
->arg1
);
392 h
= NLMSG_NEXT(h
, msglen
);
399 "Dump was interrupted and may be inconsistent.\n");
403 if (msg
.msg_flags
& MSG_TRUNC
) {
404 fprintf(stderr
, "Message truncated\n");
408 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
414 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
415 rtnl_filter_t filter
,
416 void *arg1
, __u16 nc_flags
)
418 const struct rtnl_dump_filter_arg a
[2] = {
419 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
420 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
423 return rtnl_dump_filter_l(rth
, a
);
426 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
427 struct nlmsghdr
*answer
, size_t maxlen
,
433 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
436 .iov_len
= n
->nlmsg_len
438 struct msghdr msg
= {
440 .msg_namelen
= sizeof(nladdr
),
444 char buf
[32768] = {};
446 n
->nlmsg_seq
= seq
= ++rtnl
->seq
;
449 n
->nlmsg_flags
|= NLM_F_ACK
;
451 status
= sendmsg(rtnl
->fd
, &msg
, 0);
453 perror("Cannot talk to rtnetlink");
459 iov
.iov_len
= sizeof(buf
);
460 status
= recvmsg(rtnl
->fd
, &msg
, 0);
463 if (errno
== EINTR
|| errno
== EAGAIN
)
465 fprintf(stderr
, "netlink receive error %s (%d)\n",
466 strerror(errno
), errno
);
470 fprintf(stderr
, "EOF on netlink\n");
473 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
475 "sender address length == %d\n",
479 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
480 int len
= h
->nlmsg_len
;
481 int l
= len
- sizeof(*h
);
483 if (l
< 0 || len
> status
) {
484 if (msg
.msg_flags
& MSG_TRUNC
) {
485 fprintf(stderr
, "Truncated message\n");
489 "!!!malformed message: len=%d\n",
494 if (nladdr
.nl_pid
!= 0 ||
495 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
496 h
->nlmsg_seq
!= seq
) {
497 /* Don't forget to skip that message. */
498 status
-= NLMSG_ALIGN(len
);
499 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
503 if (h
->nlmsg_type
== NLMSG_ERROR
) {
504 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
506 if (l
< sizeof(struct nlmsgerr
)) {
507 fprintf(stderr
, "ERROR truncated\n");
508 } else if (!err
->error
) {
511 MIN(maxlen
, h
->nlmsg_len
));
515 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&& show_rtnl_err
)
517 "RTNETLINK answers: %s\n",
518 strerror(-err
->error
));
525 MIN(maxlen
, h
->nlmsg_len
));
529 fprintf(stderr
, "Unexpected reply!!!\n");
531 status
-= NLMSG_ALIGN(len
);
532 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
535 if (msg
.msg_flags
& MSG_TRUNC
) {
536 fprintf(stderr
, "Message truncated\n");
541 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
547 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
548 struct nlmsghdr
*answer
, size_t maxlen
)
550 return __rtnl_talk(rtnl
, n
, answer
, maxlen
, true);
553 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
554 struct nlmsghdr
*answer
, size_t maxlen
)
556 return __rtnl_talk(rtnl
, n
, answer
, maxlen
, false);
559 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
563 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
565 perror("NETLINK_LISTEN_ALL_NSID");
568 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
572 int rtnl_listen(struct rtnl_handle
*rtnl
,
573 rtnl_listen_filter_t handler
,
578 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
580 struct msghdr msg
= {
582 .msg_namelen
= sizeof(nladdr
),
587 char cmsgbuf
[BUFSIZ
];
589 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
590 msg
.msg_control
= &cmsgbuf
;
591 msg
.msg_controllen
= sizeof(cmsgbuf
);
596 struct rtnl_ctrl_data ctrl
;
597 struct cmsghdr
*cmsg
;
599 iov
.iov_len
= sizeof(buf
);
600 status
= recvmsg(rtnl
->fd
, &msg
, 0);
603 if (errno
== EINTR
|| errno
== EAGAIN
)
605 fprintf(stderr
, "netlink receive error %s (%d)\n",
606 strerror(errno
), errno
);
607 if (errno
== ENOBUFS
)
612 fprintf(stderr
, "EOF on netlink\n");
615 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
617 "Sender address length == %d\n",
622 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
623 memset(&ctrl
, 0, sizeof(ctrl
));
625 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
626 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
627 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
628 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
629 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
630 int *data
= (int *)CMSG_DATA(cmsg
);
636 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
638 int len
= h
->nlmsg_len
;
639 int l
= len
- sizeof(*h
);
641 if (l
< 0 || len
> status
) {
642 if (msg
.msg_flags
& MSG_TRUNC
) {
643 fprintf(stderr
, "Truncated message\n");
647 "!!!malformed message: len=%d\n",
652 err
= handler(&nladdr
, &ctrl
, h
, jarg
);
656 status
-= NLMSG_ALIGN(len
);
657 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
659 if (msg
.msg_flags
& MSG_TRUNC
) {
660 fprintf(stderr
, "Message truncated\n");
664 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
670 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
674 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
676 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
682 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
687 perror("rtnl_from_file: fread");
694 l
= len
- sizeof(*h
);
696 if (l
< 0 || len
> sizeof(buf
)) {
697 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
702 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
705 perror("rtnl_from_file: fread");
709 fprintf(stderr
, "rtnl-from_file: truncated message\n");
713 err
= handler(&nladdr
, NULL
, h
, jarg
);
719 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
721 return addattr_l(n
, maxlen
, type
, NULL
, 0);
724 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
726 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
729 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
731 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
734 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
736 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
739 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
741 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
744 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
746 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
749 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
752 int len
= RTA_LENGTH(alen
);
755 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
757 "addattr_l ERROR: message exceeded bound of %d\n",
762 rta
->rta_type
= type
;
764 memcpy(RTA_DATA(rta
), data
, alen
);
765 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
769 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
771 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
773 "addraw_l ERROR: message exceeded bound of %d\n",
778 memcpy(NLMSG_TAIL(n
), data
, len
);
779 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
780 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
784 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
786 struct rtattr
*nest
= NLMSG_TAIL(n
);
788 addattr_l(n
, maxlen
, type
, NULL
, 0);
792 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
794 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
798 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
799 const void *data
, int len
)
801 struct rtattr
*start
= NLMSG_TAIL(n
);
803 addattr_l(n
, maxlen
, type
, data
, len
);
804 addattr_nest(n
, maxlen
, type
);
808 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
810 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
812 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
813 addattr_nest_end(n
, nest
);
817 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
819 int len
= RTA_LENGTH(4);
820 struct rtattr
*subrta
;
822 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
824 "rta_addattr32: Error! max allowed bound %d exceeded\n",
828 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
829 subrta
->rta_type
= type
;
830 subrta
->rta_len
= len
;
831 memcpy(RTA_DATA(subrta
), &data
, 4);
832 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
836 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
837 const void *data
, int alen
)
839 struct rtattr
*subrta
;
840 int len
= RTA_LENGTH(alen
);
842 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
844 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
848 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
849 subrta
->rta_type
= type
;
850 subrta
->rta_len
= len
;
851 memcpy(RTA_DATA(subrta
), data
, alen
);
852 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
856 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
858 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
861 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
863 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
866 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
868 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
871 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
873 struct rtattr
*nest
= RTA_TAIL(rta
);
875 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
880 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
882 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
887 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
889 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
892 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
893 int len
, unsigned short flags
)
897 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
898 while (RTA_OK(rta
, len
)) {
899 type
= rta
->rta_type
& ~flags
;
900 if ((type
<= max
) && (!tb
[type
]))
902 rta
= RTA_NEXT(rta
, len
);
905 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
910 int parse_rtattr_byindex(struct rtattr
*tb
[], int max
,
911 struct rtattr
*rta
, int len
)
915 memset(tb
, 0, sizeof(struct rtattr
*) * max
);
916 while (RTA_OK(rta
, len
)) {
917 if (rta
->rta_type
<= max
&& i
< max
)
919 rta
= RTA_NEXT(rta
, len
);
922 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
927 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
929 while (RTA_OK(rta
, len
)) {
930 if (rta
->rta_type
== type
)
932 rta
= RTA_NEXT(rta
, len
);
936 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
941 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
945 if (RTA_PAYLOAD(rta
) < len
)
947 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
948 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
949 return parse_rtattr_nested(tb
, max
, rta
);
951 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));