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"
34 #define __aligned(x) __attribute__((aligned(x)))
38 #define SOL_NETLINK 270
42 #define MIN(a, b) ((a) < (b) ? (a) : (b))
45 int rcvbuf
= 1024 * 1024;
48 #include <libmnl/libmnl.h>
50 static const enum mnl_attr_data_type extack_policy
[NLMSGERR_ATTR_MAX
+ 1] = {
51 [NLMSGERR_ATTR_MSG
] = MNL_TYPE_NUL_STRING
,
52 [NLMSGERR_ATTR_OFFS
] = MNL_TYPE_U32
,
55 static int err_attr_cb(const struct nlattr
*attr
, void *data
)
57 const struct nlattr
**tb
= data
;
60 if (mnl_attr_type_valid(attr
, NLMSGERR_ATTR_MAX
) < 0) {
61 fprintf(stderr
, "Invalid extack attribute\n");
65 type
= mnl_attr_get_type(attr
);
66 if (mnl_attr_validate(attr
, extack_policy
[type
]) < 0) {
67 fprintf(stderr
, "extack attribute %d failed validation\n",
76 static void print_ext_ack_msg(bool is_err
, const char *msg
)
78 fprintf(stderr
, "%s: %s", is_err
? "Error" : "Warning", msg
);
79 if (msg
[strlen(msg
) - 1] != '.')
81 fprintf(stderr
, "\n");
84 /* dump netlink extended ack error message */
85 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
87 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
88 const struct nlmsgerr
*err
= mnl_nlmsg_get_payload(nlh
);
89 const struct nlmsghdr
*err_nlh
= NULL
;
90 unsigned int hlen
= sizeof(*err
);
91 const char *msg
= NULL
;
94 /* no TLVs, nothing to do here */
95 if (!(nlh
->nlmsg_flags
& NLM_F_ACK_TLVS
))
98 /* if NLM_F_CAPPED is set then the inner err msg was capped */
99 if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
100 hlen
+= mnl_nlmsg_get_payload_len(&err
->msg
);
102 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
105 if (tb
[NLMSGERR_ATTR_MSG
])
106 msg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
108 if (tb
[NLMSGERR_ATTR_OFFS
]) {
109 off
= mnl_attr_get_u32(tb
[NLMSGERR_ATTR_OFFS
]);
111 if (off
> nlh
->nlmsg_len
) {
113 "Invalid offset for NLMSGERR_ATTR_OFFS\n");
115 } else if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
120 return errfn(msg
, off
, err_nlh
);
122 if (msg
&& *msg
!= '\0') {
123 bool is_err
= !!err
->error
;
125 print_ext_ack_msg(is_err
, msg
);
126 return is_err
? 1 : 0;
132 int nl_dump_ext_ack_done(const struct nlmsghdr
*nlh
, int error
)
134 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
135 unsigned int hlen
= sizeof(int);
136 const char *msg
= NULL
;
138 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
141 if (tb
[NLMSGERR_ATTR_MSG
])
142 msg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
144 if (msg
&& *msg
!= '\0') {
145 bool is_err
= !!error
;
147 print_ext_ack_msg(is_err
, msg
);
148 return is_err
? 1 : 0;
154 #warning "libmnl required for error support"
156 /* No extended error ack without libmnl */
157 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
162 int nl_dump_ext_ack_done(const struct nlmsghdr
*nlh
, int error
)
168 /* Older kernels may not support strict dump and filtering */
169 void rtnl_set_strict_dump(struct rtnl_handle
*rth
)
173 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_GET_STRICT_CHK
,
174 &one
, sizeof(one
)) < 0)
177 rth
->flags
|= RTNL_HANDLE_F_STRICT_CHK
;
180 int rtnl_add_nl_group(struct rtnl_handle
*rth
, unsigned int group
)
182 return setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_ADD_MEMBERSHIP
,
183 &group
, sizeof(group
));
186 void rtnl_close(struct rtnl_handle
*rth
)
194 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
201 memset(rth
, 0, sizeof(*rth
));
203 rth
->proto
= protocol
;
204 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
206 perror("Cannot open netlink socket");
210 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_SNDBUF
,
211 &sndbuf
, sizeof(sndbuf
)) < 0) {
216 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_RCVBUF
,
217 &rcvbuf
, sizeof(rcvbuf
)) < 0) {
222 /* Older kernels may no support extended ACK reporting */
223 setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_EXT_ACK
,
226 memset(&rth
->local
, 0, sizeof(rth
->local
));
227 rth
->local
.nl_family
= AF_NETLINK
;
228 rth
->local
.nl_groups
= subscriptions
;
230 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
,
231 sizeof(rth
->local
)) < 0) {
232 perror("Cannot bind netlink socket");
235 addr_len
= sizeof(rth
->local
);
236 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
,
238 perror("Cannot getsockname");
241 if (addr_len
!= sizeof(rth
->local
)) {
242 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
245 if (rth
->local
.nl_family
!= AF_NETLINK
) {
246 fprintf(stderr
, "Wrong address family %d\n",
247 rth
->local
.nl_family
);
250 rth
->seq
= time(NULL
);
254 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
256 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
259 int rtnl_nexthopdump_req(struct rtnl_handle
*rth
, int family
,
260 req_filter_fn_t filter_fn
)
267 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
)),
268 .nlh
.nlmsg_type
= RTM_GETNEXTHOP
,
269 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
270 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
271 .nhm
.nh_family
= family
,
277 err
= filter_fn(&req
.nlh
, sizeof(req
));
282 return send(rth
->fd
, &req
, sizeof(req
), 0);
285 int rtnl_addrdump_req(struct rtnl_handle
*rth
, int family
,
286 req_filter_fn_t filter_fn
)
290 struct ifaddrmsg ifm
;
293 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
294 .nlh
.nlmsg_type
= RTM_GETADDR
,
295 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
296 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
297 .ifm
.ifa_family
= family
,
303 err
= filter_fn(&req
.nlh
, sizeof(req
));
308 return send(rth
->fd
, &req
, sizeof(req
), 0);
311 int rtnl_addrlbldump_req(struct rtnl_handle
*rth
, int family
)
315 struct ifaddrlblmsg ifal
;
317 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrlblmsg
)),
318 .nlh
.nlmsg_type
= RTM_GETADDRLABEL
,
319 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
320 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
321 .ifal
.ifal_family
= family
,
324 return send(rth
->fd
, &req
, sizeof(req
), 0);
327 int rtnl_routedump_req(struct rtnl_handle
*rth
, int family
,
328 req_filter_fn_t filter_fn
)
335 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
)),
336 .nlh
.nlmsg_type
= RTM_GETROUTE
,
337 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
338 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
339 .rtm
.rtm_family
= family
,
345 err
= filter_fn(&req
.nlh
, sizeof(req
));
350 return send(rth
->fd
, &req
, sizeof(req
), 0);
353 int rtnl_ruledump_req(struct rtnl_handle
*rth
, int family
)
357 struct fib_rule_hdr frh
;
359 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct fib_rule_hdr
)),
360 .nlh
.nlmsg_type
= RTM_GETRULE
,
361 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
362 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
366 return send(rth
->fd
, &req
, sizeof(req
), 0);
369 int rtnl_neighdump_req(struct rtnl_handle
*rth
, int family
,
370 req_filter_fn_t filter_fn
)
377 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
)),
378 .nlh
.nlmsg_type
= RTM_GETNEIGH
,
379 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
380 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
381 .ndm
.ndm_family
= family
,
387 err
= filter_fn(&req
.nlh
, sizeof(req
));
392 return send(rth
->fd
, &req
, sizeof(req
), 0);
395 int rtnl_neightbldump_req(struct rtnl_handle
*rth
, int family
)
399 struct ndtmsg ndtmsg
;
401 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndtmsg
)),
402 .nlh
.nlmsg_type
= RTM_GETNEIGHTBL
,
403 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
404 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
405 .ndtmsg
.ndtm_family
= family
,
408 return send(rth
->fd
, &req
, sizeof(req
), 0);
411 int rtnl_mdbdump_req(struct rtnl_handle
*rth
, int family
)
415 struct br_port_msg bpm
;
417 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct br_port_msg
)),
418 .nlh
.nlmsg_type
= RTM_GETMDB
,
419 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
420 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
421 .bpm
.family
= family
,
424 return send(rth
->fd
, &req
, sizeof(req
), 0);
427 int rtnl_netconfdump_req(struct rtnl_handle
*rth
, int family
)
431 struct netconfmsg ncm
;
432 char buf
[0] __aligned(NLMSG_ALIGNTO
);
434 .nlh
.nlmsg_len
= NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct netconfmsg
))),
435 .nlh
.nlmsg_type
= RTM_GETNETCONF
,
436 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
437 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
438 .ncm
.ncm_family
= family
,
441 return send(rth
->fd
, &req
, sizeof(req
), 0);
444 int rtnl_nsiddump_req_filter_fn(struct rtnl_handle
*rth
, int family
,
445 req_filter_fn_t filter_fn
)
452 .nlh
.nlmsg_len
= NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct rtgenmsg
))),
453 .nlh
.nlmsg_type
= RTM_GETNSID
,
454 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
455 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
456 .rtm
.rtgen_family
= family
,
463 err
= filter_fn(&req
.nlh
, sizeof(req
));
467 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
470 static int __rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
474 struct ifinfomsg ifm
;
476 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
477 .nlh
.nlmsg_type
= RTM_GETLINK
,
478 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
479 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
480 .ifm
.ifi_family
= family
,
483 return send(rth
->fd
, &req
, sizeof(req
), 0);
486 int rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
488 if (family
== AF_UNSPEC
)
489 return rtnl_linkdump_req_filter(rth
, family
, RTEXT_FILTER_VF
);
491 return __rtnl_linkdump_req(rth
, family
);
494 int rtnl_linkdump_req_filter(struct rtnl_handle
*rth
, int family
,
497 if (family
== AF_UNSPEC
|| family
== AF_BRIDGE
) {
500 struct ifinfomsg ifm
;
501 /* attribute has to be NLMSG aligned */
502 struct rtattr ext_req
__aligned(NLMSG_ALIGNTO
);
503 __u32 ext_filter_mask
;
505 .nlh
.nlmsg_len
= sizeof(req
),
506 .nlh
.nlmsg_type
= RTM_GETLINK
,
507 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
508 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
509 .ifm
.ifi_family
= family
,
510 .ext_req
.rta_type
= IFLA_EXT_MASK
,
511 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
512 .ext_filter_mask
= filt_mask
,
515 return send(rth
->fd
, &req
, sizeof(req
), 0);
518 return __rtnl_linkdump_req(rth
, family
);
521 int rtnl_linkdump_req_filter_fn(struct rtnl_handle
*rth
, int family
,
522 req_filter_fn_t filter_fn
)
524 if (family
== AF_UNSPEC
|| family
== AF_PACKET
) {
527 struct ifinfomsg ifm
;
530 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
531 .nlh
.nlmsg_type
= RTM_GETLINK
,
532 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
533 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
534 .ifm
.ifi_family
= family
,
541 err
= filter_fn(&req
.nlh
, sizeof(req
));
545 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
548 return __rtnl_linkdump_req(rth
, family
);
551 int rtnl_fdb_linkdump_req_filter_fn(struct rtnl_handle
*rth
,
552 req_filter_fn_t filter_fn
)
556 struct ifinfomsg ifm
;
559 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
560 .nlh
.nlmsg_type
= RTM_GETNEIGH
,
561 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
562 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
563 .ifm
.ifi_family
= PF_BRIDGE
,
567 err
= filter_fn(&req
.nlh
, sizeof(req
));
571 return send(rth
->fd
, &req
, sizeof(req
), 0);
574 int rtnl_statsdump_req_filter(struct rtnl_handle
*rth
, int fam
, __u32 filt_mask
)
578 struct if_stats_msg ifsm
;
581 memset(&req
, 0, sizeof(req
));
582 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
583 req
.nlh
.nlmsg_type
= RTM_GETSTATS
;
584 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
585 req
.nlh
.nlmsg_pid
= 0;
586 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
587 req
.ifsm
.family
= fam
;
588 req
.ifsm
.filter_mask
= filt_mask
;
590 return send(rth
->fd
, &req
, sizeof(req
), 0);
593 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
595 return send(rth
->fd
, buf
, len
, 0);
598 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
604 status
= send(rth
->fd
, buf
, len
, 0);
608 /* Check for immediate errors */
609 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
616 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
617 h
= NLMSG_NEXT(h
, status
)) {
618 if (h
->nlmsg_type
== NLMSG_ERROR
) {
619 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
621 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
622 fprintf(stderr
, "ERROR truncated\n");
632 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
634 struct nlmsghdr nlh
= {
635 .nlmsg_len
= NLMSG_LENGTH(len
),
637 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
638 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
640 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
641 struct iovec iov
[2] = {
642 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
643 { .iov_base
= req
, .iov_len
= len
}
645 struct msghdr msg
= {
647 .msg_namelen
= sizeof(nladdr
),
652 return sendmsg(rth
->fd
, &msg
, 0);
655 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
657 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
660 .iov_len
= n
->nlmsg_len
662 struct msghdr msg
= {
664 .msg_namelen
= sizeof(nladdr
),
669 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
671 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
673 return sendmsg(rth
->fd
, &msg
, 0);
676 static int rtnl_dump_done(struct nlmsghdr
*h
)
678 int len
= *(int *)NLMSG_DATA(h
);
680 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
681 fprintf(stderr
, "DONE truncated\n");
686 /* check for any messages returned from kernel */
687 if (nl_dump_ext_ack_done(h
, len
))
697 "Error: Buffer too small for object.\n");
700 perror("RTNETLINK answers");
705 /* check for any messages returned from kernel */
706 nl_dump_ext_ack(h
, NULL
);
711 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
715 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
716 fprintf(stderr
, "ERROR truncated\n");
718 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
721 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
723 errno
== EOPNOTSUPP
))
726 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
727 perror("RTNETLINK answers");
731 static int __rtnl_recvmsg(int fd
, struct msghdr
*msg
, int flags
)
736 len
= recvmsg(fd
, msg
, flags
);
737 } while (len
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
740 fprintf(stderr
, "netlink receive error %s (%d)\n",
741 strerror(errno
), errno
);
746 fprintf(stderr
, "EOF on netlink\n");
753 static int rtnl_recvmsg(int fd
, struct msghdr
*msg
, char **answer
)
755 struct iovec
*iov
= msg
->msg_iov
;
759 iov
->iov_base
= NULL
;
762 len
= __rtnl_recvmsg(fd
, msg
, MSG_PEEK
| MSG_TRUNC
);
770 fprintf(stderr
, "malloc error: not enough buffer\n");
777 len
= __rtnl_recvmsg(fd
, msg
, 0);
791 static int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
792 const struct rtnl_dump_filter_arg
*arg
)
794 struct sockaddr_nl nladdr
;
796 struct msghdr msg
= {
798 .msg_namelen
= sizeof(nladdr
),
807 const struct rtnl_dump_filter_arg
*a
;
811 status
= rtnl_recvmsg(rth
->fd
, &msg
, &buf
);
816 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
818 for (a
= arg
; a
->filter
; a
++) {
819 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
823 while (NLMSG_OK(h
, msglen
)) {
826 h
->nlmsg_flags
&= ~a
->nc_flags
;
828 if (nladdr
.nl_pid
!= 0 ||
829 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
830 h
->nlmsg_seq
!= rth
->dump
)
833 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
836 if (h
->nlmsg_type
== NLMSG_DONE
) {
837 err
= rtnl_dump_done(h
);
844 break; /* process next filter */
847 if (h
->nlmsg_type
== NLMSG_ERROR
) {
848 rtnl_dump_error(rth
, h
);
854 err
= a
->filter(h
, a
->arg1
);
862 h
= NLMSG_NEXT(h
, msglen
);
870 "Dump was interrupted and may be inconsistent.\n");
874 if (msg
.msg_flags
& MSG_TRUNC
) {
875 fprintf(stderr
, "Message truncated\n");
879 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
885 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
886 rtnl_filter_t filter
,
887 void *arg1
, __u16 nc_flags
)
889 const struct rtnl_dump_filter_arg a
[2] = {
890 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
891 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
894 return rtnl_dump_filter_l(rth
, a
);
897 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
898 nl_ext_ack_fn_t errfn
)
900 if (nl_dump_ext_ack(h
, errfn
))
903 fprintf(stderr
, "RTNETLINK answers: %s\n",
904 strerror(-err
->error
));
908 static int __rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iov
,
909 size_t iovlen
, struct nlmsghdr
**answer
,
910 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
912 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
914 struct msghdr msg
= {
916 .msg_namelen
= sizeof(nladdr
),
918 .msg_iovlen
= iovlen
,
920 unsigned int seq
= 0;
925 for (i
= 0; i
< iovlen
; i
++) {
927 h
->nlmsg_seq
= seq
= ++rtnl
->seq
;
929 h
->nlmsg_flags
|= NLM_F_ACK
;
932 status
= sendmsg(rtnl
->fd
, &msg
, 0);
934 perror("Cannot talk to rtnetlink");
938 /* change msg to use the response iov */
944 status
= rtnl_recvmsg(rtnl
->fd
, &msg
, &buf
);
950 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
952 "sender address length == %d\n",
956 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
957 int len
= h
->nlmsg_len
;
958 int l
= len
- sizeof(*h
);
960 if (l
< 0 || len
> status
) {
961 if (msg
.msg_flags
& MSG_TRUNC
) {
962 fprintf(stderr
, "Truncated message\n");
967 "!!!malformed message: len=%d\n",
972 if (nladdr
.nl_pid
!= 0 ||
973 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
974 h
->nlmsg_seq
> seq
|| h
->nlmsg_seq
< seq
- iovlen
) {
975 /* Don't forget to skip that message. */
976 status
-= NLMSG_ALIGN(len
);
977 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
981 if (h
->nlmsg_type
== NLMSG_ERROR
) {
982 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
983 int error
= err
->error
;
985 if (l
< sizeof(struct nlmsgerr
)) {
986 fprintf(stderr
, "ERROR truncated\n");
992 /* check messages from kernel */
993 nl_dump_ext_ack(h
, errfn
);
997 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
999 rtnl_talk_error(h
, err
, errfn
);
1003 *answer
= (struct nlmsghdr
*)buf
;
1009 return error
? -i
: 0;
1013 *answer
= (struct nlmsghdr
*)buf
;
1017 fprintf(stderr
, "Unexpected reply!!!\n");
1019 status
-= NLMSG_ALIGN(len
);
1020 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
1024 if (msg
.msg_flags
& MSG_TRUNC
) {
1025 fprintf(stderr
, "Message truncated\n");
1030 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
1036 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
1037 struct nlmsghdr
**answer
,
1038 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
1040 struct iovec iov
= {
1042 .iov_len
= n
->nlmsg_len
1045 return __rtnl_talk_iov(rtnl
, &iov
, 1, answer
, show_rtnl_err
, errfn
);
1048 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
1049 struct nlmsghdr
**answer
)
1051 return __rtnl_talk(rtnl
, n
, answer
, true, NULL
);
1054 int rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iovec
, size_t iovlen
,
1055 struct nlmsghdr
**answer
)
1057 return __rtnl_talk_iov(rtnl
, iovec
, iovlen
, answer
, true, NULL
);
1060 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
1061 struct nlmsghdr
**answer
)
1063 return __rtnl_talk(rtnl
, n
, answer
, false, NULL
);
1066 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
1068 unsigned int on
= 1;
1070 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
1072 perror("NETLINK_LISTEN_ALL_NSID");
1075 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
1079 int rtnl_listen(struct rtnl_handle
*rtnl
,
1080 rtnl_listen_filter_t handler
,
1085 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
1087 struct msghdr msg
= {
1088 .msg_name
= &nladdr
,
1089 .msg_namelen
= sizeof(nladdr
),
1094 char cmsgbuf
[BUFSIZ
];
1096 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
1097 msg
.msg_control
= &cmsgbuf
;
1098 msg
.msg_controllen
= sizeof(cmsgbuf
);
1103 struct rtnl_ctrl_data ctrl
;
1104 struct cmsghdr
*cmsg
;
1106 iov
.iov_len
= sizeof(buf
);
1107 status
= recvmsg(rtnl
->fd
, &msg
, 0);
1110 if (errno
== EINTR
|| errno
== EAGAIN
)
1112 fprintf(stderr
, "netlink receive error %s (%d)\n",
1113 strerror(errno
), errno
);
1114 if (errno
== ENOBUFS
)
1119 fprintf(stderr
, "EOF on netlink\n");
1122 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
1124 "Sender address length == %d\n",
1129 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
1130 memset(&ctrl
, 0, sizeof(ctrl
));
1132 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
1133 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
1134 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
1135 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
1136 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
1137 int *data
= (int *)CMSG_DATA(cmsg
);
1143 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
1145 int len
= h
->nlmsg_len
;
1146 int l
= len
- sizeof(*h
);
1148 if (l
< 0 || len
> status
) {
1149 if (msg
.msg_flags
& MSG_TRUNC
) {
1150 fprintf(stderr
, "Truncated message\n");
1154 "!!!malformed message: len=%d\n",
1159 err
= handler(&ctrl
, h
, jarg
);
1163 status
-= NLMSG_ALIGN(len
);
1164 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
1166 if (msg
.msg_flags
& MSG_TRUNC
) {
1167 fprintf(stderr
, "Message truncated\n");
1171 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
1177 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
1182 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
1188 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
1190 if (status
== 0 && feof(rtnl
))
1192 if (status
!= sizeof(*h
)) {
1194 perror("rtnl_from_file: fread");
1196 fprintf(stderr
, "rtnl-from_file: truncated message\n");
1201 l
= len
- sizeof(*h
);
1203 if (l
< 0 || len
> sizeof(buf
)) {
1204 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
1209 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
1211 if (status
!= NLMSG_ALIGN(l
)) {
1213 perror("rtnl_from_file: fread");
1215 fprintf(stderr
, "rtnl-from_file: truncated message\n");
1219 err
= handler(NULL
, h
, jarg
);
1225 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
1227 return addattr_l(n
, maxlen
, type
, NULL
, 0);
1230 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
1232 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
1235 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
1237 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
1240 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
1242 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
1245 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
1247 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
1250 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
1252 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
1255 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
1258 int len
= RTA_LENGTH(alen
);
1261 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
1263 "addattr_l ERROR: message exceeded bound of %d\n",
1267 rta
= NLMSG_TAIL(n
);
1268 rta
->rta_type
= type
;
1271 memcpy(RTA_DATA(rta
), data
, alen
);
1272 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
1276 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
1278 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
1280 "addraw_l ERROR: message exceeded bound of %d\n",
1285 memcpy(NLMSG_TAIL(n
), data
, len
);
1286 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
1287 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
1291 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
1293 struct rtattr
*nest
= NLMSG_TAIL(n
);
1295 addattr_l(n
, maxlen
, type
, NULL
, 0);
1299 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
1301 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
1302 return n
->nlmsg_len
;
1305 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
1306 const void *data
, int len
)
1308 struct rtattr
*start
= NLMSG_TAIL(n
);
1310 addattr_l(n
, maxlen
, type
, data
, len
);
1311 addattr_nest(n
, maxlen
, type
);
1315 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
1317 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
1319 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
1320 addattr_nest_end(n
, nest
);
1321 return n
->nlmsg_len
;
1324 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
1326 int len
= RTA_LENGTH(4);
1327 struct rtattr
*subrta
;
1329 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
1331 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1335 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1336 subrta
->rta_type
= type
;
1337 subrta
->rta_len
= len
;
1338 memcpy(RTA_DATA(subrta
), &data
, 4);
1339 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
1343 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
1344 const void *data
, int alen
)
1346 struct rtattr
*subrta
;
1347 int len
= RTA_LENGTH(alen
);
1349 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
1351 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1355 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1356 subrta
->rta_type
= type
;
1357 subrta
->rta_len
= len
;
1359 memcpy(RTA_DATA(subrta
), data
, alen
);
1360 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
1364 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
1366 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
1369 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
1371 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
1374 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
1376 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
1379 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
1381 struct rtattr
*nest
= RTA_TAIL(rta
);
1383 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
1384 nest
->rta_type
|= NLA_F_NESTED
;
1389 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
1391 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
1393 return rta
->rta_len
;
1396 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
1398 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
1401 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
1402 int len
, unsigned short flags
)
1404 unsigned short type
;
1406 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1407 while (RTA_OK(rta
, len
)) {
1408 type
= rta
->rta_type
& ~flags
;
1409 if ((type
<= max
) && (!tb
[type
]))
1411 rta
= RTA_NEXT(rta
, len
);
1414 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1419 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1421 while (RTA_OK(rta
, len
)) {
1422 if (rta
->rta_type
== type
)
1424 rta
= RTA_NEXT(rta
, len
);
1428 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1433 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1437 if (RTA_PAYLOAD(rta
) < len
)
1439 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1440 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1441 return parse_rtattr_nested(tb
, max
, rta
);
1443 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1447 static const char *get_nla_type_str(unsigned int attr
)
1450 #define C(x) case NL_ATTR_TYPE_ ## x: return #x
1471 void nl_print_policy(const struct rtattr
*attr
, FILE *fp
)
1473 const struct rtattr
*pos
;
1475 rtattr_for_each_nested(pos
, attr
) {
1476 const struct rtattr
*attr
;
1478 fprintf(fp
, " policy[%u]:", pos
->rta_type
& ~NLA_F_NESTED
);
1480 rtattr_for_each_nested(attr
, pos
) {
1481 struct rtattr
*tp
[NL_POLICY_TYPE_ATTR_MAX
+ 1];
1483 parse_rtattr_nested(tp
, ARRAY_SIZE(tp
) - 1, attr
);
1485 if (tp
[NL_POLICY_TYPE_ATTR_TYPE
])
1486 fprintf(fp
, "attr[%u]: type=%s",
1487 attr
->rta_type
& ~NLA_F_NESTED
,
1488 get_nla_type_str(rta_getattr_u32(tp
[NL_POLICY_TYPE_ATTR_TYPE
])));
1490 if (tp
[NL_POLICY_TYPE_ATTR_POLICY_IDX
])
1491 fprintf(fp
, " policy:%u",
1492 rta_getattr_u32(tp
[NL_POLICY_TYPE_ATTR_POLICY_IDX
]));
1494 if (tp
[NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE
])
1495 fprintf(fp
, " maxattr:%u",
1496 rta_getattr_u32(tp
[NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE
]));
1498 if (tp
[NL_POLICY_TYPE_ATTR_MIN_VALUE_S
] && tp
[NL_POLICY_TYPE_ATTR_MAX_VALUE_S
])
1499 fprintf(fp
, " range:[%lld,%lld]",
1500 (signed long long)rta_getattr_u64(tp
[NL_POLICY_TYPE_ATTR_MIN_VALUE_S
]),
1501 (signed long long)rta_getattr_u64(tp
[NL_POLICY_TYPE_ATTR_MAX_VALUE_S
]));
1503 if (tp
[NL_POLICY_TYPE_ATTR_MIN_VALUE_U
] && tp
[NL_POLICY_TYPE_ATTR_MAX_VALUE_U
])
1504 fprintf(fp
, " range:[%llu,%llu]",
1505 (unsigned long long)rta_getattr_u64(tp
[NL_POLICY_TYPE_ATTR_MIN_VALUE_U
]),
1506 (unsigned long long)rta_getattr_u64(tp
[NL_POLICY_TYPE_ATTR_MAX_VALUE_U
]));
1508 if (tp
[NL_POLICY_TYPE_ATTR_MIN_LENGTH
])
1509 fprintf(fp
, " min len:%u",
1510 rta_getattr_u32(tp
[NL_POLICY_TYPE_ATTR_MIN_LENGTH
]));
1512 if (tp
[NL_POLICY_TYPE_ATTR_MAX_LENGTH
])
1513 fprintf(fp
, " max len:%u",
1514 rta_getattr_u32(tp
[NL_POLICY_TYPE_ATTR_MAX_LENGTH
]));