1
2
3
4
5
6
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <fcntl.h>
12#include <inttypes.h>
13#include <sys/ioctl.h>
14#include <sys/socket.h>
15#include <netinet/in.h>
16#include <arpa/inet.h>
17#include <string.h>
18
19#include <linux/netdevice.h>
20#include <linux/if.h>
21#include <linux/if_arp.h>
22#include <linux/sockios.h>
23
24#include <rt_names.h>
25#include "utils.h"
26#include "ip_common.h"
27#include "json_print.h"
28
29static void usage(void) __attribute__((noreturn));
30
31static void usage(void)
32{
33 fprintf(stderr,
34 "Usage: ip mroute show [ [ to ] PREFIX ] [ from PREFIX ] [ iif DEVICE ]\n"
35 " [ table TABLE_ID ]\n"
36 "TABLE_ID := [ local | main | default | all | NUMBER ]\n"
37 );
38 exit(-1);
39}
40
41static struct rtfilter {
42 int tb;
43 int af;
44 int iif;
45 inet_prefix mdst;
46 inet_prefix msrc;
47} filter;
48
49int print_mroute(struct nlmsghdr *n, void *arg)
50{
51 struct rtmsg *r = NLMSG_DATA(n);
52 int len = n->nlmsg_len;
53 struct rtattr *tb[RTA_MAX+1];
54 FILE *fp = arg;
55 const char *src, *dst;
56 SPRINT_BUF(b1);
57 SPRINT_BUF(b2);
58 __u32 table;
59 int iif = 0;
60 int family;
61
62 if ((n->nlmsg_type != RTM_NEWROUTE &&
63 n->nlmsg_type != RTM_DELROUTE)) {
64 fprintf(stderr, "Not a multicast route: %08x %08x %08x\n",
65 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
66 return 0;
67 }
68 len -= NLMSG_LENGTH(sizeof(*r));
69 if (len < 0) {
70 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
71 return -1;
72 }
73
74 if (r->rtm_type != RTN_MULTICAST) {
75 fprintf(stderr,
76 "Non multicast route received, kernel does support IP multicast?\n");
77 return -1;
78 }
79
80 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
81 table = rtm_get_table(r, tb);
82
83 if (filter.tb > 0 && filter.tb != table)
84 return 0;
85
86 if (tb[RTA_IIF])
87 iif = rta_getattr_u32(tb[RTA_IIF]);
88 if (filter.iif && filter.iif != iif)
89 return 0;
90
91 if (filter.af && filter.af != r->rtm_family)
92 return 0;
93
94 if (inet_addr_match_rta(&filter.mdst, tb[RTA_DST]))
95 return 0;
96
97 if (inet_addr_match_rta(&filter.msrc, tb[RTA_SRC]))
98 return 0;
99
100 print_headers(fp, "[MROUTE]");
101
102 family = get_real_family(r->rtm_type, r->rtm_family);
103
104 open_json_object(NULL);
105 if (n->nlmsg_type == RTM_DELROUTE)
106 print_bool(PRINT_ANY, "deleted", "Deleted ", true);
107
108 if (tb[RTA_SRC])
109 src = rt_addr_n2a_r(family, RTA_PAYLOAD(tb[RTA_SRC]),
110 RTA_DATA(tb[RTA_SRC]), b1, sizeof(b1));
111 else
112 src = "unknown";
113
114 if (tb[RTA_DST])
115 dst = rt_addr_n2a_r(family, RTA_PAYLOAD(tb[RTA_DST]),
116 RTA_DATA(tb[RTA_DST]), b2, sizeof(b2));
117 else
118 dst = "unknown";
119
120 if (is_json_context()) {
121 print_string(PRINT_JSON, "src", NULL, src);
122 print_string(PRINT_JSON, "dst", NULL, dst);
123 } else {
124 char obuf[256];
125
126 snprintf(obuf, sizeof(obuf), "(%s,%s)", src, dst);
127 print_string(PRINT_FP, NULL,
128 "%-32s Iif: ", obuf);
129 }
130
131 if (iif)
132 print_color_string(PRINT_ANY, COLOR_IFNAME,
133 "iif", "%-10s ", ll_index_to_name(iif));
134 else
135 print_string(PRINT_ANY,"iif", "%s ", "unresolved");
136
137 if (tb[RTA_MULTIPATH]) {
138 struct rtnexthop *nh = RTA_DATA(tb[RTA_MULTIPATH]);
139 int first = 1;
140
141 open_json_array(PRINT_JSON, "multipath");
142 len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
143
144 for (;;) {
145 if (len < sizeof(*nh))
146 break;
147 if (nh->rtnh_len > len)
148 break;
149
150 open_json_object(NULL);
151 if (first) {
152 print_string(PRINT_FP, NULL, "Oifs: ", NULL);
153 first = 0;
154 }
155
156 print_color_string(PRINT_ANY, COLOR_IFNAME,
157 "oif", "%s", ll_index_to_name(nh->rtnh_ifindex));
158
159 if (nh->rtnh_hops > 1)
160 print_uint(PRINT_ANY,
161 "ttl", "(ttl %u) ", nh->rtnh_hops);
162 else
163 print_string(PRINT_FP, NULL, " ", NULL);
164
165 close_json_object();
166 len -= NLMSG_ALIGN(nh->rtnh_len);
167 nh = RTNH_NEXT(nh);
168 }
169 close_json_array(PRINT_JSON, NULL);
170 }
171
172 print_string(PRINT_ANY, "state", " State: %s",
173 (r->rtm_flags & RTNH_F_UNRESOLVED) ? "unresolved" : "resolved");
174
175 if (r->rtm_flags & RTNH_F_OFFLOAD)
176 print_null(PRINT_ANY, "offload", " offload", NULL);
177
178 if (show_stats && tb[RTA_MFC_STATS]) {
179 struct rta_mfc_stats *mfcs = RTA_DATA(tb[RTA_MFC_STATS]);
180
181 print_nl();
182 print_u64(PRINT_ANY, "packets", " %"PRIu64" packets,",
183 mfcs->mfcs_packets);
184 print_u64(PRINT_ANY, "bytes", " %"PRIu64" bytes", mfcs->mfcs_bytes);
185
186 if (mfcs->mfcs_wrong_if)
187 print_u64(PRINT_ANY, "wrong_if",
188 ", %"PRIu64" arrived on wrong iif.",
189 mfcs->mfcs_wrong_if);
190 }
191
192 if (show_stats && tb[RTA_EXPIRES]) {
193 struct timeval tv;
194 double age;
195
196 __jiffies_to_tv(&tv, rta_getattr_u64(tb[RTA_EXPIRES]));
197 age = tv.tv_sec;
198 age += tv.tv_usec / 1000000.;
199 print_float(PRINT_ANY, "expires",
200 ", Age %.2f", age);
201 }
202
203 if (table && (table != RT_TABLE_MAIN || show_details > 0) && !filter.tb)
204 print_string(PRINT_ANY, "table", " Table: %s",
205 rtnl_rttable_n2a(table, b1, sizeof(b1)));
206
207 print_string(PRINT_FP, NULL, "\n", NULL);
208 close_json_object();
209 fflush(fp);
210 return 0;
211}
212
213void ipmroute_reset_filter(int ifindex)
214{
215 memset(&filter, 0, sizeof(filter));
216 filter.mdst.bitlen = -1;
217 filter.msrc.bitlen = -1;
218 filter.iif = ifindex;
219}
220
221static int iproute_dump_filter(struct nlmsghdr *nlh, int reqlen)
222{
223 int err;
224
225 if (filter.tb) {
226 err = addattr32(nlh, reqlen, RTA_TABLE, filter.tb);
227 if (err)
228 return err;
229 }
230
231 return 0;
232}
233
234static int mroute_list(int argc, char **argv)
235{
236 char *id = NULL;
237 int family = preferred_family;
238
239 ipmroute_reset_filter(0);
240 if (family == AF_INET || family == AF_UNSPEC) {
241 family = RTNL_FAMILY_IPMR;
242 filter.af = RTNL_FAMILY_IPMR;
243 filter.tb = RT_TABLE_DEFAULT;
244 } else if (family == AF_INET6) {
245 family = RTNL_FAMILY_IP6MR;
246 filter.af = RTNL_FAMILY_IP6MR;
247 } else {
248
249 return 0;
250 }
251
252 filter.msrc.family = filter.mdst.family = family;
253
254 while (argc > 0) {
255 if (matches(*argv, "table") == 0) {
256 __u32 tid;
257
258 NEXT_ARG();
259 if (rtnl_rttable_a2n(&tid, *argv)) {
260 if (strcmp(*argv, "all") == 0) {
261 filter.tb = 0;
262 } else if (strcmp(*argv, "help") == 0) {
263 usage();
264 } else {
265 invarg("table id value is invalid\n", *argv);
266 }
267 } else
268 filter.tb = tid;
269 } else if (strcmp(*argv, "iif") == 0) {
270 NEXT_ARG();
271 id = *argv;
272 } else if (matches(*argv, "from") == 0) {
273 NEXT_ARG();
274 if (get_prefix(&filter.msrc, *argv, preferred_family))
275 invarg("from value is invalid\n", *argv);
276 } else {
277 if (strcmp(*argv, "to") == 0) {
278 NEXT_ARG();
279 }
280 if (matches(*argv, "help") == 0)
281 usage();
282 if (get_prefix(&filter.mdst, *argv, preferred_family))
283 invarg("to value is invalid\n", *argv);
284 }
285 argc--; argv++;
286 }
287
288 ll_init_map(&rth);
289
290 if (id) {
291 int idx;
292
293 idx = ll_name_to_index(id);
294 if (!idx)
295 return nodev(id);
296 filter.iif = idx;
297 }
298
299 if (rtnl_routedump_req(&rth, filter.af, iproute_dump_filter) < 0) {
300 perror("Cannot send dump request");
301 return 1;
302 }
303
304 new_json_obj(json);
305 if (rtnl_dump_filter(&rth, print_mroute, stdout) < 0) {
306 delete_json_obj();
307 fprintf(stderr, "Dump terminated\n");
308 exit(1);
309 }
310 delete_json_obj();
311
312 return 0;
313}
314
315int do_multiroute(int argc, char **argv)
316{
317 if (argc < 1)
318 return mroute_list(0, NULL);
319
320 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
321 || matches(*argv, "lst") == 0)
322 return mroute_list(argc-1, argv+1);
323 if (matches(*argv, "help") == 0)
324 usage();
325 fprintf(stderr, "Command \"%s\" is unknown, try \"ip mroute help\".\n", *argv);
326 exit(-1);
327}
328