xref: /netbsd-src/usr.bin/netstat/mroute.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: mroute.c,v 1.10 1996/05/11 13:51:27 mycroft Exp $	*/
2 
3 /*
4  * Copyright (c) 1989 Stephen Deering
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Stephen Deering of Stanford University.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	from: @(#)mroute.c	8.1 (Berkeley) 6/6/93
40  */
41 
42 /*
43  * Print DVMRP multicast routing structures and statistics.
44  *
45  * MROUTING 1.0
46  */
47 
48 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 
53 #include <net/if.h>
54 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/igmp.h>
57 #define _KERNEL
58 #include <netinet/ip_mroute.h>
59 #undef _KERNEL
60 
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include "netstat.h"
64 
65 char *
66 pktscale(n)
67 	u_long n;
68 {
69 	static char buf[8];
70 	char t;
71 
72 	if (n < 1024)
73 		t = ' ';
74 	else if (n < 1024 * 1024) {
75 		t = 'k';
76 		n /= 1024;
77 	} else {
78 		t = 'm';
79 		n /= 1048576;
80 	}
81 
82 	sprintf(buf, "%u%c", n, t);
83 	return (buf);
84 }
85 
86 void
87 mroutepr(mrpaddr, mfchashtbladdr, mfchashaddr, vifaddr)
88 	u_long mrpaddr, mfchashtbladdr, mfchashaddr, vifaddr;
89 {
90 	u_int mrtproto;
91 	LIST_HEAD(, mfc) *mfchashtbl;
92 	u_long mfchash;
93 	struct vif viftable[MAXVIFS];
94 	struct mfc *mfcp, mfc;
95 	register struct vif *v;
96 	register vifi_t vifi;
97 	struct in_addr *grp;
98 	int i;
99 	int banner_printed;
100 	int saved_nflag;
101 	int numvifs;
102 	int nmfc;		/* No. of cache entries */
103 
104 	if (mrpaddr == 0) {
105 		printf("ip_mrtproto: symbol not in namelist\n");
106 		return;
107 	}
108 
109 	kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
110 	switch (mrtproto) {
111 	case 0:
112 		printf("no multicast routing compiled into this system\n");
113 		return;
114 
115 	case IGMP_DVMRP:
116 		break;
117 
118 	default:
119 		printf("multicast routing protocol %u, unknown\n", mrtproto);
120 		return;
121 	}
122 
123 	if (mfchashtbladdr == 0) {
124 		printf("mfchashtbl: symbol not in namelist\n");
125 		return;
126 	}
127 	if (mfchashaddr == 0) {
128 		printf("mfchash: symbol not in namelist\n");
129 		return;
130 	}
131 	if (vifaddr == 0) {
132 		printf("viftable: symbol not in namelist\n");
133 		return;
134 	}
135 
136 	saved_nflag = nflag;
137 	nflag = 1;
138 
139 	kread(vifaddr, (char *)&viftable, sizeof(viftable));
140 	banner_printed = 0;
141 	numvifs = 0;
142 
143 	for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
144 		if (v->v_lcl_addr.s_addr == 0)
145 			continue;
146 		numvifs = vifi;
147 
148 		if (!banner_printed) {
149 			printf("\nVirtual Interface Table\n %s%s",
150 			    "Vif  Thresh  Limit  Local-Address    ",
151 			    "Remote-Address   Pkt_in  Pkt_out\n");
152 			banner_printed = 1;
153 		}
154 
155 		printf(" %3u     %3u  %5u  %-15.15s",
156 		    vifi, v->v_threshold, v->v_rate_limit,
157 		    routename(v->v_lcl_addr.s_addr));
158 		printf("  %-15.15s  %6u  %7u\n", (v->v_flags & VIFF_TUNNEL) ?
159 		    routename(v->v_rmt_addr.s_addr) : "",
160 		    v->v_pkt_in, v->v_pkt_out);
161 	}
162 	if (!banner_printed)
163 		printf("\nVirtual Interface Table is empty\n");
164 
165 	kread(mfchashtbladdr, (char *)&mfchashtbl, sizeof(mfchashtbl));
166 	kread(mfchashaddr, (char *)&mfchash, sizeof(mfchash));
167 	banner_printed = 0;
168 	nmfc = 0;
169 
170 	if (mfchashtbl != 0)
171 	for (i = 0; i <= mfchash; ++i) {
172 		kread((u_long)&mfchashtbl[i], (char *)&mfcp, sizeof(mfcp));
173 
174 		for (; mfcp != 0; mfcp = mfc.mfc_hash.le_next) {
175 			if (!banner_printed) {
176 				printf("\nMulticast Forwarding Cache\n %s%s",
177 				    "Hash  Origin           Mcastgroup       ",
178 				    "Traffic  In-Vif  Out-Vifs/Forw-ttl\n");
179 				banner_printed = 1;
180 			}
181 
182 			kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
183 			printf("  %3u  %-15.15s",
184 			    i, routename(mfc.mfc_origin.s_addr));
185 			printf("  %-15.15s  %7s     %3u ",
186 			    routename(mfc.mfc_mcastgrp.s_addr),
187 			    pktscale(mfc.mfc_pkt_cnt), mfc.mfc_parent);
188 			for (vifi = 0; vifi <= numvifs; ++vifi)
189 				if (mfc.mfc_ttls[vifi])
190 					printf(" %u/%u", vifi, mfc.mfc_ttls[vifi]);
191 
192 			printf("\n");
193 			nmfc++;
194 		}
195 	}
196 	if (!banner_printed)
197 		printf("\nMulticast Forwarding Cache is empty\n");
198 	else
199 		printf("\nTotal no. of entries in cache: %d\n", nmfc);
200 
201 	printf("\n");
202 	nflag = saved_nflag;
203 }
204 
205 
206 void
207 mrt_stats(mrpaddr, mstaddr)
208 	u_long mrpaddr, mstaddr;
209 {
210 	u_int mrtproto;
211 	struct mrtstat mrtstat;
212 
213 	if (mrpaddr == 0) {
214 		printf("ip_mrtproto: symbol not in namelist\n");
215 		return;
216 	}
217 
218 	kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
219 	switch (mrtproto) {
220 	case 0:
221 		printf("no multicast routing compiled into this system\n");
222 		return;
223 
224 	case IGMP_DVMRP:
225 		break;
226 
227 	default:
228 		printf("multicast routing protocol %u, unknown\n", mrtproto);
229 		return;
230 	}
231 
232 	if (mstaddr == 0) {
233 		printf("mrtstat: symbol not in namelist\n");
234 		return;
235 	}
236 
237 	kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
238 	printf("multicast routing:\n");
239 	printf("\t%d datagram%s with no route for origin\n",
240 	    mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
241 	printf("\t%d upcall%s made to mrouted\n",
242 	    mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls));
243 	printf("\t%d datagram%s with malformed tunnel options\n",
244 	    mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
245 	printf("\t%d datagram%s with no room for tunnel options\n",
246 	    mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
247 	printf("\t%d datagram%s arrived on wrong interface\n",
248 	    mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
249 	printf("\t%d datagram%s dropped due to upcall Q overflow\n",
250 	    mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw));
251 	printf("\t%d datagram%s dropped due to upcall socket overflow\n",
252 	    mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull));
253 	printf("\t%d datagram%s cleaned up by the cache\n",
254 	    mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups));
255 	printf("\t%d datagram%s dropped selectively by ratelimiter\n",
256 	    mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel));
257 	printf("\t%d datagram%s dropped - bucket Q overflow\n",
258 	    mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow));
259 	printf("\t%d datagram%s dropped - larger than bkt size\n",
260 	    mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large));
261 }
262