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