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
,
238 req_filter_fn_t filter_fn
)
242 struct ifaddrmsg ifm
;
245 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
246 .nlh
.nlmsg_type
= RTM_GETADDR
,
247 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
248 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
249 .ifm
.ifa_family
= family
,
255 err
= filter_fn(&req
.nlh
, sizeof(req
));
260 return send(rth
->fd
, &req
, sizeof(req
), 0);
263 int rtnl_addrlbldump_req(struct rtnl_handle
*rth
, int family
)
267 struct ifaddrlblmsg ifal
;
269 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrlblmsg
)),
270 .nlh
.nlmsg_type
= RTM_GETADDRLABEL
,
271 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
272 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
273 .ifal
.ifal_family
= family
,
276 return send(rth
->fd
, &req
, sizeof(req
), 0);
279 int rtnl_routedump_req(struct rtnl_handle
*rth
, int family
,
280 req_filter_fn_t filter_fn
)
287 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
)),
288 .nlh
.nlmsg_type
= RTM_GETROUTE
,
289 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
290 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
291 .rtm
.rtm_family
= family
,
297 err
= filter_fn(&req
.nlh
, sizeof(req
));
302 return send(rth
->fd
, &req
, sizeof(req
), 0);
305 int rtnl_ruledump_req(struct rtnl_handle
*rth
, int family
)
309 struct fib_rule_hdr frh
;
311 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct fib_rule_hdr
)),
312 .nlh
.nlmsg_type
= RTM_GETRULE
,
313 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
314 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
318 return send(rth
->fd
, &req
, sizeof(req
), 0);
321 int rtnl_neighdump_req(struct rtnl_handle
*rth
, int family
)
327 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
)),
328 .nlh
.nlmsg_type
= RTM_GETNEIGH
,
329 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
330 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
331 .ndm
.ndm_family
= family
,
334 return send(rth
->fd
, &req
, sizeof(req
), 0);
337 int rtnl_neightbldump_req(struct rtnl_handle
*rth
, int family
)
341 struct ndtmsg ndtmsg
;
343 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndtmsg
)),
344 .nlh
.nlmsg_type
= RTM_GETNEIGHTBL
,
345 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
346 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
347 .ndtmsg
.ndtm_family
= family
,
350 return send(rth
->fd
, &req
, sizeof(req
), 0);
353 int rtnl_mdbdump_req(struct rtnl_handle
*rth
, int family
)
357 struct br_port_msg bpm
;
359 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct br_port_msg
)),
360 .nlh
.nlmsg_type
= RTM_GETMDB
,
361 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
362 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
363 .bpm
.family
= family
,
366 return send(rth
->fd
, &req
, sizeof(req
), 0);
369 int rtnl_netconfdump_req(struct rtnl_handle
*rth
, int family
)
373 struct netconfmsg ncm
;
374 char buf
[0] __aligned(NLMSG_ALIGNTO
);
376 .nlh
.nlmsg_len
= NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct netconfmsg
))),
377 .nlh
.nlmsg_type
= RTM_GETNETCONF
,
378 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
379 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
380 .ncm
.ncm_family
= family
,
383 return send(rth
->fd
, &req
, sizeof(req
), 0);
386 int rtnl_nsiddump_req(struct rtnl_handle
*rth
, int family
)
391 char buf
[0] __aligned(NLMSG_ALIGNTO
);
393 .nlh
.nlmsg_len
= NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct rtgenmsg
))),
394 .nlh
.nlmsg_type
= RTM_GETNSID
,
395 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
396 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
397 .rtm
.rtgen_family
= family
,
400 return send(rth
->fd
, &req
, sizeof(req
), 0);
403 static int __rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
407 struct ifinfomsg ifm
;
409 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
410 .nlh
.nlmsg_type
= RTM_GETLINK
,
411 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
412 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
413 .ifm
.ifi_family
= family
,
416 return send(rth
->fd
, &req
, sizeof(req
), 0);
419 int rtnl_linkdump_req(struct rtnl_handle
*rth
, int family
)
421 if (family
== AF_UNSPEC
)
422 return rtnl_linkdump_req_filter(rth
, family
, RTEXT_FILTER_VF
);
424 return __rtnl_linkdump_req(rth
, family
);
427 int rtnl_linkdump_req_filter(struct rtnl_handle
*rth
, int family
,
430 if (family
== AF_UNSPEC
) {
433 struct ifinfomsg ifm
;
434 /* attribute has to be NLMSG aligned */
435 struct rtattr ext_req
__aligned(NLMSG_ALIGNTO
);
436 __u32 ext_filter_mask
;
438 .nlh
.nlmsg_len
= sizeof(req
),
439 .nlh
.nlmsg_type
= RTM_GETLINK
,
440 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
441 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
442 .ifm
.ifi_family
= family
,
443 .ext_req
.rta_type
= IFLA_EXT_MASK
,
444 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
445 .ext_filter_mask
= filt_mask
,
448 return send(rth
->fd
, &req
, sizeof(req
), 0);
451 return __rtnl_linkdump_req(rth
, family
);
454 int rtnl_linkdump_req_filter_fn(struct rtnl_handle
*rth
, int family
,
455 req_filter_fn_t filter_fn
)
457 if (family
== AF_UNSPEC
) {
460 struct ifinfomsg ifm
;
463 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
464 .nlh
.nlmsg_type
= RTM_GETLINK
,
465 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
466 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
467 .ifm
.ifi_family
= family
,
474 err
= filter_fn(&req
.nlh
, sizeof(req
));
478 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
481 return __rtnl_linkdump_req(rth
, family
);
484 int rtnl_statsdump_req_filter(struct rtnl_handle
*rth
, int fam
, __u32 filt_mask
)
488 struct if_stats_msg ifsm
;
491 memset(&req
, 0, sizeof(req
));
492 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
493 req
.nlh
.nlmsg_type
= RTM_GETSTATS
;
494 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
495 req
.nlh
.nlmsg_pid
= 0;
496 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
497 req
.ifsm
.family
= fam
;
498 req
.ifsm
.filter_mask
= filt_mask
;
500 return send(rth
->fd
, &req
, sizeof(req
), 0);
503 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
505 return send(rth
->fd
, buf
, len
, 0);
508 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
514 status
= send(rth
->fd
, buf
, len
, 0);
518 /* Check for immediate errors */
519 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
526 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
527 h
= NLMSG_NEXT(h
, status
)) {
528 if (h
->nlmsg_type
== NLMSG_ERROR
) {
529 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
531 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
532 fprintf(stderr
, "ERROR truncated\n");
542 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
544 struct nlmsghdr nlh
= {
545 .nlmsg_len
= NLMSG_LENGTH(len
),
547 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
548 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
550 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
551 struct iovec iov
[2] = {
552 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
553 { .iov_base
= req
, .iov_len
= len
}
555 struct msghdr msg
= {
557 .msg_namelen
= sizeof(nladdr
),
562 return sendmsg(rth
->fd
, &msg
, 0);
565 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
567 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
570 .iov_len
= n
->nlmsg_len
572 struct msghdr msg
= {
574 .msg_namelen
= sizeof(nladdr
),
579 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
581 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
583 return sendmsg(rth
->fd
, &msg
, 0);
586 static int rtnl_dump_done(struct nlmsghdr
*h
)
588 int len
= *(int *)NLMSG_DATA(h
);
590 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
591 fprintf(stderr
, "DONE truncated\n");
596 /* check for any messages returned from kernel */
597 if (nl_dump_ext_ack_done(h
, len
))
607 "Error: Buffer too small for object.\n");
610 perror("RTNETLINK answers");
615 /* check for any messages returned from kernel */
616 nl_dump_ext_ack(h
, NULL
);
621 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
625 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
626 fprintf(stderr
, "ERROR truncated\n");
628 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
631 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
633 errno
== EOPNOTSUPP
))
636 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
637 perror("RTNETLINK answers");
641 static int __rtnl_recvmsg(int fd
, struct msghdr
*msg
, int flags
)
646 len
= recvmsg(fd
, msg
, flags
);
647 } while (len
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
650 fprintf(stderr
, "netlink receive error %s (%d)\n",
651 strerror(errno
), errno
);
656 fprintf(stderr
, "EOF on netlink\n");
663 static int rtnl_recvmsg(int fd
, struct msghdr
*msg
, char **answer
)
665 struct iovec
*iov
= msg
->msg_iov
;
669 iov
->iov_base
= NULL
;
672 len
= __rtnl_recvmsg(fd
, msg
, MSG_PEEK
| MSG_TRUNC
);
678 fprintf(stderr
, "malloc error: not enough buffer\n");
685 len
= __rtnl_recvmsg(fd
, msg
, 0);
699 static int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
700 const struct rtnl_dump_filter_arg
*arg
)
702 struct sockaddr_nl nladdr
;
704 struct msghdr msg
= {
706 .msg_namelen
= sizeof(nladdr
),
715 const struct rtnl_dump_filter_arg
*a
;
719 status
= rtnl_recvmsg(rth
->fd
, &msg
, &buf
);
724 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
726 for (a
= arg
; a
->filter
; a
++) {
727 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
731 while (NLMSG_OK(h
, msglen
)) {
734 h
->nlmsg_flags
&= ~a
->nc_flags
;
736 if (nladdr
.nl_pid
!= 0 ||
737 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
738 h
->nlmsg_seq
!= rth
->dump
)
741 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
744 if (h
->nlmsg_type
== NLMSG_DONE
) {
745 err
= rtnl_dump_done(h
);
752 break; /* process next filter */
755 if (h
->nlmsg_type
== NLMSG_ERROR
) {
756 rtnl_dump_error(rth
, h
);
762 err
= a
->filter(h
, a
->arg1
);
770 h
= NLMSG_NEXT(h
, msglen
);
778 "Dump was interrupted and may be inconsistent.\n");
782 if (msg
.msg_flags
& MSG_TRUNC
) {
783 fprintf(stderr
, "Message truncated\n");
787 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
793 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
794 rtnl_filter_t filter
,
795 void *arg1
, __u16 nc_flags
)
797 const struct rtnl_dump_filter_arg a
[2] = {
798 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
799 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
802 return rtnl_dump_filter_l(rth
, a
);
805 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
806 nl_ext_ack_fn_t errfn
)
808 if (nl_dump_ext_ack(h
, errfn
))
811 fprintf(stderr
, "RTNETLINK answers: %s\n",
812 strerror(-err
->error
));
816 static int __rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iov
,
817 size_t iovlen
, struct nlmsghdr
**answer
,
818 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
820 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
822 struct msghdr msg
= {
824 .msg_namelen
= sizeof(nladdr
),
826 .msg_iovlen
= iovlen
,
828 unsigned int seq
= 0;
833 for (i
= 0; i
< iovlen
; i
++) {
835 h
->nlmsg_seq
= seq
= ++rtnl
->seq
;
837 h
->nlmsg_flags
|= NLM_F_ACK
;
840 status
= sendmsg(rtnl
->fd
, &msg
, 0);
842 perror("Cannot talk to rtnetlink");
846 /* change msg to use the response iov */
852 status
= rtnl_recvmsg(rtnl
->fd
, &msg
, &buf
);
858 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
860 "sender address length == %d\n",
864 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
865 int len
= h
->nlmsg_len
;
866 int l
= len
- sizeof(*h
);
868 if (l
< 0 || len
> status
) {
869 if (msg
.msg_flags
& MSG_TRUNC
) {
870 fprintf(stderr
, "Truncated message\n");
875 "!!!malformed message: len=%d\n",
880 if (nladdr
.nl_pid
!= 0 ||
881 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
882 h
->nlmsg_seq
> seq
|| h
->nlmsg_seq
< seq
- iovlen
) {
883 /* Don't forget to skip that message. */
884 status
-= NLMSG_ALIGN(len
);
885 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
889 if (h
->nlmsg_type
== NLMSG_ERROR
) {
890 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
891 int error
= err
->error
;
893 if (l
< sizeof(struct nlmsgerr
)) {
894 fprintf(stderr
, "ERROR truncated\n");
900 /* check messages from kernel */
901 nl_dump_ext_ack(h
, errfn
);
905 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
907 rtnl_talk_error(h
, err
, errfn
);
911 *answer
= (struct nlmsghdr
*)buf
;
917 return error
? -i
: 0;
921 *answer
= (struct nlmsghdr
*)buf
;
925 fprintf(stderr
, "Unexpected reply!!!\n");
927 status
-= NLMSG_ALIGN(len
);
928 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
932 if (msg
.msg_flags
& MSG_TRUNC
) {
933 fprintf(stderr
, "Message truncated\n");
938 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
944 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
945 struct nlmsghdr
**answer
,
946 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
950 .iov_len
= n
->nlmsg_len
953 return __rtnl_talk_iov(rtnl
, &iov
, 1, answer
, show_rtnl_err
, errfn
);
956 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
957 struct nlmsghdr
**answer
)
959 return __rtnl_talk(rtnl
, n
, answer
, true, NULL
);
962 int rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iovec
, size_t iovlen
,
963 struct nlmsghdr
**answer
)
965 return __rtnl_talk_iov(rtnl
, iovec
, iovlen
, answer
, true, NULL
);
968 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
969 struct nlmsghdr
**answer
)
971 return __rtnl_talk(rtnl
, n
, answer
, false, NULL
);
974 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
978 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
980 perror("NETLINK_LISTEN_ALL_NSID");
983 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
987 int rtnl_listen(struct rtnl_handle
*rtnl
,
988 rtnl_listen_filter_t handler
,
993 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
995 struct msghdr msg
= {
997 .msg_namelen
= sizeof(nladdr
),
1002 char cmsgbuf
[BUFSIZ
];
1004 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
1005 msg
.msg_control
= &cmsgbuf
;
1006 msg
.msg_controllen
= sizeof(cmsgbuf
);
1011 struct rtnl_ctrl_data ctrl
;
1012 struct cmsghdr
*cmsg
;
1014 iov
.iov_len
= sizeof(buf
);
1015 status
= recvmsg(rtnl
->fd
, &msg
, 0);
1018 if (errno
== EINTR
|| errno
== EAGAIN
)
1020 fprintf(stderr
, "netlink receive error %s (%d)\n",
1021 strerror(errno
), errno
);
1022 if (errno
== ENOBUFS
)
1027 fprintf(stderr
, "EOF on netlink\n");
1030 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
1032 "Sender address length == %d\n",
1037 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
1038 memset(&ctrl
, 0, sizeof(ctrl
));
1040 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
1041 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
1042 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
1043 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
1044 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
1045 int *data
= (int *)CMSG_DATA(cmsg
);
1051 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
1053 int len
= h
->nlmsg_len
;
1054 int l
= len
- sizeof(*h
);
1056 if (l
< 0 || len
> status
) {
1057 if (msg
.msg_flags
& MSG_TRUNC
) {
1058 fprintf(stderr
, "Truncated message\n");
1062 "!!!malformed message: len=%d\n",
1067 err
= handler(&ctrl
, h
, jarg
);
1071 status
-= NLMSG_ALIGN(len
);
1072 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
1074 if (msg
.msg_flags
& MSG_TRUNC
) {
1075 fprintf(stderr
, "Message truncated\n");
1079 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
1085 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
1090 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
1096 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
1101 perror("rtnl_from_file: fread");
1108 l
= len
- sizeof(*h
);
1110 if (l
< 0 || len
> sizeof(buf
)) {
1111 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
1116 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
1119 perror("rtnl_from_file: fread");
1123 fprintf(stderr
, "rtnl-from_file: truncated message\n");
1127 err
= handler(NULL
, h
, jarg
);
1133 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
1135 return addattr_l(n
, maxlen
, type
, NULL
, 0);
1138 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
1140 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
1143 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
1145 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
1148 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
1150 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
1153 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
1155 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
1158 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
1160 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
1163 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
1166 int len
= RTA_LENGTH(alen
);
1169 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
1171 "addattr_l ERROR: message exceeded bound of %d\n",
1175 rta
= NLMSG_TAIL(n
);
1176 rta
->rta_type
= type
;
1179 memcpy(RTA_DATA(rta
), data
, alen
);
1180 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
1184 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
1186 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
1188 "addraw_l ERROR: message exceeded bound of %d\n",
1193 memcpy(NLMSG_TAIL(n
), data
, len
);
1194 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
1195 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
1199 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
1201 struct rtattr
*nest
= NLMSG_TAIL(n
);
1203 addattr_l(n
, maxlen
, type
, NULL
, 0);
1207 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
1209 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
1210 return n
->nlmsg_len
;
1213 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
1214 const void *data
, int len
)
1216 struct rtattr
*start
= NLMSG_TAIL(n
);
1218 addattr_l(n
, maxlen
, type
, data
, len
);
1219 addattr_nest(n
, maxlen
, type
);
1223 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
1225 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
1227 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
1228 addattr_nest_end(n
, nest
);
1229 return n
->nlmsg_len
;
1232 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
1234 int len
= RTA_LENGTH(4);
1235 struct rtattr
*subrta
;
1237 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
1239 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1243 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1244 subrta
->rta_type
= type
;
1245 subrta
->rta_len
= len
;
1246 memcpy(RTA_DATA(subrta
), &data
, 4);
1247 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
1251 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
1252 const void *data
, int alen
)
1254 struct rtattr
*subrta
;
1255 int len
= RTA_LENGTH(alen
);
1257 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
1259 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1263 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1264 subrta
->rta_type
= type
;
1265 subrta
->rta_len
= len
;
1267 memcpy(RTA_DATA(subrta
), data
, alen
);
1268 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
1272 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
1274 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
1277 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
1279 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
1282 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
1284 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
1287 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
1289 struct rtattr
*nest
= RTA_TAIL(rta
);
1291 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
1296 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
1298 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
1300 return rta
->rta_len
;
1303 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
1305 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
1308 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
1309 int len
, unsigned short flags
)
1311 unsigned short type
;
1313 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1314 while (RTA_OK(rta
, len
)) {
1315 type
= rta
->rta_type
& ~flags
;
1316 if ((type
<= max
) && (!tb
[type
]))
1318 rta
= RTA_NEXT(rta
, len
);
1321 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1326 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1328 while (RTA_OK(rta
, len
)) {
1329 if (rta
->rta_type
== type
)
1331 rta
= RTA_NEXT(rta
, len
);
1335 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1340 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1344 if (RTA_PAYLOAD(rta
) < len
)
1346 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1347 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1348 return parse_rtattr_nested(tb
, max
, rta
);
1350 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));