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)
74 struct sockaddr_nl nladdr;
76 struct iovec iov[2] = { { &s.nlh, sizeof(s.nlh) }, { req, len } };
78 memset(&s, 0, sizeof(s));
80 s.msg.msg_name = (void*)&s.nladdr;
81 s.msg.msg_namelen = sizeof(s.nladdr);
84 /*s.msg.msg_control = NULL; - already is */
85 /*s.msg.msg_controllen = 0; - already is */
86 /*s.msg.msg_flags = 0; - already is */
88 s.nladdr.nl_family = AF_NETLINK;
90 s.nlh.nlmsg_len = NLMSG_LENGTH(len);
91 s.nlh.nlmsg_type = type;
92 s.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
93 /*s.nlh.nlmsg_pid = 0; - already is */
94 s.nlh.nlmsg_seq = rth->dump = ++rth->seq;
96 return sendmsg(rth->fd, &s.msg, 0);
99 static int rtnl_dump_filter(struct rtnl_handle *rth,
100 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *) FAST_FUNC,
102 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
106 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
107 struct sockaddr_nl nladdr;
108 struct iovec iov = { buf, 8*1024 };
113 /* Use designated initializers, struct layout is non-portable */
114 struct msghdr msg = {
115 .msg_name = (void*)&nladdr,
116 .msg_namelen = sizeof(nladdr),
124 status = recvmsg(rth->fd, &msg, 0);
129 bb_perror_msg("OVERRUN");
133 bb_error_msg("EOF on netlink");
136 if (msg.msg_namelen != sizeof(nladdr)) {
137 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
140 h = (struct nlmsghdr*)buf;
141 while (NLMSG_OK(h, status)) {
144 if (nladdr.nl_pid != 0 ||
145 h->nlmsg_pid != rth->local.nl_pid ||
146 h->nlmsg_seq != rth->dump
149 // err = junk(&nladdr, h, arg2);
158 if (h->nlmsg_type == NLMSG_DONE) {
161 if (h->nlmsg_type == NLMSG_ERROR) {
162 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
163 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
164 bb_error_msg("ERROR truncated");
166 errno = -l_err->error;
167 bb_perror_msg("RTNETLINK answers");
171 err = filter(&nladdr, h, arg1);
178 h = NLMSG_NEXT(h, status);
180 if (msg.msg_flags & MSG_TRUNC) {
181 bb_error_msg("message truncated");
185 bb_error_msg_and_die("remnant of size %d!", status);
195 int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth,
196 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *) FAST_FUNC,
199 int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/);
201 bb_error_msg_and_die("dump terminated");
205 int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
206 pid_t peer, unsigned groups,
207 struct nlmsghdr *answer,
208 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *),
211 /* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */
220 struct sockaddr_nl nladdr;
221 struct iovec iov = { (void*)n, n->nlmsg_len };
222 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
223 /* Use designated initializers, struct layout is non-portable */
224 struct msghdr msg = {
225 .msg_name = (void*)&nladdr,
226 .msg_namelen = sizeof(nladdr),
234 memset(&nladdr, 0, sizeof(nladdr));
235 nladdr.nl_family = AF_NETLINK;
236 // nladdr.nl_pid = peer;
237 // nladdr.nl_groups = groups;
239 n->nlmsg_seq = seq = ++rtnl->seq;
240 if (answer == NULL) {
241 n->nlmsg_flags |= NLM_F_ACK;
243 status = sendmsg(rtnl->fd, &msg, 0);
246 bb_perror_msg("can't talk to rtnetlink");
253 iov.iov_len = 8*1024;
254 status = recvmsg(rtnl->fd, &msg, 0);
257 if (errno == EINTR) {
260 bb_perror_msg("OVERRUN");
264 bb_error_msg("EOF on netlink");
267 if (msg.msg_namelen != sizeof(nladdr)) {
268 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
270 for (h = (struct nlmsghdr*)buf; status >= (int)sizeof(*h); ) {
272 int len = h->nlmsg_len;
273 int l = len - sizeof(*h);
275 if (l < 0 || len > status) {
276 if (msg.msg_flags & MSG_TRUNC) {
277 bb_error_msg("truncated message");
280 bb_error_msg_and_die("malformed message: len=%d!", len);
283 if (nladdr.nl_pid != peer ||
284 h->nlmsg_pid != rtnl->local.nl_pid ||
288 // l_err = junk(&nladdr, h, jarg);
297 if (h->nlmsg_type == NLMSG_ERROR) {
298 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
299 if (l < (int)sizeof(struct nlmsgerr)) {
300 bb_error_msg("ERROR truncated");
302 errno = - err->error;
305 memcpy(answer, h, h->nlmsg_len);
309 bb_perror_msg("RTNETLINK answers");
314 memcpy(answer, h, h->nlmsg_len);
318 bb_error_msg("unexpected reply!");
320 status -= NLMSG_ALIGN(len);
321 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
323 if (msg.msg_flags & MSG_TRUNC) {
324 bb_error_msg("message truncated");
328 bb_error_msg_and_die("remnant of size %d!", status);
338 int FAST_FUNC addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
340 int len = RTA_LENGTH(4);
343 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
346 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
347 rta->rta_type = type;
349 move_to_unaligned32(RTA_DATA(rta), data);
350 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
354 int FAST_FUNC addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
356 int len = RTA_LENGTH(alen);
359 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
362 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
363 rta->rta_type = type;
365 memcpy(RTA_DATA(rta), data, alen);
366 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
370 int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
372 int len = RTA_LENGTH(4);
373 struct rtattr *subrta;
375 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
378 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
379 subrta->rta_type = type;
380 subrta->rta_len = len;
381 move_to_unaligned32(RTA_DATA(subrta), data);
382 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
386 int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
388 struct rtattr *subrta;
389 int len = RTA_LENGTH(alen);
391 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
394 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
395 subrta->rta_type = type;
396 subrta->rta_len = len;
397 memcpy(RTA_DATA(subrta), data, alen);
398 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
403 void FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
405 while (RTA_OK(rta, len)) {
406 if (rta->rta_type <= max) {
407 tb[rta->rta_type] = rta;
409 rta = RTA_NEXT(rta, len);
412 bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);