]>
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"
28 void rtnl_close(struct rtnl_handle
*rth
)
33 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned subscriptions
, int protocol
)
39 memset(rth
, 0, sizeof(rth
));
41 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
, protocol
);
43 perror("Cannot open netlink socket");
47 if (setsockopt(rth
->fd
,SOL_SOCKET
,SO_SNDBUF
,&sndbuf
,sizeof(sndbuf
)) < 0) {
52 if (setsockopt(rth
->fd
,SOL_SOCKET
,SO_RCVBUF
,&rcvbuf
,sizeof(rcvbuf
)) < 0) {
57 memset(&rth
->local
, 0, sizeof(rth
->local
));
58 rth
->local
.nl_family
= AF_NETLINK
;
59 rth
->local
.nl_groups
= subscriptions
;
61 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
, sizeof(rth
->local
)) < 0) {
62 perror("Cannot bind netlink socket");
65 addr_len
= sizeof(rth
->local
);
66 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
, &addr_len
) < 0) {
67 perror("Cannot getsockname");
70 if (addr_len
!= sizeof(rth
->local
)) {
71 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
74 if (rth
->local
.nl_family
!= AF_NETLINK
) {
75 fprintf(stderr
, "Wrong address family %d\n", rth
->local
.nl_family
);
78 rth
->seq
= time(NULL
);
82 int rtnl_open(struct rtnl_handle
*rth
, unsigned subscriptions
)
84 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
87 int rtnl_wilddump_request(struct rtnl_handle
*rth
, int family
, int type
)
93 struct sockaddr_nl nladdr
;
95 memset(&nladdr
, 0, sizeof(nladdr
));
96 nladdr
.nl_family
= AF_NETLINK
;
98 req
.nlh
.nlmsg_len
= sizeof(req
);
99 req
.nlh
.nlmsg_type
= type
;
100 req
.nlh
.nlmsg_flags
= NLM_F_ROOT
|NLM_F_MATCH
|NLM_F_REQUEST
;
101 req
.nlh
.nlmsg_pid
= 0;
102 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
103 req
.g
.rtgen_family
= family
;
105 return sendto(rth
->fd
, (void*)&req
, sizeof(req
), 0, (struct sockaddr
*)&nladdr
, sizeof(nladdr
));
108 int rtnl_send(struct rtnl_handle
*rth
, const char *buf
, int len
)
110 struct sockaddr_nl nladdr
;
112 memset(&nladdr
, 0, sizeof(nladdr
));
113 nladdr
.nl_family
= AF_NETLINK
;
115 return sendto(rth
->fd
, buf
, len
, 0, (struct sockaddr
*)&nladdr
, sizeof(nladdr
));
118 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
121 struct sockaddr_nl nladdr
;
122 struct iovec iov
[2] = { { &nlh
, sizeof(nlh
) }, { req
, len
} };
123 struct msghdr msg
= {
124 (void*)&nladdr
, sizeof(nladdr
),
130 memset(&nladdr
, 0, sizeof(nladdr
));
131 nladdr
.nl_family
= AF_NETLINK
;
133 nlh
.nlmsg_len
= NLMSG_LENGTH(len
);
134 nlh
.nlmsg_type
= type
;
135 nlh
.nlmsg_flags
= NLM_F_ROOT
|NLM_F_MATCH
|NLM_F_REQUEST
;
137 nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
139 return sendmsg(rth
->fd
, &msg
, 0);
142 int rtnl_dump_filter(struct rtnl_handle
*rth
,
143 rtnl_filter_t filter
,
149 struct sockaddr_nl nladdr
;
150 struct iovec iov
= { buf
, sizeof(buf
) };
156 struct msghdr msg
= {
157 (void*)&nladdr
, sizeof(nladdr
),
163 status
= recvmsg(rth
->fd
, &msg
, 0);
172 fprintf(stderr
, "EOF on netlink\n");
175 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
176 fprintf(stderr
, "sender address length == %d\n", msg
.msg_namelen
);
180 h
= (struct nlmsghdr
*)buf
;
181 while (NLMSG_OK(h
, status
)) {
184 if (nladdr
.nl_pid
!= 0 ||
185 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
186 h
->nlmsg_seq
!= rth
->dump
) {
188 err
= junk(&nladdr
, h
, arg2
);
195 if (h
->nlmsg_type
== NLMSG_DONE
)
197 if (h
->nlmsg_type
== NLMSG_ERROR
) {
198 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
199 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
200 fprintf(stderr
, "ERROR truncated\n");
203 perror("RTNETLINK answers");
207 err
= filter(&nladdr
, h
, arg1
);
212 h
= NLMSG_NEXT(h
, status
);
214 if (msg
.msg_flags
& MSG_TRUNC
) {
215 fprintf(stderr
, "Message truncated\n");
219 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
225 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
, pid_t peer
,
226 unsigned groups
, struct nlmsghdr
*answer
,
233 struct sockaddr_nl nladdr
;
234 struct iovec iov
= { (void*)n
, n
->nlmsg_len
};
236 struct msghdr msg
= {
237 (void*)&nladdr
, sizeof(nladdr
),
243 memset(&nladdr
, 0, sizeof(nladdr
));
244 nladdr
.nl_family
= AF_NETLINK
;
245 nladdr
.nl_pid
= peer
;
246 nladdr
.nl_groups
= groups
;
248 n
->nlmsg_seq
= seq
= ++rtnl
->seq
;
251 n
->nlmsg_flags
|= NLM_F_ACK
;
253 status
= sendmsg(rtnl
->fd
, &msg
, 0);
256 perror("Cannot talk to rtnetlink");
260 memset(buf
,0,sizeof(buf
));
265 iov
.iov_len
= sizeof(buf
);
266 status
= recvmsg(rtnl
->fd
, &msg
, 0);
275 fprintf(stderr
, "EOF on netlink\n");
278 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
279 fprintf(stderr
, "sender address length == %d\n", msg
.msg_namelen
);
282 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
284 int len
= h
->nlmsg_len
;
285 int l
= len
- sizeof(*h
);
287 if (l
<0 || len
>status
) {
288 if (msg
.msg_flags
& MSG_TRUNC
) {
289 fprintf(stderr
, "Truncated message\n");
292 fprintf(stderr
, "!!!malformed message: len=%d\n", len
);
296 if (nladdr
.nl_pid
!= peer
||
297 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
298 h
->nlmsg_seq
!= seq
) {
300 err
= junk(&nladdr
, h
, jarg
);
307 if (h
->nlmsg_type
== NLMSG_ERROR
) {
308 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
309 if (l
< sizeof(struct nlmsgerr
)) {
310 fprintf(stderr
, "ERROR truncated\n");
315 memcpy(answer
, h
, h
->nlmsg_len
);
318 perror("RTNETLINK answers");
323 memcpy(answer
, h
, h
->nlmsg_len
);
327 fprintf(stderr
, "Unexpected reply!!!\n");
329 status
-= NLMSG_ALIGN(len
);
330 h
= (struct nlmsghdr
*)((char*)h
+ NLMSG_ALIGN(len
));
332 if (msg
.msg_flags
& MSG_TRUNC
) {
333 fprintf(stderr
, "Message truncated\n");
337 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
343 int rtnl_listen(struct rtnl_handle
*rtnl
,
344 rtnl_filter_t handler
,
349 struct sockaddr_nl nladdr
;
352 struct msghdr msg
= {
353 (void*)&nladdr
, sizeof(nladdr
),
359 memset(&nladdr
, 0, sizeof(nladdr
));
360 nladdr
.nl_family
= AF_NETLINK
;
362 nladdr
.nl_groups
= 0;
368 iov
.iov_len
= sizeof(buf
);
369 status
= recvmsg(rtnl
->fd
, &msg
, 0);
378 fprintf(stderr
, "EOF on netlink\n");
381 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
382 fprintf(stderr
, "Sender address length == %d\n", msg
.msg_namelen
);
385 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
387 int len
= h
->nlmsg_len
;
388 int l
= len
- sizeof(*h
);
390 if (l
<0 || len
>status
) {
391 if (msg
.msg_flags
& MSG_TRUNC
) {
392 fprintf(stderr
, "Truncated message\n");
395 fprintf(stderr
, "!!!malformed message: len=%d\n", len
);
399 err
= handler(&nladdr
, h
, jarg
);
403 status
-= NLMSG_ALIGN(len
);
404 h
= (struct nlmsghdr
*)((char*)h
+ NLMSG_ALIGN(len
));
406 if (msg
.msg_flags
& MSG_TRUNC
) {
407 fprintf(stderr
, "Message truncated\n");
411 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
417 int rtnl_from_file(FILE *rtnl
, rtnl_filter_t handler
,
421 struct sockaddr_nl nladdr
;
423 struct nlmsghdr
*h
= (void*)buf
;
425 memset(&nladdr
, 0, sizeof(nladdr
));
426 nladdr
.nl_family
= AF_NETLINK
;
428 nladdr
.nl_groups
= 0;
434 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
439 perror("rtnl_from_file: fread");
447 l
= len
- sizeof(*h
);
449 if (l
<0 || len
>sizeof(buf
)) {
450 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
455 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
458 perror("rtnl_from_file: fread");
462 fprintf(stderr
, "rtnl-from_file: truncated message\n");
466 err
= handler(&nladdr
, h
, jarg
);
472 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
474 int len
= RTA_LENGTH(4);
476 if (NLMSG_ALIGN(n
->nlmsg_len
) + len
> maxlen
) {
477 fprintf(stderr
,"addattr32: Error! max allowed bound %d exceeded\n",maxlen
);
481 rta
->rta_type
= type
;
483 memcpy(RTA_DATA(rta
), &data
, 4);
484 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + len
;
488 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
491 int len
= RTA_LENGTH(alen
);
494 if (NLMSG_ALIGN(n
->nlmsg_len
) + len
> maxlen
) {
495 fprintf(stderr
, "addattr_l ERROR: message exceeded bound of %d\n",maxlen
);
499 rta
->rta_type
= type
;
501 memcpy(RTA_DATA(rta
), data
, alen
);
502 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + len
;
506 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
508 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
509 fprintf(stderr
, "addraw_l ERROR: message exceeded bound of %d\n",maxlen
);
513 memcpy(NLMSG_TAIL(n
), data
, len
);
514 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
515 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
519 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
521 int len
= RTA_LENGTH(4);
522 struct rtattr
*subrta
;
524 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
525 fprintf(stderr
,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen
);
528 subrta
= (struct rtattr
*)(((char*)rta
) + RTA_ALIGN(rta
->rta_len
));
529 subrta
->rta_type
= type
;
530 subrta
->rta_len
= len
;
531 memcpy(RTA_DATA(subrta
), &data
, 4);
532 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
536 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
537 const void *data
, int alen
)
539 struct rtattr
*subrta
;
540 int len
= RTA_LENGTH(alen
);
542 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
543 fprintf(stderr
,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen
);
546 subrta
= (struct rtattr
*)(((char*)rta
) + RTA_ALIGN(rta
->rta_len
));
547 subrta
->rta_type
= type
;
548 subrta
->rta_len
= len
;
549 memcpy(RTA_DATA(subrta
), data
, alen
);
550 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
554 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
556 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
557 while (RTA_OK(rta
, len
)) {
558 if (rta
->rta_type
<= max
)
559 tb
[rta
->rta_type
] = rta
;
560 rta
= RTA_NEXT(rta
,len
);
563 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n", len
, rta
->rta_len
);
567 int parse_rtattr_byindex(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
571 memset(tb
, 0, sizeof(struct rtattr
*) * max
);
572 while (RTA_OK(rta
, len
)) {
573 if (rta
->rta_type
<= max
&& i
< max
)
575 rta
= RTA_NEXT(rta
,len
);
578 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n", len
, rta
->rta_len
);