Home | History | Annotate | Line # | Download | only in netstat
if.c revision 1.6
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1983, 1988 Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.3  mycroft /*static char sccsid[] = "from: @(#)if.c	5.15 (Berkeley) 3/1/91";*/
     36  1.6      cgd static char rcsid[] = "$Id: if.c,v 1.6 1994/03/28 10:29:31 cgd Exp $";
     37  1.1      cgd #endif /* not lint */
     38  1.1      cgd 
     39  1.1      cgd #include <sys/types.h>
     40  1.1      cgd #include <sys/socket.h>
     41  1.1      cgd 
     42  1.1      cgd #include <net/if.h>
     43  1.1      cgd #include <net/if_dl.h>
     44  1.1      cgd #include <netinet/in.h>
     45  1.1      cgd #include <netinet/in_var.h>
     46  1.1      cgd 
     47  1.1      cgd #ifdef NS
     48  1.1      cgd #include <netns/ns.h>
     49  1.1      cgd #include <netns/ns_if.h>
     50  1.1      cgd #endif /* NS */
     51  1.1      cgd 
     52  1.1      cgd #ifdef ISO
     53  1.1      cgd #include <netiso/iso.h>
     54  1.1      cgd #include <netiso/iso_var.h>
     55  1.1      cgd #endif /* ISO */
     56  1.1      cgd 
     57  1.6      cgd #include <nlist.h>
     58  1.6      cgd #include <kvm.h>
     59  1.1      cgd #include <stdio.h>
     60  1.1      cgd #include <signal.h>
     61  1.1      cgd 
     62  1.1      cgd #define	YES	1
     63  1.1      cgd #define	NO	0
     64  1.1      cgd 
     65  1.1      cgd extern	int tflag;
     66  1.1      cgd extern	int dflag;
     67  1.1      cgd extern	int nflag;
     68  1.1      cgd extern	char *interface;
     69  1.1      cgd extern	int unit;
     70  1.1      cgd extern	char *routename(), *netname(), *ns_phost();
     71  1.1      cgd char *index();
     72  1.1      cgd 
     73  1.1      cgd /*
     74  1.1      cgd  * Print a description of the network interfaces.
     75  1.1      cgd  */
     76  1.1      cgd intpr(interval, ifnetaddr)
     77  1.1      cgd 	int interval;
     78  1.1      cgd 	off_t ifnetaddr;
     79  1.1      cgd {
     80  1.1      cgd 	struct ifnet ifnet;
     81  1.5  deraadt 	struct in_addr in;
     82  1.1      cgd 	union {
     83  1.1      cgd 		struct ifaddr ifa;
     84  1.1      cgd 		struct in_ifaddr in;
     85  1.1      cgd #ifdef NS
     86  1.1      cgd 		struct ns_ifaddr ns;
     87  1.1      cgd #endif
     88  1.1      cgd #ifdef ISO
     89  1.1      cgd 		struct iso_ifaddr iso;
     90  1.1      cgd #endif
     91  1.1      cgd 	} ifaddr;
     92  1.1      cgd 	off_t ifaddraddr;
     93  1.1      cgd 	struct sockaddr *sa;
     94  1.1      cgd 	char name[16];
     95  1.1      cgd 
     96  1.1      cgd 	if (ifnetaddr == 0) {
     97  1.1      cgd 		printf("ifnet: symbol not defined\n");
     98  1.1      cgd 		return;
     99  1.1      cgd 	}
    100  1.1      cgd 	if (interval) {
    101  1.1      cgd 		sidewaysintpr((unsigned)interval, ifnetaddr);
    102  1.1      cgd 		return;
    103  1.1      cgd 	}
    104  1.6      cgd 	kvm_read((void *)(long)ifnetaddr, (char *)&ifnetaddr, sizeof ifnetaddr);
    105  1.1      cgd 	printf("%-5.5s %-5.5s %-11.11s %-15.15s %8.8s %5.5s %8.8s %5.5s",
    106  1.1      cgd 		"Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
    107  1.1      cgd 		"Opkts", "Oerrs");
    108  1.1      cgd 	printf(" %5s", "Coll");
    109  1.1      cgd 	if (tflag)
    110  1.1      cgd 		printf(" %s", "Time");
    111  1.1      cgd 	if (dflag)
    112  1.1      cgd 		printf(" %s", "Drop");
    113  1.1      cgd 	putchar('\n');
    114  1.1      cgd 	ifaddraddr = 0;
    115  1.1      cgd 	while (ifnetaddr || ifaddraddr) {
    116  1.1      cgd 		struct sockaddr_in *sin;
    117  1.2     paul #ifdef ISO
    118  1.2     paul 		struct sockaddr_iso *siso;
    119  1.2     paul #endif
    120  1.1      cgd 		register char *cp;
    121  1.1      cgd 		int n, m;
    122  1.1      cgd 		struct in_addr inet_makeaddr();
    123  1.1      cgd 
    124  1.1      cgd 		if (ifaddraddr == 0) {
    125  1.6      cgd 			kvm_read((void *)(long)ifnetaddr, (char *)&ifnet,
    126  1.6      cgd 			    sizeof ifnet);
    127  1.6      cgd 			kvm_read(ifnet.if_name, name, 16);
    128  1.1      cgd 			name[15] = '\0';
    129  1.6      cgd 			ifnetaddr = (off_t)(long)ifnet.if_next;
    130  1.1      cgd 			if (interface != 0 &&
    131  1.1      cgd 			    (strcmp(name, interface) != 0 || unit != ifnet.if_unit))
    132  1.1      cgd 				continue;
    133  1.1      cgd 			cp = index(name, '\0');
    134  1.4      cgd 			cp += sprintf(cp, "%d", ifnet.if_unit);
    135  1.1      cgd 			if ((ifnet.if_flags&IFF_UP) == 0)
    136  1.1      cgd 				*cp++ = '*';
    137  1.1      cgd 			*cp = '\0';
    138  1.6      cgd 			ifaddraddr = (off_t)(long)ifnet.if_addrlist;
    139  1.1      cgd 		}
    140  1.1      cgd 		printf("%-5.5s %-5d ", name, ifnet.if_mtu);
    141  1.1      cgd 		if (ifaddraddr == 0) {
    142  1.1      cgd 			printf("%-11.11s ", "none");
    143  1.1      cgd 			printf("%-15.15s ", "none");
    144  1.1      cgd 		} else {
    145  1.6      cgd 			kvm_read((void *)(long)ifaddraddr, (char *)&ifaddr,
    146  1.6      cgd 			    sizeof ifaddr);
    147  1.1      cgd #define CP(x) ((char *)(x))
    148  1.6      cgd 			cp = (CP(ifaddr.ifa.ifa_addr) - CP((long)ifaddraddr)) +
    149  1.1      cgd 				CP(&ifaddr); sa = (struct sockaddr *)cp;
    150  1.1      cgd 			switch (sa->sa_family) {
    151  1.1      cgd 			case AF_UNSPEC:
    152  1.1      cgd 				printf("%-11.11s ", "none");
    153  1.1      cgd 				printf("%-15.15s ", "none");
    154  1.1      cgd 				break;
    155  1.1      cgd 			case AF_INET:
    156  1.1      cgd 				sin = (struct sockaddr_in *)sa;
    157  1.1      cgd #ifdef notdef
    158  1.1      cgd 				/* can't use inet_makeaddr because kernel
    159  1.1      cgd 				 * keeps nets unshifted.
    160  1.1      cgd 				 */
    161  1.1      cgd 				in = inet_makeaddr(ifaddr.in.ia_subnet,
    162  1.1      cgd 					INADDR_ANY);
    163  1.1      cgd 				printf("%-11.11s ", netname(in));
    164  1.1      cgd #else
    165  1.5  deraadt 				in.s_addr = htonl(ifaddr.in.ia_subnet);
    166  1.5  deraadt 
    167  1.1      cgd 				printf("%-11.11s ",
    168  1.5  deraadt 					netname(in, ifaddr.in.ia_subnetmask));
    169  1.1      cgd #endif
    170  1.1      cgd 				printf("%-15.15s ", routename(sin->sin_addr));
    171  1.1      cgd 				break;
    172  1.1      cgd #ifdef NS
    173  1.1      cgd 			case AF_NS:
    174  1.1      cgd 				{
    175  1.1      cgd 				struct sockaddr_ns *sns =
    176  1.1      cgd 					(struct sockaddr_ns *)sa;
    177  1.1      cgd 				u_long net;
    178  1.1      cgd 				char netnum[8];
    179  1.1      cgd 				char *ns_phost();
    180  1.1      cgd 
    181  1.1      cgd 				*(union ns_net *) &net = sns->sns_addr.x_net;
    182  1.1      cgd 		sprintf(netnum, "%lxH", ntohl(net));
    183  1.1      cgd 				upHex(netnum);
    184  1.1      cgd 				printf("ns:%-8s ", netnum);
    185  1.1      cgd 				printf("%-15s ", ns_phost(sns));
    186  1.1      cgd 				}
    187  1.1      cgd 				break;
    188  1.1      cgd #endif
    189  1.1      cgd 			case AF_LINK:
    190  1.1      cgd 				{
    191  1.1      cgd 				struct sockaddr_dl *sdl =
    192  1.1      cgd 					(struct sockaddr_dl *)sa;
    193  1.1      cgd 				    cp = (char *)LLADDR(sdl);
    194  1.1      cgd 				    n = sdl->sdl_alen;
    195  1.1      cgd 				}
    196  1.2     paul 				m = printf("%-11.11s ","<Link>");
    197  1.1      cgd 				goto hexprint;
    198  1.2     paul #ifdef ISO
    199  1.2     paul 			case AF_ISO:
    200  1.2     paul 				siso = (struct sockaddr_iso *)sa;
    201  1.2     paul 				if(nflag) {
    202  1.2     paul 					printf("%27s ",
    203  1.2     paul 						iso_ntoa(&siso->siso_addr));
    204  1.2     paul 				} else {
    205  1.2     paul 					/* This will probably truncate the */
    206  1.2     paul 					/* NSAP prefix */
    207  1.2     paul 					printf("%-11.11s ",
    208  1.2     paul 						iso_ntoa(&siso->siso_addr));
    209  1.2     paul 					printf("%-15.15s ",
    210  1.2     paul 						iso_idtoa(&siso->siso_addr));
    211  1.2     paul 				}
    212  1.2     paul 				break;
    213  1.2     paul #endif
    214  1.1      cgd 			default:
    215  1.1      cgd 				m = printf("(%d)", sa->sa_family);
    216  1.1      cgd 				for (cp = sa->sa_len + (char *)sa;
    217  1.1      cgd 					--cp > sa->sa_data && (*cp == 0);) {}
    218  1.1      cgd 				n = cp - sa->sa_data + 1;
    219  1.1      cgd 				cp = sa->sa_data;
    220  1.1      cgd 			hexprint:
    221  1.1      cgd 				while (--n >= 0)
    222  1.1      cgd 					m += printf("%x%c", *cp++ & 0xff,
    223  1.1      cgd 						    n > 0 ? '.' : ' ');
    224  1.1      cgd 				m = 28 - m;
    225  1.1      cgd 				while (m-- > 0)
    226  1.1      cgd 					putchar(' ');
    227  1.1      cgd 				break;
    228  1.1      cgd 			}
    229  1.6      cgd 			ifaddraddr = (off_t)(long)ifaddr.ifa.ifa_next;
    230  1.1      cgd 		}
    231  1.1      cgd 		printf("%8d %5d %8d %5d %5d",
    232  1.1      cgd 		    ifnet.if_ipackets, ifnet.if_ierrors,
    233  1.1      cgd 		    ifnet.if_opackets, ifnet.if_oerrors,
    234  1.1      cgd 		    ifnet.if_collisions);
    235  1.1      cgd 		if (tflag)
    236  1.1      cgd 			printf(" %3d", ifnet.if_timer);
    237  1.1      cgd 		if (dflag)
    238  1.1      cgd 			printf(" %3d", ifnet.if_snd.ifq_drops);
    239  1.1      cgd 		putchar('\n');
    240  1.1      cgd 	}
    241  1.1      cgd }
    242  1.1      cgd 
    243  1.1      cgd #define	MAXIF	10
    244  1.1      cgd struct	iftot {
    245  1.1      cgd 	char	ift_name[16];		/* interface name */
    246  1.1      cgd 	int	ift_ip;			/* input packets */
    247  1.1      cgd 	int	ift_ie;			/* input errors */
    248  1.1      cgd 	int	ift_op;			/* output packets */
    249  1.1      cgd 	int	ift_oe;			/* output errors */
    250  1.1      cgd 	int	ift_co;			/* collisions */
    251  1.1      cgd 	int	ift_dr;			/* drops */
    252  1.1      cgd } iftot[MAXIF];
    253  1.1      cgd 
    254  1.1      cgd u_char	signalled;			/* set if alarm goes off "early" */
    255  1.1      cgd 
    256  1.1      cgd /*
    257  1.1      cgd  * Print a running summary of interface statistics.
    258  1.1      cgd  * Repeat display every interval seconds, showing statistics
    259  1.1      cgd  * collected over that interval.  Assumes that interval is non-zero.
    260  1.1      cgd  * First line printed at top of screen is always cumulative.
    261  1.1      cgd  */
    262  1.1      cgd sidewaysintpr(interval, off)
    263  1.1      cgd 	unsigned interval;
    264  1.1      cgd 	off_t off;
    265  1.1      cgd {
    266  1.1      cgd 	struct ifnet ifnet;
    267  1.1      cgd 	off_t firstifnet;
    268  1.1      cgd 	register struct iftot *ip, *total;
    269  1.1      cgd 	register int line;
    270  1.1      cgd 	struct iftot *lastif, *sum, *interesting;
    271  1.1      cgd 	int oldmask;
    272  1.1      cgd 	void catchalarm();
    273  1.1      cgd 
    274  1.6      cgd 	kvm_read((void *)(long)off, (char *)&firstifnet, sizeof (off_t));
    275  1.1      cgd 	lastif = iftot;
    276  1.1      cgd 	sum = iftot + MAXIF - 1;
    277  1.1      cgd 	total = sum - 1;
    278  1.1      cgd 	interesting = iftot;
    279  1.1      cgd 	for (off = firstifnet, ip = iftot; off;) {
    280  1.1      cgd 		char *cp;
    281  1.1      cgd 
    282  1.6      cgd 		kvm_read((void *)(long)off, (char *)&ifnet, sizeof ifnet);
    283  1.1      cgd 		ip->ift_name[0] = '(';
    284  1.6      cgd 		kvm_read(ifnet.if_name, ip->ift_name + 1, 15);
    285  1.1      cgd 		if (interface && strcmp(ip->ift_name + 1, interface) == 0 &&
    286  1.1      cgd 		    unit == ifnet.if_unit)
    287  1.1      cgd 			interesting = ip;
    288  1.1      cgd 		ip->ift_name[15] = '\0';
    289  1.1      cgd 		cp = index(ip->ift_name, '\0');
    290  1.1      cgd 		sprintf(cp, "%d)", ifnet.if_unit);
    291  1.1      cgd 		ip++;
    292  1.1      cgd 		if (ip >= iftot + MAXIF - 2)
    293  1.1      cgd 			break;
    294  1.6      cgd 		off = (off_t)(long)ifnet.if_next;
    295  1.1      cgd 	}
    296  1.1      cgd 	lastif = ip;
    297  1.1      cgd 
    298  1.1      cgd 	(void)signal(SIGALRM, catchalarm);
    299  1.1      cgd 	signalled = NO;
    300  1.1      cgd 	(void)alarm(interval);
    301  1.1      cgd banner:
    302  1.1      cgd 	printf("   input    %-6.6s    output       ", interesting->ift_name);
    303  1.1      cgd 	if (lastif - iftot > 0) {
    304  1.1      cgd 		if (dflag)
    305  1.1      cgd 			printf("      ");
    306  1.1      cgd 		printf("     input   (Total)    output");
    307  1.1      cgd 	}
    308  1.1      cgd 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
    309  1.1      cgd 		ip->ift_ip = 0;
    310  1.1      cgd 		ip->ift_ie = 0;
    311  1.1      cgd 		ip->ift_op = 0;
    312  1.1      cgd 		ip->ift_oe = 0;
    313  1.1      cgd 		ip->ift_co = 0;
    314  1.1      cgd 		ip->ift_dr = 0;
    315  1.1      cgd 	}
    316  1.1      cgd 	putchar('\n');
    317  1.1      cgd 	printf("%8.8s %5.5s %8.8s %5.5s %5.5s ",
    318  1.1      cgd 		"packets", "errs", "packets", "errs", "colls");
    319  1.1      cgd 	if (dflag)
    320  1.1      cgd 		printf("%5.5s ", "drops");
    321  1.1      cgd 	if (lastif - iftot > 0)
    322  1.1      cgd 		printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
    323  1.1      cgd 			"packets", "errs", "packets", "errs", "colls");
    324  1.1      cgd 	if (dflag)
    325  1.1      cgd 		printf(" %5.5s", "drops");
    326  1.1      cgd 	putchar('\n');
    327  1.1      cgd 	fflush(stdout);
    328  1.1      cgd 	line = 0;
    329  1.1      cgd loop:
    330  1.1      cgd 	sum->ift_ip = 0;
    331  1.1      cgd 	sum->ift_ie = 0;
    332  1.1      cgd 	sum->ift_op = 0;
    333  1.1      cgd 	sum->ift_oe = 0;
    334  1.1      cgd 	sum->ift_co = 0;
    335  1.1      cgd 	sum->ift_dr = 0;
    336  1.1      cgd 	for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
    337  1.6      cgd 		kvm_read((void *)(long)off, (char *)&ifnet, sizeof ifnet);
    338  1.1      cgd 		if (ip == interesting) {
    339  1.1      cgd 			printf("%8d %5d %8d %5d %5d",
    340  1.1      cgd 				ifnet.if_ipackets - ip->ift_ip,
    341  1.1      cgd 				ifnet.if_ierrors - ip->ift_ie,
    342  1.1      cgd 				ifnet.if_opackets - ip->ift_op,
    343  1.1      cgd 				ifnet.if_oerrors - ip->ift_oe,
    344  1.1      cgd 				ifnet.if_collisions - ip->ift_co);
    345  1.1      cgd 			if (dflag)
    346  1.1      cgd 				printf(" %5d",
    347  1.1      cgd 				    ifnet.if_snd.ifq_drops - ip->ift_dr);
    348  1.1      cgd 		}
    349  1.1      cgd 		ip->ift_ip = ifnet.if_ipackets;
    350  1.1      cgd 		ip->ift_ie = ifnet.if_ierrors;
    351  1.1      cgd 		ip->ift_op = ifnet.if_opackets;
    352  1.1      cgd 		ip->ift_oe = ifnet.if_oerrors;
    353  1.1      cgd 		ip->ift_co = ifnet.if_collisions;
    354  1.1      cgd 		ip->ift_dr = ifnet.if_snd.ifq_drops;
    355  1.1      cgd 		sum->ift_ip += ip->ift_ip;
    356  1.1      cgd 		sum->ift_ie += ip->ift_ie;
    357  1.1      cgd 		sum->ift_op += ip->ift_op;
    358  1.1      cgd 		sum->ift_oe += ip->ift_oe;
    359  1.1      cgd 		sum->ift_co += ip->ift_co;
    360  1.1      cgd 		sum->ift_dr += ip->ift_dr;
    361  1.6      cgd 		off = (off_t)(long)ifnet.if_next;
    362  1.1      cgd 	}
    363  1.1      cgd 	if (lastif - iftot > 0) {
    364  1.1      cgd 		printf("  %8d %5d %8d %5d %5d",
    365  1.1      cgd 			sum->ift_ip - total->ift_ip,
    366  1.1      cgd 			sum->ift_ie - total->ift_ie,
    367  1.1      cgd 			sum->ift_op - total->ift_op,
    368  1.1      cgd 			sum->ift_oe - total->ift_oe,
    369  1.1      cgd 			sum->ift_co - total->ift_co);
    370  1.1      cgd 		if (dflag)
    371  1.1      cgd 			printf(" %5d", sum->ift_dr - total->ift_dr);
    372  1.1      cgd 	}
    373  1.1      cgd 	*total = *sum;
    374  1.1      cgd 	putchar('\n');
    375  1.1      cgd 	fflush(stdout);
    376  1.1      cgd 	line++;
    377  1.1      cgd 	oldmask = sigblock(sigmask(SIGALRM));
    378  1.1      cgd 	if (! signalled) {
    379  1.1      cgd 		sigpause(0);
    380  1.1      cgd 	}
    381  1.1      cgd 	sigsetmask(oldmask);
    382  1.1      cgd 	signalled = NO;
    383  1.1      cgd 	(void)alarm(interval);
    384  1.1      cgd 	if (line == 21)
    385  1.1      cgd 		goto banner;
    386  1.1      cgd 	goto loop;
    387  1.1      cgd 	/*NOTREACHED*/
    388  1.1      cgd }
    389  1.1      cgd 
    390  1.1      cgd /*
    391  1.1      cgd  * Called if an interval expires before sidewaysintpr has completed a loop.
    392  1.1      cgd  * Sets a flag to not wait for the alarm.
    393  1.1      cgd  */
    394  1.1      cgd void
    395  1.1      cgd catchalarm()
    396  1.1      cgd {
    397  1.1      cgd 	signalled = YES;
    398  1.1      cgd }
    399