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