Home | History | Annotate | Line # | Download | only in netstat
main.c revision 1.20
      1 /*	$NetBSD: main.c,v 1.20 1999/02/08 11:34:36 explorer 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.20 1999/02/08 11:34:36 explorer 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 	{ "" },
    145 };
    146 
    147 struct protox {
    148 	u_char	pr_index;		/* index into nlist of cb head */
    149 	u_char	pr_sindex;		/* index into nlist of stat block */
    150 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
    151 	void	(*pr_cblocks)		/* control blocks printing routine */
    152 			__P((u_long, char *));
    153 	void	(*pr_stats)		/* statistics printing routine */
    154 			__P((u_long, char *));
    155 	void	(*pr_dump)		/* PCB state dump routine */
    156 			__P((u_long));
    157 	char	*pr_name;		/* well-known name */
    158 } protox[] = {
    159 	{ N_TCBTABLE,	N_TCPSTAT,	1,	protopr,
    160 	  tcp_stats,	tcp_dump,	"tcp" },
    161 	{ N_UDBTABLE,	N_UDPSTAT,	1,	protopr,
    162 	  udp_stats,	0,		"udp" },
    163 	{ -1,		N_IPSTAT,	1,	0,
    164 	  ip_stats,	0,		"ip" },
    165 	{ -1,		N_ICMPSTAT,	1,	0,
    166 	  icmp_stats,	0,		"icmp" },
    167 	{ -1,		N_IGMPSTAT,	1,	0,
    168 	  igmp_stats,	0,		"igmp" },
    169 	{ -1,		-1,		0,	0,
    170 	  0,		0,		0 }
    171 };
    172 
    173 #ifndef SMALL
    174 struct protox atalkprotox[] = {
    175 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
    176 	  ddp_stats,	0,		"ddp" },
    177 	{ -1,		-1,		0,	0,
    178 	  0,		0 }
    179 };
    180 
    181 struct protox nsprotox[] = {
    182 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
    183 	  idp_stats,	0,		"idp" },
    184 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
    185 	  spp_stats,	0,		"spp" },
    186 	{ -1,		N_NSERR,	1,	0,
    187 	  nserr_stats,	0,		"ns_err" },
    188 	{ -1,		-1,		0,	0,
    189 	  0,		0 }
    190 };
    191 
    192 struct protox isoprotox[] = {
    193 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
    194 	  tp_stats,	0,		"tp" },
    195 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
    196 	  cltp_stats,	0,		"cltp" },
    197 	{ -1,		N_CLNPSTAT,	1,	 0,
    198 	  clnp_stats,	0,		"clnp"},
    199 	{ -1,		N_ESISSTAT,	1,	 0,
    200 	  esis_stats,	0,		"esis"},
    201 	{ -1,		-1,		0,	0,
    202 	  0,		0,		0 }
    203 };
    204 #endif
    205 
    206 struct protox *protoprotox[] = { protox,
    207 #ifndef SMALL
    208 				 atalkprotox, nsprotox, isoprotox,
    209 #endif
    210 				 NULL };
    211 
    212 int main __P((int, char *[]));
    213 static void printproto __P((struct protox *, char *));
    214 static void usage __P((void));
    215 static struct protox *name2protox __P((char *));
    216 static struct protox *knownname __P((char *));
    217 
    218 kvm_t *kvmd;
    219 
    220 int
    221 main(argc, argv)
    222 	int argc;
    223 	char *argv[];
    224 {
    225 	extern char *optarg;
    226 	extern int optind;
    227 	struct protoent *p;
    228 	struct protox *tp;	/* for printing cblocks & stats */
    229 	int ch;
    230 	char *nlistf = NULL, *memf = NULL;
    231 	char buf[_POSIX2_LINE_MAX], *cp;
    232 	u_long pcbaddr;
    233 	gid_t egid = getegid();
    234 
    235 	(void)setegid(getgid());
    236 	tp = NULL;
    237 	af = AF_UNSPEC;
    238 	pcbaddr = 0;
    239 
    240 	while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:nP:p:rstuvw:")) != -1)
    241 		switch(ch) {
    242 		case 'A':
    243 			Aflag = 1;
    244 			break;
    245 		case 'a':
    246 			aflag = 1;
    247 			break;
    248 		case 'b':
    249 			bflag = 1;
    250 			break;
    251 		case 'd':
    252 			dflag = 1;
    253 			break;
    254 		case 'f':
    255 			if (strcmp(optarg, "ns") == 0)
    256 				af = AF_NS;
    257 			else if (strcmp(optarg, "inet") == 0)
    258 				af = AF_INET;
    259 			else if (strcmp(optarg, "unix") == 0
    260 			    || strcmp(optarg, "local") == 0)
    261 				af = AF_LOCAL;
    262 			else if (strcmp(optarg, "iso") == 0)
    263 				af = AF_ISO;
    264 			else if (strcmp(optarg, "atalk") == 0)
    265 				af = AF_APPLETALK;
    266 			else
    267 				errx(1, "%s: unknown address family",
    268 				    optarg);
    269 			break;
    270 #ifndef SMALL
    271 		case 'g':
    272 			gflag = 1;
    273 			break;
    274 #endif
    275 		case 'I':
    276 			iflag = 1;
    277 			interface = optarg;
    278 			break;
    279 		case 'i':
    280 			iflag = 1;
    281 			break;
    282 		case 'M':
    283 			memf = optarg;
    284 			break;
    285 		case 'm':
    286 			mflag = 1;
    287 			break;
    288 		case 'N':
    289 			nlistf = optarg;
    290 			break;
    291 		case 'n':
    292 			nflag = 1;
    293 			break;
    294 		case 'P':
    295 			pcbaddr = strtoul(optarg, &cp, 16);
    296 			if (*cp != '\0' || errno == ERANGE)
    297 				errx(1, "invalid PCB address %s",
    298 				    optarg);
    299 			Pflag = 1;
    300 			break;
    301 		case 'p':
    302 			if ((tp = name2protox(optarg)) == NULL)
    303 				errx(1, "%s: unknown or uninstrumented protocol",
    304 				    optarg);
    305 			pflag = 1;
    306 			break;
    307 		case 'r':
    308 			rflag = 1;
    309 			break;
    310 		case 's':
    311 			++sflag;
    312 			break;
    313 		case 't':
    314 			tflag = 1;
    315 			break;
    316 		case 'u':
    317 			af = AF_LOCAL;
    318 			break;
    319 		case 'v':
    320 			vflag = 1;
    321 			break;
    322 		case 'w':
    323 			interval = atoi(optarg);
    324 			iflag = 1;
    325 			break;
    326 		case '?':
    327 		default:
    328 			usage();
    329 		}
    330 	argv += optind;
    331 	argc -= optind;
    332 
    333 #define	BACKWARD_COMPATIBILITY
    334 #ifdef	BACKWARD_COMPATIBILITY
    335 	if (*argv) {
    336 		if (isdigit(**argv)) {
    337 			interval = atoi(*argv);
    338 			if (interval <= 0)
    339 				usage();
    340 			++argv;
    341 			iflag = 1;
    342 		}
    343 		if (*argv) {
    344 			nlistf = *argv;
    345 			if (*++argv)
    346 				memf = *argv;
    347 		}
    348 	}
    349 #endif
    350 
    351 	/*
    352 	 * Discard setgid privileges.  If not the running kernel, we toss
    353 	 * them away totally so that bad guys can't print interesting stuff
    354 	 * from kernel memory, otherwise switch back to kmem for the
    355 	 * duration of the kvm_openfiles() call.
    356 	 */
    357 	if (nlistf != NULL || memf != NULL || Pflag)
    358 		(void)setgid(getgid());
    359 	else
    360 		(void)setegid(egid);
    361 
    362 	if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
    363 	    buf)) == NULL)
    364 		errx(1, "%s", buf);
    365 
    366 	/* do this now anyway */
    367 	if (nlistf == NULL && memf == NULL)
    368 		(void)setgid(getgid());
    369 
    370 	if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
    371 		if (nlistf)
    372 			errx(1, "%s: no namelist", nlistf);
    373 		else
    374 			errx(1, "no namelist");
    375 	}
    376 	if (mflag) {
    377 		mbpr(nl[N_MBSTAT].n_value,  nl[N_MSIZE].n_value,
    378 		    nl[N_MCLBYTES].n_value);
    379 		exit(0);
    380 	}
    381 	if (Pflag) {
    382 		if (tp == NULL) {
    383 			/* Default to TCP. */
    384 			tp = name2protox("tcp");
    385 		}
    386 		if (tp->pr_dump)
    387 			(*tp->pr_dump)(pcbaddr);
    388 		else
    389 			printf("%s: no PCB dump routine\n", tp->pr_name);
    390 		exit(0);
    391 	}
    392 	if (pflag) {
    393 		if (tp->pr_stats)
    394 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
    395 				tp->pr_name);
    396 		else
    397 			printf("%s: no stats routine\n", tp->pr_name);
    398 		exit(0);
    399 	}
    400 	/*
    401 	 * Keep file descriptors open to avoid overhead
    402 	 * of open/close on each call to get* routines.
    403 	 */
    404 	sethostent(1);
    405 	setnetent(1);
    406 	if (iflag) {
    407 		intpr(interval, nl[N_IFNET].n_value);
    408 		exit(0);
    409 	}
    410 	if (rflag) {
    411 		if (sflag)
    412 			rt_stats(nl[N_RTSTAT].n_value);
    413 		else
    414 			routepr(nl[N_RTREE].n_value);
    415 		exit(0);
    416 	}
    417 #ifndef SMALL
    418 	if (gflag) {
    419 		if (sflag)
    420 			mrt_stats(nl[N_MRTPROTO].n_value,
    421 			    nl[N_MRTSTAT].n_value);
    422 		else
    423 			mroutepr(nl[N_MRTPROTO].n_value,
    424 			    nl[N_MFCHASHTBL].n_value,
    425 			    nl[N_MFCHASH].n_value,
    426 			    nl[N_VIFTABLE].n_value);
    427 		exit(0);
    428 	}
    429 #endif
    430 	if (af == AF_INET || af == AF_UNSPEC) {
    431 		setprotoent(1);
    432 		setservent(1);
    433 		/* ugh, this is O(MN) ... why do we do this? */
    434 		while ((p = getprotoent()) != NULL) {
    435 			for (tp = protox; tp->pr_name; tp++)
    436 				if (strcmp(tp->pr_name, p->p_name) == 0)
    437 					break;
    438 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
    439 				continue;
    440 			printproto(tp, p->p_name);
    441 		}
    442 		endprotoent();
    443 	}
    444 #ifndef SMALL
    445 	if (af == AF_APPLETALK || af == AF_UNSPEC)
    446 		for (tp = atalkprotox; tp->pr_name; tp++)
    447 			printproto(tp, tp->pr_name);
    448 	if (af == AF_NS || af == AF_UNSPEC)
    449 		for (tp = nsprotox; tp->pr_name; tp++)
    450 			printproto(tp, tp->pr_name);
    451 	if (af == AF_ISO || af == AF_UNSPEC)
    452 		for (tp = isoprotox; tp->pr_name; tp++)
    453 			printproto(tp, tp->pr_name);
    454 	if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
    455 		unixpr(nl[N_UNIXSW].n_value);
    456 #endif
    457 	exit(0);
    458 }
    459 
    460 /*
    461  * Print out protocol statistics or control blocks (per sflag).
    462  * If the interface was not specifically requested, and the symbol
    463  * is not in the namelist, ignore this one.
    464  */
    465 static void
    466 printproto(tp, name)
    467 	struct protox *tp;
    468 	char *name;
    469 {
    470 	void (*pr) __P((u_long, char *));
    471 	u_long off;
    472 
    473 	if (sflag) {
    474 		pr = tp->pr_stats;
    475 		off = nl[tp->pr_sindex].n_value;
    476 	} else {
    477 		pr = tp->pr_cblocks;
    478 		off = nl[tp->pr_index].n_value;
    479 	}
    480 	if (pr != NULL && (off || af != AF_UNSPEC))
    481 		(*pr)(off, name);
    482 }
    483 
    484 /*
    485  * Read kernel memory, return 0 on success.
    486  */
    487 int
    488 kread(addr, buf, size)
    489 	u_long addr;
    490 	char *buf;
    491 	int size;
    492 {
    493 
    494 	if (kvm_read(kvmd, addr, buf, size) != size) {
    495 		warnx("%s\n", kvm_geterr(kvmd));
    496 		return (-1);
    497 	}
    498 	return (0);
    499 }
    500 
    501 char *
    502 plural(n)
    503 	int n;
    504 {
    505 
    506 	return (n != 1 ? "s" : "");
    507 }
    508 
    509 char *
    510 plurales(n)
    511 	int n;
    512 {
    513 
    514 	return (n != 1 ? "es" : "");
    515 }
    516 
    517 /*
    518  * Find the protox for the given "well-known" name.
    519  */
    520 static struct protox *
    521 knownname(name)
    522 	char *name;
    523 {
    524 	struct protox **tpp, *tp;
    525 
    526 	for (tpp = protoprotox; *tpp; tpp++)
    527 		for (tp = *tpp; tp->pr_name; tp++)
    528 			if (strcmp(tp->pr_name, name) == 0)
    529 				return (tp);
    530 	return (NULL);
    531 }
    532 
    533 /*
    534  * Find the protox corresponding to name.
    535  */
    536 static struct protox *
    537 name2protox(name)
    538 	char *name;
    539 {
    540 	struct protox *tp;
    541 	char **alias;			/* alias from p->aliases */
    542 	struct protoent *p;
    543 
    544 	/*
    545 	 * Try to find the name in the list of "well-known" names. If that
    546 	 * fails, check if name is an alias for an Internet protocol.
    547 	 */
    548 	if ((tp = knownname(name)) != NULL)
    549 		return (tp);
    550 
    551 	setprotoent(1);			/* make protocol lookup cheaper */
    552 	while ((p = getprotoent()) != NULL) {
    553 		/* assert: name not same as p->name */
    554 		for (alias = p->p_aliases; *alias; alias++)
    555 			if (strcmp(name, *alias) == 0) {
    556 				endprotoent();
    557 				return (knownname(p->p_name));
    558 			}
    559 	}
    560 	endprotoent();
    561 	return (NULL);
    562 }
    563 
    564 static void
    565 usage()
    566 {
    567 	(void)fprintf(stderr,
    568 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname);
    569 	(void)fprintf(stderr,
    570 "       %s [-ghimnrsv] [-f address_family] [-M core] [-N system]\n",
    571 	__progname);
    572 	(void)fprintf(stderr,
    573 "       %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname);
    574 	(void)fprintf(stderr,
    575 "       %s [-M core] [-N system] [-p protocol]\n", __progname);
    576 	(void)fprintf(stderr,
    577 "       %s [-M core] [-N system] [-p protocol] -P pcbaddr\n", __progname);
    578 	exit(1);
    579 }
    580