1 /* $NetBSD: main.c,v 1.48 2004/10/30 20:56:20 dsl Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1988, 1993 5 * Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\n\ 35 Regents of the University of California. All rights reserved.\n"); 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "from: @(#)main.c 8.4 (Berkeley) 3/1/94"; 41 #else 42 __RCSID("$NetBSD: main.c,v 1.48 2004/10/30 20:56:20 dsl Exp $"); 43 #endif 44 #endif /* not lint */ 45 46 #include <sys/param.h> 47 #include <sys/file.h> 48 #include <sys/protosw.h> 49 #include <sys/socket.h> 50 51 #include <net/if.h> 52 #include <netinet/in.h> 53 54 #include <ctype.h> 55 #include <err.h> 56 #include <errno.h> 57 #include <kvm.h> 58 #include <limits.h> 59 #include <netdb.h> 60 #include <nlist.h> 61 #include <paths.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <unistd.h> 66 #include "netstat.h" 67 68 struct nlist nl[] = { 69 #define N_MBSTAT 0 70 { "_mbstat" }, 71 #define N_IPSTAT 1 72 { "_ipstat" }, 73 #define N_TCBTABLE 2 74 { "_tcbtable" }, 75 #define N_TCPSTAT 3 76 { "_tcpstat" }, 77 #define N_UDBTABLE 4 78 { "_udbtable" }, 79 #define N_UDPSTAT 5 80 { "_udpstat" }, 81 #define N_IFNET 6 82 { "_ifnet" }, 83 #define N_IMP 7 84 { "_imp_softc" }, 85 #define N_ICMPSTAT 8 86 { "_icmpstat" }, 87 #define N_RTSTAT 9 88 { "_rtstat" }, 89 #define N_UNIXSW 10 90 { "_unixsw" }, 91 #define N_IDP 11 92 { "_nspcb"}, 93 #define N_IDPSTAT 12 94 { "_idpstat"}, 95 #define N_SPPSTAT 13 96 { "_spp_istat"}, 97 #define N_NSERR 14 98 { "_ns_errstat"}, 99 #define N_CLNPSTAT 15 100 { "_clnp_stat"}, 101 #define IN_NOTUSED 16 102 { "_tp_inpcb" }, 103 #define ISO_TP 17 104 { "_tp_refinfo" }, 105 #define N_TPSTAT 18 106 { "_tp_stat" }, 107 #define N_ESISSTAT 19 108 { "_esis_stat"}, 109 #define N_NIMP 20 110 { "_nimp"}, 111 #define N_RTREE 21 112 { "_rt_tables"}, 113 #define N_CLTP 22 114 { "_cltb"}, 115 #define N_CLTPSTAT 23 116 { "_cltpstat"}, 117 #define N_NFILE 24 118 { "_nfile" }, 119 #define N_FILE 25 120 { "_file" }, 121 #define N_IGMPSTAT 26 122 { "_igmpstat" }, 123 #define N_MRTPROTO 27 124 { "_ip_mrtproto" }, 125 #define N_MRTSTAT 28 126 { "_mrtstat" }, 127 #define N_MFCHASHTBL 29 128 { "_mfchashtbl" }, 129 #define N_MFCHASH 30 130 { "_mfchash" }, 131 #define N_VIFTABLE 31 132 { "_viftable" }, 133 #define N_MSIZE 32 134 { "_msize" }, 135 #define N_MCLBYTES 33 136 { "_mclbytes" }, 137 #define N_DDPSTAT 34 138 { "_ddpstat"}, 139 #define N_DDPCB 35 140 { "_ddpcb"}, 141 #define N_MBPOOL 36 142 { "_mbpool" }, 143 #define N_MCLPOOL 37 144 { "_mclpool" }, 145 #define N_DIVPCB 38 146 { "_divcb"}, 147 #define N_DIVSTAT 39 148 { "_divstat"}, 149 #define N_IP6STAT 40 150 { "_ip6stat" }, 151 #define N_TCP6STAT 41 152 { "_tcp6stat" }, 153 #define N_UDP6STAT 42 154 { "_udp6stat" }, 155 #define N_ICMP6STAT 43 156 { "_icmp6stat" }, 157 #define N_IPSECSTAT 44 158 { "_ipsecstat" }, 159 #define N_IPSEC6STAT 45 160 { "_ipsec6stat" }, 161 #define N_PIM6STAT 46 162 { "_pim6stat" }, 163 #define N_MRT6PROTO 47 164 { "_ip6_mrtproto" }, 165 #define N_MRT6STAT 48 166 { "_mrt6stat" }, 167 #define N_MF6CTABLE 49 168 { "_mf6ctable" }, 169 #define N_MIF6TABLE 50 170 { "_mif6table" }, 171 #define N_PFKEYSTAT 51 172 { "_pfkeystat" }, 173 #define N_ARPSTAT 52 174 { "_arpstat" }, 175 #define N_RIP6STAT 53 176 { "_rip6stat" }, 177 #define N_ARPINTRQ 54 178 { "_arpintrq" }, 179 #define N_IPINTRQ 55 180 { "_ipintrq" }, 181 #define N_IP6INTRQ 56 182 { "_ip6intrq" }, 183 #define N_ATINTRQ1 57 184 { "_atintrq1" }, 185 #define N_ATINTRQ2 58 186 { "_atintrq2" }, 187 #define N_NSINTRQ 59 188 { "_nsintrq" }, 189 #define N_CLNLINTRQ 60 190 { "_clnlintrq" }, 191 #define N_LLCINTRQ 61 192 { "_llcintrq" }, 193 #define N_HDINTRQ 62 194 { "_hdintrq" }, 195 #define N_NATMINTRQ 63 196 { "_natmintrq" }, 197 #define N_PPPOEDISCINQ 64 198 { "_ppoediscinq" }, 199 #define N_PPPOEINQ 65 200 { "_ppoeinq" }, 201 #define N_PKINTRQ 66 202 { "_pkintrq" }, 203 #define N_HARDCLOCK_TICKS 67 204 { "_hardclock_ticks" }, 205 #define N_PIMSTAT 68 206 { "_pimstat" }, 207 { "" }, 208 }; 209 210 struct protox { 211 u_char pr_index; /* index into nlist of cb head */ 212 u_char pr_sindex; /* index into nlist of stat block */ 213 u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 214 void (*pr_cblocks) /* control blocks printing routine */ 215 __P((u_long, char *)); 216 void (*pr_stats) /* statistics printing routine */ 217 __P((u_long, char *)); 218 void (*pr_istats) 219 __P((char *)); /* per/if statistics printing routine */ 220 void (*pr_dump) /* PCB state dump routine */ 221 __P((u_long)); 222 char *pr_name; /* well-known name */ 223 } protox[] = { 224 { N_TCBTABLE, N_TCPSTAT, 1, protopr, 225 tcp_stats, NULL, tcp_dump, "tcp" }, 226 { N_UDBTABLE, N_UDPSTAT, 1, protopr, 227 udp_stats, NULL, 0, "udp" }, 228 { -1, N_IPSTAT, 1, 0, 229 ip_stats, NULL, 0, "ip" }, 230 { -1, N_ICMPSTAT, 1, 0, 231 icmp_stats, NULL, 0, "icmp" }, 232 { -1, N_IGMPSTAT, 1, 0, 233 igmp_stats, NULL, 0, "igmp" }, 234 #ifdef IPSEC 235 { -1, N_IPSECSTAT, 1, 0, 236 ipsec_switch, NULL, 0, "ipsec" }, 237 #endif 238 { -1, N_PIMSTAT, 1, 0, 239 pim_stats, NULL, 0, "pim" }, 240 { -1, -1, 0, 0, 241 0, NULL, 0, 0 } 242 }; 243 244 #ifdef INET6 245 struct protox ip6protox[] = { 246 { -1, N_IP6STAT, 1, 0, 247 ip6_stats, ip6_ifstats, 0, "ip6" }, 248 { -1, N_ICMP6STAT, 1, 0, 249 icmp6_stats, icmp6_ifstats, 0, "icmp6" }, 250 #ifdef TCP6 251 { N_TCBTABLE, N_TCP6STAT, 1, ip6protopr, 252 tcp6_stats, NULL, tcp6_dump, "tcp6" }, 253 #else 254 { N_TCBTABLE, N_TCP6STAT, 1, ip6protopr, 255 tcp_stats, NULL, tcp_dump, "tcp6" }, 256 #endif 257 { N_UDBTABLE, N_UDP6STAT, 1, ip6protopr, 258 udp6_stats, NULL, 0, "udp6" }, 259 #ifdef IPSEC 260 { -1, N_IPSEC6STAT, 1, 0, 261 ipsec_switch, NULL, 0, "ipsec6" }, 262 #endif 263 { -1, N_PIM6STAT, 1, 0, 264 pim6_stats, NULL, 0, "pim6" }, 265 { -1, N_RIP6STAT, 1, 0, 266 rip6_stats, NULL, 0, "rip6" }, 267 { -1, -1, 0, 0, 268 0, NULL, 0, 0 } 269 }; 270 #endif 271 272 struct protox arpprotox[] = { 273 { -1, N_ARPSTAT, 1, 0, 274 arp_stats, NULL, 0, "arp" }, 275 { -1, -1, 0, 0, 276 0, NULL, 0, 0 } 277 }; 278 279 #ifdef IPSEC 280 struct protox pfkeyprotox[] = { 281 { -1, N_PFKEYSTAT, 1, 0, 282 pfkey_stats, NULL, 0, "pfkey" }, 283 { -1, -1, 0, 0, 284 0, NULL, 0, 0 } 285 }; 286 #endif 287 288 #ifndef SMALL 289 struct protox atalkprotox[] = { 290 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr, 291 ddp_stats, NULL, 0, "ddp" }, 292 { -1, -1, 0, 0, 293 0, NULL, 0 } 294 }; 295 296 struct protox nsprotox[] = { 297 { N_IDP, N_IDPSTAT, 1, nsprotopr, 298 idp_stats, NULL, 0, "idp" }, 299 { N_IDP, N_SPPSTAT, 1, nsprotopr, 300 spp_stats, NULL, 0, "spp" }, 301 { -1, N_NSERR, 1, 0, 302 nserr_stats, NULL, 0, "ns_err" }, 303 { -1, -1, 0, 0, 304 0, NULL, 0 } 305 }; 306 307 struct protox isoprotox[] = { 308 { ISO_TP, N_TPSTAT, 1, iso_protopr, 309 tp_stats, NULL, 0, "tp" }, 310 { N_CLTP, N_CLTPSTAT, 1, iso_protopr, 311 cltp_stats, NULL, 0, "cltp" }, 312 { -1, N_CLNPSTAT, 1, 0, 313 clnp_stats, NULL, 0, "clnp"}, 314 { -1, N_ESISSTAT, 1, 0, 315 esis_stats, NULL, 0, "esis"}, 316 { -1, -1, 0, 0, 317 0, NULL, 0, 0 } 318 }; 319 #endif 320 321 struct protox *protoprotox[] = { protox, 322 #ifdef INET6 323 ip6protox, 324 #endif 325 arpprotox, 326 #ifdef IPSEC 327 pfkeyprotox, 328 #endif 329 #ifndef SMALL 330 atalkprotox, nsprotox, isoprotox, 331 #endif 332 NULL }; 333 334 const struct softintrq { 335 const char *siq_name; 336 int siq_index; 337 } softintrq[] = { 338 { "arpintrq", N_ARPINTRQ }, 339 { "ipintrq", N_IPINTRQ }, 340 { "ip6intrq", N_IP6INTRQ }, 341 { "atintrq1", N_ATINTRQ1 }, 342 { "atintrq2", N_ATINTRQ2 }, 343 { "nsintrq", N_NSINTRQ }, 344 { "clnlintrq", N_CLNLINTRQ }, 345 { "llcintrq", N_LLCINTRQ }, 346 { "hdintrq", N_HDINTRQ }, 347 { "natmintrq", N_NATMINTRQ }, 348 { "ppoediscinq", N_PPPOEDISCINQ }, 349 { "ppoeinq", N_PPPOEINQ }, 350 { "pkintrq", N_PKINTRQ }, 351 { NULL, -1 }, 352 }; 353 354 int main __P((int, char *[])); 355 static void printproto __P((struct protox *, char *)); 356 static void print_softintrq __P((void)); 357 static void usage __P((void)); 358 static struct protox *name2protox __P((char *)); 359 static struct protox *knownname __P((char *)); 360 361 kvm_t *kvmd; 362 363 int 364 main(argc, argv) 365 int argc; 366 char *argv[]; 367 { 368 struct protoent *p; 369 struct protox *tp; /* for printing cblocks & stats */ 370 int ch; 371 char *nlistf = NULL, *memf = NULL; 372 char buf[_POSIX2_LINE_MAX], *cp; 373 u_long pcbaddr; 374 gid_t egid = getegid(); 375 376 (void)setegid(getgid()); 377 tp = NULL; 378 af = AF_UNSPEC; 379 pcbaddr = 0; 380 381 while ((ch = getopt(argc, argv, 382 "Aabdf:ghI:LliM:mN:nP:p:qrsStuvw:")) != -1) 383 switch (ch) { 384 case 'A': 385 Aflag = 1; 386 break; 387 case 'a': 388 aflag = 1; 389 break; 390 case 'b': 391 bflag = 1; 392 break; 393 case 'd': 394 dflag = 1; 395 break; 396 case 'f': 397 if (strcmp(optarg, "ns") == 0) 398 af = AF_NS; 399 else if (strcmp(optarg, "inet") == 0) 400 af = AF_INET; 401 else if (strcmp(optarg, "inet6") == 0) 402 af = AF_INET6; 403 else if (strcmp(optarg, "arp") == 0) 404 af = AF_ARP; 405 else if (strcmp(optarg, "pfkey") == 0) 406 af = PF_KEY; 407 else if (strcmp(optarg, "unix") == 0 408 || strcmp(optarg, "local") == 0) 409 af = AF_LOCAL; 410 else if (strcmp(optarg, "iso") == 0) 411 af = AF_ISO; 412 else if (strcmp(optarg, "atalk") == 0) 413 af = AF_APPLETALK; 414 else 415 errx(1, "%s: unknown address family", 416 optarg); 417 break; 418 #ifndef SMALL 419 case 'g': 420 gflag = 1; 421 break; 422 #endif 423 case 'I': 424 iflag = 1; 425 interface = optarg; 426 break; 427 case 'i': 428 iflag = 1; 429 break; 430 case 'L': 431 Lflag = 1; 432 break; 433 case 'l': 434 lflag = 1; 435 break; 436 case 'M': 437 memf = optarg; 438 break; 439 case 'm': 440 mflag = 1; 441 break; 442 case 'N': 443 nlistf = optarg; 444 break; 445 case 'n': 446 numeric_addr = numeric_port = 1; 447 break; 448 case 'P': 449 errno = 0; 450 pcbaddr = strtoul(optarg, &cp, 16); 451 if (*cp != '\0' || errno == ERANGE) 452 errx(1, "invalid PCB address %s", 453 optarg); 454 Pflag = 1; 455 break; 456 case 'p': 457 if ((tp = name2protox(optarg)) == NULL) 458 errx(1, "%s: unknown or uninstrumented protocol", 459 optarg); 460 pflag = 1; 461 break; 462 case 'q': 463 qflag = 1; 464 break; 465 case 'r': 466 rflag = 1; 467 break; 468 case 's': 469 ++sflag; 470 break; 471 case 'S': 472 numeric_addr = 1; 473 break; 474 case 't': 475 tflag = 1; 476 break; 477 case 'u': 478 af = AF_LOCAL; 479 break; 480 case 'v': 481 vflag++; 482 break; 483 case 'w': 484 interval = atoi(optarg); 485 iflag = 1; 486 break; 487 case '?': 488 default: 489 usage(); 490 } 491 argv += optind; 492 argc -= optind; 493 494 #define BACKWARD_COMPATIBILITY 495 #ifdef BACKWARD_COMPATIBILITY 496 if (*argv) { 497 if (isdigit((unsigned char)**argv)) { 498 interval = atoi(*argv); 499 if (interval <= 0) 500 usage(); 501 ++argv; 502 iflag = 1; 503 } 504 if (*argv) { 505 nlistf = *argv; 506 if (*++argv) 507 memf = *argv; 508 } 509 } 510 #endif 511 512 /* 513 * Discard setgid privileges. If not the running kernel, we toss 514 * them away totally so that bad guys can't print interesting stuff 515 * from kernel memory, otherwise switch back to kmem for the 516 * duration of the kvm_openfiles() call. 517 */ 518 if (nlistf != NULL || memf != NULL || Pflag) 519 (void)setgid(getgid()); 520 else 521 (void)setegid(egid); 522 523 use_sysctl = (nlistf == NULL && memf == NULL); 524 525 if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, 526 buf)) == NULL) 527 errx(1, "%s", buf); 528 529 /* do this now anyway */ 530 if (nlistf == NULL && memf == NULL) 531 (void)setgid(getgid()); 532 533 if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) { 534 if (nlistf) 535 errx(1, "%s: no namelist", nlistf); 536 else 537 errx(1, "no namelist"); 538 } 539 if (mflag) { 540 mbpr(nl[N_MBSTAT].n_value, nl[N_MSIZE].n_value, 541 nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value, 542 nl[N_MCLPOOL].n_value); 543 exit(0); 544 } 545 if (Pflag) { 546 if (tp == NULL) { 547 /* Default to TCP. */ 548 tp = name2protox("tcp"); 549 } 550 if (tp->pr_dump) 551 (*tp->pr_dump)(pcbaddr); 552 else 553 printf("%s: no PCB dump routine\n", tp->pr_name); 554 exit(0); 555 } 556 if (pflag) { 557 if (iflag && tp->pr_istats) 558 intpr(interval, nl[N_IFNET].n_value, tp->pr_istats); 559 else if (tp->pr_stats) 560 (*tp->pr_stats)(nl[tp->pr_sindex].n_value, 561 tp->pr_name); 562 else 563 printf("%s: no stats routine\n", tp->pr_name); 564 exit(0); 565 } 566 if (qflag) { 567 print_softintrq(); 568 exit(0); 569 } 570 /* 571 * Keep file descriptors open to avoid overhead 572 * of open/close on each call to get* routines. 573 */ 574 sethostent(1); 575 setnetent(1); 576 if (iflag) { 577 if (af != AF_UNSPEC) 578 goto protostat; 579 580 intpr(interval, nl[N_IFNET].n_value, NULL); 581 exit(0); 582 } 583 if (rflag) { 584 if (sflag) 585 rt_stats(nl[N_RTSTAT].n_value); 586 else 587 routepr(nl[N_RTREE].n_value); 588 exit(0); 589 } 590 #ifndef SMALL 591 if (gflag) { 592 if (sflag) { 593 if (af == AF_INET || af == AF_UNSPEC) 594 mrt_stats(nl[N_MRTPROTO].n_value, 595 nl[N_MRTSTAT].n_value); 596 #ifdef INET6 597 if (af == AF_INET6 || af == AF_UNSPEC) 598 mrt6_stats(nl[N_MRT6PROTO].n_value, 599 nl[N_MRT6STAT].n_value); 600 #endif 601 } 602 else { 603 if (af == AF_INET || af == AF_UNSPEC) 604 mroutepr(nl[N_MRTPROTO].n_value, 605 nl[N_MFCHASHTBL].n_value, 606 nl[N_MFCHASH].n_value, 607 nl[N_VIFTABLE].n_value); 608 #ifdef INET6 609 if (af == AF_INET6 || af == AF_UNSPEC) 610 mroute6pr(nl[N_MRT6PROTO].n_value, 611 nl[N_MF6CTABLE].n_value, 612 nl[N_MIF6TABLE].n_value); 613 #endif 614 } 615 exit(0); 616 } 617 #endif 618 protostat: 619 if (af == AF_INET || af == AF_UNSPEC) { 620 setprotoent(1); 621 setservent(1); 622 /* ugh, this is O(MN) ... why do we do this? */ 623 while ((p = getprotoent()) != NULL) { 624 for (tp = protox; tp->pr_name; tp++) 625 if (strcmp(tp->pr_name, p->p_name) == 0) 626 break; 627 if (tp->pr_name == 0 || tp->pr_wanted == 0) 628 continue; 629 printproto(tp, p->p_name); 630 tp->pr_wanted = 0; 631 } 632 endprotoent(); 633 for (tp = protox; tp->pr_name; tp++) 634 if (tp->pr_wanted) 635 printproto(tp, tp->pr_name); 636 } 637 #ifdef INET6 638 if (af == AF_INET6 || af == AF_UNSPEC) 639 for (tp = ip6protox; tp->pr_name; tp++) 640 printproto(tp, tp->pr_name); 641 #endif 642 if (af == AF_ARP || af == AF_UNSPEC) 643 for (tp = arpprotox; tp->pr_name; tp++) 644 printproto(tp, tp->pr_name); 645 #ifdef IPSEC 646 if (af == PF_KEY || af == AF_UNSPEC) 647 for (tp = pfkeyprotox; tp->pr_name; tp++) 648 printproto(tp, tp->pr_name); 649 #endif 650 #ifndef SMALL 651 if (af == AF_APPLETALK || af == AF_UNSPEC) 652 for (tp = atalkprotox; tp->pr_name; tp++) 653 printproto(tp, tp->pr_name); 654 if (af == AF_NS || af == AF_UNSPEC) 655 for (tp = nsprotox; tp->pr_name; tp++) 656 printproto(tp, tp->pr_name); 657 if (af == AF_ISO || af == AF_UNSPEC) 658 for (tp = isoprotox; tp->pr_name; tp++) 659 printproto(tp, tp->pr_name); 660 if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag) 661 unixpr(nl[N_UNIXSW].n_value); 662 #endif 663 exit(0); 664 } 665 666 /* 667 * Print out protocol statistics or control blocks (per sflag). 668 * If the interface was not specifically requested, and the symbol 669 * is not in the namelist, ignore this one. 670 */ 671 static void 672 printproto(tp, name) 673 struct protox *tp; 674 char *name; 675 { 676 void (*pr) __P((u_long, char *)); 677 u_long off; 678 679 if (sflag) { 680 if (iflag) { 681 if (tp->pr_istats) 682 intpr(interval, nl[N_IFNET].n_value, 683 tp->pr_istats); 684 return; 685 } 686 else { 687 pr = tp->pr_stats; 688 off = nl[tp->pr_sindex].n_value; 689 } 690 } else { 691 pr = tp->pr_cblocks; 692 off = nl[tp->pr_index].n_value; 693 } 694 if (pr != NULL && (off || af != AF_UNSPEC)) 695 (*pr)(off, name); 696 } 697 698 /* 699 * Print softintrq status. 700 */ 701 void 702 print_softintrq() 703 { 704 struct ifqueue intrq, *ifq = &intrq; 705 const struct softintrq *siq; 706 u_long off; 707 708 for (siq = softintrq; siq->siq_name != NULL; siq++) { 709 off = nl[siq->siq_index].n_value; 710 if (off == 0) 711 continue; 712 713 kread(off, (char *)ifq, sizeof(*ifq)); 714 printf("%s:\n", siq->siq_name); 715 printf("\tqueue length: %d\n", ifq->ifq_len); 716 printf("\tmaximum queue length: %d\n", ifq->ifq_maxlen); 717 printf("\tpackets dropped: %d\n", ifq->ifq_drops); 718 } 719 } 720 721 /* 722 * Read kernel memory, return 0 on success. 723 */ 724 int 725 kread(addr, buf, size) 726 u_long addr; 727 char *buf; 728 int size; 729 { 730 731 if (kvm_read(kvmd, addr, buf, size) != size) { 732 warnx("%s", kvm_geterr(kvmd)); 733 return (-1); 734 } 735 return (0); 736 } 737 738 char * 739 plural(n) 740 int n; 741 { 742 743 return (n != 1 ? "s" : ""); 744 } 745 746 char * 747 plurales(n) 748 int n; 749 { 750 751 return (n != 1 ? "es" : ""); 752 } 753 754 int 755 get_hardticks(void) 756 { 757 int hardticks; 758 759 kread(nl[N_HARDCLOCK_TICKS].n_value, (char *)&hardticks, 760 sizeof(hardticks)); 761 return (hardticks); 762 } 763 764 /* 765 * Find the protox for the given "well-known" name. 766 */ 767 static struct protox * 768 knownname(name) 769 char *name; 770 { 771 struct protox **tpp, *tp; 772 773 for (tpp = protoprotox; *tpp; tpp++) 774 for (tp = *tpp; tp->pr_name; tp++) 775 if (strcmp(tp->pr_name, name) == 0) 776 return (tp); 777 return (NULL); 778 } 779 780 /* 781 * Find the protox corresponding to name. 782 */ 783 static struct protox * 784 name2protox(name) 785 char *name; 786 { 787 struct protox *tp; 788 char **alias; /* alias from p->aliases */ 789 struct protoent *p; 790 791 /* 792 * Try to find the name in the list of "well-known" names. If that 793 * fails, check if name is an alias for an Internet protocol. 794 */ 795 if ((tp = knownname(name)) != NULL) 796 return (tp); 797 798 setprotoent(1); /* make protocol lookup cheaper */ 799 while ((p = getprotoent()) != NULL) { 800 /* assert: name not same as p->name */ 801 for (alias = p->p_aliases; *alias; alias++) 802 if (strcmp(name, *alias) == 0) { 803 endprotoent(); 804 return (knownname(p->p_name)); 805 } 806 } 807 endprotoent(); 808 return (NULL); 809 } 810 811 static void 812 usage() 813 { 814 const char *progname = getprogname(); 815 816 (void)fprintf(stderr, 817 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", progname); 818 (void)fprintf(stderr, 819 " %s [-bdgiLmnqrsSv] [-f address_family] [-M core] [-N system]\n", 820 progname); 821 (void)fprintf(stderr, 822 " %s [-dn] [-I interface] [-M core] [-N system] [-w wait]\n", progname); 823 (void)fprintf(stderr, 824 " %s [-p protocol] [-M core] [-N system]\n", progname); 825 (void)fprintf(stderr, 826 " %s [-p protocol] [-M core] [-N system] -P pcbaddr\n", progname); 827 (void)fprintf(stderr, 828 " %s [-p protocol] [-i] [-I Interface] \n", progname); 829 (void)fprintf(stderr, 830 " %s [-s] [-f address_family] [-i] [-I Interface]\n", progname); 831 exit(1); 832 } 833