2008-01-02 23:26:11 +00:00
|
|
|
/*-
|
1994-05-27 12:33:43 +00:00
|
|
|
* Copyright (c) 1983, 1988, 1993
|
|
|
|
* Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-07-29 06:51:41 +00:00
|
|
|
char const copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1983, 1988, 1993\n\
|
|
|
|
Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
1997-07-29 06:51:41 +00:00
|
|
|
#if 0
|
2004-07-26 20:18:11 +00:00
|
|
|
#ifndef lint
|
1994-05-27 12:33:43 +00:00
|
|
|
static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94";
|
|
|
|
#endif /* not lint */
|
2004-07-26 20:18:11 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/socket.h>
|
2007-07-16 17:15:55 +00:00
|
|
|
#include <sys/socketvar.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2008-09-21 22:02:26 +00:00
|
|
|
#ifdef NETGRAPH
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <netgraph/ng_socket.h>
|
2008-09-21 22:02:26 +00:00
|
|
|
#endif
|
1999-10-21 09:06:11 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <ctype.h>
|
1997-07-29 06:51:41 +00:00
|
|
|
#include <err.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <errno.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <kvm.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <nlist.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <paths.h>
|
2006-07-28 16:09:19 +00:00
|
|
|
#include <stdint.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-02-21 23:47:20 +00:00
|
|
|
#include <stdbool.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "netstat.h"
|
2015-02-21 23:47:20 +00:00
|
|
|
#include <libxo/xo.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1998-08-08 08:13:04 +00:00
|
|
|
static struct nlist nl[] = {
|
2014-03-14 02:58:48 +00:00
|
|
|
#define N_RTSTAT 0
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_rtstat" },
|
2014-03-14 02:58:48 +00:00
|
|
|
#define N_RTREE 1
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_rt_tables"},
|
2014-03-14 02:58:48 +00:00
|
|
|
#define N_MRTSTAT 2
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_mrtstat" },
|
2014-03-14 02:58:48 +00:00
|
|
|
#define N_MFCHASHTBL 3
|
2009-03-19 01:43:03 +00:00
|
|
|
{ .n_name = "_mfchashtbl" },
|
2014-03-14 02:58:48 +00:00
|
|
|
#define N_VIFTABLE 4
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_viftable" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_NGSOCKS 5
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_ngsocklist"},
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_IP6STAT 6
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_ip6stat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_ICMP6STAT 7
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_icmp6stat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_IPSECSTAT 8
|
2007-07-01 12:08:08 +00:00
|
|
|
{ .n_name = "_ipsec4stat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_IPSEC6STAT 9
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_ipsec6stat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_PIM6STAT 10
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_pim6stat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_MRT6STAT 11
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_mrt6stat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_MF6CTABLE 12
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_mf6ctable" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_MIF6TABLE 13
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_mif6table" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_PFKEYSTAT 14
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_pfkeystat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_RTTRASH 15
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_rttrash" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_CARPSTAT 16
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_carpstats" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_PFSYNCSTAT 17
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_pfsyncstats" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_AHSTAT 18
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_ahstat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_ESPSTAT 19
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_espstat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_IPCOMPSTAT 20
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = "_ipcompstat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_TCPSTAT 21
|
2013-07-09 09:43:03 +00:00
|
|
|
{ .n_name = "_tcpstat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_UDPSTAT 22
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_udpstat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_IPSTAT 23
|
2013-07-09 09:43:03 +00:00
|
|
|
{ .n_name = "_ipstat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_ICMPSTAT 24
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_icmpstat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_IGMPSTAT 25
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_igmpstat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_PIMSTAT 26
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_pimstat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_TCBINFO 27
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_tcbinfo" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_UDBINFO 28
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_udbinfo" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_DIVCBINFO 29
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_divcbinfo" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_RIPCBINFO 30
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_ripcbinfo" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_UNP_COUNT 31
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_unp_count" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_UNP_GENCNT 32
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_unp_gencnt" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_UNP_DHEAD 33
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_unp_dhead" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_UNP_SHEAD 34
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_unp_shead" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_RIP6STAT 36
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_rip6stat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_SCTPSTAT 36
|
2007-07-16 17:15:55 +00:00
|
|
|
{ .n_name = "_sctpstat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_MFCTABLESIZE 37
|
2009-03-19 01:43:03 +00:00
|
|
|
{ .n_name = "_mfctablesize" },
|
2015-02-21 23:47:20 +00:00
|
|
|
#define N_ARPSTAT 38
|
2009-09-03 21:10:57 +00:00
|
|
|
{ .n_name = "_arpstat" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_UNP_SPHEAD 39
|
2009-10-05 15:06:14 +00:00
|
|
|
{ .n_name = "unp_sphead" },
|
2014-03-14 06:29:43 +00:00
|
|
|
#define N_SFSTAT 40
|
2013-07-15 06:16:57 +00:00
|
|
|
{ .n_name = "_sfstat"},
|
2006-07-28 16:16:40 +00:00
|
|
|
{ .n_name = NULL },
|
1994-05-27 12:33:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct protox {
|
2007-07-16 17:15:55 +00:00
|
|
|
int pr_index; /* index into nlist of cb head */
|
|
|
|
int pr_sindex; /* index into nlist of stat block */
|
1994-05-27 12:33:43 +00:00
|
|
|
u_char pr_wanted; /* 1 if wanted, 0 otherwise */
|
2007-07-16 17:15:55 +00:00
|
|
|
void (*pr_cblocks)(u_long, const char *, int, int);
|
2001-06-15 23:35:13 +00:00
|
|
|
/* control blocks printing routine */
|
2007-07-16 17:15:55 +00:00
|
|
|
void (*pr_stats)(u_long, const char *, int, int);
|
2001-06-15 23:35:13 +00:00
|
|
|
/* statistics printing routine */
|
|
|
|
void (*pr_istats)(char *); /* per/if statistics printing routine */
|
2002-04-28 12:12:49 +00:00
|
|
|
const char *pr_name; /* well-known name */
|
2007-07-16 18:13:12 +00:00
|
|
|
int pr_usesysctl; /* non-zero if we use sysctl, not kvm */
|
2007-07-16 17:15:55 +00:00
|
|
|
int pr_protocol;
|
1994-05-27 12:33:43 +00:00
|
|
|
} protox[] = {
|
2007-07-16 17:15:55 +00:00
|
|
|
{ N_TCBINFO, N_TCPSTAT, 1, protopr,
|
|
|
|
tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
|
|
|
|
{ N_UDBINFO, N_UDPSTAT, 1, protopr,
|
|
|
|
udp_stats, NULL, "udp", 1, IPPROTO_UDP },
|
2007-06-09 13:44:09 +00:00
|
|
|
#ifdef SCTP
|
2007-07-16 17:15:55 +00:00
|
|
|
{ -1, N_SCTPSTAT, 1, sctp_protopr,
|
|
|
|
sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP },
|
2011-03-21 09:58:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SDP
|
|
|
|
{ -1, -1, 1, protopr,
|
|
|
|
NULL, NULL, "sdp", 1, IPPROTO_TCP },
|
2007-06-09 13:44:09 +00:00
|
|
|
#endif
|
2007-07-16 17:15:55 +00:00
|
|
|
{ N_DIVCBINFO, -1, 1, protopr,
|
|
|
|
NULL, NULL, "divert", 1, IPPROTO_DIVERT },
|
|
|
|
{ N_RIPCBINFO, N_IPSTAT, 1, protopr,
|
|
|
|
ip_stats, NULL, "ip", 1, IPPROTO_RAW },
|
|
|
|
{ N_RIPCBINFO, N_ICMPSTAT, 1, protopr,
|
|
|
|
icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP },
|
|
|
|
{ N_RIPCBINFO, N_IGMPSTAT, 1, protopr,
|
|
|
|
igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP },
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef IPSEC
|
2007-07-01 12:08:08 +00:00
|
|
|
{ -1, N_IPSECSTAT, 1, NULL, /* keep as compat */
|
2007-07-16 17:15:55 +00:00
|
|
|
ipsec_stats, NULL, "ipsec", 0, 0},
|
2007-07-01 12:08:08 +00:00
|
|
|
{ -1, N_AHSTAT, 1, NULL,
|
2007-07-16 17:15:55 +00:00
|
|
|
ah_stats, NULL, "ah", 0, 0},
|
2007-07-01 12:08:08 +00:00
|
|
|
{ -1, N_ESPSTAT, 1, NULL,
|
2007-07-16 17:15:55 +00:00
|
|
|
esp_stats, NULL, "esp", 0, 0},
|
2007-07-01 12:08:08 +00:00
|
|
|
{ -1, N_IPCOMPSTAT, 1, NULL,
|
2007-07-16 17:15:55 +00:00
|
|
|
ipcomp_stats, NULL, "ipcomp", 0, 0},
|
1999-12-07 17:39:16 +00:00
|
|
|
#endif
|
2007-07-16 17:15:55 +00:00
|
|
|
{ N_RIPCBINFO, N_PIMSTAT, 1, protopr,
|
|
|
|
pim_stats, NULL, "pim", 1, IPPROTO_PIM },
|
|
|
|
{ -1, N_CARPSTAT, 1, NULL,
|
|
|
|
carp_stats, NULL, "carp", 1, 0 },
|
2013-10-29 17:38:13 +00:00
|
|
|
#ifdef PF
|
2007-07-16 17:15:55 +00:00
|
|
|
{ -1, N_PFSYNCSTAT, 1, NULL,
|
|
|
|
pfsync_stats, NULL, "pfsync", 1, 0 },
|
2013-10-29 17:38:13 +00:00
|
|
|
#endif
|
2009-09-03 21:10:57 +00:00
|
|
|
{ -1, N_ARPSTAT, 1, NULL,
|
|
|
|
arp_stats, NULL, "arp", 1, 0 },
|
2004-03-18 07:21:31 +00:00
|
|
|
{ -1, -1, 0, NULL,
|
2007-07-16 17:15:55 +00:00
|
|
|
NULL, NULL, NULL, 0, 0 }
|
1994-05-27 12:33:43 +00:00
|
|
|
};
|
|
|
|
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
struct protox ip6protox[] = {
|
2007-07-16 17:15:55 +00:00
|
|
|
{ N_TCBINFO, N_TCPSTAT, 1, protopr,
|
|
|
|
tcp_stats, NULL, "tcp", 1, IPPROTO_TCP },
|
|
|
|
{ N_UDBINFO, N_UDPSTAT, 1, protopr,
|
|
|
|
udp_stats, NULL, "udp", 1, IPPROTO_UDP },
|
|
|
|
{ N_RIPCBINFO, N_IP6STAT, 1, protopr,
|
|
|
|
ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW },
|
|
|
|
{ N_RIPCBINFO, N_ICMP6STAT, 1, protopr,
|
|
|
|
icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 },
|
2011-03-21 09:58:24 +00:00
|
|
|
#ifdef SDP
|
|
|
|
{ -1, -1, 1, protopr,
|
|
|
|
NULL, NULL, "sdp", 1, IPPROTO_TCP },
|
|
|
|
#endif
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef IPSEC
|
2004-03-18 07:21:31 +00:00
|
|
|
{ -1, N_IPSEC6STAT, 1, NULL,
|
2007-07-16 17:15:55 +00:00
|
|
|
ipsec_stats, NULL, "ipsec6", 0, 0 },
|
1999-12-07 17:39:16 +00:00
|
|
|
#endif
|
|
|
|
#ifdef notyet
|
2004-03-18 07:21:31 +00:00
|
|
|
{ -1, N_PIM6STAT, 1, NULL,
|
2007-07-16 17:15:55 +00:00
|
|
|
pim6_stats, NULL, "pim6", 1, 0 },
|
1999-12-07 17:39:16 +00:00
|
|
|
#endif
|
2007-07-16 17:15:55 +00:00
|
|
|
{ -1, N_RIP6STAT, 1, NULL,
|
|
|
|
rip6_stats, NULL, "rip6", 1, 0 },
|
2004-03-18 07:21:31 +00:00
|
|
|
{ -1, -1, 0, NULL,
|
2007-07-16 17:15:55 +00:00
|
|
|
NULL, NULL, NULL, 0, 0 }
|
1999-12-07 17:39:16 +00:00
|
|
|
};
|
|
|
|
#endif /*INET6*/
|
|
|
|
|
2000-07-05 02:02:54 +00:00
|
|
|
#ifdef IPSEC
|
|
|
|
struct protox pfkeyprotox[] = {
|
2004-03-18 07:21:31 +00:00
|
|
|
{ -1, N_PFKEYSTAT, 1, NULL,
|
2007-07-16 17:15:55 +00:00
|
|
|
pfkey_stats, NULL, "pfkey", 0, 0 },
|
2004-03-18 07:21:31 +00:00
|
|
|
{ -1, -1, 0, NULL,
|
2007-07-16 17:15:55 +00:00
|
|
|
NULL, NULL, NULL, 0, 0 }
|
2000-07-05 02:02:54 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2008-09-21 22:02:26 +00:00
|
|
|
#ifdef NETGRAPH
|
1999-10-21 09:06:11 +00:00
|
|
|
struct protox netgraphprotox[] = {
|
|
|
|
{ N_NGSOCKS, -1, 1, netgraphprotopr,
|
2007-07-16 17:15:55 +00:00
|
|
|
NULL, NULL, "ctrl", 0, 0 },
|
1999-10-21 09:06:11 +00:00
|
|
|
{ N_NGSOCKS, -1, 1, netgraphprotopr,
|
2007-07-16 17:15:55 +00:00
|
|
|
NULL, NULL, "data", 0, 0 },
|
2004-03-18 07:21:31 +00:00
|
|
|
{ -1, -1, 0, NULL,
|
2007-07-16 17:15:55 +00:00
|
|
|
NULL, NULL, NULL, 0, 0 }
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
2008-09-21 22:02:26 +00:00
|
|
|
#endif
|
1995-10-26 20:31:59 +00:00
|
|
|
|
1999-12-07 17:39:16 +00:00
|
|
|
struct protox *protoprotox[] = {
|
|
|
|
protox,
|
|
|
|
#ifdef INET6
|
|
|
|
ip6protox,
|
2000-07-05 02:02:54 +00:00
|
|
|
#endif
|
|
|
|
#ifdef IPSEC
|
|
|
|
pfkeyprotox,
|
2005-08-05 18:45:49 +00:00
|
|
|
#endif
|
2014-03-14 06:29:43 +00:00
|
|
|
NULL };
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2015-02-21 23:47:20 +00:00
|
|
|
static void printproto(struct protox *, const char *, bool *);
|
2004-07-26 20:18:11 +00:00
|
|
|
static void usage(void);
|
2006-07-28 16:16:40 +00:00
|
|
|
static struct protox *name2protox(const char *);
|
|
|
|
static struct protox *knownname(const char *);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1998-08-08 08:13:04 +00:00
|
|
|
static kvm_t *kvmd;
|
2001-03-15 20:46:04 +00:00
|
|
|
static char *nlistf = NULL, *memf = NULL;
|
|
|
|
|
|
|
|
int Aflag; /* show addresses of protocol control block */
|
|
|
|
int aflag; /* show all sockets (including servers) */
|
2005-09-07 17:35:16 +00:00
|
|
|
int Bflag; /* show information about bpf consumers */
|
2001-03-15 20:46:04 +00:00
|
|
|
int bflag; /* show i/f total bytes in/out */
|
|
|
|
int dflag; /* show i/f dropped packets */
|
|
|
|
int gflag; /* show group (multicast) routing or stats */
|
2005-08-18 21:04:12 +00:00
|
|
|
int hflag; /* show counters in human readable format */
|
2001-03-15 20:46:04 +00:00
|
|
|
int iflag; /* show interfaces */
|
|
|
|
int Lflag; /* show size of listen queues */
|
|
|
|
int mflag; /* show memory stats */
|
2010-01-11 03:00:17 +00:00
|
|
|
int noutputs = 0; /* how much outputs before we exit */
|
2001-06-15 00:25:44 +00:00
|
|
|
int numeric_addr; /* show addresses numerically */
|
|
|
|
int numeric_port; /* show ports numerically */
|
2001-06-15 18:25:38 +00:00
|
|
|
static int pflag; /* show given protocol */
|
2010-02-22 15:57:36 +00:00
|
|
|
int Qflag; /* show netisr information */
|
2001-03-15 20:46:04 +00:00
|
|
|
int rflag; /* show routing tables (or routing stats) */
|
2014-05-19 17:11:43 +00:00
|
|
|
int Rflag; /* show flow / RSS statistics */
|
2001-03-15 20:46:04 +00:00
|
|
|
int sflag; /* show protocol statistics */
|
|
|
|
int Wflag; /* wide display */
|
2010-11-17 18:55:12 +00:00
|
|
|
int Tflag; /* TCP Information */
|
2008-09-01 15:04:38 +00:00
|
|
|
int xflag; /* extra information, includes all socket buffer info */
|
2001-06-23 17:17:59 +00:00
|
|
|
int zflag; /* zero stats */
|
2001-03-15 20:46:04 +00:00
|
|
|
|
|
|
|
int interval; /* repeat interval for i/f stats */
|
|
|
|
|
|
|
|
char *interface; /* desired i/f for stats, or NULL for all i/fs */
|
|
|
|
int unit; /* unit number for above */
|
|
|
|
|
|
|
|
int af; /* address family */
|
2007-07-16 17:15:55 +00:00
|
|
|
int live; /* true if we are examining a live system */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
2002-09-05 17:06:51 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-09-05 17:06:51 +00:00
|
|
|
struct protox *tp = NULL; /* for printing cblocks & stats */
|
1994-05-27 12:33:43 +00:00
|
|
|
int ch;
|
2013-07-12 17:11:30 +00:00
|
|
|
int fib = -1;
|
|
|
|
char *endptr;
|
2015-02-21 23:47:20 +00:00
|
|
|
bool first = true;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
af = AF_UNSPEC;
|
|
|
|
|
2015-02-21 23:47:20 +00:00
|
|
|
argc = xo_parse_args(argc, argv);
|
|
|
|
|
2014-05-19 17:11:43 +00:00
|
|
|
while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:RrSTsuWw:xz"))
|
2010-02-22 15:57:36 +00:00
|
|
|
!= -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch(ch) {
|
2014-01-10 23:08:18 +00:00
|
|
|
case '4':
|
|
|
|
#ifdef INET
|
|
|
|
af = AF_INET;
|
|
|
|
#else
|
|
|
|
errx(1, "IPv4 support is not compiled in");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
#ifdef INET6
|
|
|
|
af = AF_INET6;
|
|
|
|
#else
|
|
|
|
errx(1, "IPv6 support is not compiled in");
|
|
|
|
#endif
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'A':
|
|
|
|
Aflag = 1;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
2005-09-07 17:35:16 +00:00
|
|
|
case 'B':
|
|
|
|
Bflag = 1;
|
|
|
|
break;
|
1994-10-12 02:37:35 +00:00
|
|
|
case 'b':
|
|
|
|
bflag = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
2013-07-12 17:11:30 +00:00
|
|
|
case 'F':
|
|
|
|
fib = strtol(optarg, &endptr, 0);
|
|
|
|
if (*endptr != '\0' ||
|
|
|
|
(fib == 0 && (errno == EINVAL || errno == ERANGE)))
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_errx(1, "%s: invalid fib", optarg);
|
2013-07-12 17:11:30 +00:00
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'f':
|
2014-03-14 02:58:48 +00:00
|
|
|
if (strcmp(optarg, "inet") == 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
af = AF_INET;
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
else if (strcmp(optarg, "inet6") == 0)
|
|
|
|
af = AF_INET6;
|
2005-10-22 17:50:45 +00:00
|
|
|
#endif
|
|
|
|
#ifdef IPSEC
|
2000-07-05 02:02:54 +00:00
|
|
|
else if (strcmp(optarg, "pfkey") == 0)
|
|
|
|
af = PF_KEY;
|
2005-10-22 17:50:45 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
else if (strcmp(optarg, "unix") == 0)
|
|
|
|
af = AF_UNIX;
|
2008-09-21 22:02:26 +00:00
|
|
|
#ifdef NETGRAPH
|
1999-10-21 09:06:11 +00:00
|
|
|
else if (strcmp(optarg, "ng") == 0
|
|
|
|
|| strcmp(optarg, "netgraph") == 0)
|
|
|
|
af = AF_NETGRAPH;
|
2008-09-21 22:02:26 +00:00
|
|
|
#endif
|
2001-09-11 17:14:33 +00:00
|
|
|
else if (strcmp(optarg, "link") == 0)
|
|
|
|
af = AF_LINK;
|
1994-05-27 12:33:43 +00:00
|
|
|
else {
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_errx(1, "%s: unknown address family",
|
|
|
|
optarg);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
gflag = 1;
|
|
|
|
break;
|
2005-08-18 21:04:12 +00:00
|
|
|
case 'h':
|
|
|
|
hflag = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'I': {
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
iflag = 1;
|
|
|
|
for (cp = interface = optarg; isalpha(*cp); cp++)
|
|
|
|
continue;
|
|
|
|
unit = atoi(cp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'i':
|
|
|
|
iflag = 1;
|
|
|
|
break;
|
1999-12-13 20:08:44 +00:00
|
|
|
case 'L':
|
|
|
|
Lflag = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'M':
|
|
|
|
memf = optarg;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
mflag = 1;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
nlistf = optarg;
|
|
|
|
break;
|
|
|
|
case 'n':
|
2001-06-15 00:25:44 +00:00
|
|
|
numeric_addr = numeric_port = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
if ((tp = name2protox(optarg)) == NULL) {
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_errx(1, "%s: unknown or uninstrumented "
|
|
|
|
"protocol", optarg);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
pflag = 1;
|
|
|
|
break;
|
2010-02-22 15:57:36 +00:00
|
|
|
case 'Q':
|
|
|
|
Qflag = 1;
|
|
|
|
break;
|
2010-01-11 03:00:17 +00:00
|
|
|
case 'q':
|
|
|
|
noutputs = atoi(optarg);
|
|
|
|
if (noutputs != 0)
|
|
|
|
noutputs++;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
2014-05-19 17:11:43 +00:00
|
|
|
case 'R':
|
|
|
|
Rflag = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 's':
|
|
|
|
++sflag;
|
|
|
|
break;
|
2001-06-15 00:25:44 +00:00
|
|
|
case 'S':
|
|
|
|
numeric_addr = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'u':
|
|
|
|
af = AF_UNIX;
|
|
|
|
break;
|
2001-03-15 20:46:04 +00:00
|
|
|
case 'W':
|
2001-09-07 12:00:50 +00:00
|
|
|
case 'l':
|
2001-03-15 20:46:04 +00:00
|
|
|
Wflag = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'w':
|
|
|
|
interval = atoi(optarg);
|
|
|
|
iflag = 1;
|
|
|
|
break;
|
2010-11-17 18:55:12 +00:00
|
|
|
case 'T':
|
|
|
|
Tflag = 1;
|
|
|
|
break;
|
2008-05-15 20:18:44 +00:00
|
|
|
case 'x':
|
|
|
|
xflag = 1;
|
|
|
|
break;
|
2001-06-23 17:17:59 +00:00
|
|
|
case 'z':
|
|
|
|
zflag = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argv += optind;
|
|
|
|
argc -= optind;
|
|
|
|
|
|
|
|
#define BACKWARD_COMPATIBILITY
|
|
|
|
#ifdef BACKWARD_COMPATIBILITY
|
|
|
|
if (*argv) {
|
|
|
|
if (isdigit(**argv)) {
|
|
|
|
interval = atoi(*argv);
|
|
|
|
if (interval <= 0)
|
|
|
|
usage();
|
|
|
|
++argv;
|
|
|
|
iflag = 1;
|
|
|
|
}
|
|
|
|
if (*argv) {
|
|
|
|
nlistf = *argv;
|
|
|
|
if (*++argv)
|
|
|
|
memf = *argv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Discard setgid privileges if not the running kernel so that bad
|
|
|
|
* guys can't print interesting stuff from kernel memory.
|
|
|
|
*/
|
2007-07-16 17:15:55 +00:00
|
|
|
live = (nlistf == NULL && memf == NULL);
|
|
|
|
if (!live)
|
1994-05-27 12:33:43 +00:00
|
|
|
setgid(getgid());
|
|
|
|
|
2015-02-21 23:47:20 +00:00
|
|
|
if (xflag && Tflag)
|
|
|
|
xo_errx(1, "-x and -T are incompatible, pick one.");
|
2010-11-17 18:55:12 +00:00
|
|
|
|
2005-09-07 17:35:16 +00:00
|
|
|
if (Bflag) {
|
2007-07-16 17:15:55 +00:00
|
|
|
if (!live)
|
|
|
|
usage();
|
2005-09-07 17:35:16 +00:00
|
|
|
bpf_stats(interface);
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_finish();
|
2005-09-07 17:35:16 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
if (mflag) {
|
2009-01-10 12:39:12 +00:00
|
|
|
if (!live) {
|
2007-07-16 17:15:55 +00:00
|
|
|
if (kread(0, NULL, 0) == 0)
|
2013-07-15 06:16:57 +00:00
|
|
|
mbpr(kvmd, nl[N_SFSTAT].n_value);
|
2001-06-14 15:45:09 +00:00
|
|
|
} else
|
2005-11-13 14:06:01 +00:00
|
|
|
mbpr(NULL, 0);
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_finish();
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2010-02-22 15:57:36 +00:00
|
|
|
if (Qflag) {
|
2010-03-01 00:46:45 +00:00
|
|
|
if (!live) {
|
|
|
|
if (kread(0, NULL, 0) == 0)
|
|
|
|
netisr_stats(kvmd);
|
|
|
|
} else
|
|
|
|
netisr_stats(NULL);
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_finish();
|
2010-02-22 15:57:36 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
1996-01-14 23:42:19 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Keep file descriptors open to avoid overhead
|
|
|
|
* of open/close on each call to get* routines.
|
|
|
|
*/
|
|
|
|
sethostent(1);
|
|
|
|
setnetent(1);
|
1996-01-14 23:42:19 +00:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* This does not make sense any more with DNS being default over
|
|
|
|
* the files. Doing a setXXXXent(1) causes a tcp connection to be
|
|
|
|
* used for the queries, which is slower.
|
|
|
|
*/
|
|
|
|
#endif
|
2001-06-15 18:25:38 +00:00
|
|
|
if (iflag && !sflag) {
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_open_container("statistics");
|
2013-12-20 00:17:26 +00:00
|
|
|
intpr(interval, NULL, af);
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_close_container("statistics");
|
|
|
|
xo_finish();
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (rflag) {
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_open_container("statistics");
|
2014-02-07 15:18:23 +00:00
|
|
|
if (sflag) {
|
2013-12-20 00:17:26 +00:00
|
|
|
rt_stats();
|
2014-02-07 15:18:23 +00:00
|
|
|
flowtable_stats();
|
|
|
|
} else
|
2013-12-20 00:17:26 +00:00
|
|
|
routepr(fib, af);
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_close_container("statistics");
|
|
|
|
xo_finish();
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2013-12-20 00:17:26 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if (gflag) {
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_open_container("statistics");
|
1999-12-07 17:39:16 +00:00
|
|
|
if (sflag) {
|
|
|
|
if (af == AF_INET || af == AF_UNSPEC)
|
2013-12-20 00:17:26 +00:00
|
|
|
mrt_stats();
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
if (af == AF_INET6 || af == AF_UNSPEC)
|
2013-12-20 00:17:26 +00:00
|
|
|
mrt6_stats();
|
1999-12-07 17:39:16 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
if (af == AF_INET || af == AF_UNSPEC)
|
2013-12-20 00:17:26 +00:00
|
|
|
mroutepr();
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
if (af == AF_INET6 || af == AF_UNSPEC)
|
2013-12-20 00:17:26 +00:00
|
|
|
mroute6pr();
|
1999-12-07 17:39:16 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_close_container("statistics");
|
|
|
|
xo_finish();
|
1999-12-07 17:39:16 +00:00
|
|
|
exit(0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1999-12-07 17:39:16 +00:00
|
|
|
|
2013-12-20 00:17:26 +00:00
|
|
|
/* Load all necessary kvm symbols */
|
|
|
|
kresolve_list(nl);
|
|
|
|
|
2001-06-15 18:25:38 +00:00
|
|
|
if (tp) {
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_open_container("statistics");
|
|
|
|
printproto(tp, tp->pr_name, &first);
|
|
|
|
if (!first)
|
|
|
|
xo_close_list("socket");
|
|
|
|
xo_close_container("statistics");
|
|
|
|
xo_finish();
|
2001-06-15 18:25:38 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2015-02-21 23:47:20 +00:00
|
|
|
|
|
|
|
xo_open_container("statistics");
|
1999-12-07 17:39:16 +00:00
|
|
|
if (af == AF_INET || af == AF_UNSPEC)
|
|
|
|
for (tp = protox; tp->pr_name; tp++)
|
2015-02-21 23:47:20 +00:00
|
|
|
printproto(tp, tp->pr_name, &first);
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
if (af == AF_INET6 || af == AF_UNSPEC)
|
|
|
|
for (tp = ip6protox; tp->pr_name; tp++)
|
2015-02-21 23:47:20 +00:00
|
|
|
printproto(tp, tp->pr_name, &first);
|
1999-12-07 17:39:16 +00:00
|
|
|
#endif /*INET6*/
|
2000-07-05 02:02:54 +00:00
|
|
|
#ifdef IPSEC
|
|
|
|
if (af == PF_KEY || af == AF_UNSPEC)
|
|
|
|
for (tp = pfkeyprotox; tp->pr_name; tp++)
|
2015-02-21 23:47:20 +00:00
|
|
|
printproto(tp, tp->pr_name, &first);
|
2000-07-05 02:02:54 +00:00
|
|
|
#endif /*IPSEC*/
|
2008-09-21 22:02:26 +00:00
|
|
|
#ifdef NETGRAPH
|
1999-10-21 09:06:11 +00:00
|
|
|
if (af == AF_NETGRAPH || af == AF_UNSPEC)
|
|
|
|
for (tp = netgraphprotox; tp->pr_name; tp++)
|
2015-02-21 23:47:20 +00:00
|
|
|
printproto(tp, tp->pr_name, &first);
|
2008-09-21 22:02:26 +00:00
|
|
|
#endif /* NETGRAPH */
|
2008-12-31 08:56:49 +00:00
|
|
|
if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
|
2007-07-16 17:15:55 +00:00
|
|
|
unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
|
2009-10-05 15:06:14 +00:00
|
|
|
nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
|
2015-02-21 23:47:20 +00:00
|
|
|
nl[N_UNP_SPHEAD].n_value, &first);
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
xo_close_list("socket");
|
|
|
|
xo_close_container("statistics");
|
|
|
|
xo_finish();
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print out protocol statistics or control blocks (per sflag).
|
|
|
|
* If the interface was not specifically requested, and the symbol
|
|
|
|
* is not in the namelist, ignore this one.
|
|
|
|
*/
|
|
|
|
static void
|
2015-02-21 23:47:20 +00:00
|
|
|
printproto(struct protox *tp, const char *name, bool *first)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2007-07-16 17:15:55 +00:00
|
|
|
void (*pr)(u_long, const char *, int, int);
|
1994-05-27 12:33:43 +00:00
|
|
|
u_long off;
|
2015-02-21 23:47:20 +00:00
|
|
|
bool doingdblocks = false;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (sflag) {
|
1999-12-07 17:39:16 +00:00
|
|
|
if (iflag) {
|
|
|
|
if (tp->pr_istats)
|
2013-12-20 00:17:26 +00:00
|
|
|
intpr(interval, tp->pr_istats, af);
|
2001-06-15 18:25:38 +00:00
|
|
|
else if (pflag)
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_message("%s: no per-interface stats routine",
|
2001-06-15 18:25:38 +00:00
|
|
|
tp->pr_name);
|
1999-12-07 17:39:16 +00:00
|
|
|
return;
|
2007-07-16 17:15:55 +00:00
|
|
|
} else {
|
1999-12-07 17:39:16 +00:00
|
|
|
pr = tp->pr_stats;
|
2001-06-15 18:25:38 +00:00
|
|
|
if (!pr) {
|
|
|
|
if (pflag)
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_message("%s: no stats routine",
|
2001-06-15 18:25:38 +00:00
|
|
|
tp->pr_name);
|
|
|
|
return;
|
2008-01-02 23:26:11 +00:00
|
|
|
}
|
2007-07-16 17:15:55 +00:00
|
|
|
if (tp->pr_usesysctl && live)
|
|
|
|
off = 0;
|
|
|
|
else if (tp->pr_sindex < 0) {
|
|
|
|
if (pflag)
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_message("%s: stats routine doesn't "
|
|
|
|
"work on cores", tp->pr_name);
|
2007-07-16 17:15:55 +00:00
|
|
|
return;
|
|
|
|
} else
|
|
|
|
off = nl[tp->pr_sindex].n_value;
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
} else {
|
2015-02-21 23:47:20 +00:00
|
|
|
doingdblocks = true;
|
1994-05-27 12:33:43 +00:00
|
|
|
pr = tp->pr_cblocks;
|
2001-06-15 18:25:38 +00:00
|
|
|
if (!pr) {
|
|
|
|
if (pflag)
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_message("%s: no PCB routine", tp->pr_name);
|
2001-06-15 18:25:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-07-16 17:15:55 +00:00
|
|
|
if (tp->pr_usesysctl && live)
|
|
|
|
off = 0;
|
|
|
|
else if (tp->pr_index < 0) {
|
|
|
|
if (pflag)
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_message("%s: PCB routine doesn't work on "
|
|
|
|
"cores", tp->pr_name);
|
2007-07-16 17:15:55 +00:00
|
|
|
return;
|
|
|
|
} else
|
|
|
|
off = nl[tp->pr_index].n_value;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2007-07-16 17:15:55 +00:00
|
|
|
if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
|
2015-02-21 23:47:20 +00:00
|
|
|
af != AF_UNSPEC)) {
|
|
|
|
if (doingdblocks && *first) {
|
|
|
|
xo_open_list("socket");
|
|
|
|
*first = false;
|
|
|
|
}
|
|
|
|
|
2007-07-16 17:15:55 +00:00
|
|
|
(*pr)(off, name, af, tp->pr_protocol);
|
2015-02-21 23:47:20 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 20:29:23 +00:00
|
|
|
static int
|
|
|
|
kvmd_init(void)
|
|
|
|
{
|
|
|
|
char errbuf[_POSIX2_LINE_MAX];
|
|
|
|
|
|
|
|
if (kvmd != NULL)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
|
|
|
|
setgid(getgid());
|
|
|
|
|
|
|
|
if (kvmd == NULL) {
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_warnx("kvm not available: %s", errbuf);
|
2013-04-10 20:29:23 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2013-12-20 00:17:26 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Resolve symbol list, return 0 on success.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
kresolve_list(struct nlist *_nl)
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((kvmd == NULL) && (kvmd_init() != 0))
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (_nl[0].n_type != 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (kvm_nlist(kvmd, _nl) < 0) {
|
2013-04-10 20:29:23 +00:00
|
|
|
if (nlistf)
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_errx(1, "%s: kvm_nlist: %s", nlistf,
|
|
|
|
kvm_geterr(kvmd));
|
2013-04-10 20:29:23 +00:00
|
|
|
else
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
|
2013-04-10 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Read kernel memory, return 0 on success.
|
|
|
|
*/
|
|
|
|
int
|
2007-07-16 17:15:55 +00:00
|
|
|
kread(u_long addr, void *buf, size_t size)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2007-07-16 17:15:55 +00:00
|
|
|
|
2013-04-10 20:29:23 +00:00
|
|
|
if (kvmd_init() < 0)
|
|
|
|
return (-1);
|
1998-08-05 13:54:07 +00:00
|
|
|
|
1998-08-08 08:13:04 +00:00
|
|
|
if (!buf)
|
|
|
|
return (0);
|
2007-07-16 17:15:55 +00:00
|
|
|
if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_warnx("%s", kvm_geterr(kvmd));
|
1994-05-27 12:33:43 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-03-05 01:17:47 +00:00
|
|
|
/*
|
|
|
|
* Read single counter(9).
|
|
|
|
*/
|
|
|
|
uint64_t
|
|
|
|
kread_counter(u_long addr)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (kvmd_init() < 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
return (kvm_counter_u64_fetch(kvmd, addr));
|
|
|
|
}
|
|
|
|
|
2013-04-10 20:29:23 +00:00
|
|
|
/*
|
|
|
|
* Read an array of N counters in kernel memory into array of N uint64_t's.
|
|
|
|
*/
|
|
|
|
int
|
2013-07-09 09:43:03 +00:00
|
|
|
kread_counters(u_long addr, void *buf, size_t size)
|
2013-04-10 20:29:23 +00:00
|
|
|
{
|
2013-07-09 09:43:03 +00:00
|
|
|
uint64_t *c = buf;
|
2013-04-10 20:29:23 +00:00
|
|
|
|
|
|
|
if (kvmd_init() < 0)
|
|
|
|
return (-1);
|
|
|
|
|
2013-07-09 09:43:03 +00:00
|
|
|
if (kread(addr, buf, size) < 0)
|
|
|
|
return (-1);
|
2013-04-10 20:29:23 +00:00
|
|
|
|
2013-07-09 09:43:03 +00:00
|
|
|
while (size != 0) {
|
|
|
|
*c = kvm_counter_u64_fetch(kvmd, *c);
|
|
|
|
size -= sizeof(*c);
|
|
|
|
c++;
|
|
|
|
}
|
2013-04-10 20:29:23 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-09-05 17:06:51 +00:00
|
|
|
const char *
|
2006-07-28 16:09:19 +00:00
|
|
|
plural(uintmax_t n)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
return (n != 1 ? "s" : "");
|
|
|
|
}
|
|
|
|
|
2002-09-05 17:06:51 +00:00
|
|
|
const char *
|
2006-07-28 16:09:19 +00:00
|
|
|
plurales(uintmax_t n)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
return (n != 1 ? "es" : "");
|
|
|
|
}
|
|
|
|
|
2003-01-14 16:02:56 +00:00
|
|
|
const char *
|
2006-07-28 16:09:19 +00:00
|
|
|
pluralies(uintmax_t n)
|
2003-01-14 16:02:56 +00:00
|
|
|
{
|
|
|
|
return (n != 1 ? "ies" : "y");
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Find the protox for the given "well-known" name.
|
|
|
|
*/
|
|
|
|
static struct protox *
|
2006-07-28 16:16:40 +00:00
|
|
|
knownname(const char *name)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
struct protox **tpp, *tp;
|
|
|
|
|
|
|
|
for (tpp = protoprotox; *tpp; tpp++)
|
|
|
|
for (tp = *tpp; tp->pr_name; tp++)
|
|
|
|
if (strcmp(tp->pr_name, name) == 0)
|
|
|
|
return (tp);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the protox corresponding to name.
|
|
|
|
*/
|
|
|
|
static struct protox *
|
2006-07-28 16:16:40 +00:00
|
|
|
name2protox(const char *name)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
struct protox *tp;
|
|
|
|
char **alias; /* alias from p->aliases */
|
|
|
|
struct protoent *p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to find the name in the list of "well-known" names. If that
|
|
|
|
* fails, check if name is an alias for an Internet protocol.
|
|
|
|
*/
|
1999-12-07 17:39:16 +00:00
|
|
|
if ((tp = knownname(name)) != NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
return (tp);
|
|
|
|
|
|
|
|
setprotoent(1); /* make protocol lookup cheaper */
|
1999-12-07 17:39:16 +00:00
|
|
|
while ((p = getprotoent()) != NULL) {
|
1994-05-27 12:33:43 +00:00
|
|
|
/* assert: name not same as p->name */
|
|
|
|
for (alias = p->p_aliases; *alias; alias++)
|
|
|
|
if (strcmp(name, *alias) == 0) {
|
|
|
|
endprotoent();
|
|
|
|
return (knownname(p->p_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endprotoent();
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-06-15 23:35:13 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2015-02-21 23:47:20 +00:00
|
|
|
(void)xo_error("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
|
2014-05-19 17:11:43 +00:00
|
|
|
"usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n"
|
2001-09-11 15:21:36 +00:00
|
|
|
" [-M core] [-N system]",
|
2014-01-10 23:08:18 +00:00
|
|
|
" netstat -i | -I interface [-46abdhnW] [-f address_family]\n"
|
2001-09-11 17:14:33 +00:00
|
|
|
" [-M core] [-N system]",
|
2014-06-04 04:18:33 +00:00
|
|
|
" netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n"
|
|
|
|
" [-q howmany]",
|
|
|
|
" netstat -s [-46sz] [-f protocol_family | -p protocol]\n"
|
2001-09-11 15:21:36 +00:00
|
|
|
" [-M core] [-N system]",
|
2014-06-04 04:18:33 +00:00
|
|
|
" netstat -i | -I interface -s [-46s]\n"
|
|
|
|
" [-f protocol_family | -p protocol] [-M core] [-N system]",
|
2004-10-20 11:18:31 +00:00
|
|
|
" netstat -m [-M core] [-N system]",
|
2014-06-04 04:18:33 +00:00
|
|
|
" netstat -B [-z] [-I interface]",
|
|
|
|
" netstat -r [-46AnW] [-F fibnum] [-f address_family]\n"
|
|
|
|
" [-M core] [-N system]",
|
2001-09-11 15:21:36 +00:00
|
|
|
" netstat -rs [-s] [-M core] [-N system]",
|
2014-01-10 23:08:18 +00:00
|
|
|
" netstat -g [-46W] [-f address_family] [-M core] [-N system]",
|
|
|
|
" netstat -gs [-46s] [-f address_family] [-M core] [-N system]",
|
2010-02-22 15:57:36 +00:00
|
|
|
" netstat -Q");
|
2015-02-21 23:47:20 +00:00
|
|
|
xo_finish();
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|