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