Home | History | Annotate | Line # | Download | only in netstat
if.c revision 1.36
      1  1.36    itojun /*	$NetBSD: if.c,v 1.36 2000/01/25 15:56:55 itojun Exp $	*/
      2  1.13   thorpej 
      3   1.1       cgd /*
      4   1.8   mycroft  * Copyright (c) 1983, 1988, 1993
      5   1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.25     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.13   thorpej #if 0
     39  1.13   thorpej static char sccsid[] = "from: @(#)if.c	8.2 (Berkeley) 2/21/94";
     40  1.13   thorpej #else
     41  1.36    itojun __RCSID("$NetBSD: if.c,v 1.36 2000/01/25 15:56:55 itojun Exp $");
     42  1.13   thorpej #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/types.h>
     46   1.8   mycroft #include <sys/protosw.h>
     47   1.1       cgd #include <sys/socket.h>
     48   1.1       cgd 
     49   1.1       cgd #include <net/if.h>
     50   1.1       cgd #include <net/if_dl.h>
     51  1.20   thorpej #include <net/if_types.h>
     52   1.1       cgd #include <netinet/in.h>
     53   1.1       cgd #include <netinet/in_var.h>
     54   1.1       cgd #include <netns/ns.h>
     55   1.1       cgd #include <netns/ns_if.h>
     56   1.1       cgd #include <netiso/iso.h>
     57   1.1       cgd #include <netiso/iso_var.h>
     58   1.8   mycroft #include <arpa/inet.h>
     59   1.1       cgd 
     60   1.8   mycroft #include <signal.h>
     61   1.1       cgd #include <stdio.h>
     62   1.8   mycroft #include <string.h>
     63   1.8   mycroft #include <unistd.h>
     64  1.34    itojun #include <netdb.h>
     65   1.8   mycroft 
     66   1.8   mycroft #include "netstat.h"
     67   1.1       cgd 
     68   1.1       cgd #define	YES	1
     69   1.1       cgd #define	NO	0
     70   1.1       cgd 
     71   1.8   mycroft static void sidewaysintpr __P((u_int, u_long));
     72   1.8   mycroft static void catchalarm __P((int));
     73   1.1       cgd 
     74  1.32    itojun #ifdef INET6
     75  1.34    itojun char *netname6 __P((struct sockaddr_in6 *, struct in6_addr *));
     76  1.32    itojun #endif
     77  1.32    itojun 
     78   1.1       cgd /*
     79   1.1       cgd  * Print a description of the network interfaces.
     80  1.15   thorpej  * NOTE: ifnetaddr is the location of the kernel global "ifnet",
     81  1.15   thorpej  * which is a TAILQ_HEAD.
     82   1.1       cgd  */
     83   1.8   mycroft void
     84  1.34    itojun intpr(interval, ifnetaddr, pfunc)
     85   1.1       cgd 	int interval;
     86   1.7       cgd 	u_long ifnetaddr;
     87  1.34    itojun 	void (*pfunc)(char *);
     88   1.1       cgd {
     89   1.1       cgd 	struct ifnet ifnet;
     90   1.1       cgd 	union {
     91   1.1       cgd 		struct ifaddr ifa;
     92   1.1       cgd 		struct in_ifaddr in;
     93  1.32    itojun #ifdef INET6
     94  1.32    itojun 		struct in6_ifaddr in6;
     95  1.32    itojun #endif /* INET6 */
     96   1.1       cgd 		struct ns_ifaddr ns;
     97   1.1       cgd 		struct iso_ifaddr iso;
     98   1.1       cgd 	} ifaddr;
     99   1.7       cgd 	u_long ifaddraddr;
    100   1.1       cgd 	struct sockaddr *sa;
    101  1.15   thorpej 	struct ifnet_head ifhead;	/* TAILQ_HEAD */
    102  1.15   thorpej 	char name[IFNAMSIZ];
    103  1.34    itojun #ifdef INET6
    104  1.34    itojun 	char hbuf[NI_MAXHOST];		/* for getnameinfo() */
    105  1.34    itojun #ifdef KAME_SCOPEID
    106  1.34    itojun 	const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
    107  1.34    itojun #else
    108  1.34    itojun 	const int niflag = NI_NUMERICHOST;
    109  1.34    itojun #endif
    110  1.34    itojun #endif
    111   1.1       cgd 
    112   1.1       cgd 	if (ifnetaddr == 0) {
    113   1.1       cgd 		printf("ifnet: symbol not defined\n");
    114   1.1       cgd 		return;
    115   1.1       cgd 	}
    116   1.1       cgd 	if (interval) {
    117   1.1       cgd 		sidewaysintpr((unsigned)interval, ifnetaddr);
    118   1.1       cgd 		return;
    119   1.1       cgd 	}
    120  1.15   thorpej 
    121  1.15   thorpej 	/*
    122  1.15   thorpej 	 * Find the pointer to the first ifnet structure.  Replace
    123  1.15   thorpej 	 * the pointer to the TAILQ_HEAD with the actual pointer
    124  1.15   thorpej 	 * to the first list element.
    125  1.15   thorpej 	 */
    126  1.15   thorpej 	if (kread(ifnetaddr, (char *)&ifhead, sizeof ifhead))
    127   1.8   mycroft 		return;
    128  1.15   thorpej 	ifnetaddr = (u_long)ifhead.tqh_first;
    129  1.15   thorpej 
    130  1.34    itojun 	if (!sflag & !pflag) {
    131  1.34    itojun 		if (bflag) {
    132  1.34    itojun 			printf("%-5.5s %-5.5s %-13.13s %-17.17s "
    133  1.34    itojun 			       "%10.10s %10.10s",
    134  1.34    itojun 			       "Name", "Mtu", "Network", "Address",
    135  1.34    itojun 			       "Ibytes", "Obytes");
    136  1.34    itojun 		} else {
    137  1.34    itojun 			printf("%-5.5s %-5.5s %-13.13s %-17.17s "
    138  1.34    itojun 			       "%8.8s %5.5s %8.8s %5.5s %5.5s",
    139  1.34    itojun 			       "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
    140  1.34    itojun 			       "Opkts", "Oerrs", "Colls");
    141  1.34    itojun 		}
    142  1.34    itojun 		if (tflag)
    143  1.34    itojun 			printf(" %4.4s", "Time");
    144  1.34    itojun 		if (dflag)
    145  1.34    itojun 			printf(" %5.5s", "Drops");
    146  1.34    itojun 		putchar('\n');
    147  1.26       kml 	}
    148   1.1       cgd 	ifaddraddr = 0;
    149   1.1       cgd 	while (ifnetaddr || ifaddraddr) {
    150   1.1       cgd 		struct sockaddr_in *sin;
    151  1.32    itojun #ifdef INET6
    152  1.32    itojun 		struct sockaddr_in6 *sin6;
    153  1.32    itojun #endif /* INET6 */
    154  1.25     lukem 		char *cp;
    155   1.1       cgd 		int n, m;
    156   1.1       cgd 
    157   1.1       cgd 		if (ifaddraddr == 0) {
    158  1.15   thorpej 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
    159   1.8   mycroft 				return;
    160  1.25     lukem 			memmove(name, ifnet.if_xname, IFNAMSIZ);
    161  1.15   thorpej 			name[IFNAMSIZ - 1] = '\0';	/* sanity */
    162  1.10   mycroft 			ifnetaddr = (u_long)ifnet.if_list.tqe_next;
    163  1.16   thorpej 			if (interface != 0 && strcmp(name, interface) != 0)
    164   1.1       cgd 				continue;
    165  1.25     lukem 			cp = strchr(name, '\0');
    166  1.34    itojun 
    167  1.34    itojun 			if (pfunc) {
    168  1.34    itojun 				(*pfunc)(name);
    169  1.34    itojun 				continue;
    170  1.34    itojun 			}
    171  1.34    itojun 
    172  1.10   mycroft 			if ((ifnet.if_flags & IFF_UP) == 0)
    173   1.1       cgd 				*cp++ = '*';
    174   1.1       cgd 			*cp = '\0';
    175  1.10   mycroft 			ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first;
    176   1.1       cgd 		}
    177  1.33    bouyer 		printf("%-5.5s %-5llu ", name,
    178  1.33    bouyer 		    (unsigned long long)ifnet.if_mtu);
    179   1.1       cgd 		if (ifaddraddr == 0) {
    180  1.21  christos 			printf("%-13.13s ", "none");
    181  1.23     mikel 			printf("%-17.17s ", "none");
    182   1.1       cgd 		} else {
    183  1.20   thorpej 			char hexsep = '.';		/* for hexprint */
    184  1.20   thorpej 			const char *hexfmt = "%x%c";	/* for hexprint */
    185   1.8   mycroft 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
    186   1.8   mycroft 				ifaddraddr = 0;
    187   1.8   mycroft 				continue;
    188   1.8   mycroft 			}
    189   1.1       cgd #define CP(x) ((char *)(x))
    190   1.8   mycroft 			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
    191   1.1       cgd 				CP(&ifaddr); sa = (struct sockaddr *)cp;
    192   1.1       cgd 			switch (sa->sa_family) {
    193   1.1       cgd 			case AF_UNSPEC:
    194  1.21  christos 				printf("%-13.13s ", "none");
    195  1.12       jtc 				printf("%-17.17s ", "none");
    196   1.1       cgd 				break;
    197   1.1       cgd 			case AF_INET:
    198   1.1       cgd 				sin = (struct sockaddr_in *)sa;
    199   1.1       cgd #ifdef notdef
    200  1.28       mrg 				/*
    201  1.28       mrg 				 * can't use inet_makeaddr because kernel
    202   1.1       cgd 				 * keeps nets unshifted.
    203   1.1       cgd 				 */
    204   1.1       cgd 				in = inet_makeaddr(ifaddr.in.ia_subnet,
    205   1.1       cgd 					INADDR_ANY);
    206  1.21  christos 				printf("%-13.13s ", netname(in.s_addr,
    207   1.8   mycroft 				    ifaddr.in.ia_subnetmask));
    208   1.1       cgd #else
    209  1.36    itojun 				cp = netname(ifaddr.in.ia_subnet,
    210  1.36    itojun 				    ifaddr.in.ia_subnetmask);
    211  1.36    itojun 				if (vflag)
    212  1.36    itojun 					n = strlen(cp) < 13 ? 13 : strlen(cp);
    213  1.36    itojun 				else
    214  1.36    itojun 					n = 13;
    215  1.36    itojun 				printf("%-*.*s ", n, n, cp);
    216   1.1       cgd #endif
    217  1.36    itojun 				cp = routename(sin->sin_addr.s_addr);
    218  1.36    itojun 				if (vflag)
    219  1.36    itojun 					n = strlen(cp) < 17 ? 17 : strlen(cp);
    220  1.36    itojun 				else
    221  1.36    itojun 					n = 17;
    222  1.36    itojun 				printf("%-*.*s ", n, n, cp);
    223  1.10   mycroft 				if (aflag) {
    224  1.10   mycroft 					u_long multiaddr;
    225  1.10   mycroft 					struct in_multi inm;
    226  1.10   mycroft 
    227  1.28       mrg 					multiaddr = (u_long)
    228  1.28       mrg 					    ifaddr.in.ia_multiaddrs.lh_first;
    229  1.10   mycroft 					while (multiaddr != 0) {
    230  1.10   mycroft 						kread(multiaddr, (char *)&inm,
    231  1.28       mrg 						   sizeof inm);
    232  1.23     mikel 						printf("\n%25s %-17.17s ", "",
    233  1.28       mrg 						   routename(
    234  1.28       mrg 						      inm.inm_addr.s_addr));
    235  1.28       mrg 						multiaddr =
    236  1.28       mrg 						   (u_long)inm.inm_list.le_next;
    237  1.10   mycroft 					}
    238  1.10   mycroft 				}
    239   1.1       cgd 				break;
    240  1.32    itojun #ifdef INET6
    241  1.32    itojun 			case AF_INET6:
    242  1.32    itojun 				sin6 = (struct sockaddr_in6 *)sa;
    243  1.34    itojun 				cp = netname6(&ifaddr.in6.ia_addr,
    244  1.34    itojun 					&ifaddr.in6.ia_prefixmask.sin6_addr);
    245  1.34    itojun 				if (vflag)
    246  1.34    itojun 					n = strlen(cp) < 13 ? 13 : strlen(cp);
    247  1.34    itojun 				else
    248  1.34    itojun 					n = 13;
    249  1.34    itojun 				printf("%-*.*s ", n, n, cp);
    250  1.34    itojun #if 0 /* KAME_SCOPEID: don't do it twice */
    251  1.34    itojun 				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    252  1.34    itojun 					sin6->sin6_scope_id =
    253  1.34    itojun 						ntohs(*(u_int16_t *)
    254  1.34    itojun 						  &sin6->sin6_addr.s6_addr[2]);
    255  1.34    itojun 					sin6->sin6_addr.s6_addr[2] = 0;
    256  1.34    itojun 					sin6->sin6_addr.s6_addr[3] = 0;
    257  1.34    itojun 				}
    258  1.34    itojun #endif
    259  1.34    itojun 				if (getnameinfo((struct sockaddr *)sin6,
    260  1.34    itojun 						sin6->sin6_len,
    261  1.34    itojun 						hbuf, sizeof(hbuf), NULL, 0,
    262  1.34    itojun 						niflag) != 0) {
    263  1.34    itojun 					cp = "?";
    264  1.34    itojun 				} else
    265  1.34    itojun 					cp = hbuf;
    266  1.34    itojun 				if (vflag)
    267  1.34    itojun 					n = strlen(cp) < 17 ? 17 : strlen(cp);
    268  1.34    itojun 				else
    269  1.34    itojun 					n = 17;
    270  1.34    itojun 				printf("%-*.*s ", n, n, cp);
    271  1.35    itojun 				if (aflag) {
    272  1.35    itojun 					u_long multiaddr;
    273  1.35    itojun 					struct in6_multi inm;
    274  1.35    itojun 
    275  1.35    itojun 					multiaddr = (u_long)
    276  1.35    itojun 					    ifaddr.in6.ia6_multiaddrs.lh_first;
    277  1.35    itojun 					while (multiaddr != 0) {
    278  1.35    itojun 						kread(multiaddr, (char *)&inm,
    279  1.35    itojun 						   sizeof inm);
    280  1.35    itojun 						inet_ntop(AF_INET6, &inm.in6m_addr,
    281  1.35    itojun 						    hbuf, sizeof(hbuf));
    282  1.35    itojun 						cp = hbuf;
    283  1.35    itojun 						if (vflag)
    284  1.35    itojun 						    n = strlen(cp) < 17
    285  1.35    itojun 							? 17 : strlen(cp);
    286  1.35    itojun 						else
    287  1.35    itojun 						    n = 17;
    288  1.35    itojun 						printf("\n%25s %-*.*s ", "",
    289  1.35    itojun 						    n, n, cp);
    290  1.35    itojun 						multiaddr =
    291  1.35    itojun 						   (u_long)inm.in6m_entry.le_next;
    292  1.35    itojun 					}
    293  1.35    itojun 				}
    294  1.32    itojun 				break;
    295  1.32    itojun #endif /*INET6*/
    296  1.30       mrg #ifndef SMALL
    297  1.21  christos 			case AF_APPLETALK:
    298  1.24  christos 				printf("atalk:%-7.7s ",
    299  1.21  christos 				       atalk_print(sa,0x10));
    300  1.24  christos 				printf("%-17.17s ", atalk_print(sa,0x0b));
    301  1.21  christos 				break;
    302   1.1       cgd 			case AF_NS:
    303   1.1       cgd 				{
    304   1.1       cgd 				struct sockaddr_ns *sns =
    305   1.1       cgd 					(struct sockaddr_ns *)sa;
    306   1.1       cgd 				u_long net;
    307   1.1       cgd 				char netnum[8];
    308   1.1       cgd 
    309  1.28       mrg 				*(union ns_net *)&net = sns->sns_addr.x_net;
    310  1.28       mrg 				(void)sprintf(netnum, "%xH",
    311  1.28       mrg 				    (u_int32_t)ntohl(net));
    312   1.1       cgd 				upHex(netnum);
    313  1.23     mikel 				printf("ns:%-10s ", netnum);
    314  1.23     mikel 				printf("%-17.17s ",
    315   1.8   mycroft 				    ns_phost((struct sockaddr *)sns));
    316   1.1       cgd 				}
    317   1.1       cgd 				break;
    318  1.30       mrg #endif
    319   1.1       cgd 			case AF_LINK:
    320   1.1       cgd 				{
    321   1.1       cgd 				struct sockaddr_dl *sdl =
    322   1.1       cgd 					(struct sockaddr_dl *)sa;
    323   1.1       cgd 				    cp = (char *)LLADDR(sdl);
    324  1.20   thorpej 				    if (sdl->sdl_type == IFT_FDDI
    325  1.20   thorpej 					|| sdl->sdl_type == IFT_ETHER)
    326  1.20   thorpej 					    hexsep = ':', hexfmt = "%02x%c";
    327   1.1       cgd 				    n = sdl->sdl_alen;
    328   1.1       cgd 				}
    329  1.22  christos 				m = printf("%-13.13s ", "<Link>");
    330   1.1       cgd 				goto hexprint;
    331   1.1       cgd 			default:
    332   1.1       cgd 				m = printf("(%d)", sa->sa_family);
    333   1.1       cgd 				for (cp = sa->sa_len + (char *)sa;
    334   1.1       cgd 					--cp > sa->sa_data && (*cp == 0);) {}
    335   1.1       cgd 				n = cp - sa->sa_data + 1;
    336   1.1       cgd 				cp = sa->sa_data;
    337   1.1       cgd 			hexprint:
    338   1.1       cgd 				while (--n >= 0)
    339  1.20   thorpej 					m += printf(hexfmt, *cp++ & 0xff,
    340  1.20   thorpej 						    n > 0 ? hexsep : ' ');
    341  1.22  christos 				m = 32 - m;
    342   1.1       cgd 				while (m-- > 0)
    343   1.1       cgd 					putchar(' ');
    344   1.1       cgd 				break;
    345   1.1       cgd 			}
    346  1.10   mycroft 			ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
    347   1.1       cgd 		}
    348  1.26       kml 		if (bflag) {
    349  1.33    bouyer 			printf("%10llu %10llu",
    350  1.33    bouyer 				(unsigned long long)ifnet.if_ibytes,
    351  1.33    bouyer 				(unsigned long long)ifnet.if_obytes);
    352  1.26       kml 		} else {
    353  1.33    bouyer 			printf("%8llu %5llu %8llu %5llu %5llu",
    354  1.33    bouyer 				(unsigned long long)ifnet.if_ipackets,
    355  1.33    bouyer 				(unsigned long long)ifnet.if_ierrors,
    356  1.33    bouyer 				(unsigned long long)ifnet.if_opackets,
    357  1.33    bouyer 				(unsigned long long)ifnet.if_oerrors,
    358  1.33    bouyer 				(unsigned long long)ifnet.if_collisions);
    359  1.26       kml 		}
    360   1.1       cgd 		if (tflag)
    361  1.31   mycroft 			printf(" %4d", ifnet.if_timer);
    362   1.1       cgd 		if (dflag)
    363  1.31   mycroft 			printf(" %5d", ifnet.if_snd.ifq_drops);
    364   1.1       cgd 		putchar('\n');
    365   1.1       cgd 	}
    366   1.1       cgd }
    367   1.1       cgd 
    368  1.18       cgd #define	MAXIF	100
    369   1.1       cgd struct	iftot {
    370  1.15   thorpej 	char	ift_name[IFNAMSIZ];	/* interface name */
    371  1.34    itojun 	u_long	ift_ip;			/* input packets */
    372  1.34    itojun 	u_long	ift_ib;			/* input bytes */
    373  1.34    itojun 	u_long	ift_ie;			/* input errors */
    374  1.34    itojun 	u_long	ift_op;			/* output packets */
    375  1.34    itojun 	u_long	ift_ob;			/* output bytes */
    376  1.34    itojun 	u_long	ift_oe;			/* output errors */
    377  1.34    itojun 	u_long	ift_co;			/* collisions */
    378  1.34    itojun 	int	ift_dr;			/* drops */
    379   1.1       cgd } iftot[MAXIF];
    380   1.1       cgd 
    381   1.1       cgd u_char	signalled;			/* set if alarm goes off "early" */
    382   1.1       cgd 
    383   1.1       cgd /*
    384   1.1       cgd  * Print a running summary of interface statistics.
    385   1.1       cgd  * Repeat display every interval seconds, showing statistics
    386   1.1       cgd  * collected over that interval.  Assumes that interval is non-zero.
    387   1.1       cgd  * First line printed at top of screen is always cumulative.
    388   1.1       cgd  */
    389   1.8   mycroft static void
    390   1.1       cgd sidewaysintpr(interval, off)
    391   1.1       cgd 	unsigned interval;
    392   1.7       cgd 	u_long off;
    393   1.1       cgd {
    394   1.1       cgd 	struct ifnet ifnet;
    395   1.7       cgd 	u_long firstifnet;
    396  1.25     lukem 	struct iftot *ip, *total;
    397  1.25     lukem 	int line;
    398   1.1       cgd 	struct iftot *lastif, *sum, *interesting;
    399  1.15   thorpej 	struct ifnet_head ifhead;	/* TAILQ_HEAD */
    400   1.1       cgd 	int oldmask;
    401   1.1       cgd 
    402  1.15   thorpej 	/*
    403  1.15   thorpej 	 * Find the pointer to the first ifnet structure.  Replace
    404  1.15   thorpej 	 * the pointer to the TAILQ_HEAD with the actual pointer
    405  1.15   thorpej 	 * to the first list element.
    406  1.15   thorpej 	 */
    407  1.15   thorpej 	if (kread(off, (char *)&ifhead, sizeof ifhead))
    408   1.8   mycroft 		return;
    409  1.15   thorpej 	firstifnet = (u_long)ifhead.tqh_first;
    410  1.15   thorpej 
    411   1.1       cgd 	lastif = iftot;
    412   1.1       cgd 	sum = iftot + MAXIF - 1;
    413   1.1       cgd 	total = sum - 1;
    414  1.17       cgd 	interesting = (interface == NULL) ? iftot : NULL;
    415   1.1       cgd 	for (off = firstifnet, ip = iftot; off;) {
    416   1.8   mycroft 		if (kread(off, (char *)&ifnet, sizeof ifnet))
    417   1.8   mycroft 			break;
    418  1.25     lukem 		memset(ip->ift_name, 0, sizeof(ip->ift_name));
    419  1.31   mycroft 		snprintf(ip->ift_name, IFNAMSIZ, "%s", ifnet.if_xname);
    420  1.19   thorpej 		if (interface && strcmp(ifnet.if_xname, interface) == 0)
    421   1.1       cgd 			interesting = ip;
    422   1.1       cgd 		ip++;
    423   1.1       cgd 		if (ip >= iftot + MAXIF - 2)
    424   1.1       cgd 			break;
    425  1.10   mycroft 		off = (u_long)ifnet.if_list.tqe_next;
    426  1.17       cgd 	}
    427  1.17       cgd 	if (interesting == NULL) {
    428  1.17       cgd 		fprintf(stderr, "%s: %s: unknown interface\n",
    429  1.17       cgd 		    __progname, interface);
    430  1.17       cgd 		exit(1);
    431   1.1       cgd 	}
    432   1.1       cgd 	lastif = ip;
    433   1.1       cgd 
    434   1.1       cgd 	(void)signal(SIGALRM, catchalarm);
    435   1.1       cgd 	signalled = NO;
    436   1.1       cgd 	(void)alarm(interval);
    437   1.1       cgd banner:
    438  1.31   mycroft 	if (bflag)
    439  1.31   mycroft 		printf("%7.7s in %8.8s %6.6s out %5.5s",
    440  1.31   mycroft 		    interesting->ift_name, " ",
    441  1.31   mycroft 		    interesting->ift_name, " ");
    442  1.31   mycroft 	else
    443  1.31   mycroft 		printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
    444  1.31   mycroft 		    interesting->ift_name, " ",
    445  1.31   mycroft 		    interesting->ift_name, " ", " ");
    446  1.31   mycroft 	if (dflag)
    447  1.31   mycroft 		printf(" %5.5s", " ");
    448   1.1       cgd 	if (lastif - iftot > 0) {
    449  1.31   mycroft 		if (bflag)
    450  1.31   mycroft 			printf("  %7.7s in %8.8s %6.6s out %5.5s",
    451  1.31   mycroft 			    "total", " ", "total", " ");
    452  1.31   mycroft 		else
    453  1.31   mycroft 			printf("  %5.5s in %5.5s%5.5s out %5.5s %5.5s",
    454  1.31   mycroft 			    "total", " ", "total", " ", " ");
    455   1.1       cgd 		if (dflag)
    456  1.31   mycroft 			printf(" %5.5s", " ");
    457   1.1       cgd 	}
    458   1.1       cgd 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
    459   1.1       cgd 		ip->ift_ip = 0;
    460  1.26       kml 		ip->ift_ib = 0;
    461   1.1       cgd 		ip->ift_ie = 0;
    462   1.1       cgd 		ip->ift_op = 0;
    463  1.26       kml 		ip->ift_ob = 0;
    464   1.1       cgd 		ip->ift_oe = 0;
    465   1.1       cgd 		ip->ift_co = 0;
    466   1.1       cgd 		ip->ift_dr = 0;
    467   1.1       cgd 	}
    468   1.1       cgd 	putchar('\n');
    469  1.31   mycroft 	if (bflag)
    470  1.26       kml 		printf("%10.10s %8.8s %10.10s %5.5s",
    471  1.31   mycroft 		    "bytes", " ", "bytes", " ");
    472  1.31   mycroft 	else
    473  1.31   mycroft 		printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
    474  1.31   mycroft 		    "packets", "errs", "packets", "errs", "colls");
    475   1.1       cgd 	if (dflag)
    476  1.31   mycroft 		printf(" %5.5s", "drops");
    477  1.29      ross 	if (lastif - iftot > 0) {
    478  1.31   mycroft 		if (bflag)
    479  1.31   mycroft 			printf("  %10.10s %8.8s %10.10s %5.5s",
    480  1.31   mycroft 			    "bytes", " ", "bytes", " ");
    481  1.31   mycroft 		else
    482  1.31   mycroft 			printf("  %8.8s %5.5s %8.8s %5.5s %5.5s",
    483  1.31   mycroft 			    "packets", "errs", "packets", "errs", "colls");
    484  1.31   mycroft 		if (dflag)
    485  1.31   mycroft 			printf(" %5.5s", "drops");
    486  1.29      ross 	}
    487   1.1       cgd 	putchar('\n');
    488   1.1       cgd 	fflush(stdout);
    489   1.1       cgd 	line = 0;
    490   1.1       cgd loop:
    491   1.1       cgd 	sum->ift_ip = 0;
    492  1.26       kml 	sum->ift_ib = 0;
    493   1.1       cgd 	sum->ift_ie = 0;
    494   1.1       cgd 	sum->ift_op = 0;
    495  1.26       kml 	sum->ift_ob = 0;
    496   1.1       cgd 	sum->ift_oe = 0;
    497   1.1       cgd 	sum->ift_co = 0;
    498   1.1       cgd 	sum->ift_dr = 0;
    499   1.1       cgd 	for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
    500   1.8   mycroft 		if (kread(off, (char *)&ifnet, sizeof ifnet)) {
    501   1.8   mycroft 			off = 0;
    502   1.8   mycroft 			continue;
    503   1.8   mycroft 		}
    504   1.1       cgd 		if (ip == interesting) {
    505  1.26       kml 			if (bflag) {
    506  1.33    bouyer 				printf("%10llu %8.8s %10llu %5.5s",
    507  1.33    bouyer 				    (unsigned long long)(ifnet.if_ibytes -
    508  1.33    bouyer 					ip->ift_ib), " ",
    509  1.33    bouyer 				    (unsigned long long)(ifnet.if_obytes -
    510  1.33    bouyer 					ip->ift_ob), " ");
    511  1.26       kml 			} else {
    512  1.33    bouyer 				printf("%8llu %5llu %8llu %5llu %5llu",
    513  1.33    bouyer 				    (unsigned long long)
    514  1.33    bouyer 					(ifnet.if_ipackets - ip->ift_ip),
    515  1.33    bouyer 				    (unsigned long long)
    516  1.33    bouyer 					(ifnet.if_ierrors - ip->ift_ie),
    517  1.33    bouyer 				    (unsigned long long)
    518  1.33    bouyer 					(ifnet.if_opackets - ip->ift_op),
    519  1.33    bouyer 				    (unsigned long long)
    520  1.33    bouyer 					(ifnet.if_oerrors - ip->ift_oe),
    521  1.33    bouyer 				    (unsigned long long)
    522  1.33    bouyer 					(ifnet.if_collisions - ip->ift_co));
    523  1.26       kml 			}
    524   1.1       cgd 			if (dflag)
    525  1.33    bouyer 				printf(" %5llu",
    526  1.33    bouyer 				    (unsigned long long)
    527  1.33    bouyer 					(ifnet.if_snd.ifq_drops - ip->ift_dr));
    528   1.1       cgd 		}
    529   1.1       cgd 		ip->ift_ip = ifnet.if_ipackets;
    530  1.26       kml 		ip->ift_ib = ifnet.if_ibytes;
    531   1.1       cgd 		ip->ift_ie = ifnet.if_ierrors;
    532   1.1       cgd 		ip->ift_op = ifnet.if_opackets;
    533  1.26       kml 		ip->ift_ob = ifnet.if_obytes;
    534   1.1       cgd 		ip->ift_oe = ifnet.if_oerrors;
    535   1.1       cgd 		ip->ift_co = ifnet.if_collisions;
    536   1.1       cgd 		ip->ift_dr = ifnet.if_snd.ifq_drops;
    537   1.1       cgd 		sum->ift_ip += ip->ift_ip;
    538  1.26       kml 		sum->ift_ib += ip->ift_ib;
    539   1.1       cgd 		sum->ift_ie += ip->ift_ie;
    540   1.1       cgd 		sum->ift_op += ip->ift_op;
    541  1.26       kml 		sum->ift_ob += ip->ift_ob;
    542   1.1       cgd 		sum->ift_oe += ip->ift_oe;
    543   1.1       cgd 		sum->ift_co += ip->ift_co;
    544   1.1       cgd 		sum->ift_dr += ip->ift_dr;
    545  1.10   mycroft 		off = (u_long)ifnet.if_list.tqe_next;
    546   1.1       cgd 	}
    547   1.1       cgd 	if (lastif - iftot > 0) {
    548  1.26       kml 		if (bflag) {
    549  1.33    bouyer 			printf("  %10llu %8.8s %10llu %5.5s",
    550  1.33    bouyer 			    (unsigned long long)
    551  1.33    bouyer 				(sum->ift_ib - total->ift_ib), " ",
    552  1.33    bouyer 			    (unsigned long long)
    553  1.33    bouyer 				(sum->ift_ob - total->ift_ob), " ");
    554  1.26       kml 		} else {
    555  1.33    bouyer 			printf("  %8llu %5llu %8llu %5llu %5llu",
    556  1.33    bouyer 			    (unsigned long long)
    557  1.33    bouyer 				(sum->ift_ip - total->ift_ip),
    558  1.33    bouyer 			    (unsigned long long)
    559  1.33    bouyer 				(sum->ift_ie - total->ift_ie),
    560  1.33    bouyer 			    (unsigned long long)
    561  1.33    bouyer 				(sum->ift_op - total->ift_op),
    562  1.33    bouyer 			    (unsigned long long)
    563  1.33    bouyer 				(sum->ift_oe - total->ift_oe),
    564  1.33    bouyer 			    (unsigned long long)
    565  1.33    bouyer 				(sum->ift_co - total->ift_co));
    566  1.26       kml 		}
    567   1.1       cgd 		if (dflag)
    568  1.33    bouyer 			printf(" %5llu",
    569  1.33    bouyer 			    (unsigned long long)(sum->ift_dr - total->ift_dr));
    570   1.1       cgd 	}
    571   1.1       cgd 	*total = *sum;
    572   1.1       cgd 	putchar('\n');
    573   1.1       cgd 	fflush(stdout);
    574   1.1       cgd 	line++;
    575   1.1       cgd 	oldmask = sigblock(sigmask(SIGALRM));
    576   1.1       cgd 	if (! signalled) {
    577   1.1       cgd 		sigpause(0);
    578   1.1       cgd 	}
    579   1.1       cgd 	sigsetmask(oldmask);
    580   1.1       cgd 	signalled = NO;
    581   1.1       cgd 	(void)alarm(interval);
    582   1.1       cgd 	if (line == 21)
    583   1.1       cgd 		goto banner;
    584   1.1       cgd 	goto loop;
    585   1.1       cgd 	/*NOTREACHED*/
    586   1.1       cgd }
    587   1.1       cgd 
    588   1.1       cgd /*
    589   1.1       cgd  * Called if an interval expires before sidewaysintpr has completed a loop.
    590   1.1       cgd  * Sets a flag to not wait for the alarm.
    591   1.1       cgd  */
    592   1.8   mycroft static void
    593   1.8   mycroft catchalarm(signo)
    594   1.8   mycroft 	int signo;
    595   1.1       cgd {
    596  1.28       mrg 
    597   1.1       cgd 	signalled = YES;
    598   1.1       cgd }
    599