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"
31 #define __aligned(x) __attribute__((aligned(x)))
34 #define SOL_NETLINK 270
38 #define MIN(a, b) ((a) < (b) ? (a) : (b))
41 int rcvbuf
= 1024 * 1024;
44 #include <libmnl/libmnl.h>
46 static const enum mnl_attr_data_type extack_policy
[NLMSGERR_ATTR_MAX
+ 1] = {
47 [NLMSGERR_ATTR_MSG
] = MNL_TYPE_NUL_STRING
,
48 [NLMSGERR_ATTR_OFFS
] = MNL_TYPE_U32
,
51 static int err_attr_cb(const struct nlattr
*attr
, void *data
)
53 const struct nlattr
**tb
= data
;
56 if (mnl_attr_type_valid(attr
, NLMSGERR_ATTR_MAX
) < 0) {
57 fprintf(stderr
, "Invalid extack attribute\n");
61 type
= mnl_attr_get_type(attr
);
62 if (mnl_attr_validate(attr
, extack_policy
[type
]) < 0) {
63 fprintf(stderr
, "extack attribute %d failed validation\n",
72 static void print_ext_ack_msg(bool is_err
, const char *msg
)
74 fprintf(stderr
, "%s: %s", is_err
? "Error" : "Warning", msg
);
75 if (msg
[strlen(msg
) - 1] != '.')
77 fprintf(stderr
, "\n");
80 /* dump netlink extended ack error message */
81 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
83 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
84 const struct nlmsgerr
*err
= mnl_nlmsg_get_payload(nlh
);
85 const struct nlmsghdr
*err_nlh
= NULL
;
86 unsigned int hlen
= sizeof(*err
);
87 const char *msg
= NULL
;
90 /* no TLVs, nothing to do here */
91 if (!(nlh
->nlmsg_flags
& NLM_F_ACK_TLVS
))
94 /* if NLM_F_CAPPED is set then the inner err msg was capped */
95 if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
96 hlen
+= mnl_nlmsg_get_payload_len(&err
->msg
);
98 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
101 if (tb
[NLMSGERR_ATTR_MSG
])
102 msg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
104 if (tb
[NLMSGERR_ATTR_OFFS
]) {
105 off
= mnl_attr_get_u32(tb
[NLMSGERR_ATTR_OFFS
]);
107 if (off
> nlh
->nlmsg_len
) {
109 "Invalid offset for NLMSGERR_ATTR_OFFS\n");
111 } else if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
116 return errfn(msg
, off
, err_nlh
);
118 if (msg
&& *msg
!= '\0') {
119 bool is_err
= !!err
->error
;
121 print_ext_ack_msg(is_err
, msg
);
122 return is_err
? 1 : 0;
128 static int nl_dump_ext_ack_done(const struct nlmsghdr
*nlh
, int error
)
130 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
131 unsigned int hlen
= sizeof(int);
132 const char *msg
= NULL
;
134 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
137 if (tb
[NLMSGERR_ATTR_MSG
])
138 msg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
140 if (msg
&& *msg
!= '\0') {
141 bool is_err
= !!error
;
143 print_ext_ack_msg(is_err
, msg
);
144 return is_err
? 1 : 0;
150 #warning "libmnl required for error support"
152 /* No extended error ack without libmnl */
153 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
158 static int nl_dump_ext_ack_done(const struct nlmsghdr
*nlh
, int error
)
164 void rtnl_close(struct rtnl_handle
*rth
)
172 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
179 memset(rth
, 0, sizeof(*rth
));
181 rth
->proto
= protocol
;
182 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
184 perror("Cannot open netlink socket");
188 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_SNDBUF
,
189 &sndbuf
, sizeof(sndbuf
)) < 0) {
194 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_RCVBUF
,
195 &rcvbuf
, sizeof(rcvbuf
)) < 0) {
200 /* Older kernels may no support extended ACK reporting */
201 setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_EXT_ACK
,
204 memset(&rth
->local
, 0, sizeof(rth
->local
));
205 rth
->local
.nl_family
= AF_NETLINK
;
206 rth
->local
.nl_groups
= subscriptions
;
208 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
,
209 sizeof(rth
->local
)) < 0) {
210 perror("Cannot bind netlink socket");
213 addr_len
= sizeof(rth
->local
);
214 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
,
216 perror("Cannot getsockname");
219 if (addr_len
!= sizeof(rth
->local
)) {
220 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
223 if (rth
->local
.nl_family
!= AF_NETLINK
) {
224 fprintf(stderr
, "Wrong address family %d\n",
225 rth
->local
.nl_family
);
228 rth
->seq
= time(NULL
);
232 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
234 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
237 int rtnl_addrdump_req(struct rtnl_handle
*rth
, int family
)
241 struct ifaddrmsg ifm
;
243 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
244 .nlh
.nlmsg_type
= RTM_GETADDR
,
245 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
246 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
247 .ifm
.ifa_family
= family
,
250 return send(rth
->fd
, &req
, sizeof(req
), 0);
253 int rtnl_addrlbldump_req(struct rtnl_handle
*rth
, int family
)
257 struct ifaddrlblmsg ifal
;
259 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrlblmsg
)),
260 .nlh
.nlmsg_type
= RTM_GETADDRLABEL
,
261 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
262 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
263 .ifal
.ifal_family
= family
,
266 return send(rth
->fd
, &req
, sizeof(req
), 0);
269 int rtnl_routedump_req(struct rtnl_handle
*rth
, int family
,
270 req_filter_fn_t filter_fn
)
277 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
)),
278 .nlh
.nlmsg_type
= RTM_GETROUTE
,
279 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
280 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
281 .rtm
.rtm_family
= family
,
287 err
= filter_fn(&req
.nlh
, sizeof(req
));
292 return send(rth
->fd
, &req
, sizeof(req
), 0);
295 int rtnl_ruledump_req(struct rtnl_handle
*rth
, int family
)
299 struct fib_rule_hdr frh
;
301 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct fib_rule_hdr
)),
302 .nlh
.nlmsg_type
= RTM_GETRULE
,
303 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
304 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
308 return send(rth
->fd
, &req
, sizeof(req
), 0);
311 int rtnl_neighdump_req(struct rtnl_handle
*rth
, int family
)
317 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
)),
318 .nlh
.nlmsg_type
= RTM_GETNEIGH
,
319 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
320 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
321 .ndm
.ndm_family
= family
,
324 return send(rth
->fd
, &req
, sizeof(req
), 0);
327 int rtnl_neightbldump_req(struct rtnl_handle
*rth
, int family
)
331 struct ndtmsg ndtmsg
;
333 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndtmsg
)),
334 .nlh
.nlmsg_type
= RTM_GETNEIGHTBL
,
335 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
336 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
337 .ndtmsg
.ndtm_family
= family
,
340 return send(rth
->fd
, &req
, sizeof(req
), 0);
343 int rtnl_mdbdump_req(struct rtnl_handle
*rth
, int family
)
347 struct br_port_msg bpm
;
349 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct br_port_msg
)),
350 .nlh
.nlmsg_type
= RTM_GETMDB
,
351 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
352 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
353 .bpm
.family
= family
,
356 return send(rth
->fd
, &req
, sizeof(req
), 0);
359 int rtnl_netconfdump_req(struct rtnl_handle
*rth
, int family
)
363 struct netconfmsg ncm
;
364 char buf
[0] __aligned(NLMSG_ALIGNTO
);
366 .nlh
.nlmsg_len
= NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct netconfmsg
))),
367 .nlh
.nlmsg_type
= RTM_GETNETCONF
,
368 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
369 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
370 .ncm
.ncm_family
= family
,
373 return send(rth
->fd
, &req
, sizeof(req
), 0);
376 int rtnl_nsiddump_req(struct rtnl_handle
*rth
, int family
)
381 char buf
[0] __aligned(NLMSG_ALIGNTO
);
383 .nlh
.nlmsg_len
= NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct rtgenmsg
))),
384 .nlh
.nlmsg_type
= RTM_GETNSID
,
385 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
386 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
387 .rtm
.rtgen_family
= family
,
390 return send(rth
->fd
, &req
, sizeof(req
), 0);
393 static int __rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
397 struct ifinfomsg ifm
;
399 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
400 .nlh
.nlmsg_type
= RTM_GETLINK
,
401 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
402 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
403 .ifm
.ifi_family
= family
,
406 return send(rth
->fd
, &req
, sizeof(req
), 0);
409 int rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
411 if (family
== AF_UNSPEC
)
412 return rtnl_linkdump_req_filter(rth
, family
, RTEXT_FILTER_VF
);
414 return __rtnl_linkdump_req(rth
, family
);
417 int rtnl_linkdump_req_filter(struct rtnl_handle
*rth
, int family
,
420 if (family
== AF_UNSPEC
) {
423 struct ifinfomsg ifm
;
424 /* attribute has to be NLMSG aligned */
425 struct rtattr ext_req
__aligned(NLMSG_ALIGNTO
);
426 __u32 ext_filter_mask
;
428 .nlh
.nlmsg_len
= sizeof(req
),
429 .nlh
.nlmsg_type
= RTM_GETLINK
,
430 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
431 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
432 .ifm
.ifi_family
= family
,
433 .ext_req
.rta_type
= IFLA_EXT_MASK
,
434 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
435 .ext_filter_mask
= filt_mask
,
438 return send(rth
->fd
, &req
, sizeof(req
), 0);
441 return __rtnl_linkdump_req(rth
, family
);
444 int rtnl_linkdump_req_filter_fn(struct rtnl_handle
*rth
, int family
,
445 req_filter_fn_t filter_fn
)
447 if (family
== AF_UNSPEC
) {
450 struct ifinfomsg ifm
;
453 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
454 .nlh
.nlmsg_type
= RTM_GETLINK
,
455 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
456 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
457 .ifm
.ifi_family
= family
,
464 err
= filter_fn(&req
.nlh
, sizeof(req
));
468 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
471 return __rtnl_linkdump_req(rth
, family
);
474 int rtnl_statsdump_req_filter(struct rtnl_handle
*rth
, int fam
, __u32 filt_mask
)
478 struct if_stats_msg ifsm
;
481 memset(&req
, 0, sizeof(req
));
482 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
483 req
.nlh
.nlmsg_type
= RTM_GETSTATS
;
484 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
485 req
.nlh
.nlmsg_pid
= 0;
486 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
487 req
.ifsm
.family
= fam
;
488 req
.ifsm
.filter_mask
= filt_mask
;
490 return send(rth
->fd
, &req
, sizeof(req
), 0);
493 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
495 return send(rth
->fd
, buf
, len
, 0);
498 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
504 status
= send(rth
->fd
, buf
, len
, 0);
508 /* Check for immediate errors */
509 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
516 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
517 h
= NLMSG_NEXT(h
, status
)) {
518 if (h
->nlmsg_type
== NLMSG_ERROR
) {
519 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
521 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
522 fprintf(stderr
, "ERROR truncated\n");
532 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
534 struct nlmsghdr nlh
= {
535 .nlmsg_len
= NLMSG_LENGTH(len
),
537 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
538 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
540 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
541 struct iovec iov
[2] = {
542 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
543 { .iov_base
= req
, .iov_len
= len
}
545 struct msghdr msg
= {
547 .msg_namelen
= sizeof(nladdr
),
552 return sendmsg(rth
->fd
, &msg
, 0);
555 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
557 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
560 .iov_len
= n
->nlmsg_len
562 struct msghdr msg
= {
564 .msg_namelen
= sizeof(nladdr
),
569 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
571 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
573 return sendmsg(rth
->fd
, &msg
, 0);
576 static int rtnl_dump_done(struct nlmsghdr
*h
)
578 int len
= *(int *)NLMSG_DATA(h
);
580 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
581 fprintf(stderr
, "DONE truncated\n");
586 /* check for any messages returned from kernel */
587 if (nl_dump_ext_ack_done(h
, len
))
597 "Error: Buffer too small for object.\n");
600 perror("RTNETLINK answers");
605 /* check for any messages returned from kernel */
606 nl_dump_ext_ack(h
, NULL
);
611 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
615 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
616 fprintf(stderr
, "ERROR truncated\n");
618 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
621 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
623 errno
== EOPNOTSUPP
))
626 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
627 perror("RTNETLINK answers");
631 static int __rtnl_recvmsg(int fd
, struct msghdr
*msg
, int flags
)
636 len
= recvmsg(fd
, msg
, flags
);
637 } while (len
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
640 fprintf(stderr
, "netlink receive error %s (%d)\n",
641 strerror(errno
), errno
);
646 fprintf(stderr
, "EOF on netlink\n");
653 static int rtnl_recvmsg(int fd
, struct msghdr
*msg
, char **answer
)
655 struct iovec
*iov
= msg
->msg_iov
;
659 iov
->iov_base
= NULL
;
662 len
= __rtnl_recvmsg(fd
, msg
, MSG_PEEK
| MSG_TRUNC
);
668 fprintf(stderr
, "malloc error: not enough buffer\n");
675 len
= __rtnl_recvmsg(fd
, msg
, 0);
689 static int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
690 const struct rtnl_dump_filter_arg
*arg
)
692 struct sockaddr_nl nladdr
;
694 struct msghdr msg
= {
696 .msg_namelen
= sizeof(nladdr
),
705 const struct rtnl_dump_filter_arg
*a
;
709 status
= rtnl_recvmsg(rth
->fd
, &msg
, &buf
);
714 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
716 for (a
= arg
; a
->filter
; a
++) {
717 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
721 while (NLMSG_OK(h
, msglen
)) {
724 h
->nlmsg_flags
&= ~a
->nc_flags
;
726 if (nladdr
.nl_pid
!= 0 ||
727 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
728 h
->nlmsg_seq
!= rth
->dump
)
731 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
734 if (h
->nlmsg_type
== NLMSG_DONE
) {
735 err
= rtnl_dump_done(h
);
742 break; /* process next filter */
745 if (h
->nlmsg_type
== NLMSG_ERROR
) {
746 rtnl_dump_error(rth
, h
);
752 err
= a
->filter(h
, a
->arg1
);
760 h
= NLMSG_NEXT(h
, msglen
);
768 "Dump was interrupted and may be inconsistent.\n");
772 if (msg
.msg_flags
& MSG_TRUNC
) {
773 fprintf(stderr
, "Message truncated\n");
777 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
783 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
784 rtnl_filter_t filter
,
785 void *arg1
, __u16 nc_flags
)
787 const struct rtnl_dump_filter_arg a
[2] = {
788 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
789 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
792 return rtnl_dump_filter_l(rth
, a
);
795 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
796 nl_ext_ack_fn_t errfn
)
798 if (nl_dump_ext_ack(h
, errfn
))
801 fprintf(stderr
, "RTNETLINK answers: %s\n",
802 strerror(-err
->error
));
806 static int __rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iov
,
807 size_t iovlen
, struct nlmsghdr
**answer
,
808 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
810 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
812 struct msghdr msg
= {
814 .msg_namelen
= sizeof(nladdr
),
816 .msg_iovlen
= iovlen
,
818 unsigned int seq
= 0;
823 for (i
= 0; i
< iovlen
; i
++) {
825 h
->nlmsg_seq
= seq
= ++rtnl
->seq
;
827 h
->nlmsg_flags
|= NLM_F_ACK
;
830 status
= sendmsg(rtnl
->fd
, &msg
, 0);
832 perror("Cannot talk to rtnetlink");
836 /* change msg to use the response iov */
842 status
= rtnl_recvmsg(rtnl
->fd
, &msg
, &buf
);
848 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
850 "sender address length == %d\n",
854 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
855 int len
= h
->nlmsg_len
;
856 int l
= len
- sizeof(*h
);
858 if (l
< 0 || len
> status
) {
859 if (msg
.msg_flags
& MSG_TRUNC
) {
860 fprintf(stderr
, "Truncated message\n");
865 "!!!malformed message: len=%d\n",
870 if (nladdr
.nl_pid
!= 0 ||
871 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
872 h
->nlmsg_seq
> seq
|| h
->nlmsg_seq
< seq
- iovlen
) {
873 /* Don't forget to skip that message. */
874 status
-= NLMSG_ALIGN(len
);
875 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
879 if (h
->nlmsg_type
== NLMSG_ERROR
) {
880 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
881 int error
= err
->error
;
883 if (l
< sizeof(struct nlmsgerr
)) {
884 fprintf(stderr
, "ERROR truncated\n");
890 /* check messages from kernel */
891 nl_dump_ext_ack(h
, errfn
);
895 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
897 rtnl_talk_error(h
, err
, errfn
);
901 *answer
= (struct nlmsghdr
*)buf
;
907 return error
? -i
: 0;
911 *answer
= (struct nlmsghdr
*)buf
;
915 fprintf(stderr
, "Unexpected reply!!!\n");
917 status
-= NLMSG_ALIGN(len
);
918 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
922 if (msg
.msg_flags
& MSG_TRUNC
) {
923 fprintf(stderr
, "Message truncated\n");
928 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
934 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
935 struct nlmsghdr
**answer
,
936 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
940 .iov_len
= n
->nlmsg_len
943 return __rtnl_talk_iov(rtnl
, &iov
, 1, answer
, show_rtnl_err
, errfn
);
946 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
947 struct nlmsghdr
**answer
)
949 return __rtnl_talk(rtnl
, n
, answer
, true, NULL
);
952 int rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iovec
, size_t iovlen
,
953 struct nlmsghdr
**answer
)
955 return __rtnl_talk_iov(rtnl
, iovec
, iovlen
, answer
, true, NULL
);
958 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
959 struct nlmsghdr
**answer
)
961 return __rtnl_talk(rtnl
, n
, answer
, false, NULL
);
964 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
968 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
970 perror("NETLINK_LISTEN_ALL_NSID");
973 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
977 int rtnl_listen(struct rtnl_handle
*rtnl
,
978 rtnl_listen_filter_t handler
,
983 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
985 struct msghdr msg
= {
987 .msg_namelen
= sizeof(nladdr
),
992 char cmsgbuf
[BUFSIZ
];
994 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
995 msg
.msg_control
= &cmsgbuf
;
996 msg
.msg_controllen
= sizeof(cmsgbuf
);
1001 struct rtnl_ctrl_data ctrl
;
1002 struct cmsghdr
*cmsg
;
1004 iov
.iov_len
= sizeof(buf
);
1005 status
= recvmsg(rtnl
->fd
, &msg
, 0);
1008 if (errno
== EINTR
|| errno
== EAGAIN
)
1010 fprintf(stderr
, "netlink receive error %s (%d)\n",
1011 strerror(errno
), errno
);
1012 if (errno
== ENOBUFS
)
1017 fprintf(stderr
, "EOF on netlink\n");
1020 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
1022 "Sender address length == %d\n",
1027 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
1028 memset(&ctrl
, 0, sizeof(ctrl
));
1030 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
1031 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
1032 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
1033 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
1034 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
1035 int *data
= (int *)CMSG_DATA(cmsg
);
1041 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
1043 int len
= h
->nlmsg_len
;
1044 int l
= len
- sizeof(*h
);
1046 if (l
< 0 || len
> status
) {
1047 if (msg
.msg_flags
& MSG_TRUNC
) {
1048 fprintf(stderr
, "Truncated message\n");
1052 "!!!malformed message: len=%d\n",
1057 err
= handler(&ctrl
, h
, jarg
);
1061 status
-= NLMSG_ALIGN(len
);
1062 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
1064 if (msg
.msg_flags
& MSG_TRUNC
) {
1065 fprintf(stderr
, "Message truncated\n");
1069 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
1075 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
1080 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
1086 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
1091 perror("rtnl_from_file: fread");
1098 l
= len
- sizeof(*h
);
1100 if (l
< 0 || len
> sizeof(buf
)) {
1101 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
1106 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
1109 perror("rtnl_from_file: fread");
1113 fprintf(stderr
, "rtnl-from_file: truncated message\n");
1117 err
= handler(NULL
, h
, jarg
);
1123 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
1125 return addattr_l(n
, maxlen
, type
, NULL
, 0);
1128 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
1130 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
1133 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
1135 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
1138 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
1140 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
1143 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
1145 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
1148 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
1150 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
1153 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
1156 int len
= RTA_LENGTH(alen
);
1159 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
1161 "addattr_l ERROR: message exceeded bound of %d\n",
1165 rta
= NLMSG_TAIL(n
);
1166 rta
->rta_type
= type
;
1169 memcpy(RTA_DATA(rta
), data
, alen
);
1170 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
1174 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
1176 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
1178 "addraw_l ERROR: message exceeded bound of %d\n",
1183 memcpy(NLMSG_TAIL(n
), data
, len
);
1184 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
1185 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
1189 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
1191 struct rtattr
*nest
= NLMSG_TAIL(n
);
1193 addattr_l(n
, maxlen
, type
, NULL
, 0);
1197 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
1199 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
1200 return n
->nlmsg_len
;
1203 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
1204 const void *data
, int len
)
1206 struct rtattr
*start
= NLMSG_TAIL(n
);
1208 addattr_l(n
, maxlen
, type
, data
, len
);
1209 addattr_nest(n
, maxlen
, type
);
1213 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
1215 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
1217 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
1218 addattr_nest_end(n
, nest
);
1219 return n
->nlmsg_len
;
1222 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
1224 int len
= RTA_LENGTH(4);
1225 struct rtattr
*subrta
;
1227 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
1229 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1233 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1234 subrta
->rta_type
= type
;
1235 subrta
->rta_len
= len
;
1236 memcpy(RTA_DATA(subrta
), &data
, 4);
1237 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
1241 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
1242 const void *data
, int alen
)
1244 struct rtattr
*subrta
;
1245 int len
= RTA_LENGTH(alen
);
1247 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
1249 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1253 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1254 subrta
->rta_type
= type
;
1255 subrta
->rta_len
= len
;
1257 memcpy(RTA_DATA(subrta
), data
, alen
);
1258 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
1262 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
1264 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
1267 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
1269 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
1272 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
1274 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
1277 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
1279 struct rtattr
*nest
= RTA_TAIL(rta
);
1281 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
1286 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
1288 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
1290 return rta
->rta_len
;
1293 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
1295 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
1298 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
1299 int len
, unsigned short flags
)
1301 unsigned short type
;
1303 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1304 while (RTA_OK(rta
, len
)) {
1305 type
= rta
->rta_type
& ~flags
;
1306 if ((type
<= max
) && (!tb
[type
]))
1308 rta
= RTA_NEXT(rta
, len
);
1311 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1316 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1318 while (RTA_OK(rta
, len
)) {
1319 if (rta
->rta_type
== type
)
1321 rta
= RTA_NEXT(rta
, len
);
1325 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1330 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1334 if (RTA_PAYLOAD(rta
) < len
)
1336 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1337 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1338 return parse_rtattr_nested(tb
, max
, rta
);
1340 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));