Home | History | Annotate | Line # | Download | only in netstat
main.c revision 1.14
      1 /*	$NetBSD: main.c,v 1.14 1998/06/03 02:41:11 thorpej 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.14 1998/06/03 02:41:11 thorpej 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 <errno.h>
     59 #include <kvm.h>
     60 #include <limits.h>
     61 #include <netdb.h>
     62 #include <nlist.h>
     63 #include <paths.h>
     64 #include <stdio.h>
     65 #include <stdlib.h>
     66 #include <string.h>
     67 #include <unistd.h>
     68 #include "netstat.h"
     69 
     70 struct nlist nl[] = {
     71 #define	N_MBSTAT	0
     72 	{ "_mbstat" },
     73 #define	N_IPSTAT	1
     74 	{ "_ipstat" },
     75 #define	N_TCBTABLE	2
     76 	{ "_tcbtable" },
     77 #define	N_TCPSTAT	3
     78 	{ "_tcpstat" },
     79 #define	N_UDBTABLE	4
     80 	{ "_udbtable" },
     81 #define	N_UDPSTAT	5
     82 	{ "_udpstat" },
     83 #define	N_IFNET		6
     84 	{ "_ifnet" },
     85 #define	N_IMP		7
     86 	{ "_imp_softc" },
     87 #define	N_ICMPSTAT	8
     88 	{ "_icmpstat" },
     89 #define	N_RTSTAT	9
     90 	{ "_rtstat" },
     91 #define	N_UNIXSW	10
     92 	{ "_unixsw" },
     93 #define N_IDP		11
     94 	{ "_nspcb"},
     95 #define N_IDPSTAT	12
     96 	{ "_idpstat"},
     97 #define N_SPPSTAT	13
     98 	{ "_spp_istat"},
     99 #define N_NSERR		14
    100 	{ "_ns_errstat"},
    101 #define	N_CLNPSTAT	15
    102 	{ "_clnp_stat"},
    103 #define	IN_NOTUSED	16
    104 	{ "_tp_inpcb" },
    105 #define	ISO_TP		17
    106 	{ "_tp_refinfo" },
    107 #define	N_TPSTAT	18
    108 	{ "_tp_stat" },
    109 #define	N_ESISSTAT	19
    110 	{ "_esis_stat"},
    111 #define N_NIMP		20
    112 	{ "_nimp"},
    113 #define N_RTREE		21
    114 	{ "_rt_tables"},
    115 #define N_CLTP		22
    116 	{ "_cltb"},
    117 #define N_CLTPSTAT	23
    118 	{ "_cltpstat"},
    119 #define	N_NFILE		24
    120 	{ "_nfile" },
    121 #define	N_FILE		25
    122 	{ "_file" },
    123 #define N_IGMPSTAT	26
    124 	{ "_igmpstat" },
    125 #define N_MRTPROTO	27
    126 	{ "_ip_mrtproto" },
    127 #define N_MRTSTAT	28
    128 	{ "_mrtstat" },
    129 #define N_MFCHASHTBL	29
    130 	{ "_mfchashtbl" },
    131 #define	N_MFCHASH	30
    132 	{ "_mfchash" },
    133 #define N_VIFTABLE	31
    134 	{ "_viftable" },
    135 #define N_MSIZE		32
    136 	{ "_msize" },
    137 #define N_MCLBYTES	33
    138 	{ "_mclbytes" },
    139 #define N_DDPSTAT	34
    140 	{ "_ddpstat"},
    141 #define N_DDPCB		35
    142 	{ "_ddpcb"},
    143 	{ "" },
    144 };
    145 
    146 struct protox {
    147 	u_char	pr_index;		/* index into nlist of cb head */
    148 	u_char	pr_sindex;		/* index into nlist of stat block */
    149 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
    150 	void	(*pr_cblocks)		/* control blocks printing routine */
    151 			__P((u_long, char *));
    152 	void	(*pr_stats)		/* statistics printing routine */
    153 			__P((u_long, char *));
    154 	void	(*pr_dump)		/* PCB state dump routine */
    155 			__P((u_long));
    156 	char	*pr_name;		/* well-known name */
    157 } protox[] = {
    158 	{ N_TCBTABLE,	N_TCPSTAT,	1,	protopr,
    159 	  tcp_stats,	tcp_dump,	"tcp" },
    160 	{ N_UDBTABLE,	N_UDPSTAT,	1,	protopr,
    161 	  udp_stats,	0,		"udp" },
    162 	{ -1,		N_IPSTAT,	1,	0,
    163 	  ip_stats,	0,		"ip" },
    164 	{ -1,		N_ICMPSTAT,	1,	0,
    165 	  icmp_stats,	0,		"icmp" },
    166 	{ -1,		N_IGMPSTAT,	1,	0,
    167 	  igmp_stats,	0,		"igmp" },
    168 	{ -1,		-1,		0,	0,
    169 	  0,		0,		0 }
    170 };
    171 
    172 struct protox atalkprotox[] = {
    173 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
    174 	  ddp_stats,	0,		"ddp" },
    175 	{ -1,		-1,		0,	0,
    176 	  0,		0 }
    177 };
    178 
    179 struct protox nsprotox[] = {
    180 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
    181 	  idp_stats,	0,		"idp" },
    182 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
    183 	  spp_stats,	0,		"spp" },
    184 	{ -1,		N_NSERR,	1,	0,
    185 	  nserr_stats,	0,		"ns_err" },
    186 	{ -1,		-1,		0,	0,
    187 	  0,		0 }
    188 };
    189 
    190 struct protox isoprotox[] = {
    191 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
    192 	  tp_stats,	0,		"tp" },
    193 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
    194 	  cltp_stats,	0,		"cltp" },
    195 	{ -1,		N_CLNPSTAT,	1,	 0,
    196 	  clnp_stats,	0,		"clnp"},
    197 	{ -1,		N_ESISSTAT,	1,	 0,
    198 	  esis_stats,	0,		"esis"},
    199 	{ -1,		-1,		0,	0,
    200 	  0,		0,		0 }
    201 };
    202 
    203 struct protox *protoprotox[] = { protox, atalkprotox,
    204 					 nsprotox, isoprotox, NULL };
    205 
    206 int main __P((int, char *[]));
    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 
    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 
    229 	tp = NULL;
    230 	af = AF_UNSPEC;
    231 	pcbaddr = 0;
    232 
    233 	while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:nP:p:rstuw:")) != -1)
    234 		switch(ch) {
    235 		case 'A':
    236 			Aflag = 1;
    237 			break;
    238 		case 'a':
    239 			aflag = 1;
    240 			break;
    241 		case 'b':
    242 			bflag = 1;
    243 			break;
    244 		case 'd':
    245 			dflag = 1;
    246 			break;
    247 		case 'f':
    248 			if (strcmp(optarg, "ns") == 0)
    249 				af = AF_NS;
    250 			else if (strcmp(optarg, "inet") == 0)
    251 				af = AF_INET;
    252 			else if (strcmp(optarg, "unix") == 0)
    253 				af = AF_UNIX;
    254 			else if (strcmp(optarg, "iso") == 0)
    255 				af = AF_ISO;
    256 			else if (strcmp(optarg, "atalk") == 0)
    257 				af = AF_APPLETALK;
    258 			else {
    259 				(void)fprintf(stderr,
    260 				    "%s: %s: unknown address family\n",
    261 				    __progname, optarg);
    262 				exit(1);
    263 			}
    264 			break;
    265 		case 'g':
    266 			gflag = 1;
    267 			break;
    268 		case 'I':
    269 			iflag = 1;
    270 			interface = optarg;
    271 			break;
    272 		case 'i':
    273 			iflag = 1;
    274 			break;
    275 		case 'M':
    276 			memf = optarg;
    277 			break;
    278 		case 'm':
    279 			mflag = 1;
    280 			break;
    281 		case 'N':
    282 			nlistf = optarg;
    283 			break;
    284 		case 'n':
    285 			nflag = 1;
    286 			break;
    287 		case 'P':
    288 			pcbaddr = strtoul(optarg, &cp, 16);
    289 			if (*cp != '\0' || errno == ERANGE) {
    290 				(void)fprintf(stderr,
    291 				    "%s: invalid PCB address %s\n",
    292 				    __progname, optarg);
    293 				exit(1);
    294 			}
    295 			Pflag = 1;
    296 			break;
    297 		case 'p':
    298 			if ((tp = name2protox(optarg)) == NULL) {
    299 				(void)fprintf(stderr,
    300 				    "%s: %s: unknown or uninstrumented protocol\n",
    301 				    __progname, optarg);
    302 				exit(1);
    303 			}
    304 			pflag = 1;
    305 			break;
    306 		case 'r':
    307 			rflag = 1;
    308 			break;
    309 		case 's':
    310 			++sflag;
    311 			break;
    312 		case 't':
    313 			tflag = 1;
    314 			break;
    315 		case 'u':
    316 			af = AF_UNIX;
    317 			break;
    318 		case 'w':
    319 			interval = atoi(optarg);
    320 			iflag = 1;
    321 			break;
    322 		case '?':
    323 		default:
    324 			usage();
    325 		}
    326 	argv += optind;
    327 	argc -= optind;
    328 
    329 #define	BACKWARD_COMPATIBILITY
    330 #ifdef	BACKWARD_COMPATIBILITY
    331 	if (*argv) {
    332 		if (isdigit(**argv)) {
    333 			interval = atoi(*argv);
    334 			if (interval <= 0)
    335 				usage();
    336 			++argv;
    337 			iflag = 1;
    338 		}
    339 		if (*argv) {
    340 			nlistf = *argv;
    341 			if (*++argv)
    342 				memf = *argv;
    343 		}
    344 	}
    345 #endif
    346 
    347 	/*
    348 	 * Discard setgid privileges if not the running kernel so that bad
    349 	 * guys can't print interesting stuff from kernel memory.
    350 	 */
    351 	if (nlistf != NULL || memf != NULL)
    352 		setgid(getgid());
    353 
    354 	if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
    355 	    buf)) == NULL) {
    356 		fprintf(stderr, "%s: kvm_open: %s\n", __progname, buf);
    357 		exit(1);
    358 	}
    359 	if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
    360 		if (nlistf)
    361 			fprintf(stderr, "%s: %s: no namelist\n", __progname,
    362 			    nlistf);
    363 		else
    364 			fprintf(stderr, "%s: no namelist\n", __progname);
    365 		exit(1);
    366 	}
    367 	if (mflag) {
    368 		mbpr(nl[N_MBSTAT].n_value,  nl[N_MSIZE].n_value,
    369 		    nl[N_MCLBYTES].n_value);
    370 		exit(0);
    371 	}
    372 	if (Pflag) {
    373 		if (tp == NULL) {
    374 			/* Default to TCP. */
    375 			tp = name2protox("tcp");
    376 		}
    377 		if (tp->pr_dump)
    378 			(*tp->pr_dump)(pcbaddr);
    379 		else
    380 			printf("%s: no PCB dump routine\n", tp->pr_name);
    381 		exit(0);
    382 	}
    383 	if (pflag) {
    384 		if (tp->pr_stats)
    385 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
    386 				tp->pr_name);
    387 		else
    388 			printf("%s: no stats routine\n", tp->pr_name);
    389 		exit(0);
    390 	}
    391 	/*
    392 	 * Keep file descriptors open to avoid overhead
    393 	 * of open/close on each call to get* routines.
    394 	 */
    395 	sethostent(1);
    396 	setnetent(1);
    397 	if (iflag) {
    398 		intpr(interval, nl[N_IFNET].n_value);
    399 		exit(0);
    400 	}
    401 	if (rflag) {
    402 		if (sflag)
    403 			rt_stats(nl[N_RTSTAT].n_value);
    404 		else
    405 			routepr(nl[N_RTREE].n_value);
    406 		exit(0);
    407 	}
    408 	if (gflag) {
    409 		if (sflag)
    410 			mrt_stats(nl[N_MRTPROTO].n_value,
    411 			    nl[N_MRTSTAT].n_value);
    412 		else
    413 			mroutepr(nl[N_MRTPROTO].n_value,
    414 			    nl[N_MFCHASHTBL].n_value,
    415 			    nl[N_MFCHASH].n_value,
    416 			    nl[N_VIFTABLE].n_value);
    417 		exit(0);
    418 	}
    419 	if (af == AF_INET || af == AF_UNSPEC) {
    420 		setprotoent(1);
    421 		setservent(1);
    422 		/* ugh, this is O(MN) ... why do we do this? */
    423 		while ((p = getprotoent()) != NULL) {
    424 			for (tp = protox; tp->pr_name; tp++)
    425 				if (strcmp(tp->pr_name, p->p_name) == 0)
    426 					break;
    427 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
    428 				continue;
    429 			printproto(tp, p->p_name);
    430 		}
    431 		endprotoent();
    432 	}
    433 	if (af == AF_APPLETALK || af == AF_UNSPEC)
    434 		for (tp = atalkprotox; tp->pr_name; tp++)
    435 			printproto(tp, tp->pr_name);
    436 	if (af == AF_NS || af == AF_UNSPEC)
    437 		for (tp = nsprotox; tp->pr_name; tp++)
    438 			printproto(tp, tp->pr_name);
    439 	if (af == AF_ISO || af == AF_UNSPEC)
    440 		for (tp = isoprotox; tp->pr_name; tp++)
    441 			printproto(tp, tp->pr_name);
    442 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
    443 		unixpr(nl[N_UNIXSW].n_value);
    444 	exit(0);
    445 }
    446 
    447 /*
    448  * Print out protocol statistics or control blocks (per sflag).
    449  * If the interface was not specifically requested, and the symbol
    450  * is not in the namelist, ignore this one.
    451  */
    452 static void
    453 printproto(tp, name)
    454 	struct protox *tp;
    455 	char *name;
    456 {
    457 	void (*pr) __P((u_long, char *));
    458 	u_long off;
    459 
    460 	if (sflag) {
    461 		pr = tp->pr_stats;
    462 		off = nl[tp->pr_sindex].n_value;
    463 	} else {
    464 		pr = tp->pr_cblocks;
    465 		off = nl[tp->pr_index].n_value;
    466 	}
    467 	if (pr != NULL && (off || af != AF_UNSPEC))
    468 		(*pr)(off, name);
    469 }
    470 
    471 /*
    472  * Read kernel memory, return 0 on success.
    473  */
    474 int
    475 kread(addr, buf, size)
    476 	u_long addr;
    477 	char *buf;
    478 	int size;
    479 {
    480 
    481 	if (kvm_read(kvmd, addr, buf, size) != size) {
    482 		(void)fprintf(stderr, "%s: %s\n", __progname,
    483 		    kvm_geterr(kvmd));
    484 		return (-1);
    485 	}
    486 	return (0);
    487 }
    488 
    489 char *
    490 plural(n)
    491 	int n;
    492 {
    493 	return (n != 1 ? "s" : "");
    494 }
    495 
    496 char *
    497 plurales(n)
    498 	int n;
    499 {
    500 	return (n != 1 ? "es" : "");
    501 }
    502 
    503 /*
    504  * Find the protox for the given "well-known" name.
    505  */
    506 static struct protox *
    507 knownname(name)
    508 	char *name;
    509 {
    510 	struct protox **tpp, *tp;
    511 
    512 	for (tpp = protoprotox; *tpp; tpp++)
    513 		for (tp = *tpp; tp->pr_name; tp++)
    514 			if (strcmp(tp->pr_name, name) == 0)
    515 				return (tp);
    516 	return (NULL);
    517 }
    518 
    519 /*
    520  * Find the protox corresponding to name.
    521  */
    522 static struct protox *
    523 name2protox(name)
    524 	char *name;
    525 {
    526 	struct protox *tp;
    527 	char **alias;			/* alias from p->aliases */
    528 	struct protoent *p;
    529 
    530 	/*
    531 	 * Try to find the name in the list of "well-known" names. If that
    532 	 * fails, check if name is an alias for an Internet protocol.
    533 	 */
    534 	if ((tp = knownname(name)) != NULL)
    535 		return (tp);
    536 
    537 	setprotoent(1);			/* make protocol lookup cheaper */
    538 	while ((p = getprotoent()) != NULL) {
    539 		/* assert: name not same as p->name */
    540 		for (alias = p->p_aliases; *alias; alias++)
    541 			if (strcmp(name, *alias) == 0) {
    542 				endprotoent();
    543 				return (knownname(p->p_name));
    544 			}
    545 	}
    546 	endprotoent();
    547 	return (NULL);
    548 }
    549 
    550 static void
    551 usage()
    552 {
    553 	(void)fprintf(stderr,
    554 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname);
    555 	(void)fprintf(stderr,
    556 "       %s [-ghimnrs] [-f address_family] [-M core] [-N system]\n", __progname);
    557 	(void)fprintf(stderr,
    558 "       %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname);
    559 	(void)fprintf(stderr,
    560 "       %s [-M core] [-N system] [-p protocol]\n", __progname);
    561 	(void)fprintf(stderr,
    562 "       %s [-M core] [-N system] [-p protocol] -P pcbaddr\n", __progname);
    563 	exit(1);
    564 }
    565