blob: 5307cec2c9ede22459dc6b57d98e5b7bbdd0b56b [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
3 * libnetlink.c 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
Rob Landleyecae66a2006-06-02 20:53:38 +000014#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015#include <sys/socket.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000016
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017#include <errno.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000018#include <string.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000019#include <time.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000020#include <unistd.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000021
Eric Andersen0f08e532003-06-20 09:05:00 +000022#include <sys/uio.h>
23
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000024#include "libnetlink.h"
25
26void rtnl_close(struct rtnl_handle *rth)
27{
28 close(rth->fd);
29}
30
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000031int xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000032{
Eric Andersend78aea82006-01-30 18:00:02 +000033 socklen_t addr_len;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000034
35 memset(rth, 0, sizeof(rth));
36
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000037 rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000038
39 memset(&rth->local, 0, sizeof(rth->local));
40 rth->local.nl_family = AF_NETLINK;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000041 /*rth->local.nl_groups = subscriptions;*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000042
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000043 xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000044 addr_len = sizeof(rth->local);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000045 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0)
46 bb_perror_msg_and_die("cannot getsockname");
47 if (addr_len != sizeof(rth->local))
48 bb_error_msg_and_die("wrong address length %d", addr_len);
49 if (rth->local.nl_family != AF_NETLINK)
50 bb_error_msg_and_die("wrong address family %d", rth->local.nl_family);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000051 rth->seq = time(NULL);
52 return 0;
53}
54
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000055int xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000056{
57 struct {
58 struct nlmsghdr nlh;
59 struct rtgenmsg g;
60 } req;
61 struct sockaddr_nl nladdr;
62
63 memset(&nladdr, 0, sizeof(nladdr));
64 nladdr.nl_family = AF_NETLINK;
65
66 req.nlh.nlmsg_len = sizeof(req);
67 req.nlh.nlmsg_type = type;
68 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
69 req.nlh.nlmsg_pid = 0;
70 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
71 req.g.rtgen_family = family;
72
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000073 return xsendto(rth->fd, (void*)&req, sizeof(req),
74 (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000075}
76
77int rtnl_send(struct rtnl_handle *rth, char *buf, int len)
78{
79 struct sockaddr_nl nladdr;
80
81 memset(&nladdr, 0, sizeof(nladdr));
82 nladdr.nl_family = AF_NETLINK;
83
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000084 return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000085}
86
87int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
88{
89 struct nlmsghdr nlh;
90 struct sockaddr_nl nladdr;
91 struct iovec iov[2] = { { &nlh, sizeof(nlh) }, { req, len } };
92 struct msghdr msg = {
93 (void*)&nladdr, sizeof(nladdr),
94 iov, 2,
95 NULL, 0,
96 0
97 };
98
99 memset(&nladdr, 0, sizeof(nladdr));
100 nladdr.nl_family = AF_NETLINK;
101
102 nlh.nlmsg_len = NLMSG_LENGTH(len);
103 nlh.nlmsg_type = type;
104 nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
105 nlh.nlmsg_pid = 0;
106 nlh.nlmsg_seq = rth->dump = ++rth->seq;
107
108 return sendmsg(rth->fd, &msg, 0);
109}
110
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000111static int rtnl_dump_filter(struct rtnl_handle *rth,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000112 int (*filter)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000113 void *arg1/*,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000114 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000115 void *arg2*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000116{
Denis Vlasenkoea620772006-10-14 02:23:43 +0000117 char buf[8192];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000118 struct sockaddr_nl nladdr;
119 struct iovec iov = { buf, sizeof(buf) };
120
121 while (1) {
122 int status;
123 struct nlmsghdr *h;
124
125 struct msghdr msg = {
126 (void*)&nladdr, sizeof(nladdr),
127 &iov, 1,
128 NULL, 0,
129 0
130 };
131
132 status = recvmsg(rth->fd, &msg, 0);
133
134 if (status < 0) {
135 if (errno == EINTR)
136 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137 bb_perror_msg("OVERRUN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000138 continue;
139 }
140 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000141 bb_error_msg("EOF on netlink");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000142 return -1;
143 }
144 if (msg.msg_namelen != sizeof(nladdr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000145 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000146 }
147
148 h = (struct nlmsghdr*)buf;
149 while (NLMSG_OK(h, status)) {
150 int err;
151
Eric Andersen6c8161d2004-05-05 07:05:32 +0000152 if (nladdr.nl_pid != 0 ||
153 h->nlmsg_pid != rth->local.nl_pid ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000154 h->nlmsg_seq != rth->dump) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000155/* if (junk) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000156 err = junk(&nladdr, h, arg2);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000157 if (err < 0)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000158 return err;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000159 } */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000160 goto skip_it;
161 }
162
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000163 if (h->nlmsg_type == NLMSG_DONE) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000164 return 0;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000165 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000166 if (h->nlmsg_type == NLMSG_ERROR) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000167 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000168 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000170 } else {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000171 errno = -l_err->error;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000173 }
174 return -1;
175 }
176 err = filter(&nladdr, h, arg1);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000177 if (err < 0)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000178 return err;
179
180skip_it:
181 h = NLMSG_NEXT(h, status);
182 }
183 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000184 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000185 continue;
186 }
187 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000188 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000189 }
190 }
191}
192
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000193int xrtnl_dump_filter(struct rtnl_handle *rth,
194 int (*filter)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
195 void *arg1)
196{
197 int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/);
198 if (ret < 0)
199 bb_error_msg_and_die("dump terminated");
200 return ret;
201}
202
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000203int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
204 unsigned groups, struct nlmsghdr *answer,
205 int (*junk)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
206 void *jarg)
207{
208 int status;
209 unsigned seq;
210 struct nlmsghdr *h;
211 struct sockaddr_nl nladdr;
212 struct iovec iov = { (void*)n, n->nlmsg_len };
213 char buf[8192];
214 struct msghdr msg = {
215 (void*)&nladdr, sizeof(nladdr),
216 &iov, 1,
217 NULL, 0,
218 0
219 };
220
221 memset(&nladdr, 0, sizeof(nladdr));
222 nladdr.nl_family = AF_NETLINK;
223 nladdr.nl_pid = peer;
224 nladdr.nl_groups = groups;
225
226 n->nlmsg_seq = seq = ++rtnl->seq;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000227 if (answer == NULL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000228 n->nlmsg_flags |= NLM_F_ACK;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000229 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000230 status = sendmsg(rtnl->fd, &msg, 0);
231
232 if (status < 0) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000233 bb_perror_msg("cannot talk to rtnetlink");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000234 return -1;
235 }
236
237 iov.iov_base = buf;
238
239 while (1) {
240 iov.iov_len = sizeof(buf);
241 status = recvmsg(rtnl->fd, &msg, 0);
242
243 if (status < 0) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000244 if (errno == EINTR) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000245 continue;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000246 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000247 bb_perror_msg("OVERRUN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000248 continue;
249 }
250 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000251 bb_error_msg("EOF on netlink");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000252 return -1;
253 }
254 if (msg.msg_namelen != sizeof(nladdr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000255 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000256 }
257 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000258 int l_err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000259 int len = h->nlmsg_len;
260 int l = len - sizeof(*h);
261
262 if (l<0 || len>status) {
263 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000264 bb_error_msg("truncated message");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000265 return -1;
266 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000267 bb_error_msg_and_die("malformed message: len=%d!", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000268 }
269
Eric Andersen6c8161d2004-05-05 07:05:32 +0000270 if (nladdr.nl_pid != peer ||
271 h->nlmsg_pid != rtnl->local.nl_pid ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000272 h->nlmsg_seq != seq) {
273 if (junk) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000274 l_err = junk(&nladdr, h, jarg);
275 if (l_err < 0) {
276 return l_err;
277 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000278 }
279 continue;
280 }
281
282 if (h->nlmsg_type == NLMSG_ERROR) {
283 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
284 if (l < sizeof(struct nlmsgerr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000285 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000286 } else {
287 errno = -err->error;
288 if (errno == 0) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000289 if (answer) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000290 memcpy(answer, h, h->nlmsg_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000291 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000292 return 0;
293 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000294 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000295 }
296 return -1;
297 }
298 if (answer) {
299 memcpy(answer, h, h->nlmsg_len);
300 return 0;
301 }
302
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000303 bb_error_msg("unexpected reply!");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000304
305 status -= NLMSG_ALIGN(len);
306 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
307 }
308 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000309 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000310 continue;
311 }
312 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000313 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000314 }
315 }
316}
317
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000318int addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000319{
320 int len = RTA_LENGTH(4);
321 struct rtattr *rta;
322 if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
323 return -1;
324 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
325 rta->rta_type = type;
326 rta->rta_len = len;
327 memcpy(RTA_DATA(rta), &data, 4);
328 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
329 return 0;
330}
331
332int addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
333{
334 int len = RTA_LENGTH(alen);
335 struct rtattr *rta;
336
337 if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
338 return -1;
339 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
340 rta->rta_type = type;
341 rta->rta_len = len;
342 memcpy(RTA_DATA(rta), data, alen);
343 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
344 return 0;
345}
346
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000347int rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000348{
349 int len = RTA_LENGTH(4);
350 struct rtattr *subrta;
351
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000352 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000353 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000354 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000355 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
356 subrta->rta_type = type;
357 subrta->rta_len = len;
358 memcpy(RTA_DATA(subrta), &data, 4);
359 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
360 return 0;
361}
362
363int rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
364{
365 struct rtattr *subrta;
366 int len = RTA_LENGTH(alen);
367
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000368 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000369 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000370 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000371 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
372 subrta->rta_type = type;
373 subrta->rta_len = len;
374 memcpy(RTA_DATA(subrta), data, alen);
375 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
376 return 0;
377}
378
379
380int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
381{
382 while (RTA_OK(rta, len)) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000383 if (rta->rta_type <= max) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000384 tb[rta->rta_type] = rta;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000385 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000386 rta = RTA_NEXT(rta,len);
387 }
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000388 if (len) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000389 bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000390 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000391 return 0;
392}