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.6 2004/03/08 22:41:05 dillon 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[] = {
103 #define N_MFCTABLE 20
105 #define N_VIFTABLE 21
121 #define N_ICMP6STAT 29
123 #define N_IPSECSTAT 30
125 #define N_IPSEC6STAT 31
127 #define N_PIM6STAT 32
129 #define N_MRT6PROTO 33
131 #define N_MRT6STAT 34
133 #define N_MF6CTABLE 35
135 #define N_MIF6TABLE 36
137 #define N_PFKEYSTAT 37
143 #define N_NMBCLUSTERS 40
153 u_char pr_index; /* index into nlist of cb head */
154 u_char pr_sindex; /* index into nlist of stat block */
155 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
156 void (*pr_cblocks)(u_long, char *, int);
157 /* control blocks printing routine */
158 void (*pr_stats)(u_long, char *, int);
159 /* statistics printing routine */
160 void (*pr_istats)(char *); /* per/if statistics printing routine */
161 char *pr_name; /* well-known name */
162 int pr_usesysctl; /* true if we use sysctl, not kvm */
164 { -1, -1, 1, protopr,
165 tcp_stats, NULL, "tcp", IPPROTO_TCP },
166 { -1, -1, 1, protopr,
167 udp_stats, NULL, "udp", IPPROTO_UDP },
168 { -1, -1, 1, protopr,
169 NULL, NULL, "divert",IPPROTO_DIVERT },
170 { -1, -1, 1, protopr,
171 ip_stats, NULL, "ip", IPPROTO_RAW },
172 { -1, -1, 1, protopr,
173 icmp_stats, NULL, "icmp", IPPROTO_ICMP },
174 { -1, -1, 1, protopr,
175 igmp_stats, NULL, "igmp", IPPROTO_IGMP },
177 { -1, N_IPSECSTAT, 1, 0,
178 ipsec_stats, NULL, "ipsec", 0},
181 bdg_stats, NULL, "bdg", 1 /* bridging... */ },
187 struct protox ip6protox[] = {
188 { -1, -1, 1, protopr,
189 tcp_stats, NULL, "tcp", IPPROTO_TCP },
190 { -1, -1, 1, protopr,
191 udp_stats, NULL, "udp", IPPROTO_UDP },
192 { -1, N_IP6STAT, 1, protopr,
193 ip6_stats, ip6_ifstats, "ip6", IPPROTO_RAW },
194 { -1, N_ICMP6STAT, 1, protopr,
195 icmp6_stats, icmp6_ifstats, "icmp6",IPPROTO_ICMPV6 },
197 { -1, N_IPSEC6STAT, 1, 0,
198 ipsec_stats, NULL, "ipsec6",0 },
201 { -1, N_PIM6STAT, 1, 0,
202 pim6_stats, NULL, "pim6", 0 },
205 rip6_stats, NULL, "rip6", 0 },
207 bdg_stats, NULL, "bdg", 1 /* bridging... */ },
208 { -1, -1, 1, protopr,
209 pim_stats, NULL, "pim", IPPROTO_PIM },
216 struct protox pfkeyprotox[] = {
217 { -1, N_PFKEYSTAT, 1, 0,
218 pfkey_stats, NULL, "pfkey", 0 },
224 struct protox atalkprotox[] = {
225 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
226 ddp_stats, NULL, "ddp" },
231 struct protox netgraphprotox[] = {
232 { N_NGSOCKS, -1, 1, netgraphprotopr,
233 NULL, NULL, "ctrl" },
234 { N_NGSOCKS, -1, 1, netgraphprotopr,
235 NULL, NULL, "data" },
240 struct protox ipxprotox[] = {
241 { N_IPX, N_IPXSTAT, 1, ipxprotopr,
242 ipx_stats, NULL, "ipx", 0 },
243 { N_IPX, N_SPXSTAT, 1, ipxprotopr,
244 spx_stats, NULL, "spx", 0 },
250 struct protox nsprotox[] = {
251 { N_IDP, N_IDPSTAT, 1, nsprotopr,
252 idp_stats, NULL, "idp" },
253 { N_IDP, N_SPPSTAT, 1, nsprotopr,
254 spp_stats, NULL, "spp" },
256 nserr_stats, NULL, "ns_err" },
263 struct protox isoprotox[] = {
264 { ISO_TP, N_TPSTAT, 1, iso_protopr,
265 tp_stats, NULL, "tp" },
266 { N_CLTP, N_CLTPSTAT, 1, iso_protopr,
267 cltp_stats, NULL, "cltp" },
268 { -1, N_CLNPSTAT, 1, 0,
269 clnp_stats, NULL, "clnp"},
270 { -1, N_ESISSTAT, 1, 0,
271 esis_stats, NULL, "esis"},
277 struct protox *protoprotox[] = {
285 ipxprotox, atalkprotox,
294 static void printproto (struct protox *, char *);
295 static void usage (void);
296 static struct protox *name2protox (char *);
297 static struct protox *knownname (char *);
300 static char *nlistf = NULL, *memf = NULL;
302 int Aflag; /* show addresses of protocol control block */
303 int aflag; /* show all sockets (including servers) */
304 int bflag; /* show i/f total bytes in/out */
305 int dflag; /* show i/f dropped packets */
306 int gflag; /* show group (multicast) routing or stats */
307 int iflag; /* show interfaces */
308 int Lflag; /* show size of listen queues */
309 int mflag; /* show memory stats */
310 int numeric_addr; /* show addresses numerically */
311 int numeric_port; /* show ports numerically */
312 static int pflag; /* show given protocol */
313 int rflag; /* show routing tables (or routing stats) */
314 int sflag; /* show protocol statistics */
315 int tflag; /* show i/f watchdog timers */
316 int Bflag; /* show buffer limit instead of buffer use */
317 int Wflag; /* wide display */
318 int zflag; /* zero stats */
320 int interval; /* repeat interval for i/f stats */
322 char *interface; /* desired i/f for stats, or NULL for all i/fs */
323 int unit; /* unit number for above */
325 int af; /* address family */
328 main(int argc, char **argv)
330 register struct protox *tp = NULL; /* for printing cblocks & stats */
335 while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rSsBtuWw:z")) != -1)
351 if (strcmp(optarg, "ns") == 0)
355 if (strcmp(optarg, "ipx") == 0)
357 else if (strcmp(optarg, "inet") == 0)
360 else if (strcmp(optarg, "inet6") == 0)
364 else if (strcmp(optarg, "pfkey") == 0)
367 else if (strcmp(optarg, "unix") == 0)
369 else if (strcmp(optarg, "atalk") == 0)
371 else if (strcmp(optarg, "ng") == 0
372 || strcmp(optarg, "netgraph") == 0)
375 else if (strcmp(optarg, "iso") == 0)
378 else if (strcmp(optarg, "link") == 0)
381 errx(1, "%s: unknown address family", optarg);
391 for (cp = interface = optarg; isalpha(*cp); cp++)
412 numeric_addr = numeric_port = 1;
415 if ((tp = name2protox(optarg)) == NULL) {
417 "%s: unknown or uninstrumented protocol",
445 interval = atoi(optarg);
458 #define BACKWARD_COMPATIBILITY
459 #ifdef BACKWARD_COMPATIBILITY
461 if (isdigit(**argv)) {
462 interval = atoi(*argv);
477 * Discard setgid privileges if not the running kernel so that bad
478 * guys can't print interesting stuff from kernel memory.
480 if (nlistf != NULL || memf != NULL)
485 if (kread(0, 0, 0) == 0)
486 mbpr(nl[N_MBSTAT].n_value,
487 nl[N_MBTYPES].n_value,
488 nl[N_NMBCLUSTERS].n_value,
489 nl[N_NMBUFS].n_value);
496 * Keep file descriptors open to avoid overhead
497 * of open/close on each call to get* routines.
503 * This does not make sense any more with DNS being default over
504 * the files. Doing a setXXXXent(1) causes a tcp connection to be
505 * used for the queries, which is slower.
508 if (iflag && !sflag) {
510 intpr(interval, nl[N_IFNET].n_value, NULL);
516 rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
518 routepr(nl[N_RTREE].n_value);
524 if (af == AF_INET || af == AF_UNSPEC)
525 mrt_stats(nl[N_MRTSTAT].n_value);
527 if (af == AF_INET6 || af == AF_UNSPEC)
528 mrt6_stats(nl[N_MRT6STAT].n_value);
531 if (af == AF_INET || af == AF_UNSPEC)
532 mroutepr(nl[N_MFCTABLE].n_value,
533 nl[N_VIFTABLE].n_value);
535 if (af == AF_INET6 || af == AF_UNSPEC)
536 mroute6pr(nl[N_MF6CTABLE].n_value,
537 nl[N_MIF6TABLE].n_value);
545 printproto(tp, tp->pr_name);
548 if (af == AF_INET || af == AF_UNSPEC)
549 for (tp = protox; tp->pr_name; tp++)
550 printproto(tp, tp->pr_name);
552 if (af == AF_INET6 || af == AF_UNSPEC)
553 for (tp = ip6protox; tp->pr_name; tp++)
554 printproto(tp, tp->pr_name);
557 if (af == PF_KEY || af == AF_UNSPEC)
558 for (tp = pfkeyprotox; tp->pr_name; tp++)
559 printproto(tp, tp->pr_name);
561 if (af == AF_IPX || af == AF_UNSPEC) {
563 for (tp = ipxprotox; tp->pr_name; tp++)
564 printproto(tp, tp->pr_name);
566 if (af == AF_APPLETALK || af == AF_UNSPEC)
567 for (tp = atalkprotox; tp->pr_name; tp++)
568 printproto(tp, tp->pr_name);
569 if (af == AF_NETGRAPH || af == AF_UNSPEC)
570 for (tp = netgraphprotox; tp->pr_name; tp++)
571 printproto(tp, tp->pr_name);
573 if (af == AF_NS || af == AF_UNSPEC)
574 for (tp = nsprotox; tp->pr_name; tp++)
575 printproto(tp, tp->pr_name);
578 if (af == AF_ISO || af == AF_UNSPEC)
579 for (tp = isoprotox; tp->pr_name; tp++)
580 printproto(tp, tp->pr_name);
582 if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
588 * Print out protocol statistics or control blocks (per sflag).
589 * If the interface was not specifically requested, and the symbol
590 * is not in the namelist, ignore this one.
593 printproto(register struct protox *tp, char *name)
595 void (*pr)(u_long, char *, int);
601 intpr(interval, nl[N_IFNET].n_value,
604 printf("%s: no per-interface stats routine\n",
612 printf("%s: no stats routine\n",
616 off = tp->pr_usesysctl ? tp->pr_usesysctl
617 : nl[tp->pr_sindex].n_value;
623 printf("%s: no PCB routine\n", tp->pr_name);
626 off = tp->pr_usesysctl ? tp->pr_usesysctl
627 : nl[tp->pr_index].n_value;
629 if (pr != NULL && (off || af != AF_UNSPEC))
630 (*pr)(off, name, af);
634 * Read kernel memory, return 0 on success.
637 kread(u_long addr, char *buf, int size)
643 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
645 if (kvm_nlist(kvmd, nl) < 0) {
647 errx(1, "%s: kvm_nlist: %s", nlistf,
650 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
653 if (nl[0].n_type == 0) {
655 errx(1, "%s: no namelist", nlistf);
657 errx(1, "no namelist");
660 warnx("kvm not available");
666 if (kvm_read(kvmd, addr, buf, size) != size) {
667 warnx("%s", kvm_geterr(kvmd));
676 return (n != 1 ? "s" : "");
682 return (n != 1 ? "es" : "");
686 * Find the protox for the given "well-known" name.
688 static struct protox *
689 knownname(char *name)
691 struct protox **tpp, *tp;
693 for (tpp = protoprotox; *tpp; tpp++)
694 for (tp = *tpp; tp->pr_name; tp++)
695 if (strcmp(tp->pr_name, name) == 0)
701 * Find the protox corresponding to name.
703 static struct protox *
704 name2protox(char *name)
707 char **alias; /* alias from p->aliases */
711 * Try to find the name in the list of "well-known" names. If that
712 * fails, check if name is an alias for an Internet protocol.
714 if ((tp = knownname(name)) != NULL)
717 setprotoent(1); /* make protocol lookup cheaper */
718 while ((p = getprotoent()) != NULL) {
719 /* assert: name not same as p->name */
720 for (alias = p->p_aliases; *alias; alias++)
721 if (strcmp(name, *alias) == 0) {
723 return (knownname(p->p_name));
733 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
734 "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
735 " [-M core] [-N system]",
736 " netstat -i | -I interface [-abdnt] [-f address_family]\n"
737 " [-M core] [-N system]",
738 " netstat -w wait [-I interface] [-d] [-M core] [-N system]",
739 " netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
740 " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
741 " [-M core] [-N system]",
742 " netstat -m [-M core] [-N system]",
743 " netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
744 " netstat -rs [-s] [-M core] [-N system]",
745 " netstat -g [-W] [-f address_family] [-M core] [-N system]",
746 " netstat -gs [-s] [-f address_family] [-M core] [-N system]");