xref: /freebsd/usr.bin/netstat/mroute.c (revision 61afd5bb22d787b0641523e7b9b95c964d669bd5)
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 /*
41  * Print DVMRP multicast routing structures and statistics.
42  *
43  * MROUTING 1.0
44  */
45 
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/mbuf.h>
52 #include <sys/time.h>
53 
54 #include <net/if.h>
55 #include <netinet/in.h>
56 #include <netinet/igmp.h>
57 #include <net/route.h>
58 #include <netinet/ip_mroute.h>
59 
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include "netstat.h"
63 
64 void
65 mroutepr(mrpaddr, mfcaddr, vifaddr)
66 	u_long mrpaddr, mfcaddr, vifaddr;
67 {
68 	u_int mrtproto;
69 	struct mbuf *mfctable[MFCTBLSIZ];
70 	struct vif viftable[MAXVIFS];
71 	struct mbuf mb, *m;
72 	struct mfc smfc;
73 	register struct vif *v;
74 	register vifi_t vifi;
75 	register int i;
76 	register int banner_printed;
77 	register int saved_nflag;
78 	vifi_t maxvif = 0;
79 
80 	if (mrpaddr == 0) {
81 		printf("ip_mrtproto: symbol not in namelist\n");
82 		return;
83 	}
84 
85 	kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
86 	switch (mrtproto) {
87 
88 	case 0:
89 		printf("no multicast routing compiled into this system\n");
90 		return;
91 
92 	case IGMP_DVMRP:
93 		break;
94 
95 	default:
96 		printf("multicast routing protocol %u, unknown\n", mrtproto);
97 		return;
98 	}
99 
100 	if (mfcaddr == 0) {
101 		printf("mfctable: symbol not in namelist\n");
102 		return;
103 	}
104 	if (vifaddr == 0) {
105 		printf("viftable: symbol not in namelist\n");
106 		return;
107 	}
108 
109 	saved_nflag = nflag;
110 	nflag = 1;
111 
112 	kread(vifaddr, (char *)&viftable, sizeof(viftable));
113 	banner_printed = 0;
114 	for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
115 		if (v->v_lcl_addr.s_addr == 0)
116 			continue;
117 
118 		maxvif = vifi;
119 		if (!banner_printed) {
120 			printf("\nVirtual Interface Table\n"
121 			       " Vif   Thresh   Rate   Local-Address   "
122 			       "Remote-Address    Pkts-In   Pkts-Out\n");
123 			banner_printed = 1;
124 		}
125 
126 		printf(" %2u    %6u   %4d   %-15.15s",
127 		    vifi, v->v_threshold, v->v_rate_limit,
128 		    routename(v->v_lcl_addr.s_addr));
129 		printf(" %-15.15s", (v->v_flags & VIFF_TUNNEL) ?
130 		    routename(v->v_rmt_addr.s_addr) : "");
131 
132 		printf(" %9lu  %9lu\n", v->v_pkt_in, v->v_pkt_out);
133 	}
134 	if (!banner_printed)
135 		printf("\nVirtual Interface Table is empty\n");
136 
137 	kread(mfcaddr, (char *)&mfctable, sizeof(mfctable));
138 	banner_printed = 0;
139 	for (i = 0; i < MFCTBLSIZ; ++i) {
140 		m = mfctable[i];
141 		while(m) {
142 			kread((u_long)m, (char *)&mb, sizeof mb);
143 			m = &mb;
144 
145 			if (!banner_printed) {
146 				printf("\nMulticast Forwarding Cache\n"
147 				       " Origin          Group            "
148 				       " Packets In-Vif  Out-Vifs:Ttls\n");
149 				banner_printed = 1;
150 			}
151 
152 			kread((u_long)mtod(m, char *),
153 			      (char *)&smfc, sizeof smfc);
154 			printf(" %-15.15s", routename(smfc.mfc_origin.s_addr));
155 			printf(" %-15.15s", routename(smfc.mfc_mcastgrp.s_addr));
156 			printf(" %9lu", smfc.mfc_pkt_cnt);
157 			printf("  %3d   ", smfc.mfc_parent);
158 			for (vifi = 0; vifi <= maxvif; vifi++) {
159 				if (smfc.mfc_ttls[vifi] > 0)
160 					printf(" %u:%u", vifi,
161 					       smfc.mfc_ttls[vifi]);
162 			}
163 			printf("\n");
164 			m = m->m_act;
165 		}
166 	}
167 	if (!banner_printed)
168 		printf("\nMulticast Routing Table is empty\n");
169 
170 	printf("\n");
171 	nflag = saved_nflag;
172 }
173 
174 
175 void
176 mrt_stats(mrpaddr, mstaddr)
177 	u_long mrpaddr, mstaddr;
178 {
179 	u_int mrtproto;
180 	struct mrtstat mrtstat;
181 
182 	if(mrpaddr == 0) {
183 		printf("ip_mrtproto: symbol not in namelist\n");
184 		return;
185 	}
186 
187 	kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
188 	switch (mrtproto) {
189 	    case 0:
190 		printf("no multicast routing compiled into this system\n");
191 		return;
192 
193 	    case IGMP_DVMRP:
194 		break;
195 
196 	    default:
197 		printf("multicast routing protocol %u, unknown\n", mrtproto);
198 		return;
199 	}
200 
201 	if (mstaddr == 0) {
202 		printf("mrtstat: symbol not in namelist\n");
203 		return;
204 	}
205 
206 	kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
207 	printf("multicast forwarding:\n");
208 	printf(" %10lu multicast forwarding cache lookup%s\n",
209 	  mrtstat.mrts_mfc_lookups, plural(mrtstat.mrts_mfc_lookups));
210 	printf(" %10lu multicast forwarding cache miss%s\n",
211 	  mrtstat.mrts_mfc_misses, plurales(mrtstat.mrts_mfc_misses));
212 	printf(" %10lu upcall%s to mrouted\n",
213 	  mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls));
214 	printf(" %10lu upcall queue overflow%s\n",
215 	  mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw));
216 	printf(" %10lu upcall%s dropped due to full socket buffer\n",
217 	  mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull));
218 	printf(" %10lu cache cleanup%s\n",
219 	  mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups));
220 	printf(" %10lu datagram%s with no route for origin\n",
221 	  mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
222 	printf(" %10lu datagram%s arrived with bad tunneling\n",
223 	  mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
224 	printf(" %10lu datagram%s could not be tunneled\n",
225 	  mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
226 	printf(" %10lu datagram%s arrived on wrong interface\n",
227 	  mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
228 	printf(" %10lu datagram%s selectively dropped\n",
229 	  mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel));
230 	printf(" %10lu datagram%s dropped due to queue overflow\n",
231 	  mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow));
232 	printf(" %10lu datagram%s dropped for being too large\n",
233 	  mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large));
234 }
235