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)
97 __u16 align_rta; /* attribute has to be 32bit aligned */
98 struct rtattr ext_req;
99 __u32 ext_filter_mask;
102 memset(&req, 0, sizeof(req));
103 req.nlh.nlmsg_len = sizeof(req);
104 req.nlh.nlmsg_type = type;
105 req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
106 req.nlh.nlmsg_pid = 0;
107 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
108 req.g.rtgen_family = family;
110 req.ext_req.rta_type = IFLA_EXT_MASK;
111 req.ext_req.rta_len = RTA_LENGTH(sizeof(__u32));
112 req.ext_filter_mask = RTEXT_FILTER_VF;
114 return send(rth->fd, (void*)&req, sizeof(req), 0);
117 int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
119 return send(rth->fd, buf, len, 0);
122 int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
128 status = send(rth->fd, buf, len, 0);
132 /* Check for immediate errors */
133 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
140 for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status);
141 h = NLMSG_NEXT(h, status)) {
142 if (h->nlmsg_type == NLMSG_ERROR) {
143 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
144 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
145 fprintf(stderr, "ERROR truncated\n");
155 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
158 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
159 struct iovec iov[2] = {
160 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
161 { .iov_base = req, .iov_len = len }
163 struct msghdr msg = {
165 .msg_namelen = sizeof(nladdr),
170 nlh.nlmsg_len = NLMSG_LENGTH(len);
171 nlh.nlmsg_type = type;
172 nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
174 nlh.nlmsg_seq = rth->dump = ++rth->seq;
176 return sendmsg(rth->fd, &msg, 0);
179 int rtnl_dump_filter_l(struct rtnl_handle *rth,
180 const struct rtnl_dump_filter_arg *arg)
182 struct sockaddr_nl nladdr;
184 struct msghdr msg = {
186 .msg_namelen = sizeof(nladdr),
195 const struct rtnl_dump_filter_arg *a;
199 iov.iov_len = sizeof(buf);
200 status = recvmsg(rth->fd, &msg, 0);
203 if (errno == EINTR || errno == EAGAIN)
205 fprintf(stderr, "netlink receive error %s (%d)\n",
206 strerror(errno), errno);
211 fprintf(stderr, "EOF on netlink\n");
215 for (a = arg; a->filter; a++) {
216 struct nlmsghdr *h = (struct nlmsghdr*)buf;
219 while (NLMSG_OK(h, msglen)) {
222 if (nladdr.nl_pid != 0 ||
223 h->nlmsg_pid != rth->local.nl_pid ||
224 h->nlmsg_seq != rth->dump)
227 if (h->nlmsg_type == NLMSG_DONE) {
229 break; /* process next filter */
231 if (h->nlmsg_type == NLMSG_ERROR) {
232 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
233 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
235 "ERROR truncated\n");
238 perror("RTNETLINK answers");
242 err = a->filter(&nladdr, h, a->arg1);
247 h = NLMSG_NEXT(h, msglen);
254 if (msg.msg_flags & MSG_TRUNC) {
255 fprintf(stderr, "Message truncated\n");
259 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
265 int rtnl_dump_filter(struct rtnl_handle *rth,
266 rtnl_filter_t filter,
269 const struct rtnl_dump_filter_arg a[2] = {
270 { .filter = filter, .arg1 = arg1, },
271 { .filter = NULL, .arg1 = NULL, },
274 return rtnl_dump_filter_l(rth, a);
277 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
278 unsigned groups, struct nlmsghdr *answer)
283 struct sockaddr_nl nladdr;
285 .iov_base = (void*) n,
286 .iov_len = n->nlmsg_len
288 struct msghdr msg = {
290 .msg_namelen = sizeof(nladdr),
296 memset(&nladdr, 0, sizeof(nladdr));
297 nladdr.nl_family = AF_NETLINK;
298 nladdr.nl_pid = peer;
299 nladdr.nl_groups = groups;
301 n->nlmsg_seq = seq = ++rtnl->seq;
304 n->nlmsg_flags |= NLM_F_ACK;
306 status = sendmsg(rtnl->fd, &msg, 0);
309 perror("Cannot talk to rtnetlink");
313 memset(buf,0,sizeof(buf));
318 iov.iov_len = sizeof(buf);
319 status = recvmsg(rtnl->fd, &msg, 0);
322 if (errno == EINTR || errno == EAGAIN)
324 fprintf(stderr, "netlink receive error %s (%d)\n",
325 strerror(errno), errno);
329 fprintf(stderr, "EOF on netlink\n");
332 if (msg.msg_namelen != sizeof(nladdr)) {
333 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
336 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
337 int len = h->nlmsg_len;
338 int l = len - sizeof(*h);
340 if (l < 0 || len>status) {
341 if (msg.msg_flags & MSG_TRUNC) {
342 fprintf(stderr, "Truncated message\n");
345 fprintf(stderr, "!!!malformed message: len=%d\n", len);
349 if (nladdr.nl_pid != peer ||
350 h->nlmsg_pid != rtnl->local.nl_pid ||
351 h->nlmsg_seq != seq) {
352 /* Don't forget to skip that message. */
353 status -= NLMSG_ALIGN(len);
354 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
358 if (h->nlmsg_type == NLMSG_ERROR) {
359 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
360 if (l < sizeof(struct nlmsgerr)) {
361 fprintf(stderr, "ERROR truncated\n");
366 memcpy(answer, h, h->nlmsg_len);
369 perror("RTNETLINK answers");
374 memcpy(answer, h, h->nlmsg_len);
378 fprintf(stderr, "Unexpected reply!!!\n");
380 status -= NLMSG_ALIGN(len);
381 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
383 if (msg.msg_flags & MSG_TRUNC) {
384 fprintf(stderr, "Message truncated\n");
388 fprintf(stderr, "!!!Remnant of size %d\n", status);
394 int rtnl_listen(struct rtnl_handle *rtnl,
395 rtnl_filter_t handler,
400 struct sockaddr_nl nladdr;
402 struct msghdr msg = {
404 .msg_namelen = sizeof(nladdr),
410 memset(&nladdr, 0, sizeof(nladdr));
411 nladdr.nl_family = AF_NETLINK;
413 nladdr.nl_groups = 0;
417 iov.iov_len = sizeof(buf);
418 status = recvmsg(rtnl->fd, &msg, 0);
421 if (errno == EINTR || errno == EAGAIN)
423 fprintf(stderr, "netlink receive error %s (%d)\n",
424 strerror(errno), errno);
425 if (errno == ENOBUFS)
430 fprintf(stderr, "EOF on netlink\n");
433 if (msg.msg_namelen != sizeof(nladdr)) {
434 fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
437 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
439 int len = h->nlmsg_len;
440 int l = len - sizeof(*h);
442 if (l<0 || len>status) {
443 if (msg.msg_flags & MSG_TRUNC) {
444 fprintf(stderr, "Truncated message\n");
447 fprintf(stderr, "!!!malformed message: len=%d\n", len);
451 err = handler(&nladdr, h, jarg);
455 status -= NLMSG_ALIGN(len);
456 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
458 if (msg.msg_flags & MSG_TRUNC) {
459 fprintf(stderr, "Message truncated\n");
463 fprintf(stderr, "!!!Remnant of size %d\n", status);
469 int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
473 struct sockaddr_nl nladdr;
475 struct nlmsghdr *h = (void*)buf;
477 memset(&nladdr, 0, sizeof(nladdr));
478 nladdr.nl_family = AF_NETLINK;
480 nladdr.nl_groups = 0;
486 status = fread(&buf, 1, sizeof(*h), rtnl);
491 perror("rtnl_from_file: fread");
498 l = len - sizeof(*h);
500 if (l<0 || len>sizeof(buf)) {
501 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
506 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
509 perror("rtnl_from_file: fread");
513 fprintf(stderr, "rtnl-from_file: truncated message\n");
517 err = handler(&nladdr, h, jarg);
523 int addattr(struct nlmsghdr *n, int maxlen, int type)
525 return addattr_l(n, maxlen, type, NULL, 0);
528 int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
530 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
533 int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
535 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
538 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
540 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
543 int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
545 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
548 int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
550 return addattr_l(n, maxlen, type, str, strlen(str)+1);
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));