]>
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)
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"
43 "Usage: ip mroute [ add | del ] DESTINATION from SOURCE [ iif DEVICE ] [ oif DEVICE ]\n"
49 static struct rtfilter
{
57 int print_mroute(struct nlmsghdr
*n
, void *arg
)
59 struct rtmsg
*r
= NLMSG_DATA(n
);
60 int len
= n
->nlmsg_len
;
61 struct rtattr
*tb
[RTA_MAX
+1];
63 const char *src
, *dst
;
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
);
76 len
-= NLMSG_LENGTH(sizeof(*r
));
78 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
82 if (r
->rtm_type
!= RTN_MULTICAST
) {
84 "Non multicast route received, kernel does support IP multicast?\n");
88 parse_rtattr(tb
, RTA_MAX
, RTM_RTA(r
), len
);
89 table
= rtm_get_table(r
, tb
);
91 if (filter
.tb
> 0 && filter
.tb
!= table
)
95 iif
= rta_getattr_u32(tb
[RTA_IIF
]);
96 if (filter
.iif
&& filter
.iif
!= iif
)
99 if (filter
.af
&& filter
.af
!= r
->rtm_family
)
102 if (inet_addr_match_rta(&filter
.mdst
, tb
[RTA_DST
]))
105 if (inet_addr_match_rta(&filter
.msrc
, tb
[RTA_SRC
]))
108 family
= get_real_family(r
->rtm_type
, r
->rtm_family
);
110 open_json_object(NULL
);
111 if (n
->nlmsg_type
== RTM_DELROUTE
)
112 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
115 src
= rt_addr_n2a_r(family
, RTA_PAYLOAD(tb
[RTA_SRC
]),
116 RTA_DATA(tb
[RTA_SRC
]), b1
, sizeof(b1
));
121 dst
= rt_addr_n2a_r(family
, RTA_PAYLOAD(tb
[RTA_DST
]),
122 RTA_DATA(tb
[RTA_DST
]), b2
, sizeof(b2
));
126 if (is_json_context()) {
127 print_string(PRINT_JSON
, "src", NULL
, src
);
128 print_string(PRINT_JSON
, "dst", NULL
, dst
);
132 snprintf(obuf
, sizeof(obuf
), "(%s,%s)", src
, dst
);
133 print_string(PRINT_FP
, NULL
,
134 "%-32s Iif: ", obuf
);
138 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
139 "iif", "%-10s ", ll_index_to_name(iif
));
141 print_string(PRINT_ANY
,"iif", "%s ", "unresolved");
143 if (tb
[RTA_MULTIPATH
]) {
144 struct rtnexthop
*nh
= RTA_DATA(tb
[RTA_MULTIPATH
]);
147 open_json_array(PRINT_JSON
, "multipath");
148 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
151 if (len
< sizeof(*nh
))
153 if (nh
->rtnh_len
> len
)
156 open_json_object(NULL
);
158 print_string(PRINT_FP
, NULL
, "Oifs: ", NULL
);
162 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
163 "oif", "%s", ll_index_to_name(nh
->rtnh_ifindex
));
165 if (nh
->rtnh_hops
> 1)
166 print_uint(PRINT_ANY
,
167 "ttl", "(ttl %u) ", nh
->rtnh_hops
);
169 print_string(PRINT_FP
, NULL
, " ", NULL
);
172 len
-= NLMSG_ALIGN(nh
->rtnh_len
);
175 close_json_array(PRINT_JSON
, NULL
);
178 print_string(PRINT_ANY
, "state", " State: %s",
179 (r
->rtm_flags
& RTNH_F_UNRESOLVED
) ? "unresolved" : "resolved");
181 if (r
->rtm_flags
& RTNH_F_OFFLOAD
)
182 print_null(PRINT_ANY
, "offload", " offload", NULL
);
184 if (show_stats
&& tb
[RTA_MFC_STATS
]) {
185 struct rta_mfc_stats
*mfcs
= RTA_DATA(tb
[RTA_MFC_STATS
]);
188 print_u64(PRINT_ANY
, "packets", " %"PRIu64
" packets,",
190 print_u64(PRINT_ANY
, "bytes", " %"PRIu64
" bytes", mfcs
->mfcs_bytes
);
192 if (mfcs
->mfcs_wrong_if
)
193 print_u64(PRINT_ANY
, "wrong_if",
194 ", %"PRIu64
" arrived on wrong iif.",
195 mfcs
->mfcs_wrong_if
);
198 if (show_stats
&& tb
[RTA_EXPIRES
]) {
202 __jiffies_to_tv(&tv
, rta_getattr_u64(tb
[RTA_EXPIRES
]));
204 age
+= tv
.tv_usec
/ 1000000.;
205 print_float(PRINT_ANY
, "expires",
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
)));
213 print_string(PRINT_FP
, NULL
, "\n", NULL
);
219 void ipmroute_reset_filter(int ifindex
)
221 memset(&filter
, 0, sizeof(filter
));
222 filter
.mdst
.bitlen
= -1;
223 filter
.msrc
.bitlen
= -1;
224 filter
.iif
= ifindex
;
227 static int iproute_dump_filter(struct nlmsghdr
*nlh
, int reqlen
)
232 err
= addattr32(nlh
, reqlen
, RTA_TABLE
, filter
.tb
);
240 static int mroute_list(int argc
, char **argv
)
243 int family
= preferred_family
;
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
;
254 /* family does not have multicast routing */
258 filter
.msrc
.family
= filter
.mdst
.family
= family
;
261 if (matches(*argv
, "table") == 0) {
265 if (rtnl_rttable_a2n(&tid
, *argv
)) {
266 if (strcmp(*argv
, "all") == 0) {
268 } else if (strcmp(*argv
, "help") == 0) {
271 invarg("table id value is invalid\n", *argv
);
275 } else if (strcmp(*argv
, "iif") == 0) {
278 } else if (matches(*argv
, "from") == 0) {
280 if (get_prefix(&filter
.msrc
, *argv
, family
))
281 invarg("from value is invalid\n", *argv
);
283 if (strcmp(*argv
, "to") == 0) {
286 if (matches(*argv
, "help") == 0)
288 if (get_prefix(&filter
.mdst
, *argv
, family
))
289 invarg("to value is invalid\n", *argv
);
299 idx
= ll_name_to_index(id
);
305 if (rtnl_routedump_req(&rth
, filter
.af
, iproute_dump_filter
) < 0) {
306 perror("Cannot send dump request");
311 if (rtnl_dump_filter(&rth
, print_mroute
, stdout
) < 0) {
313 fprintf(stderr
, "Dump terminated\n");
321 int do_multiroute(int argc
, char **argv
)
324 return mroute_list(0, NULL
);
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);
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)
338 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip mroute help\".\n", *argv
);