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", 1, 0},
112 { -1, N_AHSTAT, 1, NULL,
113 ah_stats, NULL, "ah", 1, 0},
114 { -1, N_ESPSTAT, 1, NULL,
115 esp_stats, NULL, "esp", 1, 0},
116 { -1, N_IPCOMPSTAT, 1, NULL,
117 ipcomp_stats, NULL, "ipcomp", 1, 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", 1, 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);
248 while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:RrSTsuWw:xz"))
255 errx(1, "IPv4 support is not compiled in");
262 errx(1, "IPv6 support is not compiled in");
281 fib = strtol(optarg, &endptr, 0);
282 if (*endptr != '\0' ||
283 (fib == 0 && (errno == EINVAL || errno == ERANGE)))
284 xo_errx(1, "%s: invalid fib", optarg);
287 if (strcmp(optarg, "inet") == 0)
290 else if (strcmp(optarg, "inet6") == 0)
294 else if (strcmp(optarg, "pfkey") == 0)
297 else if (strcmp(optarg, "unix") == 0 ||
298 strcmp(optarg, "local") == 0)
301 else if (strcmp(optarg, "ng") == 0
302 || strcmp(optarg, "netgraph") == 0)
305 else if (strcmp(optarg, "link") == 0)
308 xo_errx(1, "%s: unknown address family",
322 for (cp = interface = optarg; isalpha(*cp); cp++)
343 numeric_addr = numeric_port = 1;
346 if ((tp = name2protox(optarg)) == NULL) {
347 xo_errx(1, "%s: unknown or uninstrumented "
356 noutputs = atoi(optarg);
380 interval = atoi(optarg);
399 #define BACKWARD_COMPATIBILITY
400 #ifdef BACKWARD_COMPATIBILITY
402 if (isdigit(**argv)) {
403 interval = atoi(*argv);
418 * Discard setgid privileges if not the running kernel so that bad
419 * guys can't print interesting stuff from kernel memory.
421 live = (nlistf == NULL && memf == NULL);
423 if (setgid(getgid()) != 0)
424 xo_err(-1, "setgid");
425 /* Load all necessary kvm symbols */
430 xo_errx(1, "-x and -T are incompatible, pick one.");
435 bpf_stats(interface);
441 if (kread(0, NULL, 0) == 0)
442 mbpr(kvmd, nl[N_SFSTAT].n_value);
450 if (kread(0, NULL, 0) == 0)
459 * Keep file descriptors open to avoid overhead
460 * of open/close on each call to get* routines.
466 * This does not make sense any more with DNS being default over
467 * the files. Doing a setXXXXent(1) causes a tcp connection to be
468 * used for the queries, which is slower.
471 if (iflag && !sflag) {
472 xo_open_container("statistics");
474 xo_close_container("statistics");
479 xo_open_container("statistics");
485 xo_close_container("statistics");
491 xo_open_container("statistics");
493 if (af == AF_INET || af == AF_UNSPEC)
496 if (af == AF_INET6 || af == AF_UNSPEC)
500 if (af == AF_INET || af == AF_UNSPEC)
503 if (af == AF_INET6 || af == AF_UNSPEC)
507 xo_close_container("statistics");
513 xo_open_container("statistics");
514 printproto(tp, tp->pr_name, &first);
516 xo_close_list("socket");
517 xo_close_container("statistics");
522 xo_open_container("statistics");
523 if (af == AF_INET || af == AF_UNSPEC)
524 for (tp = protox; tp->pr_name; tp++)
525 printproto(tp, tp->pr_name, &first);
527 if (af == AF_INET6 || af == AF_UNSPEC)
528 for (tp = ip6protox; tp->pr_name; tp++)
529 printproto(tp, tp->pr_name, &first);
532 if (af == PF_KEY || af == AF_UNSPEC)
533 for (tp = pfkeyprotox; tp->pr_name; tp++)
534 printproto(tp, tp->pr_name, &first);
537 if (af == AF_NETGRAPH || af == AF_UNSPEC)
538 for (tp = netgraphprotox; tp->pr_name; tp++)
539 printproto(tp, tp->pr_name, &first);
540 #endif /* NETGRAPH */
541 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
542 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
543 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
544 nl[N_UNP_SPHEAD].n_value, &first);
547 xo_close_list("socket");
548 xo_close_container("statistics");
554 fetch_stats_internal(const char *sysctlname, u_long off, void *stats,
555 size_t len, kreadfn_t kreadfn, int zero)
560 memset(stats, 0, len);
562 error = sysctlbyname(sysctlname, NULL, NULL, stats,
565 error = sysctlbyname(sysctlname, stats, &len, NULL, 0);
566 if (error == -1 && errno != ENOENT)
567 xo_warn("sysctl %s", sysctlname);
571 error = kreadfn(off, stats, len);
577 fetch_stats(const char *sysctlname, u_long off, void *stats,
578 size_t len, kreadfn_t kreadfn)
581 return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn,
586 fetch_stats_ro(const char *sysctlname, u_long off, void *stats,
587 size_t len, kreadfn_t kreadfn)
590 return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0));
594 * Print out protocol statistics or control blocks (per sflag).
595 * If the interface was not specifically requested, and the symbol
596 * is not in the namelist, ignore this one.
599 printproto(struct protox *tp, const char *name, bool *first)
601 void (*pr)(u_long, const char *, int, int);
603 bool doingdblocks = false;
608 intpr(tp->pr_istats, af);
610 xo_message("%s: no per-interface stats routine",
617 xo_message("%s: no stats routine",
621 if (tp->pr_usesysctl && live)
623 else if (tp->pr_sindex < 0) {
625 xo_message("%s: stats routine doesn't "
626 "work on cores", tp->pr_name);
629 off = nl[tp->pr_sindex].n_value;
636 xo_message("%s: no PCB routine", tp->pr_name);
639 if (tp->pr_usesysctl && live)
641 else if (tp->pr_index < 0) {
643 xo_message("%s: PCB routine doesn't work on "
644 "cores", tp->pr_name);
647 off = nl[tp->pr_index].n_value;
649 if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
651 if (doingdblocks && *first) {
652 xo_open_list("socket");
656 (*pr)(off, name, af, tp->pr_protocol);
663 char errbuf[_POSIX2_LINE_MAX];
668 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
669 if (setgid(getgid()) != 0)
670 xo_err(-1, "setgid");
673 xo_warnx("kvm not available: %s", errbuf);
681 * Resolve symbol list, return 0 on success.
684 kresolve_list(struct nlist *_nl)
687 if ((kvmd == NULL) && (kvmd_init() != 0))
690 if (_nl[0].n_type != 0)
693 if (kvm_nlist(kvmd, _nl) < 0) {
695 xo_errx(1, "%s: kvm_nlist: %s", nlistf,
698 xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
705 * Wrapper of kvm_dpcpu_setcpu().
708 kset_dpcpu(u_int cpuid)
711 if ((kvmd == NULL) && (kvmd_init() != 0))
712 xo_errx(-1, "%s: kvm is not available", __func__);
714 if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0)
715 xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
716 cpuid, kvm_geterr(kvmd));
721 * Read kernel memory, return 0 on success.
724 kread(u_long addr, void *buf, size_t size)
732 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
733 xo_warnx("%s", kvm_geterr(kvmd));
740 * Read single counter(9).
743 kread_counter(u_long addr)
749 return (kvm_counter_u64_fetch(kvmd, addr));
753 * Read an array of N counters in kernel memory into array of N uint64_t's.
756 kread_counters(u_long addr, void *buf, size_t size)
765 if (size % sizeof(uint64_t) != 0) {
766 xo_warnx("kread_counters: invalid counter set size");
770 n = size / sizeof(uint64_t);
771 if ((counters = malloc(n * sizeof(u_long))) == NULL)
772 xo_err(-1, "malloc");
773 if (kread(addr, counters, n * sizeof(u_long)) < 0) {
779 for (i = 0; i < n; i++)
780 c[i] = kvm_counter_u64_fetch(kvmd, counters[i]);
789 return (n != 1 ? "s" : "");
793 plurales(uintmax_t n)
795 return (n != 1 ? "es" : "");
799 pluralies(uintmax_t n)
801 return (n != 1 ? "ies" : "y");
805 * Find the protox for the given "well-known" name.
807 static struct protox *
808 knownname(const char *name)
810 struct protox **tpp, *tp;
812 for (tpp = protoprotox; *tpp; tpp++)
813 for (tp = *tpp; tp->pr_name; tp++)
814 if (strcmp(tp->pr_name, name) == 0)
820 * Find the protox corresponding to name.
822 static struct protox *
823 name2protox(const char *name)
826 char **alias; /* alias from p->aliases */
830 * Try to find the name in the list of "well-known" names. If that
831 * fails, check if name is an alias for an Internet protocol.
833 if ((tp = knownname(name)) != NULL)
836 setprotoent(1); /* make protocol lookup cheaper */
837 while ((p = getprotoent()) != NULL) {
838 /* assert: name not same as p->name */
839 for (alias = p->p_aliases; *alias; alias++)
840 if (strcmp(name, *alias) == 0) {
842 return (knownname(p->p_name));
852 (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",
853 "usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n"
854 " [-M core] [-N system]",
855 " netstat -i | -I interface [-46abdhnW] [-f address_family]\n"
856 " [-M core] [-N system]",
857 " netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n"
859 " netstat -s [-46sz] [-f protocol_family | -p protocol]\n"
860 " [-M core] [-N system]",
861 " netstat -i | -I interface -s [-46s]\n"
862 " [-f protocol_family | -p protocol] [-M core] [-N system]",
863 " netstat -m [-M core] [-N system]",
864 " netstat -B [-z] [-I interface]",
865 " netstat -r [-46AnW] [-F fibnum] [-f address_family]\n"
866 " [-M core] [-N system]",
867 " netstat -rs [-s] [-M core] [-N system]",
868 " netstat -g [-46W] [-f address_family] [-M core] [-N system]",
869 " netstat -gs [-46s] [-f address_family] [-M core] [-N system]",