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>
13 #include <sys/socket.h>
23 #include "libnetlink.h"
26 void rtnl_close(struct rtnl_handle *rth)
31 int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
35 memset(rth, 0, sizeof(rth));
37 rth->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
39 bb_perror_msg("Cannot open netlink socket");
43 memset(&rth->local, 0, sizeof(rth->local));
44 rth->local.nl_family = AF_NETLINK;
45 rth->local.nl_groups = subscriptions;
47 if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
48 bb_perror_msg("Cannot bind netlink socket");
51 addr_len = sizeof(rth->local);
52 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
53 bb_perror_msg("Cannot getsockname");
56 if (addr_len != sizeof(rth->local)) {
57 bb_error_msg("Wrong address length %d", addr_len);
60 if (rth->local.nl_family != AF_NETLINK) {
61 bb_error_msg("Wrong address family %d", rth->local.nl_family);
64 rth->seq = time(NULL);
68 int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
74 struct sockaddr_nl nladdr;
76 memset(&nladdr, 0, sizeof(nladdr));
77 nladdr.nl_family = AF_NETLINK;
79 req.nlh.nlmsg_len = sizeof(req);
80 req.nlh.nlmsg_type = type;
81 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
82 req.nlh.nlmsg_pid = 0;
83 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
84 req.g.rtgen_family = family;
86 return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
89 int rtnl_send(struct rtnl_handle *rth, char *buf, int len)
91 struct sockaddr_nl nladdr;
93 memset(&nladdr, 0, sizeof(nladdr));
94 nladdr.nl_family = AF_NETLINK;
96 return sendto(rth->fd, buf, len, 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
99 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
102 struct sockaddr_nl nladdr;
103 struct iovec iov[2] = { { &nlh, sizeof(nlh) }, { req, len } };
104 struct msghdr msg = {
105 (void*)&nladdr, sizeof(nladdr),
111 memset(&nladdr, 0, sizeof(nladdr));
112 nladdr.nl_family = AF_NETLINK;
114 nlh.nlmsg_len = NLMSG_LENGTH(len);
115 nlh.nlmsg_type = type;
116 nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
118 nlh.nlmsg_seq = rth->dump = ++rth->seq;
120 return sendmsg(rth->fd, &msg, 0);
123 int rtnl_dump_filter(struct rtnl_handle *rth,
124 int (*filter)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
126 int (*junk)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
130 struct sockaddr_nl nladdr;
131 struct iovec iov = { buf, sizeof(buf) };
137 struct msghdr msg = {
138 (void*)&nladdr, sizeof(nladdr),
144 status = recvmsg(rth->fd, &msg, 0);
149 bb_perror_msg("OVERRUN");
153 bb_error_msg("EOF on netlink");
156 if (msg.msg_namelen != sizeof(nladdr)) {
157 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
160 h = (struct nlmsghdr*)buf;
161 while (NLMSG_OK(h, status)) {
164 if (h->nlmsg_pid != rth->local.nl_pid ||
165 h->nlmsg_seq != rth->dump) {
167 err = junk(&nladdr, h, arg2);
175 if (h->nlmsg_type == NLMSG_DONE) {
178 if (h->nlmsg_type == NLMSG_ERROR) {
179 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
180 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
181 bb_error_msg("ERROR truncated");
183 errno = -l_err->error;
184 bb_perror_msg("RTNETLINK answers");
188 err = filter(&nladdr, h, arg1);
194 h = NLMSG_NEXT(h, status);
196 if (msg.msg_flags & MSG_TRUNC) {
197 bb_error_msg("Message truncated");
201 bb_error_msg_and_die("!!!Remnant of size %d", status);
206 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
207 unsigned groups, struct nlmsghdr *answer,
208 int (*junk)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
214 struct sockaddr_nl nladdr;
215 struct iovec iov = { (void*)n, n->nlmsg_len };
217 struct msghdr msg = {
218 (void*)&nladdr, sizeof(nladdr),
224 memset(&nladdr, 0, sizeof(nladdr));
225 nladdr.nl_family = AF_NETLINK;
226 nladdr.nl_pid = peer;
227 nladdr.nl_groups = groups;
229 n->nlmsg_seq = seq = ++rtnl->seq;
230 if (answer == NULL) {
231 n->nlmsg_flags |= NLM_F_ACK;
233 status = sendmsg(rtnl->fd, &msg, 0);
236 bb_perror_msg("Cannot talk to rtnetlink");
243 iov.iov_len = sizeof(buf);
244 status = recvmsg(rtnl->fd, &msg, 0);
247 if (errno == EINTR) {
250 bb_perror_msg("OVERRUN");
254 bb_error_msg("EOF on netlink");
257 if (msg.msg_namelen != sizeof(nladdr)) {
258 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
260 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
262 int len = h->nlmsg_len;
263 int l = len - sizeof(*h);
265 if (l<0 || len>status) {
266 if (msg.msg_flags & MSG_TRUNC) {
267 bb_error_msg("Truncated message");
270 bb_error_msg_and_die("!!!malformed message: len=%d", len);
273 if (h->nlmsg_pid != rtnl->local.nl_pid ||
274 h->nlmsg_seq != seq) {
276 l_err = junk(&nladdr, h, jarg);
284 if (h->nlmsg_type == NLMSG_ERROR) {
285 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
286 if (l < sizeof(struct nlmsgerr)) {
287 bb_error_msg("ERROR truncated");
292 memcpy(answer, h, h->nlmsg_len);
296 bb_perror_msg("RTNETLINK answers");
301 memcpy(answer, h, h->nlmsg_len);
305 bb_error_msg("Unexpected reply!!!");
307 status -= NLMSG_ALIGN(len);
308 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
310 if (msg.msg_flags & MSG_TRUNC) {
311 bb_error_msg("Message truncated");
315 bb_error_msg_and_die("!!!Remnant of size %d", status);
320 int rtnl_listen(struct rtnl_handle *rtnl,
321 int (*handler)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
326 struct sockaddr_nl nladdr;
329 struct msghdr msg = {
330 (void*)&nladdr, sizeof(nladdr),
336 memset(&nladdr, 0, sizeof(nladdr));
337 nladdr.nl_family = AF_NETLINK;
339 nladdr.nl_groups = 0;
345 iov.iov_len = sizeof(buf);
346 status = recvmsg(rtnl->fd, &msg, 0);
351 bb_perror_msg("OVERRUN");
355 bb_error_msg("EOF on netlink");
358 if (msg.msg_namelen != sizeof(nladdr)) {
359 bb_error_msg_and_die("Sender address length == %d", msg.msg_namelen);
361 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
363 int len = h->nlmsg_len;
364 int l = len - sizeof(*h);
366 if (l<0 || len>status) {
367 if (msg.msg_flags & MSG_TRUNC) {
368 bb_error_msg("Truncated message");
371 bb_error_msg_and_die("!!!malformed message: len=%d", len);
374 err = handler(&nladdr, h, jarg);
379 status -= NLMSG_ALIGN(len);
380 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
382 if (msg.msg_flags & MSG_TRUNC) {
383 bb_error_msg("Message truncated");
387 bb_error_msg_and_die("!!!Remnant of size %d", status);
392 int rtnl_from_file(FILE *rtnl,
393 int (*handler)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
397 struct sockaddr_nl nladdr;
399 struct nlmsghdr *h = (void*)buf;
401 memset(&nladdr, 0, sizeof(nladdr));
402 nladdr.nl_family = AF_NETLINK;
404 nladdr.nl_groups = 0;
410 status = fread(&buf, 1, sizeof(*h), rtnl);
415 bb_perror_msg("rtnl_from_file: fread");
423 l = len - sizeof(*h);
425 if (l<0 || len>sizeof(buf)) {
426 bb_error_msg("!!!malformed message: len=%d @%lu",
431 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
434 bb_perror_msg("rtnl_from_file: fread");
438 bb_error_msg("rtnl-from_file: truncated message");
442 err = handler(&nladdr, h, jarg);
448 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
450 int len = RTA_LENGTH(4);
452 if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
454 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
455 rta->rta_type = type;
457 memcpy(RTA_DATA(rta), &data, 4);
458 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
462 int addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
464 int len = RTA_LENGTH(alen);
467 if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
469 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
470 rta->rta_type = type;
472 memcpy(RTA_DATA(rta), data, alen);
473 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
477 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
479 int len = RTA_LENGTH(4);
480 struct rtattr *subrta;
482 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
485 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
486 subrta->rta_type = type;
487 subrta->rta_len = len;
488 memcpy(RTA_DATA(subrta), &data, 4);
489 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
493 int rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
495 struct rtattr *subrta;
496 int len = RTA_LENGTH(alen);
498 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
501 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
502 subrta->rta_type = type;
503 subrta->rta_len = len;
504 memcpy(RTA_DATA(subrta), data, alen);
505 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
510 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
512 while (RTA_OK(rta, len)) {
513 if (rta->rta_type <= max) {
514 tb[rta->rta_type] = rta;
516 rta = RTA_NEXT(rta,len);
519 bb_error_msg("!!!Deficit %d, rta_len=%d", len, rta->rta_len);