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