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