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 sflag; /* show protocol statistics */
330 int Wflag; /* wide display */
331 int Tflag; /* TCP Information */
332 int xflag; /* extra information, includes all socket buffer info */
333 int zflag; /* zero stats */
335 int interval; /* repeat interval for i/f stats */
337 char *interface; /* desired i/f for stats, or NULL for all i/fs */
338 int unit; /* unit number for above */
340 int af; /* address family */
341 int live; /* true if we are examining a live system */
344 main(int argc, char *argv[])
346 struct protox *tp = NULL; /* for printing cblocks & stats */
353 while ((ch = getopt(argc, argv, "AaBbdF:f:ghI:iLlM:mN:np:Qq:rSTsuWw:xz"))
372 fib = strtol(optarg, &endptr, 0);
373 if (*endptr != '\0' ||
374 (fib == 0 && (errno == EINVAL || errno == ERANGE)))
375 errx(1, "%s: invalid fib", optarg);
378 if (strcmp(optarg, "ipx") == 0)
380 else if (strcmp(optarg, "inet") == 0)
383 else if (strcmp(optarg, "inet6") == 0)
387 else if (strcmp(optarg, "pfkey") == 0)
390 else if (strcmp(optarg, "unix") == 0)
392 else if (strcmp(optarg, "atalk") == 0)
395 else if (strcmp(optarg, "ng") == 0
396 || strcmp(optarg, "netgraph") == 0)
399 else if (strcmp(optarg, "link") == 0)
402 errx(1, "%s: unknown address family", optarg);
415 for (cp = interface = optarg; isalpha(*cp); cp++)
436 numeric_addr = numeric_port = 1;
439 if ((tp = name2protox(optarg)) == NULL) {
441 "%s: unknown or uninstrumented protocol",
450 noutputs = atoi(optarg);
471 interval = atoi(optarg);
490 #define BACKWARD_COMPATIBILITY
491 #ifdef BACKWARD_COMPATIBILITY
493 if (isdigit(**argv)) {
494 interval = atoi(*argv);
509 * Discard setgid privileges if not the running kernel so that bad
510 * guys can't print interesting stuff from kernel memory.
512 live = (nlistf == NULL && memf == NULL);
517 errx(1, "-x and -T are incompatible, pick one.");
522 bpf_stats(interface);
527 if (kread(0, NULL, 0) == 0)
528 mbpr(kvmd, nl[N_SFSTAT].n_value);
535 if (kread(0, NULL, 0) == 0)
543 * Keep file descriptors open to avoid overhead
544 * of open/close on each call to get* routines.
550 * This does not make sense any more with DNS being default over
551 * the files. Doing a setXXXXent(1) causes a tcp connection to be
552 * used for the queries, which is slower.
556 if (iflag && !sflag) {
557 intpr(interval, NULL);
562 rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
565 routepr(nl[N_RTREE].n_value, fib);
570 if (af == AF_INET || af == AF_UNSPEC)
571 mrt_stats(nl[N_MRTSTAT].n_value);
573 if (af == AF_INET6 || af == AF_UNSPEC)
574 mrt6_stats(nl[N_MRT6STAT].n_value);
577 if (af == AF_INET || af == AF_UNSPEC)
578 mroutepr(nl[N_MFCHASHTBL].n_value,
579 nl[N_MFCTABLESIZE].n_value,
580 nl[N_VIFTABLE].n_value);
582 if (af == AF_INET6 || af == AF_UNSPEC)
583 mroute6pr(nl[N_MF6CTABLE].n_value,
584 nl[N_MIF6TABLE].n_value);
591 printproto(tp, tp->pr_name);
594 if (af == AF_INET || af == AF_UNSPEC)
595 for (tp = protox; tp->pr_name; tp++)
596 printproto(tp, tp->pr_name);
598 if (af == AF_INET6 || af == AF_UNSPEC)
599 for (tp = ip6protox; tp->pr_name; tp++)
600 printproto(tp, tp->pr_name);
603 if (af == PF_KEY || af == AF_UNSPEC)
604 for (tp = pfkeyprotox; tp->pr_name; tp++)
605 printproto(tp, tp->pr_name);
608 if (af == AF_IPX || af == AF_UNSPEC) {
609 for (tp = ipxprotox; tp->pr_name; tp++)
610 printproto(tp, tp->pr_name);
613 if (af == AF_APPLETALK || af == AF_UNSPEC)
614 for (tp = atalkprotox; tp->pr_name; tp++)
615 printproto(tp, tp->pr_name);
617 if (af == AF_NETGRAPH || af == AF_UNSPEC)
618 for (tp = netgraphprotox; tp->pr_name; tp++)
619 printproto(tp, tp->pr_name);
620 #endif /* NETGRAPH */
621 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
622 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
623 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
624 nl[N_UNP_SPHEAD].n_value);
629 * Print out protocol statistics or control blocks (per sflag).
630 * If the interface was not specifically requested, and the symbol
631 * is not in the namelist, ignore this one.
634 printproto(struct protox *tp, const char *name)
636 void (*pr)(u_long, const char *, int, int);
642 intpr(interval, tp->pr_istats);
644 printf("%s: no per-interface stats routine\n",
651 printf("%s: no stats routine\n",
655 if (tp->pr_usesysctl && live)
657 else if (tp->pr_sindex < 0) {
660 "%s: stats routine doesn't work on cores\n",
664 off = nl[tp->pr_sindex].n_value;
670 printf("%s: no PCB routine\n", tp->pr_name);
673 if (tp->pr_usesysctl && live)
675 else if (tp->pr_index < 0) {
678 "%s: PCB routine doesn't work on cores\n",
682 off = nl[tp->pr_index].n_value;
684 if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
686 (*pr)(off, name, af, tp->pr_protocol);
692 char errbuf[_POSIX2_LINE_MAX];
697 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
701 warnx("kvm not available: %s", errbuf);
705 if (kvm_nlist(kvmd, nl) < 0) {
707 errx(1, "%s: kvm_nlist: %s", nlistf,
710 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
713 if (nl[0].n_type == 0) {
715 errx(1, "%s: no namelist", nlistf);
717 errx(1, "no namelist");
724 * Read kernel memory, return 0 on success.
727 kread(u_long addr, void *buf, size_t size)
735 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
736 warnx("%s", kvm_geterr(kvmd));
743 * Read an array of N counters in kernel memory into array of N uint64_t's.
746 kread_counters(u_long addr, void *buf, size_t size)
753 if (kread(addr, buf, size) < 0)
757 *c = kvm_counter_u64_fetch(kvmd, *c);
767 return (n != 1 ? "s" : "");
771 plurales(uintmax_t n)
773 return (n != 1 ? "es" : "");
777 pluralies(uintmax_t n)
779 return (n != 1 ? "ies" : "y");
783 * Find the protox for the given "well-known" name.
785 static struct protox *
786 knownname(const char *name)
788 struct protox **tpp, *tp;
790 for (tpp = protoprotox; *tpp; tpp++)
791 for (tp = *tpp; tp->pr_name; tp++)
792 if (strcmp(tp->pr_name, name) == 0)
798 * Find the protox corresponding to name.
800 static struct protox *
801 name2protox(const char *name)
804 char **alias; /* alias from p->aliases */
808 * Try to find the name in the list of "well-known" names. If that
809 * fails, check if name is an alias for an Internet protocol.
811 if ((tp = knownname(name)) != NULL)
814 setprotoent(1); /* make protocol lookup cheaper */
815 while ((p = getprotoent()) != NULL) {
816 /* assert: name not same as p->name */
817 for (alias = p->p_aliases; *alias; alias++)
818 if (strcmp(name, *alias) == 0) {
820 return (knownname(p->p_name));
830 (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",
831 "usage: netstat [-AaLnSTWx] [-f protocol_family | -p protocol]\n"
832 " [-M core] [-N system]",
833 " netstat -i | -I interface [-abdhnW] [-f address_family]\n"
834 " [-M core] [-N system]",
835 " netstat -w wait [-I interface] [-d] [-M core] [-N system] [-q howmany]",
836 " netstat -s [-s] [-z] [-f protocol_family | -p protocol]\n"
837 " [-M core] [-N system]",
838 " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
839 " [-M core] [-N system]",
840 " netstat -m [-M core] [-N system]",
841 " netstat -B [-I interface]",
842 " netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
843 " netstat -rs [-s] [-M core] [-N system]",
844 " netstat -g [-W] [-f address_family] [-M core] [-N system]",
845 " netstat -gs [-s] [-f address_family] [-M core] [-N system]",