1 1.27 msaitoh /* $NetBSD: mroute.c,v 1.27 2022/09/01 10:10:20 msaitoh 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.27 msaitoh __RCSID("$NetBSD: mroute.c,v 1.27 2022/09/01 10:10:20 msaitoh 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.25 christos #include "rtutil.h" 107 1.1 brezak 108 1.24 matt static char *pktscale(u_long); 109 1.24 matt static void print_bw_meter(struct bw_meter *, int *); 110 1.11 christos 111 1.11 christos static char * 112 1.24 matt pktscale(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.27 msaitoh return buf; 129 1.7 mycroft } 130 1.7 mycroft 131 1.4 mycroft void 132 1.24 matt mroutepr(u_long mrpaddr, u_long mfchashtbladdr, u_long mfchashaddr, 133 1.24 matt u_long 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.25 christos routename4(v->v_lcl_addr.s_addr, nflag)); 201 1.11 christos printf(" %-15.15s %6lu %7lu\n", (v->v_flags & VIFF_TUNNEL) ? 202 1.25 christos routename4(v->v_rmt_addr.s_addr, nflag) : "", 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.25 christos i, routename4(mfc.mfc_origin.s_addr, nflag)); 228 1.7 mycroft printf(" %-15.15s %7s %3u ", 229 1.25 christos routename4(mfc.mfc_mcastgrp.s_addr, nflag), 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.27 msaitoh printf(" %u/%u", vifi, 234 1.27 msaitoh mfc.mfc_ttls[vifi]); 235 1.7 mycroft 236 1.4 mycroft printf("\n"); 237 1.18 manu 238 1.18 manu /* Print the bw meter information */ 239 1.18 manu { 240 1.18 manu struct bw_meter bw_meter, *bwm; 241 1.18 manu int banner_printed2 = 0; 242 1.18 manu 243 1.18 manu bwm = mfc.mfc_bw_meter; 244 1.18 manu while (bwm) { 245 1.27 msaitoh kread((u_long)bwm, 246 1.27 msaitoh (char *)&bw_meter, 247 1.27 msaitoh sizeof bw_meter); 248 1.27 msaitoh print_bw_meter(&bw_meter, 249 1.27 msaitoh &banner_printed2); 250 1.27 msaitoh bwm = bw_meter.bm_mfc_next; 251 1.18 manu } 252 1.18 manu #if 0 /* Don't ever print it? */ 253 1.18 manu if (! banner_printed2) 254 1.18 manu printf("\n No Bandwidth Meters\n"); 255 1.18 manu #endif 256 1.18 manu } 257 1.18 manu 258 1.7 mycroft nmfc++; 259 1.1 brezak } 260 1.1 brezak } 261 1.4 mycroft if (!banner_printed) 262 1.7 mycroft printf("\nMulticast Forwarding Cache is empty\n"); 263 1.7 mycroft else 264 1.7 mycroft printf("\nTotal no. of entries in cache: %d\n", nmfc); 265 1.1 brezak 266 1.1 brezak printf("\n"); 267 1.14 assar numeric_addr = saved_numeric_addr; 268 1.1 brezak } 269 1.1 brezak 270 1.18 manu static void 271 1.24 matt print_bw_meter(struct bw_meter *bw_meter, int *banner_printed) 272 1.18 manu { 273 1.26 mrg char s0[256*3], s1[256], s2[256], s3[256]; 274 1.18 manu struct timeval now, end, delta; 275 1.18 manu 276 1.18 manu gettimeofday(&now, NULL); 277 1.18 manu 278 1.18 manu if (! *banner_printed) { 279 1.18 manu printf(" Bandwidth Meters\n"); 280 1.18 manu printf(" %-30s", "Measured(Start|Packets|Bytes)"); 281 1.18 manu printf(" %s", "Type"); 282 1.18 manu printf(" %-30s", "Thresh(Interval|Packets|Bytes)"); 283 1.18 manu printf(" Remain"); 284 1.18 manu printf("\n"); 285 1.18 manu *banner_printed = 1; 286 1.18 manu } 287 1.18 manu 288 1.18 manu /* The measured values */ 289 1.18 manu if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) 290 1.27 msaitoh snprintf(s1, sizeof s1, "%llu", 291 1.27 msaitoh (unsigned long long)bw_meter->bm_measured.b_packets); 292 1.18 manu else 293 1.26 mrg snprintf(s1, sizeof s1, "?"); 294 1.18 manu if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) 295 1.27 msaitoh snprintf(s2, sizeof s2, "%llu", 296 1.27 msaitoh (unsigned long long)bw_meter->bm_measured.b_bytes); 297 1.18 manu else 298 1.26 mrg snprintf(s2, sizeof s2, "?"); 299 1.26 mrg snprintf(s0, sizeof s0, "%lld.%ld|%s|%s", 300 1.22 christos (long long)bw_meter->bm_start_time.tv_sec, 301 1.22 christos (long)bw_meter->bm_start_time.tv_usec, 302 1.18 manu s1, s2); 303 1.18 manu printf(" %-30s", s0); 304 1.18 manu 305 1.18 manu /* The type of entry */ 306 1.26 mrg snprintf(s0, sizeof s0, "%s", "?"); 307 1.18 manu if (bw_meter->bm_flags & BW_METER_GEQ) 308 1.26 mrg snprintf(s0, sizeof s0, "%s", ">="); 309 1.18 manu else if (bw_meter->bm_flags & BW_METER_LEQ) 310 1.26 mrg snprintf(s0, sizeof s0, "%s", "<="); 311 1.18 manu printf(" %-3s", s0); 312 1.18 manu 313 1.18 manu /* The threshold values */ 314 1.18 manu if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) 315 1.27 msaitoh snprintf(s1, sizeof s1, "%llu", 316 1.27 msaitoh (unsigned long long)bw_meter->bm_threshold.b_packets); 317 1.18 manu else 318 1.26 mrg snprintf(s1, sizeof s1, "?"); 319 1.18 manu if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) 320 1.27 msaitoh snprintf(s2, sizeof s2, "%llu", 321 1.27 msaitoh (unsigned long long)bw_meter->bm_threshold.b_bytes); 322 1.18 manu else 323 1.26 mrg snprintf(s2, sizeof s2, "?"); 324 1.26 mrg snprintf(s0, sizeof s0, "%lld.%ld|%s|%s", 325 1.22 christos (long long)bw_meter->bm_threshold.b_time.tv_sec, 326 1.22 christos (long)bw_meter->bm_threshold.b_time.tv_usec, 327 1.18 manu s1, s2); 328 1.18 manu printf(" %-30s", s0); 329 1.18 manu 330 1.18 manu /* Remaining time */ 331 1.18 manu timeradd(&bw_meter->bm_start_time, 332 1.18 manu &bw_meter->bm_threshold.b_time, &end); 333 1.18 manu if (timercmp(&now, &end, <=)) { 334 1.18 manu timersub(&end, &now, &delta); 335 1.26 mrg snprintf(s3, sizeof s3, "%lld.%ld", 336 1.22 christos (long long)delta.tv_sec, (long)delta.tv_usec); 337 1.18 manu } else { 338 1.18 manu /* Negative time */ 339 1.18 manu timersub(&now, &end, &delta); 340 1.26 mrg snprintf(s3, sizeof s3, "-%lld.%ld", 341 1.22 christos (long long)delta.tv_sec, (long)delta.tv_usec); 342 1.18 manu } 343 1.18 manu printf(" %s", s3); 344 1.18 manu 345 1.18 manu printf("\n"); 346 1.18 manu } 347 1.1 brezak 348 1.4 mycroft void 349 1.24 matt mrt_stats(u_long mrpaddr, u_long mstaddr) 350 1.1 brezak { 351 1.1 brezak u_int mrtproto; 352 1.1 brezak struct mrtstat mrtstat; 353 1.1 brezak 354 1.7 mycroft if (mrpaddr == 0) { 355 1.1 brezak printf("ip_mrtproto: symbol not in namelist\n"); 356 1.1 brezak return; 357 1.1 brezak } 358 1.1 brezak 359 1.4 mycroft kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto)); 360 1.1 brezak switch (mrtproto) { 361 1.7 mycroft case 0: 362 1.1 brezak printf("no multicast routing compiled into this system\n"); 363 1.1 brezak return; 364 1.1 brezak 365 1.7 mycroft case IGMP_DVMRP: 366 1.1 brezak break; 367 1.1 brezak 368 1.7 mycroft default: 369 1.1 brezak printf("multicast routing protocol %u, unknown\n", mrtproto); 370 1.1 brezak return; 371 1.1 brezak } 372 1.1 brezak 373 1.1 brezak if (mstaddr == 0) { 374 1.1 brezak printf("mrtstat: symbol not in namelist\n"); 375 1.1 brezak return; 376 1.1 brezak } 377 1.1 brezak 378 1.4 mycroft kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat)); 379 1.1 brezak printf("multicast routing:\n"); 380 1.15 itojun printf("\t%lu datagram%s with no route for origin\n", 381 1.7 mycroft mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route)); 382 1.15 itojun printf("\t%lu upcall%s made to mrouted\n", 383 1.7 mycroft mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls)); 384 1.15 itojun printf("\t%lu datagram%s with malformed tunnel options\n", 385 1.7 mycroft mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel)); 386 1.15 itojun printf("\t%lu datagram%s with no room for tunnel options\n", 387 1.7 mycroft mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel)); 388 1.15 itojun printf("\t%lu datagram%s arrived on wrong interface\n", 389 1.7 mycroft mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if)); 390 1.15 itojun printf("\t%lu datagram%s dropped due to upcall Q overflow\n", 391 1.7 mycroft mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw)); 392 1.15 itojun printf("\t%lu datagram%s dropped due to upcall socket overflow\n", 393 1.7 mycroft mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull)); 394 1.15 itojun printf("\t%lu datagram%s cleaned up by the cache\n", 395 1.7 mycroft mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups)); 396 1.15 itojun printf("\t%lu datagram%s dropped selectively by ratelimiter\n", 397 1.7 mycroft mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel)); 398 1.15 itojun printf("\t%lu datagram%s dropped - bucket Q overflow\n", 399 1.7 mycroft mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow)); 400 1.15 itojun printf("\t%lu datagram%s dropped - larger than bkt size\n", 401 1.7 mycroft mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large)); 402 1.1 brezak } 403