xref: /freebsd/usr.bin/netstat/if.c (revision d6d3f01e0a3395c1fae34a3c4be7b051cb2d7581)
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  * 4. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
306cc6f122SPhilippe Charnier #if 0
319b50d902SRodney W. Grimes #ifndef lint
3205ddff6eSPeter Wemm static char sccsid[] = "@(#)if.c	8.3 (Berkeley) 4/28/95";
339b50d902SRodney W. Grimes #endif /* not lint */
346cc6f122SPhilippe Charnier #endif
356cc6f122SPhilippe Charnier 
366cc6f122SPhilippe Charnier #include <sys/cdefs.h>
376cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$");
389b50d902SRodney W. Grimes 
399b50d902SRodney W. Grimes #include <sys/types.h>
409b50d902SRodney W. Grimes #include <sys/protosw.h>
419b50d902SRodney W. Grimes #include <sys/socket.h>
42feda1a43SJohn Baldwin #include <sys/socketvar.h>
430024d1dbSLuigi Rizzo #include <sys/sysctl.h>
44628d2ac1SGarrett Wollman #include <sys/time.h>
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes #include <net/if.h>
4787669425SGarrett Wollman #include <net/if_var.h>
489b50d902SRodney W. Grimes #include <net/if_dl.h>
49f6719675SBill Fenner #include <net/if_types.h>
5075fb8770SGarrett Wollman #include <net/ethernet.h>
512e37c5a3SMax Laier #include <net/pfvar.h>
522e37c5a3SMax Laier #include <net/if_pfsync.h>
539b50d902SRodney W. Grimes #include <netinet/in.h>
549b50d902SRodney W. Grimes #include <netinet/in_var.h>
55cc6a66f2SJulian Elischer #include <netipx/ipx.h>
56cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
579b50d902SRodney W. Grimes #include <arpa/inet.h>
589b50d902SRodney W. Grimes 
592e37c5a3SMax Laier #include <err.h>
602e37c5a3SMax Laier #include <errno.h>
61c2dfd19fSGleb Smirnoff #include <libutil.h>
62cd05232aSHajimu UMEMOTO #ifdef INET6
63cd05232aSHajimu UMEMOTO #include <netdb.h>
64cd05232aSHajimu UMEMOTO #endif
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>
70821df508SXin LI #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 
77b6c86f4bSBruce Evans static void sidewaysintpr(int, u_long);
785e051718SAssar Westerlund static void catchalarm(int);
799b50d902SRodney W. Grimes 
80cfa1ca9dSYoshinobu Inoue #ifdef INET6
81cd05232aSHajimu UMEMOTO static char addr_buf[NI_MAXHOST];		/* for getnameinfo() */
82cfa1ca9dSYoshinobu Inoue #endif
83cfa1ca9dSYoshinobu Inoue 
84*d6d3f01eSGleb Smirnoff static const char* pfsyncacts[] = {
85*d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_CLR */		"clear all request",
86*d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_INS */		"state insert",
87*d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_INS_ACK */	"state inserted ack",
88*d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_UPD */		"state update",
89*d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_UPD_C */		"compressed state update",
90*d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_UPD_REQ */	"uncompressed state request",
91*d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_DEL */		"state delete",
92*d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_DEL_C */		"compressed state delete",
93*d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_INS_F */		"fragment insert",
94*d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_DEL_F */		"fragment delete",
95*d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_BUS */		"bulk update mark",
96*d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_TDB */		"TDB replay counter update",
97*d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_EOF */		"end of frame mark",
98*d6d3f01eSGleb Smirnoff };
99*d6d3f01eSGleb Smirnoff 
100*d6d3f01eSGleb Smirnoff static void
101*d6d3f01eSGleb Smirnoff pfsync_acts_stats(const char *fmt, uint64_t *a)
102*d6d3f01eSGleb Smirnoff {
103*d6d3f01eSGleb Smirnoff 	int i;
104*d6d3f01eSGleb Smirnoff 
105*d6d3f01eSGleb Smirnoff 	for (i = 0; i < PFSYNC_ACT_MAX; i++, a++)
106*d6d3f01eSGleb Smirnoff 		if (*a || sflag <= 1)
107*d6d3f01eSGleb Smirnoff 			printf(fmt, *a, pfsyncacts[i], plural(*a));
108*d6d3f01eSGleb Smirnoff }
109*d6d3f01eSGleb Smirnoff 
1102e37c5a3SMax Laier /*
1112e37c5a3SMax Laier  * Dump pfsync statistics structure.
1122e37c5a3SMax Laier  */
1132e37c5a3SMax Laier void
114feda1a43SJohn Baldwin pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1152e37c5a3SMax Laier {
1162e37c5a3SMax Laier 	struct pfsyncstats pfsyncstat, zerostat;
1172e37c5a3SMax Laier 	size_t len = sizeof(struct pfsyncstats);
118445f17bbSJosef Karthauser 
119feda1a43SJohn Baldwin 	if (live) {
1202e37c5a3SMax Laier 		if (zflag)
1212e37c5a3SMax Laier 			memset(&zerostat, 0, len);
12216410af7SGleb Smirnoff 		if (sysctlbyname("net.pfsync.stats", &pfsyncstat, &len,
1232e37c5a3SMax Laier 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1242e37c5a3SMax Laier 			if (errno != ENOENT)
12516410af7SGleb Smirnoff 				warn("sysctl: net.pfsync.stats");
1262e37c5a3SMax Laier 			return;
1272e37c5a3SMax Laier 		}
128feda1a43SJohn Baldwin 	} else
129feda1a43SJohn Baldwin 		kread(off, &pfsyncstat, len);
130445f17bbSJosef Karthauser 
1312e37c5a3SMax Laier 	printf("%s:\n", name);
1322e37c5a3SMax Laier 
1332e37c5a3SMax Laier #define	p(f, m) if (pfsyncstat.f || sflag <= 1) \
1347b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f))
1352e37c5a3SMax Laier 
1367b95a1ebSYaroslav Tykhiy 	p(pfsyncs_ipackets, "\t%ju packet%s received (IPv4)\n");
1377b95a1ebSYaroslav Tykhiy 	p(pfsyncs_ipackets6, "\t%ju packet%s received (IPv6)\n");
138*d6d3f01eSGleb Smirnoff 	pfsync_acts_stats("\t    %ju %s%s received\n",
139*d6d3f01eSGleb Smirnoff 	    &pfsyncstat.pfsyncs_iacts[0]);
1407b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badif, "\t\t%ju packet%s discarded for bad interface\n");
1417b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badttl, "\t\t%ju packet%s discarded for bad ttl\n");
1427b95a1ebSYaroslav Tykhiy 	p(pfsyncs_hdrops, "\t\t%ju packet%s shorter than header\n");
1437b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badver, "\t\t%ju packet%s discarded for bad version\n");
1447b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badauth, "\t\t%ju packet%s discarded for bad HMAC\n");
1457b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badact,"\t\t%ju packet%s discarded for bad action\n");
1467b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badlen, "\t\t%ju packet%s discarded for short packet\n");
1477b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badval, "\t\t%ju state%s discarded for bad values\n");
1487b95a1ebSYaroslav Tykhiy 	p(pfsyncs_stale, "\t\t%ju stale state%s\n");
1497b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badstate, "\t\t%ju failed state lookup/insert%s\n");
1507b95a1ebSYaroslav Tykhiy 	p(pfsyncs_opackets, "\t%ju packet%s sent (IPv4)\n");
1517b95a1ebSYaroslav Tykhiy 	p(pfsyncs_opackets6, "\t%ju packet%s sent (IPv6)\n");
152*d6d3f01eSGleb Smirnoff 	pfsync_acts_stats("\t    %ju %s%s sent\n",
153*d6d3f01eSGleb Smirnoff 	    &pfsyncstat.pfsyncs_oacts[0]);
154*d6d3f01eSGleb Smirnoff 	p(pfsyncs_onomem, "\t\t%ju failure%s due to mbuf memory error\n");
155*d6d3f01eSGleb Smirnoff 	p(pfsyncs_oerrors, "\t\t%ju send error%s\n");
1562e37c5a3SMax Laier #undef p
1572e37c5a3SMax Laier }
158445f17bbSJosef Karthauser 
159445f17bbSJosef Karthauser /*
160445f17bbSJosef Karthauser  * Display a formatted value, or a '-' in the same space.
161445f17bbSJosef Karthauser  */
1625e051718SAssar Westerlund static void
163f964d60dSAssar Westerlund show_stat(const char *fmt, int width, u_long value, short showvalue)
164445f17bbSJosef Karthauser {
1651f575ce8SBruce Evans 	const char *lsep, *rsep;
166445f17bbSJosef Karthauser 	char newfmt[32];
167445f17bbSJosef Karthauser 
1681f575ce8SBruce Evans 	lsep = "";
1691f575ce8SBruce Evans 	if (strncmp(fmt, "LS", 2) == 0) {
1701f575ce8SBruce Evans 		lsep = " ";
1711f575ce8SBruce Evans 		fmt += 2;
1721f575ce8SBruce Evans 	}
1731f575ce8SBruce Evans 	rsep = " ";
1741f575ce8SBruce Evans 	if (strncmp(fmt, "NRS", 3) == 0) {
1751f575ce8SBruce Evans 		rsep = "";
1761f575ce8SBruce Evans 		fmt += 3;
1771f575ce8SBruce Evans 	}
178c2dfd19fSGleb Smirnoff 	if (showvalue == 0) {
179c2dfd19fSGleb Smirnoff 		/* Print just dash. */
1801f575ce8SBruce Evans 		sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
181445f17bbSJosef Karthauser 		printf(newfmt, "-");
182c2dfd19fSGleb Smirnoff 		return;
183445f17bbSJosef Karthauser 	}
184445f17bbSJosef Karthauser 
185c2dfd19fSGleb Smirnoff 	if (hflag) {
186c2dfd19fSGleb Smirnoff 		char buf[5];
187445f17bbSJosef Karthauser 
188c2dfd19fSGleb Smirnoff 		/* Format in human readable form. */
189c2dfd19fSGleb Smirnoff 		humanize_number(buf, sizeof(buf), (int64_t)value, "",
190c2dfd19fSGleb Smirnoff 		    HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
1911f575ce8SBruce Evans 		sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
192c2dfd19fSGleb Smirnoff 		printf(newfmt, buf);
193c2dfd19fSGleb Smirnoff 	} else {
194c2dfd19fSGleb Smirnoff 		/* Construct the format string. */
1951f575ce8SBruce Evans 		sprintf(newfmt, "%s%%%d%s%s", lsep, width, fmt, rsep);
196c2dfd19fSGleb Smirnoff 		printf(newfmt, value);
197c2dfd19fSGleb Smirnoff 	}
198c2dfd19fSGleb Smirnoff }
199445f17bbSJosef Karthauser 
2009b50d902SRodney W. Grimes /*
2019b50d902SRodney W. Grimes  * Print a description of the network interfaces.
2029b50d902SRodney W. Grimes  */
2039b50d902SRodney W. Grimes void
204b6c86f4bSBruce Evans intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *))
2059b50d902SRodney W. Grimes {
2069b50d902SRodney W. Grimes 	struct ifnet ifnet;
2070e27dc05SGarrett Wollman 	struct ifnethead ifnethead;
2089b50d902SRodney W. Grimes 	union {
2099b50d902SRodney W. Grimes 		struct ifaddr ifa;
2109b50d902SRodney W. Grimes 		struct in_ifaddr in;
211cfa1ca9dSYoshinobu Inoue #ifdef INET6
212cfa1ca9dSYoshinobu Inoue 		struct in6_ifaddr in6;
213cfa1ca9dSYoshinobu Inoue #endif
214cc6a66f2SJulian Elischer 		struct ipx_ifaddr ipx;
2159b50d902SRodney W. Grimes 	} ifaddr;
2169b50d902SRodney W. Grimes 	u_long ifaddraddr;
217f6719675SBill Fenner 	u_long ifaddrfound;
2185da9f8faSJosef Karthauser 	u_long opackets;
2195da9f8faSJosef Karthauser 	u_long ipackets;
2205da9f8faSJosef Karthauser 	u_long obytes;
2215da9f8faSJosef Karthauser 	u_long ibytes;
222e1655201SRuslan Ermilov 	u_long omcasts;
223e1655201SRuslan Ermilov 	u_long imcasts;
2245da9f8faSJosef Karthauser 	u_long oerrors;
2255da9f8faSJosef Karthauser 	u_long ierrors;
226d72dc9a7SAttilio Rao 	u_long idrops;
2275da9f8faSJosef Karthauser 	u_long collisions;
2285da9f8faSJosef Karthauser 	int drops;
229a97a9922SJulian Elischer 	struct sockaddr *sa = NULL;
2309bf40edeSBrooks Davis 	char name[IFNAMSIZ];
231445f17bbSJosef Karthauser 	short network_layer;
232445f17bbSJosef Karthauser 	short link_layer;
2339b50d902SRodney W. Grimes 
2349b50d902SRodney W. Grimes 	if (ifnetaddr == 0) {
2359b50d902SRodney W. Grimes 		printf("ifnet: symbol not defined\n");
2369b50d902SRodney W. Grimes 		return;
2379b50d902SRodney W. Grimes 	}
238b6c86f4bSBruce Evans 	if (interval1) {
239b6c86f4bSBruce Evans 		sidewaysintpr(interval1, ifnetaddr);
2409b50d902SRodney W. Grimes 		return;
2419b50d902SRodney W. Grimes 	}
24265475bc8SDavid E. O'Brien 	if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead) != 0)
2439b50d902SRodney W. Grimes 		return;
2444d51ef63SPoul-Henning Kamp 	ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
24565475bc8SDavid E. O'Brien 	if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
2460e27dc05SGarrett Wollman 		return;
2470e27dc05SGarrett Wollman 
248cf5e44f8SRuslan Ermilov 	if (!pfunc) {
24925d295e1SBruce M Simpson 		if (Wflag)
25025d295e1SBruce M Simpson 			printf("%-7.7s", "Name");
25125d295e1SBruce M Simpson 		else
25225d295e1SBruce M Simpson 			printf("%-5.5s", "Name");
253d72dc9a7SAttilio Rao 		printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s %5.5s",
254d72dc9a7SAttilio Rao 		    "Mtu", "Network", "Address", "Ipkts", "Ierrs", "Idrop");
255e1e293a5SDavid Greenman 		if (bflag)
256e1e293a5SDavid Greenman 			printf(" %10.10s","Ibytes");
257e1e293a5SDavid Greenman 		printf(" %8.8s %5.5s", "Opkts", "Oerrs");
258e1e293a5SDavid Greenman 		if (bflag)
259e1e293a5SDavid Greenman 			printf(" %10.10s","Obytes");
2609b50d902SRodney W. Grimes 		printf(" %5s", "Coll");
2619b50d902SRodney W. Grimes 		if (dflag)
2629b50d902SRodney W. Grimes 			printf(" %s", "Drop");
2639b50d902SRodney W. Grimes 		putchar('\n');
264cfa1ca9dSYoshinobu Inoue 	}
2659b50d902SRodney W. Grimes 	ifaddraddr = 0;
2669b50d902SRodney W. Grimes 	while (ifnetaddr || ifaddraddr) {
2676cc6f122SPhilippe Charnier 		struct sockaddr_in *sockin;
268cfa1ca9dSYoshinobu Inoue #ifdef INET6
2696cc6f122SPhilippe Charnier 		struct sockaddr_in6 *sockin6;
270cfa1ca9dSYoshinobu Inoue #endif
271a01e3379SDavid Malone 		char *cp;
2729b50d902SRodney W. Grimes 		int n, m;
2739b50d902SRodney W. Grimes 
274445f17bbSJosef Karthauser 		network_layer = 0;
275445f17bbSJosef Karthauser 		link_layer = 0;
276445f17bbSJosef Karthauser 
2779b50d902SRodney W. Grimes 		if (ifaddraddr == 0) {
27865475bc8SDavid E. O'Brien 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
2799b50d902SRodney W. Grimes 				return;
2809bf40edeSBrooks Davis 			strlcpy(name, ifnet.if_xname, sizeof(name));
2814d51ef63SPoul-Henning Kamp 			ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
28265475bc8SDavid E. O'Brien 			if (interface != 0 && strcmp(name, interface) != 0)
2839b50d902SRodney W. Grimes 				continue;
284b3608ae1SEd Schouten 			cp = strchr(name, '\0');
285cfa1ca9dSYoshinobu Inoue 
286cfa1ca9dSYoshinobu Inoue 			if (pfunc) {
287cfa1ca9dSYoshinobu Inoue 				(*pfunc)(name);
288cfa1ca9dSYoshinobu Inoue 				continue;
289cfa1ca9dSYoshinobu Inoue 			}
290cfa1ca9dSYoshinobu Inoue 
2919b50d902SRodney W. Grimes 			if ((ifnet.if_flags&IFF_UP) == 0)
2929b50d902SRodney W. Grimes 				*cp++ = '*';
2939b50d902SRodney W. Grimes 			*cp = '\0';
2944d51ef63SPoul-Henning Kamp 			ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
2959b50d902SRodney W. Grimes 		}
296f6719675SBill Fenner 		ifaddrfound = ifaddraddr;
2975da9f8faSJosef Karthauser 
2985da9f8faSJosef Karthauser 		/*
2995da9f8faSJosef Karthauser 		 * Get the interface stats.  These may get
3005da9f8faSJosef Karthauser 		 * overriden below on a per-interface basis.
3015da9f8faSJosef Karthauser 		 */
3025da9f8faSJosef Karthauser 		opackets = ifnet.if_opackets;
3035da9f8faSJosef Karthauser 		ipackets = ifnet.if_ipackets;
3045da9f8faSJosef Karthauser 		obytes = ifnet.if_obytes;
3055da9f8faSJosef Karthauser 		ibytes = ifnet.if_ibytes;
306e1655201SRuslan Ermilov 		omcasts = ifnet.if_omcasts;
307e1655201SRuslan Ermilov 		imcasts = ifnet.if_imcasts;
3085da9f8faSJosef Karthauser 		oerrors = ifnet.if_oerrors;
3095da9f8faSJosef Karthauser 		ierrors = ifnet.if_ierrors;
310d72dc9a7SAttilio Rao 		idrops = ifnet.if_iqdrops;
3115da9f8faSJosef Karthauser 		collisions = ifnet.if_collisions;
3125da9f8faSJosef Karthauser 		drops = ifnet.if_snd.ifq_drops;
3135da9f8faSJosef Karthauser 
3149b50d902SRodney W. Grimes 		if (ifaddraddr == 0) {
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);
320acb60e59SRuslan Ermilov 			printf("%-13.13s ", "none");
321dd1f8b9bSMatthew N. Dodd 			printf("%-17.17s ", "none");
3229b50d902SRodney W. Grimes 		} else {
32365475bc8SDavid E. O'Brien 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)
32465475bc8SDavid E. O'Brien 			    != 0) {
3259b50d902SRodney W. Grimes 				ifaddraddr = 0;
3269b50d902SRodney W. Grimes 				continue;
3279b50d902SRodney W. Grimes 			}
3289b50d902SRodney W. Grimes #define	CP(x) ((char *)(x))
3299b50d902SRodney W. Grimes 			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
330a97a9922SJulian Elischer 				CP(&ifaddr);
331a97a9922SJulian Elischer 			sa = (struct sockaddr *)cp;
332d44ddba9SRuslan Ermilov 			if (af != AF_UNSPEC && sa->sa_family != af) {
333d44ddba9SRuslan Ermilov 				ifaddraddr =
334d44ddba9SRuslan Ermilov 				    (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
335d44ddba9SRuslan Ermilov 				continue;
336d44ddba9SRuslan Ermilov 			}
33725d295e1SBruce M Simpson 			if (Wflag)
33825d295e1SBruce M Simpson 				printf("%-7.7s", name);
33925d295e1SBruce M Simpson 			else
34025d295e1SBruce M Simpson 				printf("%-5.5s", name);
34125d295e1SBruce M Simpson 			printf(" %5lu ", ifnet.if_mtu);
3429b50d902SRodney W. Grimes 			switch (sa->sa_family) {
3439b50d902SRodney W. Grimes 			case AF_UNSPEC:
344cc6a66f2SJulian Elischer 				printf("%-13.13s ", "none");
3459b50d902SRodney W. Grimes 				printf("%-15.15s ", "none");
3469b50d902SRodney W. Grimes 				break;
3479b50d902SRodney W. Grimes 			case AF_INET:
3486cc6f122SPhilippe Charnier 				sockin = (struct sockaddr_in *)sa;
3499b50d902SRodney W. Grimes #ifdef notdef
3509b50d902SRodney W. Grimes 				/* can't use inet_makeaddr because kernel
3519b50d902SRodney W. Grimes 				 * keeps nets unshifted.
3529b50d902SRodney W. Grimes 				 */
3539b50d902SRodney W. Grimes 				in = inet_makeaddr(ifaddr.in.ia_subnet,
3549b50d902SRodney W. Grimes 					INADDR_ANY);
35509a67ffaSStefan Eßer 				printf("%-13.13s ", netname(in.s_addr,
3569b50d902SRodney W. Grimes 				    ifaddr.in.ia_subnetmask));
3579b50d902SRodney W. Grimes #else
358acb60e59SRuslan Ermilov 				printf("%-13.13s ",
3599b50d902SRodney W. Grimes 				    netname(htonl(ifaddr.in.ia_subnet),
3609b50d902SRodney W. Grimes 				    ifaddr.in.ia_subnetmask));
3619b50d902SRodney W. Grimes #endif
362dd1f8b9bSMatthew N. Dodd 				printf("%-17.17s ",
3636cc6f122SPhilippe Charnier 				    routename(sockin->sin_addr.s_addr));
364445f17bbSJosef Karthauser 
365445f17bbSJosef Karthauser 				network_layer = 1;
3669b50d902SRodney W. Grimes 				break;
367cfa1ca9dSYoshinobu Inoue #ifdef INET6
368cfa1ca9dSYoshinobu Inoue 			case AF_INET6:
3696cc6f122SPhilippe Charnier 				sockin6 = (struct sockaddr_in6 *)sa;
370cd05232aSHajimu UMEMOTO 				in6_fillscopeid(&ifaddr.in6.ia_addr);
371acb60e59SRuslan Ermilov 				printf("%-13.13s ",
372cfa1ca9dSYoshinobu Inoue 				       netname6(&ifaddr.in6.ia_addr,
373cfa1ca9dSYoshinobu Inoue 						&ifaddr.in6.ia_prefixmask.sin6_addr));
374cd05232aSHajimu UMEMOTO 				in6_fillscopeid(sockin6);
375cd05232aSHajimu UMEMOTO 				getnameinfo(sa, sa->sa_len, addr_buf,
376cd05232aSHajimu UMEMOTO 				    sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
377cd05232aSHajimu UMEMOTO 				printf("%-17.17s ", addr_buf);
378445f17bbSJosef Karthauser 
379445f17bbSJosef Karthauser 				network_layer = 1;
380cfa1ca9dSYoshinobu Inoue 				break;
381cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
382cc6a66f2SJulian Elischer 			case AF_IPX:
383cc6a66f2SJulian Elischer 				{
384cc6a66f2SJulian Elischer 				struct sockaddr_ipx *sipx =
385cc6a66f2SJulian Elischer 					(struct sockaddr_ipx *)sa;
386cc6a66f2SJulian Elischer 				u_long net;
387cc6a66f2SJulian Elischer 				char netnum[10];
388cc6a66f2SJulian Elischer 
389cc6a66f2SJulian Elischer 				*(union ipx_net *) &net = sipx->sipx_addr.x_net;
39022694ebaSBruce Evans 				sprintf(netnum, "%lx", (u_long)ntohl(net));
391cc6a66f2SJulian Elischer 				printf("ipx:%-8s  ", netnum);
392cc6a66f2SJulian Elischer /*				printf("ipx:%-8s ", netname(net, 0L)); */
393acb60e59SRuslan Ermilov 				printf("%-17s ",
394cc6a66f2SJulian Elischer 				    ipx_phost((struct sockaddr *)sipx));
395cc6a66f2SJulian Elischer 				}
3966f9cdfceSMatthew N. Dodd 
3976f9cdfceSMatthew N. Dodd 				network_layer = 1;
398cc6a66f2SJulian Elischer 				break;
3996ffcfd6cSJulian Elischer 
4006ffcfd6cSJulian Elischer 			case AF_APPLETALK:
401a8d37845SJulian Elischer 				printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
402acb60e59SRuslan Ermilov 				printf("%-11.11s  ",atalk_print(sa,0x0b) );
4036ffcfd6cSJulian Elischer 				break;
4049b50d902SRodney W. Grimes 			case AF_LINK:
4059b50d902SRodney W. Grimes 				{
4069b50d902SRodney W. Grimes 				struct sockaddr_dl *sdl =
4079b50d902SRodney W. Grimes 					(struct sockaddr_dl *)sa;
408cfa1ca9dSYoshinobu Inoue 				char linknum[10];
4099b50d902SRodney W. Grimes 				cp = (char *)LLADDR(sdl);
4109b50d902SRodney W. Grimes 				n = sdl->sdl_alen;
411cfa1ca9dSYoshinobu Inoue 				sprintf(linknum, "<Link#%d>", sdl->sdl_index);
412acb60e59SRuslan Ermilov 				m = printf("%-13.13s ", linknum);
4139b50d902SRodney W. Grimes 				}
4149b50d902SRodney W. Grimes 				goto hexprint;
4159b50d902SRodney W. Grimes 			default:
4169b50d902SRodney W. Grimes 				m = printf("(%d)", sa->sa_family);
4179b50d902SRodney W. Grimes 				for (cp = sa->sa_len + (char *)sa;
4189b50d902SRodney W. Grimes 					--cp > sa->sa_data && (*cp == 0);) {}
4199b50d902SRodney W. Grimes 				n = cp - sa->sa_data + 1;
4209b50d902SRodney W. Grimes 				cp = sa->sa_data;
4219b50d902SRodney W. Grimes 			hexprint:
42262372898SMichael Tuexen 				while ((--n >= 0) && (m < 30))
423541f2562SDavid Greenman 					m += printf("%02x%c", *cp++ & 0xff,
424e54ca68cSJordan K. Hubbard 						    n > 0 ? ':' : ' ');
425acb60e59SRuslan Ermilov 				m = 32 - m;
4269b50d902SRodney W. Grimes 				while (m-- > 0)
4279b50d902SRodney W. Grimes 					putchar(' ');
428445f17bbSJosef Karthauser 
429445f17bbSJosef Karthauser 				link_layer = 1;
4309b50d902SRodney W. Grimes 				break;
4319b50d902SRodney W. Grimes 			}
4325da9f8faSJosef Karthauser 
4335da9f8faSJosef Karthauser 			/*
4345da9f8faSJosef Karthauser 			 * Fixup the statistics for interfaces that
4355da9f8faSJosef Karthauser 			 * update stats for their network addresses
4365da9f8faSJosef Karthauser 			 */
437445f17bbSJosef Karthauser 			if (network_layer) {
4385da9f8faSJosef Karthauser 				opackets = ifaddr.in.ia_ifa.if_opackets;
4395da9f8faSJosef Karthauser 				ipackets = ifaddr.in.ia_ifa.if_ipackets;
4405da9f8faSJosef Karthauser 				obytes = ifaddr.in.ia_ifa.if_obytes;
4415da9f8faSJosef Karthauser 				ibytes = ifaddr.in.ia_ifa.if_ibytes;
4425da9f8faSJosef Karthauser 			}
4435da9f8faSJosef Karthauser 
4444d51ef63SPoul-Henning Kamp 			ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
4459b50d902SRodney W. Grimes 		}
4465da9f8faSJosef Karthauser 
447445f17bbSJosef Karthauser 		show_stat("lu", 8, ipackets, link_layer|network_layer);
448445f17bbSJosef Karthauser 		show_stat("lu", 5, ierrors, link_layer);
449d72dc9a7SAttilio Rao 		show_stat("lu", 5, idrops, link_layer);
4507c23a867SGleb Smirnoff 		if (bflag)
451445f17bbSJosef Karthauser 			show_stat("lu", 10, ibytes, link_layer|network_layer);
4527c23a867SGleb Smirnoff 
453445f17bbSJosef Karthauser 		show_stat("lu", 8, opackets, link_layer|network_layer);
454445f17bbSJosef Karthauser 		show_stat("lu", 5, oerrors, link_layer);
4557c23a867SGleb Smirnoff 		if (bflag)
456445f17bbSJosef Karthauser 			show_stat("lu", 10, obytes, link_layer|network_layer);
4577c23a867SGleb Smirnoff 
4581f575ce8SBruce Evans 		show_stat("NRSlu", 5, collisions, link_layer);
4597c23a867SGleb Smirnoff 		if (dflag)
4601f575ce8SBruce Evans 			show_stat("LSd", 4, drops, link_layer);
4619b50d902SRodney W. Grimes 		putchar('\n');
4621f575ce8SBruce Evans 
463f6719675SBill Fenner 		if (aflag && ifaddrfound) {
464f6719675SBill Fenner 			/*
465f6719675SBill Fenner 			 * Print family's multicast addresses
466f6719675SBill Fenner 			 */
4674d51ef63SPoul-Henning Kamp 			struct ifmultiaddr *multiaddr;
46875fb8770SGarrett Wollman 			struct ifmultiaddr ifma;
46975fb8770SGarrett Wollman 			union {
47075fb8770SGarrett Wollman 				struct sockaddr sa;
47175fb8770SGarrett Wollman 				struct sockaddr_in in;
472cfa1ca9dSYoshinobu Inoue #ifdef INET6
473cfa1ca9dSYoshinobu Inoue 				struct sockaddr_in6 in6;
474cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
47575fb8770SGarrett Wollman 				struct sockaddr_dl dl;
47675fb8770SGarrett Wollman 			} msa;
47775fb8770SGarrett Wollman 			const char *fmt;
478f6719675SBill Fenner 
4796817526dSPoul-Henning Kamp 			TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
4800b23654bSPoul-Henning Kamp 				if (kread((u_long)multiaddr, (char *)&ifma,
48165475bc8SDavid E. O'Brien 					  sizeof ifma) != 0)
482f6719675SBill Fenner 					break;
4830b23654bSPoul-Henning Kamp 				multiaddr = &ifma;
48475fb8770SGarrett Wollman 				if (kread((u_long)ifma.ifma_addr, (char *)&msa,
48565475bc8SDavid E. O'Brien 					  sizeof msa) != 0)
48675fb8770SGarrett Wollman 					break;
48775fb8770SGarrett Wollman 				if (msa.sa.sa_family != sa->sa_family)
48875fb8770SGarrett Wollman 					continue;
48975fb8770SGarrett Wollman 
49075fb8770SGarrett Wollman 				fmt = 0;
49175fb8770SGarrett Wollman 				switch (msa.sa.sa_family) {
49275fb8770SGarrett Wollman 				case AF_INET:
49375fb8770SGarrett Wollman 					fmt = routename(msa.in.sin_addr.s_addr);
49475fb8770SGarrett Wollman 					break;
495cfa1ca9dSYoshinobu Inoue #ifdef INET6
496cfa1ca9dSYoshinobu Inoue 				case AF_INET6:
497cd05232aSHajimu UMEMOTO 					in6_fillscopeid(&msa.in6);
498cd05232aSHajimu UMEMOTO 					getnameinfo(&msa.sa, msa.sa.sa_len,
499cd05232aSHajimu UMEMOTO 					    addr_buf, sizeof(addr_buf), 0, 0,
500cd05232aSHajimu UMEMOTO 					    NI_NUMERICHOST);
501bce2e7c8SRuslan Ermilov 					printf("%*s %-19.19s(refs: %d)\n",
502bce2e7c8SRuslan Ermilov 					       Wflag ? 27 : 25, "",
503cd05232aSHajimu UMEMOTO 					       addr_buf, ifma.ifma_refcount);
504b9d92bf5SBill Fenner 					break;
505cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
506f6719675SBill Fenner 				case AF_LINK:
507b9d92bf5SBill Fenner 					switch (msa.dl.sdl_type) {
508f6719675SBill Fenner 					case IFT_ETHER:
50975fb8770SGarrett Wollman 					case IFT_FDDI:
51075fb8770SGarrett Wollman 						fmt = ether_ntoa(
51175fb8770SGarrett Wollman 							(struct ether_addr *)
51275fb8770SGarrett Wollman 							LLADDR(&msa.dl));
51375fb8770SGarrett Wollman 						break;
514f6719675SBill Fenner 					}
515f6719675SBill Fenner 					break;
516f6719675SBill Fenner 				}
517e1655201SRuslan Ermilov 				if (fmt) {
518e1655201SRuslan Ermilov 					printf("%*s %-17.17s",
519bce2e7c8SRuslan Ermilov 					    Wflag ? 27 : 25, "", fmt);
520e1655201SRuslan Ermilov 					if (msa.sa.sa_family == AF_LINK) {
521e1655201SRuslan Ermilov 						printf(" %8lu", imcasts);
522e1655201SRuslan Ermilov 						printf("%*s",
523e1655201SRuslan Ermilov 						    bflag ? 17 : 6, "");
524e1655201SRuslan Ermilov 						printf(" %8lu", omcasts);
525e1655201SRuslan Ermilov 					}
526e1655201SRuslan Ermilov 					putchar('\n');
527e1655201SRuslan Ermilov 				}
528f6719675SBill Fenner 			}
529f6719675SBill Fenner 		}
5309b50d902SRodney W. Grimes 	}
5319b50d902SRodney W. Grimes }
5329b50d902SRodney W. Grimes 
5339b50d902SRodney W. Grimes struct	iftot {
5344d51ef63SPoul-Henning Kamp 	SLIST_ENTRY(iftot) chain;
5359bf40edeSBrooks Davis 	char	ift_name[IFNAMSIZ];	/* interface name */
536890bc2a5SDoug Rabson 	u_long	ift_ip;			/* input packets */
537890bc2a5SDoug Rabson 	u_long	ift_ie;			/* input errors */
538d72dc9a7SAttilio Rao 	u_long	ift_id;			/* input drops */
539890bc2a5SDoug Rabson 	u_long	ift_op;			/* output packets */
540890bc2a5SDoug Rabson 	u_long	ift_oe;			/* output errors */
541890bc2a5SDoug Rabson 	u_long	ift_co;			/* collisions */
542e1e293a5SDavid Greenman 	u_int	ift_dr;			/* drops */
543890bc2a5SDoug Rabson 	u_long	ift_ib;			/* input bytes */
544890bc2a5SDoug Rabson 	u_long	ift_ob;			/* output bytes */
545591c194aSGuido van Rooij };
5469b50d902SRodney W. Grimes 
5479b50d902SRodney W. Grimes u_char	signalled;			/* set if alarm goes off "early" */
5489b50d902SRodney W. Grimes 
5499b50d902SRodney W. Grimes /*
5509b50d902SRodney W. Grimes  * Print a running summary of interface statistics.
551b6c86f4bSBruce Evans  * Repeat display every interval1 seconds, showing statistics
552b6c86f4bSBruce Evans  * collected over that interval.  Assumes that interval1 is non-zero.
5539b50d902SRodney W. Grimes  * First line printed at top of screen is always cumulative.
5540e27dc05SGarrett Wollman  * XXX - should be rewritten to use ifmib(4).
5559b50d902SRodney W. Grimes  */
5569b50d902SRodney W. Grimes static void
557b6c86f4bSBruce Evans sidewaysintpr(int interval1, u_long off)
5589b50d902SRodney W. Grimes {
5599b50d902SRodney W. Grimes 	struct ifnet ifnet;
5609b50d902SRodney W. Grimes 	u_long firstifnet;
5610e27dc05SGarrett Wollman 	struct ifnethead ifnethead;
56293547b07SBruce Evans 	struct itimerval interval_it;
563591c194aSGuido van Rooij 	struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
564a01e3379SDavid Malone 	int line;
5650b87c915SDavid Greenman 	int oldmask, first;
5660b87c915SDavid Greenman 	u_long interesting_off;
5679b50d902SRodney W. Grimes 
56865475bc8SDavid E. O'Brien 	if (kread(off, (char *)&ifnethead, sizeof ifnethead) != 0)
5699b50d902SRodney W. Grimes 		return;
5704d51ef63SPoul-Henning Kamp 	firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
5710e27dc05SGarrett Wollman 
572591c194aSGuido van Rooij 	if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
573591c194aSGuido van Rooij 		printf("malloc failed\n");
574591c194aSGuido van Rooij 		exit(1);
575591c194aSGuido van Rooij 	}
576591c194aSGuido van Rooij 	memset(iftot, 0, sizeof(struct iftot));
577591c194aSGuido van Rooij 
5780b87c915SDavid Greenman 	interesting = NULL;
5790b87c915SDavid Greenman 	interesting_off = 0;
5809b50d902SRodney W. Grimes 	for (off = firstifnet, ip = iftot; off;) {
5819bf40edeSBrooks Davis 		char name[IFNAMSIZ];
5829b50d902SRodney W. Grimes 
58365475bc8SDavid E. O'Brien 		if (kread(off, (char *)&ifnet, sizeof ifnet) != 0)
5849b50d902SRodney W. Grimes 			break;
5859bf40edeSBrooks Davis 		strlcpy(name, ifnet.if_xname, sizeof(name));
5860b87c915SDavid Greenman 		if (interface && strcmp(name, interface) == 0) {
5879b50d902SRodney W. Grimes 			interesting = ip;
5880b87c915SDavid Greenman 			interesting_off = off;
5890b87c915SDavid Greenman 		}
5905eb2aa45SEd Maste 		snprintf(ip->ift_name, sizeof(ip->ift_name), "(%s)", name);
591591c194aSGuido van Rooij 		if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
592591c194aSGuido van Rooij 			printf("malloc failed\n");
593591c194aSGuido van Rooij 			exit(1);
594591c194aSGuido van Rooij 		}
595591c194aSGuido van Rooij 		memset(ipn, 0, sizeof(struct iftot));
5964d51ef63SPoul-Henning Kamp 		SLIST_NEXT(ip, chain) = ipn;
597591c194aSGuido van Rooij 		ip = ipn;
5984d51ef63SPoul-Henning Kamp 		off = (u_long)TAILQ_NEXT(&ifnet, if_link);
5999b50d902SRodney W. Grimes 	}
600c6358a5eSRuslan Ermilov 	if (interface && interesting == NULL)
601c6358a5eSRuslan Ermilov 		errx(1, "%s: unknown interface", interface);
602591c194aSGuido van Rooij 	if ((total = malloc(sizeof(struct iftot))) == NULL) {
603591c194aSGuido van Rooij 		printf("malloc failed\n");
604591c194aSGuido van Rooij 		exit(1);
605591c194aSGuido van Rooij 	}
606591c194aSGuido van Rooij 	memset(total, 0, sizeof(struct iftot));
607591c194aSGuido van Rooij 	if ((sum = malloc(sizeof(struct iftot))) == NULL) {
608591c194aSGuido van Rooij 		printf("malloc failed\n");
609591c194aSGuido van Rooij 		exit(1);
610591c194aSGuido van Rooij 	}
611591c194aSGuido van Rooij 	memset(sum, 0, sizeof(struct iftot));
612591c194aSGuido van Rooij 
6139b50d902SRodney W. Grimes 	(void)signal(SIGALRM, catchalarm);
6149b50d902SRodney W. Grimes 	signalled = NO;
61593547b07SBruce Evans 	interval_it.it_interval.tv_sec = interval1;
61693547b07SBruce Evans 	interval_it.it_interval.tv_usec = 0;
61793547b07SBruce Evans 	interval_it.it_value = interval_it.it_interval;
61893547b07SBruce Evans 	setitimer(ITIMER_REAL, &interval_it, NULL);
6190b87c915SDavid Greenman 	first = 1;
6200b87c915SDavid Greenman banner:
6210b87c915SDavid Greenman 	printf("%17s %14s %16s", "input",
6220b87c915SDavid Greenman 	    interesting ? interesting->ift_name : "(Total)", "output");
6239b50d902SRodney W. Grimes 	putchar('\n');
624d72dc9a7SAttilio Rao 	printf("%10s %5s %5s %10s %10s %5s %10s %5s",
625d72dc9a7SAttilio Rao 	    "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
626d72dc9a7SAttilio Rao 	    "colls");
6279b50d902SRodney W. Grimes 	if (dflag)
6289b50d902SRodney W. Grimes 		printf(" %5.5s", "drops");
6299b50d902SRodney W. Grimes 	putchar('\n');
6309b50d902SRodney W. Grimes 	fflush(stdout);
6319b50d902SRodney W. Grimes 	line = 0;
6329b50d902SRodney W. Grimes loop:
6330b87c915SDavid Greenman 	if (interesting != NULL) {
6340b87c915SDavid Greenman 		ip = interesting;
63565475bc8SDavid E. O'Brien 		if (kread(interesting_off, (char *)&ifnet, sizeof ifnet) != 0) {
6360b87c915SDavid Greenman 			printf("???\n");
6370b87c915SDavid Greenman 			exit(1);
6380b87c915SDavid Greenman 		};
6390b87c915SDavid Greenman 		if (!first) {
6407c23a867SGleb Smirnoff 			show_stat("lu", 10, ifnet.if_ipackets - ip->ift_ip, 1);
6417c23a867SGleb Smirnoff 			show_stat("lu", 5, ifnet.if_ierrors - ip->ift_ie, 1);
642d72dc9a7SAttilio Rao 			show_stat("lu", 5, ifnet.if_iqdrops - ip->ift_id, 1);
6437c23a867SGleb Smirnoff 			show_stat("lu", 10, ifnet.if_ibytes - ip->ift_ib, 1);
6447c23a867SGleb Smirnoff 			show_stat("lu", 10, ifnet.if_opackets - ip->ift_op, 1);
6457c23a867SGleb Smirnoff 			show_stat("lu", 5, ifnet.if_oerrors - ip->ift_oe, 1);
6467c23a867SGleb Smirnoff 			show_stat("lu", 10, ifnet.if_obytes - ip->ift_ob, 1);
6471f575ce8SBruce Evans 			show_stat("NRSlu", 5,
6481f575ce8SBruce Evans 			    ifnet.if_collisions - ip->ift_co, 1);
6490b87c915SDavid Greenman 			if (dflag)
6501f575ce8SBruce Evans 				show_stat("LSu", 5,
6517c23a867SGleb Smirnoff 				    ifnet.if_snd.ifq_drops - ip->ift_dr, 1);
6520b87c915SDavid Greenman 		}
6530b87c915SDavid Greenman 		ip->ift_ip = ifnet.if_ipackets;
6540b87c915SDavid Greenman 		ip->ift_ie = ifnet.if_ierrors;
6553c578b2aSRyan Stone 		ip->ift_id = ifnet.if_iqdrops;
6560b87c915SDavid Greenman 		ip->ift_ib = ifnet.if_ibytes;
6570b87c915SDavid Greenman 		ip->ift_op = ifnet.if_opackets;
6580b87c915SDavid Greenman 		ip->ift_oe = ifnet.if_oerrors;
6590b87c915SDavid Greenman 		ip->ift_ob = ifnet.if_obytes;
6600b87c915SDavid Greenman 		ip->ift_co = ifnet.if_collisions;
6610b87c915SDavid Greenman 		ip->ift_dr = ifnet.if_snd.ifq_drops;
6620b87c915SDavid Greenman 	} else {
6639b50d902SRodney W. Grimes 		sum->ift_ip = 0;
6649b50d902SRodney W. Grimes 		sum->ift_ie = 0;
665d72dc9a7SAttilio Rao 		sum->ift_id = 0;
6660b87c915SDavid Greenman 		sum->ift_ib = 0;
6679b50d902SRodney W. Grimes 		sum->ift_op = 0;
6689b50d902SRodney W. Grimes 		sum->ift_oe = 0;
6690b87c915SDavid Greenman 		sum->ift_ob = 0;
6709b50d902SRodney W. Grimes 		sum->ift_co = 0;
6719b50d902SRodney W. Grimes 		sum->ift_dr = 0;
6724d51ef63SPoul-Henning Kamp 		for (off = firstifnet, ip = iftot;
6734d51ef63SPoul-Henning Kamp 		     off && SLIST_NEXT(ip, chain) != NULL;
6744d51ef63SPoul-Henning Kamp 		     ip = SLIST_NEXT(ip, chain)) {
67565475bc8SDavid E. O'Brien 			if (kread(off, (char *)&ifnet, sizeof ifnet) != 0) {
6769b50d902SRodney W. Grimes 				off = 0;
6779b50d902SRodney W. Grimes 				continue;
6789b50d902SRodney W. Grimes 			}
6790b87c915SDavid Greenman 			sum->ift_ip += ifnet.if_ipackets;
6800b87c915SDavid Greenman 			sum->ift_ie += ifnet.if_ierrors;
681d72dc9a7SAttilio Rao 			sum->ift_id += ifnet.if_iqdrops;
6820b87c915SDavid Greenman 			sum->ift_ib += ifnet.if_ibytes;
6830b87c915SDavid Greenman 			sum->ift_op += ifnet.if_opackets;
6840b87c915SDavid Greenman 			sum->ift_oe += ifnet.if_oerrors;
6850b87c915SDavid Greenman 			sum->ift_ob += ifnet.if_obytes;
6860b87c915SDavid Greenman 			sum->ift_co += ifnet.if_collisions;
6870b87c915SDavid Greenman 			sum->ift_dr += ifnet.if_snd.ifq_drops;
6884d51ef63SPoul-Henning Kamp 			off = (u_long)TAILQ_NEXT(&ifnet, if_link);
6899b50d902SRodney W. Grimes 		}
6900b87c915SDavid Greenman 		if (!first) {
6917c23a867SGleb Smirnoff 			show_stat("lu", 10, sum->ift_ip - total->ift_ip, 1);
6927c23a867SGleb Smirnoff 			show_stat("lu", 5, sum->ift_ie - total->ift_ie, 1);
693d72dc9a7SAttilio Rao 			show_stat("lu", 5, sum->ift_id - total->ift_id, 1);
6947c23a867SGleb Smirnoff 			show_stat("lu", 10, sum->ift_ib - total->ift_ib, 1);
6957c23a867SGleb Smirnoff 			show_stat("lu", 10, sum->ift_op - total->ift_op, 1);
6967c23a867SGleb Smirnoff 			show_stat("lu", 5, sum->ift_oe - total->ift_oe, 1);
6977c23a867SGleb Smirnoff 			show_stat("lu", 10, sum->ift_ob - total->ift_ob, 1);
6981f575ce8SBruce Evans 			show_stat("NRSlu", 5, sum->ift_co - total->ift_co, 1);
6999b50d902SRodney W. Grimes 			if (dflag)
7001f575ce8SBruce Evans 				show_stat("LSu", 5,
7017c23a867SGleb Smirnoff 				    sum->ift_dr - total->ift_dr, 1);
7029b50d902SRodney W. Grimes 		}
7039b50d902SRodney W. Grimes 		*total = *sum;
7040b87c915SDavid Greenman 	}
7050b87c915SDavid Greenman 	if (!first)
7069b50d902SRodney W. Grimes 		putchar('\n');
7079b50d902SRodney W. Grimes 	fflush(stdout);
708bf10ffe1SXin LI 	if ((noutputs != 0) && (--noutputs == 0))
709bf10ffe1SXin LI 		exit(0);
7109b50d902SRodney W. Grimes 	oldmask = sigblock(sigmask(SIGALRM));
71193547b07SBruce Evans 	while (!signalled)
7129b50d902SRodney W. Grimes 		sigpause(0);
7139b50d902SRodney W. Grimes 	signalled = NO;
71493547b07SBruce Evans 	sigsetmask(oldmask);
7150b87c915SDavid Greenman 	line++;
7160b87c915SDavid Greenman 	first = 0;
7179b50d902SRodney W. Grimes 	if (line == 21)
7189b50d902SRodney W. Grimes 		goto banner;
7190b87c915SDavid Greenman 	else
7209b50d902SRodney W. Grimes 		goto loop;
7219b50d902SRodney W. Grimes 	/*NOTREACHED*/
7229b50d902SRodney W. Grimes }
7239b50d902SRodney W. Grimes 
7249b50d902SRodney W. Grimes /*
72593547b07SBruce Evans  * Set a flag to indicate that a signal from the periodic itimer has been
72693547b07SBruce Evans  * caught.
7279b50d902SRodney W. Grimes  */
7289b50d902SRodney W. Grimes static void
729f964d60dSAssar Westerlund catchalarm(int signo __unused)
7309b50d902SRodney W. Grimes {
7319b50d902SRodney W. Grimes 	signalled = YES;
7329b50d902SRodney W. Grimes }
733