Annotation of src/usr.bin/netstat/if.c, Revision 1.15
1.15 ! denny 1: /* $OpenBSD: if.c,v 1.14 1997/06/30 03:11:53 millert Exp $ */
1.6 deraadt 2: /* $NetBSD: if.c,v 1.16.4.2 1996/06/07 21:46:46 thorpej Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1983, 1988, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "from: @(#)if.c 8.2 (Berkeley) 2/21/94";
40: #else
1.15 ! denny 41: static char *rcsid = "$OpenBSD: if.c,v 1.14 1997/06/30 03:11:53 millert Exp $";
1.1 deraadt 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>
1.5 deraadt 51: #include <net/if_types.h>
1.1 deraadt 52: #include <netinet/in.h>
53: #include <netinet/in_var.h>
1.11 millert 54: #include <netinet/if_ether.h>
1.1 deraadt 55: #include <netns/ns.h>
56: #include <netns/ns_if.h>
1.8 mickey 57: #include <netipx/ipx.h>
58: #include <netipx/ipx_if.h>
1.1 deraadt 59: #include <netiso/iso.h>
60: #include <netiso/iso_var.h>
61: #include <arpa/inet.h>
62:
1.13 millert 63: #include <limits.h>
1.1 deraadt 64: #include <signal.h>
65: #include <stdio.h>
66: #include <string.h>
67: #include <unistd.h>
68:
69: #include "netstat.h"
70:
71: #define YES 1
72: #define NO 0
73:
74: static void sidewaysintpr __P((u_int, u_long));
75: static void catchalarm __P((int));
76:
77: /*
78: * Print a description of the network interfaces.
1.3 deraadt 79: * NOTE: ifnetaddr is the location of the kernel global "ifnet",
80: * which is a TAILQ_HEAD.
1.1 deraadt 81: */
82: void
83: intpr(interval, ifnetaddr)
84: int interval;
85: u_long ifnetaddr;
86: {
87: struct ifnet ifnet;
88: union {
89: struct ifaddr ifa;
90: struct in_ifaddr in;
91: struct ns_ifaddr ns;
1.8 mickey 92: struct ipx_ifaddr ipx;
1.1 deraadt 93: struct iso_ifaddr iso;
94: } ifaddr;
95: u_long ifaddraddr;
96: struct sockaddr *sa;
1.3 deraadt 97: struct ifnet_head ifhead; /* TAILQ_HEAD */
98: char name[IFNAMSIZ];
1.1 deraadt 99:
100: if (ifnetaddr == 0) {
101: printf("ifnet: symbol not defined\n");
102: return;
103: }
104: if (interval) {
105: sidewaysintpr((unsigned)interval, ifnetaddr);
106: return;
107: }
1.3 deraadt 108:
109: /*
110: * Find the pointer to the first ifnet structure. Replace
111: * the pointer to the TAILQ_HEAD with the actual pointer
112: * to the first list element.
113: */
114: if (kread(ifnetaddr, (char *)&ifhead, sizeof ifhead))
1.1 deraadt 115: return;
1.3 deraadt 116: ifnetaddr = (u_long)ifhead.tqh_first;
117:
1.2 deraadt 118: printf("%-5.5s %-5.5s %-11.11s %-17.17s %8.8s %5.5s %8.8s %5.5s",
1.1 deraadt 119: "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
120: "Opkts", "Oerrs");
121: printf(" %5s", "Coll");
122: if (tflag)
123: printf(" %s", "Time");
124: if (dflag)
125: printf(" %s", "Drop");
126: putchar('\n');
127: ifaddraddr = 0;
128: while (ifnetaddr || ifaddraddr) {
129: struct sockaddr_in *sin;
130: register char *cp;
131: int n, m;
132:
133: if (ifaddraddr == 0) {
1.3 deraadt 134: if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
1.1 deraadt 135: return;
1.3 deraadt 136: bcopy(ifnet.if_xname, name, IFNAMSIZ);
137: name[IFNAMSIZ - 1] = '\0'; /* sanity */
1.1 deraadt 138: ifnetaddr = (u_long)ifnet.if_list.tqe_next;
1.3 deraadt 139: if (interface != 0 && strcmp(name, interface) != 0)
1.1 deraadt 140: continue;
1.10 millert 141: cp = strchr(name, '\0');
1.1 deraadt 142: if ((ifnet.if_flags & IFF_UP) == 0)
143: *cp++ = '*';
144: *cp = '\0';
145: ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first;
146: }
1.11 millert 147: printf("%-5.5s %-5ld ", name, ifnet.if_mtu);
1.1 deraadt 148: if (ifaddraddr == 0) {
149: printf("%-11.11s ", "none");
150: printf("%-15.15s ", "none");
151: } else {
152: if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
153: ifaddraddr = 0;
154: continue;
155: }
156: #define CP(x) ((char *)(x))
157: cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
158: CP(&ifaddr); sa = (struct sockaddr *)cp;
159: switch (sa->sa_family) {
160: case AF_UNSPEC:
161: printf("%-11.11s ", "none");
162: printf("%-17.17s ", "none");
163: break;
164: case AF_INET:
165: sin = (struct sockaddr_in *)sa;
166: #ifdef notdef
167: /* can't use inet_makeaddr because kernel
168: * keeps nets unshifted.
169: */
170: in = inet_makeaddr(ifaddr.in.ia_subnet,
171: INADDR_ANY);
172: printf("%-11.11s ", netname(in.s_addr,
173: ifaddr.in.ia_subnetmask));
174: #else
175: printf("%-11.11s ",
176: netname(ifaddr.in.ia_subnet,
177: ifaddr.in.ia_subnetmask));
178: #endif
179: printf("%-17.17s ",
180: routename(sin->sin_addr.s_addr));
181:
182: if (aflag) {
183: u_long multiaddr;
184: struct in_multi inm;
185:
186: multiaddr = (u_long)ifaddr.in.ia_multiaddrs.lh_first;
187: while (multiaddr != 0) {
188: kread(multiaddr, (char *)&inm,
189: sizeof inm);
1.14 millert 190: printf("\n%23s %-17.17s ", "",
1.1 deraadt 191: routename(inm.inm_addr.s_addr));
192: multiaddr = (u_long)inm.inm_list.le_next;
193: }
1.8 mickey 194: }
195: break;
196: case AF_IPX:
197: {
198: struct sockaddr_ipx *sipx =
199: (struct sockaddr_ipx *)sa;
200: u_long net;
201: char netnum[8];
202:
203: *(union ipx_net *) &net = sipx->sipx_addr.ipx_net;
1.12 millert 204: sprintf(netnum, "%xH", ntohl(net));
1.8 mickey 205: upHex(netnum);
206: printf("ipx:%-8s", netnum);
207: printf("%-17s ",
208: ipx_phost((struct sockaddr *)sipx));
1.1 deraadt 209: }
1.15 ! denny 210: break;
! 211: case AF_APPLETALK:
! 212: printf("atlk:%-12s",atalk_print(sa,0x10) );
! 213: printf("%-12s ",atalk_print(sa,0x0b) );
1.1 deraadt 214: break;
215: case AF_NS:
216: {
217: struct sockaddr_ns *sns =
218: (struct sockaddr_ns *)sa;
219: u_long net;
220: char netnum[8];
221:
222: *(union ns_net *) &net = sns->sns_addr.x_net;
1.12 millert 223: sprintf(netnum, "%xH", ntohl(net));
1.1 deraadt 224: upHex(netnum);
225: printf("ns:%-8s ", netnum);
226: printf("%-17s ",
227: ns_phost((struct sockaddr *)sns));
228: }
229: break;
230: case AF_LINK:
231: {
232: struct sockaddr_dl *sdl =
233: (struct sockaddr_dl *)sa;
1.5 deraadt 234: m = printf("%-11.11s ", "<Link>");
235: if (sdl->sdl_type == IFT_ETHER ||
236: sdl->sdl_type == IFT_FDDI)
237: printf("%-17.17s ",
1.11 millert 238: ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1.5 deraadt 239: else {
240: cp = (char *)LLADDR(sdl);
241: n = sdl->sdl_alen;
242: goto hexprint;
243: }
1.1 deraadt 244: }
1.5 deraadt 245: break;
1.1 deraadt 246: default:
247: m = printf("(%d)", sa->sa_family);
248: for (cp = sa->sa_len + (char *)sa;
249: --cp > sa->sa_data && (*cp == 0);) {}
250: n = cp - sa->sa_data + 1;
251: cp = sa->sa_data;
252: hexprint:
253: while (--n >= 0)
254: m += printf("%x%c", *cp++ & 0xff,
255: n > 0 ? '.' : ' ');
256: m = 30 - m;
257: while (m-- > 0)
258: putchar(' ');
259: break;
260: }
261: ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
262: }
1.11 millert 263: printf("%8ld %5ld %8ld %5ld %5ld",
1.1 deraadt 264: ifnet.if_ipackets, ifnet.if_ierrors,
265: ifnet.if_opackets, ifnet.if_oerrors,
266: ifnet.if_collisions);
267: if (tflag)
268: printf(" %3d", ifnet.if_timer);
269: if (dflag)
270: printf(" %3d", ifnet.if_snd.ifq_drops);
271: putchar('\n');
272: }
273: }
274:
1.4 deraadt 275: #define MAXIF 100
1.1 deraadt 276: struct iftot {
1.3 deraadt 277: char ift_name[IFNAMSIZ]; /* interface name */
1.1 deraadt 278: int ift_ip; /* input packets */
279: int ift_ie; /* input errors */
280: int ift_op; /* output packets */
281: int ift_oe; /* output errors */
282: int ift_co; /* collisions */
283: int ift_dr; /* drops */
284: } iftot[MAXIF];
285:
286: u_char signalled; /* set if alarm goes off "early" */
287:
288: /*
289: * Print a running summary of interface statistics.
290: * Repeat display every interval seconds, showing statistics
291: * collected over that interval. Assumes that interval is non-zero.
292: * First line printed at top of screen is always cumulative.
293: */
294: static void
295: sidewaysintpr(interval, off)
296: unsigned interval;
297: u_long off;
298: {
299: struct ifnet ifnet;
300: u_long firstifnet;
301: register struct iftot *ip, *total;
302: register int line;
303: struct iftot *lastif, *sum, *interesting;
1.3 deraadt 304: struct ifnet_head ifhead; /* TAILQ_HEAD */
1.1 deraadt 305: int oldmask;
306:
1.3 deraadt 307: /*
308: * Find the pointer to the first ifnet structure. Replace
309: * the pointer to the TAILQ_HEAD with the actual pointer
310: * to the first list element.
311: */
312: if (kread(off, (char *)&ifhead, sizeof ifhead))
1.1 deraadt 313: return;
1.3 deraadt 314: firstifnet = (u_long)ifhead.tqh_first;
315:
1.1 deraadt 316: lastif = iftot;
317: sum = iftot + MAXIF - 1;
318: total = sum - 1;
1.4 deraadt 319: interesting = (interface == NULL) ? iftot : NULL;
1.1 deraadt 320: for (off = firstifnet, ip = iftot; off;) {
321: if (kread(off, (char *)&ifnet, sizeof ifnet))
322: break;
1.6 deraadt 323: bzero(ip->ift_name, sizeof(ip->ift_name));
324: snprintf(ip->ift_name, IFNAMSIZ, "(%s)", ifnet.if_xname);
325: if (interface && strcmp(ifnet.if_xname, interface) == 0)
1.1 deraadt 326: interesting = ip;
327: ip++;
328: if (ip >= iftot + MAXIF - 2)
329: break;
330: off = (u_long)ifnet.if_list.tqe_next;
1.4 deraadt 331: }
332: if (interesting == NULL) {
333: fprintf(stderr, "%s: %s: unknown interface\n",
334: __progname, interface);
335: exit(1);
1.1 deraadt 336: }
337: lastif = ip;
338:
339: (void)signal(SIGALRM, catchalarm);
340: signalled = NO;
341: (void)alarm(interval);
342: banner:
343: printf(" input %-6.6s output ", interesting->ift_name);
344: if (lastif - iftot > 0) {
345: if (dflag)
346: printf(" ");
347: printf(" input (Total) output");
348: }
349: for (ip = iftot; ip < iftot + MAXIF; ip++) {
350: ip->ift_ip = 0;
351: ip->ift_ie = 0;
352: ip->ift_op = 0;
353: ip->ift_oe = 0;
354: ip->ift_co = 0;
355: ip->ift_dr = 0;
356: }
357: putchar('\n');
358: printf("%8.8s %5.5s %8.8s %5.5s %5.5s ",
359: "packets", "errs", "packets", "errs", "colls");
360: if (dflag)
361: printf("%5.5s ", "drops");
362: if (lastif - iftot > 0)
363: printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
364: "packets", "errs", "packets", "errs", "colls");
365: if (dflag)
366: printf(" %5.5s", "drops");
367: putchar('\n');
368: fflush(stdout);
369: line = 0;
370: loop:
371: sum->ift_ip = 0;
372: sum->ift_ie = 0;
373: sum->ift_op = 0;
374: sum->ift_oe = 0;
375: sum->ift_co = 0;
376: sum->ift_dr = 0;
377: for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
378: if (kread(off, (char *)&ifnet, sizeof ifnet)) {
379: off = 0;
380: continue;
381: }
382: if (ip == interesting) {
1.11 millert 383: printf("%8ld %5ld %8ld %5ld %5ld",
1.1 deraadt 384: ifnet.if_ipackets - ip->ift_ip,
385: ifnet.if_ierrors - ip->ift_ie,
386: ifnet.if_opackets - ip->ift_op,
387: ifnet.if_oerrors - ip->ift_oe,
388: ifnet.if_collisions - ip->ift_co);
389: if (dflag)
390: printf(" %5d",
391: ifnet.if_snd.ifq_drops - ip->ift_dr);
392: }
393: ip->ift_ip = ifnet.if_ipackets;
394: ip->ift_ie = ifnet.if_ierrors;
395: ip->ift_op = ifnet.if_opackets;
396: ip->ift_oe = ifnet.if_oerrors;
397: ip->ift_co = ifnet.if_collisions;
398: ip->ift_dr = ifnet.if_snd.ifq_drops;
399: sum->ift_ip += ip->ift_ip;
400: sum->ift_ie += ip->ift_ie;
401: sum->ift_op += ip->ift_op;
402: sum->ift_oe += ip->ift_oe;
403: sum->ift_co += ip->ift_co;
404: sum->ift_dr += ip->ift_dr;
405: off = (u_long)ifnet.if_list.tqe_next;
406: }
407: if (lastif - iftot > 0) {
408: printf(" %8d %5d %8d %5d %5d",
409: sum->ift_ip - total->ift_ip,
410: sum->ift_ie - total->ift_ie,
411: sum->ift_op - total->ift_op,
412: sum->ift_oe - total->ift_oe,
413: sum->ift_co - total->ift_co);
414: if (dflag)
415: printf(" %5d", sum->ift_dr - total->ift_dr);
416: }
417: *total = *sum;
418: putchar('\n');
419: fflush(stdout);
420: line++;
421: oldmask = sigblock(sigmask(SIGALRM));
422: if (! signalled) {
423: sigpause(0);
424: }
425: sigsetmask(oldmask);
426: signalled = NO;
427: (void)alarm(interval);
428: if (line == 21)
429: goto banner;
430: goto loop;
431: /*NOTREACHED*/
432: }
433:
434: /*
435: * Called if an interval expires before sidewaysintpr has completed a loop.
436: * Sets a flag to not wait for the alarm.
437: */
438: static void
439: catchalarm(signo)
440: int signo;
441: {
442: signalled = YES;
443: }