]> git.proxmox.com Git - mirror_iproute2.git/blame - lib/libnetlink.c
examples: Remove dhcp-client-script
[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
56eeeda9 403int rtnl_statsdump_req_filter(struct rtnl_handle *rth, int fam, __u32 filt_mask)
7abf5de6
NA
404{
405 struct {
406 struct nlmsghdr nlh;
407 struct if_stats_msg ifsm;
408 } req;
409
410 memset(&req, 0, sizeof(req));
411 req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct if_stats_msg));
56eeeda9 412 req.nlh.nlmsg_type = RTM_GETSTATS;
7abf5de6
NA
413 req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
414 req.nlh.nlmsg_pid = 0;
415 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
416 req.ifsm.family = fam;
417 req.ifsm.filter_mask = filt_mask;
418
2c500a4d 419 return send(rth->fd, &req, sizeof(req), 0);
7abf5de6
NA
420}
421
6cf8398f 422int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
aba5acdf 423{
f31a37f7
SH
424 return send(rth->fd, buf, len, 0);
425}
426
6cf8398f 427int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
f31a37f7 428{
54bb35c6
SH
429 struct nlmsghdr *h;
430 int status;
431 char resp[1024];
aba5acdf 432
f31a37f7 433 status = send(rth->fd, buf, len, 0);
54bb35c6
SH
434 if (status < 0)
435 return status;
436
2d8240f8
SH
437 /* Check for immediate errors */
438 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
54bb35c6
SH
439 if (status < 0) {
440 if (errno == EAGAIN)
441 return 0;
442 return -1;
443 }
444
445 for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status);
446 h = NLMSG_NEXT(h, status)) {
447 if (h->nlmsg_type == NLMSG_ERROR) {
2c500a4d
SH
448 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
449
54bb35c6
SH
450 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
451 fprintf(stderr, "ERROR truncated\n");
e9e9365b 452 else
54bb35c6 453 errno = -err->error;
24f38182 454 return -1;
54bb35c6 455 }
54bb35c6
SH
456 }
457
458 return 0;
aba5acdf
SH
459}
460
461int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
462{
d17b136f
PS
463 struct nlmsghdr nlh = {
464 .nlmsg_len = NLMSG_LENGTH(len),
465 .nlmsg_type = type,
466 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
467 .nlmsg_seq = rth->dump = ++rth->seq,
468 };
6cf8398f 469 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
8ed63ab1
SH
470 struct iovec iov[2] = {
471 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
472 { .iov_base = req, .iov_len = len }
473 };
aba5acdf 474 struct msghdr msg = {
8ed63ab1 475 .msg_name = &nladdr,
d17b136f 476 .msg_namelen = sizeof(nladdr),
8ed63ab1
SH
477 .msg_iov = iov,
478 .msg_iovlen = 2,
aba5acdf
SH
479 };
480
aba5acdf
SH
481 return sendmsg(rth->fd, &msg, 0);
482}
483
0d238ca2
DA
484int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n)
485{
486 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
487 struct iovec iov = {
2c500a4d 488 .iov_base = n,
0d238ca2
DA
489 .iov_len = n->nlmsg_len
490 };
491 struct msghdr msg = {
492 .msg_name = &nladdr,
493 .msg_namelen = sizeof(nladdr),
494 .msg_iov = &iov,
495 .msg_iovlen = 1,
496 };
497
498 n->nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
499 n->nlmsg_pid = 0;
500 n->nlmsg_seq = rth->dump = ++rth->seq;
501
502 return sendmsg(rth->fd, &msg, 0);
503}
504
0efa6257 505static int rtnl_dump_done(struct nlmsghdr *h)
892a25e2
SH
506{
507 int len = *(int *)NLMSG_DATA(h);
508
05a14fc1
DA
509 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(int))) {
510 fprintf(stderr, "DONE truncated\n");
511 return -1;
512 }
892a25e2 513
05a14fc1
DA
514 if (len < 0) {
515 errno = -len;
516 switch (errno) {
517 case ENOENT:
518 case EOPNOTSUPP:
519 return -1;
520 case EMSGSIZE:
521 fprintf(stderr,
522 "Error: Buffer too small for object.\n");
523 break;
524 default:
892a25e2 525 perror("RTNETLINK answers");
892a25e2 526 }
05a14fc1 527 return len;
892a25e2 528 }
05a14fc1 529
844c37b4
DA
530 /* check for any messages returned from kernel */
531 nl_dump_ext_ack(h, NULL);
532
892a25e2
SH
533 return 0;
534}
535
536static void rtnl_dump_error(const struct rtnl_handle *rth,
537 struct nlmsghdr *h)
538{
539
540 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
541 fprintf(stderr, "ERROR truncated\n");
542 } else {
543 const struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
544
545 errno = -err->error;
546 if (rth->proto == NETLINK_SOCK_DIAG &&
547 (errno == ENOENT ||
548 errno == EOPNOTSUPP))
549 return;
550
3ad6d176
DA
551 if (!(rth->flags & RTNL_HANDLE_F_SUPPRESS_NLERR))
552 perror("RTNETLINK answers");
892a25e2
SH
553 }
554}
555
2d34851c
HL
556static int __rtnl_recvmsg(int fd, struct msghdr *msg, int flags)
557{
558 int len;
559
560 do {
561 len = recvmsg(fd, msg, flags);
562 } while (len < 0 && (errno == EINTR || errno == EAGAIN));
563
564 if (len < 0) {
565 fprintf(stderr, "netlink receive error %s (%d)\n",
566 strerror(errno), errno);
567 return -errno;
568 }
569
570 if (len == 0) {
571 fprintf(stderr, "EOF on netlink\n");
572 return -ENODATA;
573 }
574
575 return len;
576}
577
578static int rtnl_recvmsg(int fd, struct msghdr *msg, char **answer)
579{
580 struct iovec *iov = msg->msg_iov;
581 char *buf;
582 int len;
583
584 iov->iov_base = NULL;
585 iov->iov_len = 0;
586
587 len = __rtnl_recvmsg(fd, msg, MSG_PEEK | MSG_TRUNC);
588 if (len < 0)
589 return len;
590
591 buf = malloc(len);
592 if (!buf) {
593 fprintf(stderr, "malloc error: not enough buffer\n");
594 return -ENOMEM;
595 }
596
597 iov->iov_base = buf;
598 iov->iov_len = len;
599
600 len = __rtnl_recvmsg(fd, msg, 0);
601 if (len < 0) {
602 free(buf);
603 return len;
604 }
605
606 if (answer)
607 *answer = buf;
608 else
609 free(buf);
610
611 return len;
612}
613
1d2fac41
SH
614static int rtnl_dump_filter_l(struct rtnl_handle *rth,
615 const struct rtnl_dump_filter_arg *arg)
aba5acdf 616{
aba5acdf 617 struct sockaddr_nl nladdr;
8ed63ab1
SH
618 struct iovec iov;
619 struct msghdr msg = {
620 .msg_name = &nladdr,
621 .msg_namelen = sizeof(nladdr),
622 .msg_iov = &iov,
623 .msg_iovlen = 1,
624 };
2d34851c 625 char *buf;
16f02e14 626 int dump_intr = 0;
aba5acdf
SH
627
628 while (1) {
629 int status;
b49240ec 630 const struct rtnl_dump_filter_arg *a;
3bc1c4f2
BG
631 int found_done = 0;
632 int msglen = 0;
aba5acdf 633
2d34851c
HL
634 status = rtnl_recvmsg(rth->fd, &msg, &buf);
635 if (status < 0)
636 return status;
aba5acdf 637
486ccd99
VK
638 if (rth->dump_fp)
639 fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp);
640
b49240ec 641 for (a = arg; a->filter; a++) {
2c500a4d
SH
642 struct nlmsghdr *h = (struct nlmsghdr *)buf;
643
3bc1c4f2 644 msglen = status;
b49240ec 645
3bc1c4f2 646 while (NLMSG_OK(h, msglen)) {
486ccd99 647 int err = 0;
b49240ec 648
8e72880f
PS
649 h->nlmsg_flags &= ~a->nc_flags;
650
b49240ec
SH
651 if (nladdr.nl_pid != 0 ||
652 h->nlmsg_pid != rth->local.nl_pid ||
cd70f3f5 653 h->nlmsg_seq != rth->dump)
b49240ec 654 goto skip_it;
aba5acdf 655
16f02e14
ND
656 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
657 dump_intr = 1;
658
3bc1c4f2 659 if (h->nlmsg_type == NLMSG_DONE) {
0efa6257 660 err = rtnl_dump_done(h);
2d34851c
HL
661 if (err < 0) {
662 free(buf);
892a25e2 663 return -1;
2d34851c 664 }
892a25e2 665
3bc1c4f2
BG
666 found_done = 1;
667 break; /* process next filter */
668 }
892a25e2 669
b49240ec 670 if (h->nlmsg_type == NLMSG_ERROR) {
892a25e2 671 rtnl_dump_error(rth, h);
2d34851c 672 free(buf);
b49240ec 673 return -1;
aba5acdf 674 }
486ccd99
VK
675
676 if (!rth->dump_fp) {
cd554f2c 677 err = a->filter(h, a->arg1);
2d34851c
HL
678 if (err < 0) {
679 free(buf);
486ccd99 680 return err;
2d34851c 681 }
486ccd99 682 }
aba5acdf
SH
683
684skip_it:
3bc1c4f2 685 h = NLMSG_NEXT(h, msglen);
b49240ec 686 }
3bc1c4f2 687 }
2d34851c 688 free(buf);
3bc1c4f2 689
16f02e14
ND
690 if (found_done) {
691 if (dump_intr)
692 fprintf(stderr,
693 "Dump was interrupted and may be inconsistent.\n");
3bc1c4f2 694 return 0;
16f02e14 695 }
3bc1c4f2 696
aba5acdf
SH
697 if (msg.msg_flags & MSG_TRUNC) {
698 fprintf(stderr, "Message truncated\n");
699 continue;
700 }
3bc1c4f2
BG
701 if (msglen) {
702 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
aba5acdf
SH
703 exit(1);
704 }
705 }
706}
707
8e72880f 708int rtnl_dump_filter_nc(struct rtnl_handle *rth,
b49240ec 709 rtnl_filter_t filter,
8e72880f 710 void *arg1, __u16 nc_flags)
b49240ec
SH
711{
712 const struct rtnl_dump_filter_arg a[2] = {
8e72880f
PS
713 { .filter = filter, .arg1 = arg1, .nc_flags = nc_flags, },
714 { .filter = NULL, .arg1 = NULL, .nc_flags = 0, },
b49240ec
SH
715 };
716
717 return rtnl_dump_filter_l(rth, a);
718}
719
b6432e68
SH
720static void rtnl_talk_error(struct nlmsghdr *h, struct nlmsgerr *err,
721 nl_ext_ack_fn_t errfn)
722{
844c37b4 723 if (nl_dump_ext_ack(h, errfn))
b6432e68
SH
724 return;
725
726 fprintf(stderr, "RTNETLINK answers: %s\n",
727 strerror(-err->error));
728}
729
72a2ff39
CM
730
731static int __rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iov,
732 size_t iovlen, struct nlmsghdr **answer,
733 bool show_rtnl_err, nl_ext_ack_fn_t errfn)
aba5acdf 734{
d17b136f 735 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
72a2ff39 736 struct iovec riov;
aba5acdf 737 struct msghdr msg = {
8ed63ab1
SH
738 .msg_name = &nladdr,
739 .msg_namelen = sizeof(nladdr),
72a2ff39
CM
740 .msg_iov = iov,
741 .msg_iovlen = iovlen,
aba5acdf 742 };
72a2ff39
CM
743 unsigned int seq = 0;
744 struct nlmsghdr *h;
745 int i, status;
2d34851c 746 char *buf;
aba5acdf 747
72a2ff39
CM
748 for (i = 0; i < iovlen; i++) {
749 h = iov[i].iov_base;
750 h->nlmsg_seq = seq = ++rtnl->seq;
751 if (answer == NULL)
752 h->nlmsg_flags |= NLM_F_ACK;
753 }
aba5acdf
SH
754
755 status = sendmsg(rtnl->fd, &msg, 0);
aba5acdf
SH
756 if (status < 0) {
757 perror("Cannot talk to rtnetlink");
758 return -1;
759 }
760
72a2ff39
CM
761 /* change msg to use the response iov */
762 msg.msg_iov = &riov;
763 msg.msg_iovlen = 1;
764 i = 0;
aba5acdf 765 while (1) {
0951cbcd 766next:
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 805 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
8c50b728 806 int error = err->error;
2c500a4d 807
aba5acdf
SH
808 if (l < sizeof(struct nlmsgerr)) {
809 fprintf(stderr, "ERROR truncated\n");
c60389e4
SH
810 free(buf);
811 return -1;
812 }
813
bfb3bf18 814 if (!error) {
844c37b4
DA
815 /* check messages from kernel */
816 nl_dump_ext_ack(h, errfn);
bfb3bf18
SH
817 } else {
818 errno = -error;
844c37b4 819
b45e3000
SH
820 if (rtnl->proto != NETLINK_SOCK_DIAG &&
821 show_rtnl_err)
822 rtnl_talk_error(h, err, errfn);
823 }
b6432e68 824
c60389e4
SH
825 if (answer)
826 *answer = (struct nlmsghdr *)buf;
827 else
828 free(buf);
b45e3000 829
0951cbcd
PM
830 if (i < iovlen)
831 goto next;
8c50b728 832 return error ? -i : 0;
aba5acdf 833 }
c079e121 834
aba5acdf 835 if (answer) {
86bf43c7 836 *answer = (struct nlmsghdr *)buf;
aba5acdf
SH
837 return 0;
838 }
839
840 fprintf(stderr, "Unexpected reply!!!\n");
841
842 status -= NLMSG_ALIGN(len);
2c500a4d 843 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
aba5acdf 844 }
2d34851c 845 free(buf);
c079e121 846
aba5acdf
SH
847 if (msg.msg_flags & MSG_TRUNC) {
848 fprintf(stderr, "Message truncated\n");
849 continue;
850 }
c079e121 851
aba5acdf
SH
852 if (status) {
853 fprintf(stderr, "!!!Remnant of size %d\n", status);
854 exit(1);
855 }
856 }
857}
858
72a2ff39
CM
859static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
860 struct nlmsghdr **answer,
861 bool show_rtnl_err, nl_ext_ack_fn_t errfn)
862{
863 struct iovec iov = {
864 .iov_base = n,
865 .iov_len = n->nlmsg_len
866 };
867
868 return __rtnl_talk_iov(rtnl, &iov, 1, answer, show_rtnl_err, errfn);
869}
870
463d9efa 871int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
86bf43c7 872 struct nlmsghdr **answer)
463d9efa 873{
86bf43c7 874 return __rtnl_talk(rtnl, n, answer, true, NULL);
b6432e68
SH
875}
876
72a2ff39
CM
877int rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iovec, size_t iovlen,
878 struct nlmsghdr **answer)
879{
880 return __rtnl_talk_iov(rtnl, iovec, iovlen, answer, true, NULL);
881}
882
463d9efa 883int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n,
86bf43c7 884 struct nlmsghdr **answer)
463d9efa 885{
86bf43c7 886 return __rtnl_talk(rtnl, n, answer, false, NULL);
463d9efa
DA
887}
888
449b824a
ND
889int rtnl_listen_all_nsid(struct rtnl_handle *rth)
890{
891 unsigned int on = 1;
892
893 if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &on,
894 sizeof(on)) < 0) {
895 perror("NETLINK_LISTEN_ALL_NSID");
896 return -1;
897 }
898 rth->flags |= RTNL_HANDLE_F_LISTEN_ALL_NSID;
899 return 0;
900}
901
8ed63ab1 902int rtnl_listen(struct rtnl_handle *rtnl,
0628cddd 903 rtnl_listen_filter_t handler,
6dc9f016 904 void *jarg)
aba5acdf
SH
905{
906 int status;
907 struct nlmsghdr *h;
d17b136f 908 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
aba5acdf 909 struct iovec iov;
aba5acdf 910 struct msghdr msg = {
8ed63ab1
SH
911 .msg_name = &nladdr,
912 .msg_namelen = sizeof(nladdr),
913 .msg_iov = &iov,
914 .msg_iovlen = 1,
aba5acdf 915 };
e557212e 916 char buf[16384];
449b824a
ND
917 char cmsgbuf[BUFSIZ];
918
919 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
920 msg.msg_control = &cmsgbuf;
921 msg.msg_controllen = sizeof(cmsgbuf);
922 }
aba5acdf 923
aba5acdf 924 iov.iov_base = buf;
aba5acdf 925 while (1) {
449b824a
ND
926 struct rtnl_ctrl_data ctrl;
927 struct cmsghdr *cmsg;
928
aba5acdf
SH
929 iov.iov_len = sizeof(buf);
930 status = recvmsg(rtnl->fd, &msg, 0);
931
932 if (status < 0) {
aa8032e6 933 if (errno == EINTR || errno == EAGAIN)
aba5acdf 934 continue;
aa8032e6
SH
935 fprintf(stderr, "netlink receive error %s (%d)\n",
936 strerror(errno), errno);
7f03191f
PM
937 if (errno == ENOBUFS)
938 continue;
aa8032e6 939 return -1;
aba5acdf
SH
940 }
941 if (status == 0) {
942 fprintf(stderr, "EOF on netlink\n");
943 return -1;
944 }
945 if (msg.msg_namelen != sizeof(nladdr)) {
2c500a4d
SH
946 fprintf(stderr,
947 "Sender address length == %d\n",
948 msg.msg_namelen);
aba5acdf
SH
949 exit(1);
950 }
449b824a
ND
951
952 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
953 memset(&ctrl, 0, sizeof(ctrl));
954 ctrl.nsid = -1;
955 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
956 cmsg = CMSG_NXTHDR(&msg, cmsg))
957 if (cmsg->cmsg_level == SOL_NETLINK &&
958 cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID &&
959 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
960 int *data = (int *)CMSG_DATA(cmsg);
961
962 ctrl.nsid = *data;
963 }
964 }
965
2c500a4d 966 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
aba5acdf
SH
967 int err;
968 int len = h->nlmsg_len;
969 int l = len - sizeof(*h);
970
2c500a4d 971 if (l < 0 || len > status) {
aba5acdf
SH
972 if (msg.msg_flags & MSG_TRUNC) {
973 fprintf(stderr, "Truncated message\n");
974 return -1;
975 }
2c500a4d
SH
976 fprintf(stderr,
977 "!!!malformed message: len=%d\n",
978 len);
aba5acdf
SH
979 exit(1);
980 }
981
cd554f2c 982 err = handler(&ctrl, h, jarg);
aba5acdf
SH
983 if (err < 0)
984 return err;
985
986 status -= NLMSG_ALIGN(len);
2c500a4d 987 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
aba5acdf
SH
988 }
989 if (msg.msg_flags & MSG_TRUNC) {
990 fprintf(stderr, "Message truncated\n");
991 continue;
992 }
993 if (status) {
994 fprintf(stderr, "!!!Remnant of size %d\n", status);
995 exit(1);
996 }
997 }
998}
999
0628cddd 1000int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
6dc9f016 1001 void *jarg)
aba5acdf
SH
1002{
1003 int status;
2c500a4d
SH
1004 char buf[16384];
1005 struct nlmsghdr *h = (struct nlmsghdr *)buf;
aba5acdf 1006
aba5acdf 1007 while (1) {
2dd9f8e0 1008 int err, len;
aba5acdf
SH
1009 int l;
1010
1011 status = fread(&buf, 1, sizeof(*h), rtnl);
1012
1013 if (status < 0) {
1014 if (errno == EINTR)
1015 continue;
1016 perror("rtnl_from_file: fread");
1017 return -1;
1018 }
1019 if (status == 0)
1020 return 0;
1021
1022 len = h->nlmsg_len;
aba5acdf
SH
1023 l = len - sizeof(*h);
1024
2c500a4d 1025 if (l < 0 || len > sizeof(buf)) {
aba5acdf
SH
1026 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
1027 len, ftell(rtnl));
1028 return -1;
1029 }
1030
1031 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
1032
1033 if (status < 0) {
1034 perror("rtnl_from_file: fread");
1035 return -1;
1036 }
1037 if (status < l) {
1038 fprintf(stderr, "rtnl-from_file: truncated message\n");
1039 return -1;
1040 }
1041
cd554f2c 1042 err = handler(NULL, h, jarg);
aba5acdf
SH
1043 if (err < 0)
1044 return err;
1045 }
1046}
1047
2aa3dd29
SH
1048int addattr(struct nlmsghdr *n, int maxlen, int type)
1049{
1050 return addattr_l(n, maxlen, type, NULL, 0);
1051}
1052
1053int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
1054{
1055 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
1056}
1057
1058int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
1059{
1060 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
1061}
1062
aba5acdf
SH
1063int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
1064{
2aa3dd29
SH
1065 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
1066}
1067
1068int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
1069{
1070 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
1071}
1072
1073int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
1074{
1075 return addattr_l(n, maxlen, type, str, strlen(str)+1);
aba5acdf
SH
1076}
1077
8ed63ab1 1078int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
6dc9f016 1079 int alen)
aba5acdf
SH
1080{
1081 int len = RTA_LENGTH(alen);
1082 struct rtattr *rta;
1083
3dabdbb3 1084 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
2c500a4d
SH
1085 fprintf(stderr,
1086 "addattr_l ERROR: message exceeded bound of %d\n",
1087 maxlen);
aba5acdf 1088 return -1;
007d3a3e 1089 }
07f94362 1090 rta = NLMSG_TAIL(n);
aba5acdf
SH
1091 rta->rta_type = type;
1092 rta->rta_len = len;
893deac4
PS
1093 if (alen)
1094 memcpy(RTA_DATA(rta), data, alen);
3dabdbb3 1095 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
aba5acdf
SH
1096 return 0;
1097}
1098
07f94362 1099int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
1100{
1101 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
2c500a4d
SH
1102 fprintf(stderr,
1103 "addraw_l ERROR: message exceeded bound of %d\n",
1104 maxlen);
07f94362 1105 return -1;
1106 }
1107
1108 memcpy(NLMSG_TAIL(n), data, len);
1109 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
1110 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
1111 return 0;
1112}
1113
2f90c9c0
PM
1114struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
1115{
1116 struct rtattr *nest = NLMSG_TAIL(n);
1117
1118 addattr_l(n, maxlen, type, NULL, 0);
1119 return nest;
1120}
1121
1122int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
1123{
1124 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
1125 return n->nlmsg_len;
1126}
1127
1128struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
1129 const void *data, int len)
1130{
1131 struct rtattr *start = NLMSG_TAIL(n);
1132
1133 addattr_l(n, maxlen, type, data, len);
1134 addattr_nest(n, maxlen, type);
1135 return start;
1136}
1137
1138int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
1139{
1140 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
1141
1142 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
1143 addattr_nest_end(n, nest);
1144 return n->nlmsg_len;
1145}
1146
aba5acdf
SH
1147int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
1148{
1149 int len = RTA_LENGTH(4);
1150 struct rtattr *subrta;
1151
007d3a3e 1152 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
2c500a4d
SH
1153 fprintf(stderr,
1154 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1155 maxlen);
aba5acdf 1156 return -1;
007d3a3e 1157 }
2c500a4d 1158 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
aba5acdf
SH
1159 subrta->rta_type = type;
1160 subrta->rta_len = len;
1161 memcpy(RTA_DATA(subrta), &data, 4);
1162 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
1163 return 0;
1164}
1165
8ed63ab1 1166int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
6dc9f016 1167 const void *data, int alen)
aba5acdf
SH
1168{
1169 struct rtattr *subrta;
1170 int len = RTA_LENGTH(alen);
1171
3dabdbb3 1172 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
2c500a4d
SH
1173 fprintf(stderr,
1174 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1175 maxlen);
aba5acdf 1176 return -1;
007d3a3e 1177 }
2c500a4d 1178 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
aba5acdf
SH
1179 subrta->rta_type = type;
1180 subrta->rta_len = len;
893deac4
PS
1181 if (alen)
1182 memcpy(RTA_DATA(subrta), data, alen);
3dabdbb3 1183 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
aba5acdf
SH
1184 return 0;
1185}
1186
303cc9cb
RP
1187int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data)
1188{
1189 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u8));
1190}
1191
1192int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data)
1193{
1194 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u16));
1195}
1196
1197int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data)
1198{
1199 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u64));
1200}
1201
1202struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type)
1203{
1204 struct rtattr *nest = RTA_TAIL(rta);
1205
1206 rta_addattr_l(rta, maxlen, type, NULL, 0);
1207
1208 return nest;
1209}
1210
1211int rta_nest_end(struct rtattr *rta, struct rtattr *nest)
1212{
1213 nest->rta_len = (void *)RTA_TAIL(rta) - (void *)nest;
1214
1215 return rta->rta_len;
1216}
1217
aba5acdf
SH
1218int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
1219{
b1b7ce0f
VY
1220 return parse_rtattr_flags(tb, max, rta, len, 0);
1221}
1222
1223int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
1224 int len, unsigned short flags)
1225{
1226 unsigned short type;
1227
175e2440 1228 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
aba5acdf 1229 while (RTA_OK(rta, len)) {
b1b7ce0f
VY
1230 type = rta->rta_type & ~flags;
1231 if ((type <= max) && (!tb[type]))
1232 tb[type] = rta;
2c500a4d 1233 rta = RTA_NEXT(rta, len);
aba5acdf
SH
1234 }
1235 if (len)
2c500a4d
SH
1236 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1237 len, rta->rta_len);
aba5acdf
SH
1238 return 0;
1239}
c7699875 1240
decbb437
JP
1241struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len)
1242{
1243 while (RTA_OK(rta, len)) {
1244 if (rta->rta_type == type)
1245 return rta;
1246 rta = RTA_NEXT(rta, len);
1247 }
2c500a4d 1248
decbb437 1249 if (len)
2c500a4d
SH
1250 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1251 len, rta->rta_len);
decbb437
JP
1252 return NULL;
1253}
1254
2c500a4d
SH
1255int __parse_rtattr_nested_compat(struct rtattr *tb[], int max,
1256 struct rtattr *rta,
1257 int len)
2f90c9c0
PM
1258{
1259 if (RTA_PAYLOAD(rta) < len)
1260 return -1;
1261 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
1262 rta = RTA_DATA(rta) + RTA_ALIGN(len);
1263 return parse_rtattr_nested(tb, max, rta);
1264 }
037c635e 1265 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
2f90c9c0
PM
1266 return 0;
1267}