xref: /freebsd/usr.bin/netstat/if.c (revision 5da9f8fa978ae925af826ba78653757d2c84c9c1)
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 
81cfa1ca9dSYoshinobu Inoue #ifdef INET6
82cfa1ca9dSYoshinobu Inoue char *netname6 __P((struct sockaddr_in6 *, struct in6_addr *));
83cfa1ca9dSYoshinobu Inoue static char ntop_buf[INET6_ADDRSTRLEN];		/* for inet_ntop() */
84cfa1ca9dSYoshinobu Inoue static int bdg_done;
85cfa1ca9dSYoshinobu Inoue #endif
86cfa1ca9dSYoshinobu Inoue 
870024d1dbSLuigi Rizzo void
880024d1dbSLuigi Rizzo bdg_stats(u_long dummy, char *name) /* print bridge statistics */
890024d1dbSLuigi Rizzo {
90890bc2a5SDoug Rabson     int i;
91890bc2a5SDoug Rabson     size_t slen ;
920024d1dbSLuigi Rizzo     struct bdg_stats s ;
930024d1dbSLuigi Rizzo     int mib[4] ;
940024d1dbSLuigi Rizzo 
950024d1dbSLuigi Rizzo     slen = sizeof(s);
960024d1dbSLuigi Rizzo 
970024d1dbSLuigi Rizzo     mib[0] = CTL_NET ;
980024d1dbSLuigi Rizzo     mib[1] = PF_LINK ;
990024d1dbSLuigi Rizzo     mib[2] = IFT_ETHER ;
1000024d1dbSLuigi Rizzo     mib[3] = PF_BDG ;
1010024d1dbSLuigi Rizzo     if (sysctl(mib,4, &s,&slen,NULL,0)==-1)
1020024d1dbSLuigi Rizzo 	return ; /* no bridging */
103cfa1ca9dSYoshinobu Inoue #ifdef INET6
104cfa1ca9dSYoshinobu Inoue     if (bdg_done != 0)
105cfa1ca9dSYoshinobu Inoue 	return;
106cfa1ca9dSYoshinobu Inoue     else
107cfa1ca9dSYoshinobu Inoue 	bdg_done = 1;
108cfa1ca9dSYoshinobu Inoue #endif
1090024d1dbSLuigi Rizzo     printf("-- Bridging statistics (%s) --\n", name) ;
1100024d1dbSLuigi Rizzo     printf(
1110024d1dbSLuigi Rizzo "Name          In      Out  Forward     Drop    Bcast    Mcast    Local  Unknown\n");
1120024d1dbSLuigi Rizzo     for (i = 0 ; i < 16 ; i++) {
1130024d1dbSLuigi Rizzo 	if (s.s[i].name[0])
114f41f949dSMatthew Dillon 	printf("%-6s %9ld%9ld%9ld%9ld%9ld%9ld%9ld%9ld\n",
1150024d1dbSLuigi Rizzo 	  s.s[i].name,
1160024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_IN],
1170024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_OUT],
1180024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_FORWARD],
1190024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_DROP],
1200024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_BCAST],
1210024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_MCAST],
1220024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_LOCAL],
1230024d1dbSLuigi Rizzo 	  s.s[i].p_in[(int)BDG_UNKNOWN] );
1240024d1dbSLuigi Rizzo     }
1250024d1dbSLuigi Rizzo }
1260024d1dbSLuigi Rizzo 
1279b50d902SRodney W. Grimes /*
1289b50d902SRodney W. Grimes  * Print a description of the network interfaces.
1299b50d902SRodney W. Grimes  */
1309b50d902SRodney W. Grimes void
131cfa1ca9dSYoshinobu Inoue intpr(interval, ifnetaddr, pfunc)
1329b50d902SRodney W. Grimes 	int interval;
1339b50d902SRodney W. Grimes 	u_long ifnetaddr;
134cfa1ca9dSYoshinobu Inoue 	void (*pfunc)(char *);
1359b50d902SRodney W. Grimes {
1369b50d902SRodney W. Grimes 	struct ifnet ifnet;
1370e27dc05SGarrett Wollman 	struct ifnethead ifnethead;
1389b50d902SRodney W. Grimes 	union {
1399b50d902SRodney W. Grimes 		struct ifaddr ifa;
1409b50d902SRodney W. Grimes 		struct in_ifaddr in;
141cfa1ca9dSYoshinobu Inoue #ifdef INET6
142cfa1ca9dSYoshinobu Inoue 		struct in6_ifaddr in6;
143cfa1ca9dSYoshinobu Inoue #endif
144cc6a66f2SJulian Elischer 		struct ipx_ifaddr ipx;
145cbc17e71SGarrett Wollman #ifdef NS
1469b50d902SRodney W. Grimes 		struct ns_ifaddr ns;
147cbc17e71SGarrett Wollman #endif
1480761cb29SGarrett Wollman #ifdef ISO
1499b50d902SRodney W. Grimes 		struct iso_ifaddr iso;
1500761cb29SGarrett Wollman #endif
1519b50d902SRodney W. Grimes 	} ifaddr;
1529b50d902SRodney W. Grimes 	u_long ifaddraddr;
153f6719675SBill Fenner 	u_long ifaddrfound;
154f6719675SBill Fenner 	u_long ifnetfound;
1555da9f8faSJosef Karthauser 	u_long opackets;
1565da9f8faSJosef Karthauser 	u_long ipackets;
1575da9f8faSJosef Karthauser 	u_long obytes;
1585da9f8faSJosef Karthauser 	u_long ibytes;
1595da9f8faSJosef Karthauser 	u_long oerrors;
1605da9f8faSJosef Karthauser 	u_long ierrors;
1615da9f8faSJosef Karthauser 	u_long collisions;
1625da9f8faSJosef Karthauser 	short timer;
1635da9f8faSJosef Karthauser 	int drops;
164a97a9922SJulian Elischer 	struct sockaddr *sa = NULL;
1651b72e71cSDavid Greenman 	char name[32], tname[16];
1669b50d902SRodney W. Grimes 
1679b50d902SRodney W. Grimes 	if (ifnetaddr == 0) {
1689b50d902SRodney W. Grimes 		printf("ifnet: symbol not defined\n");
1699b50d902SRodney W. Grimes 		return;
1709b50d902SRodney W. Grimes 	}
1719b50d902SRodney W. Grimes 	if (interval) {
1729b50d902SRodney W. Grimes 		sidewaysintpr((unsigned)interval, ifnetaddr);
1739b50d902SRodney W. Grimes 		return;
1749b50d902SRodney W. Grimes 	}
1750e27dc05SGarrett Wollman 	if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
1769b50d902SRodney W. Grimes 		return;
1770e27dc05SGarrett Wollman 	ifnetaddr = (u_long)ifnethead.tqh_first;
1780e27dc05SGarrett Wollman 	if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
1790e27dc05SGarrett Wollman 		return;
1800e27dc05SGarrett Wollman 
1813cfac2c5SJohn Polstra 	if ((!sflag || iflag) && !pflag) {
182cc6a66f2SJulian Elischer 		printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
183e1e293a5SDavid Greenman 		       "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
184e1e293a5SDavid Greenman 		if (bflag)
185e1e293a5SDavid Greenman 			printf(" %10.10s","Ibytes");
186e1e293a5SDavid Greenman 		printf(" %8.8s %5.5s", "Opkts", "Oerrs");
187e1e293a5SDavid Greenman 		if (bflag)
188e1e293a5SDavid Greenman 			printf(" %10.10s","Obytes");
1899b50d902SRodney W. Grimes 		printf(" %5s", "Coll");
1909b50d902SRodney W. Grimes 		if (tflag)
1919b50d902SRodney W. Grimes 			printf(" %s", "Time");
1929b50d902SRodney W. Grimes 		if (dflag)
1939b50d902SRodney W. Grimes 			printf(" %s", "Drop");
1949b50d902SRodney W. Grimes 		putchar('\n');
195cfa1ca9dSYoshinobu Inoue 	}
1969b50d902SRodney W. Grimes 	ifaddraddr = 0;
1979b50d902SRodney W. Grimes 	while (ifnetaddr || ifaddraddr) {
1989b50d902SRodney W. Grimes 		struct sockaddr_in *sin;
199cfa1ca9dSYoshinobu Inoue #ifdef INET6
200cfa1ca9dSYoshinobu Inoue 		struct sockaddr_in6 *sin6;
201cfa1ca9dSYoshinobu Inoue #endif
2029b50d902SRodney W. Grimes 		register char *cp;
2039b50d902SRodney W. Grimes 		int n, m;
2049b50d902SRodney W. Grimes 
2059b50d902SRodney W. Grimes 		if (ifaddraddr == 0) {
206f6719675SBill Fenner 			ifnetfound = ifnetaddr;
2079b50d902SRodney W. Grimes 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) ||
2081b72e71cSDavid Greenman 			    kread((u_long)ifnet.if_name, tname, 16))
2099b50d902SRodney W. Grimes 				return;
2101b72e71cSDavid Greenman 			tname[15] = '\0';
2110e27dc05SGarrett Wollman 			ifnetaddr = (u_long)ifnet.if_link.tqe_next;
2121b72e71cSDavid Greenman 			snprintf(name, 32, "%s%d", tname, ifnet.if_unit);
2131b72e71cSDavid Greenman 			if (interface != 0 && (strcmp(name, interface) != 0))
2149b50d902SRodney W. Grimes 				continue;
2159b50d902SRodney W. Grimes 			cp = index(name, '\0');
216cfa1ca9dSYoshinobu Inoue 
217cfa1ca9dSYoshinobu Inoue 			if (pfunc) {
218cfa1ca9dSYoshinobu Inoue 				(*pfunc)(name);
219cfa1ca9dSYoshinobu Inoue 				continue;
220cfa1ca9dSYoshinobu Inoue 			}
221cfa1ca9dSYoshinobu Inoue 
2229b50d902SRodney W. Grimes 			if ((ifnet.if_flags&IFF_UP) == 0)
2239b50d902SRodney W. Grimes 				*cp++ = '*';
2249b50d902SRodney W. Grimes 			*cp = '\0';
22515244cd5SGarrett Wollman 			ifaddraddr = (u_long)ifnet.if_addrhead.tqh_first;
2269b50d902SRodney W. Grimes 		}
2277d56c0eeSAlexander Langer 		printf("%-5.5s %-5lu ", name, ifnet.if_mtu);
228f6719675SBill Fenner 		ifaddrfound = ifaddraddr;
2295da9f8faSJosef Karthauser 
2305da9f8faSJosef Karthauser 		/*
2315da9f8faSJosef Karthauser 		 * Get the interface stats.  These may get
2325da9f8faSJosef Karthauser 		 * overriden below on a per-interface basis.
2335da9f8faSJosef Karthauser 		 */
2345da9f8faSJosef Karthauser 		opackets = ifnet.if_opackets;
2355da9f8faSJosef Karthauser 		ipackets = ifnet.if_ipackets;
2365da9f8faSJosef Karthauser 		obytes = ifnet.if_obytes;
2375da9f8faSJosef Karthauser 		ibytes = ifnet.if_ibytes;
2385da9f8faSJosef Karthauser 		oerrors = ifnet.if_oerrors;
2395da9f8faSJosef Karthauser 		ierrors = ifnet.if_ierrors;
2405da9f8faSJosef Karthauser 		collisions = ifnet.if_collisions;
2415da9f8faSJosef Karthauser 		timer = ifnet.if_timer;
2425da9f8faSJosef Karthauser 		drops = ifnet.if_snd.ifq_drops;
2435da9f8faSJosef Karthauser 
2449b50d902SRodney W. Grimes 		if (ifaddraddr == 0) {
245cc6a66f2SJulian Elischer 			printf("%-13.13s ", "none");
2469b50d902SRodney W. Grimes 			printf("%-15.15s ", "none");
2479b50d902SRodney W. Grimes 		} else {
2489b50d902SRodney W. Grimes 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
2499b50d902SRodney W. Grimes 				ifaddraddr = 0;
2509b50d902SRodney W. Grimes 				continue;
2519b50d902SRodney W. Grimes 			}
2529b50d902SRodney W. Grimes #define CP(x) ((char *)(x))
2539b50d902SRodney W. Grimes 			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
254a97a9922SJulian Elischer 				CP(&ifaddr);
255a97a9922SJulian Elischer 			sa = (struct sockaddr *)cp;
2569b50d902SRodney W. Grimes 			switch (sa->sa_family) {
2579b50d902SRodney W. Grimes 			case AF_UNSPEC:
258cc6a66f2SJulian Elischer 				printf("%-13.13s ", "none");
2599b50d902SRodney W. Grimes 				printf("%-15.15s ", "none");
2609b50d902SRodney W. Grimes 				break;
2619b50d902SRodney W. Grimes 			case AF_INET:
2629b50d902SRodney W. Grimes 				sin = (struct sockaddr_in *)sa;
2639b50d902SRodney W. Grimes #ifdef notdef
2649b50d902SRodney W. Grimes 				/* can't use inet_makeaddr because kernel
2659b50d902SRodney W. Grimes 				 * keeps nets unshifted.
2669b50d902SRodney W. Grimes 				 */
2679b50d902SRodney W. Grimes 				in = inet_makeaddr(ifaddr.in.ia_subnet,
2689b50d902SRodney W. Grimes 					INADDR_ANY);
26909a67ffaSStefan Eßer 				printf("%-13.13s ", netname(in.s_addr,
2709b50d902SRodney W. Grimes 				    ifaddr.in.ia_subnetmask));
2719b50d902SRodney W. Grimes #else
27209a67ffaSStefan Eßer 				printf("%-13.13s ",
2739b50d902SRodney W. Grimes 				    netname(htonl(ifaddr.in.ia_subnet),
2749b50d902SRodney W. Grimes 				    ifaddr.in.ia_subnetmask));
2759b50d902SRodney W. Grimes #endif
2769b50d902SRodney W. Grimes 				printf("%-15.15s ",
2779b50d902SRodney W. Grimes 				    routename(sin->sin_addr.s_addr));
2789b50d902SRodney W. Grimes 				break;
279cfa1ca9dSYoshinobu Inoue #ifdef INET6
280cfa1ca9dSYoshinobu Inoue 			case AF_INET6:
281cfa1ca9dSYoshinobu Inoue 				sin6 = (struct sockaddr_in6 *)sa;
282cfa1ca9dSYoshinobu Inoue 				printf("%-11.11s ",
283cfa1ca9dSYoshinobu Inoue 				       netname6(&ifaddr.in6.ia_addr,
284cfa1ca9dSYoshinobu Inoue 						&ifaddr.in6.ia_prefixmask.sin6_addr));
285cfa1ca9dSYoshinobu Inoue 				printf("%-17.17s ",
286cfa1ca9dSYoshinobu Inoue 				    (char *)inet_ntop(AF_INET6,
287cfa1ca9dSYoshinobu Inoue 					&sin6->sin6_addr,
288cfa1ca9dSYoshinobu Inoue 					ntop_buf, sizeof(ntop_buf)));
289cfa1ca9dSYoshinobu Inoue 				break;
290cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
291cc6a66f2SJulian Elischer 			case AF_IPX:
292cc6a66f2SJulian Elischer 				{
293cc6a66f2SJulian Elischer 				struct sockaddr_ipx *sipx =
294cc6a66f2SJulian Elischer 					(struct sockaddr_ipx *)sa;
295cc6a66f2SJulian Elischer 				u_long net;
296cc6a66f2SJulian Elischer 				char netnum[10];
297cc6a66f2SJulian Elischer 
298cc6a66f2SJulian Elischer 				*(union ipx_net *) &net = sipx->sipx_addr.x_net;
29922694ebaSBruce Evans 				sprintf(netnum, "%lx", (u_long)ntohl(net));
300cc6a66f2SJulian Elischer 				printf("ipx:%-8s  ", netnum);
301cc6a66f2SJulian Elischer /*				printf("ipx:%-8s ", netname(net, 0L)); */
302cc6a66f2SJulian Elischer 				printf("%-15s ",
303cc6a66f2SJulian Elischer 				    ipx_phost((struct sockaddr *)sipx));
304cc6a66f2SJulian Elischer 				}
305cc6a66f2SJulian Elischer 				break;
3066ffcfd6cSJulian Elischer 
3076ffcfd6cSJulian Elischer 			case AF_APPLETALK:
308a8d37845SJulian Elischer 				printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
309a8d37845SJulian Elischer 				printf("%-9.9s  ",atalk_print(sa,0x0b) );
3106ffcfd6cSJulian Elischer 				break;
311cbc17e71SGarrett Wollman #ifdef NS
3129b50d902SRodney W. Grimes 			case AF_NS:
3139b50d902SRodney W. Grimes 				{
3149b50d902SRodney W. Grimes 				struct sockaddr_ns *sns =
3159b50d902SRodney W. Grimes 					(struct sockaddr_ns *)sa;
3169b50d902SRodney W. Grimes 				u_long net;
317cc6a66f2SJulian Elischer 				char netnum[10];
3189b50d902SRodney W. Grimes 
3199b50d902SRodney W. Grimes 				*(union ns_net *) &net = sns->sns_addr.x_net;
3209b50d902SRodney W. Grimes 				sprintf(netnum, "%lxH", ntohl(net));
3219b50d902SRodney W. Grimes 				upHex(netnum);
3229b50d902SRodney W. Grimes 				printf("ns:%-8s ", netnum);
3239b50d902SRodney W. Grimes 				printf("%-15s ",
3249b50d902SRodney W. Grimes 				    ns_phost((struct sockaddr *)sns));
3259b50d902SRodney W. Grimes 				}
3269b50d902SRodney W. Grimes 				break;
327cbc17e71SGarrett Wollman #endif
3289b50d902SRodney W. Grimes 			case AF_LINK:
3299b50d902SRodney W. Grimes 				{
3309b50d902SRodney W. Grimes 				struct sockaddr_dl *sdl =
3319b50d902SRodney W. Grimes 					(struct sockaddr_dl *)sa;
332cfa1ca9dSYoshinobu Inoue 				char linknum[10];
3339b50d902SRodney W. Grimes 				cp = (char *)LLADDR(sdl);
3349b50d902SRodney W. Grimes 				n = sdl->sdl_alen;
335cfa1ca9dSYoshinobu Inoue 				sprintf(linknum, "<Link#%d>", sdl->sdl_index);
336cfa1ca9dSYoshinobu Inoue 				m = printf("%-11.11s ", linknum);
3379b50d902SRodney W. Grimes 				}
3389b50d902SRodney W. Grimes 				goto hexprint;
3399b50d902SRodney W. Grimes 			default:
3409b50d902SRodney W. Grimes 				m = printf("(%d)", sa->sa_family);
3419b50d902SRodney W. Grimes 				for (cp = sa->sa_len + (char *)sa;
3429b50d902SRodney W. Grimes 					--cp > sa->sa_data && (*cp == 0);) {}
3439b50d902SRodney W. Grimes 				n = cp - sa->sa_data + 1;
3449b50d902SRodney W. Grimes 				cp = sa->sa_data;
3459b50d902SRodney W. Grimes 			hexprint:
3469b50d902SRodney W. Grimes 				while (--n >= 0)
347541f2562SDavid Greenman 					m += printf("%02x%c", *cp++ & 0xff,
348e54ca68cSJordan K. Hubbard 						    n > 0 ? ':' : ' ');
349cc6a66f2SJulian Elischer 				m = 30 - m;
3509b50d902SRodney W. Grimes 				while (m-- > 0)
3519b50d902SRodney W. Grimes 					putchar(' ');
3529b50d902SRodney W. Grimes 				break;
3539b50d902SRodney W. Grimes 			}
3545da9f8faSJosef Karthauser 
3555da9f8faSJosef Karthauser 			/*
3565da9f8faSJosef Karthauser 			 * Fixup the statistics for interfaces that
3575da9f8faSJosef Karthauser 			 * update stats for their network addresses
3585da9f8faSJosef Karthauser 			 */
3595da9f8faSJosef Karthauser 			if (sa->sa_family == AF_INET ||
3605da9f8faSJosef Karthauser 			    sa->sa_family == AF_INET6) {
3615da9f8faSJosef Karthauser 				opackets = ifaddr.in.ia_ifa.if_opackets;
3625da9f8faSJosef Karthauser 				ipackets = ifaddr.in.ia_ifa.if_ipackets;
3635da9f8faSJosef Karthauser 				obytes = ifaddr.in.ia_ifa.if_obytes;
3645da9f8faSJosef Karthauser 				ibytes = ifaddr.in.ia_ifa.if_ibytes;
3655da9f8faSJosef Karthauser 				oerrors = ierrors = 0;
3665da9f8faSJosef Karthauser 				collisions = timer = drops = 0;
3675da9f8faSJosef Karthauser 			}
3685da9f8faSJosef Karthauser 
36915244cd5SGarrett Wollman 			ifaddraddr = (u_long)ifaddr.ifa.ifa_link.tqe_next;
3709b50d902SRodney W. Grimes 		}
3715da9f8faSJosef Karthauser 
3725da9f8faSJosef Karthauser 		printf("%8lu %5lu ", ipackets, ierrors);
373e1e293a5SDavid Greenman 		if (bflag)
3745da9f8faSJosef Karthauser 			printf("%10lu ", ibytes);
3755da9f8faSJosef Karthauser 		printf("%8lu %5lu ", opackets, oerrors);
376e1e293a5SDavid Greenman 		if (bflag)
3775da9f8faSJosef Karthauser 			printf("%10lu ", obytes);
3785da9f8faSJosef Karthauser 		printf("%5lu", collisions);
3799b50d902SRodney W. Grimes 		if (tflag)
3805da9f8faSJosef Karthauser 			printf(" %3d", timer);
3819b50d902SRodney W. Grimes 		if (dflag)
3825da9f8faSJosef Karthauser 			printf(" %3d", drops);
3839b50d902SRodney W. Grimes 		putchar('\n');
384f6719675SBill Fenner 		if (aflag && ifaddrfound) {
385f6719675SBill Fenner 			/*
386f6719675SBill Fenner 			 * Print family's multicast addresses
387f6719675SBill Fenner 			 */
388f6719675SBill Fenner 			u_long multiaddr;
38975fb8770SGarrett Wollman 			struct ifmultiaddr ifma;
39075fb8770SGarrett Wollman 			union {
39175fb8770SGarrett Wollman 				struct sockaddr sa;
39275fb8770SGarrett Wollman 				struct sockaddr_in in;
393cfa1ca9dSYoshinobu Inoue #ifdef INET6
394cfa1ca9dSYoshinobu Inoue 				struct sockaddr_in6 in6;
395cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
39675fb8770SGarrett Wollman 				struct sockaddr_dl dl;
39775fb8770SGarrett Wollman 			} msa;
39875fb8770SGarrett Wollman 			const char *fmt;
399f6719675SBill Fenner 
40075fb8770SGarrett Wollman 			for(multiaddr = (u_long)ifnet.if_multiaddrs.lh_first;
40175fb8770SGarrett Wollman 			    multiaddr;
40275fb8770SGarrett Wollman 			    multiaddr = (u_long)ifma.ifma_link.le_next) {
40375fb8770SGarrett Wollman 				if (kread(multiaddr, (char *)&ifma,
40475fb8770SGarrett Wollman 					  sizeof ifma))
405f6719675SBill Fenner 					break;
40675fb8770SGarrett Wollman 				if (kread((u_long)ifma.ifma_addr, (char *)&msa,
40775fb8770SGarrett Wollman 					  sizeof msa))
40875fb8770SGarrett Wollman 					break;
40975fb8770SGarrett Wollman 				if (msa.sa.sa_family != sa->sa_family)
41075fb8770SGarrett Wollman 					continue;
41175fb8770SGarrett Wollman 
41275fb8770SGarrett Wollman 				fmt = 0;
41375fb8770SGarrett Wollman 				switch (msa.sa.sa_family) {
41475fb8770SGarrett Wollman 				case AF_INET:
41575fb8770SGarrett Wollman 					fmt = routename(msa.in.sin_addr.s_addr);
41675fb8770SGarrett Wollman 					break;
417cfa1ca9dSYoshinobu Inoue #ifdef INET6
418cfa1ca9dSYoshinobu Inoue 				case AF_INET6:
419cfa1ca9dSYoshinobu Inoue 					printf("%23s %-19.19s(refs: %d)\n", "",
420cfa1ca9dSYoshinobu Inoue 					       inet_ntop(AF_INET6,
421cfa1ca9dSYoshinobu Inoue 							 &msa.in6.sin6_addr,
422cfa1ca9dSYoshinobu Inoue 							 ntop_buf,
423cfa1ca9dSYoshinobu Inoue 							 sizeof(ntop_buf)),
424cfa1ca9dSYoshinobu Inoue 					       ifma.ifma_refcount);
425cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
426f6719675SBill Fenner 				case AF_LINK:
427f6719675SBill Fenner 					switch (ifnet.if_type) {
428f6719675SBill Fenner 					case IFT_ETHER:
42975fb8770SGarrett Wollman 					case IFT_FDDI:
43075fb8770SGarrett Wollman 						fmt = ether_ntoa(
43175fb8770SGarrett Wollman 							(struct ether_addr *)
43275fb8770SGarrett Wollman 							LLADDR(&msa.dl));
43375fb8770SGarrett Wollman 						break;
434f6719675SBill Fenner 					}
435f6719675SBill Fenner 					break;
436f6719675SBill Fenner 				}
43775fb8770SGarrett Wollman 				if (fmt)
43875fb8770SGarrett Wollman 					printf("%23s %s\n", "", fmt);
439f6719675SBill Fenner 			}
440f6719675SBill Fenner 		}
4419b50d902SRodney W. Grimes 	}
4429b50d902SRodney W. Grimes }
4439b50d902SRodney W. Grimes 
4449b50d902SRodney W. Grimes #define	MAXIF	10
4459b50d902SRodney W. Grimes struct	iftot {
4469b50d902SRodney W. Grimes 	char	ift_name[16];		/* interface name */
447890bc2a5SDoug Rabson 	u_long	ift_ip;			/* input packets */
448890bc2a5SDoug Rabson 	u_long	ift_ie;			/* input errors */
449890bc2a5SDoug Rabson 	u_long	ift_op;			/* output packets */
450890bc2a5SDoug Rabson 	u_long	ift_oe;			/* output errors */
451890bc2a5SDoug Rabson 	u_long	ift_co;			/* collisions */
452e1e293a5SDavid Greenman 	u_int	ift_dr;			/* drops */
453890bc2a5SDoug Rabson 	u_long	ift_ib;			/* input bytes */
454890bc2a5SDoug Rabson 	u_long	ift_ob;			/* output bytes */
4559b50d902SRodney W. Grimes } iftot[MAXIF];
4569b50d902SRodney W. Grimes 
4579b50d902SRodney W. Grimes u_char	signalled;			/* set if alarm goes off "early" */
4589b50d902SRodney W. Grimes 
4599b50d902SRodney W. Grimes /*
4609b50d902SRodney W. Grimes  * Print a running summary of interface statistics.
4619b50d902SRodney W. Grimes  * Repeat display every interval seconds, showing statistics
4629b50d902SRodney W. Grimes  * collected over that interval.  Assumes that interval is non-zero.
4639b50d902SRodney W. Grimes  * First line printed at top of screen is always cumulative.
4640e27dc05SGarrett Wollman  * XXX - should be rewritten to use ifmib(4).
4659b50d902SRodney W. Grimes  */
4669b50d902SRodney W. Grimes static void
4679b50d902SRodney W. Grimes sidewaysintpr(interval, off)
4689b50d902SRodney W. Grimes 	unsigned interval;
4699b50d902SRodney W. Grimes 	u_long off;
4709b50d902SRodney W. Grimes {
4719b50d902SRodney W. Grimes 	struct ifnet ifnet;
4729b50d902SRodney W. Grimes 	u_long firstifnet;
4730e27dc05SGarrett Wollman 	struct ifnethead ifnethead;
4749b50d902SRodney W. Grimes 	register struct iftot *ip, *total;
4759b50d902SRodney W. Grimes 	register int line;
4769b50d902SRodney W. Grimes 	struct iftot *lastif, *sum, *interesting;
4770b87c915SDavid Greenman 	int oldmask, first;
4780b87c915SDavid Greenman 	u_long interesting_off;
4799b50d902SRodney W. Grimes 
4800e27dc05SGarrett Wollman 	if (kread(off, (char *)&ifnethead, sizeof ifnethead))
4819b50d902SRodney W. Grimes 		return;
4820e27dc05SGarrett Wollman 	firstifnet = (u_long)ifnethead.tqh_first;
4830e27dc05SGarrett Wollman 
4849b50d902SRodney W. Grimes 	lastif = iftot;
4859b50d902SRodney W. Grimes 	sum = iftot + MAXIF - 1;
4869b50d902SRodney W. Grimes 	total = sum - 1;
4870b87c915SDavid Greenman 	interesting = NULL;
4880b87c915SDavid Greenman 	interesting_off = 0;
4899b50d902SRodney W. Grimes 	for (off = firstifnet, ip = iftot; off;) {
4901b72e71cSDavid Greenman 		char name[16], tname[16];
4919b50d902SRodney W. Grimes 
4929b50d902SRodney W. Grimes 		if (kread(off, (char *)&ifnet, sizeof ifnet))
4939b50d902SRodney W. Grimes 			break;
4941b72e71cSDavid Greenman 		if (kread((u_long)ifnet.if_name, tname, 16))
4959b50d902SRodney W. Grimes 			break;
4961b72e71cSDavid Greenman 		tname[15] = '\0';
4971b72e71cSDavid Greenman 		snprintf(name, 16, "%s%d", tname, ifnet.if_unit);
4980b87c915SDavid Greenman 		if (interface && strcmp(name, interface) == 0) {
4999b50d902SRodney W. Grimes 			interesting = ip;
5000b87c915SDavid Greenman 			interesting_off = off;
5010b87c915SDavid Greenman 		}
5021b72e71cSDavid Greenman 		snprintf(ip->ift_name, 16, "(%s)", name);;
5039b50d902SRodney W. Grimes 		ip++;
5049b50d902SRodney W. Grimes 		if (ip >= iftot + MAXIF - 2)
5059b50d902SRodney W. Grimes 			break;
5060e27dc05SGarrett Wollman 		off = (u_long) ifnet.if_link.tqe_next;
5079b50d902SRodney W. Grimes 	}
5089b50d902SRodney W. Grimes 	lastif = ip;
5099b50d902SRodney W. Grimes 
5109b50d902SRodney W. Grimes 	(void)signal(SIGALRM, catchalarm);
5119b50d902SRodney W. Grimes 	signalled = NO;
5129b50d902SRodney W. Grimes 	(void)alarm(interval);
5139b50d902SRodney W. Grimes 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
5149b50d902SRodney W. Grimes 		ip->ift_ip = 0;
5159b50d902SRodney W. Grimes 		ip->ift_ie = 0;
5160b87c915SDavid Greenman 		ip->ift_ib = 0;
5179b50d902SRodney W. Grimes 		ip->ift_op = 0;
5189b50d902SRodney W. Grimes 		ip->ift_oe = 0;
5190b87c915SDavid Greenman 		ip->ift_ob = 0;
5209b50d902SRodney W. Grimes 		ip->ift_co = 0;
5219b50d902SRodney W. Grimes 		ip->ift_dr = 0;
5229b50d902SRodney W. Grimes 	}
5230b87c915SDavid Greenman 	first = 1;
5240b87c915SDavid Greenman banner:
5250b87c915SDavid Greenman 	printf("%17s %14s %16s", "input",
5260b87c915SDavid Greenman 	    interesting ? interesting->ift_name : "(Total)", "output");
5279b50d902SRodney W. Grimes 	putchar('\n');
5280b87c915SDavid Greenman 	printf("%10s %5s %10s %10s %5s %10s %5s",
5290b87c915SDavid Greenman 	    "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
5309b50d902SRodney W. Grimes 	if (dflag)
5319b50d902SRodney W. Grimes 		printf(" %5.5s", "drops");
5329b50d902SRodney W. Grimes 	putchar('\n');
5339b50d902SRodney W. Grimes 	fflush(stdout);
5349b50d902SRodney W. Grimes 	line = 0;
5359b50d902SRodney W. Grimes loop:
5360b87c915SDavid Greenman 	if (interesting != NULL) {
5370b87c915SDavid Greenman 		ip = interesting;
5380b87c915SDavid Greenman 		if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
5390b87c915SDavid Greenman 			printf("???\n");
5400b87c915SDavid Greenman 			exit(1);
5410b87c915SDavid Greenman 		};
5420b87c915SDavid Greenman 		if (!first) {
5437d56c0eeSAlexander Langer 			printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
5440b87c915SDavid Greenman 				ifnet.if_ipackets - ip->ift_ip,
5450b87c915SDavid Greenman 				ifnet.if_ierrors - ip->ift_ie,
5460b87c915SDavid Greenman 				ifnet.if_ibytes - ip->ift_ib,
5470b87c915SDavid Greenman 				ifnet.if_opackets - ip->ift_op,
5480b87c915SDavid Greenman 				ifnet.if_oerrors - ip->ift_oe,
5490b87c915SDavid Greenman 				ifnet.if_obytes - ip->ift_ob,
5500b87c915SDavid Greenman 				ifnet.if_collisions - ip->ift_co);
5510b87c915SDavid Greenman 			if (dflag)
5520b87c915SDavid Greenman 				printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
5530b87c915SDavid Greenman 		}
5540b87c915SDavid Greenman 		ip->ift_ip = ifnet.if_ipackets;
5550b87c915SDavid Greenman 		ip->ift_ie = ifnet.if_ierrors;
5560b87c915SDavid Greenman 		ip->ift_ib = ifnet.if_ibytes;
5570b87c915SDavid Greenman 		ip->ift_op = ifnet.if_opackets;
5580b87c915SDavid Greenman 		ip->ift_oe = ifnet.if_oerrors;
5590b87c915SDavid Greenman 		ip->ift_ob = ifnet.if_obytes;
5600b87c915SDavid Greenman 		ip->ift_co = ifnet.if_collisions;
5610b87c915SDavid Greenman 		ip->ift_dr = ifnet.if_snd.ifq_drops;
5620b87c915SDavid Greenman 	} else {
5639b50d902SRodney W. Grimes 		sum->ift_ip = 0;
5649b50d902SRodney W. Grimes 		sum->ift_ie = 0;
5650b87c915SDavid Greenman 		sum->ift_ib = 0;
5669b50d902SRodney W. Grimes 		sum->ift_op = 0;
5679b50d902SRodney W. Grimes 		sum->ift_oe = 0;
5680b87c915SDavid Greenman 		sum->ift_ob = 0;
5699b50d902SRodney W. Grimes 		sum->ift_co = 0;
5709b50d902SRodney W. Grimes 		sum->ift_dr = 0;
5719b50d902SRodney W. Grimes 		for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
5729b50d902SRodney W. Grimes 			if (kread(off, (char *)&ifnet, sizeof ifnet)) {
5739b50d902SRodney W. Grimes 				off = 0;
5749b50d902SRodney W. Grimes 				continue;
5759b50d902SRodney W. Grimes 			}
5760b87c915SDavid Greenman 			sum->ift_ip += ifnet.if_ipackets;
5770b87c915SDavid Greenman 			sum->ift_ie += ifnet.if_ierrors;
5780b87c915SDavid Greenman 			sum->ift_ib += ifnet.if_ibytes;
5790b87c915SDavid Greenman 			sum->ift_op += ifnet.if_opackets;
5800b87c915SDavid Greenman 			sum->ift_oe += ifnet.if_oerrors;
5810b87c915SDavid Greenman 			sum->ift_ob += ifnet.if_obytes;
5820b87c915SDavid Greenman 			sum->ift_co += ifnet.if_collisions;
5830b87c915SDavid Greenman 			sum->ift_dr += ifnet.if_snd.ifq_drops;
5840e27dc05SGarrett Wollman 			off = (u_long) ifnet.if_link.tqe_next;
5859b50d902SRodney W. Grimes 		}
5860b87c915SDavid Greenman 		if (!first) {
587890bc2a5SDoug Rabson 			printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
5889b50d902SRodney W. Grimes 				sum->ift_ip - total->ift_ip,
5890b87c915SDavid Greenman 				sum->ift_ie - total->ift_ie,
5900b87c915SDavid Greenman 				sum->ift_ib - total->ift_ib,
5919b50d902SRodney W. Grimes 				sum->ift_op - total->ift_op,
5920b87c915SDavid Greenman 				sum->ift_oe - total->ift_oe,
5930b87c915SDavid Greenman 				sum->ift_ob - total->ift_ob,
5940b87c915SDavid Greenman 				sum->ift_co - total->ift_co);
5959b50d902SRodney W. Grimes 			if (dflag)
5963aa80b1dSDavid Greenman 				printf(" %5u", sum->ift_dr - total->ift_dr);
5979b50d902SRodney W. Grimes 		}
5989b50d902SRodney W. Grimes 		*total = *sum;
5990b87c915SDavid Greenman 	}
6000b87c915SDavid Greenman 	if (!first)
6019b50d902SRodney W. Grimes 		putchar('\n');
6029b50d902SRodney W. Grimes 	fflush(stdout);
6039b50d902SRodney W. Grimes 	oldmask = sigblock(sigmask(SIGALRM));
6049b50d902SRodney W. Grimes 	if (! signalled) {
6059b50d902SRodney W. Grimes 		sigpause(0);
6069b50d902SRodney W. Grimes 	}
6079b50d902SRodney W. Grimes 	sigsetmask(oldmask);
6089b50d902SRodney W. Grimes 	signalled = NO;
6099b50d902SRodney W. Grimes 	(void)alarm(interval);
6100b87c915SDavid Greenman 	line++;
6110b87c915SDavid Greenman 	first = 0;
6129b50d902SRodney W. Grimes 	if (line == 21)
6139b50d902SRodney W. Grimes 		goto banner;
6140b87c915SDavid Greenman 	else
6159b50d902SRodney W. Grimes 		goto loop;
6169b50d902SRodney W. Grimes 	/*NOTREACHED*/
6179b50d902SRodney W. Grimes }
6189b50d902SRodney W. Grimes 
6199b50d902SRodney W. Grimes /*
6209b50d902SRodney W. Grimes  * Called if an interval expires before sidewaysintpr has completed a loop.
6219b50d902SRodney W. Grimes  * Sets a flag to not wait for the alarm.
6229b50d902SRodney W. Grimes  */
6239b50d902SRodney W. Grimes static void
6249b50d902SRodney W. Grimes catchalarm(signo)
6259b50d902SRodney W. Grimes 	int signo;
6269b50d902SRodney W. Grimes {
6279b50d902SRodney W. Grimes 	signalled = YES;
6289b50d902SRodney W. Grimes }
629