Debian Codegraph @

lib/libnetlink.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * libnetlink.c RTnetlink service routines.
4 *
5 * Authors: Alexey Kuznetsov, <[email protected]>
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdbool.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <net/if_arp.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <time.h>
19 #include <sys/uio.h>
20 #include <linux/fib_rules.h>
21 #include <linux/if_addrlabel.h>
22 #include <linux/if_bridge.h>
23 #include <linux/nexthop.h>
24
25 #include "libnetlink.h"
26 #include "json_print.h"
27 #include "utils.h"
28
29 #ifndef __aligned
30 #define __aligned(x) __attribute__((aligned(x)))
31 #endif
32
33 #ifndef SOL_NETLINK
34 #define SOL_NETLINK 270
35 #endif
36
37 #ifndef MIN
38 #define MIN(a, b) ((a) < (b) ? (a) : (b))
39 #endif
40
41 int rcvbuf = 1024 * 1024;
42
43 #ifdef HAVE_LIBMNL
44 #include <libmnl/libmnl.h>
45
46 static const enum mnl_attr_data_type extack_policy[NLMSGERR_ATTR_MAX + 1] = {
47 [NLMSGERR_ATTR_MSG] = MNL_TYPE_NUL_STRING,
48 [NLMSGERR_ATTR_OFFS] = MNL_TYPE_U32,
49 };
50
51 static int err_attr_cb(const struct nlattr *attr, void *data)
52 {
53 const struct nlattr **tb = data;
54 uint16_t type;
55
56 if (mnl_attr_type_valid(attr, NLMSGERR_ATTR_MAX) < 0) {
57 fprintf(stderr, "Invalid extack attribute\n");
58 return MNL_CB_ERROR;
59 }
60
61 type = mnl_attr_get_type(attr);
62 if (mnl_attr_validate(attr, extack_policy[type]) < 0) {
63 fprintf(stderr, "extack attribute %d failed validation\n",
64 type);
65 return MNL_CB_ERROR;
66 }
67
68 tb[type] = attr;
69 return MNL_CB_OK;
70 }
71
72 static void print_ext_ack_msg(bool is_err, const char *msg)
73 {
74 fprintf(stderr, "%s: %s", is_err ? "Error" : "Warning", msg);
75 if (msg[strlen(msg) - 1] != '.')
76 fprintf(stderr, ".");
77 fprintf(stderr, "\n");
78 }
79
80 /* dump netlink extended ack error message */
81 int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn)
82 {
83 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {};
84 const struct nlmsgerr *err = mnl_nlmsg_get_payload(nlh);
85 const struct nlmsghdr *err_nlh = NULL;
86 unsigned int hlen = sizeof(*err);
87 const char *msg = NULL;
88 uint32_t off = 0;
89
90 /* no TLVs, nothing to do here */
91 if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
92 return 0;
93
94 /* if NLM_F_CAPPED is set then the inner err msg was capped */
95 if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
96 hlen += mnl_nlmsg_get_payload_len(&err->msg);
97
98 if (mnl_attr_parse(nlh, hlen, err_attr_cb, tb) != MNL_CB_OK)
99 return 0;
100
101 if (tb[NLMSGERR_ATTR_MSG])
102 msg = mnl_attr_get_str(tb[NLMSGERR_ATTR_MSG]);
103
104 if (tb[NLMSGERR_ATTR_OFFS]) {
105 off = mnl_attr_get_u32(tb[NLMSGERR_ATTR_OFFS]);
106
107 if (off > nlh->nlmsg_len) {
108 fprintf(stderr,
109 "Invalid offset for NLMSGERR_ATTR_OFFS\n");
110 off = 0;
111 } else if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
112 err_nlh = &err->msg;
113 }
114
115 if (tb[NLMSGERR_ATTR_MISS_TYPE])
116 fprintf(stderr, "Missing required attribute type %u\n",
117 mnl_attr_get_u32(tb[NLMSGERR_ATTR_MISS_TYPE]));
118
119 if (errfn)
120 return errfn(msg, off, err_nlh);
121
122 if (msg && *msg != '\0') {
123 bool is_err = !!err->error;
124
125 print_ext_ack_msg(is_err, msg);
126 return is_err ? 1 : 0;
127 }
128
129 return 0;
130 }
131
132 int nl_dump_ext_ack_done(const struct nlmsghdr *nlh, unsigned int offset, int error)
133 {
134 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {};
135 const char *msg = NULL;
136
137 if (mnl_attr_parse(nlh, offset, err_attr_cb, tb) != MNL_CB_OK)
138 return 0;
139
140 if (tb[NLMSGERR_ATTR_MSG])
141 msg = mnl_attr_get_str(tb[NLMSGERR_ATTR_MSG]);
142
143 if (msg && *msg != '\0') {
144 bool is_err = !!error;
145
146 print_ext_ack_msg(is_err, msg);
147 return is_err ? 1 : 0;
148 }
149
150 return 0;
151 }
152 #else
153 #warning "libmnl required for error support"
154
155 /* No extended error ack without libmnl */
156 int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn)
157 {
158 return 0;
159 }
160
161 int nl_dump_ext_ack_done(const struct nlmsghdr *nlh, unsigned int offset, int error)
162 {
163 return 0;
164 }
165 #endif
166
167 /* Older kernels may not support strict dump and filtering */
168 void rtnl_set_strict_dump(struct rtnl_handle *rth)
169 {
170 int one = 1;
171
172 if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_GET_STRICT_CHK,
173 &one, sizeof(one)) < 0)
174 return;
175
176 rth->flags |= RTNL_HANDLE_F_STRICT_CHK;
177 }
178
179 int rtnl_add_nl_group(struct rtnl_handle *rth, unsigned int group)
180 {
181 return setsockopt(rth->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
182 &group, sizeof(group));
183 }
184
185 void rtnl_close(struct rtnl_handle *rth)
186 {
187 if (rth->fd >= 0) {
188 close(rth->fd);
189 rth->fd = -1;
190 }
191 }
192
193 int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions,
194 int protocol)
195 {
196 socklen_t addr_len;
197 int sndbuf = 32768;
198 int one = 1;
199
200 memset(rth, 0, sizeof(*rth));
201
202 rth->proto = protocol;
203 rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
204 if (rth->fd < 0) {
205 perror("Cannot open netlink socket");
206 return -1;
207 }
208
209 if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF,
210 &sndbuf, sizeof(sndbuf)) < 0) {
211 perror("SO_SNDBUF");
212 goto err;
213 }
214
215 if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF,
216 &rcvbuf, sizeof(rcvbuf)) < 0) {
217 perror("SO_RCVBUF");
218 goto err;
219 }
220
221 /* Older kernels may no support extended ACK reporting */
222 setsockopt(rth->fd, SOL_NETLINK, NETLINK_EXT_ACK,
223 &one, sizeof(one));
224
225 memset(&rth->local, 0, sizeof(rth->local));
226 rth->local.nl_family = AF_NETLINK;
227 rth->local.nl_groups = subscriptions;
228
229 if (bind(rth->fd, (struct sockaddr *)&rth->local,
230 sizeof(rth->local)) < 0) {
231 perror("Cannot bind netlink socket");
232 goto err;
233 }
234 addr_len = sizeof(rth->local);
235 if (getsockname(rth->fd, (struct sockaddr *)&rth->local,
236 &addr_len) < 0) {
237 perror("Cannot getsockname");
238 goto err;
239 }
240 if (addr_len != sizeof(rth->local)) {
241 fprintf(stderr, "Wrong address length %d\n", addr_len);
242 goto err;
243 }
244 if (rth->local.nl_family != AF_NETLINK) {
245 fprintf(stderr, "Wrong address family %d\n",
246 rth->local.nl_family);
247 goto err;
248 }
249 rth->seq = time(NULL);
250 return 0;
251 err:
252 rtnl_close(rth);
253 return -1;
254 }
255
256 int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
257 {
258 return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
259 }
260
261 int rtnl_nexthopdump_req(struct rtnl_handle *rth, int family,
262 req_filter_fn_t filter_fn)
263 {
264 struct {
265 struct nlmsghdr nlh;
266 struct nhmsg nhm;
267 char buf[128];
268 } req = {
269 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg)),
270 .nlh.nlmsg_type = RTM_GETNEXTHOP,
271 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
272 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
273 .nhm.nh_family = family,
274 };
275
276 if (filter_fn) {
277 int err;
278
279 err = filter_fn(&req.nlh, sizeof(req));
280 if (err)
281 return err;
282 }
283
284 return send(rth->fd, &req, sizeof(req), 0);
285 }
286
287 int rtnl_nexthop_bucket_dump_req(struct rtnl_handle *rth, int family,
288 req_filter_fn_t filter_fn)
289 {
290 struct {
291 struct nlmsghdr nlh;
292 struct nhmsg nhm;
293 char buf[128];
294 } req = {
295 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg)),
296 .nlh.nlmsg_type = RTM_GETNEXTHOPBUCKET,
297 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
298 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
299 .nhm.nh_family = family,
300 };
301
302 if (filter_fn) {
303 int err;
304
305 err = filter_fn(&req.nlh, sizeof(req));
306 if (err)
307 return err;
308 }
309
310 return send(rth->fd, &req, sizeof(req), 0);
311 }
312
313 int rtnl_addrdump_req(struct rtnl_handle *rth, int family,
314 req_filter_fn_t filter_fn)
315 {
316 struct {
317 struct nlmsghdr nlh;
318 struct ifaddrmsg ifm;
319 char buf[128];
320 } req = {
321 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
322 .nlh.nlmsg_type = RTM_GETADDR,
323 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
324 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
325 .ifm.ifa_family = family,
326 };
327
328 if (filter_fn) {
329 int err;
330
331 err = filter_fn(&req.nlh, sizeof(req));
332 if (err)
333 return err;
334 }
335
336 return send(rth->fd, &req, sizeof(req), 0);
337 }
338
339 int rtnl_addrlbldump_req(struct rtnl_handle *rth, int family)
340 {
341 struct {
342 struct nlmsghdr nlh;
343 struct ifaddrlblmsg ifal;
344 } req = {
345 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg)),
346 .nlh.nlmsg_type = RTM_GETADDRLABEL,
347 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
348 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
349 .ifal.ifal_family = family,
350 };
351
352 return send(rth->fd, &req, sizeof(req), 0);
353 }
354
355 int rtnl_routedump_req(struct rtnl_handle *rth, int family,
356 req_filter_fn_t filter_fn)
357 {
358 struct {
359 struct nlmsghdr nlh;
360 struct rtmsg rtm;
361 char buf[128];
362 } req = {
363 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)),
364 .nlh.nlmsg_type = RTM_GETROUTE,
365 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
366 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
367 .rtm.rtm_family = family,
368 };
369
370 if (filter_fn) {
371 int err;
372
373 err = filter_fn(&req.nlh, sizeof(req));
374 if (err)
375 return err;
376 }
377
378 return send(rth->fd, &req, sizeof(req), 0);
379 }
380
381 int rtnl_ruledump_req(struct rtnl_handle *rth, int family)
382 {
383 struct {
384 struct nlmsghdr nlh;
385 struct fib_rule_hdr frh;
386 } req = {
387 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
388 .nlh.nlmsg_type = RTM_GETRULE,
389 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
390 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
391 .frh.family = family
392 };
393
394 return send(rth->fd, &req, sizeof(req), 0);
395 }
396
397 int rtnl_neighdump_req(struct rtnl_handle *rth, int family,
398 req_filter_fn_t filter_fn)
399 {
400 struct {
401 struct nlmsghdr nlh;
402 struct ndmsg ndm;
403 char buf[256];
404 } req = {
405 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)),
406 .nlh.nlmsg_type = RTM_GETNEIGH,
407 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
408 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
409 .ndm.ndm_family = family,
410 };
411
412 if (filter_fn) {
413 int err;
414
415 err = filter_fn(&req.nlh, sizeof(req));
416 if (err)
417 return err;
418 }
419
420 return send(rth->fd, &req, sizeof(req), 0);
421 }
422
423 int rtnl_neightbldump_req(struct rtnl_handle *rth, int family)
424 {
425 struct {
426 struct nlmsghdr nlh;
427 struct ndtmsg ndtmsg;
428 } req = {
429 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndtmsg)),
430 .nlh.nlmsg_type = RTM_GETNEIGHTBL,
431 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
432 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
433 .ndtmsg.ndtm_family = family,
434 };
435
436 return send(rth->fd, &req, sizeof(req), 0);
437 }
438
439 int rtnl_mdbdump_req(struct rtnl_handle *rth, int family)
440 {
441 struct {
442 struct nlmsghdr nlh;
443 struct br_port_msg bpm;
444 } req = {
445 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct br_port_msg)),
446 .nlh.nlmsg_type = RTM_GETMDB,
447 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
448 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
449 .bpm.family = family,
450 };
451
452 return send(rth->fd, &req, sizeof(req), 0);
453 }
454
455 int rtnl_brvlandump_req(struct rtnl_handle *rth, int family, __u32 dump_flags)
456 {
457 struct {
458 struct nlmsghdr nlh;
459 struct br_vlan_msg bvm;
460 char buf[256];
461 } req = {
462 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct br_vlan_msg)),
463 .nlh.nlmsg_type = RTM_GETVLAN,
464 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
465 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
466 .bvm.family = family,
467 };
468
469 addattr32(&req.nlh, sizeof(req), BRIDGE_VLANDB_DUMP_FLAGS, dump_flags);
470
471 return send(rth->fd, &req, sizeof(req), 0);
472 }
473
474 int rtnl_netconfdump_req(struct rtnl_handle *rth, int family)
475 {
476 struct {
477 struct nlmsghdr nlh;
478 struct netconfmsg ncm;
479 char buf[0] __aligned(NLMSG_ALIGNTO);
480 } req = {
481 .nlh.nlmsg_len = NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct netconfmsg))),
482 .nlh.nlmsg_type = RTM_GETNETCONF,
483 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
484 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
485 .ncm.ncm_family = family,
486 };
487
488 return send(rth->fd, &req, sizeof(req), 0);
489 }
490
491 int rtnl_nsiddump_req_filter_fn(struct rtnl_handle *rth, int family,
492 req_filter_fn_t filter_fn)
493 {
494 struct {
495 struct nlmsghdr nlh;
496 struct rtgenmsg rtm;
497 char buf[1024];
498 } req = {
499 .nlh.nlmsg_len = NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct rtgenmsg))),
500 .nlh.nlmsg_type = RTM_GETNSID,
501 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
502 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
503 .rtm.rtgen_family = family,
504 };
505 int err;
506
507 if (!filter_fn)
508 return -EINVAL;
509
510 err = filter_fn(&req.nlh, sizeof(req));
511 if (err)
512 return err;
513
514 return send(rth->fd, &req, req.nlh.nlmsg_len, 0);
515 }
516
517 static int __rtnl_linkdump_req(struct rtnl_handle *rth, int family)
518 {
519 struct {
520 struct nlmsghdr nlh;
521 struct ifinfomsg ifm;
522 } req = {
523 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
524 .nlh.nlmsg_type = RTM_GETLINK,
525 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
526 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
527 .ifm.ifi_family = family,
528 };
529
530 return send(rth->fd, &req, sizeof(req), 0);
531 }
532
533 int rtnl_linkdump_req(struct rtnl_handle *rth, int family)
534 {
535 if (family == AF_UNSPEC)
536 return rtnl_linkdump_req_filter(rth, family, RTEXT_FILTER_VF);
537
538 return __rtnl_linkdump_req(rth, family);
539 }
540
541 int rtnl_linkdump_req_filter(struct rtnl_handle *rth, int family,
542 __u32 filt_mask)
543 {
544 if (family == AF_UNSPEC || family == AF_BRIDGE) {
545 struct {
546 struct nlmsghdr nlh;
547 struct ifinfomsg ifm;
548 /* attribute has to be NLMSG aligned */
549 struct rtattr ext_req __aligned(NLMSG_ALIGNTO);
550 __u32 ext_filter_mask;
551 } req = {
552 .nlh.nlmsg_len = sizeof(req),
553 .nlh.nlmsg_type = RTM_GETLINK,
554 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
555 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
556 .ifm.ifi_family = family,
557 .ext_req.rta_type = IFLA_EXT_MASK,
558 .ext_req.rta_len = RTA_LENGTH(sizeof(__u32)),
559 .ext_filter_mask = filt_mask,
560 };
561
562 return send(rth->fd, &req, sizeof(req), 0);
563 }
564
565 return __rtnl_linkdump_req(rth, family);
566 }
567
568 int rtnl_linkdump_req_filter_fn(struct rtnl_handle *rth, int family,
569 req_filter_fn_t filter_fn)
570 {
571 if (family == AF_UNSPEC || family == AF_PACKET) {
572 struct {
573 struct nlmsghdr nlh;
574 struct ifinfomsg ifm;
575 char buf[1024];
576 } req = {
577 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
578 .nlh.nlmsg_type = RTM_GETLINK,
579 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
580 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
581 .ifm.ifi_family = family,
582 };
583 int err;
584
585 if (!filter_fn)
586 return -EINVAL;
587
588 err = filter_fn(&req.nlh, sizeof(req));
589 if (err)
590 return err;
591
592 return send(rth->fd, &req, req.nlh.nlmsg_len, 0);
593 }
594
595 return __rtnl_linkdump_req(rth, family);
596 }
597
598 int rtnl_fdb_linkdump_req_filter_fn(struct rtnl_handle *rth,
599 req_filter_fn_t filter_fn)
600 {
601 struct {
602 struct nlmsghdr nlh;
603 struct ifinfomsg ifm;
604 char buf[128];
605 } req = {
606 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
607 .nlh.nlmsg_type = RTM_GETNEIGH,
608 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
609 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
610 .ifm.ifi_family = PF_BRIDGE,
611 };
612 int err;
613
614 err = filter_fn(&req.nlh, sizeof(req));
615 if (err)
616 return err;
617
618 return send(rth->fd, &req, sizeof(req), 0);
619 }
620
621 int rtnl_statsdump_req_filter(struct rtnl_handle *rth, int fam,
622 __u32 filt_mask,
623 int (*filter_fn)(struct ipstats_req *req,
624 void *data),
625 void *filter_data)
626 {
627 struct ipstats_req req;
628
629 memset(&req, 0, sizeof(req));
630 req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct if_stats_msg));
631 req.nlh.nlmsg_type = RTM_GETSTATS;
632 req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
633 req.nlh.nlmsg_pid = 0;
634 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
635 req.ifsm.family = fam;
636 req.ifsm.filter_mask = filt_mask;
637
638 if (filter_fn) {
639 int err;
640
641 err = filter_fn(&req, filter_data);
642 if (err)
643 return err;
644 }
645
646 return send(rth->fd, &req, sizeof(req), 0);
647 }
648
649 int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
650 {
651 return send(rth->fd, buf, len, 0);
652 }
653
654 int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
655 {
656 struct nlmsghdr *h;
657 int status;
658 char resp[1024];
659
660 status = send(rth->fd, buf, len, 0);
661 if (status < 0)
662 return status;
663
664 /* Check for immediate errors */
665 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
666 if (status < 0) {
667 if (errno == EAGAIN)
668 return 0;
669 return -1;
670 }
671
672 for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status);
673 h = NLMSG_NEXT(h, status)) {
674 if (h->nlmsg_type == NLMSG_ERROR) {
675 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
676
677 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
678 fprintf(stderr, "ERROR truncated\n");
679 else
680 errno = -err->error;
681 return -1;
682 }
683 }
684
685 return 0;
686 }
687
688 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
689 {
690 struct nlmsghdr nlh = {
691 .nlmsg_len = NLMSG_LENGTH(len),
692 .nlmsg_type = type,
693 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
694 .nlmsg_seq = rth->dump = ++rth->seq,
695 };
696 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
697 struct iovec iov[2] = {
698 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
699 { .iov_base = req, .iov_len = len }
700 };
701 struct msghdr msg = {
702 .msg_name = &nladdr,
703 .msg_namelen = sizeof(nladdr),
704 .msg_iov = iov,
705 .msg_iovlen = 2,
706 };
707
708 return sendmsg(rth->fd, &msg, 0);
709 }
710
711 int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n)
712 {
713 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
714 struct iovec iov = {
715 .iov_base = n,
716 .iov_len = n->nlmsg_len
717 };
718 struct msghdr msg = {
719 .msg_name = &nladdr,
720 .msg_namelen = sizeof(nladdr),
721 .msg_iov = &iov,
722 .msg_iovlen = 1,
723 };
724
725 n->nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
726 n->nlmsg_pid = 0;
727 n->nlmsg_seq = rth->dump = ++rth->seq;
728
729 return sendmsg(rth->fd, &msg, 0);
730 }
731
732 static int rtnl_dump_done(struct nlmsghdr *h,
733 const struct rtnl_dump_filter_arg *a)
734 {
735 int len;
736
737 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(int))) {
738 fprintf(stderr, "DONE truncated\n");
739 return -1;
740 }
741
742 len = *(int *)NLMSG_DATA(h);
743
744 if (len < 0) {
745 errno = -len;
746
747 if (a->errhndlr && (a->errhndlr(h, a->arg2) & RTNL_SUPPRESS_NLMSG_DONE_NLERR))
748 return 0;
749
750 /* check for any messages returned from kernel */
751 if (nl_dump_ext_ack_done(h, sizeof(int), len))
752 return len;
753
754 switch (errno) {
755 case ENOENT:
756 case EOPNOTSUPP:
757 return -1;
758 case EMSGSIZE:
759 fprintf(stderr,
760 "Error: Buffer too small for object.\n");
761 break;
762 default:
763 perror("RTNETLINK answers");
764 }
765 return len;
766 }
767
768 /* check for any messages returned from kernel */
769 nl_dump_ext_ack(h, NULL);
770
771 return 0;
772 }
773
774 static int rtnl_dump_error(const struct rtnl_handle *rth,
775 struct nlmsghdr *h,
776 const struct rtnl_dump_filter_arg *a)
777 {
778
779 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
780 fprintf(stderr, "ERROR truncated\n");
781 } else {
782 const struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
783
784 errno = -err->error;
785 if (rth->proto == NETLINK_SOCK_DIAG &&
786 (errno == ENOENT ||
787 errno == EOPNOTSUPP))
788 return -1;
789
790 if (a->errhndlr && (a->errhndlr(h, a->arg2) & RTNL_SUPPRESS_NLMSG_ERROR_NLERR))
791 return 0;
792
793 if (!(rth->flags & RTNL_HANDLE_F_SUPPRESS_NLERR))
794 perror("RTNETLINK answers");
795 }
796
797 return -1;
798 }
799
800 static int __rtnl_recvmsg(int fd, struct msghdr *msg, int flags)
801 {
802 int len;
803
804 do {
805 len = recvmsg(fd, msg, flags);
806 } while (len < 0 && (errno == EINTR || errno == EAGAIN));
807
808 if (len < 0) {
809 fprintf(stderr, "netlink receive error %s (%d)\n",
810 strerror(errno), errno);
811 return -errno;
812 }
813
814 if (len == 0) {
815 fprintf(stderr, "EOF on netlink\n");
816 return -ENODATA;
817 }
818
819 return len;
820 }
821
822 static int rtnl_recvmsg(int fd, struct msghdr *msg, char **answer)
823 {
824 struct iovec *iov = msg->msg_iov;
825 char *buf;
826 int len;
827
828 iov->iov_base = NULL;
829 iov->iov_len = 0;
830
831 len = __rtnl_recvmsg(fd, msg, MSG_PEEK | MSG_TRUNC);
832 if (len < 0)
833 return len;
834
835 if (len < 32768)
836 len = 32768;
837 buf = malloc(len);
838 if (!buf) {
839 fprintf(stderr, "malloc error: not enough buffer\n");
840 return -ENOMEM;
841 }
842
843 iov->iov_base = buf;
844 iov->iov_len = len;
845
846 len = __rtnl_recvmsg(fd, msg, 0);
847 if (len < 0) {
848 free(buf);
849 return len;
850 }
851
852 if (answer)
853 *answer = buf;
854 else
855 free(buf);
856
857 return len;
858 }
859
860 static int rtnl_dump_filter_l(struct rtnl_handle *rth,
861 const struct rtnl_dump_filter_arg *arg)
862 {
863 struct sockaddr_nl nladdr;
864 struct iovec iov;
865 struct msghdr msg = {
866 .msg_name = &nladdr,
867 .msg_namelen = sizeof(nladdr),
868 .msg_iov = &iov,
869 .msg_iovlen = 1,
870 };
871 char *buf;
872 int dump_intr = 0;
873
874 while (1) {
875 int status;
876 const struct rtnl_dump_filter_arg *a;
877 int found_done = 0;
878 int msglen = 0;
879
880 status = rtnl_recvmsg(rth->fd, &msg, &buf);
881 if (status < 0)
882 return status;
883
884 if (rth->dump_fp)
885 fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp);
886
887 for (a = arg; a->filter; a++) {
888 struct nlmsghdr *h = (struct nlmsghdr *)buf;
889
890 msglen = status;
891
892 while (NLMSG_OK(h, msglen)) {
893 int err = 0;
894
895 h->nlmsg_flags &= ~a->nc_flags;
896
897 if (nladdr.nl_pid != 0 ||
898 h->nlmsg_pid != rth->local.nl_pid ||
899 h->nlmsg_seq != rth->dump)
900 goto skip_it;
901
902 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
903 dump_intr = 1;
904
905 if (h->nlmsg_type == NLMSG_DONE) {
906 err = rtnl_dump_done(h, a);
907 if (err < 0) {
908 free(buf);
909 return -1;
910 }
911
912 found_done = 1;
913 break; /* process next filter */
914 }
915
916 if (h->nlmsg_type == NLMSG_ERROR) {
917 err = rtnl_dump_error(rth, h, a);
918 if (err < 0) {
919 free(buf);
920 return -1;
921 }
922
923 goto skip_it;
924 }
925
926 if (!rth->dump_fp) {
927 err = a->filter(h, a->arg1);
928 if (err < 0) {
929 free(buf);
930 return err;
931 }
932 }
933
934 skip_it:
935 h = NLMSG_NEXT(h, msglen);
936 }
937 }
938 free(buf);
939
940 if (found_done) {
941 if (dump_intr)
942 fprintf(stderr,
943 "Dump was interrupted and may be inconsistent.\n");
944 return 0;
945 }
946
947 if (msg.msg_flags & MSG_TRUNC) {
948 fprintf(stderr, "Message truncated\n");
949 continue;
950 }
951 if (msglen) {
952 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
953 exit(1);
954 }
955 }
956 }
957
958 int rtnl_dump_filter_nc(struct rtnl_handle *rth,
959 rtnl_filter_t filter,
960 void *arg1, __u16 nc_flags)
961 {
962 const struct rtnl_dump_filter_arg a[] = {
963 {
964 .filter = filter, .arg1 = arg1,
965 .nc_flags = nc_flags,
966 },
967 { },
968 };
969
970 return rtnl_dump_filter_l(rth, a);
971 }
972
973 int rtnl_dump_filter_errhndlr_nc(struct rtnl_handle *rth,
974 rtnl_filter_t filter,
975 void *arg1,
976 rtnl_err_hndlr_t errhndlr,
977 void *arg2,
978 __u16 nc_flags)
979 {
980 const struct rtnl_dump_filter_arg a[] = {
981 {
982 .filter = filter, .arg1 = arg1,
983 .errhndlr = errhndlr, .arg2 = arg2,
984 .nc_flags = nc_flags,
985 },
986 { },
987 };
988
989 return rtnl_dump_filter_l(rth, a);
990 }
991
992 static void rtnl_talk_error(struct nlmsghdr *h, struct nlmsgerr *err,
993 nl_ext_ack_fn_t errfn)
994 {
995 if (nl_dump_ext_ack(h, errfn))
996 return;
997
998 fprintf(stderr, "RTNETLINK answers: %s\n",
999 strerror(-err->error));
1000 }
1001
1002
1003 static int __rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iov,
1004 size_t iovlen, struct nlmsghdr **answer,
1005 bool show_rtnl_err, nl_ext_ack_fn_t errfn)
1006 {
1007 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
1008 struct iovec riov;
1009 struct msghdr msg = {
1010 .msg_name = &nladdr,
1011 .msg_namelen = sizeof(nladdr),
1012 .msg_iov = iov,
1013 .msg_iovlen = iovlen,
1014 };
1015 unsigned int seq = 0;
1016 struct nlmsghdr *h;
1017 int i, status;
1018 char *buf;
1019
1020 for (i = 0; i < iovlen; i++) {
1021 h = iov[i].iov_base;
1022 h->nlmsg_seq = seq = ++rtnl->seq;
1023 if (answer == NULL)
1024 h->nlmsg_flags |= NLM_F_ACK;
1025 }
1026
1027 status = sendmsg(rtnl->fd, &msg, 0);
1028 if (status < 0) {
1029 perror("Cannot talk to rtnetlink");
1030 return -1;
1031 }
1032
1033 /* change msg to use the response iov */
1034 msg.msg_iov = &riov;
1035 msg.msg_iovlen = 1;
1036 i = 0;
1037 while (1) {
1038 next:
1039 status = rtnl_recvmsg(rtnl->fd, &msg, &buf);
1040 ++i;
1041
1042 if (status < 0)
1043 return status;
1044
1045 if (msg.msg_namelen != sizeof(nladdr)) {
1046 fprintf(stderr,
1047 "sender address length == %d\n",
1048 msg.msg_namelen);
1049 exit(1);
1050 }
1051 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
1052 int len = h->nlmsg_len;
1053 int l = len - sizeof(*h);
1054
1055 if (l < 0 || len > status) {
1056 if (msg.msg_flags & MSG_TRUNC) {
1057 fprintf(stderr, "Truncated message\n");
1058 free(buf);
1059 return -1;
1060 }
1061 fprintf(stderr,
1062 "!!!malformed message: len=%d\n",
1063 len);
1064 exit(1);
1065 }
1066
1067 if (nladdr.nl_pid != 0 ||
1068 h->nlmsg_pid != rtnl->local.nl_pid ||
1069 h->nlmsg_seq > seq || h->nlmsg_seq < seq - iovlen) {
1070 /* Don't forget to skip that message. */
1071 status -= NLMSG_ALIGN(len);
1072 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
1073 continue;
1074 }
1075
1076 if (h->nlmsg_type == NLMSG_ERROR) {
1077 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
1078 int error = err->error;
1079
1080 if (l < sizeof(struct nlmsgerr)) {
1081 fprintf(stderr, "ERROR truncated\n");
1082 free(buf);
1083 return -1;
1084 }
1085
1086 if (!error) {
1087 /* check messages from kernel */
1088 nl_dump_ext_ack(h, errfn);
1089 } else {
1090 errno = -error;
1091
1092 if (rtnl->proto != NETLINK_SOCK_DIAG &&
1093 show_rtnl_err)
1094 rtnl_talk_error(h, err, errfn);
1095 }
1096
1097 if (i < iovlen) {
1098 free(buf);
1099 goto next;
1100 }
1101
1102 if (error) {
1103 free(buf);
1104 return -i;
1105 }
1106
1107 if (answer)
1108 *answer = (struct nlmsghdr *)buf;
1109 else
1110 free(buf);
1111 return 0;
1112 }
1113
1114 if (answer) {
1115 *answer = (struct nlmsghdr *)buf;
1116 return 0;
1117 }
1118
1119 fprintf(stderr, "Unexpected reply!!!\n");
1120
1121 status -= NLMSG_ALIGN(len);
1122 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
1123 }
1124 free(buf);
1125
1126 if (msg.msg_flags & MSG_TRUNC) {
1127 fprintf(stderr, "Message truncated\n");
1128 continue;
1129 }
1130
1131 if (status) {
1132 fprintf(stderr, "!!!Remnant of size %d\n", status);
1133 exit(1);
1134 }
1135 }
1136 }
1137
1138 static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
1139 struct nlmsghdr **answer,
1140 bool show_rtnl_err, nl_ext_ack_fn_t errfn)
1141 {
1142 struct iovec iov = {
1143 .iov_base = n,
1144 .iov_len = n->nlmsg_len
1145 };
1146
1147 return __rtnl_talk_iov(rtnl, &iov, 1, answer, show_rtnl_err, errfn);
1148 }
1149
1150 int rtnl_echo_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, int use_json,
1151 int (*print_info)(struct nlmsghdr *n, void *arg))
1152 {
1153 struct nlmsghdr *answer;
1154 int ret;
1155
1156 n->nlmsg_flags |= NLM_F_ECHO | NLM_F_ACK;
1157
1158 ret = rtnl_talk(rtnl, n, &answer);
1159 if (ret)
1160 return ret;
1161
1162 new_json_obj(use_json);
1163 open_json_object(NULL);
1164 print_info(answer, stdout);
1165 close_json_object();
1166 delete_json_obj();
1167 free(answer);
1168
1169 return 0;
1170 }
1171
1172 int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
1173 struct nlmsghdr **answer)
1174 {
1175 return __rtnl_talk(rtnl, n, answer, true, NULL);
1176 }
1177
1178 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n,
1179 struct nlmsghdr **answer)
1180 {
1181 return __rtnl_talk(rtnl, n, answer, false, NULL);
1182 }
1183
1184 int rtnl_listen_all_nsid(struct rtnl_handle *rth)
1185 {
1186 unsigned int on = 1;
1187
1188 if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &on,
1189 sizeof(on)) < 0) {
1190 perror("NETLINK_LISTEN_ALL_NSID");
1191 return -1;
1192 }
1193 rth->flags |= RTNL_HANDLE_F_LISTEN_ALL_NSID;
1194 return 0;
1195 }
1196
1197 int rtnl_listen(struct rtnl_handle *rtnl,
1198 rtnl_listen_filter_t handler,
1199 void *jarg)
1200 {
1201 int status;
1202 struct nlmsghdr *h;
1203 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
1204 struct iovec iov;
1205 struct msghdr msg = {
1206 .msg_name = &nladdr,
1207 .msg_namelen = sizeof(nladdr),
1208 .msg_iov = &iov,
1209 .msg_iovlen = 1,
1210 };
1211 char buf[16384];
1212 char cmsgbuf[BUFSIZ];
1213
1214 iov.iov_base = buf;
1215 while (1) {
1216 struct rtnl_ctrl_data ctrl;
1217 struct cmsghdr *cmsg;
1218
1219 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
1220 msg.msg_control = &cmsgbuf;
1221 msg.msg_controllen = sizeof(cmsgbuf);
1222 }
1223
1224 iov.iov_len = sizeof(buf);
1225 status = recvmsg(rtnl->fd, &msg, 0);
1226
1227 if (status < 0) {
1228 if (errno == EINTR || errno == EAGAIN)
1229 continue;
1230 fprintf(stderr, "netlink receive error %s (%d)\n",
1231 strerror(errno), errno);
1232 if (errno == ENOBUFS)
1233 continue;
1234 return -1;
1235 }
1236 if (status == 0) {
1237 fprintf(stderr, "EOF on netlink\n");
1238 return -1;
1239 }
1240 if (msg.msg_namelen != sizeof(nladdr)) {
1241 fprintf(stderr,
1242 "Sender address length == %d\n",
1243 msg.msg_namelen);
1244 exit(1);
1245 }
1246
1247 if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
1248 memset(&ctrl, 0, sizeof(ctrl));
1249 ctrl.nsid = -1;
1250 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1251 cmsg = CMSG_NXTHDR(&msg, cmsg))
1252 if (cmsg->cmsg_level == SOL_NETLINK &&
1253 cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID &&
1254 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
1255 int *data = (int *)CMSG_DATA(cmsg);
1256
1257 ctrl.nsid = *data;
1258 }
1259 }
1260
1261 for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
1262 int err;
1263 int len = h->nlmsg_len;
1264 int l = len - sizeof(*h);
1265
1266 if (l < 0 || len > status) {
1267 if (msg.msg_flags & MSG_TRUNC) {
1268 fprintf(stderr, "Truncated message\n");
1269 return -1;
1270 }
1271 fprintf(stderr,
1272 "!!!malformed message: len=%d\n",
1273 len);
1274 exit(1);
1275 }
1276
1277 err = handler(&ctrl, h, jarg);
1278 if (err < 0)
1279 return err;
1280
1281 status -= NLMSG_ALIGN(len);
1282 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
1283 }
1284 if (msg.msg_flags & MSG_TRUNC) {
1285 fprintf(stderr, "Message truncated\n");
1286 continue;
1287 }
1288 if (status) {
1289 fprintf(stderr, "!!!Remnant of size %d\n", status);
1290 exit(1);
1291 }
1292 }
1293 }
1294
1295 int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
1296 void *jarg)
1297 {
1298 size_t status;
1299 char buf[16384];
1300 struct nlmsghdr *h = (struct nlmsghdr *)buf;
1301
1302 while (1) {
1303 int err, len;
1304 int l;
1305
1306 status = fread(&buf, 1, sizeof(*h), rtnl);
1307
1308 if (status == 0 && feof(rtnl))
1309 return 0;
1310 if (status != sizeof(*h)) {
1311 if (ferror(rtnl))
1312 perror("rtnl_from_file: fread");
1313 if (feof(rtnl))
1314 fprintf(stderr, "rtnl-from_file: truncated message\n");
1315 return -1;
1316 }
1317
1318 len = h->nlmsg_len;
1319 l = len - sizeof(*h);
1320
1321 if (l < 0 || len > sizeof(buf)) {
1322 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
1323 len, ftell(rtnl));
1324 return -1;
1325 }
1326
1327 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
1328
1329 if (status != NLMSG_ALIGN(l)) {
1330 if (ferror(rtnl))
1331 perror("rtnl_from_file: fread");
1332 if (feof(rtnl))
1333 fprintf(stderr, "rtnl-from_file: truncated message\n");
1334 return -1;
1335 }
1336
1337 err = handler(NULL, h, jarg);
1338 if (err < 0)
1339 return err;
1340 }
1341 }
1342
1343 int addattr(struct nlmsghdr *n, int maxlen, int type)
1344 {
1345 return addattr_l(n, maxlen, type, NULL, 0);
1346 }
1347
1348 int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
1349 {
1350 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
1351 }
1352
1353 int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
1354 {
1355 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
1356 }
1357
1358 int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
1359 {
1360 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
1361 }
1362
1363 int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
1364 {
1365 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
1366 }
1367
1368 int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
1369 {
1370 return addattr_l(n, maxlen, type, str, strlen(str)+1);
1371 }
1372
1373 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
1374 int alen)
1375 {
1376 int len = RTA_LENGTH(alen);
1377 struct rtattr *rta;
1378
1379 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
1380 fprintf(stderr,
1381 "addattr_l ERROR: message exceeded bound of %d\n",
1382 maxlen);
1383 return -1;
1384 }
1385 rta = NLMSG_TAIL(n);
1386 rta->rta_type = type;
1387 rta->rta_len = len;
1388 if (alen)
1389 memcpy(RTA_DATA(rta), data, alen);
1390 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
1391 return 0;
1392 }
1393
1394 int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
1395 {
1396 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
1397 fprintf(stderr,
1398 "addraw_l ERROR: message exceeded bound of %d\n",
1399 maxlen);
1400 return -1;
1401 }
1402
1403 memcpy(NLMSG_TAIL(n), data, len);
1404 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
1405 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
1406 return 0;
1407 }
1408
1409 struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
1410 {
1411 struct rtattr *nest = NLMSG_TAIL(n);
1412
1413 addattr_l(n, maxlen, type, NULL, 0);
1414 return nest;
1415 }
1416
1417 int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
1418 {
1419 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
1420 return n->nlmsg_len;
1421 }
1422
1423 struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
1424 const void *data, int len)
1425 {
1426 struct rtattr *start = NLMSG_TAIL(n);
1427
1428 addattr_l(n, maxlen, type, data, len);
1429 addattr_nest(n, maxlen, type);
1430 return start;
1431 }
1432
1433 int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
1434 {
1435 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
1436
1437 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
1438 addattr_nest_end(n, nest);
1439 return n->nlmsg_len;
1440 }
1441
1442 int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
1443 {
1444 int len = RTA_LENGTH(4);
1445 struct rtattr *subrta;
1446
1447 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
1448 fprintf(stderr,
1449 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1450 maxlen);
1451 return -1;
1452 }
1453 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
1454 subrta->rta_type = type;
1455 subrta->rta_len = len;
1456 memcpy(RTA_DATA(subrta), &data, 4);
1457 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
1458 return 0;
1459 }
1460
1461 int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
1462 const void *data, int alen)
1463 {
1464 struct rtattr *subrta;
1465 int len = RTA_LENGTH(alen);
1466
1467 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
1468 fprintf(stderr,
1469 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1470 maxlen);
1471 return -1;
1472 }
1473 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
1474 subrta->rta_type = type;
1475 subrta->rta_len = len;
1476 if (alen)
1477 memcpy(RTA_DATA(subrta), data, alen);
1478 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
1479 return 0;
1480 }
1481
1482 int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data)
1483 {
1484 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u8));
1485 }
1486
1487 int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data)
1488 {
1489 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u16));
1490 }
1491
1492 int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data)
1493 {
1494 return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u64));
1495 }
1496
1497 struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type)
1498 {
1499 struct rtattr *nest = RTA_TAIL(rta);
1500
1501 rta_addattr_l(rta, maxlen, type, NULL, 0);
1502 nest->rta_type |= NLA_F_NESTED;
1503
1504 return nest;
1505 }
1506
1507 int rta_nest_end(struct rtattr *rta, struct rtattr *nest)
1508 {
1509 nest->rta_len = (void *)RTA_TAIL(rta) - (void *)nest;
1510
1511 return rta->rta_len;
1512 }
1513
1514 int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
1515 {
1516 return parse_rtattr_flags(tb, max, rta, len, 0);
1517 }
1518
1519 int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
1520 int len, unsigned short flags)
1521 {
1522 unsigned short type;
1523
1524 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
1525 while (RTA_OK(rta, len)) {
1526 type = rta->rta_type & ~flags;
1527 if ((type <= max) && (!tb[type]))
1528 tb[type] = rta;
1529 rta = RTA_NEXT(rta, len);
1530 }
1531 if (len)
1532 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1533 len, rta->rta_len);
1534 return 0;
1535 }
1536
1537 struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len)
1538 {
1539 while (RTA_OK(rta, len)) {
1540 if (rta->rta_type == type)
1541 return rta;
1542 rta = RTA_NEXT(rta, len);
1543 }
1544
1545 if (len)
1546 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
1547 len, rta->rta_len);
1548 return NULL;
1549 }
1550
1551 int __parse_rtattr_nested_compat(struct rtattr *tb[], int max,
1552 struct rtattr *rta,
1553 int len)
1554 {
1555 if (RTA_PAYLOAD(rta) < len)
1556 return -1;
1557 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
1558 rta = RTA_DATA(rta) + RTA_ALIGN(len);
1559 return parse_rtattr_nested(tb, max, rta);
1560 }
1561 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
1562 return 0;
1563 }
1564
1565 int rtnl_tunneldump_req(struct rtnl_handle *rth, int family, int ifindex,
1566 __u8 flags)
1567 {
1568 struct {
1569 struct nlmsghdr nlh;
1570 struct tunnel_msg tmsg;
1571 char buf[256];
1572 } req = {
1573 .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tunnel_msg)),
1574 .nlh.nlmsg_type = RTM_GETTUNNEL,
1575 .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
1576 .nlh.nlmsg_seq = rth->dump = ++rth->seq,
1577 .tmsg.family = family,
1578 .tmsg.flags = flags,
1579 .tmsg.ifindex = ifindex,
1580 };
1581
1582 return send(rth->fd, &req, sizeof(req), 0);
1583 }