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