Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / usr.bin / netstat / mroute.c
blob739caa35128474a8d0e323aeb5f4902c42fa3805
1 /*
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
11 * are met:
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
35 * SUCH DAMAGE.
37 * @(#)mroute.c 8.2 (Berkeley) 4/28/95
39 * $FreeBSD: src/usr.bin/netstat/mroute.c,v 1.11.2.4 2001/09/17 14:53:17 ru Exp $
40 * $DragonFly: src/usr.bin/netstat/mroute.c,v 1.5 2005/08/04 17:31:23 drhodus Exp $
44 * Print multicast routing structures and statistics.
46 * MROUTING 1.0
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/protosw.h>
54 #include <sys/mbuf.h>
55 #include <sys/time.h>
57 #include <net/if.h>
58 #include <netinet/in.h>
59 #include <netinet/igmp.h>
60 #include <net/route.h>
61 #include <net/ip_mroute/ip_mroute.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include "netstat.h"
67 static void print_bw_meter(struct bw_meter *bw_meter, int *banner_printed);
69 void
70 mroutepr(u_long mfcaddr, u_long vifaddr)
72 struct mfc *mfctable[MFCTBLSIZ];
73 struct vif viftable[MAXVIFS];
74 struct mfc mfc, *m;
75 struct vif *v;
76 vifi_t vifi;
77 int i;
78 int banner_printed;
79 int saved_numeric_addr;
80 vifi_t maxvif = 0;
82 if (mfcaddr == 0 || vifaddr == 0) {
83 printf("No IPv4 multicast routing compiled into this system.\n");
84 return;
87 saved_numeric_addr = numeric_addr;
88 numeric_addr = 1;
90 kread(vifaddr, (char *)&viftable, sizeof(viftable));
91 banner_printed = 0;
92 for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
93 if (v->v_lcl_addr.s_addr == 0)
94 continue;
96 maxvif = vifi;
97 if (!banner_printed) {
98 printf("\nVirtual Interface Table\n"
99 " Vif Thresh Rate Local-Address "
100 "Remote-Address Pkts-In Pkts-Out\n");
101 banner_printed = 1;
104 printf(" %2u %6u %4d %-15.15s",
105 /* opposite math of add_vif() */
106 vifi, v->v_threshold, v->v_rate_limit * 1000 / 1024,
107 routename(v->v_lcl_addr.s_addr));
108 printf(" %-15.15s", (v->v_flags & VIFF_TUNNEL) ?
109 routename(v->v_rmt_addr.s_addr) : "");
111 printf(" %9lu %9lu\n", v->v_pkt_in, v->v_pkt_out);
113 if (!banner_printed)
114 printf("\nVirtual Interface Table is empty\n");
116 kread(mfcaddr, (char *)&mfctable, sizeof(mfctable));
117 banner_printed = 0;
118 for (i = 0; i < MFCTBLSIZ; ++i) {
119 m = mfctable[i];
120 while(m) {
121 kread((u_long)m, (char *)&mfc, sizeof mfc);
123 if (!banner_printed) {
124 printf("\nIPv4 Multicast Forwarding Cache\n"
125 " Origin Group "
126 " Packets In-Vif Out-Vifs:Ttls\n");
127 banner_printed = 1;
130 printf(" %-15.15s", routename(mfc.mfc_origin.s_addr));
131 printf(" %-15.15s", routename(mfc.mfc_mcastgrp.s_addr));
132 printf(" %9lu", mfc.mfc_pkt_cnt);
133 printf(" %3d ", mfc.mfc_parent);
134 for (vifi = 0; vifi <= maxvif; vifi++) {
135 if (mfc.mfc_ttls[vifi] > 0)
136 printf(" %u:%u", vifi,
137 mfc.mfc_ttls[vifi]);
139 printf("\n");
141 /* Print the bw meter information */
143 struct bw_meter bw_meter, *bwm;
144 int banner_printed2 = 0;
146 bwm = mfc.mfc_bw_meter;
147 while (bwm) {
148 kread((u_long)bwm, (char *)&bw_meter,
149 sizeof bw_meter);
150 print_bw_meter(&bw_meter,
151 &banner_printed2);
152 bwm = bw_meter.bm_mfc_next;
154 #if 0 /* Don't ever print it? */
155 if (! banner_printed2)
156 printf("\n No Bandwidth Meters\n");
157 #endif
160 m = mfc.mfc_next;
163 if (!banner_printed)
164 printf("\nMulticast Routing Table is empty\n");
166 printf("\n");
167 numeric_addr = saved_numeric_addr;
170 static void
171 print_bw_meter(struct bw_meter *bw_meter, int *banner_printed)
173 char s0[256], s1[256], s2[256], s3[256];
174 struct timeval now, end, delta;
176 gettimeofday(&now, NULL);
178 if (! *banner_printed) {
179 printf(" Bandwidth Meters\n");
180 printf(" %-30s", "Measured(Start|Packets|Bytes)");
181 printf(" %s", "Type");
182 printf(" %-30s", "Thresh(Interval|Packets|Bytes)");
183 printf(" Remain");
184 printf("\n");
185 *banner_printed = 1;
188 /* The measured values */
189 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
190 sprintf(s1, "%llu", bw_meter->bm_measured.b_packets);
191 else
192 sprintf(s1, "?");
193 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
194 sprintf(s2, "%llu", bw_meter->bm_measured.b_bytes);
195 else
196 sprintf(s2, "?");
197 sprintf(s0, "%lu.%lu|%s|%s",
198 bw_meter->bm_start_time.tv_sec,
199 bw_meter->bm_start_time.tv_usec,
200 s1, s2);
201 printf(" %-30s", s0);
203 /* The type of entry */
204 sprintf(s0, "%s", "?");
205 if (bw_meter->bm_flags & BW_METER_GEQ)
206 sprintf(s0, "%s", ">=");
207 else if (bw_meter->bm_flags & BW_METER_LEQ)
208 sprintf(s0, "%s", "<=");
209 printf(" %-3s", s0);
211 /* The threshold values */
212 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
213 sprintf(s1, "%llu", bw_meter->bm_threshold.b_packets);
214 else
215 sprintf(s1, "?");
216 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
217 sprintf(s2, "%llu", bw_meter->bm_threshold.b_bytes);
218 else
219 sprintf(s2, "?");
220 sprintf(s0, "%lu.%lu|%s|%s",
221 bw_meter->bm_threshold.b_time.tv_sec,
222 bw_meter->bm_threshold.b_time.tv_usec,
223 s1, s2);
224 printf(" %-30s", s0);
226 /* Remaining time */
227 timeradd(&bw_meter->bm_start_time,
228 &bw_meter->bm_threshold.b_time, &end);
229 if (timercmp(&now, &end, <=)) {
230 timersub(&end, &now, &delta);
231 sprintf(s3, "%lu.%lu", delta.tv_sec, delta.tv_usec);
232 } else {
233 /* Negative time */
234 timersub(&now, &end, &delta);
235 sprintf(s3, "-%lu.%lu", delta.tv_sec, delta.tv_usec);
237 printf(" %s", s3);
239 printf("\n");
242 void
243 mrt_stats(u_long mstaddr)
245 struct mrtstat mrtstat;
247 if (mstaddr == 0) {
248 printf("No IPv4 multicast routing compiled into this system.\n");
249 return;
252 kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
253 printf("IPv4 multicast forwarding:\n");
255 #define p(f, m) if (mrtstat.f || sflag <= 1) \
256 printf(m, mrtstat.f, plural(mrtstat.f))
257 #define p2(f, m) if (mrtstat.f || sflag <= 1) \
258 printf(m, mrtstat.f, plurales(mrtstat.f))
260 p(mrts_mfc_lookups, "\t%lu multicast forwarding cache lookup%s\n");
261 p2(mrts_mfc_misses, "\t%lu multicast forwarding cache miss%s\n");
262 p(mrts_upcalls, "\t%lu upcall%s to mrouted\n");
263 p(mrts_upq_ovflw, "\t%lu upcall queue overflow%s\n");
264 p(mrts_upq_sockfull,
265 "\t%lu upcall%s dropped due to full socket buffer\n");
266 p(mrts_cache_cleanups, "\t%lu cache cleanup%s\n");
267 p(mrts_no_route, "\t%lu datagram%s with no route for origin\n");
268 p(mrts_bad_tunnel, "\t%lu datagram%s arrived with bad tunneling\n");
269 p(mrts_cant_tunnel, "\t%lu datagram%s could not be tunneled\n");
270 p(mrts_wrong_if, "\t%lu datagram%s arrived on wrong interface\n");
271 p(mrts_drop_sel, "\t%lu datagram%s selectively dropped\n");
272 p(mrts_q_overflow, "\t%lu datagram%s dropped due to queue overflow\n");
273 p(mrts_pkt2large, "\t%lu datagram%s dropped for being too large\n");
275 #undef p2
276 #undef p