1175061Sobrien/*-
256722Sshin * Copyright (C) 1998 WIDE Project.
356722Sshin * All rights reserved.
456722Sshin *
556722Sshin * Redistribution and use in source and binary forms, with or without
656722Sshin * modification, are permitted provided that the following conditions
756722Sshin * are met:
856722Sshin * 1. Redistributions of source code must retain the above copyright
956722Sshin *    notice, this list of conditions and the following disclaimer.
1056722Sshin * 2. Redistributions in binary form must reproduce the above copyright
1156722Sshin *    notice, this list of conditions and the following disclaimer in the
1256722Sshin *    documentation and/or other materials provided with the distribution.
1356722Sshin * 3. Neither the name of the project nor the names of its contributors
1456722Sshin *    may be used to endorse or promote products derived from this software
1556722Sshin *    without specific prior written permission.
1656722Sshin *
1756722Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1856722Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1956722Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2056722Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2156722Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2256722Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2356722Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2456722Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2556722Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2656722Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2756722Sshin * SUCH DAMAGE.
2856722Sshin */
29175061Sobrien/*-
3056722Sshin * Copyright (c) 1989 Stephen Deering
3156722Sshin * Copyright (c) 1992, 1993
3256722Sshin *	The Regents of the University of California.  All rights reserved.
3356722Sshin *
3456722Sshin * This code is derived from software contributed to Berkeley by
3556722Sshin * Stephen Deering of Stanford University.
3656722Sshin *
3756722Sshin * Redistribution and use in source and binary forms, with or without
3856722Sshin * modification, are permitted provided that the following conditions
3956722Sshin * are met:
4056722Sshin * 1. Redistributions of source code must retain the above copyright
4156722Sshin *    notice, this list of conditions and the following disclaimer.
4256722Sshin * 2. Redistributions in binary form must reproduce the above copyright
4356722Sshin *    notice, this list of conditions and the following disclaimer in the
4456722Sshin *    documentation and/or other materials provided with the distribution.
4556722Sshin * 3. All advertising materials mentioning features or use of this software
4656722Sshin *    must display the following acknowledgement:
4756722Sshin *	This product includes software developed by the University of
4856722Sshin *	California, Berkeley and its contributors.
4956722Sshin * 4. Neither the name of the University nor the names of its contributors
5056722Sshin *    may be used to endorse or promote products derived from this software
5156722Sshin *    without specific prior written permission.
5256722Sshin *
5356722Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5456722Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5556722Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5656722Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5756722Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5856722Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5956722Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6056722Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6156722Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6256722Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6356722Sshin * SUCH DAMAGE.
6456722Sshin *
6556722Sshin *	@(#)mroute.c	8.2 (Berkeley) 4/28/95
6656722Sshin */
6756722Sshin
68132671Scharnier#include <sys/cdefs.h>
69132671Scharnier__FBSDID("$FreeBSD: releng/10.3/usr.bin/netstat/mroute6.c 293307 2016-01-07 07:21:37Z markj $");
70132671Scharnier
7164342Sume#ifdef INET6
7256722Sshin#include <sys/param.h>
7356722Sshin#include <sys/queue.h>
7456722Sshin#include <sys/socket.h>
7556722Sshin#include <sys/socketvar.h>
76166952Sbms#include <sys/sysctl.h>
7756722Sshin#include <sys/protosw.h>
78166952Sbms#include <sys/mbuf.h>
79166952Sbms#include <sys/time.h>
8056722Sshin
8156722Sshin#include <net/if.h>
8256722Sshin#include <net/if_var.h>
8378064Sume#include <net/route.h>
8456722Sshin
8556722Sshin#include <netinet/in.h>
8656722Sshin
87166952Sbms#include <err.h>
88263335Sglebius#include <nlist.h>
89160787Syar#include <stdint.h>
9056722Sshin#include <stdio.h>
91166952Sbms#include <stdlib.h>
9256722Sshin
9356722Sshin#define	KERNEL 1
9456722Sshin#include <netinet6/ip6_mroute.h>
9556722Sshin#undef KERNEL
9656722Sshin
9756722Sshin#include "netstat.h"
9856722Sshin
99263335Sglebius/*
100263335Sglebius * kvm(3) bindings for every needed symbol
101263335Sglebius */
102263335Sglebiusstatic struct nlist mrl[] = {
103263335Sglebius#define	N_MF6CTABLE	0
104263335Sglebius	{ .n_name = "_mf6ctable" },
105263335Sglebius#define	N_MIF6TABLE	1
106263335Sglebius	{ .n_name = "_mif6table" },
107263335Sglebius#define	N_MRT6STAT	2
108263335Sglebius	{ .n_name = "_mrt6stat" },
109263335Sglebius	{ .n_name = NULL },
110263335Sglebius};
111263335Sglebius
112263335Sglebius
11383200Sru#define	WID_ORG	(Wflag ? 39 : (numeric_addr ? 29 : 18)) /* width of origin column */
11483200Sru#define	WID_GRP	(Wflag ? 18 : (numeric_addr ? 16 : 18)) /* width of group column */
11556722Sshin
11656722Sshinvoid
117263335Sglebiusmroute6pr()
11856722Sshin{
11956722Sshin	struct mf6c *mf6ctable[MF6CTBLSIZ], *mfcp;
12056722Sshin	struct mif6 mif6table[MAXMIFS];
12156722Sshin	struct mf6c mfc;
12256722Sshin	struct rtdetq rte, *rtep;
123102975Sdwmalone	struct mif6 *mifp;
124263335Sglebius	u_long mfcaddr, mifaddr;
125102975Sdwmalone	mifi_t mifi;
126102975Sdwmalone	int i;
127102975Sdwmalone	int banner_printed;
128102975Sdwmalone	int saved_numeric_addr;
12956722Sshin	mifi_t maxmif = 0;
13056722Sshin	long int waitings;
131166952Sbms	size_t len;
13256722Sshin
133263335Sglebius	kresolve_list(mrl);
134263335Sglebius	mfcaddr = mrl[N_MF6CTABLE].n_value;
135263335Sglebius	mifaddr = mrl[N_MIF6TABLE].n_value;
136263335Sglebius
137263335Sglebius	if (mfcaddr == 0 || mifaddr == 0) {
138263335Sglebius		fprintf(stderr, "No IPv6 MROUTING kernel support.\n");
139263335Sglebius		return;
140263335Sglebius	}
141263335Sglebius
142166952Sbms	len = sizeof(mif6table);
143171465Sjhb	if (live) {
144171465Sjhb		if (sysctlbyname("net.inet6.ip6.mif6table", mif6table, &len,
145171465Sjhb		    NULL, 0) < 0) {
146171465Sjhb			warn("sysctl: net.inet6.ip6.mif6table");
147166952Sbms			return;
148166952Sbms		}
149171465Sjhb	} else
150166952Sbms		kread(mifaddr, (char *)mif6table, sizeof(mif6table));
15156722Sshin
15278238Sassar	saved_numeric_addr = numeric_addr;
15378238Sassar	numeric_addr = 1;
154166952Sbms	banner_printed = 0;
15556722Sshin
15656722Sshin	for (mifi = 0, mifp = mif6table; mifi < MAXMIFS; ++mifi, ++mifp) {
15756722Sshin		struct ifnet ifnet;
15856722Sshin		char ifname[IFNAMSIZ];
15956722Sshin
16056722Sshin		if (mifp->m6_ifp == NULL)
16156722Sshin			continue;
16256722Sshin
163166952Sbms		/* XXX KVM */
16456722Sshin		kread((u_long)mifp->m6_ifp, (char *)&ifnet, sizeof(ifnet));
165166952Sbms
16656722Sshin		maxmif = mifi;
16756722Sshin		if (!banner_printed) {
16856722Sshin			printf("\nIPv6 Multicast Interface Table\n"
16956722Sshin			       " Mif   Rate   PhyIF   "
17056722Sshin			       "Pkts-In   Pkts-Out\n");
17156722Sshin			banner_printed = 1;
17256722Sshin		}
17356722Sshin
17456722Sshin		printf("  %2u   %4d",
17556722Sshin		       mifi, mifp->m6_rate_limit);
17656722Sshin		printf("   %5s", (mifp->m6_flags & MIFF_REGISTER) ?
17756722Sshin		       "reg0" : if_indextoname(ifnet.if_index, ifname));
17856722Sshin
179160787Syar		printf(" %9ju  %9ju\n", (uintmax_t)mifp->m6_pkt_in,
180160787Syar		    (uintmax_t)mifp->m6_pkt_out);
18156722Sshin	}
18256722Sshin	if (!banner_printed)
18356722Sshin		printf("\nIPv6 Multicast Interface Table is empty\n");
18456722Sshin
185166952Sbms	len = sizeof(mf6ctable);
186171465Sjhb	if (live) {
187171465Sjhb		if (sysctlbyname("net.inet6.ip6.mf6ctable", mf6ctable, &len,
188171465Sjhb		    NULL, 0) < 0) {
189171465Sjhb			warn("sysctl: net.inet6.ip6.mf6ctable");
190166952Sbms			return;
191166952Sbms		}
192171465Sjhb	} else
193166952Sbms		kread(mfcaddr, (char *)mf6ctable, sizeof(mf6ctable));
194166952Sbms
19556722Sshin	banner_printed = 0;
196166952Sbms
19756722Sshin	for (i = 0; i < MF6CTBLSIZ; ++i) {
19856722Sshin		mfcp = mf6ctable[i];
19956722Sshin		while(mfcp) {
20056722Sshin			kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
20156722Sshin			if (!banner_printed) {
20256722Sshin				printf ("\nIPv6 Multicast Forwarding Cache\n");
20356722Sshin				printf(" %-*.*s %-*.*s %s",
20456722Sshin				       WID_ORG, WID_ORG, "Origin",
20556722Sshin				       WID_GRP, WID_GRP, "Group",
20656722Sshin				       "  Packets Waits In-Mif  Out-Mifs\n");
20756722Sshin				banner_printed = 1;
20856722Sshin			}
20956722Sshin
21056722Sshin			printf(" %-*.*s", WID_ORG, WID_ORG,
21156722Sshin			       routename6(&mfc.mf6c_origin));
21256722Sshin			printf(" %-*.*s", WID_GRP, WID_GRP,
21356722Sshin			       routename6(&mfc.mf6c_mcastgrp));
214160787Syar			printf(" %9ju", (uintmax_t)mfc.mf6c_pkt_cnt);
21556722Sshin
21656722Sshin			for (waitings = 0, rtep = mfc.mf6c_stall; rtep; ) {
21756722Sshin				waitings++;
218166952Sbms				/* XXX KVM */
21956722Sshin				kread((u_long)rtep, (char *)&rte, sizeof(rte));
22056722Sshin				rtep = rte.next;
22156722Sshin			}
22256722Sshin			printf("   %3ld", waitings);
22356722Sshin
22456722Sshin			if (mfc.mf6c_parent == MF6C_INCOMPLETE_PARENT)
22556722Sshin				printf(" ---   ");
22656722Sshin			else
22756722Sshin				printf("  %3d   ", mfc.mf6c_parent);
22856722Sshin			for (mifi = 0; mifi <= maxmif; mifi++) {
22956722Sshin				if (IF_ISSET(mifi, &mfc.mf6c_ifset))
23056722Sshin					printf(" %u", mifi);
23156722Sshin			}
23256722Sshin			printf("\n");
23356722Sshin
23456722Sshin			mfcp = mfc.mf6c_next;
23556722Sshin		}
23656722Sshin	}
23756722Sshin	if (!banner_printed)
238166952Sbms		printf("\nIPv6 Multicast Forwarding Table is empty\n");
23956722Sshin
24056722Sshin	printf("\n");
24178238Sassar	numeric_addr = saved_numeric_addr;
24256722Sshin}
24356722Sshin
24456722Sshinvoid
245263335Sglebiusmrt6_stats()
24656722Sshin{
24756722Sshin	struct mrt6stat mrtstat;
248263335Sglebius	u_long mstaddr;
24956722Sshin
250263335Sglebius	kresolve_list(mrl);
251263335Sglebius	mstaddr = mrl[N_MRT6STAT].n_value;
252263335Sglebius
253263335Sglebius	if (mstaddr == 0) {
254263335Sglebius		fprintf(stderr, "No IPv6 MROUTING kernel support.\n");
255263335Sglebius		return;
256263335Sglebius	}
257293307Smarkj	if (fetch_stats("net.inet6.ip6.mrt6stat", 0, &mrtstat,
258293307Smarkj	    sizeof(mrtstat), kread_counters) != 0)
259293307Smarkj		return;
260263335Sglebius
26156722Sshin	printf("IPv6 multicast forwarding:\n");
26283204Sru
26383204Sru#define	p(f, m) if (mrtstat.f || sflag <= 1) \
264160787Syar	printf(m, (uintmax_t)mrtstat.f, plural(mrtstat.f))
26583204Sru#define	p2(f, m) if (mrtstat.f || sflag <= 1) \
266160787Syar	printf(m, (uintmax_t)mrtstat.f, plurales(mrtstat.f))
26783204Sru
268160787Syar	p(mrt6s_mfc_lookups, "\t%ju multicast forwarding cache lookup%s\n");
269160787Syar	p2(mrt6s_mfc_misses, "\t%ju multicast forwarding cache miss%s\n");
270162797Sbms	p(mrt6s_upcalls, "\t%ju upcall%s to multicast routing daemon\n");
271160787Syar	p(mrt6s_upq_ovflw, "\t%ju upcall queue overflow%s\n");
27283204Sru	p(mrt6s_upq_sockfull,
273160787Syar	    "\t%ju upcall%s dropped due to full socket buffer\n");
274160787Syar	p(mrt6s_cache_cleanups, "\t%ju cache cleanup%s\n");
275160787Syar	p(mrt6s_no_route, "\t%ju datagram%s with no route for origin\n");
276160787Syar	p(mrt6s_bad_tunnel, "\t%ju datagram%s arrived with bad tunneling\n");
277160787Syar	p(mrt6s_cant_tunnel, "\t%ju datagram%s could not be tunneled\n");
278160787Syar	p(mrt6s_wrong_if, "\t%ju datagram%s arrived on wrong interface\n");
279160787Syar	p(mrt6s_drop_sel, "\t%ju datagram%s selectively dropped\n");
28083204Sru	p(mrt6s_q_overflow,
281160787Syar	    "\t%ju datagram%s dropped due to queue overflow\n");
282160787Syar	p(mrt6s_pkt2large, "\t%ju datagram%s dropped for being too large\n");
28383204Sru
28483204Sru#undef	p2
28583204Sru#undef	p
28656722Sshin}
28764342Sume#endif /*INET6*/
288