Home | History | Annotate | Line # | Download | only in netstat
if.c revision 1.12
      1   1.1      cgd /*
      2   1.8  mycroft  * Copyright (c) 1983, 1988, 1993
      3   1.8  mycroft  *	The Regents of the University of California.  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.8  mycroft /*static char sccsid[] = "from: @(#)if.c	8.2 (Berkeley) 2/21/94";*/
     36  1.12      jtc static char *rcsid = "$Id: if.c,v 1.12 1995/09/28 06:22:54 jtc Exp $";
     37   1.1      cgd #endif /* not lint */
     38   1.1      cgd 
     39   1.1      cgd #include <sys/types.h>
     40   1.8  mycroft #include <sys/protosw.h>
     41   1.1      cgd #include <sys/socket.h>
     42   1.1      cgd 
     43   1.1      cgd #include <net/if.h>
     44   1.1      cgd #include <net/if_dl.h>
     45   1.1      cgd #include <netinet/in.h>
     46   1.1      cgd #include <netinet/in_var.h>
     47   1.1      cgd #include <netns/ns.h>
     48   1.1      cgd #include <netns/ns_if.h>
     49   1.1      cgd #include <netiso/iso.h>
     50   1.1      cgd #include <netiso/iso_var.h>
     51   1.8  mycroft #include <arpa/inet.h>
     52   1.1      cgd 
     53   1.8  mycroft #include <signal.h>
     54   1.1      cgd #include <stdio.h>
     55   1.8  mycroft #include <string.h>
     56   1.8  mycroft #include <unistd.h>
     57   1.8  mycroft 
     58   1.8  mycroft #include "netstat.h"
     59   1.1      cgd 
     60   1.1      cgd #define	YES	1
     61   1.1      cgd #define	NO	0
     62   1.1      cgd 
     63   1.8  mycroft static void sidewaysintpr __P((u_int, u_long));
     64   1.8  mycroft static void catchalarm __P((int));
     65   1.1      cgd 
     66   1.1      cgd /*
     67   1.1      cgd  * Print a description of the network interfaces.
     68   1.1      cgd  */
     69   1.8  mycroft void
     70   1.1      cgd intpr(interval, ifnetaddr)
     71   1.1      cgd 	int interval;
     72   1.7      cgd 	u_long ifnetaddr;
     73   1.1      cgd {
     74   1.1      cgd 	struct ifnet ifnet;
     75   1.1      cgd 	union {
     76   1.1      cgd 		struct ifaddr ifa;
     77   1.1      cgd 		struct in_ifaddr in;
     78   1.1      cgd 		struct ns_ifaddr ns;
     79   1.1      cgd 		struct iso_ifaddr iso;
     80   1.1      cgd 	} ifaddr;
     81   1.7      cgd 	u_long ifaddraddr;
     82   1.1      cgd 	struct sockaddr *sa;
     83   1.1      cgd 	char name[16];
     84   1.1      cgd 
     85   1.1      cgd 	if (ifnetaddr == 0) {
     86   1.1      cgd 		printf("ifnet: symbol not defined\n");
     87   1.1      cgd 		return;
     88   1.1      cgd 	}
     89   1.1      cgd 	if (interval) {
     90   1.1      cgd 		sidewaysintpr((unsigned)interval, ifnetaddr);
     91   1.1      cgd 		return;
     92   1.1      cgd 	}
     93   1.8  mycroft 	if (kread(ifnetaddr, (char *)&ifnetaddr, sizeof ifnetaddr))
     94   1.8  mycroft 		return;
     95  1.12      jtc 	printf("%-5.5s %-5.5s %-11.11s %-15.15s %10.10s %5.5s %8.8s %5.5s",
     96   1.1      cgd 		"Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
     97   1.1      cgd 		"Opkts", "Oerrs");
     98   1.1      cgd 	printf(" %5s", "Coll");
     99   1.1      cgd 	if (tflag)
    100   1.1      cgd 		printf(" %s", "Time");
    101   1.1      cgd 	if (dflag)
    102   1.1      cgd 		printf(" %s", "Drop");
    103   1.1      cgd 	putchar('\n');
    104   1.1      cgd 	ifaddraddr = 0;
    105   1.1      cgd 	while (ifnetaddr || ifaddraddr) {
    106   1.1      cgd 		struct sockaddr_in *sin;
    107   1.1      cgd 		register char *cp;
    108   1.1      cgd 		int n, m;
    109   1.1      cgd 
    110   1.1      cgd 		if (ifaddraddr == 0) {
    111   1.8  mycroft 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) ||
    112   1.8  mycroft 			    kread((u_long)ifnet.if_name, name, 16))
    113   1.8  mycroft 				return;
    114   1.1      cgd 			name[15] = '\0';
    115  1.10  mycroft 			ifnetaddr = (u_long)ifnet.if_list.tqe_next;
    116   1.8  mycroft 			if (interface != 0 && (strcmp(name, interface) != 0 ||
    117   1.8  mycroft 			    unit != ifnet.if_unit))
    118   1.1      cgd 				continue;
    119   1.1      cgd 			cp = index(name, '\0');
    120   1.4      cgd 			cp += sprintf(cp, "%d", ifnet.if_unit);
    121  1.10  mycroft 			if ((ifnet.if_flags & IFF_UP) == 0)
    122   1.1      cgd 				*cp++ = '*';
    123   1.1      cgd 			*cp = '\0';
    124  1.10  mycroft 			ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first;
    125   1.1      cgd 		}
    126   1.1      cgd 		printf("%-5.5s %-5d ", name, ifnet.if_mtu);
    127   1.1      cgd 		if (ifaddraddr == 0) {
    128   1.1      cgd 			printf("%-11.11s ", "none");
    129   1.1      cgd 			printf("%-15.15s ", "none");
    130   1.1      cgd 		} else {
    131   1.8  mycroft 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
    132   1.8  mycroft 				ifaddraddr = 0;
    133   1.8  mycroft 				continue;
    134   1.8  mycroft 			}
    135   1.1      cgd #define CP(x) ((char *)(x))
    136   1.8  mycroft 			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
    137   1.1      cgd 				CP(&ifaddr); sa = (struct sockaddr *)cp;
    138   1.1      cgd 			switch (sa->sa_family) {
    139   1.1      cgd 			case AF_UNSPEC:
    140   1.1      cgd 				printf("%-11.11s ", "none");
    141  1.12      jtc 				printf("%-17.17s ", "none");
    142   1.1      cgd 				break;
    143   1.1      cgd 			case AF_INET:
    144   1.1      cgd 				sin = (struct sockaddr_in *)sa;
    145   1.1      cgd #ifdef notdef
    146   1.1      cgd 				/* can't use inet_makeaddr because kernel
    147   1.1      cgd 				 * keeps nets unshifted.
    148   1.1      cgd 				 */
    149   1.1      cgd 				in = inet_makeaddr(ifaddr.in.ia_subnet,
    150   1.1      cgd 					INADDR_ANY);
    151   1.8  mycroft 				printf("%-11.11s ", netname(in.s_addr,
    152   1.8  mycroft 				    ifaddr.in.ia_subnetmask));
    153   1.1      cgd #else
    154   1.1      cgd 				printf("%-11.11s ",
    155  1.11  mycroft 				    netname(ifaddr.in.ia_subnet,
    156   1.8  mycroft 				    ifaddr.in.ia_subnetmask));
    157   1.1      cgd #endif
    158  1.12      jtc 				printf("%-17.17s ",
    159   1.8  mycroft 				    routename(sin->sin_addr.s_addr));
    160  1.10  mycroft 
    161  1.10  mycroft 				if (aflag) {
    162  1.10  mycroft 					u_long multiaddr;
    163  1.10  mycroft 					struct in_multi inm;
    164  1.10  mycroft 
    165  1.10  mycroft 					multiaddr = (u_long)ifaddr.in.ia_multiaddrs.lh_first;
    166  1.10  mycroft 					while (multiaddr != 0) {
    167  1.10  mycroft 						kread(multiaddr, (char *)&inm,
    168  1.10  mycroft 						    sizeof inm);
    169  1.10  mycroft 						printf("\n%23s %-15.15s ", "",
    170  1.10  mycroft 						    routename(inm.inm_addr.s_addr));
    171  1.10  mycroft 						multiaddr = (u_long)inm.inm_list.le_next;
    172  1.10  mycroft 					}
    173  1.10  mycroft 				}
    174   1.1      cgd 				break;
    175   1.1      cgd 			case AF_NS:
    176   1.1      cgd 				{
    177   1.1      cgd 				struct sockaddr_ns *sns =
    178   1.1      cgd 					(struct sockaddr_ns *)sa;
    179   1.1      cgd 				u_long net;
    180   1.1      cgd 				char netnum[8];
    181   1.1      cgd 
    182   1.1      cgd 				*(union ns_net *) &net = sns->sns_addr.x_net;
    183   1.1      cgd 		sprintf(netnum, "%lxH", ntohl(net));
    184   1.1      cgd 				upHex(netnum);
    185   1.1      cgd 				printf("ns:%-8s ", netnum);
    186  1.12      jtc 				printf("%-17s ",
    187   1.8  mycroft 				    ns_phost((struct sockaddr *)sns));
    188   1.1      cgd 				}
    189   1.1      cgd 				break;
    190   1.1      cgd 			case AF_LINK:
    191   1.1      cgd 				{
    192   1.1      cgd 				struct sockaddr_dl *sdl =
    193   1.1      cgd 					(struct sockaddr_dl *)sa;
    194   1.1      cgd 				    cp = (char *)LLADDR(sdl);
    195   1.1      cgd 				    n = sdl->sdl_alen;
    196   1.1      cgd 				}
    197   1.9  mycroft 				m = printf("%-11.11s ", "<Link>");
    198   1.1      cgd 				goto hexprint;
    199   1.1      cgd 			default:
    200   1.1      cgd 				m = printf("(%d)", sa->sa_family);
    201   1.1      cgd 				for (cp = sa->sa_len + (char *)sa;
    202   1.1      cgd 					--cp > sa->sa_data && (*cp == 0);) {}
    203   1.1      cgd 				n = cp - sa->sa_data + 1;
    204   1.1      cgd 				cp = sa->sa_data;
    205   1.1      cgd 			hexprint:
    206   1.1      cgd 				while (--n >= 0)
    207   1.1      cgd 					m += printf("%x%c", *cp++ & 0xff,
    208   1.1      cgd 						    n > 0 ? '.' : ' ');
    209  1.12      jtc 				m = 30 - m;
    210   1.1      cgd 				while (m-- > 0)
    211   1.1      cgd 					putchar(' ');
    212   1.1      cgd 				break;
    213   1.1      cgd 			}
    214  1.10  mycroft 			ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
    215   1.1      cgd 		}
    216   1.1      cgd 		printf("%8d %5d %8d %5d %5d",
    217   1.1      cgd 		    ifnet.if_ipackets, ifnet.if_ierrors,
    218   1.1      cgd 		    ifnet.if_opackets, ifnet.if_oerrors,
    219   1.1      cgd 		    ifnet.if_collisions);
    220   1.1      cgd 		if (tflag)
    221   1.1      cgd 			printf(" %3d", ifnet.if_timer);
    222   1.1      cgd 		if (dflag)
    223   1.1      cgd 			printf(" %3d", ifnet.if_snd.ifq_drops);
    224   1.1      cgd 		putchar('\n');
    225   1.1      cgd 	}
    226   1.1      cgd }
    227   1.1      cgd 
    228   1.1      cgd #define	MAXIF	10
    229   1.1      cgd struct	iftot {
    230   1.1      cgd 	char	ift_name[16];		/* interface name */
    231   1.1      cgd 	int	ift_ip;			/* input packets */
    232   1.1      cgd 	int	ift_ie;			/* input errors */
    233   1.1      cgd 	int	ift_op;			/* output packets */
    234   1.1      cgd 	int	ift_oe;			/* output errors */
    235   1.1      cgd 	int	ift_co;			/* collisions */
    236   1.1      cgd 	int	ift_dr;			/* drops */
    237   1.1      cgd } iftot[MAXIF];
    238   1.1      cgd 
    239   1.1      cgd u_char	signalled;			/* set if alarm goes off "early" */
    240   1.1      cgd 
    241   1.1      cgd /*
    242   1.1      cgd  * Print a running summary of interface statistics.
    243   1.1      cgd  * Repeat display every interval seconds, showing statistics
    244   1.1      cgd  * collected over that interval.  Assumes that interval is non-zero.
    245   1.1      cgd  * First line printed at top of screen is always cumulative.
    246   1.1      cgd  */
    247   1.8  mycroft static void
    248   1.1      cgd sidewaysintpr(interval, off)
    249   1.1      cgd 	unsigned interval;
    250   1.7      cgd 	u_long off;
    251   1.1      cgd {
    252   1.1      cgd 	struct ifnet ifnet;
    253   1.7      cgd 	u_long firstifnet;
    254   1.1      cgd 	register struct iftot *ip, *total;
    255   1.1      cgd 	register int line;
    256   1.1      cgd 	struct iftot *lastif, *sum, *interesting;
    257   1.1      cgd 	int oldmask;
    258   1.1      cgd 
    259   1.8  mycroft 	if (kread(off, (char *)&firstifnet, sizeof (u_long)))
    260   1.8  mycroft 		return;
    261   1.1      cgd 	lastif = iftot;
    262   1.1      cgd 	sum = iftot + MAXIF - 1;
    263   1.1      cgd 	total = sum - 1;
    264   1.1      cgd 	interesting = iftot;
    265   1.1      cgd 	for (off = firstifnet, ip = iftot; off;) {
    266   1.1      cgd 		char *cp;
    267   1.1      cgd 
    268   1.8  mycroft 		if (kread(off, (char *)&ifnet, sizeof ifnet))
    269   1.8  mycroft 			break;
    270   1.1      cgd 		ip->ift_name[0] = '(';
    271   1.8  mycroft 		if (kread((u_long)ifnet.if_name, ip->ift_name + 1, 15))
    272   1.8  mycroft 			break;
    273   1.1      cgd 		if (interface && strcmp(ip->ift_name + 1, interface) == 0 &&
    274   1.1      cgd 		    unit == ifnet.if_unit)
    275   1.1      cgd 			interesting = ip;
    276   1.1      cgd 		ip->ift_name[15] = '\0';
    277   1.1      cgd 		cp = index(ip->ift_name, '\0');
    278   1.1      cgd 		sprintf(cp, "%d)", ifnet.if_unit);
    279   1.1      cgd 		ip++;
    280   1.1      cgd 		if (ip >= iftot + MAXIF - 2)
    281   1.1      cgd 			break;
    282  1.10  mycroft 		off = (u_long)ifnet.if_list.tqe_next;
    283   1.1      cgd 	}
    284   1.1      cgd 	lastif = ip;
    285   1.1      cgd 
    286   1.1      cgd 	(void)signal(SIGALRM, catchalarm);
    287   1.1      cgd 	signalled = NO;
    288   1.1      cgd 	(void)alarm(interval);
    289   1.1      cgd banner:
    290   1.1      cgd 	printf("   input    %-6.6s    output       ", interesting->ift_name);
    291   1.1      cgd 	if (lastif - iftot > 0) {
    292   1.1      cgd 		if (dflag)
    293   1.1      cgd 			printf("      ");
    294   1.1      cgd 		printf("     input   (Total)    output");
    295   1.1      cgd 	}
    296   1.1      cgd 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
    297   1.1      cgd 		ip->ift_ip = 0;
    298   1.1      cgd 		ip->ift_ie = 0;
    299   1.1      cgd 		ip->ift_op = 0;
    300   1.1      cgd 		ip->ift_oe = 0;
    301   1.1      cgd 		ip->ift_co = 0;
    302   1.1      cgd 		ip->ift_dr = 0;
    303   1.1      cgd 	}
    304   1.1      cgd 	putchar('\n');
    305   1.1      cgd 	printf("%8.8s %5.5s %8.8s %5.5s %5.5s ",
    306   1.1      cgd 		"packets", "errs", "packets", "errs", "colls");
    307   1.1      cgd 	if (dflag)
    308   1.1      cgd 		printf("%5.5s ", "drops");
    309   1.1      cgd 	if (lastif - iftot > 0)
    310   1.1      cgd 		printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
    311   1.1      cgd 			"packets", "errs", "packets", "errs", "colls");
    312   1.1      cgd 	if (dflag)
    313   1.1      cgd 		printf(" %5.5s", "drops");
    314   1.1      cgd 	putchar('\n');
    315   1.1      cgd 	fflush(stdout);
    316   1.1      cgd 	line = 0;
    317   1.1      cgd loop:
    318   1.1      cgd 	sum->ift_ip = 0;
    319   1.1      cgd 	sum->ift_ie = 0;
    320   1.1      cgd 	sum->ift_op = 0;
    321   1.1      cgd 	sum->ift_oe = 0;
    322   1.1      cgd 	sum->ift_co = 0;
    323   1.1      cgd 	sum->ift_dr = 0;
    324   1.1      cgd 	for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
    325   1.8  mycroft 		if (kread(off, (char *)&ifnet, sizeof ifnet)) {
    326   1.8  mycroft 			off = 0;
    327   1.8  mycroft 			continue;
    328   1.8  mycroft 		}
    329   1.1      cgd 		if (ip == interesting) {
    330   1.1      cgd 			printf("%8d %5d %8d %5d %5d",
    331   1.1      cgd 				ifnet.if_ipackets - ip->ift_ip,
    332   1.1      cgd 				ifnet.if_ierrors - ip->ift_ie,
    333   1.1      cgd 				ifnet.if_opackets - ip->ift_op,
    334   1.1      cgd 				ifnet.if_oerrors - ip->ift_oe,
    335   1.1      cgd 				ifnet.if_collisions - ip->ift_co);
    336   1.1      cgd 			if (dflag)
    337   1.1      cgd 				printf(" %5d",
    338   1.1      cgd 				    ifnet.if_snd.ifq_drops - ip->ift_dr);
    339   1.1      cgd 		}
    340   1.1      cgd 		ip->ift_ip = ifnet.if_ipackets;
    341   1.1      cgd 		ip->ift_ie = ifnet.if_ierrors;
    342   1.1      cgd 		ip->ift_op = ifnet.if_opackets;
    343   1.1      cgd 		ip->ift_oe = ifnet.if_oerrors;
    344   1.1      cgd 		ip->ift_co = ifnet.if_collisions;
    345   1.1      cgd 		ip->ift_dr = ifnet.if_snd.ifq_drops;
    346   1.1      cgd 		sum->ift_ip += ip->ift_ip;
    347   1.1      cgd 		sum->ift_ie += ip->ift_ie;
    348   1.1      cgd 		sum->ift_op += ip->ift_op;
    349   1.1      cgd 		sum->ift_oe += ip->ift_oe;
    350   1.1      cgd 		sum->ift_co += ip->ift_co;
    351   1.1      cgd 		sum->ift_dr += ip->ift_dr;
    352  1.10  mycroft 		off = (u_long)ifnet.if_list.tqe_next;
    353   1.1      cgd 	}
    354   1.1      cgd 	if (lastif - iftot > 0) {
    355   1.1      cgd 		printf("  %8d %5d %8d %5d %5d",
    356   1.1      cgd 			sum->ift_ip - total->ift_ip,
    357   1.1      cgd 			sum->ift_ie - total->ift_ie,
    358   1.1      cgd 			sum->ift_op - total->ift_op,
    359   1.1      cgd 			sum->ift_oe - total->ift_oe,
    360   1.1      cgd 			sum->ift_co - total->ift_co);
    361   1.1      cgd 		if (dflag)
    362   1.1      cgd 			printf(" %5d", sum->ift_dr - total->ift_dr);
    363   1.1      cgd 	}
    364   1.1      cgd 	*total = *sum;
    365   1.1      cgd 	putchar('\n');
    366   1.1      cgd 	fflush(stdout);
    367   1.1      cgd 	line++;
    368   1.1      cgd 	oldmask = sigblock(sigmask(SIGALRM));
    369   1.1      cgd 	if (! signalled) {
    370   1.1      cgd 		sigpause(0);
    371   1.1      cgd 	}
    372   1.1      cgd 	sigsetmask(oldmask);
    373   1.1      cgd 	signalled = NO;
    374   1.1      cgd 	(void)alarm(interval);
    375   1.1      cgd 	if (line == 21)
    376   1.1      cgd 		goto banner;
    377   1.1      cgd 	goto loop;
    378   1.1      cgd 	/*NOTREACHED*/
    379   1.1      cgd }
    380   1.1      cgd 
    381   1.1      cgd /*
    382   1.1      cgd  * Called if an interval expires before sidewaysintpr has completed a loop.
    383   1.1      cgd  * Sets a flag to not wait for the alarm.
    384   1.1      cgd  */
    385   1.8  mycroft static void
    386   1.8  mycroft catchalarm(signo)
    387   1.8  mycroft 	int signo;
    388   1.1      cgd {
    389   1.1      cgd 	signalled = YES;
    390   1.1      cgd }
    391