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" },
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 },
217 { -1, N_PFSYNCSTAT, 1, NULL,
218 pfsync_stats, NULL, "pfsync", 1, 0 },
219 { -1, N_ARPSTAT, 1, NULL,
220 arp_stats, NULL, "arp", 1, 0 },
222 NULL, NULL, NULL, 0, 0 }
226 struct protox ip6protox[] = {
227 { N_TCBINFO, N_TCPSTAT, 1, protopr,
228 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
229 { N_UDBINFO, N_UDPSTAT, 1, protopr,
230 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
231 { N_RIPCBINFO, N_IP6STAT, 1, protopr,
232 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW },
233 { N_RIPCBINFO, N_ICMP6STAT, 1, protopr,
234 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 },
236 { -1, -1, 1, protopr,
237 NULL, NULL, "sdp", 1, IPPROTO_TCP },
240 { -1, N_IPSEC6STAT, 1, NULL,
241 ipsec_stats, NULL, "ipsec6", 0, 0 },
244 { -1, N_PIM6STAT, 1, NULL,
245 pim6_stats, NULL, "pim6", 1, 0 },
247 { -1, N_RIP6STAT, 1, NULL,
248 rip6_stats, NULL, "rip6", 1, 0 },
250 NULL, NULL, NULL, 0, 0 }
255 struct protox pfkeyprotox[] = {
256 { -1, N_PFKEYSTAT, 1, NULL,
257 pfkey_stats, NULL, "pfkey", 0, 0 },
259 NULL, NULL, NULL, 0, 0 }
263 struct protox atalkprotox[] = {
264 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
265 ddp_stats, NULL, "ddp", 0, 0 },
267 NULL, NULL, NULL, 0, 0 }
270 struct protox netgraphprotox[] = {
271 { N_NGSOCKS, -1, 1, netgraphprotopr,
272 NULL, NULL, "ctrl", 0, 0 },
273 { N_NGSOCKS, -1, 1, netgraphprotopr,
274 NULL, NULL, "data", 0, 0 },
276 NULL, NULL, NULL, 0, 0 }
280 struct protox ipxprotox[] = {
281 { N_IPX, N_IPXSTAT, 1, ipxprotopr,
282 ipx_stats, NULL, "ipx", 0, 0 },
283 { N_IPX, N_SPXSTAT, 1, ipxprotopr,
284 spx_stats, NULL, "spx", 0, 0 },
286 NULL, NULL, 0, 0, 0 }
290 struct protox *protoprotox[] = {
303 static void printproto(struct protox *, const char *);
304 static void usage(void);
305 static struct protox *name2protox(const char *);
306 static struct protox *knownname(const char *);
309 static char *nlistf = NULL, *memf = NULL;
311 int Aflag; /* show addresses of protocol control block */
312 int aflag; /* show all sockets (including servers) */
313 int Bflag; /* show information about bpf consumers */
314 int bflag; /* show i/f total bytes in/out */
315 int dflag; /* show i/f dropped packets */
316 int gflag; /* show group (multicast) routing or stats */
317 int hflag; /* show counters in human readable format */
318 int iflag; /* show interfaces */
319 int Lflag; /* show size of listen queues */
320 int mflag; /* show memory stats */
321 int noutputs = 0; /* how much outputs before we exit */
322 int numeric_addr; /* show addresses numerically */
323 int numeric_port; /* show ports numerically */
324 static int pflag; /* show given protocol */
325 int Qflag; /* show netisr information */
326 int rflag; /* show routing tables (or routing stats) */
327 int sflag; /* show protocol statistics */
328 int Wflag; /* wide display */
329 int Tflag; /* TCP Information */
330 int xflag; /* extra information, includes all socket buffer info */
331 int zflag; /* zero stats */
333 int interval; /* repeat interval for i/f stats */
335 char *interface; /* desired i/f for stats, or NULL for all i/fs */
336 int unit; /* unit number for above */
338 int af; /* address family */
339 int live; /* true if we are examining a live system */
342 main(int argc, char *argv[])
344 struct protox *tp = NULL; /* for printing cblocks & stats */
351 while ((ch = getopt(argc, argv, "AaBbdF:f:ghI:iLlM:mN:np:Qq:rSTsuWw:xz"))
370 fib = strtol(optarg, &endptr, 0);
371 if (*endptr != '\0' ||
372 (fib == 0 && (errno == EINVAL || errno == ERANGE)))
373 errx(1, "%s: invalid fib", optarg);
376 if (strcmp(optarg, "ipx") == 0)
378 else if (strcmp(optarg, "inet") == 0)
381 else if (strcmp(optarg, "inet6") == 0)
385 else if (strcmp(optarg, "pfkey") == 0)
388 else if (strcmp(optarg, "unix") == 0)
390 else if (strcmp(optarg, "atalk") == 0)
393 else if (strcmp(optarg, "ng") == 0
394 || strcmp(optarg, "netgraph") == 0)
397 else if (strcmp(optarg, "link") == 0)
400 errx(1, "%s: unknown address family", optarg);
413 for (cp = interface = optarg; isalpha(*cp); cp++)
434 numeric_addr = numeric_port = 1;
437 if ((tp = name2protox(optarg)) == NULL) {
439 "%s: unknown or uninstrumented protocol",
448 noutputs = atoi(optarg);
469 interval = atoi(optarg);
488 #define BACKWARD_COMPATIBILITY
489 #ifdef BACKWARD_COMPATIBILITY
491 if (isdigit(**argv)) {
492 interval = atoi(*argv);
507 * Discard setgid privileges if not the running kernel so that bad
508 * guys can't print interesting stuff from kernel memory.
510 live = (nlistf == NULL && memf == NULL);
515 errx(1, "-x and -T are incompatible, pick one.");
520 bpf_stats(interface);
525 if (kread(0, NULL, 0) == 0)
526 mbpr(kvmd, nl[N_SFSTAT].n_value);
533 if (kread(0, NULL, 0) == 0)
541 * Keep file descriptors open to avoid overhead
542 * of open/close on each call to get* routines.
548 * This does not make sense any more with DNS being default over
549 * the files. Doing a setXXXXent(1) causes a tcp connection to be
550 * used for the queries, which is slower.
554 if (iflag && !sflag) {
555 intpr(interval, nl[N_IFNET].n_value, NULL);
560 rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
562 routepr(nl[N_RTREE].n_value, fib);
567 if (af == AF_INET || af == AF_UNSPEC)
568 mrt_stats(nl[N_MRTSTAT].n_value);
570 if (af == AF_INET6 || af == AF_UNSPEC)
571 mrt6_stats(nl[N_MRT6STAT].n_value);
574 if (af == AF_INET || af == AF_UNSPEC)
575 mroutepr(nl[N_MFCHASHTBL].n_value,
576 nl[N_MFCTABLESIZE].n_value,
577 nl[N_VIFTABLE].n_value);
579 if (af == AF_INET6 || af == AF_UNSPEC)
580 mroute6pr(nl[N_MF6CTABLE].n_value,
581 nl[N_MIF6TABLE].n_value);
588 printproto(tp, tp->pr_name);
591 if (af == AF_INET || af == AF_UNSPEC)
592 for (tp = protox; tp->pr_name; tp++)
593 printproto(tp, tp->pr_name);
595 if (af == AF_INET6 || af == AF_UNSPEC)
596 for (tp = ip6protox; tp->pr_name; tp++)
597 printproto(tp, tp->pr_name);
600 if (af == PF_KEY || af == AF_UNSPEC)
601 for (tp = pfkeyprotox; tp->pr_name; tp++)
602 printproto(tp, tp->pr_name);
605 if (af == AF_IPX || af == AF_UNSPEC) {
606 for (tp = ipxprotox; tp->pr_name; tp++)
607 printproto(tp, tp->pr_name);
610 if (af == AF_APPLETALK || af == AF_UNSPEC)
611 for (tp = atalkprotox; tp->pr_name; tp++)
612 printproto(tp, tp->pr_name);
614 if (af == AF_NETGRAPH || af == AF_UNSPEC)
615 for (tp = netgraphprotox; tp->pr_name; tp++)
616 printproto(tp, tp->pr_name);
617 #endif /* NETGRAPH */
618 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
619 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
620 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
621 nl[N_UNP_SPHEAD].n_value);
626 * Print out protocol statistics or control blocks (per sflag).
627 * If the interface was not specifically requested, and the symbol
628 * is not in the namelist, ignore this one.
631 printproto(struct protox *tp, const char *name)
633 void (*pr)(u_long, const char *, int, int);
639 intpr(interval, nl[N_IFNET].n_value,
642 printf("%s: no per-interface stats routine\n",
649 printf("%s: no stats routine\n",
653 if (tp->pr_usesysctl && live)
655 else if (tp->pr_sindex < 0) {
658 "%s: stats routine doesn't work on cores\n",
662 off = nl[tp->pr_sindex].n_value;
668 printf("%s: no PCB routine\n", tp->pr_name);
671 if (tp->pr_usesysctl && live)
673 else if (tp->pr_index < 0) {
676 "%s: PCB routine doesn't work on cores\n",
680 off = nl[tp->pr_index].n_value;
682 if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
684 (*pr)(off, name, af, tp->pr_protocol);
690 char errbuf[_POSIX2_LINE_MAX];
695 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
699 warnx("kvm not available: %s", errbuf);
703 if (kvm_nlist(kvmd, nl) < 0) {
705 errx(1, "%s: kvm_nlist: %s", nlistf,
708 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
711 if (nl[0].n_type == 0) {
713 errx(1, "%s: no namelist", nlistf);
715 errx(1, "no namelist");
722 * Read kernel memory, return 0 on success.
725 kread(u_long addr, void *buf, size_t size)
733 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
734 warnx("%s", kvm_geterr(kvmd));
741 * Read an array of N counters in kernel memory into array of N uint64_t's.
744 kread_counters(u_long addr, void *buf, size_t size)
751 if (kread(addr, buf, size) < 0)
755 *c = kvm_counter_u64_fetch(kvmd, *c);
765 return (n != 1 ? "s" : "");
769 plurales(uintmax_t n)
771 return (n != 1 ? "es" : "");
775 pluralies(uintmax_t n)
777 return (n != 1 ? "ies" : "y");
781 * Find the protox for the given "well-known" name.
783 static struct protox *
784 knownname(const char *name)
786 struct protox **tpp, *tp;
788 for (tpp = protoprotox; *tpp; tpp++)
789 for (tp = *tpp; tp->pr_name; tp++)
790 if (strcmp(tp->pr_name, name) == 0)
796 * Find the protox corresponding to name.
798 static struct protox *
799 name2protox(const char *name)
802 char **alias; /* alias from p->aliases */
806 * Try to find the name in the list of "well-known" names. If that
807 * fails, check if name is an alias for an Internet protocol.
809 if ((tp = knownname(name)) != NULL)
812 setprotoent(1); /* make protocol lookup cheaper */
813 while ((p = getprotoent()) != NULL) {
814 /* assert: name not same as p->name */
815 for (alias = p->p_aliases; *alias; alias++)
816 if (strcmp(name, *alias) == 0) {
818 return (knownname(p->p_name));
828 (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",
829 "usage: netstat [-AaLnSTWx] [-f protocol_family | -p protocol]\n"
830 " [-M core] [-N system]",
831 " netstat -i | -I interface [-abdhnW] [-f address_family]\n"
832 " [-M core] [-N system]",
833 " netstat -w wait [-I interface] [-d] [-M core] [-N system] [-q howmany]",
834 " netstat -s [-s] [-z] [-f protocol_family | -p protocol]\n"
835 " [-M core] [-N system]",
836 " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
837 " [-M core] [-N system]",
838 " netstat -m [-M core] [-N system]",
839 " netstat -B [-I interface]",
840 " netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
841 " netstat -rs [-s] [-M core] [-N system]",
842 " netstat -g [-W] [-f address_family] [-M core] [-N system]",
843 " netstat -gs [-s] [-f address_family] [-M core] [-N system]",