]>
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");
55 int print_mroute(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
, void *arg
)
57 FILE *fp
= (FILE *)arg
;
58 struct rtmsg
*r
= NLMSG_DATA(n
);
59 int len
= n
->nlmsg_len
;
60 struct rtattr
*tb
[RTA_MAX
+1];
68 if ((n
->nlmsg_type
!= RTM_NEWROUTE
&&
69 n
->nlmsg_type
!= RTM_DELROUTE
)) {
70 fprintf(stderr
, "Not a multicast route: %08x %08x %08x\n",
71 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
74 len
-= NLMSG_LENGTH(sizeof(*r
));
76 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
79 if (r
->rtm_type
!= RTN_MULTICAST
) {
80 fprintf(stderr
, "Not a multicast route (type: %s)\n",
81 rtnl_rtntype_n2a(r
->rtm_type
, b1
, sizeof(b1
)));
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 (tb
[RTA_DST
] && filter
.mdst
.bitlen
> 0) {
100 inet_prefix dst
= { .family
= r
->rtm_family
};
102 memcpy(&dst
.data
, RTA_DATA(tb
[RTA_DST
]), RTA_PAYLOAD(tb
[RTA_DST
]));
103 if (inet_addr_match(&dst
, &filter
.mdst
, filter
.mdst
.bitlen
))
107 if (tb
[RTA_SRC
] && filter
.msrc
.bitlen
> 0) {
108 inet_prefix src
= { .family
= r
->rtm_family
};
110 memcpy(&src
.data
, RTA_DATA(tb
[RTA_SRC
]), RTA_PAYLOAD(tb
[RTA_SRC
]));
111 if (inet_addr_match(&src
, &filter
.msrc
, filter
.msrc
.bitlen
))
115 family
= get_real_family(r
->rtm_type
, r
->rtm_family
);
117 if (n
->nlmsg_type
== RTM_DELROUTE
)
118 fprintf(fp
, "Deleted ");
121 len
= snprintf(obuf
, sizeof(obuf
),
122 "(%s, ", rt_addr_n2a_rta(family
, tb
[RTA_SRC
]));
124 len
= sprintf(obuf
, "(unknown, ");
126 snprintf(obuf
+ len
, sizeof(obuf
) - len
,
127 "%s)", rt_addr_n2a_rta(family
, tb
[RTA_DST
]));
129 snprintf(obuf
+ len
, sizeof(obuf
) - len
, "unknown) ");
131 fprintf(fp
, "%-32s Iif: ", obuf
);
133 fprintf(fp
, "%-10s ", ll_index_to_name(iif
));
135 fprintf(fp
, "unresolved ");
137 if (tb
[RTA_MULTIPATH
]) {
138 struct rtnexthop
*nh
= RTA_DATA(tb
[RTA_MULTIPATH
]);
141 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
144 if (len
< sizeof(*nh
))
146 if (nh
->rtnh_len
> len
)
150 fprintf(fp
, "Oifs: ");
153 fprintf(fp
, "%s", ll_index_to_name(nh
->rtnh_ifindex
));
154 if (nh
->rtnh_hops
> 1)
155 fprintf(fp
, "(ttl %d) ", nh
->rtnh_hops
);
158 len
-= NLMSG_ALIGN(nh
->rtnh_len
);
162 fprintf(fp
, " State: %s",
163 r
->rtm_flags
& RTNH_F_UNRESOLVED
? "unresolved" : "resolved");
164 if (show_stats
&& tb
[RTA_MFC_STATS
]) {
165 struct rta_mfc_stats
*mfcs
= RTA_DATA(tb
[RTA_MFC_STATS
]);
167 fprintf(fp
, "%s %"PRIu64
" packets, %"PRIu64
" bytes", _SL_
,
168 (uint64_t)mfcs
->mfcs_packets
,
169 (uint64_t)mfcs
->mfcs_bytes
);
170 if (mfcs
->mfcs_wrong_if
)
171 fprintf(fp
, ", %"PRIu64
" arrived on wrong iif.",
172 (uint64_t)mfcs
->mfcs_wrong_if
);
174 if (show_stats
&& tb
[RTA_EXPIRES
]) {
177 __jiffies_to_tv(&tv
, rta_getattr_u64(tb
[RTA_EXPIRES
]));
178 fprintf(fp
, ", Age %4i.%.2i", (int)tv
.tv_sec
,
179 (int)tv
.tv_usec
/10000);
186 void ipmroute_reset_filter(int ifindex
)
188 memset(&filter
, 0, sizeof(filter
));
189 filter
.mdst
.bitlen
= -1;
190 filter
.msrc
.bitlen
= -1;
191 filter
.iif
= ifindex
;
194 static int mroute_list(int argc
, char **argv
)
199 ipmroute_reset_filter(0);
200 if (preferred_family
== AF_UNSPEC
)
204 if (family
== AF_INET
) {
205 filter
.af
= RTNL_FAMILY_IPMR
;
206 filter
.tb
= RT_TABLE_DEFAULT
; /* for backward compatibility */
208 filter
.af
= RTNL_FAMILY_IP6MR
;
211 if (matches(*argv
, "table") == 0) {
215 if (rtnl_rttable_a2n(&tid
, *argv
)) {
216 if (strcmp(*argv
, "all") == 0) {
218 } else if (strcmp(*argv
, "help") == 0) {
221 invarg("table id value is invalid\n", *argv
);
225 } else if (strcmp(*argv
, "iif") == 0) {
228 } else if (matches(*argv
, "from") == 0) {
230 get_prefix(&filter
.msrc
, *argv
, family
);
232 if (strcmp(*argv
, "to") == 0) {
235 if (matches(*argv
, "help") == 0)
237 get_prefix(&filter
.mdst
, *argv
, family
);
247 if ((idx
= ll_name_to_index(id
)) == 0) {
248 fprintf(stderr
, "Cannot find device \"%s\"\n", id
);
254 if (rtnl_wilddump_request(&rth
, filter
.af
, RTM_GETROUTE
) < 0) {
255 perror("Cannot send dump request");
259 if (rtnl_dump_filter(&rth
, print_mroute
, stdout
) < 0) {
260 fprintf(stderr
, "Dump terminated\n");
267 int do_multiroute(int argc
, char **argv
)
270 return mroute_list(0, NULL
);
272 if (matches(*argv
, "add") == 0)
273 return mroute_modify(RTM_NEWADDR
, argc
-1, argv
+1);
274 if (matches(*argv
, "delete") == 0)
275 return mroute_modify(RTM_DELADDR
, argc
-1, argv
+1);
276 if (matches(*argv
, "get") == 0)
277 return mroute_get(argc
-1, argv
+1);
279 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
280 || matches(*argv
, "lst") == 0)
281 return mroute_list(argc
-1, argv
+1);
282 if (matches(*argv
, "help") == 0)
284 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip mroute help\".\n", *argv
);