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[] = {
98 #define N_ICMP6STAT 13
100 #define N_IPSECSTAT 14
102 #define N_IPSEC6STAT 15
104 #define N_PIM6STAT 16
106 #define N_MRT6STAT 17
108 #define N_MF6CTABLE 18
110 #define N_MIF6TABLE 19
112 #define N_PFKEYSTAT 20
118 #define N_NMBCLUSTERS 23
142 u_char pr_index; /* index into nlist of cb head */
143 u_char pr_sindex; /* index into nlist of stat block */
144 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
145 void (*pr_cblocks)(u_long, const char *, int);
146 /* control blocks printing routine */
147 void (*pr_stats)(u_long, const char *, int);
148 /* statistics printing routine */
149 void (*pr_istats)(char *); /* per/if statistics printing routine */
150 const char *pr_name; /* well-known name */
151 u_long pr_usesysctl; /* true if we use sysctl, not kvm */
153 { -1, -1, 1, protopr,
154 tcp_stats, NULL, "tcp", IPPROTO_TCP },
155 { -1, -1, 1, protopr,
156 udp_stats, NULL, "udp", IPPROTO_UDP },
157 { -1, -1, 1, protopr,
158 NULL, NULL, "divert",IPPROTO_DIVERT },
159 { -1, -1, 1, protopr,
160 ip_stats, NULL, "ip", IPPROTO_RAW },
161 { -1, -1, 1, protopr,
162 icmp_stats, NULL, "icmp", IPPROTO_ICMP },
163 { -1, -1, 1, protopr,
164 igmp_stats, NULL, "igmp", IPPROTO_IGMP },
166 { -1, N_IPSECSTAT, 1, 0,
167 ipsec_stats, NULL, "ipsec", 0},
170 bdg_stats, NULL, "bdg", 1 /* bridging... */ },
176 struct protox ip6protox[] = {
177 { -1, -1, 1, protopr,
178 tcp_stats, NULL, "tcp", IPPROTO_TCP },
179 { -1, -1, 1, protopr,
180 udp_stats, NULL, "udp", IPPROTO_UDP },
181 { -1, N_IP6STAT, 1, protopr,
182 ip6_stats, ip6_ifstats, "ip6", IPPROTO_RAW },
183 { -1, N_ICMP6STAT, 1, protopr,
184 icmp6_stats, icmp6_ifstats, "icmp6",IPPROTO_ICMPV6 },
186 { -1, N_IPSEC6STAT, 1, 0,
187 ipsec_stats, NULL, "ipsec6",0 },
190 { -1, N_PIM6STAT, 1, 0,
191 pim6_stats, NULL, "pim6", 0 },
194 rip6_stats, NULL, "rip6", 0 },
196 bdg_stats, NULL, "bdg", 1 /* bridging... */ },
203 struct protox pfkeyprotox[] = {
204 { -1, N_PFKEYSTAT, 1, 0,
205 pfkey_stats, NULL, "pfkey", 0 },
211 struct protox atalkprotox[] = {
212 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
213 ddp_stats, NULL, "ddp", 0 },
218 struct protox netgraphprotox[] = {
219 { N_NGSOCKS, -1, 1, netgraphprotopr,
220 NULL, NULL, "ctrl", 0 },
221 { N_NGSOCKS, -1, 1, netgraphprotopr,
222 NULL, NULL, "data", 0 },
227 struct protox ipxprotox[] = {
228 { N_IPX, N_IPXSTAT, 1, ipxprotopr,
229 ipx_stats, NULL, "ipx", 0 },
230 { N_IPX, N_SPXSTAT, 1, ipxprotopr,
231 spx_stats, NULL, "spx", 0 },
236 struct protox *protoprotox[] = {
244 ipxprotox, atalkprotox, NULL };
246 static void printproto (struct protox *, const char *);
247 static void usage (void);
248 static struct protox *name2protox (char *);
249 static struct protox *knownname (char *);
252 static char *nlistf = NULL, *memf = NULL;
254 int Aflag; /* show addresses of protocol control block */
255 int aflag; /* show all sockets (including servers) */
256 int bflag; /* show i/f total bytes in/out */
257 int dflag; /* show i/f dropped packets */
258 int gflag; /* show group (multicast) routing or stats */
259 int iflag; /* show interfaces */
260 int Lflag; /* show size of listen queues */
261 int mflag; /* show memory stats */
262 int numeric_addr; /* show addresses numerically */
263 int numeric_port; /* show ports numerically */
264 static int pflag; /* show given protocol */
265 int rflag; /* show routing tables (or routing stats) */
266 int sflag; /* show protocol statistics */
267 int tflag; /* show i/f watchdog timers */
268 int Wflag; /* wide display */
269 int zflag; /* zero stats */
271 int interval; /* repeat interval for i/f stats */
273 char *interface; /* desired i/f for stats, or NULL for all i/fs */
274 int unit; /* unit number for above */
276 int af; /* address family */
279 main(int argc, char *argv[])
281 struct protox *tp = NULL; /* for printing cblocks & stats */
286 while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rSstuWw:z")) != -1)
301 if (strcmp(optarg, "ipx") == 0)
303 else if (strcmp(optarg, "inet") == 0)
306 else if (strcmp(optarg, "inet6") == 0)
310 else if (strcmp(optarg, "pfkey") == 0)
313 else if (strcmp(optarg, "unix") == 0)
315 else if (strcmp(optarg, "atalk") == 0)
317 else if (strcmp(optarg, "ng") == 0
318 || strcmp(optarg, "netgraph") == 0)
320 else if (strcmp(optarg, "link") == 0)
323 errx(1, "%s: unknown address family", optarg);
333 for (cp = interface = optarg; isalpha(*cp); cp++)
354 numeric_addr = numeric_port = 1;
357 if ((tp = name2protox(optarg)) == NULL) {
359 "%s: unknown or uninstrumented protocol",
384 interval = atoi(optarg);
397 #define BACKWARD_COMPATIBILITY
398 #ifdef BACKWARD_COMPATIBILITY
400 if (isdigit(**argv)) {
401 interval = atoi(*argv);
416 * Discard setgid privileges if not the running kernel so that bad
417 * guys can't print interesting stuff from kernel memory.
419 if (nlistf != NULL || memf != NULL)
424 if (kread(0, 0, 0) == 0)
425 mbpr(nl[N_MBSTAT].n_value,
426 nl[N_MBTYPES].n_value,
427 nl[N_NMBCLUSTERS].n_value,
428 nl[N_NMBUFS].n_value,
434 nl[N_PAGESZ].n_value,
435 nl[N_MBPSTAT].n_value);
437 mbpr(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
442 * Keep file descriptors open to avoid overhead
443 * of open/close on each call to get* routines.
449 * This does not make sense any more with DNS being default over
450 * the files. Doing a setXXXXent(1) causes a tcp connection to be
451 * used for the queries, which is slower.
454 if (iflag && !sflag) {
456 intpr(interval, nl[N_IFNET].n_value, NULL);
462 rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
464 routepr(nl[N_RTREE].n_value);
470 if (af == AF_INET || af == AF_UNSPEC)
471 mrt_stats(nl[N_MRTSTAT].n_value);
473 if (af == AF_INET6 || af == AF_UNSPEC)
474 mrt6_stats(nl[N_MRT6STAT].n_value);
477 if (af == AF_INET || af == AF_UNSPEC)
478 mroutepr(nl[N_MFCTABLE].n_value,
479 nl[N_VIFTABLE].n_value);
481 if (af == AF_INET6 || af == AF_UNSPEC)
482 mroute6pr(nl[N_MF6CTABLE].n_value,
483 nl[N_MIF6TABLE].n_value);
491 printproto(tp, tp->pr_name);
494 if (af == AF_INET || af == AF_UNSPEC)
495 for (tp = protox; tp->pr_name; tp++)
496 printproto(tp, tp->pr_name);
498 if (af == AF_INET6 || af == AF_UNSPEC)
499 for (tp = ip6protox; tp->pr_name; tp++)
500 printproto(tp, tp->pr_name);
503 if (af == PF_KEY || af == AF_UNSPEC)
504 for (tp = pfkeyprotox; tp->pr_name; tp++)
505 printproto(tp, tp->pr_name);
507 if (af == AF_IPX || af == AF_UNSPEC) {
509 for (tp = ipxprotox; tp->pr_name; tp++)
510 printproto(tp, tp->pr_name);
512 if (af == AF_APPLETALK || af == AF_UNSPEC)
513 for (tp = atalkprotox; tp->pr_name; tp++)
514 printproto(tp, tp->pr_name);
515 if (af == AF_NETGRAPH || af == AF_UNSPEC)
516 for (tp = netgraphprotox; tp->pr_name; tp++)
517 printproto(tp, tp->pr_name);
518 if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
524 * Print out protocol statistics or control blocks (per sflag).
525 * If the interface was not specifically requested, and the symbol
526 * is not in the namelist, ignore this one.
533 void (*pr)(u_long, const char *, int);
539 intpr(interval, nl[N_IFNET].n_value,
542 printf("%s: no per-interface stats routine\n",
550 printf("%s: no stats routine\n",
554 off = tp->pr_usesysctl ? tp->pr_usesysctl
555 : nl[tp->pr_sindex].n_value;
561 printf("%s: no PCB routine\n", tp->pr_name);
564 off = tp->pr_usesysctl ? tp->pr_usesysctl
565 : nl[tp->pr_index].n_value;
567 if (pr != NULL && (off || af != AF_UNSPEC))
568 (*pr)(off, name, af);
572 * Read kernel memory, return 0 on success.
575 kread(u_long addr, char *buf, int size)
581 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
584 if (kvm_nlist(kvmd, nl) < 0) {
586 errx(1, "%s: kvm_nlist: %s", nlistf,
589 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
592 if (nl[0].n_type == 0) {
594 errx(1, "%s: no namelist", nlistf);
596 errx(1, "no namelist");
599 warnx("kvm not available");
605 if (kvm_read(kvmd, addr, buf, size) != size) {
606 warnx("%s", kvm_geterr(kvmd));
615 return (n != 1 ? "s" : "");
621 return (n != 1 ? "es" : "");
627 return (n != 1 ? "ies" : "y");
631 * Find the protox for the given "well-known" name.
633 static struct protox *
634 knownname(char *name)
636 struct protox **tpp, *tp;
638 for (tpp = protoprotox; *tpp; tpp++)
639 for (tp = *tpp; tp->pr_name; tp++)
640 if (strcmp(tp->pr_name, name) == 0)
646 * Find the protox corresponding to name.
648 static struct protox *
649 name2protox(char *name)
652 char **alias; /* alias from p->aliases */
656 * Try to find the name in the list of "well-known" names. If that
657 * fails, check if name is an alias for an Internet protocol.
659 if ((tp = knownname(name)) != NULL)
662 setprotoent(1); /* make protocol lookup cheaper */
663 while ((p = getprotoent()) != NULL) {
664 /* assert: name not same as p->name */
665 for (alias = p->p_aliases; *alias; alias++)
666 if (strcmp(name, *alias) == 0) {
668 return (knownname(p->p_name));
678 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
679 "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
680 " [-M core] [-N system]",
681 " netstat -i | -I interface [-abdnt] [-f address_family]\n"
682 " [-M core] [-N system]",
683 " netstat -w wait [-I interface] [-d] [-M core] [-N system]",
684 " netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
685 " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
686 " [-M core] [-N system]",
687 " netstat -m [-M core] [-N system]",
688 " netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
689 " netstat -rs [-s] [-M core] [-N system]",
690 " netstat -g [-W] [-f address_family] [-M core] [-N system]",
691 " netstat -gs [-s] [-f address_family] [-M core] [-N system]");