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