2 * libnetlink.c RTnetlink service routines.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
18 #include <net/if_arp.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
25 #include <linux/fib_rules.h>
26 #include <linux/if_addrlabel.h>
27 #include <linux/if_bridge.h>
29 #include "libnetlink.h"
31 #define __aligned(x) __attribute__((aligned(x)))
34 #define SOL_NETLINK 270
38 #define MIN(a, b) ((a) < (b) ? (a) : (b))
41 int rcvbuf
= 1024 * 1024;
44 #include <libmnl/libmnl.h>
46 static const enum mnl_attr_data_type extack_policy
[NLMSGERR_ATTR_MAX
+ 1] = {
47 [NLMSGERR_ATTR_MSG
] = MNL_TYPE_NUL_STRING
,
48 [NLMSGERR_ATTR_OFFS
] = MNL_TYPE_U32
,
51 static int err_attr_cb(const struct nlattr
*attr
, void *data
)
53 const struct nlattr
**tb
= data
;
56 if (mnl_attr_type_valid(attr
, NLMSGERR_ATTR_MAX
) < 0) {
57 fprintf(stderr
, "Invalid extack attribute\n");
61 type
= mnl_attr_get_type(attr
);
62 if (mnl_attr_validate(attr
, extack_policy
[type
]) < 0) {
63 fprintf(stderr
, "extack attribute %d failed validation\n",
72 static void print_ext_ack_msg(bool is_err
, const char *msg
)
74 fprintf(stderr
, "%s: %s", is_err
? "Error" : "Warning", msg
);
75 if (msg
[strlen(msg
) - 1] != '.')
77 fprintf(stderr
, "\n");
80 /* dump netlink extended ack error message */
81 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
83 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
84 const struct nlmsgerr
*err
= mnl_nlmsg_get_payload(nlh
);
85 const struct nlmsghdr
*err_nlh
= NULL
;
86 unsigned int hlen
= sizeof(*err
);
87 const char *msg
= NULL
;
90 /* no TLVs, nothing to do here */
91 if (!(nlh
->nlmsg_flags
& NLM_F_ACK_TLVS
))
94 /* if NLM_F_CAPPED is set then the inner err msg was capped */
95 if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
96 hlen
+= mnl_nlmsg_get_payload_len(&err
->msg
);
98 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
101 if (tb
[NLMSGERR_ATTR_MSG
])
102 msg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
104 if (tb
[NLMSGERR_ATTR_OFFS
]) {
105 off
= mnl_attr_get_u32(tb
[NLMSGERR_ATTR_OFFS
]);
107 if (off
> nlh
->nlmsg_len
) {
109 "Invalid offset for NLMSGERR_ATTR_OFFS\n");
111 } else if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
116 return errfn(msg
, off
, err_nlh
);
118 if (msg
&& *msg
!= '\0') {
119 bool is_err
= !!err
->error
;
121 print_ext_ack_msg(is_err
, msg
);
122 return is_err
? 1 : 0;
128 static int nl_dump_ext_ack_done(const struct nlmsghdr
*nlh
, int error
)
130 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
131 unsigned int hlen
= sizeof(int);
132 const char *msg
= NULL
;
134 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
137 if (tb
[NLMSGERR_ATTR_MSG
])
138 msg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
140 if (msg
&& *msg
!= '\0') {
141 bool is_err
= !!error
;
143 print_ext_ack_msg(is_err
, msg
);
144 return is_err
? 1 : 0;
150 #warning "libmnl required for error support"
152 /* No extended error ack without libmnl */
153 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
158 static int nl_dump_ext_ack_done(const struct nlmsghdr
*nlh
, int error
)
164 void rtnl_close(struct rtnl_handle
*rth
)
172 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
179 memset(rth
, 0, sizeof(*rth
));
181 rth
->proto
= protocol
;
182 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
184 perror("Cannot open netlink socket");
188 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_SNDBUF
,
189 &sndbuf
, sizeof(sndbuf
)) < 0) {
194 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_RCVBUF
,
195 &rcvbuf
, sizeof(rcvbuf
)) < 0) {
200 /* Older kernels may no support extended ACK reporting */
201 setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_EXT_ACK
,
204 memset(&rth
->local
, 0, sizeof(rth
->local
));
205 rth
->local
.nl_family
= AF_NETLINK
;
206 rth
->local
.nl_groups
= subscriptions
;
208 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
,
209 sizeof(rth
->local
)) < 0) {
210 perror("Cannot bind netlink socket");
213 addr_len
= sizeof(rth
->local
);
214 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
,
216 perror("Cannot getsockname");
219 if (addr_len
!= sizeof(rth
->local
)) {
220 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
223 if (rth
->local
.nl_family
!= AF_NETLINK
) {
224 fprintf(stderr
, "Wrong address family %d\n",
225 rth
->local
.nl_family
);
228 rth
->seq
= time(NULL
);
232 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
234 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
237 int rtnl_addrdump_req(struct rtnl_handle
*rth
, int family
)
241 struct ifaddrmsg ifm
;
243 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
244 .nlh
.nlmsg_type
= RTM_GETADDR
,
245 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
246 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
247 .ifm
.ifa_family
= family
,
250 return send(rth
->fd
, &req
, sizeof(req
), 0);
253 int rtnl_addrlbldump_req(struct rtnl_handle
*rth
, int family
)
257 struct ifaddrlblmsg ifal
;
259 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrlblmsg
)),
260 .nlh
.nlmsg_type
= RTM_GETADDRLABEL
,
261 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
262 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
263 .ifal
.ifal_family
= family
,
266 return send(rth
->fd
, &req
, sizeof(req
), 0);
269 int rtnl_routedump_req(struct rtnl_handle
*rth
, int family
)
275 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
)),
276 .nlh
.nlmsg_type
= RTM_GETROUTE
,
277 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
278 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
279 .rtm
.rtm_family
= family
,
282 return send(rth
->fd
, &req
, sizeof(req
), 0);
285 int rtnl_ruledump_req(struct rtnl_handle
*rth
, int family
)
289 struct fib_rule_hdr frh
;
291 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct fib_rule_hdr
)),
292 .nlh
.nlmsg_type
= RTM_GETRULE
,
293 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
294 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
298 return send(rth
->fd
, &req
, sizeof(req
), 0);
301 int rtnl_neighdump_req(struct rtnl_handle
*rth
, int family
)
307 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
)),
308 .nlh
.nlmsg_type
= RTM_GETNEIGH
,
309 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
310 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
311 .ndm
.ndm_family
= family
,
314 return send(rth
->fd
, &req
, sizeof(req
), 0);
317 int rtnl_neightbldump_req(struct rtnl_handle
*rth
, int family
)
321 struct ndtmsg ndtmsg
;
323 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndtmsg
)),
324 .nlh
.nlmsg_type
= RTM_GETNEIGHTBL
,
325 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
326 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
327 .ndtmsg
.ndtm_family
= family
,
330 return send(rth
->fd
, &req
, sizeof(req
), 0);
333 int rtnl_mdbdump_req(struct rtnl_handle
*rth
, int family
)
337 struct br_port_msg bpm
;
339 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct br_port_msg
)),
340 .nlh
.nlmsg_type
= RTM_GETMDB
,
341 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
342 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
343 .bpm
.family
= family
,
346 return send(rth
->fd
, &req
, sizeof(req
), 0);
349 int rtnl_netconfdump_req(struct rtnl_handle
*rth
, int family
)
353 struct netconfmsg ncm
;
354 char buf
[0] __aligned(NLMSG_ALIGNTO
);
356 .nlh
.nlmsg_len
= NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct netconfmsg
))),
357 .nlh
.nlmsg_type
= RTM_GETNETCONF
,
358 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
359 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
360 .ncm
.ncm_family
= family
,
363 return send(rth
->fd
, &req
, sizeof(req
), 0);
366 int rtnl_nsiddump_req(struct rtnl_handle
*rth
, int family
)
371 char buf
[0] __aligned(NLMSG_ALIGNTO
);
373 .nlh
.nlmsg_len
= NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct rtgenmsg
))),
374 .nlh
.nlmsg_type
= RTM_GETNSID
,
375 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
376 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
377 .rtm
.rtgen_family
= family
,
380 return send(rth
->fd
, &req
, sizeof(req
), 0);
383 int rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
385 return rtnl_linkdump_req_filter(rth
, family
, RTEXT_FILTER_VF
);
388 int rtnl_linkdump_req_filter(struct rtnl_handle
*rth
, int family
,
393 struct ifinfomsg ifm
;
394 /* attribute has to be NLMSG aligned */
395 struct rtattr ext_req
__aligned(NLMSG_ALIGNTO
);
396 __u32 ext_filter_mask
;
398 .nlh
.nlmsg_len
= sizeof(req
),
399 .nlh
.nlmsg_type
= RTM_GETLINK
,
400 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
401 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
402 .ifm
.ifi_family
= family
,
403 .ext_req
.rta_type
= IFLA_EXT_MASK
,
404 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
405 .ext_filter_mask
= filt_mask
,
408 return send(rth
->fd
, &req
, sizeof(req
), 0);
411 int rtnl_linkdump_req_filter_fn(struct rtnl_handle
*rth
, int family
,
412 req_filter_fn_t filter_fn
)
416 struct ifinfomsg ifm
;
419 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
420 .nlh
.nlmsg_type
= RTM_GETLINK
,
421 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
422 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
423 .ifm
.ifi_family
= family
,
430 err
= filter_fn(&req
.nlh
, sizeof(req
));
434 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
437 int rtnl_statsdump_req_filter(struct rtnl_handle
*rth
, int fam
, __u32 filt_mask
)
441 struct if_stats_msg ifsm
;
444 memset(&req
, 0, sizeof(req
));
445 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
446 req
.nlh
.nlmsg_type
= RTM_GETSTATS
;
447 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
448 req
.nlh
.nlmsg_pid
= 0;
449 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
450 req
.ifsm
.family
= fam
;
451 req
.ifsm
.filter_mask
= filt_mask
;
453 return send(rth
->fd
, &req
, sizeof(req
), 0);
456 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
458 return send(rth
->fd
, buf
, len
, 0);
461 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
467 status
= send(rth
->fd
, buf
, len
, 0);
471 /* Check for immediate errors */
472 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
479 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
480 h
= NLMSG_NEXT(h
, status
)) {
481 if (h
->nlmsg_type
== NLMSG_ERROR
) {
482 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
484 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
485 fprintf(stderr
, "ERROR truncated\n");
495 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
497 struct nlmsghdr nlh
= {
498 .nlmsg_len
= NLMSG_LENGTH(len
),
500 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
501 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
503 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
504 struct iovec iov
[2] = {
505 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
506 { .iov_base
= req
, .iov_len
= len
}
508 struct msghdr msg
= {
510 .msg_namelen
= sizeof(nladdr
),
515 return sendmsg(rth
->fd
, &msg
, 0);
518 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
520 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
523 .iov_len
= n
->nlmsg_len
525 struct msghdr msg
= {
527 .msg_namelen
= sizeof(nladdr
),
532 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
534 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
536 return sendmsg(rth
->fd
, &msg
, 0);
539 static int rtnl_dump_done(struct nlmsghdr
*h
)
541 int len
= *(int *)NLMSG_DATA(h
);
543 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
544 fprintf(stderr
, "DONE truncated\n");
549 /* check for any messages returned from kernel */
550 if (nl_dump_ext_ack_done(h
, len
))
560 "Error: Buffer too small for object.\n");
563 perror("RTNETLINK answers");
568 /* check for any messages returned from kernel */
569 nl_dump_ext_ack(h
, NULL
);
574 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
578 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
579 fprintf(stderr
, "ERROR truncated\n");
581 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
584 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
586 errno
== EOPNOTSUPP
))
589 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
590 perror("RTNETLINK answers");
594 static int __rtnl_recvmsg(int fd
, struct msghdr
*msg
, int flags
)
599 len
= recvmsg(fd
, msg
, flags
);
600 } while (len
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
603 fprintf(stderr
, "netlink receive error %s (%d)\n",
604 strerror(errno
), errno
);
609 fprintf(stderr
, "EOF on netlink\n");
616 static int rtnl_recvmsg(int fd
, struct msghdr
*msg
, char **answer
)
618 struct iovec
*iov
= msg
->msg_iov
;
622 iov
->iov_base
= NULL
;
625 len
= __rtnl_recvmsg(fd
, msg
, MSG_PEEK
| MSG_TRUNC
);
631 fprintf(stderr
, "malloc error: not enough buffer\n");
638 len
= __rtnl_recvmsg(fd
, msg
, 0);
652 static int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
653 const struct rtnl_dump_filter_arg
*arg
)
655 struct sockaddr_nl nladdr
;
657 struct msghdr msg
= {
659 .msg_namelen
= sizeof(nladdr
),
668 const struct rtnl_dump_filter_arg
*a
;
672 status
= rtnl_recvmsg(rth
->fd
, &msg
, &buf
);
677 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
679 for (a
= arg
; a
->filter
; a
++) {
680 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
684 while (NLMSG_OK(h
, msglen
)) {
687 h
->nlmsg_flags
&= ~a
->nc_flags
;
689 if (nladdr
.nl_pid
!= 0 ||
690 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
691 h
->nlmsg_seq
!= rth
->dump
)
694 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
697 if (h
->nlmsg_type
== NLMSG_DONE
) {
698 err
= rtnl_dump_done(h
);
705 break; /* process next filter */
708 if (h
->nlmsg_type
== NLMSG_ERROR
) {
709 rtnl_dump_error(rth
, h
);
715 err
= a
->filter(h
, a
->arg1
);
723 h
= NLMSG_NEXT(h
, msglen
);
731 "Dump was interrupted and may be inconsistent.\n");
735 if (msg
.msg_flags
& MSG_TRUNC
) {
736 fprintf(stderr
, "Message truncated\n");
740 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
746 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
747 rtnl_filter_t filter
,
748 void *arg1
, __u16 nc_flags
)
750 const struct rtnl_dump_filter_arg a
[2] = {
751 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
752 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
755 return rtnl_dump_filter_l(rth
, a
);
758 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
759 nl_ext_ack_fn_t errfn
)
761 if (nl_dump_ext_ack(h
, errfn
))
764 fprintf(stderr
, "RTNETLINK answers: %s\n",
765 strerror(-err
->error
));
769 static int __rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iov
,
770 size_t iovlen
, struct nlmsghdr
**answer
,
771 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
773 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
775 struct msghdr msg
= {
777 .msg_namelen
= sizeof(nladdr
),
779 .msg_iovlen
= iovlen
,
781 unsigned int seq
= 0;
786 for (i
= 0; i
< iovlen
; i
++) {
788 h
->nlmsg_seq
= seq
= ++rtnl
->seq
;
790 h
->nlmsg_flags
|= NLM_F_ACK
;
793 status
= sendmsg(rtnl
->fd
, &msg
, 0);
795 perror("Cannot talk to rtnetlink");
799 /* change msg to use the response iov */
805 status
= rtnl_recvmsg(rtnl
->fd
, &msg
, &buf
);
811 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
813 "sender address length == %d\n",
817 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
818 int len
= h
->nlmsg_len
;
819 int l
= len
- sizeof(*h
);
821 if (l
< 0 || len
> status
) {
822 if (msg
.msg_flags
& MSG_TRUNC
) {
823 fprintf(stderr
, "Truncated message\n");
828 "!!!malformed message: len=%d\n",
833 if (nladdr
.nl_pid
!= 0 ||
834 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
835 h
->nlmsg_seq
> seq
|| h
->nlmsg_seq
< seq
- iovlen
) {
836 /* Don't forget to skip that message. */
837 status
-= NLMSG_ALIGN(len
);
838 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
842 if (h
->nlmsg_type
== NLMSG_ERROR
) {
843 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
844 int error
= err
->error
;
846 if (l
< sizeof(struct nlmsgerr
)) {
847 fprintf(stderr
, "ERROR truncated\n");
853 /* check messages from kernel */
854 nl_dump_ext_ack(h
, errfn
);
858 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
860 rtnl_talk_error(h
, err
, errfn
);
864 *answer
= (struct nlmsghdr
*)buf
;
870 return error
? -i
: 0;
874 *answer
= (struct nlmsghdr
*)buf
;
878 fprintf(stderr
, "Unexpected reply!!!\n");
880 status
-= NLMSG_ALIGN(len
);
881 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
885 if (msg
.msg_flags
& MSG_TRUNC
) {
886 fprintf(stderr
, "Message truncated\n");
891 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
897 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
898 struct nlmsghdr
**answer
,
899 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
903 .iov_len
= n
->nlmsg_len
906 return __rtnl_talk_iov(rtnl
, &iov
, 1, answer
, show_rtnl_err
, errfn
);
909 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
910 struct nlmsghdr
**answer
)
912 return __rtnl_talk(rtnl
, n
, answer
, true, NULL
);
915 int rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iovec
, size_t iovlen
,
916 struct nlmsghdr
**answer
)
918 return __rtnl_talk_iov(rtnl
, iovec
, iovlen
, answer
, true, NULL
);
921 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
922 struct nlmsghdr
**answer
)
924 return __rtnl_talk(rtnl
, n
, answer
, false, NULL
);
927 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
931 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
933 perror("NETLINK_LISTEN_ALL_NSID");
936 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
940 int rtnl_listen(struct rtnl_handle
*rtnl
,
941 rtnl_listen_filter_t handler
,
946 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
948 struct msghdr msg
= {
950 .msg_namelen
= sizeof(nladdr
),
955 char cmsgbuf
[BUFSIZ
];
957 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
958 msg
.msg_control
= &cmsgbuf
;
959 msg
.msg_controllen
= sizeof(cmsgbuf
);
964 struct rtnl_ctrl_data ctrl
;
965 struct cmsghdr
*cmsg
;
967 iov
.iov_len
= sizeof(buf
);
968 status
= recvmsg(rtnl
->fd
, &msg
, 0);
971 if (errno
== EINTR
|| errno
== EAGAIN
)
973 fprintf(stderr
, "netlink receive error %s (%d)\n",
974 strerror(errno
), errno
);
975 if (errno
== ENOBUFS
)
980 fprintf(stderr
, "EOF on netlink\n");
983 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
985 "Sender address length == %d\n",
990 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
991 memset(&ctrl
, 0, sizeof(ctrl
));
993 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
994 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
995 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
996 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
997 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
998 int *data
= (int *)CMSG_DATA(cmsg
);
1004 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
1006 int len
= h
->nlmsg_len
;
1007 int l
= len
- sizeof(*h
);
1009 if (l
< 0 || len
> status
) {
1010 if (msg
.msg_flags
& MSG_TRUNC
) {
1011 fprintf(stderr
, "Truncated message\n");
1015 "!!!malformed message: len=%d\n",
1020 err
= handler(&ctrl
, h
, jarg
);
1024 status
-= NLMSG_ALIGN(len
);
1025 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
1027 if (msg
.msg_flags
& MSG_TRUNC
) {
1028 fprintf(stderr
, "Message truncated\n");
1032 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
1038 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
1043 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
1049 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
1054 perror("rtnl_from_file: fread");
1061 l
= len
- sizeof(*h
);
1063 if (l
< 0 || len
> sizeof(buf
)) {
1064 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
1069 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
1072 perror("rtnl_from_file: fread");
1076 fprintf(stderr
, "rtnl-from_file: truncated message\n");
1080 err
= handler(NULL
, h
, jarg
);
1086 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
1088 return addattr_l(n
, maxlen
, type
, NULL
, 0);
1091 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
1093 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
1096 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
1098 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
1101 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
1103 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
1106 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
1108 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
1111 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
1113 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
1116 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
1119 int len
= RTA_LENGTH(alen
);
1122 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
1124 "addattr_l ERROR: message exceeded bound of %d\n",
1128 rta
= NLMSG_TAIL(n
);
1129 rta
->rta_type
= type
;
1132 memcpy(RTA_DATA(rta
), data
, alen
);
1133 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
1137 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
1139 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
1141 "addraw_l ERROR: message exceeded bound of %d\n",
1146 memcpy(NLMSG_TAIL(n
), data
, len
);
1147 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
1148 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
1152 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
1154 struct rtattr
*nest
= NLMSG_TAIL(n
);
1156 addattr_l(n
, maxlen
, type
, NULL
, 0);
1160 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
1162 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
1163 return n
->nlmsg_len
;
1166 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
1167 const void *data
, int len
)
1169 struct rtattr
*start
= NLMSG_TAIL(n
);
1171 addattr_l(n
, maxlen
, type
, data
, len
);
1172 addattr_nest(n
, maxlen
, type
);
1176 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
1178 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
1180 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
1181 addattr_nest_end(n
, nest
);
1182 return n
->nlmsg_len
;
1185 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
1187 int len
= RTA_LENGTH(4);
1188 struct rtattr
*subrta
;
1190 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
1192 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1196 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1197 subrta
->rta_type
= type
;
1198 subrta
->rta_len
= len
;
1199 memcpy(RTA_DATA(subrta
), &data
, 4);
1200 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
1204 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
1205 const void *data
, int alen
)
1207 struct rtattr
*subrta
;
1208 int len
= RTA_LENGTH(alen
);
1210 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
1212 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1216 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1217 subrta
->rta_type
= type
;
1218 subrta
->rta_len
= len
;
1220 memcpy(RTA_DATA(subrta
), data
, alen
);
1221 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
1225 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
1227 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
1230 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
1232 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
1235 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
1237 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
1240 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
1242 struct rtattr
*nest
= RTA_TAIL(rta
);
1244 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
1249 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
1251 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
1253 return rta
->rta_len
;
1256 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
1258 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
1261 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
1262 int len
, unsigned short flags
)
1264 unsigned short type
;
1266 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1267 while (RTA_OK(rta
, len
)) {
1268 type
= rta
->rta_type
& ~flags
;
1269 if ((type
<= max
) && (!tb
[type
]))
1271 rta
= RTA_NEXT(rta
, len
);
1274 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1279 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1281 while (RTA_OK(rta
, len
)) {
1282 if (rta
->rta_type
== type
)
1284 rta
= RTA_NEXT(rta
, len
);
1288 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1293 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1297 if (RTA_PAYLOAD(rta
) < len
)
1299 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1300 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1301 return parse_rtattr_nested(tb
, max
, rta
);
1303 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));