main.c revision 12316
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
35char 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
41static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 3/1/94";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/file.h>
46#include <sys/protosw.h>
47#include <sys/socket.h>
48
49#include <netinet/in.h>
50
51#include <ctype.h>
52#include <errno.h>
53#include <kvm.h>
54#include <limits.h>
55#include <netdb.h>
56#include <nlist.h>
57#include <paths.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
62#include <err.h>
63#include "netstat.h"
64
65struct 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	{ "_localsw" },
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_MFCTABLE	29
125	{ "_mfctable" },
126#define N_VIFTABLE	30
127	{ "_viftable" },
128#define N_IPX		31
129	{ "_ipxpcb"},
130#define N_IPXSTAT	32
131	{ "_ipxstat"},
132#define N_SPXSTAT	33
133	{ "_spx_istat"},
134#define N_IPXERR	34
135	{ "_ipx_errstat"},
136	"",
137};
138
139struct protox {
140	u_char	pr_index;		/* index into nlist of cb head */
141	u_char	pr_sindex;		/* index into nlist of stat block */
142	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
143	void	(*pr_cblocks)();	/* control blocks printing routine */
144	void	(*pr_stats)();		/* statistics printing routine */
145	char	*pr_name;		/* well-known name */
146} protox[] = {
147	{ N_TCB,	N_TCPSTAT,	1,	protopr,
148	  tcp_stats,	"tcp" },
149	{ N_UDB,	N_UDPSTAT,	1,	protopr,
150	  udp_stats,	"udp" },
151	{ -1,		N_IPSTAT,	1,	0,
152	  ip_stats,	"ip" },
153	{ -1,		N_ICMPSTAT,	1,	0,
154	  icmp_stats,	"icmp" },
155	{ -1,		N_IGMPSTAT,	1,	0,
156	  igmp_stats,	"igmp" },
157	{ -1,		-1,		0,	0,
158	  0,		0 }
159};
160
161struct protox ipxprotox[] = {
162	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
163	  ipx_stats,	"ipx" },
164	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
165	  spx_stats,	"spx" },
166	{ -1,		N_IPXERR,	1,	0,
167	  ipxerr_stats,	"ipx_err" },
168	{ -1,		-1,		0,	0,
169	  0,		0 }
170};
171
172struct protox nsprotox[] = {
173	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
174	  idp_stats,	"idp" },
175	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
176	  spp_stats,	"spp" },
177	{ -1,		N_NSERR,	1,	0,
178	  nserr_stats,	"ns_err" },
179	{ -1,		-1,		0,	0,
180	  0,		0 }
181};
182
183struct protox isoprotox[] = {
184	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
185	  tp_stats,	"tp" },
186	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
187	  cltp_stats,	"cltp" },
188	{ -1,		N_CLNPSTAT,	1,	 0,
189	  clnp_stats,	"clnp"},
190	{ -1,		N_ESISSTAT,	1,	 0,
191	  esis_stats,	"esis"},
192	{ -1,		-1,		0,	0,
193	  0,		0 }
194};
195
196struct protox *protoprotox[] = { protox, ipxprotox, nsprotox, isoprotox, NULL };
197
198static void printproto __P((struct protox *, char *));
199static void usage __P((void));
200static struct protox *name2protox __P((char *));
201static struct protox *knownname __P((char *));
202
203kvm_t *kvmd;
204
205int
206main(argc, argv)
207	int argc;
208	char *argv[];
209{
210	extern char *optarg;
211	extern int optind;
212	register struct protoent *p;
213	register struct protox *tp;	/* for printing cblocks & stats */
214	register char *cp;
215	int ch;
216	char *nlistf = NULL, *memf = NULL;
217	char buf[_POSIX2_LINE_MAX];
218	char buf2[_POSIX2_LINE_MAX];
219
220	if (cp = rindex(argv[0], '/'))
221		prog = cp + 1;
222	else
223		prog = argv[0];
224	af = AF_UNSPEC;
225
226	while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:np:rstuw:")) != EOF)
227		switch(ch) {
228		case 'A':
229			Aflag = 1;
230			break;
231		case 'a':
232			aflag = 1;
233			break;
234		case 'b':
235			bflag = 1;
236			break;
237		case 'd':
238			dflag = 1;
239			break;
240		case 'f':
241			if (strcmp(optarg, "ns") == 0)
242				af = AF_NS;
243			else if (strcmp(optarg, "ipx") == 0)
244				af = AF_IPX;
245			else if (strcmp(optarg, "inet") == 0)
246				af = AF_INET;
247			else if (strcmp(optarg, "unix") == 0)
248				af = AF_UNIX;
249			else if (strcmp(optarg, "iso") == 0)
250				af = AF_ISO;
251			else {
252				errx(1, "%s: unknown address family", optarg);
253			}
254			break;
255		case 'g':
256			gflag = 1;
257			break;
258		case 'I': {
259			char *cp;
260
261			iflag = 1;
262			for (cp = interface = optarg; isalpha(*cp); cp++)
263				continue;
264			unit = atoi(cp);
265			break;
266		}
267		case 'i':
268			iflag = 1;
269			break;
270		case 'M':
271			memf = optarg;
272			break;
273		case 'm':
274			mflag = 1;
275			break;
276		case 'N':
277			nlistf = optarg;
278			break;
279		case 'n':
280			nflag = 1;
281			break;
282		case 'p':
283			if ((tp = name2protox(optarg)) == NULL) {
284				errx(1,
285				     "%s: unknown or uninstrumented protocol",
286				     optarg);
287			}
288			pflag = 1;
289			break;
290		case 'r':
291			rflag = 1;
292			break;
293		case 's':
294			++sflag;
295			break;
296		case 't':
297			tflag = 1;
298			break;
299		case 'u':
300			af = AF_UNIX;
301			break;
302		case 'w':
303			interval = atoi(optarg);
304			iflag = 1;
305			break;
306		case '?':
307		default:
308			usage();
309		}
310	argv += optind;
311	argc -= optind;
312
313#define	BACKWARD_COMPATIBILITY
314#ifdef	BACKWARD_COMPATIBILITY
315	if (*argv) {
316		if (isdigit(**argv)) {
317			interval = atoi(*argv);
318			if (interval <= 0)
319				usage();
320			++argv;
321			iflag = 1;
322		}
323		if (*argv) {
324			nlistf = *argv;
325			if (*++argv)
326				memf = *argv;
327		}
328	}
329#endif
330
331	/*
332	 * Discard setgid privileges if not the running kernel so that bad
333	 * guys can't print interesting stuff from kernel memory.
334	 */
335	if (nlistf != NULL || memf != NULL)
336		setgid(getgid());
337
338	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
339	if (kvmd == NULL) {
340		errx(1, "kvm_open: %s", buf);
341	}
342	if (kvm_nlist(kvmd, nl) < 0) {
343		if(nlistf)
344			errx(1, "%s: kvm_nlist: %s", nlistf, kvm_geterr(kvmd));
345		else
346			errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
347	}
348
349	if (nl[0].n_type == 0) {
350		if(nlistf)
351			errx(1, "%s: no namelist", nlistf);
352		else
353			errx(1, "no namelist");
354	}
355	if (mflag) {
356		mbpr(nl[N_MBSTAT].n_value);
357		exit(0);
358	}
359	if (pflag) {
360		if (tp->pr_stats)
361			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
362				tp->pr_name);
363		else
364			printf("%s: no stats routine\n", tp->pr_name);
365		exit(0);
366	}
367	/*
368	 * Keep file descriptors open to avoid overhead
369	 * of open/close on each call to get* routines.
370	 */
371	sethostent(1);
372	setnetent(1);
373	if (iflag) {
374		intpr(interval, nl[N_IFNET].n_value);
375		exit(0);
376	}
377	if (rflag) {
378		if (sflag)
379			rt_stats(nl[N_RTSTAT].n_value);
380		else
381			routepr(nl[N_RTREE].n_value);
382		exit(0);
383	}
384	if (gflag) {
385		if (sflag)
386			mrt_stats(nl[N_MRTPROTO].n_value,
387			    nl[N_MRTSTAT].n_value);
388		else
389			mroutepr(nl[N_MRTPROTO].n_value,
390			    nl[N_MFCTABLE].n_value,
391			    nl[N_VIFTABLE].n_value);
392		exit(0);
393	}
394	if (af == AF_INET || af == AF_UNSPEC) {
395		setprotoent(1);
396		setservent(1);
397		/* ugh, this is O(MN) ... why do we do this? */
398		while (p = getprotoent()) {
399			for (tp = protox; tp->pr_name; tp++)
400				if (strcmp(tp->pr_name, p->p_name) == 0)
401					break;
402			if (tp->pr_name == 0 || tp->pr_wanted == 0)
403				continue;
404			printproto(tp, p->p_name);
405		}
406		endprotoent();
407	}
408	if (af == AF_IPX || af == AF_UNSPEC)
409		for (tp = ipxprotox; tp->pr_name; tp++)
410			printproto(tp, tp->pr_name);
411	if (af == AF_NS || af == AF_UNSPEC)
412		for (tp = nsprotox; tp->pr_name; tp++)
413			printproto(tp, tp->pr_name);
414	if (af == AF_ISO || af == AF_UNSPEC)
415		for (tp = isoprotox; tp->pr_name; tp++)
416			printproto(tp, tp->pr_name);
417	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
418		unixpr(nl[N_UNIXSW].n_value);
419	exit(0);
420}
421
422/*
423 * Print out protocol statistics or control blocks (per sflag).
424 * If the interface was not specifically requested, and the symbol
425 * is not in the namelist, ignore this one.
426 */
427static void
428printproto(tp, name)
429	register struct protox *tp;
430	char *name;
431{
432	void (*pr)();
433	u_long off;
434
435	if (sflag) {
436		pr = tp->pr_stats;
437		off = nl[tp->pr_sindex].n_value;
438	} else {
439		pr = tp->pr_cblocks;
440		off = nl[tp->pr_index].n_value;
441	}
442	if (pr != NULL && (off || af != AF_UNSPEC))
443		(*pr)(off, name);
444}
445
446/*
447 * Read kernel memory, return 0 on success.
448 */
449int
450kread(addr, buf, size)
451	u_long addr;
452	char *buf;
453	int size;
454{
455
456	if (kvm_read(kvmd, addr, buf, size) != size) {
457		warnx("%s", kvm_geterr(kvmd));
458		return (-1);
459	}
460	return (0);
461}
462
463char *
464plural(n)
465	int n;
466{
467	return (n != 1 ? "s" : "");
468}
469
470char *
471plurales(n)
472	int n;
473{
474	return (n != 1 ? "es" : "");
475}
476
477/*
478 * Find the protox for the given "well-known" name.
479 */
480static struct protox *
481knownname(name)
482	char *name;
483{
484	struct protox **tpp, *tp;
485
486	for (tpp = protoprotox; *tpp; tpp++)
487		for (tp = *tpp; tp->pr_name; tp++)
488			if (strcmp(tp->pr_name, name) == 0)
489				return (tp);
490	return (NULL);
491}
492
493/*
494 * Find the protox corresponding to name.
495 */
496static struct protox *
497name2protox(name)
498	char *name;
499{
500	struct protox *tp;
501	char **alias;			/* alias from p->aliases */
502	struct protoent *p;
503
504	/*
505	 * Try to find the name in the list of "well-known" names. If that
506	 * fails, check if name is an alias for an Internet protocol.
507	 */
508	if (tp = knownname(name))
509		return (tp);
510
511	setprotoent(1);			/* make protocol lookup cheaper */
512	while (p = getprotoent()) {
513		/* assert: name not same as p->name */
514		for (alias = p->p_aliases; *alias; alias++)
515			if (strcmp(name, *alias) == 0) {
516				endprotoent();
517				return (knownname(p->p_name));
518			}
519	}
520	endprotoent();
521	return (NULL);
522}
523
524static void
525usage()
526{
527	(void)fprintf(stderr,
528"usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", prog);
529	(void)fprintf(stderr,
530"       %s [-bdghimnrs] [-f address_family] [-M core] [-N system]\n", prog);
531	(void)fprintf(stderr,
532"       %s [-bdn] [-I interface] [-M core] [-N system] [-w wait]\n", prog);
533	(void)fprintf(stderr,
534"       %s [-M core] [-N system] [-p protocol]\n", prog);
535	exit(1);
536}
537