xref: /freebsd/usr.bin/netstat/if.c (revision 65475bc8e6c268f6054a4ec9cfdb277c2c58cac3)
165475bc8SDavid E. O'Brien /*-
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>
46feda1a43SJohn Baldwin #include <sys/socketvar.h>
470024d1dbSLuigi Rizzo #include <sys/sysctl.h>
48628d2ac1SGarrett Wollman #include <sys/time.h>
499b50d902SRodney W. Grimes 
509b50d902SRodney W. Grimes #include <net/if.h>
5187669425SGarrett Wollman #include <net/if_var.h>
529b50d902SRodney W. Grimes #include <net/if_dl.h>
53f6719675SBill Fenner #include <net/if_types.h>
5475fb8770SGarrett Wollman #include <net/ethernet.h>
552e37c5a3SMax Laier #include <net/pfvar.h>
562e37c5a3SMax Laier #include <net/if_pfsync.h>
579b50d902SRodney W. Grimes #include <netinet/in.h>
589b50d902SRodney W. Grimes #include <netinet/in_var.h>
59cc6a66f2SJulian Elischer #include <netipx/ipx.h>
60cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
619b50d902SRodney W. Grimes #include <arpa/inet.h>
629b50d902SRodney W. Grimes 
632e37c5a3SMax Laier #include <err.h>
642e37c5a3SMax Laier #include <errno.h>
65c2dfd19fSGleb Smirnoff #include <libutil.h>
669b50d902SRodney W. Grimes #include <signal.h>
677b95a1ebSYaroslav Tykhiy #include <stdint.h>
689b50d902SRodney W. Grimes #include <stdio.h>
69591c194aSGuido van Rooij #include <stdlib.h>
709b50d902SRodney W. Grimes #include <string.h>
719b50d902SRodney W. Grimes #include <unistd.h>
729b50d902SRodney W. Grimes 
739b50d902SRodney W. Grimes #include "netstat.h"
749b50d902SRodney W. Grimes 
759b50d902SRodney W. Grimes #define	YES	1
769b50d902SRodney W. Grimes #define	NO	0
779b50d902SRodney W. Grimes 
78b6c86f4bSBruce Evans static void sidewaysintpr(int, u_long);
795e051718SAssar Westerlund static void catchalarm(int);
809b50d902SRodney W. Grimes 
81cfa1ca9dSYoshinobu Inoue #ifdef INET6
82cfa1ca9dSYoshinobu Inoue static char ntop_buf[INET6_ADDRSTRLEN];		/* for inet_ntop() */
83cfa1ca9dSYoshinobu Inoue #endif
84cfa1ca9dSYoshinobu Inoue 
852e37c5a3SMax Laier /*
862e37c5a3SMax Laier  * Dump pfsync statistics structure.
872e37c5a3SMax Laier  */
882e37c5a3SMax Laier void
89feda1a43SJohn Baldwin pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
902e37c5a3SMax Laier {
912e37c5a3SMax Laier 	struct pfsyncstats pfsyncstat, zerostat;
922e37c5a3SMax Laier 	size_t len = sizeof(struct pfsyncstats);
93445f17bbSJosef Karthauser 
94feda1a43SJohn Baldwin 	if (live) {
952e37c5a3SMax Laier 		if (zflag)
962e37c5a3SMax Laier 			memset(&zerostat, 0, len);
972e37c5a3SMax Laier 		if (sysctlbyname("net.inet.pfsync.stats", &pfsyncstat, &len,
982e37c5a3SMax Laier 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
992e37c5a3SMax Laier 			if (errno != ENOENT)
1002e37c5a3SMax Laier 				warn("sysctl: net.inet.pfsync.stats");
1012e37c5a3SMax Laier 			return;
1022e37c5a3SMax Laier 		}
103feda1a43SJohn Baldwin 	} else
104feda1a43SJohn Baldwin 		kread(off, &pfsyncstat, len);
105445f17bbSJosef Karthauser 
1062e37c5a3SMax Laier 	printf("%s:\n", name);
1072e37c5a3SMax Laier 
1082e37c5a3SMax Laier #define	p(f, m) if (pfsyncstat.f || sflag <= 1) \
1097b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f))
1102e37c5a3SMax Laier #define	p2(f, m) if (pfsyncstat.f || sflag <= 1) \
1117b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)pfsyncstat.f)
1122e37c5a3SMax Laier 
1137b95a1ebSYaroslav Tykhiy 	p(pfsyncs_ipackets, "\t%ju packet%s received (IPv4)\n");
1147b95a1ebSYaroslav Tykhiy 	p(pfsyncs_ipackets6, "\t%ju packet%s received (IPv6)\n");
1157b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badif, "\t\t%ju packet%s discarded for bad interface\n");
1167b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badttl, "\t\t%ju packet%s discarded for bad ttl\n");
1177b95a1ebSYaroslav Tykhiy 	p(pfsyncs_hdrops, "\t\t%ju packet%s shorter than header\n");
1187b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badver, "\t\t%ju packet%s discarded for bad version\n");
1197b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badauth, "\t\t%ju packet%s discarded for bad HMAC\n");
1207b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badact,"\t\t%ju packet%s discarded for bad action\n");
1217b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badlen, "\t\t%ju packet%s discarded for short packet\n");
1227b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badval, "\t\t%ju state%s discarded for bad values\n");
1237b95a1ebSYaroslav Tykhiy 	p(pfsyncs_stale, "\t\t%ju stale state%s\n");
1247b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badstate, "\t\t%ju failed state lookup/insert%s\n");
1257b95a1ebSYaroslav Tykhiy 	p(pfsyncs_opackets, "\t%ju packet%s sent (IPv4)\n");
1267b95a1ebSYaroslav Tykhiy 	p(pfsyncs_opackets6, "\t%ju packet%s sent (IPv6)\n");
1277b95a1ebSYaroslav Tykhiy 	p2(pfsyncs_onomem, "\t\t%ju send failed due to mbuf memory error\n");
1287b95a1ebSYaroslav Tykhiy 	p2(pfsyncs_oerrors, "\t\t%ju send error\n");
1292e37c5a3SMax Laier #undef p
1302e37c5a3SMax Laier #undef p2
1312e37c5a3SMax Laier }
132445f17bbSJosef Karthauser 
133445f17bbSJosef Karthauser /*
134445f17bbSJosef Karthauser  * Display a formatted value, or a '-' in the same space.
135445f17bbSJosef Karthauser  */
1365e051718SAssar Westerlund static void
137f964d60dSAssar Westerlund show_stat(const char *fmt, int width, u_long value, short showvalue)
138445f17bbSJosef Karthauser {
1391f575ce8SBruce Evans 	const char *lsep, *rsep;
140445f17bbSJosef Karthauser 	char newfmt[32];
141445f17bbSJosef Karthauser 
1421f575ce8SBruce Evans 	lsep = "";
1431f575ce8SBruce Evans 	if (strncmp(fmt, "LS", 2) == 0) {
1441f575ce8SBruce Evans 		lsep = " ";
1451f575ce8SBruce Evans 		fmt += 2;
1461f575ce8SBruce Evans 	}
1471f575ce8SBruce Evans 	rsep = " ";
1481f575ce8SBruce Evans 	if (strncmp(fmt, "NRS", 3) == 0) {
1491f575ce8SBruce Evans 		rsep = "";
1501f575ce8SBruce Evans 		fmt += 3;
1511f575ce8SBruce Evans 	}
152c2dfd19fSGleb Smirnoff 	if (showvalue == 0) {
153c2dfd19fSGleb Smirnoff 		/* Print just dash. */
1541f575ce8SBruce Evans 		sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
155445f17bbSJosef Karthauser 		printf(newfmt, "-");
156c2dfd19fSGleb Smirnoff 		return;
157445f17bbSJosef Karthauser 	}
158445f17bbSJosef Karthauser 
159c2dfd19fSGleb Smirnoff 	if (hflag) {
160c2dfd19fSGleb Smirnoff 		char buf[5];
161445f17bbSJosef Karthauser 
162c2dfd19fSGleb Smirnoff 		/* Format in human readable form. */
163c2dfd19fSGleb Smirnoff 		humanize_number(buf, sizeof(buf), (int64_t)value, "",
164c2dfd19fSGleb Smirnoff 		    HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
1651f575ce8SBruce Evans 		sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
166c2dfd19fSGleb Smirnoff 		printf(newfmt, buf);
167c2dfd19fSGleb Smirnoff 	} else {
168c2dfd19fSGleb Smirnoff 		/* Construct the format string. */
1691f575ce8SBruce Evans 		sprintf(newfmt, "%s%%%d%s%s", lsep, width, fmt, rsep);
170c2dfd19fSGleb Smirnoff 		printf(newfmt, value);
171c2dfd19fSGleb Smirnoff 	}
172c2dfd19fSGleb Smirnoff }
173445f17bbSJosef Karthauser 
1749b50d902SRodney W. Grimes /*
1759b50d902SRodney W. Grimes  * Print a description of the network interfaces.
1769b50d902SRodney W. Grimes  */
1779b50d902SRodney W. Grimes void
178b6c86f4bSBruce Evans intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *))
1799b50d902SRodney W. Grimes {
1809b50d902SRodney W. Grimes 	struct ifnet ifnet;
1810e27dc05SGarrett Wollman 	struct ifnethead ifnethead;
1829b50d902SRodney W. Grimes 	union {
1839b50d902SRodney W. Grimes 		struct ifaddr ifa;
1849b50d902SRodney W. Grimes 		struct in_ifaddr in;
185cfa1ca9dSYoshinobu Inoue #ifdef INET6
186cfa1ca9dSYoshinobu Inoue 		struct in6_ifaddr in6;
187cfa1ca9dSYoshinobu Inoue #endif
188cc6a66f2SJulian Elischer 		struct ipx_ifaddr ipx;
1899b50d902SRodney W. Grimes 	} ifaddr;
1909b50d902SRodney W. Grimes 	u_long ifaddraddr;
191f6719675SBill Fenner 	u_long ifaddrfound;
192f6719675SBill Fenner 	u_long ifnetfound;
1935da9f8faSJosef Karthauser 	u_long opackets;
1945da9f8faSJosef Karthauser 	u_long ipackets;
1955da9f8faSJosef Karthauser 	u_long obytes;
1965da9f8faSJosef Karthauser 	u_long ibytes;
197e1655201SRuslan Ermilov 	u_long omcasts;
198e1655201SRuslan Ermilov 	u_long imcasts;
1995da9f8faSJosef Karthauser 	u_long oerrors;
2005da9f8faSJosef Karthauser 	u_long ierrors;
2015da9f8faSJosef Karthauser 	u_long collisions;
2025da9f8faSJosef Karthauser 	short timer;
2035da9f8faSJosef Karthauser 	int drops;
204a97a9922SJulian Elischer 	struct sockaddr *sa = NULL;
2059bf40edeSBrooks Davis 	char name[IFNAMSIZ];
206445f17bbSJosef Karthauser 	short network_layer;
207445f17bbSJosef Karthauser 	short link_layer;
2089b50d902SRodney W. Grimes 
2099b50d902SRodney W. Grimes 	if (ifnetaddr == 0) {
2109b50d902SRodney W. Grimes 		printf("ifnet: symbol not defined\n");
2119b50d902SRodney W. Grimes 		return;
2129b50d902SRodney W. Grimes 	}
213b6c86f4bSBruce Evans 	if (interval1) {
214b6c86f4bSBruce Evans 		sidewaysintpr(interval1, ifnetaddr);
2159b50d902SRodney W. Grimes 		return;
2169b50d902SRodney W. Grimes 	}
21765475bc8SDavid E. O'Brien 	if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead) != 0)
2189b50d902SRodney W. Grimes 		return;
2194d51ef63SPoul-Henning Kamp 	ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
22065475bc8SDavid E. O'Brien 	if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
2210e27dc05SGarrett Wollman 		return;
2220e27dc05SGarrett Wollman 
223cf5e44f8SRuslan Ermilov 	if (!pfunc) {
22425d295e1SBruce M Simpson 		if (Wflag)
22525d295e1SBruce M Simpson 			printf("%-7.7s", "Name");
22625d295e1SBruce M Simpson 		else
22725d295e1SBruce M Simpson 			printf("%-5.5s", "Name");
22825d295e1SBruce M Simpson 		printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s",
22925d295e1SBruce M Simpson 		    "Mtu", "Network", "Address", "Ipkts", "Ierrs");
230e1e293a5SDavid Greenman 		if (bflag)
231e1e293a5SDavid Greenman 			printf(" %10.10s","Ibytes");
232e1e293a5SDavid Greenman 		printf(" %8.8s %5.5s", "Opkts", "Oerrs");
233e1e293a5SDavid Greenman 		if (bflag)
234e1e293a5SDavid Greenman 			printf(" %10.10s","Obytes");
2359b50d902SRodney W. Grimes 		printf(" %5s", "Coll");
2369b50d902SRodney W. Grimes 		if (tflag)
2379b50d902SRodney W. Grimes 			printf(" %s", "Time");
2389b50d902SRodney W. Grimes 		if (dflag)
2399b50d902SRodney W. Grimes 			printf(" %s", "Drop");
2409b50d902SRodney W. Grimes 		putchar('\n');
241cfa1ca9dSYoshinobu Inoue 	}
2429b50d902SRodney W. Grimes 	ifaddraddr = 0;
2439b50d902SRodney W. Grimes 	while (ifnetaddr || ifaddraddr) {
2446cc6f122SPhilippe Charnier 		struct sockaddr_in *sockin;
245cfa1ca9dSYoshinobu Inoue #ifdef INET6
2466cc6f122SPhilippe Charnier 		struct sockaddr_in6 *sockin6;
247cfa1ca9dSYoshinobu Inoue #endif
248a01e3379SDavid Malone 		char *cp;
2499b50d902SRodney W. Grimes 		int n, m;
2509b50d902SRodney W. Grimes 
251445f17bbSJosef Karthauser 		network_layer = 0;
252445f17bbSJosef Karthauser 		link_layer = 0;
253445f17bbSJosef Karthauser 
2549b50d902SRodney W. Grimes 		if (ifaddraddr == 0) {
255f6719675SBill Fenner 			ifnetfound = ifnetaddr;
25665475bc8SDavid E. O'Brien 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
2579b50d902SRodney W. Grimes 				return;
2589bf40edeSBrooks Davis 			strlcpy(name, ifnet.if_xname, sizeof(name));
2594d51ef63SPoul-Henning Kamp 			ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
26065475bc8SDavid E. O'Brien 			if (interface != 0 && strcmp(name, interface) != 0)
2619b50d902SRodney W. Grimes 				continue;
2629b50d902SRodney W. Grimes 			cp = index(name, '\0');
263cfa1ca9dSYoshinobu Inoue 
264cfa1ca9dSYoshinobu Inoue 			if (pfunc) {
265cfa1ca9dSYoshinobu Inoue 				(*pfunc)(name);
266cfa1ca9dSYoshinobu Inoue 				continue;
267cfa1ca9dSYoshinobu Inoue 			}
268cfa1ca9dSYoshinobu Inoue 
2699b50d902SRodney W. Grimes 			if ((ifnet.if_flags&IFF_UP) == 0)
2709b50d902SRodney W. Grimes 				*cp++ = '*';
2719b50d902SRodney W. Grimes 			*cp = '\0';
2724d51ef63SPoul-Henning Kamp 			ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
2739b50d902SRodney W. Grimes 		}
274f6719675SBill Fenner 		ifaddrfound = ifaddraddr;
2755da9f8faSJosef Karthauser 
2765da9f8faSJosef Karthauser 		/*
2775da9f8faSJosef Karthauser 		 * Get the interface stats.  These may get
2785da9f8faSJosef Karthauser 		 * overriden below on a per-interface basis.
2795da9f8faSJosef Karthauser 		 */
2805da9f8faSJosef Karthauser 		opackets = ifnet.if_opackets;
2815da9f8faSJosef Karthauser 		ipackets = ifnet.if_ipackets;
2825da9f8faSJosef Karthauser 		obytes = ifnet.if_obytes;
2835da9f8faSJosef Karthauser 		ibytes = ifnet.if_ibytes;
284e1655201SRuslan Ermilov 		omcasts = ifnet.if_omcasts;
285e1655201SRuslan Ermilov 		imcasts = ifnet.if_imcasts;
2865da9f8faSJosef Karthauser 		oerrors = ifnet.if_oerrors;
2875da9f8faSJosef Karthauser 		ierrors = ifnet.if_ierrors;
2885da9f8faSJosef Karthauser 		collisions = ifnet.if_collisions;
2895da9f8faSJosef Karthauser 		timer = ifnet.if_timer;
2905da9f8faSJosef Karthauser 		drops = ifnet.if_snd.ifq_drops;
2915da9f8faSJosef Karthauser 
2929b50d902SRodney W. Grimes 		if (ifaddraddr == 0) {
29325d295e1SBruce M Simpson 			if (Wflag)
29425d295e1SBruce M Simpson 				printf("%-7.7s", name);
29525d295e1SBruce M Simpson 			else
29625d295e1SBruce M Simpson 				printf("%-5.5s", name);
29725d295e1SBruce M Simpson 			printf(" %5lu ", ifnet.if_mtu);
298acb60e59SRuslan Ermilov 			printf("%-13.13s ", "none");
299dd1f8b9bSMatthew N. Dodd 			printf("%-17.17s ", "none");
3009b50d902SRodney W. Grimes 		} else {
30165475bc8SDavid E. O'Brien 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)
30265475bc8SDavid E. O'Brien 			    != 0) {
3039b50d902SRodney W. Grimes 				ifaddraddr = 0;
3049b50d902SRodney W. Grimes 				continue;
3059b50d902SRodney W. Grimes 			}
3069b50d902SRodney W. Grimes #define	CP(x) ((char *)(x))
3079b50d902SRodney W. Grimes 			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
308a97a9922SJulian Elischer 				CP(&ifaddr);
309a97a9922SJulian Elischer 			sa = (struct sockaddr *)cp;
310d44ddba9SRuslan Ermilov 			if (af != AF_UNSPEC && sa->sa_family != af) {
311d44ddba9SRuslan Ermilov 				ifaddraddr =
312d44ddba9SRuslan Ermilov 				    (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
313d44ddba9SRuslan Ermilov 				continue;
314d44ddba9SRuslan Ermilov 			}
31525d295e1SBruce M Simpson 			if (Wflag)
31625d295e1SBruce M Simpson 				printf("%-7.7s", name);
31725d295e1SBruce M Simpson 			else
31825d295e1SBruce M Simpson 				printf("%-5.5s", name);
31925d295e1SBruce M Simpson 			printf(" %5lu ", ifnet.if_mtu);
3209b50d902SRodney W. Grimes 			switch (sa->sa_family) {
3219b50d902SRodney W. Grimes 			case AF_UNSPEC:
322cc6a66f2SJulian Elischer 				printf("%-13.13s ", "none");
3239b50d902SRodney W. Grimes 				printf("%-15.15s ", "none");
3249b50d902SRodney W. Grimes 				break;
3259b50d902SRodney W. Grimes 			case AF_INET:
3266cc6f122SPhilippe Charnier 				sockin = (struct sockaddr_in *)sa;
3279b50d902SRodney W. Grimes #ifdef notdef
3289b50d902SRodney W. Grimes 				/* can't use inet_makeaddr because kernel
3299b50d902SRodney W. Grimes 				 * keeps nets unshifted.
3309b50d902SRodney W. Grimes 				 */
3319b50d902SRodney W. Grimes 				in = inet_makeaddr(ifaddr.in.ia_subnet,
3329b50d902SRodney W. Grimes 					INADDR_ANY);
33309a67ffaSStefan Eßer 				printf("%-13.13s ", netname(in.s_addr,
3349b50d902SRodney W. Grimes 				    ifaddr.in.ia_subnetmask));
3359b50d902SRodney W. Grimes #else
336acb60e59SRuslan Ermilov 				printf("%-13.13s ",
3379b50d902SRodney W. Grimes 				    netname(htonl(ifaddr.in.ia_subnet),
3389b50d902SRodney W. Grimes 				    ifaddr.in.ia_subnetmask));
3399b50d902SRodney W. Grimes #endif
340dd1f8b9bSMatthew N. Dodd 				printf("%-17.17s ",
3416cc6f122SPhilippe Charnier 				    routename(sockin->sin_addr.s_addr));
342445f17bbSJosef Karthauser 
343445f17bbSJosef Karthauser 				network_layer = 1;
3449b50d902SRodney W. Grimes 				break;
345cfa1ca9dSYoshinobu Inoue #ifdef INET6
346cfa1ca9dSYoshinobu Inoue 			case AF_INET6:
3476cc6f122SPhilippe Charnier 				sockin6 = (struct sockaddr_in6 *)sa;
348acb60e59SRuslan Ermilov 				printf("%-13.13s ",
349cfa1ca9dSYoshinobu Inoue 				       netname6(&ifaddr.in6.ia_addr,
350cfa1ca9dSYoshinobu Inoue 						&ifaddr.in6.ia_prefixmask.sin6_addr));
351cfa1ca9dSYoshinobu Inoue 				printf("%-17.17s ",
352a01e3379SDavid Malone 				    inet_ntop(AF_INET6,
3536cc6f122SPhilippe Charnier 					&sockin6->sin6_addr,
354cfa1ca9dSYoshinobu Inoue 					ntop_buf, sizeof(ntop_buf)));
355445f17bbSJosef Karthauser 
356445f17bbSJosef Karthauser 				network_layer = 1;
357cfa1ca9dSYoshinobu Inoue 				break;
358cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
359cc6a66f2SJulian Elischer 			case AF_IPX:
360cc6a66f2SJulian Elischer 				{
361cc6a66f2SJulian Elischer 				struct sockaddr_ipx *sipx =
362cc6a66f2SJulian Elischer 					(struct sockaddr_ipx *)sa;
363cc6a66f2SJulian Elischer 				u_long net;
364cc6a66f2SJulian Elischer 				char netnum[10];
365cc6a66f2SJulian Elischer 
366cc6a66f2SJulian Elischer 				*(union ipx_net *) &net = sipx->sipx_addr.x_net;
36722694ebaSBruce Evans 				sprintf(netnum, "%lx", (u_long)ntohl(net));
368cc6a66f2SJulian Elischer 				printf("ipx:%-8s  ", netnum);
369cc6a66f2SJulian Elischer /*				printf("ipx:%-8s ", netname(net, 0L)); */
370acb60e59SRuslan Ermilov 				printf("%-17s ",
371cc6a66f2SJulian Elischer 				    ipx_phost((struct sockaddr *)sipx));
372cc6a66f2SJulian Elischer 				}
3736f9cdfceSMatthew N. Dodd 
3746f9cdfceSMatthew N. Dodd 				network_layer = 1;
375cc6a66f2SJulian Elischer 				break;
3766ffcfd6cSJulian Elischer 
3776ffcfd6cSJulian Elischer 			case AF_APPLETALK:
378a8d37845SJulian Elischer 				printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
379acb60e59SRuslan Ermilov 				printf("%-11.11s  ",atalk_print(sa,0x0b) );
3806ffcfd6cSJulian Elischer 				break;
3819b50d902SRodney W. Grimes 			case AF_LINK:
3829b50d902SRodney W. Grimes 				{
3839b50d902SRodney W. Grimes 				struct sockaddr_dl *sdl =
3849b50d902SRodney W. Grimes 					(struct sockaddr_dl *)sa;
385cfa1ca9dSYoshinobu Inoue 				char linknum[10];
3869b50d902SRodney W. Grimes 				cp = (char *)LLADDR(sdl);
3879b50d902SRodney W. Grimes 				n = sdl->sdl_alen;
388cfa1ca9dSYoshinobu Inoue 				sprintf(linknum, "<Link#%d>", sdl->sdl_index);
389acb60e59SRuslan Ermilov 				m = printf("%-13.13s ", linknum);
3909b50d902SRodney W. Grimes 				}
3919b50d902SRodney W. Grimes 				goto hexprint;
3929b50d902SRodney W. Grimes 			default:
3939b50d902SRodney W. Grimes 				m = printf("(%d)", sa->sa_family);
3949b50d902SRodney W. Grimes 				for (cp = sa->sa_len + (char *)sa;
3959b50d902SRodney W. Grimes 					--cp > sa->sa_data && (*cp == 0);) {}
3969b50d902SRodney W. Grimes 				n = cp - sa->sa_data + 1;
3979b50d902SRodney W. Grimes 				cp = sa->sa_data;
3989b50d902SRodney W. Grimes 			hexprint:
3999b50d902SRodney W. Grimes 				while (--n >= 0)
400541f2562SDavid Greenman 					m += printf("%02x%c", *cp++ & 0xff,
401e54ca68cSJordan K. Hubbard 						    n > 0 ? ':' : ' ');
402acb60e59SRuslan Ermilov 				m = 32 - m;
4039b50d902SRodney W. Grimes 				while (m-- > 0)
4049b50d902SRodney W. Grimes 					putchar(' ');
405445f17bbSJosef Karthauser 
406445f17bbSJosef Karthauser 				link_layer = 1;
4079b50d902SRodney W. Grimes 				break;
4089b50d902SRodney W. Grimes 			}
4095da9f8faSJosef Karthauser 
4105da9f8faSJosef Karthauser 			/*
4115da9f8faSJosef Karthauser 			 * Fixup the statistics for interfaces that
4125da9f8faSJosef Karthauser 			 * update stats for their network addresses
4135da9f8faSJosef Karthauser 			 */
414445f17bbSJosef Karthauser 			if (network_layer) {
4155da9f8faSJosef Karthauser 				opackets = ifaddr.in.ia_ifa.if_opackets;
4165da9f8faSJosef Karthauser 				ipackets = ifaddr.in.ia_ifa.if_ipackets;
4175da9f8faSJosef Karthauser 				obytes = ifaddr.in.ia_ifa.if_obytes;
4185da9f8faSJosef Karthauser 				ibytes = ifaddr.in.ia_ifa.if_ibytes;
4195da9f8faSJosef Karthauser 			}
4205da9f8faSJosef Karthauser 
4214d51ef63SPoul-Henning Kamp 			ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
4229b50d902SRodney W. Grimes 		}
4235da9f8faSJosef Karthauser 
424445f17bbSJosef Karthauser 		show_stat("lu", 8, ipackets, link_layer|network_layer);
425445f17bbSJosef Karthauser 		show_stat("lu", 5, ierrors, link_layer);
4267c23a867SGleb Smirnoff 		if (bflag)
427445f17bbSJosef Karthauser 			show_stat("lu", 10, ibytes, link_layer|network_layer);
4287c23a867SGleb Smirnoff 
429445f17bbSJosef Karthauser 		show_stat("lu", 8, opackets, link_layer|network_layer);
430445f17bbSJosef Karthauser 		show_stat("lu", 5, oerrors, link_layer);
4317c23a867SGleb Smirnoff 		if (bflag)
432445f17bbSJosef Karthauser 			show_stat("lu", 10, obytes, link_layer|network_layer);
4337c23a867SGleb Smirnoff 
4341f575ce8SBruce Evans 		show_stat("NRSlu", 5, collisions, link_layer);
4357c23a867SGleb Smirnoff 		if (tflag)
4361f575ce8SBruce Evans 			show_stat("LSd", 4, timer, link_layer);
4377c23a867SGleb Smirnoff 		if (dflag)
4381f575ce8SBruce Evans 			show_stat("LSd", 4, drops, link_layer);
4399b50d902SRodney W. Grimes 		putchar('\n');
4401f575ce8SBruce Evans 
441f6719675SBill Fenner 		if (aflag && ifaddrfound) {
442f6719675SBill Fenner 			/*
443f6719675SBill Fenner 			 * Print family's multicast addresses
444f6719675SBill Fenner 			 */
4454d51ef63SPoul-Henning Kamp 			struct ifmultiaddr *multiaddr;
44675fb8770SGarrett Wollman 			struct ifmultiaddr ifma;
44775fb8770SGarrett Wollman 			union {
44875fb8770SGarrett Wollman 				struct sockaddr sa;
44975fb8770SGarrett Wollman 				struct sockaddr_in in;
450cfa1ca9dSYoshinobu Inoue #ifdef INET6
451cfa1ca9dSYoshinobu Inoue 				struct sockaddr_in6 in6;
452cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
45375fb8770SGarrett Wollman 				struct sockaddr_dl dl;
45475fb8770SGarrett Wollman 			} msa;
45575fb8770SGarrett Wollman 			const char *fmt;
456f6719675SBill Fenner 
4576817526dSPoul-Henning Kamp 			TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
4580b23654bSPoul-Henning Kamp 				if (kread((u_long)multiaddr, (char *)&ifma,
45965475bc8SDavid E. O'Brien 					  sizeof ifma) != 0)
460f6719675SBill Fenner 					break;
4610b23654bSPoul-Henning Kamp 				multiaddr = &ifma;
46275fb8770SGarrett Wollman 				if (kread((u_long)ifma.ifma_addr, (char *)&msa,
46365475bc8SDavid E. O'Brien 					  sizeof msa) != 0)
46475fb8770SGarrett Wollman 					break;
46575fb8770SGarrett Wollman 				if (msa.sa.sa_family != sa->sa_family)
46675fb8770SGarrett Wollman 					continue;
46775fb8770SGarrett Wollman 
46875fb8770SGarrett Wollman 				fmt = 0;
46975fb8770SGarrett Wollman 				switch (msa.sa.sa_family) {
47075fb8770SGarrett Wollman 				case AF_INET:
47175fb8770SGarrett Wollman 					fmt = routename(msa.in.sin_addr.s_addr);
47275fb8770SGarrett Wollman 					break;
473cfa1ca9dSYoshinobu Inoue #ifdef INET6
474cfa1ca9dSYoshinobu Inoue 				case AF_INET6:
475bce2e7c8SRuslan Ermilov 					printf("%*s %-19.19s(refs: %d)\n",
476bce2e7c8SRuslan Ermilov 					       Wflag ? 27 : 25, "",
477cfa1ca9dSYoshinobu Inoue 					       inet_ntop(AF_INET6,
478cfa1ca9dSYoshinobu Inoue 							 &msa.in6.sin6_addr,
479cfa1ca9dSYoshinobu Inoue 							 ntop_buf,
480cfa1ca9dSYoshinobu Inoue 							 sizeof(ntop_buf)),
481cfa1ca9dSYoshinobu Inoue 					       ifma.ifma_refcount);
482b9d92bf5SBill Fenner 					break;
483cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
484f6719675SBill Fenner 				case AF_LINK:
485b9d92bf5SBill Fenner 					switch (msa.dl.sdl_type) {
486f6719675SBill Fenner 					case IFT_ETHER:
48775fb8770SGarrett Wollman 					case IFT_FDDI:
48875fb8770SGarrett Wollman 						fmt = ether_ntoa(
48975fb8770SGarrett Wollman 							(struct ether_addr *)
49075fb8770SGarrett Wollman 							LLADDR(&msa.dl));
49175fb8770SGarrett Wollman 						break;
492f6719675SBill Fenner 					}
493f6719675SBill Fenner 					break;
494f6719675SBill Fenner 				}
495e1655201SRuslan Ermilov 				if (fmt) {
496e1655201SRuslan Ermilov 					printf("%*s %-17.17s",
497bce2e7c8SRuslan Ermilov 					    Wflag ? 27 : 25, "", fmt);
498e1655201SRuslan Ermilov 					if (msa.sa.sa_family == AF_LINK) {
499e1655201SRuslan Ermilov 						printf(" %8lu", imcasts);
500e1655201SRuslan Ermilov 						printf("%*s",
501e1655201SRuslan Ermilov 						    bflag ? 17 : 6, "");
502e1655201SRuslan Ermilov 						printf(" %8lu", omcasts);
503e1655201SRuslan Ermilov 					}
504e1655201SRuslan Ermilov 					putchar('\n');
505e1655201SRuslan Ermilov 				}
506f6719675SBill Fenner 			}
507f6719675SBill Fenner 		}
5089b50d902SRodney W. Grimes 	}
5099b50d902SRodney W. Grimes }
5109b50d902SRodney W. Grimes 
5119b50d902SRodney W. Grimes struct	iftot {
5124d51ef63SPoul-Henning Kamp 	SLIST_ENTRY(iftot) chain;
5139bf40edeSBrooks Davis 	char	ift_name[IFNAMSIZ];	/* interface name */
514890bc2a5SDoug Rabson 	u_long	ift_ip;			/* input packets */
515890bc2a5SDoug Rabson 	u_long	ift_ie;			/* input errors */
516890bc2a5SDoug Rabson 	u_long	ift_op;			/* output packets */
517890bc2a5SDoug Rabson 	u_long	ift_oe;			/* output errors */
518890bc2a5SDoug Rabson 	u_long	ift_co;			/* collisions */
519e1e293a5SDavid Greenman 	u_int	ift_dr;			/* drops */
520890bc2a5SDoug Rabson 	u_long	ift_ib;			/* input bytes */
521890bc2a5SDoug Rabson 	u_long	ift_ob;			/* output bytes */
522591c194aSGuido van Rooij };
5239b50d902SRodney W. Grimes 
5249b50d902SRodney W. Grimes u_char	signalled;			/* set if alarm goes off "early" */
5259b50d902SRodney W. Grimes 
5269b50d902SRodney W. Grimes /*
5279b50d902SRodney W. Grimes  * Print a running summary of interface statistics.
528b6c86f4bSBruce Evans  * Repeat display every interval1 seconds, showing statistics
529b6c86f4bSBruce Evans  * collected over that interval.  Assumes that interval1 is non-zero.
5309b50d902SRodney W. Grimes  * First line printed at top of screen is always cumulative.
5310e27dc05SGarrett Wollman  * XXX - should be rewritten to use ifmib(4).
5329b50d902SRodney W. Grimes  */
5339b50d902SRodney W. Grimes static void
534b6c86f4bSBruce Evans sidewaysintpr(int interval1, u_long off)
5359b50d902SRodney W. Grimes {
5369b50d902SRodney W. Grimes 	struct ifnet ifnet;
5379b50d902SRodney W. Grimes 	u_long firstifnet;
5380e27dc05SGarrett Wollman 	struct ifnethead ifnethead;
53993547b07SBruce Evans 	struct itimerval interval_it;
540591c194aSGuido van Rooij 	struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
541a01e3379SDavid Malone 	int line;
5420b87c915SDavid Greenman 	int oldmask, first;
5430b87c915SDavid Greenman 	u_long interesting_off;
5449b50d902SRodney W. Grimes 
54565475bc8SDavid E. O'Brien 	if (kread(off, (char *)&ifnethead, sizeof ifnethead) != 0)
5469b50d902SRodney W. Grimes 		return;
5474d51ef63SPoul-Henning Kamp 	firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
5480e27dc05SGarrett Wollman 
549591c194aSGuido van Rooij 	if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
550591c194aSGuido van Rooij 		printf("malloc failed\n");
551591c194aSGuido van Rooij 		exit(1);
552591c194aSGuido van Rooij 	}
553591c194aSGuido van Rooij 	memset(iftot, 0, sizeof(struct iftot));
554591c194aSGuido van Rooij 
5550b87c915SDavid Greenman 	interesting = NULL;
5560b87c915SDavid Greenman 	interesting_off = 0;
5579b50d902SRodney W. Grimes 	for (off = firstifnet, ip = iftot; off;) {
5589bf40edeSBrooks Davis 		char name[IFNAMSIZ];
5599b50d902SRodney W. Grimes 
56065475bc8SDavid E. O'Brien 		if (kread(off, (char *)&ifnet, sizeof ifnet) != 0)
5619b50d902SRodney W. Grimes 			break;
5629bf40edeSBrooks Davis 		strlcpy(name, ifnet.if_xname, sizeof(name));
5630b87c915SDavid Greenman 		if (interface && strcmp(name, interface) == 0) {
5649b50d902SRodney W. Grimes 			interesting = ip;
5650b87c915SDavid Greenman 			interesting_off = off;
5660b87c915SDavid Greenman 		}
567ec3b72e9SRobert Drehmel 		snprintf(ip->ift_name, sizeof(ip->ift_name), "(%s)", name);;
568591c194aSGuido van Rooij 		if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
569591c194aSGuido van Rooij 			printf("malloc failed\n");
570591c194aSGuido van Rooij 			exit(1);
571591c194aSGuido van Rooij 		}
572591c194aSGuido van Rooij 		memset(ipn, 0, sizeof(struct iftot));
5734d51ef63SPoul-Henning Kamp 		SLIST_NEXT(ip, chain) = ipn;
574591c194aSGuido van Rooij 		ip = ipn;
5754d51ef63SPoul-Henning Kamp 		off = (u_long)TAILQ_NEXT(&ifnet, if_link);
5769b50d902SRodney W. Grimes 	}
577c6358a5eSRuslan Ermilov 	if (interface && interesting == NULL)
578c6358a5eSRuslan Ermilov 		errx(1, "%s: unknown interface", interface);
579591c194aSGuido van Rooij 	if ((total = malloc(sizeof(struct iftot))) == NULL) {
580591c194aSGuido van Rooij 		printf("malloc failed\n");
581591c194aSGuido van Rooij 		exit(1);
582591c194aSGuido van Rooij 	}
583591c194aSGuido van Rooij 	memset(total, 0, sizeof(struct iftot));
584591c194aSGuido van Rooij 	if ((sum = malloc(sizeof(struct iftot))) == NULL) {
585591c194aSGuido van Rooij 		printf("malloc failed\n");
586591c194aSGuido van Rooij 		exit(1);
587591c194aSGuido van Rooij 	}
588591c194aSGuido van Rooij 	memset(sum, 0, sizeof(struct iftot));
589591c194aSGuido van Rooij 
5909b50d902SRodney W. Grimes 	(void)signal(SIGALRM, catchalarm);
5919b50d902SRodney W. Grimes 	signalled = NO;
59293547b07SBruce Evans 	interval_it.it_interval.tv_sec = interval1;
59393547b07SBruce Evans 	interval_it.it_interval.tv_usec = 0;
59493547b07SBruce Evans 	interval_it.it_value = interval_it.it_interval;
59593547b07SBruce Evans 	setitimer(ITIMER_REAL, &interval_it, NULL);
5960b87c915SDavid Greenman 	first = 1;
5970b87c915SDavid Greenman banner:
5980b87c915SDavid Greenman 	printf("%17s %14s %16s", "input",
5990b87c915SDavid Greenman 	    interesting ? interesting->ift_name : "(Total)", "output");
6009b50d902SRodney W. Grimes 	putchar('\n');
6010b87c915SDavid Greenman 	printf("%10s %5s %10s %10s %5s %10s %5s",
6020b87c915SDavid Greenman 	    "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
6039b50d902SRodney W. Grimes 	if (dflag)
6049b50d902SRodney W. Grimes 		printf(" %5.5s", "drops");
6059b50d902SRodney W. Grimes 	putchar('\n');
6069b50d902SRodney W. Grimes 	fflush(stdout);
6079b50d902SRodney W. Grimes 	line = 0;
6089b50d902SRodney W. Grimes loop:
6090b87c915SDavid Greenman 	if (interesting != NULL) {
6100b87c915SDavid Greenman 		ip = interesting;
61165475bc8SDavid E. O'Brien 		if (kread(interesting_off, (char *)&ifnet, sizeof ifnet) != 0) {
6120b87c915SDavid Greenman 			printf("???\n");
6130b87c915SDavid Greenman 			exit(1);
6140b87c915SDavid Greenman 		};
6150b87c915SDavid Greenman 		if (!first) {
6167c23a867SGleb Smirnoff 			show_stat("lu", 10, ifnet.if_ipackets - ip->ift_ip, 1);
6177c23a867SGleb Smirnoff 			show_stat("lu", 5, ifnet.if_ierrors - ip->ift_ie, 1);
6187c23a867SGleb Smirnoff 			show_stat("lu", 10, ifnet.if_ibytes - ip->ift_ib, 1);
6197c23a867SGleb Smirnoff 			show_stat("lu", 10, ifnet.if_opackets - ip->ift_op, 1);
6207c23a867SGleb Smirnoff 			show_stat("lu", 5, ifnet.if_oerrors - ip->ift_oe, 1);
6217c23a867SGleb Smirnoff 			show_stat("lu", 10, ifnet.if_obytes - ip->ift_ob, 1);
6221f575ce8SBruce Evans 			show_stat("NRSlu", 5,
6231f575ce8SBruce Evans 			    ifnet.if_collisions - ip->ift_co, 1);
6240b87c915SDavid Greenman 			if (dflag)
6251f575ce8SBruce Evans 				show_stat("LSu", 5,
6267c23a867SGleb Smirnoff 				    ifnet.if_snd.ifq_drops - ip->ift_dr, 1);
6270b87c915SDavid Greenman 		}
6280b87c915SDavid Greenman 		ip->ift_ip = ifnet.if_ipackets;
6290b87c915SDavid Greenman 		ip->ift_ie = ifnet.if_ierrors;
6300b87c915SDavid Greenman 		ip->ift_ib = ifnet.if_ibytes;
6310b87c915SDavid Greenman 		ip->ift_op = ifnet.if_opackets;
6320b87c915SDavid Greenman 		ip->ift_oe = ifnet.if_oerrors;
6330b87c915SDavid Greenman 		ip->ift_ob = ifnet.if_obytes;
6340b87c915SDavid Greenman 		ip->ift_co = ifnet.if_collisions;
6350b87c915SDavid Greenman 		ip->ift_dr = ifnet.if_snd.ifq_drops;
6360b87c915SDavid Greenman 	} else {
6379b50d902SRodney W. Grimes 		sum->ift_ip = 0;
6389b50d902SRodney W. Grimes 		sum->ift_ie = 0;
6390b87c915SDavid Greenman 		sum->ift_ib = 0;
6409b50d902SRodney W. Grimes 		sum->ift_op = 0;
6419b50d902SRodney W. Grimes 		sum->ift_oe = 0;
6420b87c915SDavid Greenman 		sum->ift_ob = 0;
6439b50d902SRodney W. Grimes 		sum->ift_co = 0;
6449b50d902SRodney W. Grimes 		sum->ift_dr = 0;
6454d51ef63SPoul-Henning Kamp 		for (off = firstifnet, ip = iftot;
6464d51ef63SPoul-Henning Kamp 		     off && SLIST_NEXT(ip, chain) != NULL;
6474d51ef63SPoul-Henning Kamp 		     ip = SLIST_NEXT(ip, chain)) {
64865475bc8SDavid E. O'Brien 			if (kread(off, (char *)&ifnet, sizeof ifnet) != 0) {
6499b50d902SRodney W. Grimes 				off = 0;
6509b50d902SRodney W. Grimes 				continue;
6519b50d902SRodney W. Grimes 			}
6520b87c915SDavid Greenman 			sum->ift_ip += ifnet.if_ipackets;
6530b87c915SDavid Greenman 			sum->ift_ie += ifnet.if_ierrors;
6540b87c915SDavid Greenman 			sum->ift_ib += ifnet.if_ibytes;
6550b87c915SDavid Greenman 			sum->ift_op += ifnet.if_opackets;
6560b87c915SDavid Greenman 			sum->ift_oe += ifnet.if_oerrors;
6570b87c915SDavid Greenman 			sum->ift_ob += ifnet.if_obytes;
6580b87c915SDavid Greenman 			sum->ift_co += ifnet.if_collisions;
6590b87c915SDavid Greenman 			sum->ift_dr += ifnet.if_snd.ifq_drops;
6604d51ef63SPoul-Henning Kamp 			off = (u_long)TAILQ_NEXT(&ifnet, if_link);
6619b50d902SRodney W. Grimes 		}
6620b87c915SDavid Greenman 		if (!first) {
6637c23a867SGleb Smirnoff 			show_stat("lu", 10, sum->ift_ip - total->ift_ip, 1);
6647c23a867SGleb Smirnoff 			show_stat("lu", 5, sum->ift_ie - total->ift_ie, 1);
6657c23a867SGleb Smirnoff 			show_stat("lu", 10, sum->ift_ib - total->ift_ib, 1);
6667c23a867SGleb Smirnoff 			show_stat("lu", 10, sum->ift_op - total->ift_op, 1);
6677c23a867SGleb Smirnoff 			show_stat("lu", 5, sum->ift_oe - total->ift_oe, 1);
6687c23a867SGleb Smirnoff 			show_stat("lu", 10, sum->ift_ob - total->ift_ob, 1);
6691f575ce8SBruce Evans 			show_stat("NRSlu", 5, sum->ift_co - total->ift_co, 1);
6709b50d902SRodney W. Grimes 			if (dflag)
6711f575ce8SBruce Evans 				show_stat("LSu", 5,
6727c23a867SGleb Smirnoff 				    sum->ift_dr - total->ift_dr, 1);
6739b50d902SRodney W. Grimes 		}
6749b50d902SRodney W. Grimes 		*total = *sum;
6750b87c915SDavid Greenman 	}
6760b87c915SDavid Greenman 	if (!first)
6779b50d902SRodney W. Grimes 		putchar('\n');
6789b50d902SRodney W. Grimes 	fflush(stdout);
6799b50d902SRodney W. Grimes 	oldmask = sigblock(sigmask(SIGALRM));
68093547b07SBruce Evans 	while (!signalled)
6819b50d902SRodney W. Grimes 		sigpause(0);
6829b50d902SRodney W. Grimes 	signalled = NO;
68393547b07SBruce Evans 	sigsetmask(oldmask);
6840b87c915SDavid Greenman 	line++;
6850b87c915SDavid Greenman 	first = 0;
6869b50d902SRodney W. Grimes 	if (line == 21)
6879b50d902SRodney W. Grimes 		goto banner;
6880b87c915SDavid Greenman 	else
6899b50d902SRodney W. Grimes 		goto loop;
6909b50d902SRodney W. Grimes 	/*NOTREACHED*/
6919b50d902SRodney W. Grimes }
6929b50d902SRodney W. Grimes 
6939b50d902SRodney W. Grimes /*
69493547b07SBruce Evans  * Set a flag to indicate that a signal from the periodic itimer has been
69593547b07SBruce Evans  * caught.
6969b50d902SRodney W. Grimes  */
6979b50d902SRodney W. Grimes static void
698f964d60dSAssar Westerlund catchalarm(int signo __unused)
6999b50d902SRodney W. Grimes {
7009b50d902SRodney W. Grimes 	signalled = YES;
7019b50d902SRodney W. Grimes }
702