1 /* 2 * Copyright (c) 1989 Stephen Deering 3 * Copyright (c) 1992, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Stephen Deering of Stanford University. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)mroute.c 8.2 (Berkeley) 4/28/95 38 */ 39 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 /* 44 * Print multicast routing structures and statistics. 45 * 46 * MROUTING 1.0 47 */ 48 49 #include <sys/param.h> 50 #include <sys/queue.h> 51 #include <sys/socket.h> 52 #include <sys/socketvar.h> 53 #include <sys/sysctl.h> 54 #include <sys/protosw.h> 55 #include <sys/mbuf.h> 56 #include <sys/time.h> 57 58 #include <net/if.h> 59 #include <netinet/in.h> 60 #include <netinet/igmp.h> 61 #include <net/route.h> 62 #include <netinet/ip_mroute.h> 63 64 #include <err.h> 65 #include <stdint.h> 66 #include <stdio.h> 67 #include <stdlib.h> 68 #include "netstat.h" 69 70 static void print_bw_meter(struct bw_meter *bw_meter, int *banner_printed); 71 72 void 73 mroutepr(u_long mfcaddr, u_long vifaddr) 74 { 75 struct mfc *mfctable[MFCTBLSIZ]; 76 struct vif viftable[MAXVIFS]; 77 struct mfc mfc, *m; 78 struct vif *v; 79 vifi_t vifi; 80 int i; 81 int banner_printed; 82 int saved_numeric_addr; 83 vifi_t maxvif = 0; 84 size_t len; 85 86 len = sizeof(mfctable); 87 if (sysctlbyname("net.inet.ip.mfctable", mfctable, &len, NULL, 0) < 0) { 88 warn("sysctl: net.inet.ip.mfctable"); 89 if (mfcaddr == 0) { 90 printf("No IPv4 multicast forwarding configured in " 91 "the running system.\n"); 92 return; 93 } 94 /* 95 * XXX: Try KVM if the module is neither compiled nor loaded. 96 * The correct behaviour would be to always use KVM if 97 * the -M option is specified to netstat(1). 98 */ 99 kread(mfcaddr, (char *)mfctable, sizeof(mfctable)); 100 } 101 102 len = sizeof(viftable); 103 if (sysctlbyname("net.inet.ip.viftable", viftable, &len, NULL, 0) < 0) { 104 warn("sysctl: net.inet.ip.viftable"); 105 if (vifaddr == 0) { 106 printf("No IPv4 multicast forwarding configured in " 107 "the running system.\n"); 108 return; 109 } 110 /* XXX KVM */ 111 kread(vifaddr, (char *)viftable, sizeof(viftable)); 112 } 113 114 saved_numeric_addr = numeric_addr; 115 numeric_addr = 1; 116 117 banner_printed = 0; 118 for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) { 119 if (v->v_lcl_addr.s_addr == 0) 120 continue; 121 122 maxvif = vifi; 123 if (!banner_printed) { 124 printf("\nVirtual Interface Table\n" 125 " Vif Thresh Rate Local-Address " 126 "Remote-Address Pkts-In Pkts-Out\n"); 127 banner_printed = 1; 128 } 129 130 printf(" %2u %6u %4d %-15.15s", 131 /* opposite math of add_vif() */ 132 vifi, v->v_threshold, v->v_rate_limit * 1000 / 1024, 133 routename(v->v_lcl_addr.s_addr)); 134 printf(" %-15.15s", (v->v_flags & VIFF_TUNNEL) ? 135 routename(v->v_rmt_addr.s_addr) : ""); 136 137 printf(" %9lu %9lu\n", v->v_pkt_in, v->v_pkt_out); 138 } 139 if (!banner_printed) 140 printf("\nVirtual Interface Table is empty\n"); 141 142 banner_printed = 0; 143 for (i = 0; i < MFCTBLSIZ; ++i) { 144 m = mfctable[i]; 145 while(m) { 146 kread((u_long)m, (char *)&mfc, sizeof mfc); 147 148 if (!banner_printed) { 149 printf("\nIPv4 Multicast Forwarding Cache\n" 150 " Origin Group " 151 " Packets In-Vif Out-Vifs:Ttls\n"); 152 banner_printed = 1; 153 } 154 155 printf(" %-15.15s", routename(mfc.mfc_origin.s_addr)); 156 printf(" %-15.15s", routename(mfc.mfc_mcastgrp.s_addr)); 157 printf(" %9lu", mfc.mfc_pkt_cnt); 158 printf(" %3d ", mfc.mfc_parent); 159 for (vifi = 0; vifi <= maxvif; vifi++) { 160 if (mfc.mfc_ttls[vifi] > 0) 161 printf(" %u:%u", vifi, 162 mfc.mfc_ttls[vifi]); 163 } 164 printf("\n"); 165 166 /* Print the bw meter information */ 167 { 168 struct bw_meter bw_meter, *bwm; 169 int banner_printed2 = 0; 170 171 bwm = mfc.mfc_bw_meter; 172 while (bwm) { 173 kread((u_long)bwm, (char *)&bw_meter, 174 sizeof bw_meter); 175 print_bw_meter(&bw_meter, 176 &banner_printed2); 177 bwm = bw_meter.bm_mfc_next; 178 } 179 #if 0 /* Don't ever print it? */ 180 if (! banner_printed2) 181 printf("\n No Bandwidth Meters\n"); 182 #endif 183 } 184 185 m = mfc.mfc_next; 186 } 187 } 188 if (!banner_printed) 189 printf("\nMulticast Routing Table is empty\n"); 190 191 printf("\n"); 192 numeric_addr = saved_numeric_addr; 193 } 194 195 static void 196 print_bw_meter(struct bw_meter *bw_meter, int *banner_printed) 197 { 198 char s0[256], s1[256], s2[256], s3[256]; 199 struct timeval now, end, delta; 200 201 gettimeofday(&now, NULL); 202 203 if (! *banner_printed) { 204 printf(" Bandwidth Meters\n"); 205 printf(" %-30s", "Measured(Start|Packets|Bytes)"); 206 printf(" %s", "Type"); 207 printf(" %-30s", "Thresh(Interval|Packets|Bytes)"); 208 printf(" Remain"); 209 printf("\n"); 210 *banner_printed = 1; 211 } 212 213 /* The measured values */ 214 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) 215 sprintf(s1, "%ju", (uintmax_t)bw_meter->bm_measured.b_packets); 216 else 217 sprintf(s1, "?"); 218 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) 219 sprintf(s2, "%ju", (uintmax_t)bw_meter->bm_measured.b_bytes); 220 else 221 sprintf(s2, "?"); 222 sprintf(s0, "%lu.%lu|%s|%s", 223 (u_long)bw_meter->bm_start_time.tv_sec, 224 (u_long)bw_meter->bm_start_time.tv_usec, 225 s1, s2); 226 printf(" %-30s", s0); 227 228 /* The type of entry */ 229 sprintf(s0, "%s", "?"); 230 if (bw_meter->bm_flags & BW_METER_GEQ) 231 sprintf(s0, "%s", ">="); 232 else if (bw_meter->bm_flags & BW_METER_LEQ) 233 sprintf(s0, "%s", "<="); 234 printf(" %-3s", s0); 235 236 /* The threshold values */ 237 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) 238 sprintf(s1, "%ju", (uintmax_t)bw_meter->bm_threshold.b_packets); 239 else 240 sprintf(s1, "?"); 241 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) 242 sprintf(s2, "%ju", (uintmax_t)bw_meter->bm_threshold.b_bytes); 243 else 244 sprintf(s2, "?"); 245 sprintf(s0, "%lu.%lu|%s|%s", 246 (u_long)bw_meter->bm_threshold.b_time.tv_sec, 247 (u_long)bw_meter->bm_threshold.b_time.tv_usec, 248 s1, s2); 249 printf(" %-30s", s0); 250 251 /* Remaining time */ 252 timeradd(&bw_meter->bm_start_time, 253 &bw_meter->bm_threshold.b_time, &end); 254 if (timercmp(&now, &end, <=)) { 255 timersub(&end, &now, &delta); 256 sprintf(s3, "%lu.%lu", 257 (u_long)delta.tv_sec, 258 (u_long)delta.tv_usec); 259 } else { 260 /* Negative time */ 261 timersub(&now, &end, &delta); 262 sprintf(s3, "-%lu.%lu", 263 (u_long)delta.tv_sec, 264 (u_long)delta.tv_usec); 265 } 266 printf(" %s", s3); 267 268 printf("\n"); 269 } 270 271 void 272 mrt_stats(u_long mstaddr) 273 { 274 struct mrtstat mrtstat; 275 size_t len = sizeof mrtstat; 276 277 if (sysctlbyname("net.inet.ip.mrtstat", &mrtstat, &len, 278 NULL, 0) < 0) { 279 warn("sysctl: net.inet.ip.mrtstat"); 280 /* Compatability with older kernels - candidate for removal */ 281 if (mstaddr == 0) { 282 printf("No IPv4 multicast forwarding configured in " 283 "the running system.\n"); 284 return; 285 } 286 287 kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat)); 288 } 289 printf("IPv4 multicast forwarding:\n"); 290 291 #define p(f, m) if (mrtstat.f || sflag <= 1) \ 292 printf(m, mrtstat.f, plural(mrtstat.f)) 293 #define p2(f, m) if (mrtstat.f || sflag <= 1) \ 294 printf(m, mrtstat.f, plurales(mrtstat.f)) 295 296 p(mrts_mfc_lookups, "\t%lu multicast forwarding cache lookup%s\n"); 297 p2(mrts_mfc_misses, "\t%lu multicast forwarding cache miss%s\n"); 298 p(mrts_upcalls, "\t%lu upcall%s to multicast routing daemon\n"); 299 p(mrts_upq_ovflw, "\t%lu upcall queue overflow%s\n"); 300 p(mrts_upq_sockfull, 301 "\t%lu upcall%s dropped due to full socket buffer\n"); 302 p(mrts_cache_cleanups, "\t%lu cache cleanup%s\n"); 303 p(mrts_no_route, "\t%lu datagram%s with no route for origin\n"); 304 p(mrts_bad_tunnel, "\t%lu datagram%s arrived with bad tunneling\n"); 305 p(mrts_cant_tunnel, "\t%lu datagram%s could not be tunneled\n"); 306 p(mrts_wrong_if, "\t%lu datagram%s arrived on wrong interface\n"); 307 p(mrts_drop_sel, "\t%lu datagram%s selectively dropped\n"); 308 p(mrts_q_overflow, "\t%lu datagram%s dropped due to queue overflow\n"); 309 p(mrts_pkt2large, "\t%lu datagram%s dropped for being too large\n"); 310 311 #undef p2 312 #undef p 313 } 314