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