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 63 #define _KERNEL 1 64 #include <netinet/ip_mroute.h> 65 #undef _KERNEL 66 67 #include <err.h> 68 #include <stdint.h> 69 #include <stdio.h> 70 #include <stdlib.h> 71 #include "netstat.h" 72 73 74 static void print_bw_meter(struct bw_meter *, int *); 75 static void print_mfc(struct mfc *, int, int *); 76 77 static void 78 print_bw_meter(struct bw_meter *bw_meter, int *banner_printed) 79 { 80 char s0[256], s1[256], s2[256], s3[256]; 81 struct timeval now, end, delta; 82 83 gettimeofday(&now, NULL); 84 85 if (! *banner_printed) { 86 printf(" Bandwidth Meters\n"); 87 printf(" %-30s", "Measured(Start|Packets|Bytes)"); 88 printf(" %s", "Type"); 89 printf(" %-30s", "Thresh(Interval|Packets|Bytes)"); 90 printf(" Remain"); 91 printf("\n"); 92 *banner_printed = 1; 93 } 94 95 /* The measured values */ 96 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) 97 sprintf(s1, "%ju", (uintmax_t)bw_meter->bm_measured.b_packets); 98 else 99 sprintf(s1, "?"); 100 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) 101 sprintf(s2, "%ju", (uintmax_t)bw_meter->bm_measured.b_bytes); 102 else 103 sprintf(s2, "?"); 104 sprintf(s0, "%lu.%lu|%s|%s", 105 (u_long)bw_meter->bm_start_time.tv_sec, 106 (u_long)bw_meter->bm_start_time.tv_usec, 107 s1, s2); 108 printf(" %-30s", s0); 109 110 /* The type of entry */ 111 sprintf(s0, "%s", "?"); 112 if (bw_meter->bm_flags & BW_METER_GEQ) 113 sprintf(s0, "%s", ">="); 114 else if (bw_meter->bm_flags & BW_METER_LEQ) 115 sprintf(s0, "%s", "<="); 116 printf(" %-3s", s0); 117 118 /* The threshold values */ 119 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) 120 sprintf(s1, "%ju", (uintmax_t)bw_meter->bm_threshold.b_packets); 121 else 122 sprintf(s1, "?"); 123 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) 124 sprintf(s2, "%ju", (uintmax_t)bw_meter->bm_threshold.b_bytes); 125 else 126 sprintf(s2, "?"); 127 sprintf(s0, "%lu.%lu|%s|%s", 128 (u_long)bw_meter->bm_threshold.b_time.tv_sec, 129 (u_long)bw_meter->bm_threshold.b_time.tv_usec, 130 s1, s2); 131 printf(" %-30s", s0); 132 133 /* Remaining time */ 134 timeradd(&bw_meter->bm_start_time, 135 &bw_meter->bm_threshold.b_time, &end); 136 if (timercmp(&now, &end, <=)) { 137 timersub(&end, &now, &delta); 138 sprintf(s3, "%lu.%lu", 139 (u_long)delta.tv_sec, 140 (u_long)delta.tv_usec); 141 } else { 142 /* Negative time */ 143 timersub(&now, &end, &delta); 144 sprintf(s3, "-%lu.%lu", 145 (u_long)delta.tv_sec, 146 (u_long)delta.tv_usec); 147 } 148 printf(" %s", s3); 149 150 printf("\n"); 151 } 152 153 static void 154 print_mfc(struct mfc *m, int maxvif, int *banner_printed) 155 { 156 struct bw_meter bw_meter, *bwm; 157 int bw_banner_printed; 158 int error; 159 vifi_t vifi; 160 161 bw_banner_printed = 0; 162 163 if (! *banner_printed) { 164 printf("\nIPv4 Multicast Forwarding Table\n" 165 " Origin Group " 166 " Packets In-Vif Out-Vifs:Ttls\n"); 167 *banner_printed = 1; 168 } 169 170 printf(" %-15.15s", routename(m->mfc_origin.s_addr)); 171 printf(" %-15.15s", routename(m->mfc_mcastgrp.s_addr)); 172 printf(" %9lu", m->mfc_pkt_cnt); 173 printf(" %3d ", m->mfc_parent); 174 for (vifi = 0; vifi <= maxvif; vifi++) { 175 if (m->mfc_ttls[vifi] > 0) 176 printf(" %u:%u", vifi, m->mfc_ttls[vifi]); 177 } 178 printf("\n"); 179 180 /* 181 * XXX We break the rules and try to use KVM to read the 182 * bandwidth meters, they are not retrievable via sysctl yet. 183 */ 184 bwm = m->mfc_bw_meter; 185 while (bwm != NULL) { 186 error = kread((u_long)bwm, (char *)&bw_meter, 187 sizeof(bw_meter)); 188 if (error) 189 break; 190 print_bw_meter(&bw_meter, &bw_banner_printed); 191 bwm = bw_meter.bm_mfc_next; 192 } 193 } 194 195 void 196 mroutepr(u_long pmfchashtbl, u_long pmfctablesize, u_long pviftbl) 197 { 198 struct vif viftable[MAXVIFS]; 199 struct vif *v; 200 struct mfc *m; 201 int banner_printed; 202 int saved_numeric_addr; 203 size_t len; 204 vifi_t vifi, maxvif; 205 206 saved_numeric_addr = numeric_addr; 207 numeric_addr = 1; 208 209 /* 210 * TODO: 211 * The VIF table will move to hanging off the struct if_info for 212 * each IPv4 configured interface. Currently it is statically 213 * allocated, and retrieved either using KVM or an opaque SYSCTL. 214 * 215 * This can't happen until the API documented in multicast(4) 216 * is itself refactored. The historical reason why VIFs use 217 * a separate ifindex space is entirely due to the legacy 218 * capability of the MROUTING code to create IPIP tunnels on 219 * the fly to support DVMRP. When gif(4) became available, this 220 * functionality was deprecated, as PIM does not use it. 221 */ 222 maxvif = 0; 223 224 len = sizeof(viftable); 225 if (live) { 226 if (sysctlbyname("net.inet.ip.viftable", viftable, &len, NULL, 227 0) < 0) { 228 warn("sysctl: net.inet.ip.viftable"); 229 return; 230 } 231 } else 232 kread(pviftbl, (char *)viftable, sizeof(viftable)); 233 234 banner_printed = 0; 235 for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) { 236 if (v->v_lcl_addr.s_addr == 0) 237 continue; 238 239 maxvif = vifi; 240 if (!banner_printed) { 241 printf("\nIPv4 Virtual Interface Table\n" 242 " Vif Thresh Local-Address " 243 "Remote-Address Pkts-In Pkts-Out\n"); 244 banner_printed = 1; 245 } 246 247 printf(" %2u %6u %-15.15s", 248 /* opposite math of add_vif() */ 249 vifi, v->v_threshold, 250 routename(v->v_lcl_addr.s_addr)); 251 printf(" %-15.15s", (v->v_flags & VIFF_TUNNEL) ? 252 routename(v->v_rmt_addr.s_addr) : ""); 253 254 printf(" %9lu %9lu\n", v->v_pkt_in, v->v_pkt_out); 255 } 256 if (!banner_printed) 257 printf("\nIPv4 Virtual Interface Table is empty\n"); 258 259 banner_printed = 0; 260 261 /* 262 * TODO: 263 * The MFC table will move into the AF_INET radix trie in future. 264 * In 8.x, it becomes a dynamically allocated structure referenced 265 * by a hashed LIST, allowing more than 256 entries w/o kernel tuning. 266 * 267 * If retrieved via opaque SYSCTL, the kernel will coalesce it into 268 * a static table for us. 269 * If retrieved via KVM, the hash list pointers must be followed. 270 */ 271 if (live) { 272 struct mfc *mfctable; 273 274 len = 0; 275 if (sysctlbyname("net.inet.ip.mfctable", NULL, &len, NULL, 276 0) < 0) { 277 warn("sysctl: net.inet.ip.mfctable"); 278 return; 279 } 280 281 mfctable = malloc(len); 282 if (mfctable == NULL) { 283 warnx("malloc %lu bytes", (u_long)len); 284 return; 285 } 286 if (sysctlbyname("net.inet.ip.mfctable", mfctable, &len, NULL, 287 0) < 0) { 288 free(mfctable); 289 warn("sysctl: net.inet.ip.mfctable"); 290 return; 291 } 292 293 m = mfctable; 294 while (len >= sizeof(*m)) { 295 print_mfc(m++, maxvif, &banner_printed); 296 len -= sizeof(*m); 297 } 298 if (len != 0) 299 warnx("print_mfc: %lu trailing bytes", (u_long)len); 300 301 free(mfctable); 302 } else { 303 LIST_HEAD(, mfc) *mfchashtbl; 304 u_long i, mfctablesize; 305 struct mfc mfc; 306 int error; 307 308 error = kread(pmfctablesize, (char *)&mfctablesize, 309 sizeof(u_long)); 310 if (error) { 311 warn("kread: mfctablesize"); 312 return; 313 } 314 315 len = sizeof(*mfchashtbl) * mfctablesize; 316 mfchashtbl = malloc(len); 317 if (mfchashtbl == NULL) { 318 warnx("malloc %lu bytes", (u_long)len); 319 return; 320 } 321 kread(pmfchashtbl, (char *)&mfchashtbl, len); 322 323 for (i = 0; i < mfctablesize; i++) { 324 LIST_FOREACH(m, &mfchashtbl[i], mfc_hash) { 325 kread((u_long)m, (char *)&mfc, sizeof(mfc)); 326 print_mfc(m, maxvif, &banner_printed); 327 } 328 } 329 330 free(mfchashtbl); 331 } 332 333 if (!banner_printed) 334 printf("\nIPv4 Multicast Forwarding Table is empty\n"); 335 336 printf("\n"); 337 numeric_addr = saved_numeric_addr; 338 } 339 340 void 341 mrt_stats(u_long mstaddr) 342 { 343 struct mrtstat mrtstat; 344 size_t len = sizeof mrtstat; 345 346 if (live) { 347 if (sysctlbyname("net.inet.ip.mrtstat", &mrtstat, &len, NULL, 348 0) < 0) { 349 warn("sysctl: net.inet.ip.mrtstat"); 350 return; 351 } 352 } else 353 kread_counters(mstaddr, &mrtstat, len); 354 355 printf("IPv4 multicast forwarding:\n"); 356 357 #define p(f, m) if (mrtstat.f || sflag <= 1) \ 358 printf(m, (uintmax_t)mrtstat.f, plural(mrtstat.f)) 359 #define p2(f, m) if (mrtstat.f || sflag <= 1) \ 360 printf(m, (uintmax_t)mrtstat.f, plurales(mrtstat.f)) 361 362 p(mrts_mfc_lookups, "\t%ju multicast forwarding cache lookup%s\n"); 363 p2(mrts_mfc_misses, "\t%ju multicast forwarding cache miss%s\n"); 364 p(mrts_upcalls, "\t%ju upcall%s to multicast routing daemon\n"); 365 p(mrts_upq_ovflw, "\t%ju upcall queue overflow%s\n"); 366 p(mrts_upq_sockfull, 367 "\t%ju upcall%s dropped due to full socket buffer\n"); 368 p(mrts_cache_cleanups, "\t%ju cache cleanup%s\n"); 369 p(mrts_no_route, "\t%ju datagram%s with no route for origin\n"); 370 p(mrts_bad_tunnel, "\t%ju datagram%s arrived with bad tunneling\n"); 371 p(mrts_cant_tunnel, "\t%ju datagram%s could not be tunneled\n"); 372 p(mrts_wrong_if, "\t%ju datagram%s arrived on wrong interface\n"); 373 p(mrts_drop_sel, "\t%ju datagram%s selectively dropped\n"); 374 p(mrts_q_overflow, "\t%ju datagram%s dropped due to queue overflow\n"); 375 p(mrts_pkt2large, "\t%ju datagram%s dropped for being too large\n"); 376 377 #undef p2 378 #undef p 379 } 380