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 * 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.
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
35 char const copyright[] =
36 "@(#) Copyright (c) 1983, 1988, 1993\n\
37 Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/param.h>
51 #include <sys/protosw.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
55 #include <netinet/in.h>
58 #include <netgraph/ng_socket.h>
76 static struct nlist nl[] = {
78 { .n_name = "_ifnet" },
80 { .n_name = "_rtstat" },
82 { .n_name = "_rt_tables"},
84 { .n_name = "_mrtstat" },
85 #define N_MFCHASHTBL 4
86 { .n_name = "_mfchashtbl" },
88 { .n_name = "_viftable" },
90 { .n_name = "_ipxpcb_list"},
92 { .n_name = "_ipxstat"},
94 { .n_name = "_spx_istat"},
96 { .n_name = "_ddpstat"},
98 { .n_name = "_ddpcb"},
100 { .n_name = "_ngsocklist"},
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" },
120 { .n_name = "_mbstat" },
122 { .n_name = "_mbtypes" },
123 #define N_NMBCLUSTERS 23
124 { .n_name = "_nmbclusters" },
126 { .n_name = "_nmbufs" },
128 { .n_name = "_mbuf_hiwm" },
130 { .n_name = "_clust_hiwm" },
132 { .n_name = "_smp_cpus" },
134 { .n_name = "_pagesize" },
136 { .n_name = "_mb_statpcpu" },
138 { .n_name = "_rttrash" },
140 { .n_name = "_mbuf_lowm" },
142 { .n_name = "_clust_lowm" },
143 #define N_CARPSTAT 33
144 { .n_name = "_carpstats" },
145 #define N_PFSYNCSTAT 34
146 { .n_name = "_pfsyncstats" },
148 { .n_name = "_ahstat" },
150 { .n_name = "_espstat" },
151 #define N_IPCOMPSTAT 37
152 { .n_name = "_ipcompstat" },
154 { .n_name = "_tcpstat" },
156 { .n_name = "_udpstat" },
158 { .n_name = "_ipstat" },
159 #define N_ICMPSTAT 41
160 { .n_name = "_icmpstat" },
161 #define N_IGMPSTAT 42
162 { .n_name = "_igmpstat" },
164 { .n_name = "_pimstat" },
166 { .n_name = "_tcbinfo" },
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 #define N_MFCTABLESIZE 54
186 { .n_name = "_mfctablesize" },
188 { .n_name = "_arpstat" },
193 int pr_index; /* index into nlist of cb head */
194 int pr_sindex; /* index into nlist of stat block */
195 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
196 void (*pr_cblocks)(u_long, const char *, int, int);
197 /* control blocks printing routine */
198 void (*pr_stats)(u_long, const char *, int, int);
199 /* statistics printing routine */
200 void (*pr_istats)(char *); /* per/if statistics printing routine */
201 const char *pr_name; /* well-known name */
202 int pr_usesysctl; /* non-zero if we use sysctl, not kvm */
205 { N_TCBINFO, N_TCPSTAT, 1, protopr,
206 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
207 { N_UDBINFO, N_UDPSTAT, 1, protopr,
208 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
210 { -1, N_SCTPSTAT, 1, sctp_protopr,
211 sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP },
213 { N_DIVCBINFO, -1, 1, protopr,
214 NULL, NULL, "divert", 1, IPPROTO_DIVERT },
215 { N_RIPCBINFO, N_IPSTAT, 1, protopr,
216 ip_stats, NULL, "ip", 1, IPPROTO_RAW },
217 { N_RIPCBINFO, N_ICMPSTAT, 1, protopr,
218 icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP },
219 { N_RIPCBINFO, N_IGMPSTAT, 1, protopr,
220 igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP },
222 { -1, N_IPSECSTAT, 1, NULL, /* keep as compat */
223 ipsec_stats, NULL, "ipsec", 0, 0},
224 { -1, N_AHSTAT, 1, NULL,
225 ah_stats, NULL, "ah", 0, 0},
226 { -1, N_ESPSTAT, 1, NULL,
227 esp_stats, NULL, "esp", 0, 0},
228 { -1, N_IPCOMPSTAT, 1, NULL,
229 ipcomp_stats, NULL, "ipcomp", 0, 0},
231 { N_RIPCBINFO, N_PIMSTAT, 1, protopr,
232 pim_stats, NULL, "pim", 1, IPPROTO_PIM },
233 { -1, N_CARPSTAT, 1, NULL,
234 carp_stats, NULL, "carp", 1, 0 },
235 { -1, N_PFSYNCSTAT, 1, NULL,
236 pfsync_stats, NULL, "pfsync", 1, 0 },
237 { -1, N_ARPSTAT, 1, NULL,
238 arp_stats, NULL, "arp", 1, 0 },
240 NULL, NULL, NULL, 0, 0 }
244 struct protox ip6protox[] = {
245 { N_TCBINFO, N_TCPSTAT, 1, protopr,
246 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
247 { N_UDBINFO, N_UDPSTAT, 1, protopr,
248 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
249 { N_RIPCBINFO, N_IP6STAT, 1, protopr,
250 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW },
251 { N_RIPCBINFO, N_ICMP6STAT, 1, protopr,
252 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 },
254 { -1, N_IPSEC6STAT, 1, NULL,
255 ipsec_stats, NULL, "ipsec6", 0, 0 },
258 { -1, N_PIM6STAT, 1, NULL,
259 pim6_stats, NULL, "pim6", 1, 0 },
261 { -1, N_RIP6STAT, 1, NULL,
262 rip6_stats, NULL, "rip6", 1, 0 },
264 NULL, NULL, NULL, 0, 0 }
269 struct protox pfkeyprotox[] = {
270 { -1, N_PFKEYSTAT, 1, NULL,
271 pfkey_stats, NULL, "pfkey", 0, 0 },
273 NULL, NULL, NULL, 0, 0 }
277 struct protox atalkprotox[] = {
278 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
279 ddp_stats, NULL, "ddp", 0, 0 },
281 NULL, NULL, NULL, 0, 0 }
284 struct protox netgraphprotox[] = {
285 { N_NGSOCKS, -1, 1, netgraphprotopr,
286 NULL, NULL, "ctrl", 0, 0 },
287 { N_NGSOCKS, -1, 1, netgraphprotopr,
288 NULL, NULL, "data", 0, 0 },
290 NULL, NULL, NULL, 0, 0 }
294 struct protox ipxprotox[] = {
295 { N_IPX, N_IPXSTAT, 1, ipxprotopr,
296 ipx_stats, NULL, "ipx", 0, 0 },
297 { N_IPX, N_SPXSTAT, 1, ipxprotopr,
298 spx_stats, NULL, "spx", 0, 0 },
300 NULL, NULL, 0, 0, 0 }
304 struct protox *protoprotox[] = {
317 static void printproto(struct protox *, const char *);
318 static void usage(void);
319 static struct protox *name2protox(const char *);
320 static struct protox *knownname(const char *);
323 static char *nlistf = NULL, *memf = NULL;
325 int Aflag; /* show addresses of protocol control block */
326 int aflag; /* show all sockets (including servers) */
327 int Bflag; /* show information about bpf consumers */
328 int bflag; /* show i/f total bytes in/out */
329 int dflag; /* show i/f dropped packets */
330 int gflag; /* show group (multicast) routing or stats */
331 int hflag; /* show counters in human readable format */
332 int iflag; /* show interfaces */
333 int Lflag; /* show size of listen queues */
334 int mflag; /* show memory stats */
335 int noutputs = 0; /* how much outputs before we exit */
336 int numeric_addr; /* show addresses numerically */
337 int numeric_port; /* show ports numerically */
338 static int pflag; /* show given protocol */
339 int Qflag; /* show netisr information */
340 int rflag; /* show routing tables (or routing stats) */
341 int sflag; /* show protocol statistics */
342 int tflag; /* show i/f watchdog timers */
343 int Wflag; /* wide display */
344 int Tflag; /* TCP Information */
345 int xflag; /* extra information, includes all socket buffer info */
346 int zflag; /* zero stats */
348 int interval; /* repeat interval for i/f stats */
350 char *interface; /* desired i/f for stats, or NULL for all i/fs */
351 int unit; /* unit number for above */
353 int af; /* address family */
354 int live; /* true if we are examining a live system */
357 main(int argc, char *argv[])
359 struct protox *tp = NULL; /* for printing cblocks & stats */
364 while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:Qq:rSTstuWw:xz"))
383 if (strcmp(optarg, "ipx") == 0)
385 else if (strcmp(optarg, "inet") == 0)
388 else if (strcmp(optarg, "inet6") == 0)
392 else if (strcmp(optarg, "pfkey") == 0)
395 else if (strcmp(optarg, "unix") == 0)
397 else if (strcmp(optarg, "atalk") == 0)
400 else if (strcmp(optarg, "ng") == 0
401 || strcmp(optarg, "netgraph") == 0)
404 else if (strcmp(optarg, "link") == 0)
407 errx(1, "%s: unknown address family", optarg);
420 for (cp = interface = optarg; isalpha(*cp); cp++)
441 numeric_addr = numeric_port = 1;
444 if ((tp = name2protox(optarg)) == NULL) {
446 "%s: unknown or uninstrumented protocol",
455 noutputs = atoi(optarg);
479 interval = atoi(optarg);
498 #define BACKWARD_COMPATIBILITY
499 #ifdef BACKWARD_COMPATIBILITY
501 if (isdigit(**argv)) {
502 interval = atoi(*argv);
517 * Discard setgid privileges if not the running kernel so that bad
518 * guys can't print interesting stuff from kernel memory.
520 live = (nlistf == NULL && memf == NULL);
525 errx(1, "-x and -T are incompatible, pick one.");
530 bpf_stats(interface);
535 if (kread(0, NULL, 0) == 0)
536 mbpr(kvmd, nl[N_MBSTAT].n_value);
543 if (kread(0, NULL, 0) == 0)
551 * Keep file descriptors open to avoid overhead
552 * of open/close on each call to get* routines.
558 * This does not make sense any more with DNS being default over
559 * the files. Doing a setXXXXent(1) causes a tcp connection to be
560 * used for the queries, which is slower.
564 if (iflag && !sflag) {
565 intpr(interval, nl[N_IFNET].n_value, NULL);
570 rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
572 routepr(nl[N_RTREE].n_value);
577 if (af == AF_INET || af == AF_UNSPEC)
578 mrt_stats(nl[N_MRTSTAT].n_value);
580 if (af == AF_INET6 || af == AF_UNSPEC)
581 mrt6_stats(nl[N_MRT6STAT].n_value);
584 if (af == AF_INET || af == AF_UNSPEC)
585 mroutepr(nl[N_MFCHASHTBL].n_value,
586 nl[N_MFCTABLESIZE].n_value,
587 nl[N_VIFTABLE].n_value);
589 if (af == AF_INET6 || af == AF_UNSPEC)
590 mroute6pr(nl[N_MF6CTABLE].n_value,
591 nl[N_MIF6TABLE].n_value);
598 printproto(tp, tp->pr_name);
601 if (af == AF_INET || af == AF_UNSPEC)
602 for (tp = protox; tp->pr_name; tp++)
603 printproto(tp, tp->pr_name);
605 if (af == AF_INET6 || af == AF_UNSPEC)
606 for (tp = ip6protox; tp->pr_name; tp++)
607 printproto(tp, tp->pr_name);
610 if (af == PF_KEY || af == AF_UNSPEC)
611 for (tp = pfkeyprotox; tp->pr_name; tp++)
612 printproto(tp, tp->pr_name);
615 if (af == AF_IPX || af == AF_UNSPEC) {
616 for (tp = ipxprotox; tp->pr_name; tp++)
617 printproto(tp, tp->pr_name);
620 if (af == AF_APPLETALK || af == AF_UNSPEC)
621 for (tp = atalkprotox; tp->pr_name; tp++)
622 printproto(tp, tp->pr_name);
624 if (af == AF_NETGRAPH || af == AF_UNSPEC)
625 for (tp = netgraphprotox; tp->pr_name; tp++)
626 printproto(tp, tp->pr_name);
627 #endif /* NETGRAPH */
628 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
629 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
630 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value);
635 * Print out protocol statistics or control blocks (per sflag).
636 * If the interface was not specifically requested, and the symbol
637 * is not in the namelist, ignore this one.
644 void (*pr)(u_long, const char *, int, int);
650 intpr(interval, nl[N_IFNET].n_value,
653 printf("%s: no per-interface stats routine\n",
660 printf("%s: no stats routine\n",
664 if (tp->pr_usesysctl && live)
666 else if (tp->pr_sindex < 0) {
669 "%s: stats routine doesn't work on cores\n",
673 off = nl[tp->pr_sindex].n_value;
679 printf("%s: no PCB routine\n", tp->pr_name);
682 if (tp->pr_usesysctl && live)
684 else if (tp->pr_index < 0) {
687 "%s: PCB routine doesn't work on cores\n",
691 off = nl[tp->pr_index].n_value;
693 if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
695 (*pr)(off, name, af, tp->pr_protocol);
699 * Read kernel memory, return 0 on success.
702 kread(u_long addr, void *buf, size_t size)
704 char errbuf[_POSIX2_LINE_MAX];
707 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
710 if (kvm_nlist(kvmd, nl) < 0) {
712 errx(1, "%s: kvm_nlist: %s", nlistf,
715 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
718 if (nl[0].n_type == 0) {
720 errx(1, "%s: no namelist", nlistf);
722 errx(1, "no namelist");
725 warnx("kvm not available: %s", errbuf);
731 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
732 warnx("%s", kvm_geterr(kvmd));
741 return (n != 1 ? "s" : "");
745 plurales(uintmax_t n)
747 return (n != 1 ? "es" : "");
751 pluralies(uintmax_t n)
753 return (n != 1 ? "ies" : "y");
757 * Find the protox for the given "well-known" name.
759 static struct protox *
760 knownname(const char *name)
762 struct protox **tpp, *tp;
764 for (tpp = protoprotox; *tpp; tpp++)
765 for (tp = *tpp; tp->pr_name; tp++)
766 if (strcmp(tp->pr_name, name) == 0)
772 * Find the protox corresponding to name.
774 static struct protox *
775 name2protox(const char *name)
778 char **alias; /* alias from p->aliases */
782 * Try to find the name in the list of "well-known" names. If that
783 * fails, check if name is an alias for an Internet protocol.
785 if ((tp = knownname(name)) != NULL)
788 setprotoent(1); /* make protocol lookup cheaper */
789 while ((p = getprotoent()) != NULL) {
790 /* assert: name not same as p->name */
791 for (alias = p->p_aliases; *alias; alias++)
792 if (strcmp(name, *alias) == 0) {
794 return (knownname(p->p_name));
804 (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",
805 "usage: netstat [-AaLnSTWx] [-f protocol_family | -p protocol]\n"
806 " [-M core] [-N system]",
807 " netstat -i | -I interface [-abdhntW] [-f address_family]\n"
808 " [-M core] [-N system]",
809 " netstat -w wait [-I interface] [-d] [-M core] [-N system] [-q howmany]",
810 " netstat -s [-s] [-z] [-f protocol_family | -p protocol]\n"
811 " [-M core] [-N system]",
812 " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
813 " [-M core] [-N system]",
814 " netstat -m [-M core] [-N system]",
815 " netstat -B [-I interface]",
816 " netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
817 " netstat -rs [-s] [-M core] [-N system]",
818 " netstat -g [-W] [-f address_family] [-M core] [-N system]",
819 " netstat -gs [-s] [-f address_family] [-M core] [-N system]",