Home | History | Annotate | Line # | Download | only in netstat
main.c revision 1.15
      1 /*	$NetBSD: main.c,v 1.15 1998/07/06 07:50:19 mrg 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.15 1998/07/06 07:50:19 mrg 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 struct protox atalkprotox[] = {
    174 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
    175 	  ddp_stats,	0,		"ddp" },
    176 	{ -1,		-1,		0,	0,
    177 	  0,		0 }
    178 };
    179 
    180 struct protox nsprotox[] = {
    181 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
    182 	  idp_stats,	0,		"idp" },
    183 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
    184 	  spp_stats,	0,		"spp" },
    185 	{ -1,		N_NSERR,	1,	0,
    186 	  nserr_stats,	0,		"ns_err" },
    187 	{ -1,		-1,		0,	0,
    188 	  0,		0 }
    189 };
    190 
    191 struct protox isoprotox[] = {
    192 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
    193 	  tp_stats,	0,		"tp" },
    194 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
    195 	  cltp_stats,	0,		"cltp" },
    196 	{ -1,		N_CLNPSTAT,	1,	 0,
    197 	  clnp_stats,	0,		"clnp"},
    198 	{ -1,		N_ESISSTAT,	1,	 0,
    199 	  esis_stats,	0,		"esis"},
    200 	{ -1,		-1,		0,	0,
    201 	  0,		0,		0 }
    202 };
    203 
    204 struct protox *protoprotox[] = { protox, atalkprotox,
    205 					 nsprotox, isoprotox, NULL };
    206 
    207 int main __P((int, char *[]));
    208 static void printproto __P((struct protox *, char *));
    209 static void usage __P((void));
    210 static struct protox *name2protox __P((char *));
    211 static struct protox *knownname __P((char *));
    212 
    213 
    214 kvm_t *kvmd;
    215 
    216 int
    217 main(argc, argv)
    218 	int argc;
    219 	char *argv[];
    220 {
    221 	extern char *optarg;
    222 	extern int optind;
    223 	struct protoent *p;
    224 	struct protox *tp;	/* for printing cblocks & stats */
    225 	int ch;
    226 	char *nlistf = NULL, *memf = NULL;
    227 	char buf[_POSIX2_LINE_MAX], *cp;
    228 	u_long pcbaddr;
    229 	gid_t egid = getegid();
    230 
    231 	(void)setegid(getgid());
    232 	tp = NULL;
    233 	af = AF_UNSPEC;
    234 	pcbaddr = 0;
    235 
    236 	while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:nP:p:rstuw:")) != -1)
    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 			if (strcmp(optarg, "ns") == 0)
    252 				af = AF_NS;
    253 			else if (strcmp(optarg, "inet") == 0)
    254 				af = AF_INET;
    255 			else if (strcmp(optarg, "unix") == 0)
    256 				af = AF_UNIX;
    257 			else if (strcmp(optarg, "iso") == 0)
    258 				af = AF_ISO;
    259 			else if (strcmp(optarg, "atalk") == 0)
    260 				af = AF_APPLETALK;
    261 			else {
    262 				(void)fprintf(stderr,
    263 				    "%s: %s: unknown address family\n",
    264 				    __progname, optarg);
    265 				exit(1);
    266 			}
    267 			break;
    268 		case 'g':
    269 			gflag = 1;
    270 			break;
    271 		case 'I':
    272 			iflag = 1;
    273 			interface = optarg;
    274 			break;
    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 			pcbaddr = strtoul(optarg, &cp, 16);
    292 			if (*cp != '\0' || errno == ERANGE) {
    293 				(void)fprintf(stderr,
    294 				    "%s: invalid PCB address %s\n",
    295 				    __progname, optarg);
    296 				exit(1);
    297 			}
    298 			Pflag = 1;
    299 			break;
    300 		case 'p':
    301 			if ((tp = name2protox(optarg)) == NULL) {
    302 				(void)fprintf(stderr,
    303 				    "%s: %s: unknown or uninstrumented protocol\n",
    304 				    __progname, optarg);
    305 				exit(1);
    306 			}
    307 			pflag = 1;
    308 			break;
    309 		case 'r':
    310 			rflag = 1;
    311 			break;
    312 		case 's':
    313 			++sflag;
    314 			break;
    315 		case 't':
    316 			tflag = 1;
    317 			break;
    318 		case 'u':
    319 			af = AF_UNIX;
    320 			break;
    321 		case 'w':
    322 			interval = atoi(optarg);
    323 			iflag = 1;
    324 			break;
    325 		case '?':
    326 		default:
    327 			usage();
    328 		}
    329 	argv += optind;
    330 	argc -= optind;
    331 
    332 #define	BACKWARD_COMPATIBILITY
    333 #ifdef	BACKWARD_COMPATIBILITY
    334 	if (*argv) {
    335 		if (isdigit(**argv)) {
    336 			interval = atoi(*argv);
    337 			if (interval <= 0)
    338 				usage();
    339 			++argv;
    340 			iflag = 1;
    341 		}
    342 		if (*argv) {
    343 			nlistf = *argv;
    344 			if (*++argv)
    345 				memf = *argv;
    346 		}
    347 	}
    348 #endif
    349 
    350 	/*
    351 	 * Discard setgid privileges.  If not the running kernel, we toss
    352 	 * them away totally so that bad guys can't print interesting stuff
    353 	 * from kernel memory, otherwise switch back to kmem for the
    354 	 * duration of the kvm_openfiles() call.
    355 	 */
    356 	if (nlistf != NULL || memf != NULL)
    357 		(void)setgid(getgid());
    358 	else
    359 		(void)setegid(egid);
    360 
    361 	if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
    362 	    buf)) == NULL)
    363 		errx(1, "%s", buf);
    364 
    365 	if (nlistf == NULL && memf == NULL)
    366 		(void)setgid(getgid());
    367 
    368 	if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
    369 		if (nlistf)
    370 			fprintf(stderr, "%s: %s: no namelist\n", __progname,
    371 			    nlistf);
    372 		else
    373 			fprintf(stderr, "%s: no namelist\n", __progname);
    374 		exit(1);
    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 	if (gflag) {
    418 		if (sflag)
    419 			mrt_stats(nl[N_MRTPROTO].n_value,
    420 			    nl[N_MRTSTAT].n_value);
    421 		else
    422 			mroutepr(nl[N_MRTPROTO].n_value,
    423 			    nl[N_MFCHASHTBL].n_value,
    424 			    nl[N_MFCHASH].n_value,
    425 			    nl[N_VIFTABLE].n_value);
    426 		exit(0);
    427 	}
    428 	if (af == AF_INET || af == AF_UNSPEC) {
    429 		setprotoent(1);
    430 		setservent(1);
    431 		/* ugh, this is O(MN) ... why do we do this? */
    432 		while ((p = getprotoent()) != NULL) {
    433 			for (tp = protox; tp->pr_name; tp++)
    434 				if (strcmp(tp->pr_name, p->p_name) == 0)
    435 					break;
    436 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
    437 				continue;
    438 			printproto(tp, p->p_name);
    439 		}
    440 		endprotoent();
    441 	}
    442 	if (af == AF_APPLETALK || af == AF_UNSPEC)
    443 		for (tp = atalkprotox; tp->pr_name; tp++)
    444 			printproto(tp, tp->pr_name);
    445 	if (af == AF_NS || af == AF_UNSPEC)
    446 		for (tp = nsprotox; tp->pr_name; tp++)
    447 			printproto(tp, tp->pr_name);
    448 	if (af == AF_ISO || af == AF_UNSPEC)
    449 		for (tp = isoprotox; tp->pr_name; tp++)
    450 			printproto(tp, tp->pr_name);
    451 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
    452 		unixpr(nl[N_UNIXSW].n_value);
    453 	exit(0);
    454 }
    455 
    456 /*
    457  * Print out protocol statistics or control blocks (per sflag).
    458  * If the interface was not specifically requested, and the symbol
    459  * is not in the namelist, ignore this one.
    460  */
    461 static void
    462 printproto(tp, name)
    463 	struct protox *tp;
    464 	char *name;
    465 {
    466 	void (*pr) __P((u_long, char *));
    467 	u_long off;
    468 
    469 	if (sflag) {
    470 		pr = tp->pr_stats;
    471 		off = nl[tp->pr_sindex].n_value;
    472 	} else {
    473 		pr = tp->pr_cblocks;
    474 		off = nl[tp->pr_index].n_value;
    475 	}
    476 	if (pr != NULL && (off || af != AF_UNSPEC))
    477 		(*pr)(off, name);
    478 }
    479 
    480 /*
    481  * Read kernel memory, return 0 on success.
    482  */
    483 int
    484 kread(addr, buf, size)
    485 	u_long addr;
    486 	char *buf;
    487 	int size;
    488 {
    489 
    490 	if (kvm_read(kvmd, addr, buf, size) != size) {
    491 		(void)fprintf(stderr, "%s: %s\n", __progname,
    492 		    kvm_geterr(kvmd));
    493 		return (-1);
    494 	}
    495 	return (0);
    496 }
    497 
    498 char *
    499 plural(n)
    500 	int n;
    501 {
    502 	return (n != 1 ? "s" : "");
    503 }
    504 
    505 char *
    506 plurales(n)
    507 	int n;
    508 {
    509 	return (n != 1 ? "es" : "");
    510 }
    511 
    512 /*
    513  * Find the protox for the given "well-known" name.
    514  */
    515 static struct protox *
    516 knownname(name)
    517 	char *name;
    518 {
    519 	struct protox **tpp, *tp;
    520 
    521 	for (tpp = protoprotox; *tpp; tpp++)
    522 		for (tp = *tpp; tp->pr_name; tp++)
    523 			if (strcmp(tp->pr_name, name) == 0)
    524 				return (tp);
    525 	return (NULL);
    526 }
    527 
    528 /*
    529  * Find the protox corresponding to name.
    530  */
    531 static struct protox *
    532 name2protox(name)
    533 	char *name;
    534 {
    535 	struct protox *tp;
    536 	char **alias;			/* alias from p->aliases */
    537 	struct protoent *p;
    538 
    539 	/*
    540 	 * Try to find the name in the list of "well-known" names. If that
    541 	 * fails, check if name is an alias for an Internet protocol.
    542 	 */
    543 	if ((tp = knownname(name)) != NULL)
    544 		return (tp);
    545 
    546 	setprotoent(1);			/* make protocol lookup cheaper */
    547 	while ((p = getprotoent()) != NULL) {
    548 		/* assert: name not same as p->name */
    549 		for (alias = p->p_aliases; *alias; alias++)
    550 			if (strcmp(name, *alias) == 0) {
    551 				endprotoent();
    552 				return (knownname(p->p_name));
    553 			}
    554 	}
    555 	endprotoent();
    556 	return (NULL);
    557 }
    558 
    559 static void
    560 usage()
    561 {
    562 	(void)fprintf(stderr,
    563 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname);
    564 	(void)fprintf(stderr,
    565 "       %s [-ghimnrs] [-f address_family] [-M core] [-N system]\n", __progname);
    566 	(void)fprintf(stderr,
    567 "       %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname);
    568 	(void)fprintf(stderr,
    569 "       %s [-M core] [-N system] [-p protocol]\n", __progname);
    570 	(void)fprintf(stderr,
    571 "       %s [-M core] [-N system] [-p protocol] -P pcbaddr\n", __progname);
    572 	exit(1);
    573 }
    574