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>
26 #include "libnetlink.h"
29 #define SOL_NETLINK 270
33 #define MIN(a, b) ((a) < (b) ? (a) : (b))
36 int rcvbuf
= 1024 * 1024;
38 void rtnl_close(struct rtnl_handle
*rth
)
46 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned subscriptions
,
52 memset(rth
, 0, sizeof(*rth
));
54 rth
->proto
= protocol
;
55 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
57 perror("Cannot open netlink socket");
61 if (setsockopt(rth
->fd
,SOL_SOCKET
,SO_SNDBUF
,&sndbuf
,sizeof(sndbuf
)) < 0) {
66 if (setsockopt(rth
->fd
,SOL_SOCKET
,SO_RCVBUF
,&rcvbuf
,sizeof(rcvbuf
)) < 0) {
71 memset(&rth
->local
, 0, sizeof(rth
->local
));
72 rth
->local
.nl_family
= AF_NETLINK
;
73 rth
->local
.nl_groups
= subscriptions
;
75 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
, sizeof(rth
->local
)) < 0) {
76 perror("Cannot bind netlink socket");
79 addr_len
= sizeof(rth
->local
);
80 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
, &addr_len
) < 0) {
81 perror("Cannot getsockname");
84 if (addr_len
!= sizeof(rth
->local
)) {
85 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
88 if (rth
->local
.nl_family
!= AF_NETLINK
) {
89 fprintf(stderr
, "Wrong address family %d\n", rth
->local
.nl_family
);
92 rth
->seq
= time(NULL
);
96 int rtnl_open(struct rtnl_handle
*rth
, unsigned subscriptions
)
98 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
101 int rtnl_wilddump_request(struct rtnl_handle
*rth
, int family
, int type
)
103 return rtnl_wilddump_req_filter(rth
, family
, type
, RTEXT_FILTER_VF
);
106 int rtnl_wilddump_req_filter(struct rtnl_handle
*rth
, int family
, int type
,
111 struct ifinfomsg ifm
;
112 /* attribute has to be NLMSG aligned */
113 struct rtattr ext_req
__attribute__ ((aligned(NLMSG_ALIGNTO
)));
114 __u32 ext_filter_mask
;
117 memset(&req
, 0, sizeof(req
));
118 req
.nlh
.nlmsg_len
= sizeof(req
);
119 req
.nlh
.nlmsg_type
= type
;
120 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
121 req
.nlh
.nlmsg_pid
= 0;
122 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
123 req
.ifm
.ifi_family
= family
;
125 req
.ext_req
.rta_type
= IFLA_EXT_MASK
;
126 req
.ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
));
127 req
.ext_filter_mask
= filt_mask
;
129 return send(rth
->fd
, (void*)&req
, sizeof(req
), 0);
132 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
134 return send(rth
->fd
, buf
, len
, 0);
137 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
143 status
= send(rth
->fd
, buf
, len
, 0);
147 /* Check for immediate errors */
148 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
155 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
156 h
= NLMSG_NEXT(h
, status
)) {
157 if (h
->nlmsg_type
== NLMSG_ERROR
) {
158 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
159 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
160 fprintf(stderr
, "ERROR truncated\n");
170 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
173 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
174 struct iovec iov
[2] = {
175 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
176 { .iov_base
= req
, .iov_len
= len
}
178 struct msghdr msg
= {
180 .msg_namelen
= sizeof(nladdr
),
185 nlh
.nlmsg_len
= NLMSG_LENGTH(len
);
186 nlh
.nlmsg_type
= type
;
187 nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
189 nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
191 return sendmsg(rth
->fd
, &msg
, 0);
194 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
196 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
198 .iov_base
= (void*) n
,
199 .iov_len
= n
->nlmsg_len
201 struct msghdr msg
= {
203 .msg_namelen
= sizeof(nladdr
),
208 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
210 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
212 return sendmsg(rth
->fd
, &msg
, 0);
215 int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
216 const struct rtnl_dump_filter_arg
*arg
)
218 struct sockaddr_nl nladdr
;
220 struct msghdr msg
= {
222 .msg_namelen
= sizeof(nladdr
),
232 const struct rtnl_dump_filter_arg
*a
;
236 iov
.iov_len
= sizeof(buf
);
237 status
= recvmsg(rth
->fd
, &msg
, 0);
240 if (errno
== EINTR
|| errno
== EAGAIN
)
242 fprintf(stderr
, "netlink receive error %s (%d)\n",
243 strerror(errno
), errno
);
248 fprintf(stderr
, "EOF on netlink\n");
253 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
255 for (a
= arg
; a
->filter
; a
++) {
256 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
259 while (NLMSG_OK(h
, msglen
)) {
262 if (nladdr
.nl_pid
!= 0 ||
263 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
264 h
->nlmsg_seq
!= rth
->dump
)
267 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
270 if (h
->nlmsg_type
== NLMSG_DONE
) {
272 break; /* process next filter */
274 if (h
->nlmsg_type
== NLMSG_ERROR
) {
275 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
276 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
278 "ERROR truncated\n");
281 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
283 errno
== EOPNOTSUPP
))
286 perror("RTNETLINK answers");
292 err
= a
->filter(&nladdr
, h
, a
->arg1
);
298 h
= NLMSG_NEXT(h
, msglen
);
305 "Dump was interrupted and may be inconsistent.\n");
309 if (msg
.msg_flags
& MSG_TRUNC
) {
310 fprintf(stderr
, "Message truncated\n");
314 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
320 int rtnl_dump_filter(struct rtnl_handle
*rth
,
321 rtnl_filter_t filter
,
324 const struct rtnl_dump_filter_arg a
[2] = {
325 { .filter
= filter
, .arg1
= arg1
, },
326 { .filter
= NULL
, .arg1
= NULL
, },
329 return rtnl_dump_filter_l(rth
, a
);
332 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
333 struct nlmsghdr
*answer
, size_t len
)
338 struct sockaddr_nl nladdr
;
340 .iov_base
= (void*) n
,
341 .iov_len
= n
->nlmsg_len
343 struct msghdr msg
= {
345 .msg_namelen
= sizeof(nladdr
),
351 memset(&nladdr
, 0, sizeof(nladdr
));
352 nladdr
.nl_family
= AF_NETLINK
;
354 n
->nlmsg_seq
= seq
= ++rtnl
->seq
;
357 n
->nlmsg_flags
|= NLM_F_ACK
;
359 status
= sendmsg(rtnl
->fd
, &msg
, 0);
361 perror("Cannot talk to rtnetlink");
365 memset(buf
,0,sizeof(buf
));
369 iov
.iov_len
= sizeof(buf
);
370 status
= recvmsg(rtnl
->fd
, &msg
, 0);
373 if (errno
== EINTR
|| errno
== EAGAIN
)
375 fprintf(stderr
, "netlink receive error %s (%d)\n",
376 strerror(errno
), errno
);
380 fprintf(stderr
, "EOF on netlink\n");
383 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
384 fprintf(stderr
, "sender address length == %d\n", msg
.msg_namelen
);
387 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
388 int len
= h
->nlmsg_len
;
389 int l
= len
- sizeof(*h
);
391 if (l
< 0 || len
>status
) {
392 if (msg
.msg_flags
& MSG_TRUNC
) {
393 fprintf(stderr
, "Truncated message\n");
396 fprintf(stderr
, "!!!malformed message: len=%d\n", len
);
400 if (nladdr
.nl_pid
!= 0 ||
401 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
402 h
->nlmsg_seq
!= seq
) {
403 /* Don't forget to skip that message. */
404 status
-= NLMSG_ALIGN(len
);
405 h
= (struct nlmsghdr
*)((char*)h
+ NLMSG_ALIGN(len
));
409 if (h
->nlmsg_type
== NLMSG_ERROR
) {
410 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
411 if (l
< sizeof(struct nlmsgerr
)) {
412 fprintf(stderr
, "ERROR truncated\n");
413 } else if (!err
->error
) {
416 MIN(len
, h
->nlmsg_len
));
420 fprintf(stderr
, "RTNETLINK answers: %s\n",
421 strerror(-err
->error
));
428 MIN(len
, h
->nlmsg_len
));
432 fprintf(stderr
, "Unexpected reply!!!\n");
434 status
-= NLMSG_ALIGN(len
);
435 h
= (struct nlmsghdr
*)((char*)h
+ NLMSG_ALIGN(len
));
438 if (msg
.msg_flags
& MSG_TRUNC
) {
439 fprintf(stderr
, "Message truncated\n");
444 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
450 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
454 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
456 perror("NETLINK_LISTEN_ALL_NSID");
459 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
463 int rtnl_listen(struct rtnl_handle
*rtnl
,
464 rtnl_listen_filter_t handler
,
469 struct sockaddr_nl nladdr
;
471 struct msghdr msg
= {
473 .msg_namelen
= sizeof(nladdr
),
478 char cmsgbuf
[BUFSIZ
];
480 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
481 msg
.msg_control
= &cmsgbuf
;
482 msg
.msg_controllen
= sizeof(cmsgbuf
);
485 memset(&nladdr
, 0, sizeof(nladdr
));
486 nladdr
.nl_family
= AF_NETLINK
;
488 nladdr
.nl_groups
= 0;
492 struct rtnl_ctrl_data ctrl
;
493 struct cmsghdr
*cmsg
;
495 iov
.iov_len
= sizeof(buf
);
496 status
= recvmsg(rtnl
->fd
, &msg
, 0);
499 if (errno
== EINTR
|| errno
== EAGAIN
)
501 fprintf(stderr
, "netlink receive error %s (%d)\n",
502 strerror(errno
), errno
);
503 if (errno
== ENOBUFS
)
508 fprintf(stderr
, "EOF on netlink\n");
511 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
512 fprintf(stderr
, "Sender address length == %d\n", msg
.msg_namelen
);
516 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
517 memset(&ctrl
, 0, sizeof(ctrl
));
519 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
520 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
521 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
522 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
523 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
524 int *data
= (int *)CMSG_DATA(cmsg
);
530 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
532 int len
= h
->nlmsg_len
;
533 int l
= len
- sizeof(*h
);
535 if (l
<0 || len
>status
) {
536 if (msg
.msg_flags
& MSG_TRUNC
) {
537 fprintf(stderr
, "Truncated message\n");
540 fprintf(stderr
, "!!!malformed message: len=%d\n", len
);
544 err
= handler(&nladdr
, &ctrl
, h
, jarg
);
548 status
-= NLMSG_ALIGN(len
);
549 h
= (struct nlmsghdr
*)((char*)h
+ NLMSG_ALIGN(len
));
551 if (msg
.msg_flags
& MSG_TRUNC
) {
552 fprintf(stderr
, "Message truncated\n");
556 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
562 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
566 struct sockaddr_nl nladdr
;
568 struct nlmsghdr
*h
= (void*)buf
;
570 memset(&nladdr
, 0, sizeof(nladdr
));
571 nladdr
.nl_family
= AF_NETLINK
;
573 nladdr
.nl_groups
= 0;
579 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
584 perror("rtnl_from_file: fread");
591 l
= len
- sizeof(*h
);
593 if (l
<0 || len
>sizeof(buf
)) {
594 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
599 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
602 perror("rtnl_from_file: fread");
606 fprintf(stderr
, "rtnl-from_file: truncated message\n");
610 err
= handler(&nladdr
, NULL
, h
, jarg
);
616 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
618 return addattr_l(n
, maxlen
, type
, NULL
, 0);
621 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
623 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
626 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
628 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
631 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
633 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
636 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
638 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
641 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
643 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
646 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
649 int len
= RTA_LENGTH(alen
);
652 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
653 fprintf(stderr
, "addattr_l ERROR: message exceeded bound of %d\n",maxlen
);
657 rta
->rta_type
= type
;
659 memcpy(RTA_DATA(rta
), data
, alen
);
660 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
664 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
666 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
667 fprintf(stderr
, "addraw_l ERROR: message exceeded bound of %d\n",maxlen
);
671 memcpy(NLMSG_TAIL(n
), data
, len
);
672 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
673 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
677 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
679 struct rtattr
*nest
= NLMSG_TAIL(n
);
681 addattr_l(n
, maxlen
, type
, NULL
, 0);
685 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
687 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
691 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
692 const void *data
, int len
)
694 struct rtattr
*start
= NLMSG_TAIL(n
);
696 addattr_l(n
, maxlen
, type
, data
, len
);
697 addattr_nest(n
, maxlen
, type
);
701 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
703 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
705 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
706 addattr_nest_end(n
, nest
);
710 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
712 int len
= RTA_LENGTH(4);
713 struct rtattr
*subrta
;
715 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
716 fprintf(stderr
,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen
);
719 subrta
= (struct rtattr
*)(((char*)rta
) + RTA_ALIGN(rta
->rta_len
));
720 subrta
->rta_type
= type
;
721 subrta
->rta_len
= len
;
722 memcpy(RTA_DATA(subrta
), &data
, 4);
723 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
727 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
728 const void *data
, int alen
)
730 struct rtattr
*subrta
;
731 int len
= RTA_LENGTH(alen
);
733 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
734 fprintf(stderr
,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen
);
737 subrta
= (struct rtattr
*)(((char*)rta
) + RTA_ALIGN(rta
->rta_len
));
738 subrta
->rta_type
= type
;
739 subrta
->rta_len
= len
;
740 memcpy(RTA_DATA(subrta
), data
, alen
);
741 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
745 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
747 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
750 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
751 int len
, unsigned short flags
)
755 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
756 while (RTA_OK(rta
, len
)) {
757 type
= rta
->rta_type
& ~flags
;
758 if ((type
<= max
) && (!tb
[type
]))
760 rta
= RTA_NEXT(rta
,len
);
763 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n", len
, rta
->rta_len
);
767 int parse_rtattr_byindex(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
771 memset(tb
, 0, sizeof(struct rtattr
*) * max
);
772 while (RTA_OK(rta
, len
)) {
773 if (rta
->rta_type
<= max
&& i
< max
)
775 rta
= RTA_NEXT(rta
,len
);
778 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n", len
, rta
->rta_len
);
782 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
784 while (RTA_OK(rta
, len
)) {
785 if (rta
->rta_type
== type
)
787 rta
= RTA_NEXT(rta
, len
);
790 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n", len
, rta
->rta_len
);
794 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
797 if (RTA_PAYLOAD(rta
) < len
)
799 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
800 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
801 return parse_rtattr_nested(tb
, max
, rta
);
803 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));