165475bc8SDavid E. O'Brien /*- 2*526b18aaSGleb Smirnoff * Copyright (c) 2013 Gleb Smirnoff <glebius@FreeBSD.org> 39b50d902SRodney W. Grimes * Copyright (c) 1983, 1988, 1993 49b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 59b50d902SRodney W. Grimes * 69b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 79b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 89b50d902SRodney W. Grimes * are met: 99b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 109b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 119b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 129b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 139b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 149b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 159b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 169b50d902SRodney W. Grimes * without specific prior written permission. 179b50d902SRodney W. Grimes * 189b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 199b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 209b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 219b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 229b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 239b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 249b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 259b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 269b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 279b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 289b50d902SRodney W. Grimes * SUCH DAMAGE. 299b50d902SRodney W. Grimes */ 309b50d902SRodney W. Grimes 316cc6f122SPhilippe Charnier #if 0 329b50d902SRodney W. Grimes #ifndef lint 3305ddff6eSPeter Wemm static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95"; 349b50d902SRodney W. Grimes #endif /* not lint */ 356cc6f122SPhilippe Charnier #endif 366cc6f122SPhilippe Charnier 376cc6f122SPhilippe Charnier #include <sys/cdefs.h> 386cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$"); 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #include <sys/types.h> 419b50d902SRodney W. Grimes #include <sys/protosw.h> 429b50d902SRodney W. Grimes #include <sys/socket.h> 43feda1a43SJohn Baldwin #include <sys/socketvar.h> 440024d1dbSLuigi Rizzo #include <sys/sysctl.h> 45628d2ac1SGarrett Wollman #include <sys/time.h> 469b50d902SRodney W. Grimes 479b50d902SRodney W. Grimes #include <net/if.h> 4887669425SGarrett Wollman #include <net/if_var.h> 499b50d902SRodney W. Grimes #include <net/if_dl.h> 50f6719675SBill Fenner #include <net/if_types.h> 5175fb8770SGarrett Wollman #include <net/ethernet.h> 529b50d902SRodney W. Grimes #include <netinet/in.h> 539b50d902SRodney W. Grimes #include <netinet/in_var.h> 54cc6a66f2SJulian Elischer #include <netipx/ipx.h> 55cc6a66f2SJulian Elischer #include <netipx/ipx_if.h> 569b50d902SRodney W. Grimes #include <arpa/inet.h> 573e4d5cd3SGleb Smirnoff #ifdef PF 583e4d5cd3SGleb Smirnoff #include <net/pfvar.h> 593e4d5cd3SGleb Smirnoff #include <net/if_pfsync.h> 603e4d5cd3SGleb Smirnoff #endif 619b50d902SRodney W. Grimes 622e37c5a3SMax Laier #include <err.h> 632e37c5a3SMax Laier #include <errno.h> 6484c1edcbSGleb Smirnoff #include <ifaddrs.h> 65c2dfd19fSGleb Smirnoff #include <libutil.h> 66cd05232aSHajimu UMEMOTO #ifdef INET6 67cd05232aSHajimu UMEMOTO #include <netdb.h> 68cd05232aSHajimu UMEMOTO #endif 699b50d902SRodney W. Grimes #include <signal.h> 7084c1edcbSGleb Smirnoff #include <stdbool.h> 717b95a1ebSYaroslav Tykhiy #include <stdint.h> 729b50d902SRodney W. Grimes #include <stdio.h> 73591c194aSGuido van Rooij #include <stdlib.h> 749b50d902SRodney W. Grimes #include <string.h> 7584c1edcbSGleb Smirnoff #include <sysexits.h> 76821df508SXin LI #include <unistd.h> 779b50d902SRodney W. Grimes 789b50d902SRodney W. Grimes #include "netstat.h" 799b50d902SRodney W. Grimes 8084c1edcbSGleb Smirnoff static void sidewaysintpr(int); 819b50d902SRodney W. Grimes 82cfa1ca9dSYoshinobu Inoue #ifdef INET6 83cd05232aSHajimu UMEMOTO static char addr_buf[NI_MAXHOST]; /* for getnameinfo() */ 84cfa1ca9dSYoshinobu Inoue #endif 85cfa1ca9dSYoshinobu Inoue 863e4d5cd3SGleb Smirnoff #ifdef PF 87d6d3f01eSGleb Smirnoff static const char* pfsyncacts[] = { 88d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_CLR */ "clear all request", 89d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_INS */ "state insert", 90d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_INS_ACK */ "state inserted ack", 91d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_UPD */ "state update", 92d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_UPD_C */ "compressed state update", 93d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_UPD_REQ */ "uncompressed state request", 94d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_DEL */ "state delete", 95d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_DEL_C */ "compressed state delete", 96d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_INS_F */ "fragment insert", 97d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_DEL_F */ "fragment delete", 98d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_BUS */ "bulk update mark", 99d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_TDB */ "TDB replay counter update", 100d6d3f01eSGleb Smirnoff /* PFSYNC_ACT_EOF */ "end of frame mark", 101d6d3f01eSGleb Smirnoff }; 102d6d3f01eSGleb Smirnoff 103d6d3f01eSGleb Smirnoff static void 104d6d3f01eSGleb Smirnoff pfsync_acts_stats(const char *fmt, uint64_t *a) 105d6d3f01eSGleb Smirnoff { 106d6d3f01eSGleb Smirnoff int i; 107d6d3f01eSGleb Smirnoff 108d6d3f01eSGleb Smirnoff for (i = 0; i < PFSYNC_ACT_MAX; i++, a++) 109d6d3f01eSGleb Smirnoff if (*a || sflag <= 1) 110d6d3f01eSGleb Smirnoff printf(fmt, *a, pfsyncacts[i], plural(*a)); 111d6d3f01eSGleb Smirnoff } 112d6d3f01eSGleb Smirnoff 1132e37c5a3SMax Laier /* 1142e37c5a3SMax Laier * Dump pfsync statistics structure. 1152e37c5a3SMax Laier */ 1162e37c5a3SMax Laier void 117feda1a43SJohn Baldwin pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 1182e37c5a3SMax Laier { 1192e37c5a3SMax Laier struct pfsyncstats pfsyncstat, zerostat; 1202e37c5a3SMax Laier size_t len = sizeof(struct pfsyncstats); 121445f17bbSJosef Karthauser 122feda1a43SJohn Baldwin if (live) { 1232e37c5a3SMax Laier if (zflag) 1242e37c5a3SMax Laier memset(&zerostat, 0, len); 12516410af7SGleb Smirnoff if (sysctlbyname("net.pfsync.stats", &pfsyncstat, &len, 1262e37c5a3SMax Laier zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 1272e37c5a3SMax Laier if (errno != ENOENT) 12816410af7SGleb Smirnoff warn("sysctl: net.pfsync.stats"); 1292e37c5a3SMax Laier return; 1302e37c5a3SMax Laier } 131feda1a43SJohn Baldwin } else 132feda1a43SJohn Baldwin kread(off, &pfsyncstat, len); 133445f17bbSJosef Karthauser 1342e37c5a3SMax Laier printf("%s:\n", name); 1352e37c5a3SMax Laier 1362e37c5a3SMax Laier #define p(f, m) if (pfsyncstat.f || sflag <= 1) \ 1377b95a1ebSYaroslav Tykhiy printf(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f)) 1382e37c5a3SMax Laier 1397b95a1ebSYaroslav Tykhiy p(pfsyncs_ipackets, "\t%ju packet%s received (IPv4)\n"); 1407b95a1ebSYaroslav Tykhiy p(pfsyncs_ipackets6, "\t%ju packet%s received (IPv6)\n"); 141d6d3f01eSGleb Smirnoff pfsync_acts_stats("\t %ju %s%s received\n", 142d6d3f01eSGleb Smirnoff &pfsyncstat.pfsyncs_iacts[0]); 1437b95a1ebSYaroslav Tykhiy p(pfsyncs_badif, "\t\t%ju packet%s discarded for bad interface\n"); 1447b95a1ebSYaroslav Tykhiy p(pfsyncs_badttl, "\t\t%ju packet%s discarded for bad ttl\n"); 1457b95a1ebSYaroslav Tykhiy p(pfsyncs_hdrops, "\t\t%ju packet%s shorter than header\n"); 1467b95a1ebSYaroslav Tykhiy p(pfsyncs_badver, "\t\t%ju packet%s discarded for bad version\n"); 1477b95a1ebSYaroslav Tykhiy p(pfsyncs_badauth, "\t\t%ju packet%s discarded for bad HMAC\n"); 1487b95a1ebSYaroslav Tykhiy p(pfsyncs_badact,"\t\t%ju packet%s discarded for bad action\n"); 1497b95a1ebSYaroslav Tykhiy p(pfsyncs_badlen, "\t\t%ju packet%s discarded for short packet\n"); 1507b95a1ebSYaroslav Tykhiy p(pfsyncs_badval, "\t\t%ju state%s discarded for bad values\n"); 1517b95a1ebSYaroslav Tykhiy p(pfsyncs_stale, "\t\t%ju stale state%s\n"); 1527b95a1ebSYaroslav Tykhiy p(pfsyncs_badstate, "\t\t%ju failed state lookup/insert%s\n"); 1537b95a1ebSYaroslav Tykhiy p(pfsyncs_opackets, "\t%ju packet%s sent (IPv4)\n"); 1547b95a1ebSYaroslav Tykhiy p(pfsyncs_opackets6, "\t%ju packet%s sent (IPv6)\n"); 155d6d3f01eSGleb Smirnoff pfsync_acts_stats("\t %ju %s%s sent\n", 156d6d3f01eSGleb Smirnoff &pfsyncstat.pfsyncs_oacts[0]); 157d6d3f01eSGleb Smirnoff p(pfsyncs_onomem, "\t\t%ju failure%s due to mbuf memory error\n"); 158d6d3f01eSGleb Smirnoff p(pfsyncs_oerrors, "\t\t%ju send error%s\n"); 1592e37c5a3SMax Laier #undef p 1602e37c5a3SMax Laier } 1613e4d5cd3SGleb Smirnoff #endif /* PF */ 162445f17bbSJosef Karthauser 163445f17bbSJosef Karthauser /* 164445f17bbSJosef Karthauser * Display a formatted value, or a '-' in the same space. 165445f17bbSJosef Karthauser */ 1665e051718SAssar Westerlund static void 167f964d60dSAssar Westerlund show_stat(const char *fmt, int width, u_long value, short showvalue) 168445f17bbSJosef Karthauser { 1691f575ce8SBruce Evans const char *lsep, *rsep; 170445f17bbSJosef Karthauser char newfmt[32]; 171445f17bbSJosef Karthauser 1721f575ce8SBruce Evans lsep = ""; 1731f575ce8SBruce Evans if (strncmp(fmt, "LS", 2) == 0) { 1741f575ce8SBruce Evans lsep = " "; 1751f575ce8SBruce Evans fmt += 2; 1761f575ce8SBruce Evans } 1771f575ce8SBruce Evans rsep = " "; 1781f575ce8SBruce Evans if (strncmp(fmt, "NRS", 3) == 0) { 1791f575ce8SBruce Evans rsep = ""; 1801f575ce8SBruce Evans fmt += 3; 1811f575ce8SBruce Evans } 182c2dfd19fSGleb Smirnoff if (showvalue == 0) { 183c2dfd19fSGleb Smirnoff /* Print just dash. */ 1841f575ce8SBruce Evans sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep); 185445f17bbSJosef Karthauser printf(newfmt, "-"); 186c2dfd19fSGleb Smirnoff return; 187445f17bbSJosef Karthauser } 188445f17bbSJosef Karthauser 189c2dfd19fSGleb Smirnoff if (hflag) { 190c2dfd19fSGleb Smirnoff char buf[5]; 191445f17bbSJosef Karthauser 192c2dfd19fSGleb Smirnoff /* Format in human readable form. */ 193c2dfd19fSGleb Smirnoff humanize_number(buf, sizeof(buf), (int64_t)value, "", 194c2dfd19fSGleb Smirnoff HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL); 1951f575ce8SBruce Evans sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep); 196c2dfd19fSGleb Smirnoff printf(newfmt, buf); 197c2dfd19fSGleb Smirnoff } else { 198c2dfd19fSGleb Smirnoff /* Construct the format string. */ 1991f575ce8SBruce Evans sprintf(newfmt, "%s%%%d%s%s", lsep, width, fmt, rsep); 200c2dfd19fSGleb Smirnoff printf(newfmt, value); 201c2dfd19fSGleb Smirnoff } 202c2dfd19fSGleb Smirnoff } 203445f17bbSJosef Karthauser 2049b50d902SRodney W. Grimes /* 20584c1edcbSGleb Smirnoff * Find next multiaddr for a given interface name. 20684c1edcbSGleb Smirnoff */ 20784c1edcbSGleb Smirnoff static struct ifmaddrs * 20884c1edcbSGleb Smirnoff next_ifma(struct ifmaddrs *ifma, const char *name, const sa_family_t family) 20984c1edcbSGleb Smirnoff { 21084c1edcbSGleb Smirnoff 21184c1edcbSGleb Smirnoff for(; ifma != NULL; ifma = ifma->ifma_next) { 21284c1edcbSGleb Smirnoff struct sockaddr_dl *sdl; 21384c1edcbSGleb Smirnoff 21484c1edcbSGleb Smirnoff sdl = (struct sockaddr_dl *)ifma->ifma_name; 21584c1edcbSGleb Smirnoff if (ifma->ifma_addr->sa_family == family && 21684c1edcbSGleb Smirnoff strcmp(sdl->sdl_data, name) == 0) 21784c1edcbSGleb Smirnoff break; 21884c1edcbSGleb Smirnoff } 21984c1edcbSGleb Smirnoff 22084c1edcbSGleb Smirnoff return (ifma); 22184c1edcbSGleb Smirnoff } 22284c1edcbSGleb Smirnoff 22384c1edcbSGleb Smirnoff /* 2249b50d902SRodney W. Grimes * Print a description of the network interfaces. 2259b50d902SRodney W. Grimes */ 2269b50d902SRodney W. Grimes void 227fc47e028SAlexander V. Chernikov intpr(int interval, void (*pfunc)(char *), int af) 2289b50d902SRodney W. Grimes { 22984c1edcbSGleb Smirnoff struct ifaddrs *ifap, *ifa; 23084c1edcbSGleb Smirnoff struct ifmaddrs *ifmap, *ifma; 2319b50d902SRodney W. Grimes 23284c1edcbSGleb Smirnoff if (interval) 23384c1edcbSGleb Smirnoff return sidewaysintpr(interval); 23484c1edcbSGleb Smirnoff 23584c1edcbSGleb Smirnoff if (getifaddrs(&ifap) != 0) 23684c1edcbSGleb Smirnoff err(EX_OSERR, "getifaddrs"); 23784c1edcbSGleb Smirnoff if (aflag && getifmaddrs(&ifmap) != 0) 23884c1edcbSGleb Smirnoff err(EX_OSERR, "getifmaddrs"); 2390e27dc05SGarrett Wollman 240cf5e44f8SRuslan Ermilov if (!pfunc) { 24125d295e1SBruce M Simpson if (Wflag) 24225d295e1SBruce M Simpson printf("%-7.7s", "Name"); 24325d295e1SBruce M Simpson else 24425d295e1SBruce M Simpson printf("%-5.5s", "Name"); 245d72dc9a7SAttilio Rao printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s %5.5s", 246d72dc9a7SAttilio Rao "Mtu", "Network", "Address", "Ipkts", "Ierrs", "Idrop"); 247e1e293a5SDavid Greenman if (bflag) 248e1e293a5SDavid Greenman printf(" %10.10s","Ibytes"); 249e1e293a5SDavid Greenman printf(" %8.8s %5.5s", "Opkts", "Oerrs"); 250e1e293a5SDavid Greenman if (bflag) 251e1e293a5SDavid Greenman printf(" %10.10s","Obytes"); 2529b50d902SRodney W. Grimes printf(" %5s", "Coll"); 2539b50d902SRodney W. Grimes if (dflag) 2549b50d902SRodney W. Grimes printf(" %s", "Drop"); 2559b50d902SRodney W. Grimes putchar('\n'); 256cfa1ca9dSYoshinobu Inoue } 2579b50d902SRodney W. Grimes 25884c1edcbSGleb Smirnoff for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 25984c1edcbSGleb Smirnoff bool network = false, link = false; 260445f17bbSJosef Karthauser 26184c1edcbSGleb Smirnoff if (interface != NULL && strcmp(ifa->ifa_name, interface) != 0) 2629b50d902SRodney W. Grimes continue; 263cfa1ca9dSYoshinobu Inoue 264cfa1ca9dSYoshinobu Inoue if (pfunc) { 26584c1edcbSGleb Smirnoff char *name; 266cfa1ca9dSYoshinobu Inoue 26784c1edcbSGleb Smirnoff name = ifa->ifa_name; 26884c1edcbSGleb Smirnoff (*pfunc)(name); 2695da9f8faSJosef Karthauser 2705da9f8faSJosef Karthauser /* 27184c1edcbSGleb Smirnoff * Skip all ifaddrs belonging to same interface. 2725da9f8faSJosef Karthauser */ 27384c1edcbSGleb Smirnoff while(ifa->ifa_next != NULL && 27484c1edcbSGleb Smirnoff (strcmp(ifa->ifa_next->ifa_name, name) == 0)) { 27584c1edcbSGleb Smirnoff ifa = ifa->ifa_next; 27684c1edcbSGleb Smirnoff } 27784c1edcbSGleb Smirnoff continue; 27884c1edcbSGleb Smirnoff } 2795da9f8faSJosef Karthauser 28084c1edcbSGleb Smirnoff if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af) 2819b50d902SRodney W. Grimes continue; 28284c1edcbSGleb Smirnoff 28325d295e1SBruce M Simpson if (Wflag) 28484c1edcbSGleb Smirnoff printf("%-7.7s", ifa->ifa_name); 28525d295e1SBruce M Simpson else 28684c1edcbSGleb Smirnoff printf("%-5.5s", ifa->ifa_name); 28784c1edcbSGleb Smirnoff 28884c1edcbSGleb Smirnoff #define IFA_MTU(ifa) (((struct if_data *)(ifa)->ifa_data)->ifi_mtu) 28984c1edcbSGleb Smirnoff show_stat("lu", 6, IFA_MTU(ifa), IFA_MTU(ifa)); 29084c1edcbSGleb Smirnoff #undef IFA_MTU 29184c1edcbSGleb Smirnoff 29284c1edcbSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 2939b50d902SRodney W. Grimes case AF_UNSPEC: 294cc6a66f2SJulian Elischer printf("%-13.13s ", "none"); 2959b50d902SRodney W. Grimes printf("%-15.15s ", "none"); 2969b50d902SRodney W. Grimes break; 2979b50d902SRodney W. Grimes case AF_INET: 29884c1edcbSGleb Smirnoff { 29984c1edcbSGleb Smirnoff struct sockaddr_in *sin, *mask; 300445f17bbSJosef Karthauser 30184c1edcbSGleb Smirnoff sin = (struct sockaddr_in *)ifa->ifa_addr; 30284c1edcbSGleb Smirnoff mask = (struct sockaddr_in *)ifa->ifa_netmask; 30384c1edcbSGleb Smirnoff printf("%-13.13s ", netname(sin->sin_addr.s_addr, 30484c1edcbSGleb Smirnoff mask->sin_addr.s_addr)); 30584c1edcbSGleb Smirnoff printf("%-17.17s ", 30684c1edcbSGleb Smirnoff routename(sin->sin_addr.s_addr)); 30784c1edcbSGleb Smirnoff 30884c1edcbSGleb Smirnoff network = true; 3099b50d902SRodney W. Grimes break; 31084c1edcbSGleb Smirnoff } 311cfa1ca9dSYoshinobu Inoue #ifdef INET6 312cfa1ca9dSYoshinobu Inoue case AF_INET6: 31384c1edcbSGleb Smirnoff { 31484c1edcbSGleb Smirnoff struct sockaddr_in6 *sin6, *mask; 31584c1edcbSGleb Smirnoff 31684c1edcbSGleb Smirnoff sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 31784c1edcbSGleb Smirnoff mask = (struct sockaddr_in6 *)ifa->ifa_netmask; 31884c1edcbSGleb Smirnoff 31984c1edcbSGleb Smirnoff printf("%-13.13s ", netname6(sin6, &mask->sin6_addr)); 32084c1edcbSGleb Smirnoff getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len, 32184c1edcbSGleb Smirnoff addr_buf, sizeof(addr_buf), 0, 0, NI_NUMERICHOST); 322cd05232aSHajimu UMEMOTO printf("%-17.17s ", addr_buf); 323445f17bbSJosef Karthauser 32484c1edcbSGleb Smirnoff network = 1; 325cfa1ca9dSYoshinobu Inoue break; 32684c1edcbSGleb Smirnoff } 327cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 328cc6a66f2SJulian Elischer case AF_IPX: 329cc6a66f2SJulian Elischer { 33084c1edcbSGleb Smirnoff struct sockaddr_ipx *sipx; 331cc6a66f2SJulian Elischer u_long net; 332cc6a66f2SJulian Elischer char netnum[10]; 333cc6a66f2SJulian Elischer 33484c1edcbSGleb Smirnoff sipx = (struct sockaddr_ipx *)ifa->ifa_addr; 335cc6a66f2SJulian Elischer *(union ipx_net *) &net = sipx->sipx_addr.x_net; 33684c1edcbSGleb Smirnoff 33722694ebaSBruce Evans sprintf(netnum, "%lx", (u_long)ntohl(net)); 338cc6a66f2SJulian Elischer printf("ipx:%-8s ", netnum); 33984c1edcbSGleb Smirnoff printf("%-17s ", ipx_phost((struct sockaddr *)sipx)); 3406f9cdfceSMatthew N. Dodd 34184c1edcbSGleb Smirnoff network = 1; 342cc6a66f2SJulian Elischer break; 34384c1edcbSGleb Smirnoff } 3446ffcfd6cSJulian Elischer case AF_APPLETALK: 34584c1edcbSGleb Smirnoff printf("atalk:%-12.12s ", 34684c1edcbSGleb Smirnoff atalk_print(ifa->ifa_addr, 0x10)); 34784c1edcbSGleb Smirnoff printf("%-11.11s ", 34884c1edcbSGleb Smirnoff atalk_print(ifa->ifa_addr, 0x0b)); 3496ffcfd6cSJulian Elischer break; 3509b50d902SRodney W. Grimes case AF_LINK: 3519b50d902SRodney W. Grimes { 35284c1edcbSGleb Smirnoff struct sockaddr_dl *sdl; 35384c1edcbSGleb Smirnoff char *cp, linknum[10]; 35484c1edcbSGleb Smirnoff int n, m; 35584c1edcbSGleb Smirnoff 35684c1edcbSGleb Smirnoff sdl = (struct sockaddr_dl *)ifa->ifa_addr; 3579b50d902SRodney W. Grimes cp = (char *)LLADDR(sdl); 3589b50d902SRodney W. Grimes n = sdl->sdl_alen; 359cfa1ca9dSYoshinobu Inoue sprintf(linknum, "<Link#%d>", sdl->sdl_index); 360acb60e59SRuslan Ermilov m = printf("%-13.13s ", linknum); 36184c1edcbSGleb Smirnoff 36262372898SMichael Tuexen while ((--n >= 0) && (m < 30)) 363541f2562SDavid Greenman m += printf("%02x%c", *cp++ & 0xff, 364e54ca68cSJordan K. Hubbard n > 0 ? ':' : ' '); 365acb60e59SRuslan Ermilov m = 32 - m; 3669b50d902SRodney W. Grimes while (m-- > 0) 3679b50d902SRodney W. Grimes putchar(' '); 368445f17bbSJosef Karthauser 36984c1edcbSGleb Smirnoff link = 1; 3709b50d902SRodney W. Grimes break; 3719b50d902SRodney W. Grimes } 3725da9f8faSJosef Karthauser } 3735da9f8faSJosef Karthauser 37484c1edcbSGleb Smirnoff #define IFA_STAT(s) (((struct if_data *)ifa->ifa_data)->ifi_ ## s) 37584c1edcbSGleb Smirnoff show_stat("lu", 8, IFA_STAT(ipackets), link|network); 37684c1edcbSGleb Smirnoff show_stat("lu", 5, IFA_STAT(ierrors), link); 37784c1edcbSGleb Smirnoff show_stat("lu", 5, IFA_STAT(iqdrops), link); 3787c23a867SGleb Smirnoff if (bflag) 37984c1edcbSGleb Smirnoff show_stat("lu", 10, IFA_STAT(ibytes), link|network); 38084c1edcbSGleb Smirnoff show_stat("lu", 8, IFA_STAT(opackets), link|network); 38184c1edcbSGleb Smirnoff show_stat("lu", 5, IFA_STAT(oerrors), link); 3827c23a867SGleb Smirnoff if (bflag) 38384c1edcbSGleb Smirnoff show_stat("lu", 10, IFA_STAT(obytes), link|network); 38484c1edcbSGleb Smirnoff show_stat("NRSlu", 5, IFA_STAT(collisions), link); 38584c1edcbSGleb Smirnoff /* XXXGL: output queue drops */ 3869b50d902SRodney W. Grimes putchar('\n'); 3871f575ce8SBruce Evans 38884c1edcbSGleb Smirnoff if (!aflag) 38975fb8770SGarrett Wollman continue; 39075fb8770SGarrett Wollman 39184c1edcbSGleb Smirnoff /* 39284c1edcbSGleb Smirnoff * Print family's multicast addresses. 39384c1edcbSGleb Smirnoff */ 39484c1edcbSGleb Smirnoff for (ifma = next_ifma(ifmap, ifa->ifa_name, 39584c1edcbSGleb Smirnoff ifa->ifa_addr->sa_family); 39684c1edcbSGleb Smirnoff ifma != NULL; 39784c1edcbSGleb Smirnoff ifma = next_ifma(ifma, ifa->ifa_name, 39884c1edcbSGleb Smirnoff ifa->ifa_addr->sa_family)) { 39984c1edcbSGleb Smirnoff const char *fmt = NULL; 40084c1edcbSGleb Smirnoff 40184c1edcbSGleb Smirnoff switch (ifma->ifma_addr->sa_family) { 40275fb8770SGarrett Wollman case AF_INET: 40384c1edcbSGleb Smirnoff { 40484c1edcbSGleb Smirnoff struct sockaddr_in *sin; 40584c1edcbSGleb Smirnoff 40684c1edcbSGleb Smirnoff sin = (struct sockaddr_in *)ifma->ifma_addr; 40784c1edcbSGleb Smirnoff fmt = routename(sin->sin_addr.s_addr); 40875fb8770SGarrett Wollman break; 40984c1edcbSGleb Smirnoff } 410cfa1ca9dSYoshinobu Inoue #ifdef INET6 411cfa1ca9dSYoshinobu Inoue case AF_INET6: 41284c1edcbSGleb Smirnoff 41384c1edcbSGleb Smirnoff /* in6_fillscopeid(&msa.in6); */ 41484c1edcbSGleb Smirnoff getnameinfo(ifma->ifma_addr, 41584c1edcbSGleb Smirnoff ifma->ifma_addr->sa_len, addr_buf, 41684c1edcbSGleb Smirnoff sizeof(addr_buf), 0, 0, NI_NUMERICHOST); 41784c1edcbSGleb Smirnoff printf("%*s %s\n", 41884c1edcbSGleb Smirnoff Wflag ? 27 : 25, "", addr_buf); 419b9d92bf5SBill Fenner break; 420cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 421f6719675SBill Fenner case AF_LINK: 42284c1edcbSGleb Smirnoff { 42384c1edcbSGleb Smirnoff struct sockaddr_dl *sdl; 42484c1edcbSGleb Smirnoff 42584c1edcbSGleb Smirnoff sdl = (struct sockaddr_dl *)ifma->ifma_addr; 42684c1edcbSGleb Smirnoff switch (sdl->sdl_type) { 427f6719675SBill Fenner case IFT_ETHER: 42875fb8770SGarrett Wollman case IFT_FDDI: 42975fb8770SGarrett Wollman fmt = ether_ntoa( 43084c1edcbSGleb Smirnoff (struct ether_addr *)LLADDR(sdl)); 43175fb8770SGarrett Wollman break; 432f6719675SBill Fenner } 433f6719675SBill Fenner break; 434f6719675SBill Fenner } 4359b50d902SRodney W. Grimes } 4369b50d902SRodney W. Grimes 43784c1edcbSGleb Smirnoff if (fmt) { 43884c1edcbSGleb Smirnoff printf("%*s %-17.17s", 43984c1edcbSGleb Smirnoff Wflag ? 27 : 25, "", fmt); 44084c1edcbSGleb Smirnoff if (ifma->ifma_addr->sa_family == AF_LINK) { 44184c1edcbSGleb Smirnoff printf(" %8lu", IFA_STAT(imcasts)); 44284c1edcbSGleb Smirnoff printf("%*s", bflag ? 17 : 6, ""); 44384c1edcbSGleb Smirnoff printf(" %8lu", IFA_STAT(omcasts)); 44484c1edcbSGleb Smirnoff } 44584c1edcbSGleb Smirnoff putchar('\n'); 44684c1edcbSGleb Smirnoff } 44784c1edcbSGleb Smirnoff 44884c1edcbSGleb Smirnoff ifma = ifma->ifma_next; 44984c1edcbSGleb Smirnoff } 45084c1edcbSGleb Smirnoff } 45184c1edcbSGleb Smirnoff 45284c1edcbSGleb Smirnoff freeifaddrs(ifap); 45384c1edcbSGleb Smirnoff if (aflag) 45484c1edcbSGleb Smirnoff freeifmaddrs(ifmap); 45584c1edcbSGleb Smirnoff } 45684c1edcbSGleb Smirnoff 4579b50d902SRodney W. Grimes struct iftot { 458890bc2a5SDoug Rabson u_long ift_ip; /* input packets */ 459890bc2a5SDoug Rabson u_long ift_ie; /* input errors */ 460d72dc9a7SAttilio Rao u_long ift_id; /* input drops */ 461890bc2a5SDoug Rabson u_long ift_op; /* output packets */ 462890bc2a5SDoug Rabson u_long ift_oe; /* output errors */ 463890bc2a5SDoug Rabson u_long ift_co; /* collisions */ 464890bc2a5SDoug Rabson u_long ift_ib; /* input bytes */ 465890bc2a5SDoug Rabson u_long ift_ob; /* output bytes */ 466591c194aSGuido van Rooij }; 4679b50d902SRodney W. Grimes 46884c1edcbSGleb Smirnoff /* 46984c1edcbSGleb Smirnoff * Obtain stats for interface(s). 47084c1edcbSGleb Smirnoff */ 47184c1edcbSGleb Smirnoff static void 47284c1edcbSGleb Smirnoff fill_iftot(struct iftot *st) 47384c1edcbSGleb Smirnoff { 47484c1edcbSGleb Smirnoff struct ifaddrs *ifap, *ifa; 47584c1edcbSGleb Smirnoff bool found = false; 47684c1edcbSGleb Smirnoff 47784c1edcbSGleb Smirnoff if (getifaddrs(&ifap) != 0) 47884c1edcbSGleb Smirnoff err(EX_OSERR, "getifaddrs"); 47984c1edcbSGleb Smirnoff 48084c1edcbSGleb Smirnoff bzero(st, sizeof(*st)); 48184c1edcbSGleb Smirnoff 48284c1edcbSGleb Smirnoff for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 48384c1edcbSGleb Smirnoff if (ifa->ifa_addr->sa_family != AF_LINK) 48484c1edcbSGleb Smirnoff continue; 48584c1edcbSGleb Smirnoff if (interface) { 48684c1edcbSGleb Smirnoff if (strcmp(ifa->ifa_name, interface) == 0) 48784c1edcbSGleb Smirnoff found = true; 48884c1edcbSGleb Smirnoff else 48984c1edcbSGleb Smirnoff continue; 49084c1edcbSGleb Smirnoff } 49184c1edcbSGleb Smirnoff 49284c1edcbSGleb Smirnoff st->ift_ip += IFA_STAT(ipackets); 49384c1edcbSGleb Smirnoff st->ift_ie += IFA_STAT(ierrors); 49484c1edcbSGleb Smirnoff st->ift_id += IFA_STAT(iqdrops); 49584c1edcbSGleb Smirnoff st->ift_ib += IFA_STAT(ibytes); 49684c1edcbSGleb Smirnoff st->ift_op += IFA_STAT(opackets); 49784c1edcbSGleb Smirnoff st->ift_oe += IFA_STAT(oerrors); 49884c1edcbSGleb Smirnoff st->ift_ob += IFA_STAT(obytes); 49984c1edcbSGleb Smirnoff st->ift_co += IFA_STAT(collisions); 50084c1edcbSGleb Smirnoff } 50184c1edcbSGleb Smirnoff 50284c1edcbSGleb Smirnoff if (interface && found == false) 50384c1edcbSGleb Smirnoff err(EX_DATAERR, "interface %s not found", interface); 50484c1edcbSGleb Smirnoff 50584c1edcbSGleb Smirnoff freeifaddrs(ifap); 50684c1edcbSGleb Smirnoff } 50784c1edcbSGleb Smirnoff 50884c1edcbSGleb Smirnoff /* 50984c1edcbSGleb Smirnoff * Set a flag to indicate that a signal from the periodic itimer has been 51084c1edcbSGleb Smirnoff * caught. 51184c1edcbSGleb Smirnoff */ 51284c1edcbSGleb Smirnoff static sig_atomic_t signalled; 51384c1edcbSGleb Smirnoff static void 51484c1edcbSGleb Smirnoff catchalarm(int signo __unused) 51584c1edcbSGleb Smirnoff { 51684c1edcbSGleb Smirnoff signalled = true; 51784c1edcbSGleb Smirnoff } 5189b50d902SRodney W. Grimes 5199b50d902SRodney W. Grimes /* 5209b50d902SRodney W. Grimes * Print a running summary of interface statistics. 52184c1edcbSGleb Smirnoff * Repeat display every interval seconds, showing statistics 52284c1edcbSGleb Smirnoff * collected over that interval. Assumes that interval is non-zero. 5239b50d902SRodney W. Grimes * First line printed at top of screen is always cumulative. 5249b50d902SRodney W. Grimes */ 5259b50d902SRodney W. Grimes static void 52684c1edcbSGleb Smirnoff sidewaysintpr(int interval) 5279b50d902SRodney W. Grimes { 52884c1edcbSGleb Smirnoff struct iftot ift[2], *new, *old; 52993547b07SBruce Evans struct itimerval interval_it; 53084c1edcbSGleb Smirnoff int oldmask, line; 5319b50d902SRodney W. Grimes 53284c1edcbSGleb Smirnoff new = &ift[0]; 53384c1edcbSGleb Smirnoff old = &ift[1]; 53484c1edcbSGleb Smirnoff fill_iftot(old); 535591c194aSGuido van Rooij 5369b50d902SRodney W. Grimes (void)signal(SIGALRM, catchalarm); 53784c1edcbSGleb Smirnoff signalled = false; 53884c1edcbSGleb Smirnoff interval_it.it_interval.tv_sec = interval; 53993547b07SBruce Evans interval_it.it_interval.tv_usec = 0; 54093547b07SBruce Evans interval_it.it_value = interval_it.it_interval; 54193547b07SBruce Evans setitimer(ITIMER_REAL, &interval_it, NULL); 54284c1edcbSGleb Smirnoff 5430b87c915SDavid Greenman banner: 5440b87c915SDavid Greenman printf("%17s %14s %16s", "input", 54584c1edcbSGleb Smirnoff interface != NULL ? interface : "(Total)", "output"); 5469b50d902SRodney W. Grimes putchar('\n'); 547d72dc9a7SAttilio Rao printf("%10s %5s %5s %10s %10s %5s %10s %5s", 548d72dc9a7SAttilio Rao "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes", 549d72dc9a7SAttilio Rao "colls"); 5509b50d902SRodney W. Grimes if (dflag) 5519b50d902SRodney W. Grimes printf(" %5.5s", "drops"); 5529b50d902SRodney W. Grimes putchar('\n'); 5539b50d902SRodney W. Grimes fflush(stdout); 5549b50d902SRodney W. Grimes line = 0; 55584c1edcbSGleb Smirnoff 5569b50d902SRodney W. Grimes loop: 557bf10ffe1SXin LI if ((noutputs != 0) && (--noutputs == 0)) 558bf10ffe1SXin LI exit(0); 5599b50d902SRodney W. Grimes oldmask = sigblock(sigmask(SIGALRM)); 56093547b07SBruce Evans while (!signalled) 5619b50d902SRodney W. Grimes sigpause(0); 56284c1edcbSGleb Smirnoff signalled = false; 56393547b07SBruce Evans sigsetmask(oldmask); 5640b87c915SDavid Greenman line++; 56584c1edcbSGleb Smirnoff 56684c1edcbSGleb Smirnoff fill_iftot(new); 56784c1edcbSGleb Smirnoff 56884c1edcbSGleb Smirnoff show_stat("lu", 10, new->ift_ip - old->ift_ip, 1); 56984c1edcbSGleb Smirnoff show_stat("lu", 5, new->ift_ie - old->ift_ie, 1); 57084c1edcbSGleb Smirnoff show_stat("lu", 5, new->ift_id - old->ift_id, 1); 57184c1edcbSGleb Smirnoff show_stat("lu", 10, new->ift_ib - old->ift_ib, 1); 57284c1edcbSGleb Smirnoff show_stat("lu", 10, new->ift_op - old->ift_op, 1); 57384c1edcbSGleb Smirnoff show_stat("lu", 5, new->ift_oe - old->ift_oe, 1); 57484c1edcbSGleb Smirnoff show_stat("lu", 10, new->ift_ob - old->ift_ob, 1); 57584c1edcbSGleb Smirnoff show_stat("NRSlu", 5, new->ift_co - old->ift_co, 1); 57684c1edcbSGleb Smirnoff /* XXXGL: output queue drops */ 57784c1edcbSGleb Smirnoff putchar('\n'); 57884c1edcbSGleb Smirnoff fflush(stdout); 57984c1edcbSGleb Smirnoff 58084c1edcbSGleb Smirnoff if (new == &ift[0]) { 58184c1edcbSGleb Smirnoff new = &ift[1]; 58284c1edcbSGleb Smirnoff old = &ift[0]; 58384c1edcbSGleb Smirnoff } else { 58484c1edcbSGleb Smirnoff new = &ift[0]; 58584c1edcbSGleb Smirnoff old = &ift[1]; 58684c1edcbSGleb Smirnoff } 58784c1edcbSGleb Smirnoff 5889b50d902SRodney W. Grimes if (line == 21) 5899b50d902SRodney W. Grimes goto banner; 5900b87c915SDavid Greenman else 5919b50d902SRodney W. Grimes goto loop; 5929b50d902SRodney W. Grimes 59384c1edcbSGleb Smirnoff /* NOTREACHED */ 5949b50d902SRodney W. Grimes } 595