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"
28 int rcvbuf
= 1024 * 1024;
30 void rtnl_close(struct rtnl_handle
*rth
)
38 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned subscriptions
,
44 memset(rth
, 0, sizeof(*rth
));
46 rth
->proto
= protocol
;
47 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
49 perror("Cannot open netlink socket");
53 if (setsockopt(rth
->fd
,SOL_SOCKET
,SO_SNDBUF
,&sndbuf
,sizeof(sndbuf
)) < 0) {
58 if (setsockopt(rth
->fd
,SOL_SOCKET
,SO_RCVBUF
,&rcvbuf
,sizeof(rcvbuf
)) < 0) {
63 memset(&rth
->local
, 0, sizeof(rth
->local
));
64 rth
->local
.nl_family
= AF_NETLINK
;
65 rth
->local
.nl_groups
= subscriptions
;
67 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
, sizeof(rth
->local
)) < 0) {
68 perror("Cannot bind netlink socket");
71 addr_len
= sizeof(rth
->local
);
72 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
, &addr_len
) < 0) {
73 perror("Cannot getsockname");
76 if (addr_len
!= sizeof(rth
->local
)) {
77 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
80 if (rth
->local
.nl_family
!= AF_NETLINK
) {
81 fprintf(stderr
, "Wrong address family %d\n", rth
->local
.nl_family
);
84 rth
->seq
= time(NULL
);
88 int rtnl_open(struct rtnl_handle
*rth
, unsigned subscriptions
)
90 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
93 int rtnl_wilddump_request(struct rtnl_handle
*rth
, int family
, int type
)
95 return rtnl_wilddump_req_filter(rth
, family
, type
, RTEXT_FILTER_VF
);
98 int rtnl_wilddump_req_filter(struct rtnl_handle
*rth
, int family
, int type
,
103 struct ifinfomsg ifm
;
104 /* attribute has to be NLMSG aligned */
105 struct rtattr ext_req
__attribute__ ((aligned(NLMSG_ALIGNTO
)));
106 __u32 ext_filter_mask
;
109 memset(&req
, 0, sizeof(req
));
110 req
.nlh
.nlmsg_len
= sizeof(req
);
111 req
.nlh
.nlmsg_type
= type
;
112 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
113 req
.nlh
.nlmsg_pid
= 0;
114 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
115 req
.ifm
.ifi_family
= family
;
117 req
.ext_req
.rta_type
= IFLA_EXT_MASK
;
118 req
.ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
));
119 req
.ext_filter_mask
= filt_mask
;
121 return send(rth
->fd
, (void*)&req
, sizeof(req
), 0);
124 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
126 return send(rth
->fd
, buf
, len
, 0);
129 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
135 status
= send(rth
->fd
, buf
, len
, 0);
139 /* Check for immediate errors */
140 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
147 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
148 h
= NLMSG_NEXT(h
, status
)) {
149 if (h
->nlmsg_type
== NLMSG_ERROR
) {
150 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
151 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
152 fprintf(stderr
, "ERROR truncated\n");
162 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
165 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
166 struct iovec iov
[2] = {
167 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
168 { .iov_base
= req
, .iov_len
= len
}
170 struct msghdr msg
= {
172 .msg_namelen
= sizeof(nladdr
),
177 nlh
.nlmsg_len
= NLMSG_LENGTH(len
);
178 nlh
.nlmsg_type
= type
;
179 nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
181 nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
183 return sendmsg(rth
->fd
, &msg
, 0);
186 int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
187 const struct rtnl_dump_filter_arg
*arg
)
189 struct sockaddr_nl nladdr
;
191 struct msghdr msg
= {
193 .msg_namelen
= sizeof(nladdr
),
203 const struct rtnl_dump_filter_arg
*a
;
207 iov
.iov_len
= sizeof(buf
);
208 status
= recvmsg(rth
->fd
, &msg
, 0);
211 if (errno
== EINTR
|| errno
== EAGAIN
)
213 fprintf(stderr
, "netlink receive error %s (%d)\n",
214 strerror(errno
), errno
);
219 fprintf(stderr
, "EOF on netlink\n");
224 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
226 for (a
= arg
; a
->filter
; a
++) {
227 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
230 while (NLMSG_OK(h
, msglen
)) {
233 if (nladdr
.nl_pid
!= 0 ||
234 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
235 h
->nlmsg_seq
!= rth
->dump
)
238 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
241 if (h
->nlmsg_type
== NLMSG_DONE
) {
243 break; /* process next filter */
245 if (h
->nlmsg_type
== NLMSG_ERROR
) {
246 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
247 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
249 "ERROR truncated\n");
252 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
254 errno
== EOPNOTSUPP
))
257 perror("RTNETLINK answers");
263 err
= a
->filter(&nladdr
, h
, a
->arg1
);
269 h
= NLMSG_NEXT(h
, msglen
);
276 "Dump was interrupted and may be inconsistent.\n");
280 if (msg
.msg_flags
& MSG_TRUNC
) {
281 fprintf(stderr
, "Message truncated\n");
285 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
291 int rtnl_dump_filter(struct rtnl_handle
*rth
,
292 rtnl_filter_t filter
,
295 const struct rtnl_dump_filter_arg a
[2] = {
296 { .filter
= filter
, .arg1
= arg1
, },
297 { .filter
= NULL
, .arg1
= NULL
, },
300 return rtnl_dump_filter_l(rth
, a
);
303 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
, pid_t peer
,
304 unsigned groups
, struct nlmsghdr
*answer
)
309 struct sockaddr_nl nladdr
;
311 .iov_base
= (void*) n
,
312 .iov_len
= n
->nlmsg_len
314 struct msghdr msg
= {
316 .msg_namelen
= sizeof(nladdr
),
322 memset(&nladdr
, 0, sizeof(nladdr
));
323 nladdr
.nl_family
= AF_NETLINK
;
324 nladdr
.nl_pid
= peer
;
325 nladdr
.nl_groups
= groups
;
327 n
->nlmsg_seq
= seq
= ++rtnl
->seq
;
330 n
->nlmsg_flags
|= NLM_F_ACK
;
332 status
= sendmsg(rtnl
->fd
, &msg
, 0);
335 perror("Cannot talk to rtnetlink");
339 memset(buf
,0,sizeof(buf
));
344 iov
.iov_len
= sizeof(buf
);
345 status
= recvmsg(rtnl
->fd
, &msg
, 0);
348 if (errno
== EINTR
|| errno
== EAGAIN
)
350 fprintf(stderr
, "netlink receive error %s (%d)\n",
351 strerror(errno
), errno
);
355 fprintf(stderr
, "EOF on netlink\n");
358 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
359 fprintf(stderr
, "sender address length == %d\n", msg
.msg_namelen
);
362 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
363 int len
= h
->nlmsg_len
;
364 int l
= len
- sizeof(*h
);
366 if (l
< 0 || len
>status
) {
367 if (msg
.msg_flags
& MSG_TRUNC
) {
368 fprintf(stderr
, "Truncated message\n");
371 fprintf(stderr
, "!!!malformed message: len=%d\n", len
);
375 if (nladdr
.nl_pid
!= peer
||
376 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
377 h
->nlmsg_seq
!= seq
) {
378 /* Don't forget to skip that message. */
379 status
-= NLMSG_ALIGN(len
);
380 h
= (struct nlmsghdr
*)((char*)h
+ NLMSG_ALIGN(len
));
384 if (h
->nlmsg_type
== NLMSG_ERROR
) {
385 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
386 if (l
< sizeof(struct nlmsgerr
)) {
387 fprintf(stderr
, "ERROR truncated\n");
391 memcpy(answer
, h
, h
->nlmsg_len
);
395 fprintf(stderr
, "RTNETLINK answers: %s\n", strerror(-err
->error
));
401 memcpy(answer
, h
, h
->nlmsg_len
);
405 fprintf(stderr
, "Unexpected reply!!!\n");
407 status
-= NLMSG_ALIGN(len
);
408 h
= (struct nlmsghdr
*)((char*)h
+ NLMSG_ALIGN(len
));
410 if (msg
.msg_flags
& MSG_TRUNC
) {
411 fprintf(stderr
, "Message truncated\n");
415 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
421 int rtnl_listen(struct rtnl_handle
*rtnl
,
422 rtnl_listen_filter_t handler
,
427 struct sockaddr_nl nladdr
;
429 struct msghdr msg
= {
431 .msg_namelen
= sizeof(nladdr
),
437 memset(&nladdr
, 0, sizeof(nladdr
));
438 nladdr
.nl_family
= AF_NETLINK
;
440 nladdr
.nl_groups
= 0;
444 iov
.iov_len
= sizeof(buf
);
445 status
= recvmsg(rtnl
->fd
, &msg
, 0);
448 if (errno
== EINTR
|| errno
== EAGAIN
)
450 fprintf(stderr
, "netlink receive error %s (%d)\n",
451 strerror(errno
), errno
);
452 if (errno
== ENOBUFS
)
457 fprintf(stderr
, "EOF on netlink\n");
460 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
461 fprintf(stderr
, "Sender address length == %d\n", msg
.msg_namelen
);
464 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
466 int len
= h
->nlmsg_len
;
467 int l
= len
- sizeof(*h
);
469 if (l
<0 || len
>status
) {
470 if (msg
.msg_flags
& MSG_TRUNC
) {
471 fprintf(stderr
, "Truncated message\n");
474 fprintf(stderr
, "!!!malformed message: len=%d\n", len
);
478 err
= handler(&nladdr
, NULL
, h
, jarg
);
482 status
-= NLMSG_ALIGN(len
);
483 h
= (struct nlmsghdr
*)((char*)h
+ NLMSG_ALIGN(len
));
485 if (msg
.msg_flags
& MSG_TRUNC
) {
486 fprintf(stderr
, "Message truncated\n");
490 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
496 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
500 struct sockaddr_nl nladdr
;
502 struct nlmsghdr
*h
= (void*)buf
;
504 memset(&nladdr
, 0, sizeof(nladdr
));
505 nladdr
.nl_family
= AF_NETLINK
;
507 nladdr
.nl_groups
= 0;
513 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
518 perror("rtnl_from_file: fread");
525 l
= len
- sizeof(*h
);
527 if (l
<0 || len
>sizeof(buf
)) {
528 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
533 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
536 perror("rtnl_from_file: fread");
540 fprintf(stderr
, "rtnl-from_file: truncated message\n");
544 err
= handler(&nladdr
, NULL
, h
, jarg
);
550 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
552 return addattr_l(n
, maxlen
, type
, NULL
, 0);
555 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
557 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
560 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
562 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
565 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
567 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
570 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
572 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
575 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
577 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
580 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
583 int len
= RTA_LENGTH(alen
);
586 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
587 fprintf(stderr
, "addattr_l ERROR: message exceeded bound of %d\n",maxlen
);
591 rta
->rta_type
= type
;
593 memcpy(RTA_DATA(rta
), data
, alen
);
594 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
598 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
600 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
601 fprintf(stderr
, "addraw_l ERROR: message exceeded bound of %d\n",maxlen
);
605 memcpy(NLMSG_TAIL(n
), data
, len
);
606 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
607 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
611 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
613 struct rtattr
*nest
= NLMSG_TAIL(n
);
615 addattr_l(n
, maxlen
, type
, NULL
, 0);
619 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
621 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
625 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
626 const void *data
, int len
)
628 struct rtattr
*start
= NLMSG_TAIL(n
);
630 addattr_l(n
, maxlen
, type
, data
, len
);
631 addattr_nest(n
, maxlen
, type
);
635 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
637 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
639 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
640 addattr_nest_end(n
, nest
);
644 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
646 int len
= RTA_LENGTH(4);
647 struct rtattr
*subrta
;
649 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
650 fprintf(stderr
,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen
);
653 subrta
= (struct rtattr
*)(((char*)rta
) + RTA_ALIGN(rta
->rta_len
));
654 subrta
->rta_type
= type
;
655 subrta
->rta_len
= len
;
656 memcpy(RTA_DATA(subrta
), &data
, 4);
657 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
661 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
662 const void *data
, int alen
)
664 struct rtattr
*subrta
;
665 int len
= RTA_LENGTH(alen
);
667 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
668 fprintf(stderr
,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen
);
671 subrta
= (struct rtattr
*)(((char*)rta
) + RTA_ALIGN(rta
->rta_len
));
672 subrta
->rta_type
= type
;
673 subrta
->rta_len
= len
;
674 memcpy(RTA_DATA(subrta
), data
, alen
);
675 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
679 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
681 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
684 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
685 int len
, unsigned short flags
)
689 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
690 while (RTA_OK(rta
, len
)) {
691 type
= rta
->rta_type
& ~flags
;
692 if ((type
<= max
) && (!tb
[type
]))
694 rta
= RTA_NEXT(rta
,len
);
697 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n", len
, rta
->rta_len
);
701 int parse_rtattr_byindex(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
705 memset(tb
, 0, sizeof(struct rtattr
*) * max
);
706 while (RTA_OK(rta
, len
)) {
707 if (rta
->rta_type
<= max
&& i
< max
)
709 rta
= RTA_NEXT(rta
,len
);
712 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n", len
, rta
->rta_len
);
716 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
718 while (RTA_OK(rta
, len
)) {
719 if (rta
->rta_type
== type
)
721 rta
= RTA_NEXT(rta
, len
);
724 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n", len
, rta
->rta_len
);
728 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
731 if (RTA_PAYLOAD(rta
) < len
)
733 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
734 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
735 return parse_rtattr_nested(tb
, max
, rta
);
737 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));