xref: /freebsd/usr.bin/netstat/main.c (revision 05c7a37afb48ddd5ee1bd921a5d46fe59cc70b15)
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[] = "@(#)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 
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 	{ "_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 
139 struct 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 
161 struct 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 
172 #ifdef NS
173 struct protox nsprotox[] = {
174 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
175 	  idp_stats,	"idp" },
176 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
177 	  spp_stats,	"spp" },
178 	{ -1,		N_NSERR,	1,	0,
179 	  nserr_stats,	"ns_err" },
180 	{ -1,		-1,		0,	0,
181 	  0,		0 }
182 };
183 #endif
184 
185 #ifdef ISO
186 struct protox isoprotox[] = {
187 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
188 	  tp_stats,	"tp" },
189 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
190 	  cltp_stats,	"cltp" },
191 	{ -1,		N_CLNPSTAT,	1,	 0,
192 	  clnp_stats,	"clnp"},
193 	{ -1,		N_ESISSTAT,	1,	 0,
194 	  esis_stats,	"esis"},
195 	{ -1,		-1,		0,	0,
196 	  0,		0 }
197 };
198 #endif
199 
200 struct protox *protoprotox[] = { protox, ipxprotox,
201 #ifdef NS
202 					 nsprotox,
203 #endif
204 #ifdef ISO
205 					 isoprotox,
206 #endif
207 					 NULL };
208 
209 static void printproto __P((struct protox *, char *));
210 static void usage __P((void));
211 static struct protox *name2protox __P((char *));
212 static struct protox *knownname __P((char *));
213 
214 kvm_t *kvmd;
215 
216 int
217 main(argc, argv)
218 	int argc;
219 	char *argv[];
220 {
221 	extern char *optarg;
222 	extern int optind;
223 	register struct protoent *p;
224 	register struct protox *tp;	/* for printing cblocks & stats */
225 	register char *cp;
226 	int ch;
227 	char *nlistf = NULL, *memf = NULL;
228 	char buf[_POSIX2_LINE_MAX];
229 	char buf2[_POSIX2_LINE_MAX];
230 
231 	if (cp = rindex(argv[0], '/'))
232 		prog = cp + 1;
233 	else
234 		prog = argv[0];
235 	af = AF_UNSPEC;
236 
237 	while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:np:rstuw:")) != EOF)
238 		switch(ch) {
239 		case 'A':
240 			Aflag = 1;
241 			break;
242 		case 'a':
243 			aflag = 1;
244 			break;
245 		case 'b':
246 			bflag = 1;
247 			break;
248 		case 'd':
249 			dflag = 1;
250 			break;
251 		case 'f':
252 #ifdef NS
253 			if (strcmp(optarg, "ns") == 0)
254 				af = AF_NS;
255 			else
256 #endif
257 			if (strcmp(optarg, "ipx") == 0)
258 				af = AF_IPX;
259 			else if (strcmp(optarg, "inet") == 0)
260 				af = AF_INET;
261 			else if (strcmp(optarg, "unix") == 0)
262 				af = AF_UNIX;
263 #ifdef ISO
264 			else if (strcmp(optarg, "iso") == 0)
265 				af = AF_ISO;
266 #endif
267 			else {
268 				errx(1, "%s: unknown address family", optarg);
269 			}
270 			break;
271 		case 'g':
272 			gflag = 1;
273 			break;
274 		case 'I': {
275 			char *cp;
276 
277 			iflag = 1;
278 			for (cp = interface = optarg; isalpha(*cp); cp++)
279 				continue;
280 			unit = atoi(cp);
281 			break;
282 		}
283 		case 'i':
284 			iflag = 1;
285 			break;
286 		case 'M':
287 			memf = optarg;
288 			break;
289 		case 'm':
290 			mflag = 1;
291 			break;
292 		case 'N':
293 			nlistf = optarg;
294 			break;
295 		case 'n':
296 			nflag = 1;
297 			break;
298 		case 'p':
299 			if ((tp = name2protox(optarg)) == NULL) {
300 				errx(1,
301 				     "%s: unknown or uninstrumented protocol",
302 				     optarg);
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 	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
355 	if (kvmd == NULL) {
356 		errx(1, "kvm_open: %s", buf);
357 	}
358 	if (kvm_nlist(kvmd, nl) < 0) {
359 		if(nlistf)
360 			errx(1, "%s: kvm_nlist: %s", nlistf, kvm_geterr(kvmd));
361 		else
362 			errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
363 	}
364 
365 	if (nl[0].n_type == 0) {
366 		if(nlistf)
367 			errx(1, "%s: no namelist", nlistf);
368 		else
369 			errx(1, "no namelist");
370 	}
371 	if (mflag) {
372 		mbpr(nl[N_MBSTAT].n_value);
373 		exit(0);
374 	}
375 	if (pflag) {
376 		if (tp->pr_stats)
377 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
378 				tp->pr_name);
379 		else
380 			printf("%s: no stats routine\n", tp->pr_name);
381 		exit(0);
382 	}
383 #if 0
384 	/*
385 	 * Keep file descriptors open to avoid overhead
386 	 * of open/close on each call to get* routines.
387 	 */
388 	sethostent(1);
389 	setnetent(1);
390 #else
391 	/*
392 	 * This does not make sense any more with DNS being default over
393 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
394 	 * used for the queries, which is slower.
395 	 */
396 #endif
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_MFCTABLE].n_value,
415 			    nl[N_VIFTABLE].n_value);
416 		exit(0);
417 	}
418 	if (af == AF_INET || af == AF_UNSPEC) {
419 		setprotoent(1);
420 		setservent(1);
421 		/* ugh, this is O(MN) ... why do we do this? */
422 		while (p = getprotoent()) {
423 			for (tp = protox; tp->pr_name; tp++)
424 				if (strcmp(tp->pr_name, p->p_name) == 0)
425 					break;
426 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
427 				continue;
428 			printproto(tp, p->p_name);
429 		}
430 		endprotoent();
431 	}
432 	if (af == AF_IPX || af == AF_UNSPEC)
433 		for (tp = ipxprotox; tp->pr_name; tp++)
434 			printproto(tp, tp->pr_name);
435 #ifdef NS
436 	if (af == AF_NS || af == AF_UNSPEC)
437 		for (tp = nsprotox; tp->pr_name; tp++)
438 			printproto(tp, tp->pr_name);
439 #endif
440 #ifdef ISO
441 	if (af == AF_ISO || af == AF_UNSPEC)
442 		for (tp = isoprotox; tp->pr_name; tp++)
443 			printproto(tp, tp->pr_name);
444 #endif
445 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
446 		unixpr(nl[N_UNIXSW].n_value);
447 	exit(0);
448 }
449 
450 /*
451  * Print out protocol statistics or control blocks (per sflag).
452  * If the interface was not specifically requested, and the symbol
453  * is not in the namelist, ignore this one.
454  */
455 static void
456 printproto(tp, name)
457 	register struct protox *tp;
458 	char *name;
459 {
460 	void (*pr)();
461 	u_long off;
462 
463 	if (sflag) {
464 		pr = tp->pr_stats;
465 		off = nl[tp->pr_sindex].n_value;
466 	} else {
467 		pr = tp->pr_cblocks;
468 		off = nl[tp->pr_index].n_value;
469 	}
470 	if (pr != NULL && (off || af != AF_UNSPEC))
471 		(*pr)(off, name);
472 }
473 
474 /*
475  * Read kernel memory, return 0 on success.
476  */
477 int
478 kread(addr, buf, size)
479 	u_long addr;
480 	char *buf;
481 	int size;
482 {
483 
484 	if (kvm_read(kvmd, addr, buf, size) != size) {
485 		warnx("%s", kvm_geterr(kvmd));
486 		return (-1);
487 	}
488 	return (0);
489 }
490 
491 char *
492 plural(n)
493 	int n;
494 {
495 	return (n != 1 ? "s" : "");
496 }
497 
498 char *
499 plurales(n)
500 	int n;
501 {
502 	return (n != 1 ? "es" : "");
503 }
504 
505 /*
506  * Find the protox for the given "well-known" name.
507  */
508 static struct protox *
509 knownname(name)
510 	char *name;
511 {
512 	struct protox **tpp, *tp;
513 
514 	for (tpp = protoprotox; *tpp; tpp++)
515 		for (tp = *tpp; tp->pr_name; tp++)
516 			if (strcmp(tp->pr_name, name) == 0)
517 				return (tp);
518 	return (NULL);
519 }
520 
521 /*
522  * Find the protox corresponding to name.
523  */
524 static struct protox *
525 name2protox(name)
526 	char *name;
527 {
528 	struct protox *tp;
529 	char **alias;			/* alias from p->aliases */
530 	struct protoent *p;
531 
532 	/*
533 	 * Try to find the name in the list of "well-known" names. If that
534 	 * fails, check if name is an alias for an Internet protocol.
535 	 */
536 	if (tp = knownname(name))
537 		return (tp);
538 
539 	setprotoent(1);			/* make protocol lookup cheaper */
540 	while (p = getprotoent()) {
541 		/* assert: name not same as p->name */
542 		for (alias = p->p_aliases; *alias; alias++)
543 			if (strcmp(name, *alias) == 0) {
544 				endprotoent();
545 				return (knownname(p->p_name));
546 			}
547 	}
548 	endprotoent();
549 	return (NULL);
550 }
551 
552 static void
553 usage()
554 {
555 	(void)fprintf(stderr,
556 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", prog);
557 	(void)fprintf(stderr,
558 "       %s [-bdghimnrs] [-f address_family] [-M core] [-N system]\n", prog);
559 	(void)fprintf(stderr,
560 "       %s [-bdn] [-I interface] [-M core] [-N system] [-w wait]\n", prog);
561 	(void)fprintf(stderr,
562 "       %s [-M core] [-N system] [-p protocol]\n", prog);
563 	exit(1);
564 }
565 
566 void
567 trimdomain(cp)
568 	char *cp;
569 {
570 	static char domain[MAXHOSTNAMELEN + 1];
571 	static int first = 1;
572 	char *s;
573 
574 	if (first) {
575 		first = 0;
576 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
577 		    (s = strchr(domain, '.')))
578 			(void) strcpy(domain, s + 1);
579 		else
580 			domain[0] = 0;
581 	}
582 
583 	if (domain[0]) {
584 		while ((cp = strchr(cp, '.'))) {
585 			if (!strcasecmp(cp + 1, domain)) {
586 				*cp = 0;	/* hit it */
587 				break;
588 			} else {
589 				cp++;
590 			}
591 		}
592 	}
593 }
594 
595