xref: /original-bsd/usr.bin/netstat/mroute.c (revision 0842ddeb)
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  * %sccs.include.redist.c%
10  *
11  *	@(#)mroute.c	8.2 (Berkeley) 04/28/95
12  */
13 
14 /*
15  * Print DVMRP multicast routing structures and statistics.
16  *
17  * MROUTING 1.0
18  */
19 
20 #include <sys/param.h>
21 #include <sys/socket.h>
22 #include <sys/socketvar.h>
23 #include <sys/protosw.h>
24 
25 #include <netinet/in.h>
26 #include <netinet/igmp.h>
27 #define KERNEL 1
28 #include <netinet/ip_mroute.h>
29 #undef KERNEL
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "netstat.h"
34 
35 void
36 mroutepr(mrpaddr, mrtaddr, vifaddr)
37 	u_long mrpaddr, mrtaddr, vifaddr;
38 {
39 	u_int mrtproto;
40 	struct mrt *mrttable[MRTHASHSIZ];
41 	struct vif viftable[MAXVIFS];
42 	register struct mrt *mrt;
43 	struct mrt smrt;
44 	register struct vif *v;
45 	register vifi_t vifi;
46 	register struct in_addr *grp;
47 	register int i, n;
48 	register int banner_printed;
49 	register int saved_nflag;
50 
51 	if (mrpaddr == 0) {
52 		printf("ip_mrtproto: symbol not in namelist\n");
53 		return;
54 	}
55 
56 	kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
57 	switch (mrtproto) {
58 
59 	case 0:
60 		printf("no multicast routing compiled into this system\n");
61 		return;
62 
63 	case IGMP_DVMRP:
64 		break;
65 
66 	default:
67 		printf("multicast routing protocol %u, unknown\n", mrtproto);
68 		return;
69 	}
70 
71 	if (mrtaddr == 0) {
72 		printf("mrttable: symbol not in namelist\n");
73 		return;
74 	}
75 	if (vifaddr == 0) {
76 		printf("viftable: symbol not in namelist\n");
77 		return;
78 	}
79 
80 	saved_nflag = nflag;
81 	nflag = 1;
82 
83 	kread(vifaddr, (char *)&viftable, sizeof(viftable));
84 	banner_printed = 0;
85 	for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
86 		if (v->v_lcl_addr.s_addr == 0)
87 			continue;
88 
89 		if (!banner_printed) {
90 			printf("\nVirtual Interface Table\n%s%s",
91 			    " Vif   Threshold   Local-Address   ",
92 			    "Remote-Address   Groups\n");
93 			banner_printed = 1;
94 		}
95 
96 		printf(" %2u       %3u      %-15.15s",
97 		    vifi, v->v_threshold, routename(v->v_lcl_addr.s_addr));
98 		printf(" %-15.15s\n", (v->v_flags & VIFF_TUNNEL) ?
99 		    routename(v->v_rmt_addr.s_addr) : "");
100 
101 		n = v->v_lcl_grps_n;
102 		grp = (struct in_addr *)malloc(n * sizeof(*grp));
103 		if (grp == NULL) {
104 			printf("v_lcl_grps_n: malloc failed\n");
105 			return;
106 		}
107 		kread((u_long)v->v_lcl_grps, (caddr_t)grp, n * sizeof(*grp));
108 		for (i = 0; i < n; ++i)
109 			printf("%51s %-15.15s\n",
110 			    "", routename((grp++)->s_addr));
111 		free(grp);
112 	}
113 	if (!banner_printed)
114 		printf("\nVirtual Interface Table is empty\n");
115 
116 	kread(mrtaddr, (char *)&mrttable, sizeof(mrttable));
117 	banner_printed = 0;
118 	for (i = 0; i < MRTHASHSIZ; ++i) {
119 		for (mrt = mrttable[i]; mrt != NULL; mrt = mrt->mrt_next) {
120 			if (!banner_printed) {
121 				printf("\nMulticast Routing Table\n%s",
122 				    " Hash  Origin-Subnet  In-Vif  Out-Vifs\n");
123 				banner_printed = 1;
124 			}
125 
126 			kread((u_long)mrt, (char *)&smrt, sizeof(*mrt));
127 			mrt = &smrt;
128 			printf(" %3u   %-15.15s  %2u   ",
129 			    i, netname(mrt->mrt_origin.s_addr,
130 			    ntohl(mrt->mrt_originmask.s_addr)),
131 			    mrt->mrt_parent);
132 			for (vifi = 0; vifi < MAXVIFS; ++vifi)
133 				if (VIFM_ISSET(vifi, mrt->mrt_children))
134 					printf(" %u%c",
135 					    vifi,
136 					    VIFM_ISSET(vifi, mrt->mrt_leaves) ?
137 					    '*' : ' ');
138 			printf("\n");
139 		}
140 	}
141 	if (!banner_printed)
142 		printf("\nMulticast Routing Table is empty\n");
143 
144 	printf("\n");
145 	nflag = saved_nflag;
146 }
147 
148 
149 void
150 mrt_stats(mrpaddr, mstaddr)
151 	u_long mrpaddr, mstaddr;
152 {
153 	u_int mrtproto;
154 	struct mrtstat mrtstat;
155 
156 	if(mrpaddr == 0) {
157 		printf("ip_mrtproto: symbol not in namelist\n");
158 		return;
159 	}
160 
161 	kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
162 	switch (mrtproto) {
163 	    case 0:
164 		printf("no multicast routing compiled into this system\n");
165 		return;
166 
167 	    case IGMP_DVMRP:
168 		break;
169 
170 	    default:
171 		printf("multicast routing protocol %u, unknown\n", mrtproto);
172 		return;
173 	}
174 
175 	if (mstaddr == 0) {
176 		printf("mrtstat: symbol not in namelist\n");
177 		return;
178 	}
179 
180 	kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
181 	printf("multicast routing:\n");
182 	printf(" %10u multicast route lookup%s\n",
183 	  mrtstat.mrts_mrt_lookups, plural(mrtstat.mrts_mrt_lookups));
184 	printf(" %10u multicast route cache miss%s\n",
185 	  mrtstat.mrts_mrt_misses, plurales(mrtstat.mrts_mrt_misses));
186 	printf(" %10u group address lookup%s\n",
187 	  mrtstat.mrts_grp_lookups, plural(mrtstat.mrts_grp_lookups));
188 	printf(" %10u group address cache miss%s\n",
189 	  mrtstat.mrts_grp_misses, plurales(mrtstat.mrts_grp_misses));
190 	printf(" %10u datagram%s with no route for origin\n",
191 	  mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
192 	printf(" %10u datagram%s with malformed tunnel options\n",
193 	  mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
194 	printf(" %10u datagram%s with no room for tunnel options\n",
195 	  mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
196 	printf(" %10u datagram%s arrived on the wrong interface\n",
197 	  mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
198 }
199