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