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