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