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->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
48 perror("Cannot open netlink socket");
52 if (setsockopt(rth->fd,SOL_SOCKET,SO_SNDBUF,&sndbuf,sizeof(sndbuf)) < 0) {
57 if (setsockopt(rth->fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf)) < 0) {
62 memset(&rth->local, 0, sizeof(rth->local));
63 rth->local.nl_family = AF_NETLINK;
64 rth->local.nl_groups = subscriptions;
66 if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
67 perror("Cannot bind netlink socket");
70 addr_len = sizeof(rth->local);
71 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
72 perror("Cannot getsockname");
75 if (addr_len != sizeof(rth->local)) {
76 fprintf(stderr, "Wrong address length %d\n", addr_len);
79 if (rth->local.nl_family != AF_NETLINK) {
80 fprintf(stderr, "Wrong address family %d\n", rth->local.nl_family);
83 rth->seq = time(NULL);
87 int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
89 return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
92 int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
99 memset(&req, 0, sizeof(req));
100 req.nlh.nlmsg_len = sizeof(req);
101 req.nlh.nlmsg_type = type;
102 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
103 req.nlh.nlmsg_pid = 0;
104 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
105 req.g.rtgen_family = family;
107 return send(rth->fd, (void*)&req, sizeof(req), 0);
110 int rtnl_send(struct rtnl_handle *rth, const char *buf, int len)
112 return send(rth->fd, buf, len, 0);
115 int rtnl_send_check(struct rtnl_handle *rth, const char *buf, int len)
121 status = send(rth->fd, buf, len, 0);
125 /* Check for immediate errors */
126 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
133 for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status);
134 h = NLMSG_NEXT(h, status)) {
135 if (h->nlmsg_type == NLMSG_ERROR) {
136 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
137 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
138 fprintf(stderr, "ERROR truncated\n");
148 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
151 struct sockaddr_nl nladdr;
152 struct iovec iov[2] = {
153 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
154 { .iov_base = req, .iov_len = len }
156 struct msghdr msg = {
158 .msg_namelen = sizeof(nladdr),
163 memset(&nladdr, 0, sizeof(nladdr));
164 nladdr.nl_family = AF_NETLINK;
166 nlh.nlmsg_len = NLMSG_LENGTH(len);
167 nlh.nlmsg_type = type;
168 nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
170 nlh.nlmsg_seq = rth->dump = ++rth->seq;
172 return sendmsg(rth->fd, &msg, 0);
175 int rtnl_dump_filter(struct rtnl_handle *rth,
176 rtnl_filter_t filter,
181 struct sockaddr_nl nladdr;
183 struct msghdr msg = {
185 .msg_namelen = sizeof(nladdr),
196 iov.iov_len = sizeof(buf);
197 status = recvmsg(rth->fd, &msg, 0);
200 if (errno == EINTR || errno == EAGAIN)
202 fprintf(stderr, "netlink receive error %s (%d)\n",
203 strerror(errno), errno);
208 fprintf(stderr, "EOF on netlink\n");
212 h = (struct nlmsghdr*)buf;
213 while (NLMSG_OK(h, status)) {
216 if (nladdr.nl_pid != 0 ||
217 h->nlmsg_pid != rth->local.nl_pid ||
218 h->nlmsg_seq != rth->dump) {
220 err = junk(&nladdr, h, arg2);
227 if (h->nlmsg_type == NLMSG_DONE)
229 if (h->nlmsg_type == NLMSG_ERROR) {
230 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
231 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
232 fprintf(stderr, "ERROR truncated\n");
235 perror("RTNETLINK answers");
239 err = filter(&nladdr, h, arg1);
244 h = NLMSG_NEXT(h, status);
246 if (msg.msg_flags & MSG_TRUNC) {
247 fprintf(stderr, "Message truncated\n");
251 fprintf(stderr, "!!!Remnant of size %d\n", status);
257 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
258 unsigned groups, struct nlmsghdr *answer,
265 struct sockaddr_nl nladdr;
267 .iov_base = (void*) n,
268 .iov_len = n->nlmsg_len
270 struct msghdr msg = {
272 .msg_namelen = sizeof(nladdr),
278 memset(&nladdr, 0, sizeof(nladdr));
279 nladdr.nl_family = AF_NETLINK;
280 nladdr.nl_pid = peer;
281 nladdr.nl_groups = groups;
283 n->nlmsg_seq = seq = ++rtnl->seq;
286 n->nlmsg_flags |= NLM_F_ACK;
288 status = sendmsg(rtnl->fd, &msg, 0);
291 perror("Cannot talk to rtnetlink");
295 memset(buf,0,sizeof(buf));
300 iov.iov_len = sizeof(buf);
301 status = recvmsg(rtnl->fd, &msg, 0);
304 if (errno == EINTR || errno == EAGAIN)
306 fprintf(stderr, "netlink receive error %s (%d)\n",
307 strerror(errno), errno);
311 fprintf(stderr, "EOF on netlink\n");
314 if (msg.msg_namelen != sizeof(nladdr)) {
315 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
318 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
320 int len = h->nlmsg_len;
321 int l = len - sizeof(*h);
323 if (l<0 || len>status) {
324 if (msg.msg_flags & MSG_TRUNC) {
325 fprintf(stderr, "Truncated message\n");
328 fprintf(stderr, "!!!malformed message: len=%d\n", len);
332 if (nladdr.nl_pid != peer ||
333 h->nlmsg_pid != rtnl->local.nl_pid ||
334 h->nlmsg_seq != seq) {
336 err = junk(&nladdr, h, jarg);
340 /* Don't forget to skip that message. */
341 status -= NLMSG_ALIGN(len);
342 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
346 if (h->nlmsg_type == NLMSG_ERROR) {
347 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
348 if (l < sizeof(struct nlmsgerr)) {
349 fprintf(stderr, "ERROR truncated\n");
354 memcpy(answer, h, h->nlmsg_len);
357 perror("RTNETLINK answers");
362 memcpy(answer, h, h->nlmsg_len);
366 fprintf(stderr, "Unexpected reply!!!\n");
368 status -= NLMSG_ALIGN(len);
369 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
371 if (msg.msg_flags & MSG_TRUNC) {
372 fprintf(stderr, "Message truncated\n");
376 fprintf(stderr, "!!!Remnant of size %d\n", status);
382 int rtnl_listen(struct rtnl_handle *rtnl,
383 rtnl_filter_t handler,
388 struct sockaddr_nl nladdr;
390 struct msghdr msg = {
392 .msg_namelen = sizeof(nladdr),
398 memset(&nladdr, 0, sizeof(nladdr));
399 nladdr.nl_family = AF_NETLINK;
401 nladdr.nl_groups = 0;
405 iov.iov_len = sizeof(buf);
406 status = recvmsg(rtnl->fd, &msg, 0);
409 if (errno == EINTR || errno == EAGAIN)
411 fprintf(stderr, "netlink receive error %s (%d)\n",
412 strerror(errno), errno);
413 if (errno == ENOBUFS)
418 fprintf(stderr, "EOF on netlink\n");
421 if (msg.msg_namelen != sizeof(nladdr)) {
422 fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
425 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
427 int len = h->nlmsg_len;
428 int l = len - sizeof(*h);
430 if (l<0 || len>status) {
431 if (msg.msg_flags & MSG_TRUNC) {
432 fprintf(stderr, "Truncated message\n");
435 fprintf(stderr, "!!!malformed message: len=%d\n", len);
439 err = handler(&nladdr, h, jarg);
443 status -= NLMSG_ALIGN(len);
444 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
446 if (msg.msg_flags & MSG_TRUNC) {
447 fprintf(stderr, "Message truncated\n");
451 fprintf(stderr, "!!!Remnant of size %d\n", status);
457 int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
461 struct sockaddr_nl nladdr;
463 struct nlmsghdr *h = (void*)buf;
465 memset(&nladdr, 0, sizeof(nladdr));
466 nladdr.nl_family = AF_NETLINK;
468 nladdr.nl_groups = 0;
474 status = fread(&buf, 1, sizeof(*h), rtnl);
479 perror("rtnl_from_file: fread");
487 l = len - sizeof(*h);
489 if (l<0 || len>sizeof(buf)) {
490 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
495 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
498 perror("rtnl_from_file: fread");
502 fprintf(stderr, "rtnl-from_file: truncated message\n");
506 err = handler(&nladdr, h, jarg);
512 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
514 int len = RTA_LENGTH(4);
516 if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) {
517 fprintf(stderr,"addattr32: Error! max allowed bound %d exceeded\n",maxlen);
521 rta->rta_type = type;
523 memcpy(RTA_DATA(rta), &data, 4);
524 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
528 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
531 int len = RTA_LENGTH(alen);
534 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
535 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
539 rta->rta_type = type;
541 memcpy(RTA_DATA(rta), data, alen);
542 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
546 int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
548 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
549 fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen);
553 memcpy(NLMSG_TAIL(n), data, len);
554 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
555 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
559 struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
561 struct rtattr *nest = NLMSG_TAIL(n);
563 addattr_l(n, maxlen, type, NULL, 0);
567 int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
569 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
573 struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
574 const void *data, int len)
576 struct rtattr *start = NLMSG_TAIL(n);
578 addattr_l(n, maxlen, type, data, len);
579 addattr_nest(n, maxlen, type);
583 int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
585 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
587 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
588 addattr_nest_end(n, nest);
592 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
594 int len = RTA_LENGTH(4);
595 struct rtattr *subrta;
597 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
598 fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen);
601 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
602 subrta->rta_type = type;
603 subrta->rta_len = len;
604 memcpy(RTA_DATA(subrta), &data, 4);
605 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
609 int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
610 const void *data, int alen)
612 struct rtattr *subrta;
613 int len = RTA_LENGTH(alen);
615 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
616 fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen);
619 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
620 subrta->rta_type = type;
621 subrta->rta_len = len;
622 memcpy(RTA_DATA(subrta), data, alen);
623 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
627 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
629 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
630 while (RTA_OK(rta, len)) {
631 if (rta->rta_type <= max)
632 tb[rta->rta_type] = rta;
633 rta = RTA_NEXT(rta,len);
636 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
640 int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len)
644 memset(tb, 0, sizeof(struct rtattr *) * max);
645 while (RTA_OK(rta, len)) {
646 if (rta->rta_type <= max && i < max)
648 rta = RTA_NEXT(rta,len);
651 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
655 int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta,
658 if (RTA_PAYLOAD(rta) < len)
660 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
661 rta = RTA_DATA(rta) + RTA_ALIGN(len);
662 return parse_rtattr_nested(tb, max, rta);
664 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));