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 #ifdef INET6
69 #include <sys/param.h>
70 #include <sys/queue.h>
71 #include <sys/socket.h>
72 #include <sys/sysctl.h>
73 #include <sys/mbuf.h>
74 #include <sys/time.h>
75
76 #include <net/if.h>
77 #include <net/route.h>
78
79 #include <netinet/in.h>
80
81 #include <stdint.h>
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <stdbool.h>
85 #include <libxo/xo.h>
86
87 #define KERNEL 1
88 struct sockopt;
89 #include <netinet6/ip6_mroute.h>
90 #undef KERNEL
91
92 #include "netstat.h"
93
94 #define WID_ORG (Wflag ? 39 : (numeric_addr ? 29 : 18)) /* width of origin column */
95 #define WID_GRP (Wflag ? 18 : (numeric_addr ? 16 : 18)) /* width of group column */
96
97 void
mroute6pr(void)98 mroute6pr(void)
99 {
100 struct mf6c *mf6ctable[MF6CTBLSIZ], *mfcp;
101 struct mif6_sctl mif6table[MAXMIFS];
102 struct mf6c mfc;
103 struct rtdetq rte, *rtep;
104 struct mif6_sctl *mifp;
105 mifi_t mifi;
106 int i;
107 int banner_printed;
108 int saved_numeric_addr;
109 mifi_t maxmif = 0;
110 long int waitings;
111 size_t len;
112
113 if (live == 0)
114 return;
115
116 len = sizeof(mif6table);
117 if (sysctlbyname("net.inet6.ip6.mif6table", mif6table, &len, NULL, 0) <
118 0) {
119 xo_warn("sysctl: net.inet6.ip6.mif6table");
120 return;
121 }
122
123 saved_numeric_addr = numeric_addr;
124 numeric_addr = 1;
125 banner_printed = 0;
126
127 for (mifi = 0, mifp = mif6table; mifi < MAXMIFS; ++mifi, ++mifp) {
128 char ifname[IFNAMSIZ];
129
130 if (mifp->m6_ifp == 0)
131 continue;
132
133 maxmif = mifi;
134 if (!banner_printed) {
135 xo_open_list("multicast-interface");
136 xo_emit("\n{T:IPv6 Multicast Interface Table}\n"
137 "{T: Mif Rate PhyIF Pkts-In Pkts-Out}\n");
138 banner_printed = 1;
139 }
140
141 xo_open_instance("multicast-interface");
142 xo_emit(" {:mif/%2u} {:rate-limit/%4d}",
143 mifi, mifp->m6_rate_limit);
144 xo_emit(" {:ifname/%5s}", (mifp->m6_flags & MIFF_REGISTER) ?
145 "reg0" : if_indextoname(mifp->m6_ifp, ifname));
146
147 xo_emit(" {:received-packets/%9ju} {:sent-packets/%9ju}\n",
148 (uintmax_t)mifp->m6_pkt_in,
149 (uintmax_t)mifp->m6_pkt_out);
150 xo_close_instance("multicast-interface");
151 }
152 if (banner_printed)
153 xo_close_list("multicast-interface");
154 else
155 xo_emit("\n{T:IPv6 Multicast Interface Table is empty}\n");
156
157 len = sizeof(mf6ctable);
158 if (sysctlbyname("net.inet6.ip6.mf6ctable", mf6ctable, &len, NULL, 0) <
159 0) {
160 xo_warn("sysctl: net.inet6.ip6.mf6ctable");
161 return;
162 }
163
164 banner_printed = 0;
165
166 for (i = 0; i < MF6CTBLSIZ; ++i) {
167 mfcp = mf6ctable[i];
168 while(mfcp) {
169 kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
170 if (!banner_printed) {
171 xo_open_list("multicast-forwarding-cache");
172 xo_emit("\n"
173 "{T:IPv6 Multicast Forwarding Cache}\n");
174 xo_emit(" {T:/%-*.*s} {T:/%-*.*s} {T:/%s}\n",
175 WID_ORG, WID_ORG, "Origin",
176 WID_GRP, WID_GRP, "Group",
177 " Packets Waits In-Mif Out-Mifs");
178 banner_printed = 1;
179 }
180
181 xo_open_instance("multicast-forwarding-cache");
182
183 xo_emit(" {t:origin/%-*.*s}", WID_ORG, WID_ORG,
184 routename(sin6tosa(&mfc.mf6c_origin),
185 numeric_addr));
186 xo_emit(" {t:group/%-*.*s}", WID_GRP, WID_GRP,
187 routename(sin6tosa(&mfc.mf6c_mcastgrp),
188 numeric_addr));
189 xo_emit(" {:total-packets/%9ju}",
190 (uintmax_t)mfc.mf6c_pkt_cnt);
191
192 for (waitings = 0, rtep = mfc.mf6c_stall; rtep; ) {
193 waitings++;
194 /* XXX KVM */
195 kread((u_long)rtep, (char *)&rte, sizeof(rte));
196 rtep = rte.next;
197 }
198 xo_emit(" {:waitings/%3ld}", waitings);
199
200 if (mfc.mf6c_parent == MF6C_INCOMPLETE_PARENT)
201 xo_emit(" --- ");
202 else
203 xo_emit(" {:parent/%3d} ", mfc.mf6c_parent);
204 xo_open_list("mif");
205 for (mifi = 0; mifi <= maxmif; mifi++) {
206 if (IF_ISSET(mifi, &mfc.mf6c_ifset))
207 xo_emit(" {l:/%u}", mifi);
208 }
209 xo_close_list("mif");
210 xo_emit("\n");
211
212 mfcp = mfc.mf6c_next;
213 xo_close_instance("multicast-forwarding-cache");
214 }
215 }
216 if (banner_printed)
217 xo_close_list("multicast-forwarding-cache");
218 else
219 xo_emit("\n{T:IPv6 Multicast Forwarding Table is empty}\n");
220
221 xo_emit("\n");
222 numeric_addr = saved_numeric_addr;
223 }
224
225 void
mrt6_stats(void)226 mrt6_stats(void)
227 {
228 struct mrt6stat mrtstat;
229
230 if (fetch_stats("net.inet6.ip6.mrt6stat", 0, &mrtstat,
231 sizeof(mrtstat), kread_counters) != 0)
232 return;
233
234 xo_open_container("multicast-statistics");
235 xo_emit("{T:IPv6 multicast forwarding}:\n");
236
237 #define p(f, m) if (mrtstat.f || sflag <= 1) \
238 xo_emit(m, (uintmax_t)mrtstat.f, plural(mrtstat.f))
239 #define p2(f, m) if (mrtstat.f || sflag <= 1) \
240 xo_emit(m, (uintmax_t)mrtstat.f, plurales(mrtstat.f))
241
242 p(mrt6s_mfc_lookups, "\t{:cache-lookups/%ju} "
243 "{N:/multicast forwarding cache lookup%s}\n");
244 p2(mrt6s_mfc_misses, "\t{:cache-misses/%ju} "
245 "{N:/multicast forwarding cache miss%s}\n");
246 p(mrt6s_upcalls, "\t{:upcalls/%ju} "
247 "{N:/upcall%s to multicast routing daemon}\n");
248 p(mrt6s_upq_ovflw, "\t{:upcall-overflows/%ju} "
249 "{N:/upcall queue overflow%s}\n");
250 p(mrt6s_upq_sockfull, "\t{:upcalls-dropped-full-buffer/%ju} "
251 "{N:/upcall%s dropped due to full socket buffer}\n");
252 p(mrt6s_cache_cleanups, "\t{:cache-cleanups/%ju} "
253 "{N:/cache cleanup%s}\n");
254 p(mrt6s_no_route, "\t{:dropped-no-origin/%ju} "
255 "{N:/datagram%s with no route for origin}\n");
256 p(mrt6s_bad_tunnel, "\t{:dropped-bad-tunnel/%ju} "
257 "{N:/datagram%s arrived with bad tunneling}\n");
258 p(mrt6s_cant_tunnel, "\t{:dropped-could-not-tunnel/%ju} "
259 "{N:/datagram%s could not be tunneled}\n");
260 p(mrt6s_wrong_if, "\t{:dropped-wrong-incoming-interface/%ju} "
261 "{N:/datagram%s arrived on wrong interface}\n");
262 p(mrt6s_drop_sel, "\t{:dropped-selectively/%ju} "
263 "{N:/datagram%s selectively dropped}\n");
264 p(mrt6s_q_overflow, "\t{:dropped-queue-overflow/%ju} "
265 "{N:/datagram%s dropped due to queue overflow}\n");
266 p(mrt6s_pkt2large, "\t{:dropped-too-large/%ju} "
267 "{N:/datagram%s dropped for being too large}\n");
268
269 #undef p2
270 #undef p
271 xo_close_container("multicast-statistics");
272 }
273 #endif /*INET6*/
274