xref: /freebsd/usr.bin/netstat/main.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
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 const 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 #if 0
42 static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 3/1/94";
43 #endif
44 static const char rcsid[] =
45 	"$Id: main.c,v 1.21 1998/08/05 13:54:07 phk Exp $";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 #include <sys/file.h>
50 #include <sys/protosw.h>
51 #include <sys/socket.h>
52 
53 #include <netinet/in.h>
54 
55 #include <ctype.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <kvm.h>
59 #include <limits.h>
60 #include <netdb.h>
61 #include <nlist.h>
62 #include <paths.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67 #include "netstat.h"
68 
69 static struct nlist nl[] = {
70 #define	N_IFNET		0
71 	{ "_ifnet" },
72 #define	N_IMP		1
73 	{ "_imp_softc" },
74 #define	N_RTSTAT	2
75 	{ "_rtstat" },
76 #define	N_UNIXSW	3
77 	{ "_localsw" },
78 #define N_IDP		4
79 	{ "_nspcb"},
80 #define N_IDPSTAT	5
81 	{ "_idpstat"},
82 #define N_SPPSTAT	6
83 	{ "_spp_istat"},
84 #define N_NSERR		7
85 	{ "_ns_errstat"},
86 #define	N_CLNPSTAT	8
87 	{ "_clnp_stat"},
88 #define	IN_NOTUSED	9
89 	{ "_tp_inpcb" },
90 #define	ISO_TP		10
91 	{ "_tp_refinfo" },
92 #define	N_TPSTAT	11
93 	{ "_tp_stat" },
94 #define	N_ESISSTAT	12
95 	{ "_esis_stat"},
96 #define N_NIMP		13
97 	{ "_nimp"},
98 #define N_RTREE		14
99 	{ "_rt_tables"},
100 #define N_CLTP		15
101 	{ "_cltb"},
102 #define N_CLTPSTAT	16
103 	{ "_cltpstat"},
104 #define	N_NFILE		17
105 	{ "_nfile" },
106 #define	N_FILE		18
107 	{ "_file" },
108 #define N_MRTPROTO	19
109 	{ "_ip_mrtproto" },
110 #define N_MRTSTAT	20
111 	{ "_mrtstat" },
112 #define N_MFCTABLE	21
113 	{ "_mfctable" },
114 #define N_VIFTABLE	22
115 	{ "_viftable" },
116 #define N_IPX		23
117 	{ "_ipxpcb"},
118 #define N_IPXSTAT	24
119 	{ "_ipxstat"},
120 #define N_SPXSTAT	25
121 	{ "_spx_istat"},
122 #define N_DDPSTAT	26
123 	{ "_ddpstat"},
124 #define N_DDPCB		27
125 	{ "_ddpcb"},
126 	{ "" },
127 };
128 
129 struct protox {
130 	u_char	pr_index;		/* index into nlist of cb head */
131 	u_char	pr_sindex;		/* index into nlist of stat block */
132 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
133 	void	(*pr_cblocks)();	/* control blocks printing routine */
134 	void	(*pr_stats)();		/* statistics printing routine */
135 	char	*pr_name;		/* well-known name */
136 	int	pr_usesysctl;		/* true if we use sysctl, not kvm */
137 } protox[] = {
138 	{ -1,		-1,		1,	protopr,
139 	  tcp_stats,	"tcp",		IPPROTO_TCP },
140 	{ -1,		-1,		1,	protopr,
141 	  udp_stats,	"udp",		IPPROTO_UDP },
142 	{ -1,		-1,		1,	protopr,
143 	  NULL,		"divert",	IPPROTO_DIVERT },
144 	{ -1,		-1,		1,	protopr,
145 	  ip_stats,	"ip",		IPPROTO_RAW },
146 	{ -1,		-1,		1,	protopr,
147 	  icmp_stats,	"icmp",		IPPROTO_ICMP },
148 	{ -1,		-1,		1,	protopr,
149 	  igmp_stats,	"igmp",		IPPROTO_IGMP },
150 	{ -1,		-1,		0,	0,
151 	  0,		0 }
152 };
153 
154 struct protox atalkprotox[] = {
155 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
156 	  ddp_stats,	"ddp" },
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,		-1,		0,	0,
167 	  0,		0 }
168 };
169 
170 #ifdef NS
171 struct protox nsprotox[] = {
172 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
173 	  idp_stats,	"idp" },
174 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
175 	  spp_stats,	"spp" },
176 	{ -1,		N_NSERR,	1,	0,
177 	  nserr_stats,	"ns_err" },
178 	{ -1,		-1,		0,	0,
179 	  0,		0 }
180 };
181 #endif
182 
183 #ifdef ISO
184 struct protox isoprotox[] = {
185 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
186 	  tp_stats,	"tp" },
187 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
188 	  cltp_stats,	"cltp" },
189 	{ -1,		N_CLNPSTAT,	1,	 0,
190 	  clnp_stats,	"clnp"},
191 	{ -1,		N_ESISSTAT,	1,	 0,
192 	  esis_stats,	"esis"},
193 	{ -1,		-1,		0,	0,
194 	  0,		0 }
195 };
196 #endif
197 
198 struct protox *protoprotox[] = { protox, ipxprotox, atalkprotox,
199 #ifdef NS
200 					 nsprotox,
201 #endif
202 #ifdef ISO
203 					 isoprotox,
204 #endif
205 					 NULL };
206 
207 static void printproto __P((struct protox *, char *));
208 static void usage __P((void));
209 static struct protox *name2protox __P((char *));
210 static struct protox *knownname __P((char *));
211 
212 static kvm_t *kvmd;
213 char *nlistf = NULL, *memf = NULL;
214 
215 int
216 main(argc, argv)
217 	int argc;
218 	char *argv[];
219 {
220 	register struct protoent *p;
221 	register struct protox *tp;	/* for printing cblocks & stats */
222 	int ch;
223 
224 	af = AF_UNSPEC;
225 
226 	while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:np:rstuw:")) != -1)
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 #ifdef NS
242 			if (strcmp(optarg, "ns") == 0)
243 				af = AF_NS;
244 			else
245 #endif
246 			if (strcmp(optarg, "ipx") == 0)
247 				af = AF_IPX;
248 			else if (strcmp(optarg, "inet") == 0)
249 				af = AF_INET;
250 			else if (strcmp(optarg, "unix") == 0)
251 				af = AF_UNIX;
252 			else if (strcmp(optarg, "atalk") == 0)
253 				af = AF_APPLETALK;
254 #ifdef ISO
255 			else if (strcmp(optarg, "iso") == 0)
256 				af = AF_ISO;
257 #endif
258 			else {
259 				errx(1, "%s: unknown address family", optarg);
260 			}
261 			break;
262 		case 'g':
263 			gflag = 1;
264 			break;
265 		case 'I': {
266 			char *cp;
267 
268 			iflag = 1;
269 			for (cp = interface = optarg; isalpha(*cp); cp++)
270 				continue;
271 			unit = atoi(cp);
272 			break;
273 		}
274 		case 'i':
275 			iflag = 1;
276 			break;
277 		case 'M':
278 			memf = optarg;
279 			break;
280 		case 'm':
281 			mflag = 1;
282 			break;
283 		case 'N':
284 			nlistf = optarg;
285 			break;
286 		case 'n':
287 			nflag = 1;
288 			break;
289 		case 'p':
290 			if ((tp = name2protox(optarg)) == NULL) {
291 				errx(1,
292 				     "%s: unknown or uninstrumented protocol",
293 				     optarg);
294 			}
295 			pflag = 1;
296 			break;
297 		case 'r':
298 			rflag = 1;
299 			break;
300 		case 's':
301 			++sflag;
302 			break;
303 		case 't':
304 			tflag = 1;
305 			break;
306 		case 'u':
307 			af = AF_UNIX;
308 			break;
309 		case 'w':
310 			interval = atoi(optarg);
311 			iflag = 1;
312 			break;
313 		case '?':
314 		default:
315 			usage();
316 		}
317 	argv += optind;
318 	argc -= optind;
319 
320 #define	BACKWARD_COMPATIBILITY
321 #ifdef	BACKWARD_COMPATIBILITY
322 	if (*argv) {
323 		if (isdigit(**argv)) {
324 			interval = atoi(*argv);
325 			if (interval <= 0)
326 				usage();
327 			++argv;
328 			iflag = 1;
329 		}
330 		if (*argv) {
331 			nlistf = *argv;
332 			if (*++argv)
333 				memf = *argv;
334 		}
335 	}
336 #endif
337 
338 	/*
339 	 * Discard setgid privileges if not the running kernel so that bad
340 	 * guys can't print interesting stuff from kernel memory.
341 	 */
342 	if (nlistf != NULL || memf != NULL)
343 		setgid(getgid());
344 
345 	if (mflag) {
346 		mbpr();
347 		exit(0);
348 	}
349 	if (pflag) {
350 		if (!tp->pr_stats) {
351 			printf("%s: no stats routine\n", tp->pr_name);
352 			exit(0);
353 		}
354 		if (tp->pr_usesysctl) {
355 			(*tp->pr_stats)(tp->pr_usesysctl, tp->pr_name);
356 		} else {
357 			kread(0, 0, 0);
358 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
359 					tp->pr_name);
360 		}
361 		exit(0);
362 	}
363 #if 0
364 	/*
365 	 * Keep file descriptors open to avoid overhead
366 	 * of open/close on each call to get* routines.
367 	 */
368 	sethostent(1);
369 	setnetent(1);
370 #else
371 	/*
372 	 * This does not make sense any more with DNS being default over
373 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
374 	 * used for the queries, which is slower.
375 	 */
376 #endif
377 	if (iflag) {
378 		kread(0, 0, 0);
379 		intpr(interval, nl[N_IFNET].n_value);
380 		exit(0);
381 	}
382 	if (rflag) {
383 		kread(0, 0, 0);
384 		if (sflag)
385 			rt_stats(nl[N_RTSTAT].n_value);
386 		else
387 			routepr(nl[N_RTREE].n_value);
388 		exit(0);
389 	}
390 	if (gflag) {
391 		kread(0, 0, 0);
392 		if (sflag)
393 			mrt_stats(nl[N_MRTPROTO].n_value,
394 			    nl[N_MRTSTAT].n_value);
395 		else
396 			mroutepr(nl[N_MRTPROTO].n_value,
397 			    nl[N_MFCTABLE].n_value,
398 			    nl[N_VIFTABLE].n_value);
399 		exit(0);
400 	}
401 	if (af == AF_INET || af == AF_UNSPEC) {
402 		setprotoent(1);
403 		setservent(1);
404 		/* ugh, this is O(MN) ... why do we do this? */
405 		while ((p = getprotoent())) {
406 			for (tp = protox; tp->pr_name; tp++)
407 				if (strcmp(tp->pr_name, p->p_name) == 0)
408 					break;
409 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
410 				continue;
411 			printproto(tp, p->p_name);
412 		}
413 		endprotoent();
414 	}
415 	if (af == AF_IPX || af == AF_UNSPEC)
416 		for (tp = ipxprotox; tp->pr_name; tp++)
417 			printproto(tp, tp->pr_name);
418 	if (af == AF_APPLETALK || af == AF_UNSPEC)
419 		for (tp = atalkprotox; tp->pr_name; tp++)
420 			printproto(tp, tp->pr_name);
421 #ifdef NS
422 	if (af == AF_NS || af == AF_UNSPEC)
423 		for (tp = nsprotox; tp->pr_name; tp++)
424 			printproto(tp, tp->pr_name);
425 #endif
426 #ifdef ISO
427 	if (af == AF_ISO || af == AF_UNSPEC)
428 		for (tp = isoprotox; tp->pr_name; tp++)
429 			printproto(tp, tp->pr_name);
430 #endif
431 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
432 		unixpr();
433 	exit(0);
434 }
435 
436 /*
437  * Print out protocol statistics or control blocks (per sflag).
438  * If the interface was not specifically requested, and the symbol
439  * is not in the namelist, ignore this one.
440  */
441 static void
442 printproto(tp, name)
443 	register struct protox *tp;
444 	char *name;
445 {
446 	void (*pr)();
447 	u_long off;
448 
449 	if (sflag) {
450 		pr = tp->pr_stats;
451 		off = tp->pr_usesysctl ? tp->pr_usesysctl
452 			: nl[tp->pr_sindex].n_value;
453 	} else {
454 		pr = tp->pr_cblocks;
455 		off = tp->pr_usesysctl ? tp->pr_usesysctl
456 			: nl[tp->pr_index].n_value;
457 	}
458 	if (pr != NULL && (off || af != AF_UNSPEC))
459 		(*pr)(off, name);
460 }
461 
462 /*
463  * Read kernel memory, return 0 on success.
464  */
465 int
466 kread(addr, buf, size)
467 	u_long addr;
468 	char *buf;
469 	int size;
470 {
471 	if (kvmd == 0) {
472 		/*
473 		 * XXX.
474 		 */
475 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
476 		if (kvmd != NULL) {
477 			if (kvm_nlist(kvmd, nl) < 0) {
478 				if(nlistf)
479 					errx(1, "%s: kvm_nlist: %s", nlistf,
480 					     kvm_geterr(kvmd));
481 				else
482 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
483 			}
484 
485 			if (nl[0].n_type == 0) {
486 				if(nlistf)
487 					errx(1, "%s: no namelist", nlistf);
488 				else
489 					errx(1, "no namelist");
490 			}
491 		} else {
492 			warnx("kvm not available");
493 			return(-1);
494 		}
495 	}
496 	if (!buf)
497 		return (0);
498 	if (kvm_read(kvmd, addr, buf, size) != size) {
499 		warnx("%s", kvm_geterr(kvmd));
500 		return (-1);
501 	}
502 	return (0);
503 }
504 
505 char *
506 plural(n)
507 	int n;
508 {
509 	return (n != 1 ? "s" : "");
510 }
511 
512 char *
513 plurales(n)
514 	int n;
515 {
516 	return (n != 1 ? "es" : "");
517 }
518 
519 /*
520  * Find the protox for the given "well-known" name.
521  */
522 static struct protox *
523 knownname(name)
524 	char *name;
525 {
526 	struct protox **tpp, *tp;
527 
528 	for (tpp = protoprotox; *tpp; tpp++)
529 		for (tp = *tpp; tp->pr_name; tp++)
530 			if (strcmp(tp->pr_name, name) == 0)
531 				return (tp);
532 	return (NULL);
533 }
534 
535 /*
536  * Find the protox corresponding to name.
537  */
538 static struct protox *
539 name2protox(name)
540 	char *name;
541 {
542 	struct protox *tp;
543 	char **alias;			/* alias from p->aliases */
544 	struct protoent *p;
545 
546 	/*
547 	 * Try to find the name in the list of "well-known" names. If that
548 	 * fails, check if name is an alias for an Internet protocol.
549 	 */
550 	if ((tp = knownname(name)))
551 		return (tp);
552 
553 	setprotoent(1);			/* make protocol lookup cheaper */
554 	while ((p = getprotoent())) {
555 		/* assert: name not same as p->name */
556 		for (alias = p->p_aliases; *alias; alias++)
557 			if (strcmp(name, *alias) == 0) {
558 				endprotoent();
559 				return (knownname(p->p_name));
560 			}
561 	}
562 	endprotoent();
563 	return (NULL);
564 }
565 
566 static void
567 usage()
568 {
569 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
570 "usage: netstat [-Aan] [-f address_family] [-M core] [-N system]",
571 "       netstat [-bdghimnrs] [-f address_family] [-M core] [-N system]",
572 "       netstat [-bdn] [-I interface] [-M core] [-N system] [-w wait]",
573 "       netstat [-M core] [-N system] [-p protocol]");
574 	exit(1);
575 }
576 
577 void
578 trimdomain(cp)
579 	char *cp;
580 {
581 	static char domain[MAXHOSTNAMELEN + 1];
582 	static int first = 1;
583 	char *s;
584 
585 	if (first) {
586 		first = 0;
587 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
588 		    (s = strchr(domain, '.')))
589 			(void) strcpy(domain, s + 1);
590 		else
591 			domain[0] = 0;
592 	}
593 
594 	if (domain[0]) {
595 		while ((cp = strchr(cp, '.'))) {
596 			if (!strcasecmp(cp + 1, domain)) {
597 				*cp = 0;	/* hit it */
598 				break;
599 			} else {
600 				cp++;
601 			}
602 		}
603 	}
604 }
605 
606