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_l(struct rtnl_handle *rth,
176 const struct rtnl_dump_filter_arg *arg)
178 struct sockaddr_nl nladdr;
180 struct msghdr msg = {
182 .msg_namelen = sizeof(nladdr),
191 const struct rtnl_dump_filter_arg *a;
195 iov.iov_len = sizeof(buf);
196 status = recvmsg(rth->fd, &msg, 0);
199 if (errno == EINTR || errno == EAGAIN)
201 fprintf(stderr, "netlink receive error %s (%d)\n",
202 strerror(errno), errno);
207 fprintf(stderr, "EOF on netlink\n");
211 for (a = arg; a->filter; a++) {
212 struct nlmsghdr *h = (struct nlmsghdr*)buf;
215 while (NLMSG_OK(h, msglen)) {
218 if (nladdr.nl_pid != 0 ||
219 h->nlmsg_pid != rth->local.nl_pid ||
220 h->nlmsg_seq != rth->dump) {
222 err = a->junk(&nladdr, h,
230 if (h->nlmsg_type == NLMSG_DONE) {
232 break; /* process next filter */
234 if (h->nlmsg_type == NLMSG_ERROR) {
235 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
236 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
238 "ERROR truncated\n");
241 perror("RTNETLINK answers");
245 err = a->filter(&nladdr, h, a->arg1);
250 h = NLMSG_NEXT(h, msglen);
257 if (msg.msg_flags & MSG_TRUNC) {
258 fprintf(stderr, "Message truncated\n");
262 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
268 int rtnl_dump_filter(struct rtnl_handle *rth,
269 rtnl_filter_t filter,
274 const struct rtnl_dump_filter_arg a[2] = {
275 { .filter = filter, .arg1 = arg1, .junk = junk, .arg2 = arg2 },
276 { .filter = NULL, .arg1 = NULL, .junk = NULL, .arg2 = NULL }
279 return rtnl_dump_filter_l(rth, a);
282 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
283 unsigned groups, struct nlmsghdr *answer,
290 struct sockaddr_nl nladdr;
292 .iov_base = (void*) n,
293 .iov_len = n->nlmsg_len
295 struct msghdr msg = {
297 .msg_namelen = sizeof(nladdr),
303 memset(&nladdr, 0, sizeof(nladdr));
304 nladdr.nl_family = AF_NETLINK;
305 nladdr.nl_pid = peer;
306 nladdr.nl_groups = groups;
308 n->nlmsg_seq = seq = ++rtnl->seq;
311 n->nlmsg_flags |= NLM_F_ACK;
313 status = sendmsg(rtnl->fd, &msg, 0);
316 perror("Cannot talk to rtnetlink");
320 memset(buf,0,sizeof(buf));
325 iov.iov_len = sizeof(buf);
326 status = recvmsg(rtnl->fd, &msg, 0);
329 if (errno == EINTR || errno == EAGAIN)
331 fprintf(stderr, "netlink receive error %s (%d)\n",
332 strerror(errno), errno);
336 fprintf(stderr, "EOF on netlink\n");
339 if (msg.msg_namelen != sizeof(nladdr)) {
340 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
343 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
345 int len = h->nlmsg_len;
346 int l = len - sizeof(*h);
348 if (l<0 || len>status) {
349 if (msg.msg_flags & MSG_TRUNC) {
350 fprintf(stderr, "Truncated message\n");
353 fprintf(stderr, "!!!malformed message: len=%d\n", len);
357 if (nladdr.nl_pid != peer ||
358 h->nlmsg_pid != rtnl->local.nl_pid ||
359 h->nlmsg_seq != seq) {
361 err = junk(&nladdr, h, jarg);
365 /* Don't forget to skip that message. */
366 status -= NLMSG_ALIGN(len);
367 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
371 if (h->nlmsg_type == NLMSG_ERROR) {
372 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
373 if (l < sizeof(struct nlmsgerr)) {
374 fprintf(stderr, "ERROR truncated\n");
379 memcpy(answer, h, h->nlmsg_len);
382 perror("RTNETLINK answers");
387 memcpy(answer, h, h->nlmsg_len);
391 fprintf(stderr, "Unexpected reply!!!\n");
393 status -= NLMSG_ALIGN(len);
394 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
396 if (msg.msg_flags & MSG_TRUNC) {
397 fprintf(stderr, "Message truncated\n");
401 fprintf(stderr, "!!!Remnant of size %d\n", status);
407 int rtnl_listen(struct rtnl_handle *rtnl,
408 rtnl_filter_t handler,
413 struct sockaddr_nl nladdr;
415 struct msghdr msg = {
417 .msg_namelen = sizeof(nladdr),
423 memset(&nladdr, 0, sizeof(nladdr));
424 nladdr.nl_family = AF_NETLINK;
426 nladdr.nl_groups = 0;
430 iov.iov_len = sizeof(buf);
431 status = recvmsg(rtnl->fd, &msg, 0);
434 if (errno == EINTR || errno == EAGAIN)
436 fprintf(stderr, "netlink receive error %s (%d)\n",
437 strerror(errno), errno);
438 if (errno == ENOBUFS)
443 fprintf(stderr, "EOF on netlink\n");
446 if (msg.msg_namelen != sizeof(nladdr)) {
447 fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
450 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
452 int len = h->nlmsg_len;
453 int l = len - sizeof(*h);
455 if (l<0 || len>status) {
456 if (msg.msg_flags & MSG_TRUNC) {
457 fprintf(stderr, "Truncated message\n");
460 fprintf(stderr, "!!!malformed message: len=%d\n", len);
464 err = handler(&nladdr, h, jarg);
468 status -= NLMSG_ALIGN(len);
469 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
471 if (msg.msg_flags & MSG_TRUNC) {
472 fprintf(stderr, "Message truncated\n");
476 fprintf(stderr, "!!!Remnant of size %d\n", status);
482 int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
486 struct sockaddr_nl nladdr;
488 struct nlmsghdr *h = (void*)buf;
490 memset(&nladdr, 0, sizeof(nladdr));
491 nladdr.nl_family = AF_NETLINK;
493 nladdr.nl_groups = 0;
499 status = fread(&buf, 1, sizeof(*h), rtnl);
504 perror("rtnl_from_file: fread");
512 l = len - sizeof(*h);
514 if (l<0 || len>sizeof(buf)) {
515 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
520 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
523 perror("rtnl_from_file: fread");
527 fprintf(stderr, "rtnl-from_file: truncated message\n");
531 err = handler(&nladdr, h, jarg);
537 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
539 int len = RTA_LENGTH(4);
541 if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) {
542 fprintf(stderr,"addattr32: Error! max allowed bound %d exceeded\n",maxlen);
546 rta->rta_type = type;
548 memcpy(RTA_DATA(rta), &data, 4);
549 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
553 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
556 int len = RTA_LENGTH(alen);
559 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
560 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
564 rta->rta_type = type;
566 memcpy(RTA_DATA(rta), data, alen);
567 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
571 int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
573 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
574 fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen);
578 memcpy(NLMSG_TAIL(n), data, len);
579 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
580 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
584 struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
586 struct rtattr *nest = NLMSG_TAIL(n);
588 addattr_l(n, maxlen, type, NULL, 0);
592 int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
594 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
598 struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
599 const void *data, int len)
601 struct rtattr *start = NLMSG_TAIL(n);
603 addattr_l(n, maxlen, type, data, len);
604 addattr_nest(n, maxlen, type);
608 int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
610 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
612 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
613 addattr_nest_end(n, nest);
617 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
619 int len = RTA_LENGTH(4);
620 struct rtattr *subrta;
622 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
623 fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen);
626 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
627 subrta->rta_type = type;
628 subrta->rta_len = len;
629 memcpy(RTA_DATA(subrta), &data, 4);
630 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
634 int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
635 const void *data, int alen)
637 struct rtattr *subrta;
638 int len = RTA_LENGTH(alen);
640 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
641 fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen);
644 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
645 subrta->rta_type = type;
646 subrta->rta_len = len;
647 memcpy(RTA_DATA(subrta), data, alen);
648 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
652 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
654 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
655 while (RTA_OK(rta, len)) {
656 if ((rta->rta_type <= max) && (!tb[rta->rta_type]))
657 tb[rta->rta_type] = rta;
658 rta = RTA_NEXT(rta,len);
661 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
665 int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len)
669 memset(tb, 0, sizeof(struct rtattr *) * max);
670 while (RTA_OK(rta, len)) {
671 if (rta->rta_type <= max && i < max)
673 rta = RTA_NEXT(rta,len);
676 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
680 int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta,
683 if (RTA_PAYLOAD(rta) < len)
685 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
686 rta = RTA_DATA(rta) + RTA_ALIGN(len);
687 return parse_rtattr_nested(tb, max, rta);
689 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));