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) 1989 Stephen Deering
30 * Copyright (c) 1992, 1993
31 *	The Regents of the University of California.  All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Stephen Deering of Stanford University.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 *    must display the following acknowledgement:
46 *	This product includes software developed by the University of
47 *	California, Berkeley and its contributors.
48 * 4. 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/*
68 * Print DVMRP multicast routing structures and statistics.
69 *
70 * MROUTING 1.0
71 */
72
73#include <sys/param.h>
74#include <sys/queue.h>
75#include <sys/socket.h>
76#include <sys/socketvar.h>
77#include <sys/mbuf.h>
78#include <sys/time.h>
79#include <sys/sysctl.h>
80
81#include <net/if.h>
82#include <netinet/in.h>
83#include <netinet/igmp.h>
84#include <net/route.h>
85#include <netinet/ip_mroute.h>
86
87#include <stdio.h>
88#include <stdlib.h>
89#include "netstat.h"
90
91void
92mroutepr(void)
93{
94	struct mfc **mfctable = 0;
95	struct vif viftable[CONFIG_MAXVIFS];
96	struct mfc *m;
97	register struct vif *v;
98	register vifi_t vifi;
99	register int i;
100	register int banner_printed;
101	register int saved_nflag;
102	vifi_t maxvif = 0;
103	size_t len;
104
105	saved_nflag = nflag;
106	nflag = 1;
107
108	len = CONFIG_MAXVIFS * sizeof(struct vif);
109	if (sysctlbyname("net.inet.ip.viftable", viftable, &len, 0, 0) == -1) {
110		//printf("No IPv4 multicast routing compiled into this system.\n");
111		return;
112	}
113
114	banner_printed = 0;
115	for (vifi = 0, v = viftable; vifi < CONFIG_MAXVIFS; ++vifi, ++v) {
116		if (v->v_lcl_addr.s_addr == 0)
117			continue;
118
119		maxvif = vifi;
120		if (!banner_printed) {
121			printf("\nVirtual Interface Table\n"
122			       " Vif   Thresh   Rate   Local-Address   "
123			       "Remote-Address    Pkts-In   Pkts-Out\n");
124			banner_printed = 1;
125		}
126
127		printf(" %2u    %6u   %4d   %-15.15s",
128					/* opposite math of add_vif() */
129		    vifi, v->v_threshold, v->v_rate_limit * 1000 / 1024,
130		    routename(v->v_lcl_addr.s_addr));
131		printf(" %-15.15s", (v->v_flags & VIFF_TUNNEL) ?
132		    routename(v->v_rmt_addr.s_addr) : "");
133
134		printf(" %9u  %9u\n", v->v_pkt_in, v->v_pkt_out);
135	}
136	if (!banner_printed)
137		printf("\nVirtual Interface Table is empty\n");
138
139	if (sysctlbyname("net.inet.ip.mfctable", 0, &len, 0, 0) == -1) {
140		//printf("No IPv4 multicast routing compiled into this system.\n");
141		return;
142	}
143	mfctable = malloc(len);
144	if (mfctable == 0)
145		return;
146	if (sysctlbyname("net.inet.ip.mfctable", mfctable, &len, 0, 0) == -1) {
147		//printf("No IPv4 multicast routing compiled into this system.\n");
148		return;
149	}
150	banner_printed = 0;
151	for (i = 0; i < CONFIG_MFCTBLSIZ; ++i) {
152		m = mfctable[i];
153		while(m) {
154			if (!banner_printed) {
155				printf("\nIPv4 Multicast Forwarding Cache\n"
156				       " Origin          Group            "
157				       " Packets In-Vif  Out-Vifs:Ttls\n");
158				banner_printed = 1;
159			}
160
161			printf(" %-15.15s", routename(m->mfc_origin.s_addr));
162			printf(" %-15.15s", routename(m->mfc_mcastgrp.s_addr));
163			printf(" %9u", m->mfc_pkt_cnt);
164			printf("  %3d   ", m->mfc_parent);
165			for (vifi = 0; vifi <= maxvif; vifi++) {
166				if (m->mfc_ttls[vifi] > 0)
167					printf(" %u:%u", vifi,
168					       m->mfc_ttls[vifi]);
169			}
170			printf("\n");
171			m = m->mfc_next;
172		}
173	}
174	if (!banner_printed)
175		printf("\nMulticast Routing Table is empty\n");
176
177	printf("\n");
178	nflag = saved_nflag;
179
180	free(mfctable);
181}
182
183
184void
185mrt_stats()
186{
187	struct mrtstat mrtstat;
188	size_t len = sizeof(struct mrtstat);
189
190	if(sysctlbyname("net.inet.ip.mrtstat", &mrtstat, &len, 0, 0) == -1) {
191		//printf("No IPv4 multicast routing compiled into this system.\n");
192		return;
193	}
194
195	printf("IPv4 multicast forwarding:\n");
196	printf(" %10u multicast forwarding cache lookup%s\n",
197	  mrtstat.mrts_mfc_lookups, plural(mrtstat.mrts_mfc_lookups));
198	printf(" %10u multicast forwarding cache miss%s\n",
199	  mrtstat.mrts_mfc_misses, plurales(mrtstat.mrts_mfc_misses));
200	printf(" %10u upcall%s to mrouted\n",
201	  mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls));
202	printf(" %10u upcall queue overflow%s\n",
203	  mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw));
204	printf(" %10u upcall%s dropped due to full socket buffer\n",
205	  mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull));
206	printf(" %10u cache cleanup%s\n",
207	  mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups));
208	printf(" %10u datagram%s with no route for origin\n",
209	  mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
210	printf(" %10u datagram%s arrived with bad tunneling\n",
211	  mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
212	printf(" %10u datagram%s could not be tunneled\n",
213	  mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
214	printf(" %10u datagram%s arrived on wrong interface\n",
215	  mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
216	printf(" %10u datagram%s selectively dropped\n",
217	  mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel));
218	printf(" %10u datagram%s dropped due to queue overflow\n",
219	  mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow));
220	printf(" %10u datagram%s dropped for being too large\n",
221	  mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large));
222}
223