]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/libnetlink.c
(Logical change 1.3)
[mirror_iproute2.git] / lib / libnetlink.c
1 /*
2 * libnetlink.c RTnetlink service routines.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <net/if_arp.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <time.h>
24 #include <sys/uio.h>
25
26 #include "libnetlink.h"
27
28 void rtnl_close(struct rtnl_handle *rth)
29 {
30 close(rth->fd);
31 }
32
33 int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
34 {
35 int addr_len;
36
37 memset(rth, 0, sizeof(rth));
38
39 rth->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
40 if (rth->fd < 0) {
41 perror("Cannot open netlink socket");
42 return -1;
43 }
44
45 memset(&rth->local, 0, sizeof(rth->local));
46 rth->local.nl_family = AF_NETLINK;
47 rth->local.nl_groups = subscriptions;
48
49 if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
50 perror("Cannot bind netlink socket");
51 return -1;
52 }
53 addr_len = sizeof(rth->local);
54 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
55 perror("Cannot getsockname");
56 return -1;
57 }
58 if (addr_len != sizeof(rth->local)) {
59 fprintf(stderr, "Wrong address length %d\n", addr_len);
60 return -1;
61 }
62 if (rth->local.nl_family != AF_NETLINK) {
63 fprintf(stderr, "Wrong address family %d\n", rth->local.nl_family);
64 return -1;
65 }
66 rth->seq = time(NULL);
67 return 0;
68 }
69
70 int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
71 {
72 struct {
73 struct nlmsghdr nlh;
74 struct rtgenmsg g;
75 } req;
76 struct sockaddr_nl nladdr;
77
78 memset(&nladdr, 0, sizeof(nladdr));
79 nladdr.nl_family = AF_NETLINK;
80
81 req.nlh.nlmsg_len = sizeof(req);
82 req.nlh.nlmsg_type = type;
83 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
84 req.nlh.nlmsg_pid = 0;
85 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
86 req.g.rtgen_family = family;
87
88 return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
89 }
90
91 int rtnl_send(struct rtnl_handle *rth, char *buf, int len)
92 {
93 struct sockaddr_nl nladdr;
94
95 memset(&nladdr, 0, sizeof(nladdr));
96 nladdr.nl_family = AF_NETLINK;
97
98 return sendto(rth->fd, buf, len, 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
99 }
100
101 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
102 {
103 struct nlmsghdr nlh;
104 struct sockaddr_nl nladdr;
105 struct iovec iov[2] = { { &nlh, sizeof(nlh) }, { req, len } };
106 struct msghdr msg = {
107 (void*)&nladdr, sizeof(nladdr),
108 iov, 2,
109 NULL, 0,
110 0
111 };
112
113 memset(&nladdr, 0, sizeof(nladdr));
114 nladdr.nl_family = AF_NETLINK;
115
116 nlh.nlmsg_len = NLMSG_LENGTH(len);
117 nlh.nlmsg_type = type;
118 nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
119 nlh.nlmsg_pid = 0;
120 nlh.nlmsg_seq = rth->dump = ++rth->seq;
121
122 return sendmsg(rth->fd, &msg, 0);
123 }
124
125 int rtnl_dump_filter(struct rtnl_handle *rth,
126 int (*filter)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
127 void *arg1,
128 int (*junk)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
129 void *arg2)
130 {
131 char buf[8192];
132 struct sockaddr_nl nladdr;
133 struct iovec iov = { buf, sizeof(buf) };
134
135 while (1) {
136 int status;
137 struct nlmsghdr *h;
138
139 struct msghdr msg = {
140 (void*)&nladdr, sizeof(nladdr),
141 &iov, 1,
142 NULL, 0,
143 0
144 };
145
146 status = recvmsg(rth->fd, &msg, 0);
147
148 if (status < 0) {
149 if (errno == EINTR)
150 continue;
151 perror("OVERRUN");
152 continue;
153 }
154 if (status == 0) {
155 fprintf(stderr, "EOF on netlink\n");
156 return -1;
157 }
158 if (msg.msg_namelen != sizeof(nladdr)) {
159 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
160 exit(1);
161 }
162
163 h = (struct nlmsghdr*)buf;
164 while (NLMSG_OK(h, status)) {
165 int err;
166
167 if (h->nlmsg_pid != rth->local.nl_pid ||
168 h->nlmsg_seq != rth->dump) {
169 if (junk) {
170 err = junk(&nladdr, h, arg2);
171 if (err < 0)
172 return err;
173 }
174 goto skip_it;
175 }
176
177 if (h->nlmsg_type == NLMSG_DONE)
178 return 0;
179 if (h->nlmsg_type == NLMSG_ERROR) {
180 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
181 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
182 fprintf(stderr, "ERROR truncated\n");
183 } else {
184 errno = -err->error;
185 perror("RTNETLINK answers");
186 }
187 return -1;
188 }
189 err = filter(&nladdr, h, arg1);
190 if (err < 0)
191 return err;
192
193 skip_it:
194 h = NLMSG_NEXT(h, status);
195 }
196 if (msg.msg_flags & MSG_TRUNC) {
197 fprintf(stderr, "Message truncated\n");
198 continue;
199 }
200 if (status) {
201 fprintf(stderr, "!!!Remnant of size %d\n", status);
202 exit(1);
203 }
204 }
205 }
206
207 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
208 unsigned groups, struct nlmsghdr *answer,
209 int (*junk)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
210 void *jarg)
211 {
212 int status;
213 unsigned seq;
214 struct nlmsghdr *h;
215 struct sockaddr_nl nladdr;
216 struct iovec iov = { (void*)n, n->nlmsg_len };
217 char buf[8192];
218 struct msghdr msg = {
219 (void*)&nladdr, sizeof(nladdr),
220 &iov, 1,
221 NULL, 0,
222 0
223 };
224
225 memset(&nladdr, 0, sizeof(nladdr));
226 nladdr.nl_family = AF_NETLINK;
227 nladdr.nl_pid = peer;
228 nladdr.nl_groups = groups;
229
230 n->nlmsg_seq = seq = ++rtnl->seq;
231 if (answer == NULL)
232 n->nlmsg_flags |= NLM_F_ACK;
233
234 status = sendmsg(rtnl->fd, &msg, 0);
235
236 if (status < 0) {
237 perror("Cannot talk to rtnetlink");
238 return -1;
239 }
240
241 iov.iov_base = buf;
242
243 while (1) {
244 iov.iov_len = sizeof(buf);
245 status = recvmsg(rtnl->fd, &msg, 0);
246
247 if (status < 0) {
248 if (errno == EINTR)
249 continue;
250 perror("OVERRUN");
251 continue;
252 }
253 if (status == 0) {
254 fprintf(stderr, "EOF on netlink\n");
255 return -1;
256 }
257 if (msg.msg_namelen != sizeof(nladdr)) {
258 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
259 exit(1);
260 }
261 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
262 int err;
263 int len = h->nlmsg_len;
264 int l = len - sizeof(*h);
265
266 if (l<0 || len>status) {
267 if (msg.msg_flags & MSG_TRUNC) {
268 fprintf(stderr, "Truncated message\n");
269 return -1;
270 }
271 fprintf(stderr, "!!!malformed message: len=%d\n", len);
272 exit(1);
273 }
274
275 if (h->nlmsg_pid != rtnl->local.nl_pid ||
276 h->nlmsg_seq != seq) {
277 if (junk) {
278 err = junk(&nladdr, h, jarg);
279 if (err < 0)
280 return err;
281 }
282 continue;
283 }
284
285 if (h->nlmsg_type == NLMSG_ERROR) {
286 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
287 if (l < sizeof(struct nlmsgerr)) {
288 fprintf(stderr, "ERROR truncated\n");
289 } else {
290 errno = -err->error;
291 if (errno == 0) {
292 if (answer)
293 memcpy(answer, h, h->nlmsg_len);
294 return 0;
295 }
296 perror("RTNETLINK answers");
297 }
298 return -1;
299 }
300 if (answer) {
301 memcpy(answer, h, h->nlmsg_len);
302 return 0;
303 }
304
305 fprintf(stderr, "Unexpected reply!!!\n");
306
307 status -= NLMSG_ALIGN(len);
308 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
309 }
310 if (msg.msg_flags & MSG_TRUNC) {
311 fprintf(stderr, "Message truncated\n");
312 continue;
313 }
314 if (status) {
315 fprintf(stderr, "!!!Remnant of size %d\n", status);
316 exit(1);
317 }
318 }
319 }
320
321 int rtnl_listen(struct rtnl_handle *rtnl,
322 int (*handler)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
323 void *jarg)
324 {
325 int status;
326 struct nlmsghdr *h;
327 struct sockaddr_nl nladdr;
328 struct iovec iov;
329 char buf[8192];
330 struct msghdr msg = {
331 (void*)&nladdr, sizeof(nladdr),
332 &iov, 1,
333 NULL, 0,
334 0
335 };
336
337 memset(&nladdr, 0, sizeof(nladdr));
338 nladdr.nl_family = AF_NETLINK;
339 nladdr.nl_pid = 0;
340 nladdr.nl_groups = 0;
341
342
343 iov.iov_base = buf;
344
345 while (1) {
346 iov.iov_len = sizeof(buf);
347 status = recvmsg(rtnl->fd, &msg, 0);
348
349 if (status < 0) {
350 if (errno == EINTR)
351 continue;
352 perror("OVERRUN");
353 continue;
354 }
355 if (status == 0) {
356 fprintf(stderr, "EOF on netlink\n");
357 return -1;
358 }
359 if (msg.msg_namelen != sizeof(nladdr)) {
360 fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
361 exit(1);
362 }
363 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
364 int err;
365 int len = h->nlmsg_len;
366 int l = len - sizeof(*h);
367
368 if (l<0 || len>status) {
369 if (msg.msg_flags & MSG_TRUNC) {
370 fprintf(stderr, "Truncated message\n");
371 return -1;
372 }
373 fprintf(stderr, "!!!malformed message: len=%d\n", len);
374 exit(1);
375 }
376
377 err = handler(&nladdr, h, jarg);
378 if (err < 0)
379 return err;
380
381 status -= NLMSG_ALIGN(len);
382 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
383 }
384 if (msg.msg_flags & MSG_TRUNC) {
385 fprintf(stderr, "Message truncated\n");
386 continue;
387 }
388 if (status) {
389 fprintf(stderr, "!!!Remnant of size %d\n", status);
390 exit(1);
391 }
392 }
393 }
394
395 int rtnl_from_file(FILE *rtnl,
396 int (*handler)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
397 void *jarg)
398 {
399 int status;
400 struct sockaddr_nl nladdr;
401 char buf[8192];
402 struct nlmsghdr *h = (void*)buf;
403
404 memset(&nladdr, 0, sizeof(nladdr));
405 nladdr.nl_family = AF_NETLINK;
406 nladdr.nl_pid = 0;
407 nladdr.nl_groups = 0;
408
409 while (1) {
410 int err, len, type;
411 int l;
412
413 status = fread(&buf, 1, sizeof(*h), rtnl);
414
415 if (status < 0) {
416 if (errno == EINTR)
417 continue;
418 perror("rtnl_from_file: fread");
419 return -1;
420 }
421 if (status == 0)
422 return 0;
423
424 len = h->nlmsg_len;
425 type= h->nlmsg_type;
426 l = len - sizeof(*h);
427
428 if (l<0 || len>sizeof(buf)) {
429 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
430 len, ftell(rtnl));
431 return -1;
432 }
433
434 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
435
436 if (status < 0) {
437 perror("rtnl_from_file: fread");
438 return -1;
439 }
440 if (status < l) {
441 fprintf(stderr, "rtnl-from_file: truncated message\n");
442 return -1;
443 }
444
445 err = handler(&nladdr, h, jarg);
446 if (err < 0)
447 return err;
448 }
449 }
450
451 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
452 {
453 int len = RTA_LENGTH(4);
454 struct rtattr *rta;
455 if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
456 return -1;
457 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
458 rta->rta_type = type;
459 rta->rta_len = len;
460 memcpy(RTA_DATA(rta), &data, 4);
461 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
462 return 0;
463 }
464
465 int addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
466 {
467 int len = RTA_LENGTH(alen);
468 struct rtattr *rta;
469
470 if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
471 return -1;
472 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
473 rta->rta_type = type;
474 rta->rta_len = len;
475 memcpy(RTA_DATA(rta), data, alen);
476 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
477 return 0;
478 }
479
480 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
481 {
482 int len = RTA_LENGTH(4);
483 struct rtattr *subrta;
484
485 if (RTA_ALIGN(rta->rta_len) + len > maxlen)
486 return -1;
487 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
488 subrta->rta_type = type;
489 subrta->rta_len = len;
490 memcpy(RTA_DATA(subrta), &data, 4);
491 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
492 return 0;
493 }
494
495 int rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
496 {
497 struct rtattr *subrta;
498 int len = RTA_LENGTH(alen);
499
500 if (RTA_ALIGN(rta->rta_len) + len > maxlen)
501 return -1;
502 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
503 subrta->rta_type = type;
504 subrta->rta_len = len;
505 memcpy(RTA_DATA(subrta), data, alen);
506 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
507 return 0;
508 }
509
510
511 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
512 {
513 while (RTA_OK(rta, len)) {
514 if (rta->rta_type <= max)
515 tb[rta->rta_type] = rta;
516 rta = RTA_NEXT(rta,len);
517 }
518 if (len)
519 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
520 return 0;
521 }