]> git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/mroute6.c
network_cmds-325.1.tar.gz
[apple/network_cmds.git] / netstat.tproj / mroute6.c
1 /*
2 * Copyright (c) 2008 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 struct mf6c **mf6ctable = 0, *mfcp;
127 struct mif6 mif6table[MAXMIFS];
128 struct mf6c mfc;
129 struct rtdetq *rtep;
130 register struct mif6 *mifp;
131 register mifi_t mifi;
132 register int i;
133 register int banner_printed;
134 register int saved_nflag;
135 mifi_t maxmif = 0;
136 long waitings;
137 size_t len;
138
139 len = sizeof(mif6table);
140 if (sysctlbyname("net.inet6.ip6.mif6table", mif6table, &len, 0, 9) == -1) {
141 printf("No IPv6 multicast routing compiled into this system.\n");
142 return;
143 }
144
145 saved_nflag = nflag;
146 nflag = 1;
147
148 banner_printed = 0;
149 for (mifi = 0, mifp = mif6table; mifi < MAXMIFS; ++mifi, ++mifp) {
150 struct ifnet ifnet = { 0 } ;
151 char ifname[IFNAMSIZ];
152
153 if (mifp->m6_ifp == NULL)
154 continue;
155
156 /*
157 * m6_ifp should be ifindex instead of ifnet pointer
158 *
159 * kread((u_long)mifp->m6_ifp, (char *)&ifnet, sizeof(ifnet));
160 */
161 maxmif = mifi;
162 if (!banner_printed) {
163 printf("\nIPv6 Multicast Interface Table\n"
164 " Mif Rate PhyIF "
165 "Pkts-In Pkts-Out\n");
166 banner_printed = 1;
167 }
168
169 printf(" %2u %4d",
170 mifi, mifp->m6_rate_limit);
171 printf(" %5s", (mifp->m6_flags & MIFF_REGISTER) ?
172 "reg0" : if_indextoname(ifnet.if_index, ifname));
173
174 printf(" %9llu %9llu\n", (unsigned long long)mifp->m6_pkt_in,
175 (unsigned long long)mifp->m6_pkt_out);
176 }
177 if (!banner_printed)
178 printf("\nIPv6 Multicast Interface Table is empty\n");
179
180 len = sizeof(MF6CTBLSIZ * sizeof(struct mf6c));
181 mf6ctable = malloc(len);
182 if (mf6ctable == 0)
183 return;
184 if (sysctlbyname("net.inet6.ip6.mf6ctable", mf6ctable, &len, 0, 0) == -1) {
185 printf("No IPv6 multicast routing compiled into this system.\n");
186 free(mf6ctable);
187 return;
188 }
189 banner_printed = 0;
190 for (i = 0; i < MF6CTBLSIZ; ++i) {
191 mfcp = mf6ctable[i];
192 while(mfcp) {
193 if (!banner_printed) {
194 printf ("\nIPv6 Multicast Forwarding Cache\n");
195 printf(" %-*.*s %-*.*s %s",
196 WID_ORG, WID_ORG, "Origin",
197 WID_GRP, WID_GRP, "Group",
198 " Packets Waits In-Mif Out-Mifs\n");
199 banner_printed = 1;
200 }
201
202 printf(" %-*.*s", WID_ORG, WID_ORG,
203 routename6(&mfc.mf6c_origin));
204 printf(" %-*.*s", WID_GRP, WID_GRP,
205 routename6(&mfc.mf6c_mcastgrp));
206 printf(" %9llu", (unsigned long long)mfc.mf6c_pkt_cnt);
207
208 for (waitings = 0, rtep = mfc.mf6c_stall; rtep; ) {
209 /* The sysctl should return the number of packet waiting
210 * struct rtdetq rte;
211 * kread((u_long)rtep, (char *)&rte, sizeof(rte));
212 * rtep = rte.next;
213 */
214 waitings++;
215 }
216 printf(" %3ld", waitings);
217
218 if (mfc.mf6c_parent == MF6C_INCOMPLETE_PARENT)
219 printf(" --- ");
220 else
221 printf(" %3d ", mfc.mf6c_parent);
222 for (mifi = 0; mifi <= maxmif; mifi++) {
223 if (IF_ISSET(mifi, &mfc.mf6c_ifset))
224 printf(" %u", mifi);
225 }
226 printf("\n");
227
228 mfcp = mfc.mf6c_next;
229 }
230 }
231 if (!banner_printed)
232 printf("\nIPv6 Multicast Routing Table is empty\n");
233
234 printf("\n");
235 nflag = saved_nflag;
236
237 free(mf6ctable);
238 }
239
240 void
241 mrt6_stats(void)
242 {
243 struct mrt6stat mrtstat;
244 size_t len;
245
246 len = sizeof(mrtstat);
247 if (sysctlbyname("net.inet6.ip6.mrt6stat", &mrtstat, &len, 0, 0) == -1) {
248 printf("No IPv6 multicast routing compiled into this system\n");
249 return;
250 }
251 printf("IPv6 multicast forwarding:\n");
252 printf(" %10llu multicast forwarding cache lookup%s\n",
253 (unsigned long long)mrtstat.mrt6s_mfc_lookups,
254 plural(mrtstat.mrt6s_mfc_lookups));
255 printf(" %10llu multicast forwarding cache miss%s\n",
256 (unsigned long long)mrtstat.mrt6s_mfc_misses,
257 plurales(mrtstat.mrt6s_mfc_misses));
258 printf(" %10llu upcall%s to mrouted\n",
259 (unsigned long long)mrtstat.mrt6s_upcalls,
260 plural(mrtstat.mrt6s_upcalls));
261 printf(" %10llu upcall queue overflow%s\n",
262 (unsigned long long)mrtstat.mrt6s_upq_ovflw,
263 plural(mrtstat.mrt6s_upq_ovflw));
264 printf(" %10llu upcall%s dropped due to full socket buffer\n",
265 (unsigned long long)mrtstat.mrt6s_upq_sockfull,
266 plural(mrtstat.mrt6s_upq_sockfull));
267 printf(" %10llu cache cleanup%s\n",
268 (unsigned long long)mrtstat.mrt6s_cache_cleanups,
269 plural(mrtstat.mrt6s_cache_cleanups));
270 printf(" %10llu datagram%s with no route for origin\n",
271 (unsigned long long)mrtstat.mrt6s_no_route,
272 plural(mrtstat.mrt6s_no_route));
273 printf(" %10llu datagram%s arrived with bad tunneling\n",
274 (unsigned long long)mrtstat.mrt6s_bad_tunnel,
275 plural(mrtstat.mrt6s_bad_tunnel));
276 printf(" %10llu datagram%s could not be tunneled\n",
277 (unsigned long long)mrtstat.mrt6s_cant_tunnel,
278 plural(mrtstat.mrt6s_cant_tunnel));
279 printf(" %10llu datagram%s arrived on wrong interface\n",
280 (unsigned long long)mrtstat.mrt6s_wrong_if,
281 plural(mrtstat.mrt6s_wrong_if));
282 printf(" %10llu datagram%s selectively dropped\n",
283 (unsigned long long)mrtstat.mrt6s_drop_sel,
284 plural(mrtstat.mrt6s_drop_sel));
285 printf(" %10llu datagram%s dropped due to queue overflow\n",
286 (unsigned long long)mrtstat.mrt6s_q_overflow,
287 plural(mrtstat.mrt6s_q_overflow));
288 printf(" %10llu datagram%s dropped for being too large\n",
289 (unsigned long long)mrtstat.mrt6s_pkt2large,
290 plural(mrtstat.mrt6s_pkt2large));
291 }
292 #endif /*INET6*/