]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/libnetlink.c
libnetlink: Add helper to create nexthop dump request
[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 <stdbool.h>
16 #include <unistd.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 #include <linux/fib_rules.h>
26 #include <linux/if_addrlabel.h>
27 #include <linux/if_bridge.h>
28 #include <linux/nexthop.h>
29
30 #include "libnetlink.h"
31
32 #define __aligned(x) __attribute__((aligned(x)))
33
34 #ifndef SOL_NETLINK
35 #define SOL_NETLINK 270
36 #endif
37
38 #ifndef MIN
39 #define MIN(a, b) ((a) < (b) ? (a) : (b))
40 #endif
41
42 int rcvbuf = 1024 * 1024;
43
44 #ifdef HAVE_LIBMNL
45 #include <libmnl/libmnl.h>
46
47 static const enum mnl_attr_data_type extack_policy[NLMSGERR_ATTR_MAX + 1] = {
48 [NLMSGERR_ATTR_MSG] = MNL_TYPE_NUL_STRING,
49 [NLMSGERR_ATTR_OFFS] = MNL_TYPE_U32,
50 };
51
52 static int err_attr_cb(const struct nlattr *attr, void *data)
53 {
54 const struct nlattr **tb = data;
55 uint16_t type;
56
57 if (mnl_attr_type_valid(attr, NLMSGERR_ATTR_MAX) < 0) {
58 fprintf(stderr, "Invalid extack attribute\n");
59 return MNL_CB_ERROR;
60 }
61
62 type = mnl_attr_get_type(attr);
63 if (mnl_attr_validate(attr, extack_policy[type]) < 0) {
64 fprintf(stderr, "extack attribute %d failed validation\n",
65 type);
66 return MNL_CB_ERROR;
67 }
68
69 tb[type] = attr;
70 return MNL_CB_OK;
71 }
72
73 static void print_ext_ack_msg(bool is_err, const char *msg)
74 {
75 fprintf(stderr, "%s: %s", is_err ? "Error" : "Warning", msg);
76 if (msg[strlen(msg) - 1] != '.')
77 fprintf(stderr, ".");
78 fprintf(stderr, "\n");
79 }
80
81 /* dump netlink extended ack error message */
82 int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn)
83 {
84 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {};
85 const struct nlmsgerr *err = mnl_nlmsg_get_payload(nlh);
86 const struct nlmsghdr *err_nlh = NULL;
87 unsigned int hlen = sizeof(*err);
88 const char *msg = NULL;
89 uint32_t off = 0;
90
91 /* no TLVs, nothing to do here */
92 if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
93 return 0;
94
95 /* if NLM_F_CAPPED is set then the inner err msg was capped */
96 if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
97 hlen += mnl_nlmsg_get_payload_len(&err->msg);
98
99 if (mnl_attr_parse(nlh, hlen, err_attr_cb, tb) != MNL_CB_OK)
100 return 0;
101
102 if (tb[NLMSGERR_ATTR_MSG])
103 msg = mnl_attr_get_str(tb[NLMSGERR_ATTR_MSG]);
104
105 if (tb[NLMSGERR_ATTR_OFFS]) {
106 off = mnl_attr_get_u32(tb[NLMSGERR_ATTR_OFFS]);
107
108 if (off > nlh->nlmsg_len) {
109 fprintf(stderr,
110 "Invalid offset for NLMSGERR_ATTR_OFFS\n");
111 off = 0;
112 } else if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
113 err_nlh = &err->msg;
114 }
115
116 if (errfn)
117 return errfn(msg, off, err_nlh);
118
119 if (msg && *msg != '\0') {
120 bool is_err = !!err->error;
121
122 print_ext_ack_msg(is_err, msg);
123 return is_err ? 1 : 0;
124 }
125
126 return 0;
127 }
128
129 static int nl_dump_ext_ack_done(const struct nlmsghdr *nlh, int error)
130 {
131 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {};
132 unsigned int hlen = sizeof(int);
133 const char *msg = NULL;
134
135 if (mnl_attr_parse(nlh, hlen, err_attr_cb, tb) != MNL_CB_OK)
136 return 0;
137
138 if (tb[NLMSGERR_ATTR_MSG])
139 msg = mnl_attr_get_str(tb[NLMSGERR_ATTR_MSG]);
140
141 if (msg && *msg != '\0') {
142 bool is_err = !!error;
143
144 print_ext_ack_msg(is_err, msg);
145 return is_err ? 1 : 0;
146 }
147
148 return 0;
149 }
150 #else
151 #warning "libmnl required for error support"
152
153 /* No extended error ack without libmnl */
154 int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn)
155 {
156 return 0;
157 }
158
159 static int nl_dump_ext_ack_done(const struct nlmsghdr *nlh, int error)
160 {
161 return 0;
162 }
163 #endif
164
165 /* Older kernels may not support strict dump and filtering */
166 void rtnl_set_strict_dump(struct rtnl_handle *rth)
167 {
168 int one = 1;
169
170 if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_GET_STRICT_CHK,
171 &one, sizeof(one)) < 0)
172 return;
173
174 rth->flags |= RTNL_HANDLE_F_STRICT_CHK;
175 }
176
177 int rtnl_add_nl_group(struct rtnl_handle *rth, unsigned int group)
178 {
179 return setsockopt(rth->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
180 &group, sizeof(group));
181 }
182
183 void rtnl_close(struct rtnl_handle *rth)
184 {
185 if (rth->fd >= 0) {
186 close(rth->fd);
187 rth->fd = -1;
188 }
189 }
190
191 int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions,
192 int protocol)
193 {
194 socklen_t addr_len;
195 int sndbuf = 32768;
196 int one = 1;
197
198 memset(rth, 0, sizeof(*rth));
199
200 rth->proto = protocol;
201 rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
202 if (rth->fd < 0) {
203 perror("Cannot open netlink socket");
204 return -1;
205 }
206
207 if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF,
208 &sndbuf, sizeof(sndbuf)) < 0) {
209 perror("SO_SNDBUF");
210 return -1;
211 }
212
213 if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF,
214 &rcvbuf, sizeof(rcvbuf)) < 0) {
215 perror("SO_RCVBUF");
216 return -1;
217 }
218
219 /* Older kernels may no support extended ACK reporting */
220 setsockopt(rth->fd, SOL_NETLINK, NETLINK_EXT_ACK,
221 &one, sizeof(one));
222
223 memset(&rth->local, 0, sizeof(rth->local));
224 rth->local.nl_family = AF_NETLINK;
225 rth->local.nl_groups = subscriptions;
226
227 if (bind(rth->fd, (struct sockaddr *)&rth->local,
228 sizeof(rth->local)) < 0) {
229 perror("Cannot bind netlink socket");
230 return -1;
231 }
232 addr_len = sizeof(rth->local);
233 if (getsockname(rth->fd, (struct sockaddr *)&rth->local,
234 &addr_len) < 0) {
235 perror("Cannot getsockname");
236 return -1;
237 }
238 if (addr_len != sizeof(rth->local)) {
239 fprintf(stderr, "Wrong address length %d\n", addr_len);
240 return -1;
241 }
242 if (rth->local.nl_family != AF_NETLINK) {
243 fprintf(stderr, "Wrong address family %d\n",
244 rth->local.nl_family);
245 return -1;
246 }
247 rth->seq = time(NULL);
248 return 0;
249 }
250
251 int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
252 {
253 return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
254 }
255
256 int rtnl_nexthopdump_req(struct rtnl_handle *rth, int family,
257 req_filter_fn_t filter_fn)
258 {
259 struct {
260 struct nlmsghdr nlh;
261 struct nhmsg nhm;
262 char buf[128];
263 } req = {
264 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg)),
265 .nlh.nlmsg_type = RTM_GETNEXTHOP,
266 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
267 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
268 .nhm.nh_family = family,
269 };
270
271 if (filter_fn) {
272 int err;
273
274 err = filter_fn(&req.nlh, sizeof(req));
275 if (err)
276 return err;
277 }
278
279 return send(rth->fd, &req, sizeof(req), 0);
280 }
281
282 int rtnl_addrdump_req(struct rtnl_handle *rth, int family,
283 req_filter_fn_t filter_fn)
284 {
285 struct {
286 struct nlmsghdr nlh;
287 struct ifaddrmsg ifm;
288 char buf[128];
289 } req = {
290 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
291 .nlh.nlmsg_type = RTM_GETADDR,
292 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
293 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
294 .ifm.ifa_family = family,
295 };
296
297 if (filter_fn) {
298 int err;
299
300 err = filter_fn(&req.nlh, sizeof(req));
301 if (err)
302 return err;
303 }
304
305 return send(rth->fd, &req, sizeof(req), 0);
306 }
307
308 int rtnl_addrlbldump_req(struct rtnl_handle *rth, int family)
309 {
310 struct {
311 struct nlmsghdr nlh;
312 struct ifaddrlblmsg ifal;
313 } req = {
314 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg)),
315 .nlh.nlmsg_type = RTM_GETADDRLABEL,
316 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
317 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
318 .ifal.ifal_family = family,
319 };
320
321 return send(rth->fd, &req, sizeof(req), 0);
322 }
323
324 int rtnl_routedump_req(struct rtnl_handle *rth, int family,
325 req_filter_fn_t filter_fn)
326 {
327 struct {
328 struct nlmsghdr nlh;
329 struct rtmsg rtm;
330 char buf[128];
331 } req = {
332 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)),
333 .nlh.nlmsg_type = RTM_GETROUTE,
334 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
335 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
336 .rtm.rtm_family = family,
337 };
338
339 if (filter_fn) {
340 int err;
341
342 err = filter_fn(&req.nlh, sizeof(req));
343 if (err)
344 return err;
345 }
346
347 return send(rth->fd, &req, sizeof(req), 0);
348 }
349
350 int rtnl_ruledump_req(struct rtnl_handle *rth, int family)
351 {
352 struct {
353 struct nlmsghdr nlh;
354 struct fib_rule_hdr frh;
355 } req = {
356 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
357 .nlh.nlmsg_type = RTM_GETRULE,
358 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
359 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
360 .frh.family = family
361 };
362
363 return send(rth->fd, &req, sizeof(req), 0);
364 }
365
366 int rtnl_neighdump_req(struct rtnl_handle *rth, int family,
367 req_filter_fn_t filter_fn)
368 {
369 struct {
370 struct nlmsghdr nlh;
371 struct ndmsg ndm;
372 char buf[256];
373 } req = {
374 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)),
375 .nlh.nlmsg_type = RTM_GETNEIGH,
376 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
377 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
378 .ndm.ndm_family = family,
379 };
380
381 if (filter_fn) {
382 int err;
383
384 err = filter_fn(&req.nlh, sizeof(req));
385 if (err)
386 return err;
387 }
388
389 return send(rth->fd, &req, sizeof(req), 0);
390 }
391
392 int rtnl_neightbldump_req(struct rtnl_handle *rth, int family)
393 {
394 struct {
395 struct nlmsghdr nlh;
396 struct ndtmsg ndtmsg;
397 } req = {
398 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndtmsg)),
399 .nlh.nlmsg_type = RTM_GETNEIGHTBL,
400 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
401 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
402 .ndtmsg.ndtm_family = family,
403 };
404
405 return send(rth->fd, &req, sizeof(req), 0);
406 }
407
408 int rtnl_mdbdump_req(struct rtnl_handle *rth, int family)
409 {
410 struct {
411 struct nlmsghdr nlh;
412 struct br_port_msg bpm;
413 } req = {
414 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct br_port_msg)),
415 .nlh.nlmsg_type = RTM_GETMDB,
416 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
417 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
418 .bpm.family = family,
419 };
420
421 return send(rth->fd, &req, sizeof(req), 0);
422 }
423
424 int rtnl_netconfdump_req(struct rtnl_handle *rth, int family)
425 {
426 struct {
427 struct nlmsghdr nlh;
428 struct netconfmsg ncm;
429 char buf[0] __aligned(NLMSG_ALIGNTO);
430 } req = {
431 .nlh.nlmsg_len = NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct netconfmsg))),
432 .nlh.nlmsg_type = RTM_GETNETCONF,
433 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
434 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
435 .ncm.ncm_family = family,
436 };
437
438 return send(rth->fd, &req, sizeof(req), 0);
439 }
440
441 int rtnl_nsiddump_req(struct rtnl_handle *rth, int family)
442 {
443 struct {
444 struct nlmsghdr nlh;
445 struct rtgenmsg rtm;
446 char buf[0] __aligned(NLMSG_ALIGNTO);
447 } req = {
448 .nlh.nlmsg_len = NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct rtgenmsg))),
449 .nlh.nlmsg_type = RTM_GETNSID,
450 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
451 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
452 .rtm.rtgen_family = family,
453 };
454
455 return send(rth->fd, &req, sizeof(req), 0);
456 }
457
458 static int __rtnl_linkdump_req(struct rtnl_handle *rth, int family)
459 {
460 struct {
461 struct nlmsghdr nlh;
462 struct ifinfomsg ifm;
463 } req = {
464 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
465 .nlh.nlmsg_type = RTM_GETLINK,
466 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
467 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
468 .ifm.ifi_family = family,
469 };
470
471 return send(rth->fd, &req, sizeof(req), 0);
472 }
473
474 int rtnl_linkdump_req(struct rtnl_handle *rth, int family)
475 {
476 if (family == AF_UNSPEC)
477 return rtnl_linkdump_req_filter(rth, family, RTEXT_FILTER_VF);
478
479 return __rtnl_linkdump_req(rth, family);
480 }
481
482 int rtnl_linkdump_req_filter(struct rtnl_handle *rth, int family,
483 __u32 filt_mask)
484 {
485 if (family == AF_UNSPEC || family == AF_BRIDGE) {
486 struct {
487 struct nlmsghdr nlh;
488 struct ifinfomsg ifm;
489 /* attribute has to be NLMSG aligned */
490 struct rtattr ext_req __aligned(NLMSG_ALIGNTO);
491 __u32 ext_filter_mask;
492 } req = {
493 .nlh.nlmsg_len = sizeof(req),
494 .nlh.nlmsg_type = RTM_GETLINK,
495 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
496 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
497 .ifm.ifi_family = family,
498 .ext_req.rta_type = IFLA_EXT_MASK,
499 .ext_req.rta_len = RTA_LENGTH(sizeof(__u32)),
500 .ext_filter_mask = filt_mask,
501 };
502
503 return send(rth->fd, &req, sizeof(req), 0);
504 }
505
506 return __rtnl_linkdump_req(rth, family);
507 }
508
509 int rtnl_linkdump_req_filter_fn(struct rtnl_handle *rth, int family,
510 req_filter_fn_t filter_fn)
511 {
512 if (family == AF_UNSPEC || family == AF_PACKET) {
513 struct {
514 struct nlmsghdr nlh;
515 struct ifinfomsg ifm;
516 char buf[1024];
517 } req = {
518 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
519 .nlh.nlmsg_type = RTM_GETLINK,
520 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
521 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
522 .ifm.ifi_family = family,
523 };
524 int err;
525
526 if (!filter_fn)
527 return -EINVAL;
528
529 err = filter_fn(&req.nlh, sizeof(req));
530 if (err)
531 return err;
532
533 return send(rth->fd, &req, req.nlh.nlmsg_len, 0);
534 }
535
536 return __rtnl_linkdump_req(rth, family);
537 }
538
539 int rtnl_fdb_linkdump_req_filter_fn(struct rtnl_handle *rth,
540 req_filter_fn_t filter_fn)
541 {
542 struct {
543 struct nlmsghdr nlh;
544 struct ifinfomsg ifm;
545 char buf[128];
546 } req = {
547 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
548 .nlh.nlmsg_type = RTM_GETNEIGH,
549 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
550 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
551 .ifm.ifi_family = PF_BRIDGE,
552 };
553 int err;
554
555 err = filter_fn(&req.nlh, sizeof(req));
556 if (err)
557 return err;
558
559 return send(rth->fd, &req, sizeof(req), 0);
560 }
561
562 int rtnl_statsdump_req_filter(struct rtnl_handle *rth, int fam, __u32 filt_mask)
563 {
564 struct {
565 struct nlmsghdr nlh;
566 struct if_stats_msg ifsm;
567 } req;
568
569 memset(&req, 0, sizeof(req));
570 req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct if_stats_msg));
571 req.nlh.nlmsg_type = RTM_GETSTATS;
572 req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
573 req.nlh.nlmsg_pid = 0;
574 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
575 req.ifsm.family = fam;
576 req.ifsm.filter_mask = filt_mask;
577
578 return send(rth->fd, &req, sizeof(req), 0);
579 }
580
581 int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
582 {
583 return send(rth->fd, buf, len, 0);
584 }
585
586 int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
587 {
588 struct nlmsghdr *h;
589 int status;
590 char resp[1024];
591
592 status = send(rth->fd, buf, len, 0);
593 if (status < 0)
594 return status;
595
596 /* Check for immediate errors */
597 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
598 if (status < 0) {
599 if (errno == EAGAIN)
600 return 0;
601 return -1;
602 }
603
604 for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status);
605 h = NLMSG_NEXT(h, status)) {
606 if (h->nlmsg_type == NLMSG_ERROR) {
607 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
608
609 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
610 fprintf(stderr, "ERROR truncated\n");
611 else
612 errno = -err->error;
613 return -1;
614 }
615 }
616
617 return 0;
618 }
619
620 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
621 {
622 struct nlmsghdr nlh = {
623 .nlmsg_len = NLMSG_LENGTH(len),
624 .nlmsg_type = type,
625 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
626 .nlmsg_seq = rth->dump = ++rth->seq,
627 };
628 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
629 struct iovec iov[2] = {
630 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
631 { .iov_base = req, .iov_len = len }
632 };
633 struct msghdr msg = {
634 .msg_name = &nladdr,
635 .msg_namelen = sizeof(nladdr),
636 .msg_iov = iov,
637 .msg_iovlen = 2,
638 };
639
640 return sendmsg(rth->fd, &msg, 0);
641 }
642
643 int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n)
644 {
645 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
646 struct iovec iov = {
647 .iov_base = n,
648 .iov_len = n->nlmsg_len
649 };
650 struct msghdr msg = {
651 .msg_name = &nladdr,
652 .msg_namelen = sizeof(nladdr),
653 .msg_iov = &iov,
654 .msg_iovlen = 1,
655 };
656
657 n->nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
658 n->nlmsg_pid = 0;
659 n->nlmsg_seq = rth->dump = ++rth->seq;
660
661 return sendmsg(rth->fd, &msg, 0);
662 }
663
664 static int rtnl_dump_done(struct nlmsghdr *h)
665 {
666 int len = *(int *)NLMSG_DATA(h);
667
668 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(int))) {
669 fprintf(stderr, "DONE truncated\n");
670 return -1;
671 }
672
673 if (len < 0) {
674 /* check for any messages returned from kernel */
675 if (nl_dump_ext_ack_done(h, len))
676 return len;
677
678 errno = -len;
679 switch (errno) {
680 case ENOENT:
681 case EOPNOTSUPP:
682 return -1;
683 case EMSGSIZE:
684 fprintf(stderr,
685 "Error: Buffer too small for object.\n");
686 break;
687 default:
688 perror("RTNETLINK answers");
689 }
690 return len;
691 }
692
693 /* check for any messages returned from kernel */
694 nl_dump_ext_ack(h, NULL);
695
696 return 0;
697 }
698
699 static void rtnl_dump_error(const struct rtnl_handle *rth,
700 struct nlmsghdr *h)
701 {
702
703 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
704 fprintf(stderr, "ERROR truncated\n");
705 } else {
706 const struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
707
708 errno = -err->error;
709 if (rth->proto == NETLINK_SOCK_DIAG &&
710 (errno == ENOENT ||
711 errno == EOPNOTSUPP))
712 return;
713
714 if (!(rth->flags & RTNL_HANDLE_F_SUPPRESS_NLERR))
715 perror("RTNETLINK answers");
716 }
717 }
718
719 static int __rtnl_recvmsg(int fd, struct msghdr *msg, int flags)
720 {
721 int len;
722
723 do {
724 len = recvmsg(fd, msg, flags);
725 } while (len < 0 && (errno == EINTR || errno == EAGAIN));
726
727 if (len < 0) {
728 fprintf(stderr, "netlink receive error %s (%d)\n",
729 strerror(errno), errno);
730 return -errno;
731 }
732
733 if (len == 0) {
734 fprintf(stderr, "EOF on netlink\n");
735 return -ENODATA;
736 }
737
738 return len;
739 }
740
741 static int rtnl_recvmsg(int fd, struct msghdr *msg, char **answer)
742 {
743 struct iovec *iov = msg->msg_iov;
744 char *buf;
745 int len;
746
747 iov->iov_base = NULL;
748 iov->iov_len = 0;
749
750 len = __rtnl_recvmsg(fd, msg, MSG_PEEK | MSG_TRUNC);
751 if (len < 0)
752 return len;
753
754 if (len < 32768)
755 len = 32768;
756 buf = malloc(len);
757 if (!buf) {
758 fprintf(stderr, "malloc error: not enough buffer\n");
759 return -ENOMEM;
760 }
761
762 iov->iov_base = buf;
763 iov->iov_len = len;
764
765 len = __rtnl_recvmsg(fd, msg, 0);
766 if (len < 0) {
767 free(buf);
768 return len;
769 }
770
771 if (answer)
772 *answer = buf;
773 else
774 free(buf);
775
776 return len;
777 }
778
779 static int rtnl_dump_filter_l(struct rtnl_handle *rth,
780 const struct rtnl_dump_filter_arg *arg)
781 {
782 struct sockaddr_nl nladdr;
783 struct iovec iov;
784 struct msghdr msg = {
785 .msg_name = &nladdr,
786 .msg_namelen = sizeof(nladdr),
787 .msg_iov = &iov,
788 .msg_iovlen = 1,
789 };
790 char *buf;
791 int dump_intr = 0;
792
793 while (1) {
794 int status;
795 const struct rtnl_dump_filter_arg *a;
796 int found_done = 0;
797 int msglen = 0;
798
799 status = rtnl_recvmsg(rth->fd, &msg, &buf);
800 if (status < 0)
801 return status;
802
803 if (rth->dump_fp)
804 fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp);
805
806 for (a = arg; a->filter; a++) {
807 struct nlmsghdr *h = (struct nlmsghdr *)buf;
808
809 msglen = status;
810
811 while (NLMSG_OK(h, msglen)) {
812 int err = 0;
813
814 h->nlmsg_flags &= ~a->nc_flags;
815
816 if (nladdr.nl_pid != 0 ||
817 h->nlmsg_pid != rth->local.nl_pid ||
818 h->nlmsg_seq != rth->dump)
819 goto skip_it;
820
821 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
822 dump_intr = 1;
823
824 if (h->nlmsg_type == NLMSG_DONE) {
825 err = rtnl_dump_done(h);
826 if (err < 0) {
827 free(buf);
828 return -1;
829 }
830
831 found_done = 1;
832 break; /* process next filter */
833 }
834
835 if (h->nlmsg_type == NLMSG_ERROR) {
836 rtnl_dump_error(rth, h);
837 free(buf);
838 return -1;
839 }
840
841 if (!rth->dump_fp) {
842 err = a->filter(h, a->arg1);
843 if (err < 0) {
844 free(buf);
845 return err;
846 }
847 }
848
849 skip_it:
850 h = NLMSG_NEXT(h, msglen);
851 }
852 }
853 free(buf);
854
855 if (found_done) {
856 if (dump_intr)
857 fprintf(stderr,
858 "Dump was interrupted and may be inconsistent.\n");
859 return 0;
860 }
861
862 if (msg.msg_flags & MSG_TRUNC) {
863 fprintf(stderr, "Message truncated\n");
864 continue;
865 }
866 if (msglen) {
867 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
868 exit(1);
869 }
870 }
871 }
872
873 int rtnl_dump_filter_nc(struct rtnl_handle *rth,
874 rtnl_filter_t filter,
875 void *arg1, __u16 nc_flags)
876 {
877 const struct rtnl_dump_filter_arg a[2] = {
878 { .filter = filter, .arg1 = arg1, .nc_flags = nc_flags, },
879 { .filter = NULL, .arg1 = NULL, .nc_flags = 0, },
880 };
881
882 return rtnl_dump_filter_l(rth, a);
883 }
884
885 static void rtnl_talk_error(struct nlmsghdr *h, struct nlmsgerr *err,
886 nl_ext_ack_fn_t errfn)
887 {
888 if (nl_dump_ext_ack(h, errfn))
889 return;
890
891 fprintf(stderr, "RTNETLINK answers: %s\n",
892 strerror(-err->error));
893 }
894
895
896 static int __rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iov,
897 size_t iovlen, struct nlmsghdr **answer,
898 bool show_rtnl_err, nl_ext_ack_fn_t errfn)
899 {
900 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
901 struct iovec riov;
902 struct msghdr msg = {
903 .msg_name = &nladdr,
904 .msg_namelen = sizeof(nladdr),
905 .msg_iov = iov,
906 .msg_iovlen = iovlen,
907 };
908 unsigned int seq = 0;
909 struct nlmsghdr *h;
910 int i, status;
911 char *buf;
912
913 for (i = 0; i < iovlen; i++) {
914 h = iov[i].iov_base;
915 h->nlmsg_seq = seq = ++rtnl->seq;
916 if (answer == NULL)
917 h->nlmsg_flags |= NLM_F_ACK;
918 }
919
920 status = sendmsg(rtnl->fd, &msg, 0);
921 if (status < 0) {
922 perror("Cannot talk to rtnetlink");
923 return -1;
924 }
925
926 /* change msg to use the response iov */
927 msg.msg_iov = &riov;
928 msg.msg_iovlen = 1;
929 i = 0;
930 while (1) {
931 next:
932 status = rtnl_recvmsg(rtnl->fd, &msg, &buf);
933 ++i;
934
935 if (status < 0)
936 return status;
937
938 if (msg.msg_namelen != sizeof(nladdr)) {
939 fprintf(stderr,
940 "sender address length == %d\n",
941 msg.msg_namelen);
942 exit(1);
943 }
944 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
945 int len = h->nlmsg_len;
946 int l = len - sizeof(*h);
947
948 if (l < 0 || len > status) {
949 if (msg.msg_flags & MSG_TRUNC) {
950 fprintf(stderr, "Truncated message\n");
951 free(buf);
952 return -1;
953 }
954 fprintf(stderr,
955 "!!!malformed message: len=%d\n",
956 len);
957 exit(1);
958 }
959
960 if (nladdr.nl_pid != 0 ||
961 h->nlmsg_pid != rtnl->local.nl_pid ||
962 h->nlmsg_seq > seq || h->nlmsg_seq < seq - iovlen) {
963 /* Don't forget to skip that message. */
964 status -= NLMSG_ALIGN(len);
965 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
966 continue;
967 }
968
969 if (h->nlmsg_type == NLMSG_ERROR) {
970 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
971 int error = err->error;
972
973 if (l < sizeof(struct nlmsgerr)) {
974 fprintf(stderr, "ERROR truncated\n");
975 free(buf);
976 return -1;
977 }
978
979 if (!error) {
980 /* check messages from kernel */
981 nl_dump_ext_ack(h, errfn);
982 } else {
983 errno = -error;
984
985 if (rtnl->proto != NETLINK_SOCK_DIAG &&
986 show_rtnl_err)
987 rtnl_talk_error(h, err, errfn);
988 }
989
990 if (answer)
991 *answer = (struct nlmsghdr *)buf;
992 else
993 free(buf);
994
995 if (i < iovlen)
996 goto next;
997 return error ? -i : 0;
998 }
999
1000 if (answer) {
1001 *answer = (struct nlmsghdr *)buf;
1002 return 0;
1003 }
1004
1005 fprintf(stderr, "Unexpected reply!!!\n");
1006
1007 status -= NLMSG_ALIGN(len);
1008 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
1009 }
1010 free(buf);
1011
1012 if (msg.msg_flags & MSG_TRUNC) {
1013 fprintf(stderr, "Message truncated\n");
1014 continue;
1015 }
1016
1017 if (status) {
1018 fprintf(stderr, "!!!Remnant of size %d\n", status);
1019 exit(1);
1020 }
1021 }
1022 }
1023
1024 static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
1025 struct nlmsghdr **answer,
1026 bool show_rtnl_err, nl_ext_ack_fn_t errfn)
1027 {
1028 struct iovec iov = {
1029 .iov_base = n,
1030 .iov_len = n->nlmsg_len
1031 };
1032
1033 return __rtnl_talk_iov(rtnl, &iov, 1, answer, show_rtnl_err, errfn);
1034 }
1035
1036 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
1037 struct nlmsghdr **answer)
1038 {
1039 return __rtnl_talk(rtnl, n, answer, true, NULL);
1040 }
1041
1042 int rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iovec, size_t iovlen,
1043 struct nlmsghdr **answer)
1044 {
1045 return __rtnl_talk_iov(rtnl, iovec, iovlen, answer, true, NULL);
1046 }
1047
1048 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n,
1049 struct nlmsghdr **answer)
1050 {
1051 return __rtnl_talk(rtnl, n, answer, false, NULL);
1052 }
1053
1054 int rtnl_listen_all_nsid(struct rtnl_handle *rth)
1055 {
1056 unsigned int on = 1;
1057
1058 if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &on,
1059 sizeof(on)) < 0) {
1060 perror("NETLINK_LISTEN_ALL_NSID");
1061 return -1;
1062 }
1063 rth->flags |= RTNL_HANDLE_F_LISTEN_ALL_NSID;
1064 return 0;
1065 }
1066
1067 int rtnl_listen(struct rtnl_handle *rtnl,
1068 rtnl_listen_filter_t handler,
1069 void *jarg)
1070 {
1071 int status;
1072 struct nlmsghdr *h;
1073 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
1074 struct iovec iov;
1075 struct msghdr msg = {
1076 .msg_name = &nladdr,
1077 .msg_namelen = sizeof(nladdr),
1078 .msg_iov = &iov,
1079 .msg_iovlen = 1,
1080 };
1081 char buf[16384];
1082 char cmsgbuf[BUFSIZ];
1083
1084 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
1085 msg.msg_control = &cmsgbuf;
1086 msg.msg_controllen = sizeof(cmsgbuf);
1087 }
1088
1089 iov.iov_base = buf;
1090 while (1) {
1091 struct rtnl_ctrl_data ctrl;
1092 struct cmsghdr *cmsg;
1093
1094 iov.iov_len = sizeof(buf);
1095 status = recvmsg(rtnl->fd, &msg, 0);
1096
1097 if (status < 0) {
1098 if (errno == EINTR || errno == EAGAIN)
1099 continue;
1100 fprintf(stderr, "netlink receive error %s (%d)\n",
1101 strerror(errno), errno);
1102 if (errno == ENOBUFS)
1103 continue;
1104 return -1;
1105 }
1106 if (status == 0) {
1107 fprintf(stderr, "EOF on netlink\n");
1108 return -1;
1109 }
1110 if (msg.msg_namelen != sizeof(nladdr)) {
1111 fprintf(stderr,
1112 "Sender address length == %d\n",
1113 msg.msg_namelen);
1114 exit(1);
1115 }
1116
1117 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
1118 memset(&ctrl, 0, sizeof(ctrl));
1119 ctrl.nsid = -1;
1120 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1121 cmsg = CMSG_NXTHDR(&msg, cmsg))
1122 if (cmsg->cmsg_level == SOL_NETLINK &&
1123 cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID &&
1124 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
1125 int *data = (int *)CMSG_DATA(cmsg);
1126
1127 ctrl.nsid = *data;
1128 }
1129 }
1130
1131 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
1132 int err;
1133 int len = h->nlmsg_len;
1134 int l = len - sizeof(*h);
1135
1136 if (l < 0 || len > status) {
1137 if (msg.msg_flags & MSG_TRUNC) {
1138 fprintf(stderr, "Truncated message\n");
1139 return -1;
1140 }
1141 fprintf(stderr,
1142 "!!!malformed message: len=%d\n",
1143 len);
1144 exit(1);
1145 }
1146
1147 err = handler(&ctrl, h, jarg);
1148 if (err < 0)
1149 return err;
1150
1151 status -= NLMSG_ALIGN(len);
1152 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
1153 }
1154 if (msg.msg_flags & MSG_TRUNC) {
1155 fprintf(stderr, "Message truncated\n");
1156 continue;
1157 }
1158 if (status) {
1159 fprintf(stderr, "!!!Remnant of size %d\n", status);
1160 exit(1);
1161 }
1162 }
1163 }
1164
1165 int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
1166 void *jarg)
1167 {
1168 int status;
1169 char buf[16384];
1170 struct nlmsghdr *h = (struct nlmsghdr *)buf;
1171
1172 while (1) {
1173 int err, len;
1174 int l;
1175
1176 status = fread(&buf, 1, sizeof(*h), rtnl);
1177
1178 if (status < 0) {
1179 if (errno == EINTR)
1180 continue;
1181 perror("rtnl_from_file: fread");
1182 return -1;
1183 }
1184 if (status == 0)
1185 return 0;
1186
1187 len = h->nlmsg_len;
1188 l = len - sizeof(*h);
1189
1190 if (l < 0 || len > sizeof(buf)) {
1191 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
1192 len, ftell(rtnl));
1193 return -1;
1194 }
1195
1196 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
1197
1198 if (status < 0) {
1199 perror("rtnl_from_file: fread");
1200 return -1;
1201 }
1202 if (status < l) {
1203 fprintf(stderr, "rtnl-from_file: truncated message\n");
1204 return -1;
1205 }
1206
1207 err = handler(NULL, h, jarg);
1208 if (err < 0)
1209 return err;
1210 }
1211 }
1212
1213 int addattr(struct nlmsghdr *n, int maxlen, int type)
1214 {
1215 return addattr_l(n, maxlen, type, NULL, 0);
1216 }
1217
1218 int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
1219 {
1220 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
1221 }
1222
1223 int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
1224 {
1225 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
1226 }
1227
1228 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
1229 {
1230 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
1231 }
1232
1233 int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
1234 {
1235 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
1236 }
1237
1238 int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
1239 {
1240 return addattr_l(n, maxlen, type, str, strlen(str)+1);
1241 }
1242
1243 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
1244 int alen)
1245 {
1246 int len = RTA_LENGTH(alen);
1247 struct rtattr *rta;
1248
1249 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
1250 fprintf(stderr,
1251 "addattr_l ERROR: message exceeded bound of %d\n",
1252 maxlen);
1253 return -1;
1254 }
1255 rta = NLMSG_TAIL(n);
1256 rta->rta_type = type;
1257 rta->rta_len = len;
1258 if (alen)
1259 memcpy(RTA_DATA(rta), data, alen);
1260 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
1261 return 0;
1262 }
1263
1264 int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
1265 {
1266 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
1267 fprintf(stderr,
1268 "addraw_l ERROR: message exceeded bound of %d\n",
1269 maxlen);
1270 return -1;
1271 }
1272
1273 memcpy(NLMSG_TAIL(n), data, len);
1274 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
1275 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
1276 return 0;
1277 }
1278
1279 struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
1280 {
1281 struct rtattr *nest = NLMSG_TAIL(n);
1282
1283 addattr_l(n, maxlen, type, NULL, 0);
1284 return nest;
1285 }
1286
1287 int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
1288 {
1289 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
1290 return n->nlmsg_len;
1291 }
1292
1293 struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
1294 const void *data, int len)
1295 {
1296 struct rtattr *start = NLMSG_TAIL(n);
1297
1298 addattr_l(n, maxlen, type, data, len);
1299 addattr_nest(n, maxlen, type);
1300 return start;
1301 }
1302
1303 int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
1304 {
1305 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
1306
1307 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
1308 addattr_nest_end(n, nest);
1309 return n->nlmsg_len;
1310 }
1311
1312 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
1313 {
1314 int len = RTA_LENGTH(4);
1315 struct rtattr *subrta;
1316
1317 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
1318 fprintf(stderr,
1319 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1320 maxlen);
1321 return -1;
1322 }
1323 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
1324 subrta->rta_type = type;
1325 subrta->rta_len = len;
1326 memcpy(RTA_DATA(subrta), &data, 4);
1327 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
1328 return 0;
1329 }
1330
1331 int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
1332 const void *data, int alen)
1333 {
1334 struct rtattr *subrta;
1335 int len = RTA_LENGTH(alen);
1336
1337 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
1338 fprintf(stderr,
1339 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1340 maxlen);
1341 return -1;
1342 }
1343 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
1344 subrta->rta_type = type;
1345 subrta->rta_len = len;
1346 if (alen)
1347 memcpy(RTA_DATA(subrta), data, alen);
1348 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
1349 return 0;
1350 }
1351
1352 int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data)
1353 {
1354 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u8));
1355 }
1356
1357 int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data)
1358 {
1359 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u16));
1360 }
1361
1362 int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data)
1363 {
1364 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u64));
1365 }
1366
1367 struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type)
1368 {
1369 struct rtattr *nest = RTA_TAIL(rta);
1370
1371 rta_addattr_l(rta, maxlen, type, NULL, 0);
1372 nest->rta_type |= NLA_F_NESTED;
1373
1374 return nest;
1375 }
1376
1377 int rta_nest_end(struct rtattr *rta, struct rtattr *nest)
1378 {
1379 nest->rta_len = (void *)RTA_TAIL(rta) - (void *)nest;
1380
1381 return rta->rta_len;
1382 }
1383
1384 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
1385 {
1386 return parse_rtattr_flags(tb, max, rta, len, 0);
1387 }
1388
1389 int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
1390 int len, unsigned short flags)
1391 {
1392 unsigned short type;
1393
1394 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
1395 while (RTA_OK(rta, len)) {
1396 type = rta->rta_type & ~flags;
1397 if ((type <= max) && (!tb[type]))
1398 tb[type] = rta;
1399 rta = RTA_NEXT(rta, len);
1400 }
1401 if (len)
1402 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1403 len, rta->rta_len);
1404 return 0;
1405 }
1406
1407 struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len)
1408 {
1409 while (RTA_OK(rta, len)) {
1410 if (rta->rta_type == type)
1411 return rta;
1412 rta = RTA_NEXT(rta, len);
1413 }
1414
1415 if (len)
1416 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1417 len, rta->rta_len);
1418 return NULL;
1419 }
1420
1421 int __parse_rtattr_nested_compat(struct rtattr *tb[], int max,
1422 struct rtattr *rta,
1423 int len)
1424 {
1425 if (RTA_PAYLOAD(rta) < len)
1426 return -1;
1427 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
1428 rta = RTA_DATA(rta) + RTA_ALIGN(len);
1429 return parse_rtattr_nested(tb, max, rta);
1430 }
1431 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
1432 return 0;
1433 }