]> git.proxmox.com Git - mirror_frr.git/blob - pimd/mtracebis_routeget.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / pimd / mtracebis_routeget.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Multicast Traceroute for FRRouting
4 * Copyright (C) 2018 Mladen Sablic
5 */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #ifdef __linux__
12
13 #include <asm/types.h>
14 #include <linux/netlink.h>
15 #include <linux/rtnetlink.h>
16 #include <sys/types.h>
17 #include <stdio.h>
18 #include <arpa/inet.h>
19 #include <string.h>
20
21 #include "mtracebis_netlink.h"
22 #include "mtracebis_routeget.h"
23
24 static int find_dst(struct nlmsghdr *n, struct in_addr *src, struct in_addr *gw)
25 {
26 struct rtmsg *r = NLMSG_DATA(n);
27 int len = n->nlmsg_len;
28 struct rtattr *tb[RTA_MAX + 1];
29
30 len -= NLMSG_LENGTH(sizeof(*r));
31 if (len < 0) {
32 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
33 return -1;
34 }
35
36 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
37 if (tb[RTA_PREFSRC])
38 src->s_addr = *(uint32_t *)RTA_DATA(tb[RTA_PREFSRC]);
39 if (tb[RTA_GATEWAY])
40 gw->s_addr = *(uint32_t *)RTA_DATA(tb[RTA_GATEWAY]);
41 if (tb[RTA_OIF])
42 return *(int *)RTA_DATA(tb[RTA_OIF]);
43 return 0;
44 }
45
46 int routeget(struct in_addr dst, struct in_addr *src, struct in_addr *gw)
47 {
48 struct {
49 struct nlmsghdr n;
50 struct rtmsg r;
51 char buf[1024];
52 } req;
53 int ret;
54 struct rtnl_handle rth = {.fd = -1};
55
56 memset(&req, 0, sizeof(req));
57
58 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
59 req.n.nlmsg_flags = NLM_F_REQUEST;
60 req.n.nlmsg_type = RTM_GETROUTE;
61 req.r.rtm_family = AF_INET;
62 req.r.rtm_table = 0;
63 req.r.rtm_protocol = 0;
64 req.r.rtm_scope = 0;
65 req.r.rtm_type = 0;
66 req.r.rtm_src_len = 0;
67 req.r.rtm_dst_len = 0;
68 req.r.rtm_tos = 0;
69
70 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.s_addr, 4);
71 req.r.rtm_dst_len = 32;
72
73 ret = rtnl_open(&rth, 0);
74
75 if (ret < 0 || rth.fd <= 0)
76 return ret;
77
78 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
79 ret = -1;
80 goto close_rth;
81 }
82
83 ret = find_dst(&req.n, src, gw);
84 close_rth:
85 rtnl_close(&rth);
86 return ret;
87 }
88
89 #endif /* __linux__ */