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