2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1988, 1993
5 * Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static char const copyright[] =
34 "@(#) Copyright (c) 1983, 1988, 1993\n\
35 Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
54 #include <netinet/in.h>
57 #include <netgraph/ng_socket.h>
78 static struct protox {
79 int pr_index; /* index into nlist of cb head */
80 int pr_sindex; /* index into nlist of stat block */
81 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
82 void (*pr_cblocks)(u_long, const char *, int, int);
83 /* control blocks printing routine */
84 void (*pr_stats)(u_long, const char *, int, int);
85 /* statistics printing routine */
86 void (*pr_istats)(char *); /* per/if statistics printing routine */
87 const char *pr_name; /* well-known name */
88 int pr_usesysctl; /* non-zero if we use sysctl, not kvm */
91 { N_TCBINFO, N_TCPSTAT, 1, protopr,
92 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
93 { N_UDBINFO, N_UDPSTAT, 1, protopr,
94 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
96 { -1, N_SCTPSTAT, 1, sctp_protopr,
97 sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP },
100 { -1, -1, 1, protopr,
101 NULL, NULL, "sdp", 1, IPPROTO_TCP },
103 { N_DIVCBINFO, -1, 1, protopr,
104 NULL, NULL, "divert", 1, IPPROTO_DIVERT },
105 { N_RIPCBINFO, N_IPSTAT, 1, protopr,
106 ip_stats, NULL, "ip", 1, IPPROTO_RAW },
107 { N_RIPCBINFO, N_ICMPSTAT, 1, protopr,
108 icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP },
109 { N_RIPCBINFO, N_IGMPSTAT, 1, protopr,
110 igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP },
112 { -1, N_IPSEC4STAT, 1, NULL, /* keep as compat */
113 ipsec_stats, NULL, "ipsec", 1, 0},
114 { -1, N_AHSTAT, 1, NULL,
115 ah_stats, NULL, "ah", 1, 0},
116 { -1, N_ESPSTAT, 1, NULL,
117 esp_stats, NULL, "esp", 1, 0},
118 { -1, N_IPCOMPSTAT, 1, NULL,
119 ipcomp_stats, NULL, "ipcomp", 1, 0},
121 { N_RIPCBINFO, N_PIMSTAT, 1, protopr,
122 pim_stats, NULL, "pim", 1, IPPROTO_PIM },
123 { -1, N_CARPSTATS, 1, NULL,
124 carp_stats, NULL, "carp", 1, 0 },
126 { -1, N_PFSYNCSTATS, 1, NULL,
127 pfsync_stats, NULL, "pfsync", 1, 0 },
129 { -1, N_ARPSTAT, 1, NULL,
130 arp_stats, NULL, "arp", 1, 0 },
132 NULL, NULL, NULL, 0, 0 }
136 static struct protox ip6protox[] = {
137 { N_TCBINFO, N_TCPSTAT, 1, protopr,
138 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
139 { N_UDBINFO, N_UDPSTAT, 1, protopr,
140 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
141 { N_RIPCBINFO, N_IP6STAT, 1, protopr,
142 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW },
143 { N_RIPCBINFO, N_ICMP6STAT, 1, protopr,
144 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 },
146 { -1, -1, 1, protopr,
147 NULL, NULL, "sdp", 1, IPPROTO_TCP },
150 { -1, N_IPSEC6STAT, 1, NULL,
151 ipsec_stats, NULL, "ipsec6", 1, 0 },
154 { -1, N_PIM6STAT, 1, NULL,
155 pim6_stats, NULL, "pim6", 1, 0 },
157 { -1, N_RIP6STAT, 1, NULL,
158 rip6_stats, NULL, "rip6", 1, 0 },
160 NULL, NULL, NULL, 0, 0 }
165 static struct protox pfkeyprotox[] = {
166 { -1, N_PFKEYSTAT, 1, NULL,
167 pfkey_stats, NULL, "pfkey", 0, 0 },
169 NULL, NULL, NULL, 0, 0 }
174 static struct protox netgraphprotox[] = {
175 { N_NGSOCKLIST, -1, 1, netgraphprotopr,
176 NULL, NULL, "ctrl", 0, 0 },
177 { N_NGSOCKLIST, -1, 1, netgraphprotopr,
178 NULL, NULL, "data", 0, 0 },
180 NULL, NULL, NULL, 0, 0 }
184 static struct protox *protoprotox[] = {
194 static void printproto(struct protox *, const char *, bool *);
195 static void usage(void);
196 static struct protox *name2protox(const char *);
197 static struct protox *knownname(const char *);
199 static int kresolve_list(struct nlist *_nl);
202 static char *nlistf = NULL, *memf = NULL;
204 int Aflag; /* show addresses of protocol control block */
205 int aflag; /* show all sockets (including servers) */
206 static int Bflag; /* show information about bpf consumers */
207 int bflag; /* show i/f total bytes in/out */
208 int cflag; /* show TCP congestion control stack */
209 int Cflag; /* show congestion control algo and vars */
210 int dflag; /* show i/f dropped packets */
211 int gflag; /* show group (multicast) routing or stats */
212 int hflag; /* show counters in human readable format */
213 int iflag; /* show interfaces */
214 int Lflag; /* show size of listen queues */
215 int mflag; /* show memory stats */
216 int noutputs = 0; /* how much outputs before we exit */
217 int numeric_addr; /* show addresses numerically */
218 int numeric_port; /* show ports numerically */
219 int Oflag; /* show nhgrp objects*/
220 int oflag; /* show nexthop objects*/
221 int Pflag; /* show TCP log ID */
222 static int pflag; /* show given protocol */
223 static int Qflag; /* show netisr information */
224 int rflag; /* show routing tables (or routing stats) */
225 int Rflag; /* show flow / RSS statistics */
226 int sflag; /* show protocol statistics */
227 int Wflag; /* wide display */
228 int Tflag; /* TCP Information */
229 int xflag; /* extra information, includes all socket buffer info */
230 int zflag; /* zero stats */
232 int interval; /* repeat interval for i/f stats */
234 char *interface; /* desired i/f for stats, or NULL for all i/fs */
235 int unit; /* unit number for above */
237 static int af; /* address family */
238 int live; /* true if we are examining a live system */
241 main(int argc, char *argv[])
243 struct protox *tp = NULL; /* for printing cblocks & stats */
251 argc = xo_parse_args(argc, argv);
255 while ((ch = getopt(argc, argv, "46AaBbCcdF:f:ghI:iLlM:mN:nOoPp:Qq:RrSTsuWw:xz"))
262 errx(1, "IPv4 support is not compiled in");
269 errx(1, "IPv6 support is not compiled in");
294 fib = strtol(optarg, &endptr, 0);
295 if (*endptr != '\0' ||
296 (fib == 0 && (errno == EINVAL || errno == ERANGE)))
297 xo_errx(1, "%s: invalid fib", optarg);
300 if (strcmp(optarg, "inet") == 0)
303 else if (strcmp(optarg, "inet6") == 0)
307 else if (strcmp(optarg, "pfkey") == 0)
310 else if (strcmp(optarg, "unix") == 0 ||
311 strcmp(optarg, "local") == 0)
314 else if (strcmp(optarg, "ng") == 0
315 || strcmp(optarg, "netgraph") == 0)
318 else if (strcmp(optarg, "link") == 0)
321 xo_errx(1, "%s: unknown address family",
335 for (cp = interface = optarg; isalpha(*cp); cp++)
356 numeric_addr = numeric_port = 1;
368 if ((tp = name2protox(optarg)) == NULL) {
369 xo_errx(1, "%s: unknown or uninstrumented "
378 noutputs = atoi(optarg);
402 interval = atoi(optarg);
421 #define BACKWARD_COMPATIBILITY
422 #ifdef BACKWARD_COMPATIBILITY
424 if (isdigit(**argv)) {
425 interval = atoi(*argv);
440 * Discard setgid privileges if not the running kernel so that bad
441 * guys can't print interesting stuff from kernel memory.
443 live = (nlistf == NULL && memf == NULL);
445 if (setgid(getgid()) != 0)
446 xo_err(-1, "setgid");
447 /* Load all necessary kvm symbols */
452 xo_errx(1, "-x and -T are incompatible, pick one.");
457 bpf_stats(interface);
463 if (kread(0, NULL, 0) == 0)
464 mbpr(kvmd, nl[N_SFSTAT].n_value);
472 if (kread(0, NULL, 0) == 0)
481 * Keep file descriptors open to avoid overhead
482 * of open/close on each call to get* routines.
488 * This does not make sense any more with DNS being default over
489 * the files. Doing a setXXXXent(1) causes a tcp connection to be
490 * used for the queries, which is slower.
493 if (iflag && !sflag) {
494 xo_open_container("statistics");
496 xo_close_container("statistics");
501 xo_open_container("statistics");
509 xo_close_container("statistics");
514 xo_open_container("statistics");
515 nhops_print(fib, af);
516 xo_close_container("statistics");
521 xo_open_container("statistics");
522 nhgrp_print(fib, af);
523 xo_close_container("statistics");
531 xo_open_container("statistics");
533 if (af == AF_INET || af == AF_UNSPEC)
536 if (af == AF_INET6 || af == AF_UNSPEC)
540 if (af == AF_INET || af == AF_UNSPEC)
543 if (af == AF_INET6 || af == AF_UNSPEC)
547 xo_close_container("statistics");
553 xo_open_container("statistics");
554 printproto(tp, tp->pr_name, &first);
556 xo_close_list("socket");
557 xo_close_container("statistics");
562 xo_open_container("statistics");
563 if (af == AF_INET || af == AF_UNSPEC)
564 for (tp = protox; tp->pr_name; tp++)
565 printproto(tp, tp->pr_name, &first);
567 if (af == AF_INET6 || af == AF_UNSPEC)
568 for (tp = ip6protox; tp->pr_name; tp++)
569 printproto(tp, tp->pr_name, &first);
572 if (af == PF_KEY || af == AF_UNSPEC)
573 for (tp = pfkeyprotox; tp->pr_name; tp++)
574 printproto(tp, tp->pr_name, &first);
577 if (af == AF_NETGRAPH || af == AF_UNSPEC)
578 for (tp = netgraphprotox; tp->pr_name; tp++)
579 printproto(tp, tp->pr_name, &first);
580 #endif /* NETGRAPH */
581 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
582 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
583 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
584 nl[N_UNP_SPHEAD].n_value, &first);
587 xo_close_list("socket");
588 xo_close_container("statistics");
594 fetch_stats_internal(const char *sysctlname, u_long off, void *stats,
595 size_t len, kreadfn_t kreadfn, int zero)
600 memset(stats, 0, len);
602 error = sysctlbyname(sysctlname, NULL, NULL, stats,
605 error = sysctlbyname(sysctlname, stats, &len, NULL, 0);
606 if (error == -1 && errno != ENOENT)
607 xo_warn("sysctl %s", sysctlname);
611 error = kreadfn(off, stats, len);
617 fetch_stats(const char *sysctlname, u_long off, void *stats,
618 size_t len, kreadfn_t kreadfn)
621 return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn,
626 fetch_stats_ro(const char *sysctlname, u_long off, void *stats,
627 size_t len, kreadfn_t kreadfn)
630 return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0));
634 * Print out protocol statistics or control blocks (per sflag).
635 * If the interface was not specifically requested, and the symbol
636 * is not in the namelist, ignore this one.
639 printproto(struct protox *tp, const char *name, bool *first)
641 void (*pr)(u_long, const char *, int, int);
643 bool doingdblocks = false;
648 intpr(tp->pr_istats, af);
650 xo_message("%s: no per-interface stats routine",
657 xo_message("%s: no stats routine",
661 if (tp->pr_usesysctl && live)
663 else if (tp->pr_sindex < 0) {
665 xo_message("%s: stats routine doesn't "
666 "work on cores", tp->pr_name);
669 off = nl[tp->pr_sindex].n_value;
676 xo_message("%s: no PCB routine", tp->pr_name);
679 if (tp->pr_usesysctl && live)
681 else if (tp->pr_index < 0) {
683 xo_message("%s: PCB routine doesn't work on "
684 "cores", tp->pr_name);
687 off = nl[tp->pr_index].n_value;
689 if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
691 if (doingdblocks && *first) {
692 xo_open_list("socket");
696 (*pr)(off, name, af, tp->pr_protocol);
703 char errbuf[_POSIX2_LINE_MAX];
708 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
709 if (setgid(getgid()) != 0)
710 xo_err(-1, "setgid");
713 xo_warnx("kvm not available: %s", errbuf);
721 * Resolve symbol list, return 0 on success.
724 kresolve_list(struct nlist *_nl)
727 if ((kvmd == NULL) && (kvmd_init() != 0))
730 if (_nl[0].n_type != 0)
733 if (kvm_nlist(kvmd, _nl) < 0) {
735 xo_errx(1, "%s: kvm_nlist: %s", nlistf,
738 xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
745 * Wrapper of kvm_dpcpu_setcpu().
748 kset_dpcpu(u_int cpuid)
751 if ((kvmd == NULL) && (kvmd_init() != 0))
752 xo_errx(-1, "%s: kvm is not available", __func__);
754 if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0)
755 xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
756 cpuid, kvm_geterr(kvmd));
761 * Read kernel memory, return 0 on success.
764 kread(u_long addr, void *buf, size_t size)
772 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
773 xo_warnx("%s", kvm_geterr(kvmd));
780 * Read single counter(9).
783 kread_counter(u_long addr)
789 return (kvm_counter_u64_fetch(kvmd, addr));
793 * Read an array of N counters in kernel memory into array of N uint64_t's.
796 kread_counters(u_long addr, void *buf, size_t size)
805 if (size % sizeof(uint64_t) != 0) {
806 xo_warnx("kread_counters: invalid counter set size");
810 n = size / sizeof(uint64_t);
811 if ((counters = malloc(n * sizeof(u_long))) == NULL)
812 xo_err(-1, "malloc");
813 if (kread(addr, counters, n * sizeof(u_long)) < 0) {
819 for (i = 0; i < n; i++)
820 c[i] = kvm_counter_u64_fetch(kvmd, counters[i]);
829 return (n != 1 ? "s" : "");
833 plurales(uintmax_t n)
835 return (n != 1 ? "es" : "");
839 pluralies(uintmax_t n)
841 return (n != 1 ? "ies" : "y");
845 * Find the protox for the given "well-known" name.
847 static struct protox *
848 knownname(const char *name)
850 struct protox **tpp, *tp;
852 for (tpp = protoprotox; *tpp; tpp++)
853 for (tp = *tpp; tp->pr_name; tp++)
854 if (strcmp(tp->pr_name, name) == 0)
860 * Find the protox corresponding to name.
862 static struct protox *
863 name2protox(const char *name)
866 char **alias; /* alias from p->aliases */
870 * Try to find the name in the list of "well-known" names. If that
871 * fails, check if name is an alias for an Internet protocol.
873 if ((tp = knownname(name)) != NULL)
876 setprotoent(1); /* make protocol lookup cheaper */
877 while ((p = getprotoent()) != NULL) {
878 /* assert: name not same as p->name */
879 for (alias = p->p_aliases; *alias; alias++)
880 if (strcmp(name, *alias) == 0) {
882 return (knownname(p->p_name));
892 (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",
893 "usage: netstat [-46AaCcLnRSTWx] [-f protocol_family | -p protocol]\n"
894 " [-M core] [-N system]",
895 " netstat -i | -I interface [-46abdhnW] [-f address_family]\n"
896 " [-M core] [-N system]",
897 " netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n"
899 " netstat -s [-46sz] [-f protocol_family | -p protocol]\n"
900 " [-M core] [-N system]",
901 " netstat -i | -I interface -s [-46s]\n"
902 " [-f protocol_family | -p protocol] [-M core] [-N system]",
903 " netstat -m [-M core] [-N system]",
904 " netstat -B [-z] [-I interface]",
905 " netstat -r [-46AnW] [-F fibnum] [-f address_family]\n"
906 " [-M core] [-N system]",
907 " netstat -rs [-s] [-M core] [-N system]",
908 " netstat -g [-46W] [-f address_family] [-M core] [-N system]",
909 " netstat -gs [-46s] [-f address_family] [-M core] [-N system]",