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 FAST_FUNC rtnl_close(struct rtnl_handle *rth)
25 int FAST_FUNC xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/)
29 memset(rth, 0, sizeof(*rth));
30 rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
31 rth->local.nl_family = AF_NETLINK;
32 /*rth->local.nl_groups = subscriptions;*/
34 xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
35 addr_len = sizeof(rth->local);
36 getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len);
39 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0)
40 bb_perror_msg_and_die("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);
46 rth->seq = time(NULL);
50 int FAST_FUNC xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
56 struct sockaddr_nl nladdr;
58 memset(&nladdr, 0, sizeof(nladdr));
59 nladdr.nl_family = AF_NETLINK;
61 req.nlh.nlmsg_len = sizeof(req);
62 req.nlh.nlmsg_type = type;
63 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
64 req.nlh.nlmsg_pid = 0;
65 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
66 req.g.rtgen_family = family;
68 return xsendto(rth->fd, (void*)&req, sizeof(req),
69 (struct sockaddr*)&nladdr, sizeof(nladdr));
72 int FAST_FUNC rtnl_send(struct rtnl_handle *rth, char *buf, int len)
74 struct sockaddr_nl nladdr;
76 memset(&nladdr, 0, sizeof(nladdr));
77 nladdr.nl_family = AF_NETLINK;
79 return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
82 int FAST_FUNC rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
85 struct sockaddr_nl nladdr;
86 struct iovec iov[2] = { { &nlh, sizeof(nlh) }, { req, len } };
88 (void*)&nladdr, sizeof(nladdr),
94 memset(&nladdr, 0, sizeof(nladdr));
95 nladdr.nl_family = AF_NETLINK;
97 nlh.nlmsg_len = NLMSG_LENGTH(len);
98 nlh.nlmsg_type = type;
99 nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
101 nlh.nlmsg_seq = rth->dump = ++rth->seq;
103 return sendmsg(rth->fd, &msg, 0);
106 static int rtnl_dump_filter(struct rtnl_handle *rth,
107 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *),
109 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
113 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
114 struct sockaddr_nl nladdr;
115 struct iovec iov = { buf, 8*1024 };
121 struct msghdr msg = {
122 (void*)&nladdr, sizeof(nladdr),
128 status = recvmsg(rth->fd, &msg, 0);
133 bb_perror_msg("OVERRUN");
137 bb_error_msg("EOF on netlink");
140 if (msg.msg_namelen != sizeof(nladdr)) {
141 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
144 h = (struct nlmsghdr*)buf;
145 while (NLMSG_OK(h, status)) {
148 if (nladdr.nl_pid != 0 ||
149 h->nlmsg_pid != rth->local.nl_pid ||
150 h->nlmsg_seq != rth->dump) {
152 // err = junk(&nladdr, h, arg2);
161 if (h->nlmsg_type == NLMSG_DONE) {
164 if (h->nlmsg_type == NLMSG_ERROR) {
165 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
166 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
167 bb_error_msg("ERROR truncated");
169 errno = -l_err->error;
170 bb_perror_msg("RTNETLINK answers");
174 err = filter(&nladdr, h, arg1);
181 h = NLMSG_NEXT(h, status);
183 if (msg.msg_flags & MSG_TRUNC) {
184 bb_error_msg("message truncated");
188 bb_error_msg_and_die("remnant of size %d!", status);
198 int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth,
199 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *),
202 int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/);
204 bb_error_msg_and_die("dump terminated");
208 int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
209 pid_t peer, unsigned groups,
210 struct nlmsghdr *answer,
211 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *),
214 /* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */
223 struct sockaddr_nl nladdr;
224 struct iovec iov = { (void*)n, n->nlmsg_len };
225 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
226 struct msghdr msg = {
227 (void*)&nladdr, sizeof(nladdr),
233 memset(&nladdr, 0, sizeof(nladdr));
234 nladdr.nl_family = AF_NETLINK;
235 // nladdr.nl_pid = peer;
236 // nladdr.nl_groups = groups;
238 n->nlmsg_seq = seq = ++rtnl->seq;
239 if (answer == NULL) {
240 n->nlmsg_flags |= NLM_F_ACK;
242 status = sendmsg(rtnl->fd, &msg, 0);
245 bb_perror_msg("cannot talk to rtnetlink");
252 iov.iov_len = 8*1024;
253 status = recvmsg(rtnl->fd, &msg, 0);
256 if (errno == EINTR) {
259 bb_perror_msg("OVERRUN");
263 bb_error_msg("EOF on netlink");
266 if (msg.msg_namelen != sizeof(nladdr)) {
267 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
269 for (h = (struct nlmsghdr*)buf; status >= (int)sizeof(*h); ) {
271 int len = h->nlmsg_len;
272 int l = len - sizeof(*h);
274 if (l < 0 || len > status) {
275 if (msg.msg_flags & MSG_TRUNC) {
276 bb_error_msg("truncated message");
279 bb_error_msg_and_die("malformed message: len=%d!", len);
282 if (nladdr.nl_pid != peer ||
283 h->nlmsg_pid != rtnl->local.nl_pid ||
284 h->nlmsg_seq != seq) {
286 // l_err = junk(&nladdr, h, jarg);
295 if (h->nlmsg_type == NLMSG_ERROR) {
296 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
297 if (l < (int)sizeof(struct nlmsgerr)) {
298 bb_error_msg("ERROR truncated");
300 errno = - err->error;
303 memcpy(answer, h, h->nlmsg_len);
307 bb_perror_msg("RTNETLINK answers");
312 memcpy(answer, h, h->nlmsg_len);
316 bb_error_msg("unexpected reply!");
318 status -= NLMSG_ALIGN(len);
319 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
321 if (msg.msg_flags & MSG_TRUNC) {
322 bb_error_msg("message truncated");
326 bb_error_msg_and_die("remnant of size %d!", status);
336 int FAST_FUNC addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
338 int len = RTA_LENGTH(4);
340 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen)
342 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
343 rta->rta_type = type;
345 move_to_unaligned32(RTA_DATA(rta), data);
346 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
350 int FAST_FUNC addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
352 int len = RTA_LENGTH(alen);
355 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen)
357 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
358 rta->rta_type = type;
360 memcpy(RTA_DATA(rta), data, alen);
361 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
365 int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
367 int len = RTA_LENGTH(4);
368 struct rtattr *subrta;
370 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
373 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
374 subrta->rta_type = type;
375 subrta->rta_len = len;
376 move_to_unaligned32(RTA_DATA(subrta), data);
377 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
381 int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
383 struct rtattr *subrta;
384 int len = RTA_LENGTH(alen);
386 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
389 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
390 subrta->rta_type = type;
391 subrta->rta_len = len;
392 memcpy(RTA_DATA(subrta), data, alen);
393 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
398 int FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
400 while (RTA_OK(rta, len)) {
401 if (rta->rta_type <= max) {
402 tb[rta->rta_type] = rta;
404 rta = RTA_NEXT(rta,len);
407 bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);