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