xref: /freebsd/usr.bin/netstat/mroute.c (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
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 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 /*
44  * Print multicast routing structures and statistics.
45  *
46  * MROUTING 1.0
47  */
48 
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/protosw.h>
55 #include <sys/mbuf.h>
56 #include <sys/time.h>
57 
58 #include <net/if.h>
59 #include <netinet/in.h>
60 #include <netinet/igmp.h>
61 #include <net/route.h>
62 #include <netinet/ip_mroute.h>
63 
64 #include <err.h>
65 #include <stdint.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include "netstat.h"
69 
70 static void print_bw_meter(struct bw_meter *bw_meter, int *banner_printed);
71 
72 void
73 mroutepr(u_long mfcaddr, u_long vifaddr)
74 {
75 	struct mfc *mfctable[MFCTBLSIZ];
76 	struct vif viftable[MAXVIFS];
77 	struct mfc mfc, *m;
78 	struct vif *v;
79 	vifi_t vifi;
80 	int i;
81 	int banner_printed;
82 	int saved_numeric_addr;
83 	vifi_t maxvif = 0;
84 	size_t len;
85 
86 	len = sizeof(mfctable);
87 	if (live) {
88 		if (sysctlbyname("net.inet.ip.mfctable", mfctable, &len, NULL,
89 		    0) < 0) {
90 			warn("sysctl: net.inet.ip.mfctable");
91 			return;
92 		}
93 	} else
94 		kread(mfcaddr, (char *)mfctable, sizeof(mfctable));
95 
96 	len = sizeof(viftable);
97 	if (live) {
98 		if (sysctlbyname("net.inet.ip.viftable", viftable, &len, NULL,
99 		    0) < 0) {
100 			warn("sysctl: net.inet.ip.viftable");
101 			return;
102 		}
103 	} else
104 		kread(vifaddr, (char *)viftable, sizeof(viftable));
105 
106 	saved_numeric_addr = numeric_addr;
107 	numeric_addr = 1;
108 
109 	banner_printed = 0;
110 	for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
111 		if (v->v_lcl_addr.s_addr == 0)
112 			continue;
113 
114 		maxvif = vifi;
115 		if (!banner_printed) {
116 			printf("\nIPv4 Virtual Interface Table\n"
117 			       " Vif   Thresh   Rate   Local-Address   "
118 			       "Remote-Address    Pkts-In   Pkts-Out\n");
119 			banner_printed = 1;
120 		}
121 
122 		printf(" %2u    %6u   %4d   %-15.15s",
123 					/* opposite math of add_vif() */
124 		    vifi, v->v_threshold, v->v_rate_limit * 1000 / 1024,
125 		    routename(v->v_lcl_addr.s_addr));
126 		printf(" %-15.15s", (v->v_flags & VIFF_TUNNEL) ?
127 		    routename(v->v_rmt_addr.s_addr) : "");
128 
129 		printf(" %9lu  %9lu\n", v->v_pkt_in, v->v_pkt_out);
130 	}
131 	if (!banner_printed)
132 		printf("\nIPv4 Virtual Interface Table is empty\n");
133 
134 	banner_printed = 0;
135 	for (i = 0; i < MFCTBLSIZ; ++i) {
136 		m = mfctable[i];
137 		while(m) {
138 			/* XXX KVM */
139 			kread((u_long)m, (char *)&mfc, sizeof mfc);
140 
141 			if (!banner_printed) {
142 				printf("\nIPv4 Multicast Forwarding Table\n"
143 				       " Origin          Group            "
144 				       " Packets In-Vif  Out-Vifs:Ttls\n");
145 				banner_printed = 1;
146 			}
147 
148 			printf(" %-15.15s", routename(mfc.mfc_origin.s_addr));
149 			printf(" %-15.15s", routename(mfc.mfc_mcastgrp.s_addr));
150 			printf(" %9lu", mfc.mfc_pkt_cnt);
151 			printf("  %3d   ", mfc.mfc_parent);
152 			for (vifi = 0; vifi <= maxvif; vifi++) {
153 				if (mfc.mfc_ttls[vifi] > 0)
154 					printf(" %u:%u", vifi,
155 					       mfc.mfc_ttls[vifi]);
156 			}
157 			printf("\n");
158 
159 			/* Print the bw meter information */
160 			{
161 				struct bw_meter bw_meter, *bwm;
162 				int banner_printed2 = 0;
163 
164 				bwm = mfc.mfc_bw_meter;
165 				while (bwm) {
166 				    /* XXX KVM */
167 				    kread((u_long)bwm, (char *)&bw_meter,
168 						sizeof bw_meter);
169 				    print_bw_meter(&bw_meter,
170 						&banner_printed2);
171 				    bwm = bw_meter.bm_mfc_next;
172 				}
173 #if 0	/* Don't ever print it? */
174 				if (! banner_printed2)
175 				    printf("\n  No Bandwidth Meters\n");
176 #endif
177 			}
178 
179 			m = mfc.mfc_next;
180 		}
181 	}
182 	if (!banner_printed)
183 		printf("\nIPv4 Multicast Forwarding Table is empty\n");
184 
185 	printf("\n");
186 	numeric_addr = saved_numeric_addr;
187 }
188 
189 static void
190 print_bw_meter(struct bw_meter *bw_meter, int *banner_printed)
191 {
192 	char s0[256], s1[256], s2[256], s3[256];
193 	struct timeval now, end, delta;
194 
195 	gettimeofday(&now, NULL);
196 
197 	if (! *banner_printed) {
198 		printf(" Bandwidth Meters\n");
199 		printf("  %-30s", "Measured(Start|Packets|Bytes)");
200 		printf(" %s", "Type");
201 		printf("  %-30s", "Thresh(Interval|Packets|Bytes)");
202 		printf(" Remain");
203 		printf("\n");
204 		*banner_printed = 1;
205 	}
206 
207 	/* The measured values */
208 	if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
209 		sprintf(s1, "%ju", (uintmax_t)bw_meter->bm_measured.b_packets);
210 	else
211 		sprintf(s1, "?");
212 	if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
213 		sprintf(s2, "%ju", (uintmax_t)bw_meter->bm_measured.b_bytes);
214 	else
215 		sprintf(s2, "?");
216 	sprintf(s0, "%lu.%lu|%s|%s",
217 		(u_long)bw_meter->bm_start_time.tv_sec,
218 		(u_long)bw_meter->bm_start_time.tv_usec,
219 		s1, s2);
220 	printf("  %-30s", s0);
221 
222 	/* The type of entry */
223 	sprintf(s0, "%s", "?");
224 	if (bw_meter->bm_flags & BW_METER_GEQ)
225 		sprintf(s0, "%s", ">=");
226 	else if (bw_meter->bm_flags & BW_METER_LEQ)
227 		sprintf(s0, "%s", "<=");
228 	printf("  %-3s", s0);
229 
230 	/* The threshold values */
231 	if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
232 		sprintf(s1, "%ju", (uintmax_t)bw_meter->bm_threshold.b_packets);
233 	else
234 		sprintf(s1, "?");
235 	if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
236 		sprintf(s2, "%ju", (uintmax_t)bw_meter->bm_threshold.b_bytes);
237 	else
238 		sprintf(s2, "?");
239 	sprintf(s0, "%lu.%lu|%s|%s",
240 		(u_long)bw_meter->bm_threshold.b_time.tv_sec,
241 		(u_long)bw_meter->bm_threshold.b_time.tv_usec,
242 		s1, s2);
243 	printf("  %-30s", s0);
244 
245 	/* Remaining time */
246 	timeradd(&bw_meter->bm_start_time,
247 		 &bw_meter->bm_threshold.b_time, &end);
248 	if (timercmp(&now, &end, <=)) {
249 		timersub(&end, &now, &delta);
250 		sprintf(s3, "%lu.%lu",
251 			(u_long)delta.tv_sec,
252 			(u_long)delta.tv_usec);
253 	} else {
254 		/* Negative time */
255 		timersub(&now, &end, &delta);
256 		sprintf(s3, "-%lu.%lu",
257 			(u_long)delta.tv_sec,
258 			(u_long)delta.tv_usec);
259 	}
260 	printf(" %s", s3);
261 
262 	printf("\n");
263 }
264 
265 void
266 mrt_stats(u_long mstaddr)
267 {
268 	struct mrtstat mrtstat;
269 	size_t len = sizeof mrtstat;
270 
271 	if (live) {
272 		if (sysctlbyname("net.inet.ip.mrtstat", &mrtstat, &len, NULL,
273 		    0) < 0) {
274 			warn("sysctl: net.inet.ip.mrtstat");
275 			return;
276 		}
277 	} else
278 		kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
279 
280 	printf("IPv4 multicast forwarding:\n");
281 
282 #define	p(f, m) if (mrtstat.f || sflag <= 1) \
283 	printf(m, mrtstat.f, plural(mrtstat.f))
284 #define	p2(f, m) if (mrtstat.f || sflag <= 1) \
285 	printf(m, mrtstat.f, plurales(mrtstat.f))
286 
287 	p(mrts_mfc_lookups, "\t%lu multicast forwarding cache lookup%s\n");
288 	p2(mrts_mfc_misses, "\t%lu multicast forwarding cache miss%s\n");
289 	p(mrts_upcalls, "\t%lu upcall%s to multicast routing daemon\n");
290 	p(mrts_upq_ovflw, "\t%lu upcall queue overflow%s\n");
291 	p(mrts_upq_sockfull,
292 	    "\t%lu upcall%s dropped due to full socket buffer\n");
293 	p(mrts_cache_cleanups, "\t%lu cache cleanup%s\n");
294 	p(mrts_no_route, "\t%lu datagram%s with no route for origin\n");
295 	p(mrts_bad_tunnel, "\t%lu datagram%s arrived with bad tunneling\n");
296 	p(mrts_cant_tunnel, "\t%lu datagram%s could not be tunneled\n");
297 	p(mrts_wrong_if, "\t%lu datagram%s arrived on wrong interface\n");
298 	p(mrts_drop_sel, "\t%lu datagram%s selectively dropped\n");
299 	p(mrts_q_overflow, "\t%lu datagram%s dropped due to queue overflow\n");
300 	p(mrts_pkt2large, "\t%lu datagram%s dropped for being too large\n");
301 
302 #undef	p2
303 #undef	p
304 }
305