1 /* vi: set sw=4 ts=4: */
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version
6 * 2 of the License, or (at your option) any later version.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 #include <sys/socket.h>
15 #include "libnetlink.h"
17 void FAST_FUNC xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/)
21 memset(rth, 0, sizeof(*rth));
22 rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
23 rth->local.nl_family = AF_NETLINK;
24 /*rth->local.nl_groups = subscriptions;*/
26 xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
27 addr_len = sizeof(rth->local);
28 getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len);
31 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0)
32 bb_perror_msg_and_die("getsockname");
33 if (addr_len != sizeof(rth->local))
34 bb_error_msg_and_die("wrong address length %d", addr_len);
35 if (rth->local.nl_family != AF_NETLINK)
36 bb_error_msg_and_die("wrong address family %d", rth->local.nl_family);
38 rth->seq = time(NULL);
41 int FAST_FUNC xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
48 req.nlh.nlmsg_len = sizeof(req);
49 req.nlh.nlmsg_type = type;
50 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
51 req.nlh.nlmsg_pid = 0;
52 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
53 req.g.rtgen_family = family;
55 return rtnl_send(rth, (void*)&req, sizeof(req));
58 //TODO: pass rth->fd instead of full rth?
59 int FAST_FUNC rtnl_send(struct rtnl_handle *rth, char *buf, int len)
61 struct sockaddr_nl nladdr;
63 memset(&nladdr, 0, sizeof(nladdr));
64 nladdr.nl_family = AF_NETLINK;
66 return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
69 int FAST_FUNC rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
72 struct sockaddr_nl nladdr;
73 struct iovec iov[2] = { { &nlh, sizeof(nlh) }, { req, len } };
74 /* Use designated initializers, struct layout is non-portable */
76 .msg_name = (void*)&nladdr,
77 .msg_namelen = sizeof(nladdr),
85 memset(&nladdr, 0, sizeof(nladdr));
86 nladdr.nl_family = AF_NETLINK;
88 nlh.nlmsg_len = NLMSG_LENGTH(len);
89 nlh.nlmsg_type = type;
90 nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
92 nlh.nlmsg_seq = rth->dump = ++rth->seq;
94 return sendmsg(rth->fd, &msg, 0);
97 static int rtnl_dump_filter(struct rtnl_handle *rth,
98 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *) FAST_FUNC,
100 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
104 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
105 struct sockaddr_nl nladdr;
106 struct iovec iov = { buf, 8*1024 };
111 /* Use designated initializers, struct layout is non-portable */
112 struct msghdr msg = {
113 .msg_name = (void*)&nladdr,
114 .msg_namelen = sizeof(nladdr),
122 status = recvmsg(rth->fd, &msg, 0);
127 bb_perror_msg("OVERRUN");
131 bb_error_msg("EOF on netlink");
134 if (msg.msg_namelen != sizeof(nladdr)) {
135 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
138 h = (struct nlmsghdr*)buf;
139 while (NLMSG_OK(h, status)) {
142 if (nladdr.nl_pid != 0 ||
143 h->nlmsg_pid != rth->local.nl_pid ||
144 h->nlmsg_seq != rth->dump
147 // err = junk(&nladdr, h, arg2);
156 if (h->nlmsg_type == NLMSG_DONE) {
159 if (h->nlmsg_type == NLMSG_ERROR) {
160 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
161 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
162 bb_error_msg("ERROR truncated");
164 errno = -l_err->error;
165 bb_perror_msg("RTNETLINK answers");
169 err = filter(&nladdr, h, arg1);
176 h = NLMSG_NEXT(h, status);
178 if (msg.msg_flags & MSG_TRUNC) {
179 bb_error_msg("message truncated");
183 bb_error_msg_and_die("remnant of size %d!", status);
193 int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth,
194 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *) FAST_FUNC,
197 int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/);
199 bb_error_msg_and_die("dump terminated");
203 int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
204 pid_t peer, unsigned groups,
205 struct nlmsghdr *answer,
206 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *),
209 /* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */
218 struct sockaddr_nl nladdr;
219 struct iovec iov = { (void*)n, n->nlmsg_len };
220 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
221 /* Use designated initializers, struct layout is non-portable */
222 struct msghdr msg = {
223 .msg_name = (void*)&nladdr,
224 .msg_namelen = 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("can't 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 ||
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);
341 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
344 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
345 rta->rta_type = type;
347 move_to_unaligned32(RTA_DATA(rta), data);
348 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
352 int FAST_FUNC addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
354 int len = RTA_LENGTH(alen);
357 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
360 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
361 rta->rta_type = type;
363 memcpy(RTA_DATA(rta), data, alen);
364 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
368 int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
370 int len = RTA_LENGTH(4);
371 struct rtattr *subrta;
373 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
376 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
377 subrta->rta_type = type;
378 subrta->rta_len = len;
379 move_to_unaligned32(RTA_DATA(subrta), data);
380 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
384 int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
386 struct rtattr *subrta;
387 int len = RTA_LENGTH(alen);
389 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
392 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
393 subrta->rta_type = type;
394 subrta->rta_len = len;
395 memcpy(RTA_DATA(subrta), data, alen);
396 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
401 void FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
403 while (RTA_OK(rta, len)) {
404 if (rta->rta_type <= max) {
405 tb[rta->rta_type] = rta;
407 rta = RTA_NEXT(rta, len);
410 bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);