summaryrefslogtreecommitdiffstats
path: root/usr.bin
diff options
context:
space:
mode:
authorremi <remi@openbsd.org>2020-06-12 06:22:31 +0000
committerremi <remi@openbsd.org>2020-06-12 06:22:31 +0000
commitb7ee87a3ee73ce8cba6cd4549206226ad6c537e9 (patch)
tree0c8225dda0ed92d9376c68e90aa5ad67b603eaab /usr.bin
parentremove unused mcx_softc members. (diff)
downloadwireguard-openbsd-b7ee87a3ee73ce8cba6cd4549206226ad6c537e9.tar.xz
wireguard-openbsd-b7ee87a3ee73ce8cba6cd4549206226ad6c537e9.zip
Add -R to show a summary of rdomains with associated interfaces and tables.
My first version also displayed the number of routes per table. But duming all routing tables to count the entries in userland is expensive. Once the kernel can export these counters the numbers can be added to the output of -R. OK benno@ previous version OK sthen@
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/netstat/main.c12
-rw-r--r--usr.bin/netstat/netstat.18
-rw-r--r--usr.bin/netstat/netstat.h5
-rw-r--r--usr.bin/netstat/route.c74
4 files changed, 93 insertions, 6 deletions
diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c
index 13f14020332..f2eaf054213 100644
--- a/usr.bin/netstat/main.c
+++ b/usr.bin/netstat/main.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: main.c,v 1.116 2019/04/28 17:59:51 mpi Exp $ */
+/* $OpenBSD: main.c,v 1.117 2020/06/12 06:22:31 remi Exp $ */
/* $NetBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej Exp $ */
/*
@@ -127,7 +127,7 @@ main(int argc, char *argv[])
tableid = getrtable();
while ((ch = getopt(argc, argv,
- "AaBbc:deFf:ghI:iLlM:mN:np:P:qrsT:tuvW:w:")) != -1)
+ "AaBbc:deFf:ghI:iLlM:mN:np:P:qRrsT:tuvW:w:")) != -1)
switch (ch) {
case 'A':
Aflag = 1;
@@ -225,6 +225,9 @@ main(int argc, char *argv[])
case 'q':
qflag = 1;
break;
+ case 'R':
+ Rflag = 1;
+ break;
case 'r':
rflag = 1;
break;
@@ -321,6 +324,11 @@ main(int argc, char *argv[])
exit(0);
}
+ if (Rflag) {
+ rdomainpr();
+ exit(0);
+ }
+
/*
* The remaining code may need kvm so lets try to open it.
* -r and -P are the only bits left that actually can use this.
diff --git a/usr.bin/netstat/netstat.1 b/usr.bin/netstat/netstat.1
index aa877f521a5..10240bd715e 100644
--- a/usr.bin/netstat/netstat.1
+++ b/usr.bin/netstat/netstat.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: netstat.1,v 1.86 2019/04/17 20:34:21 jmc Exp $
+.\" $OpenBSD: netstat.1,v 1.87 2020/06/12 06:22:32 remi Exp $
.\" $NetBSD: netstat.1,v 1.11 1995/10/03 21:42:43 thorpej Exp $
.\"
.\" Copyright (c) 1983, 1990, 1992, 1993
@@ -30,7 +30,7 @@
.\"
.\" from: @(#)netstat.1 8.8 (Berkeley) 4/18/94
.\"
-.Dd $Mdocdate: April 17 2019 $
+.Dd $Mdocdate: June 12 2020 $
.Dt NETSTAT 1
.Os
.Sh NAME
@@ -74,6 +74,8 @@
.Op Fl i | I Ar interface
.Nm
.Op Fl W Ar interface
+.Nm
+.Op Fl R
.Sh DESCRIPTION
The
.Nm
@@ -267,6 +269,8 @@ Otherwise the states of the matching sockets are shown.
Only show interfaces that have seen packets (or bytes if
.Fl b
is specified).
+.It Fl R
+List all rdomains with associated interfaces and routing tables.
.It Fl r
Show the routing tables.
The output is explained in more detail below.
diff --git a/usr.bin/netstat/netstat.h b/usr.bin/netstat/netstat.h
index 96bc6f3f8cb..c5c8681fcd3 100644
--- a/usr.bin/netstat/netstat.h
+++ b/usr.bin/netstat/netstat.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: netstat.h,v 1.74 2019/04/28 17:59:51 mpi Exp $ */
+/* $OpenBSD: netstat.h,v 1.75 2020/06/12 06:22:32 remi Exp $ */
/* $NetBSD: netstat.h,v 1.6 1996/05/07 02:55:05 thorpej Exp $ */
/*
@@ -57,6 +57,7 @@ int pflag; /* show given protocol */
int Pflag; /* show given PCB */
int qflag; /* only display non-zero values for output */
int rflag; /* show routing tables (or routing stats) */
+int Rflag; /* show rdomain and rtable summary */
int sflag; /* show protocol statistics */
int tflag; /* show i/f watchdog timers */
int vflag; /* be verbose */
@@ -113,6 +114,8 @@ void pr_rthdr(int, int);
void pr_encaphdr(void);
void pr_family(int);
+void rdomainpr(void);
+
void ip6_stats(char *);
void icmp6_stats(char *);
void pim6_stats(char *);
diff --git a/usr.bin/netstat/route.c b/usr.bin/netstat/route.c
index b1e8e1acda4..9e8e22ba54b 100644
--- a/usr.bin/netstat/route.c
+++ b/usr.bin/netstat/route.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: route.c,v 1.104 2019/06/28 13:35:02 deraadt Exp $ */
+/* $OpenBSD: route.c,v 1.105 2020/06/12 06:22:32 remi Exp $ */
/* $NetBSD: route.c,v 1.15 1996/05/07 02:55:06 thorpej Exp $ */
/*
@@ -51,6 +51,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <ifaddrs.h>
#include "netstat.h"
@@ -347,3 +348,74 @@ rt_stats(void)
printf("\t%u use%s of a wildcard route\n",
rtstat.rts_wildcard, plural(rtstat.rts_wildcard));
}
+
+/*
+ * Print rdomain and rtable summary
+ */
+
+void
+rdomainpr(void)
+{
+ struct if_data *ifd;
+ struct ifaddrs *ifap, *ifa;
+ struct rt_tableinfo info;
+
+ int rtt_dom[RT_TABLEID_MAX+1];
+ int rdom_rttcnt[RT_TABLEID_MAX+1] = { };
+ int mib[6], rdom, rtt;
+ size_t len;
+ char *old, *rdom_if[RT_TABLEID_MAX+1] = { };
+
+ getifaddrs(&ifap);
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ if (ifa->ifa_addr->sa_family != AF_LINK)
+ continue;
+ ifd = ifa->ifa_data;
+ if (rdom_if[ifd->ifi_rdomain] == NULL) {
+ if (asprintf(&rdom_if[ifd->ifi_rdomain], "%s",
+ ifa->ifa_name) == -1)
+ exit(1);
+ } else {
+ old = rdom_if[ifd->ifi_rdomain];
+ if (asprintf(&rdom_if[ifd->ifi_rdomain], "%s %s",
+ old, ifa->ifa_name) == -1)
+ exit(1);
+ free(old);
+ }
+ }
+ freeifaddrs(ifap);
+
+ mib[0] = CTL_NET;
+ mib[1] = PF_ROUTE;
+ mib[2] = 0;
+ mib[3] = 0;
+ mib[4] = NET_RT_TABLE;
+
+ len = sizeof(info);
+ for (rtt = 0; rtt <= RT_TABLEID_MAX; rtt++) {
+ mib[5] = rtt;
+ if (sysctl(mib, 6, &info, &len, NULL, 0) == -1)
+ rtt_dom[rtt] = -1;
+ else {
+ rtt_dom[rtt] = info.rti_domainid;
+ rdom_rttcnt[info.rti_domainid]++;
+ }
+ }
+
+ for (rdom = 0; rdom <= RT_TABLEID_MAX; rdom++) {
+ if (rdom_if[rdom] == NULL)
+ continue;
+ printf("Rdomain %i\n", rdom);
+ printf(" Interface%s %s\n",
+ (strchr(rdom_if[rdom], ' ') == NULL) ? ":" : "s:",
+ rdom_if[rdom]);
+ printf(" Routing table%s",
+ (rdom_rttcnt[rdom] == 1) ? ":" : "s:");
+ for (rtt = 0; rtt <= RT_TABLEID_MAX; rtt++) {
+ if (rtt_dom[rtt] == rdom)
+ printf(" %i", rtt);
+ }
+ printf("\n\n");
+ free(rdom_if[rdom]);
+ }
+}