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