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. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)mroute.c 8.2 (Berkeley) 4/28/95
35 * $FreeBSD: src/usr.bin/netstat/mroute.c,v 1.11.2.4 2001/09/17 14:53:17 ru Exp $
39 * Print multicast routing structures and statistics.
44 #include <sys/param.h>
45 #include <sys/queue.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/protosw.h>
53 #include <netinet/in.h>
54 #include <netinet/igmp.h>
55 #include <net/route.h>
56 #include <net/ip_mroute/ip_mroute.h>
62 static void print_bw_meter(struct bw_meter
*bw_meter
, int *banner_printed
);
65 mroutepr(u_long mfcaddr
, u_long vifaddr
)
67 struct mfc
*mfctable
[MFCTBLSIZ
];
68 struct vif viftable
[MAXVIFS
];
74 int saved_numeric_addr
;
77 if (mfcaddr
== 0 || vifaddr
== 0) {
78 printf("No IPv4 multicast routing compiled into this system.\n");
82 saved_numeric_addr
= numeric_addr
;
85 kread(vifaddr
, (char *)&viftable
, sizeof(viftable
));
87 for (vifi
= 0, v
= viftable
; vifi
< MAXVIFS
; ++vifi
, ++v
) {
88 if (v
->v_lcl_addr
.s_addr
== 0)
92 if (!banner_printed
) {
93 printf("\nVirtual Interface Table\n"
94 " Vif Thresh Rate Local-Address "
95 "Remote-Address Pkts-In Pkts-Out\n");
99 printf(" %2u %6u %4d %-15.15s",
100 /* opposite math of add_vif() */
101 vifi
, v
->v_threshold
, v
->v_rate_limit
* 1000 / 1024,
102 routename(v
->v_lcl_addr
.s_addr
));
103 printf(" %-15.15s", (v
->v_flags
& VIFF_TUNNEL
) ?
104 routename(v
->v_rmt_addr
.s_addr
) : "");
106 printf(" %9lu %9lu\n", v
->v_pkt_in
, v
->v_pkt_out
);
109 printf("\nVirtual Interface Table is empty\n");
111 kread(mfcaddr
, (char *)&mfctable
, sizeof(mfctable
));
113 for (i
= 0; i
< MFCTBLSIZ
; ++i
) {
116 kread((u_long
)m
, (char *)&mfc
, sizeof mfc
);
118 if (!banner_printed
) {
119 printf("\nIPv4 Multicast Forwarding Cache\n"
121 " Packets In-Vif Out-Vifs:Ttls\n");
125 printf(" %-15.15s", routename(mfc
.mfc_origin
.s_addr
));
126 printf(" %-15.15s", routename(mfc
.mfc_mcastgrp
.s_addr
));
127 printf(" %9lu", mfc
.mfc_pkt_cnt
);
128 printf(" %3d ", mfc
.mfc_parent
);
129 for (vifi
= 0; vifi
<= maxvif
; vifi
++) {
130 if (mfc
.mfc_ttls
[vifi
] > 0)
131 printf(" %u:%u", vifi
,
136 /* Print the bw meter information */
138 struct bw_meter bw_meter
, *bwm
;
139 int banner_printed2
= 0;
141 bwm
= mfc
.mfc_bw_meter
;
143 kread((u_long
)bwm
, (char *)&bw_meter
,
145 print_bw_meter(&bw_meter
,
147 bwm
= bw_meter
.bm_mfc_next
;
149 #if 0 /* Don't ever print it? */
150 if (! banner_printed2
)
151 printf("\n No Bandwidth Meters\n");
159 printf("\nMulticast Routing Table is empty\n");
162 numeric_addr
= saved_numeric_addr
;
166 print_bw_meter(struct bw_meter
*bw_meter
, int *banner_printed
)
168 char s0
[256], s1
[256], s2
[256], s3
[256];
169 struct timeval now
, end
, delta
;
171 gettimeofday(&now
, NULL
);
173 if (! *banner_printed
) {
174 printf(" Bandwidth Meters\n");
175 printf(" %-30s", "Measured(Start|Packets|Bytes)");
176 printf(" %s", "Type");
177 printf(" %-30s", "Thresh(Interval|Packets|Bytes)");
183 /* The measured values */
184 if (bw_meter
->bm_flags
& BW_METER_UNIT_PACKETS
)
185 sprintf(s1
, "%ju", (uintmax_t)bw_meter
->bm_measured
.b_packets
);
188 if (bw_meter
->bm_flags
& BW_METER_UNIT_BYTES
)
189 sprintf(s2
, "%ju", (uintmax_t)bw_meter
->bm_measured
.b_bytes
);
192 sprintf(s0
, "%lu.%lu|%.64s|%.64s",
193 bw_meter
->bm_start_time
.tv_sec
,
194 bw_meter
->bm_start_time
.tv_usec
,
196 printf(" %-30s", s0
);
198 /* The type of entry */
199 sprintf(s0
, "%s", "?");
200 if (bw_meter
->bm_flags
& BW_METER_GEQ
)
201 sprintf(s0
, "%s", ">=");
202 else if (bw_meter
->bm_flags
& BW_METER_LEQ
)
203 sprintf(s0
, "%s", "<=");
206 /* The threshold values */
207 if (bw_meter
->bm_flags
& BW_METER_UNIT_PACKETS
)
208 sprintf(s1
, "%ju", (uintmax_t)bw_meter
->bm_threshold
.b_packets
);
211 if (bw_meter
->bm_flags
& BW_METER_UNIT_BYTES
)
212 sprintf(s2
, "%ju", (uintmax_t)bw_meter
->bm_threshold
.b_bytes
);
215 sprintf(s0
, "%lu.%lu|%.64s|%.64s",
216 bw_meter
->bm_threshold
.b_time
.tv_sec
,
217 bw_meter
->bm_threshold
.b_time
.tv_usec
,
219 printf(" %-30s", s0
);
222 timeradd(&bw_meter
->bm_start_time
,
223 &bw_meter
->bm_threshold
.b_time
, &end
);
224 if (timercmp(&now
, &end
, <=)) {
225 timersub(&end
, &now
, &delta
);
226 sprintf(s3
, "%lu.%lu", delta
.tv_sec
, delta
.tv_usec
);
229 timersub(&now
, &end
, &delta
);
230 sprintf(s3
, "-%lu.%lu", delta
.tv_sec
, delta
.tv_usec
);
238 mrt_stats(u_long mstaddr
)
240 struct mrtstat mrtstat
;
243 printf("No IPv4 multicast routing compiled into this system.\n");
247 kread(mstaddr
, (char *)&mrtstat
, sizeof(mrtstat
));
248 printf("IPv4 multicast forwarding:\n");
250 #define p(f, m) if (mrtstat.f || sflag <= 1) \
251 printf(m, mrtstat.f, plural(mrtstat.f))
252 #define p2(f, m) if (mrtstat.f || sflag <= 1) \
253 printf(m, mrtstat.f, plurales(mrtstat.f))
255 p(mrts_mfc_lookups
, "\t%lu multicast forwarding cache lookup%s\n");
256 p2(mrts_mfc_misses
, "\t%lu multicast forwarding cache miss%s\n");
257 p(mrts_upcalls
, "\t%lu upcall%s to mrouted\n");
258 p(mrts_upq_ovflw
, "\t%lu upcall queue overflow%s\n");
260 "\t%lu upcall%s dropped due to full socket buffer\n");
261 p(mrts_cache_cleanups
, "\t%lu cache cleanup%s\n");
262 p(mrts_no_route
, "\t%lu datagram%s with no route for origin\n");
263 p(mrts_bad_tunnel
, "\t%lu datagram%s arrived with bad tunneling\n");
264 p(mrts_cant_tunnel
, "\t%lu datagram%s could not be tunneled\n");
265 p(mrts_wrong_if
, "\t%lu datagram%s arrived on wrong interface\n");
266 p(mrts_drop_sel
, "\t%lu datagram%s selectively dropped\n");
267 p(mrts_q_overflow
, "\t%lu datagram%s dropped due to queue overflow\n");
268 p(mrts_pkt2large
, "\t%lu datagram%s dropped for being too large\n");