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"
32 #define SOL_NETLINK 270
36 #define MIN(a, b) ((a) < (b) ? (a) : (b))
39 int rcvbuf
= 1024 * 1024;
42 #include <libmnl/libmnl.h>
44 static const enum mnl_attr_data_type extack_policy
[NLMSGERR_ATTR_MAX
+ 1] = {
45 [NLMSGERR_ATTR_MSG
] = MNL_TYPE_NUL_STRING
,
46 [NLMSGERR_ATTR_OFFS
] = MNL_TYPE_U32
,
49 static int err_attr_cb(const struct nlattr
*attr
, void *data
)
51 const struct nlattr
**tb
= data
;
54 if (mnl_attr_type_valid(attr
, NLMSGERR_ATTR_MAX
) < 0) {
55 fprintf(stderr
, "Invalid extack attribute\n");
59 type
= mnl_attr_get_type(attr
);
60 if (mnl_attr_validate(attr
, extack_policy
[type
]) < 0) {
61 fprintf(stderr
, "extack attribute %d failed validation\n",
70 /* dump netlink extended ack error message */
71 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
73 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
74 const struct nlmsgerr
*err
= mnl_nlmsg_get_payload(nlh
);
75 const struct nlmsghdr
*err_nlh
= NULL
;
76 unsigned int hlen
= sizeof(*err
);
77 const char *msg
= NULL
;
80 /* no TLVs, nothing to do here */
81 if (!(nlh
->nlmsg_flags
& NLM_F_ACK_TLVS
))
84 /* if NLM_F_CAPPED is set then the inner err msg was capped */
85 if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
86 hlen
+= mnl_nlmsg_get_payload_len(&err
->msg
);
88 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
91 if (tb
[NLMSGERR_ATTR_MSG
])
92 msg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
94 if (tb
[NLMSGERR_ATTR_OFFS
]) {
95 off
= mnl_attr_get_u32(tb
[NLMSGERR_ATTR_OFFS
]);
97 if (off
> nlh
->nlmsg_len
) {
99 "Invalid offset for NLMSGERR_ATTR_OFFS\n");
101 } else if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
106 return errfn(msg
, off
, err_nlh
);
108 if (msg
&& *msg
!= '\0') {
109 bool is_err
= !!err
->error
;
111 fprintf(stderr
, "%s: %s",
112 is_err
? "Error" : "Warning", msg
);
113 if (msg
[strlen(msg
) - 1] != '.')
114 fprintf(stderr
, ".");
115 fprintf(stderr
, "\n");
117 return is_err
? 1 : 0;
123 #warning "libmnl required for error support"
125 /* No extended error ack without libmnl */
126 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
132 void rtnl_close(struct rtnl_handle
*rth
)
140 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
147 memset(rth
, 0, sizeof(*rth
));
149 rth
->proto
= protocol
;
150 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
152 perror("Cannot open netlink socket");
156 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_SNDBUF
,
157 &sndbuf
, sizeof(sndbuf
)) < 0) {
162 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_RCVBUF
,
163 &rcvbuf
, sizeof(rcvbuf
)) < 0) {
168 /* Older kernels may no support extended ACK reporting */
169 setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_EXT_ACK
,
172 memset(&rth
->local
, 0, sizeof(rth
->local
));
173 rth
->local
.nl_family
= AF_NETLINK
;
174 rth
->local
.nl_groups
= subscriptions
;
176 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
,
177 sizeof(rth
->local
)) < 0) {
178 perror("Cannot bind netlink socket");
181 addr_len
= sizeof(rth
->local
);
182 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
,
184 perror("Cannot getsockname");
187 if (addr_len
!= sizeof(rth
->local
)) {
188 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
191 if (rth
->local
.nl_family
!= AF_NETLINK
) {
192 fprintf(stderr
, "Wrong address family %d\n",
193 rth
->local
.nl_family
);
196 rth
->seq
= time(NULL
);
200 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
202 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
205 int rtnl_addrdump_req(struct rtnl_handle
*rth
, int family
)
209 struct ifaddrmsg ifm
;
211 .nlh
.nlmsg_len
= sizeof(req
),
212 .nlh
.nlmsg_type
= RTM_GETADDR
,
213 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
214 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
215 .ifm
.ifa_family
= family
,
218 return send(rth
->fd
, &req
, sizeof(req
), 0);
221 int rtnl_addrlbldump_req(struct rtnl_handle
*rth
, int family
)
225 struct ifaddrlblmsg ifal
;
227 .nlh
.nlmsg_len
= sizeof(req
),
228 .nlh
.nlmsg_type
= RTM_GETADDRLABEL
,
229 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
230 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
231 .ifal
.ifal_family
= family
,
234 return send(rth
->fd
, &req
, sizeof(req
), 0);
237 int rtnl_routedump_req(struct rtnl_handle
*rth
, int family
)
243 .nlh
.nlmsg_len
= sizeof(req
),
244 .nlh
.nlmsg_type
= RTM_GETROUTE
,
245 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
246 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
247 .rtm
.rtm_family
= family
,
250 return send(rth
->fd
, &req
, sizeof(req
), 0);
253 int rtnl_ruledump_req(struct rtnl_handle
*rth
, int family
)
257 struct fib_rule_hdr frh
;
259 .nlh
.nlmsg_len
= sizeof(req
),
260 .nlh
.nlmsg_type
= RTM_GETRULE
,
261 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
262 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
266 return send(rth
->fd
, &req
, sizeof(req
), 0);
269 int rtnl_neighdump_req(struct rtnl_handle
*rth
, int family
)
275 .nlh
.nlmsg_len
= sizeof(req
),
276 .nlh
.nlmsg_type
= RTM_GETNEIGH
,
277 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
278 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
279 .ndm
.ndm_family
= family
,
282 return send(rth
->fd
, &req
, sizeof(req
), 0);
285 int rtnl_neightbldump_req(struct rtnl_handle
*rth
, int family
)
289 struct ndtmsg ndtmsg
;
291 .nlh
.nlmsg_len
= sizeof(req
),
292 .nlh
.nlmsg_type
= RTM_GETNEIGHTBL
,
293 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
294 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
295 .ndtmsg
.ndtm_family
= family
,
298 return send(rth
->fd
, &req
, sizeof(req
), 0);
301 int rtnl_mdbdump_req(struct rtnl_handle
*rth
, int family
)
305 struct br_port_msg bpm
;
307 .nlh
.nlmsg_len
= sizeof(req
),
308 .nlh
.nlmsg_type
= RTM_GETMDB
,
309 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
310 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
311 .bpm
.family
= family
,
314 return send(rth
->fd
, &req
, sizeof(req
), 0);
317 int rtnl_netconfdump_req(struct rtnl_handle
*rth
, int family
)
321 struct netconfmsg ncm
;
323 .nlh
.nlmsg_len
= sizeof(req
),
324 .nlh
.nlmsg_type
= RTM_GETNETCONF
,
325 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
326 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
327 .ncm
.ncm_family
= family
,
330 return send(rth
->fd
, &req
, sizeof(req
), 0);
333 int rtnl_wilddump_request(struct rtnl_handle
*rth
, int family
, int type
)
335 return rtnl_wilddump_req_filter(rth
, family
, type
, RTEXT_FILTER_VF
);
338 int rtnl_wilddump_req_filter(struct rtnl_handle
*rth
, int family
, int type
,
343 struct ifinfomsg ifm
;
344 /* attribute has to be NLMSG aligned */
345 struct rtattr ext_req
__attribute__ ((aligned(NLMSG_ALIGNTO
)));
346 __u32 ext_filter_mask
;
348 .nlh
.nlmsg_len
= sizeof(req
),
349 .nlh
.nlmsg_type
= type
,
350 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
351 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
352 .ifm
.ifi_family
= family
,
353 .ext_req
.rta_type
= IFLA_EXT_MASK
,
354 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
355 .ext_filter_mask
= filt_mask
,
358 return send(rth
->fd
, &req
, sizeof(req
), 0);
361 int rtnl_wilddump_req_filter_fn(struct rtnl_handle
*rth
, int family
, int type
,
362 req_filter_fn_t filter_fn
)
366 struct ifinfomsg ifm
;
369 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
370 .nlh
.nlmsg_type
= type
,
371 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
372 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
373 .ifm
.ifi_family
= family
,
380 err
= filter_fn(&req
.nlh
, sizeof(req
));
384 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
387 int rtnl_wilddump_stats_req_filter(struct rtnl_handle
*rth
, int fam
, int type
,
392 struct if_stats_msg ifsm
;
395 memset(&req
, 0, sizeof(req
));
396 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
397 req
.nlh
.nlmsg_type
= type
;
398 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
399 req
.nlh
.nlmsg_pid
= 0;
400 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
401 req
.ifsm
.family
= fam
;
402 req
.ifsm
.filter_mask
= filt_mask
;
404 return send(rth
->fd
, &req
, sizeof(req
), 0);
407 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
409 return send(rth
->fd
, buf
, len
, 0);
412 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
418 status
= send(rth
->fd
, buf
, len
, 0);
422 /* Check for immediate errors */
423 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
430 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
431 h
= NLMSG_NEXT(h
, status
)) {
432 if (h
->nlmsg_type
== NLMSG_ERROR
) {
433 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
435 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
436 fprintf(stderr
, "ERROR truncated\n");
446 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
448 struct nlmsghdr nlh
= {
449 .nlmsg_len
= NLMSG_LENGTH(len
),
451 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
452 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
454 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
455 struct iovec iov
[2] = {
456 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
457 { .iov_base
= req
, .iov_len
= len
}
459 struct msghdr msg
= {
461 .msg_namelen
= sizeof(nladdr
),
466 return sendmsg(rth
->fd
, &msg
, 0);
469 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
471 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
474 .iov_len
= n
->nlmsg_len
476 struct msghdr msg
= {
478 .msg_namelen
= sizeof(nladdr
),
483 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
485 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
487 return sendmsg(rth
->fd
, &msg
, 0);
490 static int rtnl_dump_done(struct nlmsghdr
*h
)
492 int len
= *(int *)NLMSG_DATA(h
);
494 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
495 fprintf(stderr
, "DONE truncated\n");
507 "Error: Buffer too small for object.\n");
510 perror("RTNETLINK answers");
515 /* check for any messages returned from kernel */
516 nl_dump_ext_ack(h
, NULL
);
521 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
525 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
526 fprintf(stderr
, "ERROR truncated\n");
528 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
531 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
533 errno
== EOPNOTSUPP
))
536 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
537 perror("RTNETLINK answers");
541 static int __rtnl_recvmsg(int fd
, struct msghdr
*msg
, int flags
)
546 len
= recvmsg(fd
, msg
, flags
);
547 } while (len
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
550 fprintf(stderr
, "netlink receive error %s (%d)\n",
551 strerror(errno
), errno
);
556 fprintf(stderr
, "EOF on netlink\n");
563 static int rtnl_recvmsg(int fd
, struct msghdr
*msg
, char **answer
)
565 struct iovec
*iov
= msg
->msg_iov
;
569 iov
->iov_base
= NULL
;
572 len
= __rtnl_recvmsg(fd
, msg
, MSG_PEEK
| MSG_TRUNC
);
578 fprintf(stderr
, "malloc error: not enough buffer\n");
585 len
= __rtnl_recvmsg(fd
, msg
, 0);
599 int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
600 const struct rtnl_dump_filter_arg
*arg
)
602 struct sockaddr_nl nladdr
;
604 struct msghdr msg
= {
606 .msg_namelen
= sizeof(nladdr
),
615 const struct rtnl_dump_filter_arg
*a
;
619 status
= rtnl_recvmsg(rth
->fd
, &msg
, &buf
);
624 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
626 for (a
= arg
; a
->filter
; a
++) {
627 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
631 while (NLMSG_OK(h
, msglen
)) {
634 h
->nlmsg_flags
&= ~a
->nc_flags
;
636 if (nladdr
.nl_pid
!= 0 ||
637 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
638 h
->nlmsg_seq
!= rth
->dump
)
641 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
644 if (h
->nlmsg_type
== NLMSG_DONE
) {
645 err
= rtnl_dump_done(h
);
652 break; /* process next filter */
655 if (h
->nlmsg_type
== NLMSG_ERROR
) {
656 rtnl_dump_error(rth
, h
);
662 err
= a
->filter(&nladdr
, h
, a
->arg1
);
670 h
= NLMSG_NEXT(h
, msglen
);
678 "Dump was interrupted and may be inconsistent.\n");
682 if (msg
.msg_flags
& MSG_TRUNC
) {
683 fprintf(stderr
, "Message truncated\n");
687 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
693 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
694 rtnl_filter_t filter
,
695 void *arg1
, __u16 nc_flags
)
697 const struct rtnl_dump_filter_arg a
[2] = {
698 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
699 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
702 return rtnl_dump_filter_l(rth
, a
);
705 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
706 nl_ext_ack_fn_t errfn
)
708 if (nl_dump_ext_ack(h
, errfn
))
711 fprintf(stderr
, "RTNETLINK answers: %s\n",
712 strerror(-err
->error
));
716 static int __rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iov
,
717 size_t iovlen
, struct nlmsghdr
**answer
,
718 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
720 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
722 struct msghdr msg
= {
724 .msg_namelen
= sizeof(nladdr
),
726 .msg_iovlen
= iovlen
,
728 unsigned int seq
= 0;
733 for (i
= 0; i
< iovlen
; i
++) {
735 h
->nlmsg_seq
= seq
= ++rtnl
->seq
;
737 h
->nlmsg_flags
|= NLM_F_ACK
;
740 status
= sendmsg(rtnl
->fd
, &msg
, 0);
742 perror("Cannot talk to rtnetlink");
746 /* change msg to use the response iov */
751 status
= rtnl_recvmsg(rtnl
->fd
, &msg
, &buf
);
757 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
759 "sender address length == %d\n",
763 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
764 int len
= h
->nlmsg_len
;
765 int l
= len
- sizeof(*h
);
767 if (l
< 0 || len
> status
) {
768 if (msg
.msg_flags
& MSG_TRUNC
) {
769 fprintf(stderr
, "Truncated message\n");
774 "!!!malformed message: len=%d\n",
779 if (nladdr
.nl_pid
!= 0 ||
780 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
781 h
->nlmsg_seq
> seq
|| h
->nlmsg_seq
< seq
- iovlen
) {
782 /* Don't forget to skip that message. */
783 status
-= NLMSG_ALIGN(len
);
784 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
788 if (h
->nlmsg_type
== NLMSG_ERROR
) {
789 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
791 if (l
< sizeof(struct nlmsgerr
)) {
792 fprintf(stderr
, "ERROR truncated\n");
798 /* check messages from kernel */
799 nl_dump_ext_ack(h
, errfn
);
803 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
805 rtnl_talk_error(h
, err
, errfn
);
809 *answer
= (struct nlmsghdr
*)buf
;
813 return err
->error
? -i
: 0;
817 *answer
= (struct nlmsghdr
*)buf
;
821 fprintf(stderr
, "Unexpected reply!!!\n");
823 status
-= NLMSG_ALIGN(len
);
824 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
828 if (msg
.msg_flags
& MSG_TRUNC
) {
829 fprintf(stderr
, "Message truncated\n");
834 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
840 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
841 struct nlmsghdr
**answer
,
842 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
846 .iov_len
= n
->nlmsg_len
849 return __rtnl_talk_iov(rtnl
, &iov
, 1, answer
, show_rtnl_err
, errfn
);
852 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
853 struct nlmsghdr
**answer
)
855 return __rtnl_talk(rtnl
, n
, answer
, true, NULL
);
858 int rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iovec
, size_t iovlen
,
859 struct nlmsghdr
**answer
)
861 return __rtnl_talk_iov(rtnl
, iovec
, iovlen
, answer
, true, NULL
);
864 int rtnl_talk_extack(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
865 struct nlmsghdr
**answer
,
866 nl_ext_ack_fn_t errfn
)
868 return __rtnl_talk(rtnl
, n
, answer
, true, errfn
);
871 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
872 struct nlmsghdr
**answer
)
874 return __rtnl_talk(rtnl
, n
, answer
, false, NULL
);
877 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
881 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
883 perror("NETLINK_LISTEN_ALL_NSID");
886 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
890 int rtnl_listen(struct rtnl_handle
*rtnl
,
891 rtnl_listen_filter_t handler
,
896 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
898 struct msghdr msg
= {
900 .msg_namelen
= sizeof(nladdr
),
905 char cmsgbuf
[BUFSIZ
];
907 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
908 msg
.msg_control
= &cmsgbuf
;
909 msg
.msg_controllen
= sizeof(cmsgbuf
);
914 struct rtnl_ctrl_data ctrl
;
915 struct cmsghdr
*cmsg
;
917 iov
.iov_len
= sizeof(buf
);
918 status
= recvmsg(rtnl
->fd
, &msg
, 0);
921 if (errno
== EINTR
|| errno
== EAGAIN
)
923 fprintf(stderr
, "netlink receive error %s (%d)\n",
924 strerror(errno
), errno
);
925 if (errno
== ENOBUFS
)
930 fprintf(stderr
, "EOF on netlink\n");
933 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
935 "Sender address length == %d\n",
940 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
941 memset(&ctrl
, 0, sizeof(ctrl
));
943 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
944 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
945 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
946 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
947 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
948 int *data
= (int *)CMSG_DATA(cmsg
);
954 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
956 int len
= h
->nlmsg_len
;
957 int l
= len
- sizeof(*h
);
959 if (l
< 0 || len
> status
) {
960 if (msg
.msg_flags
& MSG_TRUNC
) {
961 fprintf(stderr
, "Truncated message\n");
965 "!!!malformed message: len=%d\n",
970 err
= handler(&nladdr
, &ctrl
, h
, jarg
);
974 status
-= NLMSG_ALIGN(len
);
975 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
977 if (msg
.msg_flags
& MSG_TRUNC
) {
978 fprintf(stderr
, "Message truncated\n");
982 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
988 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
992 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
994 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
1000 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
1005 perror("rtnl_from_file: fread");
1012 l
= len
- sizeof(*h
);
1014 if (l
< 0 || len
> sizeof(buf
)) {
1015 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
1020 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
1023 perror("rtnl_from_file: fread");
1027 fprintf(stderr
, "rtnl-from_file: truncated message\n");
1031 err
= handler(&nladdr
, NULL
, h
, jarg
);
1037 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
1039 return addattr_l(n
, maxlen
, type
, NULL
, 0);
1042 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
1044 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
1047 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
1049 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
1052 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
1054 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
1057 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
1059 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
1062 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
1064 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
1067 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
1070 int len
= RTA_LENGTH(alen
);
1073 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
1075 "addattr_l ERROR: message exceeded bound of %d\n",
1079 rta
= NLMSG_TAIL(n
);
1080 rta
->rta_type
= type
;
1083 memcpy(RTA_DATA(rta
), data
, alen
);
1084 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
1088 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
1090 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
1092 "addraw_l ERROR: message exceeded bound of %d\n",
1097 memcpy(NLMSG_TAIL(n
), data
, len
);
1098 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
1099 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
1103 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
1105 struct rtattr
*nest
= NLMSG_TAIL(n
);
1107 addattr_l(n
, maxlen
, type
, NULL
, 0);
1111 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
1113 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
1114 return n
->nlmsg_len
;
1117 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
1118 const void *data
, int len
)
1120 struct rtattr
*start
= NLMSG_TAIL(n
);
1122 addattr_l(n
, maxlen
, type
, data
, len
);
1123 addattr_nest(n
, maxlen
, type
);
1127 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
1129 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
1131 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
1132 addattr_nest_end(n
, nest
);
1133 return n
->nlmsg_len
;
1136 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
1138 int len
= RTA_LENGTH(4);
1139 struct rtattr
*subrta
;
1141 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
1143 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1147 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1148 subrta
->rta_type
= type
;
1149 subrta
->rta_len
= len
;
1150 memcpy(RTA_DATA(subrta
), &data
, 4);
1151 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
1155 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
1156 const void *data
, int alen
)
1158 struct rtattr
*subrta
;
1159 int len
= RTA_LENGTH(alen
);
1161 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
1163 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1167 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1168 subrta
->rta_type
= type
;
1169 subrta
->rta_len
= len
;
1171 memcpy(RTA_DATA(subrta
), data
, alen
);
1172 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
1176 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
1178 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
1181 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
1183 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
1186 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
1188 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
1191 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
1193 struct rtattr
*nest
= RTA_TAIL(rta
);
1195 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
1200 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
1202 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
1204 return rta
->rta_len
;
1207 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
1209 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
1212 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
1213 int len
, unsigned short flags
)
1215 unsigned short type
;
1217 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1218 while (RTA_OK(rta
, len
)) {
1219 type
= rta
->rta_type
& ~flags
;
1220 if ((type
<= max
) && (!tb
[type
]))
1222 rta
= RTA_NEXT(rta
, len
);
1225 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1230 int parse_rtattr_byindex(struct rtattr
*tb
[], int max
,
1231 struct rtattr
*rta
, int len
)
1235 memset(tb
, 0, sizeof(struct rtattr
*) * max
);
1236 while (RTA_OK(rta
, len
)) {
1237 if (rta
->rta_type
<= max
&& i
< max
)
1239 rta
= RTA_NEXT(rta
, len
);
1242 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1247 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1249 while (RTA_OK(rta
, len
)) {
1250 if (rta
->rta_type
== type
)
1252 rta
= RTA_NEXT(rta
, len
);
1256 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1261 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1265 if (RTA_PAYLOAD(rta
) < len
)
1267 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1268 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1269 return parse_rtattr_nested(tb
, max
, rta
);
1271 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));