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