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