1 /* vi: set sw=4 ts=4: */
3 * libnetlink.c RTnetlink service routines.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
14 #include <sys/socket.h>
18 #include "libnetlink.h"
20 void rtnl_close(struct rtnl_handle *rth)
25 int xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/)
29 memset(rth, 0, sizeof(rth));
31 rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
33 memset(&rth->local, 0, sizeof(rth->local));
34 rth->local.nl_family = AF_NETLINK;
35 /*rth->local.nl_groups = subscriptions;*/
37 xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
38 addr_len = sizeof(rth->local);
39 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0)
40 bb_perror_msg_and_die("cannot getsockname");
41 if (addr_len != sizeof(rth->local))
42 bb_error_msg_and_die("wrong address length %d", addr_len);
43 if (rth->local.nl_family != AF_NETLINK)
44 bb_error_msg_and_die("wrong address family %d", rth->local.nl_family);
45 rth->seq = time(NULL);
49 int xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
55 struct sockaddr_nl nladdr;
57 memset(&nladdr, 0, sizeof(nladdr));
58 nladdr.nl_family = AF_NETLINK;
60 req.nlh.nlmsg_len = sizeof(req);
61 req.nlh.nlmsg_type = type;
62 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
63 req.nlh.nlmsg_pid = 0;
64 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
65 req.g.rtgen_family = family;
67 return xsendto(rth->fd, (void*)&req, sizeof(req),
68 (struct sockaddr*)&nladdr, sizeof(nladdr));
71 int rtnl_send(struct rtnl_handle *rth, char *buf, int len)
73 struct sockaddr_nl nladdr;
75 memset(&nladdr, 0, sizeof(nladdr));
76 nladdr.nl_family = AF_NETLINK;
78 return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
81 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
84 struct sockaddr_nl nladdr;
85 struct iovec iov[2] = { { &nlh, sizeof(nlh) }, { req, len } };
87 (void*)&nladdr, sizeof(nladdr),
93 memset(&nladdr, 0, sizeof(nladdr));
94 nladdr.nl_family = AF_NETLINK;
96 nlh.nlmsg_len = NLMSG_LENGTH(len);
97 nlh.nlmsg_type = type;
98 nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
100 nlh.nlmsg_seq = rth->dump = ++rth->seq;
102 return sendmsg(rth->fd, &msg, 0);
105 static int rtnl_dump_filter(struct rtnl_handle *rth,
106 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *),
108 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
112 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
113 struct sockaddr_nl nladdr;
114 struct iovec iov = { buf, 8*1024 };
120 struct msghdr msg = {
121 (void*)&nladdr, sizeof(nladdr),
127 status = recvmsg(rth->fd, &msg, 0);
132 bb_perror_msg("OVERRUN");
136 bb_error_msg("EOF on netlink");
139 if (msg.msg_namelen != sizeof(nladdr)) {
140 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
143 h = (struct nlmsghdr*)buf;
144 while (NLMSG_OK(h, status)) {
147 if (nladdr.nl_pid != 0 ||
148 h->nlmsg_pid != rth->local.nl_pid ||
149 h->nlmsg_seq != rth->dump) {
151 // err = junk(&nladdr, h, arg2);
160 if (h->nlmsg_type == NLMSG_DONE) {
163 if (h->nlmsg_type == NLMSG_ERROR) {
164 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
165 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
166 bb_error_msg("ERROR truncated");
168 errno = -l_err->error;
169 bb_perror_msg("RTNETLINK answers");
173 err = filter(&nladdr, h, arg1);
180 h = NLMSG_NEXT(h, status);
182 if (msg.msg_flags & MSG_TRUNC) {
183 bb_error_msg("message truncated");
187 bb_error_msg_and_die("remnant of size %d!", status);
197 int xrtnl_dump_filter(struct rtnl_handle *rth,
198 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *),
201 int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/);
203 bb_error_msg_and_die("dump terminated");
207 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
208 pid_t peer, unsigned groups,
209 struct nlmsghdr *answer,
210 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *),
213 /* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */
222 struct sockaddr_nl nladdr;
223 struct iovec iov = { (void*)n, n->nlmsg_len };
224 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
225 struct msghdr msg = {
226 (void*)&nladdr, sizeof(nladdr),
232 memset(&nladdr, 0, sizeof(nladdr));
233 nladdr.nl_family = AF_NETLINK;
234 // nladdr.nl_pid = peer;
235 // nladdr.nl_groups = groups;
237 n->nlmsg_seq = seq = ++rtnl->seq;
238 if (answer == NULL) {
239 n->nlmsg_flags |= NLM_F_ACK;
241 status = sendmsg(rtnl->fd, &msg, 0);
244 bb_perror_msg("cannot talk to rtnetlink");
251 iov.iov_len = 8*1024;
252 status = recvmsg(rtnl->fd, &msg, 0);
255 if (errno == EINTR) {
258 bb_perror_msg("OVERRUN");
262 bb_error_msg("EOF on netlink");
265 if (msg.msg_namelen != sizeof(nladdr)) {
266 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
268 for (h = (struct nlmsghdr*)buf; status >= (int)sizeof(*h); ) {
270 int len = h->nlmsg_len;
271 int l = len - sizeof(*h);
273 if (l < 0 || len > status) {
274 if (msg.msg_flags & MSG_TRUNC) {
275 bb_error_msg("truncated message");
278 bb_error_msg_and_die("malformed message: len=%d!", len);
281 if (nladdr.nl_pid != peer ||
282 h->nlmsg_pid != rtnl->local.nl_pid ||
283 h->nlmsg_seq != seq) {
285 // l_err = junk(&nladdr, h, jarg);
294 if (h->nlmsg_type == NLMSG_ERROR) {
295 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
296 if (l < (int)sizeof(struct nlmsgerr)) {
297 bb_error_msg("ERROR truncated");
299 errno = - err->error;
302 memcpy(answer, h, h->nlmsg_len);
306 bb_perror_msg("RTNETLINK answers");
311 memcpy(answer, h, h->nlmsg_len);
315 bb_error_msg("unexpected reply!");
317 status -= NLMSG_ALIGN(len);
318 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
320 if (msg.msg_flags & MSG_TRUNC) {
321 bb_error_msg("message truncated");
325 bb_error_msg_and_die("remnant of size %d!", status);
335 int addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
337 int len = RTA_LENGTH(4);
339 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen)
341 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
342 rta->rta_type = type;
344 memcpy(RTA_DATA(rta), &data, 4);
345 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
349 int addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
351 int len = RTA_LENGTH(alen);
354 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen)
356 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
357 rta->rta_type = type;
359 memcpy(RTA_DATA(rta), data, alen);
360 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
364 int rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
366 int len = RTA_LENGTH(4);
367 struct rtattr *subrta;
369 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
372 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
373 subrta->rta_type = type;
374 subrta->rta_len = len;
375 memcpy(RTA_DATA(subrta), &data, 4);
376 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
380 int rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
382 struct rtattr *subrta;
383 int len = RTA_LENGTH(alen);
385 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
388 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
389 subrta->rta_type = type;
390 subrta->rta_len = len;
391 memcpy(RTA_DATA(subrta), data, alen);
392 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
397 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
399 while (RTA_OK(rta, len)) {
400 if (rta->rta_type <= max) {
401 tb[rta->rta_type] = rta;
403 rta = RTA_NEXT(rta,len);
406 bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);