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