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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1983, 1988, 1993 Regents of the University of California. All rights reserved.
34 * @(#)main.c 8.4 (Berkeley) 3/1/94
35 * $FreeBSD: src/usr.bin/netstat/main.c,v 1.34.2.12 2001/09/17 15:17:46 ru Exp $
36 * $DragonFly: src/usr.bin/netstat/main.c,v 1.16 2008/07/11 23:09:18 thomas Exp $
39 #include <sys/param.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #include <netgraph/socket/ng_socket.h>
62 static struct nlist nl[] = {
64 { .n_name = "_ifnet" },
66 { .n_name = "_imp_softc" },
68 { .n_name = "_rtstat" },
70 { .n_name = "_localsw" },
72 { .n_name = "_nspcb"},
74 { .n_name = "_idpstat"},
76 { .n_name = "_spp_istat"},
78 { .n_name = "_ns_errstat"},
80 { .n_name = "_clnp_stat"},
82 { .n_name = "_tp_inpcb" },
84 { .n_name = "_tp_refinfo" },
86 { .n_name = "_tp_stat" },
88 { .n_name = "_esis_stat"},
92 { .n_name = "_rt_tables"},
96 { .n_name = "_cltpstat"},
98 { .n_name = "_nfile" },
100 { .n_name = "_file" },
102 { .n_name = "_mrtstat" },
103 #define N_MFCTABLE 20
104 { .n_name = "_mfctable" },
105 #define N_VIFTABLE 21
106 { .n_name = "_viftable" },
108 { .n_name = "_ipxpcb"},
110 { .n_name = "_ipxstat"},
112 { .n_name = "_spx_istat"},
114 { .n_name = "_ddpstat"},
116 { .n_name = "_ddpcb"},
118 { .n_name = "_ngsocklist"},
120 { .n_name = "_ip6stat" },
121 #define N_ICMP6STAT 29
122 { .n_name = "_icmp6stat" },
123 #define N_IPSECSTAT 30
124 { .n_name = "_ipsecstat" },
125 #define N_IPSEC6STAT 31
126 { .n_name = "_ipsec6stat" },
127 #define N_PIM6STAT 32
128 { .n_name = "_pim6stat" },
129 #define N_MRT6PROTO 33
130 { .n_name = "_ip6_mrtproto" },
131 #define N_MRT6STAT 34
132 { .n_name = "_mrt6stat" },
133 #define N_MF6CTABLE 35
134 { .n_name = "_mf6ctable" },
135 #define N_MIF6TABLE 36
136 { .n_name = "_mif6table" },
137 #define N_PFKEYSTAT 37
138 { .n_name = "_pfkeystat" },
140 { .n_name = "_mbstat" },
142 { .n_name = "_mbtypes" },
143 #define N_NMBCLUSTERS 40
144 { .n_name = "_nmbclusters" },
146 { .n_name = "_nmbufs" },
148 { .n_name = "_rttrash" },
150 { .n_name = "_ncpus" },
151 #define N_CARPSTAT 44
152 { .n_name = "_carpstats" },
157 u_char pr_index; /* index into nlist of cb head */
158 u_char pr_sindex; /* index into nlist of stat block */
159 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
160 void (*pr_cblocks)(u_long, const char *, int);
161 /* control blocks printing routine */
162 void (*pr_stats)(u_long, const char *, int);
163 /* statistics printing routine */
164 void (*pr_istats)(char *); /* per/if statistics printing routine */
165 const char *pr_name; /* well-known name */
166 u_int pr_usesysctl; /* true if we use sysctl, not kvm */
168 { -1, -1, 1, protopr,
169 tcp_stats, NULL, "tcp", IPPROTO_TCP },
170 { -1, -1, 1, protopr,
171 udp_stats, NULL, "udp", IPPROTO_UDP },
172 { -1, -1, 1, protopr,
173 NULL, NULL, "divert",IPPROTO_DIVERT },
174 { -1, -1, 1, protopr,
175 ip_stats, NULL, "ip", IPPROTO_RAW },
176 { -1, -1, 1, protopr,
177 icmp_stats, NULL, "icmp", IPPROTO_ICMP },
178 { -1, -1, 1, protopr,
179 igmp_stats, NULL, "igmp", IPPROTO_IGMP },
181 { -1, N_IPSECSTAT, 1, 0,
182 ipsec_stats, NULL, "ipsec", 0},
184 { -1, N_CARPSTAT, 1, 0,
185 carp_stats, NULL, "carp", 0},
191 struct protox ip6protox[] = {
192 { -1, -1, 1, protopr,
193 tcp_stats, NULL, "tcp", IPPROTO_TCP },
194 { -1, -1, 1, protopr,
195 udp_stats, NULL, "udp", IPPROTO_UDP },
196 { -1, N_IP6STAT, 1, protopr,
197 ip6_stats, ip6_ifstats, "ip6", IPPROTO_RAW },
198 { -1, N_ICMP6STAT, 1, protopr,
199 icmp6_stats, icmp6_ifstats, "icmp6",IPPROTO_ICMPV6 },
201 { -1, N_IPSEC6STAT, 1, 0,
202 ipsec_stats, NULL, "ipsec6",0 },
205 { -1, N_PIM6STAT, 1, 0,
206 pim6_stats, NULL, "pim6", 0 },
209 rip6_stats, NULL, "rip6", 0 },
210 { -1, -1, 1, protopr,
211 pim_stats, NULL, "pim", IPPROTO_PIM },
218 struct protox pfkeyprotox[] = {
219 { -1, N_PFKEYSTAT, 1, 0,
220 pfkey_stats, NULL, "pfkey", 0 },
226 struct protox atalkprotox[] = {
227 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
228 ddp_stats, NULL, "ddp", 0 },
233 struct protox netgraphprotox[] = {
234 { N_NGSOCKS, -1, 1, netgraphprotopr,
235 NULL, NULL, "ctrl", 0 },
236 { N_NGSOCKS, -1, 1, netgraphprotopr,
237 NULL, NULL, "data", 0 },
242 struct protox ipxprotox[] = {
243 { N_IPX, N_IPXSTAT, 1, ipxprotopr,
244 ipx_stats, NULL, "ipx", 0 },
245 { N_IPX, N_SPXSTAT, 1, ipxprotopr,
246 spx_stats, NULL, "spx", 0 },
252 struct protox isoprotox[] = {
253 { ISO_TP, N_TPSTAT, 1, iso_protopr,
254 tp_stats, NULL, "tp" },
255 { N_CLTP, N_CLTPSTAT, 1, iso_protopr,
256 cltp_stats, NULL, "cltp" },
257 { -1, N_CLNPSTAT, 1, 0,
258 clnp_stats, NULL, "clnp"},
259 { -1, N_ESISSTAT, 1, 0,
260 esis_stats, NULL, "esis"},
266 struct protox *protoprotox[] = {
274 ipxprotox, atalkprotox,
280 static void printproto (struct protox *, const char *);
281 static void usage (void);
282 static struct protox *name2protox (char *);
283 static struct protox *knownname (char *);
286 static char *nlistf = NULL, *memf = NULL;
288 int Aflag; /* show addresses of protocol control block */
289 int aflag; /* show all sockets (including servers) */
290 int bflag; /* show i/f total bytes in/out */
291 int cpuflag = -1; /* dump route table from specific cpu */
292 int dflag; /* show i/f dropped packets */
293 int gflag; /* show group (multicast) routing or stats */
294 int iflag; /* show interfaces */
295 int Lflag; /* show size of listen queues */
296 int mflag; /* show memory stats */
297 int Pflag; /* show more protocol info (go past 80 columns) */
298 int numeric_addr; /* show addresses numerically */
299 int numeric_port; /* show ports numerically */
300 static int pflag; /* show given protocol */
301 int rflag; /* show routing tables (or routing stats) */
302 int sflag; /* show protocol statistics */
303 int tflag; /* show i/f watchdog timers */
304 int Bflag; /* show buffer limit instead of buffer use */
305 int Wflag; /* wide display */
306 int zflag; /* zero stats */
308 int interval; /* repeat interval for i/f stats */
310 char *interface; /* desired i/f for stats, or NULL for all i/fs */
311 int unit; /* unit number for above */
313 int af; /* address family */
316 main(int argc, char **argv)
318 struct protox *tp = NULL; /* for printing cblocks & stats */
324 while ((ch = getopt(argc, argv, "Aabc:df:gI:iLlM:mN:nPp:rSsBtuWw:z")) != -1)
337 kread(nl[N_NCPUS].n_value, (char *)&n, sizeof(n));
338 cpuflag = strtol(optarg, NULL, 0);
339 if (cpuflag < 0 || cpuflag >= n)
340 errx(1, "cpu %d does not exist", cpuflag);
346 if (strcmp(optarg, "ipx") == 0)
348 else if (strcmp(optarg, "inet") == 0)
351 else if (strcmp(optarg, "inet6") == 0)
355 else if (strcmp(optarg, "pfkey") == 0)
358 else if (strcmp(optarg, "unix") == 0)
360 else if (strcmp(optarg, "atalk") == 0)
362 else if (strcmp(optarg, "ng") == 0
363 || strcmp(optarg, "netgraph") == 0)
366 else if (strcmp(optarg, "iso") == 0)
369 else if (strcmp(optarg, "link") == 0)
371 else if (strcmp(optarg, "mpls") == 0)
374 errx(1, "%s: unknown address family", optarg);
384 for (cp = interface = optarg; isalpha(*cp); cp++)
405 numeric_addr = numeric_port = 1;
411 if ((tp = name2protox(optarg)) == NULL) {
413 "%s: unknown or uninstrumented protocol",
441 interval = atoi(optarg);
454 #define BACKWARD_COMPATIBILITY
455 #ifdef BACKWARD_COMPATIBILITY
457 if (isdigit(**argv)) {
458 interval = atoi(*argv);
473 * Discard setgid privileges if not the running kernel so that bad
474 * guys can't print interesting stuff from kernel memory.
476 if (nlistf != NULL || memf != NULL)
481 if (kread(0, 0, 0) == 0)
482 mbpr(nl[N_MBSTAT].n_value,
483 nl[N_MBTYPES].n_value,
484 nl[N_NMBCLUSTERS].n_value,
485 nl[N_NMBUFS].n_value,
486 nl[N_NCPUS].n_value);
494 * Keep file descriptors open to avoid overhead
495 * of open/close on each call to get* routines.
501 * This does not make sense any more with DNS being default over
502 * the files. Doing a setXXXXent(1) causes a tcp connection to be
503 * used for the queries, which is slower.
506 if (iflag && !sflag) {
508 intpr(interval, nl[N_IFNET].n_value, NULL);
516 routepr(nl[N_RTREE].n_value);
522 if (af == AF_INET || af == AF_UNSPEC)
523 mrt_stats(nl[N_MRTSTAT].n_value);
525 if (af == AF_INET6 || af == AF_UNSPEC)
526 mrt6_stats(nl[N_MRT6STAT].n_value);
529 if (af == AF_INET || af == AF_UNSPEC)
530 mroutepr(nl[N_MFCTABLE].n_value,
531 nl[N_VIFTABLE].n_value);
533 if (af == AF_INET6 || af == AF_UNSPEC)
534 mroute6pr(nl[N_MF6CTABLE].n_value,
535 nl[N_MIF6TABLE].n_value);
543 printproto(tp, tp->pr_name);
546 if (af == AF_INET || af == AF_UNSPEC)
547 for (tp = protox; tp->pr_name; tp++)
548 printproto(tp, tp->pr_name);
550 if (af == AF_INET6 || af == AF_UNSPEC)
551 for (tp = ip6protox; tp->pr_name; tp++)
552 printproto(tp, tp->pr_name);
555 if (af == PF_KEY || af == AF_UNSPEC)
556 for (tp = pfkeyprotox; tp->pr_name; tp++)
557 printproto(tp, tp->pr_name);
559 if (af == AF_IPX || af == AF_UNSPEC) {
561 for (tp = ipxprotox; tp->pr_name; tp++)
562 printproto(tp, tp->pr_name);
564 if (af == AF_APPLETALK || af == AF_UNSPEC)
565 for (tp = atalkprotox; tp->pr_name; tp++)
566 printproto(tp, tp->pr_name);
567 if (af == AF_NETGRAPH || af == AF_UNSPEC)
568 for (tp = netgraphprotox; tp->pr_name; tp++)
569 printproto(tp, tp->pr_name);
571 if (af == AF_ISO || af == AF_UNSPEC)
572 for (tp = isoprotox; tp->pr_name; tp++)
573 printproto(tp, tp->pr_name);
575 if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
581 * Print out protocol statistics or control blocks (per sflag).
582 * If the interface was not specifically requested, and the symbol
583 * is not in the namelist, ignore this one.
586 printproto(struct protox *tp, const char *name)
588 void (*pr)(u_long, const char *, int);
594 intpr(interval, nl[N_IFNET].n_value,
597 printf("%s: no per-interface stats routine\n",
605 printf("%s: no stats routine\n",
609 off = tp->pr_usesysctl ? tp->pr_usesysctl
610 : nl[tp->pr_sindex].n_value;
616 printf("%s: no PCB routine\n", tp->pr_name);
619 off = tp->pr_usesysctl ? tp->pr_usesysctl
620 : nl[tp->pr_index].n_value;
622 if (pr != NULL && (off || af != AF_UNSPEC))
623 (*pr)(off, name, af);
627 * Read kernel memory, return 0 on success.
630 kread(u_long addr, char *buf, int size)
636 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
638 if (kvm_nlist(kvmd, nl) < 0) {
640 errx(1, "%s: kvm_nlist: %s", nlistf,
643 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
646 if (nl[0].n_type == 0) {
648 errx(1, "%s: no namelist", nlistf);
650 errx(1, "no namelist");
653 warnx("kvm not available");
659 if (kvm_read(kvmd, addr, buf, size) != size) {
660 warnx("%s", kvm_geterr(kvmd));
669 return (n != 1 ? "s" : "");
675 return (n != 1 ? "es" : "");
679 * Find the protox for the given "well-known" name.
681 static struct protox *
682 knownname(char *name)
684 struct protox **tpp, *tp;
686 for (tpp = protoprotox; *tpp; tpp++)
687 for (tp = *tpp; tp->pr_name; tp++)
688 if (strcmp(tp->pr_name, name) == 0)
694 * Find the protox corresponding to name.
696 static struct protox *
697 name2protox(char *name)
700 char **alias; /* alias from p->aliases */
704 * Try to find the name in the list of "well-known" names. If that
705 * fails, check if name is an alias for an Internet protocol.
707 if ((tp = knownname(name)) != NULL)
710 setprotoent(1); /* make protocol lookup cheaper */
711 while ((p = getprotoent()) != NULL) {
712 /* assert: name not same as p->name */
713 for (alias = p->p_aliases; *alias; alias++)
714 if (strcmp(name, *alias) == 0) {
716 return (knownname(p->p_name));
726 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
727 "usage: netstat [-AaLnPSW] [-c cpu] [-f protocol_family | -p protocol]\n"
728 " [-M core] [-N system]",
729 " netstat -i | -I interface [-aBbdnt] [-f address_family]\n"
730 " [-M core] [-N system]",
731 " netstat -w wait [-I interface] [-d] [-M core] [-N system]",
732 " netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
733 " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
734 " [-M core] [-N system]",
735 " netstat -m [-M core] [-N system]",
736 " netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
737 " netstat -rs [-s] [-M core] [-N system]",
738 " netstat -g [-W] [-f address_family] [-M core] [-N system]",
739 " netstat -gs [-s] [-f address_family] [-M core] [-N system]");