xref: /netbsd-src/usr.bin/netstat/mroute.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: mroute.c,v 1.19 2004/09/06 14:51:32 martin Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Stephen Deering of Stanford University.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)mroute.c	8.1 (Berkeley) 6/6/93
35  */
36 
37 /*
38  * Copyright (c) 1989 Stephen Deering
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Stephen Deering of Stanford University.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  *	from: @(#)mroute.c	8.1 (Berkeley) 6/6/93
72  */
73 
74 #include <sys/cdefs.h>
75 #ifndef lint
76 #if 0
77 static char sccsid[] = "from: @(#)mroute.c	8.1 (Berkeley) 6/6/93";
78 #else
79 __RCSID("$NetBSD: mroute.c,v 1.19 2004/09/06 14:51:32 martin Exp $");
80 #endif
81 #endif /* not lint */
82 
83 /*
84  * Print multicast routing structures and statistics.
85  *
86  * MROUTING 1.0
87  */
88 
89 #include <sys/param.h>
90 #include <sys/socket.h>
91 #include <sys/socketvar.h>
92 #include <sys/protosw.h>
93 
94 #include <net/if.h>
95 #include <net/route.h>
96 #include <netinet/in.h>
97 #include <netinet/igmp.h>
98 #define _KERNEL
99 #include <netinet/ip_mroute.h>
100 #undef _KERNEL
101 
102 #include <stdio.h>
103 #include <stdlib.h>
104 #include "netstat.h"
105 
106 static char *pktscale __P((u_long));
107 static void print_bw_meter __P((struct bw_meter *, int *));
108 
109 static char *
110 pktscale(n)
111 	u_long n;
112 {
113 	static char buf[20];
114 	char t;
115 
116 	if (n < 1024)
117 		t = ' ';
118 	else if (n < 1024 * 1024) {
119 		t = 'k';
120 		n /= 1024;
121 	} else {
122 		t = 'm';
123 		n /= 1048576;
124 	}
125 
126 	(void)snprintf(buf, sizeof buf, "%lu%c", n, t);
127 	return (buf);
128 }
129 
130 void
131 mroutepr(mrpaddr, mfchashtbladdr, mfchashaddr, vifaddr)
132 	u_long mrpaddr, mfchashtbladdr, mfchashaddr, vifaddr;
133 {
134 	u_int mrtproto;
135 	LIST_HEAD(, mfc) *mfchashtbl;
136 	u_long mfchash;
137 	struct vif viftable[MAXVIFS];
138 	struct mfc *mfcp, mfc;
139 	struct vif *v;
140 	vifi_t vifi;
141 	int i;
142 	int banner_printed;
143 	int saved_numeric_addr;
144 	int numvifs;
145 	int nmfc;		/* No. of cache entries */
146 
147 	if (mrpaddr == 0) {
148 		printf("ip_mrtproto: symbol not in namelist\n");
149 		return;
150 	}
151 
152 	kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
153 	switch (mrtproto) {
154 	case 0:
155 		printf("no multicast routing compiled into this system\n");
156 		return;
157 
158 	case IGMP_DVMRP:
159 		break;
160 
161 	default:
162 		printf("multicast routing protocol %u, unknown\n", mrtproto);
163 		return;
164 	}
165 
166 	if (mfchashtbladdr == 0) {
167 		printf("mfchashtbl: symbol not in namelist\n");
168 		return;
169 	}
170 	if (mfchashaddr == 0) {
171 		printf("mfchash: symbol not in namelist\n");
172 		return;
173 	}
174 	if (vifaddr == 0) {
175 		printf("viftable: symbol not in namelist\n");
176 		return;
177 	}
178 
179 	saved_numeric_addr = numeric_addr;
180 	numeric_addr = 1;
181 
182 	kread(vifaddr, (char *)&viftable, sizeof(viftable));
183 	banner_printed = 0;
184 	numvifs = 0;
185 
186 	for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
187 		if (v->v_lcl_addr.s_addr == 0)
188 			continue;
189 		numvifs = vifi;
190 
191 		if (!banner_printed) {
192 			printf("\nVirtual Interface Table\n %s%s",
193 			    "Vif  Thresh  Limit  Local-Address    ",
194 			    "Remote-Address   Pkt_in  Pkt_out\n");
195 			banner_printed = 1;
196 		}
197 
198 		printf(" %3u     %3u  %5u  %-15.15s",
199 		    vifi, v->v_threshold, v->v_rate_limit,
200 		    routename(v->v_lcl_addr.s_addr));
201 		printf("  %-15.15s  %6lu  %7lu\n", (v->v_flags & VIFF_TUNNEL) ?
202 		    routename(v->v_rmt_addr.s_addr) : "",
203 		    v->v_pkt_in, v->v_pkt_out);
204 	}
205 	if (!banner_printed)
206 		printf("\nVirtual Interface Table is empty\n");
207 
208 	kread(mfchashtbladdr, (char *)&mfchashtbl, sizeof(mfchashtbl));
209 	kread(mfchashaddr, (char *)&mfchash, sizeof(mfchash));
210 	banner_printed = 0;
211 	nmfc = 0;
212 
213 	if (mfchashtbl != 0)
214 	for (i = 0; i <= mfchash; ++i) {
215 		kread((u_long)&mfchashtbl[i], (char *)&mfcp, sizeof(mfcp));
216 
217 		for (; mfcp != 0; mfcp = mfc.mfc_hash.le_next) {
218 			if (!banner_printed) {
219 				printf("\nMulticast Forwarding Cache\n %s%s",
220 				    "Hash  Origin           Mcastgroup       ",
221 				    "Traffic  In-Vif  Out-Vifs/Forw-ttl\n");
222 				banner_printed = 1;
223 			}
224 
225 			kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
226 			printf("  %3u  %-15.15s",
227 			    i, routename(mfc.mfc_origin.s_addr));
228 			printf("  %-15.15s  %7s     %3u ",
229 			    routename(mfc.mfc_mcastgrp.s_addr),
230 			    pktscale(mfc.mfc_pkt_cnt), mfc.mfc_parent);
231 			for (vifi = 0; vifi <= numvifs; ++vifi)
232 				if (mfc.mfc_ttls[vifi])
233 					printf(" %u/%u", vifi, mfc.mfc_ttls[vifi]);
234 
235 			printf("\n");
236 
237 			/* Print the bw meter information */
238 			{
239 				struct bw_meter bw_meter, *bwm;
240 				int banner_printed2 = 0;
241 
242 				bwm = mfc.mfc_bw_meter;
243 				while (bwm) {
244 				kread((u_long)bwm,
245 				      (char *)&bw_meter,
246 				      sizeof bw_meter);
247 				print_bw_meter(&bw_meter,
248 					       &banner_printed2);
249 				bwm = bw_meter.bm_mfc_next;
250 				}
251 #if 0	/* Don't ever print it? */
252 				if (! banner_printed2)
253 				    printf("\n  No Bandwidth Meters\n");
254 #endif
255 			}
256 
257 			nmfc++;
258 		}
259 	}
260 	if (!banner_printed)
261 		printf("\nMulticast Forwarding Cache is empty\n");
262 	else
263 		printf("\nTotal no. of entries in cache: %d\n", nmfc);
264 
265 	printf("\n");
266 	numeric_addr = saved_numeric_addr;
267 }
268 
269 static void
270 print_bw_meter(bw_meter, banner_printed)
271 	struct bw_meter *bw_meter;
272 	int *banner_printed;
273 {
274 	char s0[256], s1[256], s2[256], s3[256];
275 	struct timeval now, end, delta;
276 
277 	gettimeofday(&now, NULL);
278 
279 	if (! *banner_printed) {
280 		printf(" Bandwidth Meters\n");
281 		printf("  %-30s", "Measured(Start|Packets|Bytes)");
282 		printf(" %s", "Type");
283 		printf("  %-30s", "Thresh(Interval|Packets|Bytes)");
284 		printf(" Remain");
285 		printf("\n");
286 		*banner_printed = 1;
287 	}
288 
289 	/* The measured values */
290 	if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
291 		sprintf(s1, "%llu", (unsigned long long)bw_meter->bm_measured.b_packets);
292 	else
293 		sprintf(s1, "?");
294 	if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
295 		sprintf(s2, "%llu", (unsigned long long)bw_meter->bm_measured.b_bytes);
296 	else
297 		sprintf(s2, "?");
298 	sprintf(s0, "%lu.%lu|%s|%s",
299 		bw_meter->bm_start_time.tv_sec,
300 		bw_meter->bm_start_time.tv_usec,
301 		s1, s2);
302 	printf("  %-30s", s0);
303 
304 	/* The type of entry */
305 	sprintf(s0, "%s", "?");
306 	if (bw_meter->bm_flags & BW_METER_GEQ)
307 		sprintf(s0, "%s", ">=");
308 	else if (bw_meter->bm_flags & BW_METER_LEQ)
309 		sprintf(s0, "%s", "<=");
310 	printf("  %-3s", s0);
311 
312 	/* The threshold values */
313 	if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
314 		sprintf(s1, "%llu", (unsigned long long)bw_meter->bm_threshold.b_packets);
315 	else
316 		sprintf(s1, "?");
317 	if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
318 		sprintf(s2, "%llu", (unsigned long long)bw_meter->bm_threshold.b_bytes);
319 	else
320 		sprintf(s2, "?");
321 	sprintf(s0, "%lu.%lu|%s|%s",
322 		bw_meter->bm_threshold.b_time.tv_sec,
323 		bw_meter->bm_threshold.b_time.tv_usec,
324 		s1, s2);
325 	printf("  %-30s", s0);
326 
327 	/* Remaining time */
328 	timeradd(&bw_meter->bm_start_time,
329 		 &bw_meter->bm_threshold.b_time, &end);
330 	if (timercmp(&now, &end, <=)) {
331 		timersub(&end, &now, &delta);
332 		sprintf(s3, "%lu.%lu", delta.tv_sec, delta.tv_usec);
333 	} else {
334 		/* Negative time */
335 		timersub(&now, &end, &delta);
336 		sprintf(s3, "-%lu.%lu", delta.tv_sec, delta.tv_usec);
337 	}
338 	printf(" %s", s3);
339 
340 	printf("\n");
341 }
342 
343 void
344 mrt_stats(mrpaddr, mstaddr)
345 	u_long mrpaddr, mstaddr;
346 {
347 	u_int mrtproto;
348 	struct mrtstat mrtstat;
349 
350 	if (mrpaddr == 0) {
351 		printf("ip_mrtproto: symbol not in namelist\n");
352 		return;
353 	}
354 
355 	kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
356 	switch (mrtproto) {
357 	case 0:
358 		printf("no multicast routing compiled into this system\n");
359 		return;
360 
361 	case IGMP_DVMRP:
362 		break;
363 
364 	default:
365 		printf("multicast routing protocol %u, unknown\n", mrtproto);
366 		return;
367 	}
368 
369 	if (mstaddr == 0) {
370 		printf("mrtstat: symbol not in namelist\n");
371 		return;
372 	}
373 
374 	kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
375 	printf("multicast routing:\n");
376 	printf("\t%lu datagram%s with no route for origin\n",
377 	    mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
378 	printf("\t%lu upcall%s made to mrouted\n",
379 	    mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls));
380 	printf("\t%lu datagram%s with malformed tunnel options\n",
381 	    mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
382 	printf("\t%lu datagram%s with no room for tunnel options\n",
383 	    mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
384 	printf("\t%lu datagram%s arrived on wrong interface\n",
385 	    mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
386 	printf("\t%lu datagram%s dropped due to upcall Q overflow\n",
387 	    mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw));
388 	printf("\t%lu datagram%s dropped due to upcall socket overflow\n",
389 	    mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull));
390 	printf("\t%lu datagram%s cleaned up by the cache\n",
391 	    mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups));
392 	printf("\t%lu datagram%s dropped selectively by ratelimiter\n",
393 	    mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel));
394 	printf("\t%lu datagram%s dropped - bucket Q overflow\n",
395 	    mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow));
396 	printf("\t%lu datagram%s dropped - larger than bkt size\n",
397 	    mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large));
398 }
399