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_DUMP|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 void *buf, int len)
112 return send(rth->fd, buf, len, 0);
115 int rtnl_send_check(struct rtnl_handle *rth, const void *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 = { .nl_family = AF_NETLINK };
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 nlh.nlmsg_len = NLMSG_LENGTH(len);
164 nlh.nlmsg_type = type;
165 nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
167 nlh.nlmsg_seq = rth->dump = ++rth->seq;
169 return sendmsg(rth->fd, &msg, 0);
172 int rtnl_dump_filter_l(struct rtnl_handle *rth,
173 const struct rtnl_dump_filter_arg *arg)
175 struct sockaddr_nl nladdr;
177 struct msghdr msg = {
179 .msg_namelen = sizeof(nladdr),
188 const struct rtnl_dump_filter_arg *a;
192 iov.iov_len = sizeof(buf);
193 status = recvmsg(rth->fd, &msg, 0);
196 if (errno == EINTR || errno == EAGAIN)
198 fprintf(stderr, "netlink receive error %s (%d)\n",
199 strerror(errno), errno);
204 fprintf(stderr, "EOF on netlink\n");
208 for (a = arg; a->filter; a++) {
209 struct nlmsghdr *h = (struct nlmsghdr*)buf;
212 while (NLMSG_OK(h, msglen)) {
215 if (nladdr.nl_pid != 0 ||
216 h->nlmsg_pid != rth->local.nl_pid ||
217 h->nlmsg_seq != rth->dump)
220 if (h->nlmsg_type == NLMSG_DONE) {
222 break; /* process next filter */
224 if (h->nlmsg_type == NLMSG_ERROR) {
225 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
226 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
228 "ERROR truncated\n");
231 perror("RTNETLINK answers");
235 err = a->filter(&nladdr, h, a->arg1);
240 h = NLMSG_NEXT(h, msglen);
247 if (msg.msg_flags & MSG_TRUNC) {
248 fprintf(stderr, "Message truncated\n");
252 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
258 int rtnl_dump_filter(struct rtnl_handle *rth,
259 rtnl_filter_t filter,
262 const struct rtnl_dump_filter_arg a[2] = {
263 { .filter = filter, .arg1 = arg1, },
264 { .filter = NULL, .arg1 = NULL, },
267 return rtnl_dump_filter_l(rth, a);
270 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
271 unsigned groups, struct nlmsghdr *answer)
276 struct sockaddr_nl nladdr;
278 .iov_base = (void*) n,
279 .iov_len = n->nlmsg_len
281 struct msghdr msg = {
283 .msg_namelen = sizeof(nladdr),
289 memset(&nladdr, 0, sizeof(nladdr));
290 nladdr.nl_family = AF_NETLINK;
291 nladdr.nl_pid = peer;
292 nladdr.nl_groups = groups;
294 n->nlmsg_seq = seq = ++rtnl->seq;
297 n->nlmsg_flags |= NLM_F_ACK;
299 status = sendmsg(rtnl->fd, &msg, 0);
302 perror("Cannot talk to rtnetlink");
306 memset(buf,0,sizeof(buf));
311 iov.iov_len = sizeof(buf);
312 status = recvmsg(rtnl->fd, &msg, 0);
315 if (errno == EINTR || errno == EAGAIN)
317 fprintf(stderr, "netlink receive error %s (%d)\n",
318 strerror(errno), errno);
322 fprintf(stderr, "EOF on netlink\n");
325 if (msg.msg_namelen != sizeof(nladdr)) {
326 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
329 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
330 int len = h->nlmsg_len;
331 int l = len - sizeof(*h);
333 if (l < 0 || len>status) {
334 if (msg.msg_flags & MSG_TRUNC) {
335 fprintf(stderr, "Truncated message\n");
338 fprintf(stderr, "!!!malformed message: len=%d\n", len);
342 if (nladdr.nl_pid != peer ||
343 h->nlmsg_pid != rtnl->local.nl_pid ||
344 h->nlmsg_seq != seq) {
345 /* Don't forget to skip that message. */
346 status -= NLMSG_ALIGN(len);
347 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
351 if (h->nlmsg_type == NLMSG_ERROR) {
352 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
353 if (l < sizeof(struct nlmsgerr)) {
354 fprintf(stderr, "ERROR truncated\n");
359 memcpy(answer, h, h->nlmsg_len);
362 perror("RTNETLINK answers");
367 memcpy(answer, h, h->nlmsg_len);
371 fprintf(stderr, "Unexpected reply!!!\n");
373 status -= NLMSG_ALIGN(len);
374 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
376 if (msg.msg_flags & MSG_TRUNC) {
377 fprintf(stderr, "Message truncated\n");
381 fprintf(stderr, "!!!Remnant of size %d\n", status);
387 int rtnl_listen(struct rtnl_handle *rtnl,
388 rtnl_filter_t handler,
393 struct sockaddr_nl nladdr;
395 struct msghdr msg = {
397 .msg_namelen = sizeof(nladdr),
403 memset(&nladdr, 0, sizeof(nladdr));
404 nladdr.nl_family = AF_NETLINK;
406 nladdr.nl_groups = 0;
410 iov.iov_len = sizeof(buf);
411 status = recvmsg(rtnl->fd, &msg, 0);
414 if (errno == EINTR || errno == EAGAIN)
416 fprintf(stderr, "netlink receive error %s (%d)\n",
417 strerror(errno), errno);
418 if (errno == ENOBUFS)
423 fprintf(stderr, "EOF on netlink\n");
426 if (msg.msg_namelen != sizeof(nladdr)) {
427 fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
430 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
432 int len = h->nlmsg_len;
433 int l = len - sizeof(*h);
435 if (l<0 || len>status) {
436 if (msg.msg_flags & MSG_TRUNC) {
437 fprintf(stderr, "Truncated message\n");
440 fprintf(stderr, "!!!malformed message: len=%d\n", len);
444 err = handler(&nladdr, h, jarg);
448 status -= NLMSG_ALIGN(len);
449 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
451 if (msg.msg_flags & MSG_TRUNC) {
452 fprintf(stderr, "Message truncated\n");
456 fprintf(stderr, "!!!Remnant of size %d\n", status);
462 int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
466 struct sockaddr_nl nladdr;
468 struct nlmsghdr *h = (void*)buf;
470 memset(&nladdr, 0, sizeof(nladdr));
471 nladdr.nl_family = AF_NETLINK;
473 nladdr.nl_groups = 0;
479 status = fread(&buf, 1, sizeof(*h), rtnl);
484 perror("rtnl_from_file: fread");
491 l = len - sizeof(*h);
493 if (l<0 || len>sizeof(buf)) {
494 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
499 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
502 perror("rtnl_from_file: fread");
506 fprintf(stderr, "rtnl-from_file: truncated message\n");
510 err = handler(&nladdr, h, jarg);
516 int addattr(struct nlmsghdr *n, int maxlen, int type)
518 return addattr_l(n, maxlen, type, NULL, 0);
521 int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
523 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
526 int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
528 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
531 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
533 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
536 int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
538 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
541 int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
543 return addattr_l(n, maxlen, type, str, strlen(str)+1);
546 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
549 int len = RTA_LENGTH(alen);
552 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
553 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
557 rta->rta_type = type;
559 memcpy(RTA_DATA(rta), data, alen);
560 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
564 int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
566 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
567 fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen);
571 memcpy(NLMSG_TAIL(n), data, len);
572 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
573 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
577 struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
579 struct rtattr *nest = NLMSG_TAIL(n);
581 addattr_l(n, maxlen, type, NULL, 0);
585 int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
587 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
591 struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
592 const void *data, int len)
594 struct rtattr *start = NLMSG_TAIL(n);
596 addattr_l(n, maxlen, type, data, len);
597 addattr_nest(n, maxlen, type);
601 int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
603 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
605 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
606 addattr_nest_end(n, nest);
610 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
612 int len = RTA_LENGTH(4);
613 struct rtattr *subrta;
615 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
616 fprintf(stderr,"rta_addattr32: 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, 4);
623 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
627 int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
628 const void *data, int alen)
630 struct rtattr *subrta;
631 int len = RTA_LENGTH(alen);
633 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
634 fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen);
637 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
638 subrta->rta_type = type;
639 subrta->rta_len = len;
640 memcpy(RTA_DATA(subrta), data, alen);
641 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
645 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
647 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
648 while (RTA_OK(rta, len)) {
649 if ((rta->rta_type <= max) && (!tb[rta->rta_type]))
650 tb[rta->rta_type] = rta;
651 rta = RTA_NEXT(rta,len);
654 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
658 int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len)
662 memset(tb, 0, sizeof(struct rtattr *) * max);
663 while (RTA_OK(rta, len)) {
664 if (rta->rta_type <= max && i < max)
666 rta = RTA_NEXT(rta,len);
669 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
673 int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta,
676 if (RTA_PAYLOAD(rta) < len)
678 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
679 rta = RTA_DATA(rta) + RTA_ALIGN(len);
680 return parse_rtattr_nested(tb, max, rta);
682 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));