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