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