xref: /freebsd/usr.bin/netstat/mroute6.c (revision b740c88bfb6453416926271c089262e7164dace3)
1 /*-
2  * Copyright (C) 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 /*-
30  * Copyright (c) 1989 Stephen Deering
31  * Copyright (c) 1992, 1993
32  *	The Regents of the University of California.  All rights reserved.
33  *
34  * This code is derived from software contributed to Berkeley by
35  * Stephen Deering of Stanford University.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)mroute.c	8.2 (Berkeley) 4/28/95
66  */
67 
68 #include <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
70 
71 #ifdef INET6
72 #include <sys/param.h>
73 #include <sys/queue.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sysctl.h>
77 #include <sys/protosw.h>
78 #include <sys/mbuf.h>
79 #include <sys/time.h>
80 
81 #include <net/if.h>
82 #include <net/route.h>
83 
84 #include <netinet/in.h>
85 
86 #include <err.h>
87 #include <stdint.h>
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <stdbool.h>
91 #include <libxo/xo.h>
92 
93 #define	KERNEL 1
94 #include <netinet6/ip6_mroute.h>
95 #undef KERNEL
96 
97 #include "netstat.h"
98 
99 #define	WID_ORG	(Wflag ? 39 : (numeric_addr ? 29 : 18)) /* width of origin column */
100 #define	WID_GRP	(Wflag ? 18 : (numeric_addr ? 16 : 18)) /* width of group column */
101 
102 void
103 mroute6pr()
104 {
105 	struct mf6c *mf6ctable[MF6CTBLSIZ], *mfcp;
106 	struct mif6_sctl mif6table[MAXMIFS];
107 	struct mf6c mfc;
108 	struct rtdetq rte, *rtep;
109 	struct mif6_sctl *mifp;
110 	mifi_t mifi;
111 	int i;
112 	int banner_printed;
113 	int saved_numeric_addr;
114 	mifi_t maxmif = 0;
115 	long int waitings;
116 	size_t len;
117 
118 	if (live == 0)
119 		return;
120 
121 	len = sizeof(mif6table);
122 	if (sysctlbyname("net.inet6.ip6.mif6table", mif6table, &len, NULL, 0) <
123 	    0) {
124 		xo_warn("sysctl: net.inet6.ip6.mif6table");
125 		return;
126 	}
127 
128 	saved_numeric_addr = numeric_addr;
129 	numeric_addr = 1;
130 	banner_printed = 0;
131 
132 	for (mifi = 0, mifp = mif6table; mifi < MAXMIFS; ++mifi, ++mifp) {
133 		char ifname[IFNAMSIZ];
134 
135 		if (mifp->m6_ifp == 0)
136 			continue;
137 
138 		maxmif = mifi;
139 		if (!banner_printed) {
140 			xo_open_list("multicast-interface");
141 			xo_emit("\n{T:IPv6 Multicast Interface Table}\n"
142 			    "{T: Mif   Rate   PhyIF   Pkts-In   Pkts-Out}\n");
143 			banner_printed = 1;
144 		}
145 
146 		xo_open_instance("multicast-interface");
147 		xo_emit("  {:mif/%2u}   {:rate-limit/%4d}",
148 		    mifi, mifp->m6_rate_limit);
149 		xo_emit("   {:ifname/%5s}", (mifp->m6_flags & MIFF_REGISTER) ?
150 		    "reg0" : if_indextoname(mifp->m6_ifp, ifname));
151 
152 		xo_emit(" {:received-packets/%9ju}  {:sent-packets/%9ju}\n",
153 		    (uintmax_t)mifp->m6_pkt_in,
154 		    (uintmax_t)mifp->m6_pkt_out);
155 		xo_close_instance("multicast-interface");
156 	}
157 	if (banner_printed)
158 		xo_open_list("multicast-interface");
159 	else
160 		xo_emit("\n{T:IPv6 Multicast Interface Table is empty}\n");
161 
162 	len = sizeof(mf6ctable);
163 	if (sysctlbyname("net.inet6.ip6.mf6ctable", mf6ctable, &len, NULL, 0) <
164 	    0) {
165 		xo_warn("sysctl: net.inet6.ip6.mf6ctable");
166 		return;
167 	}
168 
169 	banner_printed = 0;
170 
171 	for (i = 0; i < MF6CTBLSIZ; ++i) {
172 		mfcp = mf6ctable[i];
173 		while(mfcp) {
174 			kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
175 			if (!banner_printed) {
176 				xo_open_list("multicast-forwarding-cache");
177 				xo_emit("\n"
178 				    "{T:IPv6 Multicast Forwarding Cache}\n");
179 				xo_emit(" {T:%-*.*s} {T:%-*.*s} {T:%s}",
180 				    WID_ORG, WID_ORG, "Origin",
181 				    WID_GRP, WID_GRP, "Group",
182 				    "  Packets Waits In-Mif  Out-Mifs\n");
183 				banner_printed = 1;
184 			}
185 
186 			xo_open_instance("multicast-forwarding-cache");
187 
188 			xo_emit(" {:origin/%-*.*s}", WID_ORG, WID_ORG,
189 			    routename6(&mfc.mf6c_origin));
190 			xo_emit(" {:group/%-*.*s}", WID_GRP, WID_GRP,
191 			    routename6(&mfc.mf6c_mcastgrp));
192 			xo_emit(" {:total-packets/%9ju}",
193 			    (uintmax_t)mfc.mf6c_pkt_cnt);
194 
195 			for (waitings = 0, rtep = mfc.mf6c_stall; rtep; ) {
196 				waitings++;
197 				/* XXX KVM */
198 				kread((u_long)rtep, (char *)&rte, sizeof(rte));
199 				rtep = rte.next;
200 			}
201 			xo_emit("   {:waitings/%3ld}", waitings);
202 
203 			if (mfc.mf6c_parent == MF6C_INCOMPLETE_PARENT)
204 				xo_emit(" ---   ");
205 			else
206 				xo_emit("  {:parent/%3d}   ", mfc.mf6c_parent);
207 			xo_open_list("mif");
208 			for (mifi = 0; mifi <= maxmif; mifi++) {
209 				if (IF_ISSET(mifi, &mfc.mf6c_ifset))
210 					xo_emit(" {l:%u}", mifi);
211 			}
212 			xo_close_list("mif");
213 			xo_emit("\n");
214 
215 			mfcp = mfc.mf6c_next;
216 			xo_close_instance("multicast-forwarding-cache");
217 		}
218 	}
219 	if (banner_printed)
220 		xo_close_list("multicast-forwarding-cache");
221 	else
222 		xo_emit("\n{T:IPv6 Multicast Forwarding Table is empty}\n");
223 
224 	xo_emit("\n");
225 	numeric_addr = saved_numeric_addr;
226 }
227 
228 void
229 mrt6_stats()
230 {
231 	struct mrt6stat mrtstat;
232 	size_t len = sizeof mrtstat;
233 
234 	if (sysctlbyname("net.inet6.ip6.mrt6stat", &mrtstat, &len, NULL, 0) <
235 	    0) {
236 		xo_warn("sysctl: net.inet6.ip6.mrt6stat");
237 		return;
238 	}
239 
240 	xo_open_container("multicast-statistics");
241 	xo_emit("{T:IPv6 multicast forwarding}:\n");
242 
243 #define	p(f, m) if (mrtstat.f || sflag <= 1) \
244 	xo_emit(m, (uintmax_t)mrtstat.f, plural(mrtstat.f))
245 #define	p2(f, m) if (mrtstat.f || sflag <= 1) \
246 	xo_emit(m, (uintmax_t)mrtstat.f, plurales(mrtstat.f))
247 
248 	p(mrt6s_mfc_lookups, "\t{:cache-lookups/%ju} "
249 	    "{N:/multicast forwarding cache lookup%s}\n");
250 	p2(mrt6s_mfc_misses, "\t{:cache-misses/%ju} "
251 	    "{N:/multicast forwarding cache miss%s}\n");
252 	p(mrt6s_upcalls, "\t{:upcalls/%ju} "
253 	    "{N:/upcall%s to multicast routing daemon}\n");
254 	p(mrt6s_upq_ovflw, "\t{:upcall-overflows/%ju} "
255 	    "{N:/upcall queue overflow%s}\n");
256 	p(mrt6s_upq_sockfull, "\t{:upcalls-dropped-full-buffer/%ju} "
257 	    "{N:/upcall%s dropped due to full socket buffer}\n");
258 	p(mrt6s_cache_cleanups, "\t{:cache-cleanups/%ju} "
259 	    "{N:/cache cleanup%s}\n");
260 	p(mrt6s_no_route, "\t{:dropped-no-origin/%ju} "
261 	    "{N:/datagram%s with no route for origin}\n");
262 	p(mrt6s_bad_tunnel, "\t{:dropped-bad-tunnel/%ju} "
263 	    "{N:/datagram%s arrived with bad tunneling}\n");
264 	p(mrt6s_cant_tunnel, "\t{:dropped-could-not-tunnel/%ju} "
265 	    "{N:/datagram%s could not be tunneled}\n");
266 	p(mrt6s_wrong_if, "\t{:dropped-wrong-incoming-interface/%ju} "
267 	    "{N:/datagram%s arrived on wrong interface}\n");
268 	p(mrt6s_drop_sel, "\t{:dropped-selectively/%ju} "
269 	    "{N:/datagram%s selectively dropped}\n");
270 	p(mrt6s_q_overflow, "\t{:dropped-queue-overflow/%ju} "
271 	    "{N:/datagram%s dropped due to queue overflow}\n");
272 	p(mrt6s_pkt2large, "\t{:dropped-too-large/%ju} "
273 	    "{N:/datagram%s dropped for being too large}\n");
274 
275 #undef	p2
276 #undef	p
277 	xo_close_container("multicast-statistics");
278 }
279 #endif /*INET6*/
280