]> git.proxmox.com Git - mirror_iproute2.git/blame - lib/libnetlink.c
lib/libnetlink: re malloc buff if size is not enough
[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
DA
579static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
580 struct nlmsghdr *answer, size_t maxlen,
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)
654 memcpy(answer, h,
ed108cfc 655 MIN(maxlen, h->nlmsg_len));
2d34851c 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) {
c079e121 670 memcpy(answer, h,
ed108cfc 671 MIN(maxlen, h->nlmsg_len));
2d34851c 672 free(buf);
aba5acdf
SH
673 return 0;
674 }
675
676 fprintf(stderr, "Unexpected reply!!!\n");
677
678 status -= NLMSG_ALIGN(len);
2c500a4d 679 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
aba5acdf 680 }
2d34851c 681 free(buf);
c079e121 682
aba5acdf
SH
683 if (msg.msg_flags & MSG_TRUNC) {
684 fprintf(stderr, "Message truncated\n");
685 continue;
686 }
c079e121 687
aba5acdf
SH
688 if (status) {
689 fprintf(stderr, "!!!Remnant of size %d\n", status);
690 exit(1);
691 }
692 }
693}
694
463d9efa
DA
695int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
696 struct nlmsghdr *answer, size_t maxlen)
697{
b6432e68
SH
698 return __rtnl_talk(rtnl, n, answer, maxlen, true, NULL);
699}
700
701int rtnl_talk_extack(struct rtnl_handle *rtnl, struct nlmsghdr *n,
702 struct nlmsghdr *answer, size_t maxlen,
703 nl_ext_ack_fn_t errfn)
704{
705 return __rtnl_talk(rtnl, n, answer, maxlen, true, errfn);
463d9efa
DA
706}
707
708int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n,
709 struct nlmsghdr *answer, size_t maxlen)
710{
b6432e68 711 return __rtnl_talk(rtnl, n, answer, maxlen, false, NULL);
463d9efa
DA
712}
713
449b824a
ND
714int rtnl_listen_all_nsid(struct rtnl_handle *rth)
715{
716 unsigned int on = 1;
717
718 if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &on,
719 sizeof(on)) < 0) {
720 perror("NETLINK_LISTEN_ALL_NSID");
721 return -1;
722 }
723 rth->flags |= RTNL_HANDLE_F_LISTEN_ALL_NSID;
724 return 0;
725}
726
8ed63ab1 727int rtnl_listen(struct rtnl_handle *rtnl,
0628cddd 728 rtnl_listen_filter_t handler,
6dc9f016 729 void *jarg)
aba5acdf
SH
730{
731 int status;
732 struct nlmsghdr *h;
d17b136f 733 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
aba5acdf 734 struct iovec iov;
aba5acdf 735 struct msghdr msg = {
8ed63ab1
SH
736 .msg_name = &nladdr,
737 .msg_namelen = sizeof(nladdr),
738 .msg_iov = &iov,
739 .msg_iovlen = 1,
aba5acdf 740 };
e557212e 741 char buf[16384];
449b824a
ND
742 char cmsgbuf[BUFSIZ];
743
744 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
745 msg.msg_control = &cmsgbuf;
746 msg.msg_controllen = sizeof(cmsgbuf);
747 }
aba5acdf 748
aba5acdf 749 iov.iov_base = buf;
aba5acdf 750 while (1) {
449b824a
ND
751 struct rtnl_ctrl_data ctrl;
752 struct cmsghdr *cmsg;
753
aba5acdf
SH
754 iov.iov_len = sizeof(buf);
755 status = recvmsg(rtnl->fd, &msg, 0);
756
757 if (status < 0) {
aa8032e6 758 if (errno == EINTR || errno == EAGAIN)
aba5acdf 759 continue;
aa8032e6
SH
760 fprintf(stderr, "netlink receive error %s (%d)\n",
761 strerror(errno), errno);
7f03191f
PM
762 if (errno == ENOBUFS)
763 continue;
aa8032e6 764 return -1;
aba5acdf
SH
765 }
766 if (status == 0) {
767 fprintf(stderr, "EOF on netlink\n");
768 return -1;
769 }
770 if (msg.msg_namelen != sizeof(nladdr)) {
2c500a4d
SH
771 fprintf(stderr,
772 "Sender address length == %d\n",
773 msg.msg_namelen);
aba5acdf
SH
774 exit(1);
775 }
449b824a
ND
776
777 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
778 memset(&ctrl, 0, sizeof(ctrl));
779 ctrl.nsid = -1;
780 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
781 cmsg = CMSG_NXTHDR(&msg, cmsg))
782 if (cmsg->cmsg_level == SOL_NETLINK &&
783 cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID &&
784 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
785 int *data = (int *)CMSG_DATA(cmsg);
786
787 ctrl.nsid = *data;
788 }
789 }
790
2c500a4d 791 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
aba5acdf
SH
792 int err;
793 int len = h->nlmsg_len;
794 int l = len - sizeof(*h);
795
2c500a4d 796 if (l < 0 || len > status) {
aba5acdf
SH
797 if (msg.msg_flags & MSG_TRUNC) {
798 fprintf(stderr, "Truncated message\n");
799 return -1;
800 }
2c500a4d
SH
801 fprintf(stderr,
802 "!!!malformed message: len=%d\n",
803 len);
aba5acdf
SH
804 exit(1);
805 }
806
449b824a 807 err = handler(&nladdr, &ctrl, h, jarg);
aba5acdf
SH
808 if (err < 0)
809 return err;
810
811 status -= NLMSG_ALIGN(len);
2c500a4d 812 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
aba5acdf
SH
813 }
814 if (msg.msg_flags & MSG_TRUNC) {
815 fprintf(stderr, "Message truncated\n");
816 continue;
817 }
818 if (status) {
819 fprintf(stderr, "!!!Remnant of size %d\n", status);
820 exit(1);
821 }
822 }
823}
824
0628cddd 825int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
6dc9f016 826 void *jarg)
aba5acdf
SH
827{
828 int status;
d17b136f 829 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
2c500a4d
SH
830 char buf[16384];
831 struct nlmsghdr *h = (struct nlmsghdr *)buf;
aba5acdf 832
aba5acdf 833 while (1) {
2dd9f8e0 834 int err, len;
aba5acdf
SH
835 int l;
836
837 status = fread(&buf, 1, sizeof(*h), rtnl);
838
839 if (status < 0) {
840 if (errno == EINTR)
841 continue;
842 perror("rtnl_from_file: fread");
843 return -1;
844 }
845 if (status == 0)
846 return 0;
847
848 len = h->nlmsg_len;
aba5acdf
SH
849 l = len - sizeof(*h);
850
2c500a4d 851 if (l < 0 || len > sizeof(buf)) {
aba5acdf
SH
852 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
853 len, ftell(rtnl));
854 return -1;
855 }
856
857 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
858
859 if (status < 0) {
860 perror("rtnl_from_file: fread");
861 return -1;
862 }
863 if (status < l) {
864 fprintf(stderr, "rtnl-from_file: truncated message\n");
865 return -1;
866 }
867
0628cddd 868 err = handler(&nladdr, NULL, h, jarg);
aba5acdf
SH
869 if (err < 0)
870 return err;
871 }
872}
873
2aa3dd29
SH
874int addattr(struct nlmsghdr *n, int maxlen, int type)
875{
876 return addattr_l(n, maxlen, type, NULL, 0);
877}
878
879int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
880{
881 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
882}
883
884int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
885{
886 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
887}
888
aba5acdf
SH
889int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
890{
2aa3dd29
SH
891 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
892}
893
894int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
895{
896 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
897}
898
899int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
900{
901 return addattr_l(n, maxlen, type, str, strlen(str)+1);
aba5acdf
SH
902}
903
8ed63ab1 904int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
6dc9f016 905 int alen)
aba5acdf
SH
906{
907 int len = RTA_LENGTH(alen);
908 struct rtattr *rta;
909
3dabdbb3 910 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
2c500a4d
SH
911 fprintf(stderr,
912 "addattr_l ERROR: message exceeded bound of %d\n",
913 maxlen);
aba5acdf 914 return -1;
007d3a3e 915 }
07f94362 916 rta = NLMSG_TAIL(n);
aba5acdf
SH
917 rta->rta_type = type;
918 rta->rta_len = len;
893deac4
PS
919 if (alen)
920 memcpy(RTA_DATA(rta), data, alen);
3dabdbb3 921 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
aba5acdf
SH
922 return 0;
923}
924
07f94362 925int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
926{
927 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
2c500a4d
SH
928 fprintf(stderr,
929 "addraw_l ERROR: message exceeded bound of %d\n",
930 maxlen);
07f94362 931 return -1;
932 }
933
934 memcpy(NLMSG_TAIL(n), data, len);
935 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
936 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
937 return 0;
938}
939
2f90c9c0
PM
940struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
941{
942 struct rtattr *nest = NLMSG_TAIL(n);
943
944 addattr_l(n, maxlen, type, NULL, 0);
945 return nest;
946}
947
948int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
949{
950 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
951 return n->nlmsg_len;
952}
953
954struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
955 const void *data, int len)
956{
957 struct rtattr *start = NLMSG_TAIL(n);
958
959 addattr_l(n, maxlen, type, data, len);
960 addattr_nest(n, maxlen, type);
961 return start;
962}
963
964int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
965{
966 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
967
968 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
969 addattr_nest_end(n, nest);
970 return n->nlmsg_len;
971}
972
aba5acdf
SH
973int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
974{
975 int len = RTA_LENGTH(4);
976 struct rtattr *subrta;
977
007d3a3e 978 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
2c500a4d
SH
979 fprintf(stderr,
980 "rta_addattr32: Error! max allowed bound %d exceeded\n",
981 maxlen);
aba5acdf 982 return -1;
007d3a3e 983 }
2c500a4d 984 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
aba5acdf
SH
985 subrta->rta_type = type;
986 subrta->rta_len = len;
987 memcpy(RTA_DATA(subrta), &data, 4);
988 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
989 return 0;
990}
991
8ed63ab1 992int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
6dc9f016 993 const void *data, int alen)
aba5acdf
SH
994{
995 struct rtattr *subrta;
996 int len = RTA_LENGTH(alen);
997
3dabdbb3 998 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
2c500a4d
SH
999 fprintf(stderr,
1000 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1001 maxlen);
aba5acdf 1002 return -1;
007d3a3e 1003 }
2c500a4d 1004 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
aba5acdf
SH
1005 subrta->rta_type = type;
1006 subrta->rta_len = len;
893deac4
PS
1007 if (alen)
1008 memcpy(RTA_DATA(subrta), data, alen);
3dabdbb3 1009 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
aba5acdf
SH
1010 return 0;
1011}
1012
303cc9cb
RP
1013int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data)
1014{
1015 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u8));
1016}
1017
1018int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data)
1019{
1020 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u16));
1021}
1022
1023int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data)
1024{
1025 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u64));
1026}
1027
1028struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type)
1029{
1030 struct rtattr *nest = RTA_TAIL(rta);
1031
1032 rta_addattr_l(rta, maxlen, type, NULL, 0);
1033
1034 return nest;
1035}
1036
1037int rta_nest_end(struct rtattr *rta, struct rtattr *nest)
1038{
1039 nest->rta_len = (void *)RTA_TAIL(rta) - (void *)nest;
1040
1041 return rta->rta_len;
1042}
1043
aba5acdf
SH
1044int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
1045{
b1b7ce0f
VY
1046 return parse_rtattr_flags(tb, max, rta, len, 0);
1047}
1048
1049int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
1050 int len, unsigned short flags)
1051{
1052 unsigned short type;
1053
175e2440 1054 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
aba5acdf 1055 while (RTA_OK(rta, len)) {
b1b7ce0f
VY
1056 type = rta->rta_type & ~flags;
1057 if ((type <= max) && (!tb[type]))
1058 tb[type] = rta;
2c500a4d 1059 rta = RTA_NEXT(rta, len);
aba5acdf
SH
1060 }
1061 if (len)
2c500a4d
SH
1062 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1063 len, rta->rta_len);
aba5acdf
SH
1064 return 0;
1065}
c7699875 1066
2c500a4d
SH
1067int parse_rtattr_byindex(struct rtattr *tb[], int max,
1068 struct rtattr *rta, int len)
c7699875 1069{
1070 int i = 0;
175e2440
SH
1071
1072 memset(tb, 0, sizeof(struct rtattr *) * max);
c7699875 1073 while (RTA_OK(rta, len)) {
175e2440 1074 if (rta->rta_type <= max && i < max)
c7699875 1075 tb[i++] = rta;
2c500a4d 1076 rta = RTA_NEXT(rta, len);
c7699875 1077 }
1078 if (len)
2c500a4d
SH
1079 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1080 len, rta->rta_len);
c7699875 1081 return i;
1082}
2f90c9c0 1083
decbb437
JP
1084struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len)
1085{
1086 while (RTA_OK(rta, len)) {
1087 if (rta->rta_type == type)
1088 return rta;
1089 rta = RTA_NEXT(rta, len);
1090 }
2c500a4d 1091
decbb437 1092 if (len)
2c500a4d
SH
1093 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1094 len, rta->rta_len);
decbb437
JP
1095 return NULL;
1096}
1097
2c500a4d
SH
1098int __parse_rtattr_nested_compat(struct rtattr *tb[], int max,
1099 struct rtattr *rta,
1100 int len)
2f90c9c0
PM
1101{
1102 if (RTA_PAYLOAD(rta) < len)
1103 return -1;
1104 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
1105 rta = RTA_DATA(rta) + RTA_ALIGN(len);
1106 return parse_rtattr_nested(tb, max, rta);
1107 }
037c635e 1108 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
2f90c9c0
PM
1109 return 0;
1110}