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