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 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 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_filter_fn(struct rtnl_handle
*rth
, int family
,
442 req_filter_fn_t filter_fn
)
449 .nlh
.nlmsg_len
= NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct rtgenmsg
))),
450 .nlh
.nlmsg_type
= RTM_GETNSID
,
451 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
452 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
453 .rtm
.rtgen_family
= family
,
460 err
= filter_fn(&req
.nlh
, sizeof(req
));
464 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
467 static int __rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
471 struct ifinfomsg ifm
;
473 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
474 .nlh
.nlmsg_type
= RTM_GETLINK
,
475 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
476 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
477 .ifm
.ifi_family
= family
,
480 return send(rth
->fd
, &req
, sizeof(req
), 0);
483 int rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
485 if (family
== AF_UNSPEC
)
486 return rtnl_linkdump_req_filter(rth
, family
, RTEXT_FILTER_VF
);
488 return __rtnl_linkdump_req(rth
, family
);
491 int rtnl_linkdump_req_filter(struct rtnl_handle
*rth
, int family
,
494 if (family
== AF_UNSPEC
|| family
== AF_BRIDGE
) {
497 struct ifinfomsg ifm
;
498 /* attribute has to be NLMSG aligned */
499 struct rtattr ext_req
__aligned(NLMSG_ALIGNTO
);
500 __u32 ext_filter_mask
;
502 .nlh
.nlmsg_len
= sizeof(req
),
503 .nlh
.nlmsg_type
= RTM_GETLINK
,
504 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
505 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
506 .ifm
.ifi_family
= family
,
507 .ext_req
.rta_type
= IFLA_EXT_MASK
,
508 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
509 .ext_filter_mask
= filt_mask
,
512 return send(rth
->fd
, &req
, sizeof(req
), 0);
515 return __rtnl_linkdump_req(rth
, family
);
518 int rtnl_linkdump_req_filter_fn(struct rtnl_handle
*rth
, int family
,
519 req_filter_fn_t filter_fn
)
521 if (family
== AF_UNSPEC
|| family
== AF_PACKET
) {
524 struct ifinfomsg ifm
;
527 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
528 .nlh
.nlmsg_type
= RTM_GETLINK
,
529 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
530 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
531 .ifm
.ifi_family
= family
,
538 err
= filter_fn(&req
.nlh
, sizeof(req
));
542 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
545 return __rtnl_linkdump_req(rth
, family
);
548 int rtnl_fdb_linkdump_req_filter_fn(struct rtnl_handle
*rth
,
549 req_filter_fn_t filter_fn
)
553 struct ifinfomsg ifm
;
556 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
557 .nlh
.nlmsg_type
= RTM_GETNEIGH
,
558 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
559 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
560 .ifm
.ifi_family
= PF_BRIDGE
,
564 err
= filter_fn(&req
.nlh
, sizeof(req
));
568 return send(rth
->fd
, &req
, sizeof(req
), 0);
571 int rtnl_statsdump_req_filter(struct rtnl_handle
*rth
, int fam
, __u32 filt_mask
)
575 struct if_stats_msg ifsm
;
578 memset(&req
, 0, sizeof(req
));
579 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
580 req
.nlh
.nlmsg_type
= RTM_GETSTATS
;
581 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
582 req
.nlh
.nlmsg_pid
= 0;
583 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
584 req
.ifsm
.family
= fam
;
585 req
.ifsm
.filter_mask
= filt_mask
;
587 return send(rth
->fd
, &req
, sizeof(req
), 0);
590 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
592 return send(rth
->fd
, buf
, len
, 0);
595 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
601 status
= send(rth
->fd
, buf
, len
, 0);
605 /* Check for immediate errors */
606 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
613 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
614 h
= NLMSG_NEXT(h
, status
)) {
615 if (h
->nlmsg_type
== NLMSG_ERROR
) {
616 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
618 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
619 fprintf(stderr
, "ERROR truncated\n");
629 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
631 struct nlmsghdr nlh
= {
632 .nlmsg_len
= NLMSG_LENGTH(len
),
634 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
635 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
637 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
638 struct iovec iov
[2] = {
639 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
640 { .iov_base
= req
, .iov_len
= len
}
642 struct msghdr msg
= {
644 .msg_namelen
= sizeof(nladdr
),
649 return sendmsg(rth
->fd
, &msg
, 0);
652 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
654 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
657 .iov_len
= n
->nlmsg_len
659 struct msghdr msg
= {
661 .msg_namelen
= sizeof(nladdr
),
666 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
668 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
670 return sendmsg(rth
->fd
, &msg
, 0);
673 static int rtnl_dump_done(struct nlmsghdr
*h
)
675 int len
= *(int *)NLMSG_DATA(h
);
677 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
678 fprintf(stderr
, "DONE truncated\n");
683 /* check for any messages returned from kernel */
684 if (nl_dump_ext_ack_done(h
, len
))
694 "Error: Buffer too small for object.\n");
697 perror("RTNETLINK answers");
702 /* check for any messages returned from kernel */
703 nl_dump_ext_ack(h
, NULL
);
708 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
712 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
713 fprintf(stderr
, "ERROR truncated\n");
715 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
718 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
720 errno
== EOPNOTSUPP
))
723 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
724 perror("RTNETLINK answers");
728 static int __rtnl_recvmsg(int fd
, struct msghdr
*msg
, int flags
)
733 len
= recvmsg(fd
, msg
, flags
);
734 } while (len
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
737 fprintf(stderr
, "netlink receive error %s (%d)\n",
738 strerror(errno
), errno
);
743 fprintf(stderr
, "EOF on netlink\n");
750 static int rtnl_recvmsg(int fd
, struct msghdr
*msg
, char **answer
)
752 struct iovec
*iov
= msg
->msg_iov
;
756 iov
->iov_base
= NULL
;
759 len
= __rtnl_recvmsg(fd
, msg
, MSG_PEEK
| MSG_TRUNC
);
767 fprintf(stderr
, "malloc error: not enough buffer\n");
774 len
= __rtnl_recvmsg(fd
, msg
, 0);
788 static int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
789 const struct rtnl_dump_filter_arg
*arg
)
791 struct sockaddr_nl nladdr
;
793 struct msghdr msg
= {
795 .msg_namelen
= sizeof(nladdr
),
804 const struct rtnl_dump_filter_arg
*a
;
808 status
= rtnl_recvmsg(rth
->fd
, &msg
, &buf
);
813 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
815 for (a
= arg
; a
->filter
; a
++) {
816 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
820 while (NLMSG_OK(h
, msglen
)) {
823 h
->nlmsg_flags
&= ~a
->nc_flags
;
825 if (nladdr
.nl_pid
!= 0 ||
826 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
827 h
->nlmsg_seq
!= rth
->dump
)
830 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
833 if (h
->nlmsg_type
== NLMSG_DONE
) {
834 err
= rtnl_dump_done(h
);
841 break; /* process next filter */
844 if (h
->nlmsg_type
== NLMSG_ERROR
) {
845 rtnl_dump_error(rth
, h
);
851 err
= a
->filter(h
, a
->arg1
);
859 h
= NLMSG_NEXT(h
, msglen
);
867 "Dump was interrupted and may be inconsistent.\n");
871 if (msg
.msg_flags
& MSG_TRUNC
) {
872 fprintf(stderr
, "Message truncated\n");
876 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
882 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
883 rtnl_filter_t filter
,
884 void *arg1
, __u16 nc_flags
)
886 const struct rtnl_dump_filter_arg a
[2] = {
887 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
888 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
891 return rtnl_dump_filter_l(rth
, a
);
894 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
895 nl_ext_ack_fn_t errfn
)
897 if (nl_dump_ext_ack(h
, errfn
))
900 fprintf(stderr
, "RTNETLINK answers: %s\n",
901 strerror(-err
->error
));
905 static int __rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iov
,
906 size_t iovlen
, struct nlmsghdr
**answer
,
907 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
909 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
911 struct msghdr msg
= {
913 .msg_namelen
= sizeof(nladdr
),
915 .msg_iovlen
= iovlen
,
917 unsigned int seq
= 0;
922 for (i
= 0; i
< iovlen
; i
++) {
924 h
->nlmsg_seq
= seq
= ++rtnl
->seq
;
926 h
->nlmsg_flags
|= NLM_F_ACK
;
929 status
= sendmsg(rtnl
->fd
, &msg
, 0);
931 perror("Cannot talk to rtnetlink");
935 /* change msg to use the response iov */
941 status
= rtnl_recvmsg(rtnl
->fd
, &msg
, &buf
);
947 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
949 "sender address length == %d\n",
953 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
954 int len
= h
->nlmsg_len
;
955 int l
= len
- sizeof(*h
);
957 if (l
< 0 || len
> status
) {
958 if (msg
.msg_flags
& MSG_TRUNC
) {
959 fprintf(stderr
, "Truncated message\n");
964 "!!!malformed message: len=%d\n",
969 if (nladdr
.nl_pid
!= 0 ||
970 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
971 h
->nlmsg_seq
> seq
|| h
->nlmsg_seq
< seq
- iovlen
) {
972 /* Don't forget to skip that message. */
973 status
-= NLMSG_ALIGN(len
);
974 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
978 if (h
->nlmsg_type
== NLMSG_ERROR
) {
979 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
980 int error
= err
->error
;
982 if (l
< sizeof(struct nlmsgerr
)) {
983 fprintf(stderr
, "ERROR truncated\n");
989 /* check messages from kernel */
990 nl_dump_ext_ack(h
, errfn
);
994 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
996 rtnl_talk_error(h
, err
, errfn
);
1000 *answer
= (struct nlmsghdr
*)buf
;
1006 return error
? -i
: 0;
1010 *answer
= (struct nlmsghdr
*)buf
;
1014 fprintf(stderr
, "Unexpected reply!!!\n");
1016 status
-= NLMSG_ALIGN(len
);
1017 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
1021 if (msg
.msg_flags
& MSG_TRUNC
) {
1022 fprintf(stderr
, "Message truncated\n");
1027 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
1033 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
1034 struct nlmsghdr
**answer
,
1035 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
1037 struct iovec iov
= {
1039 .iov_len
= n
->nlmsg_len
1042 return __rtnl_talk_iov(rtnl
, &iov
, 1, answer
, show_rtnl_err
, errfn
);
1045 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
1046 struct nlmsghdr
**answer
)
1048 return __rtnl_talk(rtnl
, n
, answer
, true, NULL
);
1051 int rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iovec
, size_t iovlen
,
1052 struct nlmsghdr
**answer
)
1054 return __rtnl_talk_iov(rtnl
, iovec
, iovlen
, answer
, true, NULL
);
1057 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
1058 struct nlmsghdr
**answer
)
1060 return __rtnl_talk(rtnl
, n
, answer
, false, NULL
);
1063 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
1065 unsigned int on
= 1;
1067 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
1069 perror("NETLINK_LISTEN_ALL_NSID");
1072 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
1076 int rtnl_listen(struct rtnl_handle
*rtnl
,
1077 rtnl_listen_filter_t handler
,
1082 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
1084 struct msghdr msg
= {
1085 .msg_name
= &nladdr
,
1086 .msg_namelen
= sizeof(nladdr
),
1091 char cmsgbuf
[BUFSIZ
];
1093 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
1094 msg
.msg_control
= &cmsgbuf
;
1095 msg
.msg_controllen
= sizeof(cmsgbuf
);
1100 struct rtnl_ctrl_data ctrl
;
1101 struct cmsghdr
*cmsg
;
1103 iov
.iov_len
= sizeof(buf
);
1104 status
= recvmsg(rtnl
->fd
, &msg
, 0);
1107 if (errno
== EINTR
|| errno
== EAGAIN
)
1109 fprintf(stderr
, "netlink receive error %s (%d)\n",
1110 strerror(errno
), errno
);
1111 if (errno
== ENOBUFS
)
1116 fprintf(stderr
, "EOF on netlink\n");
1119 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
1121 "Sender address length == %d\n",
1126 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
1127 memset(&ctrl
, 0, sizeof(ctrl
));
1129 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
1130 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
1131 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
1132 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
1133 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
1134 int *data
= (int *)CMSG_DATA(cmsg
);
1140 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
1142 int len
= h
->nlmsg_len
;
1143 int l
= len
- sizeof(*h
);
1145 if (l
< 0 || len
> status
) {
1146 if (msg
.msg_flags
& MSG_TRUNC
) {
1147 fprintf(stderr
, "Truncated message\n");
1151 "!!!malformed message: len=%d\n",
1156 err
= handler(&ctrl
, h
, jarg
);
1160 status
-= NLMSG_ALIGN(len
);
1161 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
1163 if (msg
.msg_flags
& MSG_TRUNC
) {
1164 fprintf(stderr
, "Message truncated\n");
1168 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
1174 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
1179 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
1185 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
1190 perror("rtnl_from_file: fread");
1197 l
= len
- sizeof(*h
);
1199 if (l
< 0 || len
> sizeof(buf
)) {
1200 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
1205 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
1208 perror("rtnl_from_file: fread");
1212 fprintf(stderr
, "rtnl-from_file: truncated message\n");
1216 err
= handler(NULL
, h
, jarg
);
1222 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
1224 return addattr_l(n
, maxlen
, type
, NULL
, 0);
1227 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
1229 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
1232 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
1234 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
1237 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
1239 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
1242 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
1244 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
1247 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
1249 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
1252 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
1255 int len
= RTA_LENGTH(alen
);
1258 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
1260 "addattr_l ERROR: message exceeded bound of %d\n",
1264 rta
= NLMSG_TAIL(n
);
1265 rta
->rta_type
= type
;
1268 memcpy(RTA_DATA(rta
), data
, alen
);
1269 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
1273 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
1275 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
1277 "addraw_l ERROR: message exceeded bound of %d\n",
1282 memcpy(NLMSG_TAIL(n
), data
, len
);
1283 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
1284 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
1288 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
1290 struct rtattr
*nest
= NLMSG_TAIL(n
);
1292 addattr_l(n
, maxlen
, type
, NULL
, 0);
1296 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
1298 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
1299 return n
->nlmsg_len
;
1302 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
1303 const void *data
, int len
)
1305 struct rtattr
*start
= NLMSG_TAIL(n
);
1307 addattr_l(n
, maxlen
, type
, data
, len
);
1308 addattr_nest(n
, maxlen
, type
);
1312 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
1314 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
1316 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
1317 addattr_nest_end(n
, nest
);
1318 return n
->nlmsg_len
;
1321 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
1323 int len
= RTA_LENGTH(4);
1324 struct rtattr
*subrta
;
1326 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
1328 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1332 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1333 subrta
->rta_type
= type
;
1334 subrta
->rta_len
= len
;
1335 memcpy(RTA_DATA(subrta
), &data
, 4);
1336 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
1340 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
1341 const void *data
, int alen
)
1343 struct rtattr
*subrta
;
1344 int len
= RTA_LENGTH(alen
);
1346 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
1348 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1352 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1353 subrta
->rta_type
= type
;
1354 subrta
->rta_len
= len
;
1356 memcpy(RTA_DATA(subrta
), data
, alen
);
1357 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
1361 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
1363 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
1366 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
1368 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
1371 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
1373 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
1376 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
1378 struct rtattr
*nest
= RTA_TAIL(rta
);
1380 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
1381 nest
->rta_type
|= NLA_F_NESTED
;
1386 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
1388 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
1390 return rta
->rta_len
;
1393 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
1395 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
1398 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
1399 int len
, unsigned short flags
)
1401 unsigned short type
;
1403 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1404 while (RTA_OK(rta
, len
)) {
1405 type
= rta
->rta_type
& ~flags
;
1406 if ((type
<= max
) && (!tb
[type
]))
1408 rta
= RTA_NEXT(rta
, len
);
1411 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1416 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1418 while (RTA_OK(rta
, len
)) {
1419 if (rta
->rta_type
== type
)
1421 rta
= RTA_NEXT(rta
, len
);
1425 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1430 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1434 if (RTA_PAYLOAD(rta
) < len
)
1436 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1437 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1438 return parse_rtattr_nested(tb
, max
, rta
);
1440 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));