xref: /freebsd/usr.bin/netstat/mroute.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*
2  * Copyright (c) 1989 Stephen Deering
3  * Copyright (c) 1992, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Stephen Deering of Stanford University.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)mroute.c	8.2 (Berkeley) 4/28/95
38  */
39 
40 #ifndef lint
41 static const char rcsid[] =
42   "$FreeBSD$";
43 #endif /* not lint */
44 
45 /*
46  * Print multicast routing structures and statistics.
47  *
48  * MROUTING 1.0
49  */
50 
51 #include <sys/param.h>
52 #include <sys/queue.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/sysctl.h>
56 #include <sys/protosw.h>
57 #include <sys/mbuf.h>
58 #include <sys/time.h>
59 
60 #include <net/if.h>
61 #include <netinet/in.h>
62 #include <netinet/igmp.h>
63 #include <net/route.h>
64 #include <netinet/ip_mroute.h>
65 
66 #include <err.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include "netstat.h"
70 
71 static void print_bw_meter(struct bw_meter *bw_meter, int *banner_printed);
72 
73 void
74 mroutepr(u_long mfcaddr, u_long vifaddr)
75 {
76 	struct mfc *mfctable[MFCTBLSIZ];
77 	struct vif viftable[MAXVIFS];
78 	struct mfc mfc, *m;
79 	struct vif *v;
80 	vifi_t vifi;
81 	int i;
82 	int banner_printed;
83 	int saved_numeric_addr;
84 	vifi_t maxvif = 0;
85 	size_t len;
86 
87 	len = sizeof(mfctable);
88 	if (sysctlbyname("net.inet.ip.mfctable", mfctable, &len, NULL, 0) < 0) {
89 		warn("sysctl: net.inet.ip.mfctable");
90 		/* Compatability with older kernels - candidate for removal */
91 		if (mfcaddr == 0) {
92 			printf("No IPv4 multicast routing compiled into this system.\n");
93 			return;
94 		}
95 
96 		kread(mfcaddr, (char *)mfctable, sizeof(mfctable));
97 	}
98 
99 	len = sizeof(viftable);
100 	if (sysctlbyname("net.inet.ip.viftable", viftable, &len, NULL, 0) < 0) {
101 		warn("sysctl: net.inet.ip.viftable");
102 		/* Compatability with older kernels - candidate for removal */
103 		if (vifaddr == 0) {
104 			printf("No IPv4 multicast routing compiled into this system.\n");
105 			return;
106 		}
107 
108 		kread(vifaddr, (char *)viftable, sizeof(viftable));
109 	}
110 
111 	saved_numeric_addr = numeric_addr;
112 	numeric_addr = 1;
113 
114 	banner_printed = 0;
115 	for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
116 		if (v->v_lcl_addr.s_addr == 0)
117 			continue;
118 
119 		maxvif = vifi;
120 		if (!banner_printed) {
121 			printf("\nVirtual Interface Table\n"
122 			       " Vif   Thresh   Rate   Local-Address   "
123 			       "Remote-Address    Pkts-In   Pkts-Out\n");
124 			banner_printed = 1;
125 		}
126 
127 		printf(" %2u    %6u   %4d   %-15.15s",
128 					/* opposite math of add_vif() */
129 		    vifi, v->v_threshold, v->v_rate_limit * 1000 / 1024,
130 		    routename(v->v_lcl_addr.s_addr));
131 		printf(" %-15.15s", (v->v_flags & VIFF_TUNNEL) ?
132 		    routename(v->v_rmt_addr.s_addr) : "");
133 
134 		printf(" %9lu  %9lu\n", v->v_pkt_in, v->v_pkt_out);
135 	}
136 	if (!banner_printed)
137 		printf("\nVirtual Interface Table is empty\n");
138 
139 	banner_printed = 0;
140 	for (i = 0; i < MFCTBLSIZ; ++i) {
141 		m = mfctable[i];
142 		while(m) {
143 			kread((u_long)m, (char *)&mfc, sizeof mfc);
144 
145 			if (!banner_printed) {
146 				printf("\nIPv4 Multicast Forwarding Cache\n"
147 				       " Origin          Group            "
148 				       " Packets In-Vif  Out-Vifs:Ttls\n");
149 				banner_printed = 1;
150 			}
151 
152 			printf(" %-15.15s", routename(mfc.mfc_origin.s_addr));
153 			printf(" %-15.15s", routename(mfc.mfc_mcastgrp.s_addr));
154 			printf(" %9lu", mfc.mfc_pkt_cnt);
155 			printf("  %3d   ", mfc.mfc_parent);
156 			for (vifi = 0; vifi <= maxvif; vifi++) {
157 				if (mfc.mfc_ttls[vifi] > 0)
158 					printf(" %u:%u", vifi,
159 					       mfc.mfc_ttls[vifi]);
160 			}
161 			printf("\n");
162 
163 			/* Print the bw meter information */
164 			{
165 				struct bw_meter bw_meter, *bwm;
166 				int banner_printed2 = 0;
167 
168 				bwm = mfc.mfc_bw_meter;
169 				while (bwm) {
170 				    kread((u_long)bwm, (char *)&bw_meter,
171 						sizeof bw_meter);
172 				    print_bw_meter(&bw_meter,
173 						&banner_printed2);
174 				    bwm = bw_meter.bm_mfc_next;
175 				}
176 #if 0	/* Don't ever print it? */
177 				if (! banner_printed2)
178 				    printf("\n  No Bandwidth Meters\n");
179 #endif
180 			}
181 
182 			m = mfc.mfc_next;
183 		}
184 	}
185 	if (!banner_printed)
186 		printf("\nMulticast Routing Table is empty\n");
187 
188 	printf("\n");
189 	numeric_addr = saved_numeric_addr;
190 }
191 
192 static void
193 print_bw_meter(struct bw_meter *bw_meter, int *banner_printed)
194 {
195 	char s0[256], s1[256], s2[256], s3[256];
196 	struct timeval now, end, delta;
197 
198 	gettimeofday(&now, NULL);
199 
200 	if (! *banner_printed) {
201 		printf(" Bandwidth Meters\n");
202 		printf("  %-30s", "Measured(Start|Packets|Bytes)");
203 		printf(" %s", "Type");
204 		printf("  %-30s", "Thresh(Interval|Packets|Bytes)");
205 		printf(" Remain");
206 		printf("\n");
207 		*banner_printed = 1;
208 	}
209 
210 	/* The measured values */
211 	if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
212 		sprintf(s1, "%llu", bw_meter->bm_measured.b_packets);
213 	else
214 		sprintf(s1, "?");
215 	if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
216 		sprintf(s2, "%llu", bw_meter->bm_measured.b_bytes);
217 	else
218 		sprintf(s2, "?");
219 	sprintf(s0, "%lu.%lu|%s|%s",
220 		bw_meter->bm_start_time.tv_sec,
221 		bw_meter->bm_start_time.tv_usec,
222 		s1, s2);
223 	printf("  %-30s", s0);
224 
225 	/* The type of entry */
226 	sprintf(s0, "%s", "?");
227 	if (bw_meter->bm_flags & BW_METER_GEQ)
228 		sprintf(s0, "%s", ">=");
229 	else if (bw_meter->bm_flags & BW_METER_LEQ)
230 		sprintf(s0, "%s", "<=");
231 	printf("  %-3s", s0);
232 
233 	/* The threshold values */
234 	if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
235 		sprintf(s1, "%llu", bw_meter->bm_threshold.b_packets);
236 	else
237 		sprintf(s1, "?");
238 	if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
239 		sprintf(s2, "%llu", bw_meter->bm_threshold.b_bytes);
240 	else
241 		sprintf(s2, "?");
242 	sprintf(s0, "%lu.%lu|%s|%s",
243 		bw_meter->bm_threshold.b_time.tv_sec,
244 		bw_meter->bm_threshold.b_time.tv_usec,
245 		s1, s2);
246 	printf("  %-30s", s0);
247 
248 	/* Remaining time */
249 	timeradd(&bw_meter->bm_start_time,
250 		 &bw_meter->bm_threshold.b_time, &end);
251 	if (timercmp(&now, &end, <=)) {
252 		timersub(&end, &now, &delta);
253 		sprintf(s3, "%lu.%lu", delta.tv_sec, delta.tv_usec);
254 	} else {
255 		/* Negative time */
256 		timersub(&now, &end, &delta);
257 		sprintf(s3, "-%lu.%lu", delta.tv_sec, delta.tv_usec);
258 	}
259 	printf(" %s", s3);
260 
261 	printf("\n");
262 }
263 
264 void
265 mrt_stats(u_long mstaddr)
266 {
267 	struct mrtstat mrtstat;
268 	size_t len = sizeof mrtstat;
269 
270 	if (sysctlbyname("net.inet.ip.mrtstat", &mrtstat, &len,
271 				NULL, 0) < 0) {
272 		warn("sysctl: net.inet.ip.mrtstat");
273 		/* Compatability with older kernels - candidate for removal */
274 		if (mstaddr == 0) {
275 			printf("No IPv4 multicast routing compiled into this system.\n");
276 			return;
277 		}
278 
279 		kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
280 	}
281 	printf("IPv4 multicast forwarding:\n");
282 
283 #define	p(f, m) if (mrtstat.f || sflag <= 1) \
284 	printf(m, mrtstat.f, plural(mrtstat.f))
285 #define	p2(f, m) if (mrtstat.f || sflag <= 1) \
286 	printf(m, mrtstat.f, plurales(mrtstat.f))
287 
288 	p(mrts_mfc_lookups, "\t%lu multicast forwarding cache lookup%s\n");
289 	p2(mrts_mfc_misses, "\t%lu multicast forwarding cache miss%s\n");
290 	p(mrts_upcalls, "\t%lu upcall%s to mrouted\n");
291 	p(mrts_upq_ovflw, "\t%lu upcall queue overflow%s\n");
292 	p(mrts_upq_sockfull,
293 	    "\t%lu upcall%s dropped due to full socket buffer\n");
294 	p(mrts_cache_cleanups, "\t%lu cache cleanup%s\n");
295 	p(mrts_no_route, "\t%lu datagram%s with no route for origin\n");
296 	p(mrts_bad_tunnel, "\t%lu datagram%s arrived with bad tunneling\n");
297 	p(mrts_cant_tunnel, "\t%lu datagram%s could not be tunneled\n");
298 	p(mrts_wrong_if, "\t%lu datagram%s arrived on wrong interface\n");
299 	p(mrts_drop_sel, "\t%lu datagram%s selectively dropped\n");
300 	p(mrts_q_overflow, "\t%lu datagram%s dropped due to queue overflow\n");
301 	p(mrts_pkt2large, "\t%lu datagram%s dropped for being too large\n");
302 
303 #undef	p2
304 #undef	p
305 }
306