xref: /freebsd/usr.bin/netstat/main.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
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 #if 0
41 #ifndef lint
42 static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 3/1/94";
43 #endif /* not lint */
44 #endif
45 
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48 
49 #include <sys/param.h>
50 #include <sys/file.h>
51 #include <sys/protosw.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 
55 #include <netinet/in.h>
56 
57 #include <netgraph/ng_socket.h>
58 
59 #include <ctype.h>
60 #include <err.h>
61 #include <errno.h>
62 #include <kvm.h>
63 #include <limits.h>
64 #include <netdb.h>
65 #include <nlist.h>
66 #include <paths.h>
67 #include <stdint.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
72 #include "netstat.h"
73 
74 static struct nlist nl[] = {
75 #define	N_IFNET		0
76 	{ .n_name = "_ifnet" },
77 #define	N_RTSTAT	1
78 	{ .n_name = "_rtstat" },
79 #define N_RTREE		2
80 	{ .n_name = "_rt_tables"},
81 #define N_MRTSTAT	3
82 	{ .n_name = "_mrtstat" },
83 #define N_MFCTABLE	4
84 	{ .n_name = "_mfctable" },
85 #define N_VIFTABLE	5
86 	{ .n_name = "_viftable" },
87 #define N_IPX		6
88 	{ .n_name = "_ipxpcb_list"},
89 #define N_IPXSTAT	7
90 	{ .n_name = "_ipxstat"},
91 #define N_SPXSTAT	8
92 	{ .n_name = "_spx_istat"},
93 #define N_DDPSTAT	9
94 	{ .n_name = "_ddpstat"},
95 #define N_DDPCB		10
96 	{ .n_name = "_ddpcb"},
97 #define N_NGSOCKS	11
98 	{ .n_name = "_ngsocklist"},
99 #define N_IP6STAT	12
100 	{ .n_name = "_ip6stat" },
101 #define N_ICMP6STAT	13
102 	{ .n_name = "_icmp6stat" },
103 #define N_IPSECSTAT	14
104 	{ .n_name = "_ipsec4stat" },
105 #define N_IPSEC6STAT	15
106 	{ .n_name = "_ipsec6stat" },
107 #define N_PIM6STAT	16
108 	{ .n_name = "_pim6stat" },
109 #define N_MRT6STAT	17
110 	{ .n_name = "_mrt6stat" },
111 #define N_MF6CTABLE	18
112 	{ .n_name = "_mf6ctable" },
113 #define N_MIF6TABLE	19
114 	{ .n_name = "_mif6table" },
115 #define N_PFKEYSTAT	20
116 	{ .n_name = "_pfkeystat" },
117 #define N_MBSTAT	21
118 	{ .n_name = "_mbstat" },
119 #define N_MBTYPES	22
120 	{ .n_name = "_mbtypes" },
121 #define N_NMBCLUSTERS	23
122 	{ .n_name = "_nmbclusters" },
123 #define N_NMBUFS	24
124 	{ .n_name = "_nmbufs" },
125 #define	N_MBHI		25
126 	{ .n_name = "_mbuf_hiwm" },
127 #define	N_CLHI		26
128 	{ .n_name = "_clust_hiwm" },
129 #define	N_NCPUS		27
130 	{ .n_name = "_smp_cpus" },
131 #define	N_PAGESZ	28
132 	{ .n_name = "_pagesize" },
133 #define	N_MBPSTAT	29
134 	{ .n_name = "_mb_statpcpu" },
135 #define	N_RTTRASH	30
136 	{ .n_name = "_rttrash" },
137 #define	N_MBLO		31
138 	{ .n_name = "_mbuf_lowm" },
139 #define	N_CLLO		32
140 	{ .n_name = "_clust_lowm" },
141 #define N_CARPSTAT	33
142 	{ .n_name = "_carpstats" },
143 #define N_PFSYNCSTAT	34
144 	{ .n_name = "_pfsyncstats" },
145 #define	N_AHSTAT	35
146 	{ .n_name = "_ahstat" },
147 #define	N_ESPSTAT	36
148 	{ .n_name = "_espstat" },
149 #define	N_IPCOMPSTAT	37
150 	{ .n_name = "_ipcompstat" },
151 #define	N_TCPSTAT	38
152 	{ .n_name = "_tcpstat" },
153 #define	N_UDPSTAT	39
154 	{ .n_name = "_udpstat" },
155 #define	N_IPSTAT	40
156 	{ .n_name = "_ipstat" },
157 #define	N_ICMPSTAT	41
158 	{ .n_name = "_icmpstat" },
159 #define	N_IGMPSTAT	42
160 	{ .n_name = "_igmpstat" },
161 #define	N_PIMSTAT	43
162 	{ .n_name = "_pimstat" },
163 #define	N_TCBINFO	44
164 	{ .n_name = "_tcbinfo" },
165 #define	N_UDBINFO	45
166 	{ .n_name = "_udbinfo" },
167 #define	N_DIVCBINFO	46
168 	{ .n_name = "_divcbinfo" },
169 #define	N_RIPCBINFO	47
170 	{ .n_name = "_ripcbinfo" },
171 #define	N_UNP_COUNT	48
172 	{ .n_name = "_unp_count" },
173 #define	N_UNP_GENCNT	49
174 	{ .n_name = "_unp_gencnt" },
175 #define	N_UNP_DHEAD	50
176 	{ .n_name = "_unp_dhead" },
177 #define	N_UNP_SHEAD	51
178 	{ .n_name = "_unp_shead" },
179 #define	N_RIP6STAT	52
180 	{ .n_name = "_rip6stat" },
181 #define	N_SCTPSTAT	53
182 	{ .n_name = "_sctpstat" },
183 	{ .n_name = NULL },
184 };
185 
186 struct protox {
187 	int	pr_index;		/* index into nlist of cb head */
188 	int	pr_sindex;		/* index into nlist of stat block */
189 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
190 	void	(*pr_cblocks)(u_long, const char *, int, int);
191 					/* control blocks printing routine */
192 	void	(*pr_stats)(u_long, const char *, int, int);
193 					/* statistics printing routine */
194 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
195 	const char	*pr_name;		/* well-known name */
196 	int	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
197 	int	pr_protocol;
198 } protox[] = {
199 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
200 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
201 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
202 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
203 #ifdef SCTP
204 	{ -1,		N_SCTPSTAT,	1,	sctp_protopr,
205 	  sctp_stats,	NULL,		"sctp",	1,	IPPROTO_SCTP },
206 #endif
207 	{ N_DIVCBINFO,	-1,		1,	protopr,
208 	  NULL,		NULL,		"divert", 1,	IPPROTO_DIVERT },
209 	{ N_RIPCBINFO,	N_IPSTAT,	1,	protopr,
210 	  ip_stats,	NULL,		"ip",	1,	IPPROTO_RAW },
211 	{ N_RIPCBINFO,	N_ICMPSTAT,	1,	protopr,
212 	  icmp_stats,	NULL,		"icmp",	1,	IPPROTO_ICMP },
213 	{ N_RIPCBINFO,	N_IGMPSTAT,	1,	protopr,
214 	  igmp_stats,	NULL,		"igmp",	1,	IPPROTO_IGMP },
215 #ifdef IPSEC
216 	{ -1,		N_IPSECSTAT,	1,	NULL,	/* keep as compat */
217 	  ipsec_stats,	NULL,		"ipsec", 0,	0},
218 	{ -1,		N_AHSTAT,	1,	NULL,
219 	  ah_stats,	NULL,		"ah",	0,	0},
220 	{ -1,		N_ESPSTAT,	1,	NULL,
221 	  esp_stats,	NULL,		"esp",	0,	0},
222 	{ -1,		N_IPCOMPSTAT,	1,	NULL,
223 	  ipcomp_stats,	NULL,		"ipcomp", 0,	0},
224 #endif
225 	{ N_RIPCBINFO,	N_PIMSTAT,	1,	protopr,
226 	  pim_stats,	NULL,		"pim",	1,	IPPROTO_PIM },
227 	{ -1,		N_CARPSTAT,	1,	NULL,
228 	  carp_stats,	NULL,		"carp",	1,	0 },
229 	{ -1,		N_PFSYNCSTAT,	1,	NULL,
230 	  pfsync_stats,	NULL,		"pfsync", 1,	0 },
231 	{ -1,		-1,		0,	NULL,
232 	  NULL,		NULL,		NULL,	0,	0 }
233 };
234 
235 #ifdef INET6
236 struct protox ip6protox[] = {
237 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
238 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
239 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
240 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
241 	{ N_RIPCBINFO,	N_IP6STAT,	1,	protopr,
242 	  ip6_stats,	ip6_ifstats,	"ip6",	1,	IPPROTO_RAW },
243 	{ N_RIPCBINFO,	N_ICMP6STAT,	1,	protopr,
244 	  icmp6_stats,	icmp6_ifstats,	"icmp6", 1,	IPPROTO_ICMPV6 },
245 #ifdef IPSEC
246 	{ -1,		N_IPSEC6STAT,	1,	NULL,
247 	  ipsec_stats,	NULL,		"ipsec6", 0,	0 },
248 #endif
249 #ifdef notyet
250 	{ -1,		N_PIM6STAT,	1,	NULL,
251 	  pim6_stats,	NULL,		"pim6",	1,	0 },
252 #endif
253 	{ -1,		N_RIP6STAT,	1,	NULL,
254 	  rip6_stats,	NULL,		"rip6",	1,	0 },
255 	{ -1,		-1,		0,	NULL,
256 	  NULL,		NULL,		NULL,	0,	0 }
257 };
258 #endif /*INET6*/
259 
260 #ifdef IPSEC
261 struct protox pfkeyprotox[] = {
262 	{ -1,		N_PFKEYSTAT,	1,	NULL,
263 	  pfkey_stats,	NULL,		"pfkey", 0,	0 },
264 	{ -1,		-1,		0,	NULL,
265 	  NULL,		NULL,		NULL,	0,	0 }
266 };
267 #endif
268 
269 struct protox atalkprotox[] = {
270 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
271 	  ddp_stats,	NULL,		"ddp",	0,	0 },
272 	{ -1,		-1,		0,	NULL,
273 	  NULL,		NULL,		NULL,	0,	0 }
274 };
275 
276 struct protox netgraphprotox[] = {
277 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
278 	  NULL,		NULL,		"ctrl",	0,	0 },
279 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
280 	  NULL,		NULL,		"data",	0,	0 },
281 	{ -1,		-1,		0,	NULL,
282 	  NULL,		NULL,		NULL,	0,	0 }
283 };
284 
285 #ifdef IPX
286 struct protox ipxprotox[] = {
287 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
288 	  ipx_stats,	NULL,		"ipx",	0,	0 },
289 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
290 	  spx_stats,	NULL,		"spx",	0,	0 },
291 	{ -1,		-1,		0,	NULL,
292 	  NULL,		NULL,		0,	0,	0 }
293 };
294 #endif
295 
296 struct protox *protoprotox[] = {
297 					 protox,
298 #ifdef INET6
299 					 ip6protox,
300 #endif
301 #ifdef IPSEC
302 					 pfkeyprotox,
303 #endif
304 #ifdef IPX
305 					 ipxprotox,
306 #endif
307 					 atalkprotox, NULL };
308 
309 static void printproto(struct protox *, const char *);
310 static void usage(void);
311 static struct protox *name2protox(const char *);
312 static struct protox *knownname(const char *);
313 
314 static kvm_t *kvmd;
315 static char *nlistf = NULL, *memf = NULL;
316 
317 int	Aflag;		/* show addresses of protocol control block */
318 int	aflag;		/* show all sockets (including servers) */
319 int	Bflag;		/* show information about bpf consumers */
320 int	bflag;		/* show i/f total bytes in/out */
321 int	dflag;		/* show i/f dropped packets */
322 int	gflag;		/* show group (multicast) routing or stats */
323 int	hflag;		/* show counters in human readable format */
324 int	iflag;		/* show interfaces */
325 int	Lflag;		/* show size of listen queues */
326 int	mflag;		/* show memory stats */
327 int	numeric_addr;	/* show addresses numerically */
328 int	numeric_port;	/* show ports numerically */
329 static int pflag;	/* show given protocol */
330 int	rflag;		/* show routing tables (or routing stats) */
331 int	sflag;		/* show protocol statistics */
332 int	tflag;		/* show i/f watchdog timers */
333 int	Wflag;		/* wide display */
334 int	zflag;		/* zero stats */
335 
336 int	interval;	/* repeat interval for i/f stats */
337 
338 char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
339 int	unit;		/* unit number for above */
340 
341 int	af;		/* address family */
342 int	live;		/* true if we are examining a live system */
343 
344 int
345 main(int argc, char *argv[])
346 {
347 	struct protox *tp = NULL;  /* for printing cblocks & stats */
348 	int ch;
349 
350 	af = AF_UNSPEC;
351 
352 	while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:rSstuWw:z")) != -1)
353 		switch(ch) {
354 		case 'A':
355 			Aflag = 1;
356 			break;
357 		case 'a':
358 			aflag = 1;
359 			break;
360 		case 'B':
361 			Bflag = 1;
362 			break;
363 		case 'b':
364 			bflag = 1;
365 			break;
366 		case 'd':
367 			dflag = 1;
368 			break;
369 		case 'f':
370 			if (strcmp(optarg, "ipx") == 0)
371 				af = AF_IPX;
372 			else if (strcmp(optarg, "inet") == 0)
373 				af = AF_INET;
374 #ifdef INET6
375 			else if (strcmp(optarg, "inet6") == 0)
376 				af = AF_INET6;
377 #endif
378 #ifdef IPSEC
379 			else if (strcmp(optarg, "pfkey") == 0)
380 				af = PF_KEY;
381 #endif
382 			else if (strcmp(optarg, "unix") == 0)
383 				af = AF_UNIX;
384 			else if (strcmp(optarg, "atalk") == 0)
385 				af = AF_APPLETALK;
386 			else if (strcmp(optarg, "ng") == 0
387 			    || strcmp(optarg, "netgraph") == 0)
388 				af = AF_NETGRAPH;
389 			else if (strcmp(optarg, "link") == 0)
390 				af = AF_LINK;
391 			else {
392 				errx(1, "%s: unknown address family", optarg);
393 			}
394 			break;
395 		case 'g':
396 			gflag = 1;
397 			break;
398 		case 'h':
399 			hflag = 1;
400 			break;
401 		case 'I': {
402 			char *cp;
403 
404 			iflag = 1;
405 			for (cp = interface = optarg; isalpha(*cp); cp++)
406 				continue;
407 			unit = atoi(cp);
408 			break;
409 		}
410 		case 'i':
411 			iflag = 1;
412 			break;
413 		case 'L':
414 			Lflag = 1;
415 			break;
416 		case 'M':
417 			memf = optarg;
418 			break;
419 		case 'm':
420 			mflag = 1;
421 			break;
422 		case 'N':
423 			nlistf = optarg;
424 			break;
425 		case 'n':
426 			numeric_addr = numeric_port = 1;
427 			break;
428 		case 'p':
429 			if ((tp = name2protox(optarg)) == NULL) {
430 				errx(1,
431 				     "%s: unknown or uninstrumented protocol",
432 				     optarg);
433 			}
434 			pflag = 1;
435 			break;
436 		case 'r':
437 			rflag = 1;
438 			break;
439 		case 's':
440 			++sflag;
441 			break;
442 		case 'S':
443 			numeric_addr = 1;
444 			break;
445 		case 't':
446 			tflag = 1;
447 			break;
448 		case 'u':
449 			af = AF_UNIX;
450 			break;
451 		case 'W':
452 		case 'l':
453 			Wflag = 1;
454 			break;
455 		case 'w':
456 			interval = atoi(optarg);
457 			iflag = 1;
458 			break;
459 		case 'z':
460 			zflag = 1;
461 			break;
462 		case '?':
463 		default:
464 			usage();
465 		}
466 	argv += optind;
467 	argc -= optind;
468 
469 #define	BACKWARD_COMPATIBILITY
470 #ifdef	BACKWARD_COMPATIBILITY
471 	if (*argv) {
472 		if (isdigit(**argv)) {
473 			interval = atoi(*argv);
474 			if (interval <= 0)
475 				usage();
476 			++argv;
477 			iflag = 1;
478 		}
479 		if (*argv) {
480 			nlistf = *argv;
481 			if (*++argv)
482 				memf = *argv;
483 		}
484 	}
485 #endif
486 
487 	/*
488 	 * Discard setgid privileges if not the running kernel so that bad
489 	 * guys can't print interesting stuff from kernel memory.
490 	 */
491 	live = (nlistf == NULL && memf == NULL);
492 	if (!live)
493 		setgid(getgid());
494 
495 	if (Bflag) {
496 		if (!live)
497 			usage();
498 		bpf_stats(interface);
499 		exit(0);
500 	}
501 	if (mflag) {
502 		if (memf != NULL) {
503 			if (kread(0, NULL, 0) == 0)
504 				mbpr(kvmd, nl[N_MBSTAT].n_value);
505 		} else
506 			mbpr(NULL, 0);
507 		exit(0);
508 	}
509 #if 0
510 	/*
511 	 * Keep file descriptors open to avoid overhead
512 	 * of open/close on each call to get* routines.
513 	 */
514 	sethostent(1);
515 	setnetent(1);
516 #else
517 	/*
518 	 * This does not make sense any more with DNS being default over
519 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
520 	 * used for the queries, which is slower.
521 	 */
522 #endif
523 	kread(0, NULL, 0);
524 	if (iflag && !sflag) {
525 		intpr(interval, nl[N_IFNET].n_value, NULL);
526 		exit(0);
527 	}
528 	if (rflag) {
529 		if (sflag)
530 			rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
531 		else
532 			routepr(nl[N_RTREE].n_value);
533 		exit(0);
534 	}
535 	if (gflag) {
536 		if (sflag) {
537 			if (af == AF_INET || af == AF_UNSPEC)
538 				mrt_stats(nl[N_MRTSTAT].n_value);
539 #ifdef INET6
540 			if (af == AF_INET6 || af == AF_UNSPEC)
541 				mrt6_stats(nl[N_MRT6STAT].n_value);
542 #endif
543 		} else {
544 			if (af == AF_INET || af == AF_UNSPEC)
545 				mroutepr(nl[N_MFCTABLE].n_value,
546 					 nl[N_VIFTABLE].n_value);
547 #ifdef INET6
548 			if (af == AF_INET6 || af == AF_UNSPEC)
549 				mroute6pr(nl[N_MF6CTABLE].n_value,
550 					  nl[N_MIF6TABLE].n_value);
551 #endif
552 		}
553 		ifmalist_dump();
554 		exit(0);
555 	}
556 
557 	if (tp) {
558 		printproto(tp, tp->pr_name);
559 		exit(0);
560 	}
561 	if (af == AF_INET || af == AF_UNSPEC)
562 		for (tp = protox; tp->pr_name; tp++)
563 			printproto(tp, tp->pr_name);
564 #ifdef INET6
565 	if (af == AF_INET6 || af == AF_UNSPEC)
566 		for (tp = ip6protox; tp->pr_name; tp++)
567 			printproto(tp, tp->pr_name);
568 #endif /*INET6*/
569 #ifdef IPSEC
570 	if (af == PF_KEY || af == AF_UNSPEC)
571 		for (tp = pfkeyprotox; tp->pr_name; tp++)
572 			printproto(tp, tp->pr_name);
573 #endif /*IPSEC*/
574 #ifdef IPX
575 	if (af == AF_IPX || af == AF_UNSPEC) {
576 		for (tp = ipxprotox; tp->pr_name; tp++)
577 			printproto(tp, tp->pr_name);
578 	}
579 #endif /* IPX */
580 	if (af == AF_APPLETALK || af == AF_UNSPEC)
581 		for (tp = atalkprotox; tp->pr_name; tp++)
582 			printproto(tp, tp->pr_name);
583 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
584 		for (tp = netgraphprotox; tp->pr_name; tp++)
585 			printproto(tp, tp->pr_name);
586 	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
587 		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
588 		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value);
589 	exit(0);
590 }
591 
592 /*
593  * Print out protocol statistics or control blocks (per sflag).
594  * If the interface was not specifically requested, and the symbol
595  * is not in the namelist, ignore this one.
596  */
597 static void
598 printproto(tp, name)
599 	struct protox *tp;
600 	const char *name;
601 {
602 	void (*pr)(u_long, const char *, int, int);
603 	u_long off;
604 
605 	if (sflag) {
606 		if (iflag) {
607 			if (tp->pr_istats)
608 				intpr(interval, nl[N_IFNET].n_value,
609 				      tp->pr_istats);
610 			else if (pflag)
611 				printf("%s: no per-interface stats routine\n",
612 				    tp->pr_name);
613 			return;
614 		} else {
615 			pr = tp->pr_stats;
616 			if (!pr) {
617 				if (pflag)
618 					printf("%s: no stats routine\n",
619 					    tp->pr_name);
620 				return;
621 			}
622 			if (tp->pr_usesysctl && live)
623 				off = 0;
624 			else if (tp->pr_sindex < 0) {
625 				if (pflag)
626 					printf(
627 				    "%s: stats routine doesn't work on cores\n",
628 					    tp->pr_name);
629 				return;
630 			} else
631 				off = nl[tp->pr_sindex].n_value;
632 		}
633 	} else {
634 		pr = tp->pr_cblocks;
635 		if (!pr) {
636 			if (pflag)
637 				printf("%s: no PCB routine\n", tp->pr_name);
638 			return;
639 		}
640 		if (tp->pr_usesysctl && live)
641 			off = 0;
642 		else if (tp->pr_index < 0) {
643 			if (pflag)
644 				printf(
645 				    "%s: PCB routine doesn't work on cores\n",
646 				    tp->pr_name);
647 			return;
648 		} else
649 			off = nl[tp->pr_index].n_value;
650 	}
651 	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
652 	    af != AF_UNSPEC))
653 		(*pr)(off, name, af, tp->pr_protocol);
654 }
655 
656 /*
657  * Read kernel memory, return 0 on success.
658  */
659 int
660 kread(u_long addr, void *buf, size_t size)
661 {
662 	char errbuf[_POSIX2_LINE_MAX];
663 
664 	if (kvmd == NULL) {
665 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
666 		setgid(getgid());
667 		if (kvmd != NULL) {
668 			if (kvm_nlist(kvmd, nl) < 0) {
669 				if (nlistf)
670 					errx(1, "%s: kvm_nlist: %s", nlistf,
671 					     kvm_geterr(kvmd));
672 				else
673 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
674 			}
675 
676 			if (nl[0].n_type == 0) {
677 				if (nlistf)
678 					errx(1, "%s: no namelist", nlistf);
679 				else
680 					errx(1, "no namelist");
681 			}
682 		} else {
683 			warnx("kvm not available: %s", errbuf);
684 			return(-1);
685 		}
686 	}
687 	if (!buf)
688 		return (0);
689 	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
690 		warnx("%s", kvm_geterr(kvmd));
691 		return (-1);
692 	}
693 	return (0);
694 }
695 
696 const char *
697 plural(uintmax_t n)
698 {
699 	return (n != 1 ? "s" : "");
700 }
701 
702 const char *
703 plurales(uintmax_t n)
704 {
705 	return (n != 1 ? "es" : "");
706 }
707 
708 const char *
709 pluralies(uintmax_t n)
710 {
711 	return (n != 1 ? "ies" : "y");
712 }
713 
714 /*
715  * Find the protox for the given "well-known" name.
716  */
717 static struct protox *
718 knownname(const char *name)
719 {
720 	struct protox **tpp, *tp;
721 
722 	for (tpp = protoprotox; *tpp; tpp++)
723 		for (tp = *tpp; tp->pr_name; tp++)
724 			if (strcmp(tp->pr_name, name) == 0)
725 				return (tp);
726 	return (NULL);
727 }
728 
729 /*
730  * Find the protox corresponding to name.
731  */
732 static struct protox *
733 name2protox(const char *name)
734 {
735 	struct protox *tp;
736 	char **alias;			/* alias from p->aliases */
737 	struct protoent *p;
738 
739 	/*
740 	 * Try to find the name in the list of "well-known" names. If that
741 	 * fails, check if name is an alias for an Internet protocol.
742 	 */
743 	if ((tp = knownname(name)) != NULL)
744 		return (tp);
745 
746 	setprotoent(1);			/* make protocol lookup cheaper */
747 	while ((p = getprotoent()) != NULL) {
748 		/* assert: name not same as p->name */
749 		for (alias = p->p_aliases; *alias; alias++)
750 			if (strcmp(name, *alias) == 0) {
751 				endprotoent();
752 				return (knownname(p->p_name));
753 			}
754 	}
755 	endprotoent();
756 	return (NULL);
757 }
758 
759 static void
760 usage(void)
761 {
762 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
763 "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
764 "               [-M core] [-N system]",
765 "       netstat -i | -I interface [-abdhnt] [-f address_family]\n"
766 "               [-M core] [-N system]",
767 "       netstat -w wait [-I interface] [-d] [-M core] [-N system]",
768 "       netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
769 "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
770 "               [-M core] [-N system]",
771 "       netstat -m [-M core] [-N system]",
772 "       netstat -B [ -I interface]",
773 "       netstat -r [-AenW] [-f address_family] [-M core] [-N system]",
774 "       netstat -rs [-s] [-M core] [-N system]",
775 "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
776 "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
777 	exit(1);
778 }
779