]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipmroute.c
ipmroute: Prevent overlapping storage of `filter` global
[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 static 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(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 FILE *fp = arg;
61 const char *src, *dst;
62 SPRINT_BUF(b1);
63 SPRINT_BUF(b2);
64 __u32 table;
65 int iif = 0;
66 int family;
67
68 if ((n->nlmsg_type != RTM_NEWROUTE &&
69 n->nlmsg_type != RTM_DELROUTE)) {
70 fprintf(stderr, "Not a multicast route: %08x %08x %08x\n",
71 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
72 return 0;
73 }
74 len -= NLMSG_LENGTH(sizeof(*r));
75 if (len < 0) {
76 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
77 return -1;
78 }
79
80 if (r->rtm_type != RTN_MULTICAST) {
81 fprintf(stderr,
82 "Non multicast route received, kernel does support IP multicast?\n");
83 return -1;
84 }
85
86 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
87 table = rtm_get_table(r, tb);
88
89 if (filter.tb > 0 && filter.tb != table)
90 return 0;
91
92 if (tb[RTA_IIF])
93 iif = rta_getattr_u32(tb[RTA_IIF]);
94 if (filter.iif && filter.iif != iif)
95 return 0;
96
97 if (filter.af && filter.af != r->rtm_family)
98 return 0;
99
100 if (inet_addr_match_rta(&filter.mdst, tb[RTA_DST]))
101 return 0;
102
103 if (inet_addr_match_rta(&filter.msrc, tb[RTA_SRC]))
104 return 0;
105
106 family = get_real_family(r->rtm_type, r->rtm_family);
107
108 open_json_object(NULL);
109 if (n->nlmsg_type == RTM_DELROUTE)
110 print_bool(PRINT_ANY, "deleted", "Deleted ", true);
111
112 if (tb[RTA_SRC])
113 src = rt_addr_n2a_r(family, RTA_PAYLOAD(tb[RTA_SRC]),
114 RTA_DATA(tb[RTA_SRC]), b1, sizeof(b1));
115 else
116 src = "unknown";
117
118 if (tb[RTA_DST])
119 dst = rt_addr_n2a_r(family, RTA_PAYLOAD(tb[RTA_DST]),
120 RTA_DATA(tb[RTA_DST]), b2, sizeof(b2));
121 else
122 dst = "unknown";
123
124 if (is_json_context()) {
125 print_string(PRINT_JSON, "src", NULL, src);
126 print_string(PRINT_JSON, "dst", NULL, dst);
127 } else {
128 char obuf[256];
129
130 snprintf(obuf, sizeof(obuf), "(%s,%s)", src, dst);
131 print_string(PRINT_FP, NULL,
132 "%-32s Iif: ", obuf);
133 }
134
135 if (iif)
136 print_color_string(PRINT_ANY, COLOR_IFNAME,
137 "iif", "%-10s ", ll_index_to_name(iif));
138 else
139 print_string(PRINT_ANY,"iif", "%s ", "unresolved");
140
141 if (tb[RTA_MULTIPATH]) {
142 struct rtnexthop *nh = RTA_DATA(tb[RTA_MULTIPATH]);
143 int first = 1;
144
145 open_json_array(PRINT_JSON, "multipath");
146 len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
147
148 for (;;) {
149 if (len < sizeof(*nh))
150 break;
151 if (nh->rtnh_len > len)
152 break;
153
154 open_json_object(NULL);
155 if (first) {
156 print_string(PRINT_FP, NULL, "Oifs: ", NULL);
157 first = 0;
158 }
159
160 print_color_string(PRINT_ANY, COLOR_IFNAME,
161 "oif", "%s", ll_index_to_name(nh->rtnh_ifindex));
162
163 if (nh->rtnh_hops > 1)
164 print_uint(PRINT_ANY,
165 "ttl", "(ttl %u) ", nh->rtnh_hops);
166 else
167 print_string(PRINT_FP, NULL, " ", NULL);
168
169 close_json_object();
170 len -= NLMSG_ALIGN(nh->rtnh_len);
171 nh = RTNH_NEXT(nh);
172 }
173 close_json_array(PRINT_JSON, NULL);
174 }
175
176 print_string(PRINT_ANY, "state", " State: %s",
177 (r->rtm_flags & RTNH_F_UNRESOLVED) ? "unresolved" : "resolved");
178
179 if (r->rtm_flags & RTNH_F_OFFLOAD)
180 print_null(PRINT_ANY, "offload", " offload", NULL);
181
182 if (show_stats && tb[RTA_MFC_STATS]) {
183 struct rta_mfc_stats *mfcs = RTA_DATA(tb[RTA_MFC_STATS]);
184
185 print_nl();
186 print_u64(PRINT_ANY, "packets", " %"PRIu64" packets,",
187 mfcs->mfcs_packets);
188 print_u64(PRINT_ANY, "bytes", " %"PRIu64" bytes", mfcs->mfcs_bytes);
189
190 if (mfcs->mfcs_wrong_if)
191 print_u64(PRINT_ANY, "wrong_if",
192 ", %"PRIu64" arrived on wrong iif.",
193 mfcs->mfcs_wrong_if);
194 }
195
196 if (show_stats && tb[RTA_EXPIRES]) {
197 struct timeval tv;
198 double age;
199
200 __jiffies_to_tv(&tv, rta_getattr_u64(tb[RTA_EXPIRES]));
201 age = tv.tv_sec;
202 age += tv.tv_usec / 1000000.;
203 print_float(PRINT_ANY, "expires",
204 ", Age %.2f", age);
205 }
206
207 if (table && (table != RT_TABLE_MAIN || show_details > 0) && !filter.tb)
208 print_string(PRINT_ANY, "table", " Table: %s",
209 rtnl_rttable_n2a(table, b1, sizeof(b1)));
210
211 print_string(PRINT_FP, NULL, "\n", NULL);
212 close_json_object();
213 fflush(fp);
214 return 0;
215 }
216
217 void ipmroute_reset_filter(int ifindex)
218 {
219 memset(&filter, 0, sizeof(filter));
220 filter.mdst.bitlen = -1;
221 filter.msrc.bitlen = -1;
222 filter.iif = ifindex;
223 }
224
225 static int iproute_dump_filter(struct nlmsghdr *nlh, int reqlen)
226 {
227 int err;
228
229 if (filter.tb) {
230 err = addattr32(nlh, reqlen, RTA_TABLE, filter.tb);
231 if (err)
232 return err;
233 }
234
235 return 0;
236 }
237
238 static int mroute_list(int argc, char **argv)
239 {
240 char *id = NULL;
241 int family = preferred_family;
242
243 ipmroute_reset_filter(0);
244 if (family == AF_INET || family == AF_UNSPEC) {
245 family = RTNL_FAMILY_IPMR;
246 filter.af = RTNL_FAMILY_IPMR;
247 filter.tb = RT_TABLE_DEFAULT; /* for backward compatibility */
248 } else if (family == AF_INET6) {
249 family = RTNL_FAMILY_IP6MR;
250 filter.af = RTNL_FAMILY_IP6MR;
251 } else {
252 /* family does not have multicast routing */
253 return 0;
254 }
255
256 filter.msrc.family = filter.mdst.family = family;
257
258 while (argc > 0) {
259 if (matches(*argv, "table") == 0) {
260 __u32 tid;
261
262 NEXT_ARG();
263 if (rtnl_rttable_a2n(&tid, *argv)) {
264 if (strcmp(*argv, "all") == 0) {
265 filter.tb = 0;
266 } else if (strcmp(*argv, "help") == 0) {
267 usage();
268 } else {
269 invarg("table id value is invalid\n", *argv);
270 }
271 } else
272 filter.tb = tid;
273 } else if (strcmp(*argv, "iif") == 0) {
274 NEXT_ARG();
275 id = *argv;
276 } else if (matches(*argv, "from") == 0) {
277 NEXT_ARG();
278 if (get_prefix(&filter.msrc, *argv, family))
279 invarg("from value is invalid\n", *argv);
280 } else {
281 if (strcmp(*argv, "to") == 0) {
282 NEXT_ARG();
283 }
284 if (matches(*argv, "help") == 0)
285 usage();
286 if (get_prefix(&filter.mdst, *argv, family))
287 invarg("to value is invalid\n", *argv);
288 }
289 argc--; argv++;
290 }
291
292 ll_init_map(&rth);
293
294 if (id) {
295 int idx;
296
297 idx = ll_name_to_index(id);
298 if (!idx)
299 return nodev(id);
300 filter.iif = idx;
301 }
302
303 if (rtnl_routedump_req(&rth, filter.af, iproute_dump_filter) < 0) {
304 perror("Cannot send dump request");
305 return 1;
306 }
307
308 new_json_obj(json);
309 if (rtnl_dump_filter(&rth, print_mroute, stdout) < 0) {
310 delete_json_obj();
311 fprintf(stderr, "Dump terminated\n");
312 exit(1);
313 }
314 delete_json_obj();
315
316 return 0;
317 }
318
319 int do_multiroute(int argc, char **argv)
320 {
321 if (argc < 1)
322 return mroute_list(0, NULL);
323 #if 0
324 if (matches(*argv, "add") == 0)
325 return mroute_modify(RTM_NEWADDR, argc-1, argv+1);
326 if (matches(*argv, "delete") == 0)
327 return mroute_modify(RTM_DELADDR, argc-1, argv+1);
328 if (matches(*argv, "get") == 0)
329 return mroute_get(argc-1, argv+1);
330 #endif
331 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
332 || matches(*argv, "lst") == 0)
333 return mroute_list(argc-1, argv+1);
334 if (matches(*argv, "help") == 0)
335 usage();
336 fprintf(stderr, "Command \"%s\" is unknown, try \"ip mroute help\".\n", *argv);
337 exit(-1);
338 }