xref: /dragonfly/usr.bin/netstat/if.c (revision d22a69a4)
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  * @(#)if.c	8.3 (Berkeley) 4/28/95
34  * $FreeBSD: src/usr.bin/netstat/if.c,v 1.32.2.9 2001/09/17 14:35:46 ru Exp $
35  */
36 
37 #define _KERNEL_STRUCTURES
38 #include <sys/param.h>
39 #include <sys/protosw.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42 #include <sys/time.h>
43 
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_dl.h>
47 #include <net/if_types.h>
48 #include <net/ethernet.h>
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netproto/ipx/ipx.h>
52 #include <netproto/ipx/ipx_if.h>
53 #ifdef ISO
54 #include <netiso/iso.h>
55 #include <netiso/iso_var.h>
56 #endif
57 #include <arpa/inet.h>
58 
59 #include <signal.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 
65 #include "netstat.h"
66 
67 #define	YES	1
68 #define	NO	0
69 
70 static void sidewaysintpr (u_int, u_long);
71 static void catchalarm (int);
72 
73 #ifdef INET6
74 static char ntop_buf[INET6_ADDRSTRLEN];		/* for inet_ntop() */
75 #endif
76 
77 
78 /*
79  * Display a formatted value, or a '-' in the same space.
80  */
81 static void
82 show_stat(const char *fmt, int width, u_long value, short showvalue)
83 {
84 	char newfmt[32];
85 
86 	/* Construct the format string */
87 	if (showvalue) {
88 		sprintf(newfmt, "%%%d%s", width, fmt);
89 		printf(newfmt, value);
90 	} else {
91 		sprintf(newfmt, "%%%ds", width);
92 		printf(newfmt, "-");
93 	}
94 }
95 
96 
97 
98 /*
99  * Print a description of the network interfaces.
100  */
101 void
102 intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *))
103 {
104 	struct ifnet ifnet;
105 	struct ifaddr_container ifac;
106 	struct ifnethead ifnethead;
107 	union {
108 		struct ifaddr ifa;
109 		struct in_ifaddr in;
110 #ifdef INET6
111 		struct in6_ifaddr in6;
112 #endif
113 		struct ipx_ifaddr ipx;
114 #ifdef ISO
115 		struct iso_ifaddr iso;
116 #endif
117 	} ifaddr;
118 	u_long ifaddraddr;
119 	u_long ifaddrcont_addr;
120 	u_long ifaddrfound;
121 	u_long ifnetfound;
122 	u_long opackets;
123 	u_long ipackets;
124 	u_long obytes;
125 	u_long ibytes;
126 	u_long oerrors;
127 	u_long ierrors;
128 	u_long collisions;
129 	short timer;
130 	int drops;
131 	struct sockaddr *sa = NULL;
132 	char name[IFNAMSIZ];
133 	short network_layer;
134 	short link_layer;
135 
136 	if (ifnetaddr == 0) {
137 		printf("ifnet: symbol not defined\n");
138 		return;
139 	}
140 	if (interval1) {
141 		sidewaysintpr((unsigned)interval1, ifnetaddr);
142 		return;
143 	}
144 	if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
145 		return;
146 	ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
147 	if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
148 		return;
149 
150 	if (!pfunc) {
151 		printf("%-7.7s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
152 		       "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
153 		if (bflag)
154 			printf(" %10.10s","Ibytes");
155 		printf(" %8.8s %5.5s", "Opkts", "Oerrs");
156 		if (bflag)
157 			printf(" %10.10s","Obytes");
158 		printf(" %5s", "Coll");
159 		if (tflag)
160 			printf(" %s", "Time");
161 		if (dflag)
162 			printf(" %s", "Drop");
163 		putchar('\n');
164 	}
165 	ifaddraddr = 0;
166 	while (ifnetaddr || ifaddraddr) {
167 		struct sockaddr_in *sin;
168 #ifdef INET6
169 		struct sockaddr_in6 *sin6;
170 #endif
171 		char *cp;
172 		int n, m;
173 
174 		network_layer = 0;
175 		link_layer = 0;
176 
177 		if (ifaddraddr == 0) {
178 			struct ifaddrhead head;
179 
180 			ifnetfound = ifnetaddr;
181 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
182 				return;
183 			strlcpy(name, ifnet.if_xname, sizeof(name));
184 			ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
185 			if (interface != 0 && (strcmp(name, interface) != 0))
186 				continue;
187 			cp = strchr(name, '\0');
188 
189 			if (pfunc) {
190 				(*pfunc)(name);
191 				continue;
192 			}
193 
194 			if ((ifnet.if_flags&IFF_UP) == 0)
195 				*cp++ = '*';
196 			*cp = '\0';
197 
198 			if (kread((u_long)ifnet.if_addrheads,
199 				  (char *)&head, sizeof(head)))
200 				return;
201 
202 			ifaddrcont_addr =
203 				(u_long)TAILQ_FIRST(&head);
204 			if (ifaddrcont_addr == 0) {
205 				ifaddraddr = 0;
206 			} else {
207 				if (kread(ifaddrcont_addr, (char *)&ifac,
208 					  sizeof(ifac)))
209 					return;
210 				ifaddraddr = (u_long)ifac.ifa;
211 			}
212 		}
213 		ifaddrfound = ifaddraddr;
214 
215 		/*
216 		 * Get the interface stats.  These may get
217 		 * overriden below on a per-interface basis.
218 		 */
219 		opackets = ifnet.if_opackets;
220 		ipackets = ifnet.if_ipackets;
221 		obytes = ifnet.if_obytes;
222 		ibytes = ifnet.if_ibytes;
223 		oerrors = ifnet.if_oerrors;
224 		ierrors = ifnet.if_ierrors;
225 		collisions = ifnet.if_collisions;
226 		timer = ifnet.if_timer;
227 		drops = ifnet.if_snd.ifq_drops;
228 
229 		if (ifaddraddr == 0) {
230 			printf("%-7.7s %-5lu ", name, ifnet.if_mtu);
231 			printf("%-13.13s ", "none");
232 			printf("%-15.15s ", "none");
233 		} else {
234 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
235 				ifaddraddr = 0;
236 				continue;
237 			}
238 #define CP(x) ((char *)(x))
239 			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
240 				CP(&ifaddr);
241 			sa = (struct sockaddr *)cp;
242 			if (af != AF_UNSPEC && sa->sa_family != af) {
243 				ifaddrcont_addr =
244 					(u_long)TAILQ_NEXT(&ifac, ifa_link);
245 				if (ifaddrcont_addr == 0) {
246 					ifaddraddr = 0;
247 				} else {
248 					if (kread(ifaddrcont_addr,
249 					    (char *)&ifac, sizeof(ifac))) {
250 						ifaddraddr = 0;
251 						continue;
252 					}
253 					ifaddraddr = (u_long)ifac.ifa;
254 				}
255 				continue;
256 			}
257 			printf("%-7.7s %-5lu ", name, ifnet.if_mtu);
258 			switch (sa->sa_family) {
259 			case AF_UNSPEC:
260 				printf("%-13.13s ", "none");
261 				printf("%-15.15s ", "none");
262 				break;
263 			case AF_INET:
264 				sin = (struct sockaddr_in *)sa;
265 #ifdef notdef
266 				/* can't use inet_makeaddr because kernel
267 				 * keeps nets unshifted.
268 				 */
269 				in = inet_makeaddr(ifaddr.in.ia_subnet,
270 					INADDR_ANY);
271 				printf("%-13.13s ", netname(in.s_addr,
272 				    ifaddr.in.ia_subnetmask));
273 #else
274 				printf("%-13.13s ",
275 				    netname(htonl(ifaddr.in.ia_subnet),
276 				    ifaddr.in.ia_subnetmask));
277 #endif
278 				printf("%-15.15s ",
279 				    routename(sin->sin_addr.s_addr));
280 
281 				network_layer = 1;
282 				break;
283 #ifdef INET6
284 			case AF_INET6:
285 				sin6 = (struct sockaddr_in6 *)sa;
286 				printf("%-11.11s ",
287 				       netname6(&ifaddr.in6.ia_addr,
288 						&ifaddr.in6.ia_prefixmask.sin6_addr));
289 				printf("%-17.17s ",
290 				    inet_ntop(AF_INET6,
291 					&sin6->sin6_addr,
292 					ntop_buf, sizeof(ntop_buf)));
293 
294 				network_layer = 1;
295 				break;
296 #endif /*INET6*/
297 			case AF_IPX:
298 				{
299 				struct sockaddr_ipx *sipx =
300 					(struct sockaddr_ipx *)sa;
301 				u_long net;
302 				char netnum[10];
303 
304 				*(union ipx_net *) &net = sipx->sipx_addr.x_net;
305 				sprintf(netnum, "%lx", (u_long)ntohl(net));
306 				printf("ipx:%-8s  ", netnum);
307 /*				printf("ipx:%-8s ", netname(net, 0L)); */
308 				printf("%-15s ",
309 				    ipx_phost((struct sockaddr *)sipx));
310 				}
311 				break;
312 
313 			case AF_LINK:
314 				{
315 				struct sockaddr_dl *sdl =
316 					(struct sockaddr_dl *)sa;
317 				char linknum[10];
318 				cp = (char *)LLADDR(sdl);
319 				n = sdl->sdl_alen;
320 				sprintf(linknum, "<Link#%d>", sdl->sdl_index);
321 				m = printf("%-11.11s ", linknum);
322 				}
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("%02x%c", *cp++ & 0xff,
333 						    n > 0 ? ':' : ' ');
334 				m = 30 - m;
335 				while (m-- > 0)
336 					putchar(' ');
337 
338 				link_layer = 1;
339 				break;
340 			}
341 
342 			/*
343 			 * Fixup the statistics for interfaces that
344 			 * update stats for their network addresses
345 			 */
346 			if (network_layer) {
347 				opackets = ifaddr.in.ia_ifa.if_opackets;
348 				ipackets = ifaddr.in.ia_ifa.if_ipackets;
349 				obytes = ifaddr.in.ia_ifa.if_obytes;
350 				ibytes = ifaddr.in.ia_ifa.if_ibytes;
351 			}
352 
353 			ifaddrcont_addr =
354 				(u_long)TAILQ_NEXT(&ifac, ifa_link);
355 			if (ifaddrcont_addr == 0) {
356 				ifaddraddr = 0;
357 			} else {
358 				if (kread(ifaddrcont_addr,
359 				    (char *)&ifac, sizeof(ifac))) {
360 					ifaddraddr = 0;
361 				} else {
362 					ifaddraddr = (u_long)ifac.ifa;
363 				}
364 			}
365 		}
366 
367 		show_stat("lu", 8, ipackets, link_layer|network_layer);
368 		printf(" ");
369 		show_stat("lu", 5, ierrors, link_layer);
370 		printf(" ");
371 		if (bflag) {
372 			show_stat("lu", 10, ibytes, link_layer|network_layer);
373 			printf(" ");
374 		}
375 		show_stat("lu", 8, opackets, link_layer|network_layer);
376 		printf(" ");
377 		show_stat("lu", 5, oerrors, link_layer);
378 		printf(" ");
379 		if (bflag) {
380 			show_stat("lu", 10, obytes, link_layer|network_layer);
381 			printf(" ");
382 		}
383 		show_stat("lu", 5, collisions, link_layer);
384 		if (tflag) {
385 			printf(" ");
386 			show_stat("d", 3, timer, link_layer);
387 		}
388 		if (dflag) {
389 			printf(" ");
390 			show_stat("d", 3, drops, link_layer);
391 		}
392 		putchar('\n');
393 		if (aflag && ifaddrfound) {
394 			/*
395 			 * Print family's multicast addresses
396 			 */
397 			struct ifmultiaddr *multiaddr;
398 			struct ifmultiaddr ifma;
399 			union {
400 				struct sockaddr sa;
401 				struct sockaddr_in in;
402 #ifdef INET6
403 				struct sockaddr_in6 in6;
404 #endif /* INET6 */
405 				struct sockaddr_dl dl;
406 			} msa;
407 			const char *fmt;
408 
409 			TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
410 				if (kread((u_long)multiaddr, (char *)&ifma,
411 					  sizeof ifma))
412 					break;
413 				multiaddr = &ifma;
414 				if (kread((u_long)ifma.ifma_addr, (char *)&msa,
415 					  sizeof msa))
416 					break;
417 				if (msa.sa.sa_family != sa->sa_family)
418 					continue;
419 
420 				fmt = 0;
421 				switch (msa.sa.sa_family) {
422 				case AF_INET:
423 					fmt = routename(msa.in.sin_addr.s_addr);
424 					break;
425 #ifdef INET6
426 				case AF_INET6:
427 					printf("%23s %-19.19s(refs: %d)\n", "",
428 					       inet_ntop(AF_INET6,
429 							 &msa.in6.sin6_addr,
430 							 ntop_buf,
431 							 sizeof(ntop_buf)),
432 					       ifma.ifma_refcount);
433 					break;
434 #endif /* INET6 */
435 				case AF_LINK:
436 					switch (msa.dl.sdl_type) {
437 					case IFT_ETHER:
438 					case IFT_FDDI:
439 						fmt = ether_ntoa(
440 							(struct ether_addr *)
441 							LLADDR(&msa.dl));
442 						break;
443 					}
444 					break;
445 				}
446 				if (fmt)
447 					printf("%23s %s\n", "", fmt);
448 			}
449 		}
450 	}
451 }
452 
453 struct	iftot {
454 	SLIST_ENTRY(iftot) chain;
455 	char	ift_name[IFNAMSIZ];	/* interface name */
456 	u_long	ift_ip;			/* input packets */
457 	u_long	ift_ie;			/* input errors */
458 	u_long	ift_op;			/* output packets */
459 	u_long	ift_oe;			/* output errors */
460 	u_long	ift_co;			/* collisions */
461 	u_int	ift_dr;			/* drops */
462 	u_long	ift_ib;			/* input bytes */
463 	u_long	ift_ob;			/* output bytes */
464 };
465 
466 u_char	signalled;			/* set if alarm goes off "early" */
467 
468 /*
469  * Print a running summary of interface statistics.
470  * Repeat display every interval1 seconds, showing statistics
471  * collected over that interval.  Assumes that interval1 is non-zero.
472  * First line printed at top of screen is always cumulative.
473  * XXX - should be rewritten to use ifmib(4).
474  */
475 static void
476 sidewaysintpr(unsigned interval1, u_long off)
477 {
478 	struct ifnet ifnet;
479 	u_long firstifnet;
480 	struct ifnethead ifnethead;
481 	struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
482 	int line;
483 	int oldmask, first;
484 	u_long interesting_off;
485 
486 	if (kread(off, (char *)&ifnethead, sizeof ifnethead))
487 		return;
488 	firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
489 
490 	if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
491 		printf("malloc failed\n");
492 		exit(1);
493 	}
494 	memset(iftot, 0, sizeof(struct iftot));
495 
496 	interesting = NULL;
497 	interesting_off = 0;
498 	for (off = firstifnet, ip = iftot; off;) {
499 		char name[IFNAMSIZ];
500 
501 		if (kread(off, (char *)&ifnet, sizeof ifnet))
502 			break;
503 		strlcpy(name, ifnet.if_xname, sizeof(name));
504 		if (interface && strcmp(name, interface) == 0) {
505 			interesting = ip;
506 			interesting_off = off;
507 		}
508 		snprintf(ip->ift_name, 16, "(%s)", name);
509 		if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
510 			printf("malloc failed\n");
511 			exit(1);
512 		}
513 		memset(ipn, 0, sizeof(struct iftot));
514 		SLIST_NEXT(ip, chain) = ipn;
515 		ip = ipn;
516 		off = (u_long)TAILQ_NEXT(&ifnet, if_link);
517 	}
518 	if ((total = malloc(sizeof(struct iftot))) == NULL) {
519 		printf("malloc failed\n");
520 		exit(1);
521 	}
522 	memset(total, 0, sizeof(struct iftot));
523 	if ((sum = malloc(sizeof(struct iftot))) == NULL) {
524 		printf("malloc failed\n");
525 		exit(1);
526 	}
527 	memset(sum, 0, sizeof(struct iftot));
528 
529 
530 	(void)signal(SIGALRM, catchalarm);
531 	signalled = NO;
532 	(void)alarm(interval1);
533 	first = 1;
534 banner:
535 	printf("%17s %14s %16s", "input",
536 	    interesting ? interesting->ift_name : "(Total)", "output");
537 	putchar('\n');
538 	printf("%10s %5s %10s %10s %5s %10s %5s",
539 	    "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
540 	if (dflag)
541 		printf(" %5.5s", "drops");
542 	putchar('\n');
543 	fflush(stdout);
544 	line = 0;
545 loop:
546 	if (interesting != NULL) {
547 		ip = interesting;
548 		if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
549 			printf("???\n");
550 			exit(1);
551 		};
552 		if (!first) {
553 			printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
554 				ifnet.if_ipackets - ip->ift_ip,
555 				ifnet.if_ierrors - ip->ift_ie,
556 				ifnet.if_ibytes - ip->ift_ib,
557 				ifnet.if_opackets - ip->ift_op,
558 				ifnet.if_oerrors - ip->ift_oe,
559 				ifnet.if_obytes - ip->ift_ob,
560 				ifnet.if_collisions - ip->ift_co);
561 			if (dflag)
562 				printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
563 		}
564 		ip->ift_ip = ifnet.if_ipackets;
565 		ip->ift_ie = ifnet.if_ierrors;
566 		ip->ift_ib = ifnet.if_ibytes;
567 		ip->ift_op = ifnet.if_opackets;
568 		ip->ift_oe = ifnet.if_oerrors;
569 		ip->ift_ob = ifnet.if_obytes;
570 		ip->ift_co = ifnet.if_collisions;
571 		ip->ift_dr = ifnet.if_snd.ifq_drops;
572 	} else {
573 		sum->ift_ip = 0;
574 		sum->ift_ie = 0;
575 		sum->ift_ib = 0;
576 		sum->ift_op = 0;
577 		sum->ift_oe = 0;
578 		sum->ift_ob = 0;
579 		sum->ift_co = 0;
580 		sum->ift_dr = 0;
581 		for (off = firstifnet, ip = iftot;
582 		     off && SLIST_NEXT(ip, chain) != NULL;
583 		     ip = SLIST_NEXT(ip, chain)) {
584 			if (kread(off, (char *)&ifnet, sizeof ifnet)) {
585 				off = 0;
586 				continue;
587 			}
588 			sum->ift_ip += ifnet.if_ipackets;
589 			sum->ift_ie += ifnet.if_ierrors;
590 			sum->ift_ib += ifnet.if_ibytes;
591 			sum->ift_op += ifnet.if_opackets;
592 			sum->ift_oe += ifnet.if_oerrors;
593 			sum->ift_ob += ifnet.if_obytes;
594 			sum->ift_co += ifnet.if_collisions;
595 			sum->ift_dr += ifnet.if_snd.ifq_drops;
596 			off = (u_long)TAILQ_NEXT(&ifnet, if_link);
597 		}
598 		if (!first) {
599 			printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
600 				sum->ift_ip - total->ift_ip,
601 				sum->ift_ie - total->ift_ie,
602 				sum->ift_ib - total->ift_ib,
603 				sum->ift_op - total->ift_op,
604 				sum->ift_oe - total->ift_oe,
605 				sum->ift_ob - total->ift_ob,
606 				sum->ift_co - total->ift_co);
607 			if (dflag)
608 				printf(" %5u", sum->ift_dr - total->ift_dr);
609 		}
610 		*total = *sum;
611 	}
612 	if (!first)
613 		putchar('\n');
614 	fflush(stdout);
615 	oldmask = sigblock(sigmask(SIGALRM));
616 	if (! signalled) {
617 		sigpause(0);
618 	}
619 	sigsetmask(oldmask);
620 	signalled = NO;
621 	(void)alarm(interval1);
622 	line++;
623 	first = 0;
624 	if (line == 21)
625 		goto banner;
626 	else
627 		goto loop;
628 	/*NOTREACHED*/
629 }
630 
631 /*
632  * Called if an interval expires before sidewaysintpr has completed a loop.
633  * Sets a flag to not wait for the alarm.
634  */
635 static void
636 catchalarm(int signo __unused)
637 {
638 	signalled = YES;
639 }
640