xref: /freebsd/usr.bin/netstat/if.c (revision 55fb7d688bc6cac7ad5470c78af3693bc934c8e7)
165475bc8SDavid E. O'Brien /*-
2526b18aaSGleb Smirnoff  * Copyright (c) 2013 Gleb Smirnoff <glebius@FreeBSD.org>
39b50d902SRodney W. Grimes  * Copyright (c) 1983, 1988, 1993
49b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
59b50d902SRodney W. Grimes  *
69b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
79b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
89b50d902SRodney W. Grimes  * are met:
99b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
109b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
119b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
139b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
149b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
159b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
169b50d902SRodney W. Grimes  *    without specific prior written permission.
179b50d902SRodney W. Grimes  *
189b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
199b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
229b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
239b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
249b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
259b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
269b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
279b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
289b50d902SRodney W. Grimes  * SUCH DAMAGE.
299b50d902SRodney W. Grimes  */
309b50d902SRodney W. Grimes 
316cc6f122SPhilippe Charnier #if 0
329b50d902SRodney W. Grimes #ifndef lint
3305ddff6eSPeter Wemm static char sccsid[] = "@(#)if.c	8.3 (Berkeley) 4/28/95";
349b50d902SRodney W. Grimes #endif /* not lint */
356cc6f122SPhilippe Charnier #endif
366cc6f122SPhilippe Charnier 
376cc6f122SPhilippe Charnier #include <sys/cdefs.h>
386cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$");
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #include <sys/types.h>
419b50d902SRodney W. Grimes #include <sys/protosw.h>
429b50d902SRodney W. Grimes #include <sys/socket.h>
43feda1a43SJohn Baldwin #include <sys/socketvar.h>
440024d1dbSLuigi Rizzo #include <sys/sysctl.h>
45628d2ac1SGarrett Wollman #include <sys/time.h>
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <net/if.h>
4887669425SGarrett Wollman #include <net/if_var.h>
499b50d902SRodney W. Grimes #include <net/if_dl.h>
50f6719675SBill Fenner #include <net/if_types.h>
5175fb8770SGarrett Wollman #include <net/ethernet.h>
529b50d902SRodney W. Grimes #include <netinet/in.h>
539b50d902SRodney W. Grimes #include <netinet/in_var.h>
549b50d902SRodney W. Grimes #include <arpa/inet.h>
553e4d5cd3SGleb Smirnoff #ifdef PF
563e4d5cd3SGleb Smirnoff #include <net/pfvar.h>
573e4d5cd3SGleb Smirnoff #include <net/if_pfsync.h>
583e4d5cd3SGleb Smirnoff #endif
599b50d902SRodney W. Grimes 
602e37c5a3SMax Laier #include <err.h>
612e37c5a3SMax Laier #include <errno.h>
6284c1edcbSGleb Smirnoff #include <ifaddrs.h>
63c2dfd19fSGleb Smirnoff #include <libutil.h>
64cd05232aSHajimu UMEMOTO #ifdef INET6
65cd05232aSHajimu UMEMOTO #include <netdb.h>
66cd05232aSHajimu UMEMOTO #endif
679b50d902SRodney W. Grimes #include <signal.h>
6884c1edcbSGleb Smirnoff #include <stdbool.h>
697b95a1ebSYaroslav Tykhiy #include <stdint.h>
709b50d902SRodney W. Grimes #include <stdio.h>
71591c194aSGuido van Rooij #include <stdlib.h>
729b50d902SRodney W. Grimes #include <string.h>
7384c1edcbSGleb Smirnoff #include <sysexits.h>
74821df508SXin LI #include <unistd.h>
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes #include "netstat.h"
779b50d902SRodney W. Grimes 
7884c1edcbSGleb Smirnoff static void sidewaysintpr(int);
799b50d902SRodney W. Grimes 
80cfa1ca9dSYoshinobu Inoue #ifdef INET6
81cd05232aSHajimu UMEMOTO static char addr_buf[NI_MAXHOST];		/* for getnameinfo() */
82cfa1ca9dSYoshinobu Inoue #endif
83cfa1ca9dSYoshinobu Inoue 
843e4d5cd3SGleb Smirnoff #ifdef PF
85d6d3f01eSGleb Smirnoff static const char* pfsyncacts[] = {
86d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_CLR */		"clear all request",
87d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_INS */		"state insert",
88d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_INS_ACK */	"state inserted ack",
89d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_UPD */		"state update",
90d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_UPD_C */		"compressed state update",
91d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_UPD_REQ */	"uncompressed state request",
92d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_DEL */		"state delete",
93d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_DEL_C */		"compressed state delete",
94d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_INS_F */		"fragment insert",
95d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_DEL_F */		"fragment delete",
96d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_BUS */		"bulk update mark",
97d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_TDB */		"TDB replay counter update",
98d6d3f01eSGleb Smirnoff 	/* PFSYNC_ACT_EOF */		"end of frame mark",
99d6d3f01eSGleb Smirnoff };
100d6d3f01eSGleb Smirnoff 
101d6d3f01eSGleb Smirnoff static void
102d6d3f01eSGleb Smirnoff pfsync_acts_stats(const char *fmt, uint64_t *a)
103d6d3f01eSGleb Smirnoff {
104d6d3f01eSGleb Smirnoff 	int i;
105d6d3f01eSGleb Smirnoff 
106d6d3f01eSGleb Smirnoff 	for (i = 0; i < PFSYNC_ACT_MAX; i++, a++)
107d6d3f01eSGleb Smirnoff 		if (*a || sflag <= 1)
108d6d3f01eSGleb Smirnoff 			printf(fmt, *a, pfsyncacts[i], plural(*a));
109d6d3f01eSGleb Smirnoff }
110d6d3f01eSGleb Smirnoff 
1112e37c5a3SMax Laier /*
1122e37c5a3SMax Laier  * Dump pfsync statistics structure.
1132e37c5a3SMax Laier  */
1142e37c5a3SMax Laier void
115feda1a43SJohn Baldwin pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1162e37c5a3SMax Laier {
1172e37c5a3SMax Laier 	struct pfsyncstats pfsyncstat, zerostat;
1182e37c5a3SMax Laier 	size_t len = sizeof(struct pfsyncstats);
119445f17bbSJosef Karthauser 
120feda1a43SJohn Baldwin 	if (live) {
1212e37c5a3SMax Laier 		if (zflag)
1222e37c5a3SMax Laier 			memset(&zerostat, 0, len);
12316410af7SGleb Smirnoff 		if (sysctlbyname("net.pfsync.stats", &pfsyncstat, &len,
1242e37c5a3SMax Laier 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1252e37c5a3SMax Laier 			if (errno != ENOENT)
12616410af7SGleb Smirnoff 				warn("sysctl: net.pfsync.stats");
1272e37c5a3SMax Laier 			return;
1282e37c5a3SMax Laier 		}
129feda1a43SJohn Baldwin 	} else
130feda1a43SJohn Baldwin 		kread(off, &pfsyncstat, len);
131445f17bbSJosef Karthauser 
1322e37c5a3SMax Laier 	printf("%s:\n", name);
1332e37c5a3SMax Laier 
1342e37c5a3SMax Laier #define	p(f, m) if (pfsyncstat.f || sflag <= 1) \
1357b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f))
1362e37c5a3SMax Laier 
1377b95a1ebSYaroslav Tykhiy 	p(pfsyncs_ipackets, "\t%ju packet%s received (IPv4)\n");
1387b95a1ebSYaroslav Tykhiy 	p(pfsyncs_ipackets6, "\t%ju packet%s received (IPv6)\n");
139d6d3f01eSGleb Smirnoff 	pfsync_acts_stats("\t    %ju %s%s received\n",
140d6d3f01eSGleb Smirnoff 	    &pfsyncstat.pfsyncs_iacts[0]);
1417b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badif, "\t\t%ju packet%s discarded for bad interface\n");
1427b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badttl, "\t\t%ju packet%s discarded for bad ttl\n");
1437b95a1ebSYaroslav Tykhiy 	p(pfsyncs_hdrops, "\t\t%ju packet%s shorter than header\n");
1447b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badver, "\t\t%ju packet%s discarded for bad version\n");
1457b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badauth, "\t\t%ju packet%s discarded for bad HMAC\n");
1467b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badact,"\t\t%ju packet%s discarded for bad action\n");
1477b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badlen, "\t\t%ju packet%s discarded for short packet\n");
1487b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badval, "\t\t%ju state%s discarded for bad values\n");
1497b95a1ebSYaroslav Tykhiy 	p(pfsyncs_stale, "\t\t%ju stale state%s\n");
1507b95a1ebSYaroslav Tykhiy 	p(pfsyncs_badstate, "\t\t%ju failed state lookup/insert%s\n");
1517b95a1ebSYaroslav Tykhiy 	p(pfsyncs_opackets, "\t%ju packet%s sent (IPv4)\n");
1527b95a1ebSYaroslav Tykhiy 	p(pfsyncs_opackets6, "\t%ju packet%s sent (IPv6)\n");
153d6d3f01eSGleb Smirnoff 	pfsync_acts_stats("\t    %ju %s%s sent\n",
154d6d3f01eSGleb Smirnoff 	    &pfsyncstat.pfsyncs_oacts[0]);
155d6d3f01eSGleb Smirnoff 	p(pfsyncs_onomem, "\t\t%ju failure%s due to mbuf memory error\n");
156d6d3f01eSGleb Smirnoff 	p(pfsyncs_oerrors, "\t\t%ju send error%s\n");
1572e37c5a3SMax Laier #undef p
1582e37c5a3SMax Laier }
1593e4d5cd3SGleb Smirnoff #endif /* PF */
160445f17bbSJosef Karthauser 
161445f17bbSJosef Karthauser /*
162445f17bbSJosef Karthauser  * Display a formatted value, or a '-' in the same space.
163445f17bbSJosef Karthauser  */
1645e051718SAssar Westerlund static void
165f964d60dSAssar Westerlund show_stat(const char *fmt, int width, u_long value, short showvalue)
166445f17bbSJosef Karthauser {
1671f575ce8SBruce Evans 	const char *lsep, *rsep;
168445f17bbSJosef Karthauser 	char newfmt[32];
169445f17bbSJosef Karthauser 
1701f575ce8SBruce Evans 	lsep = "";
1711f575ce8SBruce Evans 	if (strncmp(fmt, "LS", 2) == 0) {
1721f575ce8SBruce Evans 		lsep = " ";
1731f575ce8SBruce Evans 		fmt += 2;
1741f575ce8SBruce Evans 	}
1751f575ce8SBruce Evans 	rsep = " ";
1761f575ce8SBruce Evans 	if (strncmp(fmt, "NRS", 3) == 0) {
1771f575ce8SBruce Evans 		rsep = "";
1781f575ce8SBruce Evans 		fmt += 3;
1791f575ce8SBruce Evans 	}
180c2dfd19fSGleb Smirnoff 	if (showvalue == 0) {
181c2dfd19fSGleb Smirnoff 		/* Print just dash. */
1821f575ce8SBruce Evans 		sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
183445f17bbSJosef Karthauser 		printf(newfmt, "-");
184c2dfd19fSGleb Smirnoff 		return;
185445f17bbSJosef Karthauser 	}
186445f17bbSJosef Karthauser 
187c2dfd19fSGleb Smirnoff 	if (hflag) {
188c2dfd19fSGleb Smirnoff 		char buf[5];
189445f17bbSJosef Karthauser 
190c2dfd19fSGleb Smirnoff 		/* Format in human readable form. */
191c2dfd19fSGleb Smirnoff 		humanize_number(buf, sizeof(buf), (int64_t)value, "",
192c2dfd19fSGleb Smirnoff 		    HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
1931f575ce8SBruce Evans 		sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
194c2dfd19fSGleb Smirnoff 		printf(newfmt, buf);
195c2dfd19fSGleb Smirnoff 	} else {
196c2dfd19fSGleb Smirnoff 		/* Construct the format string. */
1971f575ce8SBruce Evans 		sprintf(newfmt, "%s%%%d%s%s", lsep, width, fmt, rsep);
198c2dfd19fSGleb Smirnoff 		printf(newfmt, value);
199c2dfd19fSGleb Smirnoff 	}
200c2dfd19fSGleb Smirnoff }
201445f17bbSJosef Karthauser 
2029b50d902SRodney W. Grimes /*
20384c1edcbSGleb Smirnoff  * Find next multiaddr for a given interface name.
20484c1edcbSGleb Smirnoff  */
20584c1edcbSGleb Smirnoff static struct ifmaddrs *
20684c1edcbSGleb Smirnoff next_ifma(struct ifmaddrs *ifma, const char *name, const sa_family_t family)
20784c1edcbSGleb Smirnoff {
20884c1edcbSGleb Smirnoff 
20984c1edcbSGleb Smirnoff 	for(; ifma != NULL; ifma = ifma->ifma_next) {
21084c1edcbSGleb Smirnoff 		struct sockaddr_dl *sdl;
21184c1edcbSGleb Smirnoff 
21284c1edcbSGleb Smirnoff 		sdl = (struct sockaddr_dl *)ifma->ifma_name;
21384c1edcbSGleb Smirnoff 		if (ifma->ifma_addr->sa_family == family &&
21484c1edcbSGleb Smirnoff 		    strcmp(sdl->sdl_data, name) == 0)
21584c1edcbSGleb Smirnoff 			break;
21684c1edcbSGleb Smirnoff 	}
21784c1edcbSGleb Smirnoff 
21884c1edcbSGleb Smirnoff 	return (ifma);
21984c1edcbSGleb Smirnoff }
22084c1edcbSGleb Smirnoff 
22184c1edcbSGleb Smirnoff /*
2229b50d902SRodney W. Grimes  * Print a description of the network interfaces.
2239b50d902SRodney W. Grimes  */
2249b50d902SRodney W. Grimes void
225fc47e028SAlexander V. Chernikov intpr(int interval, void (*pfunc)(char *), int af)
2269b50d902SRodney W. Grimes {
22784c1edcbSGleb Smirnoff 	struct ifaddrs *ifap, *ifa;
22884c1edcbSGleb Smirnoff 	struct ifmaddrs *ifmap, *ifma;
2299b50d902SRodney W. Grimes 
23084c1edcbSGleb Smirnoff 	if (interval)
23184c1edcbSGleb Smirnoff 		return sidewaysintpr(interval);
23284c1edcbSGleb Smirnoff 
23384c1edcbSGleb Smirnoff 	if (getifaddrs(&ifap) != 0)
23484c1edcbSGleb Smirnoff 		err(EX_OSERR, "getifaddrs");
23584c1edcbSGleb Smirnoff 	if (aflag && getifmaddrs(&ifmap) != 0)
23684c1edcbSGleb Smirnoff 		err(EX_OSERR, "getifmaddrs");
2370e27dc05SGarrett Wollman 
238cf5e44f8SRuslan Ermilov 	if (!pfunc) {
23925d295e1SBruce M Simpson 		if (Wflag)
24025d295e1SBruce M Simpson 			printf("%-7.7s", "Name");
24125d295e1SBruce M Simpson 		else
24225d295e1SBruce M Simpson 			printf("%-5.5s", "Name");
243d72dc9a7SAttilio Rao 		printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s %5.5s",
244d72dc9a7SAttilio Rao 		    "Mtu", "Network", "Address", "Ipkts", "Ierrs", "Idrop");
245e1e293a5SDavid Greenman 		if (bflag)
246e1e293a5SDavid Greenman 			printf(" %10.10s","Ibytes");
247e1e293a5SDavid Greenman 		printf(" %8.8s %5.5s", "Opkts", "Oerrs");
248e1e293a5SDavid Greenman 		if (bflag)
249e1e293a5SDavid Greenman 			printf(" %10.10s","Obytes");
2509b50d902SRodney W. Grimes 		printf(" %5s", "Coll");
2519b50d902SRodney W. Grimes 		if (dflag)
2529b50d902SRodney W. Grimes 			printf("  %s", "Drop");
2539b50d902SRodney W. Grimes 		putchar('\n');
254cfa1ca9dSYoshinobu Inoue 	}
2559b50d902SRodney W. Grimes 
25684c1edcbSGleb Smirnoff 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
25784c1edcbSGleb Smirnoff 		bool network = false, link = false;
258445f17bbSJosef Karthauser 
25984c1edcbSGleb Smirnoff 		if (interface != NULL && strcmp(ifa->ifa_name, interface) != 0)
2609b50d902SRodney W. Grimes 			continue;
261cfa1ca9dSYoshinobu Inoue 
262cfa1ca9dSYoshinobu Inoue 		if (pfunc) {
26384c1edcbSGleb Smirnoff 			char *name;
264cfa1ca9dSYoshinobu Inoue 
26584c1edcbSGleb Smirnoff 			name = ifa->ifa_name;
26684c1edcbSGleb Smirnoff 			(*pfunc)(name);
2675da9f8faSJosef Karthauser 
2685da9f8faSJosef Karthauser 			/*
26984c1edcbSGleb Smirnoff 			 * Skip all ifaddrs belonging to same interface.
2705da9f8faSJosef Karthauser 			 */
27184c1edcbSGleb Smirnoff 			while(ifa->ifa_next != NULL &&
27284c1edcbSGleb Smirnoff 			    (strcmp(ifa->ifa_next->ifa_name, name) == 0)) {
27384c1edcbSGleb Smirnoff 				ifa = ifa->ifa_next;
27484c1edcbSGleb Smirnoff 			}
27584c1edcbSGleb Smirnoff 			continue;
27684c1edcbSGleb Smirnoff 		}
2775da9f8faSJosef Karthauser 
27884c1edcbSGleb Smirnoff 		if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
2799b50d902SRodney W. Grimes 			continue;
28084c1edcbSGleb Smirnoff 
28125d295e1SBruce M Simpson 		if (Wflag)
28284c1edcbSGleb Smirnoff 			printf("%-7.7s", ifa->ifa_name);
28325d295e1SBruce M Simpson 		else
28484c1edcbSGleb Smirnoff 			printf("%-5.5s", ifa->ifa_name);
28584c1edcbSGleb Smirnoff 
28684c1edcbSGleb Smirnoff #define IFA_MTU(ifa)	(((struct if_data *)(ifa)->ifa_data)->ifi_mtu)
28784c1edcbSGleb Smirnoff 		show_stat("lu", 6, IFA_MTU(ifa), IFA_MTU(ifa));
28884c1edcbSGleb Smirnoff #undef IFA_MTU
28984c1edcbSGleb Smirnoff 
29084c1edcbSGleb Smirnoff 		switch (ifa->ifa_addr->sa_family) {
2919b50d902SRodney W. Grimes 		case AF_UNSPEC:
292cc6a66f2SJulian Elischer 			printf("%-13.13s ", "none");
2939b50d902SRodney W. Grimes 			printf("%-15.15s ", "none");
2949b50d902SRodney W. Grimes 			break;
2959b50d902SRodney W. Grimes 		case AF_INET:
29684c1edcbSGleb Smirnoff 		    {
29784c1edcbSGleb Smirnoff 			struct sockaddr_in *sin, *mask;
298445f17bbSJosef Karthauser 
29984c1edcbSGleb Smirnoff 			sin = (struct sockaddr_in *)ifa->ifa_addr;
30084c1edcbSGleb Smirnoff 			mask = (struct sockaddr_in *)ifa->ifa_netmask;
30184c1edcbSGleb Smirnoff 			printf("%-13.13s ", netname(sin->sin_addr.s_addr,
30284c1edcbSGleb Smirnoff 			    mask->sin_addr.s_addr));
30384c1edcbSGleb Smirnoff 			printf("%-17.17s ",
30484c1edcbSGleb Smirnoff 			    routename(sin->sin_addr.s_addr));
30584c1edcbSGleb Smirnoff 
30684c1edcbSGleb Smirnoff 			network = true;
3079b50d902SRodney W. Grimes 			break;
30884c1edcbSGleb Smirnoff 		    }
309cfa1ca9dSYoshinobu Inoue #ifdef INET6
310cfa1ca9dSYoshinobu Inoue 		case AF_INET6:
31184c1edcbSGleb Smirnoff 		    {
31284c1edcbSGleb Smirnoff 			struct sockaddr_in6 *sin6, *mask;
31384c1edcbSGleb Smirnoff 
31484c1edcbSGleb Smirnoff 			sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
31584c1edcbSGleb Smirnoff 			mask = (struct sockaddr_in6 *)ifa->ifa_netmask;
31684c1edcbSGleb Smirnoff 
31784c1edcbSGleb Smirnoff 			printf("%-13.13s ", netname6(sin6, &mask->sin6_addr));
31884c1edcbSGleb Smirnoff 			getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len,
31984c1edcbSGleb Smirnoff 			    addr_buf, sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
320cd05232aSHajimu UMEMOTO 			printf("%-17.17s ", addr_buf);
321445f17bbSJosef Karthauser 
32284c1edcbSGleb Smirnoff 			network = 1;
323cfa1ca9dSYoshinobu Inoue 			break;
32484c1edcbSGleb Smirnoff 	            }
325cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
3269b50d902SRodney W. Grimes 		case AF_LINK:
3279b50d902SRodney W. Grimes 		    {
32884c1edcbSGleb Smirnoff 			struct sockaddr_dl *sdl;
32984c1edcbSGleb Smirnoff 			char *cp, linknum[10];
33084c1edcbSGleb Smirnoff 			int n, m;
33184c1edcbSGleb Smirnoff 
33284c1edcbSGleb Smirnoff 			sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3339b50d902SRodney W. Grimes 			cp = (char *)LLADDR(sdl);
3349b50d902SRodney W. Grimes 			n = sdl->sdl_alen;
335cfa1ca9dSYoshinobu Inoue 			sprintf(linknum, "<Link#%d>", sdl->sdl_index);
336acb60e59SRuslan Ermilov 			m = printf("%-13.13s ", linknum);
33784c1edcbSGleb Smirnoff 
33862372898SMichael Tuexen 			while ((--n >= 0) && (m < 30))
339541f2562SDavid Greenman 				m += printf("%02x%c", *cp++ & 0xff,
340e54ca68cSJordan K. Hubbard 					    n > 0 ? ':' : ' ');
341acb60e59SRuslan Ermilov 			m = 32 - m;
3429b50d902SRodney W. Grimes 			while (m-- > 0)
3439b50d902SRodney W. Grimes 				putchar(' ');
344445f17bbSJosef Karthauser 
34584c1edcbSGleb Smirnoff 			link = 1;
3469b50d902SRodney W. Grimes 			break;
3479b50d902SRodney W. Grimes 		    }
3485da9f8faSJosef Karthauser 		}
3495da9f8faSJosef Karthauser 
35084c1edcbSGleb Smirnoff #define	IFA_STAT(s)	(((struct if_data *)ifa->ifa_data)->ifi_ ## s)
35184c1edcbSGleb Smirnoff 		show_stat("lu", 8, IFA_STAT(ipackets), link|network);
35284c1edcbSGleb Smirnoff 		show_stat("lu", 5, IFA_STAT(ierrors), link);
35384c1edcbSGleb Smirnoff 		show_stat("lu", 5, IFA_STAT(iqdrops), link);
3547c23a867SGleb Smirnoff 		if (bflag)
35584c1edcbSGleb Smirnoff 			show_stat("lu", 10, IFA_STAT(ibytes), link|network);
35684c1edcbSGleb Smirnoff 		show_stat("lu", 8, IFA_STAT(opackets), link|network);
35784c1edcbSGleb Smirnoff 		show_stat("lu", 5, IFA_STAT(oerrors), link);
3587c23a867SGleb Smirnoff 		if (bflag)
35984c1edcbSGleb Smirnoff 			show_stat("lu", 10, IFA_STAT(obytes), link|network);
36084c1edcbSGleb Smirnoff 		show_stat("NRSlu", 5, IFA_STAT(collisions), link);
361*55fb7d68SGleb Smirnoff 		if (dflag)
362*55fb7d68SGleb Smirnoff 			show_stat("LSlu", 5, IFA_STAT(oqdrops), link);
3639b50d902SRodney W. Grimes 		putchar('\n');
3641f575ce8SBruce Evans 
36584c1edcbSGleb Smirnoff 		if (!aflag)
36675fb8770SGarrett Wollman 			continue;
36775fb8770SGarrett Wollman 
36884c1edcbSGleb Smirnoff 		/*
36984c1edcbSGleb Smirnoff 		 * Print family's multicast addresses.
37084c1edcbSGleb Smirnoff 		 */
37184c1edcbSGleb Smirnoff 		for (ifma = next_ifma(ifmap, ifa->ifa_name,
37284c1edcbSGleb Smirnoff 		     ifa->ifa_addr->sa_family);
37384c1edcbSGleb Smirnoff 		     ifma != NULL;
37484c1edcbSGleb Smirnoff 		     ifma = next_ifma(ifma, ifa->ifa_name,
37584c1edcbSGleb Smirnoff 		     ifa->ifa_addr->sa_family)) {
37684c1edcbSGleb Smirnoff 			const char *fmt = NULL;
37784c1edcbSGleb Smirnoff 
37884c1edcbSGleb Smirnoff 			switch (ifma->ifma_addr->sa_family) {
37975fb8770SGarrett Wollman 			case AF_INET:
38084c1edcbSGleb Smirnoff 			    {
38184c1edcbSGleb Smirnoff 				struct sockaddr_in *sin;
38284c1edcbSGleb Smirnoff 
38384c1edcbSGleb Smirnoff 				sin = (struct sockaddr_in *)ifma->ifma_addr;
38484c1edcbSGleb Smirnoff 				fmt = routename(sin->sin_addr.s_addr);
38575fb8770SGarrett Wollman 				break;
38684c1edcbSGleb Smirnoff 			    }
387cfa1ca9dSYoshinobu Inoue #ifdef INET6
388cfa1ca9dSYoshinobu Inoue 			case AF_INET6:
38984c1edcbSGleb Smirnoff 
39084c1edcbSGleb Smirnoff 				/* in6_fillscopeid(&msa.in6); */
39184c1edcbSGleb Smirnoff 				getnameinfo(ifma->ifma_addr,
39284c1edcbSGleb Smirnoff 				    ifma->ifma_addr->sa_len, addr_buf,
39384c1edcbSGleb Smirnoff 				    sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
39484c1edcbSGleb Smirnoff 				printf("%*s %s\n",
39584c1edcbSGleb Smirnoff 				    Wflag ? 27 : 25, "", addr_buf);
396b9d92bf5SBill Fenner 				break;
397cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
398f6719675SBill Fenner 			case AF_LINK:
39984c1edcbSGleb Smirnoff 			    {
40084c1edcbSGleb Smirnoff 				struct sockaddr_dl *sdl;
40184c1edcbSGleb Smirnoff 
40284c1edcbSGleb Smirnoff 				sdl = (struct sockaddr_dl *)ifma->ifma_addr;
40384c1edcbSGleb Smirnoff 				switch (sdl->sdl_type) {
404f6719675SBill Fenner 				case IFT_ETHER:
40575fb8770SGarrett Wollman 				case IFT_FDDI:
40675fb8770SGarrett Wollman 					fmt = ether_ntoa(
40784c1edcbSGleb Smirnoff 					    (struct ether_addr *)LLADDR(sdl));
40875fb8770SGarrett Wollman 					break;
409f6719675SBill Fenner 				}
410f6719675SBill Fenner 				break;
411f6719675SBill Fenner 			    }
4129b50d902SRodney W. Grimes 			}
4139b50d902SRodney W. Grimes 
41484c1edcbSGleb Smirnoff 			if (fmt) {
41584c1edcbSGleb Smirnoff 				printf("%*s %-17.17s",
41684c1edcbSGleb Smirnoff 				    Wflag ? 27 : 25, "", fmt);
41784c1edcbSGleb Smirnoff 				if (ifma->ifma_addr->sa_family == AF_LINK) {
418b245f96cSGleb Smirnoff 					printf(" %8ju",
419b245f96cSGleb Smirnoff 					    (uintmax_t )IFA_STAT(imcasts));
42084c1edcbSGleb Smirnoff 					printf("%*s", bflag ? 17 : 6, "");
421b245f96cSGleb Smirnoff 					printf(" %8ju",
422b245f96cSGleb Smirnoff 					    (uintmax_t )IFA_STAT(omcasts));
42384c1edcbSGleb Smirnoff 				}
42484c1edcbSGleb Smirnoff 				putchar('\n');
42584c1edcbSGleb Smirnoff 			}
42684c1edcbSGleb Smirnoff 
42784c1edcbSGleb Smirnoff 			ifma = ifma->ifma_next;
42884c1edcbSGleb Smirnoff 		}
42984c1edcbSGleb Smirnoff 	}
43084c1edcbSGleb Smirnoff 
43184c1edcbSGleb Smirnoff 	freeifaddrs(ifap);
43284c1edcbSGleb Smirnoff 	if (aflag)
43384c1edcbSGleb Smirnoff 		freeifmaddrs(ifmap);
43484c1edcbSGleb Smirnoff }
43584c1edcbSGleb Smirnoff 
4369b50d902SRodney W. Grimes struct iftot {
437890bc2a5SDoug Rabson 	u_long	ift_ip;			/* input packets */
438890bc2a5SDoug Rabson 	u_long	ift_ie;			/* input errors */
439d72dc9a7SAttilio Rao 	u_long	ift_id;			/* input drops */
440890bc2a5SDoug Rabson 	u_long	ift_op;			/* output packets */
441890bc2a5SDoug Rabson 	u_long	ift_oe;			/* output errors */
442*55fb7d68SGleb Smirnoff 	u_long	ift_od;			/* output drops */
443890bc2a5SDoug Rabson 	u_long	ift_co;			/* collisions */
444890bc2a5SDoug Rabson 	u_long	ift_ib;			/* input bytes */
445890bc2a5SDoug Rabson 	u_long	ift_ob;			/* output bytes */
446591c194aSGuido van Rooij };
4479b50d902SRodney W. Grimes 
44884c1edcbSGleb Smirnoff /*
44984c1edcbSGleb Smirnoff  * Obtain stats for interface(s).
45084c1edcbSGleb Smirnoff  */
45184c1edcbSGleb Smirnoff static void
45284c1edcbSGleb Smirnoff fill_iftot(struct iftot *st)
45384c1edcbSGleb Smirnoff {
45484c1edcbSGleb Smirnoff 	struct ifaddrs *ifap, *ifa;
45584c1edcbSGleb Smirnoff 	bool found = false;
45684c1edcbSGleb Smirnoff 
45784c1edcbSGleb Smirnoff 	if (getifaddrs(&ifap) != 0)
45884c1edcbSGleb Smirnoff 		err(EX_OSERR, "getifaddrs");
45984c1edcbSGleb Smirnoff 
46084c1edcbSGleb Smirnoff 	bzero(st, sizeof(*st));
46184c1edcbSGleb Smirnoff 
46284c1edcbSGleb Smirnoff 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
46384c1edcbSGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_LINK)
46484c1edcbSGleb Smirnoff 			continue;
46584c1edcbSGleb Smirnoff 		if (interface) {
46684c1edcbSGleb Smirnoff 			if (strcmp(ifa->ifa_name, interface) == 0)
46784c1edcbSGleb Smirnoff 				found = true;
46884c1edcbSGleb Smirnoff 			else
46984c1edcbSGleb Smirnoff 				continue;
47084c1edcbSGleb Smirnoff 		}
47184c1edcbSGleb Smirnoff 
47284c1edcbSGleb Smirnoff 		st->ift_ip += IFA_STAT(ipackets);
47384c1edcbSGleb Smirnoff 		st->ift_ie += IFA_STAT(ierrors);
47484c1edcbSGleb Smirnoff 		st->ift_id += IFA_STAT(iqdrops);
47584c1edcbSGleb Smirnoff 		st->ift_ib += IFA_STAT(ibytes);
47684c1edcbSGleb Smirnoff 		st->ift_op += IFA_STAT(opackets);
47784c1edcbSGleb Smirnoff 		st->ift_oe += IFA_STAT(oerrors);
478*55fb7d68SGleb Smirnoff 		st->ift_od += IFA_STAT(oqdrops);
47984c1edcbSGleb Smirnoff 		st->ift_ob += IFA_STAT(obytes);
48084c1edcbSGleb Smirnoff  		st->ift_co += IFA_STAT(collisions);
48184c1edcbSGleb Smirnoff 	}
48284c1edcbSGleb Smirnoff 
48384c1edcbSGleb Smirnoff 	if (interface && found == false)
48484c1edcbSGleb Smirnoff 		err(EX_DATAERR, "interface %s not found", interface);
48584c1edcbSGleb Smirnoff 
48684c1edcbSGleb Smirnoff 	freeifaddrs(ifap);
48784c1edcbSGleb Smirnoff }
48884c1edcbSGleb Smirnoff 
48984c1edcbSGleb Smirnoff /*
49084c1edcbSGleb Smirnoff  * Set a flag to indicate that a signal from the periodic itimer has been
49184c1edcbSGleb Smirnoff  * caught.
49284c1edcbSGleb Smirnoff  */
49384c1edcbSGleb Smirnoff static sig_atomic_t signalled;
49484c1edcbSGleb Smirnoff static void
49584c1edcbSGleb Smirnoff catchalarm(int signo __unused)
49684c1edcbSGleb Smirnoff {
49784c1edcbSGleb Smirnoff 	signalled = true;
49884c1edcbSGleb Smirnoff }
4999b50d902SRodney W. Grimes 
5009b50d902SRodney W. Grimes /*
5019b50d902SRodney W. Grimes  * Print a running summary of interface statistics.
50284c1edcbSGleb Smirnoff  * Repeat display every interval seconds, showing statistics
50384c1edcbSGleb Smirnoff  * collected over that interval.  Assumes that interval is non-zero.
5049b50d902SRodney W. Grimes  * First line printed at top of screen is always cumulative.
5059b50d902SRodney W. Grimes  */
5069b50d902SRodney W. Grimes static void
50784c1edcbSGleb Smirnoff sidewaysintpr(int interval)
5089b50d902SRodney W. Grimes {
50984c1edcbSGleb Smirnoff 	struct iftot ift[2], *new, *old;
51093547b07SBruce Evans 	struct itimerval interval_it;
51184c1edcbSGleb Smirnoff 	int oldmask, line;
5129b50d902SRodney W. Grimes 
51384c1edcbSGleb Smirnoff 	new = &ift[0];
51484c1edcbSGleb Smirnoff 	old = &ift[1];
51584c1edcbSGleb Smirnoff 	fill_iftot(old);
516591c194aSGuido van Rooij 
5179b50d902SRodney W. Grimes 	(void)signal(SIGALRM, catchalarm);
51884c1edcbSGleb Smirnoff 	signalled = false;
51984c1edcbSGleb Smirnoff 	interval_it.it_interval.tv_sec = interval;
52093547b07SBruce Evans 	interval_it.it_interval.tv_usec = 0;
52193547b07SBruce Evans 	interval_it.it_value = interval_it.it_interval;
52293547b07SBruce Evans 	setitimer(ITIMER_REAL, &interval_it, NULL);
52384c1edcbSGleb Smirnoff 
5240b87c915SDavid Greenman banner:
5250b87c915SDavid Greenman 	printf("%17s %14s %16s", "input",
52684c1edcbSGleb Smirnoff 	    interface != NULL ? interface : "(Total)", "output");
5279b50d902SRodney W. Grimes 	putchar('\n');
528d72dc9a7SAttilio Rao 	printf("%10s %5s %5s %10s %10s %5s %10s %5s",
529d72dc9a7SAttilio Rao 	    "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
530d72dc9a7SAttilio Rao 	    "colls");
5319b50d902SRodney W. Grimes 	if (dflag)
5329b50d902SRodney W. Grimes 		printf(" %5.5s", "drops");
5339b50d902SRodney W. Grimes 	putchar('\n');
5349b50d902SRodney W. Grimes 	fflush(stdout);
5359b50d902SRodney W. Grimes 	line = 0;
53684c1edcbSGleb Smirnoff 
5379b50d902SRodney W. Grimes loop:
538bf10ffe1SXin LI 	if ((noutputs != 0) && (--noutputs == 0))
539bf10ffe1SXin LI 		exit(0);
5409b50d902SRodney W. Grimes 	oldmask = sigblock(sigmask(SIGALRM));
54193547b07SBruce Evans 	while (!signalled)
5429b50d902SRodney W. Grimes 		sigpause(0);
54384c1edcbSGleb Smirnoff 	signalled = false;
54493547b07SBruce Evans 	sigsetmask(oldmask);
5450b87c915SDavid Greenman 	line++;
54684c1edcbSGleb Smirnoff 
54784c1edcbSGleb Smirnoff 	fill_iftot(new);
54884c1edcbSGleb Smirnoff 
54984c1edcbSGleb Smirnoff 	show_stat("lu", 10, new->ift_ip - old->ift_ip, 1);
55084c1edcbSGleb Smirnoff 	show_stat("lu", 5, new->ift_ie - old->ift_ie, 1);
55184c1edcbSGleb Smirnoff 	show_stat("lu", 5, new->ift_id - old->ift_id, 1);
55284c1edcbSGleb Smirnoff 	show_stat("lu", 10, new->ift_ib - old->ift_ib, 1);
55384c1edcbSGleb Smirnoff 	show_stat("lu", 10, new->ift_op - old->ift_op, 1);
55484c1edcbSGleb Smirnoff 	show_stat("lu", 5, new->ift_oe - old->ift_oe, 1);
55584c1edcbSGleb Smirnoff 	show_stat("lu", 10, new->ift_ob - old->ift_ob, 1);
55684c1edcbSGleb Smirnoff 	show_stat("NRSlu", 5, new->ift_co - old->ift_co, 1);
557*55fb7d68SGleb Smirnoff 	if (dflag)
558*55fb7d68SGleb Smirnoff 		show_stat("LSlu", 5, new->ift_od - old->ift_od, 1);
55984c1edcbSGleb Smirnoff 	putchar('\n');
56084c1edcbSGleb Smirnoff 	fflush(stdout);
56184c1edcbSGleb Smirnoff 
56284c1edcbSGleb Smirnoff 	if (new == &ift[0]) {
56384c1edcbSGleb Smirnoff 		new = &ift[1];
56484c1edcbSGleb Smirnoff 		old = &ift[0];
56584c1edcbSGleb Smirnoff 	} else {
56684c1edcbSGleb Smirnoff 		new = &ift[0];
56784c1edcbSGleb Smirnoff 		old = &ift[1];
56884c1edcbSGleb Smirnoff 	}
56984c1edcbSGleb Smirnoff 
5709b50d902SRodney W. Grimes 	if (line == 21)
5719b50d902SRodney W. Grimes 		goto banner;
5720b87c915SDavid Greenman 	else
5739b50d902SRodney W. Grimes 		goto loop;
5749b50d902SRodney W. Grimes 
57584c1edcbSGleb Smirnoff 	/* NOTREACHED */
5769b50d902SRodney W. Grimes }
577