1 /* $OpenBSD: mroute.c,v 1.26 2019/09/02 12:48:44 bluhm Exp $ */
2 /* $NetBSD: mroute.c,v 1.10 1996/05/11 13:51:27 mycroft Exp $ */
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.
20 * 3. Neither the name of the University nor the names of its contributors
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 /*
40 * Print multicast routing structures and statistics.
41 *
42 * MROUTING 1.0
43 */
44
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48
49 #include <netinet/in.h>
50 #include <netinet/igmp.h>
51 #include <netinet/ip_mroute.h>
52
53 #include <err.h>
54 #include <errno.h>
55 #include <limits.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <util.h>
59 #include "netstat.h"
60
61 void
mroutepr(void)62 mroutepr(void)
63 {
64 u_int mrtproto;
65 struct vifinfo *v;
66 struct mfcinfo *m;
67 size_t needed, numvifs, nummfcs, vifi, mfci;
68 char *buf = NULL;
69 char fmtbuf[FMT_SCALED_STRSIZE];
70 vifi_t maxvif = 0;
71 int mib[] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_MRTPROTO };
72 size_t len = sizeof(int);
73 int banner_printed = 0, saved_nflag;
74
75 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
76 &mrtproto, &len, NULL, 0) == -1) {
77 if (errno != ENOPROTOOPT)
78 warn("mroute");
79 return;
80 }
81 switch (mrtproto) {
82 case 0:
83 printf("no multicast routing compiled into this system\n");
84 return;
85 case IGMP_DVMRP:
86 break;
87 default:
88 printf("multicast routing protocol %u, unknown\n", mrtproto);
89 return;
90 }
91
92 saved_nflag = nflag;
93 nflag = 1;
94
95 mib[3] = IPCTL_MRTVIF;
96 needed = get_sysctl(mib, sizeof(mib) / sizeof(mib[0]), &buf);
97 numvifs = needed / sizeof(*v);
98 v = (struct vifinfo *)buf;
99 if (numvifs)
100 maxvif = v[numvifs - 1].v_vifi;
101
102 for (vifi = 0; vifi < numvifs; ++vifi, ++v) {
103 if (!banner_printed) {
104 printf("\nVirtual Interface Table\n %s%s",
105 "Vif Thresh Local-Address ",
106 "Remote-Address Pkt_in Pkt_out\n");
107 banner_printed = 1;
108 }
109
110 printf(" %3u %3u %-15.15s",
111 v->v_vifi, v->v_threshold,
112 routename4(v->v_lcl_addr.s_addr));
113 printf(" %-15.15s %6lu %7lu\n", (v->v_flags & VIFF_TUNNEL) ?
114 routename4(v->v_rmt_addr.s_addr) : "",
115 v->v_pkt_in, v->v_pkt_out);
116 }
117 if (!banner_printed)
118 printf("Virtual Interface Table is empty\n");
119
120 banner_printed = 0;
121
122 mib[3] = IPCTL_MRTMFC;
123 needed = get_sysctl(mib, sizeof(mib) / sizeof(mib[0]), &buf);
124 nummfcs = needed / sizeof(*m);
125 m = (struct mfcinfo *)buf;
126
127 for (mfci = 0; mfci < nummfcs; ++mfci, ++m) {
128 if (!banner_printed) {
129 printf("\nMulticast Forwarding Cache\n %s%s",
130 "Hash Origin Mcastgroup ",
131 "Traffic In-Vif Out-Vifs/Forw-ttl\n");
132 banner_printed = 1;
133 }
134
135 printf(" %3zu %-15.15s",
136 mfci, routename4(m->mfc_origin.s_addr));
137 fmt_scaled(m->mfc_pkt_cnt, fmtbuf);
138 printf(" %-15.15s %7s %3u ",
139 routename4(m->mfc_mcastgrp.s_addr),
140 fmtbuf, m->mfc_parent);
141 for (vifi = 0; vifi <= maxvif; ++vifi)
142 if (m->mfc_ttls[vifi])
143 printf(" %zu/%u", vifi, m->mfc_ttls[vifi]);
144
145 printf("\n");
146 }
147 if (!banner_printed)
148 printf("Multicast Forwarding Cache is empty\n");
149 else
150 printf("\nTotal no. of entries in cache: %zu\n", nummfcs);
151
152 printf("\n");
153 nflag = saved_nflag;
154
155 free(buf);
156 }
157
158 void
mrt_stats(void)159 mrt_stats(void)
160 {
161 u_int mrtproto;
162 struct mrtstat mrtstat;
163 int mib[] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_MRTPROTO };
164 int mib2[] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_MRTSTATS };
165 size_t len = sizeof(int);
166
167 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
168 &mrtproto, &len, NULL, 0) == -1) {
169 if (errno != ENOPROTOOPT)
170 warn("mroute");
171 return;
172 }
173 switch (mrtproto) {
174 case 0:
175 printf("no multicast routing compiled into this system\n");
176 return;
177
178 case IGMP_DVMRP:
179 break;
180
181 default:
182 printf("multicast routing protocol %u, unknown\n", mrtproto);
183 return;
184 }
185
186 len = sizeof(mrtstat);
187 if (sysctl(mib2, sizeof(mib2) / sizeof(mib2[0]),
188 &mrtstat, &len, NULL, 0) == -1) {
189 if (errno != ENOPROTOOPT)
190 warn("mroute");
191 return;
192 }
193
194 printf("multicast routing:\n");
195 printf("\t%lu datagram%s with no route for origin\n",
196 mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
197 printf("\t%lu upcall%s made to mrouted\n",
198 mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls));
199 printf("\t%lu datagram%s with malformed tunnel options\n",
200 mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
201 printf("\t%lu datagram%s with no room for tunnel options\n",
202 mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
203 printf("\t%lu datagram%s arrived on wrong interface\n",
204 mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
205 printf("\t%lu datagram%s dropped due to upcall Q overflow\n",
206 mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw));
207 printf("\t%lu datagram%s dropped due to upcall socket overflow\n",
208 mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull));
209 printf("\t%lu datagram%s cleaned up by the cache\n",
210 mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups));
211 printf("\t%lu datagram%s dropped selectively by ratelimiter\n",
212 mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel));
213 printf("\t%lu datagram%s dropped - bucket Q overflow\n",
214 mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow));
215 printf("\t%lu datagram%s dropped - larger than bkt size\n",
216 mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large));
217 }
218