]> git.proxmox.com Git - mirror_iproute2.git/blame - lib/libnetlink.c
Merge branch 'master' into net-next
[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
2d34851c
HL
405static int __rtnl_recvmsg(int fd, struct msghdr *msg, int flags)
406{
407 int len;
408
409 do {
410 len = recvmsg(fd, msg, flags);
411 } while (len < 0 && (errno == EINTR || errno == EAGAIN));
412
413 if (len < 0) {
414 fprintf(stderr, "netlink receive error %s (%d)\n",
415 strerror(errno), errno);
416 return -errno;
417 }
418
419 if (len == 0) {
420 fprintf(stderr, "EOF on netlink\n");
421 return -ENODATA;
422 }
423
424 return len;
425}
426
427static int rtnl_recvmsg(int fd, struct msghdr *msg, char **answer)
428{
429 struct iovec *iov = msg->msg_iov;
430 char *buf;
431 int len;
432
433 iov->iov_base = NULL;
434 iov->iov_len = 0;
435
436 len = __rtnl_recvmsg(fd, msg, MSG_PEEK | MSG_TRUNC);
437 if (len < 0)
438 return len;
439
440 buf = malloc(len);
441 if (!buf) {
442 fprintf(stderr, "malloc error: not enough buffer\n");
443 return -ENOMEM;
444 }
445
446 iov->iov_base = buf;
447 iov->iov_len = len;
448
449 len = __rtnl_recvmsg(fd, msg, 0);
450 if (len < 0) {
451 free(buf);
452 return len;
453 }
454
455 if (answer)
456 *answer = buf;
457 else
458 free(buf);
459
460 return len;
461}
462
b49240ec
SH
463int rtnl_dump_filter_l(struct rtnl_handle *rth,
464 const struct rtnl_dump_filter_arg *arg)
aba5acdf 465{
aba5acdf 466 struct sockaddr_nl nladdr;
8ed63ab1
SH
467 struct iovec iov;
468 struct msghdr msg = {
469 .msg_name = &nladdr,
470 .msg_namelen = sizeof(nladdr),
471 .msg_iov = &iov,
472 .msg_iovlen = 1,
473 };
2d34851c 474 char *buf;
16f02e14 475 int dump_intr = 0;
aba5acdf
SH
476
477 while (1) {
478 int status;
b49240ec 479 const struct rtnl_dump_filter_arg *a;
3bc1c4f2
BG
480 int found_done = 0;
481 int msglen = 0;
aba5acdf 482
2d34851c
HL
483 status = rtnl_recvmsg(rth->fd, &msg, &buf);
484 if (status < 0)
485 return status;
aba5acdf 486
486ccd99
VK
487 if (rth->dump_fp)
488 fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp);
489
b49240ec 490 for (a = arg; a->filter; a++) {
2c500a4d
SH
491 struct nlmsghdr *h = (struct nlmsghdr *)buf;
492
3bc1c4f2 493 msglen = status;
b49240ec 494
3bc1c4f2 495 while (NLMSG_OK(h, msglen)) {
486ccd99 496 int err = 0;
b49240ec 497
8e72880f
PS
498 h->nlmsg_flags &= ~a->nc_flags;
499
b49240ec
SH
500 if (nladdr.nl_pid != 0 ||
501 h->nlmsg_pid != rth->local.nl_pid ||
cd70f3f5 502 h->nlmsg_seq != rth->dump)
b49240ec 503 goto skip_it;
aba5acdf 504
16f02e14
ND
505 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
506 dump_intr = 1;
507
3bc1c4f2 508 if (h->nlmsg_type == NLMSG_DONE) {
0efa6257 509 err = rtnl_dump_done(h);
2d34851c
HL
510 if (err < 0) {
511 free(buf);
892a25e2 512 return -1;
2d34851c 513 }
892a25e2 514
3bc1c4f2
BG
515 found_done = 1;
516 break; /* process next filter */
517 }
892a25e2 518
b49240ec 519 if (h->nlmsg_type == NLMSG_ERROR) {
892a25e2 520 rtnl_dump_error(rth, h);
2d34851c 521 free(buf);
b49240ec 522 return -1;
aba5acdf 523 }
486ccd99
VK
524
525 if (!rth->dump_fp) {
526 err = a->filter(&nladdr, h, a->arg1);
2d34851c
HL
527 if (err < 0) {
528 free(buf);
486ccd99 529 return err;
2d34851c 530 }
486ccd99 531 }
aba5acdf
SH
532
533skip_it:
3bc1c4f2 534 h = NLMSG_NEXT(h, msglen);
b49240ec 535 }
3bc1c4f2 536 }
2d34851c 537 free(buf);
3bc1c4f2 538
16f02e14
ND
539 if (found_done) {
540 if (dump_intr)
541 fprintf(stderr,
542 "Dump was interrupted and may be inconsistent.\n");
3bc1c4f2 543 return 0;
16f02e14 544 }
3bc1c4f2 545
aba5acdf
SH
546 if (msg.msg_flags & MSG_TRUNC) {
547 fprintf(stderr, "Message truncated\n");
548 continue;
549 }
3bc1c4f2
BG
550 if (msglen) {
551 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
aba5acdf
SH
552 exit(1);
553 }
554 }
555}
556
8e72880f 557int rtnl_dump_filter_nc(struct rtnl_handle *rth,
b49240ec 558 rtnl_filter_t filter,
8e72880f 559 void *arg1, __u16 nc_flags)
b49240ec
SH
560{
561 const struct rtnl_dump_filter_arg a[2] = {
8e72880f
PS
562 { .filter = filter, .arg1 = arg1, .nc_flags = nc_flags, },
563 { .filter = NULL, .arg1 = NULL, .nc_flags = 0, },
b49240ec
SH
564 };
565
566 return rtnl_dump_filter_l(rth, a);
567}
568
b6432e68
SH
569static void rtnl_talk_error(struct nlmsghdr *h, struct nlmsgerr *err,
570 nl_ext_ack_fn_t errfn)
571{
572 if (nl_dump_ext_err(h, errfn))
573 return;
574
575 fprintf(stderr, "RTNETLINK answers: %s\n",
576 strerror(-err->error));
577}
578
463d9efa 579static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
86bf43c7 580 struct nlmsghdr **answer,
b6432e68 581 bool show_rtnl_err, nl_ext_ack_fn_t errfn)
aba5acdf
SH
582{
583 int status;
2c500a4d 584 unsigned int seq;
aba5acdf 585 struct nlmsghdr *h;
d17b136f 586 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
fb229759 587 struct iovec iov = {
2c500a4d 588 .iov_base = n,
fb229759
SH
589 .iov_len = n->nlmsg_len
590 };
aba5acdf 591 struct msghdr msg = {
8ed63ab1
SH
592 .msg_name = &nladdr,
593 .msg_namelen = sizeof(nladdr),
594 .msg_iov = &iov,
595 .msg_iovlen = 1,
aba5acdf 596 };
2d34851c 597 char *buf;
aba5acdf
SH
598
599 n->nlmsg_seq = seq = ++rtnl->seq;
007d3a3e 600
aba5acdf
SH
601 if (answer == NULL)
602 n->nlmsg_flags |= NLM_F_ACK;
603
604 status = sendmsg(rtnl->fd, &msg, 0);
aba5acdf
SH
605 if (status < 0) {
606 perror("Cannot talk to rtnetlink");
607 return -1;
608 }
609
aba5acdf 610 while (1) {
2d34851c
HL
611 status = rtnl_recvmsg(rtnl->fd, &msg, &buf);
612
613 if (status < 0)
614 return status;
aba5acdf 615
aba5acdf 616 if (msg.msg_namelen != sizeof(nladdr)) {
2c500a4d
SH
617 fprintf(stderr,
618 "sender address length == %d\n",
619 msg.msg_namelen);
aba5acdf
SH
620 exit(1);
621 }
2c500a4d 622 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
aba5acdf
SH
623 int len = h->nlmsg_len;
624 int l = len - sizeof(*h);
625
2c500a4d 626 if (l < 0 || len > status) {
aba5acdf
SH
627 if (msg.msg_flags & MSG_TRUNC) {
628 fprintf(stderr, "Truncated message\n");
2d34851c 629 free(buf);
aba5acdf
SH
630 return -1;
631 }
2c500a4d
SH
632 fprintf(stderr,
633 "!!!malformed message: len=%d\n",
634 len);
aba5acdf
SH
635 exit(1);
636 }
637
c079e121 638 if (nladdr.nl_pid != 0 ||
10f57ef1 639 h->nlmsg_pid != rtnl->local.nl_pid ||
aba5acdf 640 h->nlmsg_seq != seq) {
4cca16f2
SH
641 /* Don't forget to skip that message. */
642 status -= NLMSG_ALIGN(len);
2c500a4d 643 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
aba5acdf
SH
644 continue;
645 }
646
647 if (h->nlmsg_type == NLMSG_ERROR) {
2c500a4d
SH
648 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
649
aba5acdf
SH
650 if (l < sizeof(struct nlmsgerr)) {
651 fprintf(stderr, "ERROR truncated\n");
c079e121
SH
652 } else if (!err->error) {
653 if (answer)
86bf43c7
HL
654 *answer = (struct nlmsghdr *)buf;
655 else
656 free(buf);
c079e121 657 return 0;
aba5acdf 658 }
c079e121 659
b6432e68
SH
660 if (rtnl->proto != NETLINK_SOCK_DIAG &&
661 show_rtnl_err)
662 rtnl_talk_error(h, err, errfn);
663
c079e121 664 errno = -err->error;
2d34851c 665 free(buf);
aba5acdf
SH
666 return -1;
667 }
c079e121 668
aba5acdf 669 if (answer) {
86bf43c7 670 *answer = (struct nlmsghdr *)buf;
aba5acdf
SH
671 return 0;
672 }
673
674 fprintf(stderr, "Unexpected reply!!!\n");
675
676 status -= NLMSG_ALIGN(len);
2c500a4d 677 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
aba5acdf 678 }
2d34851c 679 free(buf);
c079e121 680
aba5acdf
SH
681 if (msg.msg_flags & MSG_TRUNC) {
682 fprintf(stderr, "Message truncated\n");
683 continue;
684 }
c079e121 685
aba5acdf
SH
686 if (status) {
687 fprintf(stderr, "!!!Remnant of size %d\n", status);
688 exit(1);
689 }
690 }
691}
692
463d9efa 693int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
86bf43c7 694 struct nlmsghdr **answer)
463d9efa 695{
86bf43c7 696 return __rtnl_talk(rtnl, n, answer, true, NULL);
b6432e68
SH
697}
698
699int rtnl_talk_extack(struct rtnl_handle *rtnl, struct nlmsghdr *n,
86bf43c7 700 struct nlmsghdr **answer,
b6432e68
SH
701 nl_ext_ack_fn_t errfn)
702{
86bf43c7 703 return __rtnl_talk(rtnl, n, answer, true, errfn);
463d9efa
DA
704}
705
706int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n,
86bf43c7 707 struct nlmsghdr **answer)
463d9efa 708{
86bf43c7 709 return __rtnl_talk(rtnl, n, answer, false, NULL);
463d9efa
DA
710}
711
449b824a
ND
712int rtnl_listen_all_nsid(struct rtnl_handle *rth)
713{
714 unsigned int on = 1;
715
716 if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &on,
717 sizeof(on)) < 0) {
718 perror("NETLINK_LISTEN_ALL_NSID");
719 return -1;
720 }
721 rth->flags |= RTNL_HANDLE_F_LISTEN_ALL_NSID;
722 return 0;
723}
724
8ed63ab1 725int rtnl_listen(struct rtnl_handle *rtnl,
0628cddd 726 rtnl_listen_filter_t handler,
6dc9f016 727 void *jarg)
aba5acdf
SH
728{
729 int status;
730 struct nlmsghdr *h;
d17b136f 731 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
aba5acdf 732 struct iovec iov;
aba5acdf 733 struct msghdr msg = {
8ed63ab1
SH
734 .msg_name = &nladdr,
735 .msg_namelen = sizeof(nladdr),
736 .msg_iov = &iov,
737 .msg_iovlen = 1,
aba5acdf 738 };
e557212e 739 char buf[16384];
449b824a
ND
740 char cmsgbuf[BUFSIZ];
741
742 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
743 msg.msg_control = &cmsgbuf;
744 msg.msg_controllen = sizeof(cmsgbuf);
745 }
aba5acdf 746
aba5acdf 747 iov.iov_base = buf;
aba5acdf 748 while (1) {
449b824a
ND
749 struct rtnl_ctrl_data ctrl;
750 struct cmsghdr *cmsg;
751
aba5acdf
SH
752 iov.iov_len = sizeof(buf);
753 status = recvmsg(rtnl->fd, &msg, 0);
754
755 if (status < 0) {
aa8032e6 756 if (errno == EINTR || errno == EAGAIN)
aba5acdf 757 continue;
aa8032e6
SH
758 fprintf(stderr, "netlink receive error %s (%d)\n",
759 strerror(errno), errno);
7f03191f
PM
760 if (errno == ENOBUFS)
761 continue;
aa8032e6 762 return -1;
aba5acdf
SH
763 }
764 if (status == 0) {
765 fprintf(stderr, "EOF on netlink\n");
766 return -1;
767 }
768 if (msg.msg_namelen != sizeof(nladdr)) {
2c500a4d
SH
769 fprintf(stderr,
770 "Sender address length == %d\n",
771 msg.msg_namelen);
aba5acdf
SH
772 exit(1);
773 }
449b824a
ND
774
775 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
776 memset(&ctrl, 0, sizeof(ctrl));
777 ctrl.nsid = -1;
778 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
779 cmsg = CMSG_NXTHDR(&msg, cmsg))
780 if (cmsg->cmsg_level == SOL_NETLINK &&
781 cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID &&
782 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
783 int *data = (int *)CMSG_DATA(cmsg);
784
785 ctrl.nsid = *data;
786 }
787 }
788
2c500a4d 789 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
aba5acdf
SH
790 int err;
791 int len = h->nlmsg_len;
792 int l = len - sizeof(*h);
793
2c500a4d 794 if (l < 0 || len > status) {
aba5acdf
SH
795 if (msg.msg_flags & MSG_TRUNC) {
796 fprintf(stderr, "Truncated message\n");
797 return -1;
798 }
2c500a4d
SH
799 fprintf(stderr,
800 "!!!malformed message: len=%d\n",
801 len);
aba5acdf
SH
802 exit(1);
803 }
804
449b824a 805 err = handler(&nladdr, &ctrl, h, jarg);
aba5acdf
SH
806 if (err < 0)
807 return err;
808
809 status -= NLMSG_ALIGN(len);
2c500a4d 810 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
aba5acdf
SH
811 }
812 if (msg.msg_flags & MSG_TRUNC) {
813 fprintf(stderr, "Message truncated\n");
814 continue;
815 }
816 if (status) {
817 fprintf(stderr, "!!!Remnant of size %d\n", status);
818 exit(1);
819 }
820 }
821}
822
0628cddd 823int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
6dc9f016 824 void *jarg)
aba5acdf
SH
825{
826 int status;
d17b136f 827 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
2c500a4d
SH
828 char buf[16384];
829 struct nlmsghdr *h = (struct nlmsghdr *)buf;
aba5acdf 830
aba5acdf 831 while (1) {
2dd9f8e0 832 int err, len;
aba5acdf
SH
833 int l;
834
835 status = fread(&buf, 1, sizeof(*h), rtnl);
836
837 if (status < 0) {
838 if (errno == EINTR)
839 continue;
840 perror("rtnl_from_file: fread");
841 return -1;
842 }
843 if (status == 0)
844 return 0;
845
846 len = h->nlmsg_len;
aba5acdf
SH
847 l = len - sizeof(*h);
848
2c500a4d 849 if (l < 0 || len > sizeof(buf)) {
aba5acdf
SH
850 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
851 len, ftell(rtnl));
852 return -1;
853 }
854
855 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
856
857 if (status < 0) {
858 perror("rtnl_from_file: fread");
859 return -1;
860 }
861 if (status < l) {
862 fprintf(stderr, "rtnl-from_file: truncated message\n");
863 return -1;
864 }
865
0628cddd 866 err = handler(&nladdr, NULL, h, jarg);
aba5acdf
SH
867 if (err < 0)
868 return err;
869 }
870}
871
2aa3dd29
SH
872int addattr(struct nlmsghdr *n, int maxlen, int type)
873{
874 return addattr_l(n, maxlen, type, NULL, 0);
875}
876
877int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
878{
879 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
880}
881
882int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
883{
884 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
885}
886
aba5acdf
SH
887int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
888{
2aa3dd29
SH
889 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
890}
891
892int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
893{
894 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
895}
896
897int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
898{
899 return addattr_l(n, maxlen, type, str, strlen(str)+1);
aba5acdf
SH
900}
901
8ed63ab1 902int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
6dc9f016 903 int alen)
aba5acdf
SH
904{
905 int len = RTA_LENGTH(alen);
906 struct rtattr *rta;
907
3dabdbb3 908 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
2c500a4d
SH
909 fprintf(stderr,
910 "addattr_l ERROR: message exceeded bound of %d\n",
911 maxlen);
aba5acdf 912 return -1;
007d3a3e 913 }
07f94362 914 rta = NLMSG_TAIL(n);
aba5acdf
SH
915 rta->rta_type = type;
916 rta->rta_len = len;
893deac4
PS
917 if (alen)
918 memcpy(RTA_DATA(rta), data, alen);
3dabdbb3 919 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
aba5acdf
SH
920 return 0;
921}
922
07f94362 923int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
924{
925 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
2c500a4d
SH
926 fprintf(stderr,
927 "addraw_l ERROR: message exceeded bound of %d\n",
928 maxlen);
07f94362 929 return -1;
930 }
931
932 memcpy(NLMSG_TAIL(n), data, len);
933 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
934 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
935 return 0;
936}
937
2f90c9c0
PM
938struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
939{
940 struct rtattr *nest = NLMSG_TAIL(n);
941
942 addattr_l(n, maxlen, type, NULL, 0);
943 return nest;
944}
945
946int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
947{
948 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
949 return n->nlmsg_len;
950}
951
952struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
953 const void *data, int len)
954{
955 struct rtattr *start = NLMSG_TAIL(n);
956
957 addattr_l(n, maxlen, type, data, len);
958 addattr_nest(n, maxlen, type);
959 return start;
960}
961
962int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
963{
964 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
965
966 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
967 addattr_nest_end(n, nest);
968 return n->nlmsg_len;
969}
970
aba5acdf
SH
971int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
972{
973 int len = RTA_LENGTH(4);
974 struct rtattr *subrta;
975
007d3a3e 976 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
2c500a4d
SH
977 fprintf(stderr,
978 "rta_addattr32: Error! max allowed bound %d exceeded\n",
979 maxlen);
aba5acdf 980 return -1;
007d3a3e 981 }
2c500a4d 982 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
aba5acdf
SH
983 subrta->rta_type = type;
984 subrta->rta_len = len;
985 memcpy(RTA_DATA(subrta), &data, 4);
986 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
987 return 0;
988}
989
8ed63ab1 990int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
6dc9f016 991 const void *data, int alen)
aba5acdf
SH
992{
993 struct rtattr *subrta;
994 int len = RTA_LENGTH(alen);
995
3dabdbb3 996 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
2c500a4d
SH
997 fprintf(stderr,
998 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
999 maxlen);
aba5acdf 1000 return -1;
007d3a3e 1001 }
2c500a4d 1002 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
aba5acdf
SH
1003 subrta->rta_type = type;
1004 subrta->rta_len = len;
893deac4
PS
1005 if (alen)
1006 memcpy(RTA_DATA(subrta), data, alen);
3dabdbb3 1007 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
aba5acdf
SH
1008 return 0;
1009}
1010
303cc9cb
RP
1011int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data)
1012{
1013 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u8));
1014}
1015
1016int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data)
1017{
1018 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u16));
1019}
1020
1021int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data)
1022{
1023 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u64));
1024}
1025
1026struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type)
1027{
1028 struct rtattr *nest = RTA_TAIL(rta);
1029
1030 rta_addattr_l(rta, maxlen, type, NULL, 0);
1031
1032 return nest;
1033}
1034
1035int rta_nest_end(struct rtattr *rta, struct rtattr *nest)
1036{
1037 nest->rta_len = (void *)RTA_TAIL(rta) - (void *)nest;
1038
1039 return rta->rta_len;
1040}
1041
aba5acdf
SH
1042int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
1043{
b1b7ce0f
VY
1044 return parse_rtattr_flags(tb, max, rta, len, 0);
1045}
1046
1047int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
1048 int len, unsigned short flags)
1049{
1050 unsigned short type;
1051
175e2440 1052 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
aba5acdf 1053 while (RTA_OK(rta, len)) {
b1b7ce0f
VY
1054 type = rta->rta_type & ~flags;
1055 if ((type <= max) && (!tb[type]))
1056 tb[type] = rta;
2c500a4d 1057 rta = RTA_NEXT(rta, len);
aba5acdf
SH
1058 }
1059 if (len)
2c500a4d
SH
1060 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1061 len, rta->rta_len);
aba5acdf
SH
1062 return 0;
1063}
c7699875 1064
2c500a4d
SH
1065int parse_rtattr_byindex(struct rtattr *tb[], int max,
1066 struct rtattr *rta, int len)
c7699875 1067{
1068 int i = 0;
175e2440
SH
1069
1070 memset(tb, 0, sizeof(struct rtattr *) * max);
c7699875 1071 while (RTA_OK(rta, len)) {
175e2440 1072 if (rta->rta_type <= max && i < max)
c7699875 1073 tb[i++] = rta;
2c500a4d 1074 rta = RTA_NEXT(rta, len);
c7699875 1075 }
1076 if (len)
2c500a4d
SH
1077 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1078 len, rta->rta_len);
c7699875 1079 return i;
1080}
2f90c9c0 1081
decbb437
JP
1082struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len)
1083{
1084 while (RTA_OK(rta, len)) {
1085 if (rta->rta_type == type)
1086 return rta;
1087 rta = RTA_NEXT(rta, len);
1088 }
2c500a4d 1089
decbb437 1090 if (len)
2c500a4d
SH
1091 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1092 len, rta->rta_len);
decbb437
JP
1093 return NULL;
1094}
1095
2c500a4d
SH
1096int __parse_rtattr_nested_compat(struct rtattr *tb[], int max,
1097 struct rtattr *rta,
1098 int len)
2f90c9c0
PM
1099{
1100 if (RTA_PAYLOAD(rta) < len)
1101 return -1;
1102 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
1103 rta = RTA_DATA(rta) + RTA_ALIGN(len);
1104 return parse_rtattr_nested(tb, max, rta);
1105 }
037c635e 1106 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
2f90c9c0
PM
1107 return 0;
1108}