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>
11 * Modified by Ted Felix (www.tedfelix.com) to fix warnings.
19 #include <net/if_arp.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
27 #include "libnetlink.h"
29 void rtnl_close(struct rtnl_handle *rth)
37 int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions,
44 memset(rth, 0, sizeof(struct rtnl_handle));
46 rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, 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)
98 struct sockaddr_nl nladdr;
100 memset(&nladdr, 0, sizeof(nladdr));
101 nladdr.nl_family = AF_NETLINK;
103 memset(&req, 0, sizeof(req));
104 req.nlh.nlmsg_len = sizeof(req);
105 req.nlh.nlmsg_type = type;
106 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
107 req.nlh.nlmsg_pid = 0;
108 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
109 req.g.rtgen_family = family;
111 return sendto(rth->fd, (void*)&req, sizeof(req), 0,
112 (struct sockaddr*)&nladdr, sizeof(nladdr));
115 int rtnl_send(struct rtnl_handle *rth, const char *buf, int len)
117 struct sockaddr_nl nladdr;
119 memset(&nladdr, 0, sizeof(nladdr));
120 nladdr.nl_family = AF_NETLINK;
122 return sendto(rth->fd, buf, len, 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
125 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
128 struct sockaddr_nl nladdr;
129 struct iovec iov[2] = {
130 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
131 { .iov_base = req, .iov_len = len }
133 struct msghdr msg = {
135 .msg_namelen = sizeof(nladdr),
140 memset(&nladdr, 0, sizeof(nladdr));
141 nladdr.nl_family = AF_NETLINK;
143 nlh.nlmsg_len = NLMSG_LENGTH(len);
144 nlh.nlmsg_type = type;
145 nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
147 nlh.nlmsg_seq = rth->dump = ++rth->seq;
149 return sendmsg(rth->fd, &msg, 0);
152 int rtnl_dump_filter(struct rtnl_handle *rth,
153 rtnl_filter_t filter,
158 struct sockaddr_nl nladdr;
160 struct msghdr msg = {
162 .msg_namelen = sizeof(nladdr),
173 iov.iov_len = sizeof(buf);
174 status = TEMP_FAILURE_RETRY ( recvmsg(rth->fd, &msg, MSG_CMSG_CLOEXEC) );
182 fprintf(stderr, "EOF on netlink\n");
186 h = (struct nlmsghdr*)buf;
187 while (NLMSG_OK(h, (unsigned)status)) {
190 if (nladdr.nl_pid != 0 ||
191 h->nlmsg_pid != rth->local.nl_pid ||
192 h->nlmsg_seq != rth->dump) {
194 err = junk(&nladdr, h, arg2);
201 if (h->nlmsg_type == NLMSG_DONE)
203 if (h->nlmsg_type == NLMSG_ERROR) {
204 struct nlmsgerr *msgerr = (struct nlmsgerr*)NLMSG_DATA(h);
205 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
206 fprintf(stderr, "ERROR truncated\n");
208 errno = -msgerr->error;
209 perror("RTNETLINK answers");
213 err = filter(&nladdr, h, arg1);
218 h = NLMSG_NEXT(h, status);
220 if (msg.msg_flags & MSG_TRUNC) {
221 fprintf(stderr, "Message truncated\n");
225 fprintf(stderr, "!!!Remnant of size %d\n", status);
231 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
232 unsigned groups, struct nlmsghdr *answer,
239 struct sockaddr_nl nladdr;
241 .iov_base = (void*) n,
242 .iov_len = n->nlmsg_len
244 struct msghdr msg = {
246 .msg_namelen = sizeof(nladdr),
252 memset(&nladdr, 0, sizeof(nladdr));
253 nladdr.nl_family = AF_NETLINK;
254 nladdr.nl_pid = peer;
255 nladdr.nl_groups = groups;
257 n->nlmsg_seq = seq = ++rtnl->seq;
260 n->nlmsg_flags |= NLM_F_ACK;
262 status = sendmsg(rtnl->fd, &msg, 0);
265 perror("Cannot talk to rtnetlink");
269 memset(buf,0,sizeof(buf));
274 iov.iov_len = sizeof(buf);
275 status = TEMP_FAILURE_RETRY ( recvmsg(rtnl->fd, &msg, MSG_CMSG_CLOEXEC) );
282 fprintf(stderr, "EOF on netlink\n");
285 if (msg.msg_namelen != sizeof(nladdr)) {
286 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
289 for (h = (struct nlmsghdr*)buf; (unsigned)status >= sizeof(*h); ) {
291 int len = h->nlmsg_len;
292 int l = len - sizeof(*h);
294 if (l<0 || len>status) {
295 if (msg.msg_flags & MSG_TRUNC) {
296 fprintf(stderr, "Truncated message\n");
299 fprintf(stderr, "!!!malformed message: len=%d\n", len);
303 if (nladdr.nl_pid != (unsigned)peer ||
304 h->nlmsg_pid != rtnl->local.nl_pid ||
305 h->nlmsg_seq != seq) {
307 err = junk(&nladdr, h, jarg);
311 /* Don't forget to skip that message. */
312 status -= NLMSG_ALIGN(len);
313 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
317 if (h->nlmsg_type == NLMSG_ERROR) {
318 struct nlmsgerr *msgerr = (struct nlmsgerr*)NLMSG_DATA(h);
319 if ((unsigned)l < sizeof(struct nlmsgerr)) {
320 fprintf(stderr, "ERROR truncated\n");
322 errno = -msgerr->error;
325 memcpy(answer, h, h->nlmsg_len);
328 perror("RTNETLINK1 answers");
333 memcpy(answer, h, h->nlmsg_len);
337 fprintf(stderr, "Unexpected reply!!!\n");
339 status -= NLMSG_ALIGN(len);
340 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
342 if (msg.msg_flags & MSG_TRUNC) {
343 fprintf(stderr, "Message truncated\n");
347 fprintf(stderr, "!!!Remnant of size %d\n", status);
353 int rtnl_listen(struct rtnl_handle *rtnl,
354 rtnl_filter_t handler,
359 struct sockaddr_nl nladdr;
361 struct msghdr msg = {
363 .msg_namelen = sizeof(nladdr),
369 memset(&nladdr, 0, sizeof(nladdr));
370 nladdr.nl_family = AF_NETLINK;
372 nladdr.nl_groups = 0;
376 iov.iov_len = sizeof(buf);
377 status = TEMP_FAILURE_RETRY ( recvmsg(rtnl->fd, &msg, MSG_CMSG_CLOEXEC) );
384 fprintf(stderr, "EOF on netlink\n");
387 if (msg.msg_namelen != sizeof(nladdr)) {
388 fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
391 for (h = (struct nlmsghdr*)buf; (unsigned)status >= sizeof(*h); ) {
393 int len = h->nlmsg_len;
394 int l = len - sizeof(*h);
396 if (l<0 || len>status) {
397 if (msg.msg_flags & MSG_TRUNC) {
398 fprintf(stderr, "Truncated message\n");
401 fprintf(stderr, "!!!malformed message: len=%d\n", len);
405 err = handler(&nladdr, h, jarg);
409 status -= NLMSG_ALIGN(len);
410 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
412 if (msg.msg_flags & MSG_TRUNC) {
413 fprintf(stderr, "Message truncated\n");
417 fprintf(stderr, "!!!Remnant of size %d\n", status);
423 int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
427 struct sockaddr_nl nladdr;
429 struct nlmsghdr *h = (void*)buf;
431 memset(&nladdr, 0, sizeof(nladdr));
432 nladdr.nl_family = AF_NETLINK;
434 nladdr.nl_groups = 0;
440 status = TEMP_FAILURE_RETRY ( fread(&buf, 1, sizeof(*h), rtnl) );
443 perror("rtnl_from_file: fread");
450 l = len - sizeof(*h);
452 if (l<0 || (unsigned)len>sizeof(buf)) {
453 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
458 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
461 perror("rtnl_from_file: fread");
465 fprintf(stderr, "rtnl-from_file: truncated message\n");
469 err = handler(&nladdr, h, jarg);
475 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
477 int len = RTA_LENGTH(4);
479 if ((int)NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) {
480 fprintf(stderr,"addattr32: Error! max allowed bound %d exceeded\n",maxlen);
484 rta->rta_type = type;
486 memcpy(RTA_DATA(rta), &data, 4);
487 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
491 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
494 int len = RTA_LENGTH(alen);
497 if ((int)NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
498 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
502 rta->rta_type = type;
504 memcpy(RTA_DATA(rta), data, alen);
505 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
509 int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
511 if ((int)NLMSG_ALIGN(n->nlmsg_len) + (int)NLMSG_ALIGN(len) > maxlen) {
512 fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen);
516 memcpy(NLMSG_TAIL(n), data, len);
517 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
518 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
522 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
524 int len = RTA_LENGTH(4);
525 struct rtattr *subrta;
527 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
528 fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen);
531 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
532 subrta->rta_type = type;
533 subrta->rta_len = len;
534 memcpy(RTA_DATA(subrta), &data, 4);
535 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
539 int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
540 const void *data, int alen)
542 struct rtattr *subrta;
543 int len = RTA_LENGTH(alen);
545 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
546 fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen);
549 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
550 subrta->rta_type = type;
551 subrta->rta_len = len;
552 memcpy(RTA_DATA(subrta), data, alen);
553 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
557 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
559 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
560 while (RTA_OK(rta, len)) {
561 if (rta->rta_type <= max)
562 tb[rta->rta_type] = rta;
563 rta = RTA_NEXT(rta,len);
566 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
570 int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len)
574 memset(tb, 0, sizeof(struct rtattr *) * max);
575 while (RTA_OK(rta, len)) {
576 if (rta->rta_type <= max && i < max)
578 rta = RTA_NEXT(rta,len);
581 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);