main.c revision 16080
1/* 2 * Copyright (c) 1983, 1988, 1993 3 * Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35char copyright[] = 36"@(#) Copyright (c) 1983, 1988, 1993\n\ 37 Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94"; 42#endif /* not lint */ 43 44#include <sys/param.h> 45#include <sys/file.h> 46#include <sys/protosw.h> 47#include <sys/socket.h> 48 49#include <netinet/in.h> 50 51#include <ctype.h> 52#include <errno.h> 53#include <kvm.h> 54#include <limits.h> 55#include <netdb.h> 56#include <nlist.h> 57#include <paths.h> 58#include <stdio.h> 59#include <stdlib.h> 60#include <string.h> 61#include <unistd.h> 62#include <err.h> 63#include "netstat.h" 64 65struct nlist nl[] = { 66#define N_MBSTAT 0 67 { "_mbstat" }, 68#define N_IPSTAT 1 69 { "_ipstat" }, 70#define N_TCB 2 71 { "_tcb" }, 72#define N_TCPSTAT 3 73 { "_tcpstat" }, 74#define N_UDB 4 75 { "_udb" }, 76#define N_UDPSTAT 5 77 { "_udpstat" }, 78#define N_IFNET 6 79 { "_ifnet" }, 80#define N_IMP 7 81 { "_imp_softc" }, 82#define N_ICMPSTAT 8 83 { "_icmpstat" }, 84#define N_RTSTAT 9 85 { "_rtstat" }, 86#define N_UNIXSW 10 87 { "_localsw" }, 88#define N_IDP 11 89 { "_nspcb"}, 90#define N_IDPSTAT 12 91 { "_idpstat"}, 92#define N_SPPSTAT 13 93 { "_spp_istat"}, 94#define N_NSERR 14 95 { "_ns_errstat"}, 96#define N_CLNPSTAT 15 97 { "_clnp_stat"}, 98#define IN_NOTUSED 16 99 { "_tp_inpcb" }, 100#define ISO_TP 17 101 { "_tp_refinfo" }, 102#define N_TPSTAT 18 103 { "_tp_stat" }, 104#define N_ESISSTAT 19 105 { "_esis_stat"}, 106#define N_NIMP 20 107 { "_nimp"}, 108#define N_RTREE 21 109 { "_rt_tables"}, 110#define N_CLTP 22 111 { "_cltb"}, 112#define N_CLTPSTAT 23 113 { "_cltpstat"}, 114#define N_NFILE 24 115 { "_nfile" }, 116#define N_FILE 25 117 { "_file" }, 118#define N_IGMPSTAT 26 119 { "_igmpstat" }, 120#define N_MRTPROTO 27 121 { "_ip_mrtproto" }, 122#define N_MRTSTAT 28 123 { "_mrtstat" }, 124#define N_MFCTABLE 29 125 { "_mfctable" }, 126#define N_VIFTABLE 30 127 { "_viftable" }, 128#define N_IPX 31 129 { "_ipxpcb"}, 130#define N_IPXSTAT 32 131 { "_ipxstat"}, 132#define N_SPXSTAT 33 133 { "_spx_istat"}, 134#define N_IPXERR 34 135 { "_ipx_errstat"}, 136 { "" }, 137}; 138 139struct protox { 140 u_char pr_index; /* index into nlist of cb head */ 141 u_char pr_sindex; /* index into nlist of stat block */ 142 u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 143 void (*pr_cblocks)(); /* control blocks printing routine */ 144 void (*pr_stats)(); /* statistics printing routine */ 145 char *pr_name; /* well-known name */ 146} protox[] = { 147 { N_TCB, N_TCPSTAT, 1, protopr, 148 tcp_stats, "tcp" }, 149 { N_UDB, N_UDPSTAT, 1, protopr, 150 udp_stats, "udp" }, 151 { -1, N_IPSTAT, 1, 0, 152 ip_stats, "ip" }, 153 { -1, N_ICMPSTAT, 1, 0, 154 icmp_stats, "icmp" }, 155 { -1, N_IGMPSTAT, 1, 0, 156 igmp_stats, "igmp" }, 157 { -1, -1, 0, 0, 158 0, 0 } 159}; 160 161struct protox ipxprotox[] = { 162 { N_IPX, N_IPXSTAT, 1, ipxprotopr, 163 ipx_stats, "ipx" }, 164 { N_IPX, N_SPXSTAT, 1, ipxprotopr, 165 spx_stats, "spx" }, 166 { -1, N_IPXERR, 1, 0, 167 ipxerr_stats, "ipx_err" }, 168 { -1, -1, 0, 0, 169 0, 0 } 170}; 171 172#ifdef NS 173struct protox nsprotox[] = { 174 { N_IDP, N_IDPSTAT, 1, nsprotopr, 175 idp_stats, "idp" }, 176 { N_IDP, N_SPPSTAT, 1, nsprotopr, 177 spp_stats, "spp" }, 178 { -1, N_NSERR, 1, 0, 179 nserr_stats, "ns_err" }, 180 { -1, -1, 0, 0, 181 0, 0 } 182}; 183#endif 184 185#ifdef ISO 186struct protox isoprotox[] = { 187 { ISO_TP, N_TPSTAT, 1, iso_protopr, 188 tp_stats, "tp" }, 189 { N_CLTP, N_CLTPSTAT, 1, iso_protopr, 190 cltp_stats, "cltp" }, 191 { -1, N_CLNPSTAT, 1, 0, 192 clnp_stats, "clnp"}, 193 { -1, N_ESISSTAT, 1, 0, 194 esis_stats, "esis"}, 195 { -1, -1, 0, 0, 196 0, 0 } 197}; 198#endif 199 200struct protox *protoprotox[] = { protox, ipxprotox, 201#ifdef NS 202 nsprotox, 203#endif 204#ifdef ISO 205 isoprotox, 206#endif 207 NULL }; 208 209static void printproto __P((struct protox *, char *)); 210static void usage __P((void)); 211static struct protox *name2protox __P((char *)); 212static struct protox *knownname __P((char *)); 213 214kvm_t *kvmd; 215 216int 217main(argc, argv) 218 int argc; 219 char *argv[]; 220{ 221 extern char *optarg; 222 extern int optind; 223 register struct protoent *p; 224 register struct protox *tp; /* for printing cblocks & stats */ 225 register char *cp; 226 int ch; 227 char *nlistf = NULL, *memf = NULL; 228 char buf[_POSIX2_LINE_MAX]; 229 230 if ((cp = rindex(argv[0], '/'))) 231 prog = cp + 1; 232 else 233 prog = argv[0]; 234 af = AF_UNSPEC; 235 236 while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:np:rstuw:")) != EOF) 237 switch(ch) { 238 case 'A': 239 Aflag = 1; 240 break; 241 case 'a': 242 aflag = 1; 243 break; 244 case 'b': 245 bflag = 1; 246 break; 247 case 'd': 248 dflag = 1; 249 break; 250 case 'f': 251#ifdef NS 252 if (strcmp(optarg, "ns") == 0) 253 af = AF_NS; 254 else 255#endif 256 if (strcmp(optarg, "ipx") == 0) 257 af = AF_IPX; 258 else if (strcmp(optarg, "inet") == 0) 259 af = AF_INET; 260 else if (strcmp(optarg, "unix") == 0) 261 af = AF_UNIX; 262#ifdef ISO 263 else if (strcmp(optarg, "iso") == 0) 264 af = AF_ISO; 265#endif 266 else { 267 errx(1, "%s: unknown address family", optarg); 268 } 269 break; 270 case 'g': 271 gflag = 1; 272 break; 273 case 'I': { 274 char *cp; 275 276 iflag = 1; 277 for (cp = interface = optarg; isalpha(*cp); cp++) 278 continue; 279 unit = atoi(cp); 280 break; 281 } 282 case 'i': 283 iflag = 1; 284 break; 285 case 'M': 286 memf = optarg; 287 break; 288 case 'm': 289 mflag = 1; 290 break; 291 case 'N': 292 nlistf = optarg; 293 break; 294 case 'n': 295 nflag = 1; 296 break; 297 case 'p': 298 if ((tp = name2protox(optarg)) == NULL) { 299 errx(1, 300 "%s: unknown or uninstrumented protocol", 301 optarg); 302 } 303 pflag = 1; 304 break; 305 case 'r': 306 rflag = 1; 307 break; 308 case 's': 309 ++sflag; 310 break; 311 case 't': 312 tflag = 1; 313 break; 314 case 'u': 315 af = AF_UNIX; 316 break; 317 case 'w': 318 interval = atoi(optarg); 319 iflag = 1; 320 break; 321 case '?': 322 default: 323 usage(); 324 } 325 argv += optind; 326 argc -= optind; 327 328#define BACKWARD_COMPATIBILITY 329#ifdef BACKWARD_COMPATIBILITY 330 if (*argv) { 331 if (isdigit(**argv)) { 332 interval = atoi(*argv); 333 if (interval <= 0) 334 usage(); 335 ++argv; 336 iflag = 1; 337 } 338 if (*argv) { 339 nlistf = *argv; 340 if (*++argv) 341 memf = *argv; 342 } 343 } 344#endif 345 346 /* 347 * Discard setgid privileges if not the running kernel so that bad 348 * guys can't print interesting stuff from kernel memory. 349 */ 350 if (nlistf != NULL || memf != NULL) 351 setgid(getgid()); 352 353 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf); 354 if (kvmd == NULL) { 355 errx(1, "kvm_open: %s", buf); 356 } 357 if (kvm_nlist(kvmd, nl) < 0) { 358 if(nlistf) 359 errx(1, "%s: kvm_nlist: %s", nlistf, kvm_geterr(kvmd)); 360 else 361 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd)); 362 } 363 364 if (nl[0].n_type == 0) { 365 if(nlistf) 366 errx(1, "%s: no namelist", nlistf); 367 else 368 errx(1, "no namelist"); 369 } 370 if (mflag) { 371 mbpr(nl[N_MBSTAT].n_value); 372 exit(0); 373 } 374 if (pflag) { 375 if (tp->pr_stats) 376 (*tp->pr_stats)(nl[tp->pr_sindex].n_value, 377 tp->pr_name); 378 else 379 printf("%s: no stats routine\n", tp->pr_name); 380 exit(0); 381 } 382#if 0 383 /* 384 * Keep file descriptors open to avoid overhead 385 * of open/close on each call to get* routines. 386 */ 387 sethostent(1); 388 setnetent(1); 389#else 390 /* 391 * This does not make sense any more with DNS being default over 392 * the files. Doing a setXXXXent(1) causes a tcp connection to be 393 * used for the queries, which is slower. 394 */ 395#endif 396 if (iflag) { 397 intpr(interval, nl[N_IFNET].n_value); 398 exit(0); 399 } 400 if (rflag) { 401 if (sflag) 402 rt_stats(nl[N_RTSTAT].n_value); 403 else 404 routepr(nl[N_RTREE].n_value); 405 exit(0); 406 } 407 if (gflag) { 408 if (sflag) 409 mrt_stats(nl[N_MRTPROTO].n_value, 410 nl[N_MRTSTAT].n_value); 411 else 412 mroutepr(nl[N_MRTPROTO].n_value, 413 nl[N_MFCTABLE].n_value, 414 nl[N_VIFTABLE].n_value); 415 exit(0); 416 } 417 if (af == AF_INET || af == AF_UNSPEC) { 418 setprotoent(1); 419 setservent(1); 420 /* ugh, this is O(MN) ... why do we do this? */ 421 while ((p = getprotoent())) { 422 for (tp = protox; tp->pr_name; tp++) 423 if (strcmp(tp->pr_name, p->p_name) == 0) 424 break; 425 if (tp->pr_name == 0 || tp->pr_wanted == 0) 426 continue; 427 printproto(tp, p->p_name); 428 } 429 endprotoent(); 430 } 431 if (af == AF_IPX || af == AF_UNSPEC) 432 for (tp = ipxprotox; tp->pr_name; tp++) 433 printproto(tp, tp->pr_name); 434#ifdef NS 435 if (af == AF_NS || af == AF_UNSPEC) 436 for (tp = nsprotox; tp->pr_name; tp++) 437 printproto(tp, tp->pr_name); 438#endif 439#ifdef ISO 440 if (af == AF_ISO || af == AF_UNSPEC) 441 for (tp = isoprotox; tp->pr_name; tp++) 442 printproto(tp, tp->pr_name); 443#endif 444 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 445 unixpr(nl[N_UNIXSW].n_value); 446 exit(0); 447} 448 449/* 450 * Print out protocol statistics or control blocks (per sflag). 451 * If the interface was not specifically requested, and the symbol 452 * is not in the namelist, ignore this one. 453 */ 454static void 455printproto(tp, name) 456 register struct protox *tp; 457 char *name; 458{ 459 void (*pr)(); 460 u_long off; 461 462 if (sflag) { 463 pr = tp->pr_stats; 464 off = nl[tp->pr_sindex].n_value; 465 } else { 466 pr = tp->pr_cblocks; 467 off = nl[tp->pr_index].n_value; 468 } 469 if (pr != NULL && (off || af != AF_UNSPEC)) 470 (*pr)(off, name); 471} 472 473/* 474 * Read kernel memory, return 0 on success. 475 */ 476int 477kread(addr, buf, size) 478 u_long addr; 479 char *buf; 480 int size; 481{ 482 483 if (kvm_read(kvmd, addr, buf, size) != size) { 484 warnx("%s", kvm_geterr(kvmd)); 485 return (-1); 486 } 487 return (0); 488} 489 490char * 491plural(n) 492 int n; 493{ 494 return (n != 1 ? "s" : ""); 495} 496 497char * 498plurales(n) 499 int n; 500{ 501 return (n != 1 ? "es" : ""); 502} 503 504/* 505 * Find the protox for the given "well-known" name. 506 */ 507static struct protox * 508knownname(name) 509 char *name; 510{ 511 struct protox **tpp, *tp; 512 513 for (tpp = protoprotox; *tpp; tpp++) 514 for (tp = *tpp; tp->pr_name; tp++) 515 if (strcmp(tp->pr_name, name) == 0) 516 return (tp); 517 return (NULL); 518} 519 520/* 521 * Find the protox corresponding to name. 522 */ 523static struct protox * 524name2protox(name) 525 char *name; 526{ 527 struct protox *tp; 528 char **alias; /* alias from p->aliases */ 529 struct protoent *p; 530 531 /* 532 * Try to find the name in the list of "well-known" names. If that 533 * fails, check if name is an alias for an Internet protocol. 534 */ 535 if ((tp = knownname(name))) 536 return (tp); 537 538 setprotoent(1); /* make protocol lookup cheaper */ 539 while ((p = getprotoent())) { 540 /* assert: name not same as p->name */ 541 for (alias = p->p_aliases; *alias; alias++) 542 if (strcmp(name, *alias) == 0) { 543 endprotoent(); 544 return (knownname(p->p_name)); 545 } 546 } 547 endprotoent(); 548 return (NULL); 549} 550 551static void 552usage() 553{ 554 (void)fprintf(stderr, 555"usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", prog); 556 (void)fprintf(stderr, 557" %s [-bdghimnrs] [-f address_family] [-M core] [-N system]\n", prog); 558 (void)fprintf(stderr, 559" %s [-bdn] [-I interface] [-M core] [-N system] [-w wait]\n", prog); 560 (void)fprintf(stderr, 561" %s [-M core] [-N system] [-p protocol]\n", prog); 562 exit(1); 563} 564 565void 566trimdomain(cp) 567 char *cp; 568{ 569 static char domain[MAXHOSTNAMELEN + 1]; 570 static int first = 1; 571 char *s; 572 573 if (first) { 574 first = 0; 575 if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 576 (s = strchr(domain, '.'))) 577 (void) strcpy(domain, s + 1); 578 else 579 domain[0] = 0; 580 } 581 582 if (domain[0]) { 583 while ((cp = strchr(cp, '.'))) { 584 if (!strcasecmp(cp + 1, domain)) { 585 *cp = 0; /* hit it */ 586 break; 587 } else { 588 cp++; 589 } 590 } 591 } 592} 593 594