if.c revision 1.35
1/*	$NetBSD: if.c,v 1.35 2000/01/17 18:24:37 itojun 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.35 2000/01/17 18:24:37 itojun 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#include <netdb.h>
65
66#include "netstat.h"
67
68#define	YES	1
69#define	NO	0
70
71static void sidewaysintpr __P((u_int, u_long));
72static void catchalarm __P((int));
73
74#ifdef INET6
75char *netname6 __P((struct sockaddr_in6 *, struct in6_addr *));
76#endif
77
78/*
79 * Print a description of the network interfaces.
80 * NOTE: ifnetaddr is the location of the kernel global "ifnet",
81 * which is a TAILQ_HEAD.
82 */
83void
84intpr(interval, ifnetaddr, pfunc)
85	int interval;
86	u_long ifnetaddr;
87	void (*pfunc)(char *);
88{
89	struct ifnet ifnet;
90	union {
91		struct ifaddr ifa;
92		struct in_ifaddr in;
93#ifdef INET6
94		struct in6_ifaddr in6;
95#endif /* INET6 */
96		struct ns_ifaddr ns;
97		struct iso_ifaddr iso;
98	} ifaddr;
99	u_long ifaddraddr;
100	struct sockaddr *sa;
101	struct ifnet_head ifhead;	/* TAILQ_HEAD */
102	char name[IFNAMSIZ];
103#ifdef INET6
104	char hbuf[NI_MAXHOST];		/* for getnameinfo() */
105#ifdef KAME_SCOPEID
106	const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
107#else
108	const int niflag = NI_NUMERICHOST;
109#endif
110#endif
111
112	if (ifnetaddr == 0) {
113		printf("ifnet: symbol not defined\n");
114		return;
115	}
116	if (interval) {
117		sidewaysintpr((unsigned)interval, ifnetaddr);
118		return;
119	}
120
121	/*
122	 * Find the pointer to the first ifnet structure.  Replace
123	 * the pointer to the TAILQ_HEAD with the actual pointer
124	 * to the first list element.
125	 */
126	if (kread(ifnetaddr, (char *)&ifhead, sizeof ifhead))
127		return;
128	ifnetaddr = (u_long)ifhead.tqh_first;
129
130	if (!sflag & !pflag) {
131		if (bflag) {
132			printf("%-5.5s %-5.5s %-13.13s %-17.17s "
133			       "%10.10s %10.10s",
134			       "Name", "Mtu", "Network", "Address",
135			       "Ibytes", "Obytes");
136		} else {
137			printf("%-5.5s %-5.5s %-13.13s %-17.17s "
138			       "%8.8s %5.5s %8.8s %5.5s %5.5s",
139			       "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
140			       "Opkts", "Oerrs", "Colls");
141		}
142		if (tflag)
143			printf(" %4.4s", "Time");
144		if (dflag)
145			printf(" %5.5s", "Drops");
146		putchar('\n');
147	}
148	ifaddraddr = 0;
149	while (ifnetaddr || ifaddraddr) {
150		struct sockaddr_in *sin;
151#ifdef INET6
152		struct sockaddr_in6 *sin6;
153#endif /* INET6 */
154		char *cp;
155		int n, m;
156
157		if (ifaddraddr == 0) {
158			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
159				return;
160			memmove(name, ifnet.if_xname, IFNAMSIZ);
161			name[IFNAMSIZ - 1] = '\0';	/* sanity */
162			ifnetaddr = (u_long)ifnet.if_list.tqe_next;
163			if (interface != 0 && strcmp(name, interface) != 0)
164				continue;
165			cp = strchr(name, '\0');
166
167			if (pfunc) {
168				(*pfunc)(name);
169				continue;
170			}
171
172			if ((ifnet.if_flags & IFF_UP) == 0)
173				*cp++ = '*';
174			*cp = '\0';
175			ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first;
176		}
177		printf("%-5.5s %-5llu ", name,
178		    (unsigned long long)ifnet.if_mtu);
179		if (ifaddraddr == 0) {
180			printf("%-13.13s ", "none");
181			printf("%-17.17s ", "none");
182		} else {
183			char hexsep = '.';		/* for hexprint */
184			const char *hexfmt = "%x%c";	/* for hexprint */
185			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
186				ifaddraddr = 0;
187				continue;
188			}
189#define CP(x) ((char *)(x))
190			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
191				CP(&ifaddr); sa = (struct sockaddr *)cp;
192			switch (sa->sa_family) {
193			case AF_UNSPEC:
194				printf("%-13.13s ", "none");
195				printf("%-17.17s ", "none");
196				break;
197			case AF_INET:
198				sin = (struct sockaddr_in *)sa;
199#ifdef notdef
200				/*
201				 * can't use inet_makeaddr because kernel
202				 * keeps nets unshifted.
203				 */
204				in = inet_makeaddr(ifaddr.in.ia_subnet,
205					INADDR_ANY);
206				printf("%-13.13s ", netname(in.s_addr,
207				    ifaddr.in.ia_subnetmask));
208#else
209				printf("%-13.13s ",
210				    netname(ifaddr.in.ia_subnet,
211				    ifaddr.in.ia_subnetmask));
212#endif
213				printf("%-17.17s ",
214				    routename(sin->sin_addr.s_addr));
215
216				if (aflag) {
217					u_long multiaddr;
218					struct in_multi inm;
219
220					multiaddr = (u_long)
221					    ifaddr.in.ia_multiaddrs.lh_first;
222					while (multiaddr != 0) {
223						kread(multiaddr, (char *)&inm,
224						   sizeof inm);
225						printf("\n%25s %-17.17s ", "",
226						   routename(
227						      inm.inm_addr.s_addr));
228						multiaddr =
229						   (u_long)inm.inm_list.le_next;
230					}
231				}
232				break;
233#ifdef INET6
234			case AF_INET6:
235				sin6 = (struct sockaddr_in6 *)sa;
236				cp = netname6(&ifaddr.in6.ia_addr,
237					&ifaddr.in6.ia_prefixmask.sin6_addr);
238				if (vflag)
239					n = strlen(cp) < 13 ? 13 : strlen(cp);
240				else
241					n = 13;
242				printf("%-*.*s ", n, n, cp);
243#if 0 /* KAME_SCOPEID: don't do it twice */
244				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
245					sin6->sin6_scope_id =
246						ntohs(*(u_int16_t *)
247						  &sin6->sin6_addr.s6_addr[2]);
248					sin6->sin6_addr.s6_addr[2] = 0;
249					sin6->sin6_addr.s6_addr[3] = 0;
250				}
251#endif
252				if (getnameinfo((struct sockaddr *)sin6,
253						sin6->sin6_len,
254						hbuf, sizeof(hbuf), NULL, 0,
255						niflag) != 0) {
256					cp = "?";
257				} else
258					cp = hbuf;
259				if (vflag)
260					n = strlen(cp) < 17 ? 17 : strlen(cp);
261				else
262					n = 17;
263				printf("%-*.*s ", n, n, cp);
264				if (aflag) {
265					u_long multiaddr;
266					struct in6_multi inm;
267
268					multiaddr = (u_long)
269					    ifaddr.in6.ia6_multiaddrs.lh_first;
270					while (multiaddr != 0) {
271						kread(multiaddr, (char *)&inm,
272						   sizeof inm);
273						inet_ntop(AF_INET6, &inm.in6m_addr,
274						    hbuf, sizeof(hbuf));
275						cp = hbuf;
276						if (vflag)
277						    n = strlen(cp) < 17
278							? 17 : strlen(cp);
279						else
280						    n = 17;
281						printf("\n%25s %-*.*s ", "",
282						    n, n, cp);
283						multiaddr =
284						   (u_long)inm.in6m_entry.le_next;
285					}
286				}
287				break;
288#endif /*INET6*/
289#ifndef SMALL
290			case AF_APPLETALK:
291				printf("atalk:%-7.7s ",
292				       atalk_print(sa,0x10));
293				printf("%-17.17s ", atalk_print(sa,0x0b));
294				break;
295			case AF_NS:
296				{
297				struct sockaddr_ns *sns =
298					(struct sockaddr_ns *)sa;
299				u_long net;
300				char netnum[8];
301
302				*(union ns_net *)&net = sns->sns_addr.x_net;
303				(void)sprintf(netnum, "%xH",
304				    (u_int32_t)ntohl(net));
305				upHex(netnum);
306				printf("ns:%-10s ", netnum);
307				printf("%-17.17s ",
308				    ns_phost((struct sockaddr *)sns));
309				}
310				break;
311#endif
312			case AF_LINK:
313				{
314				struct sockaddr_dl *sdl =
315					(struct sockaddr_dl *)sa;
316				    cp = (char *)LLADDR(sdl);
317				    if (sdl->sdl_type == IFT_FDDI
318					|| sdl->sdl_type == IFT_ETHER)
319					    hexsep = ':', hexfmt = "%02x%c";
320				    n = sdl->sdl_alen;
321				}
322				m = printf("%-13.13s ", "<Link>");
323				goto hexprint;
324			default:
325				m = printf("(%d)", sa->sa_family);
326				for (cp = sa->sa_len + (char *)sa;
327					--cp > sa->sa_data && (*cp == 0);) {}
328				n = cp - sa->sa_data + 1;
329				cp = sa->sa_data;
330			hexprint:
331				while (--n >= 0)
332					m += printf(hexfmt, *cp++ & 0xff,
333						    n > 0 ? hexsep : ' ');
334				m = 32 - m;
335				while (m-- > 0)
336					putchar(' ');
337				break;
338			}
339			ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
340		}
341		if (bflag) {
342			printf("%10llu %10llu",
343				(unsigned long long)ifnet.if_ibytes,
344				(unsigned long long)ifnet.if_obytes);
345		} else {
346			printf("%8llu %5llu %8llu %5llu %5llu",
347				(unsigned long long)ifnet.if_ipackets,
348				(unsigned long long)ifnet.if_ierrors,
349				(unsigned long long)ifnet.if_opackets,
350				(unsigned long long)ifnet.if_oerrors,
351				(unsigned long long)ifnet.if_collisions);
352		}
353		if (tflag)
354			printf(" %4d", ifnet.if_timer);
355		if (dflag)
356			printf(" %5d", ifnet.if_snd.ifq_drops);
357		putchar('\n');
358	}
359}
360
361#define	MAXIF	100
362struct	iftot {
363	char	ift_name[IFNAMSIZ];	/* interface name */
364	u_long	ift_ip;			/* input packets */
365	u_long	ift_ib;			/* input bytes */
366	u_long	ift_ie;			/* input errors */
367	u_long	ift_op;			/* output packets */
368	u_long	ift_ob;			/* output bytes */
369	u_long	ift_oe;			/* output errors */
370	u_long	ift_co;			/* collisions */
371	int	ift_dr;			/* drops */
372} iftot[MAXIF];
373
374u_char	signalled;			/* set if alarm goes off "early" */
375
376/*
377 * Print a running summary of interface statistics.
378 * Repeat display every interval seconds, showing statistics
379 * collected over that interval.  Assumes that interval is non-zero.
380 * First line printed at top of screen is always cumulative.
381 */
382static void
383sidewaysintpr(interval, off)
384	unsigned interval;
385	u_long off;
386{
387	struct ifnet ifnet;
388	u_long firstifnet;
389	struct iftot *ip, *total;
390	int line;
391	struct iftot *lastif, *sum, *interesting;
392	struct ifnet_head ifhead;	/* TAILQ_HEAD */
393	int oldmask;
394
395	/*
396	 * Find the pointer to the first ifnet structure.  Replace
397	 * the pointer to the TAILQ_HEAD with the actual pointer
398	 * to the first list element.
399	 */
400	if (kread(off, (char *)&ifhead, sizeof ifhead))
401		return;
402	firstifnet = (u_long)ifhead.tqh_first;
403
404	lastif = iftot;
405	sum = iftot + MAXIF - 1;
406	total = sum - 1;
407	interesting = (interface == NULL) ? iftot : NULL;
408	for (off = firstifnet, ip = iftot; off;) {
409		if (kread(off, (char *)&ifnet, sizeof ifnet))
410			break;
411		memset(ip->ift_name, 0, sizeof(ip->ift_name));
412		snprintf(ip->ift_name, IFNAMSIZ, "%s", ifnet.if_xname);
413		if (interface && strcmp(ifnet.if_xname, interface) == 0)
414			interesting = ip;
415		ip++;
416		if (ip >= iftot + MAXIF - 2)
417			break;
418		off = (u_long)ifnet.if_list.tqe_next;
419	}
420	if (interesting == NULL) {
421		fprintf(stderr, "%s: %s: unknown interface\n",
422		    __progname, interface);
423		exit(1);
424	}
425	lastif = ip;
426
427	(void)signal(SIGALRM, catchalarm);
428	signalled = NO;
429	(void)alarm(interval);
430banner:
431	if (bflag)
432		printf("%7.7s in %8.8s %6.6s out %5.5s",
433		    interesting->ift_name, " ",
434		    interesting->ift_name, " ");
435	else
436		printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
437		    interesting->ift_name, " ",
438		    interesting->ift_name, " ", " ");
439	if (dflag)
440		printf(" %5.5s", " ");
441	if (lastif - iftot > 0) {
442		if (bflag)
443			printf("  %7.7s in %8.8s %6.6s out %5.5s",
444			    "total", " ", "total", " ");
445		else
446			printf("  %5.5s in %5.5s%5.5s out %5.5s %5.5s",
447			    "total", " ", "total", " ", " ");
448		if (dflag)
449			printf(" %5.5s", " ");
450	}
451	for (ip = iftot; ip < iftot + MAXIF; ip++) {
452		ip->ift_ip = 0;
453		ip->ift_ib = 0;
454		ip->ift_ie = 0;
455		ip->ift_op = 0;
456		ip->ift_ob = 0;
457		ip->ift_oe = 0;
458		ip->ift_co = 0;
459		ip->ift_dr = 0;
460	}
461	putchar('\n');
462	if (bflag)
463		printf("%10.10s %8.8s %10.10s %5.5s",
464		    "bytes", " ", "bytes", " ");
465	else
466		printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
467		    "packets", "errs", "packets", "errs", "colls");
468	if (dflag)
469		printf(" %5.5s", "drops");
470	if (lastif - iftot > 0) {
471		if (bflag)
472			printf("  %10.10s %8.8s %10.10s %5.5s",
473			    "bytes", " ", "bytes", " ");
474		else
475			printf("  %8.8s %5.5s %8.8s %5.5s %5.5s",
476			    "packets", "errs", "packets", "errs", "colls");
477		if (dflag)
478			printf(" %5.5s", "drops");
479	}
480	putchar('\n');
481	fflush(stdout);
482	line = 0;
483loop:
484	sum->ift_ip = 0;
485	sum->ift_ib = 0;
486	sum->ift_ie = 0;
487	sum->ift_op = 0;
488	sum->ift_ob = 0;
489	sum->ift_oe = 0;
490	sum->ift_co = 0;
491	sum->ift_dr = 0;
492	for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
493		if (kread(off, (char *)&ifnet, sizeof ifnet)) {
494			off = 0;
495			continue;
496		}
497		if (ip == interesting) {
498			if (bflag) {
499				printf("%10llu %8.8s %10llu %5.5s",
500				    (unsigned long long)(ifnet.if_ibytes -
501					ip->ift_ib), " ",
502				    (unsigned long long)(ifnet.if_obytes -
503					ip->ift_ob), " ");
504			} else {
505				printf("%8llu %5llu %8llu %5llu %5llu",
506				    (unsigned long long)
507					(ifnet.if_ipackets - ip->ift_ip),
508				    (unsigned long long)
509					(ifnet.if_ierrors - ip->ift_ie),
510				    (unsigned long long)
511					(ifnet.if_opackets - ip->ift_op),
512				    (unsigned long long)
513					(ifnet.if_oerrors - ip->ift_oe),
514				    (unsigned long long)
515					(ifnet.if_collisions - ip->ift_co));
516			}
517			if (dflag)
518				printf(" %5llu",
519				    (unsigned long long)
520					(ifnet.if_snd.ifq_drops - ip->ift_dr));
521		}
522		ip->ift_ip = ifnet.if_ipackets;
523		ip->ift_ib = ifnet.if_ibytes;
524		ip->ift_ie = ifnet.if_ierrors;
525		ip->ift_op = ifnet.if_opackets;
526		ip->ift_ob = ifnet.if_obytes;
527		ip->ift_oe = ifnet.if_oerrors;
528		ip->ift_co = ifnet.if_collisions;
529		ip->ift_dr = ifnet.if_snd.ifq_drops;
530		sum->ift_ip += ip->ift_ip;
531		sum->ift_ib += ip->ift_ib;
532		sum->ift_ie += ip->ift_ie;
533		sum->ift_op += ip->ift_op;
534		sum->ift_ob += ip->ift_ob;
535		sum->ift_oe += ip->ift_oe;
536		sum->ift_co += ip->ift_co;
537		sum->ift_dr += ip->ift_dr;
538		off = (u_long)ifnet.if_list.tqe_next;
539	}
540	if (lastif - iftot > 0) {
541		if (bflag) {
542			printf("  %10llu %8.8s %10llu %5.5s",
543			    (unsigned long long)
544				(sum->ift_ib - total->ift_ib), " ",
545			    (unsigned long long)
546				(sum->ift_ob - total->ift_ob), " ");
547		} else {
548			printf("  %8llu %5llu %8llu %5llu %5llu",
549			    (unsigned long long)
550				(sum->ift_ip - total->ift_ip),
551			    (unsigned long long)
552				(sum->ift_ie - total->ift_ie),
553			    (unsigned long long)
554				(sum->ift_op - total->ift_op),
555			    (unsigned long long)
556				(sum->ift_oe - total->ift_oe),
557			    (unsigned long long)
558				(sum->ift_co - total->ift_co));
559		}
560		if (dflag)
561			printf(" %5llu",
562			    (unsigned long long)(sum->ift_dr - total->ift_dr));
563	}
564	*total = *sum;
565	putchar('\n');
566	fflush(stdout);
567	line++;
568	oldmask = sigblock(sigmask(SIGALRM));
569	if (! signalled) {
570		sigpause(0);
571	}
572	sigsetmask(oldmask);
573	signalled = NO;
574	(void)alarm(interval);
575	if (line == 21)
576		goto banner;
577	goto loop;
578	/*NOTREACHED*/
579}
580
581/*
582 * Called if an interval expires before sidewaysintpr has completed a loop.
583 * Sets a flag to not wait for the alarm.
584 */
585static void
586catchalarm(signo)
587	int signo;
588{
589
590	signalled = YES;
591}
592