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