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