2008-01-02 23:26:11 +00:00
|
|
|
/*-
|
2017-11-20 19:49:47 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
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.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00:00
|
|
|
* 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
|
2015-09-02 18:51:36 +00:00
|
|
|
static 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>
|
2015-09-11 04:37:01 +00:00
|
|
|
#include <sys/sysctl.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-09-02 18:51:36 +00:00
|
|
|
#include "nl_defs.h"
|
2015-02-21 23:47:20 +00:00
|
|
|
#include <libxo/xo.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2015-09-02 18:51:36 +00:00
|
|
|
static 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
|
2015-09-02 18:51:36 +00:00
|
|
|
{ -1, N_IPSEC4STAT, 1, NULL, /* keep as compat */
|
2015-12-17 02:02:09 +00:00
|
|
|
ipsec_stats, NULL, "ipsec", 1, 0},
|
2007-07-01 12:08:08 +00:00
|
|
|
{ -1, N_AHSTAT, 1, NULL,
|
2015-12-17 02:02:09 +00:00
|
|
|
ah_stats, NULL, "ah", 1, 0},
|
2007-07-01 12:08:08 +00:00
|
|
|
{ -1, N_ESPSTAT, 1, NULL,
|
2015-12-17 02:02:09 +00:00
|
|
|
esp_stats, NULL, "esp", 1, 0},
|
2007-07-01 12:08:08 +00:00
|
|
|
{ -1, N_IPCOMPSTAT, 1, NULL,
|
2015-12-17 02:02:09 +00:00
|
|
|
ipcomp_stats, NULL, "ipcomp", 1, 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 },
|
2015-09-02 18:51:36 +00:00
|
|
|
{ -1, N_CARPSTATS, 1, NULL,
|
2007-07-16 17:15:55 +00:00
|
|
|
carp_stats, NULL, "carp", 1, 0 },
|
2013-10-29 17:38:13 +00:00
|
|
|
#ifdef PF
|
2015-09-02 18:51:36 +00:00
|
|
|
{ -1, N_PFSYNCSTATS, 1, NULL,
|
2007-07-16 17:15:55 +00:00
|
|
|
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
|
2015-09-02 18:51:36 +00:00
|
|
|
static 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,
|
2015-12-17 02:02:09 +00:00
|
|
|
ipsec_stats, NULL, "ipsec6", 1, 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
|
2015-09-02 18:51:36 +00:00
|
|
|
static 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
|
2015-09-02 18:51:36 +00:00
|
|
|
static struct protox netgraphprotox[] = {
|
|
|
|
{ N_NGSOCKLIST, -1, 1, netgraphprotopr,
|
2007-07-16 17:15:55 +00:00
|
|
|
NULL, NULL, "ctrl", 0, 0 },
|
2015-09-02 18:51:36 +00:00
|
|
|
{ N_NGSOCKLIST, -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
|
|
|
|
2015-09-02 18:51:36 +00:00
|
|
|
static struct protox *protoprotox[] = {
|
1999-12-07 17:39:16 +00:00
|
|
|
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
|
|
|
|
2015-09-02 18:51:36 +00:00
|
|
|
static int kresolve_list(struct nlist *_nl);
|
|
|
|
|
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) */
|
2015-09-02 18:51:36 +00:00
|
|
|
static int Bflag; /* show information about bpf consumers */
|
2001-03-15 20:46:04 +00:00
|
|
|
int bflag; /* show i/f total bytes in/out */
|
2020-09-13 09:14:32 +00:00
|
|
|
int Cflag; /* show congestion control */
|
2001-03-15 20:46:04 +00:00
|
|
|
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 */
|
2020-10-03 10:47:17 +00:00
|
|
|
int Oflag; /* show nhgrp objects*/
|
Introduce nexthop objects and new routing KPI.
This is the foundational change for the routing subsytem rearchitecture.
More details and goals are available in https://reviews.freebsd.org/D24141 .
This patch introduces concept of nexthop objects and new nexthop-based
routing KPI.
Nexthops are objects, containing all necessary information for performing
the packet output decision. Output interface, mtu, flags, gw address goes
there. For most of the cases, these objects will serve the same role as
the struct rtentry is currently serving.
Typically there will be low tens of such objects for the router even with
multiple BGP full-views, as these objects will be shared between routing
entries. This allows to store more information in the nexthop.
New KPI:
struct nhop_object *fib4_lookup(uint32_t fibnum, struct in_addr dst,
uint32_t scopeid, uint32_t flags, uint32_t flowid);
struct nhop_object *fib6_lookup(uint32_t fibnum, const struct in6_addr *dst6,
uint32_t scopeid, uint32_t flags, uint32_t flowid);
These 2 function are intended to replace all all flavours of
<in_|in6_>rtalloc[1]<_ign><_fib>, mpath functions and the previous
fib[46]-generation functions.
Upon successful lookup, they return nexthop object which is guaranteed to
exist within current NET_EPOCH. If longer lifetime is desired, one can
specify NHR_REF as a flag and get a referenced version of the nexthop.
Reference semantic closely resembles rtentry one, allowing sed-style conversion.
Additionally, another 2 functions are introduced to support uRPF functionality
inside variety of our firewalls. Their primary goal is to hide the multipath
implementation details inside the routing subsystem, greatly simplifying
firewalls implementation:
int fib4_lookup_urpf(uint32_t fibnum, struct in_addr dst, uint32_t scopeid,
uint32_t flags, const struct ifnet *src_if);
int fib6_lookup_urpf(uint32_t fibnum, const struct in6_addr *dst6, uint32_t scopeid,
uint32_t flags, const struct ifnet *src_if);
All functions have a separate scopeid argument, paving way to eliminating IPv6 scope
embedding and allowing to support IPv4 link-locals in the future.
Structure changes:
* rtentry gets new 'rt_nhop' pointer, slightly growing the overall size.
* rib_head gets new 'rnh_preadd' callback pointer, slightly growing overall sz.
Old KPI:
During the transition state old and new KPI will coexists. As there are another 4-5
decent-sized conversion patches, it will probably take a couple of weeks.
To support both KPIs, fields not required by the new KPI (most of rtentry) has to be
kept, resulting in the temporary size increase.
Once conversion is finished, rtentry will notably shrink.
More details:
* architectural overview: https://reviews.freebsd.org/D24141
* list of the next changes: https://reviews.freebsd.org/D24232
Reviewed by: ae,glebius(initial version)
Differential Revision: https://reviews.freebsd.org/D24232
2020-04-12 14:30:00 +00:00
|
|
|
int oflag; /* show nexthop objects*/
|
2018-03-22 09:40:08 +00:00
|
|
|
int Pflag; /* show TCP log ID */
|
2001-06-15 18:25:38 +00:00
|
|
|
static int pflag; /* show given protocol */
|
2015-09-02 18:51:36 +00:00
|
|
|
static 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 */
|
|
|
|
|
2015-09-02 18:51:36 +00:00
|
|
|
static 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);
|
2015-11-24 11:07:37 +00:00
|
|
|
if (argc < 0)
|
|
|
|
exit(EXIT_FAILURE);
|
2015-02-21 23:47:20 +00:00
|
|
|
|
2020-10-03 10:47:17 +00:00
|
|
|
while ((ch = getopt(argc, argv, "46AaBbCdF:f:ghI:iLlM:mN:nOoPp: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;
|
2020-09-13 09:14:32 +00:00
|
|
|
case 'C':
|
|
|
|
Cflag = 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
|
2015-09-01 08:42:04 +00:00
|
|
|
else if (strcmp(optarg, "unix") == 0 ||
|
|
|
|
strcmp(optarg, "local") == 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
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;
|
Introduce nexthop objects and new routing KPI.
This is the foundational change for the routing subsytem rearchitecture.
More details and goals are available in https://reviews.freebsd.org/D24141 .
This patch introduces concept of nexthop objects and new nexthop-based
routing KPI.
Nexthops are objects, containing all necessary information for performing
the packet output decision. Output interface, mtu, flags, gw address goes
there. For most of the cases, these objects will serve the same role as
the struct rtentry is currently serving.
Typically there will be low tens of such objects for the router even with
multiple BGP full-views, as these objects will be shared between routing
entries. This allows to store more information in the nexthop.
New KPI:
struct nhop_object *fib4_lookup(uint32_t fibnum, struct in_addr dst,
uint32_t scopeid, uint32_t flags, uint32_t flowid);
struct nhop_object *fib6_lookup(uint32_t fibnum, const struct in6_addr *dst6,
uint32_t scopeid, uint32_t flags, uint32_t flowid);
These 2 function are intended to replace all all flavours of
<in_|in6_>rtalloc[1]<_ign><_fib>, mpath functions and the previous
fib[46]-generation functions.
Upon successful lookup, they return nexthop object which is guaranteed to
exist within current NET_EPOCH. If longer lifetime is desired, one can
specify NHR_REF as a flag and get a referenced version of the nexthop.
Reference semantic closely resembles rtentry one, allowing sed-style conversion.
Additionally, another 2 functions are introduced to support uRPF functionality
inside variety of our firewalls. Their primary goal is to hide the multipath
implementation details inside the routing subsystem, greatly simplifying
firewalls implementation:
int fib4_lookup_urpf(uint32_t fibnum, struct in_addr dst, uint32_t scopeid,
uint32_t flags, const struct ifnet *src_if);
int fib6_lookup_urpf(uint32_t fibnum, const struct in6_addr *dst6, uint32_t scopeid,
uint32_t flags, const struct ifnet *src_if);
All functions have a separate scopeid argument, paving way to eliminating IPv6 scope
embedding and allowing to support IPv4 link-locals in the future.
Structure changes:
* rtentry gets new 'rt_nhop' pointer, slightly growing the overall size.
* rib_head gets new 'rnh_preadd' callback pointer, slightly growing overall sz.
Old KPI:
During the transition state old and new KPI will coexists. As there are another 4-5
decent-sized conversion patches, it will probably take a couple of weeks.
To support both KPIs, fields not required by the new KPI (most of rtentry) has to be
kept, resulting in the temporary size increase.
Once conversion is finished, rtentry will notably shrink.
More details:
* architectural overview: https://reviews.freebsd.org/D24141
* list of the next changes: https://reviews.freebsd.org/D24232
Reviewed by: ae,glebius(initial version)
Differential Revision: https://reviews.freebsd.org/D24232
2020-04-12 14:30:00 +00:00
|
|
|
case 'o':
|
|
|
|
oflag = 1;
|
|
|
|
break;
|
2020-10-03 10:47:17 +00:00
|
|
|
case 'O':
|
|
|
|
Oflag = 1;
|
|
|
|
break;
|
2018-03-22 09:40:08 +00:00
|
|
|
case 'P':
|
|
|
|
Pflag = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
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);
|
2015-09-01 06:05:43 +00:00
|
|
|
if (!live) {
|
|
|
|
if (setgid(getgid()) != 0)
|
|
|
|
xo_err(-1, "setgid");
|
2017-08-30 07:58:33 +00:00
|
|
|
/* Load all necessary kvm symbols */
|
|
|
|
kresolve_list(nl);
|
2015-09-01 06:05:43 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
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)
|
2015-09-02 18:51:36 +00:00
|
|
|
netisr_stats();
|
2010-03-01 00:46:45 +00:00
|
|
|
} else
|
2015-09-02 18:51:36 +00:00
|
|
|
netisr_stats();
|
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");
|
2015-09-01 08:42:04 +00:00
|
|
|
intpr(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) {
|
2019-12-09 10:21:24 +00:00
|
|
|
if (live) {
|
|
|
|
kresolve_list(nl);
|
|
|
|
}
|
2013-12-20 00:17:26 +00:00
|
|
|
rt_stats();
|
2014-02-07 15:18:23 +00:00
|
|
|
} 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);
|
|
|
|
}
|
Introduce nexthop objects and new routing KPI.
This is the foundational change for the routing subsytem rearchitecture.
More details and goals are available in https://reviews.freebsd.org/D24141 .
This patch introduces concept of nexthop objects and new nexthop-based
routing KPI.
Nexthops are objects, containing all necessary information for performing
the packet output decision. Output interface, mtu, flags, gw address goes
there. For most of the cases, these objects will serve the same role as
the struct rtentry is currently serving.
Typically there will be low tens of such objects for the router even with
multiple BGP full-views, as these objects will be shared between routing
entries. This allows to store more information in the nexthop.
New KPI:
struct nhop_object *fib4_lookup(uint32_t fibnum, struct in_addr dst,
uint32_t scopeid, uint32_t flags, uint32_t flowid);
struct nhop_object *fib6_lookup(uint32_t fibnum, const struct in6_addr *dst6,
uint32_t scopeid, uint32_t flags, uint32_t flowid);
These 2 function are intended to replace all all flavours of
<in_|in6_>rtalloc[1]<_ign><_fib>, mpath functions and the previous
fib[46]-generation functions.
Upon successful lookup, they return nexthop object which is guaranteed to
exist within current NET_EPOCH. If longer lifetime is desired, one can
specify NHR_REF as a flag and get a referenced version of the nexthop.
Reference semantic closely resembles rtentry one, allowing sed-style conversion.
Additionally, another 2 functions are introduced to support uRPF functionality
inside variety of our firewalls. Their primary goal is to hide the multipath
implementation details inside the routing subsystem, greatly simplifying
firewalls implementation:
int fib4_lookup_urpf(uint32_t fibnum, struct in_addr dst, uint32_t scopeid,
uint32_t flags, const struct ifnet *src_if);
int fib6_lookup_urpf(uint32_t fibnum, const struct in6_addr *dst6, uint32_t scopeid,
uint32_t flags, const struct ifnet *src_if);
All functions have a separate scopeid argument, paving way to eliminating IPv6 scope
embedding and allowing to support IPv4 link-locals in the future.
Structure changes:
* rtentry gets new 'rt_nhop' pointer, slightly growing the overall size.
* rib_head gets new 'rnh_preadd' callback pointer, slightly growing overall sz.
Old KPI:
During the transition state old and new KPI will coexists. As there are another 4-5
decent-sized conversion patches, it will probably take a couple of weeks.
To support both KPIs, fields not required by the new KPI (most of rtentry) has to be
kept, resulting in the temporary size increase.
Once conversion is finished, rtentry will notably shrink.
More details:
* architectural overview: https://reviews.freebsd.org/D24141
* list of the next changes: https://reviews.freebsd.org/D24232
Reviewed by: ae,glebius(initial version)
Differential Revision: https://reviews.freebsd.org/D24232
2020-04-12 14:30:00 +00:00
|
|
|
if (oflag) {
|
|
|
|
xo_open_container("statistics");
|
|
|
|
nhops_print(fib, af);
|
|
|
|
xo_close_container("statistics");
|
|
|
|
xo_finish();
|
|
|
|
exit(0);
|
|
|
|
}
|
2020-10-03 10:47:17 +00:00
|
|
|
if (Oflag) {
|
|
|
|
xo_open_container("statistics");
|
|
|
|
nhgrp_print(fib, af);
|
|
|
|
xo_close_container("statistics");
|
|
|
|
xo_finish();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
Introduce nexthop objects and new routing KPI.
This is the foundational change for the routing subsytem rearchitecture.
More details and goals are available in https://reviews.freebsd.org/D24141 .
This patch introduces concept of nexthop objects and new nexthop-based
routing KPI.
Nexthops are objects, containing all necessary information for performing
the packet output decision. Output interface, mtu, flags, gw address goes
there. For most of the cases, these objects will serve the same role as
the struct rtentry is currently serving.
Typically there will be low tens of such objects for the router even with
multiple BGP full-views, as these objects will be shared between routing
entries. This allows to store more information in the nexthop.
New KPI:
struct nhop_object *fib4_lookup(uint32_t fibnum, struct in_addr dst,
uint32_t scopeid, uint32_t flags, uint32_t flowid);
struct nhop_object *fib6_lookup(uint32_t fibnum, const struct in6_addr *dst6,
uint32_t scopeid, uint32_t flags, uint32_t flowid);
These 2 function are intended to replace all all flavours of
<in_|in6_>rtalloc[1]<_ign><_fib>, mpath functions and the previous
fib[46]-generation functions.
Upon successful lookup, they return nexthop object which is guaranteed to
exist within current NET_EPOCH. If longer lifetime is desired, one can
specify NHR_REF as a flag and get a referenced version of the nexthop.
Reference semantic closely resembles rtentry one, allowing sed-style conversion.
Additionally, another 2 functions are introduced to support uRPF functionality
inside variety of our firewalls. Their primary goal is to hide the multipath
implementation details inside the routing subsystem, greatly simplifying
firewalls implementation:
int fib4_lookup_urpf(uint32_t fibnum, struct in_addr dst, uint32_t scopeid,
uint32_t flags, const struct ifnet *src_if);
int fib6_lookup_urpf(uint32_t fibnum, const struct in6_addr *dst6, uint32_t scopeid,
uint32_t flags, const struct ifnet *src_if);
All functions have a separate scopeid argument, paving way to eliminating IPv6 scope
embedding and allowing to support IPv4 link-locals in the future.
Structure changes:
* rtentry gets new 'rt_nhop' pointer, slightly growing the overall size.
* rib_head gets new 'rnh_preadd' callback pointer, slightly growing overall sz.
Old KPI:
During the transition state old and new KPI will coexists. As there are another 4-5
decent-sized conversion patches, it will probably take a couple of weeks.
To support both KPIs, fields not required by the new KPI (most of rtentry) has to be
kept, resulting in the temporary size increase.
Once conversion is finished, rtentry will notably shrink.
More details:
* architectural overview: https://reviews.freebsd.org/D24141
* list of the next changes: https://reviews.freebsd.org/D24232
Reviewed by: ae,glebius(initial version)
Differential Revision: https://reviews.freebsd.org/D24232
2020-04-12 14:30:00 +00:00
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-03-15 00:19:30 +00:00
|
|
|
static int
|
|
|
|
fetch_stats_internal(const char *sysctlname, u_long off, void *stats,
|
|
|
|
size_t len, kreadfn_t kreadfn, int zero)
|
2015-09-11 04:37:01 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (live) {
|
|
|
|
memset(stats, 0, len);
|
2016-03-15 00:19:30 +00:00
|
|
|
if (zero)
|
2015-09-11 04:37:01 +00:00
|
|
|
error = sysctlbyname(sysctlname, NULL, NULL, stats,
|
|
|
|
len);
|
|
|
|
else
|
|
|
|
error = sysctlbyname(sysctlname, stats, &len, NULL, 0);
|
|
|
|
if (error == -1 && errno != ENOENT)
|
|
|
|
xo_warn("sysctl %s", sysctlname);
|
|
|
|
} else {
|
|
|
|
if (off == 0)
|
|
|
|
return (1);
|
|
|
|
error = kreadfn(off, stats, len);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2016-03-15 00:19:30 +00:00
|
|
|
int
|
|
|
|
fetch_stats(const char *sysctlname, u_long off, void *stats,
|
|
|
|
size_t len, kreadfn_t kreadfn)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn,
|
|
|
|
zflag));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fetch_stats_ro(const char *sysctlname, u_long off, void *stats,
|
|
|
|
size_t len, kreadfn_t kreadfn)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0));
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* 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)
|
2015-09-01 08:42:04 +00:00
|
|
|
intpr(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);
|
2015-09-01 06:05:43 +00:00
|
|
|
if (setgid(getgid()) != 0)
|
|
|
|
xo_err(-1, "setgid");
|
2013-04-10 20:29:23 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2015-09-02 18:51:36 +00:00
|
|
|
static int
|
2013-12-20 00:17:26 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-09-02 18:51:36 +00:00
|
|
|
/*
|
|
|
|
* Wrapper of kvm_dpcpu_setcpu().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
kset_dpcpu(u_int cpuid)
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((kvmd == NULL) && (kvmd_init() != 0))
|
|
|
|
xo_errx(-1, "%s: kvm is not available", __func__);
|
|
|
|
|
|
|
|
if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0)
|
|
|
|
xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
|
|
|
|
cpuid, kvm_geterr(kvmd));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-07-21 23:57:38 +00:00
|
|
|
uint64_t *c;
|
|
|
|
u_long *counters;
|
|
|
|
size_t i, n;
|
2013-04-10 20:29:23 +00:00
|
|
|
|
|
|
|
if (kvmd_init() < 0)
|
|
|
|
return (-1);
|
|
|
|
|
2015-07-21 23:57:38 +00:00
|
|
|
if (size % sizeof(uint64_t) != 0) {
|
|
|
|
xo_warnx("kread_counters: invalid counter set size");
|
2013-07-09 09:43:03 +00:00
|
|
|
return (-1);
|
2015-07-21 23:57:38 +00:00
|
|
|
}
|
2013-04-10 20:29:23 +00:00
|
|
|
|
2015-07-21 23:57:38 +00:00
|
|
|
n = size / sizeof(uint64_t);
|
|
|
|
if ((counters = malloc(n * sizeof(u_long))) == NULL)
|
|
|
|
xo_err(-1, "malloc");
|
|
|
|
if (kread(addr, counters, n * sizeof(u_long)) < 0) {
|
|
|
|
free(counters);
|
|
|
|
return (-1);
|
2013-07-09 09:43:03 +00:00
|
|
|
}
|
2015-07-21 23:57:38 +00:00
|
|
|
|
|
|
|
c = buf;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
c[i] = kvm_counter_u64_fetch(kvmd, counters[i]);
|
|
|
|
|
|
|
|
free(counters);
|
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);
|
|
|
|
}
|