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 $
38 #include <sys/param.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
43 #include <netinet/in.h>
45 #include <netgraph/socket/ng_socket.h>
61 static struct nlist nl[] = {
63 { .n_name = "_ifnet" },
65 { .n_name = "_imp_softc" },
67 { .n_name = "_rtstat" },
69 { .n_name = "_localsw" },
71 { .n_name = "_nspcb"},
73 { .n_name = "_idpstat"},
75 { .n_name = "_spp_istat"},
77 { .n_name = "_ns_errstat"},
79 { .n_name = "_clnp_stat"},
81 { .n_name = "_tp_inpcb" },
83 { .n_name = "_tp_refinfo" },
85 { .n_name = "_tp_stat" },
87 { .n_name = "_esis_stat"},
91 { .n_name = "_rt_tables"},
95 { .n_name = "_cltpstat"},
97 { .n_name = "_nfile" },
99 { .n_name = "_file" },
101 { .n_name = "_mrtstat" },
102 #define N_MFCTABLE 20
103 { .n_name = "_mfctable" },
104 #define N_VIFTABLE 21
105 { .n_name = "_viftable" },
107 { .n_name = "_ipxpcb"},
109 { .n_name = "_ipxstat"},
111 { .n_name = "_spx_istat"},
113 { .n_name = "_ngsocklist"},
115 { .n_name = "_ip6stat" },
116 #define N_ICMP6STAT 29
117 { .n_name = "_icmp6stat" },
118 #define N_IPSECSTAT 30
119 { .n_name = "_ipsecstat" },
120 #define N_IPSEC6STAT 31
121 { .n_name = "_ipsec6stat" },
122 #define N_PIM6STAT 32
123 { .n_name = "_pim6stat" },
124 #define N_MRT6PROTO 33
125 { .n_name = "_ip6_mrtproto" },
126 #define N_MRT6STAT 34
127 { .n_name = "_mrt6stat" },
128 #define N_MF6CTABLE 35
129 { .n_name = "_mf6ctable" },
130 #define N_MIF6TABLE 36
131 { .n_name = "_mif6table" },
132 #define N_PFKEYSTAT 37
133 { .n_name = "_pfkeystat" },
135 { .n_name = "_mbstat" },
137 { .n_name = "_mbtypes" },
138 #define N_NMBCLUSTERS 40
139 { .n_name = "_nmbclusters" },
141 { .n_name = "_nmbufs" },
143 { .n_name = "_rttrash" },
145 { .n_name = "_ncpus" },
146 #define N_CARPSTAT 44
147 { .n_name = "_carpstats" },
152 u_char pr_index; /* index into nlist of cb head */
153 u_char pr_sindex; /* index into nlist of stat block */
154 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
155 void (*pr_cblocks)(u_long, const char *, int);
156 /* control blocks printing routine */
157 void (*pr_stats)(u_long, const char *, int);
158 /* statistics printing routine */
159 void (*pr_istats)(char *); /* per/if statistics printing routine */
160 const char *pr_name; /* well-known name */
161 u_int pr_usesysctl; /* true if we use sysctl, not kvm */
163 { -1, -1, 1, protopr,
164 tcp_stats, NULL, "tcp", IPPROTO_TCP },
165 { -1, -1, 1, protopr,
166 udp_stats, NULL, "udp", IPPROTO_UDP },
167 { -1, -1, 1, protopr,
168 NULL, NULL, "divert",IPPROTO_DIVERT },
169 { -1, -1, 1, protopr,
170 ip_stats, NULL, "ip", IPPROTO_RAW },
171 { -1, -1, 1, protopr,
172 icmp_stats, NULL, "icmp", IPPROTO_ICMP },
173 { -1, -1, 1, protopr,
174 igmp_stats, NULL, "igmp", IPPROTO_IGMP },
176 { -1, N_IPSECSTAT, 1, 0,
177 ipsec_stats, NULL, "ipsec", 0},
179 { -1, N_CARPSTAT, 1, 0,
180 carp_stats, NULL, "carp", 0},
186 struct protox ip6protox[] = {
187 { -1, -1, 1, protopr,
188 tcp_stats, NULL, "tcp", IPPROTO_TCP },
189 { -1, -1, 1, protopr,
190 udp_stats, NULL, "udp", IPPROTO_UDP },
191 { -1, N_IP6STAT, 1, protopr,
192 ip6_stats, ip6_ifstats, "ip6", IPPROTO_RAW },
193 { -1, N_ICMP6STAT, 1, protopr,
194 icmp6_stats, icmp6_ifstats, "icmp6",IPPROTO_ICMPV6 },
196 { -1, N_IPSEC6STAT, 1, 0,
197 ipsec_stats, NULL, "ipsec6",0 },
200 { -1, N_PIM6STAT, 1, 0,
201 pim6_stats, NULL, "pim6", 0 },
204 rip6_stats, NULL, "rip6", 0 },
205 { -1, -1, 1, protopr,
206 pim_stats, NULL, "pim", IPPROTO_PIM },
213 struct protox pfkeyprotox[] = {
214 { -1, N_PFKEYSTAT, 1, 0,
215 pfkey_stats, NULL, "pfkey", 0 },
221 struct protox netgraphprotox[] = {
222 { N_NGSOCKS, -1, 1, netgraphprotopr,
223 NULL, NULL, "ctrl", 0 },
224 { N_NGSOCKS, -1, 1, netgraphprotopr,
225 NULL, NULL, "data", 0 },
230 struct protox ipxprotox[] = {
231 { N_IPX, N_IPXSTAT, 1, ipxprotopr,
232 ipx_stats, NULL, "ipx", 0 },
233 { N_IPX, N_SPXSTAT, 1, ipxprotopr,
234 spx_stats, NULL, "spx", 0 },
240 struct protox isoprotox[] = {
241 { ISO_TP, N_TPSTAT, 1, iso_protopr,
242 tp_stats, NULL, "tp" },
243 { N_CLTP, N_CLTPSTAT, 1, iso_protopr,
244 cltp_stats, NULL, "cltp" },
245 { -1, N_CLNPSTAT, 1, 0,
246 clnp_stats, NULL, "clnp"},
247 { -1, N_ESISSTAT, 1, 0,
248 esis_stats, NULL, "esis"},
254 struct protox *protoprotox[] = {
268 static void printproto (struct protox *, const char *);
269 static void usage (void);
270 static struct protox *name2protox (char *);
271 static struct protox *knownname (char *);
274 static char *nlistf = NULL, *memf = NULL;
276 int Aflag; /* show addresses of protocol control block */
277 int aflag; /* show all sockets (including servers) */
278 int bflag; /* show i/f total bytes in/out */
279 int cpuflag = -1; /* dump route table from specific cpu */
280 int dflag; /* show i/f dropped packets */
281 int gflag; /* show group (multicast) routing or stats */
282 int iflag; /* show interfaces */
283 int Lflag; /* show size of listen queues */
284 int mflag; /* show memory stats */
285 int Pflag; /* show more protocol info (go past 80 columns) */
286 int numeric_addr; /* show addresses numerically */
287 int numeric_port; /* show ports numerically */
288 static int pflag; /* show given protocol */
289 int rflag; /* show routing tables (or routing stats) */
290 int sflag; /* show protocol statistics */
291 int tflag; /* show i/f watchdog timers */
292 int Bflag; /* show buffer limit instead of buffer use */
293 int Wflag; /* wide display */
294 int zflag; /* zero stats */
296 int interval; /* repeat interval for i/f stats */
298 char *interface; /* desired i/f for stats, or NULL for all i/fs */
299 int unit; /* unit number for above */
301 int af; /* address family */
304 main(int argc, char **argv)
306 struct protox *tp = NULL; /* for printing cblocks & stats */
312 while ((ch = getopt(argc, argv, "Aabc:df:gI:iLlM:mN:nPp:rSsBtuWw:z")) != -1)
325 kread(nl[N_NCPUS].n_value, (char *)&n, sizeof(n));
326 cpuflag = strtol(optarg, NULL, 0);
327 if (cpuflag < 0 || cpuflag >= n)
328 errx(1, "cpu %d does not exist", cpuflag);
334 if (strcmp(optarg, "ipx") == 0)
336 else if (strcmp(optarg, "inet") == 0)
339 else if (strcmp(optarg, "inet6") == 0)
343 else if (strcmp(optarg, "pfkey") == 0)
346 else if (strcmp(optarg, "unix") == 0)
348 else if (strcmp(optarg, "ng") == 0
349 || strcmp(optarg, "netgraph") == 0)
352 else if (strcmp(optarg, "iso") == 0)
355 else if (strcmp(optarg, "link") == 0)
357 else if (strcmp(optarg, "mpls") == 0)
360 errx(1, "%s: unknown address family", optarg);
370 for (cp = interface = optarg; isalpha(*cp); cp++)
391 numeric_addr = numeric_port = 1;
397 if ((tp = name2protox(optarg)) == NULL) {
399 "%s: unknown or uninstrumented protocol",
427 interval = atoi(optarg);
440 #define BACKWARD_COMPATIBILITY
441 #ifdef BACKWARD_COMPATIBILITY
443 if (isdigit(**argv)) {
444 interval = atoi(*argv);
459 * Discard setgid privileges if not the running kernel so that bad
460 * guys can't print interesting stuff from kernel memory.
462 if (nlistf != NULL || memf != NULL)
467 if (kread(0, 0, 0) == 0)
468 mbpr(nl[N_MBSTAT].n_value,
469 nl[N_MBTYPES].n_value,
470 nl[N_NMBCLUSTERS].n_value,
471 nl[N_NMBUFS].n_value,
472 nl[N_NCPUS].n_value);
480 * Keep file descriptors open to avoid overhead
481 * of open/close on each call to get* routines.
487 * This does not make sense any more with DNS being default over
488 * the files. Doing a setXXXXent(1) causes a tcp connection to be
489 * used for the queries, which is slower.
492 if (iflag && !sflag) {
494 intpr(interval, nl[N_IFNET].n_value, NULL);
502 routepr(nl[N_RTREE].n_value);
508 if (af == AF_INET || af == AF_UNSPEC)
509 mrt_stats(nl[N_MRTSTAT].n_value);
511 if (af == AF_INET6 || af == AF_UNSPEC)
512 mrt6_stats(nl[N_MRT6STAT].n_value);
515 if (af == AF_INET || af == AF_UNSPEC)
516 mroutepr(nl[N_MFCTABLE].n_value,
517 nl[N_VIFTABLE].n_value);
519 if (af == AF_INET6 || af == AF_UNSPEC)
520 mroute6pr(nl[N_MF6CTABLE].n_value,
521 nl[N_MIF6TABLE].n_value);
529 printproto(tp, tp->pr_name);
532 if (af == AF_INET || af == AF_UNSPEC)
533 for (tp = protox; tp->pr_name; tp++)
534 printproto(tp, tp->pr_name);
536 if (af == AF_INET6 || af == AF_UNSPEC)
537 for (tp = ip6protox; tp->pr_name; tp++)
538 printproto(tp, tp->pr_name);
541 if (af == PF_KEY || af == AF_UNSPEC)
542 for (tp = pfkeyprotox; tp->pr_name; tp++)
543 printproto(tp, tp->pr_name);
545 if (af == AF_IPX || af == AF_UNSPEC) {
547 for (tp = ipxprotox; tp->pr_name; tp++)
548 printproto(tp, tp->pr_name);
550 if (af == AF_NETGRAPH || af == AF_UNSPEC)
551 for (tp = netgraphprotox; tp->pr_name; tp++)
552 printproto(tp, tp->pr_name);
554 if (af == AF_ISO || af == AF_UNSPEC)
555 for (tp = isoprotox; tp->pr_name; tp++)
556 printproto(tp, tp->pr_name);
558 if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
564 * Print out protocol statistics or control blocks (per sflag).
565 * If the interface was not specifically requested, and the symbol
566 * is not in the namelist, ignore this one.
569 printproto(struct protox *tp, const char *name)
571 void (*pr)(u_long, const char *, int);
577 intpr(interval, nl[N_IFNET].n_value,
580 printf("%s: no per-interface stats routine\n",
588 printf("%s: no stats routine\n",
592 off = tp->pr_usesysctl ? tp->pr_usesysctl
593 : nl[tp->pr_sindex].n_value;
599 printf("%s: no PCB routine\n", tp->pr_name);
602 off = tp->pr_usesysctl ? tp->pr_usesysctl
603 : nl[tp->pr_index].n_value;
605 if (pr != NULL && (off || af != AF_UNSPEC))
606 (*pr)(off, name, af);
610 * Read kernel memory, return 0 on success.
613 kread(u_long addr, char *buf, int size)
619 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
621 if (kvm_nlist(kvmd, nl) < 0) {
623 errx(1, "%s: kvm_nlist: %s", nlistf,
626 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
629 if (nl[0].n_type == 0) {
631 errx(1, "%s: no namelist", nlistf);
633 errx(1, "no namelist");
636 warnx("kvm not available");
642 if (kvm_read(kvmd, addr, buf, size) != size) {
643 warnx("%s", kvm_geterr(kvmd));
652 return (n != 1 ? "s" : "");
658 return (n != 1 ? "es" : "");
662 * Find the protox for the given "well-known" name.
664 static struct protox *
665 knownname(char *name)
667 struct protox **tpp, *tp;
669 for (tpp = protoprotox; *tpp; tpp++)
670 for (tp = *tpp; tp->pr_name; tp++)
671 if (strcmp(tp->pr_name, name) == 0)
677 * Find the protox corresponding to name.
679 static struct protox *
680 name2protox(char *name)
683 char **alias; /* alias from p->aliases */
687 * Try to find the name in the list of "well-known" names. If that
688 * fails, check if name is an alias for an Internet protocol.
690 if ((tp = knownname(name)) != NULL)
693 setprotoent(1); /* make protocol lookup cheaper */
694 while ((p = getprotoent()) != NULL) {
695 /* assert: name not same as p->name */
696 for (alias = p->p_aliases; *alias; alias++)
697 if (strcmp(name, *alias) == 0) {
699 return (knownname(p->p_name));
709 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
710 "usage: netstat [-AaLnPSW] [-c cpu] [-f protocol_family | -p protocol]\n"
711 " [-M core] [-N system]",
712 " netstat -i | -I interface [-aBbdnt] [-f address_family]\n"
713 " [-M core] [-N system]",
714 " netstat -w wait [-I interface] [-d] [-M core] [-N system]",
715 " netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
716 " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
717 " [-M core] [-N system]",
718 " netstat -m [-M core] [-N system]",
719 " netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
720 " netstat -rs [-s] [-M core] [-N system]",
721 " netstat -g [-W] [-f address_family] [-M core] [-N system]",
722 " netstat -gs [-s] [-f address_family] [-M core] [-N system]");