]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/libnetlink.c
ip: add support for seg6local End.BPF action
[mirror_iproute2.git] / lib / libnetlink.c
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>
15 #include <stdbool.h>
16 #include <unistd.h>
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
28 #ifndef SOL_NETLINK
29 #define SOL_NETLINK 270
30 #endif
31
32 #ifndef MIN
33 #define MIN(a, b) ((a) < (b) ? (a) : (b))
34 #endif
35
36 int rcvbuf = 1024 * 1024;
37
38 #ifdef HAVE_LIBMNL
39 #include <libmnl/libmnl.h>
40
41 static 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
46 static int err_attr_cb(const struct nlattr *attr, void *data)
47 {
48 const struct nlattr **tb = data;
49 uint16_t type;
50
51 if (mnl_attr_type_valid(attr, NLMSGERR_ATTR_MAX) < 0) {
52 fprintf(stderr, "Invalid extack attribute\n");
53 return MNL_CB_ERROR;
54 }
55
56 type = mnl_attr_get_type(attr);
57 if (mnl_attr_validate(attr, extack_policy[type]) < 0) {
58 fprintf(stderr, "extack attribute %d failed validation\n",
59 type);
60 return MNL_CB_ERROR;
61 }
62
63 tb[type] = attr;
64 return MNL_CB_OK;
65 }
66
67 /* dump netlink extended ack error message */
68 int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn)
69 {
70 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {};
71 const struct nlmsgerr *err = mnl_nlmsg_get_payload(nlh);
72 const struct nlmsghdr *err_nlh = NULL;
73 unsigned int hlen = sizeof(*err);
74 const char *msg = NULL;
75 uint32_t off = 0;
76
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
85 if (mnl_attr_parse(nlh, hlen, err_attr_cb, tb) != MNL_CB_OK)
86 return 0;
87
88 if (tb[NLMSGERR_ATTR_MSG])
89 msg = mnl_attr_get_str(tb[NLMSGERR_ATTR_MSG]);
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
102 if (errfn)
103 return errfn(msg, off, err_nlh);
104
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] != '.')
111 fprintf(stderr, ".");
112 fprintf(stderr, "\n");
113
114 return is_err ? 1 : 0;
115 }
116
117 return 0;
118 }
119 #else
120 #warning "libmnl required for error support"
121
122 /* No extended error ack without libmnl */
123 int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn)
124 {
125 return 0;
126 }
127 #endif
128
129 void rtnl_close(struct rtnl_handle *rth)
130 {
131 if (rth->fd >= 0) {
132 close(rth->fd);
133 rth->fd = -1;
134 }
135 }
136
137 int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions,
138 int protocol)
139 {
140 socklen_t addr_len;
141 int sndbuf = 32768;
142 int one = 1;
143
144 memset(rth, 0, sizeof(*rth));
145
146 rth->proto = protocol;
147 rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
148 if (rth->fd < 0) {
149 perror("Cannot open netlink socket");
150 return -1;
151 }
152
153 if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF,
154 &sndbuf, sizeof(sndbuf)) < 0) {
155 perror("SO_SNDBUF");
156 return -1;
157 }
158
159 if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF,
160 &rcvbuf, sizeof(rcvbuf)) < 0) {
161 perror("SO_RCVBUF");
162 return -1;
163 }
164
165 /* Older kernels may no support extended ACK reporting */
166 setsockopt(rth->fd, SOL_NETLINK, NETLINK_EXT_ACK,
167 &one, sizeof(one));
168
169 memset(&rth->local, 0, sizeof(rth->local));
170 rth->local.nl_family = AF_NETLINK;
171 rth->local.nl_groups = subscriptions;
172
173 if (bind(rth->fd, (struct sockaddr *)&rth->local,
174 sizeof(rth->local)) < 0) {
175 perror("Cannot bind netlink socket");
176 return -1;
177 }
178 addr_len = sizeof(rth->local);
179 if (getsockname(rth->fd, (struct sockaddr *)&rth->local,
180 &addr_len) < 0) {
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) {
189 fprintf(stderr, "Wrong address family %d\n",
190 rth->local.nl_family);
191 return -1;
192 }
193 rth->seq = time(NULL);
194 return 0;
195 }
196
197 int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
198 {
199 return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
200 }
201
202 int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
203 {
204 return rtnl_wilddump_req_filter(rth, family, type, RTEXT_FILTER_VF);
205 }
206
207 int rtnl_wilddump_req_filter(struct rtnl_handle *rth, int family, int type,
208 __u32 filt_mask)
209 {
210 struct {
211 struct nlmsghdr nlh;
212 struct ifinfomsg ifm;
213 /* attribute has to be NLMSG aligned */
214 struct rtattr ext_req __attribute__ ((aligned(NLMSG_ALIGNTO)));
215 __u32 ext_filter_mask;
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 };
226
227 return send(rth->fd, &req, sizeof(req), 0);
228 }
229
230 int 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];
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 };
244 int err;
245
246 if (!filter_fn)
247 return -EINVAL;
248
249 err = filter_fn(&req.nlh, sizeof(req));
250 if (err)
251 return err;
252
253 return send(rth->fd, &req, req.nlh.nlmsg_len, 0);
254 }
255
256 int 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
273 return send(rth->fd, &req, sizeof(req), 0);
274 }
275
276 int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
277 {
278 return send(rth->fd, buf, len, 0);
279 }
280
281 int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
282 {
283 struct nlmsghdr *h;
284 int status;
285 char resp[1024];
286
287 status = send(rth->fd, buf, len, 0);
288 if (status < 0)
289 return status;
290
291 /* Check for immediate errors */
292 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
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) {
302 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
303
304 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
305 fprintf(stderr, "ERROR truncated\n");
306 else
307 errno = -err->error;
308 return -1;
309 }
310 }
311
312 return 0;
313 }
314
315 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
316 {
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 };
323 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
324 struct iovec iov[2] = {
325 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
326 { .iov_base = req, .iov_len = len }
327 };
328 struct msghdr msg = {
329 .msg_name = &nladdr,
330 .msg_namelen = sizeof(nladdr),
331 .msg_iov = iov,
332 .msg_iovlen = 2,
333 };
334
335 return sendmsg(rth->fd, &msg, 0);
336 }
337
338 int 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 = {
342 .iov_base = n,
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
359 static int rtnl_dump_done(struct nlmsghdr *h)
360 {
361 int len = *(int *)NLMSG_DATA(h);
362
363 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(int))) {
364 fprintf(stderr, "DONE truncated\n");
365 return -1;
366 }
367
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:
379 perror("RTNETLINK answers");
380 }
381 return len;
382 }
383
384 /* check for any messages returned from kernel */
385 nl_dump_ext_ack(h, NULL);
386
387 return 0;
388 }
389
390 static 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
405 if (!(rth->flags & RTNL_HANDLE_F_SUPPRESS_NLERR))
406 perror("RTNETLINK answers");
407 }
408 }
409
410 static 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
432 static 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
468 int rtnl_dump_filter_l(struct rtnl_handle *rth,
469 const struct rtnl_dump_filter_arg *arg)
470 {
471 struct sockaddr_nl nladdr;
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 };
479 char *buf;
480 int dump_intr = 0;
481
482 while (1) {
483 int status;
484 const struct rtnl_dump_filter_arg *a;
485 int found_done = 0;
486 int msglen = 0;
487
488 status = rtnl_recvmsg(rth->fd, &msg, &buf);
489 if (status < 0)
490 return status;
491
492 if (rth->dump_fp)
493 fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp);
494
495 for (a = arg; a->filter; a++) {
496 struct nlmsghdr *h = (struct nlmsghdr *)buf;
497
498 msglen = status;
499
500 while (NLMSG_OK(h, msglen)) {
501 int err = 0;
502
503 h->nlmsg_flags &= ~a->nc_flags;
504
505 if (nladdr.nl_pid != 0 ||
506 h->nlmsg_pid != rth->local.nl_pid ||
507 h->nlmsg_seq != rth->dump)
508 goto skip_it;
509
510 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
511 dump_intr = 1;
512
513 if (h->nlmsg_type == NLMSG_DONE) {
514 err = rtnl_dump_done(h);
515 if (err < 0) {
516 free(buf);
517 return -1;
518 }
519
520 found_done = 1;
521 break; /* process next filter */
522 }
523
524 if (h->nlmsg_type == NLMSG_ERROR) {
525 rtnl_dump_error(rth, h);
526 free(buf);
527 return -1;
528 }
529
530 if (!rth->dump_fp) {
531 err = a->filter(&nladdr, h, a->arg1);
532 if (err < 0) {
533 free(buf);
534 return err;
535 }
536 }
537
538 skip_it:
539 h = NLMSG_NEXT(h, msglen);
540 }
541 }
542 free(buf);
543
544 if (found_done) {
545 if (dump_intr)
546 fprintf(stderr,
547 "Dump was interrupted and may be inconsistent.\n");
548 return 0;
549 }
550
551 if (msg.msg_flags & MSG_TRUNC) {
552 fprintf(stderr, "Message truncated\n");
553 continue;
554 }
555 if (msglen) {
556 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
557 exit(1);
558 }
559 }
560 }
561
562 int rtnl_dump_filter_nc(struct rtnl_handle *rth,
563 rtnl_filter_t filter,
564 void *arg1, __u16 nc_flags)
565 {
566 const struct rtnl_dump_filter_arg a[2] = {
567 { .filter = filter, .arg1 = arg1, .nc_flags = nc_flags, },
568 { .filter = NULL, .arg1 = NULL, .nc_flags = 0, },
569 };
570
571 return rtnl_dump_filter_l(rth, a);
572 }
573
574 static void rtnl_talk_error(struct nlmsghdr *h, struct nlmsgerr *err,
575 nl_ext_ack_fn_t errfn)
576 {
577 if (nl_dump_ext_ack(h, errfn))
578 return;
579
580 fprintf(stderr, "RTNETLINK answers: %s\n",
581 strerror(-err->error));
582 }
583
584
585 static 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)
588 {
589 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
590 struct iovec riov;
591 struct msghdr msg = {
592 .msg_name = &nladdr,
593 .msg_namelen = sizeof(nladdr),
594 .msg_iov = iov,
595 .msg_iovlen = iovlen,
596 };
597 unsigned int seq = 0;
598 struct nlmsghdr *h;
599 int i, status;
600 char *buf;
601
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 }
608
609 status = sendmsg(rtnl->fd, &msg, 0);
610 if (status < 0) {
611 perror("Cannot talk to rtnetlink");
612 return -1;
613 }
614
615 /* change msg to use the response iov */
616 msg.msg_iov = &riov;
617 msg.msg_iovlen = 1;
618 i = 0;
619 while (1) {
620 next:
621 status = rtnl_recvmsg(rtnl->fd, &msg, &buf);
622 ++i;
623
624 if (status < 0)
625 return status;
626
627 if (msg.msg_namelen != sizeof(nladdr)) {
628 fprintf(stderr,
629 "sender address length == %d\n",
630 msg.msg_namelen);
631 exit(1);
632 }
633 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
634 int len = h->nlmsg_len;
635 int l = len - sizeof(*h);
636
637 if (l < 0 || len > status) {
638 if (msg.msg_flags & MSG_TRUNC) {
639 fprintf(stderr, "Truncated message\n");
640 free(buf);
641 return -1;
642 }
643 fprintf(stderr,
644 "!!!malformed message: len=%d\n",
645 len);
646 exit(1);
647 }
648
649 if (nladdr.nl_pid != 0 ||
650 h->nlmsg_pid != rtnl->local.nl_pid ||
651 h->nlmsg_seq > seq || h->nlmsg_seq < seq - iovlen) {
652 /* Don't forget to skip that message. */
653 status -= NLMSG_ALIGN(len);
654 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
655 continue;
656 }
657
658 if (h->nlmsg_type == NLMSG_ERROR) {
659 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
660
661 if (l < sizeof(struct nlmsgerr)) {
662 fprintf(stderr, "ERROR truncated\n");
663 } else if (!err->error) {
664 /* check messages from kernel */
665 nl_dump_ext_ack(h, errfn);
666
667 if (answer)
668 *answer = (struct nlmsghdr *)buf;
669 else
670 free(buf);
671 if (h->nlmsg_seq == seq)
672 return 0;
673 else if (i < iovlen)
674 goto next;
675 return 0;
676 }
677
678 if (rtnl->proto != NETLINK_SOCK_DIAG &&
679 show_rtnl_err)
680 rtnl_talk_error(h, err, errfn);
681
682 errno = -err->error;
683 free(buf);
684 return -i;
685 }
686
687 if (answer) {
688 *answer = (struct nlmsghdr *)buf;
689 return 0;
690 }
691
692 fprintf(stderr, "Unexpected reply!!!\n");
693
694 status -= NLMSG_ALIGN(len);
695 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
696 }
697 free(buf);
698
699 if (msg.msg_flags & MSG_TRUNC) {
700 fprintf(stderr, "Message truncated\n");
701 continue;
702 }
703
704 if (status) {
705 fprintf(stderr, "!!!Remnant of size %d\n", status);
706 exit(1);
707 }
708 }
709 }
710
711 static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
712 struct nlmsghdr **answer,
713 bool show_rtnl_err, nl_ext_ack_fn_t errfn)
714 {
715 struct iovec iov = {
716 .iov_base = n,
717 .iov_len = n->nlmsg_len
718 };
719
720 return __rtnl_talk_iov(rtnl, &iov, 1, answer, show_rtnl_err, errfn);
721 }
722
723 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
724 struct nlmsghdr **answer)
725 {
726 return __rtnl_talk(rtnl, n, answer, true, NULL);
727 }
728
729 int rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iovec, size_t iovlen,
730 struct nlmsghdr **answer)
731 {
732 return __rtnl_talk_iov(rtnl, iovec, iovlen, answer, true, NULL);
733 }
734
735 int rtnl_talk_extack(struct rtnl_handle *rtnl, struct nlmsghdr *n,
736 struct nlmsghdr **answer,
737 nl_ext_ack_fn_t errfn)
738 {
739 return __rtnl_talk(rtnl, n, answer, true, errfn);
740 }
741
742 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n,
743 struct nlmsghdr **answer)
744 {
745 return __rtnl_talk(rtnl, n, answer, false, NULL);
746 }
747
748 int rtnl_listen_all_nsid(struct rtnl_handle *rth)
749 {
750 unsigned int on = 1;
751
752 if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &on,
753 sizeof(on)) < 0) {
754 perror("NETLINK_LISTEN_ALL_NSID");
755 return -1;
756 }
757 rth->flags |= RTNL_HANDLE_F_LISTEN_ALL_NSID;
758 return 0;
759 }
760
761 int rtnl_listen(struct rtnl_handle *rtnl,
762 rtnl_listen_filter_t handler,
763 void *jarg)
764 {
765 int status;
766 struct nlmsghdr *h;
767 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
768 struct iovec iov;
769 struct msghdr msg = {
770 .msg_name = &nladdr,
771 .msg_namelen = sizeof(nladdr),
772 .msg_iov = &iov,
773 .msg_iovlen = 1,
774 };
775 char buf[16384];
776 char cmsgbuf[BUFSIZ];
777
778 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
779 msg.msg_control = &cmsgbuf;
780 msg.msg_controllen = sizeof(cmsgbuf);
781 }
782
783 iov.iov_base = buf;
784 while (1) {
785 struct rtnl_ctrl_data ctrl;
786 struct cmsghdr *cmsg;
787
788 iov.iov_len = sizeof(buf);
789 status = recvmsg(rtnl->fd, &msg, 0);
790
791 if (status < 0) {
792 if (errno == EINTR || errno == EAGAIN)
793 continue;
794 fprintf(stderr, "netlink receive error %s (%d)\n",
795 strerror(errno), errno);
796 if (errno == ENOBUFS)
797 continue;
798 return -1;
799 }
800 if (status == 0) {
801 fprintf(stderr, "EOF on netlink\n");
802 return -1;
803 }
804 if (msg.msg_namelen != sizeof(nladdr)) {
805 fprintf(stderr,
806 "Sender address length == %d\n",
807 msg.msg_namelen);
808 exit(1);
809 }
810
811 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
812 memset(&ctrl, 0, sizeof(ctrl));
813 ctrl.nsid = -1;
814 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
815 cmsg = CMSG_NXTHDR(&msg, cmsg))
816 if (cmsg->cmsg_level == SOL_NETLINK &&
817 cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID &&
818 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
819 int *data = (int *)CMSG_DATA(cmsg);
820
821 ctrl.nsid = *data;
822 }
823 }
824
825 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
826 int err;
827 int len = h->nlmsg_len;
828 int l = len - sizeof(*h);
829
830 if (l < 0 || len > status) {
831 if (msg.msg_flags & MSG_TRUNC) {
832 fprintf(stderr, "Truncated message\n");
833 return -1;
834 }
835 fprintf(stderr,
836 "!!!malformed message: len=%d\n",
837 len);
838 exit(1);
839 }
840
841 err = handler(&nladdr, &ctrl, h, jarg);
842 if (err < 0)
843 return err;
844
845 status -= NLMSG_ALIGN(len);
846 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
847 }
848 if (msg.msg_flags & MSG_TRUNC) {
849 fprintf(stderr, "Message truncated\n");
850 continue;
851 }
852 if (status) {
853 fprintf(stderr, "!!!Remnant of size %d\n", status);
854 exit(1);
855 }
856 }
857 }
858
859 int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
860 void *jarg)
861 {
862 int status;
863 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
864 char buf[16384];
865 struct nlmsghdr *h = (struct nlmsghdr *)buf;
866
867 while (1) {
868 int err, len;
869 int l;
870
871 status = fread(&buf, 1, sizeof(*h), rtnl);
872
873 if (status < 0) {
874 if (errno == EINTR)
875 continue;
876 perror("rtnl_from_file: fread");
877 return -1;
878 }
879 if (status == 0)
880 return 0;
881
882 len = h->nlmsg_len;
883 l = len - sizeof(*h);
884
885 if (l < 0 || len > sizeof(buf)) {
886 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
887 len, ftell(rtnl));
888 return -1;
889 }
890
891 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
892
893 if (status < 0) {
894 perror("rtnl_from_file: fread");
895 return -1;
896 }
897 if (status < l) {
898 fprintf(stderr, "rtnl-from_file: truncated message\n");
899 return -1;
900 }
901
902 err = handler(&nladdr, NULL, h, jarg);
903 if (err < 0)
904 return err;
905 }
906 }
907
908 int addattr(struct nlmsghdr *n, int maxlen, int type)
909 {
910 return addattr_l(n, maxlen, type, NULL, 0);
911 }
912
913 int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
914 {
915 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
916 }
917
918 int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
919 {
920 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
921 }
922
923 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
924 {
925 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
926 }
927
928 int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
929 {
930 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
931 }
932
933 int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
934 {
935 return addattr_l(n, maxlen, type, str, strlen(str)+1);
936 }
937
938 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
939 int alen)
940 {
941 int len = RTA_LENGTH(alen);
942 struct rtattr *rta;
943
944 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
945 fprintf(stderr,
946 "addattr_l ERROR: message exceeded bound of %d\n",
947 maxlen);
948 return -1;
949 }
950 rta = NLMSG_TAIL(n);
951 rta->rta_type = type;
952 rta->rta_len = len;
953 if (alen)
954 memcpy(RTA_DATA(rta), data, alen);
955 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
956 return 0;
957 }
958
959 int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
960 {
961 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
962 fprintf(stderr,
963 "addraw_l ERROR: message exceeded bound of %d\n",
964 maxlen);
965 return -1;
966 }
967
968 memcpy(NLMSG_TAIL(n), data, len);
969 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
970 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
971 return 0;
972 }
973
974 struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
975 {
976 struct rtattr *nest = NLMSG_TAIL(n);
977
978 addattr_l(n, maxlen, type, NULL, 0);
979 return nest;
980 }
981
982 int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
983 {
984 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
985 return n->nlmsg_len;
986 }
987
988 struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
989 const void *data, int len)
990 {
991 struct rtattr *start = NLMSG_TAIL(n);
992
993 addattr_l(n, maxlen, type, data, len);
994 addattr_nest(n, maxlen, type);
995 return start;
996 }
997
998 int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
999 {
1000 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
1001
1002 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
1003 addattr_nest_end(n, nest);
1004 return n->nlmsg_len;
1005 }
1006
1007 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
1008 {
1009 int len = RTA_LENGTH(4);
1010 struct rtattr *subrta;
1011
1012 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
1013 fprintf(stderr,
1014 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1015 maxlen);
1016 return -1;
1017 }
1018 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
1019 subrta->rta_type = type;
1020 subrta->rta_len = len;
1021 memcpy(RTA_DATA(subrta), &data, 4);
1022 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
1023 return 0;
1024 }
1025
1026 int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
1027 const void *data, int alen)
1028 {
1029 struct rtattr *subrta;
1030 int len = RTA_LENGTH(alen);
1031
1032 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
1033 fprintf(stderr,
1034 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1035 maxlen);
1036 return -1;
1037 }
1038 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
1039 subrta->rta_type = type;
1040 subrta->rta_len = len;
1041 if (alen)
1042 memcpy(RTA_DATA(subrta), data, alen);
1043 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
1044 return 0;
1045 }
1046
1047 int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data)
1048 {
1049 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u8));
1050 }
1051
1052 int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data)
1053 {
1054 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u16));
1055 }
1056
1057 int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data)
1058 {
1059 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u64));
1060 }
1061
1062 struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type)
1063 {
1064 struct rtattr *nest = RTA_TAIL(rta);
1065
1066 rta_addattr_l(rta, maxlen, type, NULL, 0);
1067
1068 return nest;
1069 }
1070
1071 int rta_nest_end(struct rtattr *rta, struct rtattr *nest)
1072 {
1073 nest->rta_len = (void *)RTA_TAIL(rta) - (void *)nest;
1074
1075 return rta->rta_len;
1076 }
1077
1078 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
1079 {
1080 return parse_rtattr_flags(tb, max, rta, len, 0);
1081 }
1082
1083 int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
1084 int len, unsigned short flags)
1085 {
1086 unsigned short type;
1087
1088 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
1089 while (RTA_OK(rta, len)) {
1090 type = rta->rta_type & ~flags;
1091 if ((type <= max) && (!tb[type]))
1092 tb[type] = rta;
1093 rta = RTA_NEXT(rta, len);
1094 }
1095 if (len)
1096 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1097 len, rta->rta_len);
1098 return 0;
1099 }
1100
1101 int parse_rtattr_byindex(struct rtattr *tb[], int max,
1102 struct rtattr *rta, int len)
1103 {
1104 int i = 0;
1105
1106 memset(tb, 0, sizeof(struct rtattr *) * max);
1107 while (RTA_OK(rta, len)) {
1108 if (rta->rta_type <= max && i < max)
1109 tb[i++] = rta;
1110 rta = RTA_NEXT(rta, len);
1111 }
1112 if (len)
1113 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1114 len, rta->rta_len);
1115 return i;
1116 }
1117
1118 struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len)
1119 {
1120 while (RTA_OK(rta, len)) {
1121 if (rta->rta_type == type)
1122 return rta;
1123 rta = RTA_NEXT(rta, len);
1124 }
1125
1126 if (len)
1127 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1128 len, rta->rta_len);
1129 return NULL;
1130 }
1131
1132 int __parse_rtattr_nested_compat(struct rtattr *tb[], int max,
1133 struct rtattr *rta,
1134 int len)
1135 {
1136 if (RTA_PAYLOAD(rta) < len)
1137 return -1;
1138 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
1139 rta = RTA_DATA(rta) + RTA_ALIGN(len);
1140 return parse_rtattr_nested(tb, max, rta);
1141 }
1142 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
1143 return 0;
1144 }