xref: /freebsd/usr.bin/netstat/if.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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 /*
36 static char sccsid[] = "@(#)if.c	8.3 (Berkeley) 4/28/95";
37 */
38 static const char rcsid[] =
39 	"$Id: if.c,v 1.25 1999/04/20 22:04:31 billf Exp $";
40 #endif /* not lint */
41 
42 #include <sys/types.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/sysctl.h>
46 #include <sys/time.h>
47 
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
52 #include <net/bridge.h>
53 #include <net/ethernet.h>
54 #include <netinet/in.h>
55 #include <netinet/in_var.h>
56 #include <netipx/ipx.h>
57 #include <netipx/ipx_if.h>
58 #ifdef NS
59 #include <netns/ns.h>
60 #include <netns/ns_if.h>
61 #endif
62 #ifdef ISO
63 #include <netiso/iso.h>
64 #include <netiso/iso_var.h>
65 #endif
66 #include <arpa/inet.h>
67 
68 #include <signal.h>
69 #include <stdio.h>
70 #include <string.h>
71 #include <unistd.h>
72 
73 #include "netstat.h"
74 
75 #define	YES	1
76 #define	NO	0
77 
78 static void sidewaysintpr __P((u_int, u_long));
79 static void catchalarm __P((int));
80 
81 void
82 bdg_stats(u_long dummy, char *name) /* print bridge statistics */
83 {
84     int i, slen ;
85     struct bdg_stats s ;
86     int mib[4] ;
87 
88     slen = sizeof(s);
89 
90     mib[0] = CTL_NET ;
91     mib[1] = PF_LINK ;
92     mib[2] = IFT_ETHER ;
93     mib[3] = PF_BDG ;
94     if (sysctl(mib,4, &s,&slen,NULL,0)==-1)
95 	return ; /* no bridging */
96     printf("-- Bridging statistics (%s) --\n", name) ;
97     printf(
98 "Name          In      Out  Forward     Drop    Bcast    Mcast    Local  Unknown\n");
99     for (i = 0 ; i < 16 ; i++) {
100 	if (s.s[i].name[0])
101 	printf("%-6s %9d%9d%9d%9d%9d%9d%9d%9d\n",
102 	  s.s[i].name,
103 	  s.s[i].p_in[(int)BDG_IN],
104 	  s.s[i].p_in[(int)BDG_OUT],
105 	  s.s[i].p_in[(int)BDG_FORWARD],
106 	  s.s[i].p_in[(int)BDG_DROP],
107 	  s.s[i].p_in[(int)BDG_BCAST],
108 	  s.s[i].p_in[(int)BDG_MCAST],
109 	  s.s[i].p_in[(int)BDG_LOCAL],
110 	  s.s[i].p_in[(int)BDG_UNKNOWN] );
111     }
112 }
113 
114 /*
115  * Print a description of the network interfaces.
116  */
117 void
118 intpr(interval, ifnetaddr)
119 	int interval;
120 	u_long ifnetaddr;
121 {
122 	struct ifnet ifnet;
123 	struct ifnethead ifnethead;
124 	union {
125 		struct ifaddr ifa;
126 		struct in_ifaddr in;
127 		struct ipx_ifaddr ipx;
128 #ifdef NS
129 		struct ns_ifaddr ns;
130 #endif
131 #ifdef ISO
132 		struct iso_ifaddr iso;
133 #endif
134 	} ifaddr;
135 	u_long ifaddraddr;
136 	u_long ifaddrfound;
137 	u_long ifnetfound;
138 	struct sockaddr *sa;
139 	char name[32], tname[16];
140 
141 	if (ifnetaddr == 0) {
142 		printf("ifnet: symbol not defined\n");
143 		return;
144 	}
145 	if (interval) {
146 		sidewaysintpr((unsigned)interval, ifnetaddr);
147 		return;
148 	}
149 	if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
150 		return;
151 	ifnetaddr = (u_long)ifnethead.tqh_first;
152 	if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
153 		return;
154 
155 	printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
156 		"Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
157 	if (bflag)
158 		printf(" %10.10s","Ibytes");
159 	printf(" %8.8s %5.5s", "Opkts", "Oerrs");
160 	if (bflag)
161 		printf(" %10.10s","Obytes");
162 	printf(" %5s", "Coll");
163 	if (tflag)
164 		printf(" %s", "Time");
165 	if (dflag)
166 		printf(" %s", "Drop");
167 	putchar('\n');
168 	ifaddraddr = 0;
169 	while (ifnetaddr || ifaddraddr) {
170 		struct sockaddr_in *sin;
171 		register char *cp;
172 		int n, m;
173 
174 		if (ifaddraddr == 0) {
175 			ifnetfound = ifnetaddr;
176 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) ||
177 			    kread((u_long)ifnet.if_name, tname, 16))
178 				return;
179 			tname[15] = '\0';
180 			ifnetaddr = (u_long)ifnet.if_link.tqe_next;
181 			snprintf(name, 32, "%s%d", tname, ifnet.if_unit);
182 			if (interface != 0 && (strcmp(name, interface) != 0))
183 				continue;
184 			cp = index(name, '\0');
185 			if ((ifnet.if_flags&IFF_UP) == 0)
186 				*cp++ = '*';
187 			*cp = '\0';
188 			ifaddraddr = (u_long)ifnet.if_addrhead.tqh_first;
189 		}
190 		printf("%-5.5s %-5lu ", name, ifnet.if_mtu);
191 		ifaddrfound = ifaddraddr;
192 		if (ifaddraddr == 0) {
193 			printf("%-13.13s ", "none");
194 			printf("%-15.15s ", "none");
195 		} else {
196 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
197 				ifaddraddr = 0;
198 				continue;
199 			}
200 #define CP(x) ((char *)(x))
201 			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
202 				CP(&ifaddr); sa = (struct sockaddr *)cp;
203 			switch (sa->sa_family) {
204 			case AF_UNSPEC:
205 				printf("%-13.13s ", "none");
206 				printf("%-15.15s ", "none");
207 				break;
208 			case AF_INET:
209 				sin = (struct sockaddr_in *)sa;
210 #ifdef notdef
211 				/* can't use inet_makeaddr because kernel
212 				 * keeps nets unshifted.
213 				 */
214 				in = inet_makeaddr(ifaddr.in.ia_subnet,
215 					INADDR_ANY);
216 				printf("%-13.13s ", netname(in.s_addr,
217 				    ifaddr.in.ia_subnetmask));
218 #else
219 				printf("%-13.13s ",
220 				    netname(htonl(ifaddr.in.ia_subnet),
221 				    ifaddr.in.ia_subnetmask));
222 #endif
223 				printf("%-15.15s ",
224 				    routename(sin->sin_addr.s_addr));
225 				break;
226 			case AF_IPX:
227 				{
228 				struct sockaddr_ipx *sipx =
229 					(struct sockaddr_ipx *)sa;
230 				u_long net;
231 				char netnum[10];
232 
233 				*(union ipx_net *) &net = sipx->sipx_addr.x_net;
234 				sprintf(netnum, "%lx", (u_long)ntohl(net));
235 				printf("ipx:%-8s  ", netnum);
236 /*				printf("ipx:%-8s ", netname(net, 0L)); */
237 				printf("%-15s ",
238 				    ipx_phost((struct sockaddr *)sipx));
239 				}
240 				break;
241 
242 			case AF_APPLETALK:
243 				printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
244 				printf("%-9.9s  ",atalk_print(sa,0x0b) );
245 				break;
246 #ifdef NS
247 			case AF_NS:
248 				{
249 				struct sockaddr_ns *sns =
250 					(struct sockaddr_ns *)sa;
251 				u_long net;
252 				char netnum[10];
253 
254 				*(union ns_net *) &net = sns->sns_addr.x_net;
255 				sprintf(netnum, "%lxH", ntohl(net));
256 				upHex(netnum);
257 				printf("ns:%-8s ", netnum);
258 				printf("%-15s ",
259 				    ns_phost((struct sockaddr *)sns));
260 				}
261 				break;
262 #endif
263 			case AF_LINK:
264 				{
265 				struct sockaddr_dl *sdl =
266 					(struct sockaddr_dl *)sa;
267 				    cp = (char *)LLADDR(sdl);
268 				    n = sdl->sdl_alen;
269 				}
270 				m = printf("%-11.11s ", "<Link>");
271 				goto hexprint;
272 			default:
273 				m = printf("(%d)", sa->sa_family);
274 				for (cp = sa->sa_len + (char *)sa;
275 					--cp > sa->sa_data && (*cp == 0);) {}
276 				n = cp - sa->sa_data + 1;
277 				cp = sa->sa_data;
278 			hexprint:
279 				while (--n >= 0)
280 					m += printf("%02x%c", *cp++ & 0xff,
281 						    n > 0 ? '.' : ' ');
282 				m = 30 - m;
283 				while (m-- > 0)
284 					putchar(' ');
285 				break;
286 			}
287 			ifaddraddr = (u_long)ifaddr.ifa.ifa_link.tqe_next;
288 		}
289 		printf("%8lu %5lu ",
290 		    ifnet.if_ipackets, ifnet.if_ierrors);
291 		if (bflag)
292 			printf("%10lu ", ifnet.if_ibytes);
293 		printf("%8lu %5lu ",
294 		    ifnet.if_opackets, ifnet.if_oerrors);
295 		if (bflag)
296 			printf("%10lu ", ifnet.if_obytes);
297 		printf("%5lu", ifnet.if_collisions);
298 		if (tflag)
299 			printf(" %3d", ifnet.if_timer);
300 		if (dflag)
301 			printf(" %3d", ifnet.if_snd.ifq_drops);
302 		putchar('\n');
303 		if (aflag && ifaddrfound) {
304 			/*
305 			 * Print family's multicast addresses
306 			 */
307 			u_long multiaddr;
308 			struct ifmultiaddr ifma;
309 			union {
310 				struct sockaddr sa;
311 				struct sockaddr_in in;
312 				struct sockaddr_dl dl;
313 			} msa;
314 			const char *fmt;
315 
316 			for(multiaddr = (u_long)ifnet.if_multiaddrs.lh_first;
317 			    multiaddr;
318 			    multiaddr = (u_long)ifma.ifma_link.le_next) {
319 				if (kread(multiaddr, (char *)&ifma,
320 					  sizeof ifma))
321 					break;
322 				if (kread((u_long)ifma.ifma_addr, (char *)&msa,
323 					  sizeof msa))
324 					break;
325 				if (msa.sa.sa_family != sa->sa_family)
326 					continue;
327 
328 				fmt = 0;
329 				switch (msa.sa.sa_family) {
330 				case AF_INET:
331 					fmt = routename(msa.in.sin_addr.s_addr);
332 					break;
333 
334 				case AF_LINK:
335 					switch (ifnet.if_type) {
336 					case IFT_ETHER:
337 					case IFT_FDDI:
338 						fmt = ether_ntoa(
339 							(struct ether_addr *)
340 							LLADDR(&msa.dl));
341 						break;
342 					}
343 					break;
344 				}
345 				if (fmt)
346 					printf("%23s %s\n", "", fmt);
347 			}
348 		}
349 	}
350 }
351 
352 #define	MAXIF	10
353 struct	iftot {
354 	char	ift_name[16];		/* interface name */
355 	u_int	ift_ip;			/* input packets */
356 	u_int	ift_ie;			/* input errors */
357 	u_int	ift_op;			/* output packets */
358 	u_int	ift_oe;			/* output errors */
359 	u_int	ift_co;			/* collisions */
360 	u_int	ift_dr;			/* drops */
361 	u_int	ift_ib;			/* input bytes */
362 	u_int	ift_ob;			/* output bytes */
363 } iftot[MAXIF];
364 
365 u_char	signalled;			/* set if alarm goes off "early" */
366 
367 /*
368  * Print a running summary of interface statistics.
369  * Repeat display every interval seconds, showing statistics
370  * collected over that interval.  Assumes that interval is non-zero.
371  * First line printed at top of screen is always cumulative.
372  * XXX - should be rewritten to use ifmib(4).
373  */
374 static void
375 sidewaysintpr(interval, off)
376 	unsigned interval;
377 	u_long off;
378 {
379 	struct ifnet ifnet;
380 	u_long firstifnet;
381 	struct ifnethead ifnethead;
382 	register struct iftot *ip, *total;
383 	register int line;
384 	struct iftot *lastif, *sum, *interesting;
385 	int oldmask, first;
386 	u_long interesting_off;
387 
388 	if (kread(off, (char *)&ifnethead, sizeof ifnethead))
389 		return;
390 	firstifnet = (u_long)ifnethead.tqh_first;
391 
392 	lastif = iftot;
393 	sum = iftot + MAXIF - 1;
394 	total = sum - 1;
395 	interesting = NULL;
396 	interesting_off = 0;
397 	for (off = firstifnet, ip = iftot; off;) {
398 		char name[16], tname[16];
399 
400 		if (kread(off, (char *)&ifnet, sizeof ifnet))
401 			break;
402 		if (kread((u_long)ifnet.if_name, tname, 16))
403 			break;
404 		tname[15] = '\0';
405 		snprintf(name, 16, "%s%d", tname, ifnet.if_unit);
406 		if (interface && strcmp(name, interface) == 0) {
407 			interesting = ip;
408 			interesting_off = off;
409 		}
410 		snprintf(ip->ift_name, 16, "(%s)", name);;
411 		ip++;
412 		if (ip >= iftot + MAXIF - 2)
413 			break;
414 		off = (u_long) ifnet.if_link.tqe_next;
415 	}
416 	lastif = ip;
417 
418 	(void)signal(SIGALRM, catchalarm);
419 	signalled = NO;
420 	(void)alarm(interval);
421 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
422 		ip->ift_ip = 0;
423 		ip->ift_ie = 0;
424 		ip->ift_ib = 0;
425 		ip->ift_op = 0;
426 		ip->ift_oe = 0;
427 		ip->ift_ob = 0;
428 		ip->ift_co = 0;
429 		ip->ift_dr = 0;
430 	}
431 	first = 1;
432 banner:
433 	printf("%17s %14s %16s", "input",
434 	    interesting ? interesting->ift_name : "(Total)", "output");
435 	putchar('\n');
436 	printf("%10s %5s %10s %10s %5s %10s %5s",
437 	    "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
438 	if (dflag)
439 		printf(" %5.5s", "drops");
440 	putchar('\n');
441 	fflush(stdout);
442 	line = 0;
443 loop:
444 	if (interesting != NULL) {
445 		ip = interesting;
446 		if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
447 			printf("???\n");
448 			exit(1);
449 		};
450 		if (!first) {
451 			printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
452 				ifnet.if_ipackets - ip->ift_ip,
453 				ifnet.if_ierrors - ip->ift_ie,
454 				ifnet.if_ibytes - ip->ift_ib,
455 				ifnet.if_opackets - ip->ift_op,
456 				ifnet.if_oerrors - ip->ift_oe,
457 				ifnet.if_obytes - ip->ift_ob,
458 				ifnet.if_collisions - ip->ift_co);
459 			if (dflag)
460 				printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
461 		}
462 		ip->ift_ip = ifnet.if_ipackets;
463 		ip->ift_ie = ifnet.if_ierrors;
464 		ip->ift_ib = ifnet.if_ibytes;
465 		ip->ift_op = ifnet.if_opackets;
466 		ip->ift_oe = ifnet.if_oerrors;
467 		ip->ift_ob = ifnet.if_obytes;
468 		ip->ift_co = ifnet.if_collisions;
469 		ip->ift_dr = ifnet.if_snd.ifq_drops;
470 	} else {
471 		sum->ift_ip = 0;
472 		sum->ift_ie = 0;
473 		sum->ift_ib = 0;
474 		sum->ift_op = 0;
475 		sum->ift_oe = 0;
476 		sum->ift_ob = 0;
477 		sum->ift_co = 0;
478 		sum->ift_dr = 0;
479 		for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
480 			if (kread(off, (char *)&ifnet, sizeof ifnet)) {
481 				off = 0;
482 				continue;
483 			}
484 			sum->ift_ip += ifnet.if_ipackets;
485 			sum->ift_ie += ifnet.if_ierrors;
486 			sum->ift_ib += ifnet.if_ibytes;
487 			sum->ift_op += ifnet.if_opackets;
488 			sum->ift_oe += ifnet.if_oerrors;
489 			sum->ift_ob += ifnet.if_obytes;
490 			sum->ift_co += ifnet.if_collisions;
491 			sum->ift_dr += ifnet.if_snd.ifq_drops;
492 			off = (u_long) ifnet.if_link.tqe_next;
493 		}
494 		if (!first) {
495 			printf("%10u %5u %10u %10u %5u %10u %5u",
496 				sum->ift_ip - total->ift_ip,
497 				sum->ift_ie - total->ift_ie,
498 				sum->ift_ib - total->ift_ib,
499 				sum->ift_op - total->ift_op,
500 				sum->ift_oe - total->ift_oe,
501 				sum->ift_ob - total->ift_ob,
502 				sum->ift_co - total->ift_co);
503 			if (dflag)
504 				printf(" %5u", sum->ift_dr - total->ift_dr);
505 		}
506 		*total = *sum;
507 	}
508 	if (!first)
509 		putchar('\n');
510 	fflush(stdout);
511 	oldmask = sigblock(sigmask(SIGALRM));
512 	if (! signalled) {
513 		sigpause(0);
514 	}
515 	sigsetmask(oldmask);
516 	signalled = NO;
517 	(void)alarm(interval);
518 	line++;
519 	first = 0;
520 	if (line == 21)
521 		goto banner;
522 	else
523 		goto loop;
524 	/*NOTREACHED*/
525 }
526 
527 /*
528  * Called if an interval expires before sidewaysintpr has completed a loop.
529  * Sets a flag to not wait for the alarm.
530  */
531 static void
532 catchalarm(signo)
533 	int signo;
534 {
535 	signalled = YES;
536 }
537