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 sflag; /* show protocol statistics */
299 int Wflag; /* wide display */
300 int Tflag; /* TCP Information */
301 int xflag; /* extra information, includes all socket buffer info */
302 int zflag; /* zero stats */
304 int interval; /* repeat interval for i/f stats */
306 char *interface; /* desired i/f for stats, or NULL for all i/fs */
307 int unit; /* unit number for above */
309 int af; /* address family */
310 int live; /* true if we are examining a live system */
313 main(int argc, char *argv[])
315 struct protox *tp = NULL; /* for printing cblocks & stats */
322 while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:rSTsuWw:xz"))
329 errx(1, "IPv4 support is not compiled in");
336 errx(1, "IPv6 support is not compiled in");
355 fib = strtol(optarg, &endptr, 0);
356 if (*endptr != '\0' ||
357 (fib == 0 && (errno == EINVAL || errno == ERANGE)))
358 errx(1, "%s: invalid fib", optarg);
361 if (strcmp(optarg, "inet") == 0)
364 else if (strcmp(optarg, "inet6") == 0)
368 else if (strcmp(optarg, "pfkey") == 0)
371 else if (strcmp(optarg, "unix") == 0)
374 else if (strcmp(optarg, "ng") == 0
375 || strcmp(optarg, "netgraph") == 0)
378 else if (strcmp(optarg, "link") == 0)
381 errx(1, "%s: unknown address family", optarg);
394 for (cp = interface = optarg; isalpha(*cp); cp++)
415 numeric_addr = numeric_port = 1;
418 if ((tp = name2protox(optarg)) == NULL) {
420 "%s: unknown or uninstrumented protocol",
429 noutputs = atoi(optarg);
450 interval = atoi(optarg);
469 #define BACKWARD_COMPATIBILITY
470 #ifdef BACKWARD_COMPATIBILITY
472 if (isdigit(**argv)) {
473 interval = atoi(*argv);
488 * Discard setgid privileges if not the running kernel so that bad
489 * guys can't print interesting stuff from kernel memory.
491 live = (nlistf == NULL && memf == NULL);
496 errx(1, "-x and -T are incompatible, pick one.");
501 bpf_stats(interface);
506 if (kread(0, NULL, 0) == 0)
507 mbpr(kvmd, nl[N_SFSTAT].n_value);
514 if (kread(0, NULL, 0) == 0)
522 * Keep file descriptors open to avoid overhead
523 * of open/close on each call to get* routines.
529 * This does not make sense any more with DNS being default over
530 * the files. Doing a setXXXXent(1) causes a tcp connection to be
531 * used for the queries, which is slower.
534 if (iflag && !sflag) {
535 intpr(interval, NULL, af);
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)
566 /* Load all necessary kvm symbols */
570 printproto(tp, tp->pr_name);
573 if (af == AF_INET || af == AF_UNSPEC)
574 for (tp = protox; tp->pr_name; tp++)
575 printproto(tp, tp->pr_name);
577 if (af == AF_INET6 || af == AF_UNSPEC)
578 for (tp = ip6protox; tp->pr_name; tp++)
579 printproto(tp, tp->pr_name);
582 if (af == PF_KEY || af == AF_UNSPEC)
583 for (tp = pfkeyprotox; tp->pr_name; tp++)
584 printproto(tp, tp->pr_name);
587 if (af == AF_NETGRAPH || af == AF_UNSPEC)
588 for (tp = netgraphprotox; tp->pr_name; tp++)
589 printproto(tp, tp->pr_name);
590 #endif /* NETGRAPH */
591 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
592 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
593 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
594 nl[N_UNP_SPHEAD].n_value);
599 * Print out protocol statistics or control blocks (per sflag).
600 * If the interface was not specifically requested, and the symbol
601 * is not in the namelist, ignore this one.
604 printproto(struct protox *tp, const char *name)
606 void (*pr)(u_long, const char *, int, int);
612 intpr(interval, tp->pr_istats, af);
614 printf("%s: no per-interface stats routine\n",
621 printf("%s: no stats routine\n",
625 if (tp->pr_usesysctl && live)
627 else if (tp->pr_sindex < 0) {
630 "%s: stats routine doesn't work on cores\n",
634 off = nl[tp->pr_sindex].n_value;
640 printf("%s: no PCB routine\n", tp->pr_name);
643 if (tp->pr_usesysctl && live)
645 else if (tp->pr_index < 0) {
648 "%s: PCB routine doesn't work on cores\n",
652 off = nl[tp->pr_index].n_value;
654 if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
656 (*pr)(off, name, af, tp->pr_protocol);
662 char errbuf[_POSIX2_LINE_MAX];
667 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
671 warnx("kvm not available: %s", errbuf);
679 * Resolve symbol list, return 0 on success.
682 kresolve_list(struct nlist *_nl)
685 if ((kvmd == NULL) && (kvmd_init() != 0))
688 if (_nl[0].n_type != 0)
691 if (kvm_nlist(kvmd, _nl) < 0) {
693 errx(1, "%s: kvm_nlist: %s", nlistf,
696 errx(1, "kvm_nlist: %s", 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 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)
745 if (kread(addr, buf, size) < 0)
749 *c = kvm_counter_u64_fetch(kvmd, *c);
759 return (n != 1 ? "s" : "");
763 plurales(uintmax_t n)
765 return (n != 1 ? "es" : "");
769 pluralies(uintmax_t n)
771 return (n != 1 ? "ies" : "y");
775 * Find the protox for the given "well-known" name.
777 static struct protox *
778 knownname(const char *name)
780 struct protox **tpp, *tp;
782 for (tpp = protoprotox; *tpp; tpp++)
783 for (tp = *tpp; tp->pr_name; tp++)
784 if (strcmp(tp->pr_name, name) == 0)
790 * Find the protox corresponding to name.
792 static struct protox *
793 name2protox(const char *name)
796 char **alias; /* alias from p->aliases */
800 * Try to find the name in the list of "well-known" names. If that
801 * fails, check if name is an alias for an Internet protocol.
803 if ((tp = knownname(name)) != NULL)
806 setprotoent(1); /* make protocol lookup cheaper */
807 while ((p = getprotoent()) != NULL) {
808 /* assert: name not same as p->name */
809 for (alias = p->p_aliases; *alias; alias++)
810 if (strcmp(name, *alias) == 0) {
812 return (knownname(p->p_name));
822 (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",
823 "usage: netstat [-46AaLnSTWx] [-f protocol_family | -p protocol]\n"
824 " [-M core] [-N system]",
825 " netstat -i | -I interface [-46abdhnW] [-f address_family]\n"
826 " [-M core] [-N system]",
827 " netstat -w wait [-I interface] [-46d] [-M core] [-N system] [-q howmany]",
828 " netstat -s [-s] [-46z] [-f protocol_family | -p protocol]\n"
829 " [-M core] [-N system]",
830 " netstat -i | -I interface [-46s] [-f protocol_family | -p protocol]\n"
831 " [-M core] [-N system]",
832 " netstat -m [-M core] [-N system]",
833 " netstat -B [-I interface]",
834 " netstat -r [-46AanW] [-f address_family] [-M core] [-N system]",
835 " netstat -rs [-s] [-M core] [-N system]",
836 " netstat -g [-46W] [-f address_family] [-M core] [-N system]",
837 " netstat -gs [-46s] [-f address_family] [-M core] [-N system]",