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