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/fib_rules.h>
26 #include <linux/if_addrlabel.h>
27 #include <linux/if_bridge.h>
29 #include "libnetlink.h"
32 #define SOL_NETLINK 270
36 #define MIN(a, b) ((a) < (b) ? (a) : (b))
39 int rcvbuf
= 1024 * 1024;
42 #include <libmnl/libmnl.h>
44 static const enum mnl_attr_data_type extack_policy
[NLMSGERR_ATTR_MAX
+ 1] = {
45 [NLMSGERR_ATTR_MSG
] = MNL_TYPE_NUL_STRING
,
46 [NLMSGERR_ATTR_OFFS
] = MNL_TYPE_U32
,
49 static int err_attr_cb(const struct nlattr
*attr
, void *data
)
51 const struct nlattr
**tb
= data
;
54 if (mnl_attr_type_valid(attr
, NLMSGERR_ATTR_MAX
) < 0) {
55 fprintf(stderr
, "Invalid extack attribute\n");
59 type
= mnl_attr_get_type(attr
);
60 if (mnl_attr_validate(attr
, extack_policy
[type
]) < 0) {
61 fprintf(stderr
, "extack attribute %d failed validation\n",
70 static void print_ext_ack_msg(bool is_err
, const char *msg
)
72 fprintf(stderr
, "%s: %s", is_err
? "Error" : "Warning", msg
);
73 if (msg
[strlen(msg
) - 1] != '.')
75 fprintf(stderr
, "\n");
78 /* dump netlink extended ack error message */
79 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
81 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
82 const struct nlmsgerr
*err
= mnl_nlmsg_get_payload(nlh
);
83 const struct nlmsghdr
*err_nlh
= NULL
;
84 unsigned int hlen
= sizeof(*err
);
85 const char *msg
= NULL
;
88 /* no TLVs, nothing to do here */
89 if (!(nlh
->nlmsg_flags
& NLM_F_ACK_TLVS
))
92 /* if NLM_F_CAPPED is set then the inner err msg was capped */
93 if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
94 hlen
+= mnl_nlmsg_get_payload_len(&err
->msg
);
96 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
99 if (tb
[NLMSGERR_ATTR_MSG
])
100 msg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
102 if (tb
[NLMSGERR_ATTR_OFFS
]) {
103 off
= mnl_attr_get_u32(tb
[NLMSGERR_ATTR_OFFS
]);
105 if (off
> nlh
->nlmsg_len
) {
107 "Invalid offset for NLMSGERR_ATTR_OFFS\n");
109 } else if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
114 return errfn(msg
, off
, err_nlh
);
116 if (msg
&& *msg
!= '\0') {
117 bool is_err
= !!err
->error
;
119 print_ext_ack_msg(is_err
, msg
);
120 return is_err
? 1 : 0;
126 static int nl_dump_ext_ack_done(const struct nlmsghdr
*nlh
, int error
)
128 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
129 unsigned int hlen
= sizeof(int);
130 const char *msg
= NULL
;
132 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
135 if (tb
[NLMSGERR_ATTR_MSG
])
136 msg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
138 if (msg
&& *msg
!= '\0') {
139 bool is_err
= !!error
;
141 print_ext_ack_msg(is_err
, msg
);
142 return is_err
? 1 : 0;
148 #warning "libmnl required for error support"
150 /* No extended error ack without libmnl */
151 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
156 static int nl_dump_ext_ack_done(const struct nlmsghdr
*nlh
, int error
)
162 void rtnl_close(struct rtnl_handle
*rth
)
170 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
177 memset(rth
, 0, sizeof(*rth
));
179 rth
->proto
= protocol
;
180 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
182 perror("Cannot open netlink socket");
186 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_SNDBUF
,
187 &sndbuf
, sizeof(sndbuf
)) < 0) {
192 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_RCVBUF
,
193 &rcvbuf
, sizeof(rcvbuf
)) < 0) {
198 /* Older kernels may no support extended ACK reporting */
199 setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_EXT_ACK
,
202 memset(&rth
->local
, 0, sizeof(rth
->local
));
203 rth
->local
.nl_family
= AF_NETLINK
;
204 rth
->local
.nl_groups
= subscriptions
;
206 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
,
207 sizeof(rth
->local
)) < 0) {
208 perror("Cannot bind netlink socket");
211 addr_len
= sizeof(rth
->local
);
212 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
,
214 perror("Cannot getsockname");
217 if (addr_len
!= sizeof(rth
->local
)) {
218 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
221 if (rth
->local
.nl_family
!= AF_NETLINK
) {
222 fprintf(stderr
, "Wrong address family %d\n",
223 rth
->local
.nl_family
);
226 rth
->seq
= time(NULL
);
230 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
232 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
235 int rtnl_addrdump_req(struct rtnl_handle
*rth
, int family
)
239 struct ifaddrmsg ifm
;
241 .nlh
.nlmsg_len
= sizeof(req
),
242 .nlh
.nlmsg_type
= RTM_GETADDR
,
243 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
244 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
245 .ifm
.ifa_family
= family
,
248 return send(rth
->fd
, &req
, sizeof(req
), 0);
251 int rtnl_addrlbldump_req(struct rtnl_handle
*rth
, int family
)
255 struct ifaddrlblmsg ifal
;
257 .nlh
.nlmsg_len
= sizeof(req
),
258 .nlh
.nlmsg_type
= RTM_GETADDRLABEL
,
259 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
260 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
261 .ifal
.ifal_family
= family
,
264 return send(rth
->fd
, &req
, sizeof(req
), 0);
267 int rtnl_routedump_req(struct rtnl_handle
*rth
, int family
)
273 .nlh
.nlmsg_len
= sizeof(req
),
274 .nlh
.nlmsg_type
= RTM_GETROUTE
,
275 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
276 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
277 .rtm
.rtm_family
= family
,
280 return send(rth
->fd
, &req
, sizeof(req
), 0);
283 int rtnl_ruledump_req(struct rtnl_handle
*rth
, int family
)
287 struct fib_rule_hdr frh
;
289 .nlh
.nlmsg_len
= sizeof(req
),
290 .nlh
.nlmsg_type
= RTM_GETRULE
,
291 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
292 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
296 return send(rth
->fd
, &req
, sizeof(req
), 0);
299 int rtnl_neighdump_req(struct rtnl_handle
*rth
, int family
)
305 .nlh
.nlmsg_len
= sizeof(req
),
306 .nlh
.nlmsg_type
= RTM_GETNEIGH
,
307 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
308 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
309 .ndm
.ndm_family
= family
,
312 return send(rth
->fd
, &req
, sizeof(req
), 0);
315 int rtnl_neightbldump_req(struct rtnl_handle
*rth
, int family
)
319 struct ndtmsg ndtmsg
;
321 .nlh
.nlmsg_len
= sizeof(req
),
322 .nlh
.nlmsg_type
= RTM_GETNEIGHTBL
,
323 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
324 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
325 .ndtmsg
.ndtm_family
= family
,
328 return send(rth
->fd
, &req
, sizeof(req
), 0);
331 int rtnl_mdbdump_req(struct rtnl_handle
*rth
, int family
)
335 struct br_port_msg bpm
;
337 .nlh
.nlmsg_len
= sizeof(req
),
338 .nlh
.nlmsg_type
= RTM_GETMDB
,
339 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
340 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
341 .bpm
.family
= family
,
344 return send(rth
->fd
, &req
, sizeof(req
), 0);
347 int rtnl_netconfdump_req(struct rtnl_handle
*rth
, int family
)
351 struct netconfmsg ncm
;
353 .nlh
.nlmsg_len
= sizeof(req
),
354 .nlh
.nlmsg_type
= RTM_GETNETCONF
,
355 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
356 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
357 .ncm
.ncm_family
= family
,
360 return send(rth
->fd
, &req
, sizeof(req
), 0);
363 int rtnl_nsiddump_req(struct rtnl_handle
*rth
, int family
)
369 .nlh
.nlmsg_len
= sizeof(req
),
370 .nlh
.nlmsg_type
= RTM_GETNSID
,
371 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
372 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
373 .rtm
.rtgen_family
= family
,
376 return send(rth
->fd
, &req
, sizeof(req
), 0);
379 int rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
381 return rtnl_linkdump_req_filter(rth
, family
, RTEXT_FILTER_VF
);
384 int rtnl_linkdump_req_filter(struct rtnl_handle
*rth
, int family
,
389 struct ifinfomsg ifm
;
390 /* attribute has to be NLMSG aligned */
391 struct rtattr ext_req
__attribute__ ((aligned(NLMSG_ALIGNTO
)));
392 __u32 ext_filter_mask
;
394 .nlh
.nlmsg_len
= sizeof(req
),
395 .nlh
.nlmsg_type
= RTM_GETLINK
,
396 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
397 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
398 .ifm
.ifi_family
= family
,
399 .ext_req
.rta_type
= IFLA_EXT_MASK
,
400 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
401 .ext_filter_mask
= filt_mask
,
404 return send(rth
->fd
, &req
, sizeof(req
), 0);
407 int rtnl_linkdump_req_filter_fn(struct rtnl_handle
*rth
, int family
,
408 req_filter_fn_t filter_fn
)
412 struct ifinfomsg ifm
;
415 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
416 .nlh
.nlmsg_type
= RTM_GETLINK
,
417 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
418 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
419 .ifm
.ifi_family
= family
,
426 err
= filter_fn(&req
.nlh
, sizeof(req
));
430 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
433 int rtnl_statsdump_req_filter(struct rtnl_handle
*rth
, int fam
, __u32 filt_mask
)
437 struct if_stats_msg ifsm
;
440 memset(&req
, 0, sizeof(req
));
441 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
442 req
.nlh
.nlmsg_type
= RTM_GETSTATS
;
443 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
444 req
.nlh
.nlmsg_pid
= 0;
445 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
446 req
.ifsm
.family
= fam
;
447 req
.ifsm
.filter_mask
= filt_mask
;
449 return send(rth
->fd
, &req
, sizeof(req
), 0);
452 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
454 return send(rth
->fd
, buf
, len
, 0);
457 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
463 status
= send(rth
->fd
, buf
, len
, 0);
467 /* Check for immediate errors */
468 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
475 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
476 h
= NLMSG_NEXT(h
, status
)) {
477 if (h
->nlmsg_type
== NLMSG_ERROR
) {
478 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
480 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
481 fprintf(stderr
, "ERROR truncated\n");
491 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
493 struct nlmsghdr nlh
= {
494 .nlmsg_len
= NLMSG_LENGTH(len
),
496 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
497 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
499 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
500 struct iovec iov
[2] = {
501 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
502 { .iov_base
= req
, .iov_len
= len
}
504 struct msghdr msg
= {
506 .msg_namelen
= sizeof(nladdr
),
511 return sendmsg(rth
->fd
, &msg
, 0);
514 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
516 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
519 .iov_len
= n
->nlmsg_len
521 struct msghdr msg
= {
523 .msg_namelen
= sizeof(nladdr
),
528 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
530 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
532 return sendmsg(rth
->fd
, &msg
, 0);
535 static int rtnl_dump_done(struct nlmsghdr
*h
)
537 int len
= *(int *)NLMSG_DATA(h
);
539 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
540 fprintf(stderr
, "DONE truncated\n");
545 /* check for any messages returned from kernel */
546 if (nl_dump_ext_ack_done(h
, len
))
556 "Error: Buffer too small for object.\n");
559 perror("RTNETLINK answers");
564 /* check for any messages returned from kernel */
565 nl_dump_ext_ack(h
, NULL
);
570 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
574 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
575 fprintf(stderr
, "ERROR truncated\n");
577 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
580 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
582 errno
== EOPNOTSUPP
))
585 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
586 perror("RTNETLINK answers");
590 static int __rtnl_recvmsg(int fd
, struct msghdr
*msg
, int flags
)
595 len
= recvmsg(fd
, msg
, flags
);
596 } while (len
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
599 fprintf(stderr
, "netlink receive error %s (%d)\n",
600 strerror(errno
), errno
);
605 fprintf(stderr
, "EOF on netlink\n");
612 static int rtnl_recvmsg(int fd
, struct msghdr
*msg
, char **answer
)
614 struct iovec
*iov
= msg
->msg_iov
;
618 iov
->iov_base
= NULL
;
621 len
= __rtnl_recvmsg(fd
, msg
, MSG_PEEK
| MSG_TRUNC
);
627 fprintf(stderr
, "malloc error: not enough buffer\n");
634 len
= __rtnl_recvmsg(fd
, msg
, 0);
648 static int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
649 const struct rtnl_dump_filter_arg
*arg
)
651 struct sockaddr_nl nladdr
;
653 struct msghdr msg
= {
655 .msg_namelen
= sizeof(nladdr
),
664 const struct rtnl_dump_filter_arg
*a
;
668 status
= rtnl_recvmsg(rth
->fd
, &msg
, &buf
);
673 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
675 for (a
= arg
; a
->filter
; a
++) {
676 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
680 while (NLMSG_OK(h
, msglen
)) {
683 h
->nlmsg_flags
&= ~a
->nc_flags
;
685 if (nladdr
.nl_pid
!= 0 ||
686 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
687 h
->nlmsg_seq
!= rth
->dump
)
690 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
693 if (h
->nlmsg_type
== NLMSG_DONE
) {
694 err
= rtnl_dump_done(h
);
701 break; /* process next filter */
704 if (h
->nlmsg_type
== NLMSG_ERROR
) {
705 rtnl_dump_error(rth
, h
);
711 err
= a
->filter(h
, a
->arg1
);
719 h
= NLMSG_NEXT(h
, msglen
);
727 "Dump was interrupted and may be inconsistent.\n");
731 if (msg
.msg_flags
& MSG_TRUNC
) {
732 fprintf(stderr
, "Message truncated\n");
736 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
742 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
743 rtnl_filter_t filter
,
744 void *arg1
, __u16 nc_flags
)
746 const struct rtnl_dump_filter_arg a
[2] = {
747 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
748 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
751 return rtnl_dump_filter_l(rth
, a
);
754 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
755 nl_ext_ack_fn_t errfn
)
757 if (nl_dump_ext_ack(h
, errfn
))
760 fprintf(stderr
, "RTNETLINK answers: %s\n",
761 strerror(-err
->error
));
765 static int __rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iov
,
766 size_t iovlen
, struct nlmsghdr
**answer
,
767 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
769 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
771 struct msghdr msg
= {
773 .msg_namelen
= sizeof(nladdr
),
775 .msg_iovlen
= iovlen
,
777 unsigned int seq
= 0;
782 for (i
= 0; i
< iovlen
; i
++) {
784 h
->nlmsg_seq
= seq
= ++rtnl
->seq
;
786 h
->nlmsg_flags
|= NLM_F_ACK
;
789 status
= sendmsg(rtnl
->fd
, &msg
, 0);
791 perror("Cannot talk to rtnetlink");
795 /* change msg to use the response iov */
801 status
= rtnl_recvmsg(rtnl
->fd
, &msg
, &buf
);
807 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
809 "sender address length == %d\n",
813 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
814 int len
= h
->nlmsg_len
;
815 int l
= len
- sizeof(*h
);
817 if (l
< 0 || len
> status
) {
818 if (msg
.msg_flags
& MSG_TRUNC
) {
819 fprintf(stderr
, "Truncated message\n");
824 "!!!malformed message: len=%d\n",
829 if (nladdr
.nl_pid
!= 0 ||
830 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
831 h
->nlmsg_seq
> seq
|| h
->nlmsg_seq
< seq
- iovlen
) {
832 /* Don't forget to skip that message. */
833 status
-= NLMSG_ALIGN(len
);
834 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
838 if (h
->nlmsg_type
== NLMSG_ERROR
) {
839 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
840 int error
= err
->error
;
842 if (l
< sizeof(struct nlmsgerr
)) {
843 fprintf(stderr
, "ERROR truncated\n");
849 /* check messages from kernel */
850 nl_dump_ext_ack(h
, errfn
);
854 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
856 rtnl_talk_error(h
, err
, errfn
);
860 *answer
= (struct nlmsghdr
*)buf
;
866 return error
? -i
: 0;
870 *answer
= (struct nlmsghdr
*)buf
;
874 fprintf(stderr
, "Unexpected reply!!!\n");
876 status
-= NLMSG_ALIGN(len
);
877 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
881 if (msg
.msg_flags
& MSG_TRUNC
) {
882 fprintf(stderr
, "Message truncated\n");
887 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
893 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
894 struct nlmsghdr
**answer
,
895 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
899 .iov_len
= n
->nlmsg_len
902 return __rtnl_talk_iov(rtnl
, &iov
, 1, answer
, show_rtnl_err
, errfn
);
905 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
906 struct nlmsghdr
**answer
)
908 return __rtnl_talk(rtnl
, n
, answer
, true, NULL
);
911 int rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iovec
, size_t iovlen
,
912 struct nlmsghdr
**answer
)
914 return __rtnl_talk_iov(rtnl
, iovec
, iovlen
, answer
, true, NULL
);
917 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
918 struct nlmsghdr
**answer
)
920 return __rtnl_talk(rtnl
, n
, answer
, false, NULL
);
923 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
927 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
929 perror("NETLINK_LISTEN_ALL_NSID");
932 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
936 int rtnl_listen(struct rtnl_handle
*rtnl
,
937 rtnl_listen_filter_t handler
,
942 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
944 struct msghdr msg
= {
946 .msg_namelen
= sizeof(nladdr
),
951 char cmsgbuf
[BUFSIZ
];
953 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
954 msg
.msg_control
= &cmsgbuf
;
955 msg
.msg_controllen
= sizeof(cmsgbuf
);
960 struct rtnl_ctrl_data ctrl
;
961 struct cmsghdr
*cmsg
;
963 iov
.iov_len
= sizeof(buf
);
964 status
= recvmsg(rtnl
->fd
, &msg
, 0);
967 if (errno
== EINTR
|| errno
== EAGAIN
)
969 fprintf(stderr
, "netlink receive error %s (%d)\n",
970 strerror(errno
), errno
);
971 if (errno
== ENOBUFS
)
976 fprintf(stderr
, "EOF on netlink\n");
979 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
981 "Sender address length == %d\n",
986 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
987 memset(&ctrl
, 0, sizeof(ctrl
));
989 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
990 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
991 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
992 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
993 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
994 int *data
= (int *)CMSG_DATA(cmsg
);
1000 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
1002 int len
= h
->nlmsg_len
;
1003 int l
= len
- sizeof(*h
);
1005 if (l
< 0 || len
> status
) {
1006 if (msg
.msg_flags
& MSG_TRUNC
) {
1007 fprintf(stderr
, "Truncated message\n");
1011 "!!!malformed message: len=%d\n",
1016 err
= handler(&ctrl
, h
, jarg
);
1020 status
-= NLMSG_ALIGN(len
);
1021 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
1023 if (msg
.msg_flags
& MSG_TRUNC
) {
1024 fprintf(stderr
, "Message truncated\n");
1028 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
1034 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
1039 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
1045 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
1050 perror("rtnl_from_file: fread");
1057 l
= len
- sizeof(*h
);
1059 if (l
< 0 || len
> sizeof(buf
)) {
1060 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
1065 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
1068 perror("rtnl_from_file: fread");
1072 fprintf(stderr
, "rtnl-from_file: truncated message\n");
1076 err
= handler(NULL
, h
, jarg
);
1082 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
1084 return addattr_l(n
, maxlen
, type
, NULL
, 0);
1087 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
1089 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
1092 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
1094 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
1097 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
1099 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
1102 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
1104 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
1107 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
1109 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
1112 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
1115 int len
= RTA_LENGTH(alen
);
1118 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
1120 "addattr_l ERROR: message exceeded bound of %d\n",
1124 rta
= NLMSG_TAIL(n
);
1125 rta
->rta_type
= type
;
1128 memcpy(RTA_DATA(rta
), data
, alen
);
1129 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
1133 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
1135 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
1137 "addraw_l ERROR: message exceeded bound of %d\n",
1142 memcpy(NLMSG_TAIL(n
), data
, len
);
1143 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
1144 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
1148 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
1150 struct rtattr
*nest
= NLMSG_TAIL(n
);
1152 addattr_l(n
, maxlen
, type
, NULL
, 0);
1156 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
1158 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
1159 return n
->nlmsg_len
;
1162 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
1163 const void *data
, int len
)
1165 struct rtattr
*start
= NLMSG_TAIL(n
);
1167 addattr_l(n
, maxlen
, type
, data
, len
);
1168 addattr_nest(n
, maxlen
, type
);
1172 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
1174 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
1176 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
1177 addattr_nest_end(n
, nest
);
1178 return n
->nlmsg_len
;
1181 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
1183 int len
= RTA_LENGTH(4);
1184 struct rtattr
*subrta
;
1186 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
1188 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1192 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1193 subrta
->rta_type
= type
;
1194 subrta
->rta_len
= len
;
1195 memcpy(RTA_DATA(subrta
), &data
, 4);
1196 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
1200 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
1201 const void *data
, int alen
)
1203 struct rtattr
*subrta
;
1204 int len
= RTA_LENGTH(alen
);
1206 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
1208 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1212 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1213 subrta
->rta_type
= type
;
1214 subrta
->rta_len
= len
;
1216 memcpy(RTA_DATA(subrta
), data
, alen
);
1217 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
1221 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
1223 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
1226 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
1228 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
1231 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
1233 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
1236 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
1238 struct rtattr
*nest
= RTA_TAIL(rta
);
1240 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
1245 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
1247 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
1249 return rta
->rta_len
;
1252 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
1254 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
1257 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
1258 int len
, unsigned short flags
)
1260 unsigned short type
;
1262 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1263 while (RTA_OK(rta
, len
)) {
1264 type
= rta
->rta_type
& ~flags
;
1265 if ((type
<= max
) && (!tb
[type
]))
1267 rta
= RTA_NEXT(rta
, len
);
1270 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1275 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1277 while (RTA_OK(rta
, len
)) {
1278 if (rta
->rta_type
== type
)
1280 rta
= RTA_NEXT(rta
, len
);
1284 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1289 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1293 if (RTA_PAYLOAD(rta
) < len
)
1295 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1296 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1297 return parse_rtattr_nested(tb
, max
, rta
);
1299 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));