mroute.c revision 1.23 1 1.23 lukem /* $NetBSD: mroute.c,v 1.23 2009/04/12 16:08:37 lukem 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.23 lukem __RCSID("$NetBSD: mroute.c,v 1.23 2009/04/12 16:08:37 lukem 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.11 christos static char *pktscale __P((u_long));
108 1.18 manu static void print_bw_meter __P((struct bw_meter *, int *));
109 1.11 christos
110 1.11 christos static char *
111 1.7 mycroft pktscale(n)
112 1.7 mycroft u_long n;
113 1.7 mycroft {
114 1.16 itojun static char buf[20];
115 1.7 mycroft char t;
116 1.7 mycroft
117 1.7 mycroft if (n < 1024)
118 1.7 mycroft t = ' ';
119 1.7 mycroft else if (n < 1024 * 1024) {
120 1.7 mycroft t = 'k';
121 1.7 mycroft n /= 1024;
122 1.7 mycroft } else {
123 1.7 mycroft t = 'm';
124 1.7 mycroft n /= 1048576;
125 1.7 mycroft }
126 1.7 mycroft
127 1.13 mrg (void)snprintf(buf, sizeof buf, "%lu%c", n, t);
128 1.7 mycroft return (buf);
129 1.7 mycroft }
130 1.7 mycroft
131 1.4 mycroft void
132 1.7 mycroft mroutepr(mrpaddr, mfchashtbladdr, mfchashaddr, vifaddr)
133 1.7 mycroft u_long mrpaddr, mfchashtbladdr, mfchashaddr, vifaddr;
134 1.1 brezak {
135 1.1 brezak u_int mrtproto;
136 1.7 mycroft LIST_HEAD(, mfc) *mfchashtbl;
137 1.23 lukem u_long mfchash, i;
138 1.1 brezak struct vif viftable[MAXVIFS];
139 1.7 mycroft struct mfc *mfcp, mfc;
140 1.12 lukem struct vif *v;
141 1.12 lukem vifi_t vifi;
142 1.7 mycroft int banner_printed;
143 1.14 assar int saved_numeric_addr;
144 1.7 mycroft int numvifs;
145 1.7 mycroft int nmfc; /* No. of cache entries */
146 1.1 brezak
147 1.4 mycroft if (mrpaddr == 0) {
148 1.1 brezak printf("ip_mrtproto: symbol not in namelist\n");
149 1.1 brezak return;
150 1.1 brezak }
151 1.1 brezak
152 1.4 mycroft kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
153 1.1 brezak switch (mrtproto) {
154 1.4 mycroft case 0:
155 1.1 brezak printf("no multicast routing compiled into this system\n");
156 1.1 brezak return;
157 1.1 brezak
158 1.4 mycroft case IGMP_DVMRP:
159 1.1 brezak break;
160 1.1 brezak
161 1.4 mycroft default:
162 1.1 brezak printf("multicast routing protocol %u, unknown\n", mrtproto);
163 1.1 brezak return;
164 1.1 brezak }
165 1.1 brezak
166 1.7 mycroft if (mfchashtbladdr == 0) {
167 1.7 mycroft printf("mfchashtbl: symbol not in namelist\n");
168 1.7 mycroft return;
169 1.7 mycroft }
170 1.7 mycroft if (mfchashaddr == 0) {
171 1.7 mycroft printf("mfchash: symbol not in namelist\n");
172 1.1 brezak return;
173 1.1 brezak }
174 1.1 brezak if (vifaddr == 0) {
175 1.1 brezak printf("viftable: symbol not in namelist\n");
176 1.1 brezak return;
177 1.1 brezak }
178 1.1 brezak
179 1.14 assar saved_numeric_addr = numeric_addr;
180 1.14 assar numeric_addr = 1;
181 1.1 brezak
182 1.4 mycroft kread(vifaddr, (char *)&viftable, sizeof(viftable));
183 1.1 brezak banner_printed = 0;
184 1.7 mycroft numvifs = 0;
185 1.7 mycroft
186 1.1 brezak for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
187 1.4 mycroft if (v->v_lcl_addr.s_addr == 0)
188 1.4 mycroft continue;
189 1.7 mycroft numvifs = vifi;
190 1.1 brezak
191 1.1 brezak if (!banner_printed) {
192 1.7 mycroft printf("\nVirtual Interface Table\n %s%s",
193 1.7 mycroft "Vif Thresh Limit Local-Address ",
194 1.7 mycroft "Remote-Address Pkt_in Pkt_out\n");
195 1.1 brezak banner_printed = 1;
196 1.1 brezak }
197 1.1 brezak
198 1.7 mycroft printf(" %3u %3u %5u %-15.15s",
199 1.7 mycroft vifi, v->v_threshold, v->v_rate_limit,
200 1.21 elad routename4(v->v_lcl_addr.s_addr));
201 1.11 christos printf(" %-15.15s %6lu %7lu\n", (v->v_flags & VIFF_TUNNEL) ?
202 1.21 elad routename4(v->v_rmt_addr.s_addr) : "",
203 1.7 mycroft v->v_pkt_in, v->v_pkt_out);
204 1.1 brezak }
205 1.4 mycroft if (!banner_printed)
206 1.4 mycroft printf("\nVirtual Interface Table is empty\n");
207 1.1 brezak
208 1.7 mycroft kread(mfchashtbladdr, (char *)&mfchashtbl, sizeof(mfchashtbl));
209 1.7 mycroft kread(mfchashaddr, (char *)&mfchash, sizeof(mfchash));
210 1.1 brezak banner_printed = 0;
211 1.7 mycroft nmfc = 0;
212 1.7 mycroft
213 1.10 mycroft if (mfchashtbl != 0)
214 1.7 mycroft for (i = 0; i <= mfchash; ++i) {
215 1.7 mycroft kread((u_long)&mfchashtbl[i], (char *)&mfcp, sizeof(mfcp));
216 1.7 mycroft
217 1.7 mycroft for (; mfcp != 0; mfcp = mfc.mfc_hash.le_next) {
218 1.4 mycroft if (!banner_printed) {
219 1.7 mycroft printf("\nMulticast Forwarding Cache\n %s%s",
220 1.7 mycroft "Hash Origin Mcastgroup ",
221 1.7 mycroft "Traffic In-Vif Out-Vifs/Forw-ttl\n");
222 1.4 mycroft banner_printed = 1;
223 1.4 mycroft }
224 1.1 brezak
225 1.7 mycroft kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
226 1.23 lukem printf(" %3lu %-15.15s",
227 1.21 elad i, routename4(mfc.mfc_origin.s_addr));
228 1.7 mycroft printf(" %-15.15s %7s %3u ",
229 1.21 elad routename4(mfc.mfc_mcastgrp.s_addr),
230 1.7 mycroft pktscale(mfc.mfc_pkt_cnt), mfc.mfc_parent);
231 1.7 mycroft for (vifi = 0; vifi <= numvifs; ++vifi)
232 1.7 mycroft if (mfc.mfc_ttls[vifi])
233 1.7 mycroft printf(" %u/%u", vifi, mfc.mfc_ttls[vifi]);
234 1.7 mycroft
235 1.4 mycroft printf("\n");
236 1.18 manu
237 1.18 manu /* Print the bw meter information */
238 1.18 manu {
239 1.18 manu struct bw_meter bw_meter, *bwm;
240 1.18 manu int banner_printed2 = 0;
241 1.18 manu
242 1.18 manu bwm = mfc.mfc_bw_meter;
243 1.18 manu while (bwm) {
244 1.18 manu kread((u_long)bwm,
245 1.18 manu (char *)&bw_meter,
246 1.18 manu sizeof bw_meter);
247 1.18 manu print_bw_meter(&bw_meter,
248 1.18 manu &banner_printed2);
249 1.18 manu bwm = bw_meter.bm_mfc_next;
250 1.18 manu }
251 1.18 manu #if 0 /* Don't ever print it? */
252 1.18 manu if (! banner_printed2)
253 1.18 manu printf("\n No Bandwidth Meters\n");
254 1.18 manu #endif
255 1.18 manu }
256 1.18 manu
257 1.7 mycroft nmfc++;
258 1.1 brezak }
259 1.1 brezak }
260 1.4 mycroft if (!banner_printed)
261 1.7 mycroft printf("\nMulticast Forwarding Cache is empty\n");
262 1.7 mycroft else
263 1.7 mycroft printf("\nTotal no. of entries in cache: %d\n", nmfc);
264 1.1 brezak
265 1.1 brezak printf("\n");
266 1.14 assar numeric_addr = saved_numeric_addr;
267 1.1 brezak }
268 1.1 brezak
269 1.18 manu static void
270 1.18 manu print_bw_meter(bw_meter, banner_printed)
271 1.18 manu struct bw_meter *bw_meter;
272 1.18 manu int *banner_printed;
273 1.18 manu {
274 1.18 manu char s0[256], s1[256], s2[256], s3[256];
275 1.18 manu struct timeval now, end, delta;
276 1.18 manu
277 1.18 manu gettimeofday(&now, NULL);
278 1.18 manu
279 1.18 manu if (! *banner_printed) {
280 1.18 manu printf(" Bandwidth Meters\n");
281 1.18 manu printf(" %-30s", "Measured(Start|Packets|Bytes)");
282 1.18 manu printf(" %s", "Type");
283 1.18 manu printf(" %-30s", "Thresh(Interval|Packets|Bytes)");
284 1.18 manu printf(" Remain");
285 1.18 manu printf("\n");
286 1.18 manu *banner_printed = 1;
287 1.18 manu }
288 1.18 manu
289 1.18 manu /* The measured values */
290 1.18 manu if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
291 1.19 martin sprintf(s1, "%llu", (unsigned long long)bw_meter->bm_measured.b_packets);
292 1.18 manu else
293 1.18 manu sprintf(s1, "?");
294 1.18 manu if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
295 1.19 martin sprintf(s2, "%llu", (unsigned long long)bw_meter->bm_measured.b_bytes);
296 1.18 manu else
297 1.18 manu sprintf(s2, "?");
298 1.22 christos sprintf(s0, "%lld.%ld|%s|%s",
299 1.22 christos (long long)bw_meter->bm_start_time.tv_sec,
300 1.22 christos (long)bw_meter->bm_start_time.tv_usec,
301 1.18 manu s1, s2);
302 1.18 manu printf(" %-30s", s0);
303 1.18 manu
304 1.18 manu /* The type of entry */
305 1.18 manu sprintf(s0, "%s", "?");
306 1.18 manu if (bw_meter->bm_flags & BW_METER_GEQ)
307 1.18 manu sprintf(s0, "%s", ">=");
308 1.18 manu else if (bw_meter->bm_flags & BW_METER_LEQ)
309 1.18 manu sprintf(s0, "%s", "<=");
310 1.18 manu printf(" %-3s", s0);
311 1.18 manu
312 1.18 manu /* The threshold values */
313 1.18 manu if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
314 1.19 martin sprintf(s1, "%llu", (unsigned long long)bw_meter->bm_threshold.b_packets);
315 1.18 manu else
316 1.18 manu sprintf(s1, "?");
317 1.18 manu if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
318 1.19 martin sprintf(s2, "%llu", (unsigned long long)bw_meter->bm_threshold.b_bytes);
319 1.18 manu else
320 1.18 manu sprintf(s2, "?");
321 1.22 christos sprintf(s0, "%lld.%ld|%s|%s",
322 1.22 christos (long long)bw_meter->bm_threshold.b_time.tv_sec,
323 1.22 christos (long)bw_meter->bm_threshold.b_time.tv_usec,
324 1.18 manu s1, s2);
325 1.18 manu printf(" %-30s", s0);
326 1.18 manu
327 1.18 manu /* Remaining time */
328 1.18 manu timeradd(&bw_meter->bm_start_time,
329 1.18 manu &bw_meter->bm_threshold.b_time, &end);
330 1.18 manu if (timercmp(&now, &end, <=)) {
331 1.18 manu timersub(&end, &now, &delta);
332 1.22 christos sprintf(s3, "%lld.%ld",
333 1.22 christos (long long)delta.tv_sec, (long)delta.tv_usec);
334 1.18 manu } else {
335 1.18 manu /* Negative time */
336 1.18 manu timersub(&now, &end, &delta);
337 1.22 christos sprintf(s3, "-%lld.%ld",
338 1.22 christos (long long)delta.tv_sec, (long)delta.tv_usec);
339 1.18 manu }
340 1.18 manu printf(" %s", s3);
341 1.18 manu
342 1.18 manu printf("\n");
343 1.18 manu }
344 1.1 brezak
345 1.4 mycroft void
346 1.1 brezak mrt_stats(mrpaddr, mstaddr)
347 1.3 cgd u_long mrpaddr, mstaddr;
348 1.1 brezak {
349 1.1 brezak u_int mrtproto;
350 1.1 brezak struct mrtstat mrtstat;
351 1.1 brezak
352 1.7 mycroft if (mrpaddr == 0) {
353 1.1 brezak printf("ip_mrtproto: symbol not in namelist\n");
354 1.1 brezak return;
355 1.1 brezak }
356 1.1 brezak
357 1.4 mycroft kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
358 1.1 brezak switch (mrtproto) {
359 1.7 mycroft case 0:
360 1.1 brezak printf("no multicast routing compiled into this system\n");
361 1.1 brezak return;
362 1.1 brezak
363 1.7 mycroft case IGMP_DVMRP:
364 1.1 brezak break;
365 1.1 brezak
366 1.7 mycroft default:
367 1.1 brezak printf("multicast routing protocol %u, unknown\n", mrtproto);
368 1.1 brezak return;
369 1.1 brezak }
370 1.1 brezak
371 1.1 brezak if (mstaddr == 0) {
372 1.1 brezak printf("mrtstat: symbol not in namelist\n");
373 1.1 brezak return;
374 1.1 brezak }
375 1.1 brezak
376 1.4 mycroft kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
377 1.1 brezak printf("multicast routing:\n");
378 1.15 itojun printf("\t%lu datagram%s with no route for origin\n",
379 1.7 mycroft mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
380 1.15 itojun printf("\t%lu upcall%s made to mrouted\n",
381 1.7 mycroft mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls));
382 1.15 itojun printf("\t%lu datagram%s with malformed tunnel options\n",
383 1.7 mycroft mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
384 1.15 itojun printf("\t%lu datagram%s with no room for tunnel options\n",
385 1.7 mycroft mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
386 1.15 itojun printf("\t%lu datagram%s arrived on wrong interface\n",
387 1.7 mycroft mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
388 1.15 itojun printf("\t%lu datagram%s dropped due to upcall Q overflow\n",
389 1.7 mycroft mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw));
390 1.15 itojun printf("\t%lu datagram%s dropped due to upcall socket overflow\n",
391 1.7 mycroft mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull));
392 1.15 itojun printf("\t%lu datagram%s cleaned up by the cache\n",
393 1.7 mycroft mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups));
394 1.15 itojun printf("\t%lu datagram%s dropped selectively by ratelimiter\n",
395 1.7 mycroft mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel));
396 1.15 itojun printf("\t%lu datagram%s dropped - bucket Q overflow\n",
397 1.7 mycroft mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow));
398 1.15 itojun printf("\t%lu datagram%s dropped - larger than bkt size\n",
399 1.7 mycroft mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large));
400 1.1 brezak }
401