xref: /freebsd/usr.bin/netstat/mroute6.c (revision ec0e626bafb335b30c499d06066997f54b10c092)
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/if_var.h>
83 #include <net/route.h>
84 
85 #include <netinet/in.h>
86 
87 #include <err.h>
88 #include <nlist.h>
89 #include <stdint.h>
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <stdbool.h>
93 #include <libxo/xo.h>
94 
95 #define	KERNEL 1
96 #include <netinet6/ip6_mroute.h>
97 #undef KERNEL
98 
99 #include "netstat.h"
100 
101 /*
102  * kvm(3) bindings for every needed symbol
103  */
104 static struct nlist mrl[] = {
105 #define	N_MF6CTABLE	0
106 	{ .n_name = "_mf6ctable" },
107 #define	N_MIF6TABLE	1
108 	{ .n_name = "_mif6table" },
109 #define	N_MRT6STAT	2
110 	{ .n_name = "_mrt6stat" },
111 	{ .n_name = NULL },
112 };
113 
114 
115 #define	WID_ORG	(Wflag ? 39 : (numeric_addr ? 29 : 18)) /* width of origin column */
116 #define	WID_GRP	(Wflag ? 18 : (numeric_addr ? 16 : 18)) /* width of group column */
117 
118 void
119 mroute6pr()
120 {
121 	struct mf6c *mf6ctable[MF6CTBLSIZ], *mfcp;
122 	struct mif6 mif6table[MAXMIFS];
123 	struct mf6c mfc;
124 	struct rtdetq rte, *rtep;
125 	struct mif6 *mifp;
126 	u_long mfcaddr, mifaddr;
127 	mifi_t mifi;
128 	int i;
129 	int banner_printed;
130 	int saved_numeric_addr;
131 	mifi_t maxmif = 0;
132 	long int waitings;
133 	size_t len;
134 
135 	kresolve_list(mrl);
136 	mfcaddr = mrl[N_MF6CTABLE].n_value;
137 	mifaddr = mrl[N_MIF6TABLE].n_value;
138 
139 	if (mfcaddr == 0 || mifaddr == 0) {
140 		fprintf(stderr, "No IPv6 MROUTING kernel support.\n");
141 		return;
142 	}
143 
144 	len = sizeof(mif6table);
145 	if (live) {
146 		if (sysctlbyname("net.inet6.ip6.mif6table", mif6table, &len,
147 		    NULL, 0) < 0) {
148 			xo_warn("sysctl: net.inet6.ip6.mif6table");
149 			return;
150 		}
151 	} else
152 		kread(mifaddr, (char *)mif6table, sizeof(mif6table));
153 
154 	saved_numeric_addr = numeric_addr;
155 	numeric_addr = 1;
156 	banner_printed = 0;
157 
158 	for (mifi = 0, mifp = mif6table; mifi < MAXMIFS; ++mifi, ++mifp) {
159 		struct ifnet ifnet;
160 		char ifname[IFNAMSIZ];
161 
162 		if (mifp->m6_ifp == NULL)
163 			continue;
164 
165 		/* XXX KVM */
166 		kread((u_long)mifp->m6_ifp, (char *)&ifnet, sizeof(ifnet));
167 
168 		maxmif = mifi;
169 		if (!banner_printed) {
170 			xo_open_list("multicast-interface");
171 			xo_emit("\n{T:IPv6 Multicast Interface Table}\n"
172 			    "{T: Mif   Rate   PhyIF   Pkts-In   Pkts-Out}\n");
173 			banner_printed = 1;
174 		}
175 
176 		xo_open_instance("multicast-interface");
177 		xo_emit("  {:mif/%2u}   {:rate-limit/%4d}",
178 		    mifi, mifp->m6_rate_limit);
179 		xo_emit("   {:ifname/%5s}", (mifp->m6_flags & MIFF_REGISTER) ?
180 		    "reg0" : if_indextoname(ifnet.if_index, ifname));
181 
182 		xo_emit(" {:received-packets/%9ju}  {:sent-packets/%9ju}\n",
183 		    (uintmax_t)mifp->m6_pkt_in,
184 		    (uintmax_t)mifp->m6_pkt_out);
185 		xo_close_instance("multicast-interface");
186 	}
187 	if (banner_printed)
188 		xo_open_list("multicast-interface");
189 	else
190 		xo_emit("\n{T:IPv6 Multicast Interface Table is empty}\n");
191 
192 	len = sizeof(mf6ctable);
193 	if (live) {
194 		if (sysctlbyname("net.inet6.ip6.mf6ctable", mf6ctable, &len,
195 		    NULL, 0) < 0) {
196 			xo_warn("sysctl: net.inet6.ip6.mf6ctable");
197 			return;
198 		}
199 	} else
200 		kread(mfcaddr, (char *)mf6ctable, sizeof(mf6ctable));
201 
202 	banner_printed = 0;
203 
204 	for (i = 0; i < MF6CTBLSIZ; ++i) {
205 		mfcp = mf6ctable[i];
206 		while(mfcp) {
207 			kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
208 			if (!banner_printed) {
209 				xo_open_list("multicast-forwarding-cache");
210 				xo_emit("\n"
211 				    "{T:IPv6 Multicast Forwarding Cache}\n");
212 				xo_emit(" {T:%-*.*s} {T:%-*.*s} {T:%s}",
213 				    WID_ORG, WID_ORG, "Origin",
214 				    WID_GRP, WID_GRP, "Group",
215 				    "  Packets Waits In-Mif  Out-Mifs\n");
216 				banner_printed = 1;
217 			}
218 
219 			xo_open_instance("multicast-forwarding-cache");
220 
221 			xo_emit(" {:origin/%-*.*s}", WID_ORG, WID_ORG,
222 			    routename6(&mfc.mf6c_origin));
223 			xo_emit(" {:group/%-*.*s}", WID_GRP, WID_GRP,
224 			    routename6(&mfc.mf6c_mcastgrp));
225 			xo_emit(" {:total-packets/%9ju}",
226 			    (uintmax_t)mfc.mf6c_pkt_cnt);
227 
228 			for (waitings = 0, rtep = mfc.mf6c_stall; rtep; ) {
229 				waitings++;
230 				/* XXX KVM */
231 				kread((u_long)rtep, (char *)&rte, sizeof(rte));
232 				rtep = rte.next;
233 			}
234 			xo_emit("   {:waitings/%3ld}", waitings);
235 
236 			if (mfc.mf6c_parent == MF6C_INCOMPLETE_PARENT)
237 				xo_emit(" ---   ");
238 			else
239 				xo_emit("  {:parent/%3d}   ", mfc.mf6c_parent);
240 			xo_open_list("mif");
241 			for (mifi = 0; mifi <= maxmif; mifi++) {
242 				if (IF_ISSET(mifi, &mfc.mf6c_ifset))
243 					xo_emit(" {l:%u}", mifi);
244 			}
245 			xo_close_list("mif");
246 			xo_emit("\n");
247 
248 			mfcp = mfc.mf6c_next;
249 			xo_close_instance("multicast-forwarding-cache");
250 		}
251 	}
252 	if (banner_printed)
253 		xo_close_list("multicast-forwarding-cache");
254 	else
255 		xo_emit("\n{T:IPv6 Multicast Forwarding Table is empty}\n");
256 
257 	xo_emit("\n");
258 	numeric_addr = saved_numeric_addr;
259 }
260 
261 void
262 mrt6_stats()
263 {
264 	struct mrt6stat mrtstat;
265 	u_long mstaddr;
266 	size_t len = sizeof mrtstat;
267 
268 	kresolve_list(mrl);
269 	mstaddr = mrl[N_MRT6STAT].n_value;
270 
271 	if (mstaddr == 0) {
272 		fprintf(stderr, "No IPv6 MROUTING kernel support.\n");
273 		return;
274 	}
275 
276 	if (live) {
277 		if (sysctlbyname("net.inet6.ip6.mrt6stat", &mrtstat, &len,
278 		    NULL, 0) < 0) {
279 			xo_warn("sysctl: net.inet6.ip6.mrt6stat");
280 			return;
281 		}
282 	} else
283 		kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
284 
285 	xo_open_container("multicast-statistics");
286 	xo_emit("{T:IPv6 multicast forwarding}:\n");
287 
288 #define	p(f, m) if (mrtstat.f || sflag <= 1) \
289 	xo_emit(m, (uintmax_t)mrtstat.f, plural(mrtstat.f))
290 #define	p2(f, m) if (mrtstat.f || sflag <= 1) \
291 	xo_emit(m, (uintmax_t)mrtstat.f, plurales(mrtstat.f))
292 
293 	p(mrt6s_mfc_lookups, "\t{:cache-lookups/%ju} "
294 	    "{N:/multicast forwarding cache lookup%s}\n");
295 	p2(mrt6s_mfc_misses, "\t{:cache-misses/%ju} "
296 	    "{N:/multicast forwarding cache miss%s}\n");
297 	p(mrt6s_upcalls, "\t{:upcalls/%ju} "
298 	    "{N:/upcall%s to multicast routing daemon}\n");
299 	p(mrt6s_upq_ovflw, "\t{:upcall-overflows/%ju} "
300 	    "{N:/upcall queue overflow%s}\n");
301 	p(mrt6s_upq_sockfull, "\t{:upcalls-dropped-full-buffer/%ju} "
302 	    "{N:/upcall%s dropped due to full socket buffer}\n");
303 	p(mrt6s_cache_cleanups, "\t{:cache-cleanups/%ju} "
304 	    "{N:/cache cleanup%s}\n");
305 	p(mrt6s_no_route, "\t{:dropped-no-origin/%ju} "
306 	    "{N:/datagram%s with no route for origin}\n");
307 	p(mrt6s_bad_tunnel, "\t{:dropped-bad-tunnel/%ju} "
308 	    "{N:/datagram%s arrived with bad tunneling}\n");
309 	p(mrt6s_cant_tunnel, "\t{:dropped-could-not-tunnel/%ju} "
310 	    "{N:/datagram%s could not be tunneled}\n");
311 	p(mrt6s_wrong_if, "\t{:dropped-wrong-incoming-interface/%ju} "
312 	    "{N:/datagram%s arrived on wrong interface}\n");
313 	p(mrt6s_drop_sel, "\t{:dropped-selectively/%ju} "
314 	    "{N:/datagram%s selectively dropped}\n");
315 	p(mrt6s_q_overflow, "\t{:dropped-queue-overflow/%ju} "
316 	    "{N:/datagram%s dropped due to queue overflow}\n");
317 	p(mrt6s_pkt2large, "\t{:dropped-too-large/%ju} "
318 	    "{N:/datagram%s dropped for being too large}\n");
319 
320 #undef	p2
321 #undef	p
322 	xo_close_container("multicast-statistics");
323 }
324 #endif /*INET6*/
325