diff options
-rw-r--r-- | usr.bin/netstat/if.c | 18 | ||||
-rw-r--r-- | usr.bin/netstat/main.c | 25 | ||||
-rw-r--r-- | usr.bin/netstat/mroute.c | 127 | ||||
-rw-r--r-- | usr.bin/netstat/mroute6.c | 125 | ||||
-rw-r--r-- | usr.bin/netstat/netstat.h | 7 | ||||
-rw-r--r-- | usr.bin/netstat/show.c | 43 |
6 files changed, 136 insertions, 209 deletions
diff --git a/usr.bin/netstat/if.c b/usr.bin/netstat/if.c index 47ac81ef090..d9a440b87ff 100644 --- a/usr.bin/netstat/if.c +++ b/usr.bin/netstat/if.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if.c,v 1.71 2015/02/08 04:25:56 claudio Exp $ */ +/* $OpenBSD: if.c,v 1.72 2015/02/09 12:25:03 claudio Exp $ */ /* $NetBSD: if.c,v 1.16.4.2 1996/06/07 21:46:46 thorpej Exp $ */ /* @@ -74,7 +74,7 @@ intpr(int interval, int repeatcount) struct if_msghdr ifm; int mib[6] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 }; char name[IFNAMSIZ + 1]; /* + 1 for the '*' */ - char *buf, *next, *lim, *cp; + char *buf = NULL, *next, *lim, *cp; struct rt_msghdr *rtm; struct ifa_msghdr *ifam; struct if_data *ifd; @@ -88,12 +88,7 @@ intpr(int interval, int repeatcount) return; } - if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) - err(1, "sysctl"); - if ((buf = malloc(len)) == NULL) - err(1, NULL); - if (sysctl(mib, 6, buf, &len, NULL, 0) == -1) - err(1, "sysctl"); + len = get_sysctl(mib, 6, &buf); printf("%-7.7s %-5.5s %-11.11s %-17.17s ", "Name", "Mtu", "Network", "Address"); @@ -514,12 +509,7 @@ fetchifs(void) int takeit = 0; int foundone = 0; - if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) - err(1, "sysctl"); - if ((buf = malloc(len)) == NULL) - err(1, NULL); - if (sysctl(mib, 6, buf, &len, NULL, 0) == -1) - err(1, "sysctl"); + len = get_sysctl(mib, 6, &buf); memset(&ip_cur, 0, sizeof(ip_cur)); lim = buf + len; diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c index 5465352a87f..41661ead9f6 100644 --- a/usr.bin/netstat/main.c +++ b/usr.bin/netstat/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.104 2015/02/06 03:22:00 reyk Exp $ */ +/* $OpenBSD: main.c,v 1.105 2015/02/09 12:25:03 claudio Exp $ */ /* $NetBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej Exp $ */ /* @@ -59,18 +59,6 @@ struct nlist nl[] = { #define N_UDBTABLE 1 { "_udbtable" }, -#define N_MFCHASHTBL 2 - { "_mfchashtbl" }, -#define N_MFCHASH 3 - { "_mfchash" }, -#define N_VIFTABLE 4 - { "_viftable" }, - -#define N_MF6CTABLE 5 - { "_mf6ctable" }, -#define N_MIF6TABLE 6 - { "_mif6table" }, - #define N_RTREE 7 { "_rt_tables"}, #define N_RTMASK 8 @@ -331,8 +319,8 @@ main(int argc, char *argv[]) #endif need_nlist = !mflag && (pflag || nlistf != NULL || memf != NULL || - (!iflag && !sflag && (rflag ? Aflag : - (gflag || af != AF_UNIX || Pflag)))); + (!iflag && !sflag && !gflag && (rflag ? Aflag : + (af != AF_UNIX || Pflag)))); /* * Discard setgid privileges if not the running kernel so that bad @@ -393,12 +381,9 @@ main(int argc, char *argv[]) mrt6_stats(); } else { if (af == AF_INET || af == AF_UNSPEC) - mroutepr(nl[N_MFCHASHTBL].n_value, - nl[N_MFCHASH].n_value, - nl[N_VIFTABLE].n_value); + mroutepr(); if (af == AF_INET6 || af == AF_UNSPEC) - mroute6pr(nl[N_MF6CTABLE].n_value, - nl[N_MIF6TABLE].n_value); + mroute6pr(); } exit(0); } diff --git a/usr.bin/netstat/mroute.c b/usr.bin/netstat/mroute.c index 48d5bee3bae..9014996bb76 100644 --- a/usr.bin/netstat/mroute.c +++ b/usr.bin/netstat/mroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mroute.c,v 1.23 2015/01/16 06:40:10 deraadt Exp $ */ +/* $OpenBSD: mroute.c,v 1.24 2015/02/09 12:25:03 claudio Exp $ */ /* $NetBSD: mroute.c,v 1.10 1996/05/11 13:51:27 mycroft Exp $ */ /* @@ -44,58 +44,33 @@ #include <sys/types.h> #include <sys/socket.h> -#include <sys/socketvar.h> -#include <sys/protosw.h> #include <sys/sysctl.h> -#include <net/if.h> -#include <net/route.h> #include <netinet/in.h> #include <netinet/igmp.h> -#define _KERNEL #include <netinet/ip_mroute.h> -#undef _KERNEL #include <err.h> #include <errno.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> +#include <util.h> #include "netstat.h" -static char * -pktscale(u_long n) -{ - static char buf[8]; - char t; - - if (n < 1024) - t = ' '; - else if (n < 1024 * 1024) { - t = 'k'; - n /= 1024; - } else { - t = 'm'; - n /= 1048576; - } - - snprintf(buf, sizeof buf, "%lu%c", n, t); - return (buf); -} - void -mroutepr(u_long mfchashtbladdr, u_long mfchashaddr, u_long vifaddr) +mroutepr(void) { u_int mrtproto; - LIST_HEAD(, mfc) *mfchashtbl; - u_long mfchash; - struct vif viftable[MAXVIFS], *v; - struct mfc *mfcp, mfc; - vifi_t vifi; + struct vifinfo *v; + struct mfcinfo *m; + size_t needed, numvifs, nummfcs, vifi, mfci; + char *buf = NULL; + char fmtbuf[FMT_SCALED_STRSIZE]; + vifi_t maxvif = 0; int mib[] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_MRTPROTO }; size_t len = sizeof(int); - int i, banner_printed = 0, saved_nflag, numvifs = 0; - int nmfc; /* No. of cache entries */ + int banner_printed = 0, saved_nflag; if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), &mrtproto, &len, NULL, 0) == -1) { @@ -114,29 +89,17 @@ mroutepr(u_long mfchashtbladdr, u_long mfchashaddr, u_long vifaddr) return; } - if (mfchashtbladdr == 0) { - printf("mfchashtbl: symbol not in namelist\n"); - return; - } - if (mfchashaddr == 0) { - printf("mfchash: symbol not in namelist\n"); - return; - } - if (vifaddr == 0) { - printf("viftable: symbol not in namelist\n"); - return; - } - saved_nflag = nflag; nflag = 1; - kread(vifaddr, &viftable, sizeof(viftable)); - - for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) { - if (v->v_lcl_addr.s_addr == 0) - continue; - numvifs = vifi; + mib[3] = IPCTL_MRTVIF; + needed = get_sysctl(mib, sizeof(mib) / sizeof(mib[0]), &buf); + numvifs = needed / sizeof(*v); + v = (struct vifinfo *)buf; + if (numvifs) + maxvif = v[numvifs - 1].v_vifi; + for (vifi = 0; vifi < numvifs; ++vifi, ++v) { if (!banner_printed) { printf("\nVirtual Interface Table\n %s%s", "Vif Thresh Limit Local-Address ", @@ -145,7 +108,7 @@ mroutepr(u_long mfchashtbladdr, u_long mfchashaddr, u_long vifaddr) } printf(" %3u %3u %-15.15s", - vifi, v->v_threshold, + v->v_vifi, v->v_threshold, routename4(v->v_lcl_addr.s_addr)); printf(" %-15.15s %6lu %7lu\n", (v->v_flags & VIFF_TUNNEL) ? routename4(v->v_rmt_addr.s_addr) : "", @@ -154,43 +117,37 @@ mroutepr(u_long mfchashtbladdr, u_long mfchashaddr, u_long vifaddr) if (!banner_printed) printf("Virtual Interface Table is empty\n"); - kread(mfchashtbladdr, &mfchashtbl, sizeof(mfchashtbl)); - kread(mfchashaddr, &mfchash, sizeof(mfchash)); banner_printed = 0; - nmfc = 0; - - if (mfchashtbl != 0) - for (i = 0; i <= mfchash; ++i) { - kread((u_long)&mfchashtbl[i], &mfcp, sizeof(mfcp)); - - for (; mfcp != 0; mfcp = LIST_NEXT(&mfc, mfc_hash)) { - if (!banner_printed) { - printf("\nMulticast Forwarding Cache\n %s%s", - "Hash Origin Mcastgroup ", - "Traffic In-Vif Out-Vifs/Forw-ttl\n"); - banner_printed = 1; - } - kread((u_long)mfcp, &mfc, sizeof(mfc)); - printf(" %3u %-15.15s", - i, routename4(mfc.mfc_origin.s_addr)); - printf(" %-15.15s %7s %3u ", - routename4(mfc.mfc_mcastgrp.s_addr), - pktscale(mfc.mfc_pkt_cnt), mfc.mfc_parent); - for (vifi = 0; vifi <= numvifs; ++vifi) - if (mfc.mfc_ttls[vifi]) - printf(" %u/%u", vifi, - mfc.mfc_ttls[vifi]); + mib[3] = IPCTL_MRTMFC; + needed = get_sysctl(mib, sizeof(mib) / sizeof(mib[0]), &buf); + nummfcs = needed / sizeof(*m); + m = (struct mfcinfo *)buf; - printf("\n"); - - nmfc++; - } + for (mfci = 0; mfci < nummfcs; ++mfci, ++m) { + if (!banner_printed) { + printf("\nMulticast Forwarding Cache\n %s%s", + "Hash Origin Mcastgroup ", + "Traffic In-Vif Out-Vifs/Forw-ttl\n"); + banner_printed = 1; } + + printf(" %3zu %-15.15s", + mfci, routename4(m->mfc_origin.s_addr)); + fmt_scaled(m->mfc_pkt_cnt, fmtbuf); + printf(" %-15.15s %7s %3u ", + routename4(m->mfc_mcastgrp.s_addr), + buf, m->mfc_parent); + for (vifi = 0; vifi <= maxvif; ++vifi) + if (m->mfc_ttls[vifi]) + printf(" %zu/%u", vifi, m->mfc_ttls[vifi]); + + printf("\n"); + } if (!banner_printed) printf("Multicast Forwarding Cache is empty\n"); else - printf("\nTotal no. of entries in cache: %d\n", nmfc); + printf("\nTotal no. of entries in cache: %zu\n", nummfcs); printf("\n"); nflag = saved_nflag; diff --git a/usr.bin/netstat/mroute6.c b/usr.bin/netstat/mroute6.c index 55026f8cca0..d841fc1450a 100644 --- a/usr.bin/netstat/mroute6.c +++ b/usr.bin/netstat/mroute6.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mroute6.c,v 1.16 2015/01/17 07:09:50 deraadt Exp $ */ +/* $OpenBSD: mroute6.c,v 1.17 2015/02/09 12:25:03 claudio Exp $ */ /* * Copyright (C) 1998 WIDE Project. @@ -65,37 +65,32 @@ */ #include <sys/types.h> -#include <sys/queue.h> #include <sys/socket.h> -#include <sys/protosw.h> #include <sys/sysctl.h> #include <net/if.h> -#include <net/if_var.h> - #include <netinet/in.h> - -#define _KERNEL #include <netinet6/ip6_mroute.h> -#undef _KERNEL #include <err.h> #include <errno.h> #include <stdio.h> +#include <util.h> #include "netstat.h" #define WID_ORG (lflag ? 39 : (nflag ? 29 : 18)) /* width of origin column */ #define WID_GRP (lflag ? 18 : (nflag ? 16 : 18)) /* width of group column */ void -mroute6pr(u_long mfcaddr, u_long mifaddr) +mroute6pr(void) { - int banner_printed, saved_nflag, waitings, i; - struct mf6c *mf6ctable[MF6CTBLSIZ], *mfcp; - struct mif6 mif6table[MAXMIFS], *mifp; - struct rtdetq rte, *rtep; - mifi_t maxmif = 0, mifi; - struct mf6c mfc; + char *buf; + char fmtbuf[FMT_SCALED_STRSIZE]; + struct mf6cinfo *mfc; + struct mif6info *mif; + size_t needed, mifi, nummifs, mfci, nummfcs; + int banner_printed, saved_nflag; + mifi_t maxmif = 0; u_int mrtproto; int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_MRTPROTO }; size_t len = sizeof(int); @@ -118,29 +113,23 @@ mroute6pr(u_long mfcaddr, u_long mifaddr) return; } - if (mfcaddr == 0) { - printf("mf6ctable: symbol not in namelist\n"); - return; - } - if (mifaddr == 0) { - printf("miftable: symbol not in namelist\n"); - return; - } - saved_nflag = nflag; nflag = 1; - kread(mifaddr, &mif6table, sizeof(mif6table)); + mib[3] = IPV6CTL_MRTMIF; + needed = get_sysctl(mib, sizeof(mib) / sizeof(mib[0]), &buf); + nummifs = needed / sizeof(*mif); + mif = (struct mif6info *)buf; + if (nummifs) + maxmif = mif[nummifs - 1].m6_mifi; + banner_printed = 0; - for (mifi = 0, mifp = mif6table; mifi < MAXMIFS; ++mifi, ++mifp) { - struct ifnet ifnet; + for (mifi = 0; mifi < nummifs; ++mifi, ++mif) { char ifname[IFNAMSIZ]; - if (mifp->m6_ifp == NULL) + if (mif->m6_ifindex == 0) continue; - kread((u_long)mifp->m6_ifp, &ifnet, sizeof(ifnet)); - maxmif = mifi; if (!banner_printed) { printf("\nIPv6 Multicast Interface Table\n" " Mif Rate PhyIF " @@ -149,55 +138,49 @@ mroute6pr(u_long mfcaddr, u_long mifaddr) } printf(" %2u %4d", - mifi, mifp->m6_rate_limit); - printf(" %5s", (mifp->m6_flags & MIFF_REGISTER) ? - "reg0" : if_indextoname(ifnet.if_index, ifname)); + mif->m6_mifi, mif->m6_rate_limit); + printf(" %5s", (mif->m6_flags & MIFF_REGISTER) ? + "reg0" : if_indextoname(mif->m6_ifindex, ifname)); - printf(" %9llu %9llu\n", mifp->m6_pkt_in, mifp->m6_pkt_out); + printf(" %9llu %9llu\n", mif->m6_pkt_in, mif->m6_pkt_out); } if (!banner_printed) printf("IPv6 Multicast Interface Table is empty\n"); - kread(mfcaddr, &mf6ctable, sizeof(mf6ctable)); - banner_printed = 0; - for (i = 0; i < MF6CTBLSIZ; ++i) { - mfcp = mf6ctable[i]; - while (mfcp) { - kread((u_long)mfcp, &mfc, sizeof(mfc)); - if (!banner_printed) { - printf("\nIPv6 Multicast Forwarding Cache\n"); - printf(" %-*.*s %-*.*s %s", - WID_ORG, WID_ORG, "Origin", - WID_GRP, WID_GRP, "Group", - " Packets Waits In-Mif Out-Mifs\n"); - banner_printed = 1; - } - - printf(" %-*.*s", WID_ORG, WID_ORG, - routename6(&mfc.mf6c_origin)); - printf(" %-*.*s", WID_GRP, WID_GRP, - routename6(&mfc.mf6c_mcastgrp)); - printf(" %9llu", mfc.mf6c_pkt_cnt); + mib[3] = IPV6CTL_MRTMFC; + needed = get_sysctl(mib, sizeof(mib) / sizeof(mib[0]), &buf); + nummfcs = needed / sizeof(*mfc); + mfc = (struct mf6cinfo *)buf; - for (waitings = 0, rtep = mfc.mf6c_stall; rtep; ) { - waitings++; - kread((u_long)rtep, &rte, sizeof(rte)); - rtep = rte.next; - } - printf(" %3d", waitings); - - if (mfc.mf6c_parent == MF6C_INCOMPLETE_PARENT) - printf(" --- "); - else - printf(" %3d ", mfc.mf6c_parent); - for (mifi = 0; mifi <= MAXMIFS; mifi++) { - if (IF_ISSET(mifi, &mfc.mf6c_ifset)) - printf(" %u", mifi); - } - printf("\n"); + banner_printed = 0; + for (mfci = 0; mfci < nummfcs; ++mfci, ++mfc) { + if (!banner_printed) { + printf("\nIPv6 Multicast Forwarding Cache\n"); + printf(" %-*.*s %-*.*s %s", + WID_ORG, WID_ORG, "Origin", + WID_GRP, WID_GRP, "Group", + " Packets Waits In-Mif Out-Mifs\n"); + banner_printed = 1; + } - mfcp = mfc.mf6c_next; + printf(" %-*.*s", WID_ORG, WID_ORG, + routename6(&mfc->mf6c_origin)); + printf(" %-*.*s", WID_GRP, WID_GRP, + routename6(&mfc->mf6c_mcastgrp)); + fmt_scaled(mfc->mf6c_pkt_cnt, fmtbuf); + printf(" %9s", fmtbuf); + + printf(" %3llu", mfc->mf6c_stall_cnt); + + if (mfc->mf6c_parent == MF6C_INCOMPLETE_PARENT) + printf(" --- "); + else + printf(" %3d ", mfc->mf6c_parent); + for (mifi = 0; mifi <= MAXMIFS; mifi++) { + if (IF_ISSET(mifi, &mfc->mf6c_ifset)) + printf(" %zu", mifi); } + printf("\n"); } if (!banner_printed) printf("IPv6 Multicast Routing Table is empty"); diff --git a/usr.bin/netstat/netstat.h b/usr.bin/netstat/netstat.h index 539d9775d00..bbc6efd2c00 100644 --- a/usr.bin/netstat/netstat.h +++ b/usr.bin/netstat/netstat.h @@ -1,4 +1,4 @@ -/* $OpenBSD: netstat.h,v 1.66 2013/10/22 16:40:28 guenther Exp $ */ +/* $OpenBSD: netstat.h,v 1.67 2015/02/09 12:25:03 claudio Exp $ */ /* $NetBSD: netstat.h,v 1.6 1996/05/07 02:55:05 thorpej Exp $ */ /* @@ -113,11 +113,12 @@ void icmp6_ifstats(char *); void pim6_stats(char *); void div6_stats(char *); void rip6_stats(char *); -void mroute6pr(u_long, u_long); +void mroute6pr(void); void mrt6_stats(void); char *routename6(struct sockaddr_in6 *); char *netname6(struct sockaddr_in6 *, struct sockaddr_in6 *); +size_t get_sysctl(const int *, u_int, char **); void p_rttables(int, u_int); void p_flags(int, char *); void p_addr(struct sockaddr *, struct sockaddr *, int); @@ -136,5 +137,5 @@ void intpr(int, int); void unixpr(kvm_t *, u_long); -void mroutepr(u_long, u_long, u_long); +void mroutepr(void); void mrt_stats(void); diff --git a/usr.bin/netstat/show.c b/usr.bin/netstat/show.c index ae539f97a14..c6bdf2522df 100644 --- a/usr.bin/netstat/show.c +++ b/usr.bin/netstat/show.c @@ -1,4 +1,4 @@ -/* $OpenBSD: show.c,v 1.45 2015/02/06 03:22:00 reyk Exp $ */ +/* $OpenBSD: show.c,v 1.46 2015/02/09 12:25:03 claudio Exp $ */ /* $NetBSD: show.c,v 1.1 1996/11/15 18:01:41 gwr Exp $ */ /* @@ -103,6 +103,29 @@ void p_flags(int, char *); char *routename4(in_addr_t); char *routename6(struct sockaddr_in6 *); +size_t +get_sysctl(const int *mib, u_int mcnt, char **buf) +{ + size_t needed; + + while (1) { + if (sysctl(mib, mcnt, NULL, &needed, NULL, 0) == -1) + err(1, "sysctl-estimate"); + if (needed == 0) + break; + if ((*buf = realloc(*buf, needed)) == NULL) + err(1, NULL); + if (sysctl(mib, mcnt, *buf, &needed, NULL, 0) == -1) { + if (errno == ENOMEM) + continue; + err(1, "sysctl"); + } + break; + } + + return needed; +} + /* * Print routing tables. */ @@ -123,21 +146,9 @@ p_rttables(int af, u_int tableid) mib[5] = 0; mib[6] = tableid; mcnt = 7; - while (1) { - if (sysctl(mib, mcnt, NULL, &needed, NULL, 0) == -1) - err(1, "route-sysctl-estimate"); - if (needed == 0) - break; - if ((buf = realloc(buf, needed)) == NULL) - err(1, NULL); - if (sysctl(mib, mcnt, buf, &needed, NULL, 0) == -1) { - if (errno == ENOMEM) - continue; - err(1, "sysctl of routing table"); - } - lim = buf + needed; - break; - } + + needed = get_sysctl(mib, mcnt, &buf); + lim = buf + needed; printf("Routing tables\n"); |