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