1 /*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 1989 Stephen Deering
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Stephen Deering of Stanford University.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 /*
42 * Print multicast routing structures and statistics.
43 *
44 * MROUTING 1.0
45 */
46
47 #include <sys/param.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/protosw.h>
53 #include <sys/mbuf.h>
54 #include <sys/time.h>
55
56 #include <net/if.h>
57 #include <netinet/in.h>
58 #include <netinet/igmp.h>
59 #include <net/route.h>
60
61 #define _NETSTAT 1
62 #include <netinet/ip_mroute.h>
63 #undef _NETSTAT_
64
65 #include <stdint.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <stdbool.h>
69 #include <string.h>
70 #include <libxo/xo.h>
71 #include "netstat.h"
72 #include "nl_defs.h"
73
74 static void print_bw_meter(struct bw_meter *, int *);
75 static void print_mfc(struct mfc *, int, int *);
76
77 static void
print_bw_meter(struct bw_meter * bw_meter,int * banner_printed)78 print_bw_meter(struct bw_meter *bw_meter, int *banner_printed)
79 {
80 char s1[256], s2[256], s3[256];
81 struct timeval now, end, delta;
82
83 gettimeofday(&now, NULL);
84
85 if (! *banner_printed) {
86 xo_open_list("bandwidth-meter");
87 xo_emit(" {T:Bandwidth Meters}\n");
88 xo_emit(" {T:/%-30s}", "Measured(Start|Packets|Bytes)");
89 xo_emit(" {T:/%s}", "Type");
90 xo_emit(" {T:/%-30s}", "Thresh(Interval|Packets|Bytes)");
91 xo_emit(" {T:Remain}");
92 xo_emit("\n");
93 *banner_printed = 1;
94 }
95
96 xo_open_instance("bandwidth-meter");
97
98 /* The measured values */
99 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) {
100 snprintf(s1, sizeof(s1), "%ju",
101 (uintmax_t)bw_meter->bm_measured.b_packets);
102 xo_emit("{e:measured-packets/%ju}",
103 (uintmax_t)bw_meter->bm_measured.b_packets);
104 } else
105 strcpy(s1, "?");
106 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) {
107 snprintf(s2, sizeof(s2), "%ju",
108 (uintmax_t)bw_meter->bm_measured.b_bytes);
109 xo_emit("{e:measured-bytes/%ju}",
110 (uintmax_t)bw_meter->bm_measured.b_bytes);
111 } else
112 strcpy(s2, "?");
113 xo_emit(" {[:-30}{:start-time/%lu.%06lu}|{q:measured-packets/%s}"
114 "|{q:measured-bytes%s}{]:}",
115 (u_long)bw_meter->bm_start_time.tv_sec,
116 (u_long)bw_meter->bm_start_time.tv_usec, s1, s2);
117
118 /* The type of entry */
119 xo_emit(" {t:type/%-3s}", (bw_meter->bm_flags & BW_METER_GEQ) ? ">=" :
120 (bw_meter->bm_flags & BW_METER_LEQ) ? "<=" : "?");
121
122 /* The threshold values */
123 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) {
124 snprintf(s1, sizeof(s1), "%ju",
125 (uintmax_t)bw_meter->bm_threshold.b_packets);
126 xo_emit("{e:threshold-packets/%ju}",
127 (uintmax_t)bw_meter->bm_threshold.b_packets);
128 } else
129 strcpy(s1, "?");
130 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) {
131 snprintf(s2, sizeof(s2), "%ju",
132 (uintmax_t)bw_meter->bm_threshold.b_bytes);
133 xo_emit("{e:threshold-bytes/%ju}",
134 (uintmax_t)bw_meter->bm_threshold.b_bytes);
135 } else
136 strcpy(s2, "?");
137
138 xo_emit(" {[:-30}{:threshold-time/%lu.%06lu}|{q:threshold-packets/%s}"
139 "|{q:threshold-bytes%s}{]:}",
140 (u_long)bw_meter->bm_threshold.b_time.tv_sec,
141 (u_long)bw_meter->bm_threshold.b_time.tv_usec, s1, s2);
142
143 /* Remaining time */
144 timeradd(&bw_meter->bm_start_time,
145 &bw_meter->bm_threshold.b_time, &end);
146 if (timercmp(&now, &end, <=)) {
147 timersub(&end, &now, &delta);
148 snprintf(s3, sizeof(s3), "%lu.%06lu",
149 (u_long)delta.tv_sec,
150 (u_long)delta.tv_usec);
151 } else {
152 /* Negative time */
153 timersub(&now, &end, &delta);
154 snprintf(s3, sizeof(s3), "-%lu.06%lu",
155 (u_long)delta.tv_sec,
156 (u_long)delta.tv_usec);
157 }
158 xo_emit(" {:remaining-time/%s}", s3);
159
160 xo_open_instance("bandwidth-meter");
161
162 xo_emit("\n");
163 }
164
165 static void
print_mfc(struct mfc * m,int maxvif,int * banner_printed)166 print_mfc(struct mfc *m, int maxvif, int *banner_printed)
167 {
168 struct sockaddr_in sin;
169 struct sockaddr *sa = (struct sockaddr *)&sin;
170 struct bw_meter bw_meter, *bwm;
171 int bw_banner_printed;
172 int error;
173 vifi_t vifi;
174
175 bw_banner_printed = 0;
176 memset(&sin, 0, sizeof(sin));
177 sin.sin_len = sizeof(sin);
178 sin.sin_family = AF_INET;
179
180 if (! *banner_printed) {
181 xo_open_list("multicast-forwarding-entry");
182 xo_emit("\n{T:IPv4 Multicast Forwarding Table}\n"
183 " {T:Origin} {T:Group} "
184 " {T:Packets In-Vif} {T:Out-Vifs:Ttls}\n");
185 *banner_printed = 1;
186 }
187
188 memcpy(&sin.sin_addr, &m->mfc_origin, sizeof(sin.sin_addr));
189 xo_emit(" {:origin-address/%-15.15s}", routename(sa, numeric_addr));
190 memcpy(&sin.sin_addr, &m->mfc_mcastgrp, sizeof(sin.sin_addr));
191 xo_emit(" {:group-address/%-15.15s}",
192 routename(sa, numeric_addr));
193 xo_emit(" {:sent-packets/%9lu}", m->mfc_pkt_cnt);
194 xo_emit(" {:parent/%3d} ", m->mfc_parent);
195 xo_open_list("vif-ttl");
196 for (vifi = 0; vifi <= maxvif; vifi++) {
197 if (m->mfc_ttls[vifi] > 0) {
198 xo_open_instance("vif-ttl");
199 xo_emit(" {k:vif/%u}:{:ttl/%u}", vifi,
200 m->mfc_ttls[vifi]);
201 xo_close_instance("vif-ttl");
202 }
203 }
204 xo_close_list("vif-ttl");
205 xo_emit("\n");
206
207 /*
208 * XXX We break the rules and try to use KVM to read the
209 * bandwidth meters, they are not retrievable via sysctl yet.
210 */
211 bwm = m->mfc_bw_meter_leq;
212 while (bwm != NULL) {
213 error = kread((u_long)bwm, (char *)&bw_meter,
214 sizeof(bw_meter));
215 if (error)
216 break;
217 print_bw_meter(&bw_meter, &bw_banner_printed);
218 bwm = bw_meter.bm_mfc_next;
219 }
220 bwm = m->mfc_bw_meter_geq;
221 while (bwm != NULL) {
222 error = kread((u_long)bwm, (char *)&bw_meter,
223 sizeof(bw_meter));
224 if (error)
225 break;
226 print_bw_meter(&bw_meter, &bw_banner_printed);
227 bwm = bw_meter.bm_mfc_next;
228 }
229 if (banner_printed)
230 xo_close_list("bandwidth-meter");
231 }
232
233 void
mroutepr(void)234 mroutepr(void)
235 {
236 struct sockaddr_in sin;
237 struct sockaddr *sa = (struct sockaddr *)&sin;
238 struct vif viftable[MAXVIFS];
239 struct vif *v;
240 struct mfc *m;
241 u_long pmfchashtbl, pmfctablesize, pviftbl;
242 int banner_printed;
243 int saved_numeric_addr;
244 size_t len;
245 vifi_t vifi, maxvif;
246
247 saved_numeric_addr = numeric_addr;
248 numeric_addr = 1;
249
250 memset(&sin, 0, sizeof(sin));
251 sin.sin_len = sizeof(sin);
252 sin.sin_family = AF_INET;
253
254 /*
255 * TODO:
256 * The VIF table will move to hanging off the struct if_info for
257 * each IPv4 configured interface. Currently it is statically
258 * allocated, and retrieved either using KVM or an opaque SYSCTL.
259 *
260 * This can't happen until the API documented in multicast(4)
261 * is itself refactored. The historical reason why VIFs use
262 * a separate ifindex space is entirely due to the legacy
263 * capability of the MROUTING code to create IPIP tunnels on
264 * the fly to support DVMRP. When gif(4) became available, this
265 * functionality was deprecated, as PIM does not use it.
266 */
267 maxvif = 0;
268 pmfchashtbl = pmfctablesize = pviftbl = 0;
269
270 len = sizeof(viftable);
271 if (live) {
272 if (sysctlbyname("net.inet.ip.viftable", viftable, &len, NULL,
273 0) < 0) {
274 xo_warn("sysctl: net.inet.ip.viftable");
275 return;
276 }
277 } else {
278 pmfchashtbl = nl[N_MFCHASHTBL].n_value;
279 pmfctablesize = nl[N_MFCTABLESIZE].n_value;
280 pviftbl = nl[N_VIFTABLE].n_value;
281
282 if (pmfchashtbl == 0 || pmfctablesize == 0 || pviftbl == 0) {
283 xo_warnx("No IPv4 MROUTING kernel support.");
284 return;
285 }
286
287 kread(pviftbl, (char *)viftable, sizeof(viftable));
288 }
289
290 banner_printed = 0;
291 for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
292 if (v->v_lcl_addr.s_addr == 0)
293 continue;
294
295 maxvif = vifi;
296 if (!banner_printed) {
297 xo_emit("\n{T:IPv4 Virtual Interface Table\n"
298 " Vif Thresh Local-Address "
299 "Remote-Address Pkts-In Pkts-Out}\n");
300 banner_printed = 1;
301 xo_open_list("vif");
302 }
303
304 xo_open_instance("vif");
305 memcpy(&sin.sin_addr, &v->v_lcl_addr, sizeof(sin.sin_addr));
306 xo_emit(" {:vif/%2u} {:threshold/%6u} {:route/%-15.15s}",
307 /* opposite math of add_vif() */
308 vifi, v->v_threshold,
309 routename(sa, numeric_addr));
310 memcpy(&sin.sin_addr, &v->v_rmt_addr, sizeof(sin.sin_addr));
311 xo_emit(" {:source/%-15.15s}", (v->v_flags & VIFF_TUNNEL) ?
312 routename(sa, numeric_addr) : "");
313
314 xo_emit(" {:received-packets/%9lu} {:sent-packets/%9lu}\n",
315 v->v_pkt_in, v->v_pkt_out);
316 xo_close_instance("vif");
317 }
318 if (banner_printed)
319 xo_close_list("vif");
320 else
321 xo_emit("\n{T:IPv4 Virtual Interface Table is empty}\n");
322
323 banner_printed = 0;
324
325 /*
326 * TODO:
327 * The MFC table will move into the AF_INET radix trie in future.
328 * In 8.x, it becomes a dynamically allocated structure referenced
329 * by a hashed LIST, allowing more than 256 entries w/o kernel tuning.
330 *
331 * If retrieved via opaque SYSCTL, the kernel will coalesce it into
332 * a static table for us.
333 * If retrieved via KVM, the hash list pointers must be followed.
334 */
335 if (live) {
336 struct mfc *mfctable;
337
338 len = 0;
339 if (sysctlbyname("net.inet.ip.mfctable", NULL, &len, NULL,
340 0) < 0) {
341 xo_warn("sysctl: net.inet.ip.mfctable");
342 return;
343 }
344
345 mfctable = malloc(len);
346 if (mfctable == NULL) {
347 xo_warnx("malloc %lu bytes", (u_long)len);
348 return;
349 }
350 if (sysctlbyname("net.inet.ip.mfctable", mfctable, &len, NULL,
351 0) < 0) {
352 free(mfctable);
353 xo_warn("sysctl: net.inet.ip.mfctable");
354 return;
355 }
356
357 m = mfctable;
358 while (len >= sizeof(*m)) {
359 print_mfc(m++, maxvif, &banner_printed);
360 len -= sizeof(*m);
361 }
362 if (banner_printed)
363 xo_close_list("multicast-forwarding-entry");
364 if (len != 0)
365 xo_warnx("print_mfc: %lu trailing bytes", (u_long)len);
366
367 free(mfctable);
368 } else {
369 LIST_HEAD(, mfc) *mfchashtbl;
370 u_long i, mfctablesize;
371 struct mfc mfc;
372 int error;
373
374 error = kread(pmfctablesize, (char *)&mfctablesize,
375 sizeof(u_long));
376 if (error) {
377 xo_warn("kread: mfctablesize");
378 return;
379 }
380
381 len = sizeof(*mfchashtbl) * mfctablesize;
382 mfchashtbl = malloc(len);
383 if (mfchashtbl == NULL) {
384 xo_warnx("malloc %lu bytes", (u_long)len);
385 return;
386 }
387 kread(pmfchashtbl, (char *)&mfchashtbl, len);
388
389 for (i = 0; i < mfctablesize; i++) {
390 LIST_FOREACH(m, &mfchashtbl[i], mfc_hash) {
391 kread((u_long)m, (char *)&mfc, sizeof(mfc));
392 print_mfc(m, maxvif, &banner_printed);
393 }
394 }
395 if (banner_printed)
396 xo_close_list("multicast-forwarding-entry");
397
398 free(mfchashtbl);
399 }
400
401 if (!banner_printed)
402 xo_emit("\n{T:IPv4 Multicast Forwarding Table is empty}\n");
403
404 xo_emit("\n");
405 numeric_addr = saved_numeric_addr;
406 }
407
408 void
mrt_stats(void)409 mrt_stats(void)
410 {
411 struct mrtstat mrtstat;
412 u_long mstaddr;
413
414 mstaddr = nl[N_MRTSTAT].n_value;
415
416 if (fetch_stats("net.inet.ip.mrtstat", mstaddr, &mrtstat,
417 sizeof(mrtstat), kread_counters) != 0) {
418 if ((live && errno == ENOENT) || (!live && mstaddr == 0))
419 fprintf(stderr, "No IPv4 MROUTING kernel support.\n");
420 return;
421 }
422
423 xo_emit("{T:IPv4 multicast forwarding}:\n");
424
425 #define p(f, m) if (mrtstat.f || sflag <= 1) \
426 xo_emit(m, (uintmax_t)mrtstat.f, plural(mrtstat.f))
427 #define p2(f, m) if (mrtstat.f || sflag <= 1) \
428 xo_emit(m, (uintmax_t)mrtstat.f, plurales(mrtstat.f))
429
430 xo_open_container("multicast-statistics");
431
432 p(mrts_mfc_lookups, "\t{:cache-lookups/%ju} "
433 "{N:/multicast forwarding cache lookup%s}\n");
434 p2(mrts_mfc_misses, "\t{:cache-misses/%ju} "
435 "{N:/multicast forwarding cache miss%s}\n");
436 p(mrts_upcalls, "\t{:upcalls-total/%ju} "
437 "{N:/upcall%s to multicast routing daemon}\n");
438 p(mrts_upq_ovflw, "\t{:upcall-overflows/%ju} "
439 "{N:/upcall queue overflow%s}\n");
440 p(mrts_upq_sockfull,
441 "\t{:upcalls-dropped-full-buffer/%ju} "
442 "{N:/upcall%s dropped due to full socket buffer}\n");
443 p(mrts_cache_cleanups, "\t{:cache-cleanups/%ju} "
444 "{N:/cache cleanup%s}\n");
445 p(mrts_no_route, "\t{:dropped-no-origin/%ju} "
446 "{N:/datagram%s with no route for origin}\n");
447 p(mrts_bad_tunnel, "\t{:dropped-bad-tunnel/%ju} "
448 "{N:/datagram%s arrived with bad tunneling}\n");
449 p(mrts_cant_tunnel, "\t{:dropped-could-not-tunnel/%ju} "
450 "{N:/datagram%s could not be tunneled}\n");
451 p(mrts_wrong_if, "\t{:dropped-wrong-incoming-interface/%ju} "
452 "{N:/datagram%s arrived on wrong interface}\n");
453 p(mrts_drop_sel, "\t{:dropped-selectively/%ju} "
454 "{N:/datagram%s selectively dropped}\n");
455 p(mrts_q_overflow, "\t{:dropped-queue-overflow/%ju} "
456 "{N:/datagram%s dropped due to queue overflow}\n");
457 p(mrts_pkt2large, "\t{:dropped-too-large/%ju} "
458 "{N:/datagram%s dropped for being too large}\n");
459
460 #undef p2
461 #undef p
462 }
463