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>
18 #include <net/if_arp.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
25 #include <linux/if_addrlabel.h>
26 #include <linux/if_bridge.h>
28 #include "libnetlink.h"
31 #define SOL_NETLINK 270
35 #define MIN(a, b) ((a) < (b) ? (a) : (b))
38 int rcvbuf
= 1024 * 1024;
41 #include <libmnl/libmnl.h>
43 static const enum mnl_attr_data_type extack_policy
[NLMSGERR_ATTR_MAX
+ 1] = {
44 [NLMSGERR_ATTR_MSG
] = MNL_TYPE_NUL_STRING
,
45 [NLMSGERR_ATTR_OFFS
] = MNL_TYPE_U32
,
48 static int err_attr_cb(const struct nlattr
*attr
, void *data
)
50 const struct nlattr
**tb
= data
;
53 if (mnl_attr_type_valid(attr
, NLMSGERR_ATTR_MAX
) < 0) {
54 fprintf(stderr
, "Invalid extack attribute\n");
58 type
= mnl_attr_get_type(attr
);
59 if (mnl_attr_validate(attr
, extack_policy
[type
]) < 0) {
60 fprintf(stderr
, "extack attribute %d failed validation\n",
69 /* dump netlink extended ack error message */
70 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
72 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
73 const struct nlmsgerr
*err
= mnl_nlmsg_get_payload(nlh
);
74 const struct nlmsghdr
*err_nlh
= NULL
;
75 unsigned int hlen
= sizeof(*err
);
76 const char *msg
= NULL
;
79 /* no TLVs, nothing to do here */
80 if (!(nlh
->nlmsg_flags
& NLM_F_ACK_TLVS
))
83 /* if NLM_F_CAPPED is set then the inner err msg was capped */
84 if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
85 hlen
+= mnl_nlmsg_get_payload_len(&err
->msg
);
87 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
90 if (tb
[NLMSGERR_ATTR_MSG
])
91 msg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
93 if (tb
[NLMSGERR_ATTR_OFFS
]) {
94 off
= mnl_attr_get_u32(tb
[NLMSGERR_ATTR_OFFS
]);
96 if (off
> nlh
->nlmsg_len
) {
98 "Invalid offset for NLMSGERR_ATTR_OFFS\n");
100 } else if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
105 return errfn(msg
, off
, err_nlh
);
107 if (msg
&& *msg
!= '\0') {
108 bool is_err
= !!err
->error
;
110 fprintf(stderr
, "%s: %s",
111 is_err
? "Error" : "Warning", msg
);
112 if (msg
[strlen(msg
) - 1] != '.')
113 fprintf(stderr
, ".");
114 fprintf(stderr
, "\n");
116 return is_err
? 1 : 0;
122 #warning "libmnl required for error support"
124 /* No extended error ack without libmnl */
125 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
131 void rtnl_close(struct rtnl_handle
*rth
)
139 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
146 memset(rth
, 0, sizeof(*rth
));
148 rth
->proto
= protocol
;
149 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
151 perror("Cannot open netlink socket");
155 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_SNDBUF
,
156 &sndbuf
, sizeof(sndbuf
)) < 0) {
161 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_RCVBUF
,
162 &rcvbuf
, sizeof(rcvbuf
)) < 0) {
167 /* Older kernels may no support extended ACK reporting */
168 setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_EXT_ACK
,
171 memset(&rth
->local
, 0, sizeof(rth
->local
));
172 rth
->local
.nl_family
= AF_NETLINK
;
173 rth
->local
.nl_groups
= subscriptions
;
175 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
,
176 sizeof(rth
->local
)) < 0) {
177 perror("Cannot bind netlink socket");
180 addr_len
= sizeof(rth
->local
);
181 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
,
183 perror("Cannot getsockname");
186 if (addr_len
!= sizeof(rth
->local
)) {
187 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
190 if (rth
->local
.nl_family
!= AF_NETLINK
) {
191 fprintf(stderr
, "Wrong address family %d\n",
192 rth
->local
.nl_family
);
195 rth
->seq
= time(NULL
);
199 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
201 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
204 int rtnl_addrdump_req(struct rtnl_handle
*rth
, int family
)
208 struct ifaddrmsg ifm
;
210 .nlh
.nlmsg_len
= sizeof(req
),
211 .nlh
.nlmsg_type
= RTM_GETADDR
,
212 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
213 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
214 .ifm
.ifa_family
= family
,
217 return send(rth
->fd
, &req
, sizeof(req
), 0);
220 int rtnl_addrlbldump_req(struct rtnl_handle
*rth
, int family
)
224 struct ifaddrlblmsg ifal
;
226 .nlh
.nlmsg_len
= sizeof(req
),
227 .nlh
.nlmsg_type
= RTM_GETADDRLABEL
,
228 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
229 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
230 .ifal
.ifal_family
= family
,
233 return send(rth
->fd
, &req
, sizeof(req
), 0);
236 int rtnl_routedump_req(struct rtnl_handle
*rth
, int family
)
242 .nlh
.nlmsg_len
= sizeof(req
),
243 .nlh
.nlmsg_type
= RTM_GETROUTE
,
244 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
245 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
246 .rtm
.rtm_family
= family
,
249 return send(rth
->fd
, &req
, sizeof(req
), 0);
252 int rtnl_mdbdump_req(struct rtnl_handle
*rth
, int family
)
256 struct br_port_msg bpm
;
258 .nlh
.nlmsg_len
= sizeof(req
),
259 .nlh
.nlmsg_type
= RTM_GETMDB
,
260 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
261 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
262 .bpm
.family
= family
,
265 return send(rth
->fd
, &req
, sizeof(req
), 0);
268 int rtnl_wilddump_request(struct rtnl_handle
*rth
, int family
, int type
)
270 return rtnl_wilddump_req_filter(rth
, family
, type
, RTEXT_FILTER_VF
);
273 int rtnl_wilddump_req_filter(struct rtnl_handle
*rth
, int family
, int type
,
278 struct ifinfomsg ifm
;
279 /* attribute has to be NLMSG aligned */
280 struct rtattr ext_req
__attribute__ ((aligned(NLMSG_ALIGNTO
)));
281 __u32 ext_filter_mask
;
283 .nlh
.nlmsg_len
= sizeof(req
),
284 .nlh
.nlmsg_type
= type
,
285 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
286 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
287 .ifm
.ifi_family
= family
,
288 .ext_req
.rta_type
= IFLA_EXT_MASK
,
289 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
290 .ext_filter_mask
= filt_mask
,
293 return send(rth
->fd
, &req
, sizeof(req
), 0);
296 int rtnl_wilddump_req_filter_fn(struct rtnl_handle
*rth
, int family
, int type
,
297 req_filter_fn_t filter_fn
)
301 struct ifinfomsg ifm
;
304 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
305 .nlh
.nlmsg_type
= type
,
306 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
307 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
308 .ifm
.ifi_family
= family
,
315 err
= filter_fn(&req
.nlh
, sizeof(req
));
319 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
322 int rtnl_wilddump_stats_req_filter(struct rtnl_handle
*rth
, int fam
, int type
,
327 struct if_stats_msg ifsm
;
330 memset(&req
, 0, sizeof(req
));
331 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
332 req
.nlh
.nlmsg_type
= type
;
333 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
334 req
.nlh
.nlmsg_pid
= 0;
335 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
336 req
.ifsm
.family
= fam
;
337 req
.ifsm
.filter_mask
= filt_mask
;
339 return send(rth
->fd
, &req
, sizeof(req
), 0);
342 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
344 return send(rth
->fd
, buf
, len
, 0);
347 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
353 status
= send(rth
->fd
, buf
, len
, 0);
357 /* Check for immediate errors */
358 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
365 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
366 h
= NLMSG_NEXT(h
, status
)) {
367 if (h
->nlmsg_type
== NLMSG_ERROR
) {
368 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
370 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
371 fprintf(stderr
, "ERROR truncated\n");
381 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
383 struct nlmsghdr nlh
= {
384 .nlmsg_len
= NLMSG_LENGTH(len
),
386 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
387 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
389 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
390 struct iovec iov
[2] = {
391 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
392 { .iov_base
= req
, .iov_len
= len
}
394 struct msghdr msg
= {
396 .msg_namelen
= sizeof(nladdr
),
401 return sendmsg(rth
->fd
, &msg
, 0);
404 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
406 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
409 .iov_len
= n
->nlmsg_len
411 struct msghdr msg
= {
413 .msg_namelen
= sizeof(nladdr
),
418 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
420 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
422 return sendmsg(rth
->fd
, &msg
, 0);
425 static int rtnl_dump_done(struct nlmsghdr
*h
)
427 int len
= *(int *)NLMSG_DATA(h
);
429 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
430 fprintf(stderr
, "DONE truncated\n");
442 "Error: Buffer too small for object.\n");
445 perror("RTNETLINK answers");
450 /* check for any messages returned from kernel */
451 nl_dump_ext_ack(h
, NULL
);
456 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
460 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
461 fprintf(stderr
, "ERROR truncated\n");
463 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
466 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
468 errno
== EOPNOTSUPP
))
471 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
472 perror("RTNETLINK answers");
476 static int __rtnl_recvmsg(int fd
, struct msghdr
*msg
, int flags
)
481 len
= recvmsg(fd
, msg
, flags
);
482 } while (len
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
485 fprintf(stderr
, "netlink receive error %s (%d)\n",
486 strerror(errno
), errno
);
491 fprintf(stderr
, "EOF on netlink\n");
498 static int rtnl_recvmsg(int fd
, struct msghdr
*msg
, char **answer
)
500 struct iovec
*iov
= msg
->msg_iov
;
504 iov
->iov_base
= NULL
;
507 len
= __rtnl_recvmsg(fd
, msg
, MSG_PEEK
| MSG_TRUNC
);
513 fprintf(stderr
, "malloc error: not enough buffer\n");
520 len
= __rtnl_recvmsg(fd
, msg
, 0);
534 int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
535 const struct rtnl_dump_filter_arg
*arg
)
537 struct sockaddr_nl nladdr
;
539 struct msghdr msg
= {
541 .msg_namelen
= sizeof(nladdr
),
550 const struct rtnl_dump_filter_arg
*a
;
554 status
= rtnl_recvmsg(rth
->fd
, &msg
, &buf
);
559 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
561 for (a
= arg
; a
->filter
; a
++) {
562 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
566 while (NLMSG_OK(h
, msglen
)) {
569 h
->nlmsg_flags
&= ~a
->nc_flags
;
571 if (nladdr
.nl_pid
!= 0 ||
572 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
573 h
->nlmsg_seq
!= rth
->dump
)
576 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
579 if (h
->nlmsg_type
== NLMSG_DONE
) {
580 err
= rtnl_dump_done(h
);
587 break; /* process next filter */
590 if (h
->nlmsg_type
== NLMSG_ERROR
) {
591 rtnl_dump_error(rth
, h
);
597 err
= a
->filter(&nladdr
, h
, a
->arg1
);
605 h
= NLMSG_NEXT(h
, msglen
);
613 "Dump was interrupted and may be inconsistent.\n");
617 if (msg
.msg_flags
& MSG_TRUNC
) {
618 fprintf(stderr
, "Message truncated\n");
622 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
628 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
629 rtnl_filter_t filter
,
630 void *arg1
, __u16 nc_flags
)
632 const struct rtnl_dump_filter_arg a
[2] = {
633 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
634 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
637 return rtnl_dump_filter_l(rth
, a
);
640 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
641 nl_ext_ack_fn_t errfn
)
643 if (nl_dump_ext_ack(h
, errfn
))
646 fprintf(stderr
, "RTNETLINK answers: %s\n",
647 strerror(-err
->error
));
651 static int __rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iov
,
652 size_t iovlen
, struct nlmsghdr
**answer
,
653 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
655 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
657 struct msghdr msg
= {
659 .msg_namelen
= sizeof(nladdr
),
661 .msg_iovlen
= iovlen
,
663 unsigned int seq
= 0;
668 for (i
= 0; i
< iovlen
; i
++) {
670 h
->nlmsg_seq
= seq
= ++rtnl
->seq
;
672 h
->nlmsg_flags
|= NLM_F_ACK
;
675 status
= sendmsg(rtnl
->fd
, &msg
, 0);
677 perror("Cannot talk to rtnetlink");
681 /* change msg to use the response iov */
686 status
= rtnl_recvmsg(rtnl
->fd
, &msg
, &buf
);
692 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
694 "sender address length == %d\n",
698 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
699 int len
= h
->nlmsg_len
;
700 int l
= len
- sizeof(*h
);
702 if (l
< 0 || len
> status
) {
703 if (msg
.msg_flags
& MSG_TRUNC
) {
704 fprintf(stderr
, "Truncated message\n");
709 "!!!malformed message: len=%d\n",
714 if (nladdr
.nl_pid
!= 0 ||
715 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
716 h
->nlmsg_seq
> seq
|| h
->nlmsg_seq
< seq
- iovlen
) {
717 /* Don't forget to skip that message. */
718 status
-= NLMSG_ALIGN(len
);
719 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
723 if (h
->nlmsg_type
== NLMSG_ERROR
) {
724 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
726 if (l
< sizeof(struct nlmsgerr
)) {
727 fprintf(stderr
, "ERROR truncated\n");
733 /* check messages from kernel */
734 nl_dump_ext_ack(h
, errfn
);
738 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
740 rtnl_talk_error(h
, err
, errfn
);
744 *answer
= (struct nlmsghdr
*)buf
;
748 return err
->error
? -i
: 0;
752 *answer
= (struct nlmsghdr
*)buf
;
756 fprintf(stderr
, "Unexpected reply!!!\n");
758 status
-= NLMSG_ALIGN(len
);
759 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
763 if (msg
.msg_flags
& MSG_TRUNC
) {
764 fprintf(stderr
, "Message truncated\n");
769 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
775 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
776 struct nlmsghdr
**answer
,
777 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
781 .iov_len
= n
->nlmsg_len
784 return __rtnl_talk_iov(rtnl
, &iov
, 1, answer
, show_rtnl_err
, errfn
);
787 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
788 struct nlmsghdr
**answer
)
790 return __rtnl_talk(rtnl
, n
, answer
, true, NULL
);
793 int rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iovec
, size_t iovlen
,
794 struct nlmsghdr
**answer
)
796 return __rtnl_talk_iov(rtnl
, iovec
, iovlen
, answer
, true, NULL
);
799 int rtnl_talk_extack(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
800 struct nlmsghdr
**answer
,
801 nl_ext_ack_fn_t errfn
)
803 return __rtnl_talk(rtnl
, n
, answer
, true, errfn
);
806 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
807 struct nlmsghdr
**answer
)
809 return __rtnl_talk(rtnl
, n
, answer
, false, NULL
);
812 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
816 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
818 perror("NETLINK_LISTEN_ALL_NSID");
821 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
825 int rtnl_listen(struct rtnl_handle
*rtnl
,
826 rtnl_listen_filter_t handler
,
831 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
833 struct msghdr msg
= {
835 .msg_namelen
= sizeof(nladdr
),
840 char cmsgbuf
[BUFSIZ
];
842 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
843 msg
.msg_control
= &cmsgbuf
;
844 msg
.msg_controllen
= sizeof(cmsgbuf
);
849 struct rtnl_ctrl_data ctrl
;
850 struct cmsghdr
*cmsg
;
852 iov
.iov_len
= sizeof(buf
);
853 status
= recvmsg(rtnl
->fd
, &msg
, 0);
856 if (errno
== EINTR
|| errno
== EAGAIN
)
858 fprintf(stderr
, "netlink receive error %s (%d)\n",
859 strerror(errno
), errno
);
860 if (errno
== ENOBUFS
)
865 fprintf(stderr
, "EOF on netlink\n");
868 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
870 "Sender address length == %d\n",
875 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
876 memset(&ctrl
, 0, sizeof(ctrl
));
878 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
879 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
880 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
881 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
882 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
883 int *data
= (int *)CMSG_DATA(cmsg
);
889 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
891 int len
= h
->nlmsg_len
;
892 int l
= len
- sizeof(*h
);
894 if (l
< 0 || len
> status
) {
895 if (msg
.msg_flags
& MSG_TRUNC
) {
896 fprintf(stderr
, "Truncated message\n");
900 "!!!malformed message: len=%d\n",
905 err
= handler(&nladdr
, &ctrl
, h
, jarg
);
909 status
-= NLMSG_ALIGN(len
);
910 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
912 if (msg
.msg_flags
& MSG_TRUNC
) {
913 fprintf(stderr
, "Message truncated\n");
917 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
923 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
927 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
929 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
935 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
940 perror("rtnl_from_file: fread");
947 l
= len
- sizeof(*h
);
949 if (l
< 0 || len
> sizeof(buf
)) {
950 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
955 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
958 perror("rtnl_from_file: fread");
962 fprintf(stderr
, "rtnl-from_file: truncated message\n");
966 err
= handler(&nladdr
, NULL
, h
, jarg
);
972 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
974 return addattr_l(n
, maxlen
, type
, NULL
, 0);
977 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
979 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
982 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
984 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
987 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
989 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
992 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
994 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
997 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
999 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
1002 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
1005 int len
= RTA_LENGTH(alen
);
1008 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
1010 "addattr_l ERROR: message exceeded bound of %d\n",
1014 rta
= NLMSG_TAIL(n
);
1015 rta
->rta_type
= type
;
1018 memcpy(RTA_DATA(rta
), data
, alen
);
1019 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
1023 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
1025 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
1027 "addraw_l ERROR: message exceeded bound of %d\n",
1032 memcpy(NLMSG_TAIL(n
), data
, len
);
1033 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
1034 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
1038 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
1040 struct rtattr
*nest
= NLMSG_TAIL(n
);
1042 addattr_l(n
, maxlen
, type
, NULL
, 0);
1046 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
1048 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
1049 return n
->nlmsg_len
;
1052 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
1053 const void *data
, int len
)
1055 struct rtattr
*start
= NLMSG_TAIL(n
);
1057 addattr_l(n
, maxlen
, type
, data
, len
);
1058 addattr_nest(n
, maxlen
, type
);
1062 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
1064 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
1066 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
1067 addattr_nest_end(n
, nest
);
1068 return n
->nlmsg_len
;
1071 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
1073 int len
= RTA_LENGTH(4);
1074 struct rtattr
*subrta
;
1076 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
1078 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1082 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1083 subrta
->rta_type
= type
;
1084 subrta
->rta_len
= len
;
1085 memcpy(RTA_DATA(subrta
), &data
, 4);
1086 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
1090 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
1091 const void *data
, int alen
)
1093 struct rtattr
*subrta
;
1094 int len
= RTA_LENGTH(alen
);
1096 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
1098 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1102 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1103 subrta
->rta_type
= type
;
1104 subrta
->rta_len
= len
;
1106 memcpy(RTA_DATA(subrta
), data
, alen
);
1107 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
1111 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
1113 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
1116 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
1118 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
1121 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
1123 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
1126 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
1128 struct rtattr
*nest
= RTA_TAIL(rta
);
1130 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
1135 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
1137 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
1139 return rta
->rta_len
;
1142 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
1144 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
1147 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
1148 int len
, unsigned short flags
)
1150 unsigned short type
;
1152 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1153 while (RTA_OK(rta
, len
)) {
1154 type
= rta
->rta_type
& ~flags
;
1155 if ((type
<= max
) && (!tb
[type
]))
1157 rta
= RTA_NEXT(rta
, len
);
1160 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1165 int parse_rtattr_byindex(struct rtattr
*tb
[], int max
,
1166 struct rtattr
*rta
, int len
)
1170 memset(tb
, 0, sizeof(struct rtattr
*) * max
);
1171 while (RTA_OK(rta
, len
)) {
1172 if (rta
->rta_type
<= max
&& i
< max
)
1174 rta
= RTA_NEXT(rta
, len
);
1177 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1182 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1184 while (RTA_OK(rta
, len
)) {
1185 if (rta
->rta_type
== type
)
1187 rta
= RTA_NEXT(rta
, len
);
1191 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1196 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1200 if (RTA_PAYLOAD(rta
) < len
)
1202 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1203 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1204 return parse_rtattr_nested(tb
, max
, rta
);
1206 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));