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