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_netconfdump_req(struct rtnl_handle
*rth
, int family
)
272 struct netconfmsg ncm
;
274 .nlh
.nlmsg_len
= sizeof(req
),
275 .nlh
.nlmsg_type
= RTM_GETNETCONF
,
276 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
277 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
278 .ncm
.ncm_family
= family
,
281 return send(rth
->fd
, &req
, sizeof(req
), 0);
284 int rtnl_wilddump_request(struct rtnl_handle
*rth
, int family
, int type
)
286 return rtnl_wilddump_req_filter(rth
, family
, type
, RTEXT_FILTER_VF
);
289 int rtnl_wilddump_req_filter(struct rtnl_handle
*rth
, int family
, int type
,
294 struct ifinfomsg ifm
;
295 /* attribute has to be NLMSG aligned */
296 struct rtattr ext_req
__attribute__ ((aligned(NLMSG_ALIGNTO
)));
297 __u32 ext_filter_mask
;
299 .nlh
.nlmsg_len
= sizeof(req
),
300 .nlh
.nlmsg_type
= type
,
301 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
302 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
303 .ifm
.ifi_family
= family
,
304 .ext_req
.rta_type
= IFLA_EXT_MASK
,
305 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
306 .ext_filter_mask
= filt_mask
,
309 return send(rth
->fd
, &req
, sizeof(req
), 0);
312 int rtnl_wilddump_req_filter_fn(struct rtnl_handle
*rth
, int family
, int type
,
313 req_filter_fn_t filter_fn
)
317 struct ifinfomsg ifm
;
320 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
321 .nlh
.nlmsg_type
= type
,
322 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
323 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
324 .ifm
.ifi_family
= family
,
331 err
= filter_fn(&req
.nlh
, sizeof(req
));
335 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
338 int rtnl_wilddump_stats_req_filter(struct rtnl_handle
*rth
, int fam
, int type
,
343 struct if_stats_msg ifsm
;
346 memset(&req
, 0, sizeof(req
));
347 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
348 req
.nlh
.nlmsg_type
= type
;
349 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
350 req
.nlh
.nlmsg_pid
= 0;
351 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
352 req
.ifsm
.family
= fam
;
353 req
.ifsm
.filter_mask
= filt_mask
;
355 return send(rth
->fd
, &req
, sizeof(req
), 0);
358 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
360 return send(rth
->fd
, buf
, len
, 0);
363 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
369 status
= send(rth
->fd
, buf
, len
, 0);
373 /* Check for immediate errors */
374 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
381 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
382 h
= NLMSG_NEXT(h
, status
)) {
383 if (h
->nlmsg_type
== NLMSG_ERROR
) {
384 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
386 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
387 fprintf(stderr
, "ERROR truncated\n");
397 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
399 struct nlmsghdr nlh
= {
400 .nlmsg_len
= NLMSG_LENGTH(len
),
402 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
403 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
405 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
406 struct iovec iov
[2] = {
407 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
408 { .iov_base
= req
, .iov_len
= len
}
410 struct msghdr msg
= {
412 .msg_namelen
= sizeof(nladdr
),
417 return sendmsg(rth
->fd
, &msg
, 0);
420 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
422 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
425 .iov_len
= n
->nlmsg_len
427 struct msghdr msg
= {
429 .msg_namelen
= sizeof(nladdr
),
434 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
436 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
438 return sendmsg(rth
->fd
, &msg
, 0);
441 static int rtnl_dump_done(struct nlmsghdr
*h
)
443 int len
= *(int *)NLMSG_DATA(h
);
445 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
446 fprintf(stderr
, "DONE truncated\n");
458 "Error: Buffer too small for object.\n");
461 perror("RTNETLINK answers");
466 /* check for any messages returned from kernel */
467 nl_dump_ext_ack(h
, NULL
);
472 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
476 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
477 fprintf(stderr
, "ERROR truncated\n");
479 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
482 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
484 errno
== EOPNOTSUPP
))
487 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
488 perror("RTNETLINK answers");
492 static int __rtnl_recvmsg(int fd
, struct msghdr
*msg
, int flags
)
497 len
= recvmsg(fd
, msg
, flags
);
498 } while (len
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
501 fprintf(stderr
, "netlink receive error %s (%d)\n",
502 strerror(errno
), errno
);
507 fprintf(stderr
, "EOF on netlink\n");
514 static int rtnl_recvmsg(int fd
, struct msghdr
*msg
, char **answer
)
516 struct iovec
*iov
= msg
->msg_iov
;
520 iov
->iov_base
= NULL
;
523 len
= __rtnl_recvmsg(fd
, msg
, MSG_PEEK
| MSG_TRUNC
);
529 fprintf(stderr
, "malloc error: not enough buffer\n");
536 len
= __rtnl_recvmsg(fd
, msg
, 0);
550 int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
551 const struct rtnl_dump_filter_arg
*arg
)
553 struct sockaddr_nl nladdr
;
555 struct msghdr msg
= {
557 .msg_namelen
= sizeof(nladdr
),
566 const struct rtnl_dump_filter_arg
*a
;
570 status
= rtnl_recvmsg(rth
->fd
, &msg
, &buf
);
575 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
577 for (a
= arg
; a
->filter
; a
++) {
578 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
582 while (NLMSG_OK(h
, msglen
)) {
585 h
->nlmsg_flags
&= ~a
->nc_flags
;
587 if (nladdr
.nl_pid
!= 0 ||
588 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
589 h
->nlmsg_seq
!= rth
->dump
)
592 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
595 if (h
->nlmsg_type
== NLMSG_DONE
) {
596 err
= rtnl_dump_done(h
);
603 break; /* process next filter */
606 if (h
->nlmsg_type
== NLMSG_ERROR
) {
607 rtnl_dump_error(rth
, h
);
613 err
= a
->filter(&nladdr
, h
, a
->arg1
);
621 h
= NLMSG_NEXT(h
, msglen
);
629 "Dump was interrupted and may be inconsistent.\n");
633 if (msg
.msg_flags
& MSG_TRUNC
) {
634 fprintf(stderr
, "Message truncated\n");
638 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
644 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
645 rtnl_filter_t filter
,
646 void *arg1
, __u16 nc_flags
)
648 const struct rtnl_dump_filter_arg a
[2] = {
649 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
650 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
653 return rtnl_dump_filter_l(rth
, a
);
656 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
657 nl_ext_ack_fn_t errfn
)
659 if (nl_dump_ext_ack(h
, errfn
))
662 fprintf(stderr
, "RTNETLINK answers: %s\n",
663 strerror(-err
->error
));
667 static int __rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iov
,
668 size_t iovlen
, struct nlmsghdr
**answer
,
669 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
671 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
673 struct msghdr msg
= {
675 .msg_namelen
= sizeof(nladdr
),
677 .msg_iovlen
= iovlen
,
679 unsigned int seq
= 0;
684 for (i
= 0; i
< iovlen
; i
++) {
686 h
->nlmsg_seq
= seq
= ++rtnl
->seq
;
688 h
->nlmsg_flags
|= NLM_F_ACK
;
691 status
= sendmsg(rtnl
->fd
, &msg
, 0);
693 perror("Cannot talk to rtnetlink");
697 /* change msg to use the response iov */
702 status
= rtnl_recvmsg(rtnl
->fd
, &msg
, &buf
);
708 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
710 "sender address length == %d\n",
714 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
715 int len
= h
->nlmsg_len
;
716 int l
= len
- sizeof(*h
);
718 if (l
< 0 || len
> status
) {
719 if (msg
.msg_flags
& MSG_TRUNC
) {
720 fprintf(stderr
, "Truncated message\n");
725 "!!!malformed message: len=%d\n",
730 if (nladdr
.nl_pid
!= 0 ||
731 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
732 h
->nlmsg_seq
> seq
|| h
->nlmsg_seq
< seq
- iovlen
) {
733 /* Don't forget to skip that message. */
734 status
-= NLMSG_ALIGN(len
);
735 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
739 if (h
->nlmsg_type
== NLMSG_ERROR
) {
740 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
742 if (l
< sizeof(struct nlmsgerr
)) {
743 fprintf(stderr
, "ERROR truncated\n");
749 /* check messages from kernel */
750 nl_dump_ext_ack(h
, errfn
);
754 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
756 rtnl_talk_error(h
, err
, errfn
);
760 *answer
= (struct nlmsghdr
*)buf
;
764 return err
->error
? -i
: 0;
768 *answer
= (struct nlmsghdr
*)buf
;
772 fprintf(stderr
, "Unexpected reply!!!\n");
774 status
-= NLMSG_ALIGN(len
);
775 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
779 if (msg
.msg_flags
& MSG_TRUNC
) {
780 fprintf(stderr
, "Message truncated\n");
785 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
791 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
792 struct nlmsghdr
**answer
,
793 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
797 .iov_len
= n
->nlmsg_len
800 return __rtnl_talk_iov(rtnl
, &iov
, 1, answer
, show_rtnl_err
, errfn
);
803 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
804 struct nlmsghdr
**answer
)
806 return __rtnl_talk(rtnl
, n
, answer
, true, NULL
);
809 int rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iovec
, size_t iovlen
,
810 struct nlmsghdr
**answer
)
812 return __rtnl_talk_iov(rtnl
, iovec
, iovlen
, answer
, true, NULL
);
815 int rtnl_talk_extack(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
816 struct nlmsghdr
**answer
,
817 nl_ext_ack_fn_t errfn
)
819 return __rtnl_talk(rtnl
, n
, answer
, true, errfn
);
822 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
823 struct nlmsghdr
**answer
)
825 return __rtnl_talk(rtnl
, n
, answer
, false, NULL
);
828 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
832 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
834 perror("NETLINK_LISTEN_ALL_NSID");
837 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
841 int rtnl_listen(struct rtnl_handle
*rtnl
,
842 rtnl_listen_filter_t handler
,
847 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
849 struct msghdr msg
= {
851 .msg_namelen
= sizeof(nladdr
),
856 char cmsgbuf
[BUFSIZ
];
858 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
859 msg
.msg_control
= &cmsgbuf
;
860 msg
.msg_controllen
= sizeof(cmsgbuf
);
865 struct rtnl_ctrl_data ctrl
;
866 struct cmsghdr
*cmsg
;
868 iov
.iov_len
= sizeof(buf
);
869 status
= recvmsg(rtnl
->fd
, &msg
, 0);
872 if (errno
== EINTR
|| errno
== EAGAIN
)
874 fprintf(stderr
, "netlink receive error %s (%d)\n",
875 strerror(errno
), errno
);
876 if (errno
== ENOBUFS
)
881 fprintf(stderr
, "EOF on netlink\n");
884 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
886 "Sender address length == %d\n",
891 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
892 memset(&ctrl
, 0, sizeof(ctrl
));
894 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
895 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
896 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
897 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
898 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
899 int *data
= (int *)CMSG_DATA(cmsg
);
905 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
907 int len
= h
->nlmsg_len
;
908 int l
= len
- sizeof(*h
);
910 if (l
< 0 || len
> status
) {
911 if (msg
.msg_flags
& MSG_TRUNC
) {
912 fprintf(stderr
, "Truncated message\n");
916 "!!!malformed message: len=%d\n",
921 err
= handler(&nladdr
, &ctrl
, h
, jarg
);
925 status
-= NLMSG_ALIGN(len
);
926 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
928 if (msg
.msg_flags
& MSG_TRUNC
) {
929 fprintf(stderr
, "Message truncated\n");
933 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
939 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
943 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
945 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
951 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
956 perror("rtnl_from_file: fread");
963 l
= len
- sizeof(*h
);
965 if (l
< 0 || len
> sizeof(buf
)) {
966 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
971 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
974 perror("rtnl_from_file: fread");
978 fprintf(stderr
, "rtnl-from_file: truncated message\n");
982 err
= handler(&nladdr
, NULL
, h
, jarg
);
988 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
990 return addattr_l(n
, maxlen
, type
, NULL
, 0);
993 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
995 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
998 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
1000 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
1003 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
1005 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
1008 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
1010 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
1013 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
1015 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
1018 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
1021 int len
= RTA_LENGTH(alen
);
1024 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
1026 "addattr_l ERROR: message exceeded bound of %d\n",
1030 rta
= NLMSG_TAIL(n
);
1031 rta
->rta_type
= type
;
1034 memcpy(RTA_DATA(rta
), data
, alen
);
1035 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
1039 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
1041 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
1043 "addraw_l ERROR: message exceeded bound of %d\n",
1048 memcpy(NLMSG_TAIL(n
), data
, len
);
1049 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
1050 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
1054 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
1056 struct rtattr
*nest
= NLMSG_TAIL(n
);
1058 addattr_l(n
, maxlen
, type
, NULL
, 0);
1062 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
1064 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
1065 return n
->nlmsg_len
;
1068 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
1069 const void *data
, int len
)
1071 struct rtattr
*start
= NLMSG_TAIL(n
);
1073 addattr_l(n
, maxlen
, type
, data
, len
);
1074 addattr_nest(n
, maxlen
, type
);
1078 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
1080 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
1082 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
1083 addattr_nest_end(n
, nest
);
1084 return n
->nlmsg_len
;
1087 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
1089 int len
= RTA_LENGTH(4);
1090 struct rtattr
*subrta
;
1092 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
1094 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1098 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1099 subrta
->rta_type
= type
;
1100 subrta
->rta_len
= len
;
1101 memcpy(RTA_DATA(subrta
), &data
, 4);
1102 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
1106 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
1107 const void *data
, int alen
)
1109 struct rtattr
*subrta
;
1110 int len
= RTA_LENGTH(alen
);
1112 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
1114 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1118 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1119 subrta
->rta_type
= type
;
1120 subrta
->rta_len
= len
;
1122 memcpy(RTA_DATA(subrta
), data
, alen
);
1123 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
1127 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
1129 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
1132 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
1134 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
1137 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
1139 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
1142 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
1144 struct rtattr
*nest
= RTA_TAIL(rta
);
1146 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
1151 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
1153 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
1155 return rta
->rta_len
;
1158 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
1160 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
1163 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
1164 int len
, unsigned short flags
)
1166 unsigned short type
;
1168 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1169 while (RTA_OK(rta
, len
)) {
1170 type
= rta
->rta_type
& ~flags
;
1171 if ((type
<= max
) && (!tb
[type
]))
1173 rta
= RTA_NEXT(rta
, len
);
1176 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1181 int parse_rtattr_byindex(struct rtattr
*tb
[], int max
,
1182 struct rtattr
*rta
, int len
)
1186 memset(tb
, 0, sizeof(struct rtattr
*) * max
);
1187 while (RTA_OK(rta
, len
)) {
1188 if (rta
->rta_type
<= max
&& i
< max
)
1190 rta
= RTA_NEXT(rta
, len
);
1193 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1198 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1200 while (RTA_OK(rta
, len
)) {
1201 if (rta
->rta_type
== type
)
1203 rta
= RTA_NEXT(rta
, len
);
1207 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1212 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1216 if (RTA_PAYLOAD(rta
) < len
)
1218 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1219 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1220 return parse_rtattr_nested(tb
, max
, rta
);
1222 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));