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
32 #include <sys/param.h>
37 #include <sys/protosw.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
40 #include <sys/sysctl.h>
42 #include <netinet/in.h>
45 #include <netgraph/ng_socket.h>
69 static struct protox {
70 int pr_index; /* index into nlist of cb head */
71 int pr_sindex; /* index into nlist of stat block */
72 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
73 void (*pr_cblocks)(u_long, const char *, int, int);
74 /* control blocks printing routine */
75 void (*pr_stats)(u_long, const char *, int, int);
76 /* statistics printing routine */
77 void (*pr_istats)(char *); /* per/if statistics printing routine */
78 const char *pr_name; /* well-known name */
79 int pr_usesysctl; /* non-zero if we use sysctl, not kvm */
82 { -1 , N_TCPSTAT, 1, protopr,
83 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
84 { -1 , N_UDPSTAT, 1, protopr,
85 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
87 { -1, N_SCTPSTAT, 1, sctp_protopr,
88 sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP },
92 NULL, NULL, "sdp", 1, IPPROTO_TCP },
94 { -1 , -1, 1, protopr,
95 divert_stats, NULL, "divert", 1, 0 },
96 { -1 , N_IPSTAT, 1, protopr,
97 ip_stats, NULL, "ip", 1, IPPROTO_RAW },
98 { -1 , N_ICMPSTAT, 1, protopr,
99 icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP },
100 { -1 , N_IGMPSTAT, 1, protopr,
101 igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP },
103 { -1, N_IPSEC4STAT, 1, NULL, /* keep as compat */
104 ipsec_stats, NULL, "ipsec", 1, 0},
105 { -1, N_AHSTAT, 1, NULL,
106 ah_stats, NULL, "ah", 1, 0},
107 { -1, N_ESPSTAT, 1, NULL,
108 esp_stats, NULL, "esp", 1, 0},
109 { -1, N_IPCOMPSTAT, 1, NULL,
110 ipcomp_stats, NULL, "ipcomp", 1, 0},
112 { -1 , N_PIMSTAT, 1, protopr,
113 pim_stats, NULL, "pim", 1, IPPROTO_PIM },
114 { -1, N_CARPSTATS, 1, NULL,
115 carp_stats, NULL, "carp", 1, 0 },
117 { -1, N_PFSYNCSTATS, 1, NULL,
118 pfsync_stats, NULL, "pfsync", 1, 0 },
120 { -1, N_ARPSTAT, 1, NULL,
121 arp_stats, NULL, "arp", 1, 0 },
123 NULL, NULL, NULL, 0, 0 }
127 static struct protox ip6protox[] = {
128 { -1 , N_TCPSTAT, 1, protopr,
129 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
130 { -1 , N_UDPSTAT, 1, protopr,
131 udp_stats, NULL, "udp", 1, IPPROTO_UDP },
132 { -1 , N_IP6STAT, 1, protopr,
133 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW },
134 { -1 , N_ICMP6STAT, 1, protopr,
135 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 },
137 { -1, -1, 1, protopr,
138 NULL, NULL, "sdp", 1, IPPROTO_TCP },
141 { -1, N_IPSEC6STAT, 1, NULL,
142 ipsec_stats, NULL, "ipsec6", 1, 0 },
145 { -1, N_PIM6STAT, 1, NULL,
146 pim6_stats, NULL, "pim6", 1, 0 },
148 { -1, N_RIP6STAT, 1, NULL,
149 rip6_stats, NULL, "rip6", 1, 0 },
151 NULL, NULL, NULL, 0, 0 }
156 static struct protox pfkeyprotox[] = {
157 { -1, N_PFKEYSTAT, 1, NULL,
158 pfkey_stats, NULL, "pfkey", 0, 0 },
160 NULL, NULL, NULL, 0, 0 }
165 static struct protox netgraphprotox[] = {
166 { N_NGSOCKLIST, -1, 1, netgraphprotopr,
167 NULL, NULL, "ctrl", 0, 0 },
168 { N_NGSOCKLIST, -1, 1, netgraphprotopr,
169 NULL, NULL, "data", 0, 0 },
171 NULL, NULL, NULL, 0, 0 }
175 static struct protox *protoprotox[] = {
185 static void printproto(struct protox *, const char *, bool *);
186 static void usage(void) __dead2;
187 static struct protox *name2protox(const char *);
188 static struct protox *knownname(const char *);
190 static int kresolve_list(struct nlist *_nl);
193 static char *nlistf = NULL, *memf = NULL;
195 int Aflag; /* show addresses of protocol control block */
196 int aflag; /* show all sockets (including servers) */
197 static int Bflag; /* show information about bpf consumers */
198 int bflag; /* show i/f total bytes in/out */
199 int cflag; /* show TCP congestion control stack */
200 int Cflag; /* show congestion control algo and vars */
201 int dflag; /* show i/f dropped packets */
202 int gflag; /* show group (multicast) routing or stats */
203 int hflag; /* show counters in human readable format */
204 int iflag; /* show interfaces */
205 int Lflag; /* show size of listen queues */
206 int mflag; /* show memory stats */
207 int noutputs = 0; /* how much outputs before we exit */
208 int numeric_addr; /* show addresses numerically */
209 int numeric_port; /* show ports numerically */
210 int Oflag; /* show nhgrp objects*/
211 int oflag; /* show nexthop objects*/
212 int Pflag; /* show TCP log ID */
213 static int pflag; /* show given protocol */
214 static int Qflag; /* show netisr information */
215 int rflag; /* show routing tables (or routing stats) */
216 int Rflag; /* show flow / RSS statistics */
217 int sflag; /* show protocol statistics */
218 int Wflag; /* wide display */
219 int Tflag; /* TCP Information */
220 int xflag; /* extra information, includes all socket buffer info */
221 int zflag; /* zero stats */
223 int interval; /* repeat interval for i/f stats */
225 char *interface; /* desired i/f for stats, or NULL for all i/fs */
226 int unit; /* unit number for above */
228 char *jail_name; /* desired jail to operate in */
231 static int af; /* address family */
232 int live; /* true if we are examining a live system */
235 main(int argc, char *argv[])
237 struct protox *tp = NULL; /* for printing cblocks & stats */
248 argc = xo_parse_args(argc, argv);
252 while ((ch = getopt(argc, argv, "46AaBbCcdF:f:ghI:ij:LlM:mN:nOoPp:Qq:RrSTsuWw:xz"))
259 errx(1, "IPv4 support is not compiled in");
266 errx(1, "IPv6 support is not compiled in");
291 fib = strtol(optarg, &endptr, 0);
292 if (*endptr != '\0' ||
293 (fib == 0 && (errno == EINVAL || errno == ERANGE)))
294 xo_errx(1, "%s: invalid fib", optarg);
297 if (strcmp(optarg, "inet") == 0)
300 else if (strcmp(optarg, "inet6") == 0)
304 else if (strcmp(optarg, "pfkey") == 0)
307 else if (strcmp(optarg, "unix") == 0 ||
308 strcmp(optarg, "local") == 0)
311 else if (strcmp(optarg, "ng") == 0
312 || strcmp(optarg, "netgraph") == 0)
315 else if (strcmp(optarg, "link") == 0)
318 xo_errx(1, "%s: unknown address family",
332 for (cp = interface = optarg; isalpha(*cp); cp++)
346 errx(1, "Jail support is not compiled in");
362 numeric_addr = numeric_port = 1;
374 if ((tp = name2protox(optarg)) == NULL) {
375 xo_errx(1, "%s: unknown or uninstrumented "
384 noutputs = atoi(optarg);
408 interval = atoi(optarg);
427 #define BACKWARD_COMPATIBILITY
428 #ifdef BACKWARD_COMPATIBILITY
430 if (isdigit(**argv)) {
431 interval = atoi(*argv);
446 if (jail_name != NULL) {
447 jid = jail_getid(jail_name);
449 errx(1, "Jail not found");
450 if (jail_attach(jid) != 0)
451 errx(1, "Cannot attach to jail");
456 * Discard setgid privileges if not the running kernel so that bad
457 * guys can't print interesting stuff from kernel memory.
459 live = (nlistf == NULL && memf == NULL);
461 if (setgid(getgid()) != 0)
462 xo_err(-1, "setgid");
463 /* Load all necessary kvm symbols */
468 xo_errx(1, "-x and -T are incompatible, pick one.");
473 bpf_stats(interface);
479 if (kread(0, NULL, 0) == 0)
480 mbpr(kvmd, nl[N_SFSTAT].n_value);
488 if (kread(0, NULL, 0) == 0)
497 * Keep file descriptors open to avoid overhead
498 * of open/close on each call to get* routines.
504 * This does not make sense any more with DNS being default over
505 * the files. Doing a setXXXXent(1) causes a tcp connection to be
506 * used for the queries, which is slower.
509 if (iflag && !sflag) {
510 xo_open_container("statistics");
512 xo_close_container("statistics");
517 xo_open_container("statistics");
525 xo_close_container("statistics");
530 xo_open_container("statistics");
531 nhops_print(fib, af);
532 xo_close_container("statistics");
537 xo_open_container("statistics");
538 nhgrp_print(fib, af);
539 xo_close_container("statistics");
547 xo_open_container("statistics");
549 if (af == AF_INET || af == AF_UNSPEC)
552 if (af == AF_INET6 || af == AF_UNSPEC)
556 if (af == AF_INET || af == AF_UNSPEC)
559 if (af == AF_INET6 || af == AF_UNSPEC)
563 xo_close_container("statistics");
569 xo_open_container("statistics");
570 printproto(tp, tp->pr_name, &first);
572 xo_close_list("socket");
573 xo_close_container("statistics");
578 xo_open_container("statistics");
579 if (af == AF_INET || af == AF_UNSPEC)
580 for (tp = protox; tp->pr_name; tp++)
581 printproto(tp, tp->pr_name, &first);
583 if (af == AF_INET6 || af == AF_UNSPEC)
584 for (tp = ip6protox; tp->pr_name; tp++)
585 printproto(tp, tp->pr_name, &first);
588 if (af == PF_KEY || af == AF_UNSPEC)
589 for (tp = pfkeyprotox; tp->pr_name; tp++)
590 printproto(tp, tp->pr_name, &first);
593 if (af == AF_NETGRAPH || af == AF_UNSPEC)
594 for (tp = netgraphprotox; tp->pr_name; tp++)
595 printproto(tp, tp->pr_name, &first);
596 #endif /* NETGRAPH */
597 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
598 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
599 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
600 nl[N_UNP_SPHEAD].n_value, &first);
603 xo_close_list("socket");
604 xo_close_container("statistics");
610 fetch_stats_internal(const char *sysctlname, u_long off, void *stats,
611 size_t len, kreadfn_t kreadfn, int zero)
616 memset(stats, 0, len);
618 error = sysctlbyname(sysctlname, NULL, NULL, stats,
621 error = sysctlbyname(sysctlname, stats, &len, NULL, 0);
622 if (error == -1 && errno != ENOENT)
623 xo_warn("sysctl %s", sysctlname);
627 error = kreadfn(off, stats, len);
633 fetch_stats(const char *sysctlname, u_long off, void *stats,
634 size_t len, kreadfn_t kreadfn)
637 return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn,
642 fetch_stats_ro(const char *sysctlname, u_long off, void *stats,
643 size_t len, kreadfn_t kreadfn)
646 return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0));
650 * Print out protocol statistics or control blocks (per sflag).
651 * If the interface was not specifically requested, and the symbol
652 * is not in the namelist, ignore this one.
655 printproto(struct protox *tp, const char *name, bool *first)
657 void (*pr)(u_long, const char *, int, int);
659 bool doingdblocks = false;
664 intpr(tp->pr_istats, af);
666 xo_message("%s: no per-interface stats routine",
673 xo_message("%s: no stats routine",
677 if (tp->pr_usesysctl && live)
679 else if (tp->pr_sindex < 0) {
681 xo_message("%s: stats routine doesn't "
682 "work on cores", tp->pr_name);
685 off = nl[tp->pr_sindex].n_value;
692 xo_message("%s: no PCB routine", tp->pr_name);
695 if (tp->pr_usesysctl && live)
697 else if (tp->pr_index < 0) {
699 xo_message("%s: PCB routine doesn't work on "
700 "cores", tp->pr_name);
703 off = nl[tp->pr_index].n_value;
705 if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
707 if (doingdblocks && *first) {
708 xo_open_list("socket");
712 (*pr)(off, name, af, tp->pr_protocol);
719 char errbuf[_POSIX2_LINE_MAX];
724 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
725 if (setgid(getgid()) != 0)
726 xo_err(-1, "setgid");
729 xo_warnx("kvm not available: %s", errbuf);
737 * Resolve symbol list, return 0 on success.
740 kresolve_list(struct nlist *_nl)
743 if ((kvmd == NULL) && (kvmd_init() != 0))
746 if (_nl[0].n_type != 0)
749 if (kvm_nlist(kvmd, _nl) < 0) {
751 xo_errx(1, "%s: kvm_nlist: %s", nlistf,
754 xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
761 * Wrapper of kvm_dpcpu_setcpu().
764 kset_dpcpu(u_int cpuid)
767 if ((kvmd == NULL) && (kvmd_init() != 0))
768 xo_errx(-1, "%s: kvm is not available", __func__);
770 if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0)
771 xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
772 cpuid, kvm_geterr(kvmd));
777 * Read kernel memory, return 0 on success.
780 kread(u_long addr, void *buf, size_t size)
788 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
789 xo_warnx("%s", kvm_geterr(kvmd));
796 * Read single counter(9).
799 kread_counter(u_long addr)
805 return (kvm_counter_u64_fetch(kvmd, addr));
809 * Read an array of N counters in kernel memory into array of N uint64_t's.
812 kread_counters(u_long addr, void *buf, size_t size)
821 if (size % sizeof(uint64_t) != 0) {
822 xo_warnx("kread_counters: invalid counter set size");
826 n = size / sizeof(uint64_t);
827 if ((counters = malloc(n * sizeof(u_long))) == NULL)
828 xo_err(-1, "malloc");
829 if (kread(addr, counters, n * sizeof(u_long)) < 0) {
835 for (i = 0; i < n; i++)
836 c[i] = kvm_counter_u64_fetch(kvmd, counters[i]);
845 return (n != 1 ? "s" : "");
849 plurales(uintmax_t n)
851 return (n != 1 ? "es" : "");
855 pluralies(uintmax_t n)
857 return (n != 1 ? "ies" : "y");
861 * Find the protox for the given "well-known" name.
863 static struct protox *
864 knownname(const char *name)
866 struct protox **tpp, *tp;
868 for (tpp = protoprotox; *tpp; tpp++)
869 for (tp = *tpp; tp->pr_name; tp++)
870 if (strcmp(tp->pr_name, name) == 0)
876 * Find the protox corresponding to name.
878 static struct protox *
879 name2protox(const char *name)
882 char **alias; /* alias from p->aliases */
886 * Try to find the name in the list of "well-known" names. If that
887 * fails, check if name is an alias for an Internet protocol.
889 if ((tp = knownname(name)) != NULL)
892 setprotoent(1); /* make protocol lookup cheaper */
893 while ((p = getprotoent()) != NULL) {
894 /* assert: name not same as p->name */
895 for (alias = p->p_aliases; *alias; alias++)
896 if (strcmp(name, *alias) == 0) {
898 return (knownname(p->p_name));
908 (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",
909 "usage: netstat [-j jail] [-46AaCcLnRSTWx] [-f protocol_family | -p protocol]\n"
910 " [-M core] [-N system]",
911 " netstat [-j jail] -i | -I interface [-46abdhnW] [-f address_family]\n"
912 " [-M core] [-N system]",
913 " netstat [-j jail] -w wait [-I interface] [-46d] [-M core] [-N system]\n"
915 " netstat [-j jail] -s [-46sz] [-f protocol_family | -p protocol]\n"
916 " [-M core] [-N system]",
917 " netstat [-j jail] -i | -I interface -s [-46s]\n"
918 " [-f protocol_family | -p protocol] [-M core] [-N system]",
919 " netstat [-j jail] -m [-M core] [-N system]",
920 " netstat [-j jail] -B [-z] [-I interface]",
921 " netstat [-j jail] -r [-46AnW] [-F fibnum] [-f address_family]\n"
922 " [-M core] [-N system]",
923 " netstat [-j jail] -rs [-s] [-M core] [-N system]",
924 " netstat [-j jail] -g [-46W] [-f address_family] [-M core] [-N system]",
925 " netstat [-j jail] -gs [-46s] [-f address_family] [-M core] [-N system]",
926 " netstat [-j jail] -Q");