]>
git.proxmox.com Git - mirror_iproute2.git/blob - lib/libnetlink.c
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 MIN(a, b) ((a) < (b) ? (a) : (b))
32 int rcvbuf
= 1024 * 1024;
34 void rtnl_close(struct rtnl_handle
*rth
)
42 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned subscriptions
,
48 memset(rth
, 0, sizeof(*rth
));
50 rth
->proto
= protocol
;
51 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
53 perror("Cannot open netlink socket");
57 if (setsockopt(rth
->fd
,SOL_SOCKET
,SO_SNDBUF
,&sndbuf
,sizeof(sndbuf
)) < 0) {
62 if (setsockopt(rth
->fd
,SOL_SOCKET
,SO_RCVBUF
,&rcvbuf
,sizeof(rcvbuf
)) < 0) {
67 memset(&rth
->local
, 0, sizeof(rth
->local
));
68 rth
->local
.nl_family
= AF_NETLINK
;
69 rth
->local
.nl_groups
= subscriptions
;
71 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
, sizeof(rth
->local
)) < 0) {
72 perror("Cannot bind netlink socket");
75 addr_len
= sizeof(rth
->local
);
76 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
, &addr_len
) < 0) {
77 perror("Cannot getsockname");
80 if (addr_len
!= sizeof(rth
->local
)) {
81 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
84 if (rth
->local
.nl_family
!= AF_NETLINK
) {
85 fprintf(stderr
, "Wrong address family %d\n", rth
->local
.nl_family
);
88 rth
->seq
= time(NULL
);
92 int rtnl_open(struct rtnl_handle
*rth
, unsigned subscriptions
)
94 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
97 int rtnl_wilddump_request(struct rtnl_handle
*rth
, int family
, int type
)
99 return rtnl_wilddump_req_filter(rth
, family
, type
, RTEXT_FILTER_VF
);
102 int rtnl_wilddump_req_filter(struct rtnl_handle
*rth
, int family
, int type
,
107 struct ifinfomsg ifm
;
108 /* attribute has to be NLMSG aligned */
109 struct rtattr ext_req
__attribute__ ((aligned(NLMSG_ALIGNTO
)));
110 __u32 ext_filter_mask
;
113 memset(&req
, 0, sizeof(req
));
114 req
.nlh
.nlmsg_len
= sizeof(req
);
115 req
.nlh
.nlmsg_type
= type
;
116 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
117 req
.nlh
.nlmsg_pid
= 0;
118 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
119 req
.ifm
.ifi_family
= family
;
121 req
.ext_req
.rta_type
= IFLA_EXT_MASK
;
122 req
.ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
));
123 req
.ext_filter_mask
= filt_mask
;
125 return send(rth
->fd
, (void*)&req
, sizeof(req
), 0);
128 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
130 return send(rth
->fd
, buf
, len
, 0);
133 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
139 status
= send(rth
->fd
, buf
, len
, 0);
143 /* Check for immediate errors */
144 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
151 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
152 h
= NLMSG_NEXT(h
, status
)) {
153 if (h
->nlmsg_type
== NLMSG_ERROR
) {
154 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
155 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
156 fprintf(stderr
, "ERROR truncated\n");
166 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
169 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
170 struct iovec iov
[2] = {
171 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
172 { .iov_base
= req
, .iov_len
= len
}
174 struct msghdr msg
= {
176 .msg_namelen
= sizeof(nladdr
),
181 nlh
.nlmsg_len
= NLMSG_LENGTH(len
);
182 nlh
.nlmsg_type
= type
;
183 nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
185 nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
187 return sendmsg(rth
->fd
, &msg
, 0);
190 int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
191 const struct rtnl_dump_filter_arg
*arg
)
193 struct sockaddr_nl nladdr
;
195 struct msghdr msg
= {
197 .msg_namelen
= sizeof(nladdr
),
207 const struct rtnl_dump_filter_arg
*a
;
211 iov
.iov_len
= sizeof(buf
);
212 status
= recvmsg(rth
->fd
, &msg
, 0);
215 if (errno
== EINTR
|| errno
== EAGAIN
)
217 fprintf(stderr
, "netlink receive error %s (%d)\n",
218 strerror(errno
), errno
);
223 fprintf(stderr
, "EOF on netlink\n");
228 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
230 for (a
= arg
; a
->filter
; a
++) {
231 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
234 while (NLMSG_OK(h
, msglen
)) {
237 if (nladdr
.nl_pid
!= 0 ||
238 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
239 h
->nlmsg_seq
!= rth
->dump
)
242 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
245 if (h
->nlmsg_type
== NLMSG_DONE
) {
247 break; /* process next filter */
249 if (h
->nlmsg_type
== NLMSG_ERROR
) {
250 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
251 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
253 "ERROR truncated\n");
256 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
258 errno
== EOPNOTSUPP
))
261 perror("RTNETLINK answers");
267 err
= a
->filter(&nladdr
, h
, a
->arg1
);
273 h
= NLMSG_NEXT(h
, msglen
);
280 "Dump was interrupted and may be inconsistent.\n");
284 if (msg
.msg_flags
& MSG_TRUNC
) {
285 fprintf(stderr
, "Message truncated\n");
289 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
295 int rtnl_dump_filter(struct rtnl_handle
*rth
,
296 rtnl_filter_t filter
,
299 const struct rtnl_dump_filter_arg a
[2] = {
300 { .filter
= filter
, .arg1
= arg1
, },
301 { .filter
= NULL
, .arg1
= NULL
, },
304 return rtnl_dump_filter_l(rth
, a
);
307 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
308 struct nlmsghdr
*answer
, size_t len
)
313 struct sockaddr_nl nladdr
;
315 .iov_base
= (void*) n
,
316 .iov_len
= n
->nlmsg_len
318 struct msghdr msg
= {
320 .msg_namelen
= sizeof(nladdr
),
326 memset(&nladdr
, 0, sizeof(nladdr
));
327 nladdr
.nl_family
= AF_NETLINK
;
329 n
->nlmsg_seq
= seq
= ++rtnl
->seq
;
332 n
->nlmsg_flags
|= NLM_F_ACK
;
334 status
= sendmsg(rtnl
->fd
, &msg
, 0);
336 perror("Cannot talk to rtnetlink");
340 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
!= 0 ||
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");
388 } else if (!err
->error
) {
391 MIN(len
, h
->nlmsg_len
));
395 fprintf(stderr
, "RTNETLINK answers: %s\n",
396 strerror(-err
->error
));
403 MIN(len
, h
->nlmsg_len
));
407 fprintf(stderr
, "Unexpected reply!!!\n");
409 status
-= NLMSG_ALIGN(len
);
410 h
= (struct nlmsghdr
*)((char*)h
+ NLMSG_ALIGN(len
));
413 if (msg
.msg_flags
& MSG_TRUNC
) {
414 fprintf(stderr
, "Message truncated\n");
419 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
425 int rtnl_listen(struct rtnl_handle
*rtnl
,
426 rtnl_filter_t handler
,
431 struct sockaddr_nl nladdr
;
433 struct msghdr msg
= {
435 .msg_namelen
= sizeof(nladdr
),
441 memset(&nladdr
, 0, sizeof(nladdr
));
442 nladdr
.nl_family
= AF_NETLINK
;
444 nladdr
.nl_groups
= 0;
448 iov
.iov_len
= sizeof(buf
);
449 status
= recvmsg(rtnl
->fd
, &msg
, 0);
452 if (errno
== EINTR
|| errno
== EAGAIN
)
454 fprintf(stderr
, "netlink receive error %s (%d)\n",
455 strerror(errno
), errno
);
456 if (errno
== ENOBUFS
)
461 fprintf(stderr
, "EOF on netlink\n");
464 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
465 fprintf(stderr
, "Sender address length == %d\n", msg
.msg_namelen
);
468 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
470 int len
= h
->nlmsg_len
;
471 int l
= len
- sizeof(*h
);
473 if (l
<0 || len
>status
) {
474 if (msg
.msg_flags
& MSG_TRUNC
) {
475 fprintf(stderr
, "Truncated message\n");
478 fprintf(stderr
, "!!!malformed message: len=%d\n", len
);
482 err
= handler(&nladdr
, h
, jarg
);
486 status
-= NLMSG_ALIGN(len
);
487 h
= (struct nlmsghdr
*)((char*)h
+ NLMSG_ALIGN(len
));
489 if (msg
.msg_flags
& MSG_TRUNC
) {
490 fprintf(stderr
, "Message truncated\n");
494 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
500 int rtnl_from_file(FILE *rtnl
, rtnl_filter_t handler
,
504 struct sockaddr_nl nladdr
;
506 struct nlmsghdr
*h
= (void*)buf
;
508 memset(&nladdr
, 0, sizeof(nladdr
));
509 nladdr
.nl_family
= AF_NETLINK
;
511 nladdr
.nl_groups
= 0;
517 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
522 perror("rtnl_from_file: fread");
529 l
= len
- sizeof(*h
);
531 if (l
<0 || len
>sizeof(buf
)) {
532 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
537 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
540 perror("rtnl_from_file: fread");
544 fprintf(stderr
, "rtnl-from_file: truncated message\n");
548 err
= handler(&nladdr
, h
, jarg
);
554 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
556 return addattr_l(n
, maxlen
, type
, NULL
, 0);
559 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
561 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
564 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
566 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
569 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
571 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
574 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
576 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
579 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
581 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
584 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
587 int len
= RTA_LENGTH(alen
);
590 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
591 fprintf(stderr
, "addattr_l ERROR: message exceeded bound of %d\n",maxlen
);
595 rta
->rta_type
= type
;
597 memcpy(RTA_DATA(rta
), data
, alen
);
598 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
602 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
604 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
605 fprintf(stderr
, "addraw_l ERROR: message exceeded bound of %d\n",maxlen
);
609 memcpy(NLMSG_TAIL(n
), data
, len
);
610 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
611 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
615 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
617 struct rtattr
*nest
= NLMSG_TAIL(n
);
619 addattr_l(n
, maxlen
, type
, NULL
, 0);
623 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
625 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
629 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
630 const void *data
, int len
)
632 struct rtattr
*start
= NLMSG_TAIL(n
);
634 addattr_l(n
, maxlen
, type
, data
, len
);
635 addattr_nest(n
, maxlen
, type
);
639 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
641 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
643 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
644 addattr_nest_end(n
, nest
);
648 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
650 int len
= RTA_LENGTH(4);
651 struct rtattr
*subrta
;
653 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
654 fprintf(stderr
,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen
);
657 subrta
= (struct rtattr
*)(((char*)rta
) + RTA_ALIGN(rta
->rta_len
));
658 subrta
->rta_type
= type
;
659 subrta
->rta_len
= len
;
660 memcpy(RTA_DATA(subrta
), &data
, 4);
661 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
665 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
666 const void *data
, int alen
)
668 struct rtattr
*subrta
;
669 int len
= RTA_LENGTH(alen
);
671 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
672 fprintf(stderr
,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen
);
675 subrta
= (struct rtattr
*)(((char*)rta
) + RTA_ALIGN(rta
->rta_len
));
676 subrta
->rta_type
= type
;
677 subrta
->rta_len
= len
;
678 memcpy(RTA_DATA(subrta
), data
, alen
);
679 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
683 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
685 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
688 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
689 int len
, unsigned short flags
)
693 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
694 while (RTA_OK(rta
, len
)) {
695 type
= rta
->rta_type
& ~flags
;
696 if ((type
<= max
) && (!tb
[type
]))
698 rta
= RTA_NEXT(rta
,len
);
701 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n", len
, rta
->rta_len
);
705 int parse_rtattr_byindex(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
709 memset(tb
, 0, sizeof(struct rtattr
*) * max
);
710 while (RTA_OK(rta
, len
)) {
711 if (rta
->rta_type
<= max
&& i
< max
)
713 rta
= RTA_NEXT(rta
,len
);
716 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n", len
, rta
->rta_len
);
720 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
722 while (RTA_OK(rta
, len
)) {
723 if (rta
->rta_type
== type
)
725 rta
= RTA_NEXT(rta
, len
);
728 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n", len
, rta
->rta_len
);
732 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
735 if (RTA_PAYLOAD(rta
) < len
)
737 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
738 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
739 return parse_rtattr_nested(tb
, max
, rta
);
741 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));