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