2 * Copyright (c) 1989 Stephen Deering
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Stephen Deering of Stanford University.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)mroute.c 8.2 (Berkeley) 4/28/95
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * Print multicast routing structures and statistics.
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/protosw.h>
59 #include <netinet/in.h>
60 #include <netinet/igmp.h>
61 #include <net/route.h>
62 #include <netinet/ip_mroute.h>
69 static void print_bw_meter(struct bw_meter *bw_meter, int *banner_printed);
72 mroutepr(u_long mfcaddr, u_long vifaddr)
74 struct mfc *mfctable[MFCTBLSIZ];
75 struct vif viftable[MAXVIFS];
81 int saved_numeric_addr;
85 len = sizeof(mfctable);
86 if (sysctlbyname("net.inet.ip.mfctable", mfctable, &len, NULL, 0) < 0) {
87 warn("sysctl: net.inet.ip.mfctable");
88 /* Compatability with older kernels - candidate for removal */
90 printf("No IPv4 multicast routing compiled into this system.\n");
94 kread(mfcaddr, (char *)mfctable, sizeof(mfctable));
97 len = sizeof(viftable);
98 if (sysctlbyname("net.inet.ip.viftable", viftable, &len, NULL, 0) < 0) {
99 warn("sysctl: net.inet.ip.viftable");
100 /* Compatability with older kernels - candidate for removal */
102 printf("No IPv4 multicast routing compiled into this system.\n");
106 kread(vifaddr, (char *)viftable, sizeof(viftable));
109 saved_numeric_addr = numeric_addr;
113 for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
114 if (v->v_lcl_addr.s_addr == 0)
118 if (!banner_printed) {
119 printf("\nVirtual Interface Table\n"
120 " Vif Thresh Rate Local-Address "
121 "Remote-Address Pkts-In Pkts-Out\n");
125 printf(" %2u %6u %4d %-15.15s",
126 /* opposite math of add_vif() */
127 vifi, v->v_threshold, v->v_rate_limit * 1000 / 1024,
128 routename(v->v_lcl_addr.s_addr));
129 printf(" %-15.15s", (v->v_flags & VIFF_TUNNEL) ?
130 routename(v->v_rmt_addr.s_addr) : "");
132 printf(" %9lu %9lu\n", v->v_pkt_in, v->v_pkt_out);
135 printf("\nVirtual Interface Table is empty\n");
138 for (i = 0; i < MFCTBLSIZ; ++i) {
141 kread((u_long)m, (char *)&mfc, sizeof mfc);
143 if (!banner_printed) {
144 printf("\nIPv4 Multicast Forwarding Cache\n"
146 " Packets In-Vif Out-Vifs:Ttls\n");
150 printf(" %-15.15s", routename(mfc.mfc_origin.s_addr));
151 printf(" %-15.15s", routename(mfc.mfc_mcastgrp.s_addr));
152 printf(" %9lu", mfc.mfc_pkt_cnt);
153 printf(" %3d ", mfc.mfc_parent);
154 for (vifi = 0; vifi <= maxvif; vifi++) {
155 if (mfc.mfc_ttls[vifi] > 0)
156 printf(" %u:%u", vifi,
161 /* Print the bw meter information */
163 struct bw_meter bw_meter, *bwm;
164 int banner_printed2 = 0;
166 bwm = mfc.mfc_bw_meter;
168 kread((u_long)bwm, (char *)&bw_meter,
170 print_bw_meter(&bw_meter,
172 bwm = bw_meter.bm_mfc_next;
174 #if 0 /* Don't ever print it? */
175 if (! banner_printed2)
176 printf("\n No Bandwidth Meters\n");
184 printf("\nMulticast Routing Table is empty\n");
187 numeric_addr = saved_numeric_addr;
191 print_bw_meter(struct bw_meter *bw_meter, int *banner_printed)
193 char s0[256], s1[256], s2[256], s3[256];
194 struct timeval now, end, delta;
196 gettimeofday(&now, NULL);
198 if (! *banner_printed) {
199 printf(" Bandwidth Meters\n");
200 printf(" %-30s", "Measured(Start|Packets|Bytes)");
201 printf(" %s", "Type");
202 printf(" %-30s", "Thresh(Interval|Packets|Bytes)");
208 /* The measured values */
209 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
210 sprintf(s1, "%llu", bw_meter->bm_measured.b_packets);
213 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
214 sprintf(s2, "%llu", bw_meter->bm_measured.b_bytes);
217 sprintf(s0, "%lu.%lu|%s|%s",
218 bw_meter->bm_start_time.tv_sec,
219 bw_meter->bm_start_time.tv_usec,
221 printf(" %-30s", s0);
223 /* The type of entry */
224 sprintf(s0, "%s", "?");
225 if (bw_meter->bm_flags & BW_METER_GEQ)
226 sprintf(s0, "%s", ">=");
227 else if (bw_meter->bm_flags & BW_METER_LEQ)
228 sprintf(s0, "%s", "<=");
231 /* The threshold values */
232 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
233 sprintf(s1, "%llu", bw_meter->bm_threshold.b_packets);
236 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
237 sprintf(s2, "%llu", bw_meter->bm_threshold.b_bytes);
240 sprintf(s0, "%lu.%lu|%s|%s",
241 bw_meter->bm_threshold.b_time.tv_sec,
242 bw_meter->bm_threshold.b_time.tv_usec,
244 printf(" %-30s", s0);
247 timeradd(&bw_meter->bm_start_time,
248 &bw_meter->bm_threshold.b_time, &end);
249 if (timercmp(&now, &end, <=)) {
250 timersub(&end, &now, &delta);
251 sprintf(s3, "%lu.%lu", delta.tv_sec, delta.tv_usec);
254 timersub(&now, &end, &delta);
255 sprintf(s3, "-%lu.%lu", delta.tv_sec, delta.tv_usec);
263 mrt_stats(u_long mstaddr)
265 struct mrtstat mrtstat;
266 size_t len = sizeof mrtstat;
268 if (sysctlbyname("net.inet.ip.mrtstat", &mrtstat, &len,
270 warn("sysctl: net.inet.ip.mrtstat");
271 /* Compatability with older kernels - candidate for removal */
273 printf("No IPv4 multicast routing compiled into this system.\n");
277 kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
279 printf("IPv4 multicast forwarding:\n");
281 #define p(f, m) if (mrtstat.f || sflag <= 1) \
282 printf(m, mrtstat.f, plural(mrtstat.f))
283 #define p2(f, m) if (mrtstat.f || sflag <= 1) \
284 printf(m, mrtstat.f, plurales(mrtstat.f))
286 p(mrts_mfc_lookups, "\t%lu multicast forwarding cache lookup%s\n");
287 p2(mrts_mfc_misses, "\t%lu multicast forwarding cache miss%s\n");
288 p(mrts_upcalls, "\t%lu upcall%s to mrouted\n");
289 p(mrts_upq_ovflw, "\t%lu upcall queue overflow%s\n");
291 "\t%lu upcall%s dropped due to full socket buffer\n");
292 p(mrts_cache_cleanups, "\t%lu cache cleanup%s\n");
293 p(mrts_no_route, "\t%lu datagram%s with no route for origin\n");
294 p(mrts_bad_tunnel, "\t%lu datagram%s arrived with bad tunneling\n");
295 p(mrts_cant_tunnel, "\t%lu datagram%s could not be tunneled\n");
296 p(mrts_wrong_if, "\t%lu datagram%s arrived on wrong interface\n");
297 p(mrts_drop_sel, "\t%lu datagram%s selectively dropped\n");
298 p(mrts_q_overflow, "\t%lu datagram%s dropped due to queue overflow\n");
299 p(mrts_pkt2large, "\t%lu datagram%s dropped for being too large\n");