Annotation of src/usr.bin/netstat/mroute.c, Revision 1.23
1.23 ! deraadt 1: /* $OpenBSD: mroute.c,v 1.22 2014/08/14 12:34:04 mpi Exp $ */
1.2 deraadt 2: /* $NetBSD: mroute.c,v 1.10 1996/05/11 13:51:27 mycroft Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1989 Stephen Deering
6: * Copyright (c) 1992, 1993
7: * The Regents of the University of California. All rights reserved.
8: *
9: * This code is derived from software contributed to Berkeley by
10: * Stephen Deering of Stanford University.
11: *
12: * Redistribution and use in source and binary forms, with or without
13: * modification, are permitted provided that the following conditions
14: * are met:
15: * 1. Redistributions of source code must retain the above copyright
16: * notice, this list of conditions and the following disclaimer.
17: * 2. Redistributions in binary form must reproduce the above copyright
18: * notice, this list of conditions and the following disclaimer in the
19: * documentation and/or other materials provided with the distribution.
1.10 millert 20: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: *
36: * from: @(#)mroute.c 8.1 (Berkeley) 6/6/93
37: */
38:
39: /*
1.12 mcbride 40: * Print multicast routing structures and statistics.
1.1 deraadt 41: *
42: * MROUTING 1.0
43: */
44:
1.23 ! deraadt 45: #include <sys/types.h>
1.1 deraadt 46: #include <sys/socket.h>
47: #include <sys/socketvar.h>
48: #include <sys/protosw.h>
1.17 deraadt 49: #include <sys/sysctl.h>
1.1 deraadt 50:
51: #include <net/if.h>
52: #include <net/route.h>
53: #include <netinet/in.h>
54: #include <netinet/igmp.h>
55: #define _KERNEL
56: #include <netinet/ip_mroute.h>
57: #undef _KERNEL
58:
1.18 chl 59: #include <err.h>
60: #include <errno.h>
1.5 millert 61: #include <limits.h>
1.1 deraadt 62: #include <stdio.h>
63: #include <stdlib.h>
64: #include "netstat.h"
65:
1.11 deraadt 66: static char *
1.9 deraadt 67: pktscale(u_long n)
1.1 deraadt 68: {
69: static char buf[8];
70: char t;
71:
72: if (n < 1024)
73: t = ' ';
74: else if (n < 1024 * 1024) {
75: t = 'k';
76: n /= 1024;
77: } else {
78: t = 'm';
79: n /= 1048576;
80: }
81:
1.6 deraadt 82: snprintf(buf, sizeof buf, "%lu%c", n, t);
1.1 deraadt 83: return (buf);
84: }
85:
86: void
1.17 deraadt 87: mroutepr(u_long mfchashtbladdr, u_long mfchashaddr, u_long vifaddr)
1.1 deraadt 88: {
89: u_int mrtproto;
90: LIST_HEAD(, mfc) *mfchashtbl;
91: u_long mfchash;
1.17 deraadt 92: struct vif viftable[MAXVIFS], *v;
1.1 deraadt 93: struct mfc *mfcp, mfc;
1.7 mpech 94: vifi_t vifi;
1.21 guenther 95: int mib[] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_MRTPROTO };
1.17 deraadt 96: size_t len = sizeof(int);
97: int i, banner_printed = 0, saved_nflag, numvifs = 0;
1.1 deraadt 98: int nmfc; /* No. of cache entries */
99:
1.17 deraadt 100: if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
101: &mrtproto, &len, NULL, 0) == -1) {
102: if (errno != ENOPROTOOPT)
103: warn("mroute");
1.1 deraadt 104: return;
105: }
106: switch (mrtproto) {
107: case 0:
108: printf("no multicast routing compiled into this system\n");
109: return;
110: case IGMP_DVMRP:
111: break;
112: default:
113: printf("multicast routing protocol %u, unknown\n", mrtproto);
114: return;
115: }
116:
117: if (mfchashtbladdr == 0) {
118: printf("mfchashtbl: symbol not in namelist\n");
119: return;
120: }
121: if (mfchashaddr == 0) {
122: printf("mfchash: symbol not in namelist\n");
123: return;
124: }
125: if (vifaddr == 0) {
126: printf("viftable: symbol not in namelist\n");
127: return;
128: }
129:
130: saved_nflag = nflag;
131: nflag = 1;
132:
1.13 jaredy 133: kread(vifaddr, &viftable, sizeof(viftable));
1.1 deraadt 134:
135: for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
136: if (v->v_lcl_addr.s_addr == 0)
137: continue;
138: numvifs = vifi;
139:
140: if (!banner_printed) {
141: printf("\nVirtual Interface Table\n %s%s",
142: "Vif Thresh Limit Local-Address ",
143: "Remote-Address Pkt_in Pkt_out\n");
144: banner_printed = 1;
145: }
146:
1.19 michele 147: printf(" %3u %3u %-15.15s",
148: vifi, v->v_threshold,
1.16 claudio 149: routename4(v->v_lcl_addr.s_addr));
1.4 millert 150: printf(" %-15.15s %6lu %7lu\n", (v->v_flags & VIFF_TUNNEL) ?
1.16 claudio 151: routename4(v->v_rmt_addr.s_addr) : "",
1.1 deraadt 152: v->v_pkt_in, v->v_pkt_out);
153: }
154: if (!banner_printed)
1.17 deraadt 155: printf("Virtual Interface Table is empty\n");
1.1 deraadt 156:
1.13 jaredy 157: kread(mfchashtbladdr, &mfchashtbl, sizeof(mfchashtbl));
158: kread(mfchashaddr, &mfchash, sizeof(mfchash));
1.1 deraadt 159: banner_printed = 0;
160: nmfc = 0;
161:
1.2 deraadt 162: if (mfchashtbl != 0)
163: for (i = 0; i <= mfchash; ++i) {
1.13 jaredy 164: kread((u_long)&mfchashtbl[i], &mfcp, sizeof(mfcp));
1.2 deraadt 165:
1.15 otto 166: for (; mfcp != 0; mfcp = LIST_NEXT(&mfc, mfc_hash)) {
1.2 deraadt 167: if (!banner_printed) {
168: printf("\nMulticast Forwarding Cache\n %s%s",
169: "Hash Origin Mcastgroup ",
170: "Traffic In-Vif Out-Vifs/Forw-ttl\n");
171: banner_printed = 1;
172: }
173:
1.13 jaredy 174: kread((u_long)mfcp, &mfc, sizeof(mfc));
1.2 deraadt 175: printf(" %3u %-15.15s",
1.16 claudio 176: i, routename4(mfc.mfc_origin.s_addr));
1.2 deraadt 177: printf(" %-15.15s %7s %3u ",
1.16 claudio 178: routename4(mfc.mfc_mcastgrp.s_addr),
1.2 deraadt 179: pktscale(mfc.mfc_pkt_cnt), mfc.mfc_parent);
180: for (vifi = 0; vifi <= numvifs; ++vifi)
181: if (mfc.mfc_ttls[vifi])
182: printf(" %u/%u", vifi,
183: mfc.mfc_ttls[vifi]);
1.1 deraadt 184:
1.2 deraadt 185: printf("\n");
1.12 mcbride 186:
1.2 deraadt 187: nmfc++;
1.1 deraadt 188: }
189: }
190: if (!banner_printed)
1.17 deraadt 191: printf("Multicast Forwarding Cache is empty\n");
1.1 deraadt 192: else
193: printf("\nTotal no. of entries in cache: %d\n", nmfc);
194:
195: printf("\n");
196: nflag = saved_nflag;
1.12 mcbride 197: }
1.1 deraadt 198:
199: void
1.17 deraadt 200: mrt_stats(void)
1.1 deraadt 201: {
202: u_int mrtproto;
203: struct mrtstat mrtstat;
1.21 guenther 204: int mib[] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_MRTPROTO };
205: int mib2[] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_MRTSTATS };
1.17 deraadt 206: size_t len = sizeof(int);
207:
208: if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
209: &mrtproto, &len, NULL, 0) == -1) {
210: if (errno != ENOPROTOOPT)
211: warn("mroute");
1.1 deraadt 212: return;
213: }
214: switch (mrtproto) {
215: case 0:
216: printf("no multicast routing compiled into this system\n");
217: return;
218:
219: case IGMP_DVMRP:
220: break;
221:
222: default:
223: printf("multicast routing protocol %u, unknown\n", mrtproto);
224: return;
225: }
226:
1.17 deraadt 227: len = sizeof(mrtstat);
228: if (sysctl(mib2, sizeof(mib2) / sizeof(mib2[0]),
229: &mrtstat, &len, NULL, 0) == -1) {
230: if (errno != ENOPROTOOPT)
231: warn("mroute");
1.1 deraadt 232: return;
233: }
234:
235: printf("multicast routing:\n");
1.8 itojun 236: printf("\t%lu datagram%s with no route for origin\n",
1.1 deraadt 237: mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
1.8 itojun 238: printf("\t%lu upcall%s made to mrouted\n",
1.1 deraadt 239: mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls));
1.8 itojun 240: printf("\t%lu datagram%s with malformed tunnel options\n",
1.1 deraadt 241: mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
1.8 itojun 242: printf("\t%lu datagram%s with no room for tunnel options\n",
1.1 deraadt 243: mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
1.8 itojun 244: printf("\t%lu datagram%s arrived on wrong interface\n",
1.1 deraadt 245: mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
1.8 itojun 246: printf("\t%lu datagram%s dropped due to upcall Q overflow\n",
1.1 deraadt 247: mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw));
1.8 itojun 248: printf("\t%lu datagram%s dropped due to upcall socket overflow\n",
1.1 deraadt 249: mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull));
1.8 itojun 250: printf("\t%lu datagram%s cleaned up by the cache\n",
1.1 deraadt 251: mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups));
1.8 itojun 252: printf("\t%lu datagram%s dropped selectively by ratelimiter\n",
1.1 deraadt 253: mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel));
1.8 itojun 254: printf("\t%lu datagram%s dropped - bucket Q overflow\n",
1.1 deraadt 255: mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow));
1.8 itojun 256: printf("\t%lu datagram%s dropped - larger than bkt size\n",
1.1 deraadt 257: mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large));
258: }