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