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