2 * Copyright (c) 1983, 1988, 1993
3 * Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 char const copyright[] =
32 "@(#) Copyright (c) 1983, 1988, 1993\n\
33 Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
51 #include <netinet/in.h>
54 #include <netgraph/ng_socket.h>
72 static struct nlist nl[] = {
74 { .n_name = "_ifnet" }, /* XXXGL: can be deleted */
76 { .n_name = "_rtstat" },
78 { .n_name = "_rt_tables"},
80 { .n_name = "_mrtstat" },
81 #define N_MFCHASHTBL 4
82 { .n_name = "_mfchashtbl" },
84 { .n_name = "_viftable" },
86 { .n_name = "_ipxpcb_list"},
88 { .n_name = "_ipxstat"},
90 { .n_name = "_spx_istat"},
92 { .n_name = "_ddpstat"},
94 { .n_name = "_ddpcb"},
96 { .n_name = "_ngsocklist"},
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" },
116 { .n_name = "_rttrash" },
117 #define N_CARPSTAT 22
118 { .n_name = "_carpstats" },
119 #define N_PFSYNCSTAT 23
120 { .n_name = "_pfsyncstats" },
122 { .n_name = "_ahstat" },
124 { .n_name = "_espstat" },
125 #define N_IPCOMPSTAT 26
126 { .n_name = "_ipcompstat" },
128 { .n_name = "_tcpstat" },
130 { .n_name = "_udpstat" },
132 { .n_name = "_ipstat" },
133 #define N_ICMPSTAT 30
134 { .n_name = "_icmpstat" },
135 #define N_IGMPSTAT 31
136 { .n_name = "_igmpstat" },
138 { .n_name = "_pimstat" },
140 { .n_name = "_tcbinfo" },
142 { .n_name = "_udbinfo" },
143 #define N_DIVCBINFO 35
144 { .n_name = "_divcbinfo" },
145 #define N_RIPCBINFO 36
146 { .n_name = "_ripcbinfo" },
147 #define N_UNP_COUNT 37
148 { .n_name = "_unp_count" },
149 #define N_UNP_GENCNT 38
150 { .n_name = "_unp_gencnt" },
151 #define N_UNP_DHEAD 39
152 { .n_name = "_unp_dhead" },
153 #define N_UNP_SHEAD 40
154 { .n_name = "_unp_shead" },
155 #define N_RIP6STAT 41
156 { .n_name = "_rip6stat" },
157 #define N_SCTPSTAT 42
158 { .n_name = "_sctpstat" },
159 #define N_MFCTABLESIZE 43
160 { .n_name = "_mfctablesize" },
162 { .n_name = "_arpstat" },
163 #define N_UNP_SPHEAD 45
164 { .n_name = "unp_sphead" },
166 { .n_name = "_sfstat"},
171 int pr_index; /* index into nlist of cb head */
172 int pr_sindex; /* index into nlist of stat block */
173 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
174 void (*pr_cblocks)(u_long, const char *, int, int);
175 /* control blocks printing routine */
176 void (*pr_stats)(u_long, const char *, int, int);
177 /* statistics printing routine */
178 void (*pr_istats)(char *); /* per/if statistics printing routine */
179 const char *pr_name; /* well-known name */
180 int pr_usesysctl; /* non-zero if we use sysctl, not kvm */
183 { N_TCBINFO, N_TCPSTAT, 1, protopr,
184 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
185 { N_UDBINFO, N_UDPSTAT, 1, protopr,
186 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
188 { -1, N_SCTPSTAT, 1, sctp_protopr,
189 sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP },
192 { -1, -1, 1, protopr,
193 NULL, NULL, "sdp", 1, IPPROTO_TCP },
195 { N_DIVCBINFO, -1, 1, protopr,
196 NULL, NULL, "divert", 1, IPPROTO_DIVERT },
197 { N_RIPCBINFO, N_IPSTAT, 1, protopr,
198 ip_stats, NULL, "ip", 1, IPPROTO_RAW },
199 { N_RIPCBINFO, N_ICMPSTAT, 1, protopr,
200 icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP },
201 { N_RIPCBINFO, N_IGMPSTAT, 1, protopr,
202 igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP },
204 { -1, N_IPSECSTAT, 1, NULL, /* keep as compat */
205 ipsec_stats, NULL, "ipsec", 0, 0},
206 { -1, N_AHSTAT, 1, NULL,
207 ah_stats, NULL, "ah", 0, 0},
208 { -1, N_ESPSTAT, 1, NULL,
209 esp_stats, NULL, "esp", 0, 0},
210 { -1, N_IPCOMPSTAT, 1, NULL,
211 ipcomp_stats, NULL, "ipcomp", 0, 0},
213 { N_RIPCBINFO, N_PIMSTAT, 1, protopr,
214 pim_stats, NULL, "pim", 1, IPPROTO_PIM },
215 { -1, N_CARPSTAT, 1, NULL,
216 carp_stats, NULL, "carp", 1, 0 },
218 { -1, N_PFSYNCSTAT, 1, NULL,
219 pfsync_stats, NULL, "pfsync", 1, 0 },
221 { -1, N_ARPSTAT, 1, NULL,
222 arp_stats, NULL, "arp", 1, 0 },
224 NULL, NULL, NULL, 0, 0 }
228 struct protox ip6protox[] = {
229 { N_TCBINFO, N_TCPSTAT, 1, protopr,
230 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
231 { N_UDBINFO, N_UDPSTAT, 1, protopr,
232 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
233 { N_RIPCBINFO, N_IP6STAT, 1, protopr,
234 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW },
235 { N_RIPCBINFO, N_ICMP6STAT, 1, protopr,
236 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 },
238 { -1, -1, 1, protopr,
239 NULL, NULL, "sdp", 1, IPPROTO_TCP },
242 { -1, N_IPSEC6STAT, 1, NULL,
243 ipsec_stats, NULL, "ipsec6", 0, 0 },
246 { -1, N_PIM6STAT, 1, NULL,
247 pim6_stats, NULL, "pim6", 1, 0 },
249 { -1, N_RIP6STAT, 1, NULL,
250 rip6_stats, NULL, "rip6", 1, 0 },
252 NULL, NULL, NULL, 0, 0 }
257 struct protox pfkeyprotox[] = {
258 { -1, N_PFKEYSTAT, 1, NULL,
259 pfkey_stats, NULL, "pfkey", 0, 0 },
261 NULL, NULL, NULL, 0, 0 }
265 struct protox atalkprotox[] = {
266 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
267 ddp_stats, NULL, "ddp", 0, 0 },
269 NULL, NULL, NULL, 0, 0 }
272 struct protox netgraphprotox[] = {
273 { N_NGSOCKS, -1, 1, netgraphprotopr,
274 NULL, NULL, "ctrl", 0, 0 },
275 { N_NGSOCKS, -1, 1, netgraphprotopr,
276 NULL, NULL, "data", 0, 0 },
278 NULL, NULL, NULL, 0, 0 }
282 struct protox ipxprotox[] = {
283 { N_IPX, N_IPXSTAT, 1, ipxprotopr,
284 ipx_stats, NULL, "ipx", 0, 0 },
285 { N_IPX, N_SPXSTAT, 1, ipxprotopr,
286 spx_stats, NULL, "spx", 0, 0 },
288 NULL, NULL, 0, 0, 0 }
292 struct protox *protoprotox[] = {
305 static void printproto(struct protox *, const char *);
306 static void usage(void);
307 static struct protox *name2protox(const char *);
308 static struct protox *knownname(const char *);
311 static char *nlistf = NULL, *memf = NULL;
313 int Aflag; /* show addresses of protocol control block */
314 int aflag; /* show all sockets (including servers) */
315 int Bflag; /* show information about bpf consumers */
316 int bflag; /* show i/f total bytes in/out */
317 int dflag; /* show i/f dropped packets */
318 int gflag; /* show group (multicast) routing or stats */
319 int hflag; /* show counters in human readable format */
320 int iflag; /* show interfaces */
321 int Lflag; /* show size of listen queues */
322 int mflag; /* show memory stats */
323 int noutputs = 0; /* how much outputs before we exit */
324 int numeric_addr; /* show addresses numerically */
325 int numeric_port; /* show ports numerically */
326 static int pflag; /* show given protocol */
327 int Qflag; /* show netisr information */
328 int rflag; /* show routing tables (or routing stats) */
329 int Rflag; /* show flow / RSS statistics */
330 int sflag; /* show protocol statistics */
331 int Wflag; /* wide display */
332 int Tflag; /* TCP Information */
333 int xflag; /* extra information, includes all socket buffer info */
334 int zflag; /* zero stats */
336 int interval; /* repeat interval for i/f stats */
338 char *interface; /* desired i/f for stats, or NULL for all i/fs */
339 int unit; /* unit number for above */
341 int af; /* address family */
342 int live; /* true if we are examining a live system */
345 main(int argc, char *argv[])
347 struct protox *tp = NULL; /* for printing cblocks & stats */
354 while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:RrSTsuWw:xz"))
361 errx(1, "IPv4 support is not compiled in");
368 errx(1, "IPv6 support is not compiled in");
387 fib = strtol(optarg, &endptr, 0);
388 if (*endptr != '\0' ||
389 (fib == 0 && (errno == EINVAL || errno == ERANGE)))
390 errx(1, "%s: invalid fib", optarg);
393 if (strcmp(optarg, "ipx") == 0)
395 else if (strcmp(optarg, "inet") == 0)
398 else if (strcmp(optarg, "inet6") == 0)
402 else if (strcmp(optarg, "pfkey") == 0)
405 else if (strcmp(optarg, "unix") == 0)
407 else if (strcmp(optarg, "atalk") == 0)
410 else if (strcmp(optarg, "ng") == 0
411 || strcmp(optarg, "netgraph") == 0)
414 else if (strcmp(optarg, "link") == 0)
417 errx(1, "%s: unknown address family", optarg);
430 for (cp = interface = optarg; isalpha(*cp); cp++)
451 numeric_addr = numeric_port = 1;
454 if ((tp = name2protox(optarg)) == NULL) {
456 "%s: unknown or uninstrumented protocol",
465 noutputs = atoi(optarg);
489 interval = atoi(optarg);
508 #define BACKWARD_COMPATIBILITY
509 #ifdef BACKWARD_COMPATIBILITY
511 if (isdigit(**argv)) {
512 interval = atoi(*argv);
527 * Discard setgid privileges if not the running kernel so that bad
528 * guys can't print interesting stuff from kernel memory.
530 live = (nlistf == NULL && memf == NULL);
535 errx(1, "-x and -T are incompatible, pick one.");
540 bpf_stats(interface);
545 if (kread(0, NULL, 0) == 0)
546 mbpr(kvmd, nl[N_SFSTAT].n_value);
553 if (kread(0, NULL, 0) == 0)
561 * Keep file descriptors open to avoid overhead
562 * of open/close on each call to get* routines.
568 * This does not make sense any more with DNS being default over
569 * the files. Doing a setXXXXent(1) causes a tcp connection to be
570 * used for the queries, which is slower.
573 if (iflag && !sflag) {
574 intpr(interval, NULL, af);
588 if (af == AF_INET || af == AF_UNSPEC)
591 if (af == AF_INET6 || af == AF_UNSPEC)
595 if (af == AF_INET || af == AF_UNSPEC)
598 if (af == AF_INET6 || af == AF_UNSPEC)
605 /* Load all necessary kvm symbols */
609 printproto(tp, tp->pr_name);
612 if (af == AF_INET || af == AF_UNSPEC)
613 for (tp = protox; tp->pr_name; tp++)
614 printproto(tp, tp->pr_name);
616 if (af == AF_INET6 || af == AF_UNSPEC)
617 for (tp = ip6protox; tp->pr_name; tp++)
618 printproto(tp, tp->pr_name);
621 if (af == PF_KEY || af == AF_UNSPEC)
622 for (tp = pfkeyprotox; tp->pr_name; tp++)
623 printproto(tp, tp->pr_name);
626 if (af == AF_IPX || af == AF_UNSPEC) {
627 for (tp = ipxprotox; tp->pr_name; tp++)
628 printproto(tp, tp->pr_name);
631 if (af == AF_APPLETALK || af == AF_UNSPEC)
632 for (tp = atalkprotox; tp->pr_name; tp++)
633 printproto(tp, tp->pr_name);
635 if (af == AF_NETGRAPH || af == AF_UNSPEC)
636 for (tp = netgraphprotox; tp->pr_name; tp++)
637 printproto(tp, tp->pr_name);
638 #endif /* NETGRAPH */
639 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
640 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
641 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
642 nl[N_UNP_SPHEAD].n_value);
647 * Print out protocol statistics or control blocks (per sflag).
648 * If the interface was not specifically requested, and the symbol
649 * is not in the namelist, ignore this one.
652 printproto(struct protox *tp, const char *name)
654 void (*pr)(u_long, const char *, int, int);
660 intpr(interval, tp->pr_istats, af);
662 printf("%s: no per-interface stats routine\n",
669 printf("%s: no stats routine\n",
673 if (tp->pr_usesysctl && live)
675 else if (tp->pr_sindex < 0) {
678 "%s: stats routine doesn't work on cores\n",
682 off = nl[tp->pr_sindex].n_value;
688 printf("%s: no PCB routine\n", tp->pr_name);
691 if (tp->pr_usesysctl && live)
693 else if (tp->pr_index < 0) {
696 "%s: PCB routine doesn't work on cores\n",
700 off = nl[tp->pr_index].n_value;
702 if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
704 (*pr)(off, name, af, tp->pr_protocol);
710 char errbuf[_POSIX2_LINE_MAX];
715 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
719 warnx("kvm not available: %s", errbuf);
727 * Resolve symbol list, return 0 on success.
730 kresolve_list(struct nlist *_nl)
733 if ((kvmd == NULL) && (kvmd_init() != 0))
736 if (_nl[0].n_type != 0)
739 if (kvm_nlist(kvmd, _nl) < 0) {
741 errx(1, "%s: kvm_nlist: %s", nlistf,
744 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
751 * Read kernel memory, return 0 on success.
754 kread(u_long addr, void *buf, size_t size)
762 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
763 warnx("%s", kvm_geterr(kvmd));
770 * Read single counter(9).
773 kread_counter(u_long addr)
779 return (kvm_counter_u64_fetch(kvmd, addr));
783 * Read an array of N counters in kernel memory into array of N uint64_t's.
786 kread_counters(u_long addr, void *buf, size_t size)
795 if (size % sizeof(uint64_t) != 0) {
796 warnx("kread_counters: invalid counter set size");
800 n = size / sizeof(uint64_t);
801 if ((counters = malloc(n * sizeof(u_long))) == NULL)
803 if (kread(addr, counters, n * sizeof(u_long)) < 0) {
809 for (i = 0; i < n; i++)
810 c[i] = kvm_counter_u64_fetch(kvmd, counters[i]);
819 return (n != 1 ? "s" : "");
823 plurales(uintmax_t n)
825 return (n != 1 ? "es" : "");
829 pluralies(uintmax_t n)
831 return (n != 1 ? "ies" : "y");
835 * Find the protox for the given "well-known" name.
837 static struct protox *
838 knownname(const char *name)
840 struct protox **tpp, *tp;
842 for (tpp = protoprotox; *tpp; tpp++)
843 for (tp = *tpp; tp->pr_name; tp++)
844 if (strcmp(tp->pr_name, name) == 0)
850 * Find the protox corresponding to name.
852 static struct protox *
853 name2protox(const char *name)
856 char **alias; /* alias from p->aliases */
860 * Try to find the name in the list of "well-known" names. If that
861 * fails, check if name is an alias for an Internet protocol.
863 if ((tp = knownname(name)) != NULL)
866 setprotoent(1); /* make protocol lookup cheaper */
867 while ((p = getprotoent()) != NULL) {
868 /* assert: name not same as p->name */
869 for (alias = p->p_aliases; *alias; alias++)
870 if (strcmp(name, *alias) == 0) {
872 return (knownname(p->p_name));
882 (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",
883 "usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n"
884 " [-M core] [-N system]",
885 " netstat -i | -I interface [-46abdhnW] [-f address_family]\n"
886 " [-M core] [-N system]",
887 " netstat -w wait [-I interface] [-46d] [-M core] [-N system] [-q howmany]",
888 " netstat -s [-s] [-46z] [-f protocol_family | -p protocol]\n"
889 " [-M core] [-N system]",
890 " netstat -i | -I interface [-46s] [-f protocol_family | -p protocol]\n"
891 " [-M core] [-N system]",
892 " netstat -m [-M core] [-N system]",
893 " netstat -B [-I interface]",
894 " netstat -r [-46AanW] [-f address_family] [-M core] [-N system]",
895 " netstat -rs [-s] [-M core] [-N system]",
896 " netstat -g [-46W] [-f address_family] [-M core] [-N system]",
897 " netstat -gs [-46s] [-f address_family] [-M core] [-N system]",