xref: /freebsd/usr.bin/netstat/mroute.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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 DVMRP multicast routing structures and statistics.
47  *
48  * MROUTING 1.0
49  */
50 
51 
52 #include <sys/param.h>
53 #include <sys/queue.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.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 <stdio.h>
67 #include <stdlib.h>
68 #include "netstat.h"
69 
70 void
71 mroutepr(mfcaddr, vifaddr)
72 	u_long mfcaddr, vifaddr;
73 {
74 	struct mfc *mfctable[MFCTBLSIZ];
75 	struct vif viftable[MAXVIFS];
76 	struct mfc mfc, *m;
77 	register struct vif *v;
78 	register vifi_t vifi;
79 	register int i;
80 	register int banner_printed;
81 	register int saved_nflag;
82 	vifi_t maxvif = 0;
83 
84 	if (mfcaddr == 0 || vifaddr == 0) {
85 		printf("No IPv4 multicast routing compiled into this system.\n");
86 		return;
87 	}
88 
89 	saved_nflag = nflag;
90 	nflag = 1;
91 
92 	kread(vifaddr, (char *)&viftable, sizeof(viftable));
93 	banner_printed = 0;
94 	for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
95 		if (v->v_lcl_addr.s_addr == 0)
96 			continue;
97 
98 		maxvif = vifi;
99 		if (!banner_printed) {
100 			printf("\nVirtual Interface Table\n"
101 			       " Vif   Thresh   Rate   Local-Address   "
102 			       "Remote-Address    Pkts-In   Pkts-Out\n");
103 			banner_printed = 1;
104 		}
105 
106 		printf(" %2u    %6u   %4d   %-15.15s",
107 					/* opposite math of add_vif() */
108 		    vifi, v->v_threshold, v->v_rate_limit * 1000 / 1024,
109 		    routename(v->v_lcl_addr.s_addr));
110 		printf(" %-15.15s", (v->v_flags & VIFF_TUNNEL) ?
111 		    routename(v->v_rmt_addr.s_addr) : "");
112 
113 		printf(" %9lu  %9lu\n", v->v_pkt_in, v->v_pkt_out);
114 	}
115 	if (!banner_printed)
116 		printf("\nVirtual Interface Table is empty\n");
117 
118 	kread(mfcaddr, (char *)&mfctable, sizeof(mfctable));
119 	banner_printed = 0;
120 	for (i = 0; i < MFCTBLSIZ; ++i) {
121 		m = mfctable[i];
122 		while(m) {
123 			kread((u_long)m, (char *)&mfc, sizeof mfc);
124 
125 			if (!banner_printed) {
126 				printf("\nMulticast Forwarding Cache\n"
127 				       " Origin          Group            "
128 				       " Packets In-Vif  Out-Vifs:Ttls\n");
129 				banner_printed = 1;
130 			}
131 
132 			printf(" %-15.15s", routename(mfc.mfc_origin.s_addr));
133 			printf(" %-15.15s", routename(mfc.mfc_mcastgrp.s_addr));
134 			printf(" %9lu", mfc.mfc_pkt_cnt);
135 			printf("  %3d   ", mfc.mfc_parent);
136 			for (vifi = 0; vifi <= maxvif; vifi++) {
137 				if (mfc.mfc_ttls[vifi] > 0)
138 					printf(" %u:%u", vifi,
139 					       mfc.mfc_ttls[vifi]);
140 			}
141 			printf("\n");
142 			m = mfc.mfc_next;
143 		}
144 	}
145 	if (!banner_printed)
146 		printf("\nMulticast Routing Table is empty\n");
147 
148 	printf("\n");
149 	nflag = saved_nflag;
150 }
151 
152 
153 void
154 mrt_stats(mstaddr)
155 	u_long mstaddr;
156 {
157 	struct mrtstat mrtstat;
158 
159 	if (mstaddr == 0) {
160 		printf("No multicast routing compiled into this system.\n");
161 		return;
162 	}
163 
164 	kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
165 	printf("multicast forwarding:\n");
166 	printf(" %10lu multicast forwarding cache lookup%s\n",
167 	  mrtstat.mrts_mfc_lookups, plural(mrtstat.mrts_mfc_lookups));
168 	printf(" %10lu multicast forwarding cache miss%s\n",
169 	  mrtstat.mrts_mfc_misses, plurales(mrtstat.mrts_mfc_misses));
170 	printf(" %10lu upcall%s to mrouted\n",
171 	  mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls));
172 	printf(" %10lu upcall queue overflow%s\n",
173 	  mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw));
174 	printf(" %10lu upcall%s dropped due to full socket buffer\n",
175 	  mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull));
176 	printf(" %10lu cache cleanup%s\n",
177 	  mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups));
178 	printf(" %10lu datagram%s with no route for origin\n",
179 	  mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
180 	printf(" %10lu datagram%s arrived with bad tunneling\n",
181 	  mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
182 	printf(" %10lu datagram%s could not be tunneled\n",
183 	  mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
184 	printf(" %10lu datagram%s arrived on wrong interface\n",
185 	  mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
186 	printf(" %10lu datagram%s selectively dropped\n",
187 	  mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel));
188 	printf(" %10lu datagram%s dropped due to queue overflow\n",
189 	  mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow));
190 	printf(" %10lu datagram%s dropped for being too large\n",
191 	  mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large));
192 }
193