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 static 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>
50 #include <sys/sysctl.h>
52 #include <netinet/in.h>
55 #include <netgraph/ng_socket.h>
76 static struct protox {
77 int pr_index; /* index into nlist of cb head */
78 int pr_sindex; /* index into nlist of stat block */
79 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
80 void (*pr_cblocks)(u_long, const char *, int, int);
81 /* control blocks printing routine */
82 void (*pr_stats)(u_long, const char *, int, int);
83 /* statistics printing routine */
84 void (*pr_istats)(char *); /* per/if statistics printing routine */
85 const char *pr_name; /* well-known name */
86 int pr_usesysctl; /* non-zero if we use sysctl, not kvm */
89 { N_TCBINFO, N_TCPSTAT, 1, protopr,
90 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
91 { N_UDBINFO, N_UDPSTAT, 1, protopr,
92 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
94 { -1, N_SCTPSTAT, 1, sctp_protopr,
95 sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP },
99 NULL, NULL, "sdp", 1, IPPROTO_TCP },
101 { N_DIVCBINFO, -1, 1, protopr,
102 NULL, NULL, "divert", 1, IPPROTO_DIVERT },
103 { N_RIPCBINFO, N_IPSTAT, 1, protopr,
104 ip_stats, NULL, "ip", 1, IPPROTO_RAW },
105 { N_RIPCBINFO, N_ICMPSTAT, 1, protopr,
106 icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP },
107 { N_RIPCBINFO, N_IGMPSTAT, 1, protopr,
108 igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP },
110 { -1, N_IPSEC4STAT, 1, NULL, /* keep as compat */
111 ipsec_stats, NULL, "ipsec", 0, 0},
112 { -1, N_AHSTAT, 1, NULL,
113 ah_stats, NULL, "ah", 0, 0},
114 { -1, N_ESPSTAT, 1, NULL,
115 esp_stats, NULL, "esp", 0, 0},
116 { -1, N_IPCOMPSTAT, 1, NULL,
117 ipcomp_stats, NULL, "ipcomp", 0, 0},
119 { N_RIPCBINFO, N_PIMSTAT, 1, protopr,
120 pim_stats, NULL, "pim", 1, IPPROTO_PIM },
121 { -1, N_CARPSTATS, 1, NULL,
122 carp_stats, NULL, "carp", 1, 0 },
124 { -1, N_PFSYNCSTATS, 1, NULL,
125 pfsync_stats, NULL, "pfsync", 1, 0 },
127 { -1, N_ARPSTAT, 1, NULL,
128 arp_stats, NULL, "arp", 1, 0 },
130 NULL, NULL, NULL, 0, 0 }
134 static struct protox ip6protox[] = {
135 { N_TCBINFO, N_TCPSTAT, 1, protopr,
136 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
137 { N_UDBINFO, N_UDPSTAT, 1, protopr,
138 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
139 { N_RIPCBINFO, N_IP6STAT, 1, protopr,
140 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW },
141 { N_RIPCBINFO, N_ICMP6STAT, 1, protopr,
142 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 },
144 { -1, -1, 1, protopr,
145 NULL, NULL, "sdp", 1, IPPROTO_TCP },
148 { -1, N_IPSEC6STAT, 1, NULL,
149 ipsec_stats, NULL, "ipsec6", 0, 0 },
152 { -1, N_PIM6STAT, 1, NULL,
153 pim6_stats, NULL, "pim6", 1, 0 },
155 { -1, N_RIP6STAT, 1, NULL,
156 rip6_stats, NULL, "rip6", 1, 0 },
158 NULL, NULL, NULL, 0, 0 }
163 static struct protox pfkeyprotox[] = {
164 { -1, N_PFKEYSTAT, 1, NULL,
165 pfkey_stats, NULL, "pfkey", 0, 0 },
167 NULL, NULL, NULL, 0, 0 }
172 static struct protox netgraphprotox[] = {
173 { N_NGSOCKLIST, -1, 1, netgraphprotopr,
174 NULL, NULL, "ctrl", 0, 0 },
175 { N_NGSOCKLIST, -1, 1, netgraphprotopr,
176 NULL, NULL, "data", 0, 0 },
178 NULL, NULL, NULL, 0, 0 }
182 static struct protox *protoprotox[] = {
192 static void printproto(struct protox *, const char *, bool *);
193 static void usage(void);
194 static struct protox *name2protox(const char *);
195 static struct protox *knownname(const char *);
197 static int kresolve_list(struct nlist *_nl);
200 static char *nlistf = NULL, *memf = NULL;
202 int Aflag; /* show addresses of protocol control block */
203 int aflag; /* show all sockets (including servers) */
204 static int Bflag; /* show information about bpf consumers */
205 int bflag; /* show i/f total bytes in/out */
206 int dflag; /* show i/f dropped packets */
207 int gflag; /* show group (multicast) routing or stats */
208 int hflag; /* show counters in human readable format */
209 int iflag; /* show interfaces */
210 int Lflag; /* show size of listen queues */
211 int mflag; /* show memory stats */
212 int noutputs = 0; /* how much outputs before we exit */
213 int numeric_addr; /* show addresses numerically */
214 int numeric_port; /* show ports numerically */
215 static int pflag; /* show given protocol */
216 static int Qflag; /* show netisr information */
217 int rflag; /* show routing tables (or routing stats) */
218 int Rflag; /* show flow / RSS statistics */
219 int sflag; /* show protocol statistics */
220 int Wflag; /* wide display */
221 int Tflag; /* TCP Information */
222 int xflag; /* extra information, includes all socket buffer info */
223 int zflag; /* zero stats */
225 int interval; /* repeat interval for i/f stats */
227 char *interface; /* desired i/f for stats, or NULL for all i/fs */
228 int unit; /* unit number for above */
230 static int af; /* address family */
231 int live; /* true if we are examining a live system */
234 main(int argc, char *argv[])
236 struct protox *tp = NULL; /* for printing cblocks & stats */
244 argc = xo_parse_args(argc, argv);
246 while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:RrSTsuWw:xz"))
253 errx(1, "IPv4 support is not compiled in");
260 errx(1, "IPv6 support is not compiled in");
279 fib = strtol(optarg, &endptr, 0);
280 if (*endptr != '\0' ||
281 (fib == 0 && (errno == EINVAL || errno == ERANGE)))
282 xo_errx(1, "%s: invalid fib", optarg);
285 if (strcmp(optarg, "inet") == 0)
288 else if (strcmp(optarg, "inet6") == 0)
292 else if (strcmp(optarg, "pfkey") == 0)
295 else if (strcmp(optarg, "unix") == 0 ||
296 strcmp(optarg, "local") == 0)
299 else if (strcmp(optarg, "ng") == 0
300 || strcmp(optarg, "netgraph") == 0)
303 else if (strcmp(optarg, "link") == 0)
306 xo_errx(1, "%s: unknown address family",
320 for (cp = interface = optarg; isalpha(*cp); cp++)
341 numeric_addr = numeric_port = 1;
344 if ((tp = name2protox(optarg)) == NULL) {
345 xo_errx(1, "%s: unknown or uninstrumented "
354 noutputs = atoi(optarg);
378 interval = atoi(optarg);
397 #define BACKWARD_COMPATIBILITY
398 #ifdef BACKWARD_COMPATIBILITY
400 if (isdigit(**argv)) {
401 interval = atoi(*argv);
416 * Discard setgid privileges if not the running kernel so that bad
417 * guys can't print interesting stuff from kernel memory.
419 live = (nlistf == NULL && memf == NULL);
421 if (setgid(getgid()) != 0)
422 xo_err(-1, "setgid");
426 xo_errx(1, "-x and -T are incompatible, pick one.");
431 bpf_stats(interface);
437 if (kread(0, NULL, 0) == 0)
438 mbpr(kvmd, nl[N_SFSTAT].n_value);
446 if (kread(0, NULL, 0) == 0)
455 * Keep file descriptors open to avoid overhead
456 * of open/close on each call to get* routines.
462 * This does not make sense any more with DNS being default over
463 * the files. Doing a setXXXXent(1) causes a tcp connection to be
464 * used for the queries, which is slower.
467 if (iflag && !sflag) {
468 xo_open_container("statistics");
470 xo_close_container("statistics");
475 xo_open_container("statistics");
481 xo_close_container("statistics");
487 xo_open_container("statistics");
489 if (af == AF_INET || af == AF_UNSPEC)
492 if (af == AF_INET6 || af == AF_UNSPEC)
496 if (af == AF_INET || af == AF_UNSPEC)
499 if (af == AF_INET6 || af == AF_UNSPEC)
503 xo_close_container("statistics");
508 /* Load all necessary kvm symbols */
512 xo_open_container("statistics");
513 printproto(tp, tp->pr_name, &first);
515 xo_close_list("socket");
516 xo_close_container("statistics");
521 xo_open_container("statistics");
522 if (af == AF_INET || af == AF_UNSPEC)
523 for (tp = protox; tp->pr_name; tp++)
524 printproto(tp, tp->pr_name, &first);
526 if (af == AF_INET6 || af == AF_UNSPEC)
527 for (tp = ip6protox; tp->pr_name; tp++)
528 printproto(tp, tp->pr_name, &first);
531 if (af == PF_KEY || af == AF_UNSPEC)
532 for (tp = pfkeyprotox; tp->pr_name; tp++)
533 printproto(tp, tp->pr_name, &first);
536 if (af == AF_NETGRAPH || af == AF_UNSPEC)
537 for (tp = netgraphprotox; tp->pr_name; tp++)
538 printproto(tp, tp->pr_name, &first);
539 #endif /* NETGRAPH */
540 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
541 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
542 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
543 nl[N_UNP_SPHEAD].n_value, &first);
546 xo_close_list("socket");
547 xo_close_container("statistics");
553 fetch_stats(const char *sysctlname, u_long off, void *stats, size_t len,
554 int (*kreadfn)(u_long, void *, size_t))
559 memset(stats, 0, len);
561 error = sysctlbyname(sysctlname, NULL, NULL, stats,
564 error = sysctlbyname(sysctlname, stats, &len, NULL, 0);
565 if (error == -1 && errno != ENOENT)
566 xo_warn("sysctl %s", sysctlname);
570 error = kreadfn(off, stats, len);
576 * Print out protocol statistics or control blocks (per sflag).
577 * If the interface was not specifically requested, and the symbol
578 * is not in the namelist, ignore this one.
581 printproto(struct protox *tp, const char *name, bool *first)
583 void (*pr)(u_long, const char *, int, int);
585 bool doingdblocks = false;
590 intpr(tp->pr_istats, af);
592 xo_message("%s: no per-interface stats routine",
599 xo_message("%s: no stats routine",
603 if (tp->pr_usesysctl && live)
605 else if (tp->pr_sindex < 0) {
607 xo_message("%s: stats routine doesn't "
608 "work on cores", tp->pr_name);
611 off = nl[tp->pr_sindex].n_value;
618 xo_message("%s: no PCB routine", tp->pr_name);
621 if (tp->pr_usesysctl && live)
623 else if (tp->pr_index < 0) {
625 xo_message("%s: PCB routine doesn't work on "
626 "cores", tp->pr_name);
629 off = nl[tp->pr_index].n_value;
631 if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
633 if (doingdblocks && *first) {
634 xo_open_list("socket");
638 (*pr)(off, name, af, tp->pr_protocol);
645 char errbuf[_POSIX2_LINE_MAX];
650 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
651 if (setgid(getgid()) != 0)
652 xo_err(-1, "setgid");
655 xo_warnx("kvm not available: %s", errbuf);
663 * Resolve symbol list, return 0 on success.
666 kresolve_list(struct nlist *_nl)
669 if ((kvmd == NULL) && (kvmd_init() != 0))
672 if (_nl[0].n_type != 0)
675 if (kvm_nlist(kvmd, _nl) < 0) {
677 xo_errx(1, "%s: kvm_nlist: %s", nlistf,
680 xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
687 * Wrapper of kvm_dpcpu_setcpu().
690 kset_dpcpu(u_int cpuid)
693 if ((kvmd == NULL) && (kvmd_init() != 0))
694 xo_errx(-1, "%s: kvm is not available", __func__);
696 if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0)
697 xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
698 cpuid, kvm_geterr(kvmd));
703 * Read kernel memory, return 0 on success.
706 kread(u_long addr, void *buf, size_t size)
714 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
715 xo_warnx("%s", kvm_geterr(kvmd));
722 * Read single counter(9).
725 kread_counter(u_long addr)
731 return (kvm_counter_u64_fetch(kvmd, addr));
735 * Read an array of N counters in kernel memory into array of N uint64_t's.
738 kread_counters(u_long addr, void *buf, size_t size)
747 if (size % sizeof(uint64_t) != 0) {
748 xo_warnx("kread_counters: invalid counter set size");
752 n = size / sizeof(uint64_t);
753 if ((counters = malloc(n * sizeof(u_long))) == NULL)
754 xo_err(-1, "malloc");
755 if (kread(addr, counters, n * sizeof(u_long)) < 0) {
761 for (i = 0; i < n; i++)
762 c[i] = kvm_counter_u64_fetch(kvmd, counters[i]);
771 return (n != 1 ? "s" : "");
775 plurales(uintmax_t n)
777 return (n != 1 ? "es" : "");
781 pluralies(uintmax_t n)
783 return (n != 1 ? "ies" : "y");
787 * Find the protox for the given "well-known" name.
789 static struct protox *
790 knownname(const char *name)
792 struct protox **tpp, *tp;
794 for (tpp = protoprotox; *tpp; tpp++)
795 for (tp = *tpp; tp->pr_name; tp++)
796 if (strcmp(tp->pr_name, name) == 0)
802 * Find the protox corresponding to name.
804 static struct protox *
805 name2protox(const char *name)
808 char **alias; /* alias from p->aliases */
812 * Try to find the name in the list of "well-known" names. If that
813 * fails, check if name is an alias for an Internet protocol.
815 if ((tp = knownname(name)) != NULL)
818 setprotoent(1); /* make protocol lookup cheaper */
819 while ((p = getprotoent()) != NULL) {
820 /* assert: name not same as p->name */
821 for (alias = p->p_aliases; *alias; alias++)
822 if (strcmp(name, *alias) == 0) {
824 return (knownname(p->p_name));
834 (void)xo_error("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
835 "usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n"
836 " [-M core] [-N system]",
837 " netstat -i | -I interface [-46abdhnW] [-f address_family]\n"
838 " [-M core] [-N system]",
839 " netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n"
841 " netstat -s [-46sz] [-f protocol_family | -p protocol]\n"
842 " [-M core] [-N system]",
843 " netstat -i | -I interface -s [-46s]\n"
844 " [-f protocol_family | -p protocol] [-M core] [-N system]",
845 " netstat -m [-M core] [-N system]",
846 " netstat -B [-z] [-I interface]",
847 " netstat -r [-46AnW] [-F fibnum] [-f address_family]\n"
848 " [-M core] [-N system]",
849 " netstat -rs [-s] [-M core] [-N system]",
850 " netstat -g [-46W] [-f address_family] [-M core] [-N system]",
851 " netstat -gs [-46s] [-f address_family] [-M core] [-N system]",