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