xref: /freebsd/usr.bin/netstat/mroute.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
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.1 (Berkeley) 6/6/93
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/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/igmp.h>
53 #define KERNEL 1
54 #include <netinet/ip_mroute.h>
55 #undef KERNEL
56 
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include "netstat.h"
60 
61 void
62 mroutepr(mrpaddr, mrtaddr, vifaddr)
63 	u_long mrpaddr, mrtaddr, vifaddr;
64 {
65 	u_int mrtproto;
66 	struct mrt *mrttable[MRTHASHSIZ];
67 	struct vif viftable[MAXVIFS];
68 	register struct mrt *mrt;
69 	struct mrt smrt;
70 	register struct vif *v;
71 	register vifi_t vifi;
72 	register struct in_addr *grp;
73 	register int i, n;
74 	register int banner_printed;
75 	register int saved_nflag;
76 
77 	if (mrpaddr == 0) {
78 		printf("ip_mrtproto: symbol not in namelist\n");
79 		return;
80 	}
81 
82 	kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
83 	switch (mrtproto) {
84 
85 	case 0:
86 		printf("no multicast routing compiled into this system\n");
87 		return;
88 
89 	case IGMP_DVMRP:
90 		break;
91 
92 	default:
93 		printf("multicast routing protocol %u, unknown\n", mrtproto);
94 		return;
95 	}
96 
97 	if (mrtaddr == 0) {
98 		printf("mrttable: symbol not in namelist\n");
99 		return;
100 	}
101 	if (vifaddr == 0) {
102 		printf("viftable: symbol not in namelist\n");
103 		return;
104 	}
105 
106 	saved_nflag = nflag;
107 	nflag = 1;
108 
109 	kread(vifaddr, (char *)&viftable, sizeof(viftable));
110 	banner_printed = 0;
111 	for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
112 		if (v->v_lcl_addr.s_addr == 0)
113 			continue;
114 
115 		if (!banner_printed) {
116 			printf("\nVirtual Interface Table\n%s%s",
117 			    " Vif   Threshold   Local-Address   ",
118 			    "Remote-Address   Groups\n");
119 			banner_printed = 1;
120 		}
121 
122 		printf(" %2u       %3u      %-15.15s",
123 		    vifi, v->v_threshold, routename(v->v_lcl_addr.s_addr));
124 		printf(" %-15.15s\n", (v->v_flags & VIFF_TUNNEL) ?
125 		    routename(v->v_rmt_addr.s_addr) : "");
126 
127 		n = v->v_lcl_grps_n;
128 		grp = (struct in_addr *)malloc(n * sizeof(*grp));
129 		if (grp == NULL) {
130 			printf("v_lcl_grps_n: malloc failed\n");
131 			return;
132 		}
133 		kread((u_long)v->v_lcl_grps, (caddr_t)grp, n * sizeof(*grp));
134 		for (i = 0; i < n; ++i)
135 			printf("%51s %-15.15s\n",
136 			    "", routename((grp++)->s_addr));
137 		free(grp);
138 	}
139 	if (!banner_printed)
140 		printf("\nVirtual Interface Table is empty\n");
141 
142 	kread(mrtaddr, (char *)&mrttable, sizeof(mrttable));
143 	banner_printed = 0;
144 	for (i = 0; i < MRTHASHSIZ; ++i) {
145 		for (mrt = mrttable[i]; mrt != NULL; mrt = mrt->mrt_next) {
146 			if (!banner_printed) {
147 				printf("\nMulticast Routing Table\n%s",
148 				    " Hash  Origin-Subnet  In-Vif  Out-Vifs\n");
149 				banner_printed = 1;
150 			}
151 
152 			kread((u_long)mrt, (char *)&smrt, sizeof(*mrt));
153 			mrt = &smrt;
154 			printf(" %3u   %-15.15s  %2u   ",
155 			    i, netname(mrt->mrt_origin.s_addr,
156 			    ntohl(mrt->mrt_originmask.s_addr)),
157 			    mrt->mrt_parent);
158 			for (vifi = 0; vifi < MAXVIFS; ++vifi)
159 				if (VIFM_ISSET(vifi, mrt->mrt_children))
160 					printf(" %u%c",
161 					    vifi,
162 					    VIFM_ISSET(vifi, mrt->mrt_leaves) ?
163 					    '*' : ' ');
164 			printf("\n");
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 routing:\n");
208 	printf(" %10u multicast route lookup%s\n",
209 	  mrtstat.mrts_mrt_lookups, plural(mrtstat.mrts_mrt_lookups));
210 	printf(" %10u multicast route cache miss%s\n",
211 	  mrtstat.mrts_mrt_misses, plurales(mrtstat.mrts_mrt_misses));
212 	printf(" %10u group address lookup%s\n",
213 	  mrtstat.mrts_grp_lookups, plural(mrtstat.mrts_grp_lookups));
214 	printf(" %10u group address cache miss%s\n",
215 	  mrtstat.mrts_grp_misses, plurales(mrtstat.mrts_grp_misses));
216 	printf(" %10u datagram%s with no route for origin\n",
217 	  mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
218 	printf(" %10u datagram%s with malformed tunnel options\n",
219 	  mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
220 	printf(" %10u datagram%s with no room for tunnel options\n",
221 	  mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
222 }
223