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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1983, 1988, 1993 Regents of the University of California. All rights reserved.
30 * @(#)main.c 8.4 (Berkeley) 3/1/94
31 * $FreeBSD: src/usr.bin/netstat/main.c,v 1.34.2.12 2001/09/17 15:17:46 ru Exp $
34 #include <sys/param.h>
36 #include <sys/protosw.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
40 #include <netinet/in.h>
42 #include <netgraph/socket/ng_socket.h>
58 static struct nlist nl
[] = {
60 { .n_name
= "_ifnet" },
62 { .n_name
= "_imp_softc" },
64 { .n_name
= "_rtstat" },
66 { .n_name
= "_localsw" },
68 { .n_name
= "_nspcb"},
70 { .n_name
= "_idpstat"},
72 { .n_name
= "_spp_istat"},
74 { .n_name
= "_ns_errstat"},
76 { .n_name
= "_clnp_stat"},
78 { .n_name
= "_tp_inpcb" },
80 { .n_name
= "_tp_refinfo" },
82 { .n_name
= "_tp_stat" },
84 { .n_name
= "_esis_stat"},
88 { .n_name
= "_rt_tables"},
92 { .n_name
= "_cltpstat"},
94 { .n_name
= "_nfile" },
96 { .n_name
= "_file" },
98 { .n_name
= "_mrtstat" },
100 { .n_name
= "_mfctable" },
101 #define N_VIFTABLE 21
102 { .n_name
= "_viftable" },
104 { .n_name
= "_ngsocklist"},
106 { .n_name
= "_ip6stat" },
107 #define N_ICMP6STAT 24
108 { .n_name
= "_icmp6stat" },
109 #define N_PIM6STAT 25
110 { .n_name
= "_pim6stat" },
111 #define N_MRT6PROTO 26
112 { .n_name
= "_ip6_mrtproto" },
113 #define N_MRT6STAT 27
114 { .n_name
= "_mrt6stat" },
115 #define N_MF6CTABLE 28
116 { .n_name
= "_mf6ctable" },
117 #define N_MIF6TABLE 29
118 { .n_name
= "_mif6table" },
120 { .n_name
= "_mbstat" },
122 { .n_name
= "_mbtypes" },
123 #define N_NMBCLUSTERS 32
124 { .n_name
= "_nmbclusters" },
126 { .n_name
= "_nmbufs" },
128 { .n_name
= "_rttrash" },
130 { .n_name
= "_ncpus" },
131 #define N_CARPSTAT 36
132 { .n_name
= "_carpstats" },
133 #define N_NMBJCLUSTERS 37
134 { .n_name
= "_nmbjclusters" },
139 u_char pr_index
; /* index into nlist of cb head */
140 u_char pr_sindex
; /* index into nlist of stat block */
141 u_char pr_wanted
; /* 1 if wanted, 0 otherwise */
142 void (*pr_cblocks
)(u_long
, const char *, int);
143 /* control blocks printing routine */
144 void (*pr_stats
)(u_long
, const char *, int);
145 /* statistics printing routine */
146 void (*pr_istats
)(char *); /* per/if statistics printing routine */
147 const char *pr_name
; /* well-known name */
148 u_int pr_usesysctl
; /* true if we use sysctl, not kvm */
150 { -1, -1, 1, protopr
,
151 tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
152 { -1, -1, 1, protopr
,
153 udp_stats
, NULL
, "udp", IPPROTO_UDP
},
154 { -1, -1, 1, protopr
,
155 NULL
, NULL
, "divert",IPPROTO_DIVERT
},
156 { -1, -1, 1, protopr
,
157 ip_stats
, NULL
, "ip", IPPROTO_RAW
},
158 { -1, -1, 1, protopr
,
159 icmp_stats
, NULL
, "icmp", IPPROTO_ICMP
},
160 { -1, -1, 1, protopr
,
161 igmp_stats
, NULL
, "igmp", IPPROTO_IGMP
},
162 { -1, N_CARPSTAT
, 1, 0,
163 carp_stats
, NULL
, "carp", 0},
169 struct protox ip6protox
[] = {
170 { -1, -1, 1, protopr
,
171 tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
172 { -1, -1, 1, protopr
,
173 udp_stats
, NULL
, "udp", IPPROTO_UDP
},
174 { -1, N_IP6STAT
, 1, protopr
,
175 ip6_stats
, ip6_ifstats
, "ip6", IPPROTO_RAW
},
176 { -1, N_ICMP6STAT
, 1, protopr
,
177 icmp6_stats
, icmp6_ifstats
, "icmp6",IPPROTO_ICMPV6
},
179 { -1, N_PIM6STAT
, 1, 0,
180 pim6_stats
, NULL
, "pim6", 0 },
183 rip6_stats
, NULL
, "rip6", 0 },
184 { -1, -1, 1, protopr
,
185 pim_stats
, NULL
, "pim", IPPROTO_PIM
},
191 struct protox netgraphprotox
[] = {
192 { N_NGSOCKS
, -1, 1, netgraphprotopr
,
193 NULL
, NULL
, "ctrl", 0 },
194 { N_NGSOCKS
, -1, 1, netgraphprotopr
,
195 NULL
, NULL
, "data", 0 },
200 struct protox
*protoprotox
[] = {
207 static void printproto (struct protox
*, const char *, u_long
);
208 static void usage (void);
209 static struct protox
*name2protox (char *);
210 static struct protox
*knownname (char *);
213 static char *nlistf
= NULL
, *memf
= NULL
;
215 int Aflag
; /* show addresses of protocol control block */
216 int aflag
; /* show all sockets (including servers) */
217 int bflag
; /* show i/f total bytes in/out */
218 int cpuflag
= -1; /* dump route table from specific cpu */
219 int dflag
; /* show i/f dropped packets */
220 int gflag
; /* show group (multicast) routing or stats */
221 int hflag
; /* show counters in human readable format */
222 int iflag
; /* show interfaces */
223 int Lflag
; /* show size of listen queues */
224 int mflag
; /* show memory stats */
225 int Pflag
; /* show more protocol info (go past 80 columns) */
226 int numeric_addr
; /* show addresses numerically */
227 int numeric_port
; /* show ports numerically */
228 static int pflag
; /* show given protocol */
229 int rflag
; /* show routing tables (or routing stats) */
230 int sflag
; /* show protocol statistics */
231 int tflag
; /* show i/f watchdog timers */
232 int Bflag
; /* show buffer limit instead of buffer use */
233 int Wflag
; /* wide display */
234 int zflag
; /* zero stats */
236 int interval
; /* repeat interval for i/f stats */
238 char *interface
; /* desired i/f for stats, or NULL for all i/fs */
239 int unit
; /* unit number for above */
241 int af
; /* address family */
244 main(int argc
, char **argv
)
246 struct protox
*tp
= NULL
; /* for printing cblocks & stats */
253 while ((ch
= getopt(argc
, argv
, "Aabc:df:ghI:iLlM:mN:nPp:rSsBtuWw:z")) != -1)
266 sysctlbyname("net.netisr.ncpus", &n
, &nsz
, NULL
, 0);
267 cpuflag
= strtol(optarg
, NULL
, 0);
268 if (cpuflag
< 0 || cpuflag
>= n
) {
269 errx(1, "cpu%d does not have network data",
277 if (strcmp(optarg
, "inet") == 0)
280 else if (strcmp(optarg
, "inet6") == 0)
283 else if (strcmp(optarg
, "unix") == 0)
285 else if (strcmp(optarg
, "ng") == 0
286 || strcmp(optarg
, "netgraph") == 0)
288 else if (strcmp(optarg
, "link") == 0)
290 else if (strcmp(optarg
, "mpls") == 0)
293 errx(1, "%s: unknown address family", optarg
);
306 for (cp
= interface
= optarg
; isalpha(*cp
); cp
++)
327 numeric_addr
= numeric_port
= 1;
333 if ((tp
= name2protox(optarg
)) == NULL
) {
335 "%s: unknown or uninstrumented protocol",
363 interval
= atoi(optarg
);
376 #define BACKWARD_COMPATIBILITY
377 #ifdef BACKWARD_COMPATIBILITY
379 if (isdigit(**argv
)) {
380 interval
= atoi(*argv
);
395 * Discard setgid privileges if not the running kernel so that bad
396 * guys can't print interesting stuff from kernel memory.
398 if (nlistf
!= NULL
|| memf
!= NULL
)
403 if (kread(0, 0, 0) == 0)
404 mbpr(nl
[N_MBSTAT
].n_value
,
405 nl
[N_MBTYPES
].n_value
,
406 nl
[N_NMBCLUSTERS
].n_value
,
407 nl
[N_NMBJCLUSTERS
].n_value
,
408 nl
[N_NMBUFS
].n_value
,
409 nl
[N_NCPUS
].n_value
);
411 mbpr(0, 0, 0, 0, 0, 0);
417 * Keep file descriptors open to avoid overhead
418 * of open/close on each call to get* routines.
424 * This does not make sense any more with DNS being default over
425 * the files. Doing a setXXXXent(1) causes a tcp connection to be
426 * used for the queries, which is slower.
429 if (iflag
&& !sflag
) {
431 intpr(interval
, nl
[N_IFNET
].n_value
, NULL
, nl
[N_NCPUS
].n_value
);
439 routepr(nl
[N_RTREE
].n_value
);
445 if (af
== AF_INET
|| af
== AF_UNSPEC
)
446 mrt_stats(nl
[N_MRTSTAT
].n_value
);
448 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
449 mrt6_stats(nl
[N_MRT6STAT
].n_value
);
452 if (af
== AF_INET
|| af
== AF_UNSPEC
)
453 mroutepr(nl
[N_MFCTABLE
].n_value
,
454 nl
[N_VIFTABLE
].n_value
);
456 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
457 mroute6pr(nl
[N_MF6CTABLE
].n_value
,
458 nl
[N_MIF6TABLE
].n_value
);
466 printproto(tp
, tp
->pr_name
, nl
[N_NCPUS
].n_value
);
469 if (af
== AF_INET
|| af
== AF_UNSPEC
)
470 for (tp
= protox
; tp
->pr_name
; tp
++)
471 printproto(tp
, tp
->pr_name
, nl
[N_NCPUS
].n_value
);
473 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
474 for (tp
= ip6protox
; tp
->pr_name
; tp
++)
475 printproto(tp
, tp
->pr_name
, nl
[N_NCPUS
].n_value
);
477 if (af
== AF_NETGRAPH
|| af
== AF_UNSPEC
)
478 for (tp
= netgraphprotox
; tp
->pr_name
; tp
++)
479 printproto(tp
, tp
->pr_name
, nl
[N_NCPUS
].n_value
);
480 if ((af
== AF_UNIX
|| af
== AF_UNSPEC
) && !Lflag
&& !sflag
)
486 * Print out protocol statistics or control blocks (per sflag).
487 * If the interface was not specifically requested, and the symbol
488 * is not in the namelist, ignore this one.
491 printproto(struct protox
*tp
, const char *name
, u_long ncpusaddr
)
493 void (*pr
)(u_long
, const char *, int);
499 intpr(interval
, nl
[N_IFNET
].n_value
,
500 tp
->pr_istats
, ncpusaddr
);
502 printf("%s: no per-interface stats routine\n",
510 printf("%s: no stats routine\n",
514 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
515 : nl
[tp
->pr_sindex
].n_value
;
521 printf("%s: no PCB routine\n", tp
->pr_name
);
524 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
525 : nl
[tp
->pr_index
].n_value
;
527 if (pr
!= NULL
&& (off
|| af
!= AF_UNSPEC
))
528 (*pr
)(off
, name
, af
);
532 * Read kernel memory, return 0 on success.
535 kread(u_long addr
, char *buf
, int size
)
541 kvmd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, buf
);
543 if (kvm_nlist(kvmd
, nl
) < 0) {
545 errx(1, "%s: kvm_nlist: %s", nlistf
,
548 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd
));
551 if (nl
[0].n_type
== 0) {
553 errx(1, "%s: no namelist", nlistf
);
555 errx(1, "no namelist");
558 warnx("kvm not available");
564 if (kvm_read(kvmd
, addr
, buf
, size
) != size
) {
565 warnx("%s", kvm_geterr(kvmd
));
574 return (n
!= 1 ? "s" : "");
580 return (n
!= 1 ? "es" : "");
584 * Find the protox for the given "well-known" name.
586 static struct protox
*
587 knownname(char *name
)
589 struct protox
**tpp
, *tp
;
591 for (tpp
= protoprotox
; *tpp
; tpp
++)
592 for (tp
= *tpp
; tp
->pr_name
; tp
++)
593 if (strcmp(tp
->pr_name
, name
) == 0)
599 * Find the protox corresponding to name.
601 static struct protox
*
602 name2protox(char *name
)
605 char **alias
; /* alias from p->aliases */
609 * Try to find the name in the list of "well-known" names. If that
610 * fails, check if name is an alias for an Internet protocol.
612 if ((tp
= knownname(name
)) != NULL
)
615 setprotoent(1); /* make protocol lookup cheaper */
616 while ((p
= getprotoent()) != NULL
) {
617 /* assert: name not same as p->name */
618 for (alias
= p
->p_aliases
; *alias
; alias
++)
619 if (strcmp(name
, *alias
) == 0) {
621 return (knownname(p
->p_name
));
631 (void)fprintf(stderr
, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
632 "usage: netstat [-AaLnPSW] [-c cpu] [-f protocol_family | -p protocol]\n"
633 " [-M core] [-N system]",
634 " netstat -i | -I interface [-aBbdhnt] [-f address_family]\n"
635 " [-M core] [-N system]",
636 " netstat -w wait [-I interface] [-dh] [-M core] [-N system]",
637 " netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
638 " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
639 " [-M core] [-N system]",
640 " netstat -m [-M core] [-N system]",
641 " netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
642 " netstat -rs [-s] [-M core] [-N system]",
643 " netstat -g [-W] [-f address_family] [-M core] [-N system]",
644 " netstat -gs [-s] [-f address_family] [-M core] [-N system]");