xref: /freebsd/usr.bin/netstat/main.c (revision ce834215a70ff69e7e222827437116eee2f9ac6f)
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_DDPSTAT	34
135 	{ "_ddpstat"},
136 #define N_DDPCB		35
137 	{ "_ddpcb"},
138 #define N_DIVPCB	36
139 	{ "_divcb"},
140 #define N_DIVSTAT	37
141 	{ "_divstat"},
142 	{ "" },
143 };
144 
145 struct protox {
146 	u_char	pr_index;		/* index into nlist of cb head */
147 	u_char	pr_sindex;		/* index into nlist of stat block */
148 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
149 	void	(*pr_cblocks)();	/* control blocks printing routine */
150 	void	(*pr_stats)();		/* statistics printing routine */
151 	char	*pr_name;		/* well-known name */
152 } protox[] = {
153 	{ N_TCB,	N_TCPSTAT,	1,	protopr,
154 	  tcp_stats,	"tcp" },
155 	{ N_UDB,	N_UDPSTAT,	1,	protopr,
156 	  udp_stats,	"udp" },
157 	{ N_DIVPCB,	N_DIVSTAT,	1,	protopr,
158 	  NULL,		"divert" }, 	/* no stat structure yet */
159 	{ -1,		N_IPSTAT,	1,	0,
160 	  ip_stats,	"ip" },
161 	{ -1,		N_ICMPSTAT,	1,	0,
162 	  icmp_stats,	"icmp" },
163 	{ -1,		N_IGMPSTAT,	1,	0,
164 	  igmp_stats,	"igmp" },
165 	{ -1,		-1,		0,	0,
166 	  0,		0 }
167 };
168 
169 struct protox atalkprotox[] = {
170 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
171 	  ddp_stats,	"ddp" },
172 	{ -1,		-1,		0,	0,
173 	  0,		0 }
174 };
175 
176 struct protox ipxprotox[] = {
177 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
178 	  ipx_stats,	"ipx" },
179 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
180 	  spx_stats,	"spx" },
181 	{ -1,		-1,		0,	0,
182 	  0,		0 }
183 };
184 
185 #ifdef NS
186 struct protox nsprotox[] = {
187 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
188 	  idp_stats,	"idp" },
189 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
190 	  spp_stats,	"spp" },
191 	{ -1,		N_NSERR,	1,	0,
192 	  nserr_stats,	"ns_err" },
193 	{ -1,		-1,		0,	0,
194 	  0,		0 }
195 };
196 #endif
197 
198 #ifdef ISO
199 struct protox isoprotox[] = {
200 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
201 	  tp_stats,	"tp" },
202 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
203 	  cltp_stats,	"cltp" },
204 	{ -1,		N_CLNPSTAT,	1,	 0,
205 	  clnp_stats,	"clnp"},
206 	{ -1,		N_ESISSTAT,	1,	 0,
207 	  esis_stats,	"esis"},
208 	{ -1,		-1,		0,	0,
209 	  0,		0 }
210 };
211 #endif
212 
213 struct protox *protoprotox[] = { protox, ipxprotox, atalkprotox,
214 #ifdef NS
215 					 nsprotox,
216 #endif
217 #ifdef ISO
218 					 isoprotox,
219 #endif
220 					 NULL };
221 
222 static void printproto __P((struct protox *, char *));
223 static void usage __P((void));
224 static struct protox *name2protox __P((char *));
225 static struct protox *knownname __P((char *));
226 
227 kvm_t *kvmd;
228 
229 int
230 main(argc, argv)
231 	int argc;
232 	char *argv[];
233 {
234 	extern char *optarg;
235 	extern int optind;
236 	register struct protoent *p;
237 	register struct protox *tp;	/* for printing cblocks & stats */
238 	register char *cp;
239 	int ch;
240 	char *nlistf = NULL, *memf = NULL;
241 	char buf[_POSIX2_LINE_MAX];
242 
243 	if ((cp = rindex(argv[0], '/')))
244 		prog = cp + 1;
245 	else
246 		prog = argv[0];
247 	af = AF_UNSPEC;
248 
249 	while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:np:rstuw:")) != -1)
250 		switch(ch) {
251 		case 'A':
252 			Aflag = 1;
253 			break;
254 		case 'a':
255 			aflag = 1;
256 			break;
257 		case 'b':
258 			bflag = 1;
259 			break;
260 		case 'd':
261 			dflag = 1;
262 			break;
263 		case 'f':
264 #ifdef NS
265 			if (strcmp(optarg, "ns") == 0)
266 				af = AF_NS;
267 			else
268 #endif
269 			if (strcmp(optarg, "ipx") == 0)
270 				af = AF_IPX;
271 			else if (strcmp(optarg, "inet") == 0)
272 				af = AF_INET;
273 			else if (strcmp(optarg, "unix") == 0)
274 				af = AF_UNIX;
275 			else if (strcmp(optarg, "atalk") == 0)
276 				af = AF_APPLETALK;
277 #ifdef ISO
278 			else if (strcmp(optarg, "iso") == 0)
279 				af = AF_ISO;
280 #endif
281 			else {
282 				errx(1, "%s: unknown address family", optarg);
283 			}
284 			break;
285 		case 'g':
286 			gflag = 1;
287 			break;
288 		case 'I': {
289 			char *cp;
290 
291 			iflag = 1;
292 			for (cp = interface = optarg; isalpha(*cp); cp++)
293 				continue;
294 			unit = atoi(cp);
295 			break;
296 		}
297 		case 'i':
298 			iflag = 1;
299 			break;
300 		case 'M':
301 			memf = optarg;
302 			break;
303 		case 'm':
304 			mflag = 1;
305 			break;
306 		case 'N':
307 			nlistf = optarg;
308 			break;
309 		case 'n':
310 			nflag = 1;
311 			break;
312 		case 'p':
313 			if ((tp = name2protox(optarg)) == NULL) {
314 				errx(1,
315 				     "%s: unknown or uninstrumented protocol",
316 				     optarg);
317 			}
318 			pflag = 1;
319 			break;
320 		case 'r':
321 			rflag = 1;
322 			break;
323 		case 's':
324 			++sflag;
325 			break;
326 		case 't':
327 			tflag = 1;
328 			break;
329 		case 'u':
330 			af = AF_UNIX;
331 			break;
332 		case 'w':
333 			interval = atoi(optarg);
334 			iflag = 1;
335 			break;
336 		case '?':
337 		default:
338 			usage();
339 		}
340 	argv += optind;
341 	argc -= optind;
342 
343 #define	BACKWARD_COMPATIBILITY
344 #ifdef	BACKWARD_COMPATIBILITY
345 	if (*argv) {
346 		if (isdigit(**argv)) {
347 			interval = atoi(*argv);
348 			if (interval <= 0)
349 				usage();
350 			++argv;
351 			iflag = 1;
352 		}
353 		if (*argv) {
354 			nlistf = *argv;
355 			if (*++argv)
356 				memf = *argv;
357 		}
358 	}
359 #endif
360 
361 	/*
362 	 * Discard setgid privileges if not the running kernel so that bad
363 	 * guys can't print interesting stuff from kernel memory.
364 	 */
365 	if (nlistf != NULL || memf != NULL)
366 		setgid(getgid());
367 
368 	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
369 	if (kvmd == NULL) {
370 		errx(1, "kvm_open: %s", buf);
371 	}
372 	if (kvm_nlist(kvmd, nl) < 0) {
373 		if(nlistf)
374 			errx(1, "%s: kvm_nlist: %s", nlistf, kvm_geterr(kvmd));
375 		else
376 			errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
377 	}
378 
379 	if (nl[0].n_type == 0) {
380 		if(nlistf)
381 			errx(1, "%s: no namelist", nlistf);
382 		else
383 			errx(1, "no namelist");
384 	}
385 	if (mflag) {
386 		mbpr(nl[N_MBSTAT].n_value);
387 		exit(0);
388 	}
389 	if (pflag) {
390 		if (tp->pr_stats)
391 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
392 				tp->pr_name);
393 		else
394 			printf("%s: no stats routine\n", tp->pr_name);
395 		exit(0);
396 	}
397 #if 0
398 	/*
399 	 * Keep file descriptors open to avoid overhead
400 	 * of open/close on each call to get* routines.
401 	 */
402 	sethostent(1);
403 	setnetent(1);
404 #else
405 	/*
406 	 * This does not make sense any more with DNS being default over
407 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
408 	 * used for the queries, which is slower.
409 	 */
410 #endif
411 	if (iflag) {
412 		intpr(interval, nl[N_IFNET].n_value);
413 		exit(0);
414 	}
415 	if (rflag) {
416 		if (sflag)
417 			rt_stats(nl[N_RTSTAT].n_value);
418 		else
419 			routepr(nl[N_RTREE].n_value);
420 		exit(0);
421 	}
422 	if (gflag) {
423 		if (sflag)
424 			mrt_stats(nl[N_MRTPROTO].n_value,
425 			    nl[N_MRTSTAT].n_value);
426 		else
427 			mroutepr(nl[N_MRTPROTO].n_value,
428 			    nl[N_MFCTABLE].n_value,
429 			    nl[N_VIFTABLE].n_value);
430 		exit(0);
431 	}
432 	if (af == AF_INET || af == AF_UNSPEC) {
433 		setprotoent(1);
434 		setservent(1);
435 		/* ugh, this is O(MN) ... why do we do this? */
436 		while ((p = getprotoent())) {
437 			for (tp = protox; tp->pr_name; tp++)
438 				if (strcmp(tp->pr_name, p->p_name) == 0)
439 					break;
440 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
441 				continue;
442 			printproto(tp, p->p_name);
443 		}
444 		endprotoent();
445 	}
446 	if (af == AF_IPX || af == AF_UNSPEC)
447 		for (tp = ipxprotox; tp->pr_name; tp++)
448 			printproto(tp, tp->pr_name);
449 	if (af == AF_APPLETALK || af == AF_UNSPEC)
450 		for (tp = atalkprotox; tp->pr_name; tp++)
451 			printproto(tp, tp->pr_name);
452 #ifdef NS
453 	if (af == AF_NS || af == AF_UNSPEC)
454 		for (tp = nsprotox; tp->pr_name; tp++)
455 			printproto(tp, tp->pr_name);
456 #endif
457 #ifdef ISO
458 	if (af == AF_ISO || af == AF_UNSPEC)
459 		for (tp = isoprotox; tp->pr_name; tp++)
460 			printproto(tp, tp->pr_name);
461 #endif
462 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
463 		unixpr(nl[N_UNIXSW].n_value);
464 	exit(0);
465 }
466 
467 /*
468  * Print out protocol statistics or control blocks (per sflag).
469  * If the interface was not specifically requested, and the symbol
470  * is not in the namelist, ignore this one.
471  */
472 static void
473 printproto(tp, name)
474 	register struct protox *tp;
475 	char *name;
476 {
477 	void (*pr)();
478 	u_long off;
479 
480 	if (sflag) {
481 		pr = tp->pr_stats;
482 		off = nl[tp->pr_sindex].n_value;
483 	} else {
484 		pr = tp->pr_cblocks;
485 		off = nl[tp->pr_index].n_value;
486 	}
487 	if (pr != NULL && (off || af != AF_UNSPEC))
488 		(*pr)(off, name);
489 }
490 
491 /*
492  * Read kernel memory, return 0 on success.
493  */
494 int
495 kread(addr, buf, size)
496 	u_long addr;
497 	char *buf;
498 	int size;
499 {
500 
501 	if (kvm_read(kvmd, addr, buf, size) != size) {
502 		warnx("%s", kvm_geterr(kvmd));
503 		return (-1);
504 	}
505 	return (0);
506 }
507 
508 char *
509 plural(n)
510 	int n;
511 {
512 	return (n != 1 ? "s" : "");
513 }
514 
515 char *
516 plurales(n)
517 	int n;
518 {
519 	return (n != 1 ? "es" : "");
520 }
521 
522 /*
523  * Find the protox for the given "well-known" name.
524  */
525 static struct protox *
526 knownname(name)
527 	char *name;
528 {
529 	struct protox **tpp, *tp;
530 
531 	for (tpp = protoprotox; *tpp; tpp++)
532 		for (tp = *tpp; tp->pr_name; tp++)
533 			if (strcmp(tp->pr_name, name) == 0)
534 				return (tp);
535 	return (NULL);
536 }
537 
538 /*
539  * Find the protox corresponding to name.
540  */
541 static struct protox *
542 name2protox(name)
543 	char *name;
544 {
545 	struct protox *tp;
546 	char **alias;			/* alias from p->aliases */
547 	struct protoent *p;
548 
549 	/*
550 	 * Try to find the name in the list of "well-known" names. If that
551 	 * fails, check if name is an alias for an Internet protocol.
552 	 */
553 	if ((tp = knownname(name)))
554 		return (tp);
555 
556 	setprotoent(1);			/* make protocol lookup cheaper */
557 	while ((p = getprotoent())) {
558 		/* assert: name not same as p->name */
559 		for (alias = p->p_aliases; *alias; alias++)
560 			if (strcmp(name, *alias) == 0) {
561 				endprotoent();
562 				return (knownname(p->p_name));
563 			}
564 	}
565 	endprotoent();
566 	return (NULL);
567 }
568 
569 static void
570 usage()
571 {
572 	(void)fprintf(stderr,
573 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", prog);
574 	(void)fprintf(stderr,
575 "       %s [-bdghimnrs] [-f address_family] [-M core] [-N system]\n", prog);
576 	(void)fprintf(stderr,
577 "       %s [-bdn] [-I interface] [-M core] [-N system] [-w wait]\n", prog);
578 	(void)fprintf(stderr,
579 "       %s [-M core] [-N system] [-p protocol]\n", prog);
580 	exit(1);
581 }
582 
583 void
584 trimdomain(cp)
585 	char *cp;
586 {
587 	static char domain[MAXHOSTNAMELEN + 1];
588 	static int first = 1;
589 	char *s;
590 
591 	if (first) {
592 		first = 0;
593 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
594 		    (s = strchr(domain, '.')))
595 			(void) strcpy(domain, s + 1);
596 		else
597 			domain[0] = 0;
598 	}
599 
600 	if (domain[0]) {
601 		while ((cp = strchr(cp, '.'))) {
602 			if (!strcasecmp(cp + 1, domain)) {
603 				*cp = 0;	/* hit it */
604 				break;
605 			} else {
606 				cp++;
607 			}
608 		}
609 	}
610 }
611 
612