]>
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>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
25 #include <linux/netdevice.h>
27 #include <linux/if_arp.h>
28 #include <linux/sockios.h>
32 #include "ip_common.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");
56 int print_mroute(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
, void *arg
)
58 FILE *fp
= (FILE*)arg
;
59 struct rtmsg
*r
= NLMSG_DATA(n
);
60 int len
= n
->nlmsg_len
;
61 struct rtattr
* tb
[RTA_MAX
+1];
69 if ((n
->nlmsg_type
!= RTM_NEWROUTE
&&
70 n
->nlmsg_type
!= RTM_DELROUTE
)) {
71 fprintf(stderr
, "Not a multicast route: %08x %08x %08x\n",
72 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
75 len
-= NLMSG_LENGTH(sizeof(*r
));
77 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
80 if (r
->rtm_type
!= RTN_MULTICAST
) {
81 fprintf(stderr
, "Not a multicast route (type: %s)\n",
82 rtnl_rtntype_n2a(r
->rtm_type
, b1
, sizeof(b1
)));
86 parse_rtattr(tb
, RTA_MAX
, RTM_RTA(r
), len
);
87 table
= rtm_get_table(r
, tb
);
89 if (filter
.tb
> 0 && filter
.tb
!= table
)
93 iif
= *(int*)RTA_DATA(tb
[RTA_IIF
]);
94 if (filter
.iif
&& filter
.iif
!= iif
)
97 if (filter
.af
&& filter
.af
!= r
->rtm_family
)
101 filter
.mdst
.bitlen
> 0 &&
102 inet_addr_match(RTA_DATA(tb
[RTA_DST
]), &filter
.mdst
, filter
.mdst
.bitlen
))
106 filter
.msrc
.bitlen
> 0 &&
107 inet_addr_match(RTA_DATA(tb
[RTA_SRC
]), &filter
.msrc
, filter
.msrc
.bitlen
))
110 family
= r
->rtm_family
== RTNL_FAMILY_IPMR
? AF_INET
: AF_INET6
;
112 if (n
->nlmsg_type
== RTM_DELROUTE
)
113 fprintf(fp
, "Deleted ");
116 len
= snprintf(obuf
, sizeof(obuf
),
117 "(%s, ", rt_addr_n2a(family
,
118 RTA_PAYLOAD(tb
[RTA_SRC
]),
119 RTA_DATA(tb
[RTA_SRC
]),
120 abuf
, sizeof(abuf
)));
122 len
= sprintf(obuf
, "(unknown, ");
124 snprintf(obuf
+ len
, sizeof(obuf
) - len
,
125 "%s)", rt_addr_n2a(family
,
126 RTA_PAYLOAD(tb
[RTA_DST
]),
127 RTA_DATA(tb
[RTA_DST
]),
128 abuf
, sizeof(abuf
)));
130 snprintf(obuf
+ len
, sizeof(obuf
) - len
, "unknown) ");
132 fprintf(fp
, "%-32s Iif: ", obuf
);
134 fprintf(fp
, "%-10s ", ll_index_to_name(iif
));
136 fprintf(fp
, "unresolved ");
138 if (tb
[RTA_MULTIPATH
]) {
139 struct rtnexthop
*nh
= RTA_DATA(tb
[RTA_MULTIPATH
]);
142 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
145 if (len
< sizeof(*nh
))
147 if (nh
->rtnh_len
> len
)
151 fprintf(fp
, "Oifs: ");
154 fprintf(fp
, "%s", ll_index_to_name(nh
->rtnh_ifindex
));
155 if (nh
->rtnh_hops
> 1)
156 fprintf(fp
, "(ttl %d) ", nh
->rtnh_hops
);
159 len
-= NLMSG_ALIGN(nh
->rtnh_len
);
163 if (show_stats
&& tb
[RTA_MFC_STATS
]) {
164 struct rta_mfc_stats
*mfcs
= RTA_DATA(tb
[RTA_MFC_STATS
]);
166 fprintf(fp
, "%s %"PRIu64
" packets, %"PRIu64
" bytes", _SL_
,
167 (uint64_t)mfcs
->mfcs_packets
,
168 (uint64_t)mfcs
->mfcs_bytes
);
169 if (mfcs
->mfcs_wrong_if
)
170 fprintf(fp
, ", %"PRIu64
" arrived on wrong iif.",
171 (uint64_t)mfcs
->mfcs_wrong_if
);
178 void ipmroute_reset_filter(int ifindex
)
180 memset(&filter
, 0, sizeof(filter
));
181 filter
.mdst
.bitlen
= -1;
182 filter
.msrc
.bitlen
= -1;
183 filter
.iif
= ifindex
;
186 static int mroute_list(int argc
, char **argv
)
191 ipmroute_reset_filter(0);
192 if (preferred_family
== AF_UNSPEC
)
196 if (family
== AF_INET
) {
197 filter
.af
= RTNL_FAMILY_IPMR
;
198 filter
.tb
= RT_TABLE_DEFAULT
; /* for backward compatibility */
200 filter
.af
= RTNL_FAMILY_IP6MR
;
203 if (matches(*argv
, "table") == 0) {
206 if (rtnl_rttable_a2n(&tid
, *argv
)) {
207 if (strcmp(*argv
, "all") == 0) {
209 } else if (strcmp(*argv
, "help") == 0) {
212 invarg("table id value is invalid\n", *argv
);
216 } else if (strcmp(*argv
, "iif") == 0) {
219 } else if (matches(*argv
, "from") == 0) {
221 get_prefix(&filter
.msrc
, *argv
, family
);
223 if (strcmp(*argv
, "to") == 0) {
226 if (matches(*argv
, "help") == 0)
228 get_prefix(&filter
.mdst
, *argv
, family
);
238 if ((idx
= ll_name_to_index(id
)) == 0) {
239 fprintf(stderr
, "Cannot find device \"%s\"\n", id
);
245 if (rtnl_wilddump_request(&rth
, filter
.af
, RTM_GETROUTE
) < 0) {
246 perror("Cannot send dump request");
250 if (rtnl_dump_filter(&rth
, print_mroute
, stdout
) < 0) {
251 fprintf(stderr
, "Dump terminated\n");
258 int do_multiroute(int argc
, char **argv
)
261 return mroute_list(0, NULL
);
263 if (matches(*argv
, "add") == 0)
264 return mroute_modify(RTM_NEWADDR
, argc
-1, argv
+1);
265 if (matches(*argv
, "delete") == 0)
266 return mroute_modify(RTM_DELADDR
, argc
-1, argv
+1);
267 if (matches(*argv
, "get") == 0)
268 return mroute_get(argc
-1, argv
+1);
270 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
271 || matches(*argv
, "lst") == 0)
272 return mroute_list(argc
-1, argv
+1);
273 if (matches(*argv
, "help") == 0)
275 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip mroute help\".\n", *argv
);