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