xref: /freebsd/usr.bin/netstat/if.c (revision 7f9d26bd9d1b2754da8429257edbde0a8237f84f)
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 <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 %9ld%9ld%9ld%9ld%9ld%9ld%9ld%9ld\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 = NULL;
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);
203 			sa = (struct sockaddr *)cp;
204 			switch (sa->sa_family) {
205 			case AF_UNSPEC:
206 				printf("%-13.13s ", "none");
207 				printf("%-15.15s ", "none");
208 				break;
209 			case AF_INET:
210 				sin = (struct sockaddr_in *)sa;
211 #ifdef notdef
212 				/* can't use inet_makeaddr because kernel
213 				 * keeps nets unshifted.
214 				 */
215 				in = inet_makeaddr(ifaddr.in.ia_subnet,
216 					INADDR_ANY);
217 				printf("%-13.13s ", netname(in.s_addr,
218 				    ifaddr.in.ia_subnetmask));
219 #else
220 				printf("%-13.13s ",
221 				    netname(htonl(ifaddr.in.ia_subnet),
222 				    ifaddr.in.ia_subnetmask));
223 #endif
224 				printf("%-15.15s ",
225 				    routename(sin->sin_addr.s_addr));
226 				break;
227 			case AF_IPX:
228 				{
229 				struct sockaddr_ipx *sipx =
230 					(struct sockaddr_ipx *)sa;
231 				u_long net;
232 				char netnum[10];
233 
234 				*(union ipx_net *) &net = sipx->sipx_addr.x_net;
235 				sprintf(netnum, "%lx", (u_long)ntohl(net));
236 				printf("ipx:%-8s  ", netnum);
237 /*				printf("ipx:%-8s ", netname(net, 0L)); */
238 				printf("%-15s ",
239 				    ipx_phost((struct sockaddr *)sipx));
240 				}
241 				break;
242 
243 			case AF_APPLETALK:
244 				printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
245 				printf("%-9.9s  ",atalk_print(sa,0x0b) );
246 				break;
247 #ifdef NS
248 			case AF_NS:
249 				{
250 				struct sockaddr_ns *sns =
251 					(struct sockaddr_ns *)sa;
252 				u_long net;
253 				char netnum[10];
254 
255 				*(union ns_net *) &net = sns->sns_addr.x_net;
256 				sprintf(netnum, "%lxH", ntohl(net));
257 				upHex(netnum);
258 				printf("ns:%-8s ", netnum);
259 				printf("%-15s ",
260 				    ns_phost((struct sockaddr *)sns));
261 				}
262 				break;
263 #endif
264 			case AF_LINK:
265 				{
266 				struct sockaddr_dl *sdl =
267 					(struct sockaddr_dl *)sa;
268 				    cp = (char *)LLADDR(sdl);
269 				    n = sdl->sdl_alen;
270 				}
271 				m = printf("%-11.11s ", "<Link>");
272 				goto hexprint;
273 			default:
274 				m = printf("(%d)", sa->sa_family);
275 				for (cp = sa->sa_len + (char *)sa;
276 					--cp > sa->sa_data && (*cp == 0);) {}
277 				n = cp - sa->sa_data + 1;
278 				cp = sa->sa_data;
279 			hexprint:
280 				while (--n >= 0)
281 					m += printf("%02x%c", *cp++ & 0xff,
282 						    n > 0 ? '.' : ' ');
283 				m = 30 - m;
284 				while (m-- > 0)
285 					putchar(' ');
286 				break;
287 			}
288 			ifaddraddr = (u_long)ifaddr.ifa.ifa_link.tqe_next;
289 		}
290 		printf("%8lu %5lu ",
291 		    ifnet.if_ipackets, ifnet.if_ierrors);
292 		if (bflag)
293 			printf("%10lu ", ifnet.if_ibytes);
294 		printf("%8lu %5lu ",
295 		    ifnet.if_opackets, ifnet.if_oerrors);
296 		if (bflag)
297 			printf("%10lu ", ifnet.if_obytes);
298 		printf("%5lu", ifnet.if_collisions);
299 		if (tflag)
300 			printf(" %3d", ifnet.if_timer);
301 		if (dflag)
302 			printf(" %3d", ifnet.if_snd.ifq_drops);
303 		putchar('\n');
304 		if (aflag && ifaddrfound) {
305 			/*
306 			 * Print family's multicast addresses
307 			 */
308 			u_long multiaddr;
309 			struct ifmultiaddr ifma;
310 			union {
311 				struct sockaddr sa;
312 				struct sockaddr_in in;
313 				struct sockaddr_dl dl;
314 			} msa;
315 			const char *fmt;
316 
317 			for(multiaddr = (u_long)ifnet.if_multiaddrs.lh_first;
318 			    multiaddr;
319 			    multiaddr = (u_long)ifma.ifma_link.le_next) {
320 				if (kread(multiaddr, (char *)&ifma,
321 					  sizeof ifma))
322 					break;
323 				if (kread((u_long)ifma.ifma_addr, (char *)&msa,
324 					  sizeof msa))
325 					break;
326 				if (msa.sa.sa_family != sa->sa_family)
327 					continue;
328 
329 				fmt = 0;
330 				switch (msa.sa.sa_family) {
331 				case AF_INET:
332 					fmt = routename(msa.in.sin_addr.s_addr);
333 					break;
334 
335 				case AF_LINK:
336 					switch (ifnet.if_type) {
337 					case IFT_ETHER:
338 					case IFT_FDDI:
339 						fmt = ether_ntoa(
340 							(struct ether_addr *)
341 							LLADDR(&msa.dl));
342 						break;
343 					}
344 					break;
345 				}
346 				if (fmt)
347 					printf("%23s %s\n", "", fmt);
348 			}
349 		}
350 	}
351 }
352 
353 #define	MAXIF	10
354 struct	iftot {
355 	char	ift_name[16];		/* interface name */
356 	u_int	ift_ip;			/* input packets */
357 	u_int	ift_ie;			/* input errors */
358 	u_int	ift_op;			/* output packets */
359 	u_int	ift_oe;			/* output errors */
360 	u_int	ift_co;			/* collisions */
361 	u_int	ift_dr;			/* drops */
362 	u_int	ift_ib;			/* input bytes */
363 	u_int	ift_ob;			/* output bytes */
364 } iftot[MAXIF];
365 
366 u_char	signalled;			/* set if alarm goes off "early" */
367 
368 /*
369  * Print a running summary of interface statistics.
370  * Repeat display every interval seconds, showing statistics
371  * collected over that interval.  Assumes that interval is non-zero.
372  * First line printed at top of screen is always cumulative.
373  * XXX - should be rewritten to use ifmib(4).
374  */
375 static void
376 sidewaysintpr(interval, off)
377 	unsigned interval;
378 	u_long off;
379 {
380 	struct ifnet ifnet;
381 	u_long firstifnet;
382 	struct ifnethead ifnethead;
383 	register struct iftot *ip, *total;
384 	register int line;
385 	struct iftot *lastif, *sum, *interesting;
386 	int oldmask, first;
387 	u_long interesting_off;
388 
389 	if (kread(off, (char *)&ifnethead, sizeof ifnethead))
390 		return;
391 	firstifnet = (u_long)ifnethead.tqh_first;
392 
393 	lastif = iftot;
394 	sum = iftot + MAXIF - 1;
395 	total = sum - 1;
396 	interesting = NULL;
397 	interesting_off = 0;
398 	for (off = firstifnet, ip = iftot; off;) {
399 		char name[16], tname[16];
400 
401 		if (kread(off, (char *)&ifnet, sizeof ifnet))
402 			break;
403 		if (kread((u_long)ifnet.if_name, tname, 16))
404 			break;
405 		tname[15] = '\0';
406 		snprintf(name, 16, "%s%d", tname, ifnet.if_unit);
407 		if (interface && strcmp(name, interface) == 0) {
408 			interesting = ip;
409 			interesting_off = off;
410 		}
411 		snprintf(ip->ift_name, 16, "(%s)", name);;
412 		ip++;
413 		if (ip >= iftot + MAXIF - 2)
414 			break;
415 		off = (u_long) ifnet.if_link.tqe_next;
416 	}
417 	lastif = ip;
418 
419 	(void)signal(SIGALRM, catchalarm);
420 	signalled = NO;
421 	(void)alarm(interval);
422 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
423 		ip->ift_ip = 0;
424 		ip->ift_ie = 0;
425 		ip->ift_ib = 0;
426 		ip->ift_op = 0;
427 		ip->ift_oe = 0;
428 		ip->ift_ob = 0;
429 		ip->ift_co = 0;
430 		ip->ift_dr = 0;
431 	}
432 	first = 1;
433 banner:
434 	printf("%17s %14s %16s", "input",
435 	    interesting ? interesting->ift_name : "(Total)", "output");
436 	putchar('\n');
437 	printf("%10s %5s %10s %10s %5s %10s %5s",
438 	    "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
439 	if (dflag)
440 		printf(" %5.5s", "drops");
441 	putchar('\n');
442 	fflush(stdout);
443 	line = 0;
444 loop:
445 	if (interesting != NULL) {
446 		ip = interesting;
447 		if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
448 			printf("???\n");
449 			exit(1);
450 		};
451 		if (!first) {
452 			printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
453 				ifnet.if_ipackets - ip->ift_ip,
454 				ifnet.if_ierrors - ip->ift_ie,
455 				ifnet.if_ibytes - ip->ift_ib,
456 				ifnet.if_opackets - ip->ift_op,
457 				ifnet.if_oerrors - ip->ift_oe,
458 				ifnet.if_obytes - ip->ift_ob,
459 				ifnet.if_collisions - ip->ift_co);
460 			if (dflag)
461 				printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
462 		}
463 		ip->ift_ip = ifnet.if_ipackets;
464 		ip->ift_ie = ifnet.if_ierrors;
465 		ip->ift_ib = ifnet.if_ibytes;
466 		ip->ift_op = ifnet.if_opackets;
467 		ip->ift_oe = ifnet.if_oerrors;
468 		ip->ift_ob = ifnet.if_obytes;
469 		ip->ift_co = ifnet.if_collisions;
470 		ip->ift_dr = ifnet.if_snd.ifq_drops;
471 	} else {
472 		sum->ift_ip = 0;
473 		sum->ift_ie = 0;
474 		sum->ift_ib = 0;
475 		sum->ift_op = 0;
476 		sum->ift_oe = 0;
477 		sum->ift_ob = 0;
478 		sum->ift_co = 0;
479 		sum->ift_dr = 0;
480 		for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
481 			if (kread(off, (char *)&ifnet, sizeof ifnet)) {
482 				off = 0;
483 				continue;
484 			}
485 			sum->ift_ip += ifnet.if_ipackets;
486 			sum->ift_ie += ifnet.if_ierrors;
487 			sum->ift_ib += ifnet.if_ibytes;
488 			sum->ift_op += ifnet.if_opackets;
489 			sum->ift_oe += ifnet.if_oerrors;
490 			sum->ift_ob += ifnet.if_obytes;
491 			sum->ift_co += ifnet.if_collisions;
492 			sum->ift_dr += ifnet.if_snd.ifq_drops;
493 			off = (u_long) ifnet.if_link.tqe_next;
494 		}
495 		if (!first) {
496 			printf("%10u %5u %10u %10u %5u %10u %5u",
497 				sum->ift_ip - total->ift_ip,
498 				sum->ift_ie - total->ift_ie,
499 				sum->ift_ib - total->ift_ib,
500 				sum->ift_op - total->ift_op,
501 				sum->ift_oe - total->ift_oe,
502 				sum->ift_ob - total->ift_ob,
503 				sum->ift_co - total->ift_co);
504 			if (dflag)
505 				printf(" %5u", sum->ift_dr - total->ift_dr);
506 		}
507 		*total = *sum;
508 	}
509 	if (!first)
510 		putchar('\n');
511 	fflush(stdout);
512 	oldmask = sigblock(sigmask(SIGALRM));
513 	if (! signalled) {
514 		sigpause(0);
515 	}
516 	sigsetmask(oldmask);
517 	signalled = NO;
518 	(void)alarm(interval);
519 	line++;
520 	first = 0;
521 	if (line == 21)
522 		goto banner;
523 	else
524 		goto loop;
525 	/*NOTREACHED*/
526 }
527 
528 /*
529  * Called if an interval expires before sidewaysintpr has completed a loop.
530  * Sets a flag to not wait for the alarm.
531  */
532 static void
533 catchalarm(signo)
534 	int signo;
535 {
536 	signalled = YES;
537 }
538