main.c revision 13433
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#if 0
368	/*
369	 * Keep file descriptors open to avoid overhead
370	 * of open/close on each call to get* routines.
371	 */
372	sethostent(1);
373	setnetent(1);
374#else
375	/*
376	 * This does not make sense any more with DNS being default over
377	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
378	 * used for the queries, which is slower.
379	 */
380#endif
381	if (iflag) {
382		intpr(interval, nl[N_IFNET].n_value);
383		exit(0);
384	}
385	if (rflag) {
386		if (sflag)
387			rt_stats(nl[N_RTSTAT].n_value);
388		else
389			routepr(nl[N_RTREE].n_value);
390		exit(0);
391	}
392	if (gflag) {
393		if (sflag)
394			mrt_stats(nl[N_MRTPROTO].n_value,
395			    nl[N_MRTSTAT].n_value);
396		else
397			mroutepr(nl[N_MRTPROTO].n_value,
398			    nl[N_MFCTABLE].n_value,
399			    nl[N_VIFTABLE].n_value);
400		exit(0);
401	}
402	if (af == AF_INET || af == AF_UNSPEC) {
403		setprotoent(1);
404		setservent(1);
405		/* ugh, this is O(MN) ... why do we do this? */
406		while (p = getprotoent()) {
407			for (tp = protox; tp->pr_name; tp++)
408				if (strcmp(tp->pr_name, p->p_name) == 0)
409					break;
410			if (tp->pr_name == 0 || tp->pr_wanted == 0)
411				continue;
412			printproto(tp, p->p_name);
413		}
414		endprotoent();
415	}
416	if (af == AF_IPX || af == AF_UNSPEC)
417		for (tp = ipxprotox; tp->pr_name; tp++)
418			printproto(tp, tp->pr_name);
419	if (af == AF_NS || af == AF_UNSPEC)
420		for (tp = nsprotox; tp->pr_name; tp++)
421			printproto(tp, tp->pr_name);
422	if (af == AF_ISO || af == AF_UNSPEC)
423		for (tp = isoprotox; tp->pr_name; tp++)
424			printproto(tp, tp->pr_name);
425	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
426		unixpr(nl[N_UNIXSW].n_value);
427	exit(0);
428}
429
430/*
431 * Print out protocol statistics or control blocks (per sflag).
432 * If the interface was not specifically requested, and the symbol
433 * is not in the namelist, ignore this one.
434 */
435static void
436printproto(tp, name)
437	register struct protox *tp;
438	char *name;
439{
440	void (*pr)();
441	u_long off;
442
443	if (sflag) {
444		pr = tp->pr_stats;
445		off = nl[tp->pr_sindex].n_value;
446	} else {
447		pr = tp->pr_cblocks;
448		off = nl[tp->pr_index].n_value;
449	}
450	if (pr != NULL && (off || af != AF_UNSPEC))
451		(*pr)(off, name);
452}
453
454/*
455 * Read kernel memory, return 0 on success.
456 */
457int
458kread(addr, buf, size)
459	u_long addr;
460	char *buf;
461	int size;
462{
463
464	if (kvm_read(kvmd, addr, buf, size) != size) {
465		warnx("%s", kvm_geterr(kvmd));
466		return (-1);
467	}
468	return (0);
469}
470
471char *
472plural(n)
473	int n;
474{
475	return (n != 1 ? "s" : "");
476}
477
478char *
479plurales(n)
480	int n;
481{
482	return (n != 1 ? "es" : "");
483}
484
485/*
486 * Find the protox for the given "well-known" name.
487 */
488static struct protox *
489knownname(name)
490	char *name;
491{
492	struct protox **tpp, *tp;
493
494	for (tpp = protoprotox; *tpp; tpp++)
495		for (tp = *tpp; tp->pr_name; tp++)
496			if (strcmp(tp->pr_name, name) == 0)
497				return (tp);
498	return (NULL);
499}
500
501/*
502 * Find the protox corresponding to name.
503 */
504static struct protox *
505name2protox(name)
506	char *name;
507{
508	struct protox *tp;
509	char **alias;			/* alias from p->aliases */
510	struct protoent *p;
511
512	/*
513	 * Try to find the name in the list of "well-known" names. If that
514	 * fails, check if name is an alias for an Internet protocol.
515	 */
516	if (tp = knownname(name))
517		return (tp);
518
519	setprotoent(1);			/* make protocol lookup cheaper */
520	while (p = getprotoent()) {
521		/* assert: name not same as p->name */
522		for (alias = p->p_aliases; *alias; alias++)
523			if (strcmp(name, *alias) == 0) {
524				endprotoent();
525				return (knownname(p->p_name));
526			}
527	}
528	endprotoent();
529	return (NULL);
530}
531
532static void
533usage()
534{
535	(void)fprintf(stderr,
536"usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", prog);
537	(void)fprintf(stderr,
538"       %s [-bdghimnrs] [-f address_family] [-M core] [-N system]\n", prog);
539	(void)fprintf(stderr,
540"       %s [-bdn] [-I interface] [-M core] [-N system] [-w wait]\n", prog);
541	(void)fprintf(stderr,
542"       %s [-M core] [-N system] [-p protocol]\n", prog);
543	exit(1);
544}
545
546void
547trimdomain(cp)
548	char *cp;
549{
550	static char domain[MAXHOSTNAMELEN + 1];
551	static int first = 1;
552	char *s;
553
554	if (first) {
555		first = 0;
556		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
557		    (s = strchr(domain, '.')))
558			(void) strcpy(domain, s + 1);
559		else
560			domain[0] = 0;
561	}
562
563	if (domain[0]) {
564		while ((cp = strchr(cp, '.'))) {
565			if (!strcasecmp(cp + 1, domain)) {
566				*cp = 0;	/* hit it */
567				break;
568			} else {
569				cp++;
570			}
571		}
572	}
573}
574
575