main.c revision 8874
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	{ "_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
131struct 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
153struct 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
164struct 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
177struct protox *protoprotox[] = { protox, nsprotox, isoprotox, NULL };
178
179static void printproto __P((struct protox *, char *));
180static void usage __P((void));
181static struct protox *name2protox __P((char *));
182static struct protox *knownname __P((char *));
183
184kvm_t *kvmd;
185
186int
187main(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	char buf2[_POSIX2_LINE_MAX];
200
201	if (cp = rindex(argv[0], '/'))
202		prog = cp + 1;
203	else
204		prog = argv[0];
205	af = AF_UNSPEC;
206
207	while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:np:rstuw:")) != EOF)
208		switch(ch) {
209		case 'A':
210			Aflag = 1;
211			break;
212		case 'a':
213			aflag = 1;
214			break;
215		case 'b':
216			bflag = 1;
217			break;
218		case 'd':
219			dflag = 1;
220			break;
221		case 'f':
222			if (strcmp(optarg, "ns") == 0)
223				af = AF_NS;
224			else if (strcmp(optarg, "inet") == 0)
225				af = AF_INET;
226			else if (strcmp(optarg, "unix") == 0)
227				af = AF_UNIX;
228			else if (strcmp(optarg, "iso") == 0)
229				af = AF_ISO;
230			else {
231				errx(1, "%s: unknown address family", optarg);
232			}
233			break;
234		case 'g':
235			gflag = 1;
236			break;
237		case 'I': {
238			char *cp;
239
240			iflag = 1;
241			for (cp = interface = optarg; isalpha(*cp); cp++)
242				continue;
243			unit = atoi(cp);
244			*cp = '\0';
245			break;
246		}
247		case 'i':
248			iflag = 1;
249			break;
250		case 'M':
251			memf = optarg;
252			break;
253		case 'm':
254			mflag = 1;
255			break;
256		case 'N':
257			nlistf = optarg;
258			break;
259		case 'n':
260			nflag = 1;
261			break;
262		case 'p':
263			if ((tp = name2protox(optarg)) == NULL) {
264				errx(1,
265				     "%s: unknown or uninstrumented protocol",
266				     optarg);
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	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
319	if (kvmd == NULL) {
320		errx(1, "kvm_open: %s", buf);
321	}
322	if (kvm_nlist(kvmd, nl) < 0) {
323		if(nlistf)
324			errx(1, "%s: kvm_nlist: %s", nlistf, kvm_geterr(kvmd));
325		else
326			errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
327	}
328
329	if (nl[0].n_type == 0) {
330		if(nlistf)
331			errx(1, "%s: no namelist", nlistf);
332		else
333			errx(1, "no namelist");
334	}
335	if (mflag) {
336		mbpr(nl[N_MBSTAT].n_value);
337		exit(0);
338	}
339	if (pflag) {
340		if (tp->pr_stats)
341			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
342				tp->pr_name);
343		else
344			printf("%s: no stats routine\n", tp->pr_name);
345		exit(0);
346	}
347	/*
348	 * Keep file descriptors open to avoid overhead
349	 * of open/close on each call to get* routines.
350	 */
351	sethostent(1);
352	setnetent(1);
353	if (iflag) {
354		intpr(interval, nl[N_IFNET].n_value);
355		exit(0);
356	}
357	if (rflag) {
358		if (sflag)
359			rt_stats(nl[N_RTSTAT].n_value);
360		else
361			routepr(nl[N_RTREE].n_value);
362		exit(0);
363	}
364	if (gflag) {
365		if (sflag)
366			mrt_stats(nl[N_MRTPROTO].n_value,
367			    nl[N_MRTSTAT].n_value);
368		else
369			mroutepr(nl[N_MRTPROTO].n_value,
370			    nl[N_MRTTABLE].n_value,
371			    nl[N_VIFTABLE].n_value);
372		exit(0);
373	}
374	if (af == AF_INET || af == AF_UNSPEC) {
375		setprotoent(1);
376		setservent(1);
377		/* ugh, this is O(MN) ... why do we do this? */
378		while (p = getprotoent()) {
379			for (tp = protox; tp->pr_name; tp++)
380				if (strcmp(tp->pr_name, p->p_name) == 0)
381					break;
382			if (tp->pr_name == 0 || tp->pr_wanted == 0)
383				continue;
384			printproto(tp, p->p_name);
385		}
386		endprotoent();
387	}
388	if (af == AF_NS || af == AF_UNSPEC)
389		for (tp = nsprotox; tp->pr_name; tp++)
390			printproto(tp, tp->pr_name);
391	if (af == AF_ISO || af == AF_UNSPEC)
392		for (tp = isoprotox; tp->pr_name; tp++)
393			printproto(tp, tp->pr_name);
394	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
395		unixpr(nl[N_UNIXSW].n_value);
396	exit(0);
397}
398
399/*
400 * Print out protocol statistics or control blocks (per sflag).
401 * If the interface was not specifically requested, and the symbol
402 * is not in the namelist, ignore this one.
403 */
404static void
405printproto(tp, name)
406	register struct protox *tp;
407	char *name;
408{
409	void (*pr)();
410	u_long off;
411
412	if (sflag) {
413		pr = tp->pr_stats;
414		off = nl[tp->pr_sindex].n_value;
415	} else {
416		pr = tp->pr_cblocks;
417		off = nl[tp->pr_index].n_value;
418	}
419	if (pr != NULL && (off || af != AF_UNSPEC))
420		(*pr)(off, name);
421}
422
423/*
424 * Read kernel memory, return 0 on success.
425 */
426int
427kread(addr, buf, size)
428	u_long addr;
429	char *buf;
430	int size;
431{
432
433	if (kvm_read(kvmd, addr, buf, size) != size) {
434		warnx("kvm_read: %s", kvm_geterr(kvmd));
435		return (-1);
436	}
437	return (0);
438}
439
440char *
441plural(n)
442	int n;
443{
444	return (n != 1 ? "s" : "");
445}
446
447char *
448plurales(n)
449	int n;
450{
451	return (n != 1 ? "es" : "");
452}
453
454/*
455 * Find the protox for the given "well-known" name.
456 */
457static struct protox *
458knownname(name)
459	char *name;
460{
461	struct protox **tpp, *tp;
462
463	for (tpp = protoprotox; *tpp; tpp++)
464		for (tp = *tpp; tp->pr_name; tp++)
465			if (strcmp(tp->pr_name, name) == 0)
466				return (tp);
467	return (NULL);
468}
469
470/*
471 * Find the protox corresponding to name.
472 */
473static struct protox *
474name2protox(name)
475	char *name;
476{
477	struct protox *tp;
478	char **alias;			/* alias from p->aliases */
479	struct protoent *p;
480
481	/*
482	 * Try to find the name in the list of "well-known" names. If that
483	 * fails, check if name is an alias for an Internet protocol.
484	 */
485	if (tp = knownname(name))
486		return (tp);
487
488	setprotoent(1);			/* make protocol lookup cheaper */
489	while (p = getprotoent()) {
490		/* assert: name not same as p->name */
491		for (alias = p->p_aliases; *alias; alias++)
492			if (strcmp(name, *alias) == 0) {
493				endprotoent();
494				return (knownname(p->p_name));
495			}
496	}
497	endprotoent();
498	return (NULL);
499}
500
501static void
502usage()
503{
504	(void)fprintf(stderr,
505"usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", prog);
506	(void)fprintf(stderr,
507"       %s [-bdghimnrs] [-f address_family] [-M core] [-N system]\n", prog);
508	(void)fprintf(stderr,
509"       %s [-bdn] [-I interface] [-M core] [-N system] [-w wait]\n", prog);
510	(void)fprintf(stderr,
511"       %s [-M core] [-N system] [-p protocol]\n", prog);
512	exit(1);
513}
514