1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1989 Stephen Deering 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Stephen Deering of Stanford University. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)mroute.c 8.2 (Berkeley) 4/28/95 40 */ 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 /* 46 * Print multicast routing structures and statistics. 47 * 48 * MROUTING 1.0 49 */ 50 51 #include <sys/param.h> 52 #include <sys/queue.h> 53 #include <sys/socket.h> 54 #include <sys/socketvar.h> 55 #include <sys/sysctl.h> 56 #include <sys/protosw.h> 57 #include <sys/mbuf.h> 58 #include <sys/time.h> 59 60 #include <net/if.h> 61 #include <netinet/in.h> 62 #include <netinet/igmp.h> 63 #include <net/route.h> 64 65 #define _NETSTAT 1 66 #include <netinet/ip_mroute.h> 67 #undef _NETSTAT_ 68 69 #include <err.h> 70 #include <stdint.h> 71 #include <stdio.h> 72 #include <stdlib.h> 73 #include <stdbool.h> 74 #include <string.h> 75 #include <libxo/xo.h> 76 #include "netstat.h" 77 #include "nl_defs.h" 78 79 static void print_bw_meter(struct bw_meter *, int *); 80 static void print_mfc(struct mfc *, int, int *); 81 82 static void 83 print_bw_meter(struct bw_meter *bw_meter, int *banner_printed) 84 { 85 char s1[256], s2[256], s3[256]; 86 struct timeval now, end, delta; 87 88 gettimeofday(&now, NULL); 89 90 if (! *banner_printed) { 91 xo_open_list("bandwidth-meter"); 92 xo_emit(" {T:Bandwidth Meters}\n"); 93 xo_emit(" {T:/%-30s}", "Measured(Start|Packets|Bytes)"); 94 xo_emit(" {T:/%s}", "Type"); 95 xo_emit(" {T:/%-30s}", "Thresh(Interval|Packets|Bytes)"); 96 xo_emit(" {T:Remain}"); 97 xo_emit("\n"); 98 *banner_printed = 1; 99 } 100 101 xo_open_instance("bandwidth-meter"); 102 103 /* The measured values */ 104 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) { 105 snprintf(s1, sizeof(s1), "%ju", 106 (uintmax_t)bw_meter->bm_measured.b_packets); 107 xo_emit("{e:measured-packets/%ju}", 108 (uintmax_t)bw_meter->bm_measured.b_packets); 109 } else 110 strcpy(s1, "?"); 111 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) { 112 snprintf(s2, sizeof(s2), "%ju", 113 (uintmax_t)bw_meter->bm_measured.b_bytes); 114 xo_emit("{e:measured-bytes/%ju}", 115 (uintmax_t)bw_meter->bm_measured.b_bytes); 116 } else 117 strcpy(s2, "?"); 118 xo_emit(" {[:-30}{:start-time/%lu.%06lu}|{q:measured-packets/%s}" 119 "|{q:measured-bytes%s}{]:}", 120 (u_long)bw_meter->bm_start_time.tv_sec, 121 (u_long)bw_meter->bm_start_time.tv_usec, s1, s2); 122 123 /* The type of entry */ 124 xo_emit(" {t:type/%-3s}", (bw_meter->bm_flags & BW_METER_GEQ) ? ">=" : 125 (bw_meter->bm_flags & BW_METER_LEQ) ? "<=" : "?"); 126 127 /* The threshold values */ 128 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) { 129 snprintf(s1, sizeof(s1), "%ju", 130 (uintmax_t)bw_meter->bm_threshold.b_packets); 131 xo_emit("{e:threshold-packets/%ju}", 132 (uintmax_t)bw_meter->bm_threshold.b_packets); 133 } else 134 strcpy(s1, "?"); 135 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) { 136 snprintf(s2, sizeof(s2), "%ju", 137 (uintmax_t)bw_meter->bm_threshold.b_bytes); 138 xo_emit("{e:threshold-bytes/%ju}", 139 (uintmax_t)bw_meter->bm_threshold.b_bytes); 140 } else 141 strcpy(s2, "?"); 142 143 xo_emit(" {[:-30}{:threshold-time/%lu.%06lu}|{q:threshold-packets/%s}" 144 "|{q:threshold-bytes%s}{]:}", 145 (u_long)bw_meter->bm_threshold.b_time.tv_sec, 146 (u_long)bw_meter->bm_threshold.b_time.tv_usec, s1, s2); 147 148 /* Remaining time */ 149 timeradd(&bw_meter->bm_start_time, 150 &bw_meter->bm_threshold.b_time, &end); 151 if (timercmp(&now, &end, <=)) { 152 timersub(&end, &now, &delta); 153 snprintf(s3, sizeof(s3), "%lu.%06lu", 154 (u_long)delta.tv_sec, 155 (u_long)delta.tv_usec); 156 } else { 157 /* Negative time */ 158 timersub(&now, &end, &delta); 159 snprintf(s3, sizeof(s3), "-%lu.06%lu", 160 (u_long)delta.tv_sec, 161 (u_long)delta.tv_usec); 162 } 163 xo_emit(" {:remaining-time/%s}", s3); 164 165 xo_open_instance("bandwidth-meter"); 166 167 xo_emit("\n"); 168 } 169 170 static void 171 print_mfc(struct mfc *m, int maxvif, int *banner_printed) 172 { 173 struct sockaddr_in sin; 174 struct sockaddr *sa = (struct sockaddr *)&sin; 175 struct bw_meter bw_meter, *bwm; 176 int bw_banner_printed; 177 int error; 178 vifi_t vifi; 179 180 bw_banner_printed = 0; 181 memset(&sin, 0, sizeof(sin)); 182 sin.sin_len = sizeof(sin); 183 sin.sin_family = AF_INET; 184 185 if (! *banner_printed) { 186 xo_open_list("multicast-forwarding-entry"); 187 xo_emit("\n{T:IPv4 Multicast Forwarding Table}\n" 188 " {T:Origin} {T:Group} " 189 " {T:Packets In-Vif} {T:Out-Vifs:Ttls}\n"); 190 *banner_printed = 1; 191 } 192 193 memcpy(&sin.sin_addr, &m->mfc_origin, sizeof(sin.sin_addr)); 194 xo_emit(" {:origin-address/%-15.15s}", routename(sa, numeric_addr)); 195 memcpy(&sin.sin_addr, &m->mfc_mcastgrp, sizeof(sin.sin_addr)); 196 xo_emit(" {:group-address/%-15.15s}", 197 routename(sa, numeric_addr)); 198 xo_emit(" {:sent-packets/%9lu}", m->mfc_pkt_cnt); 199 xo_emit(" {:parent/%3d} ", m->mfc_parent); 200 xo_open_list("vif-ttl"); 201 for (vifi = 0; vifi <= maxvif; vifi++) { 202 if (m->mfc_ttls[vifi] > 0) { 203 xo_open_instance("vif-ttl"); 204 xo_emit(" {k:vif/%u}:{:ttl/%u}", vifi, 205 m->mfc_ttls[vifi]); 206 xo_close_instance("vif-ttl"); 207 } 208 } 209 xo_close_list("vif-ttl"); 210 xo_emit("\n"); 211 212 /* 213 * XXX We break the rules and try to use KVM to read the 214 * bandwidth meters, they are not retrievable via sysctl yet. 215 */ 216 bwm = m->mfc_bw_meter_leq; 217 while (bwm != NULL) { 218 error = kread((u_long)bwm, (char *)&bw_meter, 219 sizeof(bw_meter)); 220 if (error) 221 break; 222 print_bw_meter(&bw_meter, &bw_banner_printed); 223 bwm = bw_meter.bm_mfc_next; 224 } 225 bwm = m->mfc_bw_meter_geq; 226 while (bwm != NULL) { 227 error = kread((u_long)bwm, (char *)&bw_meter, 228 sizeof(bw_meter)); 229 if (error) 230 break; 231 print_bw_meter(&bw_meter, &bw_banner_printed); 232 bwm = bw_meter.bm_mfc_next; 233 } 234 if (banner_printed) 235 xo_close_list("bandwidth-meter"); 236 } 237 238 void 239 mroutepr(void) 240 { 241 struct sockaddr_in sin; 242 struct sockaddr *sa = (struct sockaddr *)&sin; 243 struct vif viftable[MAXVIFS]; 244 struct vif *v; 245 struct mfc *m; 246 u_long pmfchashtbl, pmfctablesize, pviftbl; 247 int banner_printed; 248 int saved_numeric_addr; 249 size_t len; 250 vifi_t vifi, maxvif; 251 252 saved_numeric_addr = numeric_addr; 253 numeric_addr = 1; 254 255 memset(&sin, 0, sizeof(sin)); 256 sin.sin_len = sizeof(sin); 257 sin.sin_family = AF_INET; 258 259 /* 260 * TODO: 261 * The VIF table will move to hanging off the struct if_info for 262 * each IPv4 configured interface. Currently it is statically 263 * allocated, and retrieved either using KVM or an opaque SYSCTL. 264 * 265 * This can't happen until the API documented in multicast(4) 266 * is itself refactored. The historical reason why VIFs use 267 * a separate ifindex space is entirely due to the legacy 268 * capability of the MROUTING code to create IPIP tunnels on 269 * the fly to support DVMRP. When gif(4) became available, this 270 * functionality was deprecated, as PIM does not use it. 271 */ 272 maxvif = 0; 273 pmfchashtbl = pmfctablesize = pviftbl = 0; 274 275 len = sizeof(viftable); 276 if (live) { 277 if (sysctlbyname("net.inet.ip.viftable", viftable, &len, NULL, 278 0) < 0) { 279 xo_warn("sysctl: net.inet.ip.viftable"); 280 return; 281 } 282 } else { 283 pmfchashtbl = nl[N_MFCHASHTBL].n_value; 284 pmfctablesize = nl[N_MFCTABLESIZE].n_value; 285 pviftbl = nl[N_VIFTABLE].n_value; 286 287 if (pmfchashtbl == 0 || pmfctablesize == 0 || pviftbl == 0) { 288 xo_warnx("No IPv4 MROUTING kernel support."); 289 return; 290 } 291 292 kread(pviftbl, (char *)viftable, sizeof(viftable)); 293 } 294 295 banner_printed = 0; 296 for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) { 297 if (v->v_lcl_addr.s_addr == 0) 298 continue; 299 300 maxvif = vifi; 301 if (!banner_printed) { 302 xo_emit("\n{T:IPv4 Virtual Interface Table\n" 303 " Vif Thresh Local-Address " 304 "Remote-Address Pkts-In Pkts-Out}\n"); 305 banner_printed = 1; 306 xo_open_list("vif"); 307 } 308 309 xo_open_instance("vif"); 310 memcpy(&sin.sin_addr, &v->v_lcl_addr, sizeof(sin.sin_addr)); 311 xo_emit(" {:vif/%2u} {:threshold/%6u} {:route/%-15.15s}", 312 /* opposite math of add_vif() */ 313 vifi, v->v_threshold, 314 routename(sa, numeric_addr)); 315 memcpy(&sin.sin_addr, &v->v_rmt_addr, sizeof(sin.sin_addr)); 316 xo_emit(" {:source/%-15.15s}", (v->v_flags & VIFF_TUNNEL) ? 317 routename(sa, numeric_addr) : ""); 318 319 xo_emit(" {:received-packets/%9lu} {:sent-packets/%9lu}\n", 320 v->v_pkt_in, v->v_pkt_out); 321 xo_close_instance("vif"); 322 } 323 if (banner_printed) 324 xo_close_list("vif"); 325 else 326 xo_emit("\n{T:IPv4 Virtual Interface Table is empty}\n"); 327 328 banner_printed = 0; 329 330 /* 331 * TODO: 332 * The MFC table will move into the AF_INET radix trie in future. 333 * In 8.x, it becomes a dynamically allocated structure referenced 334 * by a hashed LIST, allowing more than 256 entries w/o kernel tuning. 335 * 336 * If retrieved via opaque SYSCTL, the kernel will coalesce it into 337 * a static table for us. 338 * If retrieved via KVM, the hash list pointers must be followed. 339 */ 340 if (live) { 341 struct mfc *mfctable; 342 343 len = 0; 344 if (sysctlbyname("net.inet.ip.mfctable", NULL, &len, NULL, 345 0) < 0) { 346 xo_warn("sysctl: net.inet.ip.mfctable"); 347 return; 348 } 349 350 mfctable = malloc(len); 351 if (mfctable == NULL) { 352 xo_warnx("malloc %lu bytes", (u_long)len); 353 return; 354 } 355 if (sysctlbyname("net.inet.ip.mfctable", mfctable, &len, NULL, 356 0) < 0) { 357 free(mfctable); 358 xo_warn("sysctl: net.inet.ip.mfctable"); 359 return; 360 } 361 362 m = mfctable; 363 while (len >= sizeof(*m)) { 364 print_mfc(m++, maxvif, &banner_printed); 365 len -= sizeof(*m); 366 } 367 if (banner_printed) 368 xo_close_list("multicast-forwarding-entry"); 369 if (len != 0) 370 xo_warnx("print_mfc: %lu trailing bytes", (u_long)len); 371 372 free(mfctable); 373 } else { 374 LIST_HEAD(, mfc) *mfchashtbl; 375 u_long i, mfctablesize; 376 struct mfc mfc; 377 int error; 378 379 error = kread(pmfctablesize, (char *)&mfctablesize, 380 sizeof(u_long)); 381 if (error) { 382 xo_warn("kread: mfctablesize"); 383 return; 384 } 385 386 len = sizeof(*mfchashtbl) * mfctablesize; 387 mfchashtbl = malloc(len); 388 if (mfchashtbl == NULL) { 389 xo_warnx("malloc %lu bytes", (u_long)len); 390 return; 391 } 392 kread(pmfchashtbl, (char *)&mfchashtbl, len); 393 394 for (i = 0; i < mfctablesize; i++) { 395 LIST_FOREACH(m, &mfchashtbl[i], mfc_hash) { 396 kread((u_long)m, (char *)&mfc, sizeof(mfc)); 397 print_mfc(m, maxvif, &banner_printed); 398 } 399 } 400 if (banner_printed) 401 xo_close_list("multicast-forwarding-entry"); 402 403 free(mfchashtbl); 404 } 405 406 if (!banner_printed) 407 xo_emit("\n{T:IPv4 Multicast Forwarding Table is empty}\n"); 408 409 xo_emit("\n"); 410 numeric_addr = saved_numeric_addr; 411 } 412 413 void 414 mrt_stats(void) 415 { 416 struct mrtstat mrtstat; 417 u_long mstaddr; 418 419 mstaddr = nl[N_MRTSTAT].n_value; 420 421 if (fetch_stats("net.inet.ip.mrtstat", mstaddr, &mrtstat, 422 sizeof(mrtstat), kread_counters) != 0) { 423 if ((live && errno == ENOENT) || (!live && mstaddr == 0)) 424 fprintf(stderr, "No IPv4 MROUTING kernel support.\n"); 425 return; 426 } 427 428 xo_emit("{T:IPv4 multicast forwarding}:\n"); 429 430 #define p(f, m) if (mrtstat.f || sflag <= 1) \ 431 xo_emit(m, (uintmax_t)mrtstat.f, plural(mrtstat.f)) 432 #define p2(f, m) if (mrtstat.f || sflag <= 1) \ 433 xo_emit(m, (uintmax_t)mrtstat.f, plurales(mrtstat.f)) 434 435 xo_open_container("multicast-statistics"); 436 437 p(mrts_mfc_lookups, "\t{:cache-lookups/%ju} " 438 "{N:/multicast forwarding cache lookup%s}\n"); 439 p2(mrts_mfc_misses, "\t{:cache-misses/%ju} " 440 "{N:/multicast forwarding cache miss%s}\n"); 441 p(mrts_upcalls, "\t{:upcalls-total/%ju} " 442 "{N:/upcall%s to multicast routing daemon}\n"); 443 p(mrts_upq_ovflw, "\t{:upcall-overflows/%ju} " 444 "{N:/upcall queue overflow%s}\n"); 445 p(mrts_upq_sockfull, 446 "\t{:upcalls-dropped-full-buffer/%ju} " 447 "{N:/upcall%s dropped due to full socket buffer}\n"); 448 p(mrts_cache_cleanups, "\t{:cache-cleanups/%ju} " 449 "{N:/cache cleanup%s}\n"); 450 p(mrts_no_route, "\t{:dropped-no-origin/%ju} " 451 "{N:/datagram%s with no route for origin}\n"); 452 p(mrts_bad_tunnel, "\t{:dropped-bad-tunnel/%ju} " 453 "{N:/datagram%s arrived with bad tunneling}\n"); 454 p(mrts_cant_tunnel, "\t{:dropped-could-not-tunnel/%ju} " 455 "{N:/datagram%s could not be tunneled}\n"); 456 p(mrts_wrong_if, "\t{:dropped-wrong-incoming-interface/%ju} " 457 "{N:/datagram%s arrived on wrong interface}\n"); 458 p(mrts_drop_sel, "\t{:dropped-selectively/%ju} " 459 "{N:/datagram%s selectively dropped}\n"); 460 p(mrts_q_overflow, "\t{:dropped-queue-overflow/%ju} " 461 "{N:/datagram%s dropped due to queue overflow}\n"); 462 p(mrts_pkt2large, "\t{:dropped-too-large/%ju} " 463 "{N:/datagram%s dropped for being too large}\n"); 464 465 #undef p2 466 #undef p 467 } 468