xref: /openbsd-src/usr.bin/netstat/main.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /*	$OpenBSD: main.c,v 1.80 2009/10/04 16:08:37 michele 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 #include <sys/param.h>
34 #include <sys/file.h>
35 #include <sys/protosw.h>
36 #include <sys/socket.h>
37 
38 #include <net/route.h>
39 #include <netinet/in.h>
40 
41 #include <ctype.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <kvm.h>
45 #include <limits.h>
46 #include <netdb.h>
47 #include <nlist.h>
48 #include <paths.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include "netstat.h"
54 
55 struct nlist nl[] = {
56 #define N_TCBTABLE	0
57 	{ "_tcbtable" },
58 #define N_UDBTABLE	1
59 	{ "_udbtable" },
60 #define N_DDPCB		2
61 	{ "_ddpcb"},
62 #define N_UNIXSW	3
63 	{ "_unixsw" },
64 
65 #define N_MFCHASHTBL	4
66 	{ "_mfchashtbl" },
67 #define N_MFCHASH	5
68 	{ "_mfchash" },
69 #define N_VIFTABLE	6
70 	{ "_viftable" },
71 
72 #define N_MF6CTABLE	7
73 	{ "_mf6ctable" },
74 #define N_MIF6TABLE	8
75 	{ "_mif6table" },
76 
77 #define N_RTREE		9
78 	{ "_rt_tables"},
79 #define N_RTMASK	10
80 	{ "_mask_rnhead" },
81 #define N_AF2RTAFIDX	11
82 	{ "_af2rtafidx" },
83 #define N_RTBLIDMAX	12
84 	{ "_rtbl_id_max" },
85 
86 #define N_RAWIPTABLE	13
87 	{ "_rawcbtable" },
88 #define N_RAWIP6TABLE	14
89 	{ "_rawin6pcbtable" },
90 #define N_DIVBTABLE	15
91 	{ "_divbtable" },
92 
93 	{ ""}
94 };
95 
96 struct protox {
97 	u_char	pr_index;			/* index into nlist of cb head */
98 	void	(*pr_cblocks)(u_long, char *);	/* control blocks printing routine */
99 	void	(*pr_stats)(char *);		/* statistics printing routine */
100 	void	(*pr_dump)(u_long);		/* pcb printing routine */
101 	char	*pr_name;			/* well-known name */
102 } protox[] = {
103 	{ N_TCBTABLE,	protopr,	tcp_stats,	tcp_dump,	"tcp" },
104 	{ N_UDBTABLE,	protopr,	udp_stats,	NULL,		"udp" },
105 	{ N_RAWIPTABLE,	protopr,	ip_stats,	NULL,		"ip" },
106 	{ N_DIVBTABLE,	protopr,	div_stats,	NULL,		"divert" },
107 	{ -1,		NULL,		icmp_stats,	NULL,		"icmp" },
108 	{ -1,		NULL,		igmp_stats,	NULL,		"igmp" },
109 	{ -1,		NULL,		ah_stats,	NULL,		"ah" },
110 	{ -1,		NULL,		esp_stats,	NULL,		"esp" },
111 	{ -1,		NULL,		ipip_stats,	NULL,		"ipencap" },
112 	{ -1,		NULL,		etherip_stats,	NULL,		"etherip" },
113 	{ -1,		NULL,		ipcomp_stats,	NULL,		"ipcomp" },
114 	{ -1,		NULL,		carp_stats,	NULL,		"carp" },
115 	{ -1,		NULL,		pfsync_stats,	NULL,		"pfsync" },
116 	{ -1,		NULL,		pim_stats,	NULL,		"pim" },
117 	{ -1,		NULL,		pflow_stats,	NULL,		"pflow" },
118 	{ -1,		NULL,		NULL,		NULL,		NULL }
119 };
120 
121 struct protox ip6protox[] = {
122 	{ N_TCBTABLE,	ip6protopr,	NULL,		tcp_dump,	"tcp" },
123 	{ N_UDBTABLE,	ip6protopr,	NULL,		NULL,		"udp" },
124 	{ N_RAWIP6TABLE,ip6protopr,	ip6_stats,	NULL,		"ip6" },
125 	{ -1,		NULL,		icmp6_stats,	NULL,		"icmp6" },
126 	{ -1,		NULL,		pim6_stats,	NULL,		"pim6" },
127 	{ -1,		NULL,		rip6_stats,	NULL,		"rip6" },
128 	{ -1,		NULL,		NULL,		NULL,		NULL }
129 };
130 
131 struct protox atalkprotox[] = {
132 	{ N_DDPCB,	atalkprotopr,	ddp_stats,	NULL,		"ddp" },
133 	{ -1,		NULL,		NULL,		NULL,		NULL }
134 };
135 
136 struct protox *protoprotox[] = {
137 	protox, ip6protox, atalkprotox, NULL
138 };
139 
140 static void printproto(struct protox *, char *);
141 static void usage(void);
142 static struct protox *name2protox(char *);
143 static struct protox *knownname(char *);
144 
145 kvm_t *kvmd;
146 
147 int
148 main(int argc, char *argv[])
149 {
150 	extern char *optarg;
151 	extern int optind;
152 	const char *errstr;
153 	struct protoent *p;
154 	struct protox *tp = NULL; /* for printing cblocks & stats */
155 	int ch;
156 	char *nlistf = NULL, *memf = NULL, *ep;
157 	char buf[_POSIX2_LINE_MAX];
158 	gid_t gid;
159 	u_long pcbaddr = 0;
160 	u_int tableid = 0;
161 
162 	af = AF_UNSPEC;
163 
164 	while ((ch = getopt(argc, argv, "AabdFf:gI:ilM:mN:np:P:qrsT:tuvW:w:")) != -1)
165 		switch (ch) {
166 		case 'A':
167 			Aflag = 1;
168 			break;
169 		case 'a':
170 			aflag = 1;
171 			break;
172 		case 'b':
173 			bflag = 1;
174 			break;
175 		case 'd':
176 			dflag = 1;
177 			break;
178 		case 'F':
179 			Fflag = 1;
180 			break;
181 		case 'f':
182 			if (strcmp(optarg, "inet") == 0)
183 				af = AF_INET;
184 			else if (strcmp(optarg, "inet6") == 0)
185 				af = AF_INET6;
186 			else if (strcmp(optarg, "local") == 0)
187 				af = AF_LOCAL;
188 			else if (strcmp(optarg, "unix") == 0)
189 				af = AF_UNIX;
190 			else if (strcmp(optarg, "encap") == 0)
191 				af = PF_KEY;
192 			else if (strcmp(optarg, "atalk") == 0)
193 				af = AF_APPLETALK;
194 			else if (strcmp(optarg, "mpls") == 0)
195 				af = AF_MPLS;
196 			else if (strcmp(optarg, "pflow") == 0)
197 				af = PF_PFLOW;
198 			else if (strcmp(optarg, "mask") == 0)
199 				af = 0xff;
200 			else {
201 				(void)fprintf(stderr,
202 				    "%s: %s: unknown address family\n",
203 				    __progname, optarg);
204 				exit(1);
205 			}
206 			break;
207 		case 'g':
208 			gflag = 1;
209 			break;
210 		case 'I':
211 			iflag = 1;
212 			interface = optarg;
213 			break;
214 		case 'i':
215 			iflag = 1;
216 			break;
217 		case 'l':
218 			lflag = 1;
219 			break;
220 		case 'M':
221 			memf = optarg;
222 			break;
223 		case 'm':
224 			mflag = 1;
225 			break;
226 		case 'N':
227 			nlistf = optarg;
228 			break;
229 		case 'n':
230 			nflag = 1;
231 			break;
232 		case 'p':
233 			if ((tp = name2protox(optarg)) == NULL) {
234 				(void)fprintf(stderr,
235 				    "%s: %s: unknown protocol\n",
236 				    __progname, optarg);
237 				exit(1);
238 			}
239 			pflag = 1;
240 			break;
241 		case 'P':
242 			errno = 0;
243 			pcbaddr = strtoul(optarg, &ep, 16);
244 			if (optarg[0] == '\0' || *ep != '\0' ||
245 			    errno == ERANGE) {
246 				(void)fprintf(stderr,
247 				    "%s: %s: invalid PCB address\n",
248 				    __progname, optarg);
249 				exit(1);
250 			}
251 			Pflag = 1;
252 			break;
253 		case 'q':
254 			qflag = 1;
255 			break;
256 		case 'r':
257 			rflag = 1;
258 			break;
259 		case 's':
260 			++sflag;
261 			break;
262 		case 'T':
263 			tableid = strtonum(optarg, 0, RT_TABLEID_MAX, &errstr);
264 			if (errstr)
265 				errx(1, "invalid table id: %s", errstr);
266 			break;
267 		case 't':
268 			tflag = 1;
269 			break;
270 		case 'u':
271 			af = AF_UNIX;
272 			break;
273 		case 'v':
274 			vflag = 1;
275 			break;
276 		case 'W':
277 			Wflag = 1;
278 			interface = optarg;
279 			break;
280 		case 'w':
281 			interval = atoi(optarg);
282 			iflag = 1;
283 			break;
284 		case '?':
285 		default:
286 			usage();
287 		}
288 	argv += optind;
289 	argc -= optind;
290 
291 	/*
292 	 * Show per-interface statistics which don't need access to
293 	 * kernel memory (they're using IOCTLs)
294 	 */
295 	if (Wflag) {
296 		if (interface == NULL)
297 			usage();
298 		net80211_ifstats(interface);
299 		exit(0);
300 	}
301 
302 	/*
303 	 * Discard setgid privileges if not the running kernel so that bad
304 	 * guys can't print interesting stuff from kernel memory.
305 	 * Dumping PCB info is also restricted.
306 	 */
307 	gid = getgid();
308 	if (nlistf != NULL || memf != NULL || Pflag)
309 		if (setresgid(gid, gid, gid) == -1)
310 			err(1, "setresgid");
311 
312 	if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
313 	    buf)) == NULL) {
314 		fprintf(stderr, "%s: kvm_openfiles: %s\n", __progname, buf);
315 		exit(1);
316 	}
317 
318 	if (nlistf == NULL && memf == NULL && !Pflag)
319 		if (setresgid(gid, gid, gid) == -1)
320 			err(1, "setresgid");
321 
322 #define	BACKWARD_COMPATIBILITY
323 #ifdef	BACKWARD_COMPATIBILITY
324 	if (*argv) {
325 		if (isdigit(**argv)) {
326 			interval = atoi(*argv);
327 			if (interval <= 0)
328 				usage();
329 			++argv;
330 			iflag = 1;
331 		}
332 		if (*argv) {
333 			nlistf = *argv;
334 			if (*++argv)
335 				memf = *argv;
336 		}
337 	}
338 #endif
339 
340 	if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
341 		if (nlistf)
342 			fprintf(stderr, "%s: %s: no namelist\n", __progname,
343 			    nlistf);
344 		else
345 			fprintf(stderr, "%s: no namelist\n", __progname);
346 		exit(1);
347 	}
348 	if (mflag) {
349 		mbpr();
350 		exit(0);
351 	}
352 	if (pflag) {
353 		printproto(tp, tp->pr_name);
354 		exit(0);
355 	}
356 	if (Pflag) {
357 		if (tp == NULL && (tp = name2protox("tcp")) == NULL) {
358 			(void)fprintf(stderr,
359 			    "%s: %s: unknown protocol\n",
360 			    __progname, "tcp");
361 			exit(1);
362 		}
363 		if (tp->pr_dump)
364 			(tp->pr_dump)(pcbaddr);
365 		exit(0);
366 	}
367 	/*
368 	 * Keep file descriptors open to avoid overhead
369 	 * of open/close on each call to get* routines.
370 	 */
371 	sethostent(1);
372 	setnetent(1);
373 
374 	if (iflag) {
375 		intpr(interval);
376 		exit(0);
377 	}
378 	if (rflag) {
379 		if (sflag)
380 			rt_stats();
381 		else if (Aflag || nlistf != NULL || memf != NULL)
382 			routepr(nl[N_RTREE].n_value, nl[N_RTMASK].n_value,
383 			    nl[N_AF2RTAFIDX].n_value, nl[N_RTBLIDMAX].n_value,
384 			    tableid);
385 		else
386 			p_rttables(af, tableid);
387 		exit(0);
388 	}
389 	if (gflag) {
390 		if (sflag) {
391 			if (af == AF_INET || af == AF_UNSPEC)
392 				mrt_stats();
393 			if (af == AF_INET6 || af == AF_UNSPEC)
394 				mrt6_stats();
395 		} else {
396 			if (af == AF_INET || af == AF_UNSPEC)
397 				mroutepr(nl[N_MFCHASHTBL].n_value,
398 				    nl[N_MFCHASH].n_value,
399 				    nl[N_VIFTABLE].n_value);
400 			if (af == AF_INET6 || af == AF_UNSPEC)
401 				mroute6pr(nl[N_MF6CTABLE].n_value,
402 				    nl[N_MIF6TABLE].n_value);
403 		}
404 		exit(0);
405 	}
406 	if (af == AF_INET || af == AF_UNSPEC) {
407 		setprotoent(1);
408 		setservent(1);
409 		/* ugh, this is O(MN) ... why do we do this? */
410 		while ((p = getprotoent())) {
411 			for (tp = protox; tp->pr_name; tp++)
412 				if (strcmp(tp->pr_name, p->p_name) == 0)
413 					break;
414 			if (tp->pr_name == 0)
415 				continue;
416 			printproto(tp, p->p_name);
417 		}
418 		endprotoent();
419 	}
420 	if (af == PF_PFLOW || af == AF_UNSPEC) {
421 		tp = name2protox("pflow");
422 		printproto(tp, tp->pr_name);
423 	}
424 	if (af == AF_INET6 || af == AF_UNSPEC)
425 		for (tp = ip6protox; tp->pr_name; tp++)
426 			printproto(tp, tp->pr_name);
427 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
428 		unixpr(nl[N_UNIXSW].n_value);
429 	if (af == AF_APPLETALK || af == AF_UNSPEC)
430 		for (tp = atalkprotox; tp->pr_name; tp++)
431 			printproto(tp, tp->pr_name);
432 	exit(0);
433 }
434 
435 /*
436  * Print out protocol statistics or control blocks (per sflag).
437  * If the interface was not specifically requested, and the symbol
438  * is not in the namelist, ignore this one.
439  */
440 static void
441 printproto(struct protox *tp, char *name)
442 {
443 	if (sflag) {
444 		if (tp->pr_stats != NULL)
445 			(*tp->pr_stats)(name);
446 	} else {
447 		u_char i = tp->pr_index;
448 		if (tp->pr_cblocks != NULL &&
449 		    i < sizeof(nl) / sizeof(nl[0]) &&
450 		    (nl[i].n_value || af != AF_UNSPEC))
451 			(*tp->pr_cblocks)(nl[i].n_value, name);
452 	}
453 }
454 
455 /*
456  * Read kernel memory, return 0 on success.
457  */
458 int
459 kread(u_long addr, void *buf, int size)
460 {
461 
462 	if (kvm_read(kvmd, addr, buf, size) != size) {
463 		(void)fprintf(stderr, "%s: %s\n", __progname,
464 		    kvm_geterr(kvmd));
465 		return (-1);
466 	}
467 	return (0);
468 }
469 
470 char *
471 plural(u_int64_t n)
472 {
473 	return (n != 1 ? "s" : "");
474 }
475 
476 char *
477 plurales(u_int64_t n)
478 {
479 	return (n != 1 ? "es" : "");
480 }
481 
482 /*
483  * Find the protox for the given "well-known" name.
484  */
485 static struct protox *
486 knownname(char *name)
487 {
488 	struct protox **tpp, *tp;
489 
490 	for (tpp = protoprotox; *tpp; tpp++)
491 		for (tp = *tpp; tp->pr_name; tp++)
492 			if (strcmp(tp->pr_name, name) == 0)
493 				return (tp);
494 	return (NULL);
495 }
496 
497 /*
498  * Find the protox corresponding to name.
499  */
500 static struct protox *
501 name2protox(char *name)
502 {
503 	struct protox *tp;
504 	char **alias;			/* alias from p->aliases */
505 	struct protoent *p;
506 
507 	/*
508 	 * Try to find the name in the list of "well-known" names. If that
509 	 * fails, check if name is an alias for an Internet protocol.
510 	 */
511 	if ((tp = knownname(name)))
512 		return (tp);
513 
514 	setprotoent(1);			/* make protocol lookup cheaper */
515 	while ((p = getprotoent())) {
516 		/* assert: name not same as p->name */
517 		for (alias = p->p_aliases; *alias; alias++)
518 			if (strcmp(name, *alias) == 0) {
519 				endprotoent();
520 				return (knownname(p->p_name));
521 			}
522 	}
523 	endprotoent();
524 	return (NULL);
525 }
526 
527 static void
528 usage(void)
529 {
530 	(void)fprintf(stderr,
531 	    "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n"
532 	    "       %s [-bdFgilmnqrstu] [-f address_family] [-M core] [-N system]\n"
533 	    "               [-T tableid]\n"
534 	    "       %s [-bdn] [-I interface] [-M core] [-N system] [-w wait]\n"
535 	    "       %s [-M core] [-N system] -P pcbaddr\n"
536 	    "       %s [-s] [-M core] [-N system] [-p protocol]\n"
537 	    "       %s [-a] [-f address_family] [-i | -I interface]\n"
538 	    "       %s [-W interface]\n",
539 	    __progname, __progname, __progname, __progname,
540 	    __progname, __progname, __progname);
541 	exit(1);
542 }
543