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