]> git.ipfire.org Git - people/ms/rstp.git/blob - libnetlink.c
Build brmon again
[people/ms/rstp.git] / 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_byproto(struct rtnl_handle *rth, unsigned subscriptions,
34 int protocol)
35 {
36 socklen_t addr_len;
37 int sndbuf = 32768;
38 int rcvbuf = 32768;
39
40 memset(rth, 0, sizeof(rth));
41
42 rth->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
43 if (rth->fd < 0) {
44 perror("Cannot open netlink socket");
45 return -1;
46 }
47
48 if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))
49 < 0) {
50 perror("SO_SNDBUF");
51 return -1;
52 }
53
54 if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf))
55 < 0) {
56 perror("SO_RCVBUF");
57 return -1;
58 }
59
60 memset(&rth->local, 0, sizeof(rth->local));
61 rth->local.nl_family = AF_NETLINK;
62 rth->local.nl_groups = subscriptions;
63
64 if (bind(rth->fd, (struct sockaddr *)&rth->local, sizeof(rth->local)) <
65 0) {
66 perror("Cannot bind netlink socket");
67 return -1;
68 }
69 addr_len = sizeof(rth->local);
70 if (getsockname(rth->fd, (struct sockaddr *)&rth->local, &addr_len) < 0) {
71 perror("Cannot getsockname");
72 return -1;
73 }
74 if (addr_len != sizeof(rth->local)) {
75 fprintf(stderr, "Wrong address length %d\n", addr_len);
76 return -1;
77 }
78 if (rth->local.nl_family != AF_NETLINK) {
79 fprintf(stderr, "Wrong address family %d\n",
80 rth->local.nl_family);
81 return -1;
82 }
83 rth->seq = time(NULL);
84 return 0;
85 }
86
87 int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
88 {
89 return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
90 }
91
92 int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
93 {
94 struct {
95 struct nlmsghdr nlh;
96 struct rtgenmsg g;
97 } req;
98 struct sockaddr_nl nladdr;
99
100 memset(&nladdr, 0, sizeof(nladdr));
101 nladdr.nl_family = AF_NETLINK;
102
103 memset(&req, 0, sizeof(req));
104 req.nlh.nlmsg_len = sizeof(req);
105 req.nlh.nlmsg_type = type;
106 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
107 req.nlh.nlmsg_pid = 0;
108 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
109 req.g.rtgen_family = family;
110
111 return sendto(rth->fd, (void *)&req, sizeof(req), 0,
112 (struct sockaddr *)&nladdr, sizeof(nladdr));
113 }
114
115 int rtnl_send(struct rtnl_handle *rth, const char *buf, int len)
116 {
117 struct sockaddr_nl nladdr;
118
119 memset(&nladdr, 0, sizeof(nladdr));
120 nladdr.nl_family = AF_NETLINK;
121
122 return sendto(rth->fd, buf, len, 0, (struct sockaddr *)&nladdr,
123 sizeof(nladdr));
124 }
125
126 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
127 {
128 struct nlmsghdr nlh;
129 struct sockaddr_nl nladdr;
130 struct iovec iov[2] = {
131 {.iov_base = &nlh,.iov_len = sizeof(nlh)}
132 ,
133 {.iov_base = req,.iov_len = len}
134 };
135 struct msghdr msg = {
136 .msg_name = &nladdr,
137 .msg_namelen = sizeof(nladdr),
138 .msg_iov = iov,
139 .msg_iovlen = 2,
140 };
141
142 memset(&nladdr, 0, sizeof(nladdr));
143 nladdr.nl_family = AF_NETLINK;
144
145 nlh.nlmsg_len = NLMSG_LENGTH(len);
146 nlh.nlmsg_type = type;
147 nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
148 nlh.nlmsg_pid = 0;
149 nlh.nlmsg_seq = rth->dump = ++rth->seq;
150
151 return sendmsg(rth->fd, &msg, 0);
152 }
153
154 int rtnl_dump_filter(struct rtnl_handle *rth,
155 rtnl_filter_t filter,
156 void *arg1, rtnl_filter_t junk, void *arg2)
157 {
158 struct sockaddr_nl nladdr;
159 struct iovec iov;
160 struct msghdr msg = {
161 .msg_name = &nladdr,
162 .msg_namelen = sizeof(nladdr),
163 .msg_iov = &iov,
164 .msg_iovlen = 1,
165 };
166 char buf[16384];
167
168 iov.iov_base = buf;
169 while (1) {
170 int status;
171 struct nlmsghdr *h;
172
173 iov.iov_len = sizeof(buf);
174 status = recvmsg(rth->fd, &msg, 0);
175
176 if (status < 0) {
177 if (errno == EINTR)
178 continue;
179 perror("OVERRUN");
180 continue;
181 }
182
183 if (status == 0) {
184 fprintf(stderr, "EOF on netlink\n");
185 return -1;
186 }
187
188 h = (struct nlmsghdr *)buf;
189 while (NLMSG_OK(h, status)) {
190 int err;
191
192 if (nladdr.nl_pid != 0 ||
193 h->nlmsg_pid != rth->local.nl_pid ||
194 h->nlmsg_seq != rth->dump) {
195 if (junk) {
196 err = junk(&nladdr, h, arg2);
197 if (err < 0)
198 return err;
199 }
200 goto skip_it;
201 }
202
203 if (h->nlmsg_type == NLMSG_DONE)
204 return 0;
205 if (h->nlmsg_type == NLMSG_ERROR) {
206 struct nlmsgerr *err =
207 (struct nlmsgerr *)NLMSG_DATA(h);
208 if (h->nlmsg_len <
209 NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
210 fprintf(stderr, "ERROR truncated\n");
211 } else {
212 errno = -err->error;
213 perror("RTNETLINK answers");
214 }
215 return -1;
216 }
217 err = filter(&nladdr, h, arg1);
218 if (err < 0)
219 return err;
220
221 skip_it:
222 h = NLMSG_NEXT(h, status);
223 }
224 if (msg.msg_flags & MSG_TRUNC) {
225 fprintf(stderr, "Message truncated\n");
226 continue;
227 }
228 if (status) {
229 fprintf(stderr, "!!!Remnant of size %d\n", status);
230 return -1;
231 }
232 }
233 }
234
235 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
236 unsigned groups, struct nlmsghdr *answer,
237 rtnl_filter_t junk, void *jarg)
238 {
239 int status;
240 unsigned seq;
241 struct nlmsghdr *h;
242 struct sockaddr_nl nladdr;
243 struct iovec iov = {
244 .iov_base = (void *)n,
245 .iov_len = n->nlmsg_len
246 };
247 struct msghdr msg = {
248 .msg_name = &nladdr,
249 .msg_namelen = sizeof(nladdr),
250 .msg_iov = &iov,
251 .msg_iovlen = 1,
252 };
253 char buf[16384];
254
255 memset(&nladdr, 0, sizeof(nladdr));
256 nladdr.nl_family = AF_NETLINK;
257 nladdr.nl_pid = peer;
258 nladdr.nl_groups = groups;
259
260 n->nlmsg_seq = seq = ++rtnl->seq;
261
262 if (answer == NULL)
263 n->nlmsg_flags |= NLM_F_ACK;
264
265 status = sendmsg(rtnl->fd, &msg, 0);
266
267 if (status < 0) {
268 perror("Cannot talk to rtnetlink");
269 return -1;
270 }
271
272 memset(buf, 0, sizeof(buf));
273
274 iov.iov_base = buf;
275
276 while (1) {
277 iov.iov_len = sizeof(buf);
278 status = recvmsg(rtnl->fd, &msg, 0);
279
280 if (status < 0) {
281 if (errno == EINTR)
282 continue;
283 perror("OVERRUN");
284 continue;
285 }
286 if (status == 0) {
287 fprintf(stderr, "EOF on netlink\n");
288 return -1;
289 }
290 if (msg.msg_namelen != sizeof(nladdr)) {
291 fprintf(stderr, "sender address length == %d\n",
292 msg.msg_namelen);
293 return -1;
294 }
295 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h);) {
296 int err;
297 int len = h->nlmsg_len;
298 int l = len - sizeof(*h);
299
300 if (l < 0 || len > status) {
301 if (msg.msg_flags & MSG_TRUNC) {
302 fprintf(stderr, "Truncated message\n");
303 return -1;
304 }
305 fprintf(stderr,
306 "!!!malformed message: len=%d\n", len);
307 return -1;
308 }
309
310 if (nladdr.nl_pid != peer ||
311 h->nlmsg_pid != rtnl->local.nl_pid ||
312 h->nlmsg_seq != seq) {
313 if (junk) {
314 err = junk(&nladdr, h, jarg);
315 if (err < 0)
316 return err;
317 }
318 /* Don't forget to skip that message. */
319 status -= NLMSG_ALIGN(len);
320 h = (struct nlmsghdr *)((char *)h +
321 NLMSG_ALIGN(len));
322 continue;
323 }
324
325 if (h->nlmsg_type == NLMSG_ERROR) {
326 struct nlmsgerr *err =
327 (struct nlmsgerr *)NLMSG_DATA(h);
328 if (l < sizeof(struct nlmsgerr)) {
329 fprintf(stderr, "ERROR truncated\n");
330 } else {
331 errno = -err->error;
332 if (errno == 0) {
333 if (answer)
334 memcpy(answer, h,
335 h->nlmsg_len);
336 return 0;
337 }
338 perror("RTNETLINK answers");
339 }
340 return -1;
341 }
342 if (answer) {
343 memcpy(answer, h, h->nlmsg_len);
344 return 0;
345 }
346
347 fprintf(stderr, "Unexpected reply!!!\n");
348
349 status -= NLMSG_ALIGN(len);
350 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
351 }
352 if (msg.msg_flags & MSG_TRUNC) {
353 fprintf(stderr, "Message truncated\n");
354 continue;
355 }
356 if (status) {
357 fprintf(stderr, "!!!Remnant of size %d\n", status);
358 return -1;
359 }
360 }
361 }
362
363 int rtnl_listen(struct rtnl_handle *rtnl, rtnl_filter_t handler, void *jarg)
364 {
365 int status;
366 struct nlmsghdr *h;
367 struct sockaddr_nl nladdr;
368 struct iovec iov;
369 struct msghdr msg = {
370 .msg_name = &nladdr,
371 .msg_namelen = sizeof(nladdr),
372 .msg_iov = &iov,
373 .msg_iovlen = 1,
374 };
375 char buf[8192];
376
377 memset(&nladdr, 0, sizeof(nladdr));
378 nladdr.nl_family = AF_NETLINK;
379 nladdr.nl_pid = 0;
380 nladdr.nl_groups = 0;
381
382 iov.iov_base = buf;
383 while (1) {
384 iov.iov_len = sizeof(buf);
385 status = recvmsg(rtnl->fd, &msg, 0);
386
387 if (status < 0) {
388 if (errno == EINTR)
389 continue;
390 if (errno == EAGAIN)
391 return 0;
392 perror("OVERRUN");
393 return -1;
394 }
395 if (status == 0) {
396 fprintf(stderr, "EOF on netlink\n");
397 return -1;
398 }
399 if (msg.msg_namelen != sizeof(nladdr)) {
400 fprintf(stderr, "Sender address length == %d\n",
401 msg.msg_namelen);
402 return -1;
403 }
404 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h);) {
405 int err;
406 int len = h->nlmsg_len;
407 int l = len - sizeof(*h);
408
409 if (l < 0 || len > status) {
410 if (msg.msg_flags & MSG_TRUNC) {
411 fprintf(stderr, "Truncated message\n");
412 return -1;
413 }
414 fprintf(stderr,
415 "!!!malformed message: len=%d\n", len);
416 return -1;
417 }
418
419 err = handler(&nladdr, h, jarg);
420 if (err < 0) {
421 fprintf(stderr, "Handler returned %d\n", err);
422 return err;
423 }
424
425 status -= NLMSG_ALIGN(len);
426 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
427 }
428 if (msg.msg_flags & MSG_TRUNC) {
429 fprintf(stderr, "Message truncated\n");
430 continue;
431 }
432 if (status) {
433 fprintf(stderr, "!!!Remnant of size %d\n", status);
434 return -1;
435 }
436 }
437 }
438
439 int rtnl_from_file(FILE * rtnl, rtnl_filter_t handler, void *jarg)
440 {
441 int status;
442 struct sockaddr_nl nladdr;
443 char buf[8192];
444 struct nlmsghdr *h = (void *)buf;
445
446 memset(&nladdr, 0, sizeof(nladdr));
447 nladdr.nl_family = AF_NETLINK;
448 nladdr.nl_pid = 0;
449 nladdr.nl_groups = 0;
450
451 while (1) {
452 int err, len, type;
453 int l;
454
455 status = fread(&buf, 1, sizeof(*h), rtnl);
456
457 if (status < 0) {
458 if (errno == EINTR)
459 continue;
460 perror("rtnl_from_file: fread");
461 return -1;
462 }
463 if (status == 0)
464 return 0;
465
466 len = h->nlmsg_len;
467 type = h->nlmsg_type;
468 l = len - sizeof(*h);
469
470 if (l < 0 || len > sizeof(buf)) {
471 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
472 len, ftell(rtnl));
473 return -1;
474 }
475
476 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
477
478 if (status < 0) {
479 perror("rtnl_from_file: fread");
480 return -1;
481 }
482 if (status < l) {
483 fprintf(stderr, "rtnl-from_file: truncated message\n");
484 return -1;
485 }
486
487 err = handler(&nladdr, h, jarg);
488 if (err < 0)
489 return err;
490 }
491 }
492
493 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
494 {
495 int len = RTA_LENGTH(4);
496 struct rtattr *rta;
497 if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) {
498 fprintf(stderr,
499 "addattr32: Error! max allowed bound %d exceeded\n",
500 maxlen);
501 return -1;
502 }
503 rta = NLMSG_TAIL(n);
504 rta->rta_type = type;
505 rta->rta_len = len;
506 memcpy(RTA_DATA(rta), &data, 4);
507 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
508 return 0;
509 }
510
511 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
512 int alen)
513 {
514 int len = RTA_LENGTH(alen);
515 struct rtattr *rta;
516
517 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
518 fprintf(stderr,
519 "addattr_l ERROR: message exceeded bound of %d\n",
520 maxlen);
521 return -1;
522 }
523 rta = NLMSG_TAIL(n);
524 rta->rta_type = type;
525 rta->rta_len = len;
526 memcpy(RTA_DATA(rta), data, alen);
527 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
528 return 0;
529 }
530
531 int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
532 {
533 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
534 fprintf(stderr,
535 "addraw_l ERROR: message exceeded bound of %d\n",
536 maxlen);
537 return -1;
538 }
539
540 memcpy(NLMSG_TAIL(n), data, len);
541 memset((void *)NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
542 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
543 return 0;
544 }
545
546 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
547 {
548 int len = RTA_LENGTH(4);
549 struct rtattr *subrta;
550
551 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
552 fprintf(stderr,
553 "rta_addattr32: Error! max allowed bound %d exceeded\n",
554 maxlen);
555 return -1;
556 }
557 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
558 subrta->rta_type = type;
559 subrta->rta_len = len;
560 memcpy(RTA_DATA(subrta), &data, 4);
561 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
562 return 0;
563 }
564
565 int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
566 const void *data, int alen)
567 {
568 struct rtattr *subrta;
569 int len = RTA_LENGTH(alen);
570
571 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
572 fprintf(stderr,
573 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
574 maxlen);
575 return -1;
576 }
577 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
578 subrta->rta_type = type;
579 subrta->rta_len = len;
580 memcpy(RTA_DATA(subrta), data, alen);
581 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
582 return 0;
583 }
584
585 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
586 {
587 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
588 while (RTA_OK(rta, len)) {
589 if (rta->rta_type <= max)
590 tb[rta->rta_type] = rta;
591 rta = RTA_NEXT(rta, len);
592 }
593 if (len)
594 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len,
595 rta->rta_len);
596 return 0;
597 }
598
599 int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta,
600 int len)
601 {
602 int i = 0;
603
604 memset(tb, 0, sizeof(struct rtattr *) * max);
605 while (RTA_OK(rta, len)) {
606 if (rta->rta_type <= max && i < max)
607 tb[i++] = rta;
608 rta = RTA_NEXT(rta, len);
609 }
610 if (len)
611 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len,
612 rta->rta_len);
613 return i;
614 }