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