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"
29 void rtnl_close(struct rtnl_handle *rth)
34 int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
38 memset(rth, 0, sizeof(rth));
40 rth->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
42 perror_msg("Cannot open netlink socket");
46 memset(&rth->local, 0, sizeof(rth->local));
47 rth->local.nl_family = AF_NETLINK;
48 rth->local.nl_groups = subscriptions;
50 if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
51 perror_msg("Cannot bind netlink socket");
54 addr_len = sizeof(rth->local);
55 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
56 perror_msg("Cannot getsockname");
59 if (addr_len != sizeof(rth->local)) {
60 error_msg("Wrong address length %d", addr_len);
63 if (rth->local.nl_family != AF_NETLINK) {
64 error_msg("Wrong address family %d", rth->local.nl_family);
67 rth->seq = time(NULL);
71 int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
77 struct sockaddr_nl nladdr;
79 memset(&nladdr, 0, sizeof(nladdr));
80 nladdr.nl_family = AF_NETLINK;
82 req.nlh.nlmsg_len = sizeof(req);
83 req.nlh.nlmsg_type = type;
84 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
85 req.nlh.nlmsg_pid = 0;
86 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
87 req.g.rtgen_family = family;
89 return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
92 int rtnl_send(struct rtnl_handle *rth, char *buf, int len)
94 struct sockaddr_nl nladdr;
96 memset(&nladdr, 0, sizeof(nladdr));
97 nladdr.nl_family = AF_NETLINK;
99 return sendto(rth->fd, buf, len, 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
102 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
105 struct sockaddr_nl nladdr;
106 struct iovec iov[2] = { { &nlh, sizeof(nlh) }, { req, len } };
107 struct msghdr msg = {
108 (void*)&nladdr, sizeof(nladdr),
114 memset(&nladdr, 0, sizeof(nladdr));
115 nladdr.nl_family = AF_NETLINK;
117 nlh.nlmsg_len = NLMSG_LENGTH(len);
118 nlh.nlmsg_type = type;
119 nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
121 nlh.nlmsg_seq = rth->dump = ++rth->seq;
123 return sendmsg(rth->fd, &msg, 0);
126 int rtnl_dump_filter(struct rtnl_handle *rth,
127 int (*filter)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
129 int (*junk)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
133 struct sockaddr_nl nladdr;
134 struct iovec iov = { buf, sizeof(buf) };
140 struct msghdr msg = {
141 (void*)&nladdr, sizeof(nladdr),
147 status = recvmsg(rth->fd, &msg, 0);
152 perror_msg("OVERRUN");
156 error_msg("EOF on netlink");
159 if (msg.msg_namelen != sizeof(nladdr)) {
160 error_msg_and_die("sender address length == %d", msg.msg_namelen);
163 h = (struct nlmsghdr*)buf;
164 while (NLMSG_OK(h, status)) {
167 if (h->nlmsg_pid != rth->local.nl_pid ||
168 h->nlmsg_seq != rth->dump) {
170 err = junk(&nladdr, h, arg2);
178 if (h->nlmsg_type == NLMSG_DONE) {
181 if (h->nlmsg_type == NLMSG_ERROR) {
182 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
183 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
184 error_msg("ERROR truncated");
186 errno = -l_err->error;
187 perror_msg("RTNETLINK answers");
191 err = filter(&nladdr, h, arg1);
197 h = NLMSG_NEXT(h, status);
199 if (msg.msg_flags & MSG_TRUNC) {
200 error_msg("Message truncated");
204 error_msg_and_die("!!!Remnant of size %d", status);
209 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
210 unsigned groups, struct nlmsghdr *answer,
211 int (*junk)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
217 struct sockaddr_nl nladdr;
218 struct iovec iov = { (void*)n, n->nlmsg_len };
220 struct msghdr msg = {
221 (void*)&nladdr, sizeof(nladdr),
227 memset(&nladdr, 0, sizeof(nladdr));
228 nladdr.nl_family = AF_NETLINK;
229 nladdr.nl_pid = peer;
230 nladdr.nl_groups = groups;
232 n->nlmsg_seq = seq = ++rtnl->seq;
233 if (answer == NULL) {
234 n->nlmsg_flags |= NLM_F_ACK;
236 status = sendmsg(rtnl->fd, &msg, 0);
239 perror_msg("Cannot talk to rtnetlink");
246 iov.iov_len = sizeof(buf);
247 status = recvmsg(rtnl->fd, &msg, 0);
250 if (errno == EINTR) {
253 perror_msg("OVERRUN");
257 error_msg("EOF on netlink");
260 if (msg.msg_namelen != sizeof(nladdr)) {
261 error_msg_and_die("sender address length == %d", msg.msg_namelen);
263 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
265 int len = h->nlmsg_len;
266 int l = len - sizeof(*h);
268 if (l<0 || len>status) {
269 if (msg.msg_flags & MSG_TRUNC) {
270 error_msg("Truncated message");
273 error_msg_and_die("!!!malformed message: len=%d", len);
276 if (h->nlmsg_pid != rtnl->local.nl_pid ||
277 h->nlmsg_seq != seq) {
279 l_err = junk(&nladdr, h, jarg);
287 if (h->nlmsg_type == NLMSG_ERROR) {
288 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
289 if (l < sizeof(struct nlmsgerr)) {
290 error_msg("ERROR truncated");
295 memcpy(answer, h, h->nlmsg_len);
299 perror_msg("RTNETLINK answers");
304 memcpy(answer, h, h->nlmsg_len);
308 error_msg("Unexpected reply!!!");
310 status -= NLMSG_ALIGN(len);
311 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
313 if (msg.msg_flags & MSG_TRUNC) {
314 error_msg("Message truncated");
318 error_msg_and_die("!!!Remnant of size %d", status);
323 int rtnl_listen(struct rtnl_handle *rtnl,
324 int (*handler)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
329 struct sockaddr_nl nladdr;
332 struct msghdr msg = {
333 (void*)&nladdr, sizeof(nladdr),
339 memset(&nladdr, 0, sizeof(nladdr));
340 nladdr.nl_family = AF_NETLINK;
342 nladdr.nl_groups = 0;
348 iov.iov_len = sizeof(buf);
349 status = recvmsg(rtnl->fd, &msg, 0);
354 perror_msg("OVERRUN");
358 error_msg("EOF on netlink");
361 if (msg.msg_namelen != sizeof(nladdr)) {
362 error_msg_and_die("Sender address length == %d", msg.msg_namelen);
364 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
366 int len = h->nlmsg_len;
367 int l = len - sizeof(*h);
369 if (l<0 || len>status) {
370 if (msg.msg_flags & MSG_TRUNC) {
371 error_msg("Truncated message");
374 error_msg_and_die("!!!malformed message: len=%d", len);
377 err = handler(&nladdr, h, jarg);
382 status -= NLMSG_ALIGN(len);
383 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
385 if (msg.msg_flags & MSG_TRUNC) {
386 error_msg("Message truncated");
390 error_msg_and_die("!!!Remnant of size %d", status);
395 int rtnl_from_file(FILE *rtnl,
396 int (*handler)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
400 struct sockaddr_nl nladdr;
402 struct nlmsghdr *h = (void*)buf;
404 memset(&nladdr, 0, sizeof(nladdr));
405 nladdr.nl_family = AF_NETLINK;
407 nladdr.nl_groups = 0;
413 status = fread(&buf, 1, sizeof(*h), rtnl);
418 perror_msg("rtnl_from_file: fread");
426 l = len - sizeof(*h);
428 if (l<0 || len>sizeof(buf)) {
429 error_msg("!!!malformed message: len=%d @%lu",
434 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
437 perror_msg("rtnl_from_file: fread");
441 error_msg("rtnl-from_file: truncated message");
445 err = handler(&nladdr, h, jarg);
451 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
453 int len = RTA_LENGTH(4);
455 if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
457 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
458 rta->rta_type = type;
460 memcpy(RTA_DATA(rta), &data, 4);
461 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
465 int addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
467 int len = RTA_LENGTH(alen);
470 if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
472 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
473 rta->rta_type = type;
475 memcpy(RTA_DATA(rta), data, alen);
476 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
480 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
482 int len = RTA_LENGTH(4);
483 struct rtattr *subrta;
485 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
488 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
489 subrta->rta_type = type;
490 subrta->rta_len = len;
491 memcpy(RTA_DATA(subrta), &data, 4);
492 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
496 int rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
498 struct rtattr *subrta;
499 int len = RTA_LENGTH(alen);
501 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
504 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
505 subrta->rta_type = type;
506 subrta->rta_len = len;
507 memcpy(RTA_DATA(subrta), data, alen);
508 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
513 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
515 while (RTA_OK(rta, len)) {
516 if (rta->rta_type <= max) {
517 tb[rta->rta_type] = rta;
519 rta = RTA_NEXT(rta,len);
522 error_msg("!!!Deficit %d, rta_len=%d", len, rta->rta_len);