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