mroute.c revision 1.24 1 1.24 matt /* $NetBSD: mroute.c,v 1.24 2012/03/20 20:34:58 matt Exp $ */
2 1.9 thorpej
3 1.1 brezak /*
4 1.4 mycroft * Copyright (c) 1992, 1993
5 1.4 mycroft * The Regents of the University of California. All rights reserved.
6 1.4 mycroft *
7 1.4 mycroft * This code is derived from software contributed to Berkeley by
8 1.4 mycroft * Stephen Deering of Stanford University.
9 1.4 mycroft *
10 1.4 mycroft * Redistribution and use in source and binary forms, with or without
11 1.4 mycroft * modification, are permitted provided that the following conditions
12 1.4 mycroft * are met:
13 1.4 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.4 mycroft * notice, this list of conditions and the following disclaimer.
15 1.4 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.4 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.4 mycroft * documentation and/or other materials provided with the distribution.
18 1.17 agc * 3. Neither the name of the University nor the names of its contributors
19 1.17 agc * may be used to endorse or promote products derived from this software
20 1.17 agc * without specific prior written permission.
21 1.17 agc *
22 1.17 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.17 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.17 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.17 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.17 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.17 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.17 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.17 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.17 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.17 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.17 agc * SUCH DAMAGE.
33 1.17 agc *
34 1.17 agc * from: @(#)mroute.c 8.1 (Berkeley) 6/6/93
35 1.17 agc */
36 1.17 agc
37 1.17 agc /*
38 1.17 agc * Copyright (c) 1989 Stephen Deering
39 1.17 agc *
40 1.17 agc * This code is derived from software contributed to Berkeley by
41 1.17 agc * Stephen Deering of Stanford University.
42 1.17 agc *
43 1.17 agc * Redistribution and use in source and binary forms, with or without
44 1.17 agc * modification, are permitted provided that the following conditions
45 1.17 agc * are met:
46 1.17 agc * 1. Redistributions of source code must retain the above copyright
47 1.17 agc * notice, this list of conditions and the following disclaimer.
48 1.17 agc * 2. Redistributions in binary form must reproduce the above copyright
49 1.17 agc * notice, this list of conditions and the following disclaimer in the
50 1.17 agc * documentation and/or other materials provided with the distribution.
51 1.4 mycroft * 3. All advertising materials mentioning features or use of this software
52 1.4 mycroft * must display the following acknowledgement:
53 1.4 mycroft * This product includes software developed by the University of
54 1.4 mycroft * California, Berkeley and its contributors.
55 1.4 mycroft * 4. Neither the name of the University nor the names of its contributors
56 1.4 mycroft * may be used to endorse or promote products derived from this software
57 1.4 mycroft * without specific prior written permission.
58 1.4 mycroft *
59 1.4 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 1.4 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 1.4 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 1.4 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 1.4 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 1.4 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 1.4 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.4 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.4 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.4 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.4 mycroft * SUCH DAMAGE.
70 1.4 mycroft *
71 1.4 mycroft * from: @(#)mroute.c 8.1 (Berkeley) 6/6/93
72 1.4 mycroft */
73 1.4 mycroft
74 1.12 lukem #include <sys/cdefs.h>
75 1.12 lukem #ifndef lint
76 1.12 lukem #if 0
77 1.12 lukem static char sccsid[] = "from: @(#)mroute.c 8.1 (Berkeley) 6/6/93";
78 1.12 lukem #else
79 1.24 matt __RCSID("$NetBSD: mroute.c,v 1.24 2012/03/20 20:34:58 matt Exp $");
80 1.12 lukem #endif
81 1.12 lukem #endif /* not lint */
82 1.12 lukem
83 1.4 mycroft /*
84 1.18 manu * Print multicast routing structures and statistics.
85 1.1 brezak *
86 1.1 brezak * MROUTING 1.0
87 1.1 brezak */
88 1.1 brezak
89 1.4 mycroft #include <sys/param.h>
90 1.4 mycroft #include <sys/socket.h>
91 1.4 mycroft #include <sys/socketvar.h>
92 1.4 mycroft #include <sys/protosw.h>
93 1.1 brezak
94 1.7 mycroft #include <net/if.h>
95 1.7 mycroft #include <net/route.h>
96 1.1 brezak #include <netinet/in.h>
97 1.1 brezak #include <netinet/igmp.h>
98 1.6 jtc #define _KERNEL
99 1.1 brezak #include <netinet/ip_mroute.h>
100 1.6 jtc #undef _KERNEL
101 1.1 brezak
102 1.4 mycroft #include <stdio.h>
103 1.4 mycroft #include <stdlib.h>
104 1.20 rpaulo #include <kvm.h>
105 1.4 mycroft #include "netstat.h"
106 1.1 brezak
107 1.24 matt static char *pktscale(u_long);
108 1.24 matt static void print_bw_meter(struct bw_meter *, int *);
109 1.11 christos
110 1.11 christos static char *
111 1.24 matt pktscale(u_long n)
112 1.7 mycroft {
113 1.16 itojun static char buf[20];
114 1.7 mycroft char t;
115 1.7 mycroft
116 1.7 mycroft if (n < 1024)
117 1.7 mycroft t = ' ';
118 1.7 mycroft else if (n < 1024 * 1024) {
119 1.7 mycroft t = 'k';
120 1.7 mycroft n /= 1024;
121 1.7 mycroft } else {
122 1.7 mycroft t = 'm';
123 1.7 mycroft n /= 1048576;
124 1.7 mycroft }
125 1.7 mycroft
126 1.13 mrg (void)snprintf(buf, sizeof buf, "%lu%c", n, t);
127 1.7 mycroft return (buf);
128 1.7 mycroft }
129 1.7 mycroft
130 1.4 mycroft void
131 1.24 matt mroutepr(u_long mrpaddr, u_long mfchashtbladdr, u_long mfchashaddr,
132 1.24 matt u_long vifaddr)
133 1.1 brezak {
134 1.1 brezak u_int mrtproto;
135 1.7 mycroft LIST_HEAD(, mfc) *mfchashtbl;
136 1.23 lukem u_long mfchash, i;
137 1.1 brezak struct vif viftable[MAXVIFS];
138 1.7 mycroft struct mfc *mfcp, mfc;
139 1.12 lukem struct vif *v;
140 1.12 lukem vifi_t vifi;
141 1.7 mycroft int banner_printed;
142 1.14 assar int saved_numeric_addr;
143 1.7 mycroft int numvifs;
144 1.7 mycroft int nmfc; /* No. of cache entries */
145 1.1 brezak
146 1.4 mycroft if (mrpaddr == 0) {
147 1.1 brezak printf("ip_mrtproto: symbol not in namelist\n");
148 1.1 brezak return;
149 1.1 brezak }
150 1.1 brezak
151 1.4 mycroft kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
152 1.1 brezak switch (mrtproto) {
153 1.4 mycroft case 0:
154 1.1 brezak printf("no multicast routing compiled into this system\n");
155 1.1 brezak return;
156 1.1 brezak
157 1.4 mycroft case IGMP_DVMRP:
158 1.1 brezak break;
159 1.1 brezak
160 1.4 mycroft default:
161 1.1 brezak printf("multicast routing protocol %u, unknown\n", mrtproto);
162 1.1 brezak return;
163 1.1 brezak }
164 1.1 brezak
165 1.7 mycroft if (mfchashtbladdr == 0) {
166 1.7 mycroft printf("mfchashtbl: symbol not in namelist\n");
167 1.7 mycroft return;
168 1.7 mycroft }
169 1.7 mycroft if (mfchashaddr == 0) {
170 1.7 mycroft printf("mfchash: symbol not in namelist\n");
171 1.1 brezak return;
172 1.1 brezak }
173 1.1 brezak if (vifaddr == 0) {
174 1.1 brezak printf("viftable: symbol not in namelist\n");
175 1.1 brezak return;
176 1.1 brezak }
177 1.1 brezak
178 1.14 assar saved_numeric_addr = numeric_addr;
179 1.14 assar numeric_addr = 1;
180 1.1 brezak
181 1.4 mycroft kread(vifaddr, (char *)&viftable, sizeof(viftable));
182 1.1 brezak banner_printed = 0;
183 1.7 mycroft numvifs = 0;
184 1.7 mycroft
185 1.1 brezak for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
186 1.4 mycroft if (v->v_lcl_addr.s_addr == 0)
187 1.4 mycroft continue;
188 1.7 mycroft numvifs = vifi;
189 1.1 brezak
190 1.1 brezak if (!banner_printed) {
191 1.7 mycroft printf("\nVirtual Interface Table\n %s%s",
192 1.7 mycroft "Vif Thresh Limit Local-Address ",
193 1.7 mycroft "Remote-Address Pkt_in Pkt_out\n");
194 1.1 brezak banner_printed = 1;
195 1.1 brezak }
196 1.1 brezak
197 1.7 mycroft printf(" %3u %3u %5u %-15.15s",
198 1.7 mycroft vifi, v->v_threshold, v->v_rate_limit,
199 1.21 elad routename4(v->v_lcl_addr.s_addr));
200 1.11 christos printf(" %-15.15s %6lu %7lu\n", (v->v_flags & VIFF_TUNNEL) ?
201 1.21 elad routename4(v->v_rmt_addr.s_addr) : "",
202 1.7 mycroft v->v_pkt_in, v->v_pkt_out);
203 1.1 brezak }
204 1.4 mycroft if (!banner_printed)
205 1.4 mycroft printf("\nVirtual Interface Table is empty\n");
206 1.1 brezak
207 1.7 mycroft kread(mfchashtbladdr, (char *)&mfchashtbl, sizeof(mfchashtbl));
208 1.7 mycroft kread(mfchashaddr, (char *)&mfchash, sizeof(mfchash));
209 1.1 brezak banner_printed = 0;
210 1.7 mycroft nmfc = 0;
211 1.7 mycroft
212 1.10 mycroft if (mfchashtbl != 0)
213 1.7 mycroft for (i = 0; i <= mfchash; ++i) {
214 1.7 mycroft kread((u_long)&mfchashtbl[i], (char *)&mfcp, sizeof(mfcp));
215 1.7 mycroft
216 1.7 mycroft for (; mfcp != 0; mfcp = mfc.mfc_hash.le_next) {
217 1.4 mycroft if (!banner_printed) {
218 1.7 mycroft printf("\nMulticast Forwarding Cache\n %s%s",
219 1.7 mycroft "Hash Origin Mcastgroup ",
220 1.7 mycroft "Traffic In-Vif Out-Vifs/Forw-ttl\n");
221 1.4 mycroft banner_printed = 1;
222 1.4 mycroft }
223 1.1 brezak
224 1.7 mycroft kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
225 1.23 lukem printf(" %3lu %-15.15s",
226 1.21 elad i, routename4(mfc.mfc_origin.s_addr));
227 1.7 mycroft printf(" %-15.15s %7s %3u ",
228 1.21 elad routename4(mfc.mfc_mcastgrp.s_addr),
229 1.7 mycroft pktscale(mfc.mfc_pkt_cnt), mfc.mfc_parent);
230 1.7 mycroft for (vifi = 0; vifi <= numvifs; ++vifi)
231 1.7 mycroft if (mfc.mfc_ttls[vifi])
232 1.7 mycroft printf(" %u/%u", vifi, mfc.mfc_ttls[vifi]);
233 1.7 mycroft
234 1.4 mycroft printf("\n");
235 1.18 manu
236 1.18 manu /* Print the bw meter information */
237 1.18 manu {
238 1.18 manu struct bw_meter bw_meter, *bwm;
239 1.18 manu int banner_printed2 = 0;
240 1.18 manu
241 1.18 manu bwm = mfc.mfc_bw_meter;
242 1.18 manu while (bwm) {
243 1.18 manu kread((u_long)bwm,
244 1.18 manu (char *)&bw_meter,
245 1.18 manu sizeof bw_meter);
246 1.18 manu print_bw_meter(&bw_meter,
247 1.18 manu &banner_printed2);
248 1.18 manu bwm = bw_meter.bm_mfc_next;
249 1.18 manu }
250 1.18 manu #if 0 /* Don't ever print it? */
251 1.18 manu if (! banner_printed2)
252 1.18 manu printf("\n No Bandwidth Meters\n");
253 1.18 manu #endif
254 1.18 manu }
255 1.18 manu
256 1.7 mycroft nmfc++;
257 1.1 brezak }
258 1.1 brezak }
259 1.4 mycroft if (!banner_printed)
260 1.7 mycroft printf("\nMulticast Forwarding Cache is empty\n");
261 1.7 mycroft else
262 1.7 mycroft printf("\nTotal no. of entries in cache: %d\n", nmfc);
263 1.1 brezak
264 1.1 brezak printf("\n");
265 1.14 assar numeric_addr = saved_numeric_addr;
266 1.1 brezak }
267 1.1 brezak
268 1.18 manu static void
269 1.24 matt print_bw_meter(struct bw_meter *bw_meter, int *banner_printed)
270 1.18 manu {
271 1.18 manu char s0[256], s1[256], s2[256], s3[256];
272 1.18 manu struct timeval now, end, delta;
273 1.18 manu
274 1.18 manu gettimeofday(&now, NULL);
275 1.18 manu
276 1.18 manu if (! *banner_printed) {
277 1.18 manu printf(" Bandwidth Meters\n");
278 1.18 manu printf(" %-30s", "Measured(Start|Packets|Bytes)");
279 1.18 manu printf(" %s", "Type");
280 1.18 manu printf(" %-30s", "Thresh(Interval|Packets|Bytes)");
281 1.18 manu printf(" Remain");
282 1.18 manu printf("\n");
283 1.18 manu *banner_printed = 1;
284 1.18 manu }
285 1.18 manu
286 1.18 manu /* The measured values */
287 1.18 manu if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
288 1.19 martin sprintf(s1, "%llu", (unsigned long long)bw_meter->bm_measured.b_packets);
289 1.18 manu else
290 1.18 manu sprintf(s1, "?");
291 1.18 manu if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
292 1.19 martin sprintf(s2, "%llu", (unsigned long long)bw_meter->bm_measured.b_bytes);
293 1.18 manu else
294 1.18 manu sprintf(s2, "?");
295 1.22 christos sprintf(s0, "%lld.%ld|%s|%s",
296 1.22 christos (long long)bw_meter->bm_start_time.tv_sec,
297 1.22 christos (long)bw_meter->bm_start_time.tv_usec,
298 1.18 manu s1, s2);
299 1.18 manu printf(" %-30s", s0);
300 1.18 manu
301 1.18 manu /* The type of entry */
302 1.18 manu sprintf(s0, "%s", "?");
303 1.18 manu if (bw_meter->bm_flags & BW_METER_GEQ)
304 1.18 manu sprintf(s0, "%s", ">=");
305 1.18 manu else if (bw_meter->bm_flags & BW_METER_LEQ)
306 1.18 manu sprintf(s0, "%s", "<=");
307 1.18 manu printf(" %-3s", s0);
308 1.18 manu
309 1.18 manu /* The threshold values */
310 1.18 manu if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
311 1.19 martin sprintf(s1, "%llu", (unsigned long long)bw_meter->bm_threshold.b_packets);
312 1.18 manu else
313 1.18 manu sprintf(s1, "?");
314 1.18 manu if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
315 1.19 martin sprintf(s2, "%llu", (unsigned long long)bw_meter->bm_threshold.b_bytes);
316 1.18 manu else
317 1.18 manu sprintf(s2, "?");
318 1.22 christos sprintf(s0, "%lld.%ld|%s|%s",
319 1.22 christos (long long)bw_meter->bm_threshold.b_time.tv_sec,
320 1.22 christos (long)bw_meter->bm_threshold.b_time.tv_usec,
321 1.18 manu s1, s2);
322 1.18 manu printf(" %-30s", s0);
323 1.18 manu
324 1.18 manu /* Remaining time */
325 1.18 manu timeradd(&bw_meter->bm_start_time,
326 1.18 manu &bw_meter->bm_threshold.b_time, &end);
327 1.18 manu if (timercmp(&now, &end, <=)) {
328 1.18 manu timersub(&end, &now, &delta);
329 1.22 christos sprintf(s3, "%lld.%ld",
330 1.22 christos (long long)delta.tv_sec, (long)delta.tv_usec);
331 1.18 manu } else {
332 1.18 manu /* Negative time */
333 1.18 manu timersub(&now, &end, &delta);
334 1.22 christos sprintf(s3, "-%lld.%ld",
335 1.22 christos (long long)delta.tv_sec, (long)delta.tv_usec);
336 1.18 manu }
337 1.18 manu printf(" %s", s3);
338 1.18 manu
339 1.18 manu printf("\n");
340 1.18 manu }
341 1.1 brezak
342 1.4 mycroft void
343 1.24 matt mrt_stats(u_long mrpaddr, u_long mstaddr)
344 1.1 brezak {
345 1.1 brezak u_int mrtproto;
346 1.1 brezak struct mrtstat mrtstat;
347 1.1 brezak
348 1.7 mycroft if (mrpaddr == 0) {
349 1.1 brezak printf("ip_mrtproto: symbol not in namelist\n");
350 1.1 brezak return;
351 1.1 brezak }
352 1.1 brezak
353 1.4 mycroft kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
354 1.1 brezak switch (mrtproto) {
355 1.7 mycroft case 0:
356 1.1 brezak printf("no multicast routing compiled into this system\n");
357 1.1 brezak return;
358 1.1 brezak
359 1.7 mycroft case IGMP_DVMRP:
360 1.1 brezak break;
361 1.1 brezak
362 1.7 mycroft default:
363 1.1 brezak printf("multicast routing protocol %u, unknown\n", mrtproto);
364 1.1 brezak return;
365 1.1 brezak }
366 1.1 brezak
367 1.1 brezak if (mstaddr == 0) {
368 1.1 brezak printf("mrtstat: symbol not in namelist\n");
369 1.1 brezak return;
370 1.1 brezak }
371 1.1 brezak
372 1.4 mycroft kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
373 1.1 brezak printf("multicast routing:\n");
374 1.15 itojun printf("\t%lu datagram%s with no route for origin\n",
375 1.7 mycroft mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
376 1.15 itojun printf("\t%lu upcall%s made to mrouted\n",
377 1.7 mycroft mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls));
378 1.15 itojun printf("\t%lu datagram%s with malformed tunnel options\n",
379 1.7 mycroft mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
380 1.15 itojun printf("\t%lu datagram%s with no room for tunnel options\n",
381 1.7 mycroft mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
382 1.15 itojun printf("\t%lu datagram%s arrived on wrong interface\n",
383 1.7 mycroft mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
384 1.15 itojun printf("\t%lu datagram%s dropped due to upcall Q overflow\n",
385 1.7 mycroft mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw));
386 1.15 itojun printf("\t%lu datagram%s dropped due to upcall socket overflow\n",
387 1.7 mycroft mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull));
388 1.15 itojun printf("\t%lu datagram%s cleaned up by the cache\n",
389 1.7 mycroft mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups));
390 1.15 itojun printf("\t%lu datagram%s dropped selectively by ratelimiter\n",
391 1.7 mycroft mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel));
392 1.15 itojun printf("\t%lu datagram%s dropped - bucket Q overflow\n",
393 1.7 mycroft mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow));
394 1.15 itojun printf("\t%lu datagram%s dropped - larger than bkt size\n",
395 1.7 mycroft mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large));
396 1.1 brezak }
397