summaryrefslogtreecommitdiff
path: root/networking/libiproute/libnetlink.c (plain)
blob: a1a9fc1dc19fd20cd89deb514d0e9dc5b67417e5
1/* vi: set sw=4 ts=4: */
2/*
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.
7 *
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 */
10
11#include <sys/socket.h>
12#include <sys/uio.h>
13
14#include "libbb.h"
15#include "libnetlink.h"
16
17void FAST_FUNC xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/)
18{
19 socklen_t addr_len;
20
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;*/
25
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);
29
30/* too much paranoia
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);
37*/
38 rth->seq = time(NULL);
39}
40
41int FAST_FUNC xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
42{
43 struct {
44 struct nlmsghdr nlh;
45 struct rtgenmsg g;
46 } req;
47
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;
54
55 return rtnl_send(rth, (void*)&req, sizeof(req));
56}
57
58//TODO: pass rth->fd instead of full rth?
59int FAST_FUNC rtnl_send(struct rtnl_handle *rth, char *buf, int len)
60{
61 struct sockaddr_nl nladdr;
62
63 memset(&nladdr, 0, sizeof(nladdr));
64 nladdr.nl_family = AF_NETLINK;
65
66 return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
67}
68
69int FAST_FUNC rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
70{
71 struct {
72 struct nlmsghdr nlh;
73 struct msghdr msg;
74 struct sockaddr_nl nladdr;
75 } s;
76 struct iovec iov[2] = { { &s.nlh, sizeof(s.nlh) }, { req, len } };
77
78 memset(&s, 0, sizeof(s));
79
80 s.msg.msg_name = (void*)&s.nladdr;
81 s.msg.msg_namelen = sizeof(s.nladdr);
82 s.msg.msg_iov = iov;
83 s.msg.msg_iovlen = 2;
84 /*s.msg.msg_control = NULL; - already is */
85 /*s.msg.msg_controllen = 0; - already is */
86 /*s.msg.msg_flags = 0; - already is */
87
88 s.nladdr.nl_family = AF_NETLINK;
89
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;
95
96 return sendmsg(rth->fd, &s.msg, 0);
97}
98
99static int rtnl_dump_filter(struct rtnl_handle *rth,
100 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *) FAST_FUNC,
101 void *arg1/*,
102 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
103 void *arg2*/)
104{
105 int retval = -1;
106 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
107 struct sockaddr_nl nladdr;
108 struct iovec iov = { buf, 8*1024 };
109
110 while (1) {
111 int status;
112 struct nlmsghdr *h;
113 /* Use designated initializers, struct layout is non-portable */
114 struct msghdr msg = {
115 .msg_name = (void*)&nladdr,
116 .msg_namelen = sizeof(nladdr),
117 .msg_iov = &iov,
118 .msg_iovlen = 1,
119 .msg_control = NULL,
120 .msg_controllen = 0,
121 .msg_flags = 0
122 };
123
124 status = recvmsg(rth->fd, &msg, 0);
125
126 if (status < 0) {
127 if (errno == EINTR)
128 continue;
129 bb_perror_msg("OVERRUN");
130 continue;
131 }
132 if (status == 0) {
133 bb_error_msg("EOF on netlink");
134 goto ret;
135 }
136 if (msg.msg_namelen != sizeof(nladdr)) {
137 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
138 }
139
140 h = (struct nlmsghdr*)buf;
141 while (NLMSG_OK(h, (unsigned) status)) {
142 int err;
143
144 if (nladdr.nl_pid != 0 ||
145 h->nlmsg_pid != rth->local.nl_pid ||
146 h->nlmsg_seq != rth->dump
147 ) {
148// if (junk) {
149// err = junk(&nladdr, h, arg2);
150// if (err < 0) {
151// retval = err;
152// goto ret;
153// }
154// }
155 goto skip_it;
156 }
157
158 if (h->nlmsg_type == NLMSG_DONE) {
159 goto ret_0;
160 }
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");
165 } else {
166 errno = -l_err->error;
167 bb_perror_msg("RTNETLINK answers");
168 }
169 goto ret;
170 }
171 err = filter(&nladdr, h, arg1);
172 if (err < 0) {
173 retval = err;
174 goto ret;
175 }
176
177 skip_it:
178 h = NLMSG_NEXT(h, status);
179 }
180 if (msg.msg_flags & MSG_TRUNC) {
181 bb_error_msg("message truncated");
182 continue;
183 }
184 if (status) {
185 bb_error_msg_and_die("remnant of size %d!", status);
186 }
187 } /* while (1) */
188 ret_0:
189 retval++; /* = 0 */
190 ret:
191 free(buf);
192 return retval;
193}
194
195int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth,
196 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *) FAST_FUNC,
197 void *arg1)
198{
199 int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/);
200 if (ret < 0)
201 bb_error_msg_and_die("dump terminated");
202 return ret;
203}
204
205int 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 *),
209 void *jarg)
210{
211/* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */
212#define peer 0
213#define groups 0
214#define junk NULL
215#define jarg NULL
216 int retval = -1;
217 int status;
218 unsigned seq;
219 struct nlmsghdr *h;
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),
227 .msg_iov = &iov,
228 .msg_iovlen = 1,
229 .msg_control = NULL,
230 .msg_controllen = 0,
231 .msg_flags = 0
232 };
233
234 memset(&nladdr, 0, sizeof(nladdr));
235 nladdr.nl_family = AF_NETLINK;
236// nladdr.nl_pid = peer;
237// nladdr.nl_groups = groups;
238
239 n->nlmsg_seq = seq = ++rtnl->seq;
240 if (answer == NULL) {
241 n->nlmsg_flags |= NLM_F_ACK;
242 }
243 status = sendmsg(rtnl->fd, &msg, 0);
244
245 if (status < 0) {
246 bb_perror_msg("can't talk to rtnetlink");
247 goto ret;
248 }
249
250 iov.iov_base = buf;
251
252 while (1) {
253 iov.iov_len = 8*1024;
254 status = recvmsg(rtnl->fd, &msg, 0);
255
256 if (status < 0) {
257 if (errno == EINTR) {
258 continue;
259 }
260 bb_perror_msg("OVERRUN");
261 continue;
262 }
263 if (status == 0) {
264 bb_error_msg("EOF on netlink");
265 goto ret;
266 }
267 if (msg.msg_namelen != sizeof(nladdr)) {
268 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
269 }
270 for (h = (struct nlmsghdr*)buf; status >= (int)sizeof(*h); ) {
271// int l_err;
272 int len = h->nlmsg_len;
273 int l = len - sizeof(*h);
274
275 if (l < 0 || len > status) {
276 if (msg.msg_flags & MSG_TRUNC) {
277 bb_error_msg("truncated message");
278 goto ret;
279 }
280 bb_error_msg_and_die("malformed message: len=%d!", len);
281 }
282
283 if (nladdr.nl_pid != peer ||
284 h->nlmsg_pid != rtnl->local.nl_pid ||
285 h->nlmsg_seq != seq
286 ) {
287// if (junk) {
288// l_err = junk(&nladdr, h, jarg);
289// if (l_err < 0) {
290// retval = l_err;
291// goto ret;
292// }
293// }
294 continue;
295 }
296
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");
301 } else {
302 errno = - err->error;
303 if (errno == 0) {
304 if (answer) {
305 memcpy(answer, h, h->nlmsg_len);
306 }
307 goto ret_0;
308 }
309 bb_perror_msg("RTNETLINK answers");
310 }
311 goto ret;
312 }
313 if (answer) {
314 memcpy(answer, h, h->nlmsg_len);
315 goto ret_0;
316 }
317
318 bb_error_msg("unexpected reply!");
319
320 status -= NLMSG_ALIGN(len);
321 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
322 }
323 if (msg.msg_flags & MSG_TRUNC) {
324 bb_error_msg("message truncated");
325 continue;
326 }
327 if (status) {
328 bb_error_msg_and_die("remnant of size %d!", status);
329 }
330 } /* while (1) */
331 ret_0:
332 retval++; /* = 0 */
333 ret:
334 free(buf);
335 return retval;
336}
337
338int FAST_FUNC addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
339{
340 int len = RTA_LENGTH(4);
341 struct rtattr *rta;
342
343 if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) {
344 return -1;
345 }
346 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
347 rta->rta_type = type;
348 rta->rta_len = len;
349 move_to_unaligned32(RTA_DATA(rta), data);
350 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len);
351 return 0;
352}
353
354int FAST_FUNC addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
355{
356 int len = RTA_LENGTH(alen);
357 struct rtattr *rta;
358
359 if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) {
360 return -1;
361 }
362 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
363 rta->rta_type = type;
364 rta->rta_len = len;
365 memcpy(RTA_DATA(rta), data, alen);
366 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len);
367 return 0;
368}
369
370int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
371{
372 int len = RTA_LENGTH(4);
373 struct rtattr *subrta;
374
375 if (RTA_ALIGN(rta->rta_len + len) > maxlen) {
376 return -1;
377 }
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);
383 return 0;
384}
385
386int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
387{
388 struct rtattr *subrta;
389 int len = RTA_LENGTH(alen);
390
391 if (RTA_ALIGN(rta->rta_len + len) > maxlen) {
392 return -1;
393 }
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);
399 return 0;
400}
401
402
403void FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
404{
405 while (RTA_OK(rta, len)) {
406 if (rta->rta_type <= max) {
407 tb[rta->rta_type] = rta;
408 }
409 rta = RTA_NEXT(rta, len);
410 }
411 if (len) {
412 bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
413 }
414}
415