]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/libnetlink.c
Merge branch 'master' into net-next
[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 #ifndef SOL_NETLINK
29 #define SOL_NETLINK 270
30 #endif
31
32 #ifndef MIN
33 #define MIN(a, b) ((a) < (b) ? (a) : (b))
34 #endif
35
36 int rcvbuf = 1024 * 1024;
37
38 void rtnl_close(struct rtnl_handle *rth)
39 {
40 if (rth->fd >= 0) {
41 close(rth->fd);
42 rth->fd = -1;
43 }
44 }
45
46 int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions,
47 int protocol)
48 {
49 socklen_t addr_len;
50 int sndbuf = 32768;
51
52 memset(rth, 0, sizeof(*rth));
53
54 rth->proto = protocol;
55 rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
56 if (rth->fd < 0) {
57 perror("Cannot open netlink socket");
58 return -1;
59 }
60
61 if (setsockopt(rth->fd,SOL_SOCKET,SO_SNDBUF,&sndbuf,sizeof(sndbuf)) < 0) {
62 perror("SO_SNDBUF");
63 return -1;
64 }
65
66 if (setsockopt(rth->fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf)) < 0) {
67 perror("SO_RCVBUF");
68 return -1;
69 }
70
71 memset(&rth->local, 0, sizeof(rth->local));
72 rth->local.nl_family = AF_NETLINK;
73 rth->local.nl_groups = subscriptions;
74
75 if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
76 perror("Cannot bind netlink socket");
77 return -1;
78 }
79 addr_len = sizeof(rth->local);
80 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
81 perror("Cannot getsockname");
82 return -1;
83 }
84 if (addr_len != sizeof(rth->local)) {
85 fprintf(stderr, "Wrong address length %d\n", addr_len);
86 return -1;
87 }
88 if (rth->local.nl_family != AF_NETLINK) {
89 fprintf(stderr, "Wrong address family %d\n", rth->local.nl_family);
90 return -1;
91 }
92 rth->seq = time(NULL);
93 return 0;
94 }
95
96 int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
97 {
98 return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
99 }
100
101 int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
102 {
103 return rtnl_wilddump_req_filter(rth, family, type, RTEXT_FILTER_VF);
104 }
105
106 int rtnl_wilddump_req_filter(struct rtnl_handle *rth, int family, int type,
107 __u32 filt_mask)
108 {
109 struct {
110 struct nlmsghdr nlh;
111 struct ifinfomsg ifm;
112 /* attribute has to be NLMSG aligned */
113 struct rtattr ext_req __attribute__ ((aligned(NLMSG_ALIGNTO)));
114 __u32 ext_filter_mask;
115 } req = {
116 .nlh.nlmsg_len = sizeof(req),
117 .nlh.nlmsg_type = type,
118 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
119 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
120 .ifm.ifi_family = family,
121 .ext_req.rta_type = IFLA_EXT_MASK,
122 .ext_req.rta_len = RTA_LENGTH(sizeof(__u32)),
123 .ext_filter_mask = filt_mask,
124 };
125
126 return send(rth->fd, (void*)&req, sizeof(req), 0);
127 }
128
129 int rtnl_wilddump_req_filter_fn(struct rtnl_handle *rth, int family, int type,
130 req_filter_fn_t filter_fn)
131 {
132 struct {
133 struct nlmsghdr nlh;
134 struct ifinfomsg ifm;
135 char buf[1024];
136 } req = {
137 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
138 .nlh.nlmsg_type = type,
139 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
140 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
141 .ifm.ifi_family = family,
142 };
143 int err;
144
145 if (!filter_fn)
146 return -EINVAL;
147
148 err = filter_fn(&req.nlh, sizeof(req));
149 if (err)
150 return err;
151
152 return send(rth->fd, (void*)&req, sizeof(req), 0);
153 }
154
155 int rtnl_wilddump_stats_req_filter(struct rtnl_handle *rth, int fam, int type,
156 __u32 filt_mask)
157 {
158 struct {
159 struct nlmsghdr nlh;
160 struct if_stats_msg ifsm;
161 } req;
162
163 memset(&req, 0, sizeof(req));
164 req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct if_stats_msg));
165 req.nlh.nlmsg_type = type;
166 req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
167 req.nlh.nlmsg_pid = 0;
168 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
169 req.ifsm.family = fam;
170 req.ifsm.filter_mask = filt_mask;
171
172 return send(rth->fd, (void *)&req, sizeof(req), 0);
173 }
174
175 int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
176 {
177 return send(rth->fd, buf, len, 0);
178 }
179
180 int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
181 {
182 struct nlmsghdr *h;
183 int status;
184 char resp[1024];
185
186 status = send(rth->fd, buf, len, 0);
187 if (status < 0)
188 return status;
189
190 /* Check for immediate errors */
191 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
192 if (status < 0) {
193 if (errno == EAGAIN)
194 return 0;
195 return -1;
196 }
197
198 for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status);
199 h = NLMSG_NEXT(h, status)) {
200 if (h->nlmsg_type == NLMSG_ERROR) {
201 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
202 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
203 fprintf(stderr, "ERROR truncated\n");
204 else
205 errno = -err->error;
206 return -1;
207 }
208 }
209
210 return 0;
211 }
212
213 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
214 {
215 struct nlmsghdr nlh = {
216 .nlmsg_len = NLMSG_LENGTH(len),
217 .nlmsg_type = type,
218 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
219 .nlmsg_seq = rth->dump = ++rth->seq,
220 };
221 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
222 struct iovec iov[2] = {
223 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
224 { .iov_base = req, .iov_len = len }
225 };
226 struct msghdr msg = {
227 .msg_name = &nladdr,
228 .msg_namelen = sizeof(nladdr),
229 .msg_iov = iov,
230 .msg_iovlen = 2,
231 };
232
233 return sendmsg(rth->fd, &msg, 0);
234 }
235
236 int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n)
237 {
238 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
239 struct iovec iov = {
240 .iov_base = (void*) n,
241 .iov_len = n->nlmsg_len
242 };
243 struct msghdr msg = {
244 .msg_name = &nladdr,
245 .msg_namelen = sizeof(nladdr),
246 .msg_iov = &iov,
247 .msg_iovlen = 1,
248 };
249
250 n->nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
251 n->nlmsg_pid = 0;
252 n->nlmsg_seq = rth->dump = ++rth->seq;
253
254 return sendmsg(rth->fd, &msg, 0);
255 }
256
257 int rtnl_dump_filter_l(struct rtnl_handle *rth,
258 const struct rtnl_dump_filter_arg *arg)
259 {
260 struct sockaddr_nl nladdr;
261 struct iovec iov;
262 struct msghdr msg = {
263 .msg_name = &nladdr,
264 .msg_namelen = sizeof(nladdr),
265 .msg_iov = &iov,
266 .msg_iovlen = 1,
267 };
268 char buf[32768];
269 int dump_intr = 0;
270
271 iov.iov_base = buf;
272 while (1) {
273 int status;
274 const struct rtnl_dump_filter_arg *a;
275 int found_done = 0;
276 int msglen = 0;
277
278 iov.iov_len = sizeof(buf);
279 status = recvmsg(rth->fd, &msg, 0);
280
281 if (status < 0) {
282 if (errno == EINTR || errno == EAGAIN)
283 continue;
284 fprintf(stderr, "netlink receive error %s (%d)\n",
285 strerror(errno), errno);
286 return -1;
287 }
288
289 if (status == 0) {
290 fprintf(stderr, "EOF on netlink\n");
291 return -1;
292 }
293
294 if (rth->dump_fp)
295 fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp);
296
297 for (a = arg; a->filter; a++) {
298 struct nlmsghdr *h = (struct nlmsghdr*)buf;
299 msglen = status;
300
301 while (NLMSG_OK(h, msglen)) {
302 int err = 0;
303
304 h->nlmsg_flags &= ~a->nc_flags;
305
306 if (nladdr.nl_pid != 0 ||
307 h->nlmsg_pid != rth->local.nl_pid ||
308 h->nlmsg_seq != rth->dump)
309 goto skip_it;
310
311 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
312 dump_intr = 1;
313
314 if (h->nlmsg_type == NLMSG_DONE) {
315 found_done = 1;
316 break; /* process next filter */
317 }
318 if (h->nlmsg_type == NLMSG_ERROR) {
319 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
320 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
321 fprintf(stderr,
322 "ERROR truncated\n");
323 } else {
324 errno = -err->error;
325 if (rth->proto == NETLINK_SOCK_DIAG &&
326 (errno == ENOENT ||
327 errno == EOPNOTSUPP))
328 return -1;
329
330 perror("RTNETLINK answers");
331 }
332 return -1;
333 }
334
335 if (!rth->dump_fp) {
336 err = a->filter(&nladdr, h, a->arg1);
337 if (err < 0)
338 return err;
339 }
340
341 skip_it:
342 h = NLMSG_NEXT(h, msglen);
343 }
344 }
345
346 if (found_done) {
347 if (dump_intr)
348 fprintf(stderr,
349 "Dump was interrupted and may be inconsistent.\n");
350 return 0;
351 }
352
353 if (msg.msg_flags & MSG_TRUNC) {
354 fprintf(stderr, "Message truncated\n");
355 continue;
356 }
357 if (msglen) {
358 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
359 exit(1);
360 }
361 }
362 }
363
364 int rtnl_dump_filter_nc(struct rtnl_handle *rth,
365 rtnl_filter_t filter,
366 void *arg1, __u16 nc_flags)
367 {
368 const struct rtnl_dump_filter_arg a[2] = {
369 { .filter = filter, .arg1 = arg1, .nc_flags = nc_flags, },
370 { .filter = NULL, .arg1 = NULL, .nc_flags = 0, },
371 };
372
373 return rtnl_dump_filter_l(rth, a);
374 }
375
376 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
377 struct nlmsghdr *answer, size_t maxlen)
378 {
379 int status;
380 unsigned seq;
381 struct nlmsghdr *h;
382 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
383 struct iovec iov = {
384 .iov_base = (void*) n,
385 .iov_len = n->nlmsg_len
386 };
387 struct msghdr msg = {
388 .msg_name = &nladdr,
389 .msg_namelen = sizeof(nladdr),
390 .msg_iov = &iov,
391 .msg_iovlen = 1,
392 };
393 char buf[32768] = {};
394
395 n->nlmsg_seq = seq = ++rtnl->seq;
396
397 if (answer == NULL)
398 n->nlmsg_flags |= NLM_F_ACK;
399
400 status = sendmsg(rtnl->fd, &msg, 0);
401 if (status < 0) {
402 perror("Cannot talk to rtnetlink");
403 return -1;
404 }
405
406 iov.iov_base = buf;
407 while (1) {
408 iov.iov_len = sizeof(buf);
409 status = recvmsg(rtnl->fd, &msg, 0);
410
411 if (status < 0) {
412 if (errno == EINTR || errno == EAGAIN)
413 continue;
414 fprintf(stderr, "netlink receive error %s (%d)\n",
415 strerror(errno), errno);
416 return -1;
417 }
418 if (status == 0) {
419 fprintf(stderr, "EOF on netlink\n");
420 return -1;
421 }
422 if (msg.msg_namelen != sizeof(nladdr)) {
423 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
424 exit(1);
425 }
426 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
427 int len = h->nlmsg_len;
428 int l = len - sizeof(*h);
429
430 if (l < 0 || len>status) {
431 if (msg.msg_flags & MSG_TRUNC) {
432 fprintf(stderr, "Truncated message\n");
433 return -1;
434 }
435 fprintf(stderr, "!!!malformed message: len=%d\n", len);
436 exit(1);
437 }
438
439 if (nladdr.nl_pid != 0 ||
440 h->nlmsg_pid != rtnl->local.nl_pid ||
441 h->nlmsg_seq != seq) {
442 /* Don't forget to skip that message. */
443 status -= NLMSG_ALIGN(len);
444 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
445 continue;
446 }
447
448 if (h->nlmsg_type == NLMSG_ERROR) {
449 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
450 if (l < sizeof(struct nlmsgerr)) {
451 fprintf(stderr, "ERROR truncated\n");
452 } else if (!err->error) {
453 if (answer)
454 memcpy(answer, h,
455 MIN(maxlen, h->nlmsg_len));
456 return 0;
457 }
458
459 if (rtnl->proto != NETLINK_SOCK_DIAG)
460 fprintf(stderr,
461 "RTNETLINK answers: %s\n",
462 strerror(-err->error));
463 errno = -err->error;
464 return -1;
465 }
466
467 if (answer) {
468 memcpy(answer, h,
469 MIN(maxlen, h->nlmsg_len));
470 return 0;
471 }
472
473 fprintf(stderr, "Unexpected reply!!!\n");
474
475 status -= NLMSG_ALIGN(len);
476 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
477 }
478
479 if (msg.msg_flags & MSG_TRUNC) {
480 fprintf(stderr, "Message truncated\n");
481 continue;
482 }
483
484 if (status) {
485 fprintf(stderr, "!!!Remnant of size %d\n", status);
486 exit(1);
487 }
488 }
489 }
490
491 int rtnl_listen_all_nsid(struct rtnl_handle *rth)
492 {
493 unsigned int on = 1;
494
495 if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &on,
496 sizeof(on)) < 0) {
497 perror("NETLINK_LISTEN_ALL_NSID");
498 return -1;
499 }
500 rth->flags |= RTNL_HANDLE_F_LISTEN_ALL_NSID;
501 return 0;
502 }
503
504 int rtnl_listen(struct rtnl_handle *rtnl,
505 rtnl_listen_filter_t handler,
506 void *jarg)
507 {
508 int status;
509 struct nlmsghdr *h;
510 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
511 struct iovec iov;
512 struct msghdr msg = {
513 .msg_name = &nladdr,
514 .msg_namelen = sizeof(nladdr),
515 .msg_iov = &iov,
516 .msg_iovlen = 1,
517 };
518 char buf[16384];
519 char cmsgbuf[BUFSIZ];
520
521 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
522 msg.msg_control = &cmsgbuf;
523 msg.msg_controllen = sizeof(cmsgbuf);
524 }
525
526 iov.iov_base = buf;
527 while (1) {
528 struct rtnl_ctrl_data ctrl;
529 struct cmsghdr *cmsg;
530
531 iov.iov_len = sizeof(buf);
532 status = recvmsg(rtnl->fd, &msg, 0);
533
534 if (status < 0) {
535 if (errno == EINTR || errno == EAGAIN)
536 continue;
537 fprintf(stderr, "netlink receive error %s (%d)\n",
538 strerror(errno), errno);
539 if (errno == ENOBUFS)
540 continue;
541 return -1;
542 }
543 if (status == 0) {
544 fprintf(stderr, "EOF on netlink\n");
545 return -1;
546 }
547 if (msg.msg_namelen != sizeof(nladdr)) {
548 fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
549 exit(1);
550 }
551
552 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
553 memset(&ctrl, 0, sizeof(ctrl));
554 ctrl.nsid = -1;
555 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
556 cmsg = CMSG_NXTHDR(&msg, cmsg))
557 if (cmsg->cmsg_level == SOL_NETLINK &&
558 cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID &&
559 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
560 int *data = (int *)CMSG_DATA(cmsg);
561
562 ctrl.nsid = *data;
563 }
564 }
565
566 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
567 int err;
568 int len = h->nlmsg_len;
569 int l = len - sizeof(*h);
570
571 if (l<0 || len>status) {
572 if (msg.msg_flags & MSG_TRUNC) {
573 fprintf(stderr, "Truncated message\n");
574 return -1;
575 }
576 fprintf(stderr, "!!!malformed message: len=%d\n", len);
577 exit(1);
578 }
579
580 err = handler(&nladdr, &ctrl, h, jarg);
581 if (err < 0)
582 return err;
583
584 status -= NLMSG_ALIGN(len);
585 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
586 }
587 if (msg.msg_flags & MSG_TRUNC) {
588 fprintf(stderr, "Message truncated\n");
589 continue;
590 }
591 if (status) {
592 fprintf(stderr, "!!!Remnant of size %d\n", status);
593 exit(1);
594 }
595 }
596 }
597
598 int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
599 void *jarg)
600 {
601 int status;
602 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
603 char buf[16384];
604 struct nlmsghdr *h = (void*)buf;
605
606 while (1) {
607 int err, len;
608 int l;
609
610 status = fread(&buf, 1, sizeof(*h), rtnl);
611
612 if (status < 0) {
613 if (errno == EINTR)
614 continue;
615 perror("rtnl_from_file: fread");
616 return -1;
617 }
618 if (status == 0)
619 return 0;
620
621 len = h->nlmsg_len;
622 l = len - sizeof(*h);
623
624 if (l<0 || len>sizeof(buf)) {
625 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
626 len, ftell(rtnl));
627 return -1;
628 }
629
630 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
631
632 if (status < 0) {
633 perror("rtnl_from_file: fread");
634 return -1;
635 }
636 if (status < l) {
637 fprintf(stderr, "rtnl-from_file: truncated message\n");
638 return -1;
639 }
640
641 err = handler(&nladdr, NULL, h, jarg);
642 if (err < 0)
643 return err;
644 }
645 }
646
647 int addattr(struct nlmsghdr *n, int maxlen, int type)
648 {
649 return addattr_l(n, maxlen, type, NULL, 0);
650 }
651
652 int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
653 {
654 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
655 }
656
657 int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
658 {
659 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
660 }
661
662 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
663 {
664 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
665 }
666
667 int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
668 {
669 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
670 }
671
672 int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
673 {
674 return addattr_l(n, maxlen, type, str, strlen(str)+1);
675 }
676
677 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
678 int alen)
679 {
680 int len = RTA_LENGTH(alen);
681 struct rtattr *rta;
682
683 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
684 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
685 return -1;
686 }
687 rta = NLMSG_TAIL(n);
688 rta->rta_type = type;
689 rta->rta_len = len;
690 memcpy(RTA_DATA(rta), data, alen);
691 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
692 return 0;
693 }
694
695 int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
696 {
697 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
698 fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen);
699 return -1;
700 }
701
702 memcpy(NLMSG_TAIL(n), data, len);
703 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
704 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
705 return 0;
706 }
707
708 struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
709 {
710 struct rtattr *nest = NLMSG_TAIL(n);
711
712 addattr_l(n, maxlen, type, NULL, 0);
713 return nest;
714 }
715
716 int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
717 {
718 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
719 return n->nlmsg_len;
720 }
721
722 struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
723 const void *data, int len)
724 {
725 struct rtattr *start = NLMSG_TAIL(n);
726
727 addattr_l(n, maxlen, type, data, len);
728 addattr_nest(n, maxlen, type);
729 return start;
730 }
731
732 int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
733 {
734 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
735
736 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
737 addattr_nest_end(n, nest);
738 return n->nlmsg_len;
739 }
740
741 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
742 {
743 int len = RTA_LENGTH(4);
744 struct rtattr *subrta;
745
746 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
747 fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen);
748 return -1;
749 }
750 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
751 subrta->rta_type = type;
752 subrta->rta_len = len;
753 memcpy(RTA_DATA(subrta), &data, 4);
754 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
755 return 0;
756 }
757
758 int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
759 const void *data, int alen)
760 {
761 struct rtattr *subrta;
762 int len = RTA_LENGTH(alen);
763
764 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
765 fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen);
766 return -1;
767 }
768 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
769 subrta->rta_type = type;
770 subrta->rta_len = len;
771 memcpy(RTA_DATA(subrta), data, alen);
772 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
773 return 0;
774 }
775
776 int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data)
777 {
778 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u8));
779 }
780
781 int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data)
782 {
783 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u16));
784 }
785
786 int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data)
787 {
788 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u64));
789 }
790
791 struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type)
792 {
793 struct rtattr *nest = RTA_TAIL(rta);
794
795 rta_addattr_l(rta, maxlen, type, NULL, 0);
796
797 return nest;
798 }
799
800 int rta_nest_end(struct rtattr *rta, struct rtattr *nest)
801 {
802 nest->rta_len = (void *)RTA_TAIL(rta) - (void *)nest;
803
804 return rta->rta_len;
805 }
806
807 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
808 {
809 return parse_rtattr_flags(tb, max, rta, len, 0);
810 }
811
812 int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
813 int len, unsigned short flags)
814 {
815 unsigned short type;
816
817 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
818 while (RTA_OK(rta, len)) {
819 type = rta->rta_type & ~flags;
820 if ((type <= max) && (!tb[type]))
821 tb[type] = rta;
822 rta = RTA_NEXT(rta,len);
823 }
824 if (len)
825 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
826 return 0;
827 }
828
829 int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len)
830 {
831 int i = 0;
832
833 memset(tb, 0, sizeof(struct rtattr *) * max);
834 while (RTA_OK(rta, len)) {
835 if (rta->rta_type <= max && i < max)
836 tb[i++] = rta;
837 rta = RTA_NEXT(rta,len);
838 }
839 if (len)
840 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
841 return i;
842 }
843
844 struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len)
845 {
846 while (RTA_OK(rta, len)) {
847 if (rta->rta_type == type)
848 return rta;
849 rta = RTA_NEXT(rta, len);
850 }
851 if (len)
852 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
853 return NULL;
854 }
855
856 int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta,
857 int len)
858 {
859 if (RTA_PAYLOAD(rta) < len)
860 return -1;
861 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
862 rta = RTA_DATA(rta) + RTA_ALIGN(len);
863 return parse_rtattr_nested(tb, max, rta);
864 }
865 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
866 return 0;
867 }