xref: /freebsd/usr.bin/netstat/mroute6.c (revision 2546665afcaf0d53dc2c7058fee96354b3680f5a)
1 /*
2  * Copyright (C) 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * Copyright (c) 1989 Stephen Deering
32  * Copyright (c) 1992, 1993
33  *	The Regents of the University of California.  All rights reserved.
34  *
35  * This code is derived from software contributed to Berkeley by
36  * Stephen Deering of Stanford University.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)mroute.c	8.2 (Berkeley) 4/28/95
67  */
68 
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
71 
72 #ifdef INET6
73 #include <sys/param.h>
74 #include <sys/queue.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/protosw.h>
78 
79 #include <net/if.h>
80 #include <net/if_var.h>
81 #include <net/route.h>
82 
83 #include <netinet/in.h>
84 
85 #include <stdio.h>
86 
87 #define	KERNEL 1
88 #include <netinet6/ip6_mroute.h>
89 #undef KERNEL
90 
91 #include "netstat.h"
92 
93 #define	WID_ORG	(Wflag ? 39 : (numeric_addr ? 29 : 18)) /* width of origin column */
94 #define	WID_GRP	(Wflag ? 18 : (numeric_addr ? 16 : 18)) /* width of group column */
95 
96 void
97 mroute6pr(u_long mfcaddr, u_long mifaddr)
98 {
99 	struct mf6c *mf6ctable[MF6CTBLSIZ], *mfcp;
100 	struct mif6 mif6table[MAXMIFS];
101 	struct mf6c mfc;
102 	struct rtdetq rte, *rtep;
103 	struct mif6 *mifp;
104 	mifi_t mifi;
105 	int i;
106 	int banner_printed;
107 	int saved_numeric_addr;
108 	mifi_t maxmif = 0;
109 	long int waitings;
110 
111 	if (mfcaddr == 0 || mifaddr == 0) {
112 		printf("No IPv6 multicast routing compiled into this"
113 		       " system.\n");
114 		return;
115 	}
116 
117 	saved_numeric_addr = numeric_addr;
118 	numeric_addr = 1;
119 
120 	kread(mifaddr, (char *)&mif6table, sizeof(mif6table));
121 	banner_printed = 0;
122 	for (mifi = 0, mifp = mif6table; mifi < MAXMIFS; ++mifi, ++mifp) {
123 		struct ifnet ifnet;
124 		char ifname[IFNAMSIZ];
125 
126 		if (mifp->m6_ifp == NULL)
127 			continue;
128 
129 		kread((u_long)mifp->m6_ifp, (char *)&ifnet, sizeof(ifnet));
130 		maxmif = mifi;
131 		if (!banner_printed) {
132 			printf("\nIPv6 Multicast Interface Table\n"
133 			       " Mif   Rate   PhyIF   "
134 			       "Pkts-In   Pkts-Out\n");
135 			banner_printed = 1;
136 		}
137 
138 		printf("  %2u   %4d",
139 		       mifi, mifp->m6_rate_limit);
140 		printf("   %5s", (mifp->m6_flags & MIFF_REGISTER) ?
141 		       "reg0" : if_indextoname(ifnet.if_index, ifname));
142 
143 		printf(" %9llu  %9llu\n", (unsigned long long)mifp->m6_pkt_in,
144 		    (unsigned long long)mifp->m6_pkt_out);
145 	}
146 	if (!banner_printed)
147 		printf("\nIPv6 Multicast Interface Table is empty\n");
148 
149 	kread(mfcaddr, (char *)&mf6ctable, sizeof(mf6ctable));
150 	banner_printed = 0;
151 	for (i = 0; i < MF6CTBLSIZ; ++i) {
152 		mfcp = mf6ctable[i];
153 		while(mfcp) {
154 			kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
155 			if (!banner_printed) {
156 				printf ("\nIPv6 Multicast Forwarding Cache\n");
157 				printf(" %-*.*s %-*.*s %s",
158 				       WID_ORG, WID_ORG, "Origin",
159 				       WID_GRP, WID_GRP, "Group",
160 				       "  Packets Waits In-Mif  Out-Mifs\n");
161 				banner_printed = 1;
162 			}
163 
164 			printf(" %-*.*s", WID_ORG, WID_ORG,
165 			       routename6(&mfc.mf6c_origin));
166 			printf(" %-*.*s", WID_GRP, WID_GRP,
167 			       routename6(&mfc.mf6c_mcastgrp));
168 			printf(" %9llu", (unsigned long long)mfc.mf6c_pkt_cnt);
169 
170 			for (waitings = 0, rtep = mfc.mf6c_stall; rtep; ) {
171 				waitings++;
172 				kread((u_long)rtep, (char *)&rte, sizeof(rte));
173 				rtep = rte.next;
174 			}
175 			printf("   %3ld", waitings);
176 
177 			if (mfc.mf6c_parent == MF6C_INCOMPLETE_PARENT)
178 				printf(" ---   ");
179 			else
180 				printf("  %3d   ", mfc.mf6c_parent);
181 			for (mifi = 0; mifi <= maxmif; mifi++) {
182 				if (IF_ISSET(mifi, &mfc.mf6c_ifset))
183 					printf(" %u", mifi);
184 			}
185 			printf("\n");
186 
187 			mfcp = mfc.mf6c_next;
188 		}
189 	}
190 	if (!banner_printed)
191 		printf("\nIPv6 Multicast Routing Table is empty\n");
192 
193 	printf("\n");
194 	numeric_addr = saved_numeric_addr;
195 }
196 
197 void
198 mrt6_stats(u_long mstaddr)
199 {
200 	struct mrt6stat mrtstat;
201 
202 	if (mstaddr == 0) {
203 		printf("No IPv6 multicast routing compiled into this"
204 		       " system.\n");
205 		return;
206 	}
207 
208 	kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
209 	printf("IPv6 multicast forwarding:\n");
210 
211 #define	p(f, m) if (mrtstat.f || sflag <= 1) \
212 	printf(m, (unsigned long long)mrtstat.f, plural(mrtstat.f))
213 #define	p2(f, m) if (mrtstat.f || sflag <= 1) \
214 	printf(m, (unsigned long long)mrtstat.f, plurales(mrtstat.f))
215 
216 	p(mrt6s_mfc_lookups, "\t%llu multicast forwarding cache lookup%s\n");
217 	p2(mrt6s_mfc_misses, "\t%llu multicast forwarding cache miss%s\n");
218 	p(mrt6s_upcalls, "\t%llu upcall%s to mrouted\n");
219 	p(mrt6s_upq_ovflw, "\t%llu upcall queue overflow%s\n");
220 	p(mrt6s_upq_sockfull,
221 	    "\t%llu upcall%s dropped due to full socket buffer\n");
222 	p(mrt6s_cache_cleanups, "\t%llu cache cleanup%s\n");
223 	p(mrt6s_no_route, "\t%llu datagram%s with no route for origin\n");
224 	p(mrt6s_bad_tunnel, "\t%llu datagram%s arrived with bad tunneling\n");
225 	p(mrt6s_cant_tunnel, "\t%llu datagram%s could not be tunneled\n");
226 	p(mrt6s_wrong_if, "\t%llu datagram%s arrived on wrong interface\n");
227 	p(mrt6s_drop_sel, "\t%llu datagram%s selectively dropped\n");
228 	p(mrt6s_q_overflow,
229 	    "\t%llu datagram%s dropped due to queue overflow\n");
230 	p(mrt6s_pkt2large, "\t%llu datagram%s dropped for being too large\n");
231 
232 #undef	p2
233 #undef	p
234 }
235 #endif /*INET6*/
236