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 /* Older kernels may not support strict dump and filtering */
165 void rtnl_set_strict_dump(struct rtnl_handle
*rth
)
169 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_GET_STRICT_CHK
,
170 &one
, sizeof(one
)) < 0)
173 rth
->flags
|= RTNL_HANDLE_F_STRICT_CHK
;
176 void rtnl_close(struct rtnl_handle
*rth
)
184 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
191 memset(rth
, 0, sizeof(*rth
));
193 rth
->proto
= protocol
;
194 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
196 perror("Cannot open netlink socket");
200 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_SNDBUF
,
201 &sndbuf
, sizeof(sndbuf
)) < 0) {
206 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_RCVBUF
,
207 &rcvbuf
, sizeof(rcvbuf
)) < 0) {
212 /* Older kernels may no support extended ACK reporting */
213 setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_EXT_ACK
,
216 memset(&rth
->local
, 0, sizeof(rth
->local
));
217 rth
->local
.nl_family
= AF_NETLINK
;
218 rth
->local
.nl_groups
= subscriptions
;
220 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
,
221 sizeof(rth
->local
)) < 0) {
222 perror("Cannot bind netlink socket");
225 addr_len
= sizeof(rth
->local
);
226 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
,
228 perror("Cannot getsockname");
231 if (addr_len
!= sizeof(rth
->local
)) {
232 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
235 if (rth
->local
.nl_family
!= AF_NETLINK
) {
236 fprintf(stderr
, "Wrong address family %d\n",
237 rth
->local
.nl_family
);
240 rth
->seq
= time(NULL
);
244 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
246 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
249 int rtnl_addrdump_req(struct rtnl_handle
*rth
, int family
,
250 req_filter_fn_t filter_fn
)
254 struct ifaddrmsg ifm
;
257 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
258 .nlh
.nlmsg_type
= RTM_GETADDR
,
259 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
260 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
261 .ifm
.ifa_family
= family
,
267 err
= filter_fn(&req
.nlh
, sizeof(req
));
272 return send(rth
->fd
, &req
, sizeof(req
), 0);
275 int rtnl_addrlbldump_req(struct rtnl_handle
*rth
, int family
)
279 struct ifaddrlblmsg ifal
;
281 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrlblmsg
)),
282 .nlh
.nlmsg_type
= RTM_GETADDRLABEL
,
283 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
284 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
285 .ifal
.ifal_family
= family
,
288 return send(rth
->fd
, &req
, sizeof(req
), 0);
291 int rtnl_routedump_req(struct rtnl_handle
*rth
, int family
,
292 req_filter_fn_t filter_fn
)
299 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
)),
300 .nlh
.nlmsg_type
= RTM_GETROUTE
,
301 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
302 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
303 .rtm
.rtm_family
= family
,
309 err
= filter_fn(&req
.nlh
, sizeof(req
));
314 return send(rth
->fd
, &req
, sizeof(req
), 0);
317 int rtnl_ruledump_req(struct rtnl_handle
*rth
, int family
)
321 struct fib_rule_hdr frh
;
323 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct fib_rule_hdr
)),
324 .nlh
.nlmsg_type
= RTM_GETRULE
,
325 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
326 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
330 return send(rth
->fd
, &req
, sizeof(req
), 0);
333 int rtnl_neighdump_req(struct rtnl_handle
*rth
, int family
,
334 req_filter_fn_t filter_fn
)
341 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
)),
342 .nlh
.nlmsg_type
= RTM_GETNEIGH
,
343 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
344 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
345 .ndm
.ndm_family
= family
,
351 err
= filter_fn(&req
.nlh
, sizeof(req
));
356 return send(rth
->fd
, &req
, sizeof(req
), 0);
359 int rtnl_neightbldump_req(struct rtnl_handle
*rth
, int family
)
363 struct ndtmsg ndtmsg
;
365 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndtmsg
)),
366 .nlh
.nlmsg_type
= RTM_GETNEIGHTBL
,
367 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
368 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
369 .ndtmsg
.ndtm_family
= family
,
372 return send(rth
->fd
, &req
, sizeof(req
), 0);
375 int rtnl_mdbdump_req(struct rtnl_handle
*rth
, int family
)
379 struct br_port_msg bpm
;
381 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct br_port_msg
)),
382 .nlh
.nlmsg_type
= RTM_GETMDB
,
383 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
384 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
385 .bpm
.family
= family
,
388 return send(rth
->fd
, &req
, sizeof(req
), 0);
391 int rtnl_netconfdump_req(struct rtnl_handle
*rth
, int family
)
395 struct netconfmsg ncm
;
396 char buf
[0] __aligned(NLMSG_ALIGNTO
);
398 .nlh
.nlmsg_len
= NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct netconfmsg
))),
399 .nlh
.nlmsg_type
= RTM_GETNETCONF
,
400 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
401 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
402 .ncm
.ncm_family
= family
,
405 return send(rth
->fd
, &req
, sizeof(req
), 0);
408 int rtnl_nsiddump_req(struct rtnl_handle
*rth
, int family
)
413 char buf
[0] __aligned(NLMSG_ALIGNTO
);
415 .nlh
.nlmsg_len
= NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct rtgenmsg
))),
416 .nlh
.nlmsg_type
= RTM_GETNSID
,
417 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
418 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
419 .rtm
.rtgen_family
= family
,
422 return send(rth
->fd
, &req
, sizeof(req
), 0);
425 static int __rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
429 struct ifinfomsg ifm
;
431 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
432 .nlh
.nlmsg_type
= RTM_GETLINK
,
433 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
434 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
435 .ifm
.ifi_family
= family
,
438 return send(rth
->fd
, &req
, sizeof(req
), 0);
441 int rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
443 if (family
== AF_UNSPEC
)
444 return rtnl_linkdump_req_filter(rth
, family
, RTEXT_FILTER_VF
);
446 return __rtnl_linkdump_req(rth
, family
);
449 int rtnl_linkdump_req_filter(struct rtnl_handle
*rth
, int family
,
452 if (family
== AF_UNSPEC
|| family
== AF_BRIDGE
) {
455 struct ifinfomsg ifm
;
456 /* attribute has to be NLMSG aligned */
457 struct rtattr ext_req
__aligned(NLMSG_ALIGNTO
);
458 __u32 ext_filter_mask
;
460 .nlh
.nlmsg_len
= sizeof(req
),
461 .nlh
.nlmsg_type
= RTM_GETLINK
,
462 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
463 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
464 .ifm
.ifi_family
= family
,
465 .ext_req
.rta_type
= IFLA_EXT_MASK
,
466 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
467 .ext_filter_mask
= filt_mask
,
470 return send(rth
->fd
, &req
, sizeof(req
), 0);
473 return __rtnl_linkdump_req(rth
, family
);
476 int rtnl_linkdump_req_filter_fn(struct rtnl_handle
*rth
, int family
,
477 req_filter_fn_t filter_fn
)
479 if (family
== AF_UNSPEC
|| family
== AF_PACKET
) {
482 struct ifinfomsg ifm
;
485 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
486 .nlh
.nlmsg_type
= RTM_GETLINK
,
487 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
488 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
489 .ifm
.ifi_family
= family
,
496 err
= filter_fn(&req
.nlh
, sizeof(req
));
500 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
503 return __rtnl_linkdump_req(rth
, family
);
506 int rtnl_fdb_linkdump_req_filter_fn(struct rtnl_handle
*rth
,
507 req_filter_fn_t filter_fn
)
511 struct ifinfomsg ifm
;
514 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
515 .nlh
.nlmsg_type
= RTM_GETNEIGH
,
516 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
517 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
518 .ifm
.ifi_family
= PF_BRIDGE
,
522 err
= filter_fn(&req
.nlh
, sizeof(req
));
526 return send(rth
->fd
, &req
, sizeof(req
), 0);
529 int rtnl_statsdump_req_filter(struct rtnl_handle
*rth
, int fam
, __u32 filt_mask
)
533 struct if_stats_msg ifsm
;
536 memset(&req
, 0, sizeof(req
));
537 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
538 req
.nlh
.nlmsg_type
= RTM_GETSTATS
;
539 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
540 req
.nlh
.nlmsg_pid
= 0;
541 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
542 req
.ifsm
.family
= fam
;
543 req
.ifsm
.filter_mask
= filt_mask
;
545 return send(rth
->fd
, &req
, sizeof(req
), 0);
548 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
550 return send(rth
->fd
, buf
, len
, 0);
553 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
559 status
= send(rth
->fd
, buf
, len
, 0);
563 /* Check for immediate errors */
564 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
571 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
572 h
= NLMSG_NEXT(h
, status
)) {
573 if (h
->nlmsg_type
== NLMSG_ERROR
) {
574 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
576 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
577 fprintf(stderr
, "ERROR truncated\n");
587 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
589 struct nlmsghdr nlh
= {
590 .nlmsg_len
= NLMSG_LENGTH(len
),
592 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
593 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
595 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
596 struct iovec iov
[2] = {
597 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
598 { .iov_base
= req
, .iov_len
= len
}
600 struct msghdr msg
= {
602 .msg_namelen
= sizeof(nladdr
),
607 return sendmsg(rth
->fd
, &msg
, 0);
610 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
612 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
615 .iov_len
= n
->nlmsg_len
617 struct msghdr msg
= {
619 .msg_namelen
= sizeof(nladdr
),
624 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
626 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
628 return sendmsg(rth
->fd
, &msg
, 0);
631 static int rtnl_dump_done(struct nlmsghdr
*h
)
633 int len
= *(int *)NLMSG_DATA(h
);
635 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
636 fprintf(stderr
, "DONE truncated\n");
641 /* check for any messages returned from kernel */
642 if (nl_dump_ext_ack_done(h
, len
))
652 "Error: Buffer too small for object.\n");
655 perror("RTNETLINK answers");
660 /* check for any messages returned from kernel */
661 nl_dump_ext_ack(h
, NULL
);
666 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
670 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
671 fprintf(stderr
, "ERROR truncated\n");
673 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
676 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
678 errno
== EOPNOTSUPP
))
681 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
682 perror("RTNETLINK answers");
686 static int __rtnl_recvmsg(int fd
, struct msghdr
*msg
, int flags
)
691 len
= recvmsg(fd
, msg
, flags
);
692 } while (len
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
695 fprintf(stderr
, "netlink receive error %s (%d)\n",
696 strerror(errno
), errno
);
701 fprintf(stderr
, "EOF on netlink\n");
708 static int rtnl_recvmsg(int fd
, struct msghdr
*msg
, char **answer
)
710 struct iovec
*iov
= msg
->msg_iov
;
714 iov
->iov_base
= NULL
;
717 len
= __rtnl_recvmsg(fd
, msg
, MSG_PEEK
| MSG_TRUNC
);
725 fprintf(stderr
, "malloc error: not enough buffer\n");
732 len
= __rtnl_recvmsg(fd
, msg
, 0);
746 static int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
747 const struct rtnl_dump_filter_arg
*arg
)
749 struct sockaddr_nl nladdr
;
751 struct msghdr msg
= {
753 .msg_namelen
= sizeof(nladdr
),
762 const struct rtnl_dump_filter_arg
*a
;
766 status
= rtnl_recvmsg(rth
->fd
, &msg
, &buf
);
771 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
773 for (a
= arg
; a
->filter
; a
++) {
774 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
778 while (NLMSG_OK(h
, msglen
)) {
781 h
->nlmsg_flags
&= ~a
->nc_flags
;
783 if (nladdr
.nl_pid
!= 0 ||
784 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
785 h
->nlmsg_seq
!= rth
->dump
)
788 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
791 if (h
->nlmsg_type
== NLMSG_DONE
) {
792 err
= rtnl_dump_done(h
);
799 break; /* process next filter */
802 if (h
->nlmsg_type
== NLMSG_ERROR
) {
803 rtnl_dump_error(rth
, h
);
809 err
= a
->filter(h
, a
->arg1
);
817 h
= NLMSG_NEXT(h
, msglen
);
825 "Dump was interrupted and may be inconsistent.\n");
829 if (msg
.msg_flags
& MSG_TRUNC
) {
830 fprintf(stderr
, "Message truncated\n");
834 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
840 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
841 rtnl_filter_t filter
,
842 void *arg1
, __u16 nc_flags
)
844 const struct rtnl_dump_filter_arg a
[2] = {
845 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
846 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
849 return rtnl_dump_filter_l(rth
, a
);
852 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
853 nl_ext_ack_fn_t errfn
)
855 if (nl_dump_ext_ack(h
, errfn
))
858 fprintf(stderr
, "RTNETLINK answers: %s\n",
859 strerror(-err
->error
));
863 static int __rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iov
,
864 size_t iovlen
, struct nlmsghdr
**answer
,
865 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
867 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
869 struct msghdr msg
= {
871 .msg_namelen
= sizeof(nladdr
),
873 .msg_iovlen
= iovlen
,
875 unsigned int seq
= 0;
880 for (i
= 0; i
< iovlen
; i
++) {
882 h
->nlmsg_seq
= seq
= ++rtnl
->seq
;
884 h
->nlmsg_flags
|= NLM_F_ACK
;
887 status
= sendmsg(rtnl
->fd
, &msg
, 0);
889 perror("Cannot talk to rtnetlink");
893 /* change msg to use the response iov */
899 status
= rtnl_recvmsg(rtnl
->fd
, &msg
, &buf
);
905 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
907 "sender address length == %d\n",
911 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
912 int len
= h
->nlmsg_len
;
913 int l
= len
- sizeof(*h
);
915 if (l
< 0 || len
> status
) {
916 if (msg
.msg_flags
& MSG_TRUNC
) {
917 fprintf(stderr
, "Truncated message\n");
922 "!!!malformed message: len=%d\n",
927 if (nladdr
.nl_pid
!= 0 ||
928 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
929 h
->nlmsg_seq
> seq
|| h
->nlmsg_seq
< seq
- iovlen
) {
930 /* Don't forget to skip that message. */
931 status
-= NLMSG_ALIGN(len
);
932 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
936 if (h
->nlmsg_type
== NLMSG_ERROR
) {
937 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
938 int error
= err
->error
;
940 if (l
< sizeof(struct nlmsgerr
)) {
941 fprintf(stderr
, "ERROR truncated\n");
947 /* check messages from kernel */
948 nl_dump_ext_ack(h
, errfn
);
952 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
954 rtnl_talk_error(h
, err
, errfn
);
958 *answer
= (struct nlmsghdr
*)buf
;
964 return error
? -i
: 0;
968 *answer
= (struct nlmsghdr
*)buf
;
972 fprintf(stderr
, "Unexpected reply!!!\n");
974 status
-= NLMSG_ALIGN(len
);
975 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
979 if (msg
.msg_flags
& MSG_TRUNC
) {
980 fprintf(stderr
, "Message truncated\n");
985 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
991 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
992 struct nlmsghdr
**answer
,
993 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
997 .iov_len
= n
->nlmsg_len
1000 return __rtnl_talk_iov(rtnl
, &iov
, 1, answer
, show_rtnl_err
, errfn
);
1003 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
1004 struct nlmsghdr
**answer
)
1006 return __rtnl_talk(rtnl
, n
, answer
, true, NULL
);
1009 int rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iovec
, size_t iovlen
,
1010 struct nlmsghdr
**answer
)
1012 return __rtnl_talk_iov(rtnl
, iovec
, iovlen
, answer
, true, NULL
);
1015 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
1016 struct nlmsghdr
**answer
)
1018 return __rtnl_talk(rtnl
, n
, answer
, false, NULL
);
1021 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
1023 unsigned int on
= 1;
1025 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
1027 perror("NETLINK_LISTEN_ALL_NSID");
1030 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
1034 int rtnl_listen(struct rtnl_handle
*rtnl
,
1035 rtnl_listen_filter_t handler
,
1040 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
1042 struct msghdr msg
= {
1043 .msg_name
= &nladdr
,
1044 .msg_namelen
= sizeof(nladdr
),
1049 char cmsgbuf
[BUFSIZ
];
1051 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
1052 msg
.msg_control
= &cmsgbuf
;
1053 msg
.msg_controllen
= sizeof(cmsgbuf
);
1058 struct rtnl_ctrl_data ctrl
;
1059 struct cmsghdr
*cmsg
;
1061 iov
.iov_len
= sizeof(buf
);
1062 status
= recvmsg(rtnl
->fd
, &msg
, 0);
1065 if (errno
== EINTR
|| errno
== EAGAIN
)
1067 fprintf(stderr
, "netlink receive error %s (%d)\n",
1068 strerror(errno
), errno
);
1069 if (errno
== ENOBUFS
)
1074 fprintf(stderr
, "EOF on netlink\n");
1077 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
1079 "Sender address length == %d\n",
1084 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
1085 memset(&ctrl
, 0, sizeof(ctrl
));
1087 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
1088 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
1089 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
1090 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
1091 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
1092 int *data
= (int *)CMSG_DATA(cmsg
);
1098 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
1100 int len
= h
->nlmsg_len
;
1101 int l
= len
- sizeof(*h
);
1103 if (l
< 0 || len
> status
) {
1104 if (msg
.msg_flags
& MSG_TRUNC
) {
1105 fprintf(stderr
, "Truncated message\n");
1109 "!!!malformed message: len=%d\n",
1114 err
= handler(&ctrl
, h
, jarg
);
1118 status
-= NLMSG_ALIGN(len
);
1119 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
1121 if (msg
.msg_flags
& MSG_TRUNC
) {
1122 fprintf(stderr
, "Message truncated\n");
1126 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
1132 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
1137 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
1143 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
1148 perror("rtnl_from_file: fread");
1155 l
= len
- sizeof(*h
);
1157 if (l
< 0 || len
> sizeof(buf
)) {
1158 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
1163 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
1166 perror("rtnl_from_file: fread");
1170 fprintf(stderr
, "rtnl-from_file: truncated message\n");
1174 err
= handler(NULL
, h
, jarg
);
1180 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
1182 return addattr_l(n
, maxlen
, type
, NULL
, 0);
1185 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
1187 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
1190 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
1192 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
1195 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
1197 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
1200 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
1202 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
1205 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
1207 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
1210 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
1213 int len
= RTA_LENGTH(alen
);
1216 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
1218 "addattr_l ERROR: message exceeded bound of %d\n",
1222 rta
= NLMSG_TAIL(n
);
1223 rta
->rta_type
= type
;
1226 memcpy(RTA_DATA(rta
), data
, alen
);
1227 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
1231 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
1233 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
1235 "addraw_l ERROR: message exceeded bound of %d\n",
1240 memcpy(NLMSG_TAIL(n
), data
, len
);
1241 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
1242 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
1246 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
1248 struct rtattr
*nest
= NLMSG_TAIL(n
);
1250 addattr_l(n
, maxlen
, type
, NULL
, 0);
1254 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
1256 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
1257 return n
->nlmsg_len
;
1260 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
1261 const void *data
, int len
)
1263 struct rtattr
*start
= NLMSG_TAIL(n
);
1265 addattr_l(n
, maxlen
, type
, data
, len
);
1266 addattr_nest(n
, maxlen
, type
);
1270 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
1272 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
1274 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
1275 addattr_nest_end(n
, nest
);
1276 return n
->nlmsg_len
;
1279 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
1281 int len
= RTA_LENGTH(4);
1282 struct rtattr
*subrta
;
1284 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
1286 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1290 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1291 subrta
->rta_type
= type
;
1292 subrta
->rta_len
= len
;
1293 memcpy(RTA_DATA(subrta
), &data
, 4);
1294 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
1298 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
1299 const void *data
, int alen
)
1301 struct rtattr
*subrta
;
1302 int len
= RTA_LENGTH(alen
);
1304 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
1306 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1310 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1311 subrta
->rta_type
= type
;
1312 subrta
->rta_len
= len
;
1314 memcpy(RTA_DATA(subrta
), data
, alen
);
1315 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
1319 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
1321 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
1324 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
1326 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
1329 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
1331 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
1334 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
1336 struct rtattr
*nest
= RTA_TAIL(rta
);
1338 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
1339 nest
->rta_type
|= NLA_F_NESTED
;
1344 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
1346 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
1348 return rta
->rta_len
;
1351 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
1353 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
1356 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
1357 int len
, unsigned short flags
)
1359 unsigned short type
;
1361 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1362 while (RTA_OK(rta
, len
)) {
1363 type
= rta
->rta_type
& ~flags
;
1364 if ((type
<= max
) && (!tb
[type
]))
1366 rta
= RTA_NEXT(rta
, len
);
1369 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1374 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1376 while (RTA_OK(rta
, len
)) {
1377 if (rta
->rta_type
== type
)
1379 rta
= RTA_NEXT(rta
, len
);
1383 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1388 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1392 if (RTA_PAYLOAD(rta
) < len
)
1394 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1395 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1396 return parse_rtattr_nested(tb
, max
, rta
);
1398 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));