xref: /openbsd/usr.bin/netstat/main.c (revision db3296cf)
1 /*	$OpenBSD: main.c,v 1.38 2003/06/26 21:59:11 deraadt Exp $	*/
2 /*	$NetBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1988, 1993
6  *	Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 char copyright[] =
35 "@(#) Copyright (c) 1983, 1988, 1993\n\
36 	Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "from: @(#)main.c	8.4 (Berkeley) 3/1/94";
42 #else
43 static char *rcsid = "$OpenBSD: main.c,v 1.38 2003/06/26 21:59:11 deraadt Exp $";
44 #endif
45 #endif /* not lint */
46 
47 #include <sys/param.h>
48 #include <sys/file.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 
52 #include <netinet/in.h>
53 
54 #include <ctype.h>
55 #include <errno.h>
56 #include <kvm.h>
57 #include <limits.h>
58 #include <netdb.h>
59 #include <nlist.h>
60 #include <paths.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 #include "netstat.h"
66 
67 struct nlist nl[] = {
68 #define	N_MBSTAT	0
69 	{ "_mbstat" },
70 #define	N_IPSTAT	1
71 	{ "_ipstat" },
72 #define	N_TCBTABLE	2
73 	{ "_tcbtable" },
74 #define	N_TCPSTAT	3
75 	{ "_tcpstat" },
76 #define	N_UDBTABLE	4
77 	{ "_udbtable" },
78 #define	N_UDPSTAT	5
79 	{ "_udpstat" },
80 #define	N_IFNET		6
81 	{ "_ifnet" },
82 #define	N_IMP		7
83 	{ "_imp_softc" },
84 #define	N_ICMPSTAT	8
85 	{ "_icmpstat" },
86 #define	N_RTSTAT	9
87 	{ "_rtstat" },
88 #define	N_UNIXSW	10
89 	{ "_unixsw" },
90 #define N_IDP		11
91 	{ "_nspcb"},
92 #define N_IDPSTAT	12
93 	{ "_idpstat"},
94 #define N_SPPSTAT	13
95 	{ "_spp_istat"},
96 #define N_NSERR		14
97 	{ "_ns_errstat"},
98 #define	N_CLNPSTAT	15
99 	{ "_clnp_stat"},
100 #define	IN_NOTUSED	16
101 	{ "_tp_inpcb" },
102 #define	ISO_TP		17
103 	{ "_tp_refinfo" },
104 #define	N_TPSTAT	18
105 	{ "_tp_stat" },
106 #define	N_ESISSTAT	19
107 	{ "_esis_stat"},
108 #define N_NIMP		20
109 	{ "_nimp"},
110 #define N_RTREE		21
111 	{ "_rt_tables"},
112 #define N_CLTP		22
113 	{ "_cltb"},
114 #define N_CLTPSTAT	23
115 	{ "_cltpstat"},
116 #define	N_NFILE		24
117 	{ "_nfile" },
118 #define	N_FILE		25
119 	{ "_file" },
120 #define N_IGMPSTAT	26
121 	{ "_igmpstat" },
122 #define N_MRTPROTO	27
123 	{ "_ip_mrtproto" },
124 #define N_MRTSTAT	28
125 	{ "_mrtstat" },
126 #define N_MFCHASHTBL	29
127 	{ "_mfchashtbl" },
128 #define	N_MFCHASH	30
129 	{ "_mfchash" },
130 #define N_VIFTABLE	31
131 	{ "_viftable" },
132 #define N_IPX		32
133 	{ "_ipxcbtable"},
134 #define N_IPXSTAT	33
135 	{ "_ipxstat"},
136 #define N_SPXSTAT	34
137 	{ "_spx_istat"},
138 #define N_IPXERR	35
139 	{ "_ipx_errstat"},
140 #define N_AHSTAT	36
141 	{ "_ahstat"},
142 #define N_ESPSTAT	37
143 	{ "_espstat"},
144 #define N_IP4STAT	38
145 	{ "_ipipstat"},
146 #define N_DDPSTAT	39
147 	{ "_ddpstat"},
148 #define N_DDPCB		40
149 	{ "_ddpcb"},
150 #define N_ETHERIPSTAT	41
151 	{ "_etheripstat"},
152 #define N_IP6STAT	42
153 	{ "_ip6stat" },
154 #define N_ICMP6STAT	43
155 	{ "_icmp6stat" },
156 #define N_IPSECSTAT	44
157 	{ "_ipsecstat" },
158 #define N_IPSEC6STAT	45
159 	{ "_ipsec6stat" },
160 #define N_PIM6STAT	46
161 	{ "_pim6stat" },
162 #define N_MRT6PROTO	47
163 	{ "_ip6_mrtproto" },
164 #define N_MRT6STAT	48
165 	{ "_mrt6stat" },
166 #define N_MF6CTABLE	49
167 	{ "_mf6ctable" },
168 #define N_MIF6TABLE	50
169 	{ "_mif6table" },
170 #define N_MBPOOL	51
171 	{ "_mbpool" },
172 #define N_MCLPOOL	52
173 	{ "_mclpool" },
174 #define N_IPCOMPSTAT	53
175 	{ "_ipcompstat" },
176 #define N_RIP6STAT	54
177 	{ "_rip6stat" },
178 	{ ""},
179 };
180 
181 struct protox {
182 	u_char	pr_index;			/* index into nlist of cb head */
183 	u_char	pr_sindex;			/* index into nlist of stat block */
184 	u_char	pr_wanted;			/* 1 if wanted, 0 otherwise */
185 	void	(*pr_cblocks)(u_long, char *);	/* control blocks printing routine */
186 	void	(*pr_stats)(u_long, char *);	/* statistics printing routine */
187 	char	*pr_name;			/* well-known name */
188 } protox[] = {
189 	{ N_TCBTABLE,	N_TCPSTAT,	1,	protopr,
190 	  tcp_stats,	"tcp" },
191 	{ N_UDBTABLE,	N_UDPSTAT,	1,	protopr,
192 	  udp_stats,	"udp" },
193 	{ -1,		N_IPSTAT,	1,	0,
194 	  ip_stats,	"ip" },
195 	{ -1,		N_ICMPSTAT,	1,	0,
196 	  icmp_stats,	"icmp" },
197 	{ -1,		N_IGMPSTAT,	1,	0,
198 	  igmp_stats,	"igmp" },
199 	{ -1,		N_AHSTAT,	1,	0,
200 	  ah_stats,	"ah" },
201 	{ -1,		N_ESPSTAT,	1,	0,
202 	  esp_stats,	"esp" },
203 	{ -1,		N_IP4STAT,	1,	0,
204 	  ipip_stats,	"ipencap" },
205 	{ -1,		N_ETHERIPSTAT,	1,	0,
206 	  etherip_stats,"etherip" },
207 	{ -1,		N_IPCOMPSTAT,	1,	0,
208 	  ipcomp_stats,	"ipcomp" },
209 	{ -1,		-1,		0,	0,
210 	  0,		0 }
211 };
212 
213 #ifdef INET6
214 struct protox ip6protox[] = {
215 	{ N_TCBTABLE,	N_TCPSTAT,	1,	ip6protopr,
216 	  0,		"tcp" },
217 	{ N_UDBTABLE,	N_UDPSTAT,	1,	ip6protopr,
218 	  0,		"udp" },
219 	{ -1,		N_IP6STAT,	1,	0,
220 	  ip6_stats,	"ip6" },
221 	{ -1,		N_ICMP6STAT,	1,	0,
222 	  icmp6_stats,	"icmp6" },
223 	{ -1,		N_PIM6STAT,	1,	0,
224 	  pim6_stats,	"pim6" },
225 	{ -1,		N_RIP6STAT,	1,	0,
226 	  rip6_stats,	"rip6" },
227 	{ -1,		-1,		0,	0,
228 	  0,		0 }
229 };
230 #endif
231 
232 struct protox ipxprotox[] = {
233 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
234 	  ipx_stats,	"ipx" },
235 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
236 	  spx_stats,	"spx" },
237 	{ -1,		-1,		0,	0,
238 	  0,		0 }
239 };
240 
241 struct protox nsprotox[] = {
242 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
243 	  idp_stats,	"idp" },
244 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
245 	  spp_stats,	"spp" },
246 	{ -1,		N_NSERR,	1,	0,
247 	  nserr_stats,	"ns_err" },
248 	{ -1,		-1,		0,	0,
249 	  0,		0 }
250 };
251 
252 struct protox isoprotox[] = {
253 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
254 	  tp_stats,	"tp" },
255 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
256 	  cltp_stats,	"cltp" },
257 	{ -1,		N_CLNPSTAT,	1,	 0,
258 	  clnp_stats,	"clnp"},
259 	{ -1,		N_ESISSTAT,	1,	 0,
260 	  esis_stats,	"esis"},
261 	{ -1,		-1,		0,	0,
262 	  0,		0 }
263 };
264 
265 struct protox atalkprotox[] = {
266 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
267 	  ddp_stats,	"ddp" },
268 	{ -1,		-1,		0,	0,
269 	  0,		0 }
270 };
271 
272 #ifndef INET6
273 struct protox *protoprotox[] = {
274 	protox, ipxprotox, nsprotox, isoprotox, atalkprotox, NULL
275 };
276 #else
277 struct protox *protoprotox[] = {
278 	protox, ip6protox, ipxprotox, nsprotox, isoprotox, atalkprotox, NULL
279 };
280 #endif
281 
282 static void printproto(struct protox *, char *);
283 static void usage(void);
284 static struct protox *name2protox(char *);
285 static struct protox *knownname(char *);
286 
287 kvm_t *kvmd;
288 
289 int
290 main(int argc, char *argv[])
291 {
292 	extern char *optarg;
293 	extern int optind;
294 	struct protoent *p;
295 	struct protox *tp = NULL; /* for printing cblocks & stats */
296 	int ch;
297 	char *nlistf = NULL, *memf = NULL;
298 	char buf[_POSIX2_LINE_MAX];
299 
300 	af = AF_UNSPEC;
301 
302 	while ((ch = getopt(argc, argv, "Aabdf:gI:ilM:mN:np:qrstuvw:")) != -1)
303 		switch (ch) {
304 		case 'A':
305 			Aflag = 1;
306 			break;
307 		case 'a':
308 			aflag = 1;
309 			break;
310 		case 'b':
311 			bflag = 1;
312 			break;
313 		case 'd':
314 			dflag = 1;
315 			break;
316 		case 'f':
317 			if (strcmp(optarg, "inet") == 0)
318 				af = AF_INET;
319 			else if (strcmp(optarg, "inet6") == 0)
320 				af = AF_INET6;
321 			else if (strcmp(optarg, "local") == 0)
322 				af = AF_LOCAL;
323 			else if (strcmp(optarg, "unix") == 0)
324 				af = AF_UNIX;
325 			else if (strcmp(optarg, "ipx") == 0)
326 				af = AF_IPX;
327 			else if (strcmp(optarg, "ns") == 0)
328 				af = AF_NS;
329 			else if (strcmp(optarg, "iso") == 0)
330 				af = AF_ISO;
331 			else if (strcmp(optarg, "encap") == 0)
332 				af = PF_KEY;
333 			else if (strcmp(optarg, "atalk") == 0)
334 				af = AF_APPLETALK;
335 			else {
336 				(void)fprintf(stderr,
337 				    "%s: %s: unknown address family\n",
338 				    __progname, optarg);
339 				exit(1);
340 			}
341 			break;
342 		case 'g':
343 			gflag = 1;
344 			break;
345 		case 'I':
346 			iflag = 1;
347 			interface = optarg;
348 			break;
349 		case 'i':
350 			iflag = 1;
351 			break;
352 		case 'l':
353 			lflag = 1;
354 			break;
355 		case 'M':
356 			memf = optarg;
357 			break;
358 		case 'm':
359 			mflag = 1;
360 			break;
361 		case 'N':
362 			nlistf = optarg;
363 			break;
364 		case 'n':
365 			nflag = 1;
366 			break;
367 		case 'p':
368 			if ((tp = name2protox(optarg)) == NULL) {
369 				(void)fprintf(stderr,
370 				    "%s: %s: unknown or uninstrumented protocol\n",
371 				    __progname, optarg);
372 				exit(1);
373 			}
374 			pflag = 1;
375 			break;
376 		case 'q':
377 			qflag = 1;
378 			break;
379 		case 'r':
380 			rflag = 1;
381 			break;
382 		case 's':
383 			++sflag;
384 			break;
385 		case 't':
386 			tflag = 1;
387 			break;
388 		case 'u':
389 			af = AF_UNIX;
390 			break;
391 		case 'v':
392 			vflag = 1;
393 			break;
394 		case 'w':
395 			interval = atoi(optarg);
396 			iflag = 1;
397 			break;
398 		case '?':
399 		default:
400 			usage();
401 		}
402 	argv += optind;
403 	argc -= optind;
404 
405 	/*
406 	 * Discard setgid privileges if not the running kernel so that bad
407 	 * guys can't print interesting stuff from kernel memory.
408 	 */
409 	if (nlistf != NULL || memf != NULL) {
410 		setegid(getgid());
411 		setgid(getgid());
412 	}
413 
414 	if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
415 	    buf)) == NULL) {
416 		fprintf(stderr, "%s: kvm_open: %s\n", __progname, buf);
417 		exit(1);
418 	}
419 	setegid(getgid());
420 	setgid(getgid());
421 
422 #define	BACKWARD_COMPATIBILITY
423 #ifdef	BACKWARD_COMPATIBILITY
424 	if (*argv) {
425 		if (isdigit(**argv)) {
426 			interval = atoi(*argv);
427 			if (interval <= 0)
428 				usage();
429 			++argv;
430 			iflag = 1;
431 		}
432 		if (*argv) {
433 			nlistf = *argv;
434 			if (*++argv)
435 				memf = *argv;
436 		}
437 	}
438 #endif
439 
440 	if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
441 		if (nlistf)
442 			fprintf(stderr, "%s: %s: no namelist\n", __progname,
443 			    nlistf);
444 		else
445 			fprintf(stderr, "%s: no namelist\n", __progname);
446 		exit(1);
447 	}
448 	if (mflag) {
449 		mbpr(nl[N_MBSTAT].n_value, nl[N_MBPOOL].n_value,
450 		    nl[N_MCLPOOL].n_value);
451 		exit(0);
452 	}
453 	if (pflag) {
454 		if (tp->pr_stats)
455 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
456 				tp->pr_name);
457 		else
458 			printf("%s: no stats routine\n", tp->pr_name);
459 		exit(0);
460 	}
461 	/*
462 	 * Keep file descriptors open to avoid overhead
463 	 * of open/close on each call to get* routines.
464 	 */
465 	sethostent(1);
466 	setnetent(1);
467 	if (iflag) {
468 		intpr(interval, nl[N_IFNET].n_value);
469 		exit(0);
470 	}
471 	if (rflag) {
472 		if (sflag)
473 			rt_stats(nl[N_RTSTAT].n_value);
474 		else
475 			routepr(nl[N_RTREE].n_value);
476 		exit(0);
477 	}
478 	if (gflag) {
479 		if (sflag) {
480 			if (af == AF_INET || af == AF_UNSPEC)
481 				mrt_stats(nl[N_MRTPROTO].n_value,
482 				    nl[N_MRTSTAT].n_value);
483 #ifdef INET6
484 			if (af == AF_INET6 || af == AF_UNSPEC)
485 				mrt6_stats(nl[N_MRT6PROTO].n_value,
486 				    nl[N_MRT6STAT].n_value);
487 #endif
488 		}
489 		else {
490 			if (af == AF_INET || af == AF_UNSPEC)
491 				mroutepr(nl[N_MRTPROTO].n_value,
492 				    nl[N_MFCHASHTBL].n_value,
493 				    nl[N_MFCHASH].n_value,
494 				    nl[N_VIFTABLE].n_value);
495 #ifdef INET6
496 			if (af == AF_INET6 || af == AF_UNSPEC)
497 				mroute6pr(nl[N_MRT6PROTO].n_value,
498 				    nl[N_MF6CTABLE].n_value,
499 				    nl[N_MIF6TABLE].n_value);
500 #endif
501 		}
502 		exit(0);
503 	}
504 	if (af == AF_INET || af == AF_UNSPEC) {
505 		setprotoent(1);
506 		setservent(1);
507 		/* ugh, this is O(MN) ... why do we do this? */
508 		while ((p = getprotoent())) {
509 			for (tp = protox; tp->pr_name; tp++)
510 				if (strcmp(tp->pr_name, p->p_name) == 0)
511 					break;
512 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
513 				continue;
514 			printproto(tp, p->p_name);
515 		}
516 		endprotoent();
517 	}
518 #ifdef INET6
519 	if (af == AF_INET6 || af == AF_UNSPEC)
520 		for (tp = ip6protox; tp->pr_name; tp++)
521 			printproto(tp, tp->pr_name);
522 #endif
523 	if (af == AF_IPX || af == AF_UNSPEC)
524 		for (tp = ipxprotox; tp->pr_name; tp++)
525 			printproto(tp, tp->pr_name);
526 	if (af == AF_NS || af == AF_UNSPEC)
527 		for (tp = nsprotox; tp->pr_name; tp++)
528 			printproto(tp, tp->pr_name);
529 	if (af == AF_ISO || af == AF_UNSPEC)
530 		for (tp = isoprotox; tp->pr_name; tp++)
531 			printproto(tp, tp->pr_name);
532 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
533 		unixpr(nl[N_UNIXSW].n_value);
534 	if (af == AF_APPLETALK || af == AF_UNSPEC)
535 		for (tp = atalkprotox; tp->pr_name; tp++)
536 			printproto(tp, tp->pr_name);
537 	exit(0);
538 }
539 
540 /*
541  * Print out protocol statistics or control blocks (per sflag).
542  * If the interface was not specifically requested, and the symbol
543  * is not in the namelist, ignore this one.
544  */
545 static void
546 printproto(struct protox *tp, char *name)
547 {
548 	void (*pr)(u_long, char *);
549 	u_char i;
550 
551 	if (sflag) {
552 		pr = tp->pr_stats;
553 		i = tp->pr_sindex;
554 	} else {
555 		pr = tp->pr_cblocks;
556 		i = tp->pr_index;
557 	}
558 	if (pr != NULL && i < sizeof(nl) / sizeof(nl[0]) &&
559 	    (nl[i].n_value || af != AF_UNSPEC))
560 		(*pr)(nl[i].n_value, name);
561 }
562 
563 /*
564  * Read kernel memory, return 0 on success.
565  */
566 int
567 kread(u_long addr, char *buf, int size)
568 {
569 
570 	if (kvm_read(kvmd, addr, buf, size) != size) {
571 		(void)fprintf(stderr, "%s: %s\n", __progname,
572 		    kvm_geterr(kvmd));
573 		return (-1);
574 	}
575 	return (0);
576 }
577 
578 char *
579 plural(int n)
580 {
581 	return (n != 1 ? "s" : "");
582 }
583 
584 char *
585 plurales(int n)
586 {
587 	return (n != 1 ? "es" : "");
588 }
589 
590 /*
591  * Find the protox for the given "well-known" name.
592  */
593 static struct protox *
594 knownname(char *name)
595 {
596 	struct protox **tpp, *tp;
597 
598 	for (tpp = protoprotox; *tpp; tpp++)
599 		for (tp = *tpp; tp->pr_name; tp++)
600 			if (strcmp(tp->pr_name, name) == 0)
601 				return (tp);
602 	return (NULL);
603 }
604 
605 /*
606  * Find the protox corresponding to name.
607  */
608 static struct protox *
609 name2protox(char *name)
610 {
611 	struct protox *tp;
612 	char **alias;			/* alias from p->aliases */
613 	struct protoent *p;
614 
615 	/*
616 	 * Try to find the name in the list of "well-known" names. If that
617 	 * fails, check if name is an alias for an Internet protocol.
618 	 */
619 	if ((tp = knownname(name)))
620 		return (tp);
621 
622 	setprotoent(1);			/* make protocol lookup cheaper */
623 	while ((p = getprotoent())) {
624 		/* assert: name not same as p->name */
625 		for (alias = p->p_aliases; *alias; alias++)
626 			if (strcmp(name, *alias) == 0) {
627 				endprotoent();
628 				return (knownname(p->p_name));
629 			}
630 	}
631 	endprotoent();
632 	return (NULL);
633 }
634 
635 static void
636 usage(void)
637 {
638 	(void)fprintf(stderr,
639 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname);
640 	(void)fprintf(stderr,
641 "       %s [-bdgilmnqrstu] [-f address_family] [-M core] [-N system]\n", __progname);
642 	(void)fprintf(stderr,
643 "       %s [-bdn] [-I interface] [-M core] [-N system] [-w wait]\n", __progname);
644 	(void)fprintf(stderr,
645 "       %s [-M core] [-N system] [-p protocol]\n", __progname);
646 	(void)fprintf(stderr,
647 "       %s [-s] [-f address_family] [-i] [-I interface]\n", __progname);
648 	exit(1);
649 }
650