/* $NetBSD: main.c,v 1.105 2023/08/18 13:18:17 martin Exp $ */
/*
* 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.
* 3. 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.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\
Regents of the University of California. All rights reserved.");
#endif /* not lint */
int Aflag;
int aflag;
int Bflag;
int bflag;
int dflag;
#ifndef SMALL
int gflag;
#endif
int hflag;
int iflag;
int Lflag;
int lflag;
int mflag;
int numeric_addr;
int numeric_port;
int nflag;
int Pflag;
int pflag;
int qflag;
int rflag;
int sflag;
int tagflag;
int tflag;
int Vflag;
int vflag;
/*
* Try to figure out if we can use sysctl or not.
*/
if (nf != NULL || mf != NULL) {
/* Of course, we can't use sysctl with dumps. */
if (force_sysctl)
errx(EXIT_FAILURE, "can't use sysctl with dumps");
/*
* If we have -M or -N, we're not dealing with live memory
* or want to use kvm interface explicitly. It is sometimes
* useful to dig inside of kernel without extending
* sysctl interface (i.e., without rebuilding kernel).
*/
use_sysctl = 0;
} else if (qflag ||
#ifndef SMALL
gflag ||
#endif
(pflag && tp->pr_sindex == N_PIMSTAT) ||
Pflag) {
/* These flags are not yet supported via sysctl(3). */
use_sysctl = 0;
} else {
/* We can use sysctl(3). */
use_sysctl = 1;
}
if (force_sysctl && !use_sysctl) {
/* Let the user know what's about to happen. */
warnx("forcing sysctl usage even though it might not be "
"supported");
use_sysctl = 1;
}
kvmd = prepare_kvmd(nf, mf, buf);
if (!use_sysctl) {
if (kvmd == NULL)
errx(1, "kvm error: %s", buf);
if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
if (nf)
errx(1, "%s: no namelist", nf);
else
errx(1, "no namelist");
}
} else
(void)setgid(getgid());
}
int
main(int argc, char *argv[])
{
struct protoent *p;
struct protox *tp; /* for printing cblocks & stats */
int ch;
char *cp;
char *afname, *afnames;
u_long pcbaddr;
if (prog_init) {
if (prog_init() == -1)
err(1, "init failed");
force_sysctl = 1; /* cheap trick */
}
while ((ch = getopt(argc, argv,
"AabBdf:ghI:LliM:mN:nP:p:qrsStTuVvw:X")) != -1)
switch (ch) {
case 'A':
Aflag = RT_AFLAG;
break;
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'B':
Bflag = 1;
break;
case 'd':
dflag = 1;
break;
case 'f':
afnames = optarg;
break;
#ifndef SMALL
case 'g':
gflag = 1;
break;
#endif
case 'h':
hflag = 1;
break;
case 'I':
iflag = 1;
interface = optarg;
break;
case 'i':
iflag = 1;
break;
case 'L':
Lflag = RT_LFLAG;
break;
case 'l':
lflag = 1;
break;
case 'M':
memf = optarg;
break;
case 'm':
mflag = 1;
break;
case 'N':
nlistf = optarg;
break;
case 'n':
numeric_addr = numeric_port = nflag = RT_NFLAG;
break;
case 'P':
errno = 0;
pcbaddr = strtoul(optarg, &cp, 16);
if (*cp != '\0' || errno == ERANGE)
errx(1, "invalid PCB address %s", optarg);
Pflag = 1;
break;
case 'p':
if ((tp = name2protox(optarg)) == NULL)
errx(1,
"%s: unknown or uninstrumented protocol",
optarg);
pflag = 1;
break;
case 'q':
qflag = 1;
break;
case 'r':
rflag = 1;
break;
case 's':
++sflag;
break;
case 'S':
numeric_addr = 1;
break;
case 't':
tflag = 1;
break;
case 'T':
tagflag = RT_TFLAG;
break;
case 'u':
af = AF_LOCAL;
break;
case 'V':
Vflag++;
break;
case 'v':
vflag = RT_VFLAG;
break;
case 'w':
interval = atoi(optarg);
iflag = 1;
break;
case 'X':
force_sysctl = 1;
break;
case '?':
default:
usage();
}
argv += optind;
argc -= optind;
#define BACKWARD_COMPATIBILITY
#ifdef BACKWARD_COMPATIBILITY
if (*argv) {
if (isdigit((unsigned char)**argv)) {
interval = atoi(*argv);
if (interval <= 0)
usage();
++argv;
iflag = 1;
}
if (*argv) {
nlistf = *argv;
if (*++argv)
memf = *argv;
}
}
#endif
prepare(nlistf, memf, tp);
#ifndef SMALL
if (Bflag) {
if (sflag)
nsbpf_stats();
else
nsbpf_dump(interface);
exit(0);
}
#endif
if (mflag) {
mbpr(nl[N_MBSTAT].n_value, nl[N_MSIZE].n_value,
nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value,
nl[N_MCLPOOL].n_value);
exit(0);
}
if (Pflag) {
if (tp == NULL) {
/* Default to TCP. */
tp = name2protox("tcp");
}
if (tp->pr_dump)
(*tp->pr_dump)(nl[tp->pr_index].n_value, tp->pr_name,
pcbaddr);
else
printf("%s: no PCB dump routine\n", tp->pr_name);
exit(0);
}
if (pflag) {
if (iflag && tp->pr_istats)
intpr(interval, nl[N_IFNET_LIST].n_value,
tp->pr_istats);
else if (tp->pr_stats)
(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
tp->pr_name);
else
printf("%s: no stats routine\n", tp->pr_name);
exit(0);
}
if (qflag) {
print_softintrq();
exit(0);
}
/*
* Keep file descriptors open to avoid overhead
* of open/close on each call to get* routines.
*/
sethostent(1);
setnetent(1);
/*
* If -f was used afnames != NULL, loop over the address families.
* Otherwise do this at least once (with af == AF_UNSPEC).
*/
afname = NULL;
do {
if (afnames != NULL) {
afname = strsep(&afnames, ",");
if (afname == NULL)
break; /* Exit early */
if (strcmp(afname, "inet") == 0)
af = AF_INET;
else if (strcmp(afname, "inet6") == 0)
af = AF_INET6;
else if (strcmp(afname, "arp") == 0)
af = AF_ARP;
else if (strcmp(afname, "pfkey") == 0)
af = PF_KEY;
else if (strcmp(afname, "unix") == 0
|| strcmp(afname, "local") == 0)
af = AF_LOCAL;
else if (strcmp(afname, "atalk") == 0)
af = AF_APPLETALK;
else if (strcmp(afname, "mpls") == 0)
af = AF_MPLS;
else {
warnx("%s: unknown address family",
afname);
continue;
}
}
if (iflag) {
if (af != AF_UNSPEC)
goto protostat;
intpr(interval, nl[N_IFNET_LIST].n_value, NULL);
break;
}
if (rflag) {
if (sflag)
rt_stats(use_sysctl ? 0 :
nl[N_RTSTAT].n_value);
else {
if (use_sysctl)
p_rttables(af,
nflag|tagflag|vflag|Lflag, 0, ~0);
else
routepr(nl[N_RTREE].n_value);
}
break;
}
#ifndef SMALL
if (gflag) {
if (sflag) {
if (af == AF_INET || af == AF_UNSPEC)
mrt_stats(nl[N_MRTPROTO].n_value,
nl[N_MRTSTAT].n_value);
#ifdef INET6
if (af == AF_INET6 || af == AF_UNSPEC)
mrt6_stats(nl[N_MRT6PROTO].n_value,
nl[N_MRT6STAT].n_value);
#endif
}
else {
if (af == AF_INET || af == AF_UNSPEC)
mroutepr(nl[N_MRTPROTO].n_value,
nl[N_MFCHASHTBL].n_value,
nl[N_MFCHASH].n_value,
nl[N_VIFTABLE].n_value);
#ifdef INET6
if (af == AF_INET6 || af == AF_UNSPEC)
mroute6pr(nl[N_MRT6PROTO].n_value,
nl[N_MF6CTABLE].n_value,
nl[N_MIF6TABLE].n_value);
#endif
}
break;
}
#endif
protostat:
if (af == AF_INET || af == AF_UNSPEC) {
setprotoent(1);
setservent(1);
/* ugh, this is O(MN) ... why do we do this? */
while ((p = getprotoent()) != NULL) {
for (tp = protox; tp->pr_name; tp++)
if (strcmp(tp->pr_name, p->p_name) == 0)
break;
if (tp->pr_name == 0 || tp->pr_wanted == 0)
continue;
printproto(tp, p->p_name);
tp->pr_wanted = 0;
}
endprotoent();
for (tp = protox; tp->pr_name; tp++)
if (tp->pr_wanted)
printproto(tp, tp->pr_name);
}
#ifdef INET6
if (af == AF_INET6 || af == AF_UNSPEC)
for (tp = ip6protox; tp->pr_name; tp++)
printproto(tp, tp->pr_name);
#endif
if (af == AF_ARP || af == AF_UNSPEC)
for (tp = arpprotox; tp->pr_name; tp++)
printproto(tp, tp->pr_name);
#ifdef IPSEC
if (af == PF_KEY || af == AF_UNSPEC)
for (tp = pfkeyprotox; tp->pr_name; tp++)
printproto(tp, tp->pr_name);
#endif
#ifndef SMALL
if (af == AF_APPLETALK || af == AF_UNSPEC)
for (tp = atalkprotox; tp->pr_name; tp++)
printproto(tp, tp->pr_name);
if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
unixpr(nl[N_UNIXSW].n_value);
#endif
} while (afnames != NULL && afname != NULL);
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
printproto(struct protox *tp, const char *name)
{
void (*pr)(u_long, const char *);
u_long off;
if (sflag) {
if (iflag) {
if (tp->pr_istats)
intpr(interval, nl[N_IFNET_LIST].n_value,
tp->pr_istats);
return;
}
else {
pr = tp->pr_stats;
off = nl[tp->pr_sindex].n_value;
}
} else {
pr = tp->pr_cblocks;
off = nl[tp->pr_index].n_value;
}
if (pr != NULL && ((off || af != AF_UNSPEC) || use_sysctl))
(*pr)(off, name);
}
/*
* Find the protox for the given "well-known" name.
*/
static struct protox *
knownname(const char *name)
{
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 *
name2protox(const char *name)
{
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.
*/
if ((tp = knownname(name)) != NULL)
return tp;
setprotoent(1); /* make protocol lookup cheaper */
while ((p = getprotoent()) != NULL) {
/* 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;
}