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
35 char const copyright[] =
36 "@(#) Copyright (c) 1983, 1988, 1993\n\
37 Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94";
44 static const char rcsid[] =
48 #include <sys/param.h>
50 #include <sys/protosw.h>
51 #include <sys/socket.h>
53 #include <netinet/in.h>
55 #include <netgraph/ng_socket.h>
71 static struct nlist nl[] = {
104 #define N_CLTPSTAT 16
112 #define N_MFCTABLE 20
114 #define N_VIFTABLE 21
130 #define N_ICMP6STAT 29
132 #define N_IPSECSTAT 30
134 #define N_IPSEC6STAT 31
136 #define N_PIM6STAT 32
138 #define N_MRT6PROTO 33
140 #define N_MRT6STAT 34
142 #define N_MF6CTABLE 35
144 #define N_MIF6TABLE 36
146 #define N_PFKEYSTAT 37
152 #define N_NMBCLUSTERS 40
172 u_char pr_index; /* index into nlist of cb head */
173 u_char pr_sindex; /* index into nlist of stat block */
174 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
175 void (*pr_cblocks)(u_long, const char *, int);
176 /* control blocks printing routine */
177 void (*pr_stats)(u_long, const char *, int);
178 /* statistics printing routine */
179 void (*pr_istats)(char *); /* per/if statistics printing routine */
180 const char *pr_name; /* well-known name */
181 u_long pr_usesysctl; /* true if we use sysctl, not kvm */
183 { -1, -1, 1, protopr,
184 tcp_stats, NULL, "tcp", IPPROTO_TCP },
185 { -1, -1, 1, protopr,
186 udp_stats, NULL, "udp", IPPROTO_UDP },
187 { -1, -1, 1, protopr,
188 NULL, NULL, "divert",IPPROTO_DIVERT },
189 { -1, -1, 1, protopr,
190 ip_stats, NULL, "ip", IPPROTO_RAW },
191 { -1, -1, 1, protopr,
192 icmp_stats, NULL, "icmp", IPPROTO_ICMP },
193 { -1, -1, 1, protopr,
194 igmp_stats, NULL, "igmp", IPPROTO_IGMP },
196 { -1, N_IPSECSTAT, 1, 0,
197 ipsec_stats, NULL, "ipsec", 0},
200 bdg_stats, NULL, "bdg", 1 /* bridging... */ },
206 struct protox ip6protox[] = {
207 { -1, -1, 1, protopr,
208 tcp_stats, NULL, "tcp", IPPROTO_TCP },
209 { -1, -1, 1, protopr,
210 udp_stats, NULL, "udp", IPPROTO_UDP },
211 { -1, N_IP6STAT, 1, protopr,
212 ip6_stats, ip6_ifstats, "ip6", IPPROTO_RAW },
213 { -1, N_ICMP6STAT, 1, protopr,
214 icmp6_stats, icmp6_ifstats, "icmp6",IPPROTO_ICMPV6 },
216 { -1, N_IPSEC6STAT, 1, 0,
217 ipsec_stats, NULL, "ipsec6",0 },
220 { -1, N_PIM6STAT, 1, 0,
221 pim6_stats, NULL, "pim6", 0 },
224 rip6_stats, NULL, "rip6", 0 },
226 bdg_stats, NULL, "bdg", 1 /* bridging... */ },
233 struct protox pfkeyprotox[] = {
234 { -1, N_PFKEYSTAT, 1, 0,
235 pfkey_stats, NULL, "pfkey", 0 },
241 struct protox atalkprotox[] = {
242 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
243 ddp_stats, NULL, "ddp", 0 },
248 struct protox netgraphprotox[] = {
249 { N_NGSOCKS, -1, 1, netgraphprotopr,
250 NULL, NULL, "ctrl", 0 },
251 { N_NGSOCKS, -1, 1, netgraphprotopr,
252 NULL, NULL, "data", 0 },
257 struct protox ipxprotox[] = {
258 { N_IPX, N_IPXSTAT, 1, ipxprotopr,
259 ipx_stats, NULL, "ipx", 0 },
260 { N_IPX, N_SPXSTAT, 1, ipxprotopr,
261 spx_stats, NULL, "spx", 0 },
267 struct protox nsprotox[] = {
268 { N_IDP, N_IDPSTAT, 1, nsprotopr,
269 idp_stats, NULL, "idp" },
270 { N_IDP, N_SPPSTAT, 1, nsprotopr,
271 spp_stats, NULL, "spp" },
273 nserr_stats, NULL, "ns_err" },
280 struct protox isoprotox[] = {
281 { ISO_TP, N_TPSTAT, 1, iso_protopr,
282 tp_stats, NULL, "tp" },
283 { N_CLTP, N_CLTPSTAT, 1, iso_protopr,
284 cltp_stats, NULL, "cltp" },
285 { -1, N_CLNPSTAT, 1, 0,
286 clnp_stats, NULL, "clnp"},
287 { -1, N_ESISSTAT, 1, 0,
288 esis_stats, NULL, "esis"},
294 struct protox *protoprotox[] = {
302 ipxprotox, atalkprotox,
311 static void printproto (struct protox *, const char *);
312 static void usage (void);
313 static struct protox *name2protox (char *);
314 static struct protox *knownname (char *);
317 static char *nlistf = NULL, *memf = NULL;
319 int Aflag; /* show addresses of protocol control block */
320 int aflag; /* show all sockets (including servers) */
321 int bflag; /* show i/f total bytes in/out */
322 int dflag; /* show i/f dropped packets */
323 int gflag; /* show group (multicast) routing or stats */
324 int iflag; /* show interfaces */
325 int Lflag; /* show size of listen queues */
326 int mflag; /* show memory stats */
327 int numeric_addr; /* show addresses numerically */
328 int numeric_port; /* show ports numerically */
329 static int pflag; /* show given protocol */
330 int rflag; /* show routing tables (or routing stats) */
331 int sflag; /* show protocol statistics */
332 int tflag; /* show i/f watchdog timers */
333 int Wflag; /* wide display */
334 int zflag; /* zero stats */
336 int interval; /* repeat interval for i/f stats */
338 char *interface; /* desired i/f for stats, or NULL for all i/fs */
339 int unit; /* unit number for above */
341 int af; /* address family */
344 main(int argc, char *argv[])
346 struct protox *tp = NULL; /* for printing cblocks & stats */
351 while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rSstuWw:z")) != -1)
367 if (strcmp(optarg, "ns") == 0)
371 if (strcmp(optarg, "ipx") == 0)
373 else if (strcmp(optarg, "inet") == 0)
376 else if (strcmp(optarg, "inet6") == 0)
380 else if (strcmp(optarg, "pfkey") == 0)
383 else if (strcmp(optarg, "unix") == 0)
385 else if (strcmp(optarg, "atalk") == 0)
387 else if (strcmp(optarg, "ng") == 0
388 || strcmp(optarg, "netgraph") == 0)
391 else if (strcmp(optarg, "iso") == 0)
394 else if (strcmp(optarg, "link") == 0)
397 errx(1, "%s: unknown address family", optarg);
407 for (cp = interface = optarg; isalpha(*cp); cp++)
428 numeric_addr = numeric_port = 1;
431 if ((tp = name2protox(optarg)) == NULL) {
433 "%s: unknown or uninstrumented protocol",
458 interval = atoi(optarg);
471 #define BACKWARD_COMPATIBILITY
472 #ifdef BACKWARD_COMPATIBILITY
474 if (isdigit(**argv)) {
475 interval = atoi(*argv);
490 * Discard setgid privileges if not the running kernel so that bad
491 * guys can't print interesting stuff from kernel memory.
493 if (nlistf != NULL || memf != NULL)
498 if (kread(0, 0, 0) == 0)
499 mbpr(nl[N_MBSTAT].n_value,
500 nl[N_MBTYPES].n_value,
501 nl[N_NMBCLUSTERS].n_value,
502 nl[N_NMBUFS].n_value,
506 nl[N_PAGESZ].n_value,
507 nl[N_MBPSTAT].n_value);
509 mbpr(0, 0, 0, 0, 0, 0, 0, 0, 0);
514 * Keep file descriptors open to avoid overhead
515 * of open/close on each call to get* routines.
521 * This does not make sense any more with DNS being default over
522 * the files. Doing a setXXXXent(1) causes a tcp connection to be
523 * used for the queries, which is slower.
526 if (iflag && !sflag) {
528 intpr(interval, nl[N_IFNET].n_value, NULL);
534 rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
536 routepr(nl[N_RTREE].n_value);
542 if (af == AF_INET || af == AF_UNSPEC)
543 mrt_stats(nl[N_MRTSTAT].n_value);
545 if (af == AF_INET6 || af == AF_UNSPEC)
546 mrt6_stats(nl[N_MRT6STAT].n_value);
549 if (af == AF_INET || af == AF_UNSPEC)
550 mroutepr(nl[N_MFCTABLE].n_value,
551 nl[N_VIFTABLE].n_value);
553 if (af == AF_INET6 || af == AF_UNSPEC)
554 mroute6pr(nl[N_MF6CTABLE].n_value,
555 nl[N_MIF6TABLE].n_value);
563 printproto(tp, tp->pr_name);
566 if (af == AF_INET || af == AF_UNSPEC)
567 for (tp = protox; tp->pr_name; tp++)
568 printproto(tp, tp->pr_name);
570 if (af == AF_INET6 || af == AF_UNSPEC)
571 for (tp = ip6protox; tp->pr_name; tp++)
572 printproto(tp, tp->pr_name);
575 if (af == PF_KEY || af == AF_UNSPEC)
576 for (tp = pfkeyprotox; tp->pr_name; tp++)
577 printproto(tp, tp->pr_name);
579 if (af == AF_IPX || af == AF_UNSPEC) {
581 for (tp = ipxprotox; tp->pr_name; tp++)
582 printproto(tp, tp->pr_name);
584 if (af == AF_APPLETALK || af == AF_UNSPEC)
585 for (tp = atalkprotox; tp->pr_name; tp++)
586 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);
591 if (af == AF_NS || af == AF_UNSPEC)
592 for (tp = nsprotox; tp->pr_name; tp++)
593 printproto(tp, tp->pr_name);
596 if (af == AF_ISO || af == AF_UNSPEC)
597 for (tp = isoprotox; tp->pr_name; tp++)
598 printproto(tp, tp->pr_name);
600 if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
606 * Print out protocol statistics or control blocks (per sflag).
607 * If the interface was not specifically requested, and the symbol
608 * is not in the namelist, ignore this one.
615 void (*pr)(u_long, const char *, int);
621 intpr(interval, nl[N_IFNET].n_value,
624 printf("%s: no per-interface stats routine\n",
632 printf("%s: no stats routine\n",
636 off = tp->pr_usesysctl ? tp->pr_usesysctl
637 : nl[tp->pr_sindex].n_value;
643 printf("%s: no PCB routine\n", tp->pr_name);
646 off = tp->pr_usesysctl ? tp->pr_usesysctl
647 : nl[tp->pr_index].n_value;
649 if (pr != NULL && (off || af != AF_UNSPEC))
650 (*pr)(off, name, af);
654 * Read kernel memory, return 0 on success.
657 kread(u_long addr, char *buf, int size)
663 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
666 if (kvm_nlist(kvmd, nl) < 0) {
668 errx(1, "%s: kvm_nlist: %s", nlistf,
671 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
674 if (nl[0].n_type == 0) {
676 errx(1, "%s: no namelist", nlistf);
678 errx(1, "no namelist");
681 warnx("kvm not available");
687 if (kvm_read(kvmd, addr, buf, size) != size) {
688 warnx("%s", kvm_geterr(kvmd));
697 return (n != 1 ? "s" : "");
703 return (n != 1 ? "es" : "");
707 * Find the protox for the given "well-known" name.
709 static struct protox *
710 knownname(char *name)
712 struct protox **tpp, *tp;
714 for (tpp = protoprotox; *tpp; tpp++)
715 for (tp = *tpp; tp->pr_name; tp++)
716 if (strcmp(tp->pr_name, name) == 0)
722 * Find the protox corresponding to name.
724 static struct protox *
725 name2protox(char *name)
728 char **alias; /* alias from p->aliases */
732 * Try to find the name in the list of "well-known" names. If that
733 * fails, check if name is an alias for an Internet protocol.
735 if ((tp = knownname(name)) != NULL)
738 setprotoent(1); /* make protocol lookup cheaper */
739 while ((p = getprotoent()) != NULL) {
740 /* assert: name not same as p->name */
741 for (alias = p->p_aliases; *alias; alias++)
742 if (strcmp(name, *alias) == 0) {
744 return (knownname(p->p_name));
754 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
755 "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
756 " [-M core] [-N system]",
757 " netstat -i | -I interface [-abdnt] [-f address_family]\n"
758 " [-M core] [-N system]",
759 " netstat -w wait [-I interface] [-d] [-M core] [-N system]",
760 " netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
761 " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
762 " [-M core] [-N system]",
763 " netstat -m [-M core] [-N system]",
764 " netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
765 " netstat -rs [-s] [-M core] [-N system]",
766 " netstat -g [-W] [-f address_family] [-M core] [-N system]",
767 " netstat -gs [-s] [-f address_family] [-M core] [-N system]");