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>
57 #include <netgraph/ng_socket.h>
74 static struct nlist nl[] = {
76 { .n_name = "_ifnet" },
78 { .n_name = "_rtstat" },
80 { .n_name = "_rt_tables"},
82 { .n_name = "_mrtstat" },
84 { .n_name = "_mfctable" },
86 { .n_name = "_viftable" },
88 { .n_name = "_ipxpcb_list"},
90 { .n_name = "_ipxstat"},
92 { .n_name = "_spx_istat"},
94 { .n_name = "_ddpstat"},
96 { .n_name = "_ddpcb"},
98 { .n_name = "_ngsocklist"},
100 { .n_name = "_ip6stat" },
101 #define N_ICMP6STAT 13
102 { .n_name = "_icmp6stat" },
103 #define N_IPSECSTAT 14
104 { .n_name = "_ipsec4stat" },
105 #define N_IPSEC6STAT 15
106 { .n_name = "_ipsec6stat" },
107 #define N_PIM6STAT 16
108 { .n_name = "_pim6stat" },
109 #define N_MRT6STAT 17
110 { .n_name = "_mrt6stat" },
111 #define N_MF6CTABLE 18
112 { .n_name = "_mf6ctable" },
113 #define N_MIF6TABLE 19
114 { .n_name = "_mif6table" },
115 #define N_PFKEYSTAT 20
116 { .n_name = "_pfkeystat" },
118 { .n_name = "_mbstat" },
120 { .n_name = "_mbtypes" },
121 #define N_NMBCLUSTERS 23
122 { .n_name = "_nmbclusters" },
124 { .n_name = "_nmbufs" },
126 { .n_name = "_mbuf_hiwm" },
128 { .n_name = "_clust_hiwm" },
130 { .n_name = "_smp_cpus" },
132 { .n_name = "_pagesize" },
134 { .n_name = "_mb_statpcpu" },
136 { .n_name = "_rttrash" },
138 { .n_name = "_mbuf_lowm" },
140 { .n_name = "_clust_lowm" },
141 #define N_CARPSTAT 33
142 { .n_name = "_carpstats" },
143 #define N_PFSYNCSTAT 34
144 { .n_name = "_pfsyncstats" },
146 { .n_name = "_ahstat" },
148 { .n_name = "_espstat" },
149 #define N_IPCOMPSTAT 37
150 { .n_name = "_ipcompstat" },
152 { .n_name = "_tcpstat" },
154 { .n_name = "_udpstat" },
156 { .n_name = "_ipstat" },
157 #define N_ICMPSTAT 41
158 { .n_name = "_icmpstat" },
159 #define N_IGMPSTAT 42
160 { .n_name = "_igmpstat" },
162 { .n_name = "_pimstat" },
164 { .n_name = "_tcbinfo" },
166 { .n_name = "_udbinfo" },
167 #define N_DIVCBINFO 46
168 { .n_name = "_divcbinfo" },
169 #define N_RIPCBINFO 47
170 { .n_name = "_ripcbinfo" },
171 #define N_UNP_COUNT 48
172 { .n_name = "_unp_count" },
173 #define N_UNP_GENCNT 49
174 { .n_name = "_unp_gencnt" },
175 #define N_UNP_DHEAD 50
176 { .n_name = "_unp_dhead" },
177 #define N_UNP_SHEAD 51
178 { .n_name = "_unp_shead" },
179 #define N_RIP6STAT 52
180 { .n_name = "_rip6stat" },
181 #define N_SCTPSTAT 53
182 { .n_name = "_sctpstat" },
187 int pr_index; /* index into nlist of cb head */
188 int pr_sindex; /* index into nlist of stat block */
189 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
190 void (*pr_cblocks)(u_long, const char *, int, int);
191 /* control blocks printing routine */
192 void (*pr_stats)(u_long, const char *, int, int);
193 /* statistics printing routine */
194 void (*pr_istats)(char *); /* per/if statistics printing routine */
195 const char *pr_name; /* well-known name */
196 int pr_usesysctl; /* non-zero if we use sysctl, not kvm */
199 { N_TCBINFO, N_TCPSTAT, 1, protopr,
200 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
201 { N_UDBINFO, N_UDPSTAT, 1, protopr,
202 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
204 { -1, N_SCTPSTAT, 1, sctp_protopr,
205 sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP },
207 { N_DIVCBINFO, -1, 1, protopr,
208 NULL, NULL, "divert", 1, IPPROTO_DIVERT },
209 { N_RIPCBINFO, N_IPSTAT, 1, protopr,
210 ip_stats, NULL, "ip", 1, IPPROTO_RAW },
211 { N_RIPCBINFO, N_ICMPSTAT, 1, protopr,
212 icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP },
213 { N_RIPCBINFO, N_IGMPSTAT, 1, protopr,
214 igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP },
216 { -1, N_IPSECSTAT, 1, NULL, /* keep as compat */
217 ipsec_stats, NULL, "ipsec", 0, 0},
218 { -1, N_AHSTAT, 1, NULL,
219 ah_stats, NULL, "ah", 0, 0},
220 { -1, N_ESPSTAT, 1, NULL,
221 esp_stats, NULL, "esp", 0, 0},
222 { -1, N_IPCOMPSTAT, 1, NULL,
223 ipcomp_stats, NULL, "ipcomp", 0, 0},
225 { N_RIPCBINFO, N_PIMSTAT, 1, protopr,
226 pim_stats, NULL, "pim", 1, IPPROTO_PIM },
227 { -1, N_CARPSTAT, 1, NULL,
228 carp_stats, NULL, "carp", 1, 0 },
229 { -1, N_PFSYNCSTAT, 1, NULL,
230 pfsync_stats, NULL, "pfsync", 1, 0 },
232 NULL, NULL, NULL, 0, 0 }
236 struct protox ip6protox[] = {
237 { N_TCBINFO, N_TCPSTAT, 1, protopr,
238 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
239 { N_UDBINFO, N_UDPSTAT, 1, protopr,
240 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
241 { N_RIPCBINFO, N_IP6STAT, 1, protopr,
242 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW },
243 { N_RIPCBINFO, N_ICMP6STAT, 1, protopr,
244 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 },
246 { -1, N_IPSEC6STAT, 1, NULL,
247 ipsec_stats, NULL, "ipsec6", 0, 0 },
250 { -1, N_PIM6STAT, 1, NULL,
251 pim6_stats, NULL, "pim6", 1, 0 },
253 { -1, N_RIP6STAT, 1, NULL,
254 rip6_stats, NULL, "rip6", 1, 0 },
256 NULL, NULL, NULL, 0, 0 }
261 struct protox pfkeyprotox[] = {
262 { -1, N_PFKEYSTAT, 1, NULL,
263 pfkey_stats, NULL, "pfkey", 0, 0 },
265 NULL, NULL, NULL, 0, 0 }
269 struct protox atalkprotox[] = {
270 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
271 ddp_stats, NULL, "ddp", 0, 0 },
273 NULL, NULL, NULL, 0, 0 }
276 struct protox netgraphprotox[] = {
277 { N_NGSOCKS, -1, 1, netgraphprotopr,
278 NULL, NULL, "ctrl", 0, 0 },
279 { N_NGSOCKS, -1, 1, netgraphprotopr,
280 NULL, NULL, "data", 0, 0 },
282 NULL, NULL, NULL, 0, 0 }
286 struct protox ipxprotox[] = {
287 { N_IPX, N_IPXSTAT, 1, ipxprotopr,
288 ipx_stats, NULL, "ipx", 0, 0 },
289 { N_IPX, N_SPXSTAT, 1, ipxprotopr,
290 spx_stats, NULL, "spx", 0, 0 },
292 NULL, NULL, 0, 0, 0 }
296 struct protox *protoprotox[] = {
309 static void printproto(struct protox *, const char *);
310 static void usage(void);
311 static struct protox *name2protox(const char *);
312 static struct protox *knownname(const char *);
315 static char *nlistf = NULL, *memf = NULL;
317 int Aflag; /* show addresses of protocol control block */
318 int aflag; /* show all sockets (including servers) */
319 int Bflag; /* show information about bpf consumers */
320 int bflag; /* show i/f total bytes in/out */
321 int dflag; /* show i/f dropped packets */
322 int gflag; /* show group (multicast) routing or stats */
323 int hflag; /* show counters in human readable format */
324 int iflag; /* show interfaces */
325 int Lflag; /* show size of listen queues */
326 int mflag; /* show memory stats */
327 int numeric_addr; /* show addresses numerically */
328 int numeric_port; /* show ports numerically */
329 static int pflag; /* show given protocol */
330 int rflag; /* show routing tables (or routing stats) */
331 int sflag; /* show protocol statistics */
332 int tflag; /* show i/f watchdog timers */
333 int Wflag; /* wide display */
334 int xflag; /* extra information, includes all socket buffer info */
335 int zflag; /* zero stats */
337 int interval; /* repeat interval for i/f stats */
339 char *interface; /* desired i/f for stats, or NULL for all i/fs */
340 int unit; /* unit number for above */
342 int af; /* address family */
343 int live; /* true if we are examining a live system */
346 main(int argc, char *argv[])
348 struct protox *tp = NULL; /* for printing cblocks & stats */
353 while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:rSstuWw:xz")) != -1)
371 if (strcmp(optarg, "ipx") == 0)
373 else if (strcmp(optarg, "inet") == 0)
376 else if (strcmp(optarg, "inet6") == 0)
380 else if (strcmp(optarg, "pfkey") == 0)
383 else if (strcmp(optarg, "unix") == 0)
385 else if (strcmp(optarg, "atalk") == 0)
387 else if (strcmp(optarg, "ng") == 0
388 || strcmp(optarg, "netgraph") == 0)
390 else if (strcmp(optarg, "link") == 0)
393 errx(1, "%s: unknown address family", optarg);
406 for (cp = interface = optarg; isalpha(*cp); cp++)
427 numeric_addr = numeric_port = 1;
430 if ((tp = name2protox(optarg)) == NULL) {
432 "%s: unknown or uninstrumented protocol",
457 interval = atoi(optarg);
473 #define BACKWARD_COMPATIBILITY
474 #ifdef BACKWARD_COMPATIBILITY
476 if (isdigit(**argv)) {
477 interval = atoi(*argv);
492 * Discard setgid privileges if not the running kernel so that bad
493 * guys can't print interesting stuff from kernel memory.
495 live = (nlistf == NULL && memf == NULL);
502 bpf_stats(interface);
507 if (kread(0, NULL, 0) == 0)
508 mbpr(kvmd, nl[N_MBSTAT].n_value);
515 * Keep file descriptors open to avoid overhead
516 * of open/close on each call to get* routines.
522 * This does not make sense any more with DNS being default over
523 * the files. Doing a setXXXXent(1) causes a tcp connection to be
524 * used for the queries, which is slower.
528 if (iflag && !sflag) {
529 intpr(interval, nl[N_IFNET].n_value, NULL);
534 rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
536 routepr(nl[N_RTREE].n_value);
541 if (af == AF_INET || af == AF_UNSPEC)
542 mrt_stats(nl[N_MRTSTAT].n_value);
544 if (af == AF_INET6 || af == AF_UNSPEC)
545 mrt6_stats(nl[N_MRT6STAT].n_value);
548 if (af == AF_INET || af == AF_UNSPEC)
549 mroutepr(nl[N_MFCTABLE].n_value,
550 nl[N_VIFTABLE].n_value);
552 if (af == AF_INET6 || af == AF_UNSPEC)
553 mroute6pr(nl[N_MF6CTABLE].n_value,
554 nl[N_MIF6TABLE].n_value);
562 printproto(tp, tp->pr_name);
565 if (af == AF_INET || af == AF_UNSPEC)
566 for (tp = protox; tp->pr_name; tp++)
567 printproto(tp, tp->pr_name);
569 if (af == AF_INET6 || af == AF_UNSPEC)
570 for (tp = ip6protox; tp->pr_name; tp++)
571 printproto(tp, tp->pr_name);
574 if (af == PF_KEY || af == AF_UNSPEC)
575 for (tp = pfkeyprotox; tp->pr_name; tp++)
576 printproto(tp, tp->pr_name);
579 if (af == AF_IPX || af == AF_UNSPEC) {
580 for (tp = ipxprotox; tp->pr_name; tp++)
581 printproto(tp, tp->pr_name);
584 if (af == AF_APPLETALK || af == AF_UNSPEC)
585 for (tp = atalkprotox; tp->pr_name; tp++)
586 printproto(tp, tp->pr_name);
587 if (af == AF_NETGRAPH || af == AF_UNSPEC)
588 for (tp = netgraphprotox; tp->pr_name; tp++)
589 printproto(tp, tp->pr_name);
590 if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
591 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
592 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value);
597 * Print out protocol statistics or control blocks (per sflag).
598 * If the interface was not specifically requested, and the symbol
599 * is not in the namelist, ignore this one.
606 void (*pr)(u_long, const char *, int, int);
612 intpr(interval, nl[N_IFNET].n_value,
615 printf("%s: no per-interface stats routine\n",
622 printf("%s: no stats routine\n",
626 if (tp->pr_usesysctl && live)
628 else if (tp->pr_sindex < 0) {
631 "%s: stats routine doesn't work on cores\n",
635 off = nl[tp->pr_sindex].n_value;
641 printf("%s: no PCB routine\n", tp->pr_name);
644 if (tp->pr_usesysctl && live)
646 else if (tp->pr_index < 0) {
649 "%s: PCB routine doesn't work on cores\n",
653 off = nl[tp->pr_index].n_value;
655 if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
657 (*pr)(off, name, af, tp->pr_protocol);
661 * Read kernel memory, return 0 on success.
664 kread(u_long addr, void *buf, size_t size)
666 char errbuf[_POSIX2_LINE_MAX];
669 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
672 if (kvm_nlist(kvmd, nl) < 0) {
674 errx(1, "%s: kvm_nlist: %s", nlistf,
677 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
680 if (nl[0].n_type == 0) {
682 errx(1, "%s: no namelist", nlistf);
684 errx(1, "no namelist");
687 warnx("kvm not available: %s", errbuf);
693 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
694 warnx("%s", kvm_geterr(kvmd));
703 return (n != 1 ? "s" : "");
707 plurales(uintmax_t n)
709 return (n != 1 ? "es" : "");
713 pluralies(uintmax_t n)
715 return (n != 1 ? "ies" : "y");
719 * Find the protox for the given "well-known" name.
721 static struct protox *
722 knownname(const char *name)
724 struct protox **tpp, *tp;
726 for (tpp = protoprotox; *tpp; tpp++)
727 for (tp = *tpp; tp->pr_name; tp++)
728 if (strcmp(tp->pr_name, name) == 0)
734 * Find the protox corresponding to name.
736 static struct protox *
737 name2protox(const char *name)
740 char **alias; /* alias from p->aliases */
744 * Try to find the name in the list of "well-known" names. If that
745 * fails, check if name is an alias for an Internet protocol.
747 if ((tp = knownname(name)) != NULL)
750 setprotoent(1); /* make protocol lookup cheaper */
751 while ((p = getprotoent()) != NULL) {
752 /* assert: name not same as p->name */
753 for (alias = p->p_aliases; *alias; alias++)
754 if (strcmp(name, *alias) == 0) {
756 return (knownname(p->p_name));
766 (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",
767 "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
768 " [-M core] [-N system]",
769 " netstat -i | -I interface [-abdhnt] [-f address_family]\n"
770 " [-M core] [-N system]",
771 " netstat -w wait [-I interface] [-d] [-M core] [-N system]",
772 " netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
773 " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
774 " [-M core] [-N system]",
775 " netstat -m [-M core] [-N system]",
776 " netstat -B [ -I interface]",
777 " netstat -r [-AenW] [-f address_family] [-M core] [-N system]",
778 " netstat -rs [-s] [-M core] [-N system]",
779 " netstat -g [-W] [-f address_family] [-M core] [-N system]",
780 " netstat -gs [-s] [-f address_family] [-M core] [-N system]");