xref: /netbsd-src/usr.bin/netstat/mroute.c (revision 466a16a118933bd295a8a104f095714fadf9cf68)
1 /*	$NetBSD: mroute.c,v 1.22 2008/12/29 01:33:03 christos 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.22 2008/12/29 01:33:03 christos 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 <kvm.h>
105 #include "netstat.h"
106 
107 static char *pktscale __P((u_long));
108 static void print_bw_meter __P((struct bw_meter *, int *));
109 
110 static char *
111 pktscale(n)
112 	u_long n;
113 {
114 	static char buf[20];
115 	char t;
116 
117 	if (n < 1024)
118 		t = ' ';
119 	else if (n < 1024 * 1024) {
120 		t = 'k';
121 		n /= 1024;
122 	} else {
123 		t = 'm';
124 		n /= 1048576;
125 	}
126 
127 	(void)snprintf(buf, sizeof buf, "%lu%c", n, t);
128 	return (buf);
129 }
130 
131 void
132 mroutepr(mrpaddr, mfchashtbladdr, mfchashaddr, vifaddr)
133 	u_long mrpaddr, mfchashtbladdr, mfchashaddr, vifaddr;
134 {
135 	u_int mrtproto;
136 	LIST_HEAD(, mfc) *mfchashtbl;
137 	u_long mfchash;
138 	struct vif viftable[MAXVIFS];
139 	struct mfc *mfcp, mfc;
140 	struct vif *v;
141 	vifi_t vifi;
142 	int i;
143 	int banner_printed;
144 	int saved_numeric_addr;
145 	int numvifs;
146 	int nmfc;		/* No. of cache entries */
147 
148 	if (mrpaddr == 0) {
149 		printf("ip_mrtproto: symbol not in namelist\n");
150 		return;
151 	}
152 
153 	kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
154 	switch (mrtproto) {
155 	case 0:
156 		printf("no multicast routing compiled into this system\n");
157 		return;
158 
159 	case IGMP_DVMRP:
160 		break;
161 
162 	default:
163 		printf("multicast routing protocol %u, unknown\n", mrtproto);
164 		return;
165 	}
166 
167 	if (mfchashtbladdr == 0) {
168 		printf("mfchashtbl: symbol not in namelist\n");
169 		return;
170 	}
171 	if (mfchashaddr == 0) {
172 		printf("mfchash: symbol not in namelist\n");
173 		return;
174 	}
175 	if (vifaddr == 0) {
176 		printf("viftable: symbol not in namelist\n");
177 		return;
178 	}
179 
180 	saved_numeric_addr = numeric_addr;
181 	numeric_addr = 1;
182 
183 	kread(vifaddr, (char *)&viftable, sizeof(viftable));
184 	banner_printed = 0;
185 	numvifs = 0;
186 
187 	for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
188 		if (v->v_lcl_addr.s_addr == 0)
189 			continue;
190 		numvifs = vifi;
191 
192 		if (!banner_printed) {
193 			printf("\nVirtual Interface Table\n %s%s",
194 			    "Vif  Thresh  Limit  Local-Address    ",
195 			    "Remote-Address   Pkt_in  Pkt_out\n");
196 			banner_printed = 1;
197 		}
198 
199 		printf(" %3u     %3u  %5u  %-15.15s",
200 		    vifi, v->v_threshold, v->v_rate_limit,
201 		    routename4(v->v_lcl_addr.s_addr));
202 		printf("  %-15.15s  %6lu  %7lu\n", (v->v_flags & VIFF_TUNNEL) ?
203 		    routename4(v->v_rmt_addr.s_addr) : "",
204 		    v->v_pkt_in, v->v_pkt_out);
205 	}
206 	if (!banner_printed)
207 		printf("\nVirtual Interface Table is empty\n");
208 
209 	kread(mfchashtbladdr, (char *)&mfchashtbl, sizeof(mfchashtbl));
210 	kread(mfchashaddr, (char *)&mfchash, sizeof(mfchash));
211 	banner_printed = 0;
212 	nmfc = 0;
213 
214 	if (mfchashtbl != 0)
215 	for (i = 0; i <= mfchash; ++i) {
216 		kread((u_long)&mfchashtbl[i], (char *)&mfcp, sizeof(mfcp));
217 
218 		for (; mfcp != 0; mfcp = mfc.mfc_hash.le_next) {
219 			if (!banner_printed) {
220 				printf("\nMulticast Forwarding Cache\n %s%s",
221 				    "Hash  Origin           Mcastgroup       ",
222 				    "Traffic  In-Vif  Out-Vifs/Forw-ttl\n");
223 				banner_printed = 1;
224 			}
225 
226 			kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
227 			printf("  %3u  %-15.15s",
228 			    i, routename4(mfc.mfc_origin.s_addr));
229 			printf("  %-15.15s  %7s     %3u ",
230 			    routename4(mfc.mfc_mcastgrp.s_addr),
231 			    pktscale(mfc.mfc_pkt_cnt), mfc.mfc_parent);
232 			for (vifi = 0; vifi <= numvifs; ++vifi)
233 				if (mfc.mfc_ttls[vifi])
234 					printf(" %u/%u", vifi, mfc.mfc_ttls[vifi]);
235 
236 			printf("\n");
237 
238 			/* Print the bw meter information */
239 			{
240 				struct bw_meter bw_meter, *bwm;
241 				int banner_printed2 = 0;
242 
243 				bwm = mfc.mfc_bw_meter;
244 				while (bwm) {
245 				kread((u_long)bwm,
246 				      (char *)&bw_meter,
247 				      sizeof bw_meter);
248 				print_bw_meter(&bw_meter,
249 					       &banner_printed2);
250 				bwm = bw_meter.bm_mfc_next;
251 				}
252 #if 0	/* Don't ever print it? */
253 				if (! banner_printed2)
254 				    printf("\n  No Bandwidth Meters\n");
255 #endif
256 			}
257 
258 			nmfc++;
259 		}
260 	}
261 	if (!banner_printed)
262 		printf("\nMulticast Forwarding Cache is empty\n");
263 	else
264 		printf("\nTotal no. of entries in cache: %d\n", nmfc);
265 
266 	printf("\n");
267 	numeric_addr = saved_numeric_addr;
268 }
269 
270 static void
271 print_bw_meter(bw_meter, banner_printed)
272 	struct bw_meter *bw_meter;
273 	int *banner_printed;
274 {
275 	char s0[256], s1[256], s2[256], s3[256];
276 	struct timeval now, end, delta;
277 
278 	gettimeofday(&now, NULL);
279 
280 	if (! *banner_printed) {
281 		printf(" Bandwidth Meters\n");
282 		printf("  %-30s", "Measured(Start|Packets|Bytes)");
283 		printf(" %s", "Type");
284 		printf("  %-30s", "Thresh(Interval|Packets|Bytes)");
285 		printf(" Remain");
286 		printf("\n");
287 		*banner_printed = 1;
288 	}
289 
290 	/* The measured values */
291 	if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
292 		sprintf(s1, "%llu", (unsigned long long)bw_meter->bm_measured.b_packets);
293 	else
294 		sprintf(s1, "?");
295 	if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
296 		sprintf(s2, "%llu", (unsigned long long)bw_meter->bm_measured.b_bytes);
297 	else
298 		sprintf(s2, "?");
299 	sprintf(s0, "%lld.%ld|%s|%s",
300 		(long long)bw_meter->bm_start_time.tv_sec,
301 		(long)bw_meter->bm_start_time.tv_usec,
302 		s1, s2);
303 	printf("  %-30s", s0);
304 
305 	/* The type of entry */
306 	sprintf(s0, "%s", "?");
307 	if (bw_meter->bm_flags & BW_METER_GEQ)
308 		sprintf(s0, "%s", ">=");
309 	else if (bw_meter->bm_flags & BW_METER_LEQ)
310 		sprintf(s0, "%s", "<=");
311 	printf("  %-3s", s0);
312 
313 	/* The threshold values */
314 	if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
315 		sprintf(s1, "%llu", (unsigned long long)bw_meter->bm_threshold.b_packets);
316 	else
317 		sprintf(s1, "?");
318 	if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
319 		sprintf(s2, "%llu", (unsigned long long)bw_meter->bm_threshold.b_bytes);
320 	else
321 		sprintf(s2, "?");
322 	sprintf(s0, "%lld.%ld|%s|%s",
323 		(long long)bw_meter->bm_threshold.b_time.tv_sec,
324 		(long)bw_meter->bm_threshold.b_time.tv_usec,
325 		s1, s2);
326 	printf("  %-30s", s0);
327 
328 	/* Remaining time */
329 	timeradd(&bw_meter->bm_start_time,
330 		 &bw_meter->bm_threshold.b_time, &end);
331 	if (timercmp(&now, &end, <=)) {
332 		timersub(&end, &now, &delta);
333 		sprintf(s3, "%lld.%ld",
334 		    (long long)delta.tv_sec, (long)delta.tv_usec);
335 	} else {
336 		/* Negative time */
337 		timersub(&now, &end, &delta);
338 		sprintf(s3, "-%lld.%ld",
339 		    (long long)delta.tv_sec, (long)delta.tv_usec);
340 	}
341 	printf(" %s", s3);
342 
343 	printf("\n");
344 }
345 
346 void
347 mrt_stats(mrpaddr, mstaddr)
348 	u_long mrpaddr, mstaddr;
349 {
350 	u_int mrtproto;
351 	struct mrtstat mrtstat;
352 
353 	if (mrpaddr == 0) {
354 		printf("ip_mrtproto: symbol not in namelist\n");
355 		return;
356 	}
357 
358 	kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
359 	switch (mrtproto) {
360 	case 0:
361 		printf("no multicast routing compiled into this system\n");
362 		return;
363 
364 	case IGMP_DVMRP:
365 		break;
366 
367 	default:
368 		printf("multicast routing protocol %u, unknown\n", mrtproto);
369 		return;
370 	}
371 
372 	if (mstaddr == 0) {
373 		printf("mrtstat: symbol not in namelist\n");
374 		return;
375 	}
376 
377 	kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
378 	printf("multicast routing:\n");
379 	printf("\t%lu datagram%s with no route for origin\n",
380 	    mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
381 	printf("\t%lu upcall%s made to mrouted\n",
382 	    mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls));
383 	printf("\t%lu datagram%s with malformed tunnel options\n",
384 	    mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
385 	printf("\t%lu datagram%s with no room for tunnel options\n",
386 	    mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
387 	printf("\t%lu datagram%s arrived on wrong interface\n",
388 	    mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
389 	printf("\t%lu datagram%s dropped due to upcall Q overflow\n",
390 	    mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw));
391 	printf("\t%lu datagram%s dropped due to upcall socket overflow\n",
392 	    mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull));
393 	printf("\t%lu datagram%s cleaned up by the cache\n",
394 	    mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups));
395 	printf("\t%lu datagram%s dropped selectively by ratelimiter\n",
396 	    mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel));
397 	printf("\t%lu datagram%s dropped - bucket Q overflow\n",
398 	    mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow));
399 	printf("\t%lu datagram%s dropped - larger than bkt size\n",
400 	    mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large));
401 }
402