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