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