]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipmroute.c
2 * ipmroute.c "ip mroute".
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 #include <linux/netdevice.h>
26 #include <linux/if_arp.h>
27 #include <linux/sockios.h>
31 #include "ip_common.h"
32 #include "json_print.h"
34 static void usage(void) __attribute__((noreturn
));
36 static void usage(void)
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");
42 fprintf(stderr
, "Usage: ip mroute [ add | del ] DESTINATION from SOURCE [ iif DEVICE ] [ oif DEVICE ]\n");
55 int print_mroute(struct nlmsghdr
*n
, void *arg
)
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
;
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
);
73 len
-= NLMSG_LENGTH(sizeof(*r
));
75 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
79 if (r
->rtm_type
!= RTN_MULTICAST
) {
81 "Non multicast route received, kernel does support IP multicast?\n");
85 parse_rtattr(tb
, RTA_MAX
, RTM_RTA(r
), len
);
86 table
= rtm_get_table(r
, tb
);
88 if (filter
.tb
> 0 && filter
.tb
!= table
)
92 iif
= rta_getattr_u32(tb
[RTA_IIF
]);
93 if (filter
.iif
&& filter
.iif
!= iif
)
96 if (filter
.af
&& filter
.af
!= r
->rtm_family
)
99 if (inet_addr_match_rta(&filter
.mdst
, tb
[RTA_DST
]))
102 if (inet_addr_match_rta(&filter
.msrc
, tb
[RTA_SRC
]))
105 family
= get_real_family(r
->rtm_type
, r
->rtm_family
);
107 open_json_object(NULL
);
108 if (n
->nlmsg_type
== RTM_DELROUTE
)
109 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
112 src
= rt_addr_n2a_r(family
, RTA_PAYLOAD(tb
[RTA_SRC
]),
113 RTA_DATA(tb
[RTA_SRC
]), b1
, sizeof(b1
));
118 dst
= rt_addr_n2a_r(family
, RTA_PAYLOAD(tb
[RTA_DST
]),
119 RTA_DATA(tb
[RTA_DST
]), b2
, sizeof(b2
));
123 if (is_json_context()) {
124 print_string(PRINT_JSON
, "src", NULL
, src
);
125 print_string(PRINT_JSON
, "dst", NULL
, dst
);
129 snprintf(obuf
, sizeof(obuf
), "(%s,%s)", src
, dst
);
130 print_string(PRINT_FP
, NULL
,
131 "%-32s Iif: ", obuf
);
135 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
136 "iif", "%-10s ", ll_index_to_name(iif
));
138 print_string(PRINT_ANY
,"iif", "%s ", "unresolved");
140 if (tb
[RTA_MULTIPATH
]) {
141 struct rtnexthop
*nh
= RTA_DATA(tb
[RTA_MULTIPATH
]);
144 open_json_array(PRINT_JSON
, "multipath");
145 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
148 if (len
< sizeof(*nh
))
150 if (nh
->rtnh_len
> len
)
153 open_json_object(NULL
);
155 print_string(PRINT_FP
, NULL
, "Oifs: ", NULL
);
159 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
160 "oif", "%s", ll_index_to_name(nh
->rtnh_ifindex
));
162 if (nh
->rtnh_hops
> 1)
163 print_uint(PRINT_ANY
,
164 "ttl", "(ttl %u) ", nh
->rtnh_hops
);
166 print_string(PRINT_FP
, NULL
, " ", NULL
);
169 len
-= NLMSG_ALIGN(nh
->rtnh_len
);
172 close_json_array(PRINT_JSON
, NULL
);
175 print_string(PRINT_ANY
, "state", " State: %s",
176 (r
->rtm_flags
& RTNH_F_UNRESOLVED
) ? "unresolved" : "resolved");
178 if (r
->rtm_flags
& RTNH_F_OFFLOAD
)
179 print_null(PRINT_ANY
, "offload", " offload", NULL
);
181 if (show_stats
&& tb
[RTA_MFC_STATS
]) {
182 struct rta_mfc_stats
*mfcs
= RTA_DATA(tb
[RTA_MFC_STATS
]);
185 print_u64(PRINT_ANY
, "packets", " %"PRIu64
" packets,",
187 print_u64(PRINT_ANY
, "bytes", " %"PRIu64
" bytes", mfcs
->mfcs_bytes
);
189 if (mfcs
->mfcs_wrong_if
)
190 print_u64(PRINT_ANY
, "wrong_if",
191 ", %"PRIu64
" arrived on wrong iif.",
192 mfcs
->mfcs_wrong_if
);
195 if (show_stats
&& tb
[RTA_EXPIRES
]) {
199 __jiffies_to_tv(&tv
, rta_getattr_u64(tb
[RTA_EXPIRES
]));
201 age
+= tv
.tv_usec
/ 1000000.;
202 print_float(PRINT_ANY
, "expires",
206 if (table
&& (table
!= RT_TABLE_MAIN
|| show_details
> 0) && !filter
.tb
)
207 print_string(PRINT_ANY
, "table", " Table: %s",
208 rtnl_rttable_n2a(table
, b1
, sizeof(b1
)));
210 print_string(PRINT_FP
, NULL
, "\n", NULL
);
215 void ipmroute_reset_filter(int ifindex
)
217 memset(&filter
, 0, sizeof(filter
));
218 filter
.mdst
.bitlen
= -1;
219 filter
.msrc
.bitlen
= -1;
220 filter
.iif
= ifindex
;
223 static int mroute_list(int argc
, char **argv
)
228 ipmroute_reset_filter(0);
229 if (preferred_family
== AF_UNSPEC
)
233 if (family
== AF_INET
) {
234 filter
.af
= RTNL_FAMILY_IPMR
;
235 filter
.tb
= RT_TABLE_DEFAULT
; /* for backward compatibility */
237 filter
.af
= RTNL_FAMILY_IP6MR
;
239 filter
.msrc
.family
= filter
.mdst
.family
= family
;
242 if (matches(*argv
, "table") == 0) {
246 if (rtnl_rttable_a2n(&tid
, *argv
)) {
247 if (strcmp(*argv
, "all") == 0) {
249 } else if (strcmp(*argv
, "help") == 0) {
252 invarg("table id value is invalid\n", *argv
);
256 } else if (strcmp(*argv
, "iif") == 0) {
259 } else if (matches(*argv
, "from") == 0) {
261 if (get_prefix(&filter
.msrc
, *argv
, family
))
262 invarg("from value is invalid\n", *argv
);
264 if (strcmp(*argv
, "to") == 0) {
267 if (matches(*argv
, "help") == 0)
269 if (get_prefix(&filter
.mdst
, *argv
, family
))
270 invarg("to value is invalid\n", *argv
);
280 idx
= ll_name_to_index(id
);
286 if (rtnl_routedump_req(&rth
, filter
.af
) < 0) {
287 perror("Cannot send dump request");
292 if (rtnl_dump_filter(&rth
, print_mroute
, stdout
) < 0) {
294 fprintf(stderr
, "Dump terminated\n");
302 int do_multiroute(int argc
, char **argv
)
305 return mroute_list(0, NULL
);
307 if (matches(*argv
, "add") == 0)
308 return mroute_modify(RTM_NEWADDR
, argc
-1, argv
+1);
309 if (matches(*argv
, "delete") == 0)
310 return mroute_modify(RTM_DELADDR
, argc
-1, argv
+1);
311 if (matches(*argv
, "get") == 0)
312 return mroute_get(argc
-1, argv
+1);
314 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
315 || matches(*argv
, "lst") == 0)
316 return mroute_list(argc
-1, argv
+1);
317 if (matches(*argv
, "help") == 0)
319 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip mroute help\".\n", *argv
);