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>
28 #include <linux/nexthop.h>
30 #include "libnetlink.h"
32 #define __aligned(x) __attribute__((aligned(x)))
35 #define SOL_NETLINK 270
39 #define MIN(a, b) ((a) < (b) ? (a) : (b))
42 int rcvbuf
= 1024 * 1024;
45 #include <libmnl/libmnl.h>
47 static const enum mnl_attr_data_type extack_policy
[NLMSGERR_ATTR_MAX
+ 1] = {
48 [NLMSGERR_ATTR_MSG
] = MNL_TYPE_NUL_STRING
,
49 [NLMSGERR_ATTR_OFFS
] = MNL_TYPE_U32
,
52 static int err_attr_cb(const struct nlattr
*attr
, void *data
)
54 const struct nlattr
**tb
= data
;
57 if (mnl_attr_type_valid(attr
, NLMSGERR_ATTR_MAX
) < 0) {
58 fprintf(stderr
, "Invalid extack attribute\n");
62 type
= mnl_attr_get_type(attr
);
63 if (mnl_attr_validate(attr
, extack_policy
[type
]) < 0) {
64 fprintf(stderr
, "extack attribute %d failed validation\n",
73 static void print_ext_ack_msg(bool is_err
, const char *msg
)
75 fprintf(stderr
, "%s: %s", is_err
? "Error" : "Warning", msg
);
76 if (msg
[strlen(msg
) - 1] != '.')
78 fprintf(stderr
, "\n");
81 /* dump netlink extended ack error message */
82 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
84 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
85 const struct nlmsgerr
*err
= mnl_nlmsg_get_payload(nlh
);
86 const struct nlmsghdr
*err_nlh
= NULL
;
87 unsigned int hlen
= sizeof(*err
);
88 const char *msg
= NULL
;
91 /* no TLVs, nothing to do here */
92 if (!(nlh
->nlmsg_flags
& NLM_F_ACK_TLVS
))
95 /* if NLM_F_CAPPED is set then the inner err msg was capped */
96 if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
97 hlen
+= mnl_nlmsg_get_payload_len(&err
->msg
);
99 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
102 if (tb
[NLMSGERR_ATTR_MSG
])
103 msg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
105 if (tb
[NLMSGERR_ATTR_OFFS
]) {
106 off
= mnl_attr_get_u32(tb
[NLMSGERR_ATTR_OFFS
]);
108 if (off
> nlh
->nlmsg_len
) {
110 "Invalid offset for NLMSGERR_ATTR_OFFS\n");
112 } else if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
117 return errfn(msg
, off
, err_nlh
);
119 if (msg
&& *msg
!= '\0') {
120 bool is_err
= !!err
->error
;
122 print_ext_ack_msg(is_err
, msg
);
123 return is_err
? 1 : 0;
129 static int nl_dump_ext_ack_done(const struct nlmsghdr
*nlh
, int error
)
131 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
132 unsigned int hlen
= sizeof(int);
133 const char *msg
= NULL
;
135 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
138 if (tb
[NLMSGERR_ATTR_MSG
])
139 msg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
141 if (msg
&& *msg
!= '\0') {
142 bool is_err
= !!error
;
144 print_ext_ack_msg(is_err
, msg
);
145 return is_err
? 1 : 0;
151 #warning "libmnl required for error support"
153 /* No extended error ack without libmnl */
154 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
159 static int nl_dump_ext_ack_done(const struct nlmsghdr
*nlh
, int error
)
165 /* Older kernels may not support strict dump and filtering */
166 void rtnl_set_strict_dump(struct rtnl_handle
*rth
)
170 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_GET_STRICT_CHK
,
171 &one
, sizeof(one
)) < 0)
174 rth
->flags
|= RTNL_HANDLE_F_STRICT_CHK
;
177 int rtnl_add_nl_group(struct rtnl_handle
*rth
, unsigned int group
)
179 return setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_ADD_MEMBERSHIP
,
180 &group
, sizeof(group
));
183 void rtnl_close(struct rtnl_handle
*rth
)
191 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
198 memset(rth
, 0, sizeof(*rth
));
200 rth
->proto
= protocol
;
201 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
203 perror("Cannot open netlink socket");
207 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_SNDBUF
,
208 &sndbuf
, sizeof(sndbuf
)) < 0) {
213 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_RCVBUF
,
214 &rcvbuf
, sizeof(rcvbuf
)) < 0) {
219 /* Older kernels may no support extended ACK reporting */
220 setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_EXT_ACK
,
223 memset(&rth
->local
, 0, sizeof(rth
->local
));
224 rth
->local
.nl_family
= AF_NETLINK
;
225 rth
->local
.nl_groups
= subscriptions
;
227 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
,
228 sizeof(rth
->local
)) < 0) {
229 perror("Cannot bind netlink socket");
232 addr_len
= sizeof(rth
->local
);
233 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
,
235 perror("Cannot getsockname");
238 if (addr_len
!= sizeof(rth
->local
)) {
239 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
242 if (rth
->local
.nl_family
!= AF_NETLINK
) {
243 fprintf(stderr
, "Wrong address family %d\n",
244 rth
->local
.nl_family
);
247 rth
->seq
= time(NULL
);
251 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
253 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
256 int rtnl_nexthopdump_req(struct rtnl_handle
*rth
, int family
,
257 req_filter_fn_t filter_fn
)
264 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
)),
265 .nlh
.nlmsg_type
= RTM_GETNEXTHOP
,
266 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
267 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
268 .nhm
.nh_family
= family
,
274 err
= filter_fn(&req
.nlh
, sizeof(req
));
279 return send(rth
->fd
, &req
, sizeof(req
), 0);
282 int rtnl_addrdump_req(struct rtnl_handle
*rth
, int family
,
283 req_filter_fn_t filter_fn
)
287 struct ifaddrmsg ifm
;
290 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
291 .nlh
.nlmsg_type
= RTM_GETADDR
,
292 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
293 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
294 .ifm
.ifa_family
= family
,
300 err
= filter_fn(&req
.nlh
, sizeof(req
));
305 return send(rth
->fd
, &req
, sizeof(req
), 0);
308 int rtnl_addrlbldump_req(struct rtnl_handle
*rth
, int family
)
312 struct ifaddrlblmsg ifal
;
314 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrlblmsg
)),
315 .nlh
.nlmsg_type
= RTM_GETADDRLABEL
,
316 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
317 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
318 .ifal
.ifal_family
= family
,
321 return send(rth
->fd
, &req
, sizeof(req
), 0);
324 int rtnl_routedump_req(struct rtnl_handle
*rth
, int family
,
325 req_filter_fn_t filter_fn
)
332 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
)),
333 .nlh
.nlmsg_type
= RTM_GETROUTE
,
334 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
335 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
336 .rtm
.rtm_family
= family
,
342 err
= filter_fn(&req
.nlh
, sizeof(req
));
347 return send(rth
->fd
, &req
, sizeof(req
), 0);
350 int rtnl_ruledump_req(struct rtnl_handle
*rth
, int family
)
354 struct fib_rule_hdr frh
;
356 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct fib_rule_hdr
)),
357 .nlh
.nlmsg_type
= RTM_GETRULE
,
358 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
359 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
363 return send(rth
->fd
, &req
, sizeof(req
), 0);
366 int rtnl_neighdump_req(struct rtnl_handle
*rth
, int family
,
367 req_filter_fn_t filter_fn
)
374 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
)),
375 .nlh
.nlmsg_type
= RTM_GETNEIGH
,
376 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
377 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
378 .ndm
.ndm_family
= family
,
384 err
= filter_fn(&req
.nlh
, sizeof(req
));
389 return send(rth
->fd
, &req
, sizeof(req
), 0);
392 int rtnl_neightbldump_req(struct rtnl_handle
*rth
, int family
)
396 struct ndtmsg ndtmsg
;
398 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndtmsg
)),
399 .nlh
.nlmsg_type
= RTM_GETNEIGHTBL
,
400 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
401 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
402 .ndtmsg
.ndtm_family
= family
,
405 return send(rth
->fd
, &req
, sizeof(req
), 0);
408 int rtnl_mdbdump_req(struct rtnl_handle
*rth
, int family
)
412 struct br_port_msg bpm
;
414 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct br_port_msg
)),
415 .nlh
.nlmsg_type
= RTM_GETMDB
,
416 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
417 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
418 .bpm
.family
= family
,
421 return send(rth
->fd
, &req
, sizeof(req
), 0);
424 int rtnl_netconfdump_req(struct rtnl_handle
*rth
, int family
)
428 struct netconfmsg ncm
;
429 char buf
[0] __aligned(NLMSG_ALIGNTO
);
431 .nlh
.nlmsg_len
= NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct netconfmsg
))),
432 .nlh
.nlmsg_type
= RTM_GETNETCONF
,
433 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
434 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
435 .ncm
.ncm_family
= family
,
438 return send(rth
->fd
, &req
, sizeof(req
), 0);
441 int rtnl_nsiddump_req(struct rtnl_handle
*rth
, int family
)
446 char buf
[0] __aligned(NLMSG_ALIGNTO
);
448 .nlh
.nlmsg_len
= NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct rtgenmsg
))),
449 .nlh
.nlmsg_type
= RTM_GETNSID
,
450 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
451 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
452 .rtm
.rtgen_family
= family
,
455 return send(rth
->fd
, &req
, sizeof(req
), 0);
458 static int __rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
462 struct ifinfomsg ifm
;
464 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
465 .nlh
.nlmsg_type
= RTM_GETLINK
,
466 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
467 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
468 .ifm
.ifi_family
= family
,
471 return send(rth
->fd
, &req
, sizeof(req
), 0);
474 int rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
476 if (family
== AF_UNSPEC
)
477 return rtnl_linkdump_req_filter(rth
, family
, RTEXT_FILTER_VF
);
479 return __rtnl_linkdump_req(rth
, family
);
482 int rtnl_linkdump_req_filter(struct rtnl_handle
*rth
, int family
,
485 if (family
== AF_UNSPEC
|| family
== AF_BRIDGE
) {
488 struct ifinfomsg ifm
;
489 /* attribute has to be NLMSG aligned */
490 struct rtattr ext_req
__aligned(NLMSG_ALIGNTO
);
491 __u32 ext_filter_mask
;
493 .nlh
.nlmsg_len
= sizeof(req
),
494 .nlh
.nlmsg_type
= RTM_GETLINK
,
495 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
496 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
497 .ifm
.ifi_family
= family
,
498 .ext_req
.rta_type
= IFLA_EXT_MASK
,
499 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
500 .ext_filter_mask
= filt_mask
,
503 return send(rth
->fd
, &req
, sizeof(req
), 0);
506 return __rtnl_linkdump_req(rth
, family
);
509 int rtnl_linkdump_req_filter_fn(struct rtnl_handle
*rth
, int family
,
510 req_filter_fn_t filter_fn
)
512 if (family
== AF_UNSPEC
|| family
== AF_PACKET
) {
515 struct ifinfomsg ifm
;
518 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
519 .nlh
.nlmsg_type
= RTM_GETLINK
,
520 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
521 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
522 .ifm
.ifi_family
= family
,
529 err
= filter_fn(&req
.nlh
, sizeof(req
));
533 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
536 return __rtnl_linkdump_req(rth
, family
);
539 int rtnl_fdb_linkdump_req_filter_fn(struct rtnl_handle
*rth
,
540 req_filter_fn_t filter_fn
)
544 struct ifinfomsg ifm
;
547 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
548 .nlh
.nlmsg_type
= RTM_GETNEIGH
,
549 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
550 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
551 .ifm
.ifi_family
= PF_BRIDGE
,
555 err
= filter_fn(&req
.nlh
, sizeof(req
));
559 return send(rth
->fd
, &req
, sizeof(req
), 0);
562 int rtnl_statsdump_req_filter(struct rtnl_handle
*rth
, int fam
, __u32 filt_mask
)
566 struct if_stats_msg ifsm
;
569 memset(&req
, 0, sizeof(req
));
570 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
571 req
.nlh
.nlmsg_type
= RTM_GETSTATS
;
572 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
573 req
.nlh
.nlmsg_pid
= 0;
574 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
575 req
.ifsm
.family
= fam
;
576 req
.ifsm
.filter_mask
= filt_mask
;
578 return send(rth
->fd
, &req
, sizeof(req
), 0);
581 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
583 return send(rth
->fd
, buf
, len
, 0);
586 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
592 status
= send(rth
->fd
, buf
, len
, 0);
596 /* Check for immediate errors */
597 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
604 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
605 h
= NLMSG_NEXT(h
, status
)) {
606 if (h
->nlmsg_type
== NLMSG_ERROR
) {
607 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
609 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
610 fprintf(stderr
, "ERROR truncated\n");
620 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
622 struct nlmsghdr nlh
= {
623 .nlmsg_len
= NLMSG_LENGTH(len
),
625 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
626 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
628 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
629 struct iovec iov
[2] = {
630 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
631 { .iov_base
= req
, .iov_len
= len
}
633 struct msghdr msg
= {
635 .msg_namelen
= sizeof(nladdr
),
640 return sendmsg(rth
->fd
, &msg
, 0);
643 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
645 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
648 .iov_len
= n
->nlmsg_len
650 struct msghdr msg
= {
652 .msg_namelen
= sizeof(nladdr
),
657 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
659 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
661 return sendmsg(rth
->fd
, &msg
, 0);
664 static int rtnl_dump_done(struct nlmsghdr
*h
)
666 int len
= *(int *)NLMSG_DATA(h
);
668 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
669 fprintf(stderr
, "DONE truncated\n");
674 /* check for any messages returned from kernel */
675 if (nl_dump_ext_ack_done(h
, len
))
685 "Error: Buffer too small for object.\n");
688 perror("RTNETLINK answers");
693 /* check for any messages returned from kernel */
694 nl_dump_ext_ack(h
, NULL
);
699 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
703 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
704 fprintf(stderr
, "ERROR truncated\n");
706 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
709 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
711 errno
== EOPNOTSUPP
))
714 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
715 perror("RTNETLINK answers");
719 static int __rtnl_recvmsg(int fd
, struct msghdr
*msg
, int flags
)
724 len
= recvmsg(fd
, msg
, flags
);
725 } while (len
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
728 fprintf(stderr
, "netlink receive error %s (%d)\n",
729 strerror(errno
), errno
);
734 fprintf(stderr
, "EOF on netlink\n");
741 static int rtnl_recvmsg(int fd
, struct msghdr
*msg
, char **answer
)
743 struct iovec
*iov
= msg
->msg_iov
;
747 iov
->iov_base
= NULL
;
750 len
= __rtnl_recvmsg(fd
, msg
, MSG_PEEK
| MSG_TRUNC
);
758 fprintf(stderr
, "malloc error: not enough buffer\n");
765 len
= __rtnl_recvmsg(fd
, msg
, 0);
779 static int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
780 const struct rtnl_dump_filter_arg
*arg
)
782 struct sockaddr_nl nladdr
;
784 struct msghdr msg
= {
786 .msg_namelen
= sizeof(nladdr
),
795 const struct rtnl_dump_filter_arg
*a
;
799 status
= rtnl_recvmsg(rth
->fd
, &msg
, &buf
);
804 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
806 for (a
= arg
; a
->filter
; a
++) {
807 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
811 while (NLMSG_OK(h
, msglen
)) {
814 h
->nlmsg_flags
&= ~a
->nc_flags
;
816 if (nladdr
.nl_pid
!= 0 ||
817 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
818 h
->nlmsg_seq
!= rth
->dump
)
821 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
824 if (h
->nlmsg_type
== NLMSG_DONE
) {
825 err
= rtnl_dump_done(h
);
832 break; /* process next filter */
835 if (h
->nlmsg_type
== NLMSG_ERROR
) {
836 rtnl_dump_error(rth
, h
);
842 err
= a
->filter(h
, a
->arg1
);
850 h
= NLMSG_NEXT(h
, msglen
);
858 "Dump was interrupted and may be inconsistent.\n");
862 if (msg
.msg_flags
& MSG_TRUNC
) {
863 fprintf(stderr
, "Message truncated\n");
867 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
873 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
874 rtnl_filter_t filter
,
875 void *arg1
, __u16 nc_flags
)
877 const struct rtnl_dump_filter_arg a
[2] = {
878 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
879 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
882 return rtnl_dump_filter_l(rth
, a
);
885 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
886 nl_ext_ack_fn_t errfn
)
888 if (nl_dump_ext_ack(h
, errfn
))
891 fprintf(stderr
, "RTNETLINK answers: %s\n",
892 strerror(-err
->error
));
896 static int __rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iov
,
897 size_t iovlen
, struct nlmsghdr
**answer
,
898 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
900 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
902 struct msghdr msg
= {
904 .msg_namelen
= sizeof(nladdr
),
906 .msg_iovlen
= iovlen
,
908 unsigned int seq
= 0;
913 for (i
= 0; i
< iovlen
; i
++) {
915 h
->nlmsg_seq
= seq
= ++rtnl
->seq
;
917 h
->nlmsg_flags
|= NLM_F_ACK
;
920 status
= sendmsg(rtnl
->fd
, &msg
, 0);
922 perror("Cannot talk to rtnetlink");
926 /* change msg to use the response iov */
932 status
= rtnl_recvmsg(rtnl
->fd
, &msg
, &buf
);
938 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
940 "sender address length == %d\n",
944 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
945 int len
= h
->nlmsg_len
;
946 int l
= len
- sizeof(*h
);
948 if (l
< 0 || len
> status
) {
949 if (msg
.msg_flags
& MSG_TRUNC
) {
950 fprintf(stderr
, "Truncated message\n");
955 "!!!malformed message: len=%d\n",
960 if (nladdr
.nl_pid
!= 0 ||
961 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
962 h
->nlmsg_seq
> seq
|| h
->nlmsg_seq
< seq
- iovlen
) {
963 /* Don't forget to skip that message. */
964 status
-= NLMSG_ALIGN(len
);
965 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
969 if (h
->nlmsg_type
== NLMSG_ERROR
) {
970 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
971 int error
= err
->error
;
973 if (l
< sizeof(struct nlmsgerr
)) {
974 fprintf(stderr
, "ERROR truncated\n");
980 /* check messages from kernel */
981 nl_dump_ext_ack(h
, errfn
);
985 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
987 rtnl_talk_error(h
, err
, errfn
);
991 *answer
= (struct nlmsghdr
*)buf
;
997 return error
? -i
: 0;
1001 *answer
= (struct nlmsghdr
*)buf
;
1005 fprintf(stderr
, "Unexpected reply!!!\n");
1007 status
-= NLMSG_ALIGN(len
);
1008 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
1012 if (msg
.msg_flags
& MSG_TRUNC
) {
1013 fprintf(stderr
, "Message truncated\n");
1018 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
1024 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
1025 struct nlmsghdr
**answer
,
1026 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
1028 struct iovec iov
= {
1030 .iov_len
= n
->nlmsg_len
1033 return __rtnl_talk_iov(rtnl
, &iov
, 1, answer
, show_rtnl_err
, errfn
);
1036 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
1037 struct nlmsghdr
**answer
)
1039 return __rtnl_talk(rtnl
, n
, answer
, true, NULL
);
1042 int rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iovec
, size_t iovlen
,
1043 struct nlmsghdr
**answer
)
1045 return __rtnl_talk_iov(rtnl
, iovec
, iovlen
, answer
, true, NULL
);
1048 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
1049 struct nlmsghdr
**answer
)
1051 return __rtnl_talk(rtnl
, n
, answer
, false, NULL
);
1054 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
1056 unsigned int on
= 1;
1058 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
1060 perror("NETLINK_LISTEN_ALL_NSID");
1063 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
1067 int rtnl_listen(struct rtnl_handle
*rtnl
,
1068 rtnl_listen_filter_t handler
,
1073 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
1075 struct msghdr msg
= {
1076 .msg_name
= &nladdr
,
1077 .msg_namelen
= sizeof(nladdr
),
1082 char cmsgbuf
[BUFSIZ
];
1084 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
1085 msg
.msg_control
= &cmsgbuf
;
1086 msg
.msg_controllen
= sizeof(cmsgbuf
);
1091 struct rtnl_ctrl_data ctrl
;
1092 struct cmsghdr
*cmsg
;
1094 iov
.iov_len
= sizeof(buf
);
1095 status
= recvmsg(rtnl
->fd
, &msg
, 0);
1098 if (errno
== EINTR
|| errno
== EAGAIN
)
1100 fprintf(stderr
, "netlink receive error %s (%d)\n",
1101 strerror(errno
), errno
);
1102 if (errno
== ENOBUFS
)
1107 fprintf(stderr
, "EOF on netlink\n");
1110 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
1112 "Sender address length == %d\n",
1117 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
1118 memset(&ctrl
, 0, sizeof(ctrl
));
1120 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
1121 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
1122 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
1123 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
1124 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
1125 int *data
= (int *)CMSG_DATA(cmsg
);
1131 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
1133 int len
= h
->nlmsg_len
;
1134 int l
= len
- sizeof(*h
);
1136 if (l
< 0 || len
> status
) {
1137 if (msg
.msg_flags
& MSG_TRUNC
) {
1138 fprintf(stderr
, "Truncated message\n");
1142 "!!!malformed message: len=%d\n",
1147 err
= handler(&ctrl
, h
, jarg
);
1151 status
-= NLMSG_ALIGN(len
);
1152 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
1154 if (msg
.msg_flags
& MSG_TRUNC
) {
1155 fprintf(stderr
, "Message truncated\n");
1159 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
1165 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
1170 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
1176 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
1181 perror("rtnl_from_file: fread");
1188 l
= len
- sizeof(*h
);
1190 if (l
< 0 || len
> sizeof(buf
)) {
1191 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
1196 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
1199 perror("rtnl_from_file: fread");
1203 fprintf(stderr
, "rtnl-from_file: truncated message\n");
1207 err
= handler(NULL
, h
, jarg
);
1213 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
1215 return addattr_l(n
, maxlen
, type
, NULL
, 0);
1218 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
1220 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
1223 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
1225 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
1228 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
1230 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
1233 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
1235 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
1238 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
1240 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
1243 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
1246 int len
= RTA_LENGTH(alen
);
1249 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
1251 "addattr_l ERROR: message exceeded bound of %d\n",
1255 rta
= NLMSG_TAIL(n
);
1256 rta
->rta_type
= type
;
1259 memcpy(RTA_DATA(rta
), data
, alen
);
1260 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
1264 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
1266 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
1268 "addraw_l ERROR: message exceeded bound of %d\n",
1273 memcpy(NLMSG_TAIL(n
), data
, len
);
1274 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
1275 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
1279 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
1281 struct rtattr
*nest
= NLMSG_TAIL(n
);
1283 addattr_l(n
, maxlen
, type
, NULL
, 0);
1287 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
1289 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
1290 return n
->nlmsg_len
;
1293 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
1294 const void *data
, int len
)
1296 struct rtattr
*start
= NLMSG_TAIL(n
);
1298 addattr_l(n
, maxlen
, type
, data
, len
);
1299 addattr_nest(n
, maxlen
, type
);
1303 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
1305 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
1307 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
1308 addattr_nest_end(n
, nest
);
1309 return n
->nlmsg_len
;
1312 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
1314 int len
= RTA_LENGTH(4);
1315 struct rtattr
*subrta
;
1317 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
1319 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1323 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1324 subrta
->rta_type
= type
;
1325 subrta
->rta_len
= len
;
1326 memcpy(RTA_DATA(subrta
), &data
, 4);
1327 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
1331 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
1332 const void *data
, int alen
)
1334 struct rtattr
*subrta
;
1335 int len
= RTA_LENGTH(alen
);
1337 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
1339 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1343 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1344 subrta
->rta_type
= type
;
1345 subrta
->rta_len
= len
;
1347 memcpy(RTA_DATA(subrta
), data
, alen
);
1348 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
1352 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
1354 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
1357 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
1359 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
1362 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
1364 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
1367 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
1369 struct rtattr
*nest
= RTA_TAIL(rta
);
1371 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
1372 nest
->rta_type
|= NLA_F_NESTED
;
1377 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
1379 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
1381 return rta
->rta_len
;
1384 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
1386 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
1389 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
1390 int len
, unsigned short flags
)
1392 unsigned short type
;
1394 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1395 while (RTA_OK(rta
, len
)) {
1396 type
= rta
->rta_type
& ~flags
;
1397 if ((type
<= max
) && (!tb
[type
]))
1399 rta
= RTA_NEXT(rta
, len
);
1402 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1407 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1409 while (RTA_OK(rta
, len
)) {
1410 if (rta
->rta_type
== type
)
1412 rta
= RTA_NEXT(rta
, len
);
1416 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1421 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1425 if (RTA_PAYLOAD(rta
) < len
)
1427 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1428 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1429 return parse_rtattr_nested(tb
, max
, rta
);
1431 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));