19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1983, 1988, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 346cc6f122SPhilippe Charnier #if 0 359b50d902SRodney W. Grimes #ifndef lint 3605ddff6eSPeter Wemm static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95"; 379b50d902SRodney W. Grimes #endif /* not lint */ 386cc6f122SPhilippe Charnier #endif 396cc6f122SPhilippe Charnier 406cc6f122SPhilippe Charnier #include <sys/cdefs.h> 416cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$"); 429b50d902SRodney W. Grimes 439b50d902SRodney W. Grimes #include <sys/types.h> 449b50d902SRodney W. Grimes #include <sys/protosw.h> 459b50d902SRodney W. Grimes #include <sys/socket.h> 460024d1dbSLuigi Rizzo #include <sys/sysctl.h> 47628d2ac1SGarrett Wollman #include <sys/time.h> 489b50d902SRodney W. Grimes 499b50d902SRodney W. Grimes #include <net/if.h> 5087669425SGarrett Wollman #include <net/if_var.h> 519b50d902SRodney W. Grimes #include <net/if_dl.h> 52f6719675SBill Fenner #include <net/if_types.h> 5375fb8770SGarrett Wollman #include <net/ethernet.h> 542e37c5a3SMax Laier #include <net/pfvar.h> 552e37c5a3SMax Laier #include <net/if_pfsync.h> 569b50d902SRodney W. Grimes #include <netinet/in.h> 579b50d902SRodney W. Grimes #include <netinet/in_var.h> 58cc6a66f2SJulian Elischer #include <netipx/ipx.h> 59cc6a66f2SJulian Elischer #include <netipx/ipx_if.h> 609b50d902SRodney W. Grimes #include <arpa/inet.h> 619b50d902SRodney W. Grimes 622e37c5a3SMax Laier #include <err.h> 632e37c5a3SMax Laier #include <errno.h> 64c2dfd19fSGleb Smirnoff #include <libutil.h> 659b50d902SRodney W. Grimes #include <signal.h> 667b95a1ebSYaroslav Tykhiy #include <stdint.h> 679b50d902SRodney W. Grimes #include <stdio.h> 68591c194aSGuido van Rooij #include <stdlib.h> 699b50d902SRodney W. Grimes #include <string.h> 709b50d902SRodney W. Grimes #include <unistd.h> 719b50d902SRodney W. Grimes 729b50d902SRodney W. Grimes #include "netstat.h" 739b50d902SRodney W. Grimes 749b50d902SRodney W. Grimes #define YES 1 759b50d902SRodney W. Grimes #define NO 0 769b50d902SRodney W. Grimes 775e051718SAssar Westerlund static void sidewaysintpr (u_int, u_long); 785e051718SAssar Westerlund static void catchalarm (int); 799b50d902SRodney W. Grimes 80cfa1ca9dSYoshinobu Inoue #ifdef INET6 81cfa1ca9dSYoshinobu Inoue static char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */ 82cfa1ca9dSYoshinobu Inoue #endif 83cfa1ca9dSYoshinobu Inoue 842e37c5a3SMax Laier /* 852e37c5a3SMax Laier * Dump pfsync statistics structure. 862e37c5a3SMax Laier */ 872e37c5a3SMax Laier void 882e37c5a3SMax Laier pfsync_stats(u_long off __unused, const char *name, int af1 __unused) 892e37c5a3SMax Laier { 902e37c5a3SMax Laier struct pfsyncstats pfsyncstat, zerostat; 912e37c5a3SMax Laier size_t len = sizeof(struct pfsyncstats); 92445f17bbSJosef Karthauser 932e37c5a3SMax Laier if (zflag) 942e37c5a3SMax Laier memset(&zerostat, 0, len); 952e37c5a3SMax Laier if (sysctlbyname("net.inet.pfsync.stats", &pfsyncstat, &len, 962e37c5a3SMax Laier zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 972e37c5a3SMax Laier if (errno != ENOENT) 982e37c5a3SMax Laier warn("sysctl: net.inet.pfsync.stats"); 992e37c5a3SMax Laier return; 1002e37c5a3SMax Laier } 101445f17bbSJosef Karthauser 1022e37c5a3SMax Laier printf("%s:\n", name); 1032e37c5a3SMax Laier 1042e37c5a3SMax Laier #define p(f, m) if (pfsyncstat.f || sflag <= 1) \ 1057b95a1ebSYaroslav Tykhiy printf(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f)) 1062e37c5a3SMax Laier #define p2(f, m) if (pfsyncstat.f || sflag <= 1) \ 1077b95a1ebSYaroslav Tykhiy printf(m, (uintmax_t)pfsyncstat.f) 1082e37c5a3SMax Laier 1097b95a1ebSYaroslav Tykhiy p(pfsyncs_ipackets, "\t%ju packet%s received (IPv4)\n"); 1107b95a1ebSYaroslav Tykhiy p(pfsyncs_ipackets6, "\t%ju packet%s received (IPv6)\n"); 1117b95a1ebSYaroslav Tykhiy p(pfsyncs_badif, "\t\t%ju packet%s discarded for bad interface\n"); 1127b95a1ebSYaroslav Tykhiy p(pfsyncs_badttl, "\t\t%ju packet%s discarded for bad ttl\n"); 1137b95a1ebSYaroslav Tykhiy p(pfsyncs_hdrops, "\t\t%ju packet%s shorter than header\n"); 1147b95a1ebSYaroslav Tykhiy p(pfsyncs_badver, "\t\t%ju packet%s discarded for bad version\n"); 1157b95a1ebSYaroslav Tykhiy p(pfsyncs_badauth, "\t\t%ju packet%s discarded for bad HMAC\n"); 1167b95a1ebSYaroslav Tykhiy p(pfsyncs_badact,"\t\t%ju packet%s discarded for bad action\n"); 1177b95a1ebSYaroslav Tykhiy p(pfsyncs_badlen, "\t\t%ju packet%s discarded for short packet\n"); 1187b95a1ebSYaroslav Tykhiy p(pfsyncs_badval, "\t\t%ju state%s discarded for bad values\n"); 1197b95a1ebSYaroslav Tykhiy p(pfsyncs_stale, "\t\t%ju stale state%s\n"); 1207b95a1ebSYaroslav Tykhiy p(pfsyncs_badstate, "\t\t%ju failed state lookup/insert%s\n"); 1217b95a1ebSYaroslav Tykhiy p(pfsyncs_opackets, "\t%ju packet%s sent (IPv4)\n"); 1227b95a1ebSYaroslav Tykhiy p(pfsyncs_opackets6, "\t%ju packet%s sent (IPv6)\n"); 1237b95a1ebSYaroslav Tykhiy p2(pfsyncs_onomem, "\t\t%ju send failed due to mbuf memory error\n"); 1247b95a1ebSYaroslav Tykhiy p2(pfsyncs_oerrors, "\t\t%ju send error\n"); 1252e37c5a3SMax Laier #undef p 1262e37c5a3SMax Laier #undef p2 1272e37c5a3SMax Laier } 128445f17bbSJosef Karthauser 129445f17bbSJosef Karthauser /* 130445f17bbSJosef Karthauser * Display a formatted value, or a '-' in the same space. 131445f17bbSJosef Karthauser */ 1325e051718SAssar Westerlund static void 133f964d60dSAssar Westerlund show_stat(const char *fmt, int width, u_long value, short showvalue) 134445f17bbSJosef Karthauser { 135445f17bbSJosef Karthauser char newfmt[32]; 136445f17bbSJosef Karthauser 137c2dfd19fSGleb Smirnoff if (showvalue == 0) { 138c2dfd19fSGleb Smirnoff /* Print just dash. */ 139445f17bbSJosef Karthauser sprintf(newfmt, "%%%ds ", width); 140445f17bbSJosef Karthauser printf(newfmt, "-"); 141c2dfd19fSGleb Smirnoff return; 142445f17bbSJosef Karthauser } 143445f17bbSJosef Karthauser 144c2dfd19fSGleb Smirnoff if (hflag) { 145c2dfd19fSGleb Smirnoff char buf[5]; 146445f17bbSJosef Karthauser 147c2dfd19fSGleb Smirnoff /* Format in human readable form. */ 148c2dfd19fSGleb Smirnoff humanize_number(buf, sizeof(buf), (int64_t)value, "", 149c2dfd19fSGleb Smirnoff HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL); 150c2dfd19fSGleb Smirnoff sprintf(newfmt, "%%%ds ", width); 151c2dfd19fSGleb Smirnoff printf(newfmt, buf); 152c2dfd19fSGleb Smirnoff } else { 153c2dfd19fSGleb Smirnoff /* Construct the format string. */ 154c2dfd19fSGleb Smirnoff sprintf(newfmt, "%%%d%s ", width, fmt); 155c2dfd19fSGleb Smirnoff printf(newfmt, value); 156c2dfd19fSGleb Smirnoff } 157c2dfd19fSGleb Smirnoff } 158445f17bbSJosef Karthauser 1599b50d902SRodney W. Grimes /* 1609b50d902SRodney W. Grimes * Print a description of the network interfaces. 1619b50d902SRodney W. Grimes */ 1629b50d902SRodney W. Grimes void 163e65dd7bcSMark Murray intpr(int _interval, u_long ifnetaddr, void (*pfunc)(char *)) 1649b50d902SRodney W. Grimes { 1659b50d902SRodney W. Grimes struct ifnet ifnet; 1660e27dc05SGarrett Wollman struct ifnethead ifnethead; 1679b50d902SRodney W. Grimes union { 1689b50d902SRodney W. Grimes struct ifaddr ifa; 1699b50d902SRodney W. Grimes struct in_ifaddr in; 170cfa1ca9dSYoshinobu Inoue #ifdef INET6 171cfa1ca9dSYoshinobu Inoue struct in6_ifaddr in6; 172cfa1ca9dSYoshinobu Inoue #endif 173cc6a66f2SJulian Elischer struct ipx_ifaddr ipx; 1749b50d902SRodney W. Grimes } ifaddr; 1759b50d902SRodney W. Grimes u_long ifaddraddr; 176f6719675SBill Fenner u_long ifaddrfound; 177f6719675SBill Fenner u_long ifnetfound; 1785da9f8faSJosef Karthauser u_long opackets; 1795da9f8faSJosef Karthauser u_long ipackets; 1805da9f8faSJosef Karthauser u_long obytes; 1815da9f8faSJosef Karthauser u_long ibytes; 182e1655201SRuslan Ermilov u_long omcasts; 183e1655201SRuslan Ermilov u_long imcasts; 1845da9f8faSJosef Karthauser u_long oerrors; 1855da9f8faSJosef Karthauser u_long ierrors; 1865da9f8faSJosef Karthauser u_long collisions; 1875da9f8faSJosef Karthauser short timer; 1885da9f8faSJosef Karthauser int drops; 189a97a9922SJulian Elischer struct sockaddr *sa = NULL; 1909bf40edeSBrooks Davis char name[IFNAMSIZ]; 191445f17bbSJosef Karthauser short network_layer; 192445f17bbSJosef Karthauser short link_layer; 1939b50d902SRodney W. Grimes 1949b50d902SRodney W. Grimes if (ifnetaddr == 0) { 1959b50d902SRodney W. Grimes printf("ifnet: symbol not defined\n"); 1969b50d902SRodney W. Grimes return; 1979b50d902SRodney W. Grimes } 198e65dd7bcSMark Murray if (_interval) { 199e65dd7bcSMark Murray sidewaysintpr((unsigned)_interval, ifnetaddr); 2009b50d902SRodney W. Grimes return; 2019b50d902SRodney W. Grimes } 2020e27dc05SGarrett Wollman if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead)) 2039b50d902SRodney W. Grimes return; 2044d51ef63SPoul-Henning Kamp ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead); 2050e27dc05SGarrett Wollman if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet)) 2060e27dc05SGarrett Wollman return; 2070e27dc05SGarrett Wollman 208cf5e44f8SRuslan Ermilov if (!pfunc) { 20925d295e1SBruce M Simpson if (Wflag) 21025d295e1SBruce M Simpson printf("%-7.7s", "Name"); 21125d295e1SBruce M Simpson else 21225d295e1SBruce M Simpson printf("%-5.5s", "Name"); 21325d295e1SBruce M Simpson printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s", 21425d295e1SBruce M Simpson "Mtu", "Network", "Address", "Ipkts", "Ierrs"); 215e1e293a5SDavid Greenman if (bflag) 216e1e293a5SDavid Greenman printf(" %10.10s","Ibytes"); 217e1e293a5SDavid Greenman printf(" %8.8s %5.5s", "Opkts", "Oerrs"); 218e1e293a5SDavid Greenman if (bflag) 219e1e293a5SDavid Greenman printf(" %10.10s","Obytes"); 2209b50d902SRodney W. Grimes printf(" %5s", "Coll"); 2219b50d902SRodney W. Grimes if (tflag) 2229b50d902SRodney W. Grimes printf(" %s", "Time"); 2239b50d902SRodney W. Grimes if (dflag) 2249b50d902SRodney W. Grimes printf(" %s", "Drop"); 2259b50d902SRodney W. Grimes putchar('\n'); 226cfa1ca9dSYoshinobu Inoue } 2279b50d902SRodney W. Grimes ifaddraddr = 0; 2289b50d902SRodney W. Grimes while (ifnetaddr || ifaddraddr) { 2296cc6f122SPhilippe Charnier struct sockaddr_in *sockin; 230cfa1ca9dSYoshinobu Inoue #ifdef INET6 2316cc6f122SPhilippe Charnier struct sockaddr_in6 *sockin6; 232cfa1ca9dSYoshinobu Inoue #endif 233a01e3379SDavid Malone char *cp; 2349b50d902SRodney W. Grimes int n, m; 2359b50d902SRodney W. Grimes 236445f17bbSJosef Karthauser network_layer = 0; 237445f17bbSJosef Karthauser link_layer = 0; 238445f17bbSJosef Karthauser 2399b50d902SRodney W. Grimes if (ifaddraddr == 0) { 240f6719675SBill Fenner ifnetfound = ifnetaddr; 2419bf40edeSBrooks Davis if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet)) 2429b50d902SRodney W. Grimes return; 2439bf40edeSBrooks Davis strlcpy(name, ifnet.if_xname, sizeof(name)); 2444d51ef63SPoul-Henning Kamp ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link); 2451b72e71cSDavid Greenman if (interface != 0 && (strcmp(name, interface) != 0)) 2469b50d902SRodney W. Grimes continue; 2479b50d902SRodney W. Grimes cp = index(name, '\0'); 248cfa1ca9dSYoshinobu Inoue 249cfa1ca9dSYoshinobu Inoue if (pfunc) { 250cfa1ca9dSYoshinobu Inoue (*pfunc)(name); 251cfa1ca9dSYoshinobu Inoue continue; 252cfa1ca9dSYoshinobu Inoue } 253cfa1ca9dSYoshinobu Inoue 2549b50d902SRodney W. Grimes if ((ifnet.if_flags&IFF_UP) == 0) 2559b50d902SRodney W. Grimes *cp++ = '*'; 2569b50d902SRodney W. Grimes *cp = '\0'; 2574d51ef63SPoul-Henning Kamp ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead); 2589b50d902SRodney W. Grimes } 259f6719675SBill Fenner ifaddrfound = ifaddraddr; 2605da9f8faSJosef Karthauser 2615da9f8faSJosef Karthauser /* 2625da9f8faSJosef Karthauser * Get the interface stats. These may get 2635da9f8faSJosef Karthauser * overriden below on a per-interface basis. 2645da9f8faSJosef Karthauser */ 2655da9f8faSJosef Karthauser opackets = ifnet.if_opackets; 2665da9f8faSJosef Karthauser ipackets = ifnet.if_ipackets; 2675da9f8faSJosef Karthauser obytes = ifnet.if_obytes; 2685da9f8faSJosef Karthauser ibytes = ifnet.if_ibytes; 269e1655201SRuslan Ermilov omcasts = ifnet.if_omcasts; 270e1655201SRuslan Ermilov imcasts = ifnet.if_imcasts; 2715da9f8faSJosef Karthauser oerrors = ifnet.if_oerrors; 2725da9f8faSJosef Karthauser ierrors = ifnet.if_ierrors; 2735da9f8faSJosef Karthauser collisions = ifnet.if_collisions; 2745da9f8faSJosef Karthauser timer = ifnet.if_timer; 2755da9f8faSJosef Karthauser drops = ifnet.if_snd.ifq_drops; 2765da9f8faSJosef Karthauser 2779b50d902SRodney W. Grimes if (ifaddraddr == 0) { 27825d295e1SBruce M Simpson if (Wflag) 27925d295e1SBruce M Simpson printf("%-7.7s", name); 28025d295e1SBruce M Simpson else 28125d295e1SBruce M Simpson printf("%-5.5s", name); 28225d295e1SBruce M Simpson printf(" %5lu ", ifnet.if_mtu); 283acb60e59SRuslan Ermilov printf("%-13.13s ", "none"); 284dd1f8b9bSMatthew N. Dodd printf("%-17.17s ", "none"); 2859b50d902SRodney W. Grimes } else { 2869b50d902SRodney W. Grimes if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) { 2879b50d902SRodney W. Grimes ifaddraddr = 0; 2889b50d902SRodney W. Grimes continue; 2899b50d902SRodney W. Grimes } 2909b50d902SRodney W. Grimes #define CP(x) ((char *)(x)) 2919b50d902SRodney W. Grimes cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) + 292a97a9922SJulian Elischer CP(&ifaddr); 293a97a9922SJulian Elischer sa = (struct sockaddr *)cp; 294d44ddba9SRuslan Ermilov if (af != AF_UNSPEC && sa->sa_family != af) { 295d44ddba9SRuslan Ermilov ifaddraddr = 296d44ddba9SRuslan Ermilov (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link); 297d44ddba9SRuslan Ermilov continue; 298d44ddba9SRuslan Ermilov } 29925d295e1SBruce M Simpson if (Wflag) 30025d295e1SBruce M Simpson printf("%-7.7s", name); 30125d295e1SBruce M Simpson else 30225d295e1SBruce M Simpson printf("%-5.5s", name); 30325d295e1SBruce M Simpson printf(" %5lu ", ifnet.if_mtu); 3049b50d902SRodney W. Grimes switch (sa->sa_family) { 3059b50d902SRodney W. Grimes case AF_UNSPEC: 306cc6a66f2SJulian Elischer printf("%-13.13s ", "none"); 3079b50d902SRodney W. Grimes printf("%-15.15s ", "none"); 3089b50d902SRodney W. Grimes break; 3099b50d902SRodney W. Grimes case AF_INET: 3106cc6f122SPhilippe Charnier sockin = (struct sockaddr_in *)sa; 3119b50d902SRodney W. Grimes #ifdef notdef 3129b50d902SRodney W. Grimes /* can't use inet_makeaddr because kernel 3139b50d902SRodney W. Grimes * keeps nets unshifted. 3149b50d902SRodney W. Grimes */ 3159b50d902SRodney W. Grimes in = inet_makeaddr(ifaddr.in.ia_subnet, 3169b50d902SRodney W. Grimes INADDR_ANY); 31709a67ffaSStefan Eßer printf("%-13.13s ", netname(in.s_addr, 3189b50d902SRodney W. Grimes ifaddr.in.ia_subnetmask)); 3199b50d902SRodney W. Grimes #else 320acb60e59SRuslan Ermilov printf("%-13.13s ", 3219b50d902SRodney W. Grimes netname(htonl(ifaddr.in.ia_subnet), 3229b50d902SRodney W. Grimes ifaddr.in.ia_subnetmask)); 3239b50d902SRodney W. Grimes #endif 324dd1f8b9bSMatthew N. Dodd printf("%-17.17s ", 3256cc6f122SPhilippe Charnier routename(sockin->sin_addr.s_addr)); 326445f17bbSJosef Karthauser 327445f17bbSJosef Karthauser network_layer = 1; 3289b50d902SRodney W. Grimes break; 329cfa1ca9dSYoshinobu Inoue #ifdef INET6 330cfa1ca9dSYoshinobu Inoue case AF_INET6: 3316cc6f122SPhilippe Charnier sockin6 = (struct sockaddr_in6 *)sa; 332acb60e59SRuslan Ermilov printf("%-13.13s ", 333cfa1ca9dSYoshinobu Inoue netname6(&ifaddr.in6.ia_addr, 334cfa1ca9dSYoshinobu Inoue &ifaddr.in6.ia_prefixmask.sin6_addr)); 335cfa1ca9dSYoshinobu Inoue printf("%-17.17s ", 336a01e3379SDavid Malone inet_ntop(AF_INET6, 3376cc6f122SPhilippe Charnier &sockin6->sin6_addr, 338cfa1ca9dSYoshinobu Inoue ntop_buf, sizeof(ntop_buf))); 339445f17bbSJosef Karthauser 340445f17bbSJosef Karthauser network_layer = 1; 341cfa1ca9dSYoshinobu Inoue break; 342cfa1ca9dSYoshinobu Inoue #endif /*INET6*/ 343cc6a66f2SJulian Elischer case AF_IPX: 344cc6a66f2SJulian Elischer { 345cc6a66f2SJulian Elischer struct sockaddr_ipx *sipx = 346cc6a66f2SJulian Elischer (struct sockaddr_ipx *)sa; 347cc6a66f2SJulian Elischer u_long net; 348cc6a66f2SJulian Elischer char netnum[10]; 349cc6a66f2SJulian Elischer 350cc6a66f2SJulian Elischer *(union ipx_net *) &net = sipx->sipx_addr.x_net; 35122694ebaSBruce Evans sprintf(netnum, "%lx", (u_long)ntohl(net)); 352cc6a66f2SJulian Elischer printf("ipx:%-8s ", netnum); 353cc6a66f2SJulian Elischer /* printf("ipx:%-8s ", netname(net, 0L)); */ 354acb60e59SRuslan Ermilov printf("%-17s ", 355cc6a66f2SJulian Elischer ipx_phost((struct sockaddr *)sipx)); 356cc6a66f2SJulian Elischer } 3576f9cdfceSMatthew N. Dodd 3586f9cdfceSMatthew N. Dodd network_layer = 1; 359cc6a66f2SJulian Elischer break; 3606ffcfd6cSJulian Elischer 3616ffcfd6cSJulian Elischer case AF_APPLETALK: 362a8d37845SJulian Elischer printf("atalk:%-12.12s ",atalk_print(sa,0x10) ); 363acb60e59SRuslan Ermilov printf("%-11.11s ",atalk_print(sa,0x0b) ); 3646ffcfd6cSJulian Elischer break; 3659b50d902SRodney W. Grimes case AF_LINK: 3669b50d902SRodney W. Grimes { 3679b50d902SRodney W. Grimes struct sockaddr_dl *sdl = 3689b50d902SRodney W. Grimes (struct sockaddr_dl *)sa; 369cfa1ca9dSYoshinobu Inoue char linknum[10]; 3709b50d902SRodney W. Grimes cp = (char *)LLADDR(sdl); 3719b50d902SRodney W. Grimes n = sdl->sdl_alen; 372cfa1ca9dSYoshinobu Inoue sprintf(linknum, "<Link#%d>", sdl->sdl_index); 373acb60e59SRuslan Ermilov m = printf("%-13.13s ", linknum); 3749b50d902SRodney W. Grimes } 3759b50d902SRodney W. Grimes goto hexprint; 3769b50d902SRodney W. Grimes default: 3779b50d902SRodney W. Grimes m = printf("(%d)", sa->sa_family); 3789b50d902SRodney W. Grimes for (cp = sa->sa_len + (char *)sa; 3799b50d902SRodney W. Grimes --cp > sa->sa_data && (*cp == 0);) {} 3809b50d902SRodney W. Grimes n = cp - sa->sa_data + 1; 3819b50d902SRodney W. Grimes cp = sa->sa_data; 3829b50d902SRodney W. Grimes hexprint: 3839b50d902SRodney W. Grimes while (--n >= 0) 384541f2562SDavid Greenman m += printf("%02x%c", *cp++ & 0xff, 385e54ca68cSJordan K. Hubbard n > 0 ? ':' : ' '); 386acb60e59SRuslan Ermilov m = 32 - m; 3879b50d902SRodney W. Grimes while (m-- > 0) 3889b50d902SRodney W. Grimes putchar(' '); 389445f17bbSJosef Karthauser 390445f17bbSJosef Karthauser link_layer = 1; 3919b50d902SRodney W. Grimes break; 3929b50d902SRodney W. Grimes } 3935da9f8faSJosef Karthauser 3945da9f8faSJosef Karthauser /* 3955da9f8faSJosef Karthauser * Fixup the statistics for interfaces that 3965da9f8faSJosef Karthauser * update stats for their network addresses 3975da9f8faSJosef Karthauser */ 398445f17bbSJosef Karthauser if (network_layer) { 3995da9f8faSJosef Karthauser opackets = ifaddr.in.ia_ifa.if_opackets; 4005da9f8faSJosef Karthauser ipackets = ifaddr.in.ia_ifa.if_ipackets; 4015da9f8faSJosef Karthauser obytes = ifaddr.in.ia_ifa.if_obytes; 4025da9f8faSJosef Karthauser ibytes = ifaddr.in.ia_ifa.if_ibytes; 4035da9f8faSJosef Karthauser } 4045da9f8faSJosef Karthauser 4054d51ef63SPoul-Henning Kamp ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link); 4069b50d902SRodney W. Grimes } 4075da9f8faSJosef Karthauser 408445f17bbSJosef Karthauser show_stat("lu", 8, ipackets, link_layer|network_layer); 409445f17bbSJosef Karthauser show_stat("lu", 5, ierrors, link_layer); 4107c23a867SGleb Smirnoff if (bflag) 411445f17bbSJosef Karthauser show_stat("lu", 10, ibytes, link_layer|network_layer); 4127c23a867SGleb Smirnoff 413445f17bbSJosef Karthauser show_stat("lu", 8, opackets, link_layer|network_layer); 414445f17bbSJosef Karthauser show_stat("lu", 5, oerrors, link_layer); 4157c23a867SGleb Smirnoff if (bflag) 416445f17bbSJosef Karthauser show_stat("lu", 10, obytes, link_layer|network_layer); 4177c23a867SGleb Smirnoff 418445f17bbSJosef Karthauser show_stat("lu", 5, collisions, link_layer); 4197c23a867SGleb Smirnoff if (tflag) 420b5f67491SRuslan Ermilov show_stat("d", 4, timer, link_layer); 4217c23a867SGleb Smirnoff 4227c23a867SGleb Smirnoff if (dflag) 423b5f67491SRuslan Ermilov show_stat("d", 4, drops, link_layer); 4247c23a867SGleb Smirnoff 4259b50d902SRodney W. Grimes putchar('\n'); 426f6719675SBill Fenner if (aflag && ifaddrfound) { 427f6719675SBill Fenner /* 428f6719675SBill Fenner * Print family's multicast addresses 429f6719675SBill Fenner */ 4304d51ef63SPoul-Henning Kamp struct ifmultiaddr *multiaddr; 43175fb8770SGarrett Wollman struct ifmultiaddr ifma; 43275fb8770SGarrett Wollman union { 43375fb8770SGarrett Wollman struct sockaddr sa; 43475fb8770SGarrett Wollman struct sockaddr_in in; 435cfa1ca9dSYoshinobu Inoue #ifdef INET6 436cfa1ca9dSYoshinobu Inoue struct sockaddr_in6 in6; 437cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 43875fb8770SGarrett Wollman struct sockaddr_dl dl; 43975fb8770SGarrett Wollman } msa; 44075fb8770SGarrett Wollman const char *fmt; 441f6719675SBill Fenner 4426817526dSPoul-Henning Kamp TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) { 4430b23654bSPoul-Henning Kamp if (kread((u_long)multiaddr, (char *)&ifma, 44475fb8770SGarrett Wollman sizeof ifma)) 445f6719675SBill Fenner break; 4460b23654bSPoul-Henning Kamp multiaddr = &ifma; 44775fb8770SGarrett Wollman if (kread((u_long)ifma.ifma_addr, (char *)&msa, 44875fb8770SGarrett Wollman sizeof msa)) 44975fb8770SGarrett Wollman break; 45075fb8770SGarrett Wollman if (msa.sa.sa_family != sa->sa_family) 45175fb8770SGarrett Wollman continue; 45275fb8770SGarrett Wollman 45375fb8770SGarrett Wollman fmt = 0; 45475fb8770SGarrett Wollman switch (msa.sa.sa_family) { 45575fb8770SGarrett Wollman case AF_INET: 45675fb8770SGarrett Wollman fmt = routename(msa.in.sin_addr.s_addr); 45775fb8770SGarrett Wollman break; 458cfa1ca9dSYoshinobu Inoue #ifdef INET6 459cfa1ca9dSYoshinobu Inoue case AF_INET6: 460bce2e7c8SRuslan Ermilov printf("%*s %-19.19s(refs: %d)\n", 461bce2e7c8SRuslan Ermilov Wflag ? 27 : 25, "", 462cfa1ca9dSYoshinobu Inoue inet_ntop(AF_INET6, 463cfa1ca9dSYoshinobu Inoue &msa.in6.sin6_addr, 464cfa1ca9dSYoshinobu Inoue ntop_buf, 465cfa1ca9dSYoshinobu Inoue sizeof(ntop_buf)), 466cfa1ca9dSYoshinobu Inoue ifma.ifma_refcount); 467b9d92bf5SBill Fenner break; 468cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 469f6719675SBill Fenner case AF_LINK: 470b9d92bf5SBill Fenner switch (msa.dl.sdl_type) { 471f6719675SBill Fenner case IFT_ETHER: 47275fb8770SGarrett Wollman case IFT_FDDI: 47375fb8770SGarrett Wollman fmt = ether_ntoa( 47475fb8770SGarrett Wollman (struct ether_addr *) 47575fb8770SGarrett Wollman LLADDR(&msa.dl)); 47675fb8770SGarrett Wollman break; 477f6719675SBill Fenner } 478f6719675SBill Fenner break; 479f6719675SBill Fenner } 480e1655201SRuslan Ermilov if (fmt) { 481e1655201SRuslan Ermilov printf("%*s %-17.17s", 482bce2e7c8SRuslan Ermilov Wflag ? 27 : 25, "", fmt); 483e1655201SRuslan Ermilov if (msa.sa.sa_family == AF_LINK) { 484e1655201SRuslan Ermilov printf(" %8lu", imcasts); 485e1655201SRuslan Ermilov printf("%*s", 486e1655201SRuslan Ermilov bflag ? 17 : 6, ""); 487e1655201SRuslan Ermilov printf(" %8lu", omcasts); 488e1655201SRuslan Ermilov } 489e1655201SRuslan Ermilov putchar('\n'); 490e1655201SRuslan Ermilov } 491f6719675SBill Fenner } 492f6719675SBill Fenner } 4939b50d902SRodney W. Grimes } 4949b50d902SRodney W. Grimes } 4959b50d902SRodney W. Grimes 4969b50d902SRodney W. Grimes struct iftot { 4974d51ef63SPoul-Henning Kamp SLIST_ENTRY(iftot) chain; 4989bf40edeSBrooks Davis char ift_name[IFNAMSIZ]; /* interface name */ 499890bc2a5SDoug Rabson u_long ift_ip; /* input packets */ 500890bc2a5SDoug Rabson u_long ift_ie; /* input errors */ 501890bc2a5SDoug Rabson u_long ift_op; /* output packets */ 502890bc2a5SDoug Rabson u_long ift_oe; /* output errors */ 503890bc2a5SDoug Rabson u_long ift_co; /* collisions */ 504e1e293a5SDavid Greenman u_int ift_dr; /* drops */ 505890bc2a5SDoug Rabson u_long ift_ib; /* input bytes */ 506890bc2a5SDoug Rabson u_long ift_ob; /* output bytes */ 507591c194aSGuido van Rooij }; 5089b50d902SRodney W. Grimes 5099b50d902SRodney W. Grimes u_char signalled; /* set if alarm goes off "early" */ 5109b50d902SRodney W. Grimes 5119b50d902SRodney W. Grimes /* 5129b50d902SRodney W. Grimes * Print a running summary of interface statistics. 5139b50d902SRodney W. Grimes * Repeat display every interval seconds, showing statistics 5149b50d902SRodney W. Grimes * collected over that interval. Assumes that interval is non-zero. 5159b50d902SRodney W. Grimes * First line printed at top of screen is always cumulative. 5160e27dc05SGarrett Wollman * XXX - should be rewritten to use ifmib(4). 5179b50d902SRodney W. Grimes */ 5189b50d902SRodney W. Grimes static void 519a01e3379SDavid Malone sidewaysintpr(unsigned interval1, u_long off) 5209b50d902SRodney W. Grimes { 5219b50d902SRodney W. Grimes struct ifnet ifnet; 5229b50d902SRodney W. Grimes u_long firstifnet; 5230e27dc05SGarrett Wollman struct ifnethead ifnethead; 524591c194aSGuido van Rooij struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting; 525a01e3379SDavid Malone int line; 5260b87c915SDavid Greenman int oldmask, first; 5270b87c915SDavid Greenman u_long interesting_off; 5289b50d902SRodney W. Grimes 5290e27dc05SGarrett Wollman if (kread(off, (char *)&ifnethead, sizeof ifnethead)) 5309b50d902SRodney W. Grimes return; 5314d51ef63SPoul-Henning Kamp firstifnet = (u_long)TAILQ_FIRST(&ifnethead); 5320e27dc05SGarrett Wollman 533591c194aSGuido van Rooij if ((iftot = malloc(sizeof(struct iftot))) == NULL) { 534591c194aSGuido van Rooij printf("malloc failed\n"); 535591c194aSGuido van Rooij exit(1); 536591c194aSGuido van Rooij } 537591c194aSGuido van Rooij memset(iftot, 0, sizeof(struct iftot)); 538591c194aSGuido van Rooij 5390b87c915SDavid Greenman interesting = NULL; 5400b87c915SDavid Greenman interesting_off = 0; 5419b50d902SRodney W. Grimes for (off = firstifnet, ip = iftot; off;) { 5429bf40edeSBrooks Davis char name[IFNAMSIZ]; 5439b50d902SRodney W. Grimes 5449b50d902SRodney W. Grimes if (kread(off, (char *)&ifnet, sizeof ifnet)) 5459b50d902SRodney W. Grimes break; 5469bf40edeSBrooks Davis strlcpy(name, ifnet.if_xname, sizeof(name)); 5470b87c915SDavid Greenman if (interface && strcmp(name, interface) == 0) { 5489b50d902SRodney W. Grimes interesting = ip; 5490b87c915SDavid Greenman interesting_off = off; 5500b87c915SDavid Greenman } 551ec3b72e9SRobert Drehmel snprintf(ip->ift_name, sizeof(ip->ift_name), "(%s)", name);; 552591c194aSGuido van Rooij if ((ipn = malloc(sizeof(struct iftot))) == NULL) { 553591c194aSGuido van Rooij printf("malloc failed\n"); 554591c194aSGuido van Rooij exit(1); 555591c194aSGuido van Rooij } 556591c194aSGuido van Rooij memset(ipn, 0, sizeof(struct iftot)); 5574d51ef63SPoul-Henning Kamp SLIST_NEXT(ip, chain) = ipn; 558591c194aSGuido van Rooij ip = ipn; 5594d51ef63SPoul-Henning Kamp off = (u_long)TAILQ_NEXT(&ifnet, if_link); 5609b50d902SRodney W. Grimes } 561c6358a5eSRuslan Ermilov if (interface && interesting == NULL) 562c6358a5eSRuslan Ermilov errx(1, "%s: unknown interface", interface); 563591c194aSGuido van Rooij if ((total = malloc(sizeof(struct iftot))) == NULL) { 564591c194aSGuido van Rooij printf("malloc failed\n"); 565591c194aSGuido van Rooij exit(1); 566591c194aSGuido van Rooij } 567591c194aSGuido van Rooij memset(total, 0, sizeof(struct iftot)); 568591c194aSGuido van Rooij if ((sum = malloc(sizeof(struct iftot))) == NULL) { 569591c194aSGuido van Rooij printf("malloc failed\n"); 570591c194aSGuido van Rooij exit(1); 571591c194aSGuido van Rooij } 572591c194aSGuido van Rooij memset(sum, 0, sizeof(struct iftot)); 573591c194aSGuido van Rooij 5749b50d902SRodney W. Grimes (void)signal(SIGALRM, catchalarm); 5759b50d902SRodney W. Grimes signalled = NO; 576a01e3379SDavid Malone (void)alarm(interval1); 5770b87c915SDavid Greenman first = 1; 5780b87c915SDavid Greenman banner: 5790b87c915SDavid Greenman printf("%17s %14s %16s", "input", 5800b87c915SDavid Greenman interesting ? interesting->ift_name : "(Total)", "output"); 5819b50d902SRodney W. Grimes putchar('\n'); 5820b87c915SDavid Greenman printf("%10s %5s %10s %10s %5s %10s %5s", 5830b87c915SDavid Greenman "packets", "errs", "bytes", "packets", "errs", "bytes", "colls"); 5849b50d902SRodney W. Grimes if (dflag) 5859b50d902SRodney W. Grimes printf(" %5.5s", "drops"); 5869b50d902SRodney W. Grimes putchar('\n'); 5879b50d902SRodney W. Grimes fflush(stdout); 5889b50d902SRodney W. Grimes line = 0; 5899b50d902SRodney W. Grimes loop: 5900b87c915SDavid Greenman if (interesting != NULL) { 5910b87c915SDavid Greenman ip = interesting; 5920b87c915SDavid Greenman if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) { 5930b87c915SDavid Greenman printf("???\n"); 5940b87c915SDavid Greenman exit(1); 5950b87c915SDavid Greenman }; 5960b87c915SDavid Greenman if (!first) { 5977c23a867SGleb Smirnoff show_stat("lu", 10, ifnet.if_ipackets - ip->ift_ip, 1); 5987c23a867SGleb Smirnoff show_stat("lu", 5, ifnet.if_ierrors - ip->ift_ie, 1); 5997c23a867SGleb Smirnoff show_stat("lu", 10, ifnet.if_ibytes - ip->ift_ib, 1); 6007c23a867SGleb Smirnoff show_stat("lu", 10, ifnet.if_opackets - ip->ift_op, 1); 6017c23a867SGleb Smirnoff show_stat("lu", 5, ifnet.if_oerrors - ip->ift_oe, 1); 6027c23a867SGleb Smirnoff show_stat("lu", 10, ifnet.if_obytes - ip->ift_ob, 1); 6037c23a867SGleb Smirnoff show_stat("lu", 5, ifnet.if_collisions - ip->ift_co, 1); 6040b87c915SDavid Greenman if (dflag) 6057c23a867SGleb Smirnoff show_stat("u", 5, 6067c23a867SGleb Smirnoff ifnet.if_snd.ifq_drops - ip->ift_dr, 1); 6070b87c915SDavid Greenman } 6080b87c915SDavid Greenman ip->ift_ip = ifnet.if_ipackets; 6090b87c915SDavid Greenman ip->ift_ie = ifnet.if_ierrors; 6100b87c915SDavid Greenman ip->ift_ib = ifnet.if_ibytes; 6110b87c915SDavid Greenman ip->ift_op = ifnet.if_opackets; 6120b87c915SDavid Greenman ip->ift_oe = ifnet.if_oerrors; 6130b87c915SDavid Greenman ip->ift_ob = ifnet.if_obytes; 6140b87c915SDavid Greenman ip->ift_co = ifnet.if_collisions; 6150b87c915SDavid Greenman ip->ift_dr = ifnet.if_snd.ifq_drops; 6160b87c915SDavid Greenman } else { 6179b50d902SRodney W. Grimes sum->ift_ip = 0; 6189b50d902SRodney W. Grimes sum->ift_ie = 0; 6190b87c915SDavid Greenman sum->ift_ib = 0; 6209b50d902SRodney W. Grimes sum->ift_op = 0; 6219b50d902SRodney W. Grimes sum->ift_oe = 0; 6220b87c915SDavid Greenman sum->ift_ob = 0; 6239b50d902SRodney W. Grimes sum->ift_co = 0; 6249b50d902SRodney W. Grimes sum->ift_dr = 0; 6254d51ef63SPoul-Henning Kamp for (off = firstifnet, ip = iftot; 6264d51ef63SPoul-Henning Kamp off && SLIST_NEXT(ip, chain) != NULL; 6274d51ef63SPoul-Henning Kamp ip = SLIST_NEXT(ip, chain)) { 6289b50d902SRodney W. Grimes if (kread(off, (char *)&ifnet, sizeof ifnet)) { 6299b50d902SRodney W. Grimes off = 0; 6309b50d902SRodney W. Grimes continue; 6319b50d902SRodney W. Grimes } 6320b87c915SDavid Greenman sum->ift_ip += ifnet.if_ipackets; 6330b87c915SDavid Greenman sum->ift_ie += ifnet.if_ierrors; 6340b87c915SDavid Greenman sum->ift_ib += ifnet.if_ibytes; 6350b87c915SDavid Greenman sum->ift_op += ifnet.if_opackets; 6360b87c915SDavid Greenman sum->ift_oe += ifnet.if_oerrors; 6370b87c915SDavid Greenman sum->ift_ob += ifnet.if_obytes; 6380b87c915SDavid Greenman sum->ift_co += ifnet.if_collisions; 6390b87c915SDavid Greenman sum->ift_dr += ifnet.if_snd.ifq_drops; 6404d51ef63SPoul-Henning Kamp off = (u_long)TAILQ_NEXT(&ifnet, if_link); 6419b50d902SRodney W. Grimes } 6420b87c915SDavid Greenman if (!first) { 6437c23a867SGleb Smirnoff show_stat("lu", 10, sum->ift_ip - total->ift_ip, 1); 6447c23a867SGleb Smirnoff show_stat("lu", 5, sum->ift_ie - total->ift_ie, 1); 6457c23a867SGleb Smirnoff show_stat("lu", 10, sum->ift_ib - total->ift_ib, 1); 6467c23a867SGleb Smirnoff show_stat("lu", 10, sum->ift_op - total->ift_op, 1); 6477c23a867SGleb Smirnoff show_stat("lu", 5, sum->ift_oe - total->ift_oe, 1); 6487c23a867SGleb Smirnoff show_stat("lu", 10, sum->ift_ob - total->ift_ob, 1); 6497c23a867SGleb Smirnoff show_stat("lu", 5, sum->ift_co - total->ift_co, 1); 6509b50d902SRodney W. Grimes if (dflag) 6517c23a867SGleb Smirnoff show_stat("u", 5, 6527c23a867SGleb Smirnoff sum->ift_dr - total->ift_dr, 1); 6539b50d902SRodney W. Grimes } 6549b50d902SRodney W. Grimes *total = *sum; 6550b87c915SDavid Greenman } 6560b87c915SDavid Greenman if (!first) 6579b50d902SRodney W. Grimes putchar('\n'); 6589b50d902SRodney W. Grimes fflush(stdout); 6599b50d902SRodney W. Grimes oldmask = sigblock(sigmask(SIGALRM)); 6609b50d902SRodney W. Grimes if (! signalled) { 6619b50d902SRodney W. Grimes sigpause(0); 6629b50d902SRodney W. Grimes } 6639b50d902SRodney W. Grimes sigsetmask(oldmask); 6649b50d902SRodney W. Grimes signalled = NO; 665a01e3379SDavid Malone (void)alarm(interval1); 6660b87c915SDavid Greenman line++; 6670b87c915SDavid Greenman first = 0; 6689b50d902SRodney W. Grimes if (line == 21) 6699b50d902SRodney W. Grimes goto banner; 6700b87c915SDavid Greenman else 6719b50d902SRodney W. Grimes goto loop; 6729b50d902SRodney W. Grimes /*NOTREACHED*/ 6739b50d902SRodney W. Grimes } 6749b50d902SRodney W. Grimes 6759b50d902SRodney W. Grimes /* 6769b50d902SRodney W. Grimes * Called if an interval expires before sidewaysintpr has completed a loop. 6779b50d902SRodney W. Grimes * Sets a flag to not wait for the alarm. 6789b50d902SRodney W. Grimes */ 6799b50d902SRodney W. Grimes static void 680f964d60dSAssar Westerlund catchalarm(int signo __unused) 6819b50d902SRodney W. Grimes { 6829b50d902SRodney W. Grimes signalled = YES; 6839b50d902SRodney W. Grimes } 684