xref: /freebsd/usr.bin/netstat/if.c (revision 890bc2a54aec39776c98d627863b533eb03c3983)
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
3587669425SGarrett Wollman /*
3605ddff6eSPeter Wemm static char sccsid[] = "@(#)if.c	8.3 (Berkeley) 4/28/95";
3787669425SGarrett Wollman */
3887669425SGarrett Wollman static const char rcsid[] =
39c3aac50fSPeter Wemm   "$FreeBSD$";
409b50d902SRodney W. Grimes #endif /* not lint */
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes #include <sys/types.h>
439b50d902SRodney W. Grimes #include <sys/protosw.h>
449b50d902SRodney W. Grimes #include <sys/socket.h>
450024d1dbSLuigi Rizzo #include <sys/sysctl.h>
46628d2ac1SGarrett Wollman #include <sys/time.h>
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <net/if.h>
4987669425SGarrett Wollman #include <net/if_var.h>
509b50d902SRodney W. Grimes #include <net/if_dl.h>
51f6719675SBill Fenner #include <net/if_types.h>
520024d1dbSLuigi Rizzo #include <net/bridge.h>
5375fb8770SGarrett Wollman #include <net/ethernet.h>
549b50d902SRodney W. Grimes #include <netinet/in.h>
559b50d902SRodney W. Grimes #include <netinet/in_var.h>
56cc6a66f2SJulian Elischer #include <netipx/ipx.h>
57cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
58cbc17e71SGarrett Wollman #ifdef NS
599b50d902SRodney W. Grimes #include <netns/ns.h>
609b50d902SRodney W. Grimes #include <netns/ns_if.h>
61cbc17e71SGarrett Wollman #endif
620761cb29SGarrett Wollman #ifdef ISO
639b50d902SRodney W. Grimes #include <netiso/iso.h>
649b50d902SRodney W. Grimes #include <netiso/iso_var.h>
650761cb29SGarrett Wollman #endif
669b50d902SRodney W. Grimes #include <arpa/inet.h>
679b50d902SRodney W. Grimes 
689b50d902SRodney W. Grimes #include <signal.h>
699b50d902SRodney W. Grimes #include <stdio.h>
709b50d902SRodney W. Grimes #include <string.h>
719b50d902SRodney W. Grimes #include <unistd.h>
729b50d902SRodney W. Grimes 
739b50d902SRodney W. Grimes #include "netstat.h"
749b50d902SRodney W. Grimes 
759b50d902SRodney W. Grimes #define	YES	1
769b50d902SRodney W. Grimes #define	NO	0
779b50d902SRodney W. Grimes 
789b50d902SRodney W. Grimes static void sidewaysintpr __P((u_int, u_long));
799b50d902SRodney W. Grimes static void catchalarm __P((int));
809b50d902SRodney W. Grimes 
810024d1dbSLuigi Rizzo void
820024d1dbSLuigi Rizzo bdg_stats(u_long dummy, char *name) /* print bridge statistics */
830024d1dbSLuigi Rizzo {
84890bc2a5SDoug Rabson     int i;
85890bc2a5SDoug Rabson     size_t slen ;
860024d1dbSLuigi Rizzo     struct bdg_stats s ;
870024d1dbSLuigi Rizzo     int mib[4] ;
880024d1dbSLuigi Rizzo 
890024d1dbSLuigi Rizzo     slen = sizeof(s);
900024d1dbSLuigi Rizzo 
910024d1dbSLuigi Rizzo     mib[0] = CTL_NET ;
920024d1dbSLuigi Rizzo     mib[1] = PF_LINK ;
930024d1dbSLuigi Rizzo     mib[2] = IFT_ETHER ;
940024d1dbSLuigi Rizzo     mib[3] = PF_BDG ;
950024d1dbSLuigi Rizzo     if (sysctl(mib,4, &s,&slen,NULL,0)==-1)
960024d1dbSLuigi Rizzo 	return ; /* no bridging */
970024d1dbSLuigi Rizzo     printf("-- Bridging statistics (%s) --\n", name) ;
980024d1dbSLuigi Rizzo     printf(
990024d1dbSLuigi Rizzo "Name          In      Out  Forward     Drop    Bcast    Mcast    Local  Unknown\n");
1000024d1dbSLuigi Rizzo     for (i = 0 ; i < 16 ; i++) {
1010024d1dbSLuigi Rizzo 	if (s.s[i].name[0])
102f41f949dSMatthew Dillon 	printf("%-6s %9ld%9ld%9ld%9ld%9ld%9ld%9ld%9ld\n",
1030024d1dbSLuigi Rizzo 	  s.s[i].name,
1040024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_IN],
1050024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_OUT],
1060024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_FORWARD],
1070024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_DROP],
1080024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_BCAST],
1090024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_MCAST],
1100024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_LOCAL],
1110024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_UNKNOWN] );
1120024d1dbSLuigi Rizzo     }
1130024d1dbSLuigi Rizzo }
1140024d1dbSLuigi Rizzo 
1159b50d902SRodney W. Grimes /*
1169b50d902SRodney W. Grimes  * Print a description of the network interfaces.
1179b50d902SRodney W. Grimes  */
1189b50d902SRodney W. Grimes void
1199b50d902SRodney W. Grimes intpr(interval, ifnetaddr)
1209b50d902SRodney W. Grimes 	int interval;
1219b50d902SRodney W. Grimes 	u_long ifnetaddr;
1229b50d902SRodney W. Grimes {
1239b50d902SRodney W. Grimes 	struct ifnet ifnet;
1240e27dc05SGarrett Wollman 	struct ifnethead ifnethead;
1259b50d902SRodney W. Grimes 	union {
1269b50d902SRodney W. Grimes 		struct ifaddr ifa;
1279b50d902SRodney W. Grimes 		struct in_ifaddr in;
128cc6a66f2SJulian Elischer 		struct ipx_ifaddr ipx;
129cbc17e71SGarrett Wollman #ifdef NS
1309b50d902SRodney W. Grimes 		struct ns_ifaddr ns;
131cbc17e71SGarrett Wollman #endif
1320761cb29SGarrett Wollman #ifdef ISO
1339b50d902SRodney W. Grimes 		struct iso_ifaddr iso;
1340761cb29SGarrett Wollman #endif
1359b50d902SRodney W. Grimes 	} ifaddr;
1369b50d902SRodney W. Grimes 	u_long ifaddraddr;
137f6719675SBill Fenner 	u_long ifaddrfound;
138f6719675SBill Fenner 	u_long ifnetfound;
139a97a9922SJulian Elischer 	struct sockaddr *sa = NULL;
1401b72e71cSDavid Greenman 	char name[32], tname[16];
1419b50d902SRodney W. Grimes 
1429b50d902SRodney W. Grimes 	if (ifnetaddr == 0) {
1439b50d902SRodney W. Grimes 		printf("ifnet: symbol not defined\n");
1449b50d902SRodney W. Grimes 		return;
1459b50d902SRodney W. Grimes 	}
1469b50d902SRodney W. Grimes 	if (interval) {
1479b50d902SRodney W. Grimes 		sidewaysintpr((unsigned)interval, ifnetaddr);
1489b50d902SRodney W. Grimes 		return;
1499b50d902SRodney W. Grimes 	}
1500e27dc05SGarrett Wollman 	if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
1519b50d902SRodney W. Grimes 		return;
1520e27dc05SGarrett Wollman 	ifnetaddr = (u_long)ifnethead.tqh_first;
1530e27dc05SGarrett Wollman 	if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
1540e27dc05SGarrett Wollman 		return;
1550e27dc05SGarrett Wollman 
156cc6a66f2SJulian Elischer 	printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
157e1e293a5SDavid Greenman 		"Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
158e1e293a5SDavid Greenman 	if (bflag)
159e1e293a5SDavid Greenman 		printf(" %10.10s","Ibytes");
160e1e293a5SDavid Greenman 	printf(" %8.8s %5.5s", "Opkts", "Oerrs");
161e1e293a5SDavid Greenman 	if (bflag)
162e1e293a5SDavid Greenman 		printf(" %10.10s","Obytes");
1639b50d902SRodney W. Grimes 	printf(" %5s", "Coll");
1649b50d902SRodney W. Grimes 	if (tflag)
1659b50d902SRodney W. Grimes 		printf(" %s", "Time");
1669b50d902SRodney W. Grimes 	if (dflag)
1679b50d902SRodney W. Grimes 		printf(" %s", "Drop");
1689b50d902SRodney W. Grimes 	putchar('\n');
1699b50d902SRodney W. Grimes 	ifaddraddr = 0;
1709b50d902SRodney W. Grimes 	while (ifnetaddr || ifaddraddr) {
1719b50d902SRodney W. Grimes 		struct sockaddr_in *sin;
1729b50d902SRodney W. Grimes 		register char *cp;
1739b50d902SRodney W. Grimes 		int n, m;
1749b50d902SRodney W. Grimes 
1759b50d902SRodney W. Grimes 		if (ifaddraddr == 0) {
176f6719675SBill Fenner 			ifnetfound = ifnetaddr;
1779b50d902SRodney W. Grimes 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) ||
1781b72e71cSDavid Greenman 			    kread((u_long)ifnet.if_name, tname, 16))
1799b50d902SRodney W. Grimes 				return;
1801b72e71cSDavid Greenman 			tname[15] = '\0';
1810e27dc05SGarrett Wollman 			ifnetaddr = (u_long)ifnet.if_link.tqe_next;
1821b72e71cSDavid Greenman 			snprintf(name, 32, "%s%d", tname, ifnet.if_unit);
1831b72e71cSDavid Greenman 			if (interface != 0 && (strcmp(name, interface) != 0))
1849b50d902SRodney W. Grimes 				continue;
1859b50d902SRodney W. Grimes 			cp = index(name, '\0');
1869b50d902SRodney W. Grimes 			if ((ifnet.if_flags&IFF_UP) == 0)
1879b50d902SRodney W. Grimes 				*cp++ = '*';
1889b50d902SRodney W. Grimes 			*cp = '\0';
18915244cd5SGarrett Wollman 			ifaddraddr = (u_long)ifnet.if_addrhead.tqh_first;
1909b50d902SRodney W. Grimes 		}
1917d56c0eeSAlexander Langer 		printf("%-5.5s %-5lu ", name, ifnet.if_mtu);
192f6719675SBill Fenner 		ifaddrfound = ifaddraddr;
1939b50d902SRodney W. Grimes 		if (ifaddraddr == 0) {
194cc6a66f2SJulian Elischer 			printf("%-13.13s ", "none");
1959b50d902SRodney W. Grimes 			printf("%-15.15s ", "none");
1969b50d902SRodney W. Grimes 		} else {
1979b50d902SRodney W. Grimes 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
1989b50d902SRodney W. Grimes 				ifaddraddr = 0;
1999b50d902SRodney W. Grimes 				continue;
2009b50d902SRodney W. Grimes 			}
2019b50d902SRodney W. Grimes #define CP(x) ((char *)(x))
2029b50d902SRodney W. Grimes 			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
203a97a9922SJulian Elischer 				CP(&ifaddr);
204a97a9922SJulian Elischer 			sa = (struct sockaddr *)cp;
2059b50d902SRodney W. Grimes 			switch (sa->sa_family) {
2069b50d902SRodney W. Grimes 			case AF_UNSPEC:
207cc6a66f2SJulian Elischer 				printf("%-13.13s ", "none");
2089b50d902SRodney W. Grimes 				printf("%-15.15s ", "none");
2099b50d902SRodney W. Grimes 				break;
2109b50d902SRodney W. Grimes 			case AF_INET:
2119b50d902SRodney W. Grimes 				sin = (struct sockaddr_in *)sa;
2129b50d902SRodney W. Grimes #ifdef notdef
2139b50d902SRodney W. Grimes 				/* can't use inet_makeaddr because kernel
2149b50d902SRodney W. Grimes 				 * keeps nets unshifted.
2159b50d902SRodney W. Grimes 				 */
2169b50d902SRodney W. Grimes 				in = inet_makeaddr(ifaddr.in.ia_subnet,
2179b50d902SRodney W. Grimes 					INADDR_ANY);
21809a67ffaSStefan Eßer 				printf("%-13.13s ", netname(in.s_addr,
2199b50d902SRodney W. Grimes 				    ifaddr.in.ia_subnetmask));
2209b50d902SRodney W. Grimes #else
22109a67ffaSStefan Eßer 				printf("%-13.13s ",
2229b50d902SRodney W. Grimes 				    netname(htonl(ifaddr.in.ia_subnet),
2239b50d902SRodney W. Grimes 				    ifaddr.in.ia_subnetmask));
2249b50d902SRodney W. Grimes #endif
2259b50d902SRodney W. Grimes 				printf("%-15.15s ",
2269b50d902SRodney W. Grimes 				    routename(sin->sin_addr.s_addr));
2279b50d902SRodney W. Grimes 				break;
228cc6a66f2SJulian Elischer 			case AF_IPX:
229cc6a66f2SJulian Elischer 				{
230cc6a66f2SJulian Elischer 				struct sockaddr_ipx *sipx =
231cc6a66f2SJulian Elischer 					(struct sockaddr_ipx *)sa;
232cc6a66f2SJulian Elischer 				u_long net;
233cc6a66f2SJulian Elischer 				char netnum[10];
234cc6a66f2SJulian Elischer 
235cc6a66f2SJulian Elischer 				*(union ipx_net *) &net = sipx->sipx_addr.x_net;
23622694ebaSBruce Evans 				sprintf(netnum, "%lx", (u_long)ntohl(net));
237cc6a66f2SJulian Elischer 				printf("ipx:%-8s  ", netnum);
238cc6a66f2SJulian Elischer /*				printf("ipx:%-8s ", netname(net, 0L)); */
239cc6a66f2SJulian Elischer 				printf("%-15s ",
240cc6a66f2SJulian Elischer 				    ipx_phost((struct sockaddr *)sipx));
241cc6a66f2SJulian Elischer 				}
242cc6a66f2SJulian Elischer 				break;
2436ffcfd6cSJulian Elischer 
2446ffcfd6cSJulian Elischer 			case AF_APPLETALK:
245a8d37845SJulian Elischer 				printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
246a8d37845SJulian Elischer 				printf("%-9.9s  ",atalk_print(sa,0x0b) );
2476ffcfd6cSJulian Elischer 				break;
248cbc17e71SGarrett Wollman #ifdef NS
2499b50d902SRodney W. Grimes 			case AF_NS:
2509b50d902SRodney W. Grimes 				{
2519b50d902SRodney W. Grimes 				struct sockaddr_ns *sns =
2529b50d902SRodney W. Grimes 					(struct sockaddr_ns *)sa;
2539b50d902SRodney W. Grimes 				u_long net;
254cc6a66f2SJulian Elischer 				char netnum[10];
2559b50d902SRodney W. Grimes 
2569b50d902SRodney W. Grimes 				*(union ns_net *) &net = sns->sns_addr.x_net;
2579b50d902SRodney W. Grimes 				sprintf(netnum, "%lxH", ntohl(net));
2589b50d902SRodney W. Grimes 				upHex(netnum);
2599b50d902SRodney W. Grimes 				printf("ns:%-8s ", netnum);
2609b50d902SRodney W. Grimes 				printf("%-15s ",
2619b50d902SRodney W. Grimes 				    ns_phost((struct sockaddr *)sns));
2629b50d902SRodney W. Grimes 				}
2639b50d902SRodney W. Grimes 				break;
264cbc17e71SGarrett Wollman #endif
2659b50d902SRodney W. Grimes 			case AF_LINK:
2669b50d902SRodney W. Grimes 				{
2679b50d902SRodney W. Grimes 				struct sockaddr_dl *sdl =
2689b50d902SRodney W. Grimes 					(struct sockaddr_dl *)sa;
2699b50d902SRodney W. Grimes 				    cp = (char *)LLADDR(sdl);
2709b50d902SRodney W. Grimes 				    n = sdl->sdl_alen;
2719b50d902SRodney W. Grimes 				}
27205ddff6eSPeter Wemm 				m = printf("%-11.11s ", "<Link>");
2739b50d902SRodney W. Grimes 				goto hexprint;
2749b50d902SRodney W. Grimes 			default:
2759b50d902SRodney W. Grimes 				m = printf("(%d)", sa->sa_family);
2769b50d902SRodney W. Grimes 				for (cp = sa->sa_len + (char *)sa;
2779b50d902SRodney W. Grimes 					--cp > sa->sa_data && (*cp == 0);) {}
2789b50d902SRodney W. Grimes 				n = cp - sa->sa_data + 1;
2799b50d902SRodney W. Grimes 				cp = sa->sa_data;
2809b50d902SRodney W. Grimes 			hexprint:
2819b50d902SRodney W. Grimes 				while (--n >= 0)
282541f2562SDavid Greenman 					m += printf("%02x%c", *cp++ & 0xff,
2839b50d902SRodney W. Grimes 						    n > 0 ? '.' : ' ');
284cc6a66f2SJulian Elischer 				m = 30 - m;
2859b50d902SRodney W. Grimes 				while (m-- > 0)
2869b50d902SRodney W. Grimes 					putchar(' ');
2879b50d902SRodney W. Grimes 				break;
2889b50d902SRodney W. Grimes 			}
28915244cd5SGarrett Wollman 			ifaddraddr = (u_long)ifaddr.ifa.ifa_link.tqe_next;
2909b50d902SRodney W. Grimes 		}
2917d56c0eeSAlexander Langer 		printf("%8lu %5lu ",
292e1e293a5SDavid Greenman 		    ifnet.if_ipackets, ifnet.if_ierrors);
293e1e293a5SDavid Greenman 		if (bflag)
2947d56c0eeSAlexander Langer 			printf("%10lu ", ifnet.if_ibytes);
2957d56c0eeSAlexander Langer 		printf("%8lu %5lu ",
296e1e293a5SDavid Greenman 		    ifnet.if_opackets, ifnet.if_oerrors);
297e1e293a5SDavid Greenman 		if (bflag)
2987d56c0eeSAlexander Langer 			printf("%10lu ", ifnet.if_obytes);
2997d56c0eeSAlexander Langer 		printf("%5lu", ifnet.if_collisions);
3009b50d902SRodney W. Grimes 		if (tflag)
3019b50d902SRodney W. Grimes 			printf(" %3d", ifnet.if_timer);
3029b50d902SRodney W. Grimes 		if (dflag)
3039b50d902SRodney W. Grimes 			printf(" %3d", ifnet.if_snd.ifq_drops);
3049b50d902SRodney W. Grimes 		putchar('\n');
305f6719675SBill Fenner 		if (aflag && ifaddrfound) {
306f6719675SBill Fenner 			/*
307f6719675SBill Fenner 			 * Print family's multicast addresses
308f6719675SBill Fenner 			 */
309f6719675SBill Fenner 			u_long multiaddr;
31075fb8770SGarrett Wollman 			struct ifmultiaddr ifma;
31175fb8770SGarrett Wollman 			union {
31275fb8770SGarrett Wollman 				struct sockaddr sa;
31375fb8770SGarrett Wollman 				struct sockaddr_in in;
31475fb8770SGarrett Wollman 				struct sockaddr_dl dl;
31575fb8770SGarrett Wollman 			} msa;
31675fb8770SGarrett Wollman 			const char *fmt;
317f6719675SBill Fenner 
31875fb8770SGarrett Wollman 			for(multiaddr = (u_long)ifnet.if_multiaddrs.lh_first;
31975fb8770SGarrett Wollman 			    multiaddr;
32075fb8770SGarrett Wollman 			    multiaddr = (u_long)ifma.ifma_link.le_next) {
32175fb8770SGarrett Wollman 				if (kread(multiaddr, (char *)&ifma,
32275fb8770SGarrett Wollman 					  sizeof ifma))
323f6719675SBill Fenner 					break;
32475fb8770SGarrett Wollman 				if (kread((u_long)ifma.ifma_addr, (char *)&msa,
32575fb8770SGarrett Wollman 					  sizeof msa))
32675fb8770SGarrett Wollman 					break;
32775fb8770SGarrett Wollman 				if (msa.sa.sa_family != sa->sa_family)
32875fb8770SGarrett Wollman 					continue;
32975fb8770SGarrett Wollman 
33075fb8770SGarrett Wollman 				fmt = 0;
33175fb8770SGarrett Wollman 				switch (msa.sa.sa_family) {
33275fb8770SGarrett Wollman 				case AF_INET:
33375fb8770SGarrett Wollman 					fmt = routename(msa.in.sin_addr.s_addr);
33475fb8770SGarrett Wollman 					break;
33575fb8770SGarrett Wollman 
336f6719675SBill Fenner 				case AF_LINK:
337f6719675SBill Fenner 					switch (ifnet.if_type) {
338f6719675SBill Fenner 					case IFT_ETHER:
33975fb8770SGarrett Wollman 					case IFT_FDDI:
34075fb8770SGarrett Wollman 						fmt = ether_ntoa(
34175fb8770SGarrett Wollman 							(struct ether_addr *)
34275fb8770SGarrett Wollman 							LLADDR(&msa.dl));
34375fb8770SGarrett Wollman 						break;
344f6719675SBill Fenner 					}
345f6719675SBill Fenner 					break;
346f6719675SBill Fenner 				}
34775fb8770SGarrett Wollman 				if (fmt)
34875fb8770SGarrett Wollman 					printf("%23s %s\n", "", fmt);
349f6719675SBill Fenner 			}
350f6719675SBill Fenner 		}
3519b50d902SRodney W. Grimes 	}
3529b50d902SRodney W. Grimes }
3539b50d902SRodney W. Grimes 
3549b50d902SRodney W. Grimes #define	MAXIF	10
3559b50d902SRodney W. Grimes struct	iftot {
3569b50d902SRodney W. Grimes 	char	ift_name[16];		/* interface name */
357890bc2a5SDoug Rabson 	u_long	ift_ip;			/* input packets */
358890bc2a5SDoug Rabson 	u_long	ift_ie;			/* input errors */
359890bc2a5SDoug Rabson 	u_long	ift_op;			/* output packets */
360890bc2a5SDoug Rabson 	u_long	ift_oe;			/* output errors */
361890bc2a5SDoug Rabson 	u_long	ift_co;			/* collisions */
362e1e293a5SDavid Greenman 	u_int	ift_dr;			/* drops */
363890bc2a5SDoug Rabson 	u_long	ift_ib;			/* input bytes */
364890bc2a5SDoug Rabson 	u_long	ift_ob;			/* output bytes */
3659b50d902SRodney W. Grimes } iftot[MAXIF];
3669b50d902SRodney W. Grimes 
3679b50d902SRodney W. Grimes u_char	signalled;			/* set if alarm goes off "early" */
3689b50d902SRodney W. Grimes 
3699b50d902SRodney W. Grimes /*
3709b50d902SRodney W. Grimes  * Print a running summary of interface statistics.
3719b50d902SRodney W. Grimes  * Repeat display every interval seconds, showing statistics
3729b50d902SRodney W. Grimes  * collected over that interval.  Assumes that interval is non-zero.
3739b50d902SRodney W. Grimes  * First line printed at top of screen is always cumulative.
3740e27dc05SGarrett Wollman  * XXX - should be rewritten to use ifmib(4).
3759b50d902SRodney W. Grimes  */
3769b50d902SRodney W. Grimes static void
3779b50d902SRodney W. Grimes sidewaysintpr(interval, off)
3789b50d902SRodney W. Grimes 	unsigned interval;
3799b50d902SRodney W. Grimes 	u_long off;
3809b50d902SRodney W. Grimes {
3819b50d902SRodney W. Grimes 	struct ifnet ifnet;
3829b50d902SRodney W. Grimes 	u_long firstifnet;
3830e27dc05SGarrett Wollman 	struct ifnethead ifnethead;
3849b50d902SRodney W. Grimes 	register struct iftot *ip, *total;
3859b50d902SRodney W. Grimes 	register int line;
3869b50d902SRodney W. Grimes 	struct iftot *lastif, *sum, *interesting;
3870b87c915SDavid Greenman 	int oldmask, first;
3880b87c915SDavid Greenman 	u_long interesting_off;
3899b50d902SRodney W. Grimes 
3900e27dc05SGarrett Wollman 	if (kread(off, (char *)&ifnethead, sizeof ifnethead))
3919b50d902SRodney W. Grimes 		return;
3920e27dc05SGarrett Wollman 	firstifnet = (u_long)ifnethead.tqh_first;
3930e27dc05SGarrett Wollman 
3949b50d902SRodney W. Grimes 	lastif = iftot;
3959b50d902SRodney W. Grimes 	sum = iftot + MAXIF - 1;
3969b50d902SRodney W. Grimes 	total = sum - 1;
3970b87c915SDavid Greenman 	interesting = NULL;
3980b87c915SDavid Greenman 	interesting_off = 0;
3999b50d902SRodney W. Grimes 	for (off = firstifnet, ip = iftot; off;) {
4001b72e71cSDavid Greenman 		char name[16], tname[16];
4019b50d902SRodney W. Grimes 
4029b50d902SRodney W. Grimes 		if (kread(off, (char *)&ifnet, sizeof ifnet))
4039b50d902SRodney W. Grimes 			break;
4041b72e71cSDavid Greenman 		if (kread((u_long)ifnet.if_name, tname, 16))
4059b50d902SRodney W. Grimes 			break;
4061b72e71cSDavid Greenman 		tname[15] = '\0';
4071b72e71cSDavid Greenman 		snprintf(name, 16, "%s%d", tname, ifnet.if_unit);
4080b87c915SDavid Greenman 		if (interface && strcmp(name, interface) == 0) {
4099b50d902SRodney W. Grimes 			interesting = ip;
4100b87c915SDavid Greenman 			interesting_off = off;
4110b87c915SDavid Greenman 		}
4121b72e71cSDavid Greenman 		snprintf(ip->ift_name, 16, "(%s)", name);;
4139b50d902SRodney W. Grimes 		ip++;
4149b50d902SRodney W. Grimes 		if (ip >= iftot + MAXIF - 2)
4159b50d902SRodney W. Grimes 			break;
4160e27dc05SGarrett Wollman 		off = (u_long) ifnet.if_link.tqe_next;
4179b50d902SRodney W. Grimes 	}
4189b50d902SRodney W. Grimes 	lastif = ip;
4199b50d902SRodney W. Grimes 
4209b50d902SRodney W. Grimes 	(void)signal(SIGALRM, catchalarm);
4219b50d902SRodney W. Grimes 	signalled = NO;
4229b50d902SRodney W. Grimes 	(void)alarm(interval);
4239b50d902SRodney W. Grimes 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
4249b50d902SRodney W. Grimes 		ip->ift_ip = 0;
4259b50d902SRodney W. Grimes 		ip->ift_ie = 0;
4260b87c915SDavid Greenman 		ip->ift_ib = 0;
4279b50d902SRodney W. Grimes 		ip->ift_op = 0;
4289b50d902SRodney W. Grimes 		ip->ift_oe = 0;
4290b87c915SDavid Greenman 		ip->ift_ob = 0;
4309b50d902SRodney W. Grimes 		ip->ift_co = 0;
4319b50d902SRodney W. Grimes 		ip->ift_dr = 0;
4329b50d902SRodney W. Grimes 	}
4330b87c915SDavid Greenman 	first = 1;
4340b87c915SDavid Greenman banner:
4350b87c915SDavid Greenman 	printf("%17s %14s %16s", "input",
4360b87c915SDavid Greenman 	    interesting ? interesting->ift_name : "(Total)", "output");
4379b50d902SRodney W. Grimes 	putchar('\n');
4380b87c915SDavid Greenman 	printf("%10s %5s %10s %10s %5s %10s %5s",
4390b87c915SDavid Greenman 	    "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
4409b50d902SRodney W. Grimes 	if (dflag)
4419b50d902SRodney W. Grimes 		printf(" %5.5s", "drops");
4429b50d902SRodney W. Grimes 	putchar('\n');
4439b50d902SRodney W. Grimes 	fflush(stdout);
4449b50d902SRodney W. Grimes 	line = 0;
4459b50d902SRodney W. Grimes loop:
4460b87c915SDavid Greenman 	if (interesting != NULL) {
4470b87c915SDavid Greenman 		ip = interesting;
4480b87c915SDavid Greenman 		if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
4490b87c915SDavid Greenman 			printf("???\n");
4500b87c915SDavid Greenman 			exit(1);
4510b87c915SDavid Greenman 		};
4520b87c915SDavid Greenman 		if (!first) {
4537d56c0eeSAlexander Langer 			printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
4540b87c915SDavid Greenman 				ifnet.if_ipackets - ip->ift_ip,
4550b87c915SDavid Greenman 				ifnet.if_ierrors - ip->ift_ie,
4560b87c915SDavid Greenman 				ifnet.if_ibytes - ip->ift_ib,
4570b87c915SDavid Greenman 				ifnet.if_opackets - ip->ift_op,
4580b87c915SDavid Greenman 				ifnet.if_oerrors - ip->ift_oe,
4590b87c915SDavid Greenman 				ifnet.if_obytes - ip->ift_ob,
4600b87c915SDavid Greenman 				ifnet.if_collisions - ip->ift_co);
4610b87c915SDavid Greenman 			if (dflag)
4620b87c915SDavid Greenman 				printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
4630b87c915SDavid Greenman 		}
4640b87c915SDavid Greenman 		ip->ift_ip = ifnet.if_ipackets;
4650b87c915SDavid Greenman 		ip->ift_ie = ifnet.if_ierrors;
4660b87c915SDavid Greenman 		ip->ift_ib = ifnet.if_ibytes;
4670b87c915SDavid Greenman 		ip->ift_op = ifnet.if_opackets;
4680b87c915SDavid Greenman 		ip->ift_oe = ifnet.if_oerrors;
4690b87c915SDavid Greenman 		ip->ift_ob = ifnet.if_obytes;
4700b87c915SDavid Greenman 		ip->ift_co = ifnet.if_collisions;
4710b87c915SDavid Greenman 		ip->ift_dr = ifnet.if_snd.ifq_drops;
4720b87c915SDavid Greenman 	} else {
4739b50d902SRodney W. Grimes 		sum->ift_ip = 0;
4749b50d902SRodney W. Grimes 		sum->ift_ie = 0;
4750b87c915SDavid Greenman 		sum->ift_ib = 0;
4769b50d902SRodney W. Grimes 		sum->ift_op = 0;
4779b50d902SRodney W. Grimes 		sum->ift_oe = 0;
4780b87c915SDavid Greenman 		sum->ift_ob = 0;
4799b50d902SRodney W. Grimes 		sum->ift_co = 0;
4809b50d902SRodney W. Grimes 		sum->ift_dr = 0;
4819b50d902SRodney W. Grimes 		for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
4829b50d902SRodney W. Grimes 			if (kread(off, (char *)&ifnet, sizeof ifnet)) {
4839b50d902SRodney W. Grimes 				off = 0;
4849b50d902SRodney W. Grimes 				continue;
4859b50d902SRodney W. Grimes 			}
4860b87c915SDavid Greenman 			sum->ift_ip += ifnet.if_ipackets;
4870b87c915SDavid Greenman 			sum->ift_ie += ifnet.if_ierrors;
4880b87c915SDavid Greenman 			sum->ift_ib += ifnet.if_ibytes;
4890b87c915SDavid Greenman 			sum->ift_op += ifnet.if_opackets;
4900b87c915SDavid Greenman 			sum->ift_oe += ifnet.if_oerrors;
4910b87c915SDavid Greenman 			sum->ift_ob += ifnet.if_obytes;
4920b87c915SDavid Greenman 			sum->ift_co += ifnet.if_collisions;
4930b87c915SDavid Greenman 			sum->ift_dr += ifnet.if_snd.ifq_drops;
4940e27dc05SGarrett Wollman 			off = (u_long) ifnet.if_link.tqe_next;
4959b50d902SRodney W. Grimes 		}
4960b87c915SDavid Greenman 		if (!first) {
497890bc2a5SDoug Rabson 			printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
4989b50d902SRodney W. Grimes 				sum->ift_ip - total->ift_ip,
4990b87c915SDavid Greenman 				sum->ift_ie - total->ift_ie,
5000b87c915SDavid Greenman 				sum->ift_ib - total->ift_ib,
5019b50d902SRodney W. Grimes 				sum->ift_op - total->ift_op,
5020b87c915SDavid Greenman 				sum->ift_oe - total->ift_oe,
5030b87c915SDavid Greenman 				sum->ift_ob - total->ift_ob,
5040b87c915SDavid Greenman 				sum->ift_co - total->ift_co);
5059b50d902SRodney W. Grimes 			if (dflag)
5063aa80b1dSDavid Greenman 				printf(" %5u", sum->ift_dr - total->ift_dr);
5079b50d902SRodney W. Grimes 		}
5089b50d902SRodney W. Grimes 		*total = *sum;
5090b87c915SDavid Greenman 	}
5100b87c915SDavid Greenman 	if (!first)
5119b50d902SRodney W. Grimes 		putchar('\n');
5129b50d902SRodney W. Grimes 	fflush(stdout);
5139b50d902SRodney W. Grimes 	oldmask = sigblock(sigmask(SIGALRM));
5149b50d902SRodney W. Grimes 	if (! signalled) {
5159b50d902SRodney W. Grimes 		sigpause(0);
5169b50d902SRodney W. Grimes 	}
5179b50d902SRodney W. Grimes 	sigsetmask(oldmask);
5189b50d902SRodney W. Grimes 	signalled = NO;
5199b50d902SRodney W. Grimes 	(void)alarm(interval);
5200b87c915SDavid Greenman 	line++;
5210b87c915SDavid Greenman 	first = 0;
5229b50d902SRodney W. Grimes 	if (line == 21)
5239b50d902SRodney W. Grimes 		goto banner;
5240b87c915SDavid Greenman 	else
5259b50d902SRodney W. Grimes 		goto loop;
5269b50d902SRodney W. Grimes 	/*NOTREACHED*/
5279b50d902SRodney W. Grimes }
5289b50d902SRodney W. Grimes 
5299b50d902SRodney W. Grimes /*
5309b50d902SRodney W. Grimes  * Called if an interval expires before sidewaysintpr has completed a loop.
5319b50d902SRodney W. Grimes  * Sets a flag to not wait for the alarm.
5329b50d902SRodney W. Grimes  */
5339b50d902SRodney W. Grimes static void
5349b50d902SRodney W. Grimes catchalarm(signo)
5359b50d902SRodney W. Grimes 	int signo;
5369b50d902SRodney W. Grimes {
5379b50d902SRodney W. Grimes 	signalled = YES;
5389b50d902SRodney W. Grimes }
539