]> git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/mroute6.c
549740f452bf867646f4281002843aa4b33c4c89
[apple/network_cmds.git] / netstat.tproj / mroute6.c
1 /*
2 * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (C) 1998 WIDE Project.
30 * All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. Neither the name of the project nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57 /*
58 * Copyright (c) 1989 Stephen Deering
59 * Copyright (c) 1992, 1993
60 * The Regents of the University of California. All rights reserved.
61 *
62 * This code is derived from software contributed to Berkeley by
63 * Stephen Deering of Stanford University.
64 *
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
67 * are met:
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. All advertising materials mentioning features or use of this software
74 * must display the following acknowledgement:
75 * This product includes software developed by the University of
76 * California, Berkeley and its contributors.
77 * 4. Neither the name of the University nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
80 *
81 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91 * SUCH DAMAGE.
92 *
93 * @(#)mroute.c 8.2 (Berkeley) 4/28/95
94 * $FreeBSD: src/usr.bin/netstat/mroute6.c,v 1.1.2.5 2001/08/10 09:07:09 ru Exp $
95 */
96
97 #ifdef INET6
98 #include <sys/param.h>
99 #include <sys/queue.h>
100 #include <sys/socket.h>
101 #include <sys/socketvar.h>
102 #include <sys/sysctl.h>
103
104 #include <net/if.h>
105 #include <net/if_var.h>
106 #include <net/route.h>
107
108 #include <netinet/in.h>
109
110 #include <stdio.h>
111 #include <stdlib.h>
112 #include <err.h>
113
114 #define KERNEL 1
115 #include <netinet6/ip6_mroute.h>
116 #undef KERNEL
117
118 #include "netstat.h"
119
120 #define WID_ORG (lflag ? 39 : (nflag ? 29 : 18)) /* width of origin column */
121 #define WID_GRP (lflag ? 18 : (nflag ? 16 : 18)) /* width of group column */
122
123 void
124 mroute6pr(void)
125 {
126 #if 0
127 struct mf6c **mf6ctable = 0, *mfcp;
128 struct mif6 mif6table[MAXMIFS];
129 struct mf6c mfc;
130 struct rtdetq *rtep;
131 register struct mif6 *mifp;
132 register mifi_t mifi;
133 register int i;
134 register int banner_printed;
135 register int saved_nflag;
136 mifi_t maxmif = 0;
137 long waitings;
138 size_t len;
139
140 len = sizeof(mif6table);
141 if (sysctlbyname("net.inet6.ip6.mif6table", mif6table, &len, 0, 9) == -1) {
142 //printf("No IPv6 multicast routing compiled into this system.\n");
143 return;
144 }
145
146 saved_nflag = nflag;
147 nflag = 1;
148
149 banner_printed = 0;
150 for (mifi = 0, mifp = mif6table; mifi < MAXMIFS; ++mifi, ++mifp) {
151 struct ifnet ifnet = { 0 } ;
152 char ifname[IFNAMSIZ];
153
154 if (mifp->m6_ifp == NULL)
155 continue;
156
157 /*
158 * m6_ifp should be ifindex instead of ifnet pointer
159 *
160 * kread((u_long)mifp->m6_ifp, (char *)&ifnet, sizeof(ifnet));
161 */
162 maxmif = mifi;
163 if (!banner_printed) {
164 printf("\nIPv6 Multicast Interface Table\n"
165 " Mif Rate PhyIF "
166 "Pkts-In Pkts-Out\n");
167 banner_printed = 1;
168 }
169
170 printf(" %2u %4d",
171 mifi, mifp->m6_rate_limit);
172 printf(" %5s", (mifp->m6_flags & MIFF_REGISTER) ?
173 "reg0" : if_indextoname(ifnet.if_index, ifname));
174
175 printf(" %9llu %9llu\n", (unsigned long long)mifp->m6_pkt_in,
176 (unsigned long long)mifp->m6_pkt_out);
177 }
178 if (!banner_printed)
179 printf("\nIPv6 Multicast Interface Table is empty\n");
180
181 len = sizeof(MF6CTBLSIZ * sizeof(struct mf6c));
182 mf6ctable = malloc(len);
183 if (mf6ctable == 0)
184 return;
185 if (sysctlbyname("net.inet6.ip6.mf6ctable", mf6ctable, &len, 0, 0) == -1) {
186 //printf("No IPv6 multicast routing compiled into this system.\n");
187 free(mf6ctable);
188 return;
189 }
190 banner_printed = 0;
191 for (i = 0; i < MF6CTBLSIZ; ++i) {
192 mfcp = mf6ctable[i];
193 while(mfcp) {
194 if (!banner_printed) {
195 printf ("\nIPv6 Multicast Forwarding Cache\n");
196 printf(" %-*.*s %-*.*s %s",
197 WID_ORG, WID_ORG, "Origin",
198 WID_GRP, WID_GRP, "Group",
199 " Packets Waits In-Mif Out-Mifs\n");
200 banner_printed = 1;
201 }
202
203 printf(" %-*.*s", WID_ORG, WID_ORG,
204 routename6(&mfc.mf6c_origin));
205 printf(" %-*.*s", WID_GRP, WID_GRP,
206 routename6(&mfc.mf6c_mcastgrp));
207 printf(" %9llu", (unsigned long long)mfc.mf6c_pkt_cnt);
208
209 for (waitings = 0, rtep = mfc.mf6c_stall; rtep; ) {
210 /* The sysctl should return the number of packet waiting
211 * struct rtdetq rte;
212 * kread((u_long)rtep, (char *)&rte, sizeof(rte));
213 * rtep = rte.next;
214 */
215 waitings++;
216 }
217 printf(" %3ld", waitings);
218
219 if (mfc.mf6c_parent == MF6C_INCOMPLETE_PARENT)
220 printf(" --- ");
221 else
222 printf(" %3d ", mfc.mf6c_parent);
223 for (mifi = 0; mifi <= maxmif; mifi++) {
224 if (IF_ISSET(mifi, &mfc.mf6c_ifset))
225 printf(" %u", mifi);
226 }
227 printf("\n");
228
229 mfcp = mfc.mf6c_next;
230 }
231 }
232 if (!banner_printed)
233 printf("\nIPv6 Multicast Routing Table is empty\n");
234
235 printf("\n");
236 nflag = saved_nflag;
237
238 free(mf6ctable);
239 #endif
240 }
241
242 void
243 mrt6_stats(void)
244 {
245 struct mrt6stat mrtstat;
246 size_t len;
247
248 len = sizeof(mrtstat);
249 if (sysctlbyname("net.inet6.ip6.mrt6stat", &mrtstat, &len, 0, 0) == -1) {
250 //printf("No IPv6 multicast routing compiled into this system\n");
251 return;
252 }
253 printf("IPv6 multicast forwarding:\n");
254 printf(" %10llu multicast forwarding cache lookup%s\n",
255 (unsigned long long)mrtstat.mrt6s_mfc_lookups,
256 plural(mrtstat.mrt6s_mfc_lookups));
257 printf(" %10llu multicast forwarding cache miss%s\n",
258 (unsigned long long)mrtstat.mrt6s_mfc_misses,
259 plurales(mrtstat.mrt6s_mfc_misses));
260 printf(" %10llu upcall%s to mrouted\n",
261 (unsigned long long)mrtstat.mrt6s_upcalls,
262 plural(mrtstat.mrt6s_upcalls));
263 printf(" %10llu upcall queue overflow%s\n",
264 (unsigned long long)mrtstat.mrt6s_upq_ovflw,
265 plural(mrtstat.mrt6s_upq_ovflw));
266 printf(" %10llu upcall%s dropped due to full socket buffer\n",
267 (unsigned long long)mrtstat.mrt6s_upq_sockfull,
268 plural(mrtstat.mrt6s_upq_sockfull));
269 printf(" %10llu cache cleanup%s\n",
270 (unsigned long long)mrtstat.mrt6s_cache_cleanups,
271 plural(mrtstat.mrt6s_cache_cleanups));
272 printf(" %10llu datagram%s with no route for origin\n",
273 (unsigned long long)mrtstat.mrt6s_no_route,
274 plural(mrtstat.mrt6s_no_route));
275 printf(" %10llu datagram%s arrived with bad tunneling\n",
276 (unsigned long long)mrtstat.mrt6s_bad_tunnel,
277 plural(mrtstat.mrt6s_bad_tunnel));
278 printf(" %10llu datagram%s could not be tunneled\n",
279 (unsigned long long)mrtstat.mrt6s_cant_tunnel,
280 plural(mrtstat.mrt6s_cant_tunnel));
281 printf(" %10llu datagram%s arrived on wrong interface\n",
282 (unsigned long long)mrtstat.mrt6s_wrong_if,
283 plural(mrtstat.mrt6s_wrong_if));
284 printf(" %10llu datagram%s selectively dropped\n",
285 (unsigned long long)mrtstat.mrt6s_drop_sel,
286 plural(mrtstat.mrt6s_drop_sel));
287 printf(" %10llu datagram%s dropped due to queue overflow\n",
288 (unsigned long long)mrtstat.mrt6s_q_overflow,
289 plural(mrtstat.mrt6s_q_overflow));
290 printf(" %10llu datagram%s dropped for being too large\n",
291 (unsigned long long)mrtstat.mrt6s_pkt2large,
292 plural(mrtstat.mrt6s_pkt2large));
293 }
294 #endif /*INET6*/