xref: /freebsd/usr.bin/netstat/if.c (revision 628d2ac1b093f48f6fb98b17308acacc929876d8)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1983, 1988, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
3505ddff6eSPeter Wemm static char sccsid[] = "@(#)if.c	8.3 (Berkeley) 4/28/95";
369b50d902SRodney W. Grimes #endif /* not lint */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #include <sys/types.h>
399b50d902SRodney W. Grimes #include <sys/protosw.h>
409b50d902SRodney W. Grimes #include <sys/socket.h>
41628d2ac1SGarrett Wollman #include <sys/time.h>
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #include <net/if.h>
449b50d902SRodney W. Grimes #include <net/if_dl.h>
45f6719675SBill Fenner #include <net/if_types.h>
469b50d902SRodney W. Grimes #include <netinet/in.h>
479b50d902SRodney W. Grimes #include <netinet/in_var.h>
48f6719675SBill Fenner #define KERNEL 1
49f6719675SBill Fenner #include <netinet/if_ether.h>
50f6719675SBill Fenner #undef KERNEL
51cc6a66f2SJulian Elischer #include <netipx/ipx.h>
52cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
53cbc17e71SGarrett Wollman #ifdef NS
549b50d902SRodney W. Grimes #include <netns/ns.h>
559b50d902SRodney W. Grimes #include <netns/ns_if.h>
56cbc17e71SGarrett Wollman #endif
570761cb29SGarrett Wollman #ifdef ISO
589b50d902SRodney W. Grimes #include <netiso/iso.h>
599b50d902SRodney W. Grimes #include <netiso/iso_var.h>
600761cb29SGarrett Wollman #endif
619b50d902SRodney W. Grimes #include <arpa/inet.h>
629b50d902SRodney W. Grimes 
639b50d902SRodney W. Grimes #include <signal.h>
649b50d902SRodney W. Grimes #include <stdio.h>
659b50d902SRodney W. Grimes #include <string.h>
669b50d902SRodney W. Grimes #include <unistd.h>
679b50d902SRodney W. Grimes 
689b50d902SRodney W. Grimes #include "netstat.h"
699b50d902SRodney W. Grimes 
709b50d902SRodney W. Grimes #define	YES	1
719b50d902SRodney W. Grimes #define	NO	0
729b50d902SRodney W. Grimes 
739b50d902SRodney W. Grimes static void sidewaysintpr __P((u_int, u_long));
749b50d902SRodney W. Grimes static void catchalarm __P((int));
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes /*
779b50d902SRodney W. Grimes  * Print a description of the network interfaces.
789b50d902SRodney W. Grimes  */
799b50d902SRodney W. Grimes void
809b50d902SRodney W. Grimes intpr(interval, ifnetaddr)
819b50d902SRodney W. Grimes 	int interval;
829b50d902SRodney W. Grimes 	u_long ifnetaddr;
839b50d902SRodney W. Grimes {
849b50d902SRodney W. Grimes 	struct ifnet ifnet;
859b50d902SRodney W. Grimes 	union {
869b50d902SRodney W. Grimes 		struct ifaddr ifa;
879b50d902SRodney W. Grimes 		struct in_ifaddr in;
88cc6a66f2SJulian Elischer 		struct ipx_ifaddr ipx;
89cbc17e71SGarrett Wollman #ifdef NS
909b50d902SRodney W. Grimes 		struct ns_ifaddr ns;
91cbc17e71SGarrett Wollman #endif
920761cb29SGarrett Wollman #ifdef ISO
939b50d902SRodney W. Grimes 		struct iso_ifaddr iso;
940761cb29SGarrett Wollman #endif
959b50d902SRodney W. Grimes 	} ifaddr;
969b50d902SRodney W. Grimes 	u_long ifaddraddr;
97f6719675SBill Fenner 	u_long ifaddrfound;
98f6719675SBill Fenner 	u_long ifnetfound;
999b50d902SRodney W. Grimes 	struct sockaddr *sa;
1001b72e71cSDavid Greenman 	char name[32], tname[16];
1019b50d902SRodney W. Grimes 
1029b50d902SRodney W. Grimes 	if (ifnetaddr == 0) {
1039b50d902SRodney W. Grimes 		printf("ifnet: symbol not defined\n");
1049b50d902SRodney W. Grimes 		return;
1059b50d902SRodney W. Grimes 	}
1069b50d902SRodney W. Grimes 	if (interval) {
1079b50d902SRodney W. Grimes 		sidewaysintpr((unsigned)interval, ifnetaddr);
1089b50d902SRodney W. Grimes 		return;
1099b50d902SRodney W. Grimes 	}
1109b50d902SRodney W. Grimes 	if (kread(ifnetaddr, (char *)&ifnetaddr, sizeof ifnetaddr))
1119b50d902SRodney W. Grimes 		return;
112cc6a66f2SJulian Elischer 	printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
113e1e293a5SDavid Greenman 		"Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
114e1e293a5SDavid Greenman 	if (bflag)
115e1e293a5SDavid Greenman 		printf(" %10.10s","Ibytes");
116e1e293a5SDavid Greenman 	printf(" %8.8s %5.5s", "Opkts", "Oerrs");
117e1e293a5SDavid Greenman 	if (bflag)
118e1e293a5SDavid Greenman 		printf(" %10.10s","Obytes");
1199b50d902SRodney W. Grimes 	printf(" %5s", "Coll");
1209b50d902SRodney W. Grimes 	if (tflag)
1219b50d902SRodney W. Grimes 		printf(" %s", "Time");
1229b50d902SRodney W. Grimes 	if (dflag)
1239b50d902SRodney W. Grimes 		printf(" %s", "Drop");
1249b50d902SRodney W. Grimes 	putchar('\n');
1259b50d902SRodney W. Grimes 	ifaddraddr = 0;
1269b50d902SRodney W. Grimes 	while (ifnetaddr || ifaddraddr) {
1279b50d902SRodney W. Grimes 		struct sockaddr_in *sin;
1289b50d902SRodney W. Grimes 		register char *cp;
1299b50d902SRodney W. Grimes 		int n, m;
1309b50d902SRodney W. Grimes 
1319b50d902SRodney W. Grimes 		if (ifaddraddr == 0) {
132f6719675SBill Fenner 			ifnetfound = ifnetaddr;
1339b50d902SRodney W. Grimes 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) ||
1341b72e71cSDavid Greenman 			    kread((u_long)ifnet.if_name, tname, 16))
1359b50d902SRodney W. Grimes 				return;
1361b72e71cSDavid Greenman 			tname[15] = '\0';
1379b50d902SRodney W. Grimes 			ifnetaddr = (u_long)ifnet.if_next;
1381b72e71cSDavid Greenman 			snprintf(name, 32, "%s%d", tname, ifnet.if_unit);
1391b72e71cSDavid Greenman 			if (interface != 0 && (strcmp(name, interface) != 0))
1409b50d902SRodney W. Grimes 				continue;
1419b50d902SRodney W. Grimes 			cp = index(name, '\0');
1429b50d902SRodney W. Grimes 			if ((ifnet.if_flags&IFF_UP) == 0)
1439b50d902SRodney W. Grimes 				*cp++ = '*';
1449b50d902SRodney W. Grimes 			*cp = '\0';
1459b50d902SRodney W. Grimes 			ifaddraddr = (u_long)ifnet.if_addrlist;
1469b50d902SRodney W. Grimes 		}
1477d56c0eeSAlexander Langer 		printf("%-5.5s %-5lu ", name, ifnet.if_mtu);
148f6719675SBill Fenner 		ifaddrfound = ifaddraddr;
1499b50d902SRodney W. Grimes 		if (ifaddraddr == 0) {
150cc6a66f2SJulian Elischer 			printf("%-13.13s ", "none");
1519b50d902SRodney W. Grimes 			printf("%-15.15s ", "none");
1529b50d902SRodney W. Grimes 		} else {
1539b50d902SRodney W. Grimes 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
1549b50d902SRodney W. Grimes 				ifaddraddr = 0;
1559b50d902SRodney W. Grimes 				continue;
1569b50d902SRodney W. Grimes 			}
1579b50d902SRodney W. Grimes #define CP(x) ((char *)(x))
1589b50d902SRodney W. Grimes 			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
1599b50d902SRodney W. Grimes 				CP(&ifaddr); sa = (struct sockaddr *)cp;
1609b50d902SRodney W. Grimes 			switch (sa->sa_family) {
1619b50d902SRodney W. Grimes 			case AF_UNSPEC:
162cc6a66f2SJulian Elischer 				printf("%-13.13s ", "none");
1639b50d902SRodney W. Grimes 				printf("%-15.15s ", "none");
1649b50d902SRodney W. Grimes 				break;
1659b50d902SRodney W. Grimes 			case AF_INET:
1669b50d902SRodney W. Grimes 				sin = (struct sockaddr_in *)sa;
1679b50d902SRodney W. Grimes #ifdef notdef
1689b50d902SRodney W. Grimes 				/* can't use inet_makeaddr because kernel
1699b50d902SRodney W. Grimes 				 * keeps nets unshifted.
1709b50d902SRodney W. Grimes 				 */
1719b50d902SRodney W. Grimes 				in = inet_makeaddr(ifaddr.in.ia_subnet,
1729b50d902SRodney W. Grimes 					INADDR_ANY);
17309a67ffaSStefan Eßer 				printf("%-13.13s ", netname(in.s_addr,
1749b50d902SRodney W. Grimes 				    ifaddr.in.ia_subnetmask));
1759b50d902SRodney W. Grimes #else
17609a67ffaSStefan Eßer 				printf("%-13.13s ",
1779b50d902SRodney W. Grimes 				    netname(htonl(ifaddr.in.ia_subnet),
1789b50d902SRodney W. Grimes 				    ifaddr.in.ia_subnetmask));
1799b50d902SRodney W. Grimes #endif
1809b50d902SRodney W. Grimes 				printf("%-15.15s ",
1819b50d902SRodney W. Grimes 				    routename(sin->sin_addr.s_addr));
1829b50d902SRodney W. Grimes 				break;
183cc6a66f2SJulian Elischer 			case AF_IPX:
184cc6a66f2SJulian Elischer 				{
185cc6a66f2SJulian Elischer 				struct sockaddr_ipx *sipx =
186cc6a66f2SJulian Elischer 					(struct sockaddr_ipx *)sa;
187cc6a66f2SJulian Elischer 				u_long net;
188cc6a66f2SJulian Elischer 				char netnum[10];
189cc6a66f2SJulian Elischer 
190cc6a66f2SJulian Elischer 				*(union ipx_net *) &net = sipx->sipx_addr.x_net;
191cc6a66f2SJulian Elischer 				sprintf(netnum, "%lx", ntohl(net));
192cc6a66f2SJulian Elischer 				printf("ipx:%-8s ", netnum);
193cc6a66f2SJulian Elischer /*				printf("ipx:%-8s ", netname(net, 0L)); */
194cc6a66f2SJulian Elischer 				printf("%-15s ",
195cc6a66f2SJulian Elischer 				    ipx_phost((struct sockaddr *)sipx));
196cc6a66f2SJulian Elischer 				}
197cc6a66f2SJulian Elischer 				break;
1986ffcfd6cSJulian Elischer 
1996ffcfd6cSJulian Elischer 			case AF_APPLETALK:
200a8d37845SJulian Elischer 				printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
201a8d37845SJulian Elischer 				printf("%-9.9s  ",atalk_print(sa,0x0b) );
2026ffcfd6cSJulian Elischer 				break;
203cbc17e71SGarrett Wollman #ifdef NS
2049b50d902SRodney W. Grimes 			case AF_NS:
2059b50d902SRodney W. Grimes 				{
2069b50d902SRodney W. Grimes 				struct sockaddr_ns *sns =
2079b50d902SRodney W. Grimes 					(struct sockaddr_ns *)sa;
2089b50d902SRodney W. Grimes 				u_long net;
209cc6a66f2SJulian Elischer 				char netnum[10];
2109b50d902SRodney W. Grimes 
2119b50d902SRodney W. Grimes 				*(union ns_net *) &net = sns->sns_addr.x_net;
2129b50d902SRodney W. Grimes 				sprintf(netnum, "%lxH", ntohl(net));
2139b50d902SRodney W. Grimes 				upHex(netnum);
2149b50d902SRodney W. Grimes 				printf("ns:%-8s ", netnum);
2159b50d902SRodney W. Grimes 				printf("%-15s ",
2169b50d902SRodney W. Grimes 				    ns_phost((struct sockaddr *)sns));
2179b50d902SRodney W. Grimes 				}
2189b50d902SRodney W. Grimes 				break;
219cbc17e71SGarrett Wollman #endif
2209b50d902SRodney W. Grimes 			case AF_LINK:
2219b50d902SRodney W. Grimes 				{
2229b50d902SRodney W. Grimes 				struct sockaddr_dl *sdl =
2239b50d902SRodney W. Grimes 					(struct sockaddr_dl *)sa;
2249b50d902SRodney W. Grimes 				    cp = (char *)LLADDR(sdl);
2259b50d902SRodney W. Grimes 				    n = sdl->sdl_alen;
2269b50d902SRodney W. Grimes 				}
22705ddff6eSPeter Wemm 				m = printf("%-11.11s ", "<Link>");
2289b50d902SRodney W. Grimes 				goto hexprint;
2299b50d902SRodney W. Grimes 			default:
2309b50d902SRodney W. Grimes 				m = printf("(%d)", sa->sa_family);
2319b50d902SRodney W. Grimes 				for (cp = sa->sa_len + (char *)sa;
2329b50d902SRodney W. Grimes 					--cp > sa->sa_data && (*cp == 0);) {}
2339b50d902SRodney W. Grimes 				n = cp - sa->sa_data + 1;
2349b50d902SRodney W. Grimes 				cp = sa->sa_data;
2359b50d902SRodney W. Grimes 			hexprint:
2369b50d902SRodney W. Grimes 				while (--n >= 0)
237541f2562SDavid Greenman 					m += printf("%02x%c", *cp++ & 0xff,
2389b50d902SRodney W. Grimes 						    n > 0 ? '.' : ' ');
239cc6a66f2SJulian Elischer 				m = 30 - m;
2409b50d902SRodney W. Grimes 				while (m-- > 0)
2419b50d902SRodney W. Grimes 					putchar(' ');
2429b50d902SRodney W. Grimes 				break;
2439b50d902SRodney W. Grimes 			}
2449b50d902SRodney W. Grimes 			ifaddraddr = (u_long)ifaddr.ifa.ifa_next;
2459b50d902SRodney W. Grimes 		}
2467d56c0eeSAlexander Langer 		printf("%8lu %5lu ",
247e1e293a5SDavid Greenman 		    ifnet.if_ipackets, ifnet.if_ierrors);
248e1e293a5SDavid Greenman 		if (bflag)
2497d56c0eeSAlexander Langer 			printf("%10lu ", ifnet.if_ibytes);
2507d56c0eeSAlexander Langer 		printf("%8lu %5lu ",
251e1e293a5SDavid Greenman 		    ifnet.if_opackets, ifnet.if_oerrors);
252e1e293a5SDavid Greenman 		if (bflag)
2537d56c0eeSAlexander Langer 			printf("%10lu ", ifnet.if_obytes);
2547d56c0eeSAlexander Langer 		printf("%5lu", ifnet.if_collisions);
2559b50d902SRodney W. Grimes 		if (tflag)
2569b50d902SRodney W. Grimes 			printf(" %3d", ifnet.if_timer);
2579b50d902SRodney W. Grimes 		if (dflag)
2589b50d902SRodney W. Grimes 			printf(" %3d", ifnet.if_snd.ifq_drops);
2599b50d902SRodney W. Grimes 		putchar('\n');
260f6719675SBill Fenner 		if (aflag && ifaddrfound) {
261f6719675SBill Fenner 		    /*
262f6719675SBill Fenner 		     * Print family's multicast addresses
263f6719675SBill Fenner 		     */
264f6719675SBill Fenner 		    switch (sa->sa_family) {
265f6719675SBill Fenner 		    case AF_INET:
266f6719675SBill Fenner 			{
267f6719675SBill Fenner 			    u_long multiaddr;
268f6719675SBill Fenner 			    struct in_multi inm;
269f6719675SBill Fenner 
270f6719675SBill Fenner 			    multiaddr = (u_long)ifaddr.in.ia_multiaddrs.lh_first;
271f6719675SBill Fenner 			    while (multiaddr != 0) {
272f6719675SBill Fenner 				    kread(multiaddr, (char *)&inm,
273f6719675SBill Fenner 							sizeof inm);
274f6719675SBill Fenner 				    multiaddr = (u_long)inm.inm_entry.le_next;
275f6719675SBill Fenner 				    printf("%23s %s\n", "",
276f6719675SBill Fenner 					    routename(inm.inm_addr.s_addr));
277f6719675SBill Fenner 			    }
278f6719675SBill Fenner 			    break;
279f6719675SBill Fenner 			}
280f6719675SBill Fenner 		    case AF_LINK:
281f6719675SBill Fenner 			    switch (ifnet.if_type) {
282f6719675SBill Fenner 			    case IFT_ETHER:
283f6719675SBill Fenner 			    case IFT_FDDI:	/*XXX*/
284f6719675SBill Fenner 				{
285f6719675SBill Fenner 				    off_t multiaddr;
286f6719675SBill Fenner 				    struct arpcom ac;
287f6719675SBill Fenner 				    struct ether_multi enm;
288f6719675SBill Fenner 
289f6719675SBill Fenner 				    kread(ifnetfound, (char *)&ac, sizeof ac);
290f6719675SBill Fenner 				    multiaddr = (u_long)ac.ac_multiaddrs;
291f6719675SBill Fenner 				    while (multiaddr != 0) {
292f6719675SBill Fenner 					    kread(multiaddr, (char *)&enm,
293f6719675SBill Fenner 						    sizeof enm);
294f6719675SBill Fenner 					    multiaddr = (u_long)enm.enm_next;
295f6719675SBill Fenner 					    printf("%23s %s", "",
296f6719675SBill Fenner 						ether_ntoa(&enm.enm_addrlo));
297f6719675SBill Fenner 					    if (bcmp(&enm.enm_addrlo,
298f6719675SBill Fenner 						     &enm.enm_addrhi, 6) != 0)
299f6719675SBill Fenner 						printf(" to %s",
300f6719675SBill Fenner 						    ether_ntoa(&enm.enm_addrhi));
301f6719675SBill Fenner 					    printf("\n");
302f6719675SBill Fenner 				    }
303f6719675SBill Fenner 				    break;
304f6719675SBill Fenner 				}
305f6719675SBill Fenner 			    default:
306f6719675SBill Fenner 				    break;
307f6719675SBill Fenner 			    }
308f6719675SBill Fenner 		    default:
309f6719675SBill Fenner 			    break;
310f6719675SBill Fenner 		    }
311f6719675SBill Fenner 		}
3129b50d902SRodney W. Grimes 	}
3139b50d902SRodney W. Grimes }
3149b50d902SRodney W. Grimes 
3159b50d902SRodney W. Grimes #define	MAXIF	10
3169b50d902SRodney W. Grimes struct	iftot {
3179b50d902SRodney W. Grimes 	char	ift_name[16];		/* interface name */
318e1e293a5SDavid Greenman 	u_int	ift_ip;			/* input packets */
319e1e293a5SDavid Greenman 	u_int	ift_ie;			/* input errors */
320e1e293a5SDavid Greenman 	u_int	ift_op;			/* output packets */
321e1e293a5SDavid Greenman 	u_int	ift_oe;			/* output errors */
322e1e293a5SDavid Greenman 	u_int	ift_co;			/* collisions */
323e1e293a5SDavid Greenman 	u_int	ift_dr;			/* drops */
324e1e293a5SDavid Greenman 	u_int	ift_ib;			/* input bytes */
325e1e293a5SDavid Greenman 	u_int	ift_ob;			/* output bytes */
3269b50d902SRodney W. Grimes } iftot[MAXIF];
3279b50d902SRodney W. Grimes 
3289b50d902SRodney W. Grimes u_char	signalled;			/* set if alarm goes off "early" */
3299b50d902SRodney W. Grimes 
3309b50d902SRodney W. Grimes /*
3319b50d902SRodney W. Grimes  * Print a running summary of interface statistics.
3329b50d902SRodney W. Grimes  * Repeat display every interval seconds, showing statistics
3339b50d902SRodney W. Grimes  * collected over that interval.  Assumes that interval is non-zero.
3349b50d902SRodney W. Grimes  * First line printed at top of screen is always cumulative.
3359b50d902SRodney W. Grimes  */
3369b50d902SRodney W. Grimes static void
3379b50d902SRodney W. Grimes sidewaysintpr(interval, off)
3389b50d902SRodney W. Grimes 	unsigned interval;
3399b50d902SRodney W. Grimes 	u_long off;
3409b50d902SRodney W. Grimes {
3419b50d902SRodney W. Grimes 	struct ifnet ifnet;
3429b50d902SRodney W. Grimes 	u_long firstifnet;
3439b50d902SRodney W. Grimes 	register struct iftot *ip, *total;
3449b50d902SRodney W. Grimes 	register int line;
3459b50d902SRodney W. Grimes 	struct iftot *lastif, *sum, *interesting;
3460b87c915SDavid Greenman 	int oldmask, first;
3470b87c915SDavid Greenman 	u_long interesting_off;
3489b50d902SRodney W. Grimes 
3499b50d902SRodney W. Grimes 	if (kread(off, (char *)&firstifnet, sizeof (u_long)))
3509b50d902SRodney W. Grimes 		return;
3519b50d902SRodney W. Grimes 	lastif = iftot;
3529b50d902SRodney W. Grimes 	sum = iftot + MAXIF - 1;
3539b50d902SRodney W. Grimes 	total = sum - 1;
3540b87c915SDavid Greenman 	interesting = NULL;
3550b87c915SDavid Greenman 	interesting_off = 0;
3569b50d902SRodney W. Grimes 	for (off = firstifnet, ip = iftot; off;) {
3571b72e71cSDavid Greenman 		char name[16], tname[16];
3589b50d902SRodney W. Grimes 
3599b50d902SRodney W. Grimes 		if (kread(off, (char *)&ifnet, sizeof ifnet))
3609b50d902SRodney W. Grimes 			break;
3611b72e71cSDavid Greenman 		if (kread((u_long)ifnet.if_name, tname, 16))
3629b50d902SRodney W. Grimes 			break;
3631b72e71cSDavid Greenman 		tname[15] = '\0';
3641b72e71cSDavid Greenman 		snprintf(name, 16, "%s%d", tname, ifnet.if_unit);
3650b87c915SDavid Greenman 		if (interface && strcmp(name, interface) == 0) {
3669b50d902SRodney W. Grimes 			interesting = ip;
3670b87c915SDavid Greenman 			interesting_off = off;
3680b87c915SDavid Greenman 		}
3691b72e71cSDavid Greenman 		snprintf(ip->ift_name, 16, "(%s)", name);;
3709b50d902SRodney W. Grimes 		ip++;
3719b50d902SRodney W. Grimes 		if (ip >= iftot + MAXIF - 2)
3729b50d902SRodney W. Grimes 			break;
3739b50d902SRodney W. Grimes 		off = (u_long) ifnet.if_next;
3749b50d902SRodney W. Grimes 	}
3759b50d902SRodney W. Grimes 	lastif = ip;
3769b50d902SRodney W. Grimes 
3779b50d902SRodney W. Grimes 	(void)signal(SIGALRM, catchalarm);
3789b50d902SRodney W. Grimes 	signalled = NO;
3799b50d902SRodney W. Grimes 	(void)alarm(interval);
3809b50d902SRodney W. Grimes 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
3819b50d902SRodney W. Grimes 		ip->ift_ip = 0;
3829b50d902SRodney W. Grimes 		ip->ift_ie = 0;
3830b87c915SDavid Greenman 		ip->ift_ib = 0;
3849b50d902SRodney W. Grimes 		ip->ift_op = 0;
3859b50d902SRodney W. Grimes 		ip->ift_oe = 0;
3860b87c915SDavid Greenman 		ip->ift_ob = 0;
3879b50d902SRodney W. Grimes 		ip->ift_co = 0;
3889b50d902SRodney W. Grimes 		ip->ift_dr = 0;
3899b50d902SRodney W. Grimes 	}
3900b87c915SDavid Greenman 	first = 1;
3910b87c915SDavid Greenman banner:
3920b87c915SDavid Greenman 	printf("%17s %14s %16s", "input",
3930b87c915SDavid Greenman 	    interesting ? interesting->ift_name : "(Total)", "output");
3949b50d902SRodney W. Grimes 	putchar('\n');
3950b87c915SDavid Greenman 	printf("%10s %5s %10s %10s %5s %10s %5s",
3960b87c915SDavid Greenman 	    "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
3979b50d902SRodney W. Grimes 	if (dflag)
3989b50d902SRodney W. Grimes 		printf(" %5.5s", "drops");
3999b50d902SRodney W. Grimes 	putchar('\n');
4009b50d902SRodney W. Grimes 	fflush(stdout);
4019b50d902SRodney W. Grimes 	line = 0;
4029b50d902SRodney W. Grimes loop:
4030b87c915SDavid Greenman 	if (interesting != NULL) {
4040b87c915SDavid Greenman 		ip = interesting;
4050b87c915SDavid Greenman 		if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
4060b87c915SDavid Greenman 			printf("???\n");
4070b87c915SDavid Greenman 			exit(1);
4080b87c915SDavid Greenman 		};
4090b87c915SDavid Greenman 		if (!first) {
4107d56c0eeSAlexander Langer 			printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
4110b87c915SDavid Greenman 				ifnet.if_ipackets - ip->ift_ip,
4120b87c915SDavid Greenman 				ifnet.if_ierrors - ip->ift_ie,
4130b87c915SDavid Greenman 				ifnet.if_ibytes - ip->ift_ib,
4140b87c915SDavid Greenman 				ifnet.if_opackets - ip->ift_op,
4150b87c915SDavid Greenman 				ifnet.if_oerrors - ip->ift_oe,
4160b87c915SDavid Greenman 				ifnet.if_obytes - ip->ift_ob,
4170b87c915SDavid Greenman 				ifnet.if_collisions - ip->ift_co);
4180b87c915SDavid Greenman 			if (dflag)
4190b87c915SDavid Greenman 				printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
4200b87c915SDavid Greenman 		}
4210b87c915SDavid Greenman 		ip->ift_ip = ifnet.if_ipackets;
4220b87c915SDavid Greenman 		ip->ift_ie = ifnet.if_ierrors;
4230b87c915SDavid Greenman 		ip->ift_ib = ifnet.if_ibytes;
4240b87c915SDavid Greenman 		ip->ift_op = ifnet.if_opackets;
4250b87c915SDavid Greenman 		ip->ift_oe = ifnet.if_oerrors;
4260b87c915SDavid Greenman 		ip->ift_ob = ifnet.if_obytes;
4270b87c915SDavid Greenman 		ip->ift_co = ifnet.if_collisions;
4280b87c915SDavid Greenman 		ip->ift_dr = ifnet.if_snd.ifq_drops;
4290b87c915SDavid Greenman 	} else {
4309b50d902SRodney W. Grimes 		sum->ift_ip = 0;
4319b50d902SRodney W. Grimes 		sum->ift_ie = 0;
4320b87c915SDavid Greenman 		sum->ift_ib = 0;
4339b50d902SRodney W. Grimes 		sum->ift_op = 0;
4349b50d902SRodney W. Grimes 		sum->ift_oe = 0;
4350b87c915SDavid Greenman 		sum->ift_ob = 0;
4369b50d902SRodney W. Grimes 		sum->ift_co = 0;
4379b50d902SRodney W. Grimes 		sum->ift_dr = 0;
4389b50d902SRodney W. Grimes 		for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
4399b50d902SRodney W. Grimes 			if (kread(off, (char *)&ifnet, sizeof ifnet)) {
4409b50d902SRodney W. Grimes 				off = 0;
4419b50d902SRodney W. Grimes 				continue;
4429b50d902SRodney W. Grimes 			}
4430b87c915SDavid Greenman 			sum->ift_ip += ifnet.if_ipackets;
4440b87c915SDavid Greenman 			sum->ift_ie += ifnet.if_ierrors;
4450b87c915SDavid Greenman 			sum->ift_ib += ifnet.if_ibytes;
4460b87c915SDavid Greenman 			sum->ift_op += ifnet.if_opackets;
4470b87c915SDavid Greenman 			sum->ift_oe += ifnet.if_oerrors;
4480b87c915SDavid Greenman 			sum->ift_ob += ifnet.if_obytes;
4490b87c915SDavid Greenman 			sum->ift_co += ifnet.if_collisions;
4500b87c915SDavid Greenman 			sum->ift_dr += ifnet.if_snd.ifq_drops;
4519b50d902SRodney W. Grimes 			off = (u_long) ifnet.if_next;
4529b50d902SRodney W. Grimes 		}
4530b87c915SDavid Greenman 		if (!first) {
4540b87c915SDavid Greenman 			printf("%10u %5u %10u %10u %5u %10u %5u",
4559b50d902SRodney W. Grimes 				sum->ift_ip - total->ift_ip,
4560b87c915SDavid Greenman 				sum->ift_ie - total->ift_ie,
4570b87c915SDavid Greenman 				sum->ift_ib - total->ift_ib,
4589b50d902SRodney W. Grimes 				sum->ift_op - total->ift_op,
4590b87c915SDavid Greenman 				sum->ift_oe - total->ift_oe,
4600b87c915SDavid Greenman 				sum->ift_ob - total->ift_ob,
4610b87c915SDavid Greenman 				sum->ift_co - total->ift_co);
4629b50d902SRodney W. Grimes 			if (dflag)
4633aa80b1dSDavid Greenman 				printf(" %5u", sum->ift_dr - total->ift_dr);
4649b50d902SRodney W. Grimes 		}
4659b50d902SRodney W. Grimes 		*total = *sum;
4660b87c915SDavid Greenman 	}
4670b87c915SDavid Greenman 	if (!first)
4689b50d902SRodney W. Grimes 		putchar('\n');
4699b50d902SRodney W. Grimes 	fflush(stdout);
4709b50d902SRodney W. Grimes 	oldmask = sigblock(sigmask(SIGALRM));
4719b50d902SRodney W. Grimes 	if (! signalled) {
4729b50d902SRodney W. Grimes 		sigpause(0);
4739b50d902SRodney W. Grimes 	}
4749b50d902SRodney W. Grimes 	sigsetmask(oldmask);
4759b50d902SRodney W. Grimes 	signalled = NO;
4769b50d902SRodney W. Grimes 	(void)alarm(interval);
4770b87c915SDavid Greenman 	line++;
4780b87c915SDavid Greenman 	first = 0;
4799b50d902SRodney W. Grimes 	if (line == 21)
4809b50d902SRodney W. Grimes 		goto banner;
4810b87c915SDavid Greenman 	else
4829b50d902SRodney W. Grimes 		goto loop;
4839b50d902SRodney W. Grimes 	/*NOTREACHED*/
4849b50d902SRodney W. Grimes }
4859b50d902SRodney W. Grimes 
4869b50d902SRodney W. Grimes /*
4879b50d902SRodney W. Grimes  * Called if an interval expires before sidewaysintpr has completed a loop.
4889b50d902SRodney W. Grimes  * Sets a flag to not wait for the alarm.
4899b50d902SRodney W. Grimes  */
4909b50d902SRodney W. Grimes static void
4919b50d902SRodney W. Grimes catchalarm(signo)
4929b50d902SRodney W. Grimes 	int signo;
4939b50d902SRodney W. Grimes {
4949b50d902SRodney W. Grimes 	signalled = YES;
4959b50d902SRodney W. Grimes }
496