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 = "_rtstat" },
76 { .n_name = "_rt_tables"},
78 { .n_name = "_mrtstat" },
79 #define N_MFCHASHTBL 3
80 { .n_name = "_mfchashtbl" },
82 { .n_name = "_viftable" },
84 { .n_name = "_ngsocklist"},
86 { .n_name = "_ip6stat" },
88 { .n_name = "_icmp6stat" },
90 { .n_name = "_ipsec4stat" },
91 #define N_IPSEC6STAT 9
92 { .n_name = "_ipsec6stat" },
94 { .n_name = "_pim6stat" },
96 { .n_name = "_mrt6stat" },
97 #define N_MF6CTABLE 12
98 { .n_name = "_mf6ctable" },
99 #define N_MIF6TABLE 13
100 { .n_name = "_mif6table" },
101 #define N_PFKEYSTAT 14
102 { .n_name = "_pfkeystat" },
104 { .n_name = "_rttrash" },
105 #define N_CARPSTAT 16
106 { .n_name = "_carpstats" },
107 #define N_PFSYNCSTAT 17
108 { .n_name = "_pfsyncstats" },
110 { .n_name = "_ahstat" },
112 { .n_name = "_espstat" },
113 #define N_IPCOMPSTAT 20
114 { .n_name = "_ipcompstat" },
116 { .n_name = "_tcpstat" },
118 { .n_name = "_udpstat" },
120 { .n_name = "_ipstat" },
121 #define N_ICMPSTAT 24
122 { .n_name = "_icmpstat" },
123 #define N_IGMPSTAT 25
124 { .n_name = "_igmpstat" },
126 { .n_name = "_pimstat" },
128 { .n_name = "_tcbinfo" },
130 { .n_name = "_udbinfo" },
131 #define N_DIVCBINFO 29
132 { .n_name = "_divcbinfo" },
133 #define N_RIPCBINFO 30
134 { .n_name = "_ripcbinfo" },
135 #define N_UNP_COUNT 31
136 { .n_name = "_unp_count" },
137 #define N_UNP_GENCNT 32
138 { .n_name = "_unp_gencnt" },
139 #define N_UNP_DHEAD 33
140 { .n_name = "_unp_dhead" },
141 #define N_UNP_SHEAD 34
142 { .n_name = "_unp_shead" },
143 #define N_RIP6STAT 36
144 { .n_name = "_rip6stat" },
145 #define N_SCTPSTAT 36
146 { .n_name = "_sctpstat" },
147 #define N_MFCTABLESIZE 37
148 { .n_name = "_mfctablesize" },
150 { .n_name = "_arpstat" },
151 #define N_UNP_SPHEAD 39
152 { .n_name = "unp_sphead" },
154 { .n_name = "_sfstat"},
159 int pr_index; /* index into nlist of cb head */
160 int pr_sindex; /* index into nlist of stat block */
161 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
162 void (*pr_cblocks)(u_long, const char *, int, int);
163 /* control blocks printing routine */
164 void (*pr_stats)(u_long, const char *, int, int);
165 /* statistics printing routine */
166 void (*pr_istats)(char *); /* per/if statistics printing routine */
167 const char *pr_name; /* well-known name */
168 int pr_usesysctl; /* non-zero if we use sysctl, not kvm */
171 { N_TCBINFO, N_TCPSTAT, 1, protopr,
172 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
173 { N_UDBINFO, N_UDPSTAT, 1, protopr,
174 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
176 { -1, N_SCTPSTAT, 1, sctp_protopr,
177 sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP },
180 { -1, -1, 1, protopr,
181 NULL, NULL, "sdp", 1, IPPROTO_TCP },
183 { N_DIVCBINFO, -1, 1, protopr,
184 NULL, NULL, "divert", 1, IPPROTO_DIVERT },
185 { N_RIPCBINFO, N_IPSTAT, 1, protopr,
186 ip_stats, NULL, "ip", 1, IPPROTO_RAW },
187 { N_RIPCBINFO, N_ICMPSTAT, 1, protopr,
188 icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP },
189 { N_RIPCBINFO, N_IGMPSTAT, 1, protopr,
190 igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP },
192 { -1, N_IPSECSTAT, 1, NULL, /* keep as compat */
193 ipsec_stats, NULL, "ipsec", 0, 0},
194 { -1, N_AHSTAT, 1, NULL,
195 ah_stats, NULL, "ah", 0, 0},
196 { -1, N_ESPSTAT, 1, NULL,
197 esp_stats, NULL, "esp", 0, 0},
198 { -1, N_IPCOMPSTAT, 1, NULL,
199 ipcomp_stats, NULL, "ipcomp", 0, 0},
201 { N_RIPCBINFO, N_PIMSTAT, 1, protopr,
202 pim_stats, NULL, "pim", 1, IPPROTO_PIM },
203 { -1, N_CARPSTAT, 1, NULL,
204 carp_stats, NULL, "carp", 1, 0 },
206 { -1, N_PFSYNCSTAT, 1, NULL,
207 pfsync_stats, NULL, "pfsync", 1, 0 },
209 { -1, N_ARPSTAT, 1, NULL,
210 arp_stats, NULL, "arp", 1, 0 },
212 NULL, NULL, NULL, 0, 0 }
216 struct protox ip6protox[] = {
217 { N_TCBINFO, N_TCPSTAT, 1, protopr,
218 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
219 { N_UDBINFO, N_UDPSTAT, 1, protopr,
220 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
221 { N_RIPCBINFO, N_IP6STAT, 1, protopr,
222 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW },
223 { N_RIPCBINFO, N_ICMP6STAT, 1, protopr,
224 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 },
226 { -1, -1, 1, protopr,
227 NULL, NULL, "sdp", 1, IPPROTO_TCP },
230 { -1, N_IPSEC6STAT, 1, NULL,
231 ipsec_stats, NULL, "ipsec6", 0, 0 },
234 { -1, N_PIM6STAT, 1, NULL,
235 pim6_stats, NULL, "pim6", 1, 0 },
237 { -1, N_RIP6STAT, 1, NULL,
238 rip6_stats, NULL, "rip6", 1, 0 },
240 NULL, NULL, NULL, 0, 0 }
245 struct protox pfkeyprotox[] = {
246 { -1, N_PFKEYSTAT, 1, NULL,
247 pfkey_stats, NULL, "pfkey", 0, 0 },
249 NULL, NULL, NULL, 0, 0 }
254 struct protox netgraphprotox[] = {
255 { N_NGSOCKS, -1, 1, netgraphprotopr,
256 NULL, NULL, "ctrl", 0, 0 },
257 { N_NGSOCKS, -1, 1, netgraphprotopr,
258 NULL, NULL, "data", 0, 0 },
260 NULL, NULL, NULL, 0, 0 }
264 struct protox *protoprotox[] = {
274 static void printproto(struct protox *, const char *);
275 static void usage(void);
276 static struct protox *name2protox(const char *);
277 static struct protox *knownname(const char *);
280 static char *nlistf = NULL, *memf = NULL;
282 int Aflag; /* show addresses of protocol control block */
283 int aflag; /* show all sockets (including servers) */
284 int Bflag; /* show information about bpf consumers */
285 int bflag; /* show i/f total bytes in/out */
286 int dflag; /* show i/f dropped packets */
287 int gflag; /* show group (multicast) routing or stats */
288 int hflag; /* show counters in human readable format */
289 int iflag; /* show interfaces */
290 int Lflag; /* show size of listen queues */
291 int mflag; /* show memory stats */
292 int noutputs = 0; /* how much outputs before we exit */
293 int numeric_addr; /* show addresses numerically */
294 int numeric_port; /* show ports numerically */
295 static int pflag; /* show given protocol */
296 int Qflag; /* show netisr information */
297 int rflag; /* show routing tables (or routing stats) */
298 int Rflag; /* show flow / RSS statistics */
299 int sflag; /* show protocol statistics */
300 int Wflag; /* wide display */
301 int Tflag; /* TCP Information */
302 int xflag; /* extra information, includes all socket buffer info */
303 int zflag; /* zero stats */
305 int interval; /* repeat interval for i/f stats */
307 char *interface; /* desired i/f for stats, or NULL for all i/fs */
308 int unit; /* unit number for above */
310 int af; /* address family */
311 int live; /* true if we are examining a live system */
314 main(int argc, char *argv[])
316 struct protox *tp = NULL; /* for printing cblocks & stats */
323 while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:RrSTsuWw:xz"))
330 errx(1, "IPv4 support is not compiled in");
337 errx(1, "IPv6 support is not compiled in");
356 fib = strtol(optarg, &endptr, 0);
357 if (*endptr != '\0' ||
358 (fib == 0 && (errno == EINVAL || errno == ERANGE)))
359 errx(1, "%s: invalid fib", optarg);
362 if (strcmp(optarg, "inet") == 0)
365 else if (strcmp(optarg, "inet6") == 0)
369 else if (strcmp(optarg, "pfkey") == 0)
372 else if (strcmp(optarg, "unix") == 0)
375 else if (strcmp(optarg, "ng") == 0
376 || strcmp(optarg, "netgraph") == 0)
379 else if (strcmp(optarg, "link") == 0)
382 errx(1, "%s: unknown address family", optarg);
395 for (cp = interface = optarg; isalpha(*cp); cp++)
416 numeric_addr = numeric_port = 1;
419 if ((tp = name2protox(optarg)) == NULL) {
421 "%s: unknown or uninstrumented protocol",
430 noutputs = atoi(optarg);
454 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);
500 errx(1, "-x and -T are incompatible, pick one.");
505 bpf_stats(interface);
510 if (kread(0, NULL, 0) == 0)
511 mbpr(kvmd, nl[N_SFSTAT].n_value);
518 if (kread(0, NULL, 0) == 0)
526 * Keep file descriptors open to avoid overhead
527 * of open/close on each call to get* routines.
533 * This does not make sense any more with DNS being default over
534 * the files. Doing a setXXXXent(1) causes a tcp connection to be
535 * used for the queries, which is slower.
538 if (iflag && !sflag) {
539 intpr(interval, NULL, af);
553 if (af == AF_INET || af == AF_UNSPEC)
556 if (af == AF_INET6 || af == AF_UNSPEC)
560 if (af == AF_INET || af == AF_UNSPEC)
563 if (af == AF_INET6 || af == AF_UNSPEC)
570 /* Load all necessary kvm symbols */
574 printproto(tp, tp->pr_name);
577 if (af == AF_INET || af == AF_UNSPEC)
578 for (tp = protox; tp->pr_name; tp++)
579 printproto(tp, tp->pr_name);
581 if (af == AF_INET6 || af == AF_UNSPEC)
582 for (tp = ip6protox; tp->pr_name; tp++)
583 printproto(tp, tp->pr_name);
586 if (af == PF_KEY || af == AF_UNSPEC)
587 for (tp = pfkeyprotox; tp->pr_name; tp++)
588 printproto(tp, tp->pr_name);
591 if (af == AF_NETGRAPH || af == AF_UNSPEC)
592 for (tp = netgraphprotox; tp->pr_name; tp++)
593 printproto(tp, tp->pr_name);
594 #endif /* NETGRAPH */
595 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
596 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
597 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
598 nl[N_UNP_SPHEAD].n_value);
603 * Print out protocol statistics or control blocks (per sflag).
604 * If the interface was not specifically requested, and the symbol
605 * is not in the namelist, ignore this one.
608 printproto(struct protox *tp, const char *name)
610 void (*pr)(u_long, const char *, int, int);
616 intpr(interval, tp->pr_istats, af);
618 printf("%s: no per-interface stats routine\n",
625 printf("%s: no stats routine\n",
629 if (tp->pr_usesysctl && live)
631 else if (tp->pr_sindex < 0) {
634 "%s: stats routine doesn't work on cores\n",
638 off = nl[tp->pr_sindex].n_value;
644 printf("%s: no PCB routine\n", tp->pr_name);
647 if (tp->pr_usesysctl && live)
649 else if (tp->pr_index < 0) {
652 "%s: PCB routine doesn't work on cores\n",
656 off = nl[tp->pr_index].n_value;
658 if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
660 (*pr)(off, name, af, tp->pr_protocol);
666 char errbuf[_POSIX2_LINE_MAX];
671 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
675 warnx("kvm not available: %s", errbuf);
683 * Resolve symbol list, return 0 on success.
686 kresolve_list(struct nlist *_nl)
689 if ((kvmd == NULL) && (kvmd_init() != 0))
692 if (_nl[0].n_type != 0)
695 if (kvm_nlist(kvmd, _nl) < 0) {
697 errx(1, "%s: kvm_nlist: %s", nlistf,
700 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
707 * Read kernel memory, return 0 on success.
710 kread(u_long addr, void *buf, size_t size)
718 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
719 warnx("%s", kvm_geterr(kvmd));
726 * Read single counter(9).
729 kread_counter(u_long addr)
735 return (kvm_counter_u64_fetch(kvmd, addr));
739 * Read an array of N counters in kernel memory into array of N uint64_t's.
742 kread_counters(u_long addr, void *buf, size_t size)
749 if (kread(addr, buf, size) < 0)
753 *c = kvm_counter_u64_fetch(kvmd, *c);
763 return (n != 1 ? "s" : "");
767 plurales(uintmax_t n)
769 return (n != 1 ? "es" : "");
773 pluralies(uintmax_t n)
775 return (n != 1 ? "ies" : "y");
779 * Find the protox for the given "well-known" name.
781 static struct protox *
782 knownname(const char *name)
784 struct protox **tpp, *tp;
786 for (tpp = protoprotox; *tpp; tpp++)
787 for (tp = *tpp; tp->pr_name; tp++)
788 if (strcmp(tp->pr_name, name) == 0)
794 * Find the protox corresponding to name.
796 static struct protox *
797 name2protox(const char *name)
800 char **alias; /* alias from p->aliases */
804 * Try to find the name in the list of "well-known" names. If that
805 * fails, check if name is an alias for an Internet protocol.
807 if ((tp = knownname(name)) != NULL)
810 setprotoent(1); /* make protocol lookup cheaper */
811 while ((p = getprotoent()) != NULL) {
812 /* assert: name not same as p->name */
813 for (alias = p->p_aliases; *alias; alias++)
814 if (strcmp(name, *alias) == 0) {
816 return (knownname(p->p_name));
826 (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",
827 "usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n"
828 " [-M core] [-N system]",
829 " netstat -i | -I interface [-46abdhnW] [-f address_family]\n"
830 " [-M core] [-N system]",
831 " netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n"
833 " netstat -s [-46sz] [-f protocol_family | -p protocol]\n"
834 " [-M core] [-N system]",
835 " netstat -i | -I interface -s [-46s]\n"
836 " [-f protocol_family | -p protocol] [-M core] [-N system]",
837 " netstat -m [-M core] [-N system]",
838 " netstat -B [-z] [-I interface]",
839 " netstat -r [-46AnW] [-F fibnum] [-f address_family]\n"
840 " [-M core] [-N system]",
841 " netstat -rs [-s] [-M core] [-N system]",
842 " netstat -g [-46W] [-f address_family] [-M core] [-N system]",
843 " netstat -gs [-46s] [-f address_family] [-M core] [-N system]",